mirror of
https://github.com/u-boot/u-boot.git
synced 2024-11-27 14:14:37 +08:00
06985289d4
This patch tries to implement a generic watchdog_reset() function that can be used by all boards that want to service the watchdog device in U-Boot. This watchdog servicing is enabled via CONFIG_WATCHDOG. Without this approach, new boards or platforms needed to implement a board specific version of this functionality, mostly copy'ing the same code over and over again into their board or platforms code base. With this new generic function, the scattered other functions are now removed to be replaced by the generic one. The new version also enables the configuration of the watchdog timeout via the DT "timeout-sec" property (if enabled via CONFIG_OF_CONTROL). This patch also adds a new flag to the GD flags, to flag that the watchdog is ready to use and adds the pointer to the watchdog device to the GD. This enables us to remove the global "watchdog_dev" variable, which was prone to cause problems because of its potentially very early use in watchdog_reset(), even before the BSS is cleared. Signed-off-by: Stefan Roese <sr@denx.de> Cc: Heiko Schocher <hs@denx.de> Cc: Tom Rini <trini@konsulko.com> Cc: Michal Simek <michal.simek@xilinx.com> Cc: "Marek Behún" <marek.behun@nic.cz> Cc: Daniel Schwierzeck <daniel.schwierzeck@gmail.com> Cc: Maxim Sloyko <maxims@google.com> Cc: Erik van Luijk <evanluijk@interact.nl> Cc: Ryder Lee <ryder.lee@mediatek.com> Cc: Weijie Gao <weijie.gao@mediatek.com> Cc: Simon Glass <sjg@chromium.org> Cc: "Álvaro Fernández Rojas" <noltari@gmail.com> Cc: Philippe Reynes <philippe.reynes@softathome.com> Cc: Christophe Leroy <christophe.leroy@c-s.fr> Reviewed-by: Michal Simek <michal.simek@xilinx.com> Tested-by: Michal Simek <michal.simek@xilinx.com> (on zcu100)
148 lines
4.0 KiB
C
148 lines
4.0 KiB
C
/* SPDX-License-Identifier: GPL-2.0+ */
|
|
/*
|
|
* Copyright 2017 Google, Inc
|
|
*/
|
|
|
|
#ifndef _WDT_H_
|
|
#define _WDT_H_
|
|
|
|
#include <dm.h>
|
|
#include <dm/read.h>
|
|
|
|
/*
|
|
* Implement a simple watchdog uclass. Watchdog is basically a timer that
|
|
* is used to detect or recover from malfunction. During normal operation
|
|
* the watchdog would be regularly reset to prevent it from timing out.
|
|
* If, due to a hardware fault or program error, the computer fails to reset
|
|
* the watchdog, the timer will elapse and generate a timeout signal.
|
|
* The timeout signal is used to initiate corrective action or actions,
|
|
* which typically include placing the system in a safe, known state.
|
|
*/
|
|
|
|
/*
|
|
* Start the timer
|
|
*
|
|
* @dev: WDT Device
|
|
* @timeout_ms: Number of ticks (milliseconds) before timer expires
|
|
* @flags: Driver specific flags. This might be used to specify
|
|
* which action needs to be executed when the timer expires
|
|
* @return: 0 if OK, -ve on error
|
|
*/
|
|
int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags);
|
|
|
|
/*
|
|
* Stop the timer, thus disabling the Watchdog. Use wdt_start to start it again.
|
|
*
|
|
* @dev: WDT Device
|
|
* @return: 0 if OK, -ve on error
|
|
*/
|
|
int wdt_stop(struct udevice *dev);
|
|
|
|
/*
|
|
* Reset the timer, typically restoring the counter to
|
|
* the value configured by start()
|
|
*
|
|
* @dev: WDT Device
|
|
* @return: 0 if OK, -ve on error
|
|
*/
|
|
int wdt_reset(struct udevice *dev);
|
|
|
|
/*
|
|
* Expire the timer, thus executing its action immediately.
|
|
* This is typically used to reset the board or peripherals.
|
|
*
|
|
* @dev: WDT Device
|
|
* @flags: Driver specific flags
|
|
* @return 0 if OK -ve on error. If wdt action is system reset,
|
|
* this function may never return.
|
|
*/
|
|
int wdt_expire_now(struct udevice *dev, ulong flags);
|
|
|
|
/*
|
|
* struct wdt_ops - Driver model wdt operations
|
|
*
|
|
* The uclass interface is implemented by all wdt devices which use
|
|
* driver model.
|
|
*/
|
|
struct wdt_ops {
|
|
/*
|
|
* Start the timer
|
|
*
|
|
* @dev: WDT Device
|
|
* @timeout_ms: Number of ticks (milliseconds) before the timer expires
|
|
* @flags: Driver specific flags. This might be used to specify
|
|
* which action needs to be executed when the timer expires
|
|
* @return: 0 if OK, -ve on error
|
|
*/
|
|
int (*start)(struct udevice *dev, u64 timeout_ms, ulong flags);
|
|
/*
|
|
* Stop the timer
|
|
*
|
|
* @dev: WDT Device
|
|
* @return: 0 if OK, -ve on error
|
|
*/
|
|
int (*stop)(struct udevice *dev);
|
|
/*
|
|
* Reset the timer, typically restoring the counter to
|
|
* the value configured by start()
|
|
*
|
|
* @dev: WDT Device
|
|
* @return: 0 if OK, -ve on error
|
|
*/
|
|
int (*reset)(struct udevice *dev);
|
|
/*
|
|
* Expire the timer, thus executing the action immediately (optional)
|
|
*
|
|
* If this function is not provided, a default implementation
|
|
* will be used, which sets the counter to 1
|
|
* and waits forever. This is good enough for system level
|
|
* reset, where the function is not expected to return, but might not be
|
|
* good enough for other use cases.
|
|
*
|
|
* @dev: WDT Device
|
|
* @flags: Driver specific flags
|
|
* @return 0 if OK -ve on error. May not return.
|
|
*/
|
|
int (*expire_now)(struct udevice *dev, ulong flags);
|
|
};
|
|
|
|
#if defined(CONFIG_WDT)
|
|
#ifndef CONFIG_WATCHDOG_TIMEOUT_MSECS
|
|
#define CONFIG_WATCHDOG_TIMEOUT_MSECS (60 * 1000)
|
|
#endif
|
|
#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
|
|
|
|
static inline int initr_watchdog(void)
|
|
{
|
|
u32 timeout = WATCHDOG_TIMEOUT_SECS;
|
|
|
|
/*
|
|
* Init watchdog: This will call the probe function of the
|
|
* watchdog driver, enabling the use of the device
|
|
*/
|
|
if (uclass_get_device_by_seq(UCLASS_WDT, 0,
|
|
(struct udevice **)&gd->watchdog_dev)) {
|
|
debug("WDT: Not found by seq!\n");
|
|
if (uclass_get_device(UCLASS_WDT, 0,
|
|
(struct udevice **)&gd->watchdog_dev)) {
|
|
printf("WDT: Not found!\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (CONFIG_IS_ENABLED(OF_CONTROL)) {
|
|
timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
|
|
WATCHDOG_TIMEOUT_SECS);
|
|
}
|
|
|
|
wdt_start(gd->watchdog_dev, timeout * 1000, 0);
|
|
gd->flags |= GD_FLG_WDT_READY;
|
|
printf("WDT: Started with%s servicing (%ds timeout)\n",
|
|
IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
|
|
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
#endif /* _WDT_H_ */
|