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);
86 dev->req_seq = uclass_find_next_free_req_seq(drv->id);
90 if (drv->platdata_auto_alloc_size) {
91 bool alloc = !platdata;
93 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
94 if (of_platdata_size) {
95 dev->flags |= DM_FLAG_OF_PLATDATA;
96 if (of_platdata_size <
97 drv->platdata_auto_alloc_size)
102 dev->flags |= DM_FLAG_ALLOC_PDATA;
103 dev->platdata = calloc(1,
104 drv->platdata_auto_alloc_size);
105 if (!dev->platdata) {
109 if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
110 memcpy(dev->platdata, platdata,
116 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
118 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
119 dev->uclass_platdata = calloc(1, size);
120 if (!dev->uclass_platdata) {
127 size = parent->driver->per_child_platdata_auto_alloc_size;
129 size = parent->uclass->uc_drv->
130 per_child_platdata_auto_alloc_size;
133 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
134 dev->parent_platdata = calloc(1, size);
135 if (!dev->parent_platdata) {
142 /* put dev into parent's successor list */
144 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->flags |= 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->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
197 free(dev->parent_platdata);
198 dev->parent_platdata = NULL;
202 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
203 free(dev->uclass_platdata);
204 dev->uclass_platdata = NULL;
207 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
209 dev->platdata = 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 *platdata, int of_offset,
230 struct udevice **devp)
232 return device_bind_common(parent, drv, name, platdata, 0,
233 offset_to_ofnode(of_offset), 0, devp);
236 int device_bind_ofnode(struct udevice *parent, const struct driver *drv,
237 const char *name, void *platdata, ofnode node,
238 struct udevice **devp)
240 return device_bind_common(parent, drv, name, platdata, 0, node, 0,
244 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
245 const struct driver_info *info, struct udevice **devp)
248 uint platdata_size = 0;
250 drv = lists_driver_lookup_name(info->name);
253 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
256 #if CONFIG_IS_ENABLED(OF_PLATDATA)
257 platdata_size = info->platdata_size;
259 return device_bind_common(parent, drv, info->name,
260 (void *)info->platdata, 0, ofnode_null(), platdata_size,
264 static void *alloc_priv(int size, uint flags)
268 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
269 size = ROUND(size, ARCH_DMA_MINALIGN);
270 priv = memalign(ARCH_DMA_MINALIGN, size);
272 memset(priv, '\0', size);
275 * Ensure that the zero bytes are flushed to memory.
276 * This prevents problems if the driver uses this as
277 * both an input and an output buffer:
279 * 1. Zeroes written to buffer (here) and sit in the
281 * 2. Driver issues a read command to DMA
282 * 3. CPU runs out of cache space and evicts some cache
283 * data in the buffer, writing zeroes to RAM from
286 * 5. Buffer now has some DMA data and some zeroes
287 * 6. Data being read is now incorrect
289 * To prevent this, ensure that the cache is clean
290 * within this range at the start. The driver can then
291 * use normal flush-after-write, invalidate-before-read
294 * TODO(sjg@chromium.org): Drop this microblaze
297 #ifndef CONFIG_MICROBLAZE
298 flush_dcache_range((ulong)priv, (ulong)priv + size);
302 priv = calloc(1, size);
308 int device_probe(struct udevice *dev)
310 struct power_domain pd;
311 const struct driver *drv;
319 if (dev->flags & DM_FLAG_ACTIVATED)
325 /* Allocate private data if requested and not reentered */
326 if (drv->priv_auto_alloc_size && !dev->priv) {
327 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
333 /* Allocate private data if requested and not reentered */
334 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
335 if (size && !dev->uclass_priv) {
336 dev->uclass_priv = alloc_priv(size,
337 dev->uclass->uc_drv->flags);
338 if (!dev->uclass_priv) {
344 /* Ensure all parents are probed */
346 size = dev->parent->driver->per_child_auto_alloc_size;
348 size = dev->parent->uclass->uc_drv->
349 per_child_auto_alloc_size;
351 if (size && !dev->parent_priv) {
352 dev->parent_priv = alloc_priv(size, drv->flags);
353 if (!dev->parent_priv) {
359 ret = device_probe(dev->parent);
364 * The device might have already been probed during
365 * the call to device_probe() on its parent device
366 * (e.g. PCI bridge devices). Test the flags again
367 * so that we don't mess up the device.
369 if (dev->flags & DM_FLAG_ACTIVATED)
373 seq = uclass_resolve_seq(dev);
380 dev->flags |= DM_FLAG_ACTIVATED;
383 * Process pinctrl for everything except the root device, and
384 * continue regardless of the result of pinctrl. Don't process pinctrl
385 * settings for pinctrl devices since the device may not yet be
388 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
389 pinctrl_select_state(dev, "default");
391 if (dev->parent && device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) {
392 if (!power_domain_get(dev, &pd))
393 power_domain_on(&pd);
396 ret = uclass_pre_probe_device(dev);
400 if (dev->parent && dev->parent->driver->child_pre_probe) {
401 ret = dev->parent->driver->child_pre_probe(dev);
406 if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
407 ret = drv->ofdata_to_platdata(dev);
412 /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
413 ret = clk_set_defaults(dev);
418 ret = drv->probe(dev);
420 dev->flags &= ~DM_FLAG_ACTIVATED;
425 ret = uclass_post_probe_device(dev);
429 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
430 pinctrl_select_state(dev, "default");
434 if (device_remove(dev, DM_REMOVE_NORMAL)) {
435 dm_warn("%s: Device '%s' failed to remove on error path\n",
436 __func__, dev->name);
439 dev->flags &= ~DM_FLAG_ACTIVATED;
447 void *dev_get_platdata(const struct udevice *dev)
450 dm_warn("%s: null device\n", __func__);
454 return dev->platdata;
457 void *dev_get_parent_platdata(const struct udevice *dev)
460 dm_warn("%s: null device\n", __func__);
464 return dev->parent_platdata;
467 void *dev_get_uclass_platdata(const struct udevice *dev)
470 dm_warn("%s: null device\n", __func__);
474 return dev->uclass_platdata;
477 void *dev_get_priv(const struct udevice *dev)
480 dm_warn("%s: null device\n", __func__);
487 void *dev_get_uclass_priv(const struct udevice *dev)
490 dm_warn("%s: null device\n", __func__);
494 return dev->uclass_priv;
497 void *dev_get_parent_priv(const struct udevice *dev)
500 dm_warn("%s: null device\n", __func__);
504 return dev->parent_priv;
507 static int device_get_device_tail(struct udevice *dev, int ret,
508 struct udevice **devp)
513 ret = device_probe(dev);
523 * device_find_by_ofnode() - Return device associated with given ofnode
525 * The returned device is *not* activated.
527 * @node: The ofnode for which a associated device should be looked up
528 * @devp: Pointer to structure to hold the found device
529 * Return: 0 if OK, -ve on error
531 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
537 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
538 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
549 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
553 list_for_each_entry(dev, &parent->child_head, sibling_node) {
555 return device_get_device_tail(dev, 0, devp);
561 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
562 bool find_req_seq, struct udevice **devp)
567 if (seq_or_req_seq == -1)
570 list_for_each_entry(dev, &parent->child_head, sibling_node) {
571 if ((find_req_seq ? dev->req_seq : dev->seq) ==
581 int device_get_child_by_seq(struct udevice *parent, int seq,
582 struct udevice **devp)
588 ret = device_find_child_by_seq(parent, seq, false, &dev);
589 if (ret == -ENODEV) {
591 * We didn't find it in probed devices. See if there is one
592 * that will request this seq if probed.
594 ret = device_find_child_by_seq(parent, seq, true, &dev);
596 return device_get_device_tail(dev, ret, devp);
599 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
600 struct udevice **devp)
606 list_for_each_entry(dev, &parent->child_head, sibling_node) {
607 if (dev_of_offset(dev) == of_offset) {
616 int device_get_child_by_of_offset(struct udevice *parent, int node,
617 struct udevice **devp)
623 ret = device_find_child_by_of_offset(parent, node, &dev);
624 return device_get_device_tail(dev, ret, devp);
627 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
630 struct udevice *dev, *found;
632 if (ofnode_equal(dev_ofnode(parent), ofnode))
635 list_for_each_entry(dev, &parent->child_head, sibling_node) {
636 found = _device_find_global_by_ofnode(dev, ofnode);
644 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
646 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
648 return *devp ? 0 : -ENOENT;
651 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
655 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
656 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
659 int device_find_first_child(struct udevice *parent, struct udevice **devp)
661 if (list_empty(&parent->child_head)) {
664 *devp = list_first_entry(&parent->child_head, struct udevice,
671 int device_find_next_child(struct udevice **devp)
673 struct udevice *dev = *devp;
674 struct udevice *parent = dev->parent;
676 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
679 *devp = list_entry(dev->sibling_node.next, struct udevice,
686 int device_find_first_inactive_child(struct udevice *parent,
687 enum uclass_id uclass_id,
688 struct udevice **devp)
693 list_for_each_entry(dev, &parent->child_head, sibling_node) {
694 if (!device_active(dev) &&
695 device_get_uclass_id(dev) == uclass_id) {
704 int device_find_first_child_by_uclass(struct udevice *parent,
705 enum uclass_id uclass_id,
706 struct udevice **devp)
711 list_for_each_entry(dev, &parent->child_head, sibling_node) {
712 if (device_get_uclass_id(dev) == uclass_id) {
721 int device_find_child_by_name(struct udevice *parent, const char *name,
722 struct udevice **devp)
728 list_for_each_entry(dev, &parent->child_head, sibling_node) {
729 if (!strcmp(dev->name, name)) {
738 struct udevice *dev_get_parent(const struct udevice *child)
740 return child->parent;
743 ulong dev_get_driver_data(const struct udevice *dev)
745 return dev->driver_data;
748 const void *dev_get_driver_ops(const struct udevice *dev)
750 if (!dev || !dev->driver->ops)
753 return dev->driver->ops;
756 enum uclass_id device_get_uclass_id(const struct udevice *dev)
758 return dev->uclass->uc_drv->id;
761 const char *dev_get_uclass_name(const struct udevice *dev)
766 return dev->uclass->uc_drv->name;
769 bool device_has_children(const struct udevice *dev)
771 return !list_empty(&dev->child_head);
774 bool device_has_active_children(struct udevice *dev)
776 struct udevice *child;
778 for (device_find_first_child(dev, &child);
780 device_find_next_child(&child)) {
781 if (device_active(child))
788 bool device_is_last_sibling(struct udevice *dev)
790 struct udevice *parent = dev->parent;
794 return list_is_last(&dev->sibling_node, &parent->child_head);
797 void device_set_name_alloced(struct udevice *dev)
799 dev->flags |= DM_FLAG_NAME_ALLOCED;
802 int device_set_name(struct udevice *dev, const char *name)
808 device_set_name_alloced(dev);
813 bool device_is_compatible(struct udevice *dev, const char *compat)
815 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
818 bool of_machine_is_compatible(const char *compat)
820 const void *fdt = gd->fdt_blob;
822 return !fdt_node_check_compatible(fdt, 0, compat);
825 int dev_disable_by_path(const char *path)
828 ofnode node = ofnode_path(path);
832 if (!of_live_active())
835 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
836 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
844 ret = device_remove(dev, DM_REMOVE_NORMAL);
848 ret = device_unbind(dev);
852 return ofnode_set_enabled(node, false);
855 int dev_enable_by_path(const char *path)
857 ofnode node = ofnode_path(path);
858 ofnode pnode = ofnode_get_parent(node);
859 struct udevice *parent;
862 if (!of_live_active())
865 ret = device_find_by_ofnode(pnode, &parent);
869 ret = ofnode_set_enabled(node, true);
873 return lists_bind_fdt(parent, node, NULL, false);