From e7fdc03e8c694da13491019db06858e7375b79c0 Mon Sep 17 00:00:00 2001 From: Joerg Wunsch Date: Sun, 21 Jun 2020 23:19:54 +0200 Subject: [PATCH 1/4] Do not issue the MC_TCK_D5 command on non-USB-HS chips Issuing it on a FT2232D chip causes everything to go wrong afterwards, as two unexpected 0xFF responses are returned on the next SPI transaction (e.g. when reading the Flash ID). --- iceprog/mpsse.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/iceprog/mpsse.c b/iceprog/mpsse.c index 80d462f18cc..e81c73d1890 100644 --- a/iceprog/mpsse.c +++ b/iceprog/mpsse.c @@ -338,8 +338,11 @@ void mpsse_init(int ifnum, const char *devstr, bool slow_clock) mpsse_error(2); } - // enable clock divide by 5 - mpsse_send_byte(MC_TCK_D5); + // enable clock divide by 5 for USB-HS chips + if (mpsse_ftdic.type == TYPE_2232H || + mpsse_ftdic.type == TYPE_4232H || + mpsse_ftdic.type == TYPE_232H) + mpsse_send_byte(MC_TCK_D5); if (slow_clock) { // set 50 kHz clock From 74d54d6c131727c0a49ef7c0f2efc29da2a8e03e Mon Sep 17 00:00:00 2001 From: Joerg Wunsch Date: Sun, 21 Jun 2020 23:43:46 +0200 Subject: [PATCH 2/4] Replace mpsse_send_dummy_{bits,bytes} by mpsse_send_spi The "dummy" functions use commands that are only available on USB-HS chips. They do nothing on an FT2232D, resulting in no dummy bits sent at all. --- iceprog/iceprog.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index b04fe6573e9..20a3a5c5888 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -155,6 +155,13 @@ static void sram_chip_select() set_cs_creset(0, 1); } +// SRAM chip deselect assert +// Same as flash_release_reset() +static void sram_chip_deselect() +{ + set_cs_creset(1, 1); +} + static void flash_read_id() { /* JEDEC ID structure: @@ -881,10 +888,18 @@ int main(int argc, char **argv) mpsse_send_spi(buffer, rc); } - mpsse_send_dummy_bytes(6); - mpsse_send_dummy_bit(); - fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); + + sram_chip_deselect(); + + // "Issue at least 49 dummy bits after CDONE went + // high." + // mpsse_send_dummy_{bits,bytes} is only available on + // USB-HS FTDI chips, not on FT2232D. Just send 56 + // zero instead. + uint8_t dummy[7] = { 0 }; + mpsse_send_spi(dummy, 7); + } else /* program flash */ { From 380246ed8a8f81d80738bfcad3b15c316e590007 Mon Sep 17 00:00:00 2001 From: Joerg Wunsch Date: Sun, 21 Jun 2020 23:45:07 +0200 Subject: [PATCH 3/4] Add signature reading for SRAM operation This uses the undocumented Opcode 0 / Payload 9, returning 4 bytes of an ID code (somehow). Returned bytes are all 0xFF if the SRAM/Flash jumpers on the board are misconfigured, so this can be used as an indication to the user. (Observed by Logic Analyzer trace on Radiant programmer.) --- iceprog/iceprog.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index 20a3a5c5888..e1d9d5f3466 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -872,6 +872,28 @@ int main(int argc, char **argv) fprintf(stderr, "cdone: %s\n", get_cdone() ? "high" : "low"); + sram_chip_deselect(); + usleep(100); + sram_chip_select(); + + uint8_t sig[] = { 0x7e, 0xaa, 0x99, 0x7e, // key + 0x01, 0x09, // command + 0, 0, 0, 0 }; // room for result + mpsse_xfer_spi(sig, sizeof sig); + fprintf(stderr, "Signature read: "); + for (int i = 0; i < 4; i++) + fprintf(stderr, "0x%02x ", sig[i + 6]); + fprintf(stderr, "\n"); + + if (sig[6] == 0xff && sig[7] == 0xff && + sig[8] == 0xff && sig[9] == 0xff) + fprintf(stderr, + "WARNING: Signature is 0xFFFFFFFF. " + "Are CRAM jumpers set correctly?\n"); + + sram_chip_deselect(); + usleep(100); + sram_chip_select(); // --------------------------------------------------------- // Program From 9a99c43c2a2dece8a666eb87cf62e35a3c6c90c9 Mon Sep 17 00:00:00 2001 From: Joerg Wunsch Date: Mon, 22 Jun 2020 14:39:22 +0200 Subject: [PATCH 4/4] Push SRAM signature read into its own function Suggested by: smunaut --- iceprog/iceprog.c | 48 +++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/iceprog/iceprog.c b/iceprog/iceprog.c index e1d9d5f3466..ca08965b5e8 100644 --- a/iceprog/iceprog.c +++ b/iceprog/iceprog.c @@ -453,6 +453,37 @@ static void flash_disable_protection() } +/* + * Reading the SRAM / FPGA signature is not documented, but tracing + * shows the Radiant programmer just does it prior to programming. + * + * It's a good indication to verify the board is jumpered correctly + * for SRAM (as opposed to flash) access. + */ +static void sram_read_signature() +{ + uint8_t sig[] = { 0x7e, 0xaa, 0x99, 0x7e, // key + 0x01, 0x09, // command + 0, 0, 0, 0 }; // room for result + + sram_chip_select(); + + mpsse_xfer_spi(sig, sizeof sig); + fprintf(stderr, "Signature read: "); + for (int i = 0; i < 4; i++) + fprintf(stderr, "0x%02x ", sig[i + 6]); + fprintf(stderr, "\n"); + + if (sig[6] == 0xff && sig[7] == 0xff && + sig[8] == 0xff && sig[9] == 0xff) + fprintf(stderr, + "WARNING: Signature is 0xFFFFFFFF. " + "Are CRAM jumpers set correctly?\n"); + + sram_chip_deselect(); +} + + // --------------------------------------------------------- // iceprog implementation // --------------------------------------------------------- @@ -874,24 +905,9 @@ int main(int argc, char **argv) sram_chip_deselect(); usleep(100); - sram_chip_select(); - - uint8_t sig[] = { 0x7e, 0xaa, 0x99, 0x7e, // key - 0x01, 0x09, // command - 0, 0, 0, 0 }; // room for result - mpsse_xfer_spi(sig, sizeof sig); - fprintf(stderr, "Signature read: "); - for (int i = 0; i < 4; i++) - fprintf(stderr, "0x%02x ", sig[i + 6]); - fprintf(stderr, "\n"); - if (sig[6] == 0xff && sig[7] == 0xff && - sig[8] == 0xff && sig[9] == 0xff) - fprintf(stderr, - "WARNING: Signature is 0xFFFFFFFF. " - "Are CRAM jumpers set correctly?\n"); + sram_read_signature(); - sram_chip_deselect(); usleep(100); sram_chip_select();