Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2003 Eric Biederman |
| 5 | * Copyright (C) 2006-2010 coresystems GmbH |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
Kyösti Mälkki | 4566d2e | 2014-04-23 10:28:59 +0300 | [diff] [blame^] | 21 | #include <rules.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 22 | #include <arch/io.h> |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 23 | #include <console/uart.h> |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 24 | #include <trace.h> |
Kyösti Mälkki | bea6bf0 | 2014-01-30 15:45:16 +0200 | [diff] [blame] | 25 | #include "uart8250reg.h" |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 26 | |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 27 | #ifndef __ROMCC__ |
| 28 | #include <boot/coreboot_tables.h> |
| 29 | #endif |
| 30 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 31 | /* Should support 8250, 16450, 16550, 16550A type UARTs */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 32 | |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 33 | /* Nominal values only, good for the range of choices Kconfig offers for |
| 34 | * set of standard baudrates. |
| 35 | */ |
| 36 | #define BAUDRATE_REFCLK (115200) |
| 37 | #define BAUDRATE_OVERSAMPLE (1) |
| 38 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 39 | /* Expected character delay at 1200bps is 9ms for a working UART |
| 40 | * and no flow-control. Assume UART as stuck if shift register |
| 41 | * or FIFO takes more than 50ms per character to appear empty. |
| 42 | * |
| 43 | * Estimated that inb() from UART takes 1 microsecond. |
| 44 | */ |
| 45 | #define SINGLE_CHAR_TIMEOUT (50 * 1000) |
| 46 | #define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT) |
| 47 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 48 | static int uart8250_can_tx_byte(unsigned base_port) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 49 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 50 | return inb(base_port + UART_LSR) & UART_LSR_THRE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 53 | static void uart8250_tx_byte(unsigned base_port, unsigned char data) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 54 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 55 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 56 | while (i-- && !uart8250_can_tx_byte(base_port)); |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 57 | outb(data, base_port + UART_TBR); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 60 | static void uart8250_tx_flush(unsigned base_port) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 61 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 62 | unsigned long int i = FIFO_TIMEOUT; |
| 63 | while (i-- && !(inb(base_port + UART_LSR) & UART_LSR_TEMT)); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 66 | static int uart8250_can_rx_byte(unsigned base_port) |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 67 | { |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 68 | return inb(base_port + UART_LSR) & UART_LSR_DR; |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 71 | static unsigned char uart8250_rx_byte(unsigned base_port) |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 72 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 73 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 74 | while (i-- && !uart8250_can_rx_byte(base_port)); |
Patrick Georgi | 472efa6 | 2012-02-16 20:44:20 +0100 | [diff] [blame] | 75 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 76 | if (i) |
| 77 | return inb(base_port + UART_RBR); |
| 78 | else |
| 79 | return 0x0; |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 82 | static void uart8250_init(unsigned base_port, unsigned divisor) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 83 | { |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 84 | DISABLE_TRACE; |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 85 | /* Disable interrupts */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 86 | outb(0x0, base_port + UART_IER); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 87 | /* Enable FIFOs */ |
| 88 | outb(UART_FCR_FIFO_EN, base_port + UART_FCR); |
| 89 | |
Stefan Reinauer | ebafa4d | 2006-10-07 00:13:24 +0000 | [diff] [blame] | 90 | /* assert DTR and RTS so the other end is happy */ |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 91 | outb(UART_MCR_DTR | UART_MCR_RTS, base_port + UART_MCR); |
| 92 | |
| 93 | /* DLAB on */ |
| 94 | outb(UART_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART_LCR); |
| 95 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 96 | /* Set Baud Rate Divisor. 12 ==> 9600 Baud */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 97 | outb(divisor & 0xFF, base_port + UART_DLL); |
| 98 | outb((divisor >> 8) & 0xFF, base_port + UART_DLM); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 99 | |
| 100 | /* Set to 3 for 8N1 */ |
| 101 | outb(CONFIG_TTYS0_LCS, base_port + UART_LCR); |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 102 | ENABLE_TRACE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 103 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 104 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 105 | /* FIXME: Needs uart index from Kconfig. |
| 106 | * Already use array as a work-around for ROMCC. |
| 107 | */ |
| 108 | static const unsigned bases[1] = { CONFIG_TTYS0_BASE }; |
| 109 | |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 110 | unsigned int uart_platform_base(int idx) |
| 111 | { |
| 112 | return bases[idx]; |
| 113 | } |
| 114 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 115 | void uart_init(void) |
| 116 | { |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 117 | unsigned int div; |
| 118 | div = uart_baudrate_divisor(default_baudrate(), BAUDRATE_REFCLK, |
| 119 | BAUDRATE_OVERSAMPLE); |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 120 | uart8250_init(bases[0], div); |
| 121 | } |
| 122 | |
| 123 | void uart_tx_byte(unsigned char data) |
| 124 | { |
| 125 | uart8250_tx_byte(bases[0], data); |
| 126 | } |
| 127 | |
| 128 | unsigned char uart_rx_byte(void) |
| 129 | { |
| 130 | return uart8250_rx_byte(bases[0]); |
| 131 | } |
| 132 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 133 | void uart_tx_flush(void) |
| 134 | { |
| 135 | uart8250_tx_flush(bases[0]); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 136 | } |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 137 | |
Kyösti Mälkki | 4566d2e | 2014-04-23 10:28:59 +0300 | [diff] [blame^] | 138 | #if ENV_RAMSTAGE |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 139 | void uart_fill_lb(void *data) |
| 140 | { |
| 141 | struct lb_serial serial; |
| 142 | serial.type = LB_SERIAL_TYPE_IO_MAPPED; |
| 143 | serial.baseaddr = uart_platform_base(0); |
| 144 | serial.baud = default_baudrate(); |
| 145 | lb_add_serial(&serial, data); |
| 146 | |
| 147 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250, data); |
| 148 | } |
| 149 | #endif |