1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * drivers/watchdog/shwdt.c
5 * Watchdog driver for integrated watchdog in the SuperH processors.
7 * Copyright (C) 2001 - 2012 Paul Mundt <lethal@linux-sh.org>
9 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
10 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
12 * 19-Apr-2002 Rob Radez <rob@osinvestor.com>
13 * Added expect close support, made emulated timeout runtime changeable
14 * general cleanups, add some ioctls
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/platform_device.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/spinlock.h>
25 #include <linux/watchdog.h>
26 #include <linux/pm_runtime.h>
29 #include <linux/slab.h>
31 #include <linux/clk.h>
32 #include <linux/err.h>
33 #include <asm/watchdog.h>
35 #define DRV_NAME "sh-wdt"
38 * Default clock division ratio is 5.25 msecs. For an additional table of
39 * values, consult the asm-sh/watchdog.h. Overload this at module load
42 * In order for this to work reliably we need to have HZ set to 1000 or
43 * something quite higher than 100 (or we need a proper high-res timer
44 * implementation that will deal with this properly), otherwise the 10ms
45 * resolution of a jiffy is enough to trigger the overflow. For things like
46 * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
47 * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
50 * As a result of this timing problem, the only modes that are particularly
51 * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
52 * overflow periods respectively.
54 * Also, since we can't really expect userspace to be responsive enough
55 * before the overflow happens, we maintain two separate timers .. One in
56 * the kernel for clearing out WOVF every 2ms or so (again, this depends on
57 * HZ == 1000), and another for monitoring userspace writes to the WDT device.
59 * As such, we currently use a configurable heartbeat interval which defaults
60 * to 30s. In this case, the userspace daemon is only responsible for periodic
61 * writes to the device before the next heartbeat is scheduled. If the daemon
62 * misses its deadline, the kernel timer will allow the WDT to overflow.
64 static int clock_division_ratio
= WTCSR_CKS_4096
;
65 #define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
67 #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
68 static int heartbeat
= WATCHDOG_HEARTBEAT
; /* in seconds */
69 static bool nowayout
= WATCHDOG_NOWAYOUT
;
70 static unsigned long next_heartbeat
;
78 struct timer_list timer
;
81 static int sh_wdt_start(struct watchdog_device
*wdt_dev
)
83 struct sh_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
87 pm_runtime_get_sync(wdt
->dev
);
90 spin_lock_irqsave(&wdt
->lock
, flags
);
92 next_heartbeat
= jiffies
+ (heartbeat
* HZ
);
93 mod_timer(&wdt
->timer
, next_ping_period(clock_division_ratio
));
95 csr
= sh_wdt_read_csr();
96 csr
|= WTCSR_WT
| clock_division_ratio
;
97 sh_wdt_write_csr(csr
);
102 * These processors have a bit of an inconsistent initialization
103 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
104 * RSTCSR register was removed.
106 * On the SH-2 however, in addition with bits being in different
107 * locations, we must deal with RSTCSR outright..
109 csr
= sh_wdt_read_csr();
112 sh_wdt_write_csr(csr
);
114 #ifdef CONFIG_CPU_SH2
115 csr
= sh_wdt_read_rstcsr();
117 sh_wdt_write_rstcsr(csr
);
119 spin_unlock_irqrestore(&wdt
->lock
, flags
);
124 static int sh_wdt_stop(struct watchdog_device
*wdt_dev
)
126 struct sh_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
130 spin_lock_irqsave(&wdt
->lock
, flags
);
132 del_timer(&wdt
->timer
);
134 csr
= sh_wdt_read_csr();
136 sh_wdt_write_csr(csr
);
138 spin_unlock_irqrestore(&wdt
->lock
, flags
);
140 clk_disable(wdt
->clk
);
141 pm_runtime_put_sync(wdt
->dev
);
146 static int sh_wdt_keepalive(struct watchdog_device
*wdt_dev
)
148 struct sh_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
151 spin_lock_irqsave(&wdt
->lock
, flags
);
152 next_heartbeat
= jiffies
+ (heartbeat
* HZ
);
153 spin_unlock_irqrestore(&wdt
->lock
, flags
);
158 static int sh_wdt_set_heartbeat(struct watchdog_device
*wdt_dev
, unsigned t
)
160 struct sh_wdt
*wdt
= watchdog_get_drvdata(wdt_dev
);
163 if (unlikely(t
< 1 || t
> 3600)) /* arbitrary upper limit */
166 spin_lock_irqsave(&wdt
->lock
, flags
);
168 wdt_dev
->timeout
= t
;
169 spin_unlock_irqrestore(&wdt
->lock
, flags
);
174 static void sh_wdt_ping(struct timer_list
*t
)
176 struct sh_wdt
*wdt
= from_timer(wdt
, t
, timer
);
179 spin_lock_irqsave(&wdt
->lock
, flags
);
180 if (time_before(jiffies
, next_heartbeat
)) {
183 csr
= sh_wdt_read_csr();
185 sh_wdt_write_csr(csr
);
189 mod_timer(&wdt
->timer
, next_ping_period(clock_division_ratio
));
191 dev_warn(wdt
->dev
, "Heartbeat lost! Will not ping "
193 spin_unlock_irqrestore(&wdt
->lock
, flags
);
196 static const struct watchdog_info sh_wdt_info
= {
197 .options
= WDIOF_KEEPALIVEPING
| WDIOF_SETTIMEOUT
|
199 .firmware_version
= 1,
200 .identity
= "SH WDT",
203 static const struct watchdog_ops sh_wdt_ops
= {
204 .owner
= THIS_MODULE
,
205 .start
= sh_wdt_start
,
207 .ping
= sh_wdt_keepalive
,
208 .set_timeout
= sh_wdt_set_heartbeat
,
211 static struct watchdog_device sh_wdt_dev
= {
212 .info
= &sh_wdt_info
,
216 static int sh_wdt_probe(struct platform_device
*pdev
)
222 * As this driver only covers the global watchdog case, reject
223 * any attempts to register per-CPU watchdogs.
228 wdt
= devm_kzalloc(&pdev
->dev
, sizeof(struct sh_wdt
), GFP_KERNEL
);
232 wdt
->dev
= &pdev
->dev
;
234 wdt
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
235 if (IS_ERR(wdt
->clk
)) {
237 * Clock framework support is optional, continue on
238 * anyways if we don't find a matching clock.
243 wdt
->base
= devm_platform_ioremap_resource(pdev
, 0);
244 if (IS_ERR(wdt
->base
))
245 return PTR_ERR(wdt
->base
);
247 watchdog_set_nowayout(&sh_wdt_dev
, nowayout
);
248 watchdog_set_drvdata(&sh_wdt_dev
, wdt
);
249 sh_wdt_dev
.parent
= &pdev
->dev
;
251 spin_lock_init(&wdt
->lock
);
253 rc
= sh_wdt_set_heartbeat(&sh_wdt_dev
, heartbeat
);
255 /* Default timeout if invalid */
256 sh_wdt_set_heartbeat(&sh_wdt_dev
, WATCHDOG_HEARTBEAT
);
259 "heartbeat value must be 1<=x<=3600, using %d\n",
263 dev_info(&pdev
->dev
, "configured with heartbeat=%d sec (nowayout=%d)\n",
264 sh_wdt_dev
.timeout
, nowayout
);
266 rc
= watchdog_register_device(&sh_wdt_dev
);
268 dev_err(&pdev
->dev
, "Can't register watchdog (err=%d)\n", rc
);
272 timer_setup(&wdt
->timer
, sh_wdt_ping
, 0);
273 wdt
->timer
.expires
= next_ping_period(clock_division_ratio
);
275 dev_info(&pdev
->dev
, "initialized.\n");
277 pm_runtime_enable(&pdev
->dev
);
282 static int sh_wdt_remove(struct platform_device
*pdev
)
284 watchdog_unregister_device(&sh_wdt_dev
);
286 pm_runtime_disable(&pdev
->dev
);
291 static void sh_wdt_shutdown(struct platform_device
*pdev
)
293 sh_wdt_stop(&sh_wdt_dev
);
296 static struct platform_driver sh_wdt_driver
= {
301 .probe
= sh_wdt_probe
,
302 .remove
= sh_wdt_remove
,
303 .shutdown
= sh_wdt_shutdown
,
306 static int __init
sh_wdt_init(void)
308 if (unlikely(clock_division_ratio
< 0x5 ||
309 clock_division_ratio
> 0x7)) {
310 clock_division_ratio
= WTCSR_CKS_4096
;
312 pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
313 clock_division_ratio
);
316 return platform_driver_register(&sh_wdt_driver
);
319 static void __exit
sh_wdt_exit(void)
321 platform_driver_unregister(&sh_wdt_driver
);
323 module_init(sh_wdt_init
);
324 module_exit(sh_wdt_exit
);
326 MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
327 MODULE_DESCRIPTION("SuperH watchdog driver");
328 MODULE_LICENSE("GPL");
329 MODULE_ALIAS("platform:" DRV_NAME
);
331 module_param(clock_division_ratio
, int, 0);
332 MODULE_PARM_DESC(clock_division_ratio
,
333 "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
334 "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096
) ")");
336 module_param(heartbeat
, int, 0);
337 MODULE_PARM_DESC(heartbeat
,
338 "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
339 __MODULE_STRING(WATCHDOG_HEARTBEAT
) ")");
341 module_param(nowayout
, bool, 0);
342 MODULE_PARM_DESC(nowayout
,
343 "Watchdog cannot be stopped once started (default="
344 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");