blob: 071c389ca073baf975ed8ba47c07023370cdb8ce [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren89c1e2d2016-06-17 09:43:58 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
Stephen Warren89c1e2d2016-06-17 09:43:58 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Stephen Warren89c1e2d2016-06-17 09:43:58 -060011#include <reset.h>
12#include <reset-uclass.h>
Simon Glass61b29b82020-02-03 07:36:15 -070013#include <dm/devres.h>
Jean-Jacques Hiblot139e4a12020-09-09 15:37:03 +053014#include <dm/lists.h>
Stephen Warren89c1e2d2016-06-17 09:43:58 -060015
Stephen Warren89c1e2d2016-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 Glass40a475e2017-05-18 20:09:50 -060022 struct ofnode_phandle_args *args)
Stephen Warren89c1e2d2016-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 Tekiea9dc352019-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 Warren89c1e2d2016-06-17 09:43:58 -060040{
Stephen Warren89c1e2d2016-06-17 09:43:58 -060041 struct udevice *dev_reset;
42 struct reset_ops *ops;
43
Jagan Tekiea9dc352019-02-28 00:26:55 +053044 assert(reset_ctl);
Patrice Chotard3b9d1bd2017-07-18 11:57:06 +020045 reset_ctl->dev = NULL;
Jagan Tekiea9dc352019-02-28 00:26:55 +053046 if (ret)
Stephen Warren89c1e2d2016-06-17 09:43:58 -060047 return ret;
Stephen Warren89c1e2d2016-06-17 09:43:58 -060048
Jagan Tekiea9dc352019-02-28 00:26:55 +053049 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node,
Simon Glass40a475e2017-05-18 20:09:50 -060050 &dev_reset);
Stephen Warren89c1e2d2016-06-17 09:43:58 -060051 if (ret) {
Simon Glass40a475e2017-05-18 20:09:50 -060052 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren89c1e2d2016-06-17 09:43:58 -060053 __func__, ret);
Jagan Tekiea9dc352019-02-28 00:26:55 +053054 debug("%s %d\n", ofnode_get_name(args->node), args->args[0]);
Stephen Warren89c1e2d2016-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 Tekiea9dc352019-02-28 00:26:55 +053061 ret = ops->of_xlate(reset_ctl, args);
Stephen Warren89c1e2d2016-06-17 09:43:58 -060062 else
Jagan Tekiea9dc352019-02-28 00:26:55 +053063 ret = reset_of_xlate_default(reset_ctl, args);
Stephen Warren89c1e2d2016-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 Tekiea9dc352019-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 Hiblot139e4a12020-09-09 15:37:03 +0530104static int __reset_get_bulk(struct udevice *dev, ofnode node,
105 struct reset_ctl_bulk *bulk)
Neil Armstrong0c282332018-04-03 11:40:50 +0200106{
107 int i, ret, err, count;
Jean-Jacques Hiblot139e4a12020-09-09 15:37:03 +0530108
Neil Armstrong0c282332018-04-03 11:40:50 +0200109 bulk->count = 0;
110
Patrick Delaunay89f68302020-09-25 09:41:14 +0200111 count = ofnode_count_phandle_with_args(node, "resets", "#reset-cells",
112 0);
Neil Armstrong895a82c2018-04-17 11:30:22 +0200113 if (count < 1)
114 return count;
Neil Armstrong0c282332018-04-03 11:40:50 +0200115
116 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
117 GFP_KERNEL);
118 if (!bulk->resets)
119 return -ENOMEM;
120
121 for (i = 0; i < count; i++) {
Jean-Jacques Hiblot139e4a12020-09-09 15:37:03 +0530122 ret = reset_get_by_index_nodev(node, i, &bulk->resets[i]);
Neil Armstrong0c282332018-04-03 11:40:50 +0200123 if (ret < 0)
124 goto bulk_get_err;
125
126 ++bulk->count;
127 }
128
129 return 0;
130
131bulk_get_err:
132 err = reset_release_all(bulk->resets, bulk->count);
133 if (err)
134 debug("%s: could release all resets for %p\n",
135 __func__, dev);
136
137 return ret;
138}
139
Jean-Jacques Hiblot139e4a12020-09-09 15:37:03 +0530140int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
141{
142 return __reset_get_bulk(dev, dev_ofnode(dev), bulk);
143}
144
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600145int reset_get_by_name(struct udevice *dev, const char *name,
146 struct reset_ctl *reset_ctl)
147{
148 int index;
149
150 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
151 reset_ctl);
Patrice Chotard3b9d1bd2017-07-18 11:57:06 +0200152 reset_ctl->dev = NULL;
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600153
Simon Glass40a475e2017-05-18 20:09:50 -0600154 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600155 if (index < 0) {
Simon Glassb02e4042016-10-02 17:59:28 -0600156 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600157 return index;
158 }
159
160 return reset_get_by_index(dev, index, reset_ctl);
161}
162
Patrice Chotard9bd5cdf2017-07-18 11:57:05 +0200163int reset_request(struct reset_ctl *reset_ctl)
164{
165 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
166
167 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
168
169 return ops->request(reset_ctl);
170}
171
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600172int reset_free(struct reset_ctl *reset_ctl)
173{
174 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
175
176 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
177
Simon Glass94474b22020-02-03 07:35:52 -0700178 return ops->rfree(reset_ctl);
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600179}
180
181int reset_assert(struct reset_ctl *reset_ctl)
182{
183 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
184
185 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
186
187 return ops->rst_assert(reset_ctl);
188}
189
Neil Armstrong0c282332018-04-03 11:40:50 +0200190int reset_assert_bulk(struct reset_ctl_bulk *bulk)
191{
192 int i, ret;
193
194 for (i = 0; i < bulk->count; i++) {
195 ret = reset_assert(&bulk->resets[i]);
196 if (ret < 0)
197 return ret;
198 }
199
200 return 0;
201}
202
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600203int reset_deassert(struct reset_ctl *reset_ctl)
204{
205 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
206
207 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
208
209 return ops->rst_deassert(reset_ctl);
210}
211
Neil Armstrong0c282332018-04-03 11:40:50 +0200212int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
213{
214 int i, ret;
215
216 for (i = 0; i < bulk->count; i++) {
217 ret = reset_deassert(&bulk->resets[i]);
218 if (ret < 0)
219 return ret;
220 }
221
222 return 0;
223}
224
Andreas Dannenberge7012e62018-08-27 15:57:39 +0530225int reset_status(struct reset_ctl *reset_ctl)
226{
227 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
228
229 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
230
231 return ops->rst_status(reset_ctl);
232}
233
Patrice Chotard3b9d1bd2017-07-18 11:57:06 +0200234int reset_release_all(struct reset_ctl *reset_ctl, int count)
235{
236 int i, ret;
237
238 for (i = 0; i < count; i++) {
239 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
240
241 /* check if reset has been previously requested */
242 if (!reset_ctl[i].dev)
243 continue;
244
245 ret = reset_assert(&reset_ctl[i]);
246 if (ret)
247 return ret;
248
249 ret = reset_free(&reset_ctl[i]);
250 if (ret)
251 return ret;
252 }
253
254 return 0;
255}
256
Jean-Jacques Hiblot139e4a12020-09-09 15:37:03 +0530257static void devm_reset_release(struct udevice *dev, void *res)
258{
259 reset_free(res);
260}
261
262struct reset_ctl *devm_reset_control_get_by_index(struct udevice *dev,
263 int index)
264{
265 int rc;
266 struct reset_ctl *reset_ctl;
267
268 reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl),
269 __GFP_ZERO);
270 if (unlikely(!reset_ctl))
271 return ERR_PTR(-ENOMEM);
272
273 rc = reset_get_by_index(dev, index, reset_ctl);
274 if (rc)
275 return ERR_PTR(rc);
276
277 devres_add(dev, reset_ctl);
278 return reset_ctl;
279}
280
281struct reset_ctl *devm_reset_control_get(struct udevice *dev, const char *id)
282{
283 int rc;
284 struct reset_ctl *reset_ctl;
285
286 reset_ctl = devres_alloc(devm_reset_release, sizeof(struct reset_ctl),
287 __GFP_ZERO);
288 if (unlikely(!reset_ctl))
289 return ERR_PTR(-ENOMEM);
290
291 rc = reset_get_by_name(dev, id, reset_ctl);
292 if (rc)
293 return ERR_PTR(rc);
294
295 devres_add(dev, reset_ctl);
296 return reset_ctl;
297}
298
299struct reset_ctl *devm_reset_control_get_optional(struct udevice *dev,
300 const char *id)
301{
302 struct reset_ctl *r = devm_reset_control_get(dev, id);
303
304 if (IS_ERR(r))
305 return NULL;
306
307 return r;
308}
309
310static void devm_reset_bulk_release(struct udevice *dev, void *res)
311{
312 struct reset_ctl_bulk *bulk = res;
313
314 reset_release_all(bulk->resets, bulk->count);
315}
316
317struct reset_ctl_bulk *devm_reset_bulk_get_by_node(struct udevice *dev,
318 ofnode node)
319{
320 int rc;
321 struct reset_ctl_bulk *bulk;
322
323 bulk = devres_alloc(devm_reset_bulk_release,
324 sizeof(struct reset_ctl_bulk),
325 __GFP_ZERO);
326 if (unlikely(!bulk))
327 return ERR_PTR(-ENOMEM);
328
329 rc = __reset_get_bulk(dev, node, bulk);
330 if (rc)
331 return ERR_PTR(rc);
332
333 devres_add(dev, bulk);
334 return bulk;
335}
336
337struct reset_ctl_bulk *devm_reset_bulk_get_optional_by_node(struct udevice *dev,
338 ofnode node)
339{
340 struct reset_ctl_bulk *bulk;
341
342 bulk = devm_reset_bulk_get_by_node(dev, node);
343
344 if (IS_ERR(bulk))
345 return NULL;
346
347 return bulk;
348}
349
350struct reset_ctl_bulk *devm_reset_bulk_get(struct udevice *dev)
351{
352 return devm_reset_bulk_get_by_node(dev, dev_ofnode(dev));
353}
354
355struct reset_ctl_bulk *devm_reset_bulk_get_optional(struct udevice *dev)
356{
357 return devm_reset_bulk_get_optional_by_node(dev, dev_ofnode(dev));
358}
359
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600360UCLASS_DRIVER(reset) = {
361 .id = UCLASS_RESET,
362 .name = "reset",
363};