1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
9 * plagnioj@jcrosoft.com *
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 #include <jtag/interface.h>
33 #include <jtag/commands.h>
34 #include "libusb_common.h"
36 /* See Segger's public documentation:
37 * Reference manual for J-Link USB Protocol
38 * Document RM08001-R6 Date: June 16, 2009
39 * (Or newer, with some SWD information).
40 * http://www.segger.com/cms/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
44 * The default pid of the segger is 0x0101
45 * But when you change the USB Address it will also
47 * pid = ( usb_address > 0x4) ? 0x0101 : (0x101 + usb_address)
50 #define JLINK_OB_PID 0x0105
52 #define JLINK_WRITE_ENDPOINT 0x02
53 #define JLINK_READ_ENDPOINT 0x81
55 #define JLINK_OB_WRITE_ENDPOINT 0x06
56 #define JLINK_OB_READ_ENDPOINT 0x85
58 static unsigned int jlink_write_ep
= JLINK_WRITE_ENDPOINT
;
59 static unsigned int jlink_read_ep
= JLINK_READ_ENDPOINT
;
60 static unsigned int jlink_hw_jtag_version
= 2;
62 #define JLINK_USB_TIMEOUT 1000
64 /* See Section 3.3.2 of the Segger JLink USB protocol manual */
65 /* 2048 is the max value we can use here */
66 #define JLINK_TAP_BUFFER_SIZE 2048
67 /*#define JLINK_TAP_BUFFER_SIZE 256*/
68 /*#define JLINK_TAP_BUFFER_SIZE 384*/
70 #define JLINK_IN_BUFFER_SIZE (2048 + 1)
71 #define JLINK_OUT_BUFFER_SIZE (2*2048 + 4)
73 /* Global USB buffers */
74 static uint8_t usb_in_buffer
[JLINK_IN_BUFFER_SIZE
];
75 static uint8_t usb_out_buffer
[JLINK_OUT_BUFFER_SIZE
];
77 /* Constants for JLink command */
78 #define EMU_CMD_VERSION 0x01
79 #define EMU_CMD_RESET_TRST 0x02
80 #define EMU_CMD_RESET_TARGET 0x03
81 #define EMU_CMD_SET_SPEED 0x05
82 #define EMU_CMD_GET_STATE 0x07
83 #define EMU_CMD_SET_KS_POWER 0x08
84 #define EMU_CMD_GET_SPEEDS 0xc0
85 #define EMU_CMD_GET_HW_INFO 0xc1
86 #define EMU_CMD_GET_COUNTERS 0xc2
87 #define EMU_CMD_SELECT_IF 0xc7
88 #define EMU_CMD_HW_CLOCK 0xc8
89 #define EMU_CMD_HW_TMS0 0xc9
90 #define EMU_CMD_HW_TMS1 0xca
91 #define EMU_CMD_HW_DATA0 0xcb
92 #define EMU_CMD_HW_DATA1 0xcc
93 #define EMU_CMD_HW_JTAG 0xcd
94 #define EMU_CMD_HW_JTAG2 0xce
95 #define EMU_CMD_HW_JTAG3 0xcf
96 #define EMU_CMD_HW_RELEASE_RESET_STOP_EX 0xd0
97 #define EMU_CMD_HW_RELEASE_RESET_STOP_TIMED 0xd1
98 #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
99 #define EMU_CMD_HW_JTAG_WRITE 0xd5
100 #define EMU_CMD_HW_JTAG_GET_RESULT 0xd6
101 #define EMU_CMD_HW_RESET0 0xdc
102 #define EMU_CMD_HW_RESET1 0xdd
103 #define EMU_CMD_HW_TRST0 0xde
104 #define EMU_CMD_HW_TRST1 0xdf
105 #define EMU_CMD_GET_CAPS 0xe8
106 #define EMU_CMD_GET_CPU_CAPS 0xe9
107 #define EMU_CMD_EXEC_CPU_CMD 0xea
108 #define EMU_CMD_GET_CAPS_EX 0xed
109 #define EMU_CMD_GET_HW_VERSION 0xf0
110 #define EMU_CMD_WRITE_DCC 0xf1
111 #define EMU_CMD_READ_CONFIG 0xf2
112 #define EMU_CMD_WRITE_CONFIG 0xf3
113 #define EMU_CMD_WRITE_MEM 0xf4
114 #define EMU_CMD_READ_MEM 0xf5
115 #define EMU_CMD_MEASURE_RTCK_REACT 0xf6
116 #define EMU_CMD_WRITE_MEM_ARM79 0xf7
117 #define EMU_CMD_READ_MEM_ARM79 0xf8
119 /* bits return from EMU_CMD_GET_CAPS */
120 #define EMU_CAP_RESERVED_1 0
121 #define EMU_CAP_GET_HW_VERSION 1
122 #define EMU_CAP_WRITE_DCC 2
123 #define EMU_CAP_ADAPTIVE_CLOCKING 3
124 #define EMU_CAP_READ_CONFIG 4
125 #define EMU_CAP_WRITE_CONFIG 5
126 #define EMU_CAP_TRACE 6
127 #define EMU_CAP_WRITE_MEM 7
128 #define EMU_CAP_READ_MEM 8
129 #define EMU_CAP_SPEED_INFO 9
130 #define EMU_CAP_EXEC_CODE 10
131 #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
132 #define EMU_CAP_GET_HW_INFO 12
133 #define EMU_CAP_SET_KS_POWER 13
134 #define EMU_CAP_RESET_STOP_TIMED 14
135 #define EMU_CAP_RESERVED_2 15
136 #define EMU_CAP_MEASURE_RTCK_REACT 16
137 #define EMU_CAP_SELECT_IF 17
138 #define EMU_CAP_RW_MEM_ARM79 18
139 #define EMU_CAP_GET_COUNTERS 19
140 #define EMU_CAP_READ_DCC 20
141 #define EMU_CAP_GET_CPU_CAPS 21
142 #define EMU_CAP_EXEC_CPU_CMD 22
143 #define EMU_CAP_SWO 23
144 #define EMU_CAP_WRITE_DCC_EX 24
145 #define EMU_CAP_UPDATE_FIRMWARE_EX 25
146 #define EMU_CAP_FILE_IO 26
147 #define EMU_CAP_REGISTER 27
148 #define EMU_CAP_INDICATORS 28
149 #define EMU_CAP_TEST_NET_SPEED 29
150 #define EMU_CAP_RAWTRACE 30
151 #define EMU_CAP_RESERVED_3 31
153 static char *jlink_cap_str
[] = {
155 "Supports command EMU_CMD_GET_HARDWARE_VERSION",
156 "Supports command EMU_CMD_WRITE_DCC",
157 "Supports adaptive clocking",
158 "Supports command EMU_CMD_READ_CONFIG",
159 "Supports command EMU_CMD_WRITE_CONFIG",
160 "Supports trace commands",
161 "Supports command EMU_CMD_WRITE_MEM",
162 "Supports command EMU_CMD_READ_MEM",
163 "Supports command EMU_CMD_GET_SPEED",
164 "Supports command EMU_CMD_CODE_...",
165 "Supports command EMU_CMD_GET_MAX_BLOCK_SIZE",
166 "Supports command EMU_CMD_GET_HW_INFO",
167 "Supports command EMU_CMD_SET_KS_POWER",
168 "Supports command EMU_CMD_HW_RELEASE_RESET_STOP_TIMED",
170 "Supports command EMU_CMD_MEASURE_RTCK_REACT",
171 "Supports command EMU_CMD_HW_SELECT_IF",
172 "Supports command EMU_CMD_READ/WRITE_MEM_ARM79",
173 "Supports command EMU_CMD_GET_COUNTERS",
174 "Supports command EMU_CMD_READ_DCC",
175 "Supports command EMU_CMD_GET_CPU_CAPS",
176 "Supports command EMU_CMD_EXEC_CPU_CMD",
177 "Supports command EMU_CMD_SWO",
178 "Supports command EMU_CMD_WRITE_DCC_EX",
179 "Supports command EMU_CMD_UPDATE_FIRMWARE_EX",
180 "Supports command EMU_CMD_FILE_IO",
181 "Supports command EMU_CMD_REGISTER",
182 "Supports command EMU_CMD_INDICATORS",
183 "Supports command EMU_CMD_TEST_NET_SPEED",
184 "Supports command EMU_CMD_RAWTRACE",
188 /* max speed 12MHz v5.0 jlink */
189 #define JLINK_MAX_SPEED 12000
191 /* J-Link hardware versions */
192 #define JLINK_HW_TYPE_JLINK 0
193 #define JLINK_HW_TYPE_JTRACE 1
194 #define JLINK_HW_TYPE_FLASHER 2
195 #define JLINK_HW_TYPE_JLINK_PRO 3
196 #define JLINK_HW_TYPE_MAX 4
198 static char *jlink_hw_type_str
[] = {
205 #define JLINK_TIF_JTAG 0
206 #define JLINK_TIF_SWD 1
207 #define JLINK_SWD_DIR_IN 0
208 #define JLINK_SWD_DIR_OUT 1
210 /* Queue command functions */
211 static void jlink_end_state(tap_state_t state
);
212 static void jlink_state_move(void);
213 static void jlink_path_move(int num_states
, tap_state_t
*path
);
214 static void jlink_runtest(int num_cycles
);
215 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
216 int scan_size
, struct scan_command
*command
);
217 static void jlink_reset(int trst
, int srst
);
218 static void jlink_simple_command(uint8_t command
);
219 static int jlink_get_status(void);
220 static int jlink_swd_run_queue(struct adiv5_dap
*dap
);
221 static void jlink_swd_queue_cmd(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *dst
, uint32_t data
);
222 static int jlink_swd_switch_seq(struct adiv5_dap
*dap
, enum swd_special_seq seq
);
224 /* J-Link tap buffer functions */
225 static void jlink_tap_init(void);
226 static int jlink_tap_execute(void);
227 static void jlink_tap_ensure_space(int scans
, int bits
);
228 static void jlink_tap_append_step(int tms
, int tdi
);
229 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
230 struct scan_command
*command
);
232 /* Jlink lowlevel functions */
234 struct jtag_libusb_device_handle
*usb_handle
;
237 static struct jlink
*jlink_usb_open(void);
238 static void jlink_usb_close(struct jlink
*jlink
);
239 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
);
240 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
);
241 static int jlink_usb_write(struct jlink
*jlink
, int out_length
);
242 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
);
244 /* helper functions */
245 static int jlink_get_version_info(void);
247 #ifdef _DEBUG_USB_COMMS_
248 static void jlink_debug_buffer(uint8_t *buffer
, int length
);
250 static inline void jlink_debug_buffer(uint8_t *buffer
, int length
)
255 static enum tap_state jlink_last_state
= TAP_RESET
;
257 static struct jlink
*jlink_handle
;
259 /* pid could be specified at runtime */
260 static uint16_t vids
[] = { 0x1366, 0x1366, 0x1366, 0x1366, 0x1366, 0 };
261 static uint16_t pids
[] = { 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0 };
263 static uint32_t jlink_caps
;
264 static uint32_t jlink_hw_type
;
266 static int queued_retval
;
267 static bool swd_mode
;
269 /* 256 byte non-volatile memory */
270 struct jlink_config
{
272 /* 0ffset 0x01 to 0x03 */
273 uint8_t reserved_1
[3];
274 uint32_t kickstart_power_on_jtag_pin_19
;
275 /* 0ffset 0x08 to 0x1f */
276 uint8_t reserved_2
[24];
277 /* IP only for J-Link Pro */
278 uint8_t ip_address
[4];
279 uint8_t subnet_mask
[4];
280 /* 0ffset 0x28 to 0x2f */
281 uint8_t reserved_3
[8];
282 uint8_t mac_address
[6];
283 /* 0ffset 0x36 to 0xff */
284 uint8_t reserved_4
[202];
285 } __attribute__ ((packed
));
286 struct jlink_config jlink_cfg
;
288 /***************************************************************************/
289 /* External interface implementation */
291 static void jlink_execute_runtest(struct jtag_command
*cmd
)
293 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
294 cmd
->cmd
.runtest
->num_cycles
,
295 cmd
->cmd
.runtest
->end_state
);
297 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
299 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
302 static void jlink_execute_statemove(struct jtag_command
*cmd
)
304 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
306 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
310 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
312 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
313 cmd
->cmd
.pathmove
->num_states
,
314 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
316 jlink_path_move(cmd
->cmd
.pathmove
->num_states
,
317 cmd
->cmd
.pathmove
->path
);
320 static void jlink_execute_scan(struct jtag_command
*cmd
)
326 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd
->cmd
.scan
->end_state
));
328 jlink_end_state(cmd
->cmd
.scan
->end_state
);
330 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
331 DEBUG_JTAG_IO("scan input, length = %d", scan_size
);
333 jlink_debug_buffer(buffer
, (scan_size
+ 7) / 8);
334 type
= jtag_scan_type(cmd
->cmd
.scan
);
335 jlink_scan(cmd
->cmd
.scan
->ir_scan
,
336 type
, buffer
, scan_size
, cmd
->cmd
.scan
);
339 static void jlink_execute_reset(struct jtag_command
*cmd
)
341 DEBUG_JTAG_IO("reset trst: %i srst %i",
342 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
345 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
349 static void jlink_execute_sleep(struct jtag_command
*cmd
)
351 DEBUG_JTAG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
353 jtag_sleep(cmd
->cmd
.sleep
->us
);
356 static void jlink_execute_command(struct jtag_command
*cmd
)
360 jlink_execute_runtest(cmd
);
363 jlink_execute_statemove(cmd
);
366 jlink_execute_pathmove(cmd
);
369 jlink_execute_scan(cmd
);
372 jlink_execute_reset(cmd
);
375 jlink_execute_sleep(cmd
);
378 LOG_ERROR("BUG: unknown JTAG command type encountered");
383 static int jlink_execute_queue(void)
385 struct jtag_command
*cmd
= jtag_command_queue
;
387 while (cmd
!= NULL
) {
388 jlink_execute_command(cmd
);
392 return jlink_tap_execute();
395 /* Sets speed in kHz. */
396 static int jlink_speed(int speed
)
400 if (speed
> JLINK_MAX_SPEED
) {
401 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
402 speed
, JLINK_MAX_SPEED
);
403 speed
= JLINK_MAX_SPEED
;
406 /* check for RTCK setting */
410 usb_out_buffer
[0] = EMU_CMD_SET_SPEED
;
411 usb_out_buffer
[1] = (speed
>> 0) & 0xff;
412 usb_out_buffer
[2] = (speed
>> 8) & 0xff;
414 result
= jlink_usb_write(jlink_handle
, 3);
416 LOG_ERROR("J-Link setting speed failed (%d)", result
);
417 return ERROR_JTAG_DEVICE_ERROR
;
423 static int jlink_speed_div(int speed
, int *khz
)
430 static int jlink_khz(int khz
, int *jtag_speed
)
438 * select transport interface
440 * @param iface [0..31] currently: 0=JTAG, 1=SWD
441 * @returns ERROR_OK or ERROR_ code
443 * @pre jlink_handle must be opened
444 * @pre function may be called only for devices, that have
445 * EMU_CAP_SELECT_IF capability enabled
447 static int jlink_select_interface(int iface
)
449 /* According to Segger's document RM08001-R7 Date: October 8, 2010,
450 * http://www.segger.com/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
451 * section 5.5.3 EMU_CMD_SELECT_IF
452 * > SubCmd 1..31 to select interface (0..31)
454 * The table below states:
458 * This obviosly means that to select TIF_JTAG one should write SubCmd = 1.
460 * In fact, JTAG interface operates when SubCmd=0
462 * It looks like a typo in documentation, because interfaces 0..31 could not
463 * be selected by 1..31 range command.
465 assert(iface
>= 0 && iface
< 32);
468 /* get available interfaces */
469 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
470 usb_out_buffer
[1] = 0xff;
472 result
= jlink_usb_io(jlink_handle
, 2, 4);
473 if (result
!= ERROR_OK
) {
474 LOG_ERROR("J-Link query interface failed (%d)", result
);
475 return ERROR_JTAG_DEVICE_ERROR
;
478 uint32_t iface_mask
= buf_get_u32(usb_in_buffer
, 0, 32);
480 if (!(iface_mask
& (1<<iface
))) {
481 LOG_ERROR("J-Link requesting to select unsupported interface (%" PRIx32
")", iface_mask
);
482 return ERROR_JTAG_DEVICE_ERROR
;
485 /* Select interface */
486 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
487 usb_out_buffer
[1] = iface
;
489 result
= jlink_usb_io(jlink_handle
, 2, 4);
490 if (result
!= ERROR_OK
) {
491 LOG_ERROR("J-Link interface select failed (%d)", result
);
492 return ERROR_JTAG_DEVICE_ERROR
;
498 static int jlink_init(void)
502 jlink_handle
= jlink_usb_open();
504 if (jlink_handle
== 0) {
505 LOG_ERROR("Cannot find jlink Interface! Please check "
506 "connection and permissions.");
507 return ERROR_JTAG_INIT_FAILED
;
511 * The next three instructions were added after discovering a problem
512 * while using an oscilloscope.
513 * For the V8 SAM-ICE dongle (and likely other j-link device variants),
514 * the reset line to the target microprocessor was found to cycle only
515 * intermittently during emulator startup (even after encountering the
516 * downstream reset instruction later in the code).
517 * This was found to create two issues:
518 * 1) In general it is a bad practice to not reset a CPU to a known
519 * state when starting an emulator and
520 * 2) something critical happens inside the dongle when it does the
521 * first read following a new USB session.
522 * Keeping the processor in reset during the first read collecting
523 * version information seems to prevent errant
524 * "J-Link command EMU_CMD_VERSION failed" issues.
527 LOG_INFO("J-Link initialization started / target CPU reset initiated");
528 jlink_simple_command(EMU_CMD_HW_TRST0
);
529 jlink_simple_command(EMU_CMD_HW_RESET0
);
532 jlink_hw_jtag_version
= 2;
534 if (jlink_get_version_info() == ERROR_OK
) {
535 /* attempt to get status */
540 * Some versions of Segger's software do not select JTAG interface by default.
542 * Segger recommends to select interface necessarily as a part of init process,
543 * in case any previous session leaves improper interface selected.
546 if (jlink_caps
& (1<<EMU_CAP_SELECT_IF
))
547 retval
= jlink_select_interface(swd_mode
? JLINK_TIF_SWD
: JLINK_TIF_JTAG
);
549 retval
= swd_mode
? ERROR_JTAG_DEVICE_ERROR
: ERROR_OK
;
551 if (retval
!= ERROR_OK
) {
552 LOG_ERROR("Selected transport mode is not supported.");
553 return ERROR_JTAG_INIT_FAILED
;
556 LOG_INFO("J-Link JTAG Interface ready");
563 /* v5/6 jlink seems to have an issue if the first tap move
564 * is not divisible by 8, so we send a TLR on first power up */
565 for (i
= 0; i
< 8; i
++)
566 jlink_tap_append_step(1, 0);
571 jlink_swd_switch_seq(NULL
, JTAG_TO_SWD
);
573 jlink_swd_switch_seq(NULL
, SWD_TO_JTAG
);
574 jlink_swd_run_queue(NULL
);
579 static int jlink_quit(void)
581 jlink_usb_close(jlink_handle
);
585 /***************************************************************************/
586 /* Queue command implementations */
588 static void jlink_end_state(tap_state_t state
)
590 if (tap_is_state_stable(state
))
591 tap_set_end_state(state
);
593 LOG_ERROR("BUG: %i is not a valid end state", state
);
598 /* Goes to the end state. */
599 static void jlink_state_move(void)
603 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
604 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
606 for (i
= 0; i
< tms_scan_bits
; i
++) {
607 tms
= (tms_scan
>> i
) & 1;
608 jlink_tap_append_step(tms
, 0);
611 tap_set_state(tap_get_end_state());
614 static void jlink_path_move(int num_states
, tap_state_t
*path
)
618 for (i
= 0; i
< num_states
; i
++) {
619 if (path
[i
] == tap_state_transition(tap_get_state(), false))
620 jlink_tap_append_step(0, 0);
621 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
622 jlink_tap_append_step(1, 0);
624 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
625 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
629 tap_set_state(path
[i
]);
632 tap_set_end_state(tap_get_state());
635 static void jlink_runtest(int num_cycles
)
639 tap_state_t saved_end_state
= tap_get_end_state();
641 jlink_tap_ensure_space(1, num_cycles
+ 16);
643 /* only do a state_move when we're not already in IDLE */
644 if (tap_get_state() != TAP_IDLE
) {
645 jlink_end_state(TAP_IDLE
);
650 /* execute num_cycles */
651 for (i
= 0; i
< num_cycles
; i
++)
652 jlink_tap_append_step(0, 0);
654 /* finish in end_state */
655 jlink_end_state(saved_end_state
);
656 if (tap_get_state() != tap_get_end_state())
660 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
661 int scan_size
, struct scan_command
*command
)
663 tap_state_t saved_end_state
;
665 jlink_tap_ensure_space(1, scan_size
+ 16);
667 saved_end_state
= tap_get_end_state();
669 /* Move to appropriate scan state */
670 jlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
672 /* Only move if we're not already there */
673 if (tap_get_state() != tap_get_end_state())
676 jlink_end_state(saved_end_state
);
679 jlink_tap_append_scan(scan_size
, buffer
, command
);
681 /* We are in Exit1, go to Pause */
682 jlink_tap_append_step(0, 0);
684 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
686 if (tap_get_state() != tap_get_end_state())
690 static void jlink_reset(int trst
, int srst
)
692 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
694 /* Signals are active low */
696 jlink_simple_command(EMU_CMD_HW_RESET1
);
699 jlink_simple_command(EMU_CMD_HW_RESET0
);
702 jlink_simple_command(EMU_CMD_HW_TRST0
);
705 jlink_simple_command(EMU_CMD_HW_TRST1
);
708 static void jlink_simple_command(uint8_t command
)
712 DEBUG_JTAG_IO("0x%02x", command
);
714 usb_out_buffer
[0] = command
;
715 result
= jlink_usb_write(jlink_handle
, 1);
718 LOG_ERROR("J-Link command 0x%02x failed (%d)", command
, result
);
721 static int jlink_get_status(void)
725 jlink_simple_command(EMU_CMD_GET_STATE
);
727 result
= jlink_usb_read(jlink_handle
, 8);
729 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result
);
730 return ERROR_JTAG_DEVICE_ERROR
;
733 int vref
= usb_in_buffer
[0] + (usb_in_buffer
[1] << 8);
734 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
735 vref
/ 1000, vref
% 1000, \
736 usb_in_buffer
[2], usb_in_buffer
[3], usb_in_buffer
[4], \
737 usb_in_buffer
[5], usb_in_buffer
[6], usb_in_buffer
[7]);
740 LOG_ERROR("Vref too low. Check Target Power");
745 #define jlink_dump_printf(context, expr ...) \
748 command_print(context, expr); \
753 static void jlink_caps_dump(struct command_context
*ctx
)
757 jlink_dump_printf(ctx
, "J-Link Capabilities");
759 for (i
= 1; i
< 31; i
++)
760 if (jlink_caps
& (1 << i
))
761 jlink_dump_printf(ctx
, "%s", jlink_cap_str
[i
]);
764 static void jlink_config_usb_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
769 jlink_dump_printf(ctx
, "USB-Address: 0x%x", cfg
->usb_address
);
772 static void jlink_config_kickstart_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
777 jlink_dump_printf(ctx
, "Kickstart power on JTAG-pin 19: 0x%" PRIx32
,
778 cfg
->kickstart_power_on_jtag_pin_19
);
781 static void jlink_config_mac_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
786 jlink_dump_printf(ctx
, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
787 cfg
->mac_address
[5], cfg
->mac_address
[4],
788 cfg
->mac_address
[3], cfg
->mac_address
[2],
789 cfg
->mac_address
[1], cfg
->mac_address
[0]);
792 static void jlink_config_ip_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
797 jlink_dump_printf(ctx
, "IP Address: %d.%d.%d.%d",
798 cfg
->ip_address
[3], cfg
->ip_address
[2],
799 cfg
->ip_address
[1], cfg
->ip_address
[0]);
800 jlink_dump_printf(ctx
, "Subnet Mask: %d.%d.%d.%d",
801 cfg
->subnet_mask
[3], cfg
->subnet_mask
[2],
802 cfg
->subnet_mask
[1], cfg
->subnet_mask
[0]);
805 static void jlink_config_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
810 jlink_dump_printf(ctx
, "J-Link configuration");
811 jlink_config_usb_address_dump(ctx
, cfg
);
812 jlink_config_kickstart_dump(ctx
, cfg
);
814 if (jlink_hw_type
== JLINK_HW_TYPE_JLINK_PRO
) {
815 jlink_config_ip_dump(ctx
, cfg
);
816 jlink_config_mac_address_dump(ctx
, cfg
);
820 static int jlink_get_config(struct jlink_config
*cfg
)
823 int size
= sizeof(struct jlink_config
);
825 usb_out_buffer
[0] = EMU_CMD_READ_CONFIG
;
826 result
= jlink_usb_io(jlink_handle
, 1, size
);
828 if (result
!= ERROR_OK
) {
829 LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size
, result
);
833 memcpy(cfg
, usb_in_buffer
, size
);
837 static int jlink_set_config(struct jlink_config
*cfg
)
840 int size
= sizeof(struct jlink_config
);
842 jlink_simple_command(EMU_CMD_WRITE_CONFIG
);
844 memcpy(usb_out_buffer
, cfg
, size
);
846 result
= jlink_usb_write(jlink_handle
, size
);
847 if (result
!= size
) {
848 LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result
);
856 * List of unsupported version string markers.
858 * The firmware versions does not correspond directly with
859 * "Software and documentation pack for Windows", it may be
860 * distinguished by the "compile" date in the information string.
862 * For example, version string is:
863 * "J-Link ARM V8 compiled May 3 2012 18:36:22"
867 * The list must be terminated by NULL string.
869 static const char * const unsupported_versions
[] = {
872 NULL
/* End of list */
875 static void jlink_check_supported(const char *str
)
877 const char * const *p
= unsupported_versions
;
879 if (NULL
!= strstr(str
, *p
)) {
881 "Unsupported J-Link firmware version.\n"
882 " Please check http://www.segger.com/j-link-older-versions.html for updates");
889 static int jlink_get_version_info(void)
893 uint32_t jlink_max_size
;
895 /* query hardware version */
896 jlink_simple_command(EMU_CMD_VERSION
);
898 result
= jlink_usb_read(jlink_handle
, 2);
900 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
901 return ERROR_JTAG_DEVICE_ERROR
;
904 len
= buf_get_u32(usb_in_buffer
, 0, 16);
905 if (len
> JLINK_IN_BUFFER_SIZE
) {
906 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len
);
907 len
= JLINK_IN_BUFFER_SIZE
;
910 result
= jlink_usb_read(jlink_handle
, len
);
912 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
913 return ERROR_JTAG_DEVICE_ERROR
;
916 usb_in_buffer
[result
] = 0;
917 LOG_INFO("%s", (char *)usb_in_buffer
);
918 jlink_check_supported((char *)usb_in_buffer
);
920 /* query hardware capabilities */
921 jlink_simple_command(EMU_CMD_GET_CAPS
);
923 result
= jlink_usb_read(jlink_handle
, 4);
925 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result
);
926 return ERROR_JTAG_DEVICE_ERROR
;
929 jlink_caps
= buf_get_u32(usb_in_buffer
, 0, 32);
930 LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps
);
932 if (jlink_caps
& (1 << EMU_CAP_GET_HW_VERSION
)) {
933 /* query hardware version */
934 jlink_simple_command(EMU_CMD_GET_HW_VERSION
);
936 result
= jlink_usb_read(jlink_handle
, 4);
938 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result
);
939 return ERROR_JTAG_DEVICE_ERROR
;
942 uint32_t jlink_hw_version
= buf_get_u32(usb_in_buffer
, 0, 32);
943 uint32_t major_revision
= (jlink_hw_version
/ 10000) % 100;
944 jlink_hw_type
= (jlink_hw_version
/ 1000000) % 100;
945 if (major_revision
>= 5)
946 jlink_hw_jtag_version
= 3;
948 LOG_INFO("J-Link hw version %i", (int)jlink_hw_version
);
950 if (jlink_hw_type
>= JLINK_HW_TYPE_MAX
)
951 LOG_INFO("J-Link hw type uknown 0x%" PRIx32
, jlink_hw_type
);
953 LOG_INFO("J-Link hw type %s", jlink_hw_type_str
[jlink_hw_type
]);
956 if (jlink_caps
& (1 << EMU_CAP_GET_MAX_BLOCK_SIZE
)) {
957 /* query hardware maximum memory block */
958 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK
);
960 result
= jlink_usb_read(jlink_handle
, 4);
962 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result
);
963 return ERROR_JTAG_DEVICE_ERROR
;
966 jlink_max_size
= buf_get_u32(usb_in_buffer
, 0, 32);
967 LOG_INFO("J-Link max mem block %i", (int)jlink_max_size
);
970 if (jlink_caps
& (1 << EMU_CAP_READ_CONFIG
)) {
971 if (jlink_get_config(&jlink_cfg
) != ERROR_OK
)
972 return ERROR_JTAG_DEVICE_ERROR
;
974 jlink_config_dump(NULL
, &jlink_cfg
);
980 COMMAND_HANDLER(jlink_pid_command
)
983 LOG_ERROR("Need exactly one argument to jlink_pid");
987 pids
[0] = strtoul(CMD_ARGV
[0], NULL
, 16);
994 COMMAND_HANDLER(jlink_handle_jlink_info_command
)
996 if (jlink_get_version_info() == ERROR_OK
) {
997 /* attempt to get status */
1004 COMMAND_HANDLER(jlink_handle_jlink_caps_command
)
1006 jlink_caps_dump(CMD_CTX
);
1011 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command
)
1015 command_print(CMD_CTX
, "J-Link hw jtag %i", jlink_hw_jtag_version
);
1018 int request_version
= atoi(CMD_ARGV
[0]);
1019 switch (request_version
) {
1022 jlink_hw_jtag_version
= request_version
;
1025 return ERROR_COMMAND_SYNTAX_ERROR
;
1030 return ERROR_COMMAND_SYNTAX_ERROR
;
1036 COMMAND_HANDLER(jlink_handle_jlink_kickstart_command
)
1041 jlink_config_kickstart_dump(CMD_CTX
, &jlink_cfg
);
1045 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], kickstart
);
1047 jlink_cfg
.kickstart_power_on_jtag_pin_19
= kickstart
;
1051 COMMAND_HANDLER(jlink_handle_jlink_mac_address_command
)
1059 jlink_config_mac_address_dump(CMD_CTX
, &jlink_cfg
);
1065 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' || str
[8] != ':' ||
1066 str
[11] != ':' || str
[14] != ':')) {
1067 command_print(CMD_CTX
, "ethaddr miss format ff:ff:ff:ff:ff:ff");
1068 return ERROR_COMMAND_SYNTAX_ERROR
;
1071 for (i
= 5; i
>= 0; i
--) {
1072 addr
[i
] = strtoul(str
, &e
, 16);
1076 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1077 command_print(CMD_CTX
, "invalid it's zero mac_address");
1078 return ERROR_COMMAND_SYNTAX_ERROR
;
1081 if (!(0x01 & addr
[0])) {
1082 command_print(CMD_CTX
, "invalid it's a multicat mac_address");
1083 return ERROR_COMMAND_SYNTAX_ERROR
;
1086 memcpy(jlink_cfg
.mac_address
, addr
, sizeof(addr
));
1091 static int string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1095 const char *s_save
= s
;
1101 for (i
= 0; i
< 4; i
++) {
1102 lip
[i
] = strtoul(s
, &e
, 10);
1104 if (*e
!= '.' && i
!= 3)
1112 memcpy(ip
, lip
, sizeof(lip
));
1116 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1120 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1124 COMMAND_HANDLER(jlink_handle_jlink_ip_command
)
1126 uint32_t ip_address
;
1127 uint32_t subnet_mask
= 0;
1130 uint8_t subnet_bits
= 24;
1133 jlink_config_ip_dump(CMD_CTX
, &jlink_cfg
);
1137 ret
= string_to_ip(CMD_ARGV
[0], (uint8_t *)&ip_address
, &i
);
1138 if (ret
!= ERROR_OK
)
1141 len
= strlen(CMD_ARGV
[0]);
1143 /* check for this format A.B.C.D/E */
1146 if (CMD_ARGV
[0][i
] != '/')
1147 return ERROR_COMMAND_SYNTAX_ERROR
;
1149 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1152 ret
= string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
);
1153 if (ret
!= ERROR_OK
)
1159 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1160 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1162 cpy_ip(jlink_cfg
.ip_address
, (uint8_t *)&ip_address
);
1163 cpy_ip(jlink_cfg
.subnet_mask
, (uint8_t *)&subnet_mask
);
1168 COMMAND_HANDLER(jlink_handle_jlink_reset_command
)
1170 memset(&jlink_cfg
, 0xff, sizeof(jlink_cfg
));
1174 COMMAND_HANDLER(jlink_handle_jlink_save_command
)
1176 if (!(jlink_caps
& (1 << EMU_CAP_WRITE_CONFIG
))) {
1177 command_print(CMD_CTX
, "J-Link write emulator configuration not supported");
1181 command_print(CMD_CTX
, "The J-Link need to be unpluged and repluged ta have the config effective");
1182 return jlink_set_config(&jlink_cfg
);
1185 COMMAND_HANDLER(jlink_handle_jlink_usb_address_command
)
1190 jlink_config_usb_address_dump(CMD_CTX
, &jlink_cfg
);
1194 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
1196 if (address
> 0x3 && address
!= 0xff) {
1197 command_print(CMD_CTX
, "USB Address must be between 0x00 and 0x03 or 0xff");
1198 return ERROR_COMMAND_SYNTAX_ERROR
;
1201 jlink_cfg
.usb_address
= address
;
1205 COMMAND_HANDLER(jlink_handle_jlink_config_command
)
1207 struct jlink_config cfg
;
1210 if (CMD_ARGC
== 0) {
1211 if (!(jlink_caps
& (1 << EMU_CAP_READ_CONFIG
))) {
1212 command_print(CMD_CTX
, "J-Link read emulator configuration not supported");
1216 ret
= jlink_get_config(&cfg
);
1218 if (ret
!= ERROR_OK
)
1219 command_print(CMD_CTX
, "J-Link read emulator configuration failled");
1221 jlink_config_dump(CMD_CTX
, &jlink_cfg
);
1228 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1230 .name
= "kickstart",
1231 .handler
= &jlink_handle_jlink_kickstart_command
,
1232 .mode
= COMMAND_EXEC
,
1233 .help
= "set Kickstart power on JTAG-pin 19.",
1237 .name
= "mac_address",
1238 .handler
= &jlink_handle_jlink_mac_address_command
,
1239 .mode
= COMMAND_EXEC
,
1240 .help
= "set the MAC Address",
1241 .usage
= "[ff:ff:ff:ff:ff:ff]",
1245 .handler
= &jlink_handle_jlink_ip_command
,
1246 .mode
= COMMAND_EXEC
,
1247 .help
= "set the ip address of the J-Link Pro, "
1248 "where A.B.C.D is the ip, "
1249 "E the bit of the subnet mask, "
1250 "F.G.H.I the subnet mask",
1251 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1255 .handler
= &jlink_handle_jlink_reset_command
,
1256 .mode
= COMMAND_EXEC
,
1257 .help
= "reset the current config",
1261 .handler
= &jlink_handle_jlink_save_command
,
1262 .mode
= COMMAND_EXEC
,
1263 .help
= "save the current config",
1266 .name
= "usb_address",
1267 .handler
= &jlink_handle_jlink_usb_address_command
,
1268 .mode
= COMMAND_EXEC
,
1269 .help
= "set the USB-Address, "
1270 "This will change the product id",
1271 .usage
= "[0x00 to 0x03 or 0xff]",
1273 COMMAND_REGISTRATION_DONE
1276 static const struct command_registration jlink_subcommand_handlers
[] = {
1279 .handler
= &jlink_handle_jlink_caps_command
,
1280 .mode
= COMMAND_EXEC
,
1281 .help
= "show jlink capabilities",
1285 .handler
= &jlink_handle_jlink_info_command
,
1286 .mode
= COMMAND_EXEC
,
1287 .help
= "show jlink info",
1291 .handler
= &jlink_handle_jlink_hw_jtag_command
,
1292 .mode
= COMMAND_EXEC
,
1293 .help
= "access J-Link HW JTAG command version",
1298 .handler
= &jlink_handle_jlink_config_command
,
1299 .mode
= COMMAND_EXEC
,
1300 .help
= "access J-Link configuration, "
1301 "if no argument this will dump the config",
1302 .chain
= jlink_config_subcommand_handlers
,
1306 .handler
= &jlink_pid_command
,
1307 .mode
= COMMAND_CONFIG
,
1308 .help
= "set the pid of the interface we want to use",
1310 COMMAND_REGISTRATION_DONE
1313 static const struct command_registration jlink_command_handlers
[] = {
1316 .mode
= COMMAND_ANY
,
1317 .help
= "perform jlink management",
1318 .chain
= jlink_subcommand_handlers
,
1320 COMMAND_REGISTRATION_DONE
1323 static int jlink_swd_init(void)
1325 LOG_INFO("JLink SWD mode enabled");
1331 static void jlink_swd_write_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t value
)
1333 assert(!(cmd
& SWD_CMD_RnW
));
1334 jlink_swd_queue_cmd(dap
, cmd
, NULL
, value
);
1337 static void jlink_swd_read_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *value
)
1339 assert(cmd
& SWD_CMD_RnW
);
1340 jlink_swd_queue_cmd(dap
, cmd
, value
, 0);
1343 static int_least32_t jlink_swd_frequency(struct adiv5_dap
*dap
, int_least32_t hz
)
1351 static const struct swd_driver jlink_swd
= {
1352 .init
= jlink_swd_init
,
1353 .frequency
= jlink_swd_frequency
,
1354 .switch_seq
= jlink_swd_switch_seq
,
1355 .read_reg
= jlink_swd_read_reg
,
1356 .write_reg
= jlink_swd_write_reg
,
1357 .run
= jlink_swd_run_queue
,
1360 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
1362 struct jtag_interface jlink_interface
= {
1364 .commands
= jlink_command_handlers
,
1365 .transports
= jlink_transports
,
1368 .execute_queue
= jlink_execute_queue
,
1369 .speed
= jlink_speed
,
1370 .speed_div
= jlink_speed_div
,
1376 /***************************************************************************/
1377 /* J-Link tap functions */
1380 static unsigned tap_length
;
1381 /* In SWD mode use tms buffer for direction control */
1382 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1383 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1384 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1386 struct pending_scan_result
{
1387 int first
; /* First bit position in tdo_buffer to read */
1388 int length
; /* Number of bits to read */
1389 struct scan_command
*command
; /* Corresponding scan command */
1393 #define MAX_PENDING_SCAN_RESULTS 256
1395 static int pending_scan_results_length
;
1396 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1398 static void jlink_tap_init(void)
1401 pending_scan_results_length
= 0;
1404 static void jlink_tap_ensure_space(int scans
, int bits
)
1406 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1407 int available_bits
= JLINK_TAP_BUFFER_SIZE
* 8 - tap_length
- 32;
1409 if (scans
> available_scans
|| bits
> available_bits
)
1410 jlink_tap_execute();
1413 static void jlink_tap_append_step(int tms
, int tdi
)
1415 int index_var
= tap_length
/ 8;
1417 assert(index_var
< JLINK_TAP_BUFFER_SIZE
);
1419 int bit_index
= tap_length
% 8;
1420 uint8_t bit
= 1 << bit_index
;
1422 /* we do not pad TMS, so be sure to initialize all bits */
1424 tms_buffer
[index_var
] = tdi_buffer
[index_var
] = 0;
1427 tms_buffer
[index_var
] |= bit
;
1429 tms_buffer
[index_var
] &= ~bit
;
1432 tdi_buffer
[index_var
] |= bit
;
1434 tdi_buffer
[index_var
] &= ~bit
;
1439 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
1440 struct scan_command
*command
)
1442 struct pending_scan_result
*pending_scan_result
=
1443 &pending_scan_results_buffer
[pending_scan_results_length
];
1446 pending_scan_result
->first
= tap_length
;
1447 pending_scan_result
->length
= length
;
1448 pending_scan_result
->command
= command
;
1449 pending_scan_result
->buffer
= buffer
;
1451 for (i
= 0; i
< length
; i
++) {
1452 int tms
= (i
< (length
- 1)) ? 0 : 1;
1453 int tdi
= (buffer
[i
/ 8] & (1 << (i
% 8))) != 0;
1454 jlink_tap_append_step(tms
, tdi
);
1456 pending_scan_results_length
++;
1459 /* Pad and send a tap sequence to the device, and receive the answer.
1460 * For the purpose of padding we assume that we are in idle or pause state. */
1461 static int jlink_tap_execute(void)
1470 /* JLink returns an extra NULL in packet when size of incoming
1471 * message is a multiple of 64, creates problems with USB comms.
1472 * WARNING: This will interfere with tap state counting. */
1473 while ((DIV_ROUND_UP(tap_length
, 8) % 64) == 0)
1474 jlink_tap_append_step((tap_get_state() == TAP_RESET
) ? 1 : 0, 0);
1476 /* number of full bytes (plus one if some would be left over) */
1477 byte_length
= DIV_ROUND_UP(tap_length
, 8);
1479 bool use_jtag3
= jlink_hw_jtag_version
>= 3;
1480 usb_out_buffer
[0] = use_jtag3
? EMU_CMD_HW_JTAG3
: EMU_CMD_HW_JTAG2
;
1481 usb_out_buffer
[1] = 0;
1482 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1483 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1484 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1485 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1487 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
1488 tap_length
, jlink_last_state
);
1490 result
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1491 use_jtag3
? byte_length
+ 1 : byte_length
);
1492 if (result
!= ERROR_OK
) {
1493 LOG_ERROR("jlink_tap_execute failed USB io (%d)", result
);
1495 return ERROR_JTAG_QUEUE_FAILED
;
1498 result
= use_jtag3
? usb_in_buffer
[byte_length
] : 0;
1500 LOG_ERROR("jlink_tap_execute failed, result %d (%s)", result
,
1501 result
== 1 ? "adaptive clocking timeout" : "unknown");
1503 return ERROR_JTAG_QUEUE_FAILED
;
1506 memcpy(tdo_buffer
, usb_in_buffer
, byte_length
);
1508 for (i
= 0; i
< pending_scan_results_length
; i
++) {
1509 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1510 uint8_t *buffer
= pending_scan_result
->buffer
;
1511 int length
= pending_scan_result
->length
;
1512 int first
= pending_scan_result
->first
;
1513 struct scan_command
*command
= pending_scan_result
->command
;
1515 /* Copy to buffer */
1516 buf_set_buf(tdo_buffer
, first
, buffer
, 0, length
);
1518 DEBUG_JTAG_IO("pending scan result, length = %d", length
);
1520 jlink_debug_buffer(buffer
, DIV_ROUND_UP(length
, 8));
1522 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
) {
1524 return ERROR_JTAG_QUEUE_FAILED
;
1527 if (pending_scan_result
->buffer
!= NULL
)
1528 free(pending_scan_result
->buffer
);
1535 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
1537 unsigned int tap_pos
= tap_length
;
1540 buf_set_u32(buf
, tap_pos
, 32, val
);
1545 buf_set_u32(buf
, tap_pos
, len
, val
);
1548 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
1550 const uint32_t dir_out
= 0xffffffff;
1553 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
1555 fill_buffer(tdi_buffer
, 0, len
);
1556 fill_buffer(tms_buffer
, dir_out
, len
);
1560 static void jlink_queue_data_in(uint32_t len
)
1562 const uint32_t dir_in
= 0;
1564 fill_buffer(tms_buffer
, dir_in
, len
);
1568 static int jlink_swd_switch_seq(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
1575 LOG_DEBUG("SWD line reset");
1576 s
= swd_seq_line_reset
;
1577 s_len
= swd_seq_line_reset_len
;
1580 LOG_DEBUG("JTAG-to-SWD");
1581 s
= swd_seq_jtag_to_swd
;
1582 s_len
= swd_seq_jtag_to_swd_len
;
1585 LOG_DEBUG("SWD-to-JTAG");
1586 s
= swd_seq_swd_to_jtag
;
1587 s_len
= swd_seq_swd_to_jtag_len
;
1590 LOG_ERROR("Sequence %d not supported", seq
);
1594 jlink_queue_data_out(s
, s_len
);
1599 static int jlink_swd_run_queue(struct adiv5_dap
*dap
)
1601 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length
);
1604 if (queued_retval
!= ERROR_OK
) {
1605 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
1609 /* A transaction must be followed by another transaction or at least 8 idle cycles to
1610 * ensure that data is clocked through the AP. */
1611 jlink_queue_data_out(NULL
, 8);
1613 size_t byte_length
= DIV_ROUND_UP(tap_length
, 8);
1615 /* There's a comment in jlink_tap_execute saying JLink returns
1616 * an extra NULL in packet when size of incoming message is a
1617 * multiple of 64. Someone should verify if that's still the
1618 * case with the current jlink firmware */
1620 usb_out_buffer
[0] = EMU_CMD_HW_JTAG3
;
1621 usb_out_buffer
[1] = 0;
1622 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1623 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1624 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1625 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1627 retval
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1629 if (retval
!= ERROR_OK
) {
1630 LOG_ERROR("jlink_swd_run_queue failed USB io (%d)", retval
);
1634 retval
= usb_in_buffer
[byte_length
];
1636 LOG_ERROR("jlink_swd_run_queue failed, result %d", retval
);
1640 for (int i
= 0; i
< pending_scan_results_length
; i
++) {
1641 int ack
= buf_get_u32(usb_in_buffer
, pending_scan_results_buffer
[i
].first
, 3);
1643 if (ack
!= SWD_ACK_OK
) {
1644 LOG_ERROR("SWD ack not OK: %d %s", ack
,
1645 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
1646 queued_retval
= ack
;
1648 } else if (pending_scan_results_buffer
[i
].length
) {
1649 uint32_t data
= buf_get_u32(usb_in_buffer
, 3 + pending_scan_results_buffer
[i
].first
, 32);
1650 int parity
= buf_get_u32(usb_in_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
1652 if (parity
!= parity_u32(data
)) {
1653 LOG_ERROR("SWD Read data parity mismatch");
1654 queued_retval
= ERROR_FAIL
;
1658 if (pending_scan_results_buffer
[i
].buffer
)
1659 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
1665 retval
= queued_retval
;
1666 queued_retval
= ERROR_OK
;
1671 static void jlink_swd_queue_cmd(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *dst
, uint32_t data
)
1673 uint8_t data_parity_trn
[DIV_ROUND_UP(32 + 1, 8)];
1674 if (tap_length
+ 46 + 8 + dap
->memaccess_tck
>= sizeof(tdi_buffer
) * 8 ||
1675 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
1676 /* Not enough room in the queue. Run the queue. */
1677 queued_retval
= jlink_swd_run_queue(dap
);
1680 if (queued_retval
!= ERROR_OK
)
1683 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
1685 jlink_queue_data_out(&cmd
, 8);
1687 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
1689 if (cmd
& SWD_CMD_RnW
) {
1690 /* Queue a read transaction */
1691 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
1692 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
1694 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
1696 /* Queue a write transaction */
1697 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
1698 jlink_queue_data_in(1 + 3 + 1);
1700 buf_set_u32(data_parity_trn
, 0, 32, data
);
1701 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
1703 jlink_queue_data_out(data_parity_trn
, 32 + 1);
1706 pending_scan_results_length
++;
1708 /* Insert idle cycles after AP accesses to avoid WAIT */
1709 if (cmd
& SWD_CMD_APnDP
)
1710 jlink_queue_data_out(NULL
, dap
->memaccess_tck
);
1713 /*****************************************************************************/
1714 /* JLink USB low-level functions */
1716 static struct jlink
*jlink_usb_open()
1718 struct jtag_libusb_device_handle
*devh
;
1719 if (jtag_libusb_open(vids
, pids
, &devh
) != ERROR_OK
)
1722 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
1723 * AREA!!!!!!!!!!! The behavior of libusb is not completely
1724 * consistent across Windows, Linux, and Mac OS X platforms.
1725 * The actions taken in the following compiler conditionals may
1726 * not agree with published documentation for libusb, but were
1727 * found to be necessary through trials and tribulations. Even
1728 * little tweaks can break one or more platforms, so if you do
1729 * make changes test them carefully on all platforms before
1735 jtag_libusb_reset_device(devh
);
1740 /* reopen jlink after usb_reset
1741 * on win32 this may take a second or two to re-enumerate */
1743 while ((retval
= jtag_libusb_open(vids
, pids
, &devh
)) != ERROR_OK
) {
1749 if (ERROR_OK
!= retval
)
1755 /* usb_set_configuration required under win32 */
1756 struct jtag_libusb_device
*udev
= jtag_libusb_get_device(devh
);
1757 jtag_libusb_set_configuration(devh
, 0);
1758 jtag_libusb_claim_interface(devh
, 0);
1762 * This makes problems under Mac OS X. And is not needed
1763 * under Windows. Hopefully this will not break a linux build
1765 usb_set_altinterface(result
->usb_handle
, 0);
1768 /* Use the OB endpoints if the JLink we matched is a Jlink-OB adapter */
1769 uint16_t matched_pid
;
1770 if (jtag_libusb_get_pid(udev
, &matched_pid
) == ERROR_OK
) {
1771 if (matched_pid
== JLINK_OB_PID
) {
1772 jlink_read_ep
= JLINK_OB_WRITE_ENDPOINT
;
1773 jlink_write_ep
= JLINK_OB_READ_ENDPOINT
;
1777 jtag_libusb_get_endpoints(udev
, &jlink_read_ep
, &jlink_write_ep
);
1779 struct jlink
*result
= malloc(sizeof(struct jlink
));
1780 result
->usb_handle
= devh
;
1784 static void jlink_usb_close(struct jlink
*jlink
)
1786 jtag_libusb_close(jlink
->usb_handle
);
1790 /* Send a message and receive the reply. */
1791 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
)
1795 result
= jlink_usb_write(jlink
, out_length
);
1796 if (result
!= out_length
) {
1797 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1798 out_length
, result
);
1799 return ERROR_JTAG_DEVICE_ERROR
;
1802 result
= jlink_usb_read(jlink
, in_length
);
1803 if (result
!= in_length
) {
1804 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1806 return ERROR_JTAG_DEVICE_ERROR
;
1811 /* calls the given usb_bulk_* function, allowing for the data to
1812 * trickle in with some timeouts */
1813 static int usb_bulk_with_retries(
1814 int (*f
)(jtag_libusb_device_handle
*, int, char *, int, int),
1815 jtag_libusb_device_handle
*dev
, int ep
,
1816 char *bytes
, int size
, int timeout
)
1818 int tries
= 3, count
= 0;
1820 while (tries
&& (count
< size
)) {
1821 int result
= f(dev
, ep
, bytes
+ count
, size
- count
, timeout
);
1824 else if ((-ETIMEDOUT
!= result
) || !--tries
)
1830 static int wrap_usb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
,
1831 char *buff
, int size
, int timeout
)
1833 /* usb_bulk_write() takes const char *buff */
1834 return jtag_libusb_bulk_write(dev
, ep
, buff
, size
, timeout
);
1837 static inline int usb_bulk_write_ex(jtag_libusb_device_handle
*dev
, int ep
,
1838 char *bytes
, int size
, int timeout
)
1840 return usb_bulk_with_retries(&wrap_usb_bulk_write
,
1841 dev
, ep
, bytes
, size
, timeout
);
1844 static inline int usb_bulk_read_ex(jtag_libusb_device_handle
*dev
, int ep
,
1845 char *bytes
, int size
, int timeout
)
1847 return usb_bulk_with_retries(&jtag_libusb_bulk_read
,
1848 dev
, ep
, bytes
, size
, timeout
);
1851 /* Write data from out_buffer to USB. */
1852 static int jlink_usb_write(struct jlink
*jlink
, int out_length
)
1856 if (out_length
> JLINK_OUT_BUFFER_SIZE
) {
1857 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
1858 out_length
, JLINK_OUT_BUFFER_SIZE
);
1862 result
= usb_bulk_write_ex(jlink
->usb_handle
, jlink_write_ep
,
1863 (char *)usb_out_buffer
, out_length
, JLINK_USB_TIMEOUT
);
1865 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
1866 out_length
, result
);
1868 jlink_debug_buffer(usb_out_buffer
, out_length
);
1872 /* Read data from USB into in_buffer. */
1873 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
)
1875 int result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1876 (char *)usb_in_buffer
, expected_size
, JLINK_USB_TIMEOUT
);
1878 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result
);
1880 jlink_debug_buffer(usb_in_buffer
, result
);
1885 * Send a message and receive the reply - simple messages.
1887 * @param jlink pointer to driver data
1888 * @param out_length data length in @c usb_out_buffer
1889 * @param in_length data length to be read to @c usb_in_buffer
1891 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
)
1895 result
= jlink_usb_write(jlink
, out_length
);
1896 if (result
!= out_length
) {
1897 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1898 out_length
, result
);
1899 return ERROR_JTAG_DEVICE_ERROR
;
1902 result
= jlink_usb_read(jlink
, in_length
);
1903 if (result
!= in_length
) {
1904 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1906 return ERROR_JTAG_DEVICE_ERROR
;
1910 * Section 4.2.4 IN-transaction:
1911 * read dummy 0-byte packet if transaction size is
1912 * multiple of 64 bytes but not max. size of 0x8000
1914 if ((in_length
% 64) == 0 && in_length
!= 0x8000) {
1916 result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1917 &dummy_buffer
, 1, JLINK_USB_TIMEOUT
);
1919 LOG_ERROR("dummy byte read failed");
1920 return ERROR_JTAG_DEVICE_ERROR
;
1926 #ifdef _DEBUG_USB_COMMS_
1927 #define BYTES_PER_LINE 16
1929 static void jlink_debug_buffer(uint8_t *buffer
, int length
)
1936 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
1937 snprintf(line
, 5, "%04x", i
);
1938 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
1939 snprintf(s
, 4, " %02x", buffer
[j
]);
1942 LOG_DEBUG("%s", line
);