]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/char/watchdog/mv64x60_wdt.c
[WATCHDOG] mv64x60_wdt: Add WDIOC_SETOPTIONS ioctl support
[linux-2.6-omap-h63xx.git] / drivers / char / watchdog / mv64x60_wdt.c
index 20a6cbb0fbb8b3f3acc4f546e7163f84c3fb17e4..7b4812776382e0c0bf7dae0aeb976402509f3462 100644 (file)
 #include <linux/watchdog.h>
 #include <linux/platform_device.h>
 
-#include <asm/mv64x60.h>
+#include <linux/mv643xx.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
 
+#define MV64x60_WDT_WDC_OFFSET 0
+
 /* MV64x60 WDC (config) register access definitions */
 #define MV64x60_WDC_CTL1_MASK  (3 << 24)
 #define MV64x60_WDC_CTL1(val)  ((val & 3) << 24)
 
 static unsigned long wdt_flags;
 static int wdt_status;
-static void __iomem *mv64x60_regs;
+static void __iomem *mv64x60_wdt_regs;
 static int mv64x60_wdt_timeout;
+static unsigned int bus_clk;
 
 static void mv64x60_wdt_reg_write(u32 val)
 {
        /* Allow write only to CTL1 / CTL2 fields, retaining values in
         * other fields.
         */
-       u32 data = readl(mv64x60_regs + MV64x60_WDT_WDC);
+       u32 data = readl(mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
        data &= ~(MV64x60_WDC_CTL1_MASK | MV64x60_WDC_CTL2_MASK);
        data |= val;
-       writel(data, mv64x60_regs + MV64x60_WDT_WDC);
+       writel(data, mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
 }
 
 static void mv64x60_wdt_service(void)
@@ -80,6 +83,18 @@ static void mv64x60_wdt_handler_enable(void)
        }
 }
 
+static void mv64x60_wdt_set_timeout(int timeout)
+{
+       /* maximum bus cycle count is 0xFFFFFFFF */
+       if (timeout > 0xFFFFFFFF / bus_clk)
+               timeout = 0xFFFFFFFF / bus_clk;
+
+       mv64x60_wdt_timeout = timeout;
+       writel((timeout * bus_clk) >> 8,
+              mv64x60_wdt_regs + MV64x60_WDT_WDC_OFFSET);
+       mv64x60_wdt_service();
+}
+
 static int mv64x60_wdt_open(struct inode *inode, struct file *file)
 {
        if (test_and_set_bit(MV64x60_WDOG_FLAG_OPENED, &wdt_flags))
@@ -88,9 +103,7 @@ static int mv64x60_wdt_open(struct inode *inode, struct file *file)
        mv64x60_wdt_service();
        mv64x60_wdt_handler_enable();
 
-       nonseekable_open(inode, file);
-
-       return 0;
+       return nonseekable_open(inode, file);
 }
 
 static int mv64x60_wdt_release(struct inode *inode, struct file *file)
@@ -119,9 +132,11 @@ static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
                             unsigned int cmd, unsigned long arg)
 {
        int timeout;
+       int options;
        void __user *argp = (void __user *)arg;
        static struct watchdog_info info = {
-               .options = WDIOF_KEEPALIVEPING,
+               .options =      WDIOF_SETTIMEOUT        |
+                               WDIOF_KEEPALIVEPING,
                .firmware_version = 0,
                .identity = "MV64x60 watchdog",
        };
@@ -143,7 +158,15 @@ static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
                return -EOPNOTSUPP;
 
        case WDIOC_SETOPTIONS:
-               return -EOPNOTSUPP;
+               if (get_user(options, (int __user *)argp))
+                       return -EFAULT;
+
+               if (options & WDIOS_DISABLECARD)
+                       mv64x60_wdt_handler_disable();
+
+               if (options & WDIOS_ENABLECARD)
+                       mv64x60_wdt_handler_enable();
+               break;
 
        case WDIOC_KEEPALIVE:
                mv64x60_wdt_service();
@@ -151,22 +174,24 @@ static int mv64x60_wdt_ioctl(struct inode *inode, struct file *file,
                break;
 
        case WDIOC_SETTIMEOUT:
-               return -EOPNOTSUPP;
+               if (get_user(timeout, (int __user *)argp))
+                       return -EFAULT;
+               mv64x60_wdt_set_timeout(timeout);
+               /* Fall through */
 
        case WDIOC_GETTIMEOUT:
-               timeout = mv64x60_wdt_timeout * HZ;
-               if (put_user(timeout, (int __user *)argp))
+               if (put_user(mv64x60_wdt_timeout, (int __user *)argp))
                        return -EFAULT;
                break;
 
        default:
-               return -ENOIOCTLCMD;
+               return -ENOTTY;
        }
 
        return 0;
 }
 
-static struct file_operations mv64x60_wdt_fops = {
+static const struct file_operations mv64x60_wdt_fops = {
        .owner = THIS_MODULE,
        .llseek = no_llseek,
        .write = mv64x60_wdt_write,
@@ -184,18 +209,31 @@ static struct miscdevice mv64x60_wdt_miscdev = {
 static int __devinit mv64x60_wdt_probe(struct platform_device *dev)
 {
        struct mv64x60_wdt_pdata *pdata = dev->dev.platform_data;
-       int bus_clk = 133;
+       struct resource *r;
+       int timeout = 10;
 
-       mv64x60_wdt_timeout = 10;
+       bus_clk = 133;                  /* in MHz */
        if (pdata) {
-               mv64x60_wdt_timeout = pdata->timeout;
+               timeout = pdata->timeout;
                bus_clk = pdata->bus_clk;
        }
 
-       mv64x60_regs = mv64x60_get_bridge_vbase();
+       /* Since bus_clk is truncated MHz, actual frequency could be
+        * up to 1MHz higher.  Round up, since it's better to time out
+        * too late than too soon.
+        */
+       bus_clk++;
+       bus_clk *= 1000000;             /* convert to Hz */
+
+       r = platform_get_resource(dev, IORESOURCE_MEM, 0);
+       if (!r)
+               return -ENODEV;
+
+       mv64x60_wdt_regs = ioremap(r->start, r->end - r->start + 1);
+       if (mv64x60_wdt_regs == NULL)
+               return -ENOMEM;
 
-       writel((mv64x60_wdt_timeout * (bus_clk * 1000000)) >> 8,
-              mv64x60_regs + MV64x60_WDT_WDC);
+       mv64x60_wdt_set_timeout(timeout);
 
        return misc_register(&mv64x60_wdt_miscdev);
 }
@@ -207,6 +245,8 @@ static int __devexit mv64x60_wdt_remove(struct platform_device *dev)
        mv64x60_wdt_service();
        mv64x60_wdt_handler_disable();
 
+       iounmap(mv64x60_wdt_regs);
+
        return 0;
 }
 
@@ -219,40 +259,16 @@ static struct platform_driver mv64x60_wdt_driver = {
        },
 };
 
-static struct platform_device *mv64x60_wdt_dev;
-
 static int __init mv64x60_wdt_init(void)
 {
-       int ret;
-
        printk(KERN_INFO "MV64x60 watchdog driver\n");
 
-       mv64x60_wdt_dev = platform_device_alloc(MV64x60_WDT_NAME, -1);
-       if (!mv64x60_wdt_dev) {
-               ret = -ENOMEM;
-               goto out;
-       }
-
-       ret = platform_device_add(mv64x60_wdt_dev);
-       if (ret) {
-               platform_device_put(mv64x60_wdt_dev);
-               goto out;
-       }
-
-       ret = platform_driver_register(&mv64x60_wdt_driver);
-       if (ret) {
-               platform_device_unregister(mv64x60_wdt_dev);
-               goto out;
-       }
-
- out:
-       return ret;
+       return platform_driver_register(&mv64x60_wdt_driver);
 }
 
 static void __exit mv64x60_wdt_exit(void)
 {
        platform_driver_unregister(&mv64x60_wdt_driver);
-       platform_device_unregister(mv64x60_wdt_dev);
 }
 
 module_init(mv64x60_wdt_init);