2018-03-16 15:14:11 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-11-19 22:09:05 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Mans Rullgard <mans@mansr.com>
|
|
|
|
* SMP86xx/SMP87xx Watchdog driver
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
2018-06-20 05:47:28 +00:00
|
|
|
#include <linux/mod_devicetable.h>
|
2015-11-19 22:09:05 +00:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
|
|
|
|
#define DEFAULT_TIMEOUT 30
|
|
|
|
|
|
|
|
static bool nowayout = WATCHDOG_NOWAYOUT;
|
|
|
|
module_param(nowayout, bool, 0);
|
|
|
|
MODULE_PARM_DESC(nowayout,
|
|
|
|
"Watchdog cannot be stopped once started (default="
|
|
|
|
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|
|
|
|
|
|
|
|
static unsigned int timeout;
|
|
|
|
module_param(timeout, int, 0);
|
|
|
|
MODULE_PARM_DESC(timeout, "Watchdog timeout");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Counter counts down from programmed value. Reset asserts when
|
|
|
|
* the counter reaches 1.
|
|
|
|
*/
|
|
|
|
#define WD_COUNTER 0
|
|
|
|
|
|
|
|
#define WD_CONFIG 4
|
|
|
|
#define WD_CONFIG_XTAL_IN BIT(0)
|
|
|
|
#define WD_CONFIG_DISABLE BIT(31)
|
|
|
|
|
|
|
|
struct tangox_wdt_device {
|
|
|
|
struct watchdog_device wdt;
|
|
|
|
void __iomem *base;
|
|
|
|
unsigned long clk_rate;
|
|
|
|
struct clk *clk;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int tangox_wdt_set_timeout(struct watchdog_device *wdt,
|
|
|
|
unsigned int new_timeout)
|
|
|
|
{
|
|
|
|
wdt->timeout = new_timeout;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tangox_wdt_start(struct watchdog_device *wdt)
|
|
|
|
{
|
|
|
|
struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
|
|
|
|
u32 ticks;
|
|
|
|
|
|
|
|
ticks = 1 + wdt->timeout * dev->clk_rate;
|
|
|
|
writel(ticks, dev->base + WD_COUNTER);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tangox_wdt_stop(struct watchdog_device *wdt)
|
|
|
|
{
|
|
|
|
struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
|
|
|
|
|
|
|
|
writel(0, dev->base + WD_COUNTER);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int tangox_wdt_get_timeleft(struct watchdog_device *wdt)
|
|
|
|
{
|
|
|
|
struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
|
|
|
|
u32 count;
|
|
|
|
|
|
|
|
count = readl(dev->base + WD_COUNTER);
|
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (count - 1) / dev->clk_rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct watchdog_info tangox_wdt_info = {
|
|
|
|
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
|
|
|
|
.identity = "tangox watchdog",
|
|
|
|
};
|
|
|
|
|
2017-01-04 20:28:01 +00:00
|
|
|
static int tangox_wdt_restart(struct watchdog_device *wdt,
|
|
|
|
unsigned long action, void *data)
|
|
|
|
{
|
|
|
|
struct tangox_wdt_device *dev = watchdog_get_drvdata(wdt);
|
|
|
|
|
|
|
|
writel(1, dev->base + WD_COUNTER);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-19 22:09:05 +00:00
|
|
|
static const struct watchdog_ops tangox_wdt_ops = {
|
|
|
|
.start = tangox_wdt_start,
|
|
|
|
.stop = tangox_wdt_stop,
|
|
|
|
.set_timeout = tangox_wdt_set_timeout,
|
|
|
|
.get_timeleft = tangox_wdt_get_timeleft,
|
2017-01-04 20:28:01 +00:00
|
|
|
.restart = tangox_wdt_restart,
|
2015-11-19 22:09:05 +00:00
|
|
|
};
|
|
|
|
|
2019-04-09 17:24:01 +00:00
|
|
|
static void tangox_clk_disable_unprepare(void *data)
|
|
|
|
{
|
|
|
|
clk_disable_unprepare(data);
|
|
|
|
}
|
|
|
|
|
2015-11-19 22:09:05 +00:00
|
|
|
static int tangox_wdt_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct tangox_wdt_device *dev;
|
|
|
|
u32 config;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
|
|
|
|
if (!dev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
watchdog: Convert to use devm_platform_ioremap_resource
Use devm_platform_ioremap_resource to reduce source code size,
improve readability, and reduce the likelyhood of bugs.
The conversion was done automatically with coccinelle using the
following semantic patch.
@r@
identifier res, pdev;
expression a;
expression index;
expression e;
@@
<+...
- res = platform_get_resource(pdev, IORESOURCE_MEM, index);
- a = devm_ioremap_resource(e, res);
+ a = devm_platform_ioremap_resource(pdev, index);
...+>
@depends on r@
identifier r.res;
@@
- struct resource *res;
... when != res
@@
identifier res, pdev;
expression index;
expression a;
@@
- struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, index);
- a = devm_ioremap_resource(&pdev->dev, res);
+ a = devm_platform_ioremap_resource(pdev, index);
Cc: Joel Stanley <joel@jms.id.au>
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Linus Walleij <linus.walleij@linaro.org>
Cc: Baruch Siach <baruch@tkos.co.il>
Cc: Keguang Zhang <keguang.zhang@gmail.com>
Cc: Vladimir Zapolskiy <vz@mleia.com>
Cc: Kevin Hilman <khilman@baylibre.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: Avi Fishman <avifishman70@gmail.com>
Cc: Nancy Yuen <yuenn@google.com>
Cc: Brendan Higgins <brendanhiggins@google.com>
Cc: Wan ZongShun <mcuos.com@gmail.com>
Cc: Michal Simek <michal.simek@xilinx.com>
Cc: Sylvain Lemieux <slemieux.tyco@gmail.com>
Cc: Kukjin Kim <kgene@kernel.org>
Cc: Barry Song <baohua@kernel.org>
Cc: Orson Zhai <orsonzhai@gmail.com>
Cc: Patrice Chotard <patrice.chotard@st.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Maxime Ripard <maxime.ripard@bootlin.com>
Cc: Chen-Yu Tsai <wens@csie.org>
Cc: Marc Gonzalez <marc.w.gonzalez@free.fr>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Shawn Guo <shawnguo@kernel.org>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Acked-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Tested-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Acked-by: Joel Stanley <joel@jms.id.au>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Maxime Ripard <maxime.ripard@bootlin.com>
Acked-by: Michal Simek <michal.simek@xilinx.com> (cadence/xilinx wdts)
Acked-by: Thierry Reding <treding@nvidia.com>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Acked-by: Patrice Chotard <patrice.chotard@st.com>
Acked-by: Vladimir Zapolskiy <vz@mleia.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Wim Van Sebroeck <wim@linux-watchdog.org>
2019-04-02 19:01:53 +00:00
|
|
|
dev->base = devm_platform_ioremap_resource(pdev, 0);
|
2015-11-19 22:09:05 +00:00
|
|
|
if (IS_ERR(dev->base))
|
|
|
|
return PTR_ERR(dev->base);
|
|
|
|
|
|
|
|
dev->clk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(dev->clk))
|
|
|
|
return PTR_ERR(dev->clk);
|
|
|
|
|
|
|
|
err = clk_prepare_enable(dev->clk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2019-04-09 17:24:01 +00:00
|
|
|
err = devm_add_action_or_reset(&pdev->dev,
|
|
|
|
tangox_clk_disable_unprepare, dev->clk);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2015-11-19 22:09:05 +00:00
|
|
|
|
|
|
|
dev->clk_rate = clk_get_rate(dev->clk);
|
2019-04-09 17:24:01 +00:00
|
|
|
if (!dev->clk_rate)
|
|
|
|
return -EINVAL;
|
2015-11-19 22:09:05 +00:00
|
|
|
|
|
|
|
dev->wdt.parent = &pdev->dev;
|
|
|
|
dev->wdt.info = &tangox_wdt_info;
|
|
|
|
dev->wdt.ops = &tangox_wdt_ops;
|
|
|
|
dev->wdt.timeout = DEFAULT_TIMEOUT;
|
|
|
|
dev->wdt.min_timeout = 1;
|
2016-07-17 20:47:47 +00:00
|
|
|
dev->wdt.max_hw_heartbeat_ms = (U32_MAX - 1) / dev->clk_rate;
|
2015-11-19 22:09:05 +00:00
|
|
|
|
|
|
|
watchdog_init_timeout(&dev->wdt, timeout, &pdev->dev);
|
|
|
|
watchdog_set_nowayout(&dev->wdt, nowayout);
|
|
|
|
watchdog_set_drvdata(&dev->wdt, dev);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deactivate counter if disable bit is set to avoid
|
|
|
|
* accidental reset.
|
|
|
|
*/
|
|
|
|
config = readl(dev->base + WD_CONFIG);
|
|
|
|
if (config & WD_CONFIG_DISABLE)
|
|
|
|
writel(0, dev->base + WD_COUNTER);
|
|
|
|
|
|
|
|
writel(WD_CONFIG_XTAL_IN, dev->base + WD_CONFIG);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark as active and restart with configured timeout if
|
|
|
|
* already running.
|
|
|
|
*/
|
|
|
|
if (readl(dev->base + WD_COUNTER)) {
|
2016-05-27 22:28:00 +00:00
|
|
|
set_bit(WDOG_HW_RUNNING, &dev->wdt.status);
|
2015-11-19 22:09:05 +00:00
|
|
|
tangox_wdt_start(&dev->wdt);
|
|
|
|
}
|
|
|
|
|
2017-01-04 20:28:01 +00:00
|
|
|
watchdog_set_restart_priority(&dev->wdt, 128);
|
|
|
|
|
2019-04-09 17:24:01 +00:00
|
|
|
watchdog_stop_on_unregister(&dev->wdt);
|
|
|
|
err = devm_watchdog_register_device(&pdev->dev, &dev->wdt);
|
2016-03-03 08:24:12 +00:00
|
|
|
if (err)
|
2019-04-09 17:24:01 +00:00
|
|
|
return err;
|
2015-11-19 22:09:05 +00:00
|
|
|
|
|
|
|
platform_set_drvdata(pdev, dev);
|
|
|
|
|
2015-12-24 22:22:01 +00:00
|
|
|
dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
|
2015-11-19 22:09:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id tangox_wdt_dt_ids[] = {
|
|
|
|
{ .compatible = "sigma,smp8642-wdt" },
|
|
|
|
{ .compatible = "sigma,smp8759-wdt" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, tangox_wdt_dt_ids);
|
|
|
|
|
|
|
|
static struct platform_driver tangox_wdt_driver = {
|
|
|
|
.probe = tangox_wdt_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "tangox-wdt",
|
|
|
|
.of_match_table = tangox_wdt_dt_ids,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(tangox_wdt_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Mans Rullgard <mans@mansr.com>");
|
|
|
|
MODULE_DESCRIPTION("SMP86xx/SMP87xx Watchdog driver");
|
|
|
|
MODULE_LICENSE("GPL");
|