OpenOCD
xvc.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /***************************************************************************
3  * Copyright (C) 2021 by Jose Borja Castillo, DTE-UMA *
4  * joscassan@uma.es *
5  * *
6  * This implementation is XVC protocol 1.0/1.1 compatible. However *
7  * 1.1 extension instructions are not supported. *
8  * Protocol specs at: https://github.com/Xilinx/XilinxVirtualCable} *
9  * Some parts of the code are inspired on both bitbang and jlink *
10  * by Øyvind Harboe and Paul Fertser, respectively. *
11  ***************************************************************************/
12 
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16 
17 #ifndef _WIN32
18 #include <netdb.h>
19 #include <netinet/tcp.h>
20 #include <sys/un.h>
21 #else
22 #include <winsock2.h>
23 #endif
24 #include "helper/bits.h"
25 #include "helper/replacements.h"
26 #include <jtag/interface.h>
27 #include <jtag/commands.h>
28 #include "helper/log.h"
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <time.h>
35 
36 static char *xvc_host;
37 static char *xvc_port;
38 static uint32_t xvc_tck;
39 
40 static int xvc_fd;
41 static uint8_t *xvc_tms_buf;
42 static uint8_t *xvc_tdi_buf;
43 static uint8_t *xvc_send_buf;
44 static uint8_t *xvc_tdo_buf;
45 static uint32_t xvc_used_bits;
46 
47 // XVC implementation specifics.
48 static unsigned int xvc_max_vector_size;
49 // max_vector_size discounting command header.
50 static unsigned int xvc_max_usable_vector_size;
51 
52 struct shift_result {
53  // First bit position in TDO to read.
54  unsigned int first;
55  // Number of bits to read.
56  unsigned int length;
57  // Destination address to store the result
58  void *buffer;
59  // Offset in the destination buffer.
60  unsigned int buffer_offset;
61 };
62 /* MAX_BUF_SIZE holds the maximum size that internal buffers are allowed to
63  be allocated. This parameter is limited by the size of an unsigned int
64  of 4 bytes. Unsigned int types are employed by several functions in OpenOCD,
65  like 'write_socket'. The offset of 11 bytes is set to ensure XVC commands
66  like 'shift' in deed fit inside the aforementioned buffers.
67  */
68 #define MAX_BUF_SIZE (BIT(31) - 11)
69 #define MAX_SHIFT_RESULTS 256
70 static unsigned int last_used_bits;
71 static unsigned int pending_shift_results;
73 
74 static int xvc_set_tck(void);
75 static int xvc_fill_buffer(void);
76 
77 static unsigned int xvc_bits_to_bytes(unsigned int bits)
78 {
79  return DIV_ROUND_UP(bits, 8);
80 }
81 
82 static int xvc_speed(int speed)
83 {
84  // Converts TCK speed in kHz to ns.
85  xvc_tck = 1000000 / speed;
86  // Changes default speed to adapter speed
87  return xvc_set_tck();
88 }
89 
90 static int xvc_speed_div(int speed, int *khz)
91 {
92  *khz = speed;
93 
94  return ERROR_OK;
95 }
96 
97 static int xvc_khz(int khz, int *jtag_speed)
98 {
99  *jtag_speed = khz;
100 
101  return ERROR_OK;
102 }
103 
104 static int read_frame(int sock_id, unsigned char *ptr, unsigned int size)
105 {
106  unsigned int i = size;
107  while (i > 0) {
108  int state = read_socket(sock_id, ptr, i);
109  if (state == 0) {
110  LOG_ERROR("No data available in socket at %s", __func__);
111  return ERROR_FAIL;
112  }
113  if (state < 0) {
114  LOG_ERROR("Reading error in %s", __func__);
115  return ERROR_FAIL;
116  }
117  ptr += state;
118  i -= state;
119  }
120  return ERROR_OK;
121 }
122 
123 static int xvc_flush(void)
124 {
125  if (!xvc_used_bits) {
126  // Nothing to send, so we don't expect any bit back either.
127  last_used_bits = 0;
128  LOG_DEBUG("XVC flush: no bits to flush");
129  return ERROR_OK;
130  }
131 
132  // Converts bits to bytes, to reckon how many bytes we should send.
133  unsigned int number_of_bytes = xvc_bits_to_bytes(xvc_used_bits);
134  // Creates the header.
135  const char *shift = "shift:";
136  size_t shift_len = strlen(shift);
137  size_t cp_offset = 0;
138  // Copies the header.
139  memcpy(xvc_send_buf + cp_offset, shift, shift_len);
140  // Updates the offset.
141  cp_offset += shift_len;
142  // Copies number of bytes.
143  h_u32_to_le(xvc_send_buf + cp_offset, xvc_used_bits);
144  cp_offset += sizeof(xvc_used_bits);
145  // Copies TMS vector.
146  memcpy(xvc_send_buf + cp_offset, xvc_tms_buf, number_of_bytes);
147  cp_offset += number_of_bytes;
148  // Copies TDI vector.
149  memcpy(xvc_send_buf + cp_offset, xvc_tdi_buf, number_of_bytes);
150  cp_offset += number_of_bytes;
151  // Updates the number of bytes used.
152  LOG_DEBUG("XVC flush: cp_offset: %zu", cp_offset);
153  LOG_DEBUG("XVC flush: used_bits: %d", xvc_used_bits);
154 
155  int written = write_socket(xvc_fd, xvc_send_buf, cp_offset);
156  if (written != (int)cp_offset) {
157  LOG_ERROR("Error writing socket in %s", __func__);
158  return ERROR_FAIL;
159  }
160 
161  memset(xvc_tms_buf, 0, xvc_max_usable_vector_size / 2);
162  memset(xvc_tdi_buf, 0, xvc_max_usable_vector_size / 2);
164  xvc_used_bits = 0;
165 
166  return ERROR_OK;
167 }
168 
169 static int xvc_queue(const uint8_t *tms, unsigned int tms_offset, const uint8_t *tdi,
170  unsigned int tdi_offset, uint8_t *tdo, unsigned int tdo_offset, unsigned int length)
171 {
172  do {
173  unsigned int available_length =
175  if (!available_length || pending_shift_results >= MAX_SHIFT_RESULTS) {
176  xvc_flush();
177  xvc_fill_buffer();
178  }
179 
180  struct shift_result *shift_result =
182  unsigned int scan_length =
183  length > available_length ? available_length : length;
184  if (tdi)
185  buf_set_buf(tdi, tdi_offset, xvc_tdi_buf, xvc_used_bits, scan_length);
186  if (tms)
187  buf_set_buf(tms, tms_offset, xvc_tms_buf, xvc_used_bits, scan_length);
188  if (tdo) {
189  shift_result->buffer = tdo;
190  shift_result->buffer_offset = tdo_offset;
192  shift_result->length = scan_length;
194  }
195  xvc_used_bits += scan_length;
196  tdi_offset += scan_length;
197  tms_offset += scan_length;
198  tdo_offset += scan_length;
199  length -= scan_length;
200  } while (length > 0);
201 
202  return ERROR_OK;
203 }
204 
205 static int xvc_getinfo(void)
206 {
207  const char *getinfo = "getinfo:";
208  size_t len = strlen(getinfo);
209  int written = write_socket(xvc_fd, getinfo, len);
210 
211  if (written != (int)len) {
212  LOG_ERROR("%s: write", __func__);
213  return ERROR_FAIL;
214  }
215  /*
216  Response is: xvcServer_vMajor.Minor: <max_vector_size>\n
217  Example: xvcServer_v1.0: 4096\n
218  Max vector size is in bits and represented in unsigned int (4 bytes).
219  Thus, maximum representable size is 2^32 -1 = 4294967295 bits. This occupies
220  10 characters.
221  */
222  char info_recv_buf[26] = {0};
223  // Byte read until '\n' or buffer full.
224  unsigned long idx = 0;
225  while (idx < sizeof(info_recv_buf) - 1) {
226  int ret = read_socket(xvc_fd, &info_recv_buf[idx], 1);
227  if (ret <= 0) {
228  LOG_ERROR("%s: read", __func__);
229  return ERROR_FAIL;
230  }
231  if (info_recv_buf[idx] == '\n')
232  break;
233  idx++;
234  }
235  info_recv_buf[idx] = '\0';
236  // Check xvcServer_v1.0: or xvcServer_v1.1: Although 1.1 extensions are not supported.
237  if (strncmp(info_recv_buf, "xvcServer_v1.0:", 15) != 0 &&
238  strncmp(info_recv_buf, "xvcServer_v1.1:", 15) != 0) {
239  LOG_ERROR("Unexpected response from XVC server_ %s", info_recv_buf);
240  return ERROR_FAIL;
241  }
242  LOG_INFO("XVC HW server version: %s", info_recv_buf);
243  xvc_max_usable_vector_size = strtoul(&info_recv_buf[15], NULL, 10);
245  LOG_DEBUG("Exceeded maximum vector size, outputting to %lu bytes",
246  MAX_BUF_SIZE);
248  }
250  LOG_DEBUG("Maximum vector size set to: %u", xvc_max_vector_size);
251  /* Usable size: maximum vector size determined by the server minus the
252  sizeof the command, 10 bytes in worst-case (6 bytes from shift: and 4
253  additional ones for bit_length).*/
254  // Updates TX Buffer sizes:
255  xvc_send_buf = malloc(xvc_max_vector_size * sizeof(uint8_t));
256  xvc_tms_buf = malloc(xvc_max_usable_vector_size / 2 * sizeof(uint8_t));
257  xvc_tdi_buf = malloc(xvc_max_usable_vector_size / 2 * sizeof(uint8_t));
258  xvc_tdo_buf = malloc(xvc_max_usable_vector_size / 2 * sizeof(uint8_t));
259  if (!xvc_send_buf || !xvc_tms_buf || !xvc_tdi_buf || !xvc_tdo_buf) {
260  LOG_ERROR("Out of memory");
261  free(xvc_send_buf);
262  free(xvc_tms_buf);
263  free(xvc_tdi_buf);
264  free(xvc_tdo_buf);
265  return ERROR_FAIL;
266  }
267  return ERROR_OK;
268 }
269 
270 static int xvc_set_tck(void)
271 {
272  /* Creates the command:
273  * copies the header and appends the value.
274  * */
275  uint8_t set_tck[12];
276  const char *header = "settck:";
277  memcpy(set_tck, header, strlen(header));
278  h_u32_to_le(set_tck + strlen(header), xvc_tck);
279  int written = write_socket(xvc_fd, set_tck, 11);
280  if (written != 11) {
281  LOG_ERROR("%s: write", __func__);
282  return ERROR_FAIL;
283  }
284  uint32_t tck_recv_buf;
285  int read = read_socket(xvc_fd, &tck_recv_buf, sizeof(tck_recv_buf));
286  if (read < 0) {
287  LOG_ERROR("%s: read", __func__);
288  return ERROR_FAIL;
289  }
290  // Prints response, regardless of machine endianness.
291  uint32_t xvc_tck_period_ns;
292  xvc_tck_period_ns = le_to_h_u32((uint8_t *)&tck_recv_buf);
293  LOG_INFO("XVC tck period ns: %u", xvc_tck_period_ns);
294  return ERROR_OK;
295 }
296 
297 static int xvc_fill_buffer(void)
298 {
300  LOG_ERROR("Read_frame");
301  return ERROR_FAIL;
302  }
303  for (unsigned int i = 0; i < pending_shift_results; i++) {
307  }
308  memset(xvc_tdo_buf, 0, xvc_max_usable_vector_size / 2);
310  return ERROR_OK;
311 }
312 
313 static int xvc_reset(int trst, int srst)
314 {
315  // XVC does not have dedicated Reset lines.
316  static bool first_time = true;
317  if (first_time) {
318  LOG_WARNING("Adapter has no reset lines. Fix \"reset_config\" command in "
319  "config file");
320  first_time = false;
321  }
322  return ERROR_OK;
323 }
324 
325 static int xvc_init_tcp(int *fd)
326 {
327  LOG_INFO("Connecting to %s:%s", xvc_host ? xvc_host : "localhost", xvc_port);
328 
329  const struct addrinfo hints = {
330  .ai_family = AF_UNSPEC,
331  .ai_socktype = SOCK_STREAM
332  };
333 
334  struct addrinfo *result;
335  // Obtain address(es) matching host/port.
336  int s = getaddrinfo(xvc_host, xvc_port, &hints, &result);
337  if (s != 0) {
338  LOG_ERROR("getaddrinfo: %s", gai_strerror(s));
339  return ERROR_FAIL;
340  }
341 
342  /* getaddrinfo() returns a list of address structures.
343  Try each address until we successfully connect(2).
344  If socket(2) (or connect(2)) fails, we (close the socket
345  and) try the next address. */
346  struct addrinfo *rp;
347  for (rp = result; rp; rp = rp->ai_next) {
348  *fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
349 #ifndef _WIN32
350  if (*fd == -1)
351  continue;
352 #else
353  if (*fd == (int)INVALID_SOCKET)
354  continue;
355 #endif
356  if (connect(*fd, rp->ai_addr, rp->ai_addrlen) != -1)
357  break;
358 
359  close_socket(*fd);
360  }
361 
362  freeaddrinfo(result);
363 
364  if (!rp) {
365  LOG_ERROR("Failed to connect");
366  return ERROR_FAIL;
367  }
368 
369  /* We work hard to collapse the writes into the minimum number, so when
370  * we write something we want to get it to the other end of the
371  * connection as fast as possible. */
372  int one = 1;
373  // On Windows optval has to be a const char *.
374  setsockopt(*fd, IPPROTO_TCP, TCP_NODELAY, (const char *)&one, sizeof(one));
375 
376  return ERROR_OK;
377 }
378 
379 static int xvc_init_unix(int *fd)
380 {
381  if (!xvc_host) {
382  LOG_ERROR("host/socket not specified");
383  return ERROR_FAIL;
384  }
385 
386  LOG_INFO("Connecting to unix socket %s", xvc_host);
387  *fd = socket(PF_UNIX, SOCK_STREAM, 0);
388  if (*fd < 0) {
389  LOG_ERROR("socket");
390  return ERROR_FAIL;
391  }
392 
393  struct sockaddr_un addr;
394  addr.sun_family = AF_UNIX;
395  strncpy(addr.sun_path, xvc_host, sizeof(addr.sun_path));
396  addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
397 
398  if (connect(*fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
399  LOG_ERROR("connect");
400  return ERROR_FAIL;
401  }
402 
403  return ERROR_OK;
404 }
405 
406 // COMMAND HANDLERS
407 COMMAND_HANDLER(xvc_handle_port_command)
408 {
409  if (CMD_ARGC != 1)
411  uint16_t port;
412  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], port);
413  free(xvc_port);
414  xvc_port = (port == 0) ? NULL : strdup(CMD_ARGV[0]);
415  return ERROR_OK;
416 }
417 
418 COMMAND_HANDLER(xvc_handle_host_command)
419 {
420  if (CMD_ARGC != 1)
422  free(xvc_host);
423  xvc_host = strdup(CMD_ARGV[0]);
424  return ERROR_OK;
425 }
426 
427 static const struct command_registration xvc_subcommand_handlers[] = {
428  {
429  .name = "port",
430  .handler = xvc_handle_port_command,
431  .mode = COMMAND_CONFIG,
432  .help =
433  "Set the port to use to connect to the XVC remote server.\n"
434  " If 0 or unset, use unix sockets to connect to the remote server.",
435  .usage = "port_number",
436  },
437  {
438  .name = "host",
439  .handler = xvc_handle_host_command,
440  .mode = COMMAND_CONFIG,
441  .help = "Set the host to use or UNIX socket to connect to the remote XVC server.",
442  .usage = "host_name",
443  },
445 };
446 
447 static const struct command_registration xvc_command_handlers[] = {
448  {
449  .name = "xvc",
450  .mode = COMMAND_ANY,
451  .help = "perform XVC driver configuration",
452  .chain = xvc_subcommand_handlers,
453  .usage = "",
454  },
456 };
457 
458 static int xvc_init(void)
459 {
460  xvc_used_bits = 0;
461  last_used_bits = 0;
463  // Default clock: 1000 ns period.
464  xvc_tck = 1000;
465 
466  LOG_DEBUG("Initializing XVC driver");
467  int ret;
468 
469  if (!xvc_port)
470  ret = xvc_init_unix(&xvc_fd);
471  else
472  ret = xvc_init_tcp(&xvc_fd);
473 
474  if (ret != ERROR_OK)
475  return ret;
476 
477  ret = xvc_getinfo();
478  if (ret != ERROR_OK)
479  return ret;
480 
481  ret = xvc_set_tck();
482  if (ret != ERROR_OK)
483  return ret;
484 
485  LOG_DEBUG("XVC driver initialized");
486 
487  return ERROR_OK;
488 }
489 
490 static int xvc_quit(void)
491 {
492  if (close_socket(xvc_fd) != 0) {
493  LOG_ERROR("close_socket");
494  return ERROR_FAIL;
495  }
496  free(xvc_port);
497  free(xvc_host);
498  free(xvc_tms_buf);
499  free(xvc_tdi_buf);
500  free(xvc_send_buf);
501  free(xvc_tdo_buf);
502  return ERROR_OK;
503 }
504 
505 // The driver leaves the TCK 0 when in idle.
507 {
508  assert(tap_is_state_stable(state));
510 }
511 
512 static int xvc_tap_state_move(int skip)
513 {
514  uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
516 
517  xvc_queue(&tms_scan, 0, NULL, 0, NULL, 0, tms_count);
518 
520  return ERROR_OK;
521 }
522 
523 /*
524  * Clock a bunch of TMS transitions, to change the JTAG
525  * state machine. "Legacy enqueue"
526  */
528 {
529  unsigned int num_bits = cmd->cmd.tms->num_bits;
530  const uint8_t *bits = cmd->cmd.tms->bits;
531 
532  LOG_DEBUG_IO("TMS: %d bits", num_bits);
533 
534  for (unsigned int i = 0; i < num_bits; i++) {
535  uint8_t tms = 0;
536  tms = ((bits[i / 8] >> (i % 8)) & 1);
537  if (xvc_queue(&tms, 0, NULL, 0, NULL, 0, 1) != ERROR_OK)
538  return ERROR_FAIL;
539  }
540 
541  return ERROR_OK;
542 }
543 
545 {
546  unsigned int num_states = cmd->num_states;
547  unsigned int state_count = 0;
548  uint8_t tms = 0xff;
549 
550  while (num_states) {
551  if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count]) {
552  xvc_queue(NULL, 0, NULL, 0, NULL, 0, 1);
553  } else if (tap_state_transition(tap_get_state(), true) ==
554  cmd->path[state_count]) {
555  xvc_queue(&tms, 0, NULL, 0, NULL, 0, 1);
556  } else {
557  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
559  tap_state_name(cmd->path[state_count]));
560  return ERROR_FAIL;
561  }
562 
563  tap_set_state(cmd->path[state_count]);
564  state_count++;
565  num_states--;
566  }
567 
569  return ERROR_OK;
570 }
571 
572 static unsigned int xvc_tap_stableclocks(unsigned int num_cycles)
573 {
574  uint8_t tms = (tap_get_state() == TAP_RESET ? 0xff : 0);
575 
576  for (unsigned int i = 0; i < num_cycles; i++)
577  xvc_queue(&tms, 0, NULL, 0, NULL, 0, 1);
578 
579  return ERROR_OK;
580 }
581 
582 static int xvc_tap_runtest(unsigned int num_cycles)
583 {
584  enum tap_state saved_end_state = tap_get_end_state();
585 
586  // only do a state_move when we're not already in IDLE.
587  if (tap_get_state() != TAP_IDLE) {
589  if (xvc_tap_state_move(0) != ERROR_OK)
590  return ERROR_FAIL;
591  }
592 
593  xvc_tap_stableclocks(num_cycles);
594 
595  // finish in end_state.
596  xvc_tap_end_state(saved_end_state);
597  if (tap_get_state() != tap_get_end_state())
598  if (xvc_tap_state_move(0) != ERROR_OK)
599  return ERROR_FAIL;
600 
601  return ERROR_OK;
602 }
603 
605 {
606  /* Make sure there are no trailing fields with num_bits == 0, or the logic
607  * below will fail. */
608  while (cmd->num_fields > 0 && cmd->fields[cmd->num_fields - 1].num_bits == 0) {
609  cmd->num_fields--;
610  LOG_DEBUG("discarding trailing empty field");
611  }
612  if (cmd->num_fields == 0) {
613  LOG_DEBUG("empty scan, doing nothing");
614  return ERROR_OK;
615  }
616 
617  bool ir_scan = cmd->ir_scan;
618  if (ir_scan) {
619  if (tap_get_state() != TAP_IRSHIFT) {
622  }
623  } else {
624  if (tap_get_state() != TAP_DRSHIFT) {
627  }
628  }
629  xvc_tap_end_state(cmd->end_state);
630 
631  for (unsigned int i = 0; i < cmd->num_fields; i++) {
632  // Last field
633  if (i == (cmd->num_fields - 1) && tap_get_state() != tap_get_end_state()) {
634  // All bits except the last one.
635  xvc_queue(NULL, 0, cmd->fields[i].out_value, 0, cmd->fields[i].in_value, 0,
636  cmd->fields[i].num_bits - 1);
637  // Last bit to copy.
638  uint8_t last_bit = 0;
639  if (cmd->fields[i].out_value)
640  bit_copy(&last_bit, 0, cmd->fields[i].out_value,
641  cmd->fields[i].num_bits - 1, 1);
642  // TMS set to 1 to leave the current state.
643  uint8_t tms_bits = 0x01;
644  xvc_queue(&tms_bits, 0, &last_bit, 0, cmd->fields[i].in_value,
645  cmd->fields[i].num_bits - 1, 1);
647  xvc_queue(&tms_bits, 1, NULL, 0, NULL, 0, 1);
649  } else {
650  xvc_queue(NULL, 0, cmd->fields[i].out_value, 0, cmd->fields[i].in_value, 0,
651  cmd->fields[i].num_bits);
652  }
653  }
654 
655  if (tap_get_state() != tap_get_end_state()) {
656  /* we *KNOW* the above loop transitioned out of
657  * the shift state, so we skip the first state
658  * and move directly to the end state.
659  */
660 
661  if (xvc_tap_state_move(0) != ERROR_OK)
662  return ERROR_FAIL;
663  }
664  return ERROR_OK;
665 }
666 
667 static int xvc_tap_execute_queue(struct jtag_command *cmd_queue)
668 {
669  struct jtag_command *cmd = cmd_queue;
670 
671  while (cmd) {
672  switch (cmd->type) {
673  case JTAG_RUNTEST:
674  LOG_DEBUG_IO("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles,
675  tap_state_name(cmd->cmd.runtest->end_state));
676  xvc_tap_end_state(cmd->cmd.runtest->end_state);
677  if (xvc_tap_runtest(cmd->cmd.runtest->num_cycles) != ERROR_OK)
678  return ERROR_FAIL;
679  break;
680  case JTAG_STABLECLOCKS:
681  /* this is only allowed while in a stable state. A check for a stable
682  * state was done in jtag_add_clocks()
683  */
684  if (xvc_tap_stableclocks(cmd->cmd.stableclocks->num_cycles) != ERROR_OK)
685  return ERROR_FAIL;
686  break;
687  case JTAG_TLR_RESET:
688  LOG_DEBUG_IO("statemove end in %s",
689  tap_state_name(cmd->cmd.statemove->end_state));
690  xvc_tap_end_state(cmd->cmd.statemove->end_state);
691  if (xvc_tap_state_move(0) != ERROR_OK)
692  return ERROR_FAIL;
693  break;
694  case JTAG_PATHMOVE:
695  LOG_DEBUG_IO("pathmove: %i states, end in %s",
696  cmd->cmd.pathmove->num_states,
697  tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
698  if (xvc_tap_path_move(cmd->cmd.pathmove) != ERROR_OK)
699  return ERROR_FAIL;
700  break;
701  case JTAG_SCAN:
702  if (xvc_tap_scan_write(cmd->cmd.scan) != ERROR_OK)
703  return ERROR_FAIL;
704  break;
705  case JTAG_SLEEP:
706  LOG_DEBUG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
707  // Flush the cmd FIFO before entering sleep to keep timing.
708  xvc_flush();
709  jtag_sleep(cmd->cmd.sleep->us);
710  break;
711  case JTAG_TMS:
713  return ERROR_FAIL;
714  break;
715  default:
716  LOG_ERROR("BUG: unknown JTAG command type encountered");
717  return ERROR_FAIL;
718  }
719  cmd = cmd->next;
720  }
721 
722  if (xvc_flush() != ERROR_OK)
723  return ERROR_FAIL;
724  if (xvc_fill_buffer() != ERROR_OK)
725  return ERROR_FAIL;
726 
727  return ERROR_OK;
728 }
729 
730 static struct jtag_interface xvc_interface = {
732  .supported = DEBUG_CAP_TMS_SEQ,
733 };
734 
736  .name = "xvc",
737  .transport_ids = TRANSPORT_JTAG,
738  .transport_preferred_id = TRANSPORT_JTAG,
739  .commands = xvc_command_handlers,
740  .init = xvc_init,
741  .quit = xvc_quit,
742  .reset = &xvc_reset,
743  .speed = &xvc_speed,
744  .khz = &xvc_khz,
745  .speed_div = &xvc_speed_div,
746  .jtag_ops = &xvc_interface,
747 };
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
Definition: binarybuffer.c:120
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
Definition: binarybuffer.h:218
#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
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_STABLECLOCKS
Definition: commands.h:142
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_TMS
Definition: commands.h:143
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t length
Definition: esp_usb_jtag.c:1
enum tap_state tap_get_end_state(void)
For more information,.
Definition: interface.c:56
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
Definition: interface.c:223
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
Definition: interface.c:48
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
Definition: interface.c:37
bool tap_is_state_stable(enum tap_state astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:188
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
Definition: interface.h:50
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1073
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
static int read_socket(int handle, void *buffer, unsigned int count)
Definition: replacements.h:175
static int close_socket(int sock)
Definition: replacements.h:184
static int write_socket(int handle, const void *buffer, unsigned int count)
Definition: replacements.h:166
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Represents a driver for a debugging interface.
Definition: interface.h:183
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:196
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
Definition: commands.h:35
unsigned int length
Definition: xvc.c:56
unsigned int buffer_offset
Definition: xvc.c:60
unsigned int first
Definition: xvc.c:54
void * buffer
Definition: xvc.c:58
#define TRANSPORT_JTAG
Definition: transport.h:19
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21
static int xvc_tap_path_move(struct pathmove_command *cmd)
Definition: xvc.c:544
static int xvc_init_tcp(int *fd)
Definition: xvc.c:325
static int xvc_reset(int trst, int srst)
Definition: xvc.c:313
static uint8_t * xvc_tms_buf
Definition: xvc.c:41
static char * xvc_port
Definition: xvc.c:37
static int xvc_quit(void)
Definition: xvc.c:490
static struct jtag_interface xvc_interface
Definition: xvc.c:730
static char * xvc_host
Definition: xvc.c:36
static unsigned int xvc_max_usable_vector_size
Definition: xvc.c:50
static int xvc_khz(int khz, int *jtag_speed)
Definition: xvc.c:97
static int xvc_tap_execute_queue(struct jtag_command *cmd_queue)
Definition: xvc.c:667
static int xvc_getinfo(void)
Definition: xvc.c:205
static int xvc_tap_execute_tms(struct jtag_command *cmd)
Definition: xvc.c:527
static int xvc_queue(const uint8_t *tms, unsigned int tms_offset, const uint8_t *tdi, unsigned int tdi_offset, uint8_t *tdo, unsigned int tdo_offset, unsigned int length)
Definition: xvc.c:169
static uint8_t * xvc_tdo_buf
Definition: xvc.c:44
static unsigned int xvc_max_vector_size
Definition: xvc.c:48
static int xvc_speed_div(int speed, int *khz)
Definition: xvc.c:90
static struct shift_result shift_result_buffer[MAX_SHIFT_RESULTS]
Definition: xvc.c:72
static const struct command_registration xvc_command_handlers[]
Definition: xvc.c:447
COMMAND_HANDLER(xvc_handle_port_command)
Definition: xvc.c:407
#define MAX_BUF_SIZE
Definition: xvc.c:68
static int xvc_init(void)
Definition: xvc.c:458
static uint8_t * xvc_send_buf
Definition: xvc.c:43
static int xvc_init_unix(int *fd)
Definition: xvc.c:379
static int xvc_speed(int speed)
Definition: xvc.c:82
static uint8_t * xvc_tdi_buf
Definition: xvc.c:42
static unsigned int xvc_tap_stableclocks(unsigned int num_cycles)
Definition: xvc.c:572
static int xvc_tap_runtest(unsigned int num_cycles)
Definition: xvc.c:582
static int read_frame(int sock_id, unsigned char *ptr, unsigned int size)
Definition: xvc.c:104
static void xvc_tap_end_state(enum tap_state state)
Definition: xvc.c:506
static int xvc_fd
Definition: xvc.c:40
static uint32_t xvc_tck
Definition: xvc.c:38
static int xvc_fill_buffer(void)
Definition: xvc.c:297
struct adapter_driver xvc_adapter_driver
Definition: xvc.c:735
static int xvc_tap_state_move(int skip)
Definition: xvc.c:512
#define MAX_SHIFT_RESULTS
Definition: xvc.c:69
static int xvc_tap_scan_write(struct scan_command *cmd)
Definition: xvc.c:604
static uint32_t xvc_used_bits
Definition: xvc.c:45
static int xvc_flush(void)
Definition: xvc.c:123
static unsigned int pending_shift_results
Definition: xvc.c:71
static unsigned int xvc_bits_to_bytes(unsigned int bits)
Definition: xvc.c:77
static unsigned int last_used_bits
Definition: xvc.c:70
static int xvc_set_tck(void)
Definition: xvc.c:270
static const struct command_registration xvc_subcommand_handlers[]
Definition: xvc.c:427