uart: Replace Init calls with Enable/Disable
* Enable/Disable makes more sense and matches platform loader serial functions. * Rework PL011 code after finding a PDF covering the details of it. * Rename UART global defines in loader to be more exact about location
This commit is contained in:
@@ -37,6 +37,9 @@ public:
|
|||||||
void Init();
|
void Init();
|
||||||
void InitPort(uint32 baud);
|
void InitPort(uint32 baud);
|
||||||
|
|
||||||
|
void Enable();
|
||||||
|
void Disable();
|
||||||
|
|
||||||
int PutChar(char c);
|
int PutChar(char c);
|
||||||
int GetChar(bool wait);
|
int GetChar(bool wait);
|
||||||
|
|
||||||
@@ -47,6 +50,7 @@ private:
|
|||||||
void WriteUart(uint32 reg, unsigned char data);
|
void WriteUart(uint32 reg, unsigned char data);
|
||||||
unsigned char ReadUart(uint32 reg);
|
unsigned char ReadUart(uint32 reg);
|
||||||
|
|
||||||
|
bool fUARTEnabled;
|
||||||
addr_t fUARTBase;
|
addr_t fUARTBase;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -93,9 +93,11 @@ public:
|
|||||||
~UartPL011();
|
~UartPL011();
|
||||||
|
|
||||||
void InitEarly();
|
void InitEarly();
|
||||||
void Init();
|
|
||||||
void InitPort(uint32 baud);
|
void InitPort(uint32 baud);
|
||||||
|
|
||||||
|
void Enable();
|
||||||
|
void Disable();
|
||||||
|
|
||||||
int PutChar(char c);
|
int PutChar(char c);
|
||||||
int GetChar(bool wait);
|
int GetChar(bool wait);
|
||||||
|
|
||||||
@@ -106,6 +108,7 @@ private:
|
|||||||
void WriteUart(uint32 reg, unsigned char data);
|
void WriteUart(uint32 reg, unsigned char data);
|
||||||
unsigned char ReadUart(uint32 reg);
|
unsigned char ReadUart(uint32 reg);
|
||||||
|
|
||||||
|
bool fUARTEnabled;
|
||||||
addr_t fUARTBase;
|
addr_t fUARTBase;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
UartPL011* gUART;
|
UartPL011* gLoaderUART;
|
||||||
|
|
||||||
static int32 sSerialEnabled = 0;
|
static int32 sSerialEnabled = 0;
|
||||||
static char sBuffer[16384];
|
static char sBuffer[16384];
|
||||||
@@ -27,7 +27,7 @@ static uint32 sBufferPosition;
|
|||||||
static void
|
static void
|
||||||
serial_putc(char c)
|
serial_putc(char c)
|
||||||
{
|
{
|
||||||
gUART->PutChar(c);
|
gLoaderUART->PutChar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -80,13 +80,12 @@ serial_cleanup(void)
|
|||||||
extern "C" void
|
extern "C" void
|
||||||
serial_init(void)
|
serial_init(void)
|
||||||
{
|
{
|
||||||
gUART = new(nothrow) UartPL011(uart_base_debug());
|
gLoaderUART = new(nothrow) UartPL011(uart_base_debug());
|
||||||
if (gUART == 0)
|
if (gLoaderUART == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
gUART->InitEarly();
|
gLoaderUART->InitEarly();
|
||||||
gUART->Init();
|
gLoaderUART->InitPort(9600);
|
||||||
gUART->InitPort(9600);
|
|
||||||
|
|
||||||
serial_enable();
|
serial_enable();
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
gUart8250* gUART;
|
Uart8250* gLoaderUART;
|
||||||
|
|
||||||
static int32 sSerialEnabled = 0;
|
static int32 sSerialEnabled = 0;
|
||||||
static char sBuffer[16384];
|
static char sBuffer[16384];
|
||||||
@@ -26,7 +26,7 @@ static uint32 sBufferPosition;
|
|||||||
static void
|
static void
|
||||||
serial_putc(char c)
|
serial_putc(char c)
|
||||||
{
|
{
|
||||||
gUART->PutChar(c);
|
gLoaderUART->PutChar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -67,9 +67,8 @@ serial_enable(void)
|
|||||||
{
|
{
|
||||||
/* should already be initialized by U-Boot */
|
/* should already be initialized by U-Boot */
|
||||||
/*
|
/*
|
||||||
gUART->InitEarly();
|
gLoaderUART->InitEarly();
|
||||||
gUART->Init();
|
gLoaderUART->InitPort(9600);
|
||||||
gUART->InitPort(9600);
|
|
||||||
*/
|
*/
|
||||||
sSerialEnabled++;
|
sSerialEnabled++;
|
||||||
}
|
}
|
||||||
@@ -93,7 +92,9 @@ extern "C" void
|
|||||||
serial_init(void)
|
serial_init(void)
|
||||||
{
|
{
|
||||||
// Setup information on uart
|
// Setup information on uart
|
||||||
gUART = new Uart8250(uart_base_debug());
|
gLoaderUART = new(nothrow) Uart8250(uart_base_debug());
|
||||||
|
if (gLoaderUART == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
serial_enable();
|
serial_enable();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
Uart8250::Uart8250(addr_t base)
|
Uart8250::Uart8250(addr_t base)
|
||||||
:
|
:
|
||||||
|
fUARTEnabled(true),
|
||||||
fUARTBase(base)
|
fUARTBase(base)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -95,6 +96,8 @@ Uart8250::ReadUart(uint32 reg)
|
|||||||
void
|
void
|
||||||
Uart8250::InitPort(uint32 baud)
|
Uart8250::InitPort(uint32 baud)
|
||||||
{
|
{
|
||||||
|
Disable();
|
||||||
|
|
||||||
uint16 baudDivisor = BOARD_UART_CLOCK / (16 * baud);
|
uint16 baudDivisor = BOARD_UART_CLOCK / (16 * baud);
|
||||||
|
|
||||||
// Write standard uart settings
|
// Write standard uart settings
|
||||||
@@ -118,6 +121,8 @@ Uart8250::InitPort(uint32 baud)
|
|||||||
// WriteUart(UART_LCR, 0xBF); // config mode B
|
// WriteUart(UART_LCR, 0xBF); // config mode B
|
||||||
// WriteUart(UART_EFR, (1<<7)|(1<<6)); // hw flow control
|
// WriteUart(UART_EFR, (1<<7)|(1<<6)); // hw flow control
|
||||||
// WriteUart(UART_LCR, LCR_8N1); // operational mode
|
// WriteUart(UART_LCR, LCR_8N1); // operational mode
|
||||||
|
|
||||||
|
Enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -145,9 +150,16 @@ Uart8250::InitEarly()
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
Uart8250::Init()
|
Uart8250::Enable()
|
||||||
{
|
{
|
||||||
InitPort(115200);
|
fUARTEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
Uart8250::Disable()
|
||||||
|
{
|
||||||
|
fUARTEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
UartPL011::UartPL011(addr_t base)
|
UartPL011::UartPL011(addr_t base)
|
||||||
:
|
:
|
||||||
|
fUARTEnabled(true),
|
||||||
fUARTBase(base)
|
fUARTBase(base)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -43,28 +44,22 @@ UartPL011::ReadUart(uint32 reg)
|
|||||||
void
|
void
|
||||||
UartPL011::InitPort(uint32 baud)
|
UartPL011::InitPort(uint32 baud)
|
||||||
{
|
{
|
||||||
uint16 clockDiv
|
// Disable UART
|
||||||
= (baud > BOARD_UART_CLOCK / 16) ? 8 : 4;
|
Disable();
|
||||||
uint16 baudDivisor = BOARD_UART_CLOCK * clockDiv / baud;
|
|
||||||
// TODO: Round to closest baud divisor
|
|
||||||
uint16 lcr = PL01x_LCRH_WLEN_8;
|
|
||||||
|
|
||||||
// Disable everything
|
// Calculate baud divisor
|
||||||
unsigned char originalCR
|
uint16 baudDivisor = BOARD_UART_CLOCK / (16 * baud);
|
||||||
= ReadUart(PL011_CR);
|
uint16 baudFractional = BOARD_UART_CLOCK % (16 * baud);
|
||||||
WriteUart(PL011_CR, 0);
|
|
||||||
|
|
||||||
// Set baud divisor
|
// Set baud divisor
|
||||||
WriteUart(PL011_FBRD, baudDivisor & 0x3f);
|
WriteUart(PL011_IBRD, baudDivisor);
|
||||||
WriteUart(PL011_IBRD, baudDivisor >> 6);
|
WriteUart(PL011_FBRD, baudFractional);
|
||||||
|
|
||||||
// Set LCR
|
// Set LCR
|
||||||
WriteUart(PL011_LCRH, lcr);
|
WriteUart(PL011_LCRH, PL01x_LCRH_WLEN_8);
|
||||||
|
|
||||||
// Disable auto RTS / CTS in original CR
|
// Enable UART
|
||||||
originalCR &= ~(PL011_CR_CTSEN | PL011_CR_RTSEN);
|
Enable();
|
||||||
|
|
||||||
WriteUart(PL011_CR, originalCR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -77,36 +72,48 @@ UartPL011::InitEarly()
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
UartPL011::Init()
|
UartPL011::Enable()
|
||||||
{
|
{
|
||||||
// TODO: Enable clock producer?
|
// TODO: Enable clock producer?
|
||||||
// TODO: Clear pending error and receive interrupts
|
|
||||||
|
|
||||||
// Provoke TX FIFO into asserting
|
unsigned char cr = PL011_CR_UARTEN;
|
||||||
uint32 cr = PL011_CR_UARTEN | PL011_CR_TXE | PL011_IFLS;
|
// Enable UART
|
||||||
|
cr |= PL011_CR_TXE; // | PL011_CR_RXE;
|
||||||
|
// Enable TX and RX
|
||||||
WriteUart(PL011_CR, cr);
|
WriteUart(PL011_CR, cr);
|
||||||
WriteUart(PL011_FBRD, 0);
|
|
||||||
WriteUart(PL011_IBRD, 1);
|
|
||||||
|
|
||||||
// TODO: For arm vendor, st different rx vs tx
|
// TODO: For arm vendor, st different rx vs tx
|
||||||
WriteUart(PL011_LCRH, 0);
|
// WriteUart(PL011_LCRH, 0);
|
||||||
|
|
||||||
WriteUart(PL01x_DR, 0);
|
WriteUart(PL01x_DR, 0);
|
||||||
|
|
||||||
while (ReadUart(PL01x_FR) & PL01x_FR_BUSY);
|
while (ReadUart(PL01x_FR) & PL01x_FR_BUSY);
|
||||||
// Wait for xmit
|
// Wait for xmit
|
||||||
|
|
||||||
|
fUARTEnabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
UartPL011::Disable()
|
||||||
|
{
|
||||||
|
// Disable everything
|
||||||
|
WriteUart(PL011_CR, 0);
|
||||||
|
fUARTEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
UartPL011::PutChar(char c)
|
UartPL011::PutChar(char c)
|
||||||
{
|
{
|
||||||
WriteUart(PL01x_DR, (unsigned int)c);
|
if (fUARTEnabled == true) {
|
||||||
|
WriteUart(PL01x_DR, (unsigned int)c);
|
||||||
|
while (ReadUart(PL01x_FR) & PL01x_FR_TXFF);
|
||||||
|
// wait for the last char to get out
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
while (ReadUart(PL01x_FR) & PL01x_FR_TXFF);
|
return -1;
|
||||||
// wait for the last char to get out
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user