blob: 626d0e8e56c390c48bb12301208f56fec3715c15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * 60xx Single Board Computer Watchdog Timer driver for Linux 2.2.x
3 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00004 * Based on acquirewdt.c by Alan Cox.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * The author does NOT admit liability nor provide warranty for
12 * any of this software. This material is provided "AS-IS" in
13 * the hope that it may be useful for others.
14 *
15 * (c) Copyright 2000 Jakob Oestergaard <jakob@unthought.net>
16 *
17 * 12/4 - 2000 [Initial revision]
18 * 25/4 - 2000 Added /dev/watchdog support
Alan Cox1780de42008-05-19 14:08:11 +010019 * 09/5 - 2001 [smj@oro.net] fixed fop_write to "return 1"
20 * on success
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * 12/4 - 2002 [rob@osinvestor.com] eliminate fop_read
22 * fix possible wdt_is_open race
23 * add CONFIG_WATCHDOG_NOWAYOUT support
24 * remove lock_kernel/unlock_kernel pairs
25 * added KERN_* to printk's
26 * got rid of extraneous comments
Alan Cox1780de42008-05-19 14:08:11 +010027 * changed watchdog_info to correctly reflect what
28 * the driver offers
29 * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS,
30 * WDIOC_SETTIMEOUT, WDIOC_GETTIMEOUT, and
31 * WDIOC_SETOPTIONS ioctls
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces
33 * use module_param
Alan Cox1780de42008-05-19 14:08:11 +010034 * made timeout (the emulated heartbeat) a
35 * module_param
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * made the keepalive ping an internal subroutine
37 * made wdt_stop and wdt_start module params
38 * added extra printk's for startup problems
39 * added MODULE_AUTHOR and MODULE_DESCRIPTION info
40 *
41 *
42 * This WDT driver is different from the other Linux WDT
43 * drivers in the following ways:
44 * *) The driver will ping the watchdog by itself, because this
45 * particular WDT has a very short timeout (one second) and it
46 * would be insane to count on any userspace daemon always
47 * getting scheduled within that time frame.
48 *
49 */
50
51#include <linux/module.h>
52#include <linux/moduleparam.h>
53#include <linux/types.h>
54#include <linux/timer.h>
55#include <linux/jiffies.h>
56#include <linux/miscdevice.h>
57#include <linux/watchdog.h>
58#include <linux/fs.h>
59#include <linux/ioport.h>
60#include <linux/notifier.h>
61#include <linux/reboot.h>
62#include <linux/init.h>
Alan Cox1780de42008-05-19 14:08:11 +010063#include <linux/io.h>
64#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/system.h>
67
68#define OUR_NAME "sbc60xxwdt"
69#define PFX OUR_NAME ": "
70
71/*
72 * You must set these - The driver cannot probe for the settings
73 */
74
75static int wdt_stop = 0x45;
76module_param(wdt_stop, int, 0);
77MODULE_PARM_DESC(wdt_stop, "SBC60xx WDT 'stop' io port (default 0x45)");
78
79static int wdt_start = 0x443;
80module_param(wdt_start, int, 0);
81MODULE_PARM_DESC(wdt_start, "SBC60xx WDT 'start' io port (default 0x443)");
82
83/*
84 * The 60xx board can use watchdog timeout values from one second
85 * to several minutes. The default is one second, so if we reset
86 * the watchdog every ~250ms we should be safe.
87 */
88
89#define WDT_INTERVAL (HZ/4+1)
90
91/*
92 * We must not require too good response from the userspace daemon.
93 * Here we require the userspace daemon to send us a heartbeat
94 * char to /dev/watchdog every 30 seconds.
95 * If the daemon pulses us every 25 seconds, we can still afford
96 * a 5 second scheduling delay on the (high priority) daemon. That
97 * should be sufficient for a box under any load.
98 */
99
100#define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
Alan Cox1780de42008-05-19 14:08:11 +0100101static int timeout = WATCHDOG_TIMEOUT; /* in seconds, multiplied by HZ to
102 get seconds to wait for a ping */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103module_param(timeout, int, 0);
Alan Cox1780de42008-05-19 14:08:11 +0100104MODULE_PARM_DESC(timeout,
105 "Watchdog timeout in seconds. (1<=timeout<=3600, default="
106 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Andrey Panin4bfdf372005-07-27 11:43:58 -0700108static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109module_param(nowayout, int, 0);
Alan Cox1780de42008-05-19 14:08:11 +0100110MODULE_PARM_DESC(nowayout,
111 "Watchdog cannot be stopped once started (default="
112 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static void wdt_timer_ping(unsigned long);
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100115static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static unsigned long next_heartbeat;
117static unsigned long wdt_is_open;
118static char wdt_expect_close;
119
120/*
121 * Whack the dog
122 */
123
124static void wdt_timer_ping(unsigned long data)
125{
126 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
127 * we agree to ping the WDT
128 */
Alan Cox1780de42008-05-19 14:08:11 +0100129 if (time_before(jiffies, next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* Ping the WDT by reading from wdt_start */
131 inb_p(wdt_start);
132 /* Re-set the timer interval */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100133 mod_timer(&timer, jiffies + WDT_INTERVAL);
Alan Cox1780de42008-05-19 14:08:11 +0100134 } else
135 printk(KERN_WARNING PFX
136 "Heartbeat lost! Will not ping the watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139/*
140 * Utility routines
141 */
142
143static void wdt_startup(void)
144{
145 next_heartbeat = jiffies + (timeout * HZ);
146
147 /* Start the timer */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100148 mod_timer(&timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
150}
151
152static void wdt_turnoff(void)
153{
154 /* Stop the timer */
155 del_timer(&timer);
156 inb_p(wdt_stop);
157 printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
158}
159
160static void wdt_keepalive(void)
161{
162 /* user land ping */
163 next_heartbeat = jiffies + (timeout * HZ);
164}
165
166/*
167 * /dev/watchdog handling
168 */
169
Alan Cox1780de42008-05-19 14:08:11 +0100170static ssize_t fop_write(struct file *file, const char __user *buf,
171 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 /* See if we got the magic character 'V' and reload the timer */
Alan Cox1780de42008-05-19 14:08:11 +0100174 if (count) {
175 if (!nowayout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 size_t ofs;
177
Alan Cox1780de42008-05-19 14:08:11 +0100178 /* note: just in case someone wrote the
179 magic character five months ago... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 wdt_expect_close = 0;
181
Alan Cox1780de42008-05-19 14:08:11 +0100182 /* scan to see whether or not we got the
183 magic character */
184 for (ofs = 0; ofs != count; ofs++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000186 if (get_user(c, buf + ofs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EFAULT;
Alan Cox1780de42008-05-19 14:08:11 +0100188 if (c == 'V')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 wdt_expect_close = 42;
190 }
191 }
192
Alan Cox1780de42008-05-19 14:08:11 +0100193 /* Well, anyhow someone wrote to us, we should
194 return that favour */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 wdt_keepalive();
196 }
197 return count;
198}
199
Alan Cox1780de42008-05-19 14:08:11 +0100200static int fop_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Just in case we're already talking to someone... */
Alan Cox1780de42008-05-19 14:08:11 +0100203 if (test_and_set_bit(0, &wdt_is_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EBUSY;
205
206 if (nowayout)
207 __module_get(THIS_MODULE);
208
209 /* Good, fire up the show */
210 wdt_startup();
Wim Van Sebroeck6abe78b2007-07-24 21:38:37 +0000211 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Alan Cox1780de42008-05-19 14:08:11 +0100214static int fop_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Alan Cox1780de42008-05-19 14:08:11 +0100216 if (wdt_expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 wdt_turnoff();
218 else {
219 del_timer(&timer);
Alan Cox1780de42008-05-19 14:08:11 +0100220 printk(KERN_CRIT PFX
221 "device file closed unexpectedly. Will not stop the WDT!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 clear_bit(0, &wdt_is_open);
224 wdt_expect_close = 0;
225 return 0;
226}
227
Alan Cox1780de42008-05-19 14:08:11 +0100228static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 void __user *argp = (void __user *)arg;
231 int __user *p = argp;
Alan Cox1780de42008-05-19 14:08:11 +0100232 static const struct watchdog_info ident = {
233 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
234 WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .firmware_version = 1,
236 .identity = "SBC60xx",
237 };
238
Alan Cox1780de42008-05-19 14:08:11 +0100239 switch (cmd) {
Alan Cox1780de42008-05-19 14:08:11 +0100240 case WDIOC_GETSUPPORT:
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000241 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Alan Cox1780de42008-05-19 14:08:11 +0100242 case WDIOC_GETSTATUS:
243 case WDIOC_GETBOOTSTATUS:
244 return put_user(0, p);
Alan Cox1780de42008-05-19 14:08:11 +0100245 case WDIOC_SETOPTIONS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 {
Alan Cox1780de42008-05-19 14:08:11 +0100247 int new_options, retval = -EINVAL;
248 if (get_user(new_options, p))
249 return -EFAULT;
250 if (new_options & WDIOS_DISABLECARD) {
251 wdt_turnoff();
252 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Alan Cox1780de42008-05-19 14:08:11 +0100254 if (new_options & WDIOS_ENABLECARD) {
255 wdt_startup();
256 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Alan Cox1780de42008-05-19 14:08:11 +0100258 return retval;
259 }
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000260 case WDIOC_KEEPALIVE:
261 wdt_keepalive();
262 return 0;
Alan Cox1780de42008-05-19 14:08:11 +0100263 case WDIOC_SETTIMEOUT:
264 {
265 int new_timeout;
266 if (get_user(new_timeout, p))
267 return -EFAULT;
268 /* arbitrary upper limit */
269 if (new_timeout < 1 || new_timeout > 3600)
270 return -EINVAL;
271
272 timeout = new_timeout;
273 wdt_keepalive();
274 /* Fall through */
275 }
276 case WDIOC_GETTIMEOUT:
277 return put_user(timeout, p);
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000278 default:
279 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281}
282
Arjan van de Ven62322d22006-07-03 00:24:21 -0700283static const struct file_operations wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 .owner = THIS_MODULE,
285 .llseek = no_llseek,
286 .write = fop_write,
287 .open = fop_open,
288 .release = fop_close,
Alan Cox1780de42008-05-19 14:08:11 +0100289 .unlocked_ioctl = fop_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
291
292static struct miscdevice wdt_miscdev = {
293 .minor = WATCHDOG_MINOR,
294 .name = "watchdog",
295 .fops = &wdt_fops,
296};
297
298/*
299 * Notifier for system down
300 */
301
302static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
303 void *unused)
304{
Alan Cox1780de42008-05-19 14:08:11 +0100305 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 wdt_turnoff();
307 return NOTIFY_DONE;
308}
309
310/*
311 * The WDT needs to learn about soft shutdowns in order to
312 * turn the timebomb registers off.
313 */
314
Alan Cox1780de42008-05-19 14:08:11 +0100315static struct notifier_block wdt_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 .notifier_call = wdt_notify_sys,
317};
318
319static void __exit sbc60xxwdt_unload(void)
320{
321 wdt_turnoff();
322
323 /* Deregister */
324 misc_deregister(&wdt_miscdev);
325
326 unregister_reboot_notifier(&wdt_notifier);
327 if ((wdt_stop != 0x45) && (wdt_stop != wdt_start))
Alan Cox1780de42008-05-19 14:08:11 +0100328 release_region(wdt_stop, 1);
329 release_region(wdt_start, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332static int __init sbc60xxwdt_init(void)
333{
334 int rc = -EBUSY;
335
Alan Cox1780de42008-05-19 14:08:11 +0100336 if (timeout < 1 || timeout > 3600) { /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 timeout = WATCHDOG_TIMEOUT;
Alan Cox1780de42008-05-19 14:08:11 +0100338 printk(KERN_INFO PFX
339 "timeout value must be 1 <= x <= 3600, using %d\n",
340 timeout);
341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Alan Cox1780de42008-05-19 14:08:11 +0100343 if (!request_region(wdt_start, 1, "SBC 60XX WDT")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
345 wdt_start);
346 rc = -EIO;
347 goto err_out;
348 }
349
350 /* We cannot reserve 0x45 - the kernel already has! */
Alan Cox1780de42008-05-19 14:08:11 +0100351 if (wdt_stop != 0x45 && wdt_stop != wdt_start) {
352 if (!request_region(wdt_stop, 1, "SBC 60XX WDT")) {
353 printk(KERN_ERR PFX
354 "I/O address 0x%04x already in use\n",
355 wdt_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 rc = -EIO;
357 goto err_out_region1;
358 }
359 }
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 rc = register_reboot_notifier(&wdt_notifier);
Alan Cox1780de42008-05-19 14:08:11 +0100362 if (rc) {
363 printk(KERN_ERR PFX
364 "cannot register reboot notifier (err=%d)\n", rc);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000365 goto err_out_region2;
366 }
367
368 rc = misc_register(&wdt_miscdev);
Alan Cox1780de42008-05-19 14:08:11 +0100369 if (rc) {
370 printk(KERN_ERR PFX
371 "cannot register miscdev on minor=%d (err=%d)\n",
372 wdt_miscdev.minor, rc);
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000373 goto err_out_reboot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000375 printk(KERN_INFO PFX
376 "WDT driver for 60XX single board computer initialised. "
377 "timeout=%d sec (nowayout=%d)\n", timeout, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 return 0;
380
Wim Van Sebroeckc6cb13a2007-12-26 20:32:51 +0000381err_out_reboot:
382 unregister_reboot_notifier(&wdt_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383err_out_region2:
Alan Cox1780de42008-05-19 14:08:11 +0100384 if (wdt_stop != 0x45 && wdt_stop != wdt_start)
385 release_region(wdt_stop, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386err_out_region1:
Alan Cox1780de42008-05-19 14:08:11 +0100387 release_region(wdt_start, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388err_out:
389 return rc;
390}
391
392module_init(sbc60xxwdt_init);
393module_exit(sbc60xxwdt_unload);
394
395MODULE_AUTHOR("Jakob Oestergaard <jakob@unthought.net>");
396MODULE_DESCRIPTION("60xx Single Board Computer Watchdog Timer driver");
397MODULE_LICENSE("GPL");
398MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);