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 * Copyright (C) 2015 by Marc Schink *
12 * openocd-dev@marcschink.de *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 ***************************************************************************/
36 #include <jtag/interface.h>
38 #include <jtag/commands.h>
40 #include <libjaylink/libjaylink.h>
42 static struct jaylink_context *jayctx;
43 static struct jaylink_device_handle *devh;
44 static struct jaylink_connection conn;
45 static struct jaylink_connection connlist[JAYLINK_MAX_CONNECTIONS];
46 static enum jaylink_jtag_version jtag_command_version;
47 static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
49 static uint32_t serial_number;
50 static bool use_serial_number;
51 static uint8_t usb_address;
52 static bool use_usb_address;
53 static uint8_t iface = JAYLINK_TIF_JTAG;
54 static bool trace_enabled;
56 #define JLINK_MAX_SPEED 12000
57 #define JLINK_TAP_BUFFER_SIZE 2048
59 static unsigned int swd_buffer_size = JLINK_TAP_BUFFER_SIZE;
61 /* 256 byte non-volatile memory */
62 struct device_config {
64 /* 0ffset 0x01 to 0x03 */
65 uint8_t reserved_1[3];
66 uint32_t target_power;
67 /* 0ffset 0x08 to 0x1f */
68 uint8_t reserved_2[24];
69 /* IP only for J-Link Pro */
70 uint8_t ip_address[4];
71 uint8_t subnet_mask[4];
72 /* 0ffset 0x28 to 0x2f */
73 uint8_t reserved_3[8];
74 uint8_t mac_address[6];
75 /* 0ffset 0x36 to 0xff */
76 uint8_t reserved_4[202];
77 } __attribute__ ((packed));
79 static struct device_config config;
80 static struct device_config tmp_config;
82 /* Queue command functions */
83 static void jlink_end_state(tap_state_t state);
84 static void jlink_state_move(void);
85 static void jlink_path_move(int num_states, tap_state_t *path);
86 static void jlink_runtest(int num_cycles);
87 static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
88 int scan_size, struct scan_command *command);
89 static void jlink_reset(int trst, int srst);
90 static int jlink_swd_run_queue(void);
91 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk);
92 static int jlink_swd_switch_seq(enum swd_special_seq seq);
94 /* J-Link tap buffer functions */
95 static void jlink_tap_init(void);
96 static int jlink_tap_execute(void);
97 static void jlink_tap_ensure_space(int scans, int bits);
98 static void jlink_tap_append_step(int tms, int tdi);
99 static void jlink_tap_append_scan(int length, uint8_t *buffer,
100 struct scan_command *command);
102 static enum tap_state jlink_last_state = TAP_RESET;
103 static int queued_retval;
105 /***************************************************************************/
106 /* External interface implementation */
108 static void jlink_execute_runtest(struct jtag_command *cmd)
110 DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
111 cmd->cmd.runtest->end_state);
113 jlink_end_state(cmd->cmd.runtest->end_state);
114 jlink_runtest(cmd->cmd.runtest->num_cycles);
117 static void jlink_execute_statemove(struct jtag_command *cmd)
119 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
121 jlink_end_state(cmd->cmd.statemove->end_state);
125 static void jlink_execute_pathmove(struct jtag_command *cmd)
127 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
128 cmd->cmd.pathmove->num_states,
129 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
131 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
134 static void jlink_execute_scan(struct jtag_command *cmd)
140 DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd->cmd.scan->end_state));
142 jlink_end_state(cmd->cmd.scan->end_state);
144 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
145 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
147 type = jtag_scan_type(cmd->cmd.scan);
148 jlink_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
151 static void jlink_execute_reset(struct jtag_command *cmd)
153 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst,
154 cmd->cmd.reset->srst);
157 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
161 static void jlink_execute_sleep(struct jtag_command *cmd)
163 DEBUG_JTAG_IO("sleep %" PRIi32 "", cmd->cmd.sleep->us);
165 jtag_sleep(cmd->cmd.sleep->us);
168 static int jlink_execute_command(struct jtag_command *cmd)
172 jlink_execute_runtest(cmd);
175 jlink_execute_statemove(cmd);
178 jlink_execute_pathmove(cmd);
181 jlink_execute_scan(cmd);
184 jlink_execute_reset(cmd);
187 jlink_execute_sleep(cmd);
190 LOG_ERROR("BUG: Unknown JTAG command type encountered.");
191 return ERROR_JTAG_QUEUE_FAILED;
197 static int jlink_execute_queue(void)
200 struct jtag_command *cmd = jtag_command_queue;
202 while (cmd != NULL) {
203 ret = jlink_execute_command(cmd);
211 return jlink_tap_execute();
214 static int jlink_speed(int speed)
221 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
222 ret = jaylink_get_speeds(devh, &freq, &div);
224 if (ret != JAYLINK_OK) {
225 LOG_ERROR("jaylink_get_speeds() failed: %s.",
226 jaylink_strerror_name(ret));
227 return ERROR_JTAG_DEVICE_ERROR;
231 max_speed = freq / div;
233 max_speed = JLINK_MAX_SPEED;
237 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
238 LOG_ERROR("Adaptive clocking is not supported by the device.");
239 return ERROR_JTAG_NOT_IMPLEMENTED;
242 speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
243 } else if (speed > max_speed) {
244 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum).", speed,
249 ret = jaylink_set_speed(devh, speed);
251 if (ret != JAYLINK_OK) {
252 LOG_ERROR("jaylink_set_speed() failed: %s.",
253 jaylink_strerror_name(ret));
254 return ERROR_JTAG_DEVICE_ERROR;
260 static int jlink_speed_div(int speed, int *khz)
267 static int jlink_khz(int khz, int *jtag_speed)
274 static bool read_device_config(struct device_config *cfg)
278 ret = jaylink_read_raw_config(devh, (uint8_t *)cfg);
280 if (ret != JAYLINK_OK) {
281 LOG_ERROR("jaylink_read_raw_config() failed: %s.",
282 jaylink_strerror_name(ret));
286 if (cfg->usb_address == 0xff)
287 cfg->usb_address = 0x00;
289 if (cfg->target_power == 0xffffffff)
290 cfg->target_power = 0;
295 static int select_interface(void)
300 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
301 if (iface != JAYLINK_TIF_JTAG) {
302 LOG_ERROR("Device supports JTAG transport only.");
303 return ERROR_JTAG_INIT_FAILED;
309 ret = jaylink_get_available_interfaces(devh, &interfaces);
311 if (ret != JAYLINK_OK) {
312 LOG_ERROR("jaylink_get_available_interfaces() failed: %s.",
313 jaylink_strerror_name(ret));
314 return ERROR_JTAG_INIT_FAILED;
317 if (!(interfaces & (1 << iface))) {
318 LOG_ERROR("Selected transport is not supported by the device.");
319 return ERROR_JTAG_INIT_FAILED;
322 ret = jaylink_select_interface(devh, iface);
325 LOG_ERROR("jaylink_select_interface() failed: %s.",
326 jaylink_strerror_name(ret));
327 return ERROR_JTAG_INIT_FAILED;
333 static int jlink_register(void)
339 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
342 ret = jaylink_register(devh, &conn, connlist, NULL, NULL);
345 LOG_ERROR("jaylink_register() failed: %s.",
346 jaylink_strerror_name(ret));
350 handle_found = false;
352 for (i = 0; i < ret; i++) {
353 if (connlist[i].handle == conn.handle) {
360 LOG_ERROR("Registration failed: maximum number of connections on the "
369 * Adjust the SWD transaction buffer size depending on the free device internal
370 * memory. This ensures that the SWD transactions sent to the device do not
371 * exceed the internal memory of the device.
373 static bool adjust_swd_buffer_size(void)
378 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
381 ret = jaylink_get_free_memory(devh, &tmp);
383 if (ret != JAYLINK_OK) {
384 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
385 jaylink_strerror_name(ret));
390 LOG_ERROR("Not enough free device internal memory: %u bytes.", tmp);
394 tmp = MIN(JLINK_TAP_BUFFER_SIZE, (tmp - 16) / 2);
396 if (tmp != swd_buffer_size) {
397 swd_buffer_size = tmp;
398 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes.",
405 static int jlink_init(void)
408 struct jaylink_device **devs;
412 char *firmware_version;
413 struct jaylink_hardware_version hwver;
414 struct jaylink_hardware_status hwstatus;
416 ret = jaylink_init(&jayctx);
418 if (ret != JAYLINK_OK) {
419 LOG_ERROR("jaylink_init() failed: %s.",
420 jaylink_strerror_name(ret));
421 return ERROR_JTAG_INIT_FAILED;
424 ret = jaylink_get_device_list(jayctx, &devs);
427 LOG_ERROR("jaylink_get_device_list() failed: %s.",
428 jaylink_strerror_name(ret));
429 jaylink_exit(jayctx);
430 return ERROR_JTAG_INIT_FAILED;
433 found_device = false;
435 if (!use_serial_number && !use_usb_address)
436 LOG_INFO("No device selected, using first device.");
438 for (i = 0; devs[i]; i++) {
439 jaylink_device_get_serial_number(devs[i], &tmp);
440 ret = jaylink_device_get_usb_address(devs[i]);
442 if (use_usb_address && usb_address != ret)
445 if (use_serial_number && tmp != serial_number)
448 ret = jaylink_open(devs[i], &devh);
450 if (ret != JAYLINK_OK) {
451 LOG_ERROR("Failed to open device: %s.", jaylink_strerror_name(ret));
459 jaylink_free_device_list(devs, 1);
462 LOG_ERROR("No J-Link device found.");
463 jaylink_exit(jayctx);
464 return ERROR_JTAG_INIT_FAILED;
468 * Be careful with changing the following initialization sequence because
469 * some devices are known to be sensitive regarding the order.
472 ret = jaylink_get_firmware_version(devh, &firmware_version);
475 LOG_INFO("%s", firmware_version);
476 free(firmware_version);
478 LOG_WARNING("Device responds empty firmware version string.");
480 LOG_ERROR("jaylink_get_firmware_version() failed: %s.",
481 jaylink_strerror_name(ret));
483 jaylink_exit(jayctx);
484 return ERROR_JTAG_INIT_FAILED;
487 memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
488 ret = jaylink_get_caps(devh, caps);
490 if (ret != JAYLINK_OK) {
491 LOG_ERROR("jaylink_get_caps() failed: %s.", jaylink_strerror_name(ret));
493 jaylink_exit(jayctx);
494 return ERROR_JTAG_INIT_FAILED;
497 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
498 ret = jaylink_get_extended_caps(devh, caps);
500 if (ret != JAYLINK_OK) {
501 LOG_ERROR("jaylink_get_extended_caps() failed: %s.",
502 jaylink_strerror_name(ret));
504 jaylink_exit(jayctx);
505 return ERROR_JTAG_INIT_FAILED;
509 jtag_command_version = JAYLINK_JTAG_V2;
511 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
512 ret = jaylink_get_hardware_version(devh, &hwver);
514 if (ret != JAYLINK_OK) {
515 LOG_ERROR("Failed to retrieve hardware version: %s.",
516 jaylink_strerror_name(ret));
518 jaylink_exit(jayctx);
519 return ERROR_JTAG_INIT_FAILED;
522 LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
524 if (hwver.major >= 5)
525 jtag_command_version = JAYLINK_JTAG_V3;
528 if (iface == JAYLINK_TIF_SWD) {
530 * Adjust the SWD transaction buffer size in case there is already
531 * allocated memory on the device. This happens for example if the
532 * memory for SWO capturing is still allocated because the software
533 * which used the device before has not been shut down properly.
535 if (!adjust_swd_buffer_size()) {
537 jaylink_exit(jayctx);
538 return ERROR_JTAG_INIT_FAILED;
542 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
543 if (!read_device_config(&config)) {
544 LOG_ERROR("Failed to read device configuration data.");
546 jaylink_exit(jayctx);
547 return ERROR_JTAG_INIT_FAILED;
550 memcpy(&tmp_config, &config, sizeof(struct device_config));
553 ret = jaylink_get_hardware_status(devh, &hwstatus);
555 if (ret != JAYLINK_OK) {
556 LOG_ERROR("jaylink_get_hardware_status() failed: %s.",
557 jaylink_strerror_name(ret));
559 jaylink_exit(jayctx);
560 return ERROR_JTAG_INIT_FAILED;
563 LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
564 hwstatus.target_voltage % 1000);
572 ret = jlink_register();
574 if (ret != ERROR_OK) {
576 jaylink_exit(jayctx);
577 return ERROR_JTAG_INIT_FAILED;
580 ret = select_interface();
582 if (ret != ERROR_OK) {
584 jaylink_exit(jayctx);
592 jlink_speed(jtag_get_speed_khz());
594 if (iface == JAYLINK_TIF_JTAG) {
596 * J-Link devices with firmware version v5 and v6 seems to have an issue
597 * if the first tap move is not divisible by 8, so we send a TLR on
600 for (i = 0; i < 8; i++)
601 jlink_tap_append_step(1, 0);
609 static int jlink_quit(void)
614 ret = jaylink_swo_stop(devh);
616 if (ret != JAYLINK_OK)
617 LOG_ERROR("jaylink_swo_stop() failed: %s.",
618 jaylink_strerror_name(ret));
621 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
622 ret = jaylink_unregister(devh, &conn, connlist, NULL, NULL);
625 LOG_ERROR("jaylink_unregister() failed: %s.",
626 jaylink_strerror_name(ret));
630 jaylink_exit(jayctx);
635 /***************************************************************************/
636 /* Queue command implementations */
638 static void jlink_end_state(tap_state_t state)
640 if (tap_is_state_stable(state))
641 tap_set_end_state(state);
643 LOG_ERROR("BUG: %i is not a valid end state", state);
648 /* Goes to the end state. */
649 static void jlink_state_move(void)
654 uint8_t tms_scan_bits;
656 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
657 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
659 for (i = 0; i < tms_scan_bits; i++) {
660 tms = (tms_scan >> i) & 1;
661 jlink_tap_append_step(tms, 0);
664 tap_set_state(tap_get_end_state());
667 static void jlink_path_move(int num_states, tap_state_t *path)
671 for (i = 0; i < num_states; i++) {
672 if (path[i] == tap_state_transition(tap_get_state(), false))
673 jlink_tap_append_step(0, 0);
674 else if (path[i] == tap_state_transition(tap_get_state(), true))
675 jlink_tap_append_step(1, 0);
677 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
678 tap_state_name(tap_get_state()), tap_state_name(path[i]));
682 tap_set_state(path[i]);
685 tap_set_end_state(tap_get_state());
688 static void jlink_runtest(int num_cycles)
692 tap_state_t saved_end_state = tap_get_end_state();
694 jlink_tap_ensure_space(1, num_cycles + 16);
696 /* Only do a state_move when we're not already in IDLE. */
697 if (tap_get_state() != TAP_IDLE) {
698 jlink_end_state(TAP_IDLE);
703 /* Execute num_cycles. */
704 for (i = 0; i < num_cycles; i++)
705 jlink_tap_append_step(0, 0);
707 /* Finish in end_state. */
708 jlink_end_state(saved_end_state);
710 if (tap_get_state() != tap_get_end_state())
714 static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
715 int scan_size, struct scan_command *command)
717 tap_state_t saved_end_state;
719 jlink_tap_ensure_space(1, scan_size + 16);
721 saved_end_state = tap_get_end_state();
723 /* Move to appropriate scan state. */
724 jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
726 /* Only move if we're not already there. */
727 if (tap_get_state() != tap_get_end_state())
730 jlink_end_state(saved_end_state);
733 jlink_tap_append_scan(scan_size, buffer, command);
735 /* We are in Exit1, go to Pause. */
736 jlink_tap_append_step(0, 0);
738 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
740 if (tap_get_state() != tap_get_end_state())
744 static void jlink_reset(int trst, int srst)
746 LOG_DEBUG("TRST: %i, SRST: %i.", trst, srst);
748 /* Signals are active low. */
750 jaylink_set_reset(devh);
753 jaylink_clear_reset(devh);
756 jaylink_jtag_clear_trst(devh);
759 jaylink_jtag_set_trst(devh);
762 COMMAND_HANDLER(jlink_usb_command)
765 command_print(CMD_CTX, "Need exactly one argument for jlink usb.");
766 return ERROR_COMMAND_SYNTAX_ERROR;
769 if (sscanf(CMD_ARGV[0], "%" SCNd8, &usb_address) != 1) {
770 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
774 if (usb_address > JAYLINK_USB_ADDRESS_3) {
775 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
779 use_serial_number = false;
780 use_usb_address = true;
785 COMMAND_HANDLER(jlink_serial_command)
788 command_print(CMD_CTX, "Need exactly one argument for jlink serial.");
789 return ERROR_COMMAND_SYNTAX_ERROR;
792 if (sscanf(CMD_ARGV[0], "%" SCNd32, &serial_number) != 1) {
793 command_print(CMD_CTX, "Invalid serial number: %s.", CMD_ARGV[0]);
797 use_serial_number = true;
798 use_usb_address = false;
803 COMMAND_HANDLER(jlink_handle_hwstatus_command)
806 struct jaylink_hardware_status status;
808 ret = jaylink_get_hardware_status(devh, &status);
810 if (ret != JAYLINK_OK) {
811 command_print(CMD_CTX, "jaylink_get_hardware_status() failed: %s.",
812 jaylink_strerror_name(ret));
816 command_print(CMD_CTX, "VTarget = %u.%03u V",
817 status.target_voltage / 1000, status.target_voltage % 1000);
819 command_print(CMD_CTX, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
820 "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
821 status.tres, status.trst);
823 if (status.target_voltage < 1500)
824 command_print(CMD_CTX, "Target voltage too low. Check target power.");
829 COMMAND_HANDLER(jlink_handle_free_memory_command)
834 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
835 command_print(CMD_CTX, "Retrieval of free memory is not supported by "
840 ret = jaylink_get_free_memory(devh, &tmp);
842 if (ret != JAYLINK_OK) {
843 command_print(CMD_CTX, "jaylink_get_free_memory() failed: %s.",
844 jaylink_strerror_name(ret));
848 command_print(CMD_CTX, "Device has %u bytes of free memory.", tmp);
853 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
859 switch (jtag_command_version) {
860 case JAYLINK_JTAG_V2:
863 case JAYLINK_JTAG_V3:
870 command_print(CMD_CTX, "JTAG command version: %i", version);
871 } else if (CMD_ARGC == 1) {
872 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
873 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
874 return ERROR_COMMAND_SYNTAX_ERROR;
879 jtag_command_version = JAYLINK_JTAG_V2;
882 jtag_command_version = JAYLINK_JTAG_V3;
885 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
886 return ERROR_COMMAND_SYNTAX_ERROR;
889 command_print(CMD_CTX, "Need exactly one argument for jlink jtag.");
890 return ERROR_COMMAND_SYNTAX_ERROR;
896 COMMAND_HANDLER(jlink_handle_target_power_command)
902 command_print(CMD_CTX, "Need exactly one argument for jlink "
904 return ERROR_COMMAND_SYNTAX_ERROR;
907 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
908 command_print(CMD_CTX, "Target power supply is not supported by the "
913 if (!strcmp(CMD_ARGV[0], "on")) {
915 } else if (!strcmp(CMD_ARGV[0], "off")) {
918 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
922 ret = jaylink_set_target_power(devh, enable);
924 if (ret != JAYLINK_OK) {
925 command_print(CMD_CTX, "jaylink_set_target_power() failed: %s.",
926 jaylink_strerror_name(ret));
933 static void show_config_usb_address(struct command_context *ctx)
935 if (config.usb_address != tmp_config.usb_address)
936 command_print(ctx, "USB address: %u [%u]", config.usb_address,
937 tmp_config.usb_address);
939 command_print(ctx, "USB address: %u", config.usb_address);
942 static void show_config_ip_address(struct command_context *ctx)
944 if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
945 command_print(ctx, "IP address: %d.%d.%d.%d",
946 config.ip_address[3], config.ip_address[2],
947 config.ip_address[1], config.ip_address[0]);
949 command_print(ctx, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
950 config.ip_address[3], config.ip_address[2],
951 config.ip_address[1], config.ip_address[0],
952 tmp_config.ip_address[3], tmp_config.ip_address[2],
953 tmp_config.ip_address[1], tmp_config.ip_address[0]);
955 if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
956 command_print(ctx, "Subnet mask: %d.%d.%d.%d",
957 config.subnet_mask[3], config.subnet_mask[2],
958 config.subnet_mask[1], config.subnet_mask[0]);
960 command_print(ctx, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
961 config.subnet_mask[3], config.subnet_mask[2],
962 config.subnet_mask[1], config.subnet_mask[0],
963 tmp_config.subnet_mask[3], tmp_config.subnet_mask[2],
964 tmp_config.subnet_mask[1], tmp_config.subnet_mask[0]);
967 static void show_config_mac_address(struct command_context *ctx)
969 if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
970 command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
971 config.mac_address[5], config.mac_address[4],
972 config.mac_address[3], config.mac_address[2],
973 config.mac_address[1], config.mac_address[0]);
975 command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
976 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
977 config.mac_address[5], config.mac_address[4],
978 config.mac_address[3], config.mac_address[2],
979 config.mac_address[1], config.mac_address[0],
980 tmp_config.mac_address[5], tmp_config.mac_address[4],
981 tmp_config.mac_address[3], tmp_config.mac_address[2],
982 tmp_config.mac_address[1], tmp_config.mac_address[0]);
985 static void show_config_target_power(struct command_context *ctx)
987 const char *target_power;
988 const char *current_target_power;
990 if (!config.target_power)
991 target_power = "off";
995 if (!tmp_config.target_power)
996 current_target_power = "off";
998 current_target_power = "on";
1000 if (config.target_power != tmp_config.target_power)
1001 command_print(ctx, "Target power supply: %s [%s]", target_power,
1002 current_target_power);
1004 command_print(ctx, "Target power supply: %s", target_power);
1007 static void show_config(struct command_context *ctx)
1009 command_print(ctx, "J-Link device configuration:");
1011 show_config_usb_address(ctx);
1013 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1014 show_config_target_power(ctx);
1016 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1017 show_config_ip_address(ctx);
1018 show_config_mac_address(ctx);
1022 static int poll_trace(uint8_t *buf, size_t *size)
1029 ret = jaylink_swo_read(devh, buf, &length);
1031 if (ret != JAYLINK_OK) {
1032 LOG_ERROR("jaylink_swo_read() failed: %s.", jaylink_strerror_name(ret));
1041 static uint32_t calculate_trace_buffer_size(void)
1046 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1049 ret = jaylink_get_free_memory(devh, &tmp);
1051 if (ret != JAYLINK_OK) {
1052 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
1053 jaylink_strerror_name(ret));
1057 if (tmp > 0x3fff || tmp <= 0x600)
1062 return tmp & 0xffffff00;
1065 static bool check_trace_freq(uint32_t freq, uint32_t div, uint32_t trace_freq)
1070 min = fabs(1.0 - (freq / ((double)trace_freq * div)));
1072 while (freq / div > 0) {
1073 deviation = fabs(1.0 - (freq / ((double)trace_freq * div)));
1075 if (deviation < 0.03) {
1076 LOG_DEBUG("Found suitable frequency divider %u with deviation of "
1077 "%.02f %%.", div, deviation);
1081 if (deviation < min)
1087 LOG_ERROR("Selected trace frequency is not supported by the device. "
1088 "Please choose a different trace frequency.");
1089 LOG_ERROR("Maximum permitted deviation is 3.00 %%, but only %.02f %% "
1090 "could be achieved.", min * 100);
1095 static int config_trace(bool enabled, enum tpio_pin_protocol pin_protocol,
1096 uint32_t port_size, unsigned int *trace_freq)
1099 uint32_t buffer_size;
1103 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1104 LOG_ERROR("Trace capturing is not supported by the device.");
1108 if (pin_protocol != ASYNC_UART) {
1109 LOG_ERROR("Selected pin protocol is not supported.");
1113 trace_enabled = enabled;
1115 ret = jaylink_swo_stop(devh);
1117 if (ret != JAYLINK_OK) {
1118 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror_name(ret));
1124 * Adjust the SWD transaction buffer size as stopping SWO capturing
1125 * deallocates device internal memory.
1127 if (!adjust_swd_buffer_size())
1133 buffer_size = calculate_trace_buffer_size();
1136 LOG_ERROR("Not enough free device memory to start trace capturing.");
1140 ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &freq, &div);
1142 if (ret != JAYLINK_OK) {
1143 LOG_ERROR("jaylink_swo_get_speeds() failed: %s.",
1144 jaylink_strerror_name(ret));
1149 *trace_freq = freq / div;
1151 if (!check_trace_freq(freq, div, *trace_freq))
1154 LOG_DEBUG("Using %u bytes device memory for trace capturing.", buffer_size);
1156 ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1159 if (ret != JAYLINK_OK) {
1160 LOG_ERROR("jaylink_start_swo() failed: %s.",
1161 jaylink_strerror_name(ret));
1166 * Adjust the SWD transaction buffer size as starting SWO capturing
1167 * allocates device internal memory.
1169 if (!adjust_swd_buffer_size())
1175 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1179 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1180 command_print(CMD_CTX, "Reading configuration is not supported by the "
1186 show_config_usb_address(CMD_CTX);
1187 } else if (CMD_ARGC == 1) {
1188 if (sscanf(CMD_ARGV[0], "%" SCNd8, &tmp) != 1) {
1189 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
1193 if (tmp > JAYLINK_USB_ADDRESS_3) {
1194 command_print(CMD_CTX, "Invalid USB address: %u.", tmp);
1198 tmp_config.usb_address = tmp;
1200 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1202 return ERROR_COMMAND_SYNTAX_ERROR;
1208 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1212 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1213 command_print(CMD_CTX, "Reading configuration is not supported by the "
1218 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1219 command_print(CMD_CTX, "Target power supply is not supported by the "
1225 show_config_target_power(CMD_CTX);
1226 } else if (CMD_ARGC == 1) {
1227 if (!strcmp(CMD_ARGV[0], "on")) {
1229 } else if (!strcmp(CMD_ARGV[0], "off")) {
1232 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1236 tmp_config.target_power = enable;
1238 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1240 return ERROR_COMMAND_SYNTAX_ERROR;
1246 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1253 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1254 command_print(CMD_CTX, "Reading configuration is not supported by the "
1259 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1260 command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
1266 show_config_mac_address(CMD_CTX);
1267 } else if (CMD_ARGC == 1) {
1270 if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || \
1271 str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1272 command_print(CMD_CTX, "Invalid MAC address format.");
1273 return ERROR_COMMAND_SYNTAX_ERROR;
1276 for (i = 5; i >= 0; i--) {
1277 addr[i] = strtoul(str, &e, 16);
1281 if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1282 command_print(CMD_CTX, "Invalid MAC address: zero address.");
1283 return ERROR_COMMAND_SYNTAX_ERROR;
1286 if (!(0x01 & addr[0])) {
1287 command_print(CMD_CTX, "Invalid MAC address: multicast address.");
1288 return ERROR_COMMAND_SYNTAX_ERROR;
1291 memcpy(tmp_config.mac_address, addr, sizeof(addr));
1293 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1295 return ERROR_COMMAND_SYNTAX_ERROR;
1301 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1305 const char *s_save = s;
1311 for (i = 0; i < 4; i++) {
1312 lip[i] = strtoul(s, &e, 10);
1314 if (*e != '.' && i != 3)
1321 memcpy(ip, lip, sizeof(lip));
1326 static void cpy_ip(uint8_t *dst, uint8_t *src)
1330 for (i = 0, j = 3; i < 4; i++, j--)
1334 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1336 uint8_t ip_address[4];
1337 uint32_t subnet_mask = 0;
1339 uint8_t subnet_bits = 24;
1341 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1342 command_print(CMD_CTX, "Reading configuration is not supported by the "
1347 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1348 command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
1354 show_config_ip_address(CMD_CTX);
1356 if (!string_to_ip(CMD_ARGV[0], ip_address, &i))
1357 return ERROR_COMMAND_SYNTAX_ERROR;
1359 len = strlen(CMD_ARGV[0]);
1361 /* Check for format A.B.C.D/E. */
1363 if (CMD_ARGV[0][i] != '/')
1364 return ERROR_COMMAND_SYNTAX_ERROR;
1366 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1367 } else if (CMD_ARGC > 1) {
1368 if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i))
1369 return ERROR_COMMAND_SYNTAX_ERROR;
1373 subnet_mask = (uint32_t)(subnet_bits < 32 ?
1374 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1376 cpy_ip(tmp_config.ip_address, ip_address);
1377 cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1383 COMMAND_HANDLER(jlink_handle_config_reset_command)
1385 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1388 memcpy(&tmp_config, &config, sizeof(struct device_config));
1394 COMMAND_HANDLER(jlink_handle_config_write_command)
1398 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1399 command_print(CMD_CTX, "Reading configuration is not supported by the "
1404 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1405 command_print(CMD_CTX, "Writing configuration is not supported by the "
1410 if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1411 command_print(CMD_CTX, "Operation not performed due to no changes in "
1412 "the configuration.");
1416 ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1418 if (ret != JAYLINK_OK) {
1419 LOG_ERROR("jaylink_write_raw_config() failed: %s.",
1420 jaylink_strerror_name(ret));
1424 if (!read_device_config(&config)) {
1425 LOG_ERROR("Failed to read device configuration for verification.");
1429 if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1430 LOG_ERROR("Verification of device configuration failed. Please check "
1435 memcpy(&tmp_config, &config, sizeof(struct device_config));
1436 command_print(CMD_CTX, "The new device configuration applies after power "
1437 "cycling the J-Link device.");
1442 COMMAND_HANDLER(jlink_handle_config_command)
1444 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1445 command_print(CMD_CTX, "Device doesn't support reading configuration.");
1450 show_config(CMD_CTX);
1455 static const struct command_registration jlink_config_subcommand_handlers[] = {
1458 .handler = &jlink_handle_config_usb_address_command,
1459 .mode = COMMAND_EXEC,
1460 .help = "set the USB address",
1464 .name = "targetpower",
1465 .handler = &jlink_handle_config_target_power_command,
1466 .mode = COMMAND_EXEC,
1467 .help = "set the target power supply",
1472 .handler = &jlink_handle_config_mac_address_command,
1473 .mode = COMMAND_EXEC,
1474 .help = "set the MAC Address",
1475 .usage = "[ff:ff:ff:ff:ff:ff]",
1479 .handler = &jlink_handle_config_ip_address_command,
1480 .mode = COMMAND_EXEC,
1481 .help = "set the IP address, where A.B.C.D is the IP address, "
1482 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1483 .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1487 .handler = &jlink_handle_config_reset_command,
1488 .mode = COMMAND_EXEC,
1489 .help = "undo configuration changes"
1493 .handler = &jlink_handle_config_write_command,
1494 .mode = COMMAND_EXEC,
1495 .help = "write configuration to the device"
1497 COMMAND_REGISTRATION_DONE
1500 static const struct command_registration jlink_subcommand_handlers[] = {
1503 .handler = &jlink_handle_jlink_jtag_command,
1504 .mode = COMMAND_EXEC,
1505 .help = "select the JTAG command version",
1509 .name = "targetpower",
1510 .handler = &jlink_handle_target_power_command,
1511 .mode = COMMAND_EXEC,
1512 .help = "set the target power supply",
1517 .handler = &jlink_handle_free_memory_command,
1518 .mode = COMMAND_EXEC,
1519 .help = "show free device memory"
1523 .handler = &jlink_handle_hwstatus_command,
1524 .mode = COMMAND_EXEC,
1525 .help = "show the hardware status"
1529 .handler = &jlink_usb_command,
1530 .mode = COMMAND_CONFIG,
1531 .help = "set the USB address of the device that should be used",
1536 .handler = &jlink_serial_command,
1537 .mode = COMMAND_CONFIG,
1538 .help = "set the serial number of the device that should be used",
1539 .usage = "<serial number>"
1543 .handler = &jlink_handle_config_command,
1544 .mode = COMMAND_EXEC,
1545 .help = "access the device configuration. If no argument is given "
1546 "this will show the device configuration",
1547 .chain = jlink_config_subcommand_handlers,
1549 COMMAND_REGISTRATION_DONE
1552 static const struct command_registration jlink_command_handlers[] = {
1555 .mode = COMMAND_ANY,
1556 .help = "perform jlink management",
1557 .chain = jlink_subcommand_handlers,
1559 COMMAND_REGISTRATION_DONE
1562 static int jlink_swd_init(void)
1564 iface = JAYLINK_TIF_SWD;
1569 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1571 assert(!(cmd & SWD_CMD_RnW));
1572 jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1575 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1577 assert(cmd & SWD_CMD_RnW);
1578 jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1581 static int_least32_t jlink_swd_frequency(int_least32_t hz)
1584 jlink_speed(hz / 1000);
1589 /***************************************************************************/
1590 /* J-Link tap functions */
1592 static unsigned tap_length;
1593 /* In SWD mode use tms buffer for direction control */
1594 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
1595 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
1596 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
1598 struct pending_scan_result {
1599 int first; /* First bit position in tdo_buffer to read. */
1600 int length; /* Number of bits to read. */
1601 struct scan_command *command; /* Corresponding scan command. */
1605 #define MAX_PENDING_SCAN_RESULTS 256
1607 static int pending_scan_results_length;
1608 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1610 static void jlink_tap_init(void)
1613 pending_scan_results_length = 0;
1616 static void jlink_tap_ensure_space(int scans, int bits)
1618 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
1619 int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
1621 if (scans > available_scans || bits > available_bits)
1622 jlink_tap_execute();
1625 static void jlink_tap_append_step(int tms, int tdi)
1627 int index_var = tap_length / 8;
1629 assert(index_var < JLINK_TAP_BUFFER_SIZE);
1631 int bit_index = tap_length % 8;
1632 uint8_t bit = 1 << bit_index;
1634 /* We do not pad TMS, so be sure to initialize all bits. */
1636 tms_buffer[index_var] = tdi_buffer[index_var] = 0;
1639 tms_buffer[index_var] |= bit;
1641 tms_buffer[index_var] &= ~bit;
1644 tdi_buffer[index_var] |= bit;
1646 tdi_buffer[index_var] &= ~bit;
1651 static void jlink_tap_append_scan(int length, uint8_t *buffer,
1652 struct scan_command *command)
1654 struct pending_scan_result *pending_scan_result =
1655 &pending_scan_results_buffer[pending_scan_results_length];
1658 pending_scan_result->first = tap_length;
1659 pending_scan_result->length = length;
1660 pending_scan_result->command = command;
1661 pending_scan_result->buffer = buffer;
1663 for (i = 0; i < length; i++) {
1664 int tms = (i < (length - 1)) ? 0 : 1;
1665 int tdi = (buffer[i / 8] & (1 << (i % 8))) != 0;
1666 jlink_tap_append_step(tms, tdi);
1669 pending_scan_results_length++;
1673 * Pad and send a tap sequence to the device, and receive the answer. For the
1674 * purpose of padding we assume that we are in idle or pause state.
1676 static int jlink_tap_execute(void)
1684 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
1685 tap_length, jlink_last_state);
1687 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
1688 tap_length, jlink_last_state);
1690 ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
1691 tap_length, jtag_command_version);
1693 if (ret != JAYLINK_OK) {
1694 LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror_name(ret));
1696 return ERROR_JTAG_QUEUE_FAILED;
1699 for (i = 0; i < pending_scan_results_length; i++) {
1700 struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
1701 uint8_t *buffer = pending_scan_result->buffer;
1702 int length = pending_scan_result->length;
1703 int first = pending_scan_result->first;
1704 struct scan_command *command = pending_scan_result->command;
1706 /* Copy to buffer. */
1707 buf_set_buf(tdo_buffer, first, buffer, 0, length);
1709 DEBUG_JTAG_IO("Pending scan result, length = %d.", length);
1711 if (jtag_read_buffer(buffer, command) != ERROR_OK) {
1713 return ERROR_JTAG_QUEUE_FAILED;
1716 if (pending_scan_result->buffer != NULL)
1717 free(pending_scan_result->buffer);
1725 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
1727 unsigned int tap_pos = tap_length;
1730 buf_set_u32(buf, tap_pos, 32, val);
1736 buf_set_u32(buf, tap_pos, len, val);
1739 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
1741 const uint32_t dir_out = 0xffffffff;
1744 bit_copy(tdi_buffer, tap_length, data, 0, len);
1746 fill_buffer(tdi_buffer, 0, len);
1748 fill_buffer(tms_buffer, dir_out, len);
1752 static void jlink_queue_data_in(uint32_t len)
1754 const uint32_t dir_in = 0;
1756 fill_buffer(tms_buffer, dir_in, len);
1760 static int jlink_swd_switch_seq(enum swd_special_seq seq)
1767 LOG_DEBUG("SWD line reset");
1768 s = swd_seq_line_reset;
1769 s_len = swd_seq_line_reset_len;
1772 LOG_DEBUG("JTAG-to-SWD");
1773 s = swd_seq_jtag_to_swd;
1774 s_len = swd_seq_jtag_to_swd_len;
1777 LOG_DEBUG("SWD-to-JTAG");
1778 s = swd_seq_swd_to_jtag;
1779 s_len = swd_seq_swd_to_jtag_len;
1782 LOG_ERROR("Sequence %d not supported.", seq);
1786 jlink_queue_data_out(s, s_len);
1791 static int jlink_swd_run_queue(void)
1796 LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length);
1798 if (queued_retval != ERROR_OK) {
1799 LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval);
1804 * A transaction must be followed by another transaction or at least 8 idle
1805 * cycles to ensure that data is clocked through the AP.
1807 jlink_queue_data_out(NULL, 8);
1809 ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
1811 if (ret != JAYLINK_OK) {
1812 LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror_name(ret));
1816 for (i = 0; i < pending_scan_results_length; i++) {
1817 int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
1819 if (ack != SWD_ACK_OK) {
1820 LOG_DEBUG("SWD ack not OK: %d %s", ack,
1821 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
1822 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
1824 } else if (pending_scan_results_buffer[i].length) {
1825 uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
1826 int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
1828 if (parity != parity_u32(data)) {
1829 LOG_ERROR("SWD: Read data parity mismatch.");
1830 queued_retval = ERROR_FAIL;
1834 if (pending_scan_results_buffer[i].buffer)
1835 *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
1841 ret = queued_retval;
1842 queued_retval = ERROR_OK;
1847 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
1849 uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
1850 if (tap_length + 46 + 8 + ap_delay_clk >= sizeof(tdi_buffer) * 8 ||
1851 pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
1852 /* Not enough room in the queue. Run the queue. */
1853 queued_retval = jlink_swd_run_queue();
1856 if (queued_retval != ERROR_OK)
1859 cmd |= SWD_CMD_START | SWD_CMD_PARK;
1861 jlink_queue_data_out(&cmd, 8);
1863 pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
1865 if (cmd & SWD_CMD_RnW) {
1866 /* Queue a read transaction. */
1867 pending_scan_results_buffer[pending_scan_results_length].length = 32;
1868 pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
1870 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
1872 /* Queue a write transaction. */
1873 pending_scan_results_buffer[pending_scan_results_length].length = 0;
1874 jlink_queue_data_in(1 + 3 + 1);
1876 buf_set_u32(data_parity_trn, 0, 32, data);
1877 buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
1879 jlink_queue_data_out(data_parity_trn, 32 + 1);
1882 pending_scan_results_length++;
1884 /* Insert idle cycles after AP accesses to avoid WAIT. */
1885 if (cmd & SWD_CMD_APnDP)
1886 jlink_queue_data_out(NULL, ap_delay_clk);
1889 static const struct swd_driver jlink_swd = {
1890 .init = &jlink_swd_init,
1891 .frequency = &jlink_swd_frequency,
1892 .switch_seq = &jlink_swd_switch_seq,
1893 .read_reg = &jlink_swd_read_reg,
1894 .write_reg = &jlink_swd_write_reg,
1895 .run = &jlink_swd_run_queue,
1898 static const char * const jlink_transports[] = { "jtag", "swd", NULL };
1900 struct jtag_interface jlink_interface = {
1902 .commands = jlink_command_handlers,
1903 .transports = jlink_transports,
1905 .execute_queue = &jlink_execute_queue,
1906 .speed = &jlink_speed,
1907 .speed_div = &jlink_speed_div,
1909 .init = &jlink_init,
1910 .quit = &jlink_quit,
1911 .config_trace = &config_trace,
1912 .poll_trace = &poll_trace,