1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2022 by Daniel Anselmi *
6 ***************************************************************************/
13 #include <jtag/jtag.h>
15 #include "lattice_bit.h"
25 struct lattice_devices_elem
{
27 size_t preload_length
;
28 enum lattice_family_e family
;
31 static const struct lattice_devices_elem lattice_devices
[] = {
32 {0x01270043, 654, LATTICE_ECP2
/* ecp2-6e */},
33 {0x01271043, 643, LATTICE_ECP2
/* ecp2-12e */},
34 {0x01272043, 827, LATTICE_ECP2
/* ecp2-20e */},
35 {0x01274043, 1011, LATTICE_ECP2
/* ecp2-35e */},
36 {0x01273043, 1219, LATTICE_ECP2
/* ecp2-50e */},
37 {0x01275043, 654, LATTICE_ECP2
/* ecp2-70e */},
38 {0x01279043, 680, LATTICE_ECP2
/* ecp2m20e */},
39 {0x0127A043, 936, LATTICE_ECP2
/* ecp2m35e */},
40 {0x0127B043, 1056, LATTICE_ECP2
/* ecp2m50e */},
41 {0x0127C043, 1039, LATTICE_ECP2
/* ecp2m70e */},
42 {0x0127D043, 1311, LATTICE_ECP2
/* ecp2m100e */},
43 {0x01010043, 467, LATTICE_ECP3
/* ecp3 lae3-17ea & lfe3-17ea*/},
44 {0x01012043, 675, LATTICE_ECP3
/* ecp3 lae3-35ea & lfe3-35ea*/},
45 {0x01014043, 1077, LATTICE_ECP3
/* ecp3 lfe3-70ea & lfe3-70e & lfe3-95ea && lfe3-95e*/},
46 {0x01015043, 1326, LATTICE_ECP3
/* ecp3 lfe3-150e*/},
47 {0x21111043, 409, LATTICE_ECP5
/* "LAE5U-12F & LFE5U-12F" */},
48 {0x41111043, 409, LATTICE_ECP5
/* "LFE5U-25F" */},
49 {0x41112043, 510, LATTICE_ECP5
/* "LFE5U-45F" */},
50 {0x41113043, 750, LATTICE_ECP5
/* "LFE5U-85F" */},
51 {0x81111043, 409, LATTICE_ECP5
/* "LFE5UM5G-25F" */},
52 {0x81112043, 510, LATTICE_ECP5
/* "LFE5UM5G-45F" */},
53 {0x81113043, 750, LATTICE_ECP5
/* "LFE5UM5G-85F" */},
54 {0x01111043, 409, LATTICE_ECP5
/* "LAE5UM-25F" */},
55 {0x01112043, 510, LATTICE_ECP5
/* "LAE5UM-45F" */},
56 {0x01113043, 750, LATTICE_ECP5
/* "LAE5UM-85F" */},
57 {0x310f0043, 362, LATTICE_CERTUS
/* LFD2NX-17 */},
58 {0x310f1043, 362, LATTICE_CERTUS
/* LFD2NX-40 */},
59 {0x010f4043, 362, LATTICE_CERTUS
/* LFCPNX-100 */},
62 int lattice_set_instr(struct jtag_tap
*tap
, uint8_t new_instr
, tap_state_t endstate
)
64 struct scan_field field
;
65 field
.num_bits
= tap
->ir_length
;
66 void *t
= calloc(DIV_ROUND_UP(field
.num_bits
, 8), 1);
68 LOG_ERROR("Out of memory");
72 buf_set_u32(t
, 0, field
.num_bits
, new_instr
);
73 field
.in_value
= NULL
;
74 jtag_add_ir_scan(tap
, &field
, endstate
);
79 static int lattice_check_device_family(struct lattice_pld_device
*lattice_device
)
81 if (lattice_device
->family
!= LATTICE_UNKNOWN
&& lattice_device
->preload_length
!= 0)
84 if (!lattice_device
->tap
|| !lattice_device
->tap
->hasidcode
)
87 for (size_t i
= 0; i
< ARRAY_SIZE(lattice_devices
); ++i
) {
88 if (lattice_devices
[i
].id
== lattice_device
->tap
->idcode
) {
89 if (lattice_device
->family
== LATTICE_UNKNOWN
)
90 lattice_device
->family
= lattice_devices
[i
].family
;
91 if (lattice_device
->preload_length
== 0)
92 lattice_device
->preload_length
= lattice_devices
[i
].preload_length
;
96 LOG_ERROR("Unknown id! Specify family and preload-length manually.");
100 int lattice_read_u32_register(struct jtag_tap
*tap
, uint8_t cmd
, uint32_t *in_val
,
101 uint32_t out_val
, bool do_idle
)
103 struct scan_field field
;
106 int retval
= lattice_set_instr(tap
, cmd
, TAP_IDLE
);
107 if (retval
!= ERROR_OK
)
110 jtag_add_runtest(2, TAP_IDLE
);
111 jtag_add_sleep(1000);
114 h_u32_to_le(buffer
, out_val
);
116 field
.out_value
= buffer
;
117 field
.in_value
= buffer
;
118 jtag_add_dr_scan(tap
, 1, &field
, TAP_IDLE
);
119 retval
= jtag_execute_queue();
120 if (retval
== ERROR_OK
)
121 *in_val
= le_to_h_u32(buffer
);
126 int lattice_read_u64_register(struct jtag_tap
*tap
, uint8_t cmd
, uint64_t *in_val
,
129 struct scan_field field
;
132 int retval
= lattice_set_instr(tap
, cmd
, TAP_IDLE
);
133 if (retval
!= ERROR_OK
)
135 h_u64_to_le(buffer
, out_val
);
137 field
.out_value
= buffer
;
138 field
.in_value
= buffer
;
139 jtag_add_dr_scan(tap
, 1, &field
, TAP_IDLE
);
140 retval
= jtag_execute_queue();
141 if (retval
== ERROR_OK
)
142 *in_val
= le_to_h_u64(buffer
);
147 int lattice_preload(struct lattice_pld_device
*lattice_device
)
149 struct scan_field field
;
150 size_t sz_bytes
= DIV_ROUND_UP(lattice_device
->preload_length
, 8);
152 int retval
= lattice_set_instr(lattice_device
->tap
, PRELOAD
, TAP_IDLE
);
153 if (retval
!= ERROR_OK
)
155 uint8_t *buffer
= malloc(sz_bytes
);
157 LOG_ERROR("Out of memory");
160 memset(buffer
, 0xff, sz_bytes
);
162 field
.num_bits
= lattice_device
->preload_length
;
163 field
.out_value
= buffer
;
164 field
.in_value
= NULL
;
165 jtag_add_dr_scan(lattice_device
->tap
, 1, &field
, TAP_IDLE
);
166 retval
= jtag_execute_queue();
171 static int lattice_read_usercode(struct lattice_pld_device
*lattice_device
, uint32_t *usercode
, uint32_t out
)
173 struct jtag_tap
*tap
= lattice_device
->tap
;
177 if (lattice_device
->family
== LATTICE_ECP2
|| lattice_device
->family
== LATTICE_ECP3
)
178 return lattice_ecp2_3_read_usercode(tap
, usercode
, out
);
179 else if (lattice_device
->family
== LATTICE_ECP5
)
180 return lattice_ecp5_read_usercode(tap
, usercode
, out
);
181 else if (lattice_device
->family
== LATTICE_CERTUS
)
182 return lattice_certus_read_usercode(tap
, usercode
, out
);
187 int lattice_verify_usercode(struct lattice_pld_device
*lattice_device
, uint32_t out
,
188 uint32_t expected
, uint32_t mask
)
192 int retval
= lattice_read_usercode(lattice_device
, &usercode
, out
);
193 if (retval
!= ERROR_OK
)
196 if ((usercode
& mask
) != expected
) {
197 LOG_ERROR("verifying user code register failed got: 0x%08" PRIx32
" expected: 0x%08" PRIx32
,
198 usercode
& mask
, expected
);
204 static int lattice_write_usercode(struct lattice_pld_device
*lattice_device
, uint32_t usercode
)
206 if (lattice_device
->family
== LATTICE_ECP2
|| lattice_device
->family
== LATTICE_ECP3
)
207 return lattice_ecp2_3_write_usercode(lattice_device
, usercode
);
208 else if (lattice_device
->family
== LATTICE_ECP5
)
209 return lattice_ecp5_write_usercode(lattice_device
, usercode
);
210 else if (lattice_device
->family
== LATTICE_CERTUS
)
211 return lattice_certus_write_usercode(lattice_device
, usercode
);
216 static int lattice_read_status_u32(struct lattice_pld_device
*lattice_device
, uint32_t *status
,
217 uint32_t out
, bool do_idle
)
219 if (!lattice_device
->tap
)
222 if (lattice_device
->family
== LATTICE_ECP2
|| lattice_device
->family
== LATTICE_ECP3
)
223 return lattice_ecp2_3_read_status(lattice_device
->tap
, status
, out
, do_idle
);
224 else if (lattice_device
->family
== LATTICE_ECP5
)
225 return lattice_ecp5_read_status(lattice_device
->tap
, status
, out
, do_idle
);
229 static int lattice_read_status_u64(struct lattice_pld_device
*lattice_device
, uint64_t *status
,
232 if (!lattice_device
->tap
)
235 if (lattice_device
->family
== LATTICE_CERTUS
)
236 return lattice_certus_read_status(lattice_device
->tap
, status
, out
);
241 int lattice_verify_status_register_u32(struct lattice_pld_device
*lattice_device
, uint32_t out
,
242 uint32_t expected
, uint32_t mask
, bool do_idle
)
245 int retval
= lattice_read_status_u32(lattice_device
, &status
, out
, do_idle
);
246 if (retval
!= ERROR_OK
)
249 if ((status
& mask
) != expected
) {
250 LOG_ERROR("verifying status register failed got: 0x%08" PRIx32
" expected: 0x%08" PRIx32
,
251 status
& mask
, expected
);
257 int lattice_verify_status_register_u64(struct lattice_pld_device
*lattice_device
, uint64_t out
,
258 uint64_t expected
, uint64_t mask
)
261 int retval
= lattice_read_status_u64(lattice_device
, &status
, out
);
262 if (retval
!= ERROR_OK
)
265 if ((status
& mask
) != expected
) {
266 LOG_ERROR("verifying status register failed got: 0x%08" PRIx64
" expected: 0x%08" PRIx64
,
267 status
& mask
, expected
);
273 static int lattice_load_command(struct pld_device
*pld_device
, const char *filename
)
278 struct lattice_pld_device
*lattice_device
= pld_device
->driver_priv
;
279 if (!lattice_device
|| !lattice_device
->tap
)
281 struct jtag_tap
*tap
= lattice_device
->tap
;
283 if (!tap
|| !tap
->hasidcode
)
286 int retval
= lattice_check_device_family(lattice_device
);
287 if (retval
!= ERROR_OK
)
290 struct lattice_bit_file bit_file
;
291 retval
= lattice_read_file(&bit_file
, filename
, lattice_device
->family
);
292 if (retval
!= ERROR_OK
)
295 uint32_t id
= tap
->idcode
;
297 switch (lattice_device
->family
) {
299 retval
= lattice_ecp2_load(lattice_device
, &bit_file
);
302 retval
= lattice_ecp3_load(lattice_device
, &bit_file
);
306 if (bit_file
.has_id
&& id
!= bit_file
.idcode
)
307 LOG_WARNING("Id on device (0x%8.8" PRIx32
") and id in bit-stream (0x%8.8" PRIx32
") don't match.",
308 id
, bit_file
.idcode
);
309 if (lattice_device
->family
== LATTICE_ECP5
)
310 retval
= lattice_ecp5_load(lattice_device
, &bit_file
);
312 retval
= lattice_certus_load(lattice_device
, &bit_file
);
315 LOG_ERROR("loading unknown device family");
318 free(bit_file
.raw_bit
.data
);
322 static int lattice_get_ipdbg_hub(int user_num
, struct pld_device
*pld_device
, struct pld_ipdbg_hub
*hub
)
327 struct lattice_pld_device
*pld_device_info
= pld_device
->driver_priv
;
329 if (!pld_device_info
|| !pld_device_info
->tap
)
332 hub
->tap
= pld_device_info
->tap
;
335 hub
->user_ir_code
= USER1
;
336 } else if (user_num
== 2) {
337 hub
->user_ir_code
= USER2
;
339 LOG_ERROR("lattice devices only have user register 1 & 2");
345 static int lattice_connect_spi_to_jtag(struct pld_device
*pld_device
)
350 struct lattice_pld_device
*pld_device_info
= pld_device
->driver_priv
;
352 int retval
= lattice_check_device_family(pld_device_info
);
353 if (retval
!= ERROR_OK
)
356 if (pld_device_info
->family
== LATTICE_ECP2
|| pld_device_info
->family
== LATTICE_ECP3
)
357 return lattice_ecp2_3_connect_spi_to_jtag(pld_device_info
);
358 else if (pld_device_info
->family
== LATTICE_ECP5
)
359 return lattice_ecp5_connect_spi_to_jtag(pld_device_info
);
364 static int lattice_disconnect_spi_from_jtag(struct pld_device
*pld_device
)
369 struct lattice_pld_device
*pld_device_info
= pld_device
->driver_priv
;
371 int retval
= lattice_check_device_family(pld_device_info
);
372 if (retval
!= ERROR_OK
)
375 if (pld_device_info
->family
== LATTICE_ECP2
|| pld_device_info
->family
== LATTICE_ECP3
)
376 return lattice_ecp2_3_disconnect_spi_from_jtag(pld_device_info
);
377 else if (pld_device_info
->family
== LATTICE_ECP5
)
378 return lattice_ecp5_disconnect_spi_from_jtag(pld_device_info
);
383 static int lattice_get_stuff_bits(struct pld_device
*pld_device
, unsigned int *facing_read_bits
,
384 unsigned int *trailing_write_bits
)
389 struct lattice_pld_device
*pld_device_info
= pld_device
->driver_priv
;
391 int retval
= lattice_check_device_family(pld_device_info
);
392 if (retval
!= ERROR_OK
)
395 if (pld_device_info
->family
== LATTICE_ECP2
|| pld_device_info
->family
== LATTICE_ECP3
)
396 return lattice_ecp2_3_get_facing_read_bits(pld_device_info
, facing_read_bits
);
397 else if (pld_device_info
->family
== LATTICE_ECP5
)
398 return lattice_ecp5_get_facing_read_bits(pld_device_info
, facing_read_bits
);
403 static int lattice_has_jtagspi_instruction(struct pld_device
*device
, bool *has_instruction
)
405 *has_instruction
= true;
409 PLD_CREATE_COMMAND_HANDLER(lattice_pld_create_command
)
411 if (CMD_ARGC
!= 4 && CMD_ARGC
!= 6)
412 return ERROR_COMMAND_SYNTAX_ERROR
;
414 if (strcmp(CMD_ARGV
[2], "-chain-position") != 0)
415 return ERROR_COMMAND_SYNTAX_ERROR
;
417 struct jtag_tap
*tap
= jtag_tap_by_string(CMD_ARGV
[3]);
419 command_print(CMD
, "Tap: %s does not exist", CMD_ARGV
[3]);
423 /* id is not known yet -> postpone lattice_check_device_family() */
424 enum lattice_family_e family
= LATTICE_UNKNOWN
;
426 if (strcmp(CMD_ARGV
[4], "-family") != 0)
427 return ERROR_COMMAND_SYNTAX_ERROR
;
429 if (strcasecmp(CMD_ARGV
[5], "ecp2") == 0) {
430 family
= LATTICE_ECP2
;
431 } else if (strcasecmp(CMD_ARGV
[5], "ecp3") == 0) {
432 family
= LATTICE_ECP3
;
433 } else if (strcasecmp(CMD_ARGV
[5], "ecp5") == 0) {
434 family
= LATTICE_ECP5
;
435 } else if (strcasecmp(CMD_ARGV
[5], "certus") == 0) {
436 family
= LATTICE_CERTUS
;
438 command_print(CMD
, "unknown family");
443 struct lattice_pld_device
*lattice_device
= malloc(sizeof(struct lattice_pld_device
));
444 if (!lattice_device
) {
445 LOG_ERROR("Out of memory");
449 lattice_device
->tap
= tap
;
450 lattice_device
->family
= family
;
451 lattice_device
->preload_length
= 0;
453 pld
->driver_priv
= lattice_device
;
458 COMMAND_HANDLER(lattice_read_usercode_register_command_handler
)
463 return ERROR_COMMAND_SYNTAX_ERROR
;
465 struct pld_device
*device
= get_pld_device_by_name_or_numstr(CMD_ARGV
[0]);
467 command_print(CMD
, "pld device '#%s' is out of bounds or unknown", CMD_ARGV
[0]);
471 struct lattice_pld_device
*lattice_device
= device
->driver_priv
;
475 int retval
= lattice_check_device_family(lattice_device
);
476 if (retval
!= ERROR_OK
)
479 retval
= lattice_read_usercode(lattice_device
, &usercode
, 0x0);
480 if (retval
== ERROR_OK
)
481 command_print(CMD
, "0x%8.8" PRIx32
, usercode
);
486 COMMAND_HANDLER(lattice_set_preload_command_handler
)
488 unsigned int preload_length
;
491 return ERROR_COMMAND_SYNTAX_ERROR
;
493 struct pld_device
*device
= get_pld_device_by_name_or_numstr(CMD_ARGV
[0]);
495 command_print(CMD
, "pld device '#%s' is out of bounds or unknown", CMD_ARGV
[0]);
499 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], preload_length
);
501 struct lattice_pld_device
*lattice_device
= device
->driver_priv
;
506 lattice_device
->preload_length
= preload_length
;
511 COMMAND_HANDLER(lattice_write_usercode_register_command_handler
)
516 return ERROR_COMMAND_SYNTAX_ERROR
;
518 struct pld_device
*device
= get_pld_device_by_name_or_numstr(CMD_ARGV
[0]);
520 command_print(CMD
, "pld device '#%s' is out of bounds or unknown", CMD_ARGV
[0]);
524 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], usercode
);
526 struct lattice_pld_device
*lattice_device
= device
->driver_priv
;
530 int retval
= lattice_check_device_family(lattice_device
);
531 if (retval
!= ERROR_OK
)
534 return lattice_write_usercode(lattice_device
, usercode
);
537 COMMAND_HANDLER(lattice_read_status_command_handler
)
540 return ERROR_COMMAND_SYNTAX_ERROR
;
542 struct pld_device
*device
= get_pld_device_by_name_or_numstr(CMD_ARGV
[0]);
544 command_print(CMD
, "pld device '#%s' is out of bounds or unknown", CMD_ARGV
[0]);
548 struct lattice_pld_device
*lattice_device
= device
->driver_priv
;
552 int retval
= lattice_check_device_family(lattice_device
);
553 if (retval
!= ERROR_OK
)
556 if (lattice_device
->family
== LATTICE_CERTUS
) {
558 retval
= lattice_read_status_u64(lattice_device
, &status
, 0x0);
559 if (retval
== ERROR_OK
)
560 command_print(CMD
, "0x%016" PRIx64
, status
);
563 const bool do_idle
= lattice_device
->family
== LATTICE_ECP5
;
564 retval
= lattice_read_status_u32(lattice_device
, &status
, 0x0, do_idle
);
565 if (retval
== ERROR_OK
)
566 command_print(CMD
, "0x%8.8" PRIx32
, status
);
572 static const struct command_registration lattice_exec_command_handlers
[] = {
574 .name
= "read_status",
575 .mode
= COMMAND_EXEC
,
576 .handler
= lattice_read_status_command_handler
,
577 .help
= "reading status register from FPGA",
581 .mode
= COMMAND_EXEC
,
582 .handler
= lattice_read_usercode_register_command_handler
,
583 .help
= "reading usercode register from FPGA",
586 .name
= "write_user",
587 .mode
= COMMAND_EXEC
,
588 .handler
= lattice_write_usercode_register_command_handler
,
589 .help
= "writing usercode register to FPGA",
590 .usage
= "pld_name value",
592 .name
= "set_preload",
594 .handler
= lattice_set_preload_command_handler
,
595 .help
= "set length for preload (device specific)",
596 .usage
= "pld_name value",
598 COMMAND_REGISTRATION_DONE
601 static const struct command_registration lattice_command_handler
[] = {
605 .help
= "lattice specific commands",
607 .chain
= lattice_exec_command_handlers
,
609 COMMAND_REGISTRATION_DONE
612 struct pld_driver lattice_pld
= {
614 .commands
= lattice_command_handler
,
615 .pld_create_command
= &lattice_pld_create_command
,
616 .load
= &lattice_load_command
,
617 .get_ipdbg_hub
= lattice_get_ipdbg_hub
,
618 .has_jtagspi_instruction
= lattice_has_jtagspi_instruction
,
619 .connect_spi_to_jtag
= lattice_connect_spi_to_jtag
,
620 .disconnect_spi_from_jtag
= lattice_disconnect_spi_from_jtag
,
621 .get_stuff_bits
= lattice_get_stuff_bits
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)