platform: msm_shared: fix issue with tuning command

As of now we ignore CRC/INDEX command failures to tuning command
and still wait for data from card but in case the card did not
receive the command, it won't send the data. This is causing
software request timeout for tuning commands. Hence, software
must not ignore such cmd errors for tuning commands but end the
request immediately after resetting the controller for both CMD
and DATA. Also, wait for 146 MCLK cycles for card to send out the
data and thus to move to transfer state. Its corresponding phase must
also be considered as bad phase.

Change-Id: I18aef24fc344d0e474ce8414273fd043d0bde082
diff --git a/platform/msm_shared/sdhci.c b/platform/msm_shared/sdhci.c
index caaec78..72126bf 100644
--- a/platform/msm_shared/sdhci.c
+++ b/platform/msm_shared/sdhci.c
@@ -451,10 +451,11 @@
 		{
 			err_status = REG_READ16(host, SDHCI_ERR_INT_STS_REG);
 			if ((err_status & SDHCI_CMD_CRC_MASK) || (err_status & SDHCI_CMD_END_BIT_MASK)
-				|| err_status & SDHCI_CMD_TIMEOUT_MASK)
+				|| (err_status & SDHCI_CMD_TIMEOUT_MASK)
+				|| (err_status & SDHCI_CMD_IDX_MASK))
 			{
 				sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
-				return 0;
+				return 1;
 			}
 		}
 
@@ -523,7 +524,7 @@
 				if ((err_status & SDHCI_DAT_TIMEOUT_MASK) || (err_status & SDHCI_DAT_CRC_MASK))
 				{
 					sdhci_reset(host, (SOFT_RESET_CMD | SOFT_RESET_DATA));
-					return 0;
+					return 1;
 				}
 			}
 
@@ -878,10 +879,6 @@
 	/* Write the command register */
 	REG_WRITE16(host, SDHCI_PREP_CMD(cmd->cmd_index, flags), SDHCI_CMD_REG);
 
-#if USE_TARGET_HS200_DELAY
-	udelay(1000);
-#endif
-
 	/* Command complete sequence */
 	if (sdhci_cmd_complete(host, cmd))
 	{
diff --git a/platform/msm_shared/sdhci_msm.c b/platform/msm_shared/sdhci_msm.c
index 47bdfd1..f4c5c8c 100644
--- a/platform/msm_shared/sdhci_msm.c
+++ b/platform/msm_shared/sdhci_msm.c
@@ -714,6 +714,7 @@
 	bool drv_type_changed = false;
 	int ret = 0;
 	uint32_t i;
+	uint32_t err = 0;
 	struct sdhci_msm_data *msm_host;
 
 	msm_host = host->msm_host;
@@ -756,6 +757,9 @@
 	tuned_phase_cnt = 0;
 	phase = 0;
 	struct mmc_command cmd = {0};
+	struct mmc_command sts_cmd = {0};
+	uint32_t sts_retry;
+	uint32_t sts_err;
 
 	while (phase < MAX_PHASES)
 	{
@@ -777,7 +781,30 @@
 		cmd.data.num_blocks = 0x1;
 
 		/* send command */
-		if (!sdhci_send_command(host, &cmd) && !memcmp(tuning_data, tuning_block, size))
+		err = sdhci_send_command(host, &cmd);
+		if(err)
+		{
+
+			sts_retry = 100;
+			sts_cmd.cmd_index = CMD13_SEND_STATUS;
+			sts_cmd.argument = card->rca << 16;
+			sts_cmd.cmd_type = SDHCI_CMD_TYPE_NORMAL;
+			sts_cmd.resp_type = SDHCI_CMD_RESP_R1;
+			while(sts_retry)
+			{
+				sts_err = sdhci_send_command(host, &sts_cmd);
+				DBG(" response is %d err is %d phase is %d\n",sts_cmd.resp[0],sts_err, phase);
+				if( sts_err || (MMC_CARD_STATUS(sts_cmd.resp[0]) != MMC_TRAN_STATE) )
+				{
+					udelay(10);
+					sts_retry--;
+					continue;
+				}
+				break;
+			}
+		}
+
+		if (!err && !memcmp(tuning_data, tuning_block, size))
 				tuned_phases[tuned_phase_cnt++] = phase;
 
 		phase++;