|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, ATL Electronics |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#define DT_DRV_COMPAT bflb_bl_uart |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief UART driver for Bouffalo Lab MCU family. |
| 10 | + */ |
| 11 | +#include <drivers/uart.h> |
| 12 | +#include <drivers/pinctrl.h> |
| 13 | +#include <bflb_uart.h> |
| 14 | +#include <bflb_glb.h> |
| 15 | + |
| 16 | +#define UART_CTS_FLOWCONTROL_ENABLE (0) |
| 17 | +#define UART_RTS_FLOWCONTROL_ENABLE (0) |
| 18 | +#define UART_MSB_FIRST_ENABLE (0) |
| 19 | +#define UART_DEFAULT_RTO_TIMEOUT (255) |
| 20 | +#define UART_CLOCK_DIV (0) |
| 21 | + |
| 22 | +struct bl_config { |
| 23 | + const struct pinctrl_dev_config *pinctrl_cfg; |
| 24 | + uint32_t periph_id; |
| 25 | + UART_CFG_Type uart_cfg; |
| 26 | + UART_FifoCfg_Type fifo_cfg; |
| 27 | +}; |
| 28 | + |
| 29 | +static int uart_bl_init(const struct device *dev) |
| 30 | +{ |
| 31 | + const struct bl_config *cfg = dev->config; |
| 32 | + |
| 33 | + pinctrl_apply_state(cfg->pinctrl_cfg, PINCTRL_STATE_DEFAULT); |
| 34 | + |
| 35 | + GLB_Set_UART_CLK(1, HBN_UART_CLK_160M, UART_CLOCK_DIV); |
| 36 | + |
| 37 | + UART_IntMask(cfg->periph_id, UART_INT_ALL, 1); |
| 38 | + UART_Disable(cfg->periph_id, UART_TXRX); |
| 39 | + |
| 40 | + UART_Init(cfg->periph_id, (UART_CFG_Type *)&cfg->uart_cfg); |
| 41 | + UART_TxFreeRun(cfg->periph_id, 1); |
| 42 | + UART_SetRxTimeoutValue(cfg->periph_id, UART_DEFAULT_RTO_TIMEOUT); |
| 43 | + UART_FifoConfig(cfg->periph_id, (UART_FifoCfg_Type *)&cfg->fifo_cfg); |
| 44 | + UART_Enable(cfg->periph_id, UART_TXRX); |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static int uart_bl_poll_in(const struct device *dev, unsigned char *c) |
| 50 | +{ |
| 51 | + const struct bl_config *cfg = dev->config; |
| 52 | + |
| 53 | + return UART_ReceiveData(cfg->periph_id, (uint8_t *)c, 1); |
| 54 | +} |
| 55 | + |
| 56 | +static void uart_bl_poll_out(const struct device *dev, unsigned char c) |
| 57 | +{ |
| 58 | + const struct bl_config *cfg = dev->config; |
| 59 | + |
| 60 | + while (UART_GetTxFifoCount(cfg->periph_id) == 0) { |
| 61 | + ; |
| 62 | + } |
| 63 | + |
| 64 | + (void)UART_SendData(cfg->periph_id, (uint8_t *)&c, 1); |
| 65 | +} |
| 66 | + |
| 67 | +#ifdef CONFIG_PM_DEVICE |
| 68 | +static int uart_bl_pm_control(const struct device *dev, |
| 69 | + enum pm_device_action action) |
| 70 | +{ |
| 71 | + const struct bl_config *cfg = dev->config; |
| 72 | + |
| 73 | + switch (action) { |
| 74 | + case PM_DEVICE_ACTION_RESUME: |
| 75 | + (void)pinctrl_apply_state(cfg->pinctrl_cfg, PINCTRL_STATE_DEFAULT); |
| 76 | + UART_Enable(cfg->periph_id, UART_TXRX); |
| 77 | + break; |
| 78 | + case PM_DEVICE_ACTION_SUSPEND: |
| 79 | + if (pinctrl_apply_state(cfg->pinctrl_cfg, PINCTRL_STATE_SLEEP)) { |
| 80 | + return -ENOTSUP; |
| 81 | + } |
| 82 | + UART_Disable(cfg->periph_id, UART_TXRX); |
| 83 | + break; |
| 84 | + default: |
| 85 | + return -ENOTSUP; |
| 86 | + } |
| 87 | + |
| 88 | + return 0; |
| 89 | +} |
| 90 | +#endif /* CONFIG_PM_DEVICE */ |
| 91 | + |
| 92 | +static const struct uart_driver_api uart_bl_driver_api = { |
| 93 | + .poll_in = uart_bl_poll_in, |
| 94 | + .poll_out = uart_bl_poll_out, |
| 95 | +}; |
| 96 | + |
| 97 | +#define BL_UART_INIT(n) \ |
| 98 | + PINCTRL_DT_INST_DEFINE(n) \ |
| 99 | + static const struct bl_config bl_uart##n##_config = { \ |
| 100 | + .pinctrl_cfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \ |
| 101 | + .periph_id = DT_INST_PROP(n, peripheral_id), \ |
| 102 | + \ |
| 103 | + .uart_cfg.baudRate = DT_INST_PROP(n, current_speed), \ |
| 104 | + .uart_cfg.dataBits = UART_DATABITS_8, \ |
| 105 | + .uart_cfg.stopBits = UART_STOPBITS_1, \ |
| 106 | + .uart_cfg.parity = UART_PARITY_NONE, \ |
| 107 | + .uart_cfg.uartClk = SOC_BOUFFALOLAB_BL_PLL160_FREQ_HZ, \ |
| 108 | + .uart_cfg.ctsFlowControl = UART_CTS_FLOWCONTROL_ENABLE, \ |
| 109 | + .uart_cfg.rtsSoftwareControl = UART_RTS_FLOWCONTROL_ENABLE, \ |
| 110 | + .uart_cfg.byteBitInverse = UART_MSB_FIRST_ENABLE, \ |
| 111 | + \ |
| 112 | + .fifo_cfg.txFifoDmaThreshold = 1, \ |
| 113 | + .fifo_cfg.rxFifoDmaThreshold = 1, \ |
| 114 | + .fifo_cfg.txFifoDmaEnable = 0, \ |
| 115 | + .fifo_cfg.rxFifoDmaEnable = 0, \ |
| 116 | + }; \ |
| 117 | + DEVICE_DT_INST_DEFINE(n, &uart_bl_init, \ |
| 118 | + uart_bl_pm_control, \ |
| 119 | + NULL, \ |
| 120 | + &bl_uart##n##_config, PRE_KERNEL_1, \ |
| 121 | + CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| 122 | + &uart_bl_driver_api); |
| 123 | + |
| 124 | +DT_INST_FOREACH_STATUS_OKAY(BL_UART_INIT) |
0 commit comments