xref: /OK3568_Linux_fs/kernel/drivers/watchdog/sc520_wdt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *	AMD Elan SC520 processor Watchdog Timer driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *	Based on acquirewdt.c by Alan Cox,
6*4882a593Smuzhiyun  *	     and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *	The authors do NOT admit liability nor provide warranty for
9*4882a593Smuzhiyun  *	any of this software. This material is provided "AS-IS" in
10*4882a593Smuzhiyun  *	the hope that it may be useful for others.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *	(c) Copyright 2001    Scott Jennings <linuxdrivers@oro.net>
13*4882a593Smuzhiyun  *           9/27 - 2001      [Initial release]
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *	Additional fixes Alan Cox
16*4882a593Smuzhiyun  *	-	Fixed formatting
17*4882a593Smuzhiyun  *	-	Removed debug printks
18*4882a593Smuzhiyun  *	-	Fixed SMP built kernel deadlock
19*4882a593Smuzhiyun  *	-	Switched to private locks not lock_kernel
20*4882a593Smuzhiyun  *	-	Used ioremap/writew/readw
21*4882a593Smuzhiyun  *	-	Added NOWAYOUT support
22*4882a593Smuzhiyun  *	4/12 - 2002 Changes by Rob Radez <rob@osinvestor.com>
23*4882a593Smuzhiyun  *	-	Change comments
24*4882a593Smuzhiyun  *	-	Eliminate fop_llseek
25*4882a593Smuzhiyun  *	-	Change CONFIG_WATCHDOG_NOWAYOUT semantics
26*4882a593Smuzhiyun  *	-	Add KERN_* tags to printks
27*4882a593Smuzhiyun  *	-	fix possible wdt_is_open race
28*4882a593Smuzhiyun  *	-	Report proper capabilities in watchdog_info
29*4882a593Smuzhiyun  *	-	Add WDIOC_{GETSTATUS, GETBOOTSTATUS, SETTIMEOUT,
30*4882a593Smuzhiyun  *		GETTIMEOUT, SETOPTIONS} ioctls
31*4882a593Smuzhiyun  *	09/8 - 2003 Changes by Wim Van Sebroeck <wim@iguana.be>
32*4882a593Smuzhiyun  *	-	cleanup of trailing spaces
33*4882a593Smuzhiyun  *	-	added extra printk's for startup problems
34*4882a593Smuzhiyun  *	-	use module_param
35*4882a593Smuzhiyun  *	-	made timeout (the emulated heartbeat) a module_param
36*4882a593Smuzhiyun  *	-	made the keepalive ping an internal subroutine
37*4882a593Smuzhiyun  *	3/27 - 2004 Changes by Sean Young <sean@mess.org>
38*4882a593Smuzhiyun  *	-	set MMCR_BASE to 0xfffef000
39*4882a593Smuzhiyun  *	-	CBAR does not need to be read
40*4882a593Smuzhiyun  *	-	removed debugging printks
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  *  This WDT driver is different from most other Linux WDT
43*4882a593Smuzhiyun  *  drivers in that the driver will ping the watchdog by itself,
44*4882a593Smuzhiyun  *  because this particular WDT has a very short timeout (1.6
45*4882a593Smuzhiyun  *  seconds) and it would be insane to count on any userspace
46*4882a593Smuzhiyun  *  daemon always getting scheduled within that time frame.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  *  This driver uses memory mapped IO, and spinlock.
49*4882a593Smuzhiyun  */
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun #include <linux/module.h>
54*4882a593Smuzhiyun #include <linux/moduleparam.h>
55*4882a593Smuzhiyun #include <linux/types.h>
56*4882a593Smuzhiyun #include <linux/timer.h>
57*4882a593Smuzhiyun #include <linux/miscdevice.h>
58*4882a593Smuzhiyun #include <linux/watchdog.h>
59*4882a593Smuzhiyun #include <linux/fs.h>
60*4882a593Smuzhiyun #include <linux/ioport.h>
61*4882a593Smuzhiyun #include <linux/notifier.h>
62*4882a593Smuzhiyun #include <linux/reboot.h>
63*4882a593Smuzhiyun #include <linux/init.h>
64*4882a593Smuzhiyun #include <linux/jiffies.h>
65*4882a593Smuzhiyun #include <linux/io.h>
66*4882a593Smuzhiyun #include <linux/uaccess.h>
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  * The AMD Elan SC520 timeout value is 492us times a power of 2 (0-7)
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  *   0: 492us    2: 1.01s    4: 4.03s   6: 16.22s
73*4882a593Smuzhiyun  *   1: 503ms    3: 2.01s    5: 8.05s   7: 32.21s
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  * We will program the SC520 watchdog for a timeout of 2.01s.
76*4882a593Smuzhiyun  * If we reset the watchdog every ~250ms we should be safe.
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #define WDT_INTERVAL (HZ/4+1)
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * We must not require too good response from the userspace daemon.
83*4882a593Smuzhiyun  * Here we require the userspace daemon to send us a heartbeat
84*4882a593Smuzhiyun  * char to /dev/watchdog every 30 seconds.
85*4882a593Smuzhiyun  */
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun #define WATCHDOG_TIMEOUT 30		/* 30 sec default timeout */
88*4882a593Smuzhiyun /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
89*4882a593Smuzhiyun static int timeout = WATCHDOG_TIMEOUT;
90*4882a593Smuzhiyun module_param(timeout, int, 0);
91*4882a593Smuzhiyun MODULE_PARM_DESC(timeout,
92*4882a593Smuzhiyun 	"Watchdog timeout in seconds. (1 <= timeout <= 3600, default="
93*4882a593Smuzhiyun 				__MODULE_STRING(WATCHDOG_TIMEOUT) ")");
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun static bool nowayout = WATCHDOG_NOWAYOUT;
96*4882a593Smuzhiyun module_param(nowayout, bool, 0);
97*4882a593Smuzhiyun MODULE_PARM_DESC(nowayout,
98*4882a593Smuzhiyun 		"Watchdog cannot be stopped once started (default="
99*4882a593Smuzhiyun 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun  * AMD Elan SC520 - Watchdog Timer Registers
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun #define MMCR_BASE	0xfffef000	/* The default base address */
105*4882a593Smuzhiyun #define OFFS_WDTMRCTL	0xCB0	/* Watchdog Timer Control Register */
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /* WDT Control Register bit definitions */
108*4882a593Smuzhiyun #define WDT_EXP_SEL_01	0x0001	/* [01] Time-out = 496 us (with 33 Mhz clk). */
109*4882a593Smuzhiyun #define WDT_EXP_SEL_02	0x0002	/* [02] Time-out = 508 ms (with 33 Mhz clk). */
110*4882a593Smuzhiyun #define WDT_EXP_SEL_03	0x0004	/* [03] Time-out = 1.02 s (with 33 Mhz clk). */
111*4882a593Smuzhiyun #define WDT_EXP_SEL_04	0x0008	/* [04] Time-out = 2.03 s (with 33 Mhz clk). */
112*4882a593Smuzhiyun #define WDT_EXP_SEL_05	0x0010	/* [05] Time-out = 4.07 s (with 33 Mhz clk). */
113*4882a593Smuzhiyun #define WDT_EXP_SEL_06	0x0020	/* [06] Time-out = 8.13 s (with 33 Mhz clk). */
114*4882a593Smuzhiyun #define WDT_EXP_SEL_07	0x0040	/* [07] Time-out = 16.27s (with 33 Mhz clk). */
115*4882a593Smuzhiyun #define WDT_EXP_SEL_08	0x0080	/* [08] Time-out = 32.54s (with 33 Mhz clk). */
116*4882a593Smuzhiyun #define WDT_IRQ_FLG	0x1000	/* [12] Interrupt Request Flag */
117*4882a593Smuzhiyun #define WDT_WRST_ENB	0x4000	/* [14] Watchdog Timer Reset Enable */
118*4882a593Smuzhiyun #define WDT_ENB		0x8000	/* [15] Watchdog Timer Enable */
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun static __u16 __iomem *wdtmrctl;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun static void wdt_timer_ping(struct timer_list *);
123*4882a593Smuzhiyun static DEFINE_TIMER(timer, wdt_timer_ping);
124*4882a593Smuzhiyun static unsigned long next_heartbeat;
125*4882a593Smuzhiyun static unsigned long wdt_is_open;
126*4882a593Smuzhiyun static char wdt_expect_close;
127*4882a593Smuzhiyun static DEFINE_SPINLOCK(wdt_spinlock);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun  *	Whack the dog
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun 
wdt_timer_ping(struct timer_list * unused)133*4882a593Smuzhiyun static void wdt_timer_ping(struct timer_list *unused)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	/* If we got a heartbeat pulse within the WDT_US_INTERVAL
136*4882a593Smuzhiyun 	 * we agree to ping the WDT
137*4882a593Smuzhiyun 	 */
138*4882a593Smuzhiyun 	if (time_before(jiffies, next_heartbeat)) {
139*4882a593Smuzhiyun 		/* Ping the WDT */
140*4882a593Smuzhiyun 		spin_lock(&wdt_spinlock);
141*4882a593Smuzhiyun 		writew(0xAAAA, wdtmrctl);
142*4882a593Smuzhiyun 		writew(0x5555, wdtmrctl);
143*4882a593Smuzhiyun 		spin_unlock(&wdt_spinlock);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		/* Re-set the timer interval */
146*4882a593Smuzhiyun 		mod_timer(&timer, jiffies + WDT_INTERVAL);
147*4882a593Smuzhiyun 	} else
148*4882a593Smuzhiyun 		pr_warn("Heartbeat lost! Will not ping the watchdog\n");
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun  *	Utility routines
153*4882a593Smuzhiyun  */
154*4882a593Smuzhiyun 
wdt_config(int writeval)155*4882a593Smuzhiyun static void wdt_config(int writeval)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	unsigned long flags;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* buy some time (ping) */
160*4882a593Smuzhiyun 	spin_lock_irqsave(&wdt_spinlock, flags);
161*4882a593Smuzhiyun 	readw(wdtmrctl);	/* ensure write synchronization */
162*4882a593Smuzhiyun 	writew(0xAAAA, wdtmrctl);
163*4882a593Smuzhiyun 	writew(0x5555, wdtmrctl);
164*4882a593Smuzhiyun 	/* unlock WDT = make WDT configuration register writable one time */
165*4882a593Smuzhiyun 	writew(0x3333, wdtmrctl);
166*4882a593Smuzhiyun 	writew(0xCCCC, wdtmrctl);
167*4882a593Smuzhiyun 	/* write WDT configuration register */
168*4882a593Smuzhiyun 	writew(writeval, wdtmrctl);
169*4882a593Smuzhiyun 	spin_unlock_irqrestore(&wdt_spinlock, flags);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
wdt_startup(void)172*4882a593Smuzhiyun static int wdt_startup(void)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	next_heartbeat = jiffies + (timeout * HZ);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* Start the timer */
177*4882a593Smuzhiyun 	mod_timer(&timer, jiffies + WDT_INTERVAL);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/* Start the watchdog */
180*4882a593Smuzhiyun 	wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	pr_info("Watchdog timer is now enabled\n");
183*4882a593Smuzhiyun 	return 0;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
wdt_turnoff(void)186*4882a593Smuzhiyun static int wdt_turnoff(void)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	/* Stop the timer */
189*4882a593Smuzhiyun 	del_timer_sync(&timer);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	/* Stop the watchdog */
192*4882a593Smuzhiyun 	wdt_config(0);
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	pr_info("Watchdog timer is now disabled...\n");
195*4882a593Smuzhiyun 	return 0;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
wdt_keepalive(void)198*4882a593Smuzhiyun static int wdt_keepalive(void)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	/* user land ping */
201*4882a593Smuzhiyun 	next_heartbeat = jiffies + (timeout * HZ);
202*4882a593Smuzhiyun 	return 0;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
wdt_set_heartbeat(int t)205*4882a593Smuzhiyun static int wdt_set_heartbeat(int t)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun 	if ((t < 1) || (t > 3600))	/* arbitrary upper limit */
208*4882a593Smuzhiyun 		return -EINVAL;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	timeout = t;
211*4882a593Smuzhiyun 	return 0;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun  *	/dev/watchdog handling
216*4882a593Smuzhiyun  */
217*4882a593Smuzhiyun 
fop_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)218*4882a593Smuzhiyun static ssize_t fop_write(struct file *file, const char __user *buf,
219*4882a593Smuzhiyun 						size_t count, loff_t *ppos)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	/* See if we got the magic character 'V' and reload the timer */
222*4882a593Smuzhiyun 	if (count) {
223*4882a593Smuzhiyun 		if (!nowayout) {
224*4882a593Smuzhiyun 			size_t ofs;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 			/* note: just in case someone wrote the magic character
227*4882a593Smuzhiyun 			 * five months ago... */
228*4882a593Smuzhiyun 			wdt_expect_close = 0;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 			/* now scan */
231*4882a593Smuzhiyun 			for (ofs = 0; ofs != count; ofs++) {
232*4882a593Smuzhiyun 				char c;
233*4882a593Smuzhiyun 				if (get_user(c, buf + ofs))
234*4882a593Smuzhiyun 					return -EFAULT;
235*4882a593Smuzhiyun 				if (c == 'V')
236*4882a593Smuzhiyun 					wdt_expect_close = 42;
237*4882a593Smuzhiyun 			}
238*4882a593Smuzhiyun 		}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 		/* Well, anyhow someone wrote to us, we should
241*4882a593Smuzhiyun 		   return that favour */
242*4882a593Smuzhiyun 		wdt_keepalive();
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 	return count;
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun 
fop_open(struct inode * inode,struct file * file)247*4882a593Smuzhiyun static int fop_open(struct inode *inode, struct file *file)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun 	/* Just in case we're already talking to someone... */
250*4882a593Smuzhiyun 	if (test_and_set_bit(0, &wdt_is_open))
251*4882a593Smuzhiyun 		return -EBUSY;
252*4882a593Smuzhiyun 	if (nowayout)
253*4882a593Smuzhiyun 		__module_get(THIS_MODULE);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	/* Good, fire up the show */
256*4882a593Smuzhiyun 	wdt_startup();
257*4882a593Smuzhiyun 	return stream_open(inode, file);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
fop_close(struct inode * inode,struct file * file)260*4882a593Smuzhiyun static int fop_close(struct inode *inode, struct file *file)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	if (wdt_expect_close == 42)
263*4882a593Smuzhiyun 		wdt_turnoff();
264*4882a593Smuzhiyun 	else {
265*4882a593Smuzhiyun 		pr_crit("Unexpected close, not stopping watchdog!\n");
266*4882a593Smuzhiyun 		wdt_keepalive();
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 	clear_bit(0, &wdt_is_open);
269*4882a593Smuzhiyun 	wdt_expect_close = 0;
270*4882a593Smuzhiyun 	return 0;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
fop_ioctl(struct file * file,unsigned int cmd,unsigned long arg)273*4882a593Smuzhiyun static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun 	void __user *argp = (void __user *)arg;
276*4882a593Smuzhiyun 	int __user *p = argp;
277*4882a593Smuzhiyun 	static const struct watchdog_info ident = {
278*4882a593Smuzhiyun 		.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
279*4882a593Smuzhiyun 							| WDIOF_MAGICCLOSE,
280*4882a593Smuzhiyun 		.firmware_version = 1,
281*4882a593Smuzhiyun 		.identity = "SC520",
282*4882a593Smuzhiyun 	};
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	switch (cmd) {
285*4882a593Smuzhiyun 	case WDIOC_GETSUPPORT:
286*4882a593Smuzhiyun 		return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
287*4882a593Smuzhiyun 	case WDIOC_GETSTATUS:
288*4882a593Smuzhiyun 	case WDIOC_GETBOOTSTATUS:
289*4882a593Smuzhiyun 		return put_user(0, p);
290*4882a593Smuzhiyun 	case WDIOC_SETOPTIONS:
291*4882a593Smuzhiyun 	{
292*4882a593Smuzhiyun 		int new_options, retval = -EINVAL;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 		if (get_user(new_options, p))
295*4882a593Smuzhiyun 			return -EFAULT;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 		if (new_options & WDIOS_DISABLECARD) {
298*4882a593Smuzhiyun 			wdt_turnoff();
299*4882a593Smuzhiyun 			retval = 0;
300*4882a593Smuzhiyun 		}
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 		if (new_options & WDIOS_ENABLECARD) {
303*4882a593Smuzhiyun 			wdt_startup();
304*4882a593Smuzhiyun 			retval = 0;
305*4882a593Smuzhiyun 		}
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 		return retval;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 	case WDIOC_KEEPALIVE:
310*4882a593Smuzhiyun 		wdt_keepalive();
311*4882a593Smuzhiyun 		return 0;
312*4882a593Smuzhiyun 	case WDIOC_SETTIMEOUT:
313*4882a593Smuzhiyun 	{
314*4882a593Smuzhiyun 		int new_timeout;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 		if (get_user(new_timeout, p))
317*4882a593Smuzhiyun 			return -EFAULT;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 		if (wdt_set_heartbeat(new_timeout))
320*4882a593Smuzhiyun 			return -EINVAL;
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 		wdt_keepalive();
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 		fallthrough;
325*4882a593Smuzhiyun 	case WDIOC_GETTIMEOUT:
326*4882a593Smuzhiyun 		return put_user(timeout, p);
327*4882a593Smuzhiyun 	default:
328*4882a593Smuzhiyun 		return -ENOTTY;
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun static const struct file_operations wdt_fops = {
333*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
334*4882a593Smuzhiyun 	.llseek		= no_llseek,
335*4882a593Smuzhiyun 	.write		= fop_write,
336*4882a593Smuzhiyun 	.open		= fop_open,
337*4882a593Smuzhiyun 	.release	= fop_close,
338*4882a593Smuzhiyun 	.unlocked_ioctl	= fop_ioctl,
339*4882a593Smuzhiyun 	.compat_ioctl	= compat_ptr_ioctl,
340*4882a593Smuzhiyun };
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun static struct miscdevice wdt_miscdev = {
343*4882a593Smuzhiyun 	.minor	= WATCHDOG_MINOR,
344*4882a593Smuzhiyun 	.name	= "watchdog",
345*4882a593Smuzhiyun 	.fops	= &wdt_fops,
346*4882a593Smuzhiyun };
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun /*
349*4882a593Smuzhiyun  *	Notifier for system down
350*4882a593Smuzhiyun  */
351*4882a593Smuzhiyun 
wdt_notify_sys(struct notifier_block * this,unsigned long code,void * unused)352*4882a593Smuzhiyun static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
353*4882a593Smuzhiyun 	void *unused)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	if (code == SYS_DOWN || code == SYS_HALT)
356*4882a593Smuzhiyun 		wdt_turnoff();
357*4882a593Smuzhiyun 	return NOTIFY_DONE;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun /*
361*4882a593Smuzhiyun  *	The WDT needs to learn about soft shutdowns in order to
362*4882a593Smuzhiyun  *	turn the timebomb registers off.
363*4882a593Smuzhiyun  */
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun static struct notifier_block wdt_notifier = {
366*4882a593Smuzhiyun 	.notifier_call = wdt_notify_sys,
367*4882a593Smuzhiyun };
368*4882a593Smuzhiyun 
sc520_wdt_unload(void)369*4882a593Smuzhiyun static void __exit sc520_wdt_unload(void)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	if (!nowayout)
372*4882a593Smuzhiyun 		wdt_turnoff();
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	/* Deregister */
375*4882a593Smuzhiyun 	misc_deregister(&wdt_miscdev);
376*4882a593Smuzhiyun 	unregister_reboot_notifier(&wdt_notifier);
377*4882a593Smuzhiyun 	iounmap(wdtmrctl);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
sc520_wdt_init(void)380*4882a593Smuzhiyun static int __init sc520_wdt_init(void)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	int rc = -EBUSY;
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/* Check that the timeout value is within it's range ;
385*4882a593Smuzhiyun 	   if not reset to the default */
386*4882a593Smuzhiyun 	if (wdt_set_heartbeat(timeout)) {
387*4882a593Smuzhiyun 		wdt_set_heartbeat(WATCHDOG_TIMEOUT);
388*4882a593Smuzhiyun 		pr_info("timeout value must be 1 <= timeout <= 3600, using %d\n",
389*4882a593Smuzhiyun 			WATCHDOG_TIMEOUT);
390*4882a593Smuzhiyun 	}
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	wdtmrctl = ioremap(MMCR_BASE + OFFS_WDTMRCTL, 2);
393*4882a593Smuzhiyun 	if (!wdtmrctl) {
394*4882a593Smuzhiyun 		pr_err("Unable to remap memory\n");
395*4882a593Smuzhiyun 		rc = -ENOMEM;
396*4882a593Smuzhiyun 		goto err_out_region2;
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	rc = register_reboot_notifier(&wdt_notifier);
400*4882a593Smuzhiyun 	if (rc) {
401*4882a593Smuzhiyun 		pr_err("cannot register reboot notifier (err=%d)\n", rc);
402*4882a593Smuzhiyun 		goto err_out_ioremap;
403*4882a593Smuzhiyun 	}
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	rc = misc_register(&wdt_miscdev);
406*4882a593Smuzhiyun 	if (rc) {
407*4882a593Smuzhiyun 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
408*4882a593Smuzhiyun 		       WATCHDOG_MINOR, rc);
409*4882a593Smuzhiyun 		goto err_out_notifier;
410*4882a593Smuzhiyun 	}
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	pr_info("WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n",
413*4882a593Smuzhiyun 		timeout, nowayout);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	return 0;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun err_out_notifier:
418*4882a593Smuzhiyun 	unregister_reboot_notifier(&wdt_notifier);
419*4882a593Smuzhiyun err_out_ioremap:
420*4882a593Smuzhiyun 	iounmap(wdtmrctl);
421*4882a593Smuzhiyun err_out_region2:
422*4882a593Smuzhiyun 	return rc;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun module_init(sc520_wdt_init);
426*4882a593Smuzhiyun module_exit(sc520_wdt_unload);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun MODULE_AUTHOR("Scott and Bill Jennings");
429*4882a593Smuzhiyun MODULE_DESCRIPTION(
430*4882a593Smuzhiyun 	"Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor");
431*4882a593Smuzhiyun MODULE_LICENSE("GPL");
432