2 * Industrial Computer Source WDT500/501 driver for Linux 2.1.x
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>
49 #include <linux/pci.h>
52 #include <asm/uaccess.h>
53 #include <asm/system.h>
58 #define PFX "wdt_pci: "
61 * Until Access I/O gets their application for a PCI vendor ID approved,
62 * I don't think that it's appropriate to move these constants into the
63 * regular pci_ids.h file. -- JPN 2000/01/18
66 #ifndef PCI_VENDOR_ID_ACCESSIO
67 #define PCI_VENDOR_ID_ACCESSIO 0x494f
69 #ifndef PCI_DEVICE_ID_WDG_CSM
70 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
73 static struct semaphore open_sem;
74 static spinlock_t wdtpci_lock;
75 static int expect_close = 0;
81 #define WD_TIMO (100*60) /* 1 minute */
82 #define WD_TIMO_MAX (WD_TIMO*60) /* 1 hour(?) */
84 static int wd_margin = WD_TIMO;
86 #ifdef CONFIG_WATCHDOG_NOWAYOUT
87 static int nowayout = 1;
89 static int nowayout = 0;
92 module_param(nowayout, int, 0);
93 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
99 static void wdtpci_ctr_mode(int ctr, int mode)
107 static void wdtpci_ctr_load(int ctr, int val)
109 outb_p(val&0xFF, WDT_COUNT0+ctr);
110 outb_p(val>>8, WDT_COUNT0+ctr);
121 * Extract the status information from a WDT watchdog device. There are
122 * several board variants so we have to know which bits are valid. Some
123 * bits default to one and some to zero in order to be maximally painful.
125 * we then map the bits onto the status ioctl flags.
128 static int wdtpci_status(void)
131 * Status register to bit flags
135 unsigned char status=inb_p(WDT_SR);
137 status&=~FEATUREMAP2;
139 if(!(status&WDC_SR_TGOOD))
140 flag|=WDIOF_OVERHEAT;
141 if(!(status&WDC_SR_PSUOVER))
142 flag|=WDIOF_POWEROVER;
143 if(!(status&WDC_SR_PSUUNDR))
144 flag|=WDIOF_POWERUNDER;
145 if(!(status&WDC_SR_FANGOOD))
146 flag|=WDIOF_FANFAULT;
147 if(status&WDC_SR_ISOI0)
149 if(status&WDC_SR_ISII1)
156 * @irq: Interrupt number
157 * @dev_id: Unused as we don't allow multiple devices.
160 * Handle an interrupt from the board. These are raised when the status
161 * map changes in what the board considers an interesting way. That means
162 * a failure condition occurring.
165 static irqreturn_t wdtpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
168 * Read the status register see what is up and
172 unsigned char status=inb_p(WDT_SR);
175 status&=~FEATUREMAP2;
177 printk(KERN_CRIT "WDT status %d\n", status);
179 if(!(status&WDC_SR_TGOOD))
180 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
181 if(!(status&WDC_SR_PSUOVER))
182 printk(KERN_CRIT "PSU over voltage.\n");
183 if(!(status&WDC_SR_PSUUNDR))
184 printk(KERN_CRIT "PSU under voltage.\n");
185 if(!(status&WDC_SR_FANGOOD))
186 printk(KERN_CRIT "Possible fan fault.\n");
187 if(!(status&WDC_SR_WCCR))
188 #ifdef SOFTWARE_REBOOT
190 printk(KERN_CRIT "Would Reboot.\n");
192 printk(KERN_CRIT "Initiating system reboot.\n");
193 machine_restart(NULL);
196 printk(KERN_CRIT "Reset in 5ms.\n");
205 * Reload counter one with the watchdog timeout. We don't bother reloading
206 * the cascade counter.
209 static void wdtpci_ping(void)
213 /* Write a watchdog value */
214 spin_lock_irqsave(&wdtpci_lock, flags);
216 wdtpci_ctr_mode(1,2);
217 wdtpci_ctr_load(1,wd_margin); /* Timeout */
219 spin_unlock_irqrestore(&wdtpci_lock, flags);
224 * @file: file handle to the watchdog
225 * @buf: buffer to write (unused as data does not matter here
226 * @count: count of bytes
227 * @ppos: pointer to the position to write. No seeks allowed
229 * A write to a watchdog device is defined as a keepalive signal. Any
230 * write of data will do, as we we don't define content meaning.
233 static ssize_t wdtpci_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
235 /* Can't seek (pwrite) on this device */
236 if (ppos != &file->f_pos)
245 for (i = 0; i != count; i++) {
247 if(get_user(c, buf+i))
261 * @file: file handle to the watchdog board
262 * @buf: buffer to write 1 byte into
263 * @count: length of buffer
264 * @ptr: offset (no seek allowed)
266 * Read reports the temperature in degrees Fahrenheit. The API is in
267 * fahrenheit. It was designed by an imperial measurement luddite.
270 static ssize_t wdtpci_read(struct file *file, char *buf, size_t count, loff_t *ptr)
272 unsigned short c=inb_p(WDT_RT);
275 /* Can't seek (pread) on this device */
276 if (ptr != &file->f_pos)
279 switch(minor(file->f_dentry->d_inode->i_rdev))
285 if(copy_to_user(buf,&cp,1))
295 * @inode: inode of the device
296 * @file: file handle to the device
297 * @cmd: watchdog command
298 * @arg: argument pointer
300 * The watchdog API defines a common set of functions for all watchdogs
301 * according to their available features. We only actually usefully support
302 * querying capabilities and current status.
305 static int wdtpci_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
309 static struct watchdog_info ident = {
310 .options = WDIOF_OVERHEAT | WDIOF_POWERUNDER |
311 WDIOF_POWEROVER | WDIOF_EXTERN1 |
312 WDIOF_EXTERN2 | WDIOF_FANFAULT |
313 WDIOF_SETTIMEOUT|WDIOF_MAGICCLOSE,
314 .firmware_version = 1,
315 .identity = "WDT500/501PCI",
318 ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
323 case WDIOC_GETSUPPORT:
324 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
326 case WDIOC_GETSTATUS:
327 return put_user(wdtpci_status(),(int *)arg);
328 case WDIOC_GETBOOTSTATUS:
329 return put_user(0, (int *)arg);
330 case WDIOC_KEEPALIVE:
333 case WDIOC_SETTIMEOUT:
334 if (get_user(new_margin, (int *)arg))
336 /* Arbitrary, can't find the card's limits */
338 if ((new_margin < 0) || (new_margin > WD_TIMO_MAX))
340 wd_margin = new_margin;
343 case WDIOC_GETTIMEOUT:
344 return put_user(wd_margin / 100, (int *)arg);
350 * @inode: inode of device
351 * @file: file handle to device
353 * One of our two misc devices has been opened. The watchdog device is
354 * single open and on opening we load the counters. Counter zero is a
355 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
356 * counter triggers counter 2 downcounts the length of the reset pulse
357 * which set set to be as long as possible.
360 static int wdtpci_open(struct inode *inode, struct file *file)
364 switch(minor(inode->i_rdev))
367 if (down_trylock(&open_sem))
371 __module_get(THIS_MODULE);
376 spin_lock_irqsave(&wdtpci_lock, flags);
378 inb_p(WDT_DC); /* Disable */
381 * "pet" the watchdog, as Access says.
382 * This resets the clock outputs.
385 wdtpci_ctr_mode(2,0);
390 outb_p(0, WDT_CLOCK); /* 2.0833MHz clock */
391 inb_p(WDT_BUZZER); /* disable */
392 inb_p(WDT_OPTONOTRST); /* disable */
393 inb_p(WDT_OPTORST); /* disable */
394 inb_p(WDT_PROGOUT); /* disable */
395 wdtpci_ctr_mode(0,3);
396 wdtpci_ctr_mode(1,2);
397 wdtpci_ctr_mode(2,1);
398 wdtpci_ctr_load(0,20833); /* count at 100Hz */
399 wdtpci_ctr_load(1,wd_margin);/* Timeout 60 seconds */
400 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
401 outb_p(0, WDT_DC); /* Enable */
402 spin_unlock_irqrestore(&wdtpci_lock, flags);
413 * @inode: inode to board
414 * @file: file handle to board
416 * The watchdog has a configurable API. There is a religious dispute
417 * between people who want their watchdog to be able to shut down and
418 * those who want to be sure if the watchdog manager dies the machine
419 * reboots. In the former case we disable the counters, in the latter
420 * case you have to open it again very soon.
423 static int wdtpci_release(struct inode *inode, struct file *file)
426 if (minor(inode->i_rdev)==WATCHDOG_MINOR) {
429 spin_lock_irqsave(&wdtpci_lock, flags);
430 inb_p(WDT_DC); /* Disable counters */
431 wdtpci_ctr_load(2,0); /* 0 length reset pulses now */
432 spin_unlock_irqrestore(&wdtpci_lock, flags);
434 printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
444 * @this: our notifier block
445 * @code: the event being reported
448 * Our notifier is called on system shutdowns. We want to turn the card
449 * off at reboot otherwise the machine will reboot again during memory
450 * test or worse yet during the following fsck. This would suck, in fact
451 * trust me - if it happens it does suck.
454 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
459 if (code==SYS_DOWN || code==SYS_HALT) {
460 /* Turn the card off */
461 spin_lock_irqsave(&wdtpci_lock, flags);
463 wdtpci_ctr_load(2,0);
464 spin_unlock_irqrestore(&wdtpci_lock, flags);
474 static struct file_operations wdtpci_fops = {
475 .owner = THIS_MODULE,
478 .write = wdtpci_write,
479 .ioctl = wdtpci_ioctl,
481 .release = wdtpci_release,
484 static struct miscdevice wdtpci_miscdev = {
485 .minor = WATCHDOG_MINOR,
487 .fops = &wdtpci_fops,
490 #ifdef CONFIG_WDT_501_PCI
491 static struct miscdevice temp_miscdev = {
493 .name = "temperature",
494 .fops = &wdtpci_fops,
499 * The WDT card needs to learn about soft shutdowns in order to
500 * turn the timebomb registers off.
503 static struct notifier_block wdtpci_notifier = {
504 .notifier_call = wdtpci_notify_sys,
508 static int __init wdtpci_init_one (struct pci_dev *dev,
509 const struct pci_device_id *ent)
511 static int dev_count = 0;
517 "this driver only supports 1 device\n");
521 if (pci_enable_device (dev))
524 sema_init(&open_sem, 1);
525 spin_lock_init(&wdtpci_lock);
528 io = pci_resource_start (dev, 2);
529 printk ("WDT501-P(PCI-WDG-CSM) driver 0.07 at %X "
530 "(Interrupt %d)\n", io, irq);
532 if (request_region (io, 16, "wdt-pci") == NULL) {
533 printk (KERN_ERR PFX "I/O %d is not free.\n", io);
537 if (request_irq (irq, wdtpci_interrupt, SA_INTERRUPT | SA_SHIRQ,
538 "wdt-pci", &wdtpci_miscdev)) {
539 printk (KERN_ERR PFX "IRQ %d is not free.\n", irq);
543 ret = misc_register (&wdtpci_miscdev);
545 printk (KERN_ERR PFX "can't misc_register on minor=%d\n", WATCHDOG_MINOR);
549 ret = register_reboot_notifier (&wdtpci_notifier);
551 printk (KERN_ERR PFX "can't misc_register on minor=%d\n", WATCHDOG_MINOR);
554 #ifdef CONFIG_WDT_501_PCI
555 ret = misc_register (&temp_miscdev);
557 printk (KERN_ERR PFX "can't misc_register (temp) on minor=%d\n", TEMP_MINOR);
566 #ifdef CONFIG_WDT_501_PCI
568 unregister_reboot_notifier(&wdtpci_notifier);
571 misc_deregister(&wdtpci_miscdev);
573 free_irq(irq, &wdtpci_miscdev);
575 release_region (io, 16);
580 static void __devexit wdtpci_remove_one (struct pci_dev *pdev)
582 /* here we assume only one device will ever have
583 * been picked up and registered by probe function */
584 unregister_reboot_notifier(&wdtpci_notifier);
585 #ifdef CONFIG_WDT_501_PCI
586 misc_deregister(&temp_miscdev);
588 misc_deregister(&wdtpci_miscdev);
589 free_irq(irq, &wdtpci_miscdev);
590 release_region(io, 16);
594 static struct pci_device_id wdtpci_pci_tbl[] = {
596 .vendor = PCI_VENDOR_ID_ACCESSIO,
597 .device = PCI_DEVICE_ID_WDG_CSM,
598 .subvendor = PCI_ANY_ID,
599 .subdevice = PCI_ANY_ID,
601 { 0, }, /* terminate list */
603 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
606 static struct pci_driver wdtpci_driver = {
608 .id_table = wdtpci_pci_tbl,
609 .probe = wdtpci_init_one,
610 .remove = __devexit_p(wdtpci_remove_one),
617 * Unload the watchdog. You cannot do this with any file handles open.
618 * If your watchdog is set to continue ticking on close and you unload
619 * it, well it keeps ticking. We won't get the interrupt but the board
620 * will not touch PC memory so all is fine. You just have to load a new
621 * module in 60 seconds or reboot.
624 static void __exit wdtpci_cleanup(void)
626 pci_unregister_driver (&wdtpci_driver);
633 * Set up the WDT watchdog board. All we have to do is grab the
634 * resources we require and bitch if anyone beat us to them.
635 * The open() function will actually kick the board off.
638 static int __init wdtpci_init(void)
640 int rc = pci_register_driver (&wdtpci_driver);
649 module_init(wdtpci_init);
650 module_exit(wdtpci_cleanup);
652 MODULE_AUTHOR("JP Nollmann, Alan Cox");
653 MODULE_DESCRIPTION("Driver for the ICS PCI watchdog cards");
654 MODULE_LICENSE("GPL");