1 /*
2 * Copyright (c) 2006-2021, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2018/5/5 Bernard The first version
9 */
10
11 #include <rthw.h>
12 #include <rtthread.h>
13 #include <rtdevice.h>
14
15 #include "raspi.h"
16 #include "board.h"
17 #include "drv_uart.h"
18
19 #define AUX_BASE (0x3F000000 + 0x215000)
20
21 struct hw_uart_device
22 {
23 rt_ubase_t hw_base;
24 rt_uint32_t irqno;
25 };
26
uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)27 static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
28 {
29 struct hw_uart_device *uart;
30
31 RT_ASSERT(serial != RT_NULL);
32 uart = (struct hw_uart_device *)serial->parent.user_data;
33
34 if (uart->hw_base == AUX_BASE)
35 {
36 rt_uint32_t value;
37
38 /* GPIO function set */
39 value = BCM283X_GPIO_GPFSEL(1);
40 value &= ~(7 << 12); /* GPIO14 */
41 value |= 2 << 12 ; /* ALT5 */
42 value &= ~(7 << 15); /* GPIO15 */
43 value |= 2 << 15 ; /* ALT5 */
44 BCM283X_GPIO_GPFSEL(1) = value;
45
46 BCM283X_GPIO_GPPUD = 0;
47 BCM283X_GPIO_GPPUDCLK(0) = (1 << 14) | (1 << 15);
48 BCM283X_GPIO_GPPUDCLK(0) = 0;
49
50 AUX_ENABLES(uart->hw_base) = 1; /* Enable UART1 */
51 AUX_MU_IER_REG(uart->hw_base) = 0; /* Disable interrupt */
52 AUX_MU_CNTL_REG(uart->hw_base) = 0; /* Disable Transmitter and Receiver */
53 AUX_MU_LCR_REG(uart->hw_base) = 3; /* Works in 8-bit mode */
54 AUX_MU_MCR_REG(uart->hw_base) = 0; /* Disable RTS */
55 AUX_MU_IIR_REG(uart->hw_base) = 0xC6; /* Enable FIFO, Clear FIFO */
56 AUX_MU_BAUD_REG(uart->hw_base) = 270; /* 115200 = system clock 250MHz / (8 * (baud + 1)), baud = 270 */
57 AUX_MU_CNTL_REG(uart->hw_base) = 3; /* Enable Transmitter and Receiver */
58 }
59
60 return RT_EOK;
61 }
62
uart_control(struct rt_serial_device * serial,int cmd,void * arg)63 static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
64 {
65 struct hw_uart_device *uart;
66
67 RT_ASSERT(serial != RT_NULL);
68 uart = (struct hw_uart_device *)serial->parent.user_data;
69
70 switch (cmd)
71 {
72 case RT_DEVICE_CTRL_CLR_INT:
73 /* disable rx irq */
74 AUX_MU_IER_REG(uart->hw_base) = 0x0;
75 rt_hw_interrupt_mask(uart->irqno);
76 break;
77
78 case RT_DEVICE_CTRL_SET_INT:
79 /* enable rx irq */
80 AUX_MU_IER_REG(uart->hw_base) = 0x1;
81 rt_hw_interrupt_umask(uart->irqno);
82 break;
83 }
84
85 return RT_EOK;
86 }
87
uart_putc(struct rt_serial_device * serial,char c)88 static int uart_putc(struct rt_serial_device *serial, char c)
89 {
90 struct hw_uart_device *uart;
91
92 RT_ASSERT(serial != RT_NULL);
93 uart = (struct hw_uart_device *)serial->parent.user_data;
94
95 while (!(AUX_MU_LSR_REG(uart->hw_base) & 0x20));
96 AUX_MU_IO_REG(uart->hw_base) = c;
97
98 return 1;
99 }
100
uart_getc(struct rt_serial_device * serial)101 static int uart_getc(struct rt_serial_device *serial)
102 {
103 int ch = -1;
104 struct hw_uart_device *uart;
105
106 RT_ASSERT(serial != RT_NULL);
107 uart = (struct hw_uart_device *)serial->parent.user_data;
108
109 if ((AUX_MU_LSR_REG(uart->hw_base) & 0x01))
110 {
111 ch = AUX_MU_IO_REG(uart->hw_base) & 0xff;
112 }
113
114 return ch;
115 }
116
117 static const struct rt_uart_ops _uart_ops =
118 {
119 uart_configure,
120 uart_control,
121 uart_putc,
122 uart_getc,
123 };
124
rt_hw_uart_isr(int irqno,void * param)125 static void rt_hw_uart_isr(int irqno, void *param)
126 {
127 struct rt_serial_device *serial = (struct rt_serial_device*)param;
128 rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
129 }
130
131 #ifdef RT_USING_UART0
132 /* UART device driver structure */
133 static struct hw_uart_device _uart0_device =
134 {
135 RPI_UART0_BASE,
136 IRQ_PBA8_UART0,
137 };
138 static struct rt_serial_device _serial0;
139 #endif
140
141 #ifdef RT_USING_UART1
142 /* UART1 device driver structure */
143 static struct hw_uart_device _uart1_device =
144 {
145 AUX_BASE,
146 IRQ_AUX,
147 };
148 static struct rt_serial_device _serial1;
149 #endif
150
rt_hw_uart_init(void)151 int rt_hw_uart_init(void)
152 {
153 struct hw_uart_device *uart;
154 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
155
156 #ifdef RT_USING_UART0
157 uart = &_uart0_device;
158
159 _serial0.ops = &_uart_ops;
160 _serial0.config = config;
161
162 /* register UART1 device */
163 rt_hw_serial_register(&_serial0, "uart0",
164 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
165 uart);
166 rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
167 #endif
168
169 #ifdef RT_USING_UART1
170 uart = &_uart1_device;
171 _serial1.ops = &_uart_ops;
172 _serial1.config = config;
173
174 /* register UART1 device */
175 rt_hw_serial_register(&_serial1, "uart1",
176 RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
177 /* enable Rx and Tx of UART */
178 rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial1, "uart1");
179 #endif
180
181 return 0;
182 }
183