Skip to contentSkip to conversation

hw/sd: Add SDHC support for SD card SPI-mode

PATCH Superseded [email protected] 2 participants 2 messages Dec 28, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/sd: Add SDHC support for SD card SPI-mode

From: Frank Chang <[email protected]>

In SPI-mode, SD card's OCR register: Card Capacity Status (CCS) bit is not set to 1 correclty when the assigned SD image size is larger than 2GB (SDHC). This will cause the SD card to be indentified as SDSC incorrectly. CCS bit should be set to 1 if we are using SDHC.

Also, as there's no power up emulation in SPI-mode.
The OCR register: Card power up status bit bit (busy) should also be set to 1 when reset. (busy bit is set to LOW if the card has not finished the power up routine.)

---
 hw/sd/sd.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/sd/sd.c +13 -1
index bb5dbff68c..9169208dbc 100644
@@ -294,6 +294,18 @@ static void sd_set_ocr(SDState *sd)
 {
     /* All voltages OK */
     sd->ocr = R_OCR_VDD_VOLTAGE_WIN_HI_MASK;
+
+    if (sd->spi) {
+        /*
+         * We don't need to emulate power up sequence in SPI-mode.
+         * Thus, the card's power up status bit should be set to 1 when reset.
+         * The card's capacity status bit should also be set if SD card size
+         * is larger than 2GB for SDHC support.
+         */
+        sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_POWER_UP, 1);
+        sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_CAPACITY,
+                             sd->size > SDSC_MAX_CAPACITY);
+    }
 }
 
 static void sd_ocr_powerup(void *opaque)
@@ -560,6 +572,7 @@ static void sd_reset(DeviceState *dev)
 
     sd->state = sd_idle_state;
     sd->rca = 0x0000;
+    sd->size = size;
     sd_set_ocr(sd);
     sd_set_scr(sd);
     sd_set_cid(sd);
@@ -574,7 +587,6 @@ static void sd_reset(DeviceState *dev)
     memset(sd->function_group, 0, sizeof(sd->function_group));
     sd->erase_start = INVALID_ADDRESS;
     sd->erase_end = INVALID_ADDRESS;
-    sd->size = size;
     sd->blk_len = 0x200;
     sd->pwd_len = 0;
     sd->expecting_acmd = false;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · On 12/28/21 02:50, [email protected] wrote:

From: Frank Chang <[email protected]>

In SPI-mode, SD card's OCR register: Card Capacity Status (CCS) bit is not set to 1 correclty when the assigned SD image size is larger than 2GB (SDHC). This will cause the SD card to be indentified as SDSC incorrectly. CCS bit should be set to 1 if we are using SDHC.

Also, as there's no power up emulation in SPI-mode.
The OCR register: Card power up status bit bit (busy) should also be set to 1 when reset. (busy bit is set to LOW if the card has not finished the power up routine.)

---
 hw/sd/sd.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/sd/sd.c +1 -0
index bb5dbff68c..9169208dbc 100644
@@ -294,6 +294,18 @@ static void sd_set_ocr(SDState *sd)
 {
     /* All voltages OK */
     sd->ocr = R_OCR_VDD_VOLTAGE_WIN_HI_MASK;
+

hw/sd/sd.c static void sd_set_ocr(SDState *sd) · line 307

Quoted 10 lines
hw/sd/sd.c +10 -0
+    if (sd->spi) {
+        /*
+         * We don't need to emulate power up sequence in SPI-mode.
+         * Thus, the card's power up status bit should be set to 1 when reset.
+         * The card's capacity status bit should also be set if SD card size
+         * is larger than 2GB for SDHC support.
+         */
+        sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_POWER_UP, 1);
+        sd->ocr = FIELD_DP32(sd->ocr, OCR, CARD_CAPACITY,
+                             sd->size > SDSC_MAX_CAPACITY);

Simply call sd_ocr_powerup() instead, so we get the trace event.

Calling sd_ocr_powerup():

Quoted 20 lines
hw/sd/sd.c +2 -1
+    }
 }
 
 static void sd_ocr_powerup(void *opaque)
@@ -560,6 +572,7 @@ static void sd_reset(DeviceState *dev)
 
     sd->state = sd_idle_state;
     sd->rca = 0x0000;
+    sd->size = size;
     sd_set_ocr(sd);
     sd_set_scr(sd);
     sd_set_cid(sd);
@@ -574,7 +587,6 @@ static void sd_reset(DeviceState *dev)
     memset(sd->function_group, 0, sizeof(sd->function_group));
     sd->erase_start = INVALID_ADDRESS;
     sd->erase_end = INVALID_ADDRESS;
-    sd->size = size;
     sd->blk_len = 0x200;
     sd->pwd_len = 0;
     sd->expecting_acmd = false;