Patchwork jason.mcmullan@gmail.com

login
register
mail settings
Submitter Jason McMullan
Date Oct. 28, 2009, 5:48 a.m.
Message ID <20091028054827.2C108EB38D@mcmullan-linux>
Download mbox | patch
Permalink /patch/37243/
State New
Headers show

Comments

Jason McMullan - Oct. 28, 2009, 5:48 a.m.
From 73d1d21d98a9d98045bc69cfe58124ef6f73a336 Mon Sep 17 00:00:00 2001
From: Jason S. McMullan <jason.mcmullan@gmail.com>
Date: Wed, 28 Oct 2009 00:56:00 -0400
Subject: [PATCH] hw/sd: Support SDHC size cards

Signed-off-by: Jason S. McMullan <jason.mcmullan@gmail.com>
---
 hw/sd.c |  153 ++++++++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 93 insertions(+), 60 deletions(-)

Patch

diff --git a/hw/sd.c b/hw/sd.c
index 9888547..4d306af 100644
--- a/hw/sd.c
+++ b/hw/sd.c
@@ -81,7 +81,7 @@  struct SDState {
     uint32_t vhs;
     int wp_switch;
     int *wp_groups;
-    uint32_t size;
+    uint64_t size;
     int blk_len;
     uint32_t erase_start;
     uint32_t erase_end;
@@ -92,7 +92,7 @@  struct SDState {
     int spi;
     int current_cmd;
     int blk_written;
-    uint32_t data_start;
+    uint64_t data_start;
     uint32_t data_offset;
     uint8_t data[512];
     qemu_irq readonly_cb;
@@ -249,37 +249,59 @@  static const uint8_t sd_csd_rw_mask[16] = {
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xfe,
 };
 
-static void sd_set_csd(SDState *sd, uint32_t size)
+static void sd_set_csd(SDState *sd, uint64_t size)
 {
     uint32_t csize = (size >> (CMULT_SHIFT + HWBLOCK_SHIFT)) - 1;
     uint32_t sectsize = (1 << (SECTOR_SHIFT + 1)) - 1;
     uint32_t wpsize = (1 << (WPGROUP_SHIFT + 1)) - 1;
 
-    sd->csd[0] = 0x00;		/* CSD structure */
-    sd->csd[1] = 0x26;		/* Data read access-time-1 */
-    sd->csd[2] = 0x00;		/* Data read access-time-2 */
-    sd->csd[3] = 0x5a;		/* Max. data transfer rate */
-    sd->csd[4] = 0x5f;		/* Card Command Classes */
-    sd->csd[5] = 0x50 |		/* Max. read data block length */
-        HWBLOCK_SHIFT;
-    sd->csd[6] = 0xe0 |		/* Partial block for read allowed */
-        ((csize >> 10) & 0x03);
-    sd->csd[7] = 0x00 |		/* Device size */
-        ((csize >> 2) & 0xff);
-    sd->csd[8] = 0x3f |		/* Max. read current */
-        ((csize << 6) & 0xc0);
-    sd->csd[9] = 0xfc |		/* Max. write current */
-        ((CMULT_SHIFT - 2) >> 1);
-    sd->csd[10] = 0x40 |	/* Erase sector size */
-        (((CMULT_SHIFT - 2) << 7) & 0x80) | (sectsize >> 1);
-    sd->csd[11] = 0x00 |	/* Write protect group size */
-        ((sectsize << 7) & 0x80) | wpsize;
-    sd->csd[12] = 0x90 |	/* Write speed factor */
-        (HWBLOCK_SHIFT >> 2);
-    sd->csd[13] = 0x20 |	/* Max. write data block length */
-        ((HWBLOCK_SHIFT << 6) & 0xc0);
-    sd->csd[14] = 0x00;		/* File format group */
-    sd->csd[15] = (sd_crc7(sd->csd, 15) << 1) | 1;
+    if (size <= 0x40000000) {
+	    sd->csd[0] = 0x00;		/* CSD structure */
+	    sd->csd[1] = 0x26;		/* Data read access-time-1 */
+	    sd->csd[2] = 0x00;		/* Data read access-time-2 */
+	    sd->csd[3] = 0x5a;		/* Max. data transfer rate */
+	    sd->csd[4] = 0x5f;		/* Card Command Classes */
+	    sd->csd[5] = 0x50 |		/* Max. read data block length */
+		HWBLOCK_SHIFT;
+	    sd->csd[6] = 0xe0 |		/* Partial block for read allowed */
+		((csize >> 10) & 0x03);
+	    sd->csd[7] = 0x00 |		/* Device size */
+		((csize >> 2) & 0xff);
+	    sd->csd[8] = 0x3f |		/* Max. read current */
+		((csize << 6) & 0xc0);
+	    sd->csd[9] = 0xfc |		/* Max. write current */
+		((CMULT_SHIFT - 2) >> 1);
+	    sd->csd[10] = 0x40 |	/* Erase sector size */
+		(((CMULT_SHIFT - 2) << 7) & 0x80) | (sectsize >> 1);
+	    sd->csd[11] = 0x00 |	/* Write protect group size */
+		((sectsize << 7) & 0x80) | wpsize;
+	    sd->csd[12] = 0x90 |	/* Write speed factor */
+		(HWBLOCK_SHIFT >> 2);
+	    sd->csd[13] = 0x20 |	/* Max. write data block length */
+		((HWBLOCK_SHIFT << 6) & 0xc0);
+	    sd->csd[14] = 0x00;		/* File format group */
+	    sd->csd[15] = (sd_crc7(sd->csd, 15) << 1) | 1;
+    } else {
+    	    size /= 512 * 1024;
+    	    size -= 1;
+    	    sd->csd[0] = 0x40;
+    	    sd->csd[1] = 0x0e;
+    	    sd->csd[2] = 0x00;
+    	    sd->csd[3] = 0x32;
+    	    sd->csd[4] = 0x5b;
+    	    sd->csd[5] = 0x59;
+    	    sd->csd[6] = 0x00;
+    	    sd->csd[7] = (size >> 16) & 0xff;
+    	    sd->csd[8] = (size >> 8) & 0xff;
+    	    sd->csd[9] = (size & 0xff);
+    	    sd->csd[10] = 0x7f;
+    	    sd->csd[11] = 0x80;
+    	    sd->csd[12] = 0x0a;
+    	    sd->csd[13] = 0x40;
+    	    sd->csd[14] = 0x00;
+    	    sd->csd[15] = 0x00;
+    	    sd->ocr |= (1 << 30);
+    }
 }
 
 static void sd_set_rca(SDState *sd)
@@ -362,7 +384,7 @@  static void sd_response_r7_make(SDState *sd, uint8_t *response)
 
 static void sd_reset(SDState *sd, BlockDriverState *bdrv)
 {
-    uint32_t size;
+    uint64_t size;
     uint64_t sect;
 
     if (bdrv) {
@@ -372,10 +394,7 @@  static void sd_reset(SDState *sd, BlockDriverState *bdrv)
     }
     sect <<= 9;
 
-    if (sect > 0x40000000)
-        size = 0x40000000;	/* 1 gig */
-    else
-        size = sect + 1;
+    size = sect + 1;
 
     sect = (size >> (HWBLOCK_SHIFT + SECTOR_SHIFT + WPGROUP_SHIFT)) + 1;
 
@@ -460,7 +479,7 @@  static void sd_erase(SDState *sd)
             sd->card_status |= WP_ERASE_SKIP;
 }
 
-static uint32_t sd_wpbits(SDState *sd, uint32_t addr)
+static uint32_t sd_wpbits(SDState *sd, uint64_t addr)
 {
     uint32_t i, wpnum;
     uint32_t ret = 0;
@@ -583,7 +602,14 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
                                        SDRequest req)
 {
     uint32_t rca = 0x0000;
+    uint64_t addr;
 
+    if (sd->ocr & (1 << 30)) {
+    	    addr = (uint64_t)req.arg << 9;
+    } else {
+    	    addr = req.arg;
+    }
+    
     if (sd_cmd_type[req.cmd] == sd_ac || sd_cmd_type[req.cmd] == sd_adtc)
         rca = req.arg >> 16;
 
@@ -735,7 +761,7 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
                 break;
             sd->state = sd_sendingdata_state;
             memcpy(sd->data, sd->csd, 16);
-            sd->data_start = req.arg;
+            sd->data_start = addr;
             sd->data_offset = 0;
             return sd_r1;
 
@@ -757,7 +783,7 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
                 break;
             sd->state = sd_sendingdata_state;
             memcpy(sd->data, sd->cid, 16);
-            sd->data_start = req.arg;
+            sd->data_start = addr;
             sd->data_offset = 0;
             return sd_r1;
 
@@ -850,7 +876,7 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
         switch (sd->state) {
         case sd_transfer_state:
             sd->state = sd_sendingdata_state;
-            sd->data_start = req.arg;
+            sd->data_start = addr;
             sd->data_offset = 0;
 
             if (sd->data_start + sd->blk_len > sd->size)
@@ -866,7 +892,7 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
         switch (sd->state) {
         case sd_transfer_state:
             sd->state = sd_sendingdata_state;
-            sd->data_start = req.arg;
+            sd->data_start = addr;
             sd->data_offset = 0;
 
             if (sd->data_start + sd->blk_len > sd->size)
@@ -888,7 +914,7 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
             if (sd->spi)
                 break;
             sd->state = sd_receivingdata_state;
-            sd->data_start = req.arg;
+            sd->data_start = addr;
             sd->data_offset = 0;
             sd->blk_written = 0;
 
@@ -914,7 +940,7 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
             if (sd->spi)
                 break;
             sd->state = sd_receivingdata_state;
-            sd->data_start = req.arg;
+            sd->data_start = addr;
             sd->data_offset = 0;
             sd->blk_written = 0;
 
@@ -965,13 +991,13 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
     case 28:	/* CMD28:  SET_WRITE_PROT */
         switch (sd->state) {
         case sd_transfer_state:
-            if (req.arg >= sd->size) {
+            if (addr >= sd->size) {
                 sd->card_status = ADDRESS_ERROR;
                 return sd_r1b;
             }
 
             sd->state = sd_programming_state;
-            sd->wp_groups[req.arg >> (HWBLOCK_SHIFT +
+            sd->wp_groups[addr >> (HWBLOCK_SHIFT +
                             SECTOR_SHIFT + WPGROUP_SHIFT)] = 1;
             /* Bzzzzzzztt .... Operation complete.  */
             sd->state = sd_transfer_state;
@@ -985,13 +1011,13 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
     case 29:	/* CMD29:  CLR_WRITE_PROT */
         switch (sd->state) {
         case sd_transfer_state:
-            if (req.arg >= sd->size) {
+            if (addr >= sd->size) {
                 sd->card_status = ADDRESS_ERROR;
                 return sd_r1b;
             }
 
             sd->state = sd_programming_state;
-            sd->wp_groups[req.arg >> (HWBLOCK_SHIFT +
+            sd->wp_groups[addr >> (HWBLOCK_SHIFT +
                             SECTOR_SHIFT + WPGROUP_SHIFT)] = 0;
             /* Bzzzzzzztt .... Operation complete.  */
             sd->state = sd_transfer_state;
@@ -1007,7 +1033,7 @@  static sd_rsp_type_t sd_normal_command(SDState *sd,
         case sd_transfer_state:
             sd->state = sd_sendingdata_state;
             *(uint32_t *) sd->data = sd_wpbits(sd, req.arg);
-            sd->data_start = req.arg;
+            sd->data_start = addr;
             sd->data_offset = 0;
             return sd_r1b;
 
@@ -1322,11 +1348,11 @@  int sd_do_command(SDState *sd, SDRequest *req,
     return rsplen;
 }
 
-/* No real need for 64 bit addresses here */
-static void sd_blk_read(SDState *sd, uint32_t addr, uint32_t len)
+static void sd_blk_read(SDState *sd, uint64_t addr, uint32_t len)
 {
-    uint32_t end = addr + len;
+    uint64_t end = addr + len;
 
+    DPRINTF("sd_blk_read: addr = 0x%08llx, len = %d\n", (unsigned long long)addr, len);
     if (!sd->bdrv || bdrv_read(sd->bdrv, addr >> 9, sd->buf, 1) == -1) {
         fprintf(stderr, "sd_blk_read: read error on host side\n");
         return;
@@ -1344,9 +1370,9 @@  static void sd_blk_read(SDState *sd, uint32_t addr, uint32_t len)
         memcpy(sd->data, sd->buf + (addr & 511), len);
 }
 
-static void sd_blk_write(SDState *sd, uint32_t addr, uint32_t len)
+static void sd_blk_write(SDState *sd, uint64_t addr, uint32_t len)
 {
-    uint32_t end = addr + len;
+    uint64_t end = addr + len;
 
     if ((addr & 511) || len < 512)
         if (!sd->bdrv || bdrv_read(sd->bdrv, addr >> 9, sd->buf, 1) == -1) {
@@ -1505,6 +1531,7 @@  uint8_t sd_read_data(SDState *sd)
 {
     /* TODO: Append CRCs */
     uint8_t ret;
+    int io_len;
 
     if (!sd->bdrv || !bdrv_is_inserted(sd->bdrv) || !sd->enable)
         return 0x00;
@@ -1517,6 +1544,12 @@  uint8_t sd_read_data(SDState *sd)
     if (sd->card_status & (ADDRESS_ERROR | WP_VIOLATION))
         return 0x00;
 
+    if (sd->ocr & (1 << 30)) {
+	io_len = 512;
+    } else {
+    	io_len = sd->blk_len;
+    }
+
     switch (sd->current_cmd) {
     case 6:	/* CMD6:   SWITCH_FUNCTION */
         ret = sd->data[sd->data_offset ++];
@@ -1535,13 +1568,13 @@  uint8_t sd_read_data(SDState *sd)
 
     case 11:	/* CMD11:  READ_DAT_UNTIL_STOP */
         if (sd->data_offset == 0)
-            BLK_READ_BLOCK(sd->data_start, sd->blk_len);
+            BLK_READ_BLOCK(sd->data_start, io_len);
         ret = sd->data[sd->data_offset ++];
 
-        if (sd->data_offset >= sd->blk_len) {
-            sd->data_start += sd->blk_len;
+        if (sd->data_offset >= io_len) {
+            sd->data_start += io_len;
             sd->data_offset = 0;
-            if (sd->data_start + sd->blk_len > sd->size) {
+            if (sd->data_start + io_len > sd->size) {
                 sd->card_status |= ADDRESS_ERROR;
                 break;
             }
@@ -1557,22 +1590,22 @@  uint8_t sd_read_data(SDState *sd)
 
     case 17:	/* CMD17:  READ_SINGLE_BLOCK */
         if (sd->data_offset == 0)
-            BLK_READ_BLOCK(sd->data_start, sd->blk_len);
+            BLK_READ_BLOCK(sd->data_start, io_len);
         ret = sd->data[sd->data_offset ++];
 
-        if (sd->data_offset >= sd->blk_len)
+        if (sd->data_offset >= io_len)
             sd->state = sd_transfer_state;
         break;
 
     case 18:	/* CMD18:  READ_MULTIPLE_BLOCK */
         if (sd->data_offset == 0)
-            BLK_READ_BLOCK(sd->data_start, sd->blk_len);
+            BLK_READ_BLOCK(sd->data_start, io_len);
         ret = sd->data[sd->data_offset ++];
 
-        if (sd->data_offset >= sd->blk_len) {
-            sd->data_start += sd->blk_len;
+        if (sd->data_offset >= io_len) {
+            sd->data_start += io_len;
             sd->data_offset = 0;
-            if (sd->data_start + sd->blk_len > sd->size) {
+            if (sd->data_start + io_len > sd->size) {
                 sd->card_status |= ADDRESS_ERROR;
                 break;
             }