blob: 0a1f43771c8f0330b109bbad03395d110f709d19 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
maxims@google.com0753bc22017-04-17 12:00:21 -07002/*
3 * Copyright 2017 Google, Inc
maxims@google.com0753bc22017-04-17 12:00:21 -07004 */
5
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_WDT
7
maxims@google.com0753bc22017-04-17 12:00:21 -07008#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glassdb41d652019-12-28 10:45:07 -070011#include <hang.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Chris Packham6d8eae92020-02-24 13:20:33 +130013#include <time.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070014#include <wdt.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070016#include <dm/device-internal.h>
17#include <dm/lists.h>
18
Stefan Roese06985282019-04-11 15:58:44 +020019DECLARE_GLOBAL_DATA_PTR;
20
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010021#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
22
Rasmus Villemoes6d24dc82021-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 Villemoes40d7f3c2020-03-13 17:04:58 +010037
Rasmus Villemoes3eaf6e22021-08-19 11:56:58 +020038static void init_watchdog_dev(struct udevice *dev)
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010039{
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +020040 struct wdt_priv *priv;
Pali Rohár25e20e32021-03-09 14:26:55 +010041 int ret;
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010042
Rasmus Villemoes3eaf6e22021-08-19 11:56:58 +020043 priv = dev_get_uclass_priv(dev);
44
45 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
46 printf("WDT: Not starting %s\n", dev->name);
47 return;
48 }
49
50 ret = wdt_start(dev, priv->timeout * 1000, 0);
51 if (ret != 0) {
52 printf("WDT: Failed to start %s\n", dev->name);
53 return;
54 }
55
56 printf("WDT: Started %s with%s servicing (%ds timeout)\n", dev->name,
57 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
58}
59
60int initr_watchdog(void)
61{
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010062 /*
63 * Init watchdog: This will call the probe function of the
64 * watchdog driver, enabling the use of the device
65 */
66 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
67 (struct udevice **)&gd->watchdog_dev)) {
68 debug("WDT: Not found by seq!\n");
69 if (uclass_get_device(UCLASS_WDT, 0,
70 (struct udevice **)&gd->watchdog_dev)) {
71 printf("WDT: Not found!\n");
72 return 0;
73 }
74 }
Rasmus Villemoes3eaf6e22021-08-19 11:56:58 +020075 init_watchdog_dev(gd->watchdog_dev);
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010076
77 return 0;
78}
79
Andy Shevchenkoffdec302017-08-04 15:48:28 -060080int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.com0753bc22017-04-17 12:00:21 -070081{
82 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +010083 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070084
85 if (!ops->start)
86 return -ENOSYS;
87
Pali Rohár9c44ff12021-03-09 14:26:54 +010088 ret = ops->start(dev, timeout_ms, flags);
89 if (ret == 0)
90 gd->flags |= GD_FLG_WDT_READY;
91
92 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070093}
94
95int wdt_stop(struct udevice *dev)
96{
97 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +010098 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070099
100 if (!ops->stop)
101 return -ENOSYS;
102
Pali Rohár9c44ff12021-03-09 14:26:54 +0100103 ret = ops->stop(dev);
104 if (ret == 0)
105 gd->flags &= ~GD_FLG_WDT_READY;
106
107 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -0700108}
109
110int wdt_reset(struct udevice *dev)
111{
112 const struct wdt_ops *ops = device_get_ops(dev);
113
114 if (!ops->reset)
115 return -ENOSYS;
116
117 return ops->reset(dev);
118}
119
120int wdt_expire_now(struct udevice *dev, ulong flags)
121{
122 int ret = 0;
123 const struct wdt_ops *ops;
124
Andy Shevchenkob71e0c12017-07-05 20:44:06 +0300125 debug("WDT Resetting: %lu\n", flags);
maxims@google.com0753bc22017-04-17 12:00:21 -0700126 ops = device_get_ops(dev);
127 if (ops->expire_now) {
128 return ops->expire_now(dev, flags);
129 } else {
Rasmus Villemoes8967ebb2021-08-19 11:56:55 +0200130 ret = wdt_start(dev, 1, flags);
maxims@google.com0753bc22017-04-17 12:00:21 -0700131
maxims@google.com0753bc22017-04-17 12:00:21 -0700132 if (ret < 0)
133 return ret;
134
135 hang();
136 }
137
138 return ret;
139}
140
Stefan Roese06985282019-04-11 15:58:44 +0200141#if defined(CONFIG_WATCHDOG)
142/*
143 * Called by macro WATCHDOG_RESET. This function be called *very* early,
144 * so we need to make sure, that the watchdog driver is ready before using
145 * it in this function.
146 */
147void watchdog_reset(void)
148{
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200149 struct wdt_priv *priv;
150 struct udevice *dev;
Stefan Roese06985282019-04-11 15:58:44 +0200151 ulong now;
152
153 /* Exit if GD is not ready or watchdog is not initialized yet */
154 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
155 return;
156
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200157 dev = gd->watchdog_dev;
158 priv = dev_get_uclass_priv(dev);
Stefan Roese06985282019-04-11 15:58:44 +0200159 /* Do not reset the watchdog too often */
160 now = get_timer(0);
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200161 if (time_after_eq(now, priv->next_reset)) {
162 priv->next_reset = now + priv->reset_period;
163 wdt_reset(dev);
Stefan Roese06985282019-04-11 15:58:44 +0200164 }
165}
166#endif
167
Michal Simekf238b3f2018-07-11 15:42:25 +0200168static int wdt_post_bind(struct udevice *dev)
169{
170#if defined(CONFIG_NEEDS_MANUAL_RELOC)
171 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
172 static int reloc_done;
173
174 if (!reloc_done) {
175 if (ops->start)
176 ops->start += gd->reloc_off;
177 if (ops->stop)
178 ops->stop += gd->reloc_off;
179 if (ops->reset)
180 ops->reset += gd->reloc_off;
181 if (ops->expire_now)
182 ops->expire_now += gd->reloc_off;
183
184 reloc_done++;
185 }
186#endif
187 return 0;
188}
189
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200190static int wdt_pre_probe(struct udevice *dev)
191{
192 u32 timeout = WATCHDOG_TIMEOUT_SECS;
193 /*
194 * Reset every 1000ms, or however often is required as
195 * indicated by a hw_margin_ms property.
196 */
197 ulong reset_period = 1000;
198 struct wdt_priv *priv;
199
200 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
201 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
202 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
203 4 * reset_period) / 4;
204 }
205 priv = dev_get_uclass_priv(dev);
206 priv->timeout = timeout;
207 priv->reset_period = reset_period;
208 /*
209 * Pretend this device was last reset "long" ago so the first
210 * watchdog_reset will actually call its ->reset method.
211 */
212 priv->next_reset = get_timer(0);
213
214 return 0;
215}
216
maxims@google.com0753bc22017-04-17 12:00:21 -0700217UCLASS_DRIVER(wdt) = {
Rasmus Villemoes068f8ea2021-08-19 11:56:57 +0200218 .id = UCLASS_WDT,
219 .name = "watchdog",
220 .flags = DM_UC_FLAG_SEQ_ALIAS,
221 .post_bind = wdt_post_bind,
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200222 .pre_probe = wdt_pre_probe,
223 .per_device_auto = sizeof(struct wdt_priv),
maxims@google.com0753bc22017-04-17 12:00:21 -0700224};