1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017 Google, Inc
13 #include <dm/device-internal.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
21 * Reset every 1000ms, or however often is required as indicated by a
22 * hw_margin_ms property.
24 static ulong reset_period = 1000;
26 int initr_watchdog(void)
28 u32 timeout = WATCHDOG_TIMEOUT_SECS;
31 * Init watchdog: This will call the probe function of the
32 * watchdog driver, enabling the use of the device
34 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
35 (struct udevice **)&gd->watchdog_dev)) {
36 debug("WDT: Not found by seq!\n");
37 if (uclass_get_device(UCLASS_WDT, 0,
38 (struct udevice **)&gd->watchdog_dev)) {
39 printf("WDT: Not found!\n");
44 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
45 timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
46 WATCHDOG_TIMEOUT_SECS);
47 reset_period = dev_read_u32_default(gd->watchdog_dev,
49 4 * reset_period) / 4;
52 wdt_start(gd->watchdog_dev, timeout * 1000, 0);
53 gd->flags |= GD_FLG_WDT_READY;
54 printf("WDT: Started with%s servicing (%ds timeout)\n",
55 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
60 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
62 const struct wdt_ops *ops = device_get_ops(dev);
67 return ops->start(dev, timeout_ms, flags);
70 int wdt_stop(struct udevice *dev)
72 const struct wdt_ops *ops = device_get_ops(dev);
77 return ops->stop(dev);
80 int wdt_reset(struct udevice *dev)
82 const struct wdt_ops *ops = device_get_ops(dev);
87 return ops->reset(dev);
90 int wdt_expire_now(struct udevice *dev, ulong flags)
93 const struct wdt_ops *ops;
95 debug("WDT Resetting: %lu\n", flags);
96 ops = device_get_ops(dev);
97 if (ops->expire_now) {
98 return ops->expire_now(dev, flags);
103 ret = ops->start(dev, 1, flags);
113 #if defined(CONFIG_WATCHDOG)
115 * Called by macro WATCHDOG_RESET. This function be called *very* early,
116 * so we need to make sure, that the watchdog driver is ready before using
117 * it in this function.
119 void watchdog_reset(void)
121 static ulong next_reset;
124 /* Exit if GD is not ready or watchdog is not initialized yet */
125 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
128 /* Do not reset the watchdog too often */
130 if (time_after(now, next_reset)) {
131 next_reset = now + reset_period;
132 wdt_reset(gd->watchdog_dev);
137 static int wdt_post_bind(struct udevice *dev)
139 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
140 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
141 static int reloc_done;
145 ops->start += gd->reloc_off;
147 ops->stop += gd->reloc_off;
149 ops->reset += gd->reloc_off;
151 ops->expire_now += gd->reloc_off;
159 UCLASS_DRIVER(wdt) = {
162 .flags = DM_UC_FLAG_SEQ_ALIAS,
163 .post_bind = wdt_post_bind,