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