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, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
30 /* project specific includes */
31 #include <jtag/interface.h>
32 #include <jtag/commands.h>
34 #include "rlink_st7.h"
35 #include "rlink_ep1_cmd.h"
36 #include "rlink_dtc_cmd.h"
37 #include "usb_common.h"
40 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on 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
47 #define INTERFACE_NAME "RLink"
49 #define USB_IDVENDOR (0x138e)
50 #define USB_IDPRODUCT (0x9000)
52 #define USB_EP1OUT_ADDR (0x01)
53 #define USB_EP1OUT_SIZE (16)
54 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
55 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
57 #define USB_EP2OUT_ADDR (0x02)
58 #define USB_EP2OUT_SIZE (64)
59 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
60 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
61 #define USB_EP2BANK_SIZE (512)
63 #define USB_TIMEOUT_MS (3 * 1000)
65 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
68 #define ST7_PD_NBUSY_LED ST7_PD0
69 #define ST7_PD_NRUN_LED ST7_PD1
70 /* low enables VPP at adapter header, high connects it to GND instead */
71 #define ST7_PD_VPP_SEL ST7_PD6
72 /* low: VPP = 12v, high: VPP <= 5v */
73 #define ST7_PD_VPP_SHDN ST7_PD7
75 /* These pins are connected together */
76 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
77 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
79 /* Symbolic mapping between port pins and numbered IO lines */
80 #define ST7_PA_IO1 ST7_PA1
81 #define ST7_PA_IO2 ST7_PA2
82 #define ST7_PA_IO4 ST7_PA4
83 #define ST7_PA_IO8 ST7_PA6
84 #define ST7_PA_IO10 ST7_PA7
85 #define ST7_PB_IO5 ST7_PB5
86 #define ST7_PC_IO9 ST7_PC1
87 #define ST7_PC_IO3 ST7_PC2
88 #define ST7_PC_IO7 ST7_PC3
89 #define ST7_PE_IO6 ST7_PE5
91 /* Symbolic mapping between numbered IO lines and adapter signals */
92 #define ST7_PA_RTCK ST7_PA_IO0
93 #define ST7_PA_NTRST ST7_PA_IO1
94 #define ST7_PC_TDI ST7_PC_IO3
95 #define ST7_PA_DBGRQ ST7_PA_IO4
96 #define ST7_PB_NSRST ST7_PB_IO5
97 #define ST7_PE_TMS ST7_PE_IO6
98 #define ST7_PC_TCK ST7_PC_IO7
99 #define ST7_PC_TDO ST7_PC_IO9
100 #define ST7_PA_DBGACK ST7_PA_IO10
102 static usb_dev_handle *pHDev;
106 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
107 * This function takes care of zeroing the unused bytes before sending the packet.
108 * Any reply packet is not handled by this function.
112 ep1_generic_commandl(
113 usb_dev_handle *pHDev_param,
117 uint8_t usb_buffer[USB_EP1OUT_SIZE];
118 uint8_t *usb_buffer_p;
122 if (length > sizeof(usb_buffer)) {
123 length = sizeof(usb_buffer);
126 usb_buffer_p = usb_buffer;
128 va_start(ap, length);
130 *usb_buffer_p++ = va_arg(ap, int);
137 sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
140 usb_ret = usb_bulk_write(
143 (char *)usb_buffer, sizeof(usb_buffer),
156 usb_dev_handle *pHDev,
161 uint8_t usb_buffer[USB_EP1OUT_SIZE];
166 usb_buffer[0] = EP1_CMD_MEMORY_READ;
170 sizeof(usb_buffer) - 4
177 if (remain > sizeof(usb_buffer)) {
178 length = sizeof(usb_buffer);
183 usb_buffer[1] = addr >> 8;
184 usb_buffer[2] = addr;
185 usb_buffer[3] = length;
187 usb_ret = usb_bulk_write(
188 pHDev, USB_EP1OUT_ADDR,
189 usb_buffer, sizeof(usb_buffer),
193 if (usb_ret < sizeof(usb_buffer)) {
197 usb_ret = usb_bulk_read(
198 pHDev, USB_EP1IN_ADDR,
203 if (usb_ret < length) {
222 usb_dev_handle *pHDev_param,
225 uint8_t const *buffer
227 uint8_t usb_buffer[USB_EP1OUT_SIZE];
232 usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
238 if (remain > (sizeof(usb_buffer) - 4)) {
239 length = (sizeof(usb_buffer) - 4);
244 usb_buffer[1] = addr >> 8;
245 usb_buffer[2] = addr;
246 usb_buffer[3] = length;
253 usb_buffer + 4 + length,
255 sizeof(usb_buffer) - 4 - length
258 usb_ret = usb_bulk_write(
259 pHDev_param, USB_EP1OUT_ADDR,
260 (char *)usb_buffer, sizeof(usb_buffer),
264 if ((size_t)usb_ret < sizeof(usb_buffer)) {
282 usb_dev_handle *pHDev,
287 uint8_t buffer[USB_EP1OUT_SIZE - 4];
292 if (length > sizeof(buffer)) {
293 length = sizeof(buffer);
299 va_start(ap, length);
301 *buffer_p++ = va_arg(ap, int);
305 return(ep1_memory_write(pHDev, addr, length, buffer));
310 #define DTCLOAD_COMMENT (0)
311 #define DTCLOAD_ENTRY (1)
312 #define DTCLOAD_LOAD (2)
313 #define DTCLOAD_RUN (3)
314 #define DTCLOAD_LUT_START (4)
315 #define DTCLOAD_LUT (5)
317 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
319 /* This gets set by the DTC loader */
320 static uint8_t dtc_entry_download;
323 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
326 dtc_load_from_buffer(
327 usb_dev_handle *pHDev_param,
328 const uint8_t *buffer,
337 struct header_s *header;
338 uint8_t lut_start = 0xc0;
340 dtc_entry_download = 0;
342 /* Stop the DTC before loading anything. */
343 usb_err = ep1_generic_commandl(
347 if (usb_err < 0) return(usb_err);
350 if (length < sizeof(*header)) {
351 LOG_ERROR("Malformed DTC image");
355 header = (struct header_s *)buffer;
356 buffer += sizeof(*header);
357 length -= sizeof(*header);
359 if (length < (size_t)header->length + 1) {
360 LOG_ERROR("Malformed DTC image");
364 switch (header->type) {
365 case DTCLOAD_COMMENT:
369 /* store entry addresses somewhere */
370 if (!strncmp("download", (char *)buffer + 1, 8)) {
371 dtc_entry_download = buffer[0];
376 /* Send the DTC program to ST7 RAM. */
377 usb_err = ep1_memory_write(
380 header->length + 1, buffer
382 if (usb_err < 0) return(usb_err);
384 /* Load it into the DTC. */
385 usb_err = ep1_generic_commandl(
388 (DTC_LOAD_BUFFER >> 8),
391 if (usb_err < 0) return(usb_err);
396 usb_err = ep1_generic_commandl(
402 if (usb_err < 0) return(usb_err);
406 case DTCLOAD_LUT_START:
407 lut_start = buffer[0];
411 usb_err = ep1_memory_write(
413 ST7_USB_BUF_EP0OUT + lut_start,
414 header->length + 1, buffer
416 if (usb_err < 0) return(usb_err);
420 LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
425 buffer += (header->length + 1);
426 length -= (header->length + 1);
434 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
438 dtc_start_download(void) {
442 /* set up for download mode and make sure EP2 is set up to transmit */
443 usb_err = ep1_generic_commandl(
448 EP1_CMD_SET_DOWNLOAD,
449 EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
454 if (usb_err < 0) return(usb_err);
456 /* read back ep2txr */
457 usb_err = usb_bulk_read(
458 pHDev, USB_EP1IN_ADDR,
462 if (usb_err < 0) return(usb_err);
464 usb_err = ep1_generic_commandl(
467 EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
468 DTC_STATUS_POLL_BYTE >> 8,
469 DTC_STATUS_POLL_BYTE,
472 EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
476 (ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
477 EP1_CMD_DTC_CALL, /* start running the DTC */
479 EP1_CMD_DTC_GET_CACHED_STATUS
481 if (usb_err < 0) return(usb_err);
483 /* wait for completion */
484 usb_err = usb_bulk_read(
485 pHDev, USB_EP1IN_ADDR,
497 usb_dev_handle *pHDev_param,
498 uint8_t *command_buffer,
499 int command_buffer_size,
500 uint8_t *reply_buffer,
501 int reply_buffer_size
507 LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
509 usb_err = usb_bulk_write(
512 (char *)command_buffer, USB_EP2BANK_SIZE,
515 if (usb_err < 0) return(usb_err);
518 /* Wait for DTC to finish running command buffer */
520 usb_err = ep1_generic_commandl(
524 DTC_STATUS_POLL_BYTE >> 8,
525 DTC_STATUS_POLL_BYTE,
528 if (usb_err < 0) return(usb_err);
530 usb_err = usb_bulk_read(
536 if (usb_err < 0) return(usb_err);
538 if (dtc_status & 0x01) break;
541 LOG_ERROR("too many retries waiting for DTC status");
547 if (reply_buffer && reply_buffer_size) {
548 usb_err = usb_bulk_read(
551 (char *)reply_buffer, reply_buffer_size,
555 if (usb_err < reply_buffer_size) {
556 LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
557 usb_err, reply_buffer_size
568 * The dtc reply queue is a singly linked list that describes what to do with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate these entries.
571 struct dtc_reply_queue_entry {
572 struct dtc_reply_queue_entry *next;
573 struct jtag_command *cmd; /* the command that resulted in this entry */
576 uint8_t *buffer; /* the scan buffer */
577 int size; /* size of the scan buffer in bits */
578 int offset; /* how many bits were already done before this? */
579 int length; /* how many bits are processed in this operation? */
580 enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
586 * The dtc_queue consists of a buffer of pending commands and a reply queue.
587 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
592 struct dtc_reply_queue_entry *rq_head;
593 struct dtc_reply_queue_entry *rq_tail;
595 uint32_t reply_index;
596 uint8_t cmd_buffer[USB_EP2BANK_SIZE];
601 * The tap state queue is for accumulating TAP state changes wiithout needlessly flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to the dtc_queue.
614 dtc_queue_init(void) {
615 dtc_queue.rq_head = NULL;
616 dtc_queue.rq_tail = NULL;
617 dtc_queue.cmd_index = 0;
618 dtc_queue.reply_index = 0;
625 struct dtc_reply_queue_entry *
626 dtc_queue_enqueue_reply(
632 struct jtag_command *cmd
634 struct dtc_reply_queue_entry *rq_entry;
636 rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
637 if (rq_entry != NULL) {
638 rq_entry->scan.type = type;
639 rq_entry->scan.buffer = buffer;
640 rq_entry->scan.size = size;
641 rq_entry->scan.offset = offset;
642 rq_entry->scan.length = length;
644 rq_entry->next = NULL;
646 if (dtc_queue.rq_head == NULL)
647 dtc_queue.rq_head = rq_entry;
649 dtc_queue.rq_tail->next = rq_entry;
651 dtc_queue.rq_tail = rq_entry;
659 * Running the queue means that any pending command buffer is run and any reply data dealt with. The command buffer is then cleared for subsequent processing.
660 * The queue is automatically run by append when it is necessary to get space for the append.
665 dtc_queue_run(void) {
666 struct dtc_reply_queue_entry *rq_p, *rq_next;
671 uint8_t *dtc_p, *tdo_p;
672 uint8_t dtc_mask, tdo_mask;
673 uint8_t reply_buffer[USB_EP2IN_SIZE];
675 assert((dtc_queue.rq_head != 0) == (dtc_queue.reply_index > 0));
676 assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
677 assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
681 if (dtc_queue.cmd_index < 1) return(retval);
683 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
685 usb_err = dtc_run_download(pHDev,
686 dtc_queue.cmd_buffer, dtc_queue.cmd_index,
687 reply_buffer, dtc_queue.reply_index
690 LOG_ERROR("dtc_run_download: %s", usb_strerror());
694 if (dtc_queue.rq_head != NULL) {
695 /* process the reply, which empties the reply queue and frees its entries */
696 dtc_p = reply_buffer;
698 /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It was that or craft a function to do the reversal, and that wouldn't work with bit-stuffing (supplying extra bits to use mostly byte operations), or any other scheme which would throw the byte alignment off. */
701 rq_p = dtc_queue.rq_head;
705 tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
706 tdo_mask = 1 << (rq_p->scan.offset % 8);
709 bit_cnt = rq_p->scan.length;
713 dtc_mask = 1 << (8 - 1);
720 if (*dtc_p & dtc_mask) {
729 dtc_mask = 1 << (8 - 1);
739 /* extra bits or last bit */
743 rq_p->scan.type == SCAN_IN
745 rq_p->scan.offset != rq_p->scan.size - 1
747 /* extra bits were sent as a full byte with padding on the end */
748 dtc_mask = 1 << (8 - 1);
750 dtc_mask = 1 << (bit_cnt - 1);
775 if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
776 /* feed scan buffer back into openocd and free it */
777 if (jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) {
778 retval = ERROR_JTAG_QUEUE_FAILED;
780 free(rq_p->scan.buffer);
783 rq_next = rq_p->next;
786 dtc_queue.rq_head = NULL;
787 dtc_queue.rq_tail = NULL;
791 /* reset state for new appends */
792 dtc_queue.cmd_index = 0;
793 dtc_queue.reply_index = 0;
798 /* runs the queue if it cannot take reserved_cmd bytes of command data
799 * or reserved_reply bytes of reply data */
802 dtc_queue_run_if_full(
806 /* reserve one additional byte for the STOP cmd appended during run */
807 if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
808 return dtc_queue_run();
810 if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
811 return dtc_queue_run();
818 tap_state_queue_init(void) {
819 tap_state_queue.length = 0;
820 tap_state_queue.buffer = 0;
827 tap_state_queue_run(void) {
834 if (!tap_state_queue.length) return(retval);
837 for (i = tap_state_queue.length; i--;) {
840 if (tap_state_queue.buffer & 1) {
843 if ((bits >= 8) || !i) {
844 byte_param <<= (8 - bits);
846 /* make sure there's room for two cmd bytes */
847 dtc_queue_run_if_full(2, 0);
849 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
851 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
852 DTC_CMD_SHIFT_TMS_BYTES(1);
855 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
856 DTC_CMD_SHIFT_TMS_BITS(bits);
857 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
861 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
870 tap_state_queue.buffer >>= 1;
872 retval = tap_state_queue_init();
879 tap_state_queue_append(
884 if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
885 retval = tap_state_queue_run();
886 if (retval != 0) return(retval);
890 tap_state_queue.buffer |= (1 << tap_state_queue.length);
892 tap_state_queue.length++;
899 void rlink_end_state(tap_state_t state)
901 if (tap_is_state_stable(state))
902 tap_set_end_state(state);
905 LOG_ERROR("BUG: %i is not a valid end state", state);
912 void rlink_state_move(void) {
915 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
916 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
918 for (i = 0; i < tms_count; i++)
920 tms = (tms_scan >> i) & 1;
921 tap_state_queue_append(tms);
924 tap_set_state(tap_get_end_state());
928 void rlink_path_move(struct pathmove_command *cmd)
930 int num_states = cmd->num_states;
937 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
941 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
947 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
951 tap_state_queue_append(tms);
953 tap_set_state(cmd->path[state_count]);
958 tap_set_end_state(tap_get_state());
963 void rlink_runtest(int num_cycles)
967 tap_state_t saved_end_state = tap_get_end_state();
969 /* only do a state_move when we're not already in RTI */
970 if (tap_get_state() != TAP_IDLE)
972 rlink_end_state(TAP_IDLE);
976 /* execute num_cycles */
977 for (i = 0; i < num_cycles; i++)
979 tap_state_queue_append(0);
982 /* finish in end_state */
983 rlink_end_state(saved_end_state);
984 if (tap_get_state() != tap_get_end_state())
989 /* (1) assert or (0) deassert reset lines */
991 void rlink_reset(int trst, int srst)
996 /* Read port A for bit op */
997 usb_err = ep1_generic_commandl(
1005 LOG_ERROR("%s", usb_strerror());
1009 usb_err = usb_bulk_read(
1010 pHDev, USB_EP1IN_ADDR,
1015 LOG_ERROR("%s", usb_strerror());
1020 bitmap &= ~ST7_PA_NTRST;
1022 bitmap |= ST7_PA_NTRST;
1025 /* Write port A and read port B for bit op */
1026 /* port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0 and assert NSRST by setting DDR to 1. */
1027 usb_err = ep1_generic_commandl(
1029 EP1_CMD_MEMORY_WRITE,
1034 EP1_CMD_MEMORY_READ,
1040 LOG_ERROR("%s", usb_strerror());
1044 usb_err = usb_bulk_read(
1045 pHDev, USB_EP1IN_ADDR,
1050 LOG_ERROR("%s", usb_strerror());
1055 bitmap |= ST7_PB_NSRST;
1057 bitmap &= ~ST7_PB_NSRST;
1060 /* write port B and read dummy to ensure completion before returning */
1061 usb_err = ep1_generic_commandl(
1063 EP1_CMD_MEMORY_WRITE,
1068 EP1_CMD_DTC_GET_CACHED_STATUS
1071 LOG_ERROR("%s", usb_strerror());
1075 usb_err = usb_bulk_read(
1076 pHDev, USB_EP1IN_ADDR,
1081 LOG_ERROR("%s", usb_strerror());
1090 struct jtag_command *cmd,
1091 enum scan_type type,
1096 tap_state_t saved_end_state;
1104 uint8_t tdi_mask, *tdi_p;
1107 if (scan_size < 1) {
1108 LOG_ERROR("scan_size cannot be less than 1 bit");
1112 ir_scan = cmd->cmd.scan->ir_scan;
1114 /* Move to the proper state before starting to shift TDI/TDO. */
1116 (!ir_scan && (tap_get_state() == TAP_DRSHIFT))
1118 (ir_scan && (tap_get_state() == TAP_IRSHIFT))
1120 saved_end_state = tap_get_end_state();
1121 rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1123 rlink_end_state(saved_end_state);
1126 tap_state_queue_run();
1130 printf("scan_size = %d, type = 0x%x\n", scan_size, type);
1134 /* clear unused bits in scan buffer for ease of debugging */
1135 /* (it makes diffing output easier) */
1136 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1138 printf("before scan:");
1139 for (i = 0; i < (scan_size + 7) / 8; i++) {
1140 printf(" %02x", buffer[i]);
1146 /* The number of bits that can be shifted as complete bytes */
1147 byte_bits = (int)(scan_size - 1) / 8 * 8;
1148 /* The number of bits left over, not counting the last bit */
1149 extra_bits = (scan_size - 1) - byte_bits;
1155 if (extra_bits && (type == SCAN_OUT)) {
1156 /* Schedule any extra bits into the DTC command buffer, padding as needed */
1157 /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */
1159 /* make sure there's room for two cmd bytes */
1160 dtc_queue_run_if_full(2, 0);
1163 dtc_mask = 1 << (extra_bits - 1);
1165 while (extra_bits--) {
1166 if (*tdi_p & tdi_mask) {
1173 if (tdi_mask == 0) {
1179 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1180 DTC_CMD_SHIFT_TDI_BYTES(1);
1182 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1185 /* Loop scheduling full bytes into the DTC command buffer */
1187 /* make sure there's room for one (for in scans) or two cmd bytes and
1188 * at least one reply byte for in or inout scans*/
1189 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
1191 chunk_bits = byte_bits;
1192 /* we can only use up to 16 bytes at a time */
1193 if (chunk_bits > (16 * 8)) chunk_bits = (16 * 8);
1195 if (type != SCAN_IN) {
1196 /* how much is there room for, considering stop and byte op? */
1197 x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1198 if (chunk_bits > x) chunk_bits = x;
1201 if (type != SCAN_OUT) {
1202 /* how much is there room for in the reply buffer? */
1203 x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1204 if (chunk_bits > x) chunk_bits = x;
1207 /* so the loop will end */
1208 byte_bits -= chunk_bits;
1210 if (type != SCAN_OUT) {
1211 if (dtc_queue_enqueue_reply(
1212 type, buffer, scan_size, tdi_bit_offset,
1216 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1219 dtc_queue.reply_index += (chunk_bits + 7) / 8;
1221 tdi_bit_offset += chunk_bits;
1224 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1225 chunk_bytes = chunk_bits / 8;
1229 x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1232 x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1235 x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1238 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1240 if (type != SCAN_IN) {
1242 dtc_mask = 1 << (8 - 1);
1244 while (chunk_bits--) {
1245 if (*tdi_p & tdi_mask) {
1250 if (dtc_mask == 0) {
1251 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1253 dtc_mask = 1 << (8 - 1);
1257 if (tdi_mask == 0) {
1265 if (extra_bits && (type != SCAN_OUT)) {
1266 /* Schedule any extra bits into the DTC command buffer */
1268 /* make sure there's room for one (for in scans) or two cmd bytes
1269 * and one reply byte */
1270 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
1272 if (dtc_queue_enqueue_reply(
1273 type, buffer, scan_size, tdi_bit_offset,
1277 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1281 dtc_queue.reply_index++;
1283 tdi_bit_offset += extra_bits;
1285 if (type == SCAN_IN) {
1286 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1287 DTC_CMD_SHIFT_TDO_BYTES(1);
1290 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1291 DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1294 dtc_mask = 1 << (8 - 1);
1296 while (extra_bits--) {
1297 if (*tdi_p & tdi_mask) {
1304 if (tdi_mask == 0) {
1310 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1314 /* Schedule the last bit into the DTC command buffer */
1316 /* make sure there's room for one cmd byte and one reply byte
1317 * for in or inout scans*/
1318 dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
1320 if (type == SCAN_OUT) {
1321 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1322 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1325 if (dtc_queue_enqueue_reply(
1326 type, buffer, scan_size, tdi_bit_offset,
1330 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1334 dtc_queue.reply_index++;
1336 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1337 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1340 /* Move to pause state */
1341 tap_state_queue_append(0);
1342 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1343 if (tap_get_state() != tap_get_end_state()) rlink_state_move();
1350 int rlink_execute_queue(void)
1352 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1354 enum scan_type type;
1356 int retval, tmp_retval;
1358 /* return ERROR_OK, unless something goes wrong */
1361 #ifndef AUTOMATIC_BUSY_LED
1363 ep1_generic_commandl(pHDev, 2,
1364 EP1_CMD_SET_PORTD_LEDS,
1374 case JTAG_TLR_RESET:
1380 /* some events, such as resets, need a queue flush to ensure consistency */
1381 tap_state_queue_run();
1389 #ifdef _DEBUG_JTAG_IO_
1390 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1392 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1394 tap_set_state(TAP_RESET);
1396 rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1399 #ifdef _DEBUG_JTAG_IO_
1400 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
1402 if (cmd->cmd.runtest->end_state != -1)
1403 rlink_end_state(cmd->cmd.runtest->end_state);
1404 rlink_runtest(cmd->cmd.runtest->num_cycles);
1406 case JTAG_TLR_RESET:
1407 #ifdef _DEBUG_JTAG_IO_
1408 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
1410 if (cmd->cmd.statemove->end_state != -1)
1411 rlink_end_state(cmd->cmd.statemove->end_state);
1415 #ifdef _DEBUG_JTAG_IO_
1416 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1418 rlink_path_move(cmd->cmd.pathmove);
1421 #ifdef _DEBUG_JTAG_IO_
1422 LOG_DEBUG("%s scan end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
1424 if (cmd->cmd.scan->end_state != -1)
1425 rlink_end_state(cmd->cmd.scan->end_state);
1426 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1427 type = jtag_scan_type(cmd->cmd.scan);
1428 if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK) {
1429 retval = ERROR_FAIL;
1433 #ifdef _DEBUG_JTAG_IO_
1434 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
1436 jtag_sleep(cmd->cmd.sleep->us);
1439 LOG_ERROR("BUG: unknown JTAG command type encountered");
1445 /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */
1446 tap_state_queue_run();
1447 tmp_retval = dtc_queue_run();
1448 if (tmp_retval != ERROR_OK) {
1449 retval = tmp_retval;
1452 #ifndef AUTOMATIC_BUSY_LED
1454 ep1_generic_commandl(pHDev, 2,
1455 EP1_CMD_SET_PORTD_LEDS,
1464 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1467 int rlink_speed(int speed)
1473 speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler;
1476 for (i = rlink_speed_table_size; i--;) {
1477 if (rlink_speed_table[i].prescaler == speed) {
1478 if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc, rlink_speed_table[i].dtc_size) != 0) {
1479 LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".", speed);
1483 if (dtc_start_download() < 0) {
1484 LOG_ERROR("starting DTC: %s", usb_strerror());
1492 LOG_ERROR("%d is not a supported speed", speed);
1498 int rlink_speed_div(
1504 for (i = rlink_speed_table_size; i--;) {
1505 if (rlink_speed_table[i].prescaler == speed) {
1506 *khz = rlink_speed_table[i].khz;
1511 LOG_ERROR("%d is not a supported speed", speed);
1524 LOG_ERROR("RCLK not supported");
1528 for (i = rlink_speed_table_size; i--;) {
1529 if (rlink_speed_table[i].khz <= khz) {
1530 *speed = rlink_speed_table[i].prescaler;
1535 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1536 *speed = rlink_speed_table[0].prescaler;
1542 int rlink_init(void)
1545 uint8_t reply_buffer[USB_EP1IN_SIZE];
1548 const uint16_t vids[] = { USB_IDVENDOR, 0 };
1549 const uint16_t pids[] = { USB_IDPRODUCT, 0 };
1550 if (jtag_usb_open(vids, pids, &pHDev) != ERROR_OK)
1553 struct usb_device *dev = usb_device(pHDev);
1554 if (dev->descriptor.bNumConfigurations > 1)
1556 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1559 if (dev->config->bNumInterfaces > 1)
1561 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1565 LOG_DEBUG("Opened device, pHDev = %p", pHDev);
1567 /* usb_set_configuration required under win32 */
1568 usb_set_configuration(pHDev, dev->config[0].bConfigurationValue);
1573 i = usb_claim_interface(pHDev,0);
1576 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1577 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1578 j = usb_detach_kernel_driver_np(pHDev, 0);
1580 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1585 LOG_DEBUG("interface claimed!");
1588 } while (--retries);
1592 LOG_ERROR("Initialisation failed.");
1595 if (usb_set_altinterface(pHDev,0) != 0)
1597 LOG_ERROR("Failed to set interface.");
1601 /* The device starts out in an unknown state on open. As such,
1602 * result reads time out, and it's not even known whether the
1603 * command was accepted. So, for this first command, we issue
1604 * it repeatedly until its response doesn't time out. Also, if
1605 * sending a command is going to time out, we find that out here.
1607 * It must be possible to open the device in such a way that
1608 * this special magic isn't needed, but, so far, it escapes us.
1610 for (i = 0; i < 5; i++) {
1611 j = ep1_generic_commandl(
1615 if (j < USB_EP1OUT_SIZE) {
1616 LOG_ERROR("USB write error: %s", usb_strerror());
1620 pHDev, USB_EP1IN_ADDR,
1621 (char *)reply_buffer, sizeof(reply_buffer),
1624 if (j != -ETIMEDOUT) break;
1627 if (j < (int)sizeof(reply_buffer)) {
1628 LOG_ERROR("USB read error: %s", usb_strerror());
1631 LOG_DEBUG(INTERFACE_NAME" firmware version: %d.%d.%d", reply_buffer[0], reply_buffer[1], reply_buffer[2]);
1633 if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3)) {
1634 LOG_WARNING("The rlink device is not of the version that the developers have played with. It may or may not work.");
1637 /* Probe port E for adapter presence */
1638 ep1_generic_commandl(
1640 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1645 ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1646 ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1647 EP1_CMD_MEMORY_READ, /* Read back */
1651 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1655 ST7_PE_ADAPTER_SENSE_OUT
1659 pHDev, USB_EP1IN_ADDR,
1660 (char *)reply_buffer, 1,
1664 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0) {
1665 LOG_WARNING("target detection problem");
1668 ep1_generic_commandl(
1670 EP1_CMD_MEMORY_READ, /* Read back */
1674 EP1_CMD_MEMORY_WRITE, /* float port E */
1684 pHDev, USB_EP1IN_ADDR,
1685 (char *)reply_buffer, 1,
1690 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0) {
1691 LOG_WARNING("target not plugged in");
1694 /* float ports A and B */
1695 ep1_generic_commandl(
1697 EP1_CMD_MEMORY_WRITE,
1703 EP1_CMD_MEMORY_WRITE,
1710 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1711 ep1_generic_commandl(
1714 EP1_CMD_SET_PORTD_VPP,
1716 EP1_CMD_MEMORY_WRITE,
1720 ((~(0)) & (ST7_PA_NTRST)),
1722 /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0 here and later assert NSRST by setting DDR bit to 1. */
1723 EP1_CMD_MEMORY_WRITE,
1730 /* set LED updating mode and make sure they're unlit */
1731 ep1_generic_commandl(
1733 #ifdef AUTOMATIC_BUSY_LED
1738 EP1_CMD_SET_PORTD_LEDS,
1742 tap_state_queue_init();
1751 int rlink_quit(void)
1753 /* stop DTC and make sure LEDs are off */
1754 ep1_generic_commandl(
1758 EP1_CMD_SET_PORTD_LEDS,
1760 EP1_CMD_SET_PORTD_VPP,
1764 usb_release_interface(pHDev,0);
1772 struct jtag_interface rlink_interface =
1777 .speed = rlink_speed,
1778 .speed_div = rlink_speed_div,
1780 .execute_queue = rlink_execute_queue,