xref: /linux/drivers/watchdog/shwdt.c (revision ee0fc097ef47a4a6ff6b4800f2391030131b7828)
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>
31b7e04f8cSWim Van Sebroeck #include <asm/io.h>
32b7e04f8cSWim Van Sebroeck #include <asm/uaccess.h>
33b7e04f8cSWim Van Sebroeck #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
55*ee0fc097SJoe 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;
71b7e04f8cSWim Van Sebroeck static 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;
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 
79b7e04f8cSWim Van Sebroeck static int nowayout = WATCHDOG_NOWAYOUT;
80b7e04f8cSWim Van Sebroeck 
81b7e04f8cSWim Van Sebroeck /**
82b7e04f8cSWim Van Sebroeck  * 	sh_wdt_start - Start the Watchdog
83b7e04f8cSWim Van Sebroeck  *
84b7e04f8cSWim Van Sebroeck  * 	Starts the watchdog.
85b7e04f8cSWim Van Sebroeck  */
86b7e04f8cSWim Van Sebroeck static void sh_wdt_start(void)
87b7e04f8cSWim Van Sebroeck {
88b7e04f8cSWim Van Sebroeck 	__u8 csr;
89b7e04f8cSWim Van Sebroeck 
90b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
91b7e04f8cSWim Van Sebroeck 	mod_timer(&timer, next_ping_period(clock_division_ratio));
92b7e04f8cSWim Van Sebroeck 
93b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
94b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_WT | clock_division_ratio;
95b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
96b7e04f8cSWim Van Sebroeck 
97b7e04f8cSWim Van Sebroeck 	sh_wdt_write_cnt(0);
98b7e04f8cSWim Van Sebroeck 
99b7e04f8cSWim Van Sebroeck 	/*
100b7e04f8cSWim Van Sebroeck 	 * These processors have a bit of an inconsistent initialization
101b7e04f8cSWim Van Sebroeck 	 * process.. starting with SH-3, RSTS was moved to WTCSR, and the
102b7e04f8cSWim Van Sebroeck 	 * RSTCSR register was removed.
103b7e04f8cSWim Van Sebroeck 	 *
104b7e04f8cSWim Van Sebroeck 	 * On the SH-2 however, in addition with bits being in different
105b7e04f8cSWim Van Sebroeck 	 * locations, we must deal with RSTCSR outright..
106b7e04f8cSWim Van Sebroeck 	 */
107b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
108b7e04f8cSWim Van Sebroeck 	csr |= WTCSR_TME;
109b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_RSTS;
110b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
111b7e04f8cSWim Van Sebroeck 
112b7e04f8cSWim Van Sebroeck #ifdef CONFIG_CPU_SH2
113b7e04f8cSWim Van Sebroeck 	/*
114b7e04f8cSWim Van Sebroeck 	 * Whoever came up with the RSTCSR semantics must've been smoking
115b7e04f8cSWim Van Sebroeck 	 * some of the good stuff, since in addition to the WTCSR/WTCNT write
116b7e04f8cSWim Van Sebroeck 	 * brain-damage, it's managed to fuck things up one step further..
117b7e04f8cSWim Van Sebroeck 	 *
118b7e04f8cSWim Van Sebroeck 	 * If we need to clear the WOVF bit, the upper byte has to be 0xa5..
119b7e04f8cSWim Van Sebroeck 	 * but if we want to touch RSTE or RSTS, the upper byte has to be
120b7e04f8cSWim Van Sebroeck 	 * 0x5a..
121b7e04f8cSWim Van Sebroeck 	 */
122b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_rstcsr();
123b7e04f8cSWim Van Sebroeck 	csr &= ~RSTCSR_RSTS;
124b7e04f8cSWim Van Sebroeck 	sh_wdt_write_rstcsr(csr);
125b7e04f8cSWim Van Sebroeck #endif
126b7e04f8cSWim Van Sebroeck }
127b7e04f8cSWim Van Sebroeck 
128b7e04f8cSWim Van Sebroeck /**
129b7e04f8cSWim Van Sebroeck  * 	sh_wdt_stop - Stop the Watchdog
130b7e04f8cSWim Van Sebroeck  * 	Stops the watchdog.
131b7e04f8cSWim Van Sebroeck  */
132b7e04f8cSWim Van Sebroeck static void sh_wdt_stop(void)
133b7e04f8cSWim Van Sebroeck {
134b7e04f8cSWim Van Sebroeck 	__u8 csr;
135b7e04f8cSWim Van Sebroeck 
136b7e04f8cSWim Van Sebroeck 	del_timer(&timer);
137b7e04f8cSWim Van Sebroeck 
138b7e04f8cSWim Van Sebroeck 	csr = sh_wdt_read_csr();
139b7e04f8cSWim Van Sebroeck 	csr &= ~WTCSR_TME;
140b7e04f8cSWim Van Sebroeck 	sh_wdt_write_csr(csr);
141b7e04f8cSWim Van Sebroeck }
142b7e04f8cSWim Van Sebroeck 
143b7e04f8cSWim Van Sebroeck /**
144b7e04f8cSWim Van Sebroeck  * 	sh_wdt_keepalive - Keep the Userspace Watchdog Alive
145b7e04f8cSWim Van Sebroeck  * 	The Userspace watchdog got a KeepAlive: schedule the next heartbeat.
146b7e04f8cSWim Van Sebroeck  */
147b7e04f8cSWim Van Sebroeck static inline void sh_wdt_keepalive(void)
148b7e04f8cSWim Van Sebroeck {
149b7e04f8cSWim Van Sebroeck 	next_heartbeat = jiffies + (heartbeat * HZ);
150b7e04f8cSWim Van Sebroeck }
151b7e04f8cSWim Van Sebroeck 
152b7e04f8cSWim Van Sebroeck /**
153b7e04f8cSWim Van Sebroeck  * 	sh_wdt_set_heartbeat - Set the Userspace Watchdog heartbeat
154b7e04f8cSWim Van Sebroeck  * 	Set the Userspace Watchdog heartbeat
155b7e04f8cSWim Van Sebroeck  */
156b7e04f8cSWim Van Sebroeck static int sh_wdt_set_heartbeat(int t)
157b7e04f8cSWim Van Sebroeck {
158b7e04f8cSWim Van Sebroeck 	if (unlikely((t < 1) || (t > 3600))) /* arbitrary upper limit */
159b7e04f8cSWim Van Sebroeck 		return -EINVAL;
160b7e04f8cSWim Van Sebroeck 
161b7e04f8cSWim Van Sebroeck 	heartbeat = t;
162b7e04f8cSWim Van Sebroeck 	return 0;
163b7e04f8cSWim Van Sebroeck }
164b7e04f8cSWim Van Sebroeck 
165b7e04f8cSWim Van Sebroeck /**
166b7e04f8cSWim Van Sebroeck  * 	sh_wdt_ping - Ping the Watchdog
167b7e04f8cSWim Van Sebroeck  *	@data: Unused
168b7e04f8cSWim Van Sebroeck  *
169b7e04f8cSWim Van Sebroeck  * 	Clears overflow bit, resets timer counter.
170b7e04f8cSWim Van Sebroeck  */
171b7e04f8cSWim Van Sebroeck static void sh_wdt_ping(unsigned long data)
172b7e04f8cSWim Van Sebroeck {
173b7e04f8cSWim Van Sebroeck 	if (time_before(jiffies, next_heartbeat)) {
174b7e04f8cSWim Van Sebroeck 		__u8 csr;
175b7e04f8cSWim Van Sebroeck 
176b7e04f8cSWim Van Sebroeck 		csr = sh_wdt_read_csr();
177b7e04f8cSWim Van Sebroeck 		csr &= ~WTCSR_IOVF;
178b7e04f8cSWim Van Sebroeck 		sh_wdt_write_csr(csr);
179b7e04f8cSWim Van Sebroeck 
180b7e04f8cSWim Van Sebroeck 		sh_wdt_write_cnt(0);
181b7e04f8cSWim Van Sebroeck 
182b7e04f8cSWim Van Sebroeck 		mod_timer(&timer, next_ping_period(clock_division_ratio));
183b7e04f8cSWim Van Sebroeck 	} else
184b7e04f8cSWim Van Sebroeck 		printk(KERN_WARNING PFX "Heartbeat lost! Will not ping "
185b7e04f8cSWim Van Sebroeck 		       "the watchdog\n");
186b7e04f8cSWim Van Sebroeck }
187b7e04f8cSWim Van Sebroeck 
188b7e04f8cSWim Van Sebroeck /**
189b7e04f8cSWim Van Sebroeck  * 	sh_wdt_open - Open the Device
190b7e04f8cSWim Van Sebroeck  * 	@inode: inode of device
191b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
192b7e04f8cSWim Van Sebroeck  *
193b7e04f8cSWim Van Sebroeck  * 	Watchdog device is opened and started.
194b7e04f8cSWim Van Sebroeck  */
195b7e04f8cSWim Van Sebroeck static int sh_wdt_open(struct inode *inode, struct file *file)
196b7e04f8cSWim Van Sebroeck {
197b7e04f8cSWim Van Sebroeck 	if (test_and_set_bit(0, &shwdt_is_open))
198b7e04f8cSWim Van Sebroeck 		return -EBUSY;
199b7e04f8cSWim Van Sebroeck 	if (nowayout)
200b7e04f8cSWim Van Sebroeck 		__module_get(THIS_MODULE);
201b7e04f8cSWim Van Sebroeck 
202b7e04f8cSWim Van Sebroeck 	sh_wdt_start();
203b7e04f8cSWim Van Sebroeck 
204b7e04f8cSWim Van Sebroeck 	return nonseekable_open(inode, file);
205b7e04f8cSWim Van Sebroeck }
206b7e04f8cSWim Van Sebroeck 
207b7e04f8cSWim Van Sebroeck /**
208b7e04f8cSWim Van Sebroeck  * 	sh_wdt_close - Close the Device
209b7e04f8cSWim Van Sebroeck  * 	@inode: inode of device
210b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
211b7e04f8cSWim Van Sebroeck  *
212b7e04f8cSWim Van Sebroeck  * 	Watchdog device is closed and stopped.
213b7e04f8cSWim Van Sebroeck  */
214b7e04f8cSWim Van Sebroeck static int sh_wdt_close(struct inode *inode, struct file *file)
215b7e04f8cSWim Van Sebroeck {
216b7e04f8cSWim Van Sebroeck 	if (shwdt_expect_close == 42) {
217b7e04f8cSWim Van Sebroeck 		sh_wdt_stop();
218b7e04f8cSWim Van Sebroeck 	} else {
219b7e04f8cSWim Van Sebroeck 		printk(KERN_CRIT PFX "Unexpected close, not "
220b7e04f8cSWim Van Sebroeck 		       "stopping watchdog!\n");
221b7e04f8cSWim Van Sebroeck 		sh_wdt_keepalive();
222b7e04f8cSWim Van Sebroeck 	}
223b7e04f8cSWim Van Sebroeck 
224b7e04f8cSWim Van Sebroeck 	clear_bit(0, &shwdt_is_open);
225b7e04f8cSWim Van Sebroeck 	shwdt_expect_close = 0;
226b7e04f8cSWim Van Sebroeck 
227b7e04f8cSWim Van Sebroeck 	return 0;
228b7e04f8cSWim Van Sebroeck }
229b7e04f8cSWim Van Sebroeck 
230b7e04f8cSWim Van Sebroeck /**
231b7e04f8cSWim Van Sebroeck  * 	sh_wdt_write - Write to Device
232b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
233b7e04f8cSWim Van Sebroeck  * 	@buf: buffer to write
234b7e04f8cSWim Van Sebroeck  * 	@count: length of buffer
235b7e04f8cSWim Van Sebroeck  * 	@ppos: offset
236b7e04f8cSWim Van Sebroeck  *
237b7e04f8cSWim Van Sebroeck  * 	Pings the watchdog on write.
238b7e04f8cSWim Van Sebroeck  */
239b7e04f8cSWim Van Sebroeck static ssize_t sh_wdt_write(struct file *file, const char *buf,
240b7e04f8cSWim Van Sebroeck 			    size_t count, loff_t *ppos)
241b7e04f8cSWim Van Sebroeck {
242b7e04f8cSWim Van Sebroeck 	if (count) {
243b7e04f8cSWim Van Sebroeck 		if (!nowayout) {
244b7e04f8cSWim Van Sebroeck 			size_t i;
245b7e04f8cSWim Van Sebroeck 
246b7e04f8cSWim Van Sebroeck 			shwdt_expect_close = 0;
247b7e04f8cSWim Van Sebroeck 
248b7e04f8cSWim Van Sebroeck 			for (i = 0; i != count; i++) {
249b7e04f8cSWim Van Sebroeck 				char c;
250b7e04f8cSWim Van Sebroeck 				if (get_user(c, buf + i))
251b7e04f8cSWim Van Sebroeck 					return -EFAULT;
252b7e04f8cSWim Van Sebroeck 				if (c == 'V')
253b7e04f8cSWim Van Sebroeck 					shwdt_expect_close = 42;
254b7e04f8cSWim Van Sebroeck 			}
255b7e04f8cSWim Van Sebroeck 		}
256b7e04f8cSWim Van Sebroeck 		sh_wdt_keepalive();
257b7e04f8cSWim Van Sebroeck 	}
258b7e04f8cSWim Van Sebroeck 
259b7e04f8cSWim Van Sebroeck 	return count;
260b7e04f8cSWim Van Sebroeck }
261b7e04f8cSWim Van Sebroeck 
262b7e04f8cSWim Van Sebroeck /**
263b7e04f8cSWim Van Sebroeck  * 	sh_wdt_mmap - map WDT/CPG registers into userspace
264b7e04f8cSWim Van Sebroeck  * 	@file: file structure for the device
265b7e04f8cSWim Van Sebroeck  * 	@vma: VMA to map the registers into
266b7e04f8cSWim Van Sebroeck  *
267b7e04f8cSWim Van Sebroeck  * 	A simple mmap() implementation for the corner cases where the counter
268b7e04f8cSWim Van Sebroeck  * 	needs to be mapped in userspace directly. Due to the relatively small
269b7e04f8cSWim Van Sebroeck  * 	size of the area, neighbouring registers not necessarily tied to the
270b7e04f8cSWim Van Sebroeck  * 	CPG will also be accessible through the register page, so this remains
271b7e04f8cSWim Van Sebroeck  * 	configurable for users that really know what they're doing.
272b7e04f8cSWim Van Sebroeck  *
273b7e04f8cSWim Van Sebroeck  *	Additionaly, the register page maps in the CPG register base relative
274b7e04f8cSWim Van Sebroeck  *	to the nearest page-aligned boundary, which requires that userspace do
275b7e04f8cSWim Van Sebroeck  *	the appropriate CPU subtype math for calculating the page offset for
276b7e04f8cSWim Van Sebroeck  *	the counter value.
277b7e04f8cSWim Van Sebroeck  */
278b7e04f8cSWim Van Sebroeck static int sh_wdt_mmap(struct file *file, struct vm_area_struct *vma)
279b7e04f8cSWim Van Sebroeck {
280b7e04f8cSWim Van Sebroeck 	int ret = -ENOSYS;
281b7e04f8cSWim Van Sebroeck 
282b7e04f8cSWim Van Sebroeck #ifdef CONFIG_SH_WDT_MMAP
283b7e04f8cSWim Van Sebroeck 	unsigned long addr;
284b7e04f8cSWim Van Sebroeck 
285b7e04f8cSWim Van Sebroeck 	/* Only support the simple cases where we map in a register page. */
286b7e04f8cSWim Van Sebroeck 	if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
287b7e04f8cSWim Van Sebroeck 		return -EINVAL;
288b7e04f8cSWim Van Sebroeck 
289b7e04f8cSWim Van Sebroeck 	/*
290b7e04f8cSWim Van Sebroeck 	 * Pick WTCNT as the start, it's usually the first register after the
291b7e04f8cSWim Van Sebroeck 	 * FRQCR, and neither one are generally page-aligned out of the box.
292b7e04f8cSWim Van Sebroeck 	 */
293b7e04f8cSWim Van Sebroeck 	addr = WTCNT & ~(PAGE_SIZE - 1);
294b7e04f8cSWim Van Sebroeck 
295b7e04f8cSWim Van Sebroeck 	vma->vm_flags |= VM_IO;
296b7e04f8cSWim Van Sebroeck 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
297b7e04f8cSWim Van Sebroeck 
298b7e04f8cSWim Van Sebroeck 	if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
299b7e04f8cSWim Van Sebroeck 			       PAGE_SIZE, vma->vm_page_prot)) {
300b7e04f8cSWim Van Sebroeck 		printk(KERN_ERR PFX "%s: io_remap_pfn_range failed\n",
301b7e04f8cSWim Van Sebroeck 		       __FUNCTION__);
302b7e04f8cSWim Van Sebroeck 		return -EAGAIN;
303b7e04f8cSWim Van Sebroeck 	}
304b7e04f8cSWim Van Sebroeck 
305b7e04f8cSWim Van Sebroeck 	ret = 0;
306b7e04f8cSWim Van Sebroeck #endif
307b7e04f8cSWim Van Sebroeck 
308b7e04f8cSWim Van Sebroeck 	return ret;
309b7e04f8cSWim Van Sebroeck }
310b7e04f8cSWim Van Sebroeck 
311b7e04f8cSWim Van Sebroeck /**
312b7e04f8cSWim Van Sebroeck  * 	sh_wdt_ioctl - Query Device
313b7e04f8cSWim Van Sebroeck  * 	@inode: inode of device
314b7e04f8cSWim Van Sebroeck  * 	@file: file handle of device
315b7e04f8cSWim Van Sebroeck  * 	@cmd: watchdog command
316b7e04f8cSWim Van Sebroeck  * 	@arg: argument
317b7e04f8cSWim Van Sebroeck  *
318b7e04f8cSWim Van Sebroeck  * 	Query basic information from the device or ping it, as outlined by the
319b7e04f8cSWim Van Sebroeck  * 	watchdog API.
320b7e04f8cSWim Van Sebroeck  */
321b7e04f8cSWim Van Sebroeck static int sh_wdt_ioctl(struct inode *inode, struct file *file,
322b7e04f8cSWim Van Sebroeck 			unsigned int cmd, unsigned long arg)
323b7e04f8cSWim Van Sebroeck {
324b7e04f8cSWim Van Sebroeck 	int new_heartbeat;
325b7e04f8cSWim Van Sebroeck 	int options, retval = -EINVAL;
326b7e04f8cSWim Van Sebroeck 
327b7e04f8cSWim Van Sebroeck 	switch (cmd) {
328b7e04f8cSWim Van Sebroeck 		case WDIOC_GETSUPPORT:
329b7e04f8cSWim Van Sebroeck 			return copy_to_user((struct watchdog_info *)arg,
330b7e04f8cSWim Van Sebroeck 					  &sh_wdt_info,
331b7e04f8cSWim Van Sebroeck 					  sizeof(sh_wdt_info)) ? -EFAULT : 0;
332b7e04f8cSWim Van Sebroeck 		case WDIOC_GETSTATUS:
333b7e04f8cSWim Van Sebroeck 		case WDIOC_GETBOOTSTATUS:
334b7e04f8cSWim Van Sebroeck 			return put_user(0, (int *)arg);
335b7e04f8cSWim Van Sebroeck 		case WDIOC_KEEPALIVE:
336b7e04f8cSWim Van Sebroeck 			sh_wdt_keepalive();
337b7e04f8cSWim Van Sebroeck 			return 0;
338b7e04f8cSWim Van Sebroeck 		case WDIOC_SETTIMEOUT:
339b7e04f8cSWim Van Sebroeck 			if (get_user(new_heartbeat, (int *)arg))
340b7e04f8cSWim Van Sebroeck 				return -EFAULT;
341b7e04f8cSWim Van Sebroeck 
342b7e04f8cSWim Van Sebroeck 			if (sh_wdt_set_heartbeat(new_heartbeat))
343b7e04f8cSWim Van Sebroeck 				return -EINVAL;
344b7e04f8cSWim Van Sebroeck 
345b7e04f8cSWim Van Sebroeck 			sh_wdt_keepalive();
346b7e04f8cSWim Van Sebroeck 			/* Fall */
347b7e04f8cSWim Van Sebroeck 		case WDIOC_GETTIMEOUT:
348b7e04f8cSWim Van Sebroeck 			return put_user(heartbeat, (int *)arg);
349b7e04f8cSWim Van Sebroeck 		case WDIOC_SETOPTIONS:
350b7e04f8cSWim Van Sebroeck 			if (get_user(options, (int *)arg))
351b7e04f8cSWim Van Sebroeck 				return -EFAULT;
352b7e04f8cSWim Van Sebroeck 
353b7e04f8cSWim Van Sebroeck 			if (options & WDIOS_DISABLECARD) {
354b7e04f8cSWim Van Sebroeck 				sh_wdt_stop();
355b7e04f8cSWim Van Sebroeck 				retval = 0;
356b7e04f8cSWim Van Sebroeck 			}
357b7e04f8cSWim Van Sebroeck 
358b7e04f8cSWim Van Sebroeck 			if (options & WDIOS_ENABLECARD) {
359b7e04f8cSWim Van Sebroeck 				sh_wdt_start();
360b7e04f8cSWim Van Sebroeck 				retval = 0;
361b7e04f8cSWim Van Sebroeck 			}
362b7e04f8cSWim Van Sebroeck 
363b7e04f8cSWim Van Sebroeck 			return retval;
364b7e04f8cSWim Van Sebroeck 		default:
365b7e04f8cSWim Van Sebroeck 			return -ENOTTY;
366b7e04f8cSWim Van Sebroeck 	}
367b7e04f8cSWim Van Sebroeck 
368b7e04f8cSWim Van Sebroeck 	return 0;
369b7e04f8cSWim Van Sebroeck }
370b7e04f8cSWim Van Sebroeck 
371b7e04f8cSWim Van Sebroeck /**
372b7e04f8cSWim Van Sebroeck  * 	sh_wdt_notify_sys - Notifier Handler
373b7e04f8cSWim Van Sebroeck  * 	@this: notifier block
374b7e04f8cSWim Van Sebroeck  * 	@code: notifier event
375b7e04f8cSWim Van Sebroeck  * 	@unused: unused
376b7e04f8cSWim Van Sebroeck  *
377b7e04f8cSWim Van Sebroeck  * 	Handles specific events, such as turning off the watchdog during a
378b7e04f8cSWim Van Sebroeck  * 	shutdown event.
379b7e04f8cSWim Van Sebroeck  */
380b7e04f8cSWim Van Sebroeck static int sh_wdt_notify_sys(struct notifier_block *this,
381b7e04f8cSWim Van Sebroeck 			     unsigned long code, void *unused)
382b7e04f8cSWim Van Sebroeck {
383b7e04f8cSWim Van Sebroeck 	if (code == SYS_DOWN || code == SYS_HALT)
384b7e04f8cSWim Van Sebroeck 		sh_wdt_stop();
385b7e04f8cSWim Van Sebroeck 
386b7e04f8cSWim Van Sebroeck 	return NOTIFY_DONE;
387b7e04f8cSWim Van Sebroeck }
388b7e04f8cSWim Van Sebroeck 
389b7e04f8cSWim Van Sebroeck static const struct file_operations sh_wdt_fops = {
390b7e04f8cSWim Van Sebroeck 	.owner		= THIS_MODULE,
391b7e04f8cSWim Van Sebroeck 	.llseek		= no_llseek,
392b7e04f8cSWim Van Sebroeck 	.write		= sh_wdt_write,
393b7e04f8cSWim Van Sebroeck 	.ioctl		= sh_wdt_ioctl,
394b7e04f8cSWim Van Sebroeck 	.open		= sh_wdt_open,
395b7e04f8cSWim Van Sebroeck 	.release	= sh_wdt_close,
396b7e04f8cSWim Van Sebroeck 	.mmap		= sh_wdt_mmap,
397b7e04f8cSWim Van Sebroeck };
398b7e04f8cSWim Van Sebroeck 
399b7e04f8cSWim Van Sebroeck static struct watchdog_info sh_wdt_info = {
400b7e04f8cSWim Van Sebroeck 	.options		= WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
401b7e04f8cSWim Van Sebroeck 				  WDIOF_MAGICCLOSE,
402b7e04f8cSWim Van Sebroeck 	.firmware_version	= 1,
403b7e04f8cSWim Van Sebroeck 	.identity		= "SH WDT",
404b7e04f8cSWim Van Sebroeck };
405b7e04f8cSWim Van Sebroeck 
406b7e04f8cSWim Van Sebroeck static struct notifier_block sh_wdt_notifier = {
407b7e04f8cSWim Van Sebroeck 	.notifier_call		= sh_wdt_notify_sys,
408b7e04f8cSWim Van Sebroeck };
409b7e04f8cSWim Van Sebroeck 
410b7e04f8cSWim Van Sebroeck static struct miscdevice sh_wdt_miscdev = {
411b7e04f8cSWim Van Sebroeck 	.minor		= WATCHDOG_MINOR,
412b7e04f8cSWim Van Sebroeck 	.name		= "watchdog",
413b7e04f8cSWim Van Sebroeck 	.fops		= &sh_wdt_fops,
414b7e04f8cSWim Van Sebroeck };
415b7e04f8cSWim Van Sebroeck 
416b7e04f8cSWim Van Sebroeck /**
417b7e04f8cSWim Van Sebroeck  * 	sh_wdt_init - Initialize module
418b7e04f8cSWim Van Sebroeck  * 	Registers the device and notifier handler. Actual device
419b7e04f8cSWim Van Sebroeck  * 	initialization is handled by sh_wdt_open().
420b7e04f8cSWim Van Sebroeck  */
421b7e04f8cSWim Van Sebroeck static int __init sh_wdt_init(void)
422b7e04f8cSWim Van Sebroeck {
423b7e04f8cSWim Van Sebroeck 	int rc;
424b7e04f8cSWim Van Sebroeck 
425b7e04f8cSWim Van Sebroeck 	if ((clock_division_ratio < 0x5) || (clock_division_ratio > 0x7)) {
426b7e04f8cSWim Van Sebroeck 		clock_division_ratio = WTCSR_CKS_4096;
427b7e04f8cSWim Van Sebroeck 		printk(KERN_INFO PFX "clock_division_ratio value must "
428b7e04f8cSWim Van Sebroeck 		       "be 0x5<=x<=0x7, using %d\n", clock_division_ratio);
429b7e04f8cSWim Van Sebroeck 	}
430b7e04f8cSWim Van Sebroeck 
431b7e04f8cSWim Van Sebroeck 	rc = sh_wdt_set_heartbeat(heartbeat);
432b7e04f8cSWim Van Sebroeck 	if (unlikely(rc)) {
433b7e04f8cSWim Van Sebroeck 		heartbeat = WATCHDOG_HEARTBEAT;
434b7e04f8cSWim Van Sebroeck 		printk(KERN_INFO PFX "heartbeat value must "
435b7e04f8cSWim Van Sebroeck 		       "be 1<=x<=3600, using %d\n", heartbeat);
436b7e04f8cSWim Van Sebroeck 	}
437b7e04f8cSWim Van Sebroeck 
438b7e04f8cSWim Van Sebroeck 	rc = register_reboot_notifier(&sh_wdt_notifier);
439b7e04f8cSWim Van Sebroeck 	if (unlikely(rc)) {
440b7e04f8cSWim Van Sebroeck 		printk(KERN_ERR PFX "Can't register reboot notifier (err=%d)\n",
441b7e04f8cSWim Van Sebroeck 		       rc);
442b7e04f8cSWim Van Sebroeck 		return rc;
443b7e04f8cSWim Van Sebroeck 	}
444b7e04f8cSWim Van Sebroeck 
445b7e04f8cSWim Van Sebroeck 	rc = misc_register(&sh_wdt_miscdev);
446b7e04f8cSWim Van Sebroeck 	if (unlikely(rc)) {
447b7e04f8cSWim Van Sebroeck 		printk(KERN_ERR PFX "Can't register miscdev on "
448b7e04f8cSWim Van Sebroeck 		       "minor=%d (err=%d)\n", sh_wdt_miscdev.minor, rc);
449b7e04f8cSWim Van Sebroeck 		unregister_reboot_notifier(&sh_wdt_notifier);
450b7e04f8cSWim Van Sebroeck 		return rc;
451b7e04f8cSWim Van Sebroeck 	}
452b7e04f8cSWim Van Sebroeck 
453b7e04f8cSWim Van Sebroeck 	printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
454b7e04f8cSWim Van Sebroeck 		heartbeat, nowayout);
455b7e04f8cSWim Van Sebroeck 
456b7e04f8cSWim Van Sebroeck 	return 0;
457b7e04f8cSWim Van Sebroeck }
458b7e04f8cSWim Van Sebroeck 
459b7e04f8cSWim Van Sebroeck /**
460b7e04f8cSWim Van Sebroeck  * 	sh_wdt_exit - Deinitialize module
461b7e04f8cSWim Van Sebroeck  * 	Unregisters the device and notifier handler. Actual device
462b7e04f8cSWim Van Sebroeck  * 	deinitialization is handled by sh_wdt_close().
463b7e04f8cSWim Van Sebroeck  */
464b7e04f8cSWim Van Sebroeck static void __exit sh_wdt_exit(void)
465b7e04f8cSWim Van Sebroeck {
466b7e04f8cSWim Van Sebroeck 	misc_deregister(&sh_wdt_miscdev);
467b7e04f8cSWim Van Sebroeck 	unregister_reboot_notifier(&sh_wdt_notifier);
468b7e04f8cSWim Van Sebroeck }
469b7e04f8cSWim Van Sebroeck 
470b7e04f8cSWim Van Sebroeck MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
471b7e04f8cSWim Van Sebroeck MODULE_DESCRIPTION("SuperH watchdog driver");
472b7e04f8cSWim Van Sebroeck MODULE_LICENSE("GPL");
473b7e04f8cSWim Van Sebroeck MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
474b7e04f8cSWim Van Sebroeck 
475b7e04f8cSWim Van Sebroeck module_param(clock_division_ratio, int, 0);
476b7e04f8cSWim Van Sebroeck MODULE_PARM_DESC(clock_division_ratio, "Clock division ratio. Valid ranges are from 0x5 (1.31ms) to 0x7 (5.25ms). (default=" __MODULE_STRING(clock_division_ratio) ")");
477b7e04f8cSWim Van Sebroeck 
478b7e04f8cSWim Van Sebroeck module_param(heartbeat, int, 0);
479b7e04f8cSWim Van Sebroeck MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1<=heartbeat<=3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
480b7e04f8cSWim Van Sebroeck 
481b7e04f8cSWim Van Sebroeck module_param(nowayout, int, 0);
482b7e04f8cSWim Van Sebroeck MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
483b7e04f8cSWim Van Sebroeck 
484b7e04f8cSWim Van Sebroeck module_init(sh_wdt_init);
485b7e04f8cSWim Van Sebroeck module_exit(sh_wdt_exit);
486