Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 2 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 3 | #include <arch/io.h> |
Kyösti Mälkki | c8cf591 | 2018-06-04 06:02:01 +0300 | [diff] [blame] | 4 | #include <boot/coreboot_tables.h> |
Elyes Haouas | 69451f1 | 2022-10-07 10:08:53 +0200 | [diff] [blame] | 5 | #include <commonlib/bsd/helpers.h> |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 6 | #include <console/uart.h> |
Elyes Haouas | 69451f1 | 2022-10-07 10:08:53 +0200 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
Kyösti Mälkki | bea6bf0 | 2014-01-30 15:45:16 +0200 | [diff] [blame] | 9 | #include "uart8250reg.h" |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 10 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 11 | /* Should support 8250, 16450, 16550, 16550A type UARTs */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 12 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 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. |
| 16 | * |
| 17 | * Estimated that inb() from UART takes 1 microsecond. |
| 18 | */ |
| 19 | #define SINGLE_CHAR_TIMEOUT (50 * 1000) |
| 20 | #define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT) |
| 21 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 22 | static int uart8250_can_tx_byte(unsigned int base_port) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 23 | { |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 24 | return inb(base_port + UART8250_LSR) & UART8250_LSR_THRE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 27 | static void uart8250_tx_byte(unsigned int base_port, unsigned char data) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 28 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 29 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 30 | while (i-- && !uart8250_can_tx_byte(base_port)); |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 31 | outb(data, base_port + UART8250_TBR); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 34 | static void uart8250_tx_flush(unsigned int base_port) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 35 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 36 | unsigned long int i = FIFO_TIMEOUT; |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 37 | while (i-- && !(inb(base_port + UART8250_LSR) & UART8250_LSR_TEMT)); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 40 | static int uart8250_can_rx_byte(unsigned int base_port) |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 41 | { |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 42 | return inb(base_port + UART8250_LSR) & UART8250_LSR_DR; |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 45 | static unsigned char uart8250_rx_byte(unsigned int base_port) |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 46 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 47 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
Werner Zeh | 43314ff | 2017-05-17 10:13:24 +0200 | [diff] [blame] | 48 | while (i && !uart8250_can_rx_byte(base_port)) |
| 49 | i--; |
Patrick Georgi | 472efa6 | 2012-02-16 20:44:20 +0100 | [diff] [blame] | 50 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 51 | if (i) |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 52 | return inb(base_port + UART8250_RBR); |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 53 | else |
| 54 | return 0x0; |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 57 | static void uart8250_init(unsigned int base_port, unsigned int divisor) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 58 | { |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 59 | /* Disable interrupts */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 60 | outb(0x0, base_port + UART8250_IER); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 61 | /* Enable FIFOs */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 62 | outb(UART8250_FCR_FIFO_EN, base_port + UART8250_FCR); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 63 | |
Stefan Reinauer | ebafa4d | 2006-10-07 00:13:24 +0000 | [diff] [blame] | 64 | /* assert DTR and RTS so the other end is happy */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 65 | outb(UART8250_MCR_DTR | UART8250_MCR_RTS, base_port + UART8250_MCR); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 66 | |
| 67 | /* DLAB on */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 68 | outb(UART8250_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART8250_LCR); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 69 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 70 | /* Set Baud Rate Divisor. 12 ==> 9600 Baud */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 71 | outb(divisor & 0xFF, base_port + UART8250_DLL); |
| 72 | outb((divisor >> 8) & 0xFF, base_port + UART8250_DLM); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 73 | |
| 74 | /* Set to 3 for 8N1 */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 75 | outb(CONFIG_TTYS0_LCS, base_port + UART8250_LCR); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 76 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 77 | |
Martin Roth | 38ddbfb | 2019-10-23 21:41:00 -0600 | [diff] [blame] | 78 | static const unsigned int bases[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 }; |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 79 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 80 | uintptr_t uart_platform_base(unsigned int idx) |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 81 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 82 | if (idx < ARRAY_SIZE(bases)) |
| 83 | return bases[idx]; |
| 84 | return 0; |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 87 | void uart_init(unsigned int idx) |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 88 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 89 | if (!CONFIG(DRIVERS_UART_8250IO_SKIP_INIT)) { |
Nico Huber | bc97b4e | 2016-09-28 22:15:07 +0200 | [diff] [blame] | 90 | unsigned int div; |
Julien Viard de Galbert | 235daa4 | 2018-02-20 11:45:48 +0100 | [diff] [blame] | 91 | div = uart_baudrate_divisor(get_uart_baudrate(), |
Nico Huber | bc97b4e | 2016-09-28 22:15:07 +0200 | [diff] [blame] | 92 | uart_platform_refclk(), uart_input_clock_divider()); |
| 93 | uart8250_init(uart_platform_base(idx), div); |
| 94 | } |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 97 | void uart_tx_byte(unsigned int idx, unsigned char data) |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 98 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 99 | uart8250_tx_byte(uart_platform_base(idx), data); |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 102 | unsigned char uart_rx_byte(unsigned int idx) |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 103 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 104 | return uart8250_rx_byte(uart_platform_base(idx)); |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 107 | void uart_tx_flush(unsigned int idx) |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 108 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 109 | uart8250_tx_flush(uart_platform_base(idx)); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 110 | } |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 111 | |
Arthur Heymans | 9948c52 | 2022-10-24 14:37:40 +0200 | [diff] [blame] | 112 | enum cb_err fill_lb_serial(struct lb_serial *serial) |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 113 | { |
Arthur Heymans | 9948c52 | 2022-10-24 14:37:40 +0200 | [diff] [blame] | 114 | serial->type = LB_SERIAL_TYPE_IO_MAPPED; |
| 115 | serial->baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE); |
| 116 | serial->baud = get_uart_baudrate(); |
| 117 | serial->regwidth = 1; |
| 118 | serial->input_hertz = uart_platform_refclk(); |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 119 | |
Arthur Heymans | 9948c52 | 2022-10-24 14:37:40 +0200 | [diff] [blame] | 120 | return CB_SUCCESS; |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 121 | } |