1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22#include <linux/init.h>
23#include <linux/types.h>
24#include <linux/miscdevice.h>
25#include <linux/watchdog.h>
26#include <linux/reboot.h>
27#include <linux/notifier.h>
28#include <linux/ioport.h>
29#include <linux/fs.h>
30#include <linux/mm.h>
31#include <asm/io.h>
32#include <asm/uaccess.h>
33#include <asm/watchdog.h>
34
35#define PFX "shwdt: "
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64static int clock_division_ratio = WTCSR_CKS_4096;
65
66#define next_ping_period(cks) msecs_to_jiffies(cks - 4)
67
68static void sh_wdt_ping(unsigned long data);
69
70static unsigned long shwdt_is_open;
71static struct watchdog_info sh_wdt_info;
72static char shwdt_expect_close;
73static DEFINE_TIMER(timer, sh_wdt_ping, 0, 0);
74static unsigned long next_heartbeat;
75
76#define WATCHDOG_HEARTBEAT 30
77static int heartbeat = WATCHDOG_HEARTBEAT;
78
79static int nowayout = WATCHDOG_NOWAYOUT;
80
81
82
83
84
85
86static void sh_wdt_start(void)
87{
88 __u8 csr;
89
90 next_heartbeat = jiffies + (heartbeat * HZ);
91 mod_timer(&timer, next_ping_period(clock_division_ratio));
92
93 csr = sh_wdt_read_csr();
94 csr |= WTCSR_WT | clock_division_ratio;
95 sh_wdt_write_csr(csr);
96
97 sh_wdt_write_cnt(0);
98
99
100
101
102
103
104
105
106
107 csr = sh_wdt_read_csr();
108 csr |= WTCSR_TME;
109 csr &= ~WTCSR_RSTS;
110 sh_wdt_write_csr(csr);
111
112#ifdef CONFIG_CPU_SH2
113
114
115
116
117
118
119
120
121
122 csr = sh_wdt_read_rstcsr();
123 csr &= ~RSTCSR_RSTS;
124 sh_wdt_write_rstcsr(csr);
125#endif
126}
127
128
129
130
131
132static void sh_wdt_stop(void)
133{
134 __u8 csr;
135
136 del_timer(&timer);
137
138 csr = sh_wdt_read_csr();
139 csr &= ~WTCSR_TME;
140 sh_wdt_write_csr(csr);
141}
142
143
144
145
146
147static inline void sh_wdt_keepalive(void)
148{
149 next_heartbeat = jiffies + (heartbeat * HZ);
150}
151
152
153
154
155
156static int sh_wdt_set_heartbeat(int t)
157{
158 if (unlikely((t < 1) || (t > 3600)))
159 return -EINVAL;
160
161 heartbeat = t;
162 return 0;
163}
164
165
166
167
168
169
170
171static void sh_wdt_ping(unsigned long data)
172{
173 if (time_before(jiffies, next_heartbeat)) {
174 __u8 csr;
175
176 csr = sh_wdt_read_csr();
177 csr &= ~WTCSR_IOVF;
178 sh_wdt_write_csr(csr);
179
180 sh_wdt_write_cnt(0);
181
182 mod_timer(&timer, next_ping_period(clock_division_ratio));
183 } else
184 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping "
185 "the watchdog\n");
186}
187
188
189
190
191
192
193
194
195static int sh_wdt_open(struct inode *inode, struct file *file)
196{
197 if (test_and_set_bit(0, &shwdt_is_open))
198 return -EBUSY;
199 if (nowayout)
200 __module_get(THIS_MODULE);
201
202 sh_wdt_start();
203
204 return nonseekable_open(inode, file);
205}
206
207
208
209
210
211
212
213
214static int sh_wdt_close(struct inode *inode, struct file *file)
215{
216 if (shwdt_expect_close == 42) {
217 sh_wdt_stop();
218 } else {
219 printk(KERN_CRIT PFX "Unexpected close, not "
220 "stopping watchdog!\n");
221 sh_wdt_keepalive();
222 }
223
224 clear_bit(0, &shwdt_is_open);
225 shwdt_expect_close = 0;
226
227 return 0;
228}
229
230
231
232
233
234
235
236
237
238
239static ssize_t sh_wdt_write(struct file *file, const char *buf,
240 size_t count, loff_t *ppos)
241{
242 if (count) {
243 if (!nowayout) {
244 size_t i;
245
246 shwdt_expect_close = 0;
247
248 for (i = 0; i != count; i++) {
249 char c;
250 if (get_user(c, buf + i))
251 return -EFAULT;
252 if (c == 'V')
253 shwdt_expect_close = 42;
254 }
255 }
256 sh_wdt_keepalive();
257 }
258
259 return count;
260}
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278static int sh_wdt_mmap(struct file *file, struct vm_area_struct *vma)
279{
280 int ret = -ENOSYS;
281
282#ifdef CONFIG_SH_WDT_MMAP
283 unsigned long addr;
284
285
286 if (((vma->vm_end - vma->vm_start) != PAGE_SIZE) || vma->vm_pgoff)
287 return -EINVAL;
288
289
290
291
292
293 addr = WTCNT & ~(PAGE_SIZE - 1);
294
295 vma->vm_flags |= VM_IO;
296 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
297
298 if (io_remap_pfn_range(vma, vma->vm_start, addr >> PAGE_SHIFT,
299 PAGE_SIZE, vma->vm_page_prot)) {
300 printk(KERN_ERR PFX "%s: io_remap_pfn_range failed\n",
301 __FUNCTION__);
302 return -EAGAIN;
303 }
304
305 ret = 0;
306#endif
307
308 return ret;
309}
310
311
312
313
314
315
316
317
318
319
320
321static int sh_wdt_ioctl(struct inode *inode, struct file *file,
322 unsigned int cmd, unsigned long arg)
323{
324 int new_heartbeat;
325 int options, retval = -EINVAL;
326
327 switch (cmd) {
328 case WDIOC_GETSUPPORT:
329 return copy_to_user((struct watchdog_info *)arg,
330 &sh_wdt_info,
331 sizeof(sh_wdt_info)) ? -EFAULT : 0;
332 case WDIOC_GETSTATUS:
333 case WDIOC_GETBOOTSTATUS:
334 return put_user(0, (int *)arg);
335 case WDIOC_KEEPALIVE:
336 sh_wdt_keepalive();
337 return 0;
338 case WDIOC_SETTIMEOUT:
339 if (get_user(new_heartbeat, (int *)arg))
340 return -EFAULT;
341
342 if (sh_wdt_set_heartbeat(new_heartbeat))
343 return -EINVAL;
344
345 sh_wdt_keepalive();
346
347 case WDIOC_GETTIMEOUT:
348 return put_user(heartbeat, (int *)arg);
349 case WDIOC_SETOPTIONS:
350 if (get_user(options, (int *)arg))
351 return -EFAULT;
352
353 if (options & WDIOS_DISABLECARD) {
354 sh_wdt_stop();
355 retval = 0;
356 }
357
358 if (options & WDIOS_ENABLECARD) {
359 sh_wdt_start();
360 retval = 0;
361 }
362
363 return retval;
364 default:
365 return -ENOTTY;
366 }
367
368 return 0;
369}
370
371
372
373
374
375
376
377
378
379
380static int sh_wdt_notify_sys(struct notifier_block *this,
381 unsigned long code, void *unused)
382{
383 if (code == SYS_DOWN || code == SYS_HALT)
384 sh_wdt_stop();
385
386 return NOTIFY_DONE;
387}
388
389static const struct file_operations sh_wdt_fops = {
390 .owner = THIS_MODULE,
391 .llseek = no_llseek,
392 .write = sh_wdt_write,
393 .ioctl = sh_wdt_ioctl,
394 .open = sh_wdt_open,
395 .release = sh_wdt_close,
396 .mmap = sh_wdt_mmap,
397};
398
399static struct watchdog_info sh_wdt_info = {
400 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
401 WDIOF_MAGICCLOSE,
402 .firmware_version = 1,
403 .identity = "SH WDT",
404};
405
406static struct notifier_block sh_wdt_notifier = {
407 .notifier_call = sh_wdt_notify_sys,
408};
409
410static struct miscdevice sh_wdt_miscdev = {
411 .minor = WATCHDOG_MINOR,
412 .name = "watchdog",
413 .fops = &sh_wdt_fops,
414};
415
416
417
418
419
420
421static int __init sh_wdt_init(void)
422{
423 int rc;
424
425 if ((clock_division_ratio < 0x5) || (clock_division_ratio > 0x7)) {
426 clock_division_ratio = WTCSR_CKS_4096;
427 printk(KERN_INFO PFX "clock_division_ratio value must "
428 "be 0x5<=x<=0x7, using %d\n", clock_division_ratio);
429 }
430
431 rc = sh_wdt_set_heartbeat(heartbeat);
432 if (unlikely(rc)) {
433 heartbeat = WATCHDOG_HEARTBEAT;
434 printk(KERN_INFO PFX "heartbeat value must "
435 "be 1<=x<=3600, using %d\n", heartbeat);
436 }
437
438 rc = register_reboot_notifier(&sh_wdt_notifier);
439 if (unlikely(rc)) {
440 printk(KERN_ERR PFX "Can't register reboot notifier (err=%d)\n",
441 rc);
442 return rc;
443 }
444
445 rc = misc_register(&sh_wdt_miscdev);
446 if (unlikely(rc)) {
447 printk(KERN_ERR PFX "Can't register miscdev on "
448 "minor=%d (err=%d)\n", sh_wdt_miscdev.minor, rc);
449 unregister_reboot_notifier(&sh_wdt_notifier);
450 return rc;
451 }
452
453 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
454 heartbeat, nowayout);
455
456 return 0;
457}
458
459
460
461
462
463
464static void __exit sh_wdt_exit(void)
465{
466 misc_deregister(&sh_wdt_miscdev);
467 unregister_reboot_notifier(&sh_wdt_notifier);
468}
469
470MODULE_AUTHOR("Paul Mundt <lethal@linux-sh.org>");
471MODULE_DESCRIPTION("SuperH watchdog driver");
472MODULE_LICENSE("GPL");
473MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
474
475module_param(clock_division_ratio, int, 0);
476MODULE_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) ")");
477
478module_param(heartbeat, int, 0);
479MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (1<=heartbeat<=3600, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
480
481module_param(nowayout, int, 0);
482MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
483
484module_init(sh_wdt_init);
485module_exit(sh_wdt_exit);
486