1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2013 Google, Inc
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
16 #include <fdt_support.h>
18 #include <dm/device.h>
19 #include <dm/device-internal.h>
21 #include <dm/of_access.h>
22 #include <dm/pinctrl.h>
23 #include <dm/platdata.h>
25 #include <dm/uclass.h>
26 #include <dm/uclass-internal.h>
28 #include <linux/err.h>
29 #include <linux/list.h>
30 #include <power-domain.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 static int device_bind_common(struct udevice *parent, const struct driver *drv,
35 const char *name, void *platdata,
36 ulong driver_data, ofnode node,
37 uint of_platdata_size, struct udevice **devp)
48 ret = uclass_get(drv->id, &uc);
50 debug("Missing uclass for driver %s\n", drv->name);
54 dev = calloc(1, sizeof(struct udevice));
58 INIT_LIST_HEAD(&dev->sibling_node);
59 INIT_LIST_HEAD(&dev->child_head);
60 INIT_LIST_HEAD(&dev->uclass_node);
62 INIT_LIST_HEAD(&dev->devres_head);
64 dev->platdata = platdata;
65 dev->driver_data = driver_data;
74 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
75 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
77 * Some devices, such as a SPI bus, I2C bus and serial ports
78 * are numbered using aliases.
80 * This is just a 'requested' sequence, and will be
81 * resolved (and ->seq updated) when the device is probed.
83 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
84 if (uc->uc_drv->name && ofnode_valid(node))
85 dev_read_alias_seq(dev, &dev->req_seq);
86 #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
87 if (dev->req_seq == -1)
89 uclass_find_next_free_req_seq(drv->id);
92 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
96 if (drv->platdata_auto_alloc_size) {
97 bool alloc = !platdata;
99 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
100 if (of_platdata_size) {
101 dev->flags |= DM_FLAG_OF_PLATDATA;
102 if (of_platdata_size <
103 drv->platdata_auto_alloc_size)
108 dev->flags |= DM_FLAG_ALLOC_PDATA;
109 dev->platdata = calloc(1,
110 drv->platdata_auto_alloc_size);
111 if (!dev->platdata) {
115 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
116 memcpy(dev->platdata, platdata,
122 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
124 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
125 dev->uclass_platdata = calloc(1, size);
126 if (!dev->uclass_platdata) {
133 size = parent->driver->per_child_platdata_auto_alloc_size;
135 size = parent->uclass->uc_drv->
136 per_child_platdata_auto_alloc_size;
139 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
140 dev->parent_platdata = calloc(1, size);
141 if (!dev->parent_platdata) {
148 /* put dev into parent's successor list */
150 list_add_tail(&dev->sibling_node, &parent->child_head);
152 ret = uclass_bind_device(dev);
154 goto fail_uclass_bind;
156 /* if we fail to bind we remove device from successors and free it */
158 ret = drv->bind(dev);
162 if (parent && parent->driver->child_post_bind) {
163 ret = parent->driver->child_post_bind(dev);
165 goto fail_child_post_bind;
167 if (uc->uc_drv->post_bind) {
168 ret = uc->uc_drv->post_bind(dev);
170 goto fail_uclass_post_bind;
174 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
178 dev->flags |= DM_FLAG_BOUND;
182 fail_uclass_post_bind:
183 /* There is no child unbind() method, so no clean-up required */
184 fail_child_post_bind:
185 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
186 if (drv->unbind && drv->unbind(dev)) {
187 dm_warn("unbind() method failed on dev '%s' on error path\n",
193 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
194 if (uclass_unbind_device(dev)) {
195 dm_warn("Failed to unbind dev '%s' on error path\n",
200 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
201 list_del(&dev->sibling_node);
202 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
203 free(dev->parent_platdata);
204 dev->parent_platdata = NULL;
208 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
209 free(dev->uclass_platdata);
210 dev->uclass_platdata = NULL;
213 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
215 dev->platdata = NULL;
218 devres_release_all(dev);
225 int device_bind_with_driver_data(struct udevice *parent,
226 const struct driver *drv, const char *name,
227 ulong driver_data, ofnode node,
228 struct udevice **devp)
230 return device_bind_common(parent, drv, name, NULL, driver_data, node,
234 int device_bind(struct udevice *parent, const struct driver *drv,
235 const char *name, void *platdata, int of_offset,
236 struct udevice **devp)
238 return device_bind_common(parent, drv, name, platdata, 0,
239 offset_to_ofnode(of_offset), 0, devp);
242 int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
243 const char *name, void *platdata, ofnode node,
244 struct udevice **devp)
246 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
250 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
251 const struct driver_info *info, struct udevice **devp)
254 uint platdata_size = 0;
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 platdata_size = info->platdata_size;
265 return device_bind_common(parent, drv, info->name,
266 (void *)info->platdata, 0, ofnode_null(), platdata_size,
270 static void *alloc_priv(int size, uint flags)
274 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
275 size = ROUND(size, ARCH_DMA_MINALIGN);
276 priv = memalign(ARCH_DMA_MINALIGN, size);
278 memset(priv, '\0', size);
281 * Ensure that the zero bytes are flushed to memory.
282 * This prevents problems if the driver uses this as
283 * both an input and an output buffer:
285 * 1. Zeroes written to buffer (here) and sit in the
287 * 2. Driver issues a read command to DMA
288 * 3. CPU runs out of cache space and evicts some cache
289 * data in the buffer, writing zeroes to RAM from
292 * 5. Buffer now has some DMA data and some zeroes
293 * 6. Data being read is now incorrect
295 * To prevent this, ensure that the cache is clean
296 * within this range at the start. The driver can then
297 * use normal flush-after-write, invalidate-before-read
300 * TODO(sjg@chromium.org): Drop this microblaze
303 #ifndef CONFIG_MICROBLAZE
304 flush_dcache_range((ulong)priv, (ulong)priv + size);
308 priv = calloc(1, size);
314 int device_ofdata_to_platdata(struct udevice *dev)
316 const struct driver *drv;
323 if (dev->flags & DM_FLAG_PLATDATA_VALID)
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 /* Allocate parent data for this child */
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) {
364 if (drv->ofdata_to_platdata &&
365 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
366 ret = drv->ofdata_to_platdata(dev);
371 dev->flags |= DM_FLAG_PLATDATA_VALID;
380 int device_probe(struct udevice *dev)
382 const struct driver *drv;
389 if (dev->flags & DM_FLAG_ACTIVATED)
395 ret = device_ofdata_to_platdata(dev);
399 /* Ensure all parents are probed */
401 ret = device_probe(dev->parent);
406 * The device might have already been probed during
407 * the call to device_probe() on its parent device
408 * (e.g. PCI bridge devices). Test the flags again
409 * so that we don't mess up the device.
411 if (dev->flags & DM_FLAG_ACTIVATED)
415 seq = uclass_resolve_seq(dev);
422 dev->flags |= DM_FLAG_ACTIVATED;
425 * Process pinctrl for everything except the root device, and
426 * continue regardless of the result of pinctrl. Don't process pinctrl
427 * settings for pinctrl devices since the device may not yet be
430 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
431 pinctrl_select_state(dev, "default");
433 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
434 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
435 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
436 ret = dev_power_domain_on(dev);
441 ret = uclass_pre_probe_device(dev);
445 if (dev->parent && dev->parent->driver->child_pre_probe) {
446 ret = dev->parent->driver->child_pre_probe(dev);
451 /* Only handle devices that have a valid ofnode */
452 if (dev_of_valid(dev)) {
454 * Process 'assigned-{clocks/clock-parents/clock-rates}'
457 ret = clk_set_defaults(dev, 0);
463 ret = drv->probe(dev);
468 ret = uclass_post_probe_device(dev);
472 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
473 pinctrl_select_state(dev, "default");
477 if (device_remove(dev, DM_REMOVE_NORMAL)) {
478 dm_warn("%s: Device '%s' failed to remove on error path\n",
479 __func__, dev->name);
482 dev->flags &= ~DM_FLAG_ACTIVATED;
490 void *dev_get_platdata(const struct udevice *dev)
493 dm_warn("%s: null device\n", __func__);
497 return dev->platdata;
500 void *dev_get_parent_platdata(const struct udevice *dev)
503 dm_warn("%s: null device\n", __func__);
507 return dev->parent_platdata;
510 void *dev_get_uclass_platdata(const struct udevice *dev)
513 dm_warn("%s: null device\n", __func__);
517 return dev->uclass_platdata;
520 void *dev_get_priv(const struct udevice *dev)
523 dm_warn("%s: null device\n", __func__);
530 void *dev_get_uclass_priv(const struct udevice *dev)
533 dm_warn("%s: null device\n", __func__);
537 return dev->uclass_priv;
540 void *dev_get_parent_priv(const struct udevice *dev)
543 dm_warn("%s: null device\n", __func__);
547 return dev->parent_priv;
550 static int device_get_device_tail(struct udevice *dev, int ret,
551 struct udevice **devp)
556 ret = device_probe(dev);
565 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
567 * device_find_by_ofnode() - Return device associated with given ofnode
569 * The returned device is *not* activated.
571 * @node: The ofnode for which a associated device should be looked up
572 * @devp: Pointer to structure to hold the found device
573 * Return: 0 if OK, -ve on error
575 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
581 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
582 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
594 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
598 list_for_each_entry(dev, &parent->child_head, sibling_node) {
600 return device_get_device_tail(dev, 0, devp);
606 int device_get_child_count(struct udevice *parent)
611 list_for_each_entry(dev, &parent->child_head, sibling_node)
617 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
618 bool find_req_seq, struct udevice **devp)
623 if (seq_or_req_seq == -1)
626 list_for_each_entry(dev, &parent->child_head, sibling_node) {
627 if ((find_req_seq ? dev->req_seq : dev->seq) ==
637 int device_get_child_by_seq(struct udevice *parent, int seq,
638 struct udevice **devp)
644 ret = device_find_child_by_seq(parent, seq, false, &dev);
645 if (ret == -ENODEV) {
647 * We didn't find it in probed devices. See if there is one
648 * that will request this seq if probed.
650 ret = device_find_child_by_seq(parent, seq, true, &dev);
652 return device_get_device_tail(dev, ret, devp);
655 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
656 struct udevice **devp)
662 list_for_each_entry(dev, &parent->child_head, sibling_node) {
663 if (dev_of_offset(dev) == of_offset) {
672 int device_get_child_by_of_offset(struct udevice *parent, int node,
673 struct udevice **devp)
679 ret = device_find_child_by_of_offset(parent, node, &dev);
680 return device_get_device_tail(dev, ret, devp);
683 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
686 struct udevice *dev, *found;
688 if (ofnode_equal(dev_ofnode(parent), ofnode))
691 list_for_each_entry(dev, &parent->child_head, sibling_node) {
692 found = _device_find_global_by_ofnode(dev, ofnode);
700 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
702 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
704 return *devp ? 0 : -ENOENT;
707 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
711 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
712 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
715 int device_find_first_child(struct udevice *parent, struct udevice **devp)
717 if (list_empty(&parent->child_head)) {
720 *devp = list_first_entry(&parent->child_head, struct udevice,
727 int device_find_next_child(struct udevice **devp)
729 struct udevice *dev = *devp;
730 struct udevice *parent = dev->parent;
732 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
735 *devp = list_entry(dev->sibling_node.next, struct udevice,
742 int device_find_first_inactive_child(struct udevice *parent,
743 enum uclass_id uclass_id,
744 struct udevice **devp)
749 list_for_each_entry(dev, &parent->child_head, sibling_node) {
750 if (!device_active(dev) &&
751 device_get_uclass_id(dev) == uclass_id) {
760 int device_find_first_child_by_uclass(struct udevice *parent,
761 enum uclass_id uclass_id,
762 struct udevice **devp)
767 list_for_each_entry(dev, &parent->child_head, sibling_node) {
768 if (device_get_uclass_id(dev) == uclass_id) {
777 int device_find_child_by_name(struct udevice *parent, const char *name,
778 struct udevice **devp)
784 list_for_each_entry(dev, &parent->child_head, sibling_node) {
785 if (!strcmp(dev->name, name)) {
794 struct udevice *dev_get_parent(const struct udevice *child)
796 return child->parent;
799 ulong dev_get_driver_data(const struct udevice *dev)
801 return dev->driver_data;
804 const void *dev_get_driver_ops(const struct udevice *dev)
806 if (!dev || !dev->driver->ops)
809 return dev->driver->ops;
812 enum uclass_id device_get_uclass_id(const struct udevice *dev)
814 return dev->uclass->uc_drv->id;
817 const char *dev_get_uclass_name(const struct udevice *dev)
822 return dev->uclass->uc_drv->name;
825 bool device_has_children(const struct udevice *dev)
827 return !list_empty(&dev->child_head);
830 bool device_has_active_children(struct udevice *dev)
832 struct udevice *child;
834 for (device_find_first_child(dev, &child);
836 device_find_next_child(&child)) {
837 if (device_active(child))
844 bool device_is_last_sibling(struct udevice *dev)
846 struct udevice *parent = dev->parent;
850 return list_is_last(&dev->sibling_node, &parent->child_head);
853 void device_set_name_alloced(struct udevice *dev)
855 dev->flags |= DM_FLAG_NAME_ALLOCED;
858 int device_set_name(struct udevice *dev, const char *name)
864 device_set_name_alloced(dev);
869 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
870 bool device_is_compatible(struct udevice *dev, const char *compat)
872 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
875 bool of_machine_is_compatible(const char *compat)
877 const void *fdt = gd->fdt_blob;
879 return !fdt_node_check_compatible(fdt, 0, compat);
882 int dev_disable_by_path(const char *path)
885 ofnode node = ofnode_path(path);
889 if (!of_live_active())
892 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
893 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
901 ret = device_remove(dev, DM_REMOVE_NORMAL);
905 ret = device_unbind(dev);
909 return ofnode_set_enabled(node, false);
912 int dev_enable_by_path(const char *path)
914 ofnode node = ofnode_path(path);
915 ofnode pnode = ofnode_get_parent(node);
916 struct udevice *parent;
919 if (!of_live_active())
922 ret = device_find_by_ofnode(pnode, &parent);
926 ret = ofnode_set_enabled(node, true);
930 return lists_bind_fdt(parent, node, NULL, false);