1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 #define LOG_CATEGORY LOGC_DM
16 #include <asm/global_data.h>
17 #include <dm/device.h>
18 #include <dm/device-internal.h>
20 #include <dm/uclass.h>
21 #include <dm/uclass-internal.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 struct uclass *uclass_find(enum uclass_id key)
33 * TODO(sjg@chromium.org): Optimise this, perhaps moving the found
34 * node to the start of the list, or creating a linear array mapping
37 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
38 if (uc->uc_drv->id == key)
46 * uclass_add() - Create new uclass in list
47 * @id: Id number to create
48 * @ucp: Returns pointer to uclass, or NULL on error
49 * @return 0 on success, -ve on error
51 * The new uclass is added to the list. There must be only one uclass for
54 static int uclass_add(enum uclass_id id, struct uclass **ucp)
56 struct uclass_driver *uc_drv;
61 uc_drv = lists_uclass_lookup(id);
63 debug("Cannot find uclass for id %d: please add the UCLASS_DRIVER() declaration for this UCLASS_... id\n",
66 * Use a strange error to make this case easier to find. When
67 * a uclass is not available it can prevent driver model from
68 * starting up and this failure is otherwise hard to debug.
72 uc = calloc(1, sizeof(*uc));
75 if (uc_drv->priv_auto) {
78 ptr = calloc(1, uc_drv->priv_auto);
83 uclass_set_priv(uc, ptr);
86 INIT_LIST_HEAD(&uc->sibling_node);
87 INIT_LIST_HEAD(&uc->dev_head);
88 list_add(&uc->sibling_node, DM_UCLASS_ROOT_NON_CONST);
91 ret = uc_drv->init(uc);
100 if (uc_drv->priv_auto) {
101 free(uclass_get_priv(uc));
102 uclass_set_priv(uc, NULL);
104 list_del(&uc->sibling_node);
111 int uclass_destroy(struct uclass *uc)
113 struct uclass_driver *uc_drv;
118 * We cannot use list_for_each_entry_safe() here. If a device in this
119 * uclass has a child device also in this uclass, it will be also be
120 * unbound (by the recursion in the call to device_unbind() below).
121 * We can loop until the list is empty.
123 while (!list_empty(&uc->dev_head)) {
124 dev = list_first_entry(&uc->dev_head, struct udevice,
126 ret = device_remove(dev, DM_REMOVE_NORMAL | DM_REMOVE_NO_PD);
128 return log_msg_ret("remove", ret);
129 ret = device_unbind(dev);
131 return log_msg_ret("unbind", ret);
137 list_del(&uc->sibling_node);
138 if (uc_drv->priv_auto)
139 free(uclass_get_priv(uc));
145 int uclass_get(enum uclass_id id, struct uclass **ucp)
149 /* Immediately fail if driver model is not set up */
150 if (!gd->uclass_root)
153 uc = uclass_find(id);
155 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST))
157 return uclass_add(id, ucp);
164 const char *uclass_get_name(enum uclass_id id)
168 if (uclass_get(id, &uc))
170 return uc->uc_drv->name;
173 void *uclass_get_priv(const struct uclass *uc)
178 void uclass_set_priv(struct uclass *uc, void *priv)
183 enum uclass_id uclass_get_by_name(const char *name)
187 for (i = 0; i < UCLASS_COUNT; i++) {
188 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
190 if (uc_drv && !strcmp(uc_drv->name, name))
194 return UCLASS_INVALID;
197 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
199 struct udevice *iter;
200 struct uclass *uc = dev->uclass;
203 if (list_empty(&uc->dev_head))
206 uclass_foreach_dev(iter, uc) {
218 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
225 ret = uclass_get(id, &uc);
228 if (list_empty(&uc->dev_head))
231 uclass_foreach_dev(dev, uc) {
241 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
247 ret = uclass_get(id, &uc);
250 if (list_empty(&uc->dev_head))
253 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
258 int uclass_find_next_device(struct udevice **devp)
260 struct udevice *dev = *devp;
263 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
266 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
271 int uclass_find_device_by_name(enum uclass_id id, const char *name,
272 struct udevice **devp)
281 ret = uclass_get(id, &uc);
285 uclass_foreach_dev(dev, uc) {
286 if (!strcmp(dev->name, name)) {
295 int uclass_find_next_free_seq(struct uclass *uc)
300 /* If using aliases, start with the highest alias value */
301 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
302 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
303 max = dev_read_alias_highest_id(uc->uc_drv->name);
305 /* Avoid conflict with existing devices */
306 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
311 * At this point, max will be -1 if there are no existing aliases or
318 int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
325 log_debug("%d\n", seq);
328 ret = uclass_get(id, &uc);
332 uclass_foreach_dev(dev, uc) {
333 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
334 if (dev->seq_ == seq) {
336 log_debug(" - found\n");
340 log_debug(" - not found\n");
345 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
346 struct udevice **devp)
355 ret = uclass_get(id, &uc);
359 uclass_foreach_dev(dev, uc) {
360 if (dev_of_offset(dev) == node) {
369 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
370 struct udevice **devp)
376 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
378 if (!ofnode_valid(node))
380 ret = uclass_get(id, &uc);
384 uclass_foreach_dev(dev, uc) {
385 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
387 if (ofnode_equal(dev_ofnode(dev), node)) {
395 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
396 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
400 #if CONFIG_IS_ENABLED(OF_REAL)
401 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
402 const char *name, struct udevice **devp)
410 find_phandle = dev_read_u32_default(parent, name, -1);
411 if (find_phandle <= 0)
413 ret = uclass_get(id, &uc);
417 uclass_foreach_dev(dev, uc) {
420 phandle = dev_read_phandle(dev);
422 if (phandle == find_phandle) {
432 int uclass_get_device_by_driver(enum uclass_id id,
433 const struct driver *find_drv,
434 struct udevice **devp)
440 ret = uclass_get(id, &uc);
444 uclass_foreach_dev(dev, uc) {
445 if (dev->driver == find_drv)
446 return uclass_get_device_tail(dev, 0, devp);
452 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
458 ret = device_probe(dev);
467 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
473 ret = uclass_find_device(id, index, &dev);
474 return uclass_get_device_tail(dev, ret, devp);
477 int uclass_get_device_by_name(enum uclass_id id, const char *name,
478 struct udevice **devp)
484 ret = uclass_find_device_by_name(id, name, &dev);
485 return uclass_get_device_tail(dev, ret, devp);
488 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
494 ret = uclass_find_device_by_seq(id, seq, &dev);
496 return uclass_get_device_tail(dev, ret, devp);
499 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
500 struct udevice **devp)
506 ret = uclass_find_device_by_of_offset(id, node, &dev);
507 return uclass_get_device_tail(dev, ret, devp);
510 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
511 struct udevice **devp)
516 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
518 ret = uclass_find_device_by_ofnode(id, node, &dev);
519 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
520 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
522 return uclass_get_device_tail(dev, ret, devp);
525 #if CONFIG_IS_ENABLED(OF_CONTROL)
526 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
527 struct udevice **devp)
534 ret = uclass_get(id, &uc);
538 uclass_foreach_dev(dev, uc) {
541 phandle = dev_read_phandle(dev);
543 if (phandle == phandle_id) {
545 return uclass_get_device_tail(dev, ret, devp);
552 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
553 const char *name, struct udevice **devp)
559 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
560 return uclass_get_device_tail(dev, ret, devp);
564 int uclass_first_device(enum uclass_id id, struct udevice **devp)
570 ret = uclass_find_first_device(id, &dev);
573 return uclass_get_device_tail(dev, ret, devp);
576 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
580 ret = uclass_first_device(id, devp);
589 int uclass_next_device(struct udevice **devp)
591 struct udevice *dev = *devp;
595 ret = uclass_find_next_device(&dev);
598 return uclass_get_device_tail(dev, ret, devp);
601 int uclass_next_device_err(struct udevice **devp)
605 ret = uclass_next_device(devp);
614 int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
619 ret = uclass_find_first_device(id, devp);
625 return device_probe(*devp);
628 int uclass_next_device_check(struct udevice **devp)
632 ret = uclass_find_next_device(devp);
638 return device_probe(*devp);
641 int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
642 struct udevice **devp)
647 uclass_id_foreach_dev(id, dev, uc) {
648 if (dev_get_driver_data(dev) == driver_data) {
651 return device_probe(dev);
658 int uclass_bind_device(struct udevice *dev)
664 list_add_tail(&dev->uclass_node, &uc->dev_head);
667 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
669 if (uc_drv->child_post_bind) {
670 ret = uc_drv->child_post_bind(dev);
678 /* There is no need to undo the parent's post_bind call */
679 list_del(&dev->uclass_node);
684 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
685 int uclass_pre_unbind_device(struct udevice *dev)
691 if (uc->uc_drv->pre_unbind) {
692 ret = uc->uc_drv->pre_unbind(dev);
700 int uclass_unbind_device(struct udevice *dev)
702 list_del(&dev->uclass_node);
708 int uclass_pre_probe_device(struct udevice *dev)
710 struct uclass_driver *uc_drv;
713 uc_drv = dev->uclass->uc_drv;
714 if (uc_drv->pre_probe) {
715 ret = uc_drv->pre_probe(dev);
722 uc_drv = dev->parent->uclass->uc_drv;
723 if (uc_drv->child_pre_probe) {
724 ret = uc_drv->child_pre_probe(dev);
732 int uclass_post_probe_device(struct udevice *dev)
734 struct uclass_driver *uc_drv;
738 uc_drv = dev->parent->uclass->uc_drv;
739 if (uc_drv->child_post_probe) {
740 ret = uc_drv->child_post_probe(dev);
746 uc_drv = dev->uclass->uc_drv;
747 if (uc_drv->post_probe) {
748 ret = uc_drv->post_probe(dev);
756 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
757 int uclass_pre_remove_device(struct udevice *dev)
763 if (uc->uc_drv->pre_remove) {
764 ret = uc->uc_drv->pre_remove(dev);
773 int uclass_probe_all(enum uclass_id id)
778 ret = uclass_first_device(id, &dev);
782 /* Scanning uclass to probe all devices */
784 ret = uclass_next_device(&dev);
792 UCLASS_DRIVER(nop) = {