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