Merge branch '2019-07-26-ti-imports'
[oweals/u-boot.git] / include / wdt.h
index 9b90fbeeb3a304d44ce1149ffd7d1dc9899cc3c3..5bcff24ab310020a19b053e22e85db7345a62b64 100644 (file)
@@ -1,12 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
 /*
  * Copyright 2017 Google, Inc
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #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
@@ -104,4 +106,42 @@ struct wdt_ops {
        int (*expire_now)(struct udevice *dev, ulong flags);
 };
 
+#if CONFIG_IS_ENABLED(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_ */