1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017 Google, Inc
12 #include <dm/device-internal.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
19 int initr_watchdog(void)
21 u32 timeout = WATCHDOG_TIMEOUT_SECS;
24 * Init watchdog: This will call the probe function of the
25 * watchdog driver, enabling the use of the device
27 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
28 (struct udevice **)&gd->watchdog_dev)) {
29 debug("WDT: Not found by seq!\n");
30 if (uclass_get_device(UCLASS_WDT, 0,
31 (struct udevice **)&gd->watchdog_dev)) {
32 printf("WDT: Not found!\n");
37 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
38 timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
39 WATCHDOG_TIMEOUT_SECS);
42 wdt_start(gd->watchdog_dev, timeout * 1000, 0);
43 gd->flags |= GD_FLG_WDT_READY;
44 printf("WDT: Started with%s servicing (%ds timeout)\n",
45 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
50 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
52 const struct wdt_ops *ops = device_get_ops(dev);
57 return ops->start(dev, timeout_ms, flags);
60 int wdt_stop(struct udevice *dev)
62 const struct wdt_ops *ops = device_get_ops(dev);
67 return ops->stop(dev);
70 int wdt_reset(struct udevice *dev)
72 const struct wdt_ops *ops = device_get_ops(dev);
77 return ops->reset(dev);
80 int wdt_expire_now(struct udevice *dev, ulong flags)
83 const struct wdt_ops *ops;
85 debug("WDT Resetting: %lu\n", flags);
86 ops = device_get_ops(dev);
87 if (ops->expire_now) {
88 return ops->expire_now(dev, flags);
93 ret = ops->start(dev, 1, flags);
103 #if defined(CONFIG_WATCHDOG)
105 * Called by macro WATCHDOG_RESET. This function be called *very* early,
106 * so we need to make sure, that the watchdog driver is ready before using
107 * it in this function.
109 void watchdog_reset(void)
111 static ulong next_reset;
114 /* Exit if GD is not ready or watchdog is not initialized yet */
115 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
118 /* Do not reset the watchdog too often */
120 if (time_after(now, next_reset)) {
121 next_reset = now + 1000; /* reset every 1000ms */
122 wdt_reset(gd->watchdog_dev);
127 static int wdt_post_bind(struct udevice *dev)
129 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
130 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
131 static int reloc_done;
135 ops->start += gd->reloc_off;
137 ops->stop += gd->reloc_off;
139 ops->reset += gd->reloc_off;
141 ops->expire_now += gd->reloc_off;
149 UCLASS_DRIVER(wdt) = {
152 .flags = DM_UC_FLAG_SEQ_ALIAS,
153 .post_bind = wdt_post_bind,