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@lxorguk.ukuu.org.uk>,
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
28 * http://www.berkprod.com/ or http://www.pcwatchdog.com/
32 * Includes, defines, variables, module parameters, ...
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37 #include <linux/module.h> /* For module specific items */
38 #include <linux/moduleparam.h> /* For new moduleparam's */
39 #include <linux/types.h> /* For standard types (like size_t) */
40 #include <linux/errno.h> /* For the -ENODEV/... values */
41 #include <linux/kernel.h> /* For printk/panic/... */
42 #include <linux/delay.h> /* For mdelay function */
43 #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
44 #include <linux/watchdog.h> /* For the watchdog specific items */
45 #include <linux/notifier.h> /* For notifier support */
46 #include <linux/reboot.h> /* For reboot_notifier stuff */
47 #include <linux/init.h> /* For __init/__exit/... */
48 #include <linux/fs.h> /* For file operations */
49 #include <linux/pci.h> /* For pci functions */
50 #include <linux/ioport.h> /* For io-port access */
51 #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
52 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
53 #include <linux/io.h> /* For inb/outb/... */
55 /* Module and version information */
56 #define WATCHDOG_VERSION "1.03"
57 #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
58 #define WATCHDOG_NAME "pcwd_pci"
59 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
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 /
82 #define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
83 /* Port 2 : Control Status #2 */
84 #define WD_PCI_WDIS 0x10 /* Watchdog Disable */
85 #define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
86 #define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
87 #define WD_PCI_PCMD 0x80 /* PC has sent command */
89 /* according to documentation max. time to process a command for the pci
90 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
91 #define PCI_COMMAND_TIMEOUT 150
93 /* Watchdog's internal commands */
94 #define CMD_GET_STATUS 0x04
95 #define CMD_GET_FIRMWARE_VERSION 0x08
96 #define CMD_READ_WATCHDOG_TIMEOUT 0x18
97 #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
98 #define CMD_GET_CLEAR_RESET_COUNT 0x84
100 /* Watchdog's Dip Switch heartbeat values */
101 static const int heartbeat_tbl
[] = {
102 5, /* OFF-OFF-OFF = 5 Sec */
103 10, /* OFF-OFF-ON = 10 Sec */
104 30, /* OFF-ON-OFF = 30 Sec */
105 60, /* OFF-ON-ON = 1 Min */
106 300, /* ON-OFF-OFF = 5 Min */
107 600, /* ON-OFF-ON = 10 Min */
108 1800, /* ON-ON-OFF = 30 Min */
109 3600, /* ON-ON-ON = 1 hour */
112 /* We can only use 1 card due to the /dev/watchdog restriction */
113 static int cards_found
;
115 /* internal variables */
116 static int temp_panic
;
117 static unsigned long is_active
;
118 static char expect_release
;
119 /* this is private data for each PCI-PC watchdog card */
121 /* Wether or not the card has a temperature device */
123 /* The card's boot status */
125 /* The cards I/O address */
126 unsigned long io_addr
;
127 /* the lock for io operations */
130 struct pci_dev
*pdev
;
133 /* module parameters */
134 #define QUIET 0 /* Default */
135 #define VERBOSE 1 /* Verbose */
136 #define DEBUG 2 /* print fancy stuff too */
137 static int debug
= QUIET
;
138 module_param(debug
, int, 0);
139 MODULE_PARM_DESC(debug
, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
141 #define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
142 delay-time from dip-switches */
143 static int heartbeat
= WATCHDOG_HEARTBEAT
;
144 module_param(heartbeat
, int, 0);
145 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds. "
146 "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
147 __MODULE_STRING(WATCHDOG_HEARTBEAT
) ")");
149 static bool nowayout
= WATCHDOG_NOWAYOUT
;
150 module_param(nowayout
, bool, 0);
151 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
152 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
158 static int send_command(int cmd
, int *msb
, int *lsb
)
160 int got_response
, count
;
163 pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
166 spin_lock(&pcipcwd_private
.io_lock
);
167 /* If a command requires data it should be written first.
168 * Data for commands with 8 bits of data should be written to port 4.
169 * Commands with 16 bits of data, should be written as LSB to port 4
171 * After the required data has been written then write the command to
173 outb_p(*lsb
, pcipcwd_private
.io_addr
+ 4);
174 outb_p(*msb
, pcipcwd_private
.io_addr
+ 5);
175 outb_p(cmd
, pcipcwd_private
.io_addr
+ 6);
177 /* wait till the pci card processed the command, signaled by
178 * the WRSP bit in port 2 and give it a max. timeout of
179 * PCI_COMMAND_TIMEOUT to process */
180 got_response
= inb_p(pcipcwd_private
.io_addr
+ 2) & WD_PCI_WRSP
;
181 for (count
= 0; (count
< PCI_COMMAND_TIMEOUT
) && (!got_response
);
184 got_response
= inb_p(pcipcwd_private
.io_addr
+ 2) & WD_PCI_WRSP
;
187 if (debug
>= DEBUG
) {
189 pr_debug("time to process command was: %d ms\n",
192 pr_debug("card did not respond on command!\n");
197 /* read back response */
198 *lsb
= inb_p(pcipcwd_private
.io_addr
+ 4);
199 *msb
= inb_p(pcipcwd_private
.io_addr
+ 5);
202 inb_p(pcipcwd_private
.io_addr
+ 6);
205 pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
209 spin_unlock(&pcipcwd_private
.io_lock
);
214 static inline void pcipcwd_check_temperature_support(void)
216 if (inb_p(pcipcwd_private
.io_addr
) != 0xF0)
217 pcipcwd_private
.supports_temp
= 1;
220 static int pcipcwd_get_option_switches(void)
224 option_switches
= inb_p(pcipcwd_private
.io_addr
+ 3);
225 return option_switches
;
228 static void pcipcwd_show_card_info(void)
230 int got_fw_rev
, fw_rev_major
, fw_rev_minor
;
231 char fw_ver_str
[20]; /* The cards firmware version */
234 got_fw_rev
= send_command(CMD_GET_FIRMWARE_VERSION
, &fw_rev_major
,
237 sprintf(fw_ver_str
, "%u.%02u", fw_rev_major
, fw_rev_minor
);
239 sprintf(fw_ver_str
, "<card no answer>");
241 /* Get switch settings */
242 option_switches
= pcipcwd_get_option_switches();
244 pr_info("Found card at port 0x%04x (Firmware: %s) %s temp option\n",
245 (int) pcipcwd_private
.io_addr
, fw_ver_str
,
246 (pcipcwd_private
.supports_temp
? "with" : "without"));
248 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
250 ((option_switches
& 0x10) ? "ON" : "OFF"),
251 ((option_switches
& 0x08) ? "ON" : "OFF"));
253 if (pcipcwd_private
.boot_status
& WDIOF_CARDRESET
)
254 pr_info("Previous reset was caused by the Watchdog card\n");
256 if (pcipcwd_private
.boot_status
& WDIOF_OVERHEAT
)
257 pr_info("Card sensed a CPU Overheat\n");
259 if (pcipcwd_private
.boot_status
== 0)
260 pr_info("No previous trip detected - Cold boot or reset\n");
263 static int pcipcwd_start(void)
267 spin_lock(&pcipcwd_private
.io_lock
);
268 outb_p(0x00, pcipcwd_private
.io_addr
+ 3);
271 stat_reg
= inb_p(pcipcwd_private
.io_addr
+ 2);
272 spin_unlock(&pcipcwd_private
.io_lock
);
274 if (stat_reg
& WD_PCI_WDIS
) {
275 pr_err("Card timer not enabled\n");
279 if (debug
>= VERBOSE
)
280 pr_debug("Watchdog started\n");
285 static int pcipcwd_stop(void)
289 spin_lock(&pcipcwd_private
.io_lock
);
290 outb_p(0xA5, pcipcwd_private
.io_addr
+ 3);
293 outb_p(0xA5, pcipcwd_private
.io_addr
+ 3);
296 stat_reg
= inb_p(pcipcwd_private
.io_addr
+ 2);
297 spin_unlock(&pcipcwd_private
.io_lock
);
299 if (!(stat_reg
& WD_PCI_WDIS
)) {
300 pr_err("Card did not acknowledge disable attempt\n");
304 if (debug
>= VERBOSE
)
305 pr_debug("Watchdog stopped\n");
310 static int pcipcwd_keepalive(void)
312 /* Re-trigger watchdog by writing to port 0 */
313 spin_lock(&pcipcwd_private
.io_lock
);
314 outb_p(0x42, pcipcwd_private
.io_addr
); /* send out any data */
315 spin_unlock(&pcipcwd_private
.io_lock
);
318 pr_debug("Watchdog keepalive signal send\n");
323 static int pcipcwd_set_heartbeat(int t
)
328 if ((t
< 0x0001) || (t
> 0xFFFF))
331 /* Write new heartbeat to watchdog */
332 send_command(CMD_WRITE_WATCHDOG_TIMEOUT
, &t_msb
, &t_lsb
);
335 if (debug
>= VERBOSE
)
336 pr_debug("New heartbeat: %d\n", heartbeat
);
341 static int pcipcwd_get_status(int *status
)
346 control_status
= inb_p(pcipcwd_private
.io_addr
+ 1);
347 if (control_status
& WD_PCI_WTRP
)
348 *status
|= WDIOF_CARDRESET
;
349 if (control_status
& WD_PCI_TTRP
) {
350 *status
|= WDIOF_OVERHEAT
;
352 panic(KBUILD_MODNAME
": Temperature overheat trip!\n");
356 pr_debug("Control Status #1: 0x%02x\n", control_status
);
361 static int pcipcwd_clear_status(void)
367 if (debug
>= VERBOSE
)
368 pr_info("clearing watchdog trip status & LED\n");
370 control_status
= inb_p(pcipcwd_private
.io_addr
+ 1);
372 if (debug
>= DEBUG
) {
373 pr_debug("status was: 0x%02x\n", control_status
);
374 pr_debug("sending: 0x%02x\n",
375 (control_status
& WD_PCI_R2DS
) | WD_PCI_WTRP
);
378 /* clear trip status & LED and keep mode of relay 2 */
379 outb_p((control_status
& WD_PCI_R2DS
) | WD_PCI_WTRP
,
380 pcipcwd_private
.io_addr
+ 1);
382 /* clear reset counter */
384 reset_counter
= 0xff;
385 send_command(CMD_GET_CLEAR_RESET_COUNT
, &msb
, &reset_counter
);
387 if (debug
>= DEBUG
) {
388 pr_debug("reset count was: 0x%02x\n", reset_counter
);
394 static int pcipcwd_get_temperature(int *temperature
)
397 if (!pcipcwd_private
.supports_temp
)
400 spin_lock(&pcipcwd_private
.io_lock
);
401 *temperature
= inb_p(pcipcwd_private
.io_addr
);
402 spin_unlock(&pcipcwd_private
.io_lock
);
405 * Convert celsius to fahrenheit, since this was
406 * the decided 'standard' for this return value.
408 *temperature
= (*temperature
* 9 / 5) + 32;
410 if (debug
>= DEBUG
) {
411 pr_debug("temperature is: %d F\n", *temperature
);
417 static int pcipcwd_get_timeleft(int *time_left
)
422 /* Read the time that's left before rebooting */
423 /* Note: if the board is not yet armed then we will read 0xFFFF */
424 send_command(CMD_READ_WATCHDOG_TIMEOUT
, &msb
, &lsb
);
426 *time_left
= (msb
<< 8) + lsb
;
428 if (debug
>= VERBOSE
)
429 pr_debug("Time left before next reboot: %d\n", *time_left
);
435 * /dev/watchdog handling
438 static ssize_t
pcipcwd_write(struct file
*file
, const char __user
*data
,
439 size_t len
, loff_t
*ppos
)
441 /* See if we got the magic character 'V' and reload the timer */
446 /* note: just in case someone wrote the magic character
447 * five months ago... */
450 /* scan to see whether or not we got the
452 for (i
= 0; i
!= len
; i
++) {
454 if (get_user(c
, data
+ i
))
461 /* someone wrote to us, we should reload the timer */
467 static long pcipcwd_ioctl(struct file
*file
, unsigned int cmd
,
470 void __user
*argp
= (void __user
*)arg
;
471 int __user
*p
= argp
;
472 static const struct watchdog_info ident
= {
473 .options
= WDIOF_OVERHEAT
|
475 WDIOF_KEEPALIVEPING
|
478 .firmware_version
= 1,
479 .identity
= WATCHDOG_DRIVER_NAME
,
483 case WDIOC_GETSUPPORT
:
484 return copy_to_user(argp
, &ident
, sizeof(ident
)) ? -EFAULT
: 0;
486 case WDIOC_GETSTATUS
:
489 pcipcwd_get_status(&status
);
490 return put_user(status
, p
);
493 case WDIOC_GETBOOTSTATUS
:
494 return put_user(pcipcwd_private
.boot_status
, p
);
500 if (pcipcwd_get_temperature(&temperature
))
503 return put_user(temperature
, p
);
506 case WDIOC_SETOPTIONS
:
508 int new_options
, retval
= -EINVAL
;
510 if (get_user(new_options
, p
))
513 if (new_options
& WDIOS_DISABLECARD
) {
519 if (new_options
& WDIOS_ENABLECARD
) {
525 if (new_options
& WDIOS_TEMPPANIC
) {
533 case WDIOC_KEEPALIVE
:
537 case WDIOC_SETTIMEOUT
:
541 if (get_user(new_heartbeat
, p
))
544 if (pcipcwd_set_heartbeat(new_heartbeat
))
551 case WDIOC_GETTIMEOUT
:
552 return put_user(heartbeat
, p
);
554 case WDIOC_GETTIMELEFT
:
558 if (pcipcwd_get_timeleft(&time_left
))
561 return put_user(time_left
, p
);
569 static int pcipcwd_open(struct inode
*inode
, struct file
*file
)
571 /* /dev/watchdog can only be opened once */
572 if (test_and_set_bit(0, &is_active
)) {
573 if (debug
>= VERBOSE
)
574 pr_err("Attempt to open already opened device\n");
581 return nonseekable_open(inode
, file
);
584 static int pcipcwd_release(struct inode
*inode
, struct file
*file
)
587 * Shut off the timer.
589 if (expect_release
== 42) {
592 pr_crit("Unexpected close, not stopping watchdog!\n");
596 clear_bit(0, &is_active
);
601 * /dev/temperature handling
604 static ssize_t
pcipcwd_temp_read(struct file
*file
, char __user
*data
,
605 size_t len
, loff_t
*ppos
)
609 if (pcipcwd_get_temperature(&temperature
))
612 if (copy_to_user(data
, &temperature
, 1))
618 static int pcipcwd_temp_open(struct inode
*inode
, struct file
*file
)
620 if (!pcipcwd_private
.supports_temp
)
623 return nonseekable_open(inode
, file
);
626 static int pcipcwd_temp_release(struct inode
*inode
, struct file
*file
)
635 static int pcipcwd_notify_sys(struct notifier_block
*this, unsigned long code
,
638 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
639 pcipcwd_stop(); /* Turn the WDT off */
648 static const struct file_operations pcipcwd_fops
= {
649 .owner
= THIS_MODULE
,
651 .write
= pcipcwd_write
,
652 .unlocked_ioctl
= pcipcwd_ioctl
,
653 .open
= pcipcwd_open
,
654 .release
= pcipcwd_release
,
657 static struct miscdevice pcipcwd_miscdev
= {
658 .minor
= WATCHDOG_MINOR
,
660 .fops
= &pcipcwd_fops
,
663 static const struct file_operations pcipcwd_temp_fops
= {
664 .owner
= THIS_MODULE
,
666 .read
= pcipcwd_temp_read
,
667 .open
= pcipcwd_temp_open
,
668 .release
= pcipcwd_temp_release
,
671 static struct miscdevice pcipcwd_temp_miscdev
= {
673 .name
= "temperature",
674 .fops
= &pcipcwd_temp_fops
,
677 static struct notifier_block pcipcwd_notifier
= {
678 .notifier_call
= pcipcwd_notify_sys
,
682 * Init & exit routines
685 static int pcipcwd_card_init(struct pci_dev
*pdev
,
686 const struct pci_device_id
*ent
)
691 if (cards_found
== 1)
692 pr_info("%s\n", DRIVER_VERSION
);
694 if (cards_found
> 1) {
695 pr_err("This driver only supports 1 device\n");
699 if (pci_enable_device(pdev
)) {
700 pr_err("Not possible to enable PCI Device\n");
704 if (pci_resource_start(pdev
, 0) == 0x0000) {
705 pr_err("No I/O-Address for card detected\n");
707 goto err_out_disable_device
;
710 spin_lock_init(&pcipcwd_private
.io_lock
);
711 pcipcwd_private
.pdev
= pdev
;
712 pcipcwd_private
.io_addr
= pci_resource_start(pdev
, 0);
714 if (pci_request_regions(pdev
, WATCHDOG_NAME
)) {
715 pr_err("I/O address 0x%04x already in use\n",
716 (int) pcipcwd_private
.io_addr
);
718 goto err_out_disable_device
;
721 /* get the boot_status */
722 pcipcwd_get_status(&pcipcwd_private
.boot_status
);
724 /* clear the "card caused reboot" flag */
725 pcipcwd_clear_status();
730 /* Check whether or not the card supports the temperature device */
731 pcipcwd_check_temperature_support();
733 /* Show info about the card itself */
734 pcipcwd_show_card_info();
736 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
739 heartbeat_tbl
[(pcipcwd_get_option_switches() & 0x07)];
741 /* Check that the heartbeat value is within it's range ;
742 * if not reset to the default */
743 if (pcipcwd_set_heartbeat(heartbeat
)) {
744 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT
);
745 pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
749 ret
= register_reboot_notifier(&pcipcwd_notifier
);
751 pr_err("cannot register reboot notifier (err=%d)\n", ret
);
752 goto err_out_release_region
;
755 if (pcipcwd_private
.supports_temp
) {
756 ret
= misc_register(&pcipcwd_temp_miscdev
);
758 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
760 goto err_out_unregister_reboot
;
764 ret
= misc_register(&pcipcwd_miscdev
);
766 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
767 WATCHDOG_MINOR
, ret
);
768 goto err_out_misc_deregister
;
771 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
772 heartbeat
, nowayout
);
776 err_out_misc_deregister
:
777 if (pcipcwd_private
.supports_temp
)
778 misc_deregister(&pcipcwd_temp_miscdev
);
779 err_out_unregister_reboot
:
780 unregister_reboot_notifier(&pcipcwd_notifier
);
781 err_out_release_region
:
782 pci_release_regions(pdev
);
783 err_out_disable_device
:
784 pci_disable_device(pdev
);
788 static void pcipcwd_card_exit(struct pci_dev
*pdev
)
790 /* Stop the timer before we leave */
795 misc_deregister(&pcipcwd_miscdev
);
796 if (pcipcwd_private
.supports_temp
)
797 misc_deregister(&pcipcwd_temp_miscdev
);
798 unregister_reboot_notifier(&pcipcwd_notifier
);
799 pci_release_regions(pdev
);
800 pci_disable_device(pdev
);
804 static DEFINE_PCI_DEVICE_TABLE(pcipcwd_pci_tbl
) = {
805 { PCI_VENDOR_ID_QUICKLOGIC
, PCI_DEVICE_ID_WATCHDOG_PCIPCWD
,
806 PCI_ANY_ID
, PCI_ANY_ID
, },
807 { 0 }, /* End of list */
809 MODULE_DEVICE_TABLE(pci
, pcipcwd_pci_tbl
);
811 static struct pci_driver pcipcwd_driver
= {
812 .name
= WATCHDOG_NAME
,
813 .id_table
= pcipcwd_pci_tbl
,
814 .probe
= pcipcwd_card_init
,
815 .remove
= pcipcwd_card_exit
,
818 module_pci_driver(pcipcwd_driver
);
820 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
821 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
822 MODULE_LICENSE("GPL");
823 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
824 MODULE_ALIAS_MISCDEV(TEMP_MINOR
);