blob: e7e407ca3503622b97bf976d5a22991792400784 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren185ad872016-06-17 09:43:58 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warren185ad872016-06-17 09:43:58 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Stephen Warren185ad872016-06-17 09:43:58 -060011#include <reset.h>
12#include <reset-uclass.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070013#include <dm/devres.h>
Jean-Jacques Hiblot5be97a32020-09-09 15:37:03 +053014#include <dm/lists.h>
Stephen Warren185ad872016-06-17 09:43:58 -060015
Stephen Warren185ad872016-06-17 09:43:58 -060016static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
17{
18 return (struct reset_ops *)dev->driver->ops;
19}
20
21static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
Simon Glassaec8cc02017-05-18 20:09:50 -060022 struct ofnode_phandle_args *args)
Stephen Warren185ad872016-06-17 09:43:58 -060023{
24 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
25
26 if (args->args_count != 1) {
27 debug("Invaild args_count: %d\n", args->args_count);
28 return -EINVAL;
29 }
30
31 reset_ctl->id = args->args[0];
32
33 return 0;
34}
35
Jagan Teki01af1152019-02-28 00:26:55 +053036static int reset_get_by_index_tail(int ret, ofnode node,
37 struct ofnode_phandle_args *args,
38 const char *list_name, int index,
39 struct reset_ctl *reset_ctl)
Stephen Warren185ad872016-06-17 09:43:58 -060040{
Stephen Warren185ad872016-06-17 09:43:58 -060041 struct udevice *dev_reset;
42 struct reset_ops *ops;
43
Jagan Teki01af1152019-02-28 00:26:55 +053044 assert(reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +020045 reset_ctl->dev = NULL;
Jagan Teki01af1152019-02-28 00:26:55 +053046 if (ret)
Stephen Warren185ad872016-06-17 09:43:58 -060047 return ret;
Stephen Warren185ad872016-06-17 09:43:58 -060048
Jagan Teki01af1152019-02-28 00:26:55 +053049 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node,
Simon Glassaec8cc02017-05-18 20:09:50 -060050 &dev_reset);
Stephen Warren185ad872016-06-17 09:43:58 -060051 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060052 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060053 __func__, ret);
Jagan Teki01af1152019-02-28 00:26:55 +053054 debug("%s %d\n", ofnode_get_name(args->node), args->args[0]);
Stephen Warren185ad872016-06-17 09:43:58 -060055 return ret;
56 }
57 ops = reset_dev_ops(dev_reset);
58
59 reset_ctl->dev = dev_reset;
60 if (ops->of_xlate)
Jagan Teki01af1152019-02-28 00:26:55 +053061 ret = ops->of_xlate(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060062 else
Jagan Teki01af1152019-02-28 00:26:55 +053063 ret = reset_of_xlate_default(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060064 if (ret) {
65 debug("of_xlate() failed: %d\n", ret);
66 return ret;
67 }
68
69 ret = ops->request(reset_ctl);
70 if (ret) {
71 debug("ops->request() failed: %d\n", ret);
72 return ret;
73 }
74
75 return 0;
76}
77
Jagan Teki01af1152019-02-28 00:26:55 +053078int reset_get_by_index(struct udevice *dev, int index,
79 struct reset_ctl *reset_ctl)
80{
81 struct ofnode_phandle_args args;
82 int ret;
83
84 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
85 index, &args);
86
87 return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets",
88 index > 0, reset_ctl);
89}
90
91int reset_get_by_index_nodev(ofnode node, int index,
92 struct reset_ctl *reset_ctl)
93{
94 struct ofnode_phandle_args args;
95 int ret;
96
97 ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0,
98 index > 0, &args);
99
100 return reset_get_by_index_tail(ret, node, &args, "resets",
101 index > 0, reset_ctl);
102}
103
Jean-Jacques Hiblot5be97a32020-09-09 15:37:03 +0530104static int __reset_get_bulk(struct udevice *dev, ofnode node,
105 struct reset_ctl_bulk *bulk)
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200106{
107 int i, ret, err, count;
Jean-Jacques Hiblot5be97a32020-09-09 15:37:03 +0530108
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200109 bulk->count = 0;
110
Jean-Jacques Hiblot5be97a32020-09-09 15:37:03 +0530111 count = ofnode_count_phandle_with_args(node, "resets", "#reset-cells");
Neil Armstrong6acc49a2018-04-17 11:30:22 +0200112 if (count < 1)
113 return count;
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200114
115 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
116 GFP_KERNEL);
117 if (!bulk->resets)
118 return -ENOMEM;
119
120 for (i = 0; i < count; i++) {
Jean-Jacques Hiblot5be97a32020-09-09 15:37:03 +0530121 ret = reset_get_by_index_nodev(node, i, &bulk->resets[i]);
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200122 if (ret < 0)
123 goto bulk_get_err;
124
125 ++bulk->count;
126 }
127
128 return 0;
129
130bulk_get_err:
131 err = reset_release_all(bulk->resets, bulk->count);
132 if (err)
133 debug("%s: could release all resets for %p\n",
134 __func__, dev);
135
136 return ret;
137}
138
Jean-Jacques Hiblot5be97a32020-09-09 15:37:03 +0530139int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
140{
141 return __reset_get_bulk(dev, dev_ofnode(dev), bulk);
142}
143
Stephen Warren185ad872016-06-17 09:43:58 -0600144int reset_get_by_name(struct udevice *dev, const char *name,
145 struct reset_ctl *reset_ctl)
146{
147 int index;
148
149 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
150 reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200151 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -0600152
Simon Glassaec8cc02017-05-18 20:09:50 -0600153 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren185ad872016-06-17 09:43:58 -0600154 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600155 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren185ad872016-06-17 09:43:58 -0600156 return index;
157 }
158
159 return reset_get_by_index(dev, index, reset_ctl);
160}
161
Patrice Chotard76290e32017-07-18 11:57:05 +0200162int reset_request(struct reset_ctl *reset_ctl)
163{
164 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
165
166 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
167
168 return ops->request(reset_ctl);
169}
170
Stephen Warren185ad872016-06-17 09:43:58 -0600171int reset_free(struct reset_ctl *reset_ctl)
172{
173 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
174
175 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
176
Simon Glass1928cd42020-02-03 07:35:52 -0700177 return ops->rfree(reset_ctl);
Stephen Warren185ad872016-06-17 09:43:58 -0600178}
179
180int reset_assert(struct reset_ctl *reset_ctl)
181{
182 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
183
184 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
185
186 return ops->rst_assert(reset_ctl);
187}
188
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200189int reset_assert_bulk(struct reset_ctl_bulk *bulk)
190{
191 int i, ret;
192
193 for (i = 0; i < bulk->count; i++) {
194 ret = reset_assert(&bulk->resets[i]);
195 if (ret < 0)
196 return ret;
197 }
198
199 return 0;
200}
201
Stephen Warren185ad872016-06-17 09:43:58 -0600202int reset_deassert(struct reset_ctl *reset_ctl)
203{
204 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
205
206 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
207
208 return ops->rst_deassert(reset_ctl);
209}
210
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200211int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
212{
213 int i, ret;
214
215 for (i = 0; i < bulk->count; i++) {
216 ret = reset_deassert(&bulk->resets[i]);
217 if (ret < 0)
218 return ret;
219 }
220
221 return 0;
222}
223
Andreas Dannenberge2bc7402018-08-27 15:57:39 +0530224int reset_status(struct reset_ctl *reset_ctl)
225{
226 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
227
228 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
229
230 return ops->rst_status(reset_ctl);
231}
232
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200233int reset_release_all(struct reset_ctl *reset_ctl, int count)
234{
235 int i, ret;
236
237 for (i = 0; i < count; i++) {
238 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
239
240 /* check if reset has been previously requested */
241 if (!reset_ctl[i].dev)
242 continue;
243
244 ret = reset_assert(&reset_ctl[i]);
245 if (ret)
246 return ret;
247
248 ret = reset_free(&reset_ctl[i]);
249 if (ret)
250 return ret;
251 }
252
253 return 0;
254}
255
Jean-Jacques Hiblot5be97a32020-09-09 15:37:03 +0530256static void devm_reset_release(struct udevice *dev, void *res)
257{
258 reset_free(res);
259}
260
261struct reset_ctl *devm_reset_control_get_by_index(struct udevice *dev,
262 int index)
263{
264 int rc;
265 struct reset_ctl *reset_ctl;
266
267 reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl),
268 __GFP_ZERO);
269 if (unlikely(!reset_ctl))
270 return ERR_PTR(-ENOMEM);
271
272 rc = reset_get_by_index(dev, index, reset_ctl);
273 if (rc)
274 return ERR_PTR(rc);
275
276 devres_add(dev, reset_ctl);
277 return reset_ctl;
278}
279
280struct reset_ctl *devm_reset_control_get(struct udevice *dev, const char *id)
281{
282 int rc;
283 struct reset_ctl *reset_ctl;
284
285 reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl),
286 __GFP_ZERO);
287 if (unlikely(!reset_ctl))
288 return ERR_PTR(-ENOMEM);
289
290 rc = reset_get_by_name(dev, id, reset_ctl);
291 if (rc)
292 return ERR_PTR(rc);
293
294 devres_add(dev, reset_ctl);
295 return reset_ctl;
296}
297
298struct reset_ctl *devm_reset_control_get_optional(struct udevice *dev,
299 const char *id)
300{
301 struct reset_ctl *r = devm_reset_control_get(dev, id);
302
303 if (IS_ERR(r))
304 return NULL;
305
306 return r;
307}
308
309static void devm_reset_bulk_release(struct udevice *dev, void *res)
310{
311 struct reset_ctl_bulk *bulk = res;
312
313 reset_release_all(bulk->resets, bulk->count);
314}
315
316struct reset_ctl_bulk *devm_reset_bulk_get_by_node(struct udevice *dev,
317 ofnode node)
318{
319 int rc;
320 struct reset_ctl_bulk *bulk;
321
322 bulk = devres_alloc(devm_reset_bulk_release,
323 sizeof(struct reset_ctl_bulk),
324 __GFP_ZERO);
325 if (unlikely(!bulk))
326 return ERR_PTR(-ENOMEM);
327
328 rc = __reset_get_bulk(dev, node, bulk);
329 if (rc)
330 return ERR_PTR(rc);
331
332 devres_add(dev, bulk);
333 return bulk;
334}
335
336struct reset_ctl_bulk *devm_reset_bulk_get_optional_by_node(struct udevice *dev,
337 ofnode node)
338{
339 struct reset_ctl_bulk *bulk;
340
341 bulk = devm_reset_bulk_get_by_node(dev, node);
342
343 if (IS_ERR(bulk))
344 return NULL;
345
346 return bulk;
347}
348
349struct reset_ctl_bulk *devm_reset_bulk_get(struct udevice *dev)
350{
351 return devm_reset_bulk_get_by_node(dev, dev_ofnode(dev));
352}
353
354struct reset_ctl_bulk *devm_reset_bulk_get_optional(struct udevice *dev)
355{
356 return devm_reset_bulk_get_optional_by_node(dev, dev_ofnode(dev));
357}
358
Stephen Warren185ad872016-06-17 09:43:58 -0600359UCLASS_DRIVER(reset) = {
360 .id = UCLASS_RESET,
361 .name = "reset",
362};