blob: 5b1c0df5d6d93189b1550864267b116eaf9aeb35 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
maxims@google.comdaea6d42017-04-17 12:00:21 -07002/*
3 * Copyright 2017 Google, Inc
maxims@google.comdaea6d42017-04-17 12:00:21 -07004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_WDT
7
maxims@google.comdaea6d42017-04-17 12:00:21 -07008#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glassf11478f2019-12-28 10:45:07 -070011#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Chris Packham32a234f2020-02-24 13:20:33 +130013#include <time.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070014#include <wdt.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070016#include <dm/device-internal.h>
17#include <dm/lists.h>
18
Stefan Roese502acb02019-04-11 15:58:44 +020019DECLARE_GLOBAL_DATA_PTR;
20
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010021#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
22
Rasmus Villemoes635f9242021-08-19 11:56:56 +020023struct wdt_priv {
24 /* Timeout, in seconds, to configure this device to. */
25 u32 timeout;
26 /*
27 * Time, in milliseconds, between calling the device's ->reset()
28 * method from watchdog_reset().
29 */
30 ulong reset_period;
31 /*
32 * Next time (as returned by get_timer(0)) to call
33 * ->reset().
34 */
35 ulong next_reset;
Rasmus Villemoes85fb8222021-08-19 11:56:59 +020036 /* Whether watchdog_start() has been called on the device. */
37 bool running;
Rasmus Villemoes635f9242021-08-19 11:56:56 +020038};
Rasmus Villemoes2ba843d2020-03-13 17:04:58 +010039
Rasmus Villemoesc2453182021-08-19 11:56:58 +020040static void init_watchdog_dev(struct udevice *dev)
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010041{
Rasmus Villemoes635f9242021-08-19 11:56:56 +020042 struct wdt_priv *priv;
Pali Rohárc80b8212021-03-09 14:26:55 +010043 int ret;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010044
Rasmus Villemoesc2453182021-08-19 11:56:58 +020045 priv = dev_get_uclass_priv(dev);
46
47 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
48 printf("WDT: Not starting %s\n", dev->name);
49 return;
50 }
51
52 ret = wdt_start(dev, priv->timeout * 1000, 0);
53 if (ret != 0) {
54 printf("WDT: Failed to start %s\n", dev->name);
55 return;
56 }
57
58 printf("WDT: Started %s with%s servicing (%ds timeout)\n", dev->name,
59 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
60}
61
62int initr_watchdog(void)
63{
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010064 /*
65 * Init watchdog: This will call the probe function of the
66 * watchdog driver, enabling the use of the device
67 */
68 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
69 (struct udevice **)&gd->watchdog_dev)) {
70 debug("WDT: Not found by seq!\n");
71 if (uclass_get_device(UCLASS_WDT, 0,
72 (struct udevice **)&gd->watchdog_dev)) {
73 printf("WDT: Not found!\n");
74 return 0;
75 }
76 }
Rasmus Villemoesc2453182021-08-19 11:56:58 +020077 init_watchdog_dev(gd->watchdog_dev);
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010078
Rasmus Villemoes85fb8222021-08-19 11:56:59 +020079 gd->flags |= GD_FLG_WDT_READY;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010080 return 0;
81}
82
Andy Shevchenko2befb4b2017-08-04 15:48:28 -060083int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.comdaea6d42017-04-17 12:00:21 -070084{
85 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010086 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070087
88 if (!ops->start)
89 return -ENOSYS;
90
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010091 ret = ops->start(dev, timeout_ms, flags);
Rasmus Villemoes85fb8222021-08-19 11:56:59 +020092 if (ret == 0) {
93 struct wdt_priv *priv = dev_get_uclass_priv(dev);
94
95 priv->running = true;
96 }
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010097
98 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070099}
100
101int wdt_stop(struct udevice *dev)
102{
103 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100104 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -0700105
106 if (!ops->stop)
107 return -ENOSYS;
108
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100109 ret = ops->stop(dev);
Rasmus Villemoes85fb8222021-08-19 11:56:59 +0200110 if (ret == 0) {
111 struct wdt_priv *priv = dev_get_uclass_priv(dev);
112
113 priv->running = false;
114 }
Pali Rohár8a8a5ad2021-03-09 14:26:54 +0100115
116 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -0700117}
118
Rasmus Villemoesbeb8c2f2021-08-19 11:57:01 +0200119int wdt_stop_all(void)
120{
121 struct wdt_priv *priv;
122 struct udevice *dev;
123 struct uclass *uc;
124 int ret, err;
125
126 ret = uclass_get(UCLASS_WDT, &uc);
127 if (ret)
128 return ret;
129
130 uclass_foreach_dev(dev, uc) {
131 if (!device_active(dev))
132 continue;
133 priv = dev_get_uclass_priv(dev);
134 if (!priv->running)
135 continue;
136 err = wdt_stop(dev);
137 if (!ret)
138 ret = err;
139 }
140
141 return ret;
142}
143
maxims@google.comdaea6d42017-04-17 12:00:21 -0700144int wdt_reset(struct udevice *dev)
145{
146 const struct wdt_ops *ops = device_get_ops(dev);
147
148 if (!ops->reset)
149 return -ENOSYS;
150
151 return ops->reset(dev);
152}
153
154int wdt_expire_now(struct udevice *dev, ulong flags)
155{
156 int ret = 0;
157 const struct wdt_ops *ops;
158
Andy Shevchenkof047be42017-07-05 20:44:06 +0300159 debug("WDT Resetting: %lu\n", flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700160 ops = device_get_ops(dev);
161 if (ops->expire_now) {
162 return ops->expire_now(dev, flags);
163 } else {
Rasmus Villemoesf008c302021-08-19 11:56:55 +0200164 ret = wdt_start(dev, 1, flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700165
maxims@google.comdaea6d42017-04-17 12:00:21 -0700166 if (ret < 0)
167 return ret;
168
169 hang();
170 }
171
172 return ret;
173}
Stefan Roese502acb02019-04-11 15:58:44 +0200174
175#if defined(CONFIG_WATCHDOG)
176/*
177 * Called by macro WATCHDOG_RESET. This function be called *very* early,
178 * so we need to make sure, that the watchdog driver is ready before using
179 * it in this function.
180 */
181void watchdog_reset(void)
182{
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200183 struct wdt_priv *priv;
184 struct udevice *dev;
Stefan Roese502acb02019-04-11 15:58:44 +0200185 ulong now;
186
187 /* Exit if GD is not ready or watchdog is not initialized yet */
188 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
189 return;
190
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200191 dev = gd->watchdog_dev;
192 priv = dev_get_uclass_priv(dev);
Rasmus Villemoes85fb8222021-08-19 11:56:59 +0200193 if (!priv->running)
194 return;
195
Stefan Roese502acb02019-04-11 15:58:44 +0200196 /* Do not reset the watchdog too often */
197 now = get_timer(0);
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200198 if (time_after_eq(now, priv->next_reset)) {
199 priv->next_reset = now + priv->reset_period;
200 wdt_reset(dev);
Stefan Roese502acb02019-04-11 15:58:44 +0200201 }
202}
203#endif
maxims@google.comdaea6d42017-04-17 12:00:21 -0700204
Michal Simek7b5eca92018-07-11 15:42:25 +0200205static int wdt_post_bind(struct udevice *dev)
206{
207#if defined(CONFIG_NEEDS_MANUAL_RELOC)
208 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
209 static int reloc_done;
210
211 if (!reloc_done) {
212 if (ops->start)
213 ops->start += gd->reloc_off;
214 if (ops->stop)
215 ops->stop += gd->reloc_off;
216 if (ops->reset)
217 ops->reset += gd->reloc_off;
218 if (ops->expire_now)
219 ops->expire_now += gd->reloc_off;
220
221 reloc_done++;
222 }
223#endif
224 return 0;
225}
226
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200227static int wdt_pre_probe(struct udevice *dev)
228{
229 u32 timeout = WATCHDOG_TIMEOUT_SECS;
230 /*
231 * Reset every 1000ms, or however often is required as
232 * indicated by a hw_margin_ms property.
233 */
234 ulong reset_period = 1000;
235 struct wdt_priv *priv;
236
237 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
238 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
239 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
240 4 * reset_period) / 4;
241 }
242 priv = dev_get_uclass_priv(dev);
243 priv->timeout = timeout;
244 priv->reset_period = reset_period;
245 /*
246 * Pretend this device was last reset "long" ago so the first
247 * watchdog_reset will actually call its ->reset method.
248 */
249 priv->next_reset = get_timer(0);
250
251 return 0;
252}
253
maxims@google.comdaea6d42017-04-17 12:00:21 -0700254UCLASS_DRIVER(wdt) = {
Rasmus Villemoes1e42d372021-08-19 11:56:57 +0200255 .id = UCLASS_WDT,
256 .name = "watchdog",
257 .flags = DM_UC_FLAG_SEQ_ALIAS,
258 .post_bind = wdt_post_bind,
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200259 .pre_probe = wdt_pre_probe,
260 .per_device_auto = sizeof(struct wdt_priv),
maxims@google.comdaea6d42017-04-17 12:00:21 -0700261};