1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2013 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
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)
48 if (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
56 ret = uclass_get(drv->id, &uc);
58 debug("Missing uclass for driver %s\n", drv->name);
62 dev = calloc(1, sizeof(struct udevice));
66 INIT_LIST_HEAD(&dev->sibling_node);
67 INIT_LIST_HEAD(&dev->child_head);
68 INIT_LIST_HEAD(&dev->uclass_node);
70 INIT_LIST_HEAD(&dev->devres_head);
72 dev_set_plat(dev, plat);
73 dev->driver_data = driver_data;
75 dev_set_ofnode(dev, node);
81 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
82 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
84 * Some devices, such as a SPI bus, I2C bus and serial ports
85 * are numbered using aliases.
87 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
88 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
89 if (uc->uc_drv->name && ofnode_valid(node)) {
90 if (!dev_read_alias_seq(dev, &dev->seq_))
95 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
96 dev->seq_ = uclass_find_next_free_seq(uc);
98 /* Check if we need to allocate plat */
103 * For of-platdata, we try use the existing data, but if
104 * plat_auto is larger, we must allocate a new space
106 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
108 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
109 if (of_plat_size < drv->plat_auto)
113 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
114 ptr = calloc(1, drv->plat_auto);
121 * For of-platdata, copy the old plat into the new
124 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
125 memcpy(ptr, plat, of_plat_size);
126 dev_set_plat(dev, ptr);
130 size = uc->uc_drv->per_device_plat_auto;
132 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
133 ptr = calloc(1, size);
138 dev_set_uclass_plat(dev, ptr);
142 size = parent->driver->per_child_plat_auto;
144 size = parent->uclass->uc_drv->per_child_plat_auto;
146 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
147 ptr = calloc(1, size);
152 dev_set_parent_plat(dev, ptr);
154 /* put dev into parent's successor list */
155 list_add_tail(&dev->sibling_node, &parent->child_head);
158 ret = uclass_bind_device(dev);
160 goto fail_uclass_bind;
162 /* if we fail to bind we remove device from successors and free it */
164 ret = drv->bind(dev);
168 if (parent && parent->driver->child_post_bind) {
169 ret = parent->driver->child_post_bind(dev);
171 goto fail_child_post_bind;
173 if (uc->uc_drv->post_bind) {
174 ret = uc->uc_drv->post_bind(dev);
176 goto fail_uclass_post_bind;
180 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
184 dev_or_flags(dev, DM_FLAG_BOUND);
188 fail_uclass_post_bind:
189 /* There is no child unbind() method, so no clean-up required */
190 fail_child_post_bind:
191 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
192 if (drv->unbind && drv->unbind(dev)) {
193 dm_warn("unbind() method failed on dev '%s' on error path\n",
199 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
200 if (uclass_unbind_device(dev)) {
201 dm_warn("Failed to unbind dev '%s' on error path\n",
206 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
207 list_del(&dev->sibling_node);
208 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
209 free(dev_get_parent_plat(dev));
210 dev_set_parent_plat(dev, NULL);
214 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
215 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
216 free(dev_get_uclass_plat(dev));
217 dev_set_uclass_plat(dev, NULL);
221 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
222 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
223 free(dev_get_plat(dev));
224 dev_set_plat(dev, NULL);
228 devres_release_all(dev);
235 int device_bind_with_driver_data(struct udevice *parent,
236 const struct driver *drv, const char *name,
237 ulong driver_data, ofnode node,
238 struct udevice **devp)
240 return device_bind_common(parent, drv, name, NULL, driver_data, node,
244 int device_bind(struct udevice *parent, const struct driver *drv,
245 const char *name, void *plat, ofnode node,
246 struct udevice **devp)
248 return device_bind_common(parent, drv, name, plat, 0, node, 0,
252 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
253 const struct driver_info *info, struct udevice **devp)
259 drv = lists_driver_lookup_name(info->name);
262 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
265 #if CONFIG_IS_ENABLED(OF_PLATDATA)
266 plat_size = info->plat_size;
268 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
269 ofnode_null(), plat_size, devp);
276 int device_reparent(struct udevice *dev, struct udevice *new_parent)
278 struct udevice *pos, *n;
283 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
285 if (pos->driver != dev->driver)
288 list_del(&dev->sibling_node);
289 list_add_tail(&dev->sibling_node, &new_parent->child_head);
290 dev->parent = new_parent;
298 static void *alloc_priv(int size, uint flags)
302 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
303 size = ROUND(size, ARCH_DMA_MINALIGN);
304 priv = memalign(ARCH_DMA_MINALIGN, size);
306 memset(priv, '\0', size);
309 * Ensure that the zero bytes are flushed to memory.
310 * This prevents problems if the driver uses this as
311 * both an input and an output buffer:
313 * 1. Zeroes written to buffer (here) and sit in the
315 * 2. Driver issues a read command to DMA
316 * 3. CPU runs out of cache space and evicts some cache
317 * data in the buffer, writing zeroes to RAM from
320 * 5. Buffer now has some DMA data and some zeroes
321 * 6. Data being read is now incorrect
323 * To prevent this, ensure that the cache is clean
324 * within this range at the start. The driver can then
325 * use normal flush-after-write, invalidate-before-read
328 * TODO(sjg@chromium.org): Drop this microblaze
331 #ifndef CONFIG_MICROBLAZE
332 flush_dcache_range((ulong)priv, (ulong)priv + size);
336 priv = calloc(1, size);
343 * device_alloc_priv() - Allocate priv/plat data required by the device
345 * @dev: Device to process
346 * @return 0 if OK, -ENOMEM if out of memory
348 static int device_alloc_priv(struct udevice *dev)
350 const struct driver *drv;
357 /* Allocate private data if requested and not reentered */
358 if (drv->priv_auto && !dev_get_priv(dev)) {
359 ptr = alloc_priv(drv->priv_auto, drv->flags);
362 dev_set_priv(dev, ptr);
365 /* Allocate private data if requested and not reentered */
366 size = dev->uclass->uc_drv->per_device_auto;
367 if (size && !dev_get_uclass_priv(dev)) {
368 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
371 dev_set_uclass_priv(dev, ptr);
374 /* Allocate parent data for this child */
376 size = dev->parent->driver->per_child_auto;
378 size = dev->parent->uclass->uc_drv->per_child_auto;
379 if (size && !dev_get_parent_priv(dev)) {
380 ptr = alloc_priv(size, drv->flags);
383 dev_set_parent_priv(dev, ptr);
390 int device_of_to_plat(struct udevice *dev)
392 const struct driver *drv;
398 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
402 * This is not needed if binding is disabled, since data is allocated
405 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
406 /* Ensure all parents have ofdata */
408 ret = device_of_to_plat(dev->parent);
413 * The device might have already been probed during
414 * the call to device_probe() on its parent device
415 * (e.g. PCI bridge devices). Test the flags again
416 * so that we don't mess up the device.
418 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
422 ret = device_alloc_priv(dev);
429 if (drv->of_to_plat &&
430 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
431 ret = drv->of_to_plat(dev);
436 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
446 * device_get_dma_constraints() - Populate device's DMA constraints
448 * Gets a device's DMA constraints from firmware. This information is later
449 * used by drivers to translate physcal addresses to the device's bus address
450 * space. For now only device-tree is supported.
452 * @dev: Pointer to target device
453 * Return: 0 if OK or if no DMA constraints were found, error otherwise
455 static int device_get_dma_constraints(struct udevice *dev)
457 struct udevice *parent = dev->parent;
463 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
467 * We start parsing for dma-ranges from the device's bus node. This is
468 * specially important on nested buses.
470 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
471 /* Don't return an error if no 'dma-ranges' were found */
472 if (ret && ret != -ENOENT) {
473 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
477 dev_set_dma_offset(dev, cpu - bus);
482 int device_probe(struct udevice *dev)
484 const struct driver *drv;
490 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
496 ret = device_of_to_plat(dev);
500 /* Ensure all parents are probed */
502 ret = device_probe(dev->parent);
507 * The device might have already been probed during
508 * the call to device_probe() on its parent device
509 * (e.g. PCI bridge devices). Test the flags again
510 * so that we don't mess up the device.
512 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
516 dev_or_flags(dev, DM_FLAG_ACTIVATED);
519 * Process pinctrl for everything except the root device, and
520 * continue regardless of the result of pinctrl. Don't process pinctrl
521 * settings for pinctrl devices since the device may not yet be
524 * This call can produce some non-intuitive results. For example, on an
525 * x86 device where dev is the main PCI bus, the pinctrl device may be
526 * child or grandchild of that bus, meaning that the child will be
527 * probed here. If the child happens to be the P2SB and the pinctrl
528 * device is a child of that, then both the pinctrl and P2SB will be
529 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
530 * is set just above. However, the PCI bus' probe() method and
531 * associated uclass methods have not yet been called.
533 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
534 pinctrl_select_state(dev, "default");
536 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
537 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
538 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
539 ret = dev_power_domain_on(dev);
544 ret = device_get_dma_constraints(dev);
548 ret = uclass_pre_probe_device(dev);
552 if (dev->parent && dev->parent->driver->child_pre_probe) {
553 ret = dev->parent->driver->child_pre_probe(dev);
558 /* Only handle devices that have a valid ofnode */
559 if (dev_has_ofnode(dev)) {
561 * Process 'assigned-{clocks/clock-parents/clock-rates}'
564 ret = clk_set_defaults(dev, 0);
570 ret = drv->probe(dev);
575 ret = uclass_post_probe_device(dev);
579 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
580 pinctrl_select_state(dev, "default");
584 if (device_remove(dev, DM_REMOVE_NORMAL)) {
585 dm_warn("%s: Device '%s' failed to remove on error path\n",
586 __func__, dev->name);
589 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
596 void *dev_get_plat(const struct udevice *dev)
599 dm_warn("%s: null device\n", __func__);
603 return dm_priv_to_rw(dev->plat_);
606 void *dev_get_parent_plat(const struct udevice *dev)
609 dm_warn("%s: null device\n", __func__);
613 return dm_priv_to_rw(dev->parent_plat_);
616 void *dev_get_uclass_plat(const struct udevice *dev)
619 dm_warn("%s: null device\n", __func__);
623 return dm_priv_to_rw(dev->uclass_plat_);
626 void *dev_get_priv(const struct udevice *dev)
629 dm_warn("%s: null device\n", __func__);
633 return dm_priv_to_rw(dev->priv_);
636 void *dev_get_uclass_priv(const struct udevice *dev)
639 dm_warn("%s: null device\n", __func__);
643 return dm_priv_to_rw(dev->uclass_priv_);
646 void *dev_get_parent_priv(const struct udevice *dev)
649 dm_warn("%s: null device\n", __func__);
653 return dm_priv_to_rw(dev->parent_priv_);
656 static int device_get_device_tail(struct udevice *dev, int ret,
657 struct udevice **devp)
662 ret = device_probe(dev);
671 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
673 * device_find_by_ofnode() - Return device associated with given ofnode
675 * The returned device is *not* activated.
677 * @node: The ofnode for which a associated device should be looked up
678 * @devp: Pointer to structure to hold the found device
679 * Return: 0 if OK, -ve on error
681 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
687 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
688 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
700 int device_get_child(const struct udevice *parent, int index,
701 struct udevice **devp)
705 list_for_each_entry(dev, &parent->child_head, sibling_node) {
707 return device_get_device_tail(dev, 0, devp);
713 int device_get_child_count(const struct udevice *parent)
718 list_for_each_entry(dev, &parent->child_head, sibling_node)
724 int device_find_child_by_seq(const struct udevice *parent, int seq,
725 struct udevice **devp)
731 list_for_each_entry(dev, &parent->child_head, sibling_node) {
732 if (dev->seq_ == seq) {
741 int device_get_child_by_seq(const struct udevice *parent, int seq,
742 struct udevice **devp)
748 ret = device_find_child_by_seq(parent, seq, &dev);
750 return device_get_device_tail(dev, ret, devp);
753 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
754 struct udevice **devp)
760 list_for_each_entry(dev, &parent->child_head, sibling_node) {
761 if (dev_of_offset(dev) == of_offset) {
770 int device_get_child_by_of_offset(const struct udevice *parent, int node,
771 struct udevice **devp)
777 ret = device_find_child_by_of_offset(parent, node, &dev);
778 return device_get_device_tail(dev, ret, devp);
781 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
784 struct udevice *dev, *found;
786 if (ofnode_equal(dev_ofnode(parent), ofnode))
789 list_for_each_entry(dev, &parent->child_head, sibling_node) {
790 found = _device_find_global_by_ofnode(dev, ofnode);
798 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
800 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
802 return *devp ? 0 : -ENOENT;
805 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
809 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
810 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
813 #if CONFIG_IS_ENABLED(OF_PLATDATA)
814 int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
818 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
819 struct udevice *base = ll_entry_start(struct udevice, udevice);
823 struct driver_rt *drt = gd_dm_driver_rt() + idx;
829 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
833 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
835 if (list_empty(&parent->child_head)) {
838 *devp = list_first_entry(&parent->child_head, struct udevice,
845 int device_find_next_child(struct udevice **devp)
847 struct udevice *dev = *devp;
848 struct udevice *parent = dev->parent;
850 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
853 *devp = list_entry(dev->sibling_node.next, struct udevice,
860 int device_find_first_inactive_child(const struct udevice *parent,
861 enum uclass_id uclass_id,
862 struct udevice **devp)
867 list_for_each_entry(dev, &parent->child_head, sibling_node) {
868 if (!device_active(dev) &&
869 device_get_uclass_id(dev) == uclass_id) {
878 int device_find_first_child_by_uclass(const struct udevice *parent,
879 enum uclass_id uclass_id,
880 struct udevice **devp)
885 list_for_each_entry(dev, &parent->child_head, sibling_node) {
886 if (device_get_uclass_id(dev) == uclass_id) {
895 int device_find_child_by_name(const struct udevice *parent, const char *name,
896 struct udevice **devp)
902 list_for_each_entry(dev, &parent->child_head, sibling_node) {
903 if (!strcmp(dev->name, name)) {
912 int device_first_child_err(struct udevice *parent, struct udevice **devp)
916 device_find_first_child(parent, &dev);
920 return device_get_device_tail(dev, 0, devp);
923 int device_next_child_err(struct udevice **devp)
925 struct udevice *dev = *devp;
927 device_find_next_child(&dev);
931 return device_get_device_tail(dev, 0, devp);
934 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
939 device_find_first_child(parent, &dev);
943 ret = device_of_to_plat(dev);
952 int device_next_child_ofdata_err(struct udevice **devp)
954 struct udevice *dev = *devp;
957 device_find_next_child(&dev);
961 ret = device_of_to_plat(dev);
970 struct udevice *dev_get_parent(const struct udevice *child)
972 return child->parent;
975 ulong dev_get_driver_data(const struct udevice *dev)
977 return dev->driver_data;
980 const void *dev_get_driver_ops(const struct udevice *dev)
982 if (!dev || !dev->driver->ops)
985 return dev->driver->ops;
988 enum uclass_id device_get_uclass_id(const struct udevice *dev)
990 return dev->uclass->uc_drv->id;
993 const char *dev_get_uclass_name(const struct udevice *dev)
998 return dev->uclass->uc_drv->name;
1001 bool device_has_children(const struct udevice *dev)
1003 return !list_empty(&dev->child_head);
1006 bool device_has_active_children(const struct udevice *dev)
1008 struct udevice *child;
1010 for (device_find_first_child(dev, &child);
1012 device_find_next_child(&child)) {
1013 if (device_active(child))
1020 bool device_is_last_sibling(const struct udevice *dev)
1022 struct udevice *parent = dev->parent;
1026 return list_is_last(&dev->sibling_node, &parent->child_head);
1029 void device_set_name_alloced(struct udevice *dev)
1031 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
1034 int device_set_name(struct udevice *dev, const char *name)
1036 name = strdup(name);
1040 device_set_name_alloced(dev);
1045 void dev_set_priv(struct udevice *dev, void *priv)
1050 void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1052 dev->parent_priv_ = parent_priv;
1055 void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1057 dev->uclass_priv_ = uclass_priv;
1060 void dev_set_plat(struct udevice *dev, void *plat)
1065 void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1067 dev->parent_plat_ = parent_plat;
1070 void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1072 dev->uclass_plat_ = uclass_plat;
1075 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1076 bool device_is_compatible(const struct udevice *dev, const char *compat)
1078 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
1081 bool of_machine_is_compatible(const char *compat)
1083 const void *fdt = gd->fdt_blob;
1085 return !fdt_node_check_compatible(fdt, 0, compat);
1088 int dev_disable_by_path(const char *path)
1091 ofnode node = ofnode_path(path);
1092 struct udevice *dev;
1095 if (!of_live_active())
1098 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1099 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1107 ret = device_remove(dev, DM_REMOVE_NORMAL);
1111 ret = device_unbind(dev);
1115 return ofnode_set_enabled(node, false);
1118 int dev_enable_by_path(const char *path)
1120 ofnode node = ofnode_path(path);
1121 ofnode pnode = ofnode_get_parent(node);
1122 struct udevice *parent;
1125 if (!of_live_active())
1128 ret = device_find_by_ofnode(pnode, &parent);
1132 ret = ofnode_set_enabled(node, true);
1136 return lists_bind_fdt(parent, node, NULL, false);
1140 #if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
1141 static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1143 struct udevice *base = ll_entry_start(struct udevice, udevice);
1144 int idx = dev - base;
1146 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1151 u32 dev_get_flags(const struct udevice *dev)
1153 const struct udevice_rt *urt = dev_get_rt(dev);
1158 void dev_or_flags(const struct udevice *dev, u32 or)
1160 struct udevice_rt *urt = dev_get_rt(dev);
1165 void dev_bic_flags(const struct udevice *dev, u32 bic)
1167 struct udevice_rt *urt = dev_get_rt(dev);
1169 urt->flags_ &= ~bic;
1171 #endif /* OF_PLATDATA_RT */