blob: c6e27e2f0390e6a78f8d1b1295e25d3794279f36 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PC Watchdog Driver
3 * by Ken Hollis (khollis@bitgate.com)
4 *
5 * Permission granted from Simon Machell (73244.1270@compuserve.com)
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 Sebroeckfd41fa62005-12-10 14:22:37 +010052#include <linux/config.h> /* For CONFIG_WATCHDOG_NOWAYOUT/... */
53#include <linux/module.h> /* For module specific items */
54#include <linux/moduleparam.h> /* For new moduleparam's */
55#include <linux/types.h> /* For standard types (like size_t) */
56#include <linux/errno.h> /* For the -ENODEV/... values */
57#include <linux/kernel.h> /* For printk/panic/... */
58#include <linux/delay.h> /* For mdelay function */
59#include <linux/timer.h> /* For timer related operations */
60#include <linux/jiffies.h> /* For jiffies stuff */
61#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
62#include <linux/watchdog.h> /* For the watchdog specific items */
63#include <linux/notifier.h> /* For notifier support */
64#include <linux/reboot.h> /* For reboot_notifier stuff */
65#include <linux/init.h> /* For __init/__exit/... */
66#include <linux/fs.h> /* For file operations */
67#include <linux/ioport.h> /* For io-port access */
68#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Tim Schmielau4e57b682005-10-30 15:03:48 -080069#include <linux/sched.h> /* TASK_INTERRUPTIBLE, set_current_state() and friends */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010070#include <linux/slab.h> /* For kmalloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010072#include <asm/uaccess.h> /* For copy_to_user/put_user/... */
73#include <asm/io.h> /* For inb/outb/... */
74
75/* Module and version information */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +010076#define WATCHDOG_VERSION "1.17"
77#define WATCHDOG_DATE "12 Feb 2006"
Wim Van Sebroecka7122f92006-01-09 22:07:22 +010078#define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
79#define WATCHDOG_NAME "pcwd"
80#define PFX WATCHDOG_NAME ": "
81#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
82#define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * It should be noted that PCWD_REVISION_B was removed because A and B
86 * are essentially the same types of card, with the exception that B
87 * has temperature reporting. Since I didn't receive a Rev.B card,
88 * the Rev.B card is not supported. (It's a good thing too, as they
89 * are no longer in production.)
90 */
91#define PCWD_REVISION_A 1
92#define PCWD_REVISION_C 2
93
94/*
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +010095 * These are the defines that describe the control status bits for the
96 * PCI-PC Watchdog card.
97*/
98/* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +010099#define WD_WDRST 0x01 /* Previously reset state */
100#define WD_T110 0x02 /* Temperature overheat sense */
101#define WD_HRTBT 0x04 /* Heartbeat sense */
102#define WD_RLY2 0x08 /* External relay triggered */
103#define WD_SRLY2 0x80 /* Software external relay triggered */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100104/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100105#define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
106#define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
107#define WD_REVC_TTRP 0x04 /* Temperature Trip status */
108#define WD_REVC_RL2A 0x08 /* Relay 2 activated by on-board processor */
109#define WD_REVC_RL1A 0x10 /* Relay 1 active */
110#define WD_REVC_R2DS 0x40 /* Relay 2 disable */
111#define WD_REVC_RLY2 0x80 /* Relay 2 activated? */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100112/* Port 2 : Control Status #2 */
113#define WD_WDIS 0x10 /* Watchdog Disabled */
114#define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */
115#define WD_SSEL 0x40 /* Watchdog Switch Select (1:SW1 <-> 0:SW2) */
116#define WD_WCMD 0x80 /* Watchdog Command Mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* max. time we give an ISA watchdog card to process a command */
119/* 500ms for each 4 bit response (according to spec.) */
120#define ISA_COMMAND_TIMEOUT 1000
121
122/* Watchdog's internal commands */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100123#define CMD_ISA_IDLE 0x00
124#define CMD_ISA_VERSION_INTEGER 0x01
125#define CMD_ISA_VERSION_TENTH 0x02
126#define CMD_ISA_VERSION_HUNDRETH 0x03
127#define CMD_ISA_VERSION_MINOR 0x04
128#define CMD_ISA_SWITCH_SETTINGS 0x05
129#define CMD_ISA_DELAY_TIME_2SECS 0x0A
130#define CMD_ISA_DELAY_TIME_4SECS 0x0B
131#define CMD_ISA_DELAY_TIME_8SECS 0x0C
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133/*
134 * We are using an kernel timer to do the pinging of the watchdog
135 * every ~500ms. We try to set the internal heartbeat of the
136 * watchdog to 2 ms.
137 */
138
139#define WDT_INTERVAL (HZ/2+1)
140
141/* We can only use 1 card due to the /dev/watchdog restriction */
142static int cards_found;
143
144/* internal variables */
145static atomic_t open_allowed = ATOMIC_INIT(1);
146static char expect_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static int temp_panic;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100148static struct { /* this is private data for each ISA-PC watchdog card */
149 int revision; /* The card's revision */
150 int supports_temp; /* Wether or not the card has a temperature device */
151 int command_mode; /* Wether or not the card is in command mode */
152 int boot_status; /* The card's boot status */
153 int io_addr; /* The cards I/O address */
154 spinlock_t io_lock; /* the lock for io operations */
155 struct timer_list timer; /* The timer that pings the watchdog */
156 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
157} pcwd_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/* module parameters */
160#define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */
161static int heartbeat = WATCHDOG_HEARTBEAT;
162module_param(heartbeat, int, 0);
163MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
164
Andrey Panin4bfdf372005-07-27 11:43:58 -0700165static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166module_param(nowayout, int, 0);
167MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
168
169/*
170 * Internal functions
171 */
172
173static int send_isa_command(int cmd)
174{
175 int i;
176 int control_status;
177 int port0, last_port0; /* Double read for stabilising */
178
179 /* The WCMD bit must be 1 and the command is only 4 bits in size */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100180 control_status = (cmd & 0x0F) | WD_WCMD;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100181 outb_p(control_status, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 udelay(ISA_COMMAND_TIMEOUT);
183
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100184 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 for (i = 0; i < 25; ++i) {
186 last_port0 = port0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100187 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 if (port0 == last_port0)
190 break; /* Data is stable */
191
192 udelay (250);
193 }
194
195 return port0;
196}
197
198static int set_command_mode(void)
199{
200 int i, found=0, count=0;
201
202 /* Set the card into command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100203 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 while ((!found) && (count < 3)) {
205 i = send_isa_command(CMD_ISA_IDLE);
206
207 if (i == 0x00)
208 found = 1;
209 else if (i == 0xF3) {
210 /* Card does not like what we've done to it */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100211 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 udelay(1200); /* Spec says wait 1ms */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100213 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 udelay(ISA_COMMAND_TIMEOUT);
215 }
216 count++;
217 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100218 spin_unlock(&pcwd_private.io_lock);
219 pcwd_private.command_mode = found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 return(found);
222}
223
224static void unset_command_mode(void)
225{
226 /* Set the card into normal mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100227 spin_lock(&pcwd_private.io_lock);
228 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100230 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100232 pcwd_private.command_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100235static inline void pcwd_check_temperature_support(void)
236{
237 if (inb(pcwd_private.io_addr) != 0xF0)
238 pcwd_private.supports_temp = 1;
239}
240
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100241static inline char *get_firmware(void)
242{
243 int one, ten, hund, minor;
244 char *ret;
245
246 ret = kmalloc(6, GFP_KERNEL);
247 if(ret == NULL)
248 return NULL;
249
250 if (set_command_mode()) {
251 one = send_isa_command(CMD_ISA_VERSION_INTEGER);
252 ten = send_isa_command(CMD_ISA_VERSION_TENTH);
253 hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
254 minor = send_isa_command(CMD_ISA_VERSION_MINOR);
255 sprintf(ret, "%c.%c%c%c", one, ten, hund, minor);
256 }
257 else
258 sprintf(ret, "ERROR");
259
260 unset_command_mode();
261 return(ret);
262}
263
264static inline int pcwd_get_option_switches(void)
265{
266 int option_switches=0;
267
268 if (set_command_mode()) {
269 /* Get switch settings */
270 option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
271 }
272
273 unset_command_mode();
274 return(option_switches);
275}
276
277static void pcwd_show_card_info(void)
278{
279 char *firmware;
280 int option_switches;
281
282 /* Get some extra info from the hardware (in command/debug/diag mode) */
283 if (pcwd_private.revision == PCWD_REVISION_A)
284 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
285 else if (pcwd_private.revision == PCWD_REVISION_C) {
286 firmware = get_firmware();
287 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
288 pcwd_private.io_addr, firmware);
289 kfree(firmware);
290 option_switches = pcwd_get_option_switches();
291 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
292 option_switches,
293 ((option_switches & 0x10) ? "ON" : "OFF"),
294 ((option_switches & 0x08) ? "ON" : "OFF"));
295
296 /* Reprogram internal heartbeat to 2 seconds */
297 if (set_command_mode()) {
298 send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
299 unset_command_mode();
300 }
301 }
302
303 if (pcwd_private.supports_temp)
304 printk(KERN_INFO PFX "Temperature Option Detected\n");
305
306 if (pcwd_private.boot_status & WDIOF_CARDRESET)
307 printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
308
309 if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
310 printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
311 printk(KERN_EMERG PFX "CPU Overheat\n");
312 }
313
314 if (pcwd_private.boot_status == 0)
315 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static void pcwd_timer_ping(unsigned long data)
319{
320 int wdrst_stat;
321
322 /* If we got a heartbeat pulse within the WDT_INTERVAL
323 * we agree to ping the WDT */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100324 if(time_before(jiffies, pcwd_private.next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Ping the watchdog */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100326 spin_lock(&pcwd_private.io_lock);
327 if (pcwd_private.revision == PCWD_REVISION_A) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100329 wdrst_stat = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 wdrst_stat &= 0x0F;
331 wdrst_stat |= WD_WDRST;
332
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100333 outb_p(wdrst_stat, pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else {
335 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100336 outb_p(0x00, pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 /* Re-set the timer interval */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100340 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100342 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 } else {
344 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
345 }
346}
347
348static int pcwd_start(void)
349{
350 int stat_reg;
351
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100352 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Start the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100355 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Enable the port */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100358 if (pcwd_private.revision == PCWD_REVISION_C) {
359 spin_lock(&pcwd_private.io_lock);
360 outb_p(0x00, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100362 stat_reg = inb_p(pcwd_private.io_addr + 2);
363 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100364 if (stat_reg & WD_WDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 printk(KERN_INFO PFX "Could not start watchdog\n");
366 return -EIO;
367 }
368 }
369 return 0;
370}
371
372static int pcwd_stop(void)
373{
374 int stat_reg;
375
376 /* Stop the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100377 del_timer(&pcwd_private.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* Disable the board */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100380 if (pcwd_private.revision == PCWD_REVISION_C) {
381 spin_lock(&pcwd_private.io_lock);
382 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100384 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100386 stat_reg = inb_p(pcwd_private.io_addr + 2);
387 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100388 if ((stat_reg & WD_WDIS) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 printk(KERN_INFO PFX "Could not stop watchdog\n");
390 return -EIO;
391 }
392 }
393 return 0;
394}
395
396static int pcwd_keepalive(void)
397{
398 /* user land ping */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100399 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401}
402
403static int pcwd_set_heartbeat(int t)
404{
405 if ((t < 2) || (t > 7200)) /* arbitrary upper limit */
406 return -EINVAL;
407
408 heartbeat = t;
409 return 0;
410}
411
412static int pcwd_get_status(int *status)
413{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100414 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 *status=0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100417 spin_lock(&pcwd_private.io_lock);
418 if (pcwd_private.revision == PCWD_REVISION_A)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Rev A cards return status information from
420 * the base register, which is used for the
421 * temperature in other cards. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100422 control_status = inb(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 else {
424 /* Rev C cards return card status in the base
425 * address + 1 register. And use different bits
426 * to indicate a card initiated reset, and an
427 * over-temperature condition. And the reboot
428 * status can be reset. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100429 control_status = inb(pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100431 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100433 if (pcwd_private.revision == PCWD_REVISION_A) {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100434 if (control_status & WD_WDRST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *status |= WDIOF_CARDRESET;
436
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100437 if (control_status & WD_T110) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *status |= WDIOF_OVERHEAT;
439 if (temp_panic) {
440 printk (KERN_INFO PFX "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600441 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 }
444 } else {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100445 if (control_status & WD_REVC_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *status |= WDIOF_CARDRESET;
447
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100448 if (control_status & WD_REVC_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 *status |= WDIOF_OVERHEAT;
450 if (temp_panic) {
451 printk (KERN_INFO PFX "Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600452 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
455 }
456
457 return 0;
458}
459
460static int pcwd_clear_status(void)
461{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100462 int control_status;
463
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100464 if (pcwd_private.revision == PCWD_REVISION_C) {
465 spin_lock(&pcwd_private.io_lock);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100466
467 control_status = inb_p(pcwd_private.io_addr + 1);
468
469 /* clear reset status & Keep Relay 2 disable state as it is */
470 outb_p((control_status & WD_REVC_R2DS), pcwd_private.io_addr + 1);
471
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100472 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 return 0;
475}
476
477static int pcwd_get_temperature(int *temperature)
478{
479 /* check that port 0 gives temperature info and no command results */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100480 if (pcwd_private.command_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return -1;
482
483 *temperature = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100484 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return -ENODEV;
486
487 /*
488 * Convert celsius to fahrenheit, since this was
489 * the decided 'standard' for this return value.
490 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100491 spin_lock(&pcwd_private.io_lock);
492 *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
493 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 return 0;
496}
497
498/*
499 * /dev/watchdog handling
500 */
501
502static int pcwd_ioctl(struct inode *inode, struct file *file,
503 unsigned int cmd, unsigned long arg)
504{
505 int rv;
506 int status;
507 int temperature;
508 int new_heartbeat;
509 int __user *argp = (int __user *)arg;
510 static struct watchdog_info ident = {
511 .options = WDIOF_OVERHEAT |
512 WDIOF_CARDRESET |
513 WDIOF_KEEPALIVEPING |
514 WDIOF_SETTIMEOUT |
515 WDIOF_MAGICCLOSE,
516 .firmware_version = 1,
517 .identity = "PCWD",
518 };
519
520 switch(cmd) {
521 default:
522 return -ENOIOCTLCMD;
523
524 case WDIOC_GETSUPPORT:
525 if(copy_to_user(argp, &ident, sizeof(ident)))
526 return -EFAULT;
527 return 0;
528
529 case WDIOC_GETSTATUS:
530 pcwd_get_status(&status);
531 return put_user(status, argp);
532
533 case WDIOC_GETBOOTSTATUS:
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100534 return put_user(pcwd_private.boot_status, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 case WDIOC_GETTEMP:
537 if (pcwd_get_temperature(&temperature))
538 return -EFAULT;
539
540 return put_user(temperature, argp);
541
542 case WDIOC_SETOPTIONS:
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100543 if (pcwd_private.revision == PCWD_REVISION_C)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 {
545 if(copy_from_user(&rv, argp, sizeof(int)))
546 return -EFAULT;
547
548 if (rv & WDIOS_DISABLECARD)
549 {
550 return pcwd_stop();
551 }
552
553 if (rv & WDIOS_ENABLECARD)
554 {
555 return pcwd_start();
556 }
557
558 if (rv & WDIOS_TEMPPANIC)
559 {
560 temp_panic = 1;
561 }
562 }
563 return -EINVAL;
564
565 case WDIOC_KEEPALIVE:
566 pcwd_keepalive();
567 return 0;
568
569 case WDIOC_SETTIMEOUT:
570 if (get_user(new_heartbeat, argp))
571 return -EFAULT;
572
573 if (pcwd_set_heartbeat(new_heartbeat))
574 return -EINVAL;
575
576 pcwd_keepalive();
577 /* Fall */
578
579 case WDIOC_GETTIMEOUT:
580 return put_user(heartbeat, argp);
581 }
582
583 return 0;
584}
585
586static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
587 loff_t *ppos)
588{
589 if (len) {
590 if (!nowayout) {
591 size_t i;
592
593 /* In case it was set long ago */
594 expect_close = 0;
595
596 for (i = 0; i != len; i++) {
597 char c;
598
599 if (get_user(c, buf + i))
600 return -EFAULT;
601 if (c == 'V')
602 expect_close = 42;
603 }
604 }
605 pcwd_keepalive();
606 }
607 return len;
608}
609
610static int pcwd_open(struct inode *inode, struct file *file)
611{
612 if (!atomic_dec_and_test(&open_allowed) ) {
613 atomic_inc( &open_allowed );
614 return -EBUSY;
615 }
616
617 if (nowayout)
618 __module_get(THIS_MODULE);
619
620 /* Activate */
621 pcwd_start();
622 pcwd_keepalive();
623 return nonseekable_open(inode, file);
624}
625
626static int pcwd_close(struct inode *inode, struct file *file)
627{
628 if (expect_close == 42) {
629 pcwd_stop();
630 } else {
631 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
632 pcwd_keepalive();
633 }
634 expect_close = 0;
635 atomic_inc( &open_allowed );
636 return 0;
637}
638
639/*
640 * /dev/temperature handling
641 */
642
643static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
644 loff_t *ppos)
645{
646 int temperature;
647
648 if (pcwd_get_temperature(&temperature))
649 return -EFAULT;
650
651 if (copy_to_user(buf, &temperature, 1))
652 return -EFAULT;
653
654 return 1;
655}
656
657static int pcwd_temp_open(struct inode *inode, struct file *file)
658{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100659 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -ENODEV;
661
662 return nonseekable_open(inode, file);
663}
664
665static int pcwd_temp_close(struct inode *inode, struct file *file)
666{
667 return 0;
668}
669
670/*
671 * Notify system
672 */
673
674static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
675{
676 if (code==SYS_DOWN || code==SYS_HALT) {
677 /* Turn the WDT off */
678 pcwd_stop();
679 }
680
681 return NOTIFY_DONE;
682}
683
684/*
685 * Kernel Interfaces
686 */
687
688static struct file_operations pcwd_fops = {
689 .owner = THIS_MODULE,
690 .llseek = no_llseek,
691 .write = pcwd_write,
692 .ioctl = pcwd_ioctl,
693 .open = pcwd_open,
694 .release = pcwd_close,
695};
696
697static struct miscdevice pcwd_miscdev = {
698 .minor = WATCHDOG_MINOR,
699 .name = "watchdog",
700 .fops = &pcwd_fops,
701};
702
703static struct file_operations pcwd_temp_fops = {
704 .owner = THIS_MODULE,
705 .llseek = no_llseek,
706 .read = pcwd_temp_read,
707 .open = pcwd_temp_open,
708 .release = pcwd_temp_close,
709};
710
711static struct miscdevice temp_miscdev = {
712 .minor = TEMP_MINOR,
713 .name = "temperature",
714 .fops = &pcwd_temp_fops,
715};
716
717static struct notifier_block pcwd_notifier = {
718 .notifier_call = pcwd_notify_sys,
719};
720
721/*
722 * Init & exit routines
723 */
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static inline int get_revision(void)
726{
727 int r = PCWD_REVISION_C;
728
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100729 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* REV A cards use only 2 io ports; test
731 * presumes a floating bus reads as 0xff. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100732 if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
733 (inb(pcwd_private.io_addr + 3) == 0xFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 r=PCWD_REVISION_A;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100735 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 return r;
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int __devinit pcwatchdog_init(int base_addr)
741{
742 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 cards_found++;
745 if (cards_found == 1)
746 printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER);
747
748 if (cards_found > 1) {
749 printk(KERN_ERR PFX "This driver only supports 1 device\n");
750 return -ENODEV;
751 }
752
753 if (base_addr == 0x0000) {
754 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
755 return -ENODEV;
756 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100757 pcwd_private.io_addr = base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Check card's revision */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100760 pcwd_private.revision = get_revision();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100762 if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100764 pcwd_private.io_addr);
765 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return -EIO;
767 }
768
769 /* Initial variables */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100770 pcwd_private.supports_temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 temp_panic = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100772 pcwd_private.boot_status = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* get the boot_status */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100775 pcwd_get_status(&pcwd_private.boot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* clear the "card caused reboot" flag */
778 pcwd_clear_status();
779
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100780 init_timer(&pcwd_private.timer);
781 pcwd_private.timer.function = pcwd_timer_ping;
782 pcwd_private.timer.data = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /* Disable the board */
785 pcwd_stop();
786
787 /* Check whether or not the card supports the temperature device */
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100788 pcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100790 /* Show info about the card itself */
791 pcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Check that the heartbeat value is within it's range ; if not reset to the default */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100794 if (pcwd_set_heartbeat(heartbeat)) {
795 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
796 printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n",
797 WATCHDOG_HEARTBEAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
800 ret = register_reboot_notifier(&pcwd_notifier);
801 if (ret) {
802 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
803 ret);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100804 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
805 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return ret;
807 }
808
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100809 if (pcwd_private.supports_temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ret = misc_register(&temp_miscdev);
811 if (ret) {
812 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
813 TEMP_MINOR, ret);
814 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100815 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
816 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return ret;
818 }
819 }
820
821 ret = misc_register(&pcwd_miscdev);
822 if (ret) {
823 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
824 WATCHDOG_MINOR, ret);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100825 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 misc_deregister(&temp_miscdev);
827 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100828 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
829 pcwd_private.io_addr = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return ret;
831 }
832
833 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
834 heartbeat, nowayout);
835
836 return 0;
837}
838
839static void __devexit pcwatchdog_exit(void)
840{
841 /* Disable the board */
842 if (!nowayout)
843 pcwd_stop();
844
845 /* Deregister */
846 misc_deregister(&pcwd_miscdev);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100847 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 misc_deregister(&temp_miscdev);
849 unregister_reboot_notifier(&pcwd_notifier);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100850 release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
851 pcwd_private.io_addr = 0x0000;
Wim Van Sebroeckf1c3a052005-12-10 14:36:24 +0100852 cards_found--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855/*
856 * The ISA cards have a heartbeat bit in one of the registers, which
857 * register is card dependent. The heartbeat bit is monitored, and if
858 * found, is considered proof that a Berkshire card has been found.
859 * The initial rate is once per second at board start up, then twice
860 * per second for normal operation.
861 */
862static int __init pcwd_checkcard(int base_addr)
863{
864 int port0, last_port0; /* Reg 0, in case it's REV A */
865 int port1, last_port1; /* Register 1 for REV C cards */
866 int i;
867 int retval;
868
869 if (!request_region (base_addr, 4, "PCWD")) {
870 printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
871 return 0;
872 }
873
874 retval = 0;
875
876 port0 = inb_p(base_addr); /* For REV A boards */
877 port1 = inb_p(base_addr + 1); /* For REV C boards */
878 if (port0 != 0xff || port1 != 0xff) {
879 /* Not an 'ff' from a floating bus, so must be a card! */
880 for (i = 0; i < 4; ++i) {
881
882 msleep(500);
883
884 last_port0 = port0;
885 last_port1 = port1;
886
887 port0 = inb_p(base_addr);
888 port1 = inb_p(base_addr + 1);
889
890 /* Has either hearbeat bit changed? */
891 if ((port0 ^ last_port0) & WD_HRTBT ||
892 (port1 ^ last_port1) & WD_REVC_HRBT) {
893 retval = 1;
894 break;
895 }
896 }
897 }
898 release_region (base_addr, 4);
899
900 return retval;
901}
902
903/*
904 * These are the auto-probe addresses available.
905 *
906 * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
907 * Revision A has an address range of 2 addresses, while Revision C has 4.
908 */
909static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
910
911static int __init pcwd_init_module(void)
912{
913 int i, found = 0;
914
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100915 spin_lock_init(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 for (i = 0; pcwd_ioports[i] != 0; i++) {
918 if (pcwd_checkcard(pcwd_ioports[i])) {
919 if (!(pcwatchdog_init(pcwd_ioports[i])))
920 found++;
921 }
922 }
923
924 if (!found) {
925 printk (KERN_INFO PFX "No card detected, or port not available\n");
926 return -ENODEV;
927 }
928
929 return 0;
930}
931
932static void __exit pcwd_cleanup_module(void)
933{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100934 if (pcwd_private.io_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 pcwatchdog_exit();
936 return;
937}
938
939module_init(pcwd_init_module);
940module_exit(pcwd_cleanup_module);
941
942MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
943MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
944MODULE_LICENSE("GPL");
945MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
946MODULE_ALIAS_MISCDEV(TEMP_MINOR);