blob: 89e39c6b5aaf5f1513a612ed08bbf631d628547f [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>
9#include <reset.h>
10#include <reset-uclass.h>
11
Stephen Warren89c1e2d2016-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 Glass40a475e2017-05-18 20:09:50 -060018 struct ofnode_phandle_args *args)
Stephen Warren89c1e2d2016-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 Glass40a475e2017-05-18 20:09:50 -060035 struct ofnode_phandle_args args;
Stephen Warren89c1e2d2016-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 Chotard3b9d1bd2017-07-18 11:57:06 +020042 reset_ctl->dev = NULL;
Stephen Warren89c1e2d2016-06-17 09:43:58 -060043
Simon Glass40a475e2017-05-18 20:09:50 -060044 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
45 index, &args);
Stephen Warren89c1e2d2016-06-17 09:43:58 -060046 if (ret) {
Simon Glass40a475e2017-05-18 20:09:50 -060047 debug("%s: fdtdec_parse_phandle_with_args() failed: %d\n",
Stephen Warren89c1e2d2016-06-17 09:43:58 -060048 __func__, ret);
49 return ret;
50 }
51
Simon Glass40a475e2017-05-18 20:09:50 -060052 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args.node,
53 &dev_reset);
Stephen Warren89c1e2d2016-06-17 09:43:58 -060054 if (ret) {
Simon Glass40a475e2017-05-18 20:09:50 -060055 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren89c1e2d2016-06-17 09:43:58 -060056 __func__, ret);
Simon Glass40a475e2017-05-18 20:09:50 -060057 debug("%s %d\n", ofnode_get_name(args.node), args.args[0]);
Stephen Warren89c1e2d2016-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 Armstrong0c282332018-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 Armstrong895a82c2018-04-17 11:30:22 +020088 if (count < 1)
89 return count;
Neil Armstrong0c282332018-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 Warren89c1e2d2016-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 Chotard3b9d1bd2017-07-18 11:57:06 +0200122 reset_ctl->dev = NULL;
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600123
Simon Glass40a475e2017-05-18 20:09:50 -0600124 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600125 if (index < 0) {
Simon Glassb02e4042016-10-02 17:59:28 -0600126 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600127 return index;
128 }
129
130 return reset_get_by_index(dev, index, reset_ctl);
131}
132
Patrice Chotard9bd5cdf2017-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 Warren89c1e2d2016-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 Armstrong0c282332018-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 Warren89c1e2d2016-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 Armstrong0c282332018-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
Andreas Dannenberge7012e62018-08-27 15:57:39 +0530195int reset_status(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_status(reset_ctl);
202}
203
Patrice Chotard3b9d1bd2017-07-18 11:57:06 +0200204int reset_release_all(struct reset_ctl *reset_ctl, int count)
205{
206 int i, ret;
207
208 for (i = 0; i < count; i++) {
209 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
210
211 /* check if reset has been previously requested */
212 if (!reset_ctl[i].dev)
213 continue;
214
215 ret = reset_assert(&reset_ctl[i]);
216 if (ret)
217 return ret;
218
219 ret = reset_free(&reset_ctl[i]);
220 if (ret)
221 return ret;
222 }
223
224 return 0;
225}
226
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600227UCLASS_DRIVER(reset) = {
228 .id = UCLASS_RESET,
229 .name = "reset",
230};