1 /*
2  * Copyright (c) 2017,2021 NXP
3  * Copyright (c) 2020 Softube
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #define DT_DRV_COMPAT nxp_kinetis_lpuart
9 
10 #include <errno.h>
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/uart.h>
13 #include <zephyr/drivers/clock_control.h>
14 #include <zephyr/irq.h>
15 #include <zephyr/kernel.h>
16 #include <zephyr/pm/policy.h>
17 #include <zephyr/drivers/pinctrl.h>
18 #ifdef CONFIG_UART_ASYNC_API
19 #include <zephyr/drivers/dma.h>
20 #endif
21 #include <zephyr/logging/log.h>
22 
23 #include <fsl_lpuart.h>
24 
25 LOG_MODULE_REGISTER(uart_mcux_lpuart, LOG_LEVEL_ERR);
26 
27 #ifdef CONFIG_UART_ASYNC_API
28 struct lpuart_dma_config {
29 	const struct device *dma_dev;
30 	const uint32_t dma_channel;
31 	struct dma_config dma_cfg;
32 };
33 #endif /* CONFIG_UART_ASYNC_API */
34 
35 struct mcux_lpuart_config {
36 	LPUART_Type *base;
37 	const struct device *clock_dev;
38 	const struct pinctrl_dev_config *pincfg;
39 	clock_control_subsys_t clock_subsys;
40 	uint32_t baud_rate;
41 	uint8_t flow_ctrl;
42 	uint8_t parity;
43 	bool rs485_de_active_low;
44 	bool loopback_en;
45 #ifdef CONFIG_UART_MCUX_LPUART_ISR_SUPPORT
46 	void (*irq_config_func)(const struct device *dev);
47 #endif
48 #ifdef CONFIG_UART_ASYNC_API
49 	const struct lpuart_dma_config rx_dma_config;
50 	const struct lpuart_dma_config tx_dma_config;
51 #endif /* CONFIG_UART_ASYNC_API */
52 };
53 
54 #ifdef CONFIG_UART_ASYNC_API
55 struct mcux_lpuart_rx_dma_params {
56 	struct dma_block_config active_dma_block;
57 	uint8_t *buf;
58 	size_t buf_len;
59 	size_t offset;
60 	size_t counter;
61 	struct k_work_delayable timeout_work;
62 	size_t timeout_us;
63 };
64 
65 struct mcux_lpuart_tx_dma_params {
66 	struct dma_block_config active_dma_block;
67 	const uint8_t *buf;
68 	size_t buf_len;
69 	struct k_work_delayable timeout_work;
70 	size_t timeout_us;
71 };
72 
73 struct mcux_lpuart_async_data {
74 	const struct device *uart_dev;
75 	struct mcux_lpuart_tx_dma_params tx_dma_params;
76 	struct mcux_lpuart_rx_dma_params rx_dma_params;
77 	uint8_t *next_rx_buffer;
78 	size_t next_rx_buffer_len;
79 	uart_callback_t user_callback;
80 	void *user_data;
81 };
82 #endif
83 
84 struct mcux_lpuart_data {
85 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
86 	uart_irq_callback_user_data_t callback;
87 	void *cb_data;
88 #endif
89 #ifdef CONFIG_PM
90 	bool pm_state_lock_on;
91 	bool tx_poll_stream_on;
92 	bool tx_int_stream_on;
93 #endif /* CONFIG_PM */
94 #ifdef CONFIG_UART_ASYNC_API
95 	struct mcux_lpuart_async_data async;
96 #endif
97 	struct uart_config uart_config;
98 };
99 
100 #ifdef CONFIG_PM
mcux_lpuart_pm_policy_state_lock_get(const struct device * dev)101 static void mcux_lpuart_pm_policy_state_lock_get(const struct device *dev)
102 {
103 	struct mcux_lpuart_data *data = dev->data;
104 
105 	if (!data->pm_state_lock_on) {
106 		data->pm_state_lock_on = true;
107 		pm_policy_state_lock_get(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
108 	}
109 }
110 
mcux_lpuart_pm_policy_state_lock_put(const struct device * dev)111 static void mcux_lpuart_pm_policy_state_lock_put(const struct device *dev)
112 {
113 	struct mcux_lpuart_data *data = dev->data;
114 
115 	if (data->pm_state_lock_on) {
116 		data->pm_state_lock_on = false;
117 		pm_policy_state_lock_put(PM_STATE_SUSPEND_TO_IDLE, PM_ALL_SUBSTATES);
118 	}
119 }
120 #endif /* CONFIG_PM */
121 
mcux_lpuart_poll_in(const struct device * dev,unsigned char * c)122 static int mcux_lpuart_poll_in(const struct device *dev, unsigned char *c)
123 {
124 	const struct mcux_lpuart_config *config = dev->config;
125 	uint32_t flags = LPUART_GetStatusFlags(config->base);
126 	int ret = -1;
127 
128 	if (flags & kLPUART_RxDataRegFullFlag) {
129 		*c = LPUART_ReadByte(config->base);
130 		ret = 0;
131 	}
132 
133 	return ret;
134 }
135 
mcux_lpuart_poll_out(const struct device * dev,unsigned char c)136 static void mcux_lpuart_poll_out(const struct device *dev, unsigned char c)
137 {
138 	const struct mcux_lpuart_config *config = dev->config;
139 	unsigned int key;
140 #ifdef CONFIG_PM
141 	struct mcux_lpuart_data *data = dev->data;
142 #endif
143 
144 	while (!(LPUART_GetStatusFlags(config->base)
145 		& kLPUART_TxDataRegEmptyFlag)) {
146 	}
147 	/* Lock interrupts while we send data */
148 	key = irq_lock();
149 #ifdef CONFIG_PM
150 	/*
151 	 * We must keep the part from entering lower power mode until the
152 	 * transmission completes. Set the power constraint, and enable
153 	 * the transmission complete interrupt so we know when transmission is
154 	 * completed.
155 	 */
156 	if (!data->tx_poll_stream_on && !data->tx_int_stream_on) {
157 		data->tx_poll_stream_on = true;
158 		mcux_lpuart_pm_policy_state_lock_get(dev);
159 		/* Enable TC interrupt */
160 		LPUART_EnableInterrupts(config->base,
161 			kLPUART_TransmissionCompleteInterruptEnable);
162 
163 	}
164 #endif /* CONFIG_PM */
165 
166 	LPUART_WriteByte(config->base, c);
167 	irq_unlock(key);
168 }
169 
mcux_lpuart_err_check(const struct device * dev)170 static int mcux_lpuart_err_check(const struct device *dev)
171 {
172 	const struct mcux_lpuart_config *config = dev->config;
173 	uint32_t flags = LPUART_GetStatusFlags(config->base);
174 	int err = 0;
175 
176 	if (flags & kLPUART_RxOverrunFlag) {
177 		err |= UART_ERROR_OVERRUN;
178 	}
179 
180 	if (flags & kLPUART_ParityErrorFlag) {
181 		err |= UART_ERROR_PARITY;
182 	}
183 
184 	if (flags & kLPUART_FramingErrorFlag) {
185 		err |= UART_ERROR_FRAMING;
186 	}
187 
188 	if (flags & kLPUART_NoiseErrorFlag) {
189 		err |= UART_ERROR_PARITY;
190 	}
191 
192 	LPUART_ClearStatusFlags(config->base, kLPUART_RxOverrunFlag |
193 					      kLPUART_ParityErrorFlag |
194 					      kLPUART_FramingErrorFlag |
195 						  kLPUART_NoiseErrorFlag);
196 
197 	return err;
198 }
199 
200 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
mcux_lpuart_fifo_fill(const struct device * dev,const uint8_t * tx_data,int len)201 static int mcux_lpuart_fifo_fill(const struct device *dev,
202 				 const uint8_t *tx_data,
203 				 int len)
204 {
205 	const struct mcux_lpuart_config *config = dev->config;
206 	uint8_t num_tx = 0U;
207 
208 	while ((len - num_tx > 0) &&
209 	       (LPUART_GetStatusFlags(config->base)
210 		& kLPUART_TxDataRegEmptyFlag)) {
211 
212 		LPUART_WriteByte(config->base, tx_data[num_tx++]);
213 	}
214 	return num_tx;
215 }
216 
mcux_lpuart_fifo_read(const struct device * dev,uint8_t * rx_data,const int len)217 static int mcux_lpuart_fifo_read(const struct device *dev, uint8_t *rx_data,
218 				 const int len)
219 {
220 	const struct mcux_lpuart_config *config = dev->config;
221 	uint8_t num_rx = 0U;
222 
223 	while ((len - num_rx > 0) &&
224 	       (LPUART_GetStatusFlags(config->base)
225 		& kLPUART_RxDataRegFullFlag)) {
226 
227 		rx_data[num_rx++] = LPUART_ReadByte(config->base);
228 	}
229 
230 	return num_rx;
231 }
232 
mcux_lpuart_irq_tx_enable(const struct device * dev)233 static void mcux_lpuart_irq_tx_enable(const struct device *dev)
234 {
235 	const struct mcux_lpuart_config *config = dev->config;
236 	uint32_t mask = kLPUART_TxDataRegEmptyInterruptEnable;
237 #ifdef CONFIG_PM
238 	struct mcux_lpuart_data *data = dev->data;
239 	unsigned int key;
240 #endif
241 
242 #ifdef CONFIG_PM
243 	key = irq_lock();
244 	data->tx_poll_stream_on = false;
245 	data->tx_int_stream_on = true;
246 	/* Transmission complete interrupt no longer required */
247 	LPUART_DisableInterrupts(config->base,
248 		kLPUART_TransmissionCompleteInterruptEnable);
249 	/* Do not allow system to sleep while UART tx is ongoing */
250 	mcux_lpuart_pm_policy_state_lock_get(dev);
251 #endif
252 	LPUART_EnableInterrupts(config->base, mask);
253 #ifdef CONFIG_PM
254 	irq_unlock(key);
255 #endif
256 }
257 
mcux_lpuart_irq_tx_disable(const struct device * dev)258 static void mcux_lpuart_irq_tx_disable(const struct device *dev)
259 {
260 	const struct mcux_lpuart_config *config = dev->config;
261 	uint32_t mask = kLPUART_TxDataRegEmptyInterruptEnable;
262 #ifdef CONFIG_PM
263 	struct mcux_lpuart_data *data = dev->data;
264 	unsigned int key;
265 
266 	key = irq_lock();
267 #endif
268 
269 	LPUART_DisableInterrupts(config->base, mask);
270 #ifdef CONFIG_PM
271 	data->tx_int_stream_on = false;
272 	/*
273 	 * If transmission IRQ is no longer enabled,
274 	 * transmission is complete. Release pm constraint.
275 	 */
276 	mcux_lpuart_pm_policy_state_lock_put(dev);
277 	irq_unlock(key);
278 #endif
279 }
280 
mcux_lpuart_irq_tx_complete(const struct device * dev)281 static int mcux_lpuart_irq_tx_complete(const struct device *dev)
282 {
283 	const struct mcux_lpuart_config *config = dev->config;
284 	uint32_t flags = LPUART_GetStatusFlags(config->base);
285 
286 	return (flags & kLPUART_TransmissionCompleteFlag) != 0U;
287 }
288 
mcux_lpuart_irq_tx_ready(const struct device * dev)289 static int mcux_lpuart_irq_tx_ready(const struct device *dev)
290 {
291 	const struct mcux_lpuart_config *config = dev->config;
292 	uint32_t mask = kLPUART_TxDataRegEmptyInterruptEnable;
293 	uint32_t flags = LPUART_GetStatusFlags(config->base);
294 
295 	return (LPUART_GetEnabledInterrupts(config->base) & mask)
296 		&& (flags & kLPUART_TxDataRegEmptyFlag);
297 }
298 
mcux_lpuart_irq_rx_enable(const struct device * dev)299 static void mcux_lpuart_irq_rx_enable(const struct device *dev)
300 {
301 	const struct mcux_lpuart_config *config = dev->config;
302 	uint32_t mask = kLPUART_RxDataRegFullInterruptEnable;
303 
304 	LPUART_EnableInterrupts(config->base, mask);
305 	LPUART_EnableRx(config->base, true);
306 }
307 
mcux_lpuart_irq_rx_disable(const struct device * dev)308 static void mcux_lpuart_irq_rx_disable(const struct device *dev)
309 {
310 	const struct mcux_lpuart_config *config = dev->config;
311 	uint32_t mask = kLPUART_RxDataRegFullInterruptEnable;
312 
313 	LPUART_EnableRx(config->base, false);
314 	LPUART_DisableInterrupts(config->base, mask);
315 }
316 
mcux_lpuart_irq_rx_full(const struct device * dev)317 static int mcux_lpuart_irq_rx_full(const struct device *dev)
318 {
319 	const struct mcux_lpuart_config *config = dev->config;
320 	uint32_t flags = LPUART_GetStatusFlags(config->base);
321 
322 	return (flags & kLPUART_RxDataRegFullFlag) != 0U;
323 }
324 
mcux_lpuart_irq_rx_pending(const struct device * dev)325 static int mcux_lpuart_irq_rx_pending(const struct device *dev)
326 {
327 	const struct mcux_lpuart_config *config = dev->config;
328 	uint32_t mask = kLPUART_RxDataRegFullInterruptEnable;
329 
330 	return (LPUART_GetEnabledInterrupts(config->base) & mask)
331 		&& mcux_lpuart_irq_rx_full(dev);
332 }
333 
mcux_lpuart_irq_err_enable(const struct device * dev)334 static void mcux_lpuart_irq_err_enable(const struct device *dev)
335 {
336 	const struct mcux_lpuart_config *config = dev->config;
337 	uint32_t mask = kLPUART_NoiseErrorInterruptEnable |
338 			kLPUART_FramingErrorInterruptEnable |
339 			kLPUART_ParityErrorInterruptEnable;
340 
341 	LPUART_EnableInterrupts(config->base, mask);
342 }
343 
mcux_lpuart_irq_err_disable(const struct device * dev)344 static void mcux_lpuart_irq_err_disable(const struct device *dev)
345 {
346 	const struct mcux_lpuart_config *config = dev->config;
347 	uint32_t mask = kLPUART_NoiseErrorInterruptEnable |
348 			kLPUART_FramingErrorInterruptEnable |
349 			kLPUART_ParityErrorInterruptEnable;
350 
351 	LPUART_DisableInterrupts(config->base, mask);
352 }
353 
mcux_lpuart_irq_is_pending(const struct device * dev)354 static int mcux_lpuart_irq_is_pending(const struct device *dev)
355 {
356 	return (mcux_lpuart_irq_tx_ready(dev)
357 		|| mcux_lpuart_irq_rx_pending(dev));
358 }
359 
mcux_lpuart_irq_update(const struct device * dev)360 static int mcux_lpuart_irq_update(const struct device *dev)
361 {
362 	return 1;
363 }
364 
mcux_lpuart_irq_callback_set(const struct device * dev,uart_irq_callback_user_data_t cb,void * cb_data)365 static void mcux_lpuart_irq_callback_set(const struct device *dev,
366 					 uart_irq_callback_user_data_t cb,
367 					 void *cb_data)
368 {
369 	struct mcux_lpuart_data *data = dev->data;
370 
371 	data->callback = cb;
372 	data->cb_data = cb_data;
373 
374 #if defined(CONFIG_UART_EXCLUSIVE_API_CALLBACKS)
375 	data->async.user_callback = NULL;
376 	data->async.user_data = NULL;
377 #endif
378 }
379 
380 #endif /* CONFIG_UART_INTERRUPT_DRIVEN */
381 
382 
383 #ifdef CONFIG_UART_ASYNC_API
async_timer_start(struct k_work_delayable * work,size_t timeout_us)384 static inline void async_timer_start(struct k_work_delayable *work, size_t timeout_us)
385 {
386 	if ((timeout_us != SYS_FOREVER_US) && (timeout_us != 0)) {
387 		LOG_DBG("async timer started for %d us", timeout_us);
388 		k_work_reschedule(work, K_USEC(timeout_us));
389 	}
390 }
391 
async_user_callback(const struct device * dev,struct uart_event * evt)392 static void async_user_callback(const struct device *dev, struct uart_event *evt)
393 {
394 	const struct mcux_lpuart_data *data = dev->data;
395 
396 	if (data->async.user_callback) {
397 		data->async.user_callback(dev, evt, data->async.user_data);
398 	}
399 }
400 
async_evt_tx_done(struct device * dev)401 static void async_evt_tx_done(struct device *dev)
402 {
403 	struct mcux_lpuart_data *data = dev->data;
404 
405 	(void)k_work_cancel_delayable(&data->async.tx_dma_params.timeout_work);
406 
407 	LOG_DBG("TX done: %d", data->async.tx_dma_params.buf_len);
408 	struct uart_event event = {
409 		.type = UART_TX_DONE,
410 		.data.tx.buf = data->async.tx_dma_params.buf,
411 		.data.tx.len = data->async.tx_dma_params.buf_len
412 	};
413 
414 	/* Reset TX Buffer */
415 	data->async.tx_dma_params.buf = NULL;
416 	data->async.tx_dma_params.buf_len = 0U;
417 
418 	async_user_callback(dev, &event);
419 }
420 
async_evt_rx_rdy(const struct device * dev)421 static void async_evt_rx_rdy(const struct device *dev)
422 {
423 	struct mcux_lpuart_data *data = dev->data;
424 	struct mcux_lpuart_rx_dma_params *dma_params = &data->async.rx_dma_params;
425 
426 	struct uart_event event = {
427 		.type = UART_RX_RDY,
428 		.data.rx.buf = dma_params->buf,
429 		.data.rx.len = dma_params->counter - dma_params->offset,
430 		.data.rx.offset = dma_params->offset
431 	};
432 
433 	LOG_DBG("RX Ready: (len: %d off: %d buf: %x)", event.data.rx.len, event.data.rx.offset,
434 		(uint32_t)event.data.rx.buf);
435 
436 	/* Update the current pos for new data */
437 	dma_params->offset = dma_params->counter;
438 
439 	/* Only send event for new data */
440 	if (event.data.rx.len > 0) {
441 		async_user_callback(dev, &event);
442 	}
443 }
444 
async_evt_rx_buf_request(const struct device * dev)445 static void async_evt_rx_buf_request(const struct device *dev)
446 {
447 	struct uart_event evt = {
448 		.type = UART_RX_BUF_REQUEST,
449 	};
450 
451 	async_user_callback(dev, &evt);
452 }
453 
async_evt_rx_buf_release(const struct device * dev)454 static void async_evt_rx_buf_release(const struct device *dev)
455 {
456 	struct mcux_lpuart_data *data = (struct mcux_lpuart_data *)dev->data;
457 	struct uart_event evt = {
458 		.type = UART_RX_BUF_RELEASED,
459 		.data.rx_buf.buf = data->async.rx_dma_params.buf,
460 	};
461 
462 	async_user_callback(dev, &evt);
463 	data->async.rx_dma_params.buf = NULL;
464 	data->async.rx_dma_params.buf_len = 0U;
465 	data->async.rx_dma_params.offset = 0U;
466 	data->async.rx_dma_params.counter = 0U;
467 }
468 
mcux_lpuart_async_rx_flush(const struct device * dev)469 static void mcux_lpuart_async_rx_flush(const struct device *dev)
470 {
471 	struct dma_status status;
472 	struct mcux_lpuart_data *data = dev->data;
473 	const struct mcux_lpuart_config *config = dev->config;
474 
475 	const int get_status_result = dma_get_status(config->rx_dma_config.dma_dev,
476 						     config->rx_dma_config.dma_channel,
477 						     &status);
478 
479 	if (get_status_result == 0) {
480 		const size_t rx_rcv_len = data->async.rx_dma_params.buf_len -
481 					  status.pending_length;
482 
483 		if (rx_rcv_len > data->async.rx_dma_params.counter) {
484 			data->async.rx_dma_params.counter = rx_rcv_len;
485 			async_evt_rx_rdy(dev);
486 		}
487 	} else {
488 		LOG_ERR("Error getting DMA status");
489 	}
490 }
491 
mcux_lpuart_rx_disable(const struct device * dev)492 static int mcux_lpuart_rx_disable(const struct device *dev)
493 {
494 	LOG_INF("Disabling UART RX DMA");
495 	const struct mcux_lpuart_config *config = dev->config;
496 	struct mcux_lpuart_data *data = (struct mcux_lpuart_data *)dev->data;
497 	LPUART_Type *lpuart = config->base;
498 	const unsigned int key = irq_lock();
499 
500 	LPUART_EnableRx(lpuart, false);
501 	(void)k_work_cancel_delayable(&data->async.rx_dma_params.timeout_work);
502 	LPUART_DisableInterrupts(lpuart, kLPUART_IdleLineInterruptEnable);
503 	LPUART_ClearStatusFlags(lpuart, kLPUART_IdleLineFlag);
504 	LPUART_EnableRxDMA(lpuart, false);
505 
506 	/* No active RX buffer, cannot disable */
507 	if (!data->async.rx_dma_params.buf) {
508 		LOG_ERR("No buffers to release from RX DMA!");
509 	} else {
510 		mcux_lpuart_async_rx_flush(dev);
511 		async_evt_rx_buf_release(dev);
512 		if (data->async.next_rx_buffer != NULL) {
513 			data->async.rx_dma_params.buf = data->async.next_rx_buffer;
514 			data->async.rx_dma_params.buf_len = data->async.next_rx_buffer_len;
515 			data->async.next_rx_buffer = NULL;
516 			data->async.next_rx_buffer_len = 0;
517 			/* Release the next buffer as well */
518 			async_evt_rx_buf_release(dev);
519 		}
520 	}
521 	const int ret = dma_stop(config->rx_dma_config.dma_dev,
522 				 config->rx_dma_config.dma_channel);
523 
524 	if (ret != 0) {
525 		LOG_ERR("Error stopping rx DMA. Reason: %x", ret);
526 	}
527 	LOG_DBG("RX: Disabled");
528 	struct uart_event disabled_event = {
529 		.type = UART_RX_DISABLED
530 	};
531 
532 	async_user_callback(dev, &disabled_event);
533 	irq_unlock(key);
534 	return ret;
535 }
536 
prepare_rx_dma_block_config(const struct device * dev)537 static void prepare_rx_dma_block_config(const struct device *dev)
538 {
539 	struct mcux_lpuart_data *data = (struct mcux_lpuart_data *)dev->data;
540 	const struct mcux_lpuart_config *config = dev->config;
541 	LPUART_Type *lpuart = config->base;
542 	struct mcux_lpuart_rx_dma_params *rx_dma_params = &data->async.rx_dma_params;
543 
544 	assert(rx_dma_params->buf != NULL);
545 	assert(rx_dma_params->buf_len > 0);
546 
547 	struct dma_block_config *head_block_config = &rx_dma_params->active_dma_block;
548 
549 	head_block_config->dest_address = (uint32_t)rx_dma_params->buf;
550 	head_block_config->source_address = LPUART_GetDataRegisterAddress(lpuart);
551 	head_block_config->block_size = rx_dma_params->buf_len;
552 	head_block_config->dest_scatter_en = true;
553 }
554 
configure_and_start_rx_dma(const struct mcux_lpuart_config * config,struct mcux_lpuart_data * data,LPUART_Type * lpuart)555 static int configure_and_start_rx_dma(
556 	const struct mcux_lpuart_config *config, struct mcux_lpuart_data *data,
557 	LPUART_Type *lpuart)
558 {
559 	LOG_DBG("Configuring and Starting UART RX DMA");
560 	int ret = dma_config(config->rx_dma_config.dma_dev,
561 			     config->rx_dma_config.dma_channel,
562 			     (struct dma_config *)&config->rx_dma_config.dma_cfg);
563 
564 	if (ret != 0) {
565 		LOG_ERR("Failed to Configure RX DMA: err: %d", ret);
566 		return ret;
567 	}
568 	ret = dma_start(config->rx_dma_config.dma_dev, config->rx_dma_config.dma_channel);
569 	if (ret < 0) {
570 		LOG_ERR("Failed to start DMA(Rx) Ch %d(%d)",
571 			config->rx_dma_config.dma_channel,
572 			ret);
573 	}
574 	LPUART_EnableRxDMA(lpuart, true);
575 	return ret;
576 }
577 
uart_mcux_lpuart_dma_replace_rx_buffer(const struct device * dev)578 static int uart_mcux_lpuart_dma_replace_rx_buffer(const struct device *dev)
579 {
580 	struct mcux_lpuart_data *data = (struct mcux_lpuart_data *)dev->data;
581 	const struct mcux_lpuart_config *config = dev->config;
582 	LPUART_Type *lpuart = config->base;
583 
584 	LOG_DBG("Replacing RX buffer, new length: %d", data->async.next_rx_buffer_len);
585 	/* There must be a buffer to replace this one with */
586 	assert(data->async.next_rx_buffer != NULL);
587 	assert(data->async.next_rx_buffer_len != 0U);
588 	const int success = dma_reload(config->rx_dma_config.dma_dev,
589 				       config->rx_dma_config.dma_channel,
590 				       LPUART_GetDataRegisterAddress(lpuart),
591 				       (uint32_t)data->async.next_rx_buffer,
592 				       data->async.next_rx_buffer_len);
593 
594 	if (success != 0) {
595 		LOG_ERR("Error %d reloading DMA with next RX buffer", success);
596 	}
597 	return success;
598 }
599 
dma_callback(const struct device * dma_dev,void * callback_arg,uint32_t channel,int dma_status)600 static void dma_callback(const struct device *dma_dev, void *callback_arg, uint32_t channel,
601 			 int dma_status)
602 {
603 	struct device *dev = (struct device *)callback_arg;
604 	const struct mcux_lpuart_config *config = dev->config;
605 	LPUART_Type *lpuart = config->base;
606 	struct mcux_lpuart_data *data = (struct mcux_lpuart_data *)dev->data;
607 
608 	LOG_DBG("DMA call back on channel %d", channel);
609 	struct dma_status status;
610 	const int get_status_result = dma_get_status(dma_dev, channel, &status);
611 
612 	if (get_status_result < 0) {
613 		LOG_ERR("error on status get: %d", get_status_result);
614 	} else {
615 		LOG_DBG("DMA Status: b: %d dir: %d len_remain: %d", status.busy, status.dir,
616 			status.pending_length);
617 	}
618 
619 	if (dma_status < 0) {
620 		LOG_ERR("Got error : %d", dma_status);
621 	}
622 
623 
624 	if (channel == config->tx_dma_config.dma_channel) {
625 		LOG_DBG("TX Channel");
626 		LPUART_EnableTxDMA(lpuart, false);
627 		async_evt_tx_done(dev);
628 	} else if (channel == config->rx_dma_config.dma_channel) {
629 		LOG_DBG("RX Channel");
630 		struct mcux_lpuart_rx_dma_params *rx_dma_params = &data->async.rx_dma_params;
631 
632 		/* The RX Event indicates DMA transfer is complete and full buffer is available. */
633 		rx_dma_params->counter = rx_dma_params->buf_len;
634 
635 		LOG_DBG("Current Buf (%x) full, swapping to new buf: %x",
636 			(uint32_t)rx_dma_params->buf,
637 			(uint32_t)data->async.next_rx_buffer);
638 		async_evt_rx_rdy(dev);
639 		async_evt_rx_buf_release(dev);
640 
641 		rx_dma_params->buf = data->async.next_rx_buffer;
642 		rx_dma_params->buf_len = data->async.next_rx_buffer_len;
643 		data->async.next_rx_buffer = NULL;
644 		data->async.next_rx_buffer_len = 0U;
645 
646 		/* A new buffer was available (and already loaded into the DMA engine) */
647 		if (rx_dma_params->buf != NULL &&
648 		    rx_dma_params->buf_len > 0) {
649 			/* Request the next buffer */
650 			async_evt_rx_buf_request(dev);
651 		} else {
652 			/* Buffer full without valid next buffer, disable RX DMA */
653 			LOG_INF("Disabled RX DMA, no valid next buffer ");
654 			mcux_lpuart_rx_disable(dev);
655 		}
656 	} else {
657 		LOG_ERR("Got unexpected DMA Channel: %d", channel);
658 	}
659 }
660 
mcux_lpuart_callback_set(const struct device * dev,uart_callback_t callback,void * user_data)661 static int mcux_lpuart_callback_set(const struct device *dev, uart_callback_t callback,
662 				    void *user_data)
663 {
664 	struct mcux_lpuart_data *data = dev->data;
665 
666 	data->async.user_callback = callback;
667 	data->async.user_data = user_data;
668 
669 #if defined(CONFIG_UART_EXCLUSIVE_API_CALLBACKS)
670 	data->callback = NULL;
671 	data->cb_data = NULL;
672 #endif
673 
674 	return 0;
675 }
676 
mcux_lpuart_tx(const struct device * dev,const uint8_t * buf,size_t len,int32_t timeout_us)677 static int mcux_lpuart_tx(const struct device *dev, const uint8_t *buf, size_t len,
678 			  int32_t timeout_us)
679 {
680 	struct mcux_lpuart_data *data = dev->data;
681 	const struct mcux_lpuart_config *config = dev->config;
682 	LPUART_Type *lpuart = config->base;
683 
684 	unsigned int key = irq_lock();
685 
686 	/* Check for an ongiong transfer and abort if it is pending */
687 	struct dma_status status;
688 	const int get_status_result = dma_get_status(config->tx_dma_config.dma_dev,
689 						     config->tx_dma_config.dma_channel,
690 						     &status);
691 
692 	if (get_status_result < 0 || status.busy) {
693 		irq_unlock(key);
694 		LOG_ERR("Unable to submit UART DMA Transfer.");
695 		return get_status_result < 0 ? get_status_result : -EBUSY;
696 	}
697 
698 	int ret;
699 
700 	LPUART_EnableTxDMA(lpuart, false);
701 
702 	data->async.tx_dma_params.buf = buf;
703 	data->async.tx_dma_params.buf_len = len;
704 	data->async.tx_dma_params.active_dma_block.source_address = (uint32_t)buf;
705 	data->async.tx_dma_params.active_dma_block.dest_address =
706 		LPUART_GetDataRegisterAddress(lpuart);
707 	data->async.tx_dma_params.active_dma_block.block_size = len;
708 	data->async.tx_dma_params.active_dma_block.next_block = NULL;
709 
710 	ret = dma_config(config->tx_dma_config.dma_dev,
711 			 config->tx_dma_config.dma_channel,
712 			 (struct dma_config *)&config->tx_dma_config.dma_cfg);
713 
714 	if (ret == 0) {
715 		LOG_DBG("Starting UART DMA TX Ch %u", config->tx_dma_config.dma_channel);
716 
717 		ret = dma_start(config->tx_dma_config.dma_dev,
718 				config->tx_dma_config.dma_channel);
719 		LPUART_EnableTxDMA(lpuart, true);
720 		if (ret != 0) {
721 			LOG_ERR("Failed to start DMA(Tx) Ch %d",
722 				config->tx_dma_config.dma_channel);
723 		}
724 		async_timer_start(&data->async.tx_dma_params.timeout_work, timeout_us);
725 	} else {
726 		LOG_ERR("Error configuring UART DMA: %x", ret);
727 	}
728 	irq_unlock(key);
729 	return ret;
730 }
731 
mcux_lpuart_tx_abort(const struct device * dev)732 static int mcux_lpuart_tx_abort(const struct device *dev)
733 {
734 	struct mcux_lpuart_data *data = dev->data;
735 	const struct mcux_lpuart_config *config = dev->config;
736 	LPUART_Type *lpuart = config->base;
737 
738 	LPUART_EnableTxDMA(lpuart, false);
739 	(void)k_work_cancel_delayable(&data->async.tx_dma_params.timeout_work);
740 	struct dma_status status;
741 	const int get_status_result = dma_get_status(config->tx_dma_config.dma_dev,
742 						     config->tx_dma_config.dma_channel,
743 						     &status);
744 
745 	if (get_status_result < 0) {
746 		LOG_ERR("Error querying TX DMA Status during abort.");
747 	}
748 
749 	const size_t bytes_transmitted = (get_status_result == 0) ?
750 			 data->async.tx_dma_params.buf_len - status.pending_length : 0;
751 
752 	const int ret = dma_stop(config->tx_dma_config.dma_dev, config->tx_dma_config.dma_channel);
753 
754 	if (ret == 0) {
755 		struct uart_event tx_aborted_event = {
756 			.type = UART_TX_ABORTED,
757 			.data.tx.buf = data->async.tx_dma_params.buf,
758 			.data.tx.len = bytes_transmitted
759 		};
760 		async_user_callback(dev, &tx_aborted_event);
761 	}
762 	return ret;
763 }
764 
mcux_lpuart_rx_enable(const struct device * dev,uint8_t * buf,const size_t len,const int32_t timeout_us)765 static int mcux_lpuart_rx_enable(const struct device *dev, uint8_t *buf, const size_t len,
766 				 const int32_t timeout_us)
767 {
768 	LOG_DBG("Enabling UART RX DMA");
769 	struct mcux_lpuart_data *data = dev->data;
770 	const struct mcux_lpuart_config *config = dev->config;
771 	LPUART_Type *lpuart = config->base;
772 
773 	struct mcux_lpuart_rx_dma_params *rx_dma_params = &data->async.rx_dma_params;
774 
775 	unsigned int key = irq_lock();
776 	struct dma_status status;
777 	const int get_status_result = dma_get_status(config->rx_dma_config.dma_dev,
778 						     config->rx_dma_config.dma_channel,
779 						     &status);
780 
781 	if (get_status_result < 0 || status.busy) {
782 		LOG_ERR("Unable to start receive on UART.");
783 		irq_unlock(key);
784 		return get_status_result < 0 ? get_status_result : -EBUSY;
785 	}
786 
787 	rx_dma_params->timeout_us = timeout_us;
788 	rx_dma_params->buf = buf;
789 	rx_dma_params->buf_len = len;
790 
791 	LPUART_EnableInterrupts(config->base, kLPUART_IdleLineInterruptEnable);
792 	prepare_rx_dma_block_config(dev);
793 	const int ret = configure_and_start_rx_dma(config, data, lpuart);
794 
795 	/* Request the next buffer for when this buffer is full for continuous reception */
796 	async_evt_rx_buf_request(dev);
797 
798 	/* Clear these status flags as they can prevent the UART device from receiving data */
799 	LPUART_ClearStatusFlags(config->base, kLPUART_RxOverrunFlag |
800 					      kLPUART_ParityErrorFlag |
801 					      kLPUART_FramingErrorFlag |
802 						  kLPUART_NoiseErrorFlag);
803 	LPUART_EnableRx(lpuart, true);
804 	irq_unlock(key);
805 	return ret;
806 }
807 
mcux_lpuart_rx_buf_rsp(const struct device * dev,uint8_t * buf,size_t len)808 static int mcux_lpuart_rx_buf_rsp(const struct device *dev, uint8_t *buf, size_t len)
809 {
810 	struct mcux_lpuart_data *data = dev->data;
811 
812 	assert(data->async.next_rx_buffer == NULL);
813 	assert(data->async.next_rx_buffer_len == 0);
814 	data->async.next_rx_buffer = buf;
815 	data->async.next_rx_buffer_len = len;
816 	uart_mcux_lpuart_dma_replace_rx_buffer(dev);
817 
818 	return 0;
819 }
820 
mcux_lpuart_async_rx_timeout(struct k_work * work)821 static void mcux_lpuart_async_rx_timeout(struct k_work *work)
822 {
823 	struct k_work_delayable *dwork = k_work_delayable_from_work(work);
824 	struct mcux_lpuart_rx_dma_params *rx_params = CONTAINER_OF(dwork,
825 								   struct mcux_lpuart_rx_dma_params,
826 								   timeout_work);
827 	struct mcux_lpuart_async_data *async_data = CONTAINER_OF(rx_params,
828 								 struct mcux_lpuart_async_data,
829 								 rx_dma_params);
830 	const struct device *dev = async_data->uart_dev;
831 
832 	LOG_DBG("RX timeout");
833 	mcux_lpuart_async_rx_flush(dev);
834 }
835 
mcux_lpuart_async_tx_timeout(struct k_work * work)836 static void mcux_lpuart_async_tx_timeout(struct k_work *work)
837 {
838 	struct k_work_delayable *dwork = k_work_delayable_from_work(work);
839 	struct mcux_lpuart_tx_dma_params *tx_params = CONTAINER_OF(dwork,
840 								   struct mcux_lpuart_tx_dma_params,
841 								   timeout_work);
842 	struct mcux_lpuart_async_data *async_data = CONTAINER_OF(tx_params,
843 								 struct mcux_lpuart_async_data,
844 								 tx_dma_params);
845 	const struct device *dev = async_data->uart_dev;
846 
847 	LOG_DBG("TX timeout");
848 	(void)mcux_lpuart_tx_abort(dev);
849 }
850 
851 #endif /* CONFIG_UART_ASYNC_API */
852 
853 #if CONFIG_UART_MCUX_LPUART_ISR_SUPPORT
mcux_lpuart_isr(const struct device * dev)854 static void mcux_lpuart_isr(const struct device *dev)
855 {
856 	struct mcux_lpuart_data *data = dev->data;
857 	const struct mcux_lpuart_config *config = dev->config;
858 	const uint32_t status = LPUART_GetStatusFlags(config->base);
859 
860 #if CONFIG_PM
861 	if (status & kLPUART_TransmissionCompleteFlag) {
862 
863 		if (data->tx_poll_stream_on) {
864 			/* Poll transmission complete. Allow system to sleep */
865 			LPUART_DisableInterrupts(config->base,
866 				kLPUART_TransmissionCompleteInterruptEnable);
867 			data->tx_poll_stream_on = false;
868 			mcux_lpuart_pm_policy_state_lock_put(dev);
869 		}
870 	}
871 #endif /* CONFIG_PM */
872 
873 #if CONFIG_UART_INTERRUPT_DRIVEN
874 	if (data->callback) {
875 		data->callback(dev, data->cb_data);
876 	}
877 
878 	if (status & kLPUART_RxOverrunFlag) {
879 		LPUART_ClearStatusFlags(config->base, kLPUART_RxOverrunFlag);
880 	}
881 #endif
882 
883 #if CONFIG_UART_ASYNC_API
884 	if (status & kLPUART_IdleLineFlag) {
885 		async_timer_start(&data->async.rx_dma_params.timeout_work,
886 				  data->async.rx_dma_params.timeout_us);
887 		LPUART_ClearStatusFlags(config->base, kLPUART_IdleLineFlag);
888 	}
889 #endif /* CONFIG_UART_ASYNC_API */
890 }
891 #endif /* CONFIG_UART_MCUX_LPUART_ISR_SUPPORT */
892 
mcux_lpuart_configure_init(const struct device * dev,const struct uart_config * cfg)893 static int mcux_lpuart_configure_init(const struct device *dev, const struct uart_config *cfg)
894 {
895 	const struct mcux_lpuart_config *config = dev->config;
896 	struct mcux_lpuart_data *data = dev->data;
897 	uint32_t clock_freq;
898 
899 	if (!device_is_ready(config->clock_dev)) {
900 		return -ENODEV;
901 	}
902 
903 	if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
904 				   &clock_freq)) {
905 		return -EINVAL;
906 	}
907 
908 	lpuart_config_t uart_config;
909 	LPUART_GetDefaultConfig(&uart_config);
910 
911 	/* Translate UART API enum to LPUART enum from HAL */
912 	switch (cfg->parity) {
913 	case UART_CFG_PARITY_NONE:
914 		uart_config.parityMode = kLPUART_ParityDisabled;
915 		break;
916 	case UART_CFG_PARITY_ODD:
917 		uart_config.parityMode = kLPUART_ParityOdd;
918 		break;
919 	case UART_CFG_PARITY_EVEN:
920 		uart_config.parityMode = kLPUART_ParityEven;
921 		break;
922 	default:
923 		return -ENOTSUP;
924 	}
925 
926 	switch (cfg->data_bits) {
927 #if defined(FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT) && \
928 	FSL_FEATURE_LPUART_HAS_7BIT_DATA_SUPPORT
929 	case UART_CFG_DATA_BITS_7:
930 		uart_config.dataBitsCount  = kLPUART_SevenDataBits;
931 		break;
932 #endif
933 	case UART_CFG_DATA_BITS_8:
934 		uart_config.dataBitsCount  = kLPUART_EightDataBits;
935 		break;
936 	default:
937 		return -ENOTSUP;
938 	}
939 
940 #if defined(FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT) && \
941 	FSL_FEATURE_LPUART_HAS_STOP_BIT_CONFIG_SUPPORT
942 	switch (cfg->stop_bits) {
943 	case UART_CFG_STOP_BITS_1:
944 		uart_config.stopBitCount = kLPUART_OneStopBit;
945 		break;
946 	case UART_CFG_STOP_BITS_2:
947 		uart_config.stopBitCount = kLPUART_TwoStopBit;
948 		break;
949 	default:
950 		return -ENOTSUP;
951 	}
952 #endif
953 
954 #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT) && \
955 	FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
956 	switch (cfg->flow_ctrl) {
957 	case UART_CFG_FLOW_CTRL_NONE:
958 	case UART_CFG_FLOW_CTRL_RS485:
959 		uart_config.enableTxCTS = false;
960 		uart_config.enableRxRTS = false;
961 		break;
962 	case UART_CFG_FLOW_CTRL_RTS_CTS:
963 		uart_config.enableTxCTS = true;
964 		uart_config.enableRxRTS = true;
965 		break;
966 	default:
967 		return -ENOTSUP;
968 	}
969 #endif
970 
971 	uart_config.baudRate_Bps = cfg->baudrate;
972 	uart_config.enableRx = true;
973 	/* Tx will be enabled manually after set tx-rts */
974 	uart_config.enableTx = false;
975 
976 
977 #ifdef CONFIG_UART_ASYNC_API
978 	uart_config.rxIdleType = kLPUART_IdleTypeStopBit;
979 	uart_config.rxIdleConfig = kLPUART_IdleCharacter1;
980 	data->async.next_rx_buffer = NULL;
981 	data->async.next_rx_buffer_len = 0;
982 	data->async.uart_dev = dev;
983 	k_work_init_delayable(&data->async.rx_dma_params.timeout_work,
984 			      mcux_lpuart_async_rx_timeout);
985 	k_work_init_delayable(&data->async.tx_dma_params.timeout_work,
986 			      mcux_lpuart_async_tx_timeout);
987 
988 	/* Disable the UART Receiver until the async API provides a buffer to
989 	 * to receive into with rx_enable
990 	 */
991 	uart_config.enableRx = false;
992 
993 #endif /* CONFIG_UART_ASYNC_API */
994 
995 	LPUART_Init(config->base, &uart_config, clock_freq);
996 
997 	if (cfg->flow_ctrl == UART_CFG_FLOW_CTRL_RS485) {
998 		/* Set the LPUART into RS485 mode (tx driver enable using RTS) */
999 		config->base->MODIR |= LPUART_MODIR_TXRTSE(true);
1000 		if (!config->rs485_de_active_low) {
1001 			config->base->MODIR |= LPUART_MODIR_TXRTSPOL(1);
1002 		}
1003 	}
1004 	/* Now can enable tx */
1005 	config->base->CTRL |= LPUART_CTRL_TE(true);
1006 
1007 
1008 	if (config->loopback_en) {
1009 		/* Set the LPUART into loopback mode */
1010 		config->base->CTRL |= LPUART_CTRL_LOOPS_MASK;
1011 		config->base->CTRL &= ~LPUART_CTRL_RSRC_MASK;
1012 	}
1013 
1014 	/* update internal uart_config */
1015 	data->uart_config = *cfg;
1016 
1017 	return 0;
1018 }
1019 
1020 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
mcux_lpuart_config_get(const struct device * dev,struct uart_config * cfg)1021 static int mcux_lpuart_config_get(const struct device *dev, struct uart_config *cfg)
1022 {
1023 	struct mcux_lpuart_data *data = dev->data;
1024 	*cfg = data->uart_config;
1025 	return 0;
1026 }
1027 
mcux_lpuart_configure(const struct device * dev,const struct uart_config * cfg)1028 static int mcux_lpuart_configure(const struct device *dev,
1029 				 const struct uart_config *cfg)
1030 {
1031 	const struct mcux_lpuart_config *config = dev->config;
1032 
1033 	/* disable LPUART */
1034 	LPUART_Deinit(config->base);
1035 
1036 	int ret = mcux_lpuart_configure_init(dev, cfg);
1037 	if (ret) {
1038 		return ret;
1039 	}
1040 
1041 	/* wait for hardware init */
1042 	k_sleep(K_MSEC(1));
1043 
1044 	return 0;
1045 }
1046 #endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
1047 
mcux_lpuart_init(const struct device * dev)1048 static int mcux_lpuart_init(const struct device *dev)
1049 {
1050 	const struct mcux_lpuart_config *config = dev->config;
1051 	struct mcux_lpuart_data *data = dev->data;
1052 	struct uart_config *uart_api_config = &data->uart_config;
1053 	int err;
1054 
1055 	uart_api_config->baudrate = config->baud_rate;
1056 	uart_api_config->parity = config->parity;
1057 	uart_api_config->stop_bits = UART_CFG_STOP_BITS_1;
1058 	uart_api_config->data_bits = UART_CFG_DATA_BITS_8;
1059 	uart_api_config->flow_ctrl = config->flow_ctrl;
1060 
1061 	/* set initial configuration */
1062 	mcux_lpuart_configure_init(dev, uart_api_config);
1063 	err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
1064 	if (err < 0) {
1065 		return err;
1066 	}
1067 
1068 #ifdef CONFIG_UART_MCUX_LPUART_ISR_SUPPORT
1069 	config->irq_config_func(dev);
1070 #endif
1071 
1072 #ifdef CONFIG_PM
1073 	data->pm_state_lock_on = false;
1074 	data->tx_poll_stream_on = false;
1075 	data->tx_int_stream_on = false;
1076 #endif
1077 
1078 	return 0;
1079 }
1080 
1081 static const struct uart_driver_api mcux_lpuart_driver_api = {
1082 	.poll_in = mcux_lpuart_poll_in,
1083 	.poll_out = mcux_lpuart_poll_out,
1084 	.err_check = mcux_lpuart_err_check,
1085 #ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
1086 	.configure = mcux_lpuart_configure,
1087 	.config_get = mcux_lpuart_config_get,
1088 #endif
1089 #ifdef CONFIG_UART_INTERRUPT_DRIVEN
1090 	.fifo_fill = mcux_lpuart_fifo_fill,
1091 	.fifo_read = mcux_lpuart_fifo_read,
1092 	.irq_tx_enable = mcux_lpuart_irq_tx_enable,
1093 	.irq_tx_disable = mcux_lpuart_irq_tx_disable,
1094 	.irq_tx_complete = mcux_lpuart_irq_tx_complete,
1095 	.irq_tx_ready = mcux_lpuart_irq_tx_ready,
1096 	.irq_rx_enable = mcux_lpuart_irq_rx_enable,
1097 	.irq_rx_disable = mcux_lpuart_irq_rx_disable,
1098 	.irq_rx_ready = mcux_lpuart_irq_rx_full,
1099 	.irq_err_enable = mcux_lpuart_irq_err_enable,
1100 	.irq_err_disable = mcux_lpuart_irq_err_disable,
1101 	.irq_is_pending = mcux_lpuart_irq_is_pending,
1102 	.irq_update = mcux_lpuart_irq_update,
1103 	.irq_callback_set = mcux_lpuart_irq_callback_set,
1104 #endif
1105 #ifdef CONFIG_UART_ASYNC_API
1106 	.callback_set = mcux_lpuart_callback_set,
1107 	.tx = mcux_lpuart_tx,
1108 	.tx_abort = mcux_lpuart_tx_abort,
1109 	.rx_enable = mcux_lpuart_rx_enable,
1110 	.rx_buf_rsp = mcux_lpuart_rx_buf_rsp,
1111 	.rx_disable = mcux_lpuart_rx_disable,
1112 #endif /* CONFIG_UART_ASYNC_API */
1113 };
1114 
1115 
1116 #ifdef CONFIG_UART_MCUX_LPUART_ISR_SUPPORT
1117 #define MCUX_LPUART_IRQ_INSTALL(n, i)					\
1118 	do {								\
1119 		IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, i, irq),		\
1120 			    DT_INST_IRQ_BY_IDX(n, i, priority),		\
1121 			    mcux_lpuart_isr, DEVICE_DT_INST_GET(n), 0);	\
1122 									\
1123 		irq_enable(DT_INST_IRQ_BY_IDX(n, i, irq));		\
1124 	} while (false)
1125 #define MCUX_LPUART_IRQ_INIT(n) .irq_config_func = mcux_lpuart_config_func_##n,
1126 #define MCUX_LPUART_IRQ_DEFINE(n)						\
1127 	static void mcux_lpuart_config_func_##n(const struct device *dev)	\
1128 	{									\
1129 		MCUX_LPUART_IRQ_INSTALL(n, 0);				\
1130 									\
1131 		IF_ENABLED(DT_INST_IRQ_HAS_IDX(n, 1),			\
1132 			   (MCUX_LPUART_IRQ_INSTALL(n, 1);))		\
1133 	}
1134 #else
1135 #define MCUX_LPUART_IRQ_INIT(n)
1136 #define MCUX_LPUART_IRQ_DEFINE(n)
1137 #endif /* CONFIG_UART_MCUX_LPUART_ISR_SUPPORT */
1138 
1139 #ifdef CONFIG_UART_ASYNC_API
1140 #define TX_DMA_CONFIG(id)								       \
1141 	.tx_dma_config = {								       \
1142 		.dma_dev =								       \
1143 			DEVICE_DT_GET(DT_INST_DMAS_CTLR_BY_NAME(id, tx)),		       \
1144 		.dma_channel =								       \
1145 			DT_INST_DMAS_CELL_BY_NAME(id, tx, mux),				       \
1146 		.dma_cfg = {								       \
1147 			.source_burst_length = 1,					       \
1148 			.dest_burst_length = 1,						       \
1149 			.source_data_size = 1,						       \
1150 			.dest_data_size = 1,						       \
1151 			.complete_callback_en = 1,					       \
1152 			.error_callback_en = 1,						       \
1153 			.block_count = 1,						       \
1154 			.head_block =							       \
1155 				&mcux_lpuart_##id##_data.async.tx_dma_params.active_dma_block, \
1156 			.channel_direction = MEMORY_TO_PERIPHERAL,			       \
1157 			.dma_slot = DT_INST_DMAS_CELL_BY_NAME(				       \
1158 				id, tx, source),					       \
1159 			.dma_callback = dma_callback,					       \
1160 			.user_data = (void *)DEVICE_DT_INST_GET(id)			       \
1161 		},									       \
1162 	},
1163 #define RX_DMA_CONFIG(id)								       \
1164 	.rx_dma_config = {								       \
1165 		.dma_dev =								       \
1166 			DEVICE_DT_GET(DT_INST_DMAS_CTLR_BY_NAME(id, rx)),		       \
1167 		.dma_channel =								       \
1168 			DT_INST_DMAS_CELL_BY_NAME(id, rx, mux),				       \
1169 		.dma_cfg = {								       \
1170 			.source_burst_length = 1,					       \
1171 			.dest_burst_length = 1,						       \
1172 			.source_data_size = 1,						       \
1173 			.dest_data_size = 1,						       \
1174 			.complete_callback_en = 1,					       \
1175 			.error_callback_en = 1,						       \
1176 			.block_count = 1,						       \
1177 			.head_block =							       \
1178 				&mcux_lpuart_##id##_data.async.rx_dma_params.active_dma_block, \
1179 			.channel_direction = PERIPHERAL_TO_MEMORY,			       \
1180 			.dma_slot = DT_INST_DMAS_CELL_BY_NAME(				       \
1181 				id, rx, source),					       \
1182 			.dma_callback = dma_callback,					       \
1183 			.user_data = (void *)DEVICE_DT_INST_GET(id)			       \
1184 		},									       \
1185 	},
1186 #else
1187 #define RX_DMA_CONFIG(n)
1188 #define TX_DMA_CONFIG(n)
1189 #endif /* CONFIG_UART_ASYNC_API */
1190 
1191 #define FLOW_CONTROL(n) \
1192 	DT_INST_PROP(n, hw_flow_control)   \
1193 		? UART_CFG_FLOW_CTRL_RTS_CTS     \
1194 		: DT_INST_PROP(n, nxp_rs485_mode)\
1195 				? UART_CFG_FLOW_CTRL_RS485   \
1196 				: UART_CFG_FLOW_CTRL_NONE
1197 
1198 #define LPUART_MCUX_DECLARE_CFG(n)                                      \
1199 static const struct mcux_lpuart_config mcux_lpuart_##n##_config = {     \
1200 	.base = (LPUART_Type *) DT_INST_REG_ADDR(n),                          \
1201 	.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)),                   \
1202 	.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),	\
1203 	.baud_rate = DT_INST_PROP(n, current_speed),                          \
1204 	.flow_ctrl = FLOW_CONTROL(n),                                         \
1205 	.parity = DT_INST_ENUM_IDX_OR(n, parity, UART_CFG_PARITY_NONE),       \
1206 	.rs485_de_active_low = DT_INST_PROP(n, nxp_rs485_de_active_low),      \
1207 	.loopback_en = DT_INST_PROP(n, nxp_loopback),                         \
1208 	.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),                          \
1209 	MCUX_LPUART_IRQ_INIT(n) \
1210 	RX_DMA_CONFIG(n)        \
1211 	TX_DMA_CONFIG(n)        \
1212 };
1213 
1214 #define LPUART_MCUX_INIT(n)						\
1215 									\
1216 	static struct mcux_lpuart_data mcux_lpuart_##n##_data;		\
1217 									\
1218 	PINCTRL_DT_INST_DEFINE(n);					\
1219 	MCUX_LPUART_IRQ_DEFINE(n)					\
1220 									\
1221 	LPUART_MCUX_DECLARE_CFG(n)					\
1222 									\
1223 	DEVICE_DT_INST_DEFINE(n,					\
1224 			    &mcux_lpuart_init,				\
1225 			    NULL,					\
1226 			    &mcux_lpuart_##n##_data,			\
1227 			    &mcux_lpuart_##n##_config,			\
1228 			    PRE_KERNEL_1,				\
1229 			    CONFIG_SERIAL_INIT_PRIORITY,		\
1230 			    &mcux_lpuart_driver_api);			\
1231 
1232 DT_INST_FOREACH_STATUS_OKAY(LPUART_MCUX_INIT)
1233