From 8c6f74d4832fd1470a5ff6c878653282e926d402 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Thu, 4 Jun 2020 11:40:42 +0200 Subject: [PATCH] software/liblitesdcard: fat16 boot working with both SPI and SD modes. --- litex/soc/software/bios/cmds/cmd_litesdcard.c | 4 +-- litex/soc/software/liblitesdcard/sdcard.c | 26 +++++++++++-------- litex/soc/software/liblitesdcard/sdcard.h | 4 +-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/litex/soc/software/bios/cmds/cmd_litesdcard.c b/litex/soc/software/bios/cmds/cmd_litesdcard.c index f37f2933..66f57f70 100644 --- a/litex/soc/software/bios/cmds/cmd_litesdcard.c +++ b/litex/soc/software/bios/cmds/cmd_litesdcard.c @@ -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); diff --git a/litex/soc/software/liblitesdcard/sdcard.c b/litex/soc/software/liblitesdcard/sdcard.c index 34c68af9..960f20bf 100644 --- a/litex/soc/software/liblitesdcard/sdcard.c +++ b/litex/soc/software/liblitesdcard/sdcard.c @@ -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