1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 Rob Brown, Lou Deluxe *
9 * rob@cobbleware.com, lou.openocd012@fixit.nospammail.net *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
29 /* project specific includes */
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
33 #include "rlink_st7.h"
34 #include "rlink_ep1_cmd.h"
35 #include "rlink_dtc_cmd.h"
36 #include "usb_common.h"
38 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on
39 *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
40 #undef AUTOMATIC_BUSY_LED
42 /* This feature may require derating the speed due to reduced hold time. */
43 #undef USE_HARDWARE_SHIFTER_FOR_TMS
45 #define INTERFACE_NAME "RLink"
47 #define USB_IDVENDOR (0x138e)
48 #define USB_IDPRODUCT (0x9000)
50 #define USB_EP1OUT_ADDR (0x01)
51 #define USB_EP1OUT_SIZE (16)
52 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
53 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
55 #define USB_EP2OUT_ADDR (0x02)
56 #define USB_EP2OUT_SIZE (64)
57 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
58 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
59 #define USB_EP2BANK_SIZE (512)
61 #define USB_TIMEOUT_MS (3 * 1000)
63 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
65 #define ST7_PD_NBUSY_LED ST7_PD0
66 #define ST7_PD_NRUN_LED ST7_PD1
67 /* low enables VPP at adapter header, high connects it to GND instead */
68 #define ST7_PD_VPP_SEL ST7_PD6
69 /* low: VPP = 12v, high: VPP <= 5v */
70 #define ST7_PD_VPP_SHDN ST7_PD7
72 /* These pins are connected together */
73 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
74 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
76 /* Symbolic mapping between port pins and numbered IO lines */
77 #define ST7_PA_IO1 ST7_PA1
78 #define ST7_PA_IO2 ST7_PA2
79 #define ST7_PA_IO4 ST7_PA4
80 #define ST7_PA_IO8 ST7_PA6
81 #define ST7_PA_IO10 ST7_PA7
82 #define ST7_PB_IO5 ST7_PB5
83 #define ST7_PC_IO9 ST7_PC1
84 #define ST7_PC_IO3 ST7_PC2
85 #define ST7_PC_IO7 ST7_PC3
86 #define ST7_PE_IO6 ST7_PE5
88 /* Symbolic mapping between numbered IO lines and adapter signals */
89 #define ST7_PA_RTCK ST7_PA_IO0
90 #define ST7_PA_NTRST ST7_PA_IO1
91 #define ST7_PC_TDI ST7_PC_IO3
92 #define ST7_PA_DBGRQ ST7_PA_IO4
93 #define ST7_PB_NSRST ST7_PB_IO5
94 #define ST7_PE_TMS ST7_PE_IO6
95 #define ST7_PC_TCK ST7_PC_IO7
96 #define ST7_PC_TDO ST7_PC_IO9
97 #define ST7_PA_DBGACK ST7_PA_IO10
99 static usb_dev_handle
*pHDev
;
102 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
103 * This function takes care of zeroing the unused bytes before sending the packet.
104 * Any reply packet is not handled by this function.
106 static int ep1_generic_commandl(usb_dev_handle
*pHDev_param
, size_t length
, ...)
108 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
109 uint8_t *usb_buffer_p
;
113 if (length
> sizeof(usb_buffer
))
114 length
= sizeof(usb_buffer
);
116 usb_buffer_p
= usb_buffer
;
118 va_start(ap
, length
);
120 *usb_buffer_p
++ = va_arg(ap
, int);
127 sizeof(usb_buffer
) - (usb_buffer_p
- usb_buffer
)
130 usb_ret
= usb_bulk_write(
133 (char *)usb_buffer
, sizeof(usb_buffer
),
141 static ssize_t
ep1_memory_read(
142 usb_dev_handle
*pHDev
, uint16_t addr
,
143 size_t length
, uint8_t *buffer
)
145 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
150 usb_buffer
[0] = EP1_CMD_MEMORY_READ
;
154 sizeof(usb_buffer
) - 4
161 if (remain
> sizeof(usb_buffer
))
162 length
= sizeof(usb_buffer
);
166 usb_buffer
[1] = addr
>> 8;
167 usb_buffer
[2] = addr
;
168 usb_buffer
[3] = length
;
170 usb_ret
= usb_bulk_write(
171 pHDev
, USB_EP1OUT_ADDR
,
172 usb_buffer
, sizeof(usb_buffer
),
176 if (usb_ret
< sizeof(usb_buffer
))
179 usb_ret
= usb_bulk_read(
180 pHDev
, USB_EP1IN_ADDR
,
185 if (usb_ret
< length
)
198 static ssize_t
ep1_memory_write(usb_dev_handle
*pHDev_param
, uint16_t addr
,
199 size_t length
, uint8_t const *buffer
)
201 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
206 usb_buffer
[0] = EP1_CMD_MEMORY_WRITE
;
212 if (remain
> (sizeof(usb_buffer
) - 4))
213 length
= (sizeof(usb_buffer
) - 4);
217 usb_buffer
[1] = addr
>> 8;
218 usb_buffer
[2] = addr
;
219 usb_buffer
[3] = length
;
226 usb_buffer
+ 4 + length
,
228 sizeof(usb_buffer
) - 4 - length
231 usb_ret
= usb_bulk_write(
232 pHDev_param
, USB_EP1OUT_ADDR
,
233 (char *)usb_buffer
, sizeof(usb_buffer
),
237 if ((size_t)usb_ret
< sizeof(usb_buffer
))
251 static ssize_t
ep1_memory_writel(usb_dev_handle
*pHDev
, uint16_t addr
,
254 uint8_t buffer
[USB_EP1OUT_SIZE
- 4];
259 if (length
> sizeof(buffer
))
260 length
= sizeof(buffer
);
265 va_start(ap
, length
);
267 *buffer_p
++ = va_arg(ap
, int);
271 return ep1_memory_write(pHDev
, addr
, length
, buffer
);
275 #define DTCLOAD_COMMENT (0)
276 #define DTCLOAD_ENTRY (1)
277 #define DTCLOAD_LOAD (2)
278 #define DTCLOAD_RUN (3)
279 #define DTCLOAD_LUT_START (4)
280 #define DTCLOAD_LUT (5)
282 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
284 /* This gets set by the DTC loader */
285 static uint8_t dtc_entry_download
;
287 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
288 static int dtc_load_from_buffer(usb_dev_handle
*pHDev_param
, const uint8_t *buffer
,
297 struct header_s
*header
;
298 uint8_t lut_start
= 0xc0;
300 dtc_entry_download
= 0;
302 /* Stop the DTC before loading anything. */
303 usb_err
= ep1_generic_commandl(
311 if (length
< sizeof(*header
)) {
312 LOG_ERROR("Malformed DTC image");
316 header
= (struct header_s
*)buffer
;
317 buffer
+= sizeof(*header
);
318 length
-= sizeof(*header
);
320 if (length
< (size_t)header
->length
+ 1) {
321 LOG_ERROR("Malformed DTC image");
325 switch (header
->type
) {
326 case DTCLOAD_COMMENT
:
330 /* store entry addresses somewhere */
331 if (!strncmp("download", (char *)buffer
+ 1, 8))
332 dtc_entry_download
= buffer
[0];
336 /* Send the DTC program to ST7 RAM. */
337 usb_err
= ep1_memory_write(
340 header
->length
+ 1, buffer
345 /* Load it into the DTC. */
346 usb_err
= ep1_generic_commandl(
349 (DTC_LOAD_BUFFER
>> 8),
358 usb_err
= ep1_generic_commandl(
369 case DTCLOAD_LUT_START
:
370 lut_start
= buffer
[0];
374 usb_err
= ep1_memory_write(
376 ST7_USB_BUF_EP0OUT
+ lut_start
,
377 header
->length
+ 1, buffer
384 LOG_ERROR("Invalid DTC image record type: 0x%02x", header
->type
);
389 buffer
+= (header
->length
+ 1);
390 length
-= (header
->length
+ 1);
397 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
399 static int dtc_start_download(void)
404 /* set up for download mode and make sure EP2 is set up to transmit */
405 usb_err
= ep1_generic_commandl(
410 EP1_CMD_SET_DOWNLOAD
,
411 EP1_CMD_MEMORY_READ
, /* read EP2TXR for its data toggle */
419 /* read back ep2txr */
420 usb_err
= usb_bulk_read(
421 pHDev
, USB_EP1IN_ADDR
,
428 usb_err
= ep1_generic_commandl(
431 EP1_CMD_MEMORY_WRITE
, /* preinitialize poll byte */
432 DTC_STATUS_POLL_BYTE
>> 8,
433 DTC_STATUS_POLL_BYTE
,
436 EP1_CMD_MEMORY_WRITE
, /* set EP2IN to return data */
440 (ep2txr
& ST7_EP2TXR_DTOG_TX
) | ST7_EP2TXR_STAT_VALID
,
441 EP1_CMD_DTC_CALL
, /* start running the DTC */
443 EP1_CMD_DTC_GET_CACHED_STATUS
448 /* wait for completion */
449 usb_err
= usb_bulk_read(
450 pHDev
, USB_EP1IN_ADDR
,
458 static int dtc_run_download(
459 usb_dev_handle
*pHDev_param
,
460 uint8_t *command_buffer
,
461 int command_buffer_size
,
462 uint8_t *reply_buffer
,
463 int reply_buffer_size
470 LOG_DEBUG("%d/%d", command_buffer_size
, reply_buffer_size
);
472 usb_err
= usb_bulk_write(
475 (char *)command_buffer
, USB_EP2BANK_SIZE
,
482 /* Wait for DTC to finish running command buffer */
484 usb_err
= ep1_generic_commandl(
488 DTC_STATUS_POLL_BYTE
>> 8,
489 DTC_STATUS_POLL_BYTE
,
495 usb_err
= usb_bulk_read(
504 if (dtc_status
& 0x01)
508 LOG_ERROR("too many retries waiting for DTC status");
514 if (reply_buffer
&& reply_buffer_size
) {
515 usb_err
= usb_bulk_read(
518 (char *)reply_buffer
, reply_buffer_size
,
522 if (usb_err
< reply_buffer_size
) {
523 LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
524 usb_err
, reply_buffer_size
534 * The dtc reply queue is a singly linked list that describes what to do
535 * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
539 struct dtc_reply_queue_entry
{
540 struct dtc_reply_queue_entry
*next
;
541 struct jtag_command
*cmd
; /* the command that resulted in this entry */
544 uint8_t *buffer
; /* the scan buffer */
545 int size
; /* size of the scan buffer in bits */
546 int offset
; /* how many bits were already done before this? */
547 int length
; /* how many bits are processed in this operation? */
548 enum scan_type type
; /* SCAN_IN/SCAN_OUT/SCAN_IO */
554 * The dtc_queue consists of a buffer of pending commands and a reply queue.
555 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
559 struct dtc_reply_queue_entry
*rq_head
;
560 struct dtc_reply_queue_entry
*rq_tail
;
562 uint32_t reply_index
;
563 uint8_t cmd_buffer
[USB_EP2BANK_SIZE
];
567 * The tap state queue is for accumulating TAP state changes wiithout needlessly
568 * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
577 static int dtc_queue_init(void)
579 dtc_queue
.rq_head
= NULL
;
580 dtc_queue
.rq_tail
= NULL
;
581 dtc_queue
.cmd_index
= 0;
582 dtc_queue
.reply_index
= 0;
586 static inline struct dtc_reply_queue_entry
*dtc_queue_enqueue_reply(
587 enum scan_type type
, uint8_t *buffer
, int size
, int offset
,
588 int length
, struct jtag_command
*cmd
)
590 struct dtc_reply_queue_entry
*rq_entry
;
592 rq_entry
= malloc(sizeof(struct dtc_reply_queue_entry
));
593 if (rq_entry
!= NULL
) {
594 rq_entry
->scan
.type
= type
;
595 rq_entry
->scan
.buffer
= buffer
;
596 rq_entry
->scan
.size
= size
;
597 rq_entry
->scan
.offset
= offset
;
598 rq_entry
->scan
.length
= length
;
600 rq_entry
->next
= NULL
;
602 if (dtc_queue
.rq_head
== NULL
)
603 dtc_queue
.rq_head
= rq_entry
;
605 dtc_queue
.rq_tail
->next
= rq_entry
;
607 dtc_queue
.rq_tail
= rq_entry
;
614 * Running the queue means that any pending command buffer is run
615 * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
616 * The queue is automatically run by append when it is necessary to get space for the append.
619 static int dtc_queue_run(void)
621 struct dtc_reply_queue_entry
*rq_p
, *rq_next
;
626 uint8_t *dtc_p
, *tdo_p
;
627 uint8_t dtc_mask
, tdo_mask
;
628 uint8_t reply_buffer
[USB_EP2IN_SIZE
];
630 assert((dtc_queue
.rq_head
!= 0) == (dtc_queue
.reply_index
> 0));
631 assert(dtc_queue
.cmd_index
< USB_EP2BANK_SIZE
);
632 assert(dtc_queue
.reply_index
<= USB_EP2IN_SIZE
);
636 if (dtc_queue
.cmd_index
< 1)
639 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = DTC_CMD_STOP
;
641 usb_err
= dtc_run_download(pHDev
,
642 dtc_queue
.cmd_buffer
, dtc_queue
.cmd_index
,
643 reply_buffer
, sizeof(reply_buffer
)
646 LOG_ERROR("dtc_run_download: %s", usb_strerror());
650 if (dtc_queue
.rq_head
!= NULL
) {
651 /* process the reply, which empties the reply queue and frees its entries */
652 dtc_p
= reply_buffer
;
654 /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
655 *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
656 *was that or craft a function to do the reversal, and that wouldn't work with
657 *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
658 *scheme which would throw the byte alignment off. */
661 rq_p
= dtc_queue
.rq_head
;
665 tdo_p
= rq_p
->scan
.buffer
+ (rq_p
->scan
.offset
/ 8);
666 tdo_mask
= 1 << (rq_p
->scan
.offset
% 8);
669 bit_cnt
= rq_p
->scan
.length
;
673 dtc_mask
= 1 << (8 - 1);
680 if (*dtc_p
& dtc_mask
)
688 dtc_mask
= 1 << (8 - 1);
698 /* extra bits or last bit */
701 if ((rq_p
->scan
.type
== SCAN_IN
) && (
702 rq_p
->scan
.offset
!= rq_p
->scan
.size
- 1
704 /* extra bits were sent as a full byte with padding on the
706 dtc_mask
= 1 << (8 - 1);
708 dtc_mask
= 1 << (bit_cnt
- 1);
731 if ((rq_p
->scan
.offset
+ rq_p
->scan
.length
) >= rq_p
->scan
.size
) {
732 /* feed scan buffer back into openocd and free it */
733 if (jtag_read_buffer(rq_p
->scan
.buffer
,
734 rq_p
->cmd
->cmd
.scan
) != ERROR_OK
)
735 retval
= ERROR_JTAG_QUEUE_FAILED
;
736 free(rq_p
->scan
.buffer
);
739 rq_next
= rq_p
->next
;
742 dtc_queue
.rq_head
= NULL
;
743 dtc_queue
.rq_tail
= NULL
;
746 /* reset state for new appends */
747 dtc_queue
.cmd_index
= 0;
748 dtc_queue
.reply_index
= 0;
753 /* runs the queue if it cannot take reserved_cmd bytes of command data
754 * or reserved_reply bytes of reply data */
755 static int dtc_queue_run_if_full(int reserved_cmd
, int reserved_reply
)
757 /* reserve one additional byte for the STOP cmd appended during run */
758 if (dtc_queue
.cmd_index
+ reserved_cmd
+ 1 > USB_EP2BANK_SIZE
)
759 return dtc_queue_run();
761 if (dtc_queue
.reply_index
+ reserved_reply
> USB_EP2IN_SIZE
)
762 return dtc_queue_run();
767 static int tap_state_queue_init(void)
769 tap_state_queue
.length
= 0;
770 tap_state_queue
.buffer
= 0;
774 static int tap_state_queue_run(void)
782 if (!tap_state_queue
.length
)
786 for (i
= tap_state_queue
.length
; i
--; ) {
789 if (tap_state_queue
.buffer
& 1)
791 if ((bits
>= 8) || !i
) {
792 byte_param
<<= (8 - bits
);
794 /* make sure there's room for two cmd bytes */
795 dtc_queue_run_if_full(2, 0);
797 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
799 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
800 DTC_CMD_SHIFT_TMS_BYTES(1);
803 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
804 DTC_CMD_SHIFT_TMS_BITS(bits
);
805 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
809 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
817 tap_state_queue
.buffer
>>= 1;
819 retval
= tap_state_queue_init();
823 static int tap_state_queue_append(uint8_t tms
)
827 if (tap_state_queue
.length
>= sizeof(tap_state_queue
.buffer
) * 8) {
828 retval
= tap_state_queue_run();
834 tap_state_queue
.buffer
|= (1 << tap_state_queue
.length
);
835 tap_state_queue
.length
++;
840 static void rlink_end_state(tap_state_t state
)
842 if (tap_is_state_stable(state
))
843 tap_set_end_state(state
);
845 LOG_ERROR("BUG: %i is not a valid end state", state
);
850 static void rlink_state_move(void)
854 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
855 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
857 for (i
= 0; i
< tms_count
; i
++) {
858 tms
= (tms_scan
>> i
) & 1;
859 tap_state_queue_append(tms
);
862 tap_set_state(tap_get_end_state());
865 static void rlink_path_move(struct pathmove_command
*cmd
)
867 int num_states
= cmd
->num_states
;
873 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[state_count
])
875 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[state_count
])
878 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
879 tap_state_name(tap_get_state()),
880 tap_state_name(cmd
->path
[state_count
]));
884 tap_state_queue_append(tms
);
886 tap_set_state(cmd
->path
[state_count
]);
891 tap_set_end_state(tap_get_state());
894 static void rlink_runtest(int num_cycles
)
898 tap_state_t saved_end_state
= tap_get_end_state();
900 /* only do a state_move when we're not already in RTI */
901 if (tap_get_state() != TAP_IDLE
) {
902 rlink_end_state(TAP_IDLE
);
906 /* execute num_cycles */
907 for (i
= 0; i
< num_cycles
; i
++)
908 tap_state_queue_append(0);
910 /* finish in end_state */
911 rlink_end_state(saved_end_state
);
912 if (tap_get_state() != tap_get_end_state())
916 /* (1) assert or (0) deassert reset lines */
917 static void rlink_reset(int trst
, int srst
)
922 /* Read port A for bit op */
923 usb_err
= ep1_generic_commandl(
931 LOG_ERROR("%s", usb_strerror());
935 usb_err
= usb_bulk_read(
936 pHDev
, USB_EP1IN_ADDR
,
941 LOG_ERROR("%s", usb_strerror());
946 bitmap
&= ~ST7_PA_NTRST
;
948 bitmap
|= ST7_PA_NTRST
;
950 /* Write port A and read port B for bit op
951 * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
952 *and assert NSRST by setting DDR to 1. */
953 usb_err
= ep1_generic_commandl(
955 EP1_CMD_MEMORY_WRITE
,
966 LOG_ERROR("%s", usb_strerror());
970 usb_err
= usb_bulk_read(
971 pHDev
, USB_EP1IN_ADDR
,
976 LOG_ERROR("%s", usb_strerror());
981 bitmap
|= ST7_PB_NSRST
;
983 bitmap
&= ~ST7_PB_NSRST
;
985 /* write port B and read dummy to ensure completion before returning */
986 usb_err
= ep1_generic_commandl(
988 EP1_CMD_MEMORY_WRITE
,
993 EP1_CMD_DTC_GET_CACHED_STATUS
996 LOG_ERROR("%s", usb_strerror());
1000 usb_err
= usb_bulk_read(
1001 pHDev
, USB_EP1IN_ADDR
,
1006 LOG_ERROR("%s", usb_strerror());
1011 static int rlink_scan(struct jtag_command
*cmd
, enum scan_type type
,
1012 uint8_t *buffer
, int scan_size
)
1015 tap_state_t saved_end_state
;
1023 uint8_t tdi_mask
, *tdi_p
;
1026 if (scan_size
< 1) {
1027 LOG_ERROR("scan_size cannot be less than 1 bit");
1031 ir_scan
= cmd
->cmd
.scan
->ir_scan
;
1033 /* Move to the proper state before starting to shift TDI/TDO. */
1034 if (!((!ir_scan
&& (tap_get_state() == TAP_DRSHIFT
)) ||
1035 (ir_scan
&& (tap_get_state() == TAP_IRSHIFT
)))) {
1036 saved_end_state
= tap_get_end_state();
1037 rlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1039 rlink_end_state(saved_end_state
);
1042 tap_state_queue_run();
1046 printf("scan_size = %d, type = 0x%x\n", scan_size
, type
);
1050 /* clear unused bits in scan buffer for ease of debugging
1051 * (it makes diffing output easier) */
1052 buffer
[scan_size
/ 8] &= ((1 << ((scan_size
- 1) % 8) + 1) - 1);
1054 printf("before scan:");
1055 for (i
= 0; i
< (scan_size
+ 7) / 8; i
++)
1056 printf(" %02x", buffer
[i
]);
1061 /* The number of bits that can be shifted as complete bytes */
1062 byte_bits
= (int)(scan_size
- 1) / 8 * 8;
1063 /* The number of bits left over, not counting the last bit */
1064 extra_bits
= (scan_size
- 1) - byte_bits
;
1070 if (extra_bits
&& (type
== SCAN_OUT
)) {
1071 /* Schedule any extra bits into the DTC command buffer, padding as needed
1072 * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1073 *fall off the end */
1075 /* make sure there's room for two cmd bytes */
1076 dtc_queue_run_if_full(2, 0);
1079 dtc_mask
= 1 << (extra_bits
- 1);
1081 while (extra_bits
--) {
1082 if (*tdi_p
& tdi_mask
)
1088 if (tdi_mask
== 0) {
1094 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1095 DTC_CMD_SHIFT_TDI_BYTES(1);
1097 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1100 /* Loop scheduling full bytes into the DTC command buffer */
1102 /* make sure there's room for one (for in scans) or two cmd bytes and
1103 * at least one reply byte for in or inout scans*/
1104 dtc_queue_run_if_full(type
== SCAN_IN
? 1 : 2, type
!= SCAN_OUT
? 1 : 0);
1106 chunk_bits
= byte_bits
;
1107 /* we can only use up to 16 bytes at a time */
1108 if (chunk_bits
> (16 * 8))
1109 chunk_bits
= (16 * 8);
1111 if (type
!= SCAN_IN
) {
1112 /* how much is there room for, considering stop and byte op? */
1113 x
= (sizeof(dtc_queue
.cmd_buffer
) - (dtc_queue
.cmd_index
+ 1 + 1)) * 8;
1118 if (type
!= SCAN_OUT
) {
1119 /* how much is there room for in the reply buffer? */
1120 x
= (USB_EP2IN_SIZE
- dtc_queue
.reply_index
) * 8;
1125 /* so the loop will end */
1126 byte_bits
-= chunk_bits
;
1128 if (type
!= SCAN_OUT
) {
1129 if (dtc_queue_enqueue_reply(
1130 type
, buffer
, scan_size
, tdi_bit_offset
,
1134 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1137 dtc_queue
.reply_index
+= (chunk_bits
+ 7) / 8;
1139 tdi_bit_offset
+= chunk_bits
;
1142 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1143 chunk_bytes
= chunk_bits
/ 8;
1147 x
= DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes
);
1150 x
= DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes
);
1153 x
= DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes
);
1156 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1158 if (type
!= SCAN_IN
) {
1160 dtc_mask
= 1 << (8 - 1);
1162 while (chunk_bits
--) {
1163 if (*tdi_p
& tdi_mask
)
1167 if (dtc_mask
== 0) {
1168 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1170 dtc_mask
= 1 << (8 - 1);
1174 if (tdi_mask
== 0) {
1182 if (extra_bits
&& (type
!= SCAN_OUT
)) {
1183 /* Schedule any extra bits into the DTC command buffer */
1185 /* make sure there's room for one (for in scans) or two cmd bytes
1186 * and one reply byte */
1187 dtc_queue_run_if_full(type
== SCAN_IN
? 1 : 2, 1);
1189 if (dtc_queue_enqueue_reply(
1190 type
, buffer
, scan_size
, tdi_bit_offset
,
1194 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1198 dtc_queue
.reply_index
++;
1200 tdi_bit_offset
+= extra_bits
;
1202 if (type
== SCAN_IN
) {
1203 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1204 DTC_CMD_SHIFT_TDO_BYTES(1);
1207 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1208 DTC_CMD_SHIFT_TDIO_BITS(extra_bits
);
1211 dtc_mask
= 1 << (8 - 1);
1213 while (extra_bits
--) {
1214 if (*tdi_p
& tdi_mask
)
1220 if (tdi_mask
== 0) {
1226 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1230 /* Schedule the last bit into the DTC command buffer */
1232 /* make sure there's room for one cmd byte and one reply byte
1233 * for in or inout scans*/
1234 dtc_queue_run_if_full(1, type
== SCAN_OUT
? 0 : 1);
1236 if (type
== SCAN_OUT
) {
1237 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1238 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 0);
1241 if (dtc_queue_enqueue_reply(
1242 type
, buffer
, scan_size
, tdi_bit_offset
,
1246 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1250 dtc_queue
.reply_index
++;
1252 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1253 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 1);
1256 /* Move to pause state */
1257 tap_state_queue_append(0);
1258 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1259 if (tap_get_state() != tap_get_end_state())
1265 static int rlink_execute_queue(void)
1267 struct jtag_command
*cmd
= jtag_command_queue
; /* currently processed command */
1269 enum scan_type type
;
1271 int retval
, tmp_retval
;
1273 /* return ERROR_OK, unless something goes wrong */
1276 #ifndef AUTOMATIC_BUSY_LED
1278 ep1_generic_commandl(pHDev
, 2,
1279 EP1_CMD_SET_PORTD_LEDS
,
1285 switch (cmd
->type
) {
1287 case JTAG_TLR_RESET
:
1293 /* some events, such as resets, need a queue flush to ensure
1295 tap_state_queue_run();
1300 switch (cmd
->type
) {
1302 #ifdef _DEBUG_JTAG_IO_
1303 LOG_DEBUG("reset trst: %i srst %i",
1304 cmd
->cmd
.reset
->trst
,
1305 cmd
->cmd
.reset
->srst
);
1307 if ((cmd
->cmd
.reset
->trst
== 1) ||
1308 (cmd
->cmd
.reset
->srst
&&
1309 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1310 tap_set_state(TAP_RESET
);
1311 rlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1314 #ifdef _DEBUG_JTAG_IO_
1315 LOG_DEBUG("runtest %i cycles, end in %i",
1316 cmd
->cmd
.runtest
->num_cycles
,
1317 cmd
->cmd
.runtest
->end_state
);
1319 if (cmd
->cmd
.runtest
->end_state
!= -1)
1320 rlink_end_state(cmd
->cmd
.runtest
->end_state
);
1321 rlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
1323 case JTAG_TLR_RESET
:
1324 #ifdef _DEBUG_JTAG_IO_
1325 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
1327 if (cmd
->cmd
.statemove
->end_state
!= -1)
1328 rlink_end_state(cmd
->cmd
.statemove
->end_state
);
1332 #ifdef _DEBUG_JTAG_IO_
1333 LOG_DEBUG("pathmove: %i states, end in %i",
1334 cmd
->cmd
.pathmove
->num_states
,
1335 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1337 rlink_path_move(cmd
->cmd
.pathmove
);
1340 #ifdef _DEBUG_JTAG_IO_
1341 LOG_DEBUG("%s scan end in %i",
1342 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR",
1343 cmd
->cmd
.scan
->end_state
);
1345 if (cmd
->cmd
.scan
->end_state
!= -1)
1346 rlink_end_state(cmd
->cmd
.scan
->end_state
);
1347 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1348 type
= jtag_scan_type(cmd
->cmd
.scan
);
1349 if (rlink_scan(cmd
, type
, buffer
, scan_size
) != ERROR_OK
)
1350 retval
= ERROR_FAIL
;
1353 #ifdef _DEBUG_JTAG_IO_
1354 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
1356 jtag_sleep(cmd
->cmd
.sleep
->us
);
1359 LOG_ERROR("BUG: unknown JTAG command type encountered");
1365 /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1367 tap_state_queue_run();
1368 tmp_retval
= dtc_queue_run();
1369 if (tmp_retval
!= ERROR_OK
)
1370 retval
= tmp_retval
;
1372 #ifndef AUTOMATIC_BUSY_LED
1374 ep1_generic_commandl(pHDev
, 2,
1375 EP1_CMD_SET_PORTD_LEDS
,
1383 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1384 *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1386 static int rlink_speed(int speed
)
1392 speed
= rlink_speed_table
[rlink_speed_table_size
- 1].prescaler
;
1395 for (i
= rlink_speed_table_size
; i
--; ) {
1396 if (rlink_speed_table
[i
].prescaler
== speed
) {
1397 if (dtc_load_from_buffer(pHDev
, rlink_speed_table
[i
].dtc
,
1398 rlink_speed_table
[i
].dtc_size
) != 0) {
1400 "An error occurred while trying to load DTC code for speed \"%d\".",
1405 if (dtc_start_download() < 0) {
1406 LOG_ERROR("starting DTC: %s", usb_strerror());
1414 LOG_ERROR("%d is not a supported speed", speed
);
1418 static int rlink_speed_div(int speed
, int *khz
)
1422 for (i
= rlink_speed_table_size
; i
--; ) {
1423 if (rlink_speed_table
[i
].prescaler
== speed
) {
1424 *khz
= rlink_speed_table
[i
].khz
;
1429 LOG_ERROR("%d is not a supported speed", speed
);
1433 static int rlink_khz(int khz
, int *speed
)
1438 LOG_ERROR("RCLK not supported");
1442 for (i
= rlink_speed_table_size
; i
--; ) {
1443 if (rlink_speed_table
[i
].khz
<= khz
) {
1444 *speed
= rlink_speed_table
[i
].prescaler
;
1449 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table
[0].khz
);
1450 *speed
= rlink_speed_table
[0].prescaler
;
1454 static int rlink_init(void)
1457 uint8_t reply_buffer
[USB_EP1IN_SIZE
];
1460 const uint16_t vids
[] = { USB_IDVENDOR
, 0 };
1461 const uint16_t pids
[] = { USB_IDPRODUCT
, 0 };
1462 if (jtag_usb_open(vids
, pids
, &pHDev
) != ERROR_OK
)
1465 struct usb_device
*dev
= usb_device(pHDev
);
1466 if (dev
->descriptor
.bNumConfigurations
> 1) {
1467 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1470 if (dev
->config
->bNumInterfaces
> 1) {
1471 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1475 LOG_DEBUG("Opened device, pHDev = %p", pHDev
);
1477 /* usb_set_configuration required under win32 */
1478 usb_set_configuration(pHDev
, dev
->config
[0].bConfigurationValue
);
1482 i
= usb_claim_interface(pHDev
, 0);
1484 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1485 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1486 j
= usb_detach_kernel_driver_np(pHDev
, 0);
1488 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1491 LOG_DEBUG("interface claimed!");
1494 } while (--retries
);
1497 LOG_ERROR("Initialisation failed.");
1500 if (usb_set_altinterface(pHDev
, 0) != 0) {
1501 LOG_ERROR("Failed to set interface.");
1505 /* The device starts out in an unknown state on open. As such,
1506 * result reads time out, and it's not even known whether the
1507 * command was accepted. So, for this first command, we issue
1508 * it repeatedly until its response doesn't time out. Also, if
1509 * sending a command is going to time out, we find that out here.
1511 * It must be possible to open the device in such a way that
1512 * this special magic isn't needed, but, so far, it escapes us.
1514 for (i
= 0; i
< 5; i
++) {
1515 j
= ep1_generic_commandl(
1519 if (j
< USB_EP1OUT_SIZE
) {
1520 LOG_ERROR("USB write error: %s", usb_strerror());
1524 pHDev
, USB_EP1IN_ADDR
,
1525 (char *)reply_buffer
, sizeof(reply_buffer
),
1528 if (j
!= -ETIMEDOUT
)
1532 if (j
< (int)sizeof(reply_buffer
)) {
1533 LOG_ERROR("USB read error: %s", usb_strerror());
1536 LOG_DEBUG(INTERFACE_NAME
" firmware version: %d.%d.%d",
1541 if ((reply_buffer
[0] != 0) || (reply_buffer
[1] != 0) || (reply_buffer
[2] != 3))
1543 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1545 /* Probe port E for adapter presence */
1546 ep1_generic_commandl(
1548 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 0 */
1553 ST7_PE_ADAPTER_SENSE_OUT
, /* DDR */
1554 ST7_PE_ADAPTER_SENSE_OUT
, /* OR */
1555 EP1_CMD_MEMORY_READ
, /* Read back */
1559 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 1 */
1563 ST7_PE_ADAPTER_SENSE_OUT
1567 pHDev
, USB_EP1IN_ADDR
,
1568 (char *)reply_buffer
, 1,
1572 if ((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) != 0)
1573 LOG_WARNING("target detection problem");
1575 ep1_generic_commandl(
1577 EP1_CMD_MEMORY_READ
, /* Read back */
1581 EP1_CMD_MEMORY_WRITE
, /* float port E */
1591 pHDev
, USB_EP1IN_ADDR
,
1592 (char *)reply_buffer
, 1,
1597 if ((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) == 0)
1598 LOG_WARNING("target not plugged in");
1600 /* float ports A and B */
1601 ep1_generic_commandl(
1603 EP1_CMD_MEMORY_WRITE
,
1609 EP1_CMD_MEMORY_WRITE
,
1616 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1617 ep1_generic_commandl(
1620 EP1_CMD_SET_PORTD_VPP
,
1622 EP1_CMD_MEMORY_WRITE
,
1626 ((~(0)) & (ST7_PA_NTRST
)),
1628 /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1629 *here and later assert NSRST by setting DDR bit to 1. */
1630 EP1_CMD_MEMORY_WRITE
,
1637 /* set LED updating mode and make sure they're unlit */
1638 ep1_generic_commandl(
1640 #ifdef AUTOMATIC_BUSY_LED
1645 EP1_CMD_SET_PORTD_LEDS
,
1649 tap_state_queue_init();
1656 static int rlink_quit(void)
1658 /* stop DTC and make sure LEDs are off */
1659 ep1_generic_commandl(
1663 EP1_CMD_SET_PORTD_LEDS
,
1665 EP1_CMD_SET_PORTD_VPP
,
1669 usb_release_interface(pHDev
, 0);
1675 struct jtag_interface rlink_interface
= {
1679 .speed
= rlink_speed
,
1680 .speed_div
= rlink_speed_div
,
1682 .execute_queue
= rlink_execute_queue
,