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_USB_INTERFACE_CLASS 0xff
51 #define JLINK_USB_INTERFACE_SUBCLASS 0xff
52 #define JLINK_USB_INTERFACE_PROTOCOL 0xff
54 static unsigned int jlink_write_ep
;
55 static unsigned int jlink_read_ep
;
56 static unsigned int jlink_hw_jtag_version
= 2;
58 #define JLINK_USB_TIMEOUT 1000
60 /* See Section 3.3.2 of the Segger JLink USB protocol manual */
61 /* 2048 is the max value we can use here */
62 #define JLINK_TAP_BUFFER_SIZE 2048
63 /*#define JLINK_TAP_BUFFER_SIZE 256*/
64 /*#define JLINK_TAP_BUFFER_SIZE 384*/
66 #define JLINK_IN_BUFFER_SIZE (2048 + 1)
67 #define JLINK_OUT_BUFFER_SIZE (2*2048 + 4)
69 /* Global USB buffers */
70 static uint8_t usb_in_buffer
[JLINK_IN_BUFFER_SIZE
];
71 static uint8_t usb_out_buffer
[JLINK_OUT_BUFFER_SIZE
];
73 /* Constants for JLink command */
74 #define EMU_CMD_VERSION 0x01
75 #define EMU_CMD_RESET_TRST 0x02
76 #define EMU_CMD_RESET_TARGET 0x03
77 #define EMU_CMD_SET_SPEED 0x05
78 #define EMU_CMD_GET_STATE 0x07
79 #define EMU_CMD_SET_KS_POWER 0x08
80 #define EMU_CMD_REGISTER 0x09
81 #define EMU_CMD_GET_SPEEDS 0xc0
82 #define EMU_CMD_GET_HW_INFO 0xc1
83 #define EMU_CMD_GET_COUNTERS 0xc2
84 #define EMU_CMD_SELECT_IF 0xc7
85 #define EMU_CMD_HW_CLOCK 0xc8
86 #define EMU_CMD_HW_TMS0 0xc9
87 #define EMU_CMD_HW_TMS1 0xca
88 #define EMU_CMD_HW_DATA0 0xcb
89 #define EMU_CMD_HW_DATA1 0xcc
90 #define EMU_CMD_HW_JTAG 0xcd
91 #define EMU_CMD_HW_JTAG2 0xce
92 #define EMU_CMD_HW_JTAG3 0xcf
93 #define EMU_CMD_HW_RELEASE_RESET_STOP_EX 0xd0
94 #define EMU_CMD_HW_RELEASE_RESET_STOP_TIMED 0xd1
95 #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
96 #define EMU_CMD_HW_JTAG_WRITE 0xd5
97 #define EMU_CMD_HW_JTAG_GET_RESULT 0xd6
98 #define EMU_CMD_HW_RESET0 0xdc
99 #define EMU_CMD_HW_RESET1 0xdd
100 #define EMU_CMD_HW_TRST0 0xde
101 #define EMU_CMD_HW_TRST1 0xdf
102 #define EMU_CMD_GET_CAPS 0xe8
103 #define EMU_CMD_GET_CPU_CAPS 0xe9
104 #define EMU_CMD_EXEC_CPU_CMD 0xea
105 #define EMU_CMD_GET_CAPS_EX 0xed
106 #define EMU_CMD_GET_HW_VERSION 0xf0
107 #define EMU_CMD_WRITE_DCC 0xf1
108 #define EMU_CMD_READ_CONFIG 0xf2
109 #define EMU_CMD_WRITE_CONFIG 0xf3
110 #define EMU_CMD_WRITE_MEM 0xf4
111 #define EMU_CMD_READ_MEM 0xf5
112 #define EMU_CMD_MEASURE_RTCK_REACT 0xf6
113 #define EMU_CMD_WRITE_MEM_ARM79 0xf7
114 #define EMU_CMD_READ_MEM_ARM79 0xf8
116 /* Register subcommands */
117 #define REG_CMD_REGISTER 100
118 #define REG_CMD_UNREGISTER 101
120 /* bits return from EMU_CMD_GET_CAPS */
121 #define EMU_CAP_RESERVED_1 0
122 #define EMU_CAP_GET_HW_VERSION 1
123 #define EMU_CAP_WRITE_DCC 2
124 #define EMU_CAP_ADAPTIVE_CLOCKING 3
125 #define EMU_CAP_READ_CONFIG 4
126 #define EMU_CAP_WRITE_CONFIG 5
127 #define EMU_CAP_TRACE 6
128 #define EMU_CAP_WRITE_MEM 7
129 #define EMU_CAP_READ_MEM 8
130 #define EMU_CAP_SPEED_INFO 9
131 #define EMU_CAP_EXEC_CODE 10
132 #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
133 #define EMU_CAP_GET_HW_INFO 12
134 #define EMU_CAP_SET_KS_POWER 13
135 #define EMU_CAP_RESET_STOP_TIMED 14
136 #define EMU_CAP_RESERVED_2 15
137 #define EMU_CAP_MEASURE_RTCK_REACT 16
138 #define EMU_CAP_SELECT_IF 17
139 #define EMU_CAP_RW_MEM_ARM79 18
140 #define EMU_CAP_GET_COUNTERS 19
141 #define EMU_CAP_READ_DCC 20
142 #define EMU_CAP_GET_CPU_CAPS 21
143 #define EMU_CAP_EXEC_CPU_CMD 22
144 #define EMU_CAP_SWO 23
145 #define EMU_CAP_WRITE_DCC_EX 24
146 #define EMU_CAP_UPDATE_FIRMWARE_EX 25
147 #define EMU_CAP_FILE_IO 26
148 #define EMU_CAP_REGISTER 27
149 #define EMU_CAP_INDICATORS 28
150 #define EMU_CAP_TEST_NET_SPEED 29
151 #define EMU_CAP_RAWTRACE 30
152 #define EMU_CAP_RESERVED_3 31
154 static const char * const jlink_cap_str
[] = {
156 "Supports command EMU_CMD_GET_HARDWARE_VERSION",
157 "Supports command EMU_CMD_WRITE_DCC",
158 "Supports adaptive clocking",
159 "Supports command EMU_CMD_READ_CONFIG",
160 "Supports command EMU_CMD_WRITE_CONFIG",
161 "Supports trace commands",
162 "Supports command EMU_CMD_WRITE_MEM",
163 "Supports command EMU_CMD_READ_MEM",
164 "Supports command EMU_CMD_GET_SPEED",
165 "Supports command EMU_CMD_CODE_...",
166 "Supports command EMU_CMD_GET_MAX_BLOCK_SIZE",
167 "Supports command EMU_CMD_GET_HW_INFO",
168 "Supports command EMU_CMD_SET_KS_POWER",
169 "Supports command EMU_CMD_HW_RELEASE_RESET_STOP_TIMED",
171 "Supports command EMU_CMD_MEASURE_RTCK_REACT",
172 "Supports command EMU_CMD_HW_SELECT_IF",
173 "Supports command EMU_CMD_READ/WRITE_MEM_ARM79",
174 "Supports command EMU_CMD_GET_COUNTERS",
175 "Supports command EMU_CMD_READ_DCC",
176 "Supports command EMU_CMD_GET_CPU_CAPS",
177 "Supports command EMU_CMD_EXEC_CPU_CMD",
178 "Supports command EMU_CMD_SWO",
179 "Supports command EMU_CMD_WRITE_DCC_EX",
180 "Supports command EMU_CMD_UPDATE_FIRMWARE_EX",
181 "Supports command EMU_CMD_FILE_IO",
182 "Supports command EMU_CMD_REGISTER",
183 "Supports command EMU_CMD_INDICATORS",
184 "Supports command EMU_CMD_TEST_NET_SPEED",
185 "Supports command EMU_CMD_RAWTRACE",
189 /* max speed 12MHz v5.0 jlink */
190 #define JLINK_MAX_SPEED 12000
192 /* J-Link hardware versions */
193 #define JLINK_HW_TYPE_JLINK 0
194 #define JLINK_HW_TYPE_JTRACE 1
195 #define JLINK_HW_TYPE_FLASHER 2
196 #define JLINK_HW_TYPE_JLINK_PRO 3
197 #define JLINK_HW_TYPE_JLINK_LITE_ADI 5
198 #define JLINK_HW_TYPE_LPCLINK2 18
200 /* Interface selection */
201 #define JLINK_TIF_JTAG 0
202 #define JLINK_TIF_SWD 1
203 #define JLINK_SWD_DIR_IN 0
204 #define JLINK_SWD_DIR_OUT 1
206 /* Queue command functions */
207 static void jlink_end_state(tap_state_t state
);
208 static void jlink_state_move(void);
209 static void jlink_path_move(int num_states
, tap_state_t
*path
);
210 static void jlink_runtest(int num_cycles
);
211 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
212 int scan_size
, struct scan_command
*command
);
213 static void jlink_reset(int trst
, int srst
);
214 static void jlink_simple_command(uint8_t command
);
215 static int jlink_get_status(void);
216 static int jlink_swd_run_queue(struct adiv5_dap
*dap
);
217 static void jlink_swd_queue_cmd(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *dst
, uint32_t data
);
218 static int jlink_swd_switch_seq(struct adiv5_dap
*dap
, enum swd_special_seq seq
);
220 /* J-Link tap buffer functions */
221 static void jlink_tap_init(void);
222 static int jlink_tap_execute(void);
223 static void jlink_tap_ensure_space(int scans
, int bits
);
224 static void jlink_tap_append_step(int tms
, int tdi
);
225 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
226 struct scan_command
*command
);
228 /* Jlink lowlevel functions */
230 struct jtag_libusb_device_handle
*usb_handle
;
233 static struct jlink
*jlink_usb_open(void);
234 static void jlink_usb_close(struct jlink
*jlink
);
235 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
);
236 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
);
237 static int jlink_usb_write(struct jlink
*jlink
, int out_length
);
238 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
);
240 /* helper functions */
241 static int jlink_get_version_info(void);
243 #ifdef _DEBUG_USB_COMMS_
244 static void jlink_debug_buffer(uint8_t *buffer
, int length
);
246 static inline void jlink_debug_buffer(uint8_t *buffer
, int length
)
251 static enum tap_state jlink_last_state
= TAP_RESET
;
253 static struct jlink
*jlink_handle
;
255 /* pid could be specified at runtime */
256 static uint16_t vids
[] = { 0x1366, 0x1366, 0x1366, 0x1366, 0x1366, 0 };
257 static uint16_t pids
[] = { 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0 };
259 static char *jlink_serial
;
261 static uint32_t jlink_caps
;
262 static uint32_t jlink_hw_type
;
264 static int queued_retval
;
265 static bool swd_mode
;
267 /* 256 byte non-volatile memory */
268 struct jlink_config
{
270 /* 0ffset 0x01 to 0x03 */
271 uint8_t reserved_1
[3];
272 uint32_t kickstart_power_on_jtag_pin_19
;
273 /* 0ffset 0x08 to 0x1f */
274 uint8_t reserved_2
[24];
275 /* IP only for J-Link Pro */
276 uint8_t ip_address
[4];
277 uint8_t subnet_mask
[4];
278 /* 0ffset 0x28 to 0x2f */
279 uint8_t reserved_3
[8];
280 uint8_t mac_address
[6];
281 /* 0ffset 0x36 to 0xff */
282 uint8_t reserved_4
[202];
283 } __attribute__ ((packed
));
284 struct jlink_config jlink_cfg
;
286 /***************************************************************************/
287 /* External interface implementation */
289 static void jlink_execute_runtest(struct jtag_command
*cmd
)
291 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
292 cmd
->cmd
.runtest
->num_cycles
,
293 cmd
->cmd
.runtest
->end_state
);
295 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
297 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
300 static void jlink_execute_statemove(struct jtag_command
*cmd
)
302 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
304 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
308 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
310 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
311 cmd
->cmd
.pathmove
->num_states
,
312 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
314 jlink_path_move(cmd
->cmd
.pathmove
->num_states
,
315 cmd
->cmd
.pathmove
->path
);
318 static void jlink_execute_scan(struct jtag_command
*cmd
)
324 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd
->cmd
.scan
->end_state
));
326 jlink_end_state(cmd
->cmd
.scan
->end_state
);
328 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
329 DEBUG_JTAG_IO("scan input, length = %d", scan_size
);
331 jlink_debug_buffer(buffer
, (scan_size
+ 7) / 8);
332 type
= jtag_scan_type(cmd
->cmd
.scan
);
333 jlink_scan(cmd
->cmd
.scan
->ir_scan
,
334 type
, buffer
, scan_size
, cmd
->cmd
.scan
);
337 static void jlink_execute_reset(struct jtag_command
*cmd
)
339 DEBUG_JTAG_IO("reset trst: %i srst %i",
340 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
343 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
347 static void jlink_execute_sleep(struct jtag_command
*cmd
)
349 DEBUG_JTAG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
351 jtag_sleep(cmd
->cmd
.sleep
->us
);
354 static void jlink_execute_command(struct jtag_command
*cmd
)
358 jlink_execute_runtest(cmd
);
361 jlink_execute_statemove(cmd
);
364 jlink_execute_pathmove(cmd
);
367 jlink_execute_scan(cmd
);
370 jlink_execute_reset(cmd
);
373 jlink_execute_sleep(cmd
);
376 LOG_ERROR("BUG: unknown JTAG command type encountered");
381 static int jlink_execute_queue(void)
383 struct jtag_command
*cmd
= jtag_command_queue
;
385 while (cmd
!= NULL
) {
386 jlink_execute_command(cmd
);
390 return jlink_tap_execute();
393 /* Sets speed in kHz. */
394 static int jlink_speed(int speed
)
398 if (speed
> JLINK_MAX_SPEED
) {
399 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
400 speed
, JLINK_MAX_SPEED
);
401 speed
= JLINK_MAX_SPEED
;
404 /* check for RTCK setting */
408 usb_out_buffer
[0] = EMU_CMD_SET_SPEED
;
409 usb_out_buffer
[1] = (speed
>> 0) & 0xff;
410 usb_out_buffer
[2] = (speed
>> 8) & 0xff;
412 result
= jlink_usb_write(jlink_handle
, 3);
414 LOG_ERROR("J-Link setting speed failed (%d)", result
);
415 return ERROR_JTAG_DEVICE_ERROR
;
421 static int jlink_speed_div(int speed
, int *khz
)
428 static int jlink_khz(int khz
, int *jtag_speed
)
435 static int jlink_register(void)
438 usb_out_buffer
[0] = EMU_CMD_REGISTER
;
439 usb_out_buffer
[1] = REG_CMD_REGISTER
;
440 /* 2 - 11 is "additional parameter",
441 * 12 - 13 is connection handle, zero initially */
442 memset(&usb_out_buffer
[2], 0, 10 + 2);
444 result
= jlink_usb_write(jlink_handle
, 14);
446 LOG_ERROR("J-Link register write failed (%d)", result
);
447 return ERROR_JTAG_DEVICE_ERROR
;
451 * 0 - 1 connection handle,
452 * 2 - 3 number of information entities,
453 * 4 - 5 size of a single information struct,
454 * 6 - 7 number of additional bytes,
457 * Try to read the whole USB bulk packet
459 result
= jtag_libusb_bulk_read(jlink_handle
->usb_handle
, jlink_read_ep
,
460 (char *)usb_in_buffer
, sizeof(usb_in_buffer
),
463 LOG_ERROR("J-Link register read failed (0 bytes received)");
464 return ERROR_JTAG_DEVICE_ERROR
;
471 * select transport interface
473 * @param iface [0..31] currently: 0=JTAG, 1=SWD
474 * @returns ERROR_OK or ERROR_ code
476 * @pre jlink_handle must be opened
477 * @pre function may be called only for devices, that have
478 * EMU_CAP_SELECT_IF capability enabled
480 static int jlink_select_interface(int iface
)
482 /* According to Segger's document RM08001-R7 Date: October 8, 2010,
483 * http://www.segger.com/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
484 * section 5.5.3 EMU_CMD_SELECT_IF
485 * > SubCmd 1..31 to select interface (0..31)
487 * The table below states:
491 * This obviosly means that to select TIF_JTAG one should write SubCmd = 1.
493 * In fact, JTAG interface operates when SubCmd=0
495 * It looks like a typo in documentation, because interfaces 0..31 could not
496 * be selected by 1..31 range command.
498 assert(iface
>= 0 && iface
< 32);
501 /* get available interfaces */
502 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
503 usb_out_buffer
[1] = 0xff;
505 result
= jlink_usb_io(jlink_handle
, 2, 4);
506 if (result
!= ERROR_OK
) {
507 LOG_ERROR("J-Link query interface failed (%d)", result
);
508 return ERROR_JTAG_DEVICE_ERROR
;
511 uint32_t iface_mask
= buf_get_u32(usb_in_buffer
, 0, 32);
513 if (!(iface_mask
& (1<<iface
))) {
514 LOG_ERROR("J-Link requesting to select unsupported interface (%" PRIx32
")", iface_mask
);
515 return ERROR_JTAG_DEVICE_ERROR
;
518 /* Select interface */
519 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
520 usb_out_buffer
[1] = iface
;
522 result
= jlink_usb_io(jlink_handle
, 2, 4);
523 if (result
!= ERROR_OK
) {
524 LOG_ERROR("J-Link interface select failed (%d)", result
);
525 return ERROR_JTAG_DEVICE_ERROR
;
531 static int jlink_init(void)
535 jlink_handle
= jlink_usb_open();
537 if (jlink_handle
== 0) {
538 LOG_ERROR("Cannot find jlink Interface! Please check "
539 "connection and permissions.");
540 return ERROR_JTAG_INIT_FAILED
;
543 jlink_hw_jtag_version
= 2;
545 if (jlink_get_version_info() == ERROR_OK
) {
546 /* attempt to get status */
550 /* Registration is sometimes necessary for SWD to work */
551 if (jlink_caps
& (1<<EMU_CAP_REGISTER
))
555 * Some versions of Segger's software do not select JTAG interface by default.
557 * Segger recommends to select interface necessarily as a part of init process,
558 * in case any previous session leaves improper interface selected.
561 if (jlink_caps
& (1<<EMU_CAP_SELECT_IF
))
562 retval
= jlink_select_interface(swd_mode
? JLINK_TIF_SWD
: JLINK_TIF_JTAG
);
564 retval
= swd_mode
? ERROR_JTAG_DEVICE_ERROR
: ERROR_OK
;
566 if (retval
!= ERROR_OK
) {
567 LOG_ERROR("Selected transport mode is not supported.");
568 return ERROR_JTAG_INIT_FAILED
;
571 LOG_INFO("J-Link JTAG Interface ready");
577 jlink_speed(jtag_get_speed_khz());
580 /* v5/6 jlink seems to have an issue if the first tap move
581 * is not divisible by 8, so we send a TLR on first power up */
582 for (i
= 0; i
< 8; i
++)
583 jlink_tap_append_step(1, 0);
590 static int jlink_quit(void)
592 jlink_usb_close(jlink_handle
);
596 /***************************************************************************/
597 /* Queue command implementations */
599 static void jlink_end_state(tap_state_t state
)
601 if (tap_is_state_stable(state
))
602 tap_set_end_state(state
);
604 LOG_ERROR("BUG: %i is not a valid end state", state
);
609 /* Goes to the end state. */
610 static void jlink_state_move(void)
614 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
615 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
617 for (i
= 0; i
< tms_scan_bits
; i
++) {
618 tms
= (tms_scan
>> i
) & 1;
619 jlink_tap_append_step(tms
, 0);
622 tap_set_state(tap_get_end_state());
625 static void jlink_path_move(int num_states
, tap_state_t
*path
)
629 for (i
= 0; i
< num_states
; i
++) {
630 if (path
[i
] == tap_state_transition(tap_get_state(), false))
631 jlink_tap_append_step(0, 0);
632 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
633 jlink_tap_append_step(1, 0);
635 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
636 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
640 tap_set_state(path
[i
]);
643 tap_set_end_state(tap_get_state());
646 static void jlink_runtest(int num_cycles
)
650 tap_state_t saved_end_state
= tap_get_end_state();
652 jlink_tap_ensure_space(1, num_cycles
+ 16);
654 /* only do a state_move when we're not already in IDLE */
655 if (tap_get_state() != TAP_IDLE
) {
656 jlink_end_state(TAP_IDLE
);
661 /* execute num_cycles */
662 for (i
= 0; i
< num_cycles
; i
++)
663 jlink_tap_append_step(0, 0);
665 /* finish in end_state */
666 jlink_end_state(saved_end_state
);
667 if (tap_get_state() != tap_get_end_state())
671 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
672 int scan_size
, struct scan_command
*command
)
674 tap_state_t saved_end_state
;
676 jlink_tap_ensure_space(1, scan_size
+ 16);
678 saved_end_state
= tap_get_end_state();
680 /* Move to appropriate scan state */
681 jlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
683 /* Only move if we're not already there */
684 if (tap_get_state() != tap_get_end_state())
687 jlink_end_state(saved_end_state
);
690 jlink_tap_append_scan(scan_size
, buffer
, command
);
692 /* We are in Exit1, go to Pause */
693 jlink_tap_append_step(0, 0);
695 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
697 if (tap_get_state() != tap_get_end_state())
701 static void jlink_reset(int trst
, int srst
)
703 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
705 /* Signals are active low */
707 jlink_simple_command(EMU_CMD_HW_RESET1
);
710 jlink_simple_command(EMU_CMD_HW_RESET0
);
713 jlink_simple_command(EMU_CMD_HW_TRST0
);
716 jlink_simple_command(EMU_CMD_HW_TRST1
);
719 static void jlink_simple_command(uint8_t command
)
723 DEBUG_JTAG_IO("0x%02x", command
);
725 usb_out_buffer
[0] = command
;
726 result
= jlink_usb_write(jlink_handle
, 1);
729 LOG_ERROR("J-Link command 0x%02x failed (%d)", command
, result
);
732 static int jlink_get_status(void)
736 jlink_simple_command(EMU_CMD_GET_STATE
);
738 result
= jlink_usb_read(jlink_handle
, 8);
740 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result
);
741 return ERROR_JTAG_DEVICE_ERROR
;
744 int vref
= usb_in_buffer
[0] + (usb_in_buffer
[1] << 8);
745 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
746 vref
/ 1000, vref
% 1000, \
747 usb_in_buffer
[2], usb_in_buffer
[3], usb_in_buffer
[4], \
748 usb_in_buffer
[5], usb_in_buffer
[6], usb_in_buffer
[7]);
751 LOG_ERROR("Vref too low. Check Target Power");
756 #define jlink_dump_printf(context, expr ...) \
759 command_print(context, expr); \
764 static void jlink_caps_dump(struct command_context
*ctx
)
768 jlink_dump_printf(ctx
, "J-Link Capabilities");
770 for (i
= 1; i
< 31; i
++)
771 if (jlink_caps
& (1 << i
))
772 jlink_dump_printf(ctx
, "%s", jlink_cap_str
[i
]);
775 static void jlink_config_usb_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
780 jlink_dump_printf(ctx
, "USB-Address: 0x%x", cfg
->usb_address
);
783 static void jlink_config_kickstart_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
788 jlink_dump_printf(ctx
, "Kickstart power on JTAG-pin 19: 0x%" PRIx32
,
789 cfg
->kickstart_power_on_jtag_pin_19
);
792 static void jlink_config_mac_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
797 jlink_dump_printf(ctx
, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
798 cfg
->mac_address
[5], cfg
->mac_address
[4],
799 cfg
->mac_address
[3], cfg
->mac_address
[2],
800 cfg
->mac_address
[1], cfg
->mac_address
[0]);
803 static void jlink_config_ip_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
808 jlink_dump_printf(ctx
, "IP Address: %d.%d.%d.%d",
809 cfg
->ip_address
[3], cfg
->ip_address
[2],
810 cfg
->ip_address
[1], cfg
->ip_address
[0]);
811 jlink_dump_printf(ctx
, "Subnet Mask: %d.%d.%d.%d",
812 cfg
->subnet_mask
[3], cfg
->subnet_mask
[2],
813 cfg
->subnet_mask
[1], cfg
->subnet_mask
[0]);
816 static void jlink_config_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
821 jlink_dump_printf(ctx
, "J-Link configuration");
822 jlink_config_usb_address_dump(ctx
, cfg
);
823 jlink_config_kickstart_dump(ctx
, cfg
);
825 if (jlink_hw_type
== JLINK_HW_TYPE_JLINK_PRO
) {
826 jlink_config_ip_dump(ctx
, cfg
);
827 jlink_config_mac_address_dump(ctx
, cfg
);
831 static int jlink_get_config(struct jlink_config
*cfg
)
834 int size
= sizeof(struct jlink_config
);
836 usb_out_buffer
[0] = EMU_CMD_READ_CONFIG
;
837 result
= jlink_usb_io(jlink_handle
, 1, size
);
839 if (result
!= ERROR_OK
) {
840 LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size
, result
);
844 memcpy(cfg
, usb_in_buffer
, size
);
848 static int jlink_set_config(struct jlink_config
*cfg
)
851 int size
= sizeof(struct jlink_config
);
853 jlink_simple_command(EMU_CMD_WRITE_CONFIG
);
855 memcpy(usb_out_buffer
, cfg
, size
);
857 result
= jlink_usb_write(jlink_handle
, size
);
858 if (result
!= size
) {
859 LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result
);
867 * List of unsupported version string markers.
869 * The firmware versions does not correspond directly with
870 * "Software and documentation pack for Windows", it may be
871 * distinguished by the "compile" date in the information string.
873 * For example, version string is:
874 * "J-Link ARM V8 compiled May 3 2012 18:36:22"
878 * The list must be terminated by NULL string.
880 static const char * const unsupported_versions
[] = {
883 NULL
/* End of list */
886 static void jlink_check_supported(const char *str
)
888 const char * const *p
= unsupported_versions
;
890 if (NULL
!= strstr(str
, *p
)) {
892 "Unsupported J-Link firmware version.\n"
893 " Please check http://www.segger.com/j-link-older-versions.html for updates");
900 static int jlink_get_version_info(void)
904 uint32_t jlink_max_size
;
906 /* query hardware version */
907 jlink_simple_command(EMU_CMD_VERSION
);
909 result
= jlink_usb_read(jlink_handle
, 2);
911 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
912 return ERROR_JTAG_DEVICE_ERROR
;
915 len
= buf_get_u32(usb_in_buffer
, 0, 16);
916 if (len
> JLINK_IN_BUFFER_SIZE
) {
917 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len
);
918 len
= JLINK_IN_BUFFER_SIZE
;
921 result
= jlink_usb_read(jlink_handle
, len
);
923 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
924 return ERROR_JTAG_DEVICE_ERROR
;
927 usb_in_buffer
[result
] = 0;
928 LOG_INFO("%s", (char *)usb_in_buffer
);
929 jlink_check_supported((char *)usb_in_buffer
);
931 /* query hardware capabilities */
932 jlink_simple_command(EMU_CMD_GET_CAPS
);
934 result
= jlink_usb_read(jlink_handle
, 4);
936 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result
);
937 return ERROR_JTAG_DEVICE_ERROR
;
940 jlink_caps
= buf_get_u32(usb_in_buffer
, 0, 32);
941 LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps
);
943 if (jlink_caps
& (1 << EMU_CAP_GET_HW_VERSION
)) {
944 /* query hardware version */
945 jlink_simple_command(EMU_CMD_GET_HW_VERSION
);
947 result
= jlink_usb_read(jlink_handle
, 4);
949 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result
);
950 return ERROR_JTAG_DEVICE_ERROR
;
953 uint32_t jlink_hw_version
= buf_get_u32(usb_in_buffer
, 0, 32);
954 uint32_t major_revision
= (jlink_hw_version
/ 10000) % 100;
955 jlink_hw_type
= (jlink_hw_version
/ 1000000) % 100;
956 if (major_revision
>= 5)
957 jlink_hw_jtag_version
= 3;
959 LOG_INFO("J-Link hw version %i", (int)jlink_hw_version
);
961 switch (jlink_hw_type
) {
962 case JLINK_HW_TYPE_JLINK
:
963 LOG_INFO("J-Link hw type J-Link");
965 case JLINK_HW_TYPE_JTRACE
:
966 LOG_INFO("J-Link hw type J-Trace");
968 case JLINK_HW_TYPE_FLASHER
:
969 LOG_INFO("J-Link hw type Flasher");
971 case JLINK_HW_TYPE_JLINK_PRO
:
972 LOG_INFO("J-Link hw type J-Link Pro");
974 case JLINK_HW_TYPE_JLINK_LITE_ADI
:
975 LOG_INFO("J-Link hw type J-Link Lite-ADI");
977 case JLINK_HW_TYPE_LPCLINK2
:
978 LOG_INFO("J-Link hw type J-Link on LPC-Link2");
981 LOG_INFO("J-Link hw type unknown 0x%" PRIx32
, jlink_hw_type
);
986 if (jlink_caps
& (1 << EMU_CAP_GET_MAX_BLOCK_SIZE
)) {
987 /* query hardware maximum memory block */
988 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK
);
990 result
= jlink_usb_read(jlink_handle
, 4);
992 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result
);
993 return ERROR_JTAG_DEVICE_ERROR
;
996 jlink_max_size
= buf_get_u32(usb_in_buffer
, 0, 32);
997 LOG_INFO("J-Link max mem block %i", (int)jlink_max_size
);
1000 if (jlink_caps
& (1 << EMU_CAP_READ_CONFIG
)) {
1001 if (jlink_get_config(&jlink_cfg
) != ERROR_OK
)
1002 return ERROR_JTAG_DEVICE_ERROR
;
1004 jlink_config_dump(NULL
, &jlink_cfg
);
1010 COMMAND_HANDLER(jlink_pid_command
)
1012 if (CMD_ARGC
!= 1) {
1013 LOG_ERROR("Need exactly one argument to jlink_pid");
1017 pids
[0] = strtoul(CMD_ARGV
[0], NULL
, 16);
1024 COMMAND_HANDLER(jlink_serial_command
)
1026 if (CMD_ARGC
!= 1) {
1027 LOG_ERROR("Need exactly one argument to jlink_serial");
1032 jlink_serial
= strdup(CMD_ARGV
[0]);
1037 COMMAND_HANDLER(jlink_handle_jlink_info_command
)
1039 if (jlink_get_version_info() == ERROR_OK
) {
1040 /* attempt to get status */
1047 COMMAND_HANDLER(jlink_handle_jlink_caps_command
)
1049 jlink_caps_dump(CMD_CTX
);
1054 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command
)
1058 command_print(CMD_CTX
, "J-Link hw jtag %i", jlink_hw_jtag_version
);
1061 int request_version
= atoi(CMD_ARGV
[0]);
1062 switch (request_version
) {
1065 jlink_hw_jtag_version
= request_version
;
1068 return ERROR_COMMAND_SYNTAX_ERROR
;
1073 return ERROR_COMMAND_SYNTAX_ERROR
;
1079 COMMAND_HANDLER(jlink_handle_jlink_kickstart_command
)
1084 jlink_config_kickstart_dump(CMD_CTX
, &jlink_cfg
);
1088 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], kickstart
);
1090 jlink_cfg
.kickstart_power_on_jtag_pin_19
= kickstart
;
1094 COMMAND_HANDLER(jlink_handle_jlink_mac_address_command
)
1102 jlink_config_mac_address_dump(CMD_CTX
, &jlink_cfg
);
1108 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' || str
[8] != ':' ||
1109 str
[11] != ':' || str
[14] != ':')) {
1110 command_print(CMD_CTX
, "ethaddr miss format ff:ff:ff:ff:ff:ff");
1111 return ERROR_COMMAND_SYNTAX_ERROR
;
1114 for (i
= 5; i
>= 0; i
--) {
1115 addr
[i
] = strtoul(str
, &e
, 16);
1119 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1120 command_print(CMD_CTX
, "invalid it's zero mac_address");
1121 return ERROR_COMMAND_SYNTAX_ERROR
;
1124 if (!(0x01 & addr
[0])) {
1125 command_print(CMD_CTX
, "invalid it's a multicat mac_address");
1126 return ERROR_COMMAND_SYNTAX_ERROR
;
1129 memcpy(jlink_cfg
.mac_address
, addr
, sizeof(addr
));
1134 static int string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1138 const char *s_save
= s
;
1144 for (i
= 0; i
< 4; i
++) {
1145 lip
[i
] = strtoul(s
, &e
, 10);
1147 if (*e
!= '.' && i
!= 3)
1155 memcpy(ip
, lip
, sizeof(lip
));
1159 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1163 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1167 COMMAND_HANDLER(jlink_handle_jlink_ip_command
)
1169 uint32_t ip_address
;
1170 uint32_t subnet_mask
= 0;
1173 uint8_t subnet_bits
= 24;
1176 jlink_config_ip_dump(CMD_CTX
, &jlink_cfg
);
1180 ret
= string_to_ip(CMD_ARGV
[0], (uint8_t *)&ip_address
, &i
);
1181 if (ret
!= ERROR_OK
)
1184 len
= strlen(CMD_ARGV
[0]);
1186 /* check for this format A.B.C.D/E */
1189 if (CMD_ARGV
[0][i
] != '/')
1190 return ERROR_COMMAND_SYNTAX_ERROR
;
1192 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1195 ret
= string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
);
1196 if (ret
!= ERROR_OK
)
1202 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1203 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1205 cpy_ip(jlink_cfg
.ip_address
, (uint8_t *)&ip_address
);
1206 cpy_ip(jlink_cfg
.subnet_mask
, (uint8_t *)&subnet_mask
);
1211 COMMAND_HANDLER(jlink_handle_jlink_reset_command
)
1213 memset(&jlink_cfg
, 0xff, sizeof(jlink_cfg
));
1217 COMMAND_HANDLER(jlink_handle_jlink_save_command
)
1219 if (!(jlink_caps
& (1 << EMU_CAP_WRITE_CONFIG
))) {
1220 command_print(CMD_CTX
, "J-Link write emulator configuration not supported");
1224 command_print(CMD_CTX
, "The J-Link need to be unpluged and repluged ta have the config effective");
1225 return jlink_set_config(&jlink_cfg
);
1228 COMMAND_HANDLER(jlink_handle_jlink_usb_address_command
)
1233 jlink_config_usb_address_dump(CMD_CTX
, &jlink_cfg
);
1237 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
1239 if (address
> 0x3 && address
!= 0xff) {
1240 command_print(CMD_CTX
, "USB Address must be between 0x00 and 0x03 or 0xff");
1241 return ERROR_COMMAND_SYNTAX_ERROR
;
1244 jlink_cfg
.usb_address
= address
;
1248 COMMAND_HANDLER(jlink_handle_jlink_config_command
)
1250 struct jlink_config cfg
;
1253 if (CMD_ARGC
== 0) {
1254 if (!(jlink_caps
& (1 << EMU_CAP_READ_CONFIG
))) {
1255 command_print(CMD_CTX
, "J-Link read emulator configuration not supported");
1259 ret
= jlink_get_config(&cfg
);
1261 if (ret
!= ERROR_OK
)
1262 command_print(CMD_CTX
, "J-Link read emulator configuration failled");
1264 jlink_config_dump(CMD_CTX
, &jlink_cfg
);
1271 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1273 .name
= "kickstart",
1274 .handler
= &jlink_handle_jlink_kickstart_command
,
1275 .mode
= COMMAND_EXEC
,
1276 .help
= "set Kickstart power on JTAG-pin 19.",
1280 .name
= "mac_address",
1281 .handler
= &jlink_handle_jlink_mac_address_command
,
1282 .mode
= COMMAND_EXEC
,
1283 .help
= "set the MAC Address",
1284 .usage
= "[ff:ff:ff:ff:ff:ff]",
1288 .handler
= &jlink_handle_jlink_ip_command
,
1289 .mode
= COMMAND_EXEC
,
1290 .help
= "set the ip address of the J-Link Pro, "
1291 "where A.B.C.D is the ip, "
1292 "E the bit of the subnet mask, "
1293 "F.G.H.I the subnet mask",
1294 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1298 .handler
= &jlink_handle_jlink_reset_command
,
1299 .mode
= COMMAND_EXEC
,
1300 .help
= "reset the current config",
1304 .handler
= &jlink_handle_jlink_save_command
,
1305 .mode
= COMMAND_EXEC
,
1306 .help
= "save the current config",
1309 .name
= "usb_address",
1310 .handler
= &jlink_handle_jlink_usb_address_command
,
1311 .mode
= COMMAND_EXEC
,
1312 .help
= "set the USB-Address, "
1313 "This will change the product id",
1314 .usage
= "[0x00 to 0x03 or 0xff]",
1316 COMMAND_REGISTRATION_DONE
1319 static const struct command_registration jlink_subcommand_handlers
[] = {
1322 .handler
= &jlink_handle_jlink_caps_command
,
1323 .mode
= COMMAND_EXEC
,
1324 .help
= "show jlink capabilities",
1328 .handler
= &jlink_handle_jlink_info_command
,
1329 .mode
= COMMAND_EXEC
,
1330 .help
= "show jlink info",
1334 .handler
= &jlink_handle_jlink_hw_jtag_command
,
1335 .mode
= COMMAND_EXEC
,
1336 .help
= "access J-Link HW JTAG command version",
1341 .handler
= &jlink_handle_jlink_config_command
,
1342 .mode
= COMMAND_EXEC
,
1343 .help
= "access J-Link configuration, "
1344 "if no argument this will dump the config",
1345 .chain
= jlink_config_subcommand_handlers
,
1349 .handler
= &jlink_pid_command
,
1350 .mode
= COMMAND_CONFIG
,
1351 .help
= "set the pid of the interface we want to use",
1355 .handler
= &jlink_serial_command
,
1356 .mode
= COMMAND_CONFIG
,
1357 .help
= "set the serial number of the J-Link adapter we want to use"
1359 COMMAND_REGISTRATION_DONE
1362 static const struct command_registration jlink_command_handlers
[] = {
1365 .mode
= COMMAND_ANY
,
1366 .help
= "perform jlink management",
1367 .chain
= jlink_subcommand_handlers
,
1369 COMMAND_REGISTRATION_DONE
1372 static int jlink_swd_init(void)
1374 LOG_INFO("JLink SWD mode enabled");
1380 static void jlink_swd_write_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t value
)
1382 assert(!(cmd
& SWD_CMD_RnW
));
1383 jlink_swd_queue_cmd(dap
, cmd
, NULL
, value
);
1386 static void jlink_swd_read_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *value
)
1388 assert(cmd
& SWD_CMD_RnW
);
1389 jlink_swd_queue_cmd(dap
, cmd
, value
, 0);
1392 static int_least32_t jlink_swd_frequency(struct adiv5_dap
*dap
, int_least32_t hz
)
1395 jlink_speed(hz
/ 1000);
1400 static const struct swd_driver jlink_swd
= {
1401 .init
= jlink_swd_init
,
1402 .frequency
= jlink_swd_frequency
,
1403 .switch_seq
= jlink_swd_switch_seq
,
1404 .read_reg
= jlink_swd_read_reg
,
1405 .write_reg
= jlink_swd_write_reg
,
1406 .run
= jlink_swd_run_queue
,
1409 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
1411 struct jtag_interface jlink_interface
= {
1413 .commands
= jlink_command_handlers
,
1414 .transports
= jlink_transports
,
1417 .execute_queue
= jlink_execute_queue
,
1418 .speed
= jlink_speed
,
1419 .speed_div
= jlink_speed_div
,
1425 /***************************************************************************/
1426 /* J-Link tap functions */
1429 static unsigned tap_length
;
1430 /* In SWD mode use tms buffer for direction control */
1431 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1432 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1433 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1435 struct pending_scan_result
{
1436 int first
; /* First bit position in tdo_buffer to read */
1437 int length
; /* Number of bits to read */
1438 struct scan_command
*command
; /* Corresponding scan command */
1442 #define MAX_PENDING_SCAN_RESULTS 256
1444 static int pending_scan_results_length
;
1445 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1447 static void jlink_tap_init(void)
1450 pending_scan_results_length
= 0;
1453 static void jlink_tap_ensure_space(int scans
, int bits
)
1455 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1456 int available_bits
= JLINK_TAP_BUFFER_SIZE
* 8 - tap_length
- 32;
1458 if (scans
> available_scans
|| bits
> available_bits
)
1459 jlink_tap_execute();
1462 static void jlink_tap_append_step(int tms
, int tdi
)
1464 int index_var
= tap_length
/ 8;
1466 assert(index_var
< JLINK_TAP_BUFFER_SIZE
);
1468 int bit_index
= tap_length
% 8;
1469 uint8_t bit
= 1 << bit_index
;
1471 /* we do not pad TMS, so be sure to initialize all bits */
1473 tms_buffer
[index_var
] = tdi_buffer
[index_var
] = 0;
1476 tms_buffer
[index_var
] |= bit
;
1478 tms_buffer
[index_var
] &= ~bit
;
1481 tdi_buffer
[index_var
] |= bit
;
1483 tdi_buffer
[index_var
] &= ~bit
;
1488 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
1489 struct scan_command
*command
)
1491 struct pending_scan_result
*pending_scan_result
=
1492 &pending_scan_results_buffer
[pending_scan_results_length
];
1495 pending_scan_result
->first
= tap_length
;
1496 pending_scan_result
->length
= length
;
1497 pending_scan_result
->command
= command
;
1498 pending_scan_result
->buffer
= buffer
;
1500 for (i
= 0; i
< length
; i
++) {
1501 int tms
= (i
< (length
- 1)) ? 0 : 1;
1502 int tdi
= (buffer
[i
/ 8] & (1 << (i
% 8))) != 0;
1503 jlink_tap_append_step(tms
, tdi
);
1505 pending_scan_results_length
++;
1508 /* Pad and send a tap sequence to the device, and receive the answer.
1509 * For the purpose of padding we assume that we are in idle or pause state. */
1510 static int jlink_tap_execute(void)
1519 /* JLink returns an extra NULL in packet when size of incoming
1520 * message is a multiple of 64, creates problems with USB comms.
1521 * WARNING: This will interfere with tap state counting. */
1522 while ((DIV_ROUND_UP(tap_length
, 8) % 64) == 0)
1523 jlink_tap_append_step((tap_get_state() == TAP_RESET
) ? 1 : 0, 0);
1525 /* number of full bytes (plus one if some would be left over) */
1526 byte_length
= DIV_ROUND_UP(tap_length
, 8);
1528 bool use_jtag3
= jlink_hw_jtag_version
>= 3;
1529 usb_out_buffer
[0] = use_jtag3
? EMU_CMD_HW_JTAG3
: EMU_CMD_HW_JTAG2
;
1530 usb_out_buffer
[1] = 0;
1531 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1532 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1533 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1534 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1536 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
1537 tap_length
, jlink_last_state
);
1539 result
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1540 use_jtag3
? byte_length
+ 1 : byte_length
);
1541 if (result
!= ERROR_OK
) {
1542 LOG_ERROR("jlink_tap_execute failed USB io (%d)", result
);
1544 return ERROR_JTAG_QUEUE_FAILED
;
1547 result
= use_jtag3
? usb_in_buffer
[byte_length
] : 0;
1549 LOG_ERROR("jlink_tap_execute failed, result %d (%s)", result
,
1550 result
== 1 ? "adaptive clocking timeout" : "unknown");
1552 return ERROR_JTAG_QUEUE_FAILED
;
1555 memcpy(tdo_buffer
, usb_in_buffer
, byte_length
);
1557 for (i
= 0; i
< pending_scan_results_length
; i
++) {
1558 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1559 uint8_t *buffer
= pending_scan_result
->buffer
;
1560 int length
= pending_scan_result
->length
;
1561 int first
= pending_scan_result
->first
;
1562 struct scan_command
*command
= pending_scan_result
->command
;
1564 /* Copy to buffer */
1565 buf_set_buf(tdo_buffer
, first
, buffer
, 0, length
);
1567 DEBUG_JTAG_IO("pending scan result, length = %d", length
);
1569 jlink_debug_buffer(buffer
, DIV_ROUND_UP(length
, 8));
1571 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
) {
1573 return ERROR_JTAG_QUEUE_FAILED
;
1576 if (pending_scan_result
->buffer
!= NULL
)
1577 free(pending_scan_result
->buffer
);
1584 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
1586 unsigned int tap_pos
= tap_length
;
1589 buf_set_u32(buf
, tap_pos
, 32, val
);
1594 buf_set_u32(buf
, tap_pos
, len
, val
);
1597 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
1599 const uint32_t dir_out
= 0xffffffff;
1602 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
1604 fill_buffer(tdi_buffer
, 0, len
);
1605 fill_buffer(tms_buffer
, dir_out
, len
);
1609 static void jlink_queue_data_in(uint32_t len
)
1611 const uint32_t dir_in
= 0;
1613 fill_buffer(tms_buffer
, dir_in
, len
);
1617 static int jlink_swd_switch_seq(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
1624 LOG_DEBUG("SWD line reset");
1625 s
= swd_seq_line_reset
;
1626 s_len
= swd_seq_line_reset_len
;
1629 LOG_DEBUG("JTAG-to-SWD");
1630 s
= swd_seq_jtag_to_swd
;
1631 s_len
= swd_seq_jtag_to_swd_len
;
1634 LOG_DEBUG("SWD-to-JTAG");
1635 s
= swd_seq_swd_to_jtag
;
1636 s_len
= swd_seq_swd_to_jtag_len
;
1639 LOG_ERROR("Sequence %d not supported", seq
);
1643 jlink_queue_data_out(s
, s_len
);
1648 static int jlink_swd_run_queue(struct adiv5_dap
*dap
)
1650 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length
);
1653 if (queued_retval
!= ERROR_OK
) {
1654 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
1658 /* A transaction must be followed by another transaction or at least 8 idle cycles to
1659 * ensure that data is clocked through the AP. */
1660 jlink_queue_data_out(NULL
, 8);
1662 size_t byte_length
= DIV_ROUND_UP(tap_length
, 8);
1664 /* There's a comment in jlink_tap_execute saying JLink returns
1665 * an extra NULL in packet when size of incoming message is a
1666 * multiple of 64. Someone should verify if that's still the
1667 * case with the current jlink firmware */
1669 usb_out_buffer
[0] = EMU_CMD_HW_JTAG3
;
1670 usb_out_buffer
[1] = 0;
1671 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1672 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1673 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1674 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1676 retval
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1678 if (retval
!= ERROR_OK
) {
1679 LOG_ERROR("jlink_swd_run_queue failed USB io (%d)", retval
);
1683 retval
= usb_in_buffer
[byte_length
];
1685 LOG_ERROR("jlink_swd_run_queue failed, result %d", retval
);
1689 for (int i
= 0; i
< pending_scan_results_length
; i
++) {
1690 int ack
= buf_get_u32(usb_in_buffer
, pending_scan_results_buffer
[i
].first
, 3);
1692 if (ack
!= SWD_ACK_OK
) {
1693 LOG_DEBUG("SWD ack not OK: %d %s", ack
,
1694 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
1695 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
1697 } else if (pending_scan_results_buffer
[i
].length
) {
1698 uint32_t data
= buf_get_u32(usb_in_buffer
, 3 + pending_scan_results_buffer
[i
].first
, 32);
1699 int parity
= buf_get_u32(usb_in_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
1701 if (parity
!= parity_u32(data
)) {
1702 LOG_ERROR("SWD Read data parity mismatch");
1703 queued_retval
= ERROR_FAIL
;
1707 if (pending_scan_results_buffer
[i
].buffer
)
1708 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
1714 retval
= queued_retval
;
1715 queued_retval
= ERROR_OK
;
1720 static void jlink_swd_queue_cmd(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *dst
, uint32_t data
)
1722 uint8_t data_parity_trn
[DIV_ROUND_UP(32 + 1, 8)];
1723 if (tap_length
+ 46 + 8 + dap
->memaccess_tck
>= sizeof(tdi_buffer
) * 8 ||
1724 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
1725 /* Not enough room in the queue. Run the queue. */
1726 queued_retval
= jlink_swd_run_queue(dap
);
1729 if (queued_retval
!= ERROR_OK
)
1732 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
1734 jlink_queue_data_out(&cmd
, 8);
1736 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
1738 if (cmd
& SWD_CMD_RnW
) {
1739 /* Queue a read transaction */
1740 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
1741 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
1743 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
1745 /* Queue a write transaction */
1746 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
1747 jlink_queue_data_in(1 + 3 + 1);
1749 buf_set_u32(data_parity_trn
, 0, 32, data
);
1750 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
1752 jlink_queue_data_out(data_parity_trn
, 32 + 1);
1755 pending_scan_results_length
++;
1757 /* Insert idle cycles after AP accesses to avoid WAIT */
1758 if (cmd
& SWD_CMD_APnDP
)
1759 jlink_queue_data_out(NULL
, dap
->memaccess_tck
);
1762 /*****************************************************************************/
1763 /* JLink USB low-level functions */
1765 static struct jlink
*jlink_usb_open()
1767 struct jtag_libusb_device_handle
*devh
;
1768 if (jtag_libusb_open(vids
, pids
, jlink_serial
, &devh
) != ERROR_OK
)
1771 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
1772 * AREA!!!!!!!!!!! The behavior of libusb is not completely
1773 * consistent across Windows, Linux, and Mac OS X platforms.
1774 * The actions taken in the following compiler conditionals may
1775 * not agree with published documentation for libusb, but were
1776 * found to be necessary through trials and tribulations. Even
1777 * little tweaks can break one or more platforms, so if you do
1778 * make changes test them carefully on all platforms before
1782 /* This entire block can probably be removed. It was a workaround for
1783 * libusb0.1 and old JLink firmware. It has already be removed for
1784 * windows and causing problems (LPC Link-2 with JLink firmware) on
1785 * Linux with libusb1.0.
1787 * However, for now the behavior will be left unchanged for non-windows
1788 * platforms using libusb0.1 due to lack of testing.
1790 #if IS_WIN32 == 0 && HAVE_LIBUSB1 == 0
1792 jtag_libusb_reset_device(devh
);
1797 /* reopen jlink after usb_reset
1798 * on win32 this may take a second or two to re-enumerate */
1800 while ((retval
= jtag_libusb_open(vids
, pids
, jlink_serial
, &devh
)) != ERROR_OK
) {
1806 if (ERROR_OK
!= retval
)
1812 /* usb_set_configuration is only required under win32
1813 * with libusb 0.1 and libusb0.sys. For libusb 1.0 it is a no-op
1814 * since the configuration is already set. */
1815 jtag_libusb_set_configuration(devh
, 0);
1817 jtag_libusb_choose_interface(devh
, &jlink_read_ep
, &jlink_write_ep
,
1818 JLINK_USB_INTERFACE_CLASS
,
1819 JLINK_USB_INTERFACE_SUBCLASS
,
1820 JLINK_USB_INTERFACE_PROTOCOL
);
1822 struct jlink
*result
= malloc(sizeof(struct jlink
));
1823 result
->usb_handle
= devh
;
1827 static void jlink_usb_close(struct jlink
*jlink
)
1829 jtag_libusb_close(jlink
->usb_handle
);
1833 /* Send a message and receive the reply. */
1834 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
)
1838 result
= jlink_usb_write(jlink
, out_length
);
1839 if (result
!= out_length
) {
1840 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1841 out_length
, result
);
1842 return ERROR_JTAG_DEVICE_ERROR
;
1845 result
= jlink_usb_read(jlink
, in_length
);
1846 if (result
!= in_length
) {
1847 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1849 return ERROR_JTAG_DEVICE_ERROR
;
1854 /* calls the given usb_bulk_* function, allowing for the data to
1855 * trickle in with some timeouts */
1856 static int usb_bulk_with_retries(
1857 int (*f
)(jtag_libusb_device_handle
*, int, char *, int, int),
1858 jtag_libusb_device_handle
*dev
, int ep
,
1859 char *bytes
, int size
, int timeout
)
1861 int tries
= 3, count
= 0;
1863 while (tries
&& (count
< size
)) {
1864 int result
= f(dev
, ep
, bytes
+ count
, size
- count
, timeout
);
1867 else if ((-ETIMEDOUT
!= result
) || !--tries
)
1873 static int wrap_usb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
,
1874 char *buff
, int size
, int timeout
)
1876 /* usb_bulk_write() takes const char *buff */
1877 return jtag_libusb_bulk_write(dev
, ep
, buff
, size
, timeout
);
1880 static inline int usb_bulk_write_ex(jtag_libusb_device_handle
*dev
, int ep
,
1881 char *bytes
, int size
, int timeout
)
1883 return usb_bulk_with_retries(&wrap_usb_bulk_write
,
1884 dev
, ep
, bytes
, size
, timeout
);
1887 static inline int usb_bulk_read_ex(jtag_libusb_device_handle
*dev
, int ep
,
1888 char *bytes
, int size
, int timeout
)
1890 return usb_bulk_with_retries(&jtag_libusb_bulk_read
,
1891 dev
, ep
, bytes
, size
, timeout
);
1894 /* Write data from out_buffer to USB. */
1895 static int jlink_usb_write(struct jlink
*jlink
, int out_length
)
1899 if (out_length
> JLINK_OUT_BUFFER_SIZE
) {
1900 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
1901 out_length
, JLINK_OUT_BUFFER_SIZE
);
1905 result
= usb_bulk_write_ex(jlink
->usb_handle
, jlink_write_ep
,
1906 (char *)usb_out_buffer
, out_length
, JLINK_USB_TIMEOUT
);
1908 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
1909 out_length
, result
);
1911 jlink_debug_buffer(usb_out_buffer
, out_length
);
1915 /* Read data from USB into in_buffer. */
1916 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
)
1918 int result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1919 (char *)usb_in_buffer
, expected_size
, JLINK_USB_TIMEOUT
);
1921 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result
);
1923 jlink_debug_buffer(usb_in_buffer
, result
);
1928 * Send a message and receive the reply - simple messages.
1930 * @param jlink pointer to driver data
1931 * @param out_length data length in @c usb_out_buffer
1932 * @param in_length data length to be read to @c usb_in_buffer
1934 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
)
1938 result
= jlink_usb_write(jlink
, out_length
);
1939 if (result
!= out_length
) {
1940 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1941 out_length
, result
);
1942 return ERROR_JTAG_DEVICE_ERROR
;
1945 result
= jlink_usb_read(jlink
, in_length
);
1946 if (result
!= in_length
) {
1947 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1949 return ERROR_JTAG_DEVICE_ERROR
;
1953 * Section 4.2.4 IN-transaction:
1954 * read dummy 0-byte packet if transaction size is
1955 * multiple of 64 bytes but not max. size of 0x8000
1957 if ((in_length
% 64) == 0 && in_length
!= 0x8000) {
1959 result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1960 &dummy_buffer
, 1, JLINK_USB_TIMEOUT
);
1962 LOG_ERROR("dummy byte read failed");
1963 return ERROR_JTAG_DEVICE_ERROR
;
1969 #ifdef _DEBUG_USB_COMMS_
1970 #define BYTES_PER_LINE 16
1972 static void jlink_debug_buffer(uint8_t *buffer
, int length
)
1979 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
1980 snprintf(line
, 5, "%04x", i
);
1981 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
1982 snprintf(s
, 4, " %02x", buffer
[j
]);
1985 LOG_DEBUG("%s", line
);