jlink: Make libusb optional
[openocd.git] / src / jtag / drivers / jlink.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
9 * plagnioj@jcrosoft.com *
10 * *
11 * Copyright (C) 2015 by Marc Schink *
12 * openocd-dev@marcschink.de *
13 * *
14 * Copyright (C) 2015 by Paul Fertser *
15 * fercerpav@gmail.com *
16 * *
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. *
21 * *
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. *
26 * *
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 ***************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stdint.h>
36 #include <math.h>
37
38 #include <jtag/interface.h>
39 #include <jtag/swd.h>
40 #include <jtag/commands.h>
41
42 #include <libjaylink/libjaylink.h>
43
44 static struct jaylink_context *jayctx;
45 static struct jaylink_device_handle *devh;
46 static struct jaylink_connection conn;
47 static struct jaylink_connection connlist[JAYLINK_MAX_CONNECTIONS];
48 static enum jaylink_jtag_version jtag_command_version;
49 static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
50
51 static uint32_t serial_number;
52 static bool use_serial_number;
53 static enum jaylink_usb_address usb_address;
54 static bool use_usb_address;
55 static enum jaylink_target_interface iface = JAYLINK_TIF_JTAG;
56 static bool trace_enabled;
57
58 #define JLINK_MAX_SPEED 12000
59 #define JLINK_TAP_BUFFER_SIZE 2048
60
61 static unsigned int swd_buffer_size = JLINK_TAP_BUFFER_SIZE;
62
63 /* 256 byte non-volatile memory */
64 struct device_config {
65 uint8_t usb_address;
66 /* 0ffset 0x01 to 0x03 */
67 uint8_t reserved_1[3];
68 uint32_t target_power;
69 /* 0ffset 0x08 to 0x1f */
70 uint8_t reserved_2[24];
71 /* IP only for J-Link Pro */
72 uint8_t ip_address[4];
73 uint8_t subnet_mask[4];
74 /* 0ffset 0x28 to 0x2f */
75 uint8_t reserved_3[8];
76 uint8_t mac_address[6];
77 /* 0ffset 0x36 to 0xff */
78 uint8_t reserved_4[202];
79 } __attribute__ ((packed));
80
81 static struct device_config config;
82 static struct device_config tmp_config;
83
84 /* Queue command functions */
85 static void jlink_end_state(tap_state_t state);
86 static void jlink_state_move(void);
87 static void jlink_path_move(int num_states, tap_state_t *path);
88 static void jlink_stableclocks(int num_cycles);
89 static void jlink_runtest(int num_cycles);
90 static void jlink_reset(int trst, int srst);
91 static int jlink_swd_run_queue(void);
92 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk);
93 static int jlink_swd_switch_seq(enum swd_special_seq seq);
94
95 /* J-Link tap buffer functions */
96 static void jlink_tap_init(void);
97 static int jlink_flush(void);
98 /**
99 * Queue data to go out and in, flushing the queue as many times as
100 * necessary.
101 *
102 * @param out A pointer to TDI data, if NULL, old stale data will be used.
103 * @param out_offset A bit offset for TDI data.
104 * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
105 * @param tms_offset A bit offset for TMS data.
106 * @param in A pointer to store TDO data to, if NULL the data will be discarded.
107 * @param in_offset A bit offset for TDO data.
108 * @param length Amount of bits to transfer out and in.
109 *
110 * @retval This function doesn't return any value.
111 */
112 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
113 const uint8_t *tms_out, unsigned tms_offset,
114 uint8_t *in, unsigned in_offset,
115 unsigned length);
116
117 static enum tap_state jlink_last_state = TAP_RESET;
118 static int queued_retval;
119
120 /***************************************************************************/
121 /* External interface implementation */
122
123 static void jlink_execute_stableclocks(struct jtag_command *cmd)
124 {
125 DEBUG_JTAG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
126 jlink_stableclocks(cmd->cmd.runtest->num_cycles);
127 }
128
129 static void jlink_execute_runtest(struct jtag_command *cmd)
130 {
131 DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
132 cmd->cmd.runtest->end_state);
133
134 jlink_end_state(cmd->cmd.runtest->end_state);
135 jlink_runtest(cmd->cmd.runtest->num_cycles);
136 }
137
138 static void jlink_execute_statemove(struct jtag_command *cmd)
139 {
140 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
141
142 jlink_end_state(cmd->cmd.statemove->end_state);
143 jlink_state_move();
144 }
145
146 static void jlink_execute_pathmove(struct jtag_command *cmd)
147 {
148 DEBUG_JTAG_IO("pathmove: %i states, end in %i",
149 cmd->cmd.pathmove->num_states,
150 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
151
152 jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
153 }
154
155 static void jlink_execute_scan(struct jtag_command *cmd)
156 {
157 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
158 jtag_scan_type(cmd->cmd.scan));
159
160 /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
161 while (cmd->cmd.scan->num_fields > 0
162 && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
163 cmd->cmd.scan->num_fields--;
164 LOG_DEBUG("discarding trailing empty field");
165 }
166
167 if (cmd->cmd.scan->num_fields == 0) {
168 LOG_DEBUG("empty scan, doing nothing");
169 return;
170 }
171
172 if (cmd->cmd.scan->ir_scan) {
173 if (tap_get_state() != TAP_IRSHIFT) {
174 jlink_end_state(TAP_IRSHIFT);
175 jlink_state_move();
176 }
177 } else {
178 if (tap_get_state() != TAP_DRSHIFT) {
179 jlink_end_state(TAP_DRSHIFT);
180 jlink_state_move();
181 }
182 }
183
184 jlink_end_state(cmd->cmd.scan->end_state);
185
186 struct scan_field *field = cmd->cmd.scan->fields;
187 unsigned scan_size = 0;
188
189 for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
190 scan_size += field->num_bits;
191 DEBUG_JTAG_IO("%s%s field %d/%d %d bits",
192 field->in_value ? "in" : "",
193 field->out_value ? "out" : "",
194 i,
195 cmd->cmd.scan->num_fields,
196 field->num_bits);
197
198 if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
199 /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
200 * movement. This last field can't have length zero, it was checked above. */
201 jlink_clock_data(field->out_value,
202 0,
203 NULL,
204 0,
205 field->in_value,
206 0,
207 field->num_bits - 1);
208 uint8_t last_bit = 0;
209 if (field->out_value)
210 bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
211 uint8_t tms_bits = 0x01;
212 jlink_clock_data(&last_bit,
213 0,
214 &tms_bits,
215 0,
216 field->in_value,
217 field->num_bits - 1,
218 1);
219 tap_set_state(tap_state_transition(tap_get_state(), 1));
220 jlink_clock_data(NULL,
221 0,
222 &tms_bits,
223 1,
224 NULL,
225 0,
226 1);
227 tap_set_state(tap_state_transition(tap_get_state(), 0));
228 } else
229 jlink_clock_data(field->out_value,
230 0,
231 NULL,
232 0,
233 field->in_value,
234 0,
235 field->num_bits);
236 }
237
238 if (tap_get_state() != tap_get_end_state()) {
239 jlink_end_state(tap_get_end_state());
240 jlink_state_move();
241 }
242
243 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
244 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
245 tap_state_name(tap_get_end_state()));
246 }
247
248 static void jlink_execute_reset(struct jtag_command *cmd)
249 {
250 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst,
251 cmd->cmd.reset->srst);
252
253 jlink_flush();
254 jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
255 jlink_flush();
256 }
257
258 static void jlink_execute_sleep(struct jtag_command *cmd)
259 {
260 DEBUG_JTAG_IO("sleep %" PRIi32 "", cmd->cmd.sleep->us);
261 jlink_flush();
262 jtag_sleep(cmd->cmd.sleep->us);
263 }
264
265 static int jlink_execute_command(struct jtag_command *cmd)
266 {
267 switch (cmd->type) {
268 case JTAG_STABLECLOCKS:
269 jlink_execute_stableclocks(cmd);
270 break;
271 case JTAG_RUNTEST:
272 jlink_execute_runtest(cmd);
273 break;
274 case JTAG_TLR_RESET:
275 jlink_execute_statemove(cmd);
276 break;
277 case JTAG_PATHMOVE:
278 jlink_execute_pathmove(cmd);
279 break;
280 case JTAG_SCAN:
281 jlink_execute_scan(cmd);
282 break;
283 case JTAG_RESET:
284 jlink_execute_reset(cmd);
285 break;
286 case JTAG_SLEEP:
287 jlink_execute_sleep(cmd);
288 break;
289 default:
290 LOG_ERROR("BUG: Unknown JTAG command type encountered.");
291 return ERROR_JTAG_QUEUE_FAILED;
292 }
293
294 return ERROR_OK;
295 }
296
297 static int jlink_execute_queue(void)
298 {
299 int ret;
300 struct jtag_command *cmd = jtag_command_queue;
301
302 while (cmd != NULL) {
303 ret = jlink_execute_command(cmd);
304
305 if (ret != ERROR_OK)
306 return ret;
307
308 cmd = cmd->next;
309 }
310
311 return jlink_flush();
312 }
313
314 static int jlink_speed(int speed)
315 {
316 int ret;
317 struct jaylink_speed tmp;
318 int max_speed;
319
320 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
321 ret = jaylink_get_speeds(devh, &tmp);
322
323 if (ret != JAYLINK_OK) {
324 LOG_ERROR("jaylink_get_speeds() failed: %s.",
325 jaylink_strerror_name(ret));
326 return ERROR_JTAG_DEVICE_ERROR;
327 }
328
329 tmp.freq /= 1000;
330 max_speed = tmp.freq / tmp.div;
331 } else {
332 max_speed = JLINK_MAX_SPEED;
333 }
334
335 if (!speed) {
336 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
337 LOG_ERROR("Adaptive clocking is not supported by the device.");
338 return ERROR_JTAG_NOT_IMPLEMENTED;
339 }
340
341 speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
342 } else if (speed > max_speed) {
343 LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum).", speed,
344 max_speed);
345 speed = max_speed;
346 }
347
348 ret = jaylink_set_speed(devh, speed);
349
350 if (ret != JAYLINK_OK) {
351 LOG_ERROR("jaylink_set_speed() failed: %s.",
352 jaylink_strerror_name(ret));
353 return ERROR_JTAG_DEVICE_ERROR;
354 }
355
356 return ERROR_OK;
357 }
358
359 static int jlink_speed_div(int speed, int *khz)
360 {
361 *khz = speed;
362
363 return ERROR_OK;
364 }
365
366 static int jlink_khz(int khz, int *jtag_speed)
367 {
368 *jtag_speed = khz;
369
370 return ERROR_OK;
371 }
372
373 static bool read_device_config(struct device_config *cfg)
374 {
375 int ret;
376
377 ret = jaylink_read_raw_config(devh, (uint8_t *)cfg);
378
379 if (ret != JAYLINK_OK) {
380 LOG_ERROR("jaylink_read_raw_config() failed: %s.",
381 jaylink_strerror_name(ret));
382 return false;
383 }
384
385 if (cfg->usb_address == 0xff)
386 cfg->usb_address = 0x00;
387
388 if (cfg->target_power == 0xffffffff)
389 cfg->target_power = 0;
390
391 return true;
392 }
393
394 static int select_interface(void)
395 {
396 int ret;
397 uint32_t interfaces;
398
399 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
400 if (iface != JAYLINK_TIF_JTAG) {
401 LOG_ERROR("Device supports JTAG transport only.");
402 return ERROR_JTAG_INIT_FAILED;
403 }
404
405 return ERROR_OK;
406 }
407
408 ret = jaylink_get_available_interfaces(devh, &interfaces);
409
410 if (ret != JAYLINK_OK) {
411 LOG_ERROR("jaylink_get_available_interfaces() failed: %s.",
412 jaylink_strerror_name(ret));
413 return ERROR_JTAG_INIT_FAILED;
414 }
415
416 if (!(interfaces & (1 << iface))) {
417 LOG_ERROR("Selected transport is not supported by the device.");
418 return ERROR_JTAG_INIT_FAILED;
419 }
420
421 ret = jaylink_select_interface(devh, iface, NULL);
422
423 if (ret < 0) {
424 LOG_ERROR("jaylink_select_interface() failed: %s.",
425 jaylink_strerror_name(ret));
426 return ERROR_JTAG_INIT_FAILED;
427 }
428
429 return ERROR_OK;
430 }
431
432 static int jlink_register(void)
433 {
434 int ret;
435 size_t i;
436 bool handle_found;
437 size_t count;
438
439 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
440 return ERROR_OK;
441
442 ret = jaylink_register(devh, &conn, connlist, &count);
443
444 if (ret != JAYLINK_OK) {
445 LOG_ERROR("jaylink_register() failed: %s.",
446 jaylink_strerror_name(ret));
447 return ERROR_FAIL;
448 }
449
450 handle_found = false;
451
452 for (i = 0; i < count; i++) {
453 if (connlist[i].handle == conn.handle) {
454 handle_found = true;
455 break;
456 }
457 }
458
459 if (!handle_found) {
460 LOG_ERROR("Registration failed: maximum number of connections on the "
461 "device reached.");
462 return ERROR_FAIL;
463 }
464
465 return ERROR_OK;
466 }
467
468 /*
469 * Adjust the SWD transaction buffer size depending on the free device internal
470 * memory. This ensures that the SWD transactions sent to the device do not
471 * exceed the internal memory of the device.
472 */
473 static bool adjust_swd_buffer_size(void)
474 {
475 int ret;
476 uint32_t tmp;
477
478 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
479 return true;
480
481 ret = jaylink_get_free_memory(devh, &tmp);
482
483 if (ret != JAYLINK_OK) {
484 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
485 jaylink_strerror_name(ret));
486 return false;
487 }
488
489 if (tmp < 143) {
490 LOG_ERROR("Not enough free device internal memory: %u bytes.", tmp);
491 return false;
492 }
493
494 tmp = MIN(JLINK_TAP_BUFFER_SIZE, (tmp - 16) / 2);
495
496 if (tmp != swd_buffer_size) {
497 swd_buffer_size = tmp;
498 LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes.",
499 swd_buffer_size);
500 }
501
502 return true;
503 }
504
505 static int jaylink_log_handler(const struct jaylink_context *ctx,
506 enum jaylink_log_level level, const char *format, va_list args,
507 void *user_data)
508 {
509 enum log_levels tmp;
510
511 switch (level) {
512 case JAYLINK_LOG_LEVEL_ERROR:
513 tmp = LOG_LVL_ERROR;
514 break;
515 case JAYLINK_LOG_LEVEL_WARNING:
516 tmp = LOG_LVL_WARNING;
517 break;
518 /*
519 * Forward info messages to the debug output because they are more verbose
520 * than info messages of OpenOCD.
521 */
522 case JAYLINK_LOG_LEVEL_INFO:
523 case JAYLINK_LOG_LEVEL_DEBUG:
524 tmp = LOG_LVL_DEBUG;
525 break;
526 case JAYLINK_LOG_LEVEL_DEBUG_IO:
527 tmp = LOG_LVL_DEBUG_IO;
528 break;
529 default:
530 tmp = LOG_LVL_WARNING;
531 }
532
533 log_vprintf_lf(tmp, __FILE__, __LINE__, __func__, format, args);
534
535 return 0;
536 }
537
538 static int jlink_init(void)
539 {
540 int ret;
541 struct jaylink_device **devs;
542 unsigned int i;
543 bool found_device;
544 uint32_t tmp;
545 char *firmware_version;
546 struct jaylink_hardware_version hwver;
547 struct jaylink_hardware_status hwstatus;
548 enum jaylink_usb_address address;
549 size_t length;
550
551 LOG_DEBUG("Using libjaylink %s (compiled with %s).",
552 jaylink_version_package_get_string(), JAYLINK_VERSION_PACKAGE_STRING);
553
554 if (!jaylink_library_has_cap(JAYLINK_CAP_HIF_USB) && use_usb_address) {
555 LOG_ERROR("J-Link driver does not support USB devices.");
556 return ERROR_JTAG_INIT_FAILED;
557 }
558
559 ret = jaylink_init(&jayctx);
560
561 if (ret != JAYLINK_OK) {
562 LOG_ERROR("jaylink_init() failed: %s.",
563 jaylink_strerror_name(ret));
564 return ERROR_JTAG_INIT_FAILED;
565 }
566
567 ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
568
569 if (ret != JAYLINK_OK) {
570 LOG_ERROR("jaylink_log_set_callback() failed: %s.",
571 jaylink_strerror_name(ret));
572 jaylink_exit(jayctx);
573 return ERROR_JTAG_INIT_FAILED;
574 }
575
576 ret = jaylink_discovery_scan(jayctx, 0);
577
578 if (ret != JAYLINK_OK) {
579 LOG_ERROR("jaylink_discovery_scan() failed: %s.",
580 jaylink_strerror_name(ret));
581 jaylink_exit(jayctx);
582 return ERROR_JTAG_INIT_FAILED;
583 }
584
585 ret = jaylink_get_devices(jayctx, &devs, NULL);
586
587 if (ret != JAYLINK_OK) {
588 LOG_ERROR("jaylink_get_devices() failed: %s.",
589 jaylink_strerror_name(ret));
590 jaylink_exit(jayctx);
591 return ERROR_JTAG_INIT_FAILED;
592 }
593
594 found_device = false;
595
596 if (!use_serial_number && !use_usb_address)
597 LOG_INFO("No device selected, using first device.");
598
599 for (i = 0; devs[i]; i++) {
600 if (use_serial_number) {
601 ret = jaylink_device_get_serial_number(devs[i], &tmp);
602
603 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
604 continue;
605 } else if (ret != JAYLINK_OK) {
606 LOG_WARNING("jaylink_device_get_serial_number() failed: %s.",
607 jaylink_strerror_name(ret));
608 continue;
609 }
610
611 if (serial_number != tmp)
612 continue;
613 }
614
615 if (use_usb_address) {
616 ret = jaylink_device_get_usb_address(devs[i], &address);
617
618 if (ret == JAYLINK_ERR_NOT_SUPPORTED) {
619 continue;
620 } else if (ret != JAYLINK_OK) {
621 LOG_WARNING("jaylink_device_get_usb_address() failed: %s.",
622 jaylink_strerror_name(ret));
623 continue;
624 }
625
626 if (usb_address != address)
627 continue;
628 }
629
630 ret = jaylink_open(devs[i], &devh);
631
632 if (ret == JAYLINK_OK) {
633 found_device = true;
634 break;
635 }
636
637 LOG_ERROR("Failed to open device: %s.", jaylink_strerror_name(ret));
638 }
639
640 jaylink_free_devices(devs, true);
641
642 if (!found_device) {
643 LOG_ERROR("No J-Link device found.");
644 jaylink_exit(jayctx);
645 return ERROR_JTAG_INIT_FAILED;
646 }
647
648 /*
649 * Be careful with changing the following initialization sequence because
650 * some devices are known to be sensitive regarding the order.
651 */
652
653 ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
654
655 if (ret != JAYLINK_OK) {
656 LOG_ERROR("jaylink_get_firmware_version() failed: %s.",
657 jaylink_strerror_name(ret));
658 jaylink_close(devh);
659 jaylink_exit(jayctx);
660 return ERROR_JTAG_INIT_FAILED;
661 } else if (length > 0) {
662 LOG_INFO("%s", firmware_version);
663 free(firmware_version);
664 } else {
665 LOG_WARNING("Device responds empty firmware version string.");
666 }
667
668 memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
669 ret = jaylink_get_caps(devh, caps);
670
671 if (ret != JAYLINK_OK) {
672 LOG_ERROR("jaylink_get_caps() failed: %s.", jaylink_strerror_name(ret));
673 jaylink_close(devh);
674 jaylink_exit(jayctx);
675 return ERROR_JTAG_INIT_FAILED;
676 }
677
678 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
679 ret = jaylink_get_extended_caps(devh, caps);
680
681 if (ret != JAYLINK_OK) {
682 LOG_ERROR("jaylink_get_extended_caps() failed: %s.",
683 jaylink_strerror_name(ret));
684 jaylink_close(devh);
685 jaylink_exit(jayctx);
686 return ERROR_JTAG_INIT_FAILED;
687 }
688 }
689
690 jtag_command_version = JAYLINK_JTAG_VERSION_2;
691
692 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
693 ret = jaylink_get_hardware_version(devh, &hwver);
694
695 if (ret != JAYLINK_OK) {
696 LOG_ERROR("Failed to retrieve hardware version: %s.",
697 jaylink_strerror_name(ret));
698 jaylink_close(devh);
699 jaylink_exit(jayctx);
700 return ERROR_JTAG_INIT_FAILED;
701 }
702
703 LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
704
705 if (hwver.major >= 5)
706 jtag_command_version = JAYLINK_JTAG_VERSION_3;
707 }
708
709 if (iface == JAYLINK_TIF_SWD) {
710 /*
711 * Adjust the SWD transaction buffer size in case there is already
712 * allocated memory on the device. This happens for example if the
713 * memory for SWO capturing is still allocated because the software
714 * which used the device before has not been shut down properly.
715 */
716 if (!adjust_swd_buffer_size()) {
717 jaylink_close(devh);
718 jaylink_exit(jayctx);
719 return ERROR_JTAG_INIT_FAILED;
720 }
721 }
722
723 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
724 if (!read_device_config(&config)) {
725 LOG_ERROR("Failed to read device configuration data.");
726 jaylink_close(devh);
727 jaylink_exit(jayctx);
728 return ERROR_JTAG_INIT_FAILED;
729 }
730
731 memcpy(&tmp_config, &config, sizeof(struct device_config));
732 }
733
734 ret = jaylink_get_hardware_status(devh, &hwstatus);
735
736 if (ret != JAYLINK_OK) {
737 LOG_ERROR("jaylink_get_hardware_status() failed: %s.",
738 jaylink_strerror_name(ret));
739 jaylink_close(devh);
740 jaylink_exit(jayctx);
741 return ERROR_JTAG_INIT_FAILED;
742 }
743
744 LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
745 hwstatus.target_voltage % 1000);
746
747 conn.handle = 0;
748 conn.pid = 0;
749 strcpy(conn.hid, "0.0.0.0");
750 conn.iid = 0;
751 conn.cid = 0;
752
753 ret = jlink_register();
754
755 if (ret != ERROR_OK) {
756 jaylink_close(devh);
757 jaylink_exit(jayctx);
758 return ERROR_JTAG_INIT_FAILED;
759 }
760
761 ret = select_interface();
762
763 if (ret != ERROR_OK) {
764 jaylink_close(devh);
765 jaylink_exit(jayctx);
766 return ret;
767 }
768
769 jlink_reset(0, 0);
770 jtag_sleep(3000);
771 jlink_tap_init();
772
773 jlink_speed(jtag_get_speed_khz());
774
775 if (iface == JAYLINK_TIF_JTAG) {
776 /*
777 * J-Link devices with firmware version v5 and v6 seems to have an issue
778 * if the first tap move is not divisible by 8, so we send a TLR on
779 * first power up.
780 */
781 uint8_t tms = 0xff;
782 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
783
784 jlink_flush();
785 }
786
787 return ERROR_OK;
788 }
789
790 static int jlink_quit(void)
791 {
792 int ret;
793 size_t count;
794
795 if (trace_enabled) {
796 ret = jaylink_swo_stop(devh);
797
798 if (ret != JAYLINK_OK)
799 LOG_ERROR("jaylink_swo_stop() failed: %s.",
800 jaylink_strerror_name(ret));
801 }
802
803 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
804 ret = jaylink_unregister(devh, &conn, connlist, &count);
805
806 if (ret != JAYLINK_OK)
807 LOG_ERROR("jaylink_unregister() failed: %s.",
808 jaylink_strerror_name(ret));
809 }
810
811 jaylink_close(devh);
812 jaylink_exit(jayctx);
813
814 return ERROR_OK;
815 }
816
817 /***************************************************************************/
818 /* Queue command implementations */
819
820 static void jlink_end_state(tap_state_t state)
821 {
822 if (tap_is_state_stable(state))
823 tap_set_end_state(state);
824 else {
825 LOG_ERROR("BUG: %i is not a valid end state", state);
826 exit(-1);
827 }
828 }
829
830 /* Goes to the end state. */
831 static void jlink_state_move(void)
832 {
833 uint8_t tms_scan;
834 uint8_t tms_scan_bits;
835
836 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
837 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
838
839 jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
840
841 tap_set_state(tap_get_end_state());
842 }
843
844 static void jlink_path_move(int num_states, tap_state_t *path)
845 {
846 int i;
847 uint8_t tms = 0xff;
848
849 for (i = 0; i < num_states; i++) {
850 if (path[i] == tap_state_transition(tap_get_state(), false))
851 jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
852 else if (path[i] == tap_state_transition(tap_get_state(), true))
853 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
854 else {
855 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
856 tap_state_name(tap_get_state()), tap_state_name(path[i]));
857 exit(-1);
858 }
859
860 tap_set_state(path[i]);
861 }
862
863 tap_set_end_state(tap_get_state());
864 }
865
866 static void jlink_stableclocks(int num_cycles)
867 {
868 int i;
869
870 uint8_t tms = tap_get_state() == TAP_RESET;
871 /* Execute num_cycles. */
872 for (i = 0; i < num_cycles; i++)
873 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
874 }
875
876 static void jlink_runtest(int num_cycles)
877 {
878 tap_state_t saved_end_state = tap_get_end_state();
879
880 /* Only do a state_move when we're not already in IDLE. */
881 if (tap_get_state() != TAP_IDLE) {
882 jlink_end_state(TAP_IDLE);
883 jlink_state_move();
884 /* num_cycles--; */
885 }
886
887 jlink_stableclocks(num_cycles);
888
889 /* Finish in end_state. */
890 jlink_end_state(saved_end_state);
891
892 if (tap_get_state() != tap_get_end_state())
893 jlink_state_move();
894 }
895
896 static void jlink_reset(int trst, int srst)
897 {
898 LOG_DEBUG("TRST: %i, SRST: %i.", trst, srst);
899
900 /* Signals are active low. */
901 if (srst == 0)
902 jaylink_set_reset(devh);
903
904 if (srst == 1)
905 jaylink_clear_reset(devh);
906
907 if (trst == 1)
908 jaylink_jtag_clear_trst(devh);
909
910 if (trst == 0)
911 jaylink_jtag_set_trst(devh);
912 }
913
914 COMMAND_HANDLER(jlink_usb_command)
915 {
916 int tmp;
917
918 if (CMD_ARGC != 1) {
919 command_print(CMD_CTX, "Need exactly one argument for jlink usb.");
920 return ERROR_COMMAND_SYNTAX_ERROR;
921 }
922
923 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
924 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
925 return ERROR_FAIL;
926 }
927
928 if (tmp < JAYLINK_USB_ADDRESS_0 || tmp > JAYLINK_USB_ADDRESS_3) {
929 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
930 return ERROR_FAIL;
931 }
932
933 usb_address = tmp;
934
935 use_serial_number = false;
936 use_usb_address = true;
937
938 return ERROR_OK;
939 }
940
941 COMMAND_HANDLER(jlink_serial_command)
942 {
943 int ret;
944
945 if (CMD_ARGC != 1) {
946 command_print(CMD_CTX, "Need exactly one argument for jlink serial.");
947 return ERROR_COMMAND_SYNTAX_ERROR;
948 }
949
950 ret = jaylink_parse_serial_number(CMD_ARGV[0], &serial_number);
951
952 if (ret == JAYLINK_ERR) {
953 command_print(CMD_CTX, "Invalid serial number: %s.", CMD_ARGV[0]);
954 return ERROR_FAIL;
955 } else if (ret != JAYLINK_OK) {
956 command_print(CMD_CTX, "jaylink_parse_serial_number() failed: %s.",
957 jaylink_strerror_name(ret));
958 return ERROR_FAIL;
959 }
960
961 use_serial_number = true;
962 use_usb_address = false;
963
964 return ERROR_OK;
965 }
966
967 COMMAND_HANDLER(jlink_handle_hwstatus_command)
968 {
969 int ret;
970 struct jaylink_hardware_status status;
971
972 ret = jaylink_get_hardware_status(devh, &status);
973
974 if (ret != JAYLINK_OK) {
975 command_print(CMD_CTX, "jaylink_get_hardware_status() failed: %s.",
976 jaylink_strerror_name(ret));
977 return ERROR_FAIL;
978 }
979
980 command_print(CMD_CTX, "VTarget = %u.%03u V",
981 status.target_voltage / 1000, status.target_voltage % 1000);
982
983 command_print(CMD_CTX, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
984 "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
985 status.tres, status.trst);
986
987 if (status.target_voltage < 1500)
988 command_print(CMD_CTX, "Target voltage too low. Check target power.");
989
990 return ERROR_OK;
991 }
992
993 COMMAND_HANDLER(jlink_handle_free_memory_command)
994 {
995 int ret;
996 uint32_t tmp;
997
998 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
999 command_print(CMD_CTX, "Retrieval of free memory is not supported by "
1000 "the device.");
1001 return ERROR_OK;
1002 }
1003
1004 ret = jaylink_get_free_memory(devh, &tmp);
1005
1006 if (ret != JAYLINK_OK) {
1007 command_print(CMD_CTX, "jaylink_get_free_memory() failed: %s.",
1008 jaylink_strerror_name(ret));
1009 return ERROR_FAIL;
1010 }
1011
1012 command_print(CMD_CTX, "Device has %u bytes of free memory.", tmp);
1013
1014 return ERROR_OK;
1015 }
1016
1017 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1018 {
1019 int tmp;
1020 int version;
1021
1022 if (!CMD_ARGC) {
1023 switch (jtag_command_version) {
1024 case JAYLINK_JTAG_VERSION_2:
1025 version = 2;
1026 break;
1027 case JAYLINK_JTAG_VERSION_3:
1028 version = 3;
1029 break;
1030 default:
1031 return ERROR_FAIL;
1032 }
1033
1034 command_print(CMD_CTX, "JTAG command version: %i", version);
1035 } else if (CMD_ARGC == 1) {
1036 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
1037 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1038 return ERROR_COMMAND_SYNTAX_ERROR;
1039 }
1040
1041 switch (tmp) {
1042 case 2:
1043 jtag_command_version = JAYLINK_JTAG_VERSION_2;
1044 break;
1045 case 3:
1046 jtag_command_version = JAYLINK_JTAG_VERSION_3;
1047 break;
1048 default:
1049 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1050 return ERROR_COMMAND_SYNTAX_ERROR;
1051 }
1052 } else {
1053 command_print(CMD_CTX, "Need exactly one argument for jlink jtag.");
1054 return ERROR_COMMAND_SYNTAX_ERROR;
1055 }
1056
1057 return ERROR_OK;
1058 }
1059
1060 COMMAND_HANDLER(jlink_handle_target_power_command)
1061 {
1062 int ret;
1063 int enable;
1064
1065 if (CMD_ARGC != 1) {
1066 command_print(CMD_CTX, "Need exactly one argument for jlink "
1067 "targetpower.");
1068 return ERROR_COMMAND_SYNTAX_ERROR;
1069 }
1070
1071 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1072 command_print(CMD_CTX, "Target power supply is not supported by the "
1073 "device.");
1074 return ERROR_OK;
1075 }
1076
1077 if (!strcmp(CMD_ARGV[0], "on")) {
1078 enable = true;
1079 } else if (!strcmp(CMD_ARGV[0], "off")) {
1080 enable = false;
1081 } else {
1082 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1083 return ERROR_FAIL;
1084 }
1085
1086 ret = jaylink_set_target_power(devh, enable);
1087
1088 if (ret != JAYLINK_OK) {
1089 command_print(CMD_CTX, "jaylink_set_target_power() failed: %s.",
1090 jaylink_strerror_name(ret));
1091 return ERROR_FAIL;
1092 }
1093
1094 return ERROR_OK;
1095 }
1096
1097 static void show_config_usb_address(struct command_context *ctx)
1098 {
1099 if (config.usb_address != tmp_config.usb_address)
1100 command_print(ctx, "USB address: %u [%u]", config.usb_address,
1101 tmp_config.usb_address);
1102 else
1103 command_print(ctx, "USB address: %u", config.usb_address);
1104 }
1105
1106 static void show_config_ip_address(struct command_context *ctx)
1107 {
1108 if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1109 command_print(ctx, "IP address: %d.%d.%d.%d",
1110 config.ip_address[3], config.ip_address[2],
1111 config.ip_address[1], config.ip_address[0]);
1112 else
1113 command_print(ctx, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1114 config.ip_address[3], config.ip_address[2],
1115 config.ip_address[1], config.ip_address[0],
1116 tmp_config.ip_address[3], tmp_config.ip_address[2],
1117 tmp_config.ip_address[1], tmp_config.ip_address[0]);
1118
1119 if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1120 command_print(ctx, "Subnet mask: %d.%d.%d.%d",
1121 config.subnet_mask[3], config.subnet_mask[2],
1122 config.subnet_mask[1], config.subnet_mask[0]);
1123 else
1124 command_print(ctx, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1125 config.subnet_mask[3], config.subnet_mask[2],
1126 config.subnet_mask[1], config.subnet_mask[0],
1127 tmp_config.subnet_mask[3], tmp_config.subnet_mask[2],
1128 tmp_config.subnet_mask[1], tmp_config.subnet_mask[0]);
1129 }
1130
1131 static void show_config_mac_address(struct command_context *ctx)
1132 {
1133 if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1134 command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1135 config.mac_address[5], config.mac_address[4],
1136 config.mac_address[3], config.mac_address[2],
1137 config.mac_address[1], config.mac_address[0]);
1138 else
1139 command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1140 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1141 config.mac_address[5], config.mac_address[4],
1142 config.mac_address[3], config.mac_address[2],
1143 config.mac_address[1], config.mac_address[0],
1144 tmp_config.mac_address[5], tmp_config.mac_address[4],
1145 tmp_config.mac_address[3], tmp_config.mac_address[2],
1146 tmp_config.mac_address[1], tmp_config.mac_address[0]);
1147 }
1148
1149 static void show_config_target_power(struct command_context *ctx)
1150 {
1151 const char *target_power;
1152 const char *current_target_power;
1153
1154 if (!config.target_power)
1155 target_power = "off";
1156 else
1157 target_power = "on";
1158
1159 if (!tmp_config.target_power)
1160 current_target_power = "off";
1161 else
1162 current_target_power = "on";
1163
1164 if (config.target_power != tmp_config.target_power)
1165 command_print(ctx, "Target power supply: %s [%s]", target_power,
1166 current_target_power);
1167 else
1168 command_print(ctx, "Target power supply: %s", target_power);
1169 }
1170
1171 static void show_config(struct command_context *ctx)
1172 {
1173 command_print(ctx, "J-Link device configuration:");
1174
1175 show_config_usb_address(ctx);
1176
1177 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1178 show_config_target_power(ctx);
1179
1180 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1181 show_config_ip_address(ctx);
1182 show_config_mac_address(ctx);
1183 }
1184 }
1185
1186 static int poll_trace(uint8_t *buf, size_t *size)
1187 {
1188 int ret;
1189 uint32_t length;
1190
1191 length = *size;
1192
1193 ret = jaylink_swo_read(devh, buf, &length);
1194
1195 if (ret != JAYLINK_OK) {
1196 LOG_ERROR("jaylink_swo_read() failed: %s.", jaylink_strerror_name(ret));
1197 return ERROR_FAIL;
1198 }
1199
1200 *size = length;
1201
1202 return ERROR_OK;
1203 }
1204
1205 static uint32_t calculate_trace_buffer_size(void)
1206 {
1207 int ret;
1208 uint32_t tmp;
1209
1210 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1211 return 0;
1212
1213 ret = jaylink_get_free_memory(devh, &tmp);
1214
1215 if (ret != JAYLINK_OK) {
1216 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
1217 jaylink_strerror_name(ret));
1218 return ERROR_FAIL;
1219 }
1220
1221 if (tmp > 0x3fff || tmp <= 0x600)
1222 tmp = tmp >> 1;
1223 else
1224 tmp = tmp - 0x400;
1225
1226 return tmp & 0xffffff00;
1227 }
1228
1229 static bool check_trace_freq(struct jaylink_swo_speed speed,
1230 uint32_t trace_freq)
1231 {
1232 double min;
1233 double deviation;
1234 uint32_t divider;
1235
1236 min = fabs(1.0 - (speed.freq / ((double)trace_freq * speed.min_div)));
1237
1238 for (divider = speed.min_div; divider < speed.max_div; divider++) {
1239 deviation = fabs(1.0 - (speed.freq / ((double)trace_freq * divider)));
1240
1241 if (deviation < 0.03) {
1242 LOG_DEBUG("Found suitable frequency divider %u with deviation of "
1243 "%.02f %%.", divider, deviation);
1244 return true;
1245 }
1246
1247 if (deviation < min)
1248 min = deviation;
1249 }
1250
1251 LOG_ERROR("Selected trace frequency is not supported by the device. "
1252 "Please choose a different trace frequency.");
1253 LOG_ERROR("Maximum permitted deviation is 3.00 %%, but only %.02f %% "
1254 "could be achieved.", min * 100);
1255
1256 return false;
1257 }
1258
1259 static int config_trace(bool enabled, enum tpio_pin_protocol pin_protocol,
1260 uint32_t port_size, unsigned int *trace_freq)
1261 {
1262 int ret;
1263 uint32_t buffer_size;
1264 struct jaylink_swo_speed speed;
1265
1266 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1267 LOG_ERROR("Trace capturing is not supported by the device.");
1268 return ERROR_FAIL;
1269 }
1270
1271 if (pin_protocol != ASYNC_UART) {
1272 LOG_ERROR("Selected pin protocol is not supported.");
1273 return ERROR_FAIL;
1274 }
1275
1276 trace_enabled = enabled;
1277
1278 ret = jaylink_swo_stop(devh);
1279
1280 if (ret != JAYLINK_OK) {
1281 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror_name(ret));
1282 return ERROR_FAIL;
1283 }
1284
1285 if (!enabled) {
1286 /*
1287 * Adjust the SWD transaction buffer size as stopping SWO capturing
1288 * deallocates device internal memory.
1289 */
1290 if (!adjust_swd_buffer_size())
1291 return ERROR_FAIL;
1292
1293 return ERROR_OK;
1294 }
1295
1296 buffer_size = calculate_trace_buffer_size();
1297
1298 if (!buffer_size) {
1299 LOG_ERROR("Not enough free device memory to start trace capturing.");
1300 return ERROR_FAIL;
1301 }
1302
1303 ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1304
1305 if (ret != JAYLINK_OK) {
1306 LOG_ERROR("jaylink_swo_get_speeds() failed: %s.",
1307 jaylink_strerror_name(ret));
1308 return ERROR_FAIL;
1309 }
1310
1311 if (!*trace_freq)
1312 *trace_freq = speed.freq / speed.min_div;
1313
1314 if (!check_trace_freq(speed, *trace_freq))
1315 return ERROR_FAIL;
1316
1317 LOG_DEBUG("Using %u bytes device memory for trace capturing.", buffer_size);
1318
1319 ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1320 buffer_size);
1321
1322 if (ret != JAYLINK_OK) {
1323 LOG_ERROR("jaylink_start_swo() failed: %s.",
1324 jaylink_strerror_name(ret));
1325 return ERROR_FAIL;
1326 }
1327
1328 /*
1329 * Adjust the SWD transaction buffer size as starting SWO capturing
1330 * allocates device internal memory.
1331 */
1332 if (!adjust_swd_buffer_size())
1333 return ERROR_FAIL;
1334
1335 return ERROR_OK;
1336 }
1337
1338 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1339 {
1340 uint8_t tmp;
1341
1342 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1343 command_print(CMD_CTX, "Reading configuration is not supported by the "
1344 "device.");
1345 return ERROR_OK;
1346 }
1347
1348 if (!CMD_ARGC) {
1349 show_config_usb_address(CMD_CTX);
1350 } else if (CMD_ARGC == 1) {
1351 if (sscanf(CMD_ARGV[0], "%" SCNd8, &tmp) != 1) {
1352 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
1353 return ERROR_FAIL;
1354 }
1355
1356 if (tmp > JAYLINK_USB_ADDRESS_3) {
1357 command_print(CMD_CTX, "Invalid USB address: %u.", tmp);
1358 return ERROR_FAIL;
1359 }
1360
1361 tmp_config.usb_address = tmp;
1362 } else {
1363 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1364 "usb.");
1365 return ERROR_COMMAND_SYNTAX_ERROR;
1366 }
1367
1368 return ERROR_OK;
1369 }
1370
1371 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1372 {
1373 int enable;
1374
1375 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1376 command_print(CMD_CTX, "Reading configuration is not supported by the "
1377 "device.");
1378 return ERROR_OK;
1379 }
1380
1381 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1382 command_print(CMD_CTX, "Target power supply is not supported by the "
1383 "device.");
1384 return ERROR_OK;
1385 }
1386
1387 if (!CMD_ARGC) {
1388 show_config_target_power(CMD_CTX);
1389 } else if (CMD_ARGC == 1) {
1390 if (!strcmp(CMD_ARGV[0], "on")) {
1391 enable = true;
1392 } else if (!strcmp(CMD_ARGV[0], "off")) {
1393 enable = false;
1394 } else {
1395 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1396 return ERROR_FAIL;
1397 }
1398
1399 tmp_config.target_power = enable;
1400 } else {
1401 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1402 "targetpower.");
1403 return ERROR_COMMAND_SYNTAX_ERROR;
1404 }
1405
1406 return ERROR_OK;
1407 }
1408
1409 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1410 {
1411 uint8_t addr[6];
1412 int i;
1413 char *e;
1414 const char *str;
1415
1416 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1417 command_print(CMD_CTX, "Reading configuration is not supported by the "
1418 "device.");
1419 return ERROR_OK;
1420 }
1421
1422 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1423 command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
1424 "device.");
1425 return ERROR_OK;
1426 }
1427
1428 if (!CMD_ARGC) {
1429 show_config_mac_address(CMD_CTX);
1430 } else if (CMD_ARGC == 1) {
1431 str = CMD_ARGV[0];
1432
1433 if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || \
1434 str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1435 command_print(CMD_CTX, "Invalid MAC address format.");
1436 return ERROR_COMMAND_SYNTAX_ERROR;
1437 }
1438
1439 for (i = 5; i >= 0; i--) {
1440 addr[i] = strtoul(str, &e, 16);
1441 str = e + 1;
1442 }
1443
1444 if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1445 command_print(CMD_CTX, "Invalid MAC address: zero address.");
1446 return ERROR_COMMAND_SYNTAX_ERROR;
1447 }
1448
1449 if (!(0x01 & addr[0])) {
1450 command_print(CMD_CTX, "Invalid MAC address: multicast address.");
1451 return ERROR_COMMAND_SYNTAX_ERROR;
1452 }
1453
1454 memcpy(tmp_config.mac_address, addr, sizeof(addr));
1455 } else {
1456 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1457 " mac.");
1458 return ERROR_COMMAND_SYNTAX_ERROR;
1459 }
1460
1461 return ERROR_OK;
1462 }
1463
1464 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1465 {
1466 uint8_t lip[4];
1467 char *e;
1468 const char *s_save = s;
1469 int i;
1470
1471 if (!s)
1472 return false;
1473
1474 for (i = 0; i < 4; i++) {
1475 lip[i] = strtoul(s, &e, 10);
1476
1477 if (*e != '.' && i != 3)
1478 return false;
1479
1480 s = e + 1;
1481 }
1482
1483 *pos = e - s_save;
1484 memcpy(ip, lip, sizeof(lip));
1485
1486 return true;
1487 }
1488
1489 static void cpy_ip(uint8_t *dst, uint8_t *src)
1490 {
1491 int i, j;
1492
1493 for (i = 0, j = 3; i < 4; i++, j--)
1494 dst[i] = src[j];
1495 }
1496
1497 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1498 {
1499 uint8_t ip_address[4];
1500 uint32_t subnet_mask = 0;
1501 int i, len;
1502 uint8_t subnet_bits = 24;
1503
1504 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1505 command_print(CMD_CTX, "Reading configuration is not supported by the "
1506 "device.");
1507 return ERROR_OK;
1508 }
1509
1510 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1511 command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
1512 "device.");
1513 return ERROR_OK;
1514 }
1515
1516 if (!CMD_ARGC) {
1517 show_config_ip_address(CMD_CTX);
1518 } else {
1519 if (!string_to_ip(CMD_ARGV[0], ip_address, &i))
1520 return ERROR_COMMAND_SYNTAX_ERROR;
1521
1522 len = strlen(CMD_ARGV[0]);
1523
1524 /* Check for format A.B.C.D/E. */
1525 if (i < len) {
1526 if (CMD_ARGV[0][i] != '/')
1527 return ERROR_COMMAND_SYNTAX_ERROR;
1528
1529 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1530 } else if (CMD_ARGC > 1) {
1531 if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i))
1532 return ERROR_COMMAND_SYNTAX_ERROR;
1533 }
1534
1535 if (!subnet_mask)
1536 subnet_mask = (uint32_t)(subnet_bits < 32 ?
1537 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1538
1539 cpy_ip(tmp_config.ip_address, ip_address);
1540 cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1541 }
1542
1543 return ERROR_OK;
1544 }
1545
1546 COMMAND_HANDLER(jlink_handle_config_reset_command)
1547 {
1548 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1549 return ERROR_OK;
1550
1551 memcpy(&tmp_config, &config, sizeof(struct device_config));
1552
1553 return ERROR_OK;
1554 }
1555
1556
1557 COMMAND_HANDLER(jlink_handle_config_write_command)
1558 {
1559 int ret;
1560
1561 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1562 command_print(CMD_CTX, "Reading configuration is not supported by the "
1563 "device.");
1564 return ERROR_OK;
1565 }
1566
1567 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1568 command_print(CMD_CTX, "Writing configuration is not supported by the "
1569 "device.");
1570 return ERROR_OK;
1571 }
1572
1573 if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1574 command_print(CMD_CTX, "Operation not performed due to no changes in "
1575 "the configuration.");
1576 return ERROR_OK;
1577 }
1578
1579 ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1580
1581 if (ret != JAYLINK_OK) {
1582 LOG_ERROR("jaylink_write_raw_config() failed: %s.",
1583 jaylink_strerror_name(ret));
1584 return ERROR_FAIL;
1585 }
1586
1587 if (!read_device_config(&config)) {
1588 LOG_ERROR("Failed to read device configuration for verification.");
1589 return ERROR_FAIL;
1590 }
1591
1592 if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1593 LOG_ERROR("Verification of device configuration failed. Please check "
1594 "your device.");
1595 return ERROR_FAIL;
1596 }
1597
1598 memcpy(&tmp_config, &config, sizeof(struct device_config));
1599 command_print(CMD_CTX, "The new device configuration applies after power "
1600 "cycling the J-Link device.");
1601
1602 return ERROR_OK;
1603 }
1604
1605 COMMAND_HANDLER(jlink_handle_config_command)
1606 {
1607 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1608 command_print(CMD_CTX, "Device doesn't support reading configuration.");
1609 return ERROR_OK;
1610 }
1611
1612 if (CMD_ARGC == 0)
1613 show_config(CMD_CTX);
1614
1615 return ERROR_OK;
1616 }
1617
1618 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1619 {
1620 int ret;
1621 size_t tmp;
1622 uint32_t channel;
1623 uint32_t length;
1624 uint8_t *buf;
1625 size_t dummy;
1626
1627 if (CMD_ARGC != 2)
1628 return ERROR_COMMAND_SYNTAX_ERROR;
1629
1630 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1631 LOG_ERROR("Device does not support EMUCOM.");
1632 return ERROR_FAIL;
1633 }
1634
1635 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1636
1637 tmp = strlen(CMD_ARGV[1]);
1638
1639 if (tmp % 2 != 0) {
1640 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1641 return ERROR_COMMAND_ARGUMENT_INVALID;
1642 }
1643
1644 buf = malloc(tmp / 2);
1645
1646 if (!buf) {
1647 LOG_ERROR("Failed to allocate buffer.");
1648 return ERROR_FAIL;
1649 }
1650
1651 dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1652
1653 if (dummy != (tmp / 2)) {
1654 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1655 free(buf);
1656 return ERROR_COMMAND_ARGUMENT_INVALID;
1657 }
1658
1659 length = tmp / 2;
1660 ret = jaylink_emucom_write(devh, channel, buf, &length);
1661
1662 free(buf);
1663
1664 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1665 LOG_ERROR("Channel not supported by the device.");
1666 return ERROR_FAIL;
1667 } else if (ret != JAYLINK_OK) {
1668 LOG_ERROR("Failed to write to channel: %s.",
1669 jaylink_strerror_name(ret));
1670 return ERROR_FAIL;
1671 }
1672
1673 if (length != (tmp / 2))
1674 LOG_WARNING("Only %" PRIu32 " bytes written to the channel.", length);
1675
1676 return ERROR_OK;
1677 }
1678
1679 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1680 {
1681 int ret;
1682 uint32_t channel;
1683 uint32_t length;
1684 uint8_t *buf;
1685 size_t tmp;
1686
1687 if (CMD_ARGC != 2)
1688 return ERROR_COMMAND_SYNTAX_ERROR;
1689
1690 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1691 LOG_ERROR("Device does not support EMUCOM.");
1692 return ERROR_FAIL;
1693 }
1694
1695 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1696 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
1697
1698 buf = malloc(length * 3 + 1);
1699
1700 if (!buf) {
1701 LOG_ERROR("Failed to allocate buffer.");
1702 return ERROR_FAIL;
1703 }
1704
1705 ret = jaylink_emucom_read(devh, channel, buf, &length);
1706
1707 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1708 LOG_ERROR("Channel is not supported by the device.");
1709 free(buf);
1710 return ERROR_FAIL;
1711 } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1712 LOG_ERROR("Channel is not available for the requested amount of data. "
1713 "%" PRIu32 " bytes are avilable.", length);
1714 free(buf);
1715 return ERROR_FAIL;
1716 } else if (ret != JAYLINK_OK) {
1717 LOG_ERROR("Failed to read from channel: %s.",
1718 jaylink_strerror_name(ret));
1719 free(buf);
1720 return ERROR_FAIL;
1721 }
1722
1723 tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1724
1725 if (tmp != 2 * length) {
1726 LOG_ERROR("Failed to convert data into hexadecimal string.");
1727 free(buf);
1728 return ERROR_FAIL;
1729 }
1730
1731 command_print(CMD_CTX, "%s", buf + length);
1732 free(buf);
1733
1734 return ERROR_OK;
1735 }
1736
1737 static const struct command_registration jlink_config_subcommand_handlers[] = {
1738 {
1739 .name = "usb",
1740 .handler = &jlink_handle_config_usb_address_command,
1741 .mode = COMMAND_EXEC,
1742 .help = "set the USB address",
1743 .usage = "[0-3]",
1744 },
1745 {
1746 .name = "targetpower",
1747 .handler = &jlink_handle_config_target_power_command,
1748 .mode = COMMAND_EXEC,
1749 .help = "set the target power supply",
1750 .usage = "[on|off]"
1751 },
1752 {
1753 .name = "mac",
1754 .handler = &jlink_handle_config_mac_address_command,
1755 .mode = COMMAND_EXEC,
1756 .help = "set the MAC Address",
1757 .usage = "[ff:ff:ff:ff:ff:ff]",
1758 },
1759 {
1760 .name = "ip",
1761 .handler = &jlink_handle_config_ip_address_command,
1762 .mode = COMMAND_EXEC,
1763 .help = "set the IP address, where A.B.C.D is the IP address, "
1764 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1765 .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1766 },
1767 {
1768 .name = "reset",
1769 .handler = &jlink_handle_config_reset_command,
1770 .mode = COMMAND_EXEC,
1771 .help = "undo configuration changes"
1772 },
1773 {
1774 .name = "write",
1775 .handler = &jlink_handle_config_write_command,
1776 .mode = COMMAND_EXEC,
1777 .help = "write configuration to the device"
1778 },
1779 COMMAND_REGISTRATION_DONE
1780 };
1781
1782 static const struct command_registration jlink_emucom_subcommand_handlers[] = {
1783 {
1784 .name = "write",
1785 .handler = &jlink_handle_emucom_write_command,
1786 .mode = COMMAND_EXEC,
1787 .help = "write to a channel",
1788 .usage = "<channel> <data>",
1789 },
1790 {
1791 .name = "read",
1792 .handler = &jlink_handle_emucom_read_command,
1793 .mode = COMMAND_EXEC,
1794 .help = "read from a channel",
1795 .usage = "<channel> <length>"
1796 },
1797 COMMAND_REGISTRATION_DONE
1798 };
1799
1800 static const struct command_registration jlink_subcommand_handlers[] = {
1801 {
1802 .name = "jtag",
1803 .handler = &jlink_handle_jlink_jtag_command,
1804 .mode = COMMAND_EXEC,
1805 .help = "select the JTAG command version",
1806 .usage = "[2|3]",
1807 },
1808 {
1809 .name = "targetpower",
1810 .handler = &jlink_handle_target_power_command,
1811 .mode = COMMAND_EXEC,
1812 .help = "set the target power supply",
1813 .usage = "<on|off>"
1814 },
1815 {
1816 .name = "freemem",
1817 .handler = &jlink_handle_free_memory_command,
1818 .mode = COMMAND_EXEC,
1819 .help = "show free device memory"
1820 },
1821 {
1822 .name = "hwstatus",
1823 .handler = &jlink_handle_hwstatus_command,
1824 .mode = COMMAND_EXEC,
1825 .help = "show the hardware status"
1826 },
1827 {
1828 .name = "usb",
1829 .handler = &jlink_usb_command,
1830 .mode = COMMAND_CONFIG,
1831 .help = "set the USB address of the device that should be used",
1832 .usage = "<0-3>"
1833 },
1834 {
1835 .name = "serial",
1836 .handler = &jlink_serial_command,
1837 .mode = COMMAND_CONFIG,
1838 .help = "set the serial number of the device that should be used",
1839 .usage = "<serial number>"
1840 },
1841 {
1842 .name = "config",
1843 .handler = &jlink_handle_config_command,
1844 .mode = COMMAND_EXEC,
1845 .help = "access the device configuration. If no argument is given "
1846 "this will show the device configuration",
1847 .chain = jlink_config_subcommand_handlers,
1848 },
1849 {
1850 .name = "emucom",
1851 .mode = COMMAND_EXEC,
1852 .help = "access EMUCOM channel",
1853 .chain = jlink_emucom_subcommand_handlers
1854 },
1855 COMMAND_REGISTRATION_DONE
1856 };
1857
1858 static const struct command_registration jlink_command_handlers[] = {
1859 {
1860 .name = "jlink",
1861 .mode = COMMAND_ANY,
1862 .help = "perform jlink management",
1863 .chain = jlink_subcommand_handlers,
1864 },
1865 COMMAND_REGISTRATION_DONE
1866 };
1867
1868 static int jlink_swd_init(void)
1869 {
1870 iface = JAYLINK_TIF_SWD;
1871
1872 return ERROR_OK;
1873 }
1874
1875 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1876 {
1877 assert(!(cmd & SWD_CMD_RnW));
1878 jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1879 }
1880
1881 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1882 {
1883 assert(cmd & SWD_CMD_RnW);
1884 jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1885 }
1886
1887 static int_least32_t jlink_swd_frequency(int_least32_t hz)
1888 {
1889 if (hz > 0)
1890 jlink_speed(hz / 1000);
1891
1892 return hz;
1893 }
1894
1895 /***************************************************************************/
1896 /* J-Link tap functions */
1897
1898 static unsigned tap_length;
1899 /* In SWD mode use tms buffer for direction control */
1900 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
1901 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
1902 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
1903
1904 struct pending_scan_result {
1905 /** First bit position in tdo_buffer to read. */
1906 unsigned first;
1907 /** Number of bits to read. */
1908 unsigned length;
1909 /** Location to store the result */
1910 void *buffer;
1911 /** Offset in the destination buffer */
1912 unsigned buffer_offset;
1913 };
1914
1915 #define MAX_PENDING_SCAN_RESULTS 256
1916
1917 static int pending_scan_results_length;
1918 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1919
1920 static void jlink_tap_init(void)
1921 {
1922 tap_length = 0;
1923 pending_scan_results_length = 0;
1924 memset(tms_buffer, 0, sizeof(tms_buffer));
1925 memset(tdi_buffer, 0, sizeof(tdi_buffer));
1926 }
1927
1928 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
1929 const uint8_t *tms_out, unsigned tms_offset,
1930 uint8_t *in, unsigned in_offset,
1931 unsigned length)
1932 {
1933 do {
1934 unsigned available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
1935
1936 if (!available_length ||
1937 (in && pending_scan_results_length == MAX_PENDING_SCAN_RESULTS)) {
1938 if (jlink_flush() != ERROR_OK)
1939 return;
1940 available_length = JLINK_TAP_BUFFER_SIZE;
1941 }
1942
1943 struct pending_scan_result *pending_scan_result =
1944 &pending_scan_results_buffer[pending_scan_results_length];
1945
1946 unsigned scan_length = length > available_length ?
1947 available_length : length;
1948
1949 if (out)
1950 buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
1951 if (tms_out)
1952 buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
1953
1954 if (in) {
1955 pending_scan_result->first = tap_length;
1956 pending_scan_result->length = scan_length;
1957 pending_scan_result->buffer = in;
1958 pending_scan_result->buffer_offset = in_offset;
1959 pending_scan_results_length++;
1960 }
1961
1962 tap_length += scan_length;
1963 out_offset += scan_length;
1964 tms_offset += scan_length;
1965 in_offset += scan_length;
1966 length -= scan_length;
1967 } while (length > 0);
1968 }
1969
1970 static int jlink_flush(void)
1971 {
1972 int i;
1973 int ret;
1974
1975 if (!tap_length)
1976 return ERROR_OK;
1977
1978 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
1979 tap_length, jlink_last_state);
1980
1981 ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
1982 tap_length, jtag_command_version);
1983
1984 if (ret != JAYLINK_OK) {
1985 LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror_name(ret));
1986 jlink_tap_init();
1987 return ERROR_JTAG_QUEUE_FAILED;
1988 }
1989
1990 for (i = 0; i < pending_scan_results_length; i++) {
1991 struct pending_scan_result *p = &pending_scan_results_buffer[i];
1992
1993 buf_set_buf(tdo_buffer, p->first, p->buffer,
1994 p->buffer_offset, p->length);
1995
1996 DEBUG_JTAG_IO("Pending scan result, length = %d.", p->length);
1997 }
1998
1999 jlink_tap_init();
2000
2001 return ERROR_OK;
2002 }
2003
2004 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
2005 {
2006 unsigned int tap_pos = tap_length;
2007
2008 while (len > 32) {
2009 buf_set_u32(buf, tap_pos, 32, val);
2010 len -= 32;
2011 tap_pos += 32;
2012 }
2013
2014 if (len)
2015 buf_set_u32(buf, tap_pos, len, val);
2016 }
2017
2018 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2019 {
2020 const uint32_t dir_out = 0xffffffff;
2021
2022 if (data)
2023 bit_copy(tdi_buffer, tap_length, data, 0, len);
2024 else
2025 fill_buffer(tdi_buffer, 0, len);
2026
2027 fill_buffer(tms_buffer, dir_out, len);
2028 tap_length += len;
2029 }
2030
2031 static void jlink_queue_data_in(uint32_t len)
2032 {
2033 const uint32_t dir_in = 0;
2034
2035 fill_buffer(tms_buffer, dir_in, len);
2036 tap_length += len;
2037 }
2038
2039 static int jlink_swd_switch_seq(enum swd_special_seq seq)
2040 {
2041 const uint8_t *s;
2042 unsigned int s_len;
2043
2044 switch (seq) {
2045 case LINE_RESET:
2046 LOG_DEBUG("SWD line reset");
2047 s = swd_seq_line_reset;
2048 s_len = swd_seq_line_reset_len;
2049 break;
2050 case JTAG_TO_SWD:
2051 LOG_DEBUG("JTAG-to-SWD");
2052 s = swd_seq_jtag_to_swd;
2053 s_len = swd_seq_jtag_to_swd_len;
2054 break;
2055 case SWD_TO_JTAG:
2056 LOG_DEBUG("SWD-to-JTAG");
2057 s = swd_seq_swd_to_jtag;
2058 s_len = swd_seq_swd_to_jtag_len;
2059 break;
2060 default:
2061 LOG_ERROR("Sequence %d not supported.", seq);
2062 return ERROR_FAIL;
2063 }
2064
2065 jlink_queue_data_out(s, s_len);
2066
2067 return ERROR_OK;
2068 }
2069
2070 static int jlink_swd_run_queue(void)
2071 {
2072 int i;
2073 int ret;
2074
2075 LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length);
2076
2077 if (queued_retval != ERROR_OK) {
2078 LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval);
2079 goto skip;
2080 }
2081
2082 /*
2083 * A transaction must be followed by another transaction or at least 8 idle
2084 * cycles to ensure that data is clocked through the AP.
2085 */
2086 jlink_queue_data_out(NULL, 8);
2087
2088 ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2089
2090 if (ret != JAYLINK_OK) {
2091 LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror_name(ret));
2092 goto skip;
2093 }
2094
2095 for (i = 0; i < pending_scan_results_length; i++) {
2096 int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
2097
2098 if (ack != SWD_ACK_OK) {
2099 LOG_DEBUG("SWD ack not OK: %d %s", ack,
2100 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2101 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
2102 goto skip;
2103 } else if (pending_scan_results_buffer[i].length) {
2104 uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2105 int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
2106
2107 if (parity != parity_u32(data)) {
2108 LOG_ERROR("SWD: Read data parity mismatch.");
2109 queued_retval = ERROR_FAIL;
2110 goto skip;
2111 }
2112
2113 if (pending_scan_results_buffer[i].buffer)
2114 *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2115 }
2116 }
2117
2118 skip:
2119 jlink_tap_init();
2120 ret = queued_retval;
2121 queued_retval = ERROR_OK;
2122
2123 return ret;
2124 }
2125
2126 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2127 {
2128 uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2129 if (tap_length + 46 + 8 + ap_delay_clk >= sizeof(tdi_buffer) * 8 ||
2130 pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
2131 /* Not enough room in the queue. Run the queue. */
2132 queued_retval = jlink_swd_run_queue();
2133 }
2134
2135 if (queued_retval != ERROR_OK)
2136 return;
2137
2138 cmd |= SWD_CMD_START | SWD_CMD_PARK;
2139
2140 jlink_queue_data_out(&cmd, 8);
2141
2142 pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
2143
2144 if (cmd & SWD_CMD_RnW) {
2145 /* Queue a read transaction. */
2146 pending_scan_results_buffer[pending_scan_results_length].length = 32;
2147 pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
2148
2149 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2150 } else {
2151 /* Queue a write transaction. */
2152 pending_scan_results_buffer[pending_scan_results_length].length = 0;
2153 jlink_queue_data_in(1 + 3 + 1);
2154
2155 buf_set_u32(data_parity_trn, 0, 32, data);
2156 buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2157
2158 jlink_queue_data_out(data_parity_trn, 32 + 1);
2159 }
2160
2161 pending_scan_results_length++;
2162
2163 /* Insert idle cycles after AP accesses to avoid WAIT. */
2164 if (cmd & SWD_CMD_APnDP)
2165 jlink_queue_data_out(NULL, ap_delay_clk);
2166 }
2167
2168 static const struct swd_driver jlink_swd = {
2169 .init = &jlink_swd_init,
2170 .frequency = &jlink_swd_frequency,
2171 .switch_seq = &jlink_swd_switch_seq,
2172 .read_reg = &jlink_swd_read_reg,
2173 .write_reg = &jlink_swd_write_reg,
2174 .run = &jlink_swd_run_queue,
2175 };
2176
2177 static const char * const jlink_transports[] = { "jtag", "swd", NULL };
2178
2179 struct jtag_interface jlink_interface = {
2180 .name = "jlink",
2181 .commands = jlink_command_handlers,
2182 .transports = jlink_transports,
2183 .swd = &jlink_swd,
2184 .execute_queue = &jlink_execute_queue,
2185 .speed = &jlink_speed,
2186 .speed_div = &jlink_speed_div,
2187 .khz = &jlink_khz,
2188 .init = &jlink_init,
2189 .quit = &jlink_quit,
2190 .config_trace = &config_trace,
2191 .poll_trace = &poll_trace,
2192 };

Linking to existing account procedure

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

SSH host keys fingerprints

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