blob: 0603ffbd36d9d65276ea80a1ae85aad06ade22f5 [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
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
Simon Glassf11478f2019-12-28 10:45:07 -07009#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Chris Packham32a234f2020-02-24 13:20:33 +130011#include <time.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070012#include <wdt.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
maxims@google.comdaea6d42017-04-17 12:00:21 -070014#include <dm/device-internal.h>
15#include <dm/lists.h>
16
Stefan Roese502acb02019-04-11 15:58:44 +020017DECLARE_GLOBAL_DATA_PTR;
18
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010019#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
20
Rasmus Villemoes2ba843d2020-03-13 17:04:58 +010021/*
22 * Reset every 1000ms, or however often is required as indicated by a
23 * hw_margin_ms property.
24 */
25static ulong reset_period = 1000;
26
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010027int initr_watchdog(void)
28{
29 u32 timeout = WATCHDOG_TIMEOUT_SECS;
Pali Rohárc80b8212021-03-09 14:26:55 +010030 int ret;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010031
32 /*
33 * Init watchdog: This will call the probe function of the
34 * watchdog driver, enabling the use of the device
35 */
36 if (uclass_get_device_by_seq(UCLASS_WDT, 0,
37 (struct udevice **)&gd->watchdog_dev)) {
38 debug("WDT: Not found by seq!\n");
39 if (uclass_get_device(UCLASS_WDT, 0,
40 (struct udevice **)&gd->watchdog_dev)) {
41 printf("WDT: Not found!\n");
42 return 0;
43 }
44 }
45
46 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
47 timeout = dev_read_u32_default(gd->watchdog_dev, "timeout-sec",
48 WATCHDOG_TIMEOUT_SECS);
Rasmus Villemoes2ba843d2020-03-13 17:04:58 +010049 reset_period = dev_read_u32_default(gd->watchdog_dev,
50 "hw_margin_ms",
51 4 * reset_period) / 4;
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010052 }
53
Pali Rohárf698a772021-03-09 14:26:56 +010054 if (!CONFIG_IS_ENABLED(WATCHDOG_AUTOSTART)) {
55 printf("WDT: Not starting\n");
56 return 0;
57 }
58
Pali Rohárc80b8212021-03-09 14:26:55 +010059 ret = wdt_start(gd->watchdog_dev, timeout * 1000, 0);
60 if (ret != 0) {
61 printf("WDT: Failed to start\n");
62 return 0;
63 }
64
Rasmus Villemoesf7c2e9f2020-03-13 17:04:57 +010065 printf("WDT: Started with%s servicing (%ds timeout)\n",
66 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
67
68 return 0;
69}
70
Andy Shevchenko2befb4b2017-08-04 15:48:28 -060071int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.comdaea6d42017-04-17 12:00:21 -070072{
73 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010074 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070075
76 if (!ops->start)
77 return -ENOSYS;
78
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010079 ret = ops->start(dev, timeout_ms, flags);
80 if (ret == 0)
81 gd->flags |= GD_FLG_WDT_READY;
82
83 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070084}
85
86int wdt_stop(struct udevice *dev)
87{
88 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010089 int ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070090
91 if (!ops->stop)
92 return -ENOSYS;
93
Pali Rohár8a8a5ad2021-03-09 14:26:54 +010094 ret = ops->stop(dev);
95 if (ret == 0)
96 gd->flags &= ~GD_FLG_WDT_READY;
97
98 return ret;
maxims@google.comdaea6d42017-04-17 12:00:21 -070099}
100
101int wdt_reset(struct udevice *dev)
102{
103 const struct wdt_ops *ops = device_get_ops(dev);
104
105 if (!ops->reset)
106 return -ENOSYS;
107
108 return ops->reset(dev);
109}
110
111int wdt_expire_now(struct udevice *dev, ulong flags)
112{
113 int ret = 0;
114 const struct wdt_ops *ops;
115
Andy Shevchenkof047be42017-07-05 20:44:06 +0300116 debug("WDT Resetting: %lu\n", flags);
maxims@google.comdaea6d42017-04-17 12:00:21 -0700117 ops = device_get_ops(dev);
118 if (ops->expire_now) {
119 return ops->expire_now(dev, flags);
120 } else {
121 if (!ops->start)
122 return -ENOSYS;
123
124 ret = ops->start(dev, 1, flags);
125 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);
Chris Packham32a234f2020-02-24 13:20:33 +1300151 if (time_after(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};