blob: 5e38ce5c0669488e9c916a8b9957fc38d46a2762 [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>
Stephen Warren185ad872016-06-17 09:43:58 -060014
Stephen Warren185ad872016-06-17 09:43:58 -060015static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
16{
17 return (struct reset_ops *)dev->driver->ops;
18}
19
20static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
Simon Glassaec8cc02017-05-18 20:09:50 -060021 struct ofnode_phandle_args *args)
Stephen Warren185ad872016-06-17 09:43:58 -060022{
23 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
24
25 if (args->args_count != 1) {
26 debug("Invaild args_count: %d\n", args->args_count);
27 return -EINVAL;
28 }
29
30 reset_ctl->id = args->args[0];
31
32 return 0;
33}
34
Jagan Teki01af1152019-02-28 00:26:55 +053035static int reset_get_by_index_tail(int ret, ofnode node,
36 struct ofnode_phandle_args *args,
37 const char *list_name, int index,
38 struct reset_ctl *reset_ctl)
Stephen Warren185ad872016-06-17 09:43:58 -060039{
Stephen Warren185ad872016-06-17 09:43:58 -060040 struct udevice *dev_reset;
41 struct reset_ops *ops;
42
Jagan Teki01af1152019-02-28 00:26:55 +053043 assert(reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +020044 reset_ctl->dev = NULL;
Jagan Teki01af1152019-02-28 00:26:55 +053045 if (ret)
Stephen Warren185ad872016-06-17 09:43:58 -060046 return ret;
Stephen Warren185ad872016-06-17 09:43:58 -060047
Jagan Teki01af1152019-02-28 00:26:55 +053048 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node,
Simon Glassaec8cc02017-05-18 20:09:50 -060049 &dev_reset);
Stephen Warren185ad872016-06-17 09:43:58 -060050 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060051 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060052 __func__, ret);
Jagan Teki01af1152019-02-28 00:26:55 +053053 debug("%s %d\n", ofnode_get_name(args->node), args->args[0]);
Stephen Warren185ad872016-06-17 09:43:58 -060054 return ret;
55 }
56 ops = reset_dev_ops(dev_reset);
57
58 reset_ctl->dev = dev_reset;
59 if (ops->of_xlate)
Jagan Teki01af1152019-02-28 00:26:55 +053060 ret = ops->of_xlate(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060061 else
Jagan Teki01af1152019-02-28 00:26:55 +053062 ret = reset_of_xlate_default(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060063 if (ret) {
64 debug("of_xlate() failed: %d\n", ret);
65 return ret;
66 }
67
68 ret = ops->request(reset_ctl);
69 if (ret) {
70 debug("ops->request() failed: %d\n", ret);
71 return ret;
72 }
73
74 return 0;
75}
76
Jagan Teki01af1152019-02-28 00:26:55 +053077int reset_get_by_index(struct udevice *dev, int index,
78 struct reset_ctl *reset_ctl)
79{
80 struct ofnode_phandle_args args;
81 int ret;
82
83 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
84 index, &args);
85
86 return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets",
87 index > 0, reset_ctl);
88}
89
90int reset_get_by_index_nodev(ofnode node, int index,
91 struct reset_ctl *reset_ctl)
92{
93 struct ofnode_phandle_args args;
94 int ret;
95
96 ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0,
97 index > 0, &args);
98
99 return reset_get_by_index_tail(ret, node, &args, "resets",
100 index > 0, reset_ctl);
101}
102
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200103int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
104{
105 int i, ret, err, count;
106
107 bulk->count = 0;
108
109 count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
Neil Armstrong6acc49a2018-04-17 11:30:22 +0200110 if (count < 1)
111 return count;
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200112
113 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
114 GFP_KERNEL);
115 if (!bulk->resets)
116 return -ENOMEM;
117
118 for (i = 0; i < count; i++) {
119 ret = reset_get_by_index(dev, i, &bulk->resets[i]);
120 if (ret < 0)
121 goto bulk_get_err;
122
123 ++bulk->count;
124 }
125
126 return 0;
127
128bulk_get_err:
129 err = reset_release_all(bulk->resets, bulk->count);
130 if (err)
131 debug("%s: could release all resets for %p\n",
132 __func__, dev);
133
134 return ret;
135}
136
Stephen Warren185ad872016-06-17 09:43:58 -0600137int reset_get_by_name(struct udevice *dev, const char *name,
138 struct reset_ctl *reset_ctl)
139{
140 int index;
141
142 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
143 reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200144 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -0600145
Simon Glassaec8cc02017-05-18 20:09:50 -0600146 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren185ad872016-06-17 09:43:58 -0600147 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600148 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren185ad872016-06-17 09:43:58 -0600149 return index;
150 }
151
152 return reset_get_by_index(dev, index, reset_ctl);
153}
154
Patrice Chotard76290e32017-07-18 11:57:05 +0200155int reset_request(struct reset_ctl *reset_ctl)
156{
157 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
158
159 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
160
161 return ops->request(reset_ctl);
162}
163
Stephen Warren185ad872016-06-17 09:43:58 -0600164int reset_free(struct reset_ctl *reset_ctl)
165{
166 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
167
168 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
169
Simon Glass1928cd42020-02-03 07:35:52 -0700170 return ops->rfree(reset_ctl);
Stephen Warren185ad872016-06-17 09:43:58 -0600171}
172
173int reset_assert(struct reset_ctl *reset_ctl)
174{
175 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
176
177 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
178
179 return ops->rst_assert(reset_ctl);
180}
181
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200182int reset_assert_bulk(struct reset_ctl_bulk *bulk)
183{
184 int i, ret;
185
186 for (i = 0; i < bulk->count; i++) {
187 ret = reset_assert(&bulk->resets[i]);
188 if (ret < 0)
189 return ret;
190 }
191
192 return 0;
193}
194
Stephen Warren185ad872016-06-17 09:43:58 -0600195int reset_deassert(struct reset_ctl *reset_ctl)
196{
197 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
198
199 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
200
201 return ops->rst_deassert(reset_ctl);
202}
203
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200204int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
205{
206 int i, ret;
207
208 for (i = 0; i < bulk->count; i++) {
209 ret = reset_deassert(&bulk->resets[i]);
210 if (ret < 0)
211 return ret;
212 }
213
214 return 0;
215}
216
Andreas Dannenberge2bc7402018-08-27 15:57:39 +0530217int reset_status(struct reset_ctl *reset_ctl)
218{
219 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
220
221 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
222
223 return ops->rst_status(reset_ctl);
224}
225
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200226int reset_release_all(struct reset_ctl *reset_ctl, int count)
227{
228 int i, ret;
229
230 for (i = 0; i < count; i++) {
231 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
232
233 /* check if reset has been previously requested */
234 if (!reset_ctl[i].dev)
235 continue;
236
237 ret = reset_assert(&reset_ctl[i]);
238 if (ret)
239 return ret;
240
241 ret = reset_free(&reset_ctl[i]);
242 if (ret)
243 return ret;
244 }
245
246 return 0;
247}
248
Stephen Warren185ad872016-06-17 09:43:58 -0600249UCLASS_DRIVER(reset) = {
250 .id = UCLASS_RESET,
251 .name = "reset",
252};