Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / watchdog / wdt_pci.c
blob7651deda928c3cb9e02b8c7b6b43b082b07e4b4e
1 /*
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>
18 * Release 0.10.
20 * Fixes
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
24 * Matt Crocker).
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/fs.h>
50 #include <linux/pci.h>
52 #include <asm/io.h>
53 #include <asm/uaccess.h>
54 #include <asm/system.h>
56 #define WDT_IS_PCI
57 #include "wd501p.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
69 #endif
70 #ifndef PCI_DEVICE_ID_WDG_CSM
71 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
72 #endif
74 /* We can only use 1 card due to the /dev/watchdog restriction */
75 static int dev_count;
77 static struct semaphore open_sem;
78 static spinlock_t wdtpci_lock;
79 static char expect_close;
81 static int io;
82 static int irq;
84 /* Default timeout */
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;
94 #else
95 static int nowayout = 0;
96 #endif
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)
115 ctr<<=6;
116 ctr|=0x30;
117 ctr|=(mode<<1);
118 outb_p(ctr, WDT_CR);
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);
128 * wdtpci_start:
130 * Start the watchdog driver.
133 static int wdtpci_start(void)
135 unsigned long flags;
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);
162 return 0;
166 * wdtpci_stop:
168 * Stop the watchdog driver.
171 static int wdtpci_stop (void)
173 unsigned long flags;
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);
180 return 0;
184 * wdtpci_ping:
186 * Reload counter one with the watchdog heartbeat. We don't bother reloading
187 * the cascade counter.
190 static int wdtpci_ping(void)
192 unsigned long flags;
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);
201 return 0;
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
210 * return 0.
212 static int wdtpci_set_heartbeat(int t)
214 /* Arbitrary, can't find the card's limits */
215 if ((t < 1) || (t > 65535))
216 return -EINVAL;
218 heartbeat = t;
219 wd_heartbeat = t * 100;
220 return 0;
224 * wdtpci_get_status:
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);
238 *status=0;
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;
250 if (tachometer) {
251 if (!(new_status & WDC_SR_FANGOOD))
252 *status |= WDIOF_FANFAULT;
254 #endif /* CONFIG_WDT_501_PCI */
255 return 0;
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;
271 return 0;
273 #endif /* CONFIG_WDT_501_PCI */
276 * wdtpci_interrupt:
277 * @irq: Interrupt number
278 * @dev_id: Unused as we don't allow multiple devices.
279 * @regs: Unused.
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
290 * then printk it.
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");
303 if (tachometer) {
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
310 #ifdef ONLY_TESTING
311 printk(KERN_CRIT PFX "Would Reboot.\n");
312 #else
313 printk(KERN_CRIT PFX "Initiating system reboot.\n");
314 machine_restart(NULL);
315 #endif
316 #else
317 printk(KERN_CRIT PFX "Reset in 5ms.\n");
318 #endif
319 return IRQ_HANDLED;
324 * wdtpci_write:
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 if (count) {
337 if (!nowayout) {
338 size_t i;
340 expect_close = 0;
342 for (i = 0; i != count; i++) {
343 char c;
344 if(get_user(c, buf+i))
345 return -EFAULT;
346 if (c == 'V')
347 expect_close = 42;
350 wdtpci_ping();
353 return count;
357 * wdtpci_ioctl:
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,
369 unsigned long arg)
371 int new_heartbeat;
372 int status;
373 void __user *argp = (void __user *)arg;
374 int __user *p = argp;
376 static struct watchdog_info ident = {
377 .options = WDIOF_SETTIMEOUT|
378 WDIOF_MAGICCLOSE|
379 WDIOF_KEEPALIVEPING,
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);
388 if (tachometer)
389 ident.options |= WDIOF_FANFAULT;
390 #endif /* CONFIG_WDT_501_PCI */
392 switch(cmd)
394 default:
395 return -ENOIOCTLCMD;
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:
405 wdtpci_ping();
406 return 0;
407 case WDIOC_SETTIMEOUT:
408 if (get_user(new_heartbeat, p))
409 return -EFAULT;
411 if (wdtpci_set_heartbeat(new_heartbeat))
412 return -EINVAL;
414 wdtpci_ping();
415 /* Fall */
416 case WDIOC_GETTIMEOUT:
417 return put_user(heartbeat, p);
422 * wdtpci_open:
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))
436 return -EBUSY;
438 if (nowayout) {
439 __module_get(THIS_MODULE);
442 * Activate
444 wdtpci_start();
445 return nonseekable_open(inode, file);
449 * wdtpci_release:
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) {
463 wdtpci_stop();
464 } else {
465 printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
466 wdtpci_ping();
468 expect_close = 0;
469 up(&open_sem);
470 return 0;
473 #ifdef CONFIG_WDT_501_PCI
475 * wdtpci_temp_read:
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)
487 int temperature;
489 if (wdtpci_get_temperature(&temperature))
490 return -EFAULT;
492 if (copy_to_user (buf, &temperature, 1))
493 return -EFAULT;
495 return 1;
499 * wdtpci_temp_open:
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)
521 return 0;
523 #endif /* CONFIG_WDT_501_PCI */
526 * notify_sys:
527 * @this: our notifier block
528 * @code: the event being reported
529 * @unused: unused
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,
538 void *unused)
540 if (code==SYS_DOWN || code==SYS_HALT) {
541 /* Turn the card off */
542 wdtpci_stop();
544 return NOTIFY_DONE;
548 * Kernel Interfaces
552 static struct file_operations wdtpci_fops = {
553 .owner = THIS_MODULE,
554 .llseek = no_llseek,
555 .write = wdtpci_write,
556 .ioctl = wdtpci_ioctl,
557 .open = wdtpci_open,
558 .release = wdtpci_release,
561 static struct miscdevice wdtpci_miscdev = {
562 .minor = WATCHDOG_MINOR,
563 .name = "watchdog",
564 .fops = &wdtpci_fops,
567 #ifdef CONFIG_WDT_501_PCI
568 static struct file_operations wdtpci_temp_fops = {
569 .owner = THIS_MODULE,
570 .llseek = no_llseek,
571 .read = wdtpci_temp_read,
572 .open = wdtpci_temp_open,
573 .release = wdtpci_temp_release,
576 static struct miscdevice temp_miscdev = {
577 .minor = TEMP_MINOR,
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)
596 int ret = -EIO;
598 dev_count++;
599 if (dev_count > 1) {
600 printk (KERN_ERR PFX "this driver only supports 1 device\n");
601 return -ENODEV;
604 if (pci_enable_device (dev)) {
605 printk (KERN_ERR PFX "Not possible to enable PCI Device\n");
606 return -ENODEV;
609 if (pci_resource_start (dev, 2) == 0x0000) {
610 printk (KERN_ERR PFX "No I/O-Address for card detected\n");
611 ret = -ENODEV;
612 goto out_pci;
615 sema_init(&open_sem, 1);
616 spin_lock_init(&wdtpci_lock);
618 irq = dev->irq;
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);
623 goto out_pci;
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);
629 goto out_reg;
632 printk ("PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%04x (Interrupt %d)\n",
633 io, irq);
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",
639 WD_TIMO);
642 ret = register_reboot_notifier (&wdtpci_notifier);
643 if (ret) {
644 printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret);
645 goto out_irq;
648 #ifdef CONFIG_WDT_501_PCI
649 ret = misc_register (&temp_miscdev);
650 if (ret) {
651 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
652 TEMP_MINOR, ret);
653 goto out_rbt;
655 #endif /* CONFIG_WDT_501_PCI */
657 ret = misc_register (&wdtpci_miscdev);
658 if (ret) {
659 printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
660 WATCHDOG_MINOR, ret);
661 goto out_misc;
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 */
670 ret = 0;
671 out:
672 return ret;
674 out_misc:
675 #ifdef CONFIG_WDT_501_PCI
676 misc_deregister(&temp_miscdev);
677 out_rbt:
678 #endif /* CONFIG_WDT_501_PCI */
679 unregister_reboot_notifier(&wdtpci_notifier);
680 out_irq:
681 free_irq(irq, &wdtpci_miscdev);
682 out_reg:
683 release_region (io, 16);
684 out_pci:
685 pci_disable_device(dev);
686 goto out;
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);
702 dev_count--;
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 = {
719 .name = "wdt_pci",
720 .id_table = wdtpci_pci_tbl,
721 .probe = wdtpci_init_one,
722 .remove = __devexit_p(wdtpci_remove_one),
727 * wdtpci_cleanup:
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);
743 * wdtpci_init:
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);