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(buffer[0]) || !isxdigit(buffer[1])) {
86  fclose(input_file);
87  free(bit_file->data);
88  bit_file->data = NULL;
89  LOG_ERROR("unexpected char in hex string");
91  }
92  unhexify(&bit_file->data[idx], buffer, 2);
93  }
94 
95  fclose(input_file);
96 
97  return ERROR_OK;
98 }
99 
100 static int efinix_read_file(struct raw_bit_file *bit_file, const char *filename)
101 {
102  if (!filename || !bit_file)
104 
105  /* check if binary .bin or ascii .bit/.hex */
106  const char *file_ending_pos = strrchr(filename, '.');
107  if (!file_ending_pos) {
108  LOG_ERROR("Unable to detect filename suffix");
110  }
111 
112  if (strcasecmp(file_ending_pos, ".bin") == 0) {
113  return cpld_read_raw_bit_file(bit_file, filename);
114  } else if ((strcasecmp(file_ending_pos, ".bit") == 0) ||
115  (strcasecmp(file_ending_pos, ".hex") == 0)) {
116  return efinix_read_bit_file(bit_file, filename);
117  }
118 
119  LOG_ERROR("Unable to detect filetype");
121 }
122 
123 static int efinix_set_instr(struct jtag_tap *tap, uint8_t new_instr)
124 {
125  struct scan_field field;
126  field.num_bits = tap->ir_length;
127  void *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
128  if (!t) {
129  LOG_ERROR("Out of memory");
130  return ERROR_FAIL;
131  }
132  field.out_value = t;
133  buf_set_u32(t, 0, field.num_bits, new_instr);
134  field.in_value = NULL;
135  jtag_add_ir_scan(tap, &field, TAP_IDLE);
136  free(t);
137  return ERROR_OK;
138 }
139 
140 static int efinix_load(struct pld_device *pld_device, const char *filename)
141 {
142  struct raw_bit_file bit_file;
143  struct scan_field field[2];
144 
146  return ERROR_FAIL;
147 
148  struct efinix_pld_device *efinix_info = pld_device->driver_priv;
149  if (!efinix_info || !efinix_info->tap)
150  return ERROR_FAIL;
151  struct jtag_tap *tap = efinix_info->tap;
152 
153  jtag_add_tlr();
154 
155  int retval = efinix_set_instr(tap, PROGRAM);
156  if (retval != ERROR_OK)
157  return retval;
159  retval = efinix_set_instr(tap, PROGRAM); /* fix for T20 */
160  if (retval != ERROR_OK)
161  return retval;
162  retval = jtag_execute_queue();
163  if (retval != ERROR_OK)
164  return retval;
165 
166  retval = efinix_read_file(&bit_file, filename);
167  if (retval != ERROR_OK)
168  return retval;
169 
170  for (size_t i = 0; i < bit_file.length; i++)
171  bit_file.data[i] = flip_u32(bit_file.data[i], 8);
172 
173  /* shift in the bitstream */
174  field[0].num_bits = bit_file.length * 8;
175  field[0].out_value = bit_file.data;
176  field[0].in_value = NULL;
177 
178  /* followed by zeros */
179  field[1].num_bits = TRAILING_ZEROS;
180  uint8_t *buf = calloc(TRAILING_ZEROS / 8, 1);
181  if (!buf) {
182  free(bit_file.data);
183  LOG_ERROR("Out of memory");
184  return ERROR_FAIL;
185  }
186  field[1].out_value = buf;
187  field[1].in_value = NULL;
188 
189  jtag_add_dr_scan(tap, 2, field, TAP_DRPAUSE);
190  retval = jtag_execute_queue();
191  free(bit_file.data);
192  free(buf);
193  if (retval != ERROR_OK)
194  return retval;
195 
196  retval = efinix_set_instr(tap, ENTERUSER);
197  if (retval != ERROR_OK)
198  return retval;
199 
200  /* entering RUN/TEST for 100 cycles */
202  retval = jtag_execute_queue();
203 
204  return retval;
205 }
206 
207 static int efinix_get_ipdbg_hub(int user_num, struct pld_device *pld_device, struct pld_ipdbg_hub *hub)
208 {
209  if (!pld_device)
210  return ERROR_FAIL;
211 
212  struct efinix_pld_device *pld_device_info = pld_device->driver_priv;
213 
214  if (!pld_device_info || !pld_device_info->tap)
215  return ERROR_FAIL;
216 
217  hub->tap = pld_device_info->tap;
218 
219  if (pld_device_info->family == EFINIX_UNKNOWN) {
220  LOG_ERROR("family unknown, please specify for 'pld create'");
221  return ERROR_FAIL;
222  }
223  int num_user = 2; /* trion */
224  if (pld_device_info->family == EFINIX_TITANIUM)
225  num_user = 4;
226 
227  if (user_num > num_user) {
228  LOG_ERROR("Devices has only user register 1 to %d", num_user);
229  return ERROR_FAIL;
230  }
231 
232  switch (user_num) {
233  case 1:
234  hub->user_ir_code = USER1;
235  break;
236  case 2:
237  hub->user_ir_code = USER2;
238  break;
239  case 3:
240  hub->user_ir_code = USER3;
241  break;
242  case 4:
243  hub->user_ir_code = USER4;
244  break;
245  default:
246  LOG_ERROR("efinix devices only have user register 1 to %d", num_user);
247  return ERROR_FAIL;
248  }
249  return ERROR_OK;
250 }
251 
252 static int efinix_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
253 {
254  *ir = USER1;
255  return ERROR_OK;
256 }
257 
258 PLD_CREATE_COMMAND_HANDLER(efinix_pld_create_command)
259 {
260  if (CMD_ARGC != 4 && CMD_ARGC != 6)
262 
263  if (strcmp(CMD_ARGV[2], "-chain-position") != 0)
265 
266  struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[3]);
267  if (!tap) {
268  command_print(CMD, "Tap: %s does not exist", CMD_ARGV[3]);
269  return ERROR_FAIL;
270  }
271 
272  enum efinix_family_e family = EFINIX_UNKNOWN;
273  if (CMD_ARGC == 6) {
274  if (strcmp(CMD_ARGV[4], "-family") != 0)
276 
277  if (strcmp(CMD_ARGV[5], "trion") == 0) {
278  family = EFINIX_TRION;
279  } else if (strcmp(CMD_ARGV[5], "titanium") == 0) {
280  family = EFINIX_TITANIUM;
281  } else {
282  command_print(CMD, "unknown family");
283  return ERROR_FAIL;
284  }
285  }
286 
287  struct efinix_pld_device *efinix_info = malloc(sizeof(struct efinix_pld_device));
288  if (!efinix_info) {
289  LOG_ERROR("Out of memory");
290  return ERROR_FAIL;
291  }
292  efinix_info->tap = tap;
293  efinix_info->family = family;
294 
295  pld->driver_priv = efinix_info;
296 
297  return ERROR_OK;
298 }
299 
300 struct pld_driver efinix_pld = {
301  .name = "efinix",
302  .pld_create_command = &efinix_pld_create_command,
303  .load = &efinix_load,
304  .get_ipdbg_hub = efinix_get_ipdbg_hub,
305  .get_jtagspi_userircode = efinix_get_jtagspi_userircode,
306 };
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:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#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:300
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:140
#define USER3
Definition: efinix.c:21
static int efinix_set_instr(struct jtag_tap *tap, uint8_t new_instr)
Definition: efinix.c:123
#define USER2
Definition: efinix.c:20
PLD_CREATE_COMMAND_HANDLER(efinix_pld_create_command)
Definition: efinix.c:258
#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:252
static int efinix_get_ipdbg_hub(int user_num, struct pld_device *pld_device, struct pld_ipdbg_hub *hub)
Definition: efinix.c:207
#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:100
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:237
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_tlr(void)
Run a TAP_RESET reset where the end state is TAP_RESET, regardless of the start state.
Definition: jtag/core.c:478
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:374
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
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:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define ERROR_OK
Definition: log.h:164
#define ERROR_PLD_FILE_LOAD_FAILED
Definition: pld.h:62
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