2018-03-16 18:14:11 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-08-31 21:02:43 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Broadcom Corporation
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
|
|
|
|
#define WDT_START_1 0xff00
|
|
|
|
#define WDT_START_2 0x00ff
|
|
|
|
#define WDT_STOP_1 0xee00
|
|
|
|
#define WDT_STOP_2 0x00ee
|
|
|
|
|
|
|
|
#define WDT_TIMEOUT_REG 0x0
|
|
|
|
#define WDT_CMD_REG 0x4
|
|
|
|
|
|
|
|
#define WDT_MIN_TIMEOUT 1 /* seconds */
|
|
|
|
#define WDT_DEFAULT_TIMEOUT 30 /* seconds */
|
|
|
|
#define WDT_DEFAULT_RATE 27000000
|
|
|
|
|
|
|
|
struct bcm7038_watchdog {
|
|
|
|
void __iomem *base;
|
|
|
|
struct watchdog_device wdd;
|
|
|
|
u32 rate;
|
|
|
|
struct clk *clk;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool nowayout = WATCHDOG_NOWAYOUT;
|
|
|
|
|
2021-02-23 11:00:42 +03:00
|
|
|
static inline void bcm7038_wdt_write(u32 value, void __iomem *addr)
|
|
|
|
{
|
|
|
|
/* MIPS chips strapped for BE will automagically configure the
|
|
|
|
* peripheral registers for CPU-native byte order.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
|
|
|
|
__raw_writel(value, addr);
|
|
|
|
else
|
|
|
|
writel_relaxed(value, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bcm7038_wdt_read(void __iomem *addr)
|
|
|
|
{
|
|
|
|
if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
|
|
|
|
return __raw_readl(addr);
|
|
|
|
else
|
|
|
|
return readl_relaxed(addr);
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:02:43 +03:00
|
|
|
static void bcm7038_wdt_set_timeout_reg(struct watchdog_device *wdog)
|
|
|
|
{
|
|
|
|
struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
|
|
|
|
u32 timeout;
|
|
|
|
|
|
|
|
timeout = wdt->rate * wdog->timeout;
|
|
|
|
|
2021-02-23 11:00:42 +03:00
|
|
|
bcm7038_wdt_write(timeout, wdt->base + WDT_TIMEOUT_REG);
|
2015-08-31 21:02:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm7038_wdt_ping(struct watchdog_device *wdog)
|
|
|
|
{
|
|
|
|
struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
|
|
|
|
|
2021-02-23 11:00:42 +03:00
|
|
|
bcm7038_wdt_write(WDT_START_1, wdt->base + WDT_CMD_REG);
|
|
|
|
bcm7038_wdt_write(WDT_START_2, wdt->base + WDT_CMD_REG);
|
2015-08-31 21:02:43 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm7038_wdt_start(struct watchdog_device *wdog)
|
|
|
|
{
|
|
|
|
bcm7038_wdt_set_timeout_reg(wdog);
|
|
|
|
bcm7038_wdt_ping(wdog);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm7038_wdt_stop(struct watchdog_device *wdog)
|
|
|
|
{
|
|
|
|
struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
|
|
|
|
|
2021-02-23 11:00:42 +03:00
|
|
|
bcm7038_wdt_write(WDT_STOP_1, wdt->base + WDT_CMD_REG);
|
|
|
|
bcm7038_wdt_write(WDT_STOP_2, wdt->base + WDT_CMD_REG);
|
2015-08-31 21:02:43 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm7038_wdt_set_timeout(struct watchdog_device *wdog,
|
|
|
|
unsigned int t)
|
|
|
|
{
|
|
|
|
/* Can't modify timeout value if watchdog timer is running */
|
|
|
|
bcm7038_wdt_stop(wdog);
|
|
|
|
wdog->timeout = t;
|
|
|
|
bcm7038_wdt_start(wdog);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int bcm7038_wdt_get_timeleft(struct watchdog_device *wdog)
|
|
|
|
{
|
|
|
|
struct bcm7038_watchdog *wdt = watchdog_get_drvdata(wdog);
|
|
|
|
u32 time_left;
|
|
|
|
|
2021-02-23 11:00:42 +03:00
|
|
|
time_left = bcm7038_wdt_read(wdt->base + WDT_CMD_REG);
|
2015-08-31 21:02:43 +03:00
|
|
|
|
|
|
|
return time_left / wdt->rate;
|
|
|
|
}
|
|
|
|
|
2016-12-26 20:05:11 +03:00
|
|
|
static const struct watchdog_info bcm7038_wdt_info = {
|
2015-08-31 21:02:43 +03:00
|
|
|
.identity = "Broadcom BCM7038 Watchdog Timer",
|
|
|
|
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
|
|
|
|
WDIOF_MAGICCLOSE
|
|
|
|
};
|
|
|
|
|
2016-09-01 20:35:26 +03:00
|
|
|
static const struct watchdog_ops bcm7038_wdt_ops = {
|
2015-08-31 21:02:43 +03:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.start = bcm7038_wdt_start,
|
|
|
|
.stop = bcm7038_wdt_stop,
|
|
|
|
.set_timeout = bcm7038_wdt_set_timeout,
|
|
|
|
.get_timeleft = bcm7038_wdt_get_timeleft,
|
|
|
|
};
|
|
|
|
|
2019-04-08 22:38:30 +03:00
|
|
|
static void bcm7038_clk_disable_unprepare(void *data)
|
|
|
|
{
|
|
|
|
clk_disable_unprepare(data);
|
|
|
|
}
|
|
|
|
|
2015-08-31 21:02:43 +03:00
|
|
|
static int bcm7038_wdt_probe(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct bcm7038_watchdog *wdt;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
|
|
|
|
if (!wdt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, wdt);
|
|
|
|
|
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 22:01:53 +03:00
|
|
|
wdt->base = devm_platform_ioremap_resource(pdev, 0);
|
2015-08-31 21:02:43 +03:00
|
|
|
if (IS_ERR(wdt->base))
|
|
|
|
return PTR_ERR(wdt->base);
|
|
|
|
|
|
|
|
wdt->clk = devm_clk_get(dev, NULL);
|
|
|
|
/* If unable to get clock, use default frequency */
|
|
|
|
if (!IS_ERR(wdt->clk)) {
|
2017-07-22 23:22:59 +03:00
|
|
|
err = clk_prepare_enable(wdt->clk);
|
2019-04-08 22:38:30 +03:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
err = devm_add_action_or_reset(dev,
|
|
|
|
bcm7038_clk_disable_unprepare,
|
|
|
|
wdt->clk);
|
2017-07-22 23:22:59 +03:00
|
|
|
if (err)
|
|
|
|
return err;
|
2015-08-31 21:02:43 +03:00
|
|
|
wdt->rate = clk_get_rate(wdt->clk);
|
|
|
|
/* Prevent divide-by-zero exception */
|
|
|
|
if (!wdt->rate)
|
|
|
|
wdt->rate = WDT_DEFAULT_RATE;
|
|
|
|
} else {
|
|
|
|
wdt->rate = WDT_DEFAULT_RATE;
|
|
|
|
wdt->clk = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wdt->wdd.info = &bcm7038_wdt_info;
|
|
|
|
wdt->wdd.ops = &bcm7038_wdt_ops;
|
|
|
|
wdt->wdd.min_timeout = WDT_MIN_TIMEOUT;
|
|
|
|
wdt->wdd.timeout = WDT_DEFAULT_TIMEOUT;
|
|
|
|
wdt->wdd.max_timeout = 0xffffffff / wdt->rate;
|
|
|
|
wdt->wdd.parent = dev;
|
|
|
|
watchdog_set_drvdata(&wdt->wdd, wdt);
|
|
|
|
|
2019-04-08 22:38:30 +03:00
|
|
|
watchdog_stop_on_reboot(&wdt->wdd);
|
|
|
|
watchdog_stop_on_unregister(&wdt->wdd);
|
|
|
|
err = devm_watchdog_register_device(dev, &wdt->wdd);
|
2019-05-19 00:27:20 +03:00
|
|
|
if (err)
|
2015-08-31 21:02:43 +03:00
|
|
|
return err;
|
|
|
|
|
|
|
|
dev_info(dev, "Registered BCM7038 Watchdog\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static int bcm7038_wdt_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
if (watchdog_active(&wdt->wdd))
|
|
|
|
return bcm7038_wdt_stop(&wdt->wdd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int bcm7038_wdt_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct bcm7038_watchdog *wdt = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
if (watchdog_active(&wdt->wdd))
|
|
|
|
return bcm7038_wdt_start(&wdt->wdd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static SIMPLE_DEV_PM_OPS(bcm7038_wdt_pm_ops, bcm7038_wdt_suspend,
|
|
|
|
bcm7038_wdt_resume);
|
|
|
|
|
|
|
|
static const struct of_device_id bcm7038_wdt_match[] = {
|
|
|
|
{ .compatible = "brcm,bcm7038-wdt" },
|
|
|
|
{},
|
|
|
|
};
|
2016-10-14 18:23:50 +03:00
|
|
|
MODULE_DEVICE_TABLE(of, bcm7038_wdt_match);
|
2015-08-31 21:02:43 +03:00
|
|
|
|
|
|
|
static struct platform_driver bcm7038_wdt_driver = {
|
|
|
|
.probe = bcm7038_wdt_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "bcm7038-wdt",
|
|
|
|
.of_match_table = bcm7038_wdt_match,
|
|
|
|
.pm = &bcm7038_wdt_pm_ops,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
module_platform_driver(bcm7038_wdt_driver);
|
|
|
|
|
|
|
|
module_param(nowayout, bool, 0);
|
|
|
|
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
|
|
|
|
__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|
2018-03-16 18:14:11 +03:00
|
|
|
MODULE_LICENSE("GPL");
|
2015-08-31 21:02:43 +03:00
|
|
|
MODULE_DESCRIPTION("Driver for Broadcom 7038 SoCs Watchdog");
|
|
|
|
MODULE_AUTHOR("Justin Chen");
|