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
|
|
|
|
*/
|
|
|
|
|
2021-04-27 09:02:19 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_WDT
|
|
|
|
|
2017-04-17 19:00:21 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2019-12-28 17:45:07 +00:00
|
|
|
#include <hang.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2021-11-04 03:55:14 +00:00
|
|
|
#include <sysreset.h>
|
2020-02-24 00:20:33 +00:00
|
|
|
#include <time.h>
|
2017-04-17 19:00:21 +00:00
|
|
|
#include <wdt.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2017-04-17 19:00:21 +00:00
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/lists.h>
|
|
|
|
|
2019-04-11 13:58:44 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2020-03-13 16:04:57 +00:00
|
|
|
#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
|
|
|
|
|
2021-08-19 09:56:56 +00:00
|
|
|
struct wdt_priv {
|
|
|
|
/* Timeout, in seconds, to configure this device to. */
|
|
|
|
u32 timeout;
|
|
|
|
/*
|
|
|
|
* Time, in milliseconds, between calling the device's ->reset()
|
|
|
|
* method from watchdog_reset().
|
|
|
|
*/
|
|
|
|
ulong reset_period;
|
|
|
|
/*
|
|
|
|
* Next time (as returned by get_timer(0)) to call
|
|
|
|
* ->reset().
|
|
|
|
*/
|
|
|
|
ulong next_reset;
|
2021-08-19 09:56:59 +00:00
|
|
|
/* Whether watchdog_start() has been called on the device. */
|
|
|
|
bool running;
|
2022-02-10 17:17:54 +00:00
|
|
|
/* No autostart */
|
|
|
|
bool noautostart;
|
2021-08-19 09:56:56 +00:00
|
|
|
};
|
2020-03-13 16:04:58 +00:00
|
|
|
|
2021-08-19 09:56:58 +00:00
|
|
|
static void init_watchdog_dev(struct udevice *dev)
|
2020-03-13 16:04:57 +00:00
|
|
|
{
|
2021-08-19 09:56:56 +00:00
|
|
|
struct wdt_priv *priv;
|
2021-03-09 13:26:55 +00:00
|
|
|
int ret;
|
2020-03-13 16:04:57 +00:00
|
|
|
|
2021-08-19 09:56:58 +00:00
|
|
|
priv = dev_get_uclass_priv(dev);
|
|
|
|
|
2021-11-04 03:55:14 +00:00
|
|
|
if (IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)) {
|
|
|
|
ret = sysreset_register_wdt(dev);
|
|
|
|
if (ret)
|
|
|
|
printf("WDT: Failed to register %s for sysreset\n",
|
|
|
|
dev->name);
|
|
|
|
}
|
|
|
|
|
2022-02-10 17:17:54 +00:00
|
|
|
if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART) || priv->noautostart) {
|
2021-08-19 09:56:58 +00:00
|
|
|
printf("WDT: Not starting %s\n", dev->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = wdt_start(dev, priv->timeout * 1000, 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf("WDT: Failed to start %s\n", dev->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("WDT: Started %s with%s servicing (%ds timeout)\n", dev->name,
|
|
|
|
IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int initr_watchdog(void)
|
|
|
|
{
|
watchdog: wdt-uclass.c: handle all DM watchdogs in watchdog_reset()
A board can have and make use of more than one watchdog device, say
one built into the SOC and an external gpio-petted one. Having
wdt-uclass only handle the first is both a little arbitrary and
unexpected.
So change initr_watchdog() so we visit (probe) all DM watchdog
devices, and call the init_watchdog_dev helper for each.
Similarly let watchdog_reset() loop over the whole uclass - each
having their own ratelimiting metadata, and a separate "is this device
running" flag.
This gets rid of the watchdog_dev member of struct global_data. We
do, however, still need the GD_FLG_WDT_READY set in
initr_watchdog(). This is because watchdog_reset() can get called
before DM is ready, and I don't think we can call uclass_get() that
early.
The current code just returns 0 if "getting" the first device fails -
that can of course happen because there are no devices, but it could
also happen if its ->probe call failed. In keeping with that, continue
with the handling of the remaining devices even if one fails to
probe. This is also why we cannot use uclass_probe_all().
If desired, it's possible to later add a per-device "u-boot,autostart"
boolean property, so that one can do CONFIG_WATCHDOG_AUTOSTART
per-device.
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stefan Roese <sr@denx.de>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-08-19 09:57:03 +00:00
|
|
|
struct udevice *dev;
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get(UCLASS_WDT, &uc);
|
|
|
|
if (ret) {
|
|
|
|
log_debug("Error getting UCLASS_WDT: %d\n", ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uclass_foreach_dev(dev, uc) {
|
|
|
|
ret = device_probe(dev);
|
|
|
|
if (ret) {
|
|
|
|
log_debug("Error probing %s: %d\n", dev->name, ret);
|
|
|
|
continue;
|
2020-03-13 16:04:57 +00:00
|
|
|
}
|
watchdog: wdt-uclass.c: handle all DM watchdogs in watchdog_reset()
A board can have and make use of more than one watchdog device, say
one built into the SOC and an external gpio-petted one. Having
wdt-uclass only handle the first is both a little arbitrary and
unexpected.
So change initr_watchdog() so we visit (probe) all DM watchdog
devices, and call the init_watchdog_dev helper for each.
Similarly let watchdog_reset() loop over the whole uclass - each
having their own ratelimiting metadata, and a separate "is this device
running" flag.
This gets rid of the watchdog_dev member of struct global_data. We
do, however, still need the GD_FLG_WDT_READY set in
initr_watchdog(). This is because watchdog_reset() can get called
before DM is ready, and I don't think we can call uclass_get() that
early.
The current code just returns 0 if "getting" the first device fails -
that can of course happen because there are no devices, but it could
also happen if its ->probe call failed. In keeping with that, continue
with the handling of the remaining devices even if one fails to
probe. This is also why we cannot use uclass_probe_all().
If desired, it's possible to later add a per-device "u-boot,autostart"
boolean property, so that one can do CONFIG_WATCHDOG_AUTOSTART
per-device.
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stefan Roese <sr@denx.de>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-08-19 09:57:03 +00:00
|
|
|
init_watchdog_dev(dev);
|
2020-03-13 16:04:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 09:56:59 +00:00
|
|
|
gd->flags |= GD_FLG_WDT_READY;
|
2020-03-13 16:04:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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);
|
2021-03-09 13:26:54 +00:00
|
|
|
int ret;
|
2017-04-17 19:00:21 +00:00
|
|
|
|
|
|
|
if (!ops->start)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
2021-03-09 13:26:54 +00:00
|
|
|
ret = ops->start(dev, timeout_ms, flags);
|
2021-08-19 09:56:59 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
struct wdt_priv *priv = dev_get_uclass_priv(dev);
|
|
|
|
|
|
|
|
priv->running = true;
|
|
|
|
}
|
2021-03-09 13:26:54 +00:00
|
|
|
|
|
|
|
return ret;
|
2017-04-17 19:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int wdt_stop(struct udevice *dev)
|
|
|
|
{
|
|
|
|
const struct wdt_ops *ops = device_get_ops(dev);
|
2021-03-09 13:26:54 +00:00
|
|
|
int ret;
|
2017-04-17 19:00:21 +00:00
|
|
|
|
|
|
|
if (!ops->stop)
|
|
|
|
return -ENOSYS;
|
|
|
|
|
2021-03-09 13:26:54 +00:00
|
|
|
ret = ops->stop(dev);
|
2021-08-19 09:56:59 +00:00
|
|
|
if (ret == 0) {
|
|
|
|
struct wdt_priv *priv = dev_get_uclass_priv(dev);
|
|
|
|
|
|
|
|
priv->running = false;
|
|
|
|
}
|
2021-03-09 13:26:54 +00:00
|
|
|
|
|
|
|
return ret;
|
2017-04-17 19:00:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 09:57:01 +00:00
|
|
|
int wdt_stop_all(void)
|
|
|
|
{
|
|
|
|
struct wdt_priv *priv;
|
|
|
|
struct udevice *dev;
|
|
|
|
struct uclass *uc;
|
|
|
|
int ret, err;
|
|
|
|
|
|
|
|
ret = uclass_get(UCLASS_WDT, &uc);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
uclass_foreach_dev(dev, uc) {
|
|
|
|
if (!device_active(dev))
|
|
|
|
continue;
|
|
|
|
priv = dev_get_uclass_priv(dev);
|
|
|
|
if (!priv->running)
|
|
|
|
continue;
|
|
|
|
err = wdt_stop(dev);
|
|
|
|
if (!ret)
|
|
|
|
ret = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:00:21 +00:00
|
|
|
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 {
|
2021-08-19 09:56:55 +00:00
|
|
|
ret = wdt_start(dev, 1, flags);
|
2017-04-17 19:00:21 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-08-19 09:56:56 +00:00
|
|
|
struct wdt_priv *priv;
|
|
|
|
struct udevice *dev;
|
watchdog: wdt-uclass.c: handle all DM watchdogs in watchdog_reset()
A board can have and make use of more than one watchdog device, say
one built into the SOC and an external gpio-petted one. Having
wdt-uclass only handle the first is both a little arbitrary and
unexpected.
So change initr_watchdog() so we visit (probe) all DM watchdog
devices, and call the init_watchdog_dev helper for each.
Similarly let watchdog_reset() loop over the whole uclass - each
having their own ratelimiting metadata, and a separate "is this device
running" flag.
This gets rid of the watchdog_dev member of struct global_data. We
do, however, still need the GD_FLG_WDT_READY set in
initr_watchdog(). This is because watchdog_reset() can get called
before DM is ready, and I don't think we can call uclass_get() that
early.
The current code just returns 0 if "getting" the first device fails -
that can of course happen because there are no devices, but it could
also happen if its ->probe call failed. In keeping with that, continue
with the handling of the remaining devices even if one fails to
probe. This is also why we cannot use uclass_probe_all().
If desired, it's possible to later add a per-device "u-boot,autostart"
boolean property, so that one can do CONFIG_WATCHDOG_AUTOSTART
per-device.
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stefan Roese <sr@denx.de>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-08-19 09:57:03 +00:00
|
|
|
struct uclass *uc;
|
2019-04-11 13:58:44 +00:00
|
|
|
ulong now;
|
|
|
|
|
|
|
|
/* Exit if GD is not ready or watchdog is not initialized yet */
|
|
|
|
if (!gd || !(gd->flags & GD_FLG_WDT_READY))
|
|
|
|
return;
|
|
|
|
|
watchdog: wdt-uclass.c: handle all DM watchdogs in watchdog_reset()
A board can have and make use of more than one watchdog device, say
one built into the SOC and an external gpio-petted one. Having
wdt-uclass only handle the first is both a little arbitrary and
unexpected.
So change initr_watchdog() so we visit (probe) all DM watchdog
devices, and call the init_watchdog_dev helper for each.
Similarly let watchdog_reset() loop over the whole uclass - each
having their own ratelimiting metadata, and a separate "is this device
running" flag.
This gets rid of the watchdog_dev member of struct global_data. We
do, however, still need the GD_FLG_WDT_READY set in
initr_watchdog(). This is because watchdog_reset() can get called
before DM is ready, and I don't think we can call uclass_get() that
early.
The current code just returns 0 if "getting" the first device fails -
that can of course happen because there are no devices, but it could
also happen if its ->probe call failed. In keeping with that, continue
with the handling of the remaining devices even if one fails to
probe. This is also why we cannot use uclass_probe_all().
If desired, it's possible to later add a per-device "u-boot,autostart"
boolean property, so that one can do CONFIG_WATCHDOG_AUTOSTART
per-device.
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stefan Roese <sr@denx.de>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-08-19 09:57:03 +00:00
|
|
|
if (uclass_get(UCLASS_WDT, &uc))
|
2021-08-19 09:56:59 +00:00
|
|
|
return;
|
|
|
|
|
watchdog: wdt-uclass.c: handle all DM watchdogs in watchdog_reset()
A board can have and make use of more than one watchdog device, say
one built into the SOC and an external gpio-petted one. Having
wdt-uclass only handle the first is both a little arbitrary and
unexpected.
So change initr_watchdog() so we visit (probe) all DM watchdog
devices, and call the init_watchdog_dev helper for each.
Similarly let watchdog_reset() loop over the whole uclass - each
having their own ratelimiting metadata, and a separate "is this device
running" flag.
This gets rid of the watchdog_dev member of struct global_data. We
do, however, still need the GD_FLG_WDT_READY set in
initr_watchdog(). This is because watchdog_reset() can get called
before DM is ready, and I don't think we can call uclass_get() that
early.
The current code just returns 0 if "getting" the first device fails -
that can of course happen because there are no devices, but it could
also happen if its ->probe call failed. In keeping with that, continue
with the handling of the remaining devices even if one fails to
probe. This is also why we cannot use uclass_probe_all().
If desired, it's possible to later add a per-device "u-boot,autostart"
boolean property, so that one can do CONFIG_WATCHDOG_AUTOSTART
per-device.
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Stefan Roese <sr@denx.de>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-08-19 09:57:03 +00:00
|
|
|
/*
|
|
|
|
* All devices bound to the wdt uclass should have been probed
|
|
|
|
* in initr_watchdog(). But just in case something went wrong,
|
|
|
|
* check device_active() before accessing the uclass private
|
|
|
|
* data.
|
|
|
|
*/
|
|
|
|
uclass_foreach_dev(dev, uc) {
|
|
|
|
if (!device_active(dev))
|
|
|
|
continue;
|
|
|
|
priv = dev_get_uclass_priv(dev);
|
|
|
|
if (!priv->running)
|
|
|
|
continue;
|
|
|
|
/* Do not reset the watchdog too often */
|
|
|
|
now = get_timer(0);
|
|
|
|
if (time_after_eq(now, priv->next_reset)) {
|
|
|
|
priv->next_reset = now + priv->reset_period;
|
|
|
|
wdt_reset(dev);
|
|
|
|
}
|
2019-04-11 13:58:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2021-08-19 09:56:56 +00:00
|
|
|
static int wdt_pre_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
u32 timeout = WATCHDOG_TIMEOUT_SECS;
|
|
|
|
/*
|
|
|
|
* Reset every 1000ms, or however often is required as
|
|
|
|
* indicated by a hw_margin_ms property.
|
|
|
|
*/
|
|
|
|
ulong reset_period = 1000;
|
2022-02-10 17:17:54 +00:00
|
|
|
bool noautostart = false;
|
2021-08-19 09:56:56 +00:00
|
|
|
struct wdt_priv *priv;
|
|
|
|
|
|
|
|
if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
|
|
|
|
timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
|
|
|
|
reset_period = dev_read_u32_default(dev, "hw_margin_ms",
|
|
|
|
4 * reset_period) / 4;
|
2022-02-10 17:17:54 +00:00
|
|
|
noautostart = dev_read_bool(dev, "u-boot,noautostart");
|
2021-08-19 09:56:56 +00:00
|
|
|
}
|
|
|
|
priv = dev_get_uclass_priv(dev);
|
|
|
|
priv->timeout = timeout;
|
|
|
|
priv->reset_period = reset_period;
|
2022-02-10 17:17:54 +00:00
|
|
|
priv->noautostart = noautostart;
|
2021-08-19 09:56:56 +00:00
|
|
|
/*
|
|
|
|
* Pretend this device was last reset "long" ago so the first
|
|
|
|
* watchdog_reset will actually call its ->reset method.
|
|
|
|
*/
|
|
|
|
priv->next_reset = get_timer(0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:00:21 +00:00
|
|
|
UCLASS_DRIVER(wdt) = {
|
2021-08-19 09:56:57 +00:00
|
|
|
.id = UCLASS_WDT,
|
|
|
|
.name = "watchdog",
|
|
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
|
|
|
.post_bind = wdt_post_bind,
|
2021-08-19 09:56:56 +00:00
|
|
|
.pre_probe = wdt_pre_probe,
|
|
|
|
.per_device_auto = sizeof(struct wdt_priv),
|
2017-04-17 19:00:21 +00:00
|
|
|
};
|