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> /* For CONFIG_WATCHDOG_NOWAYOUT/... */
33 #include <linux/module.h> /* For module specific items */
34 #include <linux/moduleparam.h> /* For new moduleparam's */
35 #include <linux/types.h> /* For standard types (like size_t) */
36 #include <linux/errno.h> /* For the -ENODEV/... values */
37 #include <linux/kernel.h> /* For printk/panic/... */
38 #include <linux/delay.h> /* For mdelay function */
39 #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
40 #include <linux/watchdog.h> /* For the watchdog specific items */
41 #include <linux/notifier.h> /* For notifier support */
42 #include <linux/reboot.h> /* For reboot_notifier stuff */
43 #include <linux/init.h> /* For __init/__exit/... */
44 #include <linux/fs.h> /* For file operations */
45 #include <linux/pci.h> /* For pci functions */
46 #include <linux/ioport.h> /* For io-port access */
47 #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
49 #include <asm/uaccess.h> /* For copy_to_user/put_user/... */
50 #include <asm/io.h> /* For inb/outb/... */
52 /* Module and version information */
53 #define WATCHDOG_VERSION "1.01"
54 #define WATCHDOG_DATE "02 Sep 2005"
55 #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
56 #define WATCHDOG_NAME "pcwd_pci"
57 #define PFX WATCHDOG_NAME ": "
58 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
60 /* Stuff for the PCI ID's */
61 #ifndef PCI_VENDOR_ID_QUICKLOGIC
62 #define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
65 #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
66 #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
70 * These are the defines that describe the control status bits for the
71 * PCI-PC Watchdog card.
73 #define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
74 #define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
75 #define WD_PCI_TTRP 0x04 /* Temperature Trip status */
77 /* according to documentation max. time to process a command for the pci
78 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
79 #define PCI_COMMAND_TIMEOUT 150
81 /* Watchdog's internal commands */
82 #define CMD_GET_STATUS 0x04
83 #define CMD_GET_FIRMWARE_VERSION 0x08
84 #define CMD_READ_WATCHDOG_TIMEOUT 0x18
85 #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
87 /* We can only use 1 card due to the /dev/watchdog restriction */
88 static int cards_found;
90 /* internal variables */
91 static int temp_panic;
92 static unsigned long is_active;
93 static char expect_release;
95 int supports_temp; /* Wether or not the card has a temperature device */
96 int boot_status; /* The card's boot status */
97 unsigned long io_addr; /* The cards I/O address */
102 /* module parameters */
103 #define WATCHDOG_HEARTBEAT 2 /* 2 sec default heartbeat */
104 static int heartbeat = WATCHDOG_HEARTBEAT;
105 module_param(heartbeat, int, 0);
106 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
108 static int nowayout = WATCHDOG_NOWAYOUT;
109 module_param(nowayout, int, 0);
110 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
116 static int send_command(int cmd, int *msb, int *lsb)
118 int got_response, count;
120 spin_lock(&pcipcwd_private.io_lock);
121 /* If a command requires data it should be written first.
122 * Data for commands with 8 bits of data should be written to port 4.
123 * Commands with 16 bits of data, should be written as LSB to port 4
125 * After the required data has been written then write the command to
127 outb_p(*lsb, pcipcwd_private.io_addr + 4);
128 outb_p(*msb, pcipcwd_private.io_addr + 5);
129 outb_p(cmd, pcipcwd_private.io_addr + 6);
131 /* wait till the pci card processed the command, signaled by
132 * the WRSP bit in port 2 and give it a max. timeout of
133 * PCI_COMMAND_TIMEOUT to process */
134 got_response = inb_p(pcipcwd_private.io_addr + 2) & 0x40;
135 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
137 got_response = inb_p(pcipcwd_private.io_addr + 2) & 0x40;
141 /* read back response */
142 *lsb = inb_p(pcipcwd_private.io_addr + 4);
143 *msb = inb_p(pcipcwd_private.io_addr + 5);
146 inb_p(pcipcwd_private.io_addr + 6);
148 spin_unlock(&pcipcwd_private.io_lock);
153 static int pcipcwd_start(void)
157 spin_lock(&pcipcwd_private.io_lock);
158 outb_p(0x00, pcipcwd_private.io_addr + 3);
161 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
162 spin_unlock(&pcipcwd_private.io_lock);
164 if (stat_reg & 0x10) {
165 printk(KERN_ERR PFX "Card timer not enabled\n");
172 static int pcipcwd_stop(void)
176 spin_lock(&pcipcwd_private.io_lock);
177 outb_p(0xA5, pcipcwd_private.io_addr + 3);
180 outb_p(0xA5, pcipcwd_private.io_addr + 3);
183 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
184 spin_unlock(&pcipcwd_private.io_lock);
186 if (!(stat_reg & 0x10)) {
187 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
194 static int pcipcwd_keepalive(void)
196 /* Re-trigger watchdog by writing to port 0 */
197 outb_p(0x42, pcipcwd_private.io_addr);
201 static int pcipcwd_set_heartbeat(int t)
206 if ((t < 0x0001) || (t > 0xFFFF))
209 /* Write new heartbeat to watchdog */
210 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
216 static int pcipcwd_get_status(int *status)
221 new_status = inb_p(pcipcwd_private.io_addr + 1);
222 if (new_status & WD_PCI_WTRP)
223 *status |= WDIOF_CARDRESET;
224 if (new_status & WD_PCI_TTRP) {
225 *status |= WDIOF_OVERHEAT;
227 panic(PFX "Temperature overheat trip!\n");
233 static int pcipcwd_clear_status(void)
235 outb_p(0x01, pcipcwd_private.io_addr + 1);
239 static int pcipcwd_get_temperature(int *temperature)
242 if (!pcipcwd_private.supports_temp)
246 * Convert celsius to fahrenheit, since this was
247 * the decided 'standard' for this return value.
249 *temperature = ((inb_p(pcipcwd_private.io_addr)) * 9 / 5) + 32;
255 * /dev/watchdog handling
258 static ssize_t pcipcwd_write(struct file *file, const char __user *data,
259 size_t len, loff_t *ppos)
261 /* See if we got the magic character 'V' and reload the timer */
266 /* note: just in case someone wrote the magic character
267 * five months ago... */
270 /* scan to see whether or not we got the magic character */
271 for (i = 0; i != len; i++) {
273 if(get_user(c, data+i))
280 /* someone wrote to us, we should reload the timer */
286 static int pcipcwd_ioctl(struct inode *inode, struct file *file,
287 unsigned int cmd, unsigned long arg)
289 void __user *argp = (void __user *)arg;
290 int __user *p = argp;
291 static struct watchdog_info ident = {
292 .options = WDIOF_OVERHEAT |
294 WDIOF_KEEPALIVEPING |
297 .firmware_version = 1,
298 .identity = WATCHDOG_DRIVER_NAME,
302 case WDIOC_GETSUPPORT:
303 return copy_to_user(argp, &ident,
304 sizeof (ident)) ? -EFAULT : 0;
306 case WDIOC_GETSTATUS:
310 pcipcwd_get_status(&status);
312 return put_user(status, p);
315 case WDIOC_GETBOOTSTATUS:
316 return put_user(pcipcwd_private.boot_status, p);
322 if (pcipcwd_get_temperature(&temperature))
325 return put_user(temperature, p);
328 case WDIOC_KEEPALIVE:
332 case WDIOC_SETOPTIONS:
334 int new_options, retval = -EINVAL;
336 if (get_user (new_options, p))
339 if (new_options & WDIOS_DISABLECARD) {
345 if (new_options & WDIOS_ENABLECARD) {
351 if (new_options & WDIOS_TEMPPANIC) {
359 case WDIOC_SETTIMEOUT:
363 if (get_user(new_heartbeat, p))
366 if (pcipcwd_set_heartbeat(new_heartbeat))
373 case WDIOC_GETTIMEOUT:
374 return put_user(heartbeat, p);
381 static int pcipcwd_open(struct inode *inode, struct file *file)
383 /* /dev/watchdog can only be opened once */
384 if (test_and_set_bit(0, &is_active))
390 return nonseekable_open(inode, file);
393 static int pcipcwd_release(struct inode *inode, struct file *file)
396 * Shut off the timer.
398 if (expect_release == 42) {
401 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
405 clear_bit(0, &is_active);
410 * /dev/temperature handling
413 static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
414 size_t len, loff_t *ppos)
418 if (pcipcwd_get_temperature(&temperature))
421 if (copy_to_user (data, &temperature, 1))
427 static int pcipcwd_temp_open(struct inode *inode, struct file *file)
429 if (!pcipcwd_private.supports_temp)
432 return nonseekable_open(inode, file);
435 static int pcipcwd_temp_release(struct inode *inode, struct file *file)
444 static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
446 if (code==SYS_DOWN || code==SYS_HALT) {
447 /* Turn the WDT off */
458 static struct file_operations pcipcwd_fops = {
459 .owner = THIS_MODULE,
461 .write = pcipcwd_write,
462 .ioctl = pcipcwd_ioctl,
463 .open = pcipcwd_open,
464 .release = pcipcwd_release,
467 static struct miscdevice pcipcwd_miscdev = {
468 .minor = WATCHDOG_MINOR,
470 .fops = &pcipcwd_fops,
473 static struct file_operations pcipcwd_temp_fops = {
474 .owner = THIS_MODULE,
476 .read = pcipcwd_temp_read,
477 .open = pcipcwd_temp_open,
478 .release = pcipcwd_temp_release,
481 static struct miscdevice pcipcwd_temp_miscdev = {
483 .name = "temperature",
484 .fops = &pcipcwd_temp_fops,
487 static struct notifier_block pcipcwd_notifier = {
488 .notifier_call = pcipcwd_notify_sys,
492 * Init & exit routines
495 static inline void check_temperature_support(void)
497 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
498 pcipcwd_private.supports_temp = 1;
501 static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
502 const struct pci_device_id *ent)
505 int got_fw_rev, fw_rev_major, fw_rev_minor;
507 char option_switches;
510 if (cards_found == 1)
511 printk(KERN_INFO PFX DRIVER_VERSION);
513 if (cards_found > 1) {
514 printk(KERN_ERR PFX "This driver only supports 1 device\n");
518 if (pci_enable_device(pdev)) {
519 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
523 if (pci_resource_start(pdev, 0) == 0x0000) {
524 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
526 goto err_out_disable_device;
529 pcipcwd_private.pdev = pdev;
530 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
532 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
533 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
534 (int) pcipcwd_private.io_addr);
536 goto err_out_disable_device;
539 /* get the boot_status */
540 pcipcwd_get_status(&pcipcwd_private.boot_status);
542 /* clear the "card caused reboot" flag */
543 pcipcwd_clear_status();
548 /* Check whether or not the card supports the temperature device */
549 check_temperature_support();
551 /* Get the Firmware Version */
552 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
554 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
556 sprintf(fw_ver_str, "<card no answer>");
559 /* Get switch settings */
560 option_switches = inb_p(pcipcwd_private.io_addr + 3);
562 printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
563 (int) pcipcwd_private.io_addr, fw_ver_str,
564 (pcipcwd_private.supports_temp ? "with" : "without"));
566 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
568 ((option_switches & 0x10) ? "ON" : "OFF"),
569 ((option_switches & 0x08) ? "ON" : "OFF"));
571 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
572 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
574 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
575 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
577 if (pcipcwd_private.boot_status == 0)
578 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
580 /* Check that the heartbeat value is within it's range ; if not reset to the default */
581 if (pcipcwd_set_heartbeat(heartbeat)) {
582 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
583 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
587 ret = register_reboot_notifier(&pcipcwd_notifier);
589 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
591 goto err_out_release_region;
594 if (pcipcwd_private.supports_temp) {
595 ret = misc_register(&pcipcwd_temp_miscdev);
597 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
599 goto err_out_unregister_reboot;
603 ret = misc_register(&pcipcwd_miscdev);
605 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
606 WATCHDOG_MINOR, ret);
607 goto err_out_misc_deregister;
610 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
611 heartbeat, nowayout);
615 err_out_misc_deregister:
616 if (pcipcwd_private.supports_temp)
617 misc_deregister(&pcipcwd_temp_miscdev);
618 err_out_unregister_reboot:
619 unregister_reboot_notifier(&pcipcwd_notifier);
620 err_out_release_region:
621 pci_release_regions(pdev);
622 err_out_disable_device:
623 pci_disable_device(pdev);
627 static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
629 /* Stop the timer before we leave */
634 misc_deregister(&pcipcwd_miscdev);
635 if (pcipcwd_private.supports_temp)
636 misc_deregister(&pcipcwd_temp_miscdev);
637 unregister_reboot_notifier(&pcipcwd_notifier);
638 pci_release_regions(pdev);
639 pci_disable_device(pdev);
643 static struct pci_device_id pcipcwd_pci_tbl[] = {
644 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
645 PCI_ANY_ID, PCI_ANY_ID, },
646 { 0 }, /* End of list */
648 MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
650 static struct pci_driver pcipcwd_driver = {
651 .name = WATCHDOG_NAME,
652 .id_table = pcipcwd_pci_tbl,
653 .probe = pcipcwd_card_init,
654 .remove = __devexit_p(pcipcwd_card_exit),
657 static int __init pcipcwd_init_module(void)
659 spin_lock_init (&pcipcwd_private.io_lock);
661 return pci_register_driver(&pcipcwd_driver);
664 static void __exit pcipcwd_cleanup_module(void)
666 pci_unregister_driver(&pcipcwd_driver);
669 module_init(pcipcwd_init_module);
670 module_exit(pcipcwd_cleanup_module);
672 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
673 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
674 MODULE_LICENSE("GPL");
675 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
676 MODULE_ALIAS_MISCDEV(TEMP_MINOR);