
There are of course not a whole lot of examples in-tree yet, but before they appear, let's make this API change: Instead of separately allocating a 'struct cyclic_info', make the users embed such an instance in their own structure, and make the convention that the callback simply receives the 'struct cyclic_info *', from which the clients can get their own data using the container_of() macro. This has a number of advantages. First, it means cyclic_register() simply cannot fail, simplifying the code. The necessary storage will simply be allocated automatically when the client's own structure is allocated (often via uclass_priv_auto or similar). Second, code for which CONFIG_CYCLIC is just an option can more easily be written without #ifdefs, if we just provide an empty struct cyclic_info {}. For example, the nested CONFIG_IS_ENABLED()s in https://lore.kernel.org/u-boot/20240316201416.211480-1-marek.vasut+renesas@mailbox.org/ are mostly due to the existence of the 'struct cyclic_info *' member being guarded by #ifdef CONFIG_CYCLIC. And we do probably want to avoid the extra memory overhead of that member when !CONFIG_CYCLIC. But that is automatic if, instead of a 'struct cyclic_info *', one simply embeds a 'struct cyclic_info', which will have size 0 when !CONFIG_CYCLIC. Also, the no-op cyclic_register() function can just unconditionally be called, and the compiler will see that (1) the callback is referenced, so not emit a warning for a maybe-unused function and (2) see that it can actually never be reached, so not emit any code for it. Reviewed-by: Stefan Roese <sr@denx.de> Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
281 lines
5.6 KiB
C
281 lines
5.6 KiB
C
// SPDX-License-Identifier: GPL-2.0+
|
|
/*
|
|
* Copyright 2017 Google, Inc
|
|
*/
|
|
|
|
#define LOG_CATEGORY UCLASS_WDT
|
|
|
|
#include <cyclic.h>
|
|
#include <div64.h>
|
|
#include <dm.h>
|
|
#include <errno.h>
|
|
#include <hang.h>
|
|
#include <log.h>
|
|
#include <sysreset.h>
|
|
#include <time.h>
|
|
#include <wdt.h>
|
|
#include <asm/global_data.h>
|
|
#include <dm/device-internal.h>
|
|
#include <dm/lists.h>
|
|
#include <linux/kernel.h>
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
|
|
|
|
struct wdt_priv {
|
|
/* The udevice owning this wdt_priv. */
|
|
struct udevice *dev;
|
|
/* 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;
|
|
/* Whether watchdog_start() has been called on the device. */
|
|
bool running;
|
|
/* autostart */
|
|
bool autostart;
|
|
|
|
struct cyclic_info cyclic;
|
|
};
|
|
|
|
static void wdt_cyclic(struct cyclic_info *c)
|
|
{
|
|
struct wdt_priv *priv = container_of(c, struct wdt_priv, cyclic);
|
|
struct udevice *dev = priv->dev;
|
|
|
|
if (!device_active(dev))
|
|
return;
|
|
|
|
if (!priv->running)
|
|
return;
|
|
|
|
wdt_reset(dev);
|
|
}
|
|
|
|
static void init_watchdog_dev(struct udevice *dev)
|
|
{
|
|
struct wdt_priv *priv;
|
|
int ret;
|
|
|
|
priv = dev_get_uclass_priv(dev);
|
|
|
|
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);
|
|
}
|
|
|
|
if (!priv->autostart) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
int initr_watchdog(void)
|
|
{
|
|
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;
|
|
}
|
|
init_watchdog_dev(dev);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
|
|
{
|
|
const struct wdt_ops *ops = device_get_ops(dev);
|
|
int ret;
|
|
|
|
if (!ops->start)
|
|
return -ENOSYS;
|
|
|
|
ret = ops->start(dev, timeout_ms, flags);
|
|
if (ret == 0) {
|
|
struct wdt_priv *priv = dev_get_uclass_priv(dev);
|
|
char str[16];
|
|
|
|
memset(str, 0, 16);
|
|
if (IS_ENABLED(CONFIG_WATCHDOG)) {
|
|
if (priv->running)
|
|
cyclic_unregister(&priv->cyclic);
|
|
|
|
/* Register the watchdog driver as a cyclic function */
|
|
cyclic_register(&priv->cyclic, wdt_cyclic,
|
|
priv->reset_period * 1000,
|
|
dev->name);
|
|
|
|
snprintf(str, 16, "every %ldms", priv->reset_period);
|
|
}
|
|
|
|
priv->running = true;
|
|
printf("WDT: Started %s with%s servicing %s (%ds timeout)\n",
|
|
dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out",
|
|
str, (u32)lldiv(timeout_ms, 1000));
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
int wdt_stop(struct udevice *dev)
|
|
{
|
|
const struct wdt_ops *ops = device_get_ops(dev);
|
|
int ret;
|
|
|
|
if (!ops->stop)
|
|
return -ENOSYS;
|
|
|
|
ret = ops->stop(dev);
|
|
if (ret == 0) {
|
|
struct wdt_priv *priv = dev_get_uclass_priv(dev);
|
|
|
|
if (IS_ENABLED(CONFIG_WATCHDOG) && priv->running)
|
|
cyclic_unregister(&priv->cyclic);
|
|
|
|
priv->running = false;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
|
|
debug("WDT Resetting: %lu\n", flags);
|
|
ops = device_get_ops(dev);
|
|
if (ops->expire_now) {
|
|
return ops->expire_now(dev, flags);
|
|
} else {
|
|
ret = wdt_start(dev, 1, flags);
|
|
|
|
if (ret < 0)
|
|
return ret;
|
|
|
|
hang();
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
#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)
|
|
{
|
|
/*
|
|
* Empty function for now. The actual WDT handling is now done in
|
|
* the cyclic function instead.
|
|
*/
|
|
}
|
|
#endif
|
|
|
|
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;
|
|
bool autostart = IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART);
|
|
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;
|
|
if (dev_read_bool(dev, "u-boot,noautostart"))
|
|
autostart = false;
|
|
else if (dev_read_bool(dev, "u-boot,autostart"))
|
|
autostart = true;
|
|
}
|
|
priv = dev_get_uclass_priv(dev);
|
|
priv->dev = dev;
|
|
priv->timeout = timeout;
|
|
priv->reset_period = reset_period;
|
|
priv->autostart = autostart;
|
|
/*
|
|
* 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;
|
|
}
|
|
|
|
UCLASS_DRIVER(wdt) = {
|
|
.id = UCLASS_WDT,
|
|
.name = "watchdog",
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
|
.pre_probe = wdt_pre_probe,
|
|
.per_device_auto = sizeof(struct wdt_priv),
|
|
};
|