edf972575ca994692e47277bc5372675c28810d3
[openocd.git] / src / jtag / drivers / jlink.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
9 * plagnioj@jcrosoft.com *
10 * *
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. *
15 * *
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. *
20 * *
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 ***************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <jtag/interface.h>
32 #include <jtag/commands.h>
33 #include "libusb_common.h"
34
35 /* See Segger's public documentation:
36 * Reference manual for J-Link USB Protocol
37 * Document RM08001-R6 Date: June 16, 2009
38 * (Or newer, with some SWD information).
39 * http://www.segger.com/cms/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
40 */
41
42 /*
43 * The default pid of the segger is 0x0101
44 * But when you change the USB Address it will also
45 *
46 * pid = ( usb_address > 0x4) ? 0x0101 : (0x101 + usb_address)
47 */
48
49 #define VID 0x1366, 0x1366, 0x1366, 0x1366
50 #define PID 0x0101, 0x0102, 0x0103, 0x0104
51
52 #define JLINK_WRITE_ENDPOINT 0x02
53 #define JLINK_READ_ENDPOINT 0x81
54
55 static unsigned int jlink_write_ep = JLINK_WRITE_ENDPOINT;
56 static unsigned int jlink_read_ep = JLINK_READ_ENDPOINT;
57 static unsigned int jlink_hw_jtag_version = 2;
58
59 #define JLINK_USB_TIMEOUT 1000
60
61 /* See Section 3.3.2 of the Segger JLink USB protocol manual */
62 /* 2048 is the max value we can use here */
63 #define JLINK_TAP_BUFFER_SIZE 2048
64 /*#define JLINK_TAP_BUFFER_SIZE 256*/
65 /*#define JLINK_TAP_BUFFER_SIZE 384*/
66
67 #define JLINK_IN_BUFFER_SIZE 2048
68 #define JLINK_OUT_BUFFER_SIZE (2*2048 + 4)
69 #define JLINK_EMU_RESULT_BUFFER_SIZE 64
70
71 /* Global USB buffers */
72 static uint8_t usb_in_buffer[JLINK_IN_BUFFER_SIZE];
73 static uint8_t usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
74 static uint8_t usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
75
76 /* Constants for JLink command */
77 #define EMU_CMD_VERSION 0x01
78 #define EMU_CMD_RESET_TRST 0x02
79 #define EMU_CMD_RESET_TARGET 0x03
80 #define EMU_CMD_SET_SPEED 0x05
81 #define EMU_CMD_GET_STATE 0x07
82 #define EMU_CMD_SET_KS_POWER 0x08
83 #define EMU_CMD_GET_SPEEDS 0xc0
84 #define EMU_CMD_GET_HW_INFO 0xc1
85 #define EMU_CMD_GET_COUNTERS 0xc2
86 #define EMU_CMD_SELECT_IF 0xc7
87 #define EMU_CMD_HW_CLOCK 0xc8
88 #define EMU_CMD_HW_TMS0 0xc9
89 #define EMU_CMD_HW_TMS1 0xca
90 #define EMU_CMD_HW_DATA0 0xcb
91 #define EMU_CMD_HW_DATA1 0xcc
92 #define EMU_CMD_HW_JTAG 0xcd
93 #define EMU_CMD_HW_JTAG2 0xce
94 #define EMU_CMD_HW_JTAG3 0xcf
95 #define EMU_CMD_HW_RELEASE_RESET_STOP_EX 0xd0
96 #define EMU_CMD_HW_RELEASE_RESET_STOP_TIMED 0xd1
97 #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
98 #define EMU_CMD_HW_JTAG_WRITE 0xd5
99 #define EMU_CMD_HW_JTAG_GET_RESULT 0xd6
100 #define EMU_CMD_HW_RESET0 0xdc
101 #define EMU_CMD_HW_RESET1 0xdd
102 #define EMU_CMD_HW_TRST0 0xde
103 #define EMU_CMD_HW_TRST1 0xdf
104 #define EMU_CMD_GET_CAPS 0xe8
105 #define EMU_CMD_GET_CPU_CAPS 0xe9
106 #define EMU_CMD_EXEC_CPU_CMD 0xea
107 #define EMU_CMD_GET_CAPS_EX 0xed
108 #define EMU_CMD_GET_HW_VERSION 0xf0
109 #define EMU_CMD_WRITE_DCC 0xf1
110 #define EMU_CMD_READ_CONFIG 0xf2
111 #define EMU_CMD_WRITE_CONFIG 0xf3
112 #define EMU_CMD_WRITE_MEM 0xf4
113 #define EMU_CMD_READ_MEM 0xf5
114 #define EMU_CMD_MEASURE_RTCK_REACT 0xf6
115 #define EMU_CMD_WRITE_MEM_ARM79 0xf7
116 #define EMU_CMD_READ_MEM_ARM79 0xf8
117
118 /* bits return from EMU_CMD_GET_CAPS */
119 #define EMU_CAP_RESERVED_1 0
120 #define EMU_CAP_GET_HW_VERSION 1
121 #define EMU_CAP_WRITE_DCC 2
122 #define EMU_CAP_ADAPTIVE_CLOCKING 3
123 #define EMU_CAP_READ_CONFIG 4
124 #define EMU_CAP_WRITE_CONFIG 5
125 #define EMU_CAP_TRACE 6
126 #define EMU_CAP_WRITE_MEM 7
127 #define EMU_CAP_READ_MEM 8
128 #define EMU_CAP_SPEED_INFO 9
129 #define EMU_CAP_EXEC_CODE 10
130 #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
131 #define EMU_CAP_GET_HW_INFO 12
132 #define EMU_CAP_SET_KS_POWER 13
133 #define EMU_CAP_RESET_STOP_TIMED 14
134 #define EMU_CAP_RESERVED_2 15
135 #define EMU_CAP_MEASURE_RTCK_REACT 16
136 #define EMU_CAP_SELECT_IF 17
137 #define EMU_CAP_RW_MEM_ARM79 18
138 #define EMU_CAP_GET_COUNTERS 19
139 #define EMU_CAP_READ_DCC 20
140 #define EMU_CAP_GET_CPU_CAPS 21
141 #define EMU_CAP_EXEC_CPU_CMD 22
142 #define EMU_CAP_SWO 23
143 #define EMU_CAP_WRITE_DCC_EX 24
144 #define EMU_CAP_UPDATE_FIRMWARE_EX 25
145 #define EMU_CAP_FILE_IO 26
146 #define EMU_CAP_REGISTER 27
147 #define EMU_CAP_INDICATORS 28
148 #define EMU_CAP_TEST_NET_SPEED 29
149 #define EMU_CAP_RAWTRACE 30
150 #define EMU_CAP_RESERVED_3 31
151
152 static char *jlink_cap_str[] = {
153 "Always 1.",
154 "Supports command EMU_CMD_GET_HARDWARE_VERSION",
155 "Supports command EMU_CMD_WRITE_DCC",
156 "Supports adaptive clocking",
157 "Supports command EMU_CMD_READ_CONFIG",
158 "Supports command EMU_CMD_WRITE_CONFIG",
159 "Supports trace commands",
160 "Supports command EMU_CMD_WRITE_MEM",
161 "Supports command EMU_CMD_READ_MEM",
162 "Supports command EMU_CMD_GET_SPEED",
163 "Supports command EMU_CMD_CODE_...",
164 "Supports command EMU_CMD_GET_MAX_BLOCK_SIZE",
165 "Supports command EMU_CMD_GET_HW_INFO",
166 "Supports command EMU_CMD_SET_KS_POWER",
167 "Supports command EMU_CMD_HW_RELEASE_RESET_STOP_TIMED",
168 "Reserved",
169 "Supports command EMU_CMD_MEASURE_RTCK_REACT",
170 "Supports command EMU_CMD_HW_SELECT_IF",
171 "Supports command EMU_CMD_READ/WRITE_MEM_ARM79",
172 "Supports command EMU_CMD_GET_COUNTERS",
173 "Supports command EMU_CMD_READ_DCC",
174 "Supports command EMU_CMD_GET_CPU_CAPS",
175 "Supports command EMU_CMD_EXEC_CPU_CMD",
176 "Supports command EMU_CMD_SWO",
177 "Supports command EMU_CMD_WRITE_DCC_EX",
178 "Supports command EMU_CMD_UPDATE_FIRMWARE_EX",
179 "Supports command EMU_CMD_FILE_IO",
180 "Supports command EMU_CMD_REGISTER",
181 "Supports command EMU_CMD_INDICATORS",
182 "Supports command EMU_CMD_TEST_NET_SPEED",
183 "Supports command EMU_CMD_RAWTRACE",
184 "Reserved",
185 };
186
187 /* max speed 12MHz v5.0 jlink */
188 #define JLINK_MAX_SPEED 12000
189
190 /* J-Link hardware versions */
191 #define JLINK_HW_TYPE_JLINK 0
192 #define JLINK_HW_TYPE_JTRACE 1
193 #define JLINK_HW_TYPE_FLASHER 2
194 #define JLINK_HW_TYPE_JLINK_PRO 3
195 #define JLINK_HW_TYPE_MAX 4
196
197 static char *jlink_hw_type_str[] = {
198 "J-Link",
199 "J-Trace",
200 "Flasher",
201 "J-Link Pro",
202 };
203
204 /* Queue command functions */
205 static void jlink_end_state(tap_state_t state);
206 static void jlink_state_move(void);
207 static void jlink_path_move(int num_states, tap_state_t *path);
208 static void jlink_runtest(int num_cycles);
209 static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
210 int scan_size, struct scan_command *command);
211 static void jlink_reset(int trst, int srst);
212 static void jlink_simple_command(uint8_t command);
213 static int jlink_get_status(void);
214
215 /* J-Link tap buffer functions */
216 static void jlink_tap_init(void);
217 static int jlink_tap_execute(void);
218 static void jlink_tap_ensure_space(int scans, int bits);
219 static void jlink_tap_append_step(int tms, int tdi);
220 static void jlink_tap_append_scan(int length, uint8_t *buffer,
221 struct scan_command *command);
222
223 /* Jlink lowlevel functions */
224 struct jlink {
225 struct jtag_libusb_device_handle *usb_handle;
226 };
227
228 static struct jlink *jlink_usb_open(void);
229 static void jlink_usb_close(struct jlink *jlink);
230 static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length);
231 static int jlink_usb_io(struct jlink *jlink, int out_length, int in_length);
232 static int jlink_usb_write(struct jlink *jlink, int out_length);
233 static int jlink_usb_read(struct jlink *jlink, int expected_size);
234 static int jlink_usb_read_emu_result(struct jlink *jlink);
235
236 /* helper functions */
237 static int jlink_get_version_info(void);
238
239 #ifdef _DEBUG_USB_COMMS_
240 static void jlink_debug_buffer(uint8_t *buffer, int length);
241 #else
242 static inline void jlink_debug_buffer(uint8_t *buffer, int length)
243 {
244 }
245 #endif
246
247 static enum tap_state jlink_last_state = TAP_RESET;
248
249 static struct jlink *jlink_handle;
250
251 /* pid could be specified at runtime */
252 static uint16_t vids[] = { VID, 0 };
253 static uint16_t pids[] = { PID, 0 };
254
255 static uint32_t jlink_caps;
256 static uint32_t jlink_hw_type;
257
258 /* 256 byte non-volatile memory */
259 struct jlink_config {
260 uint8_t usb_address;
261 /* 0ffset 0x01 to 0x03 */
262 uint8_t reserved_1[3];
263 uint32_t kickstart_power_on_jtag_pin_19;
264 /* 0ffset 0x08 to 0x1f */
265 uint8_t reserved_2[24];
266 /* IP only for J-Link Pro */
267 uint8_t ip_address[4];
268 uint8_t subnet_mask[4];
269 /* 0ffset 0x28 to 0x2f */
270 uint8_t reserved_3[8];
271 uint8_t mac_address[6];
272 /* 0ffset 0x36 to 0xff */
273 uint8_t reserved_4[202];
274 } __attribute__ ((packed));
275 struct jlink_config jlink_cfg;
276
277 /***************************************************************************/
278 /* External interface implementation */
279
280 static void jlink_execute_runtest(struct jtag_command *cmd)
281 {
282 DEBUG_JTAG_IO("runtest %i cycles, end in %i",
283 cmd->cmd.runtest->num_cycles,
284 cmd->cmd.runtest->end_state);
285
286 jlink_end_state(cmd->cmd.runtest->end_state);
287
288 jlink_runtest(cmd->cmd.runtest->num_cycles);
289 }
290
291 static void jlink_execute_statemove(struct jtag_command *cmd)
292 {
293 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
294
295 jlink_end_state(cmd->cmd.statemove->end_state);
296 jlink_state_move();
297 }
298
299 static void jlink_execute_pathmove(struct jtag_command *cmd)
300 {
301 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
302 cmd->cmd.pathmove->num_states,
303 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
304
305 jlink_path_move(cmd->cmd.pathmove->num_states,
306 cmd->cmd.pathmove->path);
307 }
308
309 static void jlink_execute_scan(struct jtag_command *cmd)
310 {
311 int scan_size;
312 enum scan_type type;
313 uint8_t *buffer;
314
315 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd->cmd.scan->end_state));
316
317 jlink_end_state(cmd->cmd.scan->end_state);
318
319 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
320 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
321
322 jlink_debug_buffer(buffer, (scan_size + 7) / 8);
323 type = jtag_scan_type(cmd->cmd.scan);
324 jlink_scan(cmd->cmd.scan->ir_scan,
325 type, buffer, scan_size, cmd->cmd.scan);
326 }
327
328 static void jlink_execute_reset(struct jtag_command *cmd)
329 {
330 DEBUG_JTAG_IO("reset trst: %i srst %i",
331 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
332
333 jlink_tap_execute();
334 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
335 jlink_tap_execute();
336 }
337
338 static void jlink_execute_sleep(struct jtag_command *cmd)
339 {
340 DEBUG_JTAG_IO("sleep %" PRIi32 "", cmd->cmd.sleep->us);
341 jlink_tap_execute();
342 jtag_sleep(cmd->cmd.sleep->us);
343 }
344
345 static void jlink_execute_command(struct jtag_command *cmd)
346 {
347 switch (cmd->type) {
348 case JTAG_RUNTEST:
349 jlink_execute_runtest(cmd);
350 break;
351 case JTAG_TLR_RESET:
352 jlink_execute_statemove(cmd);
353 break;
354 case JTAG_PATHMOVE:
355 jlink_execute_pathmove(cmd);
356 break;
357 case JTAG_SCAN:
358 jlink_execute_scan(cmd);
359 break;
360 case JTAG_RESET:
361 jlink_execute_reset(cmd);
362 break;
363 case JTAG_SLEEP:
364 jlink_execute_sleep(cmd);
365 break;
366 default:
367 LOG_ERROR("BUG: unknown JTAG command type encountered");
368 exit(-1);
369 }
370 }
371
372 static int jlink_execute_queue(void)
373 {
374 struct jtag_command *cmd = jtag_command_queue;
375
376 while (cmd != NULL) {
377 jlink_execute_command(cmd);
378 cmd = cmd->next;
379 }
380
381 return jlink_tap_execute();
382 }
383
384 /* Sets speed in kHz. */
385 static int jlink_speed(int speed)
386 {
387 int result;
388
389 if (speed > JLINK_MAX_SPEED) {
390 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
391 speed, JLINK_MAX_SPEED);
392 speed = JLINK_MAX_SPEED;
393 }
394
395 /* check for RTCK setting */
396 if (speed == 0)
397 speed = -1;
398
399 usb_out_buffer[0] = EMU_CMD_SET_SPEED;
400 usb_out_buffer[1] = (speed >> 0) & 0xff;
401 usb_out_buffer[2] = (speed >> 8) & 0xff;
402
403 result = jlink_usb_write(jlink_handle, 3);
404 if (result != 3) {
405 LOG_ERROR("J-Link setting speed failed (%d)", result);
406 return ERROR_JTAG_DEVICE_ERROR;
407 }
408
409 return ERROR_OK;
410 }
411
412 static int jlink_speed_div(int speed, int *khz)
413 {
414 *khz = speed;
415
416 return ERROR_OK;
417 }
418
419 static int jlink_khz(int khz, int *jtag_speed)
420 {
421 *jtag_speed = khz;
422
423 return ERROR_OK;
424 }
425
426 /*
427 * select transport interface
428 *
429 * @param iface [0..31] currently: 0=JTAG, 1=SWD
430 * @returns ERROR_OK or ERROR_ code
431 *
432 * @pre jlink_handle must be opened
433 * @pre function may be called only for devices, that have
434 * EMU_CAP_SELECT_IF capability enabled
435 */
436 static int jlink_select_interface(int iface)
437 {
438 /* According to Segger's document RM08001-R7 Date: October 8, 2010,
439 * http://www.segger.com/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
440 * section 5.5.3 EMU_CMD_SELECT_IF
441 * > SubCmd 1..31 to select interface (0..31)
442 *
443 * The table below states:
444 * 0 TIF_JTAG
445 * 1 TIF_SWD
446 *
447 * This obviosly means that to select TIF_JTAG one should write SubCmd = 1.
448 *
449 * In fact, JTAG interface operates when SubCmd=0
450 *
451 * It looks like a typo in documentation, because interfaces 0..31 could not
452 * be selected by 1..31 range command.
453 */
454 assert(iface >= 0 && iface < 32);
455 int result;
456
457 /* get available interfaces */
458 usb_out_buffer[0] = EMU_CMD_SELECT_IF;
459 usb_out_buffer[1] = 0xff;
460
461 result = jlink_usb_io(jlink_handle, 2, 4);
462 if (result != ERROR_OK) {
463 LOG_ERROR("J-Link query interface failed (%d)", result);
464 return ERROR_JTAG_DEVICE_ERROR;
465 }
466
467 uint32_t iface_mask = buf_get_u32(usb_in_buffer, 0, 32);
468
469 if (!(iface_mask & (1<<iface))) {
470 LOG_ERROR("J-Link requesting to select unsupported interface (%x)", iface_mask);
471 return ERROR_JTAG_DEVICE_ERROR;
472 }
473
474 /* Select interface */
475 usb_out_buffer[0] = EMU_CMD_SELECT_IF;
476 usb_out_buffer[1] = iface;
477
478 result = jlink_usb_io(jlink_handle, 2, 4);
479 if (result != ERROR_OK) {
480 LOG_ERROR("J-Link interface select failed (%d)", result);
481 return ERROR_JTAG_DEVICE_ERROR;
482 }
483
484 return ERROR_OK;
485 }
486
487 static int jlink_init(void)
488 {
489 int i;
490
491 jlink_handle = jlink_usb_open();
492
493 if (jlink_handle == 0) {
494 LOG_ERROR("Cannot find jlink Interface! Please check "
495 "connection and permissions.");
496 return ERROR_JTAG_INIT_FAILED;
497 }
498
499 /*
500 * The next three instructions were added after discovering a problem
501 * while using an oscilloscope.
502 * For the V8 SAM-ICE dongle (and likely other j-link device variants),
503 * the reset line to the target microprocessor was found to cycle only
504 * intermittently during emulator startup (even after encountering the
505 * downstream reset instruction later in the code).
506 * This was found to create two issues:
507 * 1) In general it is a bad practice to not reset a CPU to a known
508 * state when starting an emulator and
509 * 2) something critical happens inside the dongle when it does the
510 * first read following a new USB session.
511 * Keeping the processor in reset during the first read collecting
512 * version information seems to prevent errant
513 * "J-Link command EMU_CMD_VERSION failed" issues.
514 */
515
516 LOG_INFO("J-Link initialization started / target CPU reset initiated");
517 jlink_simple_command(EMU_CMD_HW_TRST0);
518 jlink_simple_command(EMU_CMD_HW_RESET0);
519 usleep(1000);
520
521 jlink_hw_jtag_version = 2;
522
523 if (jlink_get_version_info() == ERROR_OK) {
524 /* attempt to get status */
525 jlink_get_status();
526 }
527
528 /*
529 * Some versions of Segger's software do not select JTAG interface by default.
530 *
531 * Segger recommends to select interface necessarily as a part of init process,
532 * in case any previous session leaves improper interface selected.
533 *
534 * Until SWD implemented, select only JTAG interface here.
535 */
536 if (jlink_caps & (1<<EMU_CAP_SELECT_IF))
537 jlink_select_interface(0);
538
539 LOG_INFO("J-Link JTAG Interface ready");
540
541 jlink_reset(0, 0);
542 jtag_sleep(3000);
543 jlink_tap_init();
544
545 /* v5/6 jlink seems to have an issue if the first tap move
546 * is not divisible by 8, so we send a TLR on first power up */
547 for (i = 0; i < 8; i++)
548 jlink_tap_append_step(1, 0);
549 jlink_tap_execute();
550
551 return ERROR_OK;
552 }
553
554 static int jlink_quit(void)
555 {
556 jlink_usb_close(jlink_handle);
557 return ERROR_OK;
558 }
559
560 /***************************************************************************/
561 /* Queue command implementations */
562
563 static void jlink_end_state(tap_state_t state)
564 {
565 if (tap_is_state_stable(state))
566 tap_set_end_state(state);
567 else {
568 LOG_ERROR("BUG: %i is not a valid end state", state);
569 exit(-1);
570 }
571 }
572
573 /* Goes to the end state. */
574 static void jlink_state_move(void)
575 {
576 int i;
577 int tms = 0;
578 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
579 uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
580
581 for (i = 0; i < tms_scan_bits; i++) {
582 tms = (tms_scan >> i) & 1;
583 jlink_tap_append_step(tms, 0);
584 }
585
586 tap_set_state(tap_get_end_state());
587 }
588
589 static void jlink_path_move(int num_states, tap_state_t *path)
590 {
591 int i;
592
593 for (i = 0; i < num_states; i++) {
594 if (path[i] == tap_state_transition(tap_get_state(), false))
595 jlink_tap_append_step(0, 0);
596 else if (path[i] == tap_state_transition(tap_get_state(), true))
597 jlink_tap_append_step(1, 0);
598 else {
599 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
600 tap_state_name(tap_get_state()), tap_state_name(path[i]));
601 exit(-1);
602 }
603
604 tap_set_state(path[i]);
605 }
606
607 tap_set_end_state(tap_get_state());
608 }
609
610 static void jlink_runtest(int num_cycles)
611 {
612 int i;
613
614 tap_state_t saved_end_state = tap_get_end_state();
615
616 jlink_tap_ensure_space(1, num_cycles + 16);
617
618 /* only do a state_move when we're not already in IDLE */
619 if (tap_get_state() != TAP_IDLE) {
620 jlink_end_state(TAP_IDLE);
621 jlink_state_move();
622 /* num_cycles--; */
623 }
624
625 /* execute num_cycles */
626 for (i = 0; i < num_cycles; i++)
627 jlink_tap_append_step(0, 0);
628
629 /* finish in end_state */
630 jlink_end_state(saved_end_state);
631 if (tap_get_state() != tap_get_end_state())
632 jlink_state_move();
633 }
634
635 static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
636 int scan_size, struct scan_command *command)
637 {
638 tap_state_t saved_end_state;
639
640 jlink_tap_ensure_space(1, scan_size + 16);
641
642 saved_end_state = tap_get_end_state();
643
644 /* Move to appropriate scan state */
645 jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
646
647 /* Only move if we're not already there */
648 if (tap_get_state() != tap_get_end_state())
649 jlink_state_move();
650
651 jlink_end_state(saved_end_state);
652
653 /* Scan */
654 jlink_tap_append_scan(scan_size, buffer, command);
655
656 /* We are in Exit1, go to Pause */
657 jlink_tap_append_step(0, 0);
658
659 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
660
661 if (tap_get_state() != tap_get_end_state())
662 jlink_state_move();
663 }
664
665 static void jlink_reset(int trst, int srst)
666 {
667 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
668
669 /* Signals are active low */
670 if (srst == 0)
671 jlink_simple_command(EMU_CMD_HW_RESET1);
672
673 if (srst == 1)
674 jlink_simple_command(EMU_CMD_HW_RESET0);
675
676 if (trst == 1)
677 jlink_simple_command(EMU_CMD_HW_TRST0);
678
679 if (trst == 0)
680 jlink_simple_command(EMU_CMD_HW_TRST1);
681 }
682
683 static void jlink_simple_command(uint8_t command)
684 {
685 int result;
686
687 DEBUG_JTAG_IO("0x%02x", command);
688
689 usb_out_buffer[0] = command;
690 result = jlink_usb_write(jlink_handle, 1);
691
692 if (result != 1)
693 LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
694 }
695
696 static int jlink_get_status(void)
697 {
698 int result;
699
700 jlink_simple_command(EMU_CMD_GET_STATE);
701
702 result = jlink_usb_read(jlink_handle, 8);
703 if (result != 8) {
704 LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result);
705 return ERROR_JTAG_DEVICE_ERROR;
706 }
707
708 int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
709 LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
710 vref / 1000, vref % 1000, \
711 usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
712 usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
713
714 if (vref < 1500)
715 LOG_ERROR("Vref too low. Check Target Power");
716
717 return ERROR_OK;
718 }
719
720 #define jlink_dump_printf(context, expr ...) \
721 do { \
722 if (context) \
723 command_print(context, expr); \
724 else \
725 LOG_INFO(expr); \
726 } while (0);
727
728 static void jlink_caps_dump(struct command_context *ctx)
729 {
730 int i;
731
732 jlink_dump_printf(ctx, "J-Link Capabilities");
733
734 for (i = 1; i < 31; i++)
735 if (jlink_caps & (1 << i))
736 jlink_dump_printf(ctx, "%s", jlink_cap_str[i]);
737 }
738
739 static void jlink_config_usb_address_dump(struct command_context *ctx, struct jlink_config *cfg)
740 {
741 if (!cfg)
742 return;
743
744 jlink_dump_printf(ctx, "USB-Address: 0x%x", cfg->usb_address);
745 }
746
747 static void jlink_config_kickstart_dump(struct command_context *ctx, struct jlink_config *cfg)
748 {
749 if (!cfg)
750 return;
751
752 jlink_dump_printf(ctx, "Kickstart power on JTAG-pin 19: 0x%x",
753 cfg->kickstart_power_on_jtag_pin_19);
754 }
755
756 static void jlink_config_mac_address_dump(struct command_context *ctx, struct jlink_config *cfg)
757 {
758 if (!cfg)
759 return;
760
761 jlink_dump_printf(ctx, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
762 cfg->mac_address[5], cfg->mac_address[4],
763 cfg->mac_address[3], cfg->mac_address[2],
764 cfg->mac_address[1], cfg->mac_address[0]);
765 }
766
767 static void jlink_config_ip_dump(struct command_context *ctx, struct jlink_config *cfg)
768 {
769 if (!cfg)
770 return;
771
772 jlink_dump_printf(ctx, "IP Address: %d.%d.%d.%d",
773 cfg->ip_address[3], cfg->ip_address[2],
774 cfg->ip_address[1], cfg->ip_address[0]);
775 jlink_dump_printf(ctx, "Subnet Mask: %d.%d.%d.%d",
776 cfg->subnet_mask[3], cfg->subnet_mask[2],
777 cfg->subnet_mask[1], cfg->subnet_mask[0]);
778 }
779
780 static void jlink_config_dump(struct command_context *ctx, struct jlink_config *cfg)
781 {
782 if (!cfg)
783 return;
784
785 jlink_dump_printf(ctx, "J-Link configuration");
786 jlink_config_usb_address_dump(ctx, cfg);
787 jlink_config_kickstart_dump(ctx, cfg);
788
789 if (jlink_hw_type == JLINK_HW_TYPE_JLINK_PRO) {
790 jlink_config_ip_dump(ctx, cfg);
791 jlink_config_mac_address_dump(ctx, cfg);
792 }
793 }
794
795 static int jlink_get_config(struct jlink_config *cfg)
796 {
797 int result;
798 int size = sizeof(struct jlink_config);
799
800 usb_out_buffer[0] = EMU_CMD_READ_CONFIG;
801 result = jlink_usb_io(jlink_handle, 1, size);
802
803 if (result != ERROR_OK) {
804 LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size, result);
805 return ERROR_FAIL;
806 }
807
808 memcpy(cfg, usb_in_buffer, size);
809 return ERROR_OK;
810 }
811
812 static int jlink_set_config(struct jlink_config *cfg)
813 {
814 int result;
815 int size = sizeof(struct jlink_config);
816
817 jlink_simple_command(EMU_CMD_WRITE_CONFIG);
818
819 memcpy(usb_out_buffer, cfg, size);
820
821 result = jlink_usb_write(jlink_handle, size);
822 if (result != size) {
823 LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result);
824 return ERROR_FAIL;
825 }
826
827 return ERROR_OK;
828 }
829
830 /*
831 * List of unsupported version string markers.
832 *
833 * The firmware versions does not correspond directly with
834 * "Software and documentation pack for Windows", it may be
835 * distinguished by the "compile" date in the information string.
836 *
837 * For example, version string is:
838 * "J-Link ARM V8 compiled May 3 2012 18:36:22"
839 * Marker sould be:
840 * "May 3 2012"
841 *
842 * The list must be terminated by NULL string.
843 */
844 static const char * const unsupported_versions[] = {
845 "Jan 31 2011",
846 "JAN 31 2011",
847 NULL /* End of list */
848 };
849
850 static void jlink_check_supported(const char *str)
851 {
852 const char * const *p = unsupported_versions;
853 while (*p) {
854 if (NULL != strstr(str, *p)) {
855 LOG_WARNING(
856 "Unsupported J-Link firmware version.\n"
857 " Please check http://www.segger.com/j-link-older-versions.html for updates");
858 return;
859 }
860 p++;
861 }
862 }
863
864 static int jlink_get_version_info(void)
865 {
866 int result;
867 int len;
868 uint32_t jlink_max_size;
869
870 /* query hardware version */
871 jlink_simple_command(EMU_CMD_VERSION);
872
873 result = jlink_usb_read(jlink_handle, 2);
874 if (2 != result) {
875 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result);
876 return ERROR_JTAG_DEVICE_ERROR;
877 }
878
879 len = buf_get_u32(usb_in_buffer, 0, 16);
880 if (len > JLINK_IN_BUFFER_SIZE) {
881 LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len);
882 len = JLINK_IN_BUFFER_SIZE;
883 }
884
885 result = jlink_usb_read(jlink_handle, len);
886 if (result != len) {
887 LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result);
888 return ERROR_JTAG_DEVICE_ERROR;
889 }
890
891 usb_in_buffer[result] = 0;
892 LOG_INFO("%s", (char *)usb_in_buffer);
893 jlink_check_supported((char *)usb_in_buffer);
894
895 /* query hardware capabilities */
896 jlink_simple_command(EMU_CMD_GET_CAPS);
897
898 result = jlink_usb_read(jlink_handle, 4);
899 if (4 != result) {
900 LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result);
901 return ERROR_JTAG_DEVICE_ERROR;
902 }
903
904 jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
905 LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps);
906
907 if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION)) {
908 /* query hardware version */
909 jlink_simple_command(EMU_CMD_GET_HW_VERSION);
910
911 result = jlink_usb_read(jlink_handle, 4);
912 if (4 != result) {
913 LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result);
914 return ERROR_JTAG_DEVICE_ERROR;
915 }
916
917 uint32_t jlink_hw_version = buf_get_u32(usb_in_buffer, 0, 32);
918 uint32_t major_revision = (jlink_hw_version / 10000) % 100;
919 jlink_hw_type = (jlink_hw_version / 1000000) % 100;
920 if (major_revision >= 5)
921 jlink_hw_jtag_version = 3;
922
923 LOG_INFO("J-Link hw version %i", (int)jlink_hw_version);
924
925 if (jlink_hw_type >= JLINK_HW_TYPE_MAX)
926 LOG_INFO("J-Link hw type uknown 0x%x", jlink_hw_type);
927 else
928 LOG_INFO("J-Link hw type %s", jlink_hw_type_str[jlink_hw_type]);
929 }
930
931 if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE)) {
932 /* query hardware maximum memory block */
933 jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
934
935 result = jlink_usb_read(jlink_handle, 4);
936 if (4 != result) {
937 LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result);
938 return ERROR_JTAG_DEVICE_ERROR;
939 }
940
941 jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
942 LOG_INFO("J-Link max mem block %i", (int)jlink_max_size);
943 }
944
945 if (jlink_caps & (1 << EMU_CAP_READ_CONFIG)) {
946 if (jlink_get_config(&jlink_cfg) != ERROR_OK)
947 return ERROR_JTAG_DEVICE_ERROR;
948
949 jlink_config_dump(NULL, &jlink_cfg);
950 }
951
952 return ERROR_OK;
953 }
954
955 COMMAND_HANDLER(jlink_pid_command)
956 {
957 if (CMD_ARGC != 1) {
958 LOG_ERROR("Need exactly one argument to jlink_pid");
959 return ERROR_FAIL;
960 }
961
962 pids[0] = strtoul(CMD_ARGV[0], NULL, 16);
963 pids[1] = 0;
964 vids[1] = 0;
965
966 return ERROR_OK;
967 }
968
969 COMMAND_HANDLER(jlink_handle_jlink_info_command)
970 {
971 if (jlink_get_version_info() == ERROR_OK) {
972 /* attempt to get status */
973 jlink_get_status();
974 }
975
976 return ERROR_OK;
977 }
978
979 COMMAND_HANDLER(jlink_handle_jlink_caps_command)
980 {
981 jlink_caps_dump(CMD_CTX);
982
983 return ERROR_OK;
984 }
985
986 COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command)
987 {
988 switch (CMD_ARGC) {
989 case 0:
990 command_print(CMD_CTX, "J-Link hw jtag %i", jlink_hw_jtag_version);
991 break;
992 case 1: {
993 int request_version = atoi(CMD_ARGV[0]);
994 switch (request_version) {
995 case 2:
996 case 3:
997 jlink_hw_jtag_version = request_version;
998 break;
999 default:
1000 return ERROR_COMMAND_SYNTAX_ERROR;
1001 }
1002 break;
1003 }
1004 default:
1005 return ERROR_COMMAND_SYNTAX_ERROR;
1006 }
1007
1008 return ERROR_OK;
1009 }
1010
1011 COMMAND_HANDLER(jlink_handle_jlink_kickstart_command)
1012 {
1013 uint32_t kickstart;
1014
1015 if (CMD_ARGC < 1) {
1016 jlink_config_kickstart_dump(CMD_CTX, &jlink_cfg);
1017 return ERROR_OK;
1018 }
1019
1020 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], kickstart);
1021
1022 jlink_cfg.kickstart_power_on_jtag_pin_19 = kickstart;
1023 return ERROR_OK;
1024 }
1025
1026 COMMAND_HANDLER(jlink_handle_jlink_mac_address_command)
1027 {
1028 uint8_t addr[6];
1029 int i;
1030 char *e;
1031 const char *str;
1032
1033 if (CMD_ARGC < 1) {
1034 jlink_config_mac_address_dump(CMD_CTX, &jlink_cfg);
1035 return ERROR_OK;
1036 }
1037
1038 str = CMD_ARGV[0];
1039
1040 if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || str[8] != ':' ||
1041 str[11] != ':' || str[14] != ':')) {
1042 command_print(CMD_CTX, "ethaddr miss format ff:ff:ff:ff:ff:ff");
1043 return ERROR_COMMAND_SYNTAX_ERROR;
1044 }
1045
1046 for (i = 5; i >= 0; i--) {
1047 addr[i] = strtoul(str, &e, 16);
1048 str = e + 1;
1049 }
1050
1051 if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1052 command_print(CMD_CTX, "invalid it's zero mac_address");
1053 return ERROR_COMMAND_SYNTAX_ERROR;
1054 }
1055
1056 if (!(0x01 & addr[0])) {
1057 command_print(CMD_CTX, "invalid it's a multicat mac_address");
1058 return ERROR_COMMAND_SYNTAX_ERROR;
1059 }
1060
1061 memcpy(jlink_cfg.mac_address, addr, sizeof(addr));
1062
1063 return ERROR_OK;
1064 }
1065
1066 static int string_to_ip(const char *s, uint8_t *ip, int *pos)
1067 {
1068 uint8_t lip[4];
1069 char *e;
1070 const char *s_save = s;
1071 int i;
1072
1073 if (!s)
1074 return -EINVAL;
1075
1076 for (i = 0; i < 4; i++) {
1077 lip[i] = strtoul(s, &e, 10);
1078
1079 if (*e != '.' && i != 3)
1080 return -EINVAL;
1081
1082 s = e + 1;
1083 }
1084
1085 *pos = e - s_save;
1086
1087 memcpy(ip, lip, sizeof(lip));
1088 return ERROR_OK;
1089 }
1090
1091 static void cpy_ip(uint8_t *dst, uint8_t *src)
1092 {
1093 int i, j;
1094
1095 for (i = 0, j = 3; i < 4; i++, j--)
1096 dst[i] = src[j];
1097 }
1098
1099 COMMAND_HANDLER(jlink_handle_jlink_ip_command)
1100 {
1101 uint32_t ip_address;
1102 uint32_t subnet_mask = 0;
1103 int i, len;
1104 int ret;
1105 uint8_t subnet_bits = 24;
1106
1107 if (CMD_ARGC < 1) {
1108 jlink_config_ip_dump(CMD_CTX, &jlink_cfg);
1109 return ERROR_OK;
1110 }
1111
1112 ret = string_to_ip(CMD_ARGV[0], (uint8_t *)&ip_address, &i);
1113 if (ret != ERROR_OK)
1114 return ret;
1115
1116 len = strlen(CMD_ARGV[0]);
1117
1118 /* check for this format A.B.C.D/E */
1119
1120 if (i < len) {
1121 if (CMD_ARGV[0][i] != '/')
1122 return ERROR_COMMAND_SYNTAX_ERROR;
1123
1124 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1125 } else {
1126 if (CMD_ARGC > 1) {
1127 ret = string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i);
1128 if (ret != ERROR_OK)
1129 return ret;
1130 }
1131 }
1132
1133 if (!subnet_mask)
1134 subnet_mask = (uint32_t)(subnet_bits < 32 ?
1135 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1136
1137 cpy_ip(jlink_cfg.ip_address, (uint8_t *)&ip_address);
1138 cpy_ip(jlink_cfg.subnet_mask, (uint8_t *)&subnet_mask);
1139
1140 return ERROR_OK;
1141 }
1142
1143 COMMAND_HANDLER(jlink_handle_jlink_reset_command)
1144 {
1145 memset(&jlink_cfg, 0xff, sizeof(jlink_cfg));
1146 return ERROR_OK;
1147 }
1148
1149 COMMAND_HANDLER(jlink_handle_jlink_save_command)
1150 {
1151 if (!(jlink_caps & (1 << EMU_CAP_WRITE_CONFIG))) {
1152 command_print(CMD_CTX, "J-Link write emulator configuration not supported");
1153 return ERROR_OK;
1154 }
1155
1156 command_print(CMD_CTX, "The J-Link need to be unpluged and repluged ta have the config effective");
1157 return jlink_set_config(&jlink_cfg);
1158 }
1159
1160 COMMAND_HANDLER(jlink_handle_jlink_usb_address_command)
1161 {
1162 uint32_t address;
1163
1164 if (CMD_ARGC < 1) {
1165 jlink_config_usb_address_dump(CMD_CTX, &jlink_cfg);
1166 return ERROR_OK;
1167 }
1168
1169 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
1170
1171 if (address > 0x3 && address != 0xff) {
1172 command_print(CMD_CTX, "USB Address must be between 0x00 and 0x03 or 0xff");
1173 return ERROR_COMMAND_SYNTAX_ERROR;
1174 }
1175
1176 jlink_cfg.usb_address = address;
1177 return ERROR_OK;
1178 }
1179
1180 COMMAND_HANDLER(jlink_handle_jlink_config_command)
1181 {
1182 struct jlink_config cfg;
1183 int ret = ERROR_OK;
1184
1185 if (CMD_ARGC == 0) {
1186 if (!(jlink_caps & (1 << EMU_CAP_READ_CONFIG))) {
1187 command_print(CMD_CTX, "J-Link read emulator configuration not supported");
1188 goto exit;
1189 }
1190
1191 ret = jlink_get_config(&cfg);
1192
1193 if (ret != ERROR_OK)
1194 command_print(CMD_CTX, "J-Link read emulator configuration failled");
1195 else
1196 jlink_config_dump(CMD_CTX, &jlink_cfg);
1197 }
1198
1199 exit:
1200 return ret;
1201 }
1202
1203 static const struct command_registration jlink_config_subcommand_handlers[] = {
1204 {
1205 .name = "kickstart",
1206 .handler = &jlink_handle_jlink_kickstart_command,
1207 .mode = COMMAND_EXEC,
1208 .help = "set Kickstart power on JTAG-pin 19.",
1209 .usage = "[val]",
1210 },
1211 {
1212 .name = "mac_address",
1213 .handler = &jlink_handle_jlink_mac_address_command,
1214 .mode = COMMAND_EXEC,
1215 .help = "set the MAC Address",
1216 .usage = "[ff:ff:ff:ff:ff:ff]",
1217 },
1218 {
1219 .name = "ip",
1220 .handler = &jlink_handle_jlink_ip_command,
1221 .mode = COMMAND_EXEC,
1222 .help = "set the ip address of the J-Link Pro, "
1223 "where A.B.C.D is the ip, "
1224 "E the bit of the subnet mask, "
1225 "F.G.H.I the subnet mask",
1226 .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1227 },
1228 {
1229 .name = "reset",
1230 .handler = &jlink_handle_jlink_reset_command,
1231 .mode = COMMAND_EXEC,
1232 .help = "reset the current config",
1233 },
1234 {
1235 .name = "save",
1236 .handler = &jlink_handle_jlink_save_command,
1237 .mode = COMMAND_EXEC,
1238 .help = "save the current config",
1239 },
1240 {
1241 .name = "usb_address",
1242 .handler = &jlink_handle_jlink_usb_address_command,
1243 .mode = COMMAND_EXEC,
1244 .help = "set the USB-Address, "
1245 "This will change the product id",
1246 .usage = "[0x00 to 0x03 or 0xff]",
1247 },
1248 COMMAND_REGISTRATION_DONE
1249 };
1250
1251 static const struct command_registration jlink_subcommand_handlers[] = {
1252 {
1253 .name = "caps",
1254 .handler = &jlink_handle_jlink_caps_command,
1255 .mode = COMMAND_EXEC,
1256 .help = "show jlink capabilities",
1257 },
1258 {
1259 .name = "info",
1260 .handler = &jlink_handle_jlink_info_command,
1261 .mode = COMMAND_EXEC,
1262 .help = "show jlink info",
1263 },
1264 {
1265 .name = "hw_jtag",
1266 .handler = &jlink_handle_jlink_hw_jtag_command,
1267 .mode = COMMAND_EXEC,
1268 .help = "access J-Link HW JTAG command version",
1269 .usage = "[2|3]",
1270 },
1271 {
1272 .name = "config",
1273 .handler = &jlink_handle_jlink_config_command,
1274 .mode = COMMAND_EXEC,
1275 .help = "access J-Link configuration, "
1276 "if no argument this will dump the config",
1277 .chain = jlink_config_subcommand_handlers,
1278 },
1279 {
1280 .name = "pid",
1281 .handler = &jlink_pid_command,
1282 .mode = COMMAND_CONFIG,
1283 .help = "set the pid of the interface we want to use",
1284 },
1285 COMMAND_REGISTRATION_DONE
1286 };
1287
1288 static const struct command_registration jlink_command_handlers[] = {
1289 {
1290 .name = "jlink",
1291 .mode = COMMAND_ANY,
1292 .help = "perform jlink management",
1293 .chain = jlink_subcommand_handlers,
1294 },
1295 COMMAND_REGISTRATION_DONE
1296 };
1297
1298 struct jtag_interface jlink_interface = {
1299 .name = "jlink",
1300 .commands = jlink_command_handlers,
1301 .transports = jtag_only,
1302
1303 .execute_queue = jlink_execute_queue,
1304 .speed = jlink_speed,
1305 .speed_div = jlink_speed_div,
1306 .khz = jlink_khz,
1307 .init = jlink_init,
1308 .quit = jlink_quit,
1309 };
1310
1311 /***************************************************************************/
1312 /* J-Link tap functions */
1313
1314
1315 static unsigned tap_length;
1316 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
1317 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
1318 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
1319
1320 struct pending_scan_result {
1321 int first; /* First bit position in tdo_buffer to read */
1322 int length; /* Number of bits to read */
1323 struct scan_command *command; /* Corresponding scan command */
1324 uint8_t *buffer;
1325 };
1326
1327 #define MAX_PENDING_SCAN_RESULTS 256
1328
1329 static int pending_scan_results_length;
1330 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1331
1332 static void jlink_tap_init(void)
1333 {
1334 tap_length = 0;
1335 pending_scan_results_length = 0;
1336 }
1337
1338 static void jlink_tap_ensure_space(int scans, int bits)
1339 {
1340 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1341 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
1342
1343 if (scans > available_scans || bits > available_bits)
1344 jlink_tap_execute();
1345 }
1346
1347 static void jlink_tap_append_step(int tms, int tdi)
1348 {
1349 int index_var = tap_length / 8;
1350
1351 assert(index_var < JLINK_TAP_BUFFER_SIZE);
1352
1353 int bit_index = tap_length % 8;
1354 uint8_t bit = 1 << bit_index;
1355
1356 /* we do not pad TMS, so be sure to initialize all bits */
1357 if (0 == bit_index)
1358 tms_buffer[index_var] = tdi_buffer[index_var] = 0;
1359
1360 if (tms)
1361 tms_buffer[index_var] |= bit;
1362 else
1363 tms_buffer[index_var] &= ~bit;
1364
1365 if (tdi)
1366 tdi_buffer[index_var] |= bit;
1367 else
1368 tdi_buffer[index_var] &= ~bit;
1369
1370 tap_length++;
1371 }
1372
1373 static void jlink_tap_append_scan(int length, uint8_t *buffer,
1374 struct scan_command *command)
1375 {
1376 struct pending_scan_result *pending_scan_result =
1377 &pending_scan_results_buffer[pending_scan_results_length];
1378 int i;
1379
1380 pending_scan_result->first = tap_length;
1381 pending_scan_result->length = length;
1382 pending_scan_result->command = command;
1383 pending_scan_result->buffer = buffer;
1384
1385 for (i = 0; i < length; i++) {
1386 int tms = (i < (length - 1)) ? 0 : 1;
1387 int tdi = (buffer[i / 8] & (1 << (i % 8))) != 0;
1388 jlink_tap_append_step(tms, tdi);
1389 }
1390 pending_scan_results_length++;
1391 }
1392
1393 /* Pad and send a tap sequence to the device, and receive the answer.
1394 * For the purpose of padding we assume that we are in idle or pause state. */
1395 static int jlink_tap_execute(void)
1396 {
1397 int byte_length;
1398 int i;
1399 int result;
1400
1401 if (!tap_length)
1402 return ERROR_OK;
1403
1404 /* JLink returns an extra NULL in packet when size of incoming
1405 * message is a multiple of 64, creates problems with USB comms.
1406 * WARNING: This will interfere with tap state counting. */
1407 while ((DIV_ROUND_UP(tap_length, 8) % 64) == 0)
1408 jlink_tap_append_step((tap_get_state() == TAP_RESET) ? 1 : 0, 0);
1409
1410 /* number of full bytes (plus one if some would be left over) */
1411 byte_length = DIV_ROUND_UP(tap_length, 8);
1412
1413 bool use_jtag3 = jlink_hw_jtag_version >= 3;
1414 usb_out_buffer[0] = use_jtag3 ? EMU_CMD_HW_JTAG3 : EMU_CMD_HW_JTAG2;
1415 usb_out_buffer[1] = 0;
1416 usb_out_buffer[2] = (tap_length >> 0) & 0xff;
1417 usb_out_buffer[3] = (tap_length >> 8) & 0xff;
1418 memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
1419 memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
1420
1421 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
1422 tap_length, jlink_last_state);
1423
1424 result = jlink_usb_message(jlink_handle, 4 + 2 * byte_length, byte_length);
1425 if (result != byte_length) {
1426 LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)",
1427 result, byte_length);
1428 jlink_tap_init();
1429 return ERROR_JTAG_QUEUE_FAILED;
1430 }
1431
1432 memcpy(tdo_buffer, usb_in_buffer, byte_length);
1433
1434 for (i = 0; i < pending_scan_results_length; i++) {
1435 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
1436 uint8_t *buffer = pending_scan_result->buffer;
1437 int length = pending_scan_result->length;
1438 int first = pending_scan_result->first;
1439 struct scan_command *command = pending_scan_result->command;
1440
1441 /* Copy to buffer */
1442 buf_set_buf(tdo_buffer, first, buffer, 0, length);
1443
1444 DEBUG_JTAG_IO("pending scan result, length = %d", length);
1445
1446 jlink_debug_buffer(buffer, DIV_ROUND_UP(length, 8));
1447
1448 if (jtag_read_buffer(buffer, command) != ERROR_OK) {
1449 jlink_tap_init();
1450 return ERROR_JTAG_QUEUE_FAILED;
1451 }
1452
1453 if (pending_scan_result->buffer != NULL)
1454 free(pending_scan_result->buffer);
1455 }
1456
1457 jlink_tap_init();
1458 return ERROR_OK;
1459 }
1460
1461 /*****************************************************************************/
1462 /* JLink USB low-level functions */
1463
1464 static struct jlink *jlink_usb_open()
1465 {
1466 struct jtag_libusb_device_handle *devh;
1467 if (jtag_libusb_open(vids, pids, &devh) != ERROR_OK)
1468 return NULL;
1469
1470 /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
1471 * AREA!!!!!!!!!!! The behavior of libusb is not completely
1472 * consistent across Windows, Linux, and Mac OS X platforms.
1473 * The actions taken in the following compiler conditionals may
1474 * not agree with published documentation for libusb, but were
1475 * found to be necessary through trials and tribulations. Even
1476 * little tweaks can break one or more platforms, so if you do
1477 * make changes test them carefully on all platforms before
1478 * committing them!
1479 */
1480
1481 #if IS_WIN32 == 0
1482
1483 jtag_libusb_reset_device(devh);
1484
1485 #if IS_DARWIN == 0
1486
1487 int timeout = 5;
1488 /* reopen jlink after usb_reset
1489 * on win32 this may take a second or two to re-enumerate */
1490 int retval;
1491 while ((retval = jtag_libusb_open(vids, pids, &devh)) != ERROR_OK) {
1492 usleep(1000);
1493 timeout--;
1494 if (!timeout)
1495 break;
1496 }
1497 if (ERROR_OK != retval)
1498 return NULL;
1499 #endif
1500
1501 #endif
1502
1503 /* usb_set_configuration required under win32 */
1504 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
1505 jtag_libusb_set_configuration(devh, 0);
1506 jtag_libusb_claim_interface(devh, 0);
1507
1508 #if 0
1509 /*
1510 * This makes problems under Mac OS X. And is not needed
1511 * under Windows. Hopefully this will not break a linux build
1512 */
1513 usb_set_altinterface(result->usb_handle, 0);
1514 #endif
1515
1516 jtag_libusb_get_endpoints(udev, &jlink_read_ep, &jlink_write_ep);
1517
1518 struct jlink *result = malloc(sizeof(struct jlink));
1519 result->usb_handle = devh;
1520 return result;
1521 }
1522
1523 static void jlink_usb_close(struct jlink *jlink)
1524 {
1525 jtag_libusb_close(jlink->usb_handle);
1526 free(jlink);
1527 }
1528
1529 /* Send a message and receive the reply. */
1530 static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length)
1531 {
1532 int result;
1533
1534 result = jlink_usb_write(jlink, out_length);
1535 if (result != out_length) {
1536 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1537 out_length, result);
1538 return ERROR_JTAG_DEVICE_ERROR;
1539 }
1540
1541 result = jlink_usb_read(jlink, in_length);
1542 if ((result != in_length) && (result != (in_length + 1))) {
1543 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1544 in_length, result);
1545 return ERROR_JTAG_DEVICE_ERROR;
1546 }
1547
1548 if (jlink_hw_jtag_version < 3)
1549 return result;
1550
1551 int result2 = ERROR_OK;
1552 if (result == in_length) {
1553 /* Must read the result from the EMU too */
1554 result2 = jlink_usb_read_emu_result(jlink);
1555 if (1 != result2) {
1556 LOG_ERROR("jlink_usb_read_emu_result retried requested = 1, "
1557 "result=%d, in_length=%i", result2, in_length);
1558 /* Try again once, should only happen if (in_length%64 == 0) */
1559 result2 = jlink_usb_read_emu_result(jlink);
1560 if (1 != result2) {
1561 LOG_ERROR("jlink_usb_read_emu_result failed "
1562 "(requested = 1, result=%d)", result2);
1563 return ERROR_JTAG_DEVICE_ERROR;
1564 }
1565 }
1566
1567 /* Check the result itself */
1568 result2 = usb_emu_result_buffer[0];
1569 } else {
1570 /* Save the result, then remove it from return value */
1571 result2 = usb_in_buffer[result--];
1572 }
1573
1574 if (result2) {
1575 LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
1576 return ERROR_JTAG_DEVICE_ERROR;
1577 }
1578
1579 return result;
1580 }
1581
1582 /* calls the given usb_bulk_* function, allowing for the data to
1583 * trickle in with some timeouts */
1584 static int usb_bulk_with_retries(
1585 int (*f)(jtag_libusb_device_handle *, int, char *, int, int),
1586 jtag_libusb_device_handle *dev, int ep,
1587 char *bytes, int size, int timeout)
1588 {
1589 int tries = 3, count = 0;
1590
1591 while (tries && (count < size)) {
1592 int result = f(dev, ep, bytes + count, size - count, timeout);
1593 if (result > 0)
1594 count += result;
1595 else if ((-ETIMEDOUT != result) || !--tries)
1596 return result;
1597 }
1598 return count;
1599 }
1600
1601 static int wrap_usb_bulk_write(jtag_libusb_device_handle *dev, int ep,
1602 char *buff, int size, int timeout)
1603 {
1604 /* usb_bulk_write() takes const char *buff */
1605 return jtag_libusb_bulk_write(dev, ep, buff, size, timeout);
1606 }
1607
1608 static inline int usb_bulk_write_ex(jtag_libusb_device_handle *dev, int ep,
1609 char *bytes, int size, int timeout)
1610 {
1611 return usb_bulk_with_retries(&wrap_usb_bulk_write,
1612 dev, ep, bytes, size, timeout);
1613 }
1614
1615 static inline int usb_bulk_read_ex(jtag_libusb_device_handle *dev, int ep,
1616 char *bytes, int size, int timeout)
1617 {
1618 return usb_bulk_with_retries(&jtag_libusb_bulk_read,
1619 dev, ep, bytes, size, timeout);
1620 }
1621
1622 /* Write data from out_buffer to USB. */
1623 static int jlink_usb_write(struct jlink *jlink, int out_length)
1624 {
1625 int result;
1626
1627 if (out_length > JLINK_OUT_BUFFER_SIZE) {
1628 LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
1629 out_length, JLINK_OUT_BUFFER_SIZE);
1630 return -1;
1631 }
1632
1633 result = usb_bulk_write_ex(jlink->usb_handle, jlink_write_ep,
1634 (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
1635
1636 DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
1637 out_length, result);
1638
1639 jlink_debug_buffer(usb_out_buffer, out_length);
1640 return result;
1641 }
1642
1643 /* Read data from USB into in_buffer. */
1644 static int jlink_usb_read(struct jlink *jlink, int expected_size)
1645 {
1646 int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
1647 (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
1648
1649 DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
1650
1651 jlink_debug_buffer(usb_in_buffer, result);
1652 return result;
1653 }
1654
1655 /* Read the result from the previous EMU cmd into result_buffer. */
1656 static int jlink_usb_read_emu_result(struct jlink *jlink)
1657 {
1658 int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
1659 (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
1660 JLINK_USB_TIMEOUT);
1661
1662 DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
1663
1664 jlink_debug_buffer(usb_emu_result_buffer, result);
1665 return result;
1666 }
1667
1668 /*
1669 * Send a message and receive the reply - simple messages.
1670 *
1671 * @param jlink pointer to driver data
1672 * @param out_length data length in @c usb_out_buffer
1673 * @param in_length data length to be read to @c usb_in_buffer
1674 */
1675 static int jlink_usb_io(struct jlink *jlink, int out_length, int in_length)
1676 {
1677 int result;
1678
1679 result = jlink_usb_write(jlink, out_length);
1680 if (result != out_length) {
1681 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
1682 out_length, result);
1683 return ERROR_JTAG_DEVICE_ERROR;
1684 }
1685
1686 result = jlink_usb_read(jlink, in_length);
1687 if (result != in_length) {
1688 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
1689 in_length, result);
1690 return ERROR_JTAG_DEVICE_ERROR;
1691 }
1692
1693 /*
1694 * Section 4.2.4 IN-transaction:
1695 * read dummy 0-byte packet if transaction size is
1696 * multiple of 64 bytes but not max. size of 0x8000
1697 */
1698 if ((in_length % 64) == 0 && in_length != 0x8000) {
1699 char dummy_buffer;
1700 result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
1701 &dummy_buffer, 1, JLINK_USB_TIMEOUT);
1702 if (result != 0) {
1703 LOG_ERROR("dummy byte read failed");
1704 return ERROR_JTAG_DEVICE_ERROR;
1705 }
1706 }
1707 return ERROR_OK;
1708 }
1709
1710 #ifdef _DEBUG_USB_COMMS_
1711 #define BYTES_PER_LINE 16
1712
1713 static void jlink_debug_buffer(uint8_t *buffer, int length)
1714 {
1715 char line[81];
1716 char s[4];
1717 int i;
1718 int j;
1719
1720 for (i = 0; i < length; i += BYTES_PER_LINE) {
1721 snprintf(line, 5, "%04x", i);
1722 for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
1723 snprintf(s, 4, " %02x", buffer[j]);
1724 strcat(line, s);
1725 }
1726 LOG_DEBUG("%s", line);
1727 }
1728 }
1729 #endif

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)