rlink: simplify and optimize queue fill level checks
[openocd.git] / src / jtag / drivers / rlink.c
blobebdc6a986d2ea059c26d6474d91dcc89f1f2545c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 Rob Brown, Lou Deluxe *
9 * rob@cobbleware.com, lou.openocd012@fixit.nospammail.net *
10 * *
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. *
15 * *
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. *
20 * *
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 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 /* project specific includes */
31 #include <jtag/interface.h>
32 #include <jtag/commands.h>
33 #include "rlink.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.
110 static
112 ep1_generic_commandl(
113 usb_dev_handle *pHDev_param,
114 size_t length,
117 uint8_t usb_buffer[USB_EP1OUT_SIZE];
118 uint8_t *usb_buffer_p;
119 va_list ap;
120 int usb_ret;
122 if (length > sizeof(usb_buffer)) {
123 length = sizeof(usb_buffer);
126 usb_buffer_p = usb_buffer;
128 va_start(ap, length);
129 while (length > 0) {
130 *usb_buffer_p++ = va_arg(ap, int);
131 length--;
134 memset(
135 usb_buffer_p,
137 sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
140 usb_ret = usb_bulk_write(
141 pHDev_param,
142 USB_EP1OUT_ADDR,
143 (char *)usb_buffer, sizeof(usb_buffer),
144 USB_TIMEOUT_MS
147 return(usb_ret);
152 #if 0
153 static
154 ssize_t
155 ep1_memory_read(
156 usb_dev_handle *pHDev,
157 uint16_t addr,
158 size_t length,
159 uint8_t *buffer
161 uint8_t usb_buffer[USB_EP1OUT_SIZE];
162 int usb_ret;
163 size_t remain;
164 ssize_t count;
166 usb_buffer[0] = EP1_CMD_MEMORY_READ;
167 memset(
168 usb_buffer + 4,
170 sizeof(usb_buffer) - 4
173 remain = length;
174 count = 0;
176 while (remain) {
177 if (remain > sizeof(usb_buffer)) {
178 length = sizeof(usb_buffer);
179 } else {
180 length = remain;
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),
190 USB_TIMEOUT_MS
193 if (usb_ret < sizeof(usb_buffer)) {
194 break;
197 usb_ret = usb_bulk_read(
198 pHDev, USB_EP1IN_ADDR,
199 buffer, length,
200 USB_TIMEOUT_MS
203 if (usb_ret < length) {
204 break;
207 addr += length;
208 buffer += length;
209 count += length;
210 remain -= length;
213 return(count);
215 #endif
219 static
220 ssize_t
221 ep1_memory_write(
222 usb_dev_handle *pHDev_param,
223 uint16_t addr,
224 size_t length,
225 uint8_t const *buffer
227 uint8_t usb_buffer[USB_EP1OUT_SIZE];
228 int usb_ret;
229 size_t remain;
230 ssize_t count;
232 usb_buffer[0] = EP1_CMD_MEMORY_WRITE;
234 remain = length;
235 count = 0;
237 while (remain) {
238 if (remain > (sizeof(usb_buffer) - 4)) {
239 length = (sizeof(usb_buffer) - 4);
240 } else {
241 length = remain;
244 usb_buffer[1] = addr >> 8;
245 usb_buffer[2] = addr;
246 usb_buffer[3] = length;
247 memcpy(
248 usb_buffer + 4,
249 buffer,
250 length
252 memset(
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),
261 USB_TIMEOUT_MS
264 if ((size_t)usb_ret < sizeof(usb_buffer)) {
265 break;
268 addr += length;
269 buffer += length;
270 count += length;
271 remain -= length;
274 return(count);
278 #if 0
279 static
280 ssize_t
281 ep1_memory_writel(
282 usb_dev_handle *pHDev,
283 uint16_t addr,
284 size_t length,
287 uint8_t buffer[USB_EP1OUT_SIZE - 4];
288 uint8_t *buffer_p;
289 va_list ap;
290 size_t remain;
292 if (length > sizeof(buffer)) {
293 length = sizeof(buffer);
296 remain = length;
297 buffer_p = buffer;
299 va_start(ap, length);
300 while (remain > 0) {
301 *buffer_p++ = va_arg(ap, int);
302 remain--;
305 return(ep1_memory_write(pHDev, addr, length, buffer));
307 #endif
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. */
324 static
326 dtc_load_from_buffer(
327 usb_dev_handle *pHDev_param,
328 const uint8_t *buffer,
329 size_t length
331 struct header_s {
332 uint8_t type;
333 uint8_t length;
336 int usb_err;
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(
344 pHDev_param, 1,
345 EP1_CMD_DTC_STOP
347 if (usb_err < 0) return(usb_err);
349 while (length) {
350 if (length < sizeof(*header)) {
351 LOG_ERROR("Malformed DTC image");
352 exit(1);
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");
361 exit(1);
364 switch (header->type) {
365 case DTCLOAD_COMMENT:
366 break;
368 case DTCLOAD_ENTRY:
369 /* store entry addresses somewhere */
370 if (!strncmp("download", (char *)buffer + 1, 8)) {
371 dtc_entry_download = buffer[0];
373 break;
375 case DTCLOAD_LOAD:
376 /* Send the DTC program to ST7 RAM. */
377 usb_err = ep1_memory_write(
378 pHDev_param,
379 DTC_LOAD_BUFFER,
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(
386 pHDev_param, 3,
387 EP1_CMD_DTC_LOAD,
388 (DTC_LOAD_BUFFER >> 8),
389 DTC_LOAD_BUFFER
391 if (usb_err < 0) return(usb_err);
393 break;
395 case DTCLOAD_RUN:
396 usb_err = ep1_generic_commandl(
397 pHDev_param, 3,
398 EP1_CMD_DTC_CALL,
399 buffer[0],
400 EP1_CMD_DTC_WAIT
402 if (usb_err < 0) return(usb_err);
404 break;
406 case DTCLOAD_LUT_START:
407 lut_start = buffer[0];
408 break;
410 case DTCLOAD_LUT:
411 usb_err = ep1_memory_write(
412 pHDev_param,
413 ST7_USB_BUF_EP0OUT + lut_start,
414 header->length + 1, buffer
416 if (usb_err < 0) return(usb_err);
417 break;
419 default:
420 LOG_ERROR("Invalid DTC image record type: 0x%02x", header->type);
421 exit(1);
422 break;
425 buffer += (header->length + 1);
426 length -= (header->length + 1);
429 return(0);
434 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
436 static
438 dtc_start_download(void) {
439 int usb_err;
440 uint8_t ep2txr;
442 /* set up for download mode and make sure EP2 is set up to transmit */
443 usb_err = ep1_generic_commandl(
444 pHDev, 7,
446 EP1_CMD_DTC_STOP,
447 EP1_CMD_SET_UPLOAD,
448 EP1_CMD_SET_DOWNLOAD,
449 EP1_CMD_MEMORY_READ, /* read EP2TXR for its data toggle */
450 ST7_EP2TXR >> 8,
451 ST7_EP2TXR,
454 if (usb_err < 0) return(usb_err);
456 /* read back ep2txr */
457 usb_err = usb_bulk_read(
458 pHDev, USB_EP1IN_ADDR,
459 (char *)&ep2txr, 1,
460 USB_TIMEOUT_MS
462 if (usb_err < 0) return(usb_err);
464 usb_err = ep1_generic_commandl(
465 pHDev, 13,
467 EP1_CMD_MEMORY_WRITE, /* preinitialize poll byte */
468 DTC_STATUS_POLL_BYTE >> 8,
469 DTC_STATUS_POLL_BYTE,
471 0x00,
472 EP1_CMD_MEMORY_WRITE, /* set EP2IN to return data */
473 ST7_EP2TXR >> 8,
474 ST7_EP2TXR,
476 (ep2txr & ST7_EP2TXR_DTOG_TX) | ST7_EP2TXR_STAT_VALID,
477 EP1_CMD_DTC_CALL, /* start running the DTC */
478 dtc_entry_download,
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,
486 (char *)&ep2txr, 1,
487 USB_TIMEOUT_MS
490 return(usb_err);
494 static
496 dtc_run_download(
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
503 uint8_t ep2_buffer[USB_EP2IN_SIZE];
504 int usb_err;
505 int i;
507 LOG_DEBUG("%d/%d", command_buffer_size, reply_buffer_size);
509 usb_err = usb_bulk_write(
510 pHDev_param,
511 USB_EP2OUT_ADDR,
512 (char *)command_buffer, USB_EP2BANK_SIZE,
513 USB_TIMEOUT_MS
515 if (usb_err < 0) return(usb_err);
518 /* Wait for DTC to finish running command buffer */
519 for (i = 10;;) {
520 usb_err = ep1_generic_commandl(
521 pHDev_param, 4,
523 EP1_CMD_MEMORY_READ,
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(
531 pHDev_param,
532 USB_EP1IN_ADDR,
533 (char *)ep2_buffer, 1,
534 USB_TIMEOUT_MS
536 if (usb_err < 0) return(usb_err);
538 if (ep2_buffer[0] & 0x01) break;
540 if (!--i) {
541 LOG_ERROR("too many retries waiting for DTC status");
542 return(-ETIMEDOUT);
547 if (!reply_buffer) reply_buffer_size = 0;
548 if (reply_buffer_size) {
549 usb_err = usb_bulk_read(
550 pHDev_param,
551 USB_EP2IN_ADDR,
552 (char *)ep2_buffer, sizeof(ep2_buffer),
553 USB_TIMEOUT_MS
556 if (usb_err < (int)sizeof(ep2_buffer)) {
557 LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
558 usb_err, reply_buffer_size
560 return(usb_err);
563 memcpy(reply_buffer, ep2_buffer, reply_buffer_size);
567 return(usb_err);
572 * 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.
575 struct dtc_reply_queue_entry {
576 struct dtc_reply_queue_entry *next;
577 struct jtag_command *cmd; /* the command that resulted in this entry */
579 struct {
580 uint8_t *buffer; /* the scan buffer */
581 int size; /* size of the scan buffer in bits */
582 int offset; /* how many bits were already done before this? */
583 int length; /* how many bits are processed in this operation? */
584 enum scan_type type; /* SCAN_IN/SCAN_OUT/SCAN_IO */
585 } scan;
590 * The dtc_queue consists of a buffer of pending commands and a reply queue.
591 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
594 static
595 struct {
596 struct dtc_reply_queue_entry *rq_head;
597 struct dtc_reply_queue_entry *rq_tail;
598 uint32_t cmd_index;
599 uint32_t reply_index;
600 uint8_t cmd_buffer[USB_EP2BANK_SIZE];
601 } dtc_queue;
605 * 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.
608 static
609 struct {
610 uint32_t length;
611 uint32_t buffer;
612 } tap_state_queue;
616 static
618 dtc_queue_init(void) {
619 dtc_queue.rq_head = NULL;
620 dtc_queue.rq_tail = NULL;
621 dtc_queue.cmd_index = 0;
622 dtc_queue.reply_index = 0;
623 return(0);
627 static
628 inline
629 struct dtc_reply_queue_entry *
630 dtc_queue_enqueue_reply(
631 enum scan_type type,
632 uint8_t *buffer,
633 int size,
634 int offset,
635 int length,
636 struct jtag_command *cmd
638 struct dtc_reply_queue_entry *rq_entry;
640 rq_entry = malloc(sizeof(struct dtc_reply_queue_entry));
641 if (rq_entry != NULL) {
642 rq_entry->scan.type = type;
643 rq_entry->scan.buffer = buffer;
644 rq_entry->scan.size = size;
645 rq_entry->scan.offset = offset;
646 rq_entry->scan.length = length;
647 rq_entry->cmd = cmd;
648 rq_entry->next = NULL;
650 if (dtc_queue.rq_head == NULL)
651 dtc_queue.rq_head = rq_entry;
652 else
653 dtc_queue.rq_tail->next = rq_entry;
655 dtc_queue.rq_tail = rq_entry;
658 return(rq_entry);
663 * 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.
664 * The queue is automatically run by append when it is necessary to get space for the append.
667 static
669 dtc_queue_run(void) {
670 struct dtc_reply_queue_entry *rq_p, *rq_next;
671 int retval;
672 int usb_err;
673 int bit_cnt;
674 int x;
675 uint8_t *dtc_p, *tdo_p;
676 uint8_t dtc_mask, tdo_mask;
677 uint8_t reply_buffer[USB_EP2IN_SIZE];
679 assert((dtc_queue.rq_head != 0) == (dtc_queue.reply_index > 0));
680 assert(dtc_queue.cmd_index < USB_EP2BANK_SIZE);
681 assert(dtc_queue.reply_index <= USB_EP2IN_SIZE);
683 retval = ERROR_OK;
685 if (dtc_queue.cmd_index < 1) return(retval);
687 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = DTC_CMD_STOP;
689 usb_err = dtc_run_download(pHDev,
690 dtc_queue.cmd_buffer, dtc_queue.cmd_index,
691 reply_buffer, dtc_queue.reply_index
693 if (usb_err < 0) {
694 LOG_ERROR("dtc_run_download: %s", usb_strerror());
695 exit(1);
698 if (dtc_queue.rq_head != NULL) {
699 /* process the reply, which empties the reply queue and frees its entries */
700 dtc_p = reply_buffer;
702 /* 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. */
704 for (
705 rq_p = dtc_queue.rq_head;
706 rq_p != NULL;
707 rq_p = rq_next
709 tdo_p = rq_p->scan.buffer + (rq_p->scan.offset / 8);
710 tdo_mask = 1 << (rq_p->scan.offset % 8);
713 bit_cnt = rq_p->scan.length;
714 if (bit_cnt >= 8) {
715 /* bytes */
717 dtc_mask = 1 << (8 - 1);
719 for (
721 bit_cnt;
722 bit_cnt--
724 if (*dtc_p & dtc_mask) {
725 *tdo_p |= tdo_mask;
726 } else {
727 *tdo_p &=~ tdo_mask;
730 dtc_mask >>= 1;
731 if (dtc_mask == 0) {
732 dtc_p++;
733 dtc_mask = 1 << (8 - 1);
736 tdo_mask <<= 1;
737 if (tdo_mask == 0) {
738 tdo_p++;
739 tdo_mask = 1;
742 } else {
743 /* extra bits or last bit */
745 x = *dtc_p++;
746 if ((
747 rq_p->scan.type == SCAN_IN
748 ) && (
749 rq_p->scan.offset != rq_p->scan.size - 1
750 )) {
751 /* extra bits were sent as a full byte with padding on the end */
752 dtc_mask = 1 << (8 - 1);
753 } else {
754 dtc_mask = 1 << (bit_cnt - 1);
757 for (
759 bit_cnt;
760 bit_cnt--
762 if (x & dtc_mask) {
763 *tdo_p |= tdo_mask;
764 } else {
765 *tdo_p &=~ tdo_mask;
768 dtc_mask >>= 1;
770 tdo_mask <<= 1;
771 if (tdo_mask == 0) {
772 tdo_p++;
773 tdo_mask = 1;
779 if ((rq_p->scan.offset + rq_p->scan.length) >= rq_p->scan.size) {
780 /* feed scan buffer back into openocd and free it */
781 if (jtag_read_buffer(rq_p->scan.buffer, rq_p->cmd->cmd.scan) != ERROR_OK) {
782 retval = ERROR_JTAG_QUEUE_FAILED;
784 free(rq_p->scan.buffer);
787 rq_next = rq_p->next;
788 free(rq_p);
790 dtc_queue.rq_head = NULL;
791 dtc_queue.rq_tail = NULL;
795 /* reset state for new appends */
796 dtc_queue.cmd_index = 0;
797 dtc_queue.reply_index = 0;
799 return(retval);
802 /* runs the queue if it cannot take reserved_cmd bytes of command data
803 * or reserved_reply bytes of reply data */
804 static
806 dtc_queue_run_if_full(
807 int reserved_cmd,
808 int reserved_reply
810 /* reserve one additional byte for the STOP cmd appended during run */
811 if (dtc_queue.cmd_index + reserved_cmd + 1 > USB_EP2BANK_SIZE)
812 return dtc_queue_run();
814 if (dtc_queue.reply_index + reserved_reply > USB_EP2IN_SIZE)
815 return dtc_queue_run();
817 return ERROR_OK;
820 static
822 tap_state_queue_init(void) {
823 tap_state_queue.length = 0;
824 tap_state_queue.buffer = 0;
825 return(0);
829 static
831 tap_state_queue_run(void) {
832 int i;
833 int bits;
834 uint8_t byte_param;
835 int retval;
837 retval = 0;
838 if (!tap_state_queue.length) return(retval);
839 bits = 1;
840 byte_param = 0;
841 for (i = tap_state_queue.length; i--;) {
843 byte_param <<= 1;
844 if (tap_state_queue.buffer & 1) {
845 byte_param |= 1;
847 if ((bits >= 8) || !i) {
848 byte_param <<= (8 - bits);
850 /* make sure there's room for two cmd bytes */
851 dtc_queue_run_if_full(2, 0);
853 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
854 if (bits == 8) {
855 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
856 DTC_CMD_SHIFT_TMS_BYTES(1);
857 } else {
858 #endif
859 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
860 DTC_CMD_SHIFT_TMS_BITS(bits);
861 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
863 #endif
865 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
866 byte_param;
868 byte_param = 0;
869 bits = 1;
870 } else {
871 bits++;
874 tap_state_queue.buffer >>= 1;
876 retval = tap_state_queue_init();
877 return(retval);
881 static
883 tap_state_queue_append(
884 uint8_t tms
886 int retval;
888 if (tap_state_queue.length >= sizeof(tap_state_queue.buffer) * 8) {
889 retval = tap_state_queue_run();
890 if (retval != 0) return(retval);
893 if (tms) {
894 tap_state_queue.buffer |= (1 << tap_state_queue.length);
896 tap_state_queue.length++;
898 return(0);
902 static
903 void rlink_end_state(tap_state_t state)
905 if (tap_is_state_stable(state))
906 tap_set_end_state(state);
907 else
909 LOG_ERROR("BUG: %i is not a valid end state", state);
910 exit(-1);
915 static
916 void rlink_state_move(void) {
918 int i = 0, tms = 0;
919 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
920 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
922 for (i = 0; i < tms_count; i++)
924 tms = (tms_scan >> i) & 1;
925 tap_state_queue_append(tms);
928 tap_set_state(tap_get_end_state());
931 static
932 void rlink_path_move(struct pathmove_command *cmd)
934 int num_states = cmd->num_states;
935 int state_count;
936 int tms = 0;
938 state_count = 0;
939 while (num_states)
941 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
943 tms = 0;
945 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
947 tms = 1;
949 else
951 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
952 exit(-1);
955 tap_state_queue_append(tms);
957 tap_set_state(cmd->path[state_count]);
958 state_count++;
959 num_states--;
962 tap_set_end_state(tap_get_state());
966 static
967 void rlink_runtest(int num_cycles)
969 int i;
971 tap_state_t saved_end_state = tap_get_end_state();
973 /* only do a state_move when we're not already in RTI */
974 if (tap_get_state() != TAP_IDLE)
976 rlink_end_state(TAP_IDLE);
977 rlink_state_move();
980 /* execute num_cycles */
981 for (i = 0; i < num_cycles; i++)
983 tap_state_queue_append(0);
986 /* finish in end_state */
987 rlink_end_state(saved_end_state);
988 if (tap_get_state() != tap_get_end_state())
989 rlink_state_move();
993 /* (1) assert or (0) deassert reset lines */
994 static
995 void rlink_reset(int trst, int srst)
997 uint8_t bitmap;
998 int usb_err;
1000 /* Read port A for bit op */
1001 usb_err = ep1_generic_commandl(
1002 pHDev, 4,
1003 EP1_CMD_MEMORY_READ,
1004 ST7_PADR >> 8,
1005 ST7_PADR,
1008 if (usb_err < 0) {
1009 LOG_ERROR("%s", usb_strerror());
1010 exit(1);
1013 usb_err = usb_bulk_read(
1014 pHDev, USB_EP1IN_ADDR,
1015 (char *)&bitmap, 1,
1016 USB_TIMEOUT_MS
1018 if (usb_err < 1) {
1019 LOG_ERROR("%s", usb_strerror());
1020 exit(1);
1023 if (trst) {
1024 bitmap &= ~ST7_PA_NTRST;
1025 } else {
1026 bitmap |= ST7_PA_NTRST;
1029 /* Write port A and read port B for bit op */
1030 /* 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. */
1031 usb_err = ep1_generic_commandl(
1032 pHDev, 9,
1033 EP1_CMD_MEMORY_WRITE,
1034 ST7_PADR >> 8,
1035 ST7_PADR,
1037 bitmap,
1038 EP1_CMD_MEMORY_READ,
1039 ST7_PBDDR >> 8,
1040 ST7_PBDDR,
1043 if (usb_err < 0) {
1044 LOG_ERROR("%s", usb_strerror());
1045 exit(1);
1048 usb_err = usb_bulk_read(
1049 pHDev, USB_EP1IN_ADDR,
1050 (char *)&bitmap, 1,
1051 USB_TIMEOUT_MS
1053 if (usb_err < 1) {
1054 LOG_ERROR("%s", usb_strerror());
1055 exit(1);
1058 if (srst) {
1059 bitmap |= ST7_PB_NSRST;
1060 } else {
1061 bitmap &= ~ST7_PB_NSRST;
1064 /* write port B and read dummy to ensure completion before returning */
1065 usb_err = ep1_generic_commandl(
1066 pHDev, 6,
1067 EP1_CMD_MEMORY_WRITE,
1068 ST7_PBDDR >> 8,
1069 ST7_PBDDR,
1071 bitmap,
1072 EP1_CMD_DTC_GET_CACHED_STATUS
1074 if (usb_err < 0) {
1075 LOG_ERROR("%s", usb_strerror());
1076 exit(1);
1079 usb_err = usb_bulk_read(
1080 pHDev, USB_EP1IN_ADDR,
1081 (char *)&bitmap, 1,
1082 USB_TIMEOUT_MS
1084 if (usb_err < 1) {
1085 LOG_ERROR("%s", usb_strerror());
1086 exit(1);
1091 static
1093 rlink_scan(
1094 struct jtag_command *cmd,
1095 enum scan_type type,
1096 uint8_t *buffer,
1097 int scan_size
1099 bool ir_scan;
1100 tap_state_t saved_end_state;
1101 int byte_bits;
1102 int extra_bits;
1103 int chunk_bits;
1104 int chunk_bytes;
1105 int x;
1107 int tdi_bit_offset;
1108 uint8_t tdi_mask, *tdi_p;
1109 uint8_t dtc_mask;
1111 if (scan_size < 1) {
1112 LOG_ERROR("scan_size cannot be less than 1 bit");
1113 exit(1);
1116 ir_scan = cmd->cmd.scan->ir_scan;
1118 /* Move to the proper state before starting to shift TDI/TDO. */
1119 if (!(
1120 (!ir_scan && (tap_get_state() == TAP_DRSHIFT))
1122 (ir_scan && (tap_get_state() == TAP_IRSHIFT))
1123 )) {
1124 saved_end_state = tap_get_end_state();
1125 rlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
1126 rlink_state_move();
1127 rlink_end_state(saved_end_state);
1130 tap_state_queue_run();
1133 #if 0
1134 printf("scan_size = %d, type = 0x%x\n", scan_size, type);
1136 int i;
1138 /* clear unused bits in scan buffer for ease of debugging */
1139 /* (it makes diffing output easier) */
1140 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1142 printf("before scan:");
1143 for (i = 0; i < (scan_size + 7) / 8; i++) {
1144 printf(" %02x", buffer[i]);
1146 printf("\n");
1148 #endif
1150 /* The number of bits that can be shifted as complete bytes */
1151 byte_bits = (int)(scan_size - 1) / 8 * 8;
1152 /* The number of bits left over, not counting the last bit */
1153 extra_bits = (scan_size - 1) - byte_bits;
1155 tdi_bit_offset = 0;
1156 tdi_p = buffer;
1157 tdi_mask = 1;
1159 if (extra_bits && (type == SCAN_OUT)) {
1160 /* Schedule any extra bits into the DTC command buffer, padding as needed */
1161 /* For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will fall off the end */
1163 /* make sure there's room for two cmd bytes */
1164 dtc_queue_run_if_full(2, 0);
1166 x = 0;
1167 dtc_mask = 1 << (extra_bits - 1);
1169 while (extra_bits--) {
1170 if (*tdi_p & tdi_mask) {
1171 x |= dtc_mask;
1174 dtc_mask >>= 1;
1176 tdi_mask <<= 1;
1177 if (tdi_mask == 0) {
1178 tdi_p++;
1179 tdi_mask = 1;
1183 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1184 DTC_CMD_SHIFT_TDI_BYTES(1);
1186 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1189 /* Loop scheduling full bytes into the DTC command buffer */
1190 while (byte_bits) {
1191 /* make sure there's room for one (for in scans) or two cmd bytes and
1192 * at least one reply byte for in or inout scans*/
1193 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, type != SCAN_OUT ? 1 : 0);
1195 chunk_bits = byte_bits;
1196 /* we can only use up to 16 bytes at a time */
1197 if (chunk_bits > (16 * 8)) chunk_bits = (16 * 8);
1199 if (type != SCAN_IN) {
1200 /* how much is there room for, considering stop and byte op? */
1201 x = (sizeof(dtc_queue.cmd_buffer) - (dtc_queue.cmd_index + 1 + 1)) * 8;
1202 if (chunk_bits > x) chunk_bits = x;
1205 if (type != SCAN_OUT) {
1206 /* how much is there room for in the reply buffer? */
1207 x = (USB_EP2IN_SIZE - dtc_queue.reply_index) * 8;
1208 if (chunk_bits > x) chunk_bits = x;
1211 /* so the loop will end */
1212 byte_bits -= chunk_bits;
1214 if (type != SCAN_OUT) {
1215 if (dtc_queue_enqueue_reply(
1216 type, buffer, scan_size, tdi_bit_offset,
1217 chunk_bits,
1219 ) == NULL) {
1220 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1221 exit(1);
1223 dtc_queue.reply_index += (chunk_bits + 7) / 8;
1225 tdi_bit_offset += chunk_bits;
1228 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1229 chunk_bytes = chunk_bits / 8;
1231 switch (type) {
1232 case SCAN_IN:
1233 x = DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes);
1234 break;
1235 case SCAN_OUT:
1236 x = DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes);
1237 break;
1238 default:
1239 x = DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes);
1240 break;
1242 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1244 if (type != SCAN_IN) {
1245 x = 0;
1246 dtc_mask = 1 << (8 - 1);
1248 while (chunk_bits--) {
1249 if (*tdi_p & tdi_mask) {
1250 x |= dtc_mask;
1253 dtc_mask >>= 1;
1254 if (dtc_mask == 0) {
1255 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1256 x = 0;
1257 dtc_mask = 1 << (8 - 1);
1260 tdi_mask <<= 1;
1261 if (tdi_mask == 0) {
1262 tdi_p++;
1263 tdi_mask = 1;
1269 if (extra_bits && (type != SCAN_OUT)) {
1270 /* Schedule any extra bits into the DTC command buffer */
1272 /* make sure there's room for one (for in scans) or two cmd bytes
1273 * and one reply byte */
1274 dtc_queue_run_if_full(type == SCAN_IN ? 1 : 2, 1);
1276 if (dtc_queue_enqueue_reply(
1277 type, buffer, scan_size, tdi_bit_offset,
1278 extra_bits,
1280 ) == NULL) {
1281 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1282 exit(1);
1285 dtc_queue.reply_index++;
1287 tdi_bit_offset += extra_bits;
1289 if (type == SCAN_IN) {
1290 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1291 DTC_CMD_SHIFT_TDO_BYTES(1);
1293 } else {
1294 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1295 DTC_CMD_SHIFT_TDIO_BITS(extra_bits);
1297 x = 0;
1298 dtc_mask = 1 << (8 - 1);
1300 while (extra_bits--) {
1301 if (*tdi_p & tdi_mask) {
1302 x |= dtc_mask;
1305 dtc_mask >>= 1;
1307 tdi_mask <<= 1;
1308 if (tdi_mask == 0) {
1309 tdi_p++;
1310 tdi_mask = 1;
1314 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] = x;
1318 /* Schedule the last bit into the DTC command buffer */
1320 /* make sure there's room for one cmd byte and one reply byte
1321 * for in or inout scans*/
1322 dtc_queue_run_if_full(1, type == SCAN_OUT ? 0 : 1);
1324 if (type == SCAN_OUT) {
1325 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1326 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 0);
1328 } else {
1329 if (dtc_queue_enqueue_reply(
1330 type, buffer, scan_size, tdi_bit_offset,
1333 ) == NULL) {
1334 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno));
1335 exit(1);
1338 dtc_queue.reply_index++;
1340 dtc_queue.cmd_buffer[dtc_queue.cmd_index++] =
1341 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p & tdi_mask), 1);
1344 /* Move to pause state */
1345 tap_state_queue_append(0);
1346 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
1347 if (tap_get_state() != tap_get_end_state()) rlink_state_move();
1349 return(0);
1353 static
1354 int rlink_execute_queue(void)
1356 struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1357 int scan_size;
1358 enum scan_type type;
1359 uint8_t *buffer;
1360 int retval, tmp_retval;
1362 /* return ERROR_OK, unless something goes wrong */
1363 retval = ERROR_OK;
1365 #ifndef AUTOMATIC_BUSY_LED
1366 /* turn LED on */
1367 ep1_generic_commandl(pHDev, 2,
1368 EP1_CMD_SET_PORTD_LEDS,
1369 ~(ST7_PD_NBUSY_LED)
1371 #endif
1373 while (cmd)
1375 switch (cmd->type)
1377 case JTAG_RUNTEST:
1378 case JTAG_TLR_RESET:
1379 case JTAG_PATHMOVE:
1380 case JTAG_SCAN:
1381 break;
1383 default:
1384 /* some events, such as resets, need a queue flush to ensure consistency */
1385 tap_state_queue_run();
1386 dtc_queue_run();
1387 break;
1390 switch (cmd->type)
1392 case JTAG_RESET:
1393 #ifdef _DEBUG_JTAG_IO_
1394 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1395 #endif
1396 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1398 tap_set_state(TAP_RESET);
1400 rlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1401 break;
1402 case JTAG_RUNTEST:
1403 #ifdef _DEBUG_JTAG_IO_
1404 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
1405 #endif
1406 if (cmd->cmd.runtest->end_state != -1)
1407 rlink_end_state(cmd->cmd.runtest->end_state);
1408 rlink_runtest(cmd->cmd.runtest->num_cycles);
1409 break;
1410 case JTAG_TLR_RESET:
1411 #ifdef _DEBUG_JTAG_IO_
1412 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
1413 #endif
1414 if (cmd->cmd.statemove->end_state != -1)
1415 rlink_end_state(cmd->cmd.statemove->end_state);
1416 rlink_state_move();
1417 break;
1418 case JTAG_PATHMOVE:
1419 #ifdef _DEBUG_JTAG_IO_
1420 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1421 #endif
1422 rlink_path_move(cmd->cmd.pathmove);
1423 break;
1424 case JTAG_SCAN:
1425 #ifdef _DEBUG_JTAG_IO_
1426 LOG_DEBUG("%s scan end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
1427 #endif
1428 if (cmd->cmd.scan->end_state != -1)
1429 rlink_end_state(cmd->cmd.scan->end_state);
1430 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1431 type = jtag_scan_type(cmd->cmd.scan);
1432 if (rlink_scan(cmd, type, buffer, scan_size) != ERROR_OK) {
1433 retval = ERROR_FAIL;
1435 break;
1436 case JTAG_SLEEP:
1437 #ifdef _DEBUG_JTAG_IO_
1438 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
1439 #endif
1440 jtag_sleep(cmd->cmd.sleep->us);
1441 break;
1442 default:
1443 LOG_ERROR("BUG: unknown JTAG command type encountered");
1444 exit(-1);
1446 cmd = cmd->next;
1449 /* Flush the DTC queue to make sure any pending reads have been done before exiting this function */
1450 tap_state_queue_run();
1451 tmp_retval = dtc_queue_run();
1452 if (tmp_retval != ERROR_OK) {
1453 retval = tmp_retval;
1456 #ifndef AUTOMATIC_BUSY_LED
1457 /* turn LED onff */
1458 ep1_generic_commandl(pHDev, 2,
1459 EP1_CMD_SET_PORTD_LEDS,
1462 #endif
1464 return retval;
1468 /* 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. */
1470 static
1471 int rlink_speed(int speed)
1473 int i;
1475 if (speed == 0) {
1476 /* fastest speed */
1477 speed = rlink_speed_table[rlink_speed_table_size - 1].prescaler;
1480 for (i = rlink_speed_table_size; i--;) {
1481 if (rlink_speed_table[i].prescaler == speed) {
1482 if (dtc_load_from_buffer(pHDev, rlink_speed_table[i].dtc, rlink_speed_table[i].dtc_size) != 0) {
1483 LOG_ERROR("An error occurred while trying to load DTC code for speed \"%d\".", speed);
1484 exit(1);
1487 if (dtc_start_download() < 0) {
1488 LOG_ERROR("starting DTC: %s", usb_strerror());
1489 exit(1);
1492 return ERROR_OK;
1496 LOG_ERROR("%d is not a supported speed", speed);
1497 return(ERROR_FAIL);
1501 static
1502 int rlink_speed_div(
1503 int speed,
1504 int *khz
1506 int i;
1508 for (i = rlink_speed_table_size; i--;) {
1509 if (rlink_speed_table[i].prescaler == speed) {
1510 *khz = rlink_speed_table[i].khz;
1511 return(ERROR_OK);
1515 LOG_ERROR("%d is not a supported speed", speed);
1516 return(ERROR_FAIL);
1520 static
1521 int rlink_khz(
1522 int khz,
1523 int *speed
1525 int i;
1527 if (khz == 0) {
1528 LOG_ERROR("RCLK not supported");
1529 return ERROR_FAIL;
1532 for (i = rlink_speed_table_size; i--;) {
1533 if (rlink_speed_table[i].khz <= khz) {
1534 *speed = rlink_speed_table[i].prescaler;
1535 return(ERROR_OK);
1539 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table[0].khz);
1540 *speed = rlink_speed_table[0].prescaler;
1541 return(ERROR_OK);
1545 static
1546 int rlink_init(void)
1548 int i, j, retries;
1549 uint8_t reply_buffer[USB_EP1IN_SIZE];
1551 usb_init();
1552 const uint16_t vids[] = { USB_IDVENDOR, 0 };
1553 const uint16_t pids[] = { USB_IDPRODUCT, 0 };
1554 if (jtag_usb_open(vids, pids, &pHDev) != ERROR_OK)
1555 return ERROR_FAIL;
1557 struct usb_device *dev = usb_device(pHDev);
1558 if (dev->descriptor.bNumConfigurations > 1)
1560 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1561 return ERROR_FAIL;
1563 if (dev->config->bNumInterfaces > 1)
1565 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1566 return ERROR_FAIL;
1569 LOG_DEBUG("Opened device, pHDev = %p", pHDev);
1571 /* usb_set_configuration required under win32 */
1572 usb_set_configuration(pHDev, dev->config[0].bConfigurationValue);
1574 retries = 3;
1577 i = usb_claim_interface(pHDev,0);
1578 if (i)
1580 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1581 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1582 j = usb_detach_kernel_driver_np(pHDev, 0);
1583 if (j)
1584 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1585 #endif
1587 else
1589 LOG_DEBUG("interface claimed!");
1590 break;
1592 } while (--retries);
1594 if (i)
1596 LOG_ERROR("Initialisation failed.");
1597 return ERROR_FAIL;
1599 if (usb_set_altinterface(pHDev,0) != 0)
1601 LOG_ERROR("Failed to set interface.");
1602 return ERROR_FAIL;
1605 /* The device starts out in an unknown state on open. As such,
1606 * result reads time out, and it's not even known whether the
1607 * command was accepted. So, for this first command, we issue
1608 * it repeatedly until its response doesn't time out. Also, if
1609 * sending a command is going to time out, we find that out here.
1611 * It must be possible to open the device in such a way that
1612 * this special magic isn't needed, but, so far, it escapes us.
1614 for (i = 0; i < 5; i++) {
1615 j = ep1_generic_commandl(
1616 pHDev, 1,
1617 EP1_CMD_GET_FWREV
1619 if (j < USB_EP1OUT_SIZE) {
1620 LOG_ERROR("USB write error: %s", usb_strerror());
1621 return(ERROR_FAIL);
1623 j = usb_bulk_read(
1624 pHDev, USB_EP1IN_ADDR,
1625 (char *)reply_buffer, sizeof(reply_buffer),
1628 if (j != -ETIMEDOUT) break;
1631 if (j < (int)sizeof(reply_buffer)) {
1632 LOG_ERROR("USB read error: %s", usb_strerror());
1633 return(ERROR_FAIL);
1635 LOG_DEBUG(INTERFACE_NAME" firmware version: %d.%d.%d", reply_buffer[0], reply_buffer[1], reply_buffer[2]);
1637 if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3)) {
1638 LOG_WARNING("The rlink device is not of the version that the developers have played with. It may or may not work.");
1641 /* Probe port E for adapter presence */
1642 ep1_generic_commandl(
1643 pHDev, 16,
1644 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 0 */
1645 ST7_PEDR >> 8,
1646 ST7_PEDR,
1648 0x00, /* DR */
1649 ST7_PE_ADAPTER_SENSE_OUT, /* DDR */
1650 ST7_PE_ADAPTER_SENSE_OUT, /* OR */
1651 EP1_CMD_MEMORY_READ, /* Read back */
1652 ST7_PEDR >> 8,
1653 ST7_PEDR,
1655 EP1_CMD_MEMORY_WRITE, /* Drive sense pin with 1 */
1656 ST7_PEDR >> 8,
1657 ST7_PEDR,
1659 ST7_PE_ADAPTER_SENSE_OUT
1662 usb_bulk_read(
1663 pHDev, USB_EP1IN_ADDR,
1664 (char *)reply_buffer, 1,
1665 USB_TIMEOUT_MS
1668 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) != 0) {
1669 LOG_WARNING("target detection problem");
1672 ep1_generic_commandl(
1673 pHDev, 11,
1674 EP1_CMD_MEMORY_READ, /* Read back */
1675 ST7_PEDR >> 8,
1676 ST7_PEDR,
1678 EP1_CMD_MEMORY_WRITE, /* float port E */
1679 ST7_PEDR >> 8,
1680 ST7_PEDR,
1682 0x00, /* DR */
1683 0x00, /* DDR */
1684 0x00 /* OR */
1687 usb_bulk_read(
1688 pHDev, USB_EP1IN_ADDR,
1689 (char *)reply_buffer, 1,
1690 USB_TIMEOUT_MS
1694 if ((reply_buffer[0] & ST7_PE_ADAPTER_SENSE_IN) == 0) {
1695 LOG_WARNING("target not plugged in");
1698 /* float ports A and B */
1699 ep1_generic_commandl(
1700 pHDev, 11,
1701 EP1_CMD_MEMORY_WRITE,
1702 ST7_PADDR >> 8,
1703 ST7_PADDR,
1705 0x00,
1706 0x00,
1707 EP1_CMD_MEMORY_WRITE,
1708 ST7_PBDDR >> 8,
1709 ST7_PBDDR,
1711 0x00
1714 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1715 ep1_generic_commandl(
1716 pHDev, 14,
1717 EP1_CMD_DTC_STOP,
1718 EP1_CMD_SET_PORTD_VPP,
1719 ~(ST7_PD_VPP_SHDN),
1720 EP1_CMD_MEMORY_WRITE,
1721 ST7_PADR >> 8,
1722 ST7_PADR,
1724 ((~(0)) & (ST7_PA_NTRST)),
1725 (ST7_PA_NTRST),
1726 /* 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. */
1727 EP1_CMD_MEMORY_WRITE,
1728 ST7_PBDR >> 8,
1729 ST7_PBDR,
1731 0x00
1734 /* set LED updating mode and make sure they're unlit */
1735 ep1_generic_commandl(
1736 pHDev, 3,
1737 #ifdef AUTOMATIC_BUSY_LED
1738 EP1_CMD_LEDUE_BUSY,
1739 #else
1740 EP1_CMD_LEDUE_NONE,
1741 #endif
1742 EP1_CMD_SET_PORTD_LEDS,
1746 tap_state_queue_init();
1747 dtc_queue_init();
1748 rlink_reset(0, 0);
1750 return ERROR_OK;
1754 static
1755 int rlink_quit(void)
1757 /* stop DTC and make sure LEDs are off */
1758 ep1_generic_commandl(
1759 pHDev, 6,
1760 EP1_CMD_DTC_STOP,
1761 EP1_CMD_LEDUE_NONE,
1762 EP1_CMD_SET_PORTD_LEDS,
1764 EP1_CMD_SET_PORTD_VPP,
1768 usb_release_interface(pHDev,0);
1769 usb_close(pHDev);
1772 return ERROR_OK;
1776 struct jtag_interface rlink_interface =
1778 .name = "rlink",
1779 .init = rlink_init,
1780 .quit = rlink_quit,
1781 .speed = rlink_speed,
1782 .speed_div = rlink_speed_div,
1783 .khz = rlink_khz,
1784 .execute_queue = rlink_execute_queue,