OpenOCD
rlink.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2007,2008 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2008 Rob Brown, Lou Deluxe *
11  * rob@cobbleware.com, lou.openocd012@fixit.nospammail.net *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 /* project specific includes */
19 #include <jtag/interface.h>
20 #include <jtag/commands.h>
21 #include "helper/replacements.h"
22 #include "rlink.h"
23 #include "rlink_st7.h"
24 #include "rlink_ep1_cmd.h"
25 #include "rlink_dtc_cmd.h"
26 #include "libusb_helper.h"
27 
28 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on
29  *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
30 #undef AUTOMATIC_BUSY_LED
31 
32 /* This feature may require derating the speed due to reduced hold time. */
33 #undef USE_HARDWARE_SHIFTER_FOR_TMS
34 
35 #define INTERFACE_NAME "RLink"
36 
37 #define USB_IDVENDOR (0x138e)
38 #define USB_IDPRODUCT (0x9000)
39 
40 #define USB_EP1OUT_ADDR (0x01)
41 #define USB_EP1OUT_SIZE (16)
42 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
43 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
44 
45 #define USB_EP2OUT_ADDR (0x02)
46 #define USB_EP2OUT_SIZE (64)
47 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
48 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
49 #define USB_EP2BANK_SIZE (512)
50 
51 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
52 
53 #define ST7_PD_NBUSY_LED ST7_PD0
54 #define ST7_PD_NRUN_LED ST7_PD1
55 /* low enables VPP at adapter header, high connects it to GND instead */
56 #define ST7_PD_VPP_SEL ST7_PD6
57 /* low: VPP = 12v, high: VPP <= 5v */
58 #define ST7_PD_VPP_SHDN ST7_PD7
59 
60 /* These pins are connected together */
61 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
62 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
63 
64 /* Symbolic mapping between port pins and numbered IO lines */
65 #define ST7_PA_IO1 ST7_PA1
66 #define ST7_PA_IO2 ST7_PA2
67 #define ST7_PA_IO4 ST7_PA4
68 #define ST7_PA_IO8 ST7_PA6
69 #define ST7_PA_IO10 ST7_PA7
70 #define ST7_PB_IO5 ST7_PB5
71 #define ST7_PC_IO9 ST7_PC1
72 #define ST7_PC_IO3 ST7_PC2
73 #define ST7_PC_IO7 ST7_PC3
74 #define ST7_PE_IO6 ST7_PE5
75 
76 /* Symbolic mapping between numbered IO lines and adapter signals */
77 #define ST7_PA_RTCK ST7_PA_IO0
78 #define ST7_PA_NTRST ST7_PA_IO1
79 #define ST7_PC_TDI ST7_PC_IO3
80 #define ST7_PA_DBGRQ ST7_PA_IO4
81 #define ST7_PB_NSRST ST7_PB_IO5
82 #define ST7_PE_TMS ST7_PE_IO6
83 #define ST7_PC_TCK ST7_PC_IO7
84 #define ST7_PC_TDO ST7_PC_IO9
85 #define ST7_PA_DBGACK ST7_PA_IO10
86 
87 static struct libusb_device_handle *hdev;
88 
89 /*
90  * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
91  * This function takes care of zeroing the unused bytes before sending the packet.
92  * Any reply packet is not handled by this function.
93  */
94 static int ep1_generic_commandl(struct libusb_device_handle *hdev_param, size_t length, ...)
95 {
96  uint8_t usb_buffer[USB_EP1OUT_SIZE];
97  uint8_t *usb_buffer_p;
98  va_list ap;
99  int usb_ret;
100  int transferred;
101 
102  if (length > sizeof(usb_buffer))
103  length = sizeof(usb_buffer);
104 
105  usb_buffer_p = usb_buffer;
106 
107  va_start(ap, length);
108  while (length > 0) {
109  *usb_buffer_p++ = va_arg(ap, int);
110  length--;
111  }
112 
113  memset(
114  usb_buffer_p,
115  0,
116  sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
117  );
118 
119  usb_ret = jtag_libusb_bulk_write(
120  hdev_param,
122  (char *)usb_buffer, sizeof(usb_buffer),
124  &transferred
125  );
126 
127  if (usb_ret != ERROR_OK)
128  return usb_ret;
129  return transferred;
130 }
131 
132 #if 0
133 static ssize_t ep1_memory_read(
134  struct libusb_device_handle *hdev_param, uint16_t addr,
135  size_t length, uint8_t *buffer)
136 {
137  uint8_t usb_buffer[USB_EP1OUT_SIZE];
138  int usb_ret;
139  size_t remain;
140  ssize_t count;
141  int transferred;
142 
143  usb_buffer[0] = EP1_CMD_MEMORY_READ;
144  memset(
145  usb_buffer + 4,
146  0,
147  sizeof(usb_buffer) - 4
148  );
149 
150  remain = length;
151  count = 0;
152 
153  while (remain) {
154  if (remain > sizeof(usb_buffer))
155  length = sizeof(usb_buffer);
156  else
157  length = remain;
158 
159  usb_buffer[1] = addr >> 8;
160  usb_buffer[2] = addr;
161  usb_buffer[3] = length;
162 
163  usb_ret = jtag_libusb_bulk_write(
164  hdev_param, USB_EP1OUT_ADDR,
165  (char *)usb_buffer, sizeof(usb_buffer),
167  &transferred
168  );
169 
170  if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
171  break;
172 
173  usb_ret = jtag_libusb_bulk_read(
174  hdev_param, USB_EP1IN_ADDR,
175  (char *)buffer, length,
177  &transferred
178  );
179 
180  if (usb_ret != ERROR_OK || transferred < (int)length)
181  break;
182 
183  addr += length;
184  buffer += length;
185  count += length;
186  remain -= length;
187  }
188 
189  return count;
190 }
191 #endif
192 
193 static ssize_t ep1_memory_write(struct libusb_device_handle *hdev_param, uint16_t addr,
194  size_t length, uint8_t const *buffer)
195 {
196  uint8_t usb_buffer[USB_EP1OUT_SIZE];
197  int usb_ret;
198  size_t remain;
199  ssize_t count;
200 
201  usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
202 
203  remain = length;
204  count = 0;
205 
206  while (remain) {
207  if (remain > (sizeof(usb_buffer) - 4))
208  length = (sizeof(usb_buffer) - 4);
209  else
210  length = remain;
211 
212  usb_buffer[1] = addr >> 8;
213  usb_buffer[2] = addr;
214  usb_buffer[3] = length;
215  memcpy(
216  usb_buffer + 4,
217  buffer,
218  length
219  );
220  memset(
221  usb_buffer + 4 + length,
222  0,
223  sizeof(usb_buffer) - 4 - length
224  );
225 
226  int transferred;
227 
228  usb_ret = jtag_libusb_bulk_write(
229  hdev_param, USB_EP1OUT_ADDR,
230  (char *)usb_buffer, sizeof(usb_buffer),
232  &transferred
233  );
234 
235  if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
236  break;
237 
238  addr += length;
239  buffer += length;
240  count += length;
241  remain -= length;
242  }
243 
244  return count;
245 }
246 
247 
248 #if 0
249 static ssize_t ep1_memory_writel(struct libusb_device_handle *hdev_param, uint16_t addr,
250  size_t length, ...)
251 {
252  uint8_t buffer[USB_EP1OUT_SIZE - 4];
253  uint8_t *buffer_p;
254  va_list ap;
255  size_t remain;
256 
257  if (length > sizeof(buffer))
258  length = sizeof(buffer);
259 
260  remain = length;
261  buffer_p = buffer;
262 
263  va_start(ap, length);
264  while (remain > 0) {
265  *buffer_p++ = va_arg(ap, int);
266  remain--;
267  }
268 
269  return ep1_memory_write(hdev_param, addr, length, buffer);
270 }
271 #endif
272 
273 #define DTCLOAD_COMMENT (0)
274 #define DTCLOAD_ENTRY (1)
275 #define DTCLOAD_LOAD (2)
276 #define DTCLOAD_RUN (3)
277 #define DTCLOAD_LUT_START (4)
278 #define DTCLOAD_LUT (5)
279 
280 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
281 
282 /* This gets set by the DTC loader */
283 static uint8_t dtc_entry_download;
284 
285 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
286 static int dtc_load_from_buffer(struct libusb_device_handle *hdev_param, const uint8_t *buffer,
287  size_t length)
288 {
289  struct header {
290  uint8_t type;
291  uint8_t length;
292  };
293 
294  int usb_err;
295  struct header *header;
296  uint8_t lut_start = 0xc0;
297 
298  dtc_entry_download = 0;
299 
300  /* Stop the DTC before loading anything. */
301  usb_err = ep1_generic_commandl(
302  hdev_param, 1,
304  );
305  if (usb_err < 0)
306  return usb_err;
307 
308  while (length) {
309  if (length < sizeof(*header)) {
310  LOG_ERROR("Malformed DTC image");
311  exit(1);
312  }
313 
314  header = (struct header *)buffer;
315  buffer += sizeof(*header);
316  length -= sizeof(*header);
317 
318  if (length < (size_t)header->length + 1) {
319  LOG_ERROR("Malformed DTC image");
320  exit(1);
321  }
322 
323  switch (header->type) {
324  case DTCLOAD_COMMENT:
325  break;
326 
327  case DTCLOAD_ENTRY:
328  /* store entry addresses somewhere */
329  if (!strncmp("download", (char *)buffer + 1, 8))
331  break;
332 
333  case DTCLOAD_LOAD:
334  /* Send the DTC program to ST7 RAM. */
335  usb_err = ep1_memory_write(hdev_param, DTC_LOAD_BUFFER,
336  header->length + 1, buffer);
337  if (usb_err < 0)
338  return usb_err;
339 
340  /* Load it into the DTC. */
341  usb_err = ep1_generic_commandl(hdev_param, 3, EP1_CMD_DTC_LOAD,
343  if (usb_err < 0)
344  return usb_err;
345 
346  break;
347 
348  case DTCLOAD_RUN:
349  usb_err = ep1_generic_commandl(hdev_param, 3, EP1_CMD_DTC_CALL,
351  if (usb_err < 0)
352  return usb_err;
353 
354  break;
355 
356  case DTCLOAD_LUT_START:
357  lut_start = buffer[0];
358  break;
359 
360  case DTCLOAD_LUT:
361  usb_err = ep1_memory_write(hdev_param,
362  ST7_USB_BUF_EP0OUT + lut_start, header->length + 1, buffer);
363  if (usb_err < 0)
364  return usb_err;
365  break;
366 
367  default:
368  LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
369  exit(1);
370  break;
371  }
372 
373  buffer += (header->length + 1);
374  length -= (header->length + 1);
375  }
376 
377  return 0;
378 }
379 
380 /*
381  * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
382  */
383 static int dtc_start_download(void)
384 {
385  int usb_err;
386  uint8_t ep2txr;
387  int transferred;
388 
389  /* set up for download mode and make sure EP2 is set up to transmit */
390  usb_err = ep1_generic_commandl(
391  hdev, 7,
392 
396  EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
397  ST7_EP2TXR >> 8,
398  ST7_EP2TXR,
399  1
400  );
401  if (usb_err < 0)
402  return usb_err;
403 
404  /* read back ep2txr */
405  usb_err = jtag_libusb_bulk_read(
407  (char *)&ep2txr, 1,
409  &transferred
410  );
411  if (usb_err != ERROR_OK)
412  return usb_err;
413 
414  usb_err = ep1_generic_commandl(
415  hdev, 13,
416 
417  EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
420  1,
421  0x00,
422  EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
423  ST7_EP2TXR >> 8,
424  ST7_EP2TXR,
425  1,
427  EP1_CMD_DTC_CALL, /* start running the DTC */
430  );
431  if (usb_err < 0)
432  return usb_err;
433 
434  /* wait for completion */
435  return jtag_libusb_bulk_read(hdev, USB_EP1IN_ADDR, (char *)&ep2txr, 1,
436  LIBUSB_TIMEOUT_MS, &transferred);
437 }
438 
439 static int dtc_run_download(
440  struct libusb_device_handle *hdev_param,
441  uint8_t *command_buffer,
442  int command_buffer_size,
443  uint8_t *reply_buffer,
444  int reply_buffer_size
445  )
446 {
447  char dtc_status;
448  int usb_err;
449  int i;
450  int transferred;
451 
452  LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
453 
454  usb_err = jtag_libusb_bulk_write(
455  hdev_param,
457  (char *)command_buffer, USB_EP2BANK_SIZE,
459  &transferred
460  );
461  if (usb_err < 0)
462  return usb_err;
463 
464 
465  /* Wait for DTC to finish running command buffer */
466  for (i = 50;; ) {
467  usb_err = ep1_generic_commandl(
468  hdev_param, 4,
469 
473  1
474  );
475  if (usb_err < 0)
476  return usb_err;
477 
478  usb_err = jtag_libusb_bulk_read(
479  hdev_param,
481  &dtc_status, 1,
483  &transferred
484  );
485  if (usb_err < 0)
486  return usb_err;
487 
488  if (dtc_status & 0x01)
489  break;
490 
491  if (!--i) {
492  LOG_ERROR("too many retries waiting for DTC status");
493  return LIBUSB_ERROR_TIMEOUT;
494  }
495  }
496 
497 
498  if (reply_buffer && reply_buffer_size) {
499  usb_err = jtag_libusb_bulk_read(
500  hdev_param,
502  (char *)reply_buffer, reply_buffer_size,
504  &transferred
505  );
506 
507  if (usb_err != ERROR_OK || transferred < reply_buffer_size) {
508  LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
509  usb_err, reply_buffer_size
510  );
511  return usb_err;
512  }
513  }
514 
515  return usb_err;
516 }
517 
518 /*
519  * The dtc reply queue is a singly linked list that describes what to do
520  * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
521  * these entries.
522  */
523 
526  struct jtag_command *cmd; /* the command that resulted in this entry */
527 
528  struct {
529  uint8_t *buffer; /* the scan buffer */
530  int size; /* size of the scan buffer in bits */
531  int offset; /* how many bits were already done before this? */
532  int length; /* how many bits are processed in this operation? */
533  enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
534  } scan;
535 };
536 
537 
538 /*
539  * The dtc_queue consists of a buffer of pending commands and a reply queue.
540  * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
541  */
542 
543 static struct {
546  uint32_t cmd_index;
547  uint32_t reply_index;
550 
551 /*
552  * The tap state queue is for accumulating TAP state changes without needlessly
553  * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
554  * the dtc_queue.
555  */
556 
557 static struct {
558  uint32_t length;
559  uint32_t buffer;
561 
562 static int dtc_queue_init(void)
563 {
564  dtc_queue.rq_head = NULL;
565  dtc_queue.rq_tail = NULL;
566  dtc_queue.cmd_index = 0;
567  dtc_queue.reply_index = 0;
568  return 0;
569 }
570 
572  enum scan_type type, uint8_t *buffer, int size, int offset,
573  int length, struct jtag_command *cmd)
574 {
575  struct dtc_reply_queue_entry *rq_entry;
576 
577  rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
578  if (rq_entry) {
579  rq_entry->scan.type = type;
580  rq_entry->scan.buffer = buffer;
581  rq_entry->scan.size = size;
582  rq_entry->scan.offset = offset;
583  rq_entry->scan.length = length;
584  rq_entry->cmd = cmd;
585  rq_entry->next = NULL;
586 
587  if (!dtc_queue.rq_head)
588  dtc_queue.rq_head = rq_entry;
589  else
590  dtc_queue.rq_tail->next = rq_entry;
591 
592  dtc_queue.rq_tail = rq_entry;
593  }
594 
595  return rq_entry;
596 }
597 
598 /*
599  * Running the queue means that any pending command buffer is run
600  * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
601  * The queue is automatically run by append when it is necessary to get space for the append.
602  */
603 
604 static int dtc_queue_run(void)
605 {
606  struct dtc_reply_queue_entry *rq_p, *rq_next;
607  int retval;
608  int usb_err;
609  int bit_cnt;
610  int x;
611  uint8_t *dtc_p, *tdo_p;
612  uint8_t dtc_mask, tdo_mask;
613  uint8_t reply_buffer[USB_EP2IN_SIZE];
614 
615  assert((!!dtc_queue.rq_head) == (dtc_queue.reply_index > 0));
616  assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
617  assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
618 
619  retval = ERROR_OK;
620 
621  if (dtc_queue.cmd_index < 1)
622  return retval;
623 
624  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
625 
626  usb_err = dtc_run_download(hdev,
627  dtc_queue.cmd_buffer, dtc_queue.cmd_index,
628  reply_buffer, sizeof(reply_buffer)
629  );
630  if (usb_err < 0) {
631  LOG_ERROR("dtc_run_download: %s", libusb_error_name(usb_err));
632  exit(1);
633  }
634 
635  if (dtc_queue.rq_head) {
636  /* process the reply, which empties the reply queue and frees its entries */
637  dtc_p = reply_buffer;
638 
639  /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
640  *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
641  *was that or craft a function to do the reversal, and that wouldn't work with
642  *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
643  *scheme which would throw the byte alignment off. */
644 
645  for (
646  rq_p = dtc_queue.rq_head;
647  rq_p;
648  rq_p = rq_next
649  ) {
650  tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
651  tdo_mask = 1 << (rq_p->scan.offset % 8);
652 
653 
654  bit_cnt = rq_p->scan.length;
655  if (bit_cnt >= 8) {
656  /* bytes */
657 
658  dtc_mask = 1 << (8 - 1);
659 
660  for (
661  ;
662  bit_cnt;
663  bit_cnt--
664  ) {
665  if (*dtc_p & dtc_mask)
666  *tdo_p |= tdo_mask;
667  else
668  *tdo_p &= ~tdo_mask;
669 
670  dtc_mask >>= 1;
671  if (dtc_mask == 0) {
672  dtc_p++;
673  dtc_mask = 1 << (8 - 1);
674  }
675 
676  tdo_mask <<= 1;
677  if (tdo_mask == 0) {
678  tdo_p++;
679  tdo_mask = 1;
680  }
681  }
682  } else {
683  /* extra bits or last bit */
684 
685  x = *dtc_p++;
686  if ((rq_p->scan.type == SCAN_IN) && (
687  rq_p->scan.offset != rq_p->scan.size - 1
688  )) {
689  /* extra bits were sent as a full byte with padding on the
690  *end */
691  dtc_mask = 1 << (8 - 1);
692  } else
693  dtc_mask = 1 << (bit_cnt - 1);
694 
695  for (
696  ;
697  bit_cnt;
698  bit_cnt--
699  ) {
700  if (x & dtc_mask)
701  *tdo_p |= tdo_mask;
702  else
703  *tdo_p &= ~tdo_mask;
704 
705  dtc_mask >>= 1;
706 
707  tdo_mask <<= 1;
708  if (tdo_mask == 0) {
709  tdo_p++;
710  tdo_mask = 1;
711  }
712 
713  }
714  }
715 
716  if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
717  /* feed scan buffer back into openocd and free it */
718  if (jtag_read_buffer(rq_p->scan.buffer,
719  rq_p->cmd->cmd.scan) != ERROR_OK)
720  retval = ERROR_JTAG_QUEUE_FAILED;
721  free(rq_p->scan.buffer);
722  }
723 
724  rq_next = rq_p->next;
725  free(rq_p);
726  }
727  dtc_queue.rq_head = NULL;
728  dtc_queue.rq_tail = NULL;
729  }
730 
731  /* reset state for new appends */
732  dtc_queue.cmd_index = 0;
733  dtc_queue.reply_index = 0;
734 
735  return retval;
736 }
737 
738 /* runs the queue if it cannot take reserved_cmd bytes of command data
739  * or reserved_reply bytes of reply data */
740 static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
741 {
742  /* reserve one additional byte for the STOP cmd appended during run */
743  if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
744  return dtc_queue_run();
745 
746  if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
747  return dtc_queue_run();
748 
749  return ERROR_OK;
750 }
751 
752 static int tap_state_queue_init(void)
753 {
754  tap_state_queue.length = 0;
755  tap_state_queue.buffer = 0;
756  return 0;
757 }
758 
759 static int tap_state_queue_run(void)
760 {
761  int i;
762  int bits;
763  uint8_t byte_param;
764  int retval;
765 
766  retval = 0;
767  if (!tap_state_queue.length)
768  return retval;
769  bits = 1;
770  byte_param = 0;
771  for (i = tap_state_queue.length; i--; ) {
772 
773  byte_param <<= 1;
774  if (tap_state_queue.buffer & 1)
775  byte_param |= 1;
776  if ((bits >= 8) || !i) {
777  byte_param <<= (8 - bits);
778 
779  /* make sure there's room for two cmd bytes */
780  dtc_queue_run_if_full(2, 0);
781 
782 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
783  if (bits == 8) {
784  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
786  } else {
787 #endif
788  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
790 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
791  }
792 #endif
793 
794  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
795  byte_param;
796 
797  byte_param = 0;
798  bits = 1;
799  } else
800  bits++;
801 
802  tap_state_queue.buffer >>= 1;
803  }
804  return tap_state_queue_init();
805 }
806 
807 static int tap_state_queue_append(uint8_t tms)
808 {
809  int retval;
810 
811  if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
812  retval = tap_state_queue_run();
813  if (retval != 0)
814  return retval;
815  }
816 
817  if (tms)
818  tap_state_queue.buffer |= (1 << tap_state_queue.length);
819  tap_state_queue.length++;
820 
821  return 0;
822 }
823 
824 static void rlink_end_state(enum tap_state state)
825 {
828  else {
829  LOG_ERROR("BUG: %i is not a valid end state", state);
830  exit(-1);
831  }
832 }
833 
834 static void rlink_state_move(void)
835 {
836 
837  int i = 0, tms = 0;
838  uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
840 
841  for (i = 0; i < tms_count; i++) {
842  tms = (tms_scan >> i) & 1;
844  }
845 
847 }
848 
850 {
851  unsigned int num_states = cmd->num_states;
852  int state_count;
853  int tms = 0;
854 
855  state_count = 0;
856  while (num_states) {
857  if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
858  tms = 0;
859  else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
860  tms = 1;
861  else {
862  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
864  tap_state_name(cmd->path[state_count]));
865  exit(-1);
866  }
867 
869 
870  tap_set_state(cmd->path[state_count]);
871  state_count++;
872  num_states--;
873  }
874 
876 }
877 
878 static void rlink_runtest(unsigned int num_cycles)
879 {
880  enum tap_state saved_end_state = tap_get_end_state();
881 
882  /* only do a state_move when we're not already in RTI */
883  if (tap_get_state() != TAP_IDLE) {
886  }
887 
888  /* execute num_cycles */
889  for (unsigned int i = 0; i < num_cycles; i++)
891 
892  /* finish in end_state */
893  rlink_end_state(saved_end_state);
894  if (tap_get_state() != tap_get_end_state())
896 }
897 
898 /* (1) assert or (0) deassert reset lines */
899 static void rlink_reset(int trst, int srst)
900 {
901  uint8_t bitmap;
902  int usb_err;
903  int transferred;
904 
905  /* Read port A for bit op */
906  usb_err = ep1_generic_commandl(
907  hdev, 4,
909  ST7_PADR >> 8,
910  ST7_PADR,
911  1
912  );
913  if (usb_err < 0) {
914  LOG_ERROR("%s", libusb_error_name(usb_err));
915  exit(1);
916  }
917 
918  usb_err = jtag_libusb_bulk_read(
920  (char *)&bitmap, 1,
922  &transferred
923  );
924  if (usb_err != ERROR_OK || transferred < 1) {
925  LOG_ERROR("%s", libusb_error_name(usb_err));
926  exit(1);
927  }
928 
929  if (trst)
930  bitmap &= ~ST7_PA_NTRST;
931  else
932  bitmap |= ST7_PA_NTRST;
933 
934  /* Write port A and read port B for bit op
935  * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
936  *and assert NSRST by setting DDR to 1. */
937  usb_err = ep1_generic_commandl(
938  hdev, 9,
940  ST7_PADR >> 8,
941  ST7_PADR,
942  1,
943  bitmap,
945  ST7_PBDDR >> 8,
946  ST7_PBDDR,
947  1
948  );
949  if (usb_err < 0) {
950  LOG_ERROR("%s", libusb_error_name(usb_err));
951  exit(1);
952  }
953 
954  usb_err = jtag_libusb_bulk_read(
956  (char *)&bitmap, 1,
958  &transferred
959  );
960  if (usb_err != ERROR_OK || transferred < 1) {
961  LOG_ERROR("%s", libusb_error_name(usb_err));
962  exit(1);
963  }
964 
965  if (srst)
966  bitmap |= ST7_PB_NSRST;
967  else
968  bitmap &= ~ST7_PB_NSRST;
969 
970  /* write port B and read dummy to ensure completion before returning */
971  usb_err = ep1_generic_commandl(
972  hdev, 6,
974  ST7_PBDDR >> 8,
975  ST7_PBDDR,
976  1,
977  bitmap,
979  );
980  if (usb_err < 0) {
981  LOG_ERROR("%s", libusb_error_name(usb_err));
982  exit(1);
983  }
984 
985  usb_err = jtag_libusb_bulk_read(
987  (char *)&bitmap, 1,
989  &transferred
990  );
991  if (usb_err != ERROR_OK || transferred < 1) {
992  LOG_ERROR("%s", libusb_error_name(usb_err));
993  exit(1);
994  }
995 }
996 
997 static int rlink_scan(struct jtag_command *cmd, enum scan_type type,
998  uint8_t *buffer, int scan_size)
999 {
1000  bool ir_scan;
1001  enum tap_state saved_end_state;
1002  int byte_bits;
1003  int extra_bits;
1004  int chunk_bits;
1005  int chunk_bytes;
1006  int x;
1007 
1008  int tdi_bit_offset;
1009  uint8_t tdi_mask, *tdi_p;
1010  uint8_t dtc_mask;
1011 
1012  if (scan_size < 1) {
1013  LOG_ERROR("scan_size cannot be less than 1 bit");
1014  exit(1);
1015  }
1016 
1017  ir_scan = cmd->cmd.scan->ir_scan;
1018 
1019  /* Move to the proper state before starting to shift TDI/TDO. */
1020  if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) ||
1021  (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
1022  saved_end_state = tap_get_end_state();
1024  rlink_state_move();
1025  rlink_end_state(saved_end_state);
1026  }
1027 
1029 
1030 
1031 #if 0
1032  printf("scan_size = %d, type = 0x%x\n", scan_size, type);
1033  {
1034  int i;
1035 
1036  /* clear unused bits in scan buffer for ease of debugging
1037  * (it makes diffing output easier) */
1038  buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1039 
1040  printf("before scan:");
1041  for (i = 0; i < (scan_size + 7) / 8; i++)
1042  printf(" %02x", buffer[i]);
1043  printf("\n");
1044  }
1045 #endif
1046 
1047  /* The number of bits that can be shifted as complete bytes */
1048  byte_bits = (int)(scan_size - 1) / 8 * 8;
1049  /* The number of bits left over, not counting the last bit */
1050  extra_bits = (scan_size - 1) - byte_bits;
1051 
1052  tdi_bit_offset = 0;
1053  tdi_p = buffer;
1054  tdi_mask = 1;
1055 
1056  if (extra_bits && (type == SCAN_OUT)) {
1057  /* Schedule any extra bits into the DTC command buffer, padding as needed
1058  * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1059  *fall off the end */
1060 
1061  /* make sure there's room for two cmd bytes */
1062  dtc_queue_run_if_full(2, 0);
1063 
1064  x = 0;
1065  dtc_mask = 1 << (extra_bits - 1);
1066 
1067  while (extra_bits--) {
1068  if (*tdi_p & tdi_mask)
1069  x |= dtc_mask;
1070 
1071  dtc_mask >>= 1;
1072 
1073  tdi_mask <<= 1;
1074  if (tdi_mask == 0) {
1075  tdi_p++;
1076  tdi_mask = 1;
1077  }
1078  }
1079 
1080  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1082 
1083  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1084  }
1085 
1086  /* Loop scheduling full bytes into the DTC command buffer */
1087  while (byte_bits) {
1088  /* make sure there's room for one (for in scans) or two cmd bytes and
1089  * at least one reply byte for in or inout scans*/
1090  dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
1091 
1092  chunk_bits = byte_bits;
1093  /* we can only use up to 16 bytes at a time */
1094  if (chunk_bits > (16 * 8))
1095  chunk_bits = (16 * 8);
1096 
1097  if (type != SCAN_IN) {
1098  /* how much is there room for, considering stop and byte op? */
1099  x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1100  if (chunk_bits > x)
1101  chunk_bits = x;
1102  }
1103 
1104  if (type != SCAN_OUT) {
1105  /* how much is there room for in the reply buffer? */
1106  x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1107  if (chunk_bits > x)
1108  chunk_bits = x;
1109  }
1110 
1111  /* so the loop will end */
1112  byte_bits -= chunk_bits;
1113 
1114  if (type != SCAN_OUT) {
1115  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1116  chunk_bits, cmd)) {
1117  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1118  exit(1);
1119  }
1120  dtc_queue.reply_index += (chunk_bits + 7) / 8;
1121 
1122  tdi_bit_offset += chunk_bits;
1123  }
1124 
1125  /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1126  chunk_bytes = chunk_bits / 8;
1127 
1128  switch (type) {
1129  case SCAN_IN:
1130  x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1131  break;
1132  case SCAN_OUT:
1133  x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1134  break;
1135  default:
1136  x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1137  break;
1138  }
1139  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1140 
1141  if (type != SCAN_IN) {
1142  x = 0;
1143  dtc_mask = 1 << (8 - 1);
1144 
1145  while (chunk_bits--) {
1146  if (*tdi_p & tdi_mask)
1147  x |= dtc_mask;
1148 
1149  dtc_mask >>= 1;
1150  if (dtc_mask == 0) {
1151  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1152  x = 0;
1153  dtc_mask = 1 << (8 - 1);
1154  }
1155 
1156  tdi_mask <<= 1;
1157  if (tdi_mask == 0) {
1158  tdi_p++;
1159  tdi_mask = 1;
1160  }
1161  }
1162  }
1163  }
1164 
1165  if (extra_bits && (type != SCAN_OUT)) {
1166  /* Schedule any extra bits into the DTC command buffer */
1167 
1168  /* make sure there's room for one (for in scans) or two cmd bytes
1169  * and one reply byte */
1170  dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
1171 
1172  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1173  extra_bits, cmd)) {
1174  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1175  exit(1);
1176  }
1177 
1178  dtc_queue.reply_index++;
1179 
1180  tdi_bit_offset += extra_bits;
1181 
1182  if (type == SCAN_IN) {
1183  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1185 
1186  } else {
1187  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1188  DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1189 
1190  x = 0;
1191  dtc_mask = 1 << (8 - 1);
1192 
1193  while (extra_bits--) {
1194  if (*tdi_p & tdi_mask)
1195  x |= dtc_mask;
1196 
1197  dtc_mask >>= 1;
1198 
1199  tdi_mask <<= 1;
1200  if (tdi_mask == 0) {
1201  tdi_p++;
1202  tdi_mask = 1;
1203  }
1204  }
1205 
1206  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1207  }
1208  }
1209 
1210  /* Schedule the last bit into the DTC command buffer */
1211 
1212  /* make sure there's room for one cmd byte and one reply byte
1213  * for in or inout scans*/
1214  dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
1215 
1216  if (type == SCAN_OUT) {
1217  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1218  DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1219 
1220  } else {
1221  if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1222  1, cmd)) {
1223  LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1224  exit(1);
1225  }
1226 
1227  dtc_queue.reply_index++;
1228 
1229  dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1230  DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1231  }
1232 
1233  /* Move to pause state */
1235  tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1236  if (tap_get_state() != tap_get_end_state())
1237  rlink_state_move();
1238 
1239  return 0;
1240 }
1241 
1242 static int rlink_execute_queue(struct jtag_command *cmd_queue)
1243 {
1244  struct jtag_command *cmd = cmd_queue; /* currently processed command */
1245  int scan_size;
1246  enum scan_type type;
1247  uint8_t *buffer;
1248  int retval, tmp_retval;
1249 
1250  /* return ERROR_OK, unless something goes wrong */
1251  retval = ERROR_OK;
1252 
1253 #ifndef AUTOMATIC_BUSY_LED
1254  /* turn LED on */
1257  ~(ST7_PD_NBUSY_LED)
1258  );
1259 #endif
1260 
1261  while (cmd) {
1262  switch (cmd->type) {
1263  case JTAG_RUNTEST:
1264  case JTAG_TLR_RESET:
1265  case JTAG_PATHMOVE:
1266  case JTAG_SCAN:
1267  break;
1268 
1269  default:
1270  /* some events, such as resets, need a queue flush to ensure
1271  *consistency */
1273  dtc_queue_run();
1274  break;
1275  }
1276 
1277  switch (cmd->type) {
1278  case JTAG_RESET:
1279  LOG_DEBUG_IO("reset trst: %i srst %i",
1280  cmd->cmd.reset->trst,
1281  cmd->cmd.reset->srst);
1282  if (cmd->cmd.reset->trst == 1 ||
1283  (cmd->cmd.reset->srst &&
1286  rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1287  break;
1288  case JTAG_RUNTEST:
1289  LOG_DEBUG_IO("runtest %i cycles, end in %i",
1290  cmd->cmd.runtest->num_cycles,
1291  cmd->cmd.runtest->end_state);
1292  if (cmd->cmd.runtest->end_state != -1)
1293  rlink_end_state(cmd->cmd.runtest->end_state);
1294  rlink_runtest(cmd->cmd.runtest->num_cycles);
1295  break;
1296  case JTAG_TLR_RESET:
1297  LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1298  if (cmd->cmd.statemove->end_state != -1)
1299  rlink_end_state(cmd->cmd.statemove->end_state);
1300  rlink_state_move();
1301  break;
1302  case JTAG_PATHMOVE:
1303  LOG_DEBUG_IO("pathmove: %u states, end in %i",
1304  cmd->cmd.pathmove->num_states,
1305  cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1306  rlink_path_move(cmd->cmd.pathmove);
1307  break;
1308  case JTAG_SCAN:
1309  LOG_DEBUG_IO("%s scan end in %i",
1310  (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
1311  cmd->cmd.scan->end_state);
1312  if (cmd->cmd.scan->end_state != -1)
1313  rlink_end_state(cmd->cmd.scan->end_state);
1314  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1315  type = jtag_scan_type(cmd->cmd.scan);
1316  if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK)
1317  retval = ERROR_FAIL;
1318  break;
1319  case JTAG_SLEEP:
1320  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
1321  jtag_sleep(cmd->cmd.sleep->us);
1322  break;
1323  default:
1324  LOG_ERROR("BUG: unknown JTAG command type encountered");
1325  exit(-1);
1326  }
1327  cmd = cmd->next;
1328  }
1329 
1330  /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1331  *function */
1333  tmp_retval = dtc_queue_run();
1334  if (tmp_retval != ERROR_OK)
1335  retval = tmp_retval;
1336 
1337 #ifndef AUTOMATIC_BUSY_LED
1338  /* turn LED off */
1341  ~0
1342  );
1343 #endif
1344 
1345  return retval;
1346 }
1347 
1348 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1349  *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1350 
1351 static int rlink_speed(int speed)
1352 {
1353  int i;
1354 
1355  if (speed == 0) {
1356  /* fastest speed */
1358  }
1359 
1360  for (i = rlink_speed_table_size; i--; ) {
1361  if (rlink_speed_table[i].prescaler == speed) {
1363  rlink_speed_table[i].dtc_size) != 0) {
1364  LOG_ERROR(
1365  "An error occurred while trying to load DTC code for speed \"%d\".",
1366  speed);
1367  exit(1);
1368  }
1369 
1370  int ret = dtc_start_download();
1371  if (ret < 0) {
1372  LOG_ERROR("starting DTC: %s", libusb_error_name(ret));
1373  exit(1);
1374  }
1375 
1376  return ERROR_OK;
1377  }
1378  }
1379 
1380  LOG_ERROR("%d is not a supported speed", speed);
1381  return ERROR_FAIL;
1382 }
1383 
1384 static int rlink_speed_div(int speed, int *khz)
1385 {
1386  int i;
1387 
1388  for (i = rlink_speed_table_size; i--; ) {
1389  if (rlink_speed_table[i].prescaler == speed) {
1390  *khz = rlink_speed_table[i].khz;
1391  return ERROR_OK;
1392  }
1393  }
1394 
1395  LOG_ERROR("%d is not a supported speed", speed);
1396  return ERROR_FAIL;
1397 }
1398 
1399 static int rlink_khz(int khz, int *speed)
1400 {
1401  int i;
1402 
1403  if (khz == 0) {
1404  LOG_ERROR("RCLK not supported");
1405  return ERROR_FAIL;
1406  }
1407 
1408  for (i = rlink_speed_table_size; i--; ) {
1409  if (rlink_speed_table[i].khz <= khz) {
1410  *speed = rlink_speed_table[i].prescaler;
1411  return ERROR_OK;
1412  }
1413  }
1414 
1415  LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1416  *speed = rlink_speed_table[0].prescaler;
1417  return ERROR_OK;
1418 }
1419 
1420 static int rlink_init(void)
1421 {
1422  int i, j, retries;
1423  uint8_t reply_buffer[USB_EP1IN_SIZE];
1424  int transferred;
1425 
1426  const uint16_t vids[] = { USB_IDVENDOR, 0 };
1427  const uint16_t pids[] = { USB_IDPRODUCT, 0 };
1428  if (jtag_libusb_open(vids, pids, NULL, &hdev, NULL) != ERROR_OK)
1429  return ERROR_FAIL;
1430 
1431  struct libusb_device_descriptor descriptor;
1432  struct libusb_device *usb_dev = libusb_get_device(hdev);
1433  int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1434  if (r < 0) {
1435  LOG_ERROR("error %d getting device descriptor", r);
1436  return ERROR_FAIL;
1437  }
1438 
1439  if (descriptor.bNumConfigurations > 1) {
1440  LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1441  return ERROR_FAIL;
1442  }
1443  struct libusb_config_descriptor *config;
1444  libusb_get_config_descriptor(usb_dev, 0, &config);
1445  if (config->bNumInterfaces > 1) {
1446  LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1447  return ERROR_FAIL;
1448  }
1449 
1450  LOG_DEBUG("Opened device, hdev = %p", hdev);
1451 
1452  /* usb_set_configuration required under win32 */
1453  libusb_set_configuration(hdev, config->bConfigurationValue);
1454 
1455  retries = 3;
1456  do {
1457  i = libusb_claim_interface(hdev, 0);
1458  if (i != LIBUSB_SUCCESS) {
1459  LOG_ERROR("usb_claim_interface: %s", libusb_error_name(i));
1460  j = libusb_detach_kernel_driver(hdev, 0);
1461  if (j != LIBUSB_SUCCESS)
1462  LOG_ERROR("detach kernel driver: %s", libusb_error_name(j));
1463  } else {
1464  LOG_DEBUG("interface claimed!");
1465  break;
1466  }
1467  } while (--retries);
1468 
1469  if (i != LIBUSB_SUCCESS) {
1470  LOG_ERROR("Initialisation failed.");
1471  return ERROR_FAIL;
1472  }
1473  if (libusb_set_interface_alt_setting(hdev, 0, 0) != LIBUSB_SUCCESS) {
1474  LOG_ERROR("Failed to set interface.");
1475  return ERROR_FAIL;
1476  }
1477 
1478  /* The device starts out in an unknown state on open. As such,
1479  * result reads time out, and it's not even known whether the
1480  * command was accepted. So, for this first command, we issue
1481  * it repeatedly until its response doesn't time out. Also, if
1482  * sending a command is going to time out, we find that out here.
1483  *
1484  * It must be possible to open the device in such a way that
1485  * this special magic isn't needed, but, so far, it escapes us.
1486  */
1487  for (i = 0; i < 5; i++) {
1489  hdev, 1,
1491  );
1492  if (j < USB_EP1OUT_SIZE) {
1493  LOG_ERROR("USB write error: %s", libusb_error_name(j));
1494  return ERROR_FAIL;
1495  }
1498  (char *)reply_buffer, sizeof(reply_buffer),
1499  200,
1500  &transferred
1501  );
1502  if (j != LIBUSB_ERROR_TIMEOUT)
1503  break;
1504  }
1505 
1506  if (j != ERROR_OK || transferred != (int)sizeof(reply_buffer)) {
1507  LOG_ERROR("USB read error: %s", libusb_error_name(j));
1508  return ERROR_FAIL;
1509  }
1510  LOG_DEBUG(INTERFACE_NAME " firmware version: %d.%d.%d",
1511  reply_buffer[0],
1512  reply_buffer[1],
1513  reply_buffer[2]);
1514 
1515  if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1516  LOG_WARNING(
1517  "The rlink device is not of the version that the developers have played with. It may or may not work.");
1518 
1519  /* Probe port E for adapter presence */
1521  hdev, 16,
1522  EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1523  ST7_PEDR >> 8,
1524  ST7_PEDR,
1525  3,
1526  0x00, /* DR */
1527  ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1528  ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1529  EP1_CMD_MEMORY_READ, /* Read back */
1530  ST7_PEDR >> 8,
1531  ST7_PEDR,
1532  1,
1533  EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1534  ST7_PEDR >> 8,
1535  ST7_PEDR,
1536  1,
1538  );
1539 
1542  (char *)reply_buffer, 1,
1544  &transferred
1545  );
1546 
1547  if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0)
1548  LOG_WARNING("target detection problem");
1549 
1551  hdev, 11,
1552  EP1_CMD_MEMORY_READ, /* Read back */
1553  ST7_PEDR >> 8,
1554  ST7_PEDR,
1555  1,
1556  EP1_CMD_MEMORY_WRITE, /* float port E */
1557  ST7_PEDR >> 8,
1558  ST7_PEDR,
1559  3,
1560  0x00, /* DR */
1561  0x00, /* DDR */
1562  0x00 /* OR */
1563  );
1564 
1567  (char *)reply_buffer, 1,
1569  &transferred
1570  );
1571 
1572 
1573  if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0)
1574  LOG_WARNING("target not plugged in");
1575 
1576  /* float ports A and B */
1578  hdev, 11,
1580  ST7_PADDR >> 8,
1581  ST7_PADDR,
1582  2,
1583  0x00,
1584  0x00,
1586  ST7_PBDDR >> 8,
1587  ST7_PBDDR,
1588  1,
1589  0x00
1590  );
1591 
1592  /* make sure DTC is stopped, set VPP control, set up ports A and B */
1594  hdev, 14,
1597  ~(ST7_PD_VPP_SHDN),
1599  ST7_PADR >> 8,
1600  ST7_PADR,
1601  2,
1602  ((~(0)) & (ST7_PA_NTRST)),
1603  (ST7_PA_NTRST),
1604  /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1605  *here and later assert NSRST by setting DDR bit to 1. */
1607  ST7_PBDR >> 8,
1608  ST7_PBDR,
1609  1,
1610  0x00
1611  );
1612 
1613  /* set LED updating mode and make sure they're unlit */
1615  hdev, 3,
1616 #ifdef AUTOMATIC_BUSY_LED
1618 #else
1620 #endif
1622  ~0
1623  );
1624 
1626  dtc_queue_init();
1627  rlink_reset(0, 0);
1628 
1629  return ERROR_OK;
1630 }
1631 
1632 static int rlink_quit(void)
1633 {
1634  /* stop DTC and make sure LEDs are off */
1636  hdev, 6,
1640  ~0,
1642  ~0
1643  );
1644 
1645  libusb_release_interface(hdev, 0);
1646  libusb_close(hdev);
1647 
1648  return ERROR_OK;
1649 }
1650 
1651 static struct jtag_interface rlink_interface = {
1653 };
1654 
1656  .name = "rlink",
1657  .transport_ids = TRANSPORT_JTAG,
1658  .transport_preferred_id = TRANSPORT_JTAG,
1659 
1660  .init = rlink_init,
1661  .quit = rlink_quit,
1662  .speed = rlink_speed,
1663  .khz = rlink_khz,
1664  .speed_div = rlink_speed_div,
1665 
1666  .jtag_ops = &rlink_interface,
1667 };
uint32_t tdi_mask
Definition: bcm2835gpio.c:79
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
Definition: commands.c:192
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
Definition: commands.c:230
scan_type
The inferred type of a scan_command structure, indicating whether the command has the host scan in fr...
Definition: commands.h:22
@ SCAN_IN
From device to host,.
Definition: commands.h:24
@ SCAN_OUT
From host to device,.
Definition: commands.h:26
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_RESET
Definition: commands.h:139
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t type
Definition: esp_usb_jtag.c:0
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 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:1070
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1742
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
@ TAP_IRPAUSE
Definition: jtag.h:52
#define ERROR_JTAG_QUEUE_FAILED
Definition: jtag.h:556
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:220
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], const char *product, struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
#define LIBUSB_TIMEOUT_MS
Definition: libusb_helper.h:26
#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_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
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
Definition: rlink.c:524
uint8_t * buffer
Definition: rlink.c:529
int offset
Definition: rlink.c:531
int size
Definition: rlink.c:530
enum scan_type type
Definition: rlink.c:533
struct dtc_reply_queue_entry * next
Definition: rlink.c:525
struct jtag_command * cmd
Definition: rlink.c:526
struct dtc_reply_queue_entry::@36 scan
int length
Definition: rlink.c:532
union jtag_command_container cmd
Definition: commands.h:147
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
#define TRANSPORT_JTAG
Definition: transport.h:19
struct scan_command * scan
Definition: commands.h:113
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22