blob: d9e4dc7cb8a4efeb251ce92238033bec98fe3265 [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>
Chris Packham6d8eae92020-02-24 13:20:33 +130010#include <time.h>
maxims@google.com0753bc22017-04-17 12:00:21 -070011#include <wdt.h>
12#include <dm/device-internal.h>
13#include <dm/lists.h>
14
Stefan Roese06985282019-04-11 15:58:44 +020015DECLARE_GLOBAL_DATA_PTR;
16
Andy Shevchenkoffdec302017-08-04 15:48:28 -060017int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
maxims@google.com0753bc22017-04-17 12:00:21 -070018{
19 const struct wdt_ops *ops = device_get_ops(dev);
20
21 if (!ops->start)
22 return -ENOSYS;
23
Andy Shevchenkoffdec302017-08-04 15:48:28 -060024 return ops->start(dev, timeout_ms, flags);
maxims@google.com0753bc22017-04-17 12:00:21 -070025}
26
27int wdt_stop(struct udevice *dev)
28{
29 const struct wdt_ops *ops = device_get_ops(dev);
30
31 if (!ops->stop)
32 return -ENOSYS;
33
34 return ops->stop(dev);
35}
36
37int wdt_reset(struct udevice *dev)
38{
39 const struct wdt_ops *ops = device_get_ops(dev);
40
41 if (!ops->reset)
42 return -ENOSYS;
43
44 return ops->reset(dev);
45}
46
47int wdt_expire_now(struct udevice *dev, ulong flags)
48{
49 int ret = 0;
50 const struct wdt_ops *ops;
51
Andy Shevchenkob71e0c12017-07-05 20:44:06 +030052 debug("WDT Resetting: %lu\n", flags);
maxims@google.com0753bc22017-04-17 12:00:21 -070053 ops = device_get_ops(dev);
54 if (ops->expire_now) {
55 return ops->expire_now(dev, flags);
56 } else {
57 if (!ops->start)
58 return -ENOSYS;
59
60 ret = ops->start(dev, 1, flags);
61 if (ret < 0)
62 return ret;
63
64 hang();
65 }
66
67 return ret;
68}
69
Stefan Roese06985282019-04-11 15:58:44 +020070#if defined(CONFIG_WATCHDOG)
71/*
72 * Called by macro WATCHDOG_RESET. This function be called *very* early,
73 * so we need to make sure, that the watchdog driver is ready before using
74 * it in this function.
75 */
76void watchdog_reset(void)
77{
78 static ulong next_reset;
79 ulong now;
80
81 /* Exit if GD is not ready or watchdog is not initialized yet */
82 if (!gd || !(gd->flags & GD_FLG_WDT_READY))
83 return;
84
85 /* Do not reset the watchdog too often */
86 now = get_timer(0);
Chris Packham6d8eae92020-02-24 13:20:33 +130087 if (time_after(now, next_reset)) {
Stefan Roese06985282019-04-11 15:58:44 +020088 next_reset = now + 1000; /* reset every 1000ms */
89 wdt_reset(gd->watchdog_dev);
90 }
91}
92#endif
93
Michal Simekf238b3f2018-07-11 15:42:25 +020094static int wdt_post_bind(struct udevice *dev)
95{
96#if defined(CONFIG_NEEDS_MANUAL_RELOC)
97 struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
98 static int reloc_done;
99
100 if (!reloc_done) {
101 if (ops->start)
102 ops->start += gd->reloc_off;
103 if (ops->stop)
104 ops->stop += gd->reloc_off;
105 if (ops->reset)
106 ops->reset += gd->reloc_off;
107 if (ops->expire_now)
108 ops->expire_now += gd->reloc_off;
109
110 reloc_done++;
111 }
112#endif
113 return 0;
114}
115
maxims@google.com0753bc22017-04-17 12:00:21 -0700116UCLASS_DRIVER(wdt) = {
117 .id = UCLASS_WDT,
Michal Simek9713fac2018-07-11 08:24:43 +0200118 .name = "watchdog",
119 .flags = DM_UC_FLAG_SEQ_ALIAS,
Michal Simekf238b3f2018-07-11 15:42:25 +0200120 .post_bind = wdt_post_bind,
maxims@google.com0753bc22017-04-17 12:00:21 -0700121};