blob: 38995376350b757aae7efb9114d2eac5763bb4e0 [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
32int reset_get_by_index(struct udevice *dev, int index,
33 struct reset_ctl *reset_ctl)
34{
Simon Glassaec8cc02017-05-18 20:09:50 -060035 struct ofnode_phandle_args args;
Stephen Warren185ad872016-06-17 09:43:58 -060036 int ret;
37 struct udevice *dev_reset;
38 struct reset_ops *ops;
39
40 debug("%s(dev=%p, index=%d, reset_ctl=%p)\n", __func__, dev, index,
41 reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +020042 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -060043
Simon Glassaec8cc02017-05-18 20:09:50 -060044 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
45 index, &args);
Stephen Warren185ad872016-06-17 09:43:58 -060046 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060047 debug("%s: fdtdec_parse_phandle_with_args() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060048 __func__, ret);
49 return ret;
50 }
51
Simon Glassaec8cc02017-05-18 20:09:50 -060052 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args.node,
53 &dev_reset);
Stephen Warren185ad872016-06-17 09:43:58 -060054 if (ret) {
Simon Glassaec8cc02017-05-18 20:09:50 -060055 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren185ad872016-06-17 09:43:58 -060056 __func__, ret);
Simon Glassaec8cc02017-05-18 20:09:50 -060057 debug("%s %d\n", ofnode_get_name(args.node), args.args[0]);
Stephen Warren185ad872016-06-17 09:43:58 -060058 return ret;
59 }
60 ops = reset_dev_ops(dev_reset);
61
62 reset_ctl->dev = dev_reset;
63 if (ops->of_xlate)
64 ret = ops->of_xlate(reset_ctl, &args);
65 else
66 ret = reset_of_xlate_default(reset_ctl, &args);
67 if (ret) {
68 debug("of_xlate() failed: %d\n", ret);
69 return ret;
70 }
71
72 ret = ops->request(reset_ctl);
73 if (ret) {
74 debug("ops->request() failed: %d\n", ret);
75 return ret;
76 }
77
78 return 0;
79}
80
Neil Armstrongdbb26d32018-04-03 11:40:50 +020081int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
82{
83 int i, ret, err, count;
84
85 bulk->count = 0;
86
87 count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
Neil Armstrong6acc49a2018-04-17 11:30:22 +020088 if (count < 1)
89 return count;
Neil Armstrongdbb26d32018-04-03 11:40:50 +020090
91 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
92 GFP_KERNEL);
93 if (!bulk->resets)
94 return -ENOMEM;
95
96 for (i = 0; i < count; i++) {
97 ret = reset_get_by_index(dev, i, &bulk->resets[i]);
98 if (ret < 0)
99 goto bulk_get_err;
100
101 ++bulk->count;
102 }
103
104 return 0;
105
106bulk_get_err:
107 err = reset_release_all(bulk->resets, bulk->count);
108 if (err)
109 debug("%s: could release all resets for %p\n",
110 __func__, dev);
111
112 return ret;
113}
114
Stephen Warren185ad872016-06-17 09:43:58 -0600115int reset_get_by_name(struct udevice *dev, const char *name,
116 struct reset_ctl *reset_ctl)
117{
118 int index;
119
120 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
121 reset_ctl);
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200122 reset_ctl->dev = NULL;
Stephen Warren185ad872016-06-17 09:43:58 -0600123
Simon Glassaec8cc02017-05-18 20:09:50 -0600124 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren185ad872016-06-17 09:43:58 -0600125 if (index < 0) {
Simon Glassb0ea7402016-10-02 17:59:28 -0600126 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren185ad872016-06-17 09:43:58 -0600127 return index;
128 }
129
130 return reset_get_by_index(dev, index, reset_ctl);
131}
132
Patrice Chotard76290e32017-07-18 11:57:05 +0200133int reset_request(struct reset_ctl *reset_ctl)
134{
135 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
136
137 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
138
139 return ops->request(reset_ctl);
140}
141
Stephen Warren185ad872016-06-17 09:43:58 -0600142int reset_free(struct reset_ctl *reset_ctl)
143{
144 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
145
146 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
147
148 return ops->free(reset_ctl);
149}
150
151int reset_assert(struct reset_ctl *reset_ctl)
152{
153 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
154
155 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
156
157 return ops->rst_assert(reset_ctl);
158}
159
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200160int reset_assert_bulk(struct reset_ctl_bulk *bulk)
161{
162 int i, ret;
163
164 for (i = 0; i < bulk->count; i++) {
165 ret = reset_assert(&bulk->resets[i]);
166 if (ret < 0)
167 return ret;
168 }
169
170 return 0;
171}
172
Stephen Warren185ad872016-06-17 09:43:58 -0600173int reset_deassert(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_deassert(reset_ctl);
180}
181
Neil Armstrongdbb26d32018-04-03 11:40:50 +0200182int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
183{
184 int i, ret;
185
186 for (i = 0; i < bulk->count; i++) {
187 ret = reset_deassert(&bulk->resets[i]);
188 if (ret < 0)
189 return ret;
190 }
191
192 return 0;
193}
194
Patrice Chotarde4d368e2017-07-18 11:57:06 +0200195int reset_release_all(struct reset_ctl *reset_ctl, int count)
196{
197 int i, ret;
198
199 for (i = 0; i < count; i++) {
200 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
201
202 /* check if reset has been previously requested */
203 if (!reset_ctl[i].dev)
204 continue;
205
206 ret = reset_assert(&reset_ctl[i]);
207 if (ret)
208 return ret;
209
210 ret = reset_free(&reset_ctl[i]);
211 if (ret)
212 return ret;
213 }
214
215 return 0;
216}
217
Stephen Warren185ad872016-06-17 09:43:58 -0600218UCLASS_DRIVER(reset) = {
219 .id = UCLASS_RESET,
220 .name = "reset",
221};