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_)) {
92 log_debug(" - seq=%d\n", dev->seq_);
97 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
98 dev->seq_ = uclass_find_next_free_seq(uc);
100 /* Check if we need to allocate plat */
101 if (drv->plat_auto) {
105 * For of-platdata, we try use the existing data, but if
106 * plat_auto is larger, we must allocate a new space
108 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
110 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
111 if (of_plat_size < drv->plat_auto)
115 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
116 ptr = calloc(1, drv->plat_auto);
123 * For of-platdata, copy the old plat into the new
126 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
127 memcpy(ptr, plat, of_plat_size);
128 dev_set_plat(dev, ptr);
132 size = uc->uc_drv->per_device_plat_auto;
134 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
135 ptr = calloc(1, size);
140 dev_set_uclass_plat(dev, ptr);
144 size = parent->driver->per_child_plat_auto;
146 size = parent->uclass->uc_drv->per_child_plat_auto;
148 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
149 ptr = calloc(1, size);
154 dev_set_parent_plat(dev, ptr);
156 /* put dev into parent's successor list */
157 list_add_tail(&dev->sibling_node, &parent->child_head);
160 ret = uclass_bind_device(dev);
162 goto fail_uclass_bind;
164 /* if we fail to bind we remove device from successors and free it */
166 ret = drv->bind(dev);
170 if (parent && parent->driver->child_post_bind) {
171 ret = parent->driver->child_post_bind(dev);
173 goto fail_child_post_bind;
175 if (uc->uc_drv->post_bind) {
176 ret = uc->uc_drv->post_bind(dev);
178 goto fail_uclass_post_bind;
182 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
186 dev_or_flags(dev, DM_FLAG_BOUND);
190 fail_uclass_post_bind:
191 /* There is no child unbind() method, so no clean-up required */
192 fail_child_post_bind:
193 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
194 if (drv->unbind && drv->unbind(dev)) {
195 dm_warn("unbind() method failed on dev '%s' on error path\n",
201 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
202 if (uclass_unbind_device(dev)) {
203 dm_warn("Failed to unbind dev '%s' on error path\n",
208 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
209 list_del(&dev->sibling_node);
210 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
211 free(dev_get_parent_plat(dev));
212 dev_set_parent_plat(dev, NULL);
216 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
217 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
218 free(dev_get_uclass_plat(dev));
219 dev_set_uclass_plat(dev, NULL);
223 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
224 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
225 free(dev_get_plat(dev));
226 dev_set_plat(dev, NULL);
230 devres_release_all(dev);
237 int device_bind_with_driver_data(struct udevice *parent,
238 const struct driver *drv, const char *name,
239 ulong driver_data, ofnode node,
240 struct udevice **devp)
242 return device_bind_common(parent, drv, name, NULL, driver_data, node,
246 int device_bind(struct udevice *parent, const struct driver *drv,
247 const char *name, void *plat, ofnode node,
248 struct udevice **devp)
250 return device_bind_common(parent, drv, name, plat, 0, node, 0,
254 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
255 const struct driver_info *info, struct udevice **devp)
261 drv = lists_driver_lookup_name(info->name);
264 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
267 #if CONFIG_IS_ENABLED(OF_PLATDATA)
268 plat_size = info->plat_size;
270 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
271 ofnode_null(), plat_size, devp);
278 int device_reparent(struct udevice *dev, struct udevice *new_parent)
280 struct udevice *pos, *n;
285 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
287 if (pos->driver != dev->driver)
290 list_del(&dev->sibling_node);
291 list_add_tail(&dev->sibling_node, &new_parent->child_head);
292 dev->parent = new_parent;
300 static void *alloc_priv(int size, uint flags)
304 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
305 size = ROUND(size, ARCH_DMA_MINALIGN);
306 priv = memalign(ARCH_DMA_MINALIGN, size);
308 memset(priv, '\0', size);
311 * Ensure that the zero bytes are flushed to memory.
312 * This prevents problems if the driver uses this as
313 * both an input and an output buffer:
315 * 1. Zeroes written to buffer (here) and sit in the
317 * 2. Driver issues a read command to DMA
318 * 3. CPU runs out of cache space and evicts some cache
319 * data in the buffer, writing zeroes to RAM from
322 * 5. Buffer now has some DMA data and some zeroes
323 * 6. Data being read is now incorrect
325 * To prevent this, ensure that the cache is clean
326 * within this range at the start. The driver can then
327 * use normal flush-after-write, invalidate-before-read
330 * TODO(sjg@chromium.org): Drop this microblaze
333 #ifndef CONFIG_MICROBLAZE
334 flush_dcache_range((ulong)priv, (ulong)priv + size);
338 priv = calloc(1, size);
345 * device_alloc_priv() - Allocate priv/plat data required by the device
347 * @dev: Device to process
348 * @return 0 if OK, -ENOMEM if out of memory
350 static int device_alloc_priv(struct udevice *dev)
352 const struct driver *drv;
359 /* Allocate private data if requested and not reentered */
360 if (drv->priv_auto && !dev_get_priv(dev)) {
361 ptr = alloc_priv(drv->priv_auto, drv->flags);
364 dev_set_priv(dev, ptr);
367 /* Allocate private data if requested and not reentered */
368 size = dev->uclass->uc_drv->per_device_auto;
369 if (size && !dev_get_uclass_priv(dev)) {
370 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
373 dev_set_uclass_priv(dev, ptr);
376 /* Allocate parent data for this child */
378 size = dev->parent->driver->per_child_auto;
380 size = dev->parent->uclass->uc_drv->per_child_auto;
381 if (size && !dev_get_parent_priv(dev)) {
382 ptr = alloc_priv(size, drv->flags);
385 dev_set_parent_priv(dev, ptr);
392 int device_of_to_plat(struct udevice *dev)
394 const struct driver *drv;
400 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
404 * This is not needed if binding is disabled, since data is allocated
407 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
408 /* Ensure all parents have ofdata */
410 ret = device_of_to_plat(dev->parent);
415 * The device might have already been probed during
416 * the call to device_probe() on its parent device
417 * (e.g. PCI bridge devices). Test the flags again
418 * so that we don't mess up the device.
420 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
424 ret = device_alloc_priv(dev);
431 if (drv->of_to_plat &&
432 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
433 ret = drv->of_to_plat(dev);
438 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
448 * device_get_dma_constraints() - Populate device's DMA constraints
450 * Gets a device's DMA constraints from firmware. This information is later
451 * used by drivers to translate physcal addresses to the device's bus address
452 * space. For now only device-tree is supported.
454 * @dev: Pointer to target device
455 * Return: 0 if OK or if no DMA constraints were found, error otherwise
457 static int device_get_dma_constraints(struct udevice *dev)
459 struct udevice *parent = dev->parent;
465 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
469 * We start parsing for dma-ranges from the device's bus node. This is
470 * specially important on nested buses.
472 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
473 /* Don't return an error if no 'dma-ranges' were found */
474 if (ret && ret != -ENOENT) {
475 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
479 dev_set_dma_offset(dev, cpu - bus);
484 int device_probe(struct udevice *dev)
486 const struct driver *drv;
492 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
498 ret = device_of_to_plat(dev);
502 /* Ensure all parents are probed */
504 ret = device_probe(dev->parent);
509 * The device might have already been probed during
510 * the call to device_probe() on its parent device
511 * (e.g. PCI bridge devices). Test the flags again
512 * so that we don't mess up the device.
514 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
518 dev_or_flags(dev, DM_FLAG_ACTIVATED);
521 * Process pinctrl for everything except the root device, and
522 * continue regardless of the result of pinctrl. Don't process pinctrl
523 * settings for pinctrl devices since the device may not yet be
526 * This call can produce some non-intuitive results. For example, on an
527 * x86 device where dev is the main PCI bus, the pinctrl device may be
528 * child or grandchild of that bus, meaning that the child will be
529 * probed here. If the child happens to be the P2SB and the pinctrl
530 * device is a child of that, then both the pinctrl and P2SB will be
531 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
532 * is set just above. However, the PCI bus' probe() method and
533 * associated uclass methods have not yet been called.
535 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
536 pinctrl_select_state(dev, "default");
538 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
539 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
540 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
541 ret = dev_power_domain_on(dev);
546 ret = device_get_dma_constraints(dev);
550 ret = uclass_pre_probe_device(dev);
554 if (dev->parent && dev->parent->driver->child_pre_probe) {
555 ret = dev->parent->driver->child_pre_probe(dev);
560 /* Only handle devices that have a valid ofnode */
561 if (dev_has_ofnode(dev)) {
563 * Process 'assigned-{clocks/clock-parents/clock-rates}'
566 ret = clk_set_defaults(dev, CLK_DEFAULTS_PRE);
572 ret = drv->probe(dev);
577 ret = uclass_post_probe_device(dev);
581 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
582 pinctrl_select_state(dev, "default");
586 if (device_remove(dev, DM_REMOVE_NORMAL)) {
587 dm_warn("%s: Device '%s' failed to remove on error path\n",
588 __func__, dev->name);
591 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
598 void *dev_get_plat(const struct udevice *dev)
601 dm_warn("%s: null device\n", __func__);
605 return dm_priv_to_rw(dev->plat_);
608 void *dev_get_parent_plat(const struct udevice *dev)
611 dm_warn("%s: null device\n", __func__);
615 return dm_priv_to_rw(dev->parent_plat_);
618 void *dev_get_uclass_plat(const struct udevice *dev)
621 dm_warn("%s: null device\n", __func__);
625 return dm_priv_to_rw(dev->uclass_plat_);
628 void *dev_get_priv(const struct udevice *dev)
631 dm_warn("%s: null device\n", __func__);
635 return dm_priv_to_rw(dev->priv_);
638 void *dev_get_uclass_priv(const struct udevice *dev)
641 dm_warn("%s: null device\n", __func__);
645 return dm_priv_to_rw(dev->uclass_priv_);
648 void *dev_get_parent_priv(const struct udevice *dev)
651 dm_warn("%s: null device\n", __func__);
655 return dm_priv_to_rw(dev->parent_priv_);
658 static int device_get_device_tail(struct udevice *dev, int ret,
659 struct udevice **devp)
664 ret = device_probe(dev);
673 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
675 * device_find_by_ofnode() - Return device associated with given ofnode
677 * The returned device is *not* activated.
679 * @node: The ofnode for which a associated device should be looked up
680 * @devp: Pointer to structure to hold the found device
681 * Return: 0 if OK, -ve on error
683 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
689 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
690 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
702 int device_get_child(const struct udevice *parent, int index,
703 struct udevice **devp)
707 list_for_each_entry(dev, &parent->child_head, sibling_node) {
709 return device_get_device_tail(dev, 0, devp);
715 int device_get_child_count(const struct udevice *parent)
720 list_for_each_entry(dev, &parent->child_head, sibling_node)
726 int device_find_child_by_seq(const struct udevice *parent, int seq,
727 struct udevice **devp)
733 list_for_each_entry(dev, &parent->child_head, sibling_node) {
734 if (dev->seq_ == seq) {
743 int device_get_child_by_seq(const struct udevice *parent, int seq,
744 struct udevice **devp)
750 ret = device_find_child_by_seq(parent, seq, &dev);
752 return device_get_device_tail(dev, ret, devp);
755 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
756 struct udevice **devp)
762 list_for_each_entry(dev, &parent->child_head, sibling_node) {
763 if (dev_of_offset(dev) == of_offset) {
772 int device_get_child_by_of_offset(const struct udevice *parent, int node,
773 struct udevice **devp)
779 ret = device_find_child_by_of_offset(parent, node, &dev);
780 return device_get_device_tail(dev, ret, devp);
783 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
786 struct udevice *dev, *found;
788 if (ofnode_equal(dev_ofnode(parent), ofnode))
791 list_for_each_entry(dev, &parent->child_head, sibling_node) {
792 found = _device_find_global_by_ofnode(dev, ofnode);
800 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
802 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
804 return *devp ? 0 : -ENOENT;
807 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
811 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
812 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
815 #if CONFIG_IS_ENABLED(OF_PLATDATA)
816 int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
820 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
821 struct udevice *base = ll_entry_start(struct udevice, udevice);
825 struct driver_rt *drt = gd_dm_driver_rt() + idx;
831 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
835 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
837 if (list_empty(&parent->child_head)) {
840 *devp = list_first_entry(&parent->child_head, struct udevice,
847 int device_find_next_child(struct udevice **devp)
849 struct udevice *dev = *devp;
850 struct udevice *parent = dev->parent;
852 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
855 *devp = list_entry(dev->sibling_node.next, struct udevice,
862 int device_find_first_inactive_child(const struct udevice *parent,
863 enum uclass_id uclass_id,
864 struct udevice **devp)
869 list_for_each_entry(dev, &parent->child_head, sibling_node) {
870 if (!device_active(dev) &&
871 device_get_uclass_id(dev) == uclass_id) {
880 int device_find_first_child_by_uclass(const struct udevice *parent,
881 enum uclass_id uclass_id,
882 struct udevice **devp)
887 list_for_each_entry(dev, &parent->child_head, sibling_node) {
888 if (device_get_uclass_id(dev) == uclass_id) {
897 int device_find_child_by_name(const struct udevice *parent, const char *name,
898 struct udevice **devp)
904 list_for_each_entry(dev, &parent->child_head, sibling_node) {
905 if (!strcmp(dev->name, name)) {
914 int device_first_child_err(struct udevice *parent, struct udevice **devp)
918 device_find_first_child(parent, &dev);
922 return device_get_device_tail(dev, 0, devp);
925 int device_next_child_err(struct udevice **devp)
927 struct udevice *dev = *devp;
929 device_find_next_child(&dev);
933 return device_get_device_tail(dev, 0, devp);
936 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
941 device_find_first_child(parent, &dev);
945 ret = device_of_to_plat(dev);
954 int device_next_child_ofdata_err(struct udevice **devp)
956 struct udevice *dev = *devp;
959 device_find_next_child(&dev);
963 ret = device_of_to_plat(dev);
972 struct udevice *dev_get_parent(const struct udevice *child)
974 return child->parent;
977 ulong dev_get_driver_data(const struct udevice *dev)
979 return dev->driver_data;
982 const void *dev_get_driver_ops(const struct udevice *dev)
984 if (!dev || !dev->driver->ops)
987 return dev->driver->ops;
990 enum uclass_id device_get_uclass_id(const struct udevice *dev)
992 return dev->uclass->uc_drv->id;
995 const char *dev_get_uclass_name(const struct udevice *dev)
1000 return dev->uclass->uc_drv->name;
1003 bool device_has_children(const struct udevice *dev)
1005 return !list_empty(&dev->child_head);
1008 bool device_has_active_children(const struct udevice *dev)
1010 struct udevice *child;
1012 for (device_find_first_child(dev, &child);
1014 device_find_next_child(&child)) {
1015 if (device_active(child))
1022 bool device_is_last_sibling(const struct udevice *dev)
1024 struct udevice *parent = dev->parent;
1028 return list_is_last(&dev->sibling_node, &parent->child_head);
1031 void device_set_name_alloced(struct udevice *dev)
1033 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
1036 int device_set_name(struct udevice *dev, const char *name)
1038 name = strdup(name);
1042 device_set_name_alloced(dev);
1047 void dev_set_priv(struct udevice *dev, void *priv)
1052 void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1054 dev->parent_priv_ = parent_priv;
1057 void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1059 dev->uclass_priv_ = uclass_priv;
1062 void dev_set_plat(struct udevice *dev, void *plat)
1067 void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1069 dev->parent_plat_ = parent_plat;
1072 void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1074 dev->uclass_plat_ = uclass_plat;
1077 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1078 bool device_is_compatible(const struct udevice *dev, const char *compat)
1080 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
1083 bool of_machine_is_compatible(const char *compat)
1085 const void *fdt = gd->fdt_blob;
1087 return !fdt_node_check_compatible(fdt, 0, compat);
1090 int dev_disable_by_path(const char *path)
1093 ofnode node = ofnode_path(path);
1094 struct udevice *dev;
1097 if (!of_live_active())
1100 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1101 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1109 ret = device_remove(dev, DM_REMOVE_NORMAL);
1113 ret = device_unbind(dev);
1117 return ofnode_set_enabled(node, false);
1120 int dev_enable_by_path(const char *path)
1122 ofnode node = ofnode_path(path);
1123 ofnode pnode = ofnode_get_parent(node);
1124 struct udevice *parent;
1127 if (!of_live_active())
1130 ret = device_find_by_ofnode(pnode, &parent);
1134 ret = ofnode_set_enabled(node, true);
1138 return lists_bind_fdt(parent, node, NULL, NULL, false);
1142 #if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
1143 static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1145 struct udevice *base = ll_entry_start(struct udevice, udevice);
1146 int idx = dev - base;
1148 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1153 u32 dev_get_flags(const struct udevice *dev)
1155 const struct udevice_rt *urt = dev_get_rt(dev);
1160 void dev_or_flags(const struct udevice *dev, u32 or)
1162 struct udevice_rt *urt = dev_get_rt(dev);
1167 void dev_bic_flags(const struct udevice *dev, u32 bic)
1169 struct udevice_rt *urt = dev_get_rt(dev);
1171 urt->flags_ &= ~bic;
1173 #endif /* OF_PLATDATA_RT */