Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +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 | |
| 21 | #include <arch/io.h> |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 22 | #include <console/uart.h> |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 23 | #include <device/device.h> |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 24 | #include <delay.h> |
Kyösti Mälkki | bea6bf0 | 2014-01-30 15:45:16 +0200 | [diff] [blame] | 25 | #include "uart8250reg.h" |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 26 | |
| 27 | /* Should support 8250, 16450, 16550, 16550A type UARTs */ |
| 28 | |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 29 | /* Expected character delay at 1200bps is 9ms for a working UART |
| 30 | * and no flow-control. Assume UART as stuck if shift register |
| 31 | * or FIFO takes more than 50ms per character to appear empty. |
| 32 | */ |
| 33 | #define SINGLE_CHAR_TIMEOUT (50 * 1000) |
| 34 | #define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT) |
| 35 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 36 | static int uart8250_mem_can_tx_byte(unsigned base_port) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 37 | { |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 38 | return read8(base_port + UART_LSR) & UART_LSR_THRE; |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 41 | static void uart8250_mem_tx_byte(unsigned base_port, unsigned char data) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 42 | { |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 43 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 44 | while(i-- && !uart8250_mem_can_tx_byte(base_port)) |
| 45 | udelay(1); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 46 | write8(base_port + UART_TBR, data); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 49 | static void uart8250_mem_tx_flush(unsigned base_port) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 50 | { |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 51 | unsigned long int i = FIFO_TIMEOUT; |
| 52 | while(i-- && !(read8(base_port + UART_LSR) & UART_LSR_TEMT)) |
| 53 | udelay(1); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 56 | static int uart8250_mem_can_rx_byte(unsigned base_port) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 57 | { |
| 58 | return read8(base_port + UART_LSR) & UART_LSR_DR; |
| 59 | } |
| 60 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 61 | static unsigned char uart8250_mem_rx_byte(unsigned base_port) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 62 | { |
Stefan Reinauer | 4781800 | 2012-03-30 16:19:31 -0700 | [diff] [blame] | 63 | unsigned long int i = SINGLE_CHAR_TIMEOUT; |
| 64 | while(i-- && !uart8250_mem_can_rx_byte(base_port)) |
| 65 | udelay(1); |
| 66 | if (i) |
| 67 | return read8(base_port + UART_RBR); |
| 68 | else |
| 69 | return 0x0; |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 72 | static void uart8250_mem_init(unsigned base_port, unsigned divisor) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 73 | { |
| 74 | /* Disable interrupts */ |
| 75 | write8(base_port + UART_IER, 0x0); |
| 76 | /* Enable FIFOs */ |
| 77 | write8(base_port + UART_FCR, UART_FCR_FIFO_EN); |
| 78 | |
| 79 | /* Assert DTR and RTS so the other end is happy */ |
| 80 | write8(base_port + UART_MCR, UART_MCR_DTR | UART_MCR_RTS); |
| 81 | |
| 82 | /* DLAB on */ |
| 83 | write8(base_port + UART_LCR, UART_LCR_DLAB | CONFIG_TTYS0_LCS); |
| 84 | |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 85 | write8(base_port + UART_DLL, divisor & 0xFF); |
| 86 | write8(base_port + UART_DLM, (divisor >> 8) & 0xFF); |
| 87 | |
| 88 | /* Set to 3 for 8N1 */ |
| 89 | write8(base_port + UART_LCR, CONFIG_TTYS0_LCS); |
| 90 | } |
| 91 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 92 | void uart_init(int idx) |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 93 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 94 | u32 base = uart_platform_base(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 95 | if (!base) |
| 96 | return; |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 97 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 98 | unsigned int div; |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 99 | div = uart_baudrate_divisor(default_baudrate(), uart_platform_refclk(), 16); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 100 | uart8250_mem_init(base, div); |
| 101 | } |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 102 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 103 | void uart_tx_byte(int idx, unsigned char data) |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 104 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 105 | u32 base = uart_platform_base(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 106 | if (!base) |
| 107 | return; |
| 108 | uart8250_mem_tx_byte(base, data); |
| 109 | } |
| 110 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 111 | unsigned char uart_rx_byte(int idx) |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 112 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 113 | u32 base = uart_platform_base(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 114 | if (!base) |
| 115 | return 0xff; |
| 116 | return uart8250_mem_rx_byte(base); |
| 117 | } |
| 118 | |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 119 | void uart_tx_flush(int idx) |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 120 | { |
Kyösti Mälkki | 70342a7 | 2014-03-14 22:28:29 +0200 | [diff] [blame^] | 121 | u32 base = uart_platform_base(idx); |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 122 | if (!base) |
| 123 | return; |
| 124 | uart8250_mem_tx_flush(base); |
Stefan Reinauer | 4885daa | 2011-04-26 23:47:04 +0000 | [diff] [blame] | 125 | } |