blob: f4a7b0ab74ea37851e80bc2acec92e44e9b1d762 [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
Kyösti Mälkki4566d2e2014-04-23 10:28:59 +030021#include <rules.h>
Kyösti Mälkki70342a72014-03-14 22:28:29 +020022#include <stdlib.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000023#include <arch/io.h>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020024#include <console/uart.h>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020025#include <trace.h>
Kyösti Mälkkibea6bf02014-01-30 15:45:16 +020026#include "uart8250reg.h"
Rudolf Marek7f0e9302011-09-02 23:23:41 +020027
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +020028#ifndef __ROMCC__
29#include <boot/coreboot_tables.h>
30#endif
31
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000032/* Should support 8250, 16450, 16550, 16550A type UARTs */
Eric Biederman8ca8d762003-04-22 19:02:15 +000033
Kyösti Mälkki3ee16682014-02-17 19:37:52 +020034/* Nominal values only, good for the range of choices Kconfig offers for
35 * set of standard baudrates.
36 */
37#define BAUDRATE_REFCLK (115200)
38#define BAUDRATE_OVERSAMPLE (1)
39
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020040/* Expected character delay at 1200bps is 9ms for a working UART
41 * and no flow-control. Assume UART as stuck if shift register
42 * or FIFO takes more than 50ms per character to appear empty.
43 *
44 * Estimated that inb() from UART takes 1 microsecond.
45 */
46#define SINGLE_CHAR_TIMEOUT (50 * 1000)
47#define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT)
48
Kyösti Mälkki47707492014-02-15 07:53:18 +020049static int uart8250_can_tx_byte(unsigned base_port)
Eric Biederman8ca8d762003-04-22 19:02:15 +000050{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020051 return inb(base_port + UART_LSR) & UART_LSR_THRE;
Eric Biederman8ca8d762003-04-22 19:02:15 +000052}
53
Kyösti Mälkki47707492014-02-15 07:53:18 +020054static void uart8250_tx_byte(unsigned base_port, unsigned char data)
Eric Biederman8ca8d762003-04-22 19:02:15 +000055{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020056 unsigned long int i = SINGLE_CHAR_TIMEOUT;
57 while (i-- && !uart8250_can_tx_byte(base_port));
Kyösti Mälkki47707492014-02-15 07:53:18 +020058 outb(data, base_port + UART_TBR);
Eric Biederman8ca8d762003-04-22 19:02:15 +000059}
60
Kyösti Mälkki47707492014-02-15 07:53:18 +020061static void uart8250_tx_flush(unsigned base_port)
Eric Biederman8ca8d762003-04-22 19:02:15 +000062{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020063 unsigned long int i = FIFO_TIMEOUT;
64 while (i-- && !(inb(base_port + UART_LSR) & UART_LSR_TEMT));
Eric Biederman8ca8d762003-04-22 19:02:15 +000065}
66
Kyösti Mälkki47707492014-02-15 07:53:18 +020067static int uart8250_can_rx_byte(unsigned base_port)
Greg Watsone54d55b2004-03-13 03:40:51 +000068{
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000069 return inb(base_port + UART_LSR) & UART_LSR_DR;
Greg Watsone54d55b2004-03-13 03:40:51 +000070}
71
Kyösti Mälkki47707492014-02-15 07:53:18 +020072static unsigned char uart8250_rx_byte(unsigned base_port)
Greg Watsone54d55b2004-03-13 03:40:51 +000073{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020074 unsigned long int i = SINGLE_CHAR_TIMEOUT;
75 while (i-- && !uart8250_can_rx_byte(base_port));
Patrick Georgi472efa62012-02-16 20:44:20 +010076
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020077 if (i)
78 return inb(base_port + UART_RBR);
79 else
80 return 0x0;
Greg Watsone54d55b2004-03-13 03:40:51 +000081}
82
Kyösti Mälkki47707492014-02-15 07:53:18 +020083static void uart8250_init(unsigned base_port, unsigned divisor)
Eric Biederman8ca8d762003-04-22 19:02:15 +000084{
Rudolf Marek7f0e9302011-09-02 23:23:41 +020085 DISABLE_TRACE;
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000086 /* Disable interrupts */
Eric Biederman8ca8d762003-04-22 19:02:15 +000087 outb(0x0, base_port + UART_IER);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000088 /* Enable FIFOs */
89 outb(UART_FCR_FIFO_EN, base_port + UART_FCR);
90
Stefan Reinauerebafa4d2006-10-07 00:13:24 +000091 /* assert DTR and RTS so the other end is happy */
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000092 outb(UART_MCR_DTR | UART_MCR_RTS, base_port + UART_MCR);
93
94 /* DLAB on */
95 outb(UART_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART_LCR);
96
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020097 /* Set Baud Rate Divisor. 12 ==> 9600 Baud */
Eric Biederman8ca8d762003-04-22 19:02:15 +000098 outb(divisor & 0xFF, base_port + UART_DLL);
99 outb((divisor >> 8) & 0xFF, base_port + UART_DLM);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000100
101 /* Set to 3 for 8N1 */
102 outb(CONFIG_TTYS0_LCS, base_port + UART_LCR);
Rudolf Marek7f0e9302011-09-02 23:23:41 +0200103 ENABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000104}
Eric Biederman5cd81732004-03-11 15:01:31 +0000105
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200106static const unsigned bases[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
Kyösti Mälkki47707492014-02-15 07:53:18 +0200107
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200108unsigned int uart_platform_base(int idx)
109{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200110 if (idx < ARRAY_SIZE(bases))
111 return bases[idx];
112 return 0;
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200113}
114
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200115void uart_init(int idx)
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000116{
Kyösti Mälkki3ee16682014-02-17 19:37:52 +0200117 unsigned int div;
118 div = uart_baudrate_divisor(default_baudrate(), BAUDRATE_REFCLK,
119 BAUDRATE_OVERSAMPLE);
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200120 uart8250_init(uart_platform_base(idx), div);
Kyösti Mälkki47707492014-02-15 07:53:18 +0200121}
122
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200123void uart_tx_byte(int idx, unsigned char data)
Kyösti Mälkki47707492014-02-15 07:53:18 +0200124{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200125 uart8250_tx_byte(uart_platform_base(idx), data);
Kyösti Mälkki47707492014-02-15 07:53:18 +0200126}
127
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200128unsigned char uart_rx_byte(int idx)
Kyösti Mälkki47707492014-02-15 07:53:18 +0200129{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200130 return uart8250_rx_byte(uart_platform_base(idx));
Kyösti Mälkki47707492014-02-15 07:53:18 +0200131}
132
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200133void uart_tx_flush(int idx)
Kyösti Mälkki47707492014-02-15 07:53:18 +0200134{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200135 uart8250_tx_flush(uart_platform_base(idx));
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000136}
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200137
Kyösti Mälkki4566d2e2014-04-23 10:28:59 +0300138#if ENV_RAMSTAGE
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200139void uart_fill_lb(void *data)
140{
141 struct lb_serial serial;
142 serial.type = LB_SERIAL_TYPE_IO_MAPPED;
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200143 serial.baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE);
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200144 serial.baud = default_baudrate();
145 lb_add_serial(&serial, data);
146
147 lb_add_console(LB_TAG_CONSOLE_SERIAL8250, data);
148}
149#endif