2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2017-04-17 19:00:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google, Inc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2019-12-28 17:45:07 +00:00
|
|
|
#include <hang.h>
|
2017-04-17 19:00:21 +00:00
|
|
|
#include <wdt.h>
|
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
|
2019-04-11 13:58:44 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2017-08-04 21:48:28 +00:00
|
|
|
int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
|
2017-04-17 19:00:21 +00:00
|
|
|
{
|
|
|
|
const struct wdt_ops *ops = device_get_ops(dev);
|
|
|
|
|
|
|
|
if (!ops->start)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
2017-08-04 21:48:28 +00:00
|
|
|
return ops->start(dev, timeout_ms, flags);
|
2017-04-17 19:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int wdt_stop(struct udevice *dev)
|
|
|
|
{
|
|
|
|
const struct wdt_ops *ops = device_get_ops(dev);
|
|
|
|
|
|
|
|
if (!ops->stop)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return ops->stop(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int wdt_reset(struct udevice *dev)
|
|
|
|
{
|
|
|
|
const struct wdt_ops *ops = device_get_ops(dev);
|
|
|
|
|
|
|
|
if (!ops->reset)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
return ops->reset(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int wdt_expire_now(struct udevice *dev, ulong flags)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
const struct wdt_ops *ops;
|
|
|
|
|
2017-07-05 17:44:06 +00:00
|
|
|
debug("WDT Resetting: %lu\n", flags);
|
2017-04-17 19:00:21 +00:00
|
|
|
ops = device_get_ops(dev);
|
|
|
|
if (ops->expire_now) {
|
|
|
|
return ops->expire_now(dev, flags);
|
|
|
|
} else {
|
|
|
|
if (!ops->start)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
|
|
|
ret = ops->start(dev, 1, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
hang();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:58:44 +00:00
|
|
|
#if defined(CONFIG_WATCHDOG)
|
|
|
|
/*
|
|
|
|
* Called by macro WATCHDOG_RESET. This function be called *very* early,
|
|
|
|
* so we need to make sure, that the watchdog driver is ready before using
|
|
|
|
* it in this function.
|
|
|
|
*/
|
|
|
|
void watchdog_reset(void)
|
|
|
|
{
|
|
|
|
static ulong next_reset;
|
|
|
|
ulong now;
|
|
|
|
|
|
|
|
/* Exit if GD is not ready or watchdog is not initialized yet */
|
|
|
|
if (!gd || !(gd->flags & GD_FLG_WDT_READY))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Do not reset the watchdog too often */
|
|
|
|
now = get_timer(0);
|
|
|
|
if (now > next_reset) {
|
|
|
|
next_reset = now + 1000; /* reset every 1000ms */
|
|
|
|
wdt_reset(gd->watchdog_dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-11 13:42:25 +00:00
|
|
|
static int wdt_post_bind(struct udevice *dev)
|
|
|
|
{
|
|
|
|
#if defined(CONFIG_NEEDS_MANUAL_RELOC)
|
|
|
|
struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
|
|
|
|
static int reloc_done;
|
|
|
|
|
|
|
|
if (!reloc_done) {
|
|
|
|
if (ops->start)
|
|
|
|
ops->start += gd->reloc_off;
|
|
|
|
if (ops->stop)
|
|
|
|
ops->stop += gd->reloc_off;
|
|
|
|
if (ops->reset)
|
|
|
|
ops->reset += gd->reloc_off;
|
|
|
|
if (ops->expire_now)
|
|
|
|
ops->expire_now += gd->reloc_off;
|
|
|
|
|
|
|
|
reloc_done++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:00:21 +00:00
|
|
|
UCLASS_DRIVER(wdt) = {
|
|
|
|
.id = UCLASS_WDT,
|
2018-07-11 06:24:43 +00:00
|
|
|
.name = "watchdog",
|
|
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
2018-07-11 13:42:25 +00:00
|
|
|
.post_bind = wdt_post_bind,
|
2017-04-17 19:00:21 +00:00
|
|
|
};
|