OpenOCD
ecp5.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 "ecp5.h"
13 #include "lattice.h"
14 #include "lattice_cmd.h"
15 
16 #define ISC_PROGRAM_USERCODE 0xC2
17 
18 #define STATUS_DONE_BIT 0x00000100
19 #define STATUS_ERROR_BITS 0x00020040
20 #define STATUS_FEA_OTP 0x00004000
21 #define STATUS_FAIL_FLAG 0x00002000
22 #define STATUS_BUSY_FLAG 0x00001000
23 #define REGISTER_ALL_BITS_1 0xffffffff
24 
25 int lattice_ecp5_read_status(struct jtag_tap *tap, uint32_t *status, uint32_t out, bool do_idle)
26 {
27  return lattice_read_u32_register(tap, LSC_READ_STATUS, status, out, do_idle);
28 }
29 
30 int lattice_ecp5_read_usercode(struct jtag_tap *tap, uint32_t *usercode, uint32_t out)
31 {
32  return lattice_read_u32_register(tap, READ_USERCODE, usercode, out, true);
33 }
34 
35 int lattice_ecp5_write_usercode(struct lattice_pld_device *lattice_device, uint32_t usercode)
36 {
37  struct jtag_tap *tap = lattice_device->tap;
38  if (!tap)
39  return ERROR_FAIL;
40 
41  int retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
42  if (retval != ERROR_OK)
43  return retval;
45  jtag_add_sleep(20000);
46 
48  if (retval != ERROR_OK)
49  return retval;
50 
51  uint8_t buffer[4];
52  struct scan_field field;
53  h_u32_to_le(buffer, usercode);
54  field.num_bits = 32;
55  field.out_value = buffer;
56  field.in_value = NULL;
57  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
59  jtag_add_sleep(2000);
60 
61  retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
62  if (retval != ERROR_OK)
63  return retval;
65  jtag_add_sleep(200000);
66 
67  retval = jtag_execute_queue();
68  if (retval != ERROR_OK)
69  return retval;
70  return lattice_verify_usercode(lattice_device, 0x0, usercode, REGISTER_ALL_BITS_1);
71 }
72 
74 {
75  int retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
76  if (retval != ERROR_OK)
77  return retval;
78 
79  struct scan_field field;
80  uint8_t buffer = 0x0;
81  field.num_bits = 8;
82  field.out_value = &buffer;
83  field.in_value = NULL;
84  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
86  jtag_add_sleep(10000);
87 
88  return jtag_execute_queue();
89 }
90 
91 static int lattice_ecp5_erase_sram(struct jtag_tap *tap)
92 {
93  int retval = lattice_set_instr(tap, ISC_ERASE, TAP_IRPAUSE);
94  if (retval != ERROR_OK)
95  return retval;
96 
97  struct scan_field field;
98  uint8_t buffer = 1;
99  field.num_bits = 8;
100  field.out_value = &buffer;
101  field.in_value = NULL;
102  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
104  jtag_add_sleep(200000);
105  return jtag_execute_queue();
106 }
107 
108 static int lattice_ecp5_init_address(struct jtag_tap *tap)
109 {
110  int retval = lattice_set_instr(tap, LSC_INIT_ADDRESS, TAP_IDLE);
111  if (retval != ERROR_OK)
112  return retval;
113 
114  struct scan_field field;
115  uint8_t buffer = 1;
116  field.num_bits = 8;
117  field.out_value = &buffer;
118  field.in_value = NULL;
119  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
121  jtag_add_sleep(10000);
122  return jtag_execute_queue();
123 }
124 
125 static int lattice_ecp5_program_config_map(struct jtag_tap *tap, struct lattice_bit_file *bit_file)
126 {
127  int retval = lattice_set_instr(tap, LSC_BITSTREAM_BURST, TAP_IDLE);
128  if (retval != ERROR_OK)
129  return retval;
131  jtag_add_sleep(10000);
132 
133  struct scan_field field;
134  field.num_bits = (bit_file->raw_bit.length - bit_file->offset) * 8;
135  field.out_value = bit_file->raw_bit.data + bit_file->offset;
136  field.in_value = NULL;
137  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
138  retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
139  if (retval != ERROR_OK)
140  return retval;
142  jtag_add_sleep(10000);
143 
144  return jtag_execute_queue();
145 }
146 
148 {
149  int retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
150  if (retval != ERROR_OK)
151  return retval;
153  jtag_add_sleep(200000);
154  retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
155  if (retval != ERROR_OK)
156  return retval;
158  jtag_add_sleep(1000);
159  return jtag_execute_queue();
160 }
161 
162 int lattice_ecp5_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
163 {
164  struct jtag_tap *tap = lattice_device->tap;
165  if (!tap)
166  return ERROR_FAIL;
167 
168  int retval = lattice_preload(lattice_device);
169  if (retval != ERROR_OK)
170  return retval;
171 
173  if (retval != ERROR_OK)
174  return retval;
175 
176  const uint32_t out = 0x0;
177  const uint32_t expected1 = 0x0;
178  const uint32_t mask1 = STATUS_ERROR_BITS | STATUS_FEA_OTP;
179  retval = lattice_verify_status_register_u32(lattice_device, out, expected1, mask1, true);
180  if (retval != ERROR_OK)
181  return retval;
182 
183  retval = lattice_ecp5_erase_sram(tap);
184  if (retval != ERROR_OK)
185  return retval;
186 
187  const uint32_t mask2 = STATUS_FAIL_FLAG | STATUS_BUSY_FLAG;
188  retval = lattice_verify_status_register_u32(lattice_device, out, expected1, mask2, false);
189  if (retval != ERROR_OK)
190  return retval;
191 
192  retval = lattice_ecp5_init_address(tap);
193  if (retval != ERROR_OK)
194  return retval;
195 
196  retval = lattice_ecp5_program_config_map(tap, bit_file);
197  if (retval != ERROR_OK)
198  return retval;
199 
201  if (retval != ERROR_OK)
202  return retval;
203 
204  const uint32_t expected2 = STATUS_DONE_BIT;
205  const uint32_t mask3 = STATUS_DONE_BIT | STATUS_FAIL_FLAG;
206  return lattice_verify_status_register_u32(lattice_device, out, expected2, mask3, false);
207 }
208 
210 {
211  if (!pld_device_info)
212  return ERROR_FAIL;
213 
214  struct jtag_tap *tap = pld_device_info->tap;
215  if (!tap)
216  return ERROR_FAIL;
217 
218  if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) == PROGRAM_SPI)
219  return ERROR_OK;
220 
221  // erase configuration
222  int retval = lattice_preload(pld_device_info);
223  if (retval != ERROR_OK)
224  return retval;
225 
227  if (retval != ERROR_OK)
228  return retval;
229 
230  retval = lattice_ecp5_erase_sram(tap);
231  if (retval != ERROR_OK)
232  return retval;
233 
235  if (retval != ERROR_OK)
236  return retval;
237 
238  // connect jtag to spi pins
239  retval = lattice_set_instr(tap, PROGRAM_SPI, TAP_IDLE);
240  if (retval != ERROR_OK)
241  return retval;
242 
243  struct scan_field field;
244  uint8_t buffer[2] = {0xfe, 0x68};
245  field.num_bits = 16;
246  field.out_value = buffer;
247  field.in_value = NULL;
248  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
249 
250  return jtag_execute_queue();
251 }
252 
254 {
255  if (!pld_device_info)
256  return ERROR_FAIL;
257 
258  struct jtag_tap *tap = pld_device_info->tap;
259  if (!tap)
260  return ERROR_FAIL;
261 
262  /* Connecting it again takes way too long to do it multiple times for writing
263  a bitstream (ca. 0.4s each access).
264  We just leave it connected since SCS will not be active when not in shift_dr state.
265  So there is no need to change instruction, just make sure we are not in shift dr state. */
267  return jtag_execute_queue();
268 }
269 
270 int lattice_ecp5_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits)
271 {
272  if (!pld_device_info)
273  return ERROR_FAIL;
274 
275  *facing_read_bits = 0;
276 
277  return ERROR_OK;
278 }
279 
280 int lattice_ecp5_refresh(struct lattice_pld_device *lattice_device)
281 {
282  struct jtag_tap *tap = lattice_device->tap;
283  if (!tap)
284  return ERROR_FAIL;
285 
286  int retval = lattice_preload(lattice_device);
287  if (retval != ERROR_OK)
288  return retval;
289 
290  retval = lattice_set_instr(tap, LSC_REFRESH, TAP_IDLE);
291  if (retval != ERROR_OK)
292  return retval;
294  jtag_add_sleep(200000);
295  retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
296  if (retval != ERROR_OK)
297  return retval;
299  jtag_add_sleep(1000);
300 
301  return jtag_execute_queue();
302 }
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
#define LSC_REFRESH
Definition: certus.c:17
#define READ_USERCODE
Definition: ecp2_3.c:20
#define PROGRAM_SPI
Definition: ecp2_3.c:24
static int lattice_ecp5_program_config_map(struct jtag_tap *tap, struct lattice_bit_file *bit_file)
Definition: ecp5.c:125
int lattice_ecp5_refresh(struct lattice_pld_device *lattice_device)
Definition: ecp5.c:280
int lattice_ecp5_connect_spi_to_jtag(struct lattice_pld_device *pld_device_info)
Definition: ecp5.c:209
#define ISC_PROGRAM_USERCODE
Definition: ecp5.c:16
int lattice_ecp5_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
Definition: ecp5.c:162
#define STATUS_FAIL_FLAG
Definition: ecp5.c:21
int lattice_ecp5_disconnect_spi_from_jtag(struct lattice_pld_device *pld_device_info)
Definition: ecp5.c:253
#define STATUS_ERROR_BITS
Definition: ecp5.c:19
int lattice_ecp5_write_usercode(struct lattice_pld_device *lattice_device, uint32_t usercode)
Definition: ecp5.c:35
#define STATUS_DONE_BIT
Definition: ecp5.c:18
int lattice_ecp5_read_usercode(struct jtag_tap *tap, uint32_t *usercode, uint32_t out)
Definition: ecp5.c:30
#define STATUS_FEA_OTP
Definition: ecp5.c:20
int lattice_ecp5_read_status(struct jtag_tap *tap, uint32_t *status, uint32_t out, bool do_idle)
Definition: ecp5.c:25
static int lattice_ecp5_init_address(struct jtag_tap *tap)
Definition: ecp5.c:108
int lattice_ecp5_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits)
Definition: ecp5.c:270
static int lattice_ecp5_erase_sram(struct jtag_tap *tap)
Definition: ecp5.c:91
#define STATUS_BUSY_FLAG
Definition: ecp5.c:22
static int lattice_ecp5_exit_programming_mode(struct jtag_tap *tap)
Definition: ecp5.c:147
#define REGISTER_ALL_BITS_1
Definition: ecp5.c:23
static int lattice_ecp5_enable_sram_programming(struct jtag_tap *tap)
Definition: ecp5.c:73
#define BYPASS
Definition: gatemate.c:19
void jtag_add_runtest(unsigned 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
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_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
#define LSC_INIT_ADDRESS
Definition: lattice_cmd.h:15
#define LSC_READ_STATUS
Definition: lattice_cmd.h:14
#define LSC_BITSTREAM_BURST
Definition: lattice_cmd.h:17
#define ERROR_FAIL
Definition: log.h:170
#define ERROR_OK
Definition: log.h:164
#define ISC_ENABLE
Definition: str9xpec.c:23
#define ISC_DISABLE
Definition: str9xpec.c:24
#define ISC_ERASE
Definition: str9xpec.c:31
Definition: jtag.h:101
uint8_t * cur_instr
current instruction
Definition: jtag.h:132
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
struct raw_bit_file raw_bit
Definition: lattice_bit.h:16
struct jtag_tap * tap
Definition: lattice.h:18
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
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
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
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