0a1f43771c8f0330b109bbad03395d110f709d19
[platform/kernel/u-boot.git] / drivers / watchdog / wdt-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017 Google, Inc
4  */
5
6 #define LOG_CATEGORY UCLASS_WDT
7
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <hang.h>
12 #include <log.h>
13 #include <time.h>
14 #include <wdt.h>
15 #include <asm/global_data.h>
16 #include <dm/device-internal.h>
17 #include <dm/lists.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 #define WATCHDOG_TIMEOUT_SECS   (CONFIG_WATCHDOG_TIMEOUT_MSECS / 1000)
22
23 struct wdt_priv {
24         /* Timeout, in seconds, to configure this device to. */
25         u32 timeout;
26         /*
27          * Time, in milliseconds, between calling the device's ->reset()
28          * method from watchdog_reset().
29          */
30         ulong reset_period;
31         /*
32          * Next time (as returned by get_timer(0)) to call
33          * ->reset().
34          */
35         ulong next_reset;
36 };
37
38 static void init_watchdog_dev(struct udevice *dev)
39 {
40         struct wdt_priv *priv;
41         int ret;
42
43         priv = dev_get_uclass_priv(dev);
44
45         if (!IS_ENABLED(CONFIG_WATCHDOG_AUTOSTART)) {
46                 printf("WDT:   Not starting %s\n", dev->name);
47                 return;
48         }
49
50         ret = wdt_start(dev, priv->timeout * 1000, 0);
51         if (ret != 0) {
52                 printf("WDT:   Failed to start %s\n", dev->name);
53                 return;
54         }
55
56         printf("WDT:   Started %s with%s servicing (%ds timeout)\n", dev->name,
57                IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out", priv->timeout);
58 }
59
60 int initr_watchdog(void)
61 {
62         /*
63          * Init watchdog: This will call the probe function of the
64          * watchdog driver, enabling the use of the device
65          */
66         if (uclass_get_device_by_seq(UCLASS_WDT, 0,
67                                      (struct udevice **)&gd->watchdog_dev)) {
68                 debug("WDT:   Not found by seq!\n");
69                 if (uclass_get_device(UCLASS_WDT, 0,
70                                       (struct udevice **)&gd->watchdog_dev)) {
71                         printf("WDT:   Not found!\n");
72                         return 0;
73                 }
74         }
75         init_watchdog_dev(gd->watchdog_dev);
76
77         return 0;
78 }
79
80 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
81 {
82         const struct wdt_ops *ops = device_get_ops(dev);
83         int ret;
84
85         if (!ops->start)
86                 return -ENOSYS;
87
88         ret = ops->start(dev, timeout_ms, flags);
89         if (ret == 0)
90                 gd->flags |= GD_FLG_WDT_READY;
91
92         return ret;
93 }
94
95 int wdt_stop(struct udevice *dev)
96 {
97         const struct wdt_ops *ops = device_get_ops(dev);
98         int ret;
99
100         if (!ops->stop)
101                 return -ENOSYS;
102
103         ret = ops->stop(dev);
104         if (ret == 0)
105                 gd->flags &= ~GD_FLG_WDT_READY;
106
107         return ret;
108 }
109
110 int wdt_reset(struct udevice *dev)
111 {
112         const struct wdt_ops *ops = device_get_ops(dev);
113
114         if (!ops->reset)
115                 return -ENOSYS;
116
117         return ops->reset(dev);
118 }
119
120 int wdt_expire_now(struct udevice *dev, ulong flags)
121 {
122         int ret = 0;
123         const struct wdt_ops *ops;
124
125         debug("WDT Resetting: %lu\n", flags);
126         ops = device_get_ops(dev);
127         if (ops->expire_now) {
128                 return ops->expire_now(dev, flags);
129         } else {
130                 ret = wdt_start(dev, 1, flags);
131
132                 if (ret < 0)
133                         return ret;
134
135                 hang();
136         }
137
138         return ret;
139 }
140
141 #if defined(CONFIG_WATCHDOG)
142 /*
143  * Called by macro WATCHDOG_RESET. This function be called *very* early,
144  * so we need to make sure, that the watchdog driver is ready before using
145  * it in this function.
146  */
147 void watchdog_reset(void)
148 {
149         struct wdt_priv *priv;
150         struct udevice *dev;
151         ulong now;
152
153         /* Exit if GD is not ready or watchdog is not initialized yet */
154         if (!gd || !(gd->flags & GD_FLG_WDT_READY))
155                 return;
156
157         dev = gd->watchdog_dev;
158         priv = dev_get_uclass_priv(dev);
159         /* Do not reset the watchdog too often */
160         now = get_timer(0);
161         if (time_after_eq(now, priv->next_reset)) {
162                 priv->next_reset = now + priv->reset_period;
163                 wdt_reset(dev);
164         }
165 }
166 #endif
167
168 static int wdt_post_bind(struct udevice *dev)
169 {
170 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
171         struct wdt_ops *ops = (struct wdt_ops *)device_get_ops(dev);
172         static int reloc_done;
173
174         if (!reloc_done) {
175                 if (ops->start)
176                         ops->start += gd->reloc_off;
177                 if (ops->stop)
178                         ops->stop += gd->reloc_off;
179                 if (ops->reset)
180                         ops->reset += gd->reloc_off;
181                 if (ops->expire_now)
182                         ops->expire_now += gd->reloc_off;
183
184                 reloc_done++;
185         }
186 #endif
187         return 0;
188 }
189
190 static int wdt_pre_probe(struct udevice *dev)
191 {
192         u32 timeout = WATCHDOG_TIMEOUT_SECS;
193         /*
194          * Reset every 1000ms, or however often is required as
195          * indicated by a hw_margin_ms property.
196          */
197         ulong reset_period = 1000;
198         struct wdt_priv *priv;
199
200         if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
201                 timeout = dev_read_u32_default(dev, "timeout-sec", timeout);
202                 reset_period = dev_read_u32_default(dev, "hw_margin_ms",
203                                                     4 * reset_period) / 4;
204         }
205         priv = dev_get_uclass_priv(dev);
206         priv->timeout = timeout;
207         priv->reset_period = reset_period;
208         /*
209          * Pretend this device was last reset "long" ago so the first
210          * watchdog_reset will actually call its ->reset method.
211          */
212         priv->next_reset = get_timer(0);
213
214         return 0;
215 }
216
217 UCLASS_DRIVER(wdt) = {
218         .id                     = UCLASS_WDT,
219         .name                   = "watchdog",
220         .flags                  = DM_UC_FLAG_SEQ_ALIAS,
221         .post_bind              = wdt_post_bind,
222         .pre_probe              = wdt_pre_probe,
223         .per_device_auto        = sizeof(struct wdt_priv),
224 };