blob: e4c51022de385d35dd70d6a61282ee106046601a [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <dev/uart.h>
25#include <platform/omap3.h>
26#include <target/debugconfig.h>
27
28struct uart_stat {
29 addr_t base;
30 uint shift;
31};
32
33static struct uart_stat uart[3] = {
34 { OMAP_UART1_BASE, 2 },
35 { OMAP_UART2_BASE, 2 },
36 { OMAP_UART3_BASE, 2 },
37};
38
39static inline void write_uart_reg(int port, uint reg, unsigned char data)
40{
41 *(volatile unsigned char *)(uart[port].base + (reg << uart[port].shift)) = data;
42}
43
44static inline unsigned char read_uart_reg(int port, uint reg)
45{
46 return *(volatile unsigned char *)(uart[port].base + (reg << uart[port].shift));
47}
48
49#define LCR_8N1 0x03
50
51#define FCR_FIFO_EN 0x01 /* Fifo enable */
52#define FCR_RXSR 0x02 /* Receiver soft reset */
53#define FCR_TXSR 0x04 /* Transmitter soft reset */
54
55#define MCR_DTR 0x01
56#define MCR_RTS 0x02
57#define MCR_DMA_EN 0x04
58#define MCR_TX_DFR 0x08
59
60#define LCR_WLS_MSK 0x03 /* character length select mask */
61#define LCR_WLS_5 0x00 /* 5 bit character length */
62#define LCR_WLS_6 0x01 /* 6 bit character length */
63#define LCR_WLS_7 0x02 /* 7 bit character length */
64#define LCR_WLS_8 0x03 /* 8 bit character length */
65#define LCR_STB 0x04 /* Number of stop Bits, off = 1, on = 1.5 or 2) */
66#define LCR_PEN 0x08 /* Parity eneble */
67#define LCR_EPS 0x10 /* Even Parity Select */
68#define LCR_STKP 0x20 /* Stick Parity */
69#define LCR_SBRK 0x40 /* Set Break */
70#define LCR_BKSE 0x80 /* Bank select enable */
71
72#define LSR_DR 0x01 /* Data ready */
73#define LSR_OE 0x02 /* Overrun */
74#define LSR_PE 0x04 /* Parity error */
75#define LSR_FE 0x08 /* Framing error */
76#define LSR_BI 0x10 /* Break */
77#define LSR_THRE 0x20 /* Xmit holding register empty */
78#define LSR_TEMT 0x40 /* Xmitter empty */
79#define LSR_ERR 0x80 /* Error */
80
81#define LCRVAL LCR_8N1 /* 8 data, 1 stop, no parity */
82#define MCRVAL (MCR_DTR | MCR_RTS) /* RTS/DTR */
83#define FCRVAL (FCR_FIFO_EN | FCR_RXSR | FCR_TXSR) /* Clear & enable FIFOs */
84
85#define V_NS16550_CLK (48000000) /* 48MHz (APLL96/2) */
86
87void uart_init_port(int port, uint baud)
88{
89 /* clear the tx & rx fifo and disable */
90 uint16_t baud_divisor = (V_NS16550_CLK / 16 / baud);
91
92 write_uart_reg(port, UART_IER, 0);
93 write_uart_reg(port, UART_LCR, LCR_BKSE | LCRVAL); // config mode A
94 write_uart_reg(port, UART_DLL, baud_divisor & 0xff);
95 write_uart_reg(port, UART_DLH, (baud_divisor >> 8) & 0xff);
96 write_uart_reg(port, UART_LCR, LCRVAL); // operational mode
97 write_uart_reg(port, UART_MCR, MCRVAL);
98 write_uart_reg(port, UART_FCR, FCRVAL);
99 write_uart_reg(port, UART_MDR1, 0); // UART 16x mode
100
101// write_uart_reg(port, UART_LCR, 0xBF); // config mode B
102// write_uart_reg(port, UART_EFR, (1<<7)|(1<<6)); // hw flow control
103// write_uart_reg(port, UART_LCR, LCRVAL); // operational mode
104}
105
106void uart_init_early(void)
107{
108 uart_init_port(DEBUG_UART, 115200);
109}
110
111void uart_init(void)
112{
113}
114
115int uart_putc(int port, char c )
116{
117 while (!(read_uart_reg(port, UART_LSR) & (1<<6))) // wait for the last char to get out
118 ;
119 write_uart_reg(port, UART_THR, c);
120 return 0;
121}
122
123int uart_getc(int port, bool wait) /* returns -1 if no data available */
124{
125 if (wait) {
126 while (!(read_uart_reg(port, UART_LSR) & (1<<0))) // wait for data to show up in the rx fifo
127 ;
128 } else {
129 if (!(read_uart_reg(port, UART_LSR) & (1<<0)))
130 return -1;
131 }
132 return read_uart_reg(port, UART_RHR);
133}
134
135void uart_flush_tx(int port)
136{
137 while (!(read_uart_reg(port, UART_LSR) & (1<<6))) // wait for the last char to get out
138 ;
139}
140
141void uart_flush_rx(int port)
142{
143 // empty the rx fifo
144 while (read_uart_reg(port, UART_LSR) & (1<<0)) {
145 volatile char c = read_uart_reg(port, UART_RHR);
146 (void)c;
147 }
148}
149
150