2 * Industrial Computer Source PCI-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 * 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 failures
33 * Joel Becker : Added WDIOC_GET/SETTIMEOUT
34 * Zwane Mwaikambo : Magic char closing, locking changes, cleanups
35 * Matt Domsch : nowayout module option
38 #include <linux/config.h>
39 #include <linux/interrupt.h>
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/types.h>
43 #include <linux/miscdevice.h>
44 #include <linux/watchdog.h>
45 #include <linux/ioport.h>
46 #include <linux/notifier.h>
47 #include <linux/reboot.h>
48 #include <linux/init.h>
50 #include <linux/pci.h>
53 #include <asm/uaccess.h>
54 #include <asm/system.h>
59 #define PFX "wdt_pci: "
62 * Until Access I/O gets their application for a PCI vendor ID approved,
63 * I don't think that it's appropriate to move these constants into the
64 * regular pci_ids.h file. -- JPN 2000/01/18
67 #ifndef PCI_VENDOR_ID_ACCESSIO
68 #define PCI_VENDOR_ID_ACCESSIO 0x494f
70 #ifndef PCI_DEVICE_ID_WDG_CSM
71 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
74 /* We can only use 1 card due to the /dev/watchdog restriction */
77 static struct semaphore open_sem;
78 static spinlock_t wdtpci_lock;
79 static char expect_close;
85 #define WD_TIMO 60 /* Default heartbeat = 60 seconds */
87 static int heartbeat = WD_TIMO;
88 static int wd_heartbeat;
89 module_param(heartbeat, int, 0);
90 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WD_TIMO) ")");
92 #ifdef CONFIG_WATCHDOG_NOWAYOUT
93 static int nowayout = 1;
95 static int nowayout = 0;
98 module_param(nowayout, int, 0);
99 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
101 #ifdef CONFIG_WDT_501_PCI
102 /* Support for the Fan Tachometer on the PCI-WDT501 */
103 static int tachometer;
105 module_param(tachometer, int, 0);
106 MODULE_PARM_DESC(tachometer, "PCI-WDT501 Fan Tachometer support (0=disable, default=0)");
107 #endif /* CONFIG_WDT_501_PCI */
110 * Programming support
113 static void wdtpci_ctr_mode(int ctr, int mode)
121 static void wdtpci_ctr_load(int ctr, int val)
123 outb_p(val&0xFF, WDT_COUNT0+ctr);
124 outb_p(val>>8, WDT_COUNT0+ctr);
130 * Start the watchdog driver.
133 static int wdtpci_start(void)
137 spin_lock_irqsave(&wdtpci_lock, flags);
140 * "pet" the watchdog, as Access says.
141 * This resets the clock outputs.
143 inb_p(WDT_DC); /* Disable watchdog */
144 wdtpci_ctr_mode(2,0); /* Program CTR2 for Mode 0: Pulse on Terminal Count */
145 outb_p(0, WDT_DC); /* Enable watchdog */
147 inb_p(WDT_DC); /* Disable watchdog */
148 outb_p(0, WDT_CLOCK); /* 2.0833MHz clock */
149 inb_p(WDT_BUZZER); /* disable */
150 inb_p(WDT_OPTONOTRST); /* disable */
151 inb_p(WDT_OPTORST); /* disable */
152 inb_p(WDT_PROGOUT); /* disable */
153 wdtpci_ctr_mode(0,3); /* Program CTR0 for Mode 3: Square Wave Generator */
154 wdtpci_ctr_mode(1,2); /* Program CTR1 for Mode 2: Rate Generator */
155 wdtpci_ctr_mode(2,1); /* Program CTR2 for Mode 1: Retriggerable One-Shot */
156 wdtpci_ctr_load(0,20833); /* count at 100Hz */
157 wdtpci_ctr_load(1,wd_heartbeat);/* Heartbeat */
158 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
159 outb_p(0, WDT_DC); /* Enable watchdog */
161 spin_unlock_irqrestore(&wdtpci_lock, flags);
168 * Stop the watchdog driver.
171 static int wdtpci_stop (void)
175 /* Turn the card off */
176 spin_lock_irqsave(&wdtpci_lock, flags);
177 inb_p(WDT_DC); /* Disable watchdog */
178 wdtpci_ctr_load(2,0); /* 0 length reset pulses now */
179 spin_unlock_irqrestore(&wdtpci_lock, flags);
186 * Reload counter one with the watchdog heartbeat. We don't bother reloading
187 * the cascade counter.
190 static int wdtpci_ping(void)
194 /* Write a watchdog value */
195 spin_lock_irqsave(&wdtpci_lock, flags);
196 inb_p(WDT_DC); /* Disable watchdog */
197 wdtpci_ctr_mode(1,2); /* Re-Program CTR1 for Mode 2: Rate Generator */
198 wdtpci_ctr_load(1,wd_heartbeat);/* Heartbeat */
199 outb_p(0, WDT_DC); /* Enable watchdog */
200 spin_unlock_irqrestore(&wdtpci_lock, flags);
205 * wdtpci_set_heartbeat:
206 * @t: the new heartbeat value that needs to be set.
208 * Set a new heartbeat value for the watchdog device. If the heartbeat value is
209 * incorrect we keep the old value and return -EINVAL. If successfull we
212 static int wdtpci_set_heartbeat(int t)
214 /* Arbitrary, can't find the card's limits */
215 if ((t < 1) || (t > 65535))
219 wd_heartbeat = t * 100;
225 * @status: the new status.
227 * Extract the status information from a WDT watchdog device. There are
228 * several board variants so we have to know which bits are valid. Some
229 * bits default to one and some to zero in order to be maximally painful.
231 * we then map the bits onto the status ioctl flags.
234 static int wdtpci_get_status(int *status)
236 unsigned char new_status=inb_p(WDT_SR);
239 if (new_status & WDC_SR_ISOI0)
240 *status |= WDIOF_EXTERN1;
241 if (new_status & WDC_SR_ISII1)
242 *status |= WDIOF_EXTERN2;
243 #ifdef CONFIG_WDT_501_PCI
244 if (!(new_status & WDC_SR_TGOOD))
245 *status |= WDIOF_OVERHEAT;
246 if (!(new_status & WDC_SR_PSUOVER))
247 *status |= WDIOF_POWEROVER;
248 if (!(new_status & WDC_SR_PSUUNDR))
249 *status |= WDIOF_POWERUNDER;
251 if (!(new_status & WDC_SR_FANGOOD))
252 *status |= WDIOF_FANFAULT;
254 #endif /* CONFIG_WDT_501_PCI */
258 #ifdef CONFIG_WDT_501_PCI
260 * wdtpci_get_temperature:
262 * Reports the temperature in degrees Fahrenheit. The API is in
263 * farenheit. It was designed by an imperial measurement luddite.
266 static int wdtpci_get_temperature(int *temperature)
268 unsigned short c=inb_p(WDT_RT);
270 *temperature = (c * 11 / 15) + 7;
273 #endif /* CONFIG_WDT_501_PCI */
277 * @irq: Interrupt number
278 * @dev_id: Unused as we don't allow multiple devices.
281 * Handle an interrupt from the board. These are raised when the status
282 * map changes in what the board considers an interesting way. That means
283 * a failure condition occurring.
286 static irqreturn_t wdtpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
289 * Read the status register see what is up and
292 unsigned char status=inb_p(WDT_SR);
294 printk(KERN_CRIT PFX "status %d\n", status);
296 #ifdef CONFIG_WDT_501_PCI
297 if (!(status & WDC_SR_TGOOD))
298 printk(KERN_CRIT PFX "Overheat alarm.(%d)\n",inb_p(WDT_RT));
299 if (!(status & WDC_SR_PSUOVER))
300 printk(KERN_CRIT PFX "PSU over voltage.\n");
301 if (!(status & WDC_SR_PSUUNDR))
302 printk(KERN_CRIT PFX "PSU under voltage.\n");
304 if (!(status & WDC_SR_FANGOOD))
305 printk(KERN_CRIT PFX "Possible fan fault.\n");
307 #endif /* CONFIG_WDT_501_PCI */
308 if (!(status&WDC_SR_WCCR))
309 #ifdef SOFTWARE_REBOOT
311 printk(KERN_CRIT PFX "Would Reboot.\n");
313 printk(KERN_CRIT PFX "Initiating system reboot.\n");
314 machine_restart(NULL);
317 printk(KERN_CRIT PFX "Reset in 5ms.\n");
325 * @file: file handle to the watchdog
326 * @buf: buffer to write (unused as data does not matter here
327 * @count: count of bytes
328 * @ppos: pointer to the position to write. No seeks allowed
330 * A write to a watchdog device is defined as a keepalive signal. Any
331 * write of data will do, as we we don't define content meaning.
334 static ssize_t wdtpci_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
336 /* Can't seek (pwrite) on this device */
337 if (ppos != &file->f_pos)
346 for (i = 0; i != count; i++) {
348 if(get_user(c, buf+i))
362 * @inode: inode of the device
363 * @file: file handle to the device
364 * @cmd: watchdog command
365 * @arg: argument pointer
367 * The watchdog API defines a common set of functions for all watchdogs
368 * according to their available features. We only actually usefully support
369 * querying capabilities and current status.
372 static int wdtpci_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
377 void __user *argp = (void __user *)arg;
378 int __user *p = argp;
380 static struct watchdog_info ident = {
381 .options = WDIOF_SETTIMEOUT|
384 .firmware_version = 1,
385 .identity = "PCI-WDT500/501",
388 /* Add options according to the card we have */
389 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
390 #ifdef CONFIG_WDT_501_PCI
391 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER);
393 ident.options |= WDIOF_FANFAULT;
394 #endif /* CONFIG_WDT_501_PCI */
400 case WDIOC_GETSUPPORT:
401 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
403 case WDIOC_GETSTATUS:
404 wdtpci_get_status(&status);
405 return put_user(status, p);
406 case WDIOC_GETBOOTSTATUS:
407 return put_user(0, p);
408 case WDIOC_KEEPALIVE:
411 case WDIOC_SETTIMEOUT:
412 if (get_user(new_heartbeat, p))
415 if (wdtpci_set_heartbeat(new_heartbeat))
420 case WDIOC_GETTIMEOUT:
421 return put_user(heartbeat, p);
427 * @inode: inode of device
428 * @file: file handle to device
430 * The watchdog device has been opened. The watchdog device is single
431 * open and on opening we load the counters. Counter zero is a 100Hz
432 * cascade, into counter 1 which downcounts to reboot. When the counter
433 * triggers counter 2 downcounts the length of the reset pulse which
434 * set set to be as long as possible.
437 static int wdtpci_open(struct inode *inode, struct file *file)
439 if (down_trylock(&open_sem))
443 __module_get(THIS_MODULE);
454 * @inode: inode to board
455 * @file: file handle to board
457 * The watchdog has a configurable API. There is a religious dispute
458 * between people who want their watchdog to be able to shut down and
459 * those who want to be sure if the watchdog manager dies the machine
460 * reboots. In the former case we disable the counters, in the latter
461 * case you have to open it again very soon.
464 static int wdtpci_release(struct inode *inode, struct file *file)
466 if (expect_close == 42) {
469 printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
477 #ifdef CONFIG_WDT_501_PCI
480 * @file: file handle to the watchdog board
481 * @buf: buffer to write 1 byte into
482 * @count: length of buffer
483 * @ptr: offset (no seek allowed)
485 * Read reports the temperature in degrees Fahrenheit. The API is in
486 * fahrenheit. It was designed by an imperial measurement luddite.
489 static ssize_t wdtpci_temp_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
493 /* Can't seek (pread) on this device */
494 if (ptr != &file->f_pos)
497 if (wdtpci_get_temperature(&temperature))
500 if (copy_to_user (buf, &temperature, 1))
508 * @inode: inode of device
509 * @file: file handle to device
511 * The temperature device has been opened.
514 static int wdtpci_temp_open(struct inode *inode, struct file *file)
520 * wdtpci_temp_release:
521 * @inode: inode to board
522 * @file: file handle to board
524 * The temperature device has been closed.
527 static int wdtpci_temp_release(struct inode *inode, struct file *file)
531 #endif /* CONFIG_WDT_501_PCI */
535 * @this: our notifier block
536 * @code: the event being reported
539 * Our notifier is called on system shutdowns. We want to turn the card
540 * off at reboot otherwise the machine will reboot again during memory
541 * test or worse yet during the following fsck. This would suck, in fact
542 * trust me - if it happens it does suck.
545 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
548 if (code==SYS_DOWN || code==SYS_HALT) {
549 /* Turn the card off */
560 static struct file_operations wdtpci_fops = {
561 .owner = THIS_MODULE,
563 .write = wdtpci_write,
564 .ioctl = wdtpci_ioctl,
566 .release = wdtpci_release,
569 static struct miscdevice wdtpci_miscdev = {
570 .minor = WATCHDOG_MINOR,
572 .fops = &wdtpci_fops,
575 #ifdef CONFIG_WDT_501_PCI
576 static struct file_operations wdtpci_temp_fops = {
577 .owner = THIS_MODULE,
579 .read = wdtpci_temp_read,
580 .open = wdtpci_temp_open,
581 .release = wdtpci_temp_release,
584 static struct miscdevice temp_miscdev = {
586 .name = "temperature",
587 .fops = &wdtpci_temp_fops,
589 #endif /* CONFIG_WDT_501_PCI */
592 * The WDT card needs to learn about soft shutdowns in order to
593 * turn the timebomb registers off.
596 static struct notifier_block wdtpci_notifier = {
597 .notifier_call = wdtpci_notify_sys,
601 static int __devinit wdtpci_init_one (struct pci_dev *dev,
602 const struct pci_device_id *ent)
608 printk (KERN_ERR PFX "this driver only supports 1 device\n");
612 if (pci_enable_device (dev)) {
613 printk (KERN_ERR PFX "Not possible to enable PCI Device\n");
617 if (pci_resource_start (dev, 2) == 0x0000) {
618 printk (KERN_ERR PFX "No I/O-Address for card detected\n");
623 sema_init(&open_sem, 1);
624 spin_lock_init(&wdtpci_lock);
627 io = pci_resource_start (dev, 2);
629 if (request_region (io, 16, "wdt_pci") == NULL) {
630 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n", io);
634 if (request_irq (irq, wdtpci_interrupt, SA_INTERRUPT | SA_SHIRQ,
635 "wdt_pci", &wdtpci_miscdev)) {
636 printk (KERN_ERR PFX "IRQ %d is not free\n", irq);
640 printk ("PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%04x (Interrupt %d)\n",
643 /* Check that the heartbeat value is within it's range ; if not reset to the default */
644 if (wdtpci_set_heartbeat(heartbeat)) {
645 wdtpci_set_heartbeat(WD_TIMO);
646 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
650 ret = register_reboot_notifier (&wdtpci_notifier);
652 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret);
656 #ifdef CONFIG_WDT_501_PCI
657 ret = misc_register (&temp_miscdev);
659 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
663 #endif /* CONFIG_WDT_501_PCI */
665 ret = misc_register (&wdtpci_miscdev);
667 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
668 WATCHDOG_MINOR, ret);
672 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
673 heartbeat, nowayout);
674 #ifdef CONFIG_WDT_501_PCI
675 printk(KERN_INFO "wdt: Fan Tachometer is %s\n", (tachometer ? "Enabled" : "Disabled"));
676 #endif /* CONFIG_WDT_501_PCI */
683 #ifdef CONFIG_WDT_501_PCI
684 misc_deregister(&temp_miscdev);
685 #endif /* CONFIG_WDT_501_PCI */
687 unregister_reboot_notifier(&wdtpci_notifier);
689 free_irq(irq, &wdtpci_miscdev);
691 release_region (io, 16);
693 pci_disable_device(dev);
698 static void __devexit wdtpci_remove_one (struct pci_dev *pdev)
700 /* here we assume only one device will ever have
701 * been picked up and registered by probe function */
702 misc_deregister(&wdtpci_miscdev);
703 #ifdef CONFIG_WDT_501_PCI
704 misc_deregister(&temp_miscdev);
705 #endif /* CONFIG_WDT_501_PCI */
706 unregister_reboot_notifier(&wdtpci_notifier);
707 free_irq(irq, &wdtpci_miscdev);
708 release_region(io, 16);
709 pci_disable_device(pdev);
714 static struct pci_device_id wdtpci_pci_tbl[] = {
716 .vendor = PCI_VENDOR_ID_ACCESSIO,
717 .device = PCI_DEVICE_ID_WDG_CSM,
718 .subvendor = PCI_ANY_ID,
719 .subdevice = PCI_ANY_ID,
721 { 0, }, /* terminate list */
723 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
726 static struct pci_driver wdtpci_driver = {
728 .id_table = wdtpci_pci_tbl,
729 .probe = wdtpci_init_one,
730 .remove = __devexit_p(wdtpci_remove_one),
737 * Unload the watchdog. You cannot do this with any file handles open.
738 * If your watchdog is set to continue ticking on close and you unload
739 * it, well it keeps ticking. We won't get the interrupt but the board
740 * will not touch PC memory so all is fine. You just have to load a new
741 * module in xx seconds or reboot.
744 static void __exit wdtpci_cleanup(void)
746 pci_unregister_driver (&wdtpci_driver);
753 * Set up the WDT watchdog board. All we have to do is grab the
754 * resources we require and bitch if anyone beat us to them.
755 * The open() function will actually kick the board off.
758 static int __init wdtpci_init(void)
760 return pci_register_driver (&wdtpci_driver);
764 module_init(wdtpci_init);
765 module_exit(wdtpci_cleanup);
767 MODULE_AUTHOR("JP Nollmann, Alan Cox");
768 MODULE_DESCRIPTION("Driver for the ICS PCI-WDT500/501 watchdog cards");
769 MODULE_LICENSE("GPL");
770 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
771 MODULE_ALIAS_MISCDEV(TEMP_MINOR);