2 * Industrial Computer Source WDT501 driver
4 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
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
28 * cleanup, parameterized timeout
29 * Tigran Aivazian : Restructured wdt_init() to handle
31 * Joel Becker : Added WDIOC_GET/SETTIMEOUT
32 * Matt Domsch : Added nowayout module option
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/interrupt.h>
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/types.h>
41 #include <linux/miscdevice.h>
42 #include <linux/watchdog.h>
44 #include <linux/ioport.h>
45 #include <linux/notifier.h>
46 #include <linux/reboot.h>
47 #include <linux/init.h>
49 #include <linux/uaccess.h>
51 #include <asm/system.h>
54 static unsigned long wdt_is_open;
55 static char expect_close;
61 #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
63 static int heartbeat = WD_TIMO;
64 static int wd_heartbeat;
65 module_param(heartbeat, int, 0);
66 MODULE_PARM_DESC(heartbeat,
67 "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
68 __MODULE_STRING(WD_TIMO) ")");
70 static bool nowayout = WATCHDOG_NOWAYOUT;
71 module_param(nowayout, bool, 0);
72 MODULE_PARM_DESC(nowayout,
73 "Watchdog cannot be stopped once started (default="
74 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
76 /* You must set these - there is no sane way to probe for this board. */
77 static int io = 0x240;
80 static DEFINE_SPINLOCK(wdt_lock);
82 module_param(io, int, 0);
83 MODULE_PARM_DESC(io, "WDT io port (default=0x240)");
84 module_param(irq, int, 0);
85 MODULE_PARM_DESC(irq, "WDT irq (default=11)");
87 /* Support for the Fan Tachometer on the WDT501-P */
88 static int tachometer;
89 module_param(tachometer, int, 0);
90 MODULE_PARM_DESC(tachometer,
91 "WDT501-P Fan Tachometer support (0=disable, default=0)");
93 static int type = 500;
94 module_param(type, int, 0);
95 MODULE_PARM_DESC(type,
96 "WDT501-P Card type (500 or 501, default=500)");
102 static void wdt_ctr_mode(int ctr, int mode)
110 static void wdt_ctr_load(int ctr, int val)
112 outb_p(val&0xFF, WDT_COUNT0+ctr);
113 outb_p(val>>8, WDT_COUNT0+ctr);
119 * Start the watchdog driver.
122 static int wdt_start(void)
125 spin_lock_irqsave(&wdt_lock, flags);
126 inb_p(WDT_DC); /* Disable watchdog */
127 wdt_ctr_mode(0, 3); /* Program CTR0 for Mode 3:
128 Square Wave Generator */
129 wdt_ctr_mode(1, 2); /* Program CTR1 for Mode 2:
131 wdt_ctr_mode(2, 0); /* Program CTR2 for Mode 0:
132 Pulse on Terminal Count */
133 wdt_ctr_load(0, 8948); /* Count at 100Hz */
134 wdt_ctr_load(1, wd_heartbeat); /* Heartbeat */
135 wdt_ctr_load(2, 65535); /* Length of reset pulse */
136 outb_p(0, WDT_DC); /* Enable watchdog */
137 spin_unlock_irqrestore(&wdt_lock, flags);
144 * Stop the watchdog driver.
147 static int wdt_stop(void)
150 spin_lock_irqsave(&wdt_lock, flags);
151 /* Turn the card off */
152 inb_p(WDT_DC); /* Disable watchdog */
153 wdt_ctr_load(2, 0); /* 0 length reset pulses now */
154 spin_unlock_irqrestore(&wdt_lock, flags);
161 * Reload counter one with the watchdog heartbeat. We don't bother
162 * reloading the cascade counter.
165 static void wdt_ping(void)
168 spin_lock_irqsave(&wdt_lock, flags);
169 /* Write a watchdog value */
170 inb_p(WDT_DC); /* Disable watchdog */
171 wdt_ctr_mode(1, 2); /* Re-Program CTR1 for Mode 2:
173 wdt_ctr_load(1, wd_heartbeat); /* Heartbeat */
174 outb_p(0, WDT_DC); /* Enable watchdog */
175 spin_unlock_irqrestore(&wdt_lock, flags);
180 * @t: the new heartbeat value that needs to be set.
182 * Set a new heartbeat value for the watchdog device. If the heartbeat
183 * value is incorrect we keep the old value and return -EINVAL. If
184 * successful we return 0.
187 static int wdt_set_heartbeat(int t)
189 if (t < 1 || t > 65535)
193 wd_heartbeat = t * 100;
200 * Extract the status information from a WDT watchdog device. There are
201 * several board variants so we have to know which bits are valid. Some
202 * bits default to one and some to zero in order to be maximally painful.
204 * we then map the bits onto the status ioctl flags.
207 static int wdt_get_status(void)
209 unsigned char new_status;
213 spin_lock_irqsave(&wdt_lock, flags);
214 new_status = inb_p(WDT_SR);
215 spin_unlock_irqrestore(&wdt_lock, flags);
217 if (new_status & WDC_SR_ISOI0)
218 status |= WDIOF_EXTERN1;
219 if (new_status & WDC_SR_ISII1)
220 status |= WDIOF_EXTERN2;
222 if (!(new_status & WDC_SR_TGOOD))
223 status |= WDIOF_OVERHEAT;
224 if (!(new_status & WDC_SR_PSUOVER))
225 status |= WDIOF_POWEROVER;
226 if (!(new_status & WDC_SR_PSUUNDR))
227 status |= WDIOF_POWERUNDER;
229 if (!(new_status & WDC_SR_FANGOOD))
230 status |= WDIOF_FANFAULT;
237 * wdt_get_temperature:
239 * Reports the temperature in degrees Fahrenheit. The API is in
240 * farenheit. It was designed by an imperial measurement luddite.
243 static int wdt_get_temperature(void)
248 spin_lock_irqsave(&wdt_lock, flags);
250 spin_unlock_irqrestore(&wdt_lock, flags);
251 return (c * 11 / 15) + 7;
254 static void wdt_decode_501(int status)
256 if (!(status & WDC_SR_TGOOD))
257 pr_crit("Overheat alarm (%d)\n", inb_p(WDT_RT));
258 if (!(status & WDC_SR_PSUOVER))
259 pr_crit("PSU over voltage\n");
260 if (!(status & WDC_SR_PSUUNDR))
261 pr_crit("PSU under voltage\n");
266 * @irq: Interrupt number
267 * @dev_id: Unused as we don't allow multiple devices.
269 * Handle an interrupt from the board. These are raised when the status
270 * map changes in what the board considers an interesting way. That means
271 * a failure condition occurring.
274 static irqreturn_t wdt_interrupt(int irq, void *dev_id)
277 * Read the status register see what is up and
280 unsigned char status;
282 spin_lock(&wdt_lock);
283 status = inb_p(WDT_SR);
285 pr_crit("WDT status %d\n", status);
288 wdt_decode_501(status);
290 if (!(status & WDC_SR_FANGOOD))
291 pr_crit("Possible fan fault\n");
294 if (!(status & WDC_SR_WCCR)) {
295 #ifdef SOFTWARE_REBOOT
297 pr_crit("Would Reboot\n");
299 pr_crit("Initiating system reboot\n");
303 pr_crit("Reset in 5ms\n");
306 spin_unlock(&wdt_lock);
313 * @file: file handle to the watchdog
314 * @buf: buffer to write (unused as data does not matter here
315 * @count: count of bytes
316 * @ppos: pointer to the position to write. No seeks allowed
318 * A write to a watchdog device is defined as a keepalive signal. Any
319 * write of data will do, as we we don't define content meaning.
322 static ssize_t wdt_write(struct file *file, const char __user *buf,
323 size_t count, loff_t *ppos)
329 /* In case it was set long ago */
332 for (i = 0; i != count; i++) {
334 if (get_user(c, buf + i))
347 * @file: file handle to the device
348 * @cmd: watchdog command
349 * @arg: argument pointer
351 * The watchdog API defines a common set of functions for all watchdogs
352 * according to their available features. We only actually usefully support
353 * querying capabilities and current status.
356 static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
358 void __user *argp = (void __user *)arg;
359 int __user *p = argp;
363 struct watchdog_info ident = {
364 .options = WDIOF_SETTIMEOUT|
367 .firmware_version = 1,
368 .identity = "WDT500/501",
371 /* Add options according to the card we have */
372 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
374 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
377 ident.options |= WDIOF_FANFAULT;
381 case WDIOC_GETSUPPORT:
382 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
383 case WDIOC_GETSTATUS:
384 status = wdt_get_status();
385 return put_user(status, p);
386 case WDIOC_GETBOOTSTATUS:
387 return put_user(0, p);
388 case WDIOC_KEEPALIVE:
391 case WDIOC_SETTIMEOUT:
392 if (get_user(new_heartbeat, p))
394 if (wdt_set_heartbeat(new_heartbeat))
398 case WDIOC_GETTIMEOUT:
399 return put_user(heartbeat, p);
407 * @inode: inode of device
408 * @file: file handle to device
410 * The watchdog device has been opened. The watchdog device is single
411 * open and on opening we load the counters. Counter zero is a 100Hz
412 * cascade, into counter 1 which downcounts to reboot. When the counter
413 * triggers counter 2 downcounts the length of the reset pulse which
414 * set set to be as long as possible.
417 static int wdt_open(struct inode *inode, struct file *file)
419 if (test_and_set_bit(0, &wdt_is_open))
425 return nonseekable_open(inode, file);
430 * @inode: inode to board
431 * @file: file handle to board
433 * The watchdog has a configurable API. There is a religious dispute
434 * between people who want their watchdog to be able to shut down and
435 * those who want to be sure if the watchdog manager dies the machine
436 * reboots. In the former case we disable the counters, in the latter
437 * case you have to open it again very soon.
440 static int wdt_release(struct inode *inode, struct file *file)
442 if (expect_close == 42) {
444 clear_bit(0, &wdt_is_open);
446 pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
455 * @file: file handle to the watchdog board
456 * @buf: buffer to write 1 byte into
457 * @count: length of buffer
458 * @ptr: offset (no seek allowed)
460 * Temp_read reports the temperature in degrees Fahrenheit. The API is in
461 * farenheit. It was designed by an imperial measurement luddite.
464 static ssize_t wdt_temp_read(struct file *file, char __user *buf,
465 size_t count, loff_t *ptr)
467 int temperature = wdt_get_temperature();
469 if (copy_to_user(buf, &temperature, 1))
477 * @inode: inode of device
478 * @file: file handle to device
480 * The temperature device has been opened.
483 static int wdt_temp_open(struct inode *inode, struct file *file)
485 return nonseekable_open(inode, file);
490 * @inode: inode to board
491 * @file: file handle to board
493 * The temperature device has been closed.
496 static int wdt_temp_release(struct inode *inode, struct file *file)
503 * @this: our notifier block
504 * @code: the event being reported
507 * Our notifier is called on system shutdowns. We want to turn the card
508 * off at reboot otherwise the machine will reboot again during memory
509 * test or worse yet during the following fsck. This would suck, in fact
510 * trust me - if it happens it does suck.
513 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
516 if (code == SYS_DOWN || code == SYS_HALT)
526 static const struct file_operations wdt_fops = {
527 .owner = THIS_MODULE,
530 .unlocked_ioctl = wdt_ioctl,
532 .release = wdt_release,
535 static struct miscdevice wdt_miscdev = {
536 .minor = WATCHDOG_MINOR,
541 static const struct file_operations wdt_temp_fops = {
542 .owner = THIS_MODULE,
544 .read = wdt_temp_read,
545 .open = wdt_temp_open,
546 .release = wdt_temp_release,
549 static struct miscdevice temp_miscdev = {
551 .name = "temperature",
552 .fops = &wdt_temp_fops,
556 * The WDT card needs to learn about soft shutdowns in order to
557 * turn the timebomb registers off.
560 static struct notifier_block wdt_notifier = {
561 .notifier_call = wdt_notify_sys,
567 * Unload the watchdog. You cannot do this with any file handles open.
568 * If your watchdog is set to continue ticking on close and you unload
569 * it, well it keeps ticking. We won't get the interrupt but the board
570 * will not touch PC memory so all is fine. You just have to load a new
571 * module in 60 seconds or reboot.
574 static void __exit wdt_exit(void)
576 misc_deregister(&wdt_miscdev);
578 misc_deregister(&temp_miscdev);
579 unregister_reboot_notifier(&wdt_notifier);
581 release_region(io, 8);
587 * Set up the WDT watchdog board. All we have to do is grab the
588 * resources we require and bitch if anyone beat us to them.
589 * The open() function will actually kick the board off.
592 static int __init wdt_init(void)
596 if (type != 500 && type != 501) {
597 pr_err("unknown card type '%d'\n", type);
601 /* Check that the heartbeat value is within it's range;
602 if not reset to the default */
603 if (wdt_set_heartbeat(heartbeat)) {
604 wdt_set_heartbeat(WD_TIMO);
605 pr_info("heartbeat value must be 0 < heartbeat < 65536, using %d\n",
609 if (!request_region(io, 8, "wdt501p")) {
610 pr_err("I/O address 0x%04x already in use\n", io);
615 ret = request_irq(irq, wdt_interrupt, 0, "wdt501p", NULL);
617 pr_err("IRQ %d is not free\n", irq);
621 ret = register_reboot_notifier(&wdt_notifier);
623 pr_err("cannot register reboot notifier (err=%d)\n", ret);
628 ret = misc_register(&temp_miscdev);
630 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
636 ret = misc_register(&wdt_miscdev);
638 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
639 WATCHDOG_MINOR, ret);
643 pr_info("WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n",
644 io, irq, heartbeat, nowayout);
646 pr_info("Fan Tachometer is %s\n",
647 tachometer ? "Enabled" : "Disabled");
652 misc_deregister(&temp_miscdev);
654 unregister_reboot_notifier(&wdt_notifier);
658 release_region(io, 8);
663 module_init(wdt_init);
664 module_exit(wdt_exit);
666 MODULE_AUTHOR("Alan Cox");
667 MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)");
668 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
669 MODULE_ALIAS_MISCDEV(TEMP_MINOR);
670 MODULE_LICENSE("GPL");