2 * Watchdog Timer Driver
3 * for ITE IT87xx Environment Control - Low Pin Count Input / Output
5 * (c) Copyright 2007 Oliver Schuster <olivers137@aol.com>
7 * Based on softdog.c by Alan Cox,
8 * 83977f_wdt.c by Jose Goncalves,
9 * it87.c by Chris Gauthron, Jean Delvare
11 * Data-sheets: Publicly available at the ITE website
12 * http://www.ite.com.tw/
14 * Support of the watchdog timers, which are available on
15 * IT8702, IT8712, IT8716, IT8718, IT8720, IT8721, IT8726
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
40 #include <linux/miscdevice.h>
41 #include <linux/init.h>
42 #include <linux/ioport.h>
43 #include <linux/watchdog.h>
44 #include <linux/notifier.h>
45 #include <linux/reboot.h>
46 #include <linux/uaccess.h>
50 #define WATCHDOG_VERSION "1.14"
51 #define WATCHDOG_NAME "IT87 WDT"
52 #define DRIVER_VERSION WATCHDOG_NAME " driver, v" WATCHDOG_VERSION "\n"
55 /* Defaults for Module Parameter */
56 #define DEFAULT_NOGAMEPORT 0
57 #define DEFAULT_EXCLUSIVE 1
58 #define DEFAULT_TIMEOUT 60
59 #define DEFAULT_TESTMODE 0
60 #define DEFAULT_NOWAYOUT WATCHDOG_NOWAYOUT
66 /* Logical device Numbers LDN */
71 /* Configuration Registers and Functions */
79 #define NO_DEV_ID 0xffff
80 #define IT8702_ID 0x8702
81 #define IT8705_ID 0x8705
82 #define IT8712_ID 0x8712
83 #define IT8716_ID 0x8716
84 #define IT8718_ID 0x8718
85 #define IT8720_ID 0x8720
86 #define IT8721_ID 0x8721
87 #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
88 #define IT8728_ID 0x8728
90 /* GPIO Configuration Registers LDN=0x07 */
93 #define WDTVALLSB 0x73
94 #define WDTVALMSB 0x74
96 /* GPIO Bits WDTCTRL */
97 #define WDT_CIRINT 0x80
98 #define WDT_MOUSEINT 0x40
99 #define WDT_KYBINT 0x20
100 #define WDT_GAMEPORT 0x10 /* not in it8718, it8720, it8721, it8728 */
101 #define WDT_FORCE 0x02
102 #define WDT_ZERO 0x01
104 /* GPIO Bits WDTCFG */
105 #define WDT_TOV1 0x80
106 #define WDT_KRST 0x40
107 #define WDT_TOVE 0x20
108 #define WDT_PWROK 0x10 /* not in it8721 */
109 #define WDT_INT_MASK 0x0f
111 /* CIR Configuration Register LDN=0x0a */
114 /* The default Base address is not always available, we use this */
115 #define CIR_BASE 0x0208
118 #define CIR_DR(b) (b)
119 #define CIR_IER(b) (b + 1)
120 #define CIR_RCR(b) (b + 2)
121 #define CIR_TCR1(b) (b + 3)
122 #define CIR_TCR2(b) (b + 4)
123 #define CIR_TSR(b) (b + 5)
124 #define CIR_RSR(b) (b + 6)
125 #define CIR_BDLR(b) (b + 5)
126 #define CIR_BDHR(b) (b + 6)
127 #define CIR_IIR(b) (b + 7)
129 /* Default Base address of Game port */
130 #define GP_BASE_DEFAULT 0x0201
133 #define WDTS_TIMER_RUN 0
134 #define WDTS_DEV_OPEN 1
135 #define WDTS_KEEPALIVE 2
136 #define WDTS_LOCKED 3
137 #define WDTS_USE_GP 4
138 #define WDTS_EXPECTED 5
140 static unsigned int base, gpact, ciract, max_units, chip_type;
141 static unsigned long wdt_status;
143 static int nogameport = DEFAULT_NOGAMEPORT;
144 static int exclusive = DEFAULT_EXCLUSIVE;
145 static int timeout = DEFAULT_TIMEOUT;
146 static int testmode = DEFAULT_TESTMODE;
147 static bool nowayout = DEFAULT_NOWAYOUT;
149 module_param(nogameport, int, 0);
150 MODULE_PARM_DESC(nogameport, "Forbid the activation of game port, default="
151 __MODULE_STRING(DEFAULT_NOGAMEPORT));
152 module_param(exclusive, int, 0);
153 MODULE_PARM_DESC(exclusive, "Watchdog exclusive device open, default="
154 __MODULE_STRING(DEFAULT_EXCLUSIVE));
155 module_param(timeout, int, 0);
156 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
157 __MODULE_STRING(DEFAULT_TIMEOUT));
158 module_param(testmode, int, 0);
159 MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
160 __MODULE_STRING(DEFAULT_TESTMODE));
161 module_param(nowayout, bool, 0);
162 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
163 __MODULE_STRING(WATCHDOG_NOWAYOUT));
167 static inline int superio_enter(void)
170 * Try to reserve REG and REG + 1 for exclusive access.
172 if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
182 static inline void superio_exit(void)
186 release_region(REG, 2);
189 static inline void superio_select(int ldn)
195 static inline int superio_inb(int reg)
201 static inline void superio_outb(int val, int reg)
207 static inline int superio_inw(int reg)
217 static inline void superio_outw(int val, int reg)
225 /* Internal function, should be called after superio_select(GPIO) */
226 static void wdt_update_timeout(void)
228 unsigned char cfg = WDT_KRST;
239 if (chip_type != IT8721_ID)
242 superio_outb(cfg, WDTCFG);
243 superio_outb(tm, WDTVALLSB);
245 superio_outb(tm>>8, WDTVALMSB);
248 static int wdt_round_time(int t)
255 /* watchdog timer handling */
257 static void wdt_keepalive(void)
259 if (test_bit(WDTS_USE_GP, &wdt_status))
262 /* The timer reloads with around 5 msec delay */
263 outb(0x55, CIR_DR(base));
264 set_bit(WDTS_KEEPALIVE, &wdt_status);
267 static int wdt_start(void)
269 int ret = superio_enter();
273 superio_select(GPIO);
274 if (test_bit(WDTS_USE_GP, &wdt_status))
275 superio_outb(WDT_GAMEPORT, WDTCTRL);
277 superio_outb(WDT_CIRINT, WDTCTRL);
278 wdt_update_timeout();
285 static int wdt_stop(void)
287 int ret = superio_enter();
291 superio_select(GPIO);
292 superio_outb(0x00, WDTCTRL);
293 superio_outb(WDT_TOV1, WDTCFG);
294 superio_outb(0x00, WDTVALLSB);
296 superio_outb(0x00, WDTVALMSB);
303 * wdt_set_timeout - set a new timeout value with watchdog ioctl
304 * @t: timeout value in seconds
306 * The hardware device has a 8 or 16 bit watchdog timer (depends on
307 * chip version) that can be configured to count seconds or minutes.
309 * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
312 static int wdt_set_timeout(int t)
314 if (t < 1 || t > max_units * 60)
318 timeout = wdt_round_time(t);
322 if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
323 int ret = superio_enter();
327 superio_select(GPIO);
328 wdt_update_timeout();
335 * wdt_get_status - determines the status supported by watchdog ioctl
336 * @status: status returned to user space
338 * The status bit of the device does not allow to distinguish
339 * between a regular system reset and a watchdog forced reset.
340 * But, in test mode it is useful, so it is supported through
341 * WDIOC_GETSTATUS watchdog ioctl. Additionally the driver
342 * reports the keepalive signal and the acception of the magic.
344 * Used within WDIOC_GETSTATUS watchdog device ioctl.
347 static int wdt_get_status(int *status)
351 int ret = superio_enter();
355 superio_select(GPIO);
356 if (superio_inb(WDTCTRL) & WDT_ZERO) {
357 superio_outb(0x00, WDTCTRL);
358 clear_bit(WDTS_TIMER_RUN, &wdt_status);
359 *status |= WDIOF_CARDRESET;
364 if (test_and_clear_bit(WDTS_KEEPALIVE, &wdt_status))
365 *status |= WDIOF_KEEPALIVEPING;
366 if (test_bit(WDTS_EXPECTED, &wdt_status))
367 *status |= WDIOF_MAGICCLOSE;
371 /* /dev/watchdog handling */
374 * wdt_open - watchdog file_operations .open
375 * @inode: inode of the device
376 * @file: file handle to the device
378 * The watchdog timer starts by opening the device.
380 * Used within the file operation of the watchdog device.
383 static int wdt_open(struct inode *inode, struct file *file)
385 if (exclusive && test_and_set_bit(WDTS_DEV_OPEN, &wdt_status))
387 if (!test_and_set_bit(WDTS_TIMER_RUN, &wdt_status)) {
389 if (nowayout && !test_and_set_bit(WDTS_LOCKED, &wdt_status))
390 __module_get(THIS_MODULE);
394 clear_bit(WDTS_LOCKED, &wdt_status);
395 clear_bit(WDTS_TIMER_RUN, &wdt_status);
396 clear_bit(WDTS_DEV_OPEN, &wdt_status);
400 return nonseekable_open(inode, file);
404 * wdt_release - watchdog file_operations .release
405 * @inode: inode of the device
406 * @file: file handle to the device
408 * Closing the watchdog device either stops the watchdog timer
409 * or in the case, that nowayout is set or the magic character
410 * wasn't written, a critical warning about an running watchdog
413 * Used within the file operation of the watchdog device.
416 static int wdt_release(struct inode *inode, struct file *file)
418 if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
419 if (test_and_clear_bit(WDTS_EXPECTED, &wdt_status)) {
420 int ret = wdt_stop();
423 * Stop failed. Just keep the watchdog alive
424 * and hope nothing bad happens.
426 set_bit(WDTS_EXPECTED, &wdt_status);
430 clear_bit(WDTS_TIMER_RUN, &wdt_status);
433 pr_crit("unexpected close, not stopping watchdog!\n");
436 clear_bit(WDTS_DEV_OPEN, &wdt_status);
441 * wdt_write - watchdog file_operations .write
442 * @file: file handle to the watchdog
443 * @buf: buffer to write
444 * @count: count of bytes
445 * @ppos: pointer to the position to write. No seeks allowed
447 * A write to a watchdog device is defined as a keepalive signal. Any
448 * write of data will do, as we don't define content meaning.
450 * Used within the file operation of the watchdog device.
453 static ssize_t wdt_write(struct file *file, const char __user *buf,
454 size_t count, loff_t *ppos)
457 clear_bit(WDTS_EXPECTED, &wdt_status);
463 /* note: just in case someone wrote the magic character long ago */
464 for (ofs = 0; ofs != count; ofs++) {
466 if (get_user(c, buf + ofs))
469 set_bit(WDTS_EXPECTED, &wdt_status);
475 static const struct watchdog_info ident = {
476 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
477 .firmware_version = 1,
478 .identity = WATCHDOG_NAME,
482 * wdt_ioctl - watchdog file_operations .unlocked_ioctl
483 * @file: file handle to the device
484 * @cmd: watchdog command
485 * @arg: argument pointer
487 * The watchdog API defines a common set of functions for all watchdogs
488 * according to their available features.
490 * Used within the file operation of the watchdog device.
493 static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
495 int rc = 0, status, new_options, new_timeout;
497 struct watchdog_info __user *ident;
501 uarg.i = (int __user *)arg;
504 case WDIOC_GETSUPPORT:
505 return copy_to_user(uarg.ident,
506 &ident, sizeof(ident)) ? -EFAULT : 0;
508 case WDIOC_GETSTATUS:
509 rc = wdt_get_status(&status);
512 return put_user(status, uarg.i);
514 case WDIOC_GETBOOTSTATUS:
515 return put_user(0, uarg.i);
517 case WDIOC_KEEPALIVE:
521 case WDIOC_SETOPTIONS:
522 if (get_user(new_options, uarg.i))
525 switch (new_options) {
526 case WDIOS_DISABLECARD:
527 if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
532 clear_bit(WDTS_TIMER_RUN, &wdt_status);
535 case WDIOS_ENABLECARD:
536 if (!test_and_set_bit(WDTS_TIMER_RUN, &wdt_status)) {
539 clear_bit(WDTS_TIMER_RUN, &wdt_status);
549 case WDIOC_SETTIMEOUT:
550 if (get_user(new_timeout, uarg.i))
552 rc = wdt_set_timeout(new_timeout);
553 case WDIOC_GETTIMEOUT:
554 if (put_user(timeout, uarg.i))
563 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
566 if (code == SYS_DOWN || code == SYS_HALT)
571 static const struct file_operations wdt_fops = {
572 .owner = THIS_MODULE,
575 .unlocked_ioctl = wdt_ioctl,
577 .release = wdt_release,
580 static struct miscdevice wdt_miscdev = {
581 .minor = WATCHDOG_MINOR,
586 static struct notifier_block wdt_notifier = {
587 .notifier_call = wdt_notify_sys,
590 static int __init it87_wdt_init(void)
593 int try_gameport = !nogameport;
595 int gp_rreq_fail = 0;
599 rc = superio_enter();
603 chip_type = superio_inw(CHIPID);
604 chip_rev = superio_inb(CHIPREV) & 0x0f;
612 max_units = (chip_rev < 8) ? 255 : 65535;
626 pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
627 chip_type, chip_rev);
630 pr_err("no device\n");
633 pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
634 chip_type, chip_rev);
638 rc = superio_enter();
642 superio_select(GPIO);
643 superio_outb(WDT_TOV1, WDTCFG);
644 superio_outb(0x00, WDTCTRL);
646 /* First try to get Gameport support */
648 superio_select(GAMEPORT);
649 base = superio_inw(BASEREG);
651 base = GP_BASE_DEFAULT;
652 superio_outw(base, BASEREG);
654 gpact = superio_inb(ACTREG);
655 superio_outb(0x01, ACTREG);
656 if (request_region(base, 1, WATCHDOG_NAME))
657 set_bit(WDTS_USE_GP, &wdt_status);
662 /* If we haven't Gameport support, try to get CIR support */
663 if (!test_bit(WDTS_USE_GP, &wdt_status)) {
664 if (!request_region(CIR_BASE, 8, WATCHDOG_NAME)) {
666 pr_err("I/O Address 0x%04x and 0x%04x already in use\n",
669 pr_err("I/O Address 0x%04x already in use\n",
677 superio_outw(base, BASEREG);
678 superio_outb(0x00, CIR_ILS);
679 ciract = superio_inb(ACTREG);
680 superio_outb(0x01, ACTREG);
682 superio_select(GAMEPORT);
683 superio_outb(gpact, ACTREG);
687 if (timeout < 1 || timeout > max_units * 60) {
688 timeout = DEFAULT_TIMEOUT;
689 pr_warn("Timeout value out of range, use default %d sec\n",
693 if (timeout > max_units)
694 timeout = wdt_round_time(timeout);
696 rc = register_reboot_notifier(&wdt_notifier);
698 pr_err("Cannot register reboot notifier (err=%d)\n", rc);
702 rc = misc_register(&wdt_miscdev);
704 pr_err("Cannot register miscdev on minor=%d (err=%d)\n",
705 wdt_miscdev.minor, rc);
709 /* Initialize CIR to use it as keepalive source */
710 if (!test_bit(WDTS_USE_GP, &wdt_status)) {
711 outb(0x00, CIR_RCR(base));
712 outb(0xc0, CIR_TCR1(base));
713 outb(0x5c, CIR_TCR2(base));
714 outb(0x10, CIR_IER(base));
715 outb(0x00, CIR_BDHR(base));
716 outb(0x01, CIR_BDLR(base));
717 outb(0x09, CIR_IER(base));
720 pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d exclusive=%d nogameport=%d)\n",
721 chip_type, chip_rev, timeout,
722 nowayout, testmode, exclusive, nogameport);
728 unregister_reboot_notifier(&wdt_notifier);
730 release_region(base, test_bit(WDTS_USE_GP, &wdt_status) ? 1 : 8);
731 if (!test_bit(WDTS_USE_GP, &wdt_status)) {
733 superio_outb(ciract, ACTREG);
737 superio_select(GAMEPORT);
738 superio_outb(gpact, ACTREG);
745 static void __exit it87_wdt_exit(void)
747 if (superio_enter() == 0) {
748 superio_select(GPIO);
749 superio_outb(0x00, WDTCTRL);
750 superio_outb(0x00, WDTCFG);
751 superio_outb(0x00, WDTVALLSB);
753 superio_outb(0x00, WDTVALMSB);
754 if (test_bit(WDTS_USE_GP, &wdt_status)) {
755 superio_select(GAMEPORT);
756 superio_outb(gpact, ACTREG);
759 superio_outb(ciract, ACTREG);
764 misc_deregister(&wdt_miscdev);
765 unregister_reboot_notifier(&wdt_notifier);
766 release_region(base, test_bit(WDTS_USE_GP, &wdt_status) ? 1 : 8);
769 module_init(it87_wdt_init);
770 module_exit(it87_wdt_exit);
772 MODULE_AUTHOR("Oliver Schuster");
773 MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
774 MODULE_LICENSE("GPL");
775 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);