Skip to content

Commit bcc4689

Browse files
ck-telecomcfriedt
authored andcommitted
drivers: watchdog: add watchdog driver for sf32lb platform
Add initial watchdog driver for SF32LB platform Signed-off-by: Qingsong Gou <[email protected]>
1 parent 7bd0604 commit bcc4689

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

drivers/watchdog/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ zephyr_library_sources_ifdef(CONFIG_WDT_SHELL wdt_shell.c)
6565
zephyr_library_sources_ifdef(CONFIG_WDT_RENESAS_RA wdt_renesas_ra.c)
6666
zephyr_library_sources_ifdef(CONFIG_WDT_RENESAS_RX_IWDT wdt_renesas_rx_iwdt.c)
6767
zephyr_library_sources_ifdef(CONFIG_WDT_NXP_EWM wdt_nxp_ewm.c)
68-
68+
zephyr_library_sources_ifdef(CONFIG_WDT_SF32LB wdt_sf32lb.c)
6969
zephyr_library_sources_ifdef(CONFIG_WDT_TI_RTI wdt_ti_rti.c)
7070

7171
zephyr_library_sources_ifdef(CONFIG_USERSPACE wdt_handlers.c)

drivers/watchdog/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ source "drivers/watchdog/Kconfig.wch"
159159

160160
source "drivers/watchdog/Kconfig.nxp_ewm"
161161

162+
source "drivers/watchdog/Kconfig.sf32lb"
163+
162164
source "drivers/watchdog/Kconfig.xilinx_wwdt"
163165

164166
source "drivers/watchdog/Kconfig.ti_rti"

drivers/watchdog/Kconfig.sf32lb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2025 Qingsong Gou <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config WDT_SF32LB
5+
bool "SF32LB Watchdog (WDT) Driver"
6+
default y
7+
depends on DT_HAS_SIFLI_SF32LB_WDT_ENABLED
8+
select HAS_WDT_DISABLE_AT_BOOT
9+
select HAS_WDT_NO_CALLBACKS
10+
help
11+
Enable WDT driver for SF32LB series MCUs.

drivers/watchdog/wdt_sf32lb.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 2025, Qingsong Gou <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#define DT_DRV_COMPAT sifli_sf32lb_wdt
6+
7+
#include <zephyr/arch/cpu.h>
8+
#include <zephyr/drivers/watchdog.h>
9+
#include <zephyr/logging/log.h>
10+
11+
#include <register.h>
12+
13+
LOG_MODULE_REGISTER(wdt_sf32lb, CONFIG_WDT_LOG_LEVEL);
14+
15+
#define WDT_CVR0 offsetof(WDT_TypeDef, WDT_CVR0)
16+
#define WDT_CR offsetof(WDT_TypeDef, WDT_CR)
17+
#define WDT_CCR offsetof(WDT_TypeDef, WDT_CCR)
18+
19+
#define WDT_CMD_START 0x00000076U
20+
#define WDT_CMD_STOP 0x00000034U
21+
22+
#define WDT_CVR0_MAX 0xFFFFFF
23+
24+
#define WDT_WDT_CR_RESPONSE_MODE1 0U
25+
26+
/* Assume LRC10 clocks WDT (LRC32 support to be added in the future) */
27+
#define WDT_CLK_KHZ 10
28+
29+
#define WDT_WINDOW_MS_MAX (WDT_CVR0_MAX / WDT_CLK_KHZ)
30+
31+
struct wdt_sf32lb_config {
32+
uintptr_t base;
33+
};
34+
35+
static int wdt_sf32lb_setup(const struct device *dev, uint8_t options)
36+
{
37+
const struct wdt_sf32lb_config *config = dev->config;
38+
39+
if (options != 0U) {
40+
LOG_ERR("Options not supported");
41+
return -ENOTSUP;
42+
}
43+
44+
sys_write32(WDT_CMD_START, config->base + WDT_CCR);
45+
46+
return 0;
47+
}
48+
49+
static int wdt_sf32lb_disable(const struct device *dev)
50+
{
51+
const struct wdt_sf32lb_config *config = dev->config;
52+
53+
sys_write32(WDT_CMD_STOP, config->base + WDT_CCR);
54+
55+
return 0;
56+
}
57+
58+
static int wdt_sf32lb_install_timeout(const struct device *dev,
59+
const struct wdt_timeout_cfg *wdt_cfg)
60+
{
61+
const struct wdt_sf32lb_config *config = dev->config;
62+
63+
if (wdt_cfg->flags != WDT_FLAG_RESET_SOC) {
64+
LOG_ERR("Only SoC reset supported");
65+
return -ENOTSUP;
66+
}
67+
68+
if (wdt_cfg->callback != NULL) {
69+
LOG_ERR("Callback not supported");
70+
return -ENOTSUP;
71+
}
72+
73+
if (wdt_cfg->window.min != 0U) {
74+
LOG_ERR("Window mode not supported!");
75+
return -ENOTSUP;
76+
};
77+
78+
if (wdt_cfg->window.max > WDT_WINDOW_MS_MAX) {
79+
return -EINVAL;
80+
}
81+
82+
sys_write32(wdt_cfg->window.max * WDT_CLK_KHZ, config->base + WDT_CVR0);
83+
84+
return 0;
85+
}
86+
87+
static int wdt_sf32lb_feed(const struct device *dev, int channel_id)
88+
{
89+
const struct wdt_sf32lb_config *config = dev->config;
90+
91+
sys_write32(WDT_CMD_START, config->base + WDT_CCR);
92+
93+
return 0;
94+
}
95+
96+
static DEVICE_API(wdt, wdt_sf32lb_api) = {
97+
.setup = wdt_sf32lb_setup,
98+
.disable = wdt_sf32lb_disable,
99+
.install_timeout = wdt_sf32lb_install_timeout,
100+
.feed = wdt_sf32lb_feed,
101+
};
102+
103+
static int wdt_sf32lb_init(const struct device *dev)
104+
{
105+
const struct wdt_sf32lb_config *config = dev->config;
106+
uint32_t cr;
107+
108+
cr = sys_read32(config->base + WDT_CR);
109+
cr &= ~WDT_WDT_CR_RESPONSE_MODE_Msk;
110+
cr |= WDT_WDT_CR_RESPONSE_MODE1;
111+
sys_write32(cr, config->base + WDT_CR);
112+
113+
return 0;
114+
}
115+
116+
#define WDT_SF32LB_INIT(index) \
117+
static const struct wdt_sf32lb_config wdt_sf32lb_config_##index = { \
118+
.base = DT_INST_REG_ADDR(index), \
119+
}; \
120+
DEVICE_DT_INST_DEFINE(index, wdt_sf32lb_init, NULL, NULL, \
121+
&wdt_sf32lb_config_##index, POST_KERNEL, \
122+
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &wdt_sf32lb_api);
123+
124+
DT_INST_FOREACH_STATUS_OKAY(WDT_SF32LB_INIT)

0 commit comments

Comments
 (0)