1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017 Google, Inc
6 #define LOG_CATEGORY UCLASS_WDT
16 #include <asm/global_data.h>
17 #include <dm/device-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
25 /* Timeout, in seconds, to configure this device to. */
28 * Time, in milliseconds, between calling the device's ->reset()
29 * method from watchdog_reset().
33 * Next time (as returned by get_timer(0)) to call
37 /* Whether watchdog_start() has been called on the device. */
43 static void init_watchdog_dev(struct udevice *dev)
45 struct wdt_priv *priv;
48 priv = dev_get_uclass_priv(dev);
50 if (IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)) {
51 ret = sysreset_register_wdt(dev);
53 printf("WDT: Failed to register %s for sysreset\n",
57 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART) || priv->noautostart) {
58 printf("WDT: Not starting %s\n", dev->name);
62 ret = wdt_start(dev, priv->timeout * 1000, 0);
64 printf("WDT: Failed to start %s\n", dev->name);
68 printf("WDT: Started %s with%s servicing (%ds timeout)\n", dev->name,
69 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
72 int initr_watchdog(void)
78 ret = uclass_get(UCLASS_WDT, &uc);
80 log_debug("Error getting UCLASS_WDT: %d\n", ret);
84 uclass_foreach_dev(dev, uc) {
85 ret = device_probe(dev);
87 log_debug("Error probing %s: %d\n", dev->name, ret);
90 init_watchdog_dev(dev);
93 gd->flags |= GD_FLG_WDT_READY;
97 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
99 const struct wdt_ops *ops = device_get_ops(dev);
105 ret = ops->start(dev, timeout_ms, flags);
107 struct wdt_priv *priv = dev_get_uclass_priv(dev);
109 priv->running = true;
115 int wdt_stop(struct udevice *dev)
117 const struct wdt_ops *ops = device_get_ops(dev);
123 ret = ops->stop(dev);
125 struct wdt_priv *priv = dev_get_uclass_priv(dev);
127 priv->running = false;
133 int wdt_stop_all(void)
135 struct wdt_priv *priv;
140 ret = uclass_get(UCLASS_WDT, &uc);
144 uclass_foreach_dev(dev, uc) {
145 if (!device_active(dev))
147 priv = dev_get_uclass_priv(dev);
158 int wdt_reset(struct udevice *dev)
160 const struct wdt_ops *ops = device_get_ops(dev);
165 return ops->reset(dev);
168 int wdt_expire_now(struct udevice *dev, ulong flags)
171 const struct wdt_ops *ops;
173 debug("WDT Resetting: %lu\n", flags);
174 ops = device_get_ops(dev);
175 if (ops->expire_now) {
176 return ops->expire_now(dev, flags);
178 ret = wdt_start(dev, 1, flags);
189 #if defined(CONFIG_WATCHDOG)
191 * Called by macro WATCHDOG_RESET. This function be called *very* early,
192 * so we need to make sure, that the watchdog driver is ready before using
193 * it in this function.
195 void watchdog_reset(void)
197 struct wdt_priv *priv;
202 /* Exit if GD is not ready or watchdog is not initialized yet */
203 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
206 if (uclass_get(UCLASS_WDT, &uc))
210 * All devices bound to the wdt uclass should have been probed
211 * in initr_watchdog(). But just in case something went wrong,
212 * check device_active() before accessing the uclass private
215 uclass_foreach_dev(dev, uc) {
216 if (!device_active(dev))
218 priv = dev_get_uclass_priv(dev);
221 /* Do not reset the watchdog too often */
223 if (time_after_eq(now, priv->next_reset)) {
224 priv->next_reset = now + priv->reset_period;
231 static int wdt_post_bind(struct udevice *dev)
233 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
234 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
235 static int reloc_done;
239 ops->start += gd->reloc_off;
241 ops->stop += gd->reloc_off;
243 ops->reset += gd->reloc_off;
245 ops->expire_now += gd->reloc_off;
253 static int wdt_pre_probe(struct udevice *dev)
255 u32 timeout = WATCHDOG_TIMEOUT_SECS;
257 * Reset every 1000ms, or however often is required as
258 * indicated by a hw_margin_ms property.
260 ulong reset_period = 1000;
261 bool noautostart = false;
262 struct wdt_priv *priv;
264 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
265 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
266 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
267 4 * reset_period) / 4;
268 noautostart = dev_read_bool(dev, "u-boot,noautostart");
270 priv = dev_get_uclass_priv(dev);
271 priv->timeout = timeout;
272 priv->reset_period = reset_period;
273 priv->noautostart = noautostart;
275 * Pretend this device was last reset "long" ago so the first
276 * watchdog_reset will actually call its ->reset method.
278 priv->next_reset = get_timer(0);
283 UCLASS_DRIVER(wdt) = {
286 .flags = DM_UC_FLAG_SEQ_ALIAS,
287 .post_bind = wdt_post_bind,
288 .pre_probe = wdt_pre_probe,
289 .per_device_auto = sizeof(struct wdt_priv),