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 emergency_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)
342 for (i = 0; i != count; i++) {
344 if(get_user(c, buf+i))
358 * @inode: inode of the device
359 * @file: file handle to the device
360 * @cmd: watchdog command
361 * @arg: argument pointer
363 * The watchdog API defines a common set of functions for all watchdogs
364 * according to their available features. We only actually usefully support
365 * querying capabilities and current status.
368 static int wdtpci_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
373 void __user *argp = (void __user *)arg;
374 int __user *p = argp;
376 static struct watchdog_info ident = {
377 .options = WDIOF_SETTIMEOUT|
380 .firmware_version = 1,
381 .identity = "PCI-WDT500/501",
384 /* Add options according to the card we have */
385 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
386 #ifdef CONFIG_WDT_501_PCI
387 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER);
389 ident.options |= WDIOF_FANFAULT;
390 #endif /* CONFIG_WDT_501_PCI */
396 case WDIOC_GETSUPPORT:
397 return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
399 case WDIOC_GETSTATUS:
400 wdtpci_get_status(&status);
401 return put_user(status, p);
402 case WDIOC_GETBOOTSTATUS:
403 return put_user(0, p);
404 case WDIOC_KEEPALIVE:
407 case WDIOC_SETTIMEOUT:
408 if (get_user(new_heartbeat, p))
411 if (wdtpci_set_heartbeat(new_heartbeat))
416 case WDIOC_GETTIMEOUT:
417 return put_user(heartbeat, p);
423 * @inode: inode of device
424 * @file: file handle to device
426 * The watchdog device has been opened. The watchdog device is single
427 * open and on opening we load the counters. Counter zero is a 100Hz
428 * cascade, into counter 1 which downcounts to reboot. When the counter
429 * triggers counter 2 downcounts the length of the reset pulse which
430 * set set to be as long as possible.
433 static int wdtpci_open(struct inode *inode, struct file *file)
435 if (down_trylock(&open_sem))
439 __module_get(THIS_MODULE);
445 return nonseekable_open(inode, file);
450 * @inode: inode to board
451 * @file: file handle to board
453 * The watchdog has a configurable API. There is a religious dispute
454 * between people who want their watchdog to be able to shut down and
455 * those who want to be sure if the watchdog manager dies the machine
456 * reboots. In the former case we disable the counters, in the latter
457 * case you have to open it again very soon.
460 static int wdtpci_release(struct inode *inode, struct file *file)
462 if (expect_close == 42) {
465 printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
473 #ifdef CONFIG_WDT_501_PCI
476 * @file: file handle to the watchdog board
477 * @buf: buffer to write 1 byte into
478 * @count: length of buffer
479 * @ptr: offset (no seek allowed)
481 * Read reports the temperature in degrees Fahrenheit. The API is in
482 * fahrenheit. It was designed by an imperial measurement luddite.
485 static ssize_t wdtpci_temp_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
489 if (wdtpci_get_temperature(&temperature))
492 if (copy_to_user (buf, &temperature, 1))
500 * @inode: inode of device
501 * @file: file handle to device
503 * The temperature device has been opened.
506 static int wdtpci_temp_open(struct inode *inode, struct file *file)
508 return nonseekable_open(inode, file);
512 * wdtpci_temp_release:
513 * @inode: inode to board
514 * @file: file handle to board
516 * The temperature device has been closed.
519 static int wdtpci_temp_release(struct inode *inode, struct file *file)
523 #endif /* CONFIG_WDT_501_PCI */
527 * @this: our notifier block
528 * @code: the event being reported
531 * Our notifier is called on system shutdowns. We want to turn the card
532 * off at reboot otherwise the machine will reboot again during memory
533 * test or worse yet during the following fsck. This would suck, in fact
534 * trust me - if it happens it does suck.
537 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
540 if (code==SYS_DOWN || code==SYS_HALT) {
541 /* Turn the card off */
552 static struct file_operations wdtpci_fops = {
553 .owner = THIS_MODULE,
555 .write = wdtpci_write,
556 .ioctl = wdtpci_ioctl,
558 .release = wdtpci_release,
561 static struct miscdevice wdtpci_miscdev = {
562 .minor = WATCHDOG_MINOR,
564 .fops = &wdtpci_fops,
567 #ifdef CONFIG_WDT_501_PCI
568 static struct file_operations wdtpci_temp_fops = {
569 .owner = THIS_MODULE,
571 .read = wdtpci_temp_read,
572 .open = wdtpci_temp_open,
573 .release = wdtpci_temp_release,
576 static struct miscdevice temp_miscdev = {
578 .name = "temperature",
579 .fops = &wdtpci_temp_fops,
581 #endif /* CONFIG_WDT_501_PCI */
584 * The WDT card needs to learn about soft shutdowns in order to
585 * turn the timebomb registers off.
588 static struct notifier_block wdtpci_notifier = {
589 .notifier_call = wdtpci_notify_sys,
593 static int __devinit wdtpci_init_one (struct pci_dev *dev,
594 const struct pci_device_id *ent)
600 printk (KERN_ERR PFX "this driver only supports 1 device\n");
604 if (pci_enable_device (dev)) {
605 printk (KERN_ERR PFX "Not possible to enable PCI Device\n");
609 if (pci_resource_start (dev, 2) == 0x0000) {
610 printk (KERN_ERR PFX "No I/O-Address for card detected\n");
615 sema_init(&open_sem, 1);
616 spin_lock_init(&wdtpci_lock);
619 io = pci_resource_start (dev, 2);
621 if (request_region (io, 16, "wdt_pci") == NULL) {
622 printk (KERN_ERR PFX "I/O address 0x%04x already in use\n", io);
626 if (request_irq (irq, wdtpci_interrupt, SA_INTERRUPT | SA_SHIRQ,
627 "wdt_pci", &wdtpci_miscdev)) {
628 printk (KERN_ERR PFX "IRQ %d is not free\n", irq);
632 printk ("PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%04x (Interrupt %d)\n",
635 /* Check that the heartbeat value is within it's range ; if not reset to the default */
636 if (wdtpci_set_heartbeat(heartbeat)) {
637 wdtpci_set_heartbeat(WD_TIMO);
638 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
642 ret = register_reboot_notifier (&wdtpci_notifier);
644 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret);
648 #ifdef CONFIG_WDT_501_PCI
649 ret = misc_register (&temp_miscdev);
651 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
655 #endif /* CONFIG_WDT_501_PCI */
657 ret = misc_register (&wdtpci_miscdev);
659 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
660 WATCHDOG_MINOR, ret);
664 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
665 heartbeat, nowayout);
666 #ifdef CONFIG_WDT_501_PCI
667 printk(KERN_INFO "wdt: Fan Tachometer is %s\n", (tachometer ? "Enabled" : "Disabled"));
668 #endif /* CONFIG_WDT_501_PCI */
675 #ifdef CONFIG_WDT_501_PCI
676 misc_deregister(&temp_miscdev);
678 #endif /* CONFIG_WDT_501_PCI */
679 unregister_reboot_notifier(&wdtpci_notifier);
681 free_irq(irq, &wdtpci_miscdev);
683 release_region (io, 16);
685 pci_disable_device(dev);
690 static void __devexit wdtpci_remove_one (struct pci_dev *pdev)
692 /* here we assume only one device will ever have
693 * been picked up and registered by probe function */
694 misc_deregister(&wdtpci_miscdev);
695 #ifdef CONFIG_WDT_501_PCI
696 misc_deregister(&temp_miscdev);
697 #endif /* CONFIG_WDT_501_PCI */
698 unregister_reboot_notifier(&wdtpci_notifier);
699 free_irq(irq, &wdtpci_miscdev);
700 release_region(io, 16);
701 pci_disable_device(pdev);
706 static struct pci_device_id wdtpci_pci_tbl[] = {
708 .vendor = PCI_VENDOR_ID_ACCESSIO,
709 .device = PCI_DEVICE_ID_WDG_CSM,
710 .subvendor = PCI_ANY_ID,
711 .subdevice = PCI_ANY_ID,
713 { 0, }, /* terminate list */
715 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
718 static struct pci_driver wdtpci_driver = {
720 .id_table = wdtpci_pci_tbl,
721 .probe = wdtpci_init_one,
722 .remove = __devexit_p(wdtpci_remove_one),
729 * Unload the watchdog. You cannot do this with any file handles open.
730 * If your watchdog is set to continue ticking on close and you unload
731 * it, well it keeps ticking. We won't get the interrupt but the board
732 * will not touch PC memory so all is fine. You just have to load a new
733 * module in xx seconds or reboot.
736 static void __exit wdtpci_cleanup(void)
738 pci_unregister_driver (&wdtpci_driver);
745 * Set up the WDT watchdog board. All we have to do is grab the
746 * resources we require and bitch if anyone beat us to them.
747 * The open() function will actually kick the board off.
750 static int __init wdtpci_init(void)
752 return pci_register_driver (&wdtpci_driver);
756 module_init(wdtpci_init);
757 module_exit(wdtpci_cleanup);
759 MODULE_AUTHOR("JP Nollmann, Alan Cox");
760 MODULE_DESCRIPTION("Driver for the ICS PCI-WDT500/501 watchdog cards");
761 MODULE_LICENSE("GPL");
762 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
763 MODULE_ALIAS_MISCDEV(TEMP_MINOR);