xref: /linux/drivers/watchdog/shwdt.c (revision 27c766aaacb265d625dc634bf7903f7f9fd0c697)
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  */
20*27c766aaSJoe Perches 
21*27c766aaSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22*27c766aaSJoe Perches 
23b7e04f8cSWim Van Sebroeck #include <linux/module.h>
24b7e04f8cSWim Van Sebroeck #include <linux/moduleparam.h>
258f5585ecSPaul Mundt #include <linux/platform_device.h>
26b7e04f8cSWim Van Sebroeck #include <linux/init.h>
27b7e04f8cSWim Van Sebroeck #include <linux/types.h>
28b7e04f8cSWim Van Sebroeck #include <linux/miscdevice.h>
29b7e04f8cSWim Van Sebroeck #include <linux/watchdog.h>
30b7e04f8cSWim Van Sebroeck #include <linux/reboot.h>
31b7e04f8cSWim Van Sebroeck #include <linux/notifier.h>
32b7e04f8cSWim Van Sebroeck #include <linux/ioport.h>
33b7e04f8cSWim Van Sebroeck #include <linux/fs.h>
34b7e04f8cSWim Van Sebroeck #include <linux/mm.h>
358f5585ecSPaul Mundt #include <linux/slab.h>
3670b814ecSAlan Cox #include <linux/io.h>
3770b814ecSAlan Cox #include <linux/uaccess.h>
3858cf4198SAdrian Bunk #include <asm/watchdog.h>
39b7e04f8cSWim Van Sebroeck 
408f5585ecSPaul Mundt #define DRV_NAME "sh-wdt"
41b7e04f8cSWim Van Sebroeck 
42b7e04f8cSWim Van Sebroeck /*
43b7e04f8cSWim Van Sebroeck  * Default clock division ratio is 5.25 msecs. For an additional table of
44b7e04f8cSWim Van Sebroeck  * values, consult the asm-sh/watchdog.h. Overload this at module load
45b7e04f8cSWim Van Sebroeck  * time.
46b7e04f8cSWim Van Sebroeck  *
47b7e04f8cSWim Van Sebroeck  * In order for this to work reliably we need to have HZ set to 1000 or
48b7e04f8cSWim Van Sebroeck  * something quite higher than 100 (or we need a proper high-res timer
49b7e04f8cSWim Van Sebroeck  * implementation that will deal with this properly), otherwise the 10ms
50b7e04f8cSWim Van Sebroeck  * resolution of a jiffy is enough to trigger the overflow. For things like
51b7e04f8cSWim Van Sebroeck  * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
52b7e04f8cSWim Van Sebroeck  * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
53b7e04f8cSWim Van Sebroeck  * necssary.
54b7e04f8cSWim Van Sebroeck  *
55b7e04f8cSWim Van Sebroeck  * As a result of this timing problem, the only modes that are particularly
5625985edcSLucas De Marchi  * feasible are the 4096 and the 2048 divisors, which yield 5.25 and 2.62ms
57b7e04f8cSWim Van Sebroeck  * overflow periods respectively.
58b7e04f8cSWim Van Sebroeck  *
59b7e04f8cSWim Van Sebroeck  * Also, since we can't really expect userspace to be responsive enough
60ee0fc097SJoe Perches  * before the overflow happens, we maintain two separate timers .. One in
61b7e04f8cSWim Van Sebroeck  * the kernel for clearing out WOVF every 2ms or so (again, this depends on
62b7e04f8cSWim Van Sebroeck  * HZ == 1000), and another for monitoring userspace writes to the WDT device.
63b7e04f8cSWim Van Sebroeck  *
64b7e04f8cSWim Van Sebroeck  * As such, we currently use a configurable heartbeat interval which defaults
65b7e04f8cSWim Van Sebroeck  * to 30s. In this case, the userspace daemon is only responsible for periodic
66b7e04f8cSWim Van Sebroeck  * writes to the device before the next heartbeat is scheduled. If the daemon
67b7e04f8cSWim Van Sebroeck  * misses its deadline, the kernel timer will allow the WDT to overflow.
68b7e04f8cSWim Van Sebroeck  */
69b7e04f8cSWim Van Sebroeck static int clock_division_ratio = WTCSR_CKS_4096;
70bea19066SDavid Engraf #define next_ping_period(cks)	(jiffies + msecs_to_jiffies(cks - 4))
71b7e04f8cSWim Van Sebroeck 
7258cf4198SAdrian Bunk static const struct watchdog_info sh_wdt_info;
738f5585ecSPaul Mundt static struct platform_device *sh_wdt_dev;
7470b814ecSAlan Cox static DEFINE_SPINLOCK(shwdt_lock);
75b7e04f8cSWim Van Sebroeck 
76b7e04f8cSWim Van Sebroeck #define WATCHDOG_HEARTBEAT 30			/* 30 sec default heartbeat */
77b7e04f8cSWim Van Sebroeck static int heartbeat = WATCHDOG_HEARTBEAT;	/* in seconds */
78b7e04f8cSWim Van Sebroeck static int nowayout = WATCHDOG_NOWAYOUT;
798f5585ecSPaul Mundt static unsigned long next_heartbeat;
80b7e04f8cSWim Van Sebroeck 
818f5585ecSPaul Mundt struct sh_wdt {
828f5585ecSPaul Mundt 	void __iomem		*base;
838f5585ecSPaul Mundt 	struct device		*dev;
848f5585ecSPaul Mundt 
858f5585ecSPaul Mundt 	struct timer_list	timer;
868f5585ecSPaul Mundt 
878f5585ecSPaul Mundt 	unsigned long		enabled;
888f5585ecSPaul Mundt 	char			expect_close;
898f5585ecSPaul Mundt };
908f5585ecSPaul Mundt 
918f5585ecSPaul Mundt static void sh_wdt_start(struct sh_wdt *wdt)
92b7e04f8cSWim Van Sebroeck {
9370b814ecSAlan Cox 	unsigned long flags;
948f5585ecSPaul Mundt 	u8 csr;
9570b814ecSAlan Cox 
9658cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
97b7e04f8cSWim Van Sebroeck 
98b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
998f5585ecSPaul Mundt 	mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
100b7e04f8cSWim Van Sebroeck 
101b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
102b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_WT | clock_division_ratio;
103b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
104b7e04f8cSWim Van Sebroeck 
105b7e04f8cSWim Van Sebroeck 	sh_wdt_write_cnt(0);
106b7e04f8cSWim Van Sebroeck 
107b7e04f8cSWim Van Sebroeck 	/*
108b7e04f8cSWim Van Sebroeck 	 * These processors have a bit of an inconsistent initialization
109b7e04f8cSWim Van Sebroeck 	 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
110b7e04f8cSWim Van Sebroeck 	 * RSTCSR register was removed.
111b7e04f8cSWim Van Sebroeck 	 *
112b7e04f8cSWim Van Sebroeck 	 * On the SH-2 however, in addition with bits being in different
113b7e04f8cSWim Van Sebroeck 	 * locations, we must deal with RSTCSR outright..
114b7e04f8cSWim Van Sebroeck 	 */
115b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
116b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_TME;
117b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_RSTS;
118b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
119b7e04f8cSWim Van Sebroeck 
120b7e04f8cSWim Van Sebroeck #ifdef CONFIG_CPU_SH2
121b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_rstcsr();
122b7e04f8cSWim Van Sebroeck 	csr &= ~RSTCSR_RSTS;
123b7e04f8cSWim Van Sebroeck 	sh_wdt_write_rstcsr(csr);
124b7e04f8cSWim Van Sebroeck #endif
12558cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
126b7e04f8cSWim Van Sebroeck }
127b7e04f8cSWim Van Sebroeck 
1288f5585ecSPaul Mundt static void sh_wdt_stop(struct sh_wdt *wdt)
129b7e04f8cSWim Van Sebroeck {
13070b814ecSAlan Cox 	unsigned long flags;
1318f5585ecSPaul Mundt 	u8 csr;
13270b814ecSAlan Cox 
13358cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
134b7e04f8cSWim Van Sebroeck 
1358f5585ecSPaul Mundt 	del_timer(&wdt->timer);
136b7e04f8cSWim Van Sebroeck 
137b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
138b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_TME;
139b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
1408f5585ecSPaul Mundt 
14158cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
142b7e04f8cSWim Van Sebroeck }
143b7e04f8cSWim Van Sebroeck 
1448f5585ecSPaul Mundt static inline void sh_wdt_keepalive(struct sh_wdt *wdt)
145b7e04f8cSWim Van Sebroeck {
14670b814ecSAlan Cox 	unsigned long flags;
14770b814ecSAlan Cox 
14858cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
149b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
15058cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
151b7e04f8cSWim Van Sebroeck }
152b7e04f8cSWim Van Sebroeck 
153b7e04f8cSWim Van Sebroeck static int sh_wdt_set_heartbeat(int t)
154b7e04f8cSWim Van Sebroeck {
15570b814ecSAlan Cox 	unsigned long flags;
15670b814ecSAlan Cox 
15770b814ecSAlan Cox 	if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
158b7e04f8cSWim Van Sebroeck 		return -EINVAL;
159b7e04f8cSWim Van Sebroeck 
16058cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
161b7e04f8cSWim Van Sebroeck 	heartbeat = t;
16258cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
163b7e04f8cSWim Van Sebroeck 	return 0;
164b7e04f8cSWim Van Sebroeck }
165b7e04f8cSWim Van Sebroeck 
166b7e04f8cSWim Van Sebroeck static void sh_wdt_ping(unsigned long data)
167b7e04f8cSWim Van Sebroeck {
1688f5585ecSPaul Mundt 	struct sh_wdt *wdt = (struct sh_wdt *)data;
16970b814ecSAlan Cox 	unsigned long flags;
17070b814ecSAlan Cox 
17158cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
172b7e04f8cSWim Van Sebroeck 	if (time_before(jiffies, next_heartbeat)) {
1738f5585ecSPaul Mundt 		u8 csr;
174b7e04f8cSWim Van Sebroeck 
175b7e04f8cSWim Van Sebroeck 		csr = sh_wdt_read_csr();
176b7e04f8cSWim Van Sebroeck 		csr &= ~WTCSR_IOVF;
177b7e04f8cSWim Van Sebroeck 		sh_wdt_write_csr(csr);
178b7e04f8cSWim Van Sebroeck 
179b7e04f8cSWim Van Sebroeck 		sh_wdt_write_cnt(0);
180b7e04f8cSWim Van Sebroeck 
1818f5585ecSPaul Mundt 		mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
182b7e04f8cSWim Van Sebroeck 	} else
1838f5585ecSPaul Mundt 		dev_warn(wdt->dev, "Heartbeat lost! Will not ping "
184b7e04f8cSWim Van Sebroeck 		         "the watchdog\n");
18558cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
186b7e04f8cSWim Van Sebroeck }
187b7e04f8cSWim Van Sebroeck 
188b7e04f8cSWim Van Sebroeck static int sh_wdt_open(struct inode *inode, struct file *file)
189b7e04f8cSWim Van Sebroeck {
1908f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev);
1918f5585ecSPaul Mundt 
1928f5585ecSPaul Mundt 	if (test_and_set_bit(0, &wdt->enabled))
193b7e04f8cSWim Van Sebroeck 		return -EBUSY;
194b7e04f8cSWim Van Sebroeck 	if (nowayout)
195b7e04f8cSWim Van Sebroeck 		__module_get(THIS_MODULE);
196b7e04f8cSWim Van Sebroeck 
1978f5585ecSPaul Mundt 	file->private_data = wdt;
1988f5585ecSPaul Mundt 
1998f5585ecSPaul Mundt 	sh_wdt_start(wdt);
200b7e04f8cSWim Van Sebroeck 
201b7e04f8cSWim Van Sebroeck 	return nonseekable_open(inode, file);
202b7e04f8cSWim Van Sebroeck }
203b7e04f8cSWim Van Sebroeck 
204b7e04f8cSWim Van Sebroeck static int sh_wdt_close(struct inode *inode, struct file *file)
205b7e04f8cSWim Van Sebroeck {
2068f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
2078f5585ecSPaul Mundt 
2088f5585ecSPaul Mundt 	if (wdt->expect_close == 42) {
2098f5585ecSPaul Mundt 		sh_wdt_stop(wdt);
210b7e04f8cSWim Van Sebroeck 	} else {
2118f5585ecSPaul Mundt 		dev_crit(wdt->dev, "Unexpected close, not "
212b7e04f8cSWim Van Sebroeck 		         "stopping watchdog!\n");
2138f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
214b7e04f8cSWim Van Sebroeck 	}
215b7e04f8cSWim Van Sebroeck 
2168f5585ecSPaul Mundt 	clear_bit(0, &wdt->enabled);
2178f5585ecSPaul Mundt 	wdt->expect_close = 0;
218b7e04f8cSWim Van Sebroeck 
219b7e04f8cSWim Van Sebroeck 	return 0;
220b7e04f8cSWim Van Sebroeck }
221b7e04f8cSWim Van Sebroeck 
222b7e04f8cSWim Van Sebroeck static ssize_t sh_wdt_write(struct file *file, const char *buf,
223b7e04f8cSWim Van Sebroeck 			    size_t count, loff_t *ppos)
224b7e04f8cSWim Van Sebroeck {
2258f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
2268f5585ecSPaul Mundt 
227b7e04f8cSWim Van Sebroeck 	if (count) {
228b7e04f8cSWim Van Sebroeck 		if (!nowayout) {
229b7e04f8cSWim Van Sebroeck 			size_t i;
230b7e04f8cSWim Van Sebroeck 
2318f5585ecSPaul Mundt 			wdt->expect_close = 0;
232b7e04f8cSWim Van Sebroeck 
233b7e04f8cSWim Van Sebroeck 			for (i = 0; i != count; i++) {
234b7e04f8cSWim Van Sebroeck 				char c;
235b7e04f8cSWim Van Sebroeck 				if (get_user(c, buf + i))
236b7e04f8cSWim Van Sebroeck 					return -EFAULT;
237b7e04f8cSWim Van Sebroeck 				if (c == 'V')
2388f5585ecSPaul Mundt 					wdt->expect_close = 42;
239b7e04f8cSWim Van Sebroeck 			}
240b7e04f8cSWim Van Sebroeck 		}
2418f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
242b7e04f8cSWim Van Sebroeck 	}
243b7e04f8cSWim Van Sebroeck 
244b7e04f8cSWim Van Sebroeck 	return count;
245b7e04f8cSWim Van Sebroeck }
246b7e04f8cSWim Van Sebroeck 
24770b814ecSAlan Cox static long sh_wdt_ioctl(struct file *file, unsigned int cmd,
24870b814ecSAlan Cox 							unsigned long arg)
249b7e04f8cSWim Van Sebroeck {
2508f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
251b7e04f8cSWim Van Sebroeck 	int new_heartbeat;
252b7e04f8cSWim Van Sebroeck 	int options, retval = -EINVAL;
253b7e04f8cSWim Van Sebroeck 
254b7e04f8cSWim Van Sebroeck 	switch (cmd) {
255b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSUPPORT:
256b7e04f8cSWim Van Sebroeck 		return copy_to_user((struct watchdog_info *)arg,
25770b814ecSAlan Cox 			  &sh_wdt_info, sizeof(sh_wdt_info)) ? -EFAULT : 0;
258b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSTATUS:
259b7e04f8cSWim Van Sebroeck 	case WDIOC_GETBOOTSTATUS:
260b7e04f8cSWim Van Sebroeck 		return put_user(0, (int *)arg);
261b7e04f8cSWim Van Sebroeck 	case WDIOC_SETOPTIONS:
262b7e04f8cSWim Van Sebroeck 		if (get_user(options, (int *)arg))
263b7e04f8cSWim Van Sebroeck 			return -EFAULT;
264b7e04f8cSWim Van Sebroeck 
265b7e04f8cSWim Van Sebroeck 		if (options & WDIOS_DISABLECARD) {
2668f5585ecSPaul Mundt 			sh_wdt_stop(wdt);
267b7e04f8cSWim Van Sebroeck 			retval = 0;
268b7e04f8cSWim Van Sebroeck 		}
269b7e04f8cSWim Van Sebroeck 
270b7e04f8cSWim Van Sebroeck 		if (options & WDIOS_ENABLECARD) {
2718f5585ecSPaul Mundt 			sh_wdt_start(wdt);
272b7e04f8cSWim Van Sebroeck 			retval = 0;
273b7e04f8cSWim Van Sebroeck 		}
274b7e04f8cSWim Van Sebroeck 
275b7e04f8cSWim Van Sebroeck 		return retval;
2760c06090cSWim Van Sebroeck 	case WDIOC_KEEPALIVE:
2778f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
2780c06090cSWim Van Sebroeck 		return 0;
2790c06090cSWim Van Sebroeck 	case WDIOC_SETTIMEOUT:
2800c06090cSWim Van Sebroeck 		if (get_user(new_heartbeat, (int *)arg))
2810c06090cSWim Van Sebroeck 			return -EFAULT;
2820c06090cSWim Van Sebroeck 
2830c06090cSWim Van Sebroeck 		if (sh_wdt_set_heartbeat(new_heartbeat))
2840c06090cSWim Van Sebroeck 			return -EINVAL;
2850c06090cSWim Van Sebroeck 
2868f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
2870c06090cSWim Van Sebroeck 		/* Fall */
2880c06090cSWim Van Sebroeck 	case WDIOC_GETTIMEOUT:
2890c06090cSWim Van Sebroeck 		return put_user(heartbeat, (int *)arg);
290b7e04f8cSWim Van Sebroeck 	default:
291b7e04f8cSWim Van Sebroeck 		return -ENOTTY;
292b7e04f8cSWim Van Sebroeck 	}
293b7e04f8cSWim Van Sebroeck 	return 0;
294b7e04f8cSWim Van Sebroeck }
295b7e04f8cSWim Van Sebroeck 
296b7e04f8cSWim Van Sebroeck static int sh_wdt_notify_sys(struct notifier_block *this,
297b7e04f8cSWim Van Sebroeck 			     unsigned long code, void *unused)
298b7e04f8cSWim Van Sebroeck {
2998f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev);
3008f5585ecSPaul Mundt 
301b7e04f8cSWim Van Sebroeck 	if (code == SYS_DOWN || code == SYS_HALT)
3028f5585ecSPaul Mundt 		sh_wdt_stop(wdt);
303b7e04f8cSWim Van Sebroeck 
304b7e04f8cSWim Van Sebroeck 	return NOTIFY_DONE;
305b7e04f8cSWim Van Sebroeck }
306b7e04f8cSWim Van Sebroeck 
307b7e04f8cSWim Van Sebroeck static const struct file_operations sh_wdt_fops = {
308b7e04f8cSWim Van Sebroeck 	.owner		= THIS_MODULE,
309b7e04f8cSWim Van Sebroeck 	.llseek		= no_llseek,
310b7e04f8cSWim Van Sebroeck 	.write		= sh_wdt_write,
31170b814ecSAlan Cox 	.unlocked_ioctl	= sh_wdt_ioctl,
312b7e04f8cSWim Van Sebroeck 	.open		= sh_wdt_open,
313b7e04f8cSWim Van Sebroeck 	.release	= sh_wdt_close,
314b7e04f8cSWim Van Sebroeck };
315b7e04f8cSWim Van Sebroeck 
31670b814ecSAlan Cox static const struct watchdog_info sh_wdt_info = {
317b7e04f8cSWim Van Sebroeck 	.options		= WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
318b7e04f8cSWim Van Sebroeck 				  WDIOF_MAGICCLOSE,
319b7e04f8cSWim Van Sebroeck 	.firmware_version	= 1,
320b7e04f8cSWim Van Sebroeck 	.identity		= "SH WDT",
321b7e04f8cSWim Van Sebroeck };
322b7e04f8cSWim Van Sebroeck 
323b7e04f8cSWim Van Sebroeck static struct notifier_block sh_wdt_notifier = {
324b7e04f8cSWim Van Sebroeck 	.notifier_call		= sh_wdt_notify_sys,
325b7e04f8cSWim Van Sebroeck };
326b7e04f8cSWim Van Sebroeck 
327b7e04f8cSWim Van Sebroeck static struct miscdevice sh_wdt_miscdev = {
328b7e04f8cSWim Van Sebroeck 	.minor		= WATCHDOG_MINOR,
329b7e04f8cSWim Van Sebroeck 	.name		= "watchdog",
330b7e04f8cSWim Van Sebroeck 	.fops		= &sh_wdt_fops,
331b7e04f8cSWim Van Sebroeck };
332b7e04f8cSWim Van Sebroeck 
3338f5585ecSPaul Mundt static int __devinit sh_wdt_probe(struct platform_device *pdev)
3348f5585ecSPaul Mundt {
3358f5585ecSPaul Mundt 	struct sh_wdt *wdt;
3368f5585ecSPaul Mundt 	struct resource *res;
3378f5585ecSPaul Mundt 	int rc;
3388f5585ecSPaul Mundt 
3398f5585ecSPaul Mundt 	/*
3408f5585ecSPaul Mundt 	 * As this driver only covers the global watchdog case, reject
3418f5585ecSPaul Mundt 	 * any attempts to register per-CPU watchdogs.
3428f5585ecSPaul Mundt 	 */
3438f5585ecSPaul Mundt 	if (pdev->id != -1)
3448f5585ecSPaul Mundt 		return -EINVAL;
3458f5585ecSPaul Mundt 
3468f5585ecSPaul Mundt 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3478f5585ecSPaul Mundt 	if (unlikely(!res))
3488f5585ecSPaul Mundt 		return -EINVAL;
3498f5585ecSPaul Mundt 
3508f5585ecSPaul Mundt 	if (!devm_request_mem_region(&pdev->dev, res->start,
3518f5585ecSPaul Mundt 				     resource_size(res), DRV_NAME))
3528f5585ecSPaul Mundt 		return -EBUSY;
3538f5585ecSPaul Mundt 
3548f5585ecSPaul Mundt 	wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
3558f5585ecSPaul Mundt 	if (unlikely(!wdt)) {
3568f5585ecSPaul Mundt 		rc = -ENOMEM;
3578f5585ecSPaul Mundt 		goto out_release;
3588f5585ecSPaul Mundt 	}
3598f5585ecSPaul Mundt 
3608f5585ecSPaul Mundt 	wdt->dev = &pdev->dev;
3618f5585ecSPaul Mundt 
3628f5585ecSPaul Mundt 	wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
3638f5585ecSPaul Mundt 	if (unlikely(!wdt->base)) {
3648f5585ecSPaul Mundt 		rc = -ENXIO;
3658f5585ecSPaul Mundt 		goto out_err;
3668f5585ecSPaul Mundt 	}
3678f5585ecSPaul Mundt 
3688f5585ecSPaul Mundt 	rc = register_reboot_notifier(&sh_wdt_notifier);
3698f5585ecSPaul Mundt 	if (unlikely(rc)) {
3708f5585ecSPaul Mundt 		dev_err(&pdev->dev,
3718f5585ecSPaul Mundt 			"Can't register reboot notifier (err=%d)\n", rc);
3728f5585ecSPaul Mundt 		goto out_unmap;
3738f5585ecSPaul Mundt 	}
3748f5585ecSPaul Mundt 
3758f5585ecSPaul Mundt 	sh_wdt_miscdev.parent = wdt->dev;
3768f5585ecSPaul Mundt 
3778f5585ecSPaul Mundt 	rc = misc_register(&sh_wdt_miscdev);
3788f5585ecSPaul Mundt 	if (unlikely(rc)) {
3798f5585ecSPaul Mundt 		dev_err(&pdev->dev,
3808f5585ecSPaul Mundt 			"Can't register miscdev on minor=%d (err=%d)\n",
3818f5585ecSPaul Mundt 						sh_wdt_miscdev.minor, rc);
3828f5585ecSPaul Mundt 		goto out_unreg;
3838f5585ecSPaul Mundt 	}
3848f5585ecSPaul Mundt 
3858f5585ecSPaul Mundt 	init_timer(&wdt->timer);
3868f5585ecSPaul Mundt 	wdt->timer.function	= sh_wdt_ping;
3878f5585ecSPaul Mundt 	wdt->timer.data		= (unsigned long)wdt;
3888f5585ecSPaul Mundt 	wdt->timer.expires	= next_ping_period(clock_division_ratio);
3898f5585ecSPaul Mundt 
3908f5585ecSPaul Mundt 	platform_set_drvdata(pdev, wdt);
3918f5585ecSPaul Mundt 	sh_wdt_dev = pdev;
3928f5585ecSPaul Mundt 
3938f5585ecSPaul Mundt 	dev_info(&pdev->dev, "initialized.\n");
3948f5585ecSPaul Mundt 
3958f5585ecSPaul Mundt 	return 0;
3968f5585ecSPaul Mundt 
3978f5585ecSPaul Mundt out_unreg:
3988f5585ecSPaul Mundt 	unregister_reboot_notifier(&sh_wdt_notifier);
3998f5585ecSPaul Mundt out_unmap:
4008f5585ecSPaul Mundt 	devm_iounmap(&pdev->dev, wdt->base);
4018f5585ecSPaul Mundt out_err:
4028f5585ecSPaul Mundt 	devm_kfree(&pdev->dev, wdt);
4038f5585ecSPaul Mundt out_release:
4048f5585ecSPaul Mundt 	devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
4058f5585ecSPaul Mundt 
4068f5585ecSPaul Mundt 	return rc;
4078f5585ecSPaul Mundt }
4088f5585ecSPaul Mundt 
4098f5585ecSPaul Mundt static int __devexit sh_wdt_remove(struct platform_device *pdev)
4108f5585ecSPaul Mundt {
4118f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(pdev);
4128f5585ecSPaul Mundt 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4138f5585ecSPaul Mundt 
4148f5585ecSPaul Mundt 	platform_set_drvdata(pdev, NULL);
4158f5585ecSPaul Mundt 
4168f5585ecSPaul Mundt 	misc_deregister(&sh_wdt_miscdev);
4178f5585ecSPaul Mundt 
4188f5585ecSPaul Mundt 	sh_wdt_dev = NULL;
4198f5585ecSPaul Mundt 
4208f5585ecSPaul Mundt 	unregister_reboot_notifier(&sh_wdt_notifier);
4218f5585ecSPaul Mundt 	devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
4228f5585ecSPaul Mundt 	devm_iounmap(&pdev->dev, wdt->base);
4238f5585ecSPaul Mundt 	devm_kfree(&pdev->dev, wdt);
4248f5585ecSPaul Mundt 
4258f5585ecSPaul Mundt 	return 0;
4268f5585ecSPaul Mundt }
4278f5585ecSPaul Mundt 
4288f5585ecSPaul Mundt static struct platform_driver sh_wdt_driver = {
4298f5585ecSPaul Mundt 	.driver		= {
4308f5585ecSPaul Mundt 		.name	= DRV_NAME,
4318f5585ecSPaul Mundt 		.owner	= THIS_MODULE,
4328f5585ecSPaul Mundt 	},
4338f5585ecSPaul Mundt 
4348f5585ecSPaul Mundt 	.probe	= sh_wdt_probe,
4358f5585ecSPaul Mundt 	.remove	= __devexit_p(sh_wdt_remove),
4368f5585ecSPaul Mundt };
4378f5585ecSPaul Mundt 
438b7e04f8cSWim Van Sebroeck static int __init sh_wdt_init(void)
439b7e04f8cSWim Van Sebroeck {
440b7e04f8cSWim Van Sebroeck 	int rc;
441b7e04f8cSWim Van Sebroeck 
4428f5585ecSPaul Mundt 	if (unlikely(clock_division_ratio < 0x5 ||
4438f5585ecSPaul Mundt 		     clock_division_ratio > 0x7)) {
444b7e04f8cSWim Van Sebroeck 		clock_division_ratio = WTCSR_CKS_4096;
4458f5585ecSPaul Mundt 
446*27c766aaSJoe Perches 		pr_info("divisor must be 0x5<=x<=0x7, using %d\n",
447*27c766aaSJoe Perches 			clock_division_ratio);
448b7e04f8cSWim Van Sebroeck 	}
449b7e04f8cSWim Van Sebroeck 
450b7e04f8cSWim Van Sebroeck 	rc = sh_wdt_set_heartbeat(heartbeat);
451b7e04f8cSWim Van Sebroeck 	if (unlikely(rc)) {
452b7e04f8cSWim Van Sebroeck 		heartbeat = WATCHDOG_HEARTBEAT;
4538f5585ecSPaul Mundt 
454*27c766aaSJoe Perches 		pr_info("heartbeat value must be 1<=x<=3600, using %d\n",
455*27c766aaSJoe Perches 			heartbeat);
456b7e04f8cSWim Van Sebroeck 	}
457b7e04f8cSWim Van Sebroeck 
458*27c766aaSJoe Perches 	pr_info("configured with heartbeat=%d sec (nowayout=%d)\n",
459*27c766aaSJoe Perches 		heartbeat, nowayout);
460b7e04f8cSWim Van Sebroeck 
4618f5585ecSPaul Mundt 	return platform_driver_register(&sh_wdt_driver);
462b7e04f8cSWim Van Sebroeck }
463b7e04f8cSWim Van Sebroeck 
464b7e04f8cSWim Van Sebroeck static void __exit sh_wdt_exit(void)
465b7e04f8cSWim Van Sebroeck {
4668f5585ecSPaul Mundt 	platform_driver_unregister(&sh_wdt_driver);
467b7e04f8cSWim Van Sebroeck }
4688f5585ecSPaul Mundt module_init(sh_wdt_init);
4698f5585ecSPaul Mundt module_exit(sh_wdt_exit);
470b7e04f8cSWim Van Sebroeck 
471b7e04f8cSWim Van Sebroeck MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
472b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("SuperH watchdog driver");
473b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL");
4748f5585ecSPaul Mundt MODULE_ALIAS("platform:" DRV_NAME);
475b7e04f8cSWim Van Sebroeck MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
476b7e04f8cSWim Van Sebroeck 
477b7e04f8cSWim Van Sebroeck module_param(clock_division_ratio, int, 0);
478a77dba7eSWim Van Sebroeck MODULE_PARM_DESC(clock_division_ratio,
479a77dba7eSWim Van Sebroeck 	"Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
48076550d32SRandy Dunlap 	"to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
481b7e04f8cSWim Van Sebroeck 
482b7e04f8cSWim Van Sebroeck module_param(heartbeat, int, 0);
48370b814ecSAlan Cox MODULE_PARM_DESC(heartbeat,
48470b814ecSAlan Cox 	"Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
48570b814ecSAlan Cox 				__MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
486b7e04f8cSWim Van Sebroeck 
487b7e04f8cSWim Van Sebroeck module_param(nowayout, int, 0);
48870b814ecSAlan Cox MODULE_PARM_DESC(nowayout,
48970b814ecSAlan Cox 	"Watchdog cannot be stopped once started (default="
49070b814ecSAlan Cox 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
491