2 * SMsC 37B787 Watchdog Timer driver for Linux 2.6.x.x
4 * Based on acquirewdt.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * and some other existing drivers
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * The authors do NOT admit liability nor provide warranty for
13 * any of this software. This material is provided "AS-IS" in
14 * the hope that it may be useful for others.
16 * (C) Copyright 2003-2006 Sven Anders <anders@anduras.de>
19 * 2003 - Created version 1.0 for Linux 2.4.x.
20 * 2006 - Ported to Linux 2.6, added nowayout and MAGICCLOSE
21 * features. Released version 1.1
23 * Theory of operation:
25 * A Watchdog Timer (WDT) is a hardware circuit that can
26 * reset the computer system in case of a software fault.
27 * You probably knew that already.
29 * Usually a userspace daemon will notify the kernel WDT driver
30 * via the /dev/watchdog special device file that userspace is
31 * still alive, at regular intervals. When such a notification
32 * occurs, the driver will usually tell the hardware watchdog
33 * that everything is in order, and that the watchdog should wait
34 * for yet another little while to reset the system.
35 * If userspace fails (RAM error, kernel bug, whatever), the
36 * notifications cease to occur, and the hardware watchdog will
37 * reset the system (causing a reboot) after the timeout occurs.
40 * mknod /dev/watchdog c 10 130
42 * For an example userspace keep-alive daemon, see:
43 * Documentation/watchdog/wdt.txt
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48 #include <linux/module.h>
49 #include <linux/moduleparam.h>
50 #include <linux/types.h>
51 #include <linux/miscdevice.h>
52 #include <linux/watchdog.h>
53 #include <linux/delay.h>
55 #include <linux/ioport.h>
56 #include <linux/notifier.h>
57 #include <linux/reboot.h>
58 #include <linux/init.h>
59 #include <linux/spinlock.h>
61 #include <linux/uaccess.h>
63 #include <asm/system.h>
65 /* enable support for minutes as units? */
66 /* (does not always work correctly, so disabled by default!) */
67 #define SMSC_SUPPORT_MINUTES
68 #undef SMSC_SUPPORT_MINUTES
70 #define MAX_TIMEOUT 255
81 static int unit
= UNIT_SECOND
; /* timer's unit */
82 static int timeout
= 60; /* timeout value: default is 60 "units" */
83 static unsigned long timer_enabled
; /* is the timer enabled? */
85 static char expect_close
; /* is the close expected? */
87 static DEFINE_SPINLOCK(io_lock
);/* to guard the watchdog from io races */
89 static int nowayout
= WATCHDOG_NOWAYOUT
;
91 /* -- Low level function ----------------------------------------*/
93 /* unlock the IO chip */
95 static inline void open_io_config(void)
102 /* lock the IO chip */
103 static inline void close_io_config(void)
108 /* select the IO device */
109 static inline void select_io_device(unsigned char devno
)
112 outb(devno
, IOPORT
+1);
115 /* write to the control register */
116 static inline void write_io_cr(unsigned char reg
, unsigned char data
)
119 outb(data
, IOPORT
+1);
122 /* read from the control register */
123 static inline char read_io_cr(unsigned char reg
)
126 return inb(IOPORT
+1);
129 /* -- Medium level functions ------------------------------------*/
131 static inline void gpio_bit12(unsigned char reg
)
133 /* -- General Purpose I/O Bit 1.2 --
134 * Bit 0, In/Out: 0 = Output, 1 = Input
135 * Bit 1, Polarity: 0 = No Invert, 1 = Invert
136 * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
137 * Bit 3/4, Function select: 00 = GPI/O, 01 = WDT, 10 = P17,
138 * 11 = Either Edge Triggered Intr. 2
140 * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
142 write_io_cr(0xE2, reg
);
145 static inline void gpio_bit13(unsigned char reg
)
147 /* -- General Purpose I/O Bit 1.3 --
148 * Bit 0, In/Out: 0 = Output, 1 = Input
149 * Bit 1, Polarity: 0 = No Invert, 1 = Invert
150 * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
151 * Bit 3, Function select: 0 = GPI/O, 1 = LED
153 * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
155 write_io_cr(0xE3, reg
);
158 static inline void wdt_timer_units(unsigned char new_units
)
160 /* -- Watchdog timer units --
162 * Bit 7, WDT Time-out Value Units Select
163 * (0 = Minutes, 1 = Seconds)
165 write_io_cr(0xF1, new_units
);
168 static inline void wdt_timeout_value(unsigned char new_timeout
)
170 /* -- Watchdog Timer Time-out Value --
171 * Bit 0-7 Binary coded units (0=Disabled, 1..255)
173 write_io_cr(0xF2, new_timeout
);
176 static inline void wdt_timer_conf(unsigned char conf
)
178 /* -- Watchdog timer configuration --
179 * Bit 0 Joystick enable: 0* = No Reset, 1 = Reset WDT upon
181 * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
182 * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr
183 * Bit 3 Reset the timer
184 * (Wrong in SMsC documentation? Given as: PowerLED Timout
186 * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
187 * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
189 write_io_cr(0xF3, conf
);
192 static inline void wdt_timer_ctrl(unsigned char reg
)
194 /* -- Watchdog timer control --
195 * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occurred
196 * Bit 1 Power LED Toggle: 0 = Disable Toggle, 1 = Toggle at 1 Hz
197 * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
198 * Bit 3 P20 Force Timeout enabled:
199 * 0 = P20 activity does not generate the WD timeout event
200 * 1 = P20 Allows rising edge of P20, from the keyboard
201 * controller, to force the WD timeout event.
203 * -- Soft power management --
204 * Bit 5 Stop Counter: 1 = Stop software power down counter
205 * set via register 0xB8, (self-cleaning)
206 * (Upon read: 0 = Counter running, 1 = Counter stopped)
207 * Bit 6 Restart Counter: 1 = Restart software power down counter
208 * set via register 0xB8, (self-cleaning)
209 * Bit 7 SPOFF: 1 = Force software power down (self-cleaning)
211 write_io_cr(0xF4, reg
);
214 /* -- Higher level functions ------------------------------------*/
216 /* initialize watchdog */
218 static void wb_smsc_wdt_initialize(void)
224 select_io_device(IODEV_NO
);
226 /* enable the watchdog */
227 gpio_bit13(0x08); /* Select pin 80 = LED not GPIO */
228 gpio_bit12(0x0A); /* Set pin 79 = WDT not
229 GPIO/Output/Polarity=Invert */
230 /* disable the timeout */
231 wdt_timeout_value(0);
233 /* reset control register */
234 wdt_timer_ctrl(0x00);
236 /* reset configuration register */
237 wdt_timer_conf(0x00);
239 /* read old (timer units) register */
240 old
= read_io_cr(0xF1) & 0x7F;
241 if (unit
== UNIT_SECOND
)
242 old
|= 0x80; /* set to seconds */
244 /* set the watchdog timer units */
245 wdt_timer_units(old
);
248 spin_unlock(&io_lock
);
251 /* shutdown the watchdog */
253 static void wb_smsc_wdt_shutdown(void)
257 select_io_device(IODEV_NO
);
259 /* disable the watchdog */
263 /* reset watchdog config register */
264 wdt_timer_conf(0x00);
266 /* reset watchdog control register */
267 wdt_timer_ctrl(0x00);
269 /* disable timeout */
270 wdt_timeout_value(0x00);
273 spin_unlock(&io_lock
);
276 /* set timeout => enable watchdog */
278 static void wb_smsc_wdt_set_timeout(unsigned char new_timeout
)
282 select_io_device(IODEV_NO
);
284 /* set Power LED to blink, if we enable the timeout */
285 wdt_timer_ctrl((new_timeout
== 0) ? 0x00 : 0x02);
287 /* set timeout value */
288 wdt_timeout_value(new_timeout
);
291 spin_unlock(&io_lock
);
296 static unsigned char wb_smsc_wdt_get_timeout(void)
298 unsigned char set_timeout
;
302 select_io_device(IODEV_NO
);
303 set_timeout
= read_io_cr(0xF2);
305 spin_unlock(&io_lock
);
310 /* disable watchdog */
312 static void wb_smsc_wdt_disable(void)
314 /* set the timeout to 0 to disable the watchdog */
315 wb_smsc_wdt_set_timeout(0);
318 /* enable watchdog by setting the current timeout */
320 static void wb_smsc_wdt_enable(void)
322 /* set the current timeout... */
323 wb_smsc_wdt_set_timeout(timeout
);
326 /* reset the timer */
328 static void wb_smsc_wdt_reset_timer(void)
332 select_io_device(IODEV_NO
);
334 /* reset the timer */
335 wdt_timeout_value(timeout
);
336 wdt_timer_conf(0x08);
339 spin_unlock(&io_lock
);
342 /* return, if the watchdog is enabled (timeout is set...) */
344 static int wb_smsc_wdt_status(void)
346 return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING
;
350 /* -- File operations -------------------------------------------*/
352 /* open => enable watchdog and set initial timeout */
354 static int wb_smsc_wdt_open(struct inode
*inode
, struct file
*file
)
356 /* /dev/watchdog can only be opened once */
358 if (test_and_set_bit(0, &timer_enabled
))
362 __module_get(THIS_MODULE
);
364 /* Reload and activate timer */
365 wb_smsc_wdt_enable();
367 pr_info("Watchdog enabled. Timeout set to %d %s\n",
368 timeout
, (unit
== UNIT_SECOND
) ? "second(s)" : "minute(s)");
370 return nonseekable_open(inode
, file
);
373 /* close => shut off the timer */
375 static int wb_smsc_wdt_release(struct inode
*inode
, struct file
*file
)
377 /* Shut off the timer. */
379 if (expect_close
== 42) {
380 wb_smsc_wdt_disable();
381 pr_info("Watchdog disabled, sleeping again...\n");
383 pr_crit("Unexpected close, not stopping watchdog!\n");
384 wb_smsc_wdt_reset_timer();
387 clear_bit(0, &timer_enabled
);
392 /* write => update the timer to keep the machine alive */
394 static ssize_t
wb_smsc_wdt_write(struct file
*file
, const char __user
*data
,
395 size_t len
, loff_t
*ppos
)
397 /* See if we got the magic character 'V' and reload the timer */
402 /* reset expect flag */
405 /* scan to see whether or not we got the
407 for (i
= 0; i
!= len
; i
++) {
409 if (get_user(c
, data
+ i
))
416 /* someone wrote to us, we should reload the timer */
417 wb_smsc_wdt_reset_timer();
422 /* ioctl => control interface */
424 static long wb_smsc_wdt_ioctl(struct file
*file
,
425 unsigned int cmd
, unsigned long arg
)
430 struct watchdog_info __user
*ident
;
434 static const struct watchdog_info ident
= {
435 .options
= WDIOF_KEEPALIVEPING
|
438 .firmware_version
= 0,
439 .identity
= "SMsC 37B787 Watchdog",
442 uarg
.i
= (int __user
*)arg
;
445 case WDIOC_GETSUPPORT
:
446 return copy_to_user(uarg
.ident
, &ident
, sizeof(ident
))
448 case WDIOC_GETSTATUS
:
449 return put_user(wb_smsc_wdt_status(), uarg
.i
);
450 case WDIOC_GETBOOTSTATUS
:
451 return put_user(0, uarg
.i
);
452 case WDIOC_SETOPTIONS
:
454 int options
, retval
= -EINVAL
;
456 if (get_user(options
, uarg
.i
))
459 if (options
& WDIOS_DISABLECARD
) {
460 wb_smsc_wdt_disable();
463 if (options
& WDIOS_ENABLECARD
) {
464 wb_smsc_wdt_enable();
469 case WDIOC_KEEPALIVE
:
470 wb_smsc_wdt_reset_timer();
472 case WDIOC_SETTIMEOUT
:
473 if (get_user(new_timeout
, uarg
.i
))
475 /* the API states this is given in secs */
476 if (unit
== UNIT_MINUTE
)
478 if (new_timeout
< 0 || new_timeout
> MAX_TIMEOUT
)
480 timeout
= new_timeout
;
481 wb_smsc_wdt_set_timeout(timeout
);
482 /* fall through and return the new timeout... */
483 case WDIOC_GETTIMEOUT
:
484 new_timeout
= timeout
;
485 if (unit
== UNIT_MINUTE
)
487 return put_user(new_timeout
, uarg
.i
);
493 /* -- Notifier funtions -----------------------------------------*/
495 static int wb_smsc_wdt_notify_sys(struct notifier_block
*this,
496 unsigned long code
, void *unused
)
498 if (code
== SYS_DOWN
|| code
== SYS_HALT
) {
499 /* set timeout to 0, to avoid possible race-condition */
501 wb_smsc_wdt_disable();
506 /* -- Module's structures ---------------------------------------*/
508 static const struct file_operations wb_smsc_wdt_fops
= {
509 .owner
= THIS_MODULE
,
511 .write
= wb_smsc_wdt_write
,
512 .unlocked_ioctl
= wb_smsc_wdt_ioctl
,
513 .open
= wb_smsc_wdt_open
,
514 .release
= wb_smsc_wdt_release
,
517 static struct notifier_block wb_smsc_wdt_notifier
= {
518 .notifier_call
= wb_smsc_wdt_notify_sys
,
521 static struct miscdevice wb_smsc_wdt_miscdev
= {
522 .minor
= WATCHDOG_MINOR
,
524 .fops
= &wb_smsc_wdt_fops
,
527 /* -- Module init functions -------------------------------------*/
529 /* module's "constructor" */
531 static int __init
wb_smsc_wdt_init(void)
535 pr_info("SMsC 37B787 watchdog component driver "
536 VERSION
" initialising...\n");
538 if (!request_region(IOPORT
, IOPORT_SIZE
, "SMsC 37B787 watchdog")) {
539 pr_err("Unable to register IO port %#x\n", IOPORT
);
544 /* set new maximum, if it's too big */
545 if (timeout
> MAX_TIMEOUT
)
546 timeout
= MAX_TIMEOUT
;
548 /* init the watchdog timer */
549 wb_smsc_wdt_initialize();
551 ret
= register_reboot_notifier(&wb_smsc_wdt_notifier
);
553 pr_err("Unable to register reboot notifier err = %d\n", ret
);
557 ret
= misc_register(&wb_smsc_wdt_miscdev
);
559 pr_err("Unable to register miscdev on minor %d\n",
565 pr_info("Timeout set to %d %s\n",
566 timeout
, (unit
== UNIT_SECOND
) ? "second(s)" : "minute(s)");
567 pr_info("Watchdog initialized and sleeping (nowayout=%d)...\n",
573 unregister_reboot_notifier(&wb_smsc_wdt_notifier
);
576 release_region(IOPORT
, IOPORT_SIZE
);
582 /* module's "destructor" */
584 static void __exit
wb_smsc_wdt_exit(void)
586 /* Stop the timer before we leave */
588 wb_smsc_wdt_shutdown();
589 pr_info("Watchdog disabled\n");
592 misc_deregister(&wb_smsc_wdt_miscdev
);
593 unregister_reboot_notifier(&wb_smsc_wdt_notifier
);
594 release_region(IOPORT
, IOPORT_SIZE
);
596 pr_info("SMsC 37B787 watchdog component driver removed\n");
599 module_init(wb_smsc_wdt_init
);
600 module_exit(wb_smsc_wdt_exit
);
602 MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
603 MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version "
605 MODULE_LICENSE("GPL");
607 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
609 #ifdef SMSC_SUPPORT_MINUTES
610 module_param(unit
, int, 0);
611 MODULE_PARM_DESC(unit
,
612 "set unit to use, 0=seconds or 1=minutes, default is 0");
615 module_param(timeout
, int, 0);
616 MODULE_PARM_DESC(timeout
, "range is 1-255 units, default is 60");
618 module_param(nowayout
, int, 0);
619 MODULE_PARM_DESC(nowayout
,
620 "Watchdog cannot be stopped once started (default="
621 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");