]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
dm: core: Split out alloc code into a new function
authorSimon Glass <sjg@chromium.org>
Sat, 19 Dec 2020 17:40:08 +0000 (10:40 -0700)
committerSimon Glass <sjg@chromium.org>
Tue, 5 Jan 2021 19:24:41 +0000 (12:24 -0700)
Add a new function to handle the allocation of private/platform data for
a device. This will make it easier to skip this feature when using the new
of-platdata.

Signed-off-by: Simon Glass <sjg@chromium.org>
drivers/core/device.c

index a4c8310f81289e090801754ce59d3456686bfc1b..72169632c881da5f62a362e11234652cec4e99fe 100644 (file)
@@ -323,34 +323,17 @@ static void *alloc_priv(int size, uint flags)
        return priv;
 }
 
-int device_of_to_plat(struct udevice *dev)
+/**
+ * device_alloc_priv() - Allocate priv/plat data required by the device
+ *
+ * @dev: Device to process
+ * @return 0 if OK, -ENOMEM if out of memory
+ */
+static int device_alloc_priv(struct udevice *dev)
 {
        const struct driver *drv;
-       int size = 0;
        void *ptr;
-       int ret;
-
-       if (!dev)
-               return -EINVAL;
-
-       if (dev->flags & DM_FLAG_PLATDATA_VALID)
-               return 0;
-
-       /* Ensure all parents have ofdata */
-       if (dev->parent) {
-               ret = device_of_to_plat(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;
-       }
+       int size;
 
        drv = dev->driver;
        assert(drv);
@@ -358,20 +341,17 @@ int device_of_to_plat(struct udevice *dev)
        /* Allocate private data if requested and not reentered */
        if (drv->priv_auto && !dev_get_priv(dev)) {
                ptr = alloc_priv(drv->priv_auto, drv->flags);
-               if (!ptr) {
-                       ret = -ENOMEM;
-                       goto fail;
-               }
+               if (!ptr)
+                       return -ENOMEM;
                dev_set_priv(dev, ptr);
        }
+
        /* Allocate private data if requested and not reentered */
        size = dev->uclass->uc_drv->per_device_auto;
        if (size && !dev_get_uclass_priv(dev)) {
                ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
-               if (!ptr) {
-                       ret = -ENOMEM;
-                       goto fail;
-               }
+               if (!ptr)
+                       return -ENOMEM;
                dev_set_uclass_priv(dev, ptr);
        }
 
@@ -382,14 +362,49 @@ int device_of_to_plat(struct udevice *dev)
                        size = dev->parent->uclass->uc_drv->per_child_auto;
                if (size && !dev_get_parent_priv(dev)) {
                        ptr = alloc_priv(size, drv->flags);
-                       if (!ptr) {
-                               ret = -ENOMEM;
-                               goto fail;
-                       }
+                       if (!ptr)
+                               return -ENOMEM;
                        dev_set_parent_priv(dev, ptr);
                }
        }
 
+       return 0;
+}
+
+int device_of_to_plat(struct udevice *dev)
+{
+       const struct driver *drv;
+       int ret;
+
+       if (!dev)
+               return -EINVAL;
+
+       if (dev->flags & DM_FLAG_PLATDATA_VALID)
+               return 0;
+
+       /* Ensure all parents have ofdata */
+       if (dev->parent) {
+               ret = device_of_to_plat(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;
+       }
+
+       ret = device_alloc_priv(dev);
+       if (ret)
+               goto fail;
+
+       drv = dev->driver;
+       assert(drv);
+
        if (drv->of_to_plat &&
            (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
                ret = drv->of_to_plat(dev);