blob: b29d21472480ed9ed116b56cfb0ecc303fdf55a3 [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;
36};
Rasmus Villemoes2ba843d2020-03-13 17:04:58 +010037
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010038int initr_watchdog(void)
39{
Rasmus Villemoes635f9242021-08-19 11:56:56 +020040 struct wdt_priv *priv;
Pali Rohárc80b8212021-03-09 14:26:55 +010041 int ret;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010042
43 /*
44 * Init watchdog: This will call the probe function of the
45 * watchdog driver, enabling the use of the device
46 */
47 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
48 (struct udevice **)&gd->watchdog_dev)) {
49 debug("WDT: Not found by seq!\n");
50 if (uclass_get_device(UCLASS_WDT, 0,
51 (struct udevice **)&gd->watchdog_dev)) {
52 printf("WDT: Not found!\n");
53 return 0;
54 }
55 }
Rasmus Villemoes635f9242021-08-19 11:56:56 +020056 priv = dev_get_uclass_priv(gd->watchdog_dev);
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010057
Teresa Remmet4e511842021-07-15 13:26:32 +020058 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
Pali Rohárf698a772021-03-09 14:26:56 +010059 printf("WDT: Not starting\n");
60 return 0;
61 }
62
Rasmus Villemoes635f9242021-08-19 11:56:56 +020063 ret = wdt_start(gd->watchdog_dev, priv->timeout * 1000, 0);
Pali Rohárc80b8212021-03-09 14:26:55 +010064 if (ret != 0) {
65 printf("WDT: Failed to start\n");
66 return 0;
67 }
68
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010069 printf("WDT: Started with%s servicing (%ds timeout)\n",
Rasmus Villemoes635f9242021-08-19 11:56:56 +020070 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010071
72 return 0;
73}
74
Andy Shevchenko2befb4b2017-08-04 15:48:28 -060075int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.comdaea6d42017-04-17 12:00:21 -070076{
77 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010078 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070079
80 if (!ops->start)
81 return -ENOSYS;
82
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010083 ret = ops->start(dev, timeout_ms, flags);
84 if (ret == 0)
85 gd->flags |= GD_FLG_WDT_READY;
86
87 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070088}
89
90int wdt_stop(struct udevice *dev)
91{
92 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010093 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070094
95 if (!ops->stop)
96 return -ENOSYS;
97
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010098 ret = ops->stop(dev);
99 if (ret == 0)
100 gd->flags &= ~GD_FLG_WDT_READY;
101
102 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -0700103}
104
105int wdt_reset(struct udevice *dev)
106{
107 const struct wdt_ops *ops = device_get_ops(dev);
108
109 if (!ops->reset)
110 return -ENOSYS;
111
112 return ops->reset(dev);
113}
114
115int wdt_expire_now(struct udevice *dev, ulong flags)
116{
117 int ret = 0;
118 const struct wdt_ops *ops;
119
Andy Shevchenkof047be42017-07-05 20:44:06 +0300120 debug("WDT Resetting: %lu\n", flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700121 ops = device_get_ops(dev);
122 if (ops->expire_now) {
123 return ops->expire_now(dev, flags);
124 } else {
Rasmus Villemoesf008c302021-08-19 11:56:55 +0200125 ret = wdt_start(dev, 1, flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700126
maxims@google.comdaea6d42017-04-17 12:00:21 -0700127 if (ret < 0)
128 return ret;
129
130 hang();
131 }
132
133 return ret;
134}
Stefan Roese502acb02019-04-11 15:58:44 +0200135
136#if defined(CONFIG_WATCHDOG)
137/*
138 * Called by macro WATCHDOG_RESET. This function be called *very* early,
139 * so we need to make sure, that the watchdog driver is ready before using
140 * it in this function.
141 */
142void watchdog_reset(void)
143{
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200144 struct wdt_priv *priv;
145 struct udevice *dev;
Stefan Roese502acb02019-04-11 15:58:44 +0200146 ulong now;
147
148 /* Exit if GD is not ready or watchdog is not initialized yet */
149 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
150 return;
151
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200152 dev = gd->watchdog_dev;
153 priv = dev_get_uclass_priv(dev);
Stefan Roese502acb02019-04-11 15:58:44 +0200154 /* Do not reset the watchdog too often */
155 now = get_timer(0);
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200156 if (time_after_eq(now, priv->next_reset)) {
157 priv->next_reset = now + priv->reset_period;
158 wdt_reset(dev);
Stefan Roese502acb02019-04-11 15:58:44 +0200159 }
160}
161#endif
maxims@google.comdaea6d42017-04-17 12:00:21 -0700162
Michal Simek7b5eca92018-07-11 15:42:25 +0200163static int wdt_post_bind(struct udevice *dev)
164{
165#if defined(CONFIG_NEEDS_MANUAL_RELOC)
166 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
167 static int reloc_done;
168
169 if (!reloc_done) {
170 if (ops->start)
171 ops->start += gd->reloc_off;
172 if (ops->stop)
173 ops->stop += gd->reloc_off;
174 if (ops->reset)
175 ops->reset += gd->reloc_off;
176 if (ops->expire_now)
177 ops->expire_now += gd->reloc_off;
178
179 reloc_done++;
180 }
181#endif
182 return 0;
183}
184
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200185static int wdt_pre_probe(struct udevice *dev)
186{
187 u32 timeout = WATCHDOG_TIMEOUT_SECS;
188 /*
189 * Reset every 1000ms, or however often is required as
190 * indicated by a hw_margin_ms property.
191 */
192 ulong reset_period = 1000;
193 struct wdt_priv *priv;
194
195 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
196 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
197 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
198 4 * reset_period) / 4;
199 }
200 priv = dev_get_uclass_priv(dev);
201 priv->timeout = timeout;
202 priv->reset_period = reset_period;
203 /*
204 * Pretend this device was last reset "long" ago so the first
205 * watchdog_reset will actually call its ->reset method.
206 */
207 priv->next_reset = get_timer(0);
208
209 return 0;
210}
211
maxims@google.comdaea6d42017-04-17 12:00:21 -0700212UCLASS_DRIVER(wdt) = {
213 .id = UCLASS_WDT,
Michal Simekfc61be32018-07-11 08:24:43 +0200214 .name = "watchdog",
215 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simek7b5eca92018-07-11 15:42:25 +0200216 .post_bind = wdt_post_bind,
Rasmus Villemoes635f9242021-08-19 11:56:56 +0200217 .pre_probe = wdt_pre_probe,
218 .per_device_auto = sizeof(struct wdt_priv),
maxims@google.comdaea6d42017-04-17 12:00:21 -0700219};