xref: /linux/drivers/watchdog/shwdt.c (revision 76550d3292ba1b0dd1ff0a13d78a2718eba599c7)
1b7e04f8cSWim Van Sebroeck /*
2b7e04f8cSWim Van Sebroeck  * drivers/char/watchdog/shwdt.c
3b7e04f8cSWim Van Sebroeck  *
4b7e04f8cSWim Van Sebroeck  * Watchdog driver for integrated watchdog in the SuperH processors.
5b7e04f8cSWim Van Sebroeck  *
6b7e04f8cSWim Van Sebroeck  * Copyright (C) 2001, 2002, 2003 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>
22b7e04f8cSWim Van Sebroeck #include <linux/init.h>
23b7e04f8cSWim Van Sebroeck #include <linux/types.h>
24b7e04f8cSWim Van Sebroeck #include <linux/miscdevice.h>
25b7e04f8cSWim Van Sebroeck #include <linux/watchdog.h>
26b7e04f8cSWim Van Sebroeck #include <linux/reboot.h>
27b7e04f8cSWim Van Sebroeck #include <linux/notifier.h>
28b7e04f8cSWim Van Sebroeck #include <linux/ioport.h>
29b7e04f8cSWim Van Sebroeck #include <linux/fs.h>
30b7e04f8cSWim Van Sebroeck #include <linux/mm.h>
3170b814ecSAlan Cox #include <linux/io.h>
3270b814ecSAlan Cox #include <linux/uaccess.h>
3358cf4198SAdrian Bunk #include <asm/watchdog.h>
34b7e04f8cSWim Van Sebroeck 
35b7e04f8cSWim Van Sebroeck #define PFX "shwdt: "
36b7e04f8cSWim Van Sebroeck 
37b7e04f8cSWim Van Sebroeck /*
38b7e04f8cSWim Van Sebroeck  * Default clock division ratio is 5.25 msecs. For an additional table of
39b7e04f8cSWim Van Sebroeck  * values, consult the asm-sh/watchdog.h. Overload this at module load
40b7e04f8cSWim Van Sebroeck  * time.
41b7e04f8cSWim Van Sebroeck  *
42b7e04f8cSWim Van Sebroeck  * In order for this to work reliably we need to have HZ set to 1000 or
43b7e04f8cSWim Van Sebroeck  * something quite higher than 100 (or we need a proper high-res timer
44b7e04f8cSWim Van Sebroeck  * implementation that will deal with this properly), otherwise the 10ms
45b7e04f8cSWim Van Sebroeck  * resolution of a jiffy is enough to trigger the overflow. For things like
46b7e04f8cSWim Van Sebroeck  * the SH-4 and SH-5, this isn't necessarily that big of a problem, though
47b7e04f8cSWim Van Sebroeck  * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely
48b7e04f8cSWim Van Sebroeck  * necssary.
49b7e04f8cSWim Van Sebroeck  *
50b7e04f8cSWim Van Sebroeck  * As a result of this timing problem, the only modes that are particularly
51b7e04f8cSWim Van Sebroeck  * feasible are the 4096 and the 2048 divisors, which yeild 5.25 and 2.62ms
52b7e04f8cSWim Van Sebroeck  * overflow periods respectively.
53b7e04f8cSWim Van Sebroeck  *
54b7e04f8cSWim Van Sebroeck  * Also, since we can't really expect userspace to be responsive enough
55ee0fc097SJoe Perches  * before the overflow happens, we maintain two separate timers .. One in
56b7e04f8cSWim Van Sebroeck  * the kernel for clearing out WOVF every 2ms or so (again, this depends on
57b7e04f8cSWim Van Sebroeck  * HZ == 1000), and another for monitoring userspace writes to the WDT device.
58b7e04f8cSWim Van Sebroeck  *
59b7e04f8cSWim Van Sebroeck  * As such, we currently use a configurable heartbeat interval which defaults
60b7e04f8cSWim Van Sebroeck  * to 30s. In this case, the userspace daemon is only responsible for periodic
61b7e04f8cSWim Van Sebroeck  * writes to the device before the next heartbeat is scheduled. If the daemon
62b7e04f8cSWim Van Sebroeck  * misses its deadline, the kernel timer will allow the WDT to overflow.
63b7e04f8cSWim Van Sebroeck  */
64b7e04f8cSWim Van Sebroeck static int clock_division_ratio = WTCSR_CKS_4096;
65b7e04f8cSWim Van Sebroeck 
66b7e04f8cSWim Van Sebroeck #define next_ping_period(cks)	msecs_to_jiffies(cks - 4)
67b7e04f8cSWim Van Sebroeck 
68b7e04f8cSWim Van Sebroeck static void sh_wdt_ping(unsigned long data);
69b7e04f8cSWim Van Sebroeck 
70b7e04f8cSWim Van Sebroeck static unsigned long shwdt_is_open;
7158cf4198SAdrian Bunk static const struct watchdog_info sh_wdt_info;
72b7e04f8cSWim Van Sebroeck static char shwdt_expect_close;
73b7e04f8cSWim Van Sebroeck static DEFINE_TIMER(timer, sh_wdt_ping, 0, 0);
74b7e04f8cSWim Van Sebroeck static unsigned long next_heartbeat;
7570b814ecSAlan Cox static DEFINE_SPINLOCK(shwdt_lock);
76b7e04f8cSWim Van Sebroeck 
77b7e04f8cSWim Van Sebroeck #define WATCHDOG_HEARTBEAT 30			/* 30 sec default heartbeat */
78b7e04f8cSWim Van Sebroeck static int heartbeat = WATCHDOG_HEARTBEAT;	/* in seconds */
79b7e04f8cSWim Van Sebroeck 
80b7e04f8cSWim Van Sebroeck static int nowayout = WATCHDOG_NOWAYOUT;
81b7e04f8cSWim Van Sebroeck 
82b7e04f8cSWim Van Sebroeck /**
83b7e04f8cSWim Van Sebroeck  * 	sh_wdt_start - Start the Watchdog
84b7e04f8cSWim Van Sebroeck  *
85b7e04f8cSWim Van Sebroeck  * 	Starts the watchdog.
86b7e04f8cSWim Van Sebroeck  */
87b7e04f8cSWim Van Sebroeck static void sh_wdt_start(void)
88b7e04f8cSWim Van Sebroeck {
89b7e04f8cSWim Van Sebroeck 	__u8 csr;
9070b814ecSAlan Cox 	unsigned long flags;
9170b814ecSAlan Cox 
9258cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
93b7e04f8cSWim Van Sebroeck 
94b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
95b7e04f8cSWim Van Sebroeck 	mod_timer(&timer, next_ping_period(clock_division_ratio));
96b7e04f8cSWim Van Sebroeck 
97b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
98b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_WT | clock_division_ratio;
99b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
100b7e04f8cSWim Van Sebroeck 
101b7e04f8cSWim Van Sebroeck 	sh_wdt_write_cnt(0);
102b7e04f8cSWim Van Sebroeck 
103b7e04f8cSWim Van Sebroeck 	/*
104b7e04f8cSWim Van Sebroeck 	 * These processors have a bit of an inconsistent initialization
105b7e04f8cSWim Van Sebroeck 	 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
106b7e04f8cSWim Van Sebroeck 	 * RSTCSR register was removed.
107b7e04f8cSWim Van Sebroeck 	 *
108b7e04f8cSWim Van Sebroeck 	 * On the SH-2 however, in addition with bits being in different
109b7e04f8cSWim Van Sebroeck 	 * locations, we must deal with RSTCSR outright..
110b7e04f8cSWim Van Sebroeck 	 */
111b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
112b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_TME;
113b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_RSTS;
114b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
115b7e04f8cSWim Van Sebroeck 
116b7e04f8cSWim Van Sebroeck #ifdef CONFIG_CPU_SH2
117b7e04f8cSWim Van Sebroeck 	/*
118b7e04f8cSWim Van Sebroeck 	 * Whoever came up with the RSTCSR semantics must've been smoking
119b7e04f8cSWim Van Sebroeck 	 * some of the good stuff, since in addition to the WTCSR/WTCNT write
120b7e04f8cSWim Van Sebroeck 	 * brain-damage, it's managed to fuck things up one step further..
121b7e04f8cSWim Van Sebroeck 	 *
122b7e04f8cSWim Van Sebroeck 	 * If we need to clear the WOVF bit, the upper byte has to be 0xa5..
123b7e04f8cSWim Van Sebroeck 	 * but if we want to touch RSTE or RSTS, the upper byte has to be
124b7e04f8cSWim Van Sebroeck 	 * 0x5a..
125b7e04f8cSWim Van Sebroeck 	 */
126b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_rstcsr();
127b7e04f8cSWim Van Sebroeck 	csr &= ~RSTCSR_RSTS;
128b7e04f8cSWim Van Sebroeck 	sh_wdt_write_rstcsr(csr);
129b7e04f8cSWim Van Sebroeck #endif
13058cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
131b7e04f8cSWim Van Sebroeck }
132b7e04f8cSWim Van Sebroeck 
133b7e04f8cSWim Van Sebroeck /**
134b7e04f8cSWim Van Sebroeck  * 	sh_wdt_stop - Stop the Watchdog
135b7e04f8cSWim Van Sebroeck  * 	Stops the watchdog.
136b7e04f8cSWim Van Sebroeck  */
137b7e04f8cSWim Van Sebroeck static void sh_wdt_stop(void)
138b7e04f8cSWim Van Sebroeck {
139b7e04f8cSWim Van Sebroeck 	__u8 csr;
14070b814ecSAlan Cox 	unsigned long flags;
14170b814ecSAlan Cox 
14258cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
143b7e04f8cSWim Van Sebroeck 
144b7e04f8cSWim Van Sebroeck 	del_timer(&timer);
145b7e04f8cSWim Van Sebroeck 
146b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
147b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_TME;
148b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
14958cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
150b7e04f8cSWim Van Sebroeck }
151b7e04f8cSWim Van Sebroeck 
152b7e04f8cSWim Van Sebroeck /**
153b7e04f8cSWim Van Sebroeck  * 	sh_wdt_keepalive - Keep the Userspace Watchdog Alive
154b7e04f8cSWim Van Sebroeck  * 	The Userspace watchdog got a KeepAlive: schedule the next heartbeat.
155b7e04f8cSWim Van Sebroeck  */
156b7e04f8cSWim Van Sebroeck static inline void sh_wdt_keepalive(void)
157b7e04f8cSWim Van Sebroeck {
15870b814ecSAlan Cox 	unsigned long flags;
15970b814ecSAlan Cox 
16058cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
161b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
16258cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
163b7e04f8cSWim Van Sebroeck }
164b7e04f8cSWim Van Sebroeck 
165b7e04f8cSWim Van Sebroeck /**
166b7e04f8cSWim Van Sebroeck  * 	sh_wdt_set_heartbeat - Set the Userspace Watchdog heartbeat
167b7e04f8cSWim Van Sebroeck  * 	Set the Userspace Watchdog heartbeat
168b7e04f8cSWim Van Sebroeck  */
169b7e04f8cSWim Van Sebroeck static int sh_wdt_set_heartbeat(int t)
170b7e04f8cSWim Van Sebroeck {
17170b814ecSAlan Cox 	unsigned long flags;
17270b814ecSAlan Cox 
17370b814ecSAlan Cox 	if (unlikely(t < 1 || t > 3600)) /* arbitrary upper limit */
174b7e04f8cSWim Van Sebroeck 		return -EINVAL;
175b7e04f8cSWim Van Sebroeck 
17658cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
177b7e04f8cSWim Van Sebroeck 	heartbeat = t;
17858cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
179b7e04f8cSWim Van Sebroeck 	return 0;
180b7e04f8cSWim Van Sebroeck }
181b7e04f8cSWim Van Sebroeck 
182b7e04f8cSWim Van Sebroeck /**
183b7e04f8cSWim Van Sebroeck  * 	sh_wdt_ping - Ping the Watchdog
184b7e04f8cSWim Van Sebroeck  *	@data: Unused
185b7e04f8cSWim Van Sebroeck  *
186b7e04f8cSWim Van Sebroeck  * 	Clears overflow bit, resets timer counter.
187b7e04f8cSWim Van Sebroeck  */
188b7e04f8cSWim Van Sebroeck static void sh_wdt_ping(unsigned long data)
189b7e04f8cSWim Van Sebroeck {
19070b814ecSAlan Cox 	unsigned long flags;
19170b814ecSAlan Cox 
19258cf4198SAdrian Bunk 	spin_lock_irqsave(&shwdt_lock, flags);
193b7e04f8cSWim Van Sebroeck 	if (time_before(jiffies, next_heartbeat)) {
194b7e04f8cSWim Van Sebroeck 		__u8 csr;
195b7e04f8cSWim Van Sebroeck 
196b7e04f8cSWim Van Sebroeck 		csr = sh_wdt_read_csr();
197b7e04f8cSWim Van Sebroeck 		csr &= ~WTCSR_IOVF;
198b7e04f8cSWim Van Sebroeck 		sh_wdt_write_csr(csr);
199b7e04f8cSWim Van Sebroeck 
200b7e04f8cSWim Van Sebroeck 		sh_wdt_write_cnt(0);
201b7e04f8cSWim Van Sebroeck 
202b7e04f8cSWim Van Sebroeck 		mod_timer(&timer, next_ping_period(clock_division_ratio));
203b7e04f8cSWim Van Sebroeck 	} else
204b7e04f8cSWim Van Sebroeck 		printk(KERN_WARNING PFX "Heartbeat lost! Will not ping "
205b7e04f8cSWim Van Sebroeck 		       "the watchdog\n");
20658cf4198SAdrian Bunk 	spin_unlock_irqrestore(&shwdt_lock, flags);
207b7e04f8cSWim Van Sebroeck }
208b7e04f8cSWim Van Sebroeck 
209b7e04f8cSWim Van Sebroeck /**
210b7e04f8cSWim Van Sebroeck  * 	sh_wdt_open - Open the Device
211b7e04f8cSWim Van Sebroeck  * 	@inode: inode of device
212b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
213b7e04f8cSWim Van Sebroeck  *
214b7e04f8cSWim Van Sebroeck  * 	Watchdog device is opened and started.
215b7e04f8cSWim Van Sebroeck  */
216b7e04f8cSWim Van Sebroeck static int sh_wdt_open(struct inode *inode, struct file *file)
217b7e04f8cSWim Van Sebroeck {
218b7e04f8cSWim Van Sebroeck 	if (test_and_set_bit(0, &shwdt_is_open))
219b7e04f8cSWim Van Sebroeck 		return -EBUSY;
220b7e04f8cSWim Van Sebroeck 	if (nowayout)
221b7e04f8cSWim Van Sebroeck 		__module_get(THIS_MODULE);
222b7e04f8cSWim Van Sebroeck 
223b7e04f8cSWim Van Sebroeck 	sh_wdt_start();
224b7e04f8cSWim Van Sebroeck 
225b7e04f8cSWim Van Sebroeck 	return nonseekable_open(inode, file);
226b7e04f8cSWim Van Sebroeck }
227b7e04f8cSWim Van Sebroeck 
228b7e04f8cSWim Van Sebroeck /**
229b7e04f8cSWim Van Sebroeck  * 	sh_wdt_close - Close the Device
230b7e04f8cSWim Van Sebroeck  * 	@inode: inode of device
231b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
232b7e04f8cSWim Van Sebroeck  *
233b7e04f8cSWim Van Sebroeck  * 	Watchdog device is closed and stopped.
234b7e04f8cSWim Van Sebroeck  */
235b7e04f8cSWim Van Sebroeck static int sh_wdt_close(struct inode *inode, struct file *file)
236b7e04f8cSWim Van Sebroeck {
237b7e04f8cSWim Van Sebroeck 	if (shwdt_expect_close == 42) {
238b7e04f8cSWim Van Sebroeck 		sh_wdt_stop();
239b7e04f8cSWim Van Sebroeck 	} else {
240b7e04f8cSWim Van Sebroeck 		printk(KERN_CRIT PFX "Unexpected close, not "
241b7e04f8cSWim Van Sebroeck 		       "stopping watchdog!\n");
242b7e04f8cSWim Van Sebroeck 		sh_wdt_keepalive();
243b7e04f8cSWim Van Sebroeck 	}
244b7e04f8cSWim Van Sebroeck 
245b7e04f8cSWim Van Sebroeck 	clear_bit(0, &shwdt_is_open);
246b7e04f8cSWim Van Sebroeck 	shwdt_expect_close = 0;
247b7e04f8cSWim Van Sebroeck 
248b7e04f8cSWim Van Sebroeck 	return 0;
249b7e04f8cSWim Van Sebroeck }
250b7e04f8cSWim Van Sebroeck 
251b7e04f8cSWim Van Sebroeck /**
252b7e04f8cSWim Van Sebroeck  * 	sh_wdt_write - Write to Device
253b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
254b7e04f8cSWim Van Sebroeck  * 	@buf: buffer to write
255b7e04f8cSWim Van Sebroeck  * 	@count: length of buffer
256b7e04f8cSWim Van Sebroeck  * 	@ppos: offset
257b7e04f8cSWim Van Sebroeck  *
258b7e04f8cSWim Van Sebroeck  * 	Pings the watchdog on write.
259b7e04f8cSWim Van Sebroeck  */
260b7e04f8cSWim Van Sebroeck static ssize_t sh_wdt_write(struct file *file, const char *buf,
261b7e04f8cSWim Van Sebroeck 			    size_t count, loff_t *ppos)
262b7e04f8cSWim Van Sebroeck {
263b7e04f8cSWim Van Sebroeck 	if (count) {
264b7e04f8cSWim Van Sebroeck 		if (!nowayout) {
265b7e04f8cSWim Van Sebroeck 			size_t i;
266b7e04f8cSWim Van Sebroeck 
267b7e04f8cSWim Van Sebroeck 			shwdt_expect_close = 0;
268b7e04f8cSWim Van Sebroeck 
269b7e04f8cSWim Van Sebroeck 			for (i = 0; i != count; i++) {
270b7e04f8cSWim Van Sebroeck 				char c;
271b7e04f8cSWim Van Sebroeck 				if (get_user(c, buf + i))
272b7e04f8cSWim Van Sebroeck 					return -EFAULT;
273b7e04f8cSWim Van Sebroeck 				if (c == 'V')
274b7e04f8cSWim Van Sebroeck 					shwdt_expect_close = 42;
275b7e04f8cSWim Van Sebroeck 			}
276b7e04f8cSWim Van Sebroeck 		}
277b7e04f8cSWim Van Sebroeck 		sh_wdt_keepalive();
278b7e04f8cSWim Van Sebroeck 	}
279b7e04f8cSWim Van Sebroeck 
280b7e04f8cSWim Van Sebroeck 	return count;
281b7e04f8cSWim Van Sebroeck }
282b7e04f8cSWim Van Sebroeck 
283b7e04f8cSWim Van Sebroeck /**
284b7e04f8cSWim Van Sebroeck  * 	sh_wdt_mmap - map WDT/CPG registers into userspace
285b7e04f8cSWim Van Sebroeck  * 	@file: file structure for the device
286b7e04f8cSWim Van Sebroeck  * 	@vma: VMA to map the registers into
287b7e04f8cSWim Van Sebroeck  *
288b7e04f8cSWim Van Sebroeck  * 	A simple mmap() implementation for the corner cases where the counter
289b7e04f8cSWim Van Sebroeck  * 	needs to be mapped in userspace directly. Due to the relatively small
290b7e04f8cSWim Van Sebroeck  * 	size of the area, neighbouring registers not necessarily tied to the
291b7e04f8cSWim Van Sebroeck  * 	CPG will also be accessible through the register page, so this remains
292b7e04f8cSWim Van Sebroeck  * 	configurable for users that really know what they're doing.
293b7e04f8cSWim Van Sebroeck  *
294b7e04f8cSWim Van Sebroeck  *	Additionaly, the register page maps in the CPG register base relative
295b7e04f8cSWim Van Sebroeck  *	to the nearest page-aligned boundary, which requires that userspace do
296b7e04f8cSWim Van Sebroeck  *	the appropriate CPU subtype math for calculating the page offset for
297b7e04f8cSWim Van Sebroeck  *	the counter value.
298b7e04f8cSWim Van Sebroeck  */
299b7e04f8cSWim Van Sebroeck static int sh_wdt_mmap(struct file *file, struct vm_area_struct *vma)
300b7e04f8cSWim Van Sebroeck {
301b7e04f8cSWim Van Sebroeck 	int ret = -ENOSYS;
302b7e04f8cSWim Van Sebroeck 
303b7e04f8cSWim Van Sebroeck #ifdef CONFIG_SH_WDT_MMAP
304b7e04f8cSWim Van Sebroeck 	unsigned long addr;
305b7e04f8cSWim Van Sebroeck 
306b7e04f8cSWim Van Sebroeck 	/* Only support the simple cases where we map in a register page. */
307b7e04f8cSWim Van Sebroeck 	if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
308b7e04f8cSWim Van Sebroeck 		return -EINVAL;
309b7e04f8cSWim Van Sebroeck 
310b7e04f8cSWim Van Sebroeck 	/*
311b7e04f8cSWim Van Sebroeck 	 * Pick WTCNT as the start, it's usually the first register after the
312b7e04f8cSWim Van Sebroeck 	 * FRQCR, and neither one are generally page-aligned out of the box.
313b7e04f8cSWim Van Sebroeck 	 */
314b7e04f8cSWim Van Sebroeck 	addr = WTCNT & ~(PAGE_SIZE - 1);
315b7e04f8cSWim Van Sebroeck 
316b7e04f8cSWim Van Sebroeck 	vma->vm_flags |= VM_IO;
317b7e04f8cSWim Van Sebroeck 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
318b7e04f8cSWim Van Sebroeck 
319b7e04f8cSWim Van Sebroeck 	if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
320b7e04f8cSWim Van Sebroeck 			       PAGE_SIZE, vma->vm_page_prot)) {
321b7e04f8cSWim Van Sebroeck 		printk(KERN_ERR PFX "%s: io_remap_pfn_range failed\n",
322fa9363c5SHarvey Harrison 		       __func__);
323b7e04f8cSWim Van Sebroeck 		return -EAGAIN;
324b7e04f8cSWim Van Sebroeck 	}
325b7e04f8cSWim Van Sebroeck 
326b7e04f8cSWim Van Sebroeck 	ret = 0;
327b7e04f8cSWim Van Sebroeck #endif
328b7e04f8cSWim Van Sebroeck 
329b7e04f8cSWim Van Sebroeck 	return ret;
330b7e04f8cSWim Van Sebroeck }
331b7e04f8cSWim Van Sebroeck 
332b7e04f8cSWim Van Sebroeck /**
333b7e04f8cSWim Van Sebroeck  * 	sh_wdt_ioctl - Query Device
334b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
335b7e04f8cSWim Van Sebroeck  * 	@cmd: watchdog command
336b7e04f8cSWim Van Sebroeck  * 	@arg: argument
337b7e04f8cSWim Van Sebroeck  *
338b7e04f8cSWim Van Sebroeck  * 	Query basic information from the device or ping it, as outlined by the
339b7e04f8cSWim Van Sebroeck  * 	watchdog API.
340b7e04f8cSWim Van Sebroeck  */
34170b814ecSAlan Cox static long sh_wdt_ioctl(struct file *file, unsigned int cmd,
34270b814ecSAlan Cox 							unsigned long arg)
343b7e04f8cSWim Van Sebroeck {
344b7e04f8cSWim Van Sebroeck 	int new_heartbeat;
345b7e04f8cSWim Van Sebroeck 	int options, retval = -EINVAL;
346b7e04f8cSWim Van Sebroeck 
347b7e04f8cSWim Van Sebroeck 	switch (cmd) {
348b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSUPPORT:
349b7e04f8cSWim Van Sebroeck 		return copy_to_user((struct watchdog_info *)arg,
35070b814ecSAlan Cox 			  &sh_wdt_info, sizeof(sh_wdt_info)) ? -EFAULT : 0;
351b7e04f8cSWim Van Sebroeck 	case WDIOC_GETSTATUS:
352b7e04f8cSWim Van Sebroeck 	case WDIOC_GETBOOTSTATUS:
353b7e04f8cSWim Van Sebroeck 		return put_user(0, (int *)arg);
354b7e04f8cSWim Van Sebroeck 	case WDIOC_SETOPTIONS:
355b7e04f8cSWim Van Sebroeck 		if (get_user(options, (int *)arg))
356b7e04f8cSWim Van Sebroeck 			return -EFAULT;
357b7e04f8cSWim Van Sebroeck 
358b7e04f8cSWim Van Sebroeck 		if (options & WDIOS_DISABLECARD) {
359b7e04f8cSWim Van Sebroeck 			sh_wdt_stop();
360b7e04f8cSWim Van Sebroeck 			retval = 0;
361b7e04f8cSWim Van Sebroeck 		}
362b7e04f8cSWim Van Sebroeck 
363b7e04f8cSWim Van Sebroeck 		if (options & WDIOS_ENABLECARD) {
364b7e04f8cSWim Van Sebroeck 			sh_wdt_start();
365b7e04f8cSWim Van Sebroeck 			retval = 0;
366b7e04f8cSWim Van Sebroeck 		}
367b7e04f8cSWim Van Sebroeck 
368b7e04f8cSWim Van Sebroeck 		return retval;
3690c06090cSWim Van Sebroeck 	case WDIOC_KEEPALIVE:
3700c06090cSWim Van Sebroeck 		sh_wdt_keepalive();
3710c06090cSWim Van Sebroeck 		return 0;
3720c06090cSWim Van Sebroeck 	case WDIOC_SETTIMEOUT:
3730c06090cSWim Van Sebroeck 		if (get_user(new_heartbeat, (int *)arg))
3740c06090cSWim Van Sebroeck 			return -EFAULT;
3750c06090cSWim Van Sebroeck 
3760c06090cSWim Van Sebroeck 		if (sh_wdt_set_heartbeat(new_heartbeat))
3770c06090cSWim Van Sebroeck 			return -EINVAL;
3780c06090cSWim Van Sebroeck 
3790c06090cSWim Van Sebroeck 		sh_wdt_keepalive();
3800c06090cSWim Van Sebroeck 		/* Fall */
3810c06090cSWim Van Sebroeck 	case WDIOC_GETTIMEOUT:
3820c06090cSWim Van Sebroeck 		return put_user(heartbeat, (int *)arg);
383b7e04f8cSWim Van Sebroeck 	default:
384b7e04f8cSWim Van Sebroeck 		return -ENOTTY;
385b7e04f8cSWim Van Sebroeck 	}
386b7e04f8cSWim Van Sebroeck 	return 0;
387b7e04f8cSWim Van Sebroeck }
388b7e04f8cSWim Van Sebroeck 
389b7e04f8cSWim Van Sebroeck /**
390b7e04f8cSWim Van Sebroeck  * 	sh_wdt_notify_sys - Notifier Handler
391b7e04f8cSWim Van Sebroeck  * 	@this: notifier block
392b7e04f8cSWim Van Sebroeck  * 	@code: notifier event
393b7e04f8cSWim Van Sebroeck  * 	@unused: unused
394b7e04f8cSWim Van Sebroeck  *
395b7e04f8cSWim Van Sebroeck  * 	Handles specific events, such as turning off the watchdog during a
396b7e04f8cSWim Van Sebroeck  * 	shutdown event.
397b7e04f8cSWim Van Sebroeck  */
398b7e04f8cSWim Van Sebroeck static int sh_wdt_notify_sys(struct notifier_block *this,
399b7e04f8cSWim Van Sebroeck 			     unsigned long code, void *unused)
400b7e04f8cSWim Van Sebroeck {
401b7e04f8cSWim Van Sebroeck 	if (code == SYS_DOWN || code == SYS_HALT)
402b7e04f8cSWim Van Sebroeck 		sh_wdt_stop();
403b7e04f8cSWim Van Sebroeck 
404b7e04f8cSWim Van Sebroeck 	return NOTIFY_DONE;
405b7e04f8cSWim Van Sebroeck }
406b7e04f8cSWim Van Sebroeck 
407b7e04f8cSWim Van Sebroeck static const struct file_operations sh_wdt_fops = {
408b7e04f8cSWim Van Sebroeck 	.owner		= THIS_MODULE,
409b7e04f8cSWim Van Sebroeck 	.llseek		= no_llseek,
410b7e04f8cSWim Van Sebroeck 	.write		= sh_wdt_write,
41170b814ecSAlan Cox 	.unlocked_ioctl	= sh_wdt_ioctl,
412b7e04f8cSWim Van Sebroeck 	.open		= sh_wdt_open,
413b7e04f8cSWim Van Sebroeck 	.release	= sh_wdt_close,
414b7e04f8cSWim Van Sebroeck 	.mmap		= sh_wdt_mmap,
415b7e04f8cSWim Van Sebroeck };
416b7e04f8cSWim Van Sebroeck 
41770b814ecSAlan Cox static const struct watchdog_info sh_wdt_info = {
418b7e04f8cSWim Van Sebroeck 	.options		= WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
419b7e04f8cSWim Van Sebroeck 				  WDIOF_MAGICCLOSE,
420b7e04f8cSWim Van Sebroeck 	.firmware_version	= 1,
421b7e04f8cSWim Van Sebroeck 	.identity		= "SH WDT",
422b7e04f8cSWim Van Sebroeck };
423b7e04f8cSWim Van Sebroeck 
424b7e04f8cSWim Van Sebroeck static struct notifier_block sh_wdt_notifier = {
425b7e04f8cSWim Van Sebroeck 	.notifier_call		= sh_wdt_notify_sys,
426b7e04f8cSWim Van Sebroeck };
427b7e04f8cSWim Van Sebroeck 
428b7e04f8cSWim Van Sebroeck static struct miscdevice sh_wdt_miscdev = {
429b7e04f8cSWim Van Sebroeck 	.minor		= WATCHDOG_MINOR,
430b7e04f8cSWim Van Sebroeck 	.name		= "watchdog",
431b7e04f8cSWim Van Sebroeck 	.fops		= &sh_wdt_fops,
432b7e04f8cSWim Van Sebroeck };
433b7e04f8cSWim Van Sebroeck 
434b7e04f8cSWim Van Sebroeck /**
435b7e04f8cSWim Van Sebroeck  * 	sh_wdt_init - Initialize module
436b7e04f8cSWim Van Sebroeck  * 	Registers the device and notifier handler. Actual device
437b7e04f8cSWim Van Sebroeck  * 	initialization is handled by sh_wdt_open().
438b7e04f8cSWim Van Sebroeck  */
439b7e04f8cSWim Van Sebroeck static int __init sh_wdt_init(void)
440b7e04f8cSWim Van Sebroeck {
441b7e04f8cSWim Van Sebroeck 	int rc;
442b7e04f8cSWim Van Sebroeck 
44370b814ecSAlan Cox 	if (clock_division_ratio < 0x5 || clock_division_ratio > 0x7) {
444b7e04f8cSWim Van Sebroeck 		clock_division_ratio = WTCSR_CKS_4096;
44570b814ecSAlan Cox 		printk(KERN_INFO PFX
44670b814ecSAlan Cox 		  "clock_division_ratio value must be 0x5<=x<=0x7, using %d\n",
44770b814ecSAlan Cox 				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;
45370b814ecSAlan Cox 		printk(KERN_INFO PFX
45470b814ecSAlan Cox 			"heartbeat value must be 1<=x<=3600, using %d\n",
45570b814ecSAlan Cox 								heartbeat);
456b7e04f8cSWim Van Sebroeck 	}
457b7e04f8cSWim Van Sebroeck 
458b7e04f8cSWim Van Sebroeck 	rc = register_reboot_notifier(&sh_wdt_notifier);
459b7e04f8cSWim Van Sebroeck 	if (unlikely(rc)) {
46070b814ecSAlan Cox 		printk(KERN_ERR PFX
46170b814ecSAlan Cox 			"Can't register reboot notifier (err=%d)\n", rc);
462b7e04f8cSWim Van Sebroeck 		return rc;
463b7e04f8cSWim Van Sebroeck 	}
464b7e04f8cSWim Van Sebroeck 
465b7e04f8cSWim Van Sebroeck 	rc = misc_register(&sh_wdt_miscdev);
466b7e04f8cSWim Van Sebroeck 	if (unlikely(rc)) {
46770b814ecSAlan Cox 		printk(KERN_ERR PFX
46870b814ecSAlan Cox 			"Can't register miscdev on minor=%d (err=%d)\n",
46970b814ecSAlan Cox 						sh_wdt_miscdev.minor, rc);
470b7e04f8cSWim Van Sebroeck 		unregister_reboot_notifier(&sh_wdt_notifier);
471b7e04f8cSWim Van Sebroeck 		return rc;
472b7e04f8cSWim Van Sebroeck 	}
473b7e04f8cSWim Van Sebroeck 
474b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
475b7e04f8cSWim Van Sebroeck 		heartbeat, nowayout);
476b7e04f8cSWim Van Sebroeck 
477b7e04f8cSWim Van Sebroeck 	return 0;
478b7e04f8cSWim Van Sebroeck }
479b7e04f8cSWim Van Sebroeck 
480b7e04f8cSWim Van Sebroeck /**
481b7e04f8cSWim Van Sebroeck  * 	sh_wdt_exit - Deinitialize module
482b7e04f8cSWim Van Sebroeck  * 	Unregisters the device and notifier handler. Actual device
483b7e04f8cSWim Van Sebroeck  * 	deinitialization is handled by sh_wdt_close().
484b7e04f8cSWim Van Sebroeck  */
485b7e04f8cSWim Van Sebroeck static void __exit sh_wdt_exit(void)
486b7e04f8cSWim Van Sebroeck {
487b7e04f8cSWim Van Sebroeck 	misc_deregister(&sh_wdt_miscdev);
488b7e04f8cSWim Van Sebroeck 	unregister_reboot_notifier(&sh_wdt_notifier);
489b7e04f8cSWim Van Sebroeck }
490b7e04f8cSWim Van Sebroeck 
491b7e04f8cSWim Van Sebroeck MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
492b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("SuperH watchdog driver");
493b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL");
494b7e04f8cSWim Van Sebroeck MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
495b7e04f8cSWim Van Sebroeck 
496b7e04f8cSWim Van Sebroeck module_param(clock_division_ratio, int, 0);
497a77dba7eSWim Van Sebroeck MODULE_PARM_DESC(clock_division_ratio,
498a77dba7eSWim Van Sebroeck 	"Clock division ratio. Valid ranges are from 0x5 (1.31ms) "
499*76550d32SRandy Dunlap 	"to 0x7 (5.25ms). (default=" __MODULE_STRING(WTCSR_CKS_4096) ")");
500b7e04f8cSWim Van Sebroeck 
501b7e04f8cSWim Van Sebroeck module_param(heartbeat, int, 0);
50270b814ecSAlan Cox MODULE_PARM_DESC(heartbeat,
50370b814ecSAlan Cox 	"Watchdog heartbeat in seconds. (1 <= heartbeat <= 3600, default="
50470b814ecSAlan Cox 				__MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
505b7e04f8cSWim Van Sebroeck 
506b7e04f8cSWim Van Sebroeck module_param(nowayout, int, 0);
50770b814ecSAlan Cox MODULE_PARM_DESC(nowayout,
50870b814ecSAlan Cox 	"Watchdog cannot be stopped once started (default="
50970b814ecSAlan Cox 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
510b7e04f8cSWim Van Sebroeck 
511b7e04f8cSWim Van Sebroeck module_init(sh_wdt_init);
512b7e04f8cSWim Van Sebroeck module_exit(sh_wdt_exit);
513