blob: bee1f5865825d6c7aac34f4ca1522f2721d8d843 [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 Mundtb1fa8882010-05-24 10:55:59 +09006 * Copyright (C) 2001 - 2010 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/types.h>
24#include <linux/miscdevice.h>
25#include <linux/watchdog.h>
26#include <linux/reboot.h>
27#include <linux/notifier.h>
28#include <linux/ioport.h>
29#include <linux/fs.h>
Paul Mundtf1184202006-09-27 17:53:59 +090030#include <linux/mm.h>
Alan Cox70b814e2008-05-19 14:08:55 +010031#include <linux/io.h>
32#include <linux/uaccess.h>
Adrian Bunk58cf4192008-08-08 18:39:11 +030033#include <asm/watchdog.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#define PFX "shwdt: "
36
37/*
38 * Default clock division ratio is 5.25 msecs. For an additional table of
39 * values, consult the asm-sh/watchdog.h. Overload this at module load
40 * time.
41 *
42 * In order for this to work reliably we need to have HZ set to 1000 or
43 * something quite higher than 100 (or we need a proper high-res timer
44 * implementation that will deal with this properly), otherwise the 10ms
45 * resolution of a jiffy is enough to trigger the overflow. For things like
46 * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
47 * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
48 * necssary.
49 *
50 * As a result of this timing problem, the only modes that are particularly
51 * feasible are the 4096 and the 2048 divisors, which yeild 5.25 and 2.62ms
52 * overflow periods respectively.
53 *
54 * Also, since we can't really expect userspace to be responsive enough
Joe Perchesee0fc092008-02-03 17:32:52 +020055 * before the overflow happens, we maintain two separate timers .. One in
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * the kernel for clearing out WOVF every 2ms or so (again, this depends on
57 * HZ == 1000), and another for monitoring userspace writes to the WDT device.
58 *
59 * As such, we currently use a configurable heartbeat interval which defaults
60 * to 30s. In this case, the userspace daemon is only responsible for periodic
61 * writes to the device before the next heartbeat is scheduled. If the daemon
62 * misses its deadline, the kernel timer will allow the WDT to overflow.
63 */
64static int clock_division_ratio = WTCSR_CKS_4096;
65
66#define next_ping_period(cks) msecs_to_jiffies(cks - 4)
67
Jiri Slaby82eb7c52007-02-08 18:39:36 +010068static void sh_wdt_ping(unsigned long data);
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static unsigned long shwdt_is_open;
Adrian Bunk58cf4192008-08-08 18:39:11 +030071static const struct watchdog_info sh_wdt_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static char shwdt_expect_close;
Jiri Slaby82eb7c52007-02-08 18:39:36 +010073static DEFINE_TIMER(timer, sh_wdt_ping, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static unsigned long next_heartbeat;
Alan Cox70b814e2008-05-19 14:08:55 +010075static DEFINE_SPINLOCK(shwdt_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */
78static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */
Andrey Panin4bfdf372005-07-27 11:43:58 -070079static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static void sh_wdt_start(void)
82{
83 __u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +010084 unsigned long flags;
85
Adrian Bunk58cf4192008-08-08 18:39:11 +030086 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 next_heartbeat = jiffies + (heartbeat * HZ);
89 mod_timer(&timer, next_ping_period(clock_division_ratio));
90
91 csr = sh_wdt_read_csr();
92 csr |= WTCSR_WT | clock_division_ratio;
93 sh_wdt_write_csr(csr);
94
95 sh_wdt_write_cnt(0);
96
97 /*
98 * These processors have a bit of an inconsistent initialization
99 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
100 * RSTCSR register was removed.
101 *
102 * On the SH-2 however, in addition with bits being in different
103 * locations, we must deal with RSTCSR outright..
104 */
105 csr = sh_wdt_read_csr();
106 csr |= WTCSR_TME;
107 csr &= ~WTCSR_RSTS;
108 sh_wdt_write_csr(csr);
109
110#ifdef CONFIG_CPU_SH2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 csr = sh_wdt_read_rstcsr();
112 csr &= ~RSTCSR_RSTS;
113 sh_wdt_write_rstcsr(csr);
114#endif
Adrian Bunk58cf4192008-08-08 18:39:11 +0300115 spin_unlock_irqrestore(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static void sh_wdt_stop(void)
119{
120 __u8 csr;
Alan Cox70b814e2008-05-19 14:08:55 +0100121 unsigned long flags;
122
Adrian Bunk58cf4192008-08-08 18:39:11 +0300123 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 del_timer(&timer);
126
127 csr = sh_wdt_read_csr();
128 csr &= ~WTCSR_TME;
129 sh_wdt_write_csr(csr);
Adrian Bunk58cf4192008-08-08 18:39:11 +0300130 spin_unlock_irqrestore(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900133static inline void sh_wdt_keepalive(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Alan Cox70b814e2008-05-19 14:08:55 +0100135 unsigned long flags;
136
Adrian Bunk58cf4192008-08-08 18:39:11 +0300137 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 next_heartbeat = jiffies + (heartbeat * HZ);
Adrian Bunk58cf4192008-08-08 18:39:11 +0300139 spin_unlock_irqrestore(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static int sh_wdt_set_heartbeat(int t)
143{
Alan Cox70b814e2008-05-19 14:08:55 +0100144 unsigned long flags;
145
146 if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -EINVAL;
148
Adrian Bunk58cf4192008-08-08 18:39:11 +0300149 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 heartbeat = t;
Adrian Bunk58cf4192008-08-08 18:39:11 +0300151 spin_unlock_irqrestore(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return 0;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void sh_wdt_ping(unsigned long data)
156{
Alan Cox70b814e2008-05-19 14:08:55 +0100157 unsigned long flags;
158
Adrian Bunk58cf4192008-08-08 18:39:11 +0300159 spin_lock_irqsave(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (time_before(jiffies, next_heartbeat)) {
161 __u8 csr;
162
163 csr = sh_wdt_read_csr();
164 csr &= ~WTCSR_IOVF;
165 sh_wdt_write_csr(csr);
166
167 sh_wdt_write_cnt(0);
168
169 mod_timer(&timer, next_ping_period(clock_division_ratio));
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900170 } else
171 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping "
172 "the watchdog\n");
Adrian Bunk58cf4192008-08-08 18:39:11 +0300173 spin_unlock_irqrestore(&shwdt_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static int sh_wdt_open(struct inode *inode, struct file *file)
177{
178 if (test_and_set_bit(0, &shwdt_is_open))
179 return -EBUSY;
180 if (nowayout)
181 __module_get(THIS_MODULE);
182
183 sh_wdt_start();
184
185 return nonseekable_open(inode, file);
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static int sh_wdt_close(struct inode *inode, struct file *file)
189{
190 if (shwdt_expect_close == 42) {
191 sh_wdt_stop();
192 } else {
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900193 printk(KERN_CRIT PFX "Unexpected close, not "
194 "stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 sh_wdt_keepalive();
196 }
197
198 clear_bit(0, &shwdt_is_open);
199 shwdt_expect_close = 0;
200
201 return 0;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static ssize_t sh_wdt_write(struct file *file, const char *buf,
205 size_t count, loff_t *ppos)
206{
207 if (count) {
208 if (!nowayout) {
209 size_t i;
210
211 shwdt_expect_close = 0;
212
213 for (i = 0; i != count; i++) {
214 char c;
215 if (get_user(c, buf + i))
216 return -EFAULT;
217 if (c == 'V')
218 shwdt_expect_close = 42;
219 }
220 }
221 sh_wdt_keepalive();
222 }
223
224 return count;
225}
226
Alan Cox70b814e2008-05-19 14:08:55 +0100227static long sh_wdt_ioctl(struct file *file, unsigned int cmd,
228 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 int new_heartbeat;
231 int options, retval = -EINVAL;
232
233 switch (cmd) {
Alan Cox70b814e2008-05-19 14:08:55 +0100234 case WDIOC_GETSUPPORT:
235 return copy_to_user((struct watchdog_info *)arg,
236 &sh_wdt_info, sizeof(sh_wdt_info)) ? -EFAULT : 0;
237 case WDIOC_GETSTATUS:
238 case WDIOC_GETBOOTSTATUS:
239 return put_user(0, (int *)arg);
Alan Cox70b814e2008-05-19 14:08:55 +0100240 case WDIOC_SETOPTIONS:
241 if (get_user(options, (int *)arg))
242 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Alan Cox70b814e2008-05-19 14:08:55 +0100244 if (options & WDIOS_DISABLECARD) {
245 sh_wdt_stop();
246 retval = 0;
247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Alan Cox70b814e2008-05-19 14:08:55 +0100249 if (options & WDIOS_ENABLECARD) {
250 sh_wdt_start();
251 retval = 0;
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Alan Cox70b814e2008-05-19 14:08:55 +0100254 return retval;
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000255 case WDIOC_KEEPALIVE:
256 sh_wdt_keepalive();
257 return 0;
258 case WDIOC_SETTIMEOUT:
259 if (get_user(new_heartbeat, (int *)arg))
260 return -EFAULT;
261
262 if (sh_wdt_set_heartbeat(new_heartbeat))
263 return -EINVAL;
264
265 sh_wdt_keepalive();
266 /* Fall */
267 case WDIOC_GETTIMEOUT:
268 return put_user(heartbeat, (int *)arg);
Alan Cox70b814e2008-05-19 14:08:55 +0100269 default:
270 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int sh_wdt_notify_sys(struct notifier_block *this,
276 unsigned long code, void *unused)
277{
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900278 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sh_wdt_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 return NOTIFY_DONE;
282}
283
Arjan van de Ven62322d22006-07-03 00:24:21 -0700284static const struct file_operations sh_wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 .owner = THIS_MODULE,
286 .llseek = no_llseek,
287 .write = sh_wdt_write,
Alan Cox70b814e2008-05-19 14:08:55 +0100288 .unlocked_ioctl = sh_wdt_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .open = sh_wdt_open,
290 .release = sh_wdt_close,
291};
292
Alan Cox70b814e2008-05-19 14:08:55 +0100293static const struct watchdog_info sh_wdt_info = {
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900294 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
295 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .firmware_version = 1,
297 .identity = "SH WDT",
298};
299
300static struct notifier_block sh_wdt_notifier = {
301 .notifier_call = sh_wdt_notify_sys,
302};
303
304static struct miscdevice sh_wdt_miscdev = {
305 .minor = WATCHDOG_MINOR,
306 .name = "watchdog",
307 .fops = &sh_wdt_fops,
308};
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static int __init sh_wdt_init(void)
311{
312 int rc;
313
Alan Cox70b814e2008-05-19 14:08:55 +0100314 if (clock_division_ratio < 0x5 || clock_division_ratio > 0x7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 clock_division_ratio = WTCSR_CKS_4096;
Alan Cox70b814e2008-05-19 14:08:55 +0100316 printk(KERN_INFO PFX
317 "clock_division_ratio value must be 0x5<=x<=0x7, using %d\n",
318 clock_division_ratio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900321 rc = sh_wdt_set_heartbeat(heartbeat);
322 if (unlikely(rc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 heartbeat = WATCHDOG_HEARTBEAT;
Alan Cox70b814e2008-05-19 14:08:55 +0100324 printk(KERN_INFO PFX
325 "heartbeat value must be 1<=x<=3600, using %d\n",
326 heartbeat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 rc = register_reboot_notifier(&sh_wdt_notifier);
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900330 if (unlikely(rc)) {
Alan Cox70b814e2008-05-19 14:08:55 +0100331 printk(KERN_ERR PFX
332 "Can't register reboot notifier (err=%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return rc;
334 }
335
336 rc = misc_register(&sh_wdt_miscdev);
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900337 if (unlikely(rc)) {
Alan Cox70b814e2008-05-19 14:08:55 +0100338 printk(KERN_ERR PFX
339 "Can't register miscdev on minor=%d (err=%d)\n",
340 sh_wdt_miscdev.minor, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unregister_reboot_notifier(&sh_wdt_notifier);
342 return rc;
343 }
344
345 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
346 heartbeat, nowayout);
347
348 return 0;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351static void __exit sh_wdt_exit(void)
352{
353 misc_deregister(&sh_wdt_miscdev);
354 unregister_reboot_notifier(&sh_wdt_notifier);
355}
356
357MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
358MODULE_DESCRIPTION("SuperH watchdog driver");
359MODULE_LICENSE("GPL");
360MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
361
362module_param(clock_division_ratio, int, 0);
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000363MODULE_PARM_DESC(clock_division_ratio,
364 "Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
365 "to 0x7 (5.25ms). (default=" __MODULE_STRING(clock_division_ratio) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367module_param(heartbeat, int, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100368MODULE_PARM_DESC(heartbeat,
369 "Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
370 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372module_param(nowayout, int, 0);
Alan Cox70b814e2008-05-19 14:08:55 +0100373MODULE_PARM_DESC(nowayout,
374 "Watchdog cannot be stopped once started (default="
375 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377module_init(sh_wdt_init);
378module_exit(sh_wdt_exit);