OpenOCD
ecp2_3.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2022 by Daniel Anselmi *
5  * danselmi@gmx.ch *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "ecp2_3.h"
13 #include "lattice.h"
14 
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
25 
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
31 
32 int lattice_ecp2_3_read_status(struct jtag_tap *tap, uint32_t *status, uint32_t out, bool do_idle)
33 {
34  return lattice_read_u32_register(tap, LSCC_READ_STATUS, status, out, do_idle);
35 }
36 
37 int lattice_ecp2_3_read_usercode(struct jtag_tap *tap, uint32_t *usercode, uint32_t out)
38 {
39  return lattice_read_u32_register(tap, READ_USERCODE, usercode, out, false);
40 }
41 
42 int lattice_ecp2_3_write_usercode(struct lattice_pld_device *lattice_device, uint32_t usercode)
43 {
44  struct jtag_tap *tap = lattice_device->tap;
45  if (!tap)
46  return ERROR_FAIL;
47 
48  int retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
49  if (retval != ERROR_OK)
50  return retval;
52  jtag_add_sleep(20000);
53 
55  if (retval != ERROR_OK)
56  return retval;
57 
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);
66  jtag_add_sleep(2000);
67 
68  retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
69  if (retval != ERROR_OK)
70  return retval;
72  jtag_add_sleep(200000);
73 
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);
78 }
79 
80 static int lattice_ecp2_3_erase_device(struct lattice_pld_device *lattice_device)
81 {
82  struct jtag_tap *tap = lattice_device->tap;
83  if (!tap)
84  return ERROR_FAIL;
85 
86  /* program user code with all bits set */
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);
97  jtag_add_sleep(2000);
98 
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;
106 
107  retval = lattice_set_instr(tap, ISC_ERASE, TAP_IDLE);
108  if (retval != ERROR_OK)
109  return retval;
111  if (lattice_device->family == LATTICE_ECP2)
112  jtag_add_sleep(100000);
113  else
114  jtag_add_sleep(2000000);
115 
117  if (retval != ERROR_OK)
118  return retval;
120  jtag_add_sleep(2000);
121 
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);
125 }
126 
127 static int lattice_ecp2_3_program_config_map(struct lattice_pld_device *lattice_device,
128  struct lattice_bit_file *bit_file)
129 {
130  struct jtag_tap *tap = lattice_device->tap;
131  if (!tap)
132  return ERROR_FAIL;
133 
134  int retval = lattice_set_instr(tap, LSCC_RESET_ADDRESS, TAP_IDLE);
135  if (retval != ERROR_OK)
136  return retval;
138  jtag_add_sleep(2000);
139 
140  struct scan_field field;
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);
149  jtag_add_sleep(2000);
150  return jtag_execute_queue();
151 }
152 
154 {
155  struct jtag_tap *tap = lattice_device->tap;
156  if (!tap)
157  return ERROR_FAIL;
158 
159  int retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
160  if (retval != ERROR_OK)
161  return retval;
163  jtag_add_sleep(200000);
164  retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
165  if (retval != ERROR_OK)
166  return retval;
168  jtag_add_sleep(1000);
169  return jtag_execute_queue();
170 }
171 
172 int lattice_ecp2_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
173 {
174  struct jtag_tap *tap = lattice_device->tap;
175  if (!tap)
176  return ERROR_FAIL;
177 
178  int retval = lattice_preload(lattice_device);
179  if (retval != ERROR_OK)
180  return retval;
181 
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;
190  jtag_add_sleep(20000);
191 
192  /* Erase the device */
193  retval = lattice_ecp2_3_erase_device(lattice_device);
194  if (retval != ERROR_OK)
195  return retval;
196 
197  /* Program Fuse Map */
198  retval = lattice_ecp2_3_program_config_map(lattice_device, bit_file);
199  if (retval != ERROR_OK)
200  return retval;
201 
202  retval = lattice_ecp2_3_exit_programming_mode(lattice_device);
203  if (retval != ERROR_OK)
204  return retval;
205 
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);
210 }
211 
212 int lattice_ecp3_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
213 {
214  struct jtag_tap *tap = lattice_device->tap;
215  if (!tap)
216  return ERROR_FAIL;
217 
218  /* Program Bscan register */
219  int retval = lattice_preload(lattice_device);
220  if (retval != ERROR_OK)
221  return retval;
222 
223  /* Enable the programming mode */
224  retval = lattice_set_instr(tap, LSCC_REFRESH, TAP_IDLE);
225  if (retval != ERROR_OK)
226  return retval;
228  jtag_add_sleep(500000);
229  retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
230  if (retval != ERROR_OK)
231  return retval;
233  jtag_add_sleep(20000);
234 
235  retval = lattice_ecp2_3_erase_device(lattice_device);
236  if (retval != ERROR_OK)
237  return retval;
238 
239  /* Program Fuse Map */
240  retval = lattice_ecp2_3_program_config_map(lattice_device, bit_file);
241  if (retval != ERROR_OK)
242  return retval;
243 
244  retval = lattice_ecp2_3_exit_programming_mode(lattice_device);
245  if (retval != ERROR_OK)
246  return retval;
247 
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);
252 }
253 
255 {
256  if (!pld_device_info)
257  return ERROR_FAIL;
258 
259  struct jtag_tap *tap = pld_device_info->tap;
260  if (!tap)
261  return ERROR_FAIL;
262 
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;
273 
274  // connect jtag to spi pins
275  retval = lattice_set_instr(tap, PROGRAM_SPI, TAP_IDLE);
276  if (retval != ERROR_OK)
277  return retval;
278 
279  return jtag_execute_queue();
280 }
281 
283 {
284  if (!pld_device_info)
285  return ERROR_FAIL;
286 
287  struct jtag_tap *tap = pld_device_info->tap;
288  if (!tap)
289  return ERROR_FAIL;
290 
291  int retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
292  if (retval != ERROR_OK)
293  return retval;
294 
295  return jtag_execute_queue();
296 }
297 
298 int lattice_ecp2_3_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits)
299 {
300  if (!pld_device_info)
301  return ERROR_FAIL;
302 
303  *facing_read_bits = 1;
304 
305  return ERROR_OK;
306 }
307 
308 int lattice_ecp2_3_refresh(struct lattice_pld_device *lattice_device)
309 {
310  if (!lattice_device || !lattice_device->tap)
311  return ERROR_FAIL;
312 
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();
317 }
int lattice_ecp2_3_read_status(struct jtag_tap *tap, uint32_t *status, uint32_t out, bool do_idle)
Definition: ecp2_3.c:32
int lattice_ecp2_3_disconnect_spi_from_jtag(struct lattice_pld_device *pld_device_info)
Definition: ecp2_3.c:282
static int lattice_ecp2_3_erase_device(struct lattice_pld_device *lattice_device)
Definition: ecp2_3.c:80
#define ISC_PROGRAM_USERCODE
Definition: ecp2_3.c:18
#define LSCC_READ_STATUS
Definition: ecp2_3.c:22
int lattice_ecp2_3_read_usercode(struct jtag_tap *tap, uint32_t *usercode, uint32_t out)
Definition: ecp2_3.c:37
#define STATUS_DONE_BIT
Definition: ecp2_3.c:26
static int lattice_ecp2_3_program_config_map(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
Definition: ecp2_3.c:127
#define ISC_ENABLE
Definition: ecp2_3.c:16
#define READ_USERCODE
Definition: ecp2_3.c:20
#define LSCC_REFRESH
Definition: ecp2_3.c:15
int lattice_ecp2_3_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits)
Definition: ecp2_3.c:298
int lattice_ecp3_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
Definition: ecp2_3.c:212
#define PROGRAM_SPI
Definition: ecp2_3.c:24
#define ISC_DISABLE
Definition: ecp2_3.c:21
static int lattice_ecp2_3_exit_programming_mode(struct lattice_pld_device *lattice_device)
Definition: ecp2_3.c:153
#define LSCC_BITSTREAM_BURST
Definition: ecp2_3.c:23
#define LSCC_RESET_ADDRESS
Definition: ecp2_3.c:17
int lattice_ecp2_3_write_usercode(struct lattice_pld_device *lattice_device, uint32_t usercode)
Definition: ecp2_3.c:42
int lattice_ecp2_3_refresh(struct lattice_pld_device *lattice_device)
Definition: ecp2_3.c:308
#define REGISTER_ALL_BITS_0
Definition: ecp2_3.c:30
#define ISC_ERASE
Definition: ecp2_3.c:19
int lattice_ecp2_3_connect_spi_to_jtag(struct lattice_pld_device *pld_device_info)
Definition: ecp2_3.c:254
#define STATUS_ERROR_BITS_ECP2
Definition: ecp2_3.c:27
#define REGISTER_ALL_BITS_1
Definition: ecp2_3.c:29
#define STATUS_ERROR_BITS_ECP3
Definition: ecp2_3.c:28
int lattice_ecp2_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
Definition: ecp2_3.c:172
int mask
Definition: esirisc.c:1741
#define BYPASS
Definition: gatemate.c:19
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1037
void jtag_add_sleep(uint32_t us)
Definition: jtag/core.c:870
void jtag_add_runtest(int num_cycles, tap_state_t state)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:592
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:451
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_IRPAUSE
Definition: jtag.h:52
int lattice_verify_status_register_u32(struct lattice_pld_device *lattice_device, uint32_t out, uint32_t expected, uint32_t mask, bool do_idle)
Definition: lattice.c:241
int lattice_set_instr(struct jtag_tap *tap, uint8_t new_instr, tap_state_t endstate)
Definition: lattice.c:62
int lattice_preload(struct lattice_pld_device *lattice_device)
Definition: lattice.c:147
int lattice_read_u32_register(struct jtag_tap *tap, uint8_t cmd, uint32_t *in_val, uint32_t out_val, bool do_idle)
Definition: lattice.c:100
int lattice_verify_usercode(struct lattice_pld_device *lattice_device, uint32_t out, uint32_t expected, uint32_t mask)
Definition: lattice.c:187
@ LATTICE_ECP2
Definition: lattice_bit.h:24
#define ERROR_FAIL
Definition: log.h:170
#define ERROR_OK
Definition: log.h:164
Definition: jtag.h:101
uint8_t * expected
Capture-IR expected value.
Definition: jtag.h:112
struct raw_bit_file raw_bit
Definition: lattice_bit.h:16
struct jtag_tap * tap
Definition: lattice.h:18
enum lattice_family_e family
Definition: lattice.h:20
uint8_t * data
Definition: raw_bit.h:16
size_t length
Definition: raw_bit.h:15
This structure defines a single scan field in the scan.
Definition: jtag.h:87
int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17