Skip to contentSkip to conversation

hw/sd: sd: Fix incorrect populated function switch status data structure

PATCH New Bin Meng 1 participant 2 messages Aug 21, 2020

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v3 1/2 hw/sd: sd: Fix incorrect populated function switch status data structure

From: Bin Meng <[email protected]>

At present the function switch status data structure bit [399:376] are wrongly pupulated. These 3 bytes encode function switch status for the 6 function groups, with 4 bits per group, starting from function group 6 at bit 399, then followed by function group 5 at bit 395, and so on.

However the codes mistakenly fills in the function group 1 status at bit 399. This fixes the code logic.

Fixes a1bb27b1e9 ("SD card emulation (initial implementation)")

---

Changes in v3:
- add Tested-by tag from Sai Pavan Boddu

Changes in v2:
- remove the pointless zero initialization

 hw/sd/sd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/sd/sd.c +2 -1
index fad9cf1..3226404 100644
@@ -806,11 +806,12 @@ static void sd_function_switch(SDState *sd, uint32_t arg)
     sd->data[11] = 0x43;
     sd->data[12] = 0x80;	/* Supported group 1 functions */
     sd->data[13] = 0x03;
+
     for (i = 0; i < 6; i ++) {
         new_func = (arg >> (i * 4)) & 0x0f;
         if (mode && new_func != 0x0f)
             sd->function_group[i] = new_func;
-        sd->data[14 + (i >> 1)] = new_func << ((i * 4) & 4);
+        sd->data[16 - (i >> 1)] |= new_func << ((i % 2) * 4);
     }
     memset(&sd->data[17], 0, 47);
     stw_be_p(sd->data + 64, sd_crc16(sd->data, 64));
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v3 2/2 hw/sd: sd: Correct the maximum size of a Standard Capacity SD Memory Card

From: Bin Meng <[email protected]>

Per the SD spec, Standard Capacity SD Memory Card (SDSC) supports capacity up to and including 2 GiB.

Fixes 2d7adea4fe ("hw/sd: Support SDHC size cards")

---

Changes in v3:
- add Reviewed-by tag from Philippe Mathieu-Daudé
- add Tested-by tag from Sai Pavan Boddu

Changes in v2:
- fix SDSC size check in sd_set_csd() too

 hw/sd/sd.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/sd/sd.c +4 -2
index 3226404..254d713 100644
@@ -50,6 +50,8 @@
 
 //#define DEBUG_SD 1
 
+#define SDSC_MAX_CAPACITY   (2 * GiB)
+
 typedef enum {
     sd_r0 = 0,    /* no response */
     sd_r1,        /* normal response command */
@@ -313,7 +315,7 @@ static void sd_ocr_powerup(void *opaque)
     /* card power-up OK */
     sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_POWER_UP, 1);
 
-    if (sd->size > 1 * GiB) {
+    if (sd->size > SDSC_MAX_CAPACITY) {
         sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_CAPACITY, 1);
     }
 }
@@ -385,7 +387,7 @@ static void sd_set_csd(SDState *sd, uint64_t size)
     uint32_t sectsize = (1 << (SECTOR_SHIFT + 1)) - 1;
     uint32_t wpsize = (1 << (WPGROUP_SHIFT + 1)) - 1;
 
-    if (size <= 1 * GiB) { /* Standard Capacity SD */
+    if (size <= SDSC_MAX_CAPACITY) { /* Standard Capacity SD */
         sd->csd[0] = 0x00;	/* CSD structure */
         sd->csd[1] = 0x26;	/* Data read access-time-1 */
         sd->csd[2] = 0x00;	/* Data read access-time-2 */
Signature · 1 line
2.7.4