4 * Copyright (c) 2013 Google, Inc
7 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 * SPDX-License-Identifier: GPL-2.0+
15 #include <fdt_support.h>
17 #include <dm/device.h>
18 #include <dm/device-internal.h>
20 #include <dm/pinctrl.h>
21 #include <dm/platdata.h>
22 #include <dm/uclass.h>
23 #include <dm/uclass-internal.h>
25 #include <linux/err.h>
26 #include <linux/list.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 static int device_bind_common(struct udevice *parent, const struct driver *drv,
31 const char *name, void *platdata,
32 ulong driver_data, int of_offset,
33 struct udevice **devp)
44 ret = uclass_get(drv->id, &uc);
46 debug("Missing uclass for driver %s\n", drv->name);
50 dev = calloc(1, sizeof(struct udevice));
54 INIT_LIST_HEAD(&dev->sibling_node);
55 INIT_LIST_HEAD(&dev->child_head);
56 INIT_LIST_HEAD(&dev->uclass_node);
58 INIT_LIST_HEAD(&dev->devres_head);
60 dev->platdata = platdata;
61 dev->driver_data = driver_data;
63 dev->of_offset = of_offset;
70 if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
72 * Some devices, such as a SPI bus, I2C bus and serial ports
73 * are numbered using aliases.
75 * This is just a 'requested' sequence, and will be
76 * resolved (and ->seq updated) when the device is probed.
78 if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
79 if (uc->uc_drv->name && of_offset != -1) {
80 fdtdec_get_alias_seq(gd->fdt_blob,
81 uc->uc_drv->name, of_offset,
87 if (!dev->platdata && drv->platdata_auto_alloc_size) {
88 dev->flags |= DM_FLAG_ALLOC_PDATA;
89 dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
96 size = uc->uc_drv->per_device_platdata_auto_alloc_size;
98 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
99 dev->uclass_platdata = calloc(1, size);
100 if (!dev->uclass_platdata) {
107 size = parent->driver->per_child_platdata_auto_alloc_size;
109 size = parent->uclass->uc_drv->
110 per_child_platdata_auto_alloc_size;
113 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
114 dev->parent_platdata = calloc(1, size);
115 if (!dev->parent_platdata) {
122 /* put dev into parent's successor list */
124 list_add_tail(&dev->sibling_node, &parent->child_head);
126 ret = uclass_bind_device(dev);
128 goto fail_uclass_bind;
130 /* if we fail to bind we remove device from successors and free it */
132 ret = drv->bind(dev);
136 if (parent && parent->driver->child_post_bind) {
137 ret = parent->driver->child_post_bind(dev);
139 goto fail_child_post_bind;
141 if (uc->uc_drv->post_bind) {
142 ret = uc->uc_drv->post_bind(dev);
144 goto fail_uclass_post_bind;
148 dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
152 dev->flags |= DM_FLAG_BOUND;
156 fail_uclass_post_bind:
157 /* There is no child unbind() method, so no clean-up required */
158 fail_child_post_bind:
159 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
160 if (drv->unbind && drv->unbind(dev)) {
161 dm_warn("unbind() method failed on dev '%s' on error path\n",
167 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
168 if (uclass_unbind_device(dev)) {
169 dm_warn("Failed to unbind dev '%s' on error path\n",
174 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
175 list_del(&dev->sibling_node);
176 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
177 free(dev->parent_platdata);
178 dev->parent_platdata = NULL;
182 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
183 free(dev->uclass_platdata);
184 dev->uclass_platdata = NULL;
187 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
189 dev->platdata = NULL;
192 devres_release_all(dev);
199 int device_bind_with_driver_data(struct udevice *parent,
200 const struct driver *drv, const char *name,
201 ulong driver_data, int of_offset,
202 struct udevice **devp)
204 return device_bind_common(parent, drv, name, NULL, driver_data,
208 int device_bind(struct udevice *parent, const struct driver *drv,
209 const char *name, void *platdata, int of_offset,
210 struct udevice **devp)
212 return device_bind_common(parent, drv, name, platdata, 0, of_offset,
216 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
217 const struct driver_info *info, struct udevice **devp)
221 drv = lists_driver_lookup_name(info->name);
224 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
227 return device_bind(parent, drv, info->name, (void *)info->platdata,
231 static void *alloc_priv(int size, uint flags)
235 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
236 priv = memalign(ARCH_DMA_MINALIGN, size);
238 memset(priv, '\0', size);
240 priv = calloc(1, size);
246 int device_probe(struct udevice *dev)
248 const struct driver *drv;
256 if (dev->flags & DM_FLAG_ACTIVATED)
262 /* Allocate private data if requested and not reentered */
263 if (drv->priv_auto_alloc_size && !dev->priv) {
264 dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
270 /* Allocate private data if requested and not reentered */
271 size = dev->uclass->uc_drv->per_device_auto_alloc_size;
272 if (size && !dev->uclass_priv) {
273 dev->uclass_priv = calloc(1, size);
274 if (!dev->uclass_priv) {
280 /* Ensure all parents are probed */
282 size = dev->parent->driver->per_child_auto_alloc_size;
284 size = dev->parent->uclass->uc_drv->
285 per_child_auto_alloc_size;
287 if (size && !dev->parent_priv) {
288 dev->parent_priv = alloc_priv(size, drv->flags);
289 if (!dev->parent_priv) {
295 ret = device_probe(dev->parent);
300 * The device might have already been probed during
301 * the call to device_probe() on its parent device
302 * (e.g. PCI bridge devices). Test the flags again
303 * so that we don't mess up the device.
305 if (dev->flags & DM_FLAG_ACTIVATED)
309 seq = uclass_resolve_seq(dev);
316 dev->flags |= DM_FLAG_ACTIVATED;
319 * Process pinctrl for everything except the root device, and
320 * continue regardless of the result of pinctrl. Don't process pinctrl
321 * settings for pinctrl devices since the device may not yet be
324 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
325 pinctrl_select_state(dev, "default");
327 ret = uclass_pre_probe_device(dev);
331 if (dev->parent && dev->parent->driver->child_pre_probe) {
332 ret = dev->parent->driver->child_pre_probe(dev);
337 if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
338 ret = drv->ofdata_to_platdata(dev);
344 ret = drv->probe(dev);
346 dev->flags &= ~DM_FLAG_ACTIVATED;
351 ret = uclass_post_probe_device(dev);
355 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
356 pinctrl_select_state(dev, "default");
360 if (device_remove(dev)) {
361 dm_warn("%s: Device '%s' failed to remove on error path\n",
362 __func__, dev->name);
365 dev->flags &= ~DM_FLAG_ACTIVATED;
373 void *dev_get_platdata(struct udevice *dev)
376 dm_warn("%s: null device\n", __func__);
380 return dev->platdata;
383 void *dev_get_parent_platdata(struct udevice *dev)
386 dm_warn("%s: null device\n", __func__);
390 return dev->parent_platdata;
393 void *dev_get_uclass_platdata(struct udevice *dev)
396 dm_warn("%s: null device\n", __func__);
400 return dev->uclass_platdata;
403 void *dev_get_priv(struct udevice *dev)
406 dm_warn("%s: null device\n", __func__);
413 void *dev_get_uclass_priv(struct udevice *dev)
416 dm_warn("%s: null device\n", __func__);
420 return dev->uclass_priv;
423 void *dev_get_parent_priv(struct udevice *dev)
426 dm_warn("%s: null device\n", __func__);
430 return dev->parent_priv;
433 static int device_get_device_tail(struct udevice *dev, int ret,
434 struct udevice **devp)
439 ret = device_probe(dev);
448 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
452 list_for_each_entry(dev, &parent->child_head, sibling_node) {
454 return device_get_device_tail(dev, 0, devp);
460 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
461 bool find_req_seq, struct udevice **devp)
466 if (seq_or_req_seq == -1)
469 list_for_each_entry(dev, &parent->child_head, sibling_node) {
470 if ((find_req_seq ? dev->req_seq : dev->seq) ==
480 int device_get_child_by_seq(struct udevice *parent, int seq,
481 struct udevice **devp)
487 ret = device_find_child_by_seq(parent, seq, false, &dev);
488 if (ret == -ENODEV) {
490 * We didn't find it in probed devices. See if there is one
491 * that will request this seq if probed.
493 ret = device_find_child_by_seq(parent, seq, true, &dev);
495 return device_get_device_tail(dev, ret, devp);
498 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
499 struct udevice **devp)
505 list_for_each_entry(dev, &parent->child_head, sibling_node) {
506 if (dev->of_offset == of_offset) {
515 int device_get_child_by_of_offset(struct udevice *parent, int node,
516 struct udevice **devp)
522 ret = device_find_child_by_of_offset(parent, node, &dev);
523 return device_get_device_tail(dev, ret, devp);
526 static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
529 struct udevice *dev, *found;
531 if (parent->of_offset == of_offset)
534 list_for_each_entry(dev, &parent->child_head, sibling_node) {
535 found = _device_find_global_by_of_offset(dev, of_offset);
543 int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
547 dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
548 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
551 int device_find_first_child(struct udevice *parent, struct udevice **devp)
553 if (list_empty(&parent->child_head)) {
556 *devp = list_first_entry(&parent->child_head, struct udevice,
563 int device_find_next_child(struct udevice **devp)
565 struct udevice *dev = *devp;
566 struct udevice *parent = dev->parent;
568 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
571 *devp = list_entry(dev->sibling_node.next, struct udevice,
578 struct udevice *dev_get_parent(struct udevice *child)
580 return child->parent;
583 ulong dev_get_driver_data(struct udevice *dev)
585 return dev->driver_data;
588 const void *dev_get_driver_ops(struct udevice *dev)
590 if (!dev || !dev->driver->ops)
593 return dev->driver->ops;
596 enum uclass_id device_get_uclass_id(struct udevice *dev)
598 return dev->uclass->uc_drv->id;
601 const char *dev_get_uclass_name(struct udevice *dev)
606 return dev->uclass->uc_drv->name;
609 fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
611 #if CONFIG_IS_ENABLED(OF_CONTROL)
614 if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
619 na = fdt_address_cells(gd->fdt_blob, dev->parent->of_offset);
621 debug("bad #address-cells\n");
622 return FDT_ADDR_T_NONE;
625 ns = fdt_size_cells(gd->fdt_blob, dev->parent->of_offset);
627 debug("bad #size-cells\n");
628 return FDT_ADDR_T_NONE;
631 reg = fdt_getprop(gd->fdt_blob, dev->of_offset, "reg", &len);
632 if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
633 debug("Req index out of range\n");
634 return FDT_ADDR_T_NONE;
637 reg += index * (na + ns);
640 * Use the full-fledged translate function for complex
643 addr = fdt_translate_address((void *)gd->fdt_blob,
644 dev->of_offset, reg);
647 * Use the "simple" translate function for less complex
650 addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
651 dev->parent->of_offset,
652 dev->of_offset, "reg",
654 if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
655 if (device_get_uclass_id(dev->parent) ==
657 addr = simple_bus_translate(dev->parent, addr);
662 * Some platforms need a special address translation. Those
663 * platforms (e.g. mvebu in SPL) can configure a translation
664 * offset in the DM by calling dm_set_translation_offset() that
665 * will get added to all addresses returned by dev_get_addr().
667 addr += dm_get_translation_offset();
671 return FDT_ADDR_T_NONE;
675 fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name)
677 #if CONFIG_IS_ENABLED(OF_CONTROL)
680 index = fdt_find_string(gd->fdt_blob, dev->of_offset, "reg-names",
685 return dev_get_addr_index(dev, index);
687 return FDT_ADDR_T_NONE;
691 fdt_addr_t dev_get_addr(struct udevice *dev)
693 return dev_get_addr_index(dev, 0);
696 void *dev_get_addr_ptr(struct udevice *dev)
698 return (void *)(uintptr_t)dev_get_addr_index(dev, 0);
701 void *dev_map_physmem(struct udevice *dev, unsigned long size)
703 fdt_addr_t addr = dev_get_addr(dev);
705 if (addr == FDT_ADDR_T_NONE)
708 return map_physmem(addr, size, MAP_NOCACHE);
711 bool device_has_children(struct udevice *dev)
713 return !list_empty(&dev->child_head);
716 bool device_has_active_children(struct udevice *dev)
718 struct udevice *child;
720 for (device_find_first_child(dev, &child);
722 device_find_next_child(&child)) {
723 if (device_active(child))
730 bool device_is_last_sibling(struct udevice *dev)
732 struct udevice *parent = dev->parent;
736 return list_is_last(&dev->sibling_node, &parent->child_head);
739 void device_set_name_alloced(struct udevice *dev)
741 dev->flags |= DM_NAME_ALLOCED;
744 int device_set_name(struct udevice *dev, const char *name)
750 device_set_name_alloced(dev);
755 bool of_device_is_compatible(struct udevice *dev, const char *compat)
757 const void *fdt = gd->fdt_blob;
759 return !fdt_node_check_compatible(fdt, dev->of_offset, compat);
762 bool of_machine_is_compatible(const char *compat)
764 const void *fdt = gd->fdt_blob;
766 return !fdt_node_check_compatible(fdt, 0, compat);