Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017 Google, Inc |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_WDT |
| 7 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 8 | #include <common.h> |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 9 | #include <cyclic.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 12 | #include <hang.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Samuel Holland | 3a8713a | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 14 | #include <sysreset.h> |
Chris Packham | 32a234f | 2020-02-24 13:20:33 +1300 | [diff] [blame] | 15 | #include <time.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 16 | #include <wdt.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/lists.h> |
| 20 | |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 23 | #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000) |
| 24 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 25 | struct 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 Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 38 | /* Whether watchdog_start() has been called on the device. */ |
| 39 | bool running; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 40 | /* autostart */ |
| 41 | bool autostart; |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 42 | |
| 43 | struct cyclic_info *cyclic; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 44 | }; |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 45 | |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 46 | static 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 Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 61 | static void init_watchdog_dev(struct udevice *dev) |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 62 | { |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 63 | struct wdt_priv *priv; |
Pali Rohár | c80b821 | 2021-03-09 14:26:55 +0100 | [diff] [blame] | 64 | int ret; |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 65 | |
Rasmus Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 66 | priv = dev_get_uclass_priv(dev); |
| 67 | |
Samuel Holland | 3a8713a | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 68 | 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 Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 75 | if (!priv->autostart) { |
Rasmus Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 76 | 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 Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | int initr_watchdog(void) |
| 88 | { |
Rasmus Villemoes | 3be6a35 | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 89 | 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 Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 104 | } |
Rasmus Villemoes | 3be6a35 | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 105 | init_watchdog_dev(dev); |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 106 | } |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 111 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 112 | { |
| 113 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 114 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 115 | |
| 116 | if (!ops->start) |
| 117 | return -ENOSYS; |
| 118 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 119 | ret = ops->start(dev, timeout_ms, flags); |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 120 | if (ret == 0) { |
| 121 | struct wdt_priv *priv = dev_get_uclass_priv(dev); |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 122 | char str[16]; |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 123 | |
| 124 | priv->running = true; |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 125 | |
| 126 | memset(str, 0, 16); |
| 127 | if (IS_ENABLED(CONFIG_WATCHDOG)) { |
| 128 | /* Register the watchdog driver as a cyclic function */ |
| 129 | priv->cyclic = cyclic_register(wdt_cyclic, |
| 130 | priv->reset_period * 1000, |
| 131 | dev->name, dev); |
| 132 | if (!priv->cyclic) { |
| 133 | printf("cyclic_register for %s failed\n", |
| 134 | dev->name); |
| 135 | return -ENODEV; |
| 136 | } else { |
| 137 | snprintf(str, 16, "every %ldms", |
| 138 | priv->reset_period); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | printf("WDT: Started %s with%s servicing %s (%ds timeout)\n", |
| 143 | dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", |
| 144 | str, priv->timeout); |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 145 | } |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 146 | |
| 147 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int wdt_stop(struct udevice *dev) |
| 151 | { |
| 152 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 153 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 154 | |
| 155 | if (!ops->stop) |
| 156 | return -ENOSYS; |
| 157 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 158 | ret = ops->stop(dev); |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 159 | if (ret == 0) { |
| 160 | struct wdt_priv *priv = dev_get_uclass_priv(dev); |
| 161 | |
| 162 | priv->running = false; |
| 163 | } |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 164 | |
| 165 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Rasmus Villemoes | beb8c2f | 2021-08-19 11:57:01 +0200 | [diff] [blame] | 168 | int wdt_stop_all(void) |
| 169 | { |
| 170 | struct wdt_priv *priv; |
| 171 | struct udevice *dev; |
| 172 | struct uclass *uc; |
| 173 | int ret, err; |
| 174 | |
| 175 | ret = uclass_get(UCLASS_WDT, &uc); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | uclass_foreach_dev(dev, uc) { |
| 180 | if (!device_active(dev)) |
| 181 | continue; |
| 182 | priv = dev_get_uclass_priv(dev); |
| 183 | if (!priv->running) |
| 184 | continue; |
| 185 | err = wdt_stop(dev); |
| 186 | if (!ret) |
| 187 | ret = err; |
| 188 | } |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 193 | int wdt_reset(struct udevice *dev) |
| 194 | { |
| 195 | const struct wdt_ops *ops = device_get_ops(dev); |
| 196 | |
| 197 | if (!ops->reset) |
| 198 | return -ENOSYS; |
| 199 | |
| 200 | return ops->reset(dev); |
| 201 | } |
| 202 | |
| 203 | int wdt_expire_now(struct udevice *dev, ulong flags) |
| 204 | { |
| 205 | int ret = 0; |
| 206 | const struct wdt_ops *ops; |
| 207 | |
Andy Shevchenko | f047be4 | 2017-07-05 20:44:06 +0300 | [diff] [blame] | 208 | debug("WDT Resetting: %lu\n", flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 209 | ops = device_get_ops(dev); |
| 210 | if (ops->expire_now) { |
| 211 | return ops->expire_now(dev, flags); |
| 212 | } else { |
Rasmus Villemoes | f008c30 | 2021-08-19 11:56:55 +0200 | [diff] [blame] | 213 | ret = wdt_start(dev, 1, flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 214 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 215 | if (ret < 0) |
| 216 | return ret; |
| 217 | |
| 218 | hang(); |
| 219 | } |
| 220 | |
| 221 | return ret; |
| 222 | } |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 223 | |
| 224 | #if defined(CONFIG_WATCHDOG) |
| 225 | /* |
| 226 | * Called by macro WATCHDOG_RESET. This function be called *very* early, |
| 227 | * so we need to make sure, that the watchdog driver is ready before using |
| 228 | * it in this function. |
| 229 | */ |
| 230 | void watchdog_reset(void) |
| 231 | { |
Rasmus Villemoes | 3be6a35 | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 232 | /* |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 233 | * Empty function for now. The actual WDT handling is now done in |
| 234 | * the cyclic function instead. |
Rasmus Villemoes | 3be6a35 | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 235 | */ |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 236 | } |
| 237 | #endif |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 238 | |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 239 | static int wdt_post_bind(struct udevice *dev) |
| 240 | { |
| 241 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 242 | struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev); |
| 243 | static int reloc_done; |
| 244 | |
| 245 | if (!reloc_done) { |
| 246 | if (ops->start) |
| 247 | ops->start += gd->reloc_off; |
| 248 | if (ops->stop) |
| 249 | ops->stop += gd->reloc_off; |
| 250 | if (ops->reset) |
| 251 | ops->reset += gd->reloc_off; |
| 252 | if (ops->expire_now) |
| 253 | ops->expire_now += gd->reloc_off; |
| 254 | |
| 255 | reloc_done++; |
| 256 | } |
| 257 | #endif |
| 258 | return 0; |
| 259 | } |
| 260 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 261 | static int wdt_pre_probe(struct udevice *dev) |
| 262 | { |
| 263 | u32 timeout = WATCHDOG_TIMEOUT_SECS; |
| 264 | /* |
| 265 | * Reset every 1000ms, or however often is required as |
| 266 | * indicated by a hw_margin_ms property. |
| 267 | */ |
| 268 | ulong reset_period = 1000; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 269 | bool autostart = IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART); |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 270 | struct wdt_priv *priv; |
| 271 | |
| 272 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 273 | timeout = dev_read_u32_default(dev, "timeout-sec", timeout); |
| 274 | reset_period = dev_read_u32_default(dev, "hw_margin_ms", |
| 275 | 4 * reset_period) / 4; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 276 | if (dev_read_bool(dev, "u-boot,noautostart")) |
| 277 | autostart = false; |
| 278 | else if (dev_read_bool(dev, "u-boot,autostart")) |
| 279 | autostart = true; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 280 | } |
| 281 | priv = dev_get_uclass_priv(dev); |
| 282 | priv->timeout = timeout; |
| 283 | priv->reset_period = reset_period; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 284 | priv->autostart = autostart; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 285 | /* |
| 286 | * Pretend this device was last reset "long" ago so the first |
| 287 | * watchdog_reset will actually call its ->reset method. |
| 288 | */ |
| 289 | priv->next_reset = get_timer(0); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 294 | UCLASS_DRIVER(wdt) = { |
Rasmus Villemoes | 1e42d37 | 2021-08-19 11:56:57 +0200 | [diff] [blame] | 295 | .id = UCLASS_WDT, |
| 296 | .name = "watchdog", |
| 297 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 298 | .post_bind = wdt_post_bind, |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 299 | .pre_probe = wdt_pre_probe, |
| 300 | .per_device_auto = sizeof(struct wdt_priv), |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 301 | }; |