mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 0/9] mci: core: add CID as parameters and fix parsing
@ 2024-05-28 15:39 Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 1/9] mci: core: rename mtd to mdt Stefan Kerkmann
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

This series adds the fields of the CID register found in MMC and SD
cards as parameters to the device. The intention is to use these
parameters in scripting e.g. for safety checks before applying
potentially destructive changes.

During the implementation I found several bugs in the implementation of
the CID parsing that usually stem from not discerning between MMC and
SD cards.

For a proof of work I have attached these outputs of both an internal
eMMC and SD card on an Phyboard Polis imx8mm device. The first output is
mmc-utils and the second barebox at the state of this patch series:

eMMC:

root@phyboard-polis-imx8mm-4:~ mmc cid read /sys/class/block/mmcblk2/device -v
======MMC/CID======
        MID: 0x13 (Micron)
        CBX: 0x1 (BGA)
        OID: 0x4e
        PNM: Q2J55L
        PRV: 0x10 (1.0)
        PSN: 0x13723b3f
        MDT: 0x93 2000 oct <-- This is likely a bug in mmc-utils
        CRC: 0x00

barebox@PHYTEC phyBOARD-Polis-i.MX8MM RDK: devinfo mmc2
Card information:
<snip>
  Manufacturer ID: 0x13
  OEM/Application ID: 0x4E
  CBX: 1
  Product name: 'Q2J55L'
  Product revision: 1.0
  Serial no: 326253375
  Manufacturing date: 2022.3
</snip>
Parameters:
<snip>
  cid_cbx: 1 (type: uint32)
  cid_mdt: 2022.3 (type: string)
  cid_mid: 0x13 (type: uint32)
  cid_oid: 0x4E (type: string)
  cid_pnm: Q2J55L (type: string)
  cid_prv: 1.0 (type: string)
  cid_psn: 326253375 (type: uint32)
</snip>

SD card:

root@phyboard-polis-imx8mm-4:~ mmc cid read /sys/class/block/mmcblk1/device -v
======SD/CID======
        MID: 0x63 (Unlisted)
        OID: C`
        PNM: CACTU
        PRV: 0x02 (0.2)
        PSN: 0x583010d2
        MDT: 0x158 2021 sep
        CRC: 0x00

barebox@PHYTEC phyBOARD-Polis-i.MX8MM RDK:/ devinfo mmc1
Card information:
<snip>
  Manufacturer ID: 0x63
  OEM/Application ID: C`
  Product name: 'CACTU'
  Product revision: 0.2
  Serial no: 2139062282
  Manufacturing date: 2021.8
</snip>
Parameters:
<snip>
  cid_mdt: 2021.8 (type: string)
  cid_mid: 0x63 (type: uint32)
  cid_oid: C` (type: string)
  cid_pnm: CACTU (type: string)
  cid_prv: 0.2 (type: string)
  cid_psn: 2139062282 (type: uint32)
</snip>

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
Stefan Kerkmann (9):
      mci: core: rename mtd to mdt
      mci: core: add cbx extraction function
      mci: core: add product name extraction function
      mci: core: add manufacturing date extraction function
      mci: core: parse cid into parameters
      mci: core: fix extract_prv and write to string buffer
      mci: core: add CBX field as parameter and print it
      mci: core: fix extract_oid and write to string buffer
      mci: core: add missing slice range for extract_psn function

 drivers/mci/mci-core.c | 119 ++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 98 insertions(+), 21 deletions(-)
---
base-commit: 80d7c9d3d80e75e2335a123d414fd7fe83508291
change-id: 20240528-feature-mmc-cid-as-parameters-685158ffc027

Best regards,
-- 
Stefan Kerkmann <s.kerkmann@pengutronix.de>




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 1/9] mci: core: rename mtd to mdt
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 2/9] mci: core: add cbx extraction function Stefan Kerkmann
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

The field MDT (manufacturing date) was written with a small typo back in
commit 7051227d2e68ad67078b9e7afb15ad5826161686, this commit fixes it.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index b4139be11e..07ac7dd512 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1962,9 +1962,9 @@ static unsigned extract_psn(struct mci *mci)
  * Extract the month of the manufacturing date from the CID
  * @param mci Instance data
  *
- * The 'MTD' is encoded in bit 19:8 in the CID, month in 11:8
+ * The 'MDT' is encoded in bit 19:8 in the CID, month in 11:8
  */
-static unsigned extract_mtd_month(struct mci *mci)
+static unsigned extract_mdt_month(struct mci *mci)
 {
 	if (IS_SD(mci))
 		return UNSTUFF_BITS(mci->cid, 8, 4);
@@ -1976,10 +1976,10 @@ static unsigned extract_mtd_month(struct mci *mci)
  * Extract the year of the manufacturing date from the CID
  * @param mci Instance data
  *
- * The 'MTD' is encoded in bit 19:8 in the CID, year in 19:12
+ * The 'MDT' is encoded in bit 19:8 in the CID, year in 19:12
  * An encoded 0 means the year 2000
  */
-static unsigned extract_mtd_year(struct mci *mci)
+static unsigned extract_mdt_year(struct mci *mci)
 {
 	unsigned year;
 	if (IS_SD(mci))
@@ -2077,8 +2077,8 @@ static void mci_info(struct device *dev)
 	printf("  Product revision: %u.%u\n", extract_prv(mci) >> 4,
 		extract_prv(mci) & 0xf);
 	printf("  Serial no: %0u\n", extract_psn(mci));
-	printf("  Manufacturing date: %u.%u\n", extract_mtd_month(mci),
-		extract_mtd_year(mci));
+	printf("  Manufacturing date: %u.%u\n", extract_mdt_month(mci),
+		extract_mdt_year(mci));
 }
 
 /**

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 2/9] mci: core: add cbx extraction function
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 1/9] mci: core: rename mtd to mdt Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 3/9] mci: core: add product name " Stefan Kerkmann
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

The CBX field is only present for eMMC and designates the type:

00 - Device (Removable)
01 - BGA (Discrete embedded)
10 - POP
11 - Reserved

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 07ac7dd512..2a71fdd39a 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1917,6 +1917,17 @@ static unsigned extract_mid(struct mci *mci)
 		return UNSTUFF_BITS(mci->cid, 120, 8);
 }
 
+/**
+ * Extract the CBX from the CID
+ * @param mci Instance data
+ *
+ * The 'CBX' is encoded in bit 113:112 in the CID and only present in MMC cards
+ */
+static unsigned extract_cbx(struct mci *mci)
+{
+	return UNSTUFF_BITS(mci->cid, 112, 2);
+}
+
 /**
  * Extract the OEM/Application ID from the CID
  * @param mci Instance data

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 3/9] mci: core: add product name extraction function
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 1/9] mci: core: rename mtd to mdt Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 2/9] mci: core: add cbx extraction function Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 4/9] mci: core: add manufacturing date " Stefan Kerkmann
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

The product name field PNM is available for both SD cards (len=5) and
MMC cards (len=6). The currently used method doesn't respect the length
for MMC cards.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 2a71fdd39a..df72445edd 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1939,6 +1939,31 @@ static unsigned extract_oid(struct mci *mci)
 	return (mci->cid[0] >> 8) & 0xffff;
 }
 
+/**
+ * Extract the product name from the CID
+ * @param mci Instance data
+ *
+ * The 'PNM' is encoded in bit 103:64 in the CID for SD cards and 103:56 for
+ * MMC cards
+ */
+static void extract_pnm(struct mci *mci, char pnm[static 7])
+{
+	pnm[0] = UNSTUFF_BITS(mci->cid, 96, 8);
+	pnm[1] = UNSTUFF_BITS(mci->cid, 88, 8);
+	pnm[2] = UNSTUFF_BITS(mci->cid, 80, 8);
+	pnm[3] = UNSTUFF_BITS(mci->cid, 72, 8);
+	pnm[4] = UNSTUFF_BITS(mci->cid, 64, 8);
+
+	if (IS_SD(mci)) {
+		// SD cards have a 5 character long product name
+		pnm[5] = '\0';
+	} else {
+		// MMC cards have a 6 character long product name
+		pnm[5] = UNSTUFF_BITS(mci->cid, 56, 8);
+		pnm[6] = '\0';
+	}
+}
+
 /**
  * Extract the product revision from the CID
  * @param mci Instance data

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 4/9] mci: core: add manufacturing date extraction function
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (2 preceding siblings ...)
  2024-05-28 15:39 ` [PATCH 3/9] mci: core: add product name " Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 5/9] mci: core: parse cid into parameters Stefan Kerkmann
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

This function operates on a string buffer and concatenates both the year
and month. It will be used later when the mdt is added as a parameter.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index df72445edd..48eee35f2c 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -2030,6 +2030,20 @@ static unsigned extract_mdt_year(struct mci *mci)
 	return year;
 }
 
+/**
+ * Extract the manufacturing date from the CID
+ * @param mci Instance data
+ *
+ * The 'MDT' is encoded in bit 19:8 in the CID
+ */
+static void extract_mdt(struct mci *mci, char mdt[static 8])
+{
+	unsigned month = extract_mdt_month(mci);
+	unsigned year = extract_mdt_year(mci);
+
+	snprintf(mdt, 8, "%u.%u", year, month);
+}
+
 static const char *mci_timing_tostr(unsigned timing)
 {
 	switch (timing) {

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 5/9] mci: core: parse cid into parameters
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (3 preceding siblings ...)
  2024-05-28 15:39 ` [PATCH 4/9] mci: core: add manufacturing date " Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 6/9] mci: core: fix extract_prv and write to string buffer Stefan Kerkmann
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

To make the MMC/SD CID information accessible for scripting all
available CID fields are added as parameters during device probing. As
some fields have different data types for MMC and SD cards a string
representation is chosen these cases.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 48eee35f2c..d1c425dda6 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -2119,16 +2119,29 @@ static void mci_info(struct device *dev)
 		mci->csd[2], mci->csd[3]);
 	printf("  Max. transfer speed: %u Hz\n", mci->tran_speed);
 	mci_print_caps(mci->card_caps);
-	printf("  Manufacturer ID: 0x%02X\n", extract_mid(mci));
-	printf("  OEM/Application ID: 0x%04X\n", extract_oid(mci));
-	printf("  Product name: '%c%c%c%c%c'\n", mci->cid[0] & 0xff,
-		(mci->cid[1] >> 24), (mci->cid[1] >> 16) & 0xff,
-		(mci->cid[1] >> 8) & 0xff, mci->cid[1] & 0xff);
-	printf("  Product revision: %u.%u\n", extract_prv(mci) >> 4,
-		extract_prv(mci) & 0xf);
-	printf("  Serial no: %0u\n", extract_psn(mci));
-	printf("  Manufacturing date: %u.%u\n", extract_mdt_month(mci),
-		extract_mdt_year(mci));
+	printf("  Manufacturer ID: %s\n", dev_get_param(dev, "cid_mid"));
+	printf("  OEM/Application ID: %s\n", dev_get_param(dev, "cid_oid"));
+	printf("  Product name: '%s'\n", dev_get_param(dev, "cid_pnm"));
+	printf("  Product revision: %s\n", dev_get_param(dev, "cid_prv"));
+	printf("  Serial no: %s\n", dev_get_param(dev, "cid_psn"));
+	printf("  Manufacturing date: %s\n", dev_get_param(dev, "cid_mdt"));
+}
+
+static void mci_parse_cid(struct mci *mci) {
+	struct device *dev = &mci->dev;
+	char buffer[8];
+	unsigned int prv;
+
+	dev_add_param_uint32_fixed(dev, "cid_mid", extract_mid(mci), "0x%02X");
+	dev_add_param_uint32_fixed(dev, "cid_oid", extract_oid(mci), "0x%04X");
+	extract_pnm(mci, buffer);
+	dev_add_param_string_fixed(dev, "cid_pnm", buffer);
+	prv = extract_prv(mci);
+	snprintf(buffer, sizeof(buffer), "%u.%u", (prv >> 4) & 0xf, prv & 0xf);
+	dev_add_param_string_fixed(dev, "cid_prv", buffer);
+	dev_add_param_uint32_fixed(dev, "cid_psn", extract_psn(mci), "%0u");
+	extract_mdt(mci, buffer);
+	dev_add_param_string_fixed(dev, "cid_mdt", buffer);
 }
 
 /**
@@ -2406,6 +2419,8 @@ static int mci_card_probe(struct mci *mci)
 			dev_add_param_bool_fixed(&mci->dev, "partitioning_completed", ret);
 	}
 
+	mci_parse_cid(mci);
+
 	dev_dbg(&mci->dev, "SD Card successfully added\n");
 
 on_error:

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 6/9] mci: core: fix extract_prv and write to string buffer
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (4 preceding siblings ...)
  2024-05-28 15:39 ` [PATCH 5/9] mci: core: parse cid into parameters Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 7/9] mci: core: add CBX field as parameter and print it Stefan Kerkmann
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

The product revision PRV field has a slightly different position for MMC
and SD cards. As we almost certainly don't want to use the BCD
representation for scripting the PRV is printed in a human friendly
representation.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index d1c425dda6..5e686c17d2 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1968,11 +1968,13 @@ static void extract_pnm(struct mci *mci, char pnm[static 7])
  * Extract the product revision from the CID
  * @param mci Instance data
  *
- * The 'PRV' is encoded in bit 63:56 in the CID
+ * The 'PRV' is encoded in bit 63:56 in the CID for SD cards and 55:48 for MMC cards
  */
-static unsigned extract_prv(struct mci *mci)
+static void extract_prv(struct mci *mci, char prv[static 8])
 {
-	return mci->cid[2] >> 24;
+	unsigned prv_bcd = IS_SD(mci) ? UNSTUFF_BITS(mci->cid, 56, 8) : UNSTUFF_BITS(mci->cid, 48, 8);
+
+	snprintf(prv, 8,"%u.%u", prv_bcd >> 4, prv_bcd & 0xf);
 }
 
 /**
@@ -2130,14 +2132,12 @@ static void mci_info(struct device *dev)
 static void mci_parse_cid(struct mci *mci) {
 	struct device *dev = &mci->dev;
 	char buffer[8];
-	unsigned int prv;
 
 	dev_add_param_uint32_fixed(dev, "cid_mid", extract_mid(mci), "0x%02X");
 	dev_add_param_uint32_fixed(dev, "cid_oid", extract_oid(mci), "0x%04X");
 	extract_pnm(mci, buffer);
 	dev_add_param_string_fixed(dev, "cid_pnm", buffer);
-	prv = extract_prv(mci);
-	snprintf(buffer, sizeof(buffer), "%u.%u", (prv >> 4) & 0xf, prv & 0xf);
+	extract_prv(mci, buffer);
 	dev_add_param_string_fixed(dev, "cid_prv", buffer);
 	dev_add_param_uint32_fixed(dev, "cid_psn", extract_psn(mci), "%0u");
 	extract_mdt(mci, buffer);

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 7/9] mci: core: add CBX field as parameter and print it
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (5 preceding siblings ...)
  2024-05-28 15:39 ` [PATCH 6/9] mci: core: fix extract_prv and write to string buffer Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 8/9] mci: core: fix extract_oid and write to string buffer Stefan Kerkmann
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 5e686c17d2..2f0f4c25f8 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -2123,6 +2123,8 @@ static void mci_info(struct device *dev)
 	mci_print_caps(mci->card_caps);
 	printf("  Manufacturer ID: %s\n", dev_get_param(dev, "cid_mid"));
 	printf("  OEM/Application ID: %s\n", dev_get_param(dev, "cid_oid"));
+	if (!IS_SD(mci))
+		printf("  CBX: %s\n", dev_get_param(dev, "cid_cbx"));
 	printf("  Product name: '%s'\n", dev_get_param(dev, "cid_pnm"));
 	printf("  Product revision: %s\n", dev_get_param(dev, "cid_prv"));
 	printf("  Serial no: %s\n", dev_get_param(dev, "cid_psn"));
@@ -2135,6 +2137,8 @@ static void mci_parse_cid(struct mci *mci) {
 
 	dev_add_param_uint32_fixed(dev, "cid_mid", extract_mid(mci), "0x%02X");
 	dev_add_param_uint32_fixed(dev, "cid_oid", extract_oid(mci), "0x%04X");
+	if (!IS_SD(mci))
+		dev_add_param_uint32_fixed(dev, "cid_cbx", extract_cbx(mci), "%u");
 	extract_pnm(mci, buffer);
 	dev_add_param_string_fixed(dev, "cid_pnm", buffer);
 	extract_prv(mci, buffer);

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 8/9] mci: core: fix extract_oid and write to string buffer
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (6 preceding siblings ...)
  2024-05-28 15:39 ` [PATCH 7/9] mci: core: add CBX field as parameter and print it Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-28 15:39 ` [PATCH 9/9] mci: core: add missing slice range for extract_psn function Stefan Kerkmann
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

The oem id (OID) field is a 2 character long ASCII string for SD cards
and a 8-bit binary number for MMC cards. Both are used in their string
representation.

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 2f0f4c25f8..8907e9dc83 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1932,11 +1932,18 @@ static unsigned extract_cbx(struct mci *mci)
  * Extract the OEM/Application ID from the CID
  * @param mci Instance data
  *
- * The 'OID' is encoded in bit 119:104 in the CID
+ * The 'OID' is encoded in bit 119:104 in the CID for SD cards and 111:104 for
+ * MMC cards
  */
-static unsigned extract_oid(struct mci *mci)
+static void extract_oid(struct mci *mci, char oid[static 5])
 {
-	return (mci->cid[0] >> 8) & 0xffff;
+	if (IS_SD(mci)) {
+		// SD cards have a 2 character long OEM ID
+		snprintf(oid, 5, "%c%c", UNSTUFF_BITS(mci->cid, 112, 8), UNSTUFF_BITS(mci->cid, 104, 8));
+	} else {
+		// MMC cards have a 8-bit binary number as OEM ID
+		snprintf(oid, 5, "0x%02X", UNSTUFF_BITS(mci->cid, 104, 8));
+	}
 }
 
 /**
@@ -2136,7 +2143,8 @@ static void mci_parse_cid(struct mci *mci) {
 	char buffer[8];
 
 	dev_add_param_uint32_fixed(dev, "cid_mid", extract_mid(mci), "0x%02X");
-	dev_add_param_uint32_fixed(dev, "cid_oid", extract_oid(mci), "0x%04X");
+	extract_oid(mci, buffer);
+	dev_add_param_string_fixed(dev, "cid_oid", buffer);
 	if (!IS_SD(mci))
 		dev_add_param_uint32_fixed(dev, "cid_cbx", extract_cbx(mci), "%u");
 	extract_pnm(mci, buffer);

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 9/9] mci: core: add missing slice range for extract_psn function
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (7 preceding siblings ...)
  2024-05-28 15:39 ` [PATCH 8/9] mci: core: fix extract_oid and write to string buffer Stefan Kerkmann
@ 2024-05-28 15:39 ` Stefan Kerkmann
  2024-05-29  6:17 ` [PATCH 0/9] mci: core: add CID as parameters and fix parsing Sascha Hauer
  2024-05-29  7:24 ` Ahmad Fatoum
  10 siblings, 0 replies; 12+ messages in thread
From: Stefan Kerkmann @ 2024-05-28 15:39 UTC (permalink / raw)
  To: Sascha Hauer, BAREBOX; +Cc: Stefan Kerkmann

Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
---
 drivers/mci/mci-core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 8907e9dc83..75cdb049fb 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1988,7 +1988,7 @@ static void extract_prv(struct mci *mci, char prv[static 8])
  * Extract the product serial number from the CID
  * @param mci Instance data
  *
- * The 'PSN' is encoded in bit 55:24 in the CID
+ * The 'PSN' is encoded in bit 55:24 in the CID for SD cards and 47:16 for MMC cards
  */
 static unsigned extract_psn(struct mci *mci)
 {

-- 
2.39.2




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/9] mci: core: add CID as parameters and fix parsing
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (8 preceding siblings ...)
  2024-05-28 15:39 ` [PATCH 9/9] mci: core: add missing slice range for extract_psn function Stefan Kerkmann
@ 2024-05-29  6:17 ` Sascha Hauer
  2024-05-29  7:24 ` Ahmad Fatoum
  10 siblings, 0 replies; 12+ messages in thread
From: Sascha Hauer @ 2024-05-29  6:17 UTC (permalink / raw)
  To: BAREBOX, Stefan Kerkmann


On Tue, 28 May 2024 17:39:19 +0200, Stefan Kerkmann wrote:
> This series adds the fields of the CID register found in MMC and SD
> cards as parameters to the device. The intention is to use these
> parameters in scripting e.g. for safety checks before applying
> potentially destructive changes.
> 
> During the implementation I found several bugs in the implementation of
> the CID parsing that usually stem from not discerning between MMC and
> SD cards.
> 
> [...]

Applied, thanks!

[1/9] mci: core: rename mtd to mdt
      https://git.pengutronix.de/cgit/barebox/commit/?id=70d4d1ab70d4 (link may not be stable)
[2/9] mci: core: add cbx extraction function
      https://git.pengutronix.de/cgit/barebox/commit/?id=b8171f6eef18 (link may not be stable)
[3/9] mci: core: add product name extraction function
      https://git.pengutronix.de/cgit/barebox/commit/?id=88743df64289 (link may not be stable)
[4/9] mci: core: add manufacturing date extraction function
      https://git.pengutronix.de/cgit/barebox/commit/?id=8df5c2065876 (link may not be stable)
[5/9] mci: core: parse cid into parameters
      https://git.pengutronix.de/cgit/barebox/commit/?id=3419ed8cf618 (link may not be stable)
[6/9] mci: core: fix extract_prv and write to string buffer
      https://git.pengutronix.de/cgit/barebox/commit/?id=fd7edc71c2db (link may not be stable)
[7/9] mci: core: add CBX field as parameter and print it
      https://git.pengutronix.de/cgit/barebox/commit/?id=6ce8b09bff82 (link may not be stable)
[8/9] mci: core: fix extract_oid and write to string buffer
      https://git.pengutronix.de/cgit/barebox/commit/?id=45c4f4939a8c (link may not be stable)
[9/9] mci: core: add missing slice range for extract_psn function
      https://git.pengutronix.de/cgit/barebox/commit/?id=4d340dea7dbd (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH 0/9] mci: core: add CID as parameters and fix parsing
  2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
                   ` (9 preceding siblings ...)
  2024-05-29  6:17 ` [PATCH 0/9] mci: core: add CID as parameters and fix parsing Sascha Hauer
@ 2024-05-29  7:24 ` Ahmad Fatoum
  10 siblings, 0 replies; 12+ messages in thread
From: Ahmad Fatoum @ 2024-05-29  7:24 UTC (permalink / raw)
  To: Stefan Kerkmann, Sascha Hauer, BAREBOX

On 28.05.24 17:39, Stefan Kerkmann wrote:
> This series adds the fields of the CID register found in MMC and SD
> cards as parameters to the device. The intention is to use these
> parameters in scripting e.g. for safety checks before applying
> potentially destructive changes.
> 
> During the implementation I found several bugs in the implementation of
> the CID parsing that usually stem from not discerning between MMC and
> SD cards.
> 
> For a proof of work I have attached these outputs of both an internal
> eMMC and SD card on an Phyboard Polis imx8mm device. The first output is
> mmc-utils and the second barebox at the state of this patch series:
> 
> eMMC:
> 
> root@phyboard-polis-imx8mm-4:~ mmc cid read /sys/class/block/mmcblk2/device -v
> ======MMC/CID======
>         MID: 0x13 (Micron)
>         CBX: 0x1 (BGA)
>         OID: 0x4e
>         PNM: Q2J55L
>         PRV: 0x10 (1.0)
>         PSN: 0x13723b3f
>         MDT: 0x93 2000 oct <-- This is likely a bug in mmc-utils
>         CRC: 0x00
> 
> barebox@PHYTEC phyBOARD-Polis-i.MX8MM RDK: devinfo mmc2
> Card information:
> <snip>
>   Manufacturer ID: 0x13
>   OEM/Application ID: 0x4E
>   CBX: 1
>   Product name: 'Q2J55L'
>   Product revision: 1.0
>   Serial no: 326253375
>   Manufacturing date: 2022.3
> </snip>
> Parameters:
> <snip>
>   cid_cbx: 1 (type: uint32)
>   cid_mdt: 2022.3 (type: string)
>   cid_mid: 0x13 (type: uint32)
>   cid_oid: 0x4E (type: string)
>   cid_pnm: Q2J55L (type: string)
>   cid_prv: 1.0 (type: string)
>   cid_psn: 326253375 (type: uint32)

Nitpick: We usually use . to denote namespaces in device parameters,
should we change this to be cid.cbx and so on?

> </snip>
> 
> SD card:
> 
> root@phyboard-polis-imx8mm-4:~ mmc cid read /sys/class/block/mmcblk1/device -v
> ======SD/CID======
>         MID: 0x63 (Unlisted)
>         OID: C`
>         PNM: CACTU
>         PRV: 0x02 (0.2)
>         PSN: 0x583010d2
>         MDT: 0x158 2021 sep
>         CRC: 0x00
> 
> barebox@PHYTEC phyBOARD-Polis-i.MX8MM RDK:/ devinfo mmc1
> Card information:
> <snip>
>   Manufacturer ID: 0x63
>   OEM/Application ID: C`
>   Product name: 'CACTU'
>   Product revision: 0.2
>   Serial no: 2139062282
>   Manufacturing date: 2021.8
> </snip>
> Parameters:
> <snip>
>   cid_mdt: 2021.8 (type: string)
>   cid_mid: 0x63 (type: uint32)
>   cid_oid: C` (type: string)
>   cid_pnm: CACTU (type: string)
>   cid_prv: 0.2 (type: string)
>   cid_psn: 2139062282 (type: uint32)
> </snip>
> 
> Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
> ---
> Stefan Kerkmann (9):
>       mci: core: rename mtd to mdt
>       mci: core: add cbx extraction function
>       mci: core: add product name extraction function
>       mci: core: add manufacturing date extraction function
>       mci: core: parse cid into parameters
>       mci: core: fix extract_prv and write to string buffer
>       mci: core: add CBX field as parameter and print it
>       mci: core: fix extract_oid and write to string buffer
>       mci: core: add missing slice range for extract_psn function
> 
>  drivers/mci/mci-core.c | 119 ++++++++++++++++++++++++++++++++++++++++---------
>  1 file changed, 98 insertions(+), 21 deletions(-)
> ---
> base-commit: 80d7c9d3d80e75e2335a123d414fd7fe83508291
> change-id: 20240528-feature-mmc-cid-as-parameters-685158ffc027
> 
> Best regards,

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-05-29  7:25 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-28 15:39 [PATCH 0/9] mci: core: add CID as parameters and fix parsing Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 1/9] mci: core: rename mtd to mdt Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 2/9] mci: core: add cbx extraction function Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 3/9] mci: core: add product name " Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 4/9] mci: core: add manufacturing date " Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 5/9] mci: core: parse cid into parameters Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 6/9] mci: core: fix extract_prv and write to string buffer Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 7/9] mci: core: add CBX field as parameter and print it Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 8/9] mci: core: fix extract_oid and write to string buffer Stefan Kerkmann
2024-05-28 15:39 ` [PATCH 9/9] mci: core: add missing slice range for extract_psn function Stefan Kerkmann
2024-05-29  6:17 ` [PATCH 0/9] mci: core: add CID as parameters and fix parsing Sascha Hauer
2024-05-29  7:24 ` Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox