blob: 12850016c93f2a1b8c1c3e253f1c9cf77d44200b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
maxims@google.com0753bc22017-04-17 12:00:21 -07002/*
3 * Copyright 2017 Google, Inc
maxims@google.com0753bc22017-04-17 12:00:21 -07004 */
5
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_WDT
7
Stefan Roesec2fd0ca2022-08-18 13:22:46 +02008#include <cyclic.h>
Chanho Parka341a0e2023-12-03 17:30:40 +09009#include <div64.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070010#include <dm.h>
11#include <errno.h>
Simon Glassdb41d652019-12-28 10:45:07 -070012#include <hang.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Samuel Hollanda8f63d12021-11-03 22:55:14 -050014#include <sysreset.h>
Chris Packham6d8eae92020-02-24 13:20:33 +130015#include <time.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070016#include <wdt.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070018#include <dm/device-internal.h>
19#include <dm/lists.h>
20
Stefan Roese06985282019-04-11 15:58:44 +020021DECLARE_GLOBAL_DATA_PTR;
22
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010023#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
24
Rasmus Villemoes6d24dc82021-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 Villemoesf1b112a2021-08-19 11:56:59 +020038 /* Whether watchdog_start() has been called on the device. */
39 bool running;
Rasmus Villemoes27836702022-09-27 11:54:03 +020040 /* autostart */
41 bool autostart;
Stefan Roesec2fd0ca2022-08-18 13:22:46 +020042
43 struct cyclic_info *cyclic;
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +020044};
Rasmus Villemoes40d7f3c2020-03-13 17:04:58 +010045
Stefan Roesec2fd0ca2022-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 Villemoes3eaf6e22021-08-19 11:56:58 +020061static void init_watchdog_dev(struct udevice *dev)
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010062{
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +020063 struct wdt_priv *priv;
Pali Rohár25e20e32021-03-09 14:26:55 +010064 int ret;
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010065
Rasmus Villemoes3eaf6e22021-08-19 11:56:58 +020066 priv = dev_get_uclass_priv(dev);
67
Samuel Hollanda8f63d12021-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 Villemoes27836702022-09-27 11:54:03 +020075 if (!priv->autostart) {
Rasmus Villemoes3eaf6e22021-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 Villemoes3eaf6e22021-08-19 11:56:58 +020085}
86
87int initr_watchdog(void)
88{
Rasmus Villemoes492ee6b2021-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;
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010097 }
Rasmus Villemoes492ee6b2021-08-19 11:57:03 +020098
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;
104 }
105 init_watchdog_dev(dev);
106 }
Rasmus Villemoesb4d94522020-03-13 17:04:57 +0100107
108 return 0;
109}
110
Andy Shevchenkoffdec302017-08-04 15:48:28 -0600111int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.com0753bc22017-04-17 12:00:21 -0700112{
113 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +0100114 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -0700115
116 if (!ops->start)
117 return -ENOSYS;
118
Pali Rohár9c44ff12021-03-09 14:26:54 +0100119 ret = ops->start(dev, timeout_ms, flags);
Rasmus Villemoesf1b112a2021-08-19 11:56:59 +0200120 if (ret == 0) {
121 struct wdt_priv *priv = dev_get_uclass_priv(dev);
Stefan Roesec2fd0ca2022-08-18 13:22:46 +0200122 char str[16];
Rasmus Villemoesf1b112a2021-08-19 11:56:59 +0200123
Stefan Roesec2fd0ca2022-08-18 13:22:46 +0200124 memset(str, 0, 16);
125 if (IS_ENABLED(CONFIG_WATCHDOG)) {
Rasmus Villemoesdf2b3822024-05-21 10:46:51 +0200126 if (priv->running)
127 cyclic_unregister(priv->cyclic);
128
Stefan Roesec2fd0ca2022-08-18 13:22:46 +0200129 /* 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
Rasmus Villemoesdf2b3822024-05-21 10:46:51 +0200143 priv->running = true;
Stefan Roesec2fd0ca2022-08-18 13:22:46 +0200144 printf("WDT: Started %s with%s servicing %s (%ds timeout)\n",
145 dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out",
Chanho Parka341a0e2023-12-03 17:30:40 +0900146 str, (u32)lldiv(timeout_ms, 1000));
Rasmus Villemoesf1b112a2021-08-19 11:56:59 +0200147 }
Pali Rohár9c44ff12021-03-09 14:26:54 +0100148
149 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -0700150}
151
152int wdt_stop(struct udevice *dev)
153{
154 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +0100155 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -0700156
157 if (!ops->stop)
158 return -ENOSYS;
159
Pali Rohár9c44ff12021-03-09 14:26:54 +0100160 ret = ops->stop(dev);
Rasmus Villemoesf1b112a2021-08-19 11:56:59 +0200161 if (ret == 0) {
162 struct wdt_priv *priv = dev_get_uclass_priv(dev);
163
Rasmus Villemoesdf2b3822024-05-21 10:46:51 +0200164 if (IS_ENABLED(CONFIG_WATCHDOG) && priv->running)
165 cyclic_unregister(priv->cyclic);
166
Rasmus Villemoesf1b112a2021-08-19 11:56:59 +0200167 priv->running = false;
168 }
Pali Rohár9c44ff12021-03-09 14:26:54 +0100169
170 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -0700171}
172
Rasmus Villemoes90555dc2021-08-19 11:57:01 +0200173int wdt_stop_all(void)
174{
175 struct wdt_priv *priv;
176 struct udevice *dev;
177 struct uclass *uc;
178 int ret, err;
179
180 ret = uclass_get(UCLASS_WDT, &uc);
181 if (ret)
182 return ret;
183
184 uclass_foreach_dev(dev, uc) {
185 if (!device_active(dev))
186 continue;
187 priv = dev_get_uclass_priv(dev);
188 if (!priv->running)
189 continue;
190 err = wdt_stop(dev);
191 if (!ret)
192 ret = err;
193 }
194
195 return ret;
196}
197
maxims@google.com0753bc22017-04-17 12:00:21 -0700198int wdt_reset(struct udevice *dev)
199{
200 const struct wdt_ops *ops = device_get_ops(dev);
201
202 if (!ops->reset)
203 return -ENOSYS;
204
205 return ops->reset(dev);
206}
207
208int wdt_expire_now(struct udevice *dev, ulong flags)
209{
210 int ret = 0;
211 const struct wdt_ops *ops;
212
Andy Shevchenkob71e0c12017-07-05 20:44:06 +0300213 debug("WDT Resetting: %lu\n", flags);
maxims@google.com0753bc22017-04-17 12:00:21 -0700214 ops = device_get_ops(dev);
215 if (ops->expire_now) {
216 return ops->expire_now(dev, flags);
217 } else {
Rasmus Villemoes8967ebb2021-08-19 11:56:55 +0200218 ret = wdt_start(dev, 1, flags);
maxims@google.com0753bc22017-04-17 12:00:21 -0700219
maxims@google.com0753bc22017-04-17 12:00:21 -0700220 if (ret < 0)
221 return ret;
222
223 hang();
224 }
225
226 return ret;
227}
228
Stefan Roese06985282019-04-11 15:58:44 +0200229#if defined(CONFIG_WATCHDOG)
230/*
231 * Called by macro WATCHDOG_RESET. This function be called *very* early,
232 * so we need to make sure, that the watchdog driver is ready before using
233 * it in this function.
234 */
235void watchdog_reset(void)
236{
Rasmus Villemoes492ee6b2021-08-19 11:57:03 +0200237 /*
Stefan Roesec2fd0ca2022-08-18 13:22:46 +0200238 * Empty function for now. The actual WDT handling is now done in
239 * the cyclic function instead.
Rasmus Villemoes492ee6b2021-08-19 11:57:03 +0200240 */
Stefan Roese06985282019-04-11 15:58:44 +0200241}
242#endif
243
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200244static int wdt_pre_probe(struct udevice *dev)
245{
246 u32 timeout = WATCHDOG_TIMEOUT_SECS;
247 /*
248 * Reset every 1000ms, or however often is required as
249 * indicated by a hw_margin_ms property.
250 */
251 ulong reset_period = 1000;
Rasmus Villemoes27836702022-09-27 11:54:03 +0200252 bool autostart = IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART);
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200253 struct wdt_priv *priv;
254
255 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
256 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
257 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
258 4 * reset_period) / 4;
Rasmus Villemoes27836702022-09-27 11:54:03 +0200259 if (dev_read_bool(dev, "u-boot,noautostart"))
260 autostart = false;
261 else if (dev_read_bool(dev, "u-boot,autostart"))
262 autostart = true;
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200263 }
264 priv = dev_get_uclass_priv(dev);
265 priv->timeout = timeout;
266 priv->reset_period = reset_period;
Rasmus Villemoes27836702022-09-27 11:54:03 +0200267 priv->autostart = autostart;
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200268 /*
269 * Pretend this device was last reset "long" ago so the first
270 * watchdog_reset will actually call its ->reset method.
271 */
272 priv->next_reset = get_timer(0);
273
274 return 0;
275}
276
maxims@google.com0753bc22017-04-17 12:00:21 -0700277UCLASS_DRIVER(wdt) = {
Rasmus Villemoes068f8ea2021-08-19 11:56:57 +0200278 .id = UCLASS_WDT,
279 .name = "watchdog",
280 .flags = DM_UC_FLAG_SEQ_ALIAS,
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200281 .pre_probe = wdt_pre_probe,
282 .per_device_auto = sizeof(struct wdt_priv),
maxims@google.com0753bc22017-04-17 12:00:21 -0700283};