blob: 82df0ff0be5a271b407337e168ea3eef71db6027 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
maxims@google.comdaea6d42017-04-17 12:00:21 -07002/*
3 * Copyright 2017 Google, Inc
maxims@google.comdaea6d42017-04-17 12:00:21 -07004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_WDT
7
maxims@google.comdaea6d42017-04-17 12:00:21 -07008#include <common.h>
Stefan Roesecce24722022-08-18 13:22:46 +02009#include <cyclic.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070010#include <dm.h>
11#include <errno.h>
Simon Glassf11478f2019-12-28 10:45:07 -070012#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Samuel Holland3a8713a2021-11-03 22:55:14 -050014#include <sysreset.h>
Chris Packham32a234f2020-02-24 13:20:33 +130015#include <time.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070016#include <wdt.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070018#include <dm/device-internal.h>
19#include <dm/lists.h>
20
Stefan Roese502acb02019-04-11 15:58:44 +020021DECLARE_GLOBAL_DATA_PTR;
22
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010023#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
24
Rasmus Villemoes635f9242021-08-19 11:56:56 +020025struct wdt_priv {
26 /* Timeout, in seconds, to configure this device to. */
27 u32 timeout;
28 /*
29 * Time, in milliseconds, between calling the device's ->reset()
30 * method from watchdog_reset().
31 */
32 ulong reset_period;
33 /*
34 * Next time (as returned by get_timer(0)) to call
35 * ->reset().
36 */
37 ulong next_reset;
Rasmus Villemoes85fb8222021-08-19 11:56:59 +020038 /* Whether watchdog_start() has been called on the device. */
39 bool running;
Rasmus Villemoesce66f192022-09-27 11:54:03 +020040 /* autostart */
41 bool autostart;
Stefan Roesecce24722022-08-18 13:22:46 +020042
43 struct cyclic_info *cyclic;
Rasmus Villemoes635f9242021-08-19 11:56:56 +020044};
Rasmus Villemoes2ba843d2020-03-13 17:04:58 +010045
Stefan Roesecce24722022-08-18 13:22:46 +020046static void wdt_cyclic(void *ctx)
47{
48 struct udevice *dev = ctx;
49 struct wdt_priv *priv;
50
51 if (!device_active(dev))
52 return;
53
54 priv = dev_get_uclass_priv(dev);
55 if (!priv->running)
56 return;
57
58 wdt_reset(dev);
59}
60
Rasmus Villemoesc2453182021-08-19 11:56:58 +020061static void init_watchdog_dev(struct udevice *dev)
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010062{
Rasmus Villemoes635f9242021-08-19 11:56:56 +020063 struct wdt_priv *priv;
Pali Rohárc80b8212021-03-09 14:26:55 +010064 int ret;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010065
Rasmus Villemoesc2453182021-08-19 11:56:58 +020066 priv = dev_get_uclass_priv(dev);
67
Samuel Holland3a8713a2021-11-03 22:55:14 -050068 if (IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)) {
69 ret = sysreset_register_wdt(dev);
70 if (ret)
71 printf("WDT: Failed to register %s for sysreset\n",
72 dev->name);
73 }
74
Rasmus Villemoesce66f192022-09-27 11:54:03 +020075 if (!priv->autostart) {
Rasmus Villemoesc2453182021-08-19 11:56:58 +020076 printf("WDT: Not starting %s\n", dev->name);
77 return;
78 }
79
80 ret = wdt_start(dev, priv->timeout * 1000, 0);
81 if (ret != 0) {
82 printf("WDT: Failed to start %s\n", dev->name);
83 return;
84 }
Rasmus Villemoesc2453182021-08-19 11:56:58 +020085}
86
87int initr_watchdog(void)
88{
Rasmus Villemoes3be6a352021-08-19 11:57:03 +020089 struct udevice *dev;
90 struct uclass *uc;
91 int ret;
92
93 ret = uclass_get(UCLASS_WDT, &uc);
94 if (ret) {
95 log_debug("Error getting UCLASS_WDT: %d\n", ret);
96 return 0;
97 }
98
99 uclass_foreach_dev(dev, uc) {
100 ret = device_probe(dev);
101 if (ret) {
102 log_debug("Error probing %s: %d\n", dev->name, ret);
103 continue;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +0100104 }
Rasmus Villemoes3be6a352021-08-19 11:57:03 +0200105 init_watchdog_dev(dev);
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +0100106 }
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +0100107
Rasmus Villemoes85fb8222021-08-19 11:56:59 +0200108 gd->flags |= GD_FLG_WDT_READY;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +0100109 return 0;
110}
111
Andy Shevchenko2befb4b2017-08-04 15:48:28 -0600112int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.comdaea6d42017-04-17 12:00:21 -0700113{
114 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100115 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -0700116
117 if (!ops->start)
118 return -ENOSYS;
119
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100120 ret = ops->start(dev, timeout_ms, flags);
Rasmus Villemoes85fb8222021-08-19 11:56:59 +0200121 if (ret == 0) {
122 struct wdt_priv *priv = dev_get_uclass_priv(dev);
Stefan Roesecce24722022-08-18 13:22:46 +0200123 char str[16];
Rasmus Villemoes85fb8222021-08-19 11:56:59 +0200124
125 priv->running = true;
Stefan Roesecce24722022-08-18 13:22:46 +0200126
127 memset(str, 0, 16);
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,
132 dev->name, dev);
133 if (!priv->cyclic) {
134 printf("cyclic_register for %s failed\n",
135 dev->name);
136 return -ENODEV;
137 } else {
138 snprintf(str, 16, "every %ldms",
139 priv->reset_period);
140 }
141 }
142
143 printf("WDT: Started %s with%s servicing %s (%ds timeout)\n",
144 dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out",
145 str, priv->timeout);
Rasmus Villemoes85fb8222021-08-19 11:56:59 +0200146 }
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100147
148 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -0700149}
150
151int wdt_stop(struct udevice *dev)
152{
153 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100154 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -0700155
156 if (!ops->stop)
157 return -ENOSYS;
158
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100159 ret = ops->stop(dev);
Rasmus Villemoes85fb8222021-08-19 11:56:59 +0200160 if (ret == 0) {
161 struct wdt_priv *priv = dev_get_uclass_priv(dev);
162
163 priv->running = false;
164 }
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100165
166 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -0700167}
168
Rasmus Villemoesbeb8c2f2021-08-19 11:57:01 +0200169int wdt_stop_all(void)
170{
171 struct wdt_priv *priv;
172 struct udevice *dev;
173 struct uclass *uc;
174 int ret, err;
175
176 ret = uclass_get(UCLASS_WDT, &uc);
177 if (ret)
178 return ret;
179
180 uclass_foreach_dev(dev, uc) {
181 if (!device_active(dev))
182 continue;
183 priv = dev_get_uclass_priv(dev);
184 if (!priv->running)
185 continue;
186 err = wdt_stop(dev);
187 if (!ret)
188 ret = err;
189 }
190
191 return ret;
192}
193
maxims@google.comdaea6d42017-04-17 12:00:21 -0700194int wdt_reset(struct udevice *dev)
195{
196 const struct wdt_ops *ops = device_get_ops(dev);
197
198 if (!ops->reset)
199 return -ENOSYS;
200
201 return ops->reset(dev);
202}
203
204int wdt_expire_now(struct udevice *dev, ulong flags)
205{
206 int ret = 0;
207 const struct wdt_ops *ops;
208
Andy Shevchenkof047be42017-07-05 20:44:06 +0300209 debug("WDT Resetting: %lu\n", flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700210 ops = device_get_ops(dev);
211 if (ops->expire_now) {
212 return ops->expire_now(dev, flags);
213 } else {
Rasmus Villemoesf008c302021-08-19 11:56:55 +0200214 ret = wdt_start(dev, 1, flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700215
maxims@google.comdaea6d42017-04-17 12:00:21 -0700216 if (ret < 0)
217 return ret;
218
219 hang();
220 }
221
222 return ret;
223}
Stefan Roese502acb02019-04-11 15:58:44 +0200224
225#if defined(CONFIG_WATCHDOG)
226/*
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.
230 */
231void watchdog_reset(void)
232{
Rasmus Villemoes3be6a352021-08-19 11:57:03 +0200233 /*
Stefan Roesecce24722022-08-18 13:22:46 +0200234 * Empty function for now. The actual WDT handling is now done in
235 * the cyclic function instead.
Rasmus Villemoes3be6a352021-08-19 11:57:03 +0200236 */
Stefan Roese502acb02019-04-11 15:58:44 +0200237}
238#endif
maxims@google.comdaea6d42017-04-17 12:00:21 -0700239
Michal Simek7b5eca92018-07-11 15:42:25 +0200240static int wdt_post_bind(struct udevice *dev)
241{
242#if defined(CONFIG_NEEDS_MANUAL_RELOC)
243 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
244 static int reloc_done;
245
246 if (!reloc_done) {
247 if (ops->start)
248 ops->start += gd->reloc_off;
249 if (ops->stop)
250 ops->stop += gd->reloc_off;
251 if (ops->reset)
252 ops->reset += gd->reloc_off;
253 if (ops->expire_now)
254 ops->expire_now += gd->reloc_off;
255
256 reloc_done++;
257 }
258#endif
259 return 0;
260}
261
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200262static int wdt_pre_probe(struct udevice *dev)
263{
264 u32 timeout = WATCHDOG_TIMEOUT_SECS;
265 /*
266 * Reset every 1000ms, or however often is required as
267 * indicated by a hw_margin_ms property.
268 */
269 ulong reset_period = 1000;
Rasmus Villemoesce66f192022-09-27 11:54:03 +0200270 bool autostart = IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART);
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200271 struct wdt_priv *priv;
272
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;
Rasmus Villemoesce66f192022-09-27 11:54:03 +0200277 if (dev_read_bool(dev, "u-boot,noautostart"))
278 autostart = false;
279 else if (dev_read_bool(dev, "u-boot,autostart"))
280 autostart = true;
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200281 }
282 priv = dev_get_uclass_priv(dev);
283 priv->timeout = timeout;
284 priv->reset_period = reset_period;
Rasmus Villemoesce66f192022-09-27 11:54:03 +0200285 priv->autostart = autostart;
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200286 /*
287 * Pretend this device was last reset "long" ago so the first
288 * watchdog_reset will actually call its ->reset method.
289 */
290 priv->next_reset = get_timer(0);
291
292 return 0;
293}
294
maxims@google.comdaea6d42017-04-17 12:00:21 -0700295UCLASS_DRIVER(wdt) = {
Rasmus Villemoes1e42d372021-08-19 11:56:57 +0200296 .id = UCLASS_WDT,
297 .name = "watchdog",
298 .flags = DM_UC_FLAG_SEQ_ALIAS,
299 .post_bind = wdt_post_bind,
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200300 .pre_probe = wdt_pre_probe,
301 .per_device_auto = sizeof(struct wdt_priv),
maxims@google.comdaea6d42017-04-17 12:00:21 -0700302};