3 * by Ken Hollis (khollis@bitgate.com)
5 * Permission granted from Simon Machell (73244.1270@compuserve.com)
6 * Written for the Linux Kernel, and GPLed by Ken Hollis
8 * 960107 Added request_region routines, modulized the whole thing.
9 * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added
11 * 960216 Added eof marker on the file, and changed verbose messages.
12 * 960716 Made functional and cosmetic changes to the source for
13 * inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
14 * 960717 Removed read/seek routines, replaced with ioctl. Also, added
15 * check_region command due to Alan's suggestion.
16 * 960821 Made changes to compile in newer 2.0.x kernels. Added
17 * "cold reboot sense" entry.
18 * 960825 Made a few changes to code, deleted some defines and made
19 * typedefs to replace them. Made heartbeat reset only available
20 * via ioctl, and removed the write routine.
21 * 960828 Added new items for PC Watchdog Rev.C card.
22 * 960829 Changed around all of the IOCTLs, added new features,
23 * added watchdog disable/re-enable routines. Added firmware
24 * version reporting. Added read routine for temperature.
25 * Removed some extra defines, added an autodetect Revision
27 * 961006 Revised some documentation, fixed some cosmetic bugs. Made
28 * drivers to panic the system if it's overheating at bootup.
29 * 961118 Changed some verbiage on some of the output, tidied up
30 * code bits, and added compatibility to 2.1.x.
31 * 970912 Enabled board on open and disable on close.
32 * 971107 Took account of recent VFS changes (broke read).
33 * 971210 Disable board on initialisation in case board already ticking.
34 * 971222 Changed open/close for temperature handling
35 * Michael Meskes <meskes@debian.org>.
36 * 980112 Used minor numbers from include/linux/miscdevice.h
37 * 990403 Clear reset status after reading control status register in
38 * pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>]
39 * 990605 Made changes to code to support Firmware 1.22a, added
40 * fairly useless proc entry.
41 * 990610 removed said useless proc code for the merge <alan>
42 * 000403 Removed last traces of proc code. <davej>
43 * 011214 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
44 * Added timeout module option to override default
48 * A bells and whistles driver is available from http://www.pcwd.de/
49 * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
52 #include <linux/module.h>
53 #include <linux/moduleparam.h>
54 #include <linux/types.h>
55 #include <linux/timer.h>
56 #include <linux/jiffies.h>
57 #include <linux/config.h>
58 #include <linux/wait.h>
59 #include <linux/slab.h>
60 #include <linux/ioport.h>
61 #include <linux/delay.h>
63 #include <linux/miscdevice.h>
64 #include <linux/watchdog.h>
65 #include <linux/notifier.h>
66 #include <linux/init.h>
67 #include <linux/spinlock.h>
68 #include <linux/reboot.h>
70 #include <asm/uaccess.h>
73 #define WD_VER "1.16 (06/12/2004)"
77 * It should be noted that PCWD_REVISION_B was removed because A and B
78 * are essentially the same types of card, with the exception that B
79 * has temperature reporting. Since I didn't receive a Rev.B card,
80 * the Rev.B card is not supported. (It's a good thing too, as they
81 * are no longer in production.)
83 #define PCWD_REVISION_A 1
84 #define PCWD_REVISION_C 2
87 * These are the defines that describe the control status bits for the
88 * PC Watchdog card, revision A.
90 #define WD_WDRST 0x01 /* Previously reset state */
91 #define WD_T110 0x02 /* Temperature overheat sense */
92 #define WD_HRTBT 0x04 /* Heartbeat sense */
93 #define WD_RLY2 0x08 /* External relay triggered */
94 #define WD_SRLY2 0x80 /* Software external relay triggered */
97 * These are the defines that describe the control status bits for the
98 * PC Watchdog card, revision C.
100 #define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
101 #define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
102 #define WD_REVC_TTRP 0x04 /* Temperature Trip status */
104 /* max. time we give an ISA watchdog card to process a command */
105 /* 500ms for each 4 bit response (according to spec.) */
106 #define ISA_COMMAND_TIMEOUT 1000
108 /* Watchdog's internal commands */
109 #define CMD_ISA_IDLE 0x00
110 #define CMD_ISA_VERSION_INTEGER 0x01
111 #define CMD_ISA_VERSION_TENTH 0x02
112 #define CMD_ISA_VERSION_HUNDRETH 0x03
113 #define CMD_ISA_VERSION_MINOR 0x04
114 #define CMD_ISA_SWITCH_SETTINGS 0x05
115 #define CMD_ISA_DELAY_TIME_2SECS 0x0A
116 #define CMD_ISA_DELAY_TIME_4SECS 0x0B
117 #define CMD_ISA_DELAY_TIME_8SECS 0x0C
120 * We are using an kernel timer to do the pinging of the watchdog
121 * every ~500ms. We try to set the internal heartbeat of the
125 #define WDT_INTERVAL (HZ/2+1)
127 /* We can only use 1 card due to the /dev/watchdog restriction */
128 static int cards_found
;
130 /* internal variables */
131 static atomic_t open_allowed
= ATOMIC_INIT(1);
132 static char expect_close
;
133 static struct timer_list timer
;
134 static unsigned long next_heartbeat
;
135 static int temp_panic
;
136 static int revision
; /* The card's revision */
137 static int supports_temp
; /* Wether or not the card has a temperature device */
138 static int command_mode
; /* Wether or not the card is in command mode */
139 static int initial_status
; /* The card's boot status */
140 static int current_readport
; /* The cards I/O address */
141 static spinlock_t io_lock
;
143 /* module parameters */
144 #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */
145 static int heartbeat
= WATCHDOG_HEARTBEAT
;
146 module_param(heartbeat
, int, 0);
147 MODULE_PARM_DESC(heartbeat
, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT
) ")");
149 #ifdef CONFIG_WATCHDOG_NOWAYOUT
150 static int nowayout
= 1;
152 static int nowayout
= 0;
155 module_param(nowayout
, int, 0);
156 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
162 static int send_isa_command(int cmd
)
166 int port0
, last_port0
; /* Double read for stabilising */
168 /* The WCMD bit must be 1 and the command is only 4 bits in size */
169 control_status
= (cmd
& 0x0F) | 0x80;
170 outb_p(control_status
, current_readport
+ 2);
171 udelay(ISA_COMMAND_TIMEOUT
);
173 port0
= inb_p(current_readport
);
174 for (i
= 0; i
< 25; ++i
) {
176 port0
= inb_p(current_readport
);
178 if (port0
== last_port0
)
179 break; /* Data is stable */
187 static int set_command_mode(void)
189 int i
, found
=0, count
=0;
191 /* Set the card into command mode */
193 while ((!found
) && (count
< 3)) {
194 i
= send_isa_command(CMD_ISA_IDLE
);
198 else if (i
== 0xF3) {
199 /* Card does not like what we've done to it */
200 outb_p(0x00, current_readport
+ 2);
201 udelay(1200); /* Spec says wait 1ms */
202 outb_p(0x00, current_readport
+ 2);
203 udelay(ISA_COMMAND_TIMEOUT
);
207 spin_unlock(&io_lock
);
208 command_mode
= found
;
213 static void unset_command_mode(void)
215 /* Set the card into normal mode */
217 outb_p(0x00, current_readport
+ 2);
218 udelay(ISA_COMMAND_TIMEOUT
);
219 spin_unlock(&io_lock
);
224 static void pcwd_timer_ping(unsigned long data
)
228 /* If we got a heartbeat pulse within the WDT_INTERVAL
229 * we agree to ping the WDT */
230 if(time_before(jiffies
, next_heartbeat
)) {
231 /* Ping the watchdog */
233 if (revision
== PCWD_REVISION_A
) {
234 /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
235 wdrst_stat
= inb_p(current_readport
);
237 wdrst_stat
|= WD_WDRST
;
239 outb_p(wdrst_stat
, current_readport
+ 1);
241 /* Re-trigger watchdog by writing to port 0 */
242 outb_p(0x00, current_readport
);
245 /* Re-set the timer interval */
246 mod_timer(&timer
, jiffies
+ WDT_INTERVAL
);
248 spin_unlock(&io_lock
);
250 printk(KERN_WARNING PFX
"Heartbeat lost! Will not ping the watchdog\n");
254 static int pcwd_start(void)
258 next_heartbeat
= jiffies
+ (heartbeat
* HZ
);
260 /* Start the timer */
261 mod_timer(&timer
, jiffies
+ WDT_INTERVAL
);
263 /* Enable the port */
264 if (revision
== PCWD_REVISION_C
) {
266 outb_p(0x00, current_readport
+ 3);
267 udelay(ISA_COMMAND_TIMEOUT
);
268 stat_reg
= inb_p(current_readport
+ 2);
269 spin_unlock(&io_lock
);
270 if (stat_reg
& 0x10) {
271 printk(KERN_INFO PFX
"Could not start watchdog\n");
278 static int pcwd_stop(void)
285 /* Disable the board */
286 if (revision
== PCWD_REVISION_C
) {
288 outb_p(0xA5, current_readport
+ 3);
289 udelay(ISA_COMMAND_TIMEOUT
);
290 outb_p(0xA5, current_readport
+ 3);
291 udelay(ISA_COMMAND_TIMEOUT
);
292 stat_reg
= inb_p(current_readport
+ 2);
293 spin_unlock(&io_lock
);
294 if ((stat_reg
& 0x10) == 0) {
295 printk(KERN_INFO PFX
"Could not stop watchdog\n");
302 static int pcwd_keepalive(void)
305 next_heartbeat
= jiffies
+ (heartbeat
* HZ
);
309 static int pcwd_set_heartbeat(int t
)
311 if ((t
< 2) || (t
> 7200)) /* arbitrary upper limit */
318 static int pcwd_get_status(int *status
)
324 if (revision
== PCWD_REVISION_A
)
325 /* Rev A cards return status information from
326 * the base register, which is used for the
327 * temperature in other cards. */
328 card_status
= inb(current_readport
);
330 /* Rev C cards return card status in the base
331 * address + 1 register. And use different bits
332 * to indicate a card initiated reset, and an
333 * over-temperature condition. And the reboot
334 * status can be reset. */
335 card_status
= inb(current_readport
+ 1);
337 spin_unlock(&io_lock
);
339 if (revision
== PCWD_REVISION_A
) {
340 if (card_status
& WD_WDRST
)
341 *status
|= WDIOF_CARDRESET
;
343 if (card_status
& WD_T110
) {
344 *status
|= WDIOF_OVERHEAT
;
346 printk (KERN_INFO PFX
"Temperature overheat trip!\n");
351 if (card_status
& WD_REVC_WTRP
)
352 *status
|= WDIOF_CARDRESET
;
354 if (card_status
& WD_REVC_TTRP
) {
355 *status
|= WDIOF_OVERHEAT
;
357 printk (KERN_INFO PFX
"Temperature overheat trip!\n");
366 static int pcwd_clear_status(void)
368 if (revision
== PCWD_REVISION_C
) {
370 outb_p(0x00, current_readport
+ 1); /* clear reset status */
371 spin_unlock(&io_lock
);
376 static int pcwd_get_temperature(int *temperature
)
378 /* check that port 0 gives temperature info and no command results */
387 * Convert celsius to fahrenheit, since this was
388 * the decided 'standard' for this return value.
391 *temperature
= ((inb(current_readport
)) * 9 / 5) + 32;
392 spin_unlock(&io_lock
);
398 * /dev/watchdog handling
401 static int pcwd_ioctl(struct inode
*inode
, struct file
*file
,
402 unsigned int cmd
, unsigned long arg
)
408 int __user
*argp
= (int __user
*)arg
;
409 static struct watchdog_info ident
= {
410 .options
= WDIOF_OVERHEAT
|
412 WDIOF_KEEPALIVEPING
|
415 .firmware_version
= 1,
423 case WDIOC_GETSUPPORT
:
424 if(copy_to_user(argp
, &ident
, sizeof(ident
)))
428 case WDIOC_GETSTATUS
:
429 pcwd_get_status(&status
);
430 return put_user(status
, argp
);
432 case WDIOC_GETBOOTSTATUS
:
433 return put_user(initial_status
, argp
);
436 if (pcwd_get_temperature(&temperature
))
439 return put_user(temperature
, argp
);
441 case WDIOC_SETOPTIONS
:
442 if (revision
== PCWD_REVISION_C
)
444 if(copy_from_user(&rv
, argp
, sizeof(int)))
447 if (rv
& WDIOS_DISABLECARD
)
452 if (rv
& WDIOS_ENABLECARD
)
457 if (rv
& WDIOS_TEMPPANIC
)
464 case WDIOC_KEEPALIVE
:
468 case WDIOC_SETTIMEOUT
:
469 if (get_user(new_heartbeat
, argp
))
472 if (pcwd_set_heartbeat(new_heartbeat
))
478 case WDIOC_GETTIMEOUT
:
479 return put_user(heartbeat
, argp
);
485 static ssize_t
pcwd_write(struct file
*file
, const char __user
*buf
, size_t len
,
492 /* In case it was set long ago */
495 for (i
= 0; i
!= len
; i
++) {
498 if (get_user(c
, buf
+ i
))
509 static int pcwd_open(struct inode
*inode
, struct file
*file
)
511 if (!atomic_dec_and_test(&open_allowed
) ) {
512 atomic_inc( &open_allowed
);
517 __module_get(THIS_MODULE
);
522 return nonseekable_open(inode
, file
);
525 static int pcwd_close(struct inode
*inode
, struct file
*file
)
527 if (expect_close
== 42) {
530 printk(KERN_CRIT PFX
"Unexpected close, not stopping watchdog!\n");
534 atomic_inc( &open_allowed
);
539 * /dev/temperature handling
542 static ssize_t
pcwd_temp_read(struct file
*file
, char __user
*buf
, size_t count
,
547 if (pcwd_get_temperature(&temperature
))
550 if (copy_to_user(buf
, &temperature
, 1))
556 static int pcwd_temp_open(struct inode
*inode
, struct file
*file
)
561 return nonseekable_open(inode
, file
);
564 static int pcwd_temp_close(struct inode
*inode
, struct file
*file
)
573 static int pcwd_notify_sys(struct notifier_block
*this, unsigned long code
, void *unused
)
575 if (code
==SYS_DOWN
|| code
==SYS_HALT
) {
576 /* Turn the WDT off */
587 static struct file_operations pcwd_fops
= {
588 .owner
= THIS_MODULE
,
593 .release
= pcwd_close
,
596 static struct miscdevice pcwd_miscdev
= {
597 .minor
= WATCHDOG_MINOR
,
602 static struct file_operations pcwd_temp_fops
= {
603 .owner
= THIS_MODULE
,
605 .read
= pcwd_temp_read
,
606 .open
= pcwd_temp_open
,
607 .release
= pcwd_temp_close
,
610 static struct miscdevice temp_miscdev
= {
612 .name
= "temperature",
613 .fops
= &pcwd_temp_fops
,
616 static struct notifier_block pcwd_notifier
= {
617 .notifier_call
= pcwd_notify_sys
,
621 * Init & exit routines
624 static inline void get_support(void)
626 if (inb(current_readport
) != 0xF0)
630 static inline int get_revision(void)
632 int r
= PCWD_REVISION_C
;
635 /* REV A cards use only 2 io ports; test
636 * presumes a floating bus reads as 0xff. */
637 if ((inb(current_readport
+ 2) == 0xFF) ||
638 (inb(current_readport
+ 3) == 0xFF))
640 spin_unlock(&io_lock
);
645 static inline char *get_firmware(void)
647 int one
, ten
, hund
, minor
;
650 ret
= kmalloc(6, GFP_KERNEL
);
654 if (set_command_mode()) {
655 one
= send_isa_command(CMD_ISA_VERSION_INTEGER
);
656 ten
= send_isa_command(CMD_ISA_VERSION_TENTH
);
657 hund
= send_isa_command(CMD_ISA_VERSION_HUNDRETH
);
658 minor
= send_isa_command(CMD_ISA_VERSION_MINOR
);
659 sprintf(ret
, "%c.%c%c%c", one
, ten
, hund
, minor
);
662 sprintf(ret
, "ERROR");
664 unset_command_mode();
668 static inline int get_option_switches(void)
672 if (set_command_mode()) {
673 /* Get switch settings */
674 rv
= send_isa_command(CMD_ISA_SWITCH_SETTINGS
);
677 unset_command_mode();
681 static int __devinit
pcwatchdog_init(int base_addr
)
688 if (cards_found
== 1)
689 printk(KERN_INFO PFX
"v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER
);
691 if (cards_found
> 1) {
692 printk(KERN_ERR PFX
"This driver only supports 1 device\n");
696 if (base_addr
== 0x0000) {
697 printk(KERN_ERR PFX
"No I/O-Address for card detected\n");
700 current_readport
= base_addr
;
702 /* Check card's revision */
703 revision
= get_revision();
705 if (!request_region(current_readport
, (revision
== PCWD_REVISION_A
) ? 2 : 4, "PCWD")) {
706 printk(KERN_ERR PFX
"I/O address 0x%04x already in use\n",
708 current_readport
= 0x0000;
712 /* Initial variables */
715 initial_status
= 0x0000;
717 /* get the boot_status */
718 pcwd_get_status(&initial_status
);
720 /* clear the "card caused reboot" flag */
724 timer
.function
= pcwd_timer_ping
;
727 /* Disable the board */
730 /* Check whether or not the card supports the temperature device */
733 /* Get some extra info from the hardware (in command/debug/diag mode) */
734 if (revision
== PCWD_REVISION_A
)
735 printk(KERN_INFO PFX
"ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", current_readport
);
736 else if (revision
== PCWD_REVISION_C
) {
737 firmware
= get_firmware();
738 printk(KERN_INFO PFX
"ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
739 current_readport
, firmware
);
741 option_switches
= get_option_switches();
742 printk(KERN_INFO PFX
"Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
744 ((option_switches
& 0x10) ? "ON" : "OFF"),
745 ((option_switches
& 0x08) ? "ON" : "OFF"));
747 /* Reprogram internal heartbeat to 2 seconds */
748 if (set_command_mode()) {
749 send_isa_command(CMD_ISA_DELAY_TIME_2SECS
);
750 unset_command_mode();
753 /* Should NEVER happen, unless get_revision() fails. */
754 printk(KERN_INFO PFX
"Unable to get revision\n");
755 release_region(current_readport
, (revision
== PCWD_REVISION_A
) ? 2 : 4);
756 current_readport
= 0x0000;
761 printk(KERN_INFO PFX
"Temperature Option Detected\n");
763 if (initial_status
& WDIOF_CARDRESET
)
764 printk(KERN_INFO PFX
"Previous reboot was caused by the card\n");
766 if (initial_status
& WDIOF_OVERHEAT
) {
767 printk(KERN_EMERG PFX
"Card senses a CPU Overheat. Panicking!\n");
768 printk(KERN_EMERG PFX
"CPU Overheat\n");
771 if (initial_status
== 0)
772 printk(KERN_INFO PFX
"No previous trip detected - Cold boot or reset\n");
774 /* Check that the heartbeat value is within it's range ; if not reset to the default */
775 if (pcwd_set_heartbeat(heartbeat
)) {
776 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT
);
777 printk(KERN_INFO PFX
"heartbeat value must be 2<=heartbeat<=7200, using %d\n",
781 ret
= register_reboot_notifier(&pcwd_notifier
);
783 printk(KERN_ERR PFX
"cannot register reboot notifier (err=%d)\n",
785 release_region(current_readport
, (revision
== PCWD_REVISION_A
) ? 2 : 4);
786 current_readport
= 0x0000;
791 ret
= misc_register(&temp_miscdev
);
793 printk(KERN_ERR PFX
"cannot register miscdev on minor=%d (err=%d)\n",
795 unregister_reboot_notifier(&pcwd_notifier
);
796 release_region(current_readport
, (revision
== PCWD_REVISION_A
) ? 2 : 4);
797 current_readport
= 0x0000;
802 ret
= misc_register(&pcwd_miscdev
);
804 printk(KERN_ERR PFX
"cannot register miscdev on minor=%d (err=%d)\n",
805 WATCHDOG_MINOR
, ret
);
807 misc_deregister(&temp_miscdev
);
808 unregister_reboot_notifier(&pcwd_notifier
);
809 release_region(current_readport
, (revision
== PCWD_REVISION_A
) ? 2 : 4);
810 current_readport
= 0x0000;
814 printk(KERN_INFO PFX
"initialized. heartbeat=%d sec (nowayout=%d)\n",
815 heartbeat
, nowayout
);
820 static void __devexit
pcwatchdog_exit(void)
822 /* Disable the board */
827 misc_deregister(&pcwd_miscdev
);
829 misc_deregister(&temp_miscdev
);
830 unregister_reboot_notifier(&pcwd_notifier
);
831 release_region(current_readport
, (revision
== PCWD_REVISION_A
) ? 2 : 4);
832 current_readport
= 0x0000;
836 * The ISA cards have a heartbeat bit in one of the registers, which
837 * register is card dependent. The heartbeat bit is monitored, and if
838 * found, is considered proof that a Berkshire card has been found.
839 * The initial rate is once per second at board start up, then twice
840 * per second for normal operation.
842 static int __init
pcwd_checkcard(int base_addr
)
844 int port0
, last_port0
; /* Reg 0, in case it's REV A */
845 int port1
, last_port1
; /* Register 1 for REV C cards */
849 if (!request_region (base_addr
, 4, "PCWD")) {
850 printk (KERN_INFO PFX
"Port 0x%04x unavailable\n", base_addr
);
856 port0
= inb_p(base_addr
); /* For REV A boards */
857 port1
= inb_p(base_addr
+ 1); /* For REV C boards */
858 if (port0
!= 0xff || port1
!= 0xff) {
859 /* Not an 'ff' from a floating bus, so must be a card! */
860 for (i
= 0; i
< 4; ++i
) {
867 port0
= inb_p(base_addr
);
868 port1
= inb_p(base_addr
+ 1);
870 /* Has either hearbeat bit changed? */
871 if ((port0
^ last_port0
) & WD_HRTBT
||
872 (port1
^ last_port1
) & WD_REVC_HRBT
) {
878 release_region (base_addr
, 4);
884 * These are the auto-probe addresses available.
886 * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
887 * Revision A has an address range of 2 addresses, while Revision C has 4.
889 static int pcwd_ioports
[] = { 0x270, 0x350, 0x370, 0x000 };
891 static int __init
pcwd_init_module(void)
895 spin_lock_init(&io_lock
);
897 for (i
= 0; pcwd_ioports
[i
] != 0; i
++) {
898 if (pcwd_checkcard(pcwd_ioports
[i
])) {
899 if (!(pcwatchdog_init(pcwd_ioports
[i
])))
905 printk (KERN_INFO PFX
"No card detected, or port not available\n");
912 static void __exit
pcwd_cleanup_module(void)
914 if (current_readport
)
919 module_init(pcwd_init_module
);
920 module_exit(pcwd_cleanup_module
);
922 MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
923 MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
924 MODULE_LICENSE("GPL");
925 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
926 MODULE_ALIAS_MISCDEV(TEMP_MINOR
);