Linux-2.6.12-rc2
[linux-2.6/kvm.git] / drivers / char / watchdog / wdt977.c
blob072e9b214759922bdaf6f1a2b17c6b8ad682d402
1 /*
2 * Wdt977 0.03: A Watchdog Device for Netwinder W83977AF chip
4 * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
6 * -----------------------
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * -----------------------
14 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
15 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
16 * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
17 * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
18 * from minutes to seconds.
19 * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
20 * nwwatchdog_init.
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/config.h>
26 #include <linux/types.h>
27 #include <linux/kernel.h>
28 #include <linux/fs.h>
29 #include <linux/miscdevice.h>
30 #include <linux/init.h>
31 #include <linux/watchdog.h>
32 #include <linux/notifier.h>
33 #include <linux/reboot.h>
35 #include <asm/io.h>
36 #include <asm/system.h>
37 #include <asm/mach-types.h>
38 #include <asm/uaccess.h>
40 #define PFX "Wdt977: "
41 #define WATCHDOG_MINOR 130
43 #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
45 static int timeout = DEFAULT_TIMEOUT;
46 static int timeoutM; /* timeout in minutes */
47 static unsigned long timer_alive;
48 static int testmode;
49 static char expect_close;
51 module_param(timeout, int, 0);
52 MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
53 module_param(testmode, int, 0);
54 MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
56 #ifdef CONFIG_WATCHDOG_NOWAYOUT
57 static int nowayout = 1;
58 #else
59 static int nowayout = 0;
60 #endif
62 module_param(nowayout, int, 0);
63 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
66 * Start the watchdog
69 static int wdt977_start(void)
71 /* unlock the SuperIO chip */
72 outb(0x87,0x370);
73 outb(0x87,0x370);
75 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
76 * F2 has the timeout in minutes
77 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
78 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
79 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
81 outb(0x07,0x370);
82 outb(0x08,0x371);
83 outb(0xF2,0x370);
84 outb(timeoutM,0x371);
85 outb(0xF3,0x370);
86 outb(0x00,0x371); /* another setting is 0E for kbd/mouse/LED */
87 outb(0xF4,0x370);
88 outb(0x00,0x371);
90 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
91 /* in test mode watch the bit 1 on F4 to indicate "triggered" */
92 if (!testmode)
94 outb(0x07,0x370);
95 outb(0x07,0x371);
96 outb(0xE6,0x370);
97 outb(0x08,0x371);
100 /* lock the SuperIO chip */
101 outb(0xAA,0x370);
103 printk(KERN_INFO PFX "activated.\n");
105 return 0;
109 * Stop the watchdog
112 static int wdt977_stop(void)
114 /* unlock the SuperIO chip */
115 outb(0x87,0x370);
116 outb(0x87,0x370);
118 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
119 * F3 is reset to its default state
120 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
121 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
123 outb(0x07,0x370);
124 outb(0x08,0x371);
125 outb(0xF2,0x370);
126 outb(0xFF,0x371);
127 outb(0xF3,0x370);
128 outb(0x00,0x371);
129 outb(0xF4,0x370);
130 outb(0x00,0x371);
131 outb(0xF2,0x370);
132 outb(0x00,0x371);
134 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
135 outb(0x07,0x370);
136 outb(0x07,0x371);
137 outb(0xE6,0x370);
138 outb(0x08,0x371);
140 /* lock the SuperIO chip */
141 outb(0xAA,0x370);
143 printk(KERN_INFO PFX "shutdown.\n");
145 return 0;
149 * Send a keepalive ping to the watchdog
150 * This is done by simply re-writing the timeout to reg. 0xF2
153 static int wdt977_keepalive(void)
155 /* unlock the SuperIO chip */
156 outb(0x87,0x370);
157 outb(0x87,0x370);
159 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
160 /* F2 has the timeout in minutes */
161 outb(0x07,0x370);
162 outb(0x08,0x371);
163 outb(0xF2,0x370);
164 outb(timeoutM,0x371);
166 /* lock the SuperIO chip */
167 outb(0xAA,0x370);
169 return 0;
173 * Set the watchdog timeout value
176 static int wdt977_set_timeout(int t)
178 int tmrval;
180 /* convert seconds to minutes, rounding up */
181 tmrval = (t + 59) / 60;
183 if (machine_is_netwinder()) {
184 /* we have a hw bug somewhere, so each 977 minute is actually only 30sec
185 * this limits the max timeout to half of device max of 255 minutes...
187 tmrval += tmrval;
190 if ((tmrval < 1) || (tmrval > 255))
191 return -EINVAL;
193 /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
194 timeout = t;
195 timeoutM = tmrval;
196 return 0;
200 * Get the watchdog status
203 static int wdt977_get_status(int *status)
205 int new_status;
207 *status=0;
209 /* unlock the SuperIO chip */
210 outb(0x87,0x370);
211 outb(0x87,0x370);
213 /* select device Aux2 (device=8) and read watchdog reg F4 */
214 outb(0x07,0x370);
215 outb(0x08,0x371);
216 outb(0xF4,0x370);
217 new_status = inb(0x371);
219 /* lock the SuperIO chip */
220 outb(0xAA,0x370);
222 if (new_status & 1)
223 *status |= WDIOF_CARDRESET;
225 return 0;
230 * /dev/watchdog handling
233 static int wdt977_open(struct inode *inode, struct file *file)
235 /* If the watchdog is alive we don't need to start it again */
236 if( test_and_set_bit(0,&timer_alive) )
237 return -EBUSY;
239 if (nowayout)
240 __module_get(THIS_MODULE);
242 wdt977_start();
243 return nonseekable_open(inode, file);
246 static int wdt977_release(struct inode *inode, struct file *file)
249 * Shut off the timer.
250 * Lock it in if it's a module and we set nowayout
252 if (expect_close == 42)
254 wdt977_stop();
255 clear_bit(0,&timer_alive);
256 } else {
257 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
258 wdt977_keepalive();
260 expect_close = 0;
261 return 0;
266 * wdt977_write:
267 * @file: file handle to the watchdog
268 * @buf: buffer to write (unused as data does not matter here
269 * @count: count of bytes
270 * @ppos: pointer to the position to write. No seeks allowed
272 * A write to a watchdog device is defined as a keepalive signal. Any
273 * write of data will do, as we we don't define content meaning.
276 static ssize_t wdt977_write(struct file *file, const char __user *buf,
277 size_t count, loff_t *ppos)
279 if (count) {
280 if (!nowayout) {
281 size_t i;
283 /* In case it was set long ago */
284 expect_close = 0;
286 for (i = 0; i != count; i++) {
287 char c;
288 if (get_user(c, buf + i))
289 return -EFAULT;
290 if (c == 'V')
291 expect_close = 42;
295 wdt977_keepalive();
297 return count;
301 * wdt977_ioctl:
302 * @inode: inode of the device
303 * @file: file handle to the device
304 * @cmd: watchdog command
305 * @arg: argument pointer
307 * The watchdog API defines a common set of functions for all watchdogs
308 * according to their available features.
311 static struct watchdog_info ident = {
312 .options = WDIOF_SETTIMEOUT |
313 WDIOF_MAGICCLOSE |
314 WDIOF_KEEPALIVEPING,
315 .firmware_version = 1,
316 .identity = "Winbond 83977",
319 static int wdt977_ioctl(struct inode *inode, struct file *file,
320 unsigned int cmd, unsigned long arg)
322 int status;
323 int new_options, retval = -EINVAL;
324 int new_timeout;
325 union {
326 struct watchdog_info __user *ident;
327 int __user *i;
328 } uarg;
330 uarg.i = (int __user *)arg;
332 switch(cmd)
334 default:
335 return -ENOIOCTLCMD;
337 case WDIOC_GETSUPPORT:
338 return copy_to_user(uarg.ident, &ident,
339 sizeof(ident)) ? -EFAULT : 0;
341 case WDIOC_GETSTATUS:
342 wdt977_get_status(&status);
343 return put_user(status, uarg.i);
345 case WDIOC_GETBOOTSTATUS:
346 return put_user(0, uarg.i);
348 case WDIOC_KEEPALIVE:
349 wdt977_keepalive();
350 return 0;
352 case WDIOC_SETOPTIONS:
353 if (get_user (new_options, uarg.i))
354 return -EFAULT;
356 if (new_options & WDIOS_DISABLECARD) {
357 wdt977_stop();
358 retval = 0;
361 if (new_options & WDIOS_ENABLECARD) {
362 wdt977_start();
363 retval = 0;
366 return retval;
368 case WDIOC_SETTIMEOUT:
369 if (get_user(new_timeout, uarg.i))
370 return -EFAULT;
372 if (wdt977_set_timeout(new_timeout))
373 return -EINVAL;
375 wdt977_keepalive();
376 /* Fall */
378 case WDIOC_GETTIMEOUT:
379 return put_user(timeout, uarg.i);
384 static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
385 void *unused)
387 if(code==SYS_DOWN || code==SYS_HALT)
388 wdt977_stop();
389 return NOTIFY_DONE;
392 static struct file_operations wdt977_fops=
394 .owner = THIS_MODULE,
395 .llseek = no_llseek,
396 .write = wdt977_write,
397 .ioctl = wdt977_ioctl,
398 .open = wdt977_open,
399 .release = wdt977_release,
402 static struct miscdevice wdt977_miscdev=
404 .minor = WATCHDOG_MINOR,
405 .name = "watchdog",
406 .fops = &wdt977_fops,
409 static struct notifier_block wdt977_notifier = {
410 .notifier_call = wdt977_notify_sys,
413 static int __init nwwatchdog_init(void)
415 int retval;
416 if (!machine_is_netwinder())
417 return -ENODEV;
419 /* Check that the timeout value is within it's range ; if not reset to the default */
420 if (wdt977_set_timeout(timeout)) {
421 wdt977_set_timeout(DEFAULT_TIMEOUT);
422 printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
423 DEFAULT_TIMEOUT);
426 retval = register_reboot_notifier(&wdt977_notifier);
427 if (retval) {
428 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
429 retval);
430 return retval;
433 retval = misc_register(&wdt977_miscdev);
434 if (retval) {
435 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
436 WATCHDOG_MINOR, retval);
437 unregister_reboot_notifier(&wdt977_notifier);
438 return retval;
441 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n",
442 timeout, nowayout, testmode);
444 return 0;
447 static void __exit nwwatchdog_exit(void)
449 misc_deregister(&wdt977_miscdev);
450 unregister_reboot_notifier(&wdt977_notifier);
453 module_init(nwwatchdog_init);
454 module_exit(nwwatchdog_exit);
456 MODULE_AUTHOR("Woody Suwalski <woody@netwinder.org>");
457 MODULE_DESCRIPTION("W83977AF Watchdog driver");
458 MODULE_LICENSE("GPL");
459 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);