Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[safe/jmp/linux-2.6] / drivers / watchdog / it8712f_wdt.c
index 1b6d7d1..daed48d 100644 (file)
@@ -7,7 +7,8 @@
  *
  *     drivers/char/watchdog/scx200_wdt.c
  *     drivers/hwmon/it87.c
- *     IT8712F EC-LPC I/O Preliminary Specification 0.9.2.pdf
+ *     IT8712F EC-LPC I/O Preliminary Specification 0.8.2
+ *     IT8712F EC-LPC I/O Preliminary Specification 0.9.3
  *
  *     This program is free software; you can redistribute it and/or
  *     modify it under the terms of the GNU General Public License as
@@ -29,9 +30,8 @@
 #include <linux/fs.h>
 #include <linux/pci.h>
 #include <linux/spinlock.h>
-
-#include <asm/uaccess.h>
-#include <asm/io.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
 
 #define NAME "it8712f_wdt"
 
@@ -40,6 +40,7 @@ MODULE_DESCRIPTION("IT8712F Watchdog Driver");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
 
+static int max_units = 255;
 static int margin = 60;                /* in seconds */
 module_param(margin, int, 0);
 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
@@ -48,9 +49,10 @@ static int nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, int, 0);
 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
 
-static struct semaphore it8712f_wdt_sem;
+static unsigned long wdt_open;
 static unsigned expect_close;
 static spinlock_t io_lock;
+static unsigned char revision;
 
 /* Dog Food address - We use the game port address */
 static unsigned short address;
@@ -83,22 +85,19 @@ static unsigned short address;
 #define WDT_OUT_PWROK  0x10
 #define WDT_OUT_KRST   0x40
 
-static int
-superio_inb(int reg)
+static int superio_inb(int reg)
 {
        outb(reg, REG);
        return inb(VAL);
 }
 
-static void
-superio_outb(int val, int reg)
+static void superio_outb(int val, int reg)
 {
        outb(reg, REG);
        outb(val, VAL);
 }
 
-static int
-superio_inw(int reg)
+static int superio_inw(int reg)
 {
        int val;
        outb(reg++, REG);
@@ -108,15 +107,13 @@ superio_inw(int reg)
        return val;
 }
 
-static inline void
-superio_select(int ldn)
+static inline void superio_select(int ldn)
 {
        outb(LDN, REG);
        outb(ldn, VAL);
 }
 
-static inline void
-superio_enter(void)
+static inline void superio_enter(void)
 {
        spin_lock(&io_lock);
        outb(0x87, REG);
@@ -125,37 +122,49 @@ superio_enter(void)
        outb(0x55, REG);
 }
 
-static inline void
-superio_exit(void)
+static inline void superio_exit(void)
 {
        outb(0x02, REG);
        outb(0x02, VAL);
        spin_unlock(&io_lock);
 }
 
-static inline void
-it8712f_wdt_ping(void)
+static inline void it8712f_wdt_ping(void)
 {
        inb(address);
 }
 
-static void
-it8712f_wdt_update_margin(void)
+static void it8712f_wdt_update_margin(void)
 {
        int config = WDT_OUT_KRST | WDT_OUT_PWROK;
-
-       printk(KERN_INFO NAME ": timer margin %d seconds\n", margin);
-
-       /* The timeout register only has 8bits wide */
-       if (margin < 256)
-               config |= WDT_UNIT_SEC; /* else UNIT are MINUTES */
+       int units = margin;
+
+       /* Switch to minutes precision if the configured margin
+        * value does not fit within the register width.
+        */
+       if (units <= max_units) {
+               config |= WDT_UNIT_SEC; /* else UNIT is MINUTES */
+               printk(KERN_INFO NAME ": timer margin %d seconds\n", units);
+       } else {
+               units /= 60;
+               printk(KERN_INFO NAME ": timer margin %d minutes\n", units);
+       }
        superio_outb(config, WDT_CONFIG);
 
-       superio_outb((margin > 255) ? (margin / 60) : margin, WDT_TIMEOUT);
+       if (revision >= 0x08)
+               superio_outb(units >> 8, WDT_TIMEOUT + 1);
+       superio_outb(units, WDT_TIMEOUT);
+}
+
+static int it8712f_wdt_get_status(void)
+{
+       if (superio_inb(WDT_CONTROL) & 0x01)
+               return WDIOF_CARDRESET;
+       else
+               return 0;
 }
 
-static void
-it8712f_wdt_enable(void)
+static void it8712f_wdt_enable(void)
 {
        printk(KERN_DEBUG NAME ": enabling watchdog timer\n");
        superio_enter();
@@ -170,8 +179,7 @@ it8712f_wdt_enable(void)
        it8712f_wdt_ping();
 }
 
-static void
-it8712f_wdt_disable(void)
+static void it8712f_wdt_disable(void)
 {
        printk(KERN_DEBUG NAME ": disabling watchdog timer\n");
 
@@ -180,13 +188,14 @@ it8712f_wdt_disable(void)
 
        superio_outb(0, WDT_CONFIG);
        superio_outb(0, WDT_CONTROL);
+       if (revision >= 0x08)
+               superio_outb(0, WDT_TIMEOUT + 1);
        superio_outb(0, WDT_TIMEOUT);
 
        superio_exit();
 }
 
-static int
-it8712f_wdt_notify(struct notifier_block *this,
+static int it8712f_wdt_notify(struct notifier_block *this,
                    unsigned long code, void *unused)
 {
        if (code == SYS_HALT || code == SYS_POWER_OFF)
@@ -200,9 +209,8 @@ static struct notifier_block it8712f_wdt_notifier = {
        .notifier_call = it8712f_wdt_notify,
 };
 
-static ssize_t
-it8712f_wdt_write(struct file *file, const char __user *data,
-       size_t len, loff_t *ppos)
+static ssize_t it8712f_wdt_write(struct file *file, const char __user *data,
+                                       size_t len, loff_t *ppos)
 {
        /* check for a magic close character */
        if (len) {
@@ -213,7 +221,7 @@ it8712f_wdt_write(struct file *file, const char __user *data,
                expect_close = 0;
                for (i = 0; i < len; ++i) {
                        char c;
-                       if (get_user(c, data+i))
+                       if (get_user(c, data + i))
                                return -EFAULT;
                        if (c == 'V')
                                expect_close = 42;
@@ -223,38 +231,46 @@ it8712f_wdt_write(struct file *file, const char __user *data,
        return len;
 }
 
-static int
-it8712f_wdt_ioctl(struct inode *inode, struct file *file,
-       unsigned int cmd, unsigned long arg)
+static long it8712f_wdt_ioctl(struct file *file, unsigned int cmd,
+                                                       unsigned long arg)
 {
        void __user *argp = (void __user *)arg;
        int __user *p = argp;
        static struct watchdog_info ident = {
                .identity = "IT8712F Watchdog",
                .firmware_version = 1,
-               .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
+               .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
+                                               WDIOF_MAGICCLOSE,
        };
-       int new_margin;
+       int value;
 
        switch (cmd) {
-       default:
-               return -ENOTTY;
        case WDIOC_GETSUPPORT:
                if (copy_to_user(argp, &ident, sizeof(ident)))
                        return -EFAULT;
                return 0;
        case WDIOC_GETSTATUS:
+               superio_enter();
+               superio_select(LDN_GPIO);
+
+               value = it8712f_wdt_get_status();
+
+               superio_exit();
+
+               return put_user(value, p);
        case WDIOC_GETBOOTSTATUS:
                return put_user(0, p);
        case WDIOC_KEEPALIVE:
                it8712f_wdt_ping();
                return 0;
        case WDIOC_SETTIMEOUT:
-               if (get_user(new_margin, p))
+               if (get_user(value, p))
                        return -EFAULT;
-               if (new_margin < 1)
+               if (value < 1)
+                       return -EINVAL;
+               if (value > (max_units * 60))
                        return -EINVAL;
-               margin = new_margin;
+               margin = value;
                superio_enter();
                superio_select(LDN_GPIO);
 
@@ -262,26 +278,26 @@ it8712f_wdt_ioctl(struct inode *inode, struct file *file,
 
                superio_exit();
                it8712f_wdt_ping();
+               /* Fall through */
        case WDIOC_GETTIMEOUT:
                if (put_user(margin, p))
                        return -EFAULT;
                return 0;
+       default:
+               return -ENOTTY;
        }
 }
 
-static int
-it8712f_wdt_open(struct inode *inode, struct file *file)
+static int it8712f_wdt_open(struct inode *inode, struct file *file)
 {
        /* only allow one at a time */
-       if (down_trylock(&it8712f_wdt_sem))
+       if (test_and_set_bit(0, &wdt_open))
                return -EBUSY;
        it8712f_wdt_enable();
-
        return nonseekable_open(inode, file);
 }
 
-static int
-it8712f_wdt_release(struct inode *inode, struct file *file)
+static int it8712f_wdt_release(struct inode *inode, struct file *file)
 {
        if (expect_close != 42) {
                printk(KERN_WARNING NAME
@@ -291,7 +307,7 @@ it8712f_wdt_release(struct inode *inode, struct file *file)
                it8712f_wdt_disable();
        }
        expect_close = 0;
-       up(&it8712f_wdt_sem);
+       clear_bit(0, &wdt_open);
 
        return 0;
 }
@@ -300,7 +316,7 @@ static const struct file_operations it8712f_wdt_fops = {
        .owner = THIS_MODULE,
        .llseek = no_llseek,
        .write = it8712f_wdt_write,
-       .ioctl = it8712f_wdt_ioctl,
+       .unlocked_ioctl = it8712f_wdt_ioctl,
        .open = it8712f_wdt_open,
        .release = it8712f_wdt_release,
 };
@@ -311,8 +327,7 @@ static struct miscdevice it8712f_wdt_miscdev = {
        .fops = &it8712f_wdt_fops,
 };
 
-static int __init
-it8712f_wdt_find(unsigned short *address)
+static int __init it8712f_wdt_find(unsigned short *address)
 {
        int err = -ENODEV;
        int chip_type;
@@ -336,17 +351,25 @@ it8712f_wdt_find(unsigned short *address)
        }
 
        err = 0;
-       printk(KERN_DEBUG NAME ": Found IT%04xF chip revision %d - "
+       revision = superio_inb(DEVREV) & 0x0f;
+
+       /* Later revisions have 16-bit values per datasheet 0.9.1 */
+       if (revision >= 0x08)
+               max_units = 65535;
+
+       if (margin > (max_units * 60))
+               margin = (max_units * 60);
+
+       printk(KERN_INFO NAME ": Found IT%04xF chip revision %d - "
                "using DogFood address 0x%x\n",
-               chip_type, superio_inb(DEVREV) & 0x0f, *address);
+               chip_type, revision, *address);
 
 exit:
        superio_exit();
        return err;
 }
 
-static int __init
-it8712f_wdt_init(void)
+static int __init it8712f_wdt_init(void)
 {
        int err = 0;
 
@@ -362,8 +385,6 @@ it8712f_wdt_init(void)
 
        it8712f_wdt_disable();
 
-       sema_init(&it8712f_wdt_sem, 1);
-
        err = register_reboot_notifier(&it8712f_wdt_notifier);
        if (err) {
                printk(KERN_ERR NAME ": unable to register reboot notifier\n");
@@ -388,8 +409,7 @@ out:
        return err;
 }
 
-static void __exit
-it8712f_wdt_exit(void)
+static void __exit it8712f_wdt_exit(void)
 {
        misc_deregister(&it8712f_wdt_miscdev);
        unregister_reboot_notifier(&it8712f_wdt_notifier);