blob: 380ada4e5d661d065fb355b269175d501843c1b1 [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>
Paul Mundtf9fb3602012-05-10 15:15:08 +090028#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/miscdevice.h>
30#include <linux/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/fs.h>
Paul Mundtf1184202006-09-27 17:53:59 +090032#include <linux/mm.h>
Paul Mundt8f5585e2010-05-25 18:31:12 +090033#include <linux/slab.h>
Alan Cox70b814e2008-05-19 14:08:55 +010034#include <linux/io.h>
Paul Mundt9ea64042012-05-10 15:46:48 +090035#include <linux/clk.h>
Adrian Bunk58cf4192008-08-08 18:39:11 +030036#include <asm/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Paul Mundt8f5585e2010-05-25 18:31:12 +090038#define DRV_NAME "sh-wdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Default clock division ratio is 5.25 msecs. For an additional table of
42 * values, consult the asm-sh/watchdog.h. Overload this at module load
43 * time.
44 *
45 * In order for this to work reliably we need to have HZ set to 1000 or
46 * something quite higher than 100 (or we need a proper high-res timer
47 * implementation that will deal with this properly), otherwise the 10ms
48 * resolution of a jiffy is enough to trigger the overflow. For things like
49 * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
50 * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
51 * necssary.
52 *
53 * As a result of this timing problem, the only modes that are particularly
Lucas De Marchi25985ed2011-03-30 22:57:33 -030054 * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * overflow periods respectively.
56 *
57 * Also, since we can't really expect userspace to be responsive enough
Joe Perchesee0fc092008-02-03 17:32:52 +020058 * before the overflow happens, we maintain two separate timers .. One in
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * the kernel for clearing out WOVF every 2ms or so (again, this depends on
60 * HZ == 1000), and another for monitoring userspace writes to the WDT device.
61 *
62 * As such, we currently use a configurable heartbeat interval which defaults
63 * to 30s. In this case, the userspace daemon is only responsible for periodic
64 * writes to the device before the next heartbeat is scheduled. If the daemon
65 * misses its deadline, the kernel timer will allow the WDT to overflow.
66 */
67static int clock_division_ratio = WTCSR_CKS_4096;
David Engrafbea19062011-07-20 15:03:39 +020068#define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#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;
Paul Mundt9ea64042012-05-10 15:46:48 +090078 struct clk *clk;
Paul Mundtf9fb3602012-05-10 15:15:08 +090079 spinlock_t lock;
Paul Mundt8f5585e2010-05-25 18:31:12 +090080
81 struct timer_list timer;
Paul Mundt8f5585e2010-05-25 18:31:12 +090082};
83
Paul Mundt1950f492012-05-10 15:07:53 +090084static int sh_wdt_start(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Paul Mundt1950f492012-05-10 15:07:53 +090086 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +010087 unsigned long flags;
Paul Mundt8f5585e2010-05-25 18:31:12 +090088 u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +010089
Paul Mundtf9fb3602012-05-10 15:15:08 +090090 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundt8f5585e2010-05-25 18:31:12 +090093 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 csr = sh_wdt_read_csr();
96 csr |= WTCSR_WT | clock_division_ratio;
97 sh_wdt_write_csr(csr);
98
99 sh_wdt_write_cnt(0);
100
101 /*
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.
105 *
106 * On the SH-2 however, in addition with bits being in different
107 * locations, we must deal with RSTCSR outright..
108 */
109 csr = sh_wdt_read_csr();
110 csr |= WTCSR_TME;
111 csr &= ~WTCSR_RSTS;
112 sh_wdt_write_csr(csr);
113
114#ifdef CONFIG_CPU_SH2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 csr = sh_wdt_read_rstcsr();
116 csr &= ~RSTCSR_RSTS;
117 sh_wdt_write_rstcsr(csr);
118#endif
Paul Mundtf9fb3602012-05-10 15:15:08 +0900119 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900120
121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Paul Mundt1950f492012-05-10 15:07:53 +0900124static int sh_wdt_stop(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Paul Mundt1950f492012-05-10 15:07:53 +0900126 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100127 unsigned long flags;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900128 u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +0100129
Paul Mundtf9fb3602012-05-10 15:15:08 +0900130 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Paul Mundt8f5585e2010-05-25 18:31:12 +0900132 del_timer(&wdt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 csr = sh_wdt_read_csr();
135 csr &= ~WTCSR_TME;
136 sh_wdt_write_csr(csr);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900137
Paul Mundtf9fb3602012-05-10 15:15:08 +0900138 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900139
140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Paul Mundt1950f492012-05-10 15:07:53 +0900143static int sh_wdt_keepalive(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900145 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100146 unsigned long flags;
147
Paul Mundtf9fb3602012-05-10 15:15:08 +0900148 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundtf9fb3602012-05-10 15:15:08 +0900150 spin_unlock_irqrestore(&wdt->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{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900157 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100158 unsigned long flags;
159
160 if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -EINVAL;
162
Paul Mundtf9fb3602012-05-10 15:15:08 +0900163 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 heartbeat = t;
Paul Mundt1950f492012-05-10 15:07:53 +0900165 wdt_dev->timeout = t;
Paul Mundtf9fb3602012-05-10 15:15:08 +0900166 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return 0;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static void sh_wdt_ping(unsigned long data)
172{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900173 struct sh_wdt *wdt = (struct sh_wdt *)data;
Alan Cox70b814e2008-05-19 14:08:55 +0100174 unsigned long flags;
175
Paul Mundtf9fb3602012-05-10 15:15:08 +0900176 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (time_before(jiffies, next_heartbeat)) {
Paul Mundt8f5585e2010-05-25 18:31:12 +0900178 u8 csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 csr = sh_wdt_read_csr();
181 csr &= ~WTCSR_IOVF;
182 sh_wdt_write_csr(csr);
183
184 sh_wdt_write_cnt(0);
185
Paul Mundt8f5585e2010-05-25 18:31:12 +0900186 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900187 } else
Paul Mundt8f5585e2010-05-25 18:31:12 +0900188 dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
189 "the watchdog\n");
Paul Mundtf9fb3602012-05-10 15:15:08 +0900190 spin_unlock_irqrestore(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Alan Cox70b814e2008-05-19 14:08:55 +0100193static const struct watchdog_info sh_wdt_info = {
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900194 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
195 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .firmware_version = 1,
197 .identity = "SH WDT",
198};
199
Paul Mundt1950f492012-05-10 15:07:53 +0900200static const struct watchdog_ops sh_wdt_ops = {
201 .owner = THIS_MODULE,
202 .start = sh_wdt_start,
203 .stop = sh_wdt_stop,
204 .ping = sh_wdt_keepalive,
205 .set_timeout = sh_wdt_set_heartbeat,
206};
207
208static struct watchdog_device sh_wdt_dev = {
209 .info = &sh_wdt_info,
210 .ops = &sh_wdt_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
Paul Mundt8f5585e2010-05-25 18:31:12 +0900213static int __devinit sh_wdt_probe(struct platform_device *pdev)
214{
215 struct sh_wdt *wdt;
216 struct resource *res;
217 int rc;
218
219 /*
220 * As this driver only covers the global watchdog case, reject
221 * any attempts to register per-CPU watchdogs.
222 */
223 if (pdev->id != -1)
224 return -EINVAL;
225
226 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
227 if (unlikely(!res))
228 return -EINVAL;
229
Paul Mundt8f5585e2010-05-25 18:31:12 +0900230 wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
Paul Mundt9ea64042012-05-10 15:46:48 +0900231 if (unlikely(!wdt))
232 return -ENOMEM;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900233
234 wdt->dev = &pdev->dev;
235
Paul Mundt9ea64042012-05-10 15:46:48 +0900236 wdt->clk = clk_get(&pdev->dev, NULL);
237 if (IS_ERR(wdt->clk)) {
238 /*
239 * Clock framework support is optional, continue on
240 * anyways if we don't find a matching clock.
241 */
242 wdt->clk = NULL;
243 }
244
245 clk_enable(wdt->clk);
246
247 wdt->base = devm_request_and_ioremap(wdt->dev, res);
248 if (unlikely(!wdt->base)) {
249 rc = -EADDRNOTAVAIL;
250 goto out_disable;
251 }
252
Paul Mundtf9fb3602012-05-10 15:15:08 +0900253 watchdog_set_nowayout(&sh_wdt_dev, nowayout);
254 watchdog_set_drvdata(&sh_wdt_dev, wdt);
255
Paul Mundtf9fb3602012-05-10 15:15:08 +0900256 spin_lock_init(&wdt->lock);
257
Paul Mundt1950f492012-05-10 15:07:53 +0900258 rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900259 if (unlikely(rc)) {
Paul Mundt1950f492012-05-10 15:07:53 +0900260 /* Default timeout if invalid */
261 sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT);
262
263 dev_warn(&pdev->dev,
264 "heartbeat value must be 1<=x<=3600, using %d\n",
265 sh_wdt_dev.timeout);
266 }
267
268 dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n",
269 sh_wdt_dev.timeout, nowayout);
270
Paul Mundt1950f492012-05-10 15:07:53 +0900271 rc = watchdog_register_device(&sh_wdt_dev);
272 if (unlikely(rc)) {
273 dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc);
Paul Mundt9ea64042012-05-10 15:46:48 +0900274 goto out_disable;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900275 }
276
277 init_timer(&wdt->timer);
278 wdt->timer.function = sh_wdt_ping;
279 wdt->timer.data = (unsigned long)wdt;
280 wdt->timer.expires = next_ping_period(clock_division_ratio);
281
282 platform_set_drvdata(pdev, wdt);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900283
284 dev_info(&pdev->dev, "initialized.\n");
285
286 return 0;
287
Paul Mundt9ea64042012-05-10 15:46:48 +0900288out_disable:
289 clk_disable(wdt->clk);
290 clk_put(wdt->clk);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900291
292 return rc;
293}
294
295static int __devexit sh_wdt_remove(struct platform_device *pdev)
296{
297 struct sh_wdt *wdt = platform_get_drvdata(pdev);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900298
299 platform_set_drvdata(pdev, NULL);
300
Paul Mundt1950f492012-05-10 15:07:53 +0900301 watchdog_unregister_device(&sh_wdt_dev);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900302
Paul Mundt9ea64042012-05-10 15:46:48 +0900303 clk_disable(wdt->clk);
304 clk_put(wdt->clk);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900305
306 return 0;
307}
308
Paul Mundt40968122012-05-10 14:21:15 +0900309static void sh_wdt_shutdown(struct platform_device *pdev)
310{
Paul Mundt1950f492012-05-10 15:07:53 +0900311 sh_wdt_stop(&sh_wdt_dev);
Paul Mundt40968122012-05-10 14:21:15 +0900312}
313
Paul Mundt8f5585e2010-05-25 18:31:12 +0900314static struct platform_driver sh_wdt_driver = {
315 .driver = {
316 .name = DRV_NAME,
317 .owner = THIS_MODULE,
318 },
319
Paul Mundt40968122012-05-10 14:21:15 +0900320 .probe = sh_wdt_probe,
321 .remove = __devexit_p(sh_wdt_remove),
322 .shutdown = sh_wdt_shutdown,
Paul Mundt8f5585e2010-05-25 18:31:12 +0900323};
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325static int __init sh_wdt_init(void)
326{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900327 if (unlikely(clock_division_ratio < 0x5 ||
328 clock_division_ratio > 0x7)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 clock_division_ratio = WTCSR_CKS_4096;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900330
Joe Perches27c766a2012-02-15 15:06:19 -0800331 pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
332 clock_division_ratio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
Paul Mundt8f5585e2010-05-25 18:31:12 +0900335 return platform_driver_register(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static void __exit sh_wdt_exit(void)
339{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900340 platform_driver_unregister(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
Paul Mundt8f5585e2010-05-25 18:31:12 +0900342module_init(sh_wdt_init);
343module_exit(sh_wdt_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
346MODULE_DESCRIPTION("SuperH watchdog driver");
347MODULE_LICENSE("GPL");
Paul Mundt8f5585e2010-05-25 18:31:12 +0900348MODULE_ALIAS("platform:" DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
350
351module_param(clock_division_ratio, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000352MODULE_PARM_DESC(clock_division_ratio,
353 "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
Randy Dunlap76550d32010-05-01 09:46:15 -0700354 "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356module_param(heartbeat, int, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100357MODULE_PARM_DESC(heartbeat,
358 "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
359 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100361module_param(nowayout, bool, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100362MODULE_PARM_DESC(nowayout,
363 "Watchdog cannot be stopped once started (default="
364 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");