software/liblitesdcard: fat16 boot working with both SPI and SD modes.
authorFlorent Kermarrec <florent@enjoy-digital.fr>
Thu, 4 Jun 2020 09:40:42 +0000 (11:40 +0200)
committerFlorent Kermarrec <florent@enjoy-digital.fr>
Thu, 4 Jun 2020 09:40:42 +0000 (11:40 +0200)
litex/soc/software/bios/cmds/cmd_litesdcard.c
litex/soc/software/liblitesdcard/sdcard.c
litex/soc/software/liblitesdcard/sdcard.h

index f37f29336d920cc45c3e52666f96cecb5a321b8f..66f57f7098ce9641671e49479da7d07c0d7e1f0e 100644 (file)
@@ -80,7 +80,7 @@ static void sdread(int nb_params, char **params)
                return;
        }
 
-       sdcard_read(block, 0);
+       sdcard_read(block*SD_BLOCK_SIZE, 0);
 }
 
 define_command(sdread, sdread, "Read SDCard block", LITESDCARD_CMDS);
@@ -109,7 +109,7 @@ static void sdwrite(int nb_params, char **params)
                return;
        }
 
-       sdcard_write(block, params[1], 0);
+       sdcard_write(block*SD_BLOCK_SIZE, params[1], 0);
 }
 
 define_command(sdwrite, sdwrite, "Write SDCard block", LITESDCARD_CMDS);
index 34c68af9a832f427ff7c1bdd6735d4d65adb4522..960f20bf4c2959a313e4aed7d58b14d9a80ce291 100644 (file)
@@ -15,6 +15,7 @@
 #include "sdcard.h"
 
 #define SDCARD_DEBUG
+#define SDCARD_WAIT_WORKAROUND
 
 #ifdef CSR_SDCORE_BASE
 
@@ -154,7 +155,9 @@ int sdcard_wait_cmd_done(void) {
        unsigned int cmdevt;
        while (1) {
                cmdevt = sdcore_cmdevt_read();
+#ifdef SDCARD_WAIT_WORKAROUND
                busy_wait(5); /* FIXME */
+#endif
 #ifdef SDCARD_DEBUG
                printf("cmdevt: %08x\n", cmdevt);
 #endif
@@ -180,7 +183,9 @@ int sdcard_wait_data_done(void) {
        unsigned int dataevt;
        while (1) {
                dataevt = sdcore_dataevt_read();
+#ifdef SDCARD_WAIT_WORKAROUND
                busy_wait(5); /* FIXME */
+#endif
 #ifdef SDCARD_DEBUG
                printf("dataevt: %08x\n", dataevt);
 #endif
@@ -622,7 +627,7 @@ int sdcard_init(void) {
 
 extern void dump_bytes(unsigned int *ptr, int count, unsigned long addr);
 
-void sdcard_write(unsigned block, const char *data, char silent)
+void sdcard_write(unsigned int addr, const char *data, char silent)
 {
 #ifdef CSR_SDDATAWRITER_BASE
        const char *c = data;
@@ -637,14 +642,14 @@ void sdcard_write(unsigned block, const char *data, char silent)
                }
        }
        if (silent == 0) {
-               printf("Writing SD block %d from mem:\n", block);
+               printf("Writing SD block %d from mem:\n", addr/SD_BLOCK_SIZE);
                dump_bytes((unsigned int *)SDWRITE_BASE, SD_BLOCK_SIZE, (unsigned long) SDWRITE_BASE);
        }
 
        sdcore_datawcrcclear_write(1);
        sdcard_set_block_count(1);
        sdcard_sddatawriter_start();
-       sdcard_write_single_block(block * SD_BLOCK_SIZE);
+       sdcard_write_single_block(addr);
        sdcard_sddatawriter_wait();
        sdcard_stop_transmission();
 #else
@@ -652,7 +657,7 @@ void sdcard_write(unsigned block, const char *data, char silent)
 #endif
 }
 
-void sdcard_read(unsigned block, char silent)
+void sdcard_read(unsigned int addr, char silent)
 {
 #ifdef CSR_SDDATAREADER_BASE
        int i;
@@ -660,11 +665,11 @@ void sdcard_read(unsigned block, char silent)
                sdread_buf[i] = 0;
        }
        if (silent == 0)
-               printf("Reading SD block %d from mem:\n", block);
+               printf("Reading SD block %d from mem:\n", addr/SD_BLOCK_SIZE);
 
        sdcard_set_block_count(1);
        sdcard_sddatareader_start();
-       sdcard_read_single_block(block * SD_BLOCK_SIZE);
+       sdcard_read_single_block(addr);
        sdcard_sddatareader_wait();
 
        if (silent == 0)
@@ -688,12 +693,12 @@ int sdcard_test(unsigned int blocks)
                for(j=0; j<SD_BLOCK_SIZE; j++)
                        sdwrite_buf[j] = (rand() + i) & 0xff;
                /* write block from write mem */
-               sdcard_write(i, NULL, 0);
+               sdcard_write(i*SD_BLOCK_SIZE, NULL, 0);
 
                busy_wait(100); /* FIXME */
 
                /* read block to read mem */
-               sdcard_read(i, 0);
+               sdcard_read(i*SD_BLOCK_SIZE, 0);
                /* check read mem */
                srand(0);
                for(j=0; j<SD_BLOCK_SIZE; j++)
@@ -730,12 +735,11 @@ uint8_t readSector(uint32_t sectorNumber, uint8_t *storage)
 {
        int n;
 
-       // FIXME: handle errors.
+       // FIXME: handle errors, svoid recopy.
 
        sdcard_read(sectorNumber, 1);
-        for(n=0; n<512; n++)
+        for(n=0; n<SD_BLOCK_SIZE; n++)
         storage[n] = sdread_buf[n];
-
     return SUCCESS;
 }
 
index 787b99f0d7ffa1a1c29ee117993da83e4d2a5e6c..613a4edd7884182062e91a17265447a90dd73d20 100644 (file)
@@ -100,8 +100,8 @@ void sdcard_bist_checker_wait(void);
 void hexdump(volatile const char *buf, size_t len);
 
 int sdcard_init(void);
-void sdcard_write(unsigned block, const char *data, char silent);
-void sdcard_read(unsigned block, char silent);
+void sdcard_write(unsigned int addr, const char *data, char silent);
+void sdcard_read(unsigned int addr, char silent);
 void sdcard_sddatawriter_start(void);
 void sdcard_sddatareader_start(void);
 int sdcard_sddatawriter_wait(void);