blob: 0d734a38d29f2e941f72967a0f826b402c068a69 [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>
Adrian Bunk58cf4192008-08-08 18:39:11 +030035#include <asm/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Paul Mundt8f5585e2010-05-25 18:31:12 +090037#define DRV_NAME "sh-wdt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Default clock division ratio is 5.25 msecs. For an additional table of
41 * values, consult the asm-sh/watchdog.h. Overload this at module load
42 * time.
43 *
44 * In order for this to work reliably we need to have HZ set to 1000 or
45 * something quite higher than 100 (or we need a proper high-res timer
46 * implementation that will deal with this properly), otherwise the 10ms
47 * resolution of a jiffy is enough to trigger the overflow. For things like
48 * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
49 * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
50 * necssary.
51 *
52 * As a result of this timing problem, the only modes that are particularly
Lucas De Marchi25985ed2011-03-30 22:57:33 -030053 * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * overflow periods respectively.
55 *
56 * Also, since we can't really expect userspace to be responsive enough
Joe Perchesee0fc092008-02-03 17:32:52 +020057 * before the overflow happens, we maintain two separate timers .. One in
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * the kernel for clearing out WOVF every 2ms or so (again, this depends on
59 * HZ == 1000), and another for monitoring userspace writes to the WDT device.
60 *
61 * As such, we currently use a configurable heartbeat interval which defaults
62 * to 30s. In this case, the userspace daemon is only responsible for periodic
63 * writes to the device before the next heartbeat is scheduled. If the daemon
64 * misses its deadline, the kernel timer will allow the WDT to overflow.
65 */
66static int clock_division_ratio = WTCSR_CKS_4096;
David Engrafbea19062011-07-20 15:03:39 +020067#define next_ping_period(cks) (jiffies + msecs_to_jiffies(cks - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
70static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010071static bool nowayout = WATCHDOG_NOWAYOUT;
Paul Mundt8f5585e2010-05-25 18:31:12 +090072static unsigned long next_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paul Mundt8f5585e2010-05-25 18:31:12 +090074struct sh_wdt {
75 void __iomem *base;
76 struct device *dev;
Paul Mundtf9fb3602012-05-10 15:15:08 +090077 spinlock_t lock;
Paul Mundt8f5585e2010-05-25 18:31:12 +090078
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
Paul Mundtf9fb3602012-05-10 15:15:08 +090091 spin_lock_irqsave(&wdt->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
Paul Mundtf9fb3602012-05-10 15:15:08 +0900120 spin_unlock_irqrestore(&wdt->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
Paul Mundtf9fb3602012-05-10 15:15:08 +0900131 spin_lock_irqsave(&wdt->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
Paul Mundtf9fb3602012-05-10 15:15:08 +0900139 spin_unlock_irqrestore(&wdt->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{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900146 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100147 unsigned long flags;
148
Paul Mundtf9fb3602012-05-10 15:15:08 +0900149 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 next_heartbeat = jiffies + (heartbeat * HZ);
Paul Mundtf9fb3602012-05-10 15:15:08 +0900151 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900152
153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Paul Mundt1950f492012-05-10 15:07:53 +0900156static int sh_wdt_set_heartbeat(struct watchdog_device *wdt_dev, unsigned t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Paul Mundtf9fb3602012-05-10 15:15:08 +0900158 struct sh_wdt *wdt = watchdog_get_drvdata(wdt_dev);
Alan Cox70b814e2008-05-19 14:08:55 +0100159 unsigned long flags;
160
161 if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EINVAL;
163
Paul Mundtf9fb3602012-05-10 15:15:08 +0900164 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 heartbeat = t;
Paul Mundt1950f492012-05-10 15:07:53 +0900166 wdt_dev->timeout = t;
Paul Mundtf9fb3602012-05-10 15:15:08 +0900167 spin_unlock_irqrestore(&wdt->lock, flags);
Paul Mundt1950f492012-05-10 15:07:53 +0900168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return 0;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static void sh_wdt_ping(unsigned long data)
173{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900174 struct sh_wdt *wdt = (struct sh_wdt *)data;
Alan Cox70b814e2008-05-19 14:08:55 +0100175 unsigned long flags;
176
Paul Mundtf9fb3602012-05-10 15:15:08 +0900177 spin_lock_irqsave(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (time_before(jiffies, next_heartbeat)) {
Paul Mundt8f5585e2010-05-25 18:31:12 +0900179 u8 csr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 csr = sh_wdt_read_csr();
182 csr &= ~WTCSR_IOVF;
183 sh_wdt_write_csr(csr);
184
185 sh_wdt_write_cnt(0);
186
Paul Mundt8f5585e2010-05-25 18:31:12 +0900187 mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900188 } else
Paul Mundt8f5585e2010-05-25 18:31:12 +0900189 dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
190 "the watchdog\n");
Paul Mundtf9fb3602012-05-10 15:15:08 +0900191 spin_unlock_irqrestore(&wdt->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Alan Cox70b814e2008-05-19 14:08:55 +0100194static const struct watchdog_info sh_wdt_info = {
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900195 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
196 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .firmware_version = 1,
198 .identity = "SH WDT",
199};
200
Paul Mundt1950f492012-05-10 15:07:53 +0900201static const struct watchdog_ops sh_wdt_ops = {
202 .owner = THIS_MODULE,
203 .start = sh_wdt_start,
204 .stop = sh_wdt_stop,
205 .ping = sh_wdt_keepalive,
206 .set_timeout = sh_wdt_set_heartbeat,
207};
208
209static struct watchdog_device sh_wdt_dev = {
210 .info = &sh_wdt_info,
211 .ops = &sh_wdt_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212};
213
Paul Mundt8f5585e2010-05-25 18:31:12 +0900214static int __devinit sh_wdt_probe(struct platform_device *pdev)
215{
216 struct sh_wdt *wdt;
217 struct resource *res;
218 int rc;
219
220 /*
221 * As this driver only covers the global watchdog case, reject
222 * any attempts to register per-CPU watchdogs.
223 */
224 if (pdev->id != -1)
225 return -EINVAL;
226
227 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
228 if (unlikely(!res))
229 return -EINVAL;
230
231 if (!devm_request_mem_region(&pdev->dev, res->start,
232 resource_size(res), DRV_NAME))
233 return -EBUSY;
234
235 wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
236 if (unlikely(!wdt)) {
237 rc = -ENOMEM;
238 goto out_release;
239 }
240
241 wdt->dev = &pdev->dev;
242
Paul Mundtf9fb3602012-05-10 15:15:08 +0900243 watchdog_set_nowayout(&sh_wdt_dev, nowayout);
244 watchdog_set_drvdata(&sh_wdt_dev, wdt);
245
Paul Mundt8f5585e2010-05-25 18:31:12 +0900246 wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
247 if (unlikely(!wdt->base)) {
248 rc = -ENXIO;
249 goto out_err;
250 }
251
Paul Mundtf9fb3602012-05-10 15:15:08 +0900252 spin_lock_init(&wdt->lock);
253
Paul Mundt1950f492012-05-10 15:07:53 +0900254 rc = sh_wdt_set_heartbeat(&sh_wdt_dev, heartbeat);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900255 if (unlikely(rc)) {
Paul Mundt1950f492012-05-10 15:07:53 +0900256 /* Default timeout if invalid */
257 sh_wdt_set_heartbeat(&sh_wdt_dev, WATCHDOG_HEARTBEAT);
258
259 dev_warn(&pdev->dev,
260 "heartbeat value must be 1<=x<=3600, using %d\n",
261 sh_wdt_dev.timeout);
262 }
263
264 dev_info(&pdev->dev, "configured with heartbeat=%d sec (nowayout=%d)\n",
265 sh_wdt_dev.timeout, nowayout);
266
Paul Mundt1950f492012-05-10 15:07:53 +0900267 rc = watchdog_register_device(&sh_wdt_dev);
268 if (unlikely(rc)) {
269 dev_err(&pdev->dev, "Can't register watchdog (err=%d)\n", rc);
Paul Mundt40968122012-05-10 14:21:15 +0900270 goto out_unmap;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900271 }
272
273 init_timer(&wdt->timer);
274 wdt->timer.function = sh_wdt_ping;
275 wdt->timer.data = (unsigned long)wdt;
276 wdt->timer.expires = next_ping_period(clock_division_ratio);
277
278 platform_set_drvdata(pdev, wdt);
Paul Mundt8f5585e2010-05-25 18:31:12 +0900279
280 dev_info(&pdev->dev, "initialized.\n");
281
282 return 0;
283
Paul Mundt8f5585e2010-05-25 18:31:12 +0900284out_unmap:
285 devm_iounmap(&pdev->dev, wdt->base);
286out_err:
287 devm_kfree(&pdev->dev, wdt);
288out_release:
289 devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
290
291 return rc;
292}
293
294static int __devexit sh_wdt_remove(struct platform_device *pdev)
295{
296 struct sh_wdt *wdt = platform_get_drvdata(pdev);
297 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
298
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 Mundt8f5585e2010-05-25 18:31:12 +0900303 devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
304 devm_iounmap(&pdev->dev, wdt->base);
305 devm_kfree(&pdev->dev, wdt);
306
307 return 0;
308}
309
Paul Mundt40968122012-05-10 14:21:15 +0900310static void sh_wdt_shutdown(struct platform_device *pdev)
311{
Paul Mundt1950f492012-05-10 15:07:53 +0900312 sh_wdt_stop(&sh_wdt_dev);
Paul Mundt40968122012-05-10 14:21:15 +0900313}
314
Paul Mundt8f5585e2010-05-25 18:31:12 +0900315static struct platform_driver sh_wdt_driver = {
316 .driver = {
317 .name = DRV_NAME,
318 .owner = THIS_MODULE,
319 },
320
Paul Mundt40968122012-05-10 14:21:15 +0900321 .probe = sh_wdt_probe,
322 .remove = __devexit_p(sh_wdt_remove),
323 .shutdown = sh_wdt_shutdown,
Paul Mundt8f5585e2010-05-25 18:31:12 +0900324};
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326static int __init sh_wdt_init(void)
327{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900328 if (unlikely(clock_division_ratio < 0x5 ||
329 clock_division_ratio > 0x7)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 clock_division_ratio = WTCSR_CKS_4096;
Paul Mundt8f5585e2010-05-25 18:31:12 +0900331
Joe Perches27c766a2012-02-15 15:06:19 -0800332 pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
333 clock_division_ratio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Paul Mundt8f5585e2010-05-25 18:31:12 +0900336 return platform_driver_register(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static void __exit sh_wdt_exit(void)
340{
Paul Mundt8f5585e2010-05-25 18:31:12 +0900341 platform_driver_unregister(&sh_wdt_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
Paul Mundt8f5585e2010-05-25 18:31:12 +0900343module_init(sh_wdt_init);
344module_exit(sh_wdt_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
347MODULE_DESCRIPTION("SuperH watchdog driver");
348MODULE_LICENSE("GPL");
Paul Mundt8f5585e2010-05-25 18:31:12 +0900349MODULE_ALIAS("platform:" DRV_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
351
352module_param(clock_division_ratio, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000353MODULE_PARM_DESC(clock_division_ratio,
354 "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
Randy Dunlap76550d32010-05-01 09:46:15 -0700355 "to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357module_param(heartbeat, int, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100358MODULE_PARM_DESC(heartbeat,
359 "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
360 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100362module_param(nowayout, bool, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100363MODULE_PARM_DESC(nowayout,
364 "Watchdog cannot be stopped once started (default="
365 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");