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>
32 #include "helper/replacements.h"
34 #include "rlink_st7.h"
35 #include "rlink_ep1_cmd.h"
36 #include "rlink_dtc_cmd.h"
37 #include "libusb_helper.h"
39 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on
40 *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
41 #undef AUTOMATIC_BUSY_LED
43 /* This feature may require derating the speed due to reduced hold time. */
44 #undef USE_HARDWARE_SHIFTER_FOR_TMS
46 #define INTERFACE_NAME "RLink"
48 #define USB_IDVENDOR (0x138e)
49 #define USB_IDPRODUCT (0x9000)
51 #define USB_EP1OUT_ADDR (0x01)
52 #define USB_EP1OUT_SIZE (16)
53 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
54 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
56 #define USB_EP2OUT_ADDR (0x02)
57 #define USB_EP2OUT_SIZE (64)
58 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
59 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
60 #define USB_EP2BANK_SIZE (512)
62 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
64 #define ST7_PD_NBUSY_LED ST7_PD0
65 #define ST7_PD_NRUN_LED ST7_PD1
66 /* low enables VPP at adapter header, high connects it to GND instead */
67 #define ST7_PD_VPP_SEL ST7_PD6
68 /* low: VPP = 12v, high: VPP <= 5v */
69 #define ST7_PD_VPP_SHDN ST7_PD7
71 /* These pins are connected together */
72 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
73 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
75 /* Symbolic mapping between port pins and numbered IO lines */
76 #define ST7_PA_IO1 ST7_PA1
77 #define ST7_PA_IO2 ST7_PA2
78 #define ST7_PA_IO4 ST7_PA4
79 #define ST7_PA_IO8 ST7_PA6
80 #define ST7_PA_IO10 ST7_PA7
81 #define ST7_PB_IO5 ST7_PB5
82 #define ST7_PC_IO9 ST7_PC1
83 #define ST7_PC_IO3 ST7_PC2
84 #define ST7_PC_IO7 ST7_PC3
85 #define ST7_PE_IO6 ST7_PE5
87 /* Symbolic mapping between numbered IO lines and adapter signals */
88 #define ST7_PA_RTCK ST7_PA_IO0
89 #define ST7_PA_NTRST ST7_PA_IO1
90 #define ST7_PC_TDI ST7_PC_IO3
91 #define ST7_PA_DBGRQ ST7_PA_IO4
92 #define ST7_PB_NSRST ST7_PB_IO5
93 #define ST7_PE_TMS ST7_PE_IO6
94 #define ST7_PC_TCK ST7_PC_IO7
95 #define ST7_PC_TDO ST7_PC_IO9
96 #define ST7_PA_DBGACK ST7_PA_IO10
98 static struct libusb_device_handle *hdev;
101 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
102 * This function takes care of zeroing the unused bytes before sending the packet.
103 * Any reply packet is not handled by this function.
105 static int ep1_generic_commandl(struct libusb_device_handle *hdev_param, size_t length, ...)
107 uint8_t usb_buffer[USB_EP1OUT_SIZE];
108 uint8_t *usb_buffer_p;
113 if (length > sizeof(usb_buffer))
114 length = sizeof(usb_buffer);
116 usb_buffer_p = usb_buffer;
118 va_start(ap, length);
120 *usb_buffer_p++ = va_arg(ap, int);
127 sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
130 usb_ret = jtag_libusb_bulk_write(
133 (char *)usb_buffer, sizeof(usb_buffer),
138 if (usb_ret != ERROR_OK)
144 static ssize_t ep1_memory_read(
145 struct libusb_device_handle *hdev_param, uint16_t addr,
146 size_t length, uint8_t *buffer)
148 uint8_t usb_buffer[USB_EP1OUT_SIZE];
154 usb_buffer[0] = EP1_CMD_MEMORY_READ;
158 sizeof(usb_buffer) - 4
165 if (remain > sizeof(usb_buffer))
166 length = sizeof(usb_buffer);
170 usb_buffer[1] = addr >> 8;
171 usb_buffer[2] = addr;
172 usb_buffer[3] = length;
174 usb_ret = jtag_libusb_bulk_write(
175 hdev_param, USB_EP1OUT_ADDR,
176 (char *)usb_buffer, sizeof(usb_buffer),
181 if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
184 usb_ret = jtag_libusb_bulk_read(
185 hdev_param, USB_EP1IN_ADDR,
186 (char *)buffer, length,
191 if (usb_ret != ERROR_OK || transferred < (int)length)
204 static ssize_t ep1_memory_write(struct libusb_device_handle *hdev_param, uint16_t addr,
205 size_t length, uint8_t const *buffer)
207 uint8_t usb_buffer[USB_EP1OUT_SIZE];
212 usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
218 if (remain > (sizeof(usb_buffer) - 4))
219 length = (sizeof(usb_buffer) - 4);
223 usb_buffer[1] = addr >> 8;
224 usb_buffer[2] = addr;
225 usb_buffer[3] = length;
232 usb_buffer + 4 + length,
234 sizeof(usb_buffer) - 4 - length
239 usb_ret = jtag_libusb_bulk_write(
240 hdev_param, USB_EP1OUT_ADDR,
241 (char *)usb_buffer, sizeof(usb_buffer),
246 if (usb_ret != ERROR_OK || transferred < (int)sizeof(usb_buffer))
260 static ssize_t ep1_memory_writel(struct libusb_device_handle *hdev_param, uint16_t addr,
263 uint8_t buffer[USB_EP1OUT_SIZE - 4];
268 if (length > sizeof(buffer))
269 length = sizeof(buffer);
274 va_start(ap, length);
276 *buffer_p++ = va_arg(ap, int);
280 return ep1_memory_write(hdev_param, addr, length, buffer);
284 #define DTCLOAD_COMMENT (0)
285 #define DTCLOAD_ENTRY (1)
286 #define DTCLOAD_LOAD (2)
287 #define DTCLOAD_RUN (3)
288 #define DTCLOAD_LUT_START (4)
289 #define DTCLOAD_LUT (5)
291 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
293 /* This gets set by the DTC loader */
294 static uint8_t dtc_entry_download;
296 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
297 static int dtc_load_from_buffer(struct libusb_device_handle *hdev_param, const uint8_t *buffer,
306 struct header_s *header;
307 uint8_t lut_start = 0xc0;
309 dtc_entry_download = 0;
311 /* Stop the DTC before loading anything. */
312 usb_err = ep1_generic_commandl(
320 if (length < sizeof(*header)) {
321 LOG_ERROR("Malformed DTC image");
325 header = (struct header_s *)buffer;
326 buffer += sizeof(*header);
327 length -= sizeof(*header);
329 if (length < (size_t)header->length + 1) {
330 LOG_ERROR("Malformed DTC image");
334 switch (header->type) {
335 case DTCLOAD_COMMENT:
339 /* store entry addresses somewhere */
340 if (!strncmp("download", (char *)buffer + 1, 8))
341 dtc_entry_download = buffer[0];
345 /* Send the DTC program to ST7 RAM. */
346 usb_err = ep1_memory_write(
349 header->length + 1, buffer
354 /* Load it into the DTC. */
355 usb_err = ep1_generic_commandl(
358 (DTC_LOAD_BUFFER >> 8),
367 usb_err = ep1_generic_commandl(
378 case DTCLOAD_LUT_START:
379 lut_start = buffer[0];
383 usb_err = ep1_memory_write(
385 ST7_USB_BUF_EP0OUT + lut_start,
386 header->length + 1, buffer
393 LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
398 buffer += (header->length + 1);
399 length -= (header->length + 1);
406 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
408 static int dtc_start_download(void)
414 /* set up for download mode and make sure EP2 is set up to transmit */
415 usb_err = ep1_generic_commandl(
420 EP1_CMD_SET_DOWNLOAD,
421 EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
429 /* read back ep2txr */
430 usb_err = jtag_libusb_bulk_read(
431 hdev, USB_EP1IN_ADDR,
436 if (usb_err != ERROR_OK)
439 usb_err = ep1_generic_commandl(
442 EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
443 DTC_STATUS_POLL_BYTE >> 8,
444 DTC_STATUS_POLL_BYTE,
447 EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
451 (ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
452 EP1_CMD_DTC_CALL, /* start running the DTC */
454 EP1_CMD_DTC_GET_CACHED_STATUS
459 /* wait for completion */
460 usb_err = jtag_libusb_bulk_read(
461 hdev, USB_EP1IN_ADDR,
470 static int dtc_run_download(
471 struct libusb_device_handle *hdev_param,
472 uint8_t *command_buffer,
473 int command_buffer_size,
474 uint8_t *reply_buffer,
475 int reply_buffer_size
483 LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
485 usb_err = jtag_libusb_bulk_write(
488 (char *)command_buffer, USB_EP2BANK_SIZE,
496 /* Wait for DTC to finish running command buffer */
498 usb_err = ep1_generic_commandl(
502 DTC_STATUS_POLL_BYTE >> 8,
503 DTC_STATUS_POLL_BYTE,
509 usb_err = jtag_libusb_bulk_read(
519 if (dtc_status & 0x01)
523 LOG_ERROR("too many retries waiting for DTC status");
524 return LIBUSB_ERROR_TIMEOUT;
529 if (reply_buffer && reply_buffer_size) {
530 usb_err = jtag_libusb_bulk_read(
533 (char *)reply_buffer, reply_buffer_size,
538 if (usb_err != ERROR_OK || transferred < reply_buffer_size) {
539 LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
540 usb_err, reply_buffer_size
550 * The dtc reply queue is a singly linked list that describes what to do
551 * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
555 struct dtc_reply_queue_entry {
556 struct dtc_reply_queue_entry *next;
557 struct jtag_command *cmd; /* the command that resulted in this entry */
560 uint8_t *buffer; /* the scan buffer */
561 int size; /* size of the scan buffer in bits */
562 int offset; /* how many bits were already done before this? */
563 int length; /* how many bits are processed in this operation? */
564 enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
570 * The dtc_queue consists of a buffer of pending commands and a reply queue.
571 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
575 struct dtc_reply_queue_entry *rq_head;
576 struct dtc_reply_queue_entry *rq_tail;
578 uint32_t reply_index;
579 uint8_t cmd_buffer[USB_EP2BANK_SIZE];
583 * The tap state queue is for accumulating TAP state changes without needlessly
584 * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
593 static int dtc_queue_init(void)
595 dtc_queue.rq_head = NULL;
596 dtc_queue.rq_tail = NULL;
597 dtc_queue.cmd_index = 0;
598 dtc_queue.reply_index = 0;
602 static inline struct dtc_reply_queue_entry *dtc_queue_enqueue_reply(
603 enum scan_type type, uint8_t *buffer, int size, int offset,
604 int length, struct jtag_command *cmd)
606 struct dtc_reply_queue_entry *rq_entry;
608 rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
610 rq_entry->scan.type = type;
611 rq_entry->scan.buffer = buffer;
612 rq_entry->scan.size = size;
613 rq_entry->scan.offset = offset;
614 rq_entry->scan.length = length;
616 rq_entry->next = NULL;
618 if (!dtc_queue.rq_head)
619 dtc_queue.rq_head = rq_entry;
621 dtc_queue.rq_tail->next = rq_entry;
623 dtc_queue.rq_tail = rq_entry;
630 * Running the queue means that any pending command buffer is run
631 * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
632 * The queue is automatically run by append when it is necessary to get space for the append.
635 static int dtc_queue_run(void)
637 struct dtc_reply_queue_entry *rq_p, *rq_next;
642 uint8_t *dtc_p, *tdo_p;
643 uint8_t dtc_mask, tdo_mask;
644 uint8_t reply_buffer[USB_EP2IN_SIZE];
646 assert((dtc_queue.rq_head != 0) == (dtc_queue.reply_index > 0));
647 assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
648 assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
652 if (dtc_queue.cmd_index < 1)
655 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
657 usb_err = dtc_run_download(hdev,
658 dtc_queue.cmd_buffer, dtc_queue.cmd_index,
659 reply_buffer, sizeof(reply_buffer)
662 LOG_ERROR("dtc_run_download: %s", libusb_error_name(usb_err));
666 if (dtc_queue.rq_head) {
667 /* process the reply, which empties the reply queue and frees its entries */
668 dtc_p = reply_buffer;
670 /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
671 *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
672 *was that or craft a function to do the reversal, and that wouldn't work with
673 *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
674 *scheme which would throw the byte alignment off. */
677 rq_p = dtc_queue.rq_head;
681 tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
682 tdo_mask = 1 << (rq_p->scan.offset % 8);
685 bit_cnt = rq_p->scan.length;
689 dtc_mask = 1 << (8 - 1);
696 if (*dtc_p & dtc_mask)
704 dtc_mask = 1 << (8 - 1);
714 /* extra bits or last bit */
717 if ((rq_p->scan.type == SCAN_IN) && (
718 rq_p->scan.offset != rq_p->scan.size - 1
720 /* extra bits were sent as a full byte with padding on the
722 dtc_mask = 1 << (8 - 1);
724 dtc_mask = 1 << (bit_cnt - 1);
747 if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
748 /* feed scan buffer back into openocd and free it */
749 if (jtag_read_buffer(rq_p->scan.buffer,
750 rq_p->cmd->cmd.scan) != ERROR_OK)
751 retval = ERROR_JTAG_QUEUE_FAILED;
752 free(rq_p->scan.buffer);
755 rq_next = rq_p->next;
758 dtc_queue.rq_head = NULL;
759 dtc_queue.rq_tail = NULL;
762 /* reset state for new appends */
763 dtc_queue.cmd_index = 0;
764 dtc_queue.reply_index = 0;
769 /* runs the queue if it cannot take reserved_cmd bytes of command data
770 * or reserved_reply bytes of reply data */
771 static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
773 /* reserve one additional byte for the STOP cmd appended during run */
774 if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
775 return dtc_queue_run();
777 if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
778 return dtc_queue_run();
783 static int tap_state_queue_init(void)
785 tap_state_queue.length = 0;
786 tap_state_queue.buffer = 0;
790 static int tap_state_queue_run(void)
798 if (!tap_state_queue.length)
802 for (i = tap_state_queue.length; i--; ) {
805 if (tap_state_queue.buffer & 1)
807 if ((bits >= 8) || !i) {
808 byte_param <<= (8 - bits);
810 /* make sure there's room for two cmd bytes */
811 dtc_queue_run_if_full(2, 0);
813 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
815 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
816 DTC_CMD_SHIFT_TMS_BYTES(1);
819 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
820 DTC_CMD_SHIFT_TMS_BITS(bits);
821 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
825 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
833 tap_state_queue.buffer >>= 1;
835 retval = tap_state_queue_init();
839 static int tap_state_queue_append(uint8_t tms)
843 if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
844 retval = tap_state_queue_run();
850 tap_state_queue.buffer |= (1 << tap_state_queue.length);
851 tap_state_queue.length++;
856 static void rlink_end_state(tap_state_t state)
858 if (tap_is_state_stable(state))
859 tap_set_end_state(state);
861 LOG_ERROR("BUG: %i is not a valid end state", state);
866 static void rlink_state_move(void)
870 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
871 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
873 for (i = 0; i < tms_count; i++) {
874 tms = (tms_scan >> i) & 1;
875 tap_state_queue_append(tms);
878 tap_set_state(tap_get_end_state());
881 static void rlink_path_move(struct pathmove_command *cmd)
883 int num_states = cmd->num_states;
889 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
891 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
894 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
895 tap_state_name(tap_get_state()),
896 tap_state_name(cmd->path[state_count]));
900 tap_state_queue_append(tms);
902 tap_set_state(cmd->path[state_count]);
907 tap_set_end_state(tap_get_state());
910 static void rlink_runtest(int num_cycles)
914 tap_state_t saved_end_state = tap_get_end_state();
916 /* only do a state_move when we're not already in RTI */
917 if (tap_get_state() != TAP_IDLE) {
918 rlink_end_state(TAP_IDLE);
922 /* execute num_cycles */
923 for (i = 0; i < num_cycles; i++)
924 tap_state_queue_append(0);
926 /* finish in end_state */
927 rlink_end_state(saved_end_state);
928 if (tap_get_state() != tap_get_end_state())
932 /* (1) assert or (0) deassert reset lines */
933 static void rlink_reset(int trst, int srst)
939 /* Read port A for bit op */
940 usb_err = ep1_generic_commandl(
948 LOG_ERROR("%s", libusb_error_name(usb_err));
952 usb_err = jtag_libusb_bulk_read(
953 hdev, USB_EP1IN_ADDR,
958 if (usb_err != ERROR_OK || transferred < 1) {
959 LOG_ERROR("%s", libusb_error_name(usb_err));
964 bitmap &= ~ST7_PA_NTRST;
966 bitmap |= ST7_PA_NTRST;
968 /* Write port A and read port B for bit op
969 * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
970 *and assert NSRST by setting DDR to 1. */
971 usb_err = ep1_generic_commandl(
973 EP1_CMD_MEMORY_WRITE,
984 LOG_ERROR("%s", libusb_error_name(usb_err));
988 usb_err = jtag_libusb_bulk_read(
989 hdev, USB_EP1IN_ADDR,
994 if (usb_err != ERROR_OK || transferred < 1) {
995 LOG_ERROR("%s", libusb_error_name(usb_err));
1000 bitmap |= ST7_PB_NSRST;
1002 bitmap &= ~ST7_PB_NSRST;
1004 /* write port B and read dummy to ensure completion before returning */
1005 usb_err = ep1_generic_commandl(
1007 EP1_CMD_MEMORY_WRITE,
1012 EP1_CMD_DTC_GET_CACHED_STATUS
1015 LOG_ERROR("%s", libusb_error_name(usb_err));
1019 usb_err = jtag_libusb_bulk_read(
1020 hdev, USB_EP1IN_ADDR,
1025 if (usb_err != ERROR_OK || transferred < 1) {
1026 LOG_ERROR("%s", libusb_error_name(usb_err));
1031 static int rlink_scan(struct jtag_command *cmd, enum scan_type type,
1032 uint8_t *buffer, int scan_size)
1035 tap_state_t saved_end_state;
1043 uint8_t tdi_mask, *tdi_p;
1046 if (scan_size < 1) {
1047 LOG_ERROR("scan_size cannot be less than 1 bit");
1051 ir_scan = cmd->cmd.scan->ir_scan;
1053 /* Move to the proper state before starting to shift TDI/TDO. */
1054 if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) ||
1055 (ir_scan && (tap_get_state() == TAP_IRSHIFT)))) {
1056 saved_end_state = tap_get_end_state();
1057 rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1059 rlink_end_state(saved_end_state);
1062 tap_state_queue_run();
1066 printf("scan_size = %d, type = 0x%x\n", scan_size, type);
1070 /* clear unused bits in scan buffer for ease of debugging
1071 * (it makes diffing output easier) */
1072 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1074 printf("before scan:");
1075 for (i = 0; i < (scan_size + 7) / 8; i++)
1076 printf(" %02x", buffer[i]);
1081 /* The number of bits that can be shifted as complete bytes */
1082 byte_bits = (int)(scan_size - 1) / 8 * 8;
1083 /* The number of bits left over, not counting the last bit */
1084 extra_bits = (scan_size - 1) - byte_bits;
1090 if (extra_bits && (type == SCAN_OUT)) {
1091 /* Schedule any extra bits into the DTC command buffer, padding as needed
1092 * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1093 *fall off the end */
1095 /* make sure there's room for two cmd bytes */
1096 dtc_queue_run_if_full(2, 0);
1099 dtc_mask = 1 << (extra_bits - 1);
1101 while (extra_bits--) {
1102 if (*tdi_p & tdi_mask)
1108 if (tdi_mask == 0) {
1114 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1115 DTC_CMD_SHIFT_TDI_BYTES(1);
1117 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1120 /* Loop scheduling full bytes into the DTC command buffer */
1122 /* make sure there's room for one (for in scans) or two cmd bytes and
1123 * at least one reply byte for in or inout scans*/
1124 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
1126 chunk_bits = byte_bits;
1127 /* we can only use up to 16 bytes at a time */
1128 if (chunk_bits > (16 * 8))
1129 chunk_bits = (16 * 8);
1131 if (type != SCAN_IN) {
1132 /* how much is there room for, considering stop and byte op? */
1133 x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1138 if (type != SCAN_OUT) {
1139 /* how much is there room for in the reply buffer? */
1140 x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1145 /* so the loop will end */
1146 byte_bits -= chunk_bits;
1148 if (type != SCAN_OUT) {
1149 if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1151 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1154 dtc_queue.reply_index += (chunk_bits + 7) / 8;
1156 tdi_bit_offset += chunk_bits;
1159 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1160 chunk_bytes = chunk_bits / 8;
1164 x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1167 x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1170 x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1173 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1175 if (type != SCAN_IN) {
1177 dtc_mask = 1 << (8 - 1);
1179 while (chunk_bits--) {
1180 if (*tdi_p & tdi_mask)
1184 if (dtc_mask == 0) {
1185 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1187 dtc_mask = 1 << (8 - 1);
1191 if (tdi_mask == 0) {
1199 if (extra_bits && (type != SCAN_OUT)) {
1200 /* Schedule any extra bits into the DTC command buffer */
1202 /* make sure there's room for one (for in scans) or two cmd bytes
1203 * and one reply byte */
1204 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
1206 if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1208 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1212 dtc_queue.reply_index++;
1214 tdi_bit_offset += extra_bits;
1216 if (type == SCAN_IN) {
1217 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1218 DTC_CMD_SHIFT_TDO_BYTES(1);
1221 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1222 DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1225 dtc_mask = 1 << (8 - 1);
1227 while (extra_bits--) {
1228 if (*tdi_p & tdi_mask)
1234 if (tdi_mask == 0) {
1240 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1244 /* Schedule the last bit into the DTC command buffer */
1246 /* make sure there's room for one cmd byte and one reply byte
1247 * for in or inout scans*/
1248 dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
1250 if (type == SCAN_OUT) {
1251 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1252 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1255 if (!dtc_queue_enqueue_reply(type, buffer, scan_size, tdi_bit_offset,
1257 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1261 dtc_queue.reply_index++;
1263 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1264 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1267 /* Move to pause state */
1268 tap_state_queue_append(0);
1269 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1270 if (tap_get_state() != tap_get_end_state())
1276 static int rlink_execute_queue(void)
1278 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1280 enum scan_type type;
1282 int retval, tmp_retval;
1284 /* return ERROR_OK, unless something goes wrong */
1287 #ifndef AUTOMATIC_BUSY_LED
1289 ep1_generic_commandl(hdev, 2,
1290 EP1_CMD_SET_PORTD_LEDS,
1296 switch (cmd->type) {
1298 case JTAG_TLR_RESET:
1304 /* some events, such as resets, need a queue flush to ensure
1306 tap_state_queue_run();
1311 switch (cmd->type) {
1313 LOG_DEBUG_IO("reset trst: %i srst %i",
1314 cmd->cmd.reset->trst,
1315 cmd->cmd.reset->srst);
1316 if ((cmd->cmd.reset->trst == 1) ||
1317 (cmd->cmd.reset->srst &&
1318 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1319 tap_set_state(TAP_RESET);
1320 rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1323 LOG_DEBUG_IO("runtest %i cycles, end in %i",
1324 cmd->cmd.runtest->num_cycles,
1325 cmd->cmd.runtest->end_state);
1326 if (cmd->cmd.runtest->end_state != -1)
1327 rlink_end_state(cmd->cmd.runtest->end_state);
1328 rlink_runtest(cmd->cmd.runtest->num_cycles);
1330 case JTAG_TLR_RESET:
1331 LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
1332 if (cmd->cmd.statemove->end_state != -1)
1333 rlink_end_state(cmd->cmd.statemove->end_state);
1337 LOG_DEBUG_IO("pathmove: %i states, end in %i",
1338 cmd->cmd.pathmove->num_states,
1339 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1340 rlink_path_move(cmd->cmd.pathmove);
1343 LOG_DEBUG_IO("%s scan end in %i",
1344 (cmd->cmd.scan->ir_scan) ? "IR" : "DR",
1345 cmd->cmd.scan->end_state);
1346 if (cmd->cmd.scan->end_state != -1)
1347 rlink_end_state(cmd->cmd.scan->end_state);
1348 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1349 type = jtag_scan_type(cmd->cmd.scan);
1350 if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK)
1351 retval = ERROR_FAIL;
1354 LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
1355 jtag_sleep(cmd->cmd.sleep->us);
1358 LOG_ERROR("BUG: unknown JTAG command type encountered");
1364 /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1366 tap_state_queue_run();
1367 tmp_retval = dtc_queue_run();
1368 if (tmp_retval != ERROR_OK)
1369 retval = tmp_retval;
1371 #ifndef AUTOMATIC_BUSY_LED
1373 ep1_generic_commandl(hdev, 2,
1374 EP1_CMD_SET_PORTD_LEDS,
1382 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1383 *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1385 static int rlink_speed(int speed)
1391 speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler;
1394 for (i = rlink_speed_table_size; i--; ) {
1395 if (rlink_speed_table[i].prescaler == speed) {
1396 if (dtc_load_from_buffer(hdev, rlink_speed_table[i].dtc,
1397 rlink_speed_table[i].dtc_size) != 0) {
1399 "An error occurred while trying to load DTC code for speed \"%d\".",
1404 int ret = dtc_start_download();
1406 LOG_ERROR("starting DTC: %s", libusb_error_name(ret));
1414 LOG_ERROR("%d is not a supported speed", speed);
1418 static int rlink_speed_div(int speed, int *khz)
1422 for (i = rlink_speed_table_size; i--; ) {
1423 if (rlink_speed_table[i].prescaler == speed) {
1424 *khz = rlink_speed_table[i].khz;
1429 LOG_ERROR("%d is not a supported speed", speed);
1433 static int rlink_khz(int khz, int *speed)
1438 LOG_ERROR("RCLK not supported");
1442 for (i = rlink_speed_table_size; i--; ) {
1443 if (rlink_speed_table[i].khz <= khz) {
1444 *speed = rlink_speed_table[i].prescaler;
1449 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1450 *speed = rlink_speed_table[0].prescaler;
1454 static int rlink_init(void)
1457 uint8_t reply_buffer[USB_EP1IN_SIZE];
1460 const uint16_t vids[] = { USB_IDVENDOR, 0 };
1461 const uint16_t pids[] = { USB_IDPRODUCT, 0 };
1462 if (jtag_libusb_open(vids, pids, &hdev, NULL) != ERROR_OK)
1465 struct libusb_device_descriptor descriptor;
1466 struct libusb_device *usb_dev = libusb_get_device(hdev);
1467 int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1469 LOG_ERROR("error %d getting device descriptor", r);
1473 if (descriptor.bNumConfigurations > 1) {
1474 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1477 struct libusb_config_descriptor *config;
1478 libusb_get_config_descriptor(usb_dev, 0, &config);
1479 if (config->bNumInterfaces > 1) {
1480 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1484 LOG_DEBUG("Opened device, hdev = %p", hdev);
1486 /* usb_set_configuration required under win32 */
1487 libusb_set_configuration(hdev, config->bConfigurationValue);
1491 i = libusb_claim_interface(hdev, 0);
1492 if (i != LIBUSB_SUCCESS) {
1493 LOG_ERROR("usb_claim_interface: %s", libusb_error_name(i));
1494 j = libusb_detach_kernel_driver(hdev, 0);
1495 if (j != LIBUSB_SUCCESS)
1496 LOG_ERROR("detach kernel driver: %s", libusb_error_name(j));
1498 LOG_DEBUG("interface claimed!");
1501 } while (--retries);
1503 if (i != LIBUSB_SUCCESS) {
1504 LOG_ERROR("Initialisation failed.");
1507 if (libusb_set_interface_alt_setting(hdev, 0, 0) != LIBUSB_SUCCESS) {
1508 LOG_ERROR("Failed to set interface.");
1512 /* The device starts out in an unknown state on open. As such,
1513 * result reads time out, and it's not even known whether the
1514 * command was accepted. So, for this first command, we issue
1515 * it repeatedly until its response doesn't time out. Also, if
1516 * sending a command is going to time out, we find that out here.
1518 * It must be possible to open the device in such a way that
1519 * this special magic isn't needed, but, so far, it escapes us.
1521 for (i = 0; i < 5; i++) {
1522 j = ep1_generic_commandl(
1526 if (j < USB_EP1OUT_SIZE) {
1527 LOG_ERROR("USB write error: %s", libusb_error_name(j));
1530 j = jtag_libusb_bulk_read(
1531 hdev, USB_EP1IN_ADDR,
1532 (char *)reply_buffer, sizeof(reply_buffer),
1536 if (j != LIBUSB_ERROR_TIMEOUT)
1540 if (j != ERROR_OK || transferred != (int)sizeof(reply_buffer)) {
1541 LOG_ERROR("USB read error: %s", libusb_error_name(j));
1544 LOG_DEBUG(INTERFACE_NAME " firmware version: %d.%d.%d",
1549 if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1551 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1553 /* Probe port E for adapter presence */
1554 ep1_generic_commandl(
1556 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1561 ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1562 ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1563 EP1_CMD_MEMORY_READ, /* Read back */
1567 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1571 ST7_PE_ADAPTER_SENSE_OUT
1574 jtag_libusb_bulk_read(
1575 hdev, USB_EP1IN_ADDR,
1576 (char *)reply_buffer, 1,
1581 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0)
1582 LOG_WARNING("target detection problem");
1584 ep1_generic_commandl(
1586 EP1_CMD_MEMORY_READ, /* Read back */
1590 EP1_CMD_MEMORY_WRITE, /* float port E */
1599 jtag_libusb_bulk_read(
1600 hdev, USB_EP1IN_ADDR,
1601 (char *)reply_buffer, 1,
1607 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0)
1608 LOG_WARNING("target not plugged in");
1610 /* float ports A and B */
1611 ep1_generic_commandl(
1613 EP1_CMD_MEMORY_WRITE,
1619 EP1_CMD_MEMORY_WRITE,
1626 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1627 ep1_generic_commandl(
1630 EP1_CMD_SET_PORTD_VPP,
1632 EP1_CMD_MEMORY_WRITE,
1636 ((~(0)) & (ST7_PA_NTRST)),
1638 /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1639 *here and later assert NSRST by setting DDR bit to 1. */
1640 EP1_CMD_MEMORY_WRITE,
1647 /* set LED updating mode and make sure they're unlit */
1648 ep1_generic_commandl(
1650 #ifdef AUTOMATIC_BUSY_LED
1655 EP1_CMD_SET_PORTD_LEDS,
1659 tap_state_queue_init();
1666 static int rlink_quit(void)
1668 /* stop DTC and make sure LEDs are off */
1669 ep1_generic_commandl(
1673 EP1_CMD_SET_PORTD_LEDS,
1675 EP1_CMD_SET_PORTD_VPP,
1679 libusb_release_interface(hdev, 0);
1685 static struct jtag_interface rlink_interface = {
1686 .execute_queue = rlink_execute_queue,
1689 struct adapter_driver rlink_adapter_driver = {
1691 .transports = jtag_only,
1695 .speed = rlink_speed,
1697 .speed_div = rlink_speed_div,
1699 .jtag_ops = &rlink_interface,