1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2013 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
15 #include <fdt_support.h>
17 #include <dm/device.h>
18 #include <dm/device-internal.h>
20 #include <dm/of_access.h>
21 #include <dm/pinctrl.h>
22 #include <dm/platdata.h>
24 #include <dm/uclass.h>
25 #include <dm/uclass-internal.h>
27 #include <linux/err.h>
28 #include <linux/list.h>
29 #include <power-domain.h>
31 DECLARE_GLOBAL_DATA_PTR;
33 static int device_bind_common(struct udevice *parent, const struct driver *drv,
34 const char *name, void *platdata,
35 ulong driver_data, ofnode node,
36 uint of_platdata_size, struct udevice **devp)
47 ret = uclass_get(drv->id, &uc);
49 debug("Missing uclass for driver %s\n", drv->name);
53 dev = calloc(1, sizeof(struct udevice));
57 INIT_LIST_HEAD(&dev->sibling_node);
58 INIT_LIST_HEAD(&dev->child_head);
59 INIT_LIST_HEAD(&dev->uclass_node);
61 INIT_LIST_HEAD(&dev->devres_head);
63 dev->platdata = platdata;
64 dev->driver_data = driver_data;
73 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
74 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
76 * Some devices, such as a SPI bus, I2C bus and serial ports
77 * are numbered using aliases.
79 * This is just a 'requested' sequence, and will be
80 * resolved (and ->seq updated) when the device is probed.
82 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
83 if (uc->uc_drv->name && ofnode_valid(node))
84 dev_read_alias_seq(dev, &dev->req_seq);
85 #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
86 if (dev->req_seq == -1)
88 uclass_find_next_free_req_seq(drv->id);
91 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
95 if (drv->platdata_auto_alloc_size) {
96 bool alloc = !platdata;
98 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
99 if (of_platdata_size) {
100 dev->flags |= DM_FLAG_OF_PLATDATA;
101 if (of_platdata_size <
102 drv->platdata_auto_alloc_size)
107 dev->flags |= DM_FLAG_ALLOC_PDATA;
108 dev->platdata = calloc(1,
109 drv->platdata_auto_alloc_size);
110 if (!dev->platdata) {
114 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
115 memcpy(dev->platdata, platdata,
121 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
123 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
124 dev->uclass_platdata = calloc(1, size);
125 if (!dev->uclass_platdata) {
132 size = parent->driver->per_child_platdata_auto_alloc_size;
134 size = parent->uclass->uc_drv->
135 per_child_platdata_auto_alloc_size;
138 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
139 dev->parent_platdata = calloc(1, size);
140 if (!dev->parent_platdata) {
147 /* put dev into parent's successor list */
149 list_add_tail(&dev->sibling_node, &parent->child_head);
151 ret = uclass_bind_device(dev);
153 goto fail_uclass_bind;
155 /* if we fail to bind we remove device from successors and free it */
157 ret = drv->bind(dev);
161 if (parent && parent->driver->child_post_bind) {
162 ret = parent->driver->child_post_bind(dev);
164 goto fail_child_post_bind;
166 if (uc->uc_drv->post_bind) {
167 ret = uc->uc_drv->post_bind(dev);
169 goto fail_uclass_post_bind;
173 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
177 dev->flags |= DM_FLAG_BOUND;
181 fail_uclass_post_bind:
182 /* There is no child unbind() method, so no clean-up required */
183 fail_child_post_bind:
184 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
185 if (drv->unbind && drv->unbind(dev)) {
186 dm_warn("unbind() method failed on dev '%s' on error path\n",
192 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
193 if (uclass_unbind_device(dev)) {
194 dm_warn("Failed to unbind dev '%s' on error path\n",
199 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
200 list_del(&dev->sibling_node);
201 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
202 free(dev->parent_platdata);
203 dev->parent_platdata = NULL;
207 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
208 free(dev->uclass_platdata);
209 dev->uclass_platdata = NULL;
212 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
214 dev->platdata = NULL;
217 devres_release_all(dev);
224 int device_bind_with_driver_data(struct udevice *parent,
225 const struct driver *drv, const char *name,
226 ulong driver_data, ofnode node,
227 struct udevice **devp)
229 return device_bind_common(parent, drv, name, NULL, driver_data, node,
233 int device_bind(struct udevice *parent, const struct driver *drv,
234 const char *name, void *platdata, int of_offset,
235 struct udevice **devp)
237 return device_bind_common(parent, drv, name, platdata, 0,
238 offset_to_ofnode(of_offset), 0, devp);
241 int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
242 const char *name, void *platdata, ofnode node,
243 struct udevice **devp)
245 return device_bind_common(parent, drv, name, platdata, 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)
253 uint platdata_size = 0;
255 drv = lists_driver_lookup_name(info->name);
258 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
261 #if CONFIG_IS_ENABLED(OF_PLATDATA)
262 platdata_size = info->platdata_size;
264 return device_bind_common(parent, drv, info->name,
265 (void *)info->platdata, 0, ofnode_null(), platdata_size,
269 static void *alloc_priv(int size, uint flags)
273 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
274 size = ROUND(size, ARCH_DMA_MINALIGN);
275 priv = memalign(ARCH_DMA_MINALIGN, size);
277 memset(priv, '\0', size);
280 * Ensure that the zero bytes are flushed to memory.
281 * This prevents problems if the driver uses this as
282 * both an input and an output buffer:
284 * 1. Zeroes written to buffer (here) and sit in the
286 * 2. Driver issues a read command to DMA
287 * 3. CPU runs out of cache space and evicts some cache
288 * data in the buffer, writing zeroes to RAM from
291 * 5. Buffer now has some DMA data and some zeroes
292 * 6. Data being read is now incorrect
294 * To prevent this, ensure that the cache is clean
295 * within this range at the start. The driver can then
296 * use normal flush-after-write, invalidate-before-read
299 * TODO(sjg@chromium.org): Drop this microblaze
302 #ifndef CONFIG_MICROBLAZE
303 flush_dcache_range((ulong)priv, (ulong)priv + size);
307 priv = calloc(1, size);
313 int device_probe(struct udevice *dev)
315 const struct driver *drv;
323 if (dev->flags & DM_FLAG_ACTIVATED)
329 /* Allocate private data if requested and not reentered */
330 if (drv->priv_auto_alloc_size && !dev->priv) {
331 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
337 /* Allocate private data if requested and not reentered */
338 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
339 if (size && !dev->uclass_priv) {
340 dev->uclass_priv = alloc_priv(size,
341 dev->uclass->uc_drv->flags);
342 if (!dev->uclass_priv) {
348 /* Ensure all parents are probed */
350 size = dev->parent->driver->per_child_auto_alloc_size;
352 size = dev->parent->uclass->uc_drv->
353 per_child_auto_alloc_size;
355 if (size && !dev->parent_priv) {
356 dev->parent_priv = alloc_priv(size, drv->flags);
357 if (!dev->parent_priv) {
363 ret = device_probe(dev->parent);
368 * The device might have already been probed during
369 * the call to device_probe() on its parent device
370 * (e.g. PCI bridge devices). Test the flags again
371 * so that we don't mess up the device.
373 if (dev->flags & DM_FLAG_ACTIVATED)
377 seq = uclass_resolve_seq(dev);
384 dev->flags |= DM_FLAG_ACTIVATED;
387 * Process pinctrl for everything except the root device, and
388 * continue regardless of the result of pinctrl. Don't process pinctrl
389 * settings for pinctrl devices since the device may not yet be
392 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
393 pinctrl_select_state(dev, "default");
395 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
396 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
397 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
398 ret = dev_power_domain_on(dev);
403 ret = uclass_pre_probe_device(dev);
407 if (dev->parent && dev->parent->driver->child_pre_probe) {
408 ret = dev->parent->driver->child_pre_probe(dev);
413 if (drv->ofdata_to_platdata &&
414 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
415 ret = drv->ofdata_to_platdata(dev);
420 /* Only handle devices that have a valid ofnode */
421 if (dev_of_valid(dev)) {
423 * Process 'assigned-{clocks/clock-parents/clock-rates}'
426 ret = clk_set_defaults(dev, 0);
432 ret = drv->probe(dev);
434 dev->flags &= ~DM_FLAG_ACTIVATED;
439 ret = uclass_post_probe_device(dev);
443 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
444 pinctrl_select_state(dev, "default");
448 if (device_remove(dev, DM_REMOVE_NORMAL)) {
449 dm_warn("%s: Device '%s' failed to remove on error path\n",
450 __func__, dev->name);
453 dev->flags &= ~DM_FLAG_ACTIVATED;
461 void *dev_get_platdata(const struct udevice *dev)
464 dm_warn("%s: null device\n", __func__);
468 return dev->platdata;
471 void *dev_get_parent_platdata(const struct udevice *dev)
474 dm_warn("%s: null device\n", __func__);
478 return dev->parent_platdata;
481 void *dev_get_uclass_platdata(const struct udevice *dev)
484 dm_warn("%s: null device\n", __func__);
488 return dev->uclass_platdata;
491 void *dev_get_priv(const struct udevice *dev)
494 dm_warn("%s: null device\n", __func__);
501 void *dev_get_uclass_priv(const struct udevice *dev)
504 dm_warn("%s: null device\n", __func__);
508 return dev->uclass_priv;
511 void *dev_get_parent_priv(const struct udevice *dev)
514 dm_warn("%s: null device\n", __func__);
518 return dev->parent_priv;
521 static int device_get_device_tail(struct udevice *dev, int ret,
522 struct udevice **devp)
527 ret = device_probe(dev);
536 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
538 * device_find_by_ofnode() - Return device associated with given ofnode
540 * The returned device is *not* activated.
542 * @node: The ofnode for which a associated device should be looked up
543 * @devp: Pointer to structure to hold the found device
544 * Return: 0 if OK, -ve on error
546 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
552 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
553 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
565 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
569 list_for_each_entry(dev, &parent->child_head, sibling_node) {
571 return device_get_device_tail(dev, 0, devp);
577 int device_get_child_count(struct udevice *parent)
582 list_for_each_entry(dev, &parent->child_head, sibling_node)
588 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
589 bool find_req_seq, struct udevice **devp)
594 if (seq_or_req_seq == -1)
597 list_for_each_entry(dev, &parent->child_head, sibling_node) {
598 if ((find_req_seq ? dev->req_seq : dev->seq) ==
608 int device_get_child_by_seq(struct udevice *parent, int seq,
609 struct udevice **devp)
615 ret = device_find_child_by_seq(parent, seq, false, &dev);
616 if (ret == -ENODEV) {
618 * We didn't find it in probed devices. See if there is one
619 * that will request this seq if probed.
621 ret = device_find_child_by_seq(parent, seq, true, &dev);
623 return device_get_device_tail(dev, ret, devp);
626 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
627 struct udevice **devp)
633 list_for_each_entry(dev, &parent->child_head, sibling_node) {
634 if (dev_of_offset(dev) == of_offset) {
643 int device_get_child_by_of_offset(struct udevice *parent, int node,
644 struct udevice **devp)
650 ret = device_find_child_by_of_offset(parent, node, &dev);
651 return device_get_device_tail(dev, ret, devp);
654 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
657 struct udevice *dev, *found;
659 if (ofnode_equal(dev_ofnode(parent), ofnode))
662 list_for_each_entry(dev, &parent->child_head, sibling_node) {
663 found = _device_find_global_by_ofnode(dev, ofnode);
671 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
673 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
675 return *devp ? 0 : -ENOENT;
678 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
682 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
683 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
686 int device_find_first_child(struct udevice *parent, struct udevice **devp)
688 if (list_empty(&parent->child_head)) {
691 *devp = list_first_entry(&parent->child_head, struct udevice,
698 int device_find_next_child(struct udevice **devp)
700 struct udevice *dev = *devp;
701 struct udevice *parent = dev->parent;
703 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
706 *devp = list_entry(dev->sibling_node.next, struct udevice,
713 int device_find_first_inactive_child(struct udevice *parent,
714 enum uclass_id uclass_id,
715 struct udevice **devp)
720 list_for_each_entry(dev, &parent->child_head, sibling_node) {
721 if (!device_active(dev) &&
722 device_get_uclass_id(dev) == uclass_id) {
731 int device_find_first_child_by_uclass(struct udevice *parent,
732 enum uclass_id uclass_id,
733 struct udevice **devp)
738 list_for_each_entry(dev, &parent->child_head, sibling_node) {
739 if (device_get_uclass_id(dev) == uclass_id) {
748 int device_find_child_by_name(struct udevice *parent, const char *name,
749 struct udevice **devp)
755 list_for_each_entry(dev, &parent->child_head, sibling_node) {
756 if (!strcmp(dev->name, name)) {
765 struct udevice *dev_get_parent(const struct udevice *child)
767 return child->parent;
770 ulong dev_get_driver_data(const struct udevice *dev)
772 return dev->driver_data;
775 const void *dev_get_driver_ops(const struct udevice *dev)
777 if (!dev || !dev->driver->ops)
780 return dev->driver->ops;
783 enum uclass_id device_get_uclass_id(const struct udevice *dev)
785 return dev->uclass->uc_drv->id;
788 const char *dev_get_uclass_name(const struct udevice *dev)
793 return dev->uclass->uc_drv->name;
796 bool device_has_children(const struct udevice *dev)
798 return !list_empty(&dev->child_head);
801 bool device_has_active_children(struct udevice *dev)
803 struct udevice *child;
805 for (device_find_first_child(dev, &child);
807 device_find_next_child(&child)) {
808 if (device_active(child))
815 bool device_is_last_sibling(struct udevice *dev)
817 struct udevice *parent = dev->parent;
821 return list_is_last(&dev->sibling_node, &parent->child_head);
824 void device_set_name_alloced(struct udevice *dev)
826 dev->flags |= DM_FLAG_NAME_ALLOCED;
829 int device_set_name(struct udevice *dev, const char *name)
835 device_set_name_alloced(dev);
840 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
841 bool device_is_compatible(struct udevice *dev, const char *compat)
843 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
846 bool of_machine_is_compatible(const char *compat)
848 const void *fdt = gd->fdt_blob;
850 return !fdt_node_check_compatible(fdt, 0, compat);
853 int dev_disable_by_path(const char *path)
856 ofnode node = ofnode_path(path);
860 if (!of_live_active())
863 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
864 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
872 ret = device_remove(dev, DM_REMOVE_NORMAL);
876 ret = device_unbind(dev);
880 return ofnode_set_enabled(node, false);
883 int dev_enable_by_path(const char *path)
885 ofnode node = ofnode_path(path);
886 ofnode pnode = ofnode_get_parent(node);
887 struct udevice *parent;
890 if (!of_live_active())
893 ret = device_find_by_ofnode(pnode, &parent);
897 ret = ofnode_set_enabled(node, true);
901 return lists_bind_fdt(parent, node, NULL, false);