47e31033a095183c16d3bbb4678576397104960c
2 * ecpprog -- simple programming tool for FTDI-based JTAG programmers
5 * Copyright (C) 2015 Clifford Wolf <clifford@clifford.at>
6 * Copyright (C) 2018 Piotr Esden-Tempski <piotr@esden.net>
7 * Copyright (C) 2020 Gregory Davill <greg.davill@gmail.com>
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * http://www.latticesemi.com/~/media/Documents/UserManuals/EI/icestickusermanual.pdf
24 * http://www.micron.com/~/media/documents/products/data-sheet/nor-flash/serial-nor/n25q/n25q_32mb_3v_65nm.pdf
37 #include <sys/types.h>
41 #include <io.h> /* _setmode() */
42 #include <fcntl.h> /* _O_BINARY */
46 #include "lattice_cmds.h"
48 static bool verbose
= false;
50 // ---------------------------------------------------------
52 // ---------------------------------------------------------
54 /* Flash command definitions */
55 /* This command list is based on the Winbond W25Q128JV Datasheet */
57 FC_WE
= 0x06, /* Write Enable */
58 FC_SRWE
= 0x50, /* Volatile SR Write Enable */
59 FC_WD
= 0x04, /* Write Disable */
60 FC_RPD
= 0xAB, /* Release Power-Down, returns Device ID */
61 FC_MFGID
= 0x90, /* Read Manufacturer/Device ID */
62 FC_JEDECID
= 0x9F, /* Read JEDEC ID */
63 FC_UID
= 0x4B, /* Read Unique ID */
64 FC_RD
= 0x03, /* Read Data */
65 FC_FR
= 0x0B, /* Fast Read */
66 FC_PP
= 0x02, /* Page Program */
67 FC_SE
= 0x20, /* Sector Erase 4kb */
68 FC_BE32
= 0x52, /* Block Erase 32kb */
69 FC_BE64
= 0xD8, /* Block Erase 64kb */
70 FC_CE
= 0xC7, /* Chip Erase */
71 FC_RSR1
= 0x05, /* Read Status Register 1 */
72 FC_WSR1
= 0x01, /* Write Status Register 1 */
73 FC_RSR2
= 0x35, /* Read Status Register 2 */
74 FC_WSR2
= 0x31, /* Write Status Register 2 */
75 FC_RSR3
= 0x15, /* Read Status Register 3 */
76 FC_WSR3
= 0x11, /* Write Status Register 3 */
77 FC_RSFDP
= 0x5A, /* Read SFDP Register */
78 FC_ESR
= 0x44, /* Erase Security Register */
79 FC_PSR
= 0x42, /* Program Security Register */
80 FC_RSR
= 0x48, /* Read Security Register */
81 FC_GBL
= 0x7E, /* Global Block Lock */
82 FC_GBU
= 0x98, /* Global Block Unlock */
83 FC_RBL
= 0x3D, /* Read Block Lock */
84 FC_RPR
= 0x3C, /* Read Sector Protection Registers (adesto) */
85 FC_IBL
= 0x36, /* Individual Block Lock */
86 FC_IBU
= 0x39, /* Individual Block Unlock */
87 FC_EPS
= 0x75, /* Erase / Program Suspend */
88 FC_EPR
= 0x7A, /* Erase / Program Resume */
89 FC_PD
= 0xB9, /* Power-down */
90 FC_QPI
= 0x38, /* Enter QPI mode */
91 FC_ERESET
= 0x66, /* Enable Reset */
92 FC_RESET
= 0x99, /* Reset Device */
96 // ---------------------------------------------------------
97 // JTAG -> SPI functions
98 // ---------------------------------------------------------
101 * JTAG performrs all shifts LSB first, our FLSAH is expeting bytes MSB first,
102 * There are a few ways to fix this, for now we just bit-reverse all the input data to the JTAG core
104 uint8_t bit_reverse(uint8_t in
){
106 uint8_t out
= (in
& 0x01) ? 0x80 : 0x00;
107 out
|= (in
& 0x02) ? 0x40 : 0x00;
108 out
|= (in
& 0x04) ? 0x20 : 0x00;
109 out
|= (in
& 0x08) ? 0x10 : 0x00;
110 out
|= (in
& 0x10) ? 0x08 : 0x00;
111 out
|= (in
& 0x20) ? 0x04 : 0x00;
112 out
|= (in
& 0x40) ? 0x02 : 0x00;
113 out
|= (in
& 0x80) ? 0x01 : 0x00;
118 void xfer_spi(uint8_t* data
, uint32_t len
){
119 /* Reverse bit order of all bytes */
120 for(int i
= 0; i
< len
; i
++){
121 data
[i
] = bit_reverse(data
[i
]);
124 /* Don't switch states if we're already in SHIFT-DR */
125 if(jtag_current_state() != STATE_SHIFT_DR
)
126 jtag_go_to_state(STATE_SHIFT_DR
);
127 jtag_tap_shift(data
, data
, len
* 8, true);
129 /* Reverse bit order of all return bytes */
130 for(int i
= 0; i
< len
; i
++){
131 data
[i
] = bit_reverse(data
[i
]);
135 void send_spi(uint8_t* data
, uint32_t len
){
137 /* Flip bit order of all bytes */
138 for(int i
= 0; i
< len
; i
++){
139 data
[i
] = bit_reverse(data
[i
]);
142 jtag_go_to_state(STATE_SHIFT_DR
);
143 /* Stay in SHIFT-DR state, this keep CS low */
144 jtag_tap_shift(data
, data
, len
* 8, false);
146 /* Flip bit order of all bytes */
147 for(int i
= 0; i
< len
; i
++){
148 data
[i
] = bit_reverse(data
[i
]);
153 // ---------------------------------------------------------
154 // FLASH function implementations
155 // ---------------------------------------------------------
157 static void flash_read_id()
159 /* JEDEC ID structure:
160 * Byte No. | Data Type
161 * ---------+----------
162 * 0 | FC_JEDECID Request Command
166 * 4 | Ext Dev Str Len
169 uint8_t data
[260] = { FC_JEDECID
};
170 int len
= 5; // command + 4 response bytes
173 fprintf(stderr
, "read flash ID..\n");
175 // Write command and read first 4 bytes
179 fprintf(stderr
, "Extended Device String Length is 0xFF, "
180 "this is likely a read error. Ignorig...\n");
182 // Read extended JEDEC ID bytes
185 data
[0] = FC_JEDECID
;
190 fprintf(stderr
, "flash ID:");
191 for (int i
= 1; i
< len
; i
++)
192 fprintf(stderr
, " 0x%02X", data
[i
]);
193 fprintf(stderr
, "\n");
196 static void flash_reset()
198 uint8_t data
[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
202 static uint8_t flash_read_status()
204 uint8_t data
[2] = { FC_RSR1
};
209 fprintf(stderr
, "SR1: 0x%02X\n", data
[1]);
210 fprintf(stderr
, " - SPRL: %s\n",
211 ((data
[1] & (1 << 7)) == 0) ?
214 fprintf(stderr
, " - SPM: %s\n",
215 ((data
[1] & (1 << 6)) == 0) ?
216 "Byte/Page Prog Mode" :
217 "Sequential Prog Mode");
218 fprintf(stderr
, " - EPE: %s\n",
219 ((data
[1] & (1 << 5)) == 0) ?
220 "Erase/Prog success" :
222 fprintf(stderr
, "- SPM: %s\n",
223 ((data
[1] & (1 << 4)) == 0) ?
226 fprintf(stderr
, " - SWP: ");
227 switch((data
[1] >> 2) & 0x3) {
229 fprintf(stderr
, "All sectors unprotected\n");
232 fprintf(stderr
, "Some sectors protected\n");
235 fprintf(stderr
, "Reserved (xxxx 10xx)\n");
238 fprintf(stderr
, "All sectors protected\n");
241 fprintf(stderr
, " - WEL: %s\n",
242 ((data
[1] & (1 << 1)) == 0) ?
243 "Not write enabled" :
245 fprintf(stderr
, " - ~RDY: %s\n",
246 ((data
[1] & (1 << 0)) == 0) ?
254 static void flash_write_enable()
257 fprintf(stderr
, "status before enable:\n");
262 fprintf(stderr
, "write enable..\n");
264 uint8_t data
[1] = { FC_WE
};
268 fprintf(stderr
, "status after enable:\n");
273 static void flash_bulk_erase()
275 fprintf(stderr
, "bulk erase..\n");
277 uint8_t data
[1] = { FC_CE
};
281 static void flash_4kB_sector_erase(int addr
)
283 fprintf(stderr
, "erase 4kB sector at 0x%06X..\n", addr
);
285 uint8_t command
[4] = { FC_SE
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
287 xfer_spi(command
, 4);
290 static void flash_32kB_sector_erase(int addr
)
292 fprintf(stderr
, "erase 64kB sector at 0x%06X..\n", addr
);
294 uint8_t command
[4] = { FC_BE32
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
296 xfer_spi(command
, 4);
299 static void flash_64kB_sector_erase(int addr
)
301 fprintf(stderr
, "erase 64kB sector at 0x%06X..\n", addr
);
303 uint8_t command
[4] = { FC_BE64
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
305 xfer_spi(command
, 4);
308 static void flash_prog(int addr
, uint8_t *data
, int n
)
311 fprintf(stderr
, "prog 0x%06X +0x%03X..\n", addr
, n
);
313 uint8_t command
[4] = { FC_PP
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
315 send_spi(command
, 4);
319 for (int i
= 0; i
< n
; i
++)
320 fprintf(stderr
, "%02x%c", data
[i
], i
== n
- 1 || i
% 32 == 31 ? '\n' : ' ');
324 static void flash_start_read(int addr
)
327 fprintf(stderr
, "Start Read 0x%06X\n", addr
);
329 uint8_t command
[4] = { FC_RD
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
331 send_spi(command
, 4);
334 static void flash_continue_read(uint8_t *data
, int n
)
337 fprintf(stderr
, "Contiune Read +0x%03X..\n", n
);
343 for (int i
= 0; i
< n
; i
++)
344 fprintf(stderr
, "%02x%c", data
[i
], i
== n
- 1 || i
% 32 == 31 ? '\n' : ' ');
347 static void flash_read(int addr
, uint8_t *data
, int n
)
350 fprintf(stderr
, "read 0x%06X +0x%03X..\n", addr
, n
);
352 uint8_t command
[4] = { FC_RD
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
354 send_spi(command
, 4);
359 for (int i
= 0; i
< n
; i
++)
360 fprintf(stderr
, "%02x%c", data
[i
], i
== n
- 1 || i
% 32 == 31 ? '\n' : ' ');
363 static void flash_wait()
366 fprintf(stderr
, "waiting..");
371 uint8_t data
[2] = { FC_RSR1
};
375 if ((data
[1] & 0x01) == 0) {
379 fprintf(stderr
, "r");
384 fprintf(stderr
, "R");
391 fprintf(stderr
, ".");
401 fprintf(stderr
, "\n");
405 static void flash_disable_protection()
407 fprintf(stderr
, "disable flash protection...\n");
409 // Write Status Register 1 <- 0x00
410 uint8_t data
[2] = { FC_WSR1
, 0x00 };
415 // Read Status Register 1
421 fprintf(stderr
, "failed to disable protection, SR now equal to 0x%02x (expected 0x00)\n", data
[1]);
425 // ---------------------------------------------------------
426 // ECP5 specific JTAG functions
427 // ---------------------------------------------------------
430 static void print_idcode(uint32_t idcode
){
431 for(int i
= 0; i
< sizeof(ecp_devices
)/sizeof(struct ecp_device_id
); i
++){
432 if(idcode
== ecp_devices
[i
].device_id
)
434 printf("IDCODE: 0x%08x (%s)\n", idcode
,ecp_devices
[i
].device_name
);
438 printf("IDCODE: 0x%08x does not match :(\n", idcode
);
441 static void read_idcode(){
443 uint8_t data
[4] = {READ_ID
};
445 jtag_go_to_state(STATE_SHIFT_IR
);
446 jtag_tap_shift(data
, data
, 8, true);
449 jtag_go_to_state(STATE_SHIFT_DR
);
450 jtag_tap_shift(data
, data
, 32, true);
454 /* Format the IDCODE into a 32bit value */
455 for(int i
= 0; i
< 4; i
++)
456 idcode
= data
[i
] << 24 | idcode
>> 8;
458 print_idcode(idcode
);
462 static void print_status_register(uint32_t status
){
463 printf("ECP5 Status Register: 0x%08x\n", status
);
466 printf(" Transparent Mode: %s\n", status
& (1 << 0) ? "Yes" : "No" );
467 printf(" Config Target: %s\n", status
& (7 << 1) ? "eFuse" : "SRAM" );
468 printf(" JTAG Active: %s\n", status
& (1 << 4) ? "Yes" : "No" );
469 printf(" PWD Protection: %s\n", status
& (1 << 5) ? "Yes" : "No" );
470 printf(" Decrypt Enable: %s\n", status
& (1 << 7) ? "Yes" : "No" );
471 printf(" DONE: %s\n", status
& (1 << 8) ? "Yes" : "No" );
472 printf(" ISC Enable: %s\n", status
& (1 << 9) ? "Yes" : "No" );
473 printf(" Write Enable: %s\n", status
& (1 << 10) ? "Writable" : "Not Writable");
474 printf(" Read Enable: %s\n", status
& (1 << 11) ? "Readable" : "Not Readable");
475 printf(" Busy Flag: %s\n", status
& (1 << 12) ? "Yes" : "No" );
476 printf(" Fail Flag: %s\n", status
& (1 << 13) ? "Yes" : "No" );
477 printf(" Feature OTP: %s\n", status
& (1 << 14) ? "Yes" : "No" );
478 printf(" Decrypt Only: %s\n", status
& (1 << 15) ? "Yes" : "No" );
479 printf(" PWD Enable: %s\n", status
& (1 << 16) ? "Yes" : "No" );
480 printf(" Encrypt Preamble: %s\n", status
& (1 << 20) ? "Yes" : "No" );
481 printf(" Std Preamble: %s\n", status
& (1 << 21) ? "Yes" : "No" );
482 printf(" SPIm Fail 1: %s\n", status
& (1 << 22) ? "Yes" : "No" );
484 uint8_t bse_error
= (status
& (7 << 23)) >> 23;
486 case 0b000: printf(" BSE Error Code: No Error (0b000)\n"); break;
487 case 0b001: printf(" BSE Error Code: ID Error (0b001)\n"); break;
488 case 0b010: printf(" BSE Error Code: CMD Error - illegal command (0b010)\n"); break;
489 case 0b011: printf(" BSE Error Code: CRC Error (0b011)\n"); break;
490 case 0b100: printf(" BSE Error Code: PRMB Error - preamble error (0b100)\n"); break;
491 case 0b101: printf(" BSE Error Code: ABRT Error - configuration aborted by the user (0b101)\n"); break;
492 case 0b110: printf(" BSE Error Code: OVFL Error - data overflow error (0b110)\n"); break;
493 case 0b111: printf(" BSE Error Code: SDM Error - bitstream pass the size of SRAM array (0b111)\n"); break;
496 printf(" Execution Error: %s\n", status
& (1 << 26) ? "Yes" : "No" );
497 printf(" ID Error: %s\n", status
& (1 << 27) ? "Yes" : "No" );
498 printf(" Invalid Command: %s\n", status
& (1 << 28) ? "Yes" : "No" );
499 printf(" SED Error: %s\n", status
& (1 << 29) ? "Yes" : "No" );
500 printf(" Bypass Mode: %s\n", status
& (1 << 30) ? "Yes" : "No" );
501 printf(" Flow Through Mode: %s\n", status
& (1 << 31) ? "Yes" : "No" );
506 static void read_status_register(){
508 uint8_t data
[4] = {LSC_READ_STATUS
};
510 jtag_go_to_state(STATE_SHIFT_IR
);
511 jtag_tap_shift(data
, data
, 8, true);
514 jtag_go_to_state(STATE_SHIFT_DR
);
515 jtag_tap_shift(data
, data
, 32, true);
516 //jtag_go_to_state(STATE_PAUSE_DR);
520 /* Format the IDCODE into a 32bit value */
521 for(int i
= 0; i
< 4; i
++)
522 status
= data
[i
] << 24 | status
>> 8;
524 print_status_register(status
);
529 static void enter_spi_background_mode(){
531 uint8_t data
[4] = {0x3A};
533 jtag_go_to_state(STATE_SHIFT_IR
);
534 jtag_tap_shift(data
, data
, 8, true);
536 /* These bytes seem to be required to un-lock the SPI interface */
539 jtag_go_to_state(STATE_SHIFT_DR
);
540 jtag_tap_shift(data
, data
, 16, true);
542 /* Entering IDLE is essential */
543 jtag_go_to_state(STATE_RUN_TEST_IDLE
);
547 void ecp_jtag_cmd(uint8_t cmd
){
548 uint8_t data
[1] = {cmd
};
550 jtag_go_to_state(STATE_SHIFT_IR
);
551 jtag_tap_shift(data
, data
, 8, true);
553 jtag_go_to_state(STATE_RUN_TEST_IDLE
);
557 void ecp_jtag_cmd8(uint8_t cmd
, uint8_t param
){
558 uint8_t data
[1] = {cmd
};
560 jtag_go_to_state(STATE_SHIFT_IR
);
561 jtag_tap_shift(data
, data
, 8, true);
564 jtag_go_to_state(STATE_SHIFT_DR
);
565 jtag_tap_shift(data
, data
, 8, true);
567 jtag_go_to_state(STATE_RUN_TEST_IDLE
);
571 // ---------------------------------------------------------
572 // iceprog implementation
573 // ---------------------------------------------------------
575 static void help(const char *progname
)
577 fprintf(stderr
, "Simple programming tool for FTDI-based Lattice ECP JTAG programmers.\n");
578 fprintf(stderr
, "Usage: %s [-b|-n|-c] <input file>\n", progname
);
579 fprintf(stderr
, " %s -r|-R<bytes> <output file>\n", progname
);
580 fprintf(stderr
, " %s -S <input file>\n", progname
);
581 fprintf(stderr
, " %s -t\n", progname
);
582 fprintf(stderr
, "\n");
583 fprintf(stderr
, "General options:\n");
584 fprintf(stderr
, " -d <device string> use the specified USB device [default: i:0x0403:0x6010 or i:0x0403:0x6014]\n");
585 fprintf(stderr
, " d:<devicenode> (e.g. d:002/005)\n");
586 fprintf(stderr
, " i:<vendor>:<product> (e.g. i:0x0403:0x6010)\n");
587 fprintf(stderr
, " i:<vendor>:<product>:<index> (e.g. i:0x0403:0x6010:0)\n");
588 fprintf(stderr
, " s:<vendor>:<product>:<serial-string>\n");
589 fprintf(stderr
, " -I [ABCD] connect to the specified interface on the FTDI chip\n");
590 fprintf(stderr
, " [default: A]\n");
591 fprintf(stderr
, " -o <offset in bytes> start address for read/write [default: 0]\n");
592 fprintf(stderr
, " (append 'k' to the argument for size in kilobytes,\n");
593 fprintf(stderr
, " or 'M' for size in megabytes)\n");
594 fprintf(stderr
, " -s slow SPI (50 kHz instead of 6 MHz)\n");
595 fprintf(stderr
, " -v verbose output\n");
596 fprintf(stderr
, " -i [4,32,64] select erase block size [default: 64k]\n");
597 fprintf(stderr
, "\n");
598 fprintf(stderr
, "Mode of operation:\n");
599 fprintf(stderr
, " [default] write file contents to flash, then verify\n");
600 fprintf(stderr
, " -X write file contents to flash only\n");
601 fprintf(stderr
, " -r read first 256 kB from flash and write to file\n");
602 fprintf(stderr
, " -R <size in bytes> read the specified number of bytes from flash\n");
603 fprintf(stderr
, " (append 'k' to the argument for size in kilobytes,\n");
604 fprintf(stderr
, " or 'M' for size in megabytes)\n");
605 fprintf(stderr
, " -c do not write flash, only verify (`check')\n");
606 fprintf(stderr
, " -S perform SRAM programming\n");
607 fprintf(stderr
, " -t just read the flash ID sequence\n");
608 fprintf(stderr
, "\n");
609 fprintf(stderr
, "Erase mode (only meaningful in default mode):\n");
610 fprintf(stderr
, " [default] erase aligned chunks of 64kB in write mode\n");
611 fprintf(stderr
, " This means that some data after the written data (or\n");
612 fprintf(stderr
, " even before when -o is used) may be erased as well.\n");
613 fprintf(stderr
, " -b bulk erase entire flash before writing\n");
614 fprintf(stderr
, " -e <size in bytes> erase flash as if we were writing that number of bytes\n");
615 fprintf(stderr
, " -n do not erase flash before writing\n");
616 fprintf(stderr
, " -p disable write protection before erasing or writing\n");
617 fprintf(stderr
, " This can be useful if flash memory appears to be\n");
618 fprintf(stderr
, " bricked and won't respond to erasing or programming.\n");
619 fprintf(stderr
, "\n");
620 fprintf(stderr
, "Miscellaneous options:\n");
621 fprintf(stderr
, " --help display this help and exit\n");
622 fprintf(stderr
, " -- treat all remaining arguments as filenames\n");
623 fprintf(stderr
, "\n");
624 fprintf(stderr
, "Exit status:\n");
625 fprintf(stderr
, " 0 on success,\n");
626 fprintf(stderr
, " 1 if a non-hardware error occurred (e.g., failure to read from or\n");
627 fprintf(stderr
, " write to a file, or invoked with invalid options),\n");
628 fprintf(stderr
, " 2 if communication with the hardware failed (e.g., cannot find the\n");
629 fprintf(stderr
, " iCE FTDI USB device),\n");
630 fprintf(stderr
, " 3 if verification of the data failed.\n");
631 fprintf(stderr
, "\n");
632 fprintf(stderr
, "If you have a bug report, please file an issue on github:\n");
633 fprintf(stderr
, " https://github.com/gregdavill/ecpprog/issues\n");
636 int main(int argc
, char **argv
)
638 /* used for error reporting */
639 const char *my_name
= argv
[0];
640 for (size_t i
= 0; argv
[0][i
]; i
++)
641 if (argv
[0][i
] == '/')
642 my_name
= argv
[0] + i
+ 1;
644 int read_size
= 256 * 1024;
645 int erase_block_size
= 64;
649 bool read_mode
= false;
650 bool check_mode
= false;
651 bool erase_mode
= false;
652 bool bulk_erase
= false;
653 bool dont_erase
= false;
654 bool prog_sram
= false;
655 bool test_mode
= false;
656 bool slow_clock
= false;
657 bool disable_protect
= false;
658 bool disable_verify
= false;
659 const char *filename
= NULL
;
660 const char *devstr
= NULL
;
664 _setmode(_fileno(stdin
), _O_BINARY
);
665 _setmode(_fileno(stdout
), _O_BINARY
);
668 static struct option long_options
[] = {
669 {"help", no_argument
, NULL
, -2},
673 /* Decode command line parameters */
676 while ((opt
= getopt_long(argc
, argv
, "d:i:I:rR:e:o:cbnStvspX", long_options
, NULL
)) != -1) {
678 case 'd': /* device string */
681 case 'i': /* block erase size */
682 if (!strcmp(optarg
, "4"))
683 erase_block_size
= 4;
684 else if (!strcmp(optarg
, "32"))
685 erase_block_size
= 32;
686 else if (!strcmp(optarg
, "64"))
687 erase_block_size
= 64;
689 fprintf(stderr
, "%s: `%s' is not a valid erase block size (must be `4', `32' or `64')\n", my_name
, optarg
);
693 case 'I': /* FTDI Chip interface select */
694 if (!strcmp(optarg
, "A"))
696 else if (!strcmp(optarg
, "B"))
698 else if (!strcmp(optarg
, "C"))
700 else if (!strcmp(optarg
, "D"))
703 fprintf(stderr
, "%s: `%s' is not a valid interface (must be `A', `B', `C', or `D')\n", my_name
, optarg
);
707 case 'r': /* Read 256 bytes to file */
710 case 'R': /* Read n bytes to file */
712 read_size
= strtol(optarg
, &endptr
, 0);
715 else if (!strcmp(endptr
, "k"))
717 else if (!strcmp(endptr
, "M"))
718 read_size
*= 1024 * 1024;
720 fprintf(stderr
, "%s: `%s' is not a valid size\n", my_name
, optarg
);
724 case 'e': /* Erase blocks as if we were writing n bytes */
726 erase_size
= strtol(optarg
, &endptr
, 0);
729 else if (!strcmp(endptr
, "k"))
731 else if (!strcmp(endptr
, "M"))
732 erase_size
*= 1024 * 1024;
734 fprintf(stderr
, "%s: `%s' is not a valid size\n", my_name
, optarg
);
738 case 'o': /* set address offset */
739 rw_offset
= strtol(optarg
, &endptr
, 0);
742 else if (!strcmp(endptr
, "k"))
744 else if (!strcmp(endptr
, "M"))
745 rw_offset
*= 1024 * 1024;
747 fprintf(stderr
, "%s: `%s' is not a valid offset\n", my_name
, optarg
);
751 case 'c': /* do not write just check */
754 case 'b': /* bulk erase before writing */
757 case 'n': /* do not erase before writing */
760 case 'S': /* write to sram directly */
763 case 't': /* just read flash id */
766 case 'v': /* provide verbose output */
769 case 's': /* use slow SPI clock */
772 case 'p': /* disable flash protect before erase/write */
773 disable_protect
= true;
775 case 'X': /* disable verification */
776 disable_verify
= true;
782 /* error message has already been printed */
783 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
788 /* Make sure that the combination of provided parameters makes sense */
790 if (read_mode
+ erase_mode
+ check_mode
+ prog_sram
+ test_mode
> 1) {
791 fprintf(stderr
, "%s: options `-r'/`-R', `-e`, `-c', `-S', and `-t' are mutually exclusive\n", my_name
);
795 if (bulk_erase
&& dont_erase
) {
796 fprintf(stderr
, "%s: options `-b' and `-n' are mutually exclusive\n", my_name
);
800 if (disable_protect
&& (read_mode
|| check_mode
|| prog_sram
|| test_mode
)) {
801 fprintf(stderr
, "%s: option `-p' only valid in programming mode\n", my_name
);
805 if (bulk_erase
&& (read_mode
|| check_mode
|| prog_sram
|| test_mode
)) {
806 fprintf(stderr
, "%s: option `-b' only valid in programming mode\n", my_name
);
810 if (dont_erase
&& (read_mode
|| check_mode
|| prog_sram
|| test_mode
)) {
811 fprintf(stderr
, "%s: option `-n' only valid in programming mode\n", my_name
);
815 if (rw_offset
!= 0 && prog_sram
) {
816 fprintf(stderr
, "%s: option `-o' not supported in SRAM mode\n", my_name
);
820 if (rw_offset
!= 0 && test_mode
) {
821 fprintf(stderr
, "%s: option `-o' not supported in test mode\n", my_name
);
825 if (optind
+ 1 == argc
) {
827 fprintf(stderr
, "%s: test mode doesn't take a file name\n", my_name
);
828 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
831 filename
= argv
[optind
];
832 } else if (optind
!= argc
) {
833 fprintf(stderr
, "%s: too many arguments\n", my_name
);
834 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
836 } else if (bulk_erase
|| disable_protect
) {
837 filename
= "/dev/null";
838 } else if (!test_mode
&& !erase_mode
&& !disable_protect
) {
839 fprintf(stderr
, "%s: missing argument\n", my_name
);
840 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
844 /* open input/output file in advance
845 so we can fail before initializing the hardware */
852 } else if (erase_mode
) {
853 file_size
= erase_size
;
854 } else if (read_mode
) {
855 f
= (strcmp(filename
, "-") == 0) ? stdout
: fopen(filename
, "wb");
857 fprintf(stderr
, "%s: can't open '%s' for writing: ", my_name
, filename
);
862 f
= (strcmp(filename
, "-") == 0) ? stdin
: fopen(filename
, "rb");
864 fprintf(stderr
, "%s: can't open '%s' for reading: ", my_name
, filename
);
869 /* For regular programming, we need to read the file
870 twice--once for programming and once for verifying--and
871 need to know the file size in advance in order to erase
872 the correct amount of memory.
874 See if we can seek on the input file. Checking for "-"
875 as an argument isn't enough as we might be reading from a
876 named pipe, or contrarily, the standard input may be an
879 if (!prog_sram
&& !check_mode
) {
880 if (fseek(f
, 0L, SEEK_END
) != -1) {
881 file_size
= ftell(f
);
882 if (file_size
== -1) {
883 fprintf(stderr
, "%s: %s: ftell: ", my_name
, filename
);
887 if (fseek(f
, 0L, SEEK_SET
) == -1) {
888 fprintf(stderr
, "%s: %s: fseek: ", my_name
, filename
);
897 fprintf(stderr
, "%s: can't open temporary file\n", my_name
);
903 static unsigned char buffer
[4096];
904 size_t rc
= fread(buffer
, 1, 4096, pipe
);
907 size_t wc
= fwrite(buffer
, 1, rc
, f
);
909 fprintf(stderr
, "%s: can't write to temporary file\n", my_name
);
916 /* now seek to the beginning so we can
917 start reading again */
918 fseek(f
, 0, SEEK_SET
);
923 // ---------------------------------------------------------
924 // Initialize USB connection to FT2232H
925 // ---------------------------------------------------------
927 fprintf(stderr
, "init..\n");
928 jtag_init(ifnum
, devstr
, slow_clock
);
931 read_status_register();
935 /* Reset ECP5 to release SPI interface */
936 ecp_jtag_cmd(ISC_ENABLE
);
937 ecp_jtag_cmd(ISC_ERASE
);
938 ecp_jtag_cmd(ISC_DISABLE
);
940 /* Put device into SPI bypass mode */
941 enter_spi_background_mode();
948 // ---------------------------------------------------------
950 // ---------------------------------------------------------
951 fprintf(stderr
, "reset..\n");
953 ecp_jtag_cmd8(ISC_ENABLE
, 0);
954 ecp_jtag_cmd8(ISC_ERASE
, 0);
955 ecp_jtag_cmd8(LSC_RESET_CRC
, 0);
957 read_status_register();
959 // ---------------------------------------------------------
961 // ---------------------------------------------------------
963 fprintf(stderr
, "programming..\n");
964 ecp_jtag_cmd(LSC_BITSTREAM_BURST
);
966 const uint32_t len
= 16*1024;
967 static unsigned char buffer
[16*1024];
968 int rc
= fread(buffer
, 1, len
, f
);
972 fprintf(stderr
, "sending %d bytes.\n", rc
);
974 for(int i
= 0; i
< len
; i
++){
975 buffer
[i
] = bit_reverse(buffer
[i
]);
978 jtag_go_to_state(STATE_CAPTURE_DR
);
979 jtag_tap_shift(buffer
, buffer
, len
*8, false);
982 ecp_jtag_cmd(ISC_DISABLE
);
983 read_status_register();
985 else /* program flash */
987 // ---------------------------------------------------------
989 // ---------------------------------------------------------
991 fprintf(stderr
, "reset..\n");
992 /* Reset ECP5 to release SPI interface */
993 ecp_jtag_cmd8(ISC_ENABLE
, 0);
994 ecp_jtag_cmd8(ISC_ERASE
, 0);
995 ecp_jtag_cmd8(ISC_DISABLE
, 0);
997 /* Put device into SPI bypass mode */
998 enter_spi_background_mode();
1005 // ---------------------------------------------------------
1007 // ---------------------------------------------------------
1009 if (!read_mode
&& !check_mode
)
1011 if (disable_protect
)
1013 flash_write_enable();
1014 flash_disable_protection();
1021 flash_write_enable();
1027 fprintf(stderr
, "file size: %ld\n", file_size
);
1029 int block_size
= erase_block_size
<< 10;
1030 int block_mask
= block_size
- 1;
1031 int begin_addr
= rw_offset
& ~block_mask
;
1032 int end_addr
= (rw_offset
+ file_size
+ block_mask
) & ~block_mask
;
1034 for (int addr
= begin_addr
; addr
< end_addr
; addr
+= block_size
) {
1035 flash_write_enable();
1036 switch(erase_block_size
) {
1038 flash_4kB_sector_erase(addr
);
1041 flash_32kB_sector_erase(addr
);
1044 flash_64kB_sector_erase(addr
);
1048 fprintf(stderr
, "Status after block erase:\n");
1049 flash_read_status();
1058 fprintf(stderr
, "programming..\n");
1060 for (int rc
, addr
= 0; true; addr
+= rc
) {
1061 uint8_t buffer
[256];
1062 int page_size
= 256 - (rw_offset
+ addr
) % 256;
1063 rc
= fread(buffer
, 1, page_size
, f
);
1066 flash_write_enable();
1067 flash_prog(rw_offset
+ addr
, buffer
, rc
);
1071 /* seek to the beginning for second pass */
1072 fseek(f
, 0, SEEK_SET
);
1076 // ---------------------------------------------------------
1078 // ---------------------------------------------------------
1081 fprintf(stderr
, "reading..\n");
1083 flash_start_read(rw_offset
);
1084 for (int addr
= 0; addr
< read_size
; addr
+= 4096) {
1085 uint8_t buffer
[4096];
1086 flash_continue_read(buffer
, 4096);
1087 fwrite(buffer
, read_size
- addr
> 4096 ? 4096 : read_size
- addr
, 1, f
);
1089 } else if (!erase_mode
&& !disable_verify
) {
1090 fprintf(stderr
, "reading..\n");
1091 for (int addr
= 0; true; addr
+= 256) {
1092 uint8_t buffer_flash
[256], buffer_file
[256];
1093 int rc
= fread(buffer_file
, 1, 256, f
);
1096 flash_read(rw_offset
+ addr
, buffer_flash
, rc
);
1097 if (memcmp(buffer_file
, buffer_flash
, rc
)) {
1098 fprintf(stderr
, "Found difference between flash and file!\n");
1103 fprintf(stderr
, "VERIFY OK\n");
1107 if (f
!= NULL
&& f
!= stdin
&& f
!= stdout
)
1110 // ---------------------------------------------------------
1112 // ---------------------------------------------------------
1114 fprintf(stderr
, "Bye.\n");