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 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 21 | #include <arch/io.h> |
| 22 | #include <uart8250.h> |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 23 | #include <pc80/mc146818rtc.h> |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame^] | 24 | #include <trace.h> |
| 25 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 26 | #if CONFIG_USE_OPTION_TABLE |
| 27 | #include "option_table.h" |
| 28 | #endif |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 29 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 30 | /* Should support 8250, 16450, 16550, 16550A type UARTs */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 31 | |
Eric Biederman | 69afe28 | 2004-11-11 06:53:24 +0000 | [diff] [blame] | 32 | static inline int uart8250_can_tx_byte(unsigned base_port) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 33 | { |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 34 | return inb(base_port + UART_LSR) & UART_MSR_DSR; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static inline void uart8250_wait_to_tx_byte(unsigned base_port) |
| 38 | { |
| 39 | while(!uart8250_can_tx_byte(base_port)) |
| 40 | ; |
| 41 | } |
| 42 | |
| 43 | static inline void uart8250_wait_until_sent(unsigned base_port) |
| 44 | { |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 45 | while(!(inb(base_port + UART_LSR) & UART_LSR_TEMT)) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 46 | ; |
| 47 | } |
| 48 | |
| 49 | void uart8250_tx_byte(unsigned base_port, unsigned char data) |
| 50 | { |
| 51 | uart8250_wait_to_tx_byte(base_port); |
| 52 | outb(data, base_port + UART_TBR); |
Kevin O'Connor | a68555f | 2011-07-09 20:22:21 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void uart8250_tx_flush(unsigned base_port) |
| 56 | { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 57 | uart8250_wait_until_sent(base_port); |
| 58 | } |
| 59 | |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 60 | int uart8250_can_rx_byte(unsigned base_port) |
| 61 | { |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 62 | return inb(base_port + UART_LSR) & UART_LSR_DR; |
Greg Watson | e54d55b | 2004-03-13 03:40:51 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | unsigned char uart8250_rx_byte(unsigned base_port) |
| 66 | { |
| 67 | while(!uart8250_can_rx_byte(base_port)) |
| 68 | ; |
| 69 | return inb(base_port + UART_RBR); |
| 70 | } |
| 71 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 72 | void uart8250_init(unsigned base_port, unsigned divisor) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 73 | { |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame^] | 74 | DISABLE_TRACE; |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 75 | /* Disable interrupts */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 76 | outb(0x0, base_port + UART_IER); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 77 | /* Enable FIFOs */ |
| 78 | outb(UART_FCR_FIFO_EN, base_port + UART_FCR); |
| 79 | |
Stefan Reinauer | ebafa4d | 2006-10-07 00:13:24 +0000 | [diff] [blame] | 80 | /* assert DTR and RTS so the other end is happy */ |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 81 | outb(UART_MCR_DTR | UART_MCR_RTS, base_port + UART_MCR); |
| 82 | |
| 83 | /* DLAB on */ |
| 84 | outb(UART_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART_LCR); |
| 85 | |
| 86 | /* Set Baud Rate Divisor. 12 ==> 115200 Baud */ |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 87 | outb(divisor & 0xFF, base_port + UART_DLL); |
| 88 | outb((divisor >> 8) & 0xFF, base_port + UART_DLM); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 89 | |
| 90 | /* Set to 3 for 8N1 */ |
| 91 | outb(CONFIG_TTYS0_LCS, base_port + UART_LCR); |
Rudolf Marek | 7f0e930 | 2011-09-02 23:23:41 +0200 | [diff] [blame^] | 92 | ENABLE_TRACE; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 93 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 94 | |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 95 | void uart_init(void) |
| 96 | { |
Stefan Reinauer | 6aca1e8 | 2011-04-22 01:45:11 +0000 | [diff] [blame] | 97 | /* TODO the divisor calculation is hard coded to standard UARTs. Some |
| 98 | * UARTs won't work with these values. This should be a property of the |
| 99 | * UART used, worst case a Kconfig variable. For now live with hard |
| 100 | * codes as the only devices that might be different are the iWave |
| 101 | * iRainbowG6 and the OXPCIe952 card (and the latter is memory mapped) |
| 102 | */ |
Stefan Reinauer | f349d55 | 2011-04-22 02:17:26 +0000 | [diff] [blame] | 103 | unsigned int div = (115200 / CONFIG_TTYS0_BAUD); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 104 | |
Stefan Reinauer | f349d55 | 2011-04-22 02:17:26 +0000 | [diff] [blame] | 105 | #if !defined(__SMM__) && CONFIG_USE_OPTION_TABLE |
| 106 | static const unsigned char divisor[8] = { 1, 2, 3, 6, 12, 24, 48, 96 }; |
| 107 | unsigned b_index = 0; |
| 108 | #if defined(__PRE_RAM__) |
Patrick Georgi | b251753 | 2011-05-10 21:53:13 +0000 | [diff] [blame] | 109 | b_index = read_option(baud_rate, 0); |
Stefan Reinauer | f349d55 | 2011-04-22 02:17:26 +0000 | [diff] [blame] | 110 | b_index &= 7; |
Stefan Reinauer | f5ce87d | 2011-04-22 02:32:03 +0000 | [diff] [blame] | 111 | div = divisor[b_index]; |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 112 | #else |
Stefan Reinauer | f349d55 | 2011-04-22 02:17:26 +0000 | [diff] [blame] | 113 | if (get_option(&b_index, "baud_rate") == 0) { |
| 114 | div = divisor[b_index]; |
| 115 | } |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 116 | #endif |
Stefan Reinauer | f349d55 | 2011-04-22 02:17:26 +0000 | [diff] [blame] | 117 | #endif |
| 118 | |
| 119 | uart8250_init(CONFIG_TTYS0_BASE, div); |
Stefan Reinauer | 85b0fa1 | 2010-12-17 00:08:21 +0000 | [diff] [blame] | 120 | } |