396 lines
11 KiB
C
396 lines
11 KiB
C
/*
|
|
* Copyright (c) 2023 TOKITA Hiroshi <tokita.hiroshi@fujitsu.com>
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#define DT_DRV_COMPAT renesas_ra_uart_sci
|
|
|
|
#include <zephyr/drivers/uart.h>
|
|
#include <zephyr/drivers/clock_control.h>
|
|
#include <zephyr/drivers/pinctrl.h>
|
|
#include <zephyr/spinlock.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(ra_uart_sci, CONFIG_UART_LOG_LEVEL);
|
|
|
|
struct uart_ra_cfg {
|
|
mem_addr_t regs;
|
|
const struct device *clock_dev;
|
|
clock_control_subsys_t clock_id;
|
|
const struct pinctrl_dev_config *pcfg;
|
|
};
|
|
|
|
struct uart_ra_data {
|
|
struct uart_config current_config;
|
|
uint32_t clk_rate;
|
|
struct k_spinlock lock;
|
|
};
|
|
|
|
#define REG_MASK(reg) (BIT_MASK(_CONCAT(reg, _LEN)) << _CONCAT(reg, _POS))
|
|
|
|
/* Registers */
|
|
#define SMR 0x00 /*!< Serial Mode Register */
|
|
#define BRR 0x01 /*!< Bit Rate Register */
|
|
#define SCR 0x02 /*!< Serial Control Register */
|
|
#define TDR 0x03 /*!< Transmit Data Register */
|
|
#define SSR 0x04 /*!< Serial Status Register */
|
|
#define RDR 0x05 /*!< Receive Data Register */
|
|
#define SEMR 0x07 /*!< Serial Extended Mode Register */
|
|
#define MDDR 0x12 /*!< Modulation Duty Register */
|
|
#define LSR 0x18 /*!< Line Status Register */
|
|
|
|
/*
|
|
* SMR (Serial Mode Register)
|
|
*
|
|
* - CKS[0..2]: Clock Select
|
|
* - MP[2..3]: Multi-Processor Mode(Valid only in asynchronous mode)
|
|
* - STOP[3..4]: Stop Bit Length(Valid only in asynchronous mode)
|
|
* - PM[4..5]: Parity Mode (Valid only when the PE bit is 1)
|
|
* - PE[5..6]: Parity Enable(Valid only in asynchronous mode)
|
|
* - CHR[6..7]: Character Length(Valid only in asynchronous mode)
|
|
* - CM[7..8]: Communication Mode
|
|
*/
|
|
#define SMR_CKS_POS (0)
|
|
#define SMR_CKS_LEN (2)
|
|
#define SMR_MP_POS (2)
|
|
#define SMR_MP_LEN (1)
|
|
#define SMR_STOP_POS (3)
|
|
#define SMR_STOP_LEN (1)
|
|
#define SMR_PM_POS (4)
|
|
#define SMR_PM_LEN (1)
|
|
#define SMR_PE_POS (5)
|
|
#define SMR_PE_LEN (1)
|
|
#define SMR_CHR_POS (6)
|
|
#define SMR_CHR_LEN (1)
|
|
#define SMR_CM_POS (7)
|
|
#define SMR_CM_LEN (1)
|
|
|
|
/**
|
|
* SCR (Serial Control Register)
|
|
*
|
|
* - CKE[0..2]: Clock Enable
|
|
* - TEIE[2..3]: Transmit End Interrupt Enable
|
|
* - MPIE[3..4]: Multi-Processor Interrupt Enable (Valid in asynchronous
|
|
* - RE[4..5]: Receive Enable
|
|
* - TE[5..6]: Transmit Enable
|
|
* - RIE[6..7]: Receive Interrupt Enable
|
|
* - TIE[7..8]: Transmit Interrupt Enable
|
|
*/
|
|
#define SCR_CKE_POS (0)
|
|
#define SCR_CKE_LEN (2)
|
|
#define SCR_TEIE_POS (2)
|
|
#define SCR_TEIE_LEN (1)
|
|
#define SCR_MPIE_POS (3)
|
|
#define SCR_MPIE_LEN (1)
|
|
#define SCR_RE_POS (4)
|
|
#define SCR_RE_LEN (1)
|
|
#define SCR_TE_POS (5)
|
|
#define SCR_TE_LEN (1)
|
|
#define SCR_RIE_POS (6)
|
|
#define SCR_RIE_LEN (1)
|
|
#define SCR_TIE_POS (7)
|
|
#define SCR_TIE_LEN (1)
|
|
|
|
/**
|
|
* SSR (Serial Status Register)
|
|
*
|
|
* - MPBT[0..1]: Multi-Processor Bit Transfer
|
|
* - MPB[1..2]: Multi-Processor
|
|
* - TEND[2..3]: Transmit End Flag
|
|
* - PER[3..4]: Parity Error Flag
|
|
* - FER[4..5]: Framing Error Flag
|
|
* - ORER[5..6]: Overrun Error Flag
|
|
* - RDRF[6..7]: Receive Data Full Flag
|
|
* - TDRE[7..8]: Transmit Data Empty Flag
|
|
*/
|
|
#define SSR_MPBT_POS (0)
|
|
#define SSR_MPBT_LEN (1)
|
|
#define SSR_MPB_POS (1)
|
|
#define SSR_MPB_LEN (1)
|
|
#define SSR_TEND_POS (2)
|
|
#define SSR_TEND_LEN (1)
|
|
#define SSR_PER_POS (3)
|
|
#define SSR_PER_LEN (1)
|
|
#define SSR_FER_POS (4)
|
|
#define SSR_FER_LEN (1)
|
|
#define SSR_ORER_POS (5)
|
|
#define SSR_ORER_LEN (1)
|
|
#define SSR_RDRF_POS (6)
|
|
#define SSR_RDRF_LEN (1)
|
|
#define SSR_TDRE_POS (7)
|
|
#define SSR_TDRE_LEN (1)
|
|
|
|
/**
|
|
* SEMR (Serial Extended Mode Register)
|
|
*
|
|
* - ACS0[0..1]: Asynchronous Mode Clock Source Select
|
|
* - PADIS[1..2]: Preamble function Disable
|
|
* - BRME[2..3]: Bit Rate Modulation Enable
|
|
* - ABCSE[3..4]: Asynchronous Mode Extended Base Clock Select
|
|
* - ABCS[4..5]: Asynchronous Mode Base Clock Select
|
|
* - NFEN[5..6]: Digital Noise Filter Function Enable
|
|
* - BGDM[6..7]: Baud Rate Generator Double-Speed Mode Select
|
|
* - RXDESEL[7..8]: Asynchronous Start Bit Edge Detection Select
|
|
*/
|
|
#define SEMR_ACS0_POS (0)
|
|
#define SEMR_ACS0_LEN (1)
|
|
#define SEMR_PADIS_POS (1)
|
|
#define SEMR_PADIS_LEN (1)
|
|
#define SEMR_BRME_POS (2)
|
|
#define SEMR_BRME_LEN (1)
|
|
#define SEMR_ABCSE_POS (3)
|
|
#define SEMR_ABCSE_LEN (1)
|
|
#define SEMR_ABCS_POS (4)
|
|
#define SEMR_ABCS_LEN (1)
|
|
#define SEMR_NFEN_POS (5)
|
|
#define SEMR_NFEN_LEN (1)
|
|
#define SEMR_BGDM_POS (6)
|
|
#define SEMR_BGDM_LEN (1)
|
|
#define SEMR_RXDESEL_POS (7)
|
|
#define SEMR_RXDESEL_LEN (1)
|
|
|
|
/**
|
|
* LSR (Line Status Register)
|
|
*
|
|
* - ORER[0..1]: Overrun Error Flag
|
|
* - FNUM[2..7]: Framing Error Count
|
|
* - PNUM[8..13]: Parity Error Count
|
|
*/
|
|
#define LSR_ORER_POS (0)
|
|
#define LSR_ORER_LEN (1)
|
|
#define LSR_FNUM_POS (2)
|
|
#define LSR_FNUM_LEN (5)
|
|
#define LSR_PNUM_POS (8)
|
|
#define LSR_PNUM_LEN (5)
|
|
|
|
static uint8_t uart_ra_read_8(const struct device *dev,
|
|
uint32_t offs)
|
|
{
|
|
const struct uart_ra_cfg *config = dev->config;
|
|
|
|
return sys_read8(config->regs + offs);
|
|
}
|
|
|
|
static void uart_ra_write_8(const struct device *dev,
|
|
uint32_t offs, uint8_t value)
|
|
{
|
|
const struct uart_ra_cfg *config = dev->config;
|
|
|
|
sys_write8(value, config->regs + offs);
|
|
}
|
|
|
|
static uint16_t uart_ra_read_16(const struct device *dev,
|
|
uint32_t offs)
|
|
{
|
|
const struct uart_ra_cfg *config = dev->config;
|
|
|
|
return sys_read16(config->regs + offs);
|
|
}
|
|
|
|
static void uart_ra_write_16(const struct device *dev,
|
|
uint32_t offs, uint16_t value)
|
|
{
|
|
const struct uart_ra_cfg *config = dev->config;
|
|
|
|
sys_write16(value, config->regs + offs);
|
|
}
|
|
|
|
static void uart_ra_set_baudrate(const struct device *dev,
|
|
uint32_t baud_rate)
|
|
{
|
|
struct uart_ra_data *data = dev->data;
|
|
uint8_t reg_val;
|
|
|
|
reg_val = uart_ra_read_8(dev, SEMR);
|
|
reg_val |= REG_MASK(SEMR_BGDM);
|
|
reg_val &= ~(REG_MASK(SEMR_BRME) | REG_MASK(SEMR_ABCSE) | REG_MASK(SEMR_ABCS));
|
|
uart_ra_write_8(dev, SEMR, reg_val);
|
|
|
|
reg_val = (data->clk_rate / (16 * data->current_config.baudrate)) - 1;
|
|
uart_ra_write_8(dev, BRR, reg_val);
|
|
}
|
|
|
|
static int uart_ra_poll_in(const struct device *dev, unsigned char *p_char)
|
|
{
|
|
struct uart_ra_data *data = dev->data;
|
|
int ret = 0;
|
|
|
|
k_spinlock_key_t key = k_spin_lock(&data->lock);
|
|
|
|
if ((uart_ra_read_8(dev, SSR) & REG_MASK(SSR_RDRF)) == 0) {
|
|
ret = -1;
|
|
goto unlock;
|
|
}
|
|
|
|
*p_char = uart_ra_read_8(dev, RDR);
|
|
unlock:
|
|
k_spin_unlock(&data->lock, key);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void uart_ra_poll_out(const struct device *dev, unsigned char out_char)
|
|
{
|
|
struct uart_ra_data *data = dev->data;
|
|
k_spinlock_key_t key = k_spin_lock(&data->lock);
|
|
|
|
uart_ra_write_8(dev, TDR, out_char);
|
|
while (!(uart_ra_read_8(dev, SSR) & REG_MASK(SSR_TEND)) ||
|
|
!(uart_ra_read_8(dev, SSR) & REG_MASK(SSR_TDRE))) {
|
|
;
|
|
}
|
|
k_spin_unlock(&data->lock, key);
|
|
}
|
|
|
|
static int uart_ra_configure(const struct device *dev,
|
|
const struct uart_config *cfg)
|
|
{
|
|
struct uart_ra_data *data = dev->data;
|
|
|
|
uint16_t reg_val;
|
|
k_spinlock_key_t key;
|
|
|
|
if (cfg->parity != UART_CFG_PARITY_NONE || cfg->stop_bits != UART_CFG_STOP_BITS_1 ||
|
|
cfg->data_bits != UART_CFG_DATA_BITS_8 || cfg->flow_ctrl != UART_CFG_FLOW_CTRL_NONE) {
|
|
return -ENOTSUP;
|
|
}
|
|
|
|
key = k_spin_lock(&data->lock);
|
|
|
|
/* Disable Transmit and Receive */
|
|
reg_val = uart_ra_read_8(dev, SCR);
|
|
reg_val &= ~(REG_MASK(SCR_TE) | REG_MASK(SCR_RE));
|
|
uart_ra_write_8(dev, SCR, reg_val);
|
|
|
|
/* Resetting Errors Registers */
|
|
reg_val = uart_ra_read_8(dev, SSR);
|
|
reg_val &= ~(REG_MASK(SSR_PER) | REG_MASK(SSR_FER) | REG_MASK(SSR_ORER) |
|
|
REG_MASK(SSR_RDRF) | REG_MASK(SSR_TDRE));
|
|
uart_ra_write_8(dev, SSR, reg_val);
|
|
|
|
reg_val = uart_ra_read_16(dev, LSR);
|
|
reg_val &= ~(REG_MASK(LSR_ORER));
|
|
uart_ra_write_16(dev, LSR, reg_val);
|
|
|
|
/* Select internal clock */
|
|
reg_val = uart_ra_read_8(dev, SCR);
|
|
reg_val &= ~(REG_MASK(SCR_CKE));
|
|
uart_ra_write_8(dev, SCR, reg_val);
|
|
|
|
/* Serial Configuration (8N1) & Clock divider selection */
|
|
reg_val = uart_ra_read_8(dev, SMR);
|
|
reg_val &= ~(REG_MASK(SMR_CM) | REG_MASK(SMR_CHR) | REG_MASK(SMR_PE) | REG_MASK(SMR_PM) |
|
|
REG_MASK(SMR_STOP) | REG_MASK(SMR_CKS));
|
|
uart_ra_write_8(dev, SMR, reg_val);
|
|
|
|
/* Set baudrate */
|
|
uart_ra_set_baudrate(dev, cfg->baudrate);
|
|
|
|
/* Enable Transmit & Receive + disable Interrupts */
|
|
reg_val = uart_ra_read_8(dev, SCR);
|
|
reg_val |= (REG_MASK(SCR_TE) | REG_MASK(SCR_RE));
|
|
reg_val &=
|
|
~(REG_MASK(SCR_TIE) | REG_MASK(SCR_RIE) | REG_MASK(SCR_MPIE) | REG_MASK(SCR_TEIE));
|
|
uart_ra_write_8(dev, SCR, reg_val);
|
|
|
|
data->current_config = *cfg;
|
|
|
|
k_spin_unlock(&data->lock, key);
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
|
|
static int uart_ra_config_get(const struct device *dev,
|
|
struct uart_config *cfg)
|
|
{
|
|
struct uart_ra_data *data = dev->data;
|
|
|
|
*cfg = data->current_config;
|
|
|
|
return 0;
|
|
}
|
|
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
|
|
|
|
static int uart_ra_init(const struct device *dev)
|
|
{
|
|
const struct uart_ra_cfg *config = dev->config;
|
|
struct uart_ra_data *data = dev->data;
|
|
int ret;
|
|
|
|
/* Configure dt provided device signals when available */
|
|
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
|
|
if (ret < 0) {
|
|
return ret;
|
|
}
|
|
|
|
if (!device_is_ready(config->clock_dev)) {
|
|
return -ENODEV;
|
|
}
|
|
|
|
ret = clock_control_on(config->clock_dev, config->clock_id);
|
|
if (ret < 0) {
|
|
return ret;
|
|
}
|
|
|
|
ret = clock_control_get_rate(config->clock_dev, config->clock_id, &data->clk_rate);
|
|
if (ret < 0) {
|
|
return ret;
|
|
}
|
|
|
|
DEVICE_MMIO_MAP(dev, K_MEM_CACHE_NONE);
|
|
|
|
ret = uart_ra_configure(dev, &data->current_config);
|
|
if (ret != 0) {
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct uart_driver_api uart_ra_driver_api = {
|
|
.poll_in = uart_ra_poll_in,
|
|
.poll_out = uart_ra_poll_out,
|
|
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
|
|
.configure = uart_ra_configure,
|
|
.config_get = uart_ra_config_get,
|
|
#endif
|
|
};
|
|
|
|
/* Device Instantiation */
|
|
#define UART_RCAR_INIT_CFG(n) \
|
|
PINCTRL_DT_DEFINE(DT_INST_PARENT(n)); \
|
|
static const struct uart_ra_cfg uart_ra_cfg_##n = { \
|
|
.regs = DT_REG_ADDR(DT_INST_PARENT(n)), \
|
|
.clock_dev = DEVICE_DT_GET(DT_CLOCKS_CTLR(DT_INST_PARENT(n))), \
|
|
.clock_id = \
|
|
(clock_control_subsys_t)DT_CLOCKS_CELL_BY_IDX(DT_INST_PARENT(n), 0, id), \
|
|
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(DT_INST_PARENT(n)), \
|
|
}
|
|
|
|
#define UART_RCAR_INIT(n) \
|
|
UART_RCAR_INIT_CFG(n); \
|
|
\
|
|
static struct uart_ra_data uart_ra_data_##n = { \
|
|
.current_config = { \
|
|
.baudrate = DT_INST_PROP(n, current_speed), \
|
|
.parity = UART_CFG_PARITY_NONE, \
|
|
.stop_bits = UART_CFG_STOP_BITS_1, \
|
|
.data_bits = UART_CFG_DATA_BITS_8, \
|
|
.flow_ctrl = UART_CFG_FLOW_CTRL_NONE, \
|
|
}, \
|
|
}; \
|
|
\
|
|
DEVICE_DT_INST_DEFINE(n, \
|
|
uart_ra_init, \
|
|
NULL, \
|
|
&uart_ra_data_##n, \
|
|
&uart_ra_cfg_##n, \
|
|
PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY, \
|
|
&uart_ra_driver_api); \
|
|
\
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(UART_RCAR_INIT)
|