blob: 63bc42fccc3be4393eddaf3855ac41fa3e828069 [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.
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000015 */
16
Kyösti Mälkki4566d2e2014-04-23 10:28:59 +030017#include <rules.h>
Kyösti Mälkki70342a72014-03-14 22:28:29 +020018#include <stdlib.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000019#include <arch/io.h>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020020#include <console/uart.h>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020021#include <trace.h>
Kyösti Mälkkibea6bf02014-01-30 15:45:16 +020022#include "uart8250reg.h"
Rudolf Marek7f0e9302011-09-02 23:23:41 +020023
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +020024#ifndef __ROMCC__
25#include <boot/coreboot_tables.h>
26#endif
27
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000028/* Should support 8250, 16450, 16550, 16550A type UARTs */
Eric Biederman8ca8d762003-04-22 19:02:15 +000029
Kyösti Mälkki3ee16682014-02-17 19:37:52 +020030/* Nominal values only, good for the range of choices Kconfig offers for
31 * set of standard baudrates.
32 */
33#define BAUDRATE_REFCLK (115200)
34#define BAUDRATE_OVERSAMPLE (1)
35
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020036/* Expected character delay at 1200bps is 9ms for a working UART
37 * and no flow-control. Assume UART as stuck if shift register
38 * or FIFO takes more than 50ms per character to appear empty.
39 *
40 * Estimated that inb() from UART takes 1 microsecond.
41 */
42#define SINGLE_CHAR_TIMEOUT (50 * 1000)
43#define FIFO_TIMEOUT (16 * SINGLE_CHAR_TIMEOUT)
44
Kyösti Mälkki47707492014-02-15 07:53:18 +020045static int uart8250_can_tx_byte(unsigned base_port)
Eric Biederman8ca8d762003-04-22 19:02:15 +000046{
Gabe Black77ffa0d2013-09-30 21:25:49 -070047 return inb(base_port + UART8250_LSR) & UART8250_LSR_THRE;
Eric Biederman8ca8d762003-04-22 19:02:15 +000048}
49
Kyösti Mälkki47707492014-02-15 07:53:18 +020050static void uart8250_tx_byte(unsigned base_port, unsigned char data)
Eric Biederman8ca8d762003-04-22 19:02:15 +000051{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020052 unsigned long int i = SINGLE_CHAR_TIMEOUT;
53 while (i-- && !uart8250_can_tx_byte(base_port));
Gabe Black77ffa0d2013-09-30 21:25:49 -070054 outb(data, base_port + UART8250_TBR);
Eric Biederman8ca8d762003-04-22 19:02:15 +000055}
56
Kyösti Mälkki47707492014-02-15 07:53:18 +020057static void uart8250_tx_flush(unsigned base_port)
Eric Biederman8ca8d762003-04-22 19:02:15 +000058{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020059 unsigned long int i = FIFO_TIMEOUT;
Gabe Black77ffa0d2013-09-30 21:25:49 -070060 while (i-- && !(inb(base_port + UART8250_LSR) & UART8250_LSR_TEMT));
Eric Biederman8ca8d762003-04-22 19:02:15 +000061}
62
Kyösti Mälkki47707492014-02-15 07:53:18 +020063static int uart8250_can_rx_byte(unsigned base_port)
Greg Watsone54d55b2004-03-13 03:40:51 +000064{
Gabe Black77ffa0d2013-09-30 21:25:49 -070065 return inb(base_port + UART8250_LSR) & UART8250_LSR_DR;
Greg Watsone54d55b2004-03-13 03:40:51 +000066}
67
Kyösti Mälkki47707492014-02-15 07:53:18 +020068static unsigned char uart8250_rx_byte(unsigned base_port)
Greg Watsone54d55b2004-03-13 03:40:51 +000069{
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020070 unsigned long int i = SINGLE_CHAR_TIMEOUT;
71 while (i-- && !uart8250_can_rx_byte(base_port));
Patrick Georgi472efa62012-02-16 20:44:20 +010072
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020073 if (i)
Gabe Black77ffa0d2013-09-30 21:25:49 -070074 return inb(base_port + UART8250_RBR);
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020075 else
76 return 0x0;
Greg Watsone54d55b2004-03-13 03:40:51 +000077}
78
Kyösti Mälkki47707492014-02-15 07:53:18 +020079static void uart8250_init(unsigned base_port, unsigned divisor)
Eric Biederman8ca8d762003-04-22 19:02:15 +000080{
Rudolf Marek7f0e9302011-09-02 23:23:41 +020081 DISABLE_TRACE;
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000082 /* Disable interrupts */
Gabe Black77ffa0d2013-09-30 21:25:49 -070083 outb(0x0, base_port + UART8250_IER);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000084 /* Enable FIFOs */
Gabe Black77ffa0d2013-09-30 21:25:49 -070085 outb(UART8250_FCR_FIFO_EN, base_port + UART8250_FCR);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000086
Stefan Reinauerebafa4d2006-10-07 00:13:24 +000087 /* assert DTR and RTS so the other end is happy */
Gabe Black77ffa0d2013-09-30 21:25:49 -070088 outb(UART8250_MCR_DTR | UART8250_MCR_RTS, base_port + UART8250_MCR);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000089
90 /* DLAB on */
Gabe Black77ffa0d2013-09-30 21:25:49 -070091 outb(UART8250_LCR_DLAB | CONFIG_TTYS0_LCS, base_port + UART8250_LCR);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000092
Kyösti Mälkki9dd3ef12012-02-07 20:50:22 +020093 /* Set Baud Rate Divisor. 12 ==> 9600 Baud */
Gabe Black77ffa0d2013-09-30 21:25:49 -070094 outb(divisor & 0xFF, base_port + UART8250_DLL);
95 outb((divisor >> 8) & 0xFF, base_port + UART8250_DLM);
Stefan Reinauer85b0fa12010-12-17 00:08:21 +000096
97 /* Set to 3 for 8N1 */
Gabe Black77ffa0d2013-09-30 21:25:49 -070098 outb(CONFIG_TTYS0_LCS, base_port + UART8250_LCR);
Rudolf Marek7f0e9302011-09-02 23:23:41 +020099 ENABLE_TRACE;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000100}
Eric Biederman5cd81732004-03-11 15:01:31 +0000101
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200102static const unsigned bases[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
Kyösti Mälkki47707492014-02-15 07:53:18 +0200103
Ronald G. Minnich2adb2972014-10-16 10:53:48 +0000104uintptr_t uart_platform_base(int idx)
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200105{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200106 if (idx < ARRAY_SIZE(bases))
107 return bases[idx];
108 return 0;
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200109}
110
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200111void uart_init(int idx)
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000112{
Kyösti Mälkki3ee16682014-02-17 19:37:52 +0200113 unsigned int div;
114 div = uart_baudrate_divisor(default_baudrate(), BAUDRATE_REFCLK,
115 BAUDRATE_OVERSAMPLE);
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200116 uart8250_init(uart_platform_base(idx), div);
Kyösti Mälkki47707492014-02-15 07:53:18 +0200117}
118
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200119void uart_tx_byte(int idx, unsigned char data)
Kyösti Mälkki47707492014-02-15 07:53:18 +0200120{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200121 uart8250_tx_byte(uart_platform_base(idx), data);
Kyösti Mälkki47707492014-02-15 07:53:18 +0200122}
123
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200124unsigned char uart_rx_byte(int idx)
Kyösti Mälkki47707492014-02-15 07:53:18 +0200125{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200126 return uart8250_rx_byte(uart_platform_base(idx));
Kyösti Mälkki47707492014-02-15 07:53:18 +0200127}
128
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200129void uart_tx_flush(int idx)
Kyösti Mälkki47707492014-02-15 07:53:18 +0200130{
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200131 uart8250_tx_flush(uart_platform_base(idx));
Stefan Reinauer85b0fa12010-12-17 00:08:21 +0000132}
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200133
Kyösti Mälkki4566d2e2014-04-23 10:28:59 +0300134#if ENV_RAMSTAGE
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200135void uart_fill_lb(void *data)
136{
137 struct lb_serial serial;
138 serial.type = LB_SERIAL_TYPE_IO_MAPPED;
Kyösti Mälkki70342a72014-03-14 22:28:29 +0200139 serial.baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE);
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200140 serial.baud = default_baudrate();
Vadim Bendebury9dccf1c2015-01-09 16:54:19 -0800141 serial.regwidth = 1;
Kyösti Mälkkibbf6f3d2014-03-15 01:32:55 +0200142 lb_add_serial(&serial, data);
143
144 lb_add_console(LB_TAG_CONSOLE_SERIAL8250, data);
145}
146#endif