1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 Rob Brown, Lou Deluxe *
9 * rob@cobbleware.com, lou.openocd012@fixit.nospammail.net *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
29 /* project specific includes */
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
33 #include "rlink_st7.h"
34 #include "rlink_ep1_cmd.h"
35 #include "rlink_dtc_cmd.h"
36 #include "libusb_helper.h"
38 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on
39 *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
40 #undef AUTOMATIC_BUSY_LED
42 /* This feature may require derating the speed due to reduced hold time. */
43 #undef USE_HARDWARE_SHIFTER_FOR_TMS
45 #define INTERFACE_NAME "RLink"
47 #define USB_IDVENDOR (0x138e)
48 #define USB_IDPRODUCT (0x9000)
50 #define USB_EP1OUT_ADDR (0x01)
51 #define USB_EP1OUT_SIZE (16)
52 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
53 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
55 #define USB_EP2OUT_ADDR (0x02)
56 #define USB_EP2OUT_SIZE (64)
57 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
58 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
59 #define USB_EP2BANK_SIZE (512)
61 #define USB_TIMEOUT_MS (3 * 1000)
63 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
65 #define ST7_PD_NBUSY_LED ST7_PD0
66 #define ST7_PD_NRUN_LED ST7_PD1
67 /* low enables VPP at adapter header, high connects it to GND instead */
68 #define ST7_PD_VPP_SEL ST7_PD6
69 /* low: VPP = 12v, high: VPP <= 5v */
70 #define ST7_PD_VPP_SHDN ST7_PD7
72 /* These pins are connected together */
73 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
74 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
76 /* Symbolic mapping between port pins and numbered IO lines */
77 #define ST7_PA_IO1 ST7_PA1
78 #define ST7_PA_IO2 ST7_PA2
79 #define ST7_PA_IO4 ST7_PA4
80 #define ST7_PA_IO8 ST7_PA6
81 #define ST7_PA_IO10 ST7_PA7
82 #define ST7_PB_IO5 ST7_PB5
83 #define ST7_PC_IO9 ST7_PC1
84 #define ST7_PC_IO3 ST7_PC2
85 #define ST7_PC_IO7 ST7_PC3
86 #define ST7_PE_IO6 ST7_PE5
88 /* Symbolic mapping between numbered IO lines and adapter signals */
89 #define ST7_PA_RTCK ST7_PA_IO0
90 #define ST7_PA_NTRST ST7_PA_IO1
91 #define ST7_PC_TDI ST7_PC_IO3
92 #define ST7_PA_DBGRQ ST7_PA_IO4
93 #define ST7_PB_NSRST ST7_PB_IO5
94 #define ST7_PE_TMS ST7_PE_IO6
95 #define ST7_PC_TCK ST7_PC_IO7
96 #define ST7_PC_TDO ST7_PC_IO9
97 #define ST7_PA_DBGACK ST7_PA_IO10
99 static libusb_device_handle *pHDev;
102 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
103 * This function takes care of zeroing the unused bytes before sending the packet.
104 * Any reply packet is not handled by this function.
106 static int ep1_generic_commandl(libusb_device_handle *pHDev_param, size_t length, ...)
108 uint8_t usb_buffer[USB_EP1OUT_SIZE];
109 uint8_t *usb_buffer_p;
114 if (length > sizeof(usb_buffer))
115 length = sizeof(usb_buffer);
117 usb_buffer_p = usb_buffer;
119 va_start(ap, length);
121 *usb_buffer_p++ = va_arg(ap, int);
128 sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
131 usb_ret = jtag_libusb_bulk_write(
134 (char *)usb_buffer, sizeof(usb_buffer),
139 if (usb_ret != ERROR_OK)
145 static ssize_t ep1_memory_read(
146 libusb_device_handle *pHDev_param, uint16_t addr,
147 size_t length, uint8_t *buffer)
149 uint8_t usb_buffer[USB_EP1OUT_SIZE];
155 usb_buffer[0] = EP1_CMD_MEMORY_READ;
159 sizeof(usb_buffer) - 4
166 if (remain > sizeof(usb_buffer))
167 length = sizeof(usb_buffer);
171 usb_buffer[1] = addr >> 8;
172 usb_buffer[2] = addr;
173 usb_buffer[3] = length;
175 usb_ret = jtag_libusb_bulk_write(
176 pHDev_param, USB_EP1OUT_ADDR,
177 (char *)usb_buffer, sizeof(usb_buffer),
182 if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
185 usb_ret = jtag_libusb_bulk_read(
186 pHDev_param, USB_EP1IN_ADDR,
187 (char *)buffer, length,
192 if (usb_ret != ERROR_OK || transferred < (int)length)
205 static ssize_t ep1_memory_write(libusb_device_handle *pHDev_param, uint16_t addr,
206 size_t length, uint8_t const *buffer)
208 uint8_t usb_buffer[USB_EP1OUT_SIZE];
213 usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
219 if (remain > (sizeof(usb_buffer) - 4))
220 length = (sizeof(usb_buffer) - 4);
224 usb_buffer[1] = addr >> 8;
225 usb_buffer[2] = addr;
226 usb_buffer[3] = length;
233 usb_buffer + 4 + length,
235 sizeof(usb_buffer) - 4 - length
240 usb_ret = jtag_libusb_bulk_write(
241 pHDev_param, USB_EP1OUT_ADDR,
242 (char *)usb_buffer, sizeof(usb_buffer),
247 if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
261 static ssize_t ep1_memory_writel(libusb_device_handle *pHDev_param, uint16_t addr,
264 uint8_t buffer[USB_EP1OUT_SIZE - 4];
269 if (length > sizeof(buffer))
270 length = sizeof(buffer);
275 va_start(ap, length);
277 *buffer_p++ = va_arg(ap, int);
281 return ep1_memory_write(pHDev_param, addr, length, buffer);
285 #define DTCLOAD_COMMENT (0)
286 #define DTCLOAD_ENTRY (1)
287 #define DTCLOAD_LOAD (2)
288 #define DTCLOAD_RUN (3)
289 #define DTCLOAD_LUT_START (4)
290 #define DTCLOAD_LUT (5)
292 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
294 /* This gets set by the DTC loader */
295 static uint8_t dtc_entry_download;
297 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
298 static int dtc_load_from_buffer(libusb_device_handle *pHDev_param, const uint8_t *buffer,
307 struct header_s *header;
308 uint8_t lut_start = 0xc0;
310 dtc_entry_download = 0;
312 /* Stop the DTC before loading anything. */
313 usb_err = ep1_generic_commandl(
321 if (length < sizeof(*header)) {
322 LOG_ERROR("Malformed DTC image");
326 header = (struct header_s *)buffer;
327 buffer += sizeof(*header);
328 length -= sizeof(*header);
330 if (length < (size_t)header->length + 1) {
331 LOG_ERROR("Malformed DTC image");
335 switch (header->type) {
336 case DTCLOAD_COMMENT:
340 /* store entry addresses somewhere */
341 if (!strncmp("download", (char *)buffer + 1, 8))
342 dtc_entry_download = buffer[0];
346 /* Send the DTC program to ST7 RAM. */
347 usb_err = ep1_memory_write(
350 header->length + 1, buffer
355 /* Load it into the DTC. */
356 usb_err = ep1_generic_commandl(
359 (DTC_LOAD_BUFFER >> 8),
368 usb_err = ep1_generic_commandl(
379 case DTCLOAD_LUT_START:
380 lut_start = buffer[0];
384 usb_err = ep1_memory_write(
386 ST7_USB_BUF_EP0OUT + lut_start,
387 header->length + 1, buffer
394 LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
399 buffer += (header->length + 1);
400 length -= (header->length + 1);
407 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
409 static int dtc_start_download(void)
415 /* set up for download mode and make sure EP2 is set up to transmit */
416 usb_err = ep1_generic_commandl(
421 EP1_CMD_SET_DOWNLOAD,
422 EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
430 /* read back ep2txr */
431 usb_err = jtag_libusb_bulk_read(
432 pHDev, USB_EP1IN_ADDR,
437 if (usb_err != ERROR_OK)
440 usb_err = ep1_generic_commandl(
443 EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
444 DTC_STATUS_POLL_BYTE >> 8,
445 DTC_STATUS_POLL_BYTE,
448 EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
452 (ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
453 EP1_CMD_DTC_CALL, /* start running the DTC */
455 EP1_CMD_DTC_GET_CACHED_STATUS
460 /* wait for completion */
461 usb_err = jtag_libusb_bulk_read(
462 pHDev, USB_EP1IN_ADDR,
471 static int dtc_run_download(
472 libusb_device_handle *pHDev_param,
473 uint8_t *command_buffer,
474 int command_buffer_size,
475 uint8_t *reply_buffer,
476 int reply_buffer_size
484 LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
486 usb_err = jtag_libusb_bulk_write(
489 (char *)command_buffer, USB_EP2BANK_SIZE,
497 /* Wait for DTC to finish running command buffer */
499 usb_err = ep1_generic_commandl(
503 DTC_STATUS_POLL_BYTE >> 8,
504 DTC_STATUS_POLL_BYTE,
510 usb_err = jtag_libusb_bulk_read(
520 if (dtc_status & 0x01)
524 LOG_ERROR("too many retries waiting for DTC status");
525 return LIBUSB_ERROR_TIMEOUT;
530 if (reply_buffer && reply_buffer_size) {
531 usb_err = jtag_libusb_bulk_read(
534 (char *)reply_buffer, reply_buffer_size,
539 if (usb_err != ERROR_OK || transferred < reply_buffer_size) {
540 LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
541 usb_err, reply_buffer_size
551 * The dtc reply queue is a singly linked list that describes what to do
552 * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
556 struct dtc_reply_queue_entry {
557 struct dtc_reply_queue_entry *next;
558 struct jtag_command *cmd; /* the command that resulted in this entry */
561 uint8_t *buffer; /* the scan buffer */
562 int size; /* size of the scan buffer in bits */
563 int offset; /* how many bits were already done before this? */
564 int length; /* how many bits are processed in this operation? */
565 enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
571 * The dtc_queue consists of a buffer of pending commands and a reply queue.
572 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
576 struct dtc_reply_queue_entry *rq_head;
577 struct dtc_reply_queue_entry *rq_tail;
579 uint32_t reply_index;
580 uint8_t cmd_buffer[USB_EP2BANK_SIZE];
584 * The tap state queue is for accumulating TAP state changes without needlessly
585 * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
594 static int dtc_queue_init(void)
596 dtc_queue.rq_head = NULL;
597 dtc_queue.rq_tail = NULL;
598 dtc_queue.cmd_index = 0;
599 dtc_queue.reply_index = 0;
603 static inline struct dtc_reply_queue_entry *dtc_queue_enqueue_reply(
604 enum scan_type type, uint8_t *buffer, int size, int offset,
605 int length, struct jtag_command *cmd)
607 struct dtc_reply_queue_entry *rq_entry;
609 rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
610 if (rq_entry != NULL) {
611 rq_entry->scan.type = type;
612 rq_entry->scan.buffer = buffer;
613 rq_entry->scan.size = size;
614 rq_entry->scan.offset = offset;
615 rq_entry->scan.length = length;
617 rq_entry->next = NULL;
619 if (dtc_queue.rq_head == NULL)
620 dtc_queue.rq_head = rq_entry;
622 dtc_queue.rq_tail->next = rq_entry;
624 dtc_queue.rq_tail = rq_entry;
631 * Running the queue means that any pending command buffer is run
632 * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
633 * The queue is automatically run by append when it is necessary to get space for the append.
636 static int dtc_queue_run(void)
638 struct dtc_reply_queue_entry *rq_p, *rq_next;
643 uint8_t *dtc_p, *tdo_p;
644 uint8_t dtc_mask, tdo_mask;
645 uint8_t reply_buffer[USB_EP2IN_SIZE];
647 assert((dtc_queue.rq_head != 0) == (dtc_queue.reply_index > 0));
648 assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
649 assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
653 if (dtc_queue.cmd_index < 1)
656 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
658 usb_err = dtc_run_download(pHDev,
659 dtc_queue.cmd_buffer, dtc_queue.cmd_index,
660 reply_buffer, sizeof(reply_buffer)
663 LOG_ERROR("dtc_run_download: %s", libusb_error_name(usb_err));
667 if (dtc_queue.rq_head != NULL) {
668 /* process the reply, which empties the reply queue and frees its entries */
669 dtc_p = reply_buffer;
671 /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
672 *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
673 *was that or craft a function to do the reversal, and that wouldn't work with
674 *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
675 *scheme which would throw the byte alignment off. */
678 rq_p = dtc_queue.rq_head;
682 tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
683 tdo_mask = 1 << (rq_p->scan.offset % 8);
686 bit_cnt = rq_p->scan.length;
690 dtc_mask = 1 << (8 - 1);
697 if (*dtc_p & dtc_mask)
705 dtc_mask = 1 << (8 - 1);
715 /* extra bits or last bit */
718 if ((rq_p->scan.type == SCAN_IN) && (
719 rq_p->scan.offset != rq_p->scan.size - 1
721 /* extra bits were sent as a full byte with padding on the
723 dtc_mask = 1 << (8 - 1);
725 dtc_mask = 1 << (bit_cnt - 1);
748 if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
749 /* feed scan buffer back into openocd and free it */
750 if (jtag_read_buffer(rq_p->scan.buffer,
751 rq_p->cmd->cmd.scan) != ERROR_OK)
752 retval = ERROR_JTAG_QUEUE_FAILED;
753 free(rq_p->scan.buffer);
756 rq_next = rq_p->next;
759 dtc_queue.rq_head = NULL;
760 dtc_queue.rq_tail = NULL;
763 /* reset state for new appends */
764 dtc_queue.cmd_index = 0;
765 dtc_queue.reply_index = 0;
770 /* runs the queue if it cannot take reserved_cmd bytes of command data
771 * or reserved_reply bytes of reply data */
772 static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
774 /* reserve one additional byte for the STOP cmd appended during run */
775 if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
776 return dtc_queue_run();
778 if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
779 return dtc_queue_run();
784 static int tap_state_queue_init(void)
786 tap_state_queue.length = 0;
787 tap_state_queue.buffer = 0;
791 static int tap_state_queue_run(void)
799 if (!tap_state_queue.length)
803 for (i = tap_state_queue.length; i--; ) {
806 if (tap_state_queue.buffer & 1)
808 if ((bits >= 8) || !i) {
809 byte_param <<= (8 - bits);
811 /* make sure there's room for two cmd bytes */
812 dtc_queue_run_if_full(2, 0);
814 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
816 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
817 DTC_CMD_SHIFT_TMS_BYTES(1);
820 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
821 DTC_CMD_SHIFT_TMS_BITS(bits);
822 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
826 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
834 tap_state_queue.buffer >>= 1;
836 retval = tap_state_queue_init();
840 static int tap_state_queue_append(uint8_t tms)
844 if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
845 retval = tap_state_queue_run();
851 tap_state_queue.buffer |= (1 << tap_state_queue.length);
852 tap_state_queue.length++;
857 static void rlink_end_state(tap_state_t state)
859 if (tap_is_state_stable(state))
860 tap_set_end_state(state);
862 LOG_ERROR("BUG: %i is not a valid end state", state);
867 static void rlink_state_move(void)
871 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
872 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
874 for (i = 0; i < tms_count; i++) {
875 tms = (tms_scan >> i) & 1;
876 tap_state_queue_append(tms);
879 tap_set_state(tap_get_end_state());
882 static void rlink_path_move(struct pathmove_command *cmd)
884 int num_states = cmd->num_states;
890 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
892 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
895 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
896 tap_state_name(tap_get_state()),
897 tap_state_name(cmd->path[state_count]));
901 tap_state_queue_append(tms);
903 tap_set_state(cmd->path[state_count]);
908 tap_set_end_state(tap_get_state());
911 static void rlink_runtest(int num_cycles)
915 tap_state_t saved_end_state = tap_get_end_state();
917 /* only do a state_move when we're not already in RTI */
918 if (tap_get_state() != TAP_IDLE) {
919 rlink_end_state(TAP_IDLE);
923 /* execute num_cycles */
924 for (i = 0; i < num_cycles; i++)
925 tap_state_queue_append(0);
927 /* finish in end_state */
928 rlink_end_state(saved_end_state);
929 if (tap_get_state() != tap_get_end_state())
933 /* (1) assert or (0) deassert reset lines */
934 static void rlink_reset(int trst, int srst)
940 /* Read port A for bit op */
941 usb_err = ep1_generic_commandl(
949 LOG_ERROR("%s", libusb_error_name(usb_err));
953 usb_err = jtag_libusb_bulk_read(
954 pHDev, USB_EP1IN_ADDR,
959 if (usb_err != ERROR_OK || transferred < 1) {
960 LOG_ERROR("%s", libusb_error_name(usb_err));
965 bitmap &= ~ST7_PA_NTRST;
967 bitmap |= ST7_PA_NTRST;
969 /* Write port A and read port B for bit op
970 * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
971 *and assert NSRST by setting DDR to 1. */
972 usb_err = ep1_generic_commandl(
974 EP1_CMD_MEMORY_WRITE,
985 LOG_ERROR("%s", libusb_error_name(usb_err));
989 usb_err = jtag_libusb_bulk_read(
990 pHDev, USB_EP1IN_ADDR,
995 if (usb_err != ERROR_OK || transferred < 1) {
996 LOG_ERROR("%s", libusb_error_name(usb_err));
1001 bitmap |= ST7_PB_NSRST;
1003 bitmap &= ~ST7_PB_NSRST;
1005 /* write port B and read dummy to ensure completion before returning */
1006 usb_err = ep1_generic_commandl(
1008 EP1_CMD_MEMORY_WRITE,
1013 EP1_CMD_DTC_GET_CACHED_STATUS
1016 LOG_ERROR("%s", libusb_error_name(usb_err));
1020 usb_err = jtag_libusb_bulk_read(
1021 pHDev, USB_EP1IN_ADDR,
1026 if (usb_err != ERROR_OK || transferred < 1) {
1027 LOG_ERROR("%s", libusb_error_name(usb_err));
1032 static int rlink_scan(struct jtag_command *cmd, enum scan_type type,
1033 uint8_t *buffer, int scan_size)
1036 tap_state_t saved_end_state;
1044 uint8_t tdi_mask, *tdi_p;
1047 if (scan_size < 1) {
1048 LOG_ERROR("scan_size cannot be less than 1 bit");
1052 ir_scan = cmd->cmd.scan->ir_scan;
1054 /* Move to the proper state before starting to shift TDI/TDO. */
1055 if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) ||
1056 (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
1057 saved_end_state = tap_get_end_state();
1058 rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1060 rlink_end_state(saved_end_state);
1063 tap_state_queue_run();
1067 printf("scan_size = %d, type = 0x%x\n", scan_size, type);
1071 /* clear unused bits in scan buffer for ease of debugging
1072 * (it makes diffing output easier) */
1073 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1075 printf("before scan:");
1076 for (i = 0; i < (scan_size + 7) / 8; i++)
1077 printf(" %02x", buffer[i]);
1082 /* The number of bits that can be shifted as complete bytes */
1083 byte_bits = (int)(scan_size - 1) / 8 * 8;
1084 /* The number of bits left over, not counting the last bit */
1085 extra_bits = (scan_size - 1) - byte_bits;
1091 if (extra_bits && (type == SCAN_OUT)) {
1092 /* Schedule any extra bits into the DTC command buffer, padding as needed
1093 * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1094 *fall off the end */
1096 /* make sure there's room for two cmd bytes */
1097 dtc_queue_run_if_full(2, 0);
1100 dtc_mask = 1 << (extra_bits - 1);
1102 while (extra_bits--) {
1103 if (*tdi_p & tdi_mask)
1109 if (tdi_mask == 0) {
1115 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1116 DTC_CMD_SHIFT_TDI_BYTES(1);
1118 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1121 /* Loop scheduling full bytes into the DTC command buffer */
1123 /* make sure there's room for one (for in scans) or two cmd bytes and
1124 * at least one reply byte for in or inout scans*/
1125 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
1127 chunk_bits = byte_bits;
1128 /* we can only use up to 16 bytes at a time */
1129 if (chunk_bits > (16 * 8))
1130 chunk_bits = (16 * 8);
1132 if (type != SCAN_IN) {
1133 /* how much is there room for, considering stop and byte op? */
1134 x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1139 if (type != SCAN_OUT) {
1140 /* how much is there room for in the reply buffer? */
1141 x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1146 /* so the loop will end */
1147 byte_bits -= chunk_bits;
1149 if (type != SCAN_OUT) {
1150 if (dtc_queue_enqueue_reply(
1151 type, buffer, scan_size, tdi_bit_offset,
1155 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1158 dtc_queue.reply_index += (chunk_bits + 7) / 8;
1160 tdi_bit_offset += chunk_bits;
1163 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1164 chunk_bytes = chunk_bits / 8;
1168 x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1171 x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1174 x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1177 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1179 if (type != SCAN_IN) {
1181 dtc_mask = 1 << (8 - 1);
1183 while (chunk_bits--) {
1184 if (*tdi_p & tdi_mask)
1188 if (dtc_mask == 0) {
1189 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1191 dtc_mask = 1 << (8 - 1);
1195 if (tdi_mask == 0) {
1203 if (extra_bits && (type != SCAN_OUT)) {
1204 /* Schedule any extra bits into the DTC command buffer */
1206 /* make sure there's room for one (for in scans) or two cmd bytes
1207 * and one reply byte */
1208 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
1210 if (dtc_queue_enqueue_reply(
1211 type, buffer, scan_size, tdi_bit_offset,
1215 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1219 dtc_queue.reply_index++;
1221 tdi_bit_offset += extra_bits;
1223 if (type == SCAN_IN) {
1224 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1225 DTC_CMD_SHIFT_TDO_BYTES(1);
1228 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1229 DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1232 dtc_mask = 1 << (8 - 1);
1234 while (extra_bits--) {
1235 if (*tdi_p & tdi_mask)
1241 if (tdi_mask == 0) {
1247 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1251 /* Schedule the last bit into the DTC command buffer */
1253 /* make sure there's room for one cmd byte and one reply byte
1254 * for in or inout scans*/
1255 dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
1257 if (type == SCAN_OUT) {
1258 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1259 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1262 if (dtc_queue_enqueue_reply(
1263 type, buffer, scan_size, tdi_bit_offset,
1267 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1271 dtc_queue.reply_index++;
1273 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1274 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1277 /* Move to pause state */
1278 tap_state_queue_append(0);
1279 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1280 if (tap_get_state() != tap_get_end_state())
1286 static int rlink_execute_queue(void)
1288 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1290 enum scan_type type;
1292 int retval, tmp_retval;
1294 /* return ERROR_OK, unless something goes wrong */
1297 #ifndef AUTOMATIC_BUSY_LED
1299 ep1_generic_commandl(pHDev, 2,
1300 EP1_CMD_SET_PORTD_LEDS,
1306 switch (cmd->type) {
1308 case JTAG_TLR_RESET:
1314 /* some events, such as resets, need a queue flush to ensure
1316 tap_state_queue_run();
1321 switch (cmd->type) {
1323 LOG_DEBUG_IO("reset trst: %i srst %i",
1324 cmd->cmd.reset->trst,
1325 cmd->cmd.reset->srst);
1326 if ((cmd->cmd.reset->trst == 1) ||
1327 (cmd->cmd.reset->srst &&
1328 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1329 tap_set_state(TAP_RESET);
1330 rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1333 LOG_DEBUG_IO("runtest %i cycles, end in %i",
1334 cmd->cmd.runtest->num_cycles,
1335 cmd->cmd.runtest->end_state);
1336 if (cmd->cmd.runtest->end_state != -1)
1337 rlink_end_state(cmd->cmd.runtest->end_state);
1338 rlink_runtest(cmd->cmd.runtest->num_cycles);
1340 case JTAG_TLR_RESET:
1341 LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1342 if (cmd->cmd.statemove->end_state != -1)
1343 rlink_end_state(cmd->cmd.statemove->end_state);
1347 LOG_DEBUG_IO("pathmove: %i states, end in %i",
1348 cmd->cmd.pathmove->num_states,
1349 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1350 rlink_path_move(cmd->cmd.pathmove);
1353 LOG_DEBUG_IO("%s scan end in %i",
1354 (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
1355 cmd->cmd.scan->end_state);
1356 if (cmd->cmd.scan->end_state != -1)
1357 rlink_end_state(cmd->cmd.scan->end_state);
1358 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1359 type = jtag_scan_type(cmd->cmd.scan);
1360 if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK)
1361 retval = ERROR_FAIL;
1364 LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
1365 jtag_sleep(cmd->cmd.sleep->us);
1368 LOG_ERROR("BUG: unknown JTAG command type encountered");
1374 /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1376 tap_state_queue_run();
1377 tmp_retval = dtc_queue_run();
1378 if (tmp_retval != ERROR_OK)
1379 retval = tmp_retval;
1381 #ifndef AUTOMATIC_BUSY_LED
1383 ep1_generic_commandl(pHDev, 2,
1384 EP1_CMD_SET_PORTD_LEDS,
1392 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1393 *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1395 static int rlink_speed(int speed)
1401 speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler;
1404 for (i = rlink_speed_table_size; i--; ) {
1405 if (rlink_speed_table[i].prescaler == speed) {
1406 if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc,
1407 rlink_speed_table[i].dtc_size) != 0) {
1409 "An error occurred while trying to load DTC code for speed \"%d\".",
1414 int ret = dtc_start_download();
1416 LOG_ERROR("starting DTC: %s", libusb_error_name(ret));
1424 LOG_ERROR("%d is not a supported speed", speed);
1428 static int rlink_speed_div(int speed, int *khz)
1432 for (i = rlink_speed_table_size; i--; ) {
1433 if (rlink_speed_table[i].prescaler == speed) {
1434 *khz = rlink_speed_table[i].khz;
1439 LOG_ERROR("%d is not a supported speed", speed);
1443 static int rlink_khz(int khz, int *speed)
1448 LOG_ERROR("RCLK not supported");
1452 for (i = rlink_speed_table_size; i--; ) {
1453 if (rlink_speed_table[i].khz <= khz) {
1454 *speed = rlink_speed_table[i].prescaler;
1459 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1460 *speed = rlink_speed_table[0].prescaler;
1464 static int rlink_init(void)
1467 uint8_t reply_buffer[USB_EP1IN_SIZE];
1470 const uint16_t vids[] = { USB_IDVENDOR, 0 };
1471 const uint16_t pids[] = { USB_IDPRODUCT, 0 };
1472 if (jtag_libusb_open(vids, pids, NULL, &pHDev, NULL) != ERROR_OK)
1475 struct libusb_device_descriptor descriptor;
1476 struct libusb_device *usb_dev = libusb_get_device(pHDev);
1477 int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1479 LOG_ERROR("error %d getting device descriptor", r);
1483 if (descriptor.bNumConfigurations > 1) {
1484 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1487 struct libusb_config_descriptor *config;
1488 libusb_get_config_descriptor(usb_dev, 0, &config);
1489 if (config->bNumInterfaces > 1) {
1490 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1494 LOG_DEBUG("Opened device, pHDev = %p", pHDev);
1496 /* usb_set_configuration required under win32 */
1497 libusb_set_configuration(pHDev, config->bConfigurationValue);
1501 i = libusb_claim_interface(pHDev, 0);
1502 if (i != LIBUSB_SUCCESS) {
1503 LOG_ERROR("usb_claim_interface: %s", libusb_error_name(i));
1504 j = libusb_detach_kernel_driver(pHDev, 0);
1505 if (j != LIBUSB_SUCCESS)
1506 LOG_ERROR("detach kernel driver: %s", libusb_error_name(j));
1508 LOG_DEBUG("interface claimed!");
1511 } while (--retries);
1513 if (i != LIBUSB_SUCCESS) {
1514 LOG_ERROR("Initialisation failed.");
1517 if (libusb_set_interface_alt_setting(pHDev, 0, 0) != LIBUSB_SUCCESS) {
1518 LOG_ERROR("Failed to set interface.");
1522 /* The device starts out in an unknown state on open. As such,
1523 * result reads time out, and it's not even known whether the
1524 * command was accepted. So, for this first command, we issue
1525 * it repeatedly until its response doesn't time out. Also, if
1526 * sending a command is going to time out, we find that out here.
1528 * It must be possible to open the device in such a way that
1529 * this special magic isn't needed, but, so far, it escapes us.
1531 for (i = 0; i < 5; i++) {
1532 j = ep1_generic_commandl(
1536 if (j < USB_EP1OUT_SIZE) {
1537 LOG_ERROR("USB write error: %s", libusb_error_name(j));
1540 j = jtag_libusb_bulk_read(
1541 pHDev, USB_EP1IN_ADDR,
1542 (char *)reply_buffer, sizeof(reply_buffer),
1546 if (j != LIBUSB_ERROR_TIMEOUT)
1550 if (j != ERROR_OK || transferred != (int)sizeof(reply_buffer)) {
1551 LOG_ERROR("USB read error: %s", libusb_error_name(j));
1554 LOG_DEBUG(INTERFACE_NAME " firmware version: %d.%d.%d",
1559 if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1561 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1563 /* Probe port E for adapter presence */
1564 ep1_generic_commandl(
1566 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1571 ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1572 ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1573 EP1_CMD_MEMORY_READ, /* Read back */
1577 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1581 ST7_PE_ADAPTER_SENSE_OUT
1584 jtag_libusb_bulk_read(
1585 pHDev, USB_EP1IN_ADDR,
1586 (char *)reply_buffer, 1,
1591 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0)
1592 LOG_WARNING("target detection problem");
1594 ep1_generic_commandl(
1596 EP1_CMD_MEMORY_READ, /* Read back */
1600 EP1_CMD_MEMORY_WRITE, /* float port E */
1609 jtag_libusb_bulk_read(
1610 pHDev, USB_EP1IN_ADDR,
1611 (char *)reply_buffer, 1,
1617 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0)
1618 LOG_WARNING("target not plugged in");
1620 /* float ports A and B */
1621 ep1_generic_commandl(
1623 EP1_CMD_MEMORY_WRITE,
1629 EP1_CMD_MEMORY_WRITE,
1636 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1637 ep1_generic_commandl(
1640 EP1_CMD_SET_PORTD_VPP,
1642 EP1_CMD_MEMORY_WRITE,
1646 ((~(0)) & (ST7_PA_NTRST)),
1648 /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1649 *here and later assert NSRST by setting DDR bit to 1. */
1650 EP1_CMD_MEMORY_WRITE,
1657 /* set LED updating mode and make sure they're unlit */
1658 ep1_generic_commandl(
1660 #ifdef AUTOMATIC_BUSY_LED
1665 EP1_CMD_SET_PORTD_LEDS,
1669 tap_state_queue_init();
1676 static int rlink_quit(void)
1678 /* stop DTC and make sure LEDs are off */
1679 ep1_generic_commandl(
1683 EP1_CMD_SET_PORTD_LEDS,
1685 EP1_CMD_SET_PORTD_VPP,
1689 libusb_release_interface(pHDev, 0);
1690 libusb_close(pHDev);
1695 static struct jtag_interface rlink_interface = {
1696 .execute_queue = rlink_execute_queue,
1699 struct adapter_driver rlink_adapter_driver = {
1701 .transports = jtag_only,
1705 .speed = rlink_speed,
1707 .speed_div = rlink_speed_div,
1709 .jtag_ops = &rlink_interface,