blob: 81287c759af6e4fa6ad31729e10293b45c834e16 [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 Villemoesb4d94522020-03-13 17:04:57 +010038int initr_watchdog(void)
39{
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
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 Villemoes6d24dc82021-08-19 11:56:56 +020056 priv = dev_get_uclass_priv(gd->watchdog_dev);
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010057
Teresa Remmet5fc09432021-07-15 13:26:32 +020058 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
Pali Rohár830d29a2021-03-09 14:26:56 +010059 printf("WDT: Not starting\n");
60 return 0;
61 }
62
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +020063 ret = wdt_start(gd->watchdog_dev, priv->timeout * 1000, 0);
Pali Rohár25e20e32021-03-09 14:26:55 +010064 if (ret != 0) {
65 printf("WDT: Failed to start\n");
66 return 0;
67 }
68
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010069 printf("WDT: Started with%s servicing (%ds timeout)\n",
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +020070 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010071
72 return 0;
73}
74
Andy Shevchenkoffdec302017-08-04 15:48:28 -060075int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.com0753bc22017-04-17 12:00:21 -070076{
77 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +010078 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070079
80 if (!ops->start)
81 return -ENOSYS;
82
Pali Rohár9c44ff12021-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.com0753bc22017-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ár9c44ff12021-03-09 14:26:54 +010093 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070094
95 if (!ops->stop)
96 return -ENOSYS;
97
Pali Rohár9c44ff12021-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.com0753bc22017-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 Shevchenkob71e0c12017-07-05 20:44:06 +0300120 debug("WDT Resetting: %lu\n", flags);
maxims@google.com0753bc22017-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 Villemoes8967ebb2021-08-19 11:56:55 +0200125 ret = wdt_start(dev, 1, flags);
maxims@google.com0753bc22017-04-17 12:00:21 -0700126
maxims@google.com0753bc22017-04-17 12:00:21 -0700127 if (ret < 0)
128 return ret;
129
130 hang();
131 }
132
133 return ret;
134}
135
Stefan Roese06985282019-04-11 15:58:44 +0200136#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 Villemoes6d24dc82021-08-19 11:56:56 +0200144 struct wdt_priv *priv;
145 struct udevice *dev;
Stefan Roese06985282019-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 Villemoes6d24dc82021-08-19 11:56:56 +0200152 dev = gd->watchdog_dev;
153 priv = dev_get_uclass_priv(dev);
Stefan Roese06985282019-04-11 15:58:44 +0200154 /* Do not reset the watchdog too often */
155 now = get_timer(0);
Rasmus Villemoes6d24dc82021-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 Roese06985282019-04-11 15:58:44 +0200159 }
160}
161#endif
162
Michal Simekf238b3f2018-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 Villemoes6d24dc82021-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.com0753bc22017-04-17 12:00:21 -0700212UCLASS_DRIVER(wdt) = {
Rasmus Villemoes068f8ea2021-08-19 11:56:57 +0200213 .id = UCLASS_WDT,
214 .name = "watchdog",
215 .flags = DM_UC_FLAG_SEQ_ALIAS,
216 .post_bind = wdt_post_bind,
Rasmus Villemoes6d24dc82021-08-19 11:56:56 +0200217 .pre_probe = wdt_pre_probe,
218 .per_device_auto = sizeof(struct wdt_priv),
maxims@google.com0753bc22017-04-17 12:00:21 -0700219};