From b74906293b1e8749c46ce0972693a06e45053908 Mon Sep 17 00:00:00 2001 From: Alexander von Gluck IV Date: Sun, 6 May 2012 22:53:56 -0500 Subject: [PATCH] pl011 uart: Add port_init code * Add code to initilize the uart port * Fix uart clock --- .../arm/board/raspberry_pi/board_config.h | 4 +-- headers/private/kernel/arch/arm/uart_pl011.h | 11 ++++++++ .../boot/platform/raspberrypi_arm/serial.cpp | 3 +- src/system/kernel/arch/arm/uart_pl011.cpp | 28 +++++++++++++++---- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/headers/private/kernel/arch/arm/board/raspberry_pi/board_config.h b/headers/private/kernel/arch/arm/board/raspberry_pi/board_config.h index 626dadf3e8..53937546e5 100644 --- a/headers/private/kernel/arch/arm/board/raspberry_pi/board_config.h +++ b/headers/private/kernel/arch/arm/board/raspberry_pi/board_config.h @@ -24,8 +24,8 @@ #define BOARD_UART_DEBUG BOARD_UART2_BASE -#define BOARD_UART_CLOCK 125000000 - /* 125Mhz, strange */ +#define BOARD_UART_CLOCK 3000000 + /* 3Mhz */ #endif /* _BOARD_RASPBERRY_PI_BOARD_CONFIG_H */ diff --git a/headers/private/kernel/arch/arm/uart_pl011.h b/headers/private/kernel/arch/arm/uart_pl011.h index b09b373b1f..5510dbc88e 100644 --- a/headers/private/kernel/arch/arm/uart_pl011.h +++ b/headers/private/kernel/arch/arm/uart_pl011.h @@ -72,6 +72,17 @@ #define PL011_CR_SIREN 0x0002 // SIR enable #define PL011_CR_UARTEN 0x0001 // UART enable +#define PL011_LCRH_SPS 0x80 +#define PL01x_LCRH_WLEN_8 0x60 +#define PL01x_LCRH_WLEN_7 0x40 +#define PL01x_LCRH_WLEN_6 0x20 +#define PL01x_LCRH_WLEN_5 0x00 +#define PL01x_LCRH_FEN 0x10 +#define PL01x_LCRH_STP2 0x08 +#define PL01x_LCRH_EPS 0x04 +#define PL01x_LCRH_PEN 0x02 +#define PL01x_LCRH_BRK 0x01 + // TODO: Other PL01x registers + values? diff --git a/src/system/boot/platform/raspberrypi_arm/serial.cpp b/src/system/boot/platform/raspberrypi_arm/serial.cpp index 243dc64958..4c2e77fa8d 100644 --- a/src/system/boot/platform/raspberrypi_arm/serial.cpp +++ b/src/system/boot/platform/raspberrypi_arm/serial.cpp @@ -89,7 +89,8 @@ serial_init(void) } gUARTInfo->init_early(); - gUARTInfo->init(gUARTInfo->base); + gUARTInfo->init_port(gUARTInfo->base, 9600); + //gUARTInfo->init(gUARTInfo->base); serial_enable(); diff --git a/src/system/kernel/arch/arm/uart_pl011.cpp b/src/system/kernel/arch/arm/uart_pl011.cpp index 52b2af1906..d57a21f8c1 100644 --- a/src/system/kernel/arch/arm/uart_pl011.cpp +++ b/src/system/kernel/arch/arm/uart_pl011.cpp @@ -31,6 +31,28 @@ read_pl011(addr_t base, uint reg) void uart_pl011_init_port(addr_t base, uint baud) { + uint16 clockDiv + = (baud > BOARD_UART_CLOCK / 16) ? 8 : 4; + uint16 baudDivisor = BOARD_UART_CLOCK * clockDiv / baud; + // TODO: Round to closest baud divisor + uint16 lcr = PL01x_LCRH_WLEN_8; + + // Disable everything + unsigned char originalCR + = read_pl011(base, PL011_CR); + write_pl011(base, PL011_CR, 0); + + // Set baud divisor + write_pl011(base, PL011_FBRD, baudDivisor & 0x3f); + write_pl011(base, PL011_IBRD, baudDivisor >> 6); + + // Set LCR + write_pl011(base, PL011_LCRH, lcr); + + // Disable auto RTS / CTS in original CR + originalCR &= ~(PL011_CR_CTSEN | PL011_CR_RTSEN); + + write_pl011(base, PL011_CR, originalCR); } @@ -60,12 +82,6 @@ uart_pl011_init(addr_t base) while (read_pl011(base, PL01x_FR) & PL01x_FR_BUSY); // Wait for xmit - - // Write baud divider - #if 0 - write_pl011(base, PL011_FBRD, div & 0x3F); - write_pl011(base, PL011_IBRD, div >> 6); - #endif }