UPSTREAM: mmc-utils: Add command to set the boot bus conditions

This was added because some eMMC devices had the boot bus conditions
set incorrectly causing them to hang on boot.

Signed-off-by: Al Cooper <acooperx@gmail.com>
Signed-off-by: Chris Ball <chris@printf.net>
(cherry picked from commit 64c2de8b1476c42ef9e9729b7ca0e436b5d90170)
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>

Conflicts:
	mmc_cmds.c

BUG=none
TEST=Compiled, Installed on gnawty

Change-Id: I8177ac4f82562b6895ea2858db2973c82f88657b
Reviewed-on: https://chromium-review.googlesource.com/338081
Commit-Ready: Gwendal Grignou <gwendal@chromium.org>
Tested-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-by: Grant Grundler <grundler@chromium.org>
diff --git a/mmc_cmds.c b/mmc_cmds.c
index 3a34bd4..420b18e 100644
--- a/mmc_cmds.c
+++ b/mmc_cmds.c
@@ -311,6 +311,75 @@
 	return ret;
 }
 
+int do_boot_bus_conditions_set(int nargs, char **argv)
+{
+	__u8 ext_csd[512];
+	__u8 value = 0;
+	int fd, ret;
+	char *device;
+
+	CHECK(nargs != 5, "Usage: mmc: bootbus set <boot_mode> "
+	      "<reset_boot_bus_conditions> <boot_bus_width> <device>\n",
+		exit(1));
+
+	if (strcmp(argv[1], "single_backward") == 0)
+		value |= 0;
+	else if (strcmp(argv[1], "single_hs") == 0)
+		value |= 0x8;
+	else if (strcmp(argv[1], "dual") == 0)
+		value |= 0x10;
+	else {
+		fprintf(stderr, "illegal <boot_mode> specified\n");
+		exit(1);
+	}
+
+	if (strcmp(argv[2], "x1") == 0)
+		value |= 0;
+	else if (strcmp(argv[2], "retain") == 0)
+		value |= 0x4;
+	else {
+		fprintf(stderr,
+			"illegal <reset_boot_bus_conditions> specified\n");
+		exit(1);
+	}
+
+	if (strcmp(argv[3], "x1") == 0)
+		value |= 0;
+	else if (strcmp(argv[3], "x4") == 0)
+		value |= 0x1;
+	else if (strcmp(argv[3], "x8") == 0)
+		value |= 0x2;
+	else {
+		fprintf(stderr,	"illegal <boot_bus_width> specified\n");
+		exit(1);
+	}
+
+	device = argv[4];
+	fd = open(device, O_RDWR);
+	if (fd < 0) {
+		perror("open");
+		exit(1);
+	}
+
+	ret = read_extcsd(fd, ext_csd);
+	if (ret) {
+		fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
+		exit(1);
+	}
+	printf("Changing ext_csd[BOOT_BUS_CONDITIONS] from 0x%02x to 0x%02x\n",
+		ext_csd[EXT_CSD_BOOT_BUS_CONDITIONS], value);
+
+	ret = write_extcsd_value(fd, EXT_CSD_BOOT_BUS_CONDITIONS, value);
+	if (ret) {
+		fprintf(stderr, "Could not write 0x%02x to "
+			"EXT_CSD[%d] in %s\n",
+			value, EXT_CSD_BOOT_BUS_CONDITIONS, device);
+		exit(1);
+	}
+	close(fd);
+	return ret;
+}
+
 int do_hwreset(int value, int nargs, char **argv)
 {
 	__u8 ext_csd[EXT_CSD_SIZE];