blob: ee1a423ffbc8f534bae753196b6bc321ae426a76 [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>
11
Stephen Warren185ad872016-06-17 09:43:58 -060012static inline struct reset_ops *reset_dev_ops(struct udevice *dev)
13{
14 return (struct reset_ops *)dev->driver->ops;
15}
16
17static int reset_of_xlate_default(struct reset_ctl *reset_ctl,
Simon Glassaec8cc02017-05-18 20:09:50 -060018 struct ofnode_phandle_args *args)
Stephen Warren185ad872016-06-17 09:43:58 -060019{
20 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
21
22 if (args->args_count != 1) {
23 debug("Invaild args_count: %d\n", args->args_count);
24 return -EINVAL;
25 }
26
27 reset_ctl->id = args->args[0];
28
29 return 0;
30}
31
Jagan Teki01af1152019-02-28 00:26:55 +053032static int reset_get_by_index_tail(int ret, ofnode node,
33 struct ofnode_phandle_args *args,
34 const char *list_name, int index,
35 struct reset_ctl *reset_ctl)
Stephen Warren185ad872016-06-17 09:43:58 -060036{
Stephen Warren185ad872016-06-17 09:43:58 -060037 struct udevice *dev_reset;
38 struct reset_ops *ops;
39
Jagan Teki01af1152019-02-28 00:26:55 +053040 assert(reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +020041 reset_ctl->dev = NULL;
Jagan Teki01af1152019-02-28 00:26:55 +053042 if (ret)
Stephen Warren185ad872016-06-17 09:43:58 -060043 return ret;
Stephen Warren185ad872016-06-17 09:43:58 -060044
Jagan Teki01af1152019-02-28 00:26:55 +053045 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args->node,
Simon Glassaec8cc02017-05-18 20:09:50 -060046 &dev_reset);
Stephen Warren185ad872016-06-17 09:43:58 -060047 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060048 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060049 __func__, ret);
Jagan Teki01af1152019-02-28 00:26:55 +053050 debug("%s %d\n", ofnode_get_name(args->node), args->args[0]);
Stephen Warren185ad872016-06-17 09:43:58 -060051 return ret;
52 }
53 ops = reset_dev_ops(dev_reset);
54
55 reset_ctl->dev = dev_reset;
56 if (ops->of_xlate)
Jagan Teki01af1152019-02-28 00:26:55 +053057 ret = ops->of_xlate(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060058 else
Jagan Teki01af1152019-02-28 00:26:55 +053059 ret = reset_of_xlate_default(reset_ctl, args);
Stephen Warren185ad872016-06-17 09:43:58 -060060 if (ret) {
61 debug("of_xlate() failed: %d\n", ret);
62 return ret;
63 }
64
65 ret = ops->request(reset_ctl);
66 if (ret) {
67 debug("ops->request() failed: %d\n", ret);
68 return ret;
69 }
70
71 return 0;
72}
73
Jagan Teki01af1152019-02-28 00:26:55 +053074int reset_get_by_index(struct udevice *dev, int index,
75 struct reset_ctl *reset_ctl)
76{
77 struct ofnode_phandle_args args;
78 int ret;
79
80 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
81 index, &args);
82
83 return reset_get_by_index_tail(ret, dev_ofnode(dev), &args, "resets",
84 index > 0, reset_ctl);
85}
86
87int reset_get_by_index_nodev(ofnode node, int index,
88 struct reset_ctl *reset_ctl)
89{
90 struct ofnode_phandle_args args;
91 int ret;
92
93 ret = ofnode_parse_phandle_with_args(node, "resets", "#reset-cells", 0,
94 index > 0, &args);
95
96 return reset_get_by_index_tail(ret, node, &args, "resets",
97 index > 0, reset_ctl);
98}
99
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200100int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
101{
102 int i, ret, err, count;
103
104 bulk->count = 0;
105
106 count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
Neil Armstrong6acc49a2018-04-17 11:30:22 +0200107 if (count < 1)
108 return count;
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200109
110 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
111 GFP_KERNEL);
112 if (!bulk->resets)
113 return -ENOMEM;
114
115 for (i = 0; i < count; i++) {
116 ret = reset_get_by_index(dev, i, &bulk->resets[i]);
117 if (ret < 0)
118 goto bulk_get_err;
119
120 ++bulk->count;
121 }
122
123 return 0;
124
125bulk_get_err:
126 err = reset_release_all(bulk->resets, bulk->count);
127 if (err)
128 debug("%s: could release all resets for %p\n",
129 __func__, dev);
130
131 return ret;
132}
133
Stephen Warren185ad872016-06-17 09:43:58 -0600134int reset_get_by_name(struct udevice *dev, const char *name,
135 struct reset_ctl *reset_ctl)
136{
137 int index;
138
139 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
140 reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200141 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -0600142
Simon Glassaec8cc02017-05-18 20:09:50 -0600143 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren185ad872016-06-17 09:43:58 -0600144 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600145 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren185ad872016-06-17 09:43:58 -0600146 return index;
147 }
148
149 return reset_get_by_index(dev, index, reset_ctl);
150}
151
Patrice Chotard76290e32017-07-18 11:57:05 +0200152int reset_request(struct reset_ctl *reset_ctl)
153{
154 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
155
156 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
157
158 return ops->request(reset_ctl);
159}
160
Stephen Warren185ad872016-06-17 09:43:58 -0600161int reset_free(struct reset_ctl *reset_ctl)
162{
163 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
164
165 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
166
167 return ops->free(reset_ctl);
168}
169
170int reset_assert(struct reset_ctl *reset_ctl)
171{
172 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
173
174 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
175
176 return ops->rst_assert(reset_ctl);
177}
178
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200179int reset_assert_bulk(struct reset_ctl_bulk *bulk)
180{
181 int i, ret;
182
183 for (i = 0; i < bulk->count; i++) {
184 ret = reset_assert(&bulk->resets[i]);
185 if (ret < 0)
186 return ret;
187 }
188
189 return 0;
190}
191
Stephen Warren185ad872016-06-17 09:43:58 -0600192int reset_deassert(struct reset_ctl *reset_ctl)
193{
194 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
195
196 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
197
198 return ops->rst_deassert(reset_ctl);
199}
200
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200201int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
202{
203 int i, ret;
204
205 for (i = 0; i < bulk->count; i++) {
206 ret = reset_deassert(&bulk->resets[i]);
207 if (ret < 0)
208 return ret;
209 }
210
211 return 0;
212}
213
Andreas Dannenberge2bc7402018-08-27 15:57:39 +0530214int reset_status(struct reset_ctl *reset_ctl)
215{
216 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
217
218 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
219
220 return ops->rst_status(reset_ctl);
221}
222
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200223int reset_release_all(struct reset_ctl *reset_ctl, int count)
224{
225 int i, ret;
226
227 for (i = 0; i < count; i++) {
228 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
229
230 /* check if reset has been previously requested */
231 if (!reset_ctl[i].dev)
232 continue;
233
234 ret = reset_assert(&reset_ctl[i]);
235 if (ret)
236 return ret;
237
238 ret = reset_free(&reset_ctl[i]);
239 if (ret)
240 return ret;
241 }
242
243 return 0;
244}
245
Stephen Warren185ad872016-06-17 09:43:58 -0600246UCLASS_DRIVER(reset) = {
247 .id = UCLASS_RESET,
248 .name = "reset",
249};