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 * Copyright (C) 2015 by Paul Fertser *
15 * fercerpav@gmail.com *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
29 ***************************************************************************/
38 #include <jtag/interface.h>
40 #include <jtag/commands.h>
41 #include <jtag/adapter.h>
42 #include <helper/replacements.h>
43 #include <target/cortex_m.h>
45 #include <libjaylink/libjaylink.h>
47 static struct jaylink_context *jayctx;
48 static struct jaylink_device_handle *devh;
49 static struct jaylink_connection conn;
50 static struct jaylink_connection connlist[JAYLINK_MAX_CONNECTIONS];
51 static enum jaylink_jtag_version jtag_command_version;
52 static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
54 static uint32_t serial_number;
55 static bool use_serial_number;
56 static bool use_usb_location;
57 static enum jaylink_usb_address usb_address;
58 static bool use_usb_address;
59 static enum jaylink_target_interface iface = JAYLINK_TIF_JTAG;
60 static bool trace_enabled;
62 #define JLINK_MAX_SPEED 12000
63 #define JLINK_TAP_BUFFER_SIZE 2048
65 static unsigned int swd_buffer_size = JLINK_TAP_BUFFER_SIZE;
67 /* Maximum SWO frequency deviation. */
68 #define SWO_MAX_FREQ_DEV 0.03
70 /* 256 byte non-volatile memory */
71 struct device_config {
73 /* 0ffset 0x01 to 0x03 */
74 uint8_t reserved_1[3];
75 uint32_t target_power;
76 /* 0ffset 0x08 to 0x1f */
77 uint8_t reserved_2[24];
78 /* IP only for J-Link Pro */
79 uint8_t ip_address[4];
80 uint8_t subnet_mask[4];
81 /* 0ffset 0x28 to 0x2f */
82 uint8_t reserved_3[8];
83 uint8_t mac_address[6];
84 /* 0ffset 0x36 to 0xff */
85 uint8_t reserved_4[202];
86 } __attribute__ ((packed));
88 static struct device_config config;
89 static struct device_config tmp_config;
91 /* Queue command functions */
92 static void jlink_end_state(tap_state_t state);
93 static void jlink_state_move(void);
94 static void jlink_path_move(int num_states, tap_state_t *path);
95 static void jlink_stableclocks(int num_cycles);
96 static void jlink_runtest(int num_cycles);
97 static void jlink_reset(int trst, int srst);
98 static int jlink_reset_safe(int trst, int srst);
99 static int jlink_swd_run_queue(void);
100 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk);
101 static int jlink_swd_switch_seq(enum swd_special_seq seq);
103 /* J-Link tap buffer functions */
104 static void jlink_tap_init(void);
105 static int jlink_flush(void);
107 * Queue data to go out and in, flushing the queue as many times as
110 * @param out A pointer to TDI data, if NULL, old stale data will be used.
111 * @param out_offset A bit offset for TDI data.
112 * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
113 * @param tms_offset A bit offset for TMS data.
114 * @param in A pointer to store TDO data to, if NULL the data will be discarded.
115 * @param in_offset A bit offset for TDO data.
116 * @param length Amount of bits to transfer out and in.
118 * @retval This function doesn't return any value.
120 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
121 const uint8_t *tms_out, unsigned tms_offset,
122 uint8_t *in, unsigned in_offset,
125 static enum tap_state jlink_last_state = TAP_RESET;
126 static int queued_retval;
128 /***************************************************************************/
129 /* External interface implementation */
131 static void jlink_execute_stableclocks(struct jtag_command *cmd)
133 LOG_DEBUG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
134 jlink_stableclocks(cmd->cmd.runtest->num_cycles);
137 static void jlink_execute_runtest(struct jtag_command *cmd)
139 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
140 cmd->cmd.runtest->end_state);
142 jlink_end_state(cmd->cmd.runtest->end_state);
143 jlink_runtest(cmd->cmd.runtest->num_cycles);
146 static void jlink_execute_statemove(struct jtag_command *cmd)
148 LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
150 jlink_end_state(cmd->cmd.statemove->end_state);
154 static void jlink_execute_pathmove(struct jtag_command *cmd)
156 LOG_DEBUG_IO("pathmove: %i states, end in %i",
157 cmd->cmd.pathmove->num_states,
158 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
160 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
163 static void jlink_execute_scan(struct jtag_command *cmd)
165 LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
166 jtag_scan_type(cmd->cmd.scan));
168 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
169 while (cmd->cmd.scan->num_fields > 0
170 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
171 cmd->cmd.scan->num_fields--;
172 LOG_DEBUG("discarding trailing empty field");
175 if (cmd->cmd.scan->num_fields == 0) {
176 LOG_DEBUG("empty scan, doing nothing");
180 if (cmd->cmd.scan->ir_scan) {
181 if (tap_get_state() != TAP_IRSHIFT) {
182 jlink_end_state(TAP_IRSHIFT);
186 if (tap_get_state() != TAP_DRSHIFT) {
187 jlink_end_state(TAP_DRSHIFT);
192 jlink_end_state(cmd->cmd.scan->end_state);
194 struct scan_field *field = cmd->cmd.scan->fields;
195 unsigned scan_size = 0;
197 for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
198 scan_size += field->num_bits;
199 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
200 field->in_value ? "in" : "",
201 field->out_value ? "out" : "",
203 cmd->cmd.scan->num_fields,
206 if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
207 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
208 * movement. This last field can't have length zero, it was checked above. */
209 jlink_clock_data(field->out_value,
215 field->num_bits - 1);
216 uint8_t last_bit = 0;
217 if (field->out_value)
218 bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
219 uint8_t tms_bits = 0x01;
220 jlink_clock_data(&last_bit,
227 tap_set_state(tap_state_transition(tap_get_state(), 1));
228 jlink_clock_data(NULL,
235 tap_set_state(tap_state_transition(tap_get_state(), 0));
237 jlink_clock_data(field->out_value,
246 if (tap_get_state() != tap_get_end_state()) {
247 jlink_end_state(tap_get_end_state());
251 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
252 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
253 tap_state_name(tap_get_end_state()));
256 static void jlink_execute_sleep(struct jtag_command *cmd)
258 LOG_DEBUG_IO("sleep %" PRIu32 "", cmd->cmd.sleep->us);
260 jtag_sleep(cmd->cmd.sleep->us);
263 static int jlink_execute_command(struct jtag_command *cmd)
266 case JTAG_STABLECLOCKS:
267 jlink_execute_stableclocks(cmd);
270 jlink_execute_runtest(cmd);
273 jlink_execute_statemove(cmd);
276 jlink_execute_pathmove(cmd);
279 jlink_execute_scan(cmd);
282 jlink_execute_sleep(cmd);
285 LOG_ERROR("BUG: Unknown JTAG command type encountered");
286 return ERROR_JTAG_QUEUE_FAILED;
292 static int jlink_execute_queue(void)
295 struct jtag_command *cmd = jtag_command_queue;
298 ret = jlink_execute_command(cmd);
306 return jlink_flush();
309 static int jlink_speed(int speed)
312 struct jaylink_speed tmp;
315 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
316 ret = jaylink_get_speeds(devh, &tmp);
318 if (ret != JAYLINK_OK) {
319 LOG_ERROR("jaylink_get_speeds() failed: %s",
320 jaylink_strerror(ret));
321 return ERROR_JTAG_DEVICE_ERROR;
325 max_speed = tmp.freq / tmp.div;
327 max_speed = JLINK_MAX_SPEED;
331 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
332 LOG_ERROR("Adaptive clocking is not supported by the device");
333 return ERROR_JTAG_NOT_IMPLEMENTED;
336 speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
337 } else if (speed > max_speed) {
338 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum)", speed,
343 ret = jaylink_set_speed(devh, speed);
345 if (ret != JAYLINK_OK) {
346 LOG_ERROR("jaylink_set_speed() failed: %s",
347 jaylink_strerror(ret));
348 return ERROR_JTAG_DEVICE_ERROR;
354 static int jlink_speed_div(int speed, int *khz)
361 static int jlink_khz(int khz, int *jtag_speed)
368 static bool read_device_config(struct device_config *cfg)
372 ret = jaylink_read_raw_config(devh, (uint8_t *)cfg);
374 if (ret != JAYLINK_OK) {
375 LOG_ERROR("jaylink_read_raw_config() failed: %s",
376 jaylink_strerror(ret));
380 if (cfg->usb_address == 0xff)
381 cfg->usb_address = 0x00;
383 if (cfg->target_power == 0xffffffff)
384 cfg->target_power = 0;
389 static int select_interface(void)
394 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
395 if (iface != JAYLINK_TIF_JTAG) {
396 LOG_ERROR("Device supports JTAG transport only");
397 return ERROR_JTAG_INIT_FAILED;
403 ret = jaylink_get_available_interfaces(devh, &interfaces);
405 if (ret != JAYLINK_OK) {
406 LOG_ERROR("jaylink_get_available_interfaces() failed: %s",
407 jaylink_strerror(ret));
408 return ERROR_JTAG_INIT_FAILED;
411 if (!(interfaces & (1 << iface))) {
412 LOG_ERROR("Selected transport is not supported by the device");
413 return ERROR_JTAG_INIT_FAILED;
416 ret = jaylink_select_interface(devh, iface, NULL);
419 LOG_ERROR("jaylink_select_interface() failed: %s",
420 jaylink_strerror(ret));
421 return ERROR_JTAG_INIT_FAILED;
427 static int jlink_register(void)
434 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
437 ret = jaylink_register(devh, &conn, connlist, &count);
439 if (ret != JAYLINK_OK) {
440 LOG_ERROR("jaylink_register() failed: %s", jaylink_strerror(ret));
444 handle_found = false;
446 for (i = 0; i < count; i++) {
447 if (connlist[i].handle == conn.handle) {
454 LOG_ERROR("Registration failed: maximum number of connections on the "
463 * Adjust the SWD transaction buffer size depending on the free device internal
464 * memory. This ensures that the SWD transactions sent to the device do not
465 * exceed the internal memory of the device.
467 static bool adjust_swd_buffer_size(void)
472 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
475 ret = jaylink_get_free_memory(devh, &tmp);
477 if (ret != JAYLINK_OK) {
478 LOG_ERROR("jaylink_get_free_memory() failed: %s",
479 jaylink_strerror(ret));
484 LOG_ERROR("Not enough free device internal memory: %" PRIu32 " bytes", tmp);
488 tmp = MIN(JLINK_TAP_BUFFER_SIZE, (tmp - 16) / 2);
490 if (tmp != swd_buffer_size) {
491 swd_buffer_size = tmp;
492 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes",
499 static int jaylink_log_handler(const struct jaylink_context *ctx,
500 enum jaylink_log_level level, const char *format, va_list args,
506 case JAYLINK_LOG_LEVEL_ERROR:
509 case JAYLINK_LOG_LEVEL_WARNING:
510 tmp = LOG_LVL_WARNING;
513 * Forward info messages to the debug output because they are more verbose
514 * than info messages of OpenOCD.
516 case JAYLINK_LOG_LEVEL_INFO:
517 case JAYLINK_LOG_LEVEL_DEBUG:
520 case JAYLINK_LOG_LEVEL_DEBUG_IO:
521 tmp = LOG_LVL_DEBUG_IO;
524 tmp = LOG_LVL_WARNING;
527 log_vprintf_lf(tmp, __FILE__, __LINE__, __func__, format, args);
532 static bool jlink_usb_location_equal(struct jaylink_device *dev)
540 retval = jaylink_device_get_usb_bus_ports(dev, &bus, &ports, &num_ports);
542 if (retval == JAYLINK_ERR_NOT_SUPPORTED) {
544 } else if (retval != JAYLINK_OK) {
545 LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s",
546 jaylink_strerror(retval));
550 equal = adapter_usb_location_equal(bus, ports, num_ports);
557 static int jlink_open_device(uint32_t ifaces, bool *found_device)
559 int ret = jaylink_discovery_scan(jayctx, ifaces);
560 if (ret != JAYLINK_OK) {
561 LOG_ERROR("jaylink_discovery_scan() failed: %s", jaylink_strerror(ret));
562 jaylink_exit(jayctx);
563 return ERROR_JTAG_INIT_FAILED;
567 struct jaylink_device **devs;
568 ret = jaylink_get_devices(jayctx, &devs, &num_devices);
570 if (ret != JAYLINK_OK) {
571 LOG_ERROR("jaylink_get_devices() failed: %s", jaylink_strerror(ret));
572 jaylink_exit(jayctx);
573 return ERROR_JTAG_INIT_FAILED;
576 use_usb_location = !!adapter_usb_get_location();
578 if (!use_serial_number && !use_usb_address && !use_usb_location && num_devices > 1) {
579 LOG_ERROR("Multiple devices found, specify the desired device");
580 jaylink_free_devices(devs, true);
581 jaylink_exit(jayctx);
582 return ERROR_JTAG_INIT_FAILED;
585 *found_device = false;
587 for (size_t i = 0; devs[i]; i++) {
588 struct jaylink_device *dev = devs[i];
590 if (use_serial_number) {
592 ret = jaylink_device_get_serial_number(dev, &tmp);
594 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
596 } else if (ret != JAYLINK_OK) {
597 LOG_WARNING("jaylink_device_get_serial_number() failed: %s",
598 jaylink_strerror(ret));
602 if (serial_number != tmp)
606 if (use_usb_address) {
607 enum jaylink_usb_address address;
608 ret = jaylink_device_get_usb_address(dev, &address);
610 if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
612 } else if (ret != JAYLINK_OK) {
613 LOG_WARNING("jaylink_device_get_usb_address() failed: %s",
614 jaylink_strerror(ret));
618 if (usb_address != address)
622 if (use_usb_location && !jlink_usb_location_equal(dev))
625 ret = jaylink_open(dev, &devh);
627 if (ret == JAYLINK_OK) {
628 *found_device = true;
632 LOG_ERROR("Failed to open device: %s", jaylink_strerror(ret));
635 jaylink_free_devices(devs, true);
640 static int jlink_init(void)
643 char *firmware_version;
644 struct jaylink_hardware_version hwver;
645 struct jaylink_hardware_status hwstatus;
648 LOG_DEBUG("Using libjaylink %s (compiled with %s)",
649 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
651 if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB) && use_usb_address) {
652 LOG_ERROR("J-Link driver does not support USB devices");
653 return ERROR_JTAG_INIT_FAILED;
656 ret = jaylink_init(&jayctx);
658 if (ret != JAYLINK_OK) {
659 LOG_ERROR("jaylink_init() failed: %s", jaylink_strerror(ret));
660 return ERROR_JTAG_INIT_FAILED;
663 ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
665 if (ret != JAYLINK_OK) {
666 LOG_ERROR("jaylink_log_set_callback() failed: %s",
667 jaylink_strerror(ret));
668 jaylink_exit(jayctx);
669 return ERROR_JTAG_INIT_FAILED;
672 const char *serial = adapter_get_required_serial();
674 ret = jaylink_parse_serial_number(serial, &serial_number);
675 if (ret == JAYLINK_ERR) {
676 LOG_ERROR("Invalid serial number: %s", serial);
677 jaylink_exit(jayctx);
678 return ERROR_JTAG_INIT_FAILED;
680 if (ret != JAYLINK_OK) {
681 LOG_ERROR("jaylink_parse_serial_number() failed: %s", jaylink_strerror(ret));
682 jaylink_exit(jayctx);
683 return ERROR_JTAG_INIT_FAILED;
685 use_serial_number = true;
686 use_usb_address = false;
690 ret = jlink_open_device(JAYLINK_HIF_USB, &found_device);
694 if (!found_device && use_serial_number) {
695 ret = jlink_open_device(JAYLINK_HIF_TCP, &found_device);
701 LOG_ERROR("No J-Link device found");
702 jaylink_exit(jayctx);
703 return ERROR_JTAG_INIT_FAILED;
707 * Be careful with changing the following initialization sequence because
708 * some devices are known to be sensitive regarding the order.
711 ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
713 if (ret != JAYLINK_OK) {
714 LOG_ERROR("jaylink_get_firmware_version() failed: %s",
715 jaylink_strerror(ret));
717 jaylink_exit(jayctx);
718 return ERROR_JTAG_INIT_FAILED;
719 } else if (length > 0) {
720 LOG_INFO("%s", firmware_version);
721 free(firmware_version);
723 LOG_WARNING("Device responds empty firmware version string");
726 memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
727 ret = jaylink_get_caps(devh, caps);
729 if (ret != JAYLINK_OK) {
730 LOG_ERROR("jaylink_get_caps() failed: %s", jaylink_strerror(ret));
732 jaylink_exit(jayctx);
733 return ERROR_JTAG_INIT_FAILED;
736 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
737 ret = jaylink_get_extended_caps(devh, caps);
739 if (ret != JAYLINK_OK) {
740 LOG_ERROR("jaylink_get_extended_caps() failed: %s",
741 jaylink_strerror(ret));
743 jaylink_exit(jayctx);
744 return ERROR_JTAG_INIT_FAILED;
748 jtag_command_version = JAYLINK_JTAG_VERSION_2;
750 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
751 ret = jaylink_get_hardware_version(devh, &hwver);
753 if (ret != JAYLINK_OK) {
754 LOG_ERROR("Failed to retrieve hardware version: %s",
755 jaylink_strerror(ret));
757 jaylink_exit(jayctx);
758 return ERROR_JTAG_INIT_FAILED;
761 LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
763 if (hwver.major >= 5)
764 jtag_command_version = JAYLINK_JTAG_VERSION_3;
767 if (iface == JAYLINK_TIF_SWD) {
769 * Adjust the SWD transaction buffer size in case there is already
770 * allocated memory on the device. This happens for example if the
771 * memory for SWO capturing is still allocated because the software
772 * which used the device before has not been shut down properly.
774 if (!adjust_swd_buffer_size()) {
776 jaylink_exit(jayctx);
777 return ERROR_JTAG_INIT_FAILED;
781 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
782 if (!read_device_config(&config)) {
783 LOG_ERROR("Failed to read device configuration data");
785 jaylink_exit(jayctx);
786 return ERROR_JTAG_INIT_FAILED;
789 memcpy(&tmp_config, &config, sizeof(struct device_config));
792 ret = jaylink_get_hardware_status(devh, &hwstatus);
794 if (ret != JAYLINK_OK) {
795 LOG_ERROR("jaylink_get_hardware_status() failed: %s",
796 jaylink_strerror(ret));
798 jaylink_exit(jayctx);
799 return ERROR_JTAG_INIT_FAILED;
802 LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
803 hwstatus.target_voltage % 1000);
807 strcpy(conn.hid, "0.0.0.0");
811 ret = jlink_register();
813 if (ret != ERROR_OK) {
815 jaylink_exit(jayctx);
816 return ERROR_JTAG_INIT_FAILED;
819 ret = select_interface();
821 if (ret != ERROR_OK) {
823 jaylink_exit(jayctx);
831 jlink_speed(adapter_get_speed_khz());
833 if (iface == JAYLINK_TIF_JTAG) {
835 * J-Link devices with firmware version v5 and v6 seems to have an issue
836 * if the first tap move is not divisible by 8, so we send a TLR on
840 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
848 static int jlink_quit(void)
854 ret = jaylink_swo_stop(devh);
856 if (ret != JAYLINK_OK)
857 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
860 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
861 ret = jaylink_unregister(devh, &conn, connlist, &count);
863 if (ret != JAYLINK_OK)
864 LOG_ERROR("jaylink_unregister() failed: %s",
865 jaylink_strerror(ret));
869 jaylink_exit(jayctx);
874 /***************************************************************************/
875 /* Queue command implementations */
877 static void jlink_end_state(tap_state_t state)
879 if (tap_is_state_stable(state))
880 tap_set_end_state(state);
882 LOG_ERROR("BUG: %i is not a valid end state", state);
887 /* Goes to the end state. */
888 static void jlink_state_move(void)
891 uint8_t tms_scan_bits;
893 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
894 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
896 jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
898 tap_set_state(tap_get_end_state());
901 static void jlink_path_move(int num_states, tap_state_t *path)
906 for (i = 0; i < num_states; i++) {
907 if (path[i] == tap_state_transition(tap_get_state(), false))
908 jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
909 else if (path[i] == tap_state_transition(tap_get_state(), true))
910 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
912 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
913 tap_state_name(tap_get_state()), tap_state_name(path[i]));
917 tap_set_state(path[i]);
920 tap_set_end_state(tap_get_state());
923 static void jlink_stableclocks(int num_cycles)
927 uint8_t tms = tap_get_state() == TAP_RESET;
928 /* Execute num_cycles. */
929 for (i = 0; i < num_cycles; i++)
930 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
933 static void jlink_runtest(int num_cycles)
935 tap_state_t saved_end_state = tap_get_end_state();
937 /* Only do a state_move when we're not already in IDLE. */
938 if (tap_get_state() != TAP_IDLE) {
939 jlink_end_state(TAP_IDLE);
944 jlink_stableclocks(num_cycles);
946 /* Finish in end_state. */
947 jlink_end_state(saved_end_state);
949 if (tap_get_state() != tap_get_end_state())
953 static void jlink_reset(int trst, int srst)
955 LOG_DEBUG("TRST: %i, SRST: %i", trst, srst);
957 /* Signals are active low. */
959 jaylink_set_reset(devh);
962 jaylink_clear_reset(devh);
965 jaylink_jtag_clear_trst(devh);
968 jaylink_jtag_set_trst(devh);
971 static int jlink_reset_safe(int trst, int srst)
974 jlink_reset(trst, srst);
975 return jlink_flush();
978 COMMAND_HANDLER(jlink_usb_command)
983 command_print(CMD, "Need exactly one argument for jlink usb");
984 return ERROR_COMMAND_SYNTAX_ERROR;
987 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
988 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
992 if (tmp < JAYLINK_USB_ADDRESS_0 || tmp > JAYLINK_USB_ADDRESS_3) {
993 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
999 use_usb_address = true;
1004 COMMAND_HANDLER(jlink_handle_hwstatus_command)
1007 struct jaylink_hardware_status status;
1009 ret = jaylink_get_hardware_status(devh, &status);
1011 if (ret != JAYLINK_OK) {
1012 command_print(CMD, "jaylink_get_hardware_status() failed: %s",
1013 jaylink_strerror(ret));
1017 command_print(CMD, "VTarget = %u.%03u V",
1018 status.target_voltage / 1000, status.target_voltage % 1000);
1020 command_print(CMD, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1021 "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
1022 status.tres, status.trst);
1024 if (status.target_voltage < 1500)
1025 command_print(CMD, "Target voltage too low. Check target power");
1030 COMMAND_HANDLER(jlink_handle_free_memory_command)
1035 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
1036 command_print(CMD, "Retrieval of free memory is not supported by "
1041 ret = jaylink_get_free_memory(devh, &tmp);
1043 if (ret != JAYLINK_OK) {
1044 command_print(CMD, "jaylink_get_free_memory() failed: %s",
1045 jaylink_strerror(ret));
1049 command_print(CMD, "Device has %" PRIu32 " bytes of free memory", tmp);
1054 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1060 switch (jtag_command_version) {
1061 case JAYLINK_JTAG_VERSION_2:
1064 case JAYLINK_JTAG_VERSION_3:
1071 command_print(CMD, "JTAG command version: %i", version);
1072 } else if (CMD_ARGC == 1) {
1073 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
1074 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1075 return ERROR_COMMAND_SYNTAX_ERROR;
1080 jtag_command_version = JAYLINK_JTAG_VERSION_2;
1083 jtag_command_version = JAYLINK_JTAG_VERSION_3;
1086 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1087 return ERROR_COMMAND_SYNTAX_ERROR;
1090 command_print(CMD, "Need exactly one argument for jlink jtag");
1091 return ERROR_COMMAND_SYNTAX_ERROR;
1097 COMMAND_HANDLER(jlink_handle_target_power_command)
1102 if (CMD_ARGC != 1) {
1103 command_print(CMD, "Need exactly one argument for jlink targetpower");
1104 return ERROR_COMMAND_SYNTAX_ERROR;
1107 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1108 command_print(CMD, "Target power supply is not supported by the "
1113 if (!strcmp(CMD_ARGV[0], "on")) {
1115 } else if (!strcmp(CMD_ARGV[0], "off")) {
1118 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1122 ret = jaylink_set_target_power(devh, enable);
1124 if (ret != JAYLINK_OK) {
1125 command_print(CMD, "jaylink_set_target_power() failed: %s",
1126 jaylink_strerror(ret));
1133 static void show_config_usb_address(struct command_invocation *cmd)
1135 if (config.usb_address != tmp_config.usb_address)
1136 command_print(cmd, "USB address: %u [%u]", config.usb_address,
1137 tmp_config.usb_address);
1139 command_print(cmd, "USB address: %u", config.usb_address);
1142 static void show_config_ip_address(struct command_invocation *cmd)
1144 if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1145 command_print(cmd, "IP address: %d.%d.%d.%d",
1146 config.ip_address[3], config.ip_address[2],
1147 config.ip_address[1], config.ip_address[0]);
1149 command_print(cmd, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1150 config.ip_address[3], config.ip_address[2],
1151 config.ip_address[1], config.ip_address[0],
1152 tmp_config.ip_address[3], tmp_config.ip_address[2],
1153 tmp_config.ip_address[1], tmp_config.ip_address[0]);
1155 if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1156 command_print(cmd, "Subnet mask: %d.%d.%d.%d",
1157 config.subnet_mask[3], config.subnet_mask[2],
1158 config.subnet_mask[1], config.subnet_mask[0]);
1160 command_print(cmd, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1161 config.subnet_mask[3], config.subnet_mask[2],
1162 config.subnet_mask[1], config.subnet_mask[0],
1163 tmp_config.subnet_mask[3], tmp_config.subnet_mask[2],
1164 tmp_config.subnet_mask[1], tmp_config.subnet_mask[0]);
1167 static void show_config_mac_address(struct command_invocation *cmd)
1169 if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1170 command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1171 config.mac_address[5], config.mac_address[4],
1172 config.mac_address[3], config.mac_address[2],
1173 config.mac_address[1], config.mac_address[0]);
1175 command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1176 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1177 config.mac_address[5], config.mac_address[4],
1178 config.mac_address[3], config.mac_address[2],
1179 config.mac_address[1], config.mac_address[0],
1180 tmp_config.mac_address[5], tmp_config.mac_address[4],
1181 tmp_config.mac_address[3], tmp_config.mac_address[2],
1182 tmp_config.mac_address[1], tmp_config.mac_address[0]);
1185 static void show_config_target_power(struct command_invocation *cmd)
1187 const char *target_power;
1188 const char *current_target_power;
1190 if (!config.target_power)
1191 target_power = "off";
1193 target_power = "on";
1195 if (!tmp_config.target_power)
1196 current_target_power = "off";
1198 current_target_power = "on";
1200 if (config.target_power != tmp_config.target_power)
1201 command_print(cmd, "Target power supply: %s [%s]", target_power,
1202 current_target_power);
1204 command_print(cmd, "Target power supply: %s", target_power);
1207 static void show_config(struct command_invocation *cmd)
1209 command_print(cmd, "J-Link device configuration:");
1211 show_config_usb_address(cmd);
1213 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1214 show_config_target_power(cmd);
1216 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1217 show_config_ip_address(cmd);
1218 show_config_mac_address(cmd);
1222 static int poll_trace(uint8_t *buf, size_t *size)
1229 ret = jaylink_swo_read(devh, buf, &length);
1231 if (ret != JAYLINK_OK) {
1232 LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret));
1241 static uint32_t calculate_trace_buffer_size(void)
1246 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1249 ret = jaylink_get_free_memory(devh, &tmp);
1251 if (ret != JAYLINK_OK) {
1252 LOG_ERROR("jaylink_get_free_memory() failed: %s",
1253 jaylink_strerror(ret));
1257 if (tmp > 0x3fff || tmp <= 0x600)
1262 return tmp & 0xffffff00;
1265 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
1266 uint32_t trace_freq, uint16_t *prescaler)
1268 unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1269 if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1272 /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1273 unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1274 if (presc * trace_freq < traceclkin_freq - max_deviation ||
1275 presc * trace_freq > traceclkin_freq + max_deviation)
1283 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed,
1284 unsigned int traceclkin_freq, unsigned int *trace_freq,
1285 uint16_t *prescaler)
1291 for (divider = speed.min_div; divider <= speed.max_div; divider++) {
1292 *trace_freq = speed.freq / divider;
1293 presc = ((1.0 - SWO_MAX_FREQ_DEV) * traceclkin_freq) / *trace_freq + 1;
1295 if (presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1298 deviation = fabs(1.0 - ((double)*trace_freq * presc / traceclkin_freq));
1300 if (deviation <= SWO_MAX_FREQ_DEV) {
1309 static int config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1310 uint32_t port_size, unsigned int *trace_freq,
1311 unsigned int traceclkin_freq, uint16_t *prescaler)
1314 uint32_t buffer_size;
1315 struct jaylink_swo_speed speed;
1320 trace_enabled = enabled;
1322 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1326 LOG_ERROR("Trace capturing is not supported by the device");
1330 ret = jaylink_swo_stop(devh);
1332 if (ret != JAYLINK_OK) {
1333 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
1339 * Adjust the SWD transaction buffer size as stopping SWO capturing
1340 * deallocates device internal memory.
1342 if (!adjust_swd_buffer_size())
1348 if (pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART) {
1349 LOG_ERROR("Selected pin protocol is not supported");
1353 buffer_size = calculate_trace_buffer_size();
1356 LOG_ERROR("Not enough free device memory to start trace capturing");
1360 ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1362 if (ret != JAYLINK_OK) {
1363 LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1364 jaylink_strerror(ret));
1368 if (*trace_freq > 0) {
1369 divider = speed.freq / *trace_freq;
1370 min_freq = speed.freq / speed.max_div;
1371 max_freq = speed.freq / speed.min_div;
1373 if (*trace_freq > max_freq) {
1374 LOG_INFO("Given SWO frequency too high, using %" PRIu32 " Hz instead",
1376 *trace_freq = max_freq;
1377 } else if (*trace_freq < min_freq) {
1378 LOG_INFO("Given SWO frequency too low, using %" PRIu32 " Hz instead",
1380 *trace_freq = min_freq;
1381 } else if (*trace_freq != speed.freq / divider) {
1382 *trace_freq = speed.freq / divider;
1384 LOG_INFO("Given SWO frequency is not supported by the device, "
1385 "using %u Hz instead", *trace_freq);
1388 if (!calculate_swo_prescaler(traceclkin_freq, *trace_freq,
1390 LOG_ERROR("SWO frequency is not suitable. Please choose a "
1391 "different frequency or use auto-detection");
1395 LOG_INFO("Trying to auto-detect SWO frequency");
1397 if (!detect_swo_freq_and_prescaler(speed, traceclkin_freq, trace_freq,
1399 LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1400 "could not be achieved", SWO_MAX_FREQ_DEV);
1401 LOG_ERROR("Auto-detection of SWO frequency failed");
1405 LOG_INFO("Using SWO frequency of %u Hz", *trace_freq);
1408 ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1411 if (ret != JAYLINK_OK) {
1412 LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret));
1416 LOG_DEBUG("Using %" PRIu32 " bytes device memory for trace capturing",
1420 * Adjust the SWD transaction buffer size as starting SWO capturing
1421 * allocates device internal memory.
1423 if (!adjust_swd_buffer_size())
1429 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1433 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1434 command_print(CMD, "Reading configuration is not supported by the "
1440 show_config_usb_address(CMD);
1441 } else if (CMD_ARGC == 1) {
1442 if (sscanf(CMD_ARGV[0], "%" SCNd8, &tmp) != 1) {
1443 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
1447 if (tmp > JAYLINK_USB_ADDRESS_3) {
1448 command_print(CMD, "Invalid USB address: %u", tmp);
1452 tmp_config.usb_address = tmp;
1454 command_print(CMD, "Need exactly one argument for jlink config usb");
1455 return ERROR_COMMAND_SYNTAX_ERROR;
1461 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1465 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1466 command_print(CMD, "Reading configuration is not supported by the "
1471 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1472 command_print(CMD, "Target power supply is not supported by the "
1478 show_config_target_power(CMD);
1479 } else if (CMD_ARGC == 1) {
1480 if (!strcmp(CMD_ARGV[0], "on")) {
1482 } else if (!strcmp(CMD_ARGV[0], "off")) {
1485 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1489 tmp_config.target_power = enable;
1491 command_print(CMD, "Need exactly one argument for jlink config "
1493 return ERROR_COMMAND_SYNTAX_ERROR;
1499 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1506 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1507 command_print(CMD, "Reading configuration is not supported by the "
1512 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1513 command_print(CMD, "Ethernet connectivity is not supported by the "
1519 show_config_mac_address(CMD);
1520 } else if (CMD_ARGC == 1) {
1523 if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' ||
1524 str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1525 command_print(CMD, "Invalid MAC address format");
1526 return ERROR_COMMAND_SYNTAX_ERROR;
1529 for (i = 5; i >= 0; i--) {
1530 addr[i] = strtoul(str, &e, 16);
1534 if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1535 command_print(CMD, "Invalid MAC address: zero address");
1536 return ERROR_COMMAND_SYNTAX_ERROR;
1539 if (!(0x01 & addr[0])) {
1540 command_print(CMD, "Invalid MAC address: multicast address");
1541 return ERROR_COMMAND_SYNTAX_ERROR;
1544 memcpy(tmp_config.mac_address, addr, sizeof(addr));
1546 command_print(CMD, "Need exactly one argument for jlink config mac");
1547 return ERROR_COMMAND_SYNTAX_ERROR;
1553 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1557 const char *s_save = s;
1563 for (i = 0; i < 4; i++) {
1564 lip[i] = strtoul(s, &e, 10);
1566 if (*e != '.' && i != 3)
1573 memcpy(ip, lip, sizeof(lip));
1578 static void cpy_ip(uint8_t *dst, uint8_t *src)
1582 for (i = 0, j = 3; i < 4; i++, j--)
1586 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1588 uint8_t ip_address[4];
1589 uint32_t subnet_mask = 0;
1591 uint8_t subnet_bits = 24;
1593 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1594 command_print(CMD, "Reading configuration is not supported by the "
1599 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1600 command_print(CMD, "Ethernet connectivity is not supported by the "
1606 show_config_ip_address(CMD);
1608 if (!string_to_ip(CMD_ARGV[0], ip_address, &i))
1609 return ERROR_COMMAND_SYNTAX_ERROR;
1611 len = strlen(CMD_ARGV[0]);
1613 /* Check for format A.B.C.D/E. */
1615 if (CMD_ARGV[0][i] != '/')
1616 return ERROR_COMMAND_SYNTAX_ERROR;
1618 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1619 } else if (CMD_ARGC > 1) {
1620 if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i))
1621 return ERROR_COMMAND_SYNTAX_ERROR;
1625 subnet_mask = (uint32_t)(subnet_bits < 32 ?
1626 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1628 cpy_ip(tmp_config.ip_address, ip_address);
1629 cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1635 COMMAND_HANDLER(jlink_handle_config_reset_command)
1637 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1640 memcpy(&tmp_config, &config, sizeof(struct device_config));
1646 COMMAND_HANDLER(jlink_handle_config_write_command)
1650 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1651 command_print(CMD, "Reading configuration is not supported by the "
1656 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1657 command_print(CMD, "Writing configuration is not supported by the "
1662 if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1663 command_print(CMD, "Operation not performed due to no changes in "
1664 "the configuration");
1668 ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1670 if (ret != JAYLINK_OK) {
1671 LOG_ERROR("jaylink_write_raw_config() failed: %s",
1672 jaylink_strerror(ret));
1676 if (!read_device_config(&config)) {
1677 LOG_ERROR("Failed to read device configuration for verification");
1681 if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1682 LOG_ERROR("Verification of device configuration failed. Please check "
1687 memcpy(&tmp_config, &config, sizeof(struct device_config));
1688 command_print(CMD, "The new device configuration applies after power "
1689 "cycling the J-Link device");
1694 COMMAND_HANDLER(jlink_handle_config_command)
1696 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1697 command_print(CMD, "Device doesn't support reading configuration");
1707 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1717 return ERROR_COMMAND_SYNTAX_ERROR;
1719 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1720 LOG_ERROR("Device does not support EMUCOM");
1724 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1726 tmp = strlen(CMD_ARGV[1]);
1729 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1730 return ERROR_COMMAND_ARGUMENT_INVALID;
1733 buf = malloc(tmp / 2);
1736 LOG_ERROR("Failed to allocate buffer");
1740 dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1742 if (dummy != (tmp / 2)) {
1743 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1745 return ERROR_COMMAND_ARGUMENT_INVALID;
1749 ret = jaylink_emucom_write(devh, channel, buf, &length);
1753 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1754 LOG_ERROR("Channel not supported by the device");
1756 } else if (ret != JAYLINK_OK) {
1757 LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret));
1761 if (length != (tmp / 2))
1762 LOG_WARNING("Only %" PRIu32 " bytes written to the channel", length);
1767 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1776 return ERROR_COMMAND_SYNTAX_ERROR;
1778 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1779 LOG_ERROR("Device does not support EMUCOM");
1783 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1784 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
1786 buf = malloc(length * 3 + 1);
1789 LOG_ERROR("Failed to allocate buffer");
1793 ret = jaylink_emucom_read(devh, channel, buf, &length);
1795 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1796 LOG_ERROR("Channel is not supported by the device");
1799 } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1800 LOG_ERROR("Channel is not available for the requested amount of data. "
1801 "%" PRIu32 " bytes are available", length);
1804 } else if (ret != JAYLINK_OK) {
1805 LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret));
1810 tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1812 if (tmp != 2 * length) {
1813 LOG_ERROR("Failed to convert data into hexadecimal string");
1818 command_print(CMD, "%s", buf + length);
1824 static const struct command_registration jlink_config_subcommand_handlers[] = {
1827 .handler = &jlink_handle_config_usb_address_command,
1828 .mode = COMMAND_EXEC,
1829 .help = "set the USB address",
1833 .name = "targetpower",
1834 .handler = &jlink_handle_config_target_power_command,
1835 .mode = COMMAND_EXEC,
1836 .help = "set the target power supply",
1841 .handler = &jlink_handle_config_mac_address_command,
1842 .mode = COMMAND_EXEC,
1843 .help = "set the MAC Address",
1844 .usage = "[ff:ff:ff:ff:ff:ff]",
1848 .handler = &jlink_handle_config_ip_address_command,
1849 .mode = COMMAND_EXEC,
1850 .help = "set the IP address, where A.B.C.D is the IP address, "
1851 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1852 .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1856 .handler = &jlink_handle_config_reset_command,
1857 .mode = COMMAND_EXEC,
1858 .help = "undo configuration changes",
1863 .handler = &jlink_handle_config_write_command,
1864 .mode = COMMAND_EXEC,
1865 .help = "write configuration to the device",
1868 COMMAND_REGISTRATION_DONE
1871 static const struct command_registration jlink_emucom_subcommand_handlers[] = {
1874 .handler = &jlink_handle_emucom_write_command,
1875 .mode = COMMAND_EXEC,
1876 .help = "write to a channel",
1877 .usage = "<channel> <data>",
1881 .handler = &jlink_handle_emucom_read_command,
1882 .mode = COMMAND_EXEC,
1883 .help = "read from a channel",
1884 .usage = "<channel> <length>"
1886 COMMAND_REGISTRATION_DONE
1889 static const struct command_registration jlink_subcommand_handlers[] = {
1892 .handler = &jlink_handle_jlink_jtag_command,
1893 .mode = COMMAND_EXEC,
1894 .help = "select the JTAG command version",
1898 .name = "targetpower",
1899 .handler = &jlink_handle_target_power_command,
1900 .mode = COMMAND_EXEC,
1901 .help = "set the target power supply",
1906 .handler = &jlink_handle_free_memory_command,
1907 .mode = COMMAND_EXEC,
1908 .help = "show free device memory",
1913 .handler = &jlink_handle_hwstatus_command,
1914 .mode = COMMAND_EXEC,
1915 .help = "show the hardware status",
1920 .handler = &jlink_usb_command,
1921 .mode = COMMAND_CONFIG,
1922 .help = "set the USB address of the device that should be used",
1927 .handler = &jlink_handle_config_command,
1928 .mode = COMMAND_EXEC,
1929 .help = "access the device configuration. If no argument is given "
1930 "this will show the device configuration",
1931 .chain = jlink_config_subcommand_handlers,
1936 .mode = COMMAND_EXEC,
1937 .help = "access EMUCOM channel",
1938 .chain = jlink_emucom_subcommand_handlers,
1941 COMMAND_REGISTRATION_DONE
1944 static const struct command_registration jlink_command_handlers[] = {
1947 .mode = COMMAND_ANY,
1948 .help = "perform jlink management",
1949 .chain = jlink_subcommand_handlers,
1952 COMMAND_REGISTRATION_DONE
1955 static int jlink_swd_init(void)
1957 iface = JAYLINK_TIF_SWD;
1962 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1964 assert(!(cmd & SWD_CMD_RNW));
1965 jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1968 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1970 assert(cmd & SWD_CMD_RNW);
1971 jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1974 /***************************************************************************/
1975 /* J-Link tap functions */
1977 static unsigned tap_length;
1978 /* In SWD mode use tms buffer for direction control */
1979 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
1980 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
1981 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
1983 struct pending_scan_result {
1984 /** First bit position in tdo_buffer to read. */
1986 /** Number of bits to read. */
1988 /** Location to store the result */
1990 /** Offset in the destination buffer */
1991 unsigned buffer_offset;
1994 #define MAX_PENDING_SCAN_RESULTS 256
1996 static int pending_scan_results_length;
1997 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1999 static void jlink_tap_init(void)
2002 pending_scan_results_length = 0;
2003 memset(tms_buffer, 0, sizeof(tms_buffer));
2004 memset(tdi_buffer, 0, sizeof(tdi_buffer));
2007 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
2008 const uint8_t *tms_out, unsigned tms_offset,
2009 uint8_t *in, unsigned in_offset,
2013 unsigned available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
2015 if (!available_length ||
2016 (in && pending_scan_results_length == MAX_PENDING_SCAN_RESULTS)) {
2017 if (jlink_flush() != ERROR_OK)
2019 available_length = JLINK_TAP_BUFFER_SIZE;
2022 struct pending_scan_result *pending_scan_result =
2023 &pending_scan_results_buffer[pending_scan_results_length];
2025 unsigned scan_length = length > available_length ?
2026 available_length : length;
2029 buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
2031 buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
2034 pending_scan_result->first = tap_length;
2035 pending_scan_result->length = scan_length;
2036 pending_scan_result->buffer = in;
2037 pending_scan_result->buffer_offset = in_offset;
2038 pending_scan_results_length++;
2041 tap_length += scan_length;
2042 out_offset += scan_length;
2043 tms_offset += scan_length;
2044 in_offset += scan_length;
2045 length -= scan_length;
2046 } while (length > 0);
2049 static int jlink_flush(void)
2057 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
2058 tap_length, jlink_last_state);
2060 ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
2061 tap_length, jtag_command_version);
2063 if (ret != JAYLINK_OK) {
2064 LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret));
2066 return ERROR_JTAG_QUEUE_FAILED;
2069 for (i = 0; i < pending_scan_results_length; i++) {
2070 struct pending_scan_result *p = &pending_scan_results_buffer[i];
2072 buf_set_buf(tdo_buffer, p->first, p->buffer,
2073 p->buffer_offset, p->length);
2075 LOG_DEBUG_IO("Pending scan result, length = %d", p->length);
2083 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
2085 unsigned int tap_pos = tap_length;
2088 buf_set_u32(buf, tap_pos, 32, val);
2094 buf_set_u32(buf, tap_pos, len, val);
2097 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2099 const uint32_t dir_out = 0xffffffff;
2102 bit_copy(tdi_buffer, tap_length, data, 0, len);
2104 fill_buffer(tdi_buffer, 0, len);
2106 fill_buffer(tms_buffer, dir_out, len);
2110 static void jlink_queue_data_in(uint32_t len)
2112 const uint32_t dir_in = 0;
2114 fill_buffer(tms_buffer, dir_in, len);
2118 static int jlink_swd_switch_seq(enum swd_special_seq seq)
2125 LOG_DEBUG("SWD line reset");
2126 s = swd_seq_line_reset;
2127 s_len = swd_seq_line_reset_len;
2130 LOG_DEBUG("JTAG-to-SWD");
2131 s = swd_seq_jtag_to_swd;
2132 s_len = swd_seq_jtag_to_swd_len;
2134 case JTAG_TO_DORMANT:
2135 LOG_DEBUG("JTAG-to-DORMANT");
2136 s = swd_seq_jtag_to_dormant;
2137 s_len = swd_seq_jtag_to_dormant_len;
2140 LOG_DEBUG("SWD-to-JTAG");
2141 s = swd_seq_swd_to_jtag;
2142 s_len = swd_seq_swd_to_jtag_len;
2144 case SWD_TO_DORMANT:
2145 LOG_DEBUG("SWD-to-DORMANT");
2146 s = swd_seq_swd_to_dormant;
2147 s_len = swd_seq_swd_to_dormant_len;
2149 case DORMANT_TO_SWD:
2150 LOG_DEBUG("DORMANT-to-SWD");
2151 s = swd_seq_dormant_to_swd;
2152 s_len = swd_seq_dormant_to_swd_len;
2154 case DORMANT_TO_JTAG:
2155 LOG_DEBUG("DORMANT-to-JTAG");
2156 s = swd_seq_dormant_to_jtag;
2157 s_len = swd_seq_dormant_to_jtag_len;
2160 LOG_ERROR("Sequence %d not supported", seq);
2164 jlink_queue_data_out(s, s_len);
2169 static int jlink_swd_run_queue(void)
2174 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length);
2176 if (queued_retval != ERROR_OK) {
2177 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
2182 * A transaction must be followed by another transaction or at least 8 idle
2183 * cycles to ensure that data is clocked through the AP.
2185 jlink_queue_data_out(NULL, 8);
2187 ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2189 if (ret != JAYLINK_OK) {
2190 LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret));
2194 for (i = 0; i < pending_scan_results_length; i++) {
2195 int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
2197 if (ack != SWD_ACK_OK) {
2198 LOG_DEBUG("SWD ack not OK: %d %s", ack,
2199 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2200 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
2202 } else if (pending_scan_results_buffer[i].length) {
2203 uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2204 int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
2206 if (parity != parity_u32(data)) {
2207 LOG_ERROR("SWD: Read data parity mismatch");
2208 queued_retval = ERROR_FAIL;
2212 if (pending_scan_results_buffer[i].buffer)
2213 *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2219 ret = queued_retval;
2220 queued_retval = ERROR_OK;
2225 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2227 uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2228 if (tap_length + 46 + 8 + ap_delay_clk >= swd_buffer_size * 8 ||
2229 pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
2230 /* Not enough room in the queue. Run the queue. */
2231 queued_retval = jlink_swd_run_queue();
2234 if (queued_retval != ERROR_OK)
2237 cmd |= SWD_CMD_START | SWD_CMD_PARK;
2239 jlink_queue_data_out(&cmd, 8);
2241 pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
2243 if (cmd & SWD_CMD_RNW) {
2244 /* Queue a read transaction. */
2245 pending_scan_results_buffer[pending_scan_results_length].length = 32;
2246 pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
2248 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2250 /* Queue a write transaction. */
2251 pending_scan_results_buffer[pending_scan_results_length].length = 0;
2252 jlink_queue_data_in(1 + 3 + 1);
2254 buf_set_u32(data_parity_trn, 0, 32, data);
2255 buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2257 jlink_queue_data_out(data_parity_trn, 32 + 1);
2260 pending_scan_results_length++;
2262 /* Insert idle cycles after AP accesses to avoid WAIT. */
2263 if (cmd & SWD_CMD_APNDP)
2264 jlink_queue_data_out(NULL, ap_delay_clk);
2267 static const struct swd_driver jlink_swd = {
2268 .init = &jlink_swd_init,
2269 .switch_seq = &jlink_swd_switch_seq,
2270 .read_reg = &jlink_swd_read_reg,
2271 .write_reg = &jlink_swd_write_reg,
2272 .run = &jlink_swd_run_queue,
2275 static const char * const jlink_transports[] = { "jtag", "swd", NULL };
2277 static struct jtag_interface jlink_interface = {
2278 .execute_queue = &jlink_execute_queue,
2281 struct adapter_driver jlink_adapter_driver = {
2283 .transports = jlink_transports,
2284 .commands = jlink_command_handlers,
2286 .init = &jlink_init,
2287 .quit = &jlink_quit,
2288 .reset = &jlink_reset_safe,
2289 .speed = &jlink_speed,
2291 .speed_div = &jlink_speed_div,
2292 .config_trace = &config_trace,
2293 .poll_trace = &poll_trace,
2295 .jtag_ops = &jlink_interface,
2296 .swd_ops = &jlink_swd,