| 1 |
|
| 2 |
|
| 3 |
#include <types.h> |
| 4 |
#include <console/uart.h> |
| 5 |
#include <device/mmio.h> |
| 6 |
#include <boot/coreboot_tables.h> |
| 7 |
#include <soc/ti/am335x/uart.h> |
| 8 |
|
| 9 |
#define EFR_ENHANCED_EN (1 << 4) |
| 10 |
#define FCR_FIFO_EN (1 << 0) |
| 11 |
#define MCR_TCR_TLR (1 << 6) |
| 12 |
#define SYSC_SOFTRESET (1 << 1) |
| 13 |
#define SYSS_RESETDONE (1 << 0) |
| 14 |
|
| 15 |
#define LSR_RXFIFOE (1 << 0) |
| 16 |
#define LSR_TXFIFOE (1 << 5) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
static void am335x_uart_init(struct am335x_uart *uart, uint16_t div) |
| 23 |
{ |
| 24 |
uint16_t lcr_orig, efr_orig, mcr_orig; |
| 25 |
|
| 26 |
|
| 27 |
write16(&uart->sysc, uart->sysc | SYSC_SOFTRESET); |
| 28 |
while (!(read16(&uart->syss) & SYSS_RESETDONE)) |
| 29 |
; |
| 30 |
|
| 31 |
|
| 32 |
lcr_orig = read16(&uart->lcr); |
| 33 |
write16(&uart->lcr, 0xbf); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
efr_orig = read16(&uart->efr); |
| 40 |
write16(&uart->efr, efr_orig | EFR_ENHANCED_EN); |
| 41 |
|
| 42 |
|
| 43 |
write16(&uart->lcr, 0x80); |
| 44 |
|
| 45 |
|
| 46 |
mcr_orig = read16(&uart->mcr); |
| 47 |
write16(&uart->mcr, mcr_orig | MCR_TCR_TLR); |
| 48 |
|
| 49 |
|
| 50 |
write16(&uart->fcr, FCR_FIFO_EN); |
| 51 |
|
| 52 |
|
| 53 |
write16(&uart->lcr, 0xbf); |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
write16(&uart->efr, efr_orig); |
| 58 |
|
| 59 |
|
| 60 |
write16(&uart->lcr, 0x80); |
| 61 |
|
| 62 |
|
| 63 |
write16(&uart->mcr, mcr_orig); |
| 64 |
|
| 65 |
|
| 66 |
write16(&uart->lcr, lcr_orig); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
write16(&uart->mdr1, read16(&uart->mdr1) | 0x7); |
| 72 |
|
| 73 |
|
| 74 |
write16(&uart->lcr, 0xbf); |
| 75 |
|
| 76 |
|
| 77 |
write16(&uart->efr, efr_orig | EFR_ENHANCED_EN); |
| 78 |
|
| 79 |
|
| 80 |
write16(&uart->lcr, 0x0); |
| 81 |
|
| 82 |
|
| 83 |
write16(&uart->ier, 0x0); |
| 84 |
|
| 85 |
|
| 86 |
write16(&uart->lcr, 0xbf); |
| 87 |
|
| 88 |
|
| 89 |
write16(&uart->dlh, (div >> 8)); |
| 90 |
write16(&uart->dll, (div & 0xff)); |
| 91 |
|
| 92 |
|
| 93 |
write16(&uart->lcr, 0x0); |
| 94 |
|
| 95 |
|
| 96 |
write16(&uart->ier, 0x0); |
| 97 |
|
| 98 |
|
| 99 |
write16(&uart->lcr, 0xbf); |
| 100 |
|
| 101 |
|
| 102 |
write16(&uart->efr, efr_orig); |
| 103 |
|
| 104 |
|
| 105 |
write16(&uart->lcr, 0x3); |
| 106 |
|
| 107 |
|
| 108 |
write16(&uart->mdr1, 0x0); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static unsigned char am335x_uart_rx_byte(struct am335x_uart *uart) |
| 117 |
{ |
| 118 |
while (!(read16(&uart->lsr) & LSR_RXFIFOE)); |
| 119 |
|
| 120 |
return read8(&uart->rhr); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static void am335x_uart_tx_byte(struct am335x_uart *uart, unsigned char data) |
| 127 |
{ |
| 128 |
while (!(read16(&uart->lsr) & LSR_TXFIFOE)); |
| 129 |
|
| 130 |
return write8(&uart->thr, data); |
| 131 |
} |
| 132 |
|
| 133 |
unsigned int uart_platform_refclk(void) |
| 134 |
{ |
| 135 |
return 48000000; |
| 136 |
} |
| 137 |
|
| 138 |
uintptr_t uart_platform_base(unsigned int idx) |
| 139 |
{ |
| 140 |
const unsigned int bases[] = { |
| 141 |
0x44e09000, 0x48022000, 0x48024000, |
| 142 |
0x481a6000, 0x481a8000, 0x481aa000 |
| 143 |
}; |
| 144 |
if (idx < ARRAY_SIZE(bases)) |
| 145 |
return bases[idx]; |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
void uart_init(unsigned int idx) |
| 150 |
{ |
| 151 |
struct am335x_uart *uart = uart_platform_baseptr(idx); |
| 152 |
uint16_t div = (uint16_t)uart_baudrate_divisor( |
| 153 |
get_uart_baudrate(), uart_platform_refclk(), 16); |
| 154 |
am335x_uart_init(uart, div); |
| 155 |
} |
| 156 |
|
| 157 |
unsigned char uart_rx_byte(unsigned int idx) |
| 158 |
{ |
| 159 |
struct am335x_uart *uart = uart_platform_baseptr(idx); |
| 160 |
return am335x_uart_rx_byte(uart); |
| 161 |
} |
| 162 |
|
| 163 |
void uart_tx_byte(unsigned int idx, unsigned char data) |
| 164 |
{ |
| 165 |
struct am335x_uart *uart = uart_platform_baseptr(idx); |
| 166 |
am335x_uart_tx_byte(uart, data); |
| 167 |
} |
| 168 |
|
| 169 |
void uart_tx_flush(unsigned int idx) |
| 170 |
{ |
| 171 |
} |
| 172 |
|
| 173 |
enum cb_err fill_lb_serial(struct lb_serial *serial) |
| 174 |
{ |
| 175 |
serial->type = LB_SERIAL_TYPE_MEMORY_MAPPED; |
| 176 |
serial->baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE); |
| 177 |
serial->baud = get_uart_baudrate(); |
| 178 |
serial->regwidth = 2; |
| 179 |
serial->input_hertz = uart_platform_refclk(); |
| 180 |
|
| 181 |
return CB_SUCCESS; |
| 182 |
} |