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 | |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 8 | #include <cyclic.h> |
Chanho Park | dfd3012 | 2023-12-03 17:30:40 +0900 | [diff] [blame] | 9 | #include <div64.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> |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 20 | #include <linux/kernel.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 21 | |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 24 | #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000) |
| 25 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 26 | struct wdt_priv { |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 27 | /* The udevice owning this wdt_priv. */ |
| 28 | struct udevice *dev; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 29 | /* Timeout, in seconds, to configure this device to. */ |
| 30 | u32 timeout; |
| 31 | /* |
| 32 | * Time, in milliseconds, between calling the device's ->reset() |
Rasmus Villemoes | 4733b8d | 2024-05-28 13:13:20 +0200 | [diff] [blame] | 33 | * method from schedule(). |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 34 | */ |
| 35 | ulong reset_period; |
| 36 | /* |
| 37 | * Next time (as returned by get_timer(0)) to call |
| 38 | * ->reset(). |
| 39 | */ |
| 40 | ulong next_reset; |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 41 | /* Whether watchdog_start() has been called on the device. */ |
| 42 | bool running; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 43 | /* autostart */ |
| 44 | bool autostart; |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 45 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 46 | struct cyclic_info cyclic; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 47 | }; |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 48 | |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 49 | static void wdt_cyclic(struct cyclic_info *c) |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 50 | { |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 51 | struct wdt_priv *priv = container_of(c, struct wdt_priv, cyclic); |
| 52 | struct udevice *dev = priv->dev; |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 53 | |
| 54 | if (!device_active(dev)) |
| 55 | return; |
| 56 | |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 57 | if (!priv->running) |
| 58 | return; |
| 59 | |
| 60 | wdt_reset(dev); |
| 61 | } |
| 62 | |
Rasmus Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 63 | static void init_watchdog_dev(struct udevice *dev) |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 64 | { |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 65 | struct wdt_priv *priv; |
Pali Rohár | c80b821 | 2021-03-09 14:26:55 +0100 | [diff] [blame] | 66 | int ret; |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 67 | |
Rasmus Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 68 | priv = dev_get_uclass_priv(dev); |
| 69 | |
Samuel Holland | 3a8713a | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 70 | if (IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)) { |
| 71 | ret = sysreset_register_wdt(dev); |
| 72 | if (ret) |
| 73 | printf("WDT: Failed to register %s for sysreset\n", |
| 74 | dev->name); |
| 75 | } |
| 76 | |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 77 | if (!priv->autostart) { |
Rasmus Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 78 | printf("WDT: Not starting %s\n", dev->name); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | ret = wdt_start(dev, priv->timeout * 1000, 0); |
| 83 | if (ret != 0) { |
| 84 | printf("WDT: Failed to start %s\n", dev->name); |
| 85 | return; |
| 86 | } |
Rasmus Villemoes | c245318 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int initr_watchdog(void) |
| 90 | { |
Rasmus Villemoes | 3be6a35 | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 91 | struct udevice *dev; |
| 92 | struct uclass *uc; |
| 93 | int ret; |
| 94 | |
| 95 | ret = uclass_get(UCLASS_WDT, &uc); |
| 96 | if (ret) { |
| 97 | log_debug("Error getting UCLASS_WDT: %d\n", ret); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | uclass_foreach_dev(dev, uc) { |
| 102 | ret = device_probe(dev); |
| 103 | if (ret) { |
| 104 | log_debug("Error probing %s: %d\n", dev->name, ret); |
| 105 | continue; |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 106 | } |
Rasmus Villemoes | 3be6a35 | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 107 | init_watchdog_dev(dev); |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 108 | } |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 113 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 114 | { |
| 115 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 116 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 117 | |
| 118 | if (!ops->start) |
| 119 | return -ENOSYS; |
| 120 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 121 | ret = ops->start(dev, timeout_ms, flags); |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 122 | if (ret == 0) { |
| 123 | struct wdt_priv *priv = dev_get_uclass_priv(dev); |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 124 | char str[16]; |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 125 | |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 126 | memset(str, 0, 16); |
| 127 | if (IS_ENABLED(CONFIG_WATCHDOG)) { |
Rasmus Villemoes | 93a82ce | 2024-05-21 10:46:51 +0200 | [diff] [blame] | 128 | if (priv->running) |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 129 | cyclic_unregister(&priv->cyclic); |
Rasmus Villemoes | 93a82ce | 2024-05-21 10:46:51 +0200 | [diff] [blame] | 130 | |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 131 | /* Register the watchdog driver as a cyclic function */ |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 132 | cyclic_register(&priv->cyclic, wdt_cyclic, |
| 133 | priv->reset_period * 1000, |
| 134 | dev->name); |
| 135 | |
| 136 | snprintf(str, 16, "every %ldms", priv->reset_period); |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Rasmus Villemoes | 93a82ce | 2024-05-21 10:46:51 +0200 | [diff] [blame] | 139 | priv->running = true; |
Stefan Roese | cce2472 | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 140 | printf("WDT: Started %s with%s servicing %s (%ds timeout)\n", |
| 141 | dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", |
Chanho Park | dfd3012 | 2023-12-03 17:30:40 +0900 | [diff] [blame] | 142 | str, (u32)lldiv(timeout_ms, 1000)); |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 143 | } |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 144 | |
| 145 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int wdt_stop(struct udevice *dev) |
| 149 | { |
| 150 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 151 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 152 | |
| 153 | if (!ops->stop) |
| 154 | return -ENOSYS; |
| 155 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 156 | ret = ops->stop(dev); |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 157 | if (ret == 0) { |
| 158 | struct wdt_priv *priv = dev_get_uclass_priv(dev); |
| 159 | |
Rasmus Villemoes | 93a82ce | 2024-05-21 10:46:51 +0200 | [diff] [blame] | 160 | if (IS_ENABLED(CONFIG_WATCHDOG) && priv->running) |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 161 | cyclic_unregister(&priv->cyclic); |
Rasmus Villemoes | 93a82ce | 2024-05-21 10:46:51 +0200 | [diff] [blame] | 162 | |
Rasmus Villemoes | 85fb822 | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 163 | priv->running = false; |
| 164 | } |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 165 | |
| 166 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Rasmus Villemoes | beb8c2f | 2021-08-19 11:57:01 +0200 | [diff] [blame] | 169 | int 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.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 194 | int 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 | |
| 204 | int wdt_expire_now(struct udevice *dev, ulong flags) |
| 205 | { |
| 206 | int ret = 0; |
| 207 | const struct wdt_ops *ops; |
| 208 | |
Andy Shevchenko | f047be4 | 2017-07-05 20:44:06 +0300 | [diff] [blame] | 209 | debug("WDT Resetting: %lu\n", flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 210 | ops = device_get_ops(dev); |
| 211 | if (ops->expire_now) { |
| 212 | return ops->expire_now(dev, flags); |
| 213 | } else { |
Rasmus Villemoes | f008c30 | 2021-08-19 11:56:55 +0200 | [diff] [blame] | 214 | ret = wdt_start(dev, 1, flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 215 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 216 | if (ret < 0) |
| 217 | return ret; |
| 218 | |
| 219 | hang(); |
| 220 | } |
| 221 | |
| 222 | return ret; |
| 223 | } |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 224 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 225 | static int wdt_pre_probe(struct udevice *dev) |
| 226 | { |
| 227 | u32 timeout = WATCHDOG_TIMEOUT_SECS; |
| 228 | /* |
| 229 | * Reset every 1000ms, or however often is required as |
| 230 | * indicated by a hw_margin_ms property. |
| 231 | */ |
| 232 | ulong reset_period = 1000; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 233 | bool autostart = IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART); |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 234 | struct wdt_priv *priv; |
| 235 | |
| 236 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 237 | timeout = dev_read_u32_default(dev, "timeout-sec", timeout); |
| 238 | reset_period = dev_read_u32_default(dev, "hw_margin_ms", |
| 239 | 4 * reset_period) / 4; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 240 | if (dev_read_bool(dev, "u-boot,noautostart")) |
| 241 | autostart = false; |
| 242 | else if (dev_read_bool(dev, "u-boot,autostart")) |
| 243 | autostart = true; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 244 | } |
| 245 | priv = dev_get_uclass_priv(dev); |
Rasmus Villemoes | ea36ada | 2024-05-21 10:46:52 +0200 | [diff] [blame] | 246 | priv->dev = dev; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 247 | priv->timeout = timeout; |
| 248 | priv->reset_period = reset_period; |
Rasmus Villemoes | ce66f19 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 249 | priv->autostart = autostart; |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 250 | /* |
| 251 | * Pretend this device was last reset "long" ago so the first |
Rasmus Villemoes | 4733b8d | 2024-05-28 13:13:20 +0200 | [diff] [blame] | 252 | * schedule() will actually call its ->reset method. |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 253 | */ |
| 254 | priv->next_reset = get_timer(0); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 259 | UCLASS_DRIVER(wdt) = { |
Rasmus Villemoes | 1e42d37 | 2021-08-19 11:56:57 +0200 | [diff] [blame] | 260 | .id = UCLASS_WDT, |
| 261 | .name = "watchdog", |
| 262 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 263 | .pre_probe = wdt_pre_probe, |
| 264 | .per_device_auto = sizeof(struct wdt_priv), |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 265 | }; |