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_len(const char *name, int len)
187 for (i = 0; i < UCLASS_COUNT; i++) {
188 struct uclass_driver *uc_drv = lists_uclass_lookup(i);
190 if (uc_drv && !strncmp(uc_drv->name, name, len))
194 return UCLASS_INVALID;
197 enum uclass_id uclass_get_by_name(const char *name)
199 return uclass_get_by_name_len(name, strlen(name));
202 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
204 struct udevice *iter;
205 struct uclass *uc = dev->uclass;
208 if (list_empty(&uc->dev_head))
211 uclass_foreach_dev(iter, uc) {
223 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
230 ret = uclass_get(id, &uc);
233 if (list_empty(&uc->dev_head))
236 uclass_foreach_dev(dev, uc) {
246 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
252 ret = uclass_get(id, &uc);
255 if (list_empty(&uc->dev_head))
258 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
263 int uclass_find_next_device(struct udevice **devp)
265 struct udevice *dev = *devp;
268 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
271 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
276 int uclass_find_device_by_name(enum uclass_id id, const char *name,
277 struct udevice **devp)
286 ret = uclass_get(id, &uc);
290 uclass_foreach_dev(dev, uc) {
291 if (!strcmp(dev->name, name)) {
300 int uclass_find_next_free_seq(struct uclass *uc)
305 /* If using aliases, start with the highest alias value */
306 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
307 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
308 max = dev_read_alias_highest_id(uc->uc_drv->name);
310 /* Avoid conflict with existing devices */
311 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
316 * At this point, max will be -1 if there are no existing aliases or
323 int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
330 log_debug("%d\n", seq);
333 ret = uclass_get(id, &uc);
337 uclass_foreach_dev(dev, uc) {
338 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
339 if (dev->seq_ == seq) {
341 log_debug(" - found\n");
345 log_debug(" - not found\n");
350 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
351 struct udevice **devp)
360 ret = uclass_get(id, &uc);
364 uclass_foreach_dev(dev, uc) {
365 if (dev_of_offset(dev) == node) {
374 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
375 struct udevice **devp)
381 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
383 if (!ofnode_valid(node))
385 ret = uclass_get(id, &uc);
389 uclass_foreach_dev(dev, uc) {
390 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
392 if (ofnode_equal(dev_ofnode(dev), node)) {
400 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
401 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
405 #if CONFIG_IS_ENABLED(OF_REAL)
406 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
407 const char *name, struct udevice **devp)
415 find_phandle = dev_read_u32_default(parent, name, -1);
416 if (find_phandle <= 0)
418 ret = uclass_get(id, &uc);
422 uclass_foreach_dev(dev, uc) {
425 phandle = dev_read_phandle(dev);
427 if (phandle == find_phandle) {
437 int uclass_get_device_by_driver(enum uclass_id id,
438 const struct driver *find_drv,
439 struct udevice **devp)
445 ret = uclass_get(id, &uc);
449 uclass_foreach_dev(dev, uc) {
450 if (dev->driver == find_drv)
451 return uclass_get_device_tail(dev, 0, devp);
457 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
463 ret = device_probe(dev);
472 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
478 ret = uclass_find_device(id, index, &dev);
479 return uclass_get_device_tail(dev, ret, devp);
482 int uclass_get_device_by_name(enum uclass_id id, const char *name,
483 struct udevice **devp)
489 ret = uclass_find_device_by_name(id, name, &dev);
490 return uclass_get_device_tail(dev, ret, devp);
493 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
499 ret = uclass_find_device_by_seq(id, seq, &dev);
501 return uclass_get_device_tail(dev, ret, devp);
504 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
505 struct udevice **devp)
511 ret = uclass_find_device_by_of_offset(id, node, &dev);
512 return uclass_get_device_tail(dev, ret, devp);
515 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
516 struct udevice **devp)
521 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
523 ret = uclass_find_device_by_ofnode(id, node, &dev);
524 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
525 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
527 return uclass_get_device_tail(dev, ret, devp);
530 #if CONFIG_IS_ENABLED(OF_CONTROL)
531 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
532 struct udevice **devp)
539 ret = uclass_get(id, &uc);
543 uclass_foreach_dev(dev, uc) {
546 phandle = dev_read_phandle(dev);
548 if (phandle == phandle_id) {
550 return uclass_get_device_tail(dev, ret, devp);
557 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
558 const char *name, struct udevice **devp)
564 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
565 return uclass_get_device_tail(dev, ret, devp);
569 int uclass_first_device(enum uclass_id id, struct udevice **devp)
575 ret = uclass_find_first_device(id, &dev);
578 return uclass_get_device_tail(dev, ret, devp);
581 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
585 ret = uclass_first_device(id, devp);
594 int uclass_next_device(struct udevice **devp)
596 struct udevice *dev = *devp;
600 ret = uclass_find_next_device(&dev);
603 return uclass_get_device_tail(dev, ret, devp);
606 int uclass_next_device_err(struct udevice **devp)
610 ret = uclass_next_device(devp);
619 int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
624 ret = uclass_find_first_device(id, devp);
630 return device_probe(*devp);
633 int uclass_next_device_check(struct udevice **devp)
637 ret = uclass_find_next_device(devp);
643 return device_probe(*devp);
646 int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
647 struct udevice **devp)
652 uclass_id_foreach_dev(id, dev, uc) {
653 if (dev_get_driver_data(dev) == driver_data) {
656 return device_probe(dev);
663 int uclass_bind_device(struct udevice *dev)
669 list_add_tail(&dev->uclass_node, &uc->dev_head);
672 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
674 if (uc_drv->child_post_bind) {
675 ret = uc_drv->child_post_bind(dev);
683 /* There is no need to undo the parent's post_bind call */
684 list_del(&dev->uclass_node);
689 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
690 int uclass_pre_unbind_device(struct udevice *dev)
696 if (uc->uc_drv->pre_unbind) {
697 ret = uc->uc_drv->pre_unbind(dev);
705 int uclass_unbind_device(struct udevice *dev)
707 list_del(&dev->uclass_node);
713 int uclass_pre_probe_device(struct udevice *dev)
715 struct uclass_driver *uc_drv;
718 uc_drv = dev->uclass->uc_drv;
719 if (uc_drv->pre_probe) {
720 ret = uc_drv->pre_probe(dev);
727 uc_drv = dev->parent->uclass->uc_drv;
728 if (uc_drv->child_pre_probe) {
729 ret = uc_drv->child_pre_probe(dev);
737 int uclass_post_probe_device(struct udevice *dev)
739 struct uclass_driver *uc_drv;
743 uc_drv = dev->parent->uclass->uc_drv;
744 if (uc_drv->child_post_probe) {
745 ret = uc_drv->child_post_probe(dev);
751 uc_drv = dev->uclass->uc_drv;
752 if (uc_drv->post_probe) {
753 ret = uc_drv->post_probe(dev);
761 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
762 int uclass_pre_remove_device(struct udevice *dev)
768 if (uc->uc_drv->pre_remove) {
769 ret = uc->uc_drv->pre_remove(dev);
778 int uclass_probe_all(enum uclass_id id)
783 ret = uclass_first_device(id, &dev);
787 /* Scanning uclass to probe all devices */
789 ret = uclass_next_device(&dev);
797 UCLASS_DRIVER(nop) = {