Commit e30926af authored by Federico Vaga's avatar Federico Vaga

tool: isolate bitstream read/alloc/free in functions

Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent 58cd033d
......@@ -60,6 +60,12 @@ const uint8_t sdb_header[] = {
0x62, 0x69, 0x6e, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x01
};
struct bitstream {
char *path;
size_t size;
void *buf;
};
struct vme_mapping map;
void *vme_va;
......@@ -295,12 +301,12 @@ flash_program_exit:
return ret;
}
int program_flash(char *name, uint8_t *buf, size_t size, int program_boot)
static int program_flash(struct bitstream *bitstream, int program_boot)
{
int ret = 0;
printf("Programming the Application FPGA flash with bitstream %s.\n",
name);
bitstream->path);
if (!spi_test_presence())
{
......@@ -331,11 +337,12 @@ int program_flash(char *name, uint8_t *buf, size_t size, int program_boot)
printf("Bootloader update aborted.\n");
return -1;
}
ret = flash_program(0, buf, size, flash_id);
ret = flash_program(0, bitstream->buf, bitstream->size, flash_id);
} else {
ret = flash_program(BOOTLOADER_SDB_BASE, sdb_header, sizeof(sdb_header), flash_id);
if (ret == 0)
ret = flash_program(BOOTLOADER_BITSTREAM_BASE, buf, size, flash_id);
ret = flash_program(BOOTLOADER_BITSTREAM_BASE,
bitstream->buf, bitstream->size, flash_id);
}
if (ret == 0)
......@@ -346,22 +353,22 @@ int program_flash(char *name, uint8_t *buf, size_t size, int program_boot)
return ret;
}
int program_afpga(char *name, uint8_t *buf, size_t size)
static int program_afpga(struct bitstream *bitstream)
{
size_t i = 0;
printf("Booting the Application FPGA with bitstream %s.\n",
name);
bitstream->path);
csr_writel(SXLDR_CSR_SWRST, SXLDR_REG_CSR);
csr_writel(SXLDR_CSR_START | SXLDR_CSR_MSBF, SXLDR_REG_CSR);
while(i < size) {
while(i < bitstream->size) {
if(! (csr_readl(SXLDR_REG_FIFO_CSR) & SXLDR_FIFO_CSR_FULL))
{
uint32_t word = *(uint32_t *) ( buf + i );
size_t n = (size-i>4?4:size-i);
uint32_t word = *(uint32_t *) ( bitstream->buf + i );
size_t n = (bitstream->size - i > 4 ? 4 : bitstream->size - i);
csr_writel((n - 1) | ((n<4) ? SXLDR_FIFO_R0_XLAST : 0), SXLDR_REG_FIFO_R0);
csr_writel(htonl(word), SXLDR_REG_FIFO_R1);
i+=n;
......@@ -385,8 +392,44 @@ int program_afpga(char *name, uint8_t *buf, size_t size)
return 0;
}
static int bitstream_buffer_alloc_and_fill(struct bitstream *bitstream)
{
FILE *f;
size_t size_ret;
int err = -1;
f = fopen(bitstream->path, "rb");
if (!f)
goto out;
fseek(f, 0, SEEK_END);
bitstream->size = ftell(f);
rewind(f);
bitstream->buf = malloc(bitstream->size);
if (!bitstream->buf)
goto out_close;
size_ret = fread(bitstream->buf, 1, bitstream->size, f);
if (size_ret != bitstream->size) {
errno = EINVAL;
goto out_close;
}
err = 0;
out_close:
fclose(f);
out:
return err;
}
static void bitstream_buffer_free(struct bitstream *bitstream)
{
free(bitstream->buf);
}
int main(int argc, char *argv[])
{
struct bitstream bitstream;
FILE *f;
void *buf;
uint32_t size;
......@@ -394,6 +437,7 @@ int main(int argc, char *argv[])
int program_boot = 0;
int direct_boot = 0;
int rv;
int err;
if (argc < 3) {
printf("usage: %s slot bitstream.bin [-b]\n", argv[0]);
......@@ -410,19 +454,13 @@ int main(int argc, char *argv[])
direct_boot = 1;
}
f = fopen(argv[2], "rb");
if (!f) {
perror("fopen()");
bitstream.path = argv[2];
err = bitstream_buffer_alloc_and_fill(&bitstream);
if (err) {
fprintf(stderr, "Can't read bitstream '%s'. %s",
bitstream.path, strerror(errno));
return -1;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
rewind(f);
buf = malloc(size);
fread(buf, 1, size, f);
fclose(f);
rv = sscanf(argv[1], "%i", &slot);
if (!rv) {
......@@ -437,12 +475,11 @@ int main(int argc, char *argv[])
enter_bootloader();
if(!direct_boot)
rv = program_flash(argv[2], buf, size, program_boot);
rv = program_flash(&bitstream, program_boot);
else
rv = program_afpga(argv[2], buf, size);
rv = program_afpga(&bitstream);
free(buf);
bitstream_buffer_free(&bitstream);
return rv;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment