Skip to contentSkip to conversation

mmc: Unify and generalize UNSTUFF_BITS macro

PATCH In review Don Mullis 1 participant 4 messages Nov 6, 2009 MMC, SD and SDIO

lore
Share & tools
lore

Don Mullis

author
#0 lore raw

PATCH 1/3 mmc: Unify and generalize UNSTUFF_BITS macro

Unify duplicated implementations of UNSTUFF_BITS() into a shared .h file. Recode UNSTUFF_BITS() as an invocation of a new more general macro UNSTUFF_BITS_ARRAY() that works on arrays of any multiple of 32 bits.

Signed-off-by [email protected]
---
 drivers/mmc/core/core.h |   21 +++++++++++++++++++++
 drivers/mmc/core/mmc.c  |   14 --------------
 drivers/mmc/core/sd.c   |   14 --------------
 3 files changed, 21 insertions(+), 28 deletions(-)
drivers/mmc/core/sd.c +0 -14
Index: linux-2.6/drivers/mmc/core/sd.c
===================================================================
@@ -41,20 +41,6 @@ static const unsigned int tacc_mant[] =
 	35,	40,	45,	50,	55,	60,	70,	80,
 };
 
-#define UNSTUFF_BITS(resp,start,size)					\
-	({								\
-		const int __size = size;				\
-		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
-		const int __off = 3 - ((start) / 32);			\
-		const int __shft = (start) & 31;			\
-		u32 __res;						\
-									\
-		__res = resp[__off] >> __shft;				\
-		if (__size + __shft > 32)				\
-			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
-		__res & __mask;						\
-	})
-
 /*
  * Given the decoded CSD structure, decode the raw CID to our CID structure.
  */
drivers/mmc/core/core.h +21 -0
Index: linux-2.6/drivers/mmc/core/core.h
===================================================================
@@ -13,6 +13,27 @@
 
 #include <linux/delay.h>
 
+#define UNSTUFF_BITS_ARRAY(u32_array, start, size)			\
+	({								\
+		const int __size = size;				\
+		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
+		const int __off = (sizeof(u32_array)/4-1) - ((start) / 32); \
+		const int __shft = (start) & 31;			\
+		u32 __res;						\
+									\
+		__res = (u32_array)[__off] >> __shft;			\
+		if (__size + __shft > 32)				\
+			__res |= (u32_array)[__off-1]			\
+				<< ((32 - __shft) % 32); 		\
+		__res & __mask;						\
+	})
+
+/*
+ * Specific to 128-bit arrays, e.g. raw_cid[] and raw_csd[].
+ */
+#define UNSTUFF_BITS(resp, start, size)	\
+	UNSTUFF_BITS_ARRAY(*(u32(*)[4])resp, start, size)
+
 #define MMC_CMD_RETRIES        3
 
 struct mmc_bus_ops {
drivers/mmc/core/mmc.c +0 -14
Index: linux-2.6/drivers/mmc/core/mmc.c
===================================================================
@@ -39,20 +39,6 @@ static const unsigned int tacc_mant[] =
 	35,	40,	45,	50,	55,	60,	70,	80,
 };
 
-#define UNSTUFF_BITS(resp,start,size)					\
-	({								\
-		const int __size = size;				\
-		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
-		const int __off = 3 - ((start) / 32);			\
-		const int __shft = (start) & 31;			\
-		u32 __res;						\
-									\
-		__res = resp[__off] >> __shft;				\
-		if (__size + __shft > 32)				\
-			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
-		__res & __mask;						\
-	})
-
 /*
  * Given the decoded CSD structure, decode the raw CID to our CID structure.
  */
author
#1 lore raw

PATCH 2/3 mmc: Clean up mmc_decode_scr() implementation

In mmc_decode_scr(), back out coercion of 128-bit-oriented UNSTUFF_BITS() macro to work on 64-bit raw_scr field; invoke UNSTUFF_BITS_ARRAY() instead.

Tested by verifying that /sys output for scr register is unchanged.

Signed-off-by [email protected]
---
 drivers/mmc/core/sd.c |   10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/mmc/core/sd.c +3 -7
Index: linux-2.6/drivers/mmc/core/sd.c
===================================================================
@@ -163,20 +163,16 @@ static int mmc_decode_scr(struct mmc_car
 {
 	struct sd_scr *scr = &card->scr;
 	unsigned int scr_struct;
-	u32 resp[4];
 
-	resp[3] = card->raw_scr[1];
-	resp[2] = card->raw_scr[0];
-
-	scr_struct = UNSTUFF_BITS(resp, 60, 4);
+	scr_struct = UNSTUFF_BITS_ARRAY(card->raw_scr, 60, 4);
 	if (scr_struct != 0) {
 		printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
 			mmc_hostname(card->host), scr_struct);
 		return -EINVAL;
 	}
 
-	scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
-	scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
+	scr->sda_vsn =    UNSTUFF_BITS_ARRAY(card->raw_scr, 56, 4);
+	scr->bus_widths = UNSTUFF_BITS_ARRAY(card->raw_scr, 48, 4);
 
 	return 0;
 }
author
#2 lore raw

PATCH 3/3 mmc: Add SD_STATUS register dump to debugfs

In reply to Don Mullis

Add a file to debugfs to read back the SD_STATUS register from an SD card. SD_STATUS contains performance-related information.

Signed-off-by [email protected]
---
 drivers/mmc/core/debugfs.c |   56 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/core/sd_ops.c  |   51 ++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/core/sd_ops.h  |    1
 include/linux/mmc/sd.h     |    1
 4 files changed, 109 insertions(+)
drivers/mmc/core/sd_ops.c +51 -0
Index: linux-2.6/drivers/mmc/core/sd_ops.c
===================================================================
@@ -299,6 +299,57 @@ int mmc_app_send_scr(struct mmc_card *ca
 	return 0;
 }
 
+int mmc_send_sd_status(struct mmc_card *card, u32 *sd_status)
+{
+	int err;
+	struct mmc_request mrq;
+	struct mmc_command cmd;
+	struct mmc_data data;
+	struct scatterlist sg;
+	int i;
+
+	BUG_ON(!card);
+	BUG_ON(!card->host);
+	BUG_ON(!sd_status);
+
+	err = mmc_app_cmd(card->host, card);
+	if (err)
+		return err;
+
+	memset(&mrq, 0, sizeof(struct mmc_request));
+	memset(&cmd, 0, sizeof(struct mmc_command));
+	memset(&data, 0, sizeof(struct mmc_data));
+
+	mrq.cmd = &cmd;
+	mrq.data = &data;
+
+	cmd.opcode = SD_APP_SEND_SD_STATUS;
+	cmd.arg = 0;  /* not used */
+	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
+
+	data.blksz = 16*sizeof(u32);
+	data.blocks = 1;
+	data.flags = MMC_DATA_READ;
+	data.sg = &sg;
+	data.sg_len = 1;
+
+	sg_init_one(&sg, sd_status, 16*sizeof(u32));
+
+	mmc_set_data_timeout(&data, card);
+
+	mmc_wait_for_req(card->host, &mrq);
+
+	if (cmd.error)
+		return cmd.error;
+	if (data.error)
+		return data.error;
+
+	for (i = 0; i < 16; i++)
+		sd_status[i] = be32_to_cpu(sd_status[i]);
+
+	return 0;
+}
+
 int mmc_sd_switch(struct mmc_card *card, int mode, int group,
 	u8 value, u8 *resp)
 {
drivers/mmc/core/sd_ops.h +1 -0
Index: linux-2.6/drivers/mmc/core/sd_ops.h
===================================================================
@@ -13,6 +13,7 @@
 #define _MMC_SD_OPS_H
 
 int mmc_app_set_bus_width(struct mmc_card *card, int width);
+int mmc_send_sd_status(struct mmc_card *card, u32 *sd_status);
 int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr);
 int mmc_send_if_cond(struct mmc_host *host, u32 ocr);
 int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca);
include/linux/mmc/sd.h +1 -0
Index: linux-2.6/include/linux/mmc/sd.h
===================================================================
@@ -26,6 +26,7 @@
 #define SD_APP_SEND_NUM_WR_BLKS  22   /* adtc                    R1  */
 #define SD_APP_OP_COND           41   /* bcr  [31:0] OCR         R3  */
 #define SD_APP_SEND_SCR          51   /* adtc                    R1  */
+#define SD_APP_SEND_SD_STATUS    13   /* adtc                    R1  */
 
 /*
  * SD_SWITCH argument format:
drivers/mmc/core/debugfs.c +56 -0

74 lines not on this page · raw message