Merge branch 'next' of git://git.denx.de/u-boot-usb into next
[oweals/u-boot.git] / drivers / watchdog / wdt-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017 Google, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <hang.h>
10 #include <time.h>
11 #include <wdt.h>
12 #include <dm/device-internal.h>
13 #include <dm/lists.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
18 {
19         const struct wdt_ops *ops = device_get_ops(dev);
20
21         if (!ops->start)
22                 return -ENOSYS;
23
24         return ops->start(dev, timeout_ms, flags);
25 }
26
27 int 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
37 int 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
47 int wdt_expire_now(struct udevice *dev, ulong flags)
48 {
49         int ret = 0;
50         const struct wdt_ops *ops;
51
52         debug("WDT Resetting: %lu\n", flags);
53         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
70 #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  */
76 void 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);
87         if (time_after(now, next_reset)) {
88                 next_reset = now + 1000;        /* reset every 1000ms */
89                 wdt_reset(gd->watchdog_dev);
90         }
91 }
92 #endif
93
94 static 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
116 UCLASS_DRIVER(wdt) = {
117         .id             = UCLASS_WDT,
118         .name           = "watchdog",
119         .flags          = DM_UC_FLAG_SEQ_ALIAS,
120         .post_bind      = wdt_post_bind,
121 };