diff options
author | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@ppc970.osdl.org> | 2005-04-16 18:20:36 -0400 |
commit | 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch) | |
tree | 0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/char/watchdog/shwdt.c |
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history,
even though we have it. We can create a separate "historical" git
archive of that later if we want to, and in the meantime it's about
3.2GB when imported into git - space that would just make the early
git days unnecessarily complicated, when we don't have a lot of good
infrastructure for it.
Let it rip!
Diffstat (limited to 'drivers/char/watchdog/shwdt.c')
-rw-r--r-- | drivers/char/watchdog/shwdt.c | 452 |
1 files changed, 452 insertions, 0 deletions
diff --git a/drivers/char/watchdog/shwdt.c b/drivers/char/watchdog/shwdt.c new file mode 100644 index 000000000000..3bc9272a474c --- /dev/null +++ b/drivers/char/watchdog/shwdt.c | |||
@@ -0,0 +1,452 @@ | |||
1 | /* | ||
2 | * drivers/char/watchdog/shwdt.c | ||
3 | * | ||
4 | * Watchdog driver for integrated watchdog in the SuperH processors. | ||
5 | * | ||
6 | * Copyright (C) 2001, 2002, 2003 Paul Mundt <lethal@linux-sh.org> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify it | ||
9 | * under the terms of the GNU General Public License as published by the | ||
10 | * Free Software Foundation; either version 2 of the License, or (at your | ||
11 | * option) any later version. | ||
12 | * | ||
13 | * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> | ||
14 | * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT | ||
15 | * | ||
16 | * 19-Apr-2002 Rob Radez <rob@osinvestor.com> | ||
17 | * Added expect close support, made emulated timeout runtime changeable | ||
18 | * general cleanups, add some ioctls | ||
19 | */ | ||
20 | #include <linux/config.h> | ||
21 | #include <linux/module.h> | ||
22 | #include <linux/moduleparam.h> | ||
23 | #include <linux/init.h> | ||
24 | #include <linux/types.h> | ||
25 | #include <linux/miscdevice.h> | ||
26 | #include <linux/watchdog.h> | ||
27 | #include <linux/reboot.h> | ||
28 | #include <linux/notifier.h> | ||
29 | #include <linux/ioport.h> | ||
30 | #include <linux/fs.h> | ||
31 | |||
32 | #include <asm/io.h> | ||
33 | #include <asm/uaccess.h> | ||
34 | #include <asm/watchdog.h> | ||
35 | |||
36 | #define PFX "shwdt: " | ||
37 | |||
38 | /* | ||
39 | * Default clock division ratio is 5.25 msecs. For an additional table of | ||
40 | * values, consult the asm-sh/watchdog.h. Overload this at module load | ||
41 | * time. | ||
42 | * | ||
43 | * In order for this to work reliably we need to have HZ set to 1000 or | ||
44 | * something quite higher than 100 (or we need a proper high-res timer | ||
45 | * implementation that will deal with this properly), otherwise the 10ms | ||
46 | * resolution of a jiffy is enough to trigger the overflow. For things like | ||
47 | * the SH-4 and SH-5, this isn't necessarily that big of a problem, though | ||
48 | * for the SH-2 and SH-3, this isn't recommended unless the WDT is absolutely | ||
49 | * necssary. | ||
50 | * | ||
51 | * As a result of this timing problem, the only modes that are particularly | ||
52 | * feasible are the 4096 and the 2048 divisors, which yeild 5.25 and 2.62ms | ||
53 | * overflow periods respectively. | ||
54 | * | ||
55 | * Also, since we can't really expect userspace to be responsive enough | ||
56 | * before the overflow happens, we maintain two seperate timers .. One in | ||
57 | * the kernel for clearing out WOVF every 2ms or so (again, this depends on | ||
58 | * HZ == 1000), and another for monitoring userspace writes to the WDT device. | ||
59 | * | ||
60 | * As such, we currently use a configurable heartbeat interval which defaults | ||
61 | * to 30s. In this case, the userspace daemon is only responsible for periodic | ||
62 | * writes to the device before the next heartbeat is scheduled. If the daemon | ||
63 | * misses its deadline, the kernel timer will allow the WDT to overflow. | ||
64 | */ | ||
65 | static int clock_division_ratio = WTCSR_CKS_4096; | ||
66 | |||
67 | #define next_ping_period(cks) msecs_to_jiffies(cks - 4) | ||
68 | |||
69 | static unsigned long shwdt_is_open; | ||
70 | static struct watchdog_info sh_wdt_info; | ||
71 | static char shwdt_expect_close; | ||
72 | static struct timer_list timer; | ||
73 | static unsigned long next_heartbeat; | ||
74 | |||
75 | #define WATCHDOG_HEARTBEAT 30 /* 30 sec default heartbeat */ | ||
76 | static int heartbeat = WATCHDOG_HEARTBEAT; /* in seconds */ | ||
77 | |||
78 | #ifdef CONFIG_WATCHDOG_NOWAYOUT | ||
79 | static int nowayout = 1; | ||
80 | #else | ||
81 | static int nowayout = 0; | ||
82 | #endif | ||
83 | |||
84 | /** | ||
85 | * sh_wdt_start - Start the Watchdog | ||
86 | * | ||
87 | * Starts the watchdog. | ||
88 | */ | ||
89 | static void sh_wdt_start(void) | ||
90 | { | ||
91 | __u8 csr; | ||
92 | |||
93 | next_heartbeat = jiffies + (heartbeat * HZ); | ||
94 | mod_timer(&timer, next_ping_period(clock_division_ratio)); | ||
95 | |||
96 | csr = sh_wdt_read_csr(); | ||
97 | csr |= WTCSR_WT | clock_division_ratio; | ||
98 | sh_wdt_write_csr(csr); | ||
99 | |||
100 | sh_wdt_write_cnt(0); | ||
101 | |||
102 | /* | ||
103 | * These processors have a bit of an inconsistent initialization | ||
104 | * process.. starting with SH-3, RSTS was moved to WTCSR, and the | ||
105 | * RSTCSR register was removed. | ||
106 | * | ||
107 | * On the SH-2 however, in addition with bits being in different | ||
108 | * locations, we must deal with RSTCSR outright.. | ||
109 | */ | ||
110 | csr = sh_wdt_read_csr(); | ||
111 | csr |= WTCSR_TME; | ||
112 | csr &= ~WTCSR_RSTS; | ||
113 | sh_wdt_write_csr(csr); | ||
114 | |||
115 | #ifdef CONFIG_CPU_SH2 | ||
116 | /* | ||
117 | * Whoever came up with the RSTCSR semantics must've been smoking | ||
118 | * some of the good stuff, since in addition to the WTCSR/WTCNT write | ||
119 | * brain-damage, it's managed to fuck things up one step further.. | ||
120 | * | ||
121 | * If we need to clear the WOVF bit, the upper byte has to be 0xa5.. | ||
122 | * but if we want to touch RSTE or RSTS, the upper byte has to be | ||
123 | * 0x5a.. | ||
124 | */ | ||
125 | csr = sh_wdt_read_rstcsr(); | ||
126 | csr &= ~RSTCSR_RSTS; | ||
127 | sh_wdt_write_rstcsr(csr); | ||
128 | #endif | ||
129 | } | ||
130 | |||
131 | /** | ||
132 | * sh_wdt_stop - Stop the Watchdog | ||
133 | * | ||
134 | * Stops the watchdog. | ||
135 | */ | ||
136 | static void sh_wdt_stop(void) | ||
137 | { | ||
138 | __u8 csr; | ||
139 | |||
140 | del_timer(&timer); | ||
141 | |||
142 | csr = sh_wdt_read_csr(); | ||
143 | csr &= ~WTCSR_TME; | ||
144 | sh_wdt_write_csr(csr); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * sh_wdt_keepalive - Keep the Userspace Watchdog Alive | ||
149 | * | ||
150 | * The Userspace watchdog got a KeepAlive: schedule the next heartbeat. | ||
151 | */ | ||
152 | static void sh_wdt_keepalive(void) | ||
153 | { | ||
154 | next_heartbeat = jiffies + (heartbeat * HZ); | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * sh_wdt_set_heartbeat - Set the Userspace Watchdog heartbeat | ||
159 | * | ||
160 | * Set the Userspace Watchdog heartbeat | ||
161 | */ | ||
162 | static int sh_wdt_set_heartbeat(int t) | ||
163 | { | ||
164 | if ((t < 1) || (t > 3600)) /* arbitrary upper limit */ | ||
165 | return -EINVAL; | ||
166 | |||
167 | heartbeat = t; | ||
168 | return 0; | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * sh_wdt_ping - Ping the Watchdog | ||
173 | * | ||
174 | * @data: Unused | ||
175 | * | ||
176 | * Clears overflow bit, resets timer counter. | ||
177 | */ | ||
178 | static void sh_wdt_ping(unsigned long data) | ||
179 | { | ||
180 | if (time_before(jiffies, next_heartbeat)) { | ||
181 | __u8 csr; | ||
182 | |||
183 | csr = sh_wdt_read_csr(); | ||
184 | csr &= ~WTCSR_IOVF; | ||
185 | sh_wdt_write_csr(csr); | ||
186 | |||
187 | sh_wdt_write_cnt(0); | ||
188 | |||
189 | mod_timer(&timer, next_ping_period(clock_division_ratio)); | ||
190 | } else { | ||
191 | printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n"); | ||
192 | } | ||
193 | } | ||
194 | |||
195 | /** | ||
196 | * sh_wdt_open - Open the Device | ||
197 | * | ||
198 | * @inode: inode of device | ||
199 | * @file: file handle of device | ||
200 | * | ||
201 | * Watchdog device is opened and started. | ||
202 | */ | ||
203 | static int sh_wdt_open(struct inode *inode, struct file *file) | ||
204 | { | ||
205 | if (test_and_set_bit(0, &shwdt_is_open)) | ||
206 | return -EBUSY; | ||
207 | if (nowayout) | ||
208 | __module_get(THIS_MODULE); | ||
209 | |||
210 | sh_wdt_start(); | ||
211 | |||
212 | return nonseekable_open(inode, file); | ||
213 | } | ||
214 | |||
215 | /** | ||
216 | * sh_wdt_close - Close the Device | ||
217 | * | ||
218 | * @inode: inode of device | ||
219 | * @file: file handle of device | ||
220 | * | ||
221 | * Watchdog device is closed and stopped. | ||
222 | */ | ||
223 | static int sh_wdt_close(struct inode *inode, struct file *file) | ||
224 | { | ||
225 | if (shwdt_expect_close == 42) { | ||
226 | sh_wdt_stop(); | ||
227 | } else { | ||
228 | printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n"); | ||
229 | sh_wdt_keepalive(); | ||
230 | } | ||
231 | |||
232 | clear_bit(0, &shwdt_is_open); | ||
233 | shwdt_expect_close = 0; | ||
234 | |||
235 | return 0; | ||
236 | } | ||
237 | |||
238 | /** | ||
239 | * sh_wdt_write - Write to Device | ||
240 | * | ||
241 | * @file: file handle of device | ||
242 | * @buf: buffer to write | ||
243 | * @count: length of buffer | ||
244 | * @ppos: offset | ||
245 | * | ||
246 | * Pings the watchdog on write. | ||
247 | */ | ||
248 | static ssize_t sh_wdt_write(struct file *file, const char *buf, | ||
249 | size_t count, loff_t *ppos) | ||
250 | { | ||
251 | if (count) { | ||
252 | if (!nowayout) { | ||
253 | size_t i; | ||
254 | |||
255 | shwdt_expect_close = 0; | ||
256 | |||
257 | for (i = 0; i != count; i++) { | ||
258 | char c; | ||
259 | if (get_user(c, buf + i)) | ||
260 | return -EFAULT; | ||
261 | if (c == 'V') | ||
262 | shwdt_expect_close = 42; | ||
263 | } | ||
264 | } | ||
265 | sh_wdt_keepalive(); | ||
266 | } | ||
267 | |||
268 | return count; | ||
269 | } | ||
270 | |||
271 | /** | ||
272 | * sh_wdt_ioctl - Query Device | ||
273 | * | ||
274 | * @inode: inode of device | ||
275 | * @file: file handle of device | ||
276 | * @cmd: watchdog command | ||
277 | * @arg: argument | ||
278 | * | ||
279 | * Query basic information from the device or ping it, as outlined by the | ||
280 | * watchdog API. | ||
281 | */ | ||
282 | static int sh_wdt_ioctl(struct inode *inode, struct file *file, | ||
283 | unsigned int cmd, unsigned long arg) | ||
284 | { | ||
285 | int new_heartbeat; | ||
286 | int options, retval = -EINVAL; | ||
287 | |||
288 | switch (cmd) { | ||
289 | case WDIOC_GETSUPPORT: | ||
290 | return copy_to_user((struct watchdog_info *)arg, | ||
291 | &sh_wdt_info, | ||
292 | sizeof(sh_wdt_info)) ? -EFAULT : 0; | ||
293 | case WDIOC_GETSTATUS: | ||
294 | case WDIOC_GETBOOTSTATUS: | ||
295 | return put_user(0, (int *)arg); | ||
296 | case WDIOC_KEEPALIVE: | ||
297 | sh_wdt_keepalive(); | ||
298 | return 0; | ||
299 | case WDIOC_SETTIMEOUT: | ||
300 | if (get_user(new_heartbeat, (int *)arg)) | ||
301 | return -EFAULT; | ||
302 | |||
303 | if (sh_wdt_set_heartbeat(new_heartbeat)) | ||
304 | return -EINVAL; | ||
305 | |||
306 | sh_wdt_keepalive(); | ||
307 | /* Fall */ | ||
308 | case WDIOC_GETTIMEOUT: | ||
309 | return put_user(heartbeat, (int *)arg); | ||
310 | case WDIOC_SETOPTIONS: | ||
311 | if (get_user(options, (int *)arg)) | ||
312 | return -EFAULT; | ||
313 | |||
314 | if (options & WDIOS_DISABLECARD) { | ||
315 | sh_wdt_stop(); | ||
316 | retval = 0; | ||
317 | } | ||
318 | |||
319 | if (options & WDIOS_ENABLECARD) { | ||
320 | sh_wdt_start(); | ||
321 | retval = 0; | ||
322 | } | ||
323 | |||
324 | return retval; | ||
325 | default: | ||
326 | return -ENOIOCTLCMD; | ||
327 | } | ||
328 | |||
329 | return 0; | ||
330 | } | ||
331 | |||
332 | /** | ||
333 | * sh_wdt_notify_sys - Notifier Handler | ||
334 | * | ||
335 | * @this: notifier block | ||
336 | * @code: notifier event | ||
337 | * @unused: unused | ||
338 | * | ||
339 | * Handles specific events, such as turning off the watchdog during a | ||
340 | * shutdown event. | ||
341 | */ | ||
342 | static int sh_wdt_notify_sys(struct notifier_block *this, | ||
343 | unsigned long code, void *unused) | ||
344 | { | ||
345 | if (code == SYS_DOWN || code == SYS_HALT) { | ||
346 | sh_wdt_stop(); | ||
347 | } | ||
348 | |||
349 | return NOTIFY_DONE; | ||
350 | } | ||
351 | |||
352 | static struct file_operations sh_wdt_fops = { | ||
353 | .owner = THIS_MODULE, | ||
354 | .llseek = no_llseek, | ||
355 | .write = sh_wdt_write, | ||
356 | .ioctl = sh_wdt_ioctl, | ||
357 | .open = sh_wdt_open, | ||
358 | .release = sh_wdt_close, | ||
359 | }; | ||
360 | |||
361 | static struct watchdog_info sh_wdt_info = { | ||
362 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, | ||
363 | .firmware_version = 1, | ||
364 | .identity = "SH WDT", | ||
365 | }; | ||
366 | |||
367 | static struct notifier_block sh_wdt_notifier = { | ||
368 | .notifier_call = sh_wdt_notify_sys, | ||
369 | }; | ||
370 | |||
371 | static struct miscdevice sh_wdt_miscdev = { | ||
372 | .minor = WATCHDOG_MINOR, | ||
373 | .name = "watchdog", | ||
374 | .fops = &sh_wdt_fops, | ||
375 | }; | ||
376 | |||
377 | /** | ||
378 | * sh_wdt_init - Initialize module | ||
379 | * | ||
380 | * Registers the device and notifier handler. Actual device | ||
381 | * initialization is handled by sh_wdt_open(). | ||
382 | */ | ||
383 | static int __init sh_wdt_init(void) | ||
384 | { | ||
385 | int rc; | ||
386 | |||
387 | if ((clock_division_ratio < 0x5) || (clock_division_ratio > 0x7)) { | ||
388 | clock_division_ratio = WTCSR_CKS_4096; | ||
389 | printk(KERN_INFO PFX "clock_division_ratio value must be 0x5<=x<=0x7, using %d\n", | ||
390 | clock_division_ratio); | ||
391 | } | ||
392 | |||
393 | if (sh_wdt_set_heartbeat(heartbeat)) | ||
394 | { | ||
395 | heartbeat = WATCHDOG_HEARTBEAT; | ||
396 | printk(KERN_INFO PFX "heartbeat value must be 1<=x<=3600, using %d\n", | ||
397 | heartbeat); | ||
398 | } | ||
399 | |||
400 | init_timer(&timer); | ||
401 | timer.function = sh_wdt_ping; | ||
402 | timer.data = 0; | ||
403 | |||
404 | rc = register_reboot_notifier(&sh_wdt_notifier); | ||
405 | if (rc) { | ||
406 | printk(KERN_ERR PFX "Can't register reboot notifier (err=%d)\n", rc); | ||
407 | return rc; | ||
408 | } | ||
409 | |||
410 | rc = misc_register(&sh_wdt_miscdev); | ||
411 | if (rc) { | ||
412 | printk(KERN_ERR PFX "Can't register miscdev on minor=%d (err=%d)\n", | ||
413 | sh_wdt_miscdev.minor, rc); | ||
414 | unregister_reboot_notifier(&sh_wdt_notifier); | ||
415 | return rc; | ||
416 | } | ||
417 | |||
418 | printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n", | ||
419 | heartbeat, nowayout); | ||
420 | |||
421 | return 0; | ||
422 | } | ||
423 | |||
424 | /** | ||
425 | * sh_wdt_exit - Deinitialize module | ||
426 | * | ||
427 | * Unregisters the device and notifier handler. Actual device | ||
428 | * deinitialization is handled by sh_wdt_close(). | ||
429 | */ | ||
430 | static void __exit sh_wdt_exit(void) | ||
431 | { | ||
432 | misc_deregister(&sh_wdt_miscdev); | ||
433 | unregister_reboot_notifier(&sh_wdt_notifier); | ||
434 | } | ||
435 | |||
436 | MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>"); | ||
437 | MODULE_DESCRIPTION("SuperH watchdog driver"); | ||
438 | MODULE_LICENSE("GPL"); | ||
439 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | ||
440 | |||
441 | module_param(clock_division_ratio, int, 0); | ||
442 | MODULE_PARM_DESC(clock_division_ratio, "Clock division ratio. Valid ranges are from 0x5 (1.31ms) to 0x7 (5.25ms). Defaults to 0x7."); | ||
443 | |||
444 | module_param(heartbeat, int, 0); | ||
445 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1<=heartbeat<=3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); | ||
446 | |||
447 | module_param(nowayout, int, 0); | ||
448 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)"); | ||
449 | |||
450 | module_init(sh_wdt_init); | ||
451 | module_exit(sh_wdt_exit); | ||
452 | |||