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