2 * SMsC 37B787 Watchdog Timer driver for Linux 2.6.x.x
4 * Based on acquirewdt.c by Alan Cox <alan@redhat.com>
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/watchdog.txt
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>
53 #include <linux/ioport.h>
54 #include <linux/notifier.h>
55 #include <linux/reboot.h>
56 #include <linux/init.h>
59 #include <asm/uaccess.h>
60 #include <asm/system.h>
62 /* enable support for minutes as units? */
63 /* (does not always work correctly, so disabled by default!) */
64 #define SMSC_SUPPORT_MINUTES
65 #undef SMSC_SUPPORT_MINUTES
67 #define MAX_TIMEOUT 255
72 #define MODNAME "smsc37b787_wdt: "
75 #define WATCHDOG_MINOR 130
81 static int unit = UNIT_SECOND; /* timer's unit */
82 static int timeout = 60; /* timeout value: default is 60 "units" */
83 static int timer_enabled = 0; /* is the timer enabled? */
85 static char expect_close; /* is the close expected? */
87 static int nowayout = WATCHDOG_NOWAYOUT;
89 /* -- Low level function ----------------------------------------*/
91 /* unlock the IO chip */
93 static inline void open_io_config(void)
100 /* lock the IO chip */
101 static inline void close_io_config(void)
106 /* select the IO device */
107 static inline void select_io_device(unsigned char devno)
110 outb(devno, IOPORT+1);
113 /* write to the control register */
114 static inline void write_io_cr(unsigned char reg, unsigned char data)
117 outb(data, IOPORT+1);
120 /* read from the control register */
121 static inline char read_io_cr(unsigned char reg)
124 return inb(IOPORT+1);
127 /* -- Medium level functions ------------------------------------*/
129 static inline void gpio_bit12(unsigned char reg)
131 // -- General Purpose I/O Bit 1.2 --
132 // Bit 0, In/Out: 0 = Output, 1 = Input
133 // Bit 1, Polarity: 0 = No Invert, 1 = Invert
134 // Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
135 // Bit 3/4, Function select: 00 = GPI/O, 01 = WDT, 10 = P17,
136 // 11 = Either Edge Triggered Intr. 2
137 // Bit 5/6 (Reserved)
138 // Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
139 write_io_cr(0xE2, reg);
142 static inline void gpio_bit13(unsigned char reg)
144 // -- General Purpose I/O Bit 1.3 --
145 // Bit 0, In/Out: 0 = Output, 1 = Input
146 // Bit 1, Polarity: 0 = No Invert, 1 = Invert
147 // Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable
148 // Bit 3, Function select: 0 = GPI/O, 1 = LED
149 // Bit 4-6 (Reserved)
150 // Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain
151 write_io_cr(0xE3, reg);
154 static inline void wdt_timer_units(unsigned char new_units)
156 // -- Watchdog timer units --
157 // Bit 0-6 (Reserved)
158 // Bit 7, WDT Time-out Value Units Select
159 // (0 = Minutes, 1 = Seconds)
160 write_io_cr(0xF1, new_units);
163 static inline void wdt_timeout_value(unsigned char new_timeout)
165 // -- Watchdog Timer Time-out Value --
166 // Bit 0-7 Binary coded units (0=Disabled, 1..255)
167 write_io_cr(0xF2, new_timeout);
170 static inline void wdt_timer_conf(unsigned char conf)
172 // -- Watchdog timer configuration --
173 // Bit 0 Joystick enable: 0* = No Reset, 1 = Reset WDT upon Gameport I/O
174 // Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr.
175 // Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr.
176 // Bit 3 Reset the timer
177 // (Wrong in SMsC documentation? Given as: PowerLED Timout Enabled)
178 // Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled,
179 // 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15)
180 write_io_cr(0xF3, conf);
183 static inline void wdt_timer_ctrl(unsigned char reg)
185 // -- Watchdog timer control --
186 // Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occured
187 // Bit 1 Power LED Toggle: 0 = Disable Toggle, 1 = Toggle at 1 Hz
188 // Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning)
189 // Bit 3 P20 Force Timeout enabled:
190 // 0 = P20 activity does not generate the WD timeout event
191 // 1 = P20 Allows rising edge of P20, from the keyboard
192 // controller, to force the WD timeout event.
194 // -- Soft power management --
195 // Bit 5 Stop Counter: 1 = Stop software power down counter
196 // set via register 0xB8, (self-cleaning)
197 // (Upon read: 0 = Counter running, 1 = Counter stopped)
198 // Bit 6 Restart Counter: 1 = Restart software power down counter
199 // set via register 0xB8, (self-cleaning)
200 // Bit 7 SPOFF: 1 = Force software power down (self-cleaning)
202 write_io_cr(0xF4, reg);
205 /* -- Higher level functions ------------------------------------*/
207 /* initialize watchdog */
209 static void wb_smsc_wdt_initialize(void)
214 select_io_device(IODEV_NO);
216 // enable the watchdog
217 gpio_bit13(0x08); // Select pin 80 = LED not GPIO
218 gpio_bit12(0x0A); // Set pin 79 = WDT not GPIO/Output/Polarity=Invert
220 // disable the timeout
221 wdt_timeout_value(0);
223 // reset control register
224 wdt_timer_ctrl(0x00);
226 // reset configuration register
227 wdt_timer_conf(0x00);
229 // read old (timer units) register
230 old = read_io_cr(0xF1) & 0x7F;
231 if (unit == UNIT_SECOND) old |= 0x80; // set to seconds
233 // set the watchdog timer units
234 wdt_timer_units(old);
239 /* shutdown the watchdog */
241 static void wb_smsc_wdt_shutdown(void)
244 select_io_device(IODEV_NO);
246 // disable the watchdog
250 // reset watchdog config register
251 wdt_timer_conf(0x00);
253 // reset watchdog control register
254 wdt_timer_ctrl(0x00);
257 wdt_timeout_value(0x00);
262 /* set timeout => enable watchdog */
264 static void wb_smsc_wdt_set_timeout(unsigned char new_timeout)
267 select_io_device(IODEV_NO);
269 // set Power LED to blink, if we enable the timeout
270 wdt_timer_ctrl((new_timeout == 0) ? 0x00 : 0x02);
273 wdt_timeout_value(new_timeout);
280 static unsigned char wb_smsc_wdt_get_timeout(void)
282 unsigned char set_timeout;
285 select_io_device(IODEV_NO);
286 set_timeout = read_io_cr(0xF2);
292 /* disable watchdog */
294 static void wb_smsc_wdt_disable(void)
296 // set the timeout to 0 to disable the watchdog
297 wb_smsc_wdt_set_timeout(0);
300 /* enable watchdog by setting the current timeout */
302 static void wb_smsc_wdt_enable(void)
304 // set the current timeout...
305 wb_smsc_wdt_set_timeout(timeout);
308 /* reset the timer */
310 static void wb_smsc_wdt_reset_timer(void)
313 select_io_device(IODEV_NO);
316 wdt_timeout_value(timeout);
317 wdt_timer_conf(0x08);
322 /* return, if the watchdog is enabled (timeout is set...) */
324 static int wb_smsc_wdt_status(void)
326 return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING;
330 /* -- File operations -------------------------------------------*/
332 /* open => enable watchdog and set initial timeout */
334 static int wb_smsc_wdt_open(struct inode *inode, struct file *file)
336 /* /dev/watchdog can only be opened once */
342 __module_get(THIS_MODULE);
344 /* Reload and activate timer */
346 wb_smsc_wdt_enable();
348 printk(KERN_INFO MODNAME "Watchdog enabled. Timeout set to %d %s.\n", timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
350 return nonseekable_open(inode, file);
353 /* close => shut off the timer */
355 static int wb_smsc_wdt_release(struct inode *inode, struct file *file)
357 /* Shut off the timer. */
359 if (expect_close == 42) {
360 wb_smsc_wdt_disable();
361 printk(KERN_INFO MODNAME "Watchdog disabled, sleeping again...\n");
363 printk(KERN_CRIT MODNAME "Unexpected close, not stopping watchdog!\n");
364 wb_smsc_wdt_reset_timer();
372 /* write => update the timer to keep the machine alive */
374 static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data,
375 size_t len, loff_t *ppos)
377 /* See if we got the magic character 'V' and reload the timer */
382 /* reset expect flag */
385 /* scan to see whether or not we got the magic character */
386 for (i = 0; i != len; i++) {
388 if (get_user(c, data+i))
395 /* someone wrote to us, we should reload the timer */
396 wb_smsc_wdt_reset_timer();
401 /* ioctl => control interface */
403 static int wb_smsc_wdt_ioctl(struct inode *inode, struct file *file,
404 unsigned int cmd, unsigned long arg)
409 struct watchdog_info __user *ident;
413 static struct watchdog_info ident = {
414 .options = WDIOF_KEEPALIVEPING |
417 .firmware_version = 0,
418 .identity = "SMsC 37B787 Watchdog"
421 uarg.i = (int __user *)arg;
427 case WDIOC_GETSUPPORT:
428 return copy_to_user(uarg.ident, &ident, sizeof(ident));
430 case WDIOC_GETSTATUS:
431 return put_user(wb_smsc_wdt_status(), uarg.i);
433 case WDIOC_GETBOOTSTATUS:
434 return put_user(0, uarg.i);
436 case WDIOC_KEEPALIVE:
437 wb_smsc_wdt_reset_timer();
440 case WDIOC_SETTIMEOUT:
441 if (get_user(new_timeout, uarg.i))
444 // the API states this is given in secs
445 if (unit == UNIT_MINUTE)
448 if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
451 timeout = new_timeout;
452 wb_smsc_wdt_set_timeout(timeout);
454 // fall through and return the new timeout...
456 case WDIOC_GETTIMEOUT:
458 new_timeout = timeout;
460 if (unit == UNIT_MINUTE)
463 return put_user(new_timeout, uarg.i);
465 case WDIOC_SETOPTIONS:
467 int options, retval = -EINVAL;
469 if (get_user(options, uarg.i))
472 if (options & WDIOS_DISABLECARD) {
473 wb_smsc_wdt_disable();
477 if (options & WDIOS_ENABLECARD) {
478 wb_smsc_wdt_enable();
487 /* -- Notifier funtions -----------------------------------------*/
489 static int wb_smsc_wdt_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
491 if (code == SYS_DOWN || code == SYS_HALT)
493 // set timeout to 0, to avoid possible race-condition
495 wb_smsc_wdt_disable();
500 /* -- Module's structures ---------------------------------------*/
502 static struct file_operations wb_smsc_wdt_fops =
504 .owner = THIS_MODULE,
506 .write = wb_smsc_wdt_write,
507 .ioctl = wb_smsc_wdt_ioctl,
508 .open = wb_smsc_wdt_open,
509 .release = wb_smsc_wdt_release
512 static struct notifier_block wb_smsc_wdt_notifier =
514 .notifier_call = wb_smsc_wdt_notify_sys
517 static struct miscdevice wb_smsc_wdt_miscdev =
519 .minor = WATCHDOG_MINOR,
521 .fops = &wb_smsc_wdt_fops,
524 /* -- Module init functions -------------------------------------*/
526 /* module's "constructor" */
528 static int __init wb_smsc_wdt_init(void)
532 printk("SMsC 37B787 watchdog component driver " VERSION " initialising...\n");
534 if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) {
535 printk(KERN_ERR MODNAME "Unable to register IO port %#x\n", IOPORT);
540 ret = register_reboot_notifier(&wb_smsc_wdt_notifier);
542 printk(KERN_ERR MODNAME "Unable to register reboot notifier err = %d\n", ret);
546 ret = misc_register(&wb_smsc_wdt_miscdev);
548 printk(KERN_ERR MODNAME "Unable to register miscdev on minor %d\n", WATCHDOG_MINOR);
552 // init the watchdog timer
553 wb_smsc_wdt_initialize();
555 // set new maximum, if it's too big
556 if (timeout > MAX_TIMEOUT)
557 timeout = MAX_TIMEOUT;
560 printk(KERN_INFO MODNAME "Timeout set to %d %s.\n", timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)");
561 printk(KERN_INFO MODNAME "Watchdog initialized and sleeping (nowayout=%d)...\n", nowayout);
569 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
572 release_region(IOPORT, IOPORT_SIZE);
578 /* module's "destructor" */
580 static void __exit wb_smsc_wdt_exit(void)
582 /* Stop the timer before we leave */
585 wb_smsc_wdt_shutdown();
586 printk(KERN_INFO MODNAME "Watchdog disabled.\n");
589 misc_deregister(&wb_smsc_wdt_miscdev);
590 unregister_reboot_notifier(&wb_smsc_wdt_notifier);
591 release_region(IOPORT, IOPORT_SIZE);
593 printk("SMsC 37B787 watchdog component driver removed.\n");
596 module_init(wb_smsc_wdt_init);
597 module_exit(wb_smsc_wdt_exit);
599 MODULE_AUTHOR("Sven Anders <anders@anduras.de>");
600 MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version " VERSION ")");
601 MODULE_LICENSE("GPL");
603 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
605 #ifdef SMSC_SUPPORT_MINUTES
606 module_param(unit, int, 0);
607 MODULE_PARM_DESC(unit, "set unit to use, 0=seconds or 1=minutes, default is 0");
610 module_param(timeout, int, 60);
611 MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60");
613 module_param(nowayout, int, 0);
614 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");