2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-06-10 06:10:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Altera Corporation <www.altera.com>
|
|
|
|
*/
|
|
|
|
|
2019-10-03 12:47:07 +00:00
|
|
|
#include <clk.h>
|
2014-06-10 06:10:21 +00:00
|
|
|
#include <common.h>
|
2019-06-26 23:19:23 +00:00
|
|
|
#include <dm.h>
|
2019-10-03 12:47:07 +00:00
|
|
|
#include <reset.h>
|
2019-06-26 23:19:23 +00:00
|
|
|
#include <wdt.h>
|
2014-06-10 06:10:21 +00:00
|
|
|
#include <asm/io.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2014-06-10 06:10:21 +00:00
|
|
|
|
|
|
|
#define DW_WDT_CR 0x00
|
|
|
|
#define DW_WDT_TORR 0x04
|
|
|
|
#define DW_WDT_CRR 0x0C
|
|
|
|
|
|
|
|
#define DW_WDT_CR_EN_OFFSET 0x00
|
|
|
|
#define DW_WDT_CR_RMOD_OFFSET 0x01
|
|
|
|
#define DW_WDT_CRR_RESTART_VAL 0x76
|
|
|
|
|
2019-06-26 23:19:23 +00:00
|
|
|
struct designware_wdt_priv {
|
|
|
|
void __iomem *base;
|
2019-10-03 12:47:07 +00:00
|
|
|
unsigned int clk_khz;
|
2019-06-26 23:19:23 +00:00
|
|
|
};
|
|
|
|
|
2014-06-10 06:10:21 +00:00
|
|
|
/*
|
|
|
|
* Set the watchdog time interval.
|
|
|
|
* Counter is 32 bit.
|
|
|
|
*/
|
2019-06-26 23:19:23 +00:00
|
|
|
static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz,
|
|
|
|
unsigned int timeout)
|
2014-06-10 06:10:21 +00:00
|
|
|
{
|
|
|
|
signed int i;
|
|
|
|
|
|
|
|
/* calculate the timeout range value */
|
2021-03-11 02:02:17 +00:00
|
|
|
i = fls(timeout * clk_khz - 1) - 16;
|
2019-06-26 23:19:23 +00:00
|
|
|
i = clamp(i, 0, 15);
|
|
|
|
|
|
|
|
writel(i | (i << 4), base + DW_WDT_TORR);
|
2014-06-10 06:10:21 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:19:23 +00:00
|
|
|
static void designware_wdt_enable(void __iomem *base)
|
2014-06-10 06:10:21 +00:00
|
|
|
{
|
2019-10-03 12:47:07 +00:00
|
|
|
writel(BIT(DW_WDT_CR_EN_OFFSET), base + DW_WDT_CR);
|
2014-06-10 06:10:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:19:23 +00:00
|
|
|
static unsigned int designware_wdt_is_enabled(void __iomem *base)
|
2014-06-10 06:10:21 +00:00
|
|
|
{
|
2019-06-26 23:19:23 +00:00
|
|
|
return readl(base + DW_WDT_CR) & BIT(0);
|
2014-06-10 06:10:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 23:19:23 +00:00
|
|
|
static void designware_wdt_reset_common(void __iomem *base)
|
2014-06-10 06:10:21 +00:00
|
|
|
{
|
2019-06-26 23:19:23 +00:00
|
|
|
if (designware_wdt_is_enabled(base))
|
2014-06-10 06:10:21 +00:00
|
|
|
/* restart the watchdog counter */
|
2019-06-26 23:19:23 +00:00
|
|
|
writel(DW_WDT_CRR_RESTART_VAL, base + DW_WDT_CRR);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !CONFIG_IS_ENABLED(WDT)
|
|
|
|
void hw_watchdog_reset(void)
|
|
|
|
{
|
|
|
|
designware_wdt_reset_common((void __iomem *)CONFIG_DW_WDT_BASE);
|
2014-06-10 06:10:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void hw_watchdog_init(void)
|
|
|
|
{
|
|
|
|
/* reset to disable the watchdog */
|
|
|
|
hw_watchdog_reset();
|
|
|
|
/* set timer in miliseconds */
|
2019-06-26 23:19:23 +00:00
|
|
|
designware_wdt_settimeout((void __iomem *)CONFIG_DW_WDT_BASE,
|
|
|
|
CONFIG_DW_WDT_CLOCK_KHZ,
|
|
|
|
CONFIG_WATCHDOG_TIMEOUT_MSECS);
|
2014-06-10 06:10:21 +00:00
|
|
|
/* enable the watchdog */
|
2019-06-26 23:19:23 +00:00
|
|
|
designware_wdt_enable((void __iomem *)CONFIG_DW_WDT_BASE);
|
2014-06-10 06:10:21 +00:00
|
|
|
/* reset the watchdog */
|
|
|
|
hw_watchdog_reset();
|
|
|
|
}
|
2019-06-26 23:19:23 +00:00
|
|
|
#else
|
|
|
|
static int designware_wdt_reset(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct designware_wdt_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
designware_wdt_reset_common(priv->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int designware_wdt_stop(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct designware_wdt_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
designware_wdt_reset(dev);
|
2019-10-03 12:47:07 +00:00
|
|
|
writel(0, priv->base + DW_WDT_CR);
|
2019-06-26 23:19:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
|
|
|
|
{
|
|
|
|
struct designware_wdt_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
designware_wdt_stop(dev);
|
|
|
|
|
|
|
|
/* set timer in miliseconds */
|
2019-10-03 12:47:07 +00:00
|
|
|
designware_wdt_settimeout(priv->base, priv->clk_khz, timeout);
|
2019-06-26 23:19:23 +00:00
|
|
|
|
|
|
|
designware_wdt_enable(priv->base);
|
|
|
|
|
|
|
|
/* reset the watchdog */
|
|
|
|
return designware_wdt_reset(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int designware_wdt_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct designware_wdt_priv *priv = dev_get_priv(dev);
|
2019-10-03 12:47:07 +00:00
|
|
|
__maybe_unused int ret;
|
2019-06-26 23:19:23 +00:00
|
|
|
|
|
|
|
priv->base = dev_remap_addr(dev);
|
|
|
|
if (!priv->base)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2019-10-03 12:47:07 +00:00
|
|
|
#if CONFIG_IS_ENABLED(CLK)
|
|
|
|
struct clk clk;
|
|
|
|
|
|
|
|
ret = clk_get_by_index(dev, 0, &clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2021-03-11 02:02:19 +00:00
|
|
|
ret = clk_enable(&clk);
|
|
|
|
if (ret)
|
2021-03-11 02:02:20 +00:00
|
|
|
goto err;
|
2021-03-11 02:02:19 +00:00
|
|
|
|
2020-09-17 09:30:40 +00:00
|
|
|
priv->clk_khz = clk_get_rate(&clk) / 1000;
|
2021-03-11 02:02:20 +00:00
|
|
|
if (!priv->clk_khz) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
2019-10-03 12:47:07 +00:00
|
|
|
#else
|
|
|
|
priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ;
|
|
|
|
#endif
|
|
|
|
|
2021-03-11 02:02:18 +00:00
|
|
|
if (CONFIG_IS_ENABLED(DM_RESET)) {
|
|
|
|
struct reset_ctl_bulk resets;
|
2019-10-03 12:47:07 +00:00
|
|
|
|
2021-03-11 02:02:18 +00:00
|
|
|
ret = reset_get_bulk(dev, &resets);
|
|
|
|
if (ret)
|
2021-03-11 02:02:20 +00:00
|
|
|
goto err;
|
2019-10-03 12:47:07 +00:00
|
|
|
|
2021-03-11 02:02:18 +00:00
|
|
|
ret = reset_deassert_bulk(&resets);
|
|
|
|
if (ret)
|
2021-03-11 02:02:20 +00:00
|
|
|
goto err;
|
2021-03-11 02:02:18 +00:00
|
|
|
}
|
2019-10-03 12:47:07 +00:00
|
|
|
|
2019-06-26 23:19:23 +00:00
|
|
|
/* reset to disable the watchdog */
|
|
|
|
return designware_wdt_stop(dev);
|
2021-03-11 02:02:20 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
#if CONFIG_IS_ENABLED(CLK)
|
|
|
|
clk_free(&clk);
|
|
|
|
#endif
|
|
|
|
return ret;
|
2019-06-26 23:19:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wdt_ops designware_wdt_ops = {
|
|
|
|
.start = designware_wdt_start,
|
|
|
|
.reset = designware_wdt_reset,
|
|
|
|
.stop = designware_wdt_stop,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id designware_wdt_ids[] = {
|
|
|
|
{ .compatible = "snps,dw-wdt"},
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(designware_wdt) = {
|
|
|
|
.name = "designware_wdt",
|
|
|
|
.id = UCLASS_WDT,
|
|
|
|
.of_match = designware_wdt_ids,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct designware_wdt_priv),
|
2019-06-26 23:19:23 +00:00
|
|
|
.probe = designware_wdt_probe,
|
|
|
|
.ops = &designware_wdt_ops,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|
2014-06-10 06:10:21 +00:00
|
|
|
#endif
|