dm: Use driver_info index instead of pointer
[platform/kernel/u-boot.git] / drivers / core / device.c
index 4e03708..4b3dcb3 100644 (file)
 
 #include <common.h>
 #include <cpu_func.h>
+#include <log.h>
 #include <asm/io.h>
 #include <clk.h>
 #include <fdtdec.h>
 #include <fdt_support.h>
 #include <malloc.h>
+#include <asm/cache.h>
 #include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/lists.h>
@@ -80,7 +82,8 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv,
                 * This is just a 'requested' sequence, and will be
                 * resolved (and ->seq updated) when the device is probed.
                 */
-               if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
+               if (CONFIG_IS_ENABLED(OF_CONTROL) &&
+                   !CONFIG_IS_ENABLED(OF_PLATDATA)) {
                        if (uc->uc_drv->name && ofnode_valid(node))
                                dev_read_alias_seq(dev, &dev->req_seq);
 #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
@@ -143,11 +146,9 @@ static int device_bind_common(struct udevice *parent, const struct driver *drv,
                                goto fail_alloc3;
                        }
                }
-       }
-
-       /* put dev into parent's successor list */
-       if (parent)
+               /* put dev into parent's successor list */
                list_add_tail(&dev->sibling_node, &parent->child_head);
+       }
 
        ret = uclass_bind_device(dev);
        if (ret)
@@ -252,6 +253,7 @@ int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
 {
        struct driver *drv;
        uint platdata_size = 0;
+       int ret;
 
        drv = lists_driver_lookup_name(info->name);
        if (!drv)
@@ -262,9 +264,35 @@ int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
 #if CONFIG_IS_ENABLED(OF_PLATDATA)
        platdata_size = info->platdata_size;
 #endif
-       return device_bind_common(parent, drv, info->name,
-                       (void *)info->platdata, 0, ofnode_null(), platdata_size,
-                       devp);
+       ret = device_bind_common(parent, drv, info->name,
+                                (void *)info->platdata, 0, ofnode_null(),
+                                platdata_size, devp);
+       if (ret)
+               return ret;
+
+       return ret;
+}
+
+int device_reparent(struct udevice *dev, struct udevice *new_parent)
+{
+       struct udevice *pos, *n;
+
+       assert(dev);
+       assert(new_parent);
+
+       list_for_each_entry_safe(pos, n, &dev->parent->child_head,
+                                sibling_node) {
+               if (pos->driver != dev->driver)
+                       continue;
+
+               list_del(&dev->sibling_node);
+               list_add_tail(&dev->sibling_node, &new_parent->child_head);
+               dev->parent = new_parent;
+
+               break;
+       }
+
+       return 0;
 }
 
 static void *alloc_priv(int size, uint flags)
@@ -311,19 +339,34 @@ static void *alloc_priv(int size, uint flags)
        return priv;
 }
 
-int device_probe(struct udevice *dev)
+int device_ofdata_to_platdata(struct udevice *dev)
 {
        const struct driver *drv;
        int size = 0;
        int ret;
-       int seq;
 
        if (!dev)
                return -EINVAL;
 
-       if (dev->flags & DM_FLAG_ACTIVATED)
+       if (dev->flags & DM_FLAG_PLATDATA_VALID)
                return 0;
 
+       /* Ensure all parents have ofdata */
+       if (dev->parent) {
+               ret = device_ofdata_to_platdata(dev->parent);
+               if (ret)
+                       goto fail;
+
+               /*
+                * The device might have already been probed during
+                * the call to device_probe() on its parent device
+                * (e.g. PCI bridge devices). Test the flags again
+                * so that we don't mess up the device.
+                */
+               if (dev->flags & DM_FLAG_PLATDATA_VALID)
+                       return 0;
+       }
+
        drv = dev->driver;
        assert(drv);
 
@@ -346,7 +389,7 @@ int device_probe(struct udevice *dev)
                }
        }
 
-       /* Ensure all parents are probed */
+       /* Allocate parent data for this child */
        if (dev->parent) {
                size = dev->parent->driver->per_child_auto_alloc_size;
                if (!size) {
@@ -360,7 +403,45 @@ int device_probe(struct udevice *dev)
                                goto fail;
                        }
                }
+       }
+
+       if (drv->ofdata_to_platdata &&
+           (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
+               ret = drv->ofdata_to_platdata(dev);
+               if (ret)
+                       goto fail;
+       }
 
+       dev->flags |= DM_FLAG_PLATDATA_VALID;
+
+       return 0;
+fail:
+       device_free(dev);
+
+       return ret;
+}
+
+int device_probe(struct udevice *dev)
+{
+       const struct driver *drv;
+       int ret;
+       int seq;
+
+       if (!dev)
+               return -EINVAL;
+
+       if (dev->flags & DM_FLAG_ACTIVATED)
+               return 0;
+
+       drv = dev->driver;
+       assert(drv);
+
+       ret = device_ofdata_to_platdata(dev);
+       if (ret)
+               goto fail;
+
+       /* Ensure all parents are probed */
+       if (dev->parent) {
                ret = device_probe(dev->parent);
                if (ret)
                        goto fail;
@@ -411,13 +492,6 @@ int device_probe(struct udevice *dev)
                        goto fail;
        }
 
-       if (drv->ofdata_to_platdata &&
-           (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
-               ret = drv->ofdata_to_platdata(dev);
-               if (ret)
-                       goto fail;
-       }
-
        /* Only handle devices that have a valid ofnode */
        if (dev_of_valid(dev)) {
                /*
@@ -431,10 +505,8 @@ int device_probe(struct udevice *dev)
 
        if (drv->probe) {
                ret = drv->probe(dev);
-               if (ret) {
-                       dev->flags &= ~DM_FLAG_ACTIVATED;
+               if (ret)
                        goto fail;
-               }
        }
 
        ret = uclass_post_probe_device(dev);
@@ -563,7 +635,8 @@ static int device_find_by_ofnode(ofnode node, struct udevice **devp)
 }
 #endif
 
-int device_get_child(struct udevice *parent, int index, struct udevice **devp)
+int device_get_child(const struct udevice *parent, int index,
+                    struct udevice **devp)
 {
        struct udevice *dev;
 
@@ -575,7 +648,7 @@ int device_get_child(struct udevice *parent, int index, struct udevice **devp)
        return -ENODEV;
 }
 
-int device_get_child_count(struct udevice *parent)
+int device_get_child_count(const struct udevice *parent)
 {
        struct udevice *dev;
        int count = 0;
@@ -586,7 +659,7 @@ int device_get_child_count(struct udevice *parent)
        return count;
 }
 
-int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
+int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
                             bool find_req_seq, struct udevice **devp)
 {
        struct udevice *dev;
@@ -606,7 +679,7 @@ int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
        return -ENODEV;
 }
 
-int device_get_child_by_seq(struct udevice *parent, int seq,
+int device_get_child_by_seq(const struct udevice *parent, int seq,
                            struct udevice **devp)
 {
        struct udevice *dev;
@@ -624,7 +697,7 @@ int device_get_child_by_seq(struct udevice *parent, int seq,
        return device_get_device_tail(dev, ret, devp);
 }
 
-int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
+int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
                                   struct udevice **devp)
 {
        struct udevice *dev;
@@ -641,7 +714,7 @@ int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
        return -ENODEV;
 }
 
-int device_get_child_by_of_offset(struct udevice *parent, int node,
+int device_get_child_by_of_offset(const struct udevice *parent, int node,
                                  struct udevice **devp)
 {
        struct udevice *dev;
@@ -684,7 +757,35 @@ int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
        return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
 }
 
-int device_find_first_child(struct udevice *parent, struct udevice **devp)
+#if CONFIG_IS_ENABLED(OF_PLATDATA)
+int device_get_by_driver_info(const struct driver_info *info,
+                             struct udevice **devp)
+{
+       struct driver_info *info_base =
+               ll_entry_start(struct driver_info, driver_info);
+       int idx = info - info_base;
+       struct driver_rt *drt = gd_dm_driver_rt() + idx;
+       struct udevice *dev;
+
+       dev = drt->dev;
+       *devp = NULL;
+
+       return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
+}
+
+int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
+{
+       struct driver_rt *drt = gd_dm_driver_rt() + idx;
+       struct udevice *dev;
+
+       dev = drt->dev;
+       *devp = NULL;
+
+       return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
+}
+#endif
+
+int device_find_first_child(const struct udevice *parent, struct udevice **devp)
 {
        if (list_empty(&parent->child_head)) {
                *devp = NULL;
@@ -711,7 +812,7 @@ int device_find_next_child(struct udevice **devp)
        return 0;
 }
 
-int device_find_first_inactive_child(struct udevice *parent,
+int device_find_first_inactive_child(const struct udevice *parent,
                                     enum uclass_id uclass_id,
                                     struct udevice **devp)
 {
@@ -729,7 +830,7 @@ int device_find_first_inactive_child(struct udevice *parent,
        return -ENODEV;
 }
 
-int device_find_first_child_by_uclass(struct udevice *parent,
+int device_find_first_child_by_uclass(const struct udevice *parent,
                                      enum uclass_id uclass_id,
                                      struct udevice **devp)
 {
@@ -746,7 +847,7 @@ int device_find_first_child_by_uclass(struct udevice *parent,
        return -ENODEV;
 }
 
-int device_find_child_by_name(struct udevice *parent, const char *name,
+int device_find_child_by_name(const struct udevice *parent, const char *name,
                              struct udevice **devp)
 {
        struct udevice *dev;
@@ -763,6 +864,64 @@ int device_find_child_by_name(struct udevice *parent, const char *name,
        return -ENODEV;
 }
 
+int device_first_child_err(struct udevice *parent, struct udevice **devp)
+{
+       struct udevice *dev;
+
+       device_find_first_child(parent, &dev);
+       if (!dev)
+               return -ENODEV;
+
+       return device_get_device_tail(dev, 0, devp);
+}
+
+int device_next_child_err(struct udevice **devp)
+{
+       struct udevice *dev = *devp;
+
+       device_find_next_child(&dev);
+       if (!dev)
+               return -ENODEV;
+
+       return device_get_device_tail(dev, 0, devp);
+}
+
+int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
+{
+       struct udevice *dev;
+       int ret;
+
+       device_find_first_child(parent, &dev);
+       if (!dev)
+               return -ENODEV;
+
+       ret = device_ofdata_to_platdata(dev);
+       if (ret)
+               return ret;
+
+       *devp = dev;
+
+       return 0;
+}
+
+int device_next_child_ofdata_err(struct udevice **devp)
+{
+       struct udevice *dev = *devp;
+       int ret;
+
+       device_find_next_child(&dev);
+       if (!dev)
+               return -ENODEV;
+
+       ret = device_ofdata_to_platdata(dev);
+       if (ret)
+               return ret;
+
+       *devp = dev;
+
+       return 0;
+}
+
 struct udevice *dev_get_parent(const struct udevice *child)
 {
        return child->parent;
@@ -799,7 +958,7 @@ bool device_has_children(const struct udevice *dev)
        return !list_empty(&dev->child_head);
 }
 
-bool device_has_active_children(struct udevice *dev)
+bool device_has_active_children(const struct udevice *dev)
 {
        struct udevice *child;
 
@@ -813,7 +972,7 @@ bool device_has_active_children(struct udevice *dev)
        return false;
 }
 
-bool device_is_last_sibling(struct udevice *dev)
+bool device_is_last_sibling(const struct udevice *dev)
 {
        struct udevice *parent = dev->parent;
 
@@ -839,7 +998,7 @@ int device_set_name(struct udevice *dev, const char *name)
 }
 
 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-bool device_is_compatible(struct udevice *dev, const char *compat)
+bool device_is_compatible(const struct udevice *dev, const char *compat)
 {
        return ofnode_device_is_compatible(dev_ofnode(dev), compat);
 }