libjaylink: Update to latest master branch
[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 default:
527 tmp = LOG_LVL_WARNING;
528 }
529
530 log_vprintf_lf(tmp, __FILE__, __LINE__, __func__, format, args);
531
532 return 0;
533 }
534
535 static int jlink_init(void)
536 {
537 int ret;
538 struct jaylink_device **devs;
539 unsigned int i;
540 bool found_device;
541 uint32_t tmp;
542 char *firmware_version;
543 struct jaylink_hardware_version hwver;
544 struct jaylink_hardware_status hwstatus;
545 enum jaylink_usb_address address;
546 size_t length;
547
548 ret = jaylink_init(&jayctx);
549
550 if (ret != JAYLINK_OK) {
551 LOG_ERROR("jaylink_init() failed: %s.",
552 jaylink_strerror_name(ret));
553 return ERROR_JTAG_INIT_FAILED;
554 }
555
556 ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
557
558 if (ret != JAYLINK_OK) {
559 LOG_ERROR("jaylink_log_set_callback() failed: %s.",
560 jaylink_strerror_name(ret));
561 jaylink_exit(jayctx);
562 return ERROR_JTAG_INIT_FAILED;
563 }
564
565 ret = jaylink_discovery_scan(jayctx, 0);
566
567 if (ret != JAYLINK_OK) {
568 LOG_ERROR("jaylink_discovery_scan() failed: %s.",
569 jaylink_strerror_name(ret));
570 jaylink_exit(jayctx);
571 return ERROR_JTAG_INIT_FAILED;
572 }
573
574 ret = jaylink_get_devices(jayctx, &devs, NULL);
575
576 if (ret != JAYLINK_OK) {
577 LOG_ERROR("jaylink_get_devices() failed: %s.",
578 jaylink_strerror_name(ret));
579 jaylink_exit(jayctx);
580 return ERROR_JTAG_INIT_FAILED;
581 }
582
583 found_device = false;
584
585 if (!use_serial_number && !use_usb_address)
586 LOG_INFO("No device selected, using first device.");
587
588 for (i = 0; devs[i]; i++) {
589 if (use_serial_number) {
590 ret = jaylink_device_get_serial_number(devs[i], &tmp);
591
592 if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
593 continue;
594 } else if (ret != JAYLINK_OK) {
595 LOG_WARNING("jaylink_device_get_serial_number() failed: %s.",
596 jaylink_strerror_name(ret));
597 continue;
598 }
599
600 if (serial_number != tmp)
601 continue;
602 }
603
604 if (use_usb_address) {
605 ret = jaylink_device_get_usb_address(devs[i], &address);
606
607 if (ret != JAYLINK_OK) {
608 LOG_WARNING("jaylink_device_get_usb_address() failed: %s.",
609 jaylink_strerror_name(ret));
610 continue;
611 }
612
613 if (usb_address != address)
614 continue;
615 }
616
617 ret = jaylink_open(devs[i], &devh);
618
619 if (ret == JAYLINK_OK) {
620 found_device = true;
621 break;
622 }
623
624 LOG_ERROR("Failed to open device: %s.", jaylink_strerror_name(ret));
625 }
626
627 jaylink_free_devices(devs, true);
628
629 if (!found_device) {
630 LOG_ERROR("No J-Link device found.");
631 jaylink_exit(jayctx);
632 return ERROR_JTAG_INIT_FAILED;
633 }
634
635 /*
636 * Be careful with changing the following initialization sequence because
637 * some devices are known to be sensitive regarding the order.
638 */
639
640 ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
641
642 if (ret != JAYLINK_OK) {
643 LOG_ERROR("jaylink_get_firmware_version() failed: %s.",
644 jaylink_strerror_name(ret));
645 jaylink_close(devh);
646 jaylink_exit(jayctx);
647 return ERROR_JTAG_INIT_FAILED;
648 } else if (length > 0) {
649 LOG_INFO("%s", firmware_version);
650 free(firmware_version);
651 } else {
652 LOG_WARNING("Device responds empty firmware version string.");
653 }
654
655 memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
656 ret = jaylink_get_caps(devh, caps);
657
658 if (ret != JAYLINK_OK) {
659 LOG_ERROR("jaylink_get_caps() failed: %s.", jaylink_strerror_name(ret));
660 jaylink_close(devh);
661 jaylink_exit(jayctx);
662 return ERROR_JTAG_INIT_FAILED;
663 }
664
665 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
666 ret = jaylink_get_extended_caps(devh, caps);
667
668 if (ret != JAYLINK_OK) {
669 LOG_ERROR("jaylink_get_extended_caps() failed: %s.",
670 jaylink_strerror_name(ret));
671 jaylink_close(devh);
672 jaylink_exit(jayctx);
673 return ERROR_JTAG_INIT_FAILED;
674 }
675 }
676
677 jtag_command_version = JAYLINK_JTAG_VERSION_2;
678
679 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
680 ret = jaylink_get_hardware_version(devh, &hwver);
681
682 if (ret != JAYLINK_OK) {
683 LOG_ERROR("Failed to retrieve hardware version: %s.",
684 jaylink_strerror_name(ret));
685 jaylink_close(devh);
686 jaylink_exit(jayctx);
687 return ERROR_JTAG_INIT_FAILED;
688 }
689
690 LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
691
692 if (hwver.major >= 5)
693 jtag_command_version = JAYLINK_JTAG_VERSION_3;
694 }
695
696 if (iface == JAYLINK_TIF_SWD) {
697 /*
698 * Adjust the SWD transaction buffer size in case there is already
699 * allocated memory on the device. This happens for example if the
700 * memory for SWO capturing is still allocated because the software
701 * which used the device before has not been shut down properly.
702 */
703 if (!adjust_swd_buffer_size()) {
704 jaylink_close(devh);
705 jaylink_exit(jayctx);
706 return ERROR_JTAG_INIT_FAILED;
707 }
708 }
709
710 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
711 if (!read_device_config(&config)) {
712 LOG_ERROR("Failed to read device configuration data.");
713 jaylink_close(devh);
714 jaylink_exit(jayctx);
715 return ERROR_JTAG_INIT_FAILED;
716 }
717
718 memcpy(&tmp_config, &config, sizeof(struct device_config));
719 }
720
721 ret = jaylink_get_hardware_status(devh, &hwstatus);
722
723 if (ret != JAYLINK_OK) {
724 LOG_ERROR("jaylink_get_hardware_status() failed: %s.",
725 jaylink_strerror_name(ret));
726 jaylink_close(devh);
727 jaylink_exit(jayctx);
728 return ERROR_JTAG_INIT_FAILED;
729 }
730
731 LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
732 hwstatus.target_voltage % 1000);
733
734 conn.handle = 0;
735 conn.pid = 0;
736 strcpy(conn.hid, "0.0.0.0");
737 conn.iid = 0;
738 conn.cid = 0;
739
740 ret = jlink_register();
741
742 if (ret != ERROR_OK) {
743 jaylink_close(devh);
744 jaylink_exit(jayctx);
745 return ERROR_JTAG_INIT_FAILED;
746 }
747
748 ret = select_interface();
749
750 if (ret != ERROR_OK) {
751 jaylink_close(devh);
752 jaylink_exit(jayctx);
753 return ret;
754 }
755
756 jlink_reset(0, 0);
757 jtag_sleep(3000);
758 jlink_tap_init();
759
760 jlink_speed(jtag_get_speed_khz());
761
762 if (iface == JAYLINK_TIF_JTAG) {
763 /*
764 * J-Link devices with firmware version v5 and v6 seems to have an issue
765 * if the first tap move is not divisible by 8, so we send a TLR on
766 * first power up.
767 */
768 uint8_t tms = 0xff;
769 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
770
771 jlink_flush();
772 }
773
774 return ERROR_OK;
775 }
776
777 static int jlink_quit(void)
778 {
779 int ret;
780 size_t count;
781
782 if (trace_enabled) {
783 ret = jaylink_swo_stop(devh);
784
785 if (ret != JAYLINK_OK)
786 LOG_ERROR("jaylink_swo_stop() failed: %s.",
787 jaylink_strerror_name(ret));
788 }
789
790 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
791 ret = jaylink_unregister(devh, &conn, connlist, &count);
792
793 if (ret != JAYLINK_OK)
794 LOG_ERROR("jaylink_unregister() failed: %s.",
795 jaylink_strerror_name(ret));
796 }
797
798 jaylink_close(devh);
799 jaylink_exit(jayctx);
800
801 return ERROR_OK;
802 }
803
804 /***************************************************************************/
805 /* Queue command implementations */
806
807 static void jlink_end_state(tap_state_t state)
808 {
809 if (tap_is_state_stable(state))
810 tap_set_end_state(state);
811 else {
812 LOG_ERROR("BUG: %i is not a valid end state", state);
813 exit(-1);
814 }
815 }
816
817 /* Goes to the end state. */
818 static void jlink_state_move(void)
819 {
820 uint8_t tms_scan;
821 uint8_t tms_scan_bits;
822
823 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
824 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
825
826 jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
827
828 tap_set_state(tap_get_end_state());
829 }
830
831 static void jlink_path_move(int num_states, tap_state_t *path)
832 {
833 int i;
834 uint8_t tms = 0xff;
835
836 for (i = 0; i < num_states; i++) {
837 if (path[i] == tap_state_transition(tap_get_state(), false))
838 jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
839 else if (path[i] == tap_state_transition(tap_get_state(), true))
840 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
841 else {
842 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
843 tap_state_name(tap_get_state()), tap_state_name(path[i]));
844 exit(-1);
845 }
846
847 tap_set_state(path[i]);
848 }
849
850 tap_set_end_state(tap_get_state());
851 }
852
853 static void jlink_stableclocks(int num_cycles)
854 {
855 int i;
856
857 uint8_t tms = tap_get_state() == TAP_RESET;
858 /* Execute num_cycles. */
859 for (i = 0; i < num_cycles; i++)
860 jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
861 }
862
863 static void jlink_runtest(int num_cycles)
864 {
865 tap_state_t saved_end_state = tap_get_end_state();
866
867 /* Only do a state_move when we're not already in IDLE. */
868 if (tap_get_state() != TAP_IDLE) {
869 jlink_end_state(TAP_IDLE);
870 jlink_state_move();
871 /* num_cycles--; */
872 }
873
874 jlink_stableclocks(num_cycles);
875
876 /* Finish in end_state. */
877 jlink_end_state(saved_end_state);
878
879 if (tap_get_state() != tap_get_end_state())
880 jlink_state_move();
881 }
882
883 static void jlink_reset(int trst, int srst)
884 {
885 LOG_DEBUG("TRST: %i, SRST: %i.", trst, srst);
886
887 /* Signals are active low. */
888 if (srst == 0)
889 jaylink_set_reset(devh);
890
891 if (srst == 1)
892 jaylink_clear_reset(devh);
893
894 if (trst == 1)
895 jaylink_jtag_clear_trst(devh);
896
897 if (trst == 0)
898 jaylink_jtag_set_trst(devh);
899 }
900
901 COMMAND_HANDLER(jlink_usb_command)
902 {
903 int tmp;
904
905 if (CMD_ARGC != 1) {
906 command_print(CMD_CTX, "Need exactly one argument for jlink usb.");
907 return ERROR_COMMAND_SYNTAX_ERROR;
908 }
909
910 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
911 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
912 return ERROR_FAIL;
913 }
914
915 if (tmp < JAYLINK_USB_ADDRESS_0 || tmp > JAYLINK_USB_ADDRESS_3) {
916 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
917 return ERROR_FAIL;
918 }
919
920 usb_address = tmp;
921
922 use_serial_number = false;
923 use_usb_address = true;
924
925 return ERROR_OK;
926 }
927
928 COMMAND_HANDLER(jlink_serial_command)
929 {
930 int ret;
931
932 if (CMD_ARGC != 1) {
933 command_print(CMD_CTX, "Need exactly one argument for jlink serial.");
934 return ERROR_COMMAND_SYNTAX_ERROR;
935 }
936
937 ret = jaylink_parse_serial_number(CMD_ARGV[0], &serial_number);
938
939 if (ret == JAYLINK_ERR) {
940 command_print(CMD_CTX, "Invalid serial number: %s.", CMD_ARGV[0]);
941 return ERROR_FAIL;
942 } else if (ret != JAYLINK_OK) {
943 command_print(CMD_CTX, "jaylink_parse_serial_number() failed: %s.",
944 jaylink_strerror_name(ret));
945 return ERROR_FAIL;
946 }
947
948 use_serial_number = true;
949 use_usb_address = false;
950
951 return ERROR_OK;
952 }
953
954 COMMAND_HANDLER(jlink_handle_hwstatus_command)
955 {
956 int ret;
957 struct jaylink_hardware_status status;
958
959 ret = jaylink_get_hardware_status(devh, &status);
960
961 if (ret != JAYLINK_OK) {
962 command_print(CMD_CTX, "jaylink_get_hardware_status() failed: %s.",
963 jaylink_strerror_name(ret));
964 return ERROR_FAIL;
965 }
966
967 command_print(CMD_CTX, "VTarget = %u.%03u V",
968 status.target_voltage / 1000, status.target_voltage % 1000);
969
970 command_print(CMD_CTX, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
971 "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
972 status.tres, status.trst);
973
974 if (status.target_voltage < 1500)
975 command_print(CMD_CTX, "Target voltage too low. Check target power.");
976
977 return ERROR_OK;
978 }
979
980 COMMAND_HANDLER(jlink_handle_free_memory_command)
981 {
982 int ret;
983 uint32_t tmp;
984
985 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
986 command_print(CMD_CTX, "Retrieval of free memory is not supported by "
987 "the device.");
988 return ERROR_OK;
989 }
990
991 ret = jaylink_get_free_memory(devh, &tmp);
992
993 if (ret != JAYLINK_OK) {
994 command_print(CMD_CTX, "jaylink_get_free_memory() failed: %s.",
995 jaylink_strerror_name(ret));
996 return ERROR_FAIL;
997 }
998
999 command_print(CMD_CTX, "Device has %u bytes of free memory.", tmp);
1000
1001 return ERROR_OK;
1002 }
1003
1004 COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
1005 {
1006 int tmp;
1007 int version;
1008
1009 if (!CMD_ARGC) {
1010 switch (jtag_command_version) {
1011 case JAYLINK_JTAG_VERSION_2:
1012 version = 2;
1013 break;
1014 case JAYLINK_JTAG_VERSION_3:
1015 version = 3;
1016 break;
1017 default:
1018 return ERROR_FAIL;
1019 }
1020
1021 command_print(CMD_CTX, "JTAG command version: %i", version);
1022 } else if (CMD_ARGC == 1) {
1023 if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
1024 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1025 return ERROR_COMMAND_SYNTAX_ERROR;
1026 }
1027
1028 switch (tmp) {
1029 case 2:
1030 jtag_command_version = JAYLINK_JTAG_VERSION_2;
1031 break;
1032 case 3:
1033 jtag_command_version = JAYLINK_JTAG_VERSION_3;
1034 break;
1035 default:
1036 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1037 return ERROR_COMMAND_SYNTAX_ERROR;
1038 }
1039 } else {
1040 command_print(CMD_CTX, "Need exactly one argument for jlink jtag.");
1041 return ERROR_COMMAND_SYNTAX_ERROR;
1042 }
1043
1044 return ERROR_OK;
1045 }
1046
1047 COMMAND_HANDLER(jlink_handle_target_power_command)
1048 {
1049 int ret;
1050 int enable;
1051
1052 if (CMD_ARGC != 1) {
1053 command_print(CMD_CTX, "Need exactly one argument for jlink "
1054 "targetpower.");
1055 return ERROR_COMMAND_SYNTAX_ERROR;
1056 }
1057
1058 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1059 command_print(CMD_CTX, "Target power supply is not supported by the "
1060 "device.");
1061 return ERROR_OK;
1062 }
1063
1064 if (!strcmp(CMD_ARGV[0], "on")) {
1065 enable = true;
1066 } else if (!strcmp(CMD_ARGV[0], "off")) {
1067 enable = false;
1068 } else {
1069 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1070 return ERROR_FAIL;
1071 }
1072
1073 ret = jaylink_set_target_power(devh, enable);
1074
1075 if (ret != JAYLINK_OK) {
1076 command_print(CMD_CTX, "jaylink_set_target_power() failed: %s.",
1077 jaylink_strerror_name(ret));
1078 return ERROR_FAIL;
1079 }
1080
1081 return ERROR_OK;
1082 }
1083
1084 static void show_config_usb_address(struct command_context *ctx)
1085 {
1086 if (config.usb_address != tmp_config.usb_address)
1087 command_print(ctx, "USB address: %u [%u]", config.usb_address,
1088 tmp_config.usb_address);
1089 else
1090 command_print(ctx, "USB address: %u", config.usb_address);
1091 }
1092
1093 static void show_config_ip_address(struct command_context *ctx)
1094 {
1095 if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
1096 command_print(ctx, "IP address: %d.%d.%d.%d",
1097 config.ip_address[3], config.ip_address[2],
1098 config.ip_address[1], config.ip_address[0]);
1099 else
1100 command_print(ctx, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
1101 config.ip_address[3], config.ip_address[2],
1102 config.ip_address[1], config.ip_address[0],
1103 tmp_config.ip_address[3], tmp_config.ip_address[2],
1104 tmp_config.ip_address[1], tmp_config.ip_address[0]);
1105
1106 if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
1107 command_print(ctx, "Subnet mask: %d.%d.%d.%d",
1108 config.subnet_mask[3], config.subnet_mask[2],
1109 config.subnet_mask[1], config.subnet_mask[0]);
1110 else
1111 command_print(ctx, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
1112 config.subnet_mask[3], config.subnet_mask[2],
1113 config.subnet_mask[1], config.subnet_mask[0],
1114 tmp_config.subnet_mask[3], tmp_config.subnet_mask[2],
1115 tmp_config.subnet_mask[1], tmp_config.subnet_mask[0]);
1116 }
1117
1118 static void show_config_mac_address(struct command_context *ctx)
1119 {
1120 if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
1121 command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
1122 config.mac_address[5], config.mac_address[4],
1123 config.mac_address[3], config.mac_address[2],
1124 config.mac_address[1], config.mac_address[0]);
1125 else
1126 command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
1127 "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
1128 config.mac_address[5], config.mac_address[4],
1129 config.mac_address[3], config.mac_address[2],
1130 config.mac_address[1], config.mac_address[0],
1131 tmp_config.mac_address[5], tmp_config.mac_address[4],
1132 tmp_config.mac_address[3], tmp_config.mac_address[2],
1133 tmp_config.mac_address[1], tmp_config.mac_address[0]);
1134 }
1135
1136 static void show_config_target_power(struct command_context *ctx)
1137 {
1138 const char *target_power;
1139 const char *current_target_power;
1140
1141 if (!config.target_power)
1142 target_power = "off";
1143 else
1144 target_power = "on";
1145
1146 if (!tmp_config.target_power)
1147 current_target_power = "off";
1148 else
1149 current_target_power = "on";
1150
1151 if (config.target_power != tmp_config.target_power)
1152 command_print(ctx, "Target power supply: %s [%s]", target_power,
1153 current_target_power);
1154 else
1155 command_print(ctx, "Target power supply: %s", target_power);
1156 }
1157
1158 static void show_config(struct command_context *ctx)
1159 {
1160 command_print(ctx, "J-Link device configuration:");
1161
1162 show_config_usb_address(ctx);
1163
1164 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
1165 show_config_target_power(ctx);
1166
1167 if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1168 show_config_ip_address(ctx);
1169 show_config_mac_address(ctx);
1170 }
1171 }
1172
1173 static int poll_trace(uint8_t *buf, size_t *size)
1174 {
1175 int ret;
1176 uint32_t length;
1177
1178 length = *size;
1179
1180 ret = jaylink_swo_read(devh, buf, &length);
1181
1182 if (ret != JAYLINK_OK) {
1183 LOG_ERROR("jaylink_swo_read() failed: %s.", jaylink_strerror_name(ret));
1184 return ERROR_FAIL;
1185 }
1186
1187 *size = length;
1188
1189 return ERROR_OK;
1190 }
1191
1192 static uint32_t calculate_trace_buffer_size(void)
1193 {
1194 int ret;
1195 uint32_t tmp;
1196
1197 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
1198 return 0;
1199
1200 ret = jaylink_get_free_memory(devh, &tmp);
1201
1202 if (ret != JAYLINK_OK) {
1203 LOG_ERROR("jaylink_get_free_memory() failed: %s.",
1204 jaylink_strerror_name(ret));
1205 return ERROR_FAIL;
1206 }
1207
1208 if (tmp > 0x3fff || tmp <= 0x600)
1209 tmp = tmp >> 1;
1210 else
1211 tmp = tmp - 0x400;
1212
1213 return tmp & 0xffffff00;
1214 }
1215
1216 static bool check_trace_freq(struct jaylink_swo_speed speed,
1217 uint32_t trace_freq)
1218 {
1219 double min;
1220 double deviation;
1221 uint32_t divider;
1222
1223 min = fabs(1.0 - (speed.freq / ((double)trace_freq * speed.min_div)));
1224
1225 for (divider = speed.min_div; divider < speed.max_div; divider++) {
1226 deviation = fabs(1.0 - (speed.freq / ((double)trace_freq * divider)));
1227
1228 if (deviation < 0.03) {
1229 LOG_DEBUG("Found suitable frequency divider %u with deviation of "
1230 "%.02f %%.", divider, deviation);
1231 return true;
1232 }
1233
1234 if (deviation < min)
1235 min = deviation;
1236 }
1237
1238 LOG_ERROR("Selected trace frequency is not supported by the device. "
1239 "Please choose a different trace frequency.");
1240 LOG_ERROR("Maximum permitted deviation is 3.00 %%, but only %.02f %% "
1241 "could be achieved.", min * 100);
1242
1243 return false;
1244 }
1245
1246 static int config_trace(bool enabled, enum tpio_pin_protocol pin_protocol,
1247 uint32_t port_size, unsigned int *trace_freq)
1248 {
1249 int ret;
1250 uint32_t buffer_size;
1251 struct jaylink_swo_speed speed;
1252
1253 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
1254 LOG_ERROR("Trace capturing is not supported by the device.");
1255 return ERROR_FAIL;
1256 }
1257
1258 if (pin_protocol != ASYNC_UART) {
1259 LOG_ERROR("Selected pin protocol is not supported.");
1260 return ERROR_FAIL;
1261 }
1262
1263 trace_enabled = enabled;
1264
1265 ret = jaylink_swo_stop(devh);
1266
1267 if (ret != JAYLINK_OK) {
1268 LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror_name(ret));
1269 return ERROR_FAIL;
1270 }
1271
1272 if (!enabled) {
1273 /*
1274 * Adjust the SWD transaction buffer size as stopping SWO capturing
1275 * deallocates device internal memory.
1276 */
1277 if (!adjust_swd_buffer_size())
1278 return ERROR_FAIL;
1279
1280 return ERROR_OK;
1281 }
1282
1283 buffer_size = calculate_trace_buffer_size();
1284
1285 if (!buffer_size) {
1286 LOG_ERROR("Not enough free device memory to start trace capturing.");
1287 return ERROR_FAIL;
1288 }
1289
1290 ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
1291
1292 if (ret != JAYLINK_OK) {
1293 LOG_ERROR("jaylink_swo_get_speeds() failed: %s.",
1294 jaylink_strerror_name(ret));
1295 return ERROR_FAIL;
1296 }
1297
1298 if (!*trace_freq)
1299 *trace_freq = speed.freq / speed.min_div;
1300
1301 if (!check_trace_freq(speed, *trace_freq))
1302 return ERROR_FAIL;
1303
1304 LOG_DEBUG("Using %u bytes device memory for trace capturing.", buffer_size);
1305
1306 ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
1307 buffer_size);
1308
1309 if (ret != JAYLINK_OK) {
1310 LOG_ERROR("jaylink_start_swo() failed: %s.",
1311 jaylink_strerror_name(ret));
1312 return ERROR_FAIL;
1313 }
1314
1315 /*
1316 * Adjust the SWD transaction buffer size as starting SWO capturing
1317 * allocates device internal memory.
1318 */
1319 if (!adjust_swd_buffer_size())
1320 return ERROR_FAIL;
1321
1322 return ERROR_OK;
1323 }
1324
1325 COMMAND_HANDLER(jlink_handle_config_usb_address_command)
1326 {
1327 uint8_t tmp;
1328
1329 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1330 command_print(CMD_CTX, "Reading configuration is not supported by the "
1331 "device.");
1332 return ERROR_OK;
1333 }
1334
1335 if (!CMD_ARGC) {
1336 show_config_usb_address(CMD_CTX);
1337 } else if (CMD_ARGC == 1) {
1338 if (sscanf(CMD_ARGV[0], "%" SCNd8, &tmp) != 1) {
1339 command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
1340 return ERROR_FAIL;
1341 }
1342
1343 if (tmp > JAYLINK_USB_ADDRESS_3) {
1344 command_print(CMD_CTX, "Invalid USB address: %u.", tmp);
1345 return ERROR_FAIL;
1346 }
1347
1348 tmp_config.usb_address = tmp;
1349 } else {
1350 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1351 "usb.");
1352 return ERROR_COMMAND_SYNTAX_ERROR;
1353 }
1354
1355 return ERROR_OK;
1356 }
1357
1358 COMMAND_HANDLER(jlink_handle_config_target_power_command)
1359 {
1360 int enable;
1361
1362 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1363 command_print(CMD_CTX, "Reading configuration is not supported by the "
1364 "device.");
1365 return ERROR_OK;
1366 }
1367
1368 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
1369 command_print(CMD_CTX, "Target power supply is not supported by the "
1370 "device.");
1371 return ERROR_OK;
1372 }
1373
1374 if (!CMD_ARGC) {
1375 show_config_target_power(CMD_CTX);
1376 } else if (CMD_ARGC == 1) {
1377 if (!strcmp(CMD_ARGV[0], "on")) {
1378 enable = true;
1379 } else if (!strcmp(CMD_ARGV[0], "off")) {
1380 enable = false;
1381 } else {
1382 command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
1383 return ERROR_FAIL;
1384 }
1385
1386 tmp_config.target_power = enable;
1387 } else {
1388 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1389 "targetpower.");
1390 return ERROR_COMMAND_SYNTAX_ERROR;
1391 }
1392
1393 return ERROR_OK;
1394 }
1395
1396 COMMAND_HANDLER(jlink_handle_config_mac_address_command)
1397 {
1398 uint8_t addr[6];
1399 int i;
1400 char *e;
1401 const char *str;
1402
1403 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1404 command_print(CMD_CTX, "Reading configuration is not supported by the "
1405 "device.");
1406 return ERROR_OK;
1407 }
1408
1409 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1410 command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
1411 "device.");
1412 return ERROR_OK;
1413 }
1414
1415 if (!CMD_ARGC) {
1416 show_config_mac_address(CMD_CTX);
1417 } else if (CMD_ARGC == 1) {
1418 str = CMD_ARGV[0];
1419
1420 if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || \
1421 str[8] != ':' || str[11] != ':' || str[14] != ':')) {
1422 command_print(CMD_CTX, "Invalid MAC address format.");
1423 return ERROR_COMMAND_SYNTAX_ERROR;
1424 }
1425
1426 for (i = 5; i >= 0; i--) {
1427 addr[i] = strtoul(str, &e, 16);
1428 str = e + 1;
1429 }
1430
1431 if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
1432 command_print(CMD_CTX, "Invalid MAC address: zero address.");
1433 return ERROR_COMMAND_SYNTAX_ERROR;
1434 }
1435
1436 if (!(0x01 & addr[0])) {
1437 command_print(CMD_CTX, "Invalid MAC address: multicast address.");
1438 return ERROR_COMMAND_SYNTAX_ERROR;
1439 }
1440
1441 memcpy(tmp_config.mac_address, addr, sizeof(addr));
1442 } else {
1443 command_print(CMD_CTX, "Need exactly one argument for jlink config "
1444 " mac.");
1445 return ERROR_COMMAND_SYNTAX_ERROR;
1446 }
1447
1448 return ERROR_OK;
1449 }
1450
1451 static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
1452 {
1453 uint8_t lip[4];
1454 char *e;
1455 const char *s_save = s;
1456 int i;
1457
1458 if (!s)
1459 return false;
1460
1461 for (i = 0; i < 4; i++) {
1462 lip[i] = strtoul(s, &e, 10);
1463
1464 if (*e != '.' && i != 3)
1465 return false;
1466
1467 s = e + 1;
1468 }
1469
1470 *pos = e - s_save;
1471 memcpy(ip, lip, sizeof(lip));
1472
1473 return true;
1474 }
1475
1476 static void cpy_ip(uint8_t *dst, uint8_t *src)
1477 {
1478 int i, j;
1479
1480 for (i = 0, j = 3; i < 4; i++, j--)
1481 dst[i] = src[j];
1482 }
1483
1484 COMMAND_HANDLER(jlink_handle_config_ip_address_command)
1485 {
1486 uint8_t ip_address[4];
1487 uint32_t subnet_mask = 0;
1488 int i, len;
1489 uint8_t subnet_bits = 24;
1490
1491 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1492 command_print(CMD_CTX, "Reading configuration is not supported by the "
1493 "device.");
1494 return ERROR_OK;
1495 }
1496
1497 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
1498 command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
1499 "device.");
1500 return ERROR_OK;
1501 }
1502
1503 if (!CMD_ARGC) {
1504 show_config_ip_address(CMD_CTX);
1505 } else {
1506 if (!string_to_ip(CMD_ARGV[0], ip_address, &i))
1507 return ERROR_COMMAND_SYNTAX_ERROR;
1508
1509 len = strlen(CMD_ARGV[0]);
1510
1511 /* Check for format A.B.C.D/E. */
1512 if (i < len) {
1513 if (CMD_ARGV[0][i] != '/')
1514 return ERROR_COMMAND_SYNTAX_ERROR;
1515
1516 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
1517 } else if (CMD_ARGC > 1) {
1518 if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i))
1519 return ERROR_COMMAND_SYNTAX_ERROR;
1520 }
1521
1522 if (!subnet_mask)
1523 subnet_mask = (uint32_t)(subnet_bits < 32 ?
1524 ((1ULL << subnet_bits) - 1) : 0xffffffff);
1525
1526 cpy_ip(tmp_config.ip_address, ip_address);
1527 cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
1528 }
1529
1530 return ERROR_OK;
1531 }
1532
1533 COMMAND_HANDLER(jlink_handle_config_reset_command)
1534 {
1535 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
1536 return ERROR_OK;
1537
1538 memcpy(&tmp_config, &config, sizeof(struct device_config));
1539
1540 return ERROR_OK;
1541 }
1542
1543
1544 COMMAND_HANDLER(jlink_handle_config_write_command)
1545 {
1546 int ret;
1547
1548 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1549 command_print(CMD_CTX, "Reading configuration is not supported by the "
1550 "device.");
1551 return ERROR_OK;
1552 }
1553
1554 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
1555 command_print(CMD_CTX, "Writing configuration is not supported by the "
1556 "device.");
1557 return ERROR_OK;
1558 }
1559
1560 if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1561 command_print(CMD_CTX, "Operation not performed due to no changes in "
1562 "the configuration.");
1563 return ERROR_OK;
1564 }
1565
1566 ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
1567
1568 if (ret != JAYLINK_OK) {
1569 LOG_ERROR("jaylink_write_raw_config() failed: %s.",
1570 jaylink_strerror_name(ret));
1571 return ERROR_FAIL;
1572 }
1573
1574 if (!read_device_config(&config)) {
1575 LOG_ERROR("Failed to read device configuration for verification.");
1576 return ERROR_FAIL;
1577 }
1578
1579 if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
1580 LOG_ERROR("Verification of device configuration failed. Please check "
1581 "your device.");
1582 return ERROR_FAIL;
1583 }
1584
1585 memcpy(&tmp_config, &config, sizeof(struct device_config));
1586 command_print(CMD_CTX, "The new device configuration applies after power "
1587 "cycling the J-Link device.");
1588
1589 return ERROR_OK;
1590 }
1591
1592 COMMAND_HANDLER(jlink_handle_config_command)
1593 {
1594 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
1595 command_print(CMD_CTX, "Device doesn't support reading configuration.");
1596 return ERROR_OK;
1597 }
1598
1599 if (CMD_ARGC == 0)
1600 show_config(CMD_CTX);
1601
1602 return ERROR_OK;
1603 }
1604
1605 COMMAND_HANDLER(jlink_handle_emucom_write_command)
1606 {
1607 int ret;
1608 size_t tmp;
1609 uint32_t channel;
1610 uint32_t length;
1611 uint8_t *buf;
1612 size_t dummy;
1613
1614 if (CMD_ARGC != 2)
1615 return ERROR_COMMAND_SYNTAX_ERROR;
1616
1617 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1618 LOG_ERROR("Device does not support EMUCOM.");
1619 return ERROR_FAIL;
1620 }
1621
1622 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1623
1624 tmp = strlen(CMD_ARGV[1]);
1625
1626 if (tmp % 2 != 0) {
1627 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1628 return ERROR_COMMAND_ARGUMENT_INVALID;
1629 }
1630
1631 buf = malloc(tmp / 2);
1632
1633 if (!buf) {
1634 LOG_ERROR("Failed to allocate buffer.");
1635 return ERROR_FAIL;
1636 }
1637
1638 dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
1639
1640 if (dummy != (tmp / 2)) {
1641 LOG_ERROR("Data must be encoded as hexadecimal pairs.");
1642 free(buf);
1643 return ERROR_COMMAND_ARGUMENT_INVALID;
1644 }
1645
1646 length = tmp / 2;
1647 ret = jaylink_emucom_write(devh, channel, buf, &length);
1648
1649 free(buf);
1650
1651 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1652 LOG_ERROR("Channel not supported by the device.");
1653 return ERROR_FAIL;
1654 } else if (ret != JAYLINK_OK) {
1655 LOG_ERROR("Failed to write to channel: %s.",
1656 jaylink_strerror_name(ret));
1657 return ERROR_FAIL;
1658 }
1659
1660 if (length != (tmp / 2))
1661 LOG_WARNING("Only %" PRIu32 " bytes written to the channel.", length);
1662
1663 return ERROR_OK;
1664 }
1665
1666 COMMAND_HANDLER(jlink_handle_emucom_read_command)
1667 {
1668 int ret;
1669 uint32_t channel;
1670 uint32_t length;
1671 uint8_t *buf;
1672 size_t tmp;
1673
1674 if (CMD_ARGC != 2)
1675 return ERROR_COMMAND_SYNTAX_ERROR;
1676
1677 if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
1678 LOG_ERROR("Device does not support EMUCOM.");
1679 return ERROR_FAIL;
1680 }
1681
1682 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
1683 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
1684
1685 buf = malloc(length * 3 + 1);
1686
1687 if (!buf) {
1688 LOG_ERROR("Failed to allocate buffer.");
1689 return ERROR_FAIL;
1690 }
1691
1692 ret = jaylink_emucom_read(devh, channel, buf, &length);
1693
1694 if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
1695 LOG_ERROR("Channel is not supported by the device.");
1696 free(buf);
1697 return ERROR_FAIL;
1698 } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
1699 LOG_ERROR("Channel is not available for the requested amount of data. "
1700 "%" PRIu32 " bytes are avilable.", length);
1701 free(buf);
1702 return ERROR_FAIL;
1703 } else if (ret != JAYLINK_OK) {
1704 LOG_ERROR("Failed to read from channel: %s.",
1705 jaylink_strerror_name(ret));
1706 free(buf);
1707 return ERROR_FAIL;
1708 }
1709
1710 tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
1711
1712 if (tmp != 2 * length) {
1713 LOG_ERROR("Failed to convert data into hexadecimal string.");
1714 free(buf);
1715 return ERROR_FAIL;
1716 }
1717
1718 command_print(CMD_CTX, "%s", buf + length);
1719 free(buf);
1720
1721 return ERROR_OK;
1722 }
1723
1724 static const struct command_registration jlink_config_subcommand_handlers[] = {
1725 {
1726 .name = "usb",
1727 .handler = &jlink_handle_config_usb_address_command,
1728 .mode = COMMAND_EXEC,
1729 .help = "set the USB address",
1730 .usage = "[0-3]",
1731 },
1732 {
1733 .name = "targetpower",
1734 .handler = &jlink_handle_config_target_power_command,
1735 .mode = COMMAND_EXEC,
1736 .help = "set the target power supply",
1737 .usage = "[on|off]"
1738 },
1739 {
1740 .name = "mac",
1741 .handler = &jlink_handle_config_mac_address_command,
1742 .mode = COMMAND_EXEC,
1743 .help = "set the MAC Address",
1744 .usage = "[ff:ff:ff:ff:ff:ff]",
1745 },
1746 {
1747 .name = "ip",
1748 .handler = &jlink_handle_config_ip_address_command,
1749 .mode = COMMAND_EXEC,
1750 .help = "set the IP address, where A.B.C.D is the IP address, "
1751 "E the bit of the subnet mask, F.G.H.I the subnet mask",
1752 .usage = "[A.B.C.D[/E] [F.G.H.I]]",
1753 },
1754 {
1755 .name = "reset",
1756 .handler = &jlink_handle_config_reset_command,
1757 .mode = COMMAND_EXEC,
1758 .help = "undo configuration changes"
1759 },
1760 {
1761 .name = "write",
1762 .handler = &jlink_handle_config_write_command,
1763 .mode = COMMAND_EXEC,
1764 .help = "write configuration to the device"
1765 },
1766 COMMAND_REGISTRATION_DONE
1767 };
1768
1769 static const struct command_registration jlink_emucom_subcommand_handlers[] = {
1770 {
1771 .name = "write",
1772 .handler = &jlink_handle_emucom_write_command,
1773 .mode = COMMAND_EXEC,
1774 .help = "write to a channel",
1775 .usage = "<channel> <data>",
1776 },
1777 {
1778 .name = "read",
1779 .handler = &jlink_handle_emucom_read_command,
1780 .mode = COMMAND_EXEC,
1781 .help = "read from a channel",
1782 .usage = "<channel> <length>"
1783 },
1784 COMMAND_REGISTRATION_DONE
1785 };
1786
1787 static const struct command_registration jlink_subcommand_handlers[] = {
1788 {
1789 .name = "jtag",
1790 .handler = &jlink_handle_jlink_jtag_command,
1791 .mode = COMMAND_EXEC,
1792 .help = "select the JTAG command version",
1793 .usage = "[2|3]",
1794 },
1795 {
1796 .name = "targetpower",
1797 .handler = &jlink_handle_target_power_command,
1798 .mode = COMMAND_EXEC,
1799 .help = "set the target power supply",
1800 .usage = "<on|off>"
1801 },
1802 {
1803 .name = "freemem",
1804 .handler = &jlink_handle_free_memory_command,
1805 .mode = COMMAND_EXEC,
1806 .help = "show free device memory"
1807 },
1808 {
1809 .name = "hwstatus",
1810 .handler = &jlink_handle_hwstatus_command,
1811 .mode = COMMAND_EXEC,
1812 .help = "show the hardware status"
1813 },
1814 {
1815 .name = "usb",
1816 .handler = &jlink_usb_command,
1817 .mode = COMMAND_CONFIG,
1818 .help = "set the USB address of the device that should be used",
1819 .usage = "<0-3>"
1820 },
1821 {
1822 .name = "serial",
1823 .handler = &jlink_serial_command,
1824 .mode = COMMAND_CONFIG,
1825 .help = "set the serial number of the device that should be used",
1826 .usage = "<serial number>"
1827 },
1828 {
1829 .name = "config",
1830 .handler = &jlink_handle_config_command,
1831 .mode = COMMAND_EXEC,
1832 .help = "access the device configuration. If no argument is given "
1833 "this will show the device configuration",
1834 .chain = jlink_config_subcommand_handlers,
1835 },
1836 {
1837 .name = "emucom",
1838 .mode = COMMAND_EXEC,
1839 .help = "access EMUCOM channel",
1840 .chain = jlink_emucom_subcommand_handlers
1841 },
1842 COMMAND_REGISTRATION_DONE
1843 };
1844
1845 static const struct command_registration jlink_command_handlers[] = {
1846 {
1847 .name = "jlink",
1848 .mode = COMMAND_ANY,
1849 .help = "perform jlink management",
1850 .chain = jlink_subcommand_handlers,
1851 },
1852 COMMAND_REGISTRATION_DONE
1853 };
1854
1855 static int jlink_swd_init(void)
1856 {
1857 iface = JAYLINK_TIF_SWD;
1858
1859 return ERROR_OK;
1860 }
1861
1862 static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1863 {
1864 assert(!(cmd & SWD_CMD_RnW));
1865 jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1866 }
1867
1868 static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1869 {
1870 assert(cmd & SWD_CMD_RnW);
1871 jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1872 }
1873
1874 static int_least32_t jlink_swd_frequency(int_least32_t hz)
1875 {
1876 if (hz > 0)
1877 jlink_speed(hz / 1000);
1878
1879 return hz;
1880 }
1881
1882 /***************************************************************************/
1883 /* J-Link tap functions */
1884
1885 static unsigned tap_length;
1886 /* In SWD mode use tms buffer for direction control */
1887 static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
1888 static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
1889 static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
1890
1891 struct pending_scan_result {
1892 /** First bit position in tdo_buffer to read. */
1893 unsigned first;
1894 /** Number of bits to read. */
1895 unsigned length;
1896 /** Location to store the result */
1897 void *buffer;
1898 /** Offset in the destination buffer */
1899 unsigned buffer_offset;
1900 };
1901
1902 #define MAX_PENDING_SCAN_RESULTS 256
1903
1904 static int pending_scan_results_length;
1905 static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
1906
1907 static void jlink_tap_init(void)
1908 {
1909 tap_length = 0;
1910 pending_scan_results_length = 0;
1911 memset(tms_buffer, 0, sizeof(tms_buffer));
1912 memset(tdi_buffer, 0, sizeof(tdi_buffer));
1913 }
1914
1915 static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
1916 const uint8_t *tms_out, unsigned tms_offset,
1917 uint8_t *in, unsigned in_offset,
1918 unsigned length)
1919 {
1920 do {
1921 unsigned available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
1922
1923 if (!available_length ||
1924 (in && pending_scan_results_length == MAX_PENDING_SCAN_RESULTS)) {
1925 if (jlink_flush() != ERROR_OK)
1926 return;
1927 available_length = JLINK_TAP_BUFFER_SIZE;
1928 }
1929
1930 struct pending_scan_result *pending_scan_result =
1931 &pending_scan_results_buffer[pending_scan_results_length];
1932
1933 unsigned scan_length = length > available_length ?
1934 available_length : length;
1935
1936 if (out)
1937 buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
1938 if (tms_out)
1939 buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
1940
1941 if (in) {
1942 pending_scan_result->first = tap_length;
1943 pending_scan_result->length = scan_length;
1944 pending_scan_result->buffer = in;
1945 pending_scan_result->buffer_offset = in_offset;
1946 pending_scan_results_length++;
1947 }
1948
1949 tap_length += scan_length;
1950 out_offset += scan_length;
1951 tms_offset += scan_length;
1952 in_offset += scan_length;
1953 length -= scan_length;
1954 } while (length > 0);
1955 }
1956
1957 static int jlink_flush(void)
1958 {
1959 int i;
1960 int ret;
1961
1962 if (!tap_length)
1963 return ERROR_OK;
1964
1965 jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
1966 tap_length, jlink_last_state);
1967
1968 ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
1969 tap_length, jtag_command_version);
1970
1971 if (ret != JAYLINK_OK) {
1972 LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror_name(ret));
1973 jlink_tap_init();
1974 return ERROR_JTAG_QUEUE_FAILED;
1975 }
1976
1977 for (i = 0; i < pending_scan_results_length; i++) {
1978 struct pending_scan_result *p = &pending_scan_results_buffer[i];
1979
1980 buf_set_buf(tdo_buffer, p->first, p->buffer,
1981 p->buffer_offset, p->length);
1982
1983 DEBUG_JTAG_IO("Pending scan result, length = %d.", p->length);
1984 }
1985
1986 jlink_tap_init();
1987
1988 return ERROR_OK;
1989 }
1990
1991 static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
1992 {
1993 unsigned int tap_pos = tap_length;
1994
1995 while (len > 32) {
1996 buf_set_u32(buf, tap_pos, 32, val);
1997 len -= 32;
1998 tap_pos += 32;
1999 }
2000
2001 if (len)
2002 buf_set_u32(buf, tap_pos, len, val);
2003 }
2004
2005 static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
2006 {
2007 const uint32_t dir_out = 0xffffffff;
2008
2009 if (data)
2010 bit_copy(tdi_buffer, tap_length, data, 0, len);
2011 else
2012 fill_buffer(tdi_buffer, 0, len);
2013
2014 fill_buffer(tms_buffer, dir_out, len);
2015 tap_length += len;
2016 }
2017
2018 static void jlink_queue_data_in(uint32_t len)
2019 {
2020 const uint32_t dir_in = 0;
2021
2022 fill_buffer(tms_buffer, dir_in, len);
2023 tap_length += len;
2024 }
2025
2026 static int jlink_swd_switch_seq(enum swd_special_seq seq)
2027 {
2028 const uint8_t *s;
2029 unsigned int s_len;
2030
2031 switch (seq) {
2032 case LINE_RESET:
2033 LOG_DEBUG("SWD line reset");
2034 s = swd_seq_line_reset;
2035 s_len = swd_seq_line_reset_len;
2036 break;
2037 case JTAG_TO_SWD:
2038 LOG_DEBUG("JTAG-to-SWD");
2039 s = swd_seq_jtag_to_swd;
2040 s_len = swd_seq_jtag_to_swd_len;
2041 break;
2042 case SWD_TO_JTAG:
2043 LOG_DEBUG("SWD-to-JTAG");
2044 s = swd_seq_swd_to_jtag;
2045 s_len = swd_seq_swd_to_jtag_len;
2046 break;
2047 default:
2048 LOG_ERROR("Sequence %d not supported.", seq);
2049 return ERROR_FAIL;
2050 }
2051
2052 jlink_queue_data_out(s, s_len);
2053
2054 return ERROR_OK;
2055 }
2056
2057 static int jlink_swd_run_queue(void)
2058 {
2059 int i;
2060 int ret;
2061
2062 LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length);
2063
2064 if (queued_retval != ERROR_OK) {
2065 LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval);
2066 goto skip;
2067 }
2068
2069 /*
2070 * A transaction must be followed by another transaction or at least 8 idle
2071 * cycles to ensure that data is clocked through the AP.
2072 */
2073 jlink_queue_data_out(NULL, 8);
2074
2075 ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
2076
2077 if (ret != JAYLINK_OK) {
2078 LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror_name(ret));
2079 goto skip;
2080 }
2081
2082 for (i = 0; i < pending_scan_results_length; i++) {
2083 int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
2084
2085 if (ack != SWD_ACK_OK) {
2086 LOG_DEBUG("SWD ack not OK: %d %s", ack,
2087 ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
2088 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
2089 goto skip;
2090 } else if (pending_scan_results_buffer[i].length) {
2091 uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
2092 int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
2093
2094 if (parity != parity_u32(data)) {
2095 LOG_ERROR("SWD: Read data parity mismatch.");
2096 queued_retval = ERROR_FAIL;
2097 goto skip;
2098 }
2099
2100 if (pending_scan_results_buffer[i].buffer)
2101 *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
2102 }
2103 }
2104
2105 skip:
2106 jlink_tap_init();
2107 ret = queued_retval;
2108 queued_retval = ERROR_OK;
2109
2110 return ret;
2111 }
2112
2113 static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
2114 {
2115 uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
2116 if (tap_length + 46 + 8 + ap_delay_clk >= sizeof(tdi_buffer) * 8 ||
2117 pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
2118 /* Not enough room in the queue. Run the queue. */
2119 queued_retval = jlink_swd_run_queue();
2120 }
2121
2122 if (queued_retval != ERROR_OK)
2123 return;
2124
2125 cmd |= SWD_CMD_START | SWD_CMD_PARK;
2126
2127 jlink_queue_data_out(&cmd, 8);
2128
2129 pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
2130
2131 if (cmd & SWD_CMD_RnW) {
2132 /* Queue a read transaction. */
2133 pending_scan_results_buffer[pending_scan_results_length].length = 32;
2134 pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
2135
2136 jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
2137 } else {
2138 /* Queue a write transaction. */
2139 pending_scan_results_buffer[pending_scan_results_length].length = 0;
2140 jlink_queue_data_in(1 + 3 + 1);
2141
2142 buf_set_u32(data_parity_trn, 0, 32, data);
2143 buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
2144
2145 jlink_queue_data_out(data_parity_trn, 32 + 1);
2146 }
2147
2148 pending_scan_results_length++;
2149
2150 /* Insert idle cycles after AP accesses to avoid WAIT. */
2151 if (cmd & SWD_CMD_APnDP)
2152 jlink_queue_data_out(NULL, ap_delay_clk);
2153 }
2154
2155 static const struct swd_driver jlink_swd = {
2156 .init = &jlink_swd_init,
2157 .frequency = &jlink_swd_frequency,
2158 .switch_seq = &jlink_swd_switch_seq,
2159 .read_reg = &jlink_swd_read_reg,
2160 .write_reg = &jlink_swd_write_reg,
2161 .run = &jlink_swd_run_queue,
2162 };
2163
2164 static const char * const jlink_transports[] = { "jtag", "swd", NULL };
2165
2166 struct jtag_interface jlink_interface = {
2167 .name = "jlink",
2168 .commands = jlink_command_handlers,
2169 .transports = jlink_transports,
2170 .swd = &jlink_swd,
2171 .execute_queue = &jlink_execute_queue,
2172 .speed = &jlink_speed,
2173 .speed_div = &jlink_speed_div,
2174 .khz = &jlink_khz,
2175 .init = &jlink_init,
2176 .quit = &jlink_quit,
2177 .config_trace = &config_trace,
2178 .poll_trace = &poll_trace,
2179 };

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)