blob: e4e8b6cfd707f012fed2951bb1c005819b2a5689 [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>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020022#include <console/uart.h>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020023#include <trace.h>
Kyösti Mälkkibea6bf02014-01-30 15:45:16 +020024#include "uart8250reg.h"
Rudolf Marek7f0e9302011-09-02 23:23:41 +020025
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000026/* Should support 8250, 16450, 16550, 16550A type UARTs */
Eric Biederman8ca8d762003-04-22 19:02:15 +000027
Kyösti Mälkki3ee16682014-02-17 19:37:52 +020028/* Nominal values only, good for the range of choices Kconfig offers for
29 * set of standard baudrates.
30 */
31#define BAUDRATE_REFCLK (115200)
32#define BAUDRATE_OVERSAMPLE (1)
33
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020034/* Expected character delay at 1200bps is 9ms for a working UART
35 * and no flow-control. Assume UART as stuck if shift register
36 * or FIFO takes more than 50ms per character to appear empty.
37 *
38 * Estimated that inb() from UART takes 1 microsecond.
39 */
40#define SINGLE_CHAR_TIMEOUT (50 * 1000)
41#define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT)
42
Kyösti Mälkki47707492014-02-15 07:53:18 +020043static int uart8250_can_tx_byte(unsigned base_port)
Eric Biederman8ca8d762003-04-22 19:02:15 +000044{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020045 return inb(base_port + UART_LSR) & UART_LSR_THRE;
Eric Biederman8ca8d762003-04-22 19:02:15 +000046}
47
Kyösti Mälkki47707492014-02-15 07:53:18 +020048static void uart8250_tx_byte(unsigned base_port, unsigned char data)
Eric Biederman8ca8d762003-04-22 19:02:15 +000049{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020050 unsigned long int i = SINGLE_CHAR_TIMEOUT;
51 while (i-- && !uart8250_can_tx_byte(base_port));
Kyösti Mälkki47707492014-02-15 07:53:18 +020052 outb(data, base_port + UART_TBR);
Eric Biederman8ca8d762003-04-22 19:02:15 +000053}
54
Kyösti Mälkki47707492014-02-15 07:53:18 +020055static void uart8250_tx_flush(unsigned base_port)
Eric Biederman8ca8d762003-04-22 19:02:15 +000056{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020057 unsigned long int i = FIFO_TIMEOUT;
58 while (i-- && !(inb(base_port + UART_LSR) & UART_LSR_TEMT));
Eric Biederman8ca8d762003-04-22 19:02:15 +000059}
60
Kyösti Mälkki47707492014-02-15 07:53:18 +020061static int uart8250_can_rx_byte(unsigned base_port)
Greg Watsone54d55b2004-03-13 03:40:51 +000062{
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000063 return inb(base_port + UART_LSR) & UART_LSR_DR;
Greg Watsone54d55b2004-03-13 03:40:51 +000064}
65
Kyösti Mälkki47707492014-02-15 07:53:18 +020066static unsigned char uart8250_rx_byte(unsigned base_port)
Greg Watsone54d55b2004-03-13 03:40:51 +000067{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020068 unsigned long int i = SINGLE_CHAR_TIMEOUT;
69 while (i-- && !uart8250_can_rx_byte(base_port));
Patrick Georgi472efa62012-02-16 20:44:20 +010070
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020071 if (i)
72 return inb(base_port + UART_RBR);
73 else
74 return 0x0;
Greg Watsone54d55b2004-03-13 03:40:51 +000075}
76
Kyösti Mälkki47707492014-02-15 07:53:18 +020077static void uart8250_init(unsigned base_port, unsigned divisor)
Eric Biederman8ca8d762003-04-22 19:02:15 +000078{
Rudolf Marek7f0e9302011-09-02 23:23:41 +020079 DISABLE_TRACE;
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000080 /* Disable interrupts */
Eric Biederman8ca8d762003-04-22 19:02:15 +000081 outb(0x0, base_port + UART_IER);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000082 /* Enable FIFOs */
83 outb(UART_FCR_FIFO_EN, base_port + UART_FCR);
84
Stefan Reinauerebafa4d2006-10-07 00:13:24 +000085 /* assert DTR and RTS so the other end is happy */
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000086 outb(UART_MCR_DTR | UART_MCR_RTS, base_port + UART_MCR);
87
88 /* DLAB on */
89 outb(UART_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART_LCR);
90
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020091 /* Set Baud Rate Divisor. 12 ==> 9600 Baud */
Eric Biederman8ca8d762003-04-22 19:02:15 +000092 outb(divisor & 0xFF, base_port + UART_DLL);
93 outb((divisor >> 8) & 0xFF, base_port + UART_DLM);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000094
95 /* Set to 3 for 8N1 */
96 outb(CONFIG_TTYS0_LCS, base_port + UART_LCR);
Rudolf Marek7f0e9302011-09-02 23:23:41 +020097 ENABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +000098}
Eric Biederman5cd81732004-03-11 15:01:31 +000099
Kyösti Mälkki47707492014-02-15 07:53:18 +0200100/* FIXME: Needs uart index from Kconfig.
101 * Already use array as a work-around for ROMCC.
102 */
103static const unsigned bases[1] = { CONFIG_TTYS0_BASE };
104
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000105void uart_init(void)
106{
Kyösti Mälkki3ee16682014-02-17 19:37:52 +0200107 unsigned int div;
108 div = uart_baudrate_divisor(default_baudrate(), BAUDRATE_REFCLK,
109 BAUDRATE_OVERSAMPLE);
Kyösti Mälkki47707492014-02-15 07:53:18 +0200110 uart8250_init(bases[0], div);
111}
112
113void uart_tx_byte(unsigned char data)
114{
115 uart8250_tx_byte(bases[0], data);
116}
117
118unsigned char uart_rx_byte(void)
119{
120 return uart8250_rx_byte(bases[0]);
121}
122
123int uart_can_rx_byte(void)
124{
125 return uart8250_can_rx_byte(bases[0]);
126}
127
128void uart_tx_flush(void)
129{
130 uart8250_tx_flush(bases[0]);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000131}