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. |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Kyösti Mälkki | 4566d2e | 2014-04-23 10:28:59 +0300 | [diff] [blame] | 17 | #include <rules.h> |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 18 | #include <stdlib.h> |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 19 | #include <arch/io.h> |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 20 | #include <console/uart.h> |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 21 | #include <trace.h> |
Kyösti Mälkki | bea6bf0 | 2014-01-30 15:45:16 +0200 | [diff] [blame] | 22 | #include "uart8250reg.h" |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 23 | |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 24 | #ifndef __ROMCC__ |
| 25 | #include <boot/coreboot_tables.h> |
| 26 | #endif |
| 27 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 28 | /* Should support 8250, 16450, 16550, 16550A type UARTs */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 29 | |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 30 | /* Nominal values only, good for the range of choices Kconfig offers for |
| 31 | * set of standard baudrates. |
| 32 | */ |
| 33 | #define BAUDRATE_REFCLK (115200) |
| 34 | #define BAUDRATE_OVERSAMPLE (1) |
| 35 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 36 | /* Expected character delay at 1200bps is 9ms for a working UART |
| 37 | * and no flow-control. Assume UART as stuck if shift register |
| 38 | * or FIFO takes more than 50ms per character to appear empty. |
| 39 | * |
| 40 | * Estimated that inb() from UART takes 1 microsecond. |
| 41 | */ |
| 42 | #define SINGLE_CHAR_TIMEOUT (50 * 1000) |
| 43 | #define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT) |
| 44 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 45 | static int uart8250_can_tx_byte(unsigned base_port) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 46 | { |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 47 | return inb(base_port + UART8250_LSR) & UART8250_LSR_THRE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 50 | static void uart8250_tx_byte(unsigned base_port, unsigned char data) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 51 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 52 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 53 | while (i-- && !uart8250_can_tx_byte(base_port)); |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 54 | outb(data, base_port + UART8250_TBR); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 57 | static void uart8250_tx_flush(unsigned base_port) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 58 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 59 | unsigned long int i = FIFO_TIMEOUT; |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 60 | while (i-- && !(inb(base_port + UART8250_LSR) & UART8250_LSR_TEMT)); |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 63 | static int uart8250_can_rx_byte(unsigned base_port) |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 64 | { |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 65 | return inb(base_port + UART8250_LSR) & UART8250_LSR_DR; |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 68 | static unsigned char uart8250_rx_byte(unsigned base_port) |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 69 | { |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 70 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 71 | while (i-- && !uart8250_can_rx_byte(base_port)); |
Patrick Georgi | 472efa6 | 2012-02-16 20:44:20 +0100 | [diff] [blame] | 72 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 73 | if (i) |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 74 | return inb(base_port + UART8250_RBR); |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 75 | else |
| 76 | return 0x0; |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 79 | static void uart8250_init(unsigned base_port, unsigned divisor) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 80 | { |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 81 | DISABLE_TRACE; |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 82 | /* Disable interrupts */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 83 | outb(0x0, base_port + UART8250_IER); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 84 | /* Enable FIFOs */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 85 | outb(UART8250_FCR_FIFO_EN, base_port + UART8250_FCR); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 86 | |
Stefan Reinauer | ebafa4d | 2006-10-07 00:13:24 +0000 | [diff] [blame] | 87 | /* assert DTR and RTS so the other end is happy */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 88 | outb(UART8250_MCR_DTR | UART8250_MCR_RTS, base_port + UART8250_MCR); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 89 | |
| 90 | /* DLAB on */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 91 | outb(UART8250_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART8250_LCR); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 92 | |
Kyösti Mälkki | 9dd3ef1 | 2012-02-07 20:50:22 +0200 | [diff] [blame] | 93 | /* Set Baud Rate Divisor. 12 ==> 9600 Baud */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 94 | outb(divisor & 0xFF, base_port + UART8250_DLL); |
| 95 | outb((divisor >> 8) & 0xFF, base_port + UART8250_DLM); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 96 | |
| 97 | /* Set to 3 for 8N1 */ |
Gabe Black | 77ffa0d | 2013-09-30 21:25:49 -0700 | [diff] [blame] | 98 | outb(CONFIG_TTYS0_LCS, base_port + UART8250_LCR); |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame] | 99 | ENABLE_TRACE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 100 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 101 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 102 | static const unsigned bases[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 }; |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 103 | |
Ronald G. Minnich | 2adb297 | 2014-10-16 10:53:48 +0000 | [diff] [blame] | 104 | uintptr_t uart_platform_base(int idx) |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 105 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 106 | if (idx < ARRAY_SIZE(bases)) |
| 107 | return bases[idx]; |
| 108 | return 0; |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 111 | void uart_init(int idx) |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 112 | { |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 113 | unsigned int div; |
| 114 | div = uart_baudrate_divisor(default_baudrate(), BAUDRATE_REFCLK, |
| 115 | BAUDRATE_OVERSAMPLE); |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 116 | uart8250_init(uart_platform_base(idx), div); |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 119 | void uart_tx_byte(int idx, unsigned char data) |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 120 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 121 | uart8250_tx_byte(uart_platform_base(idx), data); |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 124 | unsigned char uart_rx_byte(int idx) |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 125 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 126 | return uart8250_rx_byte(uart_platform_base(idx)); |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 129 | void uart_tx_flush(int idx) |
Kyösti Mälkki | 4770749 | 2014-02-15 07:53:18 +0200 | [diff] [blame] | 130 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 131 | uart8250_tx_flush(uart_platform_base(idx)); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 132 | } |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 133 | |
Kyösti Mälkki | 4566d2e | 2014-04-23 10:28:59 +0300 | [diff] [blame] | 134 | #if ENV_RAMSTAGE |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 135 | void uart_fill_lb(void *data) |
| 136 | { |
| 137 | struct lb_serial serial; |
| 138 | serial.type = LB_SERIAL_TYPE_IO_MAPPED; |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame] | 139 | serial.baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE); |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 140 | serial.baud = default_baudrate(); |
Vadim Bendebury | 9dccf1c | 2015-01-09 16:54:19 -0800 | [diff] [blame] | 141 | serial.regwidth = 1; |
Lee Leahy | f92a98c | 2016-05-04 11:59:19 -0700 | [diff] [blame^] | 142 | if (IS_ENABLED(CONFIG_UART_USE_REFCLK_AS_INPUT_CLOCK)) |
| 143 | serial.input_hertz = uart_platform_refclk(); |
| 144 | else |
| 145 | serial.input_hertz = 0; |
| 146 | serial.uart_pci_addr = CONFIG_UART_PCI_ADDR; |
Kyösti Mälkki | bbf6f3d | 2014-03-15 01:32:55 +0200 | [diff] [blame] | 147 | lb_add_serial(&serial, data); |
| 148 | |
| 149 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250, data); |
| 150 | } |
| 151 | #endif |