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 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 9 | #include <hang.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Chris Packham | 32a234f | 2020-02-24 13:20:33 +1300 | [diff] [blame] | 11 | #include <time.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 12 | #include <wdt.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/lists.h> |
| 16 | |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 19 | #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000) |
| 20 | |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 21 | /* |
| 22 | * Reset every 1000ms, or however often is required as indicated by a |
| 23 | * hw_margin_ms property. |
| 24 | */ |
| 25 | static ulong reset_period = 1000; |
| 26 | |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 27 | int initr_watchdog(void) |
| 28 | { |
| 29 | u32 timeout = WATCHDOG_TIMEOUT_SECS; |
| 30 | |
| 31 | /* |
| 32 | * Init watchdog: This will call the probe function of the |
| 33 | * watchdog driver, enabling the use of the device |
| 34 | */ |
| 35 | if (uclass_get_device_by_seq(UCLASS_WDT, 0, |
| 36 | (struct udevice **)&gd->watchdog_dev)) { |
| 37 | debug("WDT: Not found by seq!\n"); |
| 38 | if (uclass_get_device(UCLASS_WDT, 0, |
| 39 | (struct udevice **)&gd->watchdog_dev)) { |
| 40 | printf("WDT: Not found!\n"); |
| 41 | return 0; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
| 46 | timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec", |
| 47 | WATCHDOG_TIMEOUT_SECS); |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 48 | reset_period = dev_read_u32_default(gd->watchdog_dev, |
| 49 | "hw_margin_ms", |
| 50 | 4 * reset_period) / 4; |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | wdt_start(gd->watchdog_dev, timeout * 1000, 0); |
Rasmus Villemoes | f7c2e9f | 2020-03-13 17:04:57 +0100 | [diff] [blame] | 54 | printf("WDT: Started with%s servicing (%ds timeout)\n", |
| 55 | IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 60 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 61 | { |
| 62 | const struct wdt_ops *ops = device_get_ops(dev); |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame^] | 63 | int ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 64 | |
| 65 | if (!ops->start) |
| 66 | return -ENOSYS; |
| 67 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame^] | 68 | ret = ops->start(dev, timeout_ms, flags); |
| 69 | if (ret == 0) |
| 70 | gd->flags |= GD_FLG_WDT_READY; |
| 71 | |
| 72 | return ret; |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | int wdt_stop(struct udevice *dev) |
| 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->stop) |
| 81 | return -ENOSYS; |
| 82 | |
Pali Rohár | 8a8a5ad | 2021-03-09 14:26:54 +0100 | [diff] [blame^] | 83 | ret = ops->stop(dev); |
| 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_reset(struct udevice *dev) |
| 91 | { |
| 92 | const struct wdt_ops *ops = device_get_ops(dev); |
| 93 | |
| 94 | if (!ops->reset) |
| 95 | return -ENOSYS; |
| 96 | |
| 97 | return ops->reset(dev); |
| 98 | } |
| 99 | |
| 100 | int wdt_expire_now(struct udevice *dev, ulong flags) |
| 101 | { |
| 102 | int ret = 0; |
| 103 | const struct wdt_ops *ops; |
| 104 | |
Andy Shevchenko | f047be4 | 2017-07-05 20:44:06 +0300 | [diff] [blame] | 105 | debug("WDT Resetting: %lu\n", flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 106 | ops = device_get_ops(dev); |
| 107 | if (ops->expire_now) { |
| 108 | return ops->expire_now(dev, flags); |
| 109 | } else { |
| 110 | if (!ops->start) |
| 111 | return -ENOSYS; |
| 112 | |
| 113 | ret = ops->start(dev, 1, flags); |
| 114 | if (ret < 0) |
| 115 | return ret; |
| 116 | |
| 117 | hang(); |
| 118 | } |
| 119 | |
| 120 | return ret; |
| 121 | } |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 122 | |
| 123 | #if defined(CONFIG_WATCHDOG) |
| 124 | /* |
| 125 | * Called by macro WATCHDOG_RESET. This function be called *very* early, |
| 126 | * so we need to make sure, that the watchdog driver is ready before using |
| 127 | * it in this function. |
| 128 | */ |
| 129 | void watchdog_reset(void) |
| 130 | { |
| 131 | static ulong next_reset; |
| 132 | ulong now; |
| 133 | |
| 134 | /* Exit if GD is not ready or watchdog is not initialized yet */ |
| 135 | if (!gd || !(gd->flags & GD_FLG_WDT_READY)) |
| 136 | return; |
| 137 | |
| 138 | /* Do not reset the watchdog too often */ |
| 139 | now = get_timer(0); |
Chris Packham | 32a234f | 2020-02-24 13:20:33 +1300 | [diff] [blame] | 140 | if (time_after(now, next_reset)) { |
Rasmus Villemoes | 2ba843d | 2020-03-13 17:04:58 +0100 | [diff] [blame] | 141 | next_reset = now + reset_period; |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 142 | wdt_reset(gd->watchdog_dev); |
| 143 | } |
| 144 | } |
| 145 | #endif |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 146 | |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 147 | static int wdt_post_bind(struct udevice *dev) |
| 148 | { |
| 149 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 150 | struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev); |
| 151 | static int reloc_done; |
| 152 | |
| 153 | if (!reloc_done) { |
| 154 | if (ops->start) |
| 155 | ops->start += gd->reloc_off; |
| 156 | if (ops->stop) |
| 157 | ops->stop += gd->reloc_off; |
| 158 | if (ops->reset) |
| 159 | ops->reset += gd->reloc_off; |
| 160 | if (ops->expire_now) |
| 161 | ops->expire_now += gd->reloc_off; |
| 162 | |
| 163 | reloc_done++; |
| 164 | } |
| 165 | #endif |
| 166 | return 0; |
| 167 | } |
| 168 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 169 | UCLASS_DRIVER(wdt) = { |
| 170 | .id = UCLASS_WDT, |
Michal Simek | fc61be3 | 2018-07-11 08:24:43 +0200 | [diff] [blame] | 171 | .name = "watchdog", |
| 172 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 173 | .post_bind = wdt_post_bind, |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 174 | }; |