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_namelen(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) &&
191 strlen(uc_drv->name) == len)
195 return UCLASS_INVALID;
198 enum uclass_id uclass_get_by_name(const char *name)
200 return uclass_get_by_namelen(name, strlen(name));
203 int dev_get_uclass_index(struct udevice *dev, struct uclass **ucp)
205 struct udevice *iter;
206 struct uclass *uc = dev->uclass;
209 if (list_empty(&uc->dev_head))
212 uclass_foreach_dev(iter, uc) {
224 int uclass_find_device(enum uclass_id id, int index, struct udevice **devp)
231 ret = uclass_get(id, &uc);
234 if (list_empty(&uc->dev_head))
237 uclass_foreach_dev(dev, uc) {
247 int uclass_find_first_device(enum uclass_id id, struct udevice **devp)
253 ret = uclass_get(id, &uc);
256 if (list_empty(&uc->dev_head))
259 *devp = list_first_entry(&uc->dev_head, struct udevice, uclass_node);
264 int uclass_find_next_device(struct udevice **devp)
266 struct udevice *dev = *devp;
269 if (list_is_last(&dev->uclass_node, &dev->uclass->dev_head))
272 *devp = list_entry(dev->uclass_node.next, struct udevice, uclass_node);
277 int uclass_find_device_by_namelen(enum uclass_id id, const char *name, int len,
278 struct udevice **devp)
287 ret = uclass_get(id, &uc);
291 uclass_foreach_dev(dev, uc) {
292 if (!strncmp(dev->name, name, len) &&
293 strlen(dev->name) == len) {
302 int uclass_find_device_by_name(enum uclass_id id, const char *name,
303 struct udevice **devp)
305 return uclass_find_device_by_namelen(id, name, strlen(name), devp);
308 int uclass_find_next_free_seq(struct uclass *uc)
313 /* If using aliases, start with the highest alias value */
314 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
315 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS))
316 max = dev_read_alias_highest_id(uc->uc_drv->name);
318 /* Avoid conflict with existing devices */
319 list_for_each_entry(dev, &uc->dev_head, uclass_node) {
324 * At this point, max will be -1 if there are no existing aliases or
331 int uclass_find_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
338 log_debug("%d\n", seq);
341 ret = uclass_get(id, &uc);
345 uclass_foreach_dev(dev, uc) {
346 log_debug(" - %d '%s'\n", dev->seq_, dev->name);
347 if (dev->seq_ == seq) {
349 log_debug(" - found\n");
353 log_debug(" - not found\n");
358 int uclass_find_device_by_of_offset(enum uclass_id id, int node,
359 struct udevice **devp)
368 ret = uclass_get(id, &uc);
372 uclass_foreach_dev(dev, uc) {
373 if (dev_of_offset(dev) == node) {
382 int uclass_find_device_by_ofnode(enum uclass_id id, ofnode node,
383 struct udevice **devp)
389 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
391 if (!ofnode_valid(node))
393 ret = uclass_get(id, &uc);
397 uclass_foreach_dev(dev, uc) {
398 log(LOGC_DM, LOGL_DEBUG_CONTENT, " - checking %s\n",
400 if (ofnode_equal(dev_ofnode(dev), node)) {
408 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
409 ofnode_get_name(node), *devp ? (*devp)->name : "(none)", ret);
413 #if CONFIG_IS_ENABLED(OF_REAL)
414 int uclass_find_device_by_phandle(enum uclass_id id, struct udevice *parent,
415 const char *name, struct udevice **devp)
423 find_phandle = dev_read_u32_default(parent, name, -1);
424 if (find_phandle <= 0)
426 ret = uclass_get(id, &uc);
430 uclass_foreach_dev(dev, uc) {
433 phandle = dev_read_phandle(dev);
435 if (phandle == find_phandle) {
445 int uclass_get_device_by_driver(enum uclass_id id,
446 const struct driver *find_drv,
447 struct udevice **devp)
453 ret = uclass_get(id, &uc);
457 uclass_foreach_dev(dev, uc) {
458 if (dev->driver == find_drv)
459 return uclass_get_device_tail(dev, 0, devp);
465 int uclass_get_device_tail(struct udevice *dev, int ret, struct udevice **devp)
471 ret = device_probe(dev);
480 int uclass_get_device(enum uclass_id id, int index, struct udevice **devp)
486 ret = uclass_find_device(id, index, &dev);
487 return uclass_get_device_tail(dev, ret, devp);
490 int uclass_get_device_by_name(enum uclass_id id, const char *name,
491 struct udevice **devp)
497 ret = uclass_find_device_by_name(id, name, &dev);
498 return uclass_get_device_tail(dev, ret, devp);
501 int uclass_get_device_by_seq(enum uclass_id id, int seq, struct udevice **devp)
507 ret = uclass_find_device_by_seq(id, seq, &dev);
509 return uclass_get_device_tail(dev, ret, devp);
512 int uclass_get_device_by_of_offset(enum uclass_id id, int node,
513 struct udevice **devp)
519 ret = uclass_find_device_by_of_offset(id, node, &dev);
520 return uclass_get_device_tail(dev, ret, devp);
523 int uclass_get_device_by_ofnode(enum uclass_id id, ofnode node,
524 struct udevice **devp)
529 log(LOGC_DM, LOGL_DEBUG, "Looking for %s\n", ofnode_get_name(node));
531 ret = uclass_find_device_by_ofnode(id, node, &dev);
532 log(LOGC_DM, LOGL_DEBUG, " - result for %s: %s (ret=%d)\n",
533 ofnode_get_name(node), dev ? dev->name : "(none)", ret);
535 return uclass_get_device_tail(dev, ret, devp);
538 #if CONFIG_IS_ENABLED(OF_CONTROL)
539 int uclass_get_device_by_phandle_id(enum uclass_id id, uint phandle_id,
540 struct udevice **devp)
547 ret = uclass_get(id, &uc);
551 uclass_foreach_dev(dev, uc) {
554 phandle = dev_read_phandle(dev);
556 if (phandle == phandle_id) {
558 return uclass_get_device_tail(dev, ret, devp);
565 int uclass_get_device_by_phandle(enum uclass_id id, struct udevice *parent,
566 const char *name, struct udevice **devp)
572 ret = uclass_find_device_by_phandle(id, parent, name, &dev);
573 return uclass_get_device_tail(dev, ret, devp);
578 * Starting from the given device @dev, return pointer to the first device in
579 * the uclass that probes successfully in @devp.
581 static void _uclass_next_device(struct udevice *dev, struct udevice **devp)
583 for (; dev; uclass_find_next_device(&dev)) {
584 if (!device_probe(dev))
590 void uclass_first_device(enum uclass_id id, struct udevice **devp)
595 ret = uclass_find_first_device(id, &dev);
596 _uclass_next_device(dev, devp);
599 void uclass_next_device(struct udevice **devp)
601 struct udevice *dev = *devp;
603 uclass_find_next_device(&dev);
604 _uclass_next_device(dev, devp);
607 int uclass_first_device_err(enum uclass_id id, struct udevice **devp)
611 ret = uclass_first_device_check(id, devp);
620 int uclass_next_device_err(struct udevice **devp)
624 ret = uclass_next_device_check(devp);
633 int uclass_first_device_check(enum uclass_id id, struct udevice **devp)
638 ret = uclass_find_first_device(id, devp);
644 return device_probe(*devp);
647 int uclass_next_device_check(struct udevice **devp)
651 ret = uclass_find_next_device(devp);
657 return device_probe(*devp);
660 int uclass_get_count(void)
662 const struct uclass *uc;
666 list_for_each_entry(uc, gd->uclass_root, sibling_node)
673 int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
674 struct udevice **devp)
679 uclass_id_foreach_dev(id, dev, uc) {
680 if (dev_get_driver_data(dev) == driver_data) {
683 return device_probe(dev);
690 int uclass_bind_device(struct udevice *dev)
696 list_add_tail(&dev->uclass_node, &uc->dev_head);
699 struct uclass_driver *uc_drv = dev->parent->uclass->uc_drv;
701 if (uc_drv->child_post_bind) {
702 ret = uc_drv->child_post_bind(dev);
710 /* There is no need to undo the parent's post_bind call */
711 list_del(&dev->uclass_node);
716 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
717 int uclass_pre_unbind_device(struct udevice *dev)
723 if (uc->uc_drv->pre_unbind) {
724 ret = uc->uc_drv->pre_unbind(dev);
732 int uclass_unbind_device(struct udevice *dev)
734 list_del(&dev->uclass_node);
740 int uclass_pre_probe_device(struct udevice *dev)
742 struct uclass_driver *uc_drv;
745 uc_drv = dev->uclass->uc_drv;
746 if (uc_drv->pre_probe) {
747 ret = uc_drv->pre_probe(dev);
754 uc_drv = dev->parent->uclass->uc_drv;
755 if (uc_drv->child_pre_probe) {
756 ret = uc_drv->child_pre_probe(dev);
764 int uclass_post_probe_device(struct udevice *dev)
766 struct uclass_driver *uc_drv;
770 uc_drv = dev->parent->uclass->uc_drv;
771 if (uc_drv->child_post_probe) {
772 ret = uc_drv->child_post_probe(dev);
778 uc_drv = dev->uclass->uc_drv;
779 if (uc_drv->post_probe) {
780 ret = uc_drv->post_probe(dev);
788 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
789 int uclass_pre_remove_device(struct udevice *dev)
795 if (uc->uc_drv->pre_remove) {
796 ret = uc->uc_drv->pre_remove(dev);
805 int uclass_probe_all(enum uclass_id id)
810 err = uclass_first_device_check(id, &dev);
812 /* Scanning uclass to probe all devices */
814 ret = uclass_next_device_check(&dev);
822 int uclass_id_count(enum uclass_id id)
828 uclass_id_foreach_dev(id, dev, uc)
834 UCLASS_DRIVER(nop) = {