blob: 7500b3ed90e31e5b9899fd7f125000f3240a8ded [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
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
Simon Glassdb41d652019-12-28 10:45:07 -07009#include <hang.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Chris Packham6d8eae92020-02-24 13:20:33 +130011#include <time.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070012#include <wdt.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070014#include <dm/device-internal.h>
15#include <dm/lists.h>
16
Stefan Roese06985282019-04-11 15:58:44 +020017DECLARE_GLOBAL_DATA_PTR;
18
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010019#define WATCHDOG_TIMEOUT_SECS (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
20
Rasmus Villemoes40d7f3c2020-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 Villemoesb4d94522020-03-13 17:04:57 +010027int initr_watchdog(void)
28{
29 u32 timeout = WATCHDOG_TIMEOUT_SECS;
Pali Rohár25e20e32021-03-09 14:26:55 +010030 int ret;
Rasmus Villemoesb4d94522020-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 Villemoes40d7f3c2020-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 Villemoesb4d94522020-03-13 17:04:57 +010052 }
53
Pali Rohár25e20e32021-03-09 14:26:55 +010054 ret = wdt_start(gd->watchdog_dev, timeout * 1000, 0);
55 if (ret != 0) {
56 printf("WDT: Failed to start\n");
57 return 0;
58 }
59
Rasmus Villemoesb4d94522020-03-13 17:04:57 +010060 printf("WDT: Started with%s servicing (%ds timeout)\n",
61 IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", timeout);
62
63 return 0;
64}
65
Andy Shevchenkoffdec302017-08-04 15:48:28 -060066int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.com0753bc22017-04-17 12:00:21 -070067{
68 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +010069 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070070
71 if (!ops->start)
72 return -ENOSYS;
73
Pali Rohár9c44ff12021-03-09 14:26:54 +010074 ret = ops->start(dev, timeout_ms, flags);
75 if (ret == 0)
76 gd->flags |= GD_FLG_WDT_READY;
77
78 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070079}
80
81int wdt_stop(struct udevice *dev)
82{
83 const struct wdt_ops *ops = device_get_ops(dev);
Pali Rohár9c44ff12021-03-09 14:26:54 +010084 int ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070085
86 if (!ops->stop)
87 return -ENOSYS;
88
Pali Rohár9c44ff12021-03-09 14:26:54 +010089 ret = ops->stop(dev);
90 if (ret == 0)
91 gd->flags &= ~GD_FLG_WDT_READY;
92
93 return ret;
maxims@google.com0753bc22017-04-17 12:00:21 -070094}
95
96int wdt_reset(struct udevice *dev)
97{
98 const struct wdt_ops *ops = device_get_ops(dev);
99
100 if (!ops->reset)
101 return -ENOSYS;
102
103 return ops->reset(dev);
104}
105
106int wdt_expire_now(struct udevice *dev, ulong flags)
107{
108 int ret = 0;
109 const struct wdt_ops *ops;
110
Andy Shevchenkob71e0c12017-07-05 20:44:06 +0300111 debug("WDT Resetting: %lu\n", flags);
maxims@google.com0753bc22017-04-17 12:00:21 -0700112 ops = device_get_ops(dev);
113 if (ops->expire_now) {
114 return ops->expire_now(dev, flags);
115 } else {
116 if (!ops->start)
117 return -ENOSYS;
118
119 ret = ops->start(dev, 1, flags);
120 if (ret < 0)
121 return ret;
122
123 hang();
124 }
125
126 return ret;
127}
128
Stefan Roese06985282019-04-11 15:58:44 +0200129#if defined(CONFIG_WATCHDOG)
130/*
131 * Called by macro WATCHDOG_RESET. This function be called *very* early,
132 * so we need to make sure, that the watchdog driver is ready before using
133 * it in this function.
134 */
135void watchdog_reset(void)
136{
137 static ulong next_reset;
138 ulong now;
139
140 /* Exit if GD is not ready or watchdog is not initialized yet */
141 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
142 return;
143
144 /* Do not reset the watchdog too often */
145 now = get_timer(0);
Chris Packham6d8eae92020-02-24 13:20:33 +1300146 if (time_after(now, next_reset)) {
Rasmus Villemoes40d7f3c2020-03-13 17:04:58 +0100147 next_reset = now + reset_period;
Stefan Roese06985282019-04-11 15:58:44 +0200148 wdt_reset(gd->watchdog_dev);
149 }
150}
151#endif
152
Michal Simekf238b3f2018-07-11 15:42:25 +0200153static int wdt_post_bind(struct udevice *dev)
154{
155#if defined(CONFIG_NEEDS_MANUAL_RELOC)
156 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
157 static int reloc_done;
158
159 if (!reloc_done) {
160 if (ops->start)
161 ops->start += gd->reloc_off;
162 if (ops->stop)
163 ops->stop += gd->reloc_off;
164 if (ops->reset)
165 ops->reset += gd->reloc_off;
166 if (ops->expire_now)
167 ops->expire_now += gd->reloc_off;
168
169 reloc_done++;
170 }
171#endif
172 return 0;
173}
174
maxims@google.com0753bc22017-04-17 12:00:21 -0700175UCLASS_DRIVER(wdt) = {
176 .id = UCLASS_WDT,
Michal Simek9713fac2018-07-11 08:24:43 +0200177 .name = "watchdog",
178 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simekf238b3f2018-07-11 15:42:25 +0200179 .post_bind = wdt_post_bind,
maxims@google.com0753bc22017-04-17 12:00:21 -0700180};