Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / char / watchdog / pcwd_pci.c
blob8ce0666273265cd4d01471ac64859742a8120b27
1 /*
2 * Berkshire PCI-PC Watchdog Card Driver
4 * (c) Copyright 2003 Wim Van Sebroeck <wim@iguana.be>.
6 * Based on source code of the following authors:
7 * Ken Hollis <kenji@bitgate.com>,
8 * Lindsay Harris <lindsay@bluegum.com>,
9 * Alan Cox <alan@redhat.com>,
10 * Matt Domsch <Matt_Domsch@dell.com>,
11 * Rob Radez <rob@osinvestor.com>
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version
16 * 2 of the License, or (at your option) any later version.
18 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
19 * provide warranty for any of this software. This material is
20 * provided "AS-IS" and at no charge.
24 * A bells and whistles driver is available from http://www.pcwd.de/
25 * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
29 * Includes, defines, variables, module parameters, ...
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/moduleparam.h>
35 #include <linux/types.h>
36 #include <linux/delay.h>
37 #include <linux/miscdevice.h>
38 #include <linux/watchdog.h>
39 #include <linux/notifier.h>
40 #include <linux/reboot.h>
41 #include <linux/init.h>
42 #include <linux/fs.h>
43 #include <linux/pci.h>
44 #include <linux/ioport.h>
45 #include <linux/spinlock.h>
47 #include <asm/uaccess.h>
48 #include <asm/io.h>
50 /* Module and version information */
51 #define WATCHDOG_VERSION "1.01"
52 #define WATCHDOG_DATE "15 Mar 2005"
53 #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
54 #define WATCHDOG_NAME "pcwd_pci"
55 #define PFX WATCHDOG_NAME ": "
56 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
58 /* Stuff for the PCI ID's */
59 #ifndef PCI_VENDOR_ID_QUICKLOGIC
60 #define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
61 #endif
63 #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
64 #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
65 #endif
68 * These are the defines that describe the control status bits for the
69 * PCI-PC Watchdog card.
71 #define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
72 #define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
73 #define WD_PCI_TTRP 0x04 /* Temperature Trip status */
75 /* according to documentation max. time to process a command for the pci
76 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
77 #define PCI_COMMAND_TIMEOUT 150
79 /* Watchdog's internal commands */
80 #define CMD_GET_STATUS 0x04
81 #define CMD_GET_FIRMWARE_VERSION 0x08
82 #define CMD_READ_WATCHDOG_TIMEOUT 0x18
83 #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
85 /* We can only use 1 card due to the /dev/watchdog restriction */
86 static int cards_found;
88 /* internal variables */
89 static int temp_panic;
90 static unsigned long is_active;
91 static char expect_release;
92 static struct {
93 int supports_temp; /* Wether or not the card has a temperature device */
94 int boot_status; /* The card's boot status */
95 unsigned long io_addr; /* The cards I/O address */
96 spinlock_t io_lock;
97 struct pci_dev *pdev;
98 } pcipcwd_private;
100 /* module parameters */
101 #define WATCHDOG_HEARTBEAT 2 /* 2 sec default heartbeat */
102 static int heartbeat = WATCHDOG_HEARTBEAT;
103 module_param(heartbeat, int, 0);
104 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
106 #ifdef CONFIG_WATCHDOG_NOWAYOUT
107 static int nowayout = 1;
108 #else
109 static int nowayout = 0;
110 #endif
112 module_param(nowayout, int, 0);
113 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
116 * Internal functions
119 static int send_command(int cmd, int *msb, int *lsb)
121 int got_response, count;
123 spin_lock(&pcipcwd_private.io_lock);
124 /* If a command requires data it should be written first.
125 * Data for commands with 8 bits of data should be written to port 4.
126 * Commands with 16 bits of data, should be written as LSB to port 4
127 * and MSB to port 5.
128 * After the required data has been written then write the command to
129 * port 6. */
130 outb_p(*lsb, pcipcwd_private.io_addr + 4);
131 outb_p(*msb, pcipcwd_private.io_addr + 5);
132 outb_p(cmd, pcipcwd_private.io_addr + 6);
134 /* wait till the pci card processed the command, signaled by
135 * the WRSP bit in port 2 and give it a max. timeout of
136 * PCI_COMMAND_TIMEOUT to process */
137 got_response = inb_p(pcipcwd_private.io_addr + 2) & 0x40;
138 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
139 mdelay(1);
140 got_response = inb_p(pcipcwd_private.io_addr + 2) & 0x40;
143 if (got_response) {
144 /* read back response */
145 *lsb = inb_p(pcipcwd_private.io_addr + 4);
146 *msb = inb_p(pcipcwd_private.io_addr + 5);
148 /* clear WRSP bit */
149 inb_p(pcipcwd_private.io_addr + 6);
151 spin_unlock(&pcipcwd_private.io_lock);
153 return got_response;
156 static int pcipcwd_start(void)
158 int stat_reg;
160 spin_lock(&pcipcwd_private.io_lock);
161 outb_p(0x00, pcipcwd_private.io_addr + 3);
162 udelay(1000);
164 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
165 spin_unlock(&pcipcwd_private.io_lock);
167 if (stat_reg & 0x10) {
168 printk(KERN_ERR PFX "Card timer not enabled\n");
169 return -1;
172 return 0;
175 static int pcipcwd_stop(void)
177 int stat_reg;
179 spin_lock(&pcipcwd_private.io_lock);
180 outb_p(0xA5, pcipcwd_private.io_addr + 3);
181 udelay(1000);
183 outb_p(0xA5, pcipcwd_private.io_addr + 3);
184 udelay(1000);
186 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
187 spin_unlock(&pcipcwd_private.io_lock);
189 if (!(stat_reg & 0x10)) {
190 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
191 return -1;
194 return 0;
197 static int pcipcwd_keepalive(void)
199 /* Re-trigger watchdog by writing to port 0 */
200 outb_p(0x42, pcipcwd_private.io_addr);
201 return 0;
204 static int pcipcwd_set_heartbeat(int t)
206 int t_msb = t / 256;
207 int t_lsb = t % 256;
209 if ((t < 0x0001) || (t > 0xFFFF))
210 return -EINVAL;
212 /* Write new heartbeat to watchdog */
213 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
215 heartbeat = t;
216 return 0;
219 static int pcipcwd_get_status(int *status)
221 int new_status;
223 *status=0;
224 new_status = inb_p(pcipcwd_private.io_addr + 1);
225 if (new_status & WD_PCI_WTRP)
226 *status |= WDIOF_CARDRESET;
227 if (new_status & WD_PCI_TTRP) {
228 *status |= WDIOF_OVERHEAT;
229 if (temp_panic)
230 panic(PFX "Temperature overheat trip!\n");
233 return 0;
236 static int pcipcwd_clear_status(void)
238 outb_p(0x01, pcipcwd_private.io_addr + 1);
239 return 0;
242 static int pcipcwd_get_temperature(int *temperature)
244 *temperature = 0;
245 if (!pcipcwd_private.supports_temp)
246 return -ENODEV;
249 * Convert celsius to fahrenheit, since this was
250 * the decided 'standard' for this return value.
252 *temperature = ((inb_p(pcipcwd_private.io_addr)) * 9 / 5) + 32;
254 return 0;
258 * /dev/watchdog handling
261 static ssize_t pcipcwd_write(struct file *file, const char __user *data,
262 size_t len, loff_t *ppos)
264 /* See if we got the magic character 'V' and reload the timer */
265 if (len) {
266 if (!nowayout) {
267 size_t i;
269 /* note: just in case someone wrote the magic character
270 * five months ago... */
271 expect_release = 0;
273 /* scan to see whether or not we got the magic character */
274 for (i = 0; i != len; i++) {
275 char c;
276 if(get_user(c, data+i))
277 return -EFAULT;
278 if (c == 'V')
279 expect_release = 42;
283 /* someone wrote to us, we should reload the timer */
284 pcipcwd_keepalive();
286 return len;
289 static int pcipcwd_ioctl(struct inode *inode, struct file *file,
290 unsigned int cmd, unsigned long arg)
292 void __user *argp = (void __user *)arg;
293 int __user *p = argp;
294 static struct watchdog_info ident = {
295 .options = WDIOF_OVERHEAT |
296 WDIOF_CARDRESET |
297 WDIOF_KEEPALIVEPING |
298 WDIOF_SETTIMEOUT |
299 WDIOF_MAGICCLOSE,
300 .firmware_version = 1,
301 .identity = WATCHDOG_DRIVER_NAME,
304 switch (cmd) {
305 case WDIOC_GETSUPPORT:
306 return copy_to_user(argp, &ident,
307 sizeof (ident)) ? -EFAULT : 0;
309 case WDIOC_GETSTATUS:
311 int status;
313 pcipcwd_get_status(&status);
315 return put_user(status, p);
318 case WDIOC_GETBOOTSTATUS:
319 return put_user(pcipcwd_private.boot_status, p);
321 case WDIOC_GETTEMP:
323 int temperature;
325 if (pcipcwd_get_temperature(&temperature))
326 return -EFAULT;
328 return put_user(temperature, p);
331 case WDIOC_KEEPALIVE:
332 pcipcwd_keepalive();
333 return 0;
335 case WDIOC_SETOPTIONS:
337 int new_options, retval = -EINVAL;
339 if (get_user (new_options, p))
340 return -EFAULT;
342 if (new_options & WDIOS_DISABLECARD) {
343 pcipcwd_stop();
344 retval = 0;
347 if (new_options & WDIOS_ENABLECARD) {
348 pcipcwd_start();
349 retval = 0;
352 if (new_options & WDIOS_TEMPPANIC) {
353 temp_panic = 1;
354 retval = 0;
357 return retval;
360 case WDIOC_SETTIMEOUT:
362 int new_heartbeat;
364 if (get_user(new_heartbeat, p))
365 return -EFAULT;
367 if (pcipcwd_set_heartbeat(new_heartbeat))
368 return -EINVAL;
370 pcipcwd_keepalive();
371 /* Fall */
374 case WDIOC_GETTIMEOUT:
375 return put_user(heartbeat, p);
377 default:
378 return -ENOIOCTLCMD;
382 static int pcipcwd_open(struct inode *inode, struct file *file)
384 /* /dev/watchdog can only be opened once */
385 if (test_and_set_bit(0, &is_active))
386 return -EBUSY;
388 /* Activate */
389 pcipcwd_start();
390 pcipcwd_keepalive();
391 return nonseekable_open(inode, file);
394 static int pcipcwd_release(struct inode *inode, struct file *file)
397 * Shut off the timer.
399 if (expect_release == 42) {
400 pcipcwd_stop();
401 } else {
402 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
403 pcipcwd_keepalive();
405 expect_release = 0;
406 clear_bit(0, &is_active);
407 return 0;
411 * /dev/temperature handling
414 static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
415 size_t len, loff_t *ppos)
417 int temperature;
419 if (pcipcwd_get_temperature(&temperature))
420 return -EFAULT;
422 if (copy_to_user (data, &temperature, 1))
423 return -EFAULT;
425 return 1;
428 static int pcipcwd_temp_open(struct inode *inode, struct file *file)
430 if (!pcipcwd_private.supports_temp)
431 return -ENODEV;
433 return nonseekable_open(inode, file);
436 static int pcipcwd_temp_release(struct inode *inode, struct file *file)
438 return 0;
442 * Notify system
445 static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
447 if (code==SYS_DOWN || code==SYS_HALT) {
448 /* Turn the WDT off */
449 pcipcwd_stop();
452 return NOTIFY_DONE;
456 * Kernel Interfaces
459 static struct file_operations pcipcwd_fops = {
460 .owner = THIS_MODULE,
461 .llseek = no_llseek,
462 .write = pcipcwd_write,
463 .ioctl = pcipcwd_ioctl,
464 .open = pcipcwd_open,
465 .release = pcipcwd_release,
468 static struct miscdevice pcipcwd_miscdev = {
469 .minor = WATCHDOG_MINOR,
470 .name = "watchdog",
471 .fops = &pcipcwd_fops,
474 static struct file_operations pcipcwd_temp_fops = {
475 .owner = THIS_MODULE,
476 .llseek = no_llseek,
477 .read = pcipcwd_temp_read,
478 .open = pcipcwd_temp_open,
479 .release = pcipcwd_temp_release,
482 static struct miscdevice pcipcwd_temp_miscdev = {
483 .minor = TEMP_MINOR,
484 .name = "temperature",
485 .fops = &pcipcwd_temp_fops,
488 static struct notifier_block pcipcwd_notifier = {
489 .notifier_call = pcipcwd_notify_sys,
493 * Init & exit routines
496 static inline void check_temperature_support(void)
498 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
499 pcipcwd_private.supports_temp = 1;
502 static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
503 const struct pci_device_id *ent)
505 int ret = -EIO;
506 int got_fw_rev, fw_rev_major, fw_rev_minor;
507 char fw_ver_str[20];
508 char option_switches;
510 cards_found++;
511 if (cards_found == 1)
512 printk(KERN_INFO PFX DRIVER_VERSION);
514 if (cards_found > 1) {
515 printk(KERN_ERR PFX "This driver only supports 1 device\n");
516 return -ENODEV;
519 if (pci_enable_device(pdev)) {
520 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
521 return -ENODEV;
524 if (pci_resource_start(pdev, 0) == 0x0000) {
525 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
526 ret = -ENODEV;
527 goto err_out_disable_device;
530 pcipcwd_private.pdev = pdev;
531 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
533 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
534 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
535 (int) pcipcwd_private.io_addr);
536 ret = -EIO;
537 goto err_out_disable_device;
540 /* get the boot_status */
541 pcipcwd_get_status(&pcipcwd_private.boot_status);
543 /* clear the "card caused reboot" flag */
544 pcipcwd_clear_status();
546 /* disable card */
547 pcipcwd_stop();
549 /* Check whether or not the card supports the temperature device */
550 check_temperature_support();
552 /* Get the Firmware Version */
553 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
554 if (got_fw_rev) {
555 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
556 } else {
557 sprintf(fw_ver_str, "<card no answer>");
560 /* Get switch settings */
561 option_switches = inb_p(pcipcwd_private.io_addr + 3);
563 printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
564 (int) pcipcwd_private.io_addr, fw_ver_str,
565 (pcipcwd_private.supports_temp ? "with" : "without"));
567 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
568 option_switches,
569 ((option_switches & 0x10) ? "ON" : "OFF"),
570 ((option_switches & 0x08) ? "ON" : "OFF"));
572 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
573 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
575 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
576 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
578 if (pcipcwd_private.boot_status == 0)
579 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
581 /* Check that the heartbeat value is within it's range ; if not reset to the default */
582 if (pcipcwd_set_heartbeat(heartbeat)) {
583 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
584 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
585 WATCHDOG_HEARTBEAT);
588 ret = register_reboot_notifier(&pcipcwd_notifier);
589 if (ret != 0) {
590 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
591 ret);
592 goto err_out_release_region;
595 if (pcipcwd_private.supports_temp) {
596 ret = misc_register(&pcipcwd_temp_miscdev);
597 if (ret != 0) {
598 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
599 TEMP_MINOR, ret);
600 goto err_out_unregister_reboot;
604 ret = misc_register(&pcipcwd_miscdev);
605 if (ret != 0) {
606 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
607 WATCHDOG_MINOR, ret);
608 goto err_out_misc_deregister;
611 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
612 heartbeat, nowayout);
614 return 0;
616 err_out_misc_deregister:
617 if (pcipcwd_private.supports_temp)
618 misc_deregister(&pcipcwd_temp_miscdev);
619 err_out_unregister_reboot:
620 unregister_reboot_notifier(&pcipcwd_notifier);
621 err_out_release_region:
622 pci_release_regions(pdev);
623 err_out_disable_device:
624 pci_disable_device(pdev);
625 return ret;
628 static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
630 /* Stop the timer before we leave */
631 if (!nowayout)
632 pcipcwd_stop();
634 /* Deregister */
635 misc_deregister(&pcipcwd_miscdev);
636 if (pcipcwd_private.supports_temp)
637 misc_deregister(&pcipcwd_temp_miscdev);
638 unregister_reboot_notifier(&pcipcwd_notifier);
639 pci_release_regions(pdev);
640 pci_disable_device(pdev);
641 cards_found--;
644 static struct pci_device_id pcipcwd_pci_tbl[] = {
645 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
646 PCI_ANY_ID, PCI_ANY_ID, },
647 { 0 }, /* End of list */
649 MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
651 static struct pci_driver pcipcwd_driver = {
652 .name = WATCHDOG_NAME,
653 .id_table = pcipcwd_pci_tbl,
654 .probe = pcipcwd_card_init,
655 .remove = __devexit_p(pcipcwd_card_exit),
658 static int __init pcipcwd_init_module(void)
660 spin_lock_init (&pcipcwd_private.io_lock);
662 return pci_register_driver(&pcipcwd_driver);
665 static void __exit pcipcwd_cleanup_module(void)
667 pci_unregister_driver(&pcipcwd_driver);
670 module_init(pcipcwd_init_module);
671 module_exit(pcipcwd_cleanup_module);
673 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
674 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
675 MODULE_LICENSE("GPL");
676 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
677 MODULE_ALIAS_MISCDEV(TEMP_MINOR);