2019-05-27 14:55:00 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-12-06 04:08:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010, Paul Cercueil <paul@crapouillou.net>
|
|
|
|
* JZ4740 Watchdog driver
|
|
|
|
*/
|
|
|
|
|
2019-06-08 00:24:26 +08:00
|
|
|
#include <linux/mfd/ingenic-tcu.h>
|
2010-12-06 04:08:22 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/slab.h>
|
2012-01-26 18:10:45 +08:00
|
|
|
#include <linux/err.h>
|
2015-02-03 18:25:48 +08:00
|
|
|
#include <linux/of.h>
|
2010-12-06 04:08:22 +08:00
|
|
|
|
|
|
|
#include <asm/mach-jz4740/timer.h>
|
|
|
|
|
|
|
|
#define JZ_WDT_CLOCK_PCLK 0x1
|
|
|
|
#define JZ_WDT_CLOCK_RTC 0x2
|
|
|
|
#define JZ_WDT_CLOCK_EXT 0x4
|
|
|
|
|
2019-06-08 00:24:26 +08:00
|
|
|
#define JZ_WDT_CLOCK_DIV_1 (0 << TCU_TCSR_PRESCALE_LSB)
|
|
|
|
#define JZ_WDT_CLOCK_DIV_4 (1 << TCU_TCSR_PRESCALE_LSB)
|
|
|
|
#define JZ_WDT_CLOCK_DIV_16 (2 << TCU_TCSR_PRESCALE_LSB)
|
|
|
|
#define JZ_WDT_CLOCK_DIV_64 (3 << TCU_TCSR_PRESCALE_LSB)
|
|
|
|
#define JZ_WDT_CLOCK_DIV_256 (4 << TCU_TCSR_PRESCALE_LSB)
|
|
|
|
#define JZ_WDT_CLOCK_DIV_1024 (5 << TCU_TCSR_PRESCALE_LSB)
|
2010-12-06 04:08:22 +08:00
|
|
|
|
|
|
|
#define DEFAULT_HEARTBEAT 5
|
|
|
|
#define MAX_HEARTBEAT 2048
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
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) ")");
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
static unsigned int heartbeat = DEFAULT_HEARTBEAT;
|
|
|
|
module_param(heartbeat, uint, 0);
|
|
|
|
MODULE_PARM_DESC(heartbeat,
|
|
|
|
"Watchdog heartbeat period in seconds from 1 to "
|
|
|
|
__MODULE_STRING(MAX_HEARTBEAT) ", default "
|
|
|
|
__MODULE_STRING(DEFAULT_HEARTBEAT));
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
struct jz4740_wdt_drvdata {
|
|
|
|
struct watchdog_device wdt;
|
|
|
|
void __iomem *base;
|
|
|
|
struct clk *rtc_clk;
|
|
|
|
};
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
static int jz4740_wdt_ping(struct watchdog_device *wdt_dev)
|
2010-12-06 04:08:22 +08:00
|
|
|
{
|
2012-01-26 18:10:45 +08:00
|
|
|
struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
|
|
|
|
2019-06-08 00:24:26 +08:00
|
|
|
writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
|
2012-01-26 18:10:45 +08:00
|
|
|
return 0;
|
2010-12-06 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
static int jz4740_wdt_set_timeout(struct watchdog_device *wdt_dev,
|
|
|
|
unsigned int new_timeout)
|
2010-12-06 04:08:22 +08:00
|
|
|
{
|
2012-01-26 18:10:45 +08:00
|
|
|
struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
2010-12-06 04:08:22 +08:00
|
|
|
unsigned int rtc_clk_rate;
|
|
|
|
unsigned int timeout_value;
|
|
|
|
unsigned short clock_div = JZ_WDT_CLOCK_DIV_1;
|
2019-06-08 00:24:27 +08:00
|
|
|
u8 tcer;
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
rtc_clk_rate = clk_get_rate(drvdata->rtc_clk);
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
timeout_value = rtc_clk_rate * new_timeout;
|
2010-12-06 04:08:22 +08:00
|
|
|
while (timeout_value > 0xffff) {
|
|
|
|
if (clock_div == JZ_WDT_CLOCK_DIV_1024) {
|
|
|
|
/* Requested timeout too high;
|
|
|
|
* use highest possible value. */
|
|
|
|
timeout_value = 0xffff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timeout_value >>= 2;
|
2019-06-08 00:24:26 +08:00
|
|
|
clock_div += (1 << TCU_TCSR_PRESCALE_LSB);
|
2010-12-06 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
2019-06-08 00:24:27 +08:00
|
|
|
tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
|
2019-06-08 00:24:26 +08:00
|
|
|
writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
|
|
|
|
writew(clock_div, drvdata->base + TCU_REG_WDT_TCSR);
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2019-06-08 00:24:26 +08:00
|
|
|
writew((u16)timeout_value, drvdata->base + TCU_REG_WDT_TDR);
|
|
|
|
writew(0x0, drvdata->base + TCU_REG_WDT_TCNT);
|
|
|
|
writew(clock_div | JZ_WDT_CLOCK_RTC, drvdata->base + TCU_REG_WDT_TCSR);
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2019-06-08 00:24:27 +08:00
|
|
|
if (tcer & TCU_WDT_TCER_TCEN)
|
|
|
|
writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-03-01 03:20:58 +08:00
|
|
|
wdt_dev->timeout = new_timeout;
|
2012-01-26 18:10:45 +08:00
|
|
|
return 0;
|
2010-12-06 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
static int jz4740_wdt_start(struct watchdog_device *wdt_dev)
|
2010-12-06 04:08:22 +08:00
|
|
|
{
|
2019-06-08 00:24:27 +08:00
|
|
|
struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
|
|
|
u8 tcer;
|
|
|
|
|
|
|
|
tcer = readb(drvdata->base + TCU_REG_WDT_TCER);
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
jz4740_timer_enable_watchdog();
|
|
|
|
jz4740_wdt_set_timeout(wdt_dev, wdt_dev->timeout);
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2019-06-08 00:24:27 +08:00
|
|
|
/* Start watchdog if it wasn't started already */
|
|
|
|
if (!(tcer & TCU_WDT_TCER_TCEN))
|
|
|
|
writeb(TCU_WDT_TCER_TCEN, drvdata->base + TCU_REG_WDT_TCER);
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
return 0;
|
2010-12-06 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
static int jz4740_wdt_stop(struct watchdog_device *wdt_dev)
|
2010-12-06 04:08:22 +08:00
|
|
|
{
|
2012-01-26 18:10:45 +08:00
|
|
|
struct jz4740_wdt_drvdata *drvdata = watchdog_get_drvdata(wdt_dev);
|
2011-01-17 03:05:39 +08:00
|
|
|
|
2019-06-08 00:24:26 +08:00
|
|
|
writeb(0x0, drvdata->base + TCU_REG_WDT_TCER);
|
2018-05-11 02:47:44 +08:00
|
|
|
jz4740_timer_disable_watchdog();
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
return 0;
|
2010-12-06 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 02:47:46 +08:00
|
|
|
static int jz4740_wdt_restart(struct watchdog_device *wdt_dev,
|
|
|
|
unsigned long action, void *data)
|
|
|
|
{
|
|
|
|
wdt_dev->timeout = 0;
|
|
|
|
jz4740_wdt_start(wdt_dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
static const struct watchdog_info jz4740_wdt_info = {
|
|
|
|
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
|
2010-12-06 04:08:22 +08:00
|
|
|
.identity = "jz4740 Watchdog",
|
|
|
|
};
|
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
static const struct watchdog_ops jz4740_wdt_ops = {
|
2010-12-06 04:08:22 +08:00
|
|
|
.owner = THIS_MODULE,
|
2012-01-26 18:10:45 +08:00
|
|
|
.start = jz4740_wdt_start,
|
|
|
|
.stop = jz4740_wdt_stop,
|
|
|
|
.ping = jz4740_wdt_ping,
|
|
|
|
.set_timeout = jz4740_wdt_set_timeout,
|
2018-05-11 02:47:46 +08:00
|
|
|
.restart = jz4740_wdt_restart,
|
2010-12-06 04:08:22 +08:00
|
|
|
};
|
|
|
|
|
2015-02-03 18:25:48 +08:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static const struct of_device_id jz4740_wdt_of_matches[] = {
|
|
|
|
{ .compatible = "ingenic,jz4740-watchdog", },
|
2017-09-16 03:20:19 +08:00
|
|
|
{ .compatible = "ingenic,jz4780-watchdog", },
|
2015-02-03 18:25:48 +08:00
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
2016-11-11 08:02:20 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, jz4740_wdt_of_matches);
|
2015-02-03 18:25:48 +08:00
|
|
|
#endif
|
|
|
|
|
2012-11-20 02:21:41 +08:00
|
|
|
static int jz4740_wdt_probe(struct platform_device *pdev)
|
2010-12-06 04:08:22 +08:00
|
|
|
{
|
2019-04-11 00:28:01 +08:00
|
|
|
struct device *dev = &pdev->dev;
|
2012-01-26 18:10:45 +08:00
|
|
|
struct jz4740_wdt_drvdata *drvdata;
|
|
|
|
struct watchdog_device *jz4740_wdt;
|
|
|
|
|
2019-04-11 00:28:01 +08:00
|
|
|
drvdata = devm_kzalloc(dev, sizeof(struct jz4740_wdt_drvdata),
|
2012-01-26 18:10:45 +08:00
|
|
|
GFP_KERNEL);
|
2016-04-27 01:18:49 +08:00
|
|
|
if (!drvdata)
|
2012-01-26 18:10:45 +08:00
|
|
|
return -ENOMEM;
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
if (heartbeat < 1 || heartbeat > MAX_HEARTBEAT)
|
|
|
|
heartbeat = DEFAULT_HEARTBEAT;
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2012-01-26 18:10:45 +08:00
|
|
|
jz4740_wdt = &drvdata->wdt;
|
|
|
|
jz4740_wdt->info = &jz4740_wdt_info;
|
|
|
|
jz4740_wdt->ops = &jz4740_wdt_ops;
|
|
|
|
jz4740_wdt->timeout = heartbeat;
|
|
|
|
jz4740_wdt->min_timeout = 1;
|
|
|
|
jz4740_wdt->max_timeout = MAX_HEARTBEAT;
|
2019-04-11 00:28:01 +08:00
|
|
|
jz4740_wdt->parent = dev;
|
2012-01-26 18:10:45 +08:00
|
|
|
watchdog_set_nowayout(jz4740_wdt, nowayout);
|
|
|
|
watchdog_set_drvdata(jz4740_wdt, drvdata);
|
|
|
|
|
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-03 03:01:53 +08:00
|
|
|
drvdata->base = devm_platform_ioremap_resource(pdev, 0);
|
2018-05-11 02:47:45 +08:00
|
|
|
if (IS_ERR(drvdata->base))
|
|
|
|
return PTR_ERR(drvdata->base);
|
2010-12-06 04:08:22 +08:00
|
|
|
|
2019-04-11 00:28:01 +08:00
|
|
|
drvdata->rtc_clk = devm_clk_get(dev, "rtc");
|
2012-01-26 18:10:45 +08:00
|
|
|
if (IS_ERR(drvdata->rtc_clk)) {
|
2019-04-11 00:28:01 +08:00
|
|
|
dev_err(dev, "cannot find RTC clock\n");
|
2018-05-11 02:47:45 +08:00
|
|
|
return PTR_ERR(drvdata->rtc_clk);
|
2010-12-06 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
2019-05-19 05:27:34 +08:00
|
|
|
return devm_watchdog_register_device(dev, &drvdata->wdt);
|
2010-12-06 04:08:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver jz4740_wdt_driver = {
|
|
|
|
.probe = jz4740_wdt_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "jz4740-wdt",
|
2015-02-03 18:25:48 +08:00
|
|
|
.of_match_table = of_match_ptr(jz4740_wdt_of_matches),
|
2010-12-06 04:08:22 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-11-29 13:56:27 +08:00
|
|
|
module_platform_driver(jz4740_wdt_driver);
|
2010-12-06 04:08:22 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Paul Cercueil <paul@crapouillou.net>");
|
|
|
|
MODULE_DESCRIPTION("jz4740 Watchdog Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("platform:jz4740-wdt");
|