Skip to content

Commit 661d7f3

Browse files
RtkFPcaiyi_zhong
authored andcommitted
drivers: watchdog: add watchdog driver for RTS5817
Add watchdog driver for RTS5817 Signed-off-by: Darcy Lu <darcy_lu@realsil.com.cn>
1 parent c2f2844 commit 661d7f3

File tree

7 files changed

+245
-0
lines changed

7 files changed

+245
-0
lines changed

drivers/watchdog/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ zephyr_library_sources_ifdef(CONFIG_WDT_XMC4XXX wdt_xmc4xxx.c)
5353
zephyr_library_sources_ifdef(CONFIG_WWDT_NUMAKER wdt_wwdt_numaker.c)
5454
zephyr_library_sources_ifdef(CONFIG_WDT_ENE_KB1200 wdt_ene_kb1200.c)
5555
zephyr_library_sources_ifdef(CONFIG_WDT_IWDG_WCH wdt_iwdg_wch.c)
56+
zephyr_library_sources_ifdef(CONFIG_WDT_RTS5817 wdt_rts5817.c)
5657

5758
zephyr_library_sources_ifdef(CONFIG_WDT_DW wdt_dw.c wdt_dw_common.c)
5859
zephyr_library_sources_ifdef(CONFIG_WDT_INTEL_ADSP wdt_intel_adsp.c wdt_dw_common.c)

drivers/watchdog/Kconfig

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

158158
source "drivers/watchdog/Kconfig.nxp_ewm"
159159

160+
source "drivers/watchdog/Kconfig.rts5817"
161+
160162
endif # WATCHDOG

drivers/watchdog/Kconfig.rts5817

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2024 Realtek Semiconductor, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config WDT_RTS5817
5+
bool "Realtek RTS5817 Watchdog driver"
6+
default y
7+
depends on DT_HAS_REALTEK_RTS5817_WATCHDOG_ENABLED
8+
select HAS_WDT_DISABLE_AT_BOOT
9+
help
10+
Realtek RTS5817 watchdog driver.

drivers/watchdog/wdt_rts5817.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 2024 Realtek Semiconductor, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT realtek_rts5817_watchdog
8+
9+
#include <zephyr/kernel.h>
10+
#include <zephyr/drivers/watchdog.h>
11+
#include "wdt_rts5817.h"
12+
13+
#define LOG_LEVEL CONFIG_WDT_LOG_LEVEL
14+
#include <zephyr/logging/log.h>
15+
LOG_MODULE_REGISTER(wdt_rts5817);
16+
17+
#define WDOG_TIME_1S 0x0
18+
#define WDOG_TIME_2S 0x1
19+
#define WDOG_TIME_4S 0x2
20+
#define WDOG_TIME_8S 0x3
21+
22+
struct rts_fp_wdt_config {
23+
mem_addr_t base;
24+
};
25+
26+
static inline uint32_t rts_fp_read_reg(const struct device *dev)
27+
{
28+
const struct rts_fp_wdt_config *config = dev->config;
29+
30+
return sys_read32(config->base);
31+
}
32+
33+
static inline void rts_fp_write_reg(const struct device *dev, uint32_t value)
34+
{
35+
const struct rts_fp_wdt_config *config = dev->config;
36+
37+
sys_write32(value, config->base);
38+
}
39+
40+
static int rts_fp_wdt_setup(const struct device *dev, uint8_t options)
41+
{
42+
uint32_t value;
43+
44+
if ((options & WDT_OPT_PAUSE_IN_SLEEP) || (options & WDT_OPT_PAUSE_HALTED_BY_DBG)) {
45+
LOG_ERR("Pause in sleep or halted by dbg is not supported");
46+
return -ENOTSUP;
47+
}
48+
49+
value = rts_fp_read_reg(dev);
50+
value |= WDOG_EN;
51+
rts_fp_write_reg(dev, value);
52+
53+
return 0;
54+
}
55+
56+
static int rts_fp_wdt_disable(const struct device *dev)
57+
{
58+
uint32_t value;
59+
60+
value = rts_fp_read_reg(dev);
61+
value &= ~WDOG_EN;
62+
rts_fp_write_reg(dev, value);
63+
64+
return 0;
65+
}
66+
67+
static int rts_fp_wdt_install_timeout(const struct device *dev, const struct wdt_timeout_cfg *cfg)
68+
{
69+
uint32_t value;
70+
71+
if (cfg->window.min > 0 || cfg->window.max > 8000) {
72+
LOG_ERR("watchdog window.min should be equal to 0 and window.max should not exceed "
73+
"8000");
74+
return -ENOTSUP;
75+
}
76+
77+
/* Set mode of watchdog and callback */
78+
switch (cfg->flags) {
79+
case WDT_FLAG_RESET_NONE:
80+
break;
81+
82+
case WDT_FLAG_RESET_CPU_CORE:
83+
case WDT_FLAG_RESET_SOC:
84+
if (cfg->callback != NULL) {
85+
LOG_ERR("watchdog callback is not supported");
86+
return -ENOTSUP;
87+
}
88+
89+
value = rts_fp_read_reg(dev);
90+
value &= ~WDOG_TIME_MASK;
91+
92+
if (cfg->window.max <= 1000) {
93+
value |= WDOG_TIME_1S << WDOG_TIME_OFFSET;
94+
} else if (cfg->window.max > 1000 && cfg->window.max <= 2000) {
95+
value |= WDOG_TIME_2S << WDOG_TIME_OFFSET;
96+
} else if (cfg->window.max > 2000 && cfg->window.max <= 4000) {
97+
value |= WDOG_TIME_4S << WDOG_TIME_OFFSET;
98+
} else if (cfg->window.max > 4000 && cfg->window.max <= 8000) {
99+
value |= WDOG_TIME_8S << WDOG_TIME_OFFSET;
100+
} else {
101+
return -ENOTSUP;
102+
}
103+
104+
rts_fp_write_reg(dev, value);
105+
106+
break;
107+
108+
default:
109+
LOG_ERR("unknown watchdog flags");
110+
return -EINVAL;
111+
}
112+
113+
return 0;
114+
}
115+
116+
static int rts_fp_wdt_feed(const struct device *dev, int channel_id)
117+
{
118+
uint32_t value;
119+
120+
value = rts_fp_read_reg(dev);
121+
value |= WDOG_CLR;
122+
rts_fp_write_reg(dev, value);
123+
124+
return 0;
125+
}
126+
127+
static int rts_fp_wdt_init(const struct device *dev)
128+
{
129+
/* Note: Watchdog is enabled by default for rts5817.
130+
* so CONFIG_WDT_DISABLE_AT_BOOT needs to be selected by default
131+
*/
132+
133+
if (IS_ENABLED(CONFIG_WDT_DISABLE_AT_BOOT)) {
134+
rts_fp_wdt_disable(dev);
135+
}
136+
return 0;
137+
}
138+
139+
static const struct wdt_driver_api rts_fp_wdt_driver_api = {
140+
.setup = rts_fp_wdt_setup,
141+
.disable = rts_fp_wdt_disable,
142+
.install_timeout = rts_fp_wdt_install_timeout,
143+
.feed = rts_fp_wdt_feed,
144+
};
145+
146+
const struct rts_fp_wdt_config rts_fp_config = {
147+
.base = DT_INST_REG_ADDR(0),
148+
};
149+
150+
DEVICE_DT_INST_DEFINE(0, &rts_fp_wdt_init, NULL, NULL, &rts_fp_config, PRE_KERNEL_1,
151+
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &rts_fp_wdt_driver_api);

drivers/watchdog/wdt_rts5817.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2024 Realtek Semiconductor, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_DRIVERS_WATCHDOG_WDT_RTS_RTS5817_H_
8+
#define ZEPHYR_DRIVERS_WATCHDOG_WDT_RTS_RTS5817_H_
9+
10+
/* Bits of R_WTDG_CTRL (0X0048) */
11+
12+
#define WDOG_EN_OFFSET 0
13+
#define WDOG_EN_BITS 1
14+
#define WDOG_EN_MASK (((1 << 1) - 1) << 0)
15+
#define WDOG_EN (WDOG_EN_MASK)
16+
17+
#define WDOG_CLR_OFFSET 1
18+
#define WDOG_CLR_BITS 1
19+
#define WDOG_CLR_MASK (((1 << 1) - 1) << 1)
20+
#define WDOG_CLR (WDOG_CLR_MASK)
21+
22+
#define WDOG_TIME_OFFSET 2
23+
#define WDOG_TIME_BITS 2
24+
#define WDOG_TIME_MASK (((1 << 2) - 1) << 2)
25+
#define WDOG_TIME (WDOG_TIME_MASK)
26+
27+
#define WDOG_REG_RST_EN_OFFSET 4
28+
#define WDOG_REG_RST_EN_BITS 1
29+
#define WDOG_REG_RST_EN_MASK (((1 << 1) - 1) << 4)
30+
#define WDOG_REG_RST_EN (WDOG_REG_RST_EN_MASK)
31+
32+
#define WDOG_BUS_RST_EN_OFFSET 5
33+
#define WDOG_BUS_RST_EN_BITS 1
34+
#define WDOG_BUS_RST_EN_MASK (((1 << 1) - 1) << 5)
35+
#define WDOG_BUS_RST_EN (WDOG_BUS_RST_EN_MASK)
36+
37+
#define WDOG_INT_EN_OFFSET 6
38+
#define WDOG_INT_EN_BITS 1
39+
#define WDOG_INT_EN_MASK (((1 << 1) - 1) << 6)
40+
#define WDOG_INT_EN (WDOG_INT_EN_MASK)
41+
42+
#define WDOG_INT_CLR_OFFSET 7
43+
#define WDOG_INT_CLR_BITS 1
44+
#define WDOG_INT_CLR_MASK (((1 << 1) - 1) << 7)
45+
#define WDOG_INT_CLR (WDOG_INT_CLR_MASK)
46+
47+
#define WDOG_INT_OFFSET 8
48+
#define WDOG_INT_BITS 1
49+
#define WDOG_INT_MASK (((1 << 1) - 1) << 8)
50+
#define WDOG_INT (WDOG_INT_MASK)
51+
52+
#define WDOG_INT_SEL_OFFSET 9
53+
#define WDOG_INT_SEL_BITS 1
54+
#define WDOG_INT_SEL_MASK (((1 << 1) - 1) << 9)
55+
#define WDOG_INT_SEL (WDOG_INT_SEL_MASK)
56+
57+
#define WDOG_CNT_OFFSET 16
58+
#define WDOG_CNT_BITS 16
59+
#define WDOG_CNT_MASK (((1 << 16) - 1) << 16)
60+
#define WDOG_CNT (WDOG_CNT_MASK)
61+
62+
#endif /* ZEPHYR_DRIVERS_WATCHDOG_WDT_RTS_RTS5817_H_ */

dts/arm/realtek/fingerprint/rts5817/rts5817_base.dtsi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
#size-cells = <1>;
6666
status = "okay";
6767
};
68+
69+
watchdog: watchdog@40100048 {
70+
compatible = "realtek,rts5817-watchdog";
71+
reg = <0x40100048 0x4>;
72+
status = "disabled";
73+
};
6874
};
6975
};
7076

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2024 Realtek Semiconductor, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
Realtek RTS5817 Watchdog driver
6+
7+
compatible: "realtek,rts5817-watchdog"
8+
9+
include: base.yaml
10+
11+
properties:
12+
reg:
13+
required: true

0 commit comments

Comments
 (0)