1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#include <linux/module.h>
47#include <linux/moduleparam.h>
48#include <linux/types.h>
49#include <linux/miscdevice.h>
50#include <linux/watchdog.h>
51#include <linux/delay.h>
52#include <linux/fs.h>
53#include <linux/ioport.h>
54#include <linux/notifier.h>
55#include <linux/reboot.h>
56#include <linux/init.h>
57#include <linux/spinlock.h>
58#include <linux/io.h>
59#include <linux/uaccess.h>
60
61#include <asm/system.h>
62
63
64
65#define SMSC_SUPPORT_MINUTES
66#undef SMSC_SUPPORT_MINUTES
67
68#define MAX_TIMEOUT 255
69
70#define UNIT_SECOND 0
71#define UNIT_MINUTE 1
72
73#define MODNAME "smsc37b787_wdt: "
74#define VERSION "1.1"
75
76#define IOPORT 0x3F0
77#define IOPORT_SIZE 2
78#define IODEV_NO 8
79
80static int unit = UNIT_SECOND;
81static int timeout = 60;
82static unsigned long timer_enabled;
83
84static char expect_close;
85
86static DEFINE_SPINLOCK(io_lock);
87
88static int nowayout = WATCHDOG_NOWAYOUT;
89
90
91
92
93
94static inline void open_io_config(void)
95{
96 outb(0x55, IOPORT);
97 mdelay(1);
98 outb(0x55, IOPORT);
99}
100
101
102static inline void close_io_config(void)
103{
104 outb(0xAA, IOPORT);
105}
106
107
108static inline void select_io_device(unsigned char devno)
109{
110 outb(0x07, IOPORT);
111 outb(devno, IOPORT+1);
112}
113
114
115static inline void write_io_cr(unsigned char reg, unsigned char data)
116{
117 outb(reg, IOPORT);
118 outb(data, IOPORT+1);
119}
120
121
122static inline char read_io_cr(unsigned char reg)
123{
124 outb(reg, IOPORT);
125 return inb(IOPORT+1);
126}
127
128
129
130static inline void gpio_bit12(unsigned char reg)
131{
132
133
134
135
136
137
138
139
140
141 write_io_cr(0xE2, reg);
142}
143
144static inline void gpio_bit13(unsigned char reg)
145{
146
147
148
149
150
151
152
153
154 write_io_cr(0xE3, reg);
155}
156
157static inline void wdt_timer_units(unsigned char new_units)
158{
159
160
161
162
163
164 write_io_cr(0xF1, new_units);
165}
166
167static inline void wdt_timeout_value(unsigned char new_timeout)
168{
169
170
171
172 write_io_cr(0xF2, new_timeout);
173}
174
175static inline void wdt_timer_conf(unsigned char conf)
176{
177
178
179
180
181
182
183
184
185
186
187
188 write_io_cr(0xF3, conf);
189}
190
191static inline void wdt_timer_ctrl(unsigned char reg)
192{
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 write_io_cr(0xF4, reg);
211}
212
213
214
215
216
217static void wb_smsc_wdt_initialize(void)
218{
219 unsigned char old;
220
221 spin_lock(&io_lock);
222 open_io_config();
223 select_io_device(IODEV_NO);
224
225
226 gpio_bit13(0x08);
227 gpio_bit12(0x0A);
228
229
230 wdt_timeout_value(0);
231
232
233 wdt_timer_ctrl(0x00);
234
235
236 wdt_timer_conf(0x00);
237
238
239 old = read_io_cr(0xF1) & 0x7F;
240 if (unit == UNIT_SECOND)
241 old |= 0x80;
242
243
244 wdt_timer_units(old);
245
246 close_io_config();
247 spin_unlock(&io_lock);
248}
249
250
251
252static void wb_smsc_wdt_shutdown(void)
253{
254 spin_lock(&io_lock);
255 open_io_config();
256 select_io_device(IODEV_NO);
257
258
259 gpio_bit13(0x09);
260 gpio_bit12(0x09);
261
262
263 wdt_timer_conf(0x00);
264
265
266 wdt_timer_ctrl(0x00);
267
268
269 wdt_timeout_value(0x00);
270
271 close_io_config();
272 spin_unlock(&io_lock);
273}
274
275
276
277static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
278{
279 spin_lock(&io_lock);
280 open_io_config();
281 select_io_device(IODEV_NO);
282
283
284 wdt_timer_ctrl((new_timeout == 0) ? 0x00 : 0x02);
285
286
287 wdt_timeout_value(new_timeout);
288
289 close_io_config();
290 spin_unlock(&io_lock);
291}
292
293
294
295static unsigned char wb_smsc_wdt_get_timeout(void)
296{
297 unsigned char set_timeout;
298
299 spin_lock(&io_lock);
300 open_io_config();
301 select_io_device(IODEV_NO);
302 set_timeout = read_io_cr(0xF2);
303 close_io_config();
304 spin_unlock(&io_lock);
305
306 return set_timeout;
307}
308
309
310
311static void wb_smsc_wdt_disable(void)
312{
313
314 wb_smsc_wdt_set_timeout(0);
315}
316
317
318
319static void wb_smsc_wdt_enable(void)
320{
321
322 wb_smsc_wdt_set_timeout(timeout);
323}
324
325
326
327static void wb_smsc_wdt_reset_timer(void)
328{
329 spin_lock(&io_lock);
330 open_io_config();
331 select_io_device(IODEV_NO);
332
333
334 wdt_timeout_value(timeout);
335 wdt_timer_conf(0x08);
336
337 close_io_config();
338 spin_unlock(&io_lock);
339}
340
341
342
343static int wb_smsc_wdt_status(void)
344{
345 return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING;
346}
347
348
349
350
351
352
353static int wb_smsc_wdt_open(struct inode *inode, struct file *file)
354{
355
356
357 if (test_and_set_bit(0, &timer_enabled))
358 return -EBUSY;
359
360 if (nowayout)
361 __module_get(THIS_MODULE);
362
363
364 wb_smsc_wdt_enable();
365
366 printk(KERN_INFO MODNAME
367 "Watchdog enabled. Timeout set to %d %s.\n",
368 timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
369
370 return nonseekable_open(inode, file);
371}
372
373
374
375static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
376{
377
378
379 if (expect_close == 42) {
380 wb_smsc_wdt_disable();
381 printk(KERN_INFO MODNAME
382 "Watchdog disabled, sleeping again...\n");
383 } else {
384 printk(KERN_CRIT MODNAME
385 "Unexpected close, not stopping watchdog!\n");
386 wb_smsc_wdt_reset_timer();
387 }
388
389 clear_bit(0, &timer_enabled);
390 expect_close = 0;
391 return 0;
392}
393
394
395
396static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data,
397 size_t len, loff_t *ppos)
398{
399
400 if (len) {
401 if (!nowayout) {
402 size_t i;
403
404
405 expect_close = 0;
406
407
408
409 for (i = 0; i != len; i++) {
410 char c;
411 if (get_user(c, data + i))
412 return -EFAULT;
413 if (c == 'V')
414 expect_close = 42;
415 }
416 }
417
418
419 wb_smsc_wdt_reset_timer();
420 }
421 return len;
422}
423
424
425
426static long wb_smsc_wdt_ioctl(struct file *file,
427 unsigned int cmd, unsigned long arg)
428{
429 int new_timeout;
430
431 union {
432 struct watchdog_info __user *ident;
433 int __user *i;
434 } uarg;
435
436 static const struct watchdog_info ident = {
437 .options = WDIOF_KEEPALIVEPING |
438 WDIOF_SETTIMEOUT |
439 WDIOF_MAGICCLOSE,
440 .firmware_version = 0,
441 .identity = "SMsC 37B787 Watchdog",
442 };
443
444 uarg.i = (int __user *)arg;
445
446 switch (cmd) {
447 case WDIOC_GETSUPPORT:
448 return copy_to_user(uarg.ident, &ident, sizeof(ident))
449 ? -EFAULT : 0;
450 case WDIOC_GETSTATUS:
451 return put_user(wb_smsc_wdt_status(), uarg.i);
452 case WDIOC_GETBOOTSTATUS:
453 return put_user(0, uarg.i);
454 case WDIOC_SETOPTIONS:
455 {
456 int options, retval = -EINVAL;
457
458 if (get_user(options, uarg.i))
459 return -EFAULT;
460
461 if (options & WDIOS_DISABLECARD) {
462 wb_smsc_wdt_disable();
463 retval = 0;
464 }
465 if (options & WDIOS_ENABLECARD) {
466 wb_smsc_wdt_enable();
467 retval = 0;
468 }
469 return retval;
470 }
471 case WDIOC_KEEPALIVE:
472 wb_smsc_wdt_reset_timer();
473 return 0;
474 case WDIOC_SETTIMEOUT:
475 if (get_user(new_timeout, uarg.i))
476 return -EFAULT;
477
478 if (unit == UNIT_MINUTE)
479 new_timeout /= 60;
480 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
481 return -EINVAL;
482 timeout = new_timeout;
483 wb_smsc_wdt_set_timeout(timeout);
484
485 case WDIOC_GETTIMEOUT:
486 new_timeout = timeout;
487 if (unit == UNIT_MINUTE)
488 new_timeout *= 60;
489 return put_user(new_timeout, uarg.i);
490 default:
491 return -ENOTTY;
492 }
493}
494
495
496
497static int wb_smsc_wdt_notify_sys(struct notifier_block *this,
498 unsigned long code, void *unused)
499{
500 if (code == SYS_DOWN || code == SYS_HALT) {
501
502 timeout = 0;
503 wb_smsc_wdt_disable();
504 }
505 return NOTIFY_DONE;
506}
507
508
509
510static const struct file_operations wb_smsc_wdt_fops = {
511 .owner = THIS_MODULE,
512 .llseek = no_llseek,
513 .write = wb_smsc_wdt_write,
514 .unlocked_ioctl = wb_smsc_wdt_ioctl,
515 .open = wb_smsc_wdt_open,
516 .release = wb_smsc_wdt_release,
517};
518
519static struct notifier_block wb_smsc_wdt_notifier = {
520 .notifier_call = wb_smsc_wdt_notify_sys,
521};
522
523static struct miscdevice wb_smsc_wdt_miscdev = {
524 .minor = WATCHDOG_MINOR,
525 .name = "watchdog",
526 .fops = &wb_smsc_wdt_fops,
527};
528
529
530
531
532
533static int __init wb_smsc_wdt_init(void)
534{
535 int ret;
536
537 printk(KERN_INFO "SMsC 37B787 watchdog component driver "
538 VERSION " initialising...\n");
539
540 if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
541 printk(KERN_ERR MODNAME "Unable to register IO port %#x\n",
542 IOPORT);
543 ret = -EBUSY;
544 goto out_pnp;
545 }
546
547
548 if (timeout > MAX_TIMEOUT)
549 timeout = MAX_TIMEOUT;
550
551
552 wb_smsc_wdt_initialize();
553
554 ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
555 if (ret) {
556 printk(KERN_ERR MODNAME
557 "Unable to register reboot notifier err = %d\n", ret);
558 goto out_io;
559 }
560
561 ret = misc_register(&wb_smsc_wdt_miscdev);
562 if (ret) {
563 printk(KERN_ERR MODNAME
564 "Unable to register miscdev on minor %d\n",
565 WATCHDOG_MINOR);
566 goto out_rbt;
567 }
568
569
570 printk(KERN_INFO MODNAME "Timeout set to %d %s.\n",
571 timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
572 printk(KERN_INFO MODNAME
573 "Watchdog initialized and sleeping (nowayout=%d)...\n",
574 nowayout);
575out_clean:
576 return ret;
577
578out_rbt:
579 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
580
581out_io:
582 release_region(IOPORT, IOPORT_SIZE);
583
584out_pnp:
585 goto out_clean;
586}
587
588
589
590static void __exit wb_smsc_wdt_exit(void)
591{
592
593 if (!nowayout) {
594 wb_smsc_wdt_shutdown();
595 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
596 }
597
598 misc_deregister(&wb_smsc_wdt_miscdev);
599 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
600 release_region(IOPORT, IOPORT_SIZE);
601
602 printk(KERN_INFO "SMsC 37B787 watchdog component driver removed.\n");
603}
604
605module_init(wb_smsc_wdt_init);
606module_exit(wb_smsc_wdt_exit);
607
608MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
609MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version "
610 VERSION ")");
611MODULE_LICENSE("GPL");
612
613MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
614
615#ifdef SMSC_SUPPORT_MINUTES
616module_param(unit, int, 0);
617MODULE_PARM_DESC(unit,
618 "set unit to use, 0=seconds or 1=minutes, default is 0");
619#endif
620
621module_param(timeout, int, 0);
622MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
623
624module_param(nowayout, int, 0);
625MODULE_PARM_DESC(nowayout,
626 "Watchdog cannot be stopped once started (default="
627 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
628