blob: f5e406792209686a897c7395dc80d616f082036f [file] [log] [blame]
Simon Glassdd6ab882014-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>
Simon Glassee145d62017-05-18 20:09:09 -060011#include <dm.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070012#include <errno.h>
13#include <malloc.h>
14#include <dm/device.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
17#include <dm/uclass.h>
18#include <dm/uclass-internal.h>
19#include <dm/util.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23struct uclass *uclass_find(enum uclass_id key)
24{
25 struct uclass *uc;
26
Simon Glassde708672014-07-23 06:55:15 -060027 if (!gd->dm_root)
28 return NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070029 /*
30 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
31 * node to the start of the list, or creating a linear array mapping
32 * id to node.
33 */
34 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
35 if (uc->uc_drv->id == key)
36 return uc;
37 }
38
39 return NULL;
40}
41
42/**
43 * uclass_add() - Create new uclass in list
44 * @id: Id number to create
45 * @ucp: Returns pointer to uclass, or NULL on error
46 * @return 0 on success, -ve on error
47 *
48 * The new uclass is added to the list. There must be only one uclass for
49 * each id.
50 */
51static int uclass_add(enum uclass_id id, struct uclass **ucp)
52{
53 struct uclass_driver *uc_drv;
54 struct uclass *uc;
55 int ret;
56
57 *ucp = NULL;
58 uc_drv = lists_uclass_lookup(id);
59 if (!uc_drv) {
Masahiro Yamada1d06ced2015-07-07 18:51:32 +090060 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
61 id);
Simon Glass43313de2015-08-30 16:55:16 -060062 /*
63 * Use a strange error to make this case easier to find. When
64 * a uclass is not available it can prevent driver model from
65 * starting up and this failure is otherwise hard to debug.
66 */
67 return -EPFNOSUPPORT;
Simon Glassdd6ab882014-02-26 15:59:18 -070068 }
Simon Glassdd6ab882014-02-26 15:59:18 -070069 uc = calloc(1, sizeof(*uc));
70 if (!uc)
71 return -ENOMEM;
72 if (uc_drv->priv_auto_alloc_size) {
73 uc->priv = calloc(1, uc_drv->priv_auto_alloc_size);
74 if (!uc->priv) {
75 ret = -ENOMEM;
76 goto fail_mem;
77 }
78 }
79 uc->uc_drv = uc_drv;
80 INIT_LIST_HEAD(&uc->sibling_node);
81 INIT_LIST_HEAD(&uc->dev_head);
Simon Glass34a1d352014-06-11 23:29:49 -060082 list_add(&uc->sibling_node, &DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070083
84 if (uc_drv->init) {
85 ret = uc_drv->init(uc);
86 if (ret)
87 goto fail;
88 }
89
90 *ucp = uc;
91
92 return 0;
93fail:
94 if (uc_drv->priv_auto_alloc_size) {
95 free(uc->priv);
96 uc->priv = NULL;
97 }
98 list_del(&uc->sibling_node);
99fail_mem:
100 free(uc);
101
102 return ret;
103}
104
105int uclass_destroy(struct uclass *uc)
106{
107 struct uclass_driver *uc_drv;
Simon Glassf33663d2015-04-19 07:20:58 -0600108 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700109 int ret;
110
Simon Glassf33663d2015-04-19 07:20:58 -0600111 /*
112 * We cannot use list_for_each_entry_safe() here. If a device in this
113 * uclass has a child device also in this uclass, it will be also be
114 * unbound (by the recursion in the call to device_unbind() below).
115 * We can loop until the list is empty.
116 */
117 while (!list_empty(&uc->dev_head)) {
118 dev = list_first_entry(&uc->dev_head, struct udevice,
119 uclass_node);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100120 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassdd6ab882014-02-26 15:59:18 -0700121 if (ret)
122 return ret;
123 ret = device_unbind(dev);
124 if (ret)
125 return ret;
126 }
127
128 uc_drv = uc->uc_drv;
129 if (uc_drv->destroy)
130 uc_drv->destroy(uc);
131 list_del(&uc->sibling_node);
132 if (uc_drv->priv_auto_alloc_size)
133 free(uc->priv);
134 free(uc);
135
136 return 0;
137}
138
139int uclass_get(enum uclass_id id, struct uclass **ucp)
140{
141 struct uclass *uc;
142
143 *ucp = NULL;
144 uc = uclass_find(id);
145 if (!uc)
146 return uclass_add(id, ucp);
147 *ucp = uc;
148
149 return 0;
150}
151
Simon Glassd19d0732016-10-05 20:42:13 -0600152const char *uclass_get_name(enum uclass_id id)
153{
154 struct uclass *uc;
155
156 if (uclass_get(id, &uc))
157 return NULL;
158 return uc->uc_drv->name;
159}
160
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200161int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700162{
163 struct uclass *uc;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200164 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700165 int ret;
166
167 *devp = NULL;
168 ret = uclass_get(id, &uc);
169 if (ret)
170 return ret;
Simon Glass46766df2015-07-31 09:31:19 -0600171 if (list_empty(&uc->dev_head))
172 return -ENODEV;
Simon Glassdd6ab882014-02-26 15:59:18 -0700173
174 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
175 if (!index--) {
176 *devp = dev;
177 return 0;
178 }
179 }
180
181 return -ENODEV;
182}
183
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200184int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
185{
186 struct uclass *uc;
187 int ret;
188
189 *devp = NULL;
190 ret = uclass_get(id, &uc);
191 if (ret)
192 return ret;
193 if (list_empty(&uc->dev_head))
194 return 0;
195
196 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
197
198 return 0;
199}
200
201int uclass_find_next_device(struct udevice **devp)
202{
203 struct udevice *dev = *devp;
204
205 *devp = NULL;
206 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
207 return 0;
208
209 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
210
211 return 0;
212}
213
Przemyslaw Marczak2ffdf142015-04-15 13:07:22 +0200214int uclass_find_device_by_name(enum uclass_id id, const char *name,
215 struct udevice **devp)
216{
217 struct uclass *uc;
218 struct udevice *dev;
219 int ret;
220
221 *devp = NULL;
222 if (!name)
223 return -EINVAL;
224 ret = uclass_get(id, &uc);
225 if (ret)
226 return ret;
227
228 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
229 if (!strncmp(dev->name, name, strlen(name))) {
230 *devp = dev;
231 return 0;
232 }
233 }
234
235 return -ENODEV;
236}
237
Simon Glassdb6f0202014-07-23 06:55:12 -0600238int uclass_find_device_by_seq(enum uclass_id id, int seq_or_req_seq,
239 bool find_req_seq, struct udevice **devp)
240{
241 struct uclass *uc;
242 struct udevice *dev;
243 int ret;
244
245 *devp = NULL;
246 debug("%s: %d %d\n", __func__, find_req_seq, seq_or_req_seq);
247 if (seq_or_req_seq == -1)
248 return -ENODEV;
249 ret = uclass_get(id, &uc);
250 if (ret)
251 return ret;
252
253 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Alexandru Gagniuc4490f072017-04-04 10:46:56 -0700254 debug(" - %d %d '%s'\n", dev->req_seq, dev->seq, dev->name);
Simon Glassdb6f0202014-07-23 06:55:12 -0600255 if ((find_req_seq ? dev->req_seq : dev->seq) ==
256 seq_or_req_seq) {
257 *devp = dev;
258 debug(" - found\n");
259 return 0;
260 }
261 }
262 debug(" - not found\n");
263
264 return -ENODEV;
265}
266
Simon Glass96f04442016-01-21 19:43:57 -0700267int uclass_find_device_by_of_offset(enum uclass_id id, int node,
268 struct udevice **devp)
Simon Glassc1464ab2014-07-23 06:55:14 -0600269{
270 struct uclass *uc;
271 struct udevice *dev;
272 int ret;
273
274 *devp = NULL;
275 if (node < 0)
276 return -ENODEV;
277 ret = uclass_get(id, &uc);
278 if (ret)
279 return ret;
280
281 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700282 if (dev_of_offset(dev) == node) {
Simon Glassc1464ab2014-07-23 06:55:14 -0600283 *devp = dev;
284 return 0;
285 }
286 }
287
288 return -ENODEV;
289}
290
Simon Glassee145d62017-05-18 20:09:09 -0600291int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
292 struct udevice **devp)
293{
294 struct uclass *uc;
295 struct udevice *dev;
296 int ret;
297
298 *devp = NULL;
299 if (!ofnode_valid(node))
300 return -ENODEV;
301 ret = uclass_get(id, &uc);
302 if (ret)
303 return ret;
304
305 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
306 if (ofnode_equal(dev_ofnode(dev), node)) {
307 *devp = dev;
308 return 0;
309 }
310 }
311
312 return -ENODEV;
313}
314
Simon Glass359c4872015-12-19 19:38:55 -0700315#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass75f00df2015-07-02 18:15:38 -0600316static int uclass_find_device_by_phandle(enum uclass_id id,
317 struct udevice *parent,
318 const char *name,
319 struct udevice **devp)
320{
321 struct udevice *dev;
322 struct uclass *uc;
323 int find_phandle;
324 int ret;
325
326 *devp = NULL;
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600327 find_phandle = dev_read_u32_default(parent, name, -1);
Simon Glass75f00df2015-07-02 18:15:38 -0600328 if (find_phandle <= 0)
329 return -ENOENT;
330 ret = uclass_get(id, &uc);
331 if (ret)
332 return ret;
333
334 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700335 uint phandle;
336
Simon Glassbfb6e3d2017-05-18 20:09:12 -0600337 phandle = dev_read_phandle(dev);
Simon Glass75f00df2015-07-02 18:15:38 -0600338
339 if (phandle == find_phandle) {
340 *devp = dev;
341 return 0;
342 }
343 }
344
345 return -ENODEV;
346}
Simon Glass359c4872015-12-19 19:38:55 -0700347#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600348
Simon Glass32d8ab62016-07-17 15:23:15 -0600349int uclass_get_device_by_driver(enum uclass_id id,
350 const struct driver *find_drv,
351 struct udevice **devp)
352{
353 struct udevice *dev;
354 struct uclass *uc;
355 int ret;
356
357 ret = uclass_get(id, &uc);
358 if (ret)
359 return ret;
360
361 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
362 if (dev->driver == find_drv)
363 return uclass_get_device_tail(dev, 0, devp);
364 }
365
366 return -ENODEV;
367}
368
Simon Glass98c9fb02017-04-23 20:10:43 -0600369int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700370{
Simon Glassdd6ab882014-02-26 15:59:18 -0700371 if (ret)
372 return ret;
373
Simon Glass46cc8ba92015-04-24 22:33:07 -0600374 assert(dev);
Simon Glassdd6ab882014-02-26 15:59:18 -0700375 ret = device_probe(dev);
376 if (ret)
377 return ret;
378
379 *devp = dev;
380
381 return 0;
382}
383
Simon Glass4b67cef2014-07-23 06:55:08 -0600384int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
385{
386 struct udevice *dev;
387 int ret;
388
389 *devp = NULL;
390 ret = uclass_find_device(id, index, &dev);
391 return uclass_get_device_tail(dev, ret, devp);
392}
393
Przemyslaw Marczaka12a1f52015-04-15 13:07:23 +0200394int uclass_get_device_by_name(enum uclass_id id, const char *name,
395 struct udevice **devp)
396{
397 struct udevice *dev;
398 int ret;
399
400 *devp = NULL;
401 ret = uclass_find_device_by_name(id, name, &dev);
402 return uclass_get_device_tail(dev, ret, devp);
403}
404
Simon Glassdb6f0202014-07-23 06:55:12 -0600405int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
406{
407 struct udevice *dev;
408 int ret;
409
410 *devp = NULL;
411 ret = uclass_find_device_by_seq(id, seq, false, &dev);
412 if (ret == -ENODEV) {
413 /*
414 * We didn't find it in probed devices. See if there is one
415 * that will request this seq if probed.
416 */
417 ret = uclass_find_device_by_seq(id, seq, true, &dev);
418 }
419 return uclass_get_device_tail(dev, ret, devp);
420}
421
Simon Glassc1464ab2014-07-23 06:55:14 -0600422int uclass_get_device_by_of_offset(enum uclass_id id, int node,
423 struct udevice **devp)
424{
425 struct udevice *dev;
426 int ret;
427
428 *devp = NULL;
429 ret = uclass_find_device_by_of_offset(id, node, &dev);
430 return uclass_get_device_tail(dev, ret, devp);
431}
432
Simon Glassee145d62017-05-18 20:09:09 -0600433int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
434 struct udevice **devp)
435{
436 struct udevice *dev;
437 int ret;
438
439 *devp = NULL;
440 ret = uclass_find_device_by_ofnode(id, node, &dev);
441
442 return uclass_get_device_tail(dev, ret, devp);
443}
444
Simon Glass359c4872015-12-19 19:38:55 -0700445#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass75f00df2015-07-02 18:15:38 -0600446int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
447 const char *name, struct udevice **devp)
448{
449 struct udevice *dev;
450 int ret;
451
452 *devp = NULL;
453 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
454 return uclass_get_device_tail(dev, ret, devp);
455}
Simon Glass359c4872015-12-19 19:38:55 -0700456#endif
Simon Glass75f00df2015-07-02 18:15:38 -0600457
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200458int uclass_first_device(enum uclass_id id, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700459{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200460 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700461 int ret;
462
463 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200464 ret = uclass_find_first_device(id, &dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600465 if (!dev)
466 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200467 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700468}
469
Simon Glass832c3f02016-02-11 13:23:25 -0700470int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
471{
472 int ret;
473
474 ret = uclass_first_device(id, devp);
475 if (ret)
476 return ret;
477 else if (!*devp)
478 return -ENODEV;
479
480 return 0;
481}
482
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200483int uclass_next_device(struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700484{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200485 struct udevice *dev = *devp;
Simon Glassdd6ab882014-02-26 15:59:18 -0700486 int ret;
487
488 *devp = NULL;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200489 ret = uclass_find_next_device(&dev);
Simon Glass46cc8ba92015-04-24 22:33:07 -0600490 if (!dev)
491 return 0;
Przemyslaw Marczakf9d156e2015-04-15 13:07:17 +0200492 return uclass_get_device_tail(dev, ret, devp);
Simon Glassdd6ab882014-02-26 15:59:18 -0700493}
494
Simon Glass3ff83bc2017-04-23 20:10:45 -0600495int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
496{
497 int ret;
498
499 *devp = NULL;
500 ret = uclass_find_first_device(id, devp);
501 if (ret)
502 return ret;
503 if (!*devp)
504 return 0;
505
506 return device_probe(*devp);
507}
508
509int uclass_next_device_check(struct udevice **devp)
510{
511 int ret;
512
513 ret = uclass_find_next_device(devp);
514 if (ret)
515 return ret;
516 if (!*devp)
517 return 0;
518
519 return device_probe(*devp);
520}
521
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200522int uclass_bind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700523{
524 struct uclass *uc;
525 int ret;
526
527 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700528 list_add_tail(&dev->uclass_node, &uc->dev_head);
529
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700530 if (dev->parent) {
531 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
532
533 if (uc_drv->child_post_bind) {
534 ret = uc_drv->child_post_bind(dev);
535 if (ret)
536 goto err;
537 }
538 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700539
540 return 0;
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700541err:
542 /* There is no need to undo the parent's post_bind call */
543 list_del(&dev->uclass_node);
544
545 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700546}
547
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900548#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200549int uclass_unbind_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700550{
551 struct uclass *uc;
552 int ret;
553
554 uc = dev->uclass;
555 if (uc->uc_drv->pre_unbind) {
556 ret = uc->uc_drv->pre_unbind(dev);
557 if (ret)
558 return ret;
559 }
560
561 list_del(&dev->uclass_node);
562 return 0;
563}
Simon Glass8914c8a2015-02-27 22:06:31 -0700564#endif
Simon Glassdd6ab882014-02-26 15:59:18 -0700565
Simon Glassdb6f0202014-07-23 06:55:12 -0600566int uclass_resolve_seq(struct udevice *dev)
567{
568 struct udevice *dup;
569 int seq;
570 int ret;
571
572 assert(dev->seq == -1);
573 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, dev->req_seq,
574 false, &dup);
575 if (!ret) {
576 dm_warn("Device '%s': seq %d is in use by '%s'\n",
577 dev->name, dev->req_seq, dup->name);
578 } else if (ret == -ENODEV) {
579 /* Our requested sequence number is available */
580 if (dev->req_seq != -1)
581 return dev->req_seq;
582 } else {
583 return ret;
584 }
585
586 for (seq = 0; seq < DM_MAX_SEQ; seq++) {
587 ret = uclass_find_device_by_seq(dev->uclass->uc_drv->id, seq,
588 false, &dup);
589 if (ret == -ENODEV)
590 break;
591 if (ret)
592 return ret;
593 }
594 return seq;
595}
596
Simon Glass9c1f3822015-03-05 12:25:22 -0700597int uclass_pre_probe_device(struct udevice *dev)
Simon Glass5104b982015-01-25 08:27:10 -0700598{
599 struct uclass_driver *uc_drv;
Simon Glass9c1f3822015-03-05 12:25:22 -0700600 int ret;
601
602 uc_drv = dev->uclass->uc_drv;
603 if (uc_drv->pre_probe) {
604 ret = uc_drv->pre_probe(dev);
605 if (ret)
606 return ret;
607 }
Simon Glass5104b982015-01-25 08:27:10 -0700608
609 if (!dev->parent)
610 return 0;
611 uc_drv = dev->parent->uclass->uc_drv;
612 if (uc_drv->child_pre_probe)
613 return uc_drv->child_pre_probe(dev);
614
615 return 0;
616}
617
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200618int uclass_post_probe_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700619{
620 struct uclass_driver *uc_drv = dev->uclass->uc_drv;
621
622 if (uc_drv->post_probe)
623 return uc_drv->post_probe(dev);
624
625 return 0;
626}
627
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900628#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200629int uclass_pre_remove_device(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700630{
Simon Glassdd6ab882014-02-26 15:59:18 -0700631 struct uclass *uc;
632 int ret;
633
634 uc = dev->uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700635 if (uc->uc_drv->pre_remove) {
636 ret = uc->uc_drv->pre_remove(dev);
637 if (ret)
638 return ret;
639 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700640
641 return 0;
642}
Simon Glass8914c8a2015-02-27 22:06:31 -0700643#endif