2018-03-14 18:36:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Diego Sueiro <diego.sueiro@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-04-01 14:50:40 -05:00
|
|
|
#define DT_DRV_COMPAT nxp_imx_uart
|
2018-03-14 18:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Driver for UART on NXP IMX family processor.
|
|
|
|
*
|
|
|
|
* For full serial function, use the USART controller.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kernel.h>
|
|
|
|
#include <arch/cpu.h>
|
2019-06-26 10:33:39 -04:00
|
|
|
#include <sys/__assert.h>
|
2018-10-31 12:44:45 -05:00
|
|
|
#include <soc.h>
|
2018-03-14 18:36:53 +00:00
|
|
|
#include <init.h>
|
2019-06-25 15:54:01 -04:00
|
|
|
#include <drivers/uart.h>
|
2018-03-14 18:36:53 +00:00
|
|
|
#include <uart_imx.h>
|
|
|
|
|
|
|
|
#define DEV_CFG(dev) \
|
2020-05-28 20:44:16 +02:00
|
|
|
((const struct imx_uart_config *const)(dev)->config)
|
2018-03-14 18:36:53 +00:00
|
|
|
#define UART_STRUCT(dev) \
|
|
|
|
((UART_Type *)(DEV_CFG(dev))->base)
|
|
|
|
|
|
|
|
struct imx_uart_config {
|
|
|
|
UART_Type *base;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t baud_rate;
|
|
|
|
uint8_t modem_mode;
|
2018-03-14 18:36:53 +00:00
|
|
|
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
|
2020-04-30 20:33:38 +02:00
|
|
|
void (*irq_config_func)(const struct device *dev);
|
2018-03-14 18:36:53 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct imx_uart_data {
|
|
|
|
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
|
2018-07-16 21:12:26 +03:00
|
|
|
uart_irq_callback_user_data_t callback;
|
|
|
|
void *cb_data;
|
2018-03-14 18:36:53 +00:00
|
|
|
#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
|
|
|
|
*/
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_init(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct imx_uart_config *config = dev->config;
|
2018-08-14 17:57:08 -07:00
|
|
|
unsigned int old_level;
|
2018-03-14 18:36:53 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2018-07-16 09:51:21 +01:00
|
|
|
UART_SetTxFifoWatermark(uart, 2);
|
2018-03-14 18:36:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_poll_out(const struct device *dev, unsigned char c)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
2019-06-04 10:52:23 -04:00
|
|
|
while (!UART_GetStatusFlag(uart, uartStatusTxReady)) {
|
|
|
|
}
|
2018-03-14 18:36:53 +00:00
|
|
|
UART_Putchar(uart, c);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_poll_in(const struct device *dev, unsigned char *c)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
2019-06-04 10:52:23 -04:00
|
|
|
while (!UART_GetStatusFlag(uart, uartStatusRxDataReady)) {
|
|
|
|
}
|
2018-03-14 18:36:53 +00:00
|
|
|
*c = UART_Getchar(uart);
|
|
|
|
|
|
|
|
if (UART_GetStatusFlag(uart, uartStatusRxOverrun)) {
|
|
|
|
UART_ClearStatusFlag(uart, uartStatusRxOverrun);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_fifo_fill(const struct device *dev,
|
|
|
|
const uint8_t *tx_data,
|
2018-03-14 18:36:53 +00:00
|
|
|
int size)
|
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
2018-11-29 11:12:22 -08:00
|
|
|
unsigned int num_tx = 0U;
|
2018-03-14 18:36:53 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_fifo_read(const struct device *dev, uint8_t *rx_data,
|
2018-03-14 18:36:53 +00:00
|
|
|
const int size)
|
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
2018-11-29 11:12:22 -08:00
|
|
|
unsigned int num_rx = 0U;
|
2018-03-14 18:36:53 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_irq_tx_enable(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
UART_SetIntCmd(uart, uartIntTxReady, true);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_irq_tx_disable(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
UART_SetIntCmd(uart, uartIntTxReady, false);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_irq_tx_ready(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
return UART_GetStatusFlag(uart, uartStatusTxReady);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_irq_rx_enable(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
UART_SetIntCmd(uart, uartIntRxReady, true);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_irq_rx_disable(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
UART_SetIntCmd(uart, uartIntRxReady, false);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_irq_rx_ready(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
return UART_GetStatusFlag(uart, uartStatusRxReady);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_irq_err_enable(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
UART_SetIntCmd(uart, uartIntParityError, true);
|
|
|
|
UART_SetIntCmd(uart, uartIntFrameError, true);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_irq_err_disable(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
UART_SetIntCmd(uart, uartIntParityError, false);
|
|
|
|
UART_SetIntCmd(uart, uartIntFrameError, false);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_irq_is_pending(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
UART_Type *uart = UART_STRUCT(dev);
|
|
|
|
|
|
|
|
return UART_GetStatusFlag(uart, uartStatusRxReady) ||
|
|
|
|
UART_GetStatusFlag(uart, uartStatusTxReady);
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int uart_imx_irq_update(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static void uart_imx_irq_callback_set(const struct device *dev,
|
|
|
|
uart_irq_callback_user_data_t cb,
|
|
|
|
void *cb_data)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct imx_uart_data *data = dev->data;
|
2018-03-14 18:36:53 +00:00
|
|
|
|
|
|
|
data->callback = cb;
|
2018-07-16 21:12:26 +03:00
|
|
|
data->cb_data = cb_data;
|
2018-03-14 18:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
void uart_imx_isr(const struct device *dev)
|
2018-03-14 18:36:53 +00:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct imx_uart_data *data = dev->data;
|
2018-03-14 18:36:53 +00:00
|
|
|
|
|
|
|
if (data->callback) {
|
2020-06-24 15:47:15 +02:00
|
|
|
data->callback(dev, data->cb_data);
|
2018-03-14 18:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2020-04-01 14:50:40 -05:00
|
|
|
#define UART_IMX_DECLARE_CFG(n, IRQ_FUNC_INIT) \
|
|
|
|
static const struct imx_uart_config imx_uart_##n##_config = { \
|
|
|
|
.base = (UART_Type *) DT_INST_REG_ADDR(n), \
|
|
|
|
.baud_rate = DT_INST_PROP(n, current_speed), \
|
|
|
|
.modem_mode = DT_INST_PROP(n, modem_mode), \
|
|
|
|
IRQ_FUNC_INIT \
|
|
|
|
}
|
2018-03-14 18:36:53 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
|
2020-04-01 14:50:40 -05:00
|
|
|
#define UART_IMX_CONFIG_FUNC(n) \
|
2020-04-30 20:33:38 +02:00
|
|
|
static void irq_config_func_##n(const struct device *dev) \
|
2020-04-01 14:50:40 -05:00
|
|
|
{ \
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(n), \
|
|
|
|
DT_INST_IRQ(n, priority), \
|
|
|
|
uart_imx_isr, \
|
2020-12-11 10:12:30 -06:00
|
|
|
DEVICE_DT_INST_GET(n), 0); \
|
2020-04-01 14:50:40 -05:00
|
|
|
irq_enable(DT_INST_IRQN(n)); \
|
|
|
|
}
|
|
|
|
#define UART_IMX_IRQ_CFG_FUNC_INIT(n) \
|
|
|
|
.irq_config_func = irq_config_func_##n
|
|
|
|
#define UART_IMX_INIT_CFG(n) \
|
|
|
|
UART_IMX_DECLARE_CFG(n, UART_IMX_IRQ_CFG_FUNC_INIT(n))
|
|
|
|
#else
|
|
|
|
#define UART_IMX_CONFIG_FUNC(n)
|
|
|
|
#define UART_IMX_IRQ_CFG_FUNC_INIT
|
|
|
|
#define UART_IMX_INIT_CFG(n) \
|
|
|
|
UART_IMX_DECLARE_CFG(n, UART_IMX_IRQ_CFG_FUNC_INIT)
|
2018-03-14 18:36:53 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-01 14:50:40 -05:00
|
|
|
#define UART_IMX_INIT(n) \
|
|
|
|
static struct imx_uart_data imx_uart_##n##_data; \
|
|
|
|
\
|
|
|
|
static const struct imx_uart_config imx_uart_##n##_config; \
|
|
|
|
\
|
2021-04-28 12:01:21 +02:00
|
|
|
DEVICE_DT_INST_DEFINE(n, &uart_imx_init, NULL, \
|
2020-04-01 14:50:40 -05:00
|
|
|
&imx_uart_##n##_data, &imx_uart_##n##_config, \
|
|
|
|
PRE_KERNEL_1, \
|
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
|
|
|
|
&uart_imx_driver_api); \
|
|
|
|
\
|
|
|
|
UART_IMX_CONFIG_FUNC(n) \
|
|
|
|
\
|
2020-05-07 14:09:05 -05:00
|
|
|
UART_IMX_INIT_CFG(n);
|
2020-04-01 14:50:40 -05:00
|
|
|
|
2020-05-06 11:23:07 -07:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(UART_IMX_INIT)
|