Skip to contentSkip to conversation

mmc: detect illegal command response in SPI mode

PATCH In review Martí Alonso 3 participants 4 messages Jun 14 – Jul 7 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 mmc: detect illegal command response in SPI mode

At idle state, prior initialization, MMC cards (not SD) will respond with illegal command to anything other than CMD1 (MMC_SEND_OP_COND) and CMD58 (MMC_READ_OCR). The MMC driver initially sends CMD8 (SD_SEND_IF_COND) to check for an SD card, but this needs to return an error, if not an SD card, so it can later check for an MMC card with CMD1 (MMC_SEND_OP_COND).

Additionally, CMD55 + ACMD41 (SD_APP_SEND_OP_COND) can be skipped if CMD8 (SD_SEND_IF_COND) already failed.

---

 drivers/mmc/mmc.c     | 18 ++++++++++--------
 drivers/mmc/mmc_spi.c |  3 +++
 2 files changed, 13 insertions(+), 8 deletions(-)
drivers/mmc/mmc.c +10 -8
index f0e38efb262b..27eed9a76ba2 100644
@@ -3014,16 +3014,18 @@ retry:
 	/* Test for SD version 2 */
 	err = mmc_send_if_cond(mmc);
 
-	/* Now try to get the SD card's operating condition */
-	err = sd_send_op_cond(mmc, uhs_en);
-	if (err && uhs_en) {
-		uhs_en = false;
-		mmc_power_cycle(mmc);
-		goto retry;
+	if (!err) {
+		/* Now try to get the SD card's operating condition */
+		err = sd_send_op_cond(mmc, uhs_en);
+		if (err && uhs_en) {
+			uhs_en = false;
+			mmc_power_cycle(mmc);
+			goto retry;
+		}
 	}
 
-	/* If the command timed out, we check for an MMC card */
-	if (err == -ETIMEDOUT) {
+	/* If the command timed out or returned illegal, we check for an MMC card */
+	if (err == -ETIMEDOUT || err == -EOPNOTSUPP) {
 		err = mmc_send_op_cond(mmc);
 
 		if (err) {
drivers/mmc/mmc_spi.c +3 -0
index 675e642efd08..53bf4f583243 100644
@@ -168,6 +168,9 @@ static int mmc_spi_sendcmd(struct udevice *dev,
 			return -ETIMEDOUT;
 	}
 
+	if (resp[0] & R1_SPI_ILLEGAL_COMMAND)
+		return -EOPNOTSUPP;
+
 	debug("\n");
 
 	return 0;
Signature · 4 lines
2.43.0

base-commit: 5ca1a73c7d3064582498a8aa96c29e714402a6d3
branch: fix-mmc-no-sd
#1 lore raw

PATCH 2/2 mmc: handle MMC_SEND_EXT_CSD/SD_SEND_IF_COND collision in SPI mode

Both MMC and SD use index 8 for different commands, and expect different types of responses. MMC_CMD_SEND_EXT_CSD expects a data block of 512 bytes while SD_CMD_SEND_IF_COND expects an R7 response (R1 + 32 bits).

Differentiate them with the data parameter which tells us if the MMC driver expects a data block as a response.

---

 drivers/mmc/mmc_spi.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
drivers/mmc/mmc_spi.c +19 -10
index 53bf4f583243..f13ae8e6ebcd 100644
@@ -339,11 +339,16 @@ static int dm_mmc_spi_request(struct udevice *dev, struct mmc_cmd *cmd,
 		resp_size = sizeof(resp8);
 		cmd->cmdarg = 0x40000000;
 		break;
-	case SD_CMD_SEND_IF_COND:
-		resp = (u8 *)&resp40[0];
-		resp_size = sizeof(resp40);
-		resp_match = true;
-		resp_match_value = R1_SPI_IDLE;
+	case SD_CMD_SEND_IF_COND: // Also MMC_CMD_SEND_EXT_CSD
+		if (data) {
+			resp = &resp8;
+			resp_size = sizeof(resp8);
+		} else {
+			resp = (u8 *)&resp40[0];
+			resp_size = sizeof(resp40);
+			resp_match = true;
+			resp_match_value = R1_SPI_IDLE;
+		}
 		break;
 	case MMC_CMD_SPI_READ_OCR:
 		resp = (u8 *)&resp40[0];
@@ -396,12 +401,16 @@ static int dm_mmc_spi_request(struct udevice *dev, struct mmc_cmd *cmd,
 	case MMC_CMD_SEND_OP_COND:
 		cmd->response[0] = (resp8 & R1_SPI_IDLE) ? 0 : OCR_BUSY;
 		break;
-	case SD_CMD_SEND_IF_COND:
+	case SD_CMD_SEND_IF_COND: // Also MMC_CMD_SEND_EXT_CSD
 	case MMC_CMD_SPI_READ_OCR:
-		cmd->response[0] = resp40[4];
-		cmd->response[0] |= (uint)resp40[3] << 8;
-		cmd->response[0] |= (uint)resp40[2] << 16;
-		cmd->response[0] |= (uint)resp40[1] << 24;
+		if (data && cmd->cmdidx == MMC_CMD_SEND_EXT_CSD) {
+			cmd->response[0] = resp8;
+		} else {
+			cmd->response[0] = resp40[4];
+			cmd->response[0] |= (uint)resp40[3] << 8;
+			cmd->response[0] |= (uint)resp40[2] << 16;
+			cmd->response[0] |= (uint)resp40[1] << 24;
+		}
 		break;
 	case MMC_CMD_SEND_STATUS:
 		if (resp16[0] || resp16[1])
Signature · 4 lines
2.43.0

base-commit: 5ca1a73c7d3064582498a8aa96c29e714402a6d3
branch: fix-mmc-no-sd
#2 lore raw
In reply to Martí Alonso
Quoted 6 lines · Mart?? Alonso

At idle state, prior initialization, MMC cards (not SD) will respond with illegal command to anything other than CMD1 (MMC_SEND_OP_COND) and CMD58 (MMC_READ_OCR). The MMC driver initially sends CMD8 (SD_SEND_IF_COND) to check for an SD card, but this needs to return an error, if not an SD card, so it can later check for an MMC card with CMD1 (MMC_SEND_OP_COND).

CMD8 is supported in SD 2.0+, so this might break SD 1.0.

Quoted 4 lines

Additionally, CMD55 + ACMD41 (SD_APP_SEND_OP_COND) can be skipped if CMD8 (SD_SEND_IF_COND) already failed.

Regards
Peng

#3 lore raw
Quoted 12 lines · Peng Fan
Quoted 6 lines · Mart?? Alonso

At idle state, prior initialization, MMC cards (not SD) will respond with illegal command to anything other than CMD1 (MMC_SEND_OP_COND) and CMD58 (MMC_READ_OCR). The MMC driver initially sends CMD8 (SD_SEND_IF_COND) to check for an SD card, but this needs to return an error, if not an SD card, so it can later check for an MMC card with CMD1 (MMC_SEND_OP_COND).

CMD8 is supported in SD 2.0+, so this might break SD 1.0.

Quoted 3 lines

Additionally, CMD55 + ACMD41 (SD_APP_SEND_OP_COND) can be skipped if CMD8 (SD_SEND_IF_COND) already failed.

Peng is correct. CMD8 was introduced in SD spec v2.0, so SD v1.x cards return an illegal command response to it - identical to MMC. CMD8 failure alone cannot distinguish between SD v1.x and MMC.

ACMD41 is the correct discriminator: SD cards (v1.x and v2.0+) respond to it; MMC cards do not. Only an ACMD41 timeout means the card is not SD. The existing code handles this correctly by always attempting ACMD41 regardless of CMD8's result. See section 4.2.3 "Card Initialization and Identification" of the SD Physical Layer Simplified Specification.

Regards,
Tanmay Kathpalia