Linux Audio

Check our new training course

Embedded Linux Audio

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

Bootlin logo

Elixir Cross Referencer

Loading...
  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
/*
 * Copyright (c) 2013-2015 Wind River Systems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @brief UART driver for the Freescale K20 Family of microprocessors.
 *
 * Before individual UART port can be used, uart_k20_port_init() has to be
 * called to setup the port.
 */

#include <nanokernel.h>
#include <arch/cpu.h>
#include <stdint.h>

#include <board.h>
#include <init.h>
#include <uart.h>
#include <toolchain.h>
#include <sections.h>

#include "uart_k20_priv.h"

/* convenience defines */

#define DEV_CFG(dev) \
	((struct uart_device_config * const)(dev)->config->config_info)
#define DEV_DATA(dev) \
	((struct uart_k20_dev_data_t * const)(dev)->driver_data)
#define UART_STRUCT(dev) \
	((volatile struct K20_UART *)(DEV_CFG(dev))->base)

/* Device data structure */
struct uart_k20_dev_data_t {
	uint32_t baud_rate;	/* Baud rate */
};

static struct uart_driver_api uart_k20_driver_api;

/**
 * @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 DEV_OK
 */
static int uart_k20_init(struct device *dev)
{
	int old_level; /* old interrupt lock level */
	union C1 c1;				   /* UART C1 register value */
	union C2 c2;				   /* UART C2 register value */

	volatile struct K20_UART *uart = UART_STRUCT(dev);
	struct uart_device_config * const dev_cfg = DEV_CFG(dev);
	struct uart_k20_dev_data_t * const dev_data = DEV_DATA(dev);

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

	_uart_k20_baud_rate_set(uart, dev_cfg->sys_clk_freq,
				dev_data->baud_rate);

	/* 1 start bit, 8 data bits, no parity, 1 stop bit */
	c1.value = 0;

	uart->c1 = c1;

	/* enable Rx and Tx with interrupts disabled */
	c2.value = 0;
	c2.field.rx_enable = 1;
	c2.field.tx_enable = 1;

	uart->c2 = c2;

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

	dev->driver_api = &uart_k20_driver_api;

	return DEV_OK;
}

/**
 * @brief Poll the device for input.
 *
 * @param dev UART device struct
 * @param c Pointer to character
 *
 * @return 0 if a character arrived, -1 if the input buffer if empty.
 */
static int uart_k20_poll_in(struct device *dev, unsigned char *c)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	if (uart->s1.field.rx_data_full == 0)
		return (-1);

	/* got a character */
	*c = uart->d;

	return 0;
}

/**
 * @brief Output a character in polled mode.
 *
 * Checks if the transmitter is empty. If empty, a character is written to
 * the data register.
 *
 * If the hardware flow control is enabled then the handshake signal CTS has to
 * be asserted in order to send a character.
 *
 * @param dev UART device struct
 * @param c Character to send
 *
 * @return sent character
 */
static unsigned char uart_k20_poll_out(struct device *dev,
				       unsigned char c)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	/* wait for transmitter to ready to accept a character */
	while (uart->s1.field.tx_data_empty == 0)
		;

	uart->d = c;

	return c;
}

#if CONFIG_UART_INTERRUPT_DRIVEN

/**
 * @brief Fill FIFO with data
 *
 * @param dev UART device struct
 * @param tx_data Data to transmit
 * @param len Number of bytes to send
 *
 * @return number of bytes sent
 */
static int uart_k20_fifo_fill(struct device *dev, const uint8_t *tx_data,
			      int len)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);
	uint8_t num_tx = 0;

	while ((len - num_tx > 0) && (uart->s1.field.tx_data_empty == 1)) {
		uart->d = tx_data[num_tx++];
	}

	return num_tx;
}

/**
 * @brief Read data from FIFO
 *
 * @param dev UART device struct
 * @param rx_data Pointer to data container
 * @param size Container size in bytes
 *
 * @return number of bytes read
 */
static int uart_k20_fifo_read(struct device *dev, uint8_t *rx_data,
			      const int size)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);
	uint8_t num_rx = 0;

	while ((size - num_rx > 0) && (uart->s1.field.rx_data_full != 0)) {
		rx_data[num_rx++] = uart->d;
	}

	return num_rx;
}

/**
 * @brief Enable TX interrupt
 *
 * @param dev UART device struct
 *
 * @return N/A
 */
static void uart_k20_irq_tx_enable(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	uart->c2.field.tx_int_dma_tx_en = 1;
}

/**
 * @brief Disable TX interrupt in IER
 *
 * @param dev UART device struct
 *
 * @return N/A
 */
static void uart_k20_irq_tx_disable(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	uart->c2.field.tx_int_dma_tx_en = 0;
}

/**
 * @brief Check if Tx IRQ has been raised
 *
 * @param dev UART device struct
 *
 * @return 1 if an IRQ is ready, 0 otherwise
 */
static int uart_k20_irq_tx_ready(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	return (uart->c2.field.tx_int_dma_tx_en == 0) ?
			0 : uart->s1.field.tx_data_empty;
}

/**
 * @brief Enable RX interrupt in IER
 *
 * @param dev UART device struct
 *
 * @return N/A
 */
static void uart_k20_irq_rx_enable(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	uart->c2.field.rx_full_int_dma_tx_en = 1;
}

/**
 * @brief Disable RX interrupt in IER
 *
 * @param dev UART device struct
 *
 * @return N/A
 */
static void uart_k20_irq_rx_disable(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	uart->c2.field.rx_full_int_dma_tx_en = 0;
}

/**
 * @brief Check if Rx IRQ has been raised
 *
 * @param dev UART device struct
 *
 * @return 1 if an IRQ is ready, 0 otherwise
 */
static int uart_k20_irq_rx_ready(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);

	return (uart->c2.field.rx_full_int_dma_tx_en == 0) ?
			0 : uart->s1.field.rx_data_full;
}

/**
 * @brief Enable error interrupt
 *
 * @param dev UART device struct
 *
 * @return N/A
 */
static void uart_k20_irq_err_enable(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);
	union C3 c3 = uart->c3;

	c3.field.parity_err_int_en = 1;
	c3.field.frame_err_int_en = 1;
	c3.field.noise_err_int_en = 1;
	c3.field.overrun_err_int_en = 1;
	uart->c3 = c3;
}

/**
 * @brief Disable error interrupt
 *
 * @param dev UART device struct
 *
 * @return N/A
 */
static void uart_k20_irq_err_disable(struct device *dev)
{
	volatile struct K20_UART *uart = UART_STRUCT(dev);
	union C3 c3 = uart->c3;

	c3.field.parity_err_int_en = 0;
	c3.field.frame_err_int_en = 0;
	c3.field.noise_err_int_en = 0;
	c3.field.overrun_err_int_en = 0;
	uart->c3 = c3;
}

/**
 * @brief Check if Tx or Rx IRQ is pending
 *
 * @param dev UART device struct
 *
 * @return 1 if a Tx or Rx IRQ is pending, 0 otherwise
 */
static int uart_k20_irq_is_pending(struct device *dev)
{

	return uart_k20_irq_tx_ready(dev) || uart_k20_irq_rx_ready(dev);
}

/**
 * @brief Update IRQ status
 *
 * @param dev UART device struct
 *
 * @return always 1
 */
static int uart_k20_irq_update(struct device *dev)
{
	return 1;
}

#endif /* CONFIG_UART_INTERRUPT_DRIVEN */


static struct uart_driver_api uart_k20_driver_api = {
	.poll_in = uart_k20_poll_in,
	.poll_out = uart_k20_poll_out,

#ifdef CONFIG_UART_INTERRUPT_DRIVEN

	.fifo_fill = uart_k20_fifo_fill,
	.fifo_read = uart_k20_fifo_read,
	.irq_tx_enable = uart_k20_irq_tx_enable,
	.irq_tx_disable = uart_k20_irq_tx_disable,
	.irq_tx_ready = uart_k20_irq_tx_ready,
	.irq_rx_enable = uart_k20_irq_rx_enable,
	.irq_rx_disable = uart_k20_irq_rx_disable,
	.irq_rx_ready = uart_k20_irq_rx_ready,
	.irq_err_enable = uart_k20_irq_err_enable,
	.irq_err_disable = uart_k20_irq_err_disable,
	.irq_is_pending = uart_k20_irq_is_pending,
	.irq_update = uart_k20_irq_update,

#endif
};


#ifdef CONFIG_UART_K20_PORT_0

static struct uart_device_config uart_k20_dev_cfg_0 = {
	.base = (uint8_t *)CONFIG_UART_K20_PORT_0_BASE_ADDR,
	.sys_clk_freq = CONFIG_UART_K20_PORT_0_CLK_FREQ,
};

static struct uart_k20_dev_data_t uart_k20_dev_data_0 = {
	.baud_rate = CONFIG_UART_K20_PORT_0_BAUD_RATE,
};

DEVICE_INIT(uart_k20_0, CONFIG_UART_K20_PORT_0_NAME, &uart_k20_init,
			&uart_k20_dev_data_0, &uart_k20_dev_cfg_0,
			PRIMARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

#endif /* CONFIG_UART_K20_PORT_0 */

#ifdef CONFIG_UART_K20_PORT_1

static struct uart_device_config uart_k20_dev_cfg_1 = {
	.base = (uint8_t *)CONFIG_UART_K20_PORT_1_BASE_ADDR,
	.sys_clk_freq = CONFIG_UART_K20_PORT_1_CLK_FREQ,
};

static struct uart_k20_dev_data_t uart_k20_dev_data_1 = {
	.baud_rate = CONFIG_UART_K20_PORT_1_BAUD_RATE,
};

DEVICE_INIT(uart_k20_1, CONFIG_UART_K20_PORT_1_NAME, &uart_k20_init,
			&uart_k20_dev_data_1, &uart_k20_dev_cfg_1,
			PRIMARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

#endif /* CONFIG_UART_K20_PORT_1 */

#ifdef CONFIG_UART_K20_PORT_2

static struct uart_device_config uart_k20_dev_cfg_2 = {
	.base = (uint8_t *)CONFIG_UART_K20_PORT_2_BASE_ADDR,
	.sys_clk_freq = CONFIG_UART_K20_PORT_2_CLK_FREQ,
};

static struct uart_k20_dev_data_t uart_k20_dev_data_2 = {
	.baud_rate = CONFIG_UART_K20_PORT_2_BAUD_RATE,
};

DEVICE_INIT(uart_k20_2, CONFIG_UART_K20_PORT_2_NAME, &uart_k20_init,
			&uart_k20_dev_data_2, &uart_k20_dev_cfg_2,
			PRIMARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

#endif /* CONFIG_UART_K20_PORT_2 */

#ifdef CONFIG_UART_K20_PORT_3

static struct uart_device_config uart_k20_dev_cfg_3 = {
	.base = (uint8_t *)CONFIG_UART_K20_PORT_3_BASE_ADDR,
	.sys_clk_freq = CONFIG_UART_K20_PORT_3_CLK_FREQ,
};

static struct uart_k20_dev_data_t uart_k20_dev_data_3 = {
	.baud_rate = CONFIG_UART_K20_PORT_3_BAUD_RATE,
};

DEVICE_INIT(uart_k20_3, CONFIG_UART_K20_PORT_3_NAME, &uart_k20_init,
			&uart_k20_dev_data_3, &uart_k20_dev_cfg_3,
			PRIMARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

#endif /* CONFIG_UART_K20_PORT_3 */

#ifdef CONFIG_UART_K20_PORT_4

static struct uart_device_config uart_k20_dev_cfg_4 = {
	.base = (uint8_t *)CONFIG_UART_K20_PORT_4_BASE_ADDR,
	.sys_clk_freq = CONFIG_UART_K20_PORT_4_CLK_FREQ,
};

static struct uart_k20_dev_data_t uart_k20_dev_data_4 = {
	.baud_rate = CONFIG_UART_K20_PORT_4_BAUD_RATE,
};

DEVICE_INIT(uart_k20_4, CONFIG_UART_K20_PORT_4_NAME, &uart_k20_init,
			&uart_k20_dev_data_4, &uart_k20_dev_cfg_4,
			PRIMARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);

#endif /* CONFIG_UART_K20_PORT_4 */