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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
31 /* project specific includes */
32 #include <jtag/interface.h>
33 #include <jtag/commands.h>
35 #include "rlink_st7.h"
36 #include "rlink_ep1_cmd.h"
37 #include "rlink_dtc_cmd.h"
38 #include "usb_common.h"
40 /* This feature is made useless by running the DTC all the time. When automatic, the LED is on
41 *whenever the DTC is running. Otherwise, USB messages are sent to turn it on and off. */
42 #undef AUTOMATIC_BUSY_LED
44 /* This feature may require derating the speed due to reduced hold time. */
45 #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)
67 #define ST7_PD_NBUSY_LED ST7_PD0
68 #define ST7_PD_NRUN_LED ST7_PD1
69 /* low enables VPP at adapter header, high connects it to GND instead */
70 #define ST7_PD_VPP_SEL ST7_PD6
71 /* low: VPP = 12v, high: VPP <= 5v */
72 #define ST7_PD_VPP_SHDN ST7_PD7
74 /* These pins are connected together */
75 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
76 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
78 /* Symbolic mapping between port pins and numbered IO lines */
79 #define ST7_PA_IO1 ST7_PA1
80 #define ST7_PA_IO2 ST7_PA2
81 #define ST7_PA_IO4 ST7_PA4
82 #define ST7_PA_IO8 ST7_PA6
83 #define ST7_PA_IO10 ST7_PA7
84 #define ST7_PB_IO5 ST7_PB5
85 #define ST7_PC_IO9 ST7_PC1
86 #define ST7_PC_IO3 ST7_PC2
87 #define ST7_PC_IO7 ST7_PC3
88 #define ST7_PE_IO6 ST7_PE5
90 /* Symbolic mapping between numbered IO lines and adapter signals */
91 #define ST7_PA_RTCK ST7_PA_IO0
92 #define ST7_PA_NTRST ST7_PA_IO1
93 #define ST7_PC_TDI ST7_PC_IO3
94 #define ST7_PA_DBGRQ ST7_PA_IO4
95 #define ST7_PB_NSRST ST7_PB_IO5
96 #define ST7_PE_TMS ST7_PE_IO6
97 #define ST7_PC_TCK ST7_PC_IO7
98 #define ST7_PC_TDO ST7_PC_IO9
99 #define ST7_PA_DBGACK ST7_PA_IO10
101 static usb_dev_handle
*pHDev
;
104 * ep1 commands are up to USB_EP1OUT_SIZE bytes in length.
105 * This function takes care of zeroing the unused bytes before sending the packet.
106 * Any reply packet is not handled by this function.
108 static int ep1_generic_commandl(usb_dev_handle
*pHDev_param
, size_t length
, ...)
110 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
111 uint8_t *usb_buffer_p
;
115 if (length
> sizeof(usb_buffer
))
116 length
= sizeof(usb_buffer
);
118 usb_buffer_p
= usb_buffer
;
120 va_start(ap
, length
);
122 *usb_buffer_p
++ = va_arg(ap
, int);
129 sizeof(usb_buffer
) - (usb_buffer_p
- usb_buffer
)
132 usb_ret
= usb_bulk_write(
135 (char *)usb_buffer
, sizeof(usb_buffer
),
143 static ssize_t
ep1_memory_read(
144 usb_dev_handle
*pHDev
, uint16_t addr
,
145 size_t length
, uint8_t *buffer
)
147 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
152 usb_buffer
[0] = EP1_CMD_MEMORY_READ
;
156 sizeof(usb_buffer
) - 4
163 if (remain
> sizeof(usb_buffer
))
164 length
= sizeof(usb_buffer
);
168 usb_buffer
[1] = addr
>> 8;
169 usb_buffer
[2] = addr
;
170 usb_buffer
[3] = length
;
172 usb_ret
= usb_bulk_write(
173 pHDev
, USB_EP1OUT_ADDR
,
174 usb_buffer
, sizeof(usb_buffer
),
178 if (usb_ret
< sizeof(usb_buffer
))
181 usb_ret
= usb_bulk_read(
182 pHDev
, USB_EP1IN_ADDR
,
187 if (usb_ret
< length
)
200 static ssize_t
ep1_memory_write(usb_dev_handle
*pHDev_param
, uint16_t addr
,
201 size_t length
, uint8_t const *buffer
)
203 uint8_t usb_buffer
[USB_EP1OUT_SIZE
];
208 usb_buffer
[0] = EP1_CMD_MEMORY_WRITE
;
214 if (remain
> (sizeof(usb_buffer
) - 4))
215 length
= (sizeof(usb_buffer
) - 4);
219 usb_buffer
[1] = addr
>> 8;
220 usb_buffer
[2] = addr
;
221 usb_buffer
[3] = length
;
228 usb_buffer
+ 4 + length
,
230 sizeof(usb_buffer
) - 4 - length
233 usb_ret
= usb_bulk_write(
234 pHDev_param
, USB_EP1OUT_ADDR
,
235 (char *)usb_buffer
, sizeof(usb_buffer
),
239 if ((size_t)usb_ret
< sizeof(usb_buffer
))
253 static ssize_t
ep1_memory_writel(usb_dev_handle
*pHDev
, uint16_t addr
,
256 uint8_t buffer
[USB_EP1OUT_SIZE
- 4];
261 if (length
> sizeof(buffer
))
262 length
= sizeof(buffer
);
267 va_start(ap
, length
);
269 *buffer_p
++ = va_arg(ap
, int);
273 return ep1_memory_write(pHDev
, addr
, length
, buffer
);
277 #define DTCLOAD_COMMENT (0)
278 #define DTCLOAD_ENTRY (1)
279 #define DTCLOAD_LOAD (2)
280 #define DTCLOAD_RUN (3)
281 #define DTCLOAD_LUT_START (4)
282 #define DTCLOAD_LUT (5)
284 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
286 /* This gets set by the DTC loader */
287 static uint8_t dtc_entry_download
;
289 /* The buffer is specially formatted to represent a valid image to load into the DTC. */
290 static int dtc_load_from_buffer(usb_dev_handle
*pHDev_param
, const uint8_t *buffer
,
299 struct header_s
*header
;
300 uint8_t lut_start
= 0xc0;
302 dtc_entry_download
= 0;
304 /* Stop the DTC before loading anything. */
305 usb_err
= ep1_generic_commandl(
313 if (length
< sizeof(*header
)) {
314 LOG_ERROR("Malformed DTC image");
318 header
= (struct header_s
*)buffer
;
319 buffer
+= sizeof(*header
);
320 length
-= sizeof(*header
);
322 if (length
< (size_t)header
->length
+ 1) {
323 LOG_ERROR("Malformed DTC image");
327 switch (header
->type
) {
328 case DTCLOAD_COMMENT
:
332 /* store entry addresses somewhere */
333 if (!strncmp("download", (char *)buffer
+ 1, 8))
334 dtc_entry_download
= buffer
[0];
338 /* Send the DTC program to ST7 RAM. */
339 usb_err
= ep1_memory_write(
342 header
->length
+ 1, buffer
347 /* Load it into the DTC. */
348 usb_err
= ep1_generic_commandl(
351 (DTC_LOAD_BUFFER
>> 8),
360 usb_err
= ep1_generic_commandl(
371 case DTCLOAD_LUT_START
:
372 lut_start
= buffer
[0];
376 usb_err
= ep1_memory_write(
378 ST7_USB_BUF_EP0OUT
+ lut_start
,
379 header
->length
+ 1, buffer
386 LOG_ERROR("Invalid DTC image record type: 0x%02x", header
->type
);
391 buffer
+= (header
->length
+ 1);
392 length
-= (header
->length
+ 1);
399 * Start the DTC running in download mode (waiting for 512 byte command packets on ep2).
401 static int dtc_start_download(void)
406 /* set up for download mode and make sure EP2 is set up to transmit */
407 usb_err
= ep1_generic_commandl(
412 EP1_CMD_SET_DOWNLOAD
,
413 EP1_CMD_MEMORY_READ
, /* read EP2TXR for its data toggle */
421 /* read back ep2txr */
422 usb_err
= usb_bulk_read(
423 pHDev
, USB_EP1IN_ADDR
,
430 usb_err
= ep1_generic_commandl(
433 EP1_CMD_MEMORY_WRITE
, /* preinitialize poll byte */
434 DTC_STATUS_POLL_BYTE
>> 8,
435 DTC_STATUS_POLL_BYTE
,
438 EP1_CMD_MEMORY_WRITE
, /* set EP2IN to return data */
442 (ep2txr
& ST7_EP2TXR_DTOG_TX
) | ST7_EP2TXR_STAT_VALID
,
443 EP1_CMD_DTC_CALL
, /* start running the DTC */
445 EP1_CMD_DTC_GET_CACHED_STATUS
450 /* wait for completion */
451 usb_err
= usb_bulk_read(
452 pHDev
, USB_EP1IN_ADDR
,
460 static int dtc_run_download(
461 usb_dev_handle
*pHDev_param
,
462 uint8_t *command_buffer
,
463 int command_buffer_size
,
464 uint8_t *reply_buffer
,
465 int reply_buffer_size
472 LOG_DEBUG("%d/%d", command_buffer_size
, reply_buffer_size
);
474 usb_err
= usb_bulk_write(
477 (char *)command_buffer
, USB_EP2BANK_SIZE
,
484 /* Wait for DTC to finish running command buffer */
486 usb_err
= ep1_generic_commandl(
490 DTC_STATUS_POLL_BYTE
>> 8,
491 DTC_STATUS_POLL_BYTE
,
497 usb_err
= usb_bulk_read(
506 if (dtc_status
& 0x01)
510 LOG_ERROR("too many retries waiting for DTC status");
516 if (reply_buffer
&& reply_buffer_size
) {
517 usb_err
= usb_bulk_read(
520 (char *)reply_buffer
, reply_buffer_size
,
524 if (usb_err
< reply_buffer_size
) {
525 LOG_ERROR("Read of endpoint 2 returned %d, expected %d",
526 usb_err
, reply_buffer_size
536 * The dtc reply queue is a singly linked list that describes what to do
537 * with the reply packet that comes from the DTC. Only SCAN_IN and SCAN_IO generate
541 struct dtc_reply_queue_entry
{
542 struct dtc_reply_queue_entry
*next
;
543 struct jtag_command
*cmd
; /* the command that resulted in this entry */
546 uint8_t *buffer
; /* the scan buffer */
547 int size
; /* size of the scan buffer in bits */
548 int offset
; /* how many bits were already done before this? */
549 int length
; /* how many bits are processed in this operation? */
550 enum scan_type type
; /* SCAN_IN/SCAN_OUT/SCAN_IO */
556 * The dtc_queue consists of a buffer of pending commands and a reply queue.
557 * rlink_scan and tap_state_run add to the command buffer and maybe to the reply queue.
561 struct dtc_reply_queue_entry
*rq_head
;
562 struct dtc_reply_queue_entry
*rq_tail
;
564 uint32_t reply_index
;
565 uint8_t cmd_buffer
[USB_EP2BANK_SIZE
];
569 * The tap state queue is for accumulating TAP state changes wiithout needlessly
570 * flushing the dtc_queue. When it fills or is run, it adds the accumulated bytes to
579 static int dtc_queue_init(void)
581 dtc_queue
.rq_head
= NULL
;
582 dtc_queue
.rq_tail
= NULL
;
583 dtc_queue
.cmd_index
= 0;
584 dtc_queue
.reply_index
= 0;
588 static inline struct dtc_reply_queue_entry
*dtc_queue_enqueue_reply(
589 enum scan_type type
, uint8_t *buffer
, int size
, int offset
,
590 int length
, struct jtag_command
*cmd
)
592 struct dtc_reply_queue_entry
*rq_entry
;
594 rq_entry
= malloc(sizeof(struct dtc_reply_queue_entry
));
595 if (rq_entry
!= NULL
) {
596 rq_entry
->scan
.type
= type
;
597 rq_entry
->scan
.buffer
= buffer
;
598 rq_entry
->scan
.size
= size
;
599 rq_entry
->scan
.offset
= offset
;
600 rq_entry
->scan
.length
= length
;
602 rq_entry
->next
= NULL
;
604 if (dtc_queue
.rq_head
== NULL
)
605 dtc_queue
.rq_head
= rq_entry
;
607 dtc_queue
.rq_tail
->next
= rq_entry
;
609 dtc_queue
.rq_tail
= rq_entry
;
616 * Running the queue means that any pending command buffer is run
617 * and any reply data dealt with. The command buffer is then cleared for subsequent processing.
618 * The queue is automatically run by append when it is necessary to get space for the append.
621 static int dtc_queue_run(void)
623 struct dtc_reply_queue_entry
*rq_p
, *rq_next
;
628 uint8_t *dtc_p
, *tdo_p
;
629 uint8_t dtc_mask
, tdo_mask
;
630 uint8_t reply_buffer
[USB_EP2IN_SIZE
];
632 assert((dtc_queue
.rq_head
!= 0) == (dtc_queue
.reply_index
> 0));
633 assert(dtc_queue
.cmd_index
< USB_EP2BANK_SIZE
);
634 assert(dtc_queue
.reply_index
<= USB_EP2IN_SIZE
);
638 if (dtc_queue
.cmd_index
< 1)
641 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = DTC_CMD_STOP
;
643 usb_err
= dtc_run_download(pHDev
,
644 dtc_queue
.cmd_buffer
, dtc_queue
.cmd_index
,
645 reply_buffer
, sizeof(reply_buffer
)
648 LOG_ERROR("dtc_run_download: %s", usb_strerror());
652 if (dtc_queue
.rq_head
!= NULL
) {
653 /* process the reply, which empties the reply queue and frees its entries */
654 dtc_p
= reply_buffer
;
656 /* The rigamarole with the masks and doing it bit-by-bit is due to the fact that the
657 *scan buffer is LSb-first and the DTC code is MSb-first for hardware reasons. It
658 *was that or craft a function to do the reversal, and that wouldn't work with
659 *bit-stuffing (supplying extra bits to use mostly byte operations), or any other
660 *scheme which would throw the byte alignment off. */
663 rq_p
= dtc_queue
.rq_head
;
667 tdo_p
= rq_p
->scan
.buffer
+ (rq_p
->scan
.offset
/ 8);
668 tdo_mask
= 1 << (rq_p
->scan
.offset
% 8);
671 bit_cnt
= rq_p
->scan
.length
;
675 dtc_mask
= 1 << (8 - 1);
682 if (*dtc_p
& dtc_mask
)
690 dtc_mask
= 1 << (8 - 1);
700 /* extra bits or last bit */
703 if ((rq_p
->scan
.type
== SCAN_IN
) && (
704 rq_p
->scan
.offset
!= rq_p
->scan
.size
- 1
706 /* extra bits were sent as a full byte with padding on the
708 dtc_mask
= 1 << (8 - 1);
710 dtc_mask
= 1 << (bit_cnt
- 1);
733 if ((rq_p
->scan
.offset
+ rq_p
->scan
.length
) >= rq_p
->scan
.size
) {
734 /* feed scan buffer back into openocd and free it */
735 if (jtag_read_buffer(rq_p
->scan
.buffer
,
736 rq_p
->cmd
->cmd
.scan
) != ERROR_OK
)
737 retval
= ERROR_JTAG_QUEUE_FAILED
;
738 free(rq_p
->scan
.buffer
);
741 rq_next
= rq_p
->next
;
744 dtc_queue
.rq_head
= NULL
;
745 dtc_queue
.rq_tail
= NULL
;
748 /* reset state for new appends */
749 dtc_queue
.cmd_index
= 0;
750 dtc_queue
.reply_index
= 0;
755 /* runs the queue if it cannot take reserved_cmd bytes of command data
756 * or reserved_reply bytes of reply data */
757 static int dtc_queue_run_if_full(int reserved_cmd
, int reserved_reply
)
759 /* reserve one additional byte for the STOP cmd appended during run */
760 if (dtc_queue
.cmd_index
+ reserved_cmd
+ 1 > USB_EP2BANK_SIZE
)
761 return dtc_queue_run();
763 if (dtc_queue
.reply_index
+ reserved_reply
> USB_EP2IN_SIZE
)
764 return dtc_queue_run();
769 static int tap_state_queue_init(void)
771 tap_state_queue
.length
= 0;
772 tap_state_queue
.buffer
= 0;
776 static int tap_state_queue_run(void)
784 if (!tap_state_queue
.length
)
788 for (i
= tap_state_queue
.length
; i
--; ) {
791 if (tap_state_queue
.buffer
& 1)
793 if ((bits
>= 8) || !i
) {
794 byte_param
<<= (8 - bits
);
796 /* make sure there's room for two cmd bytes */
797 dtc_queue_run_if_full(2, 0);
799 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
801 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
802 DTC_CMD_SHIFT_TMS_BYTES(1);
805 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
806 DTC_CMD_SHIFT_TMS_BITS(bits
);
807 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
811 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
819 tap_state_queue
.buffer
>>= 1;
821 retval
= tap_state_queue_init();
825 static int tap_state_queue_append(uint8_t tms
)
829 if (tap_state_queue
.length
>= sizeof(tap_state_queue
.buffer
) * 8) {
830 retval
= tap_state_queue_run();
836 tap_state_queue
.buffer
|= (1 << tap_state_queue
.length
);
837 tap_state_queue
.length
++;
842 static void rlink_end_state(tap_state_t state
)
844 if (tap_is_state_stable(state
))
845 tap_set_end_state(state
);
847 LOG_ERROR("BUG: %i is not a valid end state", state
);
852 static void rlink_state_move(void)
856 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
857 int tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
859 for (i
= 0; i
< tms_count
; i
++) {
860 tms
= (tms_scan
>> i
) & 1;
861 tap_state_queue_append(tms
);
864 tap_set_state(tap_get_end_state());
867 static void rlink_path_move(struct pathmove_command
*cmd
)
869 int num_states
= cmd
->num_states
;
875 if (tap_state_transition(tap_get_state(), false) == cmd
->path
[state_count
])
877 else if (tap_state_transition(tap_get_state(), true) == cmd
->path
[state_count
])
880 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
881 tap_state_name(tap_get_state()),
882 tap_state_name(cmd
->path
[state_count
]));
886 tap_state_queue_append(tms
);
888 tap_set_state(cmd
->path
[state_count
]);
893 tap_set_end_state(tap_get_state());
896 static void rlink_runtest(int num_cycles
)
900 tap_state_t saved_end_state
= tap_get_end_state();
902 /* only do a state_move when we're not already in RTI */
903 if (tap_get_state() != TAP_IDLE
) {
904 rlink_end_state(TAP_IDLE
);
908 /* execute num_cycles */
909 for (i
= 0; i
< num_cycles
; i
++)
910 tap_state_queue_append(0);
912 /* finish in end_state */
913 rlink_end_state(saved_end_state
);
914 if (tap_get_state() != tap_get_end_state())
918 /* (1) assert or (0) deassert reset lines */
919 static void rlink_reset(int trst
, int srst
)
924 /* Read port A for bit op */
925 usb_err
= ep1_generic_commandl(
933 LOG_ERROR("%s", usb_strerror());
937 usb_err
= usb_bulk_read(
938 pHDev
, USB_EP1IN_ADDR
,
943 LOG_ERROR("%s", usb_strerror());
948 bitmap
&= ~ST7_PA_NTRST
;
950 bitmap
|= ST7_PA_NTRST
;
952 /* Write port A and read port B for bit op
953 * port B has no OR, and we want to emulate open drain on NSRST, so we initialize DR to 0
954 *and assert NSRST by setting DDR to 1. */
955 usb_err
= ep1_generic_commandl(
957 EP1_CMD_MEMORY_WRITE
,
968 LOG_ERROR("%s", usb_strerror());
972 usb_err
= usb_bulk_read(
973 pHDev
, USB_EP1IN_ADDR
,
978 LOG_ERROR("%s", usb_strerror());
983 bitmap
|= ST7_PB_NSRST
;
985 bitmap
&= ~ST7_PB_NSRST
;
987 /* write port B and read dummy to ensure completion before returning */
988 usb_err
= ep1_generic_commandl(
990 EP1_CMD_MEMORY_WRITE
,
995 EP1_CMD_DTC_GET_CACHED_STATUS
998 LOG_ERROR("%s", usb_strerror());
1002 usb_err
= usb_bulk_read(
1003 pHDev
, USB_EP1IN_ADDR
,
1008 LOG_ERROR("%s", usb_strerror());
1013 static int rlink_scan(struct jtag_command
*cmd
, enum scan_type type
,
1014 uint8_t *buffer
, int scan_size
)
1017 tap_state_t saved_end_state
;
1025 uint8_t tdi_mask
, *tdi_p
;
1028 if (scan_size
< 1) {
1029 LOG_ERROR("scan_size cannot be less than 1 bit");
1033 ir_scan
= cmd
->cmd
.scan
->ir_scan
;
1035 /* Move to the proper state before starting to shift TDI/TDO. */
1036 if (!((!ir_scan
&& (tap_get_state() == TAP_DRSHIFT
)) ||
1037 (ir_scan
&& (tap_get_state() == TAP_IRSHIFT
)))) {
1038 saved_end_state
= tap_get_end_state();
1039 rlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
1041 rlink_end_state(saved_end_state
);
1044 tap_state_queue_run();
1048 printf("scan_size = %d, type = 0x%x\n", scan_size
, type
);
1052 /* clear unused bits in scan buffer for ease of debugging
1053 * (it makes diffing output easier) */
1054 buffer
[scan_size
/ 8] &= ((1 << ((scan_size
- 1) % 8) + 1) - 1);
1056 printf("before scan:");
1057 for (i
= 0; i
< (scan_size
+ 7) / 8; i
++)
1058 printf(" %02x", buffer
[i
]);
1063 /* The number of bits that can be shifted as complete bytes */
1064 byte_bits
= (int)(scan_size
- 1) / 8 * 8;
1065 /* The number of bits left over, not counting the last bit */
1066 extra_bits
= (scan_size
- 1) - byte_bits
;
1072 if (extra_bits
&& (type
== SCAN_OUT
)) {
1073 /* Schedule any extra bits into the DTC command buffer, padding as needed
1074 * For SCAN_OUT, this comes before the full bytes so the (leading) padding bits will
1075 *fall off the end */
1077 /* make sure there's room for two cmd bytes */
1078 dtc_queue_run_if_full(2, 0);
1081 dtc_mask
= 1 << (extra_bits
- 1);
1083 while (extra_bits
--) {
1084 if (*tdi_p
& tdi_mask
)
1090 if (tdi_mask
== 0) {
1096 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1097 DTC_CMD_SHIFT_TDI_BYTES(1);
1099 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1102 /* Loop scheduling full bytes into the DTC command buffer */
1104 /* make sure there's room for one (for in scans) or two cmd bytes and
1105 * at least one reply byte for in or inout scans*/
1106 dtc_queue_run_if_full(type
== SCAN_IN
? 1 : 2, type
!= SCAN_OUT
? 1 : 0);
1108 chunk_bits
= byte_bits
;
1109 /* we can only use up to 16 bytes at a time */
1110 if (chunk_bits
> (16 * 8))
1111 chunk_bits
= (16 * 8);
1113 if (type
!= SCAN_IN
) {
1114 /* how much is there room for, considering stop and byte op? */
1115 x
= (sizeof(dtc_queue
.cmd_buffer
) - (dtc_queue
.cmd_index
+ 1 + 1)) * 8;
1120 if (type
!= SCAN_OUT
) {
1121 /* how much is there room for in the reply buffer? */
1122 x
= (USB_EP2IN_SIZE
- dtc_queue
.reply_index
) * 8;
1127 /* so the loop will end */
1128 byte_bits
-= chunk_bits
;
1130 if (type
!= SCAN_OUT
) {
1131 if (dtc_queue_enqueue_reply(
1132 type
, buffer
, scan_size
, tdi_bit_offset
,
1136 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1139 dtc_queue
.reply_index
+= (chunk_bits
+ 7) / 8;
1141 tdi_bit_offset
+= chunk_bits
;
1144 /* chunk_bits is a multiple of 8, so there are no rounding issues. */
1145 chunk_bytes
= chunk_bits
/ 8;
1149 x
= DTC_CMD_SHIFT_TDO_BYTES(chunk_bytes
);
1152 x
= DTC_CMD_SHIFT_TDI_BYTES(chunk_bytes
);
1155 x
= DTC_CMD_SHIFT_TDIO_BYTES(chunk_bytes
);
1158 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1160 if (type
!= SCAN_IN
) {
1162 dtc_mask
= 1 << (8 - 1);
1164 while (chunk_bits
--) {
1165 if (*tdi_p
& tdi_mask
)
1169 if (dtc_mask
== 0) {
1170 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1172 dtc_mask
= 1 << (8 - 1);
1176 if (tdi_mask
== 0) {
1184 if (extra_bits
&& (type
!= SCAN_OUT
)) {
1185 /* Schedule any extra bits into the DTC command buffer */
1187 /* make sure there's room for one (for in scans) or two cmd bytes
1188 * and one reply byte */
1189 dtc_queue_run_if_full(type
== SCAN_IN
? 1 : 2, 1);
1191 if (dtc_queue_enqueue_reply(
1192 type
, buffer
, scan_size
, tdi_bit_offset
,
1196 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1200 dtc_queue
.reply_index
++;
1202 tdi_bit_offset
+= extra_bits
;
1204 if (type
== SCAN_IN
) {
1205 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1206 DTC_CMD_SHIFT_TDO_BYTES(1);
1209 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1210 DTC_CMD_SHIFT_TDIO_BITS(extra_bits
);
1213 dtc_mask
= 1 << (8 - 1);
1215 while (extra_bits
--) {
1216 if (*tdi_p
& tdi_mask
)
1222 if (tdi_mask
== 0) {
1228 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] = x
;
1232 /* Schedule the last bit into the DTC command buffer */
1234 /* make sure there's room for one cmd byte and one reply byte
1235 * for in or inout scans*/
1236 dtc_queue_run_if_full(1, type
== SCAN_OUT
? 0 : 1);
1238 if (type
== SCAN_OUT
) {
1239 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1240 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 0);
1243 if (dtc_queue_enqueue_reply(
1244 type
, buffer
, scan_size
, tdi_bit_offset
,
1248 LOG_ERROR("enqueuing DTC reply entry: %s", strerror(errno
));
1252 dtc_queue
.reply_index
++;
1254 dtc_queue
.cmd_buffer
[dtc_queue
.cmd_index
++] =
1255 DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(1, (*tdi_p
& tdi_mask
), 1);
1258 /* Move to pause state */
1259 tap_state_queue_append(0);
1260 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
1261 if (tap_get_state() != tap_get_end_state())
1267 static int rlink_execute_queue(void)
1269 struct jtag_command
*cmd
= jtag_command_queue
; /* currently processed command */
1271 enum scan_type type
;
1273 int retval
, tmp_retval
;
1275 /* return ERROR_OK, unless something goes wrong */
1278 #ifndef AUTOMATIC_BUSY_LED
1280 ep1_generic_commandl(pHDev
, 2,
1281 EP1_CMD_SET_PORTD_LEDS
,
1287 switch (cmd
->type
) {
1289 case JTAG_TLR_RESET
:
1295 /* some events, such as resets, need a queue flush to ensure
1297 tap_state_queue_run();
1302 switch (cmd
->type
) {
1304 #ifdef _DEBUG_JTAG_IO_
1305 LOG_DEBUG("reset trst: %i srst %i",
1306 cmd
->cmd
.reset
->trst
,
1307 cmd
->cmd
.reset
->srst
);
1309 if ((cmd
->cmd
.reset
->trst
== 1) ||
1310 (cmd
->cmd
.reset
->srst
&&
1311 (jtag_get_reset_config() & RESET_SRST_PULLS_TRST
)))
1312 tap_set_state(TAP_RESET
);
1313 rlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
1316 #ifdef _DEBUG_JTAG_IO_
1317 LOG_DEBUG("runtest %i cycles, end in %i",
1318 cmd
->cmd
.runtest
->num_cycles
,
1319 cmd
->cmd
.runtest
->end_state
);
1321 if (cmd
->cmd
.runtest
->end_state
!= -1)
1322 rlink_end_state(cmd
->cmd
.runtest
->end_state
);
1323 rlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
1325 case JTAG_TLR_RESET
:
1326 #ifdef _DEBUG_JTAG_IO_
1327 LOG_DEBUG("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
1329 if (cmd
->cmd
.statemove
->end_state
!= -1)
1330 rlink_end_state(cmd
->cmd
.statemove
->end_state
);
1334 #ifdef _DEBUG_JTAG_IO_
1335 LOG_DEBUG("pathmove: %i states, end in %i",
1336 cmd
->cmd
.pathmove
->num_states
,
1337 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
1339 rlink_path_move(cmd
->cmd
.pathmove
);
1342 #ifdef _DEBUG_JTAG_IO_
1343 LOG_DEBUG("%s scan end in %i",
1344 (cmd
->cmd
.scan
->ir_scan
) ? "IR" : "DR",
1345 cmd
->cmd
.scan
->end_state
);
1347 if (cmd
->cmd
.scan
->end_state
!= -1)
1348 rlink_end_state(cmd
->cmd
.scan
->end_state
);
1349 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
1350 type
= jtag_scan_type(cmd
->cmd
.scan
);
1351 if (rlink_scan(cmd
, type
, buffer
, scan_size
) != ERROR_OK
)
1352 retval
= ERROR_FAIL
;
1355 #ifdef _DEBUG_JTAG_IO_
1356 LOG_DEBUG("sleep %i", cmd
->cmd
.sleep
->us
);
1358 jtag_sleep(cmd
->cmd
.sleep
->us
);
1361 LOG_ERROR("BUG: unknown JTAG command type encountered");
1367 /* Flush the DTC queue to make sure any pending reads have been done before exiting this
1369 tap_state_queue_run();
1370 tmp_retval
= dtc_queue_run();
1371 if (tmp_retval
!= ERROR_OK
)
1372 retval
= tmp_retval
;
1374 #ifndef AUTOMATIC_BUSY_LED
1376 ep1_generic_commandl(pHDev
, 2,
1377 EP1_CMD_SET_PORTD_LEDS
,
1385 /* Using an unindexed table because it is infrequently accessed and it is short. The table must be
1386 *in order of ascending speed (and descending prescaler), as it is scanned in reverse. */
1388 static int rlink_speed(int speed
)
1394 speed
= rlink_speed_table
[rlink_speed_table_size
- 1].prescaler
;
1397 for (i
= rlink_speed_table_size
; i
--; ) {
1398 if (rlink_speed_table
[i
].prescaler
== speed
) {
1399 if (dtc_load_from_buffer(pHDev
, rlink_speed_table
[i
].dtc
,
1400 rlink_speed_table
[i
].dtc_size
) != 0) {
1402 "An error occurred while trying to load DTC code for speed \"%d\".",
1407 if (dtc_start_download() < 0) {
1408 LOG_ERROR("starting DTC: %s", usb_strerror());
1416 LOG_ERROR("%d is not a supported speed", speed
);
1420 static int rlink_speed_div(int speed
, int *khz
)
1424 for (i
= rlink_speed_table_size
; i
--; ) {
1425 if (rlink_speed_table
[i
].prescaler
== speed
) {
1426 *khz
= rlink_speed_table
[i
].khz
;
1431 LOG_ERROR("%d is not a supported speed", speed
);
1435 static int rlink_khz(int khz
, int *speed
)
1440 LOG_ERROR("RCLK not supported");
1444 for (i
= rlink_speed_table_size
; i
--; ) {
1445 if (rlink_speed_table
[i
].khz
<= khz
) {
1446 *speed
= rlink_speed_table
[i
].prescaler
;
1451 LOG_WARNING("The lowest supported JTAG speed is %d KHz", rlink_speed_table
[0].khz
);
1452 *speed
= rlink_speed_table
[0].prescaler
;
1456 static int rlink_init(void)
1459 uint8_t reply_buffer
[USB_EP1IN_SIZE
];
1462 const uint16_t vids
[] = { USB_IDVENDOR
, 0 };
1463 const uint16_t pids
[] = { USB_IDPRODUCT
, 0 };
1464 if (jtag_usb_open(vids
, pids
, &pHDev
) != ERROR_OK
)
1467 struct usb_device
*dev
= usb_device(pHDev
);
1468 if (dev
->descriptor
.bNumConfigurations
> 1) {
1469 LOG_ERROR("Whoops! NumConfigurations is not 1, don't know what to do...");
1472 if (dev
->config
->bNumInterfaces
> 1) {
1473 LOG_ERROR("Whoops! NumInterfaces is not 1, don't know what to do...");
1477 LOG_DEBUG("Opened device, pHDev = %p", pHDev
);
1479 /* usb_set_configuration required under win32 */
1480 usb_set_configuration(pHDev
, dev
->config
[0].bConfigurationValue
);
1484 i
= usb_claim_interface(pHDev
, 0);
1486 LOG_ERROR("usb_claim_interface: %s", usb_strerror());
1487 #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
1488 j
= usb_detach_kernel_driver_np(pHDev
, 0);
1490 LOG_ERROR("detach kernel driver: %s", usb_strerror());
1493 LOG_DEBUG("interface claimed!");
1496 } while (--retries
);
1499 LOG_ERROR("Initialisation failed.");
1502 if (usb_set_altinterface(pHDev
, 0) != 0) {
1503 LOG_ERROR("Failed to set interface.");
1507 /* The device starts out in an unknown state on open. As such,
1508 * result reads time out, and it's not even known whether the
1509 * command was accepted. So, for this first command, we issue
1510 * it repeatedly until its response doesn't time out. Also, if
1511 * sending a command is going to time out, we find that out here.
1513 * It must be possible to open the device in such a way that
1514 * this special magic isn't needed, but, so far, it escapes us.
1516 for (i
= 0; i
< 5; i
++) {
1517 j
= ep1_generic_commandl(
1521 if (j
< USB_EP1OUT_SIZE
) {
1522 LOG_ERROR("USB write error: %s", usb_strerror());
1526 pHDev
, USB_EP1IN_ADDR
,
1527 (char *)reply_buffer
, sizeof(reply_buffer
),
1530 if (j
!= -ETIMEDOUT
)
1534 if (j
< (int)sizeof(reply_buffer
)) {
1535 LOG_ERROR("USB read error: %s", usb_strerror());
1538 LOG_DEBUG(INTERFACE_NAME
" firmware version: %d.%d.%d",
1543 if ((reply_buffer
[0] != 0) || (reply_buffer
[1] != 0) || (reply_buffer
[2] != 3))
1545 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1547 /* Probe port E for adapter presence */
1548 ep1_generic_commandl(
1550 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 0 */
1555 ST7_PE_ADAPTER_SENSE_OUT
, /* DDR */
1556 ST7_PE_ADAPTER_SENSE_OUT
, /* OR */
1557 EP1_CMD_MEMORY_READ
, /* Read back */
1561 EP1_CMD_MEMORY_WRITE
, /* Drive sense pin with 1 */
1565 ST7_PE_ADAPTER_SENSE_OUT
1569 pHDev
, USB_EP1IN_ADDR
,
1570 (char *)reply_buffer
, 1,
1574 if ((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) != 0)
1575 LOG_WARNING("target detection problem");
1577 ep1_generic_commandl(
1579 EP1_CMD_MEMORY_READ
, /* Read back */
1583 EP1_CMD_MEMORY_WRITE
, /* float port E */
1593 pHDev
, USB_EP1IN_ADDR
,
1594 (char *)reply_buffer
, 1,
1599 if ((reply_buffer
[0] & ST7_PE_ADAPTER_SENSE_IN
) == 0)
1600 LOG_WARNING("target not plugged in");
1602 /* float ports A and B */
1603 ep1_generic_commandl(
1605 EP1_CMD_MEMORY_WRITE
,
1611 EP1_CMD_MEMORY_WRITE
,
1618 /* make sure DTC is stopped, set VPP control, set up ports A and B */
1619 ep1_generic_commandl(
1622 EP1_CMD_SET_PORTD_VPP
,
1624 EP1_CMD_MEMORY_WRITE
,
1628 ((~(0)) & (ST7_PA_NTRST
)),
1630 /* port B has no OR, and we want to emulate open drain on NSRST, so we set DR to 0
1631 *here and later assert NSRST by setting DDR bit to 1. */
1632 EP1_CMD_MEMORY_WRITE
,
1639 /* set LED updating mode and make sure they're unlit */
1640 ep1_generic_commandl(
1642 #ifdef AUTOMATIC_BUSY_LED
1647 EP1_CMD_SET_PORTD_LEDS
,
1651 tap_state_queue_init();
1658 static int rlink_quit(void)
1660 /* stop DTC and make sure LEDs are off */
1661 ep1_generic_commandl(
1665 EP1_CMD_SET_PORTD_LEDS
,
1667 EP1_CMD_SET_PORTD_VPP
,
1671 usb_release_interface(pHDev
, 0);
1677 struct jtag_interface rlink_interface
= {
1681 .speed
= rlink_speed
,
1682 .speed_div
= rlink_speed_div
,
1684 .execute_queue
= rlink_execute_queue
,