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 and IT8726.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version
20 * 2 of the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
39 #include <linux/miscdevice.h>
40 #include <linux/init.h>
41 #include <linux/ioport.h>
42 #include <linux/watchdog.h>
43 #include <linux/notifier.h>
44 #include <linux/reboot.h>
45 #include <linux/uaccess.h>
48 #include <asm/system.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 */
89 /* GPIO Configuration Registers LDN=0x07 */
92 #define WDTVALLSB 0x73
93 #define WDTVALMSB 0x74
95 /* GPIO Bits WDTCTRL */
96 #define WDT_CIRINT 0x80
97 #define WDT_MOUSEINT 0x40
98 #define WDT_KYBINT 0x20
99 #define WDT_GAMEPORT 0x10 /* not in it8718, it8720, it8721 */
100 #define WDT_FORCE 0x02
101 #define WDT_ZERO 0x01
103 /* GPIO Bits WDTCFG */
104 #define WDT_TOV1 0x80
105 #define WDT_KRST 0x40
106 #define WDT_TOVE 0x20
107 #define WDT_PWROK 0x10 /* not in it8721 */
108 #define WDT_INT_MASK 0x0f
110 /* CIR Configuration Register LDN=0x0a */
113 /* The default Base address is not always available, we use this */
114 #define CIR_BASE 0x0208
117 #define CIR_DR(b) (b)
118 #define CIR_IER(b) (b + 1)
119 #define CIR_RCR(b) (b + 2)
120 #define CIR_TCR1(b) (b + 3)
121 #define CIR_TCR2(b) (b + 4)
122 #define CIR_TSR(b) (b + 5)
123 #define CIR_RSR(b) (b + 6)
124 #define CIR_BDLR(b) (b + 5)
125 #define CIR_BDHR(b) (b + 6)
126 #define CIR_IIR(b) (b + 7)
128 /* Default Base address of Game port */
129 #define GP_BASE_DEFAULT 0x0201
132 #define WDTS_TIMER_RUN 0
133 #define WDTS_DEV_OPEN 1
134 #define WDTS_KEEPALIVE 2
135 #define WDTS_LOCKED 3
136 #define WDTS_USE_GP 4
137 #define WDTS_EXPECTED 5
139 static unsigned int base, gpact, ciract, max_units, chip_type;
140 static unsigned long wdt_status;
142 static int nogameport = DEFAULT_NOGAMEPORT;
143 static int exclusive = DEFAULT_EXCLUSIVE;
144 static int timeout = DEFAULT_TIMEOUT;
145 static int testmode = DEFAULT_TESTMODE;
146 static bool nowayout = DEFAULT_NOWAYOUT;
148 module_param(nogameport, int, 0);
149 MODULE_PARM_DESC(nogameport, "Forbid the activation of game port, default="
150 __MODULE_STRING(DEFAULT_NOGAMEPORT));
151 module_param(exclusive, int, 0);
152 MODULE_PARM_DESC(exclusive, "Watchdog exclusive device open, default="
153 __MODULE_STRING(DEFAULT_EXCLUSIVE));
154 module_param(timeout, int, 0);
155 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, default="
156 __MODULE_STRING(DEFAULT_TIMEOUT));
157 module_param(testmode, int, 0);
158 MODULE_PARM_DESC(testmode, "Watchdog test mode (1 = no reboot), default="
159 __MODULE_STRING(DEFAULT_TESTMODE));
160 module_param(nowayout, bool, 0);
161 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started, default="
162 __MODULE_STRING(WATCHDOG_NOWAYOUT));
166 static inline int superio_enter(void)
169 * Try to reserve REG and REG + 1 for exclusive access.
171 if (!request_muxed_region(REG, 2, WATCHDOG_NAME))
181 static inline void superio_exit(void)
185 release_region(REG, 2);
188 static inline void superio_select(int ldn)
194 static inline int superio_inb(int reg)
200 static inline void superio_outb(int val, int reg)
206 static inline int superio_inw(int reg)
216 static inline void superio_outw(int val, int reg)
224 /* Internal function, should be called after superio_select(GPIO) */
225 static void wdt_update_timeout(void)
227 unsigned char cfg = WDT_KRST;
238 if (chip_type != IT8721_ID)
241 superio_outb(cfg, WDTCFG);
242 superio_outb(tm, WDTVALLSB);
244 superio_outb(tm>>8, WDTVALMSB);
247 static int wdt_round_time(int t)
254 /* watchdog timer handling */
256 static void wdt_keepalive(void)
258 if (test_bit(WDTS_USE_GP, &wdt_status))
261 /* The timer reloads with around 5 msec delay */
262 outb(0x55, CIR_DR(base));
263 set_bit(WDTS_KEEPALIVE, &wdt_status);
266 static int wdt_start(void)
268 int ret = superio_enter();
272 superio_select(GPIO);
273 if (test_bit(WDTS_USE_GP, &wdt_status))
274 superio_outb(WDT_GAMEPORT, WDTCTRL);
276 superio_outb(WDT_CIRINT, WDTCTRL);
277 wdt_update_timeout();
284 static int wdt_stop(void)
286 int ret = superio_enter();
290 superio_select(GPIO);
291 superio_outb(0x00, WDTCTRL);
292 superio_outb(WDT_TOV1, WDTCFG);
293 superio_outb(0x00, WDTVALLSB);
295 superio_outb(0x00, WDTVALMSB);
302 * wdt_set_timeout - set a new timeout value with watchdog ioctl
303 * @t: timeout value in seconds
305 * The hardware device has a 8 or 16 bit watchdog timer (depends on
306 * chip version) that can be configured to count seconds or minutes.
308 * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
311 static int wdt_set_timeout(int t)
313 if (t < 1 || t > max_units * 60)
317 timeout = wdt_round_time(t);
321 if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
322 int ret = superio_enter();
326 superio_select(GPIO);
327 wdt_update_timeout();
334 * wdt_get_status - determines the status supported by watchdog ioctl
335 * @status: status returned to user space
337 * The status bit of the device does not allow to distinguish
338 * between a regular system reset and a watchdog forced reset.
339 * But, in test mode it is useful, so it is supported through
340 * WDIOC_GETSTATUS watchdog ioctl. Additionally the driver
341 * reports the keepalive signal and the acception of the magic.
343 * Used within WDIOC_GETSTATUS watchdog device ioctl.
346 static int wdt_get_status(int *status)
350 int ret = superio_enter();
354 superio_select(GPIO);
355 if (superio_inb(WDTCTRL) & WDT_ZERO) {
356 superio_outb(0x00, WDTCTRL);
357 clear_bit(WDTS_TIMER_RUN, &wdt_status);
358 *status |= WDIOF_CARDRESET;
363 if (test_and_clear_bit(WDTS_KEEPALIVE, &wdt_status))
364 *status |= WDIOF_KEEPALIVEPING;
365 if (test_bit(WDTS_EXPECTED, &wdt_status))
366 *status |= WDIOF_MAGICCLOSE;
370 /* /dev/watchdog handling */
373 * wdt_open - watchdog file_operations .open
374 * @inode: inode of the device
375 * @file: file handle to the device
377 * The watchdog timer starts by opening the device.
379 * Used within the file operation of the watchdog device.
382 static int wdt_open(struct inode *inode, struct file *file)
384 if (exclusive && test_and_set_bit(WDTS_DEV_OPEN, &wdt_status))
386 if (!test_and_set_bit(WDTS_TIMER_RUN, &wdt_status)) {
388 if (nowayout && !test_and_set_bit(WDTS_LOCKED, &wdt_status))
389 __module_get(THIS_MODULE);
393 clear_bit(WDTS_LOCKED, &wdt_status);
394 clear_bit(WDTS_TIMER_RUN, &wdt_status);
395 clear_bit(WDTS_DEV_OPEN, &wdt_status);
399 return nonseekable_open(inode, file);
403 * wdt_release - watchdog file_operations .release
404 * @inode: inode of the device
405 * @file: file handle to the device
407 * Closing the watchdog device either stops the watchdog timer
408 * or in the case, that nowayout is set or the magic character
409 * wasn't written, a critical warning about an running watchdog
412 * Used within the file operation of the watchdog device.
415 static int wdt_release(struct inode *inode, struct file *file)
417 if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
418 if (test_and_clear_bit(WDTS_EXPECTED, &wdt_status)) {
419 int ret = wdt_stop();
422 * Stop failed. Just keep the watchdog alive
423 * and hope nothing bad happens.
425 set_bit(WDTS_EXPECTED, &wdt_status);
429 clear_bit(WDTS_TIMER_RUN, &wdt_status);
432 pr_crit("unexpected close, not stopping watchdog!\n");
435 clear_bit(WDTS_DEV_OPEN, &wdt_status);
440 * wdt_write - watchdog file_operations .write
441 * @file: file handle to the watchdog
442 * @buf: buffer to write
443 * @count: count of bytes
444 * @ppos: pointer to the position to write. No seeks allowed
446 * A write to a watchdog device is defined as a keepalive signal. Any
447 * write of data will do, as we don't define content meaning.
449 * Used within the file operation of the watchdog device.
452 static ssize_t wdt_write(struct file *file, const char __user *buf,
453 size_t count, loff_t *ppos)
456 clear_bit(WDTS_EXPECTED, &wdt_status);
462 /* note: just in case someone wrote the magic character long ago */
463 for (ofs = 0; ofs != count; ofs++) {
465 if (get_user(c, buf + ofs))
468 set_bit(WDTS_EXPECTED, &wdt_status);
474 static const struct watchdog_info ident = {
475 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
476 .firmware_version = 1,
477 .identity = WATCHDOG_NAME,
481 * wdt_ioctl - watchdog file_operations .unlocked_ioctl
482 * @file: file handle to the device
483 * @cmd: watchdog command
484 * @arg: argument pointer
486 * The watchdog API defines a common set of functions for all watchdogs
487 * according to their available features.
489 * Used within the file operation of the watchdog device.
492 static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
494 int rc = 0, status, new_options, new_timeout;
496 struct watchdog_info __user *ident;
500 uarg.i = (int __user *)arg;
503 case WDIOC_GETSUPPORT:
504 return copy_to_user(uarg.ident,
505 &ident, sizeof(ident)) ? -EFAULT : 0;
507 case WDIOC_GETSTATUS:
508 rc = wdt_get_status(&status);
511 return put_user(status, uarg.i);
513 case WDIOC_GETBOOTSTATUS:
514 return put_user(0, uarg.i);
516 case WDIOC_KEEPALIVE:
520 case WDIOC_SETOPTIONS:
521 if (get_user(new_options, uarg.i))
524 switch (new_options) {
525 case WDIOS_DISABLECARD:
526 if (test_bit(WDTS_TIMER_RUN, &wdt_status)) {
531 clear_bit(WDTS_TIMER_RUN, &wdt_status);
534 case WDIOS_ENABLECARD:
535 if (!test_and_set_bit(WDTS_TIMER_RUN, &wdt_status)) {
538 clear_bit(WDTS_TIMER_RUN, &wdt_status);
548 case WDIOC_SETTIMEOUT:
549 if (get_user(new_timeout, uarg.i))
551 rc = wdt_set_timeout(new_timeout);
552 case WDIOC_GETTIMEOUT:
553 if (put_user(timeout, uarg.i))
562 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
565 if (code == SYS_DOWN || code == SYS_HALT)
570 static const struct file_operations wdt_fops = {
571 .owner = THIS_MODULE,
574 .unlocked_ioctl = wdt_ioctl,
576 .release = wdt_release,
579 static struct miscdevice wdt_miscdev = {
580 .minor = WATCHDOG_MINOR,
585 static struct notifier_block wdt_notifier = {
586 .notifier_call = wdt_notify_sys,
589 static int __init it87_wdt_init(void)
592 int try_gameport = !nogameport;
594 int gp_rreq_fail = 0;
598 rc = superio_enter();
602 chip_type = superio_inw(CHIPID);
603 chip_rev = superio_inb(CHIPREV) & 0x0f;
611 max_units = (chip_rev < 8) ? 255 : 65535;
624 pr_err("Unsupported Chip found, Chip %04x Revision %02x\n",
625 chip_type, chip_rev);
628 pr_err("no device\n");
631 pr_err("Unknown Chip found, Chip %04x Revision %04x\n",
632 chip_type, chip_rev);
636 rc = superio_enter();
640 superio_select(GPIO);
641 superio_outb(WDT_TOV1, WDTCFG);
642 superio_outb(0x00, WDTCTRL);
644 /* First try to get Gameport support */
646 superio_select(GAMEPORT);
647 base = superio_inw(BASEREG);
649 base = GP_BASE_DEFAULT;
650 superio_outw(base, BASEREG);
652 gpact = superio_inb(ACTREG);
653 superio_outb(0x01, ACTREG);
654 if (request_region(base, 1, WATCHDOG_NAME))
655 set_bit(WDTS_USE_GP, &wdt_status);
660 /* If we haven't Gameport support, try to get CIR support */
661 if (!test_bit(WDTS_USE_GP, &wdt_status)) {
662 if (!request_region(CIR_BASE, 8, WATCHDOG_NAME)) {
664 pr_err("I/O Address 0x%04x and 0x%04x already in use\n",
667 pr_err("I/O Address 0x%04x already in use\n",
675 superio_outw(base, BASEREG);
676 superio_outb(0x00, CIR_ILS);
677 ciract = superio_inb(ACTREG);
678 superio_outb(0x01, ACTREG);
680 superio_select(GAMEPORT);
681 superio_outb(gpact, ACTREG);
685 if (timeout < 1 || timeout > max_units * 60) {
686 timeout = DEFAULT_TIMEOUT;
687 pr_warn("Timeout value out of range, use default %d sec\n",
691 if (timeout > max_units)
692 timeout = wdt_round_time(timeout);
694 rc = register_reboot_notifier(&wdt_notifier);
696 pr_err("Cannot register reboot notifier (err=%d)\n", rc);
700 rc = misc_register(&wdt_miscdev);
702 pr_err("Cannot register miscdev on minor=%d (err=%d)\n",
703 wdt_miscdev.minor, rc);
707 /* Initialize CIR to use it as keepalive source */
708 if (!test_bit(WDTS_USE_GP, &wdt_status)) {
709 outb(0x00, CIR_RCR(base));
710 outb(0xc0, CIR_TCR1(base));
711 outb(0x5c, CIR_TCR2(base));
712 outb(0x10, CIR_IER(base));
713 outb(0x00, CIR_BDHR(base));
714 outb(0x01, CIR_BDLR(base));
715 outb(0x09, CIR_IER(base));
718 pr_info("Chip IT%04x revision %d initialized. timeout=%d sec (nowayout=%d testmode=%d exclusive=%d nogameport=%d)\n",
719 chip_type, chip_rev, timeout,
720 nowayout, testmode, exclusive, nogameport);
726 unregister_reboot_notifier(&wdt_notifier);
728 release_region(base, test_bit(WDTS_USE_GP, &wdt_status) ? 1 : 8);
729 if (!test_bit(WDTS_USE_GP, &wdt_status)) {
731 superio_outb(ciract, ACTREG);
735 superio_select(GAMEPORT);
736 superio_outb(gpact, ACTREG);
743 static void __exit it87_wdt_exit(void)
745 if (superio_enter() == 0) {
746 superio_select(GPIO);
747 superio_outb(0x00, WDTCTRL);
748 superio_outb(0x00, WDTCFG);
749 superio_outb(0x00, WDTVALLSB);
751 superio_outb(0x00, WDTVALMSB);
752 if (test_bit(WDTS_USE_GP, &wdt_status)) {
753 superio_select(GAMEPORT);
754 superio_outb(gpact, ACTREG);
757 superio_outb(ciract, ACTREG);
762 misc_deregister(&wdt_miscdev);
763 unregister_reboot_notifier(&wdt_notifier);
764 release_region(base, test_bit(WDTS_USE_GP, &wdt_status) ? 1 : 8);
767 module_init(it87_wdt_init);
768 module_exit(it87_wdt_exit);
770 MODULE_AUTHOR("Oliver Schuster");
771 MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
772 MODULE_LICENSE("GPL");
773 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);