blob: 9e1331a3b2157e5c7e746ab164209bc0fe77cbe7 [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>
Alan Cox261dcc72008-05-19 14:07:43 +010043 * 011214 Added nowayout module option to override
44 * CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Added timeout module option to override default
46 */
47
48/*
49 * A bells and whistles driver is available from http://www.pcwd.de/
Alan Cox261dcc72008-05-19 14:07:43 +010050 * More info available at http://www.berkprod.com/ or
51 * http://www.pcwatchdog.com/
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010054#include <linux/module.h> /* For module specific items */
55#include <linux/moduleparam.h> /* For new moduleparam's */
56#include <linux/types.h> /* For standard types (like size_t) */
57#include <linux/errno.h> /* For the -ENODEV/... values */
58#include <linux/kernel.h> /* For printk/panic/... */
59#include <linux/delay.h> /* For mdelay function */
60#include <linux/timer.h> /* For timer related operations */
61#include <linux/jiffies.h> /* For jiffies stuff */
62#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
63#include <linux/watchdog.h> /* For the watchdog specific items */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000064#include <linux/reboot.h> /* For kernel_power_off() */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010065#include <linux/init.h> /* For __init/__exit/... */
66#include <linux/fs.h> /* For file operations */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000067#include <linux/isa.h> /* For isa devices */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010068#include <linux/ioport.h> /* For io-port access */
69#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Alan Cox261dcc72008-05-19 14:07:43 +010070#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
71#include <linux/io.h> /* For inb/outb/... */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010072
73/* Module and version information */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000074#define WATCHDOG_VERSION "1.20"
75#define WATCHDOG_DATE "18 Feb 2007"
Wim Van Sebroecka7122f92006-01-09 22:07:22 +010076#define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
77#define WATCHDOG_NAME "pcwd"
78#define PFX WATCHDOG_NAME ": "
79#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
80#define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * It should be noted that PCWD_REVISION_B was removed because A and B
84 * are essentially the same types of card, with the exception that B
85 * has temperature reporting. Since I didn't receive a Rev.B card,
86 * the Rev.B card is not supported. (It's a good thing too, as they
87 * are no longer in production.)
88 */
89#define PCWD_REVISION_A 1
90#define PCWD_REVISION_C 2
91
92/*
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000093 * These are the auto-probe addresses available.
94 *
95 * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
96 * Revision A has an address range of 2 addresses, while Revision C has 4.
97 */
98#define PCWD_ISA_NR_CARDS 3
99static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
100
101/*
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100102 * These are the defines that describe the control status bits for the
103 * PCI-PC Watchdog card.
104*/
105/* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100106#define WD_WDRST 0x01 /* Previously reset state */
107#define WD_T110 0x02 /* Temperature overheat sense */
108#define WD_HRTBT 0x04 /* Heartbeat sense */
109#define WD_RLY2 0x08 /* External relay triggered */
110#define WD_SRLY2 0x80 /* Software external relay triggered */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100111/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100112#define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
113#define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
114#define WD_REVC_TTRP 0x04 /* Temperature Trip status */
Alan Cox261dcc72008-05-19 14:07:43 +0100115#define WD_REVC_RL2A 0x08 /* Relay 2 activated by
116 on-board processor */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100117#define WD_REVC_RL1A 0x10 /* Relay 1 active */
118#define WD_REVC_R2DS 0x40 /* Relay 2 disable */
119#define WD_REVC_RLY2 0x80 /* Relay 2 activated? */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100120/* Port 2 : Control Status #2 */
121#define WD_WDIS 0x10 /* Watchdog Disabled */
122#define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */
Alan Cox261dcc72008-05-19 14:07:43 +0100123#define WD_SSEL 0x40 /* Watchdog Switch Select
124 (1:SW1 <-> 0:SW2) */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100125#define WD_WCMD 0x80 /* Watchdog Command Mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* max. time we give an ISA watchdog card to process a command */
128/* 500ms for each 4 bit response (according to spec.) */
129#define ISA_COMMAND_TIMEOUT 1000
130
131/* Watchdog's internal commands */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100132#define CMD_ISA_IDLE 0x00
133#define CMD_ISA_VERSION_INTEGER 0x01
134#define CMD_ISA_VERSION_TENTH 0x02
135#define CMD_ISA_VERSION_HUNDRETH 0x03
136#define CMD_ISA_VERSION_MINOR 0x04
137#define CMD_ISA_SWITCH_SETTINGS 0x05
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100138#define CMD_ISA_RESET_PC 0x06
139#define CMD_ISA_ARM_0 0x07
140#define CMD_ISA_ARM_30 0x08
141#define CMD_ISA_ARM_60 0x09
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100142#define CMD_ISA_DELAY_TIME_2SECS 0x0A
143#define CMD_ISA_DELAY_TIME_4SECS 0x0B
144#define CMD_ISA_DELAY_TIME_8SECS 0x0C
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100145#define CMD_ISA_RESET_RELAYS 0x0D
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100147/* Watchdog's Dip Switch heartbeat values */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000148static const int heartbeat_tbl[] = {
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100149 20, /* OFF-OFF-OFF = 20 Sec */
150 40, /* OFF-OFF-ON = 40 Sec */
151 60, /* OFF-ON-OFF = 1 Min */
152 300, /* OFF-ON-ON = 5 Min */
153 600, /* ON-OFF-OFF = 10 Min */
154 1800, /* ON-OFF-ON = 30 Min */
155 3600, /* ON-ON-OFF = 1 Hour */
156 7200, /* ON-ON-ON = 2 hour */
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * We are using an kernel timer to do the pinging of the watchdog
161 * every ~500ms. We try to set the internal heartbeat of the
162 * watchdog to 2 ms.
163 */
164
165#define WDT_INTERVAL (HZ/2+1)
166
167/* We can only use 1 card due to the /dev/watchdog restriction */
168static int cards_found;
169
170/* internal variables */
Wim Van Sebroeck36cbaa82008-08-10 21:57:03 +0000171static unsigned long open_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static char expect_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int temp_panic;
Alan Cox261dcc72008-05-19 14:07:43 +0100174
175/* this is private data for each ISA-PC watchdog card */
176static struct {
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100177 char fw_ver_str[6]; /* The cards firmware version */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100178 int revision; /* The card's revision */
Alan Cox261dcc72008-05-19 14:07:43 +0100179 int supports_temp; /* Whether or not the card has
180 a temperature device */
181 int command_mode; /* Whether or not the card is in
182 command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100183 int boot_status; /* The card's boot status */
184 int io_addr; /* The cards I/O address */
185 spinlock_t io_lock; /* the lock for io operations */
186 struct timer_list timer; /* The timer that pings the watchdog */
187 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
188} pcwd_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/* module parameters */
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100191#define QUIET 0 /* Default */
192#define VERBOSE 1 /* Verbose */
193#define DEBUG 2 /* print fancy stuff too */
194static int debug = QUIET;
195module_param(debug, int, 0);
Alan Cox261dcc72008-05-19 14:07:43 +0100196MODULE_PARM_DESC(debug,
197 "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100198
Alan Cox261dcc72008-05-19 14:07:43 +0100199/* default heartbeat = delay-time from dip-switches */
200#define WATCHDOG_HEARTBEAT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int heartbeat = WATCHDOG_HEARTBEAT;
202module_param(heartbeat, int, 0);
Alan Cox261dcc72008-05-19 14:07:43 +0100203MODULE_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 -0700204
Andrey Panin4bfdf372005-07-27 11:43:58 -0700205static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206module_param(nowayout, int, 0);
Alan Cox261dcc72008-05-19 14:07:43 +0100207MODULE_PARM_DESC(nowayout,
208 "Watchdog cannot be stopped once started (default="
209 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/*
212 * Internal functions
213 */
214
215static int send_isa_command(int cmd)
216{
217 int i;
218 int control_status;
219 int port0, last_port0; /* Double read for stabilising */
220
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100221 if (debug >= DEBUG)
222 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
223 cmd);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* The WCMD bit must be 1 and the command is only 4 bits in size */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100226 control_status = (cmd & 0x0F) | WD_WCMD;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100227 outb_p(control_status, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 udelay(ISA_COMMAND_TIMEOUT);
229
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100230 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 for (i = 0; i < 25; ++i) {
232 last_port0 = port0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100233 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (port0 == last_port0)
236 break; /* Data is stable */
237
Alan Cox261dcc72008-05-19 14:07:43 +0100238 udelay(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100241 if (debug >= DEBUG)
242 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
243 cmd, port0, last_port0);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return port0;
246}
247
248static int set_command_mode(void)
249{
Alan Cox261dcc72008-05-19 14:07:43 +0100250 int i, found = 0, count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* Set the card into command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100253 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 while ((!found) && (count < 3)) {
255 i = send_isa_command(CMD_ISA_IDLE);
256
257 if (i == 0x00)
258 found = 1;
259 else if (i == 0xF3) {
260 /* Card does not like what we've done to it */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100261 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 udelay(1200); /* Spec says wait 1ms */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100263 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 udelay(ISA_COMMAND_TIMEOUT);
265 }
266 count++;
267 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100268 spin_unlock(&pcwd_private.io_lock);
269 pcwd_private.command_mode = found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100271 if (debug >= DEBUG)
272 printk(KERN_DEBUG PFX "command_mode=%d\n",
273 pcwd_private.command_mode);
274
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000275 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static void unset_command_mode(void)
279{
280 /* Set the card into normal mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100281 spin_lock(&pcwd_private.io_lock);
282 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100284 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100286 pcwd_private.command_mode = 0;
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100287
288 if (debug >= DEBUG)
289 printk(KERN_DEBUG PFX "command_mode=%d\n",
290 pcwd_private.command_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100293static inline void pcwd_check_temperature_support(void)
294{
295 if (inb(pcwd_private.io_addr) != 0xF0)
296 pcwd_private.supports_temp = 1;
297}
298
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100299static inline void pcwd_get_firmware(void)
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100300{
301 int one, ten, hund, minor;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100302
Wim Van Sebroeck6bbc20b2006-03-02 20:05:16 +0100303 strcpy(pcwd_private.fw_ver_str, "ERROR");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100304
305 if (set_command_mode()) {
306 one = send_isa_command(CMD_ISA_VERSION_INTEGER);
307 ten = send_isa_command(CMD_ISA_VERSION_TENTH);
308 hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
309 minor = send_isa_command(CMD_ISA_VERSION_MINOR);
Alan Cox261dcc72008-05-19 14:07:43 +0100310 sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
311 one, ten, hund, minor);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100312 }
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100313 unset_command_mode();
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100314
315 return;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100316}
317
318static inline int pcwd_get_option_switches(void)
319{
Alan Cox261dcc72008-05-19 14:07:43 +0100320 int option_switches = 0;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100321
322 if (set_command_mode()) {
323 /* Get switch settings */
324 option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
325 }
326
327 unset_command_mode();
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000328 return option_switches;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100329}
330
331static void pcwd_show_card_info(void)
332{
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100333 int option_switches;
334
335 /* Get some extra info from the hardware (in command/debug/diag mode) */
336 if (pcwd_private.revision == PCWD_REVISION_A)
Alan Cox261dcc72008-05-19 14:07:43 +0100337 printk(KERN_INFO PFX
338 "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n",
339 pcwd_private.io_addr);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100340 else if (pcwd_private.revision == PCWD_REVISION_C) {
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100341 pcwd_get_firmware();
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100342 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 +0100343 pcwd_private.io_addr, pcwd_private.fw_ver_str);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100344 option_switches = pcwd_get_option_switches();
345 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
346 option_switches,
347 ((option_switches & 0x10) ? "ON" : "OFF"),
348 ((option_switches & 0x08) ? "ON" : "OFF"));
349
350 /* Reprogram internal heartbeat to 2 seconds */
351 if (set_command_mode()) {
352 send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
353 unset_command_mode();
354 }
355 }
356
357 if (pcwd_private.supports_temp)
358 printk(KERN_INFO PFX "Temperature Option Detected\n");
359
360 if (pcwd_private.boot_status & WDIOF_CARDRESET)
361 printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
362
363 if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
Alan Cox261dcc72008-05-19 14:07:43 +0100364 printk(KERN_EMERG PFX
365 "Card senses a CPU Overheat. Panicking!\n");
366 printk(KERN_EMERG PFX
367 "CPU Overheat\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100368 }
369
370 if (pcwd_private.boot_status == 0)
Alan Cox261dcc72008-05-19 14:07:43 +0100371 printk(KERN_INFO PFX
372 "No previous trip detected - Cold boot or reset\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static void pcwd_timer_ping(unsigned long data)
376{
377 int wdrst_stat;
378
379 /* If we got a heartbeat pulse within the WDT_INTERVAL
380 * we agree to ping the WDT */
Alan Cox261dcc72008-05-19 14:07:43 +0100381 if (time_before(jiffies, pcwd_private.next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* Ping the watchdog */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100383 spin_lock(&pcwd_private.io_lock);
384 if (pcwd_private.revision == PCWD_REVISION_A) {
Alan Cox261dcc72008-05-19 14:07:43 +0100385 /* Rev A cards are reset by setting the
386 WD_WDRST bit in register 1 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100387 wdrst_stat = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 wdrst_stat &= 0x0F;
389 wdrst_stat |= WD_WDRST;
390
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100391 outb_p(wdrst_stat, pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 } else {
393 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100394 outb_p(0x00, pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
397 /* Re-set the timer interval */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100398 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100400 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 } else {
Alan Cox261dcc72008-05-19 14:07:43 +0100402 printk(KERN_WARNING PFX
403 "Heartbeat lost! Will not ping the watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405}
406
407static int pcwd_start(void)
408{
409 int stat_reg;
410
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100411 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* Start the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100414 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* Enable the port */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100417 if (pcwd_private.revision == PCWD_REVISION_C) {
418 spin_lock(&pcwd_private.io_lock);
419 outb_p(0x00, 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 printk(KERN_INFO PFX "Could not start 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 started\n");
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return 0;
433}
434
435static int pcwd_stop(void)
436{
437 int stat_reg;
438
439 /* Stop the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100440 del_timer(&pcwd_private.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* Disable the board */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100443 if (pcwd_private.revision == PCWD_REVISION_C) {
444 spin_lock(&pcwd_private.io_lock);
445 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100447 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100449 stat_reg = inb_p(pcwd_private.io_addr + 2);
450 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100451 if ((stat_reg & WD_WDIS) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 printk(KERN_INFO PFX "Could not stop watchdog\n");
453 return -EIO;
454 }
455 }
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100456
457 if (debug >= VERBOSE)
458 printk(KERN_DEBUG PFX "Watchdog stopped\n");
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461}
462
463static int pcwd_keepalive(void)
464{
465 /* user land ping */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100466 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100467
468 if (debug >= DEBUG)
469 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return 0;
472}
473
474static int pcwd_set_heartbeat(int t)
475{
Alan Cox261dcc72008-05-19 14:07:43 +0100476 if (t < 2 || t > 7200) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return -EINVAL;
478
479 heartbeat = t;
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100480
481 if (debug >= VERBOSE)
482 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
483 heartbeat);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486}
487
488static int pcwd_get_status(int *status)
489{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100490 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alan Cox261dcc72008-05-19 14:07:43 +0100492 *status = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100493 spin_lock(&pcwd_private.io_lock);
494 if (pcwd_private.revision == PCWD_REVISION_A)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* Rev A cards return status information from
496 * the base register, which is used for the
497 * temperature in other cards. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100498 control_status = inb(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 else {
500 /* Rev C cards return card status in the base
501 * address + 1 register. And use different bits
502 * to indicate a card initiated reset, and an
503 * over-temperature condition. And the reboot
504 * status can be reset. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100505 control_status = inb(pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100507 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100509 if (pcwd_private.revision == PCWD_REVISION_A) {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100510 if (control_status & WD_WDRST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 *status |= WDIOF_CARDRESET;
512
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100513 if (control_status & WD_T110) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 *status |= WDIOF_OVERHEAT;
515 if (temp_panic) {
Alan Cox261dcc72008-05-19 14:07:43 +0100516 printk(KERN_INFO PFX
517 "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600518 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 }
521 } else {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100522 if (control_status & WD_REVC_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 *status |= WDIOF_CARDRESET;
524
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100525 if (control_status & WD_REVC_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *status |= WDIOF_OVERHEAT;
527 if (temp_panic) {
Alan Cox261dcc72008-05-19 14:07:43 +0100528 printk(KERN_INFO PFX
529 "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600530 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533 }
534
535 return 0;
536}
537
538static int pcwd_clear_status(void)
539{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100540 int control_status;
541
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100542 if (pcwd_private.revision == PCWD_REVISION_C) {
543 spin_lock(&pcwd_private.io_lock);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100544
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100545 if (debug >= VERBOSE)
Alan Cox261dcc72008-05-19 14:07:43 +0100546 printk(KERN_INFO PFX
547 "clearing watchdog trip status\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100548
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100549 control_status = inb_p(pcwd_private.io_addr + 1);
550
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100551 if (debug >= DEBUG) {
Alan Cox261dcc72008-05-19 14:07:43 +0100552 printk(KERN_DEBUG PFX "status was: 0x%02x\n",
553 control_status);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100554 printk(KERN_DEBUG PFX "sending: 0x%02x\n",
555 (control_status & WD_REVC_R2DS));
556 }
557
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100558 /* clear reset status & Keep Relay 2 disable state as it is */
Alan Cox261dcc72008-05-19 14:07:43 +0100559 outb_p((control_status & WD_REVC_R2DS),
560 pcwd_private.io_addr + 1);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100561
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100562 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 return 0;
565}
566
567static int pcwd_get_temperature(int *temperature)
568{
569 /* check that port 0 gives temperature info and no command results */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100570 if (pcwd_private.command_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -1;
572
573 *temperature = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100574 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -ENODEV;
576
577 /*
578 * Convert celsius to fahrenheit, since this was
579 * the decided 'standard' for this return value.
580 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100581 spin_lock(&pcwd_private.io_lock);
582 *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
583 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100585 if (debug >= DEBUG) {
586 printk(KERN_DEBUG PFX "temperature is: %d F\n",
587 *temperature);
588 }
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591}
592
593/*
594 * /dev/watchdog handling
595 */
596
Alan Cox261dcc72008-05-19 14:07:43 +0100597static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 int rv;
600 int status;
601 int temperature;
602 int new_heartbeat;
603 int __user *argp = (int __user *)arg;
604 static struct watchdog_info ident = {
605 .options = WDIOF_OVERHEAT |
606 WDIOF_CARDRESET |
607 WDIOF_KEEPALIVEPING |
608 WDIOF_SETTIMEOUT |
609 WDIOF_MAGICCLOSE,
610 .firmware_version = 1,
611 .identity = "PCWD",
612 };
613
Alan Cox261dcc72008-05-19 14:07:43 +0100614 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 case WDIOC_GETSUPPORT:
Alan Cox261dcc72008-05-19 14:07:43 +0100616 if (copy_to_user(argp, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EFAULT;
618 return 0;
619
620 case WDIOC_GETSTATUS:
621 pcwd_get_status(&status);
622 return put_user(status, argp);
623
624 case WDIOC_GETBOOTSTATUS:
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100625 return put_user(pcwd_private.boot_status, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 case WDIOC_GETTEMP:
628 if (pcwd_get_temperature(&temperature))
629 return -EFAULT;
630
631 return put_user(temperature, argp);
632
633 case WDIOC_SETOPTIONS:
Alan Cox261dcc72008-05-19 14:07:43 +0100634 if (pcwd_private.revision == PCWD_REVISION_C) {
635 if (get_user(rv, argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EFAULT;
637
Alan Cox261dcc72008-05-19 14:07:43 +0100638 if (rv & WDIOS_DISABLECARD) {
639 status = pcwd_stop();
640 if (status < 0)
641 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Alan Cox261dcc72008-05-19 14:07:43 +0100643 if (rv & WDIOS_ENABLECARD) {
644 status = pcwd_start();
645 if (status < 0)
646 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (rv & WDIOS_TEMPPANIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 temp_panic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 return -EINVAL;
652
653 case WDIOC_KEEPALIVE:
654 pcwd_keepalive();
655 return 0;
656
657 case WDIOC_SETTIMEOUT:
658 if (get_user(new_heartbeat, argp))
659 return -EFAULT;
660
661 if (pcwd_set_heartbeat(new_heartbeat))
662 return -EINVAL;
663
664 pcwd_keepalive();
665 /* Fall */
666
667 case WDIOC_GETTIMEOUT:
668 return put_user(heartbeat, argp);
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000669
670 default:
671 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
674 return 0;
675}
676
677static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
678 loff_t *ppos)
679{
680 if (len) {
681 if (!nowayout) {
682 size_t i;
683
684 /* In case it was set long ago */
685 expect_close = 0;
686
687 for (i = 0; i != len; i++) {
688 char c;
689
690 if (get_user(c, buf + i))
691 return -EFAULT;
692 if (c == 'V')
693 expect_close = 42;
694 }
695 }
696 pcwd_keepalive();
697 }
698 return len;
699}
700
701static int pcwd_open(struct inode *inode, struct file *file)
702{
Alan Cox261dcc72008-05-19 14:07:43 +0100703 if (test_and_set_bit(0, &open_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (nowayout)
706 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Activate */
708 pcwd_start();
709 pcwd_keepalive();
710 return nonseekable_open(inode, file);
711}
712
713static int pcwd_close(struct inode *inode, struct file *file)
714{
Alan Cox261dcc72008-05-19 14:07:43 +0100715 if (expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 pcwd_stop();
Alan Cox261dcc72008-05-19 14:07:43 +0100717 else {
718 printk(KERN_CRIT PFX
719 "Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 pcwd_keepalive();
721 }
722 expect_close = 0;
Alan Cox261dcc72008-05-19 14:07:43 +0100723 clear_bit(0, &open_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725}
726
727/*
728 * /dev/temperature handling
729 */
730
731static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
732 loff_t *ppos)
733{
734 int temperature;
735
736 if (pcwd_get_temperature(&temperature))
737 return -EFAULT;
738
739 if (copy_to_user(buf, &temperature, 1))
740 return -EFAULT;
741
742 return 1;
743}
744
745static int pcwd_temp_open(struct inode *inode, struct file *file)
746{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100747 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return -ENODEV;
749
750 return nonseekable_open(inode, file);
751}
752
753static int pcwd_temp_close(struct inode *inode, struct file *file)
754{
755 return 0;
756}
757
758/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 * Kernel Interfaces
760 */
761
Arjan van de Ven62322d22006-07-03 00:24:21 -0700762static const struct file_operations pcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 .owner = THIS_MODULE,
764 .llseek = no_llseek,
765 .write = pcwd_write,
Alan Cox261dcc72008-05-19 14:07:43 +0100766 .unlocked_ioctl = pcwd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 .open = pcwd_open,
768 .release = pcwd_close,
769};
770
771static struct miscdevice pcwd_miscdev = {
772 .minor = WATCHDOG_MINOR,
773 .name = "watchdog",
774 .fops = &pcwd_fops,
775};
776
Arjan van de Ven62322d22006-07-03 00:24:21 -0700777static const struct file_operations pcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .owner = THIS_MODULE,
779 .llseek = no_llseek,
780 .read = pcwd_temp_read,
781 .open = pcwd_temp_open,
782 .release = pcwd_temp_close,
783};
784
785static struct miscdevice temp_miscdev = {
786 .minor = TEMP_MINOR,
787 .name = "temperature",
788 .fops = &pcwd_temp_fops,
789};
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/*
792 * Init & exit routines
793 */
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795static inline int get_revision(void)
796{
797 int r = PCWD_REVISION_C;
798
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100799 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* REV A cards use only 2 io ports; test
801 * presumes a floating bus reads as 0xff. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100802 if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
803 (inb(pcwd_private.io_addr + 3) == 0xFF))
Alan Cox261dcc72008-05-19 14:07:43 +0100804 r = PCWD_REVISION_A;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100805 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 return r;
808}
809
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000810/*
811 * The ISA cards have a heartbeat bit in one of the registers, which
812 * register is card dependent. The heartbeat bit is monitored, and if
813 * found, is considered proof that a Berkshire card has been found.
814 * The initial rate is once per second at board start up, then twice
815 * per second for normal operation.
816 */
817static int __devinit pcwd_isa_match(struct device *dev, unsigned int id)
818{
Alan Cox261dcc72008-05-19 14:07:43 +0100819 int base_addr = pcwd_ioports[id];
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000820 int port0, last_port0; /* Reg 0, in case it's REV A */
821 int port1, last_port1; /* Register 1 for REV C cards */
822 int i;
823 int retval;
824
825 if (debug >= DEBUG)
826 printk(KERN_DEBUG PFX "pcwd_isa_match id=%d\n",
827 id);
828
Alan Cox261dcc72008-05-19 14:07:43 +0100829 if (!request_region(base_addr, 4, "PCWD")) {
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000830 printk(KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
831 return 0;
832 }
833
834 retval = 0;
835
836 port0 = inb_p(base_addr); /* For REV A boards */
837 port1 = inb_p(base_addr + 1); /* For REV C boards */
838 if (port0 != 0xff || port1 != 0xff) {
839 /* Not an 'ff' from a floating bus, so must be a card! */
840 for (i = 0; i < 4; ++i) {
841
842 msleep(500);
843
844 last_port0 = port0;
845 last_port1 = port1;
846
847 port0 = inb_p(base_addr);
848 port1 = inb_p(base_addr + 1);
849
850 /* Has either hearbeat bit changed? */
851 if ((port0 ^ last_port0) & WD_HRTBT ||
852 (port1 ^ last_port1) & WD_REVC_HRBT) {
853 retval = 1;
854 break;
855 }
856 }
857 }
Alan Cox261dcc72008-05-19 14:07:43 +0100858 release_region(base_addr, 4);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000859
860 return retval;
861}
862
863static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000867 if (debug >= DEBUG)
868 printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d\n",
869 id);
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 cards_found++;
872 if (cards_found == 1)
Alan Cox261dcc72008-05-19 14:07:43 +0100873 printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n",
874 WD_VER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 if (cards_found > 1) {
877 printk(KERN_ERR PFX "This driver only supports 1 device\n");
878 return -ENODEV;
879 }
880
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000881 if (pcwd_ioports[id] == 0x0000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
883 return -ENODEV;
884 }
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000885 pcwd_private.io_addr = pcwd_ioports[id];
886
887 spin_lock_init(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /* Check card's revision */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100890 pcwd_private.revision = get_revision();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Alan Cox261dcc72008-05-19 14:07:43 +0100892 if (!request_region(pcwd_private.io_addr,
893 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100895 pcwd_private.io_addr);
Alan Cox261dcc72008-05-19 14:07:43 +0100896 ret = -EIO;
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000897 goto error_request_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
900 /* Initial variables */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100901 pcwd_private.supports_temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 temp_panic = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100903 pcwd_private.boot_status = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* get the boot_status */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100906 pcwd_get_status(&pcwd_private.boot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
908 /* clear the "card caused reboot" flag */
909 pcwd_clear_status();
910
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100911 setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 /* Disable the board */
914 pcwd_stop();
915
916 /* Check whether or not the card supports the temperature device */
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100917 pcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100919 /* Show info about the card itself */
920 pcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100922 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
923 if (heartbeat == 0)
924 heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
925
Alan Cox261dcc72008-05-19 14:07:43 +0100926 /* Check that the heartbeat value is within it's range;
927 if not reset to the default */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100928 if (pcwd_set_heartbeat(heartbeat)) {
929 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
Alan Cox261dcc72008-05-19 14:07:43 +0100930 printk(KERN_INFO PFX
931 "heartbeat value must be 2 <= heartbeat <= 7200, using %d\n",
932 WATCHDOG_HEARTBEAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100935 if (pcwd_private.supports_temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ret = misc_register(&temp_miscdev);
937 if (ret) {
Alan Cox261dcc72008-05-19 14:07:43 +0100938 printk(KERN_ERR PFX
939 "cannot register miscdev on minor=%d (err=%d)\n",
940 TEMP_MINOR, ret);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000941 goto error_misc_register_temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 }
944
945 ret = misc_register(&pcwd_miscdev);
946 if (ret) {
Alan Cox261dcc72008-05-19 14:07:43 +0100947 printk(KERN_ERR PFX
948 "cannot register miscdev on minor=%d (err=%d)\n",
949 WATCHDOG_MINOR, ret);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000950 goto error_misc_register_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
953 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
954 heartbeat, nowayout);
955
956 return 0;
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000957
958error_misc_register_watchdog:
959 if (pcwd_private.supports_temp)
960 misc_deregister(&temp_miscdev);
961error_misc_register_temp:
Alan Cox261dcc72008-05-19 14:07:43 +0100962 release_region(pcwd_private.io_addr,
963 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000964error_request_region:
965 pcwd_private.io_addr = 0x0000;
966 cards_found--;
967 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000970static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000972 if (debug >= DEBUG)
973 printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d\n",
974 id);
975
976 if (!pcwd_private.io_addr)
977 return 1;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Disable the board */
980 if (!nowayout)
981 pcwd_stop();
982
983 /* Deregister */
984 misc_deregister(&pcwd_miscdev);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100985 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 misc_deregister(&temp_miscdev);
Alan Cox261dcc72008-05-19 14:07:43 +0100987 release_region(pcwd_private.io_addr,
988 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100989 pcwd_private.io_addr = 0x0000;
Wim Van Sebroeckf1c3a052005-12-10 14:36:24 +0100990 cards_found--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 return 0;
993}
994
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000995static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
996{
997 if (debug >= DEBUG)
998 printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d\n",
999 id);
1000
1001 pcwd_stop();
1002}
1003
1004static struct isa_driver pcwd_isa_driver = {
1005 .match = pcwd_isa_match,
1006 .probe = pcwd_isa_probe,
1007 .remove = __devexit_p(pcwd_isa_remove),
1008 .shutdown = pcwd_isa_shutdown,
1009 .driver = {
1010 .owner = THIS_MODULE,
1011 .name = WATCHDOG_NAME,
1012 },
1013};
1014
1015static int __init pcwd_init_module(void)
1016{
1017 return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
1018}
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020static void __exit pcwd_cleanup_module(void)
1021{
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +00001022 isa_unregister_driver(&pcwd_isa_driver);
Wim Van Sebroeck369fa252006-02-12 17:44:57 +01001023 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026module_init(pcwd_init_module);
1027module_exit(pcwd_cleanup_module);
1028
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +00001029MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, Wim Van Sebroeck <wim@iguana.be>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +00001031MODULE_VERSION(WATCHDOG_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032MODULE_LICENSE("GPL");
1033MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
1034MODULE_ALIAS_MISCDEV(TEMP_MINOR);