Skip to contentSkip to conversation

mmc: report capacity for the selected partition

PATCH New Stephen Warren 2 participants 6 messages May 21, 2013 – May 22, 2013 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 mmc: report capacity for the selected partition

From: Stephen Warren <[email protected]>

Enhance the MMC core to calculate the size of each MMC partition, and update mmc->capacity whenever a partition is selected. This causes:

mmc dev 0 1 ; mmcinfo

... to report the size of the currently selected partition, rather than always reporting the size of the user partition.

---
 drivers/mmc/mmc.c |   68 +++++++++++++++++++++++++++++++++++++++++++++++------
 include/mmc.h     |    7 ++++++
 2 files changed, 68 insertions(+), 7 deletions(-)
drivers/mmc/mmc.c +61 -7
index f65a7b0..abeff35 100644
@@ -678,16 +678,49 @@ static int mmc_change_freq(struct mmc *mmc)
 	return 0;
 }
 
+static int mmc_set_capacity(struct mmc *mmc, int part_num)
+{
+	switch (part_num) {
+	case 0:
+		mmc->capacity = mmc->capacity_user;
+		break;
+	case 1:
+	case 2:
+		mmc->capacity = mmc->capacity_boot;
+		break;
+	case 3:
+		mmc->capacity = mmc->capacity_rpmb;
+		break;
+	case 4:
+	case 5:
+	case 6:
+	case 7:
+		mmc->capacity = mmc->capacity_gp[part_num - 4];
+		break;
+	default:
+		return -1;
+	}
+
+	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
+
+	return 0;
+}
+
 int mmc_switch_part(int dev_num, unsigned int part_num)
 {
 	struct mmc *mmc = find_mmc_device(dev_num);
+	int ret;
 
 	if (!mmc)
 		return -1;
 
-	return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
-			  (mmc->part_config & ~PART_ACCESS_MASK)
-			  | (part_num & PART_ACCESS_MASK));
+	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
+			 (mmc->part_config & ~PART_ACCESS_MASK)
+			 | (part_num & PART_ACCESS_MASK));
+	if (ret)
+		return ret;
+
+	return mmc_set_capacity(mmc, part_num);
 }
 
 int mmc_getcd(struct mmc *mmc)
@@ -895,7 +928,7 @@ static void mmc_set_bus_width(struct mmc *mmc, uint width)
 
 static int mmc_startup(struct mmc *mmc)
 {
-	int err;
+	int err, i;
 	uint mult, freq;
 	u64 cmult, csize, capacity;
 	struct mmc_cmd cmd;
@@ -1013,8 +1046,12 @@ static int mmc_startup(struct mmc *mmc)
 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
 	}
 
-	mmc->capacity = (csize + 1) << (cmult + 2);
-	mmc->capacity *= mmc->read_bl_len;
+	mmc->capacity_user = (csize + 1) << (cmult + 2);
+	mmc->capacity_user *= mmc->read_bl_len;
+	mmc->capacity_boot = 0;
+	mmc->capacity_rpmb = 0;
+	for (i = 0; i < 4; i++)
+		mmc->capacity_gp[i] = 0;
 
 	if (mmc->read_bl_len > 512)
 		mmc->read_bl_len = 512;
@@ -1053,7 +1090,7 @@ static int mmc_startup(struct mmc *mmc)
 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
 			capacity *= 512;
 			if ((capacity >> 20) > 2 * 1024)
-				mmc->capacity = capacity;
+				mmc->capacity_user = capacity;
 		}
 
 		switch (ext_csd[EXT_CSD_REV]) {
@@ -1094,8 +1131,25 @@ static int mmc_startup(struct mmc *mmc)
 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
 		    ext_csd[EXT_CSD_BOOT_MULT])
 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
+
+		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
+
+		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
+
+		for (i = 0; i < 4; i++) {
+			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
+			mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
+				(ext_csd[idx + 1] << 8) + ext_csd[idx];
+			mmc->capacity_gp[i] *=
+				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
+			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
+		}
 	}
 
+	err = mmc_set_capacity(mmc, mmc->part_num);
+	if (err)
+		return err;
+
 	if (IS_SD(mmc))
 		err = sd_change_freq(mmc);
 	else
include/mmc.h +7 -0
index f0d4820..cb3f0aa 100644
@@ -157,7 +157,9 @@
 /*
  * EXT_CSD fields
  */
+#define EXT_CSD_GP_SIZE_MULT		143	/* R/W */
 #define EXT_CSD_PARTITIONING_SUPPORT	160	/* RO */
+#define EXT_CSD_RPMB_MULT		168	/* RO */
 #define EXT_CSD_ERASE_GROUP_DEF		175	/* R/W */
 #define EXT_CSD_PART_CONF		179	/* R/W */
 #define EXT_CSD_BUS_WIDTH		183	/* R/W */
@@ -165,6 +167,7 @@
 #define EXT_CSD_REV			192	/* RO */
 #define EXT_CSD_CARD_TYPE		196	/* RO */
 #define EXT_CSD_SEC_CNT			212	/* RO, 4 bytes */
+#define EXT_CSD_HC_WP_GRP_SIZE		221	/* RO */
 #define EXT_CSD_HC_ERASE_GRP_SIZE	224	/* RO */
 #define EXT_CSD_BOOT_MULT		226	/* RO */
 
@@ -259,6 +262,10 @@ struct mmc {
 	uint write_bl_len;
 	uint erase_grp_size;
 	u64 capacity;
+	u64 capacity_user;
+	u64 capacity_boot;
+	u64 capacity_rpmb;
+	u64 capacity_gp[4];
 	block_dev_desc_t block_dev;
 	int (*send_cmd)(struct mmc *mmc,
 			struct mmc_cmd *cmd, struct mmc_data *data);
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 2/3 env_mmc: allow negative CONFIG_ENV_OFFSET

From: Stephen Warren <[email protected]>

A negative value of CONFIG_ENV_OFFSET is treated as a backwards offset from the end of the eMMC device/partition, rather than a forwards offset from the start.

This is useful when a single board may be stuffed with different eMMC devices, each of which has a different capacity, and you always want the environment to be stored at the very end of the device (or eMMC boot partition for example).

One example of this case is NVIDIA's Ventana reference board.

---
 common/env_mmc.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
common/env_mmc.c +5 -1
index 02bd5ae..4448085 100644
@@ -48,7 +48,11 @@ DECLARE_GLOBAL_DATA_PTR;
 
 __weak int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
 {
-	*env_addr = CONFIG_ENV_OFFSET;
+	if (CONFIG_ENV_OFFSET < 0)
+		*env_addr = mmc->capacity + CONFIG_ENV_OFFSET;
+	else
+		*env_addr = CONFIG_ENV_OFFSET;
+
 	return 0;
 }
 
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/3 env_mmc: allow negative CONFIG_ENV_OFFSET

Quoted 6 lines · Stephen Warren

From: Stephen Warren <[email protected]>

A negative value of CONFIG_ENV_OFFSET is treated as a backwards offset from the end of the eMMC device/partition, rather than a forwards offset from the start.

Quoted 8 lines

This is useful when a single board may be stuffed with different eMMC devices, each of which has a different capacity, and you always want the environment to be stored at the very end of the device (or eMMC boot partition for example).

One example of this case is NVIDIA's Ventana reference board.

NAK because you aren't also covering CONFIG_ENV_OFFSET_REDUND and you need to update the README as it says ENV_OFFFSET is from the beginning not end.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20130522/9988cf6a/attachment.pgp>
#3 lore raw

PATCH 2/3 env_mmc: allow negative CONFIG_ENV_OFFSET

Quoted 13 lines · Tom Rini
Quoted 11 lines · Stephen Warren

From: Stephen Warren <[email protected]>

A negative value of CONFIG_ENV_OFFSET is treated as a backwards offset from the end of the eMMC device/partition, rather than a forwards offset from the start.

This is useful when a single board may be stuffed with different eMMC devices, each of which has a different capacity, and you always want the environment to be stored at the very end of the device (or eMMC boot partition for example).

Quoted 7 lines
Quoted 3 lines

One example of this case is NVIDIA's Ventana reference board.

NAK because you aren't also covering CONFIG_ENV_OFFSET_REDUND and you need to update the README as it says ENV_OFFFSET is from the beginning not end.

env_mmc.c doesn't implement ENV_OFFSET_REDUND, and ENV_IS_IN_MMC isn't documented in the README (other config options like ENV_OFFSET are all documented relative to the ENV_IS_IN_xxx that defines their semantics in the README right now).

Are you saying you want me to fix those issues before this series will be accepted? I have no way to test ENV_OFFSET_REDUND, so I really wouldn't want to implement that for MMC, although I guess that I could add the ENV_IS_IN_MMC section to the README if you need.

#4 lore raw

PATCH 2/3 env_mmc: allow negative CONFIG_ENV_OFFSET

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 27 lines · Stephen Warren
Quoted 13 lines · Tom Rini
Quoted 11 lines · Stephen Warren

From: Stephen Warren <[email protected]>

A negative value of CONFIG_ENV_OFFSET is treated as a backwards offset from the end of the eMMC device/partition, rather than a forwards offset from the start.

This is useful when a single board may be stuffed with different eMMC devices, each of which has a different capacity, and you always want the environment to be stored at the very end of the device (or eMMC boot partition for example).

Quoted 7 lines
Quoted 3 lines

One example of this case is NVIDIA's Ventana reference board.

NAK because you aren't also covering CONFIG_ENV_OFFSET_REDUND and you need to update the README as it says ENV_OFFFSET is from the beginning not end.

env_mmc.c doesn't implement ENV_OFFSET_REDUND, and ENV_IS_IN_MMC isn't documented in the README (other config options like ENV_OFFSET are all documented relative to the ENV_IS_IN_xxx that defines their semantics in the README right now).

Quoted 5 lines

Are you saying you want me to fix those issues before this series will be accepted? I have no way to test ENV_OFFSET_REDUND, so I really wouldn't want to implement that for MMC, although I guess that I could add the ENV_IS_IN_MMC section to the README if you need.

CONFIG_ENV_OFFSET_REDUND is already there, but possibly not in the tegra tree yet? And yes, if you can write up the ENV_IS_IN_MMC section that slipped by before, I'd appreciate it. And you have no where to grab another 8KiB from? :(

- --
Tom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJRnO4hAAoJENk4IS6UOR1WV5oQAKpimzwQ6a9iwPbso+B/BuTi 2sSDODOVJsula8P0rkmA6u7BleJfWoM3AH3VCpBv4hGYCxMimQ6zsKjsGT6UmBER aHz0t1fLs492VfE2pLxR781rWU6A2M7U9nlsX3HFtVpBYkz6FPgXSP13zT2DW5pN Bs7oRljUKoiUvL/ISWOLH/TEcMxTf08cmdcsE6vPnqbYUffwedNFc1jDQIEd7POs cmCPtxi8tLtO1aZrLoqvD9bMKvx8Z8DvahJ5y53xJdhkVufQcTjfK6N1n+DIZrHN VFt+vhPaKejELdAt6zp0nOpg21yNlyW3x2SfloPoJdJN+29rzI8jbi3wKE38E7rK DkrUeifSYIttm5gy/icKNIFDCClz50mpa9RlLs7CUbkCJFByHg6vnyGIMwk6Ni3x Yg6utF1+89V2A8vp2Hov8BuB7Jx7JnLqyffd8KEhXe6Dn8h0gT1z319bhm9YScQQ P2p674ZU7O7SSwAZDNAAEI5MrQV+Wxlc9I5rvr0UC5hxGnoVpqf4JEDJOQ6zZYLO sNQCikoFArlYfEJtPdkvWbHmueDCqOhs7KO16mnANjhV9+9dJMNPCEvY/A+3OOD6 eIPaCBpWO4n2Xsw7Qm5zQcFcoBCspTiDPQDAHoDq9mAuYrtuxwdei5Mt1HMeHu7Q M6QYrPPnRX9taNlH6LN+
=a/cD
-----END PGP SIGNATURE-----

#5 lore raw

PATCH 3/3 ARM: tegra: make use of negative ENV_OFFSET on NVIDIA boards

In reply to Stephen Warren

From: Stephen Warren <[email protected]>

Use a negative value of CONFIG_ENV_OFFSET for all NVIDIA reference boards that store the U-Boot environment in the 2nd eMMC boot partition. This makes U-Boot agnostic to the size of the eMMC boot partition, which can vary depending on which eMMC device was actually stuffed into the board.

---
 include/configs/beaver.h   |    2 +-
 include/configs/cardhu.h   |    2 +-
 include/configs/dalmore.h  |    2 +-
 include/configs/paz00.h    |    2 +-
 include/configs/seaboard.h |    2 +-
 include/configs/ventana.h  |    2 +-
 include/configs/whistler.h |    4 ++--
 7 files changed, 8 insertions(+), 8 deletions(-)
include/configs/beaver.h +1 -1
index 058da4f..d51f5f8 100644
@@ -56,7 +56,7 @@
 
 /* Environment in eMMC, at the end of 2nd "boot sector" */
 #define CONFIG_ENV_IS_IN_MMC
-#define CONFIG_ENV_OFFSET		((1024 * 1024) - CONFIG_ENV_SIZE)
+#define CONFIG_ENV_OFFSET		(-CONFIG_ENV_SIZE)
 #define CONFIG_SYS_MMC_ENV_DEV		0
 #define CONFIG_SYS_MMC_ENV_PART		2
 
include/configs/cardhu.h +1 -1
index fd46083..142d20b 100644
@@ -55,7 +55,7 @@
 
 /* Environment in eMMC, at the end of 2nd "boot sector" */
 #define CONFIG_ENV_IS_IN_MMC
-#define CONFIG_ENV_OFFSET		((512 * 1024) - CONFIG_ENV_SIZE)
+#define CONFIG_ENV_OFFSET		(-CONFIG_ENV_SIZE)
 #define CONFIG_SYS_MMC_ENV_DEV		0
 #define CONFIG_SYS_MMC_ENV_PART		2
 
include/configs/dalmore.h +1 -1
index 2723843..b6e0161 100644
@@ -60,7 +60,7 @@
 #define CONFIG_ENV_IS_IN_MMC
 #define CONFIG_SYS_MMC_ENV_DEV		0
 #define CONFIG_SYS_MMC_ENV_PART		2
-#define CONFIG_ENV_OFFSET		((4096 * 1024) - CONFIG_ENV_SIZE)
+#define CONFIG_ENV_OFFSET		(-CONFIG_ENV_SIZE)
 
 #define MACH_TYPE_DALMORE	4304	/* not yet in mach-types.h */
 
include/configs/paz00.h +1 -1
index eac1ef9..9e2686a 100644
@@ -46,7 +46,7 @@
 
 /* Environment in eMMC, at the end of 2nd "boot sector" */
 #define CONFIG_ENV_IS_IN_MMC
-#define CONFIG_ENV_OFFSET ((1024 * 1024) - CONFIG_ENV_SIZE)
+#define CONFIG_ENV_OFFSET (-CONFIG_ENV_SIZE)
 #define CONFIG_SYS_MMC_ENV_DEV 0
 #define CONFIG_SYS_MMC_ENV_PART 2
 
include/configs/seaboard.h +1 -1
index f66173e..f0da1fc 100644
@@ -72,7 +72,7 @@
 
 /* Environment in eMMC, at the end of 2nd "boot sector" */
 #define CONFIG_ENV_IS_IN_MMC
-#define CONFIG_ENV_OFFSET ((512 * 1024) - CONFIG_ENV_SIZE)
+#define CONFIG_ENV_OFFSET (-CONFIG_ENV_SIZE)
 #define CONFIG_SYS_MMC_ENV_DEV 0
 #define CONFIG_SYS_MMC_ENV_PART 2
 
include/configs/ventana.h +1 -1
index 5755f11..41a7176 100644
@@ -52,7 +52,7 @@
 
 /* Environment in eMMC, at the end of 2nd "boot sector" */
 #define CONFIG_ENV_IS_IN_MMC
-#define CONFIG_ENV_OFFSET ((1024 * 1024) - CONFIG_ENV_SIZE)
+#define CONFIG_ENV_OFFSET (-CONFIG_ENV_SIZE)
 #define CONFIG_SYS_MMC_ENV_DEV 0
 #define CONFIG_SYS_MMC_ENV_PART 2
 
include/configs/whistler.h +2 -2
index 9542c7e..994edec 100644
@@ -61,12 +61,12 @@
 
 /*
  * Environment in eMMC, at the end of 2nd "boot sector". Note: This assumes
- * the user plugged the standard 8MB MoviNAND card into J29/HSMMC/POP. If
+ * the user plugged the standard 8GB MoviNAND card into J29/HSMMC/POP. If
  * they didn't, the boot sector layout may be different. However, use of that
  * particular card is standard practice as far as I know.
  */
 #define CONFIG_ENV_IS_IN_MMC
-#define CONFIG_ENV_OFFSET ((512 * 1024) - CONFIG_ENV_SIZE)
+#define CONFIG_ENV_OFFSET (-CONFIG_ENV_SIZE)
 #define CONFIG_SYS_MMC_ENV_DEV 0
 #define CONFIG_SYS_MMC_ENV_PART 2
 
Signature · 1 line
1.7.10.4