blob: 3843900e94c49f8101364828f8d8bdeb656987b7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Woody Suwalski4dab06f2006-01-08 01:00:31 -08002 * Wdt977 0.04: A Watchdog Device for Netwinder W83977AF chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
5 *
6 * -----------------------
7 *
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.
12 *
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.
Woody Suwalski4dab06f2006-01-08 01:00:31 -080021 * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
22 * remove limitiation to be used on Netwinders only
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
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>
Woody Suwalski4dab06f2006-01-08 01:00:31 -080033#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/watchdog.h>
35#include <linux/notifier.h>
36#include <linux/reboot.h>
37
38#include <asm/io.h>
39#include <asm/system.h>
40#include <asm/mach-types.h>
41#include <asm/uaccess.h>
42
Woody Suwalski4dab06f2006-01-08 01:00:31 -080043#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"
47
48#define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */
49#define IO_DATA_PORT (IO_INDEX_PORT+1)
50
51#define UNLOCK_DATA 0x87
52#define LOCK_DATA 0xAA
53#define DEVICE_REGISTER 0x07
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define DEFAULT_TIMEOUT 60 /* default timeout in seconds */
57
58static int timeout = DEFAULT_TIMEOUT;
59static int timeoutM; /* timeout in minutes */
60static unsigned long timer_alive;
61static int testmode;
62static char expect_close;
Woody Suwalski4dab06f2006-01-08 01:00:31 -080063static spinlock_t spinlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65module_param(timeout, int, 0);
66MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")");
67module_param(testmode, int, 0);
68MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0");
69
Andrey Panin4bfdf372005-07-27 11:43:58 -070070static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071module_param(nowayout, int, 0);
72MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
73
74/*
75 * Start the watchdog
76 */
77
78static int wdt977_start(void)
79{
Woody Suwalski4dab06f2006-01-08 01:00:31 -080080 unsigned long flags;
81
82 spin_lock_irqsave(&spinlock, flags);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* unlock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -080085 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
86 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
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)
93 */
Woody Suwalski4dab06f2006-01-08 01:00:31 -080094 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
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)
106 {
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800107 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 /* lock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800114 outb_p(LOCK_DATA, IO_INDEX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800116 spin_unlock_irqrestore(&spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 printk(KERN_INFO PFX "activated.\n");
118
119 return 0;
120}
121
122/*
123 * Stop the watchdog
124 */
125
126static int wdt977_stop(void)
127{
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800128 unsigned long flags;
129 spin_lock_irqsave(&spinlock, flags);
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /* unlock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800132 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
133 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
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
139 */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800140 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800152 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* lock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800158 outb_p(LOCK_DATA, IO_INDEX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800160 spin_unlock_irqrestore(&spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 printk(KERN_INFO PFX "shutdown.\n");
162
163 return 0;
164}
165
166/*
167 * Send a keepalive ping to the watchdog
168 * This is done by simply re-writing the timeout to reg. 0xF2
169 */
170
171static int wdt977_keepalive(void)
172{
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800173 unsigned long flags;
174 spin_lock_irqsave(&spinlock, flags);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* unlock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800177 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
178 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
181 /* F2 has the timeout in minutes */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800182 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* lock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800188 outb_p(LOCK_DATA, IO_INDEX_PORT);
189 spin_unlock_irqrestore(&spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 return 0;
192}
193
194/*
195 * Set the watchdog timeout value
196 */
197
198static int wdt977_set_timeout(int t)
199{
200 int tmrval;
201
202 /* convert seconds to minutes, rounding up */
203 tmrval = (t + 59) / 60;
204
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...
208 */
209 tmrval += tmrval;
210 }
211
212 if ((tmrval < 1) || (tmrval > 255))
213 return -EINVAL;
214
215 /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */
216 timeout = t;
217 timeoutM = tmrval;
218 return 0;
219}
220
221/*
222 * Get the watchdog status
223 */
224
225static int wdt977_get_status(int *status)
226{
227 int new_status;
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800228 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800230 spin_lock_irqsave(&spinlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* unlock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800233 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
234 outb_p(UNLOCK_DATA, IO_INDEX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* select device Aux2 (device=8) and read watchdog reg F4 */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800237 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* lock the SuperIO chip */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800243 outb_p(LOCK_DATA, IO_INDEX_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800245 spin_unlock_irqrestore(&spinlock, flags);
246
247 *status=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (new_status & 1)
249 *status |= WDIOF_CARDRESET;
250
251 return 0;
252}
253
254
255/*
256 * /dev/watchdog handling
257 */
258
259static int wdt977_open(struct inode *inode, struct file *file)
260{
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;
264
265 if (nowayout)
266 __module_get(THIS_MODULE);
267
268 wdt977_start();
269 return nonseekable_open(inode, file);
270}
271
272static int wdt977_release(struct inode *inode, struct file *file)
273{
274 /*
275 * Shut off the timer.
276 * Lock it in if it's a module and we set nowayout
277 */
278 if (expect_close == 42)
279 {
280 wdt977_stop();
281 clear_bit(0,&timer_alive);
282 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 wdt977_keepalive();
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800284 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 expect_close = 0;
287 return 0;
288}
289
290
291/*
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
297 *
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.
300 */
301
302static ssize_t wdt977_write(struct file *file, const char __user *buf,
303 size_t count, loff_t *ppos)
304{
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800305 if (count)
306 {
307 if (!nowayout)
308 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 size_t i;
310
311 /* In case it was set long ago */
312 expect_close = 0;
313
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800314 for (i = 0; i != count; i++)
315 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 char c;
317 if (get_user(c, buf + i))
318 return -EFAULT;
319 if (c == 'V')
320 expect_close = 42;
321 }
322 }
323
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800324 /* someone wrote to us, we should restart timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 wdt977_keepalive();
326 }
327 return count;
328}
329
330/*
331 * wdt977_ioctl:
332 * @inode: inode of the device
333 * @file: file handle to the device
334 * @cmd: watchdog command
335 * @arg: argument pointer
336 *
337 * The watchdog API defines a common set of functions for all watchdogs
338 * according to their available features.
339 */
340
341static struct watchdog_info ident = {
342 .options = WDIOF_SETTIMEOUT |
343 WDIOF_MAGICCLOSE |
344 WDIOF_KEEPALIVEPING,
345 .firmware_version = 1,
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800346 .identity = WATCHDOG_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347};
348
349static int wdt977_ioctl(struct inode *inode, struct file *file,
350 unsigned int cmd, unsigned long arg)
351{
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;
359
360 uarg.i = (int __user *)arg;
361
362 switch(cmd)
363 {
364 default:
365 return -ENOIOCTLCMD;
366
367 case WDIOC_GETSUPPORT:
368 return copy_to_user(uarg.ident, &ident,
369 sizeof(ident)) ? -EFAULT : 0;
370
371 case WDIOC_GETSTATUS:
372 wdt977_get_status(&status);
373 return put_user(status, uarg.i);
374
375 case WDIOC_GETBOOTSTATUS:
376 return put_user(0, uarg.i);
377
378 case WDIOC_KEEPALIVE:
379 wdt977_keepalive();
380 return 0;
381
382 case WDIOC_SETOPTIONS:
383 if (get_user (new_options, uarg.i))
384 return -EFAULT;
385
386 if (new_options & WDIOS_DISABLECARD) {
387 wdt977_stop();
388 retval = 0;
389 }
390
391 if (new_options & WDIOS_ENABLECARD) {
392 wdt977_start();
393 retval = 0;
394 }
395
396 return retval;
397
398 case WDIOC_SETTIMEOUT:
399 if (get_user(new_timeout, uarg.i))
400 return -EFAULT;
401
402 if (wdt977_set_timeout(new_timeout))
403 return -EINVAL;
404
405 wdt977_keepalive();
406 /* Fall */
407
408 case WDIOC_GETTIMEOUT:
409 return put_user(timeout, uarg.i);
410
411 }
412}
413
414static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
415 void *unused)
416{
417 if(code==SYS_DOWN || code==SYS_HALT)
418 wdt977_stop();
419 return NOTIFY_DONE;
420}
421
422static struct file_operations wdt977_fops=
423{
424 .owner = THIS_MODULE,
425 .llseek = no_llseek,
426 .write = wdt977_write,
427 .ioctl = wdt977_ioctl,
428 .open = wdt977_open,
429 .release = wdt977_release,
430};
431
432static struct miscdevice wdt977_miscdev=
433{
434 .minor = WATCHDOG_MINOR,
435 .name = "watchdog",
436 .fops = &wdt977_fops,
437};
438
439static struct notifier_block wdt977_notifier = {
440 .notifier_call = wdt977_notify_sys,
441};
442
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800443static int __init wd977_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800445 int rc;
446
447 //if (!machine_is_netwinder())
448 // return -ENODEV;
449
450 printk(KERN_INFO PFX DRIVER_VERSION);
451
452 spin_lock_init(&spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* Check that the timeout value is within it's range ; if not reset to the default */
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800455 if (wdt977_set_timeout(timeout))
456 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 wdt977_set_timeout(DEFAULT_TIMEOUT);
458 printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n",
459 DEFAULT_TIMEOUT);
460 }
461
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800462 /* on Netwinder the IOports are already reserved by
463 * arch/arm/mach-footbridge/netwinder-hw.c
464 */
465 if (!machine_is_netwinder())
466 {
467 if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME))
468 {
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;
473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800476 rc = misc_register(&wdt977_miscdev);
477 if (rc)
478 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800480 wdt977_miscdev.minor, rc);
481 goto err_out_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800484 rc = register_reboot_notifier(&wdt977_notifier);
485 if (rc)
486 {
487 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
488 rc);
489 goto err_out_miscdev;
490 }
491
492 printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 timeout, nowayout, testmode);
494
495 return 0;
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800496
497err_out_miscdev:
498 misc_deregister(&wdt977_miscdev);
499err_out_region:
500 if (!machine_is_netwinder())
501 release_region(IO_INDEX_PORT,2);
502err_out:
503 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800506static void __exit wd977_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800508 wdt977_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 misc_deregister(&wdt977_miscdev);
510 unregister_reboot_notifier(&wdt977_notifier);
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800511 release_region(IO_INDEX_PORT,2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800514module_init(wd977_init);
515module_exit(wd977_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Woody Suwalski4dab06f2006-01-08 01:00:31 -0800517MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518MODULE_DESCRIPTION("W83977AF Watchdog driver");
519MODULE_LICENSE("GPL");
520MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);