blob: 0beabf238d4f41eb289b49fa60b61198611cd35f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundtb1fa8882010-05-24 10:55:59 +09002 * drivers/watchdog/shwdt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Watchdog driver for integrated watchdog in the SuperH processors.
5 *
Paul Mundt40968122012-05-10 14:21:15 +09006 * Copyright (C) 2001 - 2012 Paul Mundt <lethal@linux-sh.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
14 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
15 *
16 * 19-Apr-2002 Rob Radez <rob@osinvestor.com>
17 * Added expect close support, made emulated timeout runtime changeable
18 * general cleanups, add some ioctls
19 */
Joe Perches27c766a2012-02-15 15:06:19 -080020
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/moduleparam.h>
Paul Mundt8f5585e2010-05-25 18:31:12 +090025#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/miscdevice.h>
29#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/fs.h>
Paul Mundtf1184202006-09-27 17:53:59 +090031#include <linux/mm.h>
Paul Mundt8f5585e2010-05-25 18:31:12 +090032#include <linux/slab.h>
Alan Cox70b814e2008-05-19 14:08:55 +010033#include <linux/io.h>
Adrian Bunk58cf4192008-08-08 18:39:11 +030034#include <asm/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Paul Mundt8f5585e2010-05-25 18:31:12 +090036#define DRV_NAME "sh-wdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Default clock division ratio is 5.25 msecs. For an additional table of
40 * values, consult the asm-sh/watchdog.h. Overload this at module load
41 * time.
42 *
43 * In order for this to work reliably we need to have HZ set to 1000 or
44 * something quite higher than 100 (or we need a proper high-res timer
45 * implementation that will deal with this properly), otherwise the 10ms
46 * resolution of a jiffy is enough to trigger the overflow. For things like
47 * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
48 * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
49 * necssary.
50 *
51 * As a result of this timing problem, the only modes that are particularly
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * overflow periods respectively.
54 *
55 * Also, since we can't really expect userspace to be responsive enough
Joe Perchesee0fc092008-02-03 17:32:52 +020056 * before the overflow happens, we maintain two separate timers .. One in
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * the kernel for clearing out WOVF every 2ms or so (again, this depends on
58 * HZ == 1000), and another for monitoring userspace writes to the WDT device.
59 *
60 * As such, we currently use a configurable heartbeat interval which defaults
61 * to 30s. In this case, the userspace daemon is only responsible for periodic
62 * writes to the device before the next heartbeat is scheduled. If the daemon
63 * misses its deadline, the kernel timer will allow the WDT to overflow.
64 */
65static int clock_division_ratio = WTCSR_CKS_4096;
David Engrafbea19062011-07-20 15:03:39 +020066#define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Alan Cox70b814e2008-05-19 14:08:55 +010068static DEFINE_SPINLOCK(shwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
71static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010072static bool nowayout = WATCHDOG_NOWAYOUT;
Paul Mundt8f5585e2010-05-25 18:31:12 +090073static unsigned long next_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Paul Mundt8f5585e2010-05-25 18:31:12 +090075struct sh_wdt {
76 void __iomem *base;
77 struct device *dev;
78
79 struct timer_list timer;
80
81 unsigned long enabled;
82 char expect_close;
83};
84
Paul Mundt1950f492012-05-10 15:07:53 +090085static int sh_wdt_start(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Paul Mundt1950f492012-05-10 15:07:53 +090087 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +010088 unsigned long flags;
Paul Mundt8f5585e2010-05-25 18:31:12 +090089 u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +010090
Adrian Bunk58cf4192008-08-08 18:39:11 +030091 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundt8f5585e2010-05-25 18:31:12 +090094 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 csr = sh_wdt_read_csr();
97 csr |= WTCSR_WT | clock_division_ratio;
98 sh_wdt_write_csr(csr);
99
100 sh_wdt_write_cnt(0);
101
102 /*
103 * These processors have a bit of an inconsistent initialization
104 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
105 * RSTCSR register was removed.
106 *
107 * On the SH-2 however, in addition with bits being in different
108 * locations, we must deal with RSTCSR outright..
109 */
110 csr = sh_wdt_read_csr();
111 csr |= WTCSR_TME;
112 csr &= ~WTCSR_RSTS;
113 sh_wdt_write_csr(csr);
114
115#ifdef CONFIG_CPU_SH2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 csr = sh_wdt_read_rstcsr();
117 csr &= ~RSTCSR_RSTS;
118 sh_wdt_write_rstcsr(csr);
119#endif
Adrian Bunk58cf4192008-08-08 18:39:11 +0300120 spin_unlock_irqrestore(&shwdt_lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900121
122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Paul Mundt1950f492012-05-10 15:07:53 +0900125static int sh_wdt_stop(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Paul Mundt1950f492012-05-10 15:07:53 +0900127 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100128 unsigned long flags;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900129 u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +0100130
Adrian Bunk58cf4192008-08-08 18:39:11 +0300131 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Paul Mundt8f5585e2010-05-25 18:31:12 +0900133 del_timer(&wdt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 csr = sh_wdt_read_csr();
136 csr &= ~WTCSR_TME;
137 sh_wdt_write_csr(csr);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900138
Adrian Bunk58cf4192008-08-08 18:39:11 +0300139 spin_unlock_irqrestore(&shwdt_lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900140
141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Paul Mundt1950f492012-05-10 15:07:53 +0900144static int sh_wdt_keepalive(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Alan Cox70b814e2008-05-19 14:08:55 +0100146 unsigned long flags;
147
Adrian Bunk58cf4192008-08-08 18:39:11 +0300148 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 next_heartbeat = jiffies + (heartbeat * HZ);
Adrian Bunk58cf4192008-08-08 18:39:11 +0300150 spin_unlock_irqrestore(&shwdt_lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900151
152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Paul Mundt1950f492012-05-10 15:07:53 +0900155static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Alan Cox70b814e2008-05-19 14:08:55 +0100157 unsigned long flags;
158
159 if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EINVAL;
161
Adrian Bunk58cf4192008-08-08 18:39:11 +0300162 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 heartbeat = t;
Paul Mundt1950f492012-05-10 15:07:53 +0900164 wdt_dev->timeout = t;
Adrian Bunk58cf4192008-08-08 18:39:11 +0300165 spin_unlock_irqrestore(&shwdt_lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void sh_wdt_ping(unsigned long data)
171{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900172 struct sh_wdt *wdt = (struct sh_wdt *)data;
Alan Cox70b814e2008-05-19 14:08:55 +0100173 unsigned long flags;
174
Adrian Bunk58cf4192008-08-08 18:39:11 +0300175 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (time_before(jiffies, next_heartbeat)) {
Paul Mundt8f5585e2010-05-25 18:31:12 +0900177 u8 csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 csr = sh_wdt_read_csr();
180 csr &= ~WTCSR_IOVF;
181 sh_wdt_write_csr(csr);
182
183 sh_wdt_write_cnt(0);
184
Paul Mundt8f5585e2010-05-25 18:31:12 +0900185 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900186 } else
Paul Mundt8f5585e2010-05-25 18:31:12 +0900187 dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
188 "the watchdog\n");
Adrian Bunk58cf4192008-08-08 18:39:11 +0300189 spin_unlock_irqrestore(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Alan Cox70b814e2008-05-19 14:08:55 +0100192static const struct watchdog_info sh_wdt_info = {
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900193 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
194 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .firmware_version = 1,
196 .identity = "SH WDT",
197};
198
Paul Mundt1950f492012-05-10 15:07:53 +0900199static const struct watchdog_ops sh_wdt_ops = {
200 .owner = THIS_MODULE,
201 .start = sh_wdt_start,
202 .stop = sh_wdt_stop,
203 .ping = sh_wdt_keepalive,
204 .set_timeout = sh_wdt_set_heartbeat,
205};
206
207static struct watchdog_device sh_wdt_dev = {
208 .info = &sh_wdt_info,
209 .ops = &sh_wdt_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
Paul Mundt8f5585e2010-05-25 18:31:12 +0900212static int __devinit sh_wdt_probe(struct platform_device *pdev)
213{
214 struct sh_wdt *wdt;
215 struct resource *res;
216 int rc;
217
218 /*
219 * As this driver only covers the global watchdog case, reject
220 * any attempts to register per-CPU watchdogs.
221 */
222 if (pdev->id != -1)
223 return -EINVAL;
224
225 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
226 if (unlikely(!res))
227 return -EINVAL;
228
229 if (!devm_request_mem_region(&pdev->dev, res->start,
230 resource_size(res), DRV_NAME))
231 return -EBUSY;
232
233 wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
234 if (unlikely(!wdt)) {
235 rc = -ENOMEM;
236 goto out_release;
237 }
238
239 wdt->dev = &pdev->dev;
240
241 wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
242 if (unlikely(!wdt->base)) {
243 rc = -ENXIO;
244 goto out_err;
245 }
246
Paul Mundt1950f492012-05-10 15:07:53 +0900247 rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900248 if (unlikely(rc)) {
Paul Mundt1950f492012-05-10 15:07:53 +0900249 /* Default timeout if invalid */
250 sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT);
251
252 dev_warn(&pdev->dev,
253 "heartbeat value must be 1<=x<=3600, using %d\n",
254 sh_wdt_dev.timeout);
255 }
256
257 dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n",
258 sh_wdt_dev.timeout, nowayout);
259
260 watchdog_set_nowayout(&sh_wdt_dev, nowayout);
261 watchdog_set_drvdata(&sh_wdt_dev, wdt);
262
263 rc = watchdog_register_device(&sh_wdt_dev);
264 if (unlikely(rc)) {
265 dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc);
Paul Mundt40968122012-05-10 14:21:15 +0900266 goto out_unmap;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900267 }
268
269 init_timer(&wdt->timer);
270 wdt->timer.function = sh_wdt_ping;
271 wdt->timer.data = (unsigned long)wdt;
272 wdt->timer.expires = next_ping_period(clock_division_ratio);
273
274 platform_set_drvdata(pdev, wdt);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900275
276 dev_info(&pdev->dev, "initialized.\n");
277
278 return 0;
279
Paul Mundt8f5585e2010-05-25 18:31:12 +0900280out_unmap:
281 devm_iounmap(&pdev->dev, wdt->base);
282out_err:
283 devm_kfree(&pdev->dev, wdt);
284out_release:
285 devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
286
287 return rc;
288}
289
290static int __devexit sh_wdt_remove(struct platform_device *pdev)
291{
292 struct sh_wdt *wdt = platform_get_drvdata(pdev);
293 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
294
295 platform_set_drvdata(pdev, NULL);
296
Paul Mundt1950f492012-05-10 15:07:53 +0900297 watchdog_unregister_device(&sh_wdt_dev);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900298
Paul Mundt8f5585e2010-05-25 18:31:12 +0900299 devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
300 devm_iounmap(&pdev->dev, wdt->base);
301 devm_kfree(&pdev->dev, wdt);
302
303 return 0;
304}
305
Paul Mundt40968122012-05-10 14:21:15 +0900306static void sh_wdt_shutdown(struct platform_device *pdev)
307{
Paul Mundt1950f492012-05-10 15:07:53 +0900308 sh_wdt_stop(&sh_wdt_dev);
Paul Mundt40968122012-05-10 14:21:15 +0900309}
310
Paul Mundt8f5585e2010-05-25 18:31:12 +0900311static struct platform_driver sh_wdt_driver = {
312 .driver = {
313 .name = DRV_NAME,
314 .owner = THIS_MODULE,
315 },
316
Paul Mundt40968122012-05-10 14:21:15 +0900317 .probe = sh_wdt_probe,
318 .remove = __devexit_p(sh_wdt_remove),
319 .shutdown = sh_wdt_shutdown,
Paul Mundt8f5585e2010-05-25 18:31:12 +0900320};
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int __init sh_wdt_init(void)
323{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900324 if (unlikely(clock_division_ratio < 0x5 ||
325 clock_division_ratio > 0x7)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 clock_division_ratio = WTCSR_CKS_4096;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900327
Joe Perches27c766a2012-02-15 15:06:19 -0800328 pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
329 clock_division_ratio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Paul Mundt8f5585e2010-05-25 18:31:12 +0900332 return platform_driver_register(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static void __exit sh_wdt_exit(void)
336{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900337 platform_driver_unregister(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
Paul Mundt8f5585e2010-05-25 18:31:12 +0900339module_init(sh_wdt_init);
340module_exit(sh_wdt_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
343MODULE_DESCRIPTION("SuperH watchdog driver");
344MODULE_LICENSE("GPL");
Paul Mundt8f5585e2010-05-25 18:31:12 +0900345MODULE_ALIAS("platform:" DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
347
348module_param(clock_division_ratio, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000349MODULE_PARM_DESC(clock_division_ratio,
350 "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
Randy Dunlap76550d32010-05-01 09:46:15 -0700351 "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353module_param(heartbeat, int, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100354MODULE_PARM_DESC(heartbeat,
355 "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
356 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100358module_param(nowayout, bool, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100359MODULE_PARM_DESC(nowayout,
360 "Watchdog cannot be stopped once started (default="
361 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");