OpenOCD
gatemate.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 #include <jtag/adapter.h>
14 #include "pld.h"
15 #include "raw_bit.h"
16 
17 #define JTAG_CONFIGURE 0x06
18 #define JTAG_SPI_BYPASS 0x05
19 #define BYPASS 0x3F
20 
22  struct jtag_tap *tap;
23 };
24 
26  struct raw_bit_file raw_file;
27  size_t capacity;
28 };
29 
30 static int gatemate_add_byte_to_bitfile(struct gatemate_bit_file *bit_file, uint8_t byte)
31 {
32  const size_t chunk_size = 8192;
33  if (bit_file->raw_file.length + 1 > bit_file->capacity) {
34  uint8_t *buffer;
35  if (bit_file->raw_file.data)
36  buffer = realloc(bit_file->raw_file.data, bit_file->capacity + chunk_size);
37  else
38  buffer = malloc(chunk_size);
39  if (!buffer) {
40  LOG_ERROR("Out of memory");
41  return ERROR_FAIL;
42  }
43  bit_file->raw_file.data = buffer;
44  bit_file->capacity += chunk_size;
45  }
46 
47  bit_file->raw_file.data[bit_file->raw_file.length++] = byte;
48 
49  return ERROR_OK;
50 }
51 
52 static int gatemate_read_cfg_line(struct gatemate_bit_file *bit_file, const char *line_buffer, size_t nread)
53 {
54  for (size_t idx = 0; idx < nread; ++idx) {
55  if (line_buffer[idx] == ' ') {
56  continue;
57  } else if (line_buffer[idx] == 0) {
58  break;
59  } else if (idx + 1 < nread) {
60  if (isxdigit((unsigned char)line_buffer[idx]) &&
61  isxdigit((unsigned char)line_buffer[idx + 1])) {
62  uint8_t byte;
63  unhexify(&byte, line_buffer + idx, 2);
64  int retval = gatemate_add_byte_to_bitfile(bit_file, byte);
65  if (retval != ERROR_OK)
66  return retval;
67  } else if (line_buffer[idx] == '/' && line_buffer[idx + 1] == '/') {
68  break;
69  }
70  ++idx;
71  } else {
72  LOG_ERROR("parsing failed");
73  return ERROR_FAIL;
74  }
75  }
76  return ERROR_OK;
77 }
78 
79 static int gatemate_getline(char **buffer, size_t *buf_size, FILE *input_file)
80 {
81  const size_t chunk_size = 32;
82  if (!*buffer)
83  *buf_size = 0;
84 
85  size_t read = 0;
86  do {
87  if (read + 1 > *buf_size) {
88  char *new_buffer;
89  if (*buffer)
90  new_buffer = realloc(*buffer, *buf_size + chunk_size);
91  else
92  new_buffer = malloc(chunk_size);
93  if (!new_buffer) {
94  LOG_ERROR("Out of memory");
95  return -1;
96  }
97  *buffer = new_buffer;
98  *buf_size += chunk_size;
99  }
100 
101  int c = fgetc(input_file);
102  if ((c == EOF && read) || (char)c == '\n') {
103  (*buffer)[read++] = 0;
104  return read;
105  } else if (c == EOF) {
106  return -1;
107  }
108 
109  (*buffer)[read++] = (char)c;
110  } while (1);
111 
112  return -1;
113 }
114 
115 static int gatemate_read_cfg_file(struct gatemate_bit_file *bit_file, const char *filename)
116 {
117  FILE *input_file = fopen(filename, "r");
118 
119  if (!input_file) {
120  LOG_ERROR("Couldn't open %s: %s", filename, strerror(errno));
122  }
123 
124  int retval = ERROR_OK;
125  char *line_buffer = NULL;
126  size_t buffer_length = 0;
127  int nread;
128  while (((nread = gatemate_getline(&line_buffer, &buffer_length, input_file)) != -1) && (retval == ERROR_OK))
129  retval = gatemate_read_cfg_line(bit_file, line_buffer, (size_t)nread);
130 
131  if (line_buffer)
132  free(line_buffer);
133 
134  fclose(input_file);
135  if (retval != ERROR_OK)
136  free(bit_file->raw_file.data);
137  return retval;
138 }
139 
140 static int gatemate_read_file(struct gatemate_bit_file *bit_file, const char *filename)
141 {
142  if (!filename || !bit_file)
144 
145  memset(bit_file, 0, sizeof(struct gatemate_bit_file));
146 
147  /* check if binary .bit or ascii .cfg */
148  const char *file_suffix_pos = strrchr(filename, '.');
149  if (!file_suffix_pos) {
150  LOG_ERROR("Unable to detect filename suffix");
152  }
153 
154  if (strcasecmp(file_suffix_pos, ".bit") == 0)
155  return cpld_read_raw_bit_file(&bit_file->raw_file, filename);
156  else if (strcasecmp(file_suffix_pos, ".cfg") == 0)
157  return gatemate_read_cfg_file(bit_file, filename);
158 
159  LOG_ERROR("Filetype not supported, expecting .bit or .cfg file");
161 }
162 
163 static int gatemate_set_instr(struct jtag_tap *tap, uint8_t new_instr)
164 {
165  struct scan_field field;
166  field.num_bits = tap->ir_length;
167  void *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
168  if (!t) {
169  LOG_ERROR("Out of memory");
170  return ERROR_FAIL;
171  }
172  field.out_value = t;
173  buf_set_u32(t, 0, field.num_bits, new_instr);
174  field.in_value = NULL;
175  jtag_add_ir_scan(tap, &field, TAP_IDLE);
177  free(t);
178  return ERROR_OK;
179 }
180 
181 static int gatemate_load(struct pld_device *pld_device, const char *filename)
182 {
183  if (!pld_device)
184  return ERROR_FAIL;
185 
186  struct gatemate_pld_device *gatemate_info = pld_device->driver_priv;
187 
188  if (!gatemate_info || !gatemate_info->tap)
189  return ERROR_FAIL;
190  struct jtag_tap *tap = gatemate_info->tap;
191 
192  struct gatemate_bit_file bit_file;
193  int retval = gatemate_read_file(&bit_file, filename);
194  if (retval != ERROR_OK)
195  return retval;
196 
197  retval = gatemate_set_instr(tap, JTAG_CONFIGURE);
198  if (retval != ERROR_OK) {
199  free(bit_file.raw_file.data);
200  return retval;
201  }
202 
203  struct scan_field field;
204  field.num_bits = bit_file.raw_file.length * 8;
205  field.out_value = bit_file.raw_file.data;
206  field.in_value = NULL;
207  jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
208 
209  retval = jtag_execute_queue();
210  free(bit_file.raw_file.data);
211 
212  return retval;
213 }
214 
215 static int gatemate_has_jtagspi_instruction(struct pld_device *device, bool *has_instruction)
216 {
217  *has_instruction = true;
218  return ERROR_OK;
219 }
220 
222 {
223  if (!pld_device)
224  return ERROR_FAIL;
225 
226  struct gatemate_pld_device *pld_device_info = pld_device->driver_priv;
227  if (!pld_device_info)
228  return ERROR_FAIL;
229 
230  struct jtag_tap *tap = pld_device_info->tap;
231  if (!tap)
232  return ERROR_FAIL;
233 
234  if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) == JTAG_SPI_BYPASS)
235  return ERROR_OK;
236 
238 
239  return jtag_execute_queue();
240 }
241 
243 {
244  if (!pld_device)
245  return ERROR_FAIL;
246 
247  struct gatemate_pld_device *pld_device_info = pld_device->driver_priv;
248  if (!pld_device_info)
249  return ERROR_FAIL;
250 
251  struct jtag_tap *tap = pld_device_info->tap;
252  if (!tap)
253  return ERROR_FAIL;
254 
255  if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != JTAG_SPI_BYPASS)
256  return ERROR_OK;
257 
259 
260  return jtag_execute_queue();
261 }
262 
263 static int gatemate_get_stuff_bits(struct pld_device *pld_device, unsigned int *facing_read_bits,
264  unsigned int *trailing_write_bits)
265 {
266  if (!pld_device)
267  return ERROR_FAIL;
268 
269  *facing_read_bits = 1;
270  *trailing_write_bits = 1;
271 
272  return ERROR_OK;
273 }
274 
275 PLD_CREATE_COMMAND_HANDLER(gatemate_pld_create_command)
276 {
277  if (CMD_ARGC != 4)
279 
280  if (strcmp(CMD_ARGV[2], "-chain-position") != 0)
282 
283  struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[3]);
284  if (!tap) {
285  command_print(CMD, "Tap: %s does not exist", CMD_ARGV[3]);
286  return ERROR_FAIL;
287  }
288 
289  struct gatemate_pld_device *gatemate_info = malloc(sizeof(struct gatemate_pld_device));
290  if (!gatemate_info) {
291  LOG_ERROR("Out of memory");
292  return ERROR_FAIL;
293  }
294  gatemate_info->tap = tap;
295 
296  pld->driver_priv = gatemate_info;
297 
298  return ERROR_OK;
299 }
300 
301 struct pld_driver gatemate_pld = {
302  .name = "gatemate",
303  .pld_create_command = &gatemate_pld_create_command,
304  .load = &gatemate_load,
305  .has_jtagspi_instruction = gatemate_has_jtagspi_instruction,
306  .connect_spi_to_jtag = gatemate_connect_spi_to_jtag,
307  .disconnect_spi_from_jtag = gatemate_disconnect_spi_from_jtag,
308  .get_stuff_bits = gatemate_get_stuff_bits,
309 };
static const struct device_t * device
Definition: at91rm9200.c:94
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 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
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
static int gatemate_disconnect_spi_from_jtag(struct pld_device *pld_device)
Definition: gatemate.c:242
static int gatemate_add_byte_to_bitfile(struct gatemate_bit_file *bit_file, uint8_t byte)
Definition: gatemate.c:30
static int gatemate_get_stuff_bits(struct pld_device *pld_device, unsigned int *facing_read_bits, unsigned int *trailing_write_bits)
Definition: gatemate.c:263
static int gatemate_has_jtagspi_instruction(struct pld_device *device, bool *has_instruction)
Definition: gatemate.c:215
static int gatemate_getline(char **buffer, size_t *buf_size, FILE *input_file)
Definition: gatemate.c:79
static int gatemate_set_instr(struct jtag_tap *tap, uint8_t new_instr)
Definition: gatemate.c:163
#define JTAG_SPI_BYPASS
Definition: gatemate.c:18
static int gatemate_connect_spi_to_jtag(struct pld_device *pld_device)
Definition: gatemate.c:221
#define JTAG_CONFIGURE
Definition: gatemate.c:17
struct pld_driver gatemate_pld
Definition: gatemate.c:301
static int gatemate_read_cfg_line(struct gatemate_bit_file *bit_file, const char *line_buffer, size_t nread)
Definition: gatemate.c:52
static int gatemate_load(struct pld_device *pld_device, const char *filename)
Definition: gatemate.c:181
static int gatemate_read_file(struct gatemate_bit_file *bit_file, const char *filename)
Definition: gatemate.c:140
PLD_CREATE_COMMAND_HANDLER(gatemate_pld_create_command)
Definition: gatemate.c:275
#define BYPASS
Definition: gatemate.c:19
static int gatemate_read_cfg_file(struct gatemate_bit_file *bit_file, const char *filename)
Definition: gatemate.c:115
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_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_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
size_t capacity
Definition: gatemate.c:27
struct raw_bit_file raw_file
Definition: gatemate.c:26
struct jtag_tap * tap
Definition: gatemate.c:22
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
Definition: pld.h:48
void * driver_priv
Definition: pld.h:50
Definition: pld.h:31
const char * name
Definition: pld.h:32
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