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