blob: 9a5c9c91b9db88114ff9a1d2ee3cff8e985e92e9 [file] [log] [blame]
Stephen Warren89c1e2d2016-06-17 09:43:58 -06001/*
2 * Copyright (c) 2016, NVIDIA CORPORATION.
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <fdtdec.h>
10#include <reset.h>
11#include <reset-uclass.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15static 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 Glass40a475e2017-05-18 20:09:50 -060021 struct ofnode_phandle_args *args)
Stephen Warren89c1e2d2016-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
35int reset_get_by_index(struct udevice *dev, int index,
36 struct reset_ctl *reset_ctl)
37{
Simon Glass40a475e2017-05-18 20:09:50 -060038 struct ofnode_phandle_args args;
Stephen Warren89c1e2d2016-06-17 09:43:58 -060039 int ret;
40 struct udevice *dev_reset;
41 struct reset_ops *ops;
42
43 debug("%s(dev=%p, index=%d, reset_ctl=%p)\n", __func__, dev, index,
44 reset_ctl);
Patrice Chotard3b9d1bd2017-07-18 11:57:06 +020045 reset_ctl->dev = NULL;
Stephen Warren89c1e2d2016-06-17 09:43:58 -060046
Simon Glass40a475e2017-05-18 20:09:50 -060047 ret = dev_read_phandle_with_args(dev, "resets", "#reset-cells", 0,
48 index, &args);
Stephen Warren89c1e2d2016-06-17 09:43:58 -060049 if (ret) {
Simon Glass40a475e2017-05-18 20:09:50 -060050 debug("%s: fdtdec_parse_phandle_with_args() failed: %d\n",
Stephen Warren89c1e2d2016-06-17 09:43:58 -060051 __func__, ret);
52 return ret;
53 }
54
Simon Glass40a475e2017-05-18 20:09:50 -060055 ret = uclass_get_device_by_ofnode(UCLASS_RESET, args.node,
56 &dev_reset);
Stephen Warren89c1e2d2016-06-17 09:43:58 -060057 if (ret) {
Simon Glass40a475e2017-05-18 20:09:50 -060058 debug("%s: uclass_get_device_by_ofnode() failed: %d\n",
Stephen Warren89c1e2d2016-06-17 09:43:58 -060059 __func__, ret);
Simon Glass40a475e2017-05-18 20:09:50 -060060 debug("%s %d\n", ofnode_get_name(args.node), args.args[0]);
Stephen Warren89c1e2d2016-06-17 09:43:58 -060061 return ret;
62 }
63 ops = reset_dev_ops(dev_reset);
64
65 reset_ctl->dev = dev_reset;
66 if (ops->of_xlate)
67 ret = ops->of_xlate(reset_ctl, &args);
68 else
69 ret = reset_of_xlate_default(reset_ctl, &args);
70 if (ret) {
71 debug("of_xlate() failed: %d\n", ret);
72 return ret;
73 }
74
75 ret = ops->request(reset_ctl);
76 if (ret) {
77 debug("ops->request() failed: %d\n", ret);
78 return ret;
79 }
80
81 return 0;
82}
83
Neil Armstrong0c282332018-04-03 11:40:50 +020084int reset_get_bulk(struct udevice *dev, struct reset_ctl_bulk *bulk)
85{
86 int i, ret, err, count;
87
88 bulk->count = 0;
89
90 count = dev_count_phandle_with_args(dev, "resets", "#reset-cells");
91 if (!count)
92 return 0;
93
94 bulk->resets = devm_kcalloc(dev, count, sizeof(struct reset_ctl),
95 GFP_KERNEL);
96 if (!bulk->resets)
97 return -ENOMEM;
98
99 for (i = 0; i < count; i++) {
100 ret = reset_get_by_index(dev, i, &bulk->resets[i]);
101 if (ret < 0)
102 goto bulk_get_err;
103
104 ++bulk->count;
105 }
106
107 return 0;
108
109bulk_get_err:
110 err = reset_release_all(bulk->resets, bulk->count);
111 if (err)
112 debug("%s: could release all resets for %p\n",
113 __func__, dev);
114
115 return ret;
116}
117
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600118int reset_get_by_name(struct udevice *dev, const char *name,
119 struct reset_ctl *reset_ctl)
120{
121 int index;
122
123 debug("%s(dev=%p, name=%s, reset_ctl=%p)\n", __func__, dev, name,
124 reset_ctl);
Patrice Chotard3b9d1bd2017-07-18 11:57:06 +0200125 reset_ctl->dev = NULL;
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600126
Simon Glass40a475e2017-05-18 20:09:50 -0600127 index = dev_read_stringlist_search(dev, "reset-names", name);
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600128 if (index < 0) {
Simon Glassb02e4042016-10-02 17:59:28 -0600129 debug("fdt_stringlist_search() failed: %d\n", index);
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600130 return index;
131 }
132
133 return reset_get_by_index(dev, index, reset_ctl);
134}
135
Patrice Chotard9bd5cdf2017-07-18 11:57:05 +0200136int reset_request(struct reset_ctl *reset_ctl)
137{
138 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
139
140 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
141
142 return ops->request(reset_ctl);
143}
144
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600145int reset_free(struct reset_ctl *reset_ctl)
146{
147 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
148
149 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
150
151 return ops->free(reset_ctl);
152}
153
154int reset_assert(struct reset_ctl *reset_ctl)
155{
156 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
157
158 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
159
160 return ops->rst_assert(reset_ctl);
161}
162
Neil Armstrong0c282332018-04-03 11:40:50 +0200163int reset_assert_bulk(struct reset_ctl_bulk *bulk)
164{
165 int i, ret;
166
167 for (i = 0; i < bulk->count; i++) {
168 ret = reset_assert(&bulk->resets[i]);
169 if (ret < 0)
170 return ret;
171 }
172
173 return 0;
174}
175
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600176int reset_deassert(struct reset_ctl *reset_ctl)
177{
178 struct reset_ops *ops = reset_dev_ops(reset_ctl->dev);
179
180 debug("%s(reset_ctl=%p)\n", __func__, reset_ctl);
181
182 return ops->rst_deassert(reset_ctl);
183}
184
Neil Armstrong0c282332018-04-03 11:40:50 +0200185int reset_deassert_bulk(struct reset_ctl_bulk *bulk)
186{
187 int i, ret;
188
189 for (i = 0; i < bulk->count; i++) {
190 ret = reset_deassert(&bulk->resets[i]);
191 if (ret < 0)
192 return ret;
193 }
194
195 return 0;
196}
197
Patrice Chotard3b9d1bd2017-07-18 11:57:06 +0200198int reset_release_all(struct reset_ctl *reset_ctl, int count)
199{
200 int i, ret;
201
202 for (i = 0; i < count; i++) {
203 debug("%s(reset_ctl[%d]=%p)\n", __func__, i, &reset_ctl[i]);
204
205 /* check if reset has been previously requested */
206 if (!reset_ctl[i].dev)
207 continue;
208
209 ret = reset_assert(&reset_ctl[i]);
210 if (ret)
211 return ret;
212
213 ret = reset_free(&reset_ctl[i]);
214 if (ret)
215 return ret;
216 }
217
218 return 0;
219}
220
Stephen Warren89c1e2d2016-06-17 09:43:58 -0600221UCLASS_DRIVER(reset) = {
222 .id = UCLASS_RESET,
223 .name = "reset",
224};