blob: 8e6c0a4fd0963a3b38d5617dee6f8dbbf064cfa4 [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>
9#include <reset.h>
10#include <reset-uclass.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070011#include <dm/devres.h>
Stephen Warren185ad872016-06-17 09:43:58 -060012
Stephen Warren185ad872016-06-17 09:43:58 -060013static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
14{
15 return (struct reset_ops *)dev->driver->ops;
16}
17
18static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
Simon Glassaec8cc02017-05-18 20:09:50 -060019 struct ofnode_phandle_args *args)
Stephen Warren185ad872016-06-17 09:43:58 -060020{
21 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
22
23 if (args->args_count != 1) {
24 debug("Invaild args_count: %d\n", args->args_count);
25 return -EINVAL;
26 }
27
28 reset_ctl->id = args->args[0];
29
30 return 0;
31}
32
Jagan Teki01af1152019-02-28 00:26:55 +053033static int reset_get_by_index_tail(int ret, ofnode node,
34 struct ofnode_phandle_args *args,
35 const char *list_name, int index,
36 struct reset_ctl *reset_ctl)
Stephen Warren185ad872016-06-17 09:43:58 -060037{
Stephen Warren185ad872016-06-17 09:43:58 -060038 struct udevice *dev_reset;
39 struct reset_ops *ops;
40
Jagan Teki01af1152019-02-28 00:26:55 +053041 assert(reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +020042 reset_ctl->dev = NULL;
Jagan Teki01af1152019-02-28 00:26:55 +053043 if (ret)
Stephen Warren185ad872016-06-17 09:43:58 -060044 return ret;
Stephen Warren185ad872016-06-17 09:43:58 -060045
Jagan Teki01af1152019-02-28 00:26:55 +053046 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node,
Simon Glassaec8cc02017-05-18 20:09:50 -060047 &dev_reset);
Stephen Warren185ad872016-06-17 09:43:58 -060048 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060049 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060050 __func__, ret);
Jagan Teki01af1152019-02-28 00:26:55 +053051 debug("%s %d\n", ofnode_get_name(args->node), args->args[0]);
Stephen Warren185ad872016-06-17 09:43:58 -060052 return ret;
53 }
54 ops = reset_dev_ops(dev_reset);
55
56 reset_ctl->dev = dev_reset;
57 if (ops->of_xlate)
Jagan Teki01af1152019-02-28 00:26:55 +053058 ret = ops->of_xlate(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060059 else
Jagan Teki01af1152019-02-28 00:26:55 +053060 ret = reset_of_xlate_default(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060061 if (ret) {
62 debug("of_xlate() failed: %d\n", ret);
63 return ret;
64 }
65
66 ret = ops->request(reset_ctl);
67 if (ret) {
68 debug("ops->request() failed: %d\n", ret);
69 return ret;
70 }
71
72 return 0;
73}
74
Jagan Teki01af1152019-02-28 00:26:55 +053075int reset_get_by_index(struct udevice *dev, int index,
76 struct reset_ctl *reset_ctl)
77{
78 struct ofnode_phandle_args args;
79 int ret;
80
81 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
82 index, &args);
83
84 return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets",
85 index > 0, reset_ctl);
86}
87
88int reset_get_by_index_nodev(ofnode node, int index,
89 struct reset_ctl *reset_ctl)
90{
91 struct ofnode_phandle_args args;
92 int ret;
93
94 ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0,
95 index > 0, &args);
96
97 return reset_get_by_index_tail(ret, node, &args, "resets",
98 index > 0, reset_ctl);
99}
100
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200101int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
102{
103 int i, ret, err, count;
104
105 bulk->count = 0;
106
107 count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
Neil Armstrong6acc49a2018-04-17 11:30:22 +0200108 if (count < 1)
109 return count;
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200110
111 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
112 GFP_KERNEL);
113 if (!bulk->resets)
114 return -ENOMEM;
115
116 for (i = 0; i < count; i++) {
117 ret = reset_get_by_index(dev, i, &bulk->resets[i]);
118 if (ret < 0)
119 goto bulk_get_err;
120
121 ++bulk->count;
122 }
123
124 return 0;
125
126bulk_get_err:
127 err = reset_release_all(bulk->resets, bulk->count);
128 if (err)
129 debug("%s: could release all resets for %p\n",
130 __func__, dev);
131
132 return ret;
133}
134
Stephen Warren185ad872016-06-17 09:43:58 -0600135int reset_get_by_name(struct udevice *dev, const char *name,
136 struct reset_ctl *reset_ctl)
137{
138 int index;
139
140 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
141 reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200142 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -0600143
Simon Glassaec8cc02017-05-18 20:09:50 -0600144 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren185ad872016-06-17 09:43:58 -0600145 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600146 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren185ad872016-06-17 09:43:58 -0600147 return index;
148 }
149
150 return reset_get_by_index(dev, index, reset_ctl);
151}
152
Patrice Chotard76290e32017-07-18 11:57:05 +0200153int reset_request(struct reset_ctl *reset_ctl)
154{
155 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
156
157 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
158
159 return ops->request(reset_ctl);
160}
161
Stephen Warren185ad872016-06-17 09:43:58 -0600162int reset_free(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
Simon Glass1928cd42020-02-03 07:35:52 -0700168 return ops->rfree(reset_ctl);
Stephen Warren185ad872016-06-17 09:43:58 -0600169}
170
171int reset_assert(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
177 return ops->rst_assert(reset_ctl);
178}
179
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200180int reset_assert_bulk(struct reset_ctl_bulk *bulk)
181{
182 int i, ret;
183
184 for (i = 0; i < bulk->count; i++) {
185 ret = reset_assert(&bulk->resets[i]);
186 if (ret < 0)
187 return ret;
188 }
189
190 return 0;
191}
192
Stephen Warren185ad872016-06-17 09:43:58 -0600193int reset_deassert(struct reset_ctl *reset_ctl)
194{
195 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
196
197 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
198
199 return ops->rst_deassert(reset_ctl);
200}
201
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200202int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
203{
204 int i, ret;
205
206 for (i = 0; i < bulk->count; i++) {
207 ret = reset_deassert(&bulk->resets[i]);
208 if (ret < 0)
209 return ret;
210 }
211
212 return 0;
213}
214
Andreas Dannenberge2bc7402018-08-27 15:57:39 +0530215int reset_status(struct reset_ctl *reset_ctl)
216{
217 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
218
219 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
220
221 return ops->rst_status(reset_ctl);
222}
223
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200224int reset_release_all(struct reset_ctl *reset_ctl, int count)
225{
226 int i, ret;
227
228 for (i = 0; i < count; i++) {
229 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
230
231 /* check if reset has been previously requested */
232 if (!reset_ctl[i].dev)
233 continue;
234
235 ret = reset_assert(&reset_ctl[i]);
236 if (ret)
237 return ret;
238
239 ret = reset_free(&reset_ctl[i]);
240 if (ret)
241 return ret;
242 }
243
244 return 0;
245}
246
Stephen Warren185ad872016-06-17 09:43:58 -0600247UCLASS_DRIVER(reset) = {
248 .id = UCLASS_RESET,
249 .name = "reset",
250};