2 * W83877F Computer Watchdog Timer driver for Linux 2.4.x
4 * Based on acquirewdt.c by Alan Cox,
5 * and sbc60xxwdt.c by Jakob Oestergaard <jakob@ostenfeld.dk>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * The authors do NOT admit liability nor provide warranty for
13 * any of this software. This material is provided "AS-IS" in
14 * the hope that it may be useful for others.
16 * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net>
18 * 4/19 - 2001 [Initial revision]
19 * 9/27 - 2001 Added spinlocking
22 * Theory of operation:
23 * A Watchdog Timer (WDT) is a hardware circuit that can
24 * reset the computer system in case of a software fault.
25 * You probably knew that already.
27 * Usually a userspace daemon will notify the kernel WDT driver
28 * via the /proc/watchdog special device file that userspace is
29 * still alive, at regular intervals. When such a notification
30 * occurs, the driver will usually tell the hardware watchdog
31 * that everything is in order, and that the watchdog should wait
32 * for yet another little while to reset the system.
33 * If userspace fails (RAM error, kernel bug, whatever), the
34 * notifications cease to occur, and the hardware watchdog will
35 * reset the system (causing a reboot) after the timeout occurs.
37 * This WDT driver is different from most other Linux WDT
38 * drivers in that the driver will ping the watchdog by itself,
39 * because this particular WDT has a very short timeout (1.6
40 * seconds) and it would be insane to count on any userspace
41 * daemon always getting scheduled within that time frame.
44 #include <linux/module.h>
45 #include <linux/version.h>
46 #include <linux/types.h>
47 #include <linux/errno.h>
48 #include <linux/kernel.h>
49 #include <linux/timer.h>
50 #include <linux/sched.h>
51 #include <linux/miscdevice.h>
52 #include <linux/watchdog.h>
53 #include <linux/slab.h>
54 #include <linux/ioport.h>
55 #include <linux/fcntl.h>
56 #include <linux/smp_lock.h>
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
60 #include <linux/notifier.h>
61 #include <linux/reboot.h>
62 #include <linux/init.h>
64 #define OUR_NAME "w83877f_wdt"
66 #define ENABLE_W83877F_PORT 0x3F0
67 #define ENABLE_W83877F 0x87
68 #define DISABLE_W83877F 0xAA
69 #define WDT_PING 0x443
70 #define WDT_REGISTER 0x14
71 #define WDT_ENABLE 0x9C
72 #define WDT_DISABLE 0x8C
75 * The W83877F seems to be fixed at 1.6s timeout (at least on the
76 * EMACS PC-104 board I'm using). If we reset the watchdog every
77 * ~250ms we should be safe. */
79 #define WDT_INTERVAL (HZ/4+1)
82 * We must not require too good response from the userspace daemon.
83 * Here we require the userspace daemon to send us a heartbeat
84 * char to /dev/watchdog every 30 seconds.
87 #define WDT_HEARTBEAT (HZ * 30)
89 static void wdt_timer_ping(unsigned long);
90 static struct timer_list timer;
91 static unsigned long next_heartbeat;
92 static unsigned long wdt_is_open;
93 static int wdt_expect_close;
94 static spinlock_t wdt_spinlock;
100 static void wdt_timer_ping(unsigned long data)
102 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
103 * we agree to ping the WDT
105 if(time_before(jiffies, next_heartbeat))
108 spin_lock(&wdt_spinlock);
110 /* Ping the WDT by reading from WDT_PING */
113 /* Re-set the timer interval */
114 timer.expires = jiffies + WDT_INTERVAL;
117 spin_unlock(&wdt_spinlock);
120 printk(OUR_NAME ": Heartbeat lost! Will not ping the watchdog\n");
128 static void wdt_change(int writeval)
131 spin_lock_irqsave(&wdt_spinlock, flags);
136 /* make W83877F available */
137 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
138 outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT);
140 /* enable watchdog */
141 outb_p(WDT_REGISTER, ENABLE_W83877F_PORT);
142 outb_p(writeval, ENABLE_W83877F_PORT+1);
144 /* lock the W8387FF away */
145 outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT);
147 spin_unlock_irqrestore(&wdt_spinlock, flags);
150 static void wdt_startup(void)
152 next_heartbeat = jiffies + WDT_HEARTBEAT;
154 /* Start the timer */
155 timer.expires = jiffies + WDT_INTERVAL;
158 wdt_change(WDT_ENABLE);
160 printk(OUR_NAME ": Watchdog timer is now enabled.\n");
163 static void wdt_turnoff(void)
168 wdt_change(WDT_DISABLE);
170 printk(OUR_NAME ": Watchdog timer is now disabled...\n");
175 * /dev/watchdog handling
178 static ssize_t fop_write(struct file * file, const char * buf, size_t count, loff_t * ppos)
181 if(ppos != &file->f_pos)
184 /* See if we got the magic character */
189 /* note: just in case someone wrote the magic character
190 * five months ago... */
191 wdt_expect_close = 0;
194 for(ofs = 0; ofs != count; ofs++)
197 if(get_user(c, buf + ofs))
200 wdt_expect_close = 1;
203 /* someone wrote to us, we should restart timer */
204 next_heartbeat = jiffies + WDT_HEARTBEAT;
210 static ssize_t fop_read(struct file * file, char * buf, size_t count, loff_t * ppos)
216 static int fop_open(struct inode * inode, struct file * file)
218 switch(MINOR(inode->i_rdev))
221 /* Just in case we're already talking to someone... */
222 if(test_and_set_bit(0, &wdt_is_open)) {
223 spin_unlock(&wdt_spinlock);
226 /* Good, fire up the show */
235 static int fop_close(struct inode * inode, struct file * file)
237 if(MINOR(inode->i_rdev) == WATCHDOG_MINOR)
243 printk(OUR_NAME ": device file closed unexpectedly. Will not stop the WDT!\n");
246 clear_bit(0, &wdt_is_open);
250 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
252 static struct watchdog_info ident=
263 case WDIOC_GETSUPPORT:
264 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
265 case WDIOC_KEEPALIVE:
266 next_heartbeat = jiffies + WDT_HEARTBEAT;
271 static struct file_operations wdt_fops = {
281 static struct miscdevice wdt_miscdev = {
288 * Notifier for system down
291 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
294 if(code==SYS_DOWN || code==SYS_HALT)
300 * The WDT needs to learn about soft shutdowns in order to
301 * turn the timebomb registers off.
304 static struct notifier_block wdt_notifier=
311 static void __exit w83877f_wdt_unload(void)
316 misc_deregister(&wdt_miscdev);
318 unregister_reboot_notifier(&wdt_notifier);
319 release_region(WDT_PING,1);
320 release_region(ENABLE_W83877F_PORT,2);
323 static int __init w83877f_wdt_init(void)
327 spin_lock_init(&wdt_spinlock);
329 if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT"))
331 if (!request_region(WDT_PING, 1, "W8387FF WDT"))
332 goto err_out_region1;
335 timer.function = wdt_timer_ping;
338 rc = misc_register(&wdt_miscdev);
340 goto err_out_region2;
342 rc = register_reboot_notifier(&wdt_notifier);
344 goto err_out_miscdev;
346 printk(KERN_INFO OUR_NAME ": WDT driver for W83877F initialised.\n");
351 misc_deregister(&wdt_miscdev);
353 release_region(WDT_PING,1);
355 release_region(ENABLE_W83877F_PORT,2);
360 module_init(w83877f_wdt_init);
361 module_exit(w83877f_wdt_unload);
363 MODULE_AUTHOR("Scott and Bill Jennings");
364 MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip");
365 MODULE_LICENSE("GPL");