Add REBOOT_MODE_ENV_UPDATE Kconfig option and implement
automatic reboot-mode env variable update at last stage init.

When enabled, the reboot-mode uclass registers an
EVT_LAST_STAGE_INIT event handler that probes the first
reboot-mode device and calls dm_reboot_mode_update() to set
the reboot-mode environment variable.

EVT_LAST_STAGE_INIT fires after the environment is fully
initialized.

Signed-off-by: Aswin Murugan <[email protected]>
---
 drivers/reboot-mode/Kconfig              |  8 +++++++
 drivers/reboot-mode/reboot-mode-uclass.c | 29 ++++++++++++++++++++++--
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/drivers/reboot-mode/Kconfig b/drivers/reboot-mode/Kconfig
index 72b33d71223..6e8825f1e67 100644
--- a/drivers/reboot-mode/Kconfig
+++ b/drivers/reboot-mode/Kconfig
@@ -39,4 +39,12 @@ config REBOOT_MODE_NVMEM
          Use any kind of non-volatile memory (EEPROM, RTC, etc) to control the
          reboot mode.
 
+config REBOOT_MODE_ENV_UPDATE
+       bool "Automatically update reboot-mode env variable on boot"
+       depends on DM_REBOOT_MODE
+       help
+         When enabled, the reboot-mode uclass will automatically call
+         dm_reboot_mode_update() on the first reboot-mode device at last
+         stage init.
+
 endmenu
diff --git a/drivers/reboot-mode/reboot-mode-uclass.c 
b/drivers/reboot-mode/reboot-mode-uclass.c
index 7cbe02eb4ed..976544d2810 100644
--- a/drivers/reboot-mode/reboot-mode-uclass.c
+++ b/drivers/reboot-mode/reboot-mode-uclass.c
@@ -6,6 +6,7 @@
 #include <dm.h>
 #include <dm/device_compat.h>
 #include <dm/devres.h>
+#include <event.h>
 #include <exports.h>
 #include <reboot-mode/reboot-mode.h>
 
@@ -116,9 +117,33 @@ int dm_reboot_mode_pre_probe(struct udevice *dev)
        return 0;
 }
 
+/*
+ * reboot_mode_last_stage_init() - Update reboot-mode env variable at last
+ * stage init.
+ *
+ * Called via EVT_LAST_STAGE_INIT, which fires after the environment is fully
+ * initialized.
+ *
+ */
+static int reboot_mode_last_stage_init(void)
+{
+       struct udevice *dev;
+       int ret;
+
+       if (!CONFIG_IS_ENABLED(REBOOT_MODE_ENV_UPDATE))
+               return 0;
+
+       ret = uclass_first_device_err(UCLASS_REBOOT_MODE, &dev);
+       if (ret)
+               return 0;
+
+       return dm_reboot_mode_update(dev);
+}
+EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, reboot_mode_last_stage_init);
+
 UCLASS_DRIVER(reboot_mode) = {
-       .name   = "reboot-mode",
-       .id     = UCLASS_REBOOT_MODE,
+       .name           = "reboot-mode",
+       .id             = UCLASS_REBOOT_MODE,
        .pre_probe      = dm_reboot_mode_pre_probe,
        .per_device_plat_auto =
                sizeof(struct reboot_mode_uclass_platdata),
-- 
2.34.1

Reply via email to