Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017 Google, Inc |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_WDT |
| 7 | |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 8 | #include <common.h> |
Stefan Roese | c2fd0ca | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 9 | #include <cyclic.h> |
Chanho Park | a341a0e | 2023-12-03 17:30:40 +0900 | [diff] [blame] | 10 | #include <div64.h> |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <errno.h> |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 13 | #include <hang.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Samuel Holland | a8f63d1 | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 15 | #include <sysreset.h> |
Chris Packham | 6d8eae9 | 2020-02-24 13:20:33 +1300 | [diff] [blame] | 16 | #include <time.h> |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 17 | #include <wdt.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 19 | #include <dm/device-internal.h> |
| 20 | #include <dm/lists.h> |
| 21 | |
Stefan Roese | 0698528 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Rasmus Villemoes | b4d9452 | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 24 | #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000) |
| 25 | |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 26 | struct wdt_priv { |
| 27 | /* Timeout, in seconds, to configure this device to. */ |
| 28 | u32 timeout; |
| 29 | /* |
| 30 | * Time, in milliseconds, between calling the device's ->reset() |
| 31 | * method from watchdog_reset(). |
| 32 | */ |
| 33 | ulong reset_period; |
| 34 | /* |
| 35 | * Next time (as returned by get_timer(0)) to call |
| 36 | * ->reset(). |
| 37 | */ |
| 38 | ulong next_reset; |
Rasmus Villemoes | f1b112a | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 39 | /* Whether watchdog_start() has been called on the device. */ |
| 40 | bool running; |
Rasmus Villemoes | 2783670 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 41 | /* autostart */ |
| 42 | bool autostart; |
Stefan Roese | c2fd0ca | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 43 | |
| 44 | struct cyclic_info *cyclic; |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 45 | }; |
Rasmus Villemoes | 40d7f3c | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 46 | |
Stefan Roese | c2fd0ca | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 47 | static void wdt_cyclic(void *ctx) |
| 48 | { |
| 49 | struct udevice *dev = ctx; |
| 50 | struct wdt_priv *priv; |
| 51 | |
| 52 | if (!device_active(dev)) |
| 53 | return; |
| 54 | |
| 55 | priv = dev_get_uclass_priv(dev); |
| 56 | if (!priv->running) |
| 57 | return; |
| 58 | |
| 59 | wdt_reset(dev); |
| 60 | } |
| 61 | |
Rasmus Villemoes | 3eaf6e2 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 62 | static void init_watchdog_dev(struct udevice *dev) |
Rasmus Villemoes | b4d9452 | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 63 | { |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 64 | struct wdt_priv *priv; |
Pali Rohár | 25e20e3 | 2021-03-09 14:26:55 +0100 | [diff] [blame] | 65 | int ret; |
Rasmus Villemoes | b4d9452 | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 66 | |
Rasmus Villemoes | 3eaf6e2 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 67 | priv = dev_get_uclass_priv(dev); |
| 68 | |
Samuel Holland | a8f63d1 | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 69 | if (IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO)) { |
| 70 | ret = sysreset_register_wdt(dev); |
| 71 | if (ret) |
| 72 | printf("WDT: Failed to register %s for sysreset\n", |
| 73 | dev->name); |
| 74 | } |
| 75 | |
Rasmus Villemoes | 2783670 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 76 | if (!priv->autostart) { |
Rasmus Villemoes | 3eaf6e2 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 77 | printf("WDT: Not starting %s\n", dev->name); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | ret = wdt_start(dev, priv->timeout * 1000, 0); |
| 82 | if (ret != 0) { |
| 83 | printf("WDT: Failed to start %s\n", dev->name); |
| 84 | return; |
| 85 | } |
Rasmus Villemoes | 3eaf6e2 | 2021-08-19 11:56:58 +0200 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int initr_watchdog(void) |
| 89 | { |
Rasmus Villemoes | 492ee6b | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 90 | struct udevice *dev; |
| 91 | struct uclass *uc; |
| 92 | int ret; |
| 93 | |
| 94 | ret = uclass_get(UCLASS_WDT, &uc); |
| 95 | if (ret) { |
| 96 | log_debug("Error getting UCLASS_WDT: %d\n", ret); |
| 97 | return 0; |
Rasmus Villemoes | b4d9452 | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 98 | } |
Rasmus Villemoes | 492ee6b | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 99 | |
| 100 | uclass_foreach_dev(dev, uc) { |
| 101 | ret = device_probe(dev); |
| 102 | if (ret) { |
| 103 | log_debug("Error probing %s: %d\n", dev->name, ret); |
| 104 | continue; |
| 105 | } |
| 106 | init_watchdog_dev(dev); |
| 107 | } |
Rasmus Villemoes | b4d9452 | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Andy Shevchenko | ffdec30 | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 112 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 113 | { |
| 114 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 9c44ff1 | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 115 | int ret; |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 116 | |
| 117 | if (!ops->start) |
| 118 | return -ENOSYS; |
| 119 | |
Pali Rohár | 9c44ff1 | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 120 | ret = ops->start(dev, timeout_ms, flags); |
Rasmus Villemoes | f1b112a | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 121 | if (ret == 0) { |
| 122 | struct wdt_priv *priv = dev_get_uclass_priv(dev); |
Stefan Roese | c2fd0ca | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 123 | char str[16]; |
Rasmus Villemoes | f1b112a | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 124 | |
| 125 | priv->running = true; |
Stefan Roese | c2fd0ca | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 126 | |
| 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", |
Chanho Park | a341a0e | 2023-12-03 17:30:40 +0900 | [diff] [blame] | 145 | str, (u32)lldiv(timeout_ms, 1000)); |
Rasmus Villemoes | f1b112a | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 146 | } |
Pali Rohár | 9c44ff1 | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 147 | |
| 148 | return ret; |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int wdt_stop(struct udevice *dev) |
| 152 | { |
| 153 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 9c44ff1 | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 154 | int ret; |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 155 | |
| 156 | if (!ops->stop) |
| 157 | return -ENOSYS; |
| 158 | |
Pali Rohár | 9c44ff1 | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 159 | ret = ops->stop(dev); |
Rasmus Villemoes | f1b112a | 2021-08-19 11:56:59 +0200 | [diff] [blame] | 160 | if (ret == 0) { |
| 161 | struct wdt_priv *priv = dev_get_uclass_priv(dev); |
| 162 | |
| 163 | priv->running = false; |
| 164 | } |
Pali Rohár | 9c44ff1 | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 165 | |
| 166 | return ret; |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Rasmus Villemoes | 90555dc | 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 | 0753bc2 | 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 | b71e0c1 | 2017-07-05 20:44:06 +0300 | [diff] [blame] | 209 | debug("WDT Resetting: %lu\n", flags); |
maxims@google.com | 0753bc2 | 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 | 8967ebb | 2021-08-19 11:56:55 +0200 | [diff] [blame] | 214 | ret = wdt_start(dev, 1, flags); |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 215 | |
maxims@google.com | 0753bc2 | 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 | } |
| 224 | |
Stefan Roese | 0698528 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 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 | */ |
| 231 | void watchdog_reset(void) |
| 232 | { |
Rasmus Villemoes | 492ee6b | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 233 | /* |
Stefan Roese | c2fd0ca | 2022-08-18 13:22:46 +0200 | [diff] [blame] | 234 | * Empty function for now. The actual WDT handling is now done in |
| 235 | * the cyclic function instead. |
Rasmus Villemoes | 492ee6b | 2021-08-19 11:57:03 +0200 | [diff] [blame] | 236 | */ |
Stefan Roese | 0698528 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 237 | } |
| 238 | #endif |
| 239 | |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 240 | static int wdt_pre_probe(struct udevice *dev) |
| 241 | { |
| 242 | u32 timeout = WATCHDOG_TIMEOUT_SECS; |
| 243 | /* |
| 244 | * Reset every 1000ms, or however often is required as |
| 245 | * indicated by a hw_margin_ms property. |
| 246 | */ |
| 247 | ulong reset_period = 1000; |
Rasmus Villemoes | 2783670 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 248 | bool autostart = IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART); |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 249 | struct wdt_priv *priv; |
| 250 | |
| 251 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 252 | timeout = dev_read_u32_default(dev, "timeout-sec", timeout); |
| 253 | reset_period = dev_read_u32_default(dev, "hw_margin_ms", |
| 254 | 4 * reset_period) / 4; |
Rasmus Villemoes | 2783670 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 255 | if (dev_read_bool(dev, "u-boot,noautostart")) |
| 256 | autostart = false; |
| 257 | else if (dev_read_bool(dev, "u-boot,autostart")) |
| 258 | autostart = true; |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 259 | } |
| 260 | priv = dev_get_uclass_priv(dev); |
| 261 | priv->timeout = timeout; |
| 262 | priv->reset_period = reset_period; |
Rasmus Villemoes | 2783670 | 2022-09-27 11:54:03 +0200 | [diff] [blame] | 263 | priv->autostart = autostart; |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 264 | /* |
| 265 | * Pretend this device was last reset "long" ago so the first |
| 266 | * watchdog_reset will actually call its ->reset method. |
| 267 | */ |
| 268 | priv->next_reset = get_timer(0); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 273 | UCLASS_DRIVER(wdt) = { |
Rasmus Villemoes | 068f8ea | 2021-08-19 11:56:57 +0200 | [diff] [blame] | 274 | .id = UCLASS_WDT, |
| 275 | .name = "watchdog", |
| 276 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Rasmus Villemoes | 6d24dc8 | 2021-08-19 11:56:56 +0200 | [diff] [blame] | 277 | .pre_probe = wdt_pre_probe, |
| 278 | .per_device_auto = sizeof(struct wdt_priv), |
maxims@google.com | 0753bc2 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 279 | }; |