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 */
47 #include "lattice_cmds.h"
49 static bool verbose
= false;
51 // ---------------------------------------------------------
53 // ---------------------------------------------------------
55 /* Flash command definitions */
56 /* This command list is based on the Winbond W25Q128JV Datasheet */
58 FC_WE
= 0x06, /* Write Enable */
59 FC_SRWE
= 0x50, /* Volatile SR Write Enable */
60 FC_WD
= 0x04, /* Write Disable */
61 FC_RPD
= 0xAB, /* Release Power-Down, returns Device ID */
62 FC_MFGID
= 0x90, /* Read Manufacturer/Device ID */
63 FC_JEDECID
= 0x9F, /* Read JEDEC ID */
64 FC_UID
= 0x4B, /* Read Unique ID */
65 FC_RD
= 0x03, /* Read Data */
66 FC_FR
= 0x0B, /* Fast Read */
67 FC_PP
= 0x02, /* Page Program */
68 FC_SE
= 0x20, /* Sector Erase 4kb */
69 FC_BE32
= 0x52, /* Block Erase 32kb */
70 FC_BE64
= 0xD8, /* Block Erase 64kb */
71 FC_CE
= 0xC7, /* Chip Erase */
72 FC_RSR1
= 0x05, /* Read Status Register 1 */
73 FC_WSR1
= 0x01, /* Write Status Register 1 */
74 FC_RSR2
= 0x35, /* Read Status Register 2 */
75 FC_WSR2
= 0x31, /* Write Status Register 2 */
76 FC_RSR3
= 0x15, /* Read Status Register 3 */
77 FC_WSR3
= 0x11, /* Write Status Register 3 */
78 FC_RSFDP
= 0x5A, /* Read SFDP Register */
79 FC_ESR
= 0x44, /* Erase Security Register */
80 FC_PSR
= 0x42, /* Program Security Register */
81 FC_RSR
= 0x48, /* Read Security Register */
82 FC_GBL
= 0x7E, /* Global Block Lock */
83 FC_GBU
= 0x98, /* Global Block Unlock */
84 FC_RBL
= 0x3D, /* Read Block Lock */
85 FC_RPR
= 0x3C, /* Read Sector Protection Registers (adesto) */
86 FC_IBL
= 0x36, /* Individual Block Lock */
87 FC_IBU
= 0x39, /* Individual Block Unlock */
88 FC_EPS
= 0x75, /* Erase / Program Suspend */
89 FC_EPR
= 0x7A, /* Erase / Program Resume */
90 FC_PD
= 0xB9, /* Power-down */
91 FC_QPI
= 0x38, /* Enter QPI mode */
92 FC_ERESET
= 0x66, /* Enable Reset */
93 FC_RESET
= 0x99, /* Reset Device */
96 // ---------------------------------------------------------
97 // FLASH function implementations
98 // ---------------------------------------------------------
100 static void flash_read_id()
102 /* JEDEC ID structure:
103 * Byte No. | Data Type
104 * ---------+----------
105 * 0 | FC_JEDECID Request Command
109 * 4 | Ext Dev Str Len
112 uint8_t data
[260] = { FC_JEDECID
};
113 int len
= 5; // command + 4 response bytes
116 fprintf(stderr
, "read flash ID..\n");
118 //flash_chip_select();
120 // Write command and read first 4 bytes
121 //mpsse_xfer_spi(data, len);
123 //jtag_go_to_state(STATE_SHIFT_DR);
124 //jtag_tap_shift(data, data, 8*5, false);
127 fprintf(stderr
, "Extended Device String Length is 0xFF, "
128 "this is likely a read error. Ignorig...\n");
130 // Read extended JEDEC ID bytes
133 data
[0] = FC_JEDECID
;
138 ////flash_chip_deselect();
140 // TODO: Add full decode of the JEDEC ID.
141 fprintf(stderr
, "flash ID:");
142 for (int i
= 1; i
< len
; i
++)
143 fprintf(stderr
, " 0x%02X", data
[i
]);
144 fprintf(stderr
, "\n");
147 static void flash_reset()
149 uint8_t data
[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
153 static void flash_power_up()
155 uint8_t data_rpd
[1] = { FC_RPD
};
156 xfer_spi(data_rpd
, 1);
159 static void flash_power_down()
161 uint8_t data
[1] = { FC_PD
};
162 jtag_go_to_state(STATE_SHIFT_DR
);
163 jtag_tap_shift(data
, data
, 8, true);
166 static uint8_t flash_read_status()
168 uint8_t data
[2] = { FC_RSR1
};
173 fprintf(stderr
, "SR1: 0x%02X\n", data
[1]);
174 fprintf(stderr
, " - SPRL: %s\n",
175 ((data
[1] & (1 << 7)) == 0) ?
178 fprintf(stderr
, " - SPM: %s\n",
179 ((data
[1] & (1 << 6)) == 0) ?
180 "Byte/Page Prog Mode" :
181 "Sequential Prog Mode");
182 fprintf(stderr
, " - EPE: %s\n",
183 ((data
[1] & (1 << 5)) == 0) ?
184 "Erase/Prog success" :
186 fprintf(stderr
, "- SPM: %s\n",
187 ((data
[1] & (1 << 4)) == 0) ?
190 fprintf(stderr
, " - SWP: ");
191 switch((data
[1] >> 2) & 0x3) {
193 fprintf(stderr
, "All sectors unprotected\n");
196 fprintf(stderr
, "Some sectors protected\n");
199 fprintf(stderr
, "Reserved (xxxx 10xx)\n");
202 fprintf(stderr
, "All sectors protected\n");
205 fprintf(stderr
, " - WEL: %s\n",
206 ((data
[1] & (1 << 1)) == 0) ?
207 "Not write enabled" :
209 fprintf(stderr
, " - ~RDY: %s\n",
210 ((data
[1] & (1 << 0)) == 0) ?
220 static void flash_write_enable()
223 fprintf(stderr
, "status before enable:\n");
228 fprintf(stderr
, "write enable..\n");
230 uint8_t data
[1] = { FC_WE
};
231 //flash_chip_select();
232 mpsse_xfer_spi(data
, 1);
233 //flash_chip_deselect();
236 fprintf(stderr
, "status after enable:\n");
241 static void flash_bulk_erase()
243 fprintf(stderr
, "bulk erase..\n");
245 uint8_t data
[1] = { FC_CE
};
246 //flash_chip_select();
247 mpsse_xfer_spi(data
, 1);
248 //flash_chip_deselect();
251 static void flash_4kB_sector_erase(int addr
)
253 fprintf(stderr
, "erase 4kB sector at 0x%06X..\n", addr
);
255 uint8_t command
[4] = { FC_SE
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
257 //flash_chip_select();
258 mpsse_send_spi(command
, 4);
259 //flash_chip_deselect();
262 static void flash_32kB_sector_erase(int addr
)
264 fprintf(stderr
, "erase 64kB sector at 0x%06X..\n", addr
);
266 uint8_t command
[4] = { FC_BE32
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
268 //flash_chip_select();
269 mpsse_send_spi(command
, 4);
270 //flash_chip_deselect();
273 static void flash_64kB_sector_erase(int addr
)
275 fprintf(stderr
, "erase 64kB sector at 0x%06X..\n", addr
);
277 uint8_t command
[4] = { FC_BE64
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
279 //flash_chip_select();
280 mpsse_send_spi(command
, 4);
281 //flash_chip_deselect();
284 static void flash_prog(int addr
, uint8_t *data
, int n
)
287 fprintf(stderr
, "prog 0x%06X +0x%03X..\n", addr
, n
);
289 uint8_t command
[4] = { FC_PP
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
291 //flash_chip_select();
292 mpsse_send_spi(command
, 4);
293 mpsse_send_spi(data
, n
);
294 //flash_chip_deselect();
297 for (int i
= 0; i
< n
; i
++)
298 fprintf(stderr
, "%02x%c", data
[i
], i
== n
- 1 || i
% 32 == 31 ? '\n' : ' ');
301 static void flash_read(int addr
, uint8_t *data
, int n
)
304 fprintf(stderr
, "read 0x%06X +0x%03X..\n", addr
, n
);
306 uint8_t command
[4] = { FC_RD
, (uint8_t)(addr
>> 16), (uint8_t)(addr
>> 8), (uint8_t)addr
};
308 //flash_chip_select();
309 mpsse_send_spi(command
, 4);
311 mpsse_xfer_spi(data
, n
);
312 //flash_chip_deselect();
315 for (int i
= 0; i
< n
; i
++)
316 fprintf(stderr
, "%02x%c", data
[i
], i
== n
- 1 || i
% 32 == 31 ? '\n' : ' ');
319 static void flash_wait()
322 fprintf(stderr
, "waiting..");
327 uint8_t data
[2] = { FC_RSR1
};
329 //flash_chip_select();
330 mpsse_xfer_spi(data
, 2);
331 //flash_chip_deselect();
333 if ((data
[1] & 0x01) == 0) {
337 fprintf(stderr
, "r");
342 fprintf(stderr
, "R");
349 fprintf(stderr
, ".");
359 fprintf(stderr
, "\n");
363 static void flash_disable_protection()
365 fprintf(stderr
, "disable flash protection...\n");
367 // Write Status Register 1 <- 0x00
368 uint8_t data
[2] = { FC_WSR1
, 0x00 };
369 //flash_chip_select();
370 mpsse_xfer_spi(data
, 2);
371 //flash_chip_deselect();
375 // Read Status Register 1
378 //flash_chip_select();
379 mpsse_xfer_spi(data
, 2);
380 //flash_chip_deselect();
383 fprintf(stderr
, "failed to disable protection, SR now equal to 0x%02x (expected 0x00)\n", data
[1]);
387 // ---------------------------------------------------------
388 // JTAG -> SPI functions
389 // ---------------------------------------------------------
392 * JTAG performrs all shifts LSB first, our FLSAH is expeting bytes MSB first,
393 * There are a few ways to fix this, for now we just bit-reverse all the input data to the JTAG core
395 uint8_t bit_reverse(uint8_t in
){
397 uint8_t out
= (in
& 0x01) ? 0x80 : 0x00;
398 out
|= (in
& 0x02) ? 0x40 : 0x00;
399 out
|= (in
& 0x04) ? 0x20 : 0x00;
400 out
|= (in
& 0x08) ? 0x10 : 0x00;
401 out
|= (in
& 0x10) ? 0x08 : 0x00;
402 out
|= (in
& 0x20) ? 0x04 : 0x00;
403 out
|= (in
& 0x40) ? 0x02 : 0x00;
404 out
|= (in
& 0x80) ? 0x01 : 0x00;
409 void xfer_spi(uint8_t* data
, uint32_t len
){
410 /* Flip bit order of all bytes */
411 for(int i
= 0; i
< len
; i
++){
412 data
[i
] = bit_reverse(data
[i
]);
415 jtag_go_to_state(STATE_SHIFT_DR
);
416 jtag_tap_shift(data
, data
, len
* 8, true);
418 /* Flip bit order of all bytes */
419 for(int i
= 0; i
< len
; i
++){
420 data
[i
] = bit_reverse(data
[i
]);
424 // ---------------------------------------------------------
425 // ECP5 specific JTAG functions
426 // ---------------------------------------------------------
429 static void print_idcode(uint32_t idcode
){
430 for(int i
= 0; i
< sizeof(ecp_devices
)/sizeof(struct ecp_device_id
); i
++){
431 if(idcode
== ecp_devices
[i
].device_id
)
433 printf("IDCODE: 0x%08x (%s)\n", idcode
,ecp_devices
[i
].device_name
);
437 printf("IDCODE: 0x%08x does not match :(\n", idcode
);
440 static void read_idcode(){
442 uint8_t data
[4] = {READ_ID
};
444 jtag_go_to_state(STATE_SHIFT_IR
);
445 jtag_tap_shift(data
, data
, 8, true);
448 jtag_go_to_state(STATE_SHIFT_DR
);
449 jtag_tap_shift(data
, data
, 32, true);
453 /* Format the IDCODE into a 32bit value */
454 for(int i
= 0; i
< 4; i
++)
455 idcode
= data
[i
] << 24 | idcode
>> 8;
457 print_idcode(idcode
);
461 static void print_status_register(uint32_t status
){
462 printf("ECP5 Status Register: 0x%08x\n", status
);
465 printf(" Transparent Mode: %s\n", status
& (1 << 0) ? "Yes" : "No" );
466 printf(" Config Target: %s\n", status
& (7 << 1) ? "SRAM" : "eFuse" );
467 printf(" JTAG Active: %s\n", status
& (1 << 4) ? "Yes" : "No" );
468 printf(" PWD Protection: %s\n", status
& (1 << 5) ? "Yes" : "No" );
469 printf(" Decrypt Enable: %s\n", status
& (1 << 7) ? "Yes" : "No" );
470 printf(" DONE: %s\n", status
& (1 << 8) ? "Yes" : "No" );
471 printf(" ISC Enable: %s\n", status
& (1 << 9) ? "Yes" : "No" );
472 printf(" Write Enable: %s\n", status
& (1 << 10) ? "Writable" : "Not Writable");
473 printf(" Read Enable: %s\n", status
& (1 << 11) ? "Readable" : "Not Readable");
474 printf(" Busy Flag: %s\n", status
& (1 << 12) ? "Yes" : "No" );
475 printf(" Fail Flag: %s\n", status
& (1 << 13) ? "Yes" : "No" );
476 printf(" Feature OTP: %s\n", status
& (1 << 14) ? "Yes" : "No" );
477 printf(" Decrypt Only: %s\n", status
& (1 << 15) ? "Yes" : "No" );
478 printf(" PWD Enable: %s\n", status
& (1 << 16) ? "Yes" : "No" );
479 printf(" Encrypt Preamble: %s\n", status
& (1 << 20) ? "Yes" : "No" );
480 printf(" Std Preamble: %s\n", status
& (1 << 21) ? "Yes" : "No" );
481 printf(" SPIm Fail 1: %s\n", status
& (1 << 22) ? "Yes" : "No" );
483 uint8_t bse_error
= (status
& (7 << 23)) >> 23;
485 case 0b000: printf(" BSE Error Code: No Error (0b000)\n"); break;
486 case 0b001: printf(" BSE Error Code: ID Error (0b001)\n"); break;
487 case 0b010: printf(" BSE Error Code: CMD Error - illegal command (0b010)\n"); break;
488 case 0b011: printf(" BSE Error Code: CRC Error (0b011)\n"); break;
489 case 0b100: printf(" BSE Error Code: PRMB Error - preamble error (0b100)\n"); break;
490 case 0b101: printf(" BSE Error Code: ABRT Error - configuration aborted by the user (0b101)\n"); break;
491 case 0b110: printf(" BSE Error Code: OVFL Error - data overflow error (0b110)\n"); break;
492 case 0b111: printf(" BSE Error Code: SDM Error - bitstream pass the size of SRAM array (0b111)\n"); break;
495 printf(" Execution Error: %s\n", status
& (1 << 26) ? "Yes" : "No" );
496 printf(" ID Error: %s\n", status
& (1 << 27) ? "Yes" : "No" );
497 printf(" Invalid Command: %s\n", status
& (1 << 28) ? "Yes" : "No" );
498 printf(" SED Error: %s\n", status
& (1 << 29) ? "Yes" : "No" );
499 printf(" Bypass Mode: %s\n", status
& (1 << 30) ? "Yes" : "No" );
500 printf(" Flow Througuh Mode: %s\n", status
& (1 << 31) ? "Yes" : "No" );
508 static void read_status_register(){
510 uint8_t data
[4] = {LSC_READ_STATUS
};
512 jtag_go_to_state(STATE_SHIFT_IR
);
513 jtag_tap_shift(data
, data
, 8, true);
516 jtag_go_to_state(STATE_SHIFT_DR
);
517 jtag_tap_shift(data
, data
, 32, true);
521 /* Format the IDCODE into a 32bit value */
522 for(int i
= 0; i
< 4; i
++)
523 status
= data
[i
] << 24 | status
>> 8;
525 print_status_register(status
);
530 static void enter_spi_background_mode(){
532 uint8_t data_in
[4] = {0,0,0,0};
533 uint8_t data_out
[4] = {0,0,0,0};
536 jtag_go_to_state(STATE_SHIFT_IR
);
537 jtag_tap_shift(data_in
, data_out
, 8, true);
539 /* These bytes seem to be required to un-lock the SPI interface */
542 jtag_go_to_state(STATE_SHIFT_DR
);
543 jtag_tap_shift(data_in
, data_out
, 16, true);
545 /* Entering IDLE is essential */
546 jtag_go_to_state(STATE_RUN_TEST_IDLE
);
550 void ecp_jtag_cmd(uint8_t cmd
){
551 uint8_t data_in
[1] = {0};
552 uint8_t data_out
[1] = {0};
555 jtag_go_to_state(STATE_SHIFT_IR
);
556 jtag_tap_shift(data_in
, data_out
, 8, true);
558 jtag_go_to_state(STATE_RUN_TEST_IDLE
);
562 // ---------------------------------------------------------
563 // iceprog implementation
564 // ---------------------------------------------------------
566 static void help(const char *progname
)
568 fprintf(stderr
, "Simple programming tool for FTDI-based Lattice ECP JTAG programmers.\n");
569 fprintf(stderr
, "Usage: %s [-b|-n|-c] <input file>\n", progname
);
570 fprintf(stderr
, " %s -r|-R<bytes> <output file>\n", progname
);
571 fprintf(stderr
, " %s -S <input file>\n", progname
);
572 fprintf(stderr
, " %s -t\n", progname
);
573 fprintf(stderr
, "\n");
574 fprintf(stderr
, "General options:\n");
575 fprintf(stderr
, " -d <device string> use the specified USB device [default: i:0x0403:0x6010 or i:0x0403:0x6014]\n");
576 fprintf(stderr
, " d:<devicenode> (e.g. d:002/005)\n");
577 fprintf(stderr
, " i:<vendor>:<product> (e.g. i:0x0403:0x6010)\n");
578 fprintf(stderr
, " i:<vendor>:<product>:<index> (e.g. i:0x0403:0x6010:0)\n");
579 fprintf(stderr
, " s:<vendor>:<product>:<serial-string>\n");
580 fprintf(stderr
, " -I [ABCD] connect to the specified interface on the FTDI chip\n");
581 fprintf(stderr
, " [default: A]\n");
582 fprintf(stderr
, " -o <offset in bytes> start address for read/write [default: 0]\n");
583 fprintf(stderr
, " (append 'k' to the argument for size in kilobytes,\n");
584 fprintf(stderr
, " or 'M' for size in megabytes)\n");
585 fprintf(stderr
, " -s slow SPI (50 kHz instead of 6 MHz)\n");
586 fprintf(stderr
, " -v verbose output\n");
587 fprintf(stderr
, " -i [4,32,64] select erase block size [default: 64k]\n");
588 fprintf(stderr
, "\n");
589 fprintf(stderr
, "Mode of operation:\n");
590 fprintf(stderr
, " [default] write file contents to flash, then verify\n");
591 fprintf(stderr
, " -X write file contents to flash only\n");
592 fprintf(stderr
, " -r read first 256 kB from flash and write to file\n");
593 fprintf(stderr
, " -R <size in bytes> read the specified number of bytes from flash\n");
594 fprintf(stderr
, " (append 'k' to the argument for size in kilobytes,\n");
595 fprintf(stderr
, " or 'M' for size in megabytes)\n");
596 fprintf(stderr
, " -c do not write flash, only verify (`check')\n");
597 fprintf(stderr
, " -S perform SRAM programming\n");
598 fprintf(stderr
, " -t just read the flash ID sequence\n");
599 fprintf(stderr
, "\n");
600 fprintf(stderr
, "Erase mode (only meaningful in default mode):\n");
601 fprintf(stderr
, " [default] erase aligned chunks of 64kB in write mode\n");
602 fprintf(stderr
, " This means that some data after the written data (or\n");
603 fprintf(stderr
, " even before when -o is used) may be erased as well.\n");
604 fprintf(stderr
, " -b bulk erase entire flash before writing\n");
605 fprintf(stderr
, " -e <size in bytes> erase flash as if we were writing that number of bytes\n");
606 fprintf(stderr
, " -n do not erase flash before writing\n");
607 fprintf(stderr
, " -p disable write protection before erasing or writing\n");
608 fprintf(stderr
, " This can be useful if flash memory appears to be\n");
609 fprintf(stderr
, " bricked and won't respond to erasing or programming.\n");
610 fprintf(stderr
, "\n");
611 fprintf(stderr
, "Miscellaneous options:\n");
612 fprintf(stderr
, " --help display this help and exit\n");
613 fprintf(stderr
, " -- treat all remaining arguments as filenames\n");
614 fprintf(stderr
, "\n");
615 fprintf(stderr
, "Exit status:\n");
616 fprintf(stderr
, " 0 on success,\n");
617 fprintf(stderr
, " 1 if a non-hardware error occurred (e.g., failure to read from or\n");
618 fprintf(stderr
, " write to a file, or invoked with invalid options),\n");
619 fprintf(stderr
, " 2 if communication with the hardware failed (e.g., cannot find the\n");
620 fprintf(stderr
, " iCE FTDI USB device),\n");
621 fprintf(stderr
, " 3 if verification of the data failed.\n");
622 fprintf(stderr
, "\n");
623 fprintf(stderr
, "If you have a bug report, please file an issue on github:\n");
624 fprintf(stderr
, " https://github.com/gregdavill/ecpprog/issues\n");
627 int main(int argc
, char **argv
)
629 /* used for error reporting */
630 const char *my_name
= argv
[0];
631 for (size_t i
= 0; argv
[0][i
]; i
++)
632 if (argv
[0][i
] == '/')
633 my_name
= argv
[0] + i
+ 1;
635 int read_size
= 256 * 1024;
636 int erase_block_size
= 64;
640 bool read_mode
= false;
641 bool check_mode
= false;
642 bool erase_mode
= false;
643 bool bulk_erase
= false;
644 bool dont_erase
= false;
645 bool prog_sram
= false;
646 bool test_mode
= false;
647 bool slow_clock
= false;
648 bool disable_protect
= false;
649 bool disable_verify
= false;
650 const char *filename
= NULL
;
651 const char *devstr
= NULL
;
655 _setmode(_fileno(stdin
), _O_BINARY
);
656 _setmode(_fileno(stdout
), _O_BINARY
);
659 static struct option long_options
[] = {
660 {"help", no_argument
, NULL
, -2},
664 /* Decode command line parameters */
667 while ((opt
= getopt_long(argc
, argv
, "d:i:I:rR:e:o:cbnStvspX", long_options
, NULL
)) != -1) {
669 case 'd': /* device string */
672 case 'i': /* block erase size */
673 if (!strcmp(optarg
, "4"))
674 erase_block_size
= 4;
675 else if (!strcmp(optarg
, "32"))
676 erase_block_size
= 32;
677 else if (!strcmp(optarg
, "64"))
678 erase_block_size
= 64;
680 fprintf(stderr
, "%s: `%s' is not a valid erase block size (must be `4', `32' or `64')\n", my_name
, optarg
);
684 case 'I': /* FTDI Chip interface select */
685 if (!strcmp(optarg
, "A"))
687 else if (!strcmp(optarg
, "B"))
689 else if (!strcmp(optarg
, "C"))
691 else if (!strcmp(optarg
, "D"))
694 fprintf(stderr
, "%s: `%s' is not a valid interface (must be `A', `B', `C', or `D')\n", my_name
, optarg
);
698 case 'r': /* Read 256 bytes to file */
701 case 'R': /* Read n bytes to file */
703 read_size
= strtol(optarg
, &endptr
, 0);
706 else if (!strcmp(endptr
, "k"))
708 else if (!strcmp(endptr
, "M"))
709 read_size
*= 1024 * 1024;
711 fprintf(stderr
, "%s: `%s' is not a valid size\n", my_name
, optarg
);
715 case 'e': /* Erase blocks as if we were writing n bytes */
717 erase_size
= strtol(optarg
, &endptr
, 0);
720 else if (!strcmp(endptr
, "k"))
722 else if (!strcmp(endptr
, "M"))
723 erase_size
*= 1024 * 1024;
725 fprintf(stderr
, "%s: `%s' is not a valid size\n", my_name
, optarg
);
729 case 'o': /* set address offset */
730 rw_offset
= strtol(optarg
, &endptr
, 0);
733 else if (!strcmp(endptr
, "k"))
735 else if (!strcmp(endptr
, "M"))
736 rw_offset
*= 1024 * 1024;
738 fprintf(stderr
, "%s: `%s' is not a valid offset\n", my_name
, optarg
);
742 case 'c': /* do not write just check */
745 case 'b': /* bulk erase before writing */
748 case 'n': /* do not erase before writing */
751 case 'S': /* write to sram directly */
754 case 't': /* just read flash id */
757 case 'v': /* provide verbose output */
760 case 's': /* use slow SPI clock */
763 case 'p': /* disable flash protect before erase/write */
764 disable_protect
= true;
766 case 'X': /* disable verification */
767 disable_verify
= true;
773 /* error message has already been printed */
774 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
779 /* Make sure that the combination of provided parameters makes sense */
781 if (read_mode
+ erase_mode
+ check_mode
+ prog_sram
+ test_mode
> 1) {
782 fprintf(stderr
, "%s: options `-r'/`-R', `-e`, `-c', `-S', and `-t' are mutually exclusive\n", my_name
);
786 if (bulk_erase
&& dont_erase
) {
787 fprintf(stderr
, "%s: options `-b' and `-n' are mutually exclusive\n", my_name
);
791 if (disable_protect
&& (read_mode
|| check_mode
|| prog_sram
|| test_mode
)) {
792 fprintf(stderr
, "%s: option `-p' only valid in programming mode\n", my_name
);
796 if (bulk_erase
&& (read_mode
|| check_mode
|| prog_sram
|| test_mode
)) {
797 fprintf(stderr
, "%s: option `-b' only valid in programming mode\n", my_name
);
801 if (dont_erase
&& (read_mode
|| check_mode
|| prog_sram
|| test_mode
)) {
802 fprintf(stderr
, "%s: option `-n' only valid in programming mode\n", my_name
);
806 if (rw_offset
!= 0 && prog_sram
) {
807 fprintf(stderr
, "%s: option `-o' not supported in SRAM mode\n", my_name
);
811 if (rw_offset
!= 0 && test_mode
) {
812 fprintf(stderr
, "%s: option `-o' not supported in test mode\n", my_name
);
816 if (optind
+ 1 == argc
) {
818 fprintf(stderr
, "%s: test mode doesn't take a file name\n", my_name
);
819 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
822 filename
= argv
[optind
];
823 } else if (optind
!= argc
) {
824 fprintf(stderr
, "%s: too many arguments\n", my_name
);
825 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
827 } else if (bulk_erase
|| disable_protect
) {
828 filename
= "/dev/null";
829 } else if (!test_mode
&& !erase_mode
&& !disable_protect
) {
830 fprintf(stderr
, "%s: missing argument\n", my_name
);
831 fprintf(stderr
, "Try `%s --help' for more information.\n", argv
[0]);
835 /* open input/output file in advance
836 so we can fail before initializing the hardware */
843 } else if (erase_mode
) {
844 file_size
= erase_size
;
845 } else if (read_mode
) {
846 f
= (strcmp(filename
, "-") == 0) ? stdout
: fopen(filename
, "wb");
848 fprintf(stderr
, "%s: can't open '%s' for writing: ", my_name
, filename
);
853 f
= (strcmp(filename
, "-") == 0) ? stdin
: fopen(filename
, "rb");
855 fprintf(stderr
, "%s: can't open '%s' for reading: ", my_name
, filename
);
860 /* For regular programming, we need to read the file
861 twice--once for programming and once for verifying--and
862 need to know the file size in advance in order to erase
863 the correct amount of memory.
865 See if we can seek on the input file. Checking for "-"
866 as an argument isn't enough as we might be reading from a
867 named pipe, or contrarily, the standard input may be an
870 if (!prog_sram
&& !check_mode
) {
871 if (fseek(f
, 0L, SEEK_END
) != -1) {
872 file_size
= ftell(f
);
873 if (file_size
== -1) {
874 fprintf(stderr
, "%s: %s: ftell: ", my_name
, filename
);
878 if (fseek(f
, 0L, SEEK_SET
) == -1) {
879 fprintf(stderr
, "%s: %s: fseek: ", my_name
, filename
);
888 fprintf(stderr
, "%s: can't open temporary file\n", my_name
);
894 static unsigned char buffer
[4096];
895 size_t rc
= fread(buffer
, 1, 4096, pipe
);
898 size_t wc
= fwrite(buffer
, 1, rc
, f
);
900 fprintf(stderr
, "%s: can't write to temporary file\n", my_name
);
907 /* now seek to the beginning so we can
908 start reading again */
909 fseek(f
, 0, SEEK_SET
);
914 // ---------------------------------------------------------
915 // Initialize USB connection to FT2232H
916 // ---------------------------------------------------------
918 fprintf(stderr
, "init..\n");
920 mpsse_init(ifnum
, devstr
, slow_clock
);
924 read_status_register();
932 /* Reset ECP5 to release SPI interface */
933 ecp_jtag_cmd(ISC_ENABLE
);
934 ecp_jtag_cmd(ISC_ERASE
);
935 ecp_jtag_cmd(ISC_DISABLE
);
937 /* Put device into SPI bypass mode */
938 enter_spi_background_mode();
944 // ---------------------------------------------------------
946 // ---------------------------------------------------------
948 fprintf(stderr
, "reset..\n");
953 //sram_chip_select();
957 // ---------------------------------------------------------
959 // ---------------------------------------------------------
961 fprintf(stderr
, "programming..\n");
963 static unsigned char buffer
[4096];
964 int rc
= fread(buffer
, 1, 4096, f
);
968 fprintf(stderr
, "sending %d bytes.\n", rc
);
969 mpsse_send_spi(buffer
, rc
);
972 mpsse_send_dummy_bytes(6);
973 mpsse_send_dummy_bit();
977 else /* program flash */
979 // ---------------------------------------------------------
981 // ---------------------------------------------------------
983 fprintf(stderr
, "reset..\n");
985 //flash_chip_deselect();
996 // ---------------------------------------------------------
998 // ---------------------------------------------------------
1000 if (!read_mode
&& !check_mode
)
1002 if (disable_protect
)
1004 flash_write_enable();
1005 flash_disable_protection();
1012 flash_write_enable();
1018 fprintf(stderr
, "file size: %ld\n", file_size
);
1020 int block_size
= erase_block_size
<< 10;
1021 int block_mask
= block_size
- 1;
1022 int begin_addr
= rw_offset
& ~block_mask
;
1023 int end_addr
= (rw_offset
+ file_size
+ block_mask
) & ~block_mask
;
1025 for (int addr
= begin_addr
; addr
< end_addr
; addr
+= block_size
) {
1026 flash_write_enable();
1027 switch(erase_block_size
) {
1029 flash_4kB_sector_erase(addr
);
1032 flash_32kB_sector_erase(addr
);
1035 flash_64kB_sector_erase(addr
);
1039 fprintf(stderr
, "Status after block erase:\n");
1040 flash_read_status();
1049 fprintf(stderr
, "programming..\n");
1051 for (int rc
, addr
= 0; true; addr
+= rc
) {
1052 uint8_t buffer
[256];
1053 int page_size
= 256 - (rw_offset
+ addr
) % 256;
1054 rc
= fread(buffer
, 1, page_size
, f
);
1057 flash_write_enable();
1058 flash_prog(rw_offset
+ addr
, buffer
, rc
);
1062 /* seek to the beginning for second pass */
1063 fseek(f
, 0, SEEK_SET
);
1067 // ---------------------------------------------------------
1069 // ---------------------------------------------------------
1072 fprintf(stderr
, "reading..\n");
1073 for (int addr
= 0; addr
< read_size
; addr
+= 256) {
1074 uint8_t buffer
[256];
1075 flash_read(rw_offset
+ addr
, buffer
, 256);
1076 fwrite(buffer
, read_size
- addr
> 256 ? 256 : read_size
- addr
, 1, f
);
1078 } else if (!erase_mode
&& !disable_verify
) {
1079 fprintf(stderr
, "reading..\n");
1080 for (int addr
= 0; true; addr
+= 256) {
1081 uint8_t buffer_flash
[256], buffer_file
[256];
1082 int rc
= fread(buffer_file
, 1, 256, f
);
1085 flash_read(rw_offset
+ addr
, buffer_flash
, rc
);
1086 if (memcmp(buffer_file
, buffer_flash
, rc
)) {
1087 fprintf(stderr
, "Found difference between flash and file!\n");
1092 fprintf(stderr
, "VERIFY OK\n");
1096 // ---------------------------------------------------------
1098 // ---------------------------------------------------------
1100 //flash_power_down();
1107 if (f
!= NULL
&& f
!= stdin
&& f
!= stdout
)
1110 // ---------------------------------------------------------
1112 // ---------------------------------------------------------
1114 fprintf(stderr
, "Bye.\n");