File: /usr/src/linux/drivers/char/w83877f_wdt.c
1 /*
2 * W83877F Computer Watchdog Timer driver for Linux 2.4.x
3 *
4 * Based on acquirewdt.c by Alan Cox,
5 * and sbc60xxwdt.c by Jakob Oestergaard <jakob@ostenfeld.dk>
6 *
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.
11 *
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.
15 *
16 * (c) Copyright 2001 Scott Jennings <management@oro.net>
17 *
18 * 4/19 - 2001 [Initial revision]
19 *
20 *
21 * Theory of operation:
22 * A Watchdog Timer (WDT) is a hardware circuit that can
23 * reset the computer system in case of a software fault.
24 * You probably knew that already.
25 *
26 * Usually a userspace daemon will notify the kernel WDT driver
27 * via the /proc/watchdog special device file that userspace is
28 * still alive, at regular intervals. When such a notification
29 * occurs, the driver will usually tell the hardware watchdog
30 * that everything is in order, and that the watchdog should wait
31 * for yet another little while to reset the system.
32 * If userspace fails (RAM error, kernel bug, whatever), the
33 * notifications cease to occur, and the hardware watchdog will
34 * reset the system (causing a reboot) after the timeout occurs.
35 *
36 * This WDT driver is different from most other Linux WDT
37 * drivers in that the driver will ping the watchdog by itself,
38 * because this particular WDT has a very short timeout (1.6
39 * seconds) and it would be insane to count on any userspace
40 * daemon always getting scheduled within that time frame.
41 */
42
43 #include <linux/module.h>
44 #include <linux/version.h>
45 #include <linux/types.h>
46 #include <linux/errno.h>
47 #include <linux/kernel.h>
48 #include <linux/timer.h>
49 #include <linux/sched.h>
50 #include <linux/miscdevice.h>
51 #include <linux/watchdog.h>
52 #include <linux/slab.h>
53 #include <linux/ioport.h>
54 #include <linux/fcntl.h>
55 #include <linux/smp_lock.h>
56 #include <asm/io.h>
57 #include <asm/uaccess.h>
58 #include <asm/system.h>
59 #include <linux/notifier.h>
60 #include <linux/reboot.h>
61 #include <linux/init.h>
62
63 #define OUR_NAME "w83877f_wdt"
64
65 #define ENABLE_W83877F_PORT 0x3F0
66 #define ENABLE_W83877F 0x87
67 #define DISABLE_W83877F 0xAA
68 #define WDT_PING 0x443
69 #define WDT_REGISTER 0x14
70 #define WDT_ENABLE 0x9C
71 #define WDT_DISABLE 0x8C
72
73 /*
74 * The W83877F seems to be fixed at 1.6s timeout (at least on the
75 * EMACS PC-104 board I'm using). If we reset the watchdog every
76 * ~250ms we should be safe. */
77
78 #define WDT_INTERVAL (HZ/4+1)
79
80 /*
81 * We must not require too good response from the userspace daemon.
82 * Here we require the userspace daemon to send us a heartbeat
83 * char to /dev/watchdog every 30 seconds.
84 */
85
86 #define WDT_HEARTBEAT (HZ * 30)
87
88 static void wdt_timer_ping(unsigned long);
89 static struct timer_list timer;
90 static unsigned long next_heartbeat;
91 static int wdt_is_open;
92 static int wdt_expect_close;
93
94 /*
95 * Whack the dog
96 */
97
98 static void wdt_timer_ping(unsigned long data)
99 {
100 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
101 * we agree to ping the WDT
102 */
103 if(time_before(jiffies, next_heartbeat))
104 {
105 /* Ping the WDT by reading from WDT_PING */
106 inb_p(WDT_PING);
107 /* Re-set the timer interval */
108 timer.expires = jiffies + WDT_INTERVAL;
109 add_timer(&timer);
110 } else {
111 printk(OUR_NAME ": Heartbeat lost! Will not ping the watchdog\n");
112 }
113 }
114
115 /*
116 * Utility routines
117 */
118
119 static void wdt_change(int writeval)
120 {
121 /* buy some time */
122 inb_p(WDT_PING);
123
124 /* make W83877F available */
125 outb_p(ENABLE_W83877F,ENABLE_W83877F_PORT);
126 outb_p(ENABLE_W83877F,ENABLE_W83877F_PORT);
127
128 /* enable watchdog */
129 outb_p(WDT_REGISTER,ENABLE_W83877F_PORT);
130 outb_p(writeval,ENABLE_W83877F_PORT+1);
131
132 /* lock the W8387FF away */
133 outb_p(DISABLE_W83877F,ENABLE_W83877F_PORT);
134 }
135
136 static void wdt_startup(void)
137 {
138 next_heartbeat = jiffies + WDT_HEARTBEAT;
139
140 /* Start the timer */
141 timer.expires = jiffies + WDT_INTERVAL;
142 add_timer(&timer);
143
144 wdt_change(WDT_ENABLE);
145
146 printk(OUR_NAME ": Watchdog timer is now enabled.\n");
147 }
148
149 static void wdt_turnoff(void)
150 {
151 /* Stop the timer */
152 del_timer(&timer);
153
154 wdt_change(WDT_DISABLE);
155
156 printk(OUR_NAME ": Watchdog timer is now disabled...\n");
157 }
158
159
160 /*
161 * /dev/watchdog handling
162 */
163
164 static ssize_t fop_write(struct file * file, const char * buf, size_t count, loff_t * ppos)
165 {
166 /* We can't seek */
167 if(ppos != &file->f_pos)
168 return -ESPIPE;
169
170 /* See if we got the magic character */
171 if(count)
172 {
173 size_t ofs;
174
175 /* note: just in case someone wrote the magic character
176 * five months ago... */
177 wdt_expect_close = 0;
178
179 /* now scan */
180 for(ofs = 0; ofs != count; ofs++)
181 if(buf[ofs] == 'V')
182 wdt_expect_close = 1;
183
184 /* someone wrote to us, we should restart timer */
185 next_heartbeat = jiffies + WDT_HEARTBEAT;
186 return 1;
187 };
188 return 0;
189 }
190
191 static ssize_t fop_read(struct file * file, char * buf, size_t count, loff_t * ppos)
192 {
193 /* No can do */
194 return -EINVAL;
195 }
196
197 static int fop_open(struct inode * inode, struct file * file)
198 {
199 switch(MINOR(inode->i_rdev))
200 {
201 case WATCHDOG_MINOR:
202 /* Just in case we're already talking to someone... */
203 if(wdt_is_open)
204 return -EBUSY;
205 /* Good, fire up the show */
206 wdt_is_open = 1;
207 wdt_startup();
208 return 0;
209
210 default:
211 return -ENODEV;
212 }
213 }
214
215 static int fop_close(struct inode * inode, struct file * file)
216 {
217 lock_kernel();
218 if(MINOR(inode->i_rdev) == WATCHDOG_MINOR)
219 {
220 if(wdt_expect_close)
221 wdt_turnoff();
222 else {
223 del_timer(&timer);
224 printk(OUR_NAME ": device file closed unexpectedly. Will not stop the WDT!\n");
225 }
226 }
227 wdt_is_open = 0;
228 unlock_kernel();
229 return 0;
230 }
231
232 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
233 unsigned long arg)
234 {
235 static struct watchdog_info ident=
236 {
237 0,
238 1,
239 "W83877F"
240 };
241
242 switch(cmd)
243 {
244 default:
245 return -ENOIOCTLCMD;
246 case WDIOC_GETSUPPORT:
247 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
248 case WDIOC_KEEPALIVE:
249 next_heartbeat = jiffies + WDT_HEARTBEAT;
250 return 0;
251 }
252 }
253
254 static struct file_operations wdt_fops = {
255 owner: THIS_MODULE,
256 llseek: no_llseek,
257 read: fop_read,
258 write: fop_write,
259 open: fop_open,
260 release: fop_close,
261 ioctl: fop_ioctl
262 };
263
264 static struct miscdevice wdt_miscdev = {
265 WATCHDOG_MINOR,
266 "watchdog",
267 &wdt_fops
268 };
269
270 /*
271 * Notifier for system down
272 */
273
274 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
275 void *unused)
276 {
277 if(code==SYS_DOWN || code==SYS_HALT)
278 wdt_turnoff();
279 return NOTIFY_DONE;
280 }
281
282 /*
283 * The WDT needs to learn about soft shutdowns in order to
284 * turn the timebomb registers off.
285 */
286
287 static struct notifier_block wdt_notifier=
288 {
289 wdt_notify_sys,
290 0,
291 0
292 };
293
294 static void __exit w83877f_wdt_unload(void)
295 {
296 wdt_turnoff();
297
298 /* Deregister */
299 misc_deregister(&wdt_miscdev);
300
301 unregister_reboot_notifier(&wdt_notifier);
302 release_region(WDT_PING,1);
303 release_region(ENABLE_W83877F_PORT,2);
304 }
305
306 static int __init w83877f_wdt_init(void)
307 {
308 int rc = -EBUSY;
309
310 if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT"))
311 goto err_out;
312 if (!request_region(WDT_PING, 1, "W8387FF WDT"))
313 goto err_out_region1;
314
315 init_timer(&timer);
316 timer.function = wdt_timer_ping;
317 timer.data = 0;
318
319 rc = misc_register(&wdt_miscdev);
320 if (rc)
321 goto err_out_region2;
322
323 rc = register_reboot_notifier(&wdt_notifier);
324 if (rc)
325 goto err_out_miscdev;
326
327 printk(KERN_INFO OUR_NAME ": WDT driver for W83877F initialised.\n");
328
329 return 0;
330
331 err_out_miscdev:
332 misc_deregister(&wdt_miscdev);
333 err_out_region2:
334 release_region(WDT_PING,1);
335 err_out_region1:
336 release_region(ENABLE_W83877F_PORT,2);
337 err_out:
338 return rc;
339 }
340
341 module_init(w83877f_wdt_init);
342 module_exit(w83877f_wdt_unload);
343
344 MODULE_LICENSE("GPL");
345