2 * Berkshire PCI-PC Watchdog Card Driver
4 * (c) Copyright 2003-2007 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:
25 * http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
27 * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
31 * Includes, defines, variables, module parameters, ...
34 #include <linux/module.h> /* For module specific items */
35 #include <linux/moduleparam.h> /* For new moduleparam's */
36 #include <linux/types.h> /* For standard types (like size_t) */
37 #include <linux/errno.h> /* For the -ENODEV/... values */
38 #include <linux/kernel.h> /* For printk/panic/... */
39 #include <linux/delay.h> /* For mdelay function */
40 #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
41 #include <linux/watchdog.h> /* For the watchdog specific items */
42 #include <linux/notifier.h> /* For notifier support */
43 #include <linux/reboot.h> /* For reboot_notifier stuff */
44 #include <linux/init.h> /* For __init/__exit/... */
45 #include <linux/fs.h> /* For file operations */
46 #include <linux/pci.h> /* For pci functions */
47 #include <linux/ioport.h> /* For io-port access */
48 #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
49 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
50 #include <linux/io.h> /* For inb/outb/... */
52 /* Module and version information */
53 #define WATCHDOG_VERSION "1.03"
54 #define WATCHDOG_DATE "21 Jan 2007"
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 /* Port 1 : Control Status #1 */
74 #define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
75 #define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
76 #define WD_PCI_TTRP 0x04 /* Temperature Trip status */
77 #define WD_PCI_RL2A 0x08 /* Relay 2 Active */
78 #define WD_PCI_RL1A 0x10 /* Relay 1 Active */
79 #define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip/reset */
80 #define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
81 /* Port 2 : Control Status #2 */
82 #define WD_PCI_WDIS 0x10 /* Watchdog Disable */
83 #define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
84 #define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
85 #define WD_PCI_PCMD 0x80 /* PC has sent command */
87 /* according to documentation max. time to process a command for the pci
88 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
89 #define PCI_COMMAND_TIMEOUT 150
91 /* Watchdog's internal commands */
92 #define CMD_GET_STATUS 0x04
93 #define CMD_GET_FIRMWARE_VERSION 0x08
94 #define CMD_READ_WATCHDOG_TIMEOUT 0x18
95 #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
96 #define CMD_GET_CLEAR_RESET_COUNT 0x84
98 /* Watchdog's Dip Switch heartbeat values */
99 static const int heartbeat_tbl[] = {
100 5, /* OFF-OFF-OFF = 5 Sec */
101 10, /* OFF-OFF-ON = 10 Sec */
102 30, /* OFF-ON-OFF = 30 Sec */
103 60, /* OFF-ON-ON = 1 Min */
104 300, /* ON-OFF-OFF = 5 Min */
105 600, /* ON-OFF-ON = 10 Min */
106 1800, /* ON-ON-OFF = 30 Min */
107 3600, /* ON-ON-ON = 1 hour */
110 /* We can only use 1 card due to the /dev/watchdog restriction */
111 static int cards_found;
113 /* internal variables */
114 static int temp_panic;
115 static unsigned long is_active;
116 static char expect_release;
117 static struct { /* this is private data for each PCI-PC watchdog card */
118 int supports_temp; /* Wether or not the card has a temperature device */
119 int boot_status; /* The card's boot status */
120 unsigned long io_addr; /* The cards I/O address */
121 spinlock_t io_lock; /* the lock for io operations */
122 struct pci_dev *pdev; /* the PCI-device */
125 /* module parameters */
126 #define QUIET 0 /* Default */
127 #define VERBOSE 1 /* Verbose */
128 #define DEBUG 2 /* print fancy stuff too */
129 static int debug = QUIET;
130 module_param(debug, int, 0);
131 MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
133 #define WATCHDOG_HEARTBEAT 0 /* default heartbeat = delay-time from dip-switches */
134 static int heartbeat = WATCHDOG_HEARTBEAT;
135 module_param(heartbeat, int, 0);
136 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
138 static int nowayout = WATCHDOG_NOWAYOUT;
139 module_param(nowayout, int, 0);
140 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
146 static int send_command(int cmd, int *msb, int *lsb)
148 int got_response, count;
151 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
154 spin_lock(&pcipcwd_private.io_lock);
155 /* If a command requires data it should be written first.
156 * Data for commands with 8 bits of data should be written to port 4.
157 * Commands with 16 bits of data, should be written as LSB to port 4
159 * After the required data has been written then write the command to
161 outb_p(*lsb, pcipcwd_private.io_addr + 4);
162 outb_p(*msb, pcipcwd_private.io_addr + 5);
163 outb_p(cmd, pcipcwd_private.io_addr + 6);
165 /* wait till the pci card processed the command, signaled by
166 * the WRSP bit in port 2 and give it a max. timeout of
167 * PCI_COMMAND_TIMEOUT to process */
168 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
169 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
171 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
174 if (debug >= DEBUG) {
176 printk(KERN_DEBUG PFX "time to process command was: %d ms\n",
179 printk(KERN_DEBUG PFX "card did not respond on command!\n");
184 /* read back response */
185 *lsb = inb_p(pcipcwd_private.io_addr + 4);
186 *msb = inb_p(pcipcwd_private.io_addr + 5);
189 inb_p(pcipcwd_private.io_addr + 6);
192 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
196 spin_unlock(&pcipcwd_private.io_lock);
201 static inline void pcipcwd_check_temperature_support(void)
203 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
204 pcipcwd_private.supports_temp = 1;
207 static int pcipcwd_get_option_switches(void)
211 option_switches = inb_p(pcipcwd_private.io_addr + 3);
212 return option_switches;
215 static void pcipcwd_show_card_info(void)
217 int got_fw_rev, fw_rev_major, fw_rev_minor;
218 char fw_ver_str[20]; /* The cards firmware version */
221 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
223 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
225 sprintf(fw_ver_str, "<card no answer>");
227 /* Get switch settings */
228 option_switches = pcipcwd_get_option_switches();
230 printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
231 (int) pcipcwd_private.io_addr, fw_ver_str,
232 (pcipcwd_private.supports_temp ? "with" : "without"));
234 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
236 ((option_switches & 0x10) ? "ON" : "OFF"),
237 ((option_switches & 0x08) ? "ON" : "OFF"));
239 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
240 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
242 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
243 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
245 if (pcipcwd_private.boot_status == 0)
246 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
249 static int pcipcwd_start(void)
253 spin_lock(&pcipcwd_private.io_lock);
254 outb_p(0x00, pcipcwd_private.io_addr + 3);
257 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
258 spin_unlock(&pcipcwd_private.io_lock);
260 if (stat_reg & WD_PCI_WDIS) {
261 printk(KERN_ERR PFX "Card timer not enabled\n");
265 if (debug >= VERBOSE)
266 printk(KERN_DEBUG PFX "Watchdog started\n");
271 static int pcipcwd_stop(void)
275 spin_lock(&pcipcwd_private.io_lock);
276 outb_p(0xA5, pcipcwd_private.io_addr + 3);
279 outb_p(0xA5, pcipcwd_private.io_addr + 3);
282 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
283 spin_unlock(&pcipcwd_private.io_lock);
285 if (!(stat_reg & WD_PCI_WDIS)) {
286 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
290 if (debug >= VERBOSE)
291 printk(KERN_DEBUG PFX "Watchdog stopped\n");
296 static int pcipcwd_keepalive(void)
298 /* Re-trigger watchdog by writing to port 0 */
299 spin_lock(&pcipcwd_private.io_lock);
300 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
301 spin_unlock(&pcipcwd_private.io_lock);
304 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
309 static int pcipcwd_set_heartbeat(int t)
314 if ((t < 0x0001) || (t > 0xFFFF))
317 /* Write new heartbeat to watchdog */
318 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
321 if (debug >= VERBOSE)
322 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
328 static int pcipcwd_get_status(int *status)
333 control_status = inb_p(pcipcwd_private.io_addr + 1);
334 if (control_status & WD_PCI_WTRP)
335 *status |= WDIOF_CARDRESET;
336 if (control_status & WD_PCI_TTRP) {
337 *status |= WDIOF_OVERHEAT;
339 panic(PFX "Temperature overheat trip!\n");
343 printk(KERN_DEBUG PFX "Control Status #1: 0x%02x\n",
349 static int pcipcwd_clear_status(void)
355 if (debug >= VERBOSE)
356 printk(KERN_INFO PFX "clearing watchdog trip status & LED\n");
358 control_status = inb_p(pcipcwd_private.io_addr + 1);
360 if (debug >= DEBUG) {
361 printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
362 printk(KERN_DEBUG PFX "sending: 0x%02x\n",
363 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
366 /* clear trip status & LED and keep mode of relay 2 */
367 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP, pcipcwd_private.io_addr + 1);
369 /* clear reset counter */
371 reset_counter = 0xff;
372 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
374 if (debug >= DEBUG) {
375 printk(KERN_DEBUG PFX "reset count was: 0x%02x\n",
382 static int pcipcwd_get_temperature(int *temperature)
385 if (!pcipcwd_private.supports_temp)
388 spin_lock(&pcipcwd_private.io_lock);
389 *temperature = inb_p(pcipcwd_private.io_addr);
390 spin_unlock(&pcipcwd_private.io_lock);
393 * Convert celsius to fahrenheit, since this was
394 * the decided 'standard' for this return value.
396 *temperature = (*temperature * 9 / 5) + 32;
398 if (debug >= DEBUG) {
399 printk(KERN_DEBUG PFX "temperature is: %d F\n",
406 static int pcipcwd_get_timeleft(int *time_left)
411 /* Read the time that's left before rebooting */
412 /* Note: if the board is not yet armed then we will read 0xFFFF */
413 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
415 *time_left = (msb << 8) + lsb;
417 if (debug >= VERBOSE)
418 printk(KERN_DEBUG PFX "Time left before next reboot: %d\n",
425 * /dev/watchdog handling
428 static ssize_t pcipcwd_write(struct file *file, const char __user *data,
429 size_t len, loff_t *ppos)
431 /* See if we got the magic character 'V' and reload the timer */
436 /* note: just in case someone wrote the magic character
437 * five months ago... */
440 /* scan to see whether or not we got the magic character */
441 for (i = 0; i != len; i++) {
443 if (get_user(c, data + i))
450 /* someone wrote to us, we should reload the timer */
456 static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
459 void __user *argp = (void __user *)arg;
460 int __user *p = argp;
461 static struct watchdog_info ident = {
462 .options = WDIOF_OVERHEAT |
464 WDIOF_KEEPALIVEPING |
467 .firmware_version = 1,
468 .identity = WATCHDOG_DRIVER_NAME,
472 case WDIOC_GETSUPPORT:
473 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
475 case WDIOC_GETSTATUS:
478 pcipcwd_get_status(&status);
479 return put_user(status, p);
482 case WDIOC_GETBOOTSTATUS:
483 return put_user(pcipcwd_private.boot_status, p);
489 if (pcipcwd_get_temperature(&temperature))
492 return put_user(temperature, p);
495 case WDIOC_SETOPTIONS:
497 int new_options, retval = -EINVAL;
499 if (get_user(new_options, p))
502 if (new_options & WDIOS_DISABLECARD) {
508 if (new_options & WDIOS_ENABLECARD) {
514 if (new_options & WDIOS_TEMPPANIC) {
522 case WDIOC_KEEPALIVE:
526 case WDIOC_SETTIMEOUT:
530 if (get_user(new_heartbeat, p))
533 if (pcipcwd_set_heartbeat(new_heartbeat))
540 case WDIOC_GETTIMEOUT:
541 return put_user(heartbeat, p);
543 case WDIOC_GETTIMELEFT:
547 if (pcipcwd_get_timeleft(&time_left))
550 return put_user(time_left, p);
558 static int pcipcwd_open(struct inode *inode, struct file *file)
560 /* /dev/watchdog can only be opened once */
561 if (test_and_set_bit(0, &is_active)) {
562 if (debug >= VERBOSE)
563 printk(KERN_ERR PFX "Attempt to open already opened device.\n");
570 return nonseekable_open(inode, file);
573 static int pcipcwd_release(struct inode *inode, struct file *file)
576 * Shut off the timer.
578 if (expect_release == 42) {
581 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
585 clear_bit(0, &is_active);
590 * /dev/temperature handling
593 static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
594 size_t len, loff_t *ppos)
598 if (pcipcwd_get_temperature(&temperature))
601 if (copy_to_user(data, &temperature, 1))
607 static int pcipcwd_temp_open(struct inode *inode, struct file *file)
609 if (!pcipcwd_private.supports_temp)
612 return nonseekable_open(inode, file);
615 static int pcipcwd_temp_release(struct inode *inode, struct file *file)
624 static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
626 if (code == SYS_DOWN || code == SYS_HALT)
627 pcipcwd_stop(); /* Turn the WDT off */
636 static const struct file_operations pcipcwd_fops = {
637 .owner = THIS_MODULE,
639 .write = pcipcwd_write,
640 .unlocked_ioctl = pcipcwd_ioctl,
641 .open = pcipcwd_open,
642 .release = pcipcwd_release,
645 static struct miscdevice pcipcwd_miscdev = {
646 .minor = WATCHDOG_MINOR,
648 .fops = &pcipcwd_fops,
651 static const struct file_operations pcipcwd_temp_fops = {
652 .owner = THIS_MODULE,
654 .read = pcipcwd_temp_read,
655 .open = pcipcwd_temp_open,
656 .release = pcipcwd_temp_release,
659 static struct miscdevice pcipcwd_temp_miscdev = {
661 .name = "temperature",
662 .fops = &pcipcwd_temp_fops,
665 static struct notifier_block pcipcwd_notifier = {
666 .notifier_call = pcipcwd_notify_sys,
670 * Init & exit routines
673 static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
674 const struct pci_device_id *ent)
679 if (cards_found == 1)
680 printk(KERN_INFO PFX DRIVER_VERSION);
682 if (cards_found > 1) {
683 printk(KERN_ERR PFX "This driver only supports 1 device\n");
687 if (pci_enable_device(pdev)) {
688 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
692 if (pci_resource_start(pdev, 0) == 0x0000) {
693 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
695 goto err_out_disable_device;
698 pcipcwd_private.pdev = pdev;
699 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
701 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
702 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
703 (int) pcipcwd_private.io_addr);
705 goto err_out_disable_device;
708 /* get the boot_status */
709 pcipcwd_get_status(&pcipcwd_private.boot_status);
711 /* clear the "card caused reboot" flag */
712 pcipcwd_clear_status();
717 /* Check whether or not the card supports the temperature device */
718 pcipcwd_check_temperature_support();
720 /* Show info about the card itself */
721 pcipcwd_show_card_info();
723 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
725 heartbeat = heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
727 /* Check that the heartbeat value is within it's range ; if not reset to the default */
728 if (pcipcwd_set_heartbeat(heartbeat)) {
729 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
730 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
734 ret = register_reboot_notifier(&pcipcwd_notifier);
736 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
738 goto err_out_release_region;
741 if (pcipcwd_private.supports_temp) {
742 ret = misc_register(&pcipcwd_temp_miscdev);
744 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
746 goto err_out_unregister_reboot;
750 ret = misc_register(&pcipcwd_miscdev);
752 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
753 WATCHDOG_MINOR, ret);
754 goto err_out_misc_deregister;
757 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
758 heartbeat, nowayout);
762 err_out_misc_deregister:
763 if (pcipcwd_private.supports_temp)
764 misc_deregister(&pcipcwd_temp_miscdev);
765 err_out_unregister_reboot:
766 unregister_reboot_notifier(&pcipcwd_notifier);
767 err_out_release_region:
768 pci_release_regions(pdev);
769 err_out_disable_device:
770 pci_disable_device(pdev);
774 static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
776 /* Stop the timer before we leave */
781 misc_deregister(&pcipcwd_miscdev);
782 if (pcipcwd_private.supports_temp)
783 misc_deregister(&pcipcwd_temp_miscdev);
784 unregister_reboot_notifier(&pcipcwd_notifier);
785 pci_release_regions(pdev);
786 pci_disable_device(pdev);
790 static struct pci_device_id pcipcwd_pci_tbl[] = {
791 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
792 PCI_ANY_ID, PCI_ANY_ID, },
793 { 0 }, /* End of list */
795 MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
797 static struct pci_driver pcipcwd_driver = {
798 .name = WATCHDOG_NAME,
799 .id_table = pcipcwd_pci_tbl,
800 .probe = pcipcwd_card_init,
801 .remove = __devexit_p(pcipcwd_card_exit),
804 static int __init pcipcwd_init_module(void)
806 spin_lock_init(&pcipcwd_private.io_lock);
808 return pci_register_driver(&pcipcwd_driver);
811 static void __exit pcipcwd_cleanup_module(void)
813 pci_unregister_driver(&pcipcwd_driver);
815 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
818 module_init(pcipcwd_init_module);
819 module_exit(pcipcwd_cleanup_module);
821 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
822 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
823 MODULE_LICENSE("GPL");
824 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
825 MODULE_ALIAS_MISCDEV(TEMP_MINOR);