Merge tag 'v2021.01-rc5' into next
[platform/kernel/u-boot.git] / drivers / core / device-remove.c
index 3c6ab42..8a5f958 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Device manager
  *
@@ -5,29 +6,20 @@
  *
  * (C) Copyright 2012
  * Pavel Herrmann <morpheus.ibis@gmail.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <errno.h>
+#include <log.h>
 #include <malloc.h>
 #include <dm/device.h>
 #include <dm/device-internal.h>
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
+#include <power-domain.h>
 
-/**
- * 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)
+int device_chld_unbind(struct udevice *dev, struct driver *drv)
 {
        struct udevice *pos, *n;
        int ret, saved_ret = 0;
@@ -35,21 +27,22 @@ static int device_chld_unbind(struct udevice *dev)
        assert(dev);
 
        list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
+               if (drv && (pos->driver != drv))
+                       continue;
+
                ret = device_unbind(pos);
-               if (ret && !saved_ret)
+               if (ret && !saved_ret) {
+                       log_warning("device '%s' failed to unbind\n",
+                                   pos->name);
                        saved_ret = ret;
+               }
        }
 
-       return saved_ret;
+       return log_ret(saved_ret);
 }
 
-/**
- * device_chld_remove() - Stop all device's children
- * @dev:       The device whose children are to be removed
- * @pre_os_remove: Flag, if this functions is called in the pre-OS stage
- * @return 0 on success, -ve on error
- */
-static int device_chld_remove(struct udevice *dev, uint flags)
+int device_chld_remove(struct udevice *dev, struct driver *drv,
+                      uint flags)
 {
        struct udevice *pos, *n;
        int ret;
@@ -57,6 +50,9 @@ static int device_chld_remove(struct udevice *dev, uint flags)
        assert(dev);
 
        list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
+               if (drv && (pos->driver != drv))
+                       continue;
+
                ret = device_remove(pos, flags);
                if (ret)
                        return ret;
@@ -71,13 +67,13 @@ int device_unbind(struct udevice *dev)
        int ret;
 
        if (!dev)
-               return -EINVAL;
+               return log_msg_ret("dev", -EINVAL);
 
        if (dev->flags & DM_FLAG_ACTIVATED)
-               return -EINVAL;
+               return log_msg_ret("active", -EINVAL);
 
        if (!(dev->flags & DM_FLAG_BOUND))
-               return -EINVAL;
+               return log_msg_ret("not-bound", -EINVAL);
 
        drv = dev->driver;
        assert(drv);
@@ -85,28 +81,28 @@ int device_unbind(struct udevice *dev)
        if (drv->unbind) {
                ret = drv->unbind(dev);
                if (ret)
-                       return ret;
+                       return log_msg_ret("unbind", ret);
        }
 
-       ret = device_chld_unbind(dev);
+       ret = device_chld_unbind(dev, NULL);
        if (ret)
-               return ret;
+               return log_msg_ret("child unbind", ret);
 
        if (dev->flags & DM_FLAG_ALLOC_PDATA) {
-               free(dev->platdata);
-               dev->platdata = NULL;
+               free(dev->plat);
+               dev->plat = NULL;
        }
        if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
-               free(dev->uclass_platdata);
-               dev->uclass_platdata = NULL;
+               free(dev->uclass_plat);
+               dev->uclass_plat = NULL;
        }
        if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
-               free(dev->parent_platdata);
-               dev->parent_platdata = NULL;
+               free(dev->parent_plat);
+               dev->parent_plat = NULL;
        }
        ret = uclass_unbind_device(dev);
        if (ret)
-               return ret;
+               return log_msg_ret("uc", ret);
 
        if (dev->parent)
                list_del(&dev->sibling_node);
@@ -128,26 +124,27 @@ void device_free(struct udevice *dev)
 {
        int size;
 
-       if (dev->driver->priv_auto_alloc_size) {
+       if (dev->driver->priv_auto) {
                free(dev->priv);
                dev->priv = NULL;
        }
-       size = dev->uclass->uc_drv->per_device_auto_alloc_size;
+       size = dev->uclass->uc_drv->per_device_auto;
        if (size) {
                free(dev->uclass_priv);
                dev->uclass_priv = NULL;
        }
        if (dev->parent) {
-               size = dev->parent->driver->per_child_auto_alloc_size;
+               size = dev->parent->driver->per_child_auto;
                if (!size) {
                        size = dev->parent->uclass->uc_drv->
-                                       per_child_auto_alloc_size;
+                                       per_child_auto;
                }
                if (size) {
                        free(dev->parent_priv);
                        dev->parent_priv = NULL;
                }
        }
+       dev->flags &= ~DM_FLAG_PLATDATA_VALID;
 
        devres_release_probe(dev);
 }
@@ -155,7 +152,7 @@ void device_free(struct udevice *dev)
 static bool flags_remove(uint flags, uint drv_flags)
 {
        if ((flags & DM_REMOVE_NORMAL) ||
-           (flags & (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
+           (flags && (drv_flags & (DM_FLAG_ACTIVE_DMA | DM_FLAG_OS_PREPARE))))
                return true;
 
        return false;
@@ -179,7 +176,7 @@ int device_remove(struct udevice *dev, uint flags)
        if (ret)
                return ret;
 
-       ret = device_chld_remove(dev, flags);
+       ret = device_chld_remove(dev, NULL, flags);
        if (ret)
                goto err;
 
@@ -201,10 +198,15 @@ int device_remove(struct udevice *dev, uint flags)
                }
        }
 
+       if (!(flags & DM_REMOVE_NO_PD) &&
+           !(drv->flags &
+             (DM_FLAG_DEFAULT_PD_CTRL_OFF | DM_FLAG_REMOVE_WITH_PD_ON)) &&
+           dev != gd->cur_serial_dev)
+               dev_power_domain_off(dev);
+
        if (flags_remove(flags, drv->flags)) {
                device_free(dev);
 
-               dev->seq = -1;
                dev->flags &= ~DM_FLAG_ACTIVATED;
        }