1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * drivers/watchdog/shwdt.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Watchdog driver for integrated watchdog in the SuperH processors.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2001 - 2012 Paul Mundt <lethal@linux-sh.org>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
10*4882a593Smuzhiyun * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * 19-Apr-2002 Rob Radez <rob@osinvestor.com>
13*4882a593Smuzhiyun * Added expect close support, made emulated timeout runtime changeable
14*4882a593Smuzhiyun * general cleanups, add some ioctls
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/moduleparam.h>
21*4882a593Smuzhiyun #include <linux/platform_device.h>
22*4882a593Smuzhiyun #include <linux/init.h>
23*4882a593Smuzhiyun #include <linux/types.h>
24*4882a593Smuzhiyun #include <linux/spinlock.h>
25*4882a593Smuzhiyun #include <linux/watchdog.h>
26*4882a593Smuzhiyun #include <linux/pm_runtime.h>
27*4882a593Smuzhiyun #include <linux/fs.h>
28*4882a593Smuzhiyun #include <linux/mm.h>
29*4882a593Smuzhiyun #include <linux/slab.h>
30*4882a593Smuzhiyun #include <linux/io.h>
31*4882a593Smuzhiyun #include <linux/clk.h>
32*4882a593Smuzhiyun #include <linux/err.h>
33*4882a593Smuzhiyun #include <asm/watchdog.h>
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #define DRV_NAME "sh-wdt"
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun * Default clock division ratio is 5.25 msecs. For an additional table of
39*4882a593Smuzhiyun * values, consult the asm-sh/watchdog.h. Overload this at module load
40*4882a593Smuzhiyun * time.
41*4882a593Smuzhiyun *
42*4882a593Smuzhiyun * In order for this to work reliably we need to have HZ set to 1000 or
43*4882a593Smuzhiyun * something quite higher than 100 (or we need a proper high-res timer
44*4882a593Smuzhiyun * implementation that will deal with this properly), otherwise the 10ms
45*4882a593Smuzhiyun * resolution of a jiffy is enough to trigger the overflow. For things like
46*4882a593Smuzhiyun * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
47*4882a593Smuzhiyun * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
48*4882a593Smuzhiyun * necssary.
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * As a result of this timing problem, the only modes that are particularly
51*4882a593Smuzhiyun * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
52*4882a593Smuzhiyun * overflow periods respectively.
53*4882a593Smuzhiyun *
54*4882a593Smuzhiyun * Also, since we can't really expect userspace to be responsive enough
55*4882a593Smuzhiyun * before the overflow happens, we maintain two separate timers .. One in
56*4882a593Smuzhiyun * the kernel for clearing out WOVF every 2ms or so (again, this depends on
57*4882a593Smuzhiyun * HZ == 1000), and another for monitoring userspace writes to the WDT device.
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * As such, we currently use a configurable heartbeat interval which defaults
60*4882a593Smuzhiyun * to 30s. In this case, the userspace daemon is only responsible for periodic
61*4882a593Smuzhiyun * writes to the device before the next heartbeat is scheduled. If the daemon
62*4882a593Smuzhiyun * misses its deadline, the kernel timer will allow the WDT to overflow.
63*4882a593Smuzhiyun */
64*4882a593Smuzhiyun static int clock_division_ratio = WTCSR_CKS_4096;
65*4882a593Smuzhiyun #define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
68*4882a593Smuzhiyun static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
69*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
70*4882a593Smuzhiyun static unsigned long next_heartbeat;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun struct sh_wdt {
73*4882a593Smuzhiyun void __iomem *base;
74*4882a593Smuzhiyun struct device *dev;
75*4882a593Smuzhiyun struct clk *clk;
76*4882a593Smuzhiyun spinlock_t lock;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun struct timer_list timer;
79*4882a593Smuzhiyun };
80*4882a593Smuzhiyun
sh_wdt_start(struct watchdog_device * wdt_dev)81*4882a593Smuzhiyun static int sh_wdt_start(struct watchdog_device *wdt_dev)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
84*4882a593Smuzhiyun unsigned long flags;
85*4882a593Smuzhiyun u8 csr;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun pm_runtime_get_sync(wdt->dev);
88*4882a593Smuzhiyun clk_enable(wdt->clk);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun spin_lock_irqsave(&wdt->lock, flags);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun next_heartbeat = jiffies + (heartbeat * HZ);
93*4882a593Smuzhiyun mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun csr = sh_wdt_read_csr();
96*4882a593Smuzhiyun csr |= WTCSR_WT | clock_division_ratio;
97*4882a593Smuzhiyun sh_wdt_write_csr(csr);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun sh_wdt_write_cnt(0);
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun * These processors have a bit of an inconsistent initialization
103*4882a593Smuzhiyun * process.. starting with SH-3, RSTS was moved to WTCSR, and the
104*4882a593Smuzhiyun * RSTCSR register was removed.
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * On the SH-2 however, in addition with bits being in different
107*4882a593Smuzhiyun * locations, we must deal with RSTCSR outright..
108*4882a593Smuzhiyun */
109*4882a593Smuzhiyun csr = sh_wdt_read_csr();
110*4882a593Smuzhiyun csr |= WTCSR_TME;
111*4882a593Smuzhiyun csr &= ~WTCSR_RSTS;
112*4882a593Smuzhiyun sh_wdt_write_csr(csr);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun #ifdef CONFIG_CPU_SH2
115*4882a593Smuzhiyun csr = sh_wdt_read_rstcsr();
116*4882a593Smuzhiyun csr &= ~RSTCSR_RSTS;
117*4882a593Smuzhiyun sh_wdt_write_rstcsr(csr);
118*4882a593Smuzhiyun #endif
119*4882a593Smuzhiyun spin_unlock_irqrestore(&wdt->lock, flags);
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun return 0;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
sh_wdt_stop(struct watchdog_device * wdt_dev)124*4882a593Smuzhiyun static int sh_wdt_stop(struct watchdog_device *wdt_dev)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
127*4882a593Smuzhiyun unsigned long flags;
128*4882a593Smuzhiyun u8 csr;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun spin_lock_irqsave(&wdt->lock, flags);
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun del_timer(&wdt->timer);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun csr = sh_wdt_read_csr();
135*4882a593Smuzhiyun csr &= ~WTCSR_TME;
136*4882a593Smuzhiyun sh_wdt_write_csr(csr);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun spin_unlock_irqrestore(&wdt->lock, flags);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun clk_disable(wdt->clk);
141*4882a593Smuzhiyun pm_runtime_put_sync(wdt->dev);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun return 0;
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
sh_wdt_keepalive(struct watchdog_device * wdt_dev)146*4882a593Smuzhiyun static int sh_wdt_keepalive(struct watchdog_device *wdt_dev)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
149*4882a593Smuzhiyun unsigned long flags;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun spin_lock_irqsave(&wdt->lock, flags);
152*4882a593Smuzhiyun next_heartbeat = jiffies + (heartbeat * HZ);
153*4882a593Smuzhiyun spin_unlock_irqrestore(&wdt->lock, flags);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun return 0;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
sh_wdt_set_heartbeat(struct watchdog_device * wdt_dev,unsigned t)158*4882a593Smuzhiyun static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
161*4882a593Smuzhiyun unsigned long flags;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
164*4882a593Smuzhiyun return -EINVAL;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun spin_lock_irqsave(&wdt->lock, flags);
167*4882a593Smuzhiyun heartbeat = t;
168*4882a593Smuzhiyun wdt_dev->timeout = t;
169*4882a593Smuzhiyun spin_unlock_irqrestore(&wdt->lock, flags);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun return 0;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
sh_wdt_ping(struct timer_list * t)174*4882a593Smuzhiyun static void sh_wdt_ping(struct timer_list *t)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun struct sh_wdt *wdt = from_timer(wdt, t, timer);
177*4882a593Smuzhiyun unsigned long flags;
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun spin_lock_irqsave(&wdt->lock, flags);
180*4882a593Smuzhiyun if (time_before(jiffies, next_heartbeat)) {
181*4882a593Smuzhiyun u8 csr;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun csr = sh_wdt_read_csr();
184*4882a593Smuzhiyun csr &= ~WTCSR_IOVF;
185*4882a593Smuzhiyun sh_wdt_write_csr(csr);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun sh_wdt_write_cnt(0);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
190*4882a593Smuzhiyun } else
191*4882a593Smuzhiyun dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
192*4882a593Smuzhiyun "the watchdog\n");
193*4882a593Smuzhiyun spin_unlock_irqrestore(&wdt->lock, flags);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun static const struct watchdog_info sh_wdt_info = {
197*4882a593Smuzhiyun .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
198*4882a593Smuzhiyun WDIOF_MAGICCLOSE,
199*4882a593Smuzhiyun .firmware_version = 1,
200*4882a593Smuzhiyun .identity = "SH WDT",
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun static const struct watchdog_ops sh_wdt_ops = {
204*4882a593Smuzhiyun .owner = THIS_MODULE,
205*4882a593Smuzhiyun .start = sh_wdt_start,
206*4882a593Smuzhiyun .stop = sh_wdt_stop,
207*4882a593Smuzhiyun .ping = sh_wdt_keepalive,
208*4882a593Smuzhiyun .set_timeout = sh_wdt_set_heartbeat,
209*4882a593Smuzhiyun };
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun static struct watchdog_device sh_wdt_dev = {
212*4882a593Smuzhiyun .info = &sh_wdt_info,
213*4882a593Smuzhiyun .ops = &sh_wdt_ops,
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun
sh_wdt_probe(struct platform_device * pdev)216*4882a593Smuzhiyun static int sh_wdt_probe(struct platform_device *pdev)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun struct sh_wdt *wdt;
219*4882a593Smuzhiyun int rc;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * As this driver only covers the global watchdog case, reject
223*4882a593Smuzhiyun * any attempts to register per-CPU watchdogs.
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun if (pdev->id != -1)
226*4882a593Smuzhiyun return -EINVAL;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
229*4882a593Smuzhiyun if (unlikely(!wdt))
230*4882a593Smuzhiyun return -ENOMEM;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun wdt->dev = &pdev->dev;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun wdt->clk = devm_clk_get(&pdev->dev, NULL);
235*4882a593Smuzhiyun if (IS_ERR(wdt->clk)) {
236*4882a593Smuzhiyun /*
237*4882a593Smuzhiyun * Clock framework support is optional, continue on
238*4882a593Smuzhiyun * anyways if we don't find a matching clock.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun wdt->clk = NULL;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun wdt->base = devm_platform_ioremap_resource(pdev, 0);
244*4882a593Smuzhiyun if (IS_ERR(wdt->base))
245*4882a593Smuzhiyun return PTR_ERR(wdt->base);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun watchdog_set_nowayout(&sh_wdt_dev, nowayout);
248*4882a593Smuzhiyun watchdog_set_drvdata(&sh_wdt_dev, wdt);
249*4882a593Smuzhiyun sh_wdt_dev.parent = &pdev->dev;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun spin_lock_init(&wdt->lock);
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat);
254*4882a593Smuzhiyun if (unlikely(rc)) {
255*4882a593Smuzhiyun /* Default timeout if invalid */
256*4882a593Smuzhiyun sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT);
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun dev_warn(&pdev->dev,
259*4882a593Smuzhiyun "heartbeat value must be 1<=x<=3600, using %d\n",
260*4882a593Smuzhiyun sh_wdt_dev.timeout);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n",
264*4882a593Smuzhiyun sh_wdt_dev.timeout, nowayout);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun rc = watchdog_register_device(&sh_wdt_dev);
267*4882a593Smuzhiyun if (unlikely(rc)) {
268*4882a593Smuzhiyun dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc);
269*4882a593Smuzhiyun return rc;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun timer_setup(&wdt->timer, sh_wdt_ping, 0);
273*4882a593Smuzhiyun wdt->timer.expires = next_ping_period(clock_division_ratio);
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun dev_info(&pdev->dev, "initialized.\n");
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun pm_runtime_enable(&pdev->dev);
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun return 0;
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
sh_wdt_remove(struct platform_device * pdev)282*4882a593Smuzhiyun static int sh_wdt_remove(struct platform_device *pdev)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun watchdog_unregister_device(&sh_wdt_dev);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun pm_runtime_disable(&pdev->dev);
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun return 0;
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun
sh_wdt_shutdown(struct platform_device * pdev)291*4882a593Smuzhiyun static void sh_wdt_shutdown(struct platform_device *pdev)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun sh_wdt_stop(&sh_wdt_dev);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun static struct platform_driver sh_wdt_driver = {
297*4882a593Smuzhiyun .driver = {
298*4882a593Smuzhiyun .name = DRV_NAME,
299*4882a593Smuzhiyun },
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun .probe = sh_wdt_probe,
302*4882a593Smuzhiyun .remove = sh_wdt_remove,
303*4882a593Smuzhiyun .shutdown = sh_wdt_shutdown,
304*4882a593Smuzhiyun };
305*4882a593Smuzhiyun
sh_wdt_init(void)306*4882a593Smuzhiyun static int __init sh_wdt_init(void)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun if (unlikely(clock_division_ratio < 0x5 ||
309*4882a593Smuzhiyun clock_division_ratio > 0x7)) {
310*4882a593Smuzhiyun clock_division_ratio = WTCSR_CKS_4096;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
313*4882a593Smuzhiyun clock_division_ratio);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun return platform_driver_register(&sh_wdt_driver);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
sh_wdt_exit(void)319*4882a593Smuzhiyun static void __exit sh_wdt_exit(void)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun platform_driver_unregister(&sh_wdt_driver);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun module_init(sh_wdt_init);
324*4882a593Smuzhiyun module_exit(sh_wdt_exit);
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
327*4882a593Smuzhiyun MODULE_DESCRIPTION("SuperH watchdog driver");
328*4882a593Smuzhiyun MODULE_LICENSE("GPL");
329*4882a593Smuzhiyun MODULE_ALIAS("platform:" DRV_NAME);
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun module_param(clock_division_ratio, int, 0);
332*4882a593Smuzhiyun MODULE_PARM_DESC(clock_division_ratio,
333*4882a593Smuzhiyun "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
334*4882a593Smuzhiyun "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun module_param(heartbeat, int, 0);
337*4882a593Smuzhiyun MODULE_PARM_DESC(heartbeat,
338*4882a593Smuzhiyun "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
339*4882a593Smuzhiyun __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun module_param(nowayout, bool, 0);
342*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout,
343*4882a593Smuzhiyun "Watchdog cannot be stopped once started (default="
344*4882a593Smuzhiyun __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
345