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