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 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/types.h>
35 #include <linux/kernel.h>
37 #include <linux/miscdevice.h>
38 #include <linux/init.h>
39 #include <linux/ioport.h>
40 #include <linux/watchdog.h>
41 #include <linux/notifier.h>
42 #include <linux/reboot.h>
43 #include <linux/uaccess.h>
46 #include <asm/system.h>
48 #define WATCHDOG_VERSION "1.14"
49 #define WATCHDOG_NAME "IT87 WDT"
50 #define PFX WATCHDOG_NAME ": "
51 #define DRIVER_VERSION WATCHDOG_NAME " driver, v" WATCHDOG_VERSION "\n"
54 /* Defaults for Module Parameter */
55 #define DEFAULT_NOGAMEPORT 0
56 #define DEFAULT_EXCLUSIVE 1
57 #define DEFAULT_TIMEOUT 60
58 #define DEFAULT_TESTMODE 0
59 #define DEFAULT_NOWAYOUT WATCHDOG_NOWAYOUT
65 /* Logical device Numbers LDN */
70 /* Configuration Registers and Functions */
78 #define NO_DEV_ID 0xffff
79 #define IT8702_ID 0x8702
80 #define IT8705_ID 0x8705
81 #define IT8712_ID 0x8712
82 #define IT8716_ID 0x8716
83 #define IT8718_ID 0x8718
84 #define IT8720_ID 0x8720
85 #define IT8721_ID 0x8721
86 #define IT8726_ID 0x8726 /* the data sheet suggest wrongly 0x8716 */
88 /* GPIO Configuration Registers LDN=0x07 */
91 #define WDTVALLSB 0x73
92 #define WDTVALMSB 0x74
94 /* GPIO Bits WDTCTRL */
95 #define WDT_CIRINT 0x80
96 #define WDT_MOUSEINT 0x40
97 #define WDT_KYBINT 0x20
98 #define WDT_GAMEPORT 0x10 /* not in it8718, it8720, it8721 */
99 #define WDT_FORCE 0x02
100 #define WDT_ZERO 0x01
102 /* GPIO Bits WDTCFG */
103 #define WDT_TOV1 0x80
104 #define WDT_KRST 0x40
105 #define WDT_TOVE 0x20
106 #define WDT_PWROK 0x10 /* not in it8721 */
107 #define WDT_INT_MASK 0x0f
109 /* CIR Configuration Register LDN=0x0a */
112 /* The default Base address is not always available, we use this */
113 #define CIR_BASE 0x0208
116 #define CIR_DR(b) (b)
117 #define CIR_IER(b) (b + 1)
118 #define CIR_RCR(b) (b + 2)
119 #define CIR_TCR1(b) (b + 3)
120 #define CIR_TCR2(b) (b + 4)
121 #define CIR_TSR(b) (b + 5)
122 #define CIR_RSR(b) (b + 6)
123 #define CIR_BDLR(b) (b + 5)
124 #define CIR_BDHR(b) (b + 6)
125 #define CIR_IIR(b) (b + 7)
127 /* Default Base address of Game port */
128 #define GP_BASE_DEFAULT 0x0201
131 #define WDTS_TIMER_RUN 0
132 #define WDTS_DEV_OPEN 1
133 #define WDTS_KEEPALIVE 2
134 #define WDTS_LOCKED 3
135 #define WDTS_USE_GP 4
136 #define WDTS_EXPECTED 5
138 static unsigned int base
, gpact
, ciract
, max_units
, chip_type
;
139 static unsigned long wdt_status
;
140 static DEFINE_SPINLOCK(spinlock
);
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 int 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
, int, 0);
161 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started, default="
162 __MODULE_STRING(WATCHDOG_NOWAYOUT
));
166 static inline void superio_enter(void)
174 static inline void superio_exit(void)
180 static inline void superio_select(int ldn
)
186 static inline int superio_inb(int reg
)
192 static inline void superio_outb(int val
, int reg
)
198 static inline int superio_inw(int reg
)
208 static inline void superio_outw(int val
, int reg
)
216 /* Internal function, should be called after superio_select(GPIO) */
217 static void wdt_update_timeout(void)
219 unsigned char cfg
= WDT_KRST
;
230 if (chip_type
!= IT8721_ID
)
233 superio_outb(cfg
, WDTCFG
);
234 superio_outb(tm
, WDTVALLSB
);
236 superio_outb(tm
>>8, WDTVALMSB
);
239 static int wdt_round_time(int t
)
246 /* watchdog timer handling */
248 static void wdt_keepalive(void)
250 if (test_bit(WDTS_USE_GP
, &wdt_status
))
253 /* The timer reloads with around 5 msec delay */
254 outb(0x55, CIR_DR(base
));
255 set_bit(WDTS_KEEPALIVE
, &wdt_status
);
258 static void wdt_start(void)
262 spin_lock_irqsave(&spinlock
, flags
);
265 superio_select(GPIO
);
266 if (test_bit(WDTS_USE_GP
, &wdt_status
))
267 superio_outb(WDT_GAMEPORT
, WDTCTRL
);
269 superio_outb(WDT_CIRINT
, WDTCTRL
);
270 wdt_update_timeout();
273 spin_unlock_irqrestore(&spinlock
, flags
);
276 static void wdt_stop(void)
280 spin_lock_irqsave(&spinlock
, flags
);
283 superio_select(GPIO
);
284 superio_outb(0x00, WDTCTRL
);
285 superio_outb(WDT_TOV1
, WDTCFG
);
286 superio_outb(0x00, WDTVALLSB
);
288 superio_outb(0x00, WDTVALMSB
);
291 spin_unlock_irqrestore(&spinlock
, flags
);
295 * wdt_set_timeout - set a new timeout value with watchdog ioctl
296 * @t: timeout value in seconds
298 * The hardware device has a 8 or 16 bit watchdog timer (depends on
299 * chip version) that can be configured to count seconds or minutes.
301 * Used within WDIOC_SETTIMEOUT watchdog device ioctl.
304 static int wdt_set_timeout(int t
)
308 if (t
< 1 || t
> max_units
* 60)
312 timeout
= wdt_round_time(t
);
316 spin_lock_irqsave(&spinlock
, flags
);
317 if (test_bit(WDTS_TIMER_RUN
, &wdt_status
)) {
319 superio_select(GPIO
);
320 wdt_update_timeout();
323 spin_unlock_irqrestore(&spinlock
, flags
);
328 * wdt_get_status - determines the status supported by watchdog ioctl
329 * @status: status returned to user space
331 * The status bit of the device does not allow to distinguish
332 * between a regular system reset and a watchdog forced reset.
333 * But, in test mode it is useful, so it is supported through
334 * WDIOC_GETSTATUS watchdog ioctl. Additionally the driver
335 * reports the keepalive signal and the acception of the magic.
337 * Used within WDIOC_GETSTATUS watchdog device ioctl.
340 static int wdt_get_status(int *status
)
346 spin_lock_irqsave(&spinlock
, flags
);
348 superio_select(GPIO
);
349 if (superio_inb(WDTCTRL
) & WDT_ZERO
) {
350 superio_outb(0x00, WDTCTRL
);
351 clear_bit(WDTS_TIMER_RUN
, &wdt_status
);
352 *status
|= WDIOF_CARDRESET
;
356 spin_unlock_irqrestore(&spinlock
, flags
);
358 if (test_and_clear_bit(WDTS_KEEPALIVE
, &wdt_status
))
359 *status
|= WDIOF_KEEPALIVEPING
;
360 if (test_bit(WDTS_EXPECTED
, &wdt_status
))
361 *status
|= WDIOF_MAGICCLOSE
;
365 /* /dev/watchdog handling */
368 * wdt_open - watchdog file_operations .open
369 * @inode: inode of the device
370 * @file: file handle to the device
372 * The watchdog timer starts by opening the device.
374 * Used within the file operation of the watchdog device.
377 static int wdt_open(struct inode
*inode
, struct file
*file
)
379 if (exclusive
&& test_and_set_bit(WDTS_DEV_OPEN
, &wdt_status
))
381 if (!test_and_set_bit(WDTS_TIMER_RUN
, &wdt_status
)) {
382 if (nowayout
&& !test_and_set_bit(WDTS_LOCKED
, &wdt_status
))
383 __module_get(THIS_MODULE
);
386 return nonseekable_open(inode
, file
);
390 * wdt_release - watchdog file_operations .release
391 * @inode: inode of the device
392 * @file: file handle to the device
394 * Closing the watchdog device either stops the watchdog timer
395 * or in the case, that nowayout is set or the magic character
396 * wasn't written, a critical warning about an running watchdog
399 * Used within the file operation of the watchdog device.
402 static int wdt_release(struct inode
*inode
, struct file
*file
)
404 if (test_bit(WDTS_TIMER_RUN
, &wdt_status
)) {
405 if (test_and_clear_bit(WDTS_EXPECTED
, &wdt_status
)) {
407 clear_bit(WDTS_TIMER_RUN
, &wdt_status
);
411 "unexpected close, not stopping watchdog!\n");
414 clear_bit(WDTS_DEV_OPEN
, &wdt_status
);
419 * wdt_write - watchdog file_operations .write
420 * @file: file handle to the watchdog
421 * @buf: buffer to write
422 * @count: count of bytes
423 * @ppos: pointer to the position to write. No seeks allowed
425 * A write to a watchdog device is defined as a keepalive signal. Any
426 * write of data will do, as we don't define content meaning.
428 * Used within the file operation of the watchdog device.
431 static ssize_t
wdt_write(struct file
*file
, const char __user
*buf
,
432 size_t count
, loff_t
*ppos
)
435 clear_bit(WDTS_EXPECTED
, &wdt_status
);
441 /* note: just in case someone wrote the magic character long ago */
442 for (ofs
= 0; ofs
!= count
; ofs
++) {
444 if (get_user(c
, buf
+ ofs
))
447 set_bit(WDTS_EXPECTED
, &wdt_status
);
453 static const struct watchdog_info ident
= {
454 .options
= WDIOF_SETTIMEOUT
| WDIOF_MAGICCLOSE
| WDIOF_KEEPALIVEPING
,
455 .firmware_version
= 1,
456 .identity
= WATCHDOG_NAME
,
460 * wdt_ioctl - watchdog file_operations .unlocked_ioctl
461 * @file: file handle to the device
462 * @cmd: watchdog command
463 * @arg: argument pointer
465 * The watchdog API defines a common set of functions for all watchdogs
466 * according to their available features.
468 * Used within the file operation of the watchdog device.
471 static long wdt_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
473 int rc
= 0, status
, new_options
, new_timeout
;
475 struct watchdog_info __user
*ident
;
479 uarg
.i
= (int __user
*)arg
;
482 case WDIOC_GETSUPPORT
:
483 return copy_to_user(uarg
.ident
,
484 &ident
, sizeof(ident
)) ? -EFAULT
: 0;
486 case WDIOC_GETSTATUS
:
487 wdt_get_status(&status
);
488 return put_user(status
, uarg
.i
);
490 case WDIOC_GETBOOTSTATUS
:
491 return put_user(0, uarg
.i
);
493 case WDIOC_KEEPALIVE
:
497 case WDIOC_SETOPTIONS
:
498 if (get_user(new_options
, uarg
.i
))
501 switch (new_options
) {
502 case WDIOS_DISABLECARD
:
503 if (test_bit(WDTS_TIMER_RUN
, &wdt_status
))
505 clear_bit(WDTS_TIMER_RUN
, &wdt_status
);
508 case WDIOS_ENABLECARD
:
509 if (!test_and_set_bit(WDTS_TIMER_RUN
, &wdt_status
))
517 case WDIOC_SETTIMEOUT
:
518 if (get_user(new_timeout
, uarg
.i
))
520 rc
= wdt_set_timeout(new_timeout
);
521 case WDIOC_GETTIMEOUT
:
522 if (put_user(timeout
, uarg
.i
))
531 static int wdt_notify_sys(struct notifier_block
*this, unsigned long code
,
534 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
539 static const struct file_operations wdt_fops
= {
540 .owner
= THIS_MODULE
,
543 .unlocked_ioctl
= wdt_ioctl
,
545 .release
= wdt_release
,
548 static struct miscdevice wdt_miscdev
= {
549 .minor
= WATCHDOG_MINOR
,
554 static struct notifier_block wdt_notifier
= {
555 .notifier_call
= wdt_notify_sys
,
558 static int __init
it87_wdt_init(void)
561 int try_gameport
= !nogameport
;
567 spin_lock_irqsave(&spinlock
, flags
);
569 chip_type
= superio_inw(CHIPID
);
570 chip_rev
= superio_inb(CHIPREV
) & 0x0f;
572 spin_unlock_irqrestore(&spinlock
, flags
);
579 max_units
= (chip_rev
< 8) ? 255 : 65535;
593 "Unsupported Chip found, Chip %04x Revision %02x\n",
594 chip_type
, chip_rev
);
597 printk(KERN_ERR PFX
"no device\n");
601 "Unknown Chip found, Chip %04x Revision %04x\n",
602 chip_type
, chip_rev
);
606 spin_lock_irqsave(&spinlock
, flags
);
609 superio_select(GPIO
);
610 superio_outb(WDT_TOV1
, WDTCFG
);
611 superio_outb(0x00, WDTCTRL
);
613 /* First try to get Gameport support */
615 superio_select(GAMEPORT
);
616 base
= superio_inw(BASEREG
);
618 base
= GP_BASE_DEFAULT
;
619 superio_outw(base
, BASEREG
);
621 gpact
= superio_inb(ACTREG
);
622 superio_outb(0x01, ACTREG
);
624 spin_unlock_irqrestore(&spinlock
, flags
);
625 if (request_region(base
, 1, WATCHDOG_NAME
))
626 set_bit(WDTS_USE_GP
, &wdt_status
);
631 spin_unlock_irqrestore(&spinlock
, flags
);
634 /* If we haven't Gameport support, try to get CIR support */
635 if (!test_bit(WDTS_USE_GP
, &wdt_status
)) {
636 if (!request_region(CIR_BASE
, 8, WATCHDOG_NAME
)) {
639 "I/O Address 0x%04x and 0x%04x"
640 " already in use\n", base
, CIR_BASE
);
643 "I/O Address 0x%04x already in use\n",
649 spin_lock_irqsave(&spinlock
, flags
);
653 superio_outw(base
, BASEREG
);
654 superio_outb(0x00, CIR_ILS
);
655 ciract
= superio_inb(ACTREG
);
656 superio_outb(0x01, ACTREG
);
658 superio_select(GAMEPORT
);
659 superio_outb(gpact
, ACTREG
);
663 spin_unlock_irqrestore(&spinlock
, flags
);
666 if (timeout
< 1 || timeout
> max_units
* 60) {
667 timeout
= DEFAULT_TIMEOUT
;
668 printk(KERN_WARNING PFX
669 "Timeout value out of range, use default %d sec\n",
673 if (timeout
> max_units
)
674 timeout
= wdt_round_time(timeout
);
676 rc
= register_reboot_notifier(&wdt_notifier
);
679 "Cannot register reboot notifier (err=%d)\n", rc
);
683 rc
= misc_register(&wdt_miscdev
);
686 "Cannot register miscdev on minor=%d (err=%d)\n",
687 wdt_miscdev
.minor
, rc
);
691 /* Initialize CIR to use it as keepalive source */
692 if (!test_bit(WDTS_USE_GP
, &wdt_status
)) {
693 outb(0x00, CIR_RCR(base
));
694 outb(0xc0, CIR_TCR1(base
));
695 outb(0x5c, CIR_TCR2(base
));
696 outb(0x10, CIR_IER(base
));
697 outb(0x00, CIR_BDHR(base
));
698 outb(0x01, CIR_BDLR(base
));
699 outb(0x09, CIR_IER(base
));
702 printk(KERN_INFO PFX
"Chip IT%04x revision %d initialized. "
703 "timeout=%d sec (nowayout=%d testmode=%d exclusive=%d "
704 "nogameport=%d)\n", chip_type
, chip_rev
, timeout
,
705 nowayout
, testmode
, exclusive
, nogameport
);
710 unregister_reboot_notifier(&wdt_notifier
);
712 release_region(base
, test_bit(WDTS_USE_GP
, &wdt_status
) ? 1 : 8);
713 if (!test_bit(WDTS_USE_GP
, &wdt_status
)) {
714 spin_lock_irqsave(&spinlock
, flags
);
717 superio_outb(ciract
, ACTREG
);
719 spin_unlock_irqrestore(&spinlock
, flags
);
723 spin_lock_irqsave(&spinlock
, flags
);
725 superio_select(GAMEPORT
);
726 superio_outb(gpact
, ACTREG
);
728 spin_unlock_irqrestore(&spinlock
, flags
);
734 static void __exit
it87_wdt_exit(void)
739 nolock
= !spin_trylock_irqsave(&spinlock
, flags
);
741 superio_select(GPIO
);
742 superio_outb(0x00, WDTCTRL
);
743 superio_outb(0x00, WDTCFG
);
744 superio_outb(0x00, WDTVALLSB
);
746 superio_outb(0x00, WDTVALMSB
);
747 if (test_bit(WDTS_USE_GP
, &wdt_status
)) {
748 superio_select(GAMEPORT
);
749 superio_outb(gpact
, ACTREG
);
752 superio_outb(ciract
, ACTREG
);
756 spin_unlock_irqrestore(&spinlock
, flags
);
758 misc_deregister(&wdt_miscdev
);
759 unregister_reboot_notifier(&wdt_notifier
);
760 release_region(base
, test_bit(WDTS_USE_GP
, &wdt_status
) ? 1 : 8);
763 module_init(it87_wdt_init
);
764 module_exit(it87_wdt_exit
);
766 MODULE_AUTHOR("Oliver Schuster");
767 MODULE_DESCRIPTION("Hardware Watchdog Device Driver for IT87xx EC-LPC I/O");
768 MODULE_LICENSE("GPL");
769 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);