bios/spiflash: bring back write and erase, add write from sdcard file cmd

When shipping MNT RKX7, I pre-flash the SPI flash with a LiteX bitfile
for testing. cmd_spiflash had regressed because of changed SPIFLASH defines
and didn't offer the write functions anymore. This commit fixes that, and
adds convenience functions:

- flash_erase_range <offset> <count (bytes)>
- flash_from_sdcard <filename>

The latter reuses some boot code to copy the contents of the specified
file from the boot FAT partition on the SD card to SPI flash (i.e.
a bitstream).
This commit is contained in:
Lukas F. Hartmann 2023-04-25 13:30:07 +02:00
parent 118dd6ed08
commit cb2a789008
1 changed files with 90 additions and 21 deletions

View File

@ -8,23 +8,26 @@
#include "../command.h" #include "../command.h"
#include "../helpers.h" #include "../helpers.h"
#include <libbase/progress.h>
#include <liblitespi/spiflash.h>
#include <libfatfs/ff.h>
/** /**
* Command "flash_write" * Command "flash_write"
* *
* Write data from a memory buffer to SPI flash * Write data from a memory buffer to SPI flash
* *
*/ */
#if (defined CSR_SPIFLASH_BASE && defined SPIFLASH_PAGE_SIZE) #if (defined CSR_SPIFLASH_CORE_MASTER_CS_ADDR)
static void flash_write_handler(int nb_params, char **params) static void flash_write_handler(int nb_params, char **params)
{ {
char *c; char *c;
unsigned int addr; unsigned int addr;
unsigned int value; unsigned int mem_addr;
unsigned int count; unsigned int count;
unsigned int i;
if (nb_params < 2) { if (nb_params < 2) {
printf("flash_write <offset> <value> [count]"); printf("flash_write <offset> <mem_addr> [count (bytes)]");
return; return;
} }
@ -34,9 +37,9 @@ static void flash_write_handler(int nb_params, char **params)
return; return;
} }
value = strtoul(params[1], &c, 0); mem_addr = strtoul(params[1], &c, 0);
if (*c != 0) { if (*c != 0) {
printf("Incorrect value"); printf("Incorrect mem_addr");
return; return;
} }
@ -50,26 +53,92 @@ static void flash_write_handler(int nb_params, char **params)
} }
} }
for (i = 0; i < count; i++) spiflash_write_stream(addr, (unsigned char *)mem_addr, count);
write_to_flash(addr + i * 4, (unsigned char *)&value, 4);
} }
define_command(flash_write, flash_write_handler, "Write to flash", SPIFLASH_CMDS); define_command(flash_write, flash_write_handler, "Write to flash", SPIFLASH_CMDS);
#endif
/** static void flash_from_sdcard_handler(int nb_params, char **params)
* Command "flash_erase"
*
* Flash erase
*
*/
#if (defined CSR_SPIFLASH_BASE && defined SPIFLASH_PAGE_SIZE)
static void flash_erase_handler(int nb_params, char **params)
{ {
erase_flash(); FRESULT fr;
printf("Flash erased\n"); FATFS fs;
FIL file;
uint32_t br;
uint32_t offset;
unsigned long length;
uint8_t buf[512];
if (nb_params < 1) {
printf("flash_from_sdcard <filename>");
return;
} }
define_command(flash_erase, flash_erase_handler, "Erase whole flash", SPIFLASH_CMDS); char* filename = params[0];
#endif
fr = f_mount(&fs, "", 1);
if (fr != FR_OK)
return;
fr = f_open(&file, filename, FA_READ);
if (fr != FR_OK) {
printf("%s file not found.\n", filename);
f_mount(0, "", 0);
return;
}
length = f_size(&file);
printf("Copying %s to SPI flash (%ld bytes)...\n", filename, length);
init_progression_bar(length);
offset = 0;
for (;;) {
fr = f_read(&file, (void*) buf, 512, (UINT *)&br);
if (fr != FR_OK) {
printf("file read error.\n");
f_close(&file);
f_mount(0, "", 0);
return;
}
if (br == 0) {
break;
} else {
spiflash_write_stream(offset, buf, br);
}
offset += br;
show_progress(offset);
}
show_progress(offset);
printf("\n");
f_close(&file);
f_mount(0, "", 0);
}
define_command(flash_from_sdcard, flash_from_sdcard_handler, "Write file from SD card to flash", SPIFLASH_CMDS);
static void flash_erase_range_handler(int nb_params, char **params)
{
char *c;
uint32_t addr;
uint32_t count;
if (nb_params < 2) {
printf("flash_erase <offset> <count (bytes)>");
return;
}
addr = strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect offset");
return;
}
count = strtoul(params[1], &c, 0);
if (*c != 0) {
printf("Incorrect count");
return;
}
spiflash_erase_range(addr, count);
}
define_command(flash_erase_range, flash_erase_range_handler, "Erase flash range", SPIFLASH_CMDS);
#endif