Merge "platform: msm_shared: Use erase timeout from card"
diff --git a/platform/msm_shared/include/mmc_sdhci.h b/platform/msm_shared/include/mmc_sdhci.h
index abbe64d..398f982 100644
--- a/platform/msm_shared/include/mmc_sdhci.h
+++ b/platform/msm_shared/include/mmc_sdhci.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
+/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -97,6 +97,7 @@
 #define MMC_PART_CONFIG                           179
 #define MMC_ERASE_GRP_DEF                         175
 #define MMC_USR_WP                                171
+#define MMC_ERASE_TIMEOUT_MULT                    223
 #define MMC_HC_ERASE_GRP_SIZE                     224
 
 /* Values for ext csd fields */
diff --git a/platform/msm_shared/include/sdhci.h b/platform/msm_shared/include/sdhci.h
index a14b3b5..a660e29 100644
--- a/platform/msm_shared/include/sdhci.h
+++ b/platform/msm_shared/include/sdhci.h
@@ -87,6 +87,7 @@
 	uint32_t trans_mode;    /* Transfer mode, read/write */
 	uint32_t cmd_retry;     /* Retry the command, if card is busy */
 	uint32_t cmd23_support; /* If card supports cmd23 */
+	uint64_t cmd_timeout;   /* Command timeout in ms */
 	struct mmc_data data;   /* Data pointer */
 };
 
diff --git a/platform/msm_shared/mmc_sdhci.c b/platform/msm_shared/mmc_sdhci.c
index 127505c..f0d51ac 100644
--- a/platform/msm_shared/mmc_sdhci.c
+++ b/platform/msm_shared/mmc_sdhci.c
@@ -1937,7 +1937,7 @@
 /*
  * Send the erase CMD38, to erase the selected erase groups
  */
-static uint32_t mmc_send_erase(struct mmc_device *dev)
+static uint32_t mmc_send_erase(struct mmc_device *dev, uint64_t erase_timeout)
 {
 	struct mmc_command cmd;
 	uint32_t status;
@@ -1949,6 +1949,7 @@
 	cmd.argument = 0x00000000;
 	cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
 	cmd.resp_type = SDHCI_CMD_RESP_R1B;
+	cmd.cmd_timeout = erase_timeout;
 
 	/* send command */
 	if (sdhci_send_command(&dev->host, &cmd))
@@ -1992,6 +1993,7 @@
 	uint32_t erase_end;
 	uint32_t blk_end;
 	uint32_t num_erase_grps;
+	uint64_t erase_timeout = 0;
 	uint32_t *out;
 	struct mmc_card *card;
 
@@ -2058,12 +2060,19 @@
 		return 1;
 	}
 
+	/*
+	 * As per emmc 4.5 spec section 7.4.27, calculate the erase timeout
+	 * erase_timeout = 300 * ERASE_TIMEOUT_MULT * num_erase_grps
+	 */
+	erase_timeout = (300 * card->ext_csd[MMC_ERASE_TIMEOUT_MULT] * num_erase_grps);
+
 	/* Send CMD38 to perform erase */
-	if (mmc_send_erase(dev))
+	if (mmc_send_erase(dev, erase_timeout))
 	{
 		dprintf(CRITICAL, "Failed to erase the specified partition\n");
 		return 1;
 	}
+
 	return 0;
 }
 
diff --git a/platform/msm_shared/sdhci.c b/platform/msm_shared/sdhci.c
index eeb47fc..a278dea 100644
--- a/platform/msm_shared/sdhci.c
+++ b/platform/msm_shared/sdhci.c
@@ -377,6 +377,7 @@
 	uint32_t int_status;
 	uint32_t trans_complete = 0;
 	uint32_t err_status;
+	uint64_t max_trans_retry = (cmd->cmd_timeout ? cmd->cmd_timeout : SDHCI_MAX_TRANS_RETRY);
 
 	do {
 		int_status = REG_READ16(host, SDHCI_NRML_INT_STS_REG);
@@ -449,7 +450,7 @@
 
 			retry++;
 			udelay(1000);
-			if (retry == SDHCI_MAX_TRANS_RETRY) {
+			if (retry == max_trans_retry) {
 				dprintf(CRITICAL, "Error: Transfer never completed\n");
 				ret = 1;
 				goto err;