1 /***************************************************************************
2 * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
3 * Copyright (C) 2007-2009 Hans de Goede <hdegoede@redhat.com> *
4 * Copyright (C) 2010 Giel van Schijndel <me@mortis.eu> *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
22 #include <linux/err.h>
24 #include <linux/init.h>
26 #include <linux/ioport.h>
27 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/notifier.h>
31 #include <linux/reboot.h>
32 #include <linux/uaccess.h>
33 #include <linux/watchdog.h>
35 #define DRVNAME "f71808e_wdt"
37 #define SIO_F71808FG_LD_WDT 0x07 /* Watchdog timer logical device */
38 #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
39 #define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */
41 #define SIO_REG_LDSEL 0x07 /* Logical device select */
42 #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
43 #define SIO_REG_DEVREV 0x22 /* Device revision */
44 #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
45 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
46 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
48 #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
49 #define SIO_F71808_ID 0x0901 /* Chipset ID */
50 #define SIO_F71858_ID 0x0507 /* Chipset ID */
51 #define SIO_F71862_ID 0x0601 /* Chipset ID */
52 #define SIO_F71882_ID 0x0541 /* Chipset ID */
53 #define SIO_F71889_ID 0x0723 /* Chipset ID */
55 #define F71882FG_REG_START 0x01
57 #define F71808FG_REG_WDO_CONF 0xf0
58 #define F71808FG_REG_WDT_CONF 0xf5
59 #define F71808FG_REG_WD_TIME 0xf6
61 #define F71808FG_FLAG_WDOUT_EN 7
63 #define F71808FG_FLAG_WDTMOUT_STS 5
64 #define F71808FG_FLAG_WD_EN 5
65 #define F71808FG_FLAG_WD_PULSE 4
66 #define F71808FG_FLAG_WD_UNIT 3
69 #define WATCHDOG_TIMEOUT 60 /* 1 minute default timeout */
70 #define WATCHDOG_MAX_TIMEOUT (60 * 255)
71 #define WATCHDOG_PULSE_WIDTH 125 /* 125 ms, default pulse width for
74 static unsigned short force_id
;
75 module_param(force_id
, ushort
, 0);
76 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
78 static const int max_timeout
= WATCHDOG_MAX_TIMEOUT
;
79 static int timeout
= 60; /* default timeout in seconds */
80 module_param(timeout
, int, 0);
81 MODULE_PARM_DESC(timeout
,
82 "Watchdog timeout in seconds. 1<= timeout <="
83 __MODULE_STRING(WATCHDOG_MAX_TIMEOUT
) " (default="
84 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
86 static unsigned int pulse_width
= WATCHDOG_PULSE_WIDTH
;
87 module_param(pulse_width
, uint
, 0);
88 MODULE_PARM_DESC(pulse_width
,
89 "Watchdog signal pulse width. 0(=level), 1 ms, 25 ms, 125 ms or 5000 ms"
90 " (default=" __MODULE_STRING(WATCHDOG_PULSE_WIDTH
) ")");
92 static int nowayout
= WATCHDOG_NOWAYOUT
;
93 module_param(nowayout
, bool, 0444);
94 MODULE_PARM_DESC(nowayout
, "Disable watchdog shutdown on close");
96 static unsigned int start_withtimeout
;
97 module_param(start_withtimeout
, uint
, 0);
98 MODULE_PARM_DESC(start_withtimeout
, "Start watchdog timer on module load with"
99 " given initial timeout. Zero (default) disables this feature.");
101 enum chips
{ f71808fg
, f71858fg
, f71862fg
, f71882fg
, f71889fg
};
103 static const char *f71808e_names
[] = {
111 /* Super-I/O Function prototypes */
112 static inline int superio_inb(int base
, int reg
);
113 static inline int superio_inw(int base
, int reg
);
114 static inline void superio_outb(int base
, int reg
, u8 val
);
115 static inline void superio_set_bit(int base
, int reg
, int bit
);
116 static inline void superio_clear_bit(int base
, int reg
, int bit
);
117 static inline int superio_enter(int base
);
118 static inline void superio_select(int base
, int ld
);
119 static inline void superio_exit(int base
);
121 struct watchdog_data
{
122 unsigned short sioaddr
;
124 unsigned long opened
;
127 struct watchdog_info ident
;
129 unsigned short timeout
;
130 u8 timer_val
; /* content for the wd_time register */
132 u8 pulse_val
; /* pulse width flag */
133 char pulse_mode
; /* enable pulse output mode? */
134 char caused_reboot
; /* last reboot was by the watchdog */
137 static struct watchdog_data watchdog
= {
138 .lock
= __MUTEX_INITIALIZER(watchdog
.lock
),
141 /* Super I/O functions */
142 static inline int superio_inb(int base
, int reg
)
145 return inb(base
+ 1);
148 static int superio_inw(int base
, int reg
)
151 val
= superio_inb(base
, reg
) << 8;
152 val
|= superio_inb(base
, reg
+ 1);
156 static inline void superio_outb(int base
, int reg
, u8 val
)
162 static inline void superio_set_bit(int base
, int reg
, int bit
)
164 unsigned long val
= superio_inb(base
, reg
);
165 __set_bit(bit
, &val
);
166 superio_outb(base
, reg
, val
);
169 static inline void superio_clear_bit(int base
, int reg
, int bit
)
171 unsigned long val
= superio_inb(base
, reg
);
172 __clear_bit(bit
, &val
);
173 superio_outb(base
, reg
, val
);
176 static inline int superio_enter(int base
)
178 /* Don't step on other drivers' I/O space by accident */
179 if (!request_muxed_region(base
, 2, DRVNAME
)) {
180 printk(KERN_ERR DRVNAME
": I/O address 0x%04x already in use\n",
185 /* according to the datasheet the key must be send twice! */
186 outb(SIO_UNLOCK_KEY
, base
);
187 outb(SIO_UNLOCK_KEY
, base
);
192 static inline void superio_select(int base
, int ld
)
194 outb(SIO_REG_LDSEL
, base
);
198 static inline void superio_exit(int base
)
200 outb(SIO_LOCK_KEY
, base
);
201 release_region(base
, 2);
204 static int watchdog_set_timeout(int timeout
)
207 || timeout
> max_timeout
) {
208 printk(KERN_ERR DRVNAME
": watchdog timeout out of range\n");
212 mutex_lock(&watchdog
.lock
);
214 watchdog
.timeout
= timeout
;
215 if (timeout
> 0xff) {
216 watchdog
.timer_val
= DIV_ROUND_UP(timeout
, 60);
217 watchdog
.minutes_mode
= true;
219 watchdog
.timer_val
= timeout
;
220 watchdog
.minutes_mode
= false;
223 mutex_unlock(&watchdog
.lock
);
228 static int watchdog_set_pulse_width(unsigned int pw
)
232 mutex_lock(&watchdog
.lock
);
235 watchdog
.pulse_val
= 0;
236 } else if (pw
<= 25) {
237 watchdog
.pulse_val
= 1;
238 } else if (pw
<= 125) {
239 watchdog
.pulse_val
= 2;
240 } else if (pw
<= 5000) {
241 watchdog
.pulse_val
= 3;
243 printk(KERN_ERR DRVNAME
": pulse width out of range\n");
248 watchdog
.pulse_mode
= pw
;
251 mutex_unlock(&watchdog
.lock
);
255 static int watchdog_keepalive(void)
259 mutex_lock(&watchdog
.lock
);
260 err
= superio_enter(watchdog
.sioaddr
);
263 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
265 if (watchdog
.minutes_mode
)
266 /* select minutes for timer units */
267 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
268 F71808FG_FLAG_WD_UNIT
);
270 /* select seconds for timer units */
271 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
272 F71808FG_FLAG_WD_UNIT
);
274 /* Set timer value */
275 superio_outb(watchdog
.sioaddr
, F71808FG_REG_WD_TIME
,
278 superio_exit(watchdog
.sioaddr
);
281 mutex_unlock(&watchdog
.lock
);
285 static int watchdog_start(void)
287 /* Make sure we don't die as soon as the watchdog is enabled below */
288 int err
= watchdog_keepalive();
292 mutex_lock(&watchdog
.lock
);
293 err
= superio_enter(watchdog
.sioaddr
);
296 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
298 /* Watchdog pin configuration */
299 switch (watchdog
.type
) {
301 /* Set pin 21 to GPIO23/WDTRST#, then to WDTRST# */
302 superio_clear_bit(watchdog
.sioaddr
, 0x2a, 3);
303 superio_clear_bit(watchdog
.sioaddr
, 0x2b, 3);
307 /* Set pin 56 to WDTRST# */
308 superio_set_bit(watchdog
.sioaddr
, 0x29, 1);
312 /* set pin 40 to WDTRST# */
313 superio_outb(watchdog
.sioaddr
, 0x2b,
314 superio_inb(watchdog
.sioaddr
, 0x2b) & 0xcf);
319 * 'default' label to shut up the compiler and catch
326 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
327 superio_set_bit(watchdog
.sioaddr
, SIO_REG_ENABLE
, 0);
328 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDO_CONF
,
329 F71808FG_FLAG_WDOUT_EN
);
331 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
332 F71808FG_FLAG_WD_EN
);
334 if (watchdog
.pulse_mode
) {
335 /* Select "pulse" output mode with given duration */
336 u8 wdt_conf
= superio_inb(watchdog
.sioaddr
,
337 F71808FG_REG_WDT_CONF
);
339 /* Set WD_PSWIDTH bits (1:0) */
340 wdt_conf
= (wdt_conf
& 0xfc) | (watchdog
.pulse_val
& 0x03);
341 /* Set WD_PULSE to "pulse" mode */
342 wdt_conf
|= BIT(F71808FG_FLAG_WD_PULSE
);
344 superio_outb(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
347 /* Select "level" output mode */
348 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
349 F71808FG_FLAG_WD_PULSE
);
353 superio_exit(watchdog
.sioaddr
);
355 mutex_unlock(&watchdog
.lock
);
360 static int watchdog_stop(void)
364 mutex_lock(&watchdog
.lock
);
365 err
= superio_enter(watchdog
.sioaddr
);
368 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
370 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
371 F71808FG_FLAG_WD_EN
);
373 superio_exit(watchdog
.sioaddr
);
376 mutex_unlock(&watchdog
.lock
);
381 static int watchdog_get_status(void)
385 mutex_lock(&watchdog
.lock
);
386 status
= (watchdog
.caused_reboot
) ? WDIOF_CARDRESET
: 0;
387 mutex_unlock(&watchdog
.lock
);
392 static bool watchdog_is_running(void)
395 * if we fail to determine the watchdog's status assume it to be
396 * running to be on the safe side
398 bool is_running
= true;
400 mutex_lock(&watchdog
.lock
);
401 if (superio_enter(watchdog
.sioaddr
))
403 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
405 is_running
= (superio_inb(watchdog
.sioaddr
, SIO_REG_ENABLE
) & BIT(0))
406 && (superio_inb(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
)
407 & F71808FG_FLAG_WD_EN
);
409 superio_exit(watchdog
.sioaddr
);
412 mutex_unlock(&watchdog
.lock
);
416 /* /dev/watchdog api */
418 static int watchdog_open(struct inode
*inode
, struct file
*file
)
422 /* If the watchdog is alive we don't need to start it again */
423 if (test_and_set_bit(0, &watchdog
.opened
))
426 err
= watchdog_start();
428 clear_bit(0, &watchdog
.opened
);
433 __module_get(THIS_MODULE
);
435 watchdog
.expect_close
= 0;
436 return nonseekable_open(inode
, file
);
439 static int watchdog_release(struct inode
*inode
, struct file
*file
)
441 clear_bit(0, &watchdog
.opened
);
443 if (!watchdog
.expect_close
) {
444 watchdog_keepalive();
445 printk(KERN_CRIT DRVNAME
446 ": Unexpected close, not stopping watchdog!\n");
447 } else if (!nowayout
) {
455 * @file: file handle to the watchdog
456 * @buf: buffer to write
457 * @count: count of bytes
458 * @ppos: pointer to the position to write. No seeks allowed
460 * A write to a watchdog device is defined as a keepalive signal. Any
461 * write of data will do, as we we don't define content meaning.
464 static ssize_t
watchdog_write(struct file
*file
, const char __user
*buf
,
465 size_t count
, loff_t
*ppos
)
471 /* In case it was set long ago */
472 bool expect_close
= false;
474 for (i
= 0; i
!= count
; i
++) {
476 if (get_user(c
, buf
+ i
))
478 expect_close
= (c
== 'V');
481 /* Properly order writes across fork()ed processes */
482 mutex_lock(&watchdog
.lock
);
483 watchdog
.expect_close
= expect_close
;
484 mutex_unlock(&watchdog
.lock
);
487 /* someone wrote to us, we should restart timer */
488 watchdog_keepalive();
495 * @inode: inode of the device
496 * @file: file handle to the device
497 * @cmd: watchdog command
498 * @arg: argument pointer
500 * The watchdog API defines a common set of functions for all watchdogs
501 * according to their available features.
503 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
510 struct watchdog_info __user
*ident
;
514 uarg
.i
= (int __user
*)arg
;
517 case WDIOC_GETSUPPORT
:
518 return copy_to_user(uarg
.ident
, &watchdog
.ident
,
519 sizeof(watchdog
.ident
)) ? -EFAULT
: 0;
521 case WDIOC_GETSTATUS
:
522 status
= watchdog_get_status();
525 return put_user(status
, uarg
.i
);
527 case WDIOC_GETBOOTSTATUS
:
528 return put_user(0, uarg
.i
);
530 case WDIOC_SETOPTIONS
:
531 if (get_user(new_options
, uarg
.i
))
534 if (new_options
& WDIOS_DISABLECARD
)
537 if (new_options
& WDIOS_ENABLECARD
)
538 return watchdog_start();
541 case WDIOC_KEEPALIVE
:
542 watchdog_keepalive();
545 case WDIOC_SETTIMEOUT
:
546 if (get_user(new_timeout
, uarg
.i
))
549 if (watchdog_set_timeout(new_timeout
))
552 watchdog_keepalive();
555 case WDIOC_GETTIMEOUT
:
556 return put_user(watchdog
.timeout
, uarg
.i
);
564 static int watchdog_notify_sys(struct notifier_block
*this, unsigned long code
,
567 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
572 static const struct file_operations watchdog_fops
= {
573 .owner
= THIS_MODULE
,
575 .open
= watchdog_open
,
576 .release
= watchdog_release
,
577 .write
= watchdog_write
,
578 .unlocked_ioctl
= watchdog_ioctl
,
581 static struct miscdevice watchdog_miscdev
= {
582 .minor
= WATCHDOG_MINOR
,
584 .fops
= &watchdog_fops
,
587 static struct notifier_block watchdog_notifier
= {
588 .notifier_call
= watchdog_notify_sys
,
591 static int __init
watchdog_init(int sioaddr
)
593 int wdt_conf
, err
= 0;
595 /* No need to lock watchdog.lock here because no entry points
596 * into the module have been registered yet.
598 watchdog
.sioaddr
= sioaddr
;
599 watchdog
.ident
.options
= WDIOC_SETTIMEOUT
601 | WDIOF_KEEPALIVEPING
;
603 snprintf(watchdog
.ident
.identity
,
604 sizeof(watchdog
.ident
.identity
), "%s watchdog",
605 f71808e_names
[watchdog
.type
]);
607 err
= superio_enter(sioaddr
);
610 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
612 wdt_conf
= superio_inb(sioaddr
, F71808FG_REG_WDT_CONF
);
613 watchdog
.caused_reboot
= wdt_conf
& F71808FG_FLAG_WDTMOUT_STS
;
615 superio_exit(sioaddr
);
617 err
= watchdog_set_timeout(timeout
);
620 err
= watchdog_set_pulse_width(pulse_width
);
624 err
= register_reboot_notifier(&watchdog_notifier
);
628 err
= misc_register(&watchdog_miscdev
);
630 printk(KERN_ERR DRVNAME
631 ": cannot register miscdev on minor=%d\n",
632 watchdog_miscdev
.minor
);
636 if (start_withtimeout
) {
637 if (start_withtimeout
<= 0
638 || start_withtimeout
> max_timeout
) {
639 printk(KERN_ERR DRVNAME
640 ": starting timeout out of range\n");
645 err
= watchdog_start();
647 printk(KERN_ERR DRVNAME
648 ": cannot start watchdog timer\n");
652 mutex_lock(&watchdog
.lock
);
653 err
= superio_enter(sioaddr
);
656 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
658 if (start_withtimeout
> 0xff) {
659 /* select minutes for timer units */
660 superio_set_bit(sioaddr
, F71808FG_REG_WDT_CONF
,
661 F71808FG_FLAG_WD_UNIT
);
662 superio_outb(sioaddr
, F71808FG_REG_WD_TIME
,
663 DIV_ROUND_UP(start_withtimeout
, 60));
665 /* select seconds for timer units */
666 superio_clear_bit(sioaddr
, F71808FG_REG_WDT_CONF
,
667 F71808FG_FLAG_WD_UNIT
);
668 superio_outb(sioaddr
, F71808FG_REG_WD_TIME
,
672 superio_exit(sioaddr
);
673 mutex_unlock(&watchdog
.lock
);
676 __module_get(THIS_MODULE
);
678 printk(KERN_INFO DRVNAME
679 ": watchdog started with initial timeout of %u sec\n",
686 mutex_unlock(&watchdog
.lock
);
688 misc_deregister(&watchdog_miscdev
);
690 unregister_reboot_notifier(&watchdog_notifier
);
695 static int __init
f71808e_find(int sioaddr
)
698 int err
= superio_enter(sioaddr
);
702 devid
= superio_inw(sioaddr
, SIO_REG_MANID
);
703 if (devid
!= SIO_FINTEK_ID
) {
704 pr_debug(DRVNAME
": Not a Fintek device\n");
709 devid
= force_id
? force_id
: superio_inw(sioaddr
, SIO_REG_DEVID
);
712 watchdog
.type
= f71808fg
;
715 watchdog
.type
= f71882fg
;
718 watchdog
.type
= f71889fg
;
721 /* These have a watchdog, though it isn't implemented (yet). */
725 /* Confirmed (by datasheet) not to have a watchdog. */
729 printk(KERN_INFO DRVNAME
": Unrecognized Fintek device: %04x\n",
730 (unsigned int)devid
);
735 printk(KERN_INFO DRVNAME
": Found %s watchdog chip, revision %d\n",
736 f71808e_names
[watchdog
.type
],
737 (int)superio_inb(sioaddr
, SIO_REG_DEVREV
));
739 superio_exit(sioaddr
);
743 static int __init
f71808e_init(void)
745 static const unsigned short addrs
[] = { 0x2e, 0x4e };
749 for (i
= 0; i
< ARRAY_SIZE(addrs
); i
++) {
750 err
= f71808e_find(addrs
[i
]);
754 if (i
== ARRAY_SIZE(addrs
))
757 return watchdog_init(addrs
[i
]);
760 static void __exit
f71808e_exit(void)
762 if (watchdog_is_running()) {
763 printk(KERN_WARNING DRVNAME
764 ": Watchdog timer still running, stopping it\n");
767 misc_deregister(&watchdog_miscdev
);
768 unregister_reboot_notifier(&watchdog_notifier
);
771 MODULE_DESCRIPTION("F71808E Watchdog Driver");
772 MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>");
773 MODULE_LICENSE("GPL");
775 module_init(f71808e_init
);
776 module_exit(f71808e_exit
);