1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2022 by Daniel Anselmi *
6 ***************************************************************************/
15 #define LSCC_REFRESH 0x23
16 #define ISC_ENABLE 0x15
17 #define LSCC_RESET_ADDRESS 0x21
18 #define ISC_PROGRAM_USERCODE 0x1A
19 #define ISC_ERASE 0x03
20 #define READ_USERCODE 0x17
21 #define ISC_DISABLE 0x1E
22 #define LSCC_READ_STATUS 0x53
23 #define LSCC_BITSTREAM_BURST 0x02
24 #define PROGRAM_SPI 0x3A
26 #define STATUS_DONE_BIT 0x00020000
27 #define STATUS_ERROR_BITS_ECP2 0x00040003
28 #define STATUS_ERROR_BITS_ECP3 0x00040007
29 #define REGISTER_ALL_BITS_1 0xffffffff
30 #define REGISTER_ALL_BITS_0 0x00000000
32 int lattice_ecp2_3_read_status(struct jtag_tap
*tap
, uint32_t *status
, uint32_t out
, bool do_idle
)
34 return lattice_read_u32_register(tap
, LSCC_READ_STATUS
, status
, out
, do_idle
);
37 int lattice_ecp2_3_read_usercode(struct jtag_tap
*tap
, uint32_t *usercode
, uint32_t out
)
39 return lattice_read_u32_register(tap
, READ_USERCODE
, usercode
, out
, false);
42 int lattice_ecp2_3_write_usercode(struct lattice_pld_device
*lattice_device
, uint32_t usercode
)
44 struct jtag_tap
*tap
= lattice_device
->tap
;
48 int retval
= lattice_set_instr(tap
, ISC_ENABLE
, TAP_IDLE
);
49 if (retval
!= ERROR_OK
)
51 jtag_add_runtest(5, TAP_IDLE
);
52 jtag_add_sleep(20000);
54 retval
= lattice_set_instr(tap
, ISC_PROGRAM_USERCODE
, TAP_IDLE
);
55 if (retval
!= ERROR_OK
)
58 struct scan_field field
;
60 h_u32_to_le(buffer
, usercode
);
62 field
.out_value
= buffer
;
63 field
.in_value
= NULL
;
64 jtag_add_dr_scan(tap
, 1, &field
, TAP_IDLE
);
65 jtag_add_runtest(5, TAP_IDLE
);
68 retval
= lattice_set_instr(tap
, ISC_DISABLE
, TAP_IDLE
);
69 if (retval
!= ERROR_OK
)
71 jtag_add_runtest(5, TAP_IDLE
);
72 jtag_add_sleep(200000);
74 retval
= jtag_execute_queue();
75 if (retval
!= ERROR_OK
)
77 return lattice_verify_usercode(lattice_device
, 0x0, usercode
, REGISTER_ALL_BITS_1
);
80 static int lattice_ecp2_3_erase_device(struct lattice_pld_device
*lattice_device
)
82 struct jtag_tap
*tap
= lattice_device
->tap
;
86 /* program user code with all bits set */
87 int retval
= lattice_set_instr(tap
, ISC_PROGRAM_USERCODE
, TAP_IRPAUSE
);
88 if (retval
!= ERROR_OK
)
90 struct scan_field field
;
91 uint8_t buffer
[4] = {0xff, 0xff, 0xff, 0xff};
93 field
.out_value
= buffer
;
94 field
.in_value
= NULL
;
95 jtag_add_dr_scan(tap
, 1, &field
, TAP_IDLE
);
96 jtag_add_runtest(5, TAP_IDLE
);
99 /* verify every bit is set */
100 const uint32_t out
= REGISTER_ALL_BITS_1
;
101 const uint32_t mask
= REGISTER_ALL_BITS_1
;
102 const uint32_t expected_pre
= REGISTER_ALL_BITS_1
;
103 retval
= lattice_verify_usercode(lattice_device
, out
, expected_pre
, mask
);
104 if (retval
!= ERROR_OK
)
107 retval
= lattice_set_instr(tap
, ISC_ERASE
, TAP_IDLE
);
108 if (retval
!= ERROR_OK
)
110 jtag_add_runtest(5, TAP_IDLE
);
111 if (lattice_device
->family
== LATTICE_ECP2
)
112 jtag_add_sleep(100000);
114 jtag_add_sleep(2000000);
116 retval
= lattice_set_instr(tap
, LSCC_RESET_ADDRESS
, TAP_IDLE
);
117 if (retval
!= ERROR_OK
)
119 jtag_add_runtest(5, TAP_IDLE
);
120 jtag_add_sleep(2000);
122 /* after erasing check all bits in user register are cleared */
123 const uint32_t expected_post
= REGISTER_ALL_BITS_0
;
124 return lattice_verify_usercode(lattice_device
, out
, expected_post
, mask
);
127 static int lattice_ecp2_3_program_config_map(struct lattice_pld_device
*lattice_device
,
128 struct lattice_bit_file
*bit_file
)
130 struct jtag_tap
*tap
= lattice_device
->tap
;
134 int retval
= lattice_set_instr(tap
, LSCC_RESET_ADDRESS
, TAP_IDLE
);
135 if (retval
!= ERROR_OK
)
137 jtag_add_runtest(5, TAP_IDLE
);
138 jtag_add_sleep(2000);
140 struct scan_field field
;
141 retval
= lattice_set_instr(tap
, LSCC_BITSTREAM_BURST
, TAP_IDLE
);
142 if (retval
!= ERROR_OK
)
144 field
.num_bits
= (bit_file
->raw_bit
.length
- bit_file
->offset
) * 8;
145 field
.out_value
= bit_file
->raw_bit
.data
+ bit_file
->offset
;
146 field
.in_value
= NULL
;
147 jtag_add_dr_scan(tap
, 1, &field
, TAP_IDLE
);
148 jtag_add_runtest(256, TAP_IDLE
);
149 jtag_add_sleep(2000);
150 return jtag_execute_queue();
153 static int lattice_ecp2_3_exit_programming_mode(struct lattice_pld_device
*lattice_device
)
155 struct jtag_tap
*tap
= lattice_device
->tap
;
159 int retval
= lattice_set_instr(tap
, ISC_DISABLE
, TAP_IDLE
);
160 if (retval
!= ERROR_OK
)
162 jtag_add_runtest(5, TAP_IDLE
);
163 jtag_add_sleep(200000);
164 retval
= lattice_set_instr(tap
, BYPASS
, TAP_IDLE
);
165 if (retval
!= ERROR_OK
)
167 jtag_add_runtest(100, TAP_IDLE
);
168 jtag_add_sleep(1000);
169 return jtag_execute_queue();
172 int lattice_ecp2_load(struct lattice_pld_device
*lattice_device
, struct lattice_bit_file
*bit_file
)
174 struct jtag_tap
*tap
= lattice_device
->tap
;
178 int retval
= lattice_preload(lattice_device
);
179 if (retval
!= ERROR_OK
)
182 /* Enable the programming mode */
183 retval
= lattice_set_instr(tap
, LSCC_REFRESH
, TAP_IDLE
);
184 if (retval
!= ERROR_OK
)
186 retval
= lattice_set_instr(tap
, ISC_ENABLE
, TAP_IDLE
);
187 if (retval
!= ERROR_OK
)
189 jtag_add_runtest(5, TAP_IDLE
);
190 jtag_add_sleep(20000);
192 /* Erase the device */
193 retval
= lattice_ecp2_3_erase_device(lattice_device
);
194 if (retval
!= ERROR_OK
)
197 /* Program Fuse Map */
198 retval
= lattice_ecp2_3_program_config_map(lattice_device
, bit_file
);
199 if (retval
!= ERROR_OK
)
202 retval
= lattice_ecp2_3_exit_programming_mode(lattice_device
);
203 if (retval
!= ERROR_OK
)
206 const uint32_t out
= REGISTER_ALL_BITS_1
;
207 const uint32_t mask
= STATUS_DONE_BIT
| STATUS_ERROR_BITS_ECP2
;
208 const uint32_t expected
= STATUS_DONE_BIT
;
209 return lattice_verify_status_register_u32(lattice_device
, out
, expected
, mask
, false);
212 int lattice_ecp3_load(struct lattice_pld_device
*lattice_device
, struct lattice_bit_file
*bit_file
)
214 struct jtag_tap
*tap
= lattice_device
->tap
;
218 /* Program Bscan register */
219 int retval
= lattice_preload(lattice_device
);
220 if (retval
!= ERROR_OK
)
223 /* Enable the programming mode */
224 retval
= lattice_set_instr(tap
, LSCC_REFRESH
, TAP_IDLE
);
225 if (retval
!= ERROR_OK
)
227 jtag_add_runtest(5, TAP_IDLE
);
228 jtag_add_sleep(500000);
229 retval
= lattice_set_instr(tap
, ISC_ENABLE
, TAP_IDLE
);
230 if (retval
!= ERROR_OK
)
232 jtag_add_runtest(5, TAP_IDLE
);
233 jtag_add_sleep(20000);
235 retval
= lattice_ecp2_3_erase_device(lattice_device
);
236 if (retval
!= ERROR_OK
)
239 /* Program Fuse Map */
240 retval
= lattice_ecp2_3_program_config_map(lattice_device
, bit_file
);
241 if (retval
!= ERROR_OK
)
244 retval
= lattice_ecp2_3_exit_programming_mode(lattice_device
);
245 if (retval
!= ERROR_OK
)
248 const uint32_t out
= REGISTER_ALL_BITS_1
;
249 const uint32_t mask
= STATUS_DONE_BIT
| STATUS_ERROR_BITS_ECP3
;
250 const uint32_t expected
= STATUS_DONE_BIT
;
251 return lattice_verify_status_register_u32(lattice_device
, out
, expected
, mask
, false);
254 int lattice_ecp2_3_connect_spi_to_jtag(struct lattice_pld_device
*pld_device_info
)
256 if (!pld_device_info
)
259 struct jtag_tap
*tap
= pld_device_info
->tap
;
263 // erase configuration
264 int retval
= lattice_set_instr(tap
, ISC_ENABLE
, TAP_IDLE
);
265 if (retval
!= ERROR_OK
)
267 retval
= lattice_set_instr(tap
, ISC_ERASE
, TAP_IDLE
);
268 if (retval
!= ERROR_OK
)
270 retval
= lattice_set_instr(tap
, ISC_DISABLE
, TAP_IDLE
);
271 if (retval
!= ERROR_OK
)
274 // connect jtag to spi pins
275 retval
= lattice_set_instr(tap
, PROGRAM_SPI
, TAP_IDLE
);
276 if (retval
!= ERROR_OK
)
279 return jtag_execute_queue();
282 int lattice_ecp2_3_disconnect_spi_from_jtag(struct lattice_pld_device
*pld_device_info
)
284 if (!pld_device_info
)
287 struct jtag_tap
*tap
= pld_device_info
->tap
;
291 int retval
= lattice_set_instr(tap
, BYPASS
, TAP_IDLE
);
292 if (retval
!= ERROR_OK
)
295 return jtag_execute_queue();
298 int lattice_ecp2_3_get_facing_read_bits(struct lattice_pld_device
*pld_device_info
, unsigned int *facing_read_bits
)
300 if (!pld_device_info
)
303 *facing_read_bits
= 1;
308 int lattice_ecp2_3_refresh(struct lattice_pld_device
*lattice_device
)
310 if (!lattice_device
|| !lattice_device
->tap
)
313 int retval
= lattice_set_instr(lattice_device
->tap
, LSCC_REFRESH
, TAP_IDLE
);
314 if (retval
!= ERROR_OK
)
316 return jtag_execute_queue();