Commit dcd7e04e authored by Hauke Mehrtens's avatar Hauke Mehrtens Committed by Wim Van Sebroeck

watchdog: lantiq: Convert to watchdog_device

Instead of doing the ioctl handling manually just use register a
watchdog_device and let the watchdog framework do the ioctl handling.

This also removes the ltq_wdt_bootstatus_set typedef and replaces it
with a structure providing the chip specific functions pointer.
The watchdog_init_timeout() function is now used and the initial timeout
can be provided in device tree.
If the watchdog was already activated it will not be stopped any more,
but the settings from the driver will be used and the watchdog subsystem
will take care.
Signed-off-by: default avatarHauke Mehrtens <hauke@hauke-m.de>
Reviewed-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarWim Van Sebroeck <wim@linux-watchdog.org>
parent 1f59f8af
...@@ -1621,6 +1621,7 @@ config IMGPDC_WDT ...@@ -1621,6 +1621,7 @@ config IMGPDC_WDT
config LANTIQ_WDT config LANTIQ_WDT
tristate "Lantiq SoC watchdog" tristate "Lantiq SoC watchdog"
depends on LANTIQ depends on LANTIQ
select WATCHDOG_CORE
help help
Hardware driver for the Lantiq SoC Watchdog Timer. Hardware driver for the Lantiq SoC Watchdog Timer.
......
...@@ -8,11 +8,7 @@ ...@@ -8,11 +8,7 @@
* Based on EP93xx wdt driver * Based on EP93xx wdt driver
*/ */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h> #include <linux/module.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/watchdog.h> #include <linux/watchdog.h>
#include <linux/of_platform.h> #include <linux/of_platform.h>
...@@ -52,159 +48,106 @@ ...@@ -52,159 +48,106 @@
#define LTQ_WDT_CR_CLKDIV (0x3 << 24) #define LTQ_WDT_CR_CLKDIV (0x3 << 24)
#define LTQ_WDT_CR_PW_MASK GENMASK(23, 16) /* Password field */ #define LTQ_WDT_CR_PW_MASK GENMASK(23, 16) /* Password field */
#define LTQ_WDT_CR_MAX_TIMEOUT ((1 << 16) - 1) /* The reload field is 16 bit */ #define LTQ_WDT_CR_MAX_TIMEOUT ((1 << 16) - 1) /* The reload field is 16 bit */
#define LTQ_WDT_SR 0x8 /* watchdog status register */
#define LTQ_WDT_SR_EN BIT(31) /* Enable */
#define LTQ_WDT_DIVIDER 0x40000 #define LTQ_WDT_DIVIDER 0x40000
static bool nowayout = WATCHDOG_NOWAYOUT; static bool nowayout = WATCHDOG_NOWAYOUT;
static void __iomem *ltq_wdt_membase; struct ltq_wdt_hw {
static unsigned long ltq_io_region_clk_rate; int (*bootstatus_get)(struct device *dev);
};
static unsigned long ltq_wdt_bootstatus; struct ltq_wdt_priv {
static unsigned long ltq_wdt_in_use; struct watchdog_device wdt;
static int ltq_wdt_timeout = 30; void __iomem *membase;
static int ltq_wdt_ok_to_close; unsigned long clk_rate;
};
static void static u32 ltq_wdt_r32(struct ltq_wdt_priv *priv, u32 offset)
ltq_wdt_enable(void)
{ {
unsigned long int timeout = ltq_wdt_timeout * return __raw_readl(priv->membase + offset);
(ltq_io_region_clk_rate / LTQ_WDT_DIVIDER) + 0x1000;
if (timeout > LTQ_WDT_CR_MAX_TIMEOUT)
timeout = LTQ_WDT_CR_MAX_TIMEOUT;
/* write the first password magic */
ltq_w32(LTQ_WDT_CR_PW1, ltq_wdt_membase + LTQ_WDT_CR);
/* write the second magic plus the configuration and new timeout */
ltq_w32(LTQ_WDT_CR_GEN | LTQ_WDT_CR_PWL | LTQ_WDT_CR_CLKDIV |
LTQ_WDT_CR_PW2 | timeout, ltq_wdt_membase + LTQ_WDT_CR);
} }
static void static void ltq_wdt_w32(struct ltq_wdt_priv *priv, u32 val, u32 offset)
ltq_wdt_disable(void)
{ {
/* write the first password magic */ __raw_writel(val, priv->membase + offset);
ltq_w32(LTQ_WDT_CR_PW1, ltq_wdt_membase + LTQ_WDT_CR);
/*
* write the second password magic with no config
* this turns the watchdog off
*/
ltq_w32(LTQ_WDT_CR_PW2, ltq_wdt_membase + LTQ_WDT_CR);
} }
static ssize_t static void ltq_wdt_mask(struct ltq_wdt_priv *priv, u32 clear, u32 set,
ltq_wdt_write(struct file *file, const char __user *data, u32 offset)
size_t len, loff_t *ppos)
{ {
if (len) { u32 val = ltq_wdt_r32(priv, offset);
if (!nowayout) {
size_t i; val &= ~(clear);
val |= set;
ltq_wdt_ok_to_close = 0; ltq_wdt_w32(priv, val, offset);
for (i = 0; i != len; i++) { }
char c;
if (get_user(c, data + i))
return -EFAULT;
if (c == 'V')
ltq_wdt_ok_to_close = 1;
else
ltq_wdt_ok_to_close = 0;
}
}
ltq_wdt_enable();
}
return len; static struct ltq_wdt_priv *ltq_wdt_get_priv(struct watchdog_device *wdt)
{
return container_of(wdt, struct ltq_wdt_priv, wdt);
} }
static struct watchdog_info ident = { static struct watchdog_info ltq_wdt_info = {
.options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
WDIOF_CARDRESET, WDIOF_CARDRESET,
.identity = "ltq_wdt", .identity = "ltq_wdt",
}; };
static long static int ltq_wdt_start(struct watchdog_device *wdt)
ltq_wdt_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{ {
int ret = -ENOTTY; struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
u32 timeout;
switch (cmd) {
case WDIOC_GETSUPPORT: timeout = wdt->timeout * priv->clk_rate;
ret = copy_to_user((struct watchdog_info __user *)arg, &ident,
sizeof(ident)) ? -EFAULT : 0; ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
break; /* write the second magic plus the configuration and new timeout */
ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK | LTQ_WDT_CR_MAX_TIMEOUT,
case WDIOC_GETBOOTSTATUS: LTQ_WDT_CR_GEN | LTQ_WDT_CR_PWL | LTQ_WDT_CR_CLKDIV |
ret = put_user(ltq_wdt_bootstatus, (int __user *)arg); LTQ_WDT_CR_PW2 | timeout,
break; LTQ_WDT_CR);
case WDIOC_GETSTATUS: return 0;
ret = put_user(0, (int __user *)arg);
break;
case WDIOC_SETTIMEOUT:
ret = get_user(ltq_wdt_timeout, (int __user *)arg);
if (!ret)
ltq_wdt_enable();
/* intentional drop through */
case WDIOC_GETTIMEOUT:
ret = put_user(ltq_wdt_timeout, (int __user *)arg);
break;
case WDIOC_KEEPALIVE:
ltq_wdt_enable();
ret = 0;
break;
}
return ret;
} }
static int static int ltq_wdt_stop(struct watchdog_device *wdt)
ltq_wdt_open(struct inode *inode, struct file *file)
{ {
if (test_and_set_bit(0, &ltq_wdt_in_use)) struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
return -EBUSY;
ltq_wdt_in_use = 1; ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
ltq_wdt_enable(); ltq_wdt_mask(priv, LTQ_WDT_CR_GEN | LTQ_WDT_CR_PW_MASK,
LTQ_WDT_CR_PW2, LTQ_WDT_CR);
return nonseekable_open(inode, file); return 0;
} }
static int static int ltq_wdt_ping(struct watchdog_device *wdt)
ltq_wdt_release(struct inode *inode, struct file *file)
{ {
if (ltq_wdt_ok_to_close) struct ltq_wdt_priv *priv = ltq_wdt_get_priv(wdt);
ltq_wdt_disable(); u32 timeout;
else
pr_err("watchdog closed without warning\n"); timeout = wdt->timeout * priv->clk_rate;
ltq_wdt_ok_to_close = 0;
clear_bit(0, &ltq_wdt_in_use); ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK, LTQ_WDT_CR_PW1, LTQ_WDT_CR);
/* write the second magic plus the configuration and new timeout */
ltq_wdt_mask(priv, LTQ_WDT_CR_PW_MASK | LTQ_WDT_CR_MAX_TIMEOUT,
LTQ_WDT_CR_PW2 | timeout, LTQ_WDT_CR);
return 0; return 0;
} }
static const struct file_operations ltq_wdt_fops = { static const struct watchdog_ops ltq_wdt_ops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.write = ltq_wdt_write, .start = ltq_wdt_start,
.unlocked_ioctl = ltq_wdt_ioctl, .stop = ltq_wdt_stop,
.open = ltq_wdt_open, .ping = ltq_wdt_ping,
.release = ltq_wdt_release,
.llseek = no_llseek,
};
static struct miscdevice ltq_wdt_miscdev = {
.minor = WATCHDOG_MINOR,
.name = "watchdog",
.fops = &ltq_wdt_fops,
}; };
typedef int (*ltq_wdt_bootstatus_set)(struct platform_device *pdev); static int ltq_wdt_xrx_bootstatus_get(struct device *dev)
static int ltq_wdt_bootstatus_xrx(struct platform_device *pdev)
{ {
struct device *dev = &pdev->dev;
struct regmap *rcu_regmap; struct regmap *rcu_regmap;
u32 val; u32 val;
int err; int err;
...@@ -218,14 +161,13 @@ static int ltq_wdt_bootstatus_xrx(struct platform_device *pdev) ...@@ -218,14 +161,13 @@ static int ltq_wdt_bootstatus_xrx(struct platform_device *pdev)
return err; return err;
if (val & LTQ_XRX_RCU_RST_STAT_WDT) if (val & LTQ_XRX_RCU_RST_STAT_WDT)
ltq_wdt_bootstatus = WDIOF_CARDRESET; return WDIOF_CARDRESET;
return 0; return 0;
} }
static int ltq_wdt_bootstatus_falcon(struct platform_device *pdev) static int ltq_wdt_falcon_bootstatus_get(struct device *dev)
{ {
struct device *dev = &pdev->dev;
struct regmap *rcu_regmap; struct regmap *rcu_regmap;
u32 val; u32 val;
int err; int err;
...@@ -240,62 +182,90 @@ static int ltq_wdt_bootstatus_falcon(struct platform_device *pdev) ...@@ -240,62 +182,90 @@ static int ltq_wdt_bootstatus_falcon(struct platform_device *pdev)
return err; return err;
if ((val & LTQ_FALCON_SYS1_CPU0RS_MASK) == LTQ_FALCON_SYS1_CPU0RS_WDT) if ((val & LTQ_FALCON_SYS1_CPU0RS_MASK) == LTQ_FALCON_SYS1_CPU0RS_WDT)
ltq_wdt_bootstatus = WDIOF_CARDRESET; return WDIOF_CARDRESET;
return 0; return 0;
} }
static int static int ltq_wdt_probe(struct platform_device *pdev)
ltq_wdt_probe(struct platform_device *pdev)
{ {
struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); struct device *dev = &pdev->dev;
struct ltq_wdt_priv *priv;
struct watchdog_device *wdt;
struct resource *res;
struct clk *clk; struct clk *clk;
ltq_wdt_bootstatus_set ltq_wdt_bootstatus_set; const struct ltq_wdt_hw *ltq_wdt_hw;
int ret; int ret;
u32 status;
ltq_wdt_membase = devm_ioremap_resource(&pdev->dev, res); priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
if (IS_ERR(ltq_wdt_membase)) if (!priv)
return PTR_ERR(ltq_wdt_membase); return -ENOMEM;
ltq_wdt_bootstatus_set = of_device_get_match_data(&pdev->dev); res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (ltq_wdt_bootstatus_set) { priv->membase = devm_ioremap_resource(dev, res);
ret = ltq_wdt_bootstatus_set(pdev); if (IS_ERR(priv->membase))
if (ret) return PTR_ERR(priv->membase);
return ret;
}
/* we do not need to enable the clock as it is always running */ /* we do not need to enable the clock as it is always running */
clk = clk_get_io(); clk = clk_get_io();
if (IS_ERR(clk)) { priv->clk_rate = clk_get_rate(clk) / LTQ_WDT_DIVIDER;
dev_err(&pdev->dev, "Failed to get clock\n"); if (!priv->clk_rate) {
return -ENOENT; dev_err(dev, "clock rate less than divider %i\n",
LTQ_WDT_DIVIDER);
return -EINVAL;
} }
ltq_io_region_clk_rate = clk_get_rate(clk);
clk_put(clk);
dev_info(&pdev->dev, "Init done\n"); wdt = &priv->wdt;
return misc_register(&ltq_wdt_miscdev); wdt->info = &ltq_wdt_info;
} wdt->ops = &ltq_wdt_ops;
wdt->min_timeout = 1;
wdt->max_timeout = LTQ_WDT_CR_MAX_TIMEOUT / priv->clk_rate;
wdt->timeout = wdt->max_timeout;
wdt->parent = dev;
ltq_wdt_hw = of_device_get_match_data(dev);
if (ltq_wdt_hw && ltq_wdt_hw->bootstatus_get) {
ret = ltq_wdt_hw->bootstatus_get(dev);
if (ret >= 0)
wdt->bootstatus = ret;
}
static int watchdog_set_nowayout(wdt, nowayout);
ltq_wdt_remove(struct platform_device *pdev) watchdog_init_timeout(wdt, 0, dev);
{
misc_deregister(&ltq_wdt_miscdev);
return 0; status = ltq_wdt_r32(priv, LTQ_WDT_SR);
if (status & LTQ_WDT_SR_EN) {
/*
* If the watchdog is already running overwrite it with our
* new settings. Stop is not needed as the start call will
* replace all settings anyway.
*/
ltq_wdt_start(wdt);
set_bit(WDOG_HW_RUNNING, &wdt->status);
}
return devm_watchdog_register_device(dev, wdt);
} }
static const struct ltq_wdt_hw ltq_wdt_xrx100 = {
.bootstatus_get = ltq_wdt_xrx_bootstatus_get,
};
static const struct ltq_wdt_hw ltq_wdt_falcon = {
.bootstatus_get = ltq_wdt_falcon_bootstatus_get,
};
static const struct of_device_id ltq_wdt_match[] = { static const struct of_device_id ltq_wdt_match[] = {
{ .compatible = "lantiq,wdt", .data = NULL}, { .compatible = "lantiq,wdt", .data = NULL },
{ .compatible = "lantiq,xrx100-wdt", .data = ltq_wdt_bootstatus_xrx }, { .compatible = "lantiq,xrx100-wdt", .data = &ltq_wdt_xrx100 },
{ .compatible = "lantiq,falcon-wdt", .data = ltq_wdt_bootstatus_falcon }, { .compatible = "lantiq,falcon-wdt", .data = &ltq_wdt_falcon },
{}, {},
}; };
MODULE_DEVICE_TABLE(of, ltq_wdt_match); MODULE_DEVICE_TABLE(of, ltq_wdt_match);
static struct platform_driver ltq_wdt_driver = { static struct platform_driver ltq_wdt_driver = {
.probe = ltq_wdt_probe, .probe = ltq_wdt_probe,
.remove = ltq_wdt_remove,
.driver = { .driver = {
.name = "wdt", .name = "wdt",
.of_match_table = ltq_wdt_match, .of_match_table = ltq_wdt_match,
......
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