OpenOCD
efinix.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 <jtag/jtag.h>
13 
14 #include "pld.h"
15 #include "raw_bit.h"
16 
17 #define PROGRAM 0x4
18 #define ENTERUSER 0x7
19 #define USER1 0x8
20 #define USER2 0x9
21 #define USER3 0xa
22 #define USER4 0xb
23 
28 };
29 
30 #define TRAILING_ZEROS 4000
31 #define RUNTEST_START_CYCLES 100
32 #define RUNTEST_FINISH_CYCLES 100
33 
34 struct efinix_device {
35  uint32_t idcode;
36  int num_user;
37 };
38 
40  struct jtag_tap *tap;
42 };
43 
44 static int efinix_read_bit_file(struct raw_bit_file *bit_file, const char *filename)
45 {
46  FILE *input_file = fopen(filename, "r");
47  if (!input_file) {
48  LOG_ERROR("couldn't open %s: %s", filename, strerror(errno));
50  }
51 
52  fseek(input_file, 0, SEEK_END);
53  long length = ftell(input_file);
54  fseek(input_file, 0, SEEK_SET);
55 
56  if (length < 0 || ((length % 3))) {
57  fclose(input_file);
58  LOG_ERROR("Failed to get length from file %s: %s", filename, strerror(errno));
60  }
61  bit_file->length = DIV_ROUND_UP(length, 3);
62 
63  bit_file->data = malloc(bit_file->length);
64  if (!bit_file->data) {
65  fclose(input_file);
66  LOG_ERROR("Out of memory");
68  }
69 
70  bool end_detected = false;
71  char buffer[3];
72  for (size_t idx = 0; !end_detected && idx < bit_file->length; ++idx) {
73  size_t read_count = fread(buffer, sizeof(char), 3, input_file);
74  end_detected = feof(input_file);
75  if ((read_count == 3 && buffer[2] != '\n') ||
76  (read_count != 3 && !end_detected) ||
77  (read_count != 2 && end_detected)) {
78  fclose(input_file);
79  free(bit_file->data);
80  bit_file->data = NULL;
81  LOG_ERROR("unexpected line length");
83  }
84 
85  if (!isxdigit((unsigned char)buffer[0]) ||
86  !isxdigit((unsigned char)buffer[1])) {
87  fclose(input_file);
88  free(bit_file->data);
89  bit_file->data = NULL;
90  LOG_ERROR("unexpected char in hex string");
92  }
93  unhexify(&bit_file->data[idx], buffer, 2);
94  }
95 
96  fclose(input_file);
97 
98  return ERROR_OK;
99 }
100 
101 static int efinix_read_file(struct raw_bit_file *bit_file, const char *filename)
102 {
103  if (!filename || !bit_file)
105 
106  /* check if binary .bin or ascii .bit/.hex */
107  const char *file_ending_pos = strrchr(filename, '.');
108  if (!file_ending_pos) {
109  LOG_ERROR("Unable to detect filename suffix");
111  }
112 
113  if (strcasecmp(file_ending_pos, ".bin") == 0) {
114  return cpld_read_raw_bit_file(bit_file, filename);
115  } else if ((strcasecmp(file_ending_pos, ".bit") == 0) ||
116  (strcasecmp(file_ending_pos, ".hex") == 0)) {
117  return efinix_read_bit_file(bit_file, filename);
118  }
119 
120  LOG_ERROR("Unable to detect filetype");
122 }
123 
124 static int efinix_set_instr(struct jtag_tap *tap, uint8_t new_instr)
125 {
126  struct scan_field field;
127  field.num_bits = tap->ir_length;
128  void *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
129  if (!t) {
130  LOG_ERROR("Out of memory");
131  return ERROR_FAIL;
132  }
133  field.out_value = t;
134  buf_set_u32(t, 0, field.num_bits, new_instr);
135  field.in_value = NULL;
136  jtag_add_ir_scan(tap, &field, TAP_IDLE);
137  free(t);
138  return ERROR_OK;
139 }
140 
141 static int efinix_load(struct pld_device *pld_device, const char *filename)
142 {
143  struct raw_bit_file bit_file;
144  struct scan_field field[2];
145 
147  return ERROR_FAIL;
148 
149  struct efinix_pld_device *efinix_info = pld_device->driver_priv;
150  if (!efinix_info || !efinix_info->tap)
151  return ERROR_FAIL;
152  struct jtag_tap *tap = efinix_info->tap;
153 
154  jtag_add_tlr();
155 
156  int retval = efinix_set_instr(tap, PROGRAM);
157  if (retval != ERROR_OK)
158  return retval;
160  retval = efinix_set_instr(tap, PROGRAM); /* fix for T20 */
161  if (retval != ERROR_OK)
162  return retval;
163  retval = jtag_execute_queue();
164  if (retval != ERROR_OK)
165  return retval;
166 
167  retval = efinix_read_file(&bit_file, filename);
168  if (retval != ERROR_OK)
169  return retval;
170 
171  for (size_t i = 0; i < bit_file.length; i++)
172  bit_file.data[i] = flip_u32(bit_file.data[i], 8);
173 
174  /* shift in the bitstream */
175  field[0].num_bits = bit_file.length * 8;
176  field[0].out_value = bit_file.data;
177  field[0].in_value = NULL;
178 
179  /* followed by zeros */
180  field[1].num_bits = TRAILING_ZEROS;
181  uint8_t *buf = calloc(TRAILING_ZEROS / 8, 1);
182  if (!buf) {
183  free(bit_file.data);
184  LOG_ERROR("Out of memory");
185  return ERROR_FAIL;
186  }
187  field[1].out_value = buf;
188  field[1].in_value = NULL;
189 
190  jtag_add_dr_scan(tap, 2, field, TAP_DRPAUSE);
191  retval = jtag_execute_queue();
192  free(bit_file.data);
193  free(buf);
194  if (retval != ERROR_OK)
195  return retval;
196 
197  retval = efinix_set_instr(tap, ENTERUSER);
198  if (retval != ERROR_OK)
199  return retval;
200 
201  /* entering RUN/TEST for 100 cycles */
203  retval = jtag_execute_queue();
204 
205  return retval;
206 }
207 
208 static int efinix_get_ipdbg_hub(int user_num, struct pld_device *pld_device, struct pld_ipdbg_hub *hub)
209 {
210  if (!pld_device)
211  return ERROR_FAIL;
212 
213  struct efinix_pld_device *pld_device_info = pld_device->driver_priv;
214 
215  if (!pld_device_info || !pld_device_info->tap)
216  return ERROR_FAIL;
217 
218  hub->tap = pld_device_info->tap;
219 
220  if (pld_device_info->family == EFINIX_UNKNOWN) {
221  LOG_ERROR("family unknown, please specify for 'pld create'");
222  return ERROR_FAIL;
223  }
224  int num_user = 2; /* trion */
225  if (pld_device_info->family == EFINIX_TITANIUM)
226  num_user = 4;
227 
228  if (user_num > num_user) {
229  LOG_ERROR("Devices has only user register 1 to %d", num_user);
230  return ERROR_FAIL;
231  }
232 
233  switch (user_num) {
234  case 1:
235  hub->user_ir_code = USER1;
236  break;
237  case 2:
238  hub->user_ir_code = USER2;
239  break;
240  case 3:
241  hub->user_ir_code = USER3;
242  break;
243  case 4:
244  hub->user_ir_code = USER4;
245  break;
246  default:
247  LOG_ERROR("efinix devices only have user register 1 to %d", num_user);
248  return ERROR_FAIL;
249  }
250  return ERROR_OK;
251 }
252 
253 static int efinix_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
254 {
255  *ir = USER1;
256  return ERROR_OK;
257 }
258 
259 PLD_CREATE_COMMAND_HANDLER(efinix_pld_create_command)
260 {
261  if (CMD_ARGC != 4 && CMD_ARGC != 6)
263 
264  if (strcmp(CMD_ARGV[2], "-chain-position") != 0)
266 
267  struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[3]);
268  if (!tap) {
269  command_print(CMD, "Tap: %s does not exist", CMD_ARGV[3]);
270  return ERROR_FAIL;
271  }
272 
273  enum efinix_family_e family = EFINIX_UNKNOWN;
274  if (CMD_ARGC == 6) {
275  if (strcmp(CMD_ARGV[4], "-family") != 0)
277 
278  if (strcmp(CMD_ARGV[5], "trion") == 0) {
279  family = EFINIX_TRION;
280  } else if (strcmp(CMD_ARGV[5], "titanium") == 0) {
281  family = EFINIX_TITANIUM;
282  } else {
283  command_print(CMD, "unknown family");
284  return ERROR_FAIL;
285  }
286  }
287 
288  struct efinix_pld_device *efinix_info = malloc(sizeof(struct efinix_pld_device));
289  if (!efinix_info) {
290  LOG_ERROR("Out of memory");
291  return ERROR_FAIL;
292  }
293  efinix_info->tap = tap;
294  efinix_info->family = family;
295 
296  pld->driver_priv = efinix_info;
297 
298  return ERROR_OK;
299 }
300 
301 struct pld_driver efinix_pld = {
302  .name = "efinix",
303  .pld_create_command = &efinix_pld_create_command,
304  .load = &efinix_load,
305  .get_ipdbg_hub = efinix_get_ipdbg_hub,
306  .get_jtagspi_userircode = efinix_get_jtagspi_userircode,
307 };
uint32_t flip_u32(uint32_t value, unsigned int num)
Inverts the ordering of bits inside a 32-bit word (e.g.
Definition: binarybuffer.c:165
size_t unhexify(uint8_t *bin, const char *hex, size_t count)
Convert a string of hexadecimal pairs into its binary representation.
Definition: binarybuffer.c:342
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
#define ENTERUSER
Definition: efinix.c:18
#define USER1
Definition: efinix.c:19
#define USER4
Definition: efinix.c:22
struct pld_driver efinix_pld
Definition: efinix.c:301
efinix_family_e
Definition: efinix.c:24
@ EFINIX_TITANIUM
Definition: efinix.c:26
@ EFINIX_TRION
Definition: efinix.c:25
@ EFINIX_UNKNOWN
Definition: efinix.c:27
static int efinix_load(struct pld_device *pld_device, const char *filename)
Definition: efinix.c:141
#define USER3
Definition: efinix.c:21
static int efinix_set_instr(struct jtag_tap *tap, uint8_t new_instr)
Definition: efinix.c:124
#define USER2
Definition: efinix.c:20
PLD_CREATE_COMMAND_HANDLER(efinix_pld_create_command)
Definition: efinix.c:259
#define RUNTEST_START_CYCLES
Definition: efinix.c:31
static int efinix_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
Definition: efinix.c:253
static int efinix_get_ipdbg_hub(int user_num, struct pld_device *pld_device, struct pld_ipdbg_hub *hub)
Definition: efinix.c:208
#define PROGRAM
Definition: efinix.c:17
#define TRAILING_ZEROS
Definition: efinix.c:30
#define RUNTEST_FINISH_CYCLES
Definition: efinix.c:32
static int efinix_read_file(struct raw_bit_file *bit_file, const char *filename)
Definition: efinix.c:101
static int efinix_read_bit_file(struct raw_bit_file *bit_file, const char *filename)
Definition: efinix.c:44
uint8_t length
Definition: esp_usb_jtag.c:1
struct jtag_tap * jtag_tap_by_string(const char *s)
Definition: jtag/core.c:241
void jtag_add_runtest(unsigned int num_cycles, enum tap_state state)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:596
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1048
void jtag_add_tlr(void)
Run a TAP_RESET reset where the end state is TAP_RESET, regardless of the start state.
Definition: jtag/core.c:482
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, enum tap_state state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:455
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, enum tap_state state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:378
The JTAG interface can be implemented with a software or hardware fifo.
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IDLE
Definition: jtag.h:53
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define ERROR_OK
Definition: log.h:182
#define ERROR_PLD_FILE_LOAD_FAILED
Definition: pld.h:61
int cpld_read_raw_bit_file(struct raw_bit_file *bit_file, const char *filename)
Definition: raw_bit.c:19
int num_user
Definition: efinix.c:36
uint32_t idcode
Definition: efinix.c:35
enum efinix_family_e family
Definition: efinix.c:41
struct jtag_tap * tap
Definition: efinix.c:40
Definition: jtag.h:101
unsigned int ir_length
size of instruction register
Definition: jtag.h:110
Definition: pld.h:48
void * driver_priv
Definition: pld.h:50
Definition: pld.h:31
const char * name
Definition: pld.h:32
unsigned int user_ir_code
Definition: pld.h:20
struct jtag_tap * tap
Definition: pld.h:19
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
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
#define NULL
Definition: usb.h:16