zephyr/drivers/serial/leuart_gecko.c

425 lines
11 KiB
C
Raw Normal View History

/*
* Copyright (c) 2018, Christian Taedcke, Diego Sueiro
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT silabs_gecko_leuart
#include <errno.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/irq.h>
#include <em_leuart.h>
#include <em_gpio.h>
#include <em_cmu.h>
#include <soc.h>
#define LEUART_PREFIX cmuClock_LEUART
#define CLOCK_ID_PRFX2(prefix, suffix) prefix##suffix
#define CLOCK_ID_PRFX(prefix, suffix) CLOCK_ID_PRFX2(prefix, suffix)
#define CLOCK_LEUART(id) CLOCK_ID_PRFX(LEUART_PREFIX, id)
#define DEV_BASE(dev) \
((LEUART_TypeDef *) \
((const struct leuart_gecko_config * const)(dev)->config)->base)
struct leuart_gecko_config {
LEUART_TypeDef *base;
CMU_Clock_TypeDef clock;
uint32_t baud_rate;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
void (*irq_config_func)(const struct device *dev);
#endif
struct soc_gpio_pin pin_rx;
struct soc_gpio_pin pin_tx;
#ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
uint8_t loc_rx;
uint8_t loc_tx;
#else
uint8_t loc;
#endif
};
struct leuart_gecko_data {
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_callback_user_data_t callback;
void *cb_data;
#endif
};
static int leuart_gecko_poll_in(const struct device *dev, unsigned char *c)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t flags = LEUART_StatusGet(base);
if (flags & LEUART_STATUS_RXDATAV) {
*c = LEUART_Rx(base);
return 0;
}
return -1;
}
static void leuart_gecko_poll_out(const struct device *dev, unsigned char c)
{
LEUART_TypeDef *base = DEV_BASE(dev);
/* LEUART_Tx function already waits for the transmit buffer being empty
* and and waits for the bus to be free to transmit.
*/
LEUART_Tx(base, c);
}
static int leuart_gecko_err_check(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t flags = LEUART_IntGet(base);
int err = 0;
if (flags & LEUART_IF_RXOF) {
err |= UART_ERROR_OVERRUN;
}
if (flags & LEUART_IF_PERR) {
err |= UART_ERROR_PARITY;
}
if (flags & LEUART_IF_FERR) {
err |= UART_ERROR_FRAMING;
}
LEUART_IntClear(base, LEUART_IF_RXOF |
LEUART_IF_PERR |
LEUART_IF_FERR);
return err;
}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static int leuart_gecko_fifo_fill(const struct device *dev,
const uint8_t *tx_data,
int len)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint8_t num_tx = 0U;
while ((len - num_tx > 0) &&
(base->STATUS & LEUART_STATUS_TXBL)) {
base->TXDATA = (uint32_t)tx_data[num_tx++];
}
return num_tx;
}
static int leuart_gecko_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint8_t num_rx = 0U;
while ((len - num_rx > 0) &&
(base->STATUS & LEUART_STATUS_RXDATAV)) {
rx_data[num_rx++] = (uint8_t)base->RXDATA;
}
return num_rx;
}
static void leuart_gecko_irq_tx_enable(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t mask = LEUART_IEN_TXBL | LEUART_IEN_TXC;
LEUART_IntEnable(base, mask);
}
static void leuart_gecko_irq_tx_disable(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t mask = LEUART_IEN_TXBL | LEUART_IEN_TXC;
LEUART_IntDisable(base, mask);
}
static int leuart_gecko_irq_tx_complete(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t flags = LEUART_IntGet(base);
return (flags & LEUART_IF_TXC) != 0U;
}
static int leuart_gecko_irq_tx_ready(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t flags = LEUART_IntGet(base);
return (flags & LEUART_IF_TXBL) != 0U;
}
static void leuart_gecko_irq_rx_enable(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t mask = LEUART_IEN_RXDATAV;
LEUART_IntEnable(base, mask);
}
static void leuart_gecko_irq_rx_disable(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t mask = LEUART_IEN_RXDATAV;
LEUART_IntDisable(base, mask);
}
static int leuart_gecko_irq_rx_full(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t flags = LEUART_IntGet(base);
return (flags & LEUART_IF_RXDATAV) != 0U;
}
static int leuart_gecko_irq_rx_ready(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint32_t mask = LEUART_IEN_RXDATAV;
return (base->IEN & mask)
&& leuart_gecko_irq_rx_full(dev);
}
static void leuart_gecko_irq_err_enable(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
LEUART_IntEnable(base, LEUART_IF_RXOF |
LEUART_IF_PERR |
LEUART_IF_FERR);
}
static void leuart_gecko_irq_err_disable(const struct device *dev)
{
LEUART_TypeDef *base = DEV_BASE(dev);
LEUART_IntDisable(base, LEUART_IF_RXOF |
LEUART_IF_PERR |
LEUART_IF_FERR);
}
static int leuart_gecko_irq_is_pending(const struct device *dev)
{
return leuart_gecko_irq_tx_ready(dev) || leuart_gecko_irq_rx_ready(dev);
}
static int leuart_gecko_irq_update(const struct device *dev)
{
return 1;
}
static void leuart_gecko_irq_callback_set(const struct device *dev,
uart_irq_callback_user_data_t cb,
void *cb_data)
{
struct leuart_gecko_data *data = dev->data;
data->callback = cb;
data->cb_data = cb_data;
}
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
static void leuart_gecko_isr(const struct device *dev)
{
struct leuart_gecko_data *data = dev->data;
if (data->callback) {
data->callback(dev, data->cb_data);
}
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static void leuart_gecko_init_pins(const struct device *dev)
{
const struct leuart_gecko_config *config = dev->config;
LEUART_TypeDef *base = DEV_BASE(dev);
GPIO_PinModeSet(config->pin_rx.port, config->pin_rx.pin,
config->pin_rx.mode, config->pin_rx.out);
GPIO_PinModeSet(config->pin_tx.port, config->pin_tx.pin,
config->pin_tx.mode, config->pin_tx.out);
#ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
base->ROUTEPEN = LEUART_ROUTEPEN_RXPEN | LEUART_ROUTEPEN_TXPEN;
base->ROUTELOC0 =
(config->loc_tx << _LEUART_ROUTELOC0_TXLOC_SHIFT) |
(config->loc_rx << _LEUART_ROUTELOC0_RXLOC_SHIFT);
#else
base->ROUTE = LEUART_ROUTE_RXPEN | LEUART_ROUTE_TXPEN
| (config->loc << 8);
#endif
}
static int leuart_gecko_init(const struct device *dev)
{
const struct leuart_gecko_config *config = dev->config;
LEUART_TypeDef *base = DEV_BASE(dev);
LEUART_Init_TypeDef leuartInit = LEUART_INIT_DEFAULT;
/* The peripheral and gpio clock are already enabled from soc and gpio
* driver
*/
leuartInit.baudrate = config->baud_rate;
/* Enable CORE LE clock in order to access LE modules */
CMU_ClockEnable(cmuClock_CORELE, true);
/* Select LFXO for LEUARTs (and wait for it to stabilize) */
CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
/* Enable LEUART clock */
CMU_ClockEnable(config->clock, true);
/* Init LEUART */
LEUART_Init(base, &leuartInit);
/* Initialize LEUART pins */
leuart_gecko_init_pins(dev);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
config->irq_config_func(dev);
#endif
return 0;
}
static const struct uart_driver_api leuart_gecko_driver_api = {
.poll_in = leuart_gecko_poll_in,
.poll_out = leuart_gecko_poll_out,
.err_check = leuart_gecko_err_check,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.fifo_fill = leuart_gecko_fifo_fill,
.fifo_read = leuart_gecko_fifo_read,
.irq_tx_enable = leuart_gecko_irq_tx_enable,
.irq_tx_disable = leuart_gecko_irq_tx_disable,
.irq_tx_complete = leuart_gecko_irq_tx_complete,
.irq_tx_ready = leuart_gecko_irq_tx_ready,
.irq_rx_enable = leuart_gecko_irq_rx_enable,
.irq_rx_disable = leuart_gecko_irq_rx_disable,
.irq_rx_ready = leuart_gecko_irq_rx_ready,
.irq_err_enable = leuart_gecko_irq_err_enable,
.irq_err_disable = leuart_gecko_irq_err_disable,
.irq_is_pending = leuart_gecko_irq_is_pending,
.irq_update = leuart_gecko_irq_update,
.irq_callback_set = leuart_gecko_irq_callback_set,
#endif
};
#if DT_NODE_HAS_STATUS(DT_DRV_INST(0), okay)
#define PIN_LEUART_0_RXD {DT_INST_PROP_BY_IDX(0, location_rx, 1), \
DT_INST_PROP_BY_IDX(0, location_rx, 2), gpioModeInput, 1}
#define PIN_LEUART_0_TXD {DT_INST_PROP_BY_IDX(0, location_tx, 1), \
DT_INST_PROP_BY_IDX(0, location_tx, 2), gpioModePushPull, 1}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void leuart_gecko_config_func_0(const struct device *dev);
#endif
static const struct leuart_gecko_config leuart_gecko_0_config = {
.base = (LEUART_TypeDef *)DT_INST_REG_ADDR(0),
.clock = CLOCK_LEUART(DT_INST_PROP(0, peripheral_id)),
.baud_rate = DT_INST_PROP(0, current_speed),
.pin_rx = PIN_LEUART_0_RXD,
.pin_tx = PIN_LEUART_0_TXD,
#ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
.loc_rx = DT_INST_PROP_BY_IDX(0, location_rx, 0),
.loc_tx = DT_INST_PROP_BY_IDX(0, location_tx, 0),
#else
#if DT_INST_PROP_BY_IDX(0, location_rx, 0) \
!= DT_INST_PROP_BY_IDX(0, location_tx, 0)
#error LEUART_0 DTS location-* properties must have identical value
#endif
.loc = DT_INST_PROP_BY_IDX(0, location_rx, 0),
#endif
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = leuart_gecko_config_func_0,
#endif
};
static struct leuart_gecko_data leuart_gecko_0_data;
DEVICE_DT_INST_DEFINE(0, &leuart_gecko_init,
NULL, &leuart_gecko_0_data,
&leuart_gecko_0_config, PRE_KERNEL_1,
CONFIG_SERIAL_INIT_PRIORITY,
&leuart_gecko_driver_api);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void leuart_gecko_config_func_0(const struct device *dev)
{
IRQ_CONNECT(DT_INST_IRQN(0),
DT_INST_IRQ(0, priority),
leuart_gecko_isr, DEVICE_DT_INST_GET(0), 0);
irq_enable(DT_INST_IRQN(0));
}
#endif
#endif /* DT_NODE_HAS_STATUS(DT_DRV_INST(0), okay) */
#if DT_NODE_HAS_STATUS(DT_DRV_INST(1), okay)
#define PIN_LEUART_1_RXD {DT_INST_PROP_BY_IDX(1, location_rx, 1), \
DT_INST_PROP_BY_IDX(1, location_rx, 2), gpioModeInput, 1}
#define PIN_LEUART_1_TXD {DT_INST_PROP_BY_IDX(1, location_tx, 1), \
DT_INST_PROP_BY_IDX(1, location_tx, 2), gpioModePushPull, 1}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void leuart_gecko_config_func_1(const struct device *dev);
#endif
static const struct leuart_gecko_config leuart_gecko_1_config = {
.base = (LEUART_TypeDef *)DT_INST_REG_ADDR(1),
.clock = CLOCK_LEUART(DT_INST_PROP(1, peripheral_id)),
.baud_rate = DT_INST_PROP(1, current_speed),
.pin_rx = PIN_LEUART_1_RXD,
.pin_tx = PIN_LEUART_1_TXD,
#ifdef CONFIG_SOC_GECKO_HAS_INDIVIDUAL_PIN_LOCATION
.loc_rx = DT_INST_PROP_BY_IDX(1, location_rx, 0),
.loc_tx = DT_INST_PROP_BY_IDX(1, location_tx, 0),
#else
#if DT_INST_PROP_BY_IDX(1, location_rx, 0) \
!= DT_INST_PROP_BY_IDX(1, location_tx, 0)
#error LEUART_1 DTS location-* properties must have identical value
#endif
.loc = DT_INST_PROP_BY_IDX(1, location_rx, 0),
#endif
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.irq_config_func = leuart_gecko_config_func_1,
#endif
};
static struct leuart_gecko_data leuart_gecko_1_data;
DEVICE_DT_INST_DEFINE(1, &leuart_gecko_init,
NULL, &leuart_gecko_1_data,
&leuart_gecko_1_config, PRE_KERNEL_1,
CONFIG_SERIAL_INIT_PRIORITY,
&leuart_gecko_driver_api);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static void leuart_gecko_config_func_1(const struct device *dev)
{
IRQ_CONNECT(DT_INST_IRQN(1),
DT_INST_IRQ(1, priority),
leuart_gecko_isr, DEVICE_DT_INST_GET(1), 0);
irq_enable(DT_INST_IRQN(1));
}
#endif
#endif /* DT_NODE_HAS_STATUS(DT_DRV_INST(1), okay) */