[U-Boot] [PATCH v3] dm: core: device: switch off power domain after device removal

Anatolij Gustschin agust at denx.de
Wed Jul 31 21:31:23 UTC 2019


The power domain associated with a device is enabled when probing,
but currently the domain remains enabled when the device is removed.
Some boards started to disable power domains for selected devices
via custom board_quiesce_devices(), but it doesn't work in many
cases, i. e. because devices still can be accessed later in
.remove() callback on behalf of dm_remove_devices_flags().

Utilize the DM core to power off the device power domain, but add a
device flag to be able to selectively let the power domain enabled
after device removal. This might be required for devices that must
remain enabled when booting OS, i. e. serial console for debug
output, etc.

Signed-off-by: Anatolij Gustschin <agust at denx.de>
---
Changes in v3:
 - remove 'power-domain' device to fix dm power-domain test (make qcheck)
 - don't switch off the power domain for current console device

Changes in v2:
 - use CONFIG_IS_ENABLED(POWER_DOMAIN) to reduce code size

 drivers/core/device-remove.c | 18 ++++++++++++++++++
 include/dm/device.h          |  6 ++++++
 2 files changed, 24 insertions(+)

diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c
index 586fadee0a..5a0e48e182 100644
--- a/drivers/core/device-remove.c
+++ b/drivers/core/device-remove.c
@@ -16,6 +16,7 @@
 #include <dm/uclass.h>
 #include <dm/uclass-internal.h>
 #include <dm/util.h>
+#include <power-domain.h>
 
 int device_chld_unbind(struct udevice *dev, struct driver *drv)
 {
@@ -155,6 +156,7 @@ static bool flags_remove(uint flags, uint drv_flags)
 int device_remove(struct udevice *dev, uint flags)
 {
 	const struct driver *drv;
+	struct power_domain pd;
 	int ret;
 
 	if (!dev)
@@ -192,6 +194,22 @@ int device_remove(struct udevice *dev, uint flags)
 		}
 	}
 
+	if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
+	    device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN &&
+	    dev != gd->cur_serial_dev &&
+	    !(dev->flags & DM_FLAG_REMOVE_WITH_PD_ON)) {
+		if (!power_domain_get(dev, &pd)) {
+			power_domain_off(&pd);
+			/*
+			 * power_domain_get() bound the device, thus
+			 * we must remove it again to prevent unbinding
+			 * active devices (which would result in unbind
+			 * error).
+			 */
+			device_remove(pd.dev, DM_REMOVE_NORMAL);
+		}
+	}
+
 	if (flags_remove(flags, drv->flags)) {
 		device_free(dev);
 
diff --git a/include/dm/device.h b/include/dm/device.h
index 27a6d7b9fd..9a98a4a39e 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -61,6 +61,12 @@ struct driver_info;
  */
 #define DM_FLAG_OS_PREPARE		(1 << 10)
 
+/*
+ * Device is removed without switching off its power domain. This might
+ * be required, i. e. for serial console (debug) output when booting OS.
+ */
+#define DM_FLAG_REMOVE_WITH_PD_ON	(1 << 11)
+
 /*
  * One or multiple of these flags are passed to device_remove() so that
  * a selective device removal as specified by the remove-stage and the
-- 
2.17.1



More information about the U-Boot mailing list