1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2013 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
17 #include <fdt_support.h>
19 #include <asm/cache.h>
20 #include <dm/device.h>
21 #include <dm/device-internal.h>
23 #include <dm/of_access.h>
24 #include <dm/pinctrl.h>
25 #include <dm/platdata.h>
27 #include <dm/uclass.h>
28 #include <dm/uclass-internal.h>
30 #include <linux/err.h>
31 #include <linux/list.h>
32 #include <power-domain.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 static int device_bind_common(struct udevice *parent, const struct driver *drv,
37 const char *name, void *plat,
38 ulong driver_data, ofnode node,
39 uint of_plat_size, struct udevice **devp)
51 ret = uclass_get(drv->id, &uc);
53 debug("Missing uclass for driver %s\n", drv->name);
57 dev = calloc(1, sizeof(struct udevice));
61 INIT_LIST_HEAD(&dev->sibling_node);
62 INIT_LIST_HEAD(&dev->child_head);
63 INIT_LIST_HEAD(&dev->uclass_node);
65 INIT_LIST_HEAD(&dev->devres_head);
68 dev->driver_data = driver_data;
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 * This is just a 'requested' sequence, and will be
85 * resolved (and ->seq updated) when the device is probed.
87 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
88 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
89 if (uc->uc_drv->name && ofnode_valid(node)) {
90 dev_read_alias_seq(dev, &dev->sqq);
91 dev_read_alias_seq(dev, &dev->req_seq);
94 if (CONFIG_IS_ENABLED(OF_PRIOR_STAGE)) {
95 if (dev->req_seq == -1) {
98 uclass_find_next_free_req_seq(
103 dev->req_seq = uclass_find_next_free_req_seq(uc);
106 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
107 dev->sqq = uclass_find_next_free_req_seq(uc);
109 if (drv->plat_auto) {
112 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
114 dev->flags |= DM_FLAG_OF_PLATDATA;
115 if (of_plat_size < drv->plat_auto)
120 dev->flags |= DM_FLAG_ALLOC_PDATA;
121 dev->plat = calloc(1, drv->plat_auto);
126 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) {
127 memcpy(dev->plat, plat, of_plat_size);
132 size = uc->uc_drv->per_device_plat_auto;
134 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
135 dev->uclass_plat = calloc(1, size);
136 if (!dev->uclass_plat) {
143 size = parent->driver->per_child_plat_auto;
145 size = parent->uclass->uc_drv->per_child_plat_auto;
148 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
149 dev->parent_plat = calloc(1, size);
150 if (!dev->parent_plat) {
155 /* put dev into parent's successor list */
156 list_add_tail(&dev->sibling_node, &parent->child_head);
159 ret = uclass_bind_device(dev);
161 goto fail_uclass_bind;
163 /* if we fail to bind we remove device from successors and free it */
165 ret = drv->bind(dev);
169 if (parent && parent->driver->child_post_bind) {
170 ret = parent->driver->child_post_bind(dev);
172 goto fail_child_post_bind;
174 if (uc->uc_drv->post_bind) {
175 ret = uc->uc_drv->post_bind(dev);
177 goto fail_uclass_post_bind;
181 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
185 dev->flags |= DM_FLAG_BOUND;
189 fail_uclass_post_bind:
190 /* There is no child unbind() method, so no clean-up required */
191 fail_child_post_bind:
192 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
193 if (drv->unbind && drv->unbind(dev)) {
194 dm_warn("unbind() method failed on dev '%s' on error path\n",
200 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
201 if (uclass_unbind_device(dev)) {
202 dm_warn("Failed to unbind dev '%s' on error path\n",
207 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
208 list_del(&dev->sibling_node);
209 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
210 free(dev->parent_plat);
211 dev->parent_plat = NULL;
215 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
216 free(dev->uclass_plat);
217 dev->uclass_plat = NULL;
220 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
225 devres_release_all(dev);
232 int device_bind_with_driver_data(struct udevice *parent,
233 const struct driver *drv, const char *name,
234 ulong driver_data, ofnode node,
235 struct udevice **devp)
237 return device_bind_common(parent, drv, name, NULL, driver_data, node,
241 int device_bind(struct udevice *parent, const struct driver *drv,
242 const char *name, void *plat, ofnode node,
243 struct udevice **devp)
245 return device_bind_common(parent, drv, name, plat, 0, node, 0,
249 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
250 const struct driver_info *info, struct udevice **devp)
256 drv = lists_driver_lookup_name(info->name);
259 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
262 #if CONFIG_IS_ENABLED(OF_PLATDATA)
263 plat_size = info->plat_size;
265 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
266 ofnode_null(), plat_size, devp);
273 int device_reparent(struct udevice *dev, struct udevice *new_parent)
275 struct udevice *pos, *n;
280 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
282 if (pos->driver != dev->driver)
285 list_del(&dev->sibling_node);
286 list_add_tail(&dev->sibling_node, &new_parent->child_head);
287 dev->parent = new_parent;
295 static void *alloc_priv(int size, uint flags)
299 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
300 size = ROUND(size, ARCH_DMA_MINALIGN);
301 priv = memalign(ARCH_DMA_MINALIGN, size);
303 memset(priv, '\0', size);
306 * Ensure that the zero bytes are flushed to memory.
307 * This prevents problems if the driver uses this as
308 * both an input and an output buffer:
310 * 1. Zeroes written to buffer (here) and sit in the
312 * 2. Driver issues a read command to DMA
313 * 3. CPU runs out of cache space and evicts some cache
314 * data in the buffer, writing zeroes to RAM from
317 * 5. Buffer now has some DMA data and some zeroes
318 * 6. Data being read is now incorrect
320 * To prevent this, ensure that the cache is clean
321 * within this range at the start. The driver can then
322 * use normal flush-after-write, invalidate-before-read
325 * TODO(sjg@chromium.org): Drop this microblaze
328 #ifndef CONFIG_MICROBLAZE
329 flush_dcache_range((ulong)priv, (ulong)priv + size);
333 priv = calloc(1, size);
339 int device_of_to_plat(struct udevice *dev)
341 const struct driver *drv;
348 if (dev->flags & DM_FLAG_PLATDATA_VALID)
351 /* Ensure all parents have ofdata */
353 ret = device_of_to_plat(dev->parent);
358 * The device might have already been probed during
359 * the call to device_probe() on its parent device
360 * (e.g. PCI bridge devices). Test the flags again
361 * so that we don't mess up the device.
363 if (dev->flags & DM_FLAG_PLATDATA_VALID)
370 /* Allocate private data if requested and not reentered */
371 if (drv->priv_auto && !dev->priv) {
372 dev->priv = alloc_priv(drv->priv_auto, drv->flags);
378 /* Allocate private data if requested and not reentered */
379 size = dev->uclass->uc_drv->per_device_auto;
380 if (size && !dev->uclass_priv) {
381 dev->uclass_priv = alloc_priv(size,
382 dev->uclass->uc_drv->flags);
383 if (!dev->uclass_priv) {
389 /* Allocate parent data for this child */
391 size = dev->parent->driver->per_child_auto;
393 size = dev->parent->uclass->uc_drv->per_child_auto;
395 if (size && !dev->parent_priv) {
396 dev->parent_priv = alloc_priv(size, drv->flags);
397 if (!dev->parent_priv) {
404 if (drv->of_to_plat &&
405 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
406 ret = drv->of_to_plat(dev);
411 dev->flags |= DM_FLAG_PLATDATA_VALID;
420 int device_probe(struct udevice *dev)
422 const struct driver *drv;
429 if (dev->flags & DM_FLAG_ACTIVATED)
435 ret = device_of_to_plat(dev);
439 /* Ensure all parents are probed */
441 ret = device_probe(dev->parent);
446 * The device might have already been probed during
447 * the call to device_probe() on its parent device
448 * (e.g. PCI bridge devices). Test the flags again
449 * so that we don't mess up the device.
451 if (dev->flags & DM_FLAG_ACTIVATED)
455 seq = uclass_resolve_seq(dev);
462 dev->flags |= DM_FLAG_ACTIVATED;
465 * Process pinctrl for everything except the root device, and
466 * continue regardless of the result of pinctrl. Don't process pinctrl
467 * settings for pinctrl devices since the device may not yet be
470 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
471 pinctrl_select_state(dev, "default");
473 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
474 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
475 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
476 ret = dev_power_domain_on(dev);
481 ret = uclass_pre_probe_device(dev);
485 if (dev->parent && dev->parent->driver->child_pre_probe) {
486 ret = dev->parent->driver->child_pre_probe(dev);
491 /* Only handle devices that have a valid ofnode */
492 if (dev_of_valid(dev)) {
494 * Process 'assigned-{clocks/clock-parents/clock-rates}'
497 ret = clk_set_defaults(dev, 0);
503 ret = drv->probe(dev);
508 ret = uclass_post_probe_device(dev);
512 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
513 pinctrl_select_state(dev, "default");
517 if (device_remove(dev, DM_REMOVE_NORMAL)) {
518 dm_warn("%s: Device '%s' failed to remove on error path\n",
519 __func__, dev->name);
522 dev->flags &= ~DM_FLAG_ACTIVATED;
530 void *dev_get_plat(const struct udevice *dev)
533 dm_warn("%s: null device\n", __func__);
540 void *dev_get_parent_plat(const struct udevice *dev)
543 dm_warn("%s: null device\n", __func__);
547 return dev->parent_plat;
550 void *dev_get_uclass_plat(const struct udevice *dev)
553 dm_warn("%s: null device\n", __func__);
557 return dev->uclass_plat;
560 void *dev_get_priv(const struct udevice *dev)
563 dm_warn("%s: null device\n", __func__);
570 void *dev_get_uclass_priv(const struct udevice *dev)
573 dm_warn("%s: null device\n", __func__);
577 return dev->uclass_priv;
580 void *dev_get_parent_priv(const struct udevice *dev)
583 dm_warn("%s: null device\n", __func__);
587 return dev->parent_priv;
590 static int device_get_device_tail(struct udevice *dev, int ret,
591 struct udevice **devp)
596 ret = device_probe(dev);
605 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
607 * device_find_by_ofnode() - Return device associated with given ofnode
609 * The returned device is *not* activated.
611 * @node: The ofnode for which a associated device should be looked up
612 * @devp: Pointer to structure to hold the found device
613 * Return: 0 if OK, -ve on error
615 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
621 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
622 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
634 int device_get_child(const struct udevice *parent, int index,
635 struct udevice **devp)
639 list_for_each_entry(dev, &parent->child_head, sibling_node) {
641 return device_get_device_tail(dev, 0, devp);
647 int device_get_child_count(const struct udevice *parent)
652 list_for_each_entry(dev, &parent->child_head, sibling_node)
658 int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
659 bool find_req_seq, struct udevice **devp)
665 list_for_each_entry(dev, &parent->child_head, sibling_node) {
666 if (dev->sqq == seq_or_req_seq) {
675 int device_get_child_by_seq(const struct udevice *parent, int seq,
676 struct udevice **devp)
682 ret = device_find_child_by_seq(parent, seq, false, &dev);
683 if (ret == -ENODEV) {
685 * We didn't find it in probed devices. See if there is one
686 * that will request this seq if probed.
688 ret = device_find_child_by_seq(parent, seq, true, &dev);
690 return device_get_device_tail(dev, ret, devp);
693 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
694 struct udevice **devp)
700 list_for_each_entry(dev, &parent->child_head, sibling_node) {
701 if (dev_of_offset(dev) == of_offset) {
710 int device_get_child_by_of_offset(const struct udevice *parent, int node,
711 struct udevice **devp)
717 ret = device_find_child_by_of_offset(parent, node, &dev);
718 return device_get_device_tail(dev, ret, devp);
721 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
724 struct udevice *dev, *found;
726 if (ofnode_equal(dev_ofnode(parent), ofnode))
729 list_for_each_entry(dev, &parent->child_head, sibling_node) {
730 found = _device_find_global_by_ofnode(dev, ofnode);
738 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
740 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
742 return *devp ? 0 : -ENOENT;
745 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
749 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
750 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
753 #if CONFIG_IS_ENABLED(OF_PLATDATA)
754 int device_get_by_driver_info(const struct driver_info *info,
755 struct udevice **devp)
757 struct driver_info *info_base =
758 ll_entry_start(struct driver_info, driver_info);
759 int idx = info - info_base;
760 struct driver_rt *drt = gd_dm_driver_rt() + idx;
766 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
769 int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
771 struct driver_rt *drt = gd_dm_driver_rt() + idx;
777 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
781 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
783 if (list_empty(&parent->child_head)) {
786 *devp = list_first_entry(&parent->child_head, struct udevice,
793 int device_find_next_child(struct udevice **devp)
795 struct udevice *dev = *devp;
796 struct udevice *parent = dev->parent;
798 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
801 *devp = list_entry(dev->sibling_node.next, struct udevice,
808 int device_find_first_inactive_child(const struct udevice *parent,
809 enum uclass_id uclass_id,
810 struct udevice **devp)
815 list_for_each_entry(dev, &parent->child_head, sibling_node) {
816 if (!device_active(dev) &&
817 device_get_uclass_id(dev) == uclass_id) {
826 int device_find_first_child_by_uclass(const struct udevice *parent,
827 enum uclass_id uclass_id,
828 struct udevice **devp)
833 list_for_each_entry(dev, &parent->child_head, sibling_node) {
834 if (device_get_uclass_id(dev) == uclass_id) {
843 int device_find_child_by_name(const struct udevice *parent, const char *name,
844 struct udevice **devp)
850 list_for_each_entry(dev, &parent->child_head, sibling_node) {
851 if (!strcmp(dev->name, name)) {
860 int device_first_child_err(struct udevice *parent, struct udevice **devp)
864 device_find_first_child(parent, &dev);
868 return device_get_device_tail(dev, 0, devp);
871 int device_next_child_err(struct udevice **devp)
873 struct udevice *dev = *devp;
875 device_find_next_child(&dev);
879 return device_get_device_tail(dev, 0, devp);
882 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
887 device_find_first_child(parent, &dev);
891 ret = device_of_to_plat(dev);
900 int device_next_child_ofdata_err(struct udevice **devp)
902 struct udevice *dev = *devp;
905 device_find_next_child(&dev);
909 ret = device_of_to_plat(dev);
918 struct udevice *dev_get_parent(const struct udevice *child)
920 return child->parent;
923 ulong dev_get_driver_data(const struct udevice *dev)
925 return dev->driver_data;
928 const void *dev_get_driver_ops(const struct udevice *dev)
930 if (!dev || !dev->driver->ops)
933 return dev->driver->ops;
936 enum uclass_id device_get_uclass_id(const struct udevice *dev)
938 return dev->uclass->uc_drv->id;
941 const char *dev_get_uclass_name(const struct udevice *dev)
946 return dev->uclass->uc_drv->name;
949 bool device_has_children(const struct udevice *dev)
951 return !list_empty(&dev->child_head);
954 bool device_has_active_children(const struct udevice *dev)
956 struct udevice *child;
958 for (device_find_first_child(dev, &child);
960 device_find_next_child(&child)) {
961 if (device_active(child))
968 bool device_is_last_sibling(const struct udevice *dev)
970 struct udevice *parent = dev->parent;
974 return list_is_last(&dev->sibling_node, &parent->child_head);
977 void device_set_name_alloced(struct udevice *dev)
979 dev->flags |= DM_FLAG_NAME_ALLOCED;
982 int device_set_name(struct udevice *dev, const char *name)
988 device_set_name_alloced(dev);
993 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
994 bool device_is_compatible(const struct udevice *dev, const char *compat)
996 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
999 bool of_machine_is_compatible(const char *compat)
1001 const void *fdt = gd->fdt_blob;
1003 return !fdt_node_check_compatible(fdt, 0, compat);
1006 int dev_disable_by_path(const char *path)
1009 ofnode node = ofnode_path(path);
1010 struct udevice *dev;
1013 if (!of_live_active())
1016 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1017 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1025 ret = device_remove(dev, DM_REMOVE_NORMAL);
1029 ret = device_unbind(dev);
1033 return ofnode_set_enabled(node, false);
1036 int dev_enable_by_path(const char *path)
1038 ofnode node = ofnode_path(path);
1039 ofnode pnode = ofnode_get_parent(node);
1040 struct udevice *parent;
1043 if (!of_live_active())
1046 ret = device_find_by_ofnode(pnode, &parent);
1050 ret = ofnode_set_enabled(node, true);
1054 return lists_bind_fdt(parent, node, NULL, false);