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)
52 ret = uclass_get(drv->id, &uc);
54 debug("Missing uclass for driver %s\n", drv->name);
58 dev = calloc(1, sizeof(struct udevice));
62 INIT_LIST_HEAD(&dev->sibling_node);
63 INIT_LIST_HEAD(&dev->child_head);
64 INIT_LIST_HEAD(&dev->uclass_node);
66 INIT_LIST_HEAD(&dev->devres_head);
68 dev_set_plat(dev, plat);
69 dev->driver_data = driver_data;
77 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
78 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
80 * Some devices, such as a SPI bus, I2C bus and serial ports
81 * are numbered using aliases.
83 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
84 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
85 if (uc->uc_drv->name && ofnode_valid(node)) {
86 if (!dev_read_alias_seq(dev, &dev->seq_))
91 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
92 dev->seq_ = uclass_find_next_free_seq(uc);
97 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
99 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
100 if (of_plat_size < drv->plat_auto)
105 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
106 ptr = calloc(1, drv->plat_auto);
111 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
112 memcpy(ptr, plat, of_plat_size);
113 dev_set_plat(dev, ptr);
117 size = uc->uc_drv->per_device_plat_auto;
119 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
120 ptr = calloc(1, size);
125 dev_set_uclass_plat(dev, ptr);
129 size = parent->driver->per_child_plat_auto;
131 size = parent->uclass->uc_drv->per_child_plat_auto;
134 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
135 ptr = calloc(1, size);
140 dev_set_parent_plat(dev, ptr);
142 /* put dev into parent's successor list */
143 list_add_tail(&dev->sibling_node, &parent->child_head);
146 ret = uclass_bind_device(dev);
148 goto fail_uclass_bind;
150 /* if we fail to bind we remove device from successors and free it */
152 ret = drv->bind(dev);
156 if (parent && parent->driver->child_post_bind) {
157 ret = parent->driver->child_post_bind(dev);
159 goto fail_child_post_bind;
161 if (uc->uc_drv->post_bind) {
162 ret = uc->uc_drv->post_bind(dev);
164 goto fail_uclass_post_bind;
168 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
172 dev_or_flags(dev, DM_FLAG_BOUND);
176 fail_uclass_post_bind:
177 /* There is no child unbind() method, so no clean-up required */
178 fail_child_post_bind:
179 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
180 if (drv->unbind && drv->unbind(dev)) {
181 dm_warn("unbind() method failed on dev '%s' on error path\n",
187 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
188 if (uclass_unbind_device(dev)) {
189 dm_warn("Failed to unbind dev '%s' on error path\n",
194 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
195 list_del(&dev->sibling_node);
196 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
197 free(dev_get_parent_plat(dev));
198 dev_set_parent_plat(dev, NULL);
202 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
203 free(dev_get_uclass_plat(dev));
204 dev_set_uclass_plat(dev, NULL);
207 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
208 free(dev_get_plat(dev));
209 dev_set_plat(dev, NULL);
212 devres_release_all(dev);
219 int device_bind_with_driver_data(struct udevice *parent,
220 const struct driver *drv, const char *name,
221 ulong driver_data, ofnode node,
222 struct udevice **devp)
224 return device_bind_common(parent, drv, name, NULL, driver_data, node,
228 int device_bind(struct udevice *parent, const struct driver *drv,
229 const char *name, void *plat, ofnode node,
230 struct udevice **devp)
232 return device_bind_common(parent, drv, name, plat, 0, node, 0,
236 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
237 const struct driver_info *info, struct udevice **devp)
243 drv = lists_driver_lookup_name(info->name);
246 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
249 #if CONFIG_IS_ENABLED(OF_PLATDATA)
250 plat_size = info->plat_size;
252 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
253 ofnode_null(), plat_size, devp);
260 int device_reparent(struct udevice *dev, struct udevice *new_parent)
262 struct udevice *pos, *n;
267 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
269 if (pos->driver != dev->driver)
272 list_del(&dev->sibling_node);
273 list_add_tail(&dev->sibling_node, &new_parent->child_head);
274 dev->parent = new_parent;
282 static void *alloc_priv(int size, uint flags)
286 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
287 size = ROUND(size, ARCH_DMA_MINALIGN);
288 priv = memalign(ARCH_DMA_MINALIGN, size);
290 memset(priv, '\0', size);
293 * Ensure that the zero bytes are flushed to memory.
294 * This prevents problems if the driver uses this as
295 * both an input and an output buffer:
297 * 1. Zeroes written to buffer (here) and sit in the
299 * 2. Driver issues a read command to DMA
300 * 3. CPU runs out of cache space and evicts some cache
301 * data in the buffer, writing zeroes to RAM from
304 * 5. Buffer now has some DMA data and some zeroes
305 * 6. Data being read is now incorrect
307 * To prevent this, ensure that the cache is clean
308 * within this range at the start. The driver can then
309 * use normal flush-after-write, invalidate-before-read
312 * TODO(sjg@chromium.org): Drop this microblaze
315 #ifndef CONFIG_MICROBLAZE
316 flush_dcache_range((ulong)priv, (ulong)priv + size);
320 priv = calloc(1, size);
327 * device_alloc_priv() - Allocate priv/plat data required by the device
329 * @dev: Device to process
330 * @return 0 if OK, -ENOMEM if out of memory
332 static int device_alloc_priv(struct udevice *dev)
334 const struct driver *drv;
341 /* Allocate private data if requested and not reentered */
342 if (drv->priv_auto && !dev_get_priv(dev)) {
343 ptr = alloc_priv(drv->priv_auto, drv->flags);
346 dev_set_priv(dev, ptr);
349 /* Allocate private data if requested and not reentered */
350 size = dev->uclass->uc_drv->per_device_auto;
351 if (size && !dev_get_uclass_priv(dev)) {
352 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
355 dev_set_uclass_priv(dev, ptr);
358 /* Allocate parent data for this child */
360 size = dev->parent->driver->per_child_auto;
362 size = dev->parent->uclass->uc_drv->per_child_auto;
363 if (size && !dev_get_parent_priv(dev)) {
364 ptr = alloc_priv(size, drv->flags);
367 dev_set_parent_priv(dev, ptr);
374 int device_of_to_plat(struct udevice *dev)
376 const struct driver *drv;
382 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
385 /* Ensure all parents have ofdata */
387 ret = device_of_to_plat(dev->parent);
392 * The device might have already been probed during
393 * the call to device_probe() on its parent device
394 * (e.g. PCI bridge devices). Test the flags again
395 * so that we don't mess up the device.
397 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
401 ret = device_alloc_priv(dev);
408 if (drv->of_to_plat &&
409 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
410 ret = drv->of_to_plat(dev);
415 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
424 int device_probe(struct udevice *dev)
426 const struct driver *drv;
432 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
438 ret = device_of_to_plat(dev);
442 /* Ensure all parents are probed */
444 ret = device_probe(dev->parent);
449 * The device might have already been probed during
450 * the call to device_probe() on its parent device
451 * (e.g. PCI bridge devices). Test the flags again
452 * so that we don't mess up the device.
454 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
458 dev_or_flags(dev, DM_FLAG_ACTIVATED);
461 * Process pinctrl for everything except the root device, and
462 * continue regardless of the result of pinctrl. Don't process pinctrl
463 * settings for pinctrl devices since the device may not yet be
466 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
467 pinctrl_select_state(dev, "default");
469 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
470 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
471 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
472 ret = dev_power_domain_on(dev);
477 ret = uclass_pre_probe_device(dev);
481 if (dev->parent && dev->parent->driver->child_pre_probe) {
482 ret = dev->parent->driver->child_pre_probe(dev);
487 /* Only handle devices that have a valid ofnode */
488 if (dev_has_ofnode(dev)) {
490 * Process 'assigned-{clocks/clock-parents/clock-rates}'
493 ret = clk_set_defaults(dev, 0);
499 ret = drv->probe(dev);
504 ret = uclass_post_probe_device(dev);
508 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
509 pinctrl_select_state(dev, "default");
513 if (device_remove(dev, DM_REMOVE_NORMAL)) {
514 dm_warn("%s: Device '%s' failed to remove on error path\n",
515 __func__, dev->name);
518 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
525 void *dev_get_plat(const struct udevice *dev)
528 dm_warn("%s: null device\n", __func__);
535 void *dev_get_parent_plat(const struct udevice *dev)
538 dm_warn("%s: null device\n", __func__);
542 return dev->parent_plat_;
545 void *dev_get_uclass_plat(const struct udevice *dev)
548 dm_warn("%s: null device\n", __func__);
552 return dev->uclass_plat_;
555 void *dev_get_priv(const struct udevice *dev)
558 dm_warn("%s: null device\n", __func__);
565 void *dev_get_uclass_priv(const struct udevice *dev)
568 dm_warn("%s: null device\n", __func__);
572 return dev->uclass_priv_;
575 void *dev_get_parent_priv(const struct udevice *dev)
578 dm_warn("%s: null device\n", __func__);
582 return dev->parent_priv_;
585 static int device_get_device_tail(struct udevice *dev, int ret,
586 struct udevice **devp)
591 ret = device_probe(dev);
600 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
602 * device_find_by_ofnode() - Return device associated with given ofnode
604 * The returned device is *not* activated.
606 * @node: The ofnode for which a associated device should be looked up
607 * @devp: Pointer to structure to hold the found device
608 * Return: 0 if OK, -ve on error
610 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
616 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
617 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
629 int device_get_child(const struct udevice *parent, int index,
630 struct udevice **devp)
634 list_for_each_entry(dev, &parent->child_head, sibling_node) {
636 return device_get_device_tail(dev, 0, devp);
642 int device_get_child_count(const struct udevice *parent)
647 list_for_each_entry(dev, &parent->child_head, sibling_node)
653 int device_find_child_by_seq(const struct udevice *parent, int seq,
654 struct udevice **devp)
660 list_for_each_entry(dev, &parent->child_head, sibling_node) {
661 if (dev->seq_ == seq) {
670 int device_get_child_by_seq(const struct udevice *parent, int seq,
671 struct udevice **devp)
677 ret = device_find_child_by_seq(parent, seq, &dev);
679 return device_get_device_tail(dev, ret, devp);
682 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
683 struct udevice **devp)
689 list_for_each_entry(dev, &parent->child_head, sibling_node) {
690 if (dev_of_offset(dev) == of_offset) {
699 int device_get_child_by_of_offset(const struct udevice *parent, int node,
700 struct udevice **devp)
706 ret = device_find_child_by_of_offset(parent, node, &dev);
707 return device_get_device_tail(dev, ret, devp);
710 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
713 struct udevice *dev, *found;
715 if (ofnode_equal(dev_ofnode(parent), ofnode))
718 list_for_each_entry(dev, &parent->child_head, sibling_node) {
719 found = _device_find_global_by_ofnode(dev, ofnode);
727 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
729 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
731 return *devp ? 0 : -ENOENT;
734 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
738 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
739 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
742 #if CONFIG_IS_ENABLED(OF_PLATDATA)
743 int device_get_by_driver_info(const struct driver_info *info,
744 struct udevice **devp)
746 struct driver_info *info_base =
747 ll_entry_start(struct driver_info, driver_info);
748 int idx = info - info_base;
749 struct driver_rt *drt = gd_dm_driver_rt() + idx;
755 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
758 int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
760 struct driver_rt *drt = gd_dm_driver_rt() + idx;
766 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
770 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
772 if (list_empty(&parent->child_head)) {
775 *devp = list_first_entry(&parent->child_head, struct udevice,
782 int device_find_next_child(struct udevice **devp)
784 struct udevice *dev = *devp;
785 struct udevice *parent = dev->parent;
787 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
790 *devp = list_entry(dev->sibling_node.next, struct udevice,
797 int device_find_first_inactive_child(const struct udevice *parent,
798 enum uclass_id uclass_id,
799 struct udevice **devp)
804 list_for_each_entry(dev, &parent->child_head, sibling_node) {
805 if (!device_active(dev) &&
806 device_get_uclass_id(dev) == uclass_id) {
815 int device_find_first_child_by_uclass(const struct udevice *parent,
816 enum uclass_id uclass_id,
817 struct udevice **devp)
822 list_for_each_entry(dev, &parent->child_head, sibling_node) {
823 if (device_get_uclass_id(dev) == uclass_id) {
832 int device_find_child_by_name(const struct udevice *parent, const char *name,
833 struct udevice **devp)
839 list_for_each_entry(dev, &parent->child_head, sibling_node) {
840 if (!strcmp(dev->name, name)) {
849 int device_first_child_err(struct udevice *parent, struct udevice **devp)
853 device_find_first_child(parent, &dev);
857 return device_get_device_tail(dev, 0, devp);
860 int device_next_child_err(struct udevice **devp)
862 struct udevice *dev = *devp;
864 device_find_next_child(&dev);
868 return device_get_device_tail(dev, 0, devp);
871 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
876 device_find_first_child(parent, &dev);
880 ret = device_of_to_plat(dev);
889 int device_next_child_ofdata_err(struct udevice **devp)
891 struct udevice *dev = *devp;
894 device_find_next_child(&dev);
898 ret = device_of_to_plat(dev);
907 struct udevice *dev_get_parent(const struct udevice *child)
909 return child->parent;
912 ulong dev_get_driver_data(const struct udevice *dev)
914 return dev->driver_data;
917 const void *dev_get_driver_ops(const struct udevice *dev)
919 if (!dev || !dev->driver->ops)
922 return dev->driver->ops;
925 enum uclass_id device_get_uclass_id(const struct udevice *dev)
927 return dev->uclass->uc_drv->id;
930 const char *dev_get_uclass_name(const struct udevice *dev)
935 return dev->uclass->uc_drv->name;
938 bool device_has_children(const struct udevice *dev)
940 return !list_empty(&dev->child_head);
943 bool device_has_active_children(const struct udevice *dev)
945 struct udevice *child;
947 for (device_find_first_child(dev, &child);
949 device_find_next_child(&child)) {
950 if (device_active(child))
957 bool device_is_last_sibling(const struct udevice *dev)
959 struct udevice *parent = dev->parent;
963 return list_is_last(&dev->sibling_node, &parent->child_head);
966 void device_set_name_alloced(struct udevice *dev)
968 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
971 int device_set_name(struct udevice *dev, const char *name)
977 device_set_name_alloced(dev);
982 void dev_set_priv(struct udevice *dev, void *priv)
987 void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
989 dev->parent_priv_ = parent_priv;
992 void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
994 dev->uclass_priv_ = uclass_priv;
997 void dev_set_plat(struct udevice *dev, void *plat)
1002 void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1004 dev->parent_plat_ = parent_plat;
1007 void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1009 dev->uclass_plat_ = uclass_plat;
1012 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
1013 bool device_is_compatible(const struct udevice *dev, const char *compat)
1015 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
1018 bool of_machine_is_compatible(const char *compat)
1020 const void *fdt = gd->fdt_blob;
1022 return !fdt_node_check_compatible(fdt, 0, compat);
1025 int dev_disable_by_path(const char *path)
1028 ofnode node = ofnode_path(path);
1029 struct udevice *dev;
1032 if (!of_live_active())
1035 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1036 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1044 ret = device_remove(dev, DM_REMOVE_NORMAL);
1048 ret = device_unbind(dev);
1052 return ofnode_set_enabled(node, false);
1055 int dev_enable_by_path(const char *path)
1057 ofnode node = ofnode_path(path);
1058 ofnode pnode = ofnode_get_parent(node);
1059 struct udevice *parent;
1062 if (!of_live_active())
1065 ret = device_find_by_ofnode(pnode, &parent);
1069 ret = ofnode_set_enabled(node, true);
1073 return lists_bind_fdt(parent, node, NULL, false);