From 26862b4a40c31b59618f7776ca06f0ed453cc380 Mon Sep 17 00:00:00 2001
From: Faiz Abbas <faiz_abbas@ti.com>
Date: Mon, 12 Feb 2018 19:24:31 +0530
Subject: [PATCH] env: mmc/fat/ext4: make sure that the MMC sub-system is
 initialized before using it

When booting from a non-MMC device, the MMC sub-system may not be
initialized when the environment is first accessed.
We need to make sure that the MMC sub-system is ready in even a non-MMC
boot case.

Therefore, initialize mmc before loading environment from it.

Signed-off-by: Faiz Abbas <faiz_abbas@ti.com>
---
 env/ext4.c | 3 +++
 env/fat.c  | 3 +++
 env/mmc.c  | 2 ++
 3 files changed, 8 insertions(+)

diff --git a/env/ext4.c b/env/ext4.c
index 3f3aac5737..6c69a0a57f 100644
--- a/env/ext4.c
+++ b/env/ext4.c
@@ -87,6 +87,9 @@ static int env_ext4_load(void)
 	int err;
 	loff_t off;
 
+	if (!strcmp(CONFIG_ENV_EXT4_INTERFACE, "mmc"))
+		mmc_initialize(NULL);
+
 	part = blk_get_device_part_str(CONFIG_ENV_EXT4_INTERFACE,
 				       CONFIG_ENV_EXT4_DEVICE_AND_PART,
 				       &dev_desc, &info, 1);
diff --git a/env/fat.c b/env/fat.c
index e6bd1f915d..19f260e881 100644
--- a/env/fat.c
+++ b/env/fat.c
@@ -89,6 +89,9 @@ static int env_fat_load(void)
 	int dev, part;
 	int err;
 
+	if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
+		mmc_initialize(NULL);
+
 	part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
 					CONFIG_ENV_FAT_DEVICE_AND_PART,
 					&dev_desc, &info, 1);
diff --git a/env/mmc.c b/env/mmc.c
index 1058b8c512..6f11deccb1 100644
--- a/env/mmc.c
+++ b/env/mmc.c
@@ -273,6 +273,8 @@ static int env_mmc_load(void)
 	ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env1, 1);
 	ALLOC_CACHE_ALIGN_BUFFER(env_t, tmp_env2, 1);
 
+	mmc_initialize(NULL);
+
 	mmc = find_mmc_device(dev);
 
 	errmsg = init_mmc_for_env(mmc);
-- 
2.39.5