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) {
146 /* put dev into parent's successor list */
147 list_add_tail(&dev->sibling_node, &parent->child_head);
150 ret = uclass_bind_device(dev);
152 goto fail_uclass_bind;
154 /* if we fail to bind we remove device from successors and free it */
156 ret = drv->bind(dev);
160 if (parent && parent->driver->child_post_bind) {
161 ret = parent->driver->child_post_bind(dev);
163 goto fail_child_post_bind;
165 if (uc->uc_drv->post_bind) {
166 ret = uc->uc_drv->post_bind(dev);
168 goto fail_uclass_post_bind;
172 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
176 dev->flags |= DM_FLAG_BOUND;
180 fail_uclass_post_bind:
181 /* There is no child unbind() method, so no clean-up required */
182 fail_child_post_bind:
183 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
184 if (drv->unbind && drv->unbind(dev)) {
185 dm_warn("unbind() method failed on dev '%s' on error path\n",
191 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
192 if (uclass_unbind_device(dev)) {
193 dm_warn("Failed to unbind dev '%s' on error path\n",
198 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
199 list_del(&dev->sibling_node);
200 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
201 free(dev->parent_platdata);
202 dev->parent_platdata = NULL;
206 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
207 free(dev->uclass_platdata);
208 dev->uclass_platdata = NULL;
211 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
213 dev->platdata = NULL;
216 devres_release_all(dev);
223 int device_bind_with_driver_data(struct udevice *parent,
224 const struct driver *drv, const char *name,
225 ulong driver_data, ofnode node,
226 struct udevice **devp)
228 return device_bind_common(parent, drv, name, NULL, driver_data, node,
232 int device_bind(struct udevice *parent, const struct driver *drv,
233 const char *name, void *platdata, int of_offset,
234 struct udevice **devp)
236 return device_bind_common(parent, drv, name, platdata, 0,
237 offset_to_ofnode(of_offset), 0, devp);
240 int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
241 const char *name, void *platdata, ofnode node,
242 struct udevice **devp)
244 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
248 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
249 const struct driver_info *info, struct udevice **devp)
252 uint platdata_size = 0;
254 drv = lists_driver_lookup_name(info->name);
257 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
260 #if CONFIG_IS_ENABLED(OF_PLATDATA)
261 platdata_size = info->platdata_size;
263 return device_bind_common(parent, drv, info->name,
264 (void *)info->platdata, 0, ofnode_null(), platdata_size,
268 static void *alloc_priv(int size, uint flags)
272 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
273 size = ROUND(size, ARCH_DMA_MINALIGN);
274 priv = memalign(ARCH_DMA_MINALIGN, size);
276 memset(priv, '\0', size);
279 * Ensure that the zero bytes are flushed to memory.
280 * This prevents problems if the driver uses this as
281 * both an input and an output buffer:
283 * 1. Zeroes written to buffer (here) and sit in the
285 * 2. Driver issues a read command to DMA
286 * 3. CPU runs out of cache space and evicts some cache
287 * data in the buffer, writing zeroes to RAM from
290 * 5. Buffer now has some DMA data and some zeroes
291 * 6. Data being read is now incorrect
293 * To prevent this, ensure that the cache is clean
294 * within this range at the start. The driver can then
295 * use normal flush-after-write, invalidate-before-read
298 * TODO(sjg@chromium.org): Drop this microblaze
301 #ifndef CONFIG_MICROBLAZE
302 flush_dcache_range((ulong)priv, (ulong)priv + size);
306 priv = calloc(1, size);
312 int device_ofdata_to_platdata(struct udevice *dev)
314 const struct driver *drv;
321 if (dev->flags & DM_FLAG_PLATDATA_VALID)
327 /* Allocate private data if requested and not reentered */
328 if (drv->priv_auto_alloc_size && !dev->priv) {
329 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
335 /* Allocate private data if requested and not reentered */
336 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
337 if (size && !dev->uclass_priv) {
338 dev->uclass_priv = alloc_priv(size,
339 dev->uclass->uc_drv->flags);
340 if (!dev->uclass_priv) {
346 /* Allocate parent data for this child */
348 size = dev->parent->driver->per_child_auto_alloc_size;
350 size = dev->parent->uclass->uc_drv->
351 per_child_auto_alloc_size;
353 if (size && !dev->parent_priv) {
354 dev->parent_priv = alloc_priv(size, drv->flags);
355 if (!dev->parent_priv) {
362 if (drv->ofdata_to_platdata &&
363 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
364 ret = drv->ofdata_to_platdata(dev);
369 dev->flags |= DM_FLAG_PLATDATA_VALID;
378 int device_probe(struct udevice *dev)
380 const struct driver *drv;
387 if (dev->flags & DM_FLAG_ACTIVATED)
393 ret = device_ofdata_to_platdata(dev);
397 /* Ensure all parents are probed */
399 ret = device_probe(dev->parent);
404 * The device might have already been probed during
405 * the call to device_probe() on its parent device
406 * (e.g. PCI bridge devices). Test the flags again
407 * so that we don't mess up the device.
409 if (dev->flags & DM_FLAG_ACTIVATED)
413 seq = uclass_resolve_seq(dev);
420 dev->flags |= DM_FLAG_ACTIVATED;
423 * Process pinctrl for everything except the root device, and
424 * continue regardless of the result of pinctrl. Don't process pinctrl
425 * settings for pinctrl devices since the device may not yet be
428 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
429 pinctrl_select_state(dev, "default");
431 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
432 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
433 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
434 ret = dev_power_domain_on(dev);
439 ret = uclass_pre_probe_device(dev);
443 if (dev->parent && dev->parent->driver->child_pre_probe) {
444 ret = dev->parent->driver->child_pre_probe(dev);
449 /* Only handle devices that have a valid ofnode */
450 if (dev_of_valid(dev)) {
452 * Process 'assigned-{clocks/clock-parents/clock-rates}'
455 ret = clk_set_defaults(dev, 0);
461 ret = drv->probe(dev);
466 ret = uclass_post_probe_device(dev);
470 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
471 pinctrl_select_state(dev, "default");
475 if (device_remove(dev, DM_REMOVE_NORMAL)) {
476 dm_warn("%s: Device '%s' failed to remove on error path\n",
477 __func__, dev->name);
480 dev->flags &= ~DM_FLAG_ACTIVATED;
488 void *dev_get_platdata(const struct udevice *dev)
491 dm_warn("%s: null device\n", __func__);
495 return dev->platdata;
498 void *dev_get_parent_platdata(const struct udevice *dev)
501 dm_warn("%s: null device\n", __func__);
505 return dev->parent_platdata;
508 void *dev_get_uclass_platdata(const struct udevice *dev)
511 dm_warn("%s: null device\n", __func__);
515 return dev->uclass_platdata;
518 void *dev_get_priv(const struct udevice *dev)
521 dm_warn("%s: null device\n", __func__);
528 void *dev_get_uclass_priv(const struct udevice *dev)
531 dm_warn("%s: null device\n", __func__);
535 return dev->uclass_priv;
538 void *dev_get_parent_priv(const struct udevice *dev)
541 dm_warn("%s: null device\n", __func__);
545 return dev->parent_priv;
548 static int device_get_device_tail(struct udevice *dev, int ret,
549 struct udevice **devp)
554 ret = device_probe(dev);
563 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
565 * device_find_by_ofnode() - Return device associated with given ofnode
567 * The returned device is *not* activated.
569 * @node: The ofnode for which a associated device should be looked up
570 * @devp: Pointer to structure to hold the found device
571 * Return: 0 if OK, -ve on error
573 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
579 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
580 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
592 int device_get_child(const struct udevice *parent, int index,
593 struct udevice **devp)
597 list_for_each_entry(dev, &parent->child_head, sibling_node) {
599 return device_get_device_tail(dev, 0, devp);
605 int device_get_child_count(const struct udevice *parent)
610 list_for_each_entry(dev, &parent->child_head, sibling_node)
616 int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
617 bool find_req_seq, struct udevice **devp)
622 if (seq_or_req_seq == -1)
625 list_for_each_entry(dev, &parent->child_head, sibling_node) {
626 if ((find_req_seq ? dev->req_seq : dev->seq) ==
636 int device_get_child_by_seq(const struct udevice *parent, int seq,
637 struct udevice **devp)
643 ret = device_find_child_by_seq(parent, seq, false, &dev);
644 if (ret == -ENODEV) {
646 * We didn't find it in probed devices. See if there is one
647 * that will request this seq if probed.
649 ret = device_find_child_by_seq(parent, seq, true, &dev);
651 return device_get_device_tail(dev, ret, devp);
654 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
655 struct udevice **devp)
661 list_for_each_entry(dev, &parent->child_head, sibling_node) {
662 if (dev_of_offset(dev) == of_offset) {
671 int device_get_child_by_of_offset(const struct udevice *parent, int node,
672 struct udevice **devp)
678 ret = device_find_child_by_of_offset(parent, node, &dev);
679 return device_get_device_tail(dev, ret, devp);
682 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
685 struct udevice *dev, *found;
687 if (ofnode_equal(dev_ofnode(parent), ofnode))
690 list_for_each_entry(dev, &parent->child_head, sibling_node) {
691 found = _device_find_global_by_ofnode(dev, ofnode);
699 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
701 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
703 return *devp ? 0 : -ENOENT;
706 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
710 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
711 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
714 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
716 if (list_empty(&parent->child_head)) {
719 *devp = list_first_entry(&parent->child_head, struct udevice,
726 int device_find_next_child(struct udevice **devp)
728 struct udevice *dev = *devp;
729 struct udevice *parent = dev->parent;
731 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
734 *devp = list_entry(dev->sibling_node.next, struct udevice,
741 int device_find_first_inactive_child(const struct udevice *parent,
742 enum uclass_id uclass_id,
743 struct udevice **devp)
748 list_for_each_entry(dev, &parent->child_head, sibling_node) {
749 if (!device_active(dev) &&
750 device_get_uclass_id(dev) == uclass_id) {
759 int device_find_first_child_by_uclass(const struct udevice *parent,
760 enum uclass_id uclass_id,
761 struct udevice **devp)
766 list_for_each_entry(dev, &parent->child_head, sibling_node) {
767 if (device_get_uclass_id(dev) == uclass_id) {
776 int device_find_child_by_name(const struct udevice *parent, const char *name,
777 struct udevice **devp)
783 list_for_each_entry(dev, &parent->child_head, sibling_node) {
784 if (!strcmp(dev->name, name)) {
793 int device_first_child_err(struct udevice *parent, struct udevice **devp)
797 device_find_first_child(parent, &dev);
801 return device_get_device_tail(dev, 0, devp);
804 int device_next_child_err(struct udevice **devp)
806 struct udevice *dev = *devp;
808 device_find_next_child(&dev);
812 return device_get_device_tail(dev, 0, devp);
815 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
820 device_find_first_child(parent, &dev);
824 ret = device_ofdata_to_platdata(dev);
833 int device_next_child_ofdata_err(struct udevice **devp)
835 struct udevice *dev = *devp;
838 device_find_next_child(&dev);
842 ret = device_ofdata_to_platdata(dev);
851 struct udevice *dev_get_parent(const struct udevice *child)
853 return child->parent;
856 ulong dev_get_driver_data(const struct udevice *dev)
858 return dev->driver_data;
861 const void *dev_get_driver_ops(const struct udevice *dev)
863 if (!dev || !dev->driver->ops)
866 return dev->driver->ops;
869 enum uclass_id device_get_uclass_id(const struct udevice *dev)
871 return dev->uclass->uc_drv->id;
874 const char *dev_get_uclass_name(const struct udevice *dev)
879 return dev->uclass->uc_drv->name;
882 bool device_has_children(const struct udevice *dev)
884 return !list_empty(&dev->child_head);
887 bool device_has_active_children(const struct udevice *dev)
889 struct udevice *child;
891 for (device_find_first_child(dev, &child);
893 device_find_next_child(&child)) {
894 if (device_active(child))
901 bool device_is_last_sibling(const struct udevice *dev)
903 struct udevice *parent = dev->parent;
907 return list_is_last(&dev->sibling_node, &parent->child_head);
910 void device_set_name_alloced(struct udevice *dev)
912 dev->flags |= DM_FLAG_NAME_ALLOCED;
915 int device_set_name(struct udevice *dev, const char *name)
921 device_set_name_alloced(dev);
926 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
927 bool device_is_compatible(const struct udevice *dev, const char *compat)
929 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
932 bool of_machine_is_compatible(const char *compat)
934 const void *fdt = gd->fdt_blob;
936 return !fdt_node_check_compatible(fdt, 0, compat);
939 int dev_disable_by_path(const char *path)
942 ofnode node = ofnode_path(path);
946 if (!of_live_active())
949 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
950 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
958 ret = device_remove(dev, DM_REMOVE_NORMAL);
962 ret = device_unbind(dev);
966 return ofnode_set_enabled(node, false);
969 int dev_enable_by_path(const char *path)
971 ofnode node = ofnode_path(path);
972 ofnode pnode = ofnode_get_parent(node);
973 struct udevice *parent;
976 if (!of_live_active())
979 ret = device_find_by_ofnode(pnode, &parent);
983 ret = ofnode_set_enabled(node, true);
987 return lists_bind_fdt(parent, node, NULL, false);