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 | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 23 | /* |
| 24 | * Reset every 1000ms, or however often is required as indicated by a |
| 25 | * hw_margin_ms property. |
| 26 | */ |
| 27 | static ulong reset_period = 1000; |
| 28 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 29 | int initr_watchdog(void) |
| 30 | { |
| 31 | u32 timeout = WATCHDOG_TIMEOUT_SECS; |
Pali Rohár | c80b821 | 2021-03-09 14:26:55 +0100 | [diff] [blame] | 32 | int ret; |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Init watchdog: This will call the probe function of the |
| 36 | * watchdog driver, enabling the use of the device |
| 37 | */ |
| 38 | if (uclass_get_device_by_seq(UCLASS_WDT, 0, |
| 39 | (struct udevice **)&gd->watchdog_dev)) { |
| 40 | debug("WDT: Not found by seq!\n"); |
| 41 | if (uclass_get_device(UCLASS_WDT, 0, |
| 42 | (struct udevice **)&gd->watchdog_dev)) { |
| 43 | printf("WDT: Not found!\n"); |
| 44 | return 0; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 49 | timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec", |
| 50 | WATCHDOG_TIMEOUT_SECS); |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 51 | reset_period = dev_read_u32_default(gd->watchdog_dev, |
| 52 | "hw_margin_ms", |
| 53 | 4 * reset_period) / 4; |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Teresa Remmet | 4e51184 | 2021-07-15 13:26:32 +0200 | [diff] [blame] | 56 | if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) { |
Pali Rohár | f698a77 | 2021-03-09 14:26:56 +0100 | [diff] [blame] | 57 | printf("WDT: Not starting\n"); |
| 58 | return 0; |
| 59 | } |
| 60 | |
Pali Rohár | c80b821 | 2021-03-09 14:26:55 +0100 | [diff] [blame] | 61 | ret = wdt_start(gd->watchdog_dev, timeout * 1000, 0); |
| 62 | if (ret != 0) { |
| 63 | printf("WDT: Failed to start\n"); |
| 64 | return 0; |
| 65 | } |
| 66 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 67 | printf("WDT: Started with%s servicing (%ds timeout)\n", |
| 68 | IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 73 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 74 | { |
| 75 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 76 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 77 | |
| 78 | if (!ops->start) |
| 79 | return -ENOSYS; |
| 80 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 81 | ret = ops->start(dev, timeout_ms, flags); |
| 82 | if (ret == 0) |
| 83 | gd->flags |= GD_FLG_WDT_READY; |
| 84 | |
| 85 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int wdt_stop(struct udevice *dev) |
| 89 | { |
| 90 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 91 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 92 | |
| 93 | if (!ops->stop) |
| 94 | return -ENOSYS; |
| 95 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame] | 96 | ret = ops->stop(dev); |
| 97 | if (ret == 0) |
| 98 | gd->flags &= ~GD_FLG_WDT_READY; |
| 99 | |
| 100 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | int wdt_reset(struct udevice *dev) |
| 104 | { |
| 105 | const struct wdt_ops *ops = device_get_ops(dev); |
| 106 | |
| 107 | if (!ops->reset) |
| 108 | return -ENOSYS; |
| 109 | |
| 110 | return ops->reset(dev); |
| 111 | } |
| 112 | |
| 113 | int wdt_expire_now(struct udevice *dev, ulong flags) |
| 114 | { |
| 115 | int ret = 0; |
| 116 | const struct wdt_ops *ops; |
| 117 | |
Andy Shevchenko | f047be4 | 2017-07-05 20:44:06 +0300 | [diff] [blame] | 118 | debug("WDT Resetting: %lu\n", flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 119 | ops = device_get_ops(dev); |
| 120 | if (ops->expire_now) { |
| 121 | return ops->expire_now(dev, flags); |
| 122 | } else { |
| 123 | if (!ops->start) |
| 124 | return -ENOSYS; |
| 125 | |
| 126 | ret = ops->start(dev, 1, flags); |
| 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 | { |
| 144 | static ulong next_reset; |
| 145 | ulong now; |
| 146 | |
| 147 | /* Exit if GD is not ready or watchdog is not initialized yet */ |
| 148 | if (!gd || !(gd->flags & GD_FLG_WDT_READY)) |
| 149 | return; |
| 150 | |
| 151 | /* Do not reset the watchdog too often */ |
| 152 | now = get_timer(0); |
Rasmus Villemoes | 739e900 | 2021-04-13 16:43:20 +0200 | [diff] [blame] | 153 | if (time_after_eq(now, next_reset)) { |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 154 | next_reset = now + reset_period; |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 155 | wdt_reset(gd->watchdog_dev); |
| 156 | } |
| 157 | } |
| 158 | #endif |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 159 | |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 160 | static int wdt_post_bind(struct udevice *dev) |
| 161 | { |
| 162 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 163 | struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev); |
| 164 | static int reloc_done; |
| 165 | |
| 166 | if (!reloc_done) { |
| 167 | if (ops->start) |
| 168 | ops->start += gd->reloc_off; |
| 169 | if (ops->stop) |
| 170 | ops->stop += gd->reloc_off; |
| 171 | if (ops->reset) |
| 172 | ops->reset += gd->reloc_off; |
| 173 | if (ops->expire_now) |
| 174 | ops->expire_now += gd->reloc_off; |
| 175 | |
| 176 | reloc_done++; |
| 177 | } |
| 178 | #endif |
| 179 | return 0; |
| 180 | } |
| 181 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 182 | UCLASS_DRIVER(wdt) = { |
| 183 | .id = UCLASS_WDT, |
Michal Simek | fc61be3 | 2018-07-11 08:24:43 +0200 | [diff] [blame] | 184 | .name = "watchdog", |
| 185 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 186 | .post_bind = wdt_post_bind, |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 187 | }; |