Skip to contentSkip to conversation

mmc-utils: Add softreset command for issuing CMD0

PATCH In review Christian Loehle 2 participants 2 messages Jul 15, 2022 – Jul 18, 2022 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc-utils: Add softreset command for issuing CMD0

CMD0 may be used to see if the hardware can handle a UHS card that completed the voltage switch. If a UHS card has problems coming back up after CMD0 your hardware may not support a hard reset properly.

---
 mmc.c      |  5 +++++
 mmc.h      |  3 +++
 mmc_cmds.c | 30 ++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 4 files changed, 39 insertions(+)
mmc.c +5 -0
index 6c56387..ba2c883 100644
@@ -245,6 +245,11 @@ static struct Command commands[] = {
 		"be 1.",
 	NULL
 	},
+	{ do_soft_reset, -1,
+	  "softreset", "<device>\n"
+	  "Issues a CMD0 softreset, e.g. for testing if hardware reset for UHS works\n\n",
+	  NULL
+	},
 	{ 0, 0, 0, 0 }
 };
 
mmc.h +3 -0
index daff62c..9796d2e 100644
@@ -21,6 +21,7 @@
 #include <linux/mmc/ioctl.h>
 
 /* From kernel linux/mmc/mmc.h */
+#define MMC_GO_IDLE_STATE         0   /* bc                          */
 #define MMC_SWITCH		6	/* ac	[31:0] See below	R1b */
 #define MMC_SEND_EXT_CSD	8	/* adtc				R1  */
 #define MMC_SEND_STATUS		13	/* ac   [31:16] RCA        R1  */
@@ -226,6 +227,7 @@
 
 
 /* From kernel linux/mmc/core.h */
+#define MMC_RSP_NONE	0			/* no response */
 #define MMC_RSP_PRESENT	(1 << 0)
 #define MMC_RSP_136	(1 << 1)		/* 136 bit response */
 #define MMC_RSP_CRC	(1 << 2)		/* expect valid crc */
@@ -234,6 +236,7 @@
 
 #define MMC_CMD_AC	(0 << 5)
 #define MMC_CMD_ADTC	(1 << 5)
+#define MMC_CMD_BC	(2 << 5)
 
 #define MMC_RSP_SPI_S1	(1 << 7)		/* one status byte */
 #define MMC_RSP_SPI_BUSY (1 << 10)		/* card may send busy */
mmc_cmds.c +30 -0
index 12b7802..c027cfa 100644
@@ -3039,3 +3039,33 @@ out:
 	close(dev_fd);
 	return ret;
 }
+
+int do_soft_reset(int nargs, char **argv)
+{
+	int fd;
+	char *device;
+	struct mmc_ioc_cmd idata;
+
+	if (nargs != 2) {
+		fprintf(stderr, "Usage: mmc status softreset </path/to/mmcblkX>\n");
+		exit(1);
+	}
+
+	device = argv[1];
+
+	fd = open(device, O_RDWR);
+	if (fd < 0) {
+		perror("open");
+		exit(1);
+	}
+
+	memset(&idata, 0, sizeof(idata));
+	idata.opcode = MMC_GO_IDLE_STATE;
+	idata.flags = MMC_RSP_NONE | MMC_CMD_BC;
+
+	/* No need to check for error, it is expected */
+	ioctl(fd, MMC_IOC_CMD, &idata);
+	close(fd);
+
+	return 0;
+}
mmc_cmds.h +1 -0
index 0f7c004..c112a95 100644
@@ -47,3 +47,4 @@ int do_read_cid(int argc, char **argv);
 int do_read_csd(int argc, char **argv);
 int do_erase(int nargs, char **argv);
 int do_general_cmd_read(int nargs, char **argv);
+int do_soft_reset(int nargs, char **argv);
Signature · 5 lines
2.36.1

Hyperstone GmbH | Reichenaustr. 39a  | 78467 Konstanz
Managing Director: Dr. Jan Peter Berns.
Commercial register of local courts: Freiburg HRB381782
#1 lore raw
Quoted 7 lines · Christian Loehle

CMD0 may be used to see if the hardware can handle a UHS card that completed the voltage switch. If a UHS card has problems coming back up after CMD0 your hardware may not support a hard reset properly.

Please resend and add Avri Altman <[email protected]> on the to line.

Kind regards
Uffe

Quoted 105 lines
---
 mmc.c      |  5 +++++
 mmc.h      |  3 +++
 mmc_cmds.c | 30 ++++++++++++++++++++++++++++++
 mmc_cmds.h |  1 +
 4 files changed, 39 insertions(+)
mmc.c +5 -0
index 6c56387..ba2c883 100644
@@ -245,6 +245,11 @@ static struct Command commands[] = {
                "be 1.",
        NULL
        },
+       { do_soft_reset, -1,
+         "softreset", "<device>\n"
+         "Issues a CMD0 softreset, e.g. for testing if hardware reset for UHS works\n\n",
+         NULL
+       },
        { 0, 0, 0, 0 }
 };
 
mmc.h +3 -0
index daff62c..9796d2e 100644
@@ -21,6 +21,7 @@
 #include <linux/mmc/ioctl.h>
 
 /* From kernel linux/mmc/mmc.h */
+#define MMC_GO_IDLE_STATE         0   /* bc                          */
 #define MMC_SWITCH             6       /* ac   [31:0] See below        R1b */
 #define MMC_SEND_EXT_CSD       8       /* adtc                         R1  */
 #define MMC_SEND_STATUS                13      /* ac   [31:16] RCA        R1  */
@@ -226,6 +227,7 @@
 
 
 /* From kernel linux/mmc/core.h */
+#define MMC_RSP_NONE   0                       /* no response */
 #define MMC_RSP_PRESENT        (1 << 0)
 #define MMC_RSP_136    (1 << 1)                /* 136 bit response */
 #define MMC_RSP_CRC    (1 << 2)                /* expect valid crc */
@@ -234,6 +236,7 @@
 
 #define MMC_CMD_AC     (0 << 5)
 #define MMC_CMD_ADTC   (1 << 5)
+#define MMC_CMD_BC     (2 << 5)
 
 #define MMC_RSP_SPI_S1 (1 << 7)                /* one status byte */
 #define MMC_RSP_SPI_BUSY (1 << 10)             /* card may send busy */
mmc_cmds.c +30 -0
index 12b7802..c027cfa 100644
@@ -3039,3 +3039,33 @@ out:
        close(dev_fd);
        return ret;
 }
+
+int do_soft_reset(int nargs, char **argv)
+{
+       int fd;
+       char *device;
+       struct mmc_ioc_cmd idata;
+
+       if (nargs != 2) {
+               fprintf(stderr, "Usage: mmc status softreset </path/to/mmcblkX>\n");
+               exit(1);
+       }
+
+       device = argv[1];
+
+       fd = open(device, O_RDWR);
+       if (fd < 0) {
+               perror("open");
+               exit(1);
+       }
+
+       memset(&idata, 0, sizeof(idata));
+       idata.opcode = MMC_GO_IDLE_STATE;
+       idata.flags = MMC_RSP_NONE | MMC_CMD_BC;
+
+       /* No need to check for error, it is expected */
+       ioctl(fd, MMC_IOC_CMD, &idata);
+       close(fd);
+
+       return 0;
+}
mmc_cmds.h +1 -1
index 0f7c004..c112a95 100644
@@ -47,3 +47,4 @@ int do_read_cid(int argc, char **argv);
 int do_read_csd(int argc, char **argv);
 int do_erase(int nargs, char **argv);
 int do_general_cmd_read(int nargs, char **argv);
+int do_soft_reset(int nargs, char **argv);
--

2.36.1

Hyperstone GmbH | Reichenaustr. 39a | 78467 Konstanz Managing Director: Dr. Jan Peter Berns. Commercial register of local courts: Freiburg HRB381782