2019-05-27 09:55:01 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-02-26 20:20:22 +03:00
|
|
|
/*
|
|
|
|
* PIC32 deadman timer driver
|
|
|
|
*
|
|
|
|
* Purna Chandra Mandal <purna.mandal@microchip.com>
|
|
|
|
* Copyright (c) 2016, Microchip Technology Inc.
|
|
|
|
*/
|
|
|
|
#include <linux/clk.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pm.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
|
|
|
|
#include <asm/mach-pic32/pic32.h>
|
|
|
|
|
|
|
|
/* Deadman Timer Regs */
|
|
|
|
#define DMTCON_REG 0x00
|
|
|
|
#define DMTPRECLR_REG 0x10
|
|
|
|
#define DMTCLR_REG 0x20
|
|
|
|
#define DMTSTAT_REG 0x30
|
|
|
|
#define DMTCNT_REG 0x40
|
|
|
|
#define DMTPSCNT_REG 0x60
|
|
|
|
#define DMTPSINTV_REG 0x70
|
|
|
|
|
|
|
|
/* Deadman Timer Regs fields */
|
|
|
|
#define DMT_ON BIT(15)
|
|
|
|
#define DMT_STEP1_KEY BIT(6)
|
|
|
|
#define DMT_STEP2_KEY BIT(3)
|
|
|
|
#define DMTSTAT_WINOPN BIT(0)
|
|
|
|
#define DMTSTAT_EVENT BIT(5)
|
|
|
|
#define DMTSTAT_BAD2 BIT(6)
|
|
|
|
#define DMTSTAT_BAD1 BIT(7)
|
|
|
|
|
|
|
|
/* Reset Control Register fields for watchdog */
|
|
|
|
#define RESETCON_DMT_TIMEOUT BIT(5)
|
|
|
|
|
|
|
|
struct pic32_dmt {
|
|
|
|
void __iomem *regs;
|
|
|
|
struct clk *clk;
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void dmt_enable(struct pic32_dmt *dmt)
|
|
|
|
{
|
|
|
|
writel(DMT_ON, PIC32_SET(dmt->regs + DMTCON_REG));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dmt_disable(struct pic32_dmt *dmt)
|
|
|
|
{
|
|
|
|
writel(DMT_ON, PIC32_CLR(dmt->regs + DMTCON_REG));
|
|
|
|
/*
|
|
|
|
* Cannot touch registers in the CPU cycle following clearing the
|
|
|
|
* ON bit.
|
|
|
|
*/
|
|
|
|
nop();
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int dmt_bad_status(struct pic32_dmt *dmt)
|
|
|
|
{
|
|
|
|
u32 val;
|
|
|
|
|
|
|
|
val = readl(dmt->regs + DMTSTAT_REG);
|
|
|
|
val &= (DMTSTAT_BAD1 | DMTSTAT_BAD2 | DMTSTAT_EVENT);
|
|
|
|
if (val)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int dmt_keepalive(struct pic32_dmt *dmt)
|
|
|
|
{
|
|
|
|
u32 v;
|
|
|
|
u32 timeout = 500;
|
|
|
|
|
|
|
|
/* set pre-clear key */
|
|
|
|
writel(DMT_STEP1_KEY << 8, dmt->regs + DMTPRECLR_REG);
|
|
|
|
|
|
|
|
/* wait for DMT window to open */
|
|
|
|
while (--timeout) {
|
|
|
|
v = readl(dmt->regs + DMTSTAT_REG) & DMTSTAT_WINOPN;
|
|
|
|
if (v == DMTSTAT_WINOPN)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply key2 */
|
|
|
|
writel(DMT_STEP2_KEY, dmt->regs + DMTCLR_REG);
|
|
|
|
|
|
|
|
/* check whether keys are latched correctly */
|
|
|
|
return dmt_bad_status(dmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 pic32_dmt_get_timeout_secs(struct pic32_dmt *dmt)
|
|
|
|
{
|
|
|
|
unsigned long rate;
|
|
|
|
|
|
|
|
rate = clk_get_rate(dmt->clk);
|
|
|
|
if (rate)
|
|
|
|
return readl(dmt->regs + DMTPSCNT_REG) / rate;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 pic32_dmt_bootstatus(struct pic32_dmt *dmt)
|
|
|
|
{
|
|
|
|
u32 v;
|
|
|
|
void __iomem *rst_base;
|
|
|
|
|
|
|
|
rst_base = ioremap(PIC32_BASE_RESET, 0x10);
|
|
|
|
if (!rst_base)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
v = readl(rst_base);
|
|
|
|
|
|
|
|
writel(RESETCON_DMT_TIMEOUT, PIC32_CLR(rst_base));
|
|
|
|
|
|
|
|
iounmap(rst_base);
|
|
|
|
return v & RESETCON_DMT_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pic32_dmt_start(struct watchdog_device *wdd)
|
|
|
|
{
|
|
|
|
struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
|
|
|
|
|
|
|
|
dmt_enable(dmt);
|
|
|
|
return dmt_keepalive(dmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pic32_dmt_stop(struct watchdog_device *wdd)
|
|
|
|
{
|
|
|
|
struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
|
|
|
|
|
|
|
|
dmt_disable(dmt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pic32_dmt_ping(struct watchdog_device *wdd)
|
|
|
|
{
|
|
|
|
struct pic32_dmt *dmt = watchdog_get_drvdata(wdd);
|
|
|
|
|
|
|
|
return dmt_keepalive(dmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct watchdog_ops pic32_dmt_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.start = pic32_dmt_start,
|
|
|
|
.stop = pic32_dmt_stop,
|
|
|
|
.ping = pic32_dmt_ping,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct watchdog_info pic32_dmt_ident = {
|
|
|
|
.options = WDIOF_KEEPALIVEPING |
|
|
|
|
WDIOF_MAGICCLOSE,
|
|
|
|
.identity = "PIC32 Deadman Timer",
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct watchdog_device pic32_dmt_wdd = {
|
|
|
|
.info = &pic32_dmt_ident,
|
|
|
|
.ops = &pic32_dmt_fops,
|
|
|
|
};
|
|
|
|
|
2019-04-10 19:27:56 +03:00
|
|
|
static void pic32_clk_disable_unprepare(void *data)
|
|
|
|
{
|
|
|
|
clk_disable_unprepare(data);
|
|
|
|
}
|
|
|
|
|
2016-02-26 20:20:22 +03:00
|
|
|
static int pic32_dmt_probe(struct platform_device *pdev)
|
|
|
|
{
|
2019-04-10 19:27:56 +03:00
|
|
|
struct device *dev = &pdev->dev;
|
2016-02-26 20:20:22 +03:00
|
|
|
int ret;
|
|
|
|
struct pic32_dmt *dmt;
|
|
|
|
struct watchdog_device *wdd = &pic32_dmt_wdd;
|
|
|
|
|
2019-04-10 19:27:56 +03:00
|
|
|
dmt = devm_kzalloc(dev, sizeof(*dmt), GFP_KERNEL);
|
2016-06-17 20:12:05 +03:00
|
|
|
if (!dmt)
|
|
|
|
return -ENOMEM;
|
2016-02-26 20:20:22 +03:00
|
|
|
|
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
|
|
|
dmt->regs = devm_platform_ioremap_resource(pdev, 0);
|
2016-02-26 20:20:22 +03:00
|
|
|
if (IS_ERR(dmt->regs))
|
|
|
|
return PTR_ERR(dmt->regs);
|
|
|
|
|
2019-04-10 19:27:56 +03:00
|
|
|
dmt->clk = devm_clk_get(dev, NULL);
|
2016-02-26 20:20:22 +03:00
|
|
|
if (IS_ERR(dmt->clk)) {
|
2019-04-10 19:27:56 +03:00
|
|
|
dev_err(dev, "clk not found\n");
|
2016-02-26 20:20:22 +03:00
|
|
|
return PTR_ERR(dmt->clk);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = clk_prepare_enable(dmt->clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2019-04-10 19:27:56 +03:00
|
|
|
ret = devm_add_action_or_reset(dev, pic32_clk_disable_unprepare,
|
|
|
|
dmt->clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-02-26 20:20:22 +03:00
|
|
|
|
|
|
|
wdd->timeout = pic32_dmt_get_timeout_secs(dmt);
|
|
|
|
if (!wdd->timeout) {
|
2019-04-10 19:27:56 +03:00
|
|
|
dev_err(dev, "failed to read watchdog register timeout\n");
|
|
|
|
return -EINVAL;
|
2016-02-26 20:20:22 +03:00
|
|
|
}
|
|
|
|
|
2019-04-10 19:27:56 +03:00
|
|
|
dev_info(dev, "timeout %d\n", wdd->timeout);
|
2016-02-26 20:20:22 +03:00
|
|
|
|
|
|
|
wdd->bootstatus = pic32_dmt_bootstatus(dmt) ? WDIOF_CARDRESET : 0;
|
|
|
|
|
|
|
|
watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
|
|
|
|
watchdog_set_drvdata(wdd, dmt);
|
|
|
|
|
2019-04-10 19:27:56 +03:00
|
|
|
ret = devm_watchdog_register_device(dev, wdd);
|
2019-05-19 00:27:45 +03:00
|
|
|
if (ret)
|
2019-04-10 19:27:56 +03:00
|
|
|
return ret;
|
2016-02-26 20:20:22 +03:00
|
|
|
|
|
|
|
platform_set_drvdata(pdev, wdd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id pic32_dmt_of_ids[] = {
|
|
|
|
{ .compatible = "microchip,pic32mzda-dmt",},
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(of, pic32_dmt_of_ids);
|
|
|
|
|
|
|
|
static struct platform_driver pic32_dmt_driver = {
|
|
|
|
.probe = pic32_dmt_probe,
|
|
|
|
.driver = {
|
|
|
|
.name = "pic32-dmt",
|
|
|
|
.of_match_table = of_match_ptr(pic32_dmt_of_ids),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module_platform_driver(pic32_dmt_driver);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Purna Chandra Mandal <purna.mandal@microchip.com>");
|
|
|
|
MODULE_DESCRIPTION("Microchip PIC32 DMT Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|