mb/google/brya: Create rull variant
[coreboot2.git] / src / drivers / uart / uart8250mem.c
blob19677a8a6adb3d647016858c73c6832c81695a45
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
4 #include <boot/coreboot_tables.h>
5 #include <console/uart.h>
6 #include <device/device.h>
7 #include <delay.h>
8 #include <stdint.h>
9 #include "uart8250reg.h"
11 /* Should support 8250, 16450, 16550, 16550A type UARTs */
13 /* Expected character delay at 1200bps is 9ms for a working UART
14 * and no flow-control. Assume UART as stuck if shift register
15 * or FIFO takes more than 50ms per character to appear empty.
17 #define SINGLE_CHAR_TIMEOUT (50 * 1000)
18 #define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT)
20 #if CONFIG(DRIVERS_UART_8250MEM_32)
21 static uint8_t uart8250_read(void *base, uint8_t reg)
23 return read32(base + 4 * reg) & 0xff;
26 static void uart8250_write(void *base, uint8_t reg, uint8_t data)
28 write32(base + 4 * reg, data);
30 #else
31 static uint8_t uart8250_read(void *base, uint8_t reg)
33 return read8(base + reg);
36 static void uart8250_write(void *base, uint8_t reg, uint8_t data)
38 write8(base + reg, data);
40 #endif
42 static int uart8250_mem_can_tx_byte(void *base)
44 return uart8250_read(base, UART8250_LSR) & UART8250_LSR_THRE;
47 static void uart8250_mem_tx_byte(void *base, unsigned char data)
49 unsigned long int i = SINGLE_CHAR_TIMEOUT;
50 while (i-- && !uart8250_mem_can_tx_byte(base))
51 udelay(1);
52 uart8250_write(base, UART8250_TBR, data);
55 static void uart8250_mem_tx_flush(void *base)
57 unsigned long int i = FIFO_TIMEOUT;
58 while (i-- && !(uart8250_read(base, UART8250_LSR) & UART8250_LSR_TEMT))
59 udelay(1);
62 static int uart8250_mem_can_rx_byte(void *base)
64 return uart8250_read(base, UART8250_LSR) & UART8250_LSR_DR;
67 static unsigned char uart8250_mem_rx_byte(void *base)
69 unsigned long int i = SINGLE_CHAR_TIMEOUT;
70 while (i && !uart8250_mem_can_rx_byte(base)) {
71 udelay(1);
72 i--;
74 if (i)
75 return uart8250_read(base, UART8250_RBR);
76 else
77 return 0x0;
80 static void uart8250_mem_init(void *base, unsigned int divisor)
82 /* Disable interrupts */
83 uart8250_write(base, UART8250_IER, 0x0);
84 /* Enable FIFOs */
85 uart8250_write(base, UART8250_FCR, UART8250_FCR_FIFO_EN);
87 /* Assert DTR and RTS so the other end is happy */
88 uart8250_write(base, UART8250_MCR, UART8250_MCR_DTR | UART8250_MCR_RTS);
90 /* DLAB on */
91 uart8250_write(base, UART8250_LCR, UART8250_LCR_DLAB | CONFIG_TTYS0_LCS);
93 uart8250_write(base, UART8250_DLL, divisor & 0xFF);
94 uart8250_write(base, UART8250_DLM, (divisor >> 8) & 0xFF);
96 /* Set to 3 for 8N1 */
97 uart8250_write(base, UART8250_LCR, CONFIG_TTYS0_LCS);
100 void uart_init(unsigned int idx)
102 void *base = uart_platform_baseptr(idx);
103 if (!base)
104 return;
106 unsigned int div;
107 div = uart_baudrate_divisor(get_uart_baudrate(),
108 uart_platform_refclk(), uart_input_clock_divider());
109 uart8250_mem_init(base, div);
112 void uart_tx_byte(unsigned int idx, unsigned char data)
114 void *base = uart_platform_baseptr(idx);
115 if (!base)
116 return;
117 uart8250_mem_tx_byte(base, data);
120 unsigned char uart_rx_byte(unsigned int idx)
122 void *base = uart_platform_baseptr(idx);
123 if (!base)
124 return 0xff;
125 return uart8250_mem_rx_byte(base);
128 void uart_tx_flush(unsigned int idx)
130 void *base = uart_platform_baseptr(idx);
131 if (!base)
132 return;
133 uart8250_mem_tx_flush(base);
136 enum cb_err fill_lb_serial(struct lb_serial *serial)
138 serial->type = LB_SERIAL_TYPE_MEMORY_MAPPED;
139 serial->baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE);
140 if (!serial->baseaddr)
141 return CB_ERR;
142 serial->baud = get_uart_baudrate();
143 if (CONFIG(DRIVERS_UART_8250MEM_32))
144 serial->regwidth = sizeof(uint32_t);
145 else
146 serial->regwidth = sizeof(uint8_t);
147 serial->input_hertz = uart_platform_refclk();
149 return CB_SUCCESS;