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 *platdata,
38 ulong driver_data, ofnode node,
39 uint of_platdata_size, struct udevice **devp)
50 ret = uclass_get(drv->id, &uc);
52 debug("Missing uclass for driver %s\n", drv->name);
56 dev = calloc(1, sizeof(struct udevice));
60 INIT_LIST_HEAD(&dev->sibling_node);
61 INIT_LIST_HEAD(&dev->child_head);
62 INIT_LIST_HEAD(&dev->uclass_node);
64 INIT_LIST_HEAD(&dev->devres_head);
66 dev->platdata = platdata;
67 dev->driver_data = driver_data;
76 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
77 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
79 * Some devices, such as a SPI bus, I2C bus and serial ports
80 * are numbered using aliases.
82 * This is just a 'requested' sequence, and will be
83 * resolved (and ->seq updated) when the device is probed.
85 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
86 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
87 if (uc->uc_drv->name && ofnode_valid(node))
88 dev_read_alias_seq(dev, &dev->req_seq);
89 #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
90 if (dev->req_seq == -1)
92 uclass_find_next_free_req_seq(drv->id);
95 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
99 if (drv->platdata_auto_alloc_size) {
100 bool alloc = !platdata;
102 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
103 if (of_platdata_size) {
104 dev->flags |= DM_FLAG_OF_PLATDATA;
105 if (of_platdata_size <
106 drv->platdata_auto_alloc_size)
111 dev->flags |= DM_FLAG_ALLOC_PDATA;
112 dev->platdata = calloc(1,
113 drv->platdata_auto_alloc_size);
114 if (!dev->platdata) {
118 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
119 memcpy(dev->platdata, platdata,
125 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
127 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
128 dev->uclass_platdata = calloc(1, size);
129 if (!dev->uclass_platdata) {
136 size = parent->driver->per_child_platdata_auto_alloc_size;
138 size = parent->uclass->uc_drv->
139 per_child_platdata_auto_alloc_size;
142 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
143 dev->parent_platdata = calloc(1, size);
144 if (!dev->parent_platdata) {
149 /* put dev into parent's successor list */
150 list_add_tail(&dev->sibling_node, &parent->child_head);
153 ret = uclass_bind_device(dev);
155 goto fail_uclass_bind;
157 /* if we fail to bind we remove device from successors and free it */
159 ret = drv->bind(dev);
163 if (parent && parent->driver->child_post_bind) {
164 ret = parent->driver->child_post_bind(dev);
166 goto fail_child_post_bind;
168 if (uc->uc_drv->post_bind) {
169 ret = uc->uc_drv->post_bind(dev);
171 goto fail_uclass_post_bind;
175 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
179 dev->flags |= DM_FLAG_BOUND;
183 fail_uclass_post_bind:
184 /* There is no child unbind() method, so no clean-up required */
185 fail_child_post_bind:
186 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
187 if (drv->unbind && drv->unbind(dev)) {
188 dm_warn("unbind() method failed on dev '%s' on error path\n",
194 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
195 if (uclass_unbind_device(dev)) {
196 dm_warn("Failed to unbind dev '%s' on error path\n",
201 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
202 list_del(&dev->sibling_node);
203 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
204 free(dev->parent_platdata);
205 dev->parent_platdata = NULL;
209 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
210 free(dev->uclass_platdata);
211 dev->uclass_platdata = NULL;
214 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
216 dev->platdata = NULL;
219 devres_release_all(dev);
226 int device_bind_with_driver_data(struct udevice *parent,
227 const struct driver *drv, const char *name,
228 ulong driver_data, ofnode node,
229 struct udevice **devp)
231 return device_bind_common(parent, drv, name, NULL, driver_data, node,
235 int device_bind(struct udevice *parent, const struct driver *drv,
236 const char *name, void *platdata, int of_offset,
237 struct udevice **devp)
239 return device_bind_common(parent, drv, name, platdata, 0,
240 offset_to_ofnode(of_offset), 0, devp);
243 int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
244 const char *name, void *platdata, ofnode node,
245 struct udevice **devp)
247 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
251 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
252 const struct driver_info *info, struct udevice **devp)
255 uint platdata_size = 0;
258 drv = lists_driver_lookup_name(info->name);
261 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
264 #if CONFIG_IS_ENABLED(OF_PLATDATA)
265 platdata_size = info->platdata_size;
267 ret = device_bind_common(parent, drv, info->name,
268 (void *)info->platdata, 0, ofnode_null(),
269 platdata_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);
342 int device_ofdata_to_platdata(struct udevice *dev)
344 const struct driver *drv;
351 if (dev->flags & DM_FLAG_PLATDATA_VALID)
354 /* Ensure all parents have ofdata */
356 ret = device_ofdata_to_platdata(dev->parent);
361 * The device might have already been probed during
362 * the call to device_probe() on its parent device
363 * (e.g. PCI bridge devices). Test the flags again
364 * so that we don't mess up the device.
366 if (dev->flags & DM_FLAG_PLATDATA_VALID)
373 /* Allocate private data if requested and not reentered */
374 if (drv->priv_auto_alloc_size && !dev->priv) {
375 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
381 /* Allocate private data if requested and not reentered */
382 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
383 if (size && !dev->uclass_priv) {
384 dev->uclass_priv = alloc_priv(size,
385 dev->uclass->uc_drv->flags);
386 if (!dev->uclass_priv) {
392 /* Allocate parent data for this child */
394 size = dev->parent->driver->per_child_auto_alloc_size;
396 size = dev->parent->uclass->uc_drv->
397 per_child_auto_alloc_size;
399 if (size && !dev->parent_priv) {
400 dev->parent_priv = alloc_priv(size, drv->flags);
401 if (!dev->parent_priv) {
408 if (drv->ofdata_to_platdata &&
409 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
410 ret = drv->ofdata_to_platdata(dev);
415 dev->flags |= DM_FLAG_PLATDATA_VALID;
424 int device_probe(struct udevice *dev)
426 const struct driver *drv;
433 if (dev->flags & DM_FLAG_ACTIVATED)
439 ret = device_ofdata_to_platdata(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->flags & DM_FLAG_ACTIVATED)
459 seq = uclass_resolve_seq(dev);
466 dev->flags |= DM_FLAG_ACTIVATED;
469 * Process pinctrl for everything except the root device, and
470 * continue regardless of the result of pinctrl. Don't process pinctrl
471 * settings for pinctrl devices since the device may not yet be
474 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
475 pinctrl_select_state(dev, "default");
477 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
478 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
479 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
480 ret = dev_power_domain_on(dev);
485 ret = uclass_pre_probe_device(dev);
489 if (dev->parent && dev->parent->driver->child_pre_probe) {
490 ret = dev->parent->driver->child_pre_probe(dev);
495 /* Only handle devices that have a valid ofnode */
496 if (dev_of_valid(dev)) {
498 * Process 'assigned-{clocks/clock-parents/clock-rates}'
501 ret = clk_set_defaults(dev, 0);
507 ret = drv->probe(dev);
512 ret = uclass_post_probe_device(dev);
516 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
517 pinctrl_select_state(dev, "default");
521 if (device_remove(dev, DM_REMOVE_NORMAL)) {
522 dm_warn("%s: Device '%s' failed to remove on error path\n",
523 __func__, dev->name);
526 dev->flags &= ~DM_FLAG_ACTIVATED;
534 void *dev_get_platdata(const struct udevice *dev)
537 dm_warn("%s: null device\n", __func__);
541 return dev->platdata;
544 void *dev_get_parent_platdata(const struct udevice *dev)
547 dm_warn("%s: null device\n", __func__);
551 return dev->parent_platdata;
554 void *dev_get_uclass_platdata(const struct udevice *dev)
557 dm_warn("%s: null device\n", __func__);
561 return dev->uclass_platdata;
564 void *dev_get_priv(const struct udevice *dev)
567 dm_warn("%s: null device\n", __func__);
574 void *dev_get_uclass_priv(const struct udevice *dev)
577 dm_warn("%s: null device\n", __func__);
581 return dev->uclass_priv;
584 void *dev_get_parent_priv(const struct udevice *dev)
587 dm_warn("%s: null device\n", __func__);
591 return dev->parent_priv;
594 static int device_get_device_tail(struct udevice *dev, int ret,
595 struct udevice **devp)
600 ret = device_probe(dev);
609 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
611 * device_find_by_ofnode() - Return device associated with given ofnode
613 * The returned device is *not* activated.
615 * @node: The ofnode for which a associated device should be looked up
616 * @devp: Pointer to structure to hold the found device
617 * Return: 0 if OK, -ve on error
619 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
625 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
626 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
638 int device_get_child(const struct udevice *parent, int index,
639 struct udevice **devp)
643 list_for_each_entry(dev, &parent->child_head, sibling_node) {
645 return device_get_device_tail(dev, 0, devp);
651 int device_get_child_count(const struct udevice *parent)
656 list_for_each_entry(dev, &parent->child_head, sibling_node)
662 int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
663 bool find_req_seq, struct udevice **devp)
668 if (seq_or_req_seq == -1)
671 list_for_each_entry(dev, &parent->child_head, sibling_node) {
672 if ((find_req_seq ? dev->req_seq : dev->seq) ==
682 int device_get_child_by_seq(const struct udevice *parent, int seq,
683 struct udevice **devp)
689 ret = device_find_child_by_seq(parent, seq, false, &dev);
690 if (ret == -ENODEV) {
692 * We didn't find it in probed devices. See if there is one
693 * that will request this seq if probed.
695 ret = device_find_child_by_seq(parent, seq, true, &dev);
697 return device_get_device_tail(dev, ret, devp);
700 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
701 struct udevice **devp)
707 list_for_each_entry(dev, &parent->child_head, sibling_node) {
708 if (dev_of_offset(dev) == of_offset) {
717 int device_get_child_by_of_offset(const struct udevice *parent, int node,
718 struct udevice **devp)
724 ret = device_find_child_by_of_offset(parent, node, &dev);
725 return device_get_device_tail(dev, ret, devp);
728 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
731 struct udevice *dev, *found;
733 if (ofnode_equal(dev_ofnode(parent), ofnode))
736 list_for_each_entry(dev, &parent->child_head, sibling_node) {
737 found = _device_find_global_by_ofnode(dev, ofnode);
745 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
747 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
749 return *devp ? 0 : -ENOENT;
752 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
756 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
757 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
760 #if CONFIG_IS_ENABLED(OF_PLATDATA)
761 int device_get_by_driver_info(const struct driver_info *info,
762 struct udevice **devp)
764 struct driver_info *info_base =
765 ll_entry_start(struct driver_info, driver_info);
766 int idx = info - info_base;
767 struct driver_rt *drt = gd_dm_driver_rt() + idx;
773 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
777 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
779 if (list_empty(&parent->child_head)) {
782 *devp = list_first_entry(&parent->child_head, struct udevice,
789 int device_find_next_child(struct udevice **devp)
791 struct udevice *dev = *devp;
792 struct udevice *parent = dev->parent;
794 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
797 *devp = list_entry(dev->sibling_node.next, struct udevice,
804 int device_find_first_inactive_child(const struct udevice *parent,
805 enum uclass_id uclass_id,
806 struct udevice **devp)
811 list_for_each_entry(dev, &parent->child_head, sibling_node) {
812 if (!device_active(dev) &&
813 device_get_uclass_id(dev) == uclass_id) {
822 int device_find_first_child_by_uclass(const struct udevice *parent,
823 enum uclass_id uclass_id,
824 struct udevice **devp)
829 list_for_each_entry(dev, &parent->child_head, sibling_node) {
830 if (device_get_uclass_id(dev) == uclass_id) {
839 int device_find_child_by_name(const struct udevice *parent, const char *name,
840 struct udevice **devp)
846 list_for_each_entry(dev, &parent->child_head, sibling_node) {
847 if (!strcmp(dev->name, name)) {
856 int device_first_child_err(struct udevice *parent, struct udevice **devp)
860 device_find_first_child(parent, &dev);
864 return device_get_device_tail(dev, 0, devp);
867 int device_next_child_err(struct udevice **devp)
869 struct udevice *dev = *devp;
871 device_find_next_child(&dev);
875 return device_get_device_tail(dev, 0, devp);
878 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
883 device_find_first_child(parent, &dev);
887 ret = device_ofdata_to_platdata(dev);
896 int device_next_child_ofdata_err(struct udevice **devp)
898 struct udevice *dev = *devp;
901 device_find_next_child(&dev);
905 ret = device_ofdata_to_platdata(dev);
914 struct udevice *dev_get_parent(const struct udevice *child)
916 return child->parent;
919 ulong dev_get_driver_data(const struct udevice *dev)
921 return dev->driver_data;
924 const void *dev_get_driver_ops(const struct udevice *dev)
926 if (!dev || !dev->driver->ops)
929 return dev->driver->ops;
932 enum uclass_id device_get_uclass_id(const struct udevice *dev)
934 return dev->uclass->uc_drv->id;
937 const char *dev_get_uclass_name(const struct udevice *dev)
942 return dev->uclass->uc_drv->name;
945 bool device_has_children(const struct udevice *dev)
947 return !list_empty(&dev->child_head);
950 bool device_has_active_children(const struct udevice *dev)
952 struct udevice *child;
954 for (device_find_first_child(dev, &child);
956 device_find_next_child(&child)) {
957 if (device_active(child))
964 bool device_is_last_sibling(const struct udevice *dev)
966 struct udevice *parent = dev->parent;
970 return list_is_last(&dev->sibling_node, &parent->child_head);
973 void device_set_name_alloced(struct udevice *dev)
975 dev->flags |= DM_FLAG_NAME_ALLOCED;
978 int device_set_name(struct udevice *dev, const char *name)
984 device_set_name_alloced(dev);
989 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
990 bool device_is_compatible(const struct udevice *dev, const char *compat)
992 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
995 bool of_machine_is_compatible(const char *compat)
997 const void *fdt = gd->fdt_blob;
999 return !fdt_node_check_compatible(fdt, 0, compat);
1002 int dev_disable_by_path(const char *path)
1005 ofnode node = ofnode_path(path);
1006 struct udevice *dev;
1009 if (!of_live_active())
1012 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1013 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1021 ret = device_remove(dev, DM_REMOVE_NORMAL);
1025 ret = device_unbind(dev);
1029 return ofnode_set_enabled(node, false);
1032 int dev_enable_by_path(const char *path)
1034 ofnode node = ofnode_path(path);
1035 ofnode pnode = ofnode_get_parent(node);
1036 struct udevice *parent;
1039 if (!of_live_active())
1042 ret = device_find_by_ofnode(pnode, &parent);
1046 ret = ofnode_set_enabled(node, true);
1050 return lists_bind_fdt(parent, node, NULL, false);