blob: b11104702d2377e021b270502c77b5bcc9171ebe [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>
Paul Mundt8c013d962012-05-10 16:08:35 +090031#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/fs.h>
Paul Mundtf1184202006-09-27 17:53:59 +090033#include <linux/mm.h>
Paul Mundt8f5585e2010-05-25 18:31:12 +090034#include <linux/slab.h>
Alan Cox70b814e2008-05-19 14:08:55 +010035#include <linux/io.h>
Paul Mundt9ea64042012-05-10 15:46:48 +090036#include <linux/clk.h>
Adrian Bunk58cf4192008-08-08 18:39:11 +030037#include <asm/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Paul Mundt8f5585e2010-05-25 18:31:12 +090039#define DRV_NAME "sh-wdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Default clock division ratio is 5.25 msecs. For an additional table of
43 * values, consult the asm-sh/watchdog.h. Overload this at module load
44 * time.
45 *
46 * In order for this to work reliably we need to have HZ set to 1000 or
47 * something quite higher than 100 (or we need a proper high-res timer
48 * implementation that will deal with this properly), otherwise the 10ms
49 * resolution of a jiffy is enough to trigger the overflow. For things like
50 * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
51 * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
52 * necssary.
53 *
54 * As a result of this timing problem, the only modes that are particularly
Lucas De Marchi25985ed2011-03-30 22:57:33 -030055 * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * overflow periods respectively.
57 *
58 * Also, since we can't really expect userspace to be responsive enough
Joe Perchesee0fc092008-02-03 17:32:52 +020059 * before the overflow happens, we maintain two separate timers .. One in
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * the kernel for clearing out WOVF every 2ms or so (again, this depends on
61 * HZ == 1000), and another for monitoring userspace writes to the WDT device.
62 *
63 * As such, we currently use a configurable heartbeat interval which defaults
64 * to 30s. In this case, the userspace daemon is only responsible for periodic
65 * writes to the device before the next heartbeat is scheduled. If the daemon
66 * misses its deadline, the kernel timer will allow the WDT to overflow.
67 */
68static int clock_division_ratio = WTCSR_CKS_4096;
David Engrafbea19062011-07-20 15:03:39 +020069#define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
72static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010073static bool nowayout = WATCHDOG_NOWAYOUT;
Paul Mundt8f5585e2010-05-25 18:31:12 +090074static unsigned long next_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Paul Mundt8f5585e2010-05-25 18:31:12 +090076struct sh_wdt {
77 void __iomem *base;
78 struct device *dev;
Paul Mundt9ea64042012-05-10 15:46:48 +090079 struct clk *clk;
Paul Mundtf9fb3602012-05-10 15:15:08 +090080 spinlock_t lock;
Paul Mundt8f5585e2010-05-25 18:31:12 +090081
82 struct timer_list timer;
Paul Mundt8f5585e2010-05-25 18:31:12 +090083};
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
Paul Mundt8c013d962012-05-10 16:08:35 +090091 pm_runtime_get_sync(wdt->dev);
92
Paul Mundtf9fb3602012-05-10 15:15:08 +090093 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundt8f5585e2010-05-25 18:31:12 +090096 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 csr = sh_wdt_read_csr();
99 csr |= WTCSR_WT | clock_division_ratio;
100 sh_wdt_write_csr(csr);
101
102 sh_wdt_write_cnt(0);
103
104 /*
105 * These processors have a bit of an inconsistent initialization
106 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
107 * RSTCSR register was removed.
108 *
109 * On the SH-2 however, in addition with bits being in different
110 * locations, we must deal with RSTCSR outright..
111 */
112 csr = sh_wdt_read_csr();
113 csr |= WTCSR_TME;
114 csr &= ~WTCSR_RSTS;
115 sh_wdt_write_csr(csr);
116
117#ifdef CONFIG_CPU_SH2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 csr = sh_wdt_read_rstcsr();
119 csr &= ~RSTCSR_RSTS;
120 sh_wdt_write_rstcsr(csr);
121#endif
Paul Mundtf9fb3602012-05-10 15:15:08 +0900122 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900123
124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Paul Mundt1950f492012-05-10 15:07:53 +0900127static int sh_wdt_stop(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Paul Mundt1950f492012-05-10 15:07:53 +0900129 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100130 unsigned long flags;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900131 u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +0100132
Paul Mundtf9fb3602012-05-10 15:15:08 +0900133 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Paul Mundt8f5585e2010-05-25 18:31:12 +0900135 del_timer(&wdt->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 csr = sh_wdt_read_csr();
138 csr &= ~WTCSR_TME;
139 sh_wdt_write_csr(csr);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900140
Paul Mundtf9fb3602012-05-10 15:15:08 +0900141 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900142
Paul Mundt8c013d962012-05-10 16:08:35 +0900143 pm_runtime_put_sync(wdt->dev);
144
Paul Mundt1950f492012-05-10 15:07:53 +0900145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Paul Mundt1950f492012-05-10 15:07:53 +0900148static int sh_wdt_keepalive(struct watchdog_device *wdt_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900150 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100151 unsigned long flags;
152
Paul Mundtf9fb3602012-05-10 15:15:08 +0900153 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundtf9fb3602012-05-10 15:15:08 +0900155 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900156
157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Paul Mundt1950f492012-05-10 15:07:53 +0900160static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900162 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100163 unsigned long flags;
164
165 if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -EINVAL;
167
Paul Mundtf9fb3602012-05-10 15:15:08 +0900168 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 heartbeat = t;
Paul Mundt1950f492012-05-10 15:07:53 +0900170 wdt_dev->timeout = t;
Paul Mundtf9fb3602012-05-10 15:15:08 +0900171 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return 0;
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static void sh_wdt_ping(unsigned long data)
177{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900178 struct sh_wdt *wdt = (struct sh_wdt *)data;
Alan Cox70b814e2008-05-19 14:08:55 +0100179 unsigned long flags;
180
Paul Mundtf9fb3602012-05-10 15:15:08 +0900181 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (time_before(jiffies, next_heartbeat)) {
Paul Mundt8f5585e2010-05-25 18:31:12 +0900183 u8 csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 csr = sh_wdt_read_csr();
186 csr &= ~WTCSR_IOVF;
187 sh_wdt_write_csr(csr);
188
189 sh_wdt_write_cnt(0);
190
Paul Mundt8f5585e2010-05-25 18:31:12 +0900191 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900192 } else
Paul Mundt8f5585e2010-05-25 18:31:12 +0900193 dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
194 "the watchdog\n");
Paul Mundtf9fb3602012-05-10 15:15:08 +0900195 spin_unlock_irqrestore(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Alan Cox70b814e2008-05-19 14:08:55 +0100198static const struct watchdog_info sh_wdt_info = {
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900199 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
200 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .firmware_version = 1,
202 .identity = "SH WDT",
203};
204
Paul Mundt1950f492012-05-10 15:07:53 +0900205static const struct watchdog_ops sh_wdt_ops = {
206 .owner = THIS_MODULE,
207 .start = sh_wdt_start,
208 .stop = sh_wdt_stop,
209 .ping = sh_wdt_keepalive,
210 .set_timeout = sh_wdt_set_heartbeat,
211};
212
213static struct watchdog_device sh_wdt_dev = {
214 .info = &sh_wdt_info,
215 .ops = &sh_wdt_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
Paul Mundt8f5585e2010-05-25 18:31:12 +0900218static int __devinit sh_wdt_probe(struct platform_device *pdev)
219{
220 struct sh_wdt *wdt;
221 struct resource *res;
222 int rc;
223
224 /*
225 * As this driver only covers the global watchdog case, reject
226 * any attempts to register per-CPU watchdogs.
227 */
228 if (pdev->id != -1)
229 return -EINVAL;
230
231 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
232 if (unlikely(!res))
233 return -EINVAL;
234
Paul Mundt8f5585e2010-05-25 18:31:12 +0900235 wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
Paul Mundt9ea64042012-05-10 15:46:48 +0900236 if (unlikely(!wdt))
237 return -ENOMEM;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900238
239 wdt->dev = &pdev->dev;
240
Paul Mundt9ea64042012-05-10 15:46:48 +0900241 wdt->clk = clk_get(&pdev->dev, NULL);
242 if (IS_ERR(wdt->clk)) {
243 /*
244 * Clock framework support is optional, continue on
245 * anyways if we don't find a matching clock.
246 */
247 wdt->clk = NULL;
248 }
249
250 clk_enable(wdt->clk);
251
252 wdt->base = devm_request_and_ioremap(wdt->dev, res);
253 if (unlikely(!wdt->base)) {
254 rc = -EADDRNOTAVAIL;
255 goto out_disable;
256 }
257
Paul Mundtf9fb3602012-05-10 15:15:08 +0900258 watchdog_set_nowayout(&sh_wdt_dev, nowayout);
259 watchdog_set_drvdata(&sh_wdt_dev, wdt);
260
Paul Mundtf9fb3602012-05-10 15:15:08 +0900261 spin_lock_init(&wdt->lock);
262
Paul Mundt1950f492012-05-10 15:07:53 +0900263 rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900264 if (unlikely(rc)) {
Paul Mundt1950f492012-05-10 15:07:53 +0900265 /* Default timeout if invalid */
266 sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT);
267
268 dev_warn(&pdev->dev,
269 "heartbeat value must be 1<=x<=3600, using %d\n",
270 sh_wdt_dev.timeout);
271 }
272
273 dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n",
274 sh_wdt_dev.timeout, nowayout);
275
Paul Mundt1950f492012-05-10 15:07:53 +0900276 rc = watchdog_register_device(&sh_wdt_dev);
277 if (unlikely(rc)) {
278 dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc);
Paul Mundt9ea64042012-05-10 15:46:48 +0900279 goto out_disable;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900280 }
281
282 init_timer(&wdt->timer);
283 wdt->timer.function = sh_wdt_ping;
284 wdt->timer.data = (unsigned long)wdt;
285 wdt->timer.expires = next_ping_period(clock_division_ratio);
286
287 platform_set_drvdata(pdev, wdt);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900288
289 dev_info(&pdev->dev, "initialized.\n");
290
Paul Mundt8c013d962012-05-10 16:08:35 +0900291 pm_runtime_enable(&pdev->dev);
292
Paul Mundt8f5585e2010-05-25 18:31:12 +0900293 return 0;
294
Paul Mundt9ea64042012-05-10 15:46:48 +0900295out_disable:
296 clk_disable(wdt->clk);
297 clk_put(wdt->clk);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900298
299 return rc;
300}
301
302static int __devexit sh_wdt_remove(struct platform_device *pdev)
303{
304 struct sh_wdt *wdt = platform_get_drvdata(pdev);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900305
306 platform_set_drvdata(pdev, NULL);
307
Paul Mundt1950f492012-05-10 15:07:53 +0900308 watchdog_unregister_device(&sh_wdt_dev);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900309
Paul Mundt8c013d962012-05-10 16:08:35 +0900310 pm_runtime_disable(&pdev->dev);
Paul Mundt9ea64042012-05-10 15:46:48 +0900311 clk_disable(wdt->clk);
312 clk_put(wdt->clk);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900313
314 return 0;
315}
316
Paul Mundt40968122012-05-10 14:21:15 +0900317static void sh_wdt_shutdown(struct platform_device *pdev)
318{
Paul Mundt1950f492012-05-10 15:07:53 +0900319 sh_wdt_stop(&sh_wdt_dev);
Paul Mundt40968122012-05-10 14:21:15 +0900320}
321
Paul Mundt8f5585e2010-05-25 18:31:12 +0900322static struct platform_driver sh_wdt_driver = {
323 .driver = {
324 .name = DRV_NAME,
325 .owner = THIS_MODULE,
326 },
327
Paul Mundt40968122012-05-10 14:21:15 +0900328 .probe = sh_wdt_probe,
329 .remove = __devexit_p(sh_wdt_remove),
330 .shutdown = sh_wdt_shutdown,
Paul Mundt8f5585e2010-05-25 18:31:12 +0900331};
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static int __init sh_wdt_init(void)
334{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900335 if (unlikely(clock_division_ratio < 0x5 ||
336 clock_division_ratio > 0x7)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 clock_division_ratio = WTCSR_CKS_4096;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900338
Joe Perches27c766a2012-02-15 15:06:19 -0800339 pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
340 clock_division_ratio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Paul Mundt8f5585e2010-05-25 18:31:12 +0900343 return platform_driver_register(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static void __exit sh_wdt_exit(void)
347{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900348 platform_driver_unregister(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
Paul Mundt8f5585e2010-05-25 18:31:12 +0900350module_init(sh_wdt_init);
351module_exit(sh_wdt_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
354MODULE_DESCRIPTION("SuperH watchdog driver");
355MODULE_LICENSE("GPL");
Paul Mundt8f5585e2010-05-25 18:31:12 +0900356MODULE_ALIAS("platform:" DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
358
359module_param(clock_division_ratio, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000360MODULE_PARM_DESC(clock_division_ratio,
361 "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
Randy Dunlap76550d32010-05-01 09:46:15 -0700362 "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364module_param(heartbeat, int, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100365MODULE_PARM_DESC(heartbeat,
366 "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
367 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100369module_param(nowayout, bool, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100370MODULE_PARM_DESC(nowayout,
371 "Watchdog cannot be stopped once started (default="
372 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");