2 * Industrial Computer Source WDT500/501 driver
4 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
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 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
21 * Dave Gregorich : Modularisation and minor bugs
22 * Alan Cox : Added the watchdog ioctl() stuff
23 * Alan Cox : Fixed the reboot problem (as noted by
25 * Alan Cox : Added wdt= boot option
26 * Alan Cox : Cleaned up copy/user stuff
27 * Tim Hockin : Added insmod parameters, comment cleanup
28 * Parameterized timeout
29 * Tigran Aivazian : Restructured wdt_init() to handle failures
30 * Joel Becker : Added WDIOC_GET/SETTIMEOUT
31 * Matt Domsch : Added nowayout module option
34 #include <linux/config.h>
35 #include <linux/interrupt.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/types.h>
39 #include <linux/miscdevice.h>
40 #include <linux/watchdog.h>
42 #include <linux/ioport.h>
43 #include <linux/notifier.h>
44 #include <linux/reboot.h>
45 #include <linux/init.h>
48 #include <asm/uaccess.h>
49 #include <asm/system.h>
52 static unsigned long wdt_is_open;
53 static char expect_close;
59 #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
61 static int heartbeat = WD_TIMO;
62 static int wd_heartbeat;
63 module_param(heartbeat, int, 0);
64 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WD_TIMO) ")");
66 #ifdef CONFIG_WATCHDOG_NOWAYOUT
67 static int nowayout = 1;
69 static int nowayout = 0;
72 module_param(nowayout, int, 0);
73 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
75 /* You must set these - there is no sane way to probe for this board. */
79 module_param(io, int, 0);
80 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
81 module_param(irq, int, 0);
82 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
85 /* Support for the Fan Tachometer on the WDT501-P */
86 static int tachometer;
88 module_param(tachometer, int, 0);
89 MODULE_PARM_DESC(tachometer, "WDT501-P Fan Tachometer support (0=disable, default=0)");
90 #endif /* CONFIG_WDT_501 */
96 static void wdt_ctr_mode(int ctr, int mode)
104 static void wdt_ctr_load(int ctr, int val)
106 outb_p(val&0xFF, WDT_COUNT0+ctr);
107 outb_p(val>>8, WDT_COUNT0+ctr);
113 * Start the watchdog driver.
116 static int wdt_start(void)
118 inb_p(WDT_DC); /* Disable watchdog */
119 wdt_ctr_mode(0,3); /* Program CTR0 for Mode 3: Square Wave Generator */
120 wdt_ctr_mode(1,2); /* Program CTR1 for Mode 2: Rate Generator */
121 wdt_ctr_mode(2,0); /* Program CTR2 for Mode 0: Pulse on Terminal Count */
122 wdt_ctr_load(0, 8948); /* Count at 100Hz */
123 wdt_ctr_load(1,wd_heartbeat); /* Heartbeat */
124 wdt_ctr_load(2,65535); /* Length of reset pulse */
125 outb_p(0, WDT_DC); /* Enable watchdog */
132 * Stop the watchdog driver.
135 static int wdt_stop (void)
137 /* Turn the card off */
138 inb_p(WDT_DC); /* Disable watchdog */
139 wdt_ctr_load(2,0); /* 0 length reset pulses now */
146 * Reload counter one with the watchdog heartbeat. We don't bother reloading
147 * the cascade counter.
150 static int wdt_ping(void)
152 /* Write a watchdog value */
153 inb_p(WDT_DC); /* Disable watchdog */
154 wdt_ctr_mode(1,2); /* Re-Program CTR1 for Mode 2: Rate Generator */
155 wdt_ctr_load(1,wd_heartbeat); /* Heartbeat */
156 outb_p(0, WDT_DC); /* Enable watchdog */
162 * @t: the new heartbeat value that needs to be set.
164 * Set a new heartbeat value for the watchdog device. If the heartbeat value is
165 * incorrect we keep the old value and return -EINVAL. If successfull we
168 static int wdt_set_heartbeat(int t)
170 if ((t < 1) || (t > 65535))
174 wd_heartbeat = t * 100;
180 * @status: the new status.
182 * Extract the status information from a WDT watchdog device. There are
183 * several board variants so we have to know which bits are valid. Some
184 * bits default to one and some to zero in order to be maximally painful.
186 * we then map the bits onto the status ioctl flags.
189 static int wdt_get_status(int *status)
191 unsigned char new_status=inb_p(WDT_SR);
194 if (new_status & WDC_SR_ISOI0)
195 *status |= WDIOF_EXTERN1;
196 if (new_status & WDC_SR_ISII1)
197 *status |= WDIOF_EXTERN2;
198 #ifdef CONFIG_WDT_501
199 if (!(new_status & WDC_SR_TGOOD))
200 *status |= WDIOF_OVERHEAT;
201 if (!(new_status & WDC_SR_PSUOVER))
202 *status |= WDIOF_POWEROVER;
203 if (!(new_status & WDC_SR_PSUUNDR))
204 *status |= WDIOF_POWERUNDER;
206 if (!(new_status & WDC_SR_FANGOOD))
207 *status |= WDIOF_FANFAULT;
209 #endif /* CONFIG_WDT_501 */
213 #ifdef CONFIG_WDT_501
215 * wdt_get_temperature:
217 * Reports the temperature in degrees Fahrenheit. The API is in
218 * farenheit. It was designed by an imperial measurement luddite.
221 static int wdt_get_temperature(int *temperature)
223 unsigned short c=inb_p(WDT_RT);
225 *temperature = (c * 11 / 15) + 7;
228 #endif /* CONFIG_WDT_501 */
232 * @irq: Interrupt number
233 * @dev_id: Unused as we don't allow multiple devices.
236 * Handle an interrupt from the board. These are raised when the status
237 * map changes in what the board considers an interesting way. That means
238 * a failure condition occurring.
241 static irqreturn_t wdt_interrupt(int irq, void *dev_id, struct pt_regs *regs)
244 * Read the status register see what is up and
247 unsigned char status=inb_p(WDT_SR);
249 printk(KERN_CRIT "WDT status %d\n", status);
251 #ifdef CONFIG_WDT_501
252 if (!(status & WDC_SR_TGOOD))
253 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
254 if (!(status & WDC_SR_PSUOVER))
255 printk(KERN_CRIT "PSU over voltage.\n");
256 if (!(status & WDC_SR_PSUUNDR))
257 printk(KERN_CRIT "PSU under voltage.\n");
259 if (!(status & WDC_SR_FANGOOD))
260 printk(KERN_CRIT "Possible fan fault.\n");
262 #endif /* CONFIG_WDT_501 */
263 if (!(status & WDC_SR_WCCR))
264 #ifdef SOFTWARE_REBOOT
266 printk(KERN_CRIT "Would Reboot.\n");
268 printk(KERN_CRIT "Initiating system reboot.\n");
272 printk(KERN_CRIT "Reset in 5ms.\n");
280 * @file: file handle to the watchdog
281 * @buf: buffer to write (unused as data does not matter here
282 * @count: count of bytes
283 * @ppos: pointer to the position to write. No seeks allowed
285 * A write to a watchdog device is defined as a keepalive signal. Any
286 * write of data will do, as we we don't define content meaning.
289 static ssize_t wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
295 /* In case it was set long ago */
298 for (i = 0; i != count; i++) {
300 if (get_user(c, buf + i))
313 * @inode: inode of the device
314 * @file: file handle to the device
315 * @cmd: watchdog command
316 * @arg: argument pointer
318 * The watchdog API defines a common set of functions for all watchdogs
319 * according to their available features. We only actually usefully support
320 * querying capabilities and current status.
323 static int wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
326 void __user *argp = (void __user *)arg;
327 int __user *p = argp;
331 static struct watchdog_info ident = {
332 .options = WDIOF_SETTIMEOUT|
335 .firmware_version = 1,
336 .identity = "WDT500/501",
339 /* Add options according to the card we have */
340 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
341 #ifdef CONFIG_WDT_501
342 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER);
344 ident.options |= WDIOF_FANFAULT;
345 #endif /* CONFIG_WDT_501 */
351 case WDIOC_GETSUPPORT:
352 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
354 case WDIOC_GETSTATUS:
355 wdt_get_status(&status);
356 return put_user(status, p);
357 case WDIOC_GETBOOTSTATUS:
358 return put_user(0, p);
359 case WDIOC_KEEPALIVE:
362 case WDIOC_SETTIMEOUT:
363 if (get_user(new_heartbeat, p))
366 if (wdt_set_heartbeat(new_heartbeat))
371 case WDIOC_GETTIMEOUT:
372 return put_user(heartbeat, p);
378 * @inode: inode of device
379 * @file: file handle to device
381 * The watchdog device has been opened. The watchdog device is single
382 * open and on opening we load the counters. Counter zero is a 100Hz
383 * cascade, into counter 1 which downcounts to reboot. When the counter
384 * triggers counter 2 downcounts the length of the reset pulse which
385 * set set to be as long as possible.
388 static int wdt_open(struct inode *inode, struct file *file)
390 if(test_and_set_bit(0, &wdt_is_open))
396 return nonseekable_open(inode, file);
401 * @inode: inode to board
402 * @file: file handle to board
404 * The watchdog has a configurable API. There is a religious dispute
405 * between people who want their watchdog to be able to shut down and
406 * those who want to be sure if the watchdog manager dies the machine
407 * reboots. In the former case we disable the counters, in the latter
408 * case you have to open it again very soon.
411 static int wdt_release(struct inode *inode, struct file *file)
413 if (expect_close == 42) {
415 clear_bit(0, &wdt_is_open);
417 printk(KERN_CRIT "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
424 #ifdef CONFIG_WDT_501
427 * @file: file handle to the watchdog board
428 * @buf: buffer to write 1 byte into
429 * @count: length of buffer
430 * @ptr: offset (no seek allowed)
432 * Temp_read reports the temperature in degrees Fahrenheit. The API is in
433 * farenheit. It was designed by an imperial measurement luddite.
436 static ssize_t wdt_temp_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
440 if (wdt_get_temperature(&temperature))
443 if (copy_to_user (buf, &temperature, 1))
451 * @inode: inode of device
452 * @file: file handle to device
454 * The temperature device has been opened.
457 static int wdt_temp_open(struct inode *inode, struct file *file)
459 return nonseekable_open(inode, file);
464 * @inode: inode to board
465 * @file: file handle to board
467 * The temperature device has been closed.
470 static int wdt_temp_release(struct inode *inode, struct file *file)
474 #endif /* CONFIG_WDT_501 */
478 * @this: our notifier block
479 * @code: the event being reported
482 * Our notifier is called on system shutdowns. We want to turn the card
483 * off at reboot otherwise the machine will reboot again during memory
484 * test or worse yet during the following fsck. This would suck, in fact
485 * trust me - if it happens it does suck.
488 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
491 if(code==SYS_DOWN || code==SYS_HALT) {
492 /* Turn the card off */
503 static struct file_operations wdt_fops = {
504 .owner = THIS_MODULE,
509 .release = wdt_release,
512 static struct miscdevice wdt_miscdev = {
513 .minor = WATCHDOG_MINOR,
518 #ifdef CONFIG_WDT_501
519 static struct file_operations wdt_temp_fops = {
520 .owner = THIS_MODULE,
522 .read = wdt_temp_read,
523 .open = wdt_temp_open,
524 .release = wdt_temp_release,
527 static struct miscdevice temp_miscdev = {
529 .name = "temperature",
530 .fops = &wdt_temp_fops,
532 #endif /* CONFIG_WDT_501 */
535 * The WDT card needs to learn about soft shutdowns in order to
536 * turn the timebomb registers off.
539 static struct notifier_block wdt_notifier = {
540 .notifier_call = wdt_notify_sys,
546 * Unload the watchdog. You cannot do this with any file handles open.
547 * If your watchdog is set to continue ticking on close and you unload
548 * it, well it keeps ticking. We won't get the interrupt but the board
549 * will not touch PC memory so all is fine. You just have to load a new
550 * module in 60 seconds or reboot.
553 static void __exit wdt_exit(void)
555 misc_deregister(&wdt_miscdev);
556 #ifdef CONFIG_WDT_501
557 misc_deregister(&temp_miscdev);
558 #endif /* CONFIG_WDT_501 */
559 unregister_reboot_notifier(&wdt_notifier);
561 release_region(io,8);
567 * Set up the WDT watchdog board. All we have to do is grab the
568 * resources we require and bitch if anyone beat us to them.
569 * The open() function will actually kick the board off.
572 static int __init wdt_init(void)
576 /* Check that the heartbeat value is within it's range ; if not reset to the default */
577 if (wdt_set_heartbeat(heartbeat)) {
578 wdt_set_heartbeat(WD_TIMO);
579 printk(KERN_INFO "wdt: heartbeat value must be 0<heartbeat<65536, using %d\n",
583 if (!request_region(io, 8, "wdt501p")) {
584 printk(KERN_ERR "wdt: I/O address 0x%04x already in use\n", io);
589 ret = request_irq(irq, wdt_interrupt, SA_INTERRUPT, "wdt501p", NULL);
591 printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq);
595 ret = register_reboot_notifier(&wdt_notifier);
597 printk(KERN_ERR "wdt: cannot register reboot notifier (err=%d)\n", ret);
601 #ifdef CONFIG_WDT_501
602 ret = misc_register(&temp_miscdev);
604 printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
608 #endif /* CONFIG_WDT_501 */
610 ret = misc_register(&wdt_miscdev);
612 printk(KERN_ERR "wdt: cannot register miscdev on minor=%d (err=%d)\n",
613 WATCHDOG_MINOR, ret);
618 printk(KERN_INFO "WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
619 io, irq, heartbeat, nowayout);
620 #ifdef CONFIG_WDT_501
621 printk(KERN_INFO "wdt: Fan Tachometer is %s\n", (tachometer ? "Enabled" : "Disabled"));
622 #endif /* CONFIG_WDT_501 */
628 #ifdef CONFIG_WDT_501
629 misc_deregister(&temp_miscdev);
631 #endif /* CONFIG_WDT_501 */
632 unregister_reboot_notifier(&wdt_notifier);
636 release_region(io,8);
640 module_init(wdt_init);
641 module_exit(wdt_exit);
643 MODULE_AUTHOR("Alan Cox");
644 MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
645 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
646 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
647 MODULE_LICENSE("GPL");