Linux preempt-rt

Check our new training course

Real-Time Linux with PREEMPT_RT

Check our new training course
with Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * Copyright (c) 2018 Diego Sueiro <diego.sueiro@gmail.com>
 *
 * SPDX-License-Identifier: Apache-2.0
 */


/**
 * @brief Driver for UART on NXP IMX family processor.
 *
 * For full serial function, use the USART controller.
 *
 */

#include <kernel.h>
#include <arch/cpu.h>
#include <sys/__assert.h>
#include <soc.h>
#include <init.h>
#include <drivers/uart.h>
#include <uart_imx.h>

#define DEV_CFG(dev) \
	((const struct imx_uart_config *const)(dev)->config->config_info)
#define UART_STRUCT(dev) \
	((UART_Type *)(DEV_CFG(dev))->base)

struct imx_uart_config {
	UART_Type *base;
	u32_t baud_rate;
	u8_t modem_mode;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	void (*irq_config_func)(struct device *dev);
#endif
};

struct imx_uart_data {
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	uart_irq_callback_user_data_t callback;
	void *cb_data;
#endif
};

/**
 * @brief Initialize UART channel
 *
 * This routine is called to reset the chip in a quiescent state.
 * It is assumed that this function is called only once per UART.
 *
 * @param dev UART device struct
 *
 * @return 0
 */
static int uart_imx_init(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);
	const struct imx_uart_config *config = dev->config->config_info;
	unsigned int old_level;

	/* disable interrupts */
	old_level = irq_lock();

	/* Setup UART init structure */
	uart_init_config_t initConfig = {
		.baudRate	= config->baud_rate,
		.wordLength = uartWordLength8Bits,
		.stopBitNum = uartStopBitNumOne,
		.parity		= uartParityDisable,
		.direction	= uartDirectionTxRx
	};

	/* Get current module clock frequency */
	initConfig.clockRate  = get_uart_clock_freq(uart);

	UART_Init(uart, &initConfig);

	/* Set UART build-in hardware FIFO Watermark. */
	UART_SetTxFifoWatermark(uart, 2);
	UART_SetRxFifoWatermark(uart, 1);

	/* restore interrupt state */
	irq_unlock(old_level);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	config->irq_config_func(dev);
#endif

	/* Set UART modem mode */
	UART_SetModemMode(uart, config->modem_mode);

	/* Finally, enable the UART module */
	UART_Enable(uart);

	return 0;
}

static void uart_imx_poll_out(struct device *dev, unsigned char c)
{
	UART_Type *uart = UART_STRUCT(dev);

	while (!UART_GetStatusFlag(uart, uartStatusTxReady)) {
	}
	UART_Putchar(uart, c);
}

static int uart_imx_poll_in(struct device *dev, unsigned char *c)
{
	UART_Type *uart = UART_STRUCT(dev);

	while (!UART_GetStatusFlag(uart, uartStatusRxDataReady)) {
	}
	*c = UART_Getchar(uart);

	if (UART_GetStatusFlag(uart, uartStatusRxOverrun)) {
		UART_ClearStatusFlag(uart, uartStatusRxOverrun);
	}

	return 0;
}

#ifdef CONFIG_UART_INTERRUPT_DRIVEN

static int uart_imx_fifo_fill(struct device *dev, const u8_t *tx_data,
				  int size)
{
	UART_Type *uart = UART_STRUCT(dev);
	unsigned int num_tx = 0U;

	while (((size - num_tx) > 0) &&
		   UART_GetStatusFlag(uart, uartStatusTxReady)) {
		/* Send a character */
		UART_Putchar(uart, tx_data[num_tx]);
		num_tx++;
	}

	return (int)num_tx;
}

static int uart_imx_fifo_read(struct device *dev, u8_t *rx_data,
				  const int size)
{
	UART_Type *uart = UART_STRUCT(dev);
	unsigned int num_rx = 0U;

	while (((size - num_rx) > 0) &&
		   UART_GetStatusFlag(uart, uartStatusRxReady)) {
		/* Receive a character */
		rx_data[num_rx++] = UART_Getchar(uart);
	}

	if (UART_GetStatusFlag(uart, uartStatusRxOverrun)) {
		UART_ClearStatusFlag(uart, uartStatusRxOverrun);
	}

	return num_rx;
}

static void uart_imx_irq_tx_enable(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	UART_SetIntCmd(uart, uartIntTxReady, true);
}

static void uart_imx_irq_tx_disable(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	UART_SetIntCmd(uart, uartIntTxReady, false);
}

static int uart_imx_irq_tx_ready(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	return UART_GetStatusFlag(uart, uartStatusTxReady);
}

static void uart_imx_irq_rx_enable(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	UART_SetIntCmd(uart, uartIntRxReady, true);
}

static void uart_imx_irq_rx_disable(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	UART_SetIntCmd(uart, uartIntRxReady, false);
}

static int uart_imx_irq_rx_ready(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	return UART_GetStatusFlag(uart, uartStatusRxReady);
}

static void uart_imx_irq_err_enable(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	UART_SetIntCmd(uart, uartIntParityError, true);
	UART_SetIntCmd(uart, uartIntFrameError, true);
}

static void uart_imx_irq_err_disable(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	UART_SetIntCmd(uart, uartIntParityError, false);
	UART_SetIntCmd(uart, uartIntFrameError, false);
}

static int uart_imx_irq_is_pending(struct device *dev)
{
	UART_Type *uart = UART_STRUCT(dev);

	return UART_GetStatusFlag(uart, uartStatusRxReady) ||
		UART_GetStatusFlag(uart, uartStatusTxReady);
}

static int uart_imx_irq_update(struct device *dev)
{
	return 1;
}

static void uart_imx_irq_callback_set(struct device *dev,
		uart_irq_callback_user_data_t cb,
		void *cb_data)
{
	struct imx_uart_data *data = dev->driver_data;

	data->callback = cb;
	data->cb_data = cb_data;
}

/**
 * @brief Interrupt service routine.
 *
 * This simply calls the callback function, if one exists.
 *
 * Note: imx UART Tx interrupts when ready to send; Rx interrupts when char
 * received.
 *
 * @param arg Argument to ISR.
 *
 * @return N/A
 */
void uart_imx_isr(void *arg)
{
	struct device *dev = arg;
	struct imx_uart_data *data = dev->driver_data;

	if (data->callback) {
		data->callback(data->cb_data);
	}
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */

static const struct uart_driver_api uart_imx_driver_api = {
	.poll_in  = uart_imx_poll_in,
	.poll_out = uart_imx_poll_out,

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.fifo_fill		  = uart_imx_fifo_fill,
	.fifo_read		  = uart_imx_fifo_read,
	.irq_tx_enable	  = uart_imx_irq_tx_enable,
	.irq_tx_disable   = uart_imx_irq_tx_disable,
	.irq_tx_ready	  = uart_imx_irq_tx_ready,
	.irq_rx_enable	  = uart_imx_irq_rx_enable,
	.irq_rx_disable   = uart_imx_irq_rx_disable,
	.irq_rx_ready	  = uart_imx_irq_rx_ready,
	.irq_err_enable   = uart_imx_irq_err_enable,
	.irq_err_disable  = uart_imx_irq_err_disable,
	.irq_is_pending   = uart_imx_irq_is_pending,
	.irq_update		  = uart_imx_irq_update,
	.irq_callback_set = uart_imx_irq_callback_set,
#endif	/* CONFIG_UART_INTERRUPT_DRIVEN */

};


#ifdef CONFIG_UART_IMX_UART_1

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_1(struct device *port);
#endif

static const struct imx_uart_config imx_uart_1_config = {
	.base = (UART_Type *) DT_UART_IMX_UART_1_BASE_ADDRESS,
	.baud_rate = DT_UART_IMX_UART_1_BAUD_RATE,
	.modem_mode = DT_UART_IMX_UART_1_MODEM_MODE,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_config_func = irq_config_func_1,
#endif
};

static struct imx_uart_data imx_uart_1_data;

DEVICE_AND_API_INIT(uart_1, DT_UART_IMX_UART_1_NAME, &uart_imx_init,
		&imx_uart_1_data, &imx_uart_1_config,
		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		&uart_imx_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_1(struct device *dev)
{
	IRQ_CONNECT(DT_UART_IMX_UART_1_IRQ_NUM,
			DT_UART_IMX_UART_1_IRQ_PRI,
			uart_imx_isr, DEVICE_GET(uart_1),
			0);
	irq_enable(DT_UART_IMX_UART_1_IRQ_NUM);
}
#endif

#endif /* CONFIG_UART_IMX_UART_1 */


#ifdef CONFIG_UART_IMX_UART_2

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_2(struct device *port);
#endif

static const struct imx_uart_config imx_uart_2_config = {
	.base = (UART_Type *) DT_UART_IMX_UART_2_BASE_ADDRESS,
	.baud_rate = DT_UART_IMX_UART_2_BAUD_RATE,
	.modem_mode = DT_UART_IMX_UART_2_MODEM_MODE,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_config_func = irq_config_func_2,
#endif
};

static struct imx_uart_data imx_uart_2_data;

DEVICE_AND_API_INIT(uart_2, DT_UART_IMX_UART_2_NAME, &uart_imx_init,
		&imx_uart_2_data, &imx_uart_2_config,
		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		&uart_imx_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_2(struct device *dev)
{
	IRQ_CONNECT(DT_UART_IMX_UART_2_IRQ_NUM,
			DT_UART_IMX_UART_2_IRQ_PRI,
			uart_imx_isr, DEVICE_GET(uart_2),
			0);
	irq_enable(DT_UART_IMX_UART_2_IRQ_NUM);
}
#endif

#endif /* CONFIG_UART_IMX_UART_2 */

#ifdef CONFIG_UART_IMX_UART_3

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_3(struct device *port);
#endif

static const struct imx_uart_config imx_uart_3_config = {
	.base = (UART_Type *) DT_UART_IMX_UART_3_BASE_ADDRESS,
	.baud_rate = DT_UART_IMX_UART_3_BAUD_RATE,
	.modem_mode = DT_UART_IMX_UART_3_MODEM_MODE,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_config_func = irq_config_func_3,
#endif
};

static struct imx_uart_data imx_uart_3_data;

DEVICE_AND_API_INIT(uart_3, DT_UART_IMX_UART_3_NAME, &uart_imx_init,
		&imx_uart_3_data, &imx_uart_3_config,
		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		&uart_imx_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_3(struct device *dev)
{
	IRQ_CONNECT(DT_UART_IMX_UART_3_IRQ_NUM,
			DT_UART_IMX_UART_3_IRQ_PRI,
			uart_imx_isr, DEVICE_GET(uart_3),
			0);
	irq_enable(DT_UART_IMX_UART_3_IRQ_NUM);
}
#endif

#endif /* CONFIG_UART_IMX_UART_3 */

#ifdef CONFIG_UART_IMX_UART_4

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_4(struct device *port);
#endif

static const struct imx_uart_config imx_uart_4_config = {
	.base = (UART_Type *) DT_UART_IMX_UART_4_BASE_ADDRESS,
	.baud_rate = DT_UART_IMX_UART_4_BAUD_RATE,
	.modem_mode = DT_UART_IMX_UART_4_MODEM_MODE,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_config_func = irq_config_func_4,
#endif
};

static struct imx_uart_data imx_uart_4_data;

DEVICE_AND_API_INIT(uart_4, DT_UART_IMX_UART_4_NAME, &uart_imx_init,
		&imx_uart_4_data, &imx_uart_4_config,
		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		&uart_imx_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_4(struct device *dev)
{
	IRQ_CONNECT(DT_UART_IMX_UART_4_IRQ_NUM,
			DT_UART_IMX_UART_4_IRQ_PRI,
			uart_imx_isr, DEVICE_GET(uart_4),
			0);
	irq_enable(DT_UART_IMX_UART_4_IRQ_NUM);
}
#endif

#endif /* CONFIG_UART_IMX_UART_4 */

#ifdef CONFIG_UART_IMX_UART_5

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_5(struct device *port);
#endif

static const struct imx_uart_config imx_uart_5_config = {
	.base = (UART_Type *) DT_UART_IMX_UART_5_BASE_ADDRESS,
	.baud_rate = DT_UART_IMX_UART_5_BAUD_RATE,
	.modem_mode = DT_UART_IMX_UART_5_MODEM_MODE,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_config_func = irq_config_func_5,
#endif
};

static struct imx_uart_data imx_uart_5_data;

DEVICE_AND_API_INIT(uart_5, DT_UART_IMX_UART_5_NAME, &uart_imx_init,
		&imx_uart_5_data, &imx_uart_5_config,
		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		&uart_imx_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_5(struct device *dev)
{
	IRQ_CONNECT(DT_UART_IMX_UART_5_IRQ_NUM,
			DT_UART_IMX_UART_5_IRQ_PRI,
			uart_imx_isr, DEVICE_GET(uart_5),
			0);
	irq_enable(DT_UART_IMX_UART_5_IRQ_NUM);
}
#endif

#endif /* CONFIG_UART_IMX_UART_5 */

#ifdef CONFIG_UART_IMX_UART_6

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_6(struct device *port);
#endif

static const struct imx_uart_config imx_uart_6_config = {
	.base = (UART_Type *) DT_UART_IMX_UART_6_BASE_ADDRESS,
	.baud_rate = DT_UART_IMX_UART_6_BAUD_RATE,
	.modem_mode = DT_UART_IMX_UART_6_MODEM_MODE,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_config_func = irq_config_func_6,
#endif
};

static struct imx_uart_data imx_uart_6_data;

DEVICE_AND_API_INIT(uart_6, DT_UART_IMX_UART_6_NAME, &uart_imx_init,
		&imx_uart_6_data, &imx_uart_6_config,
		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		&uart_imx_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_6(struct device *dev)
{
	IRQ_CONNECT(DT_UART_IMX_UART_6_IRQ_NUM,
			DT_UART_IMX_UART_6_IRQ_PRI,
			uart_imx_isr, DEVICE_GET(uart_6),
			0);
	irq_enable(DT_UART_IMX_UART_6_IRQ_NUM);
}
#endif

#endif /* CONFIG_UART_IMX_UART_6 */

#ifdef CONFIG_UART_IMX_UART_7

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_7(struct device *port);
#endif

static const struct imx_uart_config imx_uart_7_config = {
	.base = (UART_Type *) DT_UART_IMX_UART_7_BASE_ADDRESS,
	.baud_rate = DT_UART_IMX_UART_7_BAUD_RATE,
	.modem_mode = DT_UART_IMX_UART_7_MODEM_MODE,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
	.irq_config_func = irq_config_func_7,
#endif
};

static struct imx_uart_data imx_uart_7_data;

DEVICE_AND_API_INIT(uart_7, DT_UART_IMX_UART_7_NAME, &uart_imx_init,
		&imx_uart_7_data, &imx_uart_7_config,
		PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		&uart_imx_driver_api);

#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void irq_config_func_7(struct device *dev)
{
	IRQ_CONNECT(DT_UART_IMX_UART_7_IRQ_NUM,
			DT_UART_IMX_UART_7_IRQ_PRI,
			uart_imx_isr, DEVICE_GET(uart_7),
			0);
	irq_enable(DT_UART_IMX_UART_7_IRQ_NUM);
}
#endif

#endif /* CONFIG_UART_IMX_UART_7 */