1 // SPDX-License-Identifier: GPL-2.0+
3 * Berkshire PCI-PC Watchdog Card Driver
5 * (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
7 * Based on source code of the following authors:
8 * Ken Hollis <kenji@bitgate.com>,
9 * Lindsay Harris <lindsay@bluegum.com>,
10 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
11 * Matt Domsch <Matt_Domsch@dell.com>,
12 * Rob Radez <rob@osinvestor.com>
14 * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
15 * provide warranty for any of this software. This material is
16 * provided "AS-IS" and at no charge.
20 * A bells and whistles driver is available from:
21 * http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
23 * More info available at
24 * http://www.berkprod.com/ or http://www.pcwatchdog.com/
28 * Includes, defines, variables, module parameters, ...
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 struct miscdevice */
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/... */
48 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
49 #include <linux/io.h> /* For inb/outb/... */
51 /* Module and version information */
52 #define WATCHDOG_VERSION "1.03"
53 #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
54 #define WATCHDOG_NAME "pcwd_pci"
55 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
57 /* Stuff for the PCI ID's */
58 #ifndef PCI_VENDOR_ID_QUICKLOGIC
59 #define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
62 #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
63 #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
67 * These are the defines that describe the control status bits for the
68 * PCI-PC Watchdog card.
70 /* Port 1 : Control Status #1 */
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 */
74 #define WD_PCI_RL2A 0x08 /* Relay 2 Active */
75 #define WD_PCI_RL1A 0x10 /* Relay 1 Active */
76 #define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip /
78 #define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
79 /* Port 2 : Control Status #2 */
80 #define WD_PCI_WDIS 0x10 /* Watchdog Disable */
81 #define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
82 #define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
83 #define WD_PCI_PCMD 0x80 /* PC has sent command */
85 /* according to documentation max. time to process a command for the pci
86 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
87 #define PCI_COMMAND_TIMEOUT 150
89 /* Watchdog's internal commands */
90 #define CMD_GET_STATUS 0x04
91 #define CMD_GET_FIRMWARE_VERSION 0x08
92 #define CMD_READ_WATCHDOG_TIMEOUT 0x18
93 #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
94 #define CMD_GET_CLEAR_RESET_COUNT 0x84
96 /* Watchdog's Dip Switch heartbeat values */
97 static const int heartbeat_tbl[] = {
98 5, /* OFF-OFF-OFF = 5 Sec */
99 10, /* OFF-OFF-ON = 10 Sec */
100 30, /* OFF-ON-OFF = 30 Sec */
101 60, /* OFF-ON-ON = 1 Min */
102 300, /* ON-OFF-OFF = 5 Min */
103 600, /* ON-OFF-ON = 10 Min */
104 1800, /* ON-ON-OFF = 30 Min */
105 3600, /* ON-ON-ON = 1 hour */
108 /* We can only use 1 card due to the /dev/watchdog restriction */
109 static int cards_found;
111 /* internal variables */
112 static int temp_panic;
113 static unsigned long is_active;
114 static char expect_release;
115 /* this is private data for each PCI-PC watchdog card */
117 /* Wether or not the card has a temperature device */
119 /* The card's boot status */
121 /* The cards I/O address */
122 unsigned long io_addr;
123 /* the lock for io operations */
126 struct pci_dev *pdev;
129 /* module parameters */
130 #define QUIET 0 /* Default */
131 #define VERBOSE 1 /* Verbose */
132 #define DEBUG 2 /* print fancy stuff too */
133 static int debug = QUIET;
134 module_param(debug, int, 0);
135 MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
137 #define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
138 delay-time from dip-switches */
139 static int heartbeat = WATCHDOG_HEARTBEAT;
140 module_param(heartbeat, int, 0);
141 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
142 "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
143 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
145 static bool nowayout = WATCHDOG_NOWAYOUT;
146 module_param(nowayout, bool, 0);
147 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
148 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
154 static int send_command(int cmd, int *msb, int *lsb)
156 int got_response, count;
159 pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
162 spin_lock(&pcipcwd_private.io_lock);
163 /* If a command requires data it should be written first.
164 * Data for commands with 8 bits of data should be written to port 4.
165 * Commands with 16 bits of data, should be written as LSB to port 4
167 * After the required data has been written then write the command to
169 outb_p(*lsb, pcipcwd_private.io_addr + 4);
170 outb_p(*msb, pcipcwd_private.io_addr + 5);
171 outb_p(cmd, pcipcwd_private.io_addr + 6);
173 /* wait till the pci card processed the command, signaled by
174 * the WRSP bit in port 2 and give it a max. timeout of
175 * PCI_COMMAND_TIMEOUT to process */
176 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
177 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
180 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
183 if (debug >= DEBUG) {
185 pr_debug("time to process command was: %d ms\n",
188 pr_debug("card did not respond on command!\n");
193 /* read back response */
194 *lsb = inb_p(pcipcwd_private.io_addr + 4);
195 *msb = inb_p(pcipcwd_private.io_addr + 5);
198 inb_p(pcipcwd_private.io_addr + 6);
201 pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
205 spin_unlock(&pcipcwd_private.io_lock);
210 static inline void pcipcwd_check_temperature_support(void)
212 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
213 pcipcwd_private.supports_temp = 1;
216 static int pcipcwd_get_option_switches(void)
220 option_switches = inb_p(pcipcwd_private.io_addr + 3);
221 return option_switches;
224 static void pcipcwd_show_card_info(void)
226 int got_fw_rev, fw_rev_major, fw_rev_minor;
227 char fw_ver_str[20]; /* The cards firmware version */
230 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major,
233 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
235 sprintf(fw_ver_str, "<card no answer>");
237 /* Get switch settings */
238 option_switches = pcipcwd_get_option_switches();
240 pr_info("Found card at port 0x%04x (Firmware: %s) %s temp option\n",
241 (int) pcipcwd_private.io_addr, fw_ver_str,
242 (pcipcwd_private.supports_temp ? "with" : "without"));
244 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
246 ((option_switches & 0x10) ? "ON" : "OFF"),
247 ((option_switches & 0x08) ? "ON" : "OFF"));
249 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
250 pr_info("Previous reset was caused by the Watchdog card\n");
252 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
253 pr_info("Card sensed a CPU Overheat\n");
255 if (pcipcwd_private.boot_status == 0)
256 pr_info("No previous trip detected - Cold boot or reset\n");
259 static int pcipcwd_start(void)
263 spin_lock(&pcipcwd_private.io_lock);
264 outb_p(0x00, pcipcwd_private.io_addr + 3);
267 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
268 spin_unlock(&pcipcwd_private.io_lock);
270 if (stat_reg & WD_PCI_WDIS) {
271 pr_err("Card timer not enabled\n");
275 if (debug >= VERBOSE)
276 pr_debug("Watchdog started\n");
281 static int pcipcwd_stop(void)
285 spin_lock(&pcipcwd_private.io_lock);
286 outb_p(0xA5, pcipcwd_private.io_addr + 3);
289 outb_p(0xA5, pcipcwd_private.io_addr + 3);
292 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
293 spin_unlock(&pcipcwd_private.io_lock);
295 if (!(stat_reg & WD_PCI_WDIS)) {
296 pr_err("Card did not acknowledge disable attempt\n");
300 if (debug >= VERBOSE)
301 pr_debug("Watchdog stopped\n");
306 static int pcipcwd_keepalive(void)
308 /* Re-trigger watchdog by writing to port 0 */
309 spin_lock(&pcipcwd_private.io_lock);
310 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
311 spin_unlock(&pcipcwd_private.io_lock);
314 pr_debug("Watchdog keepalive signal send\n");
319 static int pcipcwd_set_heartbeat(int t)
324 if ((t < 0x0001) || (t > 0xFFFF))
327 /* Write new heartbeat to watchdog */
328 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
331 if (debug >= VERBOSE)
332 pr_debug("New heartbeat: %d\n", heartbeat);
337 static int pcipcwd_get_status(int *status)
342 control_status = inb_p(pcipcwd_private.io_addr + 1);
343 if (control_status & WD_PCI_WTRP)
344 *status |= WDIOF_CARDRESET;
345 if (control_status & WD_PCI_TTRP) {
346 *status |= WDIOF_OVERHEAT;
348 panic(KBUILD_MODNAME ": Temperature overheat trip!\n");
352 pr_debug("Control Status #1: 0x%02x\n", control_status);
357 static int pcipcwd_clear_status(void)
363 if (debug >= VERBOSE)
364 pr_info("clearing watchdog trip status & LED\n");
366 control_status = inb_p(pcipcwd_private.io_addr + 1);
368 if (debug >= DEBUG) {
369 pr_debug("status was: 0x%02x\n", control_status);
370 pr_debug("sending: 0x%02x\n",
371 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
374 /* clear trip status & LED and keep mode of relay 2 */
375 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP,
376 pcipcwd_private.io_addr + 1);
378 /* clear reset counter */
380 reset_counter = 0xff;
381 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
383 if (debug >= DEBUG) {
384 pr_debug("reset count was: 0x%02x\n", reset_counter);
390 static int pcipcwd_get_temperature(int *temperature)
393 if (!pcipcwd_private.supports_temp)
396 spin_lock(&pcipcwd_private.io_lock);
397 *temperature = inb_p(pcipcwd_private.io_addr);
398 spin_unlock(&pcipcwd_private.io_lock);
401 * Convert celsius to fahrenheit, since this was
402 * the decided 'standard' for this return value.
404 *temperature = (*temperature * 9 / 5) + 32;
406 if (debug >= DEBUG) {
407 pr_debug("temperature is: %d F\n", *temperature);
413 static int pcipcwd_get_timeleft(int *time_left)
418 /* Read the time that's left before rebooting */
419 /* Note: if the board is not yet armed then we will read 0xFFFF */
420 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
422 *time_left = (msb << 8) + lsb;
424 if (debug >= VERBOSE)
425 pr_debug("Time left before next reboot: %d\n", *time_left);
431 * /dev/watchdog handling
434 static ssize_t pcipcwd_write(struct file *file, const char __user *data,
435 size_t len, loff_t *ppos)
437 /* See if we got the magic character 'V' and reload the timer */
442 /* note: just in case someone wrote the magic character
443 * five months ago... */
446 /* scan to see whether or not we got the
448 for (i = 0; i != len; i++) {
450 if (get_user(c, data + i))
457 /* someone wrote to us, we should reload the timer */
463 static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
466 void __user *argp = (void __user *)arg;
467 int __user *p = argp;
468 static const struct watchdog_info ident = {
469 .options = WDIOF_OVERHEAT |
471 WDIOF_KEEPALIVEPING |
474 .firmware_version = 1,
475 .identity = WATCHDOG_DRIVER_NAME,
479 case WDIOC_GETSUPPORT:
480 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
482 case WDIOC_GETSTATUS:
485 pcipcwd_get_status(&status);
486 return put_user(status, p);
489 case WDIOC_GETBOOTSTATUS:
490 return put_user(pcipcwd_private.boot_status, p);
496 if (pcipcwd_get_temperature(&temperature))
499 return put_user(temperature, p);
502 case WDIOC_SETOPTIONS:
504 int new_options, retval = -EINVAL;
506 if (get_user(new_options, p))
509 if (new_options & WDIOS_DISABLECARD) {
515 if (new_options & WDIOS_ENABLECARD) {
521 if (new_options & WDIOS_TEMPPANIC) {
529 case WDIOC_KEEPALIVE:
533 case WDIOC_SETTIMEOUT:
537 if (get_user(new_heartbeat, p))
540 if (pcipcwd_set_heartbeat(new_heartbeat))
547 case WDIOC_GETTIMEOUT:
548 return put_user(heartbeat, p);
550 case WDIOC_GETTIMELEFT:
554 if (pcipcwd_get_timeleft(&time_left))
557 return put_user(time_left, p);
565 static int pcipcwd_open(struct inode *inode, struct file *file)
567 /* /dev/watchdog can only be opened once */
568 if (test_and_set_bit(0, &is_active)) {
569 if (debug >= VERBOSE)
570 pr_err("Attempt to open already opened device\n");
577 return stream_open(inode, file);
580 static int pcipcwd_release(struct inode *inode, struct file *file)
583 * Shut off the timer.
585 if (expect_release == 42) {
588 pr_crit("Unexpected close, not stopping watchdog!\n");
592 clear_bit(0, &is_active);
597 * /dev/temperature handling
600 static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
601 size_t len, loff_t *ppos)
605 if (pcipcwd_get_temperature(&temperature))
608 if (copy_to_user(data, &temperature, 1))
614 static int pcipcwd_temp_open(struct inode *inode, struct file *file)
616 if (!pcipcwd_private.supports_temp)
619 return stream_open(inode, file);
622 static int pcipcwd_temp_release(struct inode *inode, struct file *file)
631 static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code,
634 if (code == SYS_DOWN || code == SYS_HALT)
635 pcipcwd_stop(); /* Turn the WDT off */
644 static const struct file_operations pcipcwd_fops = {
645 .owner = THIS_MODULE,
647 .write = pcipcwd_write,
648 .unlocked_ioctl = pcipcwd_ioctl,
649 .compat_ioctl = compat_ptr_ioctl,
650 .open = pcipcwd_open,
651 .release = pcipcwd_release,
654 static struct miscdevice pcipcwd_miscdev = {
655 .minor = WATCHDOG_MINOR,
657 .fops = &pcipcwd_fops,
660 static const struct file_operations pcipcwd_temp_fops = {
661 .owner = THIS_MODULE,
663 .read = pcipcwd_temp_read,
664 .open = pcipcwd_temp_open,
665 .release = pcipcwd_temp_release,
668 static struct miscdevice pcipcwd_temp_miscdev = {
670 .name = "temperature",
671 .fops = &pcipcwd_temp_fops,
674 static struct notifier_block pcipcwd_notifier = {
675 .notifier_call = pcipcwd_notify_sys,
679 * Init & exit routines
682 static int pcipcwd_card_init(struct pci_dev *pdev,
683 const struct pci_device_id *ent)
688 if (cards_found == 1)
689 pr_info("%s\n", DRIVER_VERSION);
691 if (cards_found > 1) {
692 pr_err("This driver only supports 1 device\n");
696 if (pci_enable_device(pdev)) {
697 pr_err("Not possible to enable PCI Device\n");
701 if (pci_resource_start(pdev, 0) == 0x0000) {
702 pr_err("No I/O-Address for card detected\n");
704 goto err_out_disable_device;
707 spin_lock_init(&pcipcwd_private.io_lock);
708 pcipcwd_private.pdev = pdev;
709 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
711 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
712 pr_err("I/O address 0x%04x already in use\n",
713 (int) pcipcwd_private.io_addr);
715 goto err_out_disable_device;
718 /* get the boot_status */
719 pcipcwd_get_status(&pcipcwd_private.boot_status);
721 /* clear the "card caused reboot" flag */
722 pcipcwd_clear_status();
727 /* Check whether or not the card supports the temperature device */
728 pcipcwd_check_temperature_support();
730 /* Show info about the card itself */
731 pcipcwd_show_card_info();
733 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
736 heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
738 /* Check that the heartbeat value is within it's range ;
739 * if not reset to the default */
740 if (pcipcwd_set_heartbeat(heartbeat)) {
741 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
742 pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
746 ret = register_reboot_notifier(&pcipcwd_notifier);
748 pr_err("cannot register reboot notifier (err=%d)\n", ret);
749 goto err_out_release_region;
752 if (pcipcwd_private.supports_temp) {
753 ret = misc_register(&pcipcwd_temp_miscdev);
755 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
757 goto err_out_unregister_reboot;
761 ret = misc_register(&pcipcwd_miscdev);
763 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
764 WATCHDOG_MINOR, ret);
765 goto err_out_misc_deregister;
768 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
769 heartbeat, nowayout);
773 err_out_misc_deregister:
774 if (pcipcwd_private.supports_temp)
775 misc_deregister(&pcipcwd_temp_miscdev);
776 err_out_unregister_reboot:
777 unregister_reboot_notifier(&pcipcwd_notifier);
778 err_out_release_region:
779 pci_release_regions(pdev);
780 err_out_disable_device:
781 pci_disable_device(pdev);
785 static void pcipcwd_card_exit(struct pci_dev *pdev)
787 /* Stop the timer before we leave */
792 misc_deregister(&pcipcwd_miscdev);
793 if (pcipcwd_private.supports_temp)
794 misc_deregister(&pcipcwd_temp_miscdev);
795 unregister_reboot_notifier(&pcipcwd_notifier);
796 pci_release_regions(pdev);
797 pci_disable_device(pdev);
801 static const struct pci_device_id pcipcwd_pci_tbl[] = {
802 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
803 PCI_ANY_ID, PCI_ANY_ID, },
804 { 0 }, /* End of list */
806 MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
808 static struct pci_driver pcipcwd_driver = {
809 .name = WATCHDOG_NAME,
810 .id_table = pcipcwd_pci_tbl,
811 .probe = pcipcwd_card_init,
812 .remove = pcipcwd_card_exit,
815 module_pci_driver(pcipcwd_driver);
817 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
818 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
819 MODULE_LICENSE("GPL");