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