blob: aab6621817a057a79323f55c919c7b2b0fdafa70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PC Watchdog Driver
3 * by Ken Hollis (khollis@bitgate.com)
4 *
Wim Van Sebroeckf9146f22007-01-09 22:38:54 +01005 * Permission granted from Simon Machell (smachell@berkprod.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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 Sebroeckfd41fa62005-12-10 14:22:37 +010052#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 Torvalds1da177e2005-04-16 15:20:36 -070068
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010069#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 Sebroeckf3dc0732007-01-09 22:43:49 +010073#define WATCHDOG_VERSION "1.18"
74#define WATCHDOG_DATE "06 Jan 2007"
Wim Van Sebroecka7122f92006-01-09 22:07:22 +010075#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 Torvalds1da177e2005-04-16 15:20:36 -070080
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 Sebroeck8f0235d2006-01-09 21:56:09 +010092 * 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 Sebroeck4206f0c2006-02-12 16:37:36 +010096#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 Sebroeck8f0235d2006-01-09 21:56:09 +0100101/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100102#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 Sebroeck8f0235d2006-01-09 21:56:09 +0100109/* 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 Torvalds1da177e2005-04-16 15:20:36 -0700114
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 Sebroeckfd41fa62005-12-10 14:22:37 +0100120#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 Sebroeck369fa252006-02-12 17:44:57 +0100126#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 Sebroeckfd41fa62005-12-10 14:22:37 +0100130#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 Sebroeck369fa252006-02-12 17:44:57 +0100133#define CMD_ISA_RESET_RELAYS 0x0D
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100135/* Watchdog's Dip Switch heartbeat values */
136static 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 Torvalds1da177e2005-04-16 15:20:36 -0700147/*
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 */
156static int cards_found;
157
158/* internal variables */
159static atomic_t open_allowed = ATOMIC_INIT(1);
160static char expect_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int temp_panic;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100162static struct { /* this is private data for each ISA-PC watchdog card */
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100163 char fw_ver_str[6]; /* The cards firmware version */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100164 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 Torvalds1da177e2005-04-16 15:20:36 -0700173
174/* module parameters */
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100175#define QUIET 0 /* Default */
176#define VERBOSE 1 /* Verbose */
177#define DEBUG 2 /* print fancy stuff too */
178static int debug = QUIET;
179module_param(debug, int, 0);
180MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
181
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100182#define WATCHDOG_HEARTBEAT 0 /* default heartbeat = delay-time from dip-switches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int heartbeat = WATCHDOG_HEARTBEAT;
184module_param(heartbeat, int, 0);
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100185MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Andrey Panin4bfdf372005-07-27 11:43:58 -0700187static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188module_param(nowayout, int, 0);
189MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
190
191/*
192 * Internal functions
193 */
194
195static 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 Sebroeckc324ab42006-02-12 17:12:55 +0100201 if (debug >= DEBUG)
202 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
203 cmd);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* The WCMD bit must be 1 and the command is only 4 bits in size */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100206 control_status = (cmd & 0x0F) | WD_WCMD;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100207 outb_p(control_status, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 udelay(ISA_COMMAND_TIMEOUT);
209
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100210 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 for (i = 0; i < 25; ++i) {
212 last_port0 = port0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100213 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 if (port0 == last_port0)
216 break; /* Data is stable */
217
218 udelay (250);
219 }
220
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100221 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 Torvalds1da177e2005-04-16 15:20:36 -0700225 return port0;
226}
227
228static int set_command_mode(void)
229{
230 int i, found=0, count=0;
231
232 /* Set the card into command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100233 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 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 Sebroecka2be8782006-01-09 21:53:33 +0100241 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 udelay(1200); /* Spec says wait 1ms */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100243 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 udelay(ISA_COMMAND_TIMEOUT);
245 }
246 count++;
247 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100248 spin_unlock(&pcwd_private.io_lock);
249 pcwd_private.command_mode = found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100251 if (debug >= DEBUG)
252 printk(KERN_DEBUG PFX "command_mode=%d\n",
253 pcwd_private.command_mode);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return(found);
256}
257
258static void unset_command_mode(void)
259{
260 /* Set the card into normal mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100261 spin_lock(&pcwd_private.io_lock);
262 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100264 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100266 pcwd_private.command_mode = 0;
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100267
268 if (debug >= DEBUG)
269 printk(KERN_DEBUG PFX "command_mode=%d\n",
270 pcwd_private.command_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100273static 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 Sebroeck2891b6a2006-02-12 16:47:34 +0100279static inline void pcwd_get_firmware(void)
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100280{
281 int one, ten, hund, minor;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100282
Wim Van Sebroeck6bbc20b2006-03-02 20:05:16 +0100283 strcpy(pcwd_private.fw_ver_str, "ERROR");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100284
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 Sebroeck2891b6a2006-02-12 16:47:34 +0100290 sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c", one, ten, hund, minor);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100291 }
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100292 unset_command_mode();
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100293
294 return;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100295}
296
297static 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
310static void pcwd_show_card_info(void)
311{
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100312 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 Sebroeck2891b6a2006-02-12 16:47:34 +0100318 pcwd_get_firmware();
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100319 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100320 pcwd_private.io_addr, pcwd_private.fw_ver_str);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100321 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 Torvalds1da177e2005-04-16 15:20:36 -0700349static 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 Sebroecka2be8782006-01-09 21:53:33 +0100355 if(time_before(jiffies, pcwd_private.next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* Ping the watchdog */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100357 spin_lock(&pcwd_private.io_lock);
358 if (pcwd_private.revision == PCWD_REVISION_A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100360 wdrst_stat = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 wdrst_stat &= 0x0F;
362 wdrst_stat |= WD_WDRST;
363
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100364 outb_p(wdrst_stat, pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } else {
366 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100367 outb_p(0x00, pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369
370 /* Re-set the timer interval */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100371 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100373 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 } else {
375 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
376 }
377}
378
379static int pcwd_start(void)
380{
381 int stat_reg;
382
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100383 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* Start the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100386 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* Enable the port */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100389 if (pcwd_private.revision == PCWD_REVISION_C) {
390 spin_lock(&pcwd_private.io_lock);
391 outb_p(0x00, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100393 stat_reg = inb_p(pcwd_private.io_addr + 2);
394 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100395 if (stat_reg & WD_WDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 printk(KERN_INFO PFX "Could not start watchdog\n");
397 return -EIO;
398 }
399 }
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100400
401 if (debug >= VERBOSE)
402 printk(KERN_DEBUG PFX "Watchdog started\n");
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
406
407static int pcwd_stop(void)
408{
409 int stat_reg;
410
411 /* Stop the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100412 del_timer(&pcwd_private.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* Disable the board */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100415 if (pcwd_private.revision == PCWD_REVISION_C) {
416 spin_lock(&pcwd_private.io_lock);
417 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100419 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100421 stat_reg = inb_p(pcwd_private.io_addr + 2);
422 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100423 if ((stat_reg & WD_WDIS) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 printk(KERN_INFO PFX "Could not stop watchdog\n");
425 return -EIO;
426 }
427 }
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100428
429 if (debug >= VERBOSE)
430 printk(KERN_DEBUG PFX "Watchdog stopped\n");
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0;
433}
434
435static int pcwd_keepalive(void)
436{
437 /* user land ping */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100438 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100439
440 if (debug >= DEBUG)
441 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444}
445
446static 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 Sebroeckc324ab42006-02-12 17:12:55 +0100452
453 if (debug >= VERBOSE)
454 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
455 heartbeat);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
460static int pcwd_get_status(int *status)
461{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100462 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 *status=0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100465 spin_lock(&pcwd_private.io_lock);
466 if (pcwd_private.revision == PCWD_REVISION_A)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* Rev A cards return status information from
468 * the base register, which is used for the
469 * temperature in other cards. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100470 control_status = inb(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 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 Sebroeck4206f0c2006-02-12 16:37:36 +0100477 control_status = inb(pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100479 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100481 if (pcwd_private.revision == PCWD_REVISION_A) {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100482 if (control_status & WD_WDRST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 *status |= WDIOF_CARDRESET;
484
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100485 if (control_status & WD_T110) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *status |= WDIOF_OVERHEAT;
487 if (temp_panic) {
488 printk (KERN_INFO PFX "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600489 kernel_power_off();
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100490 /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 }
493 } else {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100494 if (control_status & WD_REVC_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 *status |= WDIOF_CARDRESET;
496
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100497 if (control_status & WD_REVC_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 *status |= WDIOF_OVERHEAT;
499 if (temp_panic) {
500 printk (KERN_INFO PFX "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600501 kernel_power_off();
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100502 /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
505 }
506
507 return 0;
508}
509
510static int pcwd_clear_status(void)
511{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100512 int control_status;
513
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100514 if (pcwd_private.revision == PCWD_REVISION_C) {
515 spin_lock(&pcwd_private.io_lock);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100516
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100517 if (debug >= VERBOSE)
518 printk(KERN_INFO PFX "clearing watchdog trip status\n");
519
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100520 control_status = inb_p(pcwd_private.io_addr + 1);
521
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100522 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 Sebroeck4206f0c2006-02-12 16:37:36 +0100528 /* 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 Sebroecka2be8782006-01-09 21:53:33 +0100531 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 return 0;
534}
535
536static int pcwd_get_temperature(int *temperature)
537{
538 /* check that port 0 gives temperature info and no command results */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100539 if (pcwd_private.command_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -1;
541
542 *temperature = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100543 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -ENODEV;
545
546 /*
547 * Convert celsius to fahrenheit, since this was
548 * the decided 'standard' for this return value.
549 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100550 spin_lock(&pcwd_private.io_lock);
551 *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
552 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100554 if (debug >= DEBUG) {
555 printk(KERN_DEBUG PFX "temperature is: %d F\n",
556 *temperature);
557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
560}
561
562/*
563 * /dev/watchdog handling
564 */
565
566static 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 Tardieu795b89d2006-09-09 17:34:31 +0200586 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
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 Sebroecka2be8782006-01-09 21:53:33 +0100598 return put_user(pcwd_private.boot_status, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
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 Sebroecka2be8782006-01-09 21:53:33 +0100607 if (pcwd_private.revision == PCWD_REVISION_C)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 {
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
650static 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
674static int pcwd_open(struct inode *inode, struct file *file)
675{
676 if (!atomic_dec_and_test(&open_allowed) ) {
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100677 if (debug >= VERBOSE)
678 printk(KERN_ERR PFX "Attempt to open already opened device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 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
692static 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
709static 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
723static int pcwd_temp_open(struct inode *inode, struct file *file)
724{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100725 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -ENODEV;
727
728 return nonseekable_open(inode, file);
729}
730
731static int pcwd_temp_close(struct inode *inode, struct file *file)
732{
733 return 0;
734}
735
736/*
737 * Notify system
738 */
739
740static 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 Ven62322d22006-07-03 00:24:21 -0700754static const struct file_operations pcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 .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
763static struct miscdevice pcwd_miscdev = {
764 .minor = WATCHDOG_MINOR,
765 .name = "watchdog",
766 .fops = &pcwd_fops,
767};
768
Arjan van de Ven62322d22006-07-03 00:24:21 -0700769static const struct file_operations pcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 .owner = THIS_MODULE,
771 .llseek = no_llseek,
772 .read = pcwd_temp_read,
773 .open = pcwd_temp_open,
774 .release = pcwd_temp_close,
775};
776
777static struct miscdevice temp_miscdev = {
778 .minor = TEMP_MINOR,
779 .name = "temperature",
780 .fops = &pcwd_temp_fops,
781};
782
783static struct notifier_block pcwd_notifier = {
784 .notifier_call = pcwd_notify_sys,
785};
786
787/*
788 * Init & exit routines
789 */
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791static inline int get_revision(void)
792{
793 int r = PCWD_REVISION_C;
794
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100795 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /* REV A cards use only 2 io ports; test
797 * presumes a floating bus reads as 0xff. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100798 if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
799 (inb(pcwd_private.io_addr + 3) == 0xFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 r=PCWD_REVISION_A;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100801 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 return r;
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static int __devinit pcwatchdog_init(int base_addr)
807{
808 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
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 Sebroecka2be8782006-01-09 21:53:33 +0100823 pcwd_private.io_addr = base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* Check card's revision */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100826 pcwd_private.revision = get_revision();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100828 if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100830 pcwd_private.io_addr);
831 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return -EIO;
833 }
834
835 /* Initial variables */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100836 pcwd_private.supports_temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 temp_panic = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100838 pcwd_private.boot_status = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /* get the boot_status */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100841 pcwd_get_status(&pcwd_private.boot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* clear the "card caused reboot" flag */
844 pcwd_clear_status();
845
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100846 init_timer(&pcwd_private.timer);
847 pcwd_private.timer.function = pcwd_timer_ping;
848 pcwd_private.timer.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* Disable the board */
851 pcwd_stop();
852
853 /* Check whether or not the card supports the temperature device */
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100854 pcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100856 /* Show info about the card itself */
857 pcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100859 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700863 /* Check that the heartbeat value is within it's range ; if not reset to the default */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100864 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 Torvalds1da177e2005-04-16 15:20:36 -0700868 }
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 Sebroecka2be8782006-01-09 21:53:33 +0100874 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
875 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ret;
877 }
878
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100879 if (pcwd_private.supports_temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 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 Sebroecka2be8782006-01-09 21:53:33 +0100885 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
886 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 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 Sebroecka2be8782006-01-09 21:53:33 +0100895 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 misc_deregister(&temp_miscdev);
897 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100898 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
899 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 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
909static 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 Sebroecka2be8782006-01-09 21:53:33 +0100917 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 misc_deregister(&temp_miscdev);
919 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100920 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
921 pcwd_private.io_addr = 0x0000;
Wim Van Sebroeckf1c3a052005-12-10 14:36:24 +0100922 cards_found--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
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 */
932static 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 */
979static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
980
981static int __init pcwd_init_module(void)
982{
983 int i, found = 0;
984
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100985 spin_lock_init(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
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
1002static void __exit pcwd_cleanup_module(void)
1003{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +01001004 if (pcwd_private.io_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 pcwatchdog_exit();
Wim Van Sebroeck369fa252006-02-12 17:44:57 +01001006
1007 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010module_init(pcwd_init_module);
1011module_exit(pcwd_cleanup_module);
1012
1013MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
1014MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
1015MODULE_LICENSE("GPL");
1016MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
1017MODULE_ALIAS_MISCDEV(TEMP_MINOR);