From 113cc9875d10589a5b03c122ddba62c568c675bf Mon Sep 17 00:00:00 2001
From: Cao Da Shi <dscao999@hotmail.com, dscao999@gmail.com>
Date: Fri, 6 Nov 2015 13:54:39 +0800
Subject: [PATCH] FAT boot and boot pin message

---
 arch/arm/cpu/armv7/am33xx/board.c      |  9 +++++++++
 common/env_fat.c                       |  9 +++++++--
 configs/am335x_boneblack_fat_defconfig | 16 ++++++++++++++++
 drivers/mmc/mmc.c                      |  6 ++++++
 include/common.h                       |  1 +
 include/configs/am335x_evm.h           |  8 ++++++++
 6 files changed, 47 insertions(+), 2 deletions(-)
 create mode 100644 configs/am335x_boneblack_fat_defconfig

diff --git a/arch/arm/cpu/armv7/am33xx/board.c b/arch/arm/cpu/armv7/am33xx/board.c
index bd14326..d661ff6 100644
--- a/arch/arm/cpu/armv7/am33xx/board.c
+++ b/arch/arm/cpu/armv7/am33xx/board.c
@@ -90,6 +90,15 @@ const struct gpio_bank *const omap_gpio_bank = gpio_bank_am33xx;
 int cpu_mmc_init(bd_t *bis)
 {
 	int ret;
+	unsigned int ctl_status, *ctl_reg;
+
+	ctl_reg = (unsigned int *)(CTRL_BASE + 0x40);
+	ctl_status = *ctl_reg;
+	ctl_status &= 0x0f;
+	if (ctl_status == 0x08)
+		boot_mmc = 0;
+	else if (ctl_status == 0x0c)
+		boot_mmc = 1;
 
 	ret = omap_mmc_init(0, 0, 0, -1, -1);
 	if (ret)
diff --git a/common/env_fat.c b/common/env_fat.c
index d79d864..81be952 100644
--- a/common/env_fat.c
+++ b/common/env_fat.c
@@ -23,6 +23,9 @@ char *env_name_spec = "FAT";
 
 env_t *env_ptr;
 
+int boot_mmc = -1;
+static char fatdev[8] = FAT_ENV_DEVICE_AND_PART;
+
 DECLARE_GLOBAL_DATA_PTR;
 
 int env_init(void)
@@ -49,7 +52,7 @@ int saveenv(void)
 		return err;
 
 	part = get_device_and_partition(FAT_ENV_INTERFACE,
-					FAT_ENV_DEVICE_AND_PART,
+					fatdev,
 					&dev_desc, &info, 1);
 	if (part < 0)
 		return 1;
@@ -82,8 +85,10 @@ void env_relocate_spec(void)
 	int dev, part;
 	int err;
 
+	if (boot_mmc != -1)
+		fatdev[0] = '0' + boot_mmc;
 	part = get_device_and_partition(FAT_ENV_INTERFACE,
-					FAT_ENV_DEVICE_AND_PART,
+					fatdev,
 					&dev_desc, &info, 1);
 	if (part < 0)
 		goto err_env_relocate;
diff --git a/configs/am335x_boneblack_fat_defconfig b/configs/am335x_boneblack_fat_defconfig
new file mode 100644
index 0000000..8eff83f
--- /dev/null
+++ b/configs/am335x_boneblack_fat_defconfig
@@ -0,0 +1,16 @@
+CONFIG_ARM=y
+CONFIG_TARGET_AM335X_EVM=y
+CONFIG_DEFAULT_DEVICE_TREE="am335x-boneblack"
+CONFIG_SPL=y
+CONFIG_SPL_STACK_R=y
+CONFIG_SPL_STACK_R_ADDR=0x82000000
+CONFIG_FIT=y
+CONFIG_FIT_VERBOSE=y
+CONFIG_FIT_SIGNATURE=y
+#CONFIG_SYS_EXTRA_OPTIONS="SERIAL1,CONS_INDEX=1,EMMC_FAT_BOOT"
+CONFIG_SYS_EXTRA_OPTIONS="EMMC_FAT_BOOT,ENABLE_VBOOT"
+# CONFIG_CMD_IMLS is not set
+# CONFIG_CMD_FLASH is not set
+# CONFIG_CMD_SETEXPR is not set
+CONFIG_OF_CONTROL=y
+CONFIG_SPI_FLASH=y
diff --git a/drivers/mmc/mmc.c b/drivers/mmc/mmc.c
index 2a58702..81cb1c2 100644
--- a/drivers/mmc/mmc.c
+++ b/drivers/mmc/mmc.c
@@ -1730,6 +1730,12 @@ void print_mmc_devices(char separator)
 	}
 
 	printf("\n");
+	printf("Booted from: ");
+	list_for_each(entry,&mmc_devices) {
+		m = list_entry(entry, struct mmc, link);
+		if (m->block_dev.dev == boot_mmc)
+			printf("%s: %d\n", m->cfg->name, boot_mmc);
+	}
 }
 
 #else
diff --git a/include/common.h b/include/common.h
index ecb1f06..f30b2b9 100644
--- a/include/common.h
+++ b/include/common.h
@@ -338,6 +338,7 @@ int	source (ulong addr, const char *fit_uname);
 extern ulong load_addr;		/* Default Load Address */
 extern ulong save_addr;		/* Default Save Address */
 extern ulong save_size;		/* Default Save Size */
+extern int boot_mmc; 		/* mmc interface boot from */
 
 /* common/cmd_doc.c */
 void	doc_probe(unsigned long physadr);
diff --git a/include/configs/am335x_evm.h b/include/configs/am335x_evm.h
index 3cf768e..95c2e2a 100644
--- a/include/configs/am335x_evm.h
+++ b/include/configs/am335x_evm.h
@@ -420,6 +420,14 @@
 #define CONFIG_ENV_OFFSET		0x0
 #define CONFIG_ENV_OFFSET_REDUND	(CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE)
 #define CONFIG_SYS_REDUNDAND_ENVIRONMENT
+
+#elif defined(CONFIG_EMMC_FAT_BOOT)
+#undef CONFIG_ENV_IS_NOWHERE
+#undef CONFIG_SPL_OS_BOOT
+#define CONFIG_ENV_IS_IN_FAT
+#define FAT_ENV_INTERFACE               "mmc"
+#define FAT_ENV_DEVICE_AND_PART         "0:1"
+#define FAT_ENV_FILE                    "ubtEnv.txt"
 #endif
 
 /* SPI flash. */
-- 
2.1.4

