watchdog: Implement generic watchdog_reset() version
[project/bcm63xx/u-boot.git] / drivers / watchdog / wdt-uclass.c
index 23b7e3360d32fa0cd896b98583aeb5f514db79b9..bbfac4f0f9b4a189b39a55c176a540a46e0cbc19 100644 (file)
@@ -10,6 +10,8 @@
 #include <dm/device-internal.h>
 #include <dm/lists.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
 {
        const struct wdt_ops *ops = device_get_ops(dev);
@@ -63,6 +65,30 @@ int wdt_expire_now(struct udevice *dev, ulong flags)
        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)
+{
+       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
+
 static int wdt_post_bind(struct udevice *dev)
 {
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)