1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017 Google, Inc
6 #define LOG_CATEGORY UCLASS_WDT
17 #include <asm/global_data.h>
18 #include <dm/device-internal.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
26 /* Timeout, in seconds, to configure this device to. */
29 * Time, in milliseconds, between calling the device's ->reset()
30 * method from watchdog_reset().
34 * Next time (as returned by get_timer(0)) to call
38 /* Whether watchdog_start() has been called on the device. */
43 struct cyclic_info *cyclic;
46 static void wdt_cyclic(void *ctx)
48 struct udevice *dev = ctx;
49 struct wdt_priv *priv;
51 if (!device_active(dev))
54 priv = dev_get_uclass_priv(dev);
61 static void init_watchdog_dev(struct udevice *dev)
63 struct wdt_priv *priv;
66 priv = dev_get_uclass_priv(dev);
68 if (IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)) {
69 ret = sysreset_register_wdt(dev);
71 printf("WDT: Failed to register %s for sysreset\n",
75 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART) || priv->noautostart) {
76 printf("WDT: Not starting %s\n", dev->name);
80 ret = wdt_start(dev, priv->timeout * 1000, 0);
82 printf("WDT: Failed to start %s\n", dev->name);
87 int initr_watchdog(void)
93 ret = uclass_get(UCLASS_WDT, &uc);
95 log_debug("Error getting UCLASS_WDT: %d\n", ret);
99 uclass_foreach_dev(dev, uc) {
100 ret = device_probe(dev);
102 log_debug("Error probing %s: %d\n", dev->name, ret);
105 init_watchdog_dev(dev);
108 gd->flags |= GD_FLG_WDT_READY;
112 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
114 const struct wdt_ops *ops = device_get_ops(dev);
120 ret = ops->start(dev, timeout_ms, flags);
122 struct wdt_priv *priv = dev_get_uclass_priv(dev);
125 priv->running = true;
128 if (IS_ENABLED(CONFIG_WATCHDOG)) {
129 /* Register the watchdog driver as a cyclic function */
130 priv->cyclic = cyclic_register(wdt_cyclic,
131 priv->reset_period * 1000,
134 printf("cyclic_register for %s failed\n",
138 snprintf(str, 16, "every %ldms",
143 printf("WDT: Started %s with%s servicing %s (%ds timeout)\n",
144 dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out",
151 int wdt_stop(struct udevice *dev)
153 const struct wdt_ops *ops = device_get_ops(dev);
159 ret = ops->stop(dev);
161 struct wdt_priv *priv = dev_get_uclass_priv(dev);
163 priv->running = false;
169 int wdt_stop_all(void)
171 struct wdt_priv *priv;
176 ret = uclass_get(UCLASS_WDT, &uc);
180 uclass_foreach_dev(dev, uc) {
181 if (!device_active(dev))
183 priv = dev_get_uclass_priv(dev);
194 int wdt_reset(struct udevice *dev)
196 const struct wdt_ops *ops = device_get_ops(dev);
201 return ops->reset(dev);
204 int wdt_expire_now(struct udevice *dev, ulong flags)
207 const struct wdt_ops *ops;
209 debug("WDT Resetting: %lu\n", flags);
210 ops = device_get_ops(dev);
211 if (ops->expire_now) {
212 return ops->expire_now(dev, flags);
214 ret = wdt_start(dev, 1, flags);
225 #if defined(CONFIG_WATCHDOG)
227 * Called by macro WATCHDOG_RESET. This function be called *very* early,
228 * so we need to make sure, that the watchdog driver is ready before using
229 * it in this function.
231 void watchdog_reset(void)
234 * Empty function for now. The actual WDT handling is now done in
235 * the cyclic function instead.
240 static int wdt_post_bind(struct udevice *dev)
242 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
243 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
244 static int reloc_done;
248 ops->start += gd->reloc_off;
250 ops->stop += gd->reloc_off;
252 ops->reset += gd->reloc_off;
254 ops->expire_now += gd->reloc_off;
262 static int wdt_pre_probe(struct udevice *dev)
264 u32 timeout = WATCHDOG_TIMEOUT_SECS;
266 * Reset every 1000ms, or however often is required as
267 * indicated by a hw_margin_ms property.
269 ulong reset_period = 1000;
270 bool noautostart = false;
271 struct wdt_priv *priv;
273 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
274 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
275 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
276 4 * reset_period) / 4;
277 noautostart = dev_read_bool(dev, "u-boot,noautostart");
279 priv = dev_get_uclass_priv(dev);
280 priv->timeout = timeout;
281 priv->reset_period = reset_period;
282 priv->noautostart = noautostart;
284 * Pretend this device was last reset "long" ago so the first
285 * watchdog_reset will actually call its ->reset method.
287 priv->next_reset = get_timer(0);
292 UCLASS_DRIVER(wdt) = {
295 .flags = DM_UC_FLAG_SEQ_ALIAS,
296 .post_bind = wdt_post_bind,
297 .pre_probe = wdt_pre_probe,
298 .per_device_auto = sizeof(struct wdt_priv),