blob: e936f15dc7c7d3b2590478170a648cb2194b8d43 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PC Watchdog Driver
3 * by Ken Hollis (khollis@bitgate.com)
4 *
Wim Van Sebroeckf9146f22007-01-09 22:38:54 +01005 * Permission granted from Simon Machell (smachell@berkprod.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Written for the Linux Kernel, and GPLed by Ken Hollis
7 *
8 * 960107 Added request_region routines, modulized the whole thing.
9 * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added
10 * WD_TIMEOUT define.
11 * 960216 Added eof marker on the file, and changed verbose messages.
12 * 960716 Made functional and cosmetic changes to the source for
13 * inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
14 * 960717 Removed read/seek routines, replaced with ioctl. Also, added
15 * check_region command due to Alan's suggestion.
16 * 960821 Made changes to compile in newer 2.0.x kernels. Added
17 * "cold reboot sense" entry.
18 * 960825 Made a few changes to code, deleted some defines and made
19 * typedefs to replace them. Made heartbeat reset only available
20 * via ioctl, and removed the write routine.
21 * 960828 Added new items for PC Watchdog Rev.C card.
22 * 960829 Changed around all of the IOCTLs, added new features,
23 * added watchdog disable/re-enable routines. Added firmware
24 * version reporting. Added read routine for temperature.
25 * Removed some extra defines, added an autodetect Revision
26 * routine.
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000027 * 961006 Revised some documentation, fixed some cosmetic bugs. Made
28 * drivers to panic the system if it's overheating at bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 961118 Changed some verbiage on some of the output, tidied up
30 * code bits, and added compatibility to 2.1.x.
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000031 * 970912 Enabled board on open and disable on close.
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * 971107 Took account of recent VFS changes (broke read).
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000033 * 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>]
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * 990605 Made changes to code to support Firmware 1.22a, added
40 * fairly useless proc entry.
41 * 990610 removed said useless proc code for the merge <alan>
42 * 000403 Removed last traces of proc code. <davej>
Alan Cox261dcc72008-05-19 14:07:43 +010043 * 011214 Added nowayout module option to override
44 * CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000045 * Added timeout module option to override default
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
47
48/*
49 * A bells and whistles driver is available from http://www.pcwd.de/
Alan Cox261dcc72008-05-19 14:07:43 +010050 * More info available at http://www.berkprod.com/ or
51 * http://www.pcwatchdog.com/
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53
Joe Perches27c766a2012-02-15 15:06:19 -080054#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010056#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 */
Jean Delvare487722c2013-10-21 17:38:49 +020064#include <linux/miscdevice.h> /* For struct miscdevice */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010065#include <linux/watchdog.h> /* For the watchdog specific items */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000066#include <linux/reboot.h> /* For kernel_power_off() */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010067#include <linux/init.h> /* For __init/__exit/... */
68#include <linux/fs.h> /* For file operations */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000069#include <linux/isa.h> /* For isa devices */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010070#include <linux/ioport.h> /* For io-port access */
71#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Alan Cox261dcc72008-05-19 14:07:43 +010072#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
73#include <linux/io.h> /* For inb/outb/... */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +010074
75/* Module and version information */
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000076#define WATCHDOG_VERSION "1.20"
77#define WATCHDOG_DATE "18 Feb 2007"
Wim Van Sebroecka7122f92006-01-09 22:07:22 +010078#define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
79#define WATCHDOG_NAME "pcwd"
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000080#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION "\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * It should be noted that PCWD_REVISION_B was removed because A and B
84 * are essentially the same types of card, with the exception that B
85 * has temperature reporting. Since I didn't receive a Rev.B card,
86 * the Rev.B card is not supported. (It's a good thing too, as they
87 * are no longer in production.)
88 */
89#define PCWD_REVISION_A 1
90#define PCWD_REVISION_C 2
91
92/*
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +000093 * These are the auto-probe addresses available.
94 *
95 * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
96 * Revision A has an address range of 2 addresses, while Revision C has 4.
97 */
98#define PCWD_ISA_NR_CARDS 3
99static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
100
101/*
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100102 * These are the defines that describe the control status bits for the
103 * PCI-PC Watchdog card.
104*/
105/* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100106#define WD_WDRST 0x01 /* Previously reset state */
107#define WD_T110 0x02 /* Temperature overheat sense */
108#define WD_HRTBT 0x04 /* Heartbeat sense */
109#define WD_RLY2 0x08 /* External relay triggered */
110#define WD_SRLY2 0x80 /* Software external relay triggered */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100111/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100112#define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
113#define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
114#define WD_REVC_TTRP 0x04 /* Temperature Trip status */
Alan Cox261dcc72008-05-19 14:07:43 +0100115#define WD_REVC_RL2A 0x08 /* Relay 2 activated by
116 on-board processor */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100117#define WD_REVC_RL1A 0x10 /* Relay 1 active */
118#define WD_REVC_R2DS 0x40 /* Relay 2 disable */
119#define WD_REVC_RLY2 0x80 /* Relay 2 activated? */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100120/* Port 2 : Control Status #2 */
121#define WD_WDIS 0x10 /* Watchdog Disabled */
122#define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */
Alan Cox261dcc72008-05-19 14:07:43 +0100123#define WD_SSEL 0x40 /* Watchdog Switch Select
124 (1:SW1 <-> 0:SW2) */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100125#define WD_WCMD 0x80 /* Watchdog Command Mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* max. time we give an ISA watchdog card to process a command */
128/* 500ms for each 4 bit response (according to spec.) */
129#define ISA_COMMAND_TIMEOUT 1000
130
131/* Watchdog's internal commands */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100132#define CMD_ISA_IDLE 0x00
133#define CMD_ISA_VERSION_INTEGER 0x01
134#define CMD_ISA_VERSION_TENTH 0x02
135#define CMD_ISA_VERSION_HUNDRETH 0x03
136#define CMD_ISA_VERSION_MINOR 0x04
137#define CMD_ISA_SWITCH_SETTINGS 0x05
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100138#define CMD_ISA_RESET_PC 0x06
139#define CMD_ISA_ARM_0 0x07
140#define CMD_ISA_ARM_30 0x08
141#define CMD_ISA_ARM_60 0x09
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100142#define CMD_ISA_DELAY_TIME_2SECS 0x0A
143#define CMD_ISA_DELAY_TIME_4SECS 0x0B
144#define CMD_ISA_DELAY_TIME_8SECS 0x0C
Wim Van Sebroeck369fa252006-02-12 17:44:57 +0100145#define CMD_ISA_RESET_RELAYS 0x0D
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100147/* Watchdog's Dip Switch heartbeat values */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000148static const int heartbeat_tbl[] = {
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100149 20, /* OFF-OFF-OFF = 20 Sec */
150 40, /* OFF-OFF-ON = 40 Sec */
151 60, /* OFF-ON-OFF = 1 Min */
152 300, /* OFF-ON-ON = 5 Min */
153 600, /* ON-OFF-OFF = 10 Min */
154 1800, /* ON-OFF-ON = 30 Min */
155 3600, /* ON-ON-OFF = 1 Hour */
156 7200, /* ON-ON-ON = 2 hour */
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * We are using an kernel timer to do the pinging of the watchdog
161 * every ~500ms. We try to set the internal heartbeat of the
162 * watchdog to 2 ms.
163 */
164
165#define WDT_INTERVAL (HZ/2+1)
166
167/* We can only use 1 card due to the /dev/watchdog restriction */
168static int cards_found;
169
170/* internal variables */
Wim Van Sebroeck36cbaa82008-08-10 21:57:03 +0000171static unsigned long open_allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static char expect_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static int temp_panic;
Alan Cox261dcc72008-05-19 14:07:43 +0100174
175/* this is private data for each ISA-PC watchdog card */
176static struct {
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100177 char fw_ver_str[6]; /* The cards firmware version */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100178 int revision; /* The card's revision */
Alan Cox261dcc72008-05-19 14:07:43 +0100179 int supports_temp; /* Whether or not the card has
180 a temperature device */
181 int command_mode; /* Whether or not the card is in
182 command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100183 int boot_status; /* The card's boot status */
184 int io_addr; /* The cards I/O address */
185 spinlock_t io_lock; /* the lock for io operations */
186 struct timer_list timer; /* The timer that pings the watchdog */
187 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
188} pcwd_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/* module parameters */
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100191#define QUIET 0 /* Default */
192#define VERBOSE 1 /* Verbose */
193#define DEBUG 2 /* print fancy stuff too */
194static int debug = QUIET;
195module_param(debug, int, 0);
Alan Cox261dcc72008-05-19 14:07:43 +0100196MODULE_PARM_DESC(debug,
197 "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100198
Alan Cox261dcc72008-05-19 14:07:43 +0100199/* default heartbeat = delay-time from dip-switches */
200#define WATCHDOG_HEARTBEAT 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int heartbeat = WATCHDOG_HEARTBEAT;
202module_param(heartbeat, int, 0);
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000203MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
204 "(2 <= heartbeat <= 7200 or 0=delay-time from dip-switches, default="
205 __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100207static bool nowayout = WATCHDOG_NOWAYOUT;
208module_param(nowayout, bool, 0);
Alan Cox261dcc72008-05-19 14:07:43 +0100209MODULE_PARM_DESC(nowayout,
210 "Watchdog cannot be stopped once started (default="
211 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
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
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100223 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800224 pr_debug("sending following data cmd=0x%02x\n", cmd);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* The WCMD bit must be 1 and the command is only 4 bits in size */
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100227 control_status = (cmd & 0x0F) | WD_WCMD;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100228 outb_p(control_status, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 udelay(ISA_COMMAND_TIMEOUT);
230
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100231 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 for (i = 0; i < 25; ++i) {
233 last_port0 = port0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100234 port0 = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (port0 == last_port0)
237 break; /* Data is stable */
238
Alan Cox261dcc72008-05-19 14:07:43 +0100239 udelay(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100242 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800243 pr_debug("received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
244 cmd, port0, last_port0);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return port0;
247}
248
249static int set_command_mode(void)
250{
Alan Cox261dcc72008-05-19 14:07:43 +0100251 int i, found = 0, count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* Set the card into command mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100254 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 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 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100262 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 udelay(1200); /* Spec says wait 1ms */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100264 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 udelay(ISA_COMMAND_TIMEOUT);
266 }
267 count++;
268 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100269 spin_unlock(&pcwd_private.io_lock);
270 pcwd_private.command_mode = found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100272 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800273 pr_debug("command_mode=%d\n", pcwd_private.command_mode);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100274
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000275 return found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static void unset_command_mode(void)
279{
280 /* Set the card into normal mode */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100281 spin_lock(&pcwd_private.io_lock);
282 outb_p(0x00, pcwd_private.io_addr + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100284 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100286 pcwd_private.command_mode = 0;
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100287
288 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800289 pr_debug("command_mode=%d\n", pcwd_private.command_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100292static inline void pcwd_check_temperature_support(void)
293{
294 if (inb(pcwd_private.io_addr) != 0xF0)
295 pcwd_private.supports_temp = 1;
296}
297
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100298static inline void pcwd_get_firmware(void)
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100299{
300 int one, ten, hund, minor;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100301
Wim Van Sebroeck6bbc20b2006-03-02 20:05:16 +0100302 strcpy(pcwd_private.fw_ver_str, "ERROR");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100303
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);
Alan Cox261dcc72008-05-19 14:07:43 +0100309 sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
310 one, ten, hund, minor);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100311 }
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100312 unset_command_mode();
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100313
314 return;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100315}
316
317static inline int pcwd_get_option_switches(void)
318{
Alan Cox261dcc72008-05-19 14:07:43 +0100319 int option_switches = 0;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100320
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();
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000327 return option_switches;
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100328}
329
330static void pcwd_show_card_info(void)
331{
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100332 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)
Joe Perches27c766a2012-02-15 15:06:19 -0800336 pr_info("ISA-PC Watchdog (REV.A) detected at port 0x%04x\n",
337 pcwd_private.io_addr);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100338 else if (pcwd_private.revision == PCWD_REVISION_C) {
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100339 pcwd_get_firmware();
Joe Perches27c766a2012-02-15 15:06:19 -0800340 pr_info("ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
Wim Van Sebroeck2891b6a2006-02-12 16:47:34 +0100341 pcwd_private.io_addr, pcwd_private.fw_ver_str);
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100342 option_switches = pcwd_get_option_switches();
Joe Perches27c766a2012-02-15 15:06:19 -0800343 pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100344 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)
Joe Perches27c766a2012-02-15 15:06:19 -0800356 pr_info("Temperature Option Detected\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100357
358 if (pcwd_private.boot_status & WDIOF_CARDRESET)
Joe Perches27c766a2012-02-15 15:06:19 -0800359 pr_info("Previous reboot was caused by the card\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100360
361 if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
Joe Perches27c766a2012-02-15 15:06:19 -0800362 pr_emerg("Card senses a CPU Overheat. Panicking!\n");
363 pr_emerg("CPU Overheat\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100364 }
365
366 if (pcwd_private.boot_status == 0)
Joe Perches27c766a2012-02-15 15:06:19 -0800367 pr_info("No previous trip detected - Cold boot or reset\n");
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static 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 */
Alan Cox261dcc72008-05-19 14:07:43 +0100376 if (time_before(jiffies, pcwd_private.next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Ping the watchdog */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100378 spin_lock(&pcwd_private.io_lock);
379 if (pcwd_private.revision == PCWD_REVISION_A) {
Alan Cox261dcc72008-05-19 14:07:43 +0100380 /* Rev A cards are reset by setting the
381 WD_WDRST bit in register 1 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100382 wdrst_stat = inb_p(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 wdrst_stat &= 0x0F;
384 wdrst_stat |= WD_WDRST;
385
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100386 outb_p(wdrst_stat, pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 } else {
388 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100389 outb_p(0x00, pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 /* Re-set the timer interval */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100393 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100395 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
Joe Perches27c766a2012-02-15 15:06:19 -0800397 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399}
400
401static int pcwd_start(void)
402{
403 int stat_reg;
404
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100405 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* Start the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100408 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Enable the port */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100411 if (pcwd_private.revision == PCWD_REVISION_C) {
412 spin_lock(&pcwd_private.io_lock);
413 outb_p(0x00, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100415 stat_reg = inb_p(pcwd_private.io_addr + 2);
416 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100417 if (stat_reg & WD_WDIS) {
Joe Perches27c766a2012-02-15 15:06:19 -0800418 pr_info("Could not start watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return -EIO;
420 }
421 }
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100422
423 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800424 pr_debug("Watchdog started\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
428
429static int pcwd_stop(void)
430{
431 int stat_reg;
432
433 /* Stop the timer */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100434 del_timer(&pcwd_private.timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /* Disable the board */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100437 if (pcwd_private.revision == PCWD_REVISION_C) {
438 spin_lock(&pcwd_private.io_lock);
439 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100441 outb_p(0xA5, pcwd_private.io_addr + 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 udelay(ISA_COMMAND_TIMEOUT);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100443 stat_reg = inb_p(pcwd_private.io_addr + 2);
444 spin_unlock(&pcwd_private.io_lock);
Wim Van Sebroeck8f0235d2006-01-09 21:56:09 +0100445 if ((stat_reg & WD_WDIS) == 0) {
Joe Perches27c766a2012-02-15 15:06:19 -0800446 pr_info("Could not stop watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return -EIO;
448 }
449 }
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100450
451 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800452 pr_debug("Watchdog stopped\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
457static int pcwd_keepalive(void)
458{
459 /* user land ping */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100460 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100461
462 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800463 pr_debug("Watchdog keepalive signal send\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
467
468static int pcwd_set_heartbeat(int t)
469{
Alan Cox261dcc72008-05-19 14:07:43 +0100470 if (t < 2 || t > 7200) /* arbitrary upper limit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EINVAL;
472
473 heartbeat = t;
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100474
475 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800476 pr_debug("New heartbeat: %d\n", heartbeat);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 0;
479}
480
481static int pcwd_get_status(int *status)
482{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100483 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Alan Cox261dcc72008-05-19 14:07:43 +0100485 *status = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100486 spin_lock(&pcwd_private.io_lock);
487 if (pcwd_private.revision == PCWD_REVISION_A)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* Rev A cards return status information from
489 * the base register, which is used for the
490 * temperature in other cards. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100491 control_status = inb(pcwd_private.io_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 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. */
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100498 control_status = inb(pcwd_private.io_addr + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100500 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100502 if (pcwd_private.revision == PCWD_REVISION_A) {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100503 if (control_status & WD_WDRST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *status |= WDIOF_CARDRESET;
505
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100506 if (control_status & WD_T110) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 *status |= WDIOF_OVERHEAT;
508 if (temp_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -0800509 pr_info("Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600510 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 }
513 } else {
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100514 if (control_status & WD_REVC_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 *status |= WDIOF_CARDRESET;
516
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100517 if (control_status & WD_REVC_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *status |= WDIOF_OVERHEAT;
519 if (temp_panic) {
Joe Perches27c766a2012-02-15 15:06:19 -0800520 pr_info("Temperature overheat trip!\n");
Eric W. Biederman68acc052005-07-26 12:03:08 -0600521 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 }
524 }
525
526 return 0;
527}
528
529static int pcwd_clear_status(void)
530{
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100531 int control_status;
532
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100533 if (pcwd_private.revision == PCWD_REVISION_C) {
534 spin_lock(&pcwd_private.io_lock);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100535
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100536 if (debug >= VERBOSE)
Joe Perches27c766a2012-02-15 15:06:19 -0800537 pr_info("clearing watchdog trip status\n");
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100538
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100539 control_status = inb_p(pcwd_private.io_addr + 1);
540
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100541 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800542 pr_debug("status was: 0x%02x\n", control_status);
543 pr_debug("sending: 0x%02x\n",
544 (control_status & WD_REVC_R2DS));
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100545 }
546
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100547 /* clear reset status & Keep Relay 2 disable state as it is */
Alan Cox261dcc72008-05-19 14:07:43 +0100548 outb_p((control_status & WD_REVC_R2DS),
549 pcwd_private.io_addr + 1);
Wim Van Sebroeck4206f0c2006-02-12 16:37:36 +0100550
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100551 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
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 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100559 if (pcwd_private.command_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return -1;
561
562 *temperature = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100563 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -ENODEV;
565
566 /*
567 * Convert celsius to fahrenheit, since this was
568 * the decided 'standard' for this return value.
569 */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100570 spin_lock(&pcwd_private.io_lock);
571 *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
572 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100574 if (debug >= DEBUG) {
Joe Perches27c766a2012-02-15 15:06:19 -0800575 pr_debug("temperature is: %d F\n", *temperature);
Wim Van Sebroeckc324ab42006-02-12 17:12:55 +0100576 }
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579}
580
581/*
582 * /dev/watchdog handling
583 */
584
Alan Cox261dcc72008-05-19 14:07:43 +0100585static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 int rv;
588 int status;
589 int temperature;
590 int new_heartbeat;
591 int __user *argp = (int __user *)arg;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000592 static const struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 .options = WDIOF_OVERHEAT |
594 WDIOF_CARDRESET |
595 WDIOF_KEEPALIVEPING |
596 WDIOF_SETTIMEOUT |
597 WDIOF_MAGICCLOSE,
598 .firmware_version = 1,
599 .identity = "PCWD",
600 };
601
Alan Cox261dcc72008-05-19 14:07:43 +0100602 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 case WDIOC_GETSUPPORT:
Alan Cox261dcc72008-05-19 14:07:43 +0100604 if (copy_to_user(argp, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 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:
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100613 return put_user(pcwd_private.boot_status, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
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:
Alan Cox261dcc72008-05-19 14:07:43 +0100622 if (pcwd_private.revision == PCWD_REVISION_C) {
623 if (get_user(rv, argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -EFAULT;
625
Alan Cox261dcc72008-05-19 14:07:43 +0100626 if (rv & WDIOS_DISABLECARD) {
627 status = pcwd_stop();
628 if (status < 0)
629 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Alan Cox261dcc72008-05-19 14:07:43 +0100631 if (rv & WDIOS_ENABLECARD) {
632 status = pcwd_start();
633 if (status < 0)
634 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (rv & WDIOS_TEMPPANIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 temp_panic = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
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);
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000657
658 default:
659 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
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{
Alan Cox261dcc72008-05-19 14:07:43 +0100691 if (test_and_set_bit(0, &open_allowed))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (nowayout)
694 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* 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{
Alan Cox261dcc72008-05-19 14:07:43 +0100703 if (expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 pcwd_stop();
Alan Cox261dcc72008-05-19 14:07:43 +0100705 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800706 pr_crit("Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 pcwd_keepalive();
708 }
709 expect_close = 0;
Alan Cox261dcc72008-05-19 14:07:43 +0100710 clear_bit(0, &open_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 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{
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100734 if (!pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 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
745/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * Kernel Interfaces
747 */
748
Arjan van de Ven62322d22006-07-03 00:24:21 -0700749static const struct file_operations pcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 .owner = THIS_MODULE,
751 .llseek = no_llseek,
752 .write = pcwd_write,
Alan Cox261dcc72008-05-19 14:07:43 +0100753 .unlocked_ioctl = pcwd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 .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
Arjan van de Ven62322d22006-07-03 00:24:21 -0700764static const struct file_operations pcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/*
779 * Init & exit routines
780 */
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static inline int get_revision(void)
783{
784 int r = PCWD_REVISION_C;
785
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100786 spin_lock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /* REV A cards use only 2 io ports; test
788 * presumes a floating bus reads as 0xff. */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100789 if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
790 (inb(pcwd_private.io_addr + 3) == 0xFF))
Alan Cox261dcc72008-05-19 14:07:43 +0100791 r = PCWD_REVISION_A;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100792 spin_unlock(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 return r;
795}
796
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000797/*
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 */
Bill Pemberton2d991a12012-11-19 13:21:41 -0500804static int pcwd_isa_match(struct device *dev, unsigned int id)
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000805{
Alan Cox261dcc72008-05-19 14:07:43 +0100806 int base_addr = pcwd_ioports[id];
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000807 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)
Joe Perches27c766a2012-02-15 15:06:19 -0800813 pr_debug("pcwd_isa_match id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000814
Alan Cox261dcc72008-05-19 14:07:43 +0100815 if (!request_region(base_addr, 4, "PCWD")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800816 pr_info("Port 0x%04x unavailable\n", base_addr);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000817 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 }
Alan Cox261dcc72008-05-19 14:07:43 +0100844 release_region(base_addr, 4);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000845
846 return retval;
847}
848
Bill Pemberton2d991a12012-11-19 13:21:41 -0500849static int pcwd_isa_probe(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000853 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800854 pr_debug("pcwd_isa_probe id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 cards_found++;
857 if (cards_found == 1)
Joe Perches27c766a2012-02-15 15:06:19 -0800858 pr_info("v%s Ken Hollis (kenji@bitgate.com)\n",
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000859 WATCHDOG_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (cards_found > 1) {
Joe Perches27c766a2012-02-15 15:06:19 -0800862 pr_err("This driver only supports 1 device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return -ENODEV;
864 }
865
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000866 if (pcwd_ioports[id] == 0x0000) {
Joe Perches27c766a2012-02-15 15:06:19 -0800867 pr_err("No I/O-Address for card detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return -ENODEV;
869 }
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000870 pcwd_private.io_addr = pcwd_ioports[id];
871
872 spin_lock_init(&pcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* Check card's revision */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100875 pcwd_private.revision = get_revision();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Alan Cox261dcc72008-05-19 14:07:43 +0100877 if (!request_region(pcwd_private.io_addr,
878 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
Joe Perches27c766a2012-02-15 15:06:19 -0800879 pr_err("I/O address 0x%04x already in use\n",
880 pcwd_private.io_addr);
Alan Cox261dcc72008-05-19 14:07:43 +0100881 ret = -EIO;
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000882 goto error_request_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884
885 /* Initial variables */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100886 pcwd_private.supports_temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 temp_panic = 0;
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100888 pcwd_private.boot_status = 0x0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* get the boot_status */
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100891 pcwd_get_status(&pcwd_private.boot_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* clear the "card caused reboot" flag */
894 pcwd_clear_status();
895
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100896 setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* Disable the board */
899 pcwd_stop();
900
901 /* Check whether or not the card supports the temperature device */
Wim Van Sebroeck85875212006-01-09 21:59:39 +0100902 pcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Wim Van Sebroeckaf3b38d2006-01-09 22:03:41 +0100904 /* Show info about the card itself */
905 pcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Wim Van Sebroeckf3dc0732007-01-09 22:43:49 +0100907 /* 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
Alan Cox261dcc72008-05-19 14:07:43 +0100911 /* Check that the heartbeat value is within it's range;
912 if not reset to the default */
Wim Van Sebroeckfd41fa62005-12-10 14:22:37 +0100913 if (pcwd_set_heartbeat(heartbeat)) {
914 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
Joe Perches27c766a2012-02-15 15:06:19 -0800915 pr_info("heartbeat value must be 2 <= heartbeat <= 7200, using %d\n",
916 WATCHDOG_HEARTBEAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100919 if (pcwd_private.supports_temp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 ret = misc_register(&temp_miscdev);
921 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800922 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
923 TEMP_MINOR, ret);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000924 goto error_misc_register_temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 }
927
928 ret = misc_register(&pcwd_miscdev);
929 if (ret) {
Joe Perches27c766a2012-02-15 15:06:19 -0800930 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
931 WATCHDOG_MINOR, ret);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000932 goto error_misc_register_watchdog;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
Joe Perches27c766a2012-02-15 15:06:19 -0800935 pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 heartbeat, nowayout);
937
938 return 0;
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000939
940error_misc_register_watchdog:
941 if (pcwd_private.supports_temp)
942 misc_deregister(&temp_miscdev);
943error_misc_register_temp:
Alan Cox261dcc72008-05-19 14:07:43 +0100944 release_region(pcwd_private.io_addr,
945 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000946error_request_region:
947 pcwd_private.io_addr = 0x0000;
948 cards_found--;
949 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
Bill Pemberton4b12b892012-11-19 13:26:24 -0500952static int pcwd_isa_remove(struct device *dev, unsigned int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000954 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800955 pr_debug("pcwd_isa_remove id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000956
957 if (!pcwd_private.io_addr)
958 return 1;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 /* Disable the board */
961 if (!nowayout)
962 pcwd_stop();
963
964 /* Deregister */
965 misc_deregister(&pcwd_miscdev);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100966 if (pcwd_private.supports_temp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 misc_deregister(&temp_miscdev);
Alan Cox261dcc72008-05-19 14:07:43 +0100968 release_region(pcwd_private.io_addr,
969 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
Wim Van Sebroecka2be8782006-01-09 21:53:33 +0100970 pcwd_private.io_addr = 0x0000;
Wim Van Sebroeckf1c3a052005-12-10 14:36:24 +0100971 cards_found--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 return 0;
974}
975
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000976static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
977{
978 if (debug >= DEBUG)
Joe Perches27c766a2012-02-15 15:06:19 -0800979 pr_debug("pcwd_isa_shutdown id=%d\n", id);
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000980
981 pcwd_stop();
982}
983
984static struct isa_driver pcwd_isa_driver = {
985 .match = pcwd_isa_match,
986 .probe = pcwd_isa_probe,
Bill Pemberton82268712012-11-19 13:21:12 -0500987 .remove = pcwd_isa_remove,
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +0000988 .shutdown = pcwd_isa_shutdown,
989 .driver = {
990 .owner = THIS_MODULE,
991 .name = WATCHDOG_NAME,
992 },
993};
994
995static int __init pcwd_init_module(void)
996{
997 return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000static void __exit pcwd_cleanup_module(void)
1001{
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +00001002 isa_unregister_driver(&pcwd_isa_driver);
Joe Perches27c766a2012-02-15 15:06:19 -08001003 pr_info("Watchdog Module Unloaded\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006module_init(pcwd_init_module);
1007module_exit(pcwd_cleanup_module);
1008
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00001009MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, "
1010 "Wim Van Sebroeck <wim@iguana.be>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
Wim Van Sebroeck5aa15052007-05-05 05:48:23 +00001012MODULE_VERSION(WATCHDOG_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013MODULE_LICENSE("GPL");