xref: /linux/drivers/watchdog/shwdt.c (revision 8f5585ec3d173819dd7e751f661c33af39d7ec60)
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>
22*8f5585ecSPaul 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>
32*8f5585ecSPaul 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 
37*8f5585ecSPaul 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
53b7e04f8cSWim Van Sebroeck  * feasible are the 4096 and the 2048 divisors, which yeild 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;
70*8f5585ecSPaul 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;
76*8f5585ecSPaul Mundt static unsigned long next_heartbeat;
77b7e04f8cSWim Van Sebroeck 
78*8f5585ecSPaul Mundt struct sh_wdt {
79*8f5585ecSPaul Mundt 	void __iomem		*base;
80*8f5585ecSPaul Mundt 	struct device		*dev;
81*8f5585ecSPaul Mundt 
82*8f5585ecSPaul Mundt 	struct timer_list	timer;
83*8f5585ecSPaul Mundt 
84*8f5585ecSPaul Mundt 	unsigned long		enabled;
85*8f5585ecSPaul Mundt 	char			expect_close;
86*8f5585ecSPaul Mundt };
87*8f5585ecSPaul Mundt 
88*8f5585ecSPaul Mundt static void sh_wdt_start(struct sh_wdt *wdt)
89b7e04f8cSWim Van Sebroeck {
9070b814ecSAlan Cox 	unsigned long flags;
91*8f5585ecSPaul Mundt 	u8 csr;
9270b814ecSAlan Cox 
9358cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
94b7e04f8cSWim Van Sebroeck 
95b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
96*8f5585ecSPaul 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 
125*8f5585ecSPaul Mundt static void sh_wdt_stop(struct sh_wdt *wdt)
126b7e04f8cSWim Van Sebroeck {
12770b814ecSAlan Cox 	unsigned long flags;
128*8f5585ecSPaul Mundt 	u8 csr;
12970b814ecSAlan Cox 
13058cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
131b7e04f8cSWim Van Sebroeck 
132*8f5585ecSPaul 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);
137*8f5585ecSPaul Mundt 
13858cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
139b7e04f8cSWim Van Sebroeck }
140b7e04f8cSWim Van Sebroeck 
141*8f5585ecSPaul 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 {
165*8f5585ecSPaul 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)) {
170*8f5585ecSPaul 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 
178*8f5585ecSPaul Mundt 		mod_timer(&wdt->timer, next_ping_period(clock_division_ratio));
179b7e04f8cSWim Van Sebroeck 	} else
180*8f5585ecSPaul 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 {
187*8f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev);
188*8f5585ecSPaul Mundt 
189*8f5585ecSPaul 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 
194*8f5585ecSPaul Mundt 	file->private_data = wdt;
195*8f5585ecSPaul Mundt 
196*8f5585ecSPaul 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 {
203*8f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
204*8f5585ecSPaul Mundt 
205*8f5585ecSPaul Mundt 	if (wdt->expect_close == 42) {
206*8f5585ecSPaul Mundt 		sh_wdt_stop(wdt);
207b7e04f8cSWim Van Sebroeck 	} else {
208*8f5585ecSPaul Mundt 		dev_crit(wdt->dev, "Unexpected close, not "
209b7e04f8cSWim Van Sebroeck 		         "stopping watchdog!\n");
210*8f5585ecSPaul Mundt 		sh_wdt_keepalive(wdt);
211b7e04f8cSWim Van Sebroeck 	}
212b7e04f8cSWim Van Sebroeck 
213*8f5585ecSPaul Mundt 	clear_bit(0, &wdt->enabled);
214*8f5585ecSPaul 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 {
222*8f5585ecSPaul Mundt 	struct sh_wdt *wdt = file->private_data;
223*8f5585ecSPaul Mundt 
224b7e04f8cSWim Van Sebroeck 	if (count) {
225b7e04f8cSWim Van Sebroeck 		if (!nowayout) {
226b7e04f8cSWim Van Sebroeck 			size_t i;
227b7e04f8cSWim Van Sebroeck 
228*8f5585ecSPaul 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')
235*8f5585ecSPaul Mundt 					wdt->expect_close = 42;
236b7e04f8cSWim Van Sebroeck 			}
237b7e04f8cSWim Van Sebroeck 		}
238*8f5585ecSPaul 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 {
247*8f5585ecSPaul 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) {
263*8f5585ecSPaul Mundt 			sh_wdt_stop(wdt);
264b7e04f8cSWim Van Sebroeck 			retval = 0;
265b7e04f8cSWim Van Sebroeck 		}
266b7e04f8cSWim Van Sebroeck 
267b7e04f8cSWim Van Sebroeck 		if (options & WDIOS_ENABLECARD) {
268*8f5585ecSPaul 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:
274*8f5585ecSPaul 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 
283*8f5585ecSPaul 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 {
296*8f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(sh_wdt_dev);
297*8f5585ecSPaul Mundt 
298b7e04f8cSWim Van Sebroeck 	if (code == SYS_DOWN || code == SYS_HALT)
299*8f5585ecSPaul 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 
330*8f5585ecSPaul Mundt static int __devinit sh_wdt_probe(struct platform_device *pdev)
331*8f5585ecSPaul Mundt {
332*8f5585ecSPaul Mundt 	struct sh_wdt *wdt;
333*8f5585ecSPaul Mundt 	struct resource *res;
334*8f5585ecSPaul Mundt 	int rc;
335*8f5585ecSPaul Mundt 
336*8f5585ecSPaul Mundt 	/*
337*8f5585ecSPaul Mundt 	 * As this driver only covers the global watchdog case, reject
338*8f5585ecSPaul Mundt 	 * any attempts to register per-CPU watchdogs.
339*8f5585ecSPaul Mundt 	 */
340*8f5585ecSPaul Mundt 	if (pdev->id != -1)
341*8f5585ecSPaul Mundt 		return -EINVAL;
342*8f5585ecSPaul Mundt 
343*8f5585ecSPaul Mundt 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
344*8f5585ecSPaul Mundt 	if (unlikely(!res))
345*8f5585ecSPaul Mundt 		return -EINVAL;
346*8f5585ecSPaul Mundt 
347*8f5585ecSPaul Mundt 	if (!devm_request_mem_region(&pdev->dev, res->start,
348*8f5585ecSPaul Mundt 				     resource_size(res), DRV_NAME))
349*8f5585ecSPaul Mundt 		return -EBUSY;
350*8f5585ecSPaul Mundt 
351*8f5585ecSPaul Mundt 	wdt = devm_kzalloc(&pdev->dev, sizeof(struct sh_wdt), GFP_KERNEL);
352*8f5585ecSPaul Mundt 	if (unlikely(!wdt)) {
353*8f5585ecSPaul Mundt 		rc = -ENOMEM;
354*8f5585ecSPaul Mundt 		goto out_release;
355*8f5585ecSPaul Mundt 	}
356*8f5585ecSPaul Mundt 
357*8f5585ecSPaul Mundt 	wdt->dev = &pdev->dev;
358*8f5585ecSPaul Mundt 
359*8f5585ecSPaul Mundt 	wdt->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
360*8f5585ecSPaul Mundt 	if (unlikely(!wdt->base)) {
361*8f5585ecSPaul Mundt 		rc = -ENXIO;
362*8f5585ecSPaul Mundt 		goto out_err;
363*8f5585ecSPaul Mundt 	}
364*8f5585ecSPaul Mundt 
365*8f5585ecSPaul Mundt 	rc = register_reboot_notifier(&sh_wdt_notifier);
366*8f5585ecSPaul Mundt 	if (unlikely(rc)) {
367*8f5585ecSPaul Mundt 		dev_err(&pdev->dev,
368*8f5585ecSPaul Mundt 			"Can't register reboot notifier (err=%d)\n", rc);
369*8f5585ecSPaul Mundt 		goto out_unmap;
370*8f5585ecSPaul Mundt 	}
371*8f5585ecSPaul Mundt 
372*8f5585ecSPaul Mundt 	sh_wdt_miscdev.parent = wdt->dev;
373*8f5585ecSPaul Mundt 
374*8f5585ecSPaul Mundt 	rc = misc_register(&sh_wdt_miscdev);
375*8f5585ecSPaul Mundt 	if (unlikely(rc)) {
376*8f5585ecSPaul Mundt 		dev_err(&pdev->dev,
377*8f5585ecSPaul Mundt 			"Can't register miscdev on minor=%d (err=%d)\n",
378*8f5585ecSPaul Mundt 						sh_wdt_miscdev.minor, rc);
379*8f5585ecSPaul Mundt 		goto out_unreg;
380*8f5585ecSPaul Mundt 	}
381*8f5585ecSPaul Mundt 
382*8f5585ecSPaul Mundt 	init_timer(&wdt->timer);
383*8f5585ecSPaul Mundt 	wdt->timer.function	= sh_wdt_ping;
384*8f5585ecSPaul Mundt 	wdt->timer.data		= (unsigned long)wdt;
385*8f5585ecSPaul Mundt 	wdt->timer.expires	= next_ping_period(clock_division_ratio);
386*8f5585ecSPaul Mundt 
387*8f5585ecSPaul Mundt 	platform_set_drvdata(pdev, wdt);
388*8f5585ecSPaul Mundt 	sh_wdt_dev = pdev;
389*8f5585ecSPaul Mundt 
390*8f5585ecSPaul Mundt 	dev_info(&pdev->dev, "initialized.\n");
391*8f5585ecSPaul Mundt 
392*8f5585ecSPaul Mundt 	return 0;
393*8f5585ecSPaul Mundt 
394*8f5585ecSPaul Mundt out_unreg:
395*8f5585ecSPaul Mundt 	unregister_reboot_notifier(&sh_wdt_notifier);
396*8f5585ecSPaul Mundt out_unmap:
397*8f5585ecSPaul Mundt 	devm_iounmap(&pdev->dev, wdt->base);
398*8f5585ecSPaul Mundt out_err:
399*8f5585ecSPaul Mundt 	devm_kfree(&pdev->dev, wdt);
400*8f5585ecSPaul Mundt out_release:
401*8f5585ecSPaul Mundt 	devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
402*8f5585ecSPaul Mundt 
403*8f5585ecSPaul Mundt 	return rc;
404*8f5585ecSPaul Mundt }
405*8f5585ecSPaul Mundt 
406*8f5585ecSPaul Mundt static int __devexit sh_wdt_remove(struct platform_device *pdev)
407*8f5585ecSPaul Mundt {
408*8f5585ecSPaul Mundt 	struct sh_wdt *wdt = platform_get_drvdata(pdev);
409*8f5585ecSPaul Mundt 	struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
410*8f5585ecSPaul Mundt 
411*8f5585ecSPaul Mundt 	platform_set_drvdata(pdev, NULL);
412*8f5585ecSPaul Mundt 
413*8f5585ecSPaul Mundt 	misc_deregister(&sh_wdt_miscdev);
414*8f5585ecSPaul Mundt 
415*8f5585ecSPaul Mundt 	sh_wdt_dev = NULL;
416*8f5585ecSPaul Mundt 
417*8f5585ecSPaul Mundt 	unregister_reboot_notifier(&sh_wdt_notifier);
418*8f5585ecSPaul Mundt 	devm_release_mem_region(&pdev->dev, res->start, resource_size(res));
419*8f5585ecSPaul Mundt 	devm_iounmap(&pdev->dev, wdt->base);
420*8f5585ecSPaul Mundt 	devm_kfree(&pdev->dev, wdt);
421*8f5585ecSPaul Mundt 
422*8f5585ecSPaul Mundt 	return 0;
423*8f5585ecSPaul Mundt }
424*8f5585ecSPaul Mundt 
425*8f5585ecSPaul Mundt static struct platform_driver sh_wdt_driver = {
426*8f5585ecSPaul Mundt 	.driver		= {
427*8f5585ecSPaul Mundt 		.name	= DRV_NAME,
428*8f5585ecSPaul Mundt 		.owner	= THIS_MODULE,
429*8f5585ecSPaul Mundt 	},
430*8f5585ecSPaul Mundt 
431*8f5585ecSPaul Mundt 	.probe	= sh_wdt_probe,
432*8f5585ecSPaul Mundt 	.remove	= __devexit_p(sh_wdt_remove),
433*8f5585ecSPaul Mundt };
434*8f5585ecSPaul Mundt 
435b7e04f8cSWim Van Sebroeck static int __init sh_wdt_init(void)
436b7e04f8cSWim Van Sebroeck {
437b7e04f8cSWim Van Sebroeck 	int rc;
438b7e04f8cSWim Van Sebroeck 
439*8f5585ecSPaul Mundt 	if (unlikely(clock_division_ratio < 0x5 ||
440*8f5585ecSPaul Mundt 		     clock_division_ratio > 0x7)) {
441b7e04f8cSWim Van Sebroeck 		clock_division_ratio = WTCSR_CKS_4096;
442*8f5585ecSPaul Mundt 
443*8f5585ecSPaul Mundt 		pr_info("%s: divisor must be 0x5<=x<=0x7, using %d\n",
444*8f5585ecSPaul 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;
450*8f5585ecSPaul Mundt 
451*8f5585ecSPaul Mundt 		pr_info("%s: heartbeat value must be 1<=x<=3600, using %d\n",
452*8f5585ecSPaul Mundt 			DRV_NAME, heartbeat);
453b7e04f8cSWim Van Sebroeck 	}
454b7e04f8cSWim Van Sebroeck 
455*8f5585ecSPaul Mundt 	pr_info("%s: configured with heartbeat=%d sec (nowayout=%d)\n",
456*8f5585ecSPaul Mundt 		DRV_NAME, heartbeat, nowayout);
457b7e04f8cSWim Van Sebroeck 
458*8f5585ecSPaul 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 {
463*8f5585ecSPaul Mundt 	platform_driver_unregister(&sh_wdt_driver);
464b7e04f8cSWim Van Sebroeck }
465*8f5585ecSPaul Mundt module_init(sh_wdt_init);
466*8f5585ecSPaul 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");
471*8f5585ecSPaul 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) "
477a77dba7eSWim Van Sebroeck 	"to 0x7 (5.25ms). (default=" __MODULE_STRING(clock_division_ratio) ")");
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