1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2017 Google, Inc
6 #define LOG_CATEGORY UCLASS_WDT
15 #include <asm/global_data.h>
16 #include <dm/device-internal.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
24 /* Timeout, in seconds, to configure this device to. */
27 * Time, in milliseconds, between calling the device's ->reset()
28 * method from watchdog_reset().
32 * Next time (as returned by get_timer(0)) to call
38 static void init_watchdog_dev(struct udevice *dev)
40 struct wdt_priv *priv;
43 priv = dev_get_uclass_priv(dev);
45 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
46 printf("WDT: Not starting %s\n", dev->name);
50 ret = wdt_start(dev, priv->timeout * 1000, 0);
52 printf("WDT: Failed to start %s\n", dev->name);
56 printf("WDT: Started %s with%s servicing (%ds timeout)\n", dev->name,
57 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
60 int initr_watchdog(void)
63 * Init watchdog: This will call the probe function of the
64 * watchdog driver, enabling the use of the device
66 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
67 (struct udevice **)&gd->watchdog_dev)) {
68 debug("WDT: Not found by seq!\n");
69 if (uclass_get_device(UCLASS_WDT, 0,
70 (struct udevice **)&gd->watchdog_dev)) {
71 printf("WDT: Not found!\n");
75 init_watchdog_dev(gd->watchdog_dev);
80 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
82 const struct wdt_ops *ops = device_get_ops(dev);
88 ret = ops->start(dev, timeout_ms, flags);
90 gd->flags |= GD_FLG_WDT_READY;
95 int wdt_stop(struct udevice *dev)
97 const struct wdt_ops *ops = device_get_ops(dev);
103 ret = ops->stop(dev);
105 gd->flags &= ~GD_FLG_WDT_READY;
110 int wdt_reset(struct udevice *dev)
112 const struct wdt_ops *ops = device_get_ops(dev);
117 return ops->reset(dev);
120 int wdt_expire_now(struct udevice *dev, ulong flags)
123 const struct wdt_ops *ops;
125 debug("WDT Resetting: %lu\n", flags);
126 ops = device_get_ops(dev);
127 if (ops->expire_now) {
128 return ops->expire_now(dev, flags);
130 ret = wdt_start(dev, 1, flags);
141 #if defined(CONFIG_WATCHDOG)
143 * Called by macro WATCHDOG_RESET. This function be called *very* early,
144 * so we need to make sure, that the watchdog driver is ready before using
145 * it in this function.
147 void watchdog_reset(void)
149 struct wdt_priv *priv;
153 /* Exit if GD is not ready or watchdog is not initialized yet */
154 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
157 dev = gd->watchdog_dev;
158 priv = dev_get_uclass_priv(dev);
159 /* Do not reset the watchdog too often */
161 if (time_after_eq(now, priv->next_reset)) {
162 priv->next_reset = now + priv->reset_period;
168 static int wdt_post_bind(struct udevice *dev)
170 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
171 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
172 static int reloc_done;
176 ops->start += gd->reloc_off;
178 ops->stop += gd->reloc_off;
180 ops->reset += gd->reloc_off;
182 ops->expire_now += gd->reloc_off;
190 static int wdt_pre_probe(struct udevice *dev)
192 u32 timeout = WATCHDOG_TIMEOUT_SECS;
194 * Reset every 1000ms, or however often is required as
195 * indicated by a hw_margin_ms property.
197 ulong reset_period = 1000;
198 struct wdt_priv *priv;
200 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
201 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
202 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
203 4 * reset_period) / 4;
205 priv = dev_get_uclass_priv(dev);
206 priv->timeout = timeout;
207 priv->reset_period = reset_period;
209 * Pretend this device was last reset "long" ago so the first
210 * watchdog_reset will actually call its ->reset method.
212 priv->next_reset = get_timer(0);
217 UCLASS_DRIVER(wdt) = {
220 .flags = DM_UC_FLAG_SEQ_ALIAS,
221 .post_bind = wdt_post_bind,
222 .pre_probe = wdt_pre_probe,
223 .per_device_auto = sizeof(struct wdt_priv),