1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2013 Google, Inc
14 #include <asm/global_data.h>
18 #include <fdt_support.h>
20 #include <asm/cache.h>
21 #include <dm/device.h>
22 #include <dm/device-internal.h>
24 #include <dm/of_access.h>
25 #include <dm/pinctrl.h>
26 #include <dm/platdata.h>
28 #include <dm/uclass.h>
29 #include <dm/uclass-internal.h>
31 #include <linux/err.h>
32 #include <linux/list.h>
33 #include <power-domain.h>
35 DECLARE_GLOBAL_DATA_PTR;
37 static int device_bind_common(struct udevice *parent, const struct driver *drv,
38 const char *name, void *plat,
39 ulong driver_data, ofnode node,
40 uint of_plat_size, struct udevice **devp)
53 ret = uclass_get(drv->id, &uc);
55 debug("Missing uclass for driver %s\n", drv->name);
59 dev = calloc(1, sizeof(struct udevice));
63 INIT_LIST_HEAD(&dev->sibling_node);
64 INIT_LIST_HEAD(&dev->child_head);
65 INIT_LIST_HEAD(&dev->uclass_node);
67 INIT_LIST_HEAD(&dev->devres_head);
69 dev_set_plat(dev, plat);
70 dev->driver_data = driver_data;
72 dev_set_ofnode(dev, node);
78 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
79 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
81 * Some devices, such as a SPI bus, I2C bus and serial ports
82 * are numbered using aliases.
84 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
85 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
86 if (uc->uc_drv->name && ofnode_valid(node)) {
87 if (!dev_read_alias_seq(dev, &dev->seq_))
92 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
93 dev->seq_ = uclass_find_next_free_seq(uc);
98 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
100 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
101 if (of_plat_size < drv->plat_auto)
106 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
107 ptr = calloc(1, drv->plat_auto);
112 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
113 memcpy(ptr, plat, of_plat_size);
114 dev_set_plat(dev, ptr);
118 size = uc->uc_drv->per_device_plat_auto;
120 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
121 ptr = calloc(1, size);
126 dev_set_uclass_plat(dev, ptr);
130 size = parent->driver->per_child_plat_auto;
132 size = parent->uclass->uc_drv->per_child_plat_auto;
135 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
136 ptr = calloc(1, size);
141 dev_set_parent_plat(dev, ptr);
143 /* put dev into parent's successor list */
144 list_add_tail(&dev->sibling_node, &parent->child_head);
147 ret = uclass_bind_device(dev);
149 goto fail_uclass_bind;
151 /* if we fail to bind we remove device from successors and free it */
153 ret = drv->bind(dev);
157 if (parent && parent->driver->child_post_bind) {
158 ret = parent->driver->child_post_bind(dev);
160 goto fail_child_post_bind;
162 if (uc->uc_drv->post_bind) {
163 ret = uc->uc_drv->post_bind(dev);
165 goto fail_uclass_post_bind;
169 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
173 dev_or_flags(dev, DM_FLAG_BOUND);
177 fail_uclass_post_bind:
178 /* There is no child unbind() method, so no clean-up required */
179 fail_child_post_bind:
180 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
181 if (drv->unbind && drv->unbind(dev)) {
182 dm_warn("unbind() method failed on dev '%s' on error path\n",
188 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
189 if (uclass_unbind_device(dev)) {
190 dm_warn("Failed to unbind dev '%s' on error path\n",
195 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
196 list_del(&dev->sibling_node);
197 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
198 free(dev_get_parent_plat(dev));
199 dev_set_parent_plat(dev, NULL);
203 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
204 free(dev_get_uclass_plat(dev));
205 dev_set_uclass_plat(dev, NULL);
208 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
209 free(dev_get_plat(dev));
210 dev_set_plat(dev, NULL);
213 devres_release_all(dev);
220 int device_bind_with_driver_data(struct udevice *parent,
221 const struct driver *drv, const char *name,
222 ulong driver_data, ofnode node,
223 struct udevice **devp)
225 return device_bind_common(parent, drv, name, NULL, driver_data, node,
229 int device_bind(struct udevice *parent, const struct driver *drv,
230 const char *name, void *plat, ofnode node,
231 struct udevice **devp)
233 return device_bind_common(parent, drv, name, plat, 0, node, 0,
237 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
238 const struct driver_info *info, struct udevice **devp)
244 drv = lists_driver_lookup_name(info->name);
247 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
250 #if CONFIG_IS_ENABLED(OF_PLATDATA)
251 plat_size = info->plat_size;
253 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
254 ofnode_null(), plat_size, devp);
261 int device_reparent(struct udevice *dev, struct udevice *new_parent)
263 struct udevice *pos, *n;
268 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
270 if (pos->driver != dev->driver)
273 list_del(&dev->sibling_node);
274 list_add_tail(&dev->sibling_node, &new_parent->child_head);
275 dev->parent = new_parent;
283 static void *alloc_priv(int size, uint flags)
287 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
288 size = ROUND(size, ARCH_DMA_MINALIGN);
289 priv = memalign(ARCH_DMA_MINALIGN, size);
291 memset(priv, '\0', size);
294 * Ensure that the zero bytes are flushed to memory.
295 * This prevents problems if the driver uses this as
296 * both an input and an output buffer:
298 * 1. Zeroes written to buffer (here) and sit in the
300 * 2. Driver issues a read command to DMA
301 * 3. CPU runs out of cache space and evicts some cache
302 * data in the buffer, writing zeroes to RAM from
305 * 5. Buffer now has some DMA data and some zeroes
306 * 6. Data being read is now incorrect
308 * To prevent this, ensure that the cache is clean
309 * within this range at the start. The driver can then
310 * use normal flush-after-write, invalidate-before-read
316 #ifndef CONFIG_MICROBLAZE
317 flush_dcache_range((ulong)priv, (ulong)priv + size);
321 priv = calloc(1, size);
328 * device_alloc_priv() - Allocate priv/plat data required by the device
330 * @dev: Device to process
331 * @return 0 if OK, -ENOMEM if out of memory
333 static int device_alloc_priv(struct udevice *dev)
335 const struct driver *drv;
342 /* Allocate private data if requested and not reentered */
343 if (drv->priv_auto && !dev_get_priv(dev)) {
344 ptr = alloc_priv(drv->priv_auto, drv->flags);
347 dev_set_priv(dev, ptr);
350 /* Allocate private data if requested and not reentered */
351 size = dev->uclass->uc_drv->per_device_auto;
352 if (size && !dev_get_uclass_priv(dev)) {
353 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
356 dev_set_uclass_priv(dev, ptr);
359 /* Allocate parent data for this child */
361 size = dev->parent->driver->per_child_auto;
363 size = dev->parent->uclass->uc_drv->per_child_auto;
364 if (size && !dev_get_parent_priv(dev)) {
365 ptr = alloc_priv(size, drv->flags);
368 dev_set_parent_priv(dev, ptr);
375 int device_of_to_plat(struct udevice *dev)
377 const struct driver *drv;
383 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
386 /* Ensure all parents have ofdata */
388 ret = device_of_to_plat(dev->parent);
393 * The device might have already been probed during
394 * the call to device_probe() on its parent device
395 * (e.g. PCI bridge devices). Test the flags again
396 * so that we don't mess up the device.
398 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
402 ret = device_alloc_priv(dev);
409 if (drv->of_to_plat &&
410 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
411 ret = drv->of_to_plat(dev);
416 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
425 int device_probe(struct udevice *dev)
427 const struct driver *drv;
433 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
439 ret = device_of_to_plat(dev);
443 /* Ensure all parents are probed */
445 ret = device_probe(dev->parent);
450 * The device might have already been probed during
451 * the call to device_probe() on its parent device
452 * (e.g. PCI bridge devices). Test the flags again
453 * so that we don't mess up the device.
455 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
459 dev_or_flags(dev, DM_FLAG_ACTIVATED);
462 * Process pinctrl for everything except the root device, and
463 * continue regardless of the result of pinctrl. Don't process pinctrl
464 * settings for pinctrl devices since the device may not yet be
467 * This call can produce some non-intuitive results. For example, on an
468 * x86 device where dev is the main PCI bus, the pinctrl device may be
469 * child or grandchild of that bus, meaning that the child will be
470 * probed here. If the child happens to be the P2SB and the pinctrl
471 * device is a child of that, then both the pinctrl and P2SB will be
472 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
473 * is set just above. However, the PCI bus' probe() method and
474 * associated uclass methods have not yet been called.
476 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
477 pinctrl_select_state(dev, "default");
479 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
480 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
481 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
482 ret = dev_power_domain_on(dev);
487 ret = uclass_pre_probe_device(dev);
491 if (dev->parent && dev->parent->driver->child_pre_probe) {
492 ret = dev->parent->driver->child_pre_probe(dev);
497 /* Only handle devices that have a valid ofnode */
498 if (dev_has_ofnode(dev)) {
500 * Process 'assigned-{clocks/clock-parents/clock-rates}'
503 ret = clk_set_defaults(dev, 0);
509 ret = drv->probe(dev);
514 ret = uclass_post_probe_device(dev);
518 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
519 pinctrl_select_state(dev, "default");
523 if (device_remove(dev, DM_REMOVE_NORMAL)) {
524 dm_warn("%s: Device '%s' failed to remove on error path\n",
525 __func__, dev->name);
528 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
535 void *dev_get_plat(const struct udevice *dev)
538 dm_warn("%s: null device\n", __func__);
545 void *dev_get_parent_plat(const struct udevice *dev)
548 dm_warn("%s: null device\n", __func__);
552 return dev->parent_plat_;
555 void *dev_get_uclass_plat(const struct udevice *dev)
558 dm_warn("%s: null device\n", __func__);
562 return dev->uclass_plat_;
565 void *dev_get_priv(const struct udevice *dev)
568 dm_warn("%s: null device\n", __func__);
575 void *dev_get_uclass_priv(const struct udevice *dev)
578 dm_warn("%s: null device\n", __func__);
582 return dev->uclass_priv_;
585 void *dev_get_parent_priv(const struct udevice *dev)
588 dm_warn("%s: null device\n", __func__);
592 return dev->parent_priv_;
595 static int device_get_device_tail(struct udevice *dev, int ret,
596 struct udevice **devp)
601 ret = device_probe(dev);
610 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
612 * device_find_by_ofnode() - Return device associated with given ofnode
614 * The returned device is *not* activated.
616 * @node: The ofnode for which a associated device should be looked up
617 * @devp: Pointer to structure to hold the found device
618 * Return: 0 if OK, -ve on error
620 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
626 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
627 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
639 int device_get_child(const struct udevice *parent, int index,
640 struct udevice **devp)
644 list_for_each_entry(dev, &parent->child_head, sibling_node) {
646 return device_get_device_tail(dev, 0, devp);
652 int device_get_child_count(const struct udevice *parent)
657 list_for_each_entry(dev, &parent->child_head, sibling_node)
663 int device_find_child_by_seq(const struct udevice *parent, int seq,
664 struct udevice **devp)
670 list_for_each_entry(dev, &parent->child_head, sibling_node) {
671 if (dev->seq_ == seq) {
680 int device_get_child_by_seq(const struct udevice *parent, int seq,
681 struct udevice **devp)
687 ret = device_find_child_by_seq(parent, seq, &dev);
689 return device_get_device_tail(dev, ret, devp);
692 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
693 struct udevice **devp)
699 list_for_each_entry(dev, &parent->child_head, sibling_node) {
700 if (dev_of_offset(dev) == of_offset) {
709 int device_get_child_by_of_offset(const struct udevice *parent, int node,
710 struct udevice **devp)
716 ret = device_find_child_by_of_offset(parent, node, &dev);
717 return device_get_device_tail(dev, ret, devp);
720 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
723 struct udevice *dev, *found;
725 if (ofnode_equal(dev_ofnode(parent), ofnode))
728 list_for_each_entry(dev, &parent->child_head, sibling_node) {
729 found = _device_find_global_by_ofnode(dev, ofnode);
737 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
739 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
741 return *devp ? 0 : -ENOENT;
744 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
748 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
749 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
752 #if CONFIG_IS_ENABLED(OF_PLATDATA)
753 int device_get_by_driver_info(const struct driver_info *info,
754 struct udevice **devp)
756 struct driver_info *info_base =
757 ll_entry_start(struct driver_info, driver_info);
758 int idx = info - info_base;
759 struct driver_rt *drt = gd_dm_driver_rt() + idx;
765 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
768 int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
770 struct driver_rt *drt = gd_dm_driver_rt() + idx;
776 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
780 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
782 if (list_empty(&parent->child_head)) {
785 *devp = list_first_entry(&parent->child_head, struct udevice,
792 int device_find_next_child(struct udevice **devp)
794 struct udevice *dev = *devp;
795 struct udevice *parent = dev->parent;
797 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
800 *devp = list_entry(dev->sibling_node.next, struct udevice,
807 int device_find_first_inactive_child(const struct udevice *parent,
808 enum uclass_id uclass_id,
809 struct udevice **devp)
814 list_for_each_entry(dev, &parent->child_head, sibling_node) {
815 if (!device_active(dev) &&
816 device_get_uclass_id(dev) == uclass_id) {
825 int device_find_first_child_by_uclass(const struct udevice *parent,
826 enum uclass_id uclass_id,
827 struct udevice **devp)
832 list_for_each_entry(dev, &parent->child_head, sibling_node) {
833 if (device_get_uclass_id(dev) == uclass_id) {
842 int device_find_child_by_name(const struct udevice *parent, const char *name,
843 struct udevice **devp)
849 list_for_each_entry(dev, &parent->child_head, sibling_node) {
850 if (!strcmp(dev->name, name)) {
859 int device_first_child_err(struct udevice *parent, struct udevice **devp)
863 device_find_first_child(parent, &dev);
867 return device_get_device_tail(dev, 0, devp);
870 int device_next_child_err(struct udevice **devp)
872 struct udevice *dev = *devp;
874 device_find_next_child(&dev);
878 return device_get_device_tail(dev, 0, devp);
881 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
886 device_find_first_child(parent, &dev);
890 ret = device_of_to_plat(dev);
899 int device_next_child_ofdata_err(struct udevice **devp)
901 struct udevice *dev = *devp;
904 device_find_next_child(&dev);
908 ret = device_of_to_plat(dev);
917 struct udevice *dev_get_parent(const struct udevice *child)
919 return child->parent;
922 ulong dev_get_driver_data(const struct udevice *dev)
924 return dev->driver_data;
927 const void *dev_get_driver_ops(const struct udevice *dev)
929 if (!dev || !dev->driver->ops)
932 return dev->driver->ops;
935 enum uclass_id device_get_uclass_id(const struct udevice *dev)
937 return dev->uclass->uc_drv->id;
940 const char *dev_get_uclass_name(const struct udevice *dev)
945 return dev->uclass->uc_drv->name;
948 bool device_has_children(const struct udevice *dev)
950 return !list_empty(&dev->child_head);
953 bool device_has_active_children(const struct udevice *dev)
955 struct udevice *child;
957 for (device_find_first_child(dev, &child);
959 device_find_next_child(&child)) {
960 if (device_active(child))
967 bool device_is_last_sibling(const struct udevice *dev)
969 struct udevice *parent = dev->parent;
973 return list_is_last(&dev->sibling_node, &parent->child_head);
976 void device_set_name_alloced(struct udevice *dev)
978 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
981 int device_set_name(struct udevice *dev, const char *name)
987 device_set_name_alloced(dev);
992 void dev_set_priv(struct udevice *dev, void *priv)
997 void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
999 dev->parent_priv_ = parent_priv;
1002 void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1004 dev->uclass_priv_ = uclass_priv;
1007 void dev_set_plat(struct udevice *dev, void *plat)
1012 void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1014 dev->parent_plat_ = parent_plat;
1017 void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1019 dev->uclass_plat_ = uclass_plat;
1022 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1023 bool device_is_compatible(const struct udevice *dev, const char *compat)
1025 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
1028 bool of_machine_is_compatible(const char *compat)
1030 const void *fdt = gd->fdt_blob;
1032 return !fdt_node_check_compatible(fdt, 0, compat);
1035 int dev_disable_by_path(const char *path)
1038 ofnode node = ofnode_path(path);
1039 struct udevice *dev;
1042 if (!of_live_active())
1045 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1046 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1054 ret = device_remove(dev, DM_REMOVE_NORMAL);
1058 ret = device_unbind(dev);
1062 return ofnode_set_enabled(node, false);
1065 int dev_enable_by_path(const char *path)
1067 ofnode node = ofnode_path(path);
1068 ofnode pnode = ofnode_get_parent(node);
1069 struct udevice *parent;
1072 if (!of_live_active())
1075 ret = device_find_by_ofnode(pnode, &parent);
1079 ret = ofnode_set_enabled(node, true);
1083 return lists_bind_fdt(parent, node, NULL, false);