1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2022 by Daniel Anselmi *
6 ***************************************************************************/
12 #include <jtag/jtag.h>
13 #include <jtag/adapter.h>
14 #include <helper/bits.h>
19 #define ERASE_SRAM 0x05
20 #define SRAM_ERASE_DONE 0x09
22 #define ADDRESS_INITIALIZATION 0x12
23 #define READ_USERCODE 0x13
24 #define CONFIG_ENABLE 0x15
25 #define TRANSFER_CONFIGURATION_DATA 0x17
26 #define CONFIG_DISABLE 0x3A
28 #define STATUS_REGISTER 0x41
29 #define ERASE_FLASH 0x75
30 #define ENABLE_2ND_FLASH 0x78
35 #define STAUS_MASK_MEMORY_ERASE BIT(5)
36 #define STAUS_MASK_SYSTEM_EDIT_MODE BIT(7)
38 struct gowin_pld_device
{
42 struct gowin_bit_file
{
43 struct raw_bit_file raw_file
;
46 uint16_t stored_checksum
;
55 static uint64_t gowin_read_fs_file_bitsequence(const char *bits
, int length
)
58 for (int i
= 0; i
< length
; i
++)
59 res
= (res
<< 1) | (*bits
++ == '1' ? 1 : 0);
63 static int gowin_add_byte_to_bit_file(struct gowin_bit_file
*bit_file
, uint8_t byte
)
65 if (bit_file
->raw_file
.length
+ 1 > bit_file
->capacity
) {
67 if (bit_file
->raw_file
.data
)
68 buffer
= realloc(bit_file
->raw_file
.data
, bit_file
->capacity
+ 8192);
70 buffer
= malloc(8192);
72 LOG_ERROR("Out of memory");
75 bit_file
->raw_file
.data
= buffer
;
76 bit_file
->capacity
+= 8192;
79 bit_file
->raw_file
.data
[bit_file
->raw_file
.length
++] = byte
;
84 static int gowin_read_fs_file_header(struct gowin_bit_file
*bit_file
, FILE *stream
)
89 int end_of_header
= 0;
90 while (!end_of_header
) {
92 char *line
= fgets(buffer
, 256, stream
);
93 if (!line
|| feof(stream
) || ferror(stream
))
99 size_t line_length
= strlen(line
);
100 if (line
[line_length
- 1] != '\n')
104 for (unsigned int i
= 0; i
< line_length
; i
+= 8) {
105 uint8_t byte
= gowin_read_fs_file_bitsequence(line
+ i
, 8);
106 int retval
= gowin_add_byte_to_bit_file(bit_file
, byte
);
107 if (retval
!= ERROR_OK
)
111 uint8_t key
= gowin_read_fs_file_bitsequence(line
, 8);
113 uint64_t value
= gowin_read_fs_file_bitsequence(line
, line_length
- 8);
116 bit_file
->id
= value
& 0xffffffff;
117 } else if (key
== 0x3B) {
119 bit_file
->crc_en
= (value
& BIT(23)) ? 1 : 0;
126 static int gowin_read_fs_file(struct gowin_bit_file
*bit_file
, const char *filename
)
128 FILE *input_file
= fopen(filename
, "r");
131 LOG_ERROR("Couldn't open %s: %s", filename
, strerror(errno
));
132 return ERROR_PLD_FILE_LOAD_FAILED
;
135 int retval
= gowin_read_fs_file_header(bit_file
, input_file
);
136 if (retval
!= ERROR_OK
) {
137 free(bit_file
->raw_file
.data
);
142 char digits_buffer
[9]; /* 8 + 1 trailing zero */
144 char *digits
= fgets(digits_buffer
, 9, input_file
);
145 if (feof(input_file
))
147 if (!digits
|| ferror(input_file
)) {
148 free(bit_file
->raw_file
.data
);
152 if (digits
[0] == '\n')
155 if (strlen(digits
) != 8) {
156 free(bit_file
->raw_file
.data
);
160 uint8_t byte
= gowin_read_fs_file_bitsequence(digits
, 8);
161 retval
= gowin_add_byte_to_bit_file(bit_file
, byte
);
162 if (retval
!= ERROR_OK
) {
163 free(bit_file
->raw_file
.data
);
173 static int gowin_read_file(struct gowin_bit_file
*bit_file
, const char *filename
, bool *is_fs
)
175 memset(bit_file
, 0, sizeof(struct gowin_bit_file
));
177 if (!filename
|| !bit_file
)
178 return ERROR_COMMAND_SYNTAX_ERROR
;
180 const char *file_suffix_pos
= strrchr(filename
, '.');
181 if (!file_suffix_pos
) {
182 LOG_ERROR("Unable to detect filename suffix");
183 return ERROR_PLD_FILE_LOAD_FAILED
;
186 /* check if binary .bin or ascii .fs */
187 if (strcasecmp(file_suffix_pos
, ".bin") == 0) {
189 return cpld_read_raw_bit_file(&bit_file
->raw_file
, filename
);
190 } else if (strcasecmp(file_suffix_pos
, ".fs") == 0) {
192 return gowin_read_fs_file(bit_file
, filename
);
195 LOG_ERROR("Filetype not supported, expecting .fs or .bin file");
196 return ERROR_PLD_FILE_LOAD_FAILED
;
199 static int gowin_set_instr(struct jtag_tap
*tap
, uint8_t new_instr
)
201 struct scan_field field
;
202 field
.num_bits
= tap
->ir_length
;
203 void *t
= calloc(DIV_ROUND_UP(field
.num_bits
, 8), 1);
205 LOG_ERROR("Out of memory");
209 buf_set_u32(t
, 0, field
.num_bits
, new_instr
);
210 field
.in_value
= NULL
;
211 jtag_add_ir_scan(tap
, &field
, TAP_IDLE
);
212 jtag_add_runtest(3, TAP_IDLE
);
217 static int gowin_read_register(struct jtag_tap
*tap
, uint32_t reg
, uint32_t *result
)
219 struct scan_field field
;
221 int retval
= gowin_set_instr(tap
, reg
);
222 if (retval
!= ERROR_OK
)
224 retval
= jtag_execute_queue();
225 if (retval
!= ERROR_OK
)
228 uint8_t buf
[4] = {0};
229 field
.check_mask
= NULL
;
230 field
.check_value
= NULL
;
232 field
.out_value
= buf
;
233 field
.in_value
= buf
;
235 jtag_add_dr_scan(tap
, 1, &field
, TAP_IDLE
);
236 retval
= jtag_execute_queue();
237 *result
= le_to_h_u32(buf
);
241 static int gowin_check_status_flag(struct jtag_tap
*tap
, uint32_t mask
, uint32_t flag
)
247 int retval
= gowin_read_register(tap
, STATUS_REGISTER
, &status
);
248 if (retval
!= ERROR_OK
)
250 if (retries
++ == 100000)
252 } while ((status
& mask
) != flag
);
257 static int gowin_enable_config(struct jtag_tap
*tap
)
259 int retval
= gowin_set_instr(tap
, CONFIG_ENABLE
);
260 if (retval
!= ERROR_OK
)
262 retval
= jtag_execute_queue();
263 if (retval
!= ERROR_OK
)
266 return gowin_check_status_flag(tap
, STAUS_MASK_SYSTEM_EDIT_MODE
, STAUS_MASK_SYSTEM_EDIT_MODE
);
269 static int gowin_disable_config(struct jtag_tap
*tap
)
271 int retval
= gowin_set_instr(tap
, CONFIG_DISABLE
);
272 if (retval
!= ERROR_OK
)
274 retval
= jtag_execute_queue();
275 if (retval
!= ERROR_OK
)
278 return gowin_check_status_flag(tap
, STAUS_MASK_SYSTEM_EDIT_MODE
, 0);
281 static int gowin_reload(struct jtag_tap
*tap
)
283 int retval
= gowin_set_instr(tap
, RELOAD
);
284 if (retval
!= ERROR_OK
)
286 retval
= gowin_set_instr(tap
, NO_OP
);
287 if (retval
!= ERROR_OK
)
289 return jtag_execute_queue();
292 static int gowin_runtest_idle(struct jtag_tap
*tap
, unsigned int frac_sec
)
294 int speed
= adapter_get_speed_khz() * 1000;
295 int cycles
= DIV_ROUND_UP(speed
, frac_sec
);
296 jtag_add_runtest(cycles
, TAP_IDLE
);
297 return jtag_execute_queue();
300 static int gowin_erase_sram(struct jtag_tap
*tap
, bool tx_erase_done
)
302 /* config is already enabled */
303 int retval
= gowin_set_instr(tap
, ERASE_SRAM
);
304 if (retval
!= ERROR_OK
)
306 retval
= gowin_set_instr(tap
, NO_OP
);
307 if (retval
!= ERROR_OK
)
310 /* Delay or Run Test 2~10ms */
311 /* 10 ms is worst case for GW2A-55 */
312 jtag_add_sleep(10000);
313 retval
= jtag_execute_queue();
314 if (retval
!= ERROR_OK
)
317 retval
= gowin_check_status_flag(tap
, STAUS_MASK_MEMORY_ERASE
,
318 STAUS_MASK_MEMORY_ERASE
);
319 if (retval
!= ERROR_OK
)
323 retval
= gowin_set_instr(tap
, SRAM_ERASE_DONE
);
324 if (retval
!= ERROR_OK
)
326 retval
= gowin_set_instr(tap
, NO_OP
);
327 if (retval
!= ERROR_OK
)
329 retval
= jtag_execute_queue();
330 if (retval
!= ERROR_OK
)
332 /* gen clock cycles in RUN/IDLE for 500us -> 1/500us = 2000/s */
333 retval
= gowin_runtest_idle(tap
, 2000);
334 if (retval
!= ERROR_OK
)
338 retval
= gowin_set_instr(tap
, NO_OP
);
339 if (retval
!= ERROR_OK
)
341 return jtag_execute_queue();
344 static int gowin_load_to_sram(struct pld_device
*pld_device
, const char *filename
)
349 struct gowin_pld_device
*gowin_info
= pld_device
->driver_priv
;
351 if (!gowin_info
|| !gowin_info
->tap
)
353 struct jtag_tap
*tap
= gowin_info
->tap
;
356 struct gowin_bit_file bit_file
;
357 int retval
= gowin_read_file(&bit_file
, filename
, &is_fs
);
358 if (retval
!= ERROR_OK
)
361 for (unsigned int i
= 0; i
< bit_file
.raw_file
.length
; i
++)
362 bit_file
.raw_file
.data
[i
] = flip_u32(bit_file
.raw_file
.data
[i
], 8);
365 retval
= gowin_read_register(tap
, IDCODE
, &id
);
366 if (retval
!= ERROR_OK
) {
367 free(bit_file
.raw_file
.data
);
371 if (is_fs
&& id
!= bit_file
.id
) {
372 free(bit_file
.raw_file
.data
);
373 LOG_ERROR("Id on device (0x%8.8" PRIx32
") and id in bit-stream (0x%8.8" PRIx32
") don't match.",
378 retval
= gowin_enable_config(tap
);
379 if (retval
!= ERROR_OK
) {
380 free(bit_file
.raw_file
.data
);
384 retval
= gowin_erase_sram(tap
, false);
385 if (retval
!= ERROR_OK
) {
386 free(bit_file
.raw_file
.data
);
390 retval
= gowin_set_instr(tap
, ADDRESS_INITIALIZATION
);
391 if (retval
!= ERROR_OK
) {
392 free(bit_file
.raw_file
.data
);
395 retval
= gowin_set_instr(tap
, TRANSFER_CONFIGURATION_DATA
);
396 if (retval
!= ERROR_OK
) {
397 free(bit_file
.raw_file
.data
);
401 /* scan out the bitstream */
402 struct scan_field field
;
403 field
.num_bits
= bit_file
.raw_file
.length
* 8;
404 field
.out_value
= bit_file
.raw_file
.data
;
405 field
.in_value
= bit_file
.raw_file
.data
;
406 jtag_add_dr_scan(gowin_info
->tap
, 1, &field
, TAP_IDLE
);
407 jtag_add_runtest(3, TAP_IDLE
);
409 retval
= jtag_execute_queue();
410 if (retval
!= ERROR_OK
) {
411 free(bit_file
.raw_file
.data
);
415 retval
= gowin_disable_config(tap
);
416 free(bit_file
.raw_file
.data
);
417 if (retval
!= ERROR_OK
)
420 retval
= gowin_set_instr(gowin_info
->tap
, NO_OP
);
421 if (retval
!= ERROR_OK
)
424 retval
= jtag_execute_queue();
429 static int gowin_read_register_command(struct pld_device
*pld_device
, uint32_t cmd
, uint32_t *value
)
434 struct gowin_pld_device
*gowin_info
= pld_device
->driver_priv
;
436 if (!gowin_info
|| !gowin_info
->tap
)
439 return gowin_read_register(gowin_info
->tap
, cmd
, value
);
442 static int gowin_reload_command(struct pld_device
*pld_device
)
447 struct gowin_pld_device
*gowin_info
= pld_device
->driver_priv
;
449 if (!gowin_info
|| !gowin_info
->tap
)
452 return gowin_reload(gowin_info
->tap
);
455 static int gowin_get_ipdbg_hub(int user_num
, struct pld_device
*pld_device
, struct pld_ipdbg_hub
*hub
)
460 struct gowin_pld_device
*pld_device_info
= pld_device
->driver_priv
;
462 if (!pld_device_info
|| !pld_device_info
->tap
)
465 hub
->tap
= pld_device_info
->tap
;
468 hub
->user_ir_code
= USER1
;
469 } else if (user_num
== 2) {
470 hub
->user_ir_code
= USER2
;
472 LOG_ERROR("gowin devices only have user register 1 & 2");
478 COMMAND_HANDLER(gowin_read_status_command_handler
)
481 return ERROR_COMMAND_SYNTAX_ERROR
;
483 struct pld_device
*device
= get_pld_device_by_name_or_numstr(CMD_ARGV
[0]);
485 command_print(CMD
, "pld device '#%s' is out of bounds or unknown", CMD_ARGV
[0]);
490 int retval
= gowin_read_register_command(device
, STATUS_REGISTER
, &status
);
492 if (retval
== ERROR_OK
)
493 command_print(CMD
, "0x%8.8" PRIx32
, status
);
498 COMMAND_HANDLER(gowin_read_user_register_command_handler
)
501 return ERROR_COMMAND_SYNTAX_ERROR
;
503 struct pld_device
*device
= get_pld_device_by_name_or_numstr(CMD_ARGV
[0]);
505 command_print(CMD
, "pld device '#%s' is out of bounds or unknown", CMD_ARGV
[0]);
509 uint32_t user_reg
= 0;
510 int retval
= gowin_read_register_command(device
, READ_USERCODE
, &user_reg
);
512 if (retval
== ERROR_OK
)
513 command_print(CMD
, "0x%8.8" PRIx32
, user_reg
);
518 COMMAND_HANDLER(gowin_reload_command_handler
)
521 return ERROR_COMMAND_SYNTAX_ERROR
;
523 struct pld_device
*device
= get_pld_device_by_name_or_numstr(CMD_ARGV
[0]);
525 command_print(CMD
, "pld device '#%s' is out of bounds or unknown", CMD_ARGV
[0]);
529 return gowin_reload_command(device
);
532 static const struct command_registration gowin_exec_command_handlers
[] = {
534 .name
= "read_status",
535 .mode
= COMMAND_EXEC
,
536 .handler
= gowin_read_status_command_handler
,
537 .help
= "reading status register from FPGA",
541 .mode
= COMMAND_EXEC
,
542 .handler
= gowin_read_user_register_command_handler
,
543 .help
= "reading user register from FPGA",
547 .mode
= COMMAND_EXEC
,
548 .handler
= gowin_reload_command_handler
,
549 .help
= "reload bitstream from flash to SRAM",
552 COMMAND_REGISTRATION_DONE
555 static const struct command_registration gowin_command_handler
[] = {
559 .help
= "gowin specific commands",
561 .chain
= gowin_exec_command_handlers
563 COMMAND_REGISTRATION_DONE
566 PLD_CREATE_COMMAND_HANDLER(gowin_pld_create_command
)
569 return ERROR_COMMAND_SYNTAX_ERROR
;
571 if (strcmp(CMD_ARGV
[2], "-chain-position") != 0)
572 return ERROR_COMMAND_SYNTAX_ERROR
;
574 struct jtag_tap
*tap
= jtag_tap_by_string(CMD_ARGV
[3]);
576 command_print(CMD
, "Tap: %s does not exist", CMD_ARGV
[3]);
580 struct gowin_pld_device
*gowin_info
= malloc(sizeof(struct gowin_pld_device
));
582 LOG_ERROR("Out of memory");
585 gowin_info
->tap
= tap
;
587 pld
->driver_priv
= gowin_info
;
592 struct pld_driver gowin_pld
= {
594 .commands
= gowin_command_handler
,
595 .pld_create_command
= &gowin_pld_create_command
,
596 .load
= &gowin_load_to_sram
,
597 .get_ipdbg_hub
= gowin_get_ipdbg_hub
,