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