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 | #ifndef _WDT_H_ |
| 7 | #define _WDT_H_ |
| 8 | |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <dm/read.h> |
| 11 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 12 | /* |
| 13 | * Implement a simple watchdog uclass. Watchdog is basically a timer that |
| 14 | * is used to detect or recover from malfunction. During normal operation |
| 15 | * the watchdog would be regularly reset to prevent it from timing out. |
| 16 | * If, due to a hardware fault or program error, the computer fails to reset |
| 17 | * the watchdog, the timer will elapse and generate a timeout signal. |
| 18 | * The timeout signal is used to initiate corrective action or actions, |
| 19 | * which typically include placing the system in a safe, known state. |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | * Start the timer |
| 24 | * |
| 25 | * @dev: WDT Device |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 26 | * @timeout_ms: Number of ticks (milliseconds) before timer expires |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 27 | * @flags: Driver specific flags. This might be used to specify |
| 28 | * which action needs to be executed when the timer expires |
| 29 | * @return: 0 if OK, -ve on error |
| 30 | */ |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 31 | int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Stop the timer, thus disabling the Watchdog. Use wdt_start to start it again. |
| 35 | * |
| 36 | * @dev: WDT Device |
| 37 | * @return: 0 if OK, -ve on error |
| 38 | */ |
| 39 | int wdt_stop(struct udevice *dev); |
| 40 | |
| 41 | /* |
| 42 | * Reset the timer, typically restoring the counter to |
| 43 | * the value configured by start() |
| 44 | * |
| 45 | * @dev: WDT Device |
| 46 | * @return: 0 if OK, -ve on error |
| 47 | */ |
| 48 | int wdt_reset(struct udevice *dev); |
| 49 | |
| 50 | /* |
| 51 | * Expire the timer, thus executing its action immediately. |
| 52 | * This is typically used to reset the board or peripherals. |
| 53 | * |
| 54 | * @dev: WDT Device |
| 55 | * @flags: Driver specific flags |
| 56 | * @return 0 if OK -ve on error. If wdt action is system reset, |
| 57 | * this function may never return. |
| 58 | */ |
| 59 | int wdt_expire_now(struct udevice *dev, ulong flags); |
| 60 | |
| 61 | /* |
| 62 | * struct wdt_ops - Driver model wdt operations |
| 63 | * |
| 64 | * The uclass interface is implemented by all wdt devices which use |
| 65 | * driver model. |
| 66 | */ |
| 67 | struct wdt_ops { |
| 68 | /* |
| 69 | * Start the timer |
| 70 | * |
| 71 | * @dev: WDT Device |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 72 | * @timeout_ms: Number of ticks (milliseconds) before the timer expires |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 73 | * @flags: Driver specific flags. This might be used to specify |
| 74 | * which action needs to be executed when the timer expires |
| 75 | * @return: 0 if OK, -ve on error |
| 76 | */ |
Andy Shevchenko | 2befb4b | 2017-08-04 15:48:28 -0600 | [diff] [blame] | 77 | int (*start)(struct udevice *dev, u64 timeout_ms, ulong flags); |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 78 | /* |
| 79 | * Stop the timer |
| 80 | * |
| 81 | * @dev: WDT Device |
| 82 | * @return: 0 if OK, -ve on error |
| 83 | */ |
| 84 | int (*stop)(struct udevice *dev); |
| 85 | /* |
| 86 | * Reset the timer, typically restoring the counter to |
| 87 | * the value configured by start() |
| 88 | * |
| 89 | * @dev: WDT Device |
| 90 | * @return: 0 if OK, -ve on error |
| 91 | */ |
| 92 | int (*reset)(struct udevice *dev); |
| 93 | /* |
| 94 | * Expire the timer, thus executing the action immediately (optional) |
| 95 | * |
| 96 | * If this function is not provided, a default implementation |
| 97 | * will be used, which sets the counter to 1 |
| 98 | * and waits forever. This is good enough for system level |
| 99 | * reset, where the function is not expected to return, but might not be |
| 100 | * good enough for other use cases. |
| 101 | * |
| 102 | * @dev: WDT Device |
| 103 | * @flags: Driver specific flags |
| 104 | * @return 0 if OK -ve on error. May not return. |
| 105 | */ |
| 106 | int (*expire_now)(struct udevice *dev, ulong flags); |
| 107 | }; |
| 108 | |
Marek Vasut | 55ec91b | 2019-06-09 03:46:21 +0200 | [diff] [blame] | 109 | #if CONFIG_IS_ENABLED(WDT) |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 110 | #ifndef CONFIG_WATCHDOG_TIMEOUT_MSECS |
| 111 | #define CONFIG_WATCHDOG_TIMEOUT_MSECS (60 * 1000) |
| 112 | #endif |
| 113 | #define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000) |
| 114 | |
| 115 | static inline int initr_watchdog(void) |
| 116 | { |
| 117 | u32 timeout = WATCHDOG_TIMEOUT_SECS; |
| 118 | |
| 119 | /* |
| 120 | * Init watchdog: This will call the probe function of the |
| 121 | * watchdog driver, enabling the use of the device |
| 122 | */ |
| 123 | if (uclass_get_device_by_seq(UCLASS_WDT, 0, |
| 124 | (struct udevice **)&gd->watchdog_dev)) { |
| 125 | debug("WDT: Not found by seq!\n"); |
| 126 | if (uclass_get_device(UCLASS_WDT, 0, |
| 127 | (struct udevice **)&gd->watchdog_dev)) { |
| 128 | printf("WDT: Not found!\n"); |
| 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | |
Walter Lozano | eb382db | 2020-01-23 16:05:05 -0300 | [diff] [blame] | 133 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 134 | timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec", |
| 135 | WATCHDOG_TIMEOUT_SECS); |
| 136 | } |
Stefan Roese | 502acb0 | 2019-04-11 15:58:44 +0200 | [diff] [blame] | 137 | wdt_start(gd->watchdog_dev, timeout * 1000, 0); |
| 138 | gd->flags |= GD_FLG_WDT_READY; |
| 139 | printf("WDT: Started with%s servicing (%ds timeout)\n", |
| 140 | IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | #endif |
| 145 | |
maxims@google.com | daea6d4 | 2017-04-17 12:00:21 -0700 | [diff] [blame] | 146 | #endif /* _WDT_H_ */ |