1 // SPDX-License-Identifier: GPL-2.0-only
3 * watchdog driver for ZTE's zx2967 family
5 * Copyright (C) 2017 ZTE Ltd.
7 * Author: Baoyou Xie <baoyou.xie@linaro.org>
10 #include <linux/clk.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/reset.h>
18 #include <linux/watchdog.h>
20 #define ZX2967_WDT_CFG_REG 0x4
21 #define ZX2967_WDT_LOAD_REG 0x8
22 #define ZX2967_WDT_REFRESH_REG 0x18
23 #define ZX2967_WDT_START_REG 0x1c
25 #define ZX2967_WDT_REFRESH_MASK GENMASK(5, 0)
27 #define ZX2967_WDT_CFG_DIV(n) ((((n) & 0xff) - 1) << 8)
28 #define ZX2967_WDT_START_EN 0x1
31 * Hardware magic number.
32 * When watchdog reg is written, the lowest 16 bits are valid, but
33 * the highest 16 bits should be always this number.
35 #define ZX2967_WDT_WRITEKEY (0x1234 << 16)
36 #define ZX2967_WDT_VAL_MASK GENMASK(15, 0)
38 #define ZX2967_WDT_DIV_DEFAULT 16
39 #define ZX2967_WDT_DEFAULT_TIMEOUT 32
40 #define ZX2967_WDT_MIN_TIMEOUT 1
41 #define ZX2967_WDT_MAX_TIMEOUT 524
42 #define ZX2967_WDT_MAX_COUNT 0xffff
44 #define ZX2967_WDT_CLK_FREQ 0x8000
46 #define ZX2967_WDT_FLAG_REBOOT_MON BIT(0)
49 struct watchdog_device wdt_device
;
50 void __iomem
*reg_base
;
54 static inline u32
zx2967_wdt_readl(struct zx2967_wdt
*wdt
, u16 reg
)
56 return readl_relaxed(wdt
->reg_base
+ reg
);
59 static inline void zx2967_wdt_writel(struct zx2967_wdt
*wdt
, u16 reg
, u32 val
)
61 writel_relaxed(val
| ZX2967_WDT_WRITEKEY
, wdt
->reg_base
+ reg
);
64 static void zx2967_wdt_refresh(struct zx2967_wdt
*wdt
)
68 val
= zx2967_wdt_readl(wdt
, ZX2967_WDT_REFRESH_REG
);
70 * Bit 4-5, 1 and 2: refresh config info
71 * Bit 2-3, 1 and 2: refresh counter
72 * Bit 0-1, 1 and 2: refresh int-value
73 * we shift each group value between 1 and 2 to refresh all data.
75 val
^= ZX2967_WDT_REFRESH_MASK
;
76 zx2967_wdt_writel(wdt
, ZX2967_WDT_REFRESH_REG
,
77 val
& ZX2967_WDT_VAL_MASK
);
81 zx2967_wdt_set_timeout(struct watchdog_device
*wdd
, unsigned int timeout
)
83 struct zx2967_wdt
*wdt
= watchdog_get_drvdata(wdd
);
84 unsigned int divisor
= ZX2967_WDT_DIV_DEFAULT
;
87 count
= timeout
* ZX2967_WDT_CLK_FREQ
;
88 if (count
> divisor
* ZX2967_WDT_MAX_COUNT
)
89 divisor
= DIV_ROUND_UP(count
, ZX2967_WDT_MAX_COUNT
);
90 count
= DIV_ROUND_UP(count
, divisor
);
91 zx2967_wdt_writel(wdt
, ZX2967_WDT_CFG_REG
,
92 ZX2967_WDT_CFG_DIV(divisor
) & ZX2967_WDT_VAL_MASK
);
93 zx2967_wdt_writel(wdt
, ZX2967_WDT_LOAD_REG
,
94 count
& ZX2967_WDT_VAL_MASK
);
95 zx2967_wdt_refresh(wdt
);
96 wdd
->timeout
= (count
* divisor
) / ZX2967_WDT_CLK_FREQ
;
101 static void __zx2967_wdt_start(struct zx2967_wdt
*wdt
)
105 val
= zx2967_wdt_readl(wdt
, ZX2967_WDT_START_REG
);
106 val
|= ZX2967_WDT_START_EN
;
107 zx2967_wdt_writel(wdt
, ZX2967_WDT_START_REG
,
108 val
& ZX2967_WDT_VAL_MASK
);
111 static void __zx2967_wdt_stop(struct zx2967_wdt
*wdt
)
115 val
= zx2967_wdt_readl(wdt
, ZX2967_WDT_START_REG
);
116 val
&= ~ZX2967_WDT_START_EN
;
117 zx2967_wdt_writel(wdt
, ZX2967_WDT_START_REG
,
118 val
& ZX2967_WDT_VAL_MASK
);
121 static int zx2967_wdt_start(struct watchdog_device
*wdd
)
123 struct zx2967_wdt
*wdt
= watchdog_get_drvdata(wdd
);
125 zx2967_wdt_set_timeout(wdd
, wdd
->timeout
);
126 __zx2967_wdt_start(wdt
);
131 static int zx2967_wdt_stop(struct watchdog_device
*wdd
)
133 struct zx2967_wdt
*wdt
= watchdog_get_drvdata(wdd
);
135 __zx2967_wdt_stop(wdt
);
140 static int zx2967_wdt_keepalive(struct watchdog_device
*wdd
)
142 struct zx2967_wdt
*wdt
= watchdog_get_drvdata(wdd
);
144 zx2967_wdt_refresh(wdt
);
149 #define ZX2967_WDT_OPTIONS \
150 (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE)
151 static const struct watchdog_info zx2967_wdt_ident
= {
152 .options
= ZX2967_WDT_OPTIONS
,
153 .identity
= "zx2967 watchdog",
156 static const struct watchdog_ops zx2967_wdt_ops
= {
157 .owner
= THIS_MODULE
,
158 .start
= zx2967_wdt_start
,
159 .stop
= zx2967_wdt_stop
,
160 .ping
= zx2967_wdt_keepalive
,
161 .set_timeout
= zx2967_wdt_set_timeout
,
164 static void zx2967_wdt_reset_sysctrl(struct device
*dev
)
167 void __iomem
*regmap
;
168 unsigned int offset
, mask
, config
;
169 struct of_phandle_args out_args
;
171 ret
= of_parse_phandle_with_fixed_args(dev
->of_node
,
172 "zte,wdt-reset-sysctrl", 3, 0, &out_args
);
176 offset
= out_args
.args
[0];
177 config
= out_args
.args
[1];
178 mask
= out_args
.args
[2];
180 regmap
= syscon_node_to_regmap(out_args
.np
);
181 if (IS_ERR(regmap
)) {
182 of_node_put(out_args
.np
);
186 regmap_update_bits(regmap
, offset
, mask
, config
);
187 of_node_put(out_args
.np
);
190 static void zx2967_clk_disable_unprepare(void *data
)
192 clk_disable_unprepare(data
);
195 static int zx2967_wdt_probe(struct platform_device
*pdev
)
197 struct device
*dev
= &pdev
->dev
;
198 struct zx2967_wdt
*wdt
;
200 struct reset_control
*rstc
;
202 wdt
= devm_kzalloc(dev
, sizeof(*wdt
), GFP_KERNEL
);
206 platform_set_drvdata(pdev
, wdt
);
208 wdt
->wdt_device
.info
= &zx2967_wdt_ident
;
209 wdt
->wdt_device
.ops
= &zx2967_wdt_ops
;
210 wdt
->wdt_device
.timeout
= ZX2967_WDT_DEFAULT_TIMEOUT
;
211 wdt
->wdt_device
.max_timeout
= ZX2967_WDT_MAX_TIMEOUT
;
212 wdt
->wdt_device
.min_timeout
= ZX2967_WDT_MIN_TIMEOUT
;
213 wdt
->wdt_device
.parent
= dev
;
215 wdt
->reg_base
= devm_platform_ioremap_resource(pdev
, 0);
216 if (IS_ERR(wdt
->reg_base
))
217 return PTR_ERR(wdt
->reg_base
);
219 zx2967_wdt_reset_sysctrl(dev
);
221 wdt
->clock
= devm_clk_get(dev
, NULL
);
222 if (IS_ERR(wdt
->clock
)) {
223 dev_err(dev
, "failed to find watchdog clock source\n");
224 return PTR_ERR(wdt
->clock
);
227 ret
= clk_prepare_enable(wdt
->clock
);
229 dev_err(dev
, "failed to enable clock\n");
232 ret
= devm_add_action_or_reset(dev
, zx2967_clk_disable_unprepare
,
236 clk_set_rate(wdt
->clock
, ZX2967_WDT_CLK_FREQ
);
238 rstc
= devm_reset_control_get_exclusive(dev
, NULL
);
240 dev_err(dev
, "failed to get rstc");
241 return PTR_ERR(rstc
);
244 reset_control_assert(rstc
);
245 reset_control_deassert(rstc
);
247 watchdog_set_drvdata(&wdt
->wdt_device
, wdt
);
248 watchdog_init_timeout(&wdt
->wdt_device
,
249 ZX2967_WDT_DEFAULT_TIMEOUT
, dev
);
250 watchdog_set_nowayout(&wdt
->wdt_device
, WATCHDOG_NOWAYOUT
);
252 ret
= devm_watchdog_register_device(dev
, &wdt
->wdt_device
);
256 dev_info(dev
, "watchdog enabled (timeout=%d sec, nowayout=%d)",
257 wdt
->wdt_device
.timeout
, WATCHDOG_NOWAYOUT
);
262 static const struct of_device_id zx2967_wdt_match
[] = {
263 { .compatible
= "zte,zx296718-wdt", },
266 MODULE_DEVICE_TABLE(of
, zx2967_wdt_match
);
268 static struct platform_driver zx2967_wdt_driver
= {
269 .probe
= zx2967_wdt_probe
,
271 .name
= "zx2967-wdt",
272 .of_match_table
= of_match_ptr(zx2967_wdt_match
),
275 module_platform_driver(zx2967_wdt_driver
);
277 MODULE_AUTHOR("Baoyou Xie <baoyou.xie@linaro.org>");
278 MODULE_DESCRIPTION("ZTE zx2967 Watchdog Device Driver");
279 MODULE_LICENSE("GPL v2");