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
21 * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
22 * remove limitiation to be used on
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/types.h>
29 #include <linux/kernel.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 <linux/uaccess.h>
40 #include <asm/system.h>
41 #include <asm/mach-types.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
;
62 static char expect_close
;
63 static DEFINE_SPINLOCK(spinlock
);
65 module_param(timeout
, int, 0);
66 MODULE_PARM_DESC(timeout
, "Watchdog timeout in seconds (60..15300), default="
67 __MODULE_STRING(DEFAULT_TIMEOUT
) ")");
68 module_param(testmode
, int, 0);
69 MODULE_PARM_DESC(testmode
, "Watchdog testmode (1 = no reboot), default=0");
71 static int nowayout
= WATCHDOG_NOWAYOUT
;
72 module_param(nowayout
, int, 0);
73 MODULE_PARM_DESC(nowayout
,
74 "Watchdog cannot be stopped once started (default="
75 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
81 static int wdt977_start(void)
85 spin_lock_irqsave(&spinlock
, flags
);
87 /* unlock the SuperIO chip */
88 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
89 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
91 /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
92 * F2 has the timeout in minutes
93 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
94 * at timeout, and to reset timer on kbd/mouse activity (not impl.)
95 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
97 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
98 outb_p(0x08, IO_DATA_PORT
);
99 outb_p(0xF2, IO_INDEX_PORT
);
100 outb_p(timeoutM
, IO_DATA_PORT
);
101 outb_p(0xF3, IO_INDEX_PORT
);
102 outb_p(0x00, IO_DATA_PORT
); /* another setting is 0E for
104 outb_p(0xF4, IO_INDEX_PORT
);
105 outb_p(0x00, IO_DATA_PORT
);
107 /* At last select device Aux1 (dev=7) and set GP16 as a
108 * watchdog output. In test mode watch the bit 1 on F4 to
109 * indicate "triggered"
112 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
113 outb_p(0x07, IO_DATA_PORT
);
114 outb_p(0xE6, IO_INDEX_PORT
);
115 outb_p(0x08, IO_DATA_PORT
);
118 /* lock the SuperIO chip */
119 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
121 spin_unlock_irqrestore(&spinlock
, flags
);
122 printk(KERN_INFO PFX
"activated.\n");
131 static int wdt977_stop(void)
134 spin_lock_irqsave(&spinlock
, flags
);
136 /* unlock the SuperIO chip */
137 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
138 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
140 /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
141 * F3 is reset to its default state
142 * F4 can clear the TIMEOUT'ed state (bit 0) - back to default
143 * We can not use GP17 as a PowerLed, as we use its usage as a RedLed
145 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
146 outb_p(0x08, IO_DATA_PORT
);
147 outb_p(0xF2, IO_INDEX_PORT
);
148 outb_p(0xFF, IO_DATA_PORT
);
149 outb_p(0xF3, IO_INDEX_PORT
);
150 outb_p(0x00, IO_DATA_PORT
);
151 outb_p(0xF4, IO_INDEX_PORT
);
152 outb_p(0x00, IO_DATA_PORT
);
153 outb_p(0xF2, IO_INDEX_PORT
);
154 outb_p(0x00, IO_DATA_PORT
);
156 /* at last select device Aux1 (dev=7) and set
157 GP16 as a watchdog output */
158 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
159 outb_p(0x07, IO_DATA_PORT
);
160 outb_p(0xE6, IO_INDEX_PORT
);
161 outb_p(0x08, IO_DATA_PORT
);
163 /* lock the SuperIO chip */
164 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
166 spin_unlock_irqrestore(&spinlock
, flags
);
167 printk(KERN_INFO PFX
"shutdown.\n");
173 * Send a keepalive ping to the watchdog
174 * This is done by simply re-writing the timeout to reg. 0xF2
177 static int wdt977_keepalive(void)
180 spin_lock_irqsave(&spinlock
, flags
);
182 /* unlock the SuperIO chip */
183 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
184 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
186 /* select device Aux2 (device=8) and kicks watchdog reg F2 */
187 /* F2 has the timeout in minutes */
188 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
189 outb_p(0x08, IO_DATA_PORT
);
190 outb_p(0xF2, IO_INDEX_PORT
);
191 outb_p(timeoutM
, IO_DATA_PORT
);
193 /* lock the SuperIO chip */
194 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
195 spin_unlock_irqrestore(&spinlock
, flags
);
201 * Set the watchdog timeout value
204 static int wdt977_set_timeout(int t
)
208 /* convert seconds to minutes, rounding up */
209 tmrval
= (t
+ 59) / 60;
211 if (machine_is_netwinder()) {
212 /* we have a hw bug somewhere, so each 977 minute is actually
213 * only 30sec. This limits the max timeout to half of device
214 * max of 255 minutes...
219 if (tmrval
< 1 || tmrval
> 255)
222 /* timeout is the timeout in seconds, timeoutM is
223 the timeout in minutes) */
230 * Get the watchdog status
233 static int wdt977_get_status(int *status
)
238 spin_lock_irqsave(&spinlock
, flags
);
240 /* unlock the SuperIO chip */
241 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
242 outb_p(UNLOCK_DATA
, IO_INDEX_PORT
);
244 /* select device Aux2 (device=8) and read watchdog reg F4 */
245 outb_p(DEVICE_REGISTER
, IO_INDEX_PORT
);
246 outb_p(0x08, IO_DATA_PORT
);
247 outb_p(0xF4, IO_INDEX_PORT
);
248 new_status
= inb_p(IO_DATA_PORT
);
250 /* lock the SuperIO chip */
251 outb_p(LOCK_DATA
, IO_INDEX_PORT
);
253 spin_unlock_irqrestore(&spinlock
, flags
);
257 *status
|= WDIOF_CARDRESET
;
264 * /dev/watchdog handling
267 static int wdt977_open(struct inode
*inode
, struct file
*file
)
269 /* If the watchdog is alive we don't need to start it again */
270 if (test_and_set_bit(0, &timer_alive
))
274 __module_get(THIS_MODULE
);
277 return nonseekable_open(inode
, file
);
280 static int wdt977_release(struct inode
*inode
, struct file
*file
)
283 * Shut off the timer.
284 * Lock it in if it's a module and we set nowayout
286 if (expect_close
== 42) {
288 clear_bit(0, &timer_alive
);
292 "Unexpected close, not stopping watchdog!\n");
301 * @file: file handle to the watchdog
302 * @buf: buffer to write (unused as data does not matter here
303 * @count: count of bytes
304 * @ppos: pointer to the position to write. No seeks allowed
306 * A write to a watchdog device is defined as a keepalive signal. Any
307 * write of data will do, as we we don't define content meaning.
310 static ssize_t
wdt977_write(struct file
*file
, const char __user
*buf
,
311 size_t count
, loff_t
*ppos
)
317 /* In case it was set long ago */
320 for (i
= 0; i
!= count
; i
++) {
322 if (get_user(c
, buf
+ i
))
329 /* someone wrote to us, we should restart timer */
335 static const struct watchdog_info ident
= {
336 .options
= WDIOF_SETTIMEOUT
|
339 .firmware_version
= 1,
340 .identity
= WATCHDOG_NAME
,
345 * @inode: inode of the device
346 * @file: file handle to the device
347 * @cmd: watchdog command
348 * @arg: argument pointer
350 * The watchdog API defines a common set of functions for all watchdogs
351 * according to their available features.
354 static long wdt977_ioctl(struct file
*file
, unsigned int cmd
,
358 int new_options
, retval
= -EINVAL
;
361 struct watchdog_info __user
*ident
;
365 uarg
.i
= (int __user
*)arg
;
368 case WDIOC_GETSUPPORT
:
369 return copy_to_user(uarg
.ident
, &ident
,
370 sizeof(ident
)) ? -EFAULT
: 0;
372 case WDIOC_GETSTATUS
:
373 wdt977_get_status(&status
);
374 return put_user(status
, uarg
.i
);
376 case WDIOC_GETBOOTSTATUS
:
377 return put_user(0, uarg
.i
);
379 case WDIOC_SETOPTIONS
:
380 if (get_user(new_options
, uarg
.i
))
383 if (new_options
& WDIOS_DISABLECARD
) {
388 if (new_options
& WDIOS_ENABLECARD
) {
395 case WDIOC_KEEPALIVE
:
399 case WDIOC_SETTIMEOUT
:
400 if (get_user(new_timeout
, uarg
.i
))
403 if (wdt977_set_timeout(new_timeout
))
409 case WDIOC_GETTIMEOUT
:
410 return put_user(timeout
, uarg
.i
);
418 static int wdt977_notify_sys(struct notifier_block
*this, unsigned long code
,
421 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
426 static const struct file_operations wdt977_fops
= {
427 .owner
= THIS_MODULE
,
429 .write
= wdt977_write
,
430 .unlocked_ioctl
= wdt977_ioctl
,
432 .release
= wdt977_release
,
435 static struct miscdevice wdt977_miscdev
= {
436 .minor
= WATCHDOG_MINOR
,
438 .fops
= &wdt977_fops
,
441 static struct notifier_block wdt977_notifier
= {
442 .notifier_call
= wdt977_notify_sys
,
445 static int __init
wd977_init(void)
449 printk(KERN_INFO PFX DRIVER_VERSION
);
451 /* Check that the timeout value is within its range;
452 if not reset to the default */
453 if (wdt977_set_timeout(timeout
)) {
454 wdt977_set_timeout(DEFAULT_TIMEOUT
);
456 "timeout value must be 60 < timeout < 15300, using %d\n",
460 /* on Netwinder the IOports are already reserved by
461 * arch/arm/mach-footbridge/netwinder-hw.c
463 if (!machine_is_netwinder()) {
464 if (!request_region(IO_INDEX_PORT
, 2, WATCHDOG_NAME
)) {
466 "I/O address 0x%04x already in use\n",
473 rc
= register_reboot_notifier(&wdt977_notifier
);
476 "cannot register reboot notifier (err=%d)\n", rc
);
480 rc
= misc_register(&wdt977_miscdev
);
483 "cannot register miscdev on minor=%d (err=%d)\n",
484 wdt977_miscdev
.minor
, rc
);
489 "initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
490 timeout
, nowayout
, testmode
);
495 unregister_reboot_notifier(&wdt977_notifier
);
497 if (!machine_is_netwinder())
498 release_region(IO_INDEX_PORT
, 2);
503 static void __exit
wd977_exit(void)
506 misc_deregister(&wdt977_miscdev
);
507 unregister_reboot_notifier(&wdt977_notifier
);
508 release_region(IO_INDEX_PORT
, 2);
511 module_init(wd977_init
);
512 module_exit(wd977_exit
);
514 MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
515 MODULE_DESCRIPTION("W83977AF Watchdog driver");
516 MODULE_LICENSE("GPL");
517 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);