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 */
42 /* 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. */
43 #undef AUTOMATIC_BUSY_LED
45 /* This feature may require derating the speed due to reduced hold time. */
46 #undef USE_HARDWARE_SHIFTER_FOR_TMS
49 #define INTERFACE_NAME "RLink"
51 #define USB_IDVENDOR (0x138e)
52 #define USB_IDPRODUCT (0x9000)
54 #define USB_EP1OUT_ADDR (0x01)
55 #define USB_EP1OUT_SIZE (16)
56 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
57 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
59 #define USB_EP2OUT_ADDR (0x02)
60 #define USB_EP2OUT_SIZE (64)
61 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
62 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
63 #define USB_EP2BANK_SIZE (512)
65 #define USB_TIMEOUT_MS (3 * 1000)
67 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
70 #define ST7_PD_NBUSY_LED ST7_PD0
71 #define ST7_PD_NRUN_LED ST7_PD1
72 /* low enables VPP at adapter header, high connects it to GND instead */
73 #define ST7_PD_VPP_SEL ST7_PD6
74 /* low: VPP = 12v, high: VPP <= 5v */
75 #define ST7_PD_VPP_SHDN ST7_PD7
77 /* These pins are connected together */
78 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
79 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
81 /* Symbolic mapping between port pins and numbered IO lines */
82 #define ST7_PA_IO1 ST7_PA1
83 #define ST7_PA_IO2 ST7_PA2
84 #define ST7_PA_IO4 ST7_PA4
85 #define ST7_PA_IO8 ST7_PA6
86 #define ST7_PA_IO10 ST7_PA7
87 #define ST7_PB_IO5 ST7_PB5
88 #define ST7_PC_IO9 ST7_PC1
89 #define ST7_PC_IO3 ST7_PC2
90 #define ST7_PC_IO7 ST7_PC3
91 #define ST7_PE_IO6 ST7_PE5
93 /* Symbolic mapping between numbered IO lines and adapter signals */
94 #define ST7_PA_RTCK ST7_PA_IO0
95 #define ST7_PA_NTRST ST7_PA_IO1
96 #define ST7_PC_TDI ST7_PC_IO3
97 #define ST7_PA_DBGRQ ST7_PA_IO4
98 #define ST7_PB_NSRST ST7_PB_IO5
99 #define ST7_PE_TMS ST7_PE_IO6
100 #define ST7_PC_TCK ST7_PC_IO7
101 #define ST7_PC_TDO ST7_PC_IO9
102 #define ST7_PA_DBGACK ST7_PA_IO10
104 static usb_dev_handle *pHDev;
108 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
109 * This function takes care of zeroing the unused bytes before sending the packet.
110 * Any reply packet is not handled by this function.
114 ep1_generic_commandl(
115 usb_dev_handle *pHDev,
119 uint8_t usb_buffer[USB_EP1OUT_SIZE];
120 uint8_t *usb_buffer_p;
124 if(length > sizeof(usb_buffer)) {
125 length = sizeof(usb_buffer);
128 usb_buffer_p = usb_buffer;
130 va_start(ap, length);
132 *usb_buffer_p++ = va_arg(ap, int);
139 sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
142 usb_ret = usb_bulk_write(
145 (char *)usb_buffer, sizeof(usb_buffer),
158 usb_dev_handle *pHDev,
163 uint8_t usb_buffer[USB_EP1OUT_SIZE];
168 usb_buffer[0] = EP1_CMD_MEMORY_READ;
172 sizeof(usb_buffer) - 4
179 if(remain > sizeof(usb_buffer)) {
180 length = sizeof(usb_buffer);
185 usb_buffer[1] = addr >> 8;
186 usb_buffer[2] = addr;
187 usb_buffer[3] = length;
189 usb_ret = usb_bulk_write(
190 pHDev, USB_EP1OUT_ADDR,
191 usb_buffer, sizeof(usb_buffer),
195 if(usb_ret < sizeof(usb_buffer)) {
199 usb_ret = usb_bulk_read(
200 pHDev, USB_EP1IN_ADDR,
205 if(usb_ret < length) {
224 usb_dev_handle *pHDev,
227 uint8_t const *buffer
229 uint8_t usb_buffer[USB_EP1OUT_SIZE];
234 usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
240 if(remain > (sizeof(usb_buffer) - 4)) {
241 length = (sizeof(usb_buffer) - 4);
246 usb_buffer[1] = addr >> 8;
247 usb_buffer[2] = addr;
248 usb_buffer[3] = length;
255 usb_buffer + 4 + length,
257 sizeof(usb_buffer) - 4 - length
260 usb_ret = usb_bulk_write(
261 pHDev, USB_EP1OUT_ADDR,
262 (char *)usb_buffer, sizeof(usb_buffer),
266 if((size_t)usb_ret < sizeof(usb_buffer)) {
284 usb_dev_handle *pHDev,
289 uint8_t buffer[USB_EP1OUT_SIZE - 4];
294 if(length > sizeof(buffer)) {
295 length = sizeof(buffer);
301 va_start(ap, length);
303 *buffer_p++ = va_arg(ap, int);
307 return(ep1_memory_write(pHDev, addr, length, buffer));
312 #define DTCLOAD_COMMENT (0)
313 #define DTCLOAD_ENTRY (1)
314 #define DTCLOAD_LOAD (2)
315 #define DTCLOAD_RUN (3)
316 #define DTCLOAD_LUT_START (4)
317 #define DTCLOAD_LUT (5)
319 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
321 /* This gets set by the DTC loader */
322 static uint8_t dtc_entry_download;
325 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
328 dtc_load_from_buffer(
329 usb_dev_handle *pHDev,
339 struct header_s *header;
342 dtc_entry_download = 0;
344 /* Stop the DTC before loading anything. */
345 usb_err = ep1_generic_commandl(
349 if(usb_err < 0) return(usb_err);
352 if(length < sizeof(*header)) {
353 LOG_ERROR("Malformed DTC image\n");
357 header = (struct header_s *)buffer;
358 buffer += sizeof(*header);
359 length -= sizeof(*header);
361 if(length < (size_t)header->length + 1) {
362 LOG_ERROR("Malformed DTC image\n");
366 switch(header->type) {
367 case DTCLOAD_COMMENT:
371 /* store entry addresses somewhere */
372 if(!strncmp("download", (char *)buffer + 1, 8)) {
373 dtc_entry_download = buffer[0];
378 /* Send the DTC program to ST7 RAM. */
379 usb_err = ep1_memory_write(
382 header->length + 1, buffer
384 if(usb_err < 0) return(usb_err);
386 /* Load it into the DTC. */
387 usb_err = ep1_generic_commandl(
390 (DTC_LOAD_BUFFER >> 8),
393 if(usb_err < 0) return(usb_err);
398 usb_err = ep1_generic_commandl(
404 if(usb_err < 0) return(usb_err);
408 case DTCLOAD_LUT_START:
409 lut_start = buffer[0];
413 usb_err = ep1_memory_write(
415 ST7_USB_BUF_EP0OUT + lut_start,
416 header->length + 1, buffer
418 if(usb_err < 0) return(usb_err);
422 LOG_ERROR("Invalid DTC image record type: 0x%02x\n", header->type);
427 buffer += (header->length + 1);
428 length -= (header->length + 1);
436 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
440 dtc_start_download(void) {
444 /* set up for download mode and make sure EP2 is set up to transmit */
445 usb_err = ep1_generic_commandl(
450 EP1_CMD_SET_DOWNLOAD,
451 EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
456 if(usb_err < 0) return(usb_err);
458 /* read back ep2txr */
459 usb_err = usb_bulk_read(
460 pHDev, USB_EP1IN_ADDR,
464 if(usb_err < 0) return(usb_err);
466 usb_err = ep1_generic_commandl(
469 EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
470 DTC_STATUS_POLL_BYTE >> 8,
471 DTC_STATUS_POLL_BYTE,
474 EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
478 (ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
479 EP1_CMD_DTC_CALL, /* start running the DTC */
481 EP1_CMD_DTC_GET_CACHED_STATUS
483 if(usb_err < 0) return(usb_err);
485 /* wait for completion */
486 usb_err = usb_bulk_read(
487 pHDev, USB_EP1IN_ADDR,
499 usb_dev_handle *pHDev,
501 int command_buffer_size,
503 int reply_buffer_size
505 u8 ep2_buffer[USB_EP2IN_SIZE];
509 LOG_DEBUG(": %d/%d\n", command_buffer_size, reply_buffer_size);
511 usb_err = usb_bulk_write(
514 (char *)command_buffer, USB_EP2BANK_SIZE,
517 if(usb_err < 0) return(usb_err);
520 /* Wait for DTC to finish running command buffer */
522 usb_err = ep1_generic_commandl(
526 DTC_STATUS_POLL_BYTE >> 8,
527 DTC_STATUS_POLL_BYTE,
530 if(usb_err < 0) return(usb_err);
532 usb_err = usb_bulk_read(
535 (char *)ep2_buffer, 1,
538 if(usb_err < 0) return(usb_err);
540 if(ep2_buffer[0] & 0x01) break;
543 LOG_ERROR("%s, %d: too many retries waiting for DTC status\n",
551 if(!reply_buffer) reply_buffer_size = 0;
552 if(reply_buffer_size) {
553 usb_err = usb_bulk_read(
556 (char *)ep2_buffer, sizeof(ep2_buffer),
560 if(usb_err < (int)sizeof(ep2_buffer)) {
561 LOG_ERROR("%s, %d: Read of endpoint 2 returned %d\n",
562 __FILE__, __LINE__, usb_err
567 memcpy(reply_buffer, ep2_buffer, reply_buffer_size);
576 * 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.
580 struct dtc_reply_queue_entry_s {
581 struct dtc_reply_queue_entry_s *next;
582 jtag_command_t *cmd; /* the command that resulted in this entry */
585 u8 *buffer; /* the scan buffer */
586 int size; /* size of the scan buffer in bits */
587 int offset; /* how many bits were already done before this? */
588 int length; /* how many bits are processed in this operation? */
589 enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
591 } dtc_reply_queue_entry_t;
595 * The dtc_queue consists of a buffer of pending commands and a reply queue.
596 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
601 dtc_reply_queue_entry_t *rq_head;
602 dtc_reply_queue_entry_t *rq_tail;
605 u8 cmd_buffer[USB_EP2BANK_SIZE];
610 * 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.
623 dtc_queue_init(void) {
624 dtc_queue.rq_head = NULL;
625 dtc_queue.rq_tail = NULL;
626 dtc_queue.cmd_index = 0;
627 dtc_queue.reply_index = 0;
634 dtc_reply_queue_entry_t *
635 dtc_queue_enqueue_reply(
643 dtc_reply_queue_entry_t *rq_entry;
645 rq_entry = malloc(sizeof(dtc_reply_queue_entry_t));
646 if(rq_entry != NULL) {
647 rq_entry->scan.type = type;
648 rq_entry->scan.buffer = buffer;
649 rq_entry->scan.size = size;
650 rq_entry->scan.offset = offset;
651 rq_entry->scan.length = length;
653 rq_entry->next = NULL;
655 if(dtc_queue.rq_head == NULL)
656 dtc_queue.rq_head = rq_entry;
658 dtc_queue.rq_tail->next = rq_entry;
660 dtc_queue.rq_tail = rq_entry;
668 * 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.
669 * The queue is automatically run by append when it is necessary to get space for the append.
674 dtc_queue_run(void) {
675 dtc_reply_queue_entry_t *rq_p, *rq_next;
681 u8 dtc_mask, tdo_mask;
682 u8 reply_buffer[USB_EP2IN_SIZE];
686 if(dtc_queue.cmd_index < 1) return(retval);
688 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
691 if(dtc_queue.rq_head == NULL) {
692 usb_err = dtc_run_download(pHDev,
693 dtc_queue.cmd_buffer, dtc_queue.cmd_index,
697 LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
701 usb_err = dtc_run_download(pHDev,
702 dtc_queue.cmd_buffer, dtc_queue.cmd_index,
703 reply_buffer, dtc_queue.reply_index
706 LOG_ERROR("dtc_run_download: %s\n", usb_strerror());
709 /* process the reply, which empties the reply queue and frees its entries */
710 dtc_p = reply_buffer;
712 /* 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. */
715 rq_p = dtc_queue.rq_head;
719 tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
720 tdo_mask = 1 << (rq_p->scan.offset % 8);
723 bit_cnt = rq_p->scan.length;
727 dtc_mask = 1 << (8 - 1);
734 if(*dtc_p & dtc_mask) {
743 dtc_mask = 1 << (8 - 1);
753 /* extra bits or last bit */
757 rq_p->scan.type == SCAN_IN
759 rq_p->scan.offset != rq_p->scan.size - 1
761 /* extra bits were sent as a full byte with padding on the end */
762 dtc_mask = 1 << (8 - 1);
764 dtc_mask = 1 << (bit_cnt - 1);
789 if((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
790 /* feed scan buffer back into openocd and free it */
791 if(jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) {
792 retval = ERROR_JTAG_QUEUE_FAILED;
794 free(rq_p->scan.buffer);
797 rq_next = rq_p->next;
800 dtc_queue.rq_head = NULL;
801 dtc_queue.rq_tail = NULL;
807 /* reset state for new appends */
808 dtc_queue.cmd_index = 0;
809 dtc_queue.reply_index = 0;
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) {
846 /* make sure there's room for stop, byte op, and one byte */
847 if(dtc_queue.cmd_index >= (sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))) {
848 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
853 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
855 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
856 DTC_CMD_SHIFT_TMS_BYTES(1);
859 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
860 DTC_CMD_SHIFT_TMS_BITS(bits);
861 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
865 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
874 tap_state_queue.buffer >>= 1;
876 retval = tap_state_queue_init();
883 tap_state_queue_append(
888 if(tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
889 retval = tap_state_queue_run();
890 if(retval != 0) return(retval);
894 tap_state_queue.buffer |= (1 << tap_state_queue.length);
896 tap_state_queue.length++;
903 void rlink_end_state(tap_state_t state)
905 if (tap_is_state_stable(state))
906 tap_set_end_state(state);
909 LOG_ERROR("BUG: %i is not a valid end state", state);
916 void rlink_state_move(void) {
919 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
921 for (i = 0; i < 7; i++)
923 tms = (tms_scan >> i) & 1;
924 tap_state_queue_append(tms);
927 tap_set_state(tap_get_end_state());
931 void rlink_path_move(pathmove_command_t *cmd)
933 int num_states = cmd->num_states;
940 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
944 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
950 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
954 tap_state_queue_append(tms);
956 tap_set_state(cmd->path[state_count]);
961 tap_set_end_state(tap_get_state());
966 void rlink_runtest(int num_cycles)
970 tap_state_t saved_end_state = tap_get_end_state();
972 /* only do a state_move when we're not already in RTI */
973 if (tap_get_state() != TAP_IDLE)
975 rlink_end_state(TAP_IDLE);
979 /* execute num_cycles */
980 for (i = 0; i < num_cycles; i++)
982 tap_state_queue_append(0);
985 /* finish in end_state */
986 rlink_end_state(saved_end_state);
987 if (tap_get_state() != tap_get_end_state())
992 /* (1) assert or (0) deassert reset lines */
994 void rlink_reset(int trst, int srst)
999 /* Read port A for bit op */
1000 usb_err = ep1_generic_commandl(
1002 EP1_CMD_MEMORY_READ,
1008 LOG_ERROR("%s", usb_strerror());
1012 usb_err = usb_bulk_read(
1013 pHDev, USB_EP1IN_ADDR,
1018 LOG_ERROR("%s", usb_strerror());
1023 bitmap &= ~ST7_PA_NTRST;
1025 bitmap |= ST7_PA_NTRST;
1028 /* Write port A and read port B for bit op */
1029 /* 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. */
1030 usb_err = ep1_generic_commandl(
1032 EP1_CMD_MEMORY_WRITE,
1037 EP1_CMD_MEMORY_READ,
1043 LOG_ERROR("%s", usb_strerror());
1047 usb_err = usb_bulk_read(
1048 pHDev, USB_EP1IN_ADDR,
1053 LOG_ERROR("%s", usb_strerror());
1058 bitmap |= ST7_PB_NSRST;
1060 bitmap &= ~ST7_PB_NSRST;
1063 /* write port B and read dummy to ensure completion before returning */
1064 usb_err = ep1_generic_commandl(
1066 EP1_CMD_MEMORY_WRITE,
1071 EP1_CMD_DTC_GET_CACHED_STATUS
1074 LOG_ERROR("%s", usb_strerror());
1078 usb_err = usb_bulk_read(
1079 pHDev, USB_EP1IN_ADDR,
1084 LOG_ERROR("%s", usb_strerror());
1093 jtag_command_t *cmd,
1094 enum scan_type type,
1099 tap_state_t saved_end_state;
1107 u8 tdi_mask, *tdi_p;
1111 LOG_ERROR("scan_size cannot be less than 1 bit\n");
1115 ir_scan = cmd->cmd.scan->ir_scan;
1117 /* Move to the proper state before starting to shift TDI/TDO. */
1119 (!ir_scan && (tap_get_state() == TAP_DRSHIFT))
1121 (ir_scan && (tap_get_state() == TAP_IRSHIFT))
1123 saved_end_state = tap_get_end_state();
1124 rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1126 rlink_end_state(saved_end_state);
1129 tap_state_queue_run();
1133 printf("scan_size = %d, type=0x%x\n", scan_size, type);
1137 /* clear unused bits in scan buffer for ease of debugging */
1138 /* (it makes diffing output easier) */
1139 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1141 printf("before scan:");
1142 for(i = 0; i < (scan_size + 7) / 8; i++) {
1143 printf(" %02x", buffer[i]);
1149 /* The number of bits that can be shifted as complete bytes */
1150 byte_bits = (int)(scan_size - 1) / 8 * 8;
1151 /* The number of bits left over, not counting the last bit */
1152 extra_bits = (scan_size - 1) - byte_bits;
1158 if(extra_bits && (type == SCAN_OUT)) {
1159 /* Schedule any extra bits into the DTC command buffer, padding as needed */
1160 /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */
1161 /* make sure there's room for stop, byte op, and one byte */
1163 (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))
1169 dtc_mask = 1 << (extra_bits - 1);
1171 while(extra_bits--) {
1172 if(*tdi_p & tdi_mask) {
1185 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1186 DTC_CMD_SHIFT_TDI_BYTES(1);
1188 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1191 /* Loop scheduling full bytes into the DTC command buffer */
1193 if(type == SCAN_IN) {
1194 /* make sure there's room for stop and byte op */
1195 x = (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1));
1197 /* make sure there's room for stop, byte op, and at least one byte */
1198 x = (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1));
1201 if(type != SCAN_OUT) {
1202 /* make sure there's room for at least one reply byte */
1203 x |= (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1));
1210 chunk_bits = byte_bits;
1211 /* we can only use up to 16 bytes at a time */
1212 if(chunk_bits > (16 * 8)) chunk_bits = (16 * 8);
1214 if(type != SCAN_IN) {
1215 /* how much is there room for, considering stop and byte op? */
1216 x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1217 if(chunk_bits > x) chunk_bits = x;
1220 if(type != SCAN_OUT) {
1221 /* how much is there room for in the reply buffer? */
1222 x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1223 if(chunk_bits > x) chunk_bits = x;
1226 /* so the loop will end */
1227 byte_bits -= chunk_bits;
1229 if(type != SCAN_OUT) {
1230 if(dtc_queue_enqueue_reply(
1231 type, buffer, scan_size, tdi_bit_offset,
1235 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
1239 tdi_bit_offset += chunk_bits;
1242 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1243 chunk_bytes = chunk_bits / 8;
1247 x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1250 x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1253 x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1256 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1258 if(type != SCAN_IN) {
1260 dtc_mask = 1 << (8 - 1);
1262 while(chunk_bits--) {
1263 if(*tdi_p & tdi_mask) {
1269 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1270 dtc_queue.reply_index++;
1272 dtc_mask = 1 << (8 - 1);
1284 if(extra_bits && (type != SCAN_OUT)) {
1285 /* Schedule any extra bits into the DTC command buffer */
1286 /* make sure there's room for stop, byte op, and one byte */
1288 (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1 + 1))
1290 (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1))
1295 if(dtc_queue_enqueue_reply(
1296 type, buffer, scan_size, tdi_bit_offset,
1300 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
1304 tdi_bit_offset += extra_bits;
1306 if(type == SCAN_IN) {
1307 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1308 DTC_CMD_SHIFT_TDO_BYTES(1);
1311 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1312 DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1315 dtc_mask = 1 << (8 - 1);
1317 while(extra_bits--) {
1318 if(*tdi_p & tdi_mask) {
1331 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1334 dtc_queue.reply_index++;
1337 /* Schedule the last bit into the DTC command buffer */
1339 /* make sure there's room for stop, and bit pair command */
1341 (dtc_queue.cmd_index >= sizeof(dtc_queue.cmd_buffer) - (1 + 1))
1343 (dtc_queue.reply_index >= USB_EP2IN_SIZE - (1))
1348 if(type == SCAN_OUT) {
1349 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1350 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1353 if(dtc_queue_enqueue_reply(
1354 type, buffer, scan_size, tdi_bit_offset,
1358 LOG_ERROR("enqueuing DTC reply entry: %s\n", strerror(errno));
1362 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1363 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1365 dtc_queue.reply_index++;
1369 /* Move to pause state */
1370 tap_state_queue_append(0);
1371 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1372 if (tap_get_state() != tap_get_end_state()) rlink_state_move();
1379 int rlink_execute_queue(void)
1381 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
1383 enum scan_type type;
1385 int retval, tmp_retval;
1387 /* return ERROR_OK, unless something goes wrong */
1390 #ifndef AUTOMATIC_BUSY_LED
1392 ep1_generic_commandl(pHDev, 2,
1393 EP1_CMD_SET_PORTD_LEDS,
1402 case JTAG_END_STATE:
1404 case JTAG_STATEMOVE:
1410 /* some events, such as resets, need a queue flush to ensure consistency */
1411 tap_state_queue_run();
1418 case JTAG_END_STATE:
1419 #ifdef _DEBUG_JTAG_IO_
1420 LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
1422 if (cmd->cmd.end_state->end_state != -1)
1423 rlink_end_state(cmd->cmd.end_state->end_state);
1426 #ifdef _DEBUG_JTAG_IO_
1427 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1429 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
1431 tap_set_state(TAP_RESET);
1433 rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1436 #ifdef _DEBUG_JTAG_IO_
1437 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
1439 if (cmd->cmd.runtest->end_state != -1)
1440 rlink_end_state(cmd->cmd.runtest->end_state);
1441 rlink_runtest(cmd->cmd.runtest->num_cycles);
1443 case JTAG_STATEMOVE:
1444 #ifdef _DEBUG_JTAG_IO_
1445 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
1447 if (cmd->cmd.statemove->end_state != -1)
1448 rlink_end_state(cmd->cmd.statemove->end_state);
1452 #ifdef _DEBUG_JTAG_IO_
1453 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1455 rlink_path_move(cmd->cmd.pathmove);
1458 #ifdef _DEBUG_JTAG_IO_
1459 LOG_DEBUG("%s scan end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
1461 if (cmd->cmd.scan->end_state != -1)
1462 rlink_end_state(cmd->cmd.scan->end_state);
1463 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1464 type = jtag_scan_type(cmd->cmd.scan);
1465 if(rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK) {
1466 retval = ERROR_FAIL;
1470 #ifdef _DEBUG_JTAG_IO_
1471 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
1473 jtag_sleep(cmd->cmd.sleep->us);
1476 LOG_ERROR("BUG: unknown JTAG command type encountered");
1482 /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */
1483 tap_state_queue_run();
1484 tmp_retval = dtc_queue_run();
1485 if(tmp_retval != ERROR_OK) {
1486 retval = tmp_retval;
1489 #ifndef AUTOMATIC_BUSY_LED
1491 ep1_generic_commandl(pHDev, 2,
1492 EP1_CMD_SET_PORTD_LEDS,
1501 /* 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. */
1504 int rlink_speed(int speed)
1510 speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler;
1513 for(i = rlink_speed_table_size; i--; ) {
1514 if(rlink_speed_table[i].prescaler == speed) {
1515 if(dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc, rlink_speed_table[i].dtc_size) != 0) {
1516 LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".\n", speed);
1520 if(dtc_start_download() < 0) {
1521 LOG_ERROR("%s, %d: starting DTC: %s",
1532 LOG_ERROR("%d is not a supported speed", speed);
1538 int rlink_speed_div(
1544 for(i = rlink_speed_table_size; i--; ) {
1545 if(rlink_speed_table[i].prescaler == speed) {
1546 *khz = rlink_speed_table[i].khz;
1551 LOG_ERROR("%d is not a supported speed", speed);
1564 LOG_ERROR("RCLK not supported");
1568 for(i = rlink_speed_table_size; i--; ) {
1569 if(rlink_speed_table[i].khz <= khz) {
1570 *speed = rlink_speed_table[i].prescaler;
1575 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1576 *speed = rlink_speed_table[0].prescaler;
1584 handle_dtc_directory_command(
1585 struct command_context_s *cmd_ctx,
1591 LOG_ERROR("expected exactly one argument to rlink_dtc_directory <directory-path>");
1592 return(ERROR_INVALID_ARGUMENTS);
1595 printf("handle_dtc_directory_command called with \"%s\"\n", args[0]);
1603 int rlink_register_commands(struct command_context_s *cmd_ctx)
1606 #ifdef _DEBUG_JTAG_IO_
1607 LOG_DEBUG("rlink_register_commands called with cmd_ctx=%p\n", cmd_ctx);
1613 "rlink_dtc_directory",
1614 handle_dtc_directory_command,
1616 "The directory in which to search for DTC load images"
1625 int rlink_init(void)
1627 struct usb_bus *busses;
1628 struct usb_bus *bus;
1632 u8 reply_buffer[USB_EP1IN_SIZE];
1638 busses = usb_get_busses();
1640 for(bus = busses; bus; bus = bus->next)
1642 struct usb_device *dev;
1644 for(dev = bus->devices; dev; dev = dev->next)
1646 if( (dev->descriptor.idVendor == USB_IDVENDOR) && (dev->descriptor.idProduct == USB_IDPRODUCT) )
1649 LOG_DEBUG("Found device on bus.\n");
1653 if( dev->descriptor.bNumConfigurations > 1 )
1655 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...\n");
1658 if( dev->config->bNumInterfaces > 1 )
1660 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...\n");
1664 pHDev=usb_open(dev);
1666 LOG_ERROR ("Failed to open device.\n");
1669 LOG_DEBUG("Opened device, pHDev = %p\n",pHDev);
1671 /* usb_set_configuration required under win32 */
1672 usb_set_configuration(pHDev, dev->config[0].bConfigurationValue);
1677 i = usb_claim_interface(pHDev,0);
1680 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1681 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1682 j = usb_detach_kernel_driver_np(pHDev, 0);
1684 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1689 LOG_DEBUG("interface claimed!\n");
1696 if( usb_set_altinterface(pHDev,0) )
1698 LOG_ERROR("Failed to set interface.\n");
1712 LOG_ERROR("No device found on bus.\n");
1718 LOG_ERROR("Initialisation failed.");
1723 /* The device starts out in an unknown state on open. As such, result reads time out, and it's not even known whether the command was accepted. So, for this first command, we issue it repeatedly until its response doesn't time out. Also, if sending a command is going to time out, we'll find that out here. */
1724 /* It must be possible to open the device in such a way that this special magic isn't needed, but, so far, it escapes us. */
1725 for(i = 0; i < 5; i++) {
1726 j = ep1_generic_commandl(
1730 if(j < USB_EP1OUT_SIZE) {
1731 LOG_ERROR("USB write error: %s", usb_strerror());
1735 pHDev, USB_EP1IN_ADDR,
1736 (char *)reply_buffer, sizeof(reply_buffer),
1739 if(j != -ETIMEDOUT) break;
1742 if(j < (int)sizeof(reply_buffer)) {
1743 LOG_ERROR("USB read error: %s", usb_strerror());
1746 LOG_DEBUG(INTERFACE_NAME" firmware version: %d.%d.%d\n", reply_buffer[0], reply_buffer[1], reply_buffer[2]);
1748 if((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3)) {
1749 LOG_WARNING("The rlink device is not of the version that the developers have played with. It may or may not work.\n");
1752 /* Probe port E for adapter presence */
1753 ep1_generic_commandl(
1755 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1760 ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1761 ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1762 EP1_CMD_MEMORY_READ, /* Read back */
1766 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1770 ST7_PE_ADAPTER_SENSE_OUT
1774 pHDev, USB_EP1IN_ADDR,
1775 (char *)reply_buffer, 1,
1779 if((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0) {
1780 LOG_WARNING("target detection problem\n");
1783 ep1_generic_commandl(
1785 EP1_CMD_MEMORY_READ, /* Read back */
1789 EP1_CMD_MEMORY_WRITE, /* float port E */
1799 pHDev, USB_EP1IN_ADDR,
1800 (char *)reply_buffer, 1,
1805 if((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0) {
1806 LOG_WARNING("target not plugged in\n");
1809 /* float ports A and B */
1810 ep1_generic_commandl(
1812 EP1_CMD_MEMORY_WRITE,
1818 EP1_CMD_MEMORY_WRITE,
1825 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1826 ep1_generic_commandl(
1829 EP1_CMD_SET_PORTD_VPP,
1831 EP1_CMD_MEMORY_WRITE,
1835 ((~(0)) & (ST7_PA_NTRST)),
1837 /* 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. */
1838 EP1_CMD_MEMORY_WRITE,
1845 /* set LED updating mode and make sure they're unlit */
1846 ep1_generic_commandl(
1848 #ifdef AUTOMATIC_BUSY_LED
1853 EP1_CMD_SET_PORTD_LEDS,
1857 tap_state_queue_init();
1859 rlink_speed(jtag_speed);
1867 int rlink_quit(void)
1869 /* stop DTC and make sure LEDs are off */
1870 ep1_generic_commandl(
1874 EP1_CMD_SET_PORTD_LEDS,
1876 EP1_CMD_SET_PORTD_VPP,
1880 usb_release_interface(pHDev,0);
1888 jtag_interface_t rlink_interface =
1893 .register_commands = rlink_register_commands,
1894 .speed = rlink_speed,
1895 .speed_div = rlink_speed_div,
1897 .execute_queue = rlink_execute_queue,