blob: df8164da2ae0684d889d168de451ebdffaa317a3 [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 Villemoes2ba843d2020-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 Villemoesf7c2e9f2020-03-13 17:04:57 +010029int initr_watchdog(void)
30{
31 u32 timeout = WATCHDOG_TIMEOUT_SECS;
Pali Rohárc80b8212021-03-09 14:26:55 +010032 int ret;
Rasmus Villemoesf7c2e9f2020-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 Villemoes2ba843d2020-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 Villemoesf7c2e9f2020-03-13 17:04:57 +010054 }
55
Teresa Remmet4e511842021-07-15 13:26:32 +020056 if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
Pali Rohárf698a772021-03-09 14:26:56 +010057 printf("WDT: Not starting\n");
58 return 0;
59 }
60
Pali Rohárc80b8212021-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 Villemoesf7c2e9f2020-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 Shevchenko2befb4b2017-08-04 15:48:28 -060073int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.comdaea6d42017-04-17 12:00:21 -070074{
75 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010076 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070077
78 if (!ops->start)
79 return -ENOSYS;
80
Pali Rohár8a8a5ad2021-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.comdaea6d42017-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ár8a8a5ad2021-03-09 14:26:54 +010091 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070092
93 if (!ops->stop)
94 return -ENOSYS;
95
Pali Rohár8a8a5ad2021-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.comdaea6d42017-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 Shevchenkof047be42017-07-05 20:44:06 +0300118 debug("WDT Resetting: %lu\n", flags);
maxims@google.comdaea6d42017-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 {
Rasmus Villemoesf008c302021-08-19 11:56:55 +0200123 ret = wdt_start(dev, 1, flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700124
maxims@google.comdaea6d42017-04-17 12:00:21 -0700125 if (ret < 0)
126 return ret;
127
128 hang();
129 }
130
131 return ret;
132}
Stefan Roese502acb02019-04-11 15:58:44 +0200133
134#if defined(CONFIG_WATCHDOG)
135/*
136 * Called by macro WATCHDOG_RESET. This function be called *very* early,
137 * so we need to make sure, that the watchdog driver is ready before using
138 * it in this function.
139 */
140void watchdog_reset(void)
141{
142 static ulong next_reset;
143 ulong now;
144
145 /* Exit if GD is not ready or watchdog is not initialized yet */
146 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
147 return;
148
149 /* Do not reset the watchdog too often */
150 now = get_timer(0);
Rasmus Villemoes739e9002021-04-13 16:43:20 +0200151 if (time_after_eq(now, next_reset)) {
Rasmus Villemoes2ba843d2020-03-13 17:04:58 +0100152 next_reset = now + reset_period;
Stefan Roese502acb02019-04-11 15:58:44 +0200153 wdt_reset(gd->watchdog_dev);
154 }
155}
156#endif
maxims@google.comdaea6d42017-04-17 12:00:21 -0700157
Michal Simek7b5eca92018-07-11 15:42:25 +0200158static int wdt_post_bind(struct udevice *dev)
159{
160#if defined(CONFIG_NEEDS_MANUAL_RELOC)
161 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
162 static int reloc_done;
163
164 if (!reloc_done) {
165 if (ops->start)
166 ops->start += gd->reloc_off;
167 if (ops->stop)
168 ops->stop += gd->reloc_off;
169 if (ops->reset)
170 ops->reset += gd->reloc_off;
171 if (ops->expire_now)
172 ops->expire_now += gd->reloc_off;
173
174 reloc_done++;
175 }
176#endif
177 return 0;
178}
179
maxims@google.comdaea6d42017-04-17 12:00:21 -0700180UCLASS_DRIVER(wdt) = {
181 .id = UCLASS_WDT,
Michal Simekfc61be32018-07-11 08:24:43 +0200182 .name = "watchdog",
183 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simek7b5eca92018-07-11 15:42:25 +0200184 .post_bind = wdt_post_bind,
maxims@google.comdaea6d42017-04-17 12:00:21 -0700185};