1 /*
2  * Copyright (c) 2018 Diego Sueiro <diego.sueiro@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT nxp_imx_uart
8 
9 /**
10  * @brief Driver for UART on NXP IMX family processor.
11  *
12  * For full serial function, use the USART controller.
13  *
14  */
15 
16 #include <kernel.h>
17 #include <arch/cpu.h>
18 #include <sys/__assert.h>
19 #include <soc.h>
20 #include <init.h>
21 #include <drivers/uart.h>
22 #include <uart_imx.h>
23 
24 #define DEV_CFG(dev) \
25 	((const struct imx_uart_config *const)(dev)->config)
26 #define UART_STRUCT(dev) \
27 	((UART_Type *)(DEV_CFG(dev))->base)
28 
29 struct imx_uart_config {
30 	UART_Type *base;
31 	uint32_t baud_rate;
32 	uint8_t modem_mode;
33 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
34 	void (*irq_config_func)(const struct device *dev);
35 #endif
36 };
37 
38 struct imx_uart_data {
39 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
40 	uart_irq_callback_user_data_t callback;
41 	void *cb_data;
42 #endif
43 };
44 
45 /**
46  * @brief Initialize UART channel
47  *
48  * This routine is called to reset the chip in a quiescent state.
49  * It is assumed that this function is called only once per UART.
50  *
51  * @param dev UART device struct
52  *
53  * @return 0
54  */
uart_imx_init(const struct device * dev)55 static int uart_imx_init(const struct device *dev)
56 {
57 	UART_Type *uart = UART_STRUCT(dev);
58 	const struct imx_uart_config *config = dev->config;
59 	unsigned int old_level;
60 
61 	/* disable interrupts */
62 	old_level = irq_lock();
63 
64 	/* Setup UART init structure */
65 	uart_init_config_t initConfig = {
66 		.baudRate	= config->baud_rate,
67 		.wordLength = uartWordLength8Bits,
68 		.stopBitNum = uartStopBitNumOne,
69 		.parity		= uartParityDisable,
70 		.direction	= uartDirectionTxRx
71 	};
72 
73 	/* Get current module clock frequency */
74 	initConfig.clockRate  = get_uart_clock_freq(uart);
75 
76 	UART_Init(uart, &initConfig);
77 
78 	/* Set UART build-in hardware FIFO Watermark. */
79 	UART_SetTxFifoWatermark(uart, 2);
80 	UART_SetRxFifoWatermark(uart, 1);
81 
82 	/* restore interrupt state */
83 	irq_unlock(old_level);
84 
85 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
86 	config->irq_config_func(dev);
87 #endif
88 
89 	/* Set UART modem mode */
90 	UART_SetModemMode(uart, config->modem_mode);
91 
92 	/* Finally, enable the UART module */
93 	UART_Enable(uart);
94 
95 	return 0;
96 }
97 
uart_imx_poll_out(const struct device * dev,unsigned char c)98 static void uart_imx_poll_out(const struct device *dev, unsigned char c)
99 {
100 	UART_Type *uart = UART_STRUCT(dev);
101 
102 	while (!UART_GetStatusFlag(uart, uartStatusTxReady)) {
103 	}
104 	UART_Putchar(uart, c);
105 }
106 
uart_imx_poll_in(const struct device * dev,unsigned char * c)107 static int uart_imx_poll_in(const struct device *dev, unsigned char *c)
108 {
109 	UART_Type *uart = UART_STRUCT(dev);
110 
111 	while (!UART_GetStatusFlag(uart, uartStatusRxDataReady)) {
112 	}
113 	*c = UART_Getchar(uart);
114 
115 	if (UART_GetStatusFlag(uart, uartStatusRxOverrun)) {
116 		UART_ClearStatusFlag(uart, uartStatusRxOverrun);
117 	}
118 
119 	return 0;
120 }
121 
122 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
123 
uart_imx_fifo_fill(const struct device * dev,const uint8_t * tx_data,int size)124 static int uart_imx_fifo_fill(const struct device *dev,
125 				  const uint8_t *tx_data,
126 				  int size)
127 {
128 	UART_Type *uart = UART_STRUCT(dev);
129 	unsigned int num_tx = 0U;
130 
131 	while (((size - num_tx) > 0) &&
132 		   UART_GetStatusFlag(uart, uartStatusTxReady)) {
133 		/* Send a character */
134 		UART_Putchar(uart, tx_data[num_tx]);
135 		num_tx++;
136 	}
137 
138 	return (int)num_tx;
139 }
140 
uart_imx_fifo_read(const struct device * dev,uint8_t * rx_data,const int size)141 static int uart_imx_fifo_read(const struct device *dev, uint8_t *rx_data,
142 				  const int size)
143 {
144 	UART_Type *uart = UART_STRUCT(dev);
145 	unsigned int num_rx = 0U;
146 
147 	while (((size - num_rx) > 0) &&
148 		   UART_GetStatusFlag(uart, uartStatusRxReady)) {
149 		/* Receive a character */
150 		rx_data[num_rx++] = UART_Getchar(uart);
151 	}
152 
153 	if (UART_GetStatusFlag(uart, uartStatusRxOverrun)) {
154 		UART_ClearStatusFlag(uart, uartStatusRxOverrun);
155 	}
156 
157 	return num_rx;
158 }
159 
uart_imx_irq_tx_enable(const struct device * dev)160 static void uart_imx_irq_tx_enable(const struct device *dev)
161 {
162 	UART_Type *uart = UART_STRUCT(dev);
163 
164 	UART_SetIntCmd(uart, uartIntTxReady, true);
165 }
166 
uart_imx_irq_tx_disable(const struct device * dev)167 static void uart_imx_irq_tx_disable(const struct device *dev)
168 {
169 	UART_Type *uart = UART_STRUCT(dev);
170 
171 	UART_SetIntCmd(uart, uartIntTxReady, false);
172 }
173 
uart_imx_irq_tx_ready(const struct device * dev)174 static int uart_imx_irq_tx_ready(const struct device *dev)
175 {
176 	UART_Type *uart = UART_STRUCT(dev);
177 
178 	return UART_GetStatusFlag(uart, uartStatusTxReady);
179 }
180 
uart_imx_irq_rx_enable(const struct device * dev)181 static void uart_imx_irq_rx_enable(const struct device *dev)
182 {
183 	UART_Type *uart = UART_STRUCT(dev);
184 
185 	UART_SetIntCmd(uart, uartIntRxReady, true);
186 }
187 
uart_imx_irq_rx_disable(const struct device * dev)188 static void uart_imx_irq_rx_disable(const struct device *dev)
189 {
190 	UART_Type *uart = UART_STRUCT(dev);
191 
192 	UART_SetIntCmd(uart, uartIntRxReady, false);
193 }
194 
uart_imx_irq_rx_ready(const struct device * dev)195 static int uart_imx_irq_rx_ready(const struct device *dev)
196 {
197 	UART_Type *uart = UART_STRUCT(dev);
198 
199 	return UART_GetStatusFlag(uart, uartStatusRxReady);
200 }
201 
uart_imx_irq_err_enable(const struct device * dev)202 static void uart_imx_irq_err_enable(const struct device *dev)
203 {
204 	UART_Type *uart = UART_STRUCT(dev);
205 
206 	UART_SetIntCmd(uart, uartIntParityError, true);
207 	UART_SetIntCmd(uart, uartIntFrameError, true);
208 }
209 
uart_imx_irq_err_disable(const struct device * dev)210 static void uart_imx_irq_err_disable(const struct device *dev)
211 {
212 	UART_Type *uart = UART_STRUCT(dev);
213 
214 	UART_SetIntCmd(uart, uartIntParityError, false);
215 	UART_SetIntCmd(uart, uartIntFrameError, false);
216 }
217 
uart_imx_irq_is_pending(const struct device * dev)218 static int uart_imx_irq_is_pending(const struct device *dev)
219 {
220 	UART_Type *uart = UART_STRUCT(dev);
221 
222 	return UART_GetStatusFlag(uart, uartStatusRxReady) ||
223 		UART_GetStatusFlag(uart, uartStatusTxReady);
224 }
225 
uart_imx_irq_update(const struct device * dev)226 static int uart_imx_irq_update(const struct device *dev)
227 {
228 	return 1;
229 }
230 
uart_imx_irq_callback_set(const struct device * dev,uart_irq_callback_user_data_t cb,void * cb_data)231 static void uart_imx_irq_callback_set(const struct device *dev,
232 				      uart_irq_callback_user_data_t cb,
233 				      void *cb_data)
234 {
235 	struct imx_uart_data *data = dev->data;
236 
237 	data->callback = cb;
238 	data->cb_data = cb_data;
239 }
240 
241 /**
242  * @brief Interrupt service routine.
243  *
244  * This simply calls the callback function, if one exists.
245  *
246  * Note: imx UART Tx interrupts when ready to send; Rx interrupts when char
247  * received.
248  *
249  * @param arg Argument to ISR.
250  *
251  * @return N/A
252  */
uart_imx_isr(const struct device * dev)253 void uart_imx_isr(const struct device *dev)
254 {
255 	struct imx_uart_data *data = dev->data;
256 
257 	if (data->callback) {
258 		data->callback(dev, data->cb_data);
259 	}
260 }
261 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
262 
263 static const struct uart_driver_api uart_imx_driver_api = {
264 	.poll_in  = uart_imx_poll_in,
265 	.poll_out = uart_imx_poll_out,
266 
267 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
268 	.fifo_fill		  = uart_imx_fifo_fill,
269 	.fifo_read		  = uart_imx_fifo_read,
270 	.irq_tx_enable	  = uart_imx_irq_tx_enable,
271 	.irq_tx_disable   = uart_imx_irq_tx_disable,
272 	.irq_tx_ready	  = uart_imx_irq_tx_ready,
273 	.irq_rx_enable	  = uart_imx_irq_rx_enable,
274 	.irq_rx_disable   = uart_imx_irq_rx_disable,
275 	.irq_rx_ready	  = uart_imx_irq_rx_ready,
276 	.irq_err_enable   = uart_imx_irq_err_enable,
277 	.irq_err_disable  = uart_imx_irq_err_disable,
278 	.irq_is_pending   = uart_imx_irq_is_pending,
279 	.irq_update		  = uart_imx_irq_update,
280 	.irq_callback_set = uart_imx_irq_callback_set,
281 #endif	/* CONFIG_UART_INTERRUPT_DRIVEN */
282 
283 };
284 
285 #define UART_IMX_DECLARE_CFG(n, IRQ_FUNC_INIT)				\
286 	static const struct imx_uart_config imx_uart_##n##_config = {	\
287 		.base = (UART_Type *) DT_INST_REG_ADDR(n),		\
288 		.baud_rate = DT_INST_PROP(n, current_speed),		\
289 		.modem_mode = DT_INST_PROP(n, modem_mode),		\
290 		IRQ_FUNC_INIT						\
291 	}
292 
293 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
294 #define UART_IMX_CONFIG_FUNC(n)						\
295 	static void irq_config_func_##n(const struct device *dev)		\
296 	{								\
297 		IRQ_CONNECT(DT_INST_IRQN(n),				\
298 				DT_INST_IRQ(n, priority),		\
299 				uart_imx_isr,				\
300 				DEVICE_DT_INST_GET(n), 0);		\
301 		irq_enable(DT_INST_IRQN(n));				\
302 	}
303 #define UART_IMX_IRQ_CFG_FUNC_INIT(n)					\
304 	.irq_config_func = irq_config_func_##n
305 #define UART_IMX_INIT_CFG(n)						\
306 	UART_IMX_DECLARE_CFG(n, UART_IMX_IRQ_CFG_FUNC_INIT(n))
307 #else
308 #define UART_IMX_CONFIG_FUNC(n)
309 #define UART_IMX_IRQ_CFG_FUNC_INIT
310 #define UART_IMX_INIT_CFG(n)						\
311 	UART_IMX_DECLARE_CFG(n, UART_IMX_IRQ_CFG_FUNC_INIT)
312 #endif
313 
314 #define UART_IMX_INIT(n)						\
315 	static struct imx_uart_data imx_uart_##n##_data;		\
316 									\
317 	static const struct imx_uart_config imx_uart_##n##_config;	\
318 									\
319 	DEVICE_DT_INST_DEFINE(n, &uart_imx_init, NULL,			\
320 			&imx_uart_##n##_data, &imx_uart_##n##_config,	\
321 			PRE_KERNEL_1,					\
322 			CONFIG_SERIAL_INIT_PRIORITY,			\
323 			&uart_imx_driver_api);				\
324 									\
325 	UART_IMX_CONFIG_FUNC(n)						\
326 									\
327 	UART_IMX_INIT_CFG(n);
328 
329 DT_INST_FOREACH_STATUS_OKAY(UART_IMX_INIT)
330