blob: 98c15e585dfa81d36313899ffacdcaee9ea57ca6 [file] [log] [blame]
Simon Glass6494d702014-02-26 15:59:18 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * (C) Copyright 2012
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <errno.h>
12#include <malloc.h>
13#include <dm/device.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/uclass.h>
17#include <dm/uclass-internal.h>
18#include <dm/util.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22struct uclass *uclass_find(enum uclass_id key)
23{
24 struct uclass *uc;
25
Simon Glassc910e2e2014-07-23 06:55:15 -060026 if (!gd->dm_root)
27 return NULL;
Simon Glass6494d702014-02-26 15:59:18 -070028 /*
29 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
30 * node to the start of the list, or creating a linear array mapping
31 * id to node.
32 */
33 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
34 if (uc->uc_drv->id == key)
35 return uc;
36 }
37
38 return NULL;
39}
40
41/**
42 * uclass_add() - Create new uclass in list
43 * @id: Id number to create
44 * @ucp: Returns pointer to uclass, or NULL on error
45 * @return 0 on success, -ve on error
46 *
47 * The new uclass is added to the list. There must be only one uclass for
48 * each id.
49 */
50static int uclass_add(enum uclass_id id, struct uclass **ucp)
51{
52 struct uclass_driver *uc_drv;
53 struct uclass *uc;
54 int ret;
55
56 *ucp = NULL;
57 uc_drv = lists_uclass_lookup(id);
58 if (!uc_drv) {
59 dm_warn("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
60 id);
61 return -ENOENT;
62 }
Simon Glass6494d702014-02-26 15:59:18 -070063 uc = calloc(1, sizeof(*uc));
64 if (!uc)
65 return -ENOMEM;
66 if (uc_drv->priv_auto_alloc_size) {
67 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
68 if (!uc->priv) {
69 ret = -ENOMEM;
70 goto fail_mem;
71 }
72 }
73 uc->uc_drv = uc_drv;
74 INIT_LIST_HEAD(&uc->sibling_node);
75 INIT_LIST_HEAD(&uc->dev_head);
Simon Glass89876a52014-06-11 23:29:49 -060076 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -070077
78 if (uc_drv->init) {
79 ret = uc_drv->init(uc);
80 if (ret)
81 goto fail;
82 }
83
84 *ucp = uc;
85
86 return 0;
87fail:
88 if (uc_drv->priv_auto_alloc_size) {
89 free(uc->priv);
90 uc->priv = NULL;
91 }
92 list_del(&uc->sibling_node);
93fail_mem:
94 free(uc);
95
96 return ret;
97}
98
99int uclass_destroy(struct uclass *uc)
100{
101 struct uclass_driver *uc_drv;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200102 struct udevice *dev, *tmp;
Simon Glass6494d702014-02-26 15:59:18 -0700103 int ret;
104
105 list_for_each_entry_safe(dev, tmp, &uc->dev_head, uclass_node) {
106 ret = device_remove(dev);
107 if (ret)
108 return ret;
109 ret = device_unbind(dev);
110 if (ret)
111 return ret;
112 }
113
114 uc_drv = uc->uc_drv;
115 if (uc_drv->destroy)
116 uc_drv->destroy(uc);
117 list_del(&uc->sibling_node);
118 if (uc_drv->priv_auto_alloc_size)
119 free(uc->priv);
120 free(uc);
121
122 return 0;
123}
124
125int uclass_get(enum uclass_id id, struct uclass **ucp)
126{
127 struct uclass *uc;
128
129 *ucp = NULL;
130 uc = uclass_find(id);
131 if (!uc)
132 return uclass_add(id, ucp);
133 *ucp = uc;
134
135 return 0;
136}
137
Heiko Schocher54c5d082014-05-22 12:43:05 +0200138int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700139{
140 struct uclass *uc;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200141 struct udevice *dev;
Simon Glass6494d702014-02-26 15:59:18 -0700142 int ret;
143
144 *devp = NULL;
145 ret = uclass_get(id, &uc);
146 if (ret)
147 return ret;
148
149 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
150 if (!index--) {
151 *devp = dev;
152 return 0;
153 }
154 }
155
156 return -ENODEV;
157}
158
Simon Glass5a66a8f2014-07-23 06:55:12 -0600159int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
160 bool find_req_seq, struct udevice **devp)
161{
162 struct uclass *uc;
163 struct udevice *dev;
164 int ret;
165
166 *devp = NULL;
167 debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq);
168 if (seq_or_req_seq == -1)
169 return -ENODEV;
170 ret = uclass_get(id, &uc);
171 if (ret)
172 return ret;
173
174 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
175 debug(" - %d %d\n", dev->req_seq, dev->seq);
176 if ((find_req_seq ? dev->req_seq : dev->seq) ==
177 seq_or_req_seq) {
178 *devp = dev;
179 debug(" - found\n");
180 return 0;
181 }
182 }
183 debug(" - not found\n");
184
185 return -ENODEV;
186}
187
Simon Glassf4cdead2014-07-23 06:55:14 -0600188static int uclass_find_device_by_of_offset(enum uclass_id id, int node,
189 struct udevice **devp)
190{
191 struct uclass *uc;
192 struct udevice *dev;
193 int ret;
194
195 *devp = NULL;
196 if (node < 0)
197 return -ENODEV;
198 ret = uclass_get(id, &uc);
199 if (ret)
200 return ret;
201
202 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
203 if (dev->of_offset == node) {
204 *devp = dev;
205 return 0;
206 }
207 }
208
209 return -ENODEV;
210}
211
Simon Glass9ca296a2014-07-23 06:55:08 -0600212/**
213 * uclass_get_device_tail() - handle the end of a get_device call
214 *
215 * This handles returning an error or probing a device as needed.
216 *
217 * @dev: Device that needs to be probed
218 * @ret: Error to return. If non-zero then the device is not probed
219 * @devp: Returns the value of 'dev' if there is no error
220 * @return ret, if non-zero, else the result of the device_probe() call
221 */
222static int uclass_get_device_tail(struct udevice *dev, int ret,
223 struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700224{
Simon Glass6494d702014-02-26 15:59:18 -0700225 if (ret)
226 return ret;
227
228 ret = device_probe(dev);
229 if (ret)
230 return ret;
231
232 *devp = dev;
233
234 return 0;
235}
236
Simon Glass9ca296a2014-07-23 06:55:08 -0600237int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
238{
239 struct udevice *dev;
240 int ret;
241
242 *devp = NULL;
243 ret = uclass_find_device(id, index, &dev);
244 return uclass_get_device_tail(dev, ret, devp);
245}
246
Simon Glass5a66a8f2014-07-23 06:55:12 -0600247int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
248{
249 struct udevice *dev;
250 int ret;
251
252 *devp = NULL;
253 ret = uclass_find_device_by_seq(id, seq, false, &dev);
254 if (ret == -ENODEV) {
255 /*
256 * We didn't find it in probed devices. See if there is one
257 * that will request this seq if probed.
258 */
259 ret = uclass_find_device_by_seq(id, seq, true, &dev);
260 }
261 return uclass_get_device_tail(dev, ret, devp);
262}
263
Simon Glassf4cdead2014-07-23 06:55:14 -0600264int uclass_get_device_by_of_offset(enum uclass_id id, int node,
265 struct udevice **devp)
266{
267 struct udevice *dev;
268 int ret;
269
270 *devp = NULL;
271 ret = uclass_find_device_by_of_offset(id, node, &dev);
272 return uclass_get_device_tail(dev, ret, devp);
273}
274
Heiko Schocher54c5d082014-05-22 12:43:05 +0200275int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700276{
277 struct uclass *uc;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200278 struct udevice *dev;
Simon Glass6494d702014-02-26 15:59:18 -0700279 int ret;
280
281 *devp = NULL;
282 ret = uclass_get(id, &uc);
283 if (ret)
284 return ret;
285 if (list_empty(&uc->dev_head))
286 return 0;
287
Heiko Schocher54c5d082014-05-22 12:43:05 +0200288 dev = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
Simon Glass6494d702014-02-26 15:59:18 -0700289 ret = device_probe(dev);
290 if (ret)
291 return ret;
292 *devp = dev;
293
294 return 0;
295}
296
Heiko Schocher54c5d082014-05-22 12:43:05 +0200297int uclass_next_device(struct udevice **devp)
Simon Glass6494d702014-02-26 15:59:18 -0700298{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200299 struct udevice *dev = *devp;
Simon Glass6494d702014-02-26 15:59:18 -0700300 int ret;
301
302 *devp = NULL;
303 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
304 return 0;
305
Heiko Schocher54c5d082014-05-22 12:43:05 +0200306 dev = list_entry(dev->uclass_node.next, struct udevice,
307 uclass_node);
Simon Glass6494d702014-02-26 15:59:18 -0700308 ret = device_probe(dev);
309 if (ret)
310 return ret;
311 *devp = dev;
312
313 return 0;
314}
315
Heiko Schocher54c5d082014-05-22 12:43:05 +0200316int uclass_bind_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700317{
318 struct uclass *uc;
319 int ret;
320
321 uc = dev->uclass;
Simon Glass6494d702014-02-26 15:59:18 -0700322 list_add_tail(&dev->uclass_node, &uc->dev_head);
323
Simon Glass081f2fc2015-01-25 08:27:08 -0700324 if (dev->parent) {
325 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
326
327 if (uc_drv->child_post_bind) {
328 ret = uc_drv->child_post_bind(dev);
329 if (ret)
330 goto err;
331 }
332 }
Simon Glass6494d702014-02-26 15:59:18 -0700333 if (uc->uc_drv->post_bind) {
334 ret = uc->uc_drv->post_bind(dev);
Simon Glass081f2fc2015-01-25 08:27:08 -0700335 if (ret)
336 goto err;
Simon Glass6494d702014-02-26 15:59:18 -0700337 }
338
339 return 0;
Simon Glass081f2fc2015-01-25 08:27:08 -0700340err:
341 /* There is no need to undo the parent's post_bind call */
342 list_del(&dev->uclass_node);
343
344 return ret;
Simon Glass6494d702014-02-26 15:59:18 -0700345}
346
Heiko Schocher54c5d082014-05-22 12:43:05 +0200347int uclass_unbind_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700348{
349 struct uclass *uc;
350 int ret;
351
352 uc = dev->uclass;
353 if (uc->uc_drv->pre_unbind) {
354 ret = uc->uc_drv->pre_unbind(dev);
355 if (ret)
356 return ret;
357 }
358
359 list_del(&dev->uclass_node);
360 return 0;
361}
362
Simon Glass5a66a8f2014-07-23 06:55:12 -0600363int uclass_resolve_seq(struct udevice *dev)
364{
365 struct udevice *dup;
366 int seq;
367 int ret;
368
369 assert(dev->seq == -1);
370 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
371 false, &dup);
372 if (!ret) {
373 dm_warn("Device '%s': seq %d is in use by '%s'\n",
374 dev->name, dev->req_seq, dup->name);
375 } else if (ret == -ENODEV) {
376 /* Our requested sequence number is available */
377 if (dev->req_seq != -1)
378 return dev->req_seq;
379 } else {
380 return ret;
381 }
382
383 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
384 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
385 false, &dup);
386 if (ret == -ENODEV)
387 break;
388 if (ret)
389 return ret;
390 }
391 return seq;
392}
393
Simon Glass02c07b32015-03-05 12:25:22 -0700394int uclass_pre_probe_device(struct udevice *dev)
Simon Glass83c7e432015-01-25 08:27:10 -0700395{
396 struct uclass_driver *uc_drv;
Simon Glass02c07b32015-03-05 12:25:22 -0700397 int ret;
398
399 uc_drv = dev->uclass->uc_drv;
400 if (uc_drv->pre_probe) {
401 ret = uc_drv->pre_probe(dev);
402 if (ret)
403 return ret;
404 }
Simon Glass83c7e432015-01-25 08:27:10 -0700405
406 if (!dev->parent)
407 return 0;
408 uc_drv = dev->parent->uclass->uc_drv;
409 if (uc_drv->child_pre_probe)
410 return uc_drv->child_pre_probe(dev);
411
412 return 0;
413}
414
Heiko Schocher54c5d082014-05-22 12:43:05 +0200415int uclass_post_probe_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700416{
417 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
418
419 if (uc_drv->post_probe)
420 return uc_drv->post_probe(dev);
421
422 return 0;
423}
424
Heiko Schocher54c5d082014-05-22 12:43:05 +0200425int uclass_pre_remove_device(struct udevice *dev)
Simon Glass6494d702014-02-26 15:59:18 -0700426{
427 struct uclass_driver *uc_drv;
428 struct uclass *uc;
429 int ret;
430
431 uc = dev->uclass;
432 uc_drv = uc->uc_drv;
433 if (uc->uc_drv->pre_remove) {
434 ret = uc->uc_drv->pre_remove(dev);
435 if (ret)
436 return ret;
437 }
438 if (uc_drv->per_device_auto_alloc_size) {
439 free(dev->uclass_priv);
440 dev->uclass_priv = NULL;
441 }
Simon Glass5a66a8f2014-07-23 06:55:12 -0600442 dev->seq = -1;
Simon Glass6494d702014-02-26 15:59:18 -0700443
444 return 0;
445}