Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PC Watchdog Driver |
| 3 | * by Ken Hollis (khollis@bitgate.com) |
| 4 | * |
Wim Van Sebroeck | f9146f2 | 2007-01-09 22:38:54 +0100 | [diff] [blame] | 5 | * Permission granted from Simon Machell (smachell@berkprod.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Written for the Linux Kernel, and GPLed by Ken Hollis |
| 7 | * |
| 8 | * 960107 Added request_region routines, modulized the whole thing. |
| 9 | * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added |
| 10 | * WD_TIMEOUT define. |
| 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 |
| 26 | * routine. |
| 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 |
| 45 | */ |
| 46 | |
| 47 | /* |
| 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/ |
| 50 | */ |
| 51 | |
Wim Van Sebroeck | fd41fa6 | 2005-12-10 14:22:37 +0100 | [diff] [blame] | 52 | #include <linux/module.h> /* For module specific items */ |
| 53 | #include <linux/moduleparam.h> /* For new moduleparam's */ |
| 54 | #include <linux/types.h> /* For standard types (like size_t) */ |
| 55 | #include <linux/errno.h> /* For the -ENODEV/... values */ |
| 56 | #include <linux/kernel.h> /* For printk/panic/... */ |
| 57 | #include <linux/delay.h> /* For mdelay function */ |
| 58 | #include <linux/timer.h> /* For timer related operations */ |
| 59 | #include <linux/jiffies.h> /* For jiffies stuff */ |
| 60 | #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */ |
| 61 | #include <linux/watchdog.h> /* For the watchdog specific items */ |
| 62 | #include <linux/notifier.h> /* For notifier support */ |
| 63 | #include <linux/reboot.h> /* For reboot_notifier stuff */ |
| 64 | #include <linux/init.h> /* For __init/__exit/... */ |
| 65 | #include <linux/fs.h> /* For file operations */ |
| 66 | #include <linux/ioport.h> /* For io-port access */ |
| 67 | #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Wim Van Sebroeck | fd41fa6 | 2005-12-10 14:22:37 +0100 | [diff] [blame] | 69 | #include <asm/uaccess.h> /* For copy_to_user/put_user/... */ |
| 70 | #include <asm/io.h> /* For inb/outb/... */ |
| 71 | |
| 72 | /* Module and version information */ |
Wim Van Sebroeck | f3dc073 | 2007-01-09 22:43:49 +0100 | [diff] [blame^] | 73 | #define WATCHDOG_VERSION "1.18" |
| 74 | #define WATCHDOG_DATE "06 Jan 2007" |
Wim Van Sebroeck | a7122f9 | 2006-01-09 22:07:22 +0100 | [diff] [blame] | 75 | #define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog" |
| 76 | #define WATCHDOG_NAME "pcwd" |
| 77 | #define PFX WATCHDOG_NAME ": " |
| 78 | #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n" |
| 79 | #define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * It should be noted that PCWD_REVISION_B was removed because A and B |
| 83 | * are essentially the same types of card, with the exception that B |
| 84 | * has temperature reporting. Since I didn't receive a Rev.B card, |
| 85 | * the Rev.B card is not supported. (It's a good thing too, as they |
| 86 | * are no longer in production.) |
| 87 | */ |
| 88 | #define PCWD_REVISION_A 1 |
| 89 | #define PCWD_REVISION_C 2 |
| 90 | |
| 91 | /* |
Wim Van Sebroeck | 8f0235d | 2006-01-09 21:56:09 +0100 | [diff] [blame] | 92 | * These are the defines that describe the control status bits for the |
| 93 | * PCI-PC Watchdog card. |
| 94 | */ |
| 95 | /* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */ |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 96 | #define WD_WDRST 0x01 /* Previously reset state */ |
| 97 | #define WD_T110 0x02 /* Temperature overheat sense */ |
| 98 | #define WD_HRTBT 0x04 /* Heartbeat sense */ |
| 99 | #define WD_RLY2 0x08 /* External relay triggered */ |
| 100 | #define WD_SRLY2 0x80 /* Software external relay triggered */ |
Wim Van Sebroeck | 8f0235d | 2006-01-09 21:56:09 +0100 | [diff] [blame] | 101 | /* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */ |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 102 | #define WD_REVC_WTRP 0x01 /* Watchdog Trip status */ |
| 103 | #define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */ |
| 104 | #define WD_REVC_TTRP 0x04 /* Temperature Trip status */ |
| 105 | #define WD_REVC_RL2A 0x08 /* Relay 2 activated by on-board processor */ |
| 106 | #define WD_REVC_RL1A 0x10 /* Relay 1 active */ |
| 107 | #define WD_REVC_R2DS 0x40 /* Relay 2 disable */ |
| 108 | #define WD_REVC_RLY2 0x80 /* Relay 2 activated? */ |
Wim Van Sebroeck | 8f0235d | 2006-01-09 21:56:09 +0100 | [diff] [blame] | 109 | /* Port 2 : Control Status #2 */ |
| 110 | #define WD_WDIS 0x10 /* Watchdog Disabled */ |
| 111 | #define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */ |
| 112 | #define WD_SSEL 0x40 /* Watchdog Switch Select (1:SW1 <-> 0:SW2) */ |
| 113 | #define WD_WCMD 0x80 /* Watchdog Command Mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | /* max. time we give an ISA watchdog card to process a command */ |
| 116 | /* 500ms for each 4 bit response (according to spec.) */ |
| 117 | #define ISA_COMMAND_TIMEOUT 1000 |
| 118 | |
| 119 | /* Watchdog's internal commands */ |
Wim Van Sebroeck | fd41fa6 | 2005-12-10 14:22:37 +0100 | [diff] [blame] | 120 | #define CMD_ISA_IDLE 0x00 |
| 121 | #define CMD_ISA_VERSION_INTEGER 0x01 |
| 122 | #define CMD_ISA_VERSION_TENTH 0x02 |
| 123 | #define CMD_ISA_VERSION_HUNDRETH 0x03 |
| 124 | #define CMD_ISA_VERSION_MINOR 0x04 |
| 125 | #define CMD_ISA_SWITCH_SETTINGS 0x05 |
Wim Van Sebroeck | 369fa25 | 2006-02-12 17:44:57 +0100 | [diff] [blame] | 126 | #define CMD_ISA_RESET_PC 0x06 |
| 127 | #define CMD_ISA_ARM_0 0x07 |
| 128 | #define CMD_ISA_ARM_30 0x08 |
| 129 | #define CMD_ISA_ARM_60 0x09 |
Wim Van Sebroeck | fd41fa6 | 2005-12-10 14:22:37 +0100 | [diff] [blame] | 130 | #define CMD_ISA_DELAY_TIME_2SECS 0x0A |
| 131 | #define CMD_ISA_DELAY_TIME_4SECS 0x0B |
| 132 | #define CMD_ISA_DELAY_TIME_8SECS 0x0C |
Wim Van Sebroeck | 369fa25 | 2006-02-12 17:44:57 +0100 | [diff] [blame] | 133 | #define CMD_ISA_RESET_RELAYS 0x0D |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Wim Van Sebroeck | f3dc073 | 2007-01-09 22:43:49 +0100 | [diff] [blame^] | 135 | /* Watchdog's Dip Switch heartbeat values */ |
| 136 | static const int heartbeat_tbl [] = { |
| 137 | 20, /* OFF-OFF-OFF = 20 Sec */ |
| 138 | 40, /* OFF-OFF-ON = 40 Sec */ |
| 139 | 60, /* OFF-ON-OFF = 1 Min */ |
| 140 | 300, /* OFF-ON-ON = 5 Min */ |
| 141 | 600, /* ON-OFF-OFF = 10 Min */ |
| 142 | 1800, /* ON-OFF-ON = 30 Min */ |
| 143 | 3600, /* ON-ON-OFF = 1 Hour */ |
| 144 | 7200, /* ON-ON-ON = 2 hour */ |
| 145 | }; |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* |
| 148 | * We are using an kernel timer to do the pinging of the watchdog |
| 149 | * every ~500ms. We try to set the internal heartbeat of the |
| 150 | * watchdog to 2 ms. |
| 151 | */ |
| 152 | |
| 153 | #define WDT_INTERVAL (HZ/2+1) |
| 154 | |
| 155 | /* We can only use 1 card due to the /dev/watchdog restriction */ |
| 156 | static int cards_found; |
| 157 | |
| 158 | /* internal variables */ |
| 159 | static atomic_t open_allowed = ATOMIC_INIT(1); |
| 160 | static char expect_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | static int temp_panic; |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 162 | static struct { /* this is private data for each ISA-PC watchdog card */ |
Wim Van Sebroeck | 2891b6a | 2006-02-12 16:47:34 +0100 | [diff] [blame] | 163 | char fw_ver_str[6]; /* The cards firmware version */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 164 | int revision; /* The card's revision */ |
| 165 | int supports_temp; /* Wether or not the card has a temperature device */ |
| 166 | int command_mode; /* Wether or not the card is in command mode */ |
| 167 | int boot_status; /* The card's boot status */ |
| 168 | int io_addr; /* The cards I/O address */ |
| 169 | spinlock_t io_lock; /* the lock for io operations */ |
| 170 | struct timer_list timer; /* The timer that pings the watchdog */ |
| 171 | unsigned long next_heartbeat; /* the next_heartbeat for the timer */ |
| 172 | } pcwd_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | /* module parameters */ |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 175 | #define QUIET 0 /* Default */ |
| 176 | #define VERBOSE 1 /* Verbose */ |
| 177 | #define DEBUG 2 /* print fancy stuff too */ |
| 178 | static int debug = QUIET; |
| 179 | module_param(debug, int, 0); |
| 180 | MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)"); |
| 181 | |
Wim Van Sebroeck | f3dc073 | 2007-01-09 22:43:49 +0100 | [diff] [blame^] | 182 | #define WATCHDOG_HEARTBEAT 0 /* default heartbeat = delay-time from dip-switches */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | static int heartbeat = WATCHDOG_HEARTBEAT; |
| 184 | module_param(heartbeat, int, 0); |
Wim Van Sebroeck | f3dc073 | 2007-01-09 22:43:49 +0100 | [diff] [blame^] | 185 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 187 | static int nowayout = WATCHDOG_NOWAYOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | module_param(nowayout, int, 0); |
| 189 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)"); |
| 190 | |
| 191 | /* |
| 192 | * Internal functions |
| 193 | */ |
| 194 | |
| 195 | static int send_isa_command(int cmd) |
| 196 | { |
| 197 | int i; |
| 198 | int control_status; |
| 199 | int port0, last_port0; /* Double read for stabilising */ |
| 200 | |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 201 | if (debug >= DEBUG) |
| 202 | printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n", |
| 203 | cmd); |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* The WCMD bit must be 1 and the command is only 4 bits in size */ |
Wim Van Sebroeck | 8f0235d | 2006-01-09 21:56:09 +0100 | [diff] [blame] | 206 | control_status = (cmd & 0x0F) | WD_WCMD; |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 207 | outb_p(control_status, pcwd_private.io_addr + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | udelay(ISA_COMMAND_TIMEOUT); |
| 209 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 210 | port0 = inb_p(pcwd_private.io_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | for (i = 0; i < 25; ++i) { |
| 212 | last_port0 = port0; |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 213 | port0 = inb_p(pcwd_private.io_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | if (port0 == last_port0) |
| 216 | break; /* Data is stable */ |
| 217 | |
| 218 | udelay (250); |
| 219 | } |
| 220 | |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 221 | if (debug >= DEBUG) |
| 222 | printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n", |
| 223 | cmd, port0, last_port0); |
| 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return port0; |
| 226 | } |
| 227 | |
| 228 | static int set_command_mode(void) |
| 229 | { |
| 230 | int i, found=0, count=0; |
| 231 | |
| 232 | /* Set the card into command mode */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 233 | spin_lock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | while ((!found) && (count < 3)) { |
| 235 | i = send_isa_command(CMD_ISA_IDLE); |
| 236 | |
| 237 | if (i == 0x00) |
| 238 | found = 1; |
| 239 | else if (i == 0xF3) { |
| 240 | /* Card does not like what we've done to it */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 241 | outb_p(0x00, pcwd_private.io_addr + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | udelay(1200); /* Spec says wait 1ms */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 243 | outb_p(0x00, pcwd_private.io_addr + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | udelay(ISA_COMMAND_TIMEOUT); |
| 245 | } |
| 246 | count++; |
| 247 | } |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 248 | spin_unlock(&pcwd_private.io_lock); |
| 249 | pcwd_private.command_mode = found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 251 | if (debug >= DEBUG) |
| 252 | printk(KERN_DEBUG PFX "command_mode=%d\n", |
| 253 | pcwd_private.command_mode); |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | return(found); |
| 256 | } |
| 257 | |
| 258 | static void unset_command_mode(void) |
| 259 | { |
| 260 | /* Set the card into normal mode */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 261 | spin_lock(&pcwd_private.io_lock); |
| 262 | outb_p(0x00, pcwd_private.io_addr + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | udelay(ISA_COMMAND_TIMEOUT); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 264 | spin_unlock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 266 | pcwd_private.command_mode = 0; |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 267 | |
| 268 | if (debug >= DEBUG) |
| 269 | printk(KERN_DEBUG PFX "command_mode=%d\n", |
| 270 | pcwd_private.command_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Wim Van Sebroeck | 8587521 | 2006-01-09 21:59:39 +0100 | [diff] [blame] | 273 | static inline void pcwd_check_temperature_support(void) |
| 274 | { |
| 275 | if (inb(pcwd_private.io_addr) != 0xF0) |
| 276 | pcwd_private.supports_temp = 1; |
| 277 | } |
| 278 | |
Wim Van Sebroeck | 2891b6a | 2006-02-12 16:47:34 +0100 | [diff] [blame] | 279 | static inline void pcwd_get_firmware(void) |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 280 | { |
| 281 | int one, ten, hund, minor; |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 282 | |
Wim Van Sebroeck | 6bbc20b | 2006-03-02 20:05:16 +0100 | [diff] [blame] | 283 | strcpy(pcwd_private.fw_ver_str, "ERROR"); |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 284 | |
| 285 | if (set_command_mode()) { |
| 286 | one = send_isa_command(CMD_ISA_VERSION_INTEGER); |
| 287 | ten = send_isa_command(CMD_ISA_VERSION_TENTH); |
| 288 | hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH); |
| 289 | minor = send_isa_command(CMD_ISA_VERSION_MINOR); |
Wim Van Sebroeck | 2891b6a | 2006-02-12 16:47:34 +0100 | [diff] [blame] | 290 | sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c", one, ten, hund, minor); |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 291 | } |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 292 | unset_command_mode(); |
Wim Van Sebroeck | 2891b6a | 2006-02-12 16:47:34 +0100 | [diff] [blame] | 293 | |
| 294 | return; |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static inline int pcwd_get_option_switches(void) |
| 298 | { |
| 299 | int option_switches=0; |
| 300 | |
| 301 | if (set_command_mode()) { |
| 302 | /* Get switch settings */ |
| 303 | option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS); |
| 304 | } |
| 305 | |
| 306 | unset_command_mode(); |
| 307 | return(option_switches); |
| 308 | } |
| 309 | |
| 310 | static void pcwd_show_card_info(void) |
| 311 | { |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 312 | int option_switches; |
| 313 | |
| 314 | /* Get some extra info from the hardware (in command/debug/diag mode) */ |
| 315 | if (pcwd_private.revision == PCWD_REVISION_A) |
| 316 | printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr); |
| 317 | else if (pcwd_private.revision == PCWD_REVISION_C) { |
Wim Van Sebroeck | 2891b6a | 2006-02-12 16:47:34 +0100 | [diff] [blame] | 318 | pcwd_get_firmware(); |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 319 | printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n", |
Wim Van Sebroeck | 2891b6a | 2006-02-12 16:47:34 +0100 | [diff] [blame] | 320 | pcwd_private.io_addr, pcwd_private.fw_ver_str); |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 321 | option_switches = pcwd_get_option_switches(); |
| 322 | printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n", |
| 323 | option_switches, |
| 324 | ((option_switches & 0x10) ? "ON" : "OFF"), |
| 325 | ((option_switches & 0x08) ? "ON" : "OFF")); |
| 326 | |
| 327 | /* Reprogram internal heartbeat to 2 seconds */ |
| 328 | if (set_command_mode()) { |
| 329 | send_isa_command(CMD_ISA_DELAY_TIME_2SECS); |
| 330 | unset_command_mode(); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (pcwd_private.supports_temp) |
| 335 | printk(KERN_INFO PFX "Temperature Option Detected\n"); |
| 336 | |
| 337 | if (pcwd_private.boot_status & WDIOF_CARDRESET) |
| 338 | printk(KERN_INFO PFX "Previous reboot was caused by the card\n"); |
| 339 | |
| 340 | if (pcwd_private.boot_status & WDIOF_OVERHEAT) { |
| 341 | printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n"); |
| 342 | printk(KERN_EMERG PFX "CPU Overheat\n"); |
| 343 | } |
| 344 | |
| 345 | if (pcwd_private.boot_status == 0) |
| 346 | printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n"); |
| 347 | } |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | static void pcwd_timer_ping(unsigned long data) |
| 350 | { |
| 351 | int wdrst_stat; |
| 352 | |
| 353 | /* If we got a heartbeat pulse within the WDT_INTERVAL |
| 354 | * we agree to ping the WDT */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 355 | if(time_before(jiffies, pcwd_private.next_heartbeat)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /* Ping the watchdog */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 357 | spin_lock(&pcwd_private.io_lock); |
| 358 | if (pcwd_private.revision == PCWD_REVISION_A) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 360 | wdrst_stat = inb_p(pcwd_private.io_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | wdrst_stat &= 0x0F; |
| 362 | wdrst_stat |= WD_WDRST; |
| 363 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 364 | outb_p(wdrst_stat, pcwd_private.io_addr + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } else { |
| 366 | /* Re-trigger watchdog by writing to port 0 */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 367 | outb_p(0x00, pcwd_private.io_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Re-set the timer interval */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 371 | mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 373 | spin_unlock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } else { |
| 375 | printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n"); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | static int pcwd_start(void) |
| 380 | { |
| 381 | int stat_reg; |
| 382 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 383 | pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | /* Start the timer */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 386 | mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | /* Enable the port */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 389 | if (pcwd_private.revision == PCWD_REVISION_C) { |
| 390 | spin_lock(&pcwd_private.io_lock); |
| 391 | outb_p(0x00, pcwd_private.io_addr + 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | udelay(ISA_COMMAND_TIMEOUT); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 393 | stat_reg = inb_p(pcwd_private.io_addr + 2); |
| 394 | spin_unlock(&pcwd_private.io_lock); |
Wim Van Sebroeck | 8f0235d | 2006-01-09 21:56:09 +0100 | [diff] [blame] | 395 | if (stat_reg & WD_WDIS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | printk(KERN_INFO PFX "Could not start watchdog\n"); |
| 397 | return -EIO; |
| 398 | } |
| 399 | } |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 400 | |
| 401 | if (debug >= VERBOSE) |
| 402 | printk(KERN_DEBUG PFX "Watchdog started\n"); |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int pcwd_stop(void) |
| 408 | { |
| 409 | int stat_reg; |
| 410 | |
| 411 | /* Stop the timer */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 412 | del_timer(&pcwd_private.timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | /* Disable the board */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 415 | if (pcwd_private.revision == PCWD_REVISION_C) { |
| 416 | spin_lock(&pcwd_private.io_lock); |
| 417 | outb_p(0xA5, pcwd_private.io_addr + 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | udelay(ISA_COMMAND_TIMEOUT); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 419 | outb_p(0xA5, pcwd_private.io_addr + 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | udelay(ISA_COMMAND_TIMEOUT); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 421 | stat_reg = inb_p(pcwd_private.io_addr + 2); |
| 422 | spin_unlock(&pcwd_private.io_lock); |
Wim Van Sebroeck | 8f0235d | 2006-01-09 21:56:09 +0100 | [diff] [blame] | 423 | if ((stat_reg & WD_WDIS) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | printk(KERN_INFO PFX "Could not stop watchdog\n"); |
| 425 | return -EIO; |
| 426 | } |
| 427 | } |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 428 | |
| 429 | if (debug >= VERBOSE) |
| 430 | printk(KERN_DEBUG PFX "Watchdog stopped\n"); |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int pcwd_keepalive(void) |
| 436 | { |
| 437 | /* user land ping */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 438 | pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ); |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 439 | |
| 440 | if (debug >= DEBUG) |
| 441 | printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n"); |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int pcwd_set_heartbeat(int t) |
| 447 | { |
| 448 | if ((t < 2) || (t > 7200)) /* arbitrary upper limit */ |
| 449 | return -EINVAL; |
| 450 | |
| 451 | heartbeat = t; |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 452 | |
| 453 | if (debug >= VERBOSE) |
| 454 | printk(KERN_DEBUG PFX "New heartbeat: %d\n", |
| 455 | heartbeat); |
| 456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int pcwd_get_status(int *status) |
| 461 | { |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 462 | int control_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
| 464 | *status=0; |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 465 | spin_lock(&pcwd_private.io_lock); |
| 466 | if (pcwd_private.revision == PCWD_REVISION_A) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | /* Rev A cards return status information from |
| 468 | * the base register, which is used for the |
| 469 | * temperature in other cards. */ |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 470 | control_status = inb(pcwd_private.io_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | else { |
| 472 | /* Rev C cards return card status in the base |
| 473 | * address + 1 register. And use different bits |
| 474 | * to indicate a card initiated reset, and an |
| 475 | * over-temperature condition. And the reboot |
| 476 | * status can be reset. */ |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 477 | control_status = inb(pcwd_private.io_addr + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 479 | spin_unlock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 481 | if (pcwd_private.revision == PCWD_REVISION_A) { |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 482 | if (control_status & WD_WDRST) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | *status |= WDIOF_CARDRESET; |
| 484 | |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 485 | if (control_status & WD_T110) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | *status |= WDIOF_OVERHEAT; |
| 487 | if (temp_panic) { |
| 488 | printk (KERN_INFO PFX "Temperature overheat trip!\n"); |
Eric W. Biederman | 68acc05 | 2005-07-26 12:03:08 -0600 | [diff] [blame] | 489 | kernel_power_off(); |
Wim Van Sebroeck | 369fa25 | 2006-02-12 17:44:57 +0100 | [diff] [blame] | 490 | /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | } else { |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 494 | if (control_status & WD_REVC_WTRP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | *status |= WDIOF_CARDRESET; |
| 496 | |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 497 | if (control_status & WD_REVC_TTRP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | *status |= WDIOF_OVERHEAT; |
| 499 | if (temp_panic) { |
| 500 | printk (KERN_INFO PFX "Temperature overheat trip!\n"); |
Eric W. Biederman | 68acc05 | 2005-07-26 12:03:08 -0600 | [diff] [blame] | 501 | kernel_power_off(); |
Wim Van Sebroeck | 369fa25 | 2006-02-12 17:44:57 +0100 | [diff] [blame] | 502 | /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int pcwd_clear_status(void) |
| 511 | { |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 512 | int control_status; |
| 513 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 514 | if (pcwd_private.revision == PCWD_REVISION_C) { |
| 515 | spin_lock(&pcwd_private.io_lock); |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 516 | |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 517 | if (debug >= VERBOSE) |
| 518 | printk(KERN_INFO PFX "clearing watchdog trip status\n"); |
| 519 | |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 520 | control_status = inb_p(pcwd_private.io_addr + 1); |
| 521 | |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 522 | if (debug >= DEBUG) { |
| 523 | printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status); |
| 524 | printk(KERN_DEBUG PFX "sending: 0x%02x\n", |
| 525 | (control_status & WD_REVC_R2DS)); |
| 526 | } |
| 527 | |
Wim Van Sebroeck | 4206f0c | 2006-02-12 16:37:36 +0100 | [diff] [blame] | 528 | /* clear reset status & Keep Relay 2 disable state as it is */ |
| 529 | outb_p((control_status & WD_REVC_R2DS), pcwd_private.io_addr + 1); |
| 530 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 531 | spin_unlock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static int pcwd_get_temperature(int *temperature) |
| 537 | { |
| 538 | /* check that port 0 gives temperature info and no command results */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 539 | if (pcwd_private.command_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return -1; |
| 541 | |
| 542 | *temperature = 0; |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 543 | if (!pcwd_private.supports_temp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | return -ENODEV; |
| 545 | |
| 546 | /* |
| 547 | * Convert celsius to fahrenheit, since this was |
| 548 | * the decided 'standard' for this return value. |
| 549 | */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 550 | spin_lock(&pcwd_private.io_lock); |
| 551 | *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32; |
| 552 | spin_unlock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 554 | if (debug >= DEBUG) { |
| 555 | printk(KERN_DEBUG PFX "temperature is: %d F\n", |
| 556 | *temperature); |
| 557 | } |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * /dev/watchdog handling |
| 564 | */ |
| 565 | |
| 566 | static int pcwd_ioctl(struct inode *inode, struct file *file, |
| 567 | unsigned int cmd, unsigned long arg) |
| 568 | { |
| 569 | int rv; |
| 570 | int status; |
| 571 | int temperature; |
| 572 | int new_heartbeat; |
| 573 | int __user *argp = (int __user *)arg; |
| 574 | static struct watchdog_info ident = { |
| 575 | .options = WDIOF_OVERHEAT | |
| 576 | WDIOF_CARDRESET | |
| 577 | WDIOF_KEEPALIVEPING | |
| 578 | WDIOF_SETTIMEOUT | |
| 579 | WDIOF_MAGICCLOSE, |
| 580 | .firmware_version = 1, |
| 581 | .identity = "PCWD", |
| 582 | }; |
| 583 | |
| 584 | switch(cmd) { |
| 585 | default: |
Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 586 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
| 588 | case WDIOC_GETSUPPORT: |
| 589 | if(copy_to_user(argp, &ident, sizeof(ident))) |
| 590 | return -EFAULT; |
| 591 | return 0; |
| 592 | |
| 593 | case WDIOC_GETSTATUS: |
| 594 | pcwd_get_status(&status); |
| 595 | return put_user(status, argp); |
| 596 | |
| 597 | case WDIOC_GETBOOTSTATUS: |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 598 | return put_user(pcwd_private.boot_status, argp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | case WDIOC_GETTEMP: |
| 601 | if (pcwd_get_temperature(&temperature)) |
| 602 | return -EFAULT; |
| 603 | |
| 604 | return put_user(temperature, argp); |
| 605 | |
| 606 | case WDIOC_SETOPTIONS: |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 607 | if (pcwd_private.revision == PCWD_REVISION_C) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | { |
| 609 | if(copy_from_user(&rv, argp, sizeof(int))) |
| 610 | return -EFAULT; |
| 611 | |
| 612 | if (rv & WDIOS_DISABLECARD) |
| 613 | { |
| 614 | return pcwd_stop(); |
| 615 | } |
| 616 | |
| 617 | if (rv & WDIOS_ENABLECARD) |
| 618 | { |
| 619 | return pcwd_start(); |
| 620 | } |
| 621 | |
| 622 | if (rv & WDIOS_TEMPPANIC) |
| 623 | { |
| 624 | temp_panic = 1; |
| 625 | } |
| 626 | } |
| 627 | return -EINVAL; |
| 628 | |
| 629 | case WDIOC_KEEPALIVE: |
| 630 | pcwd_keepalive(); |
| 631 | return 0; |
| 632 | |
| 633 | case WDIOC_SETTIMEOUT: |
| 634 | if (get_user(new_heartbeat, argp)) |
| 635 | return -EFAULT; |
| 636 | |
| 637 | if (pcwd_set_heartbeat(new_heartbeat)) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | pcwd_keepalive(); |
| 641 | /* Fall */ |
| 642 | |
| 643 | case WDIOC_GETTIMEOUT: |
| 644 | return put_user(heartbeat, argp); |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len, |
| 651 | loff_t *ppos) |
| 652 | { |
| 653 | if (len) { |
| 654 | if (!nowayout) { |
| 655 | size_t i; |
| 656 | |
| 657 | /* In case it was set long ago */ |
| 658 | expect_close = 0; |
| 659 | |
| 660 | for (i = 0; i != len; i++) { |
| 661 | char c; |
| 662 | |
| 663 | if (get_user(c, buf + i)) |
| 664 | return -EFAULT; |
| 665 | if (c == 'V') |
| 666 | expect_close = 42; |
| 667 | } |
| 668 | } |
| 669 | pcwd_keepalive(); |
| 670 | } |
| 671 | return len; |
| 672 | } |
| 673 | |
| 674 | static int pcwd_open(struct inode *inode, struct file *file) |
| 675 | { |
| 676 | if (!atomic_dec_and_test(&open_allowed) ) { |
Wim Van Sebroeck | c324ab4 | 2006-02-12 17:12:55 +0100 | [diff] [blame] | 677 | if (debug >= VERBOSE) |
| 678 | printk(KERN_ERR PFX "Attempt to open already opened device.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | atomic_inc( &open_allowed ); |
| 680 | return -EBUSY; |
| 681 | } |
| 682 | |
| 683 | if (nowayout) |
| 684 | __module_get(THIS_MODULE); |
| 685 | |
| 686 | /* Activate */ |
| 687 | pcwd_start(); |
| 688 | pcwd_keepalive(); |
| 689 | return nonseekable_open(inode, file); |
| 690 | } |
| 691 | |
| 692 | static int pcwd_close(struct inode *inode, struct file *file) |
| 693 | { |
| 694 | if (expect_close == 42) { |
| 695 | pcwd_stop(); |
| 696 | } else { |
| 697 | printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n"); |
| 698 | pcwd_keepalive(); |
| 699 | } |
| 700 | expect_close = 0; |
| 701 | atomic_inc( &open_allowed ); |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /* |
| 706 | * /dev/temperature handling |
| 707 | */ |
| 708 | |
| 709 | static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count, |
| 710 | loff_t *ppos) |
| 711 | { |
| 712 | int temperature; |
| 713 | |
| 714 | if (pcwd_get_temperature(&temperature)) |
| 715 | return -EFAULT; |
| 716 | |
| 717 | if (copy_to_user(buf, &temperature, 1)) |
| 718 | return -EFAULT; |
| 719 | |
| 720 | return 1; |
| 721 | } |
| 722 | |
| 723 | static int pcwd_temp_open(struct inode *inode, struct file *file) |
| 724 | { |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 725 | if (!pcwd_private.supports_temp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | return -ENODEV; |
| 727 | |
| 728 | return nonseekable_open(inode, file); |
| 729 | } |
| 730 | |
| 731 | static int pcwd_temp_close(struct inode *inode, struct file *file) |
| 732 | { |
| 733 | return 0; |
| 734 | } |
| 735 | |
| 736 | /* |
| 737 | * Notify system |
| 738 | */ |
| 739 | |
| 740 | static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused) |
| 741 | { |
| 742 | if (code==SYS_DOWN || code==SYS_HALT) { |
| 743 | /* Turn the WDT off */ |
| 744 | pcwd_stop(); |
| 745 | } |
| 746 | |
| 747 | return NOTIFY_DONE; |
| 748 | } |
| 749 | |
| 750 | /* |
| 751 | * Kernel Interfaces |
| 752 | */ |
| 753 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 754 | static const struct file_operations pcwd_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | .owner = THIS_MODULE, |
| 756 | .llseek = no_llseek, |
| 757 | .write = pcwd_write, |
| 758 | .ioctl = pcwd_ioctl, |
| 759 | .open = pcwd_open, |
| 760 | .release = pcwd_close, |
| 761 | }; |
| 762 | |
| 763 | static struct miscdevice pcwd_miscdev = { |
| 764 | .minor = WATCHDOG_MINOR, |
| 765 | .name = "watchdog", |
| 766 | .fops = &pcwd_fops, |
| 767 | }; |
| 768 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 769 | static const struct file_operations pcwd_temp_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | .owner = THIS_MODULE, |
| 771 | .llseek = no_llseek, |
| 772 | .read = pcwd_temp_read, |
| 773 | .open = pcwd_temp_open, |
| 774 | .release = pcwd_temp_close, |
| 775 | }; |
| 776 | |
| 777 | static struct miscdevice temp_miscdev = { |
| 778 | .minor = TEMP_MINOR, |
| 779 | .name = "temperature", |
| 780 | .fops = &pcwd_temp_fops, |
| 781 | }; |
| 782 | |
| 783 | static struct notifier_block pcwd_notifier = { |
| 784 | .notifier_call = pcwd_notify_sys, |
| 785 | }; |
| 786 | |
| 787 | /* |
| 788 | * Init & exit routines |
| 789 | */ |
| 790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | static inline int get_revision(void) |
| 792 | { |
| 793 | int r = PCWD_REVISION_C; |
| 794 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 795 | spin_lock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | /* REV A cards use only 2 io ports; test |
| 797 | * presumes a floating bus reads as 0xff. */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 798 | if ((inb(pcwd_private.io_addr + 2) == 0xFF) || |
| 799 | (inb(pcwd_private.io_addr + 3) == 0xFF)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | r=PCWD_REVISION_A; |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 801 | spin_unlock(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
| 803 | return r; |
| 804 | } |
| 805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | static int __devinit pcwatchdog_init(int base_addr) |
| 807 | { |
| 808 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
| 810 | cards_found++; |
| 811 | if (cards_found == 1) |
| 812 | printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER); |
| 813 | |
| 814 | if (cards_found > 1) { |
| 815 | printk(KERN_ERR PFX "This driver only supports 1 device\n"); |
| 816 | return -ENODEV; |
| 817 | } |
| 818 | |
| 819 | if (base_addr == 0x0000) { |
| 820 | printk(KERN_ERR PFX "No I/O-Address for card detected\n"); |
| 821 | return -ENODEV; |
| 822 | } |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 823 | pcwd_private.io_addr = base_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | /* Check card's revision */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 826 | pcwd_private.revision = get_revision(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 828 | if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | printk(KERN_ERR PFX "I/O address 0x%04x already in use\n", |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 830 | pcwd_private.io_addr); |
| 831 | pcwd_private.io_addr = 0x0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return -EIO; |
| 833 | } |
| 834 | |
| 835 | /* Initial variables */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 836 | pcwd_private.supports_temp = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | temp_panic = 0; |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 838 | pcwd_private.boot_status = 0x0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
| 840 | /* get the boot_status */ |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 841 | pcwd_get_status(&pcwd_private.boot_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | /* clear the "card caused reboot" flag */ |
| 844 | pcwd_clear_status(); |
| 845 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 846 | init_timer(&pcwd_private.timer); |
| 847 | pcwd_private.timer.function = pcwd_timer_ping; |
| 848 | pcwd_private.timer.data = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | |
| 850 | /* Disable the board */ |
| 851 | pcwd_stop(); |
| 852 | |
| 853 | /* Check whether or not the card supports the temperature device */ |
Wim Van Sebroeck | 8587521 | 2006-01-09 21:59:39 +0100 | [diff] [blame] | 854 | pcwd_check_temperature_support(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
Wim Van Sebroeck | af3b38d | 2006-01-09 22:03:41 +0100 | [diff] [blame] | 856 | /* Show info about the card itself */ |
| 857 | pcwd_show_card_info(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Wim Van Sebroeck | f3dc073 | 2007-01-09 22:43:49 +0100 | [diff] [blame^] | 859 | /* If heartbeat = 0 then we use the heartbeat from the dip-switches */ |
| 860 | if (heartbeat == 0) |
| 861 | heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)]; |
| 862 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | /* Check that the heartbeat value is within it's range ; if not reset to the default */ |
Wim Van Sebroeck | fd41fa6 | 2005-12-10 14:22:37 +0100 | [diff] [blame] | 864 | if (pcwd_set_heartbeat(heartbeat)) { |
| 865 | pcwd_set_heartbeat(WATCHDOG_HEARTBEAT); |
| 866 | printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n", |
| 867 | WATCHDOG_HEARTBEAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | ret = register_reboot_notifier(&pcwd_notifier); |
| 871 | if (ret) { |
| 872 | printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", |
| 873 | ret); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 874 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
| 875 | pcwd_private.io_addr = 0x0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | return ret; |
| 877 | } |
| 878 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 879 | if (pcwd_private.supports_temp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | ret = misc_register(&temp_miscdev); |
| 881 | if (ret) { |
| 882 | printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", |
| 883 | TEMP_MINOR, ret); |
| 884 | unregister_reboot_notifier(&pcwd_notifier); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 885 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
| 886 | pcwd_private.io_addr = 0x0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return ret; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | ret = misc_register(&pcwd_miscdev); |
| 892 | if (ret) { |
| 893 | printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", |
| 894 | WATCHDOG_MINOR, ret); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 895 | if (pcwd_private.supports_temp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | misc_deregister(&temp_miscdev); |
| 897 | unregister_reboot_notifier(&pcwd_notifier); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 898 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
| 899 | pcwd_private.io_addr = 0x0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | return ret; |
| 901 | } |
| 902 | |
| 903 | printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n", |
| 904 | heartbeat, nowayout); |
| 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static void __devexit pcwatchdog_exit(void) |
| 910 | { |
| 911 | /* Disable the board */ |
| 912 | if (!nowayout) |
| 913 | pcwd_stop(); |
| 914 | |
| 915 | /* Deregister */ |
| 916 | misc_deregister(&pcwd_miscdev); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 917 | if (pcwd_private.supports_temp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | misc_deregister(&temp_miscdev); |
| 919 | unregister_reboot_notifier(&pcwd_notifier); |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 920 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
| 921 | pcwd_private.io_addr = 0x0000; |
Wim Van Sebroeck | f1c3a05 | 2005-12-10 14:36:24 +0100 | [diff] [blame] | 922 | cards_found--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* |
| 926 | * The ISA cards have a heartbeat bit in one of the registers, which |
| 927 | * register is card dependent. The heartbeat bit is monitored, and if |
| 928 | * found, is considered proof that a Berkshire card has been found. |
| 929 | * The initial rate is once per second at board start up, then twice |
| 930 | * per second for normal operation. |
| 931 | */ |
| 932 | static int __init pcwd_checkcard(int base_addr) |
| 933 | { |
| 934 | int port0, last_port0; /* Reg 0, in case it's REV A */ |
| 935 | int port1, last_port1; /* Register 1 for REV C cards */ |
| 936 | int i; |
| 937 | int retval; |
| 938 | |
| 939 | if (!request_region (base_addr, 4, "PCWD")) { |
| 940 | printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr); |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | retval = 0; |
| 945 | |
| 946 | port0 = inb_p(base_addr); /* For REV A boards */ |
| 947 | port1 = inb_p(base_addr + 1); /* For REV C boards */ |
| 948 | if (port0 != 0xff || port1 != 0xff) { |
| 949 | /* Not an 'ff' from a floating bus, so must be a card! */ |
| 950 | for (i = 0; i < 4; ++i) { |
| 951 | |
| 952 | msleep(500); |
| 953 | |
| 954 | last_port0 = port0; |
| 955 | last_port1 = port1; |
| 956 | |
| 957 | port0 = inb_p(base_addr); |
| 958 | port1 = inb_p(base_addr + 1); |
| 959 | |
| 960 | /* Has either hearbeat bit changed? */ |
| 961 | if ((port0 ^ last_port0) & WD_HRTBT || |
| 962 | (port1 ^ last_port1) & WD_REVC_HRBT) { |
| 963 | retval = 1; |
| 964 | break; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | release_region (base_addr, 4); |
| 969 | |
| 970 | return retval; |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | * These are the auto-probe addresses available. |
| 975 | * |
| 976 | * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350. |
| 977 | * Revision A has an address range of 2 addresses, while Revision C has 4. |
| 978 | */ |
| 979 | static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 }; |
| 980 | |
| 981 | static int __init pcwd_init_module(void) |
| 982 | { |
| 983 | int i, found = 0; |
| 984 | |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 985 | spin_lock_init(&pcwd_private.io_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
| 987 | for (i = 0; pcwd_ioports[i] != 0; i++) { |
| 988 | if (pcwd_checkcard(pcwd_ioports[i])) { |
| 989 | if (!(pcwatchdog_init(pcwd_ioports[i]))) |
| 990 | found++; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | if (!found) { |
| 995 | printk (KERN_INFO PFX "No card detected, or port not available\n"); |
| 996 | return -ENODEV; |
| 997 | } |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
| 1002 | static void __exit pcwd_cleanup_module(void) |
| 1003 | { |
Wim Van Sebroeck | a2be878 | 2006-01-09 21:53:33 +0100 | [diff] [blame] | 1004 | if (pcwd_private.io_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | pcwatchdog_exit(); |
Wim Van Sebroeck | 369fa25 | 2006-02-12 17:44:57 +0100 | [diff] [blame] | 1006 | |
| 1007 | printk(KERN_INFO PFX "Watchdog Module Unloaded.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | module_init(pcwd_init_module); |
| 1011 | module_exit(pcwd_cleanup_module); |
| 1012 | |
| 1013 | MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>"); |
| 1014 | MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver"); |
| 1015 | MODULE_LICENSE("GPL"); |
| 1016 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 1017 | MODULE_ALIAS_MISCDEV(TEMP_MINOR); |