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> |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 10 | #include <wdt.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | #include <dm/lists.h> |
| 13 | |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 16 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 17 | { |
| 18 | const struct wdt_ops *ops = device_get_ops(dev); |
| 19 | |
| 20 | if (!ops->start) |
| 21 | return -ENOSYS; |
| 22 | |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 23 | return ops->start(dev, timeout_ms, flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | int wdt_stop(struct udevice *dev) |
| 27 | { |
| 28 | const struct wdt_ops *ops = device_get_ops(dev); |
| 29 | |
| 30 | if (!ops->stop) |
| 31 | return -ENOSYS; |
| 32 | |
| 33 | return ops->stop(dev); |
| 34 | } |
| 35 | |
| 36 | int wdt_reset(struct udevice *dev) |
| 37 | { |
| 38 | const struct wdt_ops *ops = device_get_ops(dev); |
| 39 | |
| 40 | if (!ops->reset) |
| 41 | return -ENOSYS; |
| 42 | |
| 43 | return ops->reset(dev); |
| 44 | } |
| 45 | |
| 46 | int wdt_expire_now(struct udevice *dev, ulong flags) |
| 47 | { |
| 48 | int ret = 0; |
| 49 | const struct wdt_ops *ops; |
| 50 | |
Andy Shevchenko | f047be4 | 2017-07-05 20:44:06 +0300 | [diff] [blame] | 51 | debug("WDT Resetting: %lu\n", flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 52 | ops = device_get_ops(dev); |
| 53 | if (ops->expire_now) { |
| 54 | return ops->expire_now(dev, flags); |
| 55 | } else { |
| 56 | if (!ops->start) |
| 57 | return -ENOSYS; |
| 58 | |
| 59 | ret = ops->start(dev, 1, flags); |
| 60 | if (ret < 0) |
| 61 | return ret; |
| 62 | |
| 63 | hang(); |
| 64 | } |
| 65 | |
| 66 | return ret; |
| 67 | } |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 68 | |
| 69 | #if defined(CONFIG_WATCHDOG) |
| 70 | /* |
| 71 | * Called by macro WATCHDOG_RESET. This function be called *very* early, |
| 72 | * so we need to make sure, that the watchdog driver is ready before using |
| 73 | * it in this function. |
| 74 | */ |
| 75 | void watchdog_reset(void) |
| 76 | { |
| 77 | static ulong next_reset; |
| 78 | ulong now; |
| 79 | |
| 80 | /* Exit if GD is not ready or watchdog is not initialized yet */ |
| 81 | if (!gd || !(gd->flags & GD_FLG_WDT_READY)) |
| 82 | return; |
| 83 | |
| 84 | /* Do not reset the watchdog too often */ |
| 85 | now = get_timer(0); |
| 86 | if (now > next_reset) { |
| 87 | next_reset = now + 1000; /* reset every 1000ms */ |
| 88 | wdt_reset(gd->watchdog_dev); |
| 89 | } |
| 90 | } |
| 91 | #endif |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 92 | |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 93 | static int wdt_post_bind(struct udevice *dev) |
| 94 | { |
| 95 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 96 | struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev); |
| 97 | static int reloc_done; |
| 98 | |
| 99 | if (!reloc_done) { |
| 100 | if (ops->start) |
| 101 | ops->start += gd->reloc_off; |
| 102 | if (ops->stop) |
| 103 | ops->stop += gd->reloc_off; |
| 104 | if (ops->reset) |
| 105 | ops->reset += gd->reloc_off; |
| 106 | if (ops->expire_now) |
| 107 | ops->expire_now += gd->reloc_off; |
| 108 | |
| 109 | reloc_done++; |
| 110 | } |
| 111 | #endif |
| 112 | return 0; |
| 113 | } |
| 114 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 115 | UCLASS_DRIVER(wdt) = { |
| 116 | .id = UCLASS_WDT, |
Michal Simek | fc61be3 | 2018-07-11 08:24:43 +0200 | [diff] [blame] | 117 | .name = "watchdog", |
| 118 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Michal Simek | 7b5eca9 | 2018-07-11 15:42:25 +0200 | [diff] [blame] | 119 | .post_bind = wdt_post_bind, |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 120 | }; |