configs: Add sam9x60ek_mmc_defconfig
[oweals/u-boot.git] / drivers / core / device-remove.c
index 0e56b23fbbf44b059b6e38afd1c8366ee245e764..586fadee0a8731f9b77f24369606ed355d94e5b5 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Device manager
  *
@@ -5,8 +6,6 @@
  *
  * (C) Copyright 2012
  * Pavel Herrmann <morpheus.ibis@gmail.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.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,6 +25,9 @@ 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)
                        saved_ret = ret;
@@ -43,12 +36,8 @@ static int device_chld_unbind(struct udevice *dev)
        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)
+int device_chld_remove(struct udevice *dev, struct driver *drv,
+                      uint flags)
 {
        struct udevice *pos, *n;
        int ret;
@@ -56,7 +45,10 @@ static int device_chld_remove(struct udevice *dev)
        assert(dev);
 
        list_for_each_entry_safe(pos, n, &dev->child_head, sibling_node) {
-               ret = device_remove(pos);
+               if (drv && (pos->driver != drv))
+                       continue;
+
+               ret = device_remove(pos, flags);
                if (ret)
                        return ret;
        }
@@ -87,7 +79,7 @@ int device_unbind(struct udevice *dev)
                        return ret;
        }
 
-       ret = device_chld_unbind(dev);
+       ret = device_chld_unbind(dev, NULL);
        if (ret)
                return ret;
 
@@ -112,7 +104,7 @@ int device_unbind(struct udevice *dev)
 
        devres_release_all(dev);
 
-       if (dev->flags & DM_NAME_ALLOCED)
+       if (dev->flags & DM_FLAG_NAME_ALLOCED)
                free((char *)dev->name);
        free(dev);
 
@@ -151,7 +143,16 @@ void device_free(struct udevice *dev)
        devres_release_probe(dev);
 }
 
-int device_remove(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))))
+               return true;
+
+       return false;
+}
+
+int device_remove(struct udevice *dev, uint flags)
 {
        const struct driver *drv;
        int ret;
@@ -169,11 +170,15 @@ int device_remove(struct udevice *dev)
        if (ret)
                return ret;
 
-       ret = device_chld_remove(dev);
+       ret = device_chld_remove(dev, NULL, flags);
        if (ret)
                goto err;
 
-       if (drv->remove) {
+       /*
+        * Remove the device if called with the "normal" remove flag set,
+        * or if the remove flag matches any of the drivers remove flags
+        */
+       if (drv->remove && flags_remove(flags, drv->flags)) {
                ret = drv->remove(dev);
                if (ret)
                        goto err_remove;
@@ -187,10 +192,12 @@ int device_remove(struct udevice *dev)
                }
        }
 
-       device_free(dev);
+       if (flags_remove(flags, drv->flags)) {
+               device_free(dev);
 
-       dev->seq = -1;
-       dev->flags &= ~DM_FLAG_ACTIVATED;
+               dev->seq = -1;
+               dev->flags &= ~DM_FLAG_ACTIVATED;
+       }
 
        return ret;