]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - drivers/char/watchdog/at32ap700x_wdt.c
[WATCHDOG] at32ap700x_wdt.c - Add nowayout + MAGICCLOSE features
[mv-sheeva.git] / drivers / char / watchdog / at32ap700x_wdt.c
index f08ea9493d2fbf38597c3a010700a320ce5a79b4..fcd55c600b87731c93e1f4f3becdcbfcda6897a7 100644 (file)
 #include <linux/io.h>
 
 #define TIMEOUT_MIN            1
-#define TIMEOUT_DEFAULT                CONFIG_AT32AP700X_WDT_TIMEOUT
 #define TIMEOUT_MAX            2
+#define TIMEOUT_DEFAULT                TIMEOUT_MAX
+
+/* module parameters */
+static int timeout =  TIMEOUT_DEFAULT;
+module_param(timeout, int, 0);
+MODULE_PARM_DESC(timeout,
+               "Timeout value. Limited to be 1 or 2 seconds. (default="
+               __MODULE_STRING(TIMEOUT_DEFAULT) ")");
+
+static int nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, int, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
+               __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
 /* Watchdog registers and write/read macro */
 #define WDT_CTRL               0x00
 #define WDT_CLR                        0x04
 
 #define WDT_BIT(name)          (1 << WDT_##name)
-#define WDT_BF(name,value)     ((value) << WDT_##name)
+#define WDT_BF(name, value)    ((value) << WDT_##name)
 
-#define wdt_readl(dev,reg)                             \
+#define wdt_readl(dev, reg)                            \
        __raw_readl((dev)->regs + WDT_##reg)
-#define wdt_writel(dev,reg,value)                      \
+#define wdt_writel(dev, reg, value)                    \
        __raw_writel((value), (dev)->regs + WDT_##reg)
 
 struct wdt_at32ap700x {
@@ -47,11 +59,12 @@ struct wdt_at32ap700x {
 };
 
 static struct wdt_at32ap700x *wdt;
+static char expect_release;
 
 /*
  * Disable the watchdog.
  */
-static void inline at32_wdt_stop(void)
+static inline void at32_wdt_stop(void)
 {
        unsigned long psel = wdt_readl(wdt, CTRL) & WDT_BF(CTRL_PSEL, 0x0f);
        wdt_writel(wdt, CTRL, psel | WDT_BF(CTRL_KEY, 0x55));
@@ -61,7 +74,7 @@ static void inline at32_wdt_stop(void)
 /*
  * Enable and reset the watchdog.
  */
-static void inline at32_wdt_start(void)
+static inline void at32_wdt_start(void)
 {
        /* 0xf is 2^16 divider = 2 sec, 0xe is 2^15 divider = 1 sec */
        unsigned long psel = (wdt->timeout > 1) ? 0xf : 0xe;
@@ -77,7 +90,7 @@ static void inline at32_wdt_start(void)
 /*
  * Pat the watchdog timer.
  */
-static void inline at32_wdt_pat(void)
+static inline void at32_wdt_pat(void)
 {
        wdt_writel(wdt, CLR, 0x42);
 }
@@ -95,15 +108,19 @@ static int at32_wdt_open(struct inode *inode, struct file *file)
 }
 
 /*
- * Close the watchdog device. If CONFIG_WATCHDOG_NOWAYOUT is _not_ defined then
- * the watchdog is also disabled.
+ * Close the watchdog device.
  */
 static int at32_wdt_close(struct inode *inode, struct file *file)
 {
-#ifndef CONFIG_WATCHDOG_NOWAYOUT
-       at32_wdt_stop();
-#endif
+       if (expect_release == 42) {
+               at32_wdt_stop();
+       } else {
+               dev_dbg(wdt->miscdev.parent,
+                       "Unexpected close, not stopping watchdog!\n");
+               at32_wdt_pat();
+       }
        clear_bit(1, &wdt->users);
+       expect_release = 0;
        return 0;
 }
 
@@ -129,7 +146,9 @@ static int at32_wdt_settimeout(int time)
 
 static struct watchdog_info at32_wdt_info = {
        .identity       = "at32ap700x watchdog",
-       .options        = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
+       .options        = WDIOF_SETTIMEOUT |
+                         WDIOF_KEEPALIVEPING |
+                         WDIOF_MAGICCLOSE,
 };
 
 /*
@@ -184,10 +203,35 @@ static int at32_wdt_ioctl(struct inode *inode, struct file *file,
        return ret;
 }
 
-static ssize_t at32_wdt_write(struct file *file, const char *data, size_t len,
-                               loff_t *ppos)
+static ssize_t at32_wdt_write(struct file *file, const char __user *data,
+                               size_t len, loff_t *ppos)
 {
-       at32_wdt_pat();
+       /* See if we got the magic character 'V' and reload the timer */
+       if (len) {
+               if (!nowayout) {
+                       size_t i;
+
+                       /*
+                        * note: just in case someone wrote the magic
+                        * character five months ago...
+                        */
+                       expect_release = 0;
+
+                       /*
+                        * scan to see whether or not we got the magic
+                        * character
+                        */
+                       for (i = 0; i != len; i++) {
+                               char c;
+                               if (get_user(c, data+i))
+                                       return -EFAULT;
+                               if (c == 'V')
+                                       expect_release = 42;
+                       }
+               }
+               /* someone wrote to us, we should pat the watchdog */
+               at32_wdt_pat();
+       }
        return len;
 }
 
@@ -223,31 +267,40 @@ static int __init at32_wdt_probe(struct platform_device *pdev)
        }
 
        wdt->regs = ioremap(regs->start, regs->end - regs->start + 1);
+       if (!wdt->regs) {
+               ret = -ENOMEM;
+               dev_dbg(&pdev->dev, "could not map I/O memory\n");
+               goto err_free;
+       }
        wdt->users = 0;
        wdt->miscdev.minor = WATCHDOG_MINOR;
        wdt->miscdev.name = "watchdog";
        wdt->miscdev.fops = &at32_wdt_fops;
 
-       if (at32_wdt_settimeout(TIMEOUT_DEFAULT)) {
-               at32_wdt_settimeout(TIMEOUT_MAX);
+       if (at32_wdt_settimeout(timeout)) {
+               at32_wdt_settimeout(TIMEOUT_DEFAULT);
                dev_dbg(&pdev->dev,
                        "default timeout invalid, set to %d sec.\n",
-                       TIMEOUT_MAX);
+                       TIMEOUT_DEFAULT);
        }
 
        ret = misc_register(&wdt->miscdev);
        if (ret) {
                dev_dbg(&pdev->dev, "failed to register wdt miscdev\n");
-               goto err_register;
+               goto err_iounmap;
        }
 
        platform_set_drvdata(pdev, wdt);
        wdt->miscdev.parent = &pdev->dev;
-       dev_info(&pdev->dev, "AT32AP700X WDT at 0x%p\n", wdt->regs);
+       dev_info(&pdev->dev,
+               "AT32AP700X WDT at 0x%p, timeout %d sec (nowayout=%d)\n",
+               wdt->regs, wdt->timeout, nowayout);
 
        return 0;
 
-err_register:
+err_iounmap:
+       iounmap(wdt->regs);
+err_free:
        kfree(wdt);
        wdt = NULL;
        return ret;
@@ -256,7 +309,12 @@ err_register:
 static int __exit at32_wdt_remove(struct platform_device *pdev)
 {
        if (wdt && platform_get_drvdata(pdev) == wdt) {
+               /* Stop the timer before we leave */
+               if (!nowayout)
+                       at32_wdt_stop();
+
                misc_deregister(&wdt->miscdev);
+               iounmap(wdt->regs);
                kfree(wdt);
                wdt = NULL;
                platform_set_drvdata(pdev, NULL);