xref: /OK3568_Linux_fs/kernel/drivers/watchdog/wdt977.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	Wdt977	0.04:	A Watchdog Device for Netwinder W83977AF chip
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	(c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>)
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *			-----------------------
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *			-----------------------
10*4882a593Smuzhiyun  *      14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
11*4882a593Smuzhiyun  *           Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
12*4882a593Smuzhiyun  *	19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface
13*4882a593Smuzhiyun  *	06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts
14*4882a593Smuzhiyun  *				    from minutes to seconds.
15*4882a593Smuzhiyun  *      07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in
16*4882a593Smuzhiyun  *                                    nwwatchdog_init.
17*4882a593Smuzhiyun  *      25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks
18*4882a593Smuzhiyun  *				    remove limitiation to be used on
19*4882a593Smuzhiyun  *				    Netwinders only
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #include <linux/module.h>
25*4882a593Smuzhiyun #include <linux/moduleparam.h>
26*4882a593Smuzhiyun #include <linux/types.h>
27*4882a593Smuzhiyun #include <linux/kernel.h>
28*4882a593Smuzhiyun #include <linux/fs.h>
29*4882a593Smuzhiyun #include <linux/miscdevice.h>
30*4882a593Smuzhiyun #include <linux/init.h>
31*4882a593Smuzhiyun #include <linux/ioport.h>
32*4882a593Smuzhiyun #include <linux/watchdog.h>
33*4882a593Smuzhiyun #include <linux/notifier.h>
34*4882a593Smuzhiyun #include <linux/reboot.h>
35*4882a593Smuzhiyun #include <linux/io.h>
36*4882a593Smuzhiyun #include <linux/uaccess.h>
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #include <asm/mach-types.h>
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #define WATCHDOG_VERSION  "0.04"
41*4882a593Smuzhiyun #define WATCHDOG_NAME     "Wdt977"
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define IO_INDEX_PORT	0x370		/* on some systems it can be 0x3F0 */
44*4882a593Smuzhiyun #define IO_DATA_PORT	(IO_INDEX_PORT + 1)
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define UNLOCK_DATA	0x87
47*4882a593Smuzhiyun #define LOCK_DATA	0xAA
48*4882a593Smuzhiyun #define DEVICE_REGISTER	0x07
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define	DEFAULT_TIMEOUT	60			/* default timeout in seconds */
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static	int timeout = DEFAULT_TIMEOUT;
54*4882a593Smuzhiyun static	int timeoutM;				/* timeout in minutes */
55*4882a593Smuzhiyun static	unsigned long timer_alive;
56*4882a593Smuzhiyun static	int testmode;
57*4882a593Smuzhiyun static	char expect_close;
58*4882a593Smuzhiyun static	DEFINE_SPINLOCK(spinlock);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun module_param(timeout, int, 0);
61*4882a593Smuzhiyun MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (60..15300, default="
62*4882a593Smuzhiyun 				__MODULE_STRING(DEFAULT_TIMEOUT) ")");
63*4882a593Smuzhiyun module_param(testmode, int, 0);
64*4882a593Smuzhiyun MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0");
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
67*4882a593Smuzhiyun module_param(nowayout, bool, 0);
68*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout,
69*4882a593Smuzhiyun 		"Watchdog cannot be stopped once started (default="
70*4882a593Smuzhiyun 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /*
73*4882a593Smuzhiyun  * Start the watchdog
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun 
wdt977_start(void)76*4882a593Smuzhiyun static int wdt977_start(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	unsigned long flags;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	spin_lock_irqsave(&spinlock, flags);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/* unlock the SuperIO chip */
83*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
84*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 	/* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4
87*4882a593Smuzhiyun 	 * F2 has the timeout in minutes
88*4882a593Smuzhiyun 	 * F3 could be set to the POWER LED blink (with GP17 set to PowerLed)
89*4882a593Smuzhiyun 	 *   at timeout, and to reset timer on kbd/mouse activity (not impl.)
90*4882a593Smuzhiyun 	 * F4 is used to just clear the TIMEOUT'ed state (bit 0)
91*4882a593Smuzhiyun 	 */
92*4882a593Smuzhiyun 	outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
93*4882a593Smuzhiyun 	outb_p(0x08, IO_DATA_PORT);
94*4882a593Smuzhiyun 	outb_p(0xF2, IO_INDEX_PORT);
95*4882a593Smuzhiyun 	outb_p(timeoutM, IO_DATA_PORT);
96*4882a593Smuzhiyun 	outb_p(0xF3, IO_INDEX_PORT);
97*4882a593Smuzhiyun 	outb_p(0x00, IO_DATA_PORT);	/* another setting is 0E for
98*4882a593Smuzhiyun 					   kbd/mouse/LED */
99*4882a593Smuzhiyun 	outb_p(0xF4, IO_INDEX_PORT);
100*4882a593Smuzhiyun 	outb_p(0x00, IO_DATA_PORT);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* At last select device Aux1 (dev=7) and set GP16 as a
103*4882a593Smuzhiyun 	 * watchdog output. In test mode watch the bit 1 on F4 to
104*4882a593Smuzhiyun 	 * indicate "triggered"
105*4882a593Smuzhiyun 	 */
106*4882a593Smuzhiyun 	if (!testmode) {
107*4882a593Smuzhiyun 		outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
108*4882a593Smuzhiyun 		outb_p(0x07, IO_DATA_PORT);
109*4882a593Smuzhiyun 		outb_p(0xE6, IO_INDEX_PORT);
110*4882a593Smuzhiyun 		outb_p(0x08, IO_DATA_PORT);
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/* lock the SuperIO chip */
114*4882a593Smuzhiyun 	outb_p(LOCK_DATA, IO_INDEX_PORT);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	spin_unlock_irqrestore(&spinlock, flags);
117*4882a593Smuzhiyun 	pr_info("activated\n");
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	return 0;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /*
123*4882a593Smuzhiyun  * Stop the watchdog
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun 
wdt977_stop(void)126*4882a593Smuzhiyun static int wdt977_stop(void)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	unsigned long flags;
129*4882a593Smuzhiyun 	spin_lock_irqsave(&spinlock, flags);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	/* unlock the SuperIO chip */
132*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
133*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4
136*4882a593Smuzhiyun 	* F3 is reset to its default state
137*4882a593Smuzhiyun 	* F4 can clear the TIMEOUT'ed state (bit 0) - back to default
138*4882a593Smuzhiyun 	* We can not use GP17 as a PowerLed, as we use its usage as a RedLed
139*4882a593Smuzhiyun 	*/
140*4882a593Smuzhiyun 	outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
141*4882a593Smuzhiyun 	outb_p(0x08, IO_DATA_PORT);
142*4882a593Smuzhiyun 	outb_p(0xF2, IO_INDEX_PORT);
143*4882a593Smuzhiyun 	outb_p(0xFF, IO_DATA_PORT);
144*4882a593Smuzhiyun 	outb_p(0xF3, IO_INDEX_PORT);
145*4882a593Smuzhiyun 	outb_p(0x00, IO_DATA_PORT);
146*4882a593Smuzhiyun 	outb_p(0xF4, IO_INDEX_PORT);
147*4882a593Smuzhiyun 	outb_p(0x00, IO_DATA_PORT);
148*4882a593Smuzhiyun 	outb_p(0xF2, IO_INDEX_PORT);
149*4882a593Smuzhiyun 	outb_p(0x00, IO_DATA_PORT);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/* at last select device Aux1 (dev=7) and set
152*4882a593Smuzhiyun 	   GP16 as a watchdog output */
153*4882a593Smuzhiyun 	outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
154*4882a593Smuzhiyun 	outb_p(0x07, IO_DATA_PORT);
155*4882a593Smuzhiyun 	outb_p(0xE6, IO_INDEX_PORT);
156*4882a593Smuzhiyun 	outb_p(0x08, IO_DATA_PORT);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	/* lock the SuperIO chip */
159*4882a593Smuzhiyun 	outb_p(LOCK_DATA, IO_INDEX_PORT);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	spin_unlock_irqrestore(&spinlock, flags);
162*4882a593Smuzhiyun 	pr_info("shutdown\n");
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	return 0;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /*
168*4882a593Smuzhiyun  * Send a keepalive ping to the watchdog
169*4882a593Smuzhiyun  * This is done by simply re-writing the timeout to reg. 0xF2
170*4882a593Smuzhiyun  */
171*4882a593Smuzhiyun 
wdt977_keepalive(void)172*4882a593Smuzhiyun static int wdt977_keepalive(void)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	unsigned long flags;
175*4882a593Smuzhiyun 	spin_lock_irqsave(&spinlock, flags);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	/* unlock the SuperIO chip */
178*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
179*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* select device Aux2 (device=8) and kicks watchdog reg F2 */
182*4882a593Smuzhiyun 	/* F2 has the timeout in minutes */
183*4882a593Smuzhiyun 	outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
184*4882a593Smuzhiyun 	outb_p(0x08, IO_DATA_PORT);
185*4882a593Smuzhiyun 	outb_p(0xF2, IO_INDEX_PORT);
186*4882a593Smuzhiyun 	outb_p(timeoutM, IO_DATA_PORT);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* lock the SuperIO chip */
189*4882a593Smuzhiyun 	outb_p(LOCK_DATA, IO_INDEX_PORT);
190*4882a593Smuzhiyun 	spin_unlock_irqrestore(&spinlock, flags);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun  * Set the watchdog timeout value
197*4882a593Smuzhiyun  */
198*4882a593Smuzhiyun 
wdt977_set_timeout(int t)199*4882a593Smuzhiyun static int wdt977_set_timeout(int t)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	int tmrval;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	/* convert seconds to minutes, rounding up */
204*4882a593Smuzhiyun 	tmrval = (t + 59) / 60;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	if (machine_is_netwinder()) {
207*4882a593Smuzhiyun 		/* we have a hw bug somewhere, so each 977 minute is actually
208*4882a593Smuzhiyun 		 * only 30sec. This limits the max timeout to half of device
209*4882a593Smuzhiyun 		 * max of 255 minutes...
210*4882a593Smuzhiyun 		 */
211*4882a593Smuzhiyun 		tmrval += tmrval;
212*4882a593Smuzhiyun 	}
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	if (tmrval < 1 || tmrval > 255)
215*4882a593Smuzhiyun 		return -EINVAL;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	/* timeout is the timeout in seconds, timeoutM is
218*4882a593Smuzhiyun 	   the timeout in minutes) */
219*4882a593Smuzhiyun 	timeout = t;
220*4882a593Smuzhiyun 	timeoutM = tmrval;
221*4882a593Smuzhiyun 	return 0;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun /*
225*4882a593Smuzhiyun  * Get the watchdog status
226*4882a593Smuzhiyun  */
227*4882a593Smuzhiyun 
wdt977_get_status(int * status)228*4882a593Smuzhiyun static int wdt977_get_status(int *status)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	int new_status;
231*4882a593Smuzhiyun 	unsigned long flags;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	spin_lock_irqsave(&spinlock, flags);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/* unlock the SuperIO chip */
236*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
237*4882a593Smuzhiyun 	outb_p(UNLOCK_DATA, IO_INDEX_PORT);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	/* select device Aux2 (device=8) and read watchdog reg F4 */
240*4882a593Smuzhiyun 	outb_p(DEVICE_REGISTER, IO_INDEX_PORT);
241*4882a593Smuzhiyun 	outb_p(0x08, IO_DATA_PORT);
242*4882a593Smuzhiyun 	outb_p(0xF4, IO_INDEX_PORT);
243*4882a593Smuzhiyun 	new_status = inb_p(IO_DATA_PORT);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/* lock the SuperIO chip */
246*4882a593Smuzhiyun 	outb_p(LOCK_DATA, IO_INDEX_PORT);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	spin_unlock_irqrestore(&spinlock, flags);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	*status = 0;
251*4882a593Smuzhiyun 	if (new_status & 1)
252*4882a593Smuzhiyun 		*status |= WDIOF_CARDRESET;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	return 0;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun /*
259*4882a593Smuzhiyun  *	/dev/watchdog handling
260*4882a593Smuzhiyun  */
261*4882a593Smuzhiyun 
wdt977_open(struct inode * inode,struct file * file)262*4882a593Smuzhiyun static int wdt977_open(struct inode *inode, struct file *file)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	/* If the watchdog is alive we don't need to start it again */
265*4882a593Smuzhiyun 	if (test_and_set_bit(0, &timer_alive))
266*4882a593Smuzhiyun 		return -EBUSY;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	if (nowayout)
269*4882a593Smuzhiyun 		__module_get(THIS_MODULE);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	wdt977_start();
272*4882a593Smuzhiyun 	return stream_open(inode, file);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
wdt977_release(struct inode * inode,struct file * file)275*4882a593Smuzhiyun static int wdt977_release(struct inode *inode, struct file *file)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun 	/*
278*4882a593Smuzhiyun 	 *	Shut off the timer.
279*4882a593Smuzhiyun 	 *	Lock it in if it's a module and we set nowayout
280*4882a593Smuzhiyun 	 */
281*4882a593Smuzhiyun 	if (expect_close == 42) {
282*4882a593Smuzhiyun 		wdt977_stop();
283*4882a593Smuzhiyun 		clear_bit(0, &timer_alive);
284*4882a593Smuzhiyun 	} else {
285*4882a593Smuzhiyun 		wdt977_keepalive();
286*4882a593Smuzhiyun 		pr_crit("Unexpected close, not stopping watchdog!\n");
287*4882a593Smuzhiyun 	}
288*4882a593Smuzhiyun 	expect_close = 0;
289*4882a593Smuzhiyun 	return 0;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun /*
294*4882a593Smuzhiyun  *      wdt977_write:
295*4882a593Smuzhiyun  *      @file: file handle to the watchdog
296*4882a593Smuzhiyun  *      @buf: buffer to write (unused as data does not matter here
297*4882a593Smuzhiyun  *      @count: count of bytes
298*4882a593Smuzhiyun  *      @ppos: pointer to the position to write. No seeks allowed
299*4882a593Smuzhiyun  *
300*4882a593Smuzhiyun  *      A write to a watchdog device is defined as a keepalive signal. Any
301*4882a593Smuzhiyun  *      write of data will do, as we we don't define content meaning.
302*4882a593Smuzhiyun  */
303*4882a593Smuzhiyun 
wdt977_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)304*4882a593Smuzhiyun static ssize_t wdt977_write(struct file *file, const char __user *buf,
305*4882a593Smuzhiyun 			    size_t count, loff_t *ppos)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	if (count) {
308*4882a593Smuzhiyun 		if (!nowayout) {
309*4882a593Smuzhiyun 			size_t i;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 			/* In case it was set long ago */
312*4882a593Smuzhiyun 			expect_close = 0;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 			for (i = 0; i != count; i++) {
315*4882a593Smuzhiyun 				char c;
316*4882a593Smuzhiyun 				if (get_user(c, buf + i))
317*4882a593Smuzhiyun 					return -EFAULT;
318*4882a593Smuzhiyun 				if (c == 'V')
319*4882a593Smuzhiyun 					expect_close = 42;
320*4882a593Smuzhiyun 			}
321*4882a593Smuzhiyun 		}
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 		/* someone wrote to us, we should restart timer */
324*4882a593Smuzhiyun 		wdt977_keepalive();
325*4882a593Smuzhiyun 	}
326*4882a593Smuzhiyun 	return count;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun static const struct watchdog_info ident = {
330*4882a593Smuzhiyun 	.options =		WDIOF_SETTIMEOUT |
331*4882a593Smuzhiyun 				WDIOF_MAGICCLOSE |
332*4882a593Smuzhiyun 				WDIOF_KEEPALIVEPING,
333*4882a593Smuzhiyun 	.firmware_version =	1,
334*4882a593Smuzhiyun 	.identity =		WATCHDOG_NAME,
335*4882a593Smuzhiyun };
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun /*
338*4882a593Smuzhiyun  *      wdt977_ioctl:
339*4882a593Smuzhiyun  *      @inode: inode of the device
340*4882a593Smuzhiyun  *      @file: file handle to the device
341*4882a593Smuzhiyun  *      @cmd: watchdog command
342*4882a593Smuzhiyun  *      @arg: argument pointer
343*4882a593Smuzhiyun  *
344*4882a593Smuzhiyun  *      The watchdog API defines a common set of functions for all watchdogs
345*4882a593Smuzhiyun  *      according to their available features.
346*4882a593Smuzhiyun  */
347*4882a593Smuzhiyun 
wdt977_ioctl(struct file * file,unsigned int cmd,unsigned long arg)348*4882a593Smuzhiyun static long wdt977_ioctl(struct file *file, unsigned int cmd,
349*4882a593Smuzhiyun 							unsigned long arg)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	int status;
352*4882a593Smuzhiyun 	int new_options, retval = -EINVAL;
353*4882a593Smuzhiyun 	int new_timeout;
354*4882a593Smuzhiyun 	union {
355*4882a593Smuzhiyun 		struct watchdog_info __user *ident;
356*4882a593Smuzhiyun 		int __user *i;
357*4882a593Smuzhiyun 	} uarg;
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	uarg.i = (int __user *)arg;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	switch (cmd) {
362*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
363*4882a593Smuzhiyun 		return copy_to_user(uarg.ident, &ident,
364*4882a593Smuzhiyun 			sizeof(ident)) ? -EFAULT : 0;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
367*4882a593Smuzhiyun 		wdt977_get_status(&status);
368*4882a593Smuzhiyun 		return put_user(status, uarg.i);
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
371*4882a593Smuzhiyun 		return put_user(0, uarg.i);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
374*4882a593Smuzhiyun 		if (get_user(new_options, uarg.i))
375*4882a593Smuzhiyun 			return -EFAULT;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 		if (new_options & WDIOS_DISABLECARD) {
378*4882a593Smuzhiyun 			wdt977_stop();
379*4882a593Smuzhiyun 			retval = 0;
380*4882a593Smuzhiyun 		}
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		if (new_options & WDIOS_ENABLECARD) {
383*4882a593Smuzhiyun 			wdt977_start();
384*4882a593Smuzhiyun 			retval = 0;
385*4882a593Smuzhiyun 		}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 		return retval;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
390*4882a593Smuzhiyun 		wdt977_keepalive();
391*4882a593Smuzhiyun 		return 0;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	case WDIOC_SETTIMEOUT:
394*4882a593Smuzhiyun 		if (get_user(new_timeout, uarg.i))
395*4882a593Smuzhiyun 			return -EFAULT;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 		if (wdt977_set_timeout(new_timeout))
398*4882a593Smuzhiyun 			return -EINVAL;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 		wdt977_keepalive();
401*4882a593Smuzhiyun 		fallthrough;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
404*4882a593Smuzhiyun 		return put_user(timeout, uarg.i);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	default:
407*4882a593Smuzhiyun 		return -ENOTTY;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
wdt977_notify_sys(struct notifier_block * this,unsigned long code,void * unused)412*4882a593Smuzhiyun static int wdt977_notify_sys(struct notifier_block *this, unsigned long code,
413*4882a593Smuzhiyun 	void *unused)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	if (code == SYS_DOWN || code == SYS_HALT)
416*4882a593Smuzhiyun 		wdt977_stop();
417*4882a593Smuzhiyun 	return NOTIFY_DONE;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun static const struct file_operations wdt977_fops = {
421*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
422*4882a593Smuzhiyun 	.llseek		= no_llseek,
423*4882a593Smuzhiyun 	.write		= wdt977_write,
424*4882a593Smuzhiyun 	.unlocked_ioctl	= wdt977_ioctl,
425*4882a593Smuzhiyun 	.compat_ioctl	= compat_ptr_ioctl,
426*4882a593Smuzhiyun 	.open		= wdt977_open,
427*4882a593Smuzhiyun 	.release	= wdt977_release,
428*4882a593Smuzhiyun };
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun static struct miscdevice wdt977_miscdev = {
431*4882a593Smuzhiyun 	.minor		= WATCHDOG_MINOR,
432*4882a593Smuzhiyun 	.name		= "watchdog",
433*4882a593Smuzhiyun 	.fops		= &wdt977_fops,
434*4882a593Smuzhiyun };
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun static struct notifier_block wdt977_notifier = {
437*4882a593Smuzhiyun 	.notifier_call = wdt977_notify_sys,
438*4882a593Smuzhiyun };
439*4882a593Smuzhiyun 
wd977_init(void)440*4882a593Smuzhiyun static int __init wd977_init(void)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun 	int rc;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	pr_info("driver v%s\n", WATCHDOG_VERSION);
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	/* Check that the timeout value is within its range;
447*4882a593Smuzhiyun 	   if not reset to the default */
448*4882a593Smuzhiyun 	if (wdt977_set_timeout(timeout)) {
449*4882a593Smuzhiyun 		wdt977_set_timeout(DEFAULT_TIMEOUT);
450*4882a593Smuzhiyun 		pr_info("timeout value must be 60 < timeout < 15300, using %d\n",
451*4882a593Smuzhiyun 			DEFAULT_TIMEOUT);
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	/* on Netwinder the IOports are already reserved by
455*4882a593Smuzhiyun 	 * arch/arm/mach-footbridge/netwinder-hw.c
456*4882a593Smuzhiyun 	 */
457*4882a593Smuzhiyun 	if (!machine_is_netwinder()) {
458*4882a593Smuzhiyun 		if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME)) {
459*4882a593Smuzhiyun 			pr_err("I/O address 0x%04x already in use\n",
460*4882a593Smuzhiyun 			       IO_INDEX_PORT);
461*4882a593Smuzhiyun 			rc = -EIO;
462*4882a593Smuzhiyun 			goto err_out;
463*4882a593Smuzhiyun 		}
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	rc = register_reboot_notifier(&wdt977_notifier);
467*4882a593Smuzhiyun 	if (rc) {
468*4882a593Smuzhiyun 		pr_err("cannot register reboot notifier (err=%d)\n", rc);
469*4882a593Smuzhiyun 		goto err_out_region;
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	rc = misc_register(&wdt977_miscdev);
473*4882a593Smuzhiyun 	if (rc) {
474*4882a593Smuzhiyun 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
475*4882a593Smuzhiyun 		       wdt977_miscdev.minor, rc);
476*4882a593Smuzhiyun 		goto err_out_reboot;
477*4882a593Smuzhiyun 	}
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	pr_info("initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n",
480*4882a593Smuzhiyun 		timeout, nowayout, testmode);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	return 0;
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun err_out_reboot:
485*4882a593Smuzhiyun 	unregister_reboot_notifier(&wdt977_notifier);
486*4882a593Smuzhiyun err_out_region:
487*4882a593Smuzhiyun 	if (!machine_is_netwinder())
488*4882a593Smuzhiyun 		release_region(IO_INDEX_PORT, 2);
489*4882a593Smuzhiyun err_out:
490*4882a593Smuzhiyun 	return rc;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun 
wd977_exit(void)493*4882a593Smuzhiyun static void __exit wd977_exit(void)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	wdt977_stop();
496*4882a593Smuzhiyun 	misc_deregister(&wdt977_miscdev);
497*4882a593Smuzhiyun 	unregister_reboot_notifier(&wdt977_notifier);
498*4882a593Smuzhiyun 	release_region(IO_INDEX_PORT, 2);
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun module_init(wd977_init);
502*4882a593Smuzhiyun module_exit(wd977_exit);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>");
505*4882a593Smuzhiyun MODULE_DESCRIPTION("W83977AF Watchdog driver");
506*4882a593Smuzhiyun MODULE_LICENSE("GPL");
507