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

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)