/*
 * serial.h
 *
 * Copyright (C) 2015 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _SERIAL_H_
#define _SERIAL_H_

#include <common.h>
#include <device.h>
#include <semaphore.h>

#define SERIAL_BAUD_FREQ 115200

#define SERIAL_ODD_IRQ  4
#define SERIAL_EVEN_IRQ 3

#define SERIAL_DATA 0
#define SERIAL_IER  1
#define SERIAL_DLR  0
#define SERIAL_DHR  1
#define SERIAL_IIR  2
#define SERIAL_FCR  2
#define SERIAL_LCR  3
#define SERIAL_MCR  4
#define SERIAL_LSR  5
#define SERIAL_MSR  6

#define SERIAL_DLAB_BIT (1 << 7)

#define SERIAL_RECV_RDY_BIT     (1 << 0)
#define SERIAL_SEND_RDY_BIT     (1 << 5)

#define SERIAL_FIFO_ENABLED_BIT    (1 << 0)
#define SERIAL_RECV_FIFO_FLUSH_BIT (1 << 1)
#define SERIAL_SEND_FIFO_FLUSH_BIT (1 << 2)

#define IOCTL_SERIAL_SET_BAUD_RATE 0xC1230000

typedef struct
{
    device_t header;
    word_t port;
    semaphore_t read_mutex;
    semaphore_t write_mutex;
} serial_port_t;

dword_t serial_driver_load(void);
dword_t serial_driver_unload(void);

#endif
