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 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
119 const uint8_t *tms_out, unsigned tms_offset,
120 uint8_t *in, unsigned in_offset,
123 static enum tap_state jlink_last_state = TAP_RESET;
124 static int queued_retval;
126 /***************************************************************************/
127 /* External interface implementation */
129 static void jlink_execute_stableclocks(struct jtag_command *cmd)
131 LOG_DEBUG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
132 jlink_stableclocks(cmd->cmd.runtest->num_cycles);
135 static void jlink_execute_runtest(struct jtag_command *cmd)
137 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
138 cmd->cmd.runtest->end_state);
140 jlink_end_state(cmd->cmd.runtest->end_state);
141 jlink_runtest(cmd->cmd.runtest->num_cycles);
144 static void jlink_execute_statemove(struct jtag_command *cmd)
146 LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
148 jlink_end_state(cmd->cmd.statemove->end_state);
152 static void jlink_execute_pathmove(struct jtag_command *cmd)
154 LOG_DEBUG_IO("pathmove: %i states, end in %i",
155 cmd->cmd.pathmove->num_states,
156 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
158 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
161 static void jlink_execute_scan(struct jtag_command *cmd)
163 LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
164 jtag_scan_type(cmd->cmd.scan));
166 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
167 while (cmd->cmd.scan->num_fields > 0
168 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
169 cmd->cmd.scan->num_fields--;
170 LOG_DEBUG("discarding trailing empty field");
173 if (cmd->cmd.scan->num_fields == 0) {
174 LOG_DEBUG("empty scan, doing nothing");
178 if (cmd->cmd.scan->ir_scan) {
179 if (tap_get_state() != TAP_IRSHIFT) {
180 jlink_end_state(TAP_IRSHIFT);
184 if (tap_get_state() != TAP_DRSHIFT) {
185 jlink_end_state(TAP_DRSHIFT);
190 jlink_end_state(cmd->cmd.scan->end_state);
192 struct scan_field *field = cmd->cmd.scan->fields;
193 unsigned scan_size = 0;
195 for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
196 scan_size += field->num_bits;
197 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
198 field->in_value ? "in" : "",
199 field->out_value ? "out" : "",
201 cmd->cmd.scan->num_fields,
204 if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
205 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
206 * movement. This last field can't have length zero, it was checked above. */
207 jlink_clock_data(field->out_value,
213 field->num_bits - 1);
214 uint8_t last_bit = 0;
215 if (field->out_value)
216 bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
217 uint8_t tms_bits = 0x01;
218 jlink_clock_data(&last_bit,
225 tap_set_state(tap_state_transition(tap_get_state(), 1));
226 jlink_clock_data(NULL,
233 tap_set_state(tap_state_transition(tap_get_state(), 0));
235 jlink_clock_data(field->out_value,
244 if (tap_get_state() != tap_get_end_state()) {
245 jlink_end_state(tap_get_end_state());
249 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
250 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
251 tap_state_name(tap_get_end_state()));
254 static void jlink_execute_sleep(struct jtag_command *cmd)
256 LOG_DEBUG_IO("sleep %" PRIu32 "", cmd->cmd.sleep->us);
258 jtag_sleep(cmd->cmd.sleep->us);
261 static int jlink_execute_command(struct jtag_command *cmd)
264 case JTAG_STABLECLOCKS:
265 jlink_execute_stableclocks(cmd);
268 jlink_execute_runtest(cmd);
271 jlink_execute_statemove(cmd);
274 jlink_execute_pathmove(cmd);
277 jlink_execute_scan(cmd);
280 jlink_execute_sleep(cmd);
283 LOG_ERROR("BUG: Unknown JTAG command type encountered");
284 return ERROR_JTAG_QUEUE_FAILED;
290 static int jlink_execute_queue(void)
293 struct jtag_command *cmd = jtag_command_queue;
296 ret = jlink_execute_command(cmd);
304 return jlink_flush();
307 static int jlink_speed(int speed)
310 struct jaylink_speed tmp;
313 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
314 ret = jaylink_get_speeds(devh, &tmp);
316 if (ret != JAYLINK_OK) {
317 LOG_ERROR("jaylink_get_speeds() failed: %s",
318 jaylink_strerror(ret));
319 return ERROR_JTAG_DEVICE_ERROR;
323 max_speed = tmp.freq / tmp.div;
325 max_speed = JLINK_MAX_SPEED;
329 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
330 LOG_ERROR("Adaptive clocking is not supported by the device");
331 return ERROR_JTAG_NOT_IMPLEMENTED;
334 speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
335 } else if (speed > max_speed) {
336 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum)", speed,
341 ret = jaylink_set_speed(devh, speed);
343 if (ret != JAYLINK_OK) {
344 LOG_ERROR("jaylink_set_speed() failed: %s",
345 jaylink_strerror(ret));
346 return ERROR_JTAG_DEVICE_ERROR;
352 static int jlink_speed_div(int speed, int *khz)
359 static int jlink_khz(int khz, int *jtag_speed)
366 static bool read_device_config(struct device_config *cfg)
370 ret = jaylink_read_raw_config(devh, (uint8_t *)cfg);
372 if (ret != JAYLINK_OK) {
373 LOG_ERROR("jaylink_read_raw_config() failed: %s",
374 jaylink_strerror(ret));
378 if (cfg->usb_address == 0xff)
379 cfg->usb_address = 0x00;
381 if (cfg->target_power == 0xffffffff)
382 cfg->target_power = 0;
387 static int select_interface(void)
392 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
393 if (iface != JAYLINK_TIF_JTAG) {
394 LOG_ERROR("Device supports JTAG transport only");
395 return ERROR_JTAG_INIT_FAILED;
401 ret = jaylink_get_available_interfaces(devh, &interfaces);
403 if (ret != JAYLINK_OK) {
404 LOG_ERROR("jaylink_get_available_interfaces() failed: %s",
405 jaylink_strerror(ret));
406 return ERROR_JTAG_INIT_FAILED;
409 if (!(interfaces & (1 << iface))) {
410 LOG_ERROR("Selected transport is not supported by the device");
411 return ERROR_JTAG_INIT_FAILED;
414 ret = jaylink_select_interface(devh, iface, NULL);
417 LOG_ERROR("jaylink_select_interface() failed: %s",
418 jaylink_strerror(ret));
419 return ERROR_JTAG_INIT_FAILED;
425 static int jlink_register(void)
432 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
435 ret = jaylink_register(devh, &conn, connlist, &count);
437 if (ret != JAYLINK_OK) {
438 LOG_ERROR("jaylink_register() failed: %s", jaylink_strerror(ret));
442 handle_found = false;
444 for (i = 0; i < count; i++) {
445 if (connlist[i].handle == conn.handle) {
452 LOG_ERROR("Registration failed: maximum number of connections on the "
461 * Adjust the SWD transaction buffer size depending on the free device internal
462 * memory. This ensures that the SWD transactions sent to the device do not
463 * exceed the internal memory of the device.
465 static bool adjust_swd_buffer_size(void)
470 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
473 ret = jaylink_get_free_memory(devh, &tmp);
475 if (ret != JAYLINK_OK) {
476 LOG_ERROR("jaylink_get_free_memory() failed: %s",
477 jaylink_strerror(ret));
482 LOG_ERROR("Not enough free device internal memory: %" PRIu32 " bytes", tmp);
486 tmp = MIN(JLINK_TAP_BUFFER_SIZE, (tmp - 16) / 2);
488 if (tmp != swd_buffer_size) {
489 swd_buffer_size = tmp;
490 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes",
497 static int jaylink_log_handler(const struct jaylink_context *ctx,
498 enum jaylink_log_level level, const char *format, va_list args,
504 case JAYLINK_LOG_LEVEL_ERROR:
507 case JAYLINK_LOG_LEVEL_WARNING:
508 tmp = LOG_LVL_WARNING;
511 * Forward info messages to the debug output because they are more verbose
512 * than info messages of OpenOCD.
514 case JAYLINK_LOG_LEVEL_INFO:
515 case JAYLINK_LOG_LEVEL_DEBUG:
518 case JAYLINK_LOG_LEVEL_DEBUG_IO:
519 tmp = LOG_LVL_DEBUG_IO;
522 tmp = LOG_LVL_WARNING;
525 log_vprintf_lf(tmp, __FILE__, __LINE__, __func__, format, args);
530 static bool jlink_usb_location_equal(struct jaylink_device *dev)
538 retval = jaylink_device_get_usb_bus_ports(dev, &bus, &ports, &num_ports);
540 if (retval == JAYLINK_ERR_NOT_SUPPORTED) {
542 } else if (retval != JAYLINK_OK) {
543 LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s",
544 jaylink_strerror(retval));
548 equal = adapter_usb_location_equal(bus, ports, num_ports);
555 static int jlink_open_device(uint32_t ifaces, bool *found_device)
557 int ret = jaylink_discovery_scan(jayctx, ifaces);
558 if (ret != JAYLINK_OK) {
559 LOG_ERROR("jaylink_discovery_scan() failed: %s", jaylink_strerror(ret));
560 jaylink_exit(jayctx);
561 return ERROR_JTAG_INIT_FAILED;
565 struct jaylink_device **devs;
566 ret = jaylink_get_devices(jayctx, &devs, &num_devices);
568 if (ret != JAYLINK_OK) {
569 LOG_ERROR("jaylink_get_devices() failed: %s", jaylink_strerror(ret));
570 jaylink_exit(jayctx);
571 return ERROR_JTAG_INIT_FAILED;
574 use_usb_location = !!adapter_usb_get_location();
576 if (!use_serial_number && !use_usb_address && !use_usb_location && num_devices > 1) {
577 LOG_ERROR("Multiple devices found, specify the desired device");
578 jaylink_free_devices(devs, true);
579 jaylink_exit(jayctx);
580 return ERROR_JTAG_INIT_FAILED;
583 *found_device = false;
585 for (size_t i = 0; devs[i]; i++) {
586 struct jaylink_device *dev = devs[i];
588 if (use_serial_number) {
590 ret = jaylink_device_get_serial_number(dev, &tmp);
592 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
594 } else if (ret != JAYLINK_OK) {
595 LOG_WARNING("jaylink_device_get_serial_number() failed: %s",
596 jaylink_strerror(ret));
600 if (serial_number != tmp)
604 if (use_usb_address) {
605 enum jaylink_usb_address address;
606 ret = jaylink_device_get_usb_address(dev, &address);
608 if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
610 } else if (ret != JAYLINK_OK) {
611 LOG_WARNING("jaylink_device_get_usb_address() failed: %s",
612 jaylink_strerror(ret));
616 if (usb_address != address)
620 if (use_usb_location && !jlink_usb_location_equal(dev))
623 ret = jaylink_open(dev, &devh);
625 if (ret == JAYLINK_OK) {
626 *found_device = true;
630 LOG_ERROR("Failed to open device: %s", jaylink_strerror(ret));
633 jaylink_free_devices(devs, true);
638 static int jlink_init(void)
641 char *firmware_version;
642 struct jaylink_hardware_version hwver;
643 struct jaylink_hardware_status hwstatus;
646 LOG_DEBUG("Using libjaylink %s (compiled with %s)",
647 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
649 if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB) && use_usb_address) {
650 LOG_ERROR("J-Link driver does not support USB devices");
651 return ERROR_JTAG_INIT_FAILED;
654 ret = jaylink_init(&jayctx);
656 if (ret != JAYLINK_OK) {
657 LOG_ERROR("jaylink_init() failed: %s", jaylink_strerror(ret));
658 return ERROR_JTAG_INIT_FAILED;
661 ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
663 if (ret != JAYLINK_OK) {
664 LOG_ERROR("jaylink_log_set_callback() failed: %s",
665 jaylink_strerror(ret));
666 jaylink_exit(jayctx);
667 return ERROR_JTAG_INIT_FAILED;
670 const char *serial = adapter_get_required_serial();
672 ret = jaylink_parse_serial_number(serial, &serial_number);
673 if (ret == JAYLINK_ERR) {
674 LOG_ERROR("Invalid serial number: %s", serial);
675 jaylink_exit(jayctx);
676 return ERROR_JTAG_INIT_FAILED;
678 if (ret != JAYLINK_OK) {
679 LOG_ERROR("jaylink_parse_serial_number() failed: %s", jaylink_strerror(ret));
680 jaylink_exit(jayctx);
681 return ERROR_JTAG_INIT_FAILED;
683 use_serial_number = true;
684 use_usb_address = false;
688 ret = jlink_open_device(JAYLINK_HIF_USB, &found_device);
692 if (!found_device && use_serial_number) {
693 ret = jlink_open_device(JAYLINK_HIF_TCP, &found_device);
699 LOG_ERROR("No J-Link device found");
700 jaylink_exit(jayctx);
701 return ERROR_JTAG_INIT_FAILED;
705 * Be careful with changing the following initialization sequence because
706 * some devices are known to be sensitive regarding the order.
709 ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
711 if (ret != JAYLINK_OK) {
712 LOG_ERROR("jaylink_get_firmware_version() failed: %s",
713 jaylink_strerror(ret));
715 jaylink_exit(jayctx);
716 return ERROR_JTAG_INIT_FAILED;
717 } else if (length > 0) {
718 LOG_INFO("%s", firmware_version);
719 free(firmware_version);
721 LOG_WARNING("Device responds empty firmware version string");
724 memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
725 ret = jaylink_get_caps(devh, caps);
727 if (ret != JAYLINK_OK) {
728 LOG_ERROR("jaylink_get_caps() failed: %s", jaylink_strerror(ret));
730 jaylink_exit(jayctx);
731 return ERROR_JTAG_INIT_FAILED;
734 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
735 ret = jaylink_get_extended_caps(devh, caps);
737 if (ret != JAYLINK_OK) {
738 LOG_ERROR("jaylink_get_extended_caps() failed: %s",
739 jaylink_strerror(ret));
741 jaylink_exit(jayctx);
742 return ERROR_JTAG_INIT_FAILED;
746 jtag_command_version = JAYLINK_JTAG_VERSION_2;
748 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
749 ret = jaylink_get_hardware_version(devh, &hwver);
751 if (ret != JAYLINK_OK) {
752 LOG_ERROR("Failed to retrieve hardware version: %s",
753 jaylink_strerror(ret));
755 jaylink_exit(jayctx);
756 return ERROR_JTAG_INIT_FAILED;
759 LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
761 if (hwver.major >= 5)
762 jtag_command_version = JAYLINK_JTAG_VERSION_3;
765 if (iface == JAYLINK_TIF_SWD) {
767 * Adjust the SWD transaction buffer size in case there is already
768 * allocated memory on the device. This happens for example if the
769 * memory for SWO capturing is still allocated because the software
770 * which used the device before has not been shut down properly.
772 if (!adjust_swd_buffer_size()) {
774 jaylink_exit(jayctx);
775 return ERROR_JTAG_INIT_FAILED;
779 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
780 if (!read_device_config(&config)) {
781 LOG_ERROR("Failed to read device configuration data");
783 jaylink_exit(jayctx);
784 return ERROR_JTAG_INIT_FAILED;
787 memcpy(&tmp_config, &config, sizeof(struct device_config));
790 ret = jaylink_get_hardware_status(devh, &hwstatus);
792 if (ret != JAYLINK_OK) {
793 LOG_ERROR("jaylink_get_hardware_status() failed: %s",
794 jaylink_strerror(ret));
796 jaylink_exit(jayctx);
797 return ERROR_JTAG_INIT_FAILED;
800 LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
801 hwstatus.target_voltage % 1000);
805 strcpy(conn.hid, "0.0.0.0");
809 ret = jlink_register();
811 if (ret != ERROR_OK) {
813 jaylink_exit(jayctx);
814 return ERROR_JTAG_INIT_FAILED;
817 ret = select_interface();
819 if (ret != ERROR_OK) {
821 jaylink_exit(jayctx);
829 jlink_speed(adapter_get_speed_khz());
831 if (iface == JAYLINK_TIF_JTAG) {
833 * J-Link devices with firmware version v5 and v6 seems to have an issue
834 * if the first tap move is not divisible by 8, so we send a TLR on
838 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
846 static int jlink_quit(void)
852 ret = jaylink_swo_stop(devh);
854 if (ret != JAYLINK_OK)
855 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
858 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
859 ret = jaylink_unregister(devh, &conn, connlist, &count);
861 if (ret != JAYLINK_OK)
862 LOG_ERROR("jaylink_unregister() failed: %s",
863 jaylink_strerror(ret));
867 jaylink_exit(jayctx);
872 /***************************************************************************/
873 /* Queue command implementations */
875 static void jlink_end_state(tap_state_t state)
877 if (tap_is_state_stable(state))
878 tap_set_end_state(state);
880 LOG_ERROR("BUG: %i is not a valid end state", state);
885 /* Goes to the end state. */
886 static void jlink_state_move(void)
889 uint8_t tms_scan_bits;
891 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
892 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
894 jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
896 tap_set_state(tap_get_end_state());
899 static void jlink_path_move(int num_states, tap_state_t *path)
904 for (i = 0; i < num_states; i++) {
905 if (path[i] == tap_state_transition(tap_get_state(), false))
906 jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
907 else if (path[i] == tap_state_transition(tap_get_state(), true))
908 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
910 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
911 tap_state_name(tap_get_state()), tap_state_name(path[i]));
915 tap_set_state(path[i]);
918 tap_set_end_state(tap_get_state());
921 static void jlink_stableclocks(int num_cycles)
925 uint8_t tms = tap_get_state() == TAP_RESET;
926 /* Execute num_cycles. */
927 for (i = 0; i < num_cycles; i++)
928 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
931 static void jlink_runtest(int num_cycles)
933 tap_state_t saved_end_state = tap_get_end_state();
935 /* Only do a state_move when we're not already in IDLE. */
936 if (tap_get_state() != TAP_IDLE) {
937 jlink_end_state(TAP_IDLE);
942 jlink_stableclocks(num_cycles);
944 /* Finish in end_state. */
945 jlink_end_state(saved_end_state);
947 if (tap_get_state() != tap_get_end_state())
951 static void jlink_reset(int trst, int srst)
953 LOG_DEBUG("TRST: %i, SRST: %i", trst, srst);
955 /* Signals are active low. */
957 jaylink_set_reset(devh);
960 jaylink_clear_reset(devh);
963 jaylink_jtag_clear_trst(devh);
966 jaylink_jtag_set_trst(devh);
969 static int jlink_reset_safe(int trst, int srst)
972 jlink_reset(trst, srst);
973 return jlink_flush();
976 COMMAND_HANDLER(jlink_usb_command)
981 command_print(CMD, "Need exactly one argument for jlink usb");
982 return ERROR_COMMAND_SYNTAX_ERROR;
985 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
986 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
990 if (tmp < JAYLINK_USB_ADDRESS_0 || tmp > JAYLINK_USB_ADDRESS_3) {
991 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
997 use_usb_address = true;
1002 COMMAND_HANDLER(jlink_handle_hwstatus_command)
1005 struct jaylink_hardware_status status;
1007 ret = jaylink_get_hardware_status(devh, &status);
1009 if (ret != JAYLINK_OK) {
1010 command_print(CMD, "jaylink_get_hardware_status() failed: %s",
1011 jaylink_strerror(ret));
1015 command_print(CMD, "VTarget = %u.%03u V",
1016 status.target_voltage / 1000, status.target_voltage % 1000);
1018 command_print(CMD, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1019 "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
1020 status.tres, status.trst);
1022 if (status.target_voltage < 1500)
1023 command_print(CMD, "Target voltage too low. Check target power");
1028 COMMAND_HANDLER(jlink_handle_free_memory_command)
1033 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
1034 command_print(CMD, "Retrieval of free memory is not supported by "
1039 ret = jaylink_get_free_memory(devh, &tmp);
1041 if (ret != JAYLINK_OK) {
1042 command_print(CMD, "jaylink_get_free_memory() failed: %s",
1043 jaylink_strerror(ret));
1047 command_print(CMD, "Device has %" PRIu32 " bytes of free memory", tmp);
1052 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1058 switch (jtag_command_version) {
1059 case JAYLINK_JTAG_VERSION_2:
1062 case JAYLINK_JTAG_VERSION_3:
1069 command_print(CMD, "JTAG command version: %i", version);
1070 } else if (CMD_ARGC == 1) {
1071 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
1072 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1073 return ERROR_COMMAND_SYNTAX_ERROR;
1078 jtag_command_version = JAYLINK_JTAG_VERSION_2;
1081 jtag_command_version = JAYLINK_JTAG_VERSION_3;
1084 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1085 return ERROR_COMMAND_SYNTAX_ERROR;
1088 command_print(CMD, "Need exactly one argument for jlink jtag");
1089 return ERROR_COMMAND_SYNTAX_ERROR;
1095 COMMAND_HANDLER(jlink_handle_target_power_command)
1100 if (CMD_ARGC != 1) {
1101 command_print(CMD, "Need exactly one argument for jlink targetpower");
1102 return ERROR_COMMAND_SYNTAX_ERROR;
1105 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1106 command_print(CMD, "Target power supply is not supported by the "
1111 if (!strcmp(CMD_ARGV[0], "on")) {
1113 } else if (!strcmp(CMD_ARGV[0], "off")) {
1116 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1120 ret = jaylink_set_target_power(devh, enable);
1122 if (ret != JAYLINK_OK) {
1123 command_print(CMD, "jaylink_set_target_power() failed: %s",
1124 jaylink_strerror(ret));
1131 static void show_config_usb_address(struct command_invocation *cmd)
1133 if (config.usb_address != tmp_config.usb_address)
1134 command_print(cmd, "USB address: %u [%u]", config.usb_address,
1135 tmp_config.usb_address);
1137 command_print(cmd, "USB address: %u", config.usb_address);
1140 static void show_config_ip_address(struct command_invocation *cmd)
1142 if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1143 command_print(cmd, "IP address: %d.%d.%d.%d",
1144 config.ip_address[3], config.ip_address[2],
1145 config.ip_address[1], config.ip_address[0]);
1147 command_print(cmd, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1148 config.ip_address[3], config.ip_address[2],
1149 config.ip_address[1], config.ip_address[0],
1150 tmp_config.ip_address[3], tmp_config.ip_address[2],
1151 tmp_config.ip_address[1], tmp_config.ip_address[0]);
1153 if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1154 command_print(cmd, "Subnet mask: %d.%d.%d.%d",
1155 config.subnet_mask[3], config.subnet_mask[2],
1156 config.subnet_mask[1], config.subnet_mask[0]);
1158 command_print(cmd, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1159 config.subnet_mask[3], config.subnet_mask[2],
1160 config.subnet_mask[1], config.subnet_mask[0],
1161 tmp_config.subnet_mask[3], tmp_config.subnet_mask[2],
1162 tmp_config.subnet_mask[1], tmp_config.subnet_mask[0]);
1165 static void show_config_mac_address(struct command_invocation *cmd)
1167 if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1168 command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1169 config.mac_address[5], config.mac_address[4],
1170 config.mac_address[3], config.mac_address[2],
1171 config.mac_address[1], config.mac_address[0]);
1173 command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1174 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1175 config.mac_address[5], config.mac_address[4],
1176 config.mac_address[3], config.mac_address[2],
1177 config.mac_address[1], config.mac_address[0],
1178 tmp_config.mac_address[5], tmp_config.mac_address[4],
1179 tmp_config.mac_address[3], tmp_config.mac_address[2],
1180 tmp_config.mac_address[1], tmp_config.mac_address[0]);
1183 static void show_config_target_power(struct command_invocation *cmd)
1185 const char *target_power;
1186 const char *current_target_power;
1188 if (!config.target_power)
1189 target_power = "off";
1191 target_power = "on";
1193 if (!tmp_config.target_power)
1194 current_target_power = "off";
1196 current_target_power = "on";
1198 if (config.target_power != tmp_config.target_power)
1199 command_print(cmd, "Target power supply: %s [%s]", target_power,
1200 current_target_power);
1202 command_print(cmd, "Target power supply: %s", target_power);
1205 static void show_config(struct command_invocation *cmd)
1207 command_print(cmd, "J-Link device configuration:");
1209 show_config_usb_address(cmd);
1211 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1212 show_config_target_power(cmd);
1214 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1215 show_config_ip_address(cmd);
1216 show_config_mac_address(cmd);
1220 static int poll_trace(uint8_t *buf, size_t *size)
1227 ret = jaylink_swo_read(devh, buf, &length);
1229 if (ret != JAYLINK_OK) {
1230 LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret));
1239 static uint32_t calculate_trace_buffer_size(void)
1244 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1247 ret = jaylink_get_free_memory(devh, &tmp);
1249 if (ret != JAYLINK_OK) {
1250 LOG_ERROR("jaylink_get_free_memory() failed: %s",
1251 jaylink_strerror(ret));
1255 if (tmp > 0x3fff || tmp <= 0x600)
1260 return tmp & 0xffffff00;
1263 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
1264 uint32_t trace_freq, uint16_t *prescaler)
1266 unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1267 if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1270 /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1271 unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1272 if (presc * trace_freq < traceclkin_freq - max_deviation ||
1273 presc * trace_freq > traceclkin_freq + max_deviation)
1281 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed,
1282 unsigned int traceclkin_freq, unsigned int *trace_freq,
1283 uint16_t *prescaler)
1289 for (divider = speed.min_div; divider <= speed.max_div; divider++) {
1290 *trace_freq = speed.freq / divider;
1291 presc = ((1.0 - SWO_MAX_FREQ_DEV) * traceclkin_freq) / *trace_freq + 1;
1293 if (presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1296 deviation = fabs(1.0 - ((double)*trace_freq * presc / traceclkin_freq));
1298 if (deviation <= SWO_MAX_FREQ_DEV) {
1307 static int config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1308 uint32_t port_size, unsigned int *trace_freq,
1309 unsigned int traceclkin_freq, uint16_t *prescaler)
1312 uint32_t buffer_size;
1313 struct jaylink_swo_speed speed;
1318 trace_enabled = enabled;
1320 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1324 LOG_ERROR("Trace capturing is not supported by the device");
1328 ret = jaylink_swo_stop(devh);
1330 if (ret != JAYLINK_OK) {
1331 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
1337 * Adjust the SWD transaction buffer size as stopping SWO capturing
1338 * deallocates device internal memory.
1340 if (!adjust_swd_buffer_size())
1346 if (pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART) {
1347 LOG_ERROR("Selected pin protocol is not supported");
1351 buffer_size = calculate_trace_buffer_size();
1354 LOG_ERROR("Not enough free device memory to start trace capturing");
1358 ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1360 if (ret != JAYLINK_OK) {
1361 LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1362 jaylink_strerror(ret));
1366 if (*trace_freq > 0) {
1367 divider = speed.freq / *trace_freq;
1368 min_freq = speed.freq / speed.max_div;
1369 max_freq = speed.freq / speed.min_div;
1371 if (*trace_freq > max_freq) {
1372 LOG_INFO("Given SWO frequency too high, using %" PRIu32 " Hz instead",
1374 *trace_freq = max_freq;
1375 } else if (*trace_freq < min_freq) {
1376 LOG_INFO("Given SWO frequency too low, using %" PRIu32 " Hz instead",
1378 *trace_freq = min_freq;
1379 } else if (*trace_freq != speed.freq / divider) {
1380 *trace_freq = speed.freq / divider;
1382 LOG_INFO("Given SWO frequency is not supported by the device, "
1383 "using %u Hz instead", *trace_freq);
1386 if (!calculate_swo_prescaler(traceclkin_freq, *trace_freq,
1388 LOG_ERROR("SWO frequency is not suitable. Please choose a "
1389 "different frequency or use auto-detection");
1393 LOG_INFO("Trying to auto-detect SWO frequency");
1395 if (!detect_swo_freq_and_prescaler(speed, traceclkin_freq, trace_freq,
1397 LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1398 "could not be achieved", SWO_MAX_FREQ_DEV);
1399 LOG_ERROR("Auto-detection of SWO frequency failed");
1403 LOG_INFO("Using SWO frequency of %u Hz", *trace_freq);
1406 ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1409 if (ret != JAYLINK_OK) {
1410 LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret));
1414 LOG_DEBUG("Using %" PRIu32 " bytes device memory for trace capturing",
1418 * Adjust the SWD transaction buffer size as starting SWO capturing
1419 * allocates device internal memory.
1421 if (!adjust_swd_buffer_size())
1427 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1431 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1432 command_print(CMD, "Reading configuration is not supported by the "
1438 show_config_usb_address(CMD);
1439 } else if (CMD_ARGC == 1) {
1440 if (sscanf(CMD_ARGV[0], "%" SCNd8, &tmp) != 1) {
1441 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
1445 if (tmp > JAYLINK_USB_ADDRESS_3) {
1446 command_print(CMD, "Invalid USB address: %u", tmp);
1450 tmp_config.usb_address = tmp;
1452 command_print(CMD, "Need exactly one argument for jlink config usb");
1453 return ERROR_COMMAND_SYNTAX_ERROR;
1459 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1463 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1464 command_print(CMD, "Reading configuration is not supported by the "
1469 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1470 command_print(CMD, "Target power supply is not supported by the "
1476 show_config_target_power(CMD);
1477 } else if (CMD_ARGC == 1) {
1478 if (!strcmp(CMD_ARGV[0], "on")) {
1480 } else if (!strcmp(CMD_ARGV[0], "off")) {
1483 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1487 tmp_config.target_power = enable;
1489 command_print(CMD, "Need exactly one argument for jlink config "
1491 return ERROR_COMMAND_SYNTAX_ERROR;
1497 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1504 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1505 command_print(CMD, "Reading configuration is not supported by the "
1510 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1511 command_print(CMD, "Ethernet connectivity is not supported by the "
1517 show_config_mac_address(CMD);
1518 } else if (CMD_ARGC == 1) {
1521 if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' ||
1522 str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1523 command_print(CMD, "Invalid MAC address format");
1524 return ERROR_COMMAND_SYNTAX_ERROR;
1527 for (i = 5; i >= 0; i--) {
1528 addr[i] = strtoul(str, &e, 16);
1532 if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1533 command_print(CMD, "Invalid MAC address: zero address");
1534 return ERROR_COMMAND_SYNTAX_ERROR;
1537 if (!(0x01 & addr[0])) {
1538 command_print(CMD, "Invalid MAC address: multicast address");
1539 return ERROR_COMMAND_SYNTAX_ERROR;
1542 memcpy(tmp_config.mac_address, addr, sizeof(addr));
1544 command_print(CMD, "Need exactly one argument for jlink config mac");
1545 return ERROR_COMMAND_SYNTAX_ERROR;
1551 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1555 const char *s_save = s;
1561 for (i = 0; i < 4; i++) {
1562 lip[i] = strtoul(s, &e, 10);
1564 if (*e != '.' && i != 3)
1571 memcpy(ip, lip, sizeof(lip));
1576 static void cpy_ip(uint8_t *dst, uint8_t *src)
1580 for (i = 0, j = 3; i < 4; i++, j--)
1584 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1586 uint8_t ip_address[4];
1587 uint32_t subnet_mask = 0;
1589 uint8_t subnet_bits = 24;
1591 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1592 command_print(CMD, "Reading configuration is not supported by the "
1597 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1598 command_print(CMD, "Ethernet connectivity is not supported by the "
1604 show_config_ip_address(CMD);
1606 if (!string_to_ip(CMD_ARGV[0], ip_address, &i))
1607 return ERROR_COMMAND_SYNTAX_ERROR;
1609 len = strlen(CMD_ARGV[0]);
1611 /* Check for format A.B.C.D/E. */
1613 if (CMD_ARGV[0][i] != '/')
1614 return ERROR_COMMAND_SYNTAX_ERROR;
1616 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1617 } else if (CMD_ARGC > 1) {
1618 if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i))
1619 return ERROR_COMMAND_SYNTAX_ERROR;
1623 subnet_mask = (uint32_t)(subnet_bits < 32 ?
1624 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1626 cpy_ip(tmp_config.ip_address, ip_address);
1627 cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1633 COMMAND_HANDLER(jlink_handle_config_reset_command)
1635 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1638 memcpy(&tmp_config, &config, sizeof(struct device_config));
1644 COMMAND_HANDLER(jlink_handle_config_write_command)
1648 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1649 command_print(CMD, "Reading configuration is not supported by the "
1654 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1655 command_print(CMD, "Writing configuration is not supported by the "
1660 if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1661 command_print(CMD, "Operation not performed due to no changes in "
1662 "the configuration");
1666 ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1668 if (ret != JAYLINK_OK) {
1669 LOG_ERROR("jaylink_write_raw_config() failed: %s",
1670 jaylink_strerror(ret));
1674 if (!read_device_config(&config)) {
1675 LOG_ERROR("Failed to read device configuration for verification");
1679 if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1680 LOG_ERROR("Verification of device configuration failed. Please check "
1685 memcpy(&tmp_config, &config, sizeof(struct device_config));
1686 command_print(CMD, "The new device configuration applies after power "
1687 "cycling the J-Link device");
1692 COMMAND_HANDLER(jlink_handle_config_command)
1694 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1695 command_print(CMD, "Device doesn't support reading configuration");
1705 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1715 return ERROR_COMMAND_SYNTAX_ERROR;
1717 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1718 LOG_ERROR("Device does not support EMUCOM");
1722 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1724 tmp = strlen(CMD_ARGV[1]);
1727 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1728 return ERROR_COMMAND_ARGUMENT_INVALID;
1731 buf = malloc(tmp / 2);
1734 LOG_ERROR("Failed to allocate buffer");
1738 dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1740 if (dummy != (tmp / 2)) {
1741 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1743 return ERROR_COMMAND_ARGUMENT_INVALID;
1747 ret = jaylink_emucom_write(devh, channel, buf, &length);
1751 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1752 LOG_ERROR("Channel not supported by the device");
1754 } else if (ret != JAYLINK_OK) {
1755 LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret));
1759 if (length != (tmp / 2))
1760 LOG_WARNING("Only %" PRIu32 " bytes written to the channel", length);
1765 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1774 return ERROR_COMMAND_SYNTAX_ERROR;
1776 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1777 LOG_ERROR("Device does not support EMUCOM");
1781 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1782 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
1784 buf = malloc(length * 3 + 1);
1787 LOG_ERROR("Failed to allocate buffer");
1791 ret = jaylink_emucom_read(devh, channel, buf, &length);
1793 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1794 LOG_ERROR("Channel is not supported by the device");
1797 } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1798 LOG_ERROR("Channel is not available for the requested amount of data. "
1799 "%" PRIu32 " bytes are available", length);
1802 } else if (ret != JAYLINK_OK) {
1803 LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret));
1808 tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1810 if (tmp != 2 * length) {
1811 LOG_ERROR("Failed to convert data into hexadecimal string");
1816 command_print(CMD, "%s", buf + length);
1822 static const struct command_registration jlink_config_subcommand_handlers[] = {
1825 .handler = &jlink_handle_config_usb_address_command,
1826 .mode = COMMAND_EXEC,
1827 .help = "set the USB address",
1831 .name = "targetpower",
1832 .handler = &jlink_handle_config_target_power_command,
1833 .mode = COMMAND_EXEC,
1834 .help = "set the target power supply",
1839 .handler = &jlink_handle_config_mac_address_command,
1840 .mode = COMMAND_EXEC,
1841 .help = "set the MAC Address",
1842 .usage = "[ff:ff:ff:ff:ff:ff]",
1846 .handler = &jlink_handle_config_ip_address_command,
1847 .mode = COMMAND_EXEC,
1848 .help = "set the IP address, where A.B.C.D is the IP address, "
1849 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1850 .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1854 .handler = &jlink_handle_config_reset_command,
1855 .mode = COMMAND_EXEC,
1856 .help = "undo configuration changes",
1861 .handler = &jlink_handle_config_write_command,
1862 .mode = COMMAND_EXEC,
1863 .help = "write configuration to the device",
1866 COMMAND_REGISTRATION_DONE
1869 static const struct command_registration jlink_emucom_subcommand_handlers[] = {
1872 .handler = &jlink_handle_emucom_write_command,
1873 .mode = COMMAND_EXEC,
1874 .help = "write to a channel",
1875 .usage = "<channel> <data>",
1879 .handler = &jlink_handle_emucom_read_command,
1880 .mode = COMMAND_EXEC,
1881 .help = "read from a channel",
1882 .usage = "<channel> <length>"
1884 COMMAND_REGISTRATION_DONE
1887 static const struct command_registration jlink_subcommand_handlers[] = {
1890 .handler = &jlink_handle_jlink_jtag_command,
1891 .mode = COMMAND_EXEC,
1892 .help = "select the JTAG command version",
1896 .name = "targetpower",
1897 .handler = &jlink_handle_target_power_command,
1898 .mode = COMMAND_EXEC,
1899 .help = "set the target power supply",
1904 .handler = &jlink_handle_free_memory_command,
1905 .mode = COMMAND_EXEC,
1906 .help = "show free device memory",
1911 .handler = &jlink_handle_hwstatus_command,
1912 .mode = COMMAND_EXEC,
1913 .help = "show the hardware status",
1918 .handler = &jlink_usb_command,
1919 .mode = COMMAND_CONFIG,
1920 .help = "set the USB address of the device that should be used",
1925 .handler = &jlink_handle_config_command,
1926 .mode = COMMAND_EXEC,
1927 .help = "access the device configuration. If no argument is given "
1928 "this will show the device configuration",
1929 .chain = jlink_config_subcommand_handlers,
1934 .mode = COMMAND_EXEC,
1935 .help = "access EMUCOM channel",
1936 .chain = jlink_emucom_subcommand_handlers,
1939 COMMAND_REGISTRATION_DONE
1942 static const struct command_registration jlink_command_handlers[] = {
1945 .mode = COMMAND_ANY,
1946 .help = "perform jlink management",
1947 .chain = jlink_subcommand_handlers,
1950 COMMAND_REGISTRATION_DONE
1953 static int jlink_swd_init(void)
1955 iface = JAYLINK_TIF_SWD;
1960 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1962 assert(!(cmd & SWD_CMD_RNW));
1963 jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1966 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1968 assert(cmd & SWD_CMD_RNW);
1969 jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1972 /***************************************************************************/
1973 /* J-Link tap functions */
1975 static unsigned tap_length;
1976 /* In SWD mode use tms buffer for direction control */
1977 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
1978 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
1979 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
1981 struct pending_scan_result {
1982 /** First bit position in tdo_buffer to read. */
1984 /** Number of bits to read. */
1986 /** Location to store the result */
1988 /** Offset in the destination buffer */
1989 unsigned buffer_offset;
1992 #define MAX_PENDING_SCAN_RESULTS 256
1994 static int pending_scan_results_length;
1995 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1997 static void jlink_tap_init(void)
2000 pending_scan_results_length = 0;
2001 memset(tms_buffer, 0, sizeof(tms_buffer));
2002 memset(tdi_buffer, 0, sizeof(tdi_buffer));
2005 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
2006 const uint8_t *tms_out, unsigned tms_offset,
2007 uint8_t *in, unsigned in_offset,
2011 unsigned available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
2013 if (!available_length ||
2014 (in && pending_scan_results_length == MAX_PENDING_SCAN_RESULTS)) {
2015 if (jlink_flush() != ERROR_OK)
2017 available_length = JLINK_TAP_BUFFER_SIZE;
2020 struct pending_scan_result *pending_scan_result =
2021 &pending_scan_results_buffer[pending_scan_results_length];
2023 unsigned scan_length = length > available_length ?
2024 available_length : length;
2027 buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
2029 buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
2032 pending_scan_result->first = tap_length;
2033 pending_scan_result->length = scan_length;
2034 pending_scan_result->buffer = in;
2035 pending_scan_result->buffer_offset = in_offset;
2036 pending_scan_results_length++;
2039 tap_length += scan_length;
2040 out_offset += scan_length;
2041 tms_offset += scan_length;
2042 in_offset += scan_length;
2043 length -= scan_length;
2044 } while (length > 0);
2047 static int jlink_flush(void)
2055 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
2056 tap_length, jlink_last_state);
2058 ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
2059 tap_length, jtag_command_version);
2061 if (ret != JAYLINK_OK) {
2062 LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret));
2064 return ERROR_JTAG_QUEUE_FAILED;
2067 for (i = 0; i < pending_scan_results_length; i++) {
2068 struct pending_scan_result *p = &pending_scan_results_buffer[i];
2070 buf_set_buf(tdo_buffer, p->first, p->buffer,
2071 p->buffer_offset, p->length);
2073 LOG_DEBUG_IO("Pending scan result, length = %d", p->length);
2081 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
2083 unsigned int tap_pos = tap_length;
2086 buf_set_u32(buf, tap_pos, 32, val);
2092 buf_set_u32(buf, tap_pos, len, val);
2095 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2097 const uint32_t dir_out = 0xffffffff;
2100 bit_copy(tdi_buffer, tap_length, data, 0, len);
2102 fill_buffer(tdi_buffer, 0, len);
2104 fill_buffer(tms_buffer, dir_out, len);
2108 static void jlink_queue_data_in(uint32_t len)
2110 const uint32_t dir_in = 0;
2112 fill_buffer(tms_buffer, dir_in, len);
2116 static int jlink_swd_switch_seq(enum swd_special_seq seq)
2123 LOG_DEBUG("SWD line reset");
2124 s = swd_seq_line_reset;
2125 s_len = swd_seq_line_reset_len;
2128 LOG_DEBUG("JTAG-to-SWD");
2129 s = swd_seq_jtag_to_swd;
2130 s_len = swd_seq_jtag_to_swd_len;
2132 case JTAG_TO_DORMANT:
2133 LOG_DEBUG("JTAG-to-DORMANT");
2134 s = swd_seq_jtag_to_dormant;
2135 s_len = swd_seq_jtag_to_dormant_len;
2138 LOG_DEBUG("SWD-to-JTAG");
2139 s = swd_seq_swd_to_jtag;
2140 s_len = swd_seq_swd_to_jtag_len;
2142 case SWD_TO_DORMANT:
2143 LOG_DEBUG("SWD-to-DORMANT");
2144 s = swd_seq_swd_to_dormant;
2145 s_len = swd_seq_swd_to_dormant_len;
2147 case DORMANT_TO_SWD:
2148 LOG_DEBUG("DORMANT-to-SWD");
2149 s = swd_seq_dormant_to_swd;
2150 s_len = swd_seq_dormant_to_swd_len;
2152 case DORMANT_TO_JTAG:
2153 LOG_DEBUG("DORMANT-to-JTAG");
2154 s = swd_seq_dormant_to_jtag;
2155 s_len = swd_seq_dormant_to_jtag_len;
2158 LOG_ERROR("Sequence %d not supported", seq);
2162 jlink_queue_data_out(s, s_len);
2167 static int jlink_swd_run_queue(void)
2172 LOG_DEBUG("Executing %d queued transactions", pending_scan_results_length);
2174 if (queued_retval != ERROR_OK) {
2175 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
2180 * A transaction must be followed by another transaction or at least 8 idle
2181 * cycles to ensure that data is clocked through the AP.
2183 jlink_queue_data_out(NULL, 8);
2185 ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2187 if (ret != JAYLINK_OK) {
2188 LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret));
2192 for (i = 0; i < pending_scan_results_length; i++) {
2193 int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
2195 if (ack != SWD_ACK_OK) {
2196 LOG_DEBUG("SWD ack not OK: %d %s", ack,
2197 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2198 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
2200 } else if (pending_scan_results_buffer[i].length) {
2201 uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2202 int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
2204 if (parity != parity_u32(data)) {
2205 LOG_ERROR("SWD: Read data parity mismatch");
2206 queued_retval = ERROR_FAIL;
2210 if (pending_scan_results_buffer[i].buffer)
2211 *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2217 ret = queued_retval;
2218 queued_retval = ERROR_OK;
2223 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2225 uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2226 if (tap_length + 46 + 8 + ap_delay_clk >= swd_buffer_size * 8 ||
2227 pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
2228 /* Not enough room in the queue. Run the queue. */
2229 queued_retval = jlink_swd_run_queue();
2232 if (queued_retval != ERROR_OK)
2235 cmd |= SWD_CMD_START | SWD_CMD_PARK;
2237 jlink_queue_data_out(&cmd, 8);
2239 pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
2241 if (cmd & SWD_CMD_RNW) {
2242 /* Queue a read transaction. */
2243 pending_scan_results_buffer[pending_scan_results_length].length = 32;
2244 pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
2246 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2248 /* Queue a write transaction. */
2249 pending_scan_results_buffer[pending_scan_results_length].length = 0;
2250 jlink_queue_data_in(1 + 3 + 1);
2252 buf_set_u32(data_parity_trn, 0, 32, data);
2253 buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2255 jlink_queue_data_out(data_parity_trn, 32 + 1);
2258 pending_scan_results_length++;
2260 /* Insert idle cycles after AP accesses to avoid WAIT. */
2261 if (cmd & SWD_CMD_APNDP)
2262 jlink_queue_data_out(NULL, ap_delay_clk);
2265 static const struct swd_driver jlink_swd = {
2266 .init = &jlink_swd_init,
2267 .switch_seq = &jlink_swd_switch_seq,
2268 .read_reg = &jlink_swd_read_reg,
2269 .write_reg = &jlink_swd_write_reg,
2270 .run = &jlink_swd_run_queue,
2273 static const char * const jlink_transports[] = { "jtag", "swd", NULL };
2275 static struct jtag_interface jlink_interface = {
2276 .execute_queue = &jlink_execute_queue,
2279 struct adapter_driver jlink_adapter_driver = {
2281 .transports = jlink_transports,
2282 .commands = jlink_command_handlers,
2284 .init = &jlink_init,
2285 .quit = &jlink_quit,
2286 .reset = &jlink_reset_safe,
2287 .speed = &jlink_speed,
2289 .speed_div = &jlink_speed_div,
2290 .config_trace = &config_trace,
2291 .poll_trace = &poll_trace,
2293 .jtag_ops = &jlink_interface,
2294 .swd_ops = &jlink_swd,