Remove other '_s' suffix from structs
[openocd.git] / src / jtag / drivers / jlink.c
blob1874557dcd5a7a8fc1483ba49f8fe990afa3134e
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
5 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
6 * *
7 * Copyright (C) 2008 by Spencer Oliver *
8 * spen@spen-soft.co.uk *
9 * *
10 * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
11 * plagnioj@jcrosoft.com *
12 * *
13 * Copyright (C) 2015 by Marc Schink *
14 * openocd-dev@marcschink.de *
15 * *
16 * Copyright (C) 2015 by Paul Fertser *
17 * fercerpav@gmail.com *
18 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <stdint.h>
25 #include <math.h>
27 #include <jtag/interface.h>
28 #include <jtag/swd.h>
29 #include <jtag/commands.h>
30 #include <jtag/adapter.h>
31 #include <helper/replacements.h>
32 #include <target/cortex_m.h>
34 #include <libjaylink/libjaylink.h>
36 static struct jaylink_context *jayctx;
37 static struct jaylink_device_handle *devh;
38 static struct jaylink_connection conn;
39 static struct jaylink_connection connlist[JAYLINK_MAX_CONNECTIONS];
40 static enum jaylink_jtag_version jtag_command_version;
41 static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
43 static uint32_t serial_number;
44 static bool use_serial_number;
45 static bool use_usb_location;
46 static enum jaylink_usb_address usb_address;
47 static bool use_usb_address;
48 static enum jaylink_target_interface iface = JAYLINK_TIF_JTAG;
49 static bool trace_enabled;
51 #define JLINK_MAX_SPEED 12000
52 #define JLINK_TAP_BUFFER_SIZE 2048
54 static unsigned int swd_buffer_size = JLINK_TAP_BUFFER_SIZE;
56 /* Maximum SWO frequency deviation. */
57 #define SWO_MAX_FREQ_DEV 0.03
59 /* 256 byte non-volatile memory */
60 struct device_config {
61 uint8_t usb_address;
62 /* 0ffset 0x01 to 0x03 */
63 uint8_t reserved_1[3];
64 uint32_t target_power;
65 /* 0ffset 0x08 to 0x1f */
66 uint8_t reserved_2[24];
67 /* IP only for J-Link Pro */
68 uint8_t ip_address[4];
69 uint8_t subnet_mask[4];
70 /* 0ffset 0x28 to 0x2f */
71 uint8_t reserved_3[8];
72 uint8_t mac_address[6];
73 /* 0ffset 0x36 to 0xff */
74 uint8_t reserved_4[202];
75 } __attribute__ ((packed));
77 static struct device_config config;
78 static struct device_config tmp_config;
80 /* Queue command functions */
81 static void jlink_end_state(tap_state_t state);
82 static void jlink_state_move(void);
83 static void jlink_path_move(int num_states, tap_state_t *path);
84 static void jlink_stableclocks(int num_cycles);
85 static void jlink_runtest(int num_cycles);
86 static void jlink_reset(int trst, int srst);
87 static int jlink_reset_safe(int trst, int srst);
88 static int jlink_swd_run_queue(void);
89 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk);
90 static int jlink_swd_switch_seq(enum swd_special_seq seq);
92 /* J-Link tap buffer functions */
93 static void jlink_tap_init(void);
94 static int jlink_flush(void);
95 /**
96 * Queue data to go out and in, flushing the queue as many times as
97 * necessary.
99 * @param out A pointer to TDI data, if NULL, old stale data will be used.
100 * @param out_offset A bit offset for TDI data.
101 * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
102 * @param tms_offset A bit offset for TMS data.
103 * @param in A pointer to store TDO data to, if NULL the data will be discarded.
104 * @param in_offset A bit offset for TDO data.
105 * @param length Amount of bits to transfer out and in.
107 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
108 const uint8_t *tms_out, unsigned tms_offset,
109 uint8_t *in, unsigned in_offset,
110 unsigned length);
112 static enum tap_state jlink_last_state = TAP_RESET;
113 static int queued_retval;
115 /***************************************************************************/
116 /* External interface implementation */
118 static void jlink_execute_stableclocks(struct jtag_command *cmd)
120 LOG_DEBUG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
121 jlink_stableclocks(cmd->cmd.runtest->num_cycles);
124 static void jlink_execute_runtest(struct jtag_command *cmd)
126 LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
127 cmd->cmd.runtest->end_state);
129 jlink_end_state(cmd->cmd.runtest->end_state);
130 jlink_runtest(cmd->cmd.runtest->num_cycles);
133 static void jlink_execute_statemove(struct jtag_command *cmd)
135 LOG_DEBUG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
137 jlink_end_state(cmd->cmd.statemove->end_state);
138 jlink_state_move();
141 static void jlink_execute_pathmove(struct jtag_command *cmd)
143 LOG_DEBUG_IO("pathmove: %i states, end in %i",
144 cmd->cmd.pathmove->num_states,
145 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
147 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
150 static void jlink_execute_scan(struct jtag_command *cmd)
152 LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
153 jtag_scan_type(cmd->cmd.scan));
155 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
156 while (cmd->cmd.scan->num_fields > 0
157 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
158 cmd->cmd.scan->num_fields--;
159 LOG_DEBUG("discarding trailing empty field");
162 if (cmd->cmd.scan->num_fields == 0) {
163 LOG_DEBUG("empty scan, doing nothing");
164 return;
167 if (cmd->cmd.scan->ir_scan) {
168 if (tap_get_state() != TAP_IRSHIFT) {
169 jlink_end_state(TAP_IRSHIFT);
170 jlink_state_move();
172 } else {
173 if (tap_get_state() != TAP_DRSHIFT) {
174 jlink_end_state(TAP_DRSHIFT);
175 jlink_state_move();
179 jlink_end_state(cmd->cmd.scan->end_state);
181 struct scan_field *field = cmd->cmd.scan->fields;
182 unsigned scan_size = 0;
184 for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
185 scan_size += field->num_bits;
186 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
187 field->in_value ? "in" : "",
188 field->out_value ? "out" : "",
190 cmd->cmd.scan->num_fields,
191 field->num_bits);
193 if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
194 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
195 * movement. This last field can't have length zero, it was checked above. */
196 jlink_clock_data(field->out_value,
198 NULL,
200 field->in_value,
202 field->num_bits - 1);
203 uint8_t last_bit = 0;
204 if (field->out_value)
205 bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
206 uint8_t tms_bits = 0x01;
207 jlink_clock_data(&last_bit,
209 &tms_bits,
211 field->in_value,
212 field->num_bits - 1,
214 tap_set_state(tap_state_transition(tap_get_state(), 1));
215 jlink_clock_data(NULL,
217 &tms_bits,
219 NULL,
222 tap_set_state(tap_state_transition(tap_get_state(), 0));
223 } else
224 jlink_clock_data(field->out_value,
226 NULL,
228 field->in_value,
230 field->num_bits);
233 if (tap_get_state() != tap_get_end_state()) {
234 jlink_end_state(tap_get_end_state());
235 jlink_state_move();
238 LOG_DEBUG_IO("%s scan, %i bits, end in %s",
239 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
240 tap_state_name(tap_get_end_state()));
243 static void jlink_execute_sleep(struct jtag_command *cmd)
245 LOG_DEBUG_IO("sleep %" PRIu32 "", cmd->cmd.sleep->us);
246 jlink_flush();
247 jtag_sleep(cmd->cmd.sleep->us);
250 static int jlink_execute_command(struct jtag_command *cmd)
252 switch (cmd->type) {
253 case JTAG_STABLECLOCKS:
254 jlink_execute_stableclocks(cmd);
255 break;
256 case JTAG_RUNTEST:
257 jlink_execute_runtest(cmd);
258 break;
259 case JTAG_TLR_RESET:
260 jlink_execute_statemove(cmd);
261 break;
262 case JTAG_PATHMOVE:
263 jlink_execute_pathmove(cmd);
264 break;
265 case JTAG_SCAN:
266 jlink_execute_scan(cmd);
267 break;
268 case JTAG_SLEEP:
269 jlink_execute_sleep(cmd);
270 break;
271 default:
272 LOG_ERROR("BUG: Unknown JTAG command type encountered");
273 return ERROR_JTAG_QUEUE_FAILED;
276 return ERROR_OK;
279 static int jlink_execute_queue(struct jtag_command *cmd_queue)
281 int ret;
282 struct jtag_command *cmd = cmd_queue;
284 while (cmd) {
285 ret = jlink_execute_command(cmd);
287 if (ret != ERROR_OK)
288 return ret;
290 cmd = cmd->next;
293 return jlink_flush();
296 static int jlink_speed(int speed)
298 int ret;
299 struct jaylink_speed tmp;
300 int max_speed;
302 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
303 ret = jaylink_get_speeds(devh, &tmp);
305 if (ret != JAYLINK_OK) {
306 LOG_ERROR("jaylink_get_speeds() failed: %s",
307 jaylink_strerror(ret));
308 return ERROR_JTAG_DEVICE_ERROR;
311 tmp.freq /= 1000;
312 max_speed = tmp.freq / tmp.div;
313 } else {
314 max_speed = JLINK_MAX_SPEED;
317 if (!speed) {
318 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
319 LOG_ERROR("Adaptive clocking is not supported by the device");
320 return ERROR_JTAG_NOT_IMPLEMENTED;
323 speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
324 } else if (speed > max_speed) {
325 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum)", speed,
326 max_speed);
327 speed = max_speed;
330 ret = jaylink_set_speed(devh, speed);
332 if (ret != JAYLINK_OK) {
333 LOG_ERROR("jaylink_set_speed() failed: %s",
334 jaylink_strerror(ret));
335 return ERROR_JTAG_DEVICE_ERROR;
338 return ERROR_OK;
341 static int jlink_speed_div(int speed, int *khz)
343 *khz = speed;
345 return ERROR_OK;
348 static int jlink_khz(int khz, int *jtag_speed)
350 *jtag_speed = khz;
352 return ERROR_OK;
355 static bool read_device_config(struct device_config *cfg)
357 int ret;
359 ret = jaylink_read_raw_config(devh, (uint8_t *)cfg);
361 if (ret != JAYLINK_OK) {
362 LOG_ERROR("jaylink_read_raw_config() failed: %s",
363 jaylink_strerror(ret));
364 return false;
367 if (cfg->usb_address == 0xff)
368 cfg->usb_address = 0x00;
370 if (cfg->target_power == 0xffffffff)
371 cfg->target_power = 0;
373 return true;
376 static int select_interface(void)
378 int ret;
379 uint32_t interfaces;
381 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
382 if (iface != JAYLINK_TIF_JTAG) {
383 LOG_ERROR("Device supports JTAG transport only");
384 return ERROR_JTAG_INIT_FAILED;
387 return ERROR_OK;
390 ret = jaylink_get_available_interfaces(devh, &interfaces);
392 if (ret != JAYLINK_OK) {
393 LOG_ERROR("jaylink_get_available_interfaces() failed: %s",
394 jaylink_strerror(ret));
395 return ERROR_JTAG_INIT_FAILED;
398 if (!(interfaces & (1 << iface))) {
399 LOG_ERROR("Selected transport is not supported by the device");
400 return ERROR_JTAG_INIT_FAILED;
403 ret = jaylink_select_interface(devh, iface, NULL);
405 if (ret < 0) {
406 LOG_ERROR("jaylink_select_interface() failed: %s",
407 jaylink_strerror(ret));
408 return ERROR_JTAG_INIT_FAILED;
411 return ERROR_OK;
414 static int jlink_register(void)
416 int ret;
417 size_t i;
418 bool handle_found;
419 size_t count;
421 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
422 return ERROR_OK;
424 ret = jaylink_register(devh, &conn, connlist, &count);
426 if (ret != JAYLINK_OK) {
427 LOG_ERROR("jaylink_register() failed: %s", jaylink_strerror(ret));
428 return ERROR_FAIL;
431 handle_found = false;
433 for (i = 0; i < count; i++) {
434 if (connlist[i].handle == conn.handle) {
435 handle_found = true;
436 break;
440 if (!handle_found) {
441 LOG_ERROR("Registration failed: maximum number of connections on the "
442 "device reached");
443 return ERROR_FAIL;
446 return ERROR_OK;
450 * Adjust the SWD transaction buffer size depending on the free device internal
451 * memory. This ensures that the SWD transactions sent to the device do not
452 * exceed the internal memory of the device.
454 static bool adjust_swd_buffer_size(void)
456 int ret;
457 uint32_t tmp;
459 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
460 return true;
462 ret = jaylink_get_free_memory(devh, &tmp);
464 if (ret != JAYLINK_OK) {
465 LOG_ERROR("jaylink_get_free_memory() failed: %s",
466 jaylink_strerror(ret));
467 return false;
470 if (tmp < 143) {
471 LOG_ERROR("Not enough free device internal memory: %" PRIu32 " bytes", tmp);
472 return false;
475 tmp = MIN(JLINK_TAP_BUFFER_SIZE, (tmp - 16) / 2);
477 if (tmp != swd_buffer_size) {
478 swd_buffer_size = tmp;
479 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes",
480 swd_buffer_size);
483 return true;
486 static int jaylink_log_handler(const struct jaylink_context *ctx,
487 enum jaylink_log_level level, const char *format, va_list args,
488 void *user_data)
490 enum log_levels tmp;
492 switch (level) {
493 case JAYLINK_LOG_LEVEL_ERROR:
494 tmp = LOG_LVL_ERROR;
495 break;
496 case JAYLINK_LOG_LEVEL_WARNING:
497 tmp = LOG_LVL_WARNING;
498 break;
500 * Forward info messages to the debug output because they are more verbose
501 * than info messages of OpenOCD.
503 case JAYLINK_LOG_LEVEL_INFO:
504 case JAYLINK_LOG_LEVEL_DEBUG:
505 tmp = LOG_LVL_DEBUG;
506 break;
507 case JAYLINK_LOG_LEVEL_DEBUG_IO:
508 tmp = LOG_LVL_DEBUG_IO;
509 break;
510 default:
511 tmp = LOG_LVL_WARNING;
514 log_vprintf_lf(tmp, __FILE__, __LINE__, __func__, format, args);
516 return 0;
519 static bool jlink_usb_location_equal(struct jaylink_device *dev)
521 int retval;
522 uint8_t bus;
523 uint8_t *ports;
524 size_t num_ports;
525 bool equal = false;
527 retval = jaylink_device_get_usb_bus_ports(dev, &bus, &ports, &num_ports);
529 if (retval == JAYLINK_ERR_NOT_SUPPORTED) {
530 return false;
531 } else if (retval != JAYLINK_OK) {
532 LOG_WARNING("jaylink_device_get_usb_bus_ports() failed: %s",
533 jaylink_strerror(retval));
534 return false;
537 equal = adapter_usb_location_equal(bus, ports, num_ports);
538 free(ports);
540 return equal;
544 static int jlink_open_device(uint32_t ifaces, bool *found_device)
546 int ret = jaylink_discovery_scan(jayctx, ifaces);
547 if (ret != JAYLINK_OK) {
548 LOG_ERROR("jaylink_discovery_scan() failed: %s", jaylink_strerror(ret));
549 jaylink_exit(jayctx);
550 return ERROR_JTAG_INIT_FAILED;
553 size_t num_devices;
554 struct jaylink_device **devs;
555 ret = jaylink_get_devices(jayctx, &devs, &num_devices);
557 if (ret != JAYLINK_OK) {
558 LOG_ERROR("jaylink_get_devices() failed: %s", jaylink_strerror(ret));
559 jaylink_exit(jayctx);
560 return ERROR_JTAG_INIT_FAILED;
563 use_usb_location = !!adapter_usb_get_location();
565 if (!use_serial_number && !use_usb_address && !use_usb_location && num_devices > 1) {
566 LOG_ERROR("Multiple devices found, specify the desired device");
567 jaylink_free_devices(devs, true);
568 jaylink_exit(jayctx);
569 return ERROR_JTAG_INIT_FAILED;
572 *found_device = false;
574 for (size_t i = 0; devs[i]; i++) {
575 struct jaylink_device *dev = devs[i];
577 if (use_serial_number) {
578 uint32_t tmp;
579 ret = jaylink_device_get_serial_number(dev, &tmp);
581 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
582 continue;
583 } else if (ret != JAYLINK_OK) {
584 LOG_WARNING("jaylink_device_get_serial_number() failed: %s",
585 jaylink_strerror(ret));
586 continue;
589 if (serial_number != tmp)
590 continue;
593 if (use_usb_address) {
594 enum jaylink_usb_address address;
595 ret = jaylink_device_get_usb_address(dev, &address);
597 if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
598 continue;
599 } else if (ret != JAYLINK_OK) {
600 LOG_WARNING("jaylink_device_get_usb_address() failed: %s",
601 jaylink_strerror(ret));
602 continue;
605 if (usb_address != address)
606 continue;
609 if (use_usb_location && !jlink_usb_location_equal(dev))
610 continue;
612 ret = jaylink_open(dev, &devh);
614 if (ret == JAYLINK_OK) {
615 *found_device = true;
616 break;
619 LOG_ERROR("Failed to open device: %s", jaylink_strerror(ret));
622 jaylink_free_devices(devs, true);
623 return ERROR_OK;
627 static int jlink_init(void)
629 int ret;
630 char *firmware_version;
631 struct jaylink_hardware_version hwver;
632 struct jaylink_hardware_status hwstatus;
633 size_t length;
635 LOG_DEBUG("Using libjaylink %s (compiled with %s)",
636 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
638 if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB) && use_usb_address) {
639 LOG_ERROR("J-Link driver does not support USB devices");
640 return ERROR_JTAG_INIT_FAILED;
643 ret = jaylink_init(&jayctx);
645 if (ret != JAYLINK_OK) {
646 LOG_ERROR("jaylink_init() failed: %s", jaylink_strerror(ret));
647 return ERROR_JTAG_INIT_FAILED;
650 ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
652 if (ret != JAYLINK_OK) {
653 LOG_ERROR("jaylink_log_set_callback() failed: %s",
654 jaylink_strerror(ret));
655 jaylink_exit(jayctx);
656 return ERROR_JTAG_INIT_FAILED;
659 const char *serial = adapter_get_required_serial();
660 if (serial) {
661 ret = jaylink_parse_serial_number(serial, &serial_number);
662 if (ret == JAYLINK_ERR) {
663 LOG_ERROR("Invalid serial number: %s", serial);
664 jaylink_exit(jayctx);
665 return ERROR_JTAG_INIT_FAILED;
667 if (ret != JAYLINK_OK) {
668 LOG_ERROR("jaylink_parse_serial_number() failed: %s", jaylink_strerror(ret));
669 jaylink_exit(jayctx);
670 return ERROR_JTAG_INIT_FAILED;
672 use_serial_number = true;
673 use_usb_address = false;
676 bool found_device;
677 ret = jlink_open_device(JAYLINK_HIF_USB, &found_device);
678 if (ret != ERROR_OK)
679 return ret;
681 if (!found_device && use_serial_number) {
682 ret = jlink_open_device(JAYLINK_HIF_TCP, &found_device);
683 if (ret != ERROR_OK)
684 return ret;
687 if (!found_device) {
688 LOG_ERROR("No J-Link device found");
689 jaylink_exit(jayctx);
690 return ERROR_JTAG_INIT_FAILED;
694 * Be careful with changing the following initialization sequence because
695 * some devices are known to be sensitive regarding the order.
698 ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
700 if (ret != JAYLINK_OK) {
701 LOG_ERROR("jaylink_get_firmware_version() failed: %s",
702 jaylink_strerror(ret));
703 jaylink_close(devh);
704 jaylink_exit(jayctx);
705 return ERROR_JTAG_INIT_FAILED;
706 } else if (length > 0) {
707 LOG_INFO("%s", firmware_version);
708 free(firmware_version);
709 } else {
710 LOG_WARNING("Device responds empty firmware version string");
713 memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
714 ret = jaylink_get_caps(devh, caps);
716 if (ret != JAYLINK_OK) {
717 LOG_ERROR("jaylink_get_caps() failed: %s", jaylink_strerror(ret));
718 jaylink_close(devh);
719 jaylink_exit(jayctx);
720 return ERROR_JTAG_INIT_FAILED;
723 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
724 ret = jaylink_get_extended_caps(devh, caps);
726 if (ret != JAYLINK_OK) {
727 LOG_ERROR("jaylink_get_extended_caps() failed: %s",
728 jaylink_strerror(ret));
729 jaylink_close(devh);
730 jaylink_exit(jayctx);
731 return ERROR_JTAG_INIT_FAILED;
735 jtag_command_version = JAYLINK_JTAG_VERSION_2;
737 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
738 ret = jaylink_get_hardware_version(devh, &hwver);
740 if (ret != JAYLINK_OK) {
741 LOG_ERROR("Failed to retrieve hardware version: %s",
742 jaylink_strerror(ret));
743 jaylink_close(devh);
744 jaylink_exit(jayctx);
745 return ERROR_JTAG_INIT_FAILED;
748 LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
750 if (hwver.major >= 5)
751 jtag_command_version = JAYLINK_JTAG_VERSION_3;
754 if (iface == JAYLINK_TIF_SWD) {
756 * Adjust the SWD transaction buffer size in case there is already
757 * allocated memory on the device. This happens for example if the
758 * memory for SWO capturing is still allocated because the software
759 * which used the device before has not been shut down properly.
761 if (!adjust_swd_buffer_size()) {
762 jaylink_close(devh);
763 jaylink_exit(jayctx);
764 return ERROR_JTAG_INIT_FAILED;
768 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
769 if (!read_device_config(&config)) {
770 LOG_ERROR("Failed to read device configuration data");
771 jaylink_close(devh);
772 jaylink_exit(jayctx);
773 return ERROR_JTAG_INIT_FAILED;
776 memcpy(&tmp_config, &config, sizeof(struct device_config));
779 ret = jaylink_get_hardware_status(devh, &hwstatus);
781 if (ret != JAYLINK_OK) {
782 LOG_ERROR("jaylink_get_hardware_status() failed: %s",
783 jaylink_strerror(ret));
784 jaylink_close(devh);
785 jaylink_exit(jayctx);
786 return ERROR_JTAG_INIT_FAILED;
789 LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
790 hwstatus.target_voltage % 1000);
792 conn.handle = 0;
793 conn.pid = 0;
794 strcpy(conn.hid, "0.0.0.0");
795 conn.iid = 0;
796 conn.cid = 0;
798 ret = jlink_register();
800 if (ret != ERROR_OK) {
801 jaylink_close(devh);
802 jaylink_exit(jayctx);
803 return ERROR_JTAG_INIT_FAILED;
806 ret = select_interface();
808 if (ret != ERROR_OK) {
809 jaylink_close(devh);
810 jaylink_exit(jayctx);
811 return ret;
814 jlink_reset(0, 0);
815 jtag_sleep(3000);
816 jlink_tap_init();
818 jlink_speed(adapter_get_speed_khz());
820 if (iface == JAYLINK_TIF_JTAG) {
822 * J-Link devices with firmware version v5 and v6 seems to have an issue
823 * if the first tap move is not divisible by 8, so we send a TLR on
824 * first power up.
826 uint8_t tms = 0xff;
827 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
829 jlink_flush();
832 return ERROR_OK;
835 static int jlink_quit(void)
837 int ret;
838 size_t count;
840 if (trace_enabled) {
841 ret = jaylink_swo_stop(devh);
843 if (ret != JAYLINK_OK)
844 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
847 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
848 ret = jaylink_unregister(devh, &conn, connlist, &count);
850 if (ret != JAYLINK_OK)
851 LOG_ERROR("jaylink_unregister() failed: %s",
852 jaylink_strerror(ret));
855 jaylink_close(devh);
856 jaylink_exit(jayctx);
858 return ERROR_OK;
861 /***************************************************************************/
862 /* Queue command implementations */
864 static void jlink_end_state(tap_state_t state)
866 if (tap_is_state_stable(state))
867 tap_set_end_state(state);
868 else {
869 LOG_ERROR("BUG: %i is not a valid end state", state);
870 exit(-1);
874 /* Goes to the end state. */
875 static void jlink_state_move(void)
877 uint8_t tms_scan;
878 uint8_t tms_scan_bits;
880 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
881 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
883 jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
885 tap_set_state(tap_get_end_state());
888 static void jlink_path_move(int num_states, tap_state_t *path)
890 int i;
891 uint8_t tms = 0xff;
893 for (i = 0; i < num_states; i++) {
894 if (path[i] == tap_state_transition(tap_get_state(), false))
895 jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
896 else if (path[i] == tap_state_transition(tap_get_state(), true))
897 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
898 else {
899 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
900 tap_state_name(tap_get_state()), tap_state_name(path[i]));
901 exit(-1);
904 tap_set_state(path[i]);
907 tap_set_end_state(tap_get_state());
910 static void jlink_stableclocks(int num_cycles)
912 int i;
914 uint8_t tms = tap_get_state() == TAP_RESET;
915 /* Execute num_cycles. */
916 for (i = 0; i < num_cycles; i++)
917 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
920 static void jlink_runtest(int num_cycles)
922 tap_state_t saved_end_state = tap_get_end_state();
924 /* Only do a state_move when we're not already in IDLE. */
925 if (tap_get_state() != TAP_IDLE) {
926 jlink_end_state(TAP_IDLE);
927 jlink_state_move();
928 /* num_cycles--; */
931 jlink_stableclocks(num_cycles);
933 /* Finish in end_state. */
934 jlink_end_state(saved_end_state);
936 if (tap_get_state() != tap_get_end_state())
937 jlink_state_move();
940 static void jlink_reset(int trst, int srst)
942 LOG_DEBUG("TRST: %i, SRST: %i", trst, srst);
944 /* Signals are active low. */
945 if (srst == 0)
946 jaylink_set_reset(devh);
948 if (srst == 1)
949 jaylink_clear_reset(devh);
951 if (trst == 1)
952 jaylink_jtag_clear_trst(devh);
954 if (trst == 0)
955 jaylink_jtag_set_trst(devh);
958 static int jlink_reset_safe(int trst, int srst)
960 jlink_flush();
961 jlink_reset(trst, srst);
962 return jlink_flush();
965 COMMAND_HANDLER(jlink_usb_command)
967 int tmp;
969 if (CMD_ARGC != 1)
970 return ERROR_COMMAND_SYNTAX_ERROR;
972 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
973 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
974 return ERROR_COMMAND_ARGUMENT_INVALID;
977 if (tmp < JAYLINK_USB_ADDRESS_0 || tmp > JAYLINK_USB_ADDRESS_3) {
978 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
979 return ERROR_COMMAND_ARGUMENT_INVALID;
982 usb_address = tmp;
984 use_usb_address = true;
986 return ERROR_OK;
989 COMMAND_HANDLER(jlink_handle_hwstatus_command)
991 int ret;
992 struct jaylink_hardware_status status;
994 ret = jaylink_get_hardware_status(devh, &status);
996 if (ret != JAYLINK_OK) {
997 command_print(CMD, "jaylink_get_hardware_status() failed: %s",
998 jaylink_strerror(ret));
999 return ERROR_FAIL;
1002 command_print(CMD, "VTarget = %u.%03u V",
1003 status.target_voltage / 1000, status.target_voltage % 1000);
1005 command_print(CMD, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
1006 "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
1007 status.tres, status.trst);
1009 if (status.target_voltage < 1500)
1010 command_print(CMD, "Target voltage too low. Check target power");
1012 return ERROR_OK;
1015 COMMAND_HANDLER(jlink_handle_free_memory_command)
1017 int ret;
1018 uint32_t tmp;
1020 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
1021 command_print(CMD, "Retrieval of free memory is not supported by "
1022 "the device");
1023 return ERROR_OK;
1026 ret = jaylink_get_free_memory(devh, &tmp);
1028 if (ret != JAYLINK_OK) {
1029 command_print(CMD, "jaylink_get_free_memory() failed: %s",
1030 jaylink_strerror(ret));
1031 return ERROR_FAIL;
1034 command_print(CMD, "Device has %" PRIu32 " bytes of free memory", tmp);
1036 return ERROR_OK;
1039 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1041 int tmp;
1042 int version;
1044 if (!CMD_ARGC) {
1045 switch (jtag_command_version) {
1046 case JAYLINK_JTAG_VERSION_2:
1047 version = 2;
1048 break;
1049 case JAYLINK_JTAG_VERSION_3:
1050 version = 3;
1051 break;
1052 default:
1053 return ERROR_FAIL;
1056 command_print(CMD, "JTAG command version: %i", version);
1057 } else if (CMD_ARGC == 1) {
1058 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
1059 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1060 return ERROR_COMMAND_ARGUMENT_INVALID;
1063 switch (tmp) {
1064 case 2:
1065 jtag_command_version = JAYLINK_JTAG_VERSION_2;
1066 break;
1067 case 3:
1068 jtag_command_version = JAYLINK_JTAG_VERSION_3;
1069 break;
1070 default:
1071 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1072 return ERROR_COMMAND_ARGUMENT_INVALID;
1074 } else {
1075 return ERROR_COMMAND_SYNTAX_ERROR;
1078 return ERROR_OK;
1081 COMMAND_HANDLER(jlink_handle_target_power_command)
1083 int ret;
1084 int enable;
1086 if (CMD_ARGC != 1)
1087 return ERROR_COMMAND_SYNTAX_ERROR;
1089 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1090 command_print(CMD, "Target power supply is not supported by the "
1091 "device");
1092 return ERROR_OK;
1095 if (!strcmp(CMD_ARGV[0], "on")) {
1096 enable = true;
1097 } else if (!strcmp(CMD_ARGV[0], "off")) {
1098 enable = false;
1099 } else {
1100 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1101 return ERROR_FAIL;
1104 ret = jaylink_set_target_power(devh, enable);
1106 if (ret != JAYLINK_OK) {
1107 command_print(CMD, "jaylink_set_target_power() failed: %s",
1108 jaylink_strerror(ret));
1109 return ERROR_FAIL;
1112 return ERROR_OK;
1115 static void show_config_usb_address(struct command_invocation *cmd)
1117 if (config.usb_address != tmp_config.usb_address)
1118 command_print(cmd, "USB address: %u [%u]", config.usb_address,
1119 tmp_config.usb_address);
1120 else
1121 command_print(cmd, "USB address: %u", config.usb_address);
1124 static void show_config_ip_address(struct command_invocation *cmd)
1126 if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1127 command_print(cmd, "IP address: %d.%d.%d.%d",
1128 config.ip_address[3], config.ip_address[2],
1129 config.ip_address[1], config.ip_address[0]);
1130 else
1131 command_print(cmd, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1132 config.ip_address[3], config.ip_address[2],
1133 config.ip_address[1], config.ip_address[0],
1134 tmp_config.ip_address[3], tmp_config.ip_address[2],
1135 tmp_config.ip_address[1], tmp_config.ip_address[0]);
1137 if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1138 command_print(cmd, "Subnet mask: %d.%d.%d.%d",
1139 config.subnet_mask[3], config.subnet_mask[2],
1140 config.subnet_mask[1], config.subnet_mask[0]);
1141 else
1142 command_print(cmd, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1143 config.subnet_mask[3], config.subnet_mask[2],
1144 config.subnet_mask[1], config.subnet_mask[0],
1145 tmp_config.subnet_mask[3], tmp_config.subnet_mask[2],
1146 tmp_config.subnet_mask[1], tmp_config.subnet_mask[0]);
1149 static void show_config_mac_address(struct command_invocation *cmd)
1151 if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1152 command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1153 config.mac_address[5], config.mac_address[4],
1154 config.mac_address[3], config.mac_address[2],
1155 config.mac_address[1], config.mac_address[0]);
1156 else
1157 command_print(cmd, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1158 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1159 config.mac_address[5], config.mac_address[4],
1160 config.mac_address[3], config.mac_address[2],
1161 config.mac_address[1], config.mac_address[0],
1162 tmp_config.mac_address[5], tmp_config.mac_address[4],
1163 tmp_config.mac_address[3], tmp_config.mac_address[2],
1164 tmp_config.mac_address[1], tmp_config.mac_address[0]);
1167 static void show_config_target_power(struct command_invocation *cmd)
1169 const char *target_power;
1170 const char *current_target_power;
1172 if (!config.target_power)
1173 target_power = "off";
1174 else
1175 target_power = "on";
1177 if (!tmp_config.target_power)
1178 current_target_power = "off";
1179 else
1180 current_target_power = "on";
1182 if (config.target_power != tmp_config.target_power)
1183 command_print(cmd, "Target power supply: %s [%s]", target_power,
1184 current_target_power);
1185 else
1186 command_print(cmd, "Target power supply: %s", target_power);
1189 static void show_config(struct command_invocation *cmd)
1191 command_print(cmd, "J-Link device configuration:");
1193 show_config_usb_address(cmd);
1195 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1196 show_config_target_power(cmd);
1198 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1199 show_config_ip_address(cmd);
1200 show_config_mac_address(cmd);
1204 static int poll_trace(uint8_t *buf, size_t *size)
1206 int ret;
1207 uint32_t length;
1209 length = *size;
1211 ret = jaylink_swo_read(devh, buf, &length);
1213 if (ret != JAYLINK_OK) {
1214 LOG_ERROR("jaylink_swo_read() failed: %s", jaylink_strerror(ret));
1215 return ERROR_FAIL;
1218 *size = length;
1220 return ERROR_OK;
1223 static uint32_t calculate_trace_buffer_size(void)
1225 int ret;
1226 uint32_t tmp;
1228 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1229 return 0;
1231 ret = jaylink_get_free_memory(devh, &tmp);
1233 if (ret != JAYLINK_OK) {
1234 LOG_ERROR("jaylink_get_free_memory() failed: %s",
1235 jaylink_strerror(ret));
1236 return ERROR_FAIL;
1239 if (tmp > 0x3fff || tmp <= 0x600)
1240 tmp = tmp >> 1;
1241 else
1242 tmp = tmp - 0x400;
1244 return tmp & 0xffffff00;
1247 static bool calculate_swo_prescaler(unsigned int traceclkin_freq,
1248 uint32_t trace_freq, uint16_t *prescaler)
1250 unsigned int presc = (traceclkin_freq + trace_freq / 2) / trace_freq;
1251 if (presc == 0 || presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1252 return false;
1254 /* Probe's UART speed must be within 3% of the TPIU's SWO baud rate. */
1255 unsigned int max_deviation = (traceclkin_freq * 3) / 100;
1256 if (presc * trace_freq < traceclkin_freq - max_deviation ||
1257 presc * trace_freq > traceclkin_freq + max_deviation)
1258 return false;
1260 *prescaler = presc;
1262 return true;
1265 static bool detect_swo_freq_and_prescaler(struct jaylink_swo_speed speed,
1266 unsigned int traceclkin_freq, unsigned int *trace_freq,
1267 uint16_t *prescaler)
1269 uint32_t divider;
1270 unsigned int presc;
1271 double deviation;
1273 for (divider = speed.min_div; divider <= speed.max_div; divider++) {
1274 *trace_freq = speed.freq / divider;
1275 presc = ((1.0 - SWO_MAX_FREQ_DEV) * traceclkin_freq) / *trace_freq + 1;
1277 if (presc > TPIU_ACPR_MAX_SWOSCALER + 1)
1278 break;
1280 deviation = fabs(1.0 - ((double)*trace_freq * presc / traceclkin_freq));
1282 if (deviation <= SWO_MAX_FREQ_DEV) {
1283 *prescaler = presc;
1284 return true;
1288 return false;
1291 static int config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1292 uint32_t port_size, unsigned int *trace_freq,
1293 unsigned int traceclkin_freq, uint16_t *prescaler)
1295 int ret;
1296 uint32_t buffer_size;
1297 struct jaylink_swo_speed speed;
1298 uint32_t divider;
1299 uint32_t min_freq;
1300 uint32_t max_freq;
1302 trace_enabled = enabled;
1304 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1305 if (!enabled)
1306 return ERROR_OK;
1308 LOG_ERROR("Trace capturing is not supported by the device");
1309 return ERROR_FAIL;
1312 ret = jaylink_swo_stop(devh);
1314 if (ret != JAYLINK_OK) {
1315 LOG_ERROR("jaylink_swo_stop() failed: %s", jaylink_strerror(ret));
1316 return ERROR_FAIL;
1319 if (!enabled) {
1321 * Adjust the SWD transaction buffer size as stopping SWO capturing
1322 * deallocates device internal memory.
1324 if (!adjust_swd_buffer_size())
1325 return ERROR_FAIL;
1327 return ERROR_OK;
1330 if (pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART) {
1331 LOG_ERROR("Selected pin protocol is not supported");
1332 return ERROR_FAIL;
1335 buffer_size = calculate_trace_buffer_size();
1337 if (!buffer_size) {
1338 LOG_ERROR("Not enough free device memory to start trace capturing");
1339 return ERROR_FAIL;
1342 ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1344 if (ret != JAYLINK_OK) {
1345 LOG_ERROR("jaylink_swo_get_speeds() failed: %s",
1346 jaylink_strerror(ret));
1347 return ERROR_FAIL;
1350 if (*trace_freq > 0) {
1351 divider = speed.freq / *trace_freq;
1352 min_freq = speed.freq / speed.max_div;
1353 max_freq = speed.freq / speed.min_div;
1355 if (*trace_freq > max_freq) {
1356 LOG_INFO("Given SWO frequency too high, using %" PRIu32 " Hz instead",
1357 max_freq);
1358 *trace_freq = max_freq;
1359 } else if (*trace_freq < min_freq) {
1360 LOG_INFO("Given SWO frequency too low, using %" PRIu32 " Hz instead",
1361 min_freq);
1362 *trace_freq = min_freq;
1363 } else if (*trace_freq != speed.freq / divider) {
1364 *trace_freq = speed.freq / divider;
1366 LOG_INFO("Given SWO frequency is not supported by the device, "
1367 "using %u Hz instead", *trace_freq);
1370 if (!calculate_swo_prescaler(traceclkin_freq, *trace_freq,
1371 prescaler)) {
1372 LOG_ERROR("SWO frequency is not suitable. Please choose a "
1373 "different frequency or use auto-detection");
1374 return ERROR_FAIL;
1376 } else {
1377 LOG_INFO("Trying to auto-detect SWO frequency");
1379 if (!detect_swo_freq_and_prescaler(speed, traceclkin_freq, trace_freq,
1380 prescaler)) {
1381 LOG_ERROR("Maximum permitted frequency deviation of %.02f %% "
1382 "could not be achieved", SWO_MAX_FREQ_DEV);
1383 LOG_ERROR("Auto-detection of SWO frequency failed");
1384 return ERROR_FAIL;
1387 LOG_INFO("Using SWO frequency of %u Hz", *trace_freq);
1390 ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1391 buffer_size);
1393 if (ret != JAYLINK_OK) {
1394 LOG_ERROR("jaylink_start_swo() failed: %s", jaylink_strerror(ret));
1395 return ERROR_FAIL;
1398 LOG_DEBUG("Using %" PRIu32 " bytes device memory for trace capturing",
1399 buffer_size);
1402 * Adjust the SWD transaction buffer size as starting SWO capturing
1403 * allocates device internal memory.
1405 if (!adjust_swd_buffer_size())
1406 return ERROR_FAIL;
1408 return ERROR_OK;
1411 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1413 uint8_t tmp;
1415 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1416 command_print(CMD, "Reading configuration is not supported by the "
1417 "device");
1418 return ERROR_OK;
1421 if (!CMD_ARGC) {
1422 show_config_usb_address(CMD);
1423 } else if (CMD_ARGC == 1) {
1424 if (sscanf(CMD_ARGV[0], "%" SCNd8, &tmp) != 1) {
1425 command_print(CMD, "Invalid USB address: %s", CMD_ARGV[0]);
1426 return ERROR_COMMAND_ARGUMENT_INVALID;
1429 if (tmp > JAYLINK_USB_ADDRESS_3) {
1430 command_print(CMD, "Invalid USB address: %u", tmp);
1431 return ERROR_COMMAND_ARGUMENT_INVALID;
1434 tmp_config.usb_address = tmp;
1435 } else {
1436 return ERROR_COMMAND_SYNTAX_ERROR;
1439 return ERROR_OK;
1442 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1444 int enable;
1446 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1447 command_print(CMD, "Reading configuration is not supported by the "
1448 "device");
1449 return ERROR_OK;
1452 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1453 command_print(CMD, "Target power supply is not supported by the "
1454 "device");
1455 return ERROR_OK;
1458 if (!CMD_ARGC) {
1459 show_config_target_power(CMD);
1460 } else if (CMD_ARGC == 1) {
1461 if (!strcmp(CMD_ARGV[0], "on")) {
1462 enable = true;
1463 } else if (!strcmp(CMD_ARGV[0], "off")) {
1464 enable = false;
1465 } else {
1466 command_print(CMD, "Invalid argument: %s", CMD_ARGV[0]);
1467 return ERROR_COMMAND_ARGUMENT_INVALID;
1470 tmp_config.target_power = enable;
1471 } else {
1472 return ERROR_COMMAND_SYNTAX_ERROR;
1475 return ERROR_OK;
1478 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1480 uint8_t addr[6];
1481 int i;
1482 char *e;
1483 const char *str;
1485 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1486 command_print(CMD, "Reading configuration is not supported by the "
1487 "device");
1488 return ERROR_OK;
1491 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1492 command_print(CMD, "Ethernet connectivity is not supported by the "
1493 "device");
1494 return ERROR_OK;
1497 if (!CMD_ARGC) {
1498 show_config_mac_address(CMD);
1499 } else if (CMD_ARGC == 1) {
1500 str = CMD_ARGV[0];
1502 if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' ||
1503 str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1504 command_print(CMD, "Invalid MAC address format");
1505 return ERROR_COMMAND_ARGUMENT_INVALID;
1508 for (i = 5; i >= 0; i--) {
1509 addr[i] = strtoul(str, &e, 16);
1510 str = e + 1;
1513 if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1514 command_print(CMD, "Invalid MAC address: zero address");
1515 return ERROR_COMMAND_ARGUMENT_INVALID;
1518 if (!(0x01 & addr[0])) {
1519 command_print(CMD, "Invalid MAC address: multicast address");
1520 return ERROR_COMMAND_ARGUMENT_INVALID;
1523 memcpy(tmp_config.mac_address, addr, sizeof(addr));
1524 } else {
1525 return ERROR_COMMAND_SYNTAX_ERROR;
1528 return ERROR_OK;
1531 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1533 uint8_t lip[4];
1534 char *e;
1535 const char *s_save = s;
1536 int i;
1538 if (!s)
1539 return false;
1541 for (i = 0; i < 4; i++) {
1542 lip[i] = strtoul(s, &e, 10);
1544 if (*e != '.' && i != 3)
1545 return false;
1547 s = e + 1;
1550 *pos = e - s_save;
1551 memcpy(ip, lip, sizeof(lip));
1553 return true;
1556 static void cpy_ip(uint8_t *dst, uint8_t *src)
1558 int i, j;
1560 for (i = 0, j = 3; i < 4; i++, j--)
1561 dst[i] = src[j];
1564 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1566 uint8_t ip_address[4];
1567 uint32_t subnet_mask = 0;
1568 int i, len;
1569 uint8_t subnet_bits = 24;
1571 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1572 command_print(CMD, "Reading configuration is not supported by the "
1573 "device");
1574 return ERROR_OK;
1577 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1578 command_print(CMD, "Ethernet connectivity is not supported by the "
1579 "device");
1580 return ERROR_OK;
1583 if (!CMD_ARGC) {
1584 show_config_ip_address(CMD);
1585 } else {
1586 if (!string_to_ip(CMD_ARGV[0], ip_address, &i)) {
1587 command_print(CMD, "invalid IPv4 address");
1588 return ERROR_COMMAND_ARGUMENT_INVALID;
1591 len = strlen(CMD_ARGV[0]);
1593 /* Check for format A.B.C.D/E. */
1594 if (i < len) {
1595 if (CMD_ARGV[0][i] != '/') {
1596 command_print(CMD, "missing network mask");
1597 return ERROR_COMMAND_ARGUMENT_INVALID;
1600 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1601 } else if (CMD_ARGC > 1) {
1602 if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i)) {
1603 command_print(CMD, "invalid subnet mask");
1604 return ERROR_COMMAND_ARGUMENT_INVALID;
1608 if (!subnet_mask)
1609 subnet_mask = (uint32_t)(subnet_bits < 32 ?
1610 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1612 cpy_ip(tmp_config.ip_address, ip_address);
1613 cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1616 return ERROR_OK;
1619 COMMAND_HANDLER(jlink_handle_config_reset_command)
1621 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1622 return ERROR_OK;
1624 memcpy(&tmp_config, &config, sizeof(struct device_config));
1626 return ERROR_OK;
1630 COMMAND_HANDLER(jlink_handle_config_write_command)
1632 int ret;
1634 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1635 command_print(CMD, "Reading configuration is not supported by the "
1636 "device");
1637 return ERROR_OK;
1640 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1641 command_print(CMD, "Writing configuration is not supported by the "
1642 "device");
1643 return ERROR_OK;
1646 if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1647 command_print(CMD, "Operation not performed due to no changes in "
1648 "the configuration");
1649 return ERROR_OK;
1652 ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1654 if (ret != JAYLINK_OK) {
1655 LOG_ERROR("jaylink_write_raw_config() failed: %s",
1656 jaylink_strerror(ret));
1657 return ERROR_FAIL;
1660 if (!read_device_config(&config)) {
1661 LOG_ERROR("Failed to read device configuration for verification");
1662 return ERROR_FAIL;
1665 if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1666 LOG_ERROR("Verification of device configuration failed. Please check "
1667 "your device");
1668 return ERROR_FAIL;
1671 memcpy(&tmp_config, &config, sizeof(struct device_config));
1672 command_print(CMD, "The new device configuration applies after power "
1673 "cycling the J-Link device");
1675 return ERROR_OK;
1678 COMMAND_HANDLER(jlink_handle_config_command)
1680 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1681 command_print(CMD, "Device doesn't support reading configuration");
1682 return ERROR_OK;
1685 if (CMD_ARGC == 0)
1686 show_config(CMD);
1688 return ERROR_OK;
1691 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1693 int ret;
1694 size_t tmp;
1695 uint32_t channel;
1696 uint32_t length;
1697 uint8_t *buf;
1698 size_t dummy;
1700 if (CMD_ARGC != 2)
1701 return ERROR_COMMAND_SYNTAX_ERROR;
1703 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1704 LOG_ERROR("Device does not support EMUCOM");
1705 return ERROR_FAIL;
1708 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1710 tmp = strlen(CMD_ARGV[1]);
1712 if (tmp % 2 != 0) {
1713 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1714 return ERROR_COMMAND_ARGUMENT_INVALID;
1717 buf = malloc(tmp / 2);
1719 if (!buf) {
1720 LOG_ERROR("Failed to allocate buffer");
1721 return ERROR_FAIL;
1724 dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1726 if (dummy != (tmp / 2)) {
1727 LOG_ERROR("Data must be encoded as hexadecimal pairs");
1728 free(buf);
1729 return ERROR_COMMAND_ARGUMENT_INVALID;
1732 length = tmp / 2;
1733 ret = jaylink_emucom_write(devh, channel, buf, &length);
1735 free(buf);
1737 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1738 LOG_ERROR("Channel not supported by the device");
1739 return ERROR_FAIL;
1740 } else if (ret != JAYLINK_OK) {
1741 LOG_ERROR("Failed to write to channel: %s", jaylink_strerror(ret));
1742 return ERROR_FAIL;
1745 if (length != (tmp / 2))
1746 LOG_WARNING("Only %" PRIu32 " bytes written to the channel", length);
1748 return ERROR_OK;
1751 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1753 int ret;
1754 uint32_t channel;
1755 uint32_t length;
1756 uint8_t *buf;
1757 size_t tmp;
1759 if (CMD_ARGC != 2)
1760 return ERROR_COMMAND_SYNTAX_ERROR;
1762 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1763 LOG_ERROR("Device does not support EMUCOM");
1764 return ERROR_FAIL;
1767 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1768 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
1770 buf = malloc(length * 3 + 1);
1772 if (!buf) {
1773 LOG_ERROR("Failed to allocate buffer");
1774 return ERROR_FAIL;
1777 ret = jaylink_emucom_read(devh, channel, buf, &length);
1779 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1780 LOG_ERROR("Channel is not supported by the device");
1781 free(buf);
1782 return ERROR_FAIL;
1783 } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1784 LOG_ERROR("Channel is not available for the requested amount of data. "
1785 "%" PRIu32 " bytes are available", length);
1786 free(buf);
1787 return ERROR_FAIL;
1788 } else if (ret != JAYLINK_OK) {
1789 LOG_ERROR("Failed to read from channel: %s", jaylink_strerror(ret));
1790 free(buf);
1791 return ERROR_FAIL;
1794 tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1796 if (tmp != 2 * length) {
1797 LOG_ERROR("Failed to convert data into hexadecimal string");
1798 free(buf);
1799 return ERROR_FAIL;
1802 command_print(CMD, "%s", buf + length);
1803 free(buf);
1805 return ERROR_OK;
1808 static const struct command_registration jlink_config_subcommand_handlers[] = {
1810 .name = "usb",
1811 .handler = &jlink_handle_config_usb_address_command,
1812 .mode = COMMAND_EXEC,
1813 .help = "set the USB address",
1814 .usage = "[0-3]",
1817 .name = "targetpower",
1818 .handler = &jlink_handle_config_target_power_command,
1819 .mode = COMMAND_EXEC,
1820 .help = "set the target power supply",
1821 .usage = "[on|off]"
1824 .name = "mac",
1825 .handler = &jlink_handle_config_mac_address_command,
1826 .mode = COMMAND_EXEC,
1827 .help = "set the MAC Address",
1828 .usage = "[ff:ff:ff:ff:ff:ff]",
1831 .name = "ip",
1832 .handler = &jlink_handle_config_ip_address_command,
1833 .mode = COMMAND_EXEC,
1834 .help = "set the IP address, where A.B.C.D is the IP address, "
1835 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1836 .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1839 .name = "reset",
1840 .handler = &jlink_handle_config_reset_command,
1841 .mode = COMMAND_EXEC,
1842 .help = "undo configuration changes",
1843 .usage = "",
1846 .name = "write",
1847 .handler = &jlink_handle_config_write_command,
1848 .mode = COMMAND_EXEC,
1849 .help = "write configuration to the device",
1850 .usage = "",
1852 COMMAND_REGISTRATION_DONE
1855 static const struct command_registration jlink_emucom_subcommand_handlers[] = {
1857 .name = "write",
1858 .handler = &jlink_handle_emucom_write_command,
1859 .mode = COMMAND_EXEC,
1860 .help = "write to a channel",
1861 .usage = "<channel> <data>",
1864 .name = "read",
1865 .handler = &jlink_handle_emucom_read_command,
1866 .mode = COMMAND_EXEC,
1867 .help = "read from a channel",
1868 .usage = "<channel> <length>"
1870 COMMAND_REGISTRATION_DONE
1873 static const struct command_registration jlink_subcommand_handlers[] = {
1875 .name = "jtag",
1876 .handler = &jlink_handle_jlink_jtag_command,
1877 .mode = COMMAND_EXEC,
1878 .help = "select the JTAG command version",
1879 .usage = "[2|3]",
1882 .name = "targetpower",
1883 .handler = &jlink_handle_target_power_command,
1884 .mode = COMMAND_EXEC,
1885 .help = "set the target power supply",
1886 .usage = "<on|off>"
1889 .name = "freemem",
1890 .handler = &jlink_handle_free_memory_command,
1891 .mode = COMMAND_EXEC,
1892 .help = "show free device memory",
1893 .usage = "",
1896 .name = "hwstatus",
1897 .handler = &jlink_handle_hwstatus_command,
1898 .mode = COMMAND_EXEC,
1899 .help = "show the hardware status",
1900 .usage = "",
1903 .name = "usb",
1904 .handler = &jlink_usb_command,
1905 .mode = COMMAND_CONFIG,
1906 .help = "set the USB address of the device that should be used",
1907 .usage = "<0-3>"
1910 .name = "config",
1911 .handler = &jlink_handle_config_command,
1912 .mode = COMMAND_EXEC,
1913 .help = "access the device configuration. If no argument is given "
1914 "this will show the device configuration",
1915 .chain = jlink_config_subcommand_handlers,
1916 .usage = "[<cmd>]",
1919 .name = "emucom",
1920 .mode = COMMAND_EXEC,
1921 .help = "access EMUCOM channel",
1922 .chain = jlink_emucom_subcommand_handlers,
1923 .usage = "",
1925 COMMAND_REGISTRATION_DONE
1928 static const struct command_registration jlink_command_handlers[] = {
1930 .name = "jlink",
1931 .mode = COMMAND_ANY,
1932 .help = "perform jlink management",
1933 .chain = jlink_subcommand_handlers,
1934 .usage = "",
1936 COMMAND_REGISTRATION_DONE
1939 static int jlink_swd_init(void)
1941 iface = JAYLINK_TIF_SWD;
1943 return ERROR_OK;
1946 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1948 assert(!(cmd & SWD_CMD_RNW));
1949 jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1952 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1954 assert(cmd & SWD_CMD_RNW);
1955 jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1958 /***************************************************************************/
1959 /* J-Link tap functions */
1961 static unsigned tap_length;
1962 /* In SWD mode use tms buffer for direction control */
1963 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
1964 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
1965 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
1967 struct pending_scan_result {
1968 /** First bit position in tdo_buffer to read. */
1969 unsigned first;
1970 /** Number of bits to read. */
1971 unsigned length;
1972 /** Location to store the result */
1973 void *buffer;
1974 /** Offset in the destination buffer */
1975 unsigned buffer_offset;
1976 /** SWD command */
1977 uint8_t swd_cmd;
1980 #define MAX_PENDING_SCAN_RESULTS 256
1982 static int pending_scan_results_length;
1983 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1985 static void jlink_tap_init(void)
1987 tap_length = 0;
1988 pending_scan_results_length = 0;
1989 memset(tms_buffer, 0, sizeof(tms_buffer));
1990 memset(tdi_buffer, 0, sizeof(tdi_buffer));
1993 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
1994 const uint8_t *tms_out, unsigned tms_offset,
1995 uint8_t *in, unsigned in_offset,
1996 unsigned length)
1998 do {
1999 unsigned available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
2001 if (!available_length ||
2002 (in && pending_scan_results_length == MAX_PENDING_SCAN_RESULTS)) {
2003 if (jlink_flush() != ERROR_OK)
2004 return;
2005 available_length = JLINK_TAP_BUFFER_SIZE;
2008 struct pending_scan_result *pending_scan_result =
2009 &pending_scan_results_buffer[pending_scan_results_length];
2011 unsigned scan_length = length > available_length ?
2012 available_length : length;
2014 if (out)
2015 buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
2016 if (tms_out)
2017 buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
2019 if (in) {
2020 pending_scan_result->first = tap_length;
2021 pending_scan_result->length = scan_length;
2022 pending_scan_result->buffer = in;
2023 pending_scan_result->buffer_offset = in_offset;
2024 pending_scan_results_length++;
2027 tap_length += scan_length;
2028 out_offset += scan_length;
2029 tms_offset += scan_length;
2030 in_offset += scan_length;
2031 length -= scan_length;
2032 } while (length > 0);
2035 static int jlink_flush(void)
2037 int i;
2038 int ret;
2040 if (!tap_length)
2041 return ERROR_OK;
2043 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
2044 tap_length, jlink_last_state);
2046 ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
2047 tap_length, jtag_command_version);
2049 if (ret != JAYLINK_OK) {
2050 LOG_ERROR("jaylink_jtag_io() failed: %s", jaylink_strerror(ret));
2051 jlink_tap_init();
2052 return ERROR_JTAG_QUEUE_FAILED;
2055 for (i = 0; i < pending_scan_results_length; i++) {
2056 struct pending_scan_result *p = &pending_scan_results_buffer[i];
2058 buf_set_buf(tdo_buffer, p->first, p->buffer,
2059 p->buffer_offset, p->length);
2061 LOG_DEBUG_IO("Pending scan result, length = %d", p->length);
2064 jlink_tap_init();
2066 return ERROR_OK;
2069 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
2071 unsigned int tap_pos = tap_length;
2073 while (len > 32) {
2074 buf_set_u32(buf, tap_pos, 32, val);
2075 len -= 32;
2076 tap_pos += 32;
2079 if (len)
2080 buf_set_u32(buf, tap_pos, len, val);
2083 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2085 const uint32_t dir_out = 0xffffffff;
2087 if (data)
2088 bit_copy(tdi_buffer, tap_length, data, 0, len);
2089 else
2090 fill_buffer(tdi_buffer, 0, len);
2092 fill_buffer(tms_buffer, dir_out, len);
2093 tap_length += len;
2096 static void jlink_queue_data_in(uint32_t len)
2098 const uint32_t dir_in = 0;
2100 fill_buffer(tms_buffer, dir_in, len);
2101 tap_length += len;
2104 static int jlink_swd_switch_seq(enum swd_special_seq seq)
2106 const uint8_t *s;
2107 unsigned int s_len;
2109 switch (seq) {
2110 case LINE_RESET:
2111 LOG_DEBUG_IO("SWD line reset");
2112 s = swd_seq_line_reset;
2113 s_len = swd_seq_line_reset_len;
2114 break;
2115 case JTAG_TO_SWD:
2116 LOG_DEBUG("JTAG-to-SWD");
2117 s = swd_seq_jtag_to_swd;
2118 s_len = swd_seq_jtag_to_swd_len;
2119 break;
2120 case JTAG_TO_DORMANT:
2121 LOG_DEBUG("JTAG-to-DORMANT");
2122 s = swd_seq_jtag_to_dormant;
2123 s_len = swd_seq_jtag_to_dormant_len;
2124 break;
2125 case SWD_TO_JTAG:
2126 LOG_DEBUG("SWD-to-JTAG");
2127 s = swd_seq_swd_to_jtag;
2128 s_len = swd_seq_swd_to_jtag_len;
2129 break;
2130 case SWD_TO_DORMANT:
2131 LOG_DEBUG("SWD-to-DORMANT");
2132 s = swd_seq_swd_to_dormant;
2133 s_len = swd_seq_swd_to_dormant_len;
2134 break;
2135 case DORMANT_TO_SWD:
2136 LOG_DEBUG("DORMANT-to-SWD");
2137 s = swd_seq_dormant_to_swd;
2138 s_len = swd_seq_dormant_to_swd_len;
2139 break;
2140 case DORMANT_TO_JTAG:
2141 LOG_DEBUG("DORMANT-to-JTAG");
2142 s = swd_seq_dormant_to_jtag;
2143 s_len = swd_seq_dormant_to_jtag_len;
2144 break;
2145 default:
2146 LOG_ERROR("Sequence %d not supported", seq);
2147 return ERROR_FAIL;
2150 jlink_queue_data_out(s, s_len);
2152 return ERROR_OK;
2155 static int jlink_swd_run_queue(void)
2157 int i;
2158 int ret;
2160 LOG_DEBUG_IO("Executing %d queued transactions", pending_scan_results_length);
2162 if (queued_retval != ERROR_OK) {
2163 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
2164 goto skip;
2168 * A transaction must be followed by another transaction or at least 8 idle
2169 * cycles to ensure that data is clocked through the AP.
2171 jlink_queue_data_out(NULL, 8);
2173 ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2175 if (ret != JAYLINK_OK) {
2176 LOG_ERROR("jaylink_swd_io() failed: %s", jaylink_strerror(ret));
2177 goto skip;
2180 for (i = 0; i < pending_scan_results_length; i++) {
2181 /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
2182 bool check_ack = swd_cmd_returns_ack(pending_scan_results_buffer[i].swd_cmd);
2183 int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
2184 if (check_ack && ack != SWD_ACK_OK) {
2185 LOG_DEBUG("SWD ack not OK: %d %s", ack,
2186 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2187 queued_retval = swd_ack_to_error_code(ack);
2188 goto skip;
2189 } else if (pending_scan_results_buffer[i].length) {
2190 uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2191 int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
2193 if (parity != parity_u32(data)) {
2194 LOG_ERROR("SWD: Read data parity mismatch");
2195 queued_retval = ERROR_FAIL;
2196 goto skip;
2199 if (pending_scan_results_buffer[i].buffer)
2200 *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2204 skip:
2205 jlink_tap_init();
2206 ret = queued_retval;
2207 queued_retval = ERROR_OK;
2209 return ret;
2212 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2214 uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2215 if (tap_length + 46 + 8 + ap_delay_clk >= swd_buffer_size * 8 ||
2216 pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
2217 /* Not enough room in the queue. Run the queue. */
2218 queued_retval = jlink_swd_run_queue();
2221 if (queued_retval != ERROR_OK)
2222 return;
2224 pending_scan_results_buffer[pending_scan_results_length].swd_cmd = cmd;
2225 cmd |= SWD_CMD_START | SWD_CMD_PARK;
2227 jlink_queue_data_out(&cmd, 8);
2229 pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
2231 if (cmd & SWD_CMD_RNW) {
2232 /* Queue a read transaction. */
2233 pending_scan_results_buffer[pending_scan_results_length].length = 32;
2234 pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
2236 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2237 } else {
2238 /* Queue a write transaction. */
2239 pending_scan_results_buffer[pending_scan_results_length].length = 0;
2240 jlink_queue_data_in(1 + 3 + 1);
2242 buf_set_u32(data_parity_trn, 0, 32, data);
2243 buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2245 jlink_queue_data_out(data_parity_trn, 32 + 1);
2248 pending_scan_results_length++;
2250 /* Insert idle cycles after AP accesses to avoid WAIT. */
2251 if (cmd & SWD_CMD_APNDP)
2252 jlink_queue_data_out(NULL, ap_delay_clk);
2255 static const struct swd_driver jlink_swd = {
2256 .init = &jlink_swd_init,
2257 .switch_seq = &jlink_swd_switch_seq,
2258 .read_reg = &jlink_swd_read_reg,
2259 .write_reg = &jlink_swd_write_reg,
2260 .run = &jlink_swd_run_queue,
2263 static const char * const jlink_transports[] = { "jtag", "swd", NULL };
2265 static struct jtag_interface jlink_interface = {
2266 .execute_queue = &jlink_execute_queue,
2269 struct adapter_driver jlink_adapter_driver = {
2270 .name = "jlink",
2271 .transports = jlink_transports,
2272 .commands = jlink_command_handlers,
2274 .init = &jlink_init,
2275 .quit = &jlink_quit,
2276 .reset = &jlink_reset_safe,
2277 .speed = &jlink_speed,
2278 .khz = &jlink_khz,
2279 .speed_div = &jlink_speed_div,
2280 .config_trace = &config_trace,
2281 .poll_trace = &poll_trace,
2283 .jtag_ops = &jlink_interface,
2284 .swd_ops = &jlink_swd,