clk: sifive: fu540-prci: Add clock enable and disable ops
[oweals/u-boot.git] / drivers / watchdog / wdt-uclass.c
index cf1c52747397ff9e3f253caf1a7a89b2ae55e2d7..e632f077f342bb5e279adfd12348639bcd831bc7 100644 (file)
@@ -7,12 +7,56 @@
 #include <dm.h>
 #include <errno.h>
 #include <hang.h>
+#include <log.h>
+#include <time.h>
 #include <wdt.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+#define WATCHDOG_TIMEOUT_SECS  (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
+
+/*
+ * Reset every 1000ms, or however often is required as indicated by a
+ * hw_margin_ms property.
+ */
+static ulong reset_period = 1000;
+
+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) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
+               timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
+                                              WATCHDOG_TIMEOUT_SECS);
+               reset_period = dev_read_u32_default(gd->watchdog_dev,
+                                                   "hw_margin_ms",
+                                                   4 * reset_period) / 4;
+       }
+
+       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;
+}
+
 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
 {
        const struct wdt_ops *ops = device_get_ops(dev);
@@ -83,8 +127,8 @@ void watchdog_reset(void)
 
        /* Do not reset the watchdog too often */
        now = get_timer(0);
-       if (now > next_reset) {
-               next_reset = now + 1000;        /* reset every 1000ms */
+       if (time_after(now, next_reset)) {
+               next_reset = now + reset_period;
                wdt_reset(gd->watchdog_dev);
        }
 }