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/... */
50 #include <asm/uaccess.h> /* For copy_to_user/put_user/... */
51 #include <asm/io.h> /* For inb/outb/... */
53 /* Module and version information */
54 #define WATCHDOG_VERSION "1.03"
55 #define WATCHDOG_DATE "21 Jan 2007"
56 #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
57 #define WATCHDOG_NAME "pcwd_pci"
58 #define PFX WATCHDOG_NAME ": "
59 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
61 /* Stuff for the PCI ID's */
62 #ifndef PCI_VENDOR_ID_QUICKLOGIC
63 #define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
66 #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
67 #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
71 * These are the defines that describe the control status bits for the
72 * PCI-PC Watchdog card.
74 /* Port 1 : Control Status #1 */
75 #define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
76 #define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
77 #define WD_PCI_TTRP 0x04 /* Temperature Trip status */
78 #define WD_PCI_RL2A 0x08 /* Relay 2 Active */
79 #define WD_PCI_RL1A 0x10 /* Relay 1 Active */
80 #define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip/reset */
81 #define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
82 /* Port 2 : Control Status #2 */
83 #define WD_PCI_WDIS 0x10 /* Watchdog Disable */
84 #define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
85 #define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
86 #define WD_PCI_PCMD 0x80 /* PC has sent command */
88 /* according to documentation max. time to process a command for the pci
89 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
90 #define PCI_COMMAND_TIMEOUT 150
92 /* Watchdog's internal commands */
93 #define CMD_GET_STATUS 0x04
94 #define CMD_GET_FIRMWARE_VERSION 0x08
95 #define CMD_READ_WATCHDOG_TIMEOUT 0x18
96 #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
97 #define CMD_GET_CLEAR_RESET_COUNT 0x84
99 /* Watchdog's Dip Switch heartbeat values */
100 static const int heartbeat_tbl [] = {
101 5, /* OFF-OFF-OFF = 5 Sec */
102 10, /* OFF-OFF-ON = 10 Sec */
103 30, /* OFF-ON-OFF = 30 Sec */
104 60, /* OFF-ON-ON = 1 Min */
105 300, /* ON-OFF-OFF = 5 Min */
106 600, /* ON-OFF-ON = 10 Min */
107 1800, /* ON-ON-OFF = 30 Min */
108 3600, /* ON-ON-ON = 1 hour */
111 /* We can only use 1 card due to the /dev/watchdog restriction */
112 static int cards_found;
114 /* internal variables */
115 static int temp_panic;
116 static unsigned long is_active;
117 static char expect_release;
118 static struct { /* this is private data for each PCI-PC watchdog card */
119 int supports_temp; /* Wether or not the card has a temperature device */
120 int boot_status; /* The card's boot status */
121 unsigned long io_addr; /* The cards I/O address */
122 spinlock_t io_lock; /* the lock for io operations */
123 struct pci_dev *pdev; /* the PCI-device */
126 /* module parameters */
127 #define QUIET 0 /* Default */
128 #define VERBOSE 1 /* Verbose */
129 #define DEBUG 2 /* print fancy stuff too */
130 static int debug = QUIET;
131 module_param(debug, int, 0);
132 MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
134 #define WATCHDOG_HEARTBEAT 0 /* default heartbeat = delay-time from dip-switches */
135 static int heartbeat = WATCHDOG_HEARTBEAT;
136 module_param(heartbeat, int, 0);
137 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
139 static int nowayout = WATCHDOG_NOWAYOUT;
140 module_param(nowayout, int, 0);
141 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
147 static int send_command(int cmd, int *msb, int *lsb)
149 int got_response, count;
152 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
155 spin_lock(&pcipcwd_private.io_lock);
156 /* If a command requires data it should be written first.
157 * Data for commands with 8 bits of data should be written to port 4.
158 * Commands with 16 bits of data, should be written as LSB to port 4
160 * After the required data has been written then write the command to
162 outb_p(*lsb, pcipcwd_private.io_addr + 4);
163 outb_p(*msb, pcipcwd_private.io_addr + 5);
164 outb_p(cmd, pcipcwd_private.io_addr + 6);
166 /* wait till the pci card processed the command, signaled by
167 * the WRSP bit in port 2 and give it a max. timeout of
168 * PCI_COMMAND_TIMEOUT to process */
169 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
170 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
172 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
175 if (debug >= DEBUG) {
177 printk(KERN_DEBUG PFX "time to process command was: %d ms\n",
180 printk(KERN_DEBUG PFX "card did not respond on command!\n");
185 /* read back response */
186 *lsb = inb_p(pcipcwd_private.io_addr + 4);
187 *msb = inb_p(pcipcwd_private.io_addr + 5);
190 inb_p(pcipcwd_private.io_addr + 6);
193 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
197 spin_unlock(&pcipcwd_private.io_lock);
202 static inline void pcipcwd_check_temperature_support(void)
204 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
205 pcipcwd_private.supports_temp = 1;
208 static int pcipcwd_get_option_switches(void)
212 option_switches = inb_p(pcipcwd_private.io_addr + 3);
213 return option_switches;
216 static void pcipcwd_show_card_info(void)
218 int got_fw_rev, fw_rev_major, fw_rev_minor;
219 char fw_ver_str[20]; /* The cards firmware version */
222 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
224 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
226 sprintf(fw_ver_str, "<card no answer>");
229 /* Get switch settings */
230 option_switches = pcipcwd_get_option_switches();
232 printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
233 (int) pcipcwd_private.io_addr, fw_ver_str,
234 (pcipcwd_private.supports_temp ? "with" : "without"));
236 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
238 ((option_switches & 0x10) ? "ON" : "OFF"),
239 ((option_switches & 0x08) ? "ON" : "OFF"));
241 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
242 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
244 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
245 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
247 if (pcipcwd_private.boot_status == 0)
248 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
251 static int pcipcwd_start(void)
255 spin_lock(&pcipcwd_private.io_lock);
256 outb_p(0x00, pcipcwd_private.io_addr + 3);
259 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
260 spin_unlock(&pcipcwd_private.io_lock);
262 if (stat_reg & WD_PCI_WDIS) {
263 printk(KERN_ERR PFX "Card timer not enabled\n");
267 if (debug >= VERBOSE)
268 printk(KERN_DEBUG PFX "Watchdog started\n");
273 static int pcipcwd_stop(void)
277 spin_lock(&pcipcwd_private.io_lock);
278 outb_p(0xA5, pcipcwd_private.io_addr + 3);
281 outb_p(0xA5, pcipcwd_private.io_addr + 3);
284 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
285 spin_unlock(&pcipcwd_private.io_lock);
287 if (!(stat_reg & WD_PCI_WDIS)) {
288 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
292 if (debug >= VERBOSE)
293 printk(KERN_DEBUG PFX "Watchdog stopped\n");
298 static int pcipcwd_keepalive(void)
300 /* Re-trigger watchdog by writing to port 0 */
301 spin_lock(&pcipcwd_private.io_lock);
302 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
303 spin_unlock(&pcipcwd_private.io_lock);
306 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
311 static int pcipcwd_set_heartbeat(int t)
316 if ((t < 0x0001) || (t > 0xFFFF))
319 /* Write new heartbeat to watchdog */
320 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
323 if (debug >= VERBOSE)
324 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
330 static int pcipcwd_get_status(int *status)
335 control_status = inb_p(pcipcwd_private.io_addr + 1);
336 if (control_status & WD_PCI_WTRP)
337 *status |= WDIOF_CARDRESET;
338 if (control_status & WD_PCI_TTRP) {
339 *status |= WDIOF_OVERHEAT;
341 panic(PFX "Temperature overheat trip!\n");
345 printk(KERN_DEBUG PFX "Control Status #1: 0x%02x\n",
351 static int pcipcwd_clear_status(void)
357 if (debug >= VERBOSE)
358 printk(KERN_INFO PFX "clearing watchdog trip status & LED\n");
360 control_status = inb_p(pcipcwd_private.io_addr + 1);
362 if (debug >= DEBUG) {
363 printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
364 printk(KERN_DEBUG PFX "sending: 0x%02x\n",
365 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
368 /* clear trip status & LED and keep mode of relay 2 */
369 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP, pcipcwd_private.io_addr + 1);
371 /* clear reset counter */
374 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
376 if (debug >= DEBUG) {
377 printk(KERN_DEBUG PFX "reset count was: 0x%02x\n",
384 static int pcipcwd_get_temperature(int *temperature)
387 if (!pcipcwd_private.supports_temp)
390 spin_lock(&pcipcwd_private.io_lock);
391 *temperature = inb_p(pcipcwd_private.io_addr);
392 spin_unlock(&pcipcwd_private.io_lock);
395 * Convert celsius to fahrenheit, since this was
396 * the decided 'standard' for this return value.
398 *temperature = (*temperature * 9 / 5) + 32;
400 if (debug >= DEBUG) {
401 printk(KERN_DEBUG PFX "temperature is: %d F\n",
408 static int pcipcwd_get_timeleft(int *time_left)
413 /* Read the time that's left before rebooting */
414 /* Note: if the board is not yet armed then we will read 0xFFFF */
415 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
417 *time_left = (msb << 8) + lsb;
419 if (debug >= VERBOSE)
420 printk(KERN_DEBUG PFX "Time left before next reboot: %d\n",
427 * /dev/watchdog handling
430 static ssize_t pcipcwd_write(struct file *file, const char __user *data,
431 size_t len, loff_t *ppos)
433 /* See if we got the magic character 'V' and reload the timer */
438 /* note: just in case someone wrote the magic character
439 * five months ago... */
442 /* scan to see whether or not we got the magic character */
443 for (i = 0; i != len; i++) {
445 if(get_user(c, data+i))
452 /* someone wrote to us, we should reload the timer */
458 static int pcipcwd_ioctl(struct inode *inode, struct file *file,
459 unsigned int cmd, unsigned long arg)
461 void __user *argp = (void __user *)arg;
462 int __user *p = argp;
463 static struct watchdog_info ident = {
464 .options = WDIOF_OVERHEAT |
466 WDIOF_KEEPALIVEPING |
469 .firmware_version = 1,
470 .identity = WATCHDOG_DRIVER_NAME,
474 case WDIOC_GETSUPPORT:
475 return copy_to_user(argp, &ident,
476 sizeof (ident)) ? -EFAULT : 0;
478 case WDIOC_GETSTATUS:
482 pcipcwd_get_status(&status);
484 return put_user(status, p);
487 case WDIOC_GETBOOTSTATUS:
488 return put_user(pcipcwd_private.boot_status, p);
494 if (pcipcwd_get_temperature(&temperature))
497 return put_user(temperature, p);
500 case WDIOC_KEEPALIVE:
504 case WDIOC_SETOPTIONS:
506 int new_options, retval = -EINVAL;
508 if (get_user (new_options, p))
511 if (new_options & WDIOS_DISABLECARD) {
517 if (new_options & WDIOS_ENABLECARD) {
523 if (new_options & WDIOS_TEMPPANIC) {
531 case WDIOC_SETTIMEOUT:
535 if (get_user(new_heartbeat, p))
538 if (pcipcwd_set_heartbeat(new_heartbeat))
545 case WDIOC_GETTIMEOUT:
546 return put_user(heartbeat, p);
548 case WDIOC_GETTIMELEFT:
552 if (pcipcwd_get_timeleft(&time_left))
555 return put_user(time_left, p);
563 static int pcipcwd_open(struct inode *inode, struct file *file)
565 /* /dev/watchdog can only be opened once */
566 if (test_and_set_bit(0, &is_active)) {
567 if (debug >= VERBOSE)
568 printk(KERN_ERR PFX "Attempt to open already opened device.\n");
575 return nonseekable_open(inode, file);
578 static int pcipcwd_release(struct inode *inode, struct file *file)
581 * Shut off the timer.
583 if (expect_release == 42) {
586 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
590 clear_bit(0, &is_active);
595 * /dev/temperature handling
598 static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
599 size_t len, loff_t *ppos)
603 if (pcipcwd_get_temperature(&temperature))
606 if (copy_to_user (data, &temperature, 1))
612 static int pcipcwd_temp_open(struct inode *inode, struct file *file)
614 if (!pcipcwd_private.supports_temp)
617 return nonseekable_open(inode, file);
620 static int pcipcwd_temp_release(struct inode *inode, struct file *file)
629 static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
631 if (code==SYS_DOWN || code==SYS_HALT) {
632 /* Turn the WDT off */
643 static const struct file_operations pcipcwd_fops = {
644 .owner = THIS_MODULE,
646 .write = pcipcwd_write,
647 .ioctl = pcipcwd_ioctl,
648 .open = pcipcwd_open,
649 .release = pcipcwd_release,
652 static struct miscdevice pcipcwd_miscdev = {
653 .minor = WATCHDOG_MINOR,
655 .fops = &pcipcwd_fops,
658 static const struct file_operations pcipcwd_temp_fops = {
659 .owner = THIS_MODULE,
661 .read = pcipcwd_temp_read,
662 .open = pcipcwd_temp_open,
663 .release = pcipcwd_temp_release,
666 static struct miscdevice pcipcwd_temp_miscdev = {
668 .name = "temperature",
669 .fops = &pcipcwd_temp_fops,
672 static struct notifier_block pcipcwd_notifier = {
673 .notifier_call = pcipcwd_notify_sys,
677 * Init & exit routines
680 static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
681 const struct pci_device_id *ent)
686 if (cards_found == 1)
687 printk(KERN_INFO PFX DRIVER_VERSION);
689 if (cards_found > 1) {
690 printk(KERN_ERR PFX "This driver only supports 1 device\n");
694 if (pci_enable_device(pdev)) {
695 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
699 if (pci_resource_start(pdev, 0) == 0x0000) {
700 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
702 goto err_out_disable_device;
705 pcipcwd_private.pdev = pdev;
706 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
708 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
709 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
710 (int) pcipcwd_private.io_addr);
712 goto err_out_disable_device;
715 /* get the boot_status */
716 pcipcwd_get_status(&pcipcwd_private.boot_status);
718 /* clear the "card caused reboot" flag */
719 pcipcwd_clear_status();
724 /* Check whether or not the card supports the temperature device */
725 pcipcwd_check_temperature_support();
727 /* Show info about the card itself */
728 pcipcwd_show_card_info();
730 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
732 heartbeat = heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
734 /* Check that the heartbeat value is within it's range ; if not reset to the default */
735 if (pcipcwd_set_heartbeat(heartbeat)) {
736 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
737 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
741 ret = register_reboot_notifier(&pcipcwd_notifier);
743 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
745 goto err_out_release_region;
748 if (pcipcwd_private.supports_temp) {
749 ret = misc_register(&pcipcwd_temp_miscdev);
751 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
753 goto err_out_unregister_reboot;
757 ret = misc_register(&pcipcwd_miscdev);
759 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
760 WATCHDOG_MINOR, ret);
761 goto err_out_misc_deregister;
764 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
765 heartbeat, nowayout);
769 err_out_misc_deregister:
770 if (pcipcwd_private.supports_temp)
771 misc_deregister(&pcipcwd_temp_miscdev);
772 err_out_unregister_reboot:
773 unregister_reboot_notifier(&pcipcwd_notifier);
774 err_out_release_region:
775 pci_release_regions(pdev);
776 err_out_disable_device:
777 pci_disable_device(pdev);
781 static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
783 /* Stop the timer before we leave */
788 misc_deregister(&pcipcwd_miscdev);
789 if (pcipcwd_private.supports_temp)
790 misc_deregister(&pcipcwd_temp_miscdev);
791 unregister_reboot_notifier(&pcipcwd_notifier);
792 pci_release_regions(pdev);
793 pci_disable_device(pdev);
797 static struct pci_device_id pcipcwd_pci_tbl[] = {
798 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
799 PCI_ANY_ID, PCI_ANY_ID, },
800 { 0 }, /* End of list */
802 MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
804 static struct pci_driver pcipcwd_driver = {
805 .name = WATCHDOG_NAME,
806 .id_table = pcipcwd_pci_tbl,
807 .probe = pcipcwd_card_init,
808 .remove = __devexit_p(pcipcwd_card_exit),
811 static int __init pcipcwd_init_module(void)
813 spin_lock_init(&pcipcwd_private.io_lock);
815 return pci_register_driver(&pcipcwd_driver);
818 static void __exit pcipcwd_cleanup_module(void)
820 pci_unregister_driver(&pcipcwd_driver);
822 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
825 module_init(pcipcwd_init_module);
826 module_exit(pcipcwd_cleanup_module);
828 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
829 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
830 MODULE_LICENSE("GPL");
831 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
832 MODULE_ALIAS_MISCDEV(TEMP_MINOR);