GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / watchdog / f71808e_wdt.c
blob7e5c266cda4804b39e0acef3338ab0c67ba37c63
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> *
5 * *
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. *
10 * *
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. *
15 * *
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>
23 #include <linux/fs.h>
24 #include <linux/init.h>
25 #include <linux/io.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
68 /* Default values */
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
72 watchdog signal */
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[] = {
104 "f71808fg",
105 "f71858fg",
106 "f71862fg",
107 "f71882fg",
108 "f71889fg",
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;
123 enum chips type;
124 unsigned long opened;
125 struct mutex lock;
126 char expect_close;
127 struct watchdog_info ident;
129 unsigned short timeout;
130 u8 timer_val; /* content for the wd_time register */
131 char minutes_mode;
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)
144 outb(reg, base);
145 return inb(base + 1);
148 static int superio_inw(int base, int reg)
150 int val;
151 val = superio_inb(base, reg) << 8;
152 val |= superio_inb(base, reg + 1);
153 return val;
156 static inline void superio_outb(int base, int reg, u8 val)
158 outb(reg, base);
159 outb(val, base + 1);
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",
181 (int)base);
182 return -EBUSY;
185 /* according to the datasheet the key must be send twice! */
186 outb(SIO_UNLOCK_KEY, base);
187 outb(SIO_UNLOCK_KEY, base);
189 return 0;
192 static inline void superio_select(int base, int ld)
194 outb(SIO_REG_LDSEL, base);
195 outb(ld, base + 1);
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)
206 if (timeout <= 0
207 || timeout > max_timeout) {
208 printk(KERN_ERR DRVNAME ": watchdog timeout out of range\n");
209 return -EINVAL;
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;
218 } else {
219 watchdog.timer_val = timeout;
220 watchdog.minutes_mode = false;
223 mutex_unlock(&watchdog.lock);
225 return 0;
228 static int watchdog_set_pulse_width(unsigned int pw)
230 int err = 0;
232 mutex_lock(&watchdog.lock);
234 if (pw <= 1) {
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;
242 } else {
243 printk(KERN_ERR DRVNAME ": pulse width out of range\n");
244 err = -EINVAL;
245 goto exit_unlock;
248 watchdog.pulse_mode = pw;
250 exit_unlock:
251 mutex_unlock(&watchdog.lock);
252 return err;
255 static int watchdog_keepalive(void)
257 int err = 0;
259 mutex_lock(&watchdog.lock);
260 err = superio_enter(watchdog.sioaddr);
261 if (err)
262 goto exit_unlock;
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);
269 else
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,
276 watchdog.timer_val);
278 superio_exit(watchdog.sioaddr);
280 exit_unlock:
281 mutex_unlock(&watchdog.lock);
282 return err;
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();
289 if (err)
290 return err;
292 mutex_lock(&watchdog.lock);
293 err = superio_enter(watchdog.sioaddr);
294 if (err)
295 goto exit_unlock;
296 superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT);
298 /* Watchdog pin configuration */
299 switch (watchdog.type) {
300 case f71808fg:
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);
304 break;
306 case f71882fg:
307 /* Set pin 56 to WDTRST# */
308 superio_set_bit(watchdog.sioaddr, 0x29, 1);
309 break;
311 default:
313 * 'default' label to shut up the compiler and catch
314 * programmer errors
316 err = -ENODEV;
317 goto exit_superio;
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,
339 wdt_conf);
340 } else {
341 /* Select "level" output mode */
342 superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF,
343 F71808FG_FLAG_WD_PULSE);
346 exit_superio:
347 superio_exit(watchdog.sioaddr);
348 exit_unlock:
349 mutex_unlock(&watchdog.lock);
351 return err;
354 static int watchdog_stop(void)
356 int err = 0;
358 mutex_lock(&watchdog.lock);
359 err = superio_enter(watchdog.sioaddr);
360 if (err)
361 goto exit_unlock;
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);
369 exit_unlock:
370 mutex_unlock(&watchdog.lock);
372 return err;
375 static int watchdog_get_status(void)
377 int status = 0;
379 mutex_lock(&watchdog.lock);
380 status = (watchdog.caused_reboot) ? WDIOF_CARDRESET : 0;
381 mutex_unlock(&watchdog.lock);
383 return status;
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))
396 goto exit_unlock;
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);
405 exit_unlock:
406 mutex_unlock(&watchdog.lock);
407 return is_running;
410 /* /dev/watchdog api */
412 static int watchdog_open(struct inode *inode, struct file *file)
414 int err;
416 /* If the watchdog is alive we don't need to start it again */
417 if (test_and_set_bit(0, &watchdog.opened))
418 return -EBUSY;
420 err = watchdog_start();
421 if (err) {
422 clear_bit(0, &watchdog.opened);
423 return err;
426 if (nowayout)
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) {
442 watchdog_stop();
444 return 0;
448 * watchdog_write:
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)
461 if (count) {
462 if (!nowayout) {
463 size_t i;
465 /* In case it was set long ago */
466 bool expect_close = false;
468 for (i = 0; i != count; i++) {
469 char c;
470 if (get_user(c, buf + i))
471 return -EFAULT;
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();
484 return count;
488 * watchdog_ioctl:
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,
498 unsigned long arg)
500 int status;
501 int new_options;
502 int new_timeout;
503 union {
504 struct watchdog_info __user *ident;
505 int __user *i;
506 } uarg;
508 uarg.i = (int __user *)arg;
510 switch (cmd) {
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();
517 if (status < 0)
518 return 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))
526 return -EFAULT;
528 if (new_options & WDIOS_DISABLECARD)
529 watchdog_stop();
531 if (new_options & WDIOS_ENABLECARD)
532 return watchdog_start();
535 case WDIOC_KEEPALIVE:
536 watchdog_keepalive();
537 return 0;
539 case WDIOC_SETTIMEOUT:
540 if (get_user(new_timeout, uarg.i))
541 return -EFAULT;
543 if (watchdog_set_timeout(new_timeout))
544 return -EINVAL;
546 watchdog_keepalive();
547 /* Fall */
549 case WDIOC_GETTIMEOUT:
550 return put_user(watchdog.timeout, uarg.i);
552 default:
553 return -ENOTTY;
558 static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
559 void *unused)
561 if (code == SYS_DOWN || code == SYS_HALT)
562 watchdog_stop();
563 return NOTIFY_DONE;
566 static const struct file_operations watchdog_fops = {
567 .owner = THIS_MODULE,
568 .llseek = no_llseek,
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,
577 .name = "watchdog",
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
594 | WDIOF_MAGICCLOSE
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);
602 if (err)
603 return err;
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);
612 if (err)
613 return err;
614 err = watchdog_set_pulse_width(pulse_width);
615 if (err)
616 return err;
618 err = register_reboot_notifier(&watchdog_notifier);
619 if (err)
620 return err;
622 err = misc_register(&watchdog_miscdev);
623 if (err) {
624 printk(KERN_ERR DRVNAME
625 ": cannot register miscdev on minor=%d\n",
626 watchdog_miscdev.minor);
627 goto exit_reboot;
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");
635 err = -EINVAL;
636 goto exit_miscdev;
639 err = watchdog_start();
640 if (err) {
641 printk(KERN_ERR DRVNAME
642 ": cannot start watchdog timer\n");
643 goto exit_miscdev;
646 mutex_lock(&watchdog.lock);
647 err = superio_enter(sioaddr);
648 if (err)
649 goto exit_unlock;
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));
658 } else {
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,
663 start_withtimeout);
666 superio_exit(sioaddr);
667 mutex_unlock(&watchdog.lock);
669 if (nowayout)
670 __module_get(THIS_MODULE);
672 printk(KERN_INFO DRVNAME
673 ": watchdog started with initial timeout of %u sec\n",
674 start_withtimeout);
677 return 0;
679 exit_unlock:
680 mutex_unlock(&watchdog.lock);
681 exit_miscdev:
682 misc_deregister(&watchdog_miscdev);
683 exit_reboot:
684 unregister_reboot_notifier(&watchdog_notifier);
686 return err;
689 static int __init f71808e_find(int sioaddr)
691 u16 devid;
692 int err = superio_enter(sioaddr);
693 if (err)
694 return err;
696 devid = superio_inw(sioaddr, SIO_REG_MANID);
697 if (devid != SIO_FINTEK_ID) {
698 pr_debug(DRVNAME ": Not a Fintek device\n");
699 err = -ENODEV;
700 goto exit;
703 devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
704 switch (devid) {
705 case SIO_F71808_ID:
706 watchdog.type = f71808fg;
707 break;
708 case SIO_F71882_ID:
709 watchdog.type = f71882fg;
710 break;
711 case SIO_F71862_ID:
712 case SIO_F71889_ID:
713 /* These have a watchdog, though it isn't implemented (yet). */
714 err = -ENOSYS;
715 goto exit;
716 case SIO_F71858_ID:
717 /* Confirmed (by datasheet) not to have a watchdog. */
718 err = -ENODEV;
719 goto exit;
720 default:
721 printk(KERN_INFO DRVNAME ": Unrecognized Fintek device: %04x\n",
722 (unsigned int)devid);
723 err = -ENODEV;
724 goto exit;
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));
730 exit:
731 superio_exit(sioaddr);
732 return err;
735 static int __init f71808e_init(void)
737 static const unsigned short addrs[] = { 0x2e, 0x4e };
738 int err = -ENODEV;
739 int i;
741 for (i = 0; i < ARRAY_SIZE(addrs); i++) {
742 err = f71808e_find(addrs[i]);
743 if (err == 0)
744 break;
746 if (i == ARRAY_SIZE(addrs))
747 return err;
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");
757 watchdog_stop();
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);