2 * Industrial Computer Source PCI-WDT500/501 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 cleanup
28 * Parameterized timeout
29 * JP Nollmann : Added support for PCI wdt501p
30 * Alan Cox : Split ISA and PCI cards into two drivers
31 * Jeff Garzik : PCI cleanups
32 * Tigran Aivazian : Restructured wdtpci_init_one() to handle
34 * Joel Becker : Added WDIOC_GET/SETTIMEOUT
35 * Zwane Mwaikambo : Magic char closing, locking changes,
37 * Matt Domsch : nowayout module option
40 #include <linux/interrupt.h>
41 #include <linux/module.h>
42 #include <linux/moduleparam.h>
43 #include <linux/types.h>
44 #include <linux/miscdevice.h>
45 #include <linux/watchdog.h>
46 #include <linux/ioport.h>
47 #include <linux/delay.h>
48 #include <linux/notifier.h>
49 #include <linux/reboot.h>
50 #include <linux/init.h>
52 #include <linux/pci.h>
54 #include <linux/uaccess.h>
56 #include <asm/system.h>
61 #define PFX "wdt_pci: "
63 /* We can only use 1 card due to the /dev/watchdog restriction */
66 static unsigned long open_lock;
67 static DEFINE_SPINLOCK(wdtpci_lock);
68 static char expect_close;
70 static resource_size_t io;
74 #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
76 static int heartbeat = WD_TIMO;
77 static int wd_heartbeat;
78 module_param(heartbeat, int, 0);
79 MODULE_PARM_DESC(heartbeat,
80 "Watchdog heartbeat in seconds. (0<heartbeat<65536, default="
81 __MODULE_STRING(WD_TIMO) ")");
83 static int nowayout = WATCHDOG_NOWAYOUT;
84 module_param(nowayout, int, 0);
85 MODULE_PARM_DESC(nowayout,
86 "Watchdog cannot be stopped once started (default="
87 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
89 /* Support for the Fan Tachometer on the PCI-WDT501 */
90 static int tachometer;
91 module_param(tachometer, int, 0);
92 MODULE_PARM_DESC(tachometer,
93 "PCI-WDT501 Fan Tachometer support (0=disable, default=0)");
95 static int type = 500;
96 module_param(type, int, 0);
97 MODULE_PARM_DESC(type,
98 "PCI-WDT501 Card type (500 or 501 , default=500)");
101 * Programming support
104 static void wdtpci_ctr_mode(int ctr, int mode)
113 static void wdtpci_ctr_load(int ctr, int val)
115 outb(val & 0xFF, WDT_COUNT0 + ctr);
117 outb(val >> 8, WDT_COUNT0 + ctr);
124 * Start the watchdog driver.
127 static int wdtpci_start(void)
131 spin_lock_irqsave(&wdtpci_lock, flags);
134 * "pet" the watchdog, as Access says.
135 * This resets the clock outputs.
137 inb(WDT_DC); /* Disable watchdog */
139 wdtpci_ctr_mode(2, 0); /* Program CTR2 for Mode 0:
140 Pulse on Terminal Count */
141 outb(0, WDT_DC); /* Enable watchdog */
143 inb(WDT_DC); /* Disable watchdog */
145 outb(0, WDT_CLOCK); /* 2.0833MHz clock */
147 inb(WDT_BUZZER); /* disable */
149 inb(WDT_OPTONOTRST); /* disable */
151 inb(WDT_OPTORST); /* disable */
153 inb(WDT_PROGOUT); /* disable */
155 wdtpci_ctr_mode(0, 3); /* Program CTR0 for Mode 3:
156 Square Wave Generator */
157 wdtpci_ctr_mode(1, 2); /* Program CTR1 for Mode 2:
159 wdtpci_ctr_mode(2, 1); /* Program CTR2 for Mode 1:
160 Retriggerable One-Shot */
161 wdtpci_ctr_load(0, 20833); /* count at 100Hz */
162 wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */
163 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
164 outb(0, WDT_DC); /* Enable watchdog */
167 spin_unlock_irqrestore(&wdtpci_lock, flags);
174 * Stop the watchdog driver.
177 static int wdtpci_stop(void)
181 /* Turn the card off */
182 spin_lock_irqsave(&wdtpci_lock, flags);
183 inb(WDT_DC); /* Disable watchdog */
185 wdtpci_ctr_load(2, 0); /* 0 length reset pulses now */
186 spin_unlock_irqrestore(&wdtpci_lock, flags);
193 * Reload counter one with the watchdog heartbeat. We don't bother
194 * reloading the cascade counter.
197 static int wdtpci_ping(void)
201 spin_lock_irqsave(&wdtpci_lock, flags);
202 /* Write a watchdog value */
203 inb(WDT_DC); /* Disable watchdog */
205 wdtpci_ctr_mode(1, 2); /* Re-Program CTR1 for Mode 2:
207 wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */
208 outb(0, WDT_DC); /* Enable watchdog */
210 spin_unlock_irqrestore(&wdtpci_lock, flags);
215 * wdtpci_set_heartbeat:
216 * @t: the new heartbeat value that needs to be set.
218 * Set a new heartbeat value for the watchdog device. If the heartbeat
219 * value is incorrect we keep the old value and return -EINVAL.
220 * If successful we return 0.
222 static int wdtpci_set_heartbeat(int t)
224 /* Arbitrary, can't find the card's limits */
225 if (t < 1 || t > 65535)
229 wd_heartbeat = t * 100;
235 * @status: the new status.
237 * Extract the status information from a WDT watchdog device. There are
238 * several board variants so we have to know which bits are valid. Some
239 * bits default to one and some to zero in order to be maximally painful.
241 * we then map the bits onto the status ioctl flags.
244 static int wdtpci_get_status(int *status)
246 unsigned char new_status;
249 spin_lock_irqsave(&wdtpci_lock, flags);
250 new_status = inb(WDT_SR);
251 spin_unlock_irqrestore(&wdtpci_lock, flags);
254 if (new_status & WDC_SR_ISOI0)
255 *status |= WDIOF_EXTERN1;
256 if (new_status & WDC_SR_ISII1)
257 *status |= WDIOF_EXTERN2;
259 if (!(new_status & WDC_SR_TGOOD))
260 *status |= WDIOF_OVERHEAT;
261 if (!(new_status & WDC_SR_PSUOVER))
262 *status |= WDIOF_POWEROVER;
263 if (!(new_status & WDC_SR_PSUUNDR))
264 *status |= WDIOF_POWERUNDER;
266 if (!(new_status & WDC_SR_FANGOOD))
267 *status |= WDIOF_FANFAULT;
274 * wdtpci_get_temperature:
276 * Reports the temperature in degrees Fahrenheit. The API is in
277 * farenheit. It was designed by an imperial measurement luddite.
280 static int wdtpci_get_temperature(int *temperature)
284 spin_lock_irqsave(&wdtpci_lock, flags);
287 spin_unlock_irqrestore(&wdtpci_lock, flags);
288 *temperature = (c * 11 / 15) + 7;
294 * @irq: Interrupt number
295 * @dev_id: Unused as we don't allow multiple devices.
297 * Handle an interrupt from the board. These are raised when the status
298 * map changes in what the board considers an interesting way. That means
299 * a failure condition occurring.
302 static irqreturn_t wdtpci_interrupt(int irq, void *dev_id)
305 * Read the status register see what is up and
308 unsigned char status;
310 spin_lock(&wdtpci_lock);
312 status = inb(WDT_SR);
315 printk(KERN_CRIT PFX "status %d\n", status);
318 if (!(status & WDC_SR_TGOOD)) {
319 printk(KERN_CRIT PFX "Overheat alarm.(%d)\n",
323 if (!(status & WDC_SR_PSUOVER))
324 printk(KERN_CRIT PFX "PSU over voltage.\n");
325 if (!(status & WDC_SR_PSUUNDR))
326 printk(KERN_CRIT PFX "PSU under voltage.\n");
328 if (!(status & WDC_SR_FANGOOD))
329 printk(KERN_CRIT PFX "Possible fan fault.\n");
332 if (!(status & WDC_SR_WCCR)) {
333 #ifdef SOFTWARE_REBOOT
335 printk(KERN_CRIT PFX "Would Reboot.\n");
337 printk(KERN_CRIT PFX "Initiating system reboot.\n");
338 emergency_restart(NULL);
341 printk(KERN_CRIT PFX "Reset in 5ms.\n");
344 spin_unlock(&wdtpci_lock);
351 * @file: file handle to the watchdog
352 * @buf: buffer to write (unused as data does not matter here
353 * @count: count of bytes
354 * @ppos: pointer to the position to write. No seeks allowed
356 * A write to a watchdog device is defined as a keepalive signal. Any
357 * write of data will do, as we we don't define content meaning.
360 static ssize_t wdtpci_write(struct file *file, const char __user *buf,
361 size_t count, loff_t *ppos)
367 /* In case it was set long ago */
370 for (i = 0; i != count; i++) {
372 if (get_user(c, buf + i))
385 * @file: file handle to the device
386 * @cmd: watchdog command
387 * @arg: argument pointer
389 * The watchdog API defines a common set of functions for all watchdogs
390 * according to their available features. We only actually usefully support
391 * querying capabilities and current status.
394 static long wdtpci_ioctl(struct file *file, unsigned int cmd,
397 void __user *argp = (void __user *)arg;
398 int __user *p = argp;
402 struct watchdog_info ident = {
403 .options = WDIOF_SETTIMEOUT|
406 .firmware_version = 1,
407 .identity = "PCI-WDT500/501",
410 /* Add options according to the card we have */
411 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
413 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
416 ident.options |= WDIOF_FANFAULT;
420 case WDIOC_GETSUPPORT:
421 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
422 case WDIOC_GETSTATUS:
423 wdtpci_get_status(&status);
424 return put_user(status, p);
425 case WDIOC_GETBOOTSTATUS:
426 return put_user(0, p);
427 case WDIOC_KEEPALIVE:
430 case WDIOC_SETTIMEOUT:
431 if (get_user(new_heartbeat, p))
433 if (wdtpci_set_heartbeat(new_heartbeat))
437 case WDIOC_GETTIMEOUT:
438 return put_user(heartbeat, p);
446 * @inode: inode of device
447 * @file: file handle to device
449 * The watchdog device has been opened. The watchdog device is single
450 * open and on opening we load the counters. Counter zero is a 100Hz
451 * cascade, into counter 1 which downcounts to reboot. When the counter
452 * triggers counter 2 downcounts the length of the reset pulse which
453 * set set to be as long as possible.
456 static int wdtpci_open(struct inode *inode, struct file *file)
458 if (test_and_set_bit(0, &open_lock))
462 __module_get(THIS_MODULE);
467 return nonseekable_open(inode, file);
472 * @inode: inode to board
473 * @file: file handle to board
475 * The watchdog has a configurable API. There is a religious dispute
476 * between people who want their watchdog to be able to shut down and
477 * those who want to be sure if the watchdog manager dies the machine
478 * reboots. In the former case we disable the counters, in the latter
479 * case you have to open it again very soon.
482 static int wdtpci_release(struct inode *inode, struct file *file)
484 if (expect_close == 42) {
487 printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
491 clear_bit(0, &open_lock);
497 * @file: file handle to the watchdog board
498 * @buf: buffer to write 1 byte into
499 * @count: length of buffer
500 * @ptr: offset (no seek allowed)
502 * Read reports the temperature in degrees Fahrenheit. The API is in
503 * fahrenheit. It was designed by an imperial measurement luddite.
506 static ssize_t wdtpci_temp_read(struct file *file, char __user *buf,
507 size_t count, loff_t *ptr)
511 if (wdtpci_get_temperature(&temperature))
514 if (copy_to_user(buf, &temperature, 1))
522 * @inode: inode of device
523 * @file: file handle to device
525 * The temperature device has been opened.
528 static int wdtpci_temp_open(struct inode *inode, struct file *file)
530 return nonseekable_open(inode, file);
534 * wdtpci_temp_release:
535 * @inode: inode to board
536 * @file: file handle to board
538 * The temperature device has been closed.
541 static int wdtpci_temp_release(struct inode *inode, struct file *file)
548 * @this: our notifier block
549 * @code: the event being reported
552 * Our notifier is called on system shutdowns. We want to turn the card
553 * off at reboot otherwise the machine will reboot again during memory
554 * test or worse yet during the following fsck. This would suck, in fact
555 * trust me - if it happens it does suck.
558 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
561 if (code == SYS_DOWN || code == SYS_HALT)
571 static const struct file_operations wdtpci_fops = {
572 .owner = THIS_MODULE,
574 .write = wdtpci_write,
575 .unlocked_ioctl = wdtpci_ioctl,
577 .release = wdtpci_release,
580 static struct miscdevice wdtpci_miscdev = {
581 .minor = WATCHDOG_MINOR,
583 .fops = &wdtpci_fops,
586 static const struct file_operations wdtpci_temp_fops = {
587 .owner = THIS_MODULE,
589 .read = wdtpci_temp_read,
590 .open = wdtpci_temp_open,
591 .release = wdtpci_temp_release,
594 static struct miscdevice temp_miscdev = {
596 .name = "temperature",
597 .fops = &wdtpci_temp_fops,
601 * The WDT card needs to learn about soft shutdowns in order to
602 * turn the timebomb registers off.
605 static struct notifier_block wdtpci_notifier = {
606 .notifier_call = wdtpci_notify_sys,
610 static int __devinit wdtpci_init_one(struct pci_dev *dev,
611 const struct pci_device_id *ent)
617 printk(KERN_ERR PFX "This driver only supports one device\n");
621 if (type != 500 && type != 501) {
622 printk(KERN_ERR PFX "unknown card type '%d'.\n", type);
626 if (pci_enable_device(dev)) {
627 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
631 if (pci_resource_start(dev, 2) == 0x0000) {
632 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
637 if (pci_request_region(dev, 2, "wdt_pci")) {
638 printk(KERN_ERR PFX "I/O address 0x%llx already in use\n",
639 (unsigned long long)pci_resource_start(dev, 2));
644 io = pci_resource_start(dev, 2);
646 if (request_irq(irq, wdtpci_interrupt, IRQF_DISABLED | IRQF_SHARED,
647 "wdt_pci", &wdtpci_miscdev)) {
648 printk(KERN_ERR PFX "IRQ %d is not free\n", irq);
653 "PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%llx (Interrupt %d)\n",
654 (unsigned long long)io, irq);
656 /* Check that the heartbeat value is within its range;
657 if not reset to the default */
658 if (wdtpci_set_heartbeat(heartbeat)) {
659 wdtpci_set_heartbeat(WD_TIMO);
661 "heartbeat value must be 0 < heartbeat < 65536, using %d\n",
665 ret = register_reboot_notifier(&wdtpci_notifier);
668 "cannot register reboot notifier (err=%d)\n", ret);
673 ret = misc_register(&temp_miscdev);
676 "cannot register miscdev on minor=%d (err=%d)\n",
682 ret = misc_register(&wdtpci_miscdev);
685 "cannot register miscdev on minor=%d (err=%d)\n",
686 WATCHDOG_MINOR, ret);
690 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
691 heartbeat, nowayout);
693 printk(KERN_INFO "wdt: Fan Tachometer is %s\n",
694 (tachometer ? "Enabled" : "Disabled"));
702 misc_deregister(&temp_miscdev);
704 unregister_reboot_notifier(&wdtpci_notifier);
706 free_irq(irq, &wdtpci_miscdev);
708 pci_release_region(dev, 2);
710 pci_disable_device(dev);
715 static void __devexit wdtpci_remove_one(struct pci_dev *pdev)
717 /* here we assume only one device will ever have
718 * been picked up and registered by probe function */
719 misc_deregister(&wdtpci_miscdev);
721 misc_deregister(&temp_miscdev);
722 unregister_reboot_notifier(&wdtpci_notifier);
723 free_irq(irq, &wdtpci_miscdev);
724 pci_release_region(pdev, 2);
725 pci_disable_device(pdev);
730 static DEFINE_PCI_DEVICE_TABLE(wdtpci_pci_tbl) = {
732 .vendor = PCI_VENDOR_ID_ACCESSIO,
733 .device = PCI_DEVICE_ID_ACCESSIO_WDG_CSM,
734 .subvendor = PCI_ANY_ID,
735 .subdevice = PCI_ANY_ID,
737 { 0, }, /* terminate list */
739 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
742 static struct pci_driver wdtpci_driver = {
744 .id_table = wdtpci_pci_tbl,
745 .probe = wdtpci_init_one,
746 .remove = __devexit_p(wdtpci_remove_one),
753 * Unload the watchdog. You cannot do this with any file handles open.
754 * If your watchdog is set to continue ticking on close and you unload
755 * it, well it keeps ticking. We won't get the interrupt but the board
756 * will not touch PC memory so all is fine. You just have to load a new
757 * module in xx seconds or reboot.
760 static void __exit wdtpci_cleanup(void)
762 pci_unregister_driver(&wdtpci_driver);
769 * Set up the WDT watchdog board. All we have to do is grab the
770 * resources we require and bitch if anyone beat us to them.
771 * The open() function will actually kick the board off.
774 static int __init wdtpci_init(void)
776 return pci_register_driver(&wdtpci_driver);
780 module_init(wdtpci_init);
781 module_exit(wdtpci_cleanup);
783 MODULE_AUTHOR("JP Nollmann, Alan Cox");
784 MODULE_DESCRIPTION("Driver for the ICS PCI-WDT500/501 watchdog cards");
785 MODULE_LICENSE("GPL");
786 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
787 MODULE_ALIAS_MISCDEV(TEMP_MINOR);