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_JLINK_LITE_XMC4200 17
199 #define JLINK_HW_TYPE_LPCLINK2 18
201 /* Interface selection */
202 #define JLINK_TIF_JTAG 0
203 #define JLINK_TIF_SWD 1
204 #define JLINK_SWD_DIR_IN 0
205 #define JLINK_SWD_DIR_OUT 1
207 /* Queue command functions */
208 static void jlink_end_state(tap_state_t state
);
209 static void jlink_state_move(void);
210 static void jlink_path_move(int num_states
, tap_state_t
*path
);
211 static void jlink_runtest(int num_cycles
);
212 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
213 int scan_size
, struct scan_command
*command
);
214 static void jlink_reset(int trst
, int srst
);
215 static void jlink_simple_command(uint8_t command
);
216 static int jlink_get_status(void);
217 static int jlink_swd_run_queue(struct adiv5_dap
*dap
);
218 static void jlink_swd_queue_cmd(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *dst
, uint32_t data
);
219 static int jlink_swd_switch_seq(struct adiv5_dap
*dap
, enum swd_special_seq seq
);
221 /* J-Link tap buffer functions */
222 static void jlink_tap_init(void);
223 static int jlink_tap_execute(void);
224 static void jlink_tap_ensure_space(int scans
, int bits
);
225 static void jlink_tap_append_step(int tms
, int tdi
);
226 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
227 struct scan_command
*command
);
229 /* Jlink lowlevel functions */
231 struct jtag_libusb_device_handle
*usb_handle
;
234 static struct jlink
*jlink_usb_open(void);
235 static void jlink_usb_close(struct jlink
*jlink
);
236 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
);
237 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
);
238 static int jlink_usb_write(struct jlink
*jlink
, int out_length
);
239 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
);
241 /* helper functions */
242 static int jlink_get_version_info(void);
244 #ifdef _DEBUG_USB_COMMS_
245 static void jlink_debug_buffer(uint8_t *buffer
, int length
);
247 static inline void jlink_debug_buffer(uint8_t *buffer
, int length
)
252 static enum tap_state jlink_last_state
= TAP_RESET
;
254 static struct jlink
*jlink_handle
;
256 /* pid could be specified at runtime */
257 static uint16_t vids
[] = { 0x1366, 0x1366, 0x1366, 0x1366, 0x1366, 0 };
258 static uint16_t pids
[] = { 0x0101, 0x0102, 0x0103, 0x0104, 0x0105, 0 };
260 static char *jlink_serial
;
262 static uint32_t jlink_caps
;
263 static uint32_t jlink_hw_type
;
265 static int queued_retval
;
266 static bool swd_mode
;
268 /* 256 byte non-volatile memory */
269 struct jlink_config
{
271 /* 0ffset 0x01 to 0x03 */
272 uint8_t reserved_1
[3];
273 uint32_t kickstart_power_on_jtag_pin_19
;
274 /* 0ffset 0x08 to 0x1f */
275 uint8_t reserved_2
[24];
276 /* IP only for J-Link Pro */
277 uint8_t ip_address
[4];
278 uint8_t subnet_mask
[4];
279 /* 0ffset 0x28 to 0x2f */
280 uint8_t reserved_3
[8];
281 uint8_t mac_address
[6];
282 /* 0ffset 0x36 to 0xff */
283 uint8_t reserved_4
[202];
284 } __attribute__ ((packed
));
285 struct jlink_config jlink_cfg
;
287 /***************************************************************************/
288 /* External interface implementation */
290 static void jlink_execute_runtest(struct jtag_command
*cmd
)
292 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
293 cmd
->cmd
.runtest
->num_cycles
,
294 cmd
->cmd
.runtest
->end_state
);
296 jlink_end_state(cmd
->cmd
.runtest
->end_state
);
298 jlink_runtest(cmd
->cmd
.runtest
->num_cycles
);
301 static void jlink_execute_statemove(struct jtag_command
*cmd
)
303 DEBUG_JTAG_IO("statemove end in %i", cmd
->cmd
.statemove
->end_state
);
305 jlink_end_state(cmd
->cmd
.statemove
->end_state
);
309 static void jlink_execute_pathmove(struct jtag_command
*cmd
)
311 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
312 cmd
->cmd
.pathmove
->num_states
,
313 cmd
->cmd
.pathmove
->path
[cmd
->cmd
.pathmove
->num_states
- 1]);
315 jlink_path_move(cmd
->cmd
.pathmove
->num_states
,
316 cmd
->cmd
.pathmove
->path
);
319 static void jlink_execute_scan(struct jtag_command
*cmd
)
325 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd
->cmd
.scan
->end_state
));
327 jlink_end_state(cmd
->cmd
.scan
->end_state
);
329 scan_size
= jtag_build_buffer(cmd
->cmd
.scan
, &buffer
);
330 DEBUG_JTAG_IO("scan input, length = %d", scan_size
);
332 jlink_debug_buffer(buffer
, (scan_size
+ 7) / 8);
333 type
= jtag_scan_type(cmd
->cmd
.scan
);
334 jlink_scan(cmd
->cmd
.scan
->ir_scan
,
335 type
, buffer
, scan_size
, cmd
->cmd
.scan
);
338 static void jlink_execute_reset(struct jtag_command
*cmd
)
340 DEBUG_JTAG_IO("reset trst: %i srst %i",
341 cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
344 jlink_reset(cmd
->cmd
.reset
->trst
, cmd
->cmd
.reset
->srst
);
348 static void jlink_execute_sleep(struct jtag_command
*cmd
)
350 DEBUG_JTAG_IO("sleep %" PRIi32
"", cmd
->cmd
.sleep
->us
);
352 jtag_sleep(cmd
->cmd
.sleep
->us
);
355 static void jlink_execute_command(struct jtag_command
*cmd
)
359 jlink_execute_runtest(cmd
);
362 jlink_execute_statemove(cmd
);
365 jlink_execute_pathmove(cmd
);
368 jlink_execute_scan(cmd
);
371 jlink_execute_reset(cmd
);
374 jlink_execute_sleep(cmd
);
377 LOG_ERROR("BUG: unknown JTAG command type encountered");
382 static int jlink_execute_queue(void)
384 struct jtag_command
*cmd
= jtag_command_queue
;
386 while (cmd
!= NULL
) {
387 jlink_execute_command(cmd
);
391 return jlink_tap_execute();
394 /* Sets speed in kHz. */
395 static int jlink_speed(int speed
)
399 if (speed
> JLINK_MAX_SPEED
) {
400 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
401 speed
, JLINK_MAX_SPEED
);
402 speed
= JLINK_MAX_SPEED
;
405 /* check for RTCK setting */
409 usb_out_buffer
[0] = EMU_CMD_SET_SPEED
;
410 usb_out_buffer
[1] = (speed
>> 0) & 0xff;
411 usb_out_buffer
[2] = (speed
>> 8) & 0xff;
413 result
= jlink_usb_write(jlink_handle
, 3);
415 LOG_ERROR("J-Link setting speed failed (%d)", result
);
416 return ERROR_JTAG_DEVICE_ERROR
;
422 static int jlink_speed_div(int speed
, int *khz
)
429 static int jlink_khz(int khz
, int *jtag_speed
)
436 static int jlink_register(void)
439 usb_out_buffer
[0] = EMU_CMD_REGISTER
;
440 usb_out_buffer
[1] = REG_CMD_REGISTER
;
441 /* 2 - 11 is "additional parameter",
442 * 12 - 13 is connection handle, zero initially */
443 memset(&usb_out_buffer
[2], 0, 10 + 2);
445 result
= jlink_usb_write(jlink_handle
, 14);
447 LOG_ERROR("J-Link register write failed (%d)", result
);
448 return ERROR_JTAG_DEVICE_ERROR
;
452 * 0 - 1 connection handle,
453 * 2 - 3 number of information entities,
454 * 4 - 5 size of a single information struct,
455 * 6 - 7 number of additional bytes,
458 * Try to read the whole USB bulk packet
460 result
= jtag_libusb_bulk_read(jlink_handle
->usb_handle
, jlink_read_ep
,
461 (char *)usb_in_buffer
, sizeof(usb_in_buffer
),
464 LOG_ERROR("J-Link register read failed (0 bytes received)");
465 return ERROR_JTAG_DEVICE_ERROR
;
472 * select transport interface
474 * @param iface [0..31] currently: 0=JTAG, 1=SWD
475 * @returns ERROR_OK or ERROR_ code
477 * @pre jlink_handle must be opened
478 * @pre function may be called only for devices, that have
479 * EMU_CAP_SELECT_IF capability enabled
481 static int jlink_select_interface(int iface
)
483 /* According to Segger's document RM08001-R7 Date: October 8, 2010,
484 * http://www.segger.com/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
485 * section 5.5.3 EMU_CMD_SELECT_IF
486 * > SubCmd 1..31 to select interface (0..31)
488 * The table below states:
492 * This obviosly means that to select TIF_JTAG one should write SubCmd = 1.
494 * In fact, JTAG interface operates when SubCmd=0
496 * It looks like a typo in documentation, because interfaces 0..31 could not
497 * be selected by 1..31 range command.
499 assert(iface
>= 0 && iface
< 32);
502 /* get available interfaces */
503 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
504 usb_out_buffer
[1] = 0xff;
506 result
= jlink_usb_io(jlink_handle
, 2, 4);
507 if (result
!= ERROR_OK
) {
508 LOG_ERROR("J-Link query interface failed (%d)", result
);
509 return ERROR_JTAG_DEVICE_ERROR
;
512 uint32_t iface_mask
= buf_get_u32(usb_in_buffer
, 0, 32);
514 if (!(iface_mask
& (1<<iface
))) {
515 LOG_ERROR("J-Link requesting to select unsupported interface (%" PRIx32
")", iface_mask
);
516 return ERROR_JTAG_DEVICE_ERROR
;
519 /* Select interface */
520 usb_out_buffer
[0] = EMU_CMD_SELECT_IF
;
521 usb_out_buffer
[1] = iface
;
523 result
= jlink_usb_io(jlink_handle
, 2, 4);
524 if (result
!= ERROR_OK
) {
525 LOG_ERROR("J-Link interface select failed (%d)", result
);
526 return ERROR_JTAG_DEVICE_ERROR
;
532 static int jlink_init(void)
536 jlink_handle
= jlink_usb_open();
538 if (jlink_handle
== 0) {
539 LOG_ERROR("Cannot find jlink Interface! Please check "
540 "connection and permissions.");
541 return ERROR_JTAG_INIT_FAILED
;
544 jlink_hw_jtag_version
= 2;
546 if (jlink_get_version_info() == ERROR_OK
) {
547 /* attempt to get status */
551 /* Registration is sometimes necessary for SWD to work */
552 if (jlink_caps
& (1<<EMU_CAP_REGISTER
))
556 * Some versions of Segger's software do not select JTAG interface by default.
558 * Segger recommends to select interface necessarily as a part of init process,
559 * in case any previous session leaves improper interface selected.
562 if (jlink_caps
& (1<<EMU_CAP_SELECT_IF
))
563 retval
= jlink_select_interface(swd_mode
? JLINK_TIF_SWD
: JLINK_TIF_JTAG
);
565 retval
= swd_mode
? ERROR_JTAG_DEVICE_ERROR
: ERROR_OK
;
567 if (retval
!= ERROR_OK
) {
568 LOG_ERROR("Selected transport mode is not supported.");
569 return ERROR_JTAG_INIT_FAILED
;
572 LOG_INFO("J-Link JTAG Interface ready");
578 jlink_speed(jtag_get_speed_khz());
581 /* v5/6 jlink seems to have an issue if the first tap move
582 * is not divisible by 8, so we send a TLR on first power up */
583 for (i
= 0; i
< 8; i
++)
584 jlink_tap_append_step(1, 0);
591 static int jlink_quit(void)
593 jlink_usb_close(jlink_handle
);
597 /***************************************************************************/
598 /* Queue command implementations */
600 static void jlink_end_state(tap_state_t state
)
602 if (tap_is_state_stable(state
))
603 tap_set_end_state(state
);
605 LOG_ERROR("BUG: %i is not a valid end state", state
);
610 /* Goes to the end state. */
611 static void jlink_state_move(void)
615 uint8_t tms_scan
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
616 uint8_t tms_scan_bits
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
618 for (i
= 0; i
< tms_scan_bits
; i
++) {
619 tms
= (tms_scan
>> i
) & 1;
620 jlink_tap_append_step(tms
, 0);
623 tap_set_state(tap_get_end_state());
626 static void jlink_path_move(int num_states
, tap_state_t
*path
)
630 for (i
= 0; i
< num_states
; i
++) {
631 if (path
[i
] == tap_state_transition(tap_get_state(), false))
632 jlink_tap_append_step(0, 0);
633 else if (path
[i
] == tap_state_transition(tap_get_state(), true))
634 jlink_tap_append_step(1, 0);
636 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
637 tap_state_name(tap_get_state()), tap_state_name(path
[i
]));
641 tap_set_state(path
[i
]);
644 tap_set_end_state(tap_get_state());
647 static void jlink_runtest(int num_cycles
)
651 tap_state_t saved_end_state
= tap_get_end_state();
653 jlink_tap_ensure_space(1, num_cycles
+ 16);
655 /* only do a state_move when we're not already in IDLE */
656 if (tap_get_state() != TAP_IDLE
) {
657 jlink_end_state(TAP_IDLE
);
662 /* execute num_cycles */
663 for (i
= 0; i
< num_cycles
; i
++)
664 jlink_tap_append_step(0, 0);
666 /* finish in end_state */
667 jlink_end_state(saved_end_state
);
668 if (tap_get_state() != tap_get_end_state())
672 static void jlink_scan(bool ir_scan
, enum scan_type type
, uint8_t *buffer
,
673 int scan_size
, struct scan_command
*command
)
675 tap_state_t saved_end_state
;
677 jlink_tap_ensure_space(1, scan_size
+ 16);
679 saved_end_state
= tap_get_end_state();
681 /* Move to appropriate scan state */
682 jlink_end_state(ir_scan
? TAP_IRSHIFT
: TAP_DRSHIFT
);
684 /* Only move if we're not already there */
685 if (tap_get_state() != tap_get_end_state())
688 jlink_end_state(saved_end_state
);
691 jlink_tap_append_scan(scan_size
, buffer
, command
);
693 /* We are in Exit1, go to Pause */
694 jlink_tap_append_step(0, 0);
696 tap_set_state(ir_scan
? TAP_IRPAUSE
: TAP_DRPAUSE
);
698 if (tap_get_state() != tap_get_end_state())
702 static void jlink_reset(int trst
, int srst
)
704 LOG_DEBUG("trst: %i, srst: %i", trst
, srst
);
706 /* Signals are active low */
708 jlink_simple_command(EMU_CMD_HW_RESET1
);
711 jlink_simple_command(EMU_CMD_HW_RESET0
);
714 jlink_simple_command(EMU_CMD_HW_TRST0
);
717 jlink_simple_command(EMU_CMD_HW_TRST1
);
720 static void jlink_simple_command(uint8_t command
)
724 DEBUG_JTAG_IO("0x%02x", command
);
726 usb_out_buffer
[0] = command
;
727 result
= jlink_usb_write(jlink_handle
, 1);
730 LOG_ERROR("J-Link command 0x%02x failed (%d)", command
, result
);
733 static int jlink_get_status(void)
737 jlink_simple_command(EMU_CMD_GET_STATE
);
739 result
= jlink_usb_read(jlink_handle
, 8);
741 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result
);
742 return ERROR_JTAG_DEVICE_ERROR
;
745 int vref
= usb_in_buffer
[0] + (usb_in_buffer
[1] << 8);
746 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
747 vref
/ 1000, vref
% 1000, \
748 usb_in_buffer
[2], usb_in_buffer
[3], usb_in_buffer
[4], \
749 usb_in_buffer
[5], usb_in_buffer
[6], usb_in_buffer
[7]);
752 LOG_ERROR("Vref too low. Check Target Power");
757 #define jlink_dump_printf(context, expr ...) \
760 command_print(context, expr); \
765 static void jlink_caps_dump(struct command_context
*ctx
)
769 jlink_dump_printf(ctx
, "J-Link Capabilities");
771 for (i
= 1; i
< 31; i
++)
772 if (jlink_caps
& (1 << i
))
773 jlink_dump_printf(ctx
, "%s", jlink_cap_str
[i
]);
776 static void jlink_config_usb_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
781 jlink_dump_printf(ctx
, "USB-Address: 0x%x", cfg
->usb_address
);
784 static void jlink_config_kickstart_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
789 jlink_dump_printf(ctx
, "Kickstart power on JTAG-pin 19: 0x%" PRIx32
,
790 cfg
->kickstart_power_on_jtag_pin_19
);
793 static void jlink_config_mac_address_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
798 jlink_dump_printf(ctx
, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
799 cfg
->mac_address
[5], cfg
->mac_address
[4],
800 cfg
->mac_address
[3], cfg
->mac_address
[2],
801 cfg
->mac_address
[1], cfg
->mac_address
[0]);
804 static void jlink_config_ip_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
809 jlink_dump_printf(ctx
, "IP Address: %d.%d.%d.%d",
810 cfg
->ip_address
[3], cfg
->ip_address
[2],
811 cfg
->ip_address
[1], cfg
->ip_address
[0]);
812 jlink_dump_printf(ctx
, "Subnet Mask: %d.%d.%d.%d",
813 cfg
->subnet_mask
[3], cfg
->subnet_mask
[2],
814 cfg
->subnet_mask
[1], cfg
->subnet_mask
[0]);
817 static void jlink_config_dump(struct command_context
*ctx
, struct jlink_config
*cfg
)
822 jlink_dump_printf(ctx
, "J-Link configuration");
823 jlink_config_usb_address_dump(ctx
, cfg
);
824 jlink_config_kickstart_dump(ctx
, cfg
);
826 if (jlink_hw_type
== JLINK_HW_TYPE_JLINK_PRO
) {
827 jlink_config_ip_dump(ctx
, cfg
);
828 jlink_config_mac_address_dump(ctx
, cfg
);
832 static int jlink_get_config(struct jlink_config
*cfg
)
835 int size
= sizeof(struct jlink_config
);
837 usb_out_buffer
[0] = EMU_CMD_READ_CONFIG
;
838 result
= jlink_usb_io(jlink_handle
, 1, size
);
840 if (result
!= ERROR_OK
) {
841 LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size
, result
);
845 memcpy(cfg
, usb_in_buffer
, size
);
849 static int jlink_set_config(struct jlink_config
*cfg
)
852 int size
= sizeof(struct jlink_config
);
854 jlink_simple_command(EMU_CMD_WRITE_CONFIG
);
856 memcpy(usb_out_buffer
, cfg
, size
);
858 result
= jlink_usb_write(jlink_handle
, size
);
859 if (result
!= size
) {
860 LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result
);
868 * List of unsupported version string markers.
870 * The firmware versions does not correspond directly with
871 * "Software and documentation pack for Windows", it may be
872 * distinguished by the "compile" date in the information string.
874 * For example, version string is:
875 * "J-Link ARM V8 compiled May 3 2012 18:36:22"
879 * The list must be terminated by NULL string.
881 static const char * const unsupported_versions
[] = {
884 NULL
/* End of list */
887 static void jlink_check_supported(const char *str
)
889 const char * const *p
= unsupported_versions
;
891 if (NULL
!= strstr(str
, *p
)) {
893 "Unsupported J-Link firmware version.\n"
894 " Please check http://www.segger.com/j-link-older-versions.html for updates");
901 static int jlink_get_version_info(void)
905 uint32_t jlink_max_size
;
907 /* query hardware version */
908 jlink_simple_command(EMU_CMD_VERSION
);
910 result
= jlink_usb_read(jlink_handle
, 2);
912 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
913 return ERROR_JTAG_DEVICE_ERROR
;
916 len
= buf_get_u32(usb_in_buffer
, 0, 16);
917 if (len
> JLINK_IN_BUFFER_SIZE
) {
918 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len
);
919 len
= JLINK_IN_BUFFER_SIZE
;
922 result
= jlink_usb_read(jlink_handle
, len
);
924 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result
);
925 return ERROR_JTAG_DEVICE_ERROR
;
928 usb_in_buffer
[result
] = 0;
929 LOG_INFO("%s", (char *)usb_in_buffer
);
930 jlink_check_supported((char *)usb_in_buffer
);
932 /* query hardware capabilities */
933 jlink_simple_command(EMU_CMD_GET_CAPS
);
935 result
= jlink_usb_read(jlink_handle
, 4);
937 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result
);
938 return ERROR_JTAG_DEVICE_ERROR
;
941 jlink_caps
= buf_get_u32(usb_in_buffer
, 0, 32);
942 LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps
);
944 if (jlink_caps
& (1 << EMU_CAP_GET_HW_VERSION
)) {
945 /* query hardware version */
946 jlink_simple_command(EMU_CMD_GET_HW_VERSION
);
948 result
= jlink_usb_read(jlink_handle
, 4);
950 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result
);
951 return ERROR_JTAG_DEVICE_ERROR
;
954 uint32_t jlink_hw_version
= buf_get_u32(usb_in_buffer
, 0, 32);
955 uint32_t major_revision
= (jlink_hw_version
/ 10000) % 100;
956 jlink_hw_type
= (jlink_hw_version
/ 1000000) % 100;
957 if (major_revision
>= 5)
958 jlink_hw_jtag_version
= 3;
960 LOG_INFO("J-Link hw version %i", (int)jlink_hw_version
);
962 switch (jlink_hw_type
) {
963 case JLINK_HW_TYPE_JLINK
:
964 LOG_INFO("J-Link hw type J-Link");
966 case JLINK_HW_TYPE_JTRACE
:
967 LOG_INFO("J-Link hw type J-Trace");
969 case JLINK_HW_TYPE_FLASHER
:
970 LOG_INFO("J-Link hw type Flasher");
972 case JLINK_HW_TYPE_JLINK_PRO
:
973 LOG_INFO("J-Link hw type J-Link Pro");
975 case JLINK_HW_TYPE_JLINK_LITE_ADI
:
976 LOG_INFO("J-Link hw type J-Link Lite-ADI");
978 case JLINK_HW_TYPE_JLINK_LITE_XMC4200
:
979 LOG_INFO("J-Link hw type J-Link Lite-XMC4200");
981 case JLINK_HW_TYPE_LPCLINK2
:
982 LOG_INFO("J-Link hw type J-Link on LPC-Link2");
985 LOG_INFO("J-Link hw type unknown 0x%" PRIx32
, jlink_hw_type
);
990 if (jlink_caps
& (1 << EMU_CAP_GET_MAX_BLOCK_SIZE
)) {
991 /* query hardware maximum memory block */
992 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK
);
994 result
= jlink_usb_read(jlink_handle
, 4);
996 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result
);
997 return ERROR_JTAG_DEVICE_ERROR
;
1000 jlink_max_size
= buf_get_u32(usb_in_buffer
, 0, 32);
1001 LOG_INFO("J-Link max mem block %i", (int)jlink_max_size
);
1004 if (jlink_caps
& (1 << EMU_CAP_READ_CONFIG
)) {
1005 if (jlink_get_config(&jlink_cfg
) != ERROR_OK
)
1006 return ERROR_JTAG_DEVICE_ERROR
;
1008 jlink_config_dump(NULL
, &jlink_cfg
);
1014 COMMAND_HANDLER(jlink_pid_command
)
1016 if (CMD_ARGC
!= 1) {
1017 LOG_ERROR("Need exactly one argument to jlink_pid");
1021 pids
[0] = strtoul(CMD_ARGV
[0], NULL
, 16);
1028 COMMAND_HANDLER(jlink_serial_command
)
1030 if (CMD_ARGC
!= 1) {
1031 LOG_ERROR("Need exactly one argument to jlink_serial");
1036 jlink_serial
= strdup(CMD_ARGV
[0]);
1041 COMMAND_HANDLER(jlink_handle_jlink_info_command
)
1043 if (jlink_get_version_info() == ERROR_OK
) {
1044 /* attempt to get status */
1051 COMMAND_HANDLER(jlink_handle_jlink_caps_command
)
1053 jlink_caps_dump(CMD_CTX
);
1058 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command
)
1062 command_print(CMD_CTX
, "J-Link hw jtag %i", jlink_hw_jtag_version
);
1065 int request_version
= atoi(CMD_ARGV
[0]);
1066 switch (request_version
) {
1069 jlink_hw_jtag_version
= request_version
;
1072 return ERROR_COMMAND_SYNTAX_ERROR
;
1077 return ERROR_COMMAND_SYNTAX_ERROR
;
1083 COMMAND_HANDLER(jlink_handle_jlink_kickstart_command
)
1088 jlink_config_kickstart_dump(CMD_CTX
, &jlink_cfg
);
1092 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], kickstart
);
1094 jlink_cfg
.kickstart_power_on_jtag_pin_19
= kickstart
;
1098 COMMAND_HANDLER(jlink_handle_jlink_mac_address_command
)
1106 jlink_config_mac_address_dump(CMD_CTX
, &jlink_cfg
);
1112 if ((strlen(str
) != 17) || (str
[2] != ':' || str
[5] != ':' || str
[8] != ':' ||
1113 str
[11] != ':' || str
[14] != ':')) {
1114 command_print(CMD_CTX
, "ethaddr miss format ff:ff:ff:ff:ff:ff");
1115 return ERROR_COMMAND_SYNTAX_ERROR
;
1118 for (i
= 5; i
>= 0; i
--) {
1119 addr
[i
] = strtoul(str
, &e
, 16);
1123 if (!(addr
[0] | addr
[1] | addr
[2] | addr
[3] | addr
[4] | addr
[5])) {
1124 command_print(CMD_CTX
, "invalid it's zero mac_address");
1125 return ERROR_COMMAND_SYNTAX_ERROR
;
1128 if (!(0x01 & addr
[0])) {
1129 command_print(CMD_CTX
, "invalid it's a multicat mac_address");
1130 return ERROR_COMMAND_SYNTAX_ERROR
;
1133 memcpy(jlink_cfg
.mac_address
, addr
, sizeof(addr
));
1138 static int string_to_ip(const char *s
, uint8_t *ip
, int *pos
)
1142 const char *s_save
= s
;
1148 for (i
= 0; i
< 4; i
++) {
1149 lip
[i
] = strtoul(s
, &e
, 10);
1151 if (*e
!= '.' && i
!= 3)
1159 memcpy(ip
, lip
, sizeof(lip
));
1163 static void cpy_ip(uint8_t *dst
, uint8_t *src
)
1167 for (i
= 0, j
= 3; i
< 4; i
++, j
--)
1171 COMMAND_HANDLER(jlink_handle_jlink_ip_command
)
1173 uint32_t ip_address
;
1174 uint32_t subnet_mask
= 0;
1177 uint8_t subnet_bits
= 24;
1180 jlink_config_ip_dump(CMD_CTX
, &jlink_cfg
);
1184 ret
= string_to_ip(CMD_ARGV
[0], (uint8_t *)&ip_address
, &i
);
1185 if (ret
!= ERROR_OK
)
1188 len
= strlen(CMD_ARGV
[0]);
1190 /* check for this format A.B.C.D/E */
1193 if (CMD_ARGV
[0][i
] != '/')
1194 return ERROR_COMMAND_SYNTAX_ERROR
;
1196 COMMAND_PARSE_NUMBER(u8
, CMD_ARGV
[0] + i
+ 1, subnet_bits
);
1199 ret
= string_to_ip(CMD_ARGV
[1], (uint8_t *)&subnet_mask
, &i
);
1200 if (ret
!= ERROR_OK
)
1206 subnet_mask
= (uint32_t)(subnet_bits
< 32 ?
1207 ((1ULL << subnet_bits
) - 1) : 0xffffffff);
1209 cpy_ip(jlink_cfg
.ip_address
, (uint8_t *)&ip_address
);
1210 cpy_ip(jlink_cfg
.subnet_mask
, (uint8_t *)&subnet_mask
);
1215 COMMAND_HANDLER(jlink_handle_jlink_reset_command
)
1217 memset(&jlink_cfg
, 0xff, sizeof(jlink_cfg
));
1221 COMMAND_HANDLER(jlink_handle_jlink_save_command
)
1223 if (!(jlink_caps
& (1 << EMU_CAP_WRITE_CONFIG
))) {
1224 command_print(CMD_CTX
, "J-Link write emulator configuration not supported");
1228 command_print(CMD_CTX
, "The J-Link need to be unpluged and repluged ta have the config effective");
1229 return jlink_set_config(&jlink_cfg
);
1232 COMMAND_HANDLER(jlink_handle_jlink_usb_address_command
)
1237 jlink_config_usb_address_dump(CMD_CTX
, &jlink_cfg
);
1241 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
1243 if (address
> 0x3 && address
!= 0xff) {
1244 command_print(CMD_CTX
, "USB Address must be between 0x00 and 0x03 or 0xff");
1245 return ERROR_COMMAND_SYNTAX_ERROR
;
1248 jlink_cfg
.usb_address
= address
;
1252 COMMAND_HANDLER(jlink_handle_jlink_config_command
)
1254 struct jlink_config cfg
;
1257 if (CMD_ARGC
== 0) {
1258 if (!(jlink_caps
& (1 << EMU_CAP_READ_CONFIG
))) {
1259 command_print(CMD_CTX
, "J-Link read emulator configuration not supported");
1263 ret
= jlink_get_config(&cfg
);
1265 if (ret
!= ERROR_OK
)
1266 command_print(CMD_CTX
, "J-Link read emulator configuration failled");
1268 jlink_config_dump(CMD_CTX
, &jlink_cfg
);
1275 static const struct command_registration jlink_config_subcommand_handlers
[] = {
1277 .name
= "kickstart",
1278 .handler
= &jlink_handle_jlink_kickstart_command
,
1279 .mode
= COMMAND_EXEC
,
1280 .help
= "set Kickstart power on JTAG-pin 19.",
1284 .name
= "mac_address",
1285 .handler
= &jlink_handle_jlink_mac_address_command
,
1286 .mode
= COMMAND_EXEC
,
1287 .help
= "set the MAC Address",
1288 .usage
= "[ff:ff:ff:ff:ff:ff]",
1292 .handler
= &jlink_handle_jlink_ip_command
,
1293 .mode
= COMMAND_EXEC
,
1294 .help
= "set the ip address of the J-Link Pro, "
1295 "where A.B.C.D is the ip, "
1296 "E the bit of the subnet mask, "
1297 "F.G.H.I the subnet mask",
1298 .usage
= "[A.B.C.D[/E] [F.G.H.I]]",
1302 .handler
= &jlink_handle_jlink_reset_command
,
1303 .mode
= COMMAND_EXEC
,
1304 .help
= "reset the current config",
1308 .handler
= &jlink_handle_jlink_save_command
,
1309 .mode
= COMMAND_EXEC
,
1310 .help
= "save the current config",
1313 .name
= "usb_address",
1314 .handler
= &jlink_handle_jlink_usb_address_command
,
1315 .mode
= COMMAND_EXEC
,
1316 .help
= "set the USB-Address, "
1317 "This will change the product id",
1318 .usage
= "[0x00 to 0x03 or 0xff]",
1320 COMMAND_REGISTRATION_DONE
1323 static const struct command_registration jlink_subcommand_handlers
[] = {
1326 .handler
= &jlink_handle_jlink_caps_command
,
1327 .mode
= COMMAND_EXEC
,
1328 .help
= "show jlink capabilities",
1332 .handler
= &jlink_handle_jlink_info_command
,
1333 .mode
= COMMAND_EXEC
,
1334 .help
= "show jlink info",
1338 .handler
= &jlink_handle_jlink_hw_jtag_command
,
1339 .mode
= COMMAND_EXEC
,
1340 .help
= "access J-Link HW JTAG command version",
1345 .handler
= &jlink_handle_jlink_config_command
,
1346 .mode
= COMMAND_EXEC
,
1347 .help
= "access J-Link configuration, "
1348 "if no argument this will dump the config",
1349 .chain
= jlink_config_subcommand_handlers
,
1353 .handler
= &jlink_pid_command
,
1354 .mode
= COMMAND_CONFIG
,
1355 .help
= "set the pid of the interface we want to use",
1359 .handler
= &jlink_serial_command
,
1360 .mode
= COMMAND_CONFIG
,
1361 .help
= "set the serial number of the J-Link adapter we want to use"
1363 COMMAND_REGISTRATION_DONE
1366 static const struct command_registration jlink_command_handlers
[] = {
1369 .mode
= COMMAND_ANY
,
1370 .help
= "perform jlink management",
1371 .chain
= jlink_subcommand_handlers
,
1373 COMMAND_REGISTRATION_DONE
1376 static int jlink_swd_init(void)
1378 LOG_INFO("JLink SWD mode enabled");
1384 static void jlink_swd_write_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t value
)
1386 assert(!(cmd
& SWD_CMD_RnW
));
1387 jlink_swd_queue_cmd(dap
, cmd
, NULL
, value
);
1390 static void jlink_swd_read_reg(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *value
)
1392 assert(cmd
& SWD_CMD_RnW
);
1393 jlink_swd_queue_cmd(dap
, cmd
, value
, 0);
1396 static int_least32_t jlink_swd_frequency(struct adiv5_dap
*dap
, int_least32_t hz
)
1399 jlink_speed(hz
/ 1000);
1404 static const struct swd_driver jlink_swd
= {
1405 .init
= jlink_swd_init
,
1406 .frequency
= jlink_swd_frequency
,
1407 .switch_seq
= jlink_swd_switch_seq
,
1408 .read_reg
= jlink_swd_read_reg
,
1409 .write_reg
= jlink_swd_write_reg
,
1410 .run
= jlink_swd_run_queue
,
1413 static const char * const jlink_transports
[] = { "jtag", "swd", NULL
};
1415 struct jtag_interface jlink_interface
= {
1417 .commands
= jlink_command_handlers
,
1418 .transports
= jlink_transports
,
1421 .execute_queue
= jlink_execute_queue
,
1422 .speed
= jlink_speed
,
1423 .speed_div
= jlink_speed_div
,
1429 /***************************************************************************/
1430 /* J-Link tap functions */
1433 static unsigned tap_length
;
1434 /* In SWD mode use tms buffer for direction control */
1435 static uint8_t tms_buffer
[JLINK_TAP_BUFFER_SIZE
];
1436 static uint8_t tdi_buffer
[JLINK_TAP_BUFFER_SIZE
];
1437 static uint8_t tdo_buffer
[JLINK_TAP_BUFFER_SIZE
];
1439 struct pending_scan_result
{
1440 int first
; /* First bit position in tdo_buffer to read */
1441 int length
; /* Number of bits to read */
1442 struct scan_command
*command
; /* Corresponding scan command */
1446 #define MAX_PENDING_SCAN_RESULTS 256
1448 static int pending_scan_results_length
;
1449 static struct pending_scan_result pending_scan_results_buffer
[MAX_PENDING_SCAN_RESULTS
];
1451 static void jlink_tap_init(void)
1454 pending_scan_results_length
= 0;
1457 static void jlink_tap_ensure_space(int scans
, int bits
)
1459 int available_scans
= MAX_PENDING_SCAN_RESULTS
- pending_scan_results_length
;
1460 int available_bits
= JLINK_TAP_BUFFER_SIZE
* 8 - tap_length
- 32;
1462 if (scans
> available_scans
|| bits
> available_bits
)
1463 jlink_tap_execute();
1466 static void jlink_tap_append_step(int tms
, int tdi
)
1468 int index_var
= tap_length
/ 8;
1470 assert(index_var
< JLINK_TAP_BUFFER_SIZE
);
1472 int bit_index
= tap_length
% 8;
1473 uint8_t bit
= 1 << bit_index
;
1475 /* we do not pad TMS, so be sure to initialize all bits */
1477 tms_buffer
[index_var
] = tdi_buffer
[index_var
] = 0;
1480 tms_buffer
[index_var
] |= bit
;
1482 tms_buffer
[index_var
] &= ~bit
;
1485 tdi_buffer
[index_var
] |= bit
;
1487 tdi_buffer
[index_var
] &= ~bit
;
1492 static void jlink_tap_append_scan(int length
, uint8_t *buffer
,
1493 struct scan_command
*command
)
1495 struct pending_scan_result
*pending_scan_result
=
1496 &pending_scan_results_buffer
[pending_scan_results_length
];
1499 pending_scan_result
->first
= tap_length
;
1500 pending_scan_result
->length
= length
;
1501 pending_scan_result
->command
= command
;
1502 pending_scan_result
->buffer
= buffer
;
1504 for (i
= 0; i
< length
; i
++) {
1505 int tms
= (i
< (length
- 1)) ? 0 : 1;
1506 int tdi
= (buffer
[i
/ 8] & (1 << (i
% 8))) != 0;
1507 jlink_tap_append_step(tms
, tdi
);
1509 pending_scan_results_length
++;
1512 /* Pad and send a tap sequence to the device, and receive the answer.
1513 * For the purpose of padding we assume that we are in idle or pause state. */
1514 static int jlink_tap_execute(void)
1523 /* JLink returns an extra NULL in packet when size of incoming
1524 * message is a multiple of 64, creates problems with USB comms.
1525 * WARNING: This will interfere with tap state counting. */
1526 while ((DIV_ROUND_UP(tap_length
, 8) % 64) == 0)
1527 jlink_tap_append_step((tap_get_state() == TAP_RESET
) ? 1 : 0, 0);
1529 /* number of full bytes (plus one if some would be left over) */
1530 byte_length
= DIV_ROUND_UP(tap_length
, 8);
1532 bool use_jtag3
= jlink_hw_jtag_version
>= 3;
1533 usb_out_buffer
[0] = use_jtag3
? EMU_CMD_HW_JTAG3
: EMU_CMD_HW_JTAG2
;
1534 usb_out_buffer
[1] = 0;
1535 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1536 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1537 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1538 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1540 jlink_last_state
= jtag_debug_state_machine(tms_buffer
, tdi_buffer
,
1541 tap_length
, jlink_last_state
);
1543 result
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1544 use_jtag3
? byte_length
+ 1 : byte_length
);
1545 if (result
!= ERROR_OK
) {
1546 LOG_ERROR("jlink_tap_execute failed USB io (%d)", result
);
1548 return ERROR_JTAG_QUEUE_FAILED
;
1551 result
= use_jtag3
? usb_in_buffer
[byte_length
] : 0;
1553 LOG_ERROR("jlink_tap_execute failed, result %d (%s)", result
,
1554 result
== 1 ? "adaptive clocking timeout" : "unknown");
1556 return ERROR_JTAG_QUEUE_FAILED
;
1559 memcpy(tdo_buffer
, usb_in_buffer
, byte_length
);
1561 for (i
= 0; i
< pending_scan_results_length
; i
++) {
1562 struct pending_scan_result
*pending_scan_result
= &pending_scan_results_buffer
[i
];
1563 uint8_t *buffer
= pending_scan_result
->buffer
;
1564 int length
= pending_scan_result
->length
;
1565 int first
= pending_scan_result
->first
;
1566 struct scan_command
*command
= pending_scan_result
->command
;
1568 /* Copy to buffer */
1569 buf_set_buf(tdo_buffer
, first
, buffer
, 0, length
);
1571 DEBUG_JTAG_IO("pending scan result, length = %d", length
);
1573 jlink_debug_buffer(buffer
, DIV_ROUND_UP(length
, 8));
1575 if (jtag_read_buffer(buffer
, command
) != ERROR_OK
) {
1577 return ERROR_JTAG_QUEUE_FAILED
;
1580 if (pending_scan_result
->buffer
!= NULL
)
1581 free(pending_scan_result
->buffer
);
1588 static void fill_buffer(uint8_t *buf
, uint32_t val
, uint32_t len
)
1590 unsigned int tap_pos
= tap_length
;
1593 buf_set_u32(buf
, tap_pos
, 32, val
);
1598 buf_set_u32(buf
, tap_pos
, len
, val
);
1601 static void jlink_queue_data_out(const uint8_t *data
, uint32_t len
)
1603 const uint32_t dir_out
= 0xffffffff;
1606 bit_copy(tdi_buffer
, tap_length
, data
, 0, len
);
1608 fill_buffer(tdi_buffer
, 0, len
);
1609 fill_buffer(tms_buffer
, dir_out
, len
);
1613 static void jlink_queue_data_in(uint32_t len
)
1615 const uint32_t dir_in
= 0;
1617 fill_buffer(tms_buffer
, dir_in
, len
);
1621 static int jlink_swd_switch_seq(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
1628 LOG_DEBUG("SWD line reset");
1629 s
= swd_seq_line_reset
;
1630 s_len
= swd_seq_line_reset_len
;
1633 LOG_DEBUG("JTAG-to-SWD");
1634 s
= swd_seq_jtag_to_swd
;
1635 s_len
= swd_seq_jtag_to_swd_len
;
1638 LOG_DEBUG("SWD-to-JTAG");
1639 s
= swd_seq_swd_to_jtag
;
1640 s_len
= swd_seq_swd_to_jtag_len
;
1643 LOG_ERROR("Sequence %d not supported", seq
);
1647 jlink_queue_data_out(s
, s_len
);
1652 static int jlink_swd_run_queue(struct adiv5_dap
*dap
)
1654 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length
);
1657 if (queued_retval
!= ERROR_OK
) {
1658 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval
);
1662 /* A transaction must be followed by another transaction or at least 8 idle cycles to
1663 * ensure that data is clocked through the AP. */
1664 jlink_queue_data_out(NULL
, 8);
1666 size_t byte_length
= DIV_ROUND_UP(tap_length
, 8);
1668 /* There's a comment in jlink_tap_execute saying JLink returns
1669 * an extra NULL in packet when size of incoming message is a
1670 * multiple of 64. Someone should verify if that's still the
1671 * case with the current jlink firmware */
1673 usb_out_buffer
[0] = EMU_CMD_HW_JTAG3
;
1674 usb_out_buffer
[1] = 0;
1675 usb_out_buffer
[2] = (tap_length
>> 0) & 0xff;
1676 usb_out_buffer
[3] = (tap_length
>> 8) & 0xff;
1677 memcpy(usb_out_buffer
+ 4, tms_buffer
, byte_length
);
1678 memcpy(usb_out_buffer
+ 4 + byte_length
, tdi_buffer
, byte_length
);
1680 retval
= jlink_usb_message(jlink_handle
, 4 + 2 * byte_length
,
1682 if (retval
!= ERROR_OK
) {
1683 LOG_ERROR("jlink_swd_run_queue failed USB io (%d)", retval
);
1687 retval
= usb_in_buffer
[byte_length
];
1689 LOG_ERROR("jlink_swd_run_queue failed, result %d", retval
);
1693 for (int i
= 0; i
< pending_scan_results_length
; i
++) {
1694 int ack
= buf_get_u32(usb_in_buffer
, pending_scan_results_buffer
[i
].first
, 3);
1696 if (ack
!= SWD_ACK_OK
) {
1697 LOG_DEBUG("SWD ack not OK: %d %s", ack
,
1698 ack
== SWD_ACK_WAIT
? "WAIT" : ack
== SWD_ACK_FAULT
? "FAULT" : "JUNK");
1699 queued_retval
= ack
== SWD_ACK_WAIT
? ERROR_WAIT
: ERROR_FAIL
;
1701 } else if (pending_scan_results_buffer
[i
].length
) {
1702 uint32_t data
= buf_get_u32(usb_in_buffer
, 3 + pending_scan_results_buffer
[i
].first
, 32);
1703 int parity
= buf_get_u32(usb_in_buffer
, 3 + 32 + pending_scan_results_buffer
[i
].first
, 1);
1705 if (parity
!= parity_u32(data
)) {
1706 LOG_ERROR("SWD Read data parity mismatch");
1707 queued_retval
= ERROR_FAIL
;
1711 if (pending_scan_results_buffer
[i
].buffer
)
1712 *(uint32_t *)pending_scan_results_buffer
[i
].buffer
= data
;
1718 retval
= queued_retval
;
1719 queued_retval
= ERROR_OK
;
1724 static void jlink_swd_queue_cmd(struct adiv5_dap
*dap
, uint8_t cmd
, uint32_t *dst
, uint32_t data
)
1726 uint8_t data_parity_trn
[DIV_ROUND_UP(32 + 1, 8)];
1727 if (tap_length
+ 46 + 8 + dap
->memaccess_tck
>= sizeof(tdi_buffer
) * 8 ||
1728 pending_scan_results_length
== MAX_PENDING_SCAN_RESULTS
) {
1729 /* Not enough room in the queue. Run the queue. */
1730 queued_retval
= jlink_swd_run_queue(dap
);
1733 if (queued_retval
!= ERROR_OK
)
1736 cmd
|= SWD_CMD_START
| SWD_CMD_PARK
;
1738 jlink_queue_data_out(&cmd
, 8);
1740 pending_scan_results_buffer
[pending_scan_results_length
].first
= tap_length
;
1742 if (cmd
& SWD_CMD_RnW
) {
1743 /* Queue a read transaction */
1744 pending_scan_results_buffer
[pending_scan_results_length
].length
= 32;
1745 pending_scan_results_buffer
[pending_scan_results_length
].buffer
= dst
;
1747 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
1749 /* Queue a write transaction */
1750 pending_scan_results_buffer
[pending_scan_results_length
].length
= 0;
1751 jlink_queue_data_in(1 + 3 + 1);
1753 buf_set_u32(data_parity_trn
, 0, 32, data
);
1754 buf_set_u32(data_parity_trn
, 32, 1, parity_u32(data
));
1756 jlink_queue_data_out(data_parity_trn
, 32 + 1);
1759 pending_scan_results_length
++;
1761 /* Insert idle cycles after AP accesses to avoid WAIT */
1762 if (cmd
& SWD_CMD_APnDP
)
1763 jlink_queue_data_out(NULL
, dap
->memaccess_tck
);
1766 /*****************************************************************************/
1767 /* JLink USB low-level functions */
1769 static struct jlink
*jlink_usb_open()
1771 struct jtag_libusb_device_handle
*devh
;
1772 if (jtag_libusb_open(vids
, pids
, jlink_serial
, &devh
) != ERROR_OK
)
1775 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
1776 * AREA!!!!!!!!!!! The behavior of libusb is not completely
1777 * consistent across Windows, Linux, and Mac OS X platforms.
1778 * The actions taken in the following compiler conditionals may
1779 * not agree with published documentation for libusb, but were
1780 * found to be necessary through trials and tribulations. Even
1781 * little tweaks can break one or more platforms, so if you do
1782 * make changes test them carefully on all platforms before
1786 /* This entire block can probably be removed. It was a workaround for
1787 * libusb0.1 and old JLink firmware. It has already be removed for
1788 * windows and causing problems (LPC Link-2 with JLink firmware) on
1789 * Linux with libusb1.0.
1791 * However, for now the behavior will be left unchanged for non-windows
1792 * platforms using libusb0.1 due to lack of testing.
1794 #if IS_WIN32 == 0 && HAVE_LIBUSB1 == 0
1796 jtag_libusb_reset_device(devh
);
1801 /* reopen jlink after usb_reset
1802 * on win32 this may take a second or two to re-enumerate */
1804 while ((retval
= jtag_libusb_open(vids
, pids
, jlink_serial
, &devh
)) != ERROR_OK
) {
1810 if (ERROR_OK
!= retval
)
1816 /* usb_set_configuration is only required under win32
1817 * with libusb 0.1 and libusb0.sys. For libusb 1.0 it is a no-op
1818 * since the configuration is already set. */
1819 jtag_libusb_set_configuration(devh
, 0);
1821 jtag_libusb_choose_interface(devh
, &jlink_read_ep
, &jlink_write_ep
,
1822 JLINK_USB_INTERFACE_CLASS
,
1823 JLINK_USB_INTERFACE_SUBCLASS
,
1824 JLINK_USB_INTERFACE_PROTOCOL
);
1826 struct jlink
*result
= malloc(sizeof(struct jlink
));
1827 result
->usb_handle
= devh
;
1831 static void jlink_usb_close(struct jlink
*jlink
)
1833 jtag_libusb_close(jlink
->usb_handle
);
1837 /* Send a message and receive the reply. */
1838 static int jlink_usb_message(struct jlink
*jlink
, int out_length
, int in_length
)
1842 result
= jlink_usb_write(jlink
, out_length
);
1843 if (result
!= out_length
) {
1844 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1845 out_length
, result
);
1846 return ERROR_JTAG_DEVICE_ERROR
;
1849 result
= jlink_usb_read(jlink
, in_length
);
1850 if (result
!= in_length
) {
1851 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1853 return ERROR_JTAG_DEVICE_ERROR
;
1858 /* calls the given usb_bulk_* function, allowing for the data to
1859 * trickle in with some timeouts */
1860 static int usb_bulk_with_retries(
1861 int (*f
)(jtag_libusb_device_handle
*, int, char *, int, int),
1862 jtag_libusb_device_handle
*dev
, int ep
,
1863 char *bytes
, int size
, int timeout
)
1865 int tries
= 3, count
= 0;
1867 while (tries
&& (count
< size
)) {
1868 int result
= f(dev
, ep
, bytes
+ count
, size
- count
, timeout
);
1871 else if ((-ETIMEDOUT
!= result
) || !--tries
)
1877 static int wrap_usb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
,
1878 char *buff
, int size
, int timeout
)
1880 /* usb_bulk_write() takes const char *buff */
1881 return jtag_libusb_bulk_write(dev
, ep
, buff
, size
, timeout
);
1884 static inline int usb_bulk_write_ex(jtag_libusb_device_handle
*dev
, int ep
,
1885 char *bytes
, int size
, int timeout
)
1887 return usb_bulk_with_retries(&wrap_usb_bulk_write
,
1888 dev
, ep
, bytes
, size
, timeout
);
1891 static inline int usb_bulk_read_ex(jtag_libusb_device_handle
*dev
, int ep
,
1892 char *bytes
, int size
, int timeout
)
1894 return usb_bulk_with_retries(&jtag_libusb_bulk_read
,
1895 dev
, ep
, bytes
, size
, timeout
);
1898 /* Write data from out_buffer to USB. */
1899 static int jlink_usb_write(struct jlink
*jlink
, int out_length
)
1903 if (out_length
> JLINK_OUT_BUFFER_SIZE
) {
1904 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
1905 out_length
, JLINK_OUT_BUFFER_SIZE
);
1909 result
= usb_bulk_write_ex(jlink
->usb_handle
, jlink_write_ep
,
1910 (char *)usb_out_buffer
, out_length
, JLINK_USB_TIMEOUT
);
1912 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
1913 out_length
, result
);
1915 jlink_debug_buffer(usb_out_buffer
, out_length
);
1919 /* Read data from USB into in_buffer. */
1920 static int jlink_usb_read(struct jlink
*jlink
, int expected_size
)
1922 int result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1923 (char *)usb_in_buffer
, expected_size
, JLINK_USB_TIMEOUT
);
1925 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result
);
1927 jlink_debug_buffer(usb_in_buffer
, result
);
1932 * Send a message and receive the reply - simple messages.
1934 * @param jlink pointer to driver data
1935 * @param out_length data length in @c usb_out_buffer
1936 * @param in_length data length to be read to @c usb_in_buffer
1938 static int jlink_usb_io(struct jlink
*jlink
, int out_length
, int in_length
)
1942 result
= jlink_usb_write(jlink
, out_length
);
1943 if (result
!= out_length
) {
1944 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1945 out_length
, result
);
1946 return ERROR_JTAG_DEVICE_ERROR
;
1949 result
= jlink_usb_read(jlink
, in_length
);
1950 if (result
!= in_length
) {
1951 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1953 return ERROR_JTAG_DEVICE_ERROR
;
1957 * Section 4.2.4 IN-transaction:
1958 * read dummy 0-byte packet if transaction size is
1959 * multiple of 64 bytes but not max. size of 0x8000
1961 if ((in_length
% 64) == 0 && in_length
!= 0x8000) {
1963 result
= usb_bulk_read_ex(jlink
->usb_handle
, jlink_read_ep
,
1964 &dummy_buffer
, 1, JLINK_USB_TIMEOUT
);
1966 LOG_ERROR("dummy byte read failed");
1967 return ERROR_JTAG_DEVICE_ERROR
;
1973 #ifdef _DEBUG_USB_COMMS_
1974 #define BYTES_PER_LINE 16
1976 static void jlink_debug_buffer(uint8_t *buffer
, int length
)
1983 for (i
= 0; i
< length
; i
+= BYTES_PER_LINE
) {
1984 snprintf(line
, 5, "%04x", i
);
1985 for (j
= i
; j
< i
+ BYTES_PER_LINE
&& j
< length
; j
++) {
1986 snprintf(s
, 4, " %02x", buffer
[j
]);
1989 LOG_DEBUG("%s", line
);