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);
313 * 'default' label to shut up the compiler and catch
320 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
321 superio_set_bit(watchdog
.sioaddr
, SIO_REG_ENABLE
, 0);
322 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDO_CONF
,
323 F71808FG_FLAG_WDOUT_EN
);
325 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
326 F71808FG_FLAG_WD_EN
);
328 if (watchdog
.pulse_mode
) {
329 /* Select "pulse" output mode with given duration */
330 u8 wdt_conf
= superio_inb(watchdog
.sioaddr
,
331 F71808FG_REG_WDT_CONF
);
333 /* Set WD_PSWIDTH bits (1:0) */
334 wdt_conf
= (wdt_conf
& 0xfc) | (watchdog
.pulse_val
& 0x03);
335 /* Set WD_PULSE to "pulse" mode */
336 wdt_conf
|= BIT(F71808FG_FLAG_WD_PULSE
);
338 superio_outb(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
341 /* Select "level" output mode */
342 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
343 F71808FG_FLAG_WD_PULSE
);
347 superio_exit(watchdog
.sioaddr
);
349 mutex_unlock(&watchdog
.lock
);
354 static int watchdog_stop(void)
358 mutex_lock(&watchdog
.lock
);
359 err
= superio_enter(watchdog
.sioaddr
);
362 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
364 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
365 F71808FG_FLAG_WD_EN
);
367 superio_exit(watchdog
.sioaddr
);
370 mutex_unlock(&watchdog
.lock
);
375 static int watchdog_get_status(void)
379 mutex_lock(&watchdog
.lock
);
380 status
= (watchdog
.caused_reboot
) ? WDIOF_CARDRESET
: 0;
381 mutex_unlock(&watchdog
.lock
);
386 static bool watchdog_is_running(void)
389 * if we fail to determine the watchdog's status assume it to be
390 * running to be on the safe side
392 bool is_running
= true;
394 mutex_lock(&watchdog
.lock
);
395 if (superio_enter(watchdog
.sioaddr
))
397 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
399 is_running
= (superio_inb(watchdog
.sioaddr
, SIO_REG_ENABLE
) & BIT(0))
400 && (superio_inb(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
)
401 & F71808FG_FLAG_WD_EN
);
403 superio_exit(watchdog
.sioaddr
);
406 mutex_unlock(&watchdog
.lock
);
410 /* /dev/watchdog api */
412 static int watchdog_open(struct inode
*inode
, struct file
*file
)
416 /* If the watchdog is alive we don't need to start it again */
417 if (test_and_set_bit(0, &watchdog
.opened
))
420 err
= watchdog_start();
422 clear_bit(0, &watchdog
.opened
);
427 __module_get(THIS_MODULE
);
429 watchdog
.expect_close
= 0;
430 return nonseekable_open(inode
, file
);
433 static int watchdog_release(struct inode
*inode
, struct file
*file
)
435 clear_bit(0, &watchdog
.opened
);
437 if (!watchdog
.expect_close
) {
438 watchdog_keepalive();
439 printk(KERN_CRIT DRVNAME
440 ": Unexpected close, not stopping watchdog!\n");
441 } else if (!nowayout
) {
449 * @file: file handle to the watchdog
450 * @buf: buffer to write
451 * @count: count of bytes
452 * @ppos: pointer to the position to write. No seeks allowed
454 * A write to a watchdog device is defined as a keepalive signal. Any
455 * write of data will do, as we we don't define content meaning.
458 static ssize_t
watchdog_write(struct file
*file
, const char __user
*buf
,
459 size_t count
, loff_t
*ppos
)
465 /* In case it was set long ago */
466 bool expect_close
= false;
468 for (i
= 0; i
!= count
; i
++) {
470 if (get_user(c
, buf
+ i
))
472 expect_close
= (c
== 'V');
475 /* Properly order writes across fork()ed processes */
476 mutex_lock(&watchdog
.lock
);
477 watchdog
.expect_close
= expect_close
;
478 mutex_unlock(&watchdog
.lock
);
481 /* someone wrote to us, we should restart timer */
482 watchdog_keepalive();
489 * @inode: inode of the device
490 * @file: file handle to the device
491 * @cmd: watchdog command
492 * @arg: argument pointer
494 * The watchdog API defines a common set of functions for all watchdogs
495 * according to their available features.
497 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
504 struct watchdog_info __user
*ident
;
508 uarg
.i
= (int __user
*)arg
;
511 case WDIOC_GETSUPPORT
:
512 return copy_to_user(uarg
.ident
, &watchdog
.ident
,
513 sizeof(watchdog
.ident
)) ? -EFAULT
: 0;
515 case WDIOC_GETSTATUS
:
516 status
= watchdog_get_status();
519 return put_user(status
, uarg
.i
);
521 case WDIOC_GETBOOTSTATUS
:
522 return put_user(0, uarg
.i
);
524 case WDIOC_SETOPTIONS
:
525 if (get_user(new_options
, uarg
.i
))
528 if (new_options
& WDIOS_DISABLECARD
)
531 if (new_options
& WDIOS_ENABLECARD
)
532 return watchdog_start();
535 case WDIOC_KEEPALIVE
:
536 watchdog_keepalive();
539 case WDIOC_SETTIMEOUT
:
540 if (get_user(new_timeout
, uarg
.i
))
543 if (watchdog_set_timeout(new_timeout
))
546 watchdog_keepalive();
549 case WDIOC_GETTIMEOUT
:
550 return put_user(watchdog
.timeout
, uarg
.i
);
558 static int watchdog_notify_sys(struct notifier_block
*this, unsigned long code
,
561 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
566 static const struct file_operations watchdog_fops
= {
567 .owner
= THIS_MODULE
,
569 .open
= watchdog_open
,
570 .release
= watchdog_release
,
571 .write
= watchdog_write
,
572 .unlocked_ioctl
= watchdog_ioctl
,
575 static struct miscdevice watchdog_miscdev
= {
576 .minor
= WATCHDOG_MINOR
,
578 .fops
= &watchdog_fops
,
581 static struct notifier_block watchdog_notifier
= {
582 .notifier_call
= watchdog_notify_sys
,
585 static int __init
watchdog_init(int sioaddr
)
587 int wdt_conf
, err
= 0;
589 /* No need to lock watchdog.lock here because no entry points
590 * into the module have been registered yet.
592 watchdog
.sioaddr
= sioaddr
;
593 watchdog
.ident
.options
= WDIOC_SETTIMEOUT
595 | WDIOF_KEEPALIVEPING
;
597 snprintf(watchdog
.ident
.identity
,
598 sizeof(watchdog
.ident
.identity
), "%s watchdog",
599 f71808e_names
[watchdog
.type
]);
601 err
= superio_enter(sioaddr
);
604 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
606 wdt_conf
= superio_inb(sioaddr
, F71808FG_REG_WDT_CONF
);
607 watchdog
.caused_reboot
= wdt_conf
& F71808FG_FLAG_WDTMOUT_STS
;
609 superio_exit(sioaddr
);
611 err
= watchdog_set_timeout(timeout
);
614 err
= watchdog_set_pulse_width(pulse_width
);
618 err
= register_reboot_notifier(&watchdog_notifier
);
622 err
= misc_register(&watchdog_miscdev
);
624 printk(KERN_ERR DRVNAME
625 ": cannot register miscdev on minor=%d\n",
626 watchdog_miscdev
.minor
);
630 if (start_withtimeout
) {
631 if (start_withtimeout
<= 0
632 || start_withtimeout
> max_timeout
) {
633 printk(KERN_ERR DRVNAME
634 ": starting timeout out of range\n");
639 err
= watchdog_start();
641 printk(KERN_ERR DRVNAME
642 ": cannot start watchdog timer\n");
646 mutex_lock(&watchdog
.lock
);
647 err
= superio_enter(sioaddr
);
650 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
652 if (start_withtimeout
> 0xff) {
653 /* select minutes for timer units */
654 superio_set_bit(sioaddr
, F71808FG_REG_WDT_CONF
,
655 F71808FG_FLAG_WD_UNIT
);
656 superio_outb(sioaddr
, F71808FG_REG_WD_TIME
,
657 DIV_ROUND_UP(start_withtimeout
, 60));
659 /* select seconds for timer units */
660 superio_clear_bit(sioaddr
, F71808FG_REG_WDT_CONF
,
661 F71808FG_FLAG_WD_UNIT
);
662 superio_outb(sioaddr
, F71808FG_REG_WD_TIME
,
666 superio_exit(sioaddr
);
667 mutex_unlock(&watchdog
.lock
);
670 __module_get(THIS_MODULE
);
672 printk(KERN_INFO DRVNAME
673 ": watchdog started with initial timeout of %u sec\n",
680 mutex_unlock(&watchdog
.lock
);
682 misc_deregister(&watchdog_miscdev
);
684 unregister_reboot_notifier(&watchdog_notifier
);
689 static int __init
f71808e_find(int sioaddr
)
692 int err
= superio_enter(sioaddr
);
696 devid
= superio_inw(sioaddr
, SIO_REG_MANID
);
697 if (devid
!= SIO_FINTEK_ID
) {
698 pr_debug(DRVNAME
": Not a Fintek device\n");
703 devid
= force_id
? force_id
: superio_inw(sioaddr
, SIO_REG_DEVID
);
706 watchdog
.type
= f71808fg
;
709 watchdog
.type
= f71882fg
;
713 /* These have a watchdog, though it isn't implemented (yet). */
717 /* Confirmed (by datasheet) not to have a watchdog. */
721 printk(KERN_INFO DRVNAME
": Unrecognized Fintek device: %04x\n",
722 (unsigned int)devid
);
727 printk(KERN_INFO DRVNAME
": Found %s watchdog chip, revision %d\n",
728 f71808e_names
[watchdog
.type
],
729 (int)superio_inb(sioaddr
, SIO_REG_DEVREV
));
731 superio_exit(sioaddr
);
735 static int __init
f71808e_init(void)
737 static const unsigned short addrs
[] = { 0x2e, 0x4e };
741 for (i
= 0; i
< ARRAY_SIZE(addrs
); i
++) {
742 err
= f71808e_find(addrs
[i
]);
746 if (i
== ARRAY_SIZE(addrs
))
749 return watchdog_init(addrs
[i
]);
752 static void __exit
f71808e_exit(void)
754 if (watchdog_is_running()) {
755 printk(KERN_WARNING DRVNAME
756 ": Watchdog timer still running, stopping it\n");
759 misc_deregister(&watchdog_miscdev
);
760 unregister_reboot_notifier(&watchdog_notifier
);
763 MODULE_DESCRIPTION("F71808E Watchdog Driver");
764 MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>");
765 MODULE_LICENSE("GPL");
767 module_init(f71808e_init
);
768 module_exit(f71808e_exit
);