[PATCH] chardev: GPIO for SCx200 & PC-8736x: fix gpio_current, use shadow regs
[linux-2.6/kmemtrace.git] / drivers / char / watchdog / wdt977.c
blob3843900e94c49f8101364828f8d8bdeb656987b7
1 /*
2 * Wdt977 0.04: 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.
21 * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
22 * remove limitiation to be used on Netwinders only
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/config.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/fs.h>
31 #include <linux/miscdevice.h>
32 #include <linux/init.h>
33 #include <linux/ioport.h>
34 #include <linux/watchdog.h>
35 #include <linux/notifier.h>
36 #include <linux/reboot.h>
38 #include <asm/io.h>
39 #include <asm/system.h>
40 #include <asm/mach-types.h>
41 #include <asm/uaccess.h>
43 #define WATCHDOG_VERSION "0.04"
44 #define WATCHDOG_NAME "Wdt977"
45 #define PFX WATCHDOG_NAME ": "
46 #define DRIVER_VERSION WATCHDOG_NAME " driver, v" WATCHDOG_VERSION "\n"
48 #define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */
49 #define IO_DATA_PORT (IO_INDEX_PORT+1)
51 #define UNLOCK_DATA 0x87
52 #define LOCK_DATA 0xAA
53 #define DEVICE_REGISTER 0x07
56 #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
58 static int timeout = DEFAULT_TIMEOUT;
59 static int timeoutM; /* timeout in minutes */
60 static unsigned long timer_alive;
61 static int testmode;
62 static char expect_close;
63 static spinlock_t spinlock;
65 module_param(timeout, int, 0);
66 MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
67 module_param(testmode, int, 0);
68 MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
70 static int nowayout = WATCHDOG_NOWAYOUT;
71 module_param(nowayout, int, 0);
72 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
75 * Start the watchdog
78 static int wdt977_start(void)
80 unsigned long flags;
82 spin_lock_irqsave(&spinlock, flags);
84 /* unlock the SuperIO chip */
85 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
86 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
88 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
89 * F2 has the timeout in minutes
90 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
91 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
92 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
94 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
95 outb_p(0x08, IO_DATA_PORT);
96 outb_p(0xF2, IO_INDEX_PORT);
97 outb_p(timeoutM, IO_DATA_PORT);
98 outb_p(0xF3, IO_INDEX_PORT);
99 outb_p(0x00, IO_DATA_PORT); /* another setting is 0E for kbd/mouse/LED */
100 outb_p(0xF4, IO_INDEX_PORT);
101 outb_p(0x00, IO_DATA_PORT);
103 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
104 /* in test mode watch the bit 1 on F4 to indicate "triggered" */
105 if (!testmode)
107 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
108 outb_p(0x07, IO_DATA_PORT);
109 outb_p(0xE6, IO_INDEX_PORT);
110 outb_p(0x08, IO_DATA_PORT);
113 /* lock the SuperIO chip */
114 outb_p(LOCK_DATA, IO_INDEX_PORT);
116 spin_unlock_irqrestore(&spinlock, flags);
117 printk(KERN_INFO PFX "activated.\n");
119 return 0;
123 * Stop the watchdog
126 static int wdt977_stop(void)
128 unsigned long flags;
129 spin_lock_irqsave(&spinlock, flags);
131 /* unlock the SuperIO chip */
132 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
133 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
135 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
136 * F3 is reset to its default state
137 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
138 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
140 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
141 outb_p(0x08, IO_DATA_PORT);
142 outb_p(0xF2, IO_INDEX_PORT);
143 outb_p(0xFF, IO_DATA_PORT);
144 outb_p(0xF3, IO_INDEX_PORT);
145 outb_p(0x00, IO_DATA_PORT);
146 outb_p(0xF4, IO_INDEX_PORT);
147 outb_p(0x00, IO_DATA_PORT);
148 outb_p(0xF2, IO_INDEX_PORT);
149 outb_p(0x00, IO_DATA_PORT);
151 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
152 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
153 outb_p(0x07, IO_DATA_PORT);
154 outb_p(0xE6, IO_INDEX_PORT);
155 outb_p(0x08, IO_DATA_PORT);
157 /* lock the SuperIO chip */
158 outb_p(LOCK_DATA, IO_INDEX_PORT);
160 spin_unlock_irqrestore(&spinlock, flags);
161 printk(KERN_INFO PFX "shutdown.\n");
163 return 0;
167 * Send a keepalive ping to the watchdog
168 * This is done by simply re-writing the timeout to reg. 0xF2
171 static int wdt977_keepalive(void)
173 unsigned long flags;
174 spin_lock_irqsave(&spinlock, flags);
176 /* unlock the SuperIO chip */
177 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
178 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
180 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
181 /* F2 has the timeout in minutes */
182 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
183 outb_p(0x08, IO_DATA_PORT);
184 outb_p(0xF2, IO_INDEX_PORT);
185 outb_p(timeoutM, IO_DATA_PORT);
187 /* lock the SuperIO chip */
188 outb_p(LOCK_DATA, IO_INDEX_PORT);
189 spin_unlock_irqrestore(&spinlock, flags);
191 return 0;
195 * Set the watchdog timeout value
198 static int wdt977_set_timeout(int t)
200 int tmrval;
202 /* convert seconds to minutes, rounding up */
203 tmrval = (t + 59) / 60;
205 if (machine_is_netwinder()) {
206 /* we have a hw bug somewhere, so each 977 minute is actually only 30sec
207 * this limits the max timeout to half of device max of 255 minutes...
209 tmrval += tmrval;
212 if ((tmrval < 1) || (tmrval > 255))
213 return -EINVAL;
215 /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
216 timeout = t;
217 timeoutM = tmrval;
218 return 0;
222 * Get the watchdog status
225 static int wdt977_get_status(int *status)
227 int new_status;
228 unsigned long flags;
230 spin_lock_irqsave(&spinlock, flags);
232 /* unlock the SuperIO chip */
233 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
234 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
236 /* select device Aux2 (device=8) and read watchdog reg F4 */
237 outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
238 outb_p(0x08, IO_DATA_PORT);
239 outb_p(0xF4, IO_INDEX_PORT);
240 new_status = inb_p(IO_DATA_PORT);
242 /* lock the SuperIO chip */
243 outb_p(LOCK_DATA, IO_INDEX_PORT);
245 spin_unlock_irqrestore(&spinlock, flags);
247 *status=0;
248 if (new_status & 1)
249 *status |= WDIOF_CARDRESET;
251 return 0;
256 * /dev/watchdog handling
259 static int wdt977_open(struct inode *inode, struct file *file)
261 /* If the watchdog is alive we don't need to start it again */
262 if( test_and_set_bit(0,&timer_alive) )
263 return -EBUSY;
265 if (nowayout)
266 __module_get(THIS_MODULE);
268 wdt977_start();
269 return nonseekable_open(inode, file);
272 static int wdt977_release(struct inode *inode, struct file *file)
275 * Shut off the timer.
276 * Lock it in if it's a module and we set nowayout
278 if (expect_close == 42)
280 wdt977_stop();
281 clear_bit(0,&timer_alive);
282 } else {
283 wdt977_keepalive();
284 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
286 expect_close = 0;
287 return 0;
292 * wdt977_write:
293 * @file: file handle to the watchdog
294 * @buf: buffer to write (unused as data does not matter here
295 * @count: count of bytes
296 * @ppos: pointer to the position to write. No seeks allowed
298 * A write to a watchdog device is defined as a keepalive signal. Any
299 * write of data will do, as we we don't define content meaning.
302 static ssize_t wdt977_write(struct file *file, const char __user *buf,
303 size_t count, loff_t *ppos)
305 if (count)
307 if (!nowayout)
309 size_t i;
311 /* In case it was set long ago */
312 expect_close = 0;
314 for (i = 0; i != count; i++)
316 char c;
317 if (get_user(c, buf + i))
318 return -EFAULT;
319 if (c == 'V')
320 expect_close = 42;
324 /* someone wrote to us, we should restart timer */
325 wdt977_keepalive();
327 return count;
331 * wdt977_ioctl:
332 * @inode: inode of the device
333 * @file: file handle to the device
334 * @cmd: watchdog command
335 * @arg: argument pointer
337 * The watchdog API defines a common set of functions for all watchdogs
338 * according to their available features.
341 static struct watchdog_info ident = {
342 .options = WDIOF_SETTIMEOUT |
343 WDIOF_MAGICCLOSE |
344 WDIOF_KEEPALIVEPING,
345 .firmware_version = 1,
346 .identity = WATCHDOG_NAME,
349 static int wdt977_ioctl(struct inode *inode, struct file *file,
350 unsigned int cmd, unsigned long arg)
352 int status;
353 int new_options, retval = -EINVAL;
354 int new_timeout;
355 union {
356 struct watchdog_info __user *ident;
357 int __user *i;
358 } uarg;
360 uarg.i = (int __user *)arg;
362 switch(cmd)
364 default:
365 return -ENOIOCTLCMD;
367 case WDIOC_GETSUPPORT:
368 return copy_to_user(uarg.ident, &ident,
369 sizeof(ident)) ? -EFAULT : 0;
371 case WDIOC_GETSTATUS:
372 wdt977_get_status(&status);
373 return put_user(status, uarg.i);
375 case WDIOC_GETBOOTSTATUS:
376 return put_user(0, uarg.i);
378 case WDIOC_KEEPALIVE:
379 wdt977_keepalive();
380 return 0;
382 case WDIOC_SETOPTIONS:
383 if (get_user (new_options, uarg.i))
384 return -EFAULT;
386 if (new_options & WDIOS_DISABLECARD) {
387 wdt977_stop();
388 retval = 0;
391 if (new_options & WDIOS_ENABLECARD) {
392 wdt977_start();
393 retval = 0;
396 return retval;
398 case WDIOC_SETTIMEOUT:
399 if (get_user(new_timeout, uarg.i))
400 return -EFAULT;
402 if (wdt977_set_timeout(new_timeout))
403 return -EINVAL;
405 wdt977_keepalive();
406 /* Fall */
408 case WDIOC_GETTIMEOUT:
409 return put_user(timeout, uarg.i);
414 static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
415 void *unused)
417 if(code==SYS_DOWN || code==SYS_HALT)
418 wdt977_stop();
419 return NOTIFY_DONE;
422 static struct file_operations wdt977_fops=
424 .owner = THIS_MODULE,
425 .llseek = no_llseek,
426 .write = wdt977_write,
427 .ioctl = wdt977_ioctl,
428 .open = wdt977_open,
429 .release = wdt977_release,
432 static struct miscdevice wdt977_miscdev=
434 .minor = WATCHDOG_MINOR,
435 .name = "watchdog",
436 .fops = &wdt977_fops,
439 static struct notifier_block wdt977_notifier = {
440 .notifier_call = wdt977_notify_sys,
443 static int __init wd977_init(void)
445 int rc;
447 //if (!machine_is_netwinder())
448 // return -ENODEV;
450 printk(KERN_INFO PFX DRIVER_VERSION);
452 spin_lock_init(&spinlock);
454 /* Check that the timeout value is within it's range ; if not reset to the default */
455 if (wdt977_set_timeout(timeout))
457 wdt977_set_timeout(DEFAULT_TIMEOUT);
458 printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
459 DEFAULT_TIMEOUT);
462 /* on Netwinder the IOports are already reserved by
463 * arch/arm/mach-footbridge/netwinder-hw.c
465 if (!machine_is_netwinder())
467 if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME))
469 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
470 IO_INDEX_PORT);
471 rc = -EIO;
472 goto err_out;
476 rc = misc_register(&wdt977_miscdev);
477 if (rc)
479 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
480 wdt977_miscdev.minor, rc);
481 goto err_out_region;
484 rc = register_reboot_notifier(&wdt977_notifier);
485 if (rc)
487 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
488 rc);
489 goto err_out_miscdev;
492 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
493 timeout, nowayout, testmode);
495 return 0;
497 err_out_miscdev:
498 misc_deregister(&wdt977_miscdev);
499 err_out_region:
500 if (!machine_is_netwinder())
501 release_region(IO_INDEX_PORT,2);
502 err_out:
503 return rc;
506 static void __exit wd977_exit(void)
508 wdt977_stop();
509 misc_deregister(&wdt977_miscdev);
510 unregister_reboot_notifier(&wdt977_notifier);
511 release_region(IO_INDEX_PORT,2);
514 module_init(wd977_init);
515 module_exit(wd977_exit);
517 MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
518 MODULE_DESCRIPTION("W83977AF Watchdog driver");
519 MODULE_LICENSE("GPL");
520 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);