xref: /linux/drivers/watchdog/shwdt.c (revision 25985edcedea6396277003854657b5f3cb31a628)
1b7e04f8cSWim Van Sebroeck /*
2b1fa888eSPaul Mundt  * drivers/watchdog/shwdt.c
3b7e04f8cSWim Van Sebroeck  *
4b7e04f8cSWim Van Sebroeck  * Watchdog driver for integrated watchdog in the SuperH processors.
5b7e04f8cSWim Van Sebroeck  *
6b1fa888eSPaul Mundt  * Copyright (C) 2001 - 2010  Paul Mundt <lethal@linux-sh.org>
7b7e04f8cSWim Van Sebroeck  *
8b7e04f8cSWim Van Sebroeck  * This program is free software; you can redistribute it and/or modify it
9b7e04f8cSWim Van Sebroeck  * under the terms of the GNU General Public License as published by the
10b7e04f8cSWim Van Sebroeck  * Free Software Foundation; either version 2 of the License, or (at your
11b7e04f8cSWim Van Sebroeck  * option) any later version.
12b7e04f8cSWim Van Sebroeck  *
13b7e04f8cSWim Van Sebroeck  * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
14b7e04f8cSWim Van Sebroeck  *     Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
15b7e04f8cSWim Van Sebroeck  *
16b7e04f8cSWim Van Sebroeck  * 19-Apr-2002 Rob Radez <rob@osinvestor.com>
17b7e04f8cSWim Van Sebroeck  *     Added expect close support, made emulated timeout runtime changeable
18b7e04f8cSWim Van Sebroeck  *     general cleanups, add some ioctls
19b7e04f8cSWim Van Sebroeck  */
20b7e04f8cSWim Van Sebroeck #include <linux/module.h>
21b7e04f8cSWim Van Sebroeck #include <linux/moduleparam.h>
228f5585ecSPaul Mundt #include <linux/platform_device.h>
23b7e04f8cSWim Van Sebroeck #include <linux/init.h>
24b7e04f8cSWim Van Sebroeck #include <linux/types.h>
25b7e04f8cSWim Van Sebroeck #include <linux/miscdevice.h>
26b7e04f8cSWim Van Sebroeck #include <linux/watchdog.h>
27b7e04f8cSWim Van Sebroeck #include <linux/reboot.h>
28b7e04f8cSWim Van Sebroeck #include <linux/notifier.h>
29b7e04f8cSWim Van Sebroeck #include <linux/ioport.h>
30b7e04f8cSWim Van Sebroeck #include <linux/fs.h>
31b7e04f8cSWim Van Sebroeck #include <linux/mm.h>
328f5585ecSPaul Mundt #include <linux/slab.h>
3370b814ecSAlan Cox #include <linux/io.h>
3470b814ecSAlan Cox #include <linux/uaccess.h>
3558cf4198SAdrian Bunk #include <asm/watchdog.h>
36b7e04f8cSWim Van Sebroeck 
378f5585ecSPaul Mundt #define DRV_NAME "sh-wdt"
38b7e04f8cSWim Van Sebroeck 
39b7e04f8cSWim Van Sebroeck /*
40b7e04f8cSWim Van Sebroeck  * Default clock division ratio is 5.25 msecs. For an additional table of
41b7e04f8cSWim Van Sebroeck  * values, consult the asm-sh/watchdog.h. Overload this at module load
42b7e04f8cSWim Van Sebroeck  * time.
43b7e04f8cSWim Van Sebroeck  *
44b7e04f8cSWim Van Sebroeck  * In order for this to work reliably we need to have HZ set to 1000 or
45b7e04f8cSWim Van Sebroeck  * something quite higher than 100 (or we need a proper high-res timer
46b7e04f8cSWim Van Sebroeck  * implementation that will deal with this properly), otherwise the 10ms
47b7e04f8cSWim Van Sebroeck  * resolution of a jiffy is enough to trigger the overflow. For things like
48b7e04f8cSWim Van Sebroeck  * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
49b7e04f8cSWim Van Sebroeck  * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
50b7e04f8cSWim Van Sebroeck  * necssary.
51b7e04f8cSWim Van Sebroeck  *
52b7e04f8cSWim Van Sebroeck  * As a result of this timing problem, the only modes that are particularly
53*25985edcSLucas De Marchi  * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
54b7e04f8cSWim Van Sebroeck  * overflow periods respectively.
55b7e04f8cSWim Van Sebroeck  *
56b7e04f8cSWim Van Sebroeck  * Also, since we can't really expect userspace to be responsive enough
57ee0fc097SJoe Perches  * before the overflow happens, we maintain two separate timers .. One in
58b7e04f8cSWim Van Sebroeck  * the kernel for clearing out WOVF every 2ms or so (again, this depends on
59b7e04f8cSWim Van Sebroeck  * HZ == 1000), and another for monitoring userspace writes to the WDT device.
60b7e04f8cSWim Van Sebroeck  *
61b7e04f8cSWim Van Sebroeck  * As such, we currently use a configurable heartbeat interval which defaults
62b7e04f8cSWim Van Sebroeck  * to 30s. In this case, the userspace daemon is only responsible for periodic
63b7e04f8cSWim Van Sebroeck  * writes to the device before the next heartbeat is scheduled. If the daemon
64b7e04f8cSWim Van Sebroeck  * misses its deadline, the kernel timer will allow the WDT to overflow.
65b7e04f8cSWim Van Sebroeck  */
66b7e04f8cSWim Van Sebroeck static int clock_division_ratio = WTCSR_CKS_4096;
67b7e04f8cSWim Van Sebroeck #define next_ping_period(cks)	msecs_to_jiffies(cks - 4)
68b7e04f8cSWim Van Sebroeck 
6958cf4198SAdrian Bunk static const struct watchdog_info sh_wdt_info;
708f5585ecSPaul Mundt static struct platform_device *sh_wdt_dev;
7170b814ecSAlan Cox static DEFINE_SPINLOCK(shwdt_lock);
72b7e04f8cSWim Van Sebroeck 
73b7e04f8cSWim Van Sebroeck #define WATCHDOG_HEARTBEAT 30			/* 30 sec default heartbeat */
74b7e04f8cSWim Van Sebroeck static int heartbeat = WATCHDOG_HEARTBEAT;	/* in seconds */
75b7e04f8cSWim Van Sebroeck static int nowayout = WATCHDOG_NOWAYOUT;
768f5585ecSPaul Mundt static unsigned long next_heartbeat;
77b7e04f8cSWim Van Sebroeck 
788f5585ecSPaul Mundt struct sh_wdt {
798f5585ecSPaul Mundt 	void __iomem		*base;
808f5585ecSPaul Mundt 	struct device		*dev;
818f5585ecSPaul Mundt 
828f5585ecSPaul Mundt 	struct timer_list	timer;
838f5585ecSPaul Mundt 
848f5585ecSPaul Mundt 	unsigned long		enabled;
858f5585ecSPaul Mundt 	char			expect_close;
868f5585ecSPaul Mundt };
878f5585ecSPaul Mundt 
888f5585ecSPaul Mundt static void sh_wdt_start(struct sh_wdt *wdt)
89b7e04f8cSWim Van Sebroeck {
9070b814ecSAlan Cox 	unsigned long flags;
918f5585ecSPaul Mundt 	u8 csr;
9270b814ecSAlan Cox 
9358cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
94b7e04f8cSWim Van Sebroeck 
95b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
968f5585ecSPaul Mundt 	mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
97b7e04f8cSWim Van Sebroeck 
98b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
99b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_WT | clock_division_ratio;
100b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
101b7e04f8cSWim Van Sebroeck 
102b7e04f8cSWim Van Sebroeck 	sh_wdt_write_cnt(0);
103b7e04f8cSWim Van Sebroeck 
104b7e04f8cSWim Van Sebroeck 	/*
105b7e04f8cSWim Van Sebroeck 	 * These processors have a bit of an inconsistent initialization
106b7e04f8cSWim Van Sebroeck 	 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
107b7e04f8cSWim Van Sebroeck 	 * RSTCSR register was removed.
108b7e04f8cSWim Van Sebroeck 	 *
109b7e04f8cSWim Van Sebroeck 	 * On the SH-2 however, in addition with bits being in different
110b7e04f8cSWim Van Sebroeck 	 * locations, we must deal with RSTCSR outright..
111b7e04f8cSWim Van Sebroeck 	 */
112b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
113b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_TME;
114b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_RSTS;
115b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
116b7e04f8cSWim Van Sebroeck 
117b7e04f8cSWim Van Sebroeck #ifdef CONFIG_CPU_SH2
118b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_rstcsr();
119b7e04f8cSWim Van Sebroeck 	csr &= ~RSTCSR_RSTS;
120b7e04f8cSWim Van Sebroeck 	sh_wdt_write_rstcsr(csr);
121b7e04f8cSWim Van Sebroeck #endif
12258cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
123b7e04f8cSWim Van Sebroeck }
124b7e04f8cSWim Van Sebroeck 
1258f5585ecSPaul Mundt static void sh_wdt_stop(struct sh_wdt *wdt)
126b7e04f8cSWim Van Sebroeck {
12770b814ecSAlan Cox 	unsigned long flags;
1288f5585ecSPaul Mundt 	u8 csr;
12970b814ecSAlan Cox 
13058cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
131b7e04f8cSWim Van Sebroeck 
1328f5585ecSPaul Mundt 	del_timer(&wdt->timer);
133b7e04f8cSWim Van Sebroeck 
134b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
135b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_TME;
136b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
1378f5585ecSPaul Mundt 
13858cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
139b7e04f8cSWim Van Sebroeck }
140b7e04f8cSWim Van Sebroeck 
1418f5585ecSPaul Mundt static inline void sh_wdt_keepalive(struct sh_wdt *wdt)
142b7e04f8cSWim Van Sebroeck {
14370b814ecSAlan Cox 	unsigned long flags;
14470b814ecSAlan Cox 
14558cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
146b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
14758cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
148b7e04f8cSWim Van Sebroeck }
149b7e04f8cSWim Van Sebroeck 
150b7e04f8cSWim Van Sebroeck static int sh_wdt_set_heartbeat(int t)
151b7e04f8cSWim Van Sebroeck {
15270b814ecSAlan Cox 	unsigned long flags;
15370b814ecSAlan Cox 
15470b814ecSAlan Cox 	if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
155b7e04f8cSWim Van Sebroeck 		return -EINVAL;
156b7e04f8cSWim Van Sebroeck 
15758cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
158b7e04f8cSWim Van Sebroeck 	heartbeat = t;
15958cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
160b7e04f8cSWim Van Sebroeck 	return 0;
161b7e04f8cSWim Van Sebroeck }
162b7e04f8cSWim Van Sebroeck 
163b7e04f8cSWim Van Sebroeck static void sh_wdt_ping(unsigned long data)
164b7e04f8cSWim Van Sebroeck {
1658f5585ecSPaul Mundt 	struct sh_wdt *wdt = (struct sh_wdt *)data;
16670b814ecSAlan Cox 	unsigned long flags;
16770b814ecSAlan Cox 
16858cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
169b7e04f8cSWim Van Sebroeck 	if (time_before(jiffies, next_heartbeat)) {
1708f5585ecSPaul Mundt 		u8 csr;
171b7e04f8cSWim Van Sebroeck 
172b7e04f8cSWim Van Sebroeck 		csr = sh_wdt_read_csr();
173b7e04f8cSWim Van Sebroeck 		csr &= ~WTCSR_IOVF;
174b7e04f8cSWim Van Sebroeck 		sh_wdt_write_csr(csr);
175b7e04f8cSWim Van Sebroeck 
176b7e04f8cSWim Van Sebroeck 		sh_wdt_write_cnt(0);
177b7e04f8cSWim Van Sebroeck 
1788f5585ecSPaul Mundt 		mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
179b7e04f8cSWim Van Sebroeck 	} else
1808f5585ecSPaul Mundt 		dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
181b7e04f8cSWim Van Sebroeck 		         "the watchdog\n");
18258cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
183b7e04f8cSWim Van Sebroeck }
184b7e04f8cSWim Van Sebroeck 
185b7e04f8cSWim Van Sebroeck static int sh_wdt_open(struct inode *inode, struct file *file)
186b7e04f8cSWim Van Sebroeck {
1878f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev);
1888f5585ecSPaul Mundt 
1898f5585ecSPaul Mundt 	if (test_and_set_bit(0, &wdt->enabled))
190b7e04f8cSWim Van Sebroeck 		return -EBUSY;
191b7e04f8cSWim Van Sebroeck 	if (nowayout)
192b7e04f8cSWim Van Sebroeck 		__module_get(THIS_MODULE);
193b7e04f8cSWim Van Sebroeck 
1948f5585ecSPaul Mundt 	file->private_data = wdt;
1958f5585ecSPaul Mundt 
1968f5585ecSPaul Mundt 	sh_wdt_start(wdt);
197b7e04f8cSWim Van Sebroeck 
198b7e04f8cSWim Van Sebroeck 	return nonseekable_open(inode, file);
199b7e04f8cSWim Van Sebroeck }
200b7e04f8cSWim Van Sebroeck 
201b7e04f8cSWim Van Sebroeck static int sh_wdt_close(struct inode *inode, struct file *file)
202b7e04f8cSWim Van Sebroeck {
2038f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
2048f5585ecSPaul Mundt 
2058f5585ecSPaul Mundt 	if (wdt->expect_close == 42) {
2068f5585ecSPaul Mundt 		sh_wdt_stop(wdt);
207b7e04f8cSWim Van Sebroeck 	} else {
2088f5585ecSPaul Mundt 		dev_crit(wdt->dev, "Unexpected close, not "
209b7e04f8cSWim Van Sebroeck 		         "stopping watchdog!\n");
2108f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
211b7e04f8cSWim Van Sebroeck 	}
212b7e04f8cSWim Van Sebroeck 
2138f5585ecSPaul Mundt 	clear_bit(0, &wdt->enabled);
2148f5585ecSPaul Mundt 	wdt->expect_close = 0;
215b7e04f8cSWim Van Sebroeck 
216b7e04f8cSWim Van Sebroeck 	return 0;
217b7e04f8cSWim Van Sebroeck }
218b7e04f8cSWim Van Sebroeck 
219b7e04f8cSWim Van Sebroeck static ssize_t sh_wdt_write(struct file *file, const char *buf,
220b7e04f8cSWim Van Sebroeck 			    size_t count, loff_t *ppos)
221b7e04f8cSWim Van Sebroeck {
2228f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
2238f5585ecSPaul Mundt 
224b7e04f8cSWim Van Sebroeck 	if (count) {
225b7e04f8cSWim Van Sebroeck 		if (!nowayout) {
226b7e04f8cSWim Van Sebroeck 			size_t i;
227b7e04f8cSWim Van Sebroeck 
2288f5585ecSPaul Mundt 			wdt->expect_close = 0;
229b7e04f8cSWim Van Sebroeck 
230b7e04f8cSWim Van Sebroeck 			for (i = 0; i != count; i++) {
231b7e04f8cSWim Van Sebroeck 				char c;
232b7e04f8cSWim Van Sebroeck 				if (get_user(c, buf + i))
233b7e04f8cSWim Van Sebroeck 					return -EFAULT;
234b7e04f8cSWim Van Sebroeck 				if (c == 'V')
2358f5585ecSPaul Mundt 					wdt->expect_close = 42;
236b7e04f8cSWim Van Sebroeck 			}
237b7e04f8cSWim Van Sebroeck 		}
2388f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
239b7e04f8cSWim Van Sebroeck 	}
240b7e04f8cSWim Van Sebroeck 
241b7e04f8cSWim Van Sebroeck 	return count;
242b7e04f8cSWim Van Sebroeck }
243b7e04f8cSWim Van Sebroeck 
24470b814ecSAlan Cox static long sh_wdt_ioctl(struct file *file, unsigned int cmd,
24570b814ecSAlan Cox 							unsigned long arg)
246b7e04f8cSWim Van Sebroeck {
2478f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
248b7e04f8cSWim Van Sebroeck 	int new_heartbeat;
249b7e04f8cSWim Van Sebroeck 	int options, retval = -EINVAL;
250b7e04f8cSWim Van Sebroeck 
251b7e04f8cSWim Van Sebroeck 	switch (cmd) {
252b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSUPPORT:
253b7e04f8cSWim Van Sebroeck 		return copy_to_user((struct watchdog_info *)arg,
25470b814ecSAlan Cox 			  &sh_wdt_info, sizeof(sh_wdt_info)) ? -EFAULT : 0;
255b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSTATUS:
256b7e04f8cSWim Van Sebroeck 	case WDIOC_GETBOOTSTATUS:
257b7e04f8cSWim Van Sebroeck 		return put_user(0, (int *)arg);
258b7e04f8cSWim Van Sebroeck 	case WDIOC_SETOPTIONS:
259b7e04f8cSWim Van Sebroeck 		if (get_user(options, (int *)arg))
260b7e04f8cSWim Van Sebroeck 			return -EFAULT;
261b7e04f8cSWim Van Sebroeck 
262b7e04f8cSWim Van Sebroeck 		if (options & WDIOS_DISABLECARD) {
2638f5585ecSPaul Mundt 			sh_wdt_stop(wdt);
264b7e04f8cSWim Van Sebroeck 			retval = 0;
265b7e04f8cSWim Van Sebroeck 		}
266b7e04f8cSWim Van Sebroeck 
267b7e04f8cSWim Van Sebroeck 		if (options & WDIOS_ENABLECARD) {
2688f5585ecSPaul Mundt 			sh_wdt_start(wdt);
269b7e04f8cSWim Van Sebroeck 			retval = 0;
270b7e04f8cSWim Van Sebroeck 		}
271b7e04f8cSWim Van Sebroeck 
272b7e04f8cSWim Van Sebroeck 		return retval;
2730c06090cSWim Van Sebroeck 	case WDIOC_KEEPALIVE:
2748f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
2750c06090cSWim Van Sebroeck 		return 0;
2760c06090cSWim Van Sebroeck 	case WDIOC_SETTIMEOUT:
2770c06090cSWim Van Sebroeck 		if (get_user(new_heartbeat, (int *)arg))
2780c06090cSWim Van Sebroeck 			return -EFAULT;
2790c06090cSWim Van Sebroeck 
2800c06090cSWim Van Sebroeck 		if (sh_wdt_set_heartbeat(new_heartbeat))
2810c06090cSWim Van Sebroeck 			return -EINVAL;
2820c06090cSWim Van Sebroeck 
2838f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
2840c06090cSWim Van Sebroeck 		/* Fall */
2850c06090cSWim Van Sebroeck 	case WDIOC_GETTIMEOUT:
2860c06090cSWim Van Sebroeck 		return put_user(heartbeat, (int *)arg);
287b7e04f8cSWim Van Sebroeck 	default:
288b7e04f8cSWim Van Sebroeck 		return -ENOTTY;
289b7e04f8cSWim Van Sebroeck 	}
290b7e04f8cSWim Van Sebroeck 	return 0;
291b7e04f8cSWim Van Sebroeck }
292b7e04f8cSWim Van Sebroeck 
293b7e04f8cSWim Van Sebroeck static int sh_wdt_notify_sys(struct notifier_block *this,
294b7e04f8cSWim Van Sebroeck 			     unsigned long code, void *unused)
295b7e04f8cSWim Van Sebroeck {
2968f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev);
2978f5585ecSPaul Mundt 
298b7e04f8cSWim Van Sebroeck 	if (code == SYS_DOWN || code == SYS_HALT)
2998f5585ecSPaul Mundt 		sh_wdt_stop(wdt);
300b7e04f8cSWim Van Sebroeck 
301b7e04f8cSWim Van Sebroeck 	return NOTIFY_DONE;
302b7e04f8cSWim Van Sebroeck }
303b7e04f8cSWim Van Sebroeck 
304b7e04f8cSWim Van Sebroeck static const struct file_operations sh_wdt_fops = {
305b7e04f8cSWim Van Sebroeck 	.owner		= THIS_MODULE,
306b7e04f8cSWim Van Sebroeck 	.llseek		= no_llseek,
307b7e04f8cSWim Van Sebroeck 	.write		= sh_wdt_write,
30870b814ecSAlan Cox 	.unlocked_ioctl	= sh_wdt_ioctl,
309b7e04f8cSWim Van Sebroeck 	.open		= sh_wdt_open,
310b7e04f8cSWim Van Sebroeck 	.release	= sh_wdt_close,
311b7e04f8cSWim Van Sebroeck };
312b7e04f8cSWim Van Sebroeck 
31370b814ecSAlan Cox static const struct watchdog_info sh_wdt_info = {
314b7e04f8cSWim Van Sebroeck 	.options		= WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
315b7e04f8cSWim Van Sebroeck 				  WDIOF_MAGICCLOSE,
316b7e04f8cSWim Van Sebroeck 	.firmware_version	= 1,
317b7e04f8cSWim Van Sebroeck 	.identity		= "SH WDT",
318b7e04f8cSWim Van Sebroeck };
319b7e04f8cSWim Van Sebroeck 
320b7e04f8cSWim Van Sebroeck static struct notifier_block sh_wdt_notifier = {
321b7e04f8cSWim Van Sebroeck 	.notifier_call		= sh_wdt_notify_sys,
322b7e04f8cSWim Van Sebroeck };
323b7e04f8cSWim Van Sebroeck 
324b7e04f8cSWim Van Sebroeck static struct miscdevice sh_wdt_miscdev = {
325b7e04f8cSWim Van Sebroeck 	.minor		= WATCHDOG_MINOR,
326b7e04f8cSWim Van Sebroeck 	.name		= "watchdog",
327b7e04f8cSWim Van Sebroeck 	.fops		= &sh_wdt_fops,
328b7e04f8cSWim Van Sebroeck };
329b7e04f8cSWim Van Sebroeck 
3308f5585ecSPaul Mundt static int __devinit sh_wdt_probe(struct platform_device *pdev)
3318f5585ecSPaul Mundt {
3328f5585ecSPaul Mundt 	struct sh_wdt *wdt;
3338f5585ecSPaul Mundt 	struct resource *res;
3348f5585ecSPaul Mundt 	int rc;
3358f5585ecSPaul Mundt 
3368f5585ecSPaul Mundt 	/*
3378f5585ecSPaul Mundt 	 * As this driver only covers the global watchdog case, reject
3388f5585ecSPaul Mundt 	 * any attempts to register per-CPU watchdogs.
3398f5585ecSPaul Mundt 	 */
3408f5585ecSPaul Mundt 	if (pdev->id != -1)
3418f5585ecSPaul Mundt 		return -EINVAL;
3428f5585ecSPaul Mundt 
3438f5585ecSPaul Mundt 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3448f5585ecSPaul Mundt 	if (unlikely(!res))
3458f5585ecSPaul Mundt 		return -EINVAL;
3468f5585ecSPaul Mundt 
3478f5585ecSPaul Mundt 	if (!devm_request_mem_region(&pdev->dev, res->start,
3488f5585ecSPaul Mundt 				     resource_size(res), DRV_NAME))
3498f5585ecSPaul Mundt 		return -EBUSY;
3508f5585ecSPaul Mundt 
3518f5585ecSPaul Mundt 	wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
3528f5585ecSPaul Mundt 	if (unlikely(!wdt)) {
3538f5585ecSPaul Mundt 		rc = -ENOMEM;
3548f5585ecSPaul Mundt 		goto out_release;
3558f5585ecSPaul Mundt 	}
3568f5585ecSPaul Mundt 
3578f5585ecSPaul Mundt 	wdt->dev = &pdev->dev;
3588f5585ecSPaul Mundt 
3598f5585ecSPaul Mundt 	wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
3608f5585ecSPaul Mundt 	if (unlikely(!wdt->base)) {
3618f5585ecSPaul Mundt 		rc = -ENXIO;
3628f5585ecSPaul Mundt 		goto out_err;
3638f5585ecSPaul Mundt 	}
3648f5585ecSPaul Mundt 
3658f5585ecSPaul Mundt 	rc = register_reboot_notifier(&sh_wdt_notifier);
3668f5585ecSPaul Mundt 	if (unlikely(rc)) {
3678f5585ecSPaul Mundt 		dev_err(&pdev->dev,
3688f5585ecSPaul Mundt 			"Can't register reboot notifier (err=%d)\n", rc);
3698f5585ecSPaul Mundt 		goto out_unmap;
3708f5585ecSPaul Mundt 	}
3718f5585ecSPaul Mundt 
3728f5585ecSPaul Mundt 	sh_wdt_miscdev.parent = wdt->dev;
3738f5585ecSPaul Mundt 
3748f5585ecSPaul Mundt 	rc = misc_register(&sh_wdt_miscdev);
3758f5585ecSPaul Mundt 	if (unlikely(rc)) {
3768f5585ecSPaul Mundt 		dev_err(&pdev->dev,
3778f5585ecSPaul Mundt 			"Can't register miscdev on minor=%d (err=%d)\n",
3788f5585ecSPaul Mundt 						sh_wdt_miscdev.minor, rc);
3798f5585ecSPaul Mundt 		goto out_unreg;
3808f5585ecSPaul Mundt 	}
3818f5585ecSPaul Mundt 
3828f5585ecSPaul Mundt 	init_timer(&wdt->timer);
3838f5585ecSPaul Mundt 	wdt->timer.function	= sh_wdt_ping;
3848f5585ecSPaul Mundt 	wdt->timer.data		= (unsigned long)wdt;
3858f5585ecSPaul Mundt 	wdt->timer.expires	= next_ping_period(clock_division_ratio);
3868f5585ecSPaul Mundt 
3878f5585ecSPaul Mundt 	platform_set_drvdata(pdev, wdt);
3888f5585ecSPaul Mundt 	sh_wdt_dev = pdev;
3898f5585ecSPaul Mundt 
3908f5585ecSPaul Mundt 	dev_info(&pdev->dev, "initialized.\n");
3918f5585ecSPaul Mundt 
3928f5585ecSPaul Mundt 	return 0;
3938f5585ecSPaul Mundt 
3948f5585ecSPaul Mundt out_unreg:
3958f5585ecSPaul Mundt 	unregister_reboot_notifier(&sh_wdt_notifier);
3968f5585ecSPaul Mundt out_unmap:
3978f5585ecSPaul Mundt 	devm_iounmap(&pdev->dev, wdt->base);
3988f5585ecSPaul Mundt out_err:
3998f5585ecSPaul Mundt 	devm_kfree(&pdev->dev, wdt);
4008f5585ecSPaul Mundt out_release:
4018f5585ecSPaul Mundt 	devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
4028f5585ecSPaul Mundt 
4038f5585ecSPaul Mundt 	return rc;
4048f5585ecSPaul Mundt }
4058f5585ecSPaul Mundt 
4068f5585ecSPaul Mundt static int __devexit sh_wdt_remove(struct platform_device *pdev)
4078f5585ecSPaul Mundt {
4088f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(pdev);
4098f5585ecSPaul Mundt 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4108f5585ecSPaul Mundt 
4118f5585ecSPaul Mundt 	platform_set_drvdata(pdev, NULL);
4128f5585ecSPaul Mundt 
4138f5585ecSPaul Mundt 	misc_deregister(&sh_wdt_miscdev);
4148f5585ecSPaul Mundt 
4158f5585ecSPaul Mundt 	sh_wdt_dev = NULL;
4168f5585ecSPaul Mundt 
4178f5585ecSPaul Mundt 	unregister_reboot_notifier(&sh_wdt_notifier);
4188f5585ecSPaul Mundt 	devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
4198f5585ecSPaul Mundt 	devm_iounmap(&pdev->dev, wdt->base);
4208f5585ecSPaul Mundt 	devm_kfree(&pdev->dev, wdt);
4218f5585ecSPaul Mundt 
4228f5585ecSPaul Mundt 	return 0;
4238f5585ecSPaul Mundt }
4248f5585ecSPaul Mundt 
4258f5585ecSPaul Mundt static struct platform_driver sh_wdt_driver = {
4268f5585ecSPaul Mundt 	.driver		= {
4278f5585ecSPaul Mundt 		.name	= DRV_NAME,
4288f5585ecSPaul Mundt 		.owner	= THIS_MODULE,
4298f5585ecSPaul Mundt 	},
4308f5585ecSPaul Mundt 
4318f5585ecSPaul Mundt 	.probe	= sh_wdt_probe,
4328f5585ecSPaul Mundt 	.remove	= __devexit_p(sh_wdt_remove),
4338f5585ecSPaul Mundt };
4348f5585ecSPaul Mundt 
435b7e04f8cSWim Van Sebroeck static int __init sh_wdt_init(void)
436b7e04f8cSWim Van Sebroeck {
437b7e04f8cSWim Van Sebroeck 	int rc;
438b7e04f8cSWim Van Sebroeck 
4398f5585ecSPaul Mundt 	if (unlikely(clock_division_ratio < 0x5 ||
4408f5585ecSPaul Mundt 		     clock_division_ratio > 0x7)) {
441b7e04f8cSWim Van Sebroeck 		clock_division_ratio = WTCSR_CKS_4096;
4428f5585ecSPaul Mundt 
4438f5585ecSPaul Mundt 		pr_info("%s: divisor must be 0x5<=x<=0x7, using %d\n",
4448f5585ecSPaul Mundt 			 DRV_NAME, clock_division_ratio);
445b7e04f8cSWim Van Sebroeck 	}
446b7e04f8cSWim Van Sebroeck 
447b7e04f8cSWim Van Sebroeck 	rc = sh_wdt_set_heartbeat(heartbeat);
448b7e04f8cSWim Van Sebroeck 	if (unlikely(rc)) {
449b7e04f8cSWim Van Sebroeck 		heartbeat = WATCHDOG_HEARTBEAT;
4508f5585ecSPaul Mundt 
4518f5585ecSPaul Mundt 		pr_info("%s: heartbeat value must be 1<=x<=3600, using %d\n",
4528f5585ecSPaul Mundt 			DRV_NAME, heartbeat);
453b7e04f8cSWim Van Sebroeck 	}
454b7e04f8cSWim Van Sebroeck 
4558f5585ecSPaul Mundt 	pr_info("%s: configured with heartbeat=%d sec (nowayout=%d)\n",
4568f5585ecSPaul Mundt 		DRV_NAME, heartbeat, nowayout);
457b7e04f8cSWim Van Sebroeck 
4588f5585ecSPaul Mundt 	return platform_driver_register(&sh_wdt_driver);
459b7e04f8cSWim Van Sebroeck }
460b7e04f8cSWim Van Sebroeck 
461b7e04f8cSWim Van Sebroeck static void __exit sh_wdt_exit(void)
462b7e04f8cSWim Van Sebroeck {
4638f5585ecSPaul Mundt 	platform_driver_unregister(&sh_wdt_driver);
464b7e04f8cSWim Van Sebroeck }
4658f5585ecSPaul Mundt module_init(sh_wdt_init);
4668f5585ecSPaul Mundt module_exit(sh_wdt_exit);
467b7e04f8cSWim Van Sebroeck 
468b7e04f8cSWim Van Sebroeck MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
469b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("SuperH watchdog driver");
470b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL");
4718f5585ecSPaul Mundt MODULE_ALIAS("platform:" DRV_NAME);
472b7e04f8cSWim Van Sebroeck MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
473b7e04f8cSWim Van Sebroeck 
474b7e04f8cSWim Van Sebroeck module_param(clock_division_ratio, int, 0);
475a77dba7eSWim Van Sebroeck MODULE_PARM_DESC(clock_division_ratio,
476a77dba7eSWim Van Sebroeck 	"Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
47776550d32SRandy Dunlap 	"to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
478b7e04f8cSWim Van Sebroeck 
479b7e04f8cSWim Van Sebroeck module_param(heartbeat, int, 0);
48070b814ecSAlan Cox MODULE_PARM_DESC(heartbeat,
48170b814ecSAlan Cox 	"Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
48270b814ecSAlan Cox 				__MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
483b7e04f8cSWim Van Sebroeck 
484b7e04f8cSWim Van Sebroeck module_param(nowayout, int, 0);
48570b814ecSAlan Cox MODULE_PARM_DESC(nowayout,
48670b814ecSAlan Cox 	"Watchdog cannot be stopped once started (default="
48770b814ecSAlan Cox 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
488