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_MFUNCT1 0x29 /* Multi function select 1 */
46 #define SIO_REG_MFUNCT2 0x2a /* Multi function select 2 */
47 #define SIO_REG_MFUNCT3 0x2b /* Multi function select 3 */
48 #define SIO_REG_ENABLE 0x30 /* Logical device enable */
49 #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
51 #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
52 #define SIO_F71808_ID 0x0901 /* Chipset ID */
53 #define SIO_F71858_ID 0x0507 /* Chipset ID */
54 #define SIO_F71862_ID 0x0601 /* Chipset ID */
55 #define SIO_F71882_ID 0x0541 /* Chipset ID */
56 #define SIO_F71889_ID 0x0723 /* Chipset ID */
58 #define F71808FG_REG_WDO_CONF 0xf0
59 #define F71808FG_REG_WDT_CONF 0xf5
60 #define F71808FG_REG_WD_TIME 0xf6
62 #define F71808FG_FLAG_WDOUT_EN 7
64 #define F71808FG_FLAG_WDTMOUT_STS 5
65 #define F71808FG_FLAG_WD_EN 5
66 #define F71808FG_FLAG_WD_PULSE 4
67 #define F71808FG_FLAG_WD_UNIT 3
70 #define WATCHDOG_TIMEOUT 60 /* 1 minute default timeout */
71 #define WATCHDOG_MAX_TIMEOUT (60 * 255)
72 #define WATCHDOG_PULSE_WIDTH 125 /* 125 ms, default pulse width for
75 static unsigned short force_id
;
76 module_param(force_id
, ushort
, 0);
77 MODULE_PARM_DESC(force_id
, "Override the detected device ID");
79 static const int max_timeout
= WATCHDOG_MAX_TIMEOUT
;
80 static int timeout
= WATCHDOG_TIMEOUT
; /* default timeout in seconds */
81 module_param(timeout
, int, 0);
82 MODULE_PARM_DESC(timeout
,
83 "Watchdog timeout in seconds. 1<= timeout <="
84 __MODULE_STRING(WATCHDOG_MAX_TIMEOUT
) " (default="
85 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
87 static unsigned int pulse_width
= WATCHDOG_PULSE_WIDTH
;
88 module_param(pulse_width
, uint
, 0);
89 MODULE_PARM_DESC(pulse_width
,
90 "Watchdog signal pulse width. 0(=level), 1 ms, 25 ms, 125 ms or 5000 ms"
91 " (default=" __MODULE_STRING(WATCHDOG_PULSE_WIDTH
) ")");
93 static int nowayout
= WATCHDOG_NOWAYOUT
;
94 module_param(nowayout
, bool, 0444);
95 MODULE_PARM_DESC(nowayout
, "Disable watchdog shutdown on close");
97 static unsigned int start_withtimeout
;
98 module_param(start_withtimeout
, uint
, 0);
99 MODULE_PARM_DESC(start_withtimeout
, "Start watchdog timer on module load with"
100 " given initial timeout. Zero (default) disables this feature.");
102 enum chips
{ f71808fg
, f71858fg
, f71862fg
, f71882fg
, f71889fg
};
104 static const char *f71808e_names
[] = {
112 /* Super-I/O Function prototypes */
113 static inline int superio_inb(int base
, int reg
);
114 static inline int superio_inw(int base
, int reg
);
115 static inline void superio_outb(int base
, int reg
, u8 val
);
116 static inline void superio_set_bit(int base
, int reg
, int bit
);
117 static inline void superio_clear_bit(int base
, int reg
, int bit
);
118 static inline int superio_enter(int base
);
119 static inline void superio_select(int base
, int ld
);
120 static inline void superio_exit(int base
);
122 struct watchdog_data
{
123 unsigned short sioaddr
;
125 unsigned long opened
;
128 struct watchdog_info ident
;
130 unsigned short timeout
;
131 u8 timer_val
; /* content for the wd_time register */
133 u8 pulse_val
; /* pulse width flag */
134 char pulse_mode
; /* enable pulse output mode? */
135 char caused_reboot
; /* last reboot was by the watchdog */
138 static struct watchdog_data watchdog
= {
139 .lock
= __MUTEX_INITIALIZER(watchdog
.lock
),
142 /* Super I/O functions */
143 static inline int superio_inb(int base
, int reg
)
146 return inb(base
+ 1);
149 static int superio_inw(int base
, int reg
)
152 val
= superio_inb(base
, reg
) << 8;
153 val
|= superio_inb(base
, reg
+ 1);
157 static inline void superio_outb(int base
, int reg
, u8 val
)
163 static inline void superio_set_bit(int base
, int reg
, int bit
)
165 unsigned long val
= superio_inb(base
, reg
);
166 __set_bit(bit
, &val
);
167 superio_outb(base
, reg
, val
);
170 static inline void superio_clear_bit(int base
, int reg
, int bit
)
172 unsigned long val
= superio_inb(base
, reg
);
173 __clear_bit(bit
, &val
);
174 superio_outb(base
, reg
, val
);
177 static inline int superio_enter(int base
)
179 /* Don't step on other drivers' I/O space by accident */
180 if (!request_muxed_region(base
, 2, DRVNAME
)) {
181 printk(KERN_ERR DRVNAME
": I/O address 0x%04x already in use\n",
186 /* according to the datasheet the key must be send twice! */
187 outb(SIO_UNLOCK_KEY
, base
);
188 outb(SIO_UNLOCK_KEY
, base
);
193 static inline void superio_select(int base
, int ld
)
195 outb(SIO_REG_LDSEL
, base
);
199 static inline void superio_exit(int base
)
201 outb(SIO_LOCK_KEY
, base
);
202 release_region(base
, 2);
205 static int watchdog_set_timeout(int timeout
)
208 || timeout
> max_timeout
) {
209 printk(KERN_ERR DRVNAME
": watchdog timeout out of range\n");
213 mutex_lock(&watchdog
.lock
);
215 watchdog
.timeout
= timeout
;
216 if (timeout
> 0xff) {
217 watchdog
.timer_val
= DIV_ROUND_UP(timeout
, 60);
218 watchdog
.minutes_mode
= true;
220 watchdog
.timer_val
= timeout
;
221 watchdog
.minutes_mode
= false;
224 mutex_unlock(&watchdog
.lock
);
229 static int watchdog_set_pulse_width(unsigned int pw
)
233 mutex_lock(&watchdog
.lock
);
236 watchdog
.pulse_val
= 0;
237 } else if (pw
<= 25) {
238 watchdog
.pulse_val
= 1;
239 } else if (pw
<= 125) {
240 watchdog
.pulse_val
= 2;
241 } else if (pw
<= 5000) {
242 watchdog
.pulse_val
= 3;
244 printk(KERN_ERR DRVNAME
": pulse width out of range\n");
249 watchdog
.pulse_mode
= pw
;
252 mutex_unlock(&watchdog
.lock
);
256 static int watchdog_keepalive(void)
260 mutex_lock(&watchdog
.lock
);
261 err
= superio_enter(watchdog
.sioaddr
);
264 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
266 if (watchdog
.minutes_mode
)
267 /* select minutes for timer units */
268 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
269 F71808FG_FLAG_WD_UNIT
);
271 /* select seconds for timer units */
272 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
273 F71808FG_FLAG_WD_UNIT
);
275 /* Set timer value */
276 superio_outb(watchdog
.sioaddr
, F71808FG_REG_WD_TIME
,
279 superio_exit(watchdog
.sioaddr
);
282 mutex_unlock(&watchdog
.lock
);
286 static int watchdog_start(void)
288 /* Make sure we don't die as soon as the watchdog is enabled below */
289 int err
= watchdog_keepalive();
293 mutex_lock(&watchdog
.lock
);
294 err
= superio_enter(watchdog
.sioaddr
);
297 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
299 /* Watchdog pin configuration */
300 switch (watchdog
.type
) {
302 /* Set pin 21 to GPIO23/WDTRST#, then to WDTRST# */
303 superio_clear_bit(watchdog
.sioaddr
, SIO_REG_MFUNCT2
, 3);
304 superio_clear_bit(watchdog
.sioaddr
, SIO_REG_MFUNCT3
, 3);
308 /* Set pin 56 to WDTRST# */
309 superio_set_bit(watchdog
.sioaddr
, SIO_REG_MFUNCT1
, 1);
313 /* set pin 40 to WDTRST# */
314 superio_outb(watchdog
.sioaddr
, SIO_REG_MFUNCT3
,
315 superio_inb(watchdog
.sioaddr
, SIO_REG_MFUNCT3
) & 0xcf);
320 * 'default' label to shut up the compiler and catch
327 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
328 superio_set_bit(watchdog
.sioaddr
, SIO_REG_ENABLE
, 0);
329 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDO_CONF
,
330 F71808FG_FLAG_WDOUT_EN
);
332 superio_set_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
333 F71808FG_FLAG_WD_EN
);
335 if (watchdog
.pulse_mode
) {
336 /* Select "pulse" output mode with given duration */
337 u8 wdt_conf
= superio_inb(watchdog
.sioaddr
,
338 F71808FG_REG_WDT_CONF
);
340 /* Set WD_PSWIDTH bits (1:0) */
341 wdt_conf
= (wdt_conf
& 0xfc) | (watchdog
.pulse_val
& 0x03);
342 /* Set WD_PULSE to "pulse" mode */
343 wdt_conf
|= BIT(F71808FG_FLAG_WD_PULSE
);
345 superio_outb(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
348 /* Select "level" output mode */
349 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
350 F71808FG_FLAG_WD_PULSE
);
354 superio_exit(watchdog
.sioaddr
);
356 mutex_unlock(&watchdog
.lock
);
361 static int watchdog_stop(void)
365 mutex_lock(&watchdog
.lock
);
366 err
= superio_enter(watchdog
.sioaddr
);
369 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
371 superio_clear_bit(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
,
372 F71808FG_FLAG_WD_EN
);
374 superio_exit(watchdog
.sioaddr
);
377 mutex_unlock(&watchdog
.lock
);
382 static int watchdog_get_status(void)
386 mutex_lock(&watchdog
.lock
);
387 status
= (watchdog
.caused_reboot
) ? WDIOF_CARDRESET
: 0;
388 mutex_unlock(&watchdog
.lock
);
393 static bool watchdog_is_running(void)
396 * if we fail to determine the watchdog's status assume it to be
397 * running to be on the safe side
399 bool is_running
= true;
401 mutex_lock(&watchdog
.lock
);
402 if (superio_enter(watchdog
.sioaddr
))
404 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
406 is_running
= (superio_inb(watchdog
.sioaddr
, SIO_REG_ENABLE
) & BIT(0))
407 && (superio_inb(watchdog
.sioaddr
, F71808FG_REG_WDT_CONF
)
408 & F71808FG_FLAG_WD_EN
);
410 superio_exit(watchdog
.sioaddr
);
413 mutex_unlock(&watchdog
.lock
);
417 /* /dev/watchdog api */
419 static int watchdog_open(struct inode
*inode
, struct file
*file
)
423 /* If the watchdog is alive we don't need to start it again */
424 if (test_and_set_bit(0, &watchdog
.opened
))
427 err
= watchdog_start();
429 clear_bit(0, &watchdog
.opened
);
434 __module_get(THIS_MODULE
);
436 watchdog
.expect_close
= 0;
437 return nonseekable_open(inode
, file
);
440 static int watchdog_release(struct inode
*inode
, struct file
*file
)
442 clear_bit(0, &watchdog
.opened
);
444 if (!watchdog
.expect_close
) {
445 watchdog_keepalive();
446 printk(KERN_CRIT DRVNAME
447 ": Unexpected close, not stopping watchdog!\n");
448 } else if (!nowayout
) {
456 * @file: file handle to the watchdog
457 * @buf: buffer to write
458 * @count: count of bytes
459 * @ppos: pointer to the position to write. No seeks allowed
461 * A write to a watchdog device is defined as a keepalive signal. Any
462 * write of data will do, as we we don't define content meaning.
465 static ssize_t
watchdog_write(struct file
*file
, const char __user
*buf
,
466 size_t count
, loff_t
*ppos
)
472 /* In case it was set long ago */
473 bool expect_close
= false;
475 for (i
= 0; i
!= count
; i
++) {
477 if (get_user(c
, buf
+ i
))
479 expect_close
= (c
== 'V');
482 /* Properly order writes across fork()ed processes */
483 mutex_lock(&watchdog
.lock
);
484 watchdog
.expect_close
= expect_close
;
485 mutex_unlock(&watchdog
.lock
);
488 /* someone wrote to us, we should restart timer */
489 watchdog_keepalive();
496 * @inode: inode of the device
497 * @file: file handle to the device
498 * @cmd: watchdog command
499 * @arg: argument pointer
501 * The watchdog API defines a common set of functions for all watchdogs
502 * according to their available features.
504 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
511 struct watchdog_info __user
*ident
;
515 uarg
.i
= (int __user
*)arg
;
518 case WDIOC_GETSUPPORT
:
519 return copy_to_user(uarg
.ident
, &watchdog
.ident
,
520 sizeof(watchdog
.ident
)) ? -EFAULT
: 0;
522 case WDIOC_GETSTATUS
:
523 status
= watchdog_get_status();
526 return put_user(status
, uarg
.i
);
528 case WDIOC_GETBOOTSTATUS
:
529 return put_user(0, uarg
.i
);
531 case WDIOC_SETOPTIONS
:
532 if (get_user(new_options
, uarg
.i
))
535 if (new_options
& WDIOS_DISABLECARD
)
538 if (new_options
& WDIOS_ENABLECARD
)
539 return watchdog_start();
542 case WDIOC_KEEPALIVE
:
543 watchdog_keepalive();
546 case WDIOC_SETTIMEOUT
:
547 if (get_user(new_timeout
, uarg
.i
))
550 if (watchdog_set_timeout(new_timeout
))
553 watchdog_keepalive();
556 case WDIOC_GETTIMEOUT
:
557 return put_user(watchdog
.timeout
, uarg
.i
);
565 static int watchdog_notify_sys(struct notifier_block
*this, unsigned long code
,
568 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
573 static const struct file_operations watchdog_fops
= {
574 .owner
= THIS_MODULE
,
576 .open
= watchdog_open
,
577 .release
= watchdog_release
,
578 .write
= watchdog_write
,
579 .unlocked_ioctl
= watchdog_ioctl
,
582 static struct miscdevice watchdog_miscdev
= {
583 .minor
= WATCHDOG_MINOR
,
585 .fops
= &watchdog_fops
,
588 static struct notifier_block watchdog_notifier
= {
589 .notifier_call
= watchdog_notify_sys
,
592 static int __init
watchdog_init(int sioaddr
)
594 int wdt_conf
, err
= 0;
596 /* No need to lock watchdog.lock here because no entry points
597 * into the module have been registered yet.
599 watchdog
.sioaddr
= sioaddr
;
600 watchdog
.ident
.options
= WDIOC_SETTIMEOUT
602 | WDIOF_KEEPALIVEPING
;
604 snprintf(watchdog
.ident
.identity
,
605 sizeof(watchdog
.ident
.identity
), "%s watchdog",
606 f71808e_names
[watchdog
.type
]);
608 err
= superio_enter(sioaddr
);
611 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
613 wdt_conf
= superio_inb(sioaddr
, F71808FG_REG_WDT_CONF
);
614 watchdog
.caused_reboot
= wdt_conf
& F71808FG_FLAG_WDTMOUT_STS
;
616 superio_exit(sioaddr
);
618 err
= watchdog_set_timeout(timeout
);
621 err
= watchdog_set_pulse_width(pulse_width
);
625 err
= register_reboot_notifier(&watchdog_notifier
);
629 err
= misc_register(&watchdog_miscdev
);
631 printk(KERN_ERR DRVNAME
632 ": cannot register miscdev on minor=%d\n",
633 watchdog_miscdev
.minor
);
637 if (start_withtimeout
) {
638 if (start_withtimeout
<= 0
639 || start_withtimeout
> max_timeout
) {
640 printk(KERN_ERR DRVNAME
641 ": starting timeout out of range\n");
646 err
= watchdog_start();
648 printk(KERN_ERR DRVNAME
649 ": cannot start watchdog timer\n");
653 mutex_lock(&watchdog
.lock
);
654 err
= superio_enter(sioaddr
);
657 superio_select(watchdog
.sioaddr
, SIO_F71808FG_LD_WDT
);
659 if (start_withtimeout
> 0xff) {
660 /* select minutes for timer units */
661 superio_set_bit(sioaddr
, F71808FG_REG_WDT_CONF
,
662 F71808FG_FLAG_WD_UNIT
);
663 superio_outb(sioaddr
, F71808FG_REG_WD_TIME
,
664 DIV_ROUND_UP(start_withtimeout
, 60));
666 /* select seconds for timer units */
667 superio_clear_bit(sioaddr
, F71808FG_REG_WDT_CONF
,
668 F71808FG_FLAG_WD_UNIT
);
669 superio_outb(sioaddr
, F71808FG_REG_WD_TIME
,
673 superio_exit(sioaddr
);
674 mutex_unlock(&watchdog
.lock
);
677 __module_get(THIS_MODULE
);
679 printk(KERN_INFO DRVNAME
680 ": watchdog started with initial timeout of %u sec\n",
687 mutex_unlock(&watchdog
.lock
);
689 misc_deregister(&watchdog_miscdev
);
691 unregister_reboot_notifier(&watchdog_notifier
);
696 static int __init
f71808e_find(int sioaddr
)
699 int err
= superio_enter(sioaddr
);
703 devid
= superio_inw(sioaddr
, SIO_REG_MANID
);
704 if (devid
!= SIO_FINTEK_ID
) {
705 pr_debug(DRVNAME
": Not a Fintek device\n");
710 devid
= force_id
? force_id
: superio_inw(sioaddr
, SIO_REG_DEVID
);
713 watchdog
.type
= f71808fg
;
716 watchdog
.type
= f71882fg
;
719 watchdog
.type
= f71889fg
;
722 /* These have a watchdog, though it isn't implemented (yet). */
726 /* Confirmed (by datasheet) not to have a watchdog. */
730 printk(KERN_INFO DRVNAME
": Unrecognized Fintek device: %04x\n",
731 (unsigned int)devid
);
736 printk(KERN_INFO DRVNAME
": Found %s watchdog chip, revision %d\n",
737 f71808e_names
[watchdog
.type
],
738 (int)superio_inb(sioaddr
, SIO_REG_DEVREV
));
740 superio_exit(sioaddr
);
744 static int __init
f71808e_init(void)
746 static const unsigned short addrs
[] = { 0x2e, 0x4e };
750 for (i
= 0; i
< ARRAY_SIZE(addrs
); i
++) {
751 err
= f71808e_find(addrs
[i
]);
755 if (i
== ARRAY_SIZE(addrs
))
758 return watchdog_init(addrs
[i
]);
761 static void __exit
f71808e_exit(void)
763 if (watchdog_is_running()) {
764 printk(KERN_WARNING DRVNAME
765 ": Watchdog timer still running, stopping it\n");
768 misc_deregister(&watchdog_miscdev
);
769 unregister_reboot_notifier(&watchdog_notifier
);
772 MODULE_DESCRIPTION("F71808E Watchdog Driver");
773 MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>");
774 MODULE_LICENSE("GPL");
776 module_init(f71808e_init
);
777 module_exit(f71808e_exit
);