watchdog: imx2_wdt: convert to watchdog core api

Convert the imx2_wdt driver to the new watchdog core api.

Signed-off-by: Anatolij Gustschin <agust@denx.de>
Reviewed-by: Guenter Roeck <linux@roeck-us.net>
Cc: Wolfram Sang <wsa@the-dreams.de>
Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
This commit is contained in:
Anatolij Gustschin 2014-04-11 08:57:14 +02:00 committed by Wim Van Sebroeck
parent a797700329
commit faad5de0b1
2 changed files with 128 additions and 175 deletions

View file

@ -379,6 +379,7 @@ config IMX2_WDT
tristate "IMX2+ Watchdog" tristate "IMX2+ Watchdog"
depends on ARCH_MXC depends on ARCH_MXC
select REGMAP_MMIO select REGMAP_MMIO
select WATCHDOG_CORE
help help
This is the driver for the hardware watchdog This is the driver for the hardware watchdog
on the Freescale IMX2 and later processors. on the Freescale IMX2 and later processors.

View file

@ -22,18 +22,15 @@
*/ */
#include <linux/clk.h> #include <linux/clk.h>
#include <linux/fs.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/moduleparam.h> #include <linux/moduleparam.h>
#include <linux/platform_device.h> #include <linux/platform_device.h>
#include <linux/regmap.h> #include <linux/regmap.h>
#include <linux/timer.h> #include <linux/timer.h>
#include <linux/uaccess.h>
#include <linux/watchdog.h> #include <linux/watchdog.h>
#define DRIVER_NAME "imx2-wdt" #define DRIVER_NAME "imx2-wdt"
@ -56,19 +53,12 @@
#define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8) #define WDOG_SEC_TO_COUNT(s) ((s * 2 - 1) << 8)
#define IMX2_WDT_STATUS_OPEN 0 struct imx2_wdt_device {
#define IMX2_WDT_STATUS_STARTED 1
#define IMX2_WDT_EXPECT_CLOSE 2
static struct {
struct clk *clk; struct clk *clk;
struct regmap *regmap; struct regmap *regmap;
unsigned timeout;
unsigned long status;
struct timer_list timer; /* Pings the watchdog when closed */ struct timer_list timer; /* Pings the watchdog when closed */
} imx2_wdt; struct watchdog_device wdog;
};
static struct miscdevice imx2_wdt_miscdev;
static bool nowayout = WATCHDOG_NOWAYOUT; static bool nowayout = WATCHDOG_NOWAYOUT;
module_param(nowayout, bool, 0); module_param(nowayout, bool, 0);
@ -86,11 +76,12 @@ static const struct watchdog_info imx2_wdt_info = {
.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
}; };
static inline void imx2_wdt_setup(void) static inline void imx2_wdt_setup(struct watchdog_device *wdog)
{ {
struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
u32 val; u32 val;
regmap_read(imx2_wdt.regmap, IMX2_WDT_WCR, &val); regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
/* Suspend timer in low power mode, write once-only */ /* Suspend timer in low power mode, write once-only */
val |= IMX2_WDT_WCR_WDZST; val |= IMX2_WDT_WCR_WDZST;
@ -101,157 +92,93 @@ static inline void imx2_wdt_setup(void)
/* Keep Watchdog Disabled */ /* Keep Watchdog Disabled */
val &= ~IMX2_WDT_WCR_WDE; val &= ~IMX2_WDT_WCR_WDE;
/* Set the watchdog's Time-Out value */ /* Set the watchdog's Time-Out value */
val |= WDOG_SEC_TO_COUNT(imx2_wdt.timeout); val |= WDOG_SEC_TO_COUNT(wdog->timeout);
regmap_write(imx2_wdt.regmap, IMX2_WDT_WCR, val); regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
/* enable the watchdog */ /* enable the watchdog */
val |= IMX2_WDT_WCR_WDE; val |= IMX2_WDT_WCR_WDE;
regmap_write(imx2_wdt.regmap, IMX2_WDT_WCR, val); regmap_write(wdev->regmap, IMX2_WDT_WCR, val);
} }
static inline void imx2_wdt_ping(void) static inline bool imx2_wdt_is_running(struct imx2_wdt_device *wdev)
{ {
regmap_write(imx2_wdt.regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1); u32 val;
regmap_write(imx2_wdt.regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2);
regmap_read(wdev->regmap, IMX2_WDT_WCR, &val);
return val & IMX2_WDT_WCR_WDE;
}
static int imx2_wdt_ping(struct watchdog_device *wdog)
{
struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ1);
regmap_write(wdev->regmap, IMX2_WDT_WSR, IMX2_WDT_SEQ2);
return 0;
} }
static void imx2_wdt_timer_ping(unsigned long arg) static void imx2_wdt_timer_ping(unsigned long arg)
{ {
/* ping it every imx2_wdt.timeout / 2 seconds to prevent reboot */ struct watchdog_device *wdog = (struct watchdog_device *)arg;
imx2_wdt_ping(); struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
mod_timer(&imx2_wdt.timer, jiffies + imx2_wdt.timeout * HZ / 2);
/* ping it every wdog->timeout / 2 seconds to prevent reboot */
imx2_wdt_ping(wdog);
mod_timer(&wdev->timer, jiffies + wdog->timeout * HZ / 2);
} }
static void imx2_wdt_start(void) static int imx2_wdt_set_timeout(struct watchdog_device *wdog,
unsigned int new_timeout)
{ {
if (!test_and_set_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) { struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
/* at our first start we enable clock and do initialisations */
clk_prepare_enable(imx2_wdt.clk);
imx2_wdt_setup(); regmap_update_bits(wdev->regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
} else /* delete the timer that pings the watchdog after close */
del_timer_sync(&imx2_wdt.timer);
/* Watchdog is enabled - time to reload the timeout value */
imx2_wdt_ping();
}
static void imx2_wdt_stop(void)
{
/* we don't need a clk_disable, it cannot be disabled once started.
* We use a timer to ping the watchdog while /dev/watchdog is closed */
imx2_wdt_timer_ping(0);
}
static void imx2_wdt_set_timeout(int new_timeout)
{
regmap_update_bits(imx2_wdt.regmap, IMX2_WDT_WCR, IMX2_WDT_WCR_WT,
WDOG_SEC_TO_COUNT(new_timeout)); WDOG_SEC_TO_COUNT(new_timeout));
}
static int imx2_wdt_open(struct inode *inode, struct file *file)
{
if (test_and_set_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status))
return -EBUSY;
imx2_wdt_start();
return nonseekable_open(inode, file);
}
static int imx2_wdt_close(struct inode *inode, struct file *file)
{
if (test_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status) && !nowayout)
imx2_wdt_stop();
else {
dev_crit(imx2_wdt_miscdev.parent,
"Unexpected close: Expect reboot!\n");
imx2_wdt_ping();
}
clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
clear_bit(IMX2_WDT_STATUS_OPEN, &imx2_wdt.status);
return 0; return 0;
} }
static long imx2_wdt_ioctl(struct file *file, unsigned int cmd, static int imx2_wdt_start(struct watchdog_device *wdog)
unsigned long arg)
{ {
void __user *argp = (void __user *)arg; struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
int __user *p = argp;
int new_value;
u32 val;
switch (cmd) { if (imx2_wdt_is_running(wdev)) {
case WDIOC_GETSUPPORT: /* delete the timer that pings the watchdog after close */
return copy_to_user(argp, &imx2_wdt_info, del_timer_sync(&wdev->timer);
sizeof(struct watchdog_info)) ? -EFAULT : 0; imx2_wdt_set_timeout(wdog, wdog->timeout);
} else
imx2_wdt_setup(wdog);
case WDIOC_GETSTATUS: return imx2_wdt_ping(wdog);
return put_user(0, p); }
case WDIOC_GETBOOTSTATUS: static int imx2_wdt_stop(struct watchdog_device *wdog)
regmap_read(imx2_wdt.regmap, IMX2_WDT_WRSR, &val); {
new_value = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0; /*
return put_user(new_value, p); * We don't need a clk_disable, it cannot be disabled once started.
* We use a timer to ping the watchdog while /dev/watchdog is closed
case WDIOC_KEEPALIVE: */
imx2_wdt_ping(); imx2_wdt_timer_ping((unsigned long)wdog);
return 0; return 0;
}
case WDIOC_SETTIMEOUT: static inline void imx2_wdt_ping_if_active(struct watchdog_device *wdog)
if (get_user(new_value, p)) {
return -EFAULT; struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
if ((new_value < 1) || (new_value > IMX2_WDT_MAX_TIME))
return -EINVAL;
imx2_wdt_set_timeout(new_value);
imx2_wdt.timeout = new_value;
imx2_wdt_ping();
/* Fallthrough to return current value */ if (imx2_wdt_is_running(wdev)) {
case WDIOC_GETTIMEOUT: imx2_wdt_set_timeout(wdog, wdog->timeout);
return put_user(imx2_wdt.timeout, p); imx2_wdt_timer_ping((unsigned long)wdog);
default:
return -ENOTTY;
} }
} }
static ssize_t imx2_wdt_write(struct file *file, const char __user *data, static struct watchdog_ops imx2_wdt_ops = {
size_t len, loff_t *ppos)
{
size_t i;
char c;
if (len == 0) /* Can we see this even ? */
return 0;
clear_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
/* scan to see whether or not we got the magic character */
for (i = 0; i != len; i++) {
if (get_user(c, data + i))
return -EFAULT;
if (c == 'V')
set_bit(IMX2_WDT_EXPECT_CLOSE, &imx2_wdt.status);
}
imx2_wdt_ping();
return len;
}
static const struct file_operations imx2_wdt_fops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.llseek = no_llseek, .start = imx2_wdt_start,
.unlocked_ioctl = imx2_wdt_ioctl, .stop = imx2_wdt_stop,
.open = imx2_wdt_open, .ping = imx2_wdt_ping,
.release = imx2_wdt_close, .set_timeout = imx2_wdt_set_timeout,
.write = imx2_wdt_write,
};
static struct miscdevice imx2_wdt_miscdev = {
.minor = WATCHDOG_MINOR,
.name = "watchdog",
.fops = &imx2_wdt_fops,
}; };
static struct regmap_config imx2_wdt_regmap_config = { static struct regmap_config imx2_wdt_regmap_config = {
@ -263,76 +190,101 @@ static struct regmap_config imx2_wdt_regmap_config = {
static int __init imx2_wdt_probe(struct platform_device *pdev) static int __init imx2_wdt_probe(struct platform_device *pdev)
{ {
struct imx2_wdt_device *wdev;
struct watchdog_device *wdog;
struct resource *res; struct resource *res;
void __iomem *base; void __iomem *base;
int ret; int ret;
u32 val;
wdev = devm_kzalloc(&pdev->dev, sizeof(*wdev), GFP_KERNEL);
if (!wdev)
return -ENOMEM;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
base = devm_ioremap_resource(&pdev->dev, res); base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(base)) if (IS_ERR(base))
return PTR_ERR(base); return PTR_ERR(base);
imx2_wdt.regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base, wdev->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
&imx2_wdt_regmap_config); &imx2_wdt_regmap_config);
if (IS_ERR(imx2_wdt.regmap)) { if (IS_ERR(wdev->regmap)) {
dev_err(&pdev->dev, "regmap init failed\n"); dev_err(&pdev->dev, "regmap init failed\n");
return PTR_ERR(imx2_wdt.regmap); return PTR_ERR(wdev->regmap);
} }
imx2_wdt.clk = devm_clk_get(&pdev->dev, NULL); wdev->clk = devm_clk_get(&pdev->dev, NULL);
if (IS_ERR(imx2_wdt.clk)) { if (IS_ERR(wdev->clk)) {
dev_err(&pdev->dev, "can't get Watchdog clock\n"); dev_err(&pdev->dev, "can't get Watchdog clock\n");
return PTR_ERR(imx2_wdt.clk); return PTR_ERR(wdev->clk);
} }
imx2_wdt.timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME); wdog = &wdev->wdog;
if (imx2_wdt.timeout != timeout) wdog->info = &imx2_wdt_info;
dev_warn(&pdev->dev, "Initial timeout out of range! " wdog->ops = &imx2_wdt_ops;
"Clamped from %u to %u\n", timeout, imx2_wdt.timeout); wdog->min_timeout = 1;
wdog->max_timeout = IMX2_WDT_MAX_TIME;
setup_timer(&imx2_wdt.timer, imx2_wdt_timer_ping, 0); clk_prepare_enable(wdev->clk);
imx2_wdt_miscdev.parent = &pdev->dev; regmap_read(wdev->regmap, IMX2_WDT_WRSR, &val);
ret = misc_register(&imx2_wdt_miscdev); wdog->bootstatus = val & IMX2_WDT_WRSR_TOUT ? WDIOF_CARDRESET : 0;
if (ret)
goto fail;
dev_info(&pdev->dev, wdog->timeout = clamp_t(unsigned, timeout, 1, IMX2_WDT_MAX_TIME);
"IMX2+ Watchdog Timer enabled. timeout=%ds (nowayout=%d)\n", if (wdog->timeout != timeout)
imx2_wdt.timeout, nowayout); dev_warn(&pdev->dev, "Initial timeout out of range! Clamped from %u to %u\n",
return 0; timeout, wdog->timeout);
fail: platform_set_drvdata(pdev, wdog);
imx2_wdt_miscdev.parent = NULL; watchdog_set_drvdata(wdog, wdev);
watchdog_set_nowayout(wdog, nowayout);
watchdog_init_timeout(wdog, timeout, &pdev->dev);
setup_timer(&wdev->timer, imx2_wdt_timer_ping, (unsigned long)wdog);
imx2_wdt_ping_if_active(wdog);
ret = watchdog_register_device(wdog);
if (ret) {
dev_err(&pdev->dev, "cannot register watchdog device\n");
return ret; return ret;
}
dev_info(&pdev->dev, "timeout %d sec (nowayout=%d)\n",
wdog->timeout, nowayout);
return 0;
} }
static int __exit imx2_wdt_remove(struct platform_device *pdev) static int __exit imx2_wdt_remove(struct platform_device *pdev)
{ {
misc_deregister(&imx2_wdt_miscdev); struct watchdog_device *wdog = platform_get_drvdata(pdev);
struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) { watchdog_unregister_device(wdog);
del_timer_sync(&imx2_wdt.timer);
dev_crit(imx2_wdt_miscdev.parent, if (imx2_wdt_is_running(wdev)) {
"Device removed: Expect reboot!\n"); del_timer_sync(&wdev->timer);
imx2_wdt_ping(wdog);
dev_crit(&pdev->dev, "Device removed: Expect reboot!\n");
} }
imx2_wdt_miscdev.parent = NULL;
return 0; return 0;
} }
static void imx2_wdt_shutdown(struct platform_device *pdev) static void imx2_wdt_shutdown(struct platform_device *pdev)
{ {
if (test_bit(IMX2_WDT_STATUS_STARTED, &imx2_wdt.status)) { struct watchdog_device *wdog = platform_get_drvdata(pdev);
/* we are running, we need to delete the timer but will give struct imx2_wdt_device *wdev = watchdog_get_drvdata(wdog);
* max timeout before reboot will take place */
del_timer_sync(&imx2_wdt.timer);
imx2_wdt_set_timeout(IMX2_WDT_MAX_TIME);
imx2_wdt_ping();
dev_crit(imx2_wdt_miscdev.parent, if (imx2_wdt_is_running(wdev)) {
"Device shutdown: Expect reboot!\n"); /*
* We are running, we need to delete the timer but will
* give max timeout before reboot will take place
*/
del_timer_sync(&wdev->timer);
imx2_wdt_set_timeout(wdog, IMX2_WDT_MAX_TIME);
imx2_wdt_ping(wdog);
dev_crit(&pdev->dev, "Device shutdown: Expect reboot!\n");
} }
} }