Skip to content

Commit 2a2dc3d

Browse files
committed
drivers: serial: bouffalolab: Add bflb serial driver
Add Bouffalo Lab serial driver. The driver uses pinctrl to configure pins and have power management capabilities. Signed-off-by: Gerson Fernando Budke <[email protected]>
1 parent 1f65d69 commit 2a2dc3d

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed

drivers/serial/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ zephyr_library_sources_ifdef(CONFIG_UART_ALTERA uart_altera.c)
1919
zephyr_library_sources_ifdef(CONFIG_UART_ALTERA_JTAG uart_altera_jtag.c)
2020
zephyr_library_sources_ifdef(CONFIG_UART_APBUART uart_apbuart.c)
2121
zephyr_library_sources_ifdef(CONFIG_UART_BCM2711_MU uart_bcm2711.c)
22+
zephyr_library_sources_ifdef(CONFIG_UART_BFLB uart_bflb.c)
2223
zephyr_library_sources_ifdef(CONFIG_UART_BT uart_bt.c)
2324
zephyr_library_sources_ifdef(CONFIG_UART_CC13XX_CC26XX uart_cc13xx_cc26xx.c)
2425
zephyr_library_sources_ifdef(CONFIG_UART_CC32XX uart_cc32xx.c)

drivers/serial/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ rsource "Kconfig.xlnx"
229229
rsource "Kconfig.xmc4xxx"
230230
# zephyr-keep-sorted-stop
231231

232+
rsource "Kconfig.bflb"
233+
232234
source "drivers/serial/Kconfig.si32"
233235

234236
source "drivers/serial/Kconfig.wch_usart"

drivers/serial/Kconfig.bflb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2021-2025 ATL Electronics
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
config UART_BFLB
6+
bool "Bouffalo Lab serial driver"
7+
depends on DT_HAS_BFLB_UART_ENABLED
8+
select PINCTRL
9+
select SERIAL_HAS_DRIVER
10+
select USE_BFLB_UART
11+
help
12+
This option enables the UART driver for Bouffalo Lab SoC family.

drivers/serial/uart_bflb.c

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

dts/bindings/serial/bflb,uart.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) 2021-2025 ATL Electronics
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
description: Bouffalo Lab UART
6+
7+
compatible: "bflb,uart"
8+
9+
include:
10+
- name: uart-controller.yaml
11+
- name: pinctrl-device.yaml
12+
13+
properties:
14+
reg:
15+
required: true
16+
17+
peripheral-id:
18+
type: int
19+
description: peripheral ID
20+
required: true

dts/riscv/bouffalolab/bl60x.dtsi

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@
8383
};
8484
};
8585

86+
uart0: uart@4000a000 {
87+
compatible = "bflb,uart";
88+
reg = <0x4000a000 0x100>;
89+
peripheral-id = <0>;
90+
interrupts = <29 0>;
91+
interrupt-parent = <&ictrl>;
92+
status = "disabled";
93+
};
94+
95+
uart1: uart@4000a100 {
96+
compatible = "bflb,uart";
97+
reg = <0x4000a100 0x100>;
98+
peripheral-id = <1>;
99+
interrupts = <30 0>;
100+
interrupt-parent = <&ictrl>;
101+
status = "disabled";
102+
};
103+
86104
spi0: spi@4000a200 {
87105
compatible = "bflb,spi";
88106
reg = <0x4000a200 0x100>;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright (c) 2021-2025 Gerson Fernando Budke <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_HAL_BFLB_UART_H_
8+
#define ZEPHYR_HAL_BFLB_UART_H_
9+
10+
#ifdef CONFIG_SOC_SERIES_BL60X
11+
#include <bl602_uart.h>
12+
#endif
13+
14+
#endif /* ZEPHYR_HAL_BFLB_UART_H_ */

0 commit comments

Comments
 (0)