Commit 0dd6e484 authored by Axel Lin's avatar Axel Lin Committed by Wim Van Sebroeck

watchdog: orion_wdt: Convert driver to watchdog core

Convert orion_wdt driver to use watchdog framework API.
Signed-off-by: default avatarAxel Lin <axel.lin@gmail.com>
Tested-by: default avatarAndrew Lunn <andrew@lunn.ch>
Signed-off-by: default avatarWim Van Sebroeck <wim@iguana.be>
parent 6b761b29
...@@ -279,6 +279,7 @@ config DAVINCI_WATCHDOG ...@@ -279,6 +279,7 @@ config DAVINCI_WATCHDOG
config ORION_WATCHDOG config ORION_WATCHDOG
tristate "Orion watchdog" tristate "Orion watchdog"
depends on ARCH_ORION5X || ARCH_KIRKWOOD depends on ARCH_ORION5X || ARCH_KIRKWOOD
select WATCHDOG_CORE
help help
Say Y here if to include support for the watchdog timer Say Y here if to include support for the watchdog timer
in the Marvell Orion5x and Kirkwood ARM SoCs. in the Marvell Orion5x and Kirkwood ARM SoCs.
......
...@@ -16,22 +16,21 @@ ...@@ -16,22 +16,21 @@
#include <linux/moduleparam.h> #include <linux/moduleparam.h>
#include <linux/types.h> #include <linux/types.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h> #include <linux/miscdevice.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/watchdog.h> #include <linux/watchdog.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/uaccess.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/clk.h> #include <linux/clk.h>
#include <linux/err.h>
#include <mach/bridge-regs.h> #include <mach/bridge-regs.h>
/* /*
* Watchdog timer block registers. * Watchdog timer block registers.
*/ */
#define TIMER_CTRL 0x0000 #define TIMER_CTRL 0x0000
#define WDT_EN 0x0010 #define WDT_EN 0x0010
#define WDT_VAL 0x0024 #define WDT_VAL 0x0024
#define WDT_MAX_CYCLE_COUNT 0xffffffff #define WDT_MAX_CYCLE_COUNT 0xffffffff
...@@ -44,27 +43,27 @@ static unsigned int wdt_max_duration; /* (seconds) */ ...@@ -44,27 +43,27 @@ static unsigned int wdt_max_duration; /* (seconds) */
static struct clk *clk; static struct clk *clk;
static unsigned int wdt_tclk; static unsigned int wdt_tclk;
static void __iomem *wdt_reg; static void __iomem *wdt_reg;
static unsigned long wdt_status;
static DEFINE_SPINLOCK(wdt_lock); static DEFINE_SPINLOCK(wdt_lock);
static void orion_wdt_ping(void) static int orion_wdt_ping(struct watchdog_device *wdt_dev)
{ {
spin_lock(&wdt_lock); spin_lock(&wdt_lock);
/* Reload watchdog duration */ /* Reload watchdog duration */
writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL); writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
spin_unlock(&wdt_lock); spin_unlock(&wdt_lock);
return 0;
} }
static void orion_wdt_enable(void) static int orion_wdt_start(struct watchdog_device *wdt_dev)
{ {
u32 reg; u32 reg;
spin_lock(&wdt_lock); spin_lock(&wdt_lock);
/* Set watchdog duration */ /* Set watchdog duration */
writel(wdt_tclk * heartbeat, wdt_reg + WDT_VAL); writel(wdt_tclk * wdt_dev->timeout, wdt_reg + WDT_VAL);
/* Clear watchdog timer interrupt */ /* Clear watchdog timer interrupt */
reg = readl(BRIDGE_CAUSE); reg = readl(BRIDGE_CAUSE);
...@@ -82,9 +81,10 @@ static void orion_wdt_enable(void) ...@@ -82,9 +81,10 @@ static void orion_wdt_enable(void)
writel(reg, RSTOUTn_MASK); writel(reg, RSTOUTn_MASK);
spin_unlock(&wdt_lock); spin_unlock(&wdt_lock);
return 0;
} }
static void orion_wdt_disable(void) static int orion_wdt_stop(struct watchdog_device *wdt_dev)
{ {
u32 reg; u32 reg;
...@@ -101,139 +101,44 @@ static void orion_wdt_disable(void) ...@@ -101,139 +101,44 @@ static void orion_wdt_disable(void)
writel(reg, wdt_reg + TIMER_CTRL); writel(reg, wdt_reg + TIMER_CTRL);
spin_unlock(&wdt_lock); spin_unlock(&wdt_lock);
return 0;
} }
static int orion_wdt_get_timeleft(int *time_left) static unsigned int orion_wdt_get_timeleft(struct watchdog_device *wdt_dev)
{ {
unsigned int time_left;
spin_lock(&wdt_lock); spin_lock(&wdt_lock);
*time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk; time_left = readl(wdt_reg + WDT_VAL) / wdt_tclk;
spin_unlock(&wdt_lock); spin_unlock(&wdt_lock);
return 0;
}
static int orion_wdt_open(struct inode *inode, struct file *file) return time_left;
{
if (test_and_set_bit(WDT_IN_USE, &wdt_status))
return -EBUSY;
clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
orion_wdt_enable();
return nonseekable_open(inode, file);
} }
static ssize_t orion_wdt_write(struct file *file, const char *data, static int orion_wdt_set_timeout(struct watchdog_device *wdt_dev,
size_t len, loff_t *ppos) unsigned int timeout)
{ {
if (len) { wdt_dev->timeout = timeout;
if (!nowayout) {
size_t i;
clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
for (i = 0; i != len; i++) {
char c;
if (get_user(c, data + i))
return -EFAULT;
if (c == 'V')
set_bit(WDT_OK_TO_CLOSE, &wdt_status);
}
}
orion_wdt_ping();
}
return len;
}
static int orion_wdt_settimeout(int new_time)
{
if ((new_time <= 0) || (new_time > wdt_max_duration))
return -EINVAL;
/* Set new watchdog time to be used when
* orion_wdt_enable() or orion_wdt_ping() is called. */
heartbeat = new_time;
return 0; return 0;
} }
static const struct watchdog_info ident = { static const struct watchdog_info orion_wdt_info = {
.options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
WDIOF_KEEPALIVEPING, .identity = "Orion Watchdog",
.identity = "Orion Watchdog",
}; };
static long orion_wdt_ioctl(struct file *file, unsigned int cmd, static const struct watchdog_ops orion_wdt_ops = {
unsigned long arg) .owner = THIS_MODULE,
{ .start = orion_wdt_start,
int ret = -ENOTTY; .stop = orion_wdt_stop,
int time; .ping = orion_wdt_ping,
.set_timeout = orion_wdt_set_timeout,
switch (cmd) { .get_timeleft = orion_wdt_get_timeleft,
case WDIOC_GETSUPPORT:
ret = copy_to_user((struct watchdog_info *)arg, &ident,
sizeof(ident)) ? -EFAULT : 0;
break;
case WDIOC_GETSTATUS:
case WDIOC_GETBOOTSTATUS:
ret = put_user(0, (int *)arg);
break;
case WDIOC_KEEPALIVE:
orion_wdt_ping();
ret = 0;
break;
case WDIOC_SETTIMEOUT:
ret = get_user(time, (int *)arg);
if (ret)
break;
if (orion_wdt_settimeout(time)) {
ret = -EINVAL;
break;
}
orion_wdt_ping();
/* Fall through */
case WDIOC_GETTIMEOUT:
ret = put_user(heartbeat, (int *)arg);
break;
case WDIOC_GETTIMELEFT:
if (orion_wdt_get_timeleft(&time)) {
ret = -EINVAL;
break;
}
ret = put_user(time, (int *)arg);
break;
}
return ret;
}
static int orion_wdt_release(struct inode *inode, struct file *file)
{
if (test_bit(WDT_OK_TO_CLOSE, &wdt_status))
orion_wdt_disable();
else
pr_crit("Device closed unexpectedly - timer will not stop\n");
clear_bit(WDT_IN_USE, &wdt_status);
clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
return 0;
}
static const struct file_operations orion_wdt_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.write = orion_wdt_write,
.unlocked_ioctl = orion_wdt_ioctl,
.open = orion_wdt_open,
.release = orion_wdt_release,
}; };
static struct miscdevice orion_wdt_miscdev = { static struct watchdog_device orion_wdt = {
.minor = WATCHDOG_MINOR, .info = &orion_wdt_info,
.name = "watchdog", .ops = &orion_wdt_ops,
.fops = &orion_wdt_fops,
}; };
static int __devinit orion_wdt_probe(struct platform_device *pdev) static int __devinit orion_wdt_probe(struct platform_device *pdev)
...@@ -241,29 +146,34 @@ static int __devinit orion_wdt_probe(struct platform_device *pdev) ...@@ -241,29 +146,34 @@ static int __devinit orion_wdt_probe(struct platform_device *pdev)
struct resource *res; struct resource *res;
int ret; int ret;
clk = clk_get(&pdev->dev, NULL); clk = devm_clk_get(&pdev->dev, NULL);
if (IS_ERR(clk)) { if (IS_ERR(clk)) {
printk(KERN_ERR "Orion Watchdog missing clock\n"); dev_err(&pdev->dev, "Orion Watchdog missing clock\n");
return -ENODEV; return -ENODEV;
} }
clk_prepare_enable(clk); clk_prepare_enable(clk);
wdt_tclk = clk_get_rate(clk); wdt_tclk = clk_get_rate(clk);
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
wdt_reg = devm_ioremap(&pdev->dev, res->start, resource_size(res));
wdt_reg = ioremap(res->start, resource_size(res)); if (!wdt_reg)
return -ENOMEM;
if (orion_wdt_miscdev.parent)
return -EBUSY;
orion_wdt_miscdev.parent = &pdev->dev;
wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk; wdt_max_duration = WDT_MAX_CYCLE_COUNT / wdt_tclk;
if (orion_wdt_settimeout(heartbeat))
if ((heartbeat < 1) || (heartbeat > wdt_max_duration))
heartbeat = wdt_max_duration; heartbeat = wdt_max_duration;
ret = misc_register(&orion_wdt_miscdev); orion_wdt.timeout = heartbeat;
if (ret) orion_wdt.min_timeout = 1;
orion_wdt.max_timeout = wdt_max_duration;
watchdog_set_nowayout(&orion_wdt, nowayout);
ret = watchdog_register_device(&orion_wdt);
if (ret) {
clk_disable_unprepare(clk);
return ret; return ret;
}
pr_info("Initial timeout %d sec%s\n", pr_info("Initial timeout %d sec%s\n",
heartbeat, nowayout ? ", nowayout" : ""); heartbeat, nowayout ? ", nowayout" : "");
...@@ -272,27 +182,14 @@ static int __devinit orion_wdt_probe(struct platform_device *pdev) ...@@ -272,27 +182,14 @@ static int __devinit orion_wdt_probe(struct platform_device *pdev)
static int __devexit orion_wdt_remove(struct platform_device *pdev) static int __devexit orion_wdt_remove(struct platform_device *pdev)
{ {
int ret; watchdog_unregister_device(&orion_wdt);
if (test_bit(WDT_IN_USE, &wdt_status)) {
orion_wdt_disable();
clear_bit(WDT_IN_USE, &wdt_status);
}
ret = misc_deregister(&orion_wdt_miscdev);
if (!ret)
orion_wdt_miscdev.parent = NULL;
clk_disable_unprepare(clk); clk_disable_unprepare(clk);
clk_put(clk); return 0;
return ret;
} }
static void orion_wdt_shutdown(struct platform_device *pdev) static void orion_wdt_shutdown(struct platform_device *pdev)
{ {
if (test_bit(WDT_IN_USE, &wdt_status)) orion_wdt_stop(&orion_wdt);
orion_wdt_disable();
} }
static struct platform_driver orion_wdt_driver = { static struct platform_driver orion_wdt_driver = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment