1 /*
2 * Copyright (c) 2006-2019, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2019-12-04 Jiaxun Yang Initial version
9 */
10
11 /**
12 * @addtogroup mipssim
13 */
14
15 /*@{*/
16
17 #include <rtthread.h>
18 #include <rtdevice.h>
19 #include <rthw.h>
20 #include "drv_uart.h"
21
22 #define TRUE 1
23 #define FALSE 0
24
25 struct rt_uart_mipssim
26 {
27 void *base;
28 rt_uint32_t IRQ;
29 };
30
mipssim_uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)31 static rt_err_t mipssim_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
32 {
33 struct rt_uart_mipssim *uart_dev = RT_NULL;
34
35 RT_ASSERT(serial != RT_NULL);
36 RT_ASSERT(cfg != RT_NULL);
37
38 uart_dev = (struct rt_uart_mipssim *)serial->parent.user_data;
39
40 UART_IER(uart_dev->base) = 0; /* clear interrupt */
41 UART_FCR(uart_dev->base) = 0xc1; /* reset UART Rx/Tx */
42 /* set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
43 UART_LCR(uart_dev->base) = 0x3;
44 UART_MCR(uart_dev->base) = 0x3;
45 UART_LSR(uart_dev->base) = 0x60;
46 UART_MSR(uart_dev->base) = 0xb0;
47
48 return RT_EOK;
49 }
50
mipssim_uart_control(struct rt_serial_device * serial,int cmd,void * arg)51 static rt_err_t mipssim_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
52 {
53 struct rt_uart_mipssim *uart_dev = RT_NULL;
54
55 RT_ASSERT(serial != RT_NULL);
56 uart_dev = (struct rt_uart_mipssim *)serial->parent.user_data;
57
58 switch (cmd)
59 {
60 case RT_DEVICE_CTRL_CLR_INT: /* Disable RX IRQ */
61 rt_hw_interrupt_mask(uart_dev->IRQ);
62 break;
63
64 case RT_DEVICE_CTRL_SET_INT: /* Enable RX IRQ */
65 rt_hw_interrupt_umask(uart_dev->IRQ);
66 UART_IER(uart_dev->base) |= (IER_IRxE|IER_ILE);
67 break;
68
69 default:
70 break;
71 }
72
73 return RT_EOK;
74
75 }
76
uart_is_transmit_empty(struct rt_uart_mipssim * uart_dev)77 static rt_bool_t uart_is_transmit_empty(struct rt_uart_mipssim *uart_dev)
78 {
79 unsigned char status = UART_LSR(uart_dev->base);
80
81 if (status & (UARTLSR_TE | UARTLSR_TFE))
82 {
83 return TRUE;
84 }
85 else
86 {
87 return FALSE;
88 }
89 }
90
mipssim_uart_putc(struct rt_serial_device * serial,char c)91 static int mipssim_uart_putc(struct rt_serial_device *serial, char c)
92 {
93 struct rt_uart_mipssim *uart_dev = RT_NULL;
94
95 RT_ASSERT(serial != RT_NULL);
96
97 uart_dev = (struct rt_uart_mipssim *)serial->parent.user_data;
98
99 while (FALSE == uart_is_transmit_empty(uart_dev))
100 ;
101
102 UART_DAT(uart_dev->base) = c;
103
104 return 1;
105 }
106
mipssim_uart_getc(struct rt_serial_device * serial)107 static int mipssim_uart_getc(struct rt_serial_device *serial)
108 {
109 struct rt_uart_mipssim *uart_dev = RT_NULL;
110
111 RT_ASSERT(serial != RT_NULL);
112
113 uart_dev = (struct rt_uart_mipssim *)serial->parent.user_data;
114
115 if (LSR_RXRDY & UART_LSR(uart_dev->base))
116 {
117 return UART_DAT(uart_dev->base);
118 }
119
120 return -1;
121 }
122
123 /* UART interrupt handler */
uart_irq_handler(int vector,void * param)124 static void uart_irq_handler(int vector, void *param)
125 {
126 struct rt_serial_device *serial = (struct rt_serial_device *)param;
127 struct rt_uart_mipssim *uart_dev = RT_NULL;
128
129 RT_ASSERT(serial != RT_NULL);
130
131 uart_dev = (struct rt_uart_mipssim *)serial->parent.user_data;
132 unsigned char iir = UART_IIR(uart_dev->base);
133
134 /* Find out interrupt reason */
135 if ((IIR_RXTOUT & iir) || (IIR_RXRDY & iir))
136 {
137 rt_interrupt_enter();
138 rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
139 rt_interrupt_leave();
140 }
141
142 }
143
144 static const struct rt_uart_ops mipssim_uart_ops =
145 {
146 mipssim_uart_configure,
147 mipssim_uart_control,
148 mipssim_uart_putc,
149 mipssim_uart_getc,
150 };
151
152 struct rt_uart_mipssim uart_dev0 =
153 {
154 (void *)UART0_BASE,
155 4,
156 };
157 struct rt_serial_device serial;
158
159
rt_hw_uart_init(void)160 void rt_hw_uart_init(void)
161 {
162 struct rt_uart_mipssim *uart;
163 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
164
165 uart = &uart_dev0;
166
167 serial.ops = &mipssim_uart_ops;
168 serial.config = config;
169
170 rt_hw_interrupt_install(uart->IRQ, uart_irq_handler, &serial, "UART");
171
172 /* register UART device */
173 rt_hw_serial_register(&serial,
174 "uart",
175 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
176 uart);
177 }
178 /*@}*/
179