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 uint32_t jlink_caps
;
260 static uint32_t jlink_hw_type
;
262 static int queued_retval
;
263 static bool swd_mode
;
265 /* 256 byte non-volatile memory */
266 struct jlink_config
{
268 /* 0ffset 0x01 to 0x03 */
269 uint8_t reserved_1
[3];
270 uint32_t kickstart_power_on_jtag_pin_19
;
271 /* 0ffset 0x08 to 0x1f */
272 uint8_t reserved_2
[24];
273 /* IP only for J-Link Pro */
274 uint8_t ip_address
[4];
275 uint8_t subnet_mask
[4];
276 /* 0ffset 0x28 to 0x2f */
277 uint8_t reserved_3
[8];
278 uint8_t mac_address
[6];
279 /* 0ffset 0x36 to 0xff */
280 uint8_t reserved_4
[202];
281 } __attribute__ ((packed
));
282 struct jlink_config jlink_cfg
;
284 /***************************************************************************/
285 /* External interface implementation */
287 static void jlink_execute_runtest(struct jtag_command
*cmd
)
289 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
290 cmd
->cmd
.runtest
->num_cycles
,
291 cmd
->cmd
.runtest
->end_state
);
293 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
295 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
298 static void jlink_execute_statemove(struct jtag_command
*cmd
)
300 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
302 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
306 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
308 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
309 cmd
->cmd
.pathmove
->num_states
,
310 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
312 jlink_path_move(cmd
->cmd
.pathmove
->num_states
,
313 cmd
->cmd
.pathmove
->path
);
316 static void jlink_execute_scan(struct jtag_command
*cmd
)
322 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd
->cmd
.scan
->end_state
));
324 jlink_end_state(cmd
->cmd
.scan
->end_state
);
326 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
327 DEBUG_JTAG_IO("scan input, length = %d", scan_size
);
329 jlink_debug_buffer(buffer
, (scan_size
+ 7) / 8);
330 type
= jtag_scan_type(cmd
->cmd
.scan
);
331 jlink_scan(cmd
->cmd
.scan
->ir_scan
,
332 type
, buffer
, scan_size
, cmd
->cmd
.scan
);
335 static void jlink_execute_reset(struct jtag_command
*cmd
)
337 DEBUG_JTAG_IO("reset trst: %i srst %i",
338 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
341 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
345 static void jlink_execute_sleep(struct jtag_command
*cmd
)
347 DEBUG_JTAG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
349 jtag_sleep(cmd
->cmd
.sleep
->us
);
352 static void jlink_execute_command(struct jtag_command
*cmd
)
356 jlink_execute_runtest(cmd
);
359 jlink_execute_statemove(cmd
);
362 jlink_execute_pathmove(cmd
);
365 jlink_execute_scan(cmd
);
368 jlink_execute_reset(cmd
);
371 jlink_execute_sleep(cmd
);
374 LOG_ERROR("BUG: unknown JTAG command type encountered");
379 static int jlink_execute_queue(void)
381 struct jtag_command
*cmd
= jtag_command_queue
;
383 while (cmd
!= NULL
) {
384 jlink_execute_command(cmd
);
388 return jlink_tap_execute();
391 /* Sets speed in kHz. */
392 static int jlink_speed(int speed
)
396 if (speed
> JLINK_MAX_SPEED
) {
397 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
398 speed
, JLINK_MAX_SPEED
);
399 speed
= JLINK_MAX_SPEED
;
402 /* check for RTCK setting */
406 usb_out_buffer
[0] = EMU_CMD_SET_SPEED
;
407 usb_out_buffer
[1] = (speed
>> 0) & 0xff;
408 usb_out_buffer
[2] = (speed
>> 8) & 0xff;
410 result
= jlink_usb_write(jlink_handle
, 3);
412 LOG_ERROR("J-Link setting speed failed (%d)", result
);
413 return ERROR_JTAG_DEVICE_ERROR
;
419 static int jlink_speed_div(int speed
, int *khz
)
426 static int jlink_khz(int khz
, int *jtag_speed
)
433 static int jlink_register(void)
436 usb_out_buffer
[0] = EMU_CMD_REGISTER
;
437 usb_out_buffer
[1] = REG_CMD_REGISTER
;
438 /* 2 - 11 is "additional parameter",
439 * 12 - 13 is connection handle, zero initially */
440 memset(&usb_out_buffer
[2], 0, 10 + 2);
442 result
= jlink_usb_write(jlink_handle
, 14);
444 LOG_ERROR("J-Link register write failed (%d)", result
);
445 return ERROR_JTAG_DEVICE_ERROR
;
449 * 0 - 1 connection handle,
450 * 2 - 3 number of information entities,
451 * 4 - 5 size of a single information struct,
452 * 6 - 7 number of additional bytes,
455 * Try to read the whole USB bulk packet
457 result
= jtag_libusb_bulk_read(jlink_handle
->usb_handle
, jlink_read_ep
,
458 (char *)usb_in_buffer
, sizeof(usb_in_buffer
),
461 LOG_ERROR("J-Link register read failed (0 bytes received)");
462 return ERROR_JTAG_DEVICE_ERROR
;
469 * select transport interface
471 * @param iface [0..31] currently: 0=JTAG, 1=SWD
472 * @returns ERROR_OK or ERROR_ code
474 * @pre jlink_handle must be opened
475 * @pre function may be called only for devices, that have
476 * EMU_CAP_SELECT_IF capability enabled
478 static int jlink_select_interface(int iface
)
480 /* According to Segger's document RM08001-R7 Date: October 8, 2010,
481 * http://www.segger.com/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
482 * section 5.5.3 EMU_CMD_SELECT_IF
483 * > SubCmd 1..31 to select interface (0..31)
485 * The table below states:
489 * This obviosly means that to select TIF_JTAG one should write SubCmd = 1.
491 * In fact, JTAG interface operates when SubCmd=0
493 * It looks like a typo in documentation, because interfaces 0..31 could not
494 * be selected by 1..31 range command.
496 assert(iface
>= 0 && iface
< 32);
499 /* get available interfaces */
500 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
501 usb_out_buffer
[1] = 0xff;
503 result
= jlink_usb_io(jlink_handle
, 2, 4);
504 if (result
!= ERROR_OK
) {
505 LOG_ERROR("J-Link query interface failed (%d)", result
);
506 return ERROR_JTAG_DEVICE_ERROR
;
509 uint32_t iface_mask
= buf_get_u32(usb_in_buffer
, 0, 32);
511 if (!(iface_mask
& (1<<iface
))) {
512 LOG_ERROR("J-Link requesting to select unsupported interface (%" PRIx32
")", iface_mask
);
513 return ERROR_JTAG_DEVICE_ERROR
;
516 /* Select interface */
517 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
518 usb_out_buffer
[1] = iface
;
520 result
= jlink_usb_io(jlink_handle
, 2, 4);
521 if (result
!= ERROR_OK
) {
522 LOG_ERROR("J-Link interface select failed (%d)", result
);
523 return ERROR_JTAG_DEVICE_ERROR
;
529 static int jlink_init(void)
533 jlink_handle
= jlink_usb_open();
535 if (jlink_handle
== 0) {
536 LOG_ERROR("Cannot find jlink Interface! Please check "
537 "connection and permissions.");
538 return ERROR_JTAG_INIT_FAILED
;
541 jlink_hw_jtag_version
= 2;
543 if (jlink_get_version_info() == ERROR_OK
) {
544 /* attempt to get status */
548 /* Registration is sometimes necessary for SWD to work */
549 if (jlink_caps
& (1<<EMU_CAP_REGISTER
))
553 * Some versions of Segger's software do not select JTAG interface by default.
555 * Segger recommends to select interface necessarily as a part of init process,
556 * in case any previous session leaves improper interface selected.
559 if (jlink_caps
& (1<<EMU_CAP_SELECT_IF
))
560 retval
= jlink_select_interface(swd_mode
? JLINK_TIF_SWD
: JLINK_TIF_JTAG
);
562 retval
= swd_mode
? ERROR_JTAG_DEVICE_ERROR
: ERROR_OK
;
564 if (retval
!= ERROR_OK
) {
565 LOG_ERROR("Selected transport mode is not supported.");
566 return ERROR_JTAG_INIT_FAILED
;
569 LOG_INFO("J-Link JTAG Interface ready");
575 jlink_speed(jtag_get_speed_khz());
578 /* v5/6 jlink seems to have an issue if the first tap move
579 * is not divisible by 8, so we send a TLR on first power up */
580 for (i
= 0; i
< 8; i
++)
581 jlink_tap_append_step(1, 0);
588 static int jlink_quit(void)
590 jlink_usb_close(jlink_handle
);
594 /***************************************************************************/
595 /* Queue command implementations */
597 static void jlink_end_state(tap_state_t state
)
599 if (tap_is_state_stable(state
))
600 tap_set_end_state(state
);
602 LOG_ERROR("BUG: %i is not a valid end state", state
);
607 /* Goes to the end state. */
608 static void jlink_state_move(void)
612 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
613 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
615 for (i
= 0; i
< tms_scan_bits
; i
++) {
616 tms
= (tms_scan
>> i
) & 1;
617 jlink_tap_append_step(tms
, 0);
620 tap_set_state(tap_get_end_state());
623 static void jlink_path_move(int num_states
, tap_state_t
*path
)
627 for (i
= 0; i
< num_states
; i
++) {
628 if (path
[i
] == tap_state_transition(tap_get_state(), false))
629 jlink_tap_append_step(0, 0);
630 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
631 jlink_tap_append_step(1, 0);
633 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
634 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
638 tap_set_state(path
[i
]);
641 tap_set_end_state(tap_get_state());
644 static void jlink_runtest(int num_cycles
)
648 tap_state_t saved_end_state
= tap_get_end_state();
650 jlink_tap_ensure_space(1, num_cycles
+ 16);
652 /* only do a state_move when we're not already in IDLE */
653 if (tap_get_state() != TAP_IDLE
) {
654 jlink_end_state(TAP_IDLE
);
659 /* execute num_cycles */
660 for (i
= 0; i
< num_cycles
; i
++)
661 jlink_tap_append_step(0, 0);
663 /* finish in end_state */
664 jlink_end_state(saved_end_state
);
665 if (tap_get_state() != tap_get_end_state())
669 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
670 int scan_size
, struct scan_command
*command
)
672 tap_state_t saved_end_state
;
674 jlink_tap_ensure_space(1, scan_size
+ 16);
676 saved_end_state
= tap_get_end_state();
678 /* Move to appropriate scan state */
679 jlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
681 /* Only move if we're not already there */
682 if (tap_get_state() != tap_get_end_state())
685 jlink_end_state(saved_end_state
);
688 jlink_tap_append_scan(scan_size
, buffer
, command
);
690 /* We are in Exit1, go to Pause */
691 jlink_tap_append_step(0, 0);
693 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
695 if (tap_get_state() != tap_get_end_state())
699 static void jlink_reset(int trst
, int srst
)
701 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
703 /* Signals are active low */
705 jlink_simple_command(EMU_CMD_HW_RESET1
);
708 jlink_simple_command(EMU_CMD_HW_RESET0
);
711 jlink_simple_command(EMU_CMD_HW_TRST0
);
714 jlink_simple_command(EMU_CMD_HW_TRST1
);
717 static void jlink_simple_command(uint8_t command
)
721 DEBUG_JTAG_IO("0x%02x", command
);
723 usb_out_buffer
[0] = command
;
724 result
= jlink_usb_write(jlink_handle
, 1);
727 LOG_ERROR("J-Link command 0x%02x failed (%d)", command
, result
);
730 static int jlink_get_status(void)
734 jlink_simple_command(EMU_CMD_GET_STATE
);
736 result
= jlink_usb_read(jlink_handle
, 8);
738 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result
);
739 return ERROR_JTAG_DEVICE_ERROR
;
742 int vref
= usb_in_buffer
[0] + (usb_in_buffer
[1] << 8);
743 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
744 vref
/ 1000, vref
% 1000, \
745 usb_in_buffer
[2], usb_in_buffer
[3], usb_in_buffer
[4], \
746 usb_in_buffer
[5], usb_in_buffer
[6], usb_in_buffer
[7]);
749 LOG_ERROR("Vref too low. Check Target Power");
754 #define jlink_dump_printf(context, expr ...) \
757 command_print(context, expr); \
762 static void jlink_caps_dump(struct command_context
*ctx
)
766 jlink_dump_printf(ctx
, "J-Link Capabilities");
768 for (i
= 1; i
< 31; i
++)
769 if (jlink_caps
& (1 << i
))
770 jlink_dump_printf(ctx
, "%s", jlink_cap_str
[i
]);
773 static void jlink_config_usb_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
778 jlink_dump_printf(ctx
, "USB-Address: 0x%x", cfg
->usb_address
);
781 static void jlink_config_kickstart_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
786 jlink_dump_printf(ctx
, "Kickstart power on JTAG-pin 19: 0x%" PRIx32
,
787 cfg
->kickstart_power_on_jtag_pin_19
);
790 static void jlink_config_mac_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
795 jlink_dump_printf(ctx
, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
796 cfg
->mac_address
[5], cfg
->mac_address
[4],
797 cfg
->mac_address
[3], cfg
->mac_address
[2],
798 cfg
->mac_address
[1], cfg
->mac_address
[0]);
801 static void jlink_config_ip_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
806 jlink_dump_printf(ctx
, "IP Address: %d.%d.%d.%d",
807 cfg
->ip_address
[3], cfg
->ip_address
[2],
808 cfg
->ip_address
[1], cfg
->ip_address
[0]);
809 jlink_dump_printf(ctx
, "Subnet Mask: %d.%d.%d.%d",
810 cfg
->subnet_mask
[3], cfg
->subnet_mask
[2],
811 cfg
->subnet_mask
[1], cfg
->subnet_mask
[0]);
814 static void jlink_config_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
819 jlink_dump_printf(ctx
, "J-Link configuration");
820 jlink_config_usb_address_dump(ctx
, cfg
);
821 jlink_config_kickstart_dump(ctx
, cfg
);
823 if (jlink_hw_type
== JLINK_HW_TYPE_JLINK_PRO
) {
824 jlink_config_ip_dump(ctx
, cfg
);
825 jlink_config_mac_address_dump(ctx
, cfg
);
829 static int jlink_get_config(struct jlink_config
*cfg
)
832 int size
= sizeof(struct jlink_config
);
834 usb_out_buffer
[0] = EMU_CMD_READ_CONFIG
;
835 result
= jlink_usb_io(jlink_handle
, 1, size
);
837 if (result
!= ERROR_OK
) {
838 LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size
, result
);
842 memcpy(cfg
, usb_in_buffer
, size
);
846 static int jlink_set_config(struct jlink_config
*cfg
)
849 int size
= sizeof(struct jlink_config
);
851 jlink_simple_command(EMU_CMD_WRITE_CONFIG
);
853 memcpy(usb_out_buffer
, cfg
, size
);
855 result
= jlink_usb_write(jlink_handle
, size
);
856 if (result
!= size
) {
857 LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result
);
865 * List of unsupported version string markers.
867 * The firmware versions does not correspond directly with
868 * "Software and documentation pack for Windows", it may be
869 * distinguished by the "compile" date in the information string.
871 * For example, version string is:
872 * "J-Link ARM V8 compiled May 3 2012 18:36:22"
876 * The list must be terminated by NULL string.
878 static const char * const unsupported_versions
[] = {
881 NULL
/* End of list */
884 static void jlink_check_supported(const char *str
)
886 const char * const *p
= unsupported_versions
;
888 if (NULL
!= strstr(str
, *p
)) {
890 "Unsupported J-Link firmware version.\n"
891 " Please check http://www.segger.com/j-link-older-versions.html for updates");
898 static int jlink_get_version_info(void)
902 uint32_t jlink_max_size
;
904 /* query hardware version */
905 jlink_simple_command(EMU_CMD_VERSION
);
907 result
= jlink_usb_read(jlink_handle
, 2);
909 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
910 return ERROR_JTAG_DEVICE_ERROR
;
913 len
= buf_get_u32(usb_in_buffer
, 0, 16);
914 if (len
> JLINK_IN_BUFFER_SIZE
) {
915 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len
);
916 len
= JLINK_IN_BUFFER_SIZE
;
919 result
= jlink_usb_read(jlink_handle
, len
);
921 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
922 return ERROR_JTAG_DEVICE_ERROR
;
925 usb_in_buffer
[result
] = 0;
926 LOG_INFO("%s", (char *)usb_in_buffer
);
927 jlink_check_supported((char *)usb_in_buffer
);
929 /* query hardware capabilities */
930 jlink_simple_command(EMU_CMD_GET_CAPS
);
932 result
= jlink_usb_read(jlink_handle
, 4);
934 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result
);
935 return ERROR_JTAG_DEVICE_ERROR
;
938 jlink_caps
= buf_get_u32(usb_in_buffer
, 0, 32);
939 LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps
);
941 if (jlink_caps
& (1 << EMU_CAP_GET_HW_VERSION
)) {
942 /* query hardware version */
943 jlink_simple_command(EMU_CMD_GET_HW_VERSION
);
945 result
= jlink_usb_read(jlink_handle
, 4);
947 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result
);
948 return ERROR_JTAG_DEVICE_ERROR
;
951 uint32_t jlink_hw_version
= buf_get_u32(usb_in_buffer
, 0, 32);
952 uint32_t major_revision
= (jlink_hw_version
/ 10000) % 100;
953 jlink_hw_type
= (jlink_hw_version
/ 1000000) % 100;
954 if (major_revision
>= 5)
955 jlink_hw_jtag_version
= 3;
957 LOG_INFO("J-Link hw version %i", (int)jlink_hw_version
);
959 switch (jlink_hw_type
) {
960 case JLINK_HW_TYPE_JLINK
:
961 LOG_INFO("J-Link hw type J-Link");
963 case JLINK_HW_TYPE_JTRACE
:
964 LOG_INFO("J-Link hw type J-Trace");
966 case JLINK_HW_TYPE_FLASHER
:
967 LOG_INFO("J-Link hw type Flasher");
969 case JLINK_HW_TYPE_JLINK_PRO
:
970 LOG_INFO("J-Link hw type J-Link Pro");
972 case JLINK_HW_TYPE_JLINK_LITE_ADI
:
973 LOG_INFO("J-Link hw type J-Link Lite-ADI");
975 case JLINK_HW_TYPE_LPCLINK2
:
976 LOG_INFO("J-Link hw type J-Link on LPC-Link2");
979 LOG_INFO("J-Link hw type unknown 0x%" PRIx32
, jlink_hw_type
);
984 if (jlink_caps
& (1 << EMU_CAP_GET_MAX_BLOCK_SIZE
)) {
985 /* query hardware maximum memory block */
986 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK
);
988 result
= jlink_usb_read(jlink_handle
, 4);
990 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result
);
991 return ERROR_JTAG_DEVICE_ERROR
;
994 jlink_max_size
= buf_get_u32(usb_in_buffer
, 0, 32);
995 LOG_INFO("J-Link max mem block %i", (int)jlink_max_size
);
998 if (jlink_caps
& (1 << EMU_CAP_READ_CONFIG
)) {
999 if (jlink_get_config(&jlink_cfg
) != ERROR_OK
)
1000 return ERROR_JTAG_DEVICE_ERROR
;
1002 jlink_config_dump(NULL
, &jlink_cfg
);
1008 COMMAND_HANDLER(jlink_pid_command
)
1010 if (CMD_ARGC
!= 1) {
1011 LOG_ERROR("Need exactly one argument to jlink_pid");
1015 pids
[0] = strtoul(CMD_ARGV
[0], NULL
, 16);
1022 COMMAND_HANDLER(jlink_handle_jlink_info_command
)
1024 if (jlink_get_version_info() == ERROR_OK
) {
1025 /* attempt to get status */
1032 COMMAND_HANDLER(jlink_handle_jlink_caps_command
)
1034 jlink_caps_dump(CMD_CTX
);
1039 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command
)
1043 command_print(CMD_CTX
, "J-Link hw jtag %i", jlink_hw_jtag_version
);
1046 int request_version
= atoi(CMD_ARGV
[0]);
1047 switch (request_version
) {
1050 jlink_hw_jtag_version
= request_version
;
1053 return ERROR_COMMAND_SYNTAX_ERROR
;
1058 return ERROR_COMMAND_SYNTAX_ERROR
;
1064 COMMAND_HANDLER(jlink_handle_jlink_kickstart_command
)
1069 jlink_config_kickstart_dump(CMD_CTX
, &jlink_cfg
);
1073 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], kickstart
);
1075 jlink_cfg
.kickstart_power_on_jtag_pin_19
= kickstart
;
1079 COMMAND_HANDLER(jlink_handle_jlink_mac_address_command
)
1087 jlink_config_mac_address_dump(CMD_CTX
, &jlink_cfg
);
1093 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' || str
[8] != ':' ||
1094 str
[11] != ':' || str
[14] != ':')) {
1095 command_print(CMD_CTX
, "ethaddr miss format ff:ff:ff:ff:ff:ff");
1096 return ERROR_COMMAND_SYNTAX_ERROR
;
1099 for (i
= 5; i
>= 0; i
--) {
1100 addr
[i
] = strtoul(str
, &e
, 16);
1104 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1105 command_print(CMD_CTX
, "invalid it's zero mac_address");
1106 return ERROR_COMMAND_SYNTAX_ERROR
;
1109 if (!(0x01 & addr
[0])) {
1110 command_print(CMD_CTX
, "invalid it's a multicat mac_address");
1111 return ERROR_COMMAND_SYNTAX_ERROR
;
1114 memcpy(jlink_cfg
.mac_address
, addr
, sizeof(addr
));
1119 static int string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1123 const char *s_save
= s
;
1129 for (i
= 0; i
< 4; i
++) {
1130 lip
[i
] = strtoul(s
, &e
, 10);
1132 if (*e
!= '.' && i
!= 3)
1140 memcpy(ip
, lip
, sizeof(lip
));
1144 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1148 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1152 COMMAND_HANDLER(jlink_handle_jlink_ip_command
)
1154 uint32_t ip_address
;
1155 uint32_t subnet_mask
= 0;
1158 uint8_t subnet_bits
= 24;
1161 jlink_config_ip_dump(CMD_CTX
, &jlink_cfg
);
1165 ret
= string_to_ip(CMD_ARGV
[0], (uint8_t *)&ip_address
, &i
);
1166 if (ret
!= ERROR_OK
)
1169 len
= strlen(CMD_ARGV
[0]);
1171 /* check for this format A.B.C.D/E */
1174 if (CMD_ARGV
[0][i
] != '/')
1175 return ERROR_COMMAND_SYNTAX_ERROR
;
1177 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1180 ret
= string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
);
1181 if (ret
!= ERROR_OK
)
1187 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1188 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1190 cpy_ip(jlink_cfg
.ip_address
, (uint8_t *)&ip_address
);
1191 cpy_ip(jlink_cfg
.subnet_mask
, (uint8_t *)&subnet_mask
);
1196 COMMAND_HANDLER(jlink_handle_jlink_reset_command
)
1198 memset(&jlink_cfg
, 0xff, sizeof(jlink_cfg
));
1202 COMMAND_HANDLER(jlink_handle_jlink_save_command
)
1204 if (!(jlink_caps
& (1 << EMU_CAP_WRITE_CONFIG
))) {
1205 command_print(CMD_CTX
, "J-Link write emulator configuration not supported");
1209 command_print(CMD_CTX
, "The J-Link need to be unpluged and repluged ta have the config effective");
1210 return jlink_set_config(&jlink_cfg
);
1213 COMMAND_HANDLER(jlink_handle_jlink_usb_address_command
)
1218 jlink_config_usb_address_dump(CMD_CTX
, &jlink_cfg
);
1222 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
1224 if (address
> 0x3 && address
!= 0xff) {
1225 command_print(CMD_CTX
, "USB Address must be between 0x00 and 0x03 or 0xff");
1226 return ERROR_COMMAND_SYNTAX_ERROR
;
1229 jlink_cfg
.usb_address
= address
;
1233 COMMAND_HANDLER(jlink_handle_jlink_config_command
)
1235 struct jlink_config cfg
;
1238 if (CMD_ARGC
== 0) {
1239 if (!(jlink_caps
& (1 << EMU_CAP_READ_CONFIG
))) {
1240 command_print(CMD_CTX
, "J-Link read emulator configuration not supported");
1244 ret
= jlink_get_config(&cfg
);
1246 if (ret
!= ERROR_OK
)
1247 command_print(CMD_CTX
, "J-Link read emulator configuration failled");
1249 jlink_config_dump(CMD_CTX
, &jlink_cfg
);
1256 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1258 .name
= "kickstart",
1259 .handler
= &jlink_handle_jlink_kickstart_command
,
1260 .mode
= COMMAND_EXEC
,
1261 .help
= "set Kickstart power on JTAG-pin 19.",
1265 .name
= "mac_address",
1266 .handler
= &jlink_handle_jlink_mac_address_command
,
1267 .mode
= COMMAND_EXEC
,
1268 .help
= "set the MAC Address",
1269 .usage
= "[ff:ff:ff:ff:ff:ff]",
1273 .handler
= &jlink_handle_jlink_ip_command
,
1274 .mode
= COMMAND_EXEC
,
1275 .help
= "set the ip address of the J-Link Pro, "
1276 "where A.B.C.D is the ip, "
1277 "E the bit of the subnet mask, "
1278 "F.G.H.I the subnet mask",
1279 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1283 .handler
= &jlink_handle_jlink_reset_command
,
1284 .mode
= COMMAND_EXEC
,
1285 .help
= "reset the current config",
1289 .handler
= &jlink_handle_jlink_save_command
,
1290 .mode
= COMMAND_EXEC
,
1291 .help
= "save the current config",
1294 .name
= "usb_address",
1295 .handler
= &jlink_handle_jlink_usb_address_command
,
1296 .mode
= COMMAND_EXEC
,
1297 .help
= "set the USB-Address, "
1298 "This will change the product id",
1299 .usage
= "[0x00 to 0x03 or 0xff]",
1301 COMMAND_REGISTRATION_DONE
1304 static const struct command_registration jlink_subcommand_handlers
[] = {
1307 .handler
= &jlink_handle_jlink_caps_command
,
1308 .mode
= COMMAND_EXEC
,
1309 .help
= "show jlink capabilities",
1313 .handler
= &jlink_handle_jlink_info_command
,
1314 .mode
= COMMAND_EXEC
,
1315 .help
= "show jlink info",
1319 .handler
= &jlink_handle_jlink_hw_jtag_command
,
1320 .mode
= COMMAND_EXEC
,
1321 .help
= "access J-Link HW JTAG command version",
1326 .handler
= &jlink_handle_jlink_config_command
,
1327 .mode
= COMMAND_EXEC
,
1328 .help
= "access J-Link configuration, "
1329 "if no argument this will dump the config",
1330 .chain
= jlink_config_subcommand_handlers
,
1334 .handler
= &jlink_pid_command
,
1335 .mode
= COMMAND_CONFIG
,
1336 .help
= "set the pid of the interface we want to use",
1338 COMMAND_REGISTRATION_DONE
1341 static const struct command_registration jlink_command_handlers
[] = {
1344 .mode
= COMMAND_ANY
,
1345 .help
= "perform jlink management",
1346 .chain
= jlink_subcommand_handlers
,
1348 COMMAND_REGISTRATION_DONE
1351 static int jlink_swd_init(void)
1353 LOG_INFO("JLink SWD mode enabled");
1359 static void jlink_swd_write_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t value
)
1361 assert(!(cmd
& SWD_CMD_RnW
));
1362 jlink_swd_queue_cmd(dap
, cmd
, NULL
, value
);
1365 static void jlink_swd_read_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *value
)
1367 assert(cmd
& SWD_CMD_RnW
);
1368 jlink_swd_queue_cmd(dap
, cmd
, value
, 0);
1371 static int_least32_t jlink_swd_frequency(struct adiv5_dap
*dap
, int_least32_t hz
)
1374 jlink_speed(hz
/ 1000);
1379 static const struct swd_driver jlink_swd
= {
1380 .init
= jlink_swd_init
,
1381 .frequency
= jlink_swd_frequency
,
1382 .switch_seq
= jlink_swd_switch_seq
,
1383 .read_reg
= jlink_swd_read_reg
,
1384 .write_reg
= jlink_swd_write_reg
,
1385 .run
= jlink_swd_run_queue
,
1388 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
1390 struct jtag_interface jlink_interface
= {
1392 .commands
= jlink_command_handlers
,
1393 .transports
= jlink_transports
,
1396 .execute_queue
= jlink_execute_queue
,
1397 .speed
= jlink_speed
,
1398 .speed_div
= jlink_speed_div
,
1404 /***************************************************************************/
1405 /* J-Link tap functions */
1408 static unsigned tap_length
;
1409 /* In SWD mode use tms buffer for direction control */
1410 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1411 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1412 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1414 struct pending_scan_result
{
1415 int first
; /* First bit position in tdo_buffer to read */
1416 int length
; /* Number of bits to read */
1417 struct scan_command
*command
; /* Corresponding scan command */
1421 #define MAX_PENDING_SCAN_RESULTS 256
1423 static int pending_scan_results_length
;
1424 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1426 static void jlink_tap_init(void)
1429 pending_scan_results_length
= 0;
1432 static void jlink_tap_ensure_space(int scans
, int bits
)
1434 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1435 int available_bits
= JLINK_TAP_BUFFER_SIZE
* 8 - tap_length
- 32;
1437 if (scans
> available_scans
|| bits
> available_bits
)
1438 jlink_tap_execute();
1441 static void jlink_tap_append_step(int tms
, int tdi
)
1443 int index_var
= tap_length
/ 8;
1445 assert(index_var
< JLINK_TAP_BUFFER_SIZE
);
1447 int bit_index
= tap_length
% 8;
1448 uint8_t bit
= 1 << bit_index
;
1450 /* we do not pad TMS, so be sure to initialize all bits */
1452 tms_buffer
[index_var
] = tdi_buffer
[index_var
] = 0;
1455 tms_buffer
[index_var
] |= bit
;
1457 tms_buffer
[index_var
] &= ~bit
;
1460 tdi_buffer
[index_var
] |= bit
;
1462 tdi_buffer
[index_var
] &= ~bit
;
1467 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
1468 struct scan_command
*command
)
1470 struct pending_scan_result
*pending_scan_result
=
1471 &pending_scan_results_buffer
[pending_scan_results_length
];
1474 pending_scan_result
->first
= tap_length
;
1475 pending_scan_result
->length
= length
;
1476 pending_scan_result
->command
= command
;
1477 pending_scan_result
->buffer
= buffer
;
1479 for (i
= 0; i
< length
; i
++) {
1480 int tms
= (i
< (length
- 1)) ? 0 : 1;
1481 int tdi
= (buffer
[i
/ 8] & (1 << (i
% 8))) != 0;
1482 jlink_tap_append_step(tms
, tdi
);
1484 pending_scan_results_length
++;
1487 /* Pad and send a tap sequence to the device, and receive the answer.
1488 * For the purpose of padding we assume that we are in idle or pause state. */
1489 static int jlink_tap_execute(void)
1498 /* JLink returns an extra NULL in packet when size of incoming
1499 * message is a multiple of 64, creates problems with USB comms.
1500 * WARNING: This will interfere with tap state counting. */
1501 while ((DIV_ROUND_UP(tap_length
, 8) % 64) == 0)
1502 jlink_tap_append_step((tap_get_state() == TAP_RESET
) ? 1 : 0, 0);
1504 /* number of full bytes (plus one if some would be left over) */
1505 byte_length
= DIV_ROUND_UP(tap_length
, 8);
1507 bool use_jtag3
= jlink_hw_jtag_version
>= 3;
1508 usb_out_buffer
[0] = use_jtag3
? EMU_CMD_HW_JTAG3
: EMU_CMD_HW_JTAG2
;
1509 usb_out_buffer
[1] = 0;
1510 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1511 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1512 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1513 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1515 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
1516 tap_length
, jlink_last_state
);
1518 result
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1519 use_jtag3
? byte_length
+ 1 : byte_length
);
1520 if (result
!= ERROR_OK
) {
1521 LOG_ERROR("jlink_tap_execute failed USB io (%d)", result
);
1523 return ERROR_JTAG_QUEUE_FAILED
;
1526 result
= use_jtag3
? usb_in_buffer
[byte_length
] : 0;
1528 LOG_ERROR("jlink_tap_execute failed, result %d (%s)", result
,
1529 result
== 1 ? "adaptive clocking timeout" : "unknown");
1531 return ERROR_JTAG_QUEUE_FAILED
;
1534 memcpy(tdo_buffer
, usb_in_buffer
, byte_length
);
1536 for (i
= 0; i
< pending_scan_results_length
; i
++) {
1537 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1538 uint8_t *buffer
= pending_scan_result
->buffer
;
1539 int length
= pending_scan_result
->length
;
1540 int first
= pending_scan_result
->first
;
1541 struct scan_command
*command
= pending_scan_result
->command
;
1543 /* Copy to buffer */
1544 buf_set_buf(tdo_buffer
, first
, buffer
, 0, length
);
1546 DEBUG_JTAG_IO("pending scan result, length = %d", length
);
1548 jlink_debug_buffer(buffer
, DIV_ROUND_UP(length
, 8));
1550 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
) {
1552 return ERROR_JTAG_QUEUE_FAILED
;
1555 if (pending_scan_result
->buffer
!= NULL
)
1556 free(pending_scan_result
->buffer
);
1563 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
1565 unsigned int tap_pos
= tap_length
;
1568 buf_set_u32(buf
, tap_pos
, 32, val
);
1573 buf_set_u32(buf
, tap_pos
, len
, val
);
1576 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
1578 const uint32_t dir_out
= 0xffffffff;
1581 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
1583 fill_buffer(tdi_buffer
, 0, len
);
1584 fill_buffer(tms_buffer
, dir_out
, len
);
1588 static void jlink_queue_data_in(uint32_t len
)
1590 const uint32_t dir_in
= 0;
1592 fill_buffer(tms_buffer
, dir_in
, len
);
1596 static int jlink_swd_switch_seq(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
1603 LOG_DEBUG("SWD line reset");
1604 s
= swd_seq_line_reset
;
1605 s_len
= swd_seq_line_reset_len
;
1608 LOG_DEBUG("JTAG-to-SWD");
1609 s
= swd_seq_jtag_to_swd
;
1610 s_len
= swd_seq_jtag_to_swd_len
;
1613 LOG_DEBUG("SWD-to-JTAG");
1614 s
= swd_seq_swd_to_jtag
;
1615 s_len
= swd_seq_swd_to_jtag_len
;
1618 LOG_ERROR("Sequence %d not supported", seq
);
1622 jlink_queue_data_out(s
, s_len
);
1627 static int jlink_swd_run_queue(struct adiv5_dap
*dap
)
1629 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length
);
1632 if (queued_retval
!= ERROR_OK
) {
1633 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
1637 /* A transaction must be followed by another transaction or at least 8 idle cycles to
1638 * ensure that data is clocked through the AP. */
1639 jlink_queue_data_out(NULL
, 8);
1641 size_t byte_length
= DIV_ROUND_UP(tap_length
, 8);
1643 /* There's a comment in jlink_tap_execute saying JLink returns
1644 * an extra NULL in packet when size of incoming message is a
1645 * multiple of 64. Someone should verify if that's still the
1646 * case with the current jlink firmware */
1648 usb_out_buffer
[0] = EMU_CMD_HW_JTAG3
;
1649 usb_out_buffer
[1] = 0;
1650 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1651 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1652 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1653 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1655 retval
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1657 if (retval
!= ERROR_OK
) {
1658 LOG_ERROR("jlink_swd_run_queue failed USB io (%d)", retval
);
1662 retval
= usb_in_buffer
[byte_length
];
1664 LOG_ERROR("jlink_swd_run_queue failed, result %d", retval
);
1668 for (int i
= 0; i
< pending_scan_results_length
; i
++) {
1669 int ack
= buf_get_u32(usb_in_buffer
, pending_scan_results_buffer
[i
].first
, 3);
1671 if (ack
!= SWD_ACK_OK
) {
1672 LOG_DEBUG("SWD ack not OK: %d %s", ack
,
1673 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
1674 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
1676 } else if (pending_scan_results_buffer
[i
].length
) {
1677 uint32_t data
= buf_get_u32(usb_in_buffer
, 3 + pending_scan_results_buffer
[i
].first
, 32);
1678 int parity
= buf_get_u32(usb_in_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
1680 if (parity
!= parity_u32(data
)) {
1681 LOG_ERROR("SWD Read data parity mismatch");
1682 queued_retval
= ERROR_FAIL
;
1686 if (pending_scan_results_buffer
[i
].buffer
)
1687 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
1693 retval
= queued_retval
;
1694 queued_retval
= ERROR_OK
;
1699 static void jlink_swd_queue_cmd(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *dst
, uint32_t data
)
1701 uint8_t data_parity_trn
[DIV_ROUND_UP(32 + 1, 8)];
1702 if (tap_length
+ 46 + 8 + dap
->memaccess_tck
>= sizeof(tdi_buffer
) * 8 ||
1703 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
1704 /* Not enough room in the queue. Run the queue. */
1705 queued_retval
= jlink_swd_run_queue(dap
);
1708 if (queued_retval
!= ERROR_OK
)
1711 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
1713 jlink_queue_data_out(&cmd
, 8);
1715 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
1717 if (cmd
& SWD_CMD_RnW
) {
1718 /* Queue a read transaction */
1719 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
1720 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
1722 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
1724 /* Queue a write transaction */
1725 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
1726 jlink_queue_data_in(1 + 3 + 1);
1728 buf_set_u32(data_parity_trn
, 0, 32, data
);
1729 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
1731 jlink_queue_data_out(data_parity_trn
, 32 + 1);
1734 pending_scan_results_length
++;
1736 /* Insert idle cycles after AP accesses to avoid WAIT */
1737 if (cmd
& SWD_CMD_APnDP
)
1738 jlink_queue_data_out(NULL
, dap
->memaccess_tck
);
1741 /*****************************************************************************/
1742 /* JLink USB low-level functions */
1744 static struct jlink
*jlink_usb_open()
1746 struct jtag_libusb_device_handle
*devh
;
1747 if (jtag_libusb_open(vids
, pids
, NULL
, &devh
) != ERROR_OK
)
1750 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
1751 * AREA!!!!!!!!!!! The behavior of libusb is not completely
1752 * consistent across Windows, Linux, and Mac OS X platforms.
1753 * The actions taken in the following compiler conditionals may
1754 * not agree with published documentation for libusb, but were
1755 * found to be necessary through trials and tribulations. Even
1756 * little tweaks can break one or more platforms, so if you do
1757 * make changes test them carefully on all platforms before
1761 /* This entire block can probably be removed. It was a workaround for
1762 * libusb0.1 and old JLink firmware. It has already be removed for
1763 * windows and causing problems (LPC Link-2 with JLink firmware) on
1764 * Linux with libusb1.0.
1766 * However, for now the behavior will be left unchanged for non-windows
1767 * platforms using libusb0.1 due to lack of testing.
1769 #if IS_WIN32 == 0 && HAVE_LIBUSB1 == 0
1771 jtag_libusb_reset_device(devh
);
1776 /* reopen jlink after usb_reset
1777 * on win32 this may take a second or two to re-enumerate */
1779 while ((retval
= jtag_libusb_open(vids
, pids
, NULL
, &devh
)) != ERROR_OK
) {
1785 if (ERROR_OK
!= retval
)
1791 /* usb_set_configuration is only required under win32
1792 * with libusb 0.1 and libusb0.sys. For libusb 1.0 it is a no-op
1793 * since the configuration is already set. */
1794 jtag_libusb_set_configuration(devh
, 0);
1796 jtag_libusb_choose_interface(devh
, &jlink_read_ep
, &jlink_write_ep
,
1797 JLINK_USB_INTERFACE_CLASS
,
1798 JLINK_USB_INTERFACE_SUBCLASS
,
1799 JLINK_USB_INTERFACE_PROTOCOL
);
1801 struct jlink
*result
= malloc(sizeof(struct jlink
));
1802 result
->usb_handle
= devh
;
1806 static void jlink_usb_close(struct jlink
*jlink
)
1808 jtag_libusb_close(jlink
->usb_handle
);
1812 /* Send a message and receive the reply. */
1813 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
)
1817 result
= jlink_usb_write(jlink
, out_length
);
1818 if (result
!= out_length
) {
1819 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1820 out_length
, result
);
1821 return ERROR_JTAG_DEVICE_ERROR
;
1824 result
= jlink_usb_read(jlink
, in_length
);
1825 if (result
!= in_length
) {
1826 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1828 return ERROR_JTAG_DEVICE_ERROR
;
1833 /* calls the given usb_bulk_* function, allowing for the data to
1834 * trickle in with some timeouts */
1835 static int usb_bulk_with_retries(
1836 int (*f
)(jtag_libusb_device_handle
*, int, char *, int, int),
1837 jtag_libusb_device_handle
*dev
, int ep
,
1838 char *bytes
, int size
, int timeout
)
1840 int tries
= 3, count
= 0;
1842 while (tries
&& (count
< size
)) {
1843 int result
= f(dev
, ep
, bytes
+ count
, size
- count
, timeout
);
1846 else if ((-ETIMEDOUT
!= result
) || !--tries
)
1852 static int wrap_usb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
,
1853 char *buff
, int size
, int timeout
)
1855 /* usb_bulk_write() takes const char *buff */
1856 return jtag_libusb_bulk_write(dev
, ep
, buff
, size
, timeout
);
1859 static inline int usb_bulk_write_ex(jtag_libusb_device_handle
*dev
, int ep
,
1860 char *bytes
, int size
, int timeout
)
1862 return usb_bulk_with_retries(&wrap_usb_bulk_write
,
1863 dev
, ep
, bytes
, size
, timeout
);
1866 static inline int usb_bulk_read_ex(jtag_libusb_device_handle
*dev
, int ep
,
1867 char *bytes
, int size
, int timeout
)
1869 return usb_bulk_with_retries(&jtag_libusb_bulk_read
,
1870 dev
, ep
, bytes
, size
, timeout
);
1873 /* Write data from out_buffer to USB. */
1874 static int jlink_usb_write(struct jlink
*jlink
, int out_length
)
1878 if (out_length
> JLINK_OUT_BUFFER_SIZE
) {
1879 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
1880 out_length
, JLINK_OUT_BUFFER_SIZE
);
1884 result
= usb_bulk_write_ex(jlink
->usb_handle
, jlink_write_ep
,
1885 (char *)usb_out_buffer
, out_length
, JLINK_USB_TIMEOUT
);
1887 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
1888 out_length
, result
);
1890 jlink_debug_buffer(usb_out_buffer
, out_length
);
1894 /* Read data from USB into in_buffer. */
1895 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
)
1897 int result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1898 (char *)usb_in_buffer
, expected_size
, JLINK_USB_TIMEOUT
);
1900 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result
);
1902 jlink_debug_buffer(usb_in_buffer
, result
);
1907 * Send a message and receive the reply - simple messages.
1909 * @param jlink pointer to driver data
1910 * @param out_length data length in @c usb_out_buffer
1911 * @param in_length data length to be read to @c usb_in_buffer
1913 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
)
1917 result
= jlink_usb_write(jlink
, out_length
);
1918 if (result
!= out_length
) {
1919 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1920 out_length
, result
);
1921 return ERROR_JTAG_DEVICE_ERROR
;
1924 result
= jlink_usb_read(jlink
, in_length
);
1925 if (result
!= in_length
) {
1926 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1928 return ERROR_JTAG_DEVICE_ERROR
;
1932 * Section 4.2.4 IN-transaction:
1933 * read dummy 0-byte packet if transaction size is
1934 * multiple of 64 bytes but not max. size of 0x8000
1936 if ((in_length
% 64) == 0 && in_length
!= 0x8000) {
1938 result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1939 &dummy_buffer
, 1, JLINK_USB_TIMEOUT
);
1941 LOG_ERROR("dummy byte read failed");
1942 return ERROR_JTAG_DEVICE_ERROR
;
1948 #ifdef _DEBUG_USB_COMMS_
1949 #define BYTES_PER_LINE 16
1951 static void jlink_debug_buffer(uint8_t *buffer
, int length
)
1958 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
1959 snprintf(line
, 5, "%04x", i
);
1960 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
1961 snprintf(s
, 4, " %02x", buffer
[j
]);
1964 LOG_DEBUG("%s", line
);