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