2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2009-03-27 22:26:42 +00:00
|
|
|
/*
|
|
|
|
* [origin: Linux kernel drivers/watchdog/at91sam9_wdt.c]
|
|
|
|
*
|
2018-10-09 18:46:40 +00:00
|
|
|
* Watchdog driver for AT91SAM9x processors.
|
2009-03-27 22:26:42 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
|
|
|
|
* Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Watchdog Timer Mode Register can be only written to once. If the
|
|
|
|
* timeout need to be set from U-Boot, be sure that the bootstrap doesn't
|
|
|
|
* write to this register. Inform Linux to it too
|
|
|
|
*/
|
|
|
|
|
2011-02-04 19:17:33 +00:00
|
|
|
#include <asm/io.h>
|
2009-03-27 22:26:42 +00:00
|
|
|
#include <asm/arch/at91_wdt.h>
|
2018-10-09 18:46:40 +00:00
|
|
|
#include <common.h>
|
2019-04-02 08:57:19 +00:00
|
|
|
#include <div64.h>
|
2018-10-09 18:46:40 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <wdt.h>
|
|
|
|
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2009-03-27 22:26:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
|
|
|
|
* use this to convert a watchdog
|
2018-10-09 18:46:40 +00:00
|
|
|
* value from seconds.
|
2009-03-27 22:26:42 +00:00
|
|
|
*/
|
2018-10-09 18:46:40 +00:00
|
|
|
#define WDT_SEC2TICKS(s) (((s) << 8) - 1)
|
2009-03-27 22:26:42 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the watchdog time interval in 1/256Hz (write-once)
|
|
|
|
* Counter is 12 bit.
|
|
|
|
*/
|
2019-04-02 08:57:19 +00:00
|
|
|
static int at91_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
|
2009-03-27 22:26:42 +00:00
|
|
|
{
|
2018-10-09 18:46:40 +00:00
|
|
|
struct at91_wdt_priv *priv = dev_get_priv(dev);
|
2019-04-02 08:57:19 +00:00
|
|
|
u64 timeout;
|
|
|
|
u32 ticks;
|
2018-10-09 18:46:40 +00:00
|
|
|
|
2019-04-02 08:57:19 +00:00
|
|
|
/* Calculate timeout in seconds and the resulting ticks */
|
|
|
|
timeout = timeout_ms;
|
|
|
|
do_div(timeout, 1000);
|
|
|
|
timeout = min_t(u64, timeout, WDT_MAX_TIMEOUT);
|
|
|
|
ticks = WDT_SEC2TICKS(timeout);
|
2009-03-27 22:26:42 +00:00
|
|
|
|
|
|
|
/* Check if disabled */
|
2018-10-09 18:46:40 +00:00
|
|
|
if (readl(priv->regs + AT91_WDT_MR) & AT91_WDT_MR_WDDIS) {
|
2009-03-27 22:26:42 +00:00
|
|
|
printf("sorry, watchdog is disabled\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All counting occurs at SLOW_CLOCK / 128 = 256 Hz
|
|
|
|
*
|
|
|
|
* Since WDV is a 12-bit counter, the maximum period is
|
|
|
|
* 4096 / 256 = 16 seconds.
|
|
|
|
*/
|
2018-10-09 18:46:40 +00:00
|
|
|
priv->regval = AT91_WDT_MR_WDRSTEN /* causes watchdog reset */
|
2010-03-17 13:50:29 +00:00
|
|
|
| AT91_WDT_MR_WDDBGHLT /* disabled in debug mode */
|
|
|
|
| AT91_WDT_MR_WDD(0xfff) /* restart at any time */
|
2019-04-02 08:57:19 +00:00
|
|
|
| AT91_WDT_MR_WDV(ticks); /* timer value */
|
2018-10-09 18:46:40 +00:00
|
|
|
writel(priv->regval, priv->regs + AT91_WDT_MR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int at91_wdt_stop(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct at91_wdt_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
/* Disable Watchdog Timer */
|
|
|
|
priv->regval |= AT91_WDT_MR_WDDIS;
|
|
|
|
writel(priv->regval, priv->regs + AT91_WDT_MR);
|
2009-03-27 22:26:42 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-09 18:46:40 +00:00
|
|
|
static int at91_wdt_reset(struct udevice *dev)
|
2009-03-27 22:26:42 +00:00
|
|
|
{
|
2018-10-09 18:46:40 +00:00
|
|
|
struct at91_wdt_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
writel(AT91_WDT_CR_WDRSTT | AT91_WDT_CR_KEY, priv->regs + AT91_WDT_CR);
|
|
|
|
|
|
|
|
return 0;
|
2009-03-27 22:26:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 18:46:40 +00:00
|
|
|
static const struct wdt_ops at91_wdt_ops = {
|
|
|
|
.start = at91_wdt_start,
|
|
|
|
.stop = at91_wdt_stop,
|
|
|
|
.reset = at91_wdt_reset,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id at91_wdt_ids[] = {
|
|
|
|
{ .compatible = "atmel,at91sam9260-wdt" },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int at91_wdt_probe(struct udevice *dev)
|
2009-03-27 22:26:42 +00:00
|
|
|
{
|
2018-10-09 18:46:40 +00:00
|
|
|
struct at91_wdt_priv *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
priv->regs = dev_remap_addr(dev);
|
|
|
|
if (!priv->regs)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
debug("%s: Probing wdt%u\n", __func__, dev->seq);
|
|
|
|
|
|
|
|
return 0;
|
2009-03-27 22:26:42 +00:00
|
|
|
}
|
2018-10-09 18:46:40 +00:00
|
|
|
|
|
|
|
U_BOOT_DRIVER(at91_wdt) = {
|
|
|
|
.name = "at91_wdt",
|
|
|
|
.id = UCLASS_WDT,
|
|
|
|
.of_match = at91_wdt_ids,
|
|
|
|
.priv_auto_alloc_size = sizeof(struct at91_wdt_priv),
|
|
|
|
.ops = &at91_wdt_ops,
|
|
|
|
.probe = at91_wdt_probe,
|
|
|
|
};
|