Skip to content

Commit 3b68a0f

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 db422fd commit 3b68a0f

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed

drivers/serial/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ zephyr_library_sources_ifdef(CONFIG_UART_XEC uart_mchp_xec.c)
4444
zephyr_library_sources_ifdef(CONFIG_UART_NEORV32 uart_neorv32.c)
4545
zephyr_library_sources_ifdef(CONFIG_USART_GD32 usart_gd32.c)
4646
zephyr_library_sources_ifdef(CONFIG_UART_XEN_HVC uart_hvc_xen.c)
47+
zephyr_library_sources_ifdef(CONFIG_UART_BFLB uart_bflb.c)
4748

4849
zephyr_library_sources_ifdef(CONFIG_USERSPACE uart_handlers.c)
4950

drivers/serial/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,6 @@ source "drivers/serial/Kconfig.neorv32"
169169

170170
source "drivers/serial/Kconfig.xen"
171171

172+
source "drivers/serial/Kconfig.bflb"
173+
172174
endif # SERIAL

drivers/serial/Kconfig.bflb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Bouffalo Lab UART configuration
2+
#
3+
# Copyright (c) 2021 ATL Electronics
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
# Workaround for not being able to have commas in macro arguments
7+
DT_COMPAT_BFLB_BL_UART := bflb,bl-uart
8+
9+
config UART_BFLB
10+
bool "Bouffalo Lab serial driver"
11+
default $(dt_compat_enabled,$(DT_COMPAT_BFLB_BL_UART))
12+
depends on SOC_FAMILY_BFLB
13+
select SERIAL_HAS_DRIVER
14+
select USE_BFLB_UART
15+
help
16+
This option enables the UART driver for Bouffalo Lab SoC family.

drivers/serial/uart_bflb.c

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

dts/riscv/bouffalolab/bl6.dtsi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,24 @@
119119
#address-cells = <1>;
120120
#size-cells = <0>;
121121
};
122+
123+
uart0: uart@4000a000 {
124+
compatible = "bflb,bl-uart";
125+
reg = <0x4000a000 0x100>;
126+
peripheral-id = <0>;
127+
interrupts = <29 0>;
128+
interrupt-parent = <&ictrl>;
129+
status = "disabled";
130+
label = "uart_0";
131+
};
132+
uart1: uart@4000a100 {
133+
compatible = "bflb,bl-uart";
134+
reg = <0x4000a100 0x100>;
135+
peripheral-id = <1>;
136+
interrupts = <30 0>;
137+
interrupt-parent = <&ictrl>;
138+
status = "disabled";
139+
label = "uart_1";
140+
};
122141
};
123142
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2021 Gerson Fernando Budke <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef ZEPHYR_HAL_BFLB_UART_H_
7+
#define ZEPHYR_HAL_BFLB_UART_H_
8+
9+
#ifdef CONFIG_SOC_SERIES_BL6
10+
#include <bl602_uart.h>
11+
#endif
12+
13+
#endif /* ZEPHYR_HAL_BFLB_UART_H_ */

0 commit comments

Comments
 (0)