1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Email: opensource_embedded@phytium.com.cn
7  *
8  * Change Logs:
9  * Date        Author       Notes
10  * 2022-10-26  huanghe      first commit
11  * 2023-04-27  huanghe      support RT-Smart
12  */
13 
14 #include "rtconfig.h"
15 
16 #include "board.h"
17 #include <mmu.h>
18 #include "drv_usart.h"
19 #include "interrupt.h"
20 #include "fpl011.h"
21 
22 extern u32 FUart_GetInterruptMask(FPl011 *uart_ptr);
23 
24 static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData);
25 
rt_hw_uart_isr(int irqno,void * param)26 static void rt_hw_uart_isr(int irqno, void *param)
27 {
28     FPl011InterruptHandler(irqno, param);
29 }
30 
uart_configure(struct rt_serial_device * serial,struct serial_configure * cfg)31 static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
32 {
33     struct drv_usart *uart = RT_NULL;
34     FPl011 *uart_hw = RT_NULL;
35     u32 intr_mask;
36     FPl011Config config;
37 
38     RT_ASSERT(serial != RT_NULL);
39     RT_ASSERT(cfg != RT_NULL);
40     uart = rt_container_of(serial, struct drv_usart, serial);
41     uart_hw = uart->handle;
42     config = *(const FPl011Config *)FPl011LookupConfig(uart->config.uart_instance);
43 
44 #ifdef RT_USING_SMART
45     config.base_address = (uintptr)rt_ioremap((void *)config.base_address, 0x1000);
46 #endif
47 
48     RT_ASSERT(FPl011CfgInitialize(uart_hw, &config) == FT_SUCCESS);
49     FPl011SetHandler(uart_hw, Ft_Os_Uart_Callback, serial);
50 
51     FPl011SetRxFifoThreadhold(uart_hw, FPL011IFLS_RXIFLSEL_1_4);
52     FPl011SetTxFifoThreadHold(uart_hw, FPL011IFLS_TXIFLSEL_1_2);
53 
54     //<! 打开接收中断
55     intr_mask = uart->config.isr_event_mask;
56     FPl011SetInterruptMask(uart_hw, intr_mask);
57     FPl011SetOptions(uart_hw, FPL011_OPTION_UARTEN | FPL011_OPTION_RXEN | FPL011_OPTION_TXEN | FPL011_OPTION_FIFOEN);
58 
59     rt_hw_interrupt_set_priority(uart_hw->config.irq_num, uart->config.isr_priority);
60     rt_hw_interrupt_install(uart_hw->config.irq_num, rt_hw_uart_isr, uart_hw, "uart");
61     rt_hw_interrupt_umask(uart_hw->config.irq_num);
62 
63     return RT_EOK;
64 }
65 
uart_control(struct rt_serial_device * serial,int cmd,void * arg)66 static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
67 {
68     struct drv_usart *uart = RT_NULL;
69     FPl011 *uart_ptr = RT_NULL;
70     RT_ASSERT(serial != RT_NULL);
71 
72     uart = rt_container_of(serial, struct drv_usart, serial);
73     uart_ptr = uart->handle;
74 
75     switch (cmd)
76     {
77         case RT_DEVICE_CTRL_CLR_INT:
78             /* disable rx irq */
79             rt_hw_interrupt_mask(uart_ptr->config.irq_num);
80             break;
81 
82         case RT_DEVICE_CTRL_SET_INT:
83             /* enable rx irq */
84             rt_hw_interrupt_umask(uart_ptr->config.irq_num);
85             break;
86     }
87 
88     return RT_EOK;
89 }
90 
Ft_Os_Uart_Callback(void * Args,u32 Event,u32 EventData)91 static void Ft_Os_Uart_Callback(void *Args, u32 Event, u32 EventData)
92 {
93     struct rt_serial_device *serial = (struct rt_serial_device *)Args;
94 
95     if (FPL011_EVENT_RECV_DATA == Event || FPL011_EVENT_RECV_TOUT == Event)
96     {
97         if (serial->serial_rx)
98         {
99             rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
100         }
101     }
102     else if (FPL011_EVENT_RECV_ERROR == Event)
103     {
104     }
105     else if (FPL011_EVENT_SENT_DATA == Event)
106     {
107     }
108     else if (FPL011_EVENT_PARE_FRAME_BRKE == Event)
109     {
110     }
111     else if (FPL011_EVENT_RECV_ORERR == Event)
112     {
113     }
114 
115     if (FPL011_EVENT_SENT_DATA == Event)
116     {
117     }
118     else
119     {
120     }
121 }
122 
uart_putc(struct rt_serial_device * serial,char c)123 static int uart_putc(struct rt_serial_device *serial, char c)
124 {
125     struct drv_usart *uart = RT_NULL;
126     FPl011 *uart_ptr = RT_NULL;
127     RT_ASSERT(serial != RT_NULL);
128 
129     uart = rt_container_of(serial, struct drv_usart, serial);
130     uart_ptr = uart->handle;
131 
132     FPl011SendByte(uart_ptr->config.base_address, c);
133 
134     return 1;
135 }
136 
FPl011RecvByteNoBlocking(uintptr addr)137 u32 FPl011RecvByteNoBlocking(uintptr addr)
138 {
139     u32 recieved_byte;
140 
141     while (FUART_RECEIVEDATAEMPTY(addr))
142     {
143         return 0xffff;
144     }
145     recieved_byte = FUART_READREG32(addr, FPL011DR_OFFSET);
146     return recieved_byte;
147 }
148 
uart_getc(struct rt_serial_device * serial)149 static int uart_getc(struct rt_serial_device *serial)
150 {
151     int ch;
152     struct drv_usart *uart = RT_NULL;
153     FPl011 *uart_ptr = RT_NULL;
154     RT_ASSERT(serial != RT_NULL);
155 
156     uart = rt_container_of(serial, struct drv_usart, serial);
157     uart_ptr = uart->handle;
158 
159     ch = FPl011RecvByteNoBlocking(uart_ptr->config.base_address);
160     if (ch == 0xffff)
161     {
162         ch = -1;
163     }
164     else
165     {
166         ch &= 0xff;
167     }
168 
169     return ch;
170 }
171 
172 static const struct rt_uart_ops _uart_ops =
173 {
174     uart_configure,
175     uart_control,
176     uart_putc,
177     uart_getc,
178     NULL
179 };
180 
uart_init(struct drv_usart * uart_dev)181 static int uart_init(struct drv_usart *uart_dev)
182 {
183     struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
184 
185     config.bufsz = RT_SERIAL_RB_BUFSZ;
186     uart_dev->serial.ops = &_uart_ops;
187     uart_dev->serial.config = config;
188 
189     uart_dev->config.isr_priority = 0xd0;
190     uart_dev->config.isr_event_mask = (RTOS_UART_ISR_OEIM_MASK | RTOS_UART_ISR_BEIM_MASK | RTOS_UART_ISR_PEIM_MASK | RTOS_UART_ISR_FEIM_MASK | RTOS_UART_ISR_RTIM_MASK | RTOS_UART_ISR_RXIM_MASK);
191     uart_dev->config.uart_baudrate = 115200;
192 
193     rt_hw_serial_register(&uart_dev->serial, uart_dev->name,
194                           RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
195                           uart_dev);
196 
197     return 0;
198 }
199 
200 #ifdef RT_USING_UART0
201     static FPl011 Ft_Uart0;
202     static struct drv_usart drv_uart0;
203 #endif
204 #ifdef RT_USING_UART1
205     static FPl011 Ft_Uart1;
206     static struct drv_usart drv_uart1;
207 #endif
208 #ifdef RT_USING_UART2
209     static FPl011 Ft_Uart2;
210     static struct drv_usart drv_uart2;
211 #endif
212 
rt_hw_uart_init(void)213 int rt_hw_uart_init(void)
214 {
215 
216 #ifdef RT_USING_UART0
217     drv_uart0.name = "uart0";
218     drv_uart0.handle = &Ft_Uart0;
219     drv_uart0.config.uart_instance = FUART0_ID;
220     uart_init(&drv_uart0);
221 #endif
222 #ifdef RT_USING_UART1
223     drv_uart1.name = "uart1";
224     drv_uart1.handle = &Ft_Uart1;
225     drv_uart1.config.uart_instance = FUART1_ID;
226     uart_init(&drv_uart1);
227 #endif
228 #ifdef RT_USING_UART2
229     drv_uart2.name = "uart2";
230     drv_uart2.handle = &Ft_Uart2;
231     drv_uart2.config.uart_instance = FUART2_ID;
232     uart_init(&drv_uart2);
233 #endif
234 
235     return 0;
236 }
237 INIT_BOARD_EXPORT(rt_hw_uart_init);
238