1 /*
2 * serial.c UART driver
3 * Copyright (c) 2006-2021, RT-Thread Development Team
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Change Logs:
8 * Date Author Notes
9 * 2013-03-30 Bernard the first verion
10 */
11
12 #include <rthw.h>
13 #include <rtdevice.h>
14
15 #include "board.h"
16 #include "mmu.h"
17
18 struct hw_uart_device
19 {
20 rt_size_t hw_base;
21 rt_size_t irqno;
22 };
23
24 #define UART_DR(base) __REG32(base + 0x00)
25 #define UART_FR(base) __REG32(base + 0x18)
26 #define UART_CR(base) __REG32(base + 0x30)
27 #define UART_IMSC(base) __REG32(base + 0x38)
28 #define UART_ICR(base) __REG32(base + 0x44)
29
30 #define UARTFR_RXFE 0x10
31 #define UARTFR_TXFF 0x20
32 #define UARTIMSC_RXIM 0x10
33 #define UARTIMSC_TXIM 0x20
34 #define UARTICR_RXIC 0x10
35 #define UARTICR_TXIC 0x20
36
rt_hw_uart_isr(int irqno,void * param)37 static void rt_hw_uart_isr(int irqno, void *param)
38 {
39 struct rt_serial_device *serial = (struct rt_serial_device *)param;
40
41 rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
42 }
43
uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)44 static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
45 {
46 return RT_EOK;
47 }
48
uart_control(struct rt_serial_device * serial,int cmd,void * arg)49 static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
50 {
51 struct hw_uart_device *uart;
52
53 RT_ASSERT(serial != RT_NULL);
54 uart = (struct hw_uart_device *)serial->parent.user_data;
55
56 switch (cmd)
57 {
58 case RT_DEVICE_CTRL_CLR_INT:
59 /* disable rx irq */
60 UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
61 break;
62
63 case RT_DEVICE_CTRL_SET_INT:
64 /* enable rx irq */
65 UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
66 rt_hw_interrupt_umask(uart->irqno);
67 break;
68
69 default:
70 return -1;
71 }
72
73 return RT_EOK;
74 }
75
uart_putc(struct rt_serial_device * serial,char c)76 static int uart_putc(struct rt_serial_device *serial, char c)
77 {
78 struct hw_uart_device *uart;
79
80 RT_ASSERT(serial != RT_NULL);
81 uart = (struct hw_uart_device *)serial->parent.user_data;
82
83 while (UART_FR(uart->hw_base) & UARTFR_TXFF);
84 UART_DR(uart->hw_base) = c;
85
86 return 1;
87 }
88
uart_getc(struct rt_serial_device * serial)89 static int uart_getc(struct rt_serial_device *serial)
90 {
91 int ch;
92 struct hw_uart_device *uart;
93
94 RT_ASSERT(serial != RT_NULL);
95 uart = (struct hw_uart_device *)serial->parent.user_data;
96
97 ch = -1;
98 if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
99 {
100 ch = UART_DR(uart->hw_base) & 0xff;
101 }
102
103 return ch;
104 }
105
106 static const struct rt_uart_ops _uart_ops =
107 {
108 uart_configure,
109 uart_control,
110 uart_putc,
111 uart_getc,
112 };
113
114 #ifdef RT_USING_UART0
115 /* UART device driver structure */
116 static struct hw_uart_device _uart0_device =
117 {
118 PL011_UART0_BASE,
119 PL011_UART0_IRQNUM,
120 };
121 static struct rt_serial_device _serial0;
122 #endif
123
rt_hw_uart_init(void)124 int rt_hw_uart_init(void)
125 {
126 struct hw_uart_device *uart;
127 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
128
129 #ifdef RT_USING_UART0
130 _uart0_device.hw_base = (rt_size_t)rt_ioremap((void*)_uart0_device.hw_base, PL011_UART0_SIZE);
131 uart = &_uart0_device;
132
133 _serial0.ops = &_uart_ops;
134 _serial0.config = config;
135
136 /* register UART1 device */
137 rt_hw_serial_register(&_serial0, "uart0",
138 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
139 uart);
140 rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
141 /* enable Rx and Tx of UART */
142 UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
143 #endif
144
145 return 0;
146 }
147