xref: /OK3568_Linux_fs/kernel/drivers/watchdog/pcwd_pci.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	Berkshire PCI-PC Watchdog Card Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	(c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *	Based on source code of the following authors:
8*4882a593Smuzhiyun  *	  Ken Hollis <kenji@bitgate.com>,
9*4882a593Smuzhiyun  *	  Lindsay Harris <lindsay@bluegum.com>,
10*4882a593Smuzhiyun  *	  Alan Cox <alan@lxorguk.ukuu.org.uk>,
11*4882a593Smuzhiyun  *	  Matt Domsch <Matt_Domsch@dell.com>,
12*4882a593Smuzhiyun  *	  Rob Radez <rob@osinvestor.com>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *	Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
15*4882a593Smuzhiyun  *	provide warranty for any of this software. This material is
16*4882a593Smuzhiyun  *	provided "AS-IS" and at no charge.
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun  *	A bells and whistles driver is available from:
21*4882a593Smuzhiyun  *	http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  *	More info available at
24*4882a593Smuzhiyun  *	http://www.berkprod.com/ or http://www.pcwatchdog.com/
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun  *	Includes, defines, variables, module parameters, ...
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <linux/module.h>	/* For module specific items */
34*4882a593Smuzhiyun #include <linux/moduleparam.h>	/* For new moduleparam's */
35*4882a593Smuzhiyun #include <linux/types.h>	/* For standard types (like size_t) */
36*4882a593Smuzhiyun #include <linux/errno.h>	/* For the -ENODEV/... values */
37*4882a593Smuzhiyun #include <linux/kernel.h>	/* For printk/panic/... */
38*4882a593Smuzhiyun #include <linux/delay.h>	/* For mdelay function */
39*4882a593Smuzhiyun #include <linux/miscdevice.h>	/* For struct miscdevice */
40*4882a593Smuzhiyun #include <linux/watchdog.h>	/* For the watchdog specific items */
41*4882a593Smuzhiyun #include <linux/notifier.h>	/* For notifier support */
42*4882a593Smuzhiyun #include <linux/reboot.h>	/* For reboot_notifier stuff */
43*4882a593Smuzhiyun #include <linux/init.h>		/* For __init/__exit/... */
44*4882a593Smuzhiyun #include <linux/fs.h>		/* For file operations */
45*4882a593Smuzhiyun #include <linux/pci.h>		/* For pci functions */
46*4882a593Smuzhiyun #include <linux/ioport.h>	/* For io-port access */
47*4882a593Smuzhiyun #include <linux/spinlock.h>	/* For spin_lock/spin_unlock/... */
48*4882a593Smuzhiyun #include <linux/uaccess.h>	/* For copy_to_user/put_user/... */
49*4882a593Smuzhiyun #include <linux/io.h>		/* For inb/outb/... */
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* Module and version information */
52*4882a593Smuzhiyun #define WATCHDOG_VERSION "1.03"
53*4882a593Smuzhiyun #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
54*4882a593Smuzhiyun #define WATCHDOG_NAME "pcwd_pci"
55*4882a593Smuzhiyun #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /* Stuff for the PCI ID's  */
58*4882a593Smuzhiyun #ifndef PCI_VENDOR_ID_QUICKLOGIC
59*4882a593Smuzhiyun #define PCI_VENDOR_ID_QUICKLOGIC    0x11e3
60*4882a593Smuzhiyun #endif
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
63*4882a593Smuzhiyun #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun  * These are the defines that describe the control status bits for the
68*4882a593Smuzhiyun  * PCI-PC Watchdog card.
69*4882a593Smuzhiyun  */
70*4882a593Smuzhiyun /* Port 1 : Control Status #1 */
71*4882a593Smuzhiyun #define WD_PCI_WTRP		0x01	/* Watchdog Trip status */
72*4882a593Smuzhiyun #define WD_PCI_HRBT		0x02	/* Watchdog Heartbeat */
73*4882a593Smuzhiyun #define WD_PCI_TTRP		0x04	/* Temperature Trip status */
74*4882a593Smuzhiyun #define WD_PCI_RL2A		0x08	/* Relay 2 Active */
75*4882a593Smuzhiyun #define WD_PCI_RL1A		0x10	/* Relay 1 Active */
76*4882a593Smuzhiyun #define WD_PCI_R2DS		0x40	/* Relay 2 Disable Temperature-trip /
77*4882a593Smuzhiyun 									reset */
78*4882a593Smuzhiyun #define WD_PCI_RLY2		0x80	/* Activate Relay 2 on the board */
79*4882a593Smuzhiyun /* Port 2 : Control Status #2 */
80*4882a593Smuzhiyun #define WD_PCI_WDIS		0x10	/* Watchdog Disable */
81*4882a593Smuzhiyun #define WD_PCI_ENTP		0x20	/* Enable Temperature Trip Reset */
82*4882a593Smuzhiyun #define WD_PCI_WRSP		0x40	/* Watchdog wrote response */
83*4882a593Smuzhiyun #define WD_PCI_PCMD		0x80	/* PC has sent command */
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /* according to documentation max. time to process a command for the pci
86*4882a593Smuzhiyun  * watchdog card is 100 ms, so we give it 150 ms to do it's job */
87*4882a593Smuzhiyun #define PCI_COMMAND_TIMEOUT	150
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /* Watchdog's internal commands */
90*4882a593Smuzhiyun #define CMD_GET_STATUS				0x04
91*4882a593Smuzhiyun #define CMD_GET_FIRMWARE_VERSION		0x08
92*4882a593Smuzhiyun #define CMD_READ_WATCHDOG_TIMEOUT		0x18
93*4882a593Smuzhiyun #define CMD_WRITE_WATCHDOG_TIMEOUT		0x19
94*4882a593Smuzhiyun #define CMD_GET_CLEAR_RESET_COUNT		0x84
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /* Watchdog's Dip Switch heartbeat values */
97*4882a593Smuzhiyun static const int heartbeat_tbl[] = {
98*4882a593Smuzhiyun 	5,	/* OFF-OFF-OFF	=  5 Sec  */
99*4882a593Smuzhiyun 	10,	/* OFF-OFF-ON	= 10 Sec  */
100*4882a593Smuzhiyun 	30,	/* OFF-ON-OFF	= 30 Sec  */
101*4882a593Smuzhiyun 	60,	/* OFF-ON-ON	=  1 Min  */
102*4882a593Smuzhiyun 	300,	/* ON-OFF-OFF	=  5 Min  */
103*4882a593Smuzhiyun 	600,	/* ON-OFF-ON	= 10 Min  */
104*4882a593Smuzhiyun 	1800,	/* ON-ON-OFF	= 30 Min  */
105*4882a593Smuzhiyun 	3600,	/* ON-ON-ON	=  1 hour */
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /* We can only use 1 card due to the /dev/watchdog restriction */
109*4882a593Smuzhiyun static int cards_found;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /* internal variables */
112*4882a593Smuzhiyun static int temp_panic;
113*4882a593Smuzhiyun static unsigned long is_active;
114*4882a593Smuzhiyun static char expect_release;
115*4882a593Smuzhiyun /* this is private data for each PCI-PC watchdog card */
116*4882a593Smuzhiyun static struct {
117*4882a593Smuzhiyun 	/* Wether or not the card has a temperature device */
118*4882a593Smuzhiyun 	int supports_temp;
119*4882a593Smuzhiyun 	/* The card's boot status */
120*4882a593Smuzhiyun 	int boot_status;
121*4882a593Smuzhiyun 	/* The cards I/O address */
122*4882a593Smuzhiyun 	unsigned long io_addr;
123*4882a593Smuzhiyun 	/* the lock for io operations */
124*4882a593Smuzhiyun 	spinlock_t io_lock;
125*4882a593Smuzhiyun 	/* the PCI-device */
126*4882a593Smuzhiyun 	struct pci_dev *pdev;
127*4882a593Smuzhiyun } pcipcwd_private;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /* module parameters */
130*4882a593Smuzhiyun #define QUIET	0	/* Default */
131*4882a593Smuzhiyun #define VERBOSE	1	/* Verbose */
132*4882a593Smuzhiyun #define DEBUG	2	/* print fancy stuff too */
133*4882a593Smuzhiyun static int debug = QUIET;
134*4882a593Smuzhiyun module_param(debug, int, 0);
135*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #define WATCHDOG_HEARTBEAT 0	/* default heartbeat =
138*4882a593Smuzhiyun 						delay-time from dip-switches */
139*4882a593Smuzhiyun static int heartbeat = WATCHDOG_HEARTBEAT;
140*4882a593Smuzhiyun module_param(heartbeat, int, 0);
141*4882a593Smuzhiyun MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
142*4882a593Smuzhiyun 	"(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
143*4882a593Smuzhiyun 				__MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
146*4882a593Smuzhiyun module_param(nowayout, bool, 0);
147*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
148*4882a593Smuzhiyun 					__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /*
151*4882a593Smuzhiyun  *	Internal functions
152*4882a593Smuzhiyun  */
153*4882a593Smuzhiyun 
send_command(int cmd,int * msb,int * lsb)154*4882a593Smuzhiyun static int send_command(int cmd, int *msb, int *lsb)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun 	int got_response, count;
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	if (debug >= DEBUG)
159*4882a593Smuzhiyun 		pr_debug("sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
160*4882a593Smuzhiyun 			 cmd, *msb, *lsb);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	spin_lock(&pcipcwd_private.io_lock);
163*4882a593Smuzhiyun 	/* If a command requires data it should be written first.
164*4882a593Smuzhiyun 	 * Data for commands with 8 bits of data should be written to port 4.
165*4882a593Smuzhiyun 	 * Commands with 16 bits of data, should be written as LSB to port 4
166*4882a593Smuzhiyun 	 * and MSB to port 5.
167*4882a593Smuzhiyun 	 * After the required data has been written then write the command to
168*4882a593Smuzhiyun 	 * port 6. */
169*4882a593Smuzhiyun 	outb_p(*lsb, pcipcwd_private.io_addr + 4);
170*4882a593Smuzhiyun 	outb_p(*msb, pcipcwd_private.io_addr + 5);
171*4882a593Smuzhiyun 	outb_p(cmd, pcipcwd_private.io_addr + 6);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	/* wait till the pci card processed the command, signaled by
174*4882a593Smuzhiyun 	 * the WRSP bit in port 2 and give it a max. timeout of
175*4882a593Smuzhiyun 	 * PCI_COMMAND_TIMEOUT to process */
176*4882a593Smuzhiyun 	got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
177*4882a593Smuzhiyun 	for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
178*4882a593Smuzhiyun 								count++) {
179*4882a593Smuzhiyun 		mdelay(1);
180*4882a593Smuzhiyun 		got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (debug >= DEBUG) {
184*4882a593Smuzhiyun 		if (got_response) {
185*4882a593Smuzhiyun 			pr_debug("time to process command was: %d ms\n",
186*4882a593Smuzhiyun 				 count);
187*4882a593Smuzhiyun 		} else {
188*4882a593Smuzhiyun 			pr_debug("card did not respond on command!\n");
189*4882a593Smuzhiyun 		}
190*4882a593Smuzhiyun 	}
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	if (got_response) {
193*4882a593Smuzhiyun 		/* read back response */
194*4882a593Smuzhiyun 		*lsb = inb_p(pcipcwd_private.io_addr + 4);
195*4882a593Smuzhiyun 		*msb = inb_p(pcipcwd_private.io_addr + 5);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 		/* clear WRSP bit */
198*4882a593Smuzhiyun 		inb_p(pcipcwd_private.io_addr + 6);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		if (debug >= DEBUG)
201*4882a593Smuzhiyun 			pr_debug("received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
202*4882a593Smuzhiyun 				 cmd, *msb, *lsb);
203*4882a593Smuzhiyun 	}
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	spin_unlock(&pcipcwd_private.io_lock);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	return got_response;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
pcipcwd_check_temperature_support(void)210*4882a593Smuzhiyun static inline void pcipcwd_check_temperature_support(void)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	if (inb_p(pcipcwd_private.io_addr) != 0xF0)
213*4882a593Smuzhiyun 		pcipcwd_private.supports_temp = 1;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
pcipcwd_get_option_switches(void)216*4882a593Smuzhiyun static int pcipcwd_get_option_switches(void)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	int option_switches;
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	option_switches = inb_p(pcipcwd_private.io_addr + 3);
221*4882a593Smuzhiyun 	return option_switches;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
pcipcwd_show_card_info(void)224*4882a593Smuzhiyun static void pcipcwd_show_card_info(void)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	int got_fw_rev, fw_rev_major, fw_rev_minor;
227*4882a593Smuzhiyun 	char fw_ver_str[20];		/* The cards firmware version */
228*4882a593Smuzhiyun 	int option_switches;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major,
231*4882a593Smuzhiyun 								&fw_rev_minor);
232*4882a593Smuzhiyun 	if (got_fw_rev)
233*4882a593Smuzhiyun 		sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
234*4882a593Smuzhiyun 	else
235*4882a593Smuzhiyun 		sprintf(fw_ver_str, "<card no answer>");
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	/* Get switch settings */
238*4882a593Smuzhiyun 	option_switches = pcipcwd_get_option_switches();
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	pr_info("Found card at port 0x%04x (Firmware: %s) %s temp option\n",
241*4882a593Smuzhiyun 		(int) pcipcwd_private.io_addr, fw_ver_str,
242*4882a593Smuzhiyun 		(pcipcwd_private.supports_temp ? "with" : "without"));
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
245*4882a593Smuzhiyun 		option_switches,
246*4882a593Smuzhiyun 		((option_switches & 0x10) ? "ON" : "OFF"),
247*4882a593Smuzhiyun 		((option_switches & 0x08) ? "ON" : "OFF"));
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
250*4882a593Smuzhiyun 		pr_info("Previous reset was caused by the Watchdog card\n");
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
253*4882a593Smuzhiyun 		pr_info("Card sensed a CPU Overheat\n");
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (pcipcwd_private.boot_status == 0)
256*4882a593Smuzhiyun 		pr_info("No previous trip detected - Cold boot or reset\n");
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
pcipcwd_start(void)259*4882a593Smuzhiyun static int pcipcwd_start(void)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	int stat_reg;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	spin_lock(&pcipcwd_private.io_lock);
264*4882a593Smuzhiyun 	outb_p(0x00, pcipcwd_private.io_addr + 3);
265*4882a593Smuzhiyun 	udelay(1000);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	stat_reg = inb_p(pcipcwd_private.io_addr + 2);
268*4882a593Smuzhiyun 	spin_unlock(&pcipcwd_private.io_lock);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	if (stat_reg & WD_PCI_WDIS) {
271*4882a593Smuzhiyun 		pr_err("Card timer not enabled\n");
272*4882a593Smuzhiyun 		return -1;
273*4882a593Smuzhiyun 	}
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (debug >= VERBOSE)
276*4882a593Smuzhiyun 		pr_debug("Watchdog started\n");
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	return 0;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
pcipcwd_stop(void)281*4882a593Smuzhiyun static int pcipcwd_stop(void)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	int stat_reg;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	spin_lock(&pcipcwd_private.io_lock);
286*4882a593Smuzhiyun 	outb_p(0xA5, pcipcwd_private.io_addr + 3);
287*4882a593Smuzhiyun 	udelay(1000);
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	outb_p(0xA5, pcipcwd_private.io_addr + 3);
290*4882a593Smuzhiyun 	udelay(1000);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	stat_reg = inb_p(pcipcwd_private.io_addr + 2);
293*4882a593Smuzhiyun 	spin_unlock(&pcipcwd_private.io_lock);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (!(stat_reg & WD_PCI_WDIS)) {
296*4882a593Smuzhiyun 		pr_err("Card did not acknowledge disable attempt\n");
297*4882a593Smuzhiyun 		return -1;
298*4882a593Smuzhiyun 	}
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	if (debug >= VERBOSE)
301*4882a593Smuzhiyun 		pr_debug("Watchdog stopped\n");
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return 0;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
pcipcwd_keepalive(void)306*4882a593Smuzhiyun static int pcipcwd_keepalive(void)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun 	/* Re-trigger watchdog by writing to port 0 */
309*4882a593Smuzhiyun 	spin_lock(&pcipcwd_private.io_lock);
310*4882a593Smuzhiyun 	outb_p(0x42, pcipcwd_private.io_addr);	/* send out any data */
311*4882a593Smuzhiyun 	spin_unlock(&pcipcwd_private.io_lock);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (debug >= DEBUG)
314*4882a593Smuzhiyun 		pr_debug("Watchdog keepalive signal send\n");
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	return 0;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
pcipcwd_set_heartbeat(int t)319*4882a593Smuzhiyun static int pcipcwd_set_heartbeat(int t)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	int t_msb = t / 256;
322*4882a593Smuzhiyun 	int t_lsb = t % 256;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if ((t < 0x0001) || (t > 0xFFFF))
325*4882a593Smuzhiyun 		return -EINVAL;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/* Write new heartbeat to watchdog */
328*4882a593Smuzhiyun 	send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	heartbeat = t;
331*4882a593Smuzhiyun 	if (debug >= VERBOSE)
332*4882a593Smuzhiyun 		pr_debug("New heartbeat: %d\n", heartbeat);
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	return 0;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun 
pcipcwd_get_status(int * status)337*4882a593Smuzhiyun static int pcipcwd_get_status(int *status)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	int control_status;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	*status = 0;
342*4882a593Smuzhiyun 	control_status = inb_p(pcipcwd_private.io_addr + 1);
343*4882a593Smuzhiyun 	if (control_status & WD_PCI_WTRP)
344*4882a593Smuzhiyun 		*status |= WDIOF_CARDRESET;
345*4882a593Smuzhiyun 	if (control_status & WD_PCI_TTRP) {
346*4882a593Smuzhiyun 		*status |= WDIOF_OVERHEAT;
347*4882a593Smuzhiyun 		if (temp_panic)
348*4882a593Smuzhiyun 			panic(KBUILD_MODNAME ": Temperature overheat trip!\n");
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	if (debug >= DEBUG)
352*4882a593Smuzhiyun 		pr_debug("Control Status #1: 0x%02x\n", control_status);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	return 0;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
pcipcwd_clear_status(void)357*4882a593Smuzhiyun static int pcipcwd_clear_status(void)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	int control_status;
360*4882a593Smuzhiyun 	int msb;
361*4882a593Smuzhiyun 	int reset_counter;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	if (debug >= VERBOSE)
364*4882a593Smuzhiyun 		pr_info("clearing watchdog trip status & LED\n");
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	control_status = inb_p(pcipcwd_private.io_addr + 1);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (debug >= DEBUG) {
369*4882a593Smuzhiyun 		pr_debug("status was: 0x%02x\n", control_status);
370*4882a593Smuzhiyun 		pr_debug("sending: 0x%02x\n",
371*4882a593Smuzhiyun 			 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	/* clear trip status & LED and keep mode of relay 2 */
375*4882a593Smuzhiyun 	outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP,
376*4882a593Smuzhiyun 						pcipcwd_private.io_addr + 1);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	/* clear reset counter */
379*4882a593Smuzhiyun 	msb = 0;
380*4882a593Smuzhiyun 	reset_counter = 0xff;
381*4882a593Smuzhiyun 	send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	if (debug >= DEBUG) {
384*4882a593Smuzhiyun 		pr_debug("reset count was: 0x%02x\n", reset_counter);
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	return 0;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
pcipcwd_get_temperature(int * temperature)390*4882a593Smuzhiyun static int pcipcwd_get_temperature(int *temperature)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	*temperature = 0;
393*4882a593Smuzhiyun 	if (!pcipcwd_private.supports_temp)
394*4882a593Smuzhiyun 		return -ENODEV;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	spin_lock(&pcipcwd_private.io_lock);
397*4882a593Smuzhiyun 	*temperature = inb_p(pcipcwd_private.io_addr);
398*4882a593Smuzhiyun 	spin_unlock(&pcipcwd_private.io_lock);
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/*
401*4882a593Smuzhiyun 	 * Convert celsius to fahrenheit, since this was
402*4882a593Smuzhiyun 	 * the decided 'standard' for this return value.
403*4882a593Smuzhiyun 	 */
404*4882a593Smuzhiyun 	*temperature = (*temperature * 9 / 5) + 32;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	if (debug >= DEBUG) {
407*4882a593Smuzhiyun 		pr_debug("temperature is: %d F\n", *temperature);
408*4882a593Smuzhiyun 	}
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	return 0;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
pcipcwd_get_timeleft(int * time_left)413*4882a593Smuzhiyun static int pcipcwd_get_timeleft(int *time_left)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	int msb;
416*4882a593Smuzhiyun 	int lsb;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	/* Read the time that's left before rebooting */
419*4882a593Smuzhiyun 	/* Note: if the board is not yet armed then we will read 0xFFFF */
420*4882a593Smuzhiyun 	send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	*time_left = (msb << 8) + lsb;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	if (debug >= VERBOSE)
425*4882a593Smuzhiyun 		pr_debug("Time left before next reboot: %d\n", *time_left);
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	return 0;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun /*
431*4882a593Smuzhiyun  *	/dev/watchdog handling
432*4882a593Smuzhiyun  */
433*4882a593Smuzhiyun 
pcipcwd_write(struct file * file,const char __user * data,size_t len,loff_t * ppos)434*4882a593Smuzhiyun static ssize_t pcipcwd_write(struct file *file, const char __user *data,
435*4882a593Smuzhiyun 			     size_t len, loff_t *ppos)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	/* See if we got the magic character 'V' and reload the timer */
438*4882a593Smuzhiyun 	if (len) {
439*4882a593Smuzhiyun 		if (!nowayout) {
440*4882a593Smuzhiyun 			size_t i;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 			/* note: just in case someone wrote the magic character
443*4882a593Smuzhiyun 			 * five months ago... */
444*4882a593Smuzhiyun 			expect_release = 0;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 			/* scan to see whether or not we got the
447*4882a593Smuzhiyun 			 * magic character */
448*4882a593Smuzhiyun 			for (i = 0; i != len; i++) {
449*4882a593Smuzhiyun 				char c;
450*4882a593Smuzhiyun 				if (get_user(c, data + i))
451*4882a593Smuzhiyun 					return -EFAULT;
452*4882a593Smuzhiyun 				if (c == 'V')
453*4882a593Smuzhiyun 					expect_release = 42;
454*4882a593Smuzhiyun 			}
455*4882a593Smuzhiyun 		}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 		/* someone wrote to us, we should reload the timer */
458*4882a593Smuzhiyun 		pcipcwd_keepalive();
459*4882a593Smuzhiyun 	}
460*4882a593Smuzhiyun 	return len;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
pcipcwd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)463*4882a593Smuzhiyun static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
464*4882a593Smuzhiyun 						unsigned long arg)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun 	void __user *argp = (void __user *)arg;
467*4882a593Smuzhiyun 	int __user *p = argp;
468*4882a593Smuzhiyun 	static const struct watchdog_info ident = {
469*4882a593Smuzhiyun 		.options =		WDIOF_OVERHEAT |
470*4882a593Smuzhiyun 					WDIOF_CARDRESET |
471*4882a593Smuzhiyun 					WDIOF_KEEPALIVEPING |
472*4882a593Smuzhiyun 					WDIOF_SETTIMEOUT |
473*4882a593Smuzhiyun 					WDIOF_MAGICCLOSE,
474*4882a593Smuzhiyun 		.firmware_version =	1,
475*4882a593Smuzhiyun 		.identity =		WATCHDOG_DRIVER_NAME,
476*4882a593Smuzhiyun 	};
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	switch (cmd) {
479*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
480*4882a593Smuzhiyun 		return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
483*4882a593Smuzhiyun 	{
484*4882a593Smuzhiyun 		int status;
485*4882a593Smuzhiyun 		pcipcwd_get_status(&status);
486*4882a593Smuzhiyun 		return put_user(status, p);
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
490*4882a593Smuzhiyun 		return put_user(pcipcwd_private.boot_status, p);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	case WDIOC_GETTEMP:
493*4882a593Smuzhiyun 	{
494*4882a593Smuzhiyun 		int temperature;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 		if (pcipcwd_get_temperature(&temperature))
497*4882a593Smuzhiyun 			return -EFAULT;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 		return put_user(temperature, p);
500*4882a593Smuzhiyun 	}
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
503*4882a593Smuzhiyun 	{
504*4882a593Smuzhiyun 		int new_options, retval = -EINVAL;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 		if (get_user(new_options, p))
507*4882a593Smuzhiyun 			return -EFAULT;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		if (new_options & WDIOS_DISABLECARD) {
510*4882a593Smuzhiyun 			if (pcipcwd_stop())
511*4882a593Smuzhiyun 				return -EIO;
512*4882a593Smuzhiyun 			retval = 0;
513*4882a593Smuzhiyun 		}
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 		if (new_options & WDIOS_ENABLECARD) {
516*4882a593Smuzhiyun 			if (pcipcwd_start())
517*4882a593Smuzhiyun 				return -EIO;
518*4882a593Smuzhiyun 			retval = 0;
519*4882a593Smuzhiyun 		}
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 		if (new_options & WDIOS_TEMPPANIC) {
522*4882a593Smuzhiyun 			temp_panic = 1;
523*4882a593Smuzhiyun 			retval = 0;
524*4882a593Smuzhiyun 		}
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 		return retval;
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
530*4882a593Smuzhiyun 		pcipcwd_keepalive();
531*4882a593Smuzhiyun 		return 0;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	case WDIOC_SETTIMEOUT:
534*4882a593Smuzhiyun 	{
535*4882a593Smuzhiyun 		int new_heartbeat;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 		if (get_user(new_heartbeat, p))
538*4882a593Smuzhiyun 			return -EFAULT;
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 		if (pcipcwd_set_heartbeat(new_heartbeat))
541*4882a593Smuzhiyun 			return -EINVAL;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 		pcipcwd_keepalive();
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun 		fallthrough;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
548*4882a593Smuzhiyun 		return put_user(heartbeat, p);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	case WDIOC_GETTIMELEFT:
551*4882a593Smuzhiyun 	{
552*4882a593Smuzhiyun 		int time_left;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 		if (pcipcwd_get_timeleft(&time_left))
555*4882a593Smuzhiyun 			return -EFAULT;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 		return put_user(time_left, p);
558*4882a593Smuzhiyun 	}
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	default:
561*4882a593Smuzhiyun 		return -ENOTTY;
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun 
pcipcwd_open(struct inode * inode,struct file * file)565*4882a593Smuzhiyun static int pcipcwd_open(struct inode *inode, struct file *file)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun 	/* /dev/watchdog can only be opened once */
568*4882a593Smuzhiyun 	if (test_and_set_bit(0, &is_active)) {
569*4882a593Smuzhiyun 		if (debug >= VERBOSE)
570*4882a593Smuzhiyun 			pr_err("Attempt to open already opened device\n");
571*4882a593Smuzhiyun 		return -EBUSY;
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	/* Activate */
575*4882a593Smuzhiyun 	pcipcwd_start();
576*4882a593Smuzhiyun 	pcipcwd_keepalive();
577*4882a593Smuzhiyun 	return stream_open(inode, file);
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
pcipcwd_release(struct inode * inode,struct file * file)580*4882a593Smuzhiyun static int pcipcwd_release(struct inode *inode, struct file *file)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	/*
583*4882a593Smuzhiyun 	 *      Shut off the timer.
584*4882a593Smuzhiyun 	 */
585*4882a593Smuzhiyun 	if (expect_release == 42) {
586*4882a593Smuzhiyun 		pcipcwd_stop();
587*4882a593Smuzhiyun 	} else {
588*4882a593Smuzhiyun 		pr_crit("Unexpected close, not stopping watchdog!\n");
589*4882a593Smuzhiyun 		pcipcwd_keepalive();
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 	expect_release = 0;
592*4882a593Smuzhiyun 	clear_bit(0, &is_active);
593*4882a593Smuzhiyun 	return 0;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun /*
597*4882a593Smuzhiyun  *	/dev/temperature handling
598*4882a593Smuzhiyun  */
599*4882a593Smuzhiyun 
pcipcwd_temp_read(struct file * file,char __user * data,size_t len,loff_t * ppos)600*4882a593Smuzhiyun static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
601*4882a593Smuzhiyun 				size_t len, loff_t *ppos)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun 	int temperature;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	if (pcipcwd_get_temperature(&temperature))
606*4882a593Smuzhiyun 		return -EFAULT;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	if (copy_to_user(data, &temperature, 1))
609*4882a593Smuzhiyun 		return -EFAULT;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	return 1;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun 
pcipcwd_temp_open(struct inode * inode,struct file * file)614*4882a593Smuzhiyun static int pcipcwd_temp_open(struct inode *inode, struct file *file)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun 	if (!pcipcwd_private.supports_temp)
617*4882a593Smuzhiyun 		return -ENODEV;
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	return stream_open(inode, file);
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun 
pcipcwd_temp_release(struct inode * inode,struct file * file)622*4882a593Smuzhiyun static int pcipcwd_temp_release(struct inode *inode, struct file *file)
623*4882a593Smuzhiyun {
624*4882a593Smuzhiyun 	return 0;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun /*
628*4882a593Smuzhiyun  *	Notify system
629*4882a593Smuzhiyun  */
630*4882a593Smuzhiyun 
pcipcwd_notify_sys(struct notifier_block * this,unsigned long code,void * unused)631*4882a593Smuzhiyun static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code,
632*4882a593Smuzhiyun 								void *unused)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	if (code == SYS_DOWN || code == SYS_HALT)
635*4882a593Smuzhiyun 		pcipcwd_stop();	/* Turn the WDT off */
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	return NOTIFY_DONE;
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun /*
641*4882a593Smuzhiyun  *	Kernel Interfaces
642*4882a593Smuzhiyun  */
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun static const struct file_operations pcipcwd_fops = {
645*4882a593Smuzhiyun 	.owner =	THIS_MODULE,
646*4882a593Smuzhiyun 	.llseek =	no_llseek,
647*4882a593Smuzhiyun 	.write =	pcipcwd_write,
648*4882a593Smuzhiyun 	.unlocked_ioctl = pcipcwd_ioctl,
649*4882a593Smuzhiyun 	.compat_ioctl = compat_ptr_ioctl,
650*4882a593Smuzhiyun 	.open =		pcipcwd_open,
651*4882a593Smuzhiyun 	.release =	pcipcwd_release,
652*4882a593Smuzhiyun };
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun static struct miscdevice pcipcwd_miscdev = {
655*4882a593Smuzhiyun 	.minor =	WATCHDOG_MINOR,
656*4882a593Smuzhiyun 	.name =		"watchdog",
657*4882a593Smuzhiyun 	.fops =		&pcipcwd_fops,
658*4882a593Smuzhiyun };
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun static const struct file_operations pcipcwd_temp_fops = {
661*4882a593Smuzhiyun 	.owner =	THIS_MODULE,
662*4882a593Smuzhiyun 	.llseek =	no_llseek,
663*4882a593Smuzhiyun 	.read =		pcipcwd_temp_read,
664*4882a593Smuzhiyun 	.open =		pcipcwd_temp_open,
665*4882a593Smuzhiyun 	.release =	pcipcwd_temp_release,
666*4882a593Smuzhiyun };
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun static struct miscdevice pcipcwd_temp_miscdev = {
669*4882a593Smuzhiyun 	.minor =	TEMP_MINOR,
670*4882a593Smuzhiyun 	.name =		"temperature",
671*4882a593Smuzhiyun 	.fops =		&pcipcwd_temp_fops,
672*4882a593Smuzhiyun };
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun static struct notifier_block pcipcwd_notifier = {
675*4882a593Smuzhiyun 	.notifier_call =	pcipcwd_notify_sys,
676*4882a593Smuzhiyun };
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun /*
679*4882a593Smuzhiyun  *	Init & exit routines
680*4882a593Smuzhiyun  */
681*4882a593Smuzhiyun 
pcipcwd_card_init(struct pci_dev * pdev,const struct pci_device_id * ent)682*4882a593Smuzhiyun static int pcipcwd_card_init(struct pci_dev *pdev,
683*4882a593Smuzhiyun 		const struct pci_device_id *ent)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	int ret = -EIO;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	cards_found++;
688*4882a593Smuzhiyun 	if (cards_found == 1)
689*4882a593Smuzhiyun 		pr_info("%s\n", DRIVER_VERSION);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (cards_found > 1) {
692*4882a593Smuzhiyun 		pr_err("This driver only supports 1 device\n");
693*4882a593Smuzhiyun 		return -ENODEV;
694*4882a593Smuzhiyun 	}
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	if (pci_enable_device(pdev)) {
697*4882a593Smuzhiyun 		pr_err("Not possible to enable PCI Device\n");
698*4882a593Smuzhiyun 		return -ENODEV;
699*4882a593Smuzhiyun 	}
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 	if (pci_resource_start(pdev, 0) == 0x0000) {
702*4882a593Smuzhiyun 		pr_err("No I/O-Address for card detected\n");
703*4882a593Smuzhiyun 		ret = -ENODEV;
704*4882a593Smuzhiyun 		goto err_out_disable_device;
705*4882a593Smuzhiyun 	}
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	spin_lock_init(&pcipcwd_private.io_lock);
708*4882a593Smuzhiyun 	pcipcwd_private.pdev = pdev;
709*4882a593Smuzhiyun 	pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	if (pci_request_regions(pdev, WATCHDOG_NAME)) {
712*4882a593Smuzhiyun 		pr_err("I/O address 0x%04x already in use\n",
713*4882a593Smuzhiyun 		       (int) pcipcwd_private.io_addr);
714*4882a593Smuzhiyun 		ret = -EIO;
715*4882a593Smuzhiyun 		goto err_out_disable_device;
716*4882a593Smuzhiyun 	}
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	/* get the boot_status */
719*4882a593Smuzhiyun 	pcipcwd_get_status(&pcipcwd_private.boot_status);
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	/* clear the "card caused reboot" flag */
722*4882a593Smuzhiyun 	pcipcwd_clear_status();
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	/* disable card */
725*4882a593Smuzhiyun 	pcipcwd_stop();
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	/* Check whether or not the card supports the temperature device */
728*4882a593Smuzhiyun 	pcipcwd_check_temperature_support();
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	/* Show info about the card itself */
731*4882a593Smuzhiyun 	pcipcwd_show_card_info();
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	/* If heartbeat = 0 then we use the heartbeat from the dip-switches */
734*4882a593Smuzhiyun 	if (heartbeat == 0)
735*4882a593Smuzhiyun 		heartbeat =
736*4882a593Smuzhiyun 			heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 	/* Check that the heartbeat value is within it's range ;
739*4882a593Smuzhiyun 	 * if not reset to the default */
740*4882a593Smuzhiyun 	if (pcipcwd_set_heartbeat(heartbeat)) {
741*4882a593Smuzhiyun 		pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
742*4882a593Smuzhiyun 		pr_info("heartbeat value must be 0<heartbeat<65536, using %d\n",
743*4882a593Smuzhiyun 			WATCHDOG_HEARTBEAT);
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	ret = register_reboot_notifier(&pcipcwd_notifier);
747*4882a593Smuzhiyun 	if (ret != 0) {
748*4882a593Smuzhiyun 		pr_err("cannot register reboot notifier (err=%d)\n", ret);
749*4882a593Smuzhiyun 		goto err_out_release_region;
750*4882a593Smuzhiyun 	}
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun 	if (pcipcwd_private.supports_temp) {
753*4882a593Smuzhiyun 		ret = misc_register(&pcipcwd_temp_miscdev);
754*4882a593Smuzhiyun 		if (ret != 0) {
755*4882a593Smuzhiyun 			pr_err("cannot register miscdev on minor=%d (err=%d)\n",
756*4882a593Smuzhiyun 			       TEMP_MINOR, ret);
757*4882a593Smuzhiyun 			goto err_out_unregister_reboot;
758*4882a593Smuzhiyun 		}
759*4882a593Smuzhiyun 	}
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	ret = misc_register(&pcipcwd_miscdev);
762*4882a593Smuzhiyun 	if (ret != 0) {
763*4882a593Smuzhiyun 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
764*4882a593Smuzhiyun 		       WATCHDOG_MINOR, ret);
765*4882a593Smuzhiyun 		goto err_out_misc_deregister;
766*4882a593Smuzhiyun 	}
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
769*4882a593Smuzhiyun 		heartbeat, nowayout);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	return 0;
772*4882a593Smuzhiyun 
773*4882a593Smuzhiyun err_out_misc_deregister:
774*4882a593Smuzhiyun 	if (pcipcwd_private.supports_temp)
775*4882a593Smuzhiyun 		misc_deregister(&pcipcwd_temp_miscdev);
776*4882a593Smuzhiyun err_out_unregister_reboot:
777*4882a593Smuzhiyun 	unregister_reboot_notifier(&pcipcwd_notifier);
778*4882a593Smuzhiyun err_out_release_region:
779*4882a593Smuzhiyun 	pci_release_regions(pdev);
780*4882a593Smuzhiyun err_out_disable_device:
781*4882a593Smuzhiyun 	pci_disable_device(pdev);
782*4882a593Smuzhiyun 	return ret;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
pcipcwd_card_exit(struct pci_dev * pdev)785*4882a593Smuzhiyun static void pcipcwd_card_exit(struct pci_dev *pdev)
786*4882a593Smuzhiyun {
787*4882a593Smuzhiyun 	/* Stop the timer before we leave */
788*4882a593Smuzhiyun 	if (!nowayout)
789*4882a593Smuzhiyun 		pcipcwd_stop();
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	/* Deregister */
792*4882a593Smuzhiyun 	misc_deregister(&pcipcwd_miscdev);
793*4882a593Smuzhiyun 	if (pcipcwd_private.supports_temp)
794*4882a593Smuzhiyun 		misc_deregister(&pcipcwd_temp_miscdev);
795*4882a593Smuzhiyun 	unregister_reboot_notifier(&pcipcwd_notifier);
796*4882a593Smuzhiyun 	pci_release_regions(pdev);
797*4882a593Smuzhiyun 	pci_disable_device(pdev);
798*4882a593Smuzhiyun 	cards_found--;
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun static const struct pci_device_id pcipcwd_pci_tbl[] = {
802*4882a593Smuzhiyun 	{ PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
803*4882a593Smuzhiyun 		PCI_ANY_ID, PCI_ANY_ID, },
804*4882a593Smuzhiyun 	{ 0 },			/* End of list */
805*4882a593Smuzhiyun };
806*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun static struct pci_driver pcipcwd_driver = {
809*4882a593Smuzhiyun 	.name		= WATCHDOG_NAME,
810*4882a593Smuzhiyun 	.id_table	= pcipcwd_pci_tbl,
811*4882a593Smuzhiyun 	.probe		= pcipcwd_card_init,
812*4882a593Smuzhiyun 	.remove		= pcipcwd_card_exit,
813*4882a593Smuzhiyun };
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun module_pci_driver(pcipcwd_driver);
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
818*4882a593Smuzhiyun MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
819*4882a593Smuzhiyun MODULE_LICENSE("GPL");
820