blob: d7ebb3edd378130122caa455e07eb4f96ed2b56a [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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 <reg.h>
25#include <dev/uart.h>
26#include <target/debugconfig.h>
27#include <platform/or1ksim.h>
28
29struct uart_stat {
30 addr_t base;
31 uint32_t clk_freq;
32 uint shift;
33};
34
35static struct uart_stat uart[1] = {
36 { UART1_BASE, UART1_CLOCK_FREQ, 0 },
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 UART_RHR 0
50#define UART_THR 0
51#define UART_DLL 0
52#define UART_IER 1
53#define UART_DLH 1
54#define UART_IIR 2
55#define UART_FCR 2
56#define UART_EFR 2
57#define UART_LCR 3
58#define UART_MCR 4
59#define UART_LSR 5
60#define UART_MSR 6
61#define UART_TCR 6
62#define UART_SPR 7
63#define UART_TLR 7
64#define UART_MDR1 8
65#define UART_MDR2 9
66#define UART_SFLSR 10
67#define UART_RESUME 11
68#define UART_TXFLL 10
69#define UART_TXFLH 11
70#define UART_SFREGL 12
71#define UART_SFREGH 13
72#define UART_RXFLL 12
73#define UART_RXFLH 13
74#define UART_BLR 14
75#define UART_UASR 14
76#define UART_ACREG 15
77#define UART_SCR 16
78#define UART_SSR 17
79#define UART_EBLR 18
80#define UART_MVR 19
81#define UART_SYSC 20
82
83#define LCR_8N1 0x03
84
85#define FCR_FIFO_EN 0x01 /* Fifo enable */
86#define FCR_RXSR 0x02 /* Receiver soft reset */
87#define FCR_TXSR 0x04 /* Transmitter soft reset */
88
89#define MCR_DTR 0x01
90#define MCR_RTS 0x02
91#define MCR_DMA_EN 0x04
92#define MCR_TX_DFR 0x08
93
94#define LCR_WLS_MSK 0x03 /* character length select mask */
95#define LCR_WLS_5 0x00 /* 5 bit character length */
96#define LCR_WLS_6 0x01 /* 6 bit character length */
97#define LCR_WLS_7 0x02 /* 7 bit character length */
98#define LCR_WLS_8 0x03 /* 8 bit character length */
99#define LCR_STB 0x04 /* Number of stop Bits, off = 1, on = 1.5 or 2) */
100#define LCR_PEN 0x08 /* Parity eneble */
101#define LCR_EPS 0x10 /* Even Parity Select */
102#define LCR_STKP 0x20 /* Stick Parity */
103#define LCR_SBRK 0x40 /* Set Break */
104#define LCR_BKSE 0x80 /* Bank select enable */
105
106#define LSR_DR 0x01 /* Data ready */
107#define LSR_OE 0x02 /* Overrun */
108#define LSR_PE 0x04 /* Parity error */
109#define LSR_FE 0x08 /* Framing error */
110#define LSR_BI 0x10 /* Break */
111#define LSR_THRE 0x20 /* Xmit holding register empty */
112#define LSR_TEMT 0x40 /* Xmitter empty */
113#define LSR_ERR 0x80 /* Error */
114
115#define LCRVAL LCR_8N1 /* 8 data, 1 stop, no parity */
116#define MCRVAL (MCR_DTR | MCR_RTS) /* RTS/DTR */
117#define FCRVAL (FCR_FIFO_EN | FCR_RXSR | FCR_TXSR) /* Clear & enable FIFOs */
118
119void uart_init_port(int port, uint baud)
120{
121 /* clear the tx & rx fifo and disable */
122 uint16_t baud_divisor = (uart[port].clk_freq / 16 / baud);
123
124 write_uart_reg(port, UART_IER, 0);
125 write_uart_reg(port, UART_LCR, LCR_BKSE | LCRVAL); // config mode A
126 write_uart_reg(port, UART_DLL, baud_divisor & 0xff);
127 write_uart_reg(port, UART_DLH, (baud_divisor >> 8) & 0xff);
128 write_uart_reg(port, UART_LCR, LCRVAL); // operational mode
129 write_uart_reg(port, UART_MCR, MCRVAL);
130 write_uart_reg(port, UART_FCR, FCRVAL);
131}
132
133void uart_init_early(void)
134{
135 uart_init_port(DEBUG_UART, 115200);
136}
137
138void uart_init(void)
139{
140}
141
142int uart_putc(int port, char c )
143{
144 while (!(read_uart_reg(port, UART_LSR) & (1<<6))) // wait for the last char to get out
145 ;
146 write_uart_reg(port, UART_THR, c);
147 return 0;
148}
149
150int uart_getc(int port, bool wait) /* returns -1 if no data available */
151{
152 if (wait) {
153 while (!(read_uart_reg(port, UART_LSR) & (1<<0))) // wait for data to show up in the rx fifo
154 ;
155 } else {
156 if (!(read_uart_reg(port, UART_LSR) & (1<<0)))
157 return -1;
158 }
159 return read_uart_reg(port, UART_RHR);
160}
161
162void uart_flush_tx(int port)
163{
164 while (!(read_uart_reg(port, UART_LSR) & (1<<6))) // wait for the last char to get out
165 ;
166}
167
168void uart_flush_rx(int port)
169{
170 // empty the rx fifo
171 while (read_uart_reg(port, UART_LSR) & (1<<0)) {
172 volatile char c = read_uart_reg(port, UART_RHR);
173 (void)c;
174 }
175}