dm: core: Add a post_bind method for parents
[oweals/u-boot.git] / drivers / core / device.c
index 32e80e82b59c99f9ac5734c8f3dadc7c3b0c5f28..365676b9121941afb9982013296422ee8b5b77af 100644 (file)
 
 DECLARE_GLOBAL_DATA_PTR;
 
-/**
- * device_chld_unbind() - Unbind all device's children from the device
- *
- * On error, the function continues to unbind all children, and reports the
- * first error.
- *
- * @dev:       The device that is to be stripped of its children
- * @return 0 on success, -ve on error
- */
-static int device_chld_unbind(struct udevice *dev)
-{
-       struct udevice *pos, *n;
-       int ret, saved_ret = 0;
-
-       assert(dev);
-
-       list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
-               ret = device_unbind(pos);
-               if (ret && !saved_ret)
-                       saved_ret = ret;
-       }
-
-       return saved_ret;
-}
-
-/**
- * device_chld_remove() - Stop all device's children
- * @dev:       The device whose children are to be removed
- * @return 0 on success, -ve on error
- */
-static int device_chld_remove(struct udevice *dev)
-{
-       struct udevice *pos, *n;
-       int ret;
-
-       assert(dev);
-
-       list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
-               ret = device_remove(pos);
-               if (ret)
-                       return ret;
-       }
-
-       return 0;
-}
-
 int device_bind(struct udevice *parent, struct driver *drv, const char *name,
                void *platdata, int of_offset, struct udevice **devp)
 {
@@ -118,8 +72,30 @@ int device_bind(struct udevice *parent, struct driver *drv, const char *name,
 #else
        dev->req_seq = -1;
 #endif
-       if (!dev->platdata && drv->platdata_auto_alloc_size)
+       if (!dev->platdata && drv->platdata_auto_alloc_size) {
                dev->flags |= DM_FLAG_ALLOC_PDATA;
+               dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
+               if (!dev->platdata) {
+                       ret = -ENOMEM;
+                       goto fail_alloc1;
+               }
+       }
+       if (parent) {
+               int size = parent->driver->per_child_platdata_auto_alloc_size;
+
+               if (!size) {
+                       size = parent->uclass->uc_drv->
+                                       per_child_platdata_auto_alloc_size;
+               }
+               if (size) {
+                       dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
+                       dev->parent_platdata = calloc(1, size);
+                       if (!dev->parent_platdata) {
+                               ret = -ENOMEM;
+                               goto fail_alloc2;
+                       }
+               }
+       }
 
        /* put dev into parent's successor list */
        if (parent)
@@ -127,28 +103,51 @@ int device_bind(struct udevice *parent, struct driver *drv, const char *name,
 
        ret = uclass_bind_device(dev);
        if (ret)
-               goto fail_bind;
+               goto fail_uclass_bind;
 
        /* if we fail to bind we remove device from successors and free it */
        if (drv->bind) {
                ret = drv->bind(dev);
-               if (ret) {
-                       if (uclass_unbind_device(dev)) {
-                               dm_warn("Failed to unbind dev '%s' on error path\n",
-                                       dev->name);
-                       }
+               if (ret)
                        goto fail_bind;
-               }
        }
+       if (parent && parent->driver->child_post_bind) {
+               ret = parent->driver->child_post_bind(dev);
+               if (ret)
+                       goto fail_child_post_bind;
+       }
+
        if (parent)
                dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
        *devp = dev;
 
        return 0;
 
+fail_child_post_bind:
+       if (drv->unbind && drv->unbind(dev)) {
+               dm_warn("unbind() method failed on dev '%s' on error path\n",
+                       dev->name);
+       }
+
 fail_bind:
+       if (uclass_unbind_device(dev)) {
+               dm_warn("Failed to unbind dev '%s' on error path\n",
+                       dev->name);
+       }
+fail_uclass_bind:
        list_del(&dev->sibling_node);
+       if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
+               free(dev->parent_platdata);
+               dev->parent_platdata = NULL;
+       }
+fail_alloc2:
+       if (dev->flags & DM_FLAG_ALLOC_PDATA) {
+               free(dev->platdata);
+               dev->platdata = NULL;
+       }
+fail_alloc1:
        free(dev);
+
        return ret;
 }
 
@@ -167,72 +166,7 @@ int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
                           -1, devp);
 }
 
-int device_unbind(struct udevice *dev)
-{
-       struct driver *drv;
-       int ret;
-
-       if (!dev)
-               return -EINVAL;
-
-       if (dev->flags & DM_FLAG_ACTIVATED)
-               return -EINVAL;
-
-       drv = dev->driver;
-       assert(drv);
-
-       if (drv->unbind) {
-               ret = drv->unbind(dev);
-               if (ret)
-                       return ret;
-       }
-
-       ret = device_chld_unbind(dev);
-       if (ret)
-               return ret;
-
-       ret = uclass_unbind_device(dev);
-       if (ret)
-               return ret;
-
-       if (dev->parent)
-               list_del(&dev->sibling_node);
-       free(dev);
-
-       return 0;
-}
-
-/**
- * device_free() - Free memory buffers allocated by a device
- * @dev:       Device that is to be started
- */
-static void device_free(struct udevice *dev)
-{
-       int size;
-
-       if (dev->driver->priv_auto_alloc_size) {
-               free(dev->priv);
-               dev->priv = NULL;
-       }
-       if (dev->flags & DM_FLAG_ALLOC_PDATA) {
-               free(dev->platdata);
-               dev->platdata = NULL;
-       }
-       size = dev->uclass->uc_drv->per_device_auto_alloc_size;
-       if (size) {
-               free(dev->uclass_priv);
-               dev->uclass_priv = NULL;
-       }
-       if (dev->parent) {
-               size = dev->parent->driver->per_child_auto_alloc_size;
-               if (size) {
-                       free(dev->parent_priv);
-                       dev->parent_priv = NULL;
-               }
-       }
-}
-
-int device_probe(struct udevice *dev)
+int device_probe_child(struct udevice *dev, void *parent_priv)
 {
        struct driver *drv;
        int size = 0;
@@ -248,7 +182,7 @@ int device_probe(struct udevice *dev)
        drv = dev->driver;
        assert(drv);
 
-       /* Allocate private data and platdata if requested */
+       /* Allocate private data if requested */
        if (drv->priv_auto_alloc_size) {
                dev->priv = calloc(1, drv->priv_auto_alloc_size);
                if (!dev->priv) {
@@ -257,13 +191,6 @@ int device_probe(struct udevice *dev)
                }
        }
        /* Allocate private data if requested */
-       if (dev->flags & DM_FLAG_ALLOC_PDATA) {
-               dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
-               if (!dev->platdata) {
-                       ret = -ENOMEM;
-                       goto fail;
-               }
-       }
        size = dev->uclass->uc_drv->per_device_auto_alloc_size;
        if (size) {
                dev->uclass_priv = calloc(1, size);
@@ -282,6 +209,8 @@ int device_probe(struct udevice *dev)
                                ret = -ENOMEM;
                                goto fail;
                        }
+                       if (parent_priv)
+                               memcpy(dev->parent_priv, parent_priv, size);
                }
 
                ret = device_probe(dev->parent);
@@ -335,77 +264,35 @@ fail:
        return ret;
 }
 
-int device_remove(struct udevice *dev)
+int device_probe(struct udevice *dev)
 {
-       struct driver *drv;
-       int ret;
-
-       if (!dev)
-               return -EINVAL;
-
-       if (!(dev->flags & DM_FLAG_ACTIVATED))
-               return 0;
-
-       drv = dev->driver;
-       assert(drv);
-
-       ret = uclass_pre_remove_device(dev);
-       if (ret)
-               return ret;
-
-       ret = device_chld_remove(dev);
-       if (ret)
-               goto err;
-
-       if (drv->remove) {
-               ret = drv->remove(dev);
-               if (ret)
-                       goto err_remove;
-       }
-
-       if (dev->parent && dev->parent->driver->child_post_remove) {
-               ret = dev->parent->driver->child_post_remove(dev);
-               if (ret) {
-                       dm_warn("%s: Device '%s' failed child_post_remove()",
-                               __func__, dev->name);
-               }
-       }
-
-       device_free(dev);
-
-       dev->seq = -1;
-       dev->flags &= ~DM_FLAG_ACTIVATED;
-
-       return ret;
+       return device_probe_child(dev, NULL);
+}
 
-err_remove:
-       /* We can't put the children back */
-       dm_warn("%s: Device '%s' failed to remove, but children are gone\n",
-               __func__, dev->name);
-err:
-       ret = uclass_post_probe_device(dev);
-       if (ret) {
-               dm_warn("%s: Device '%s' failed to post_probe on error path\n",
-                       __func__, dev->name);
+void *dev_get_platdata(struct udevice *dev)
+{
+       if (!dev) {
+               dm_warn("%s: null device\n", __func__);
+               return NULL;
        }
 
-       return ret;
+       return dev->platdata;
 }
 
-void *dev_get_platdata(struct udevice *dev)
+void *dev_get_parent_platdata(struct udevice *dev)
 {
        if (!dev) {
                dm_warn("%s: null device", __func__);
                return NULL;
        }
 
-       return dev->platdata;
+       return dev->parent_platdata;
 }
 
 void *dev_get_priv(struct udevice *dev)
 {
        if (!dev) {
-               dm_warn("%s: null device", __func__);
+               dm_warn("%s: null device\n", __func__);
                return NULL;
        }
 
@@ -415,7 +302,7 @@ void *dev_get_priv(struct udevice *dev)
 void *dev_get_parentdata(struct udevice *dev)
 {
        if (!dev) {
-               dm_warn("%s: null device", __func__);
+               dm_warn("%s: null device\n", __func__);
                return NULL;
        }
 
@@ -514,3 +401,40 @@ int device_get_child_by_of_offset(struct udevice *parent, int seq,
        ret = device_find_child_by_of_offset(parent, seq, &dev);
        return device_get_device_tail(dev, ret, devp);
 }
+
+int device_find_first_child(struct udevice *parent, struct udevice **devp)
+{
+       if (list_empty(&parent->child_head)) {
+               *devp = NULL;
+       } else {
+               *devp = list_first_entry(&parent->child_head, struct udevice,
+                                        sibling_node);
+       }
+
+       return 0;
+}
+
+int device_find_next_child(struct udevice **devp)
+{
+       struct udevice *dev = *devp;
+       struct udevice *parent = dev->parent;
+
+       if (list_is_last(&dev->sibling_node, &parent->child_head)) {
+               *devp = NULL;
+       } else {
+               *devp = list_entry(dev->sibling_node.next, struct udevice,
+                                  sibling_node);
+       }
+
+       return 0;
+}
+
+struct udevice *dev_get_parent(struct udevice *child)
+{
+       return child->parent;
+}
+
+ulong dev_get_of_data(struct udevice *dev)
+{
+       return dev->of_id->data;
+}