blob: b2246713cac087d03d4f010121219ace09eff13b [file] [log] [blame]
Stefan Reinauer85b0fa12010-12-17 00:08:21 +00001/*
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 Biederman8ca8d762003-04-22 19:02:15 +000021#include <arch/io.h>
22#include <uart8250.h>
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000023#include <pc80/mc146818rtc.h>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020024#include <trace.h>
25
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000026#if CONFIG_USE_OPTION_TABLE
27#include "option_table.h"
28#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +000029
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000030/* Should support 8250, 16450, 16550, 16550A type UARTs */
Eric Biederman8ca8d762003-04-22 19:02:15 +000031
Eric Biederman69afe282004-11-11 06:53:24 +000032static inline int uart8250_can_tx_byte(unsigned base_port)
Eric Biederman8ca8d762003-04-22 19:02:15 +000033{
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000034 return inb(base_port + UART_LSR) & UART_MSR_DSR;
Eric Biederman8ca8d762003-04-22 19:02:15 +000035}
36
37static inline void uart8250_wait_to_tx_byte(unsigned base_port)
38{
39 while(!uart8250_can_tx_byte(base_port))
40 ;
41}
42
43static inline void uart8250_wait_until_sent(unsigned base_port)
44{
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000045 while(!(inb(base_port + UART_LSR) & UART_LSR_TEMT))
Eric Biederman8ca8d762003-04-22 19:02:15 +000046 ;
47}
48
49void 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'Connora68555f2011-07-09 20:22:21 -040053}
54
55void uart8250_tx_flush(unsigned base_port)
56{
Eric Biederman8ca8d762003-04-22 19:02:15 +000057 uart8250_wait_until_sent(base_port);
58}
59
Greg Watsone54d55b2004-03-13 03:40:51 +000060int uart8250_can_rx_byte(unsigned base_port)
61{
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000062 return inb(base_port + UART_LSR) & UART_LSR_DR;
Greg Watsone54d55b2004-03-13 03:40:51 +000063}
64
65unsigned 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 Reinauer85b0fa12010-12-17 00:08:21 +000072void uart8250_init(unsigned base_port, unsigned divisor)
Eric Biederman8ca8d762003-04-22 19:02:15 +000073{
Rudolf Marek7f0e9302011-09-02 23:23:41 +020074 DISABLE_TRACE;
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000075 /* Disable interrupts */
Eric Biederman8ca8d762003-04-22 19:02:15 +000076 outb(0x0, base_port + UART_IER);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000077 /* Enable FIFOs */
78 outb(UART_FCR_FIFO_EN, base_port + UART_FCR);
79
Stefan Reinauerebafa4d2006-10-07 00:13:24 +000080 /* assert DTR and RTS so the other end is happy */
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000081 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 Biederman8ca8d762003-04-22 19:02:15 +000087 outb(divisor & 0xFF, base_port + UART_DLL);
88 outb((divisor >> 8) & 0xFF, base_port + UART_DLM);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000089
90 /* Set to 3 for 8N1 */
91 outb(CONFIG_TTYS0_LCS, base_port + UART_LCR);
Rudolf Marek7f0e9302011-09-02 23:23:41 +020092 ENABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +000093}
Eric Biederman5cd81732004-03-11 15:01:31 +000094
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000095void uart_init(void)
96{
Stefan Reinauer6aca1e82011-04-22 01:45:11 +000097 /* 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 Reinauerf349d552011-04-22 02:17:26 +0000103 unsigned int div = (115200 / CONFIG_TTYS0_BAUD);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000104
Stefan Reinauerf349d552011-04-22 02:17:26 +0000105#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 Georgib2517532011-05-10 21:53:13 +0000109 b_index = read_option(baud_rate, 0);
Stefan Reinauerf349d552011-04-22 02:17:26 +0000110 b_index &= 7;
Stefan Reinauerf5ce87d2011-04-22 02:32:03 +0000111 div = divisor[b_index];
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000112#else
Stefan Reinauerf349d552011-04-22 02:17:26 +0000113 if (get_option(&b_index, "baud_rate") == 0) {
114 div = divisor[b_index];
115 }
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000116#endif
Stefan Reinauerf349d552011-04-22 02:17:26 +0000117#endif
118
119 uart8250_init(CONFIG_TTYS0_BASE, div);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000120}