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> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 11 | #include <hang.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Chris Packham | 32a234f | 2020-02-24 13:20:33 +1300 | [diff] [blame] | 13 | #include <time.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 14 | #include <wdt.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/lists.h> |
| 18 | |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 21 | #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000) |
| 22 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 23 | struct wdt_priv { |
| 24 | /* Timeout, in seconds, to configure this device to. */ |
| 25 | u32 timeout; |
| 26 | /* |
| 27 | * Time, in milliseconds, between calling the device's ->reset() |
| 28 | * method from watchdog_reset(). |
| 29 | */ |
| 30 | ulong reset_period; |
| 31 | /* |
| 32 | * Next time (as returned by get_timer(0)) to call |
| 33 | * ->reset(). |
| 34 | */ |
| 35 | ulong next_reset; |
| 36 | }; |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 37 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 38 | int initr_watchdog(void) |
| 39 | { |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 40 | struct wdt_priv *priv; |
Pali Rohár | c80b821 | 2021-03-09 14:26:55 +0100 | [diff] [blame] | 41 | int ret; |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Init watchdog: This will call the probe function of the |
| 45 | * watchdog driver, enabling the use of the device |
| 46 | */ |
| 47 | if (uclass_get_device_by_seq(UCLASS_WDT, 0, |
| 48 | (struct udevice **)&gd->watchdog_dev)) { |
| 49 | debug("WDT: Not found by seq!\n"); |
| 50 | if (uclass_get_device(UCLASS_WDT, 0, |
| 51 | (struct udevice **)&gd->watchdog_dev)) { |
| 52 | printf("WDT: Not found!\n"); |
| 53 | return 0; |
| 54 | } |
| 55 | } |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 56 | priv = dev_get_uclass_priv(gd->watchdog_dev); |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 57 | |
Teresa Remmet | 4e51184 | 2021-07-15 13:26:32 +0200 | [diff] [blame] | 58 | if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) { |
Pali Rohár | f698a77 | 2021-03-09 14:26:56 +0100 | [diff] [blame] | 59 | printf("WDT: Not starting\n"); |
| 60 | return 0; |
| 61 | } |
| 62 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 63 | ret = wdt_start(gd->watchdog_dev, priv->timeout * 1000, 0); |
Pali Rohár | c80b821 | 2021-03-09 14:26:55 +0100 | [diff] [blame] | 64 | if (ret != 0) { |
| 65 | printf("WDT: Failed to start\n"); |
| 66 | return 0; |
| 67 | } |
| 68 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 69 | printf("WDT: Started with%s servicing (%ds timeout)\n", |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 70 | IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout); |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 75 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 76 | { |
| 77 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 78 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 79 | |
| 80 | if (!ops->start) |
| 81 | return -ENOSYS; |
| 82 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 83 | ret = ops->start(dev, timeout_ms, flags); |
| 84 | if (ret == 0) |
| 85 | gd->flags |= GD_FLG_WDT_READY; |
| 86 | |
| 87 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int wdt_stop(struct udevice *dev) |
| 91 | { |
| 92 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 93 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 94 | |
| 95 | if (!ops->stop) |
| 96 | return -ENOSYS; |
| 97 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 98 | ret = ops->stop(dev); |
| 99 | if (ret == 0) |
| 100 | gd->flags &= ~GD_FLG_WDT_READY; |
| 101 | |
| 102 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int wdt_reset(struct udevice *dev) |
| 106 | { |
| 107 | const struct wdt_ops *ops = device_get_ops(dev); |
| 108 | |
| 109 | if (!ops->reset) |
| 110 | return -ENOSYS; |
| 111 | |
| 112 | return ops->reset(dev); |
| 113 | } |
| 114 | |
| 115 | int wdt_expire_now(struct udevice *dev, ulong flags) |
| 116 | { |
| 117 | int ret = 0; |
| 118 | const struct wdt_ops *ops; |
| 119 | |
Andy Shevchenko | f047be4 | 2017-07-05 20:44:06 +0300 | [diff] [blame] | 120 | debug("WDT Resetting: %lu\n", flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 121 | ops = device_get_ops(dev); |
| 122 | if (ops->expire_now) { |
| 123 | return ops->expire_now(dev, flags); |
| 124 | } else { |
Rasmus Villemoes | f008c30 | 2021-08-19 11:56:55 +0200 | [diff] [blame] | 125 | ret = wdt_start(dev, 1, flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 126 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 127 | if (ret < 0) |
| 128 | return ret; |
| 129 | |
| 130 | hang(); |
| 131 | } |
| 132 | |
| 133 | return ret; |
| 134 | } |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 135 | |
| 136 | #if defined(CONFIG_WATCHDOG) |
| 137 | /* |
| 138 | * Called by macro WATCHDOG_RESET. This function be called *very* early, |
| 139 | * so we need to make sure, that the watchdog driver is ready before using |
| 140 | * it in this function. |
| 141 | */ |
| 142 | void watchdog_reset(void) |
| 143 | { |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 144 | struct wdt_priv *priv; |
| 145 | struct udevice *dev; |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 146 | ulong now; |
| 147 | |
| 148 | /* Exit if GD is not ready or watchdog is not initialized yet */ |
| 149 | if (!gd || !(gd->flags & GD_FLG_WDT_READY)) |
| 150 | return; |
| 151 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 152 | dev = gd->watchdog_dev; |
| 153 | priv = dev_get_uclass_priv(dev); |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 154 | /* Do not reset the watchdog too often */ |
| 155 | now = get_timer(0); |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 156 | if (time_after_eq(now, priv->next_reset)) { |
| 157 | priv->next_reset = now + priv->reset_period; |
| 158 | wdt_reset(dev); |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | #endif |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 162 | |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 163 | static int wdt_post_bind(struct udevice *dev) |
| 164 | { |
| 165 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 166 | struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev); |
| 167 | static int reloc_done; |
| 168 | |
| 169 | if (!reloc_done) { |
| 170 | if (ops->start) |
| 171 | ops->start += gd->reloc_off; |
| 172 | if (ops->stop) |
| 173 | ops->stop += gd->reloc_off; |
| 174 | if (ops->reset) |
| 175 | ops->reset += gd->reloc_off; |
| 176 | if (ops->expire_now) |
| 177 | ops->expire_now += gd->reloc_off; |
| 178 | |
| 179 | reloc_done++; |
| 180 | } |
| 181 | #endif |
| 182 | return 0; |
| 183 | } |
| 184 | |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 185 | static int wdt_pre_probe(struct udevice *dev) |
| 186 | { |
| 187 | u32 timeout = WATCHDOG_TIMEOUT_SECS; |
| 188 | /* |
| 189 | * Reset every 1000ms, or however often is required as |
| 190 | * indicated by a hw_margin_ms property. |
| 191 | */ |
| 192 | ulong reset_period = 1000; |
| 193 | struct wdt_priv *priv; |
| 194 | |
| 195 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 196 | timeout = dev_read_u32_default(dev, "timeout-sec", timeout); |
| 197 | reset_period = dev_read_u32_default(dev, "hw_margin_ms", |
| 198 | 4 * reset_period) / 4; |
| 199 | } |
| 200 | priv = dev_get_uclass_priv(dev); |
| 201 | priv->timeout = timeout; |
| 202 | priv->reset_period = reset_period; |
| 203 | /* |
| 204 | * Pretend this device was last reset "long" ago so the first |
| 205 | * watchdog_reset will actually call its ->reset method. |
| 206 | */ |
| 207 | priv->next_reset = get_timer(0); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 212 | UCLASS_DRIVER(wdt) = { |
| 213 | .id = UCLASS_WDT, |
Michal Simek | fc61be3 | 2018-07-11 08:24:43 +0200 | [diff] [blame] | 214 | .name = "watchdog", |
| 215 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 216 | .post_bind = wdt_post_bind, |
Rasmus Villemoes | 635f924 | 2021-08-19 11:56:56 +0200 | [diff] [blame^] | 217 | .pre_probe = wdt_pre_probe, |
| 218 | .per_device_auto = sizeof(struct wdt_priv), |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 219 | }; |