blob: 50569e03bfd5af0b20b21ec2d27b696e3aa92f32 [file] [log] [blame]
Furquan Shaikh732b83e2014-06-09 13:20:04 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2009 Samsung Electronics
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Furquan Shaikh732b83e2014-06-09 13:20:04 -070014 */
15
Furquan Shaikh732b83e2014-06-09 13:20:04 -070016#include <arch/io.h>
17#include <boot/coreboot_tables.h>
18#include <console/console.h> /* for __console definition */
Julius Werner96195ee2014-10-20 13:25:21 -070019#include <console/uart.h>
Furquan Shaikh732b83e2014-06-09 13:20:04 -070020#include <drivers/uart/uart8250reg.h>
Julius Werner96195ee2014-10-20 13:25:21 -070021#include <stdint.h>
Furquan Shaikh732b83e2014-06-09 13:20:04 -070022
23/*
24 * TODO: Use DRIVERS_UART_8250MEM driver instead.
25 * There is an issue in the IO call functions where x86 and ARM
26 * ordering is reversed. This 8250MEM driver uses the x86 convention.
27 * This driver can be replaced once the IO calls are sorted.
28 */
29struct tegra132_uart {
30 union {
31 uint32_t thr; // Transmit holding register.
32 uint32_t rbr; // Receive buffer register.
33 uint32_t dll; // Divisor latch lsb.
34 };
35 union {
36 uint32_t ier; // Interrupt enable register.
37 uint32_t dlm; // Divisor latch msb.
38 };
39 union {
40 uint32_t iir; // Interrupt identification register.
41 uint32_t fcr; // FIFO control register.
42 };
43 uint32_t lcr; // Line control register.
44 uint32_t mcr; // Modem control register.
45 uint32_t lsr; // Line status register.
46 uint32_t msr; // Modem status register.
47} __attribute__ ((packed));
48
49static void tegra132_uart_tx_flush(struct tegra132_uart *uart_ptr);
50static int tegra132_uart_tst_byte(struct tegra132_uart *uart_ptr);
51
52static void tegra132_uart_init(struct tegra132_uart *uart_ptr)
53{
54 const uint8_t line_config = UART8250_LCR_WLS_8; // 8n1
55
56 uint16_t divisor = (u16) uart_baudrate_divisor(default_baudrate(),
57 uart_platform_refclk(), 16);
58
59 tegra132_uart_tx_flush(uart_ptr);
60
61 // Disable interrupts.
Julius Werner2f37bd62015-02-19 14:51:15 -080062 write8(&uart_ptr->ier, 0);
Furquan Shaikh732b83e2014-06-09 13:20:04 -070063 // Force DTR and RTS to high.
Julius Werner2f37bd62015-02-19 14:51:15 -080064 write8(&uart_ptr->mcr, UART8250_MCR_DTR | UART8250_MCR_RTS);
Furquan Shaikh732b83e2014-06-09 13:20:04 -070065 // Set line configuration, access divisor latches.
Julius Werner2f37bd62015-02-19 14:51:15 -080066 write8(&uart_ptr->lcr, UART8250_LCR_DLAB | line_config);
Furquan Shaikh732b83e2014-06-09 13:20:04 -070067 // Set the divisor.
Julius Werner2f37bd62015-02-19 14:51:15 -080068 write8(&uart_ptr->dll, divisor & 0xff);
69 write8(&uart_ptr->dlm, (divisor >> 8) & 0xff);
Furquan Shaikh732b83e2014-06-09 13:20:04 -070070 // Hide the divisor latches.
Julius Werner2f37bd62015-02-19 14:51:15 -080071 write8(&uart_ptr->lcr, line_config);
Furquan Shaikh732b83e2014-06-09 13:20:04 -070072 // Enable FIFOs, and clear receive and transmit.
Julius Werner94184762015-02-19 20:19:23 -080073 write8(&uart_ptr->fcr, UART8250_FCR_FIFO_EN |
74 UART8250_FCR_CLEAR_RCVR | UART8250_FCR_CLEAR_XMIT);
Furquan Shaikh732b83e2014-06-09 13:20:04 -070075}
76
77static unsigned char tegra132_uart_rx_byte(struct tegra132_uart *uart_ptr)
78{
79 if (!tegra132_uart_tst_byte(uart_ptr))
80 return 0;
81 return read8(&uart_ptr->rbr);
82}
83
84static void tegra132_uart_tx_byte(struct tegra132_uart *uart_ptr, unsigned char data)
85{
86 while (!(read8(&uart_ptr->lsr) & UART8250_LSR_THRE));
Julius Werner2f37bd62015-02-19 14:51:15 -080087 write8(&uart_ptr->thr, data);
Furquan Shaikh732b83e2014-06-09 13:20:04 -070088}
89
90static void tegra132_uart_tx_flush(struct tegra132_uart *uart_ptr)
91{
92 while (!(read8(&uart_ptr->lsr) & UART8250_LSR_TEMT));
93}
94
95static int tegra132_uart_tst_byte(struct tegra132_uart *uart_ptr)
96{
97 return (read8(&uart_ptr->lsr) & UART8250_LSR_DR) == UART8250_LSR_DR;
98}
99
100/* FIXME: Add mainboard override */
101unsigned int uart_platform_refclk(void)
102{
103 return 408000000;
104}
105
106uintptr_t uart_platform_base(int idx)
107{
108 /* Default to UART A */
109 unsigned int base = 0x70006000;
110 /* UARTs A - E are mapped as index 0 - 4 */
111 if ((idx < 5) && (idx >= 0)) {
112 if (idx != 1) { /* Not UART B */
113 base += idx * 0x100;
114 } else {
115 base += 0x40;
116 }
117 }
118 return base;
119}
120
121void uart_init(int idx)
122{
123 struct tegra132_uart *uart_ptr = uart_platform_baseptr(idx);
124 tegra132_uart_init(uart_ptr);
125}
126
127unsigned char uart_rx_byte(int idx)
128{
129 struct tegra132_uart *uart_ptr = uart_platform_baseptr(idx);
130 return tegra132_uart_rx_byte(uart_ptr);
131}
132
133void uart_tx_byte(int idx, unsigned char data)
134{
135 struct tegra132_uart *uart_ptr = uart_platform_baseptr(idx);
136 tegra132_uart_tx_byte(uart_ptr, data);
137}
138
139void uart_tx_flush(int idx)
140{
141 struct tegra132_uart *uart_ptr = uart_platform_baseptr(idx);
142 tegra132_uart_tx_flush(uart_ptr);
143}
144
Patrick Georgic96ff452015-05-07 12:29:13 +0200145#if ENV_RAMSTAGE
Furquan Shaikh732b83e2014-06-09 13:20:04 -0700146void uart_fill_lb(void *data)
147{
148 struct lb_serial serial;
149 serial.type = LB_SERIAL_TYPE_MEMORY_MAPPED;
150 serial.baseaddr = uart_platform_base(CONFIG_UART_FOR_CONSOLE);
151 serial.baud = default_baudrate();
Vadim Bendebury9dccf1c2015-01-09 16:54:19 -0800152 serial.regwidth = 1;
Furquan Shaikh732b83e2014-06-09 13:20:04 -0700153 lb_add_serial(&serial, data);
154
155 lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, data);
156}
157#endif