2019-05-27 13:55:01 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-07-31 02:20:46 +07:00
|
|
|
/*
|
|
|
|
* sunxi Watchdog Driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Carlo Caione
|
|
|
|
* 2012 Henrik Nordstrom
|
|
|
|
*
|
|
|
|
* Based on xen_wdt.c
|
|
|
|
* (c) Copyright 2010 Novell, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
2014-05-07 09:44:19 +07:00
|
|
|
#include <linux/delay.h>
|
2013-07-31 02:20:46 +07:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/of.h>
|
2014-09-21 23:05:18 +07:00
|
|
|
#include <linux/of_device.h>
|
2013-07-31 02:20:46 +07:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
|
|
|
|
#define WDT_MAX_TIMEOUT 16
|
|
|
|
#define WDT_MIN_TIMEOUT 1
|
2014-09-21 23:05:18 +07:00
|
|
|
#define WDT_TIMEOUT_MASK 0x0F
|
2013-07-31 02:20:46 +07:00
|
|
|
|
|
|
|
#define WDT_CTRL_RELOAD ((1 << 0) | (0x0a57 << 1))
|
|
|
|
|
|
|
|
#define WDT_MODE_EN (1 << 0)
|
|
|
|
|
|
|
|
#define DRV_NAME "sunxi-wdt"
|
|
|
|
#define DRV_VERSION "1.0"
|
|
|
|
|
|
|
|
static bool nowayout = WATCHDOG_NOWAYOUT;
|
2018-02-12 03:08:42 +07:00
|
|
|
static unsigned int timeout;
|
2013-07-31 02:20:46 +07:00
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
/*
|
|
|
|
* This structure stores the register offsets for different variants
|
|
|
|
* of Allwinner's watchdog hardware.
|
|
|
|
*/
|
|
|
|
struct sunxi_wdt_reg {
|
|
|
|
u8 wdt_ctrl;
|
|
|
|
u8 wdt_cfg;
|
|
|
|
u8 wdt_mode;
|
|
|
|
u8 wdt_timeout_shift;
|
|
|
|
u8 wdt_reset_mask;
|
|
|
|
u8 wdt_reset_val;
|
|
|
|
};
|
|
|
|
|
2013-07-31 02:20:46 +07:00
|
|
|
struct sunxi_wdt_dev {
|
|
|
|
struct watchdog_device wdt_dev;
|
|
|
|
void __iomem *wdt_base;
|
2014-09-21 23:05:18 +07:00
|
|
|
const struct sunxi_wdt_reg *wdt_regs;
|
2013-07-31 02:20:46 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* wdt_timeout_map maps the watchdog timer interval value in seconds to
|
2014-09-21 23:05:18 +07:00
|
|
|
* the value of the register WDT_MODE at bits .wdt_timeout_shift ~ +3
|
2013-07-31 02:20:46 +07:00
|
|
|
*
|
|
|
|
* [timeout seconds] = register value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const int wdt_timeout_map[] = {
|
2014-04-05 00:24:25 +07:00
|
|
|
[1] = 0x1, /* 1s */
|
|
|
|
[2] = 0x2, /* 2s */
|
|
|
|
[3] = 0x3, /* 3s */
|
|
|
|
[4] = 0x4, /* 4s */
|
|
|
|
[5] = 0x5, /* 5s */
|
|
|
|
[6] = 0x6, /* 6s */
|
|
|
|
[8] = 0x7, /* 8s */
|
|
|
|
[10] = 0x8, /* 10s */
|
|
|
|
[12] = 0x9, /* 12s */
|
|
|
|
[14] = 0xA, /* 14s */
|
|
|
|
[16] = 0xB, /* 16s */
|
2013-07-31 02:20:46 +07:00
|
|
|
};
|
|
|
|
|
2014-05-07 09:44:19 +07:00
|
|
|
|
2016-02-27 08:32:49 +07:00
|
|
|
static int sunxi_wdt_restart(struct watchdog_device *wdt_dev,
|
|
|
|
unsigned long action, void *data)
|
2014-05-07 09:44:19 +07:00
|
|
|
{
|
2015-11-17 00:28:11 +07:00
|
|
|
struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
|
2014-09-26 07:03:17 +07:00
|
|
|
void __iomem *wdt_base = sunxi_wdt->wdt_base;
|
2014-09-21 23:05:18 +07:00
|
|
|
const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
/* Set system reset function */
|
|
|
|
val = readl(wdt_base + regs->wdt_cfg);
|
|
|
|
val &= ~(regs->wdt_reset_mask);
|
|
|
|
val |= regs->wdt_reset_val;
|
|
|
|
writel(val, wdt_base + regs->wdt_cfg);
|
2014-09-26 07:03:17 +07:00
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
/* Set lowest timeout and enable watchdog */
|
|
|
|
val = readl(wdt_base + regs->wdt_mode);
|
|
|
|
val &= ~(WDT_TIMEOUT_MASK << regs->wdt_timeout_shift);
|
|
|
|
val |= WDT_MODE_EN;
|
|
|
|
writel(val, wdt_base + regs->wdt_mode);
|
2014-05-07 09:44:19 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Restart the watchdog. The default (and lowest) interval
|
|
|
|
* value for the watchdog is 0.5s.
|
|
|
|
*/
|
2014-09-21 23:05:18 +07:00
|
|
|
writel(WDT_CTRL_RELOAD, wdt_base + regs->wdt_ctrl);
|
2014-05-07 09:44:19 +07:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
mdelay(5);
|
2014-09-21 23:05:18 +07:00
|
|
|
val = readl(wdt_base + regs->wdt_mode);
|
|
|
|
val |= WDT_MODE_EN;
|
|
|
|
writel(val, wdt_base + regs->wdt_mode);
|
2014-05-07 09:44:19 +07:00
|
|
|
}
|
2015-11-17 00:28:11 +07:00
|
|
|
return 0;
|
2014-05-07 09:44:19 +07:00
|
|
|
}
|
|
|
|
|
2013-07-31 02:20:46 +07:00
|
|
|
static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
|
|
|
|
{
|
|
|
|
struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
|
|
|
|
void __iomem *wdt_base = sunxi_wdt->wdt_base;
|
2014-09-21 23:05:18 +07:00
|
|
|
const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
|
2013-07-31 02:20:46 +07:00
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
writel(WDT_CTRL_RELOAD, wdt_base + regs->wdt_ctrl);
|
2013-07-31 02:20:46 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sunxi_wdt_set_timeout(struct watchdog_device *wdt_dev,
|
|
|
|
unsigned int timeout)
|
|
|
|
{
|
|
|
|
struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
|
|
|
|
void __iomem *wdt_base = sunxi_wdt->wdt_base;
|
2014-09-21 23:05:18 +07:00
|
|
|
const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
|
2013-07-31 02:20:46 +07:00
|
|
|
u32 reg;
|
|
|
|
|
|
|
|
if (wdt_timeout_map[timeout] == 0)
|
|
|
|
timeout++;
|
|
|
|
|
|
|
|
sunxi_wdt->wdt_dev.timeout = timeout;
|
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
reg = readl(wdt_base + regs->wdt_mode);
|
|
|
|
reg &= ~(WDT_TIMEOUT_MASK << regs->wdt_timeout_shift);
|
|
|
|
reg |= wdt_timeout_map[timeout] << regs->wdt_timeout_shift;
|
|
|
|
writel(reg, wdt_base + regs->wdt_mode);
|
2013-07-31 02:20:46 +07:00
|
|
|
|
|
|
|
sunxi_wdt_ping(wdt_dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sunxi_wdt_stop(struct watchdog_device *wdt_dev)
|
|
|
|
{
|
|
|
|
struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
|
|
|
|
void __iomem *wdt_base = sunxi_wdt->wdt_base;
|
2014-09-21 23:05:18 +07:00
|
|
|
const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
|
2013-07-31 02:20:46 +07:00
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
writel(0, wdt_base + regs->wdt_mode);
|
2013-07-31 02:20:46 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sunxi_wdt_start(struct watchdog_device *wdt_dev)
|
|
|
|
{
|
|
|
|
u32 reg;
|
|
|
|
struct sunxi_wdt_dev *sunxi_wdt = watchdog_get_drvdata(wdt_dev);
|
|
|
|
void __iomem *wdt_base = sunxi_wdt->wdt_base;
|
2014-09-21 23:05:18 +07:00
|
|
|
const struct sunxi_wdt_reg *regs = sunxi_wdt->wdt_regs;
|
2013-07-31 02:20:46 +07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = sunxi_wdt_set_timeout(&sunxi_wdt->wdt_dev,
|
|
|
|
sunxi_wdt->wdt_dev.timeout);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
/* Set system reset function */
|
|
|
|
reg = readl(wdt_base + regs->wdt_cfg);
|
|
|
|
reg &= ~(regs->wdt_reset_mask);
|
2015-07-25 13:25:18 +07:00
|
|
|
reg |= regs->wdt_reset_val;
|
2014-09-21 23:05:18 +07:00
|
|
|
writel(reg, wdt_base + regs->wdt_cfg);
|
|
|
|
|
|
|
|
/* Enable watchdog */
|
|
|
|
reg = readl(wdt_base + regs->wdt_mode);
|
|
|
|
reg |= WDT_MODE_EN;
|
|
|
|
writel(reg, wdt_base + regs->wdt_mode);
|
2013-07-31 02:20:46 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct watchdog_info sunxi_wdt_info = {
|
|
|
|
.identity = DRV_NAME,
|
|
|
|
.options = WDIOF_SETTIMEOUT |
|
|
|
|
WDIOF_KEEPALIVEPING |
|
|
|
|
WDIOF_MAGICCLOSE,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct watchdog_ops sunxi_wdt_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.start = sunxi_wdt_start,
|
|
|
|
.stop = sunxi_wdt_stop,
|
|
|
|
.ping = sunxi_wdt_ping,
|
|
|
|
.set_timeout = sunxi_wdt_set_timeout,
|
2015-11-17 00:28:11 +07:00
|
|
|
.restart = sunxi_wdt_restart,
|
2013-07-31 02:20:46 +07:00
|
|
|
};
|
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
static const struct sunxi_wdt_reg sun4i_wdt_reg = {
|
|
|
|
.wdt_ctrl = 0x00,
|
|
|
|
.wdt_cfg = 0x04,
|
|
|
|
.wdt_mode = 0x04,
|
|
|
|
.wdt_timeout_shift = 3,
|
|
|
|
.wdt_reset_mask = 0x02,
|
|
|
|
.wdt_reset_val = 0x02,
|
|
|
|
};
|
|
|
|
|
2014-09-21 23:05:19 +07:00
|
|
|
static const struct sunxi_wdt_reg sun6i_wdt_reg = {
|
|
|
|
.wdt_ctrl = 0x10,
|
|
|
|
.wdt_cfg = 0x14,
|
|
|
|
.wdt_mode = 0x18,
|
|
|
|
.wdt_timeout_shift = 4,
|
|
|
|
.wdt_reset_mask = 0x03,
|
|
|
|
.wdt_reset_val = 0x01,
|
|
|
|
};
|
|
|
|
|
2014-09-21 23:05:18 +07:00
|
|
|
static const struct of_device_id sunxi_wdt_dt_ids[] = {
|
|
|
|
{ .compatible = "allwinner,sun4i-a10-wdt", .data = &sun4i_wdt_reg },
|
2014-09-21 23:05:19 +07:00
|
|
|
{ .compatible = "allwinner,sun6i-a31-wdt", .data = &sun6i_wdt_reg },
|
2014-09-21 23:05:18 +07:00
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, sunxi_wdt_dt_ids);
|
|
|
|
|
2013-10-05 21:20:17 +07:00
|
|
|
static int sunxi_wdt_probe(struct platform_device *pdev)
|
2013-07-31 02:20:46 +07:00
|
|
|
{
|
2019-04-10 00:24:00 +07:00
|
|
|
struct device *dev = &pdev->dev;
|
2013-07-31 02:20:46 +07:00
|
|
|
struct sunxi_wdt_dev *sunxi_wdt;
|
|
|
|
int err;
|
|
|
|
|
2019-04-10 00:24:00 +07:00
|
|
|
sunxi_wdt = devm_kzalloc(dev, sizeof(*sunxi_wdt), GFP_KERNEL);
|
2013-07-31 02:20:46 +07:00
|
|
|
if (!sunxi_wdt)
|
2020-05-29 16:45:14 +07:00
|
|
|
return -ENOMEM;
|
2013-07-31 02:20:46 +07:00
|
|
|
|
2019-04-10 00:24:00 +07:00
|
|
|
sunxi_wdt->wdt_regs = of_device_get_match_data(dev);
|
2017-10-22 00:45:13 +07:00
|
|
|
if (!sunxi_wdt->wdt_regs)
|
2014-09-21 23:05:18 +07:00
|
|
|
return -ENODEV;
|
|
|
|
|
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 02:01:53 +07:00
|
|
|
sunxi_wdt->wdt_base = devm_platform_ioremap_resource(pdev, 0);
|
2013-07-31 02:20:46 +07:00
|
|
|
if (IS_ERR(sunxi_wdt->wdt_base))
|
|
|
|
return PTR_ERR(sunxi_wdt->wdt_base);
|
|
|
|
|
|
|
|
sunxi_wdt->wdt_dev.info = &sunxi_wdt_info;
|
|
|
|
sunxi_wdt->wdt_dev.ops = &sunxi_wdt_ops;
|
|
|
|
sunxi_wdt->wdt_dev.timeout = WDT_MAX_TIMEOUT;
|
|
|
|
sunxi_wdt->wdt_dev.max_timeout = WDT_MAX_TIMEOUT;
|
|
|
|
sunxi_wdt->wdt_dev.min_timeout = WDT_MIN_TIMEOUT;
|
2019-04-10 00:24:00 +07:00
|
|
|
sunxi_wdt->wdt_dev.parent = dev;
|
2013-07-31 02:20:46 +07:00
|
|
|
|
2019-04-10 00:24:00 +07:00
|
|
|
watchdog_init_timeout(&sunxi_wdt->wdt_dev, timeout, dev);
|
2013-07-31 02:20:46 +07:00
|
|
|
watchdog_set_nowayout(&sunxi_wdt->wdt_dev, nowayout);
|
2015-11-17 00:28:11 +07:00
|
|
|
watchdog_set_restart_priority(&sunxi_wdt->wdt_dev, 128);
|
2013-07-31 02:20:46 +07:00
|
|
|
|
|
|
|
watchdog_set_drvdata(&sunxi_wdt->wdt_dev, sunxi_wdt);
|
|
|
|
|
|
|
|
sunxi_wdt_stop(&sunxi_wdt->wdt_dev);
|
|
|
|
|
2017-01-11 06:21:59 +07:00
|
|
|
watchdog_stop_on_reboot(&sunxi_wdt->wdt_dev);
|
2019-04-10 00:24:00 +07:00
|
|
|
err = devm_watchdog_register_device(dev, &sunxi_wdt->wdt_dev);
|
2013-07-31 02:20:46 +07:00
|
|
|
if (unlikely(err))
|
|
|
|
return err;
|
|
|
|
|
2019-04-10 00:24:00 +07:00
|
|
|
dev_info(dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
|
|
|
|
sunxi_wdt->wdt_dev.timeout, nowayout);
|
2013-07-31 02:20:46 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver sunxi_wdt_driver = {
|
|
|
|
.probe = sunxi_wdt_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = DRV_NAME,
|
2013-09-30 11:42:51 +07:00
|
|
|
.of_match_table = sunxi_wdt_dt_ids,
|
2013-07-31 02:20:46 +07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(sunxi_wdt_driver);
|
|
|
|
|
|
|
|
module_param(timeout, uint, 0);
|
|
|
|
MODULE_PARM_DESC(timeout, "Watchdog heartbeat in seconds");
|
|
|
|
|
|
|
|
module_param(nowayout, bool, 0);
|
|
|
|
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
|
|
|
|
"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Carlo Caione <carlo.caione@gmail.com>");
|
|
|
|
MODULE_AUTHOR("Henrik Nordstrom <henrik@henriknordstrom.net>");
|
|
|
|
MODULE_DESCRIPTION("sunxi WatchDog Timer Driver");
|
|
|
|
MODULE_VERSION(DRV_VERSION);
|