OpenOCD
certus.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 "certus.h"
13 #include "lattice.h"
14 #include "lattice_cmd.h"
15 
16 #define LSC_ENABLE_X 0x74
17 #define LSC_REFRESH 0x79
18 #define LSC_DEVICE_CTRL 0x7D
19 
20 int lattice_certus_read_status(struct jtag_tap *tap, uint64_t *status, uint64_t out)
21 {
23 }
24 
25 int lattice_certus_read_usercode(struct jtag_tap *tap, uint32_t *usercode, uint32_t out)
26 {
27  return lattice_read_u32_register(tap, READ_USERCODE, usercode, out, false);
28 }
29 
30 int lattice_certus_write_usercode(struct lattice_pld_device *lattice_device, uint32_t usercode)
31 {
32  LOG_ERROR("Not supported to write usercode on certus devices");
33  return ERROR_FAIL;
34 }
35 
37 {
38  struct scan_field field;
39 
40  int retval = lattice_set_instr(tap, LSC_ENABLE_X, TAP_IDLE);
41  if (retval != ERROR_OK)
42  return retval;
43 
44  uint8_t buffer = 0x0;
45  field.num_bits = 8;
46  field.out_value = &buffer;
47  field.in_value = NULL;
48  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
50 
51  return jtag_execute_queue();
52 }
53 
54 static int lattice_certus_erase_device(struct lattice_pld_device *lattice_device)
55 {
56  struct jtag_tap *tap = lattice_device->tap;
57  if (!tap)
58  return ERROR_FAIL;
59 
60  int retval = lattice_set_instr(tap, LSC_DEVICE_CTRL, TAP_IRPAUSE);
61  if (retval != ERROR_OK)
62  return retval;
63 
64  struct scan_field field;
65  uint8_t buffer = 8;
66  field.num_bits = 8;
67  field.out_value = &buffer;
68  field.in_value = NULL;
69  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
71  retval = jtag_execute_queue();
72  if (retval != ERROR_OK)
73  return retval;
74 
76  if (retval != ERROR_OK)
77  return retval;
78  buffer = 0;
79  field.num_bits = 8;
80  field.out_value = &buffer;
81  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
83  retval = jtag_execute_queue();
84  if (retval != ERROR_OK)
85  return retval;
86 
87  retval = lattice_set_instr(tap, ISC_ERASE, TAP_IDLE);
88  if (retval != ERROR_OK)
89  return retval;
90  buffer = 0;
91  field.num_bits = 8;
92  field.out_value = &buffer;
93  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
95  jtag_add_sleep(5000);
96  retval = jtag_execute_queue();
97  if (retval != ERROR_OK)
98  return retval;
99 
100  /* check done is cleared and fail is cleared */
101  const uint64_t status_done_flag = 0x100;
102  const uint64_t status_fail_flag = 0x2000;
103  return lattice_verify_status_register_u64(lattice_device, 0x0, 0x0, status_done_flag | status_fail_flag);
104 }
105 
107 {
108  struct scan_field field;
109 
110  int retval = lattice_set_instr(tap, LSC_REFRESH, TAP_IDLE);
111  if (retval != ERROR_OK)
112  return retval;
114  retval = jtag_execute_queue();
115  if (retval != ERROR_OK)
116  return retval;
117 
118  retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
119  if (retval != ERROR_OK)
120  return retval;
121  uint8_t buffer = 0;
122  field.num_bits = 8;
123  field.out_value = &buffer;
124  field.in_value = NULL;
125  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
127  return jtag_execute_queue();
128 }
129 
130 static int lattice_certus_init_address(struct jtag_tap *tap)
131 {
132  int retval = lattice_set_instr(tap, LSC_INIT_ADDRESS, TAP_IDLE);
133  if (retval != ERROR_OK)
134  return retval;
136  return jtag_execute_queue();
137 }
138 
140 {
141  int retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
142  if (retval != ERROR_OK)
143  return retval;
145  retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
146  if (retval != ERROR_OK)
147  return retval;
149  return jtag_execute_queue();
150 }
151 
152 static int lattice_certus_program_config_map(struct jtag_tap *tap, struct lattice_bit_file *bit_file)
153 {
154  struct scan_field field;
155 
156  int retval = lattice_set_instr(tap, LSC_BITSTREAM_BURST, TAP_IDLE);
157  if (retval != ERROR_OK)
158  return retval;
159 
160  field.num_bits = (bit_file->raw_bit.length - bit_file->offset) * 8;
161  field.out_value = bit_file->raw_bit.data + bit_file->offset;
162  field.in_value = NULL;
163  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
164 
165  return jtag_execute_queue();
166 }
167 
168 int lattice_certus_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
169 {
170  struct jtag_tap *tap = lattice_device->tap;
171  if (!tap)
172  return ERROR_FAIL;
173 
174  int retval = lattice_preload(lattice_device);
175  if (retval != ERROR_OK)
176  return retval;
177 
178  /* check password protection is disabled */
179  const uint64_t status_pwd_protection = 0x20000;
180  retval = lattice_verify_status_register_u64(lattice_device, 0x0, 0x0, status_pwd_protection);
181  if (retval != ERROR_OK) {
182  LOG_ERROR("Password protection is set");
183  return retval;
184  }
185 
187  if (retval != ERROR_OK)
188  return retval;
189 
190  /* Check the SRAM Erase Lock */
191  const uint64_t status_otp = 0x40;
192  retval = lattice_verify_status_register_u64(lattice_device, 0x0, status_otp, status_otp);
193  if (retval != ERROR_OK) {
194  LOG_ERROR("NV User Feature Sector OTP is Set");
195  return retval;
196  }
197 
198  /* Check the SRAM Lock */
199  const uint64_t status_write_protected = 0x400;
200  retval = lattice_verify_status_register_u64(lattice_device, 0x0, 0x0, status_write_protected);
201  if (retval != ERROR_OK) {
202  LOG_ERROR("NV User Feature Sector OTP is Set");
203  return retval;
204  }
205 
206  retval = lattice_certus_enable_programming(tap);
207  if (retval != ERROR_OK) {
208  LOG_ERROR("failed to enable programming mode");
209  return retval;
210  }
211 
212  retval = lattice_certus_erase_device(lattice_device);
213  if (retval != ERROR_OK) {
214  LOG_ERROR("erasing device failed");
215  return retval;
216  }
217 
218  retval = lattice_certus_init_address(tap);
219  if (retval != ERROR_OK)
220  return retval;
221 
222  retval = lattice_certus_program_config_map(tap, bit_file);
223  if (retval != ERROR_OK)
224  return retval;
225  const uint32_t expected = 0x100; // done
226  const uint32_t mask = expected |
227  0x3000 | // Busy Flag and Fail Flag
228  0xf000000; // BSE Error
229  retval = lattice_verify_status_register_u64(lattice_device, 0x0, 0x100, mask);
230  if (retval != ERROR_OK)
231  return retval;
232 
234 }
235 
237 {
238  if (!pld_device_info)
239  return ERROR_FAIL;
240 
241  struct jtag_tap *tap = pld_device_info->tap;
242  if (!tap)
243  return ERROR_FAIL;
244 
245  if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) == PROGRAM_SPI)
246  return ERROR_OK;
247 
248  // erase configuration
249  int retval = lattice_preload(pld_device_info);
250  if (retval != ERROR_OK)
251  return retval;
252 
253  retval = lattice_certus_enable_programming(tap);
254  if (retval != ERROR_OK)
255  return retval;
256 
257  retval = lattice_certus_erase_device(pld_device_info);
258  if (retval != ERROR_OK) {
259  LOG_ERROR("erasing device failed");
260  return retval;
261  }
262 
264  if (retval != ERROR_OK)
265  return retval;
266 
267  // connect jtag to spi pins
268  retval = lattice_set_instr(tap, PROGRAM_SPI, TAP_IDLE);
269  if (retval != ERROR_OK)
270  return retval;
271 
272  struct scan_field field;
273  uint8_t buffer[2] = {0xfe, 0x68};
274  field.num_bits = 16;
275  field.out_value = buffer;
276  field.in_value = NULL;
277  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
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  /* Connecting it again takes way too long to do it multiple times for writing
292  a bitstream (ca. 0.4s each access).
293  We just leave it connected since SCS will not be active when not in shift_dr state.
294  So there is no need to change instruction, just make sure we are not in shift dr state. */
296  return jtag_execute_queue();
297 }
298 
299 int lattice_certus_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits)
300 {
301  if (!pld_device_info)
302  return ERROR_FAIL;
303 
304  *facing_read_bits = 0;
305 
306  return ERROR_OK;
307 }
308 
309 int lattice_certus_refresh(struct lattice_pld_device *lattice_device)
310 {
311  struct jtag_tap *tap = lattice_device->tap;
312  if (!tap)
313  return ERROR_FAIL;
314 
315  int retval = lattice_preload(lattice_device);
316  if (retval != ERROR_OK)
317  return retval;
318 
319  retval = lattice_set_instr(tap, LSC_REFRESH, TAP_IDLE);
320  if (retval != ERROR_OK)
321  return retval;
323  jtag_add_sleep(200000);
324  retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
325  if (retval != ERROR_OK)
326  return retval;
328  jtag_add_sleep(1000);
329 
330  return jtag_execute_queue();
331 }
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:99
int lattice_certus_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file)
Definition: certus.c:168
static int lattice_certus_enable_programming(struct jtag_tap *tap)
Definition: certus.c:106
#define LSC_ENABLE_X
Definition: certus.c:16
int lattice_certus_refresh(struct lattice_pld_device *lattice_device)
Definition: certus.c:309
static int lattice_certus_enable_transparent_mode(struct jtag_tap *tap)
Definition: certus.c:36
static int lattice_certus_exit_programming_mode(struct jtag_tap *tap)
Definition: certus.c:139
static int lattice_certus_init_address(struct jtag_tap *tap)
Definition: certus.c:130
static int lattice_certus_erase_device(struct lattice_pld_device *lattice_device)
Definition: certus.c:54
int lattice_certus_write_usercode(struct lattice_pld_device *lattice_device, uint32_t usercode)
Definition: certus.c:30
int lattice_certus_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits)
Definition: certus.c:299
int lattice_certus_read_usercode(struct jtag_tap *tap, uint32_t *usercode, uint32_t out)
Definition: certus.c:25
int lattice_certus_read_status(struct jtag_tap *tap, uint64_t *status, uint64_t out)
Definition: certus.c:20
int lattice_certus_connect_spi_to_jtag(struct lattice_pld_device *pld_device_info)
Definition: certus.c:236
#define LSC_DEVICE_CTRL
Definition: certus.c:18
#define LSC_REFRESH
Definition: certus.c:17
int lattice_certus_disconnect_spi_from_jtag(struct lattice_pld_device *pld_device_info)
Definition: certus.c:282
static int lattice_certus_program_config_map(struct jtag_tap *tap, struct lattice_bit_file *bit_file)
Definition: certus.c:152
#define READ_USERCODE
Definition: ecp2_3.c:20
#define PROGRAM_SPI
Definition: ecp2_3.c:24
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_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_u64_register(struct jtag_tap *tap, uint8_t cmd, uint64_t *in_val, uint64_t out_val)
Definition: lattice.c:126
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_status_register_u64(struct lattice_pld_device *lattice_device, uint64_t out, uint64_t expected, uint64_t mask)
Definition: lattice.c:257
#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 LOG_ERROR(expr ...)
Definition: log.h:132
#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
int ir_length
size of instruction register
Definition: jtag.h:110
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
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
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17