blob: a0c2429e5a43d7a73a4aecf9f868b66acc7edb87 [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 Villemoes40d7f3c2020-03-13 17:04:58 +010023/*
24 * Reset every 1000ms, or however often is required as indicated by a
25 * hw_margin_ms property.
26 */
27static ulong reset_period = 1000;
28
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010029int initr_watchdog(void)
30{
31 u32 timeout = WATCHDOG_TIMEOUT_SECS;
Pali Rohár25e20e32021-03-09 14:26:55 +010032 int ret;
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010033
34 /*
35 * Init watchdog: This will call the probe function of the
36 * watchdog driver, enabling the use of the device
37 */
38 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
39 (struct udevice **)&gd->watchdog_dev)) {
40 debug("WDT: Not found by seq!\n");
41 if (uclass_get_device(UCLASS_WDT, 0,
42 (struct udevice **)&gd->watchdog_dev)) {
43 printf("WDT: Not found!\n");
44 return 0;
45 }
46 }
47
48 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
49 timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
50 WATCHDOG_TIMEOUT_SECS);
Rasmus Villemoes40d7f3c2020-03-13 17:04:58 +010051 reset_period = dev_read_u32_default(gd->watchdog_dev,
52 "hw_margin_ms",
53 4 * reset_period) / 4;
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010054 }
55
Pali Rohár830d29a2021-03-09 14:26:56 +010056 if (!CONFIG_IS_ENABLED(WATCHDOG_AUTOSTART)) {
57 printf("WDT: Not starting\n");
58 return 0;
59 }
60
Pali Rohár25e20e32021-03-09 14:26:55 +010061 ret = wdt_start(gd->watchdog_dev, timeout * 1000, 0);
62 if (ret != 0) {
63 printf("WDT: Failed to start\n");
64 return 0;
65 }
66
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010067 printf("WDT: Started with%s servicing (%ds timeout)\n",
68 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
69
70 return 0;
71}
72
Andy Shevchenkoffdec302017-08-04 15:48:28 -060073int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.com0753bc22017-04-17 12:00:21 -070074{
75 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +010076 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070077
78 if (!ops->start)
79 return -ENOSYS;
80
Pali Rohár9c44ff12021-03-09 14:26:54 +010081 ret = ops->start(dev, timeout_ms, flags);
82 if (ret == 0)
83 gd->flags |= GD_FLG_WDT_READY;
84
85 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070086}
87
88int wdt_stop(struct udevice *dev)
89{
90 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +010091 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070092
93 if (!ops->stop)
94 return -ENOSYS;
95
Pali Rohár9c44ff12021-03-09 14:26:54 +010096 ret = ops->stop(dev);
97 if (ret == 0)
98 gd->flags &= ~GD_FLG_WDT_READY;
99
100 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -0700101}
102
103int wdt_reset(struct udevice *dev)
104{
105 const struct wdt_ops *ops = device_get_ops(dev);
106
107 if (!ops->reset)
108 return -ENOSYS;
109
110 return ops->reset(dev);
111}
112
113int wdt_expire_now(struct udevice *dev, ulong flags)
114{
115 int ret = 0;
116 const struct wdt_ops *ops;
117
Andy Shevchenkob71e0c12017-07-05 20:44:06 +0300118 debug("WDT Resetting: %lu\n", flags);
maxims@google.com0753bc22017-04-17 12:00:21 -0700119 ops = device_get_ops(dev);
120 if (ops->expire_now) {
121 return ops->expire_now(dev, flags);
122 } else {
123 if (!ops->start)
124 return -ENOSYS;
125
126 ret = ops->start(dev, 1, flags);
127 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{
144 static ulong next_reset;
145 ulong now;
146
147 /* Exit if GD is not ready or watchdog is not initialized yet */
148 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
149 return;
150
151 /* Do not reset the watchdog too often */
152 now = get_timer(0);
Rasmus Villemoes7dd20972021-04-13 16:43:20 +0200153 if (time_after_eq(now, next_reset)) {
Rasmus Villemoes40d7f3c2020-03-13 17:04:58 +0100154 next_reset = now + reset_period;
Stefan Roese06985282019-04-11 15:58:44 +0200155 wdt_reset(gd->watchdog_dev);
156 }
157}
158#endif
159
Michal Simekf238b3f2018-07-11 15:42:25 +0200160static int wdt_post_bind(struct udevice *dev)
161{
162#if defined(CONFIG_NEEDS_MANUAL_RELOC)
163 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
164 static int reloc_done;
165
166 if (!reloc_done) {
167 if (ops->start)
168 ops->start += gd->reloc_off;
169 if (ops->stop)
170 ops->stop += gd->reloc_off;
171 if (ops->reset)
172 ops->reset += gd->reloc_off;
173 if (ops->expire_now)
174 ops->expire_now += gd->reloc_off;
175
176 reloc_done++;
177 }
178#endif
179 return 0;
180}
181
maxims@google.com0753bc22017-04-17 12:00:21 -0700182UCLASS_DRIVER(wdt) = {
183 .id = UCLASS_WDT,
Michal Simek9713fac2018-07-11 08:24:43 +0200184 .name = "watchdog",
185 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simekf238b3f2018-07-11 15:42:25 +0200186 .post_bind = wdt_post_bind,
maxims@google.com0753bc22017-04-17 12:00:21 -0700187};