#include <arch/generic/debug_uart_8250.h>
#include <debug.h>
#include <new>
DebugUART8250::DebugUART8250(addr_t base, int64 clock)
:
DebugUART(base, clock)
{
}
DebugUART8250::~DebugUART8250()
{
}
#define UART_RHR 0
#define UART_THR 0
#define UART_DLL 0
#define UART_IER 1
#define UART_DLH 1
#define UART_IIR 2
#define UART_FCR 2
#define UART_EFR 2
#define UART_LCR 3
#define UART_MCR 4
#define UART_LSR 5
#define UART_MSR 6
#define UART_TCR 6
#define UART_SPR 7
#define UART_TLR 7
#define LCR_8N1 0x03
#define FCR_FIFO_EN 0x01
#define FCR_RXSR 0x02
#define FCR_TXSR 0x04
#define MCR_DTR 0x01
#define MCR_RTS 0x02
#define MCR_DMA_EN 0x04
#define MCR_TX_DFR 0x08
#define LCR_WLS_MSK 0x03
#define LCR_WLS_5 0x00
#define LCR_WLS_6 0x01
#define LCR_WLS_7 0x02
#define LCR_WLS_8 0x03
#define LCR_STB 0x04
#define LCR_PEN 0x08
#define LCR_EPS 0x10
#define LCR_STKP 0x20
#define LCR_SBRK 0x40
#define LCR_BKSE 0x80
#define LSR_DR 0x01
#define LSR_OE 0x02
#define LSR_PE 0x04
#define LSR_FE 0x08
#define LSR_BI 0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_ERR 0x80
void
DebugUART8250::InitPort(uint32 baud)
{
Disable();
uint16 baudDivisor = Clock() / (16 * baud);
Out8(UART_LCR, LCR_8N1);
Out8(UART_IER, 0);
Out8(UART_FCR, 0);
Out8(UART_MCR, MCR_DTR | MCR_RTS);
unsigned char buffer = In8(UART_LCR);
Out8(UART_LCR, buffer | LCR_BKSE);
Out8(UART_DLL, baudDivisor & 0xff);
Out8(UART_DLH, (baudDivisor >> 8) & 0xff);
Out8(UART_LCR, buffer & ~LCR_BKSE);
Enable();
}
void
DebugUART8250::InitEarly()
{
}
void
DebugUART8250::Init()
{
}
int
DebugUART8250::PutChar(char c)
{
int32 timeout = 256 * 1024;
while (!(In8(UART_LSR) & (1<<6))) {
if (--timeout == 0)
return -1;
}
Out8(UART_THR, c);
return 0;
}
int
DebugUART8250::GetChar(bool wait)
{
if (wait) {
while (!(In8(UART_LSR) & (1<<0)));
} else {
if (!(In8(UART_LSR) & (1<<0)))
return -1;
}
return In8(UART_RHR);
}
void
DebugUART8250::FlushTx()
{
while (!(In8(UART_LSR) & (1<<6)));
}
void
DebugUART8250::FlushRx()
{
while (In8(UART_LSR) & (1<<0)) {
volatile char c = In8(UART_RHR);
(void)c;
}
}
DebugUART8250*
arch_get_uart_8250(addr_t base, int64 clock)
{
static char buffer[sizeof(DebugUART8250)];
DebugUART8250* uart = new(buffer) DebugUART8250(base, clock);
return uart;
}