jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / pld / ecp2_3.c
blob5dfea9a2770a7fc8bae5c3bf38b8140881e6b61e
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2022 by Daniel Anselmi *
5 * danselmi@gmx.ch *
6 ***************************************************************************/
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
12 #include "ecp2_3.h"
13 #include "lattice.h"
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;
45 if (!tap)
46 return ERROR_FAIL;
48 int retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
49 if (retval != ERROR_OK)
50 return retval;
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)
56 return retval;
58 struct scan_field field;
59 uint8_t buffer[4];
60 h_u32_to_le(buffer, usercode);
61 field.num_bits = 32;
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);
66 jtag_add_sleep(2000);
68 retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
69 if (retval != ERROR_OK)
70 return retval;
71 jtag_add_runtest(5, TAP_IDLE);
72 jtag_add_sleep(200000);
74 retval = jtag_execute_queue();
75 if (retval != ERROR_OK)
76 return retval;
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;
83 if (!tap)
84 return ERROR_FAIL;
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)
89 return retval;
90 struct scan_field field;
91 uint8_t buffer[4] = {0xff, 0xff, 0xff, 0xff};
92 field.num_bits = 32;
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);
97 jtag_add_sleep(2000);
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)
105 return retval;
107 retval = lattice_set_instr(tap, ISC_ERASE, TAP_IDLE);
108 if (retval != ERROR_OK)
109 return retval;
110 jtag_add_runtest(5, TAP_IDLE);
111 if (lattice_device->family == LATTICE_ECP2)
112 jtag_add_sleep(100000);
113 else
114 jtag_add_sleep(2000000);
116 retval = lattice_set_instr(tap, LSCC_RESET_ADDRESS, TAP_IDLE);
117 if (retval != ERROR_OK)
118 return retval;
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;
131 if (!tap)
132 return ERROR_FAIL;
134 int retval = lattice_set_instr(tap, LSCC_RESET_ADDRESS, TAP_IDLE);
135 if (retval != ERROR_OK)
136 return retval;
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)
143 return retval;
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;
156 if (!tap)
157 return ERROR_FAIL;
159 int retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
160 if (retval != ERROR_OK)
161 return retval;
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)
166 return retval;
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;
175 if (!tap)
176 return ERROR_FAIL;
178 int retval = lattice_preload(lattice_device);
179 if (retval != ERROR_OK)
180 return retval;
182 /* Enable the programming mode */
183 retval = lattice_set_instr(tap, LSCC_REFRESH, TAP_IDLE);
184 if (retval != ERROR_OK)
185 return retval;
186 retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
187 if (retval != ERROR_OK)
188 return retval;
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)
195 return retval;
197 /* Program Fuse Map */
198 retval = lattice_ecp2_3_program_config_map(lattice_device, bit_file);
199 if (retval != ERROR_OK)
200 return retval;
202 retval = lattice_ecp2_3_exit_programming_mode(lattice_device);
203 if (retval != ERROR_OK)
204 return retval;
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;
215 if (!tap)
216 return ERROR_FAIL;
218 /* Program Bscan register */
219 int retval = lattice_preload(lattice_device);
220 if (retval != ERROR_OK)
221 return retval;
223 /* Enable the programming mode */
224 retval = lattice_set_instr(tap, LSCC_REFRESH, TAP_IDLE);
225 if (retval != ERROR_OK)
226 return retval;
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)
231 return retval;
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)
237 return retval;
239 /* Program Fuse Map */
240 retval = lattice_ecp2_3_program_config_map(lattice_device, bit_file);
241 if (retval != ERROR_OK)
242 return retval;
244 retval = lattice_ecp2_3_exit_programming_mode(lattice_device);
245 if (retval != ERROR_OK)
246 return retval;
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)
257 return ERROR_FAIL;
259 struct jtag_tap *tap = pld_device_info->tap;
260 if (!tap)
261 return ERROR_FAIL;
263 // erase configuration
264 int retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
265 if (retval != ERROR_OK)
266 return retval;
267 retval = lattice_set_instr(tap, ISC_ERASE, TAP_IDLE);
268 if (retval != ERROR_OK)
269 return retval;
270 retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
271 if (retval != ERROR_OK)
272 return retval;
274 // connect jtag to spi pins
275 retval = lattice_set_instr(tap, PROGRAM_SPI, TAP_IDLE);
276 if (retval != ERROR_OK)
277 return retval;
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)
285 return ERROR_FAIL;
287 struct jtag_tap *tap = pld_device_info->tap;
288 if (!tap)
289 return ERROR_FAIL;
291 int retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
292 if (retval != ERROR_OK)
293 return retval;
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)
301 return ERROR_FAIL;
303 *facing_read_bits = 1;
305 return ERROR_OK;
308 int lattice_ecp2_3_refresh(struct lattice_pld_device *lattice_device)
310 if (!lattice_device || !lattice_device->tap)
311 return ERROR_FAIL;
313 int retval = lattice_set_instr(lattice_device->tap, LSCC_REFRESH, TAP_IDLE);
314 if (retval != ERROR_OK)
315 return retval;
316 return jtag_execute_queue();