OpenOCD
xds110.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2017 by Texas Instruments, Inc. *
5  ***************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <transport/transport.h>
12 #include <jtag/adapter.h>
13 #include <jtag/swd.h>
14 #include <jtag/interface.h>
15 #include <jtag/commands.h>
16 #include <jtag/tcl.h>
17 #include <libusb.h>
18 
19 /* XDS110 stand-alone probe voltage supply limits */
20 #define XDS110_MIN_VOLTAGE 1800
21 #define XDS110_MAX_VOLTAGE 3600
22 
23 /* XDS110 stand-alone probe hardware ID */
24 #define XDS110_STAND_ALONE_ID 0x21
25 
26 /* Firmware version that introduced OpenOCD support via block accesses */
27 #define OCD_FIRMWARE_VERSION 0x02030011
28 #define OCD_FIRMWARE_UPGRADE \
29  "XDS110: upgrade to version 2.3.0.11+ for improved support"
30 
31 /* Firmware version that introduced improved TCK performance */
32 #define FAST_TCK_FIRMWARE_VERSION 0x03000000
33 
34 /* Firmware version that introduced 10 MHz and 12 MHz TCK support */
35 #define FAST_TCK_PLUS_FIRMWARE_VERSION 0x03000003
36 
37 /***************************************************************************
38  * USB Connection Buffer Definitions *
39  ***************************************************************************/
40 
41 /* Max USB packet size for up to USB 3.0 */
42 #define MAX_PACKET 1024
43 
44 /*
45  * Maximum data payload that can be handled in a single call
46  * Limitation is the size of the buffers in the XDS110 firmware
47  */
48 #define MAX_DATA_BLOCK 4096
49 
50 #ifndef USB_PAYLOAD_SIZE
51 /* Largest data block plus parameters */
52 #define USB_PAYLOAD_SIZE (MAX_DATA_BLOCK + 60)
53 #endif
54 #define MAX_RESULT_QUEUE (MAX_DATA_BLOCK / 4)
55 
56 /***************************************************************************
57  * XDS110 Firmware API Definitions *
58  ***************************************************************************/
59 
60 /*
61  * Default values controlling how the host communicates commands
62  * with XDS110 firmware (automatic retry count and wait timeout)
63  */
64 #define DEFAULT_ATTEMPTS (1)
65 #define DEFAULT_TIMEOUT (4000)
66 
67 /* XDS110 API error codes */
68 #define SC_ERR_NONE 0
69 #define SC_ERR_XDS110_FAIL -261
70 #define SC_ERR_SWD_WAIT -613
71 #define SC_ERR_SWD_FAULT -614
72 #define SC_ERR_SWD_PROTOCOL -615
73 #define SC_ERR_SWD_PARITY -616
74 #define SC_ERR_SWD_DEVICE_ID -617
75 
76 /* TCK frequency limits */
77 #define XDS110_MIN_TCK_SPEED 100 /* kHz */
78 #define XDS110_MAX_SLOW_TCK_SPEED 2500 /* kHz */
79 #define XDS110_MAX_FAST_TCK_SPEED 14000 /* kHz */
80 #define XDS110_DEFAULT_TCK_SPEED 2500 /* kHz */
81 
82 /* Fixed TCK delay values for "Fast" TCK frequencies */
83 #define FAST_TCK_DELAY_14000_KHZ 0
84 #define FAST_TCK_DELAY_10000_KHZ 0xfffffffd
85 #define FAST_TCK_DELAY_12000_KHZ 0xfffffffe
86 #define FAST_TCK_DELAY_8500_KHZ 1
87 #define FAST_TCK_DELAY_5500_KHZ 2
88 /* For TCK frequencies below 5500 kHz, use calculated delay */
89 
90 /* Scan mode on connect */
91 #define MODE_JTAG 1
92 
93 /* XDS110 API JTAG state definitions */
94 #define XDS_JTAG_STATE_RESET 1
95 #define XDS_JTAG_STATE_IDLE 2
96 #define XDS_JTAG_STATE_SHIFT_DR 3
97 #define XDS_JTAG_STATE_SHIFT_IR 4
98 #define XDS_JTAG_STATE_PAUSE_DR 5
99 #define XDS_JTAG_STATE_PAUSE_IR 6
100 #define XDS_JTAG_STATE_EXIT1_DR 8
101 #define XDS_JTAG_STATE_EXIT1_IR 9
102 #define XDS_JTAG_STATE_EXIT2_DR 10
103 #define XDS_JTAG_STATE_EXIT2_IR 11
104 #define XDS_JTAG_STATE_SELECT_DR 12
105 #define XDS_JTAG_STATE_SELECT_IR 13
106 #define XDS_JTAG_STATE_UPDATE_DR 14
107 #define XDS_JTAG_STATE_UPDATE_IR 15
108 #define XDS_JTAG_STATE_CAPTURE_DR 16
109 #define XDS_JTAG_STATE_CAPTURE_IR 17
110 
111 /* XDS110 API JTAG transit definitions */
112 #define XDS_JTAG_TRANSIT_QUICKEST 1
113 #define XDS_JTAG_TRANSIT_VIA_CAPTURE 2
114 #define XDS_JTAG_TRANSIT_VIA_IDLE 3
115 
116 /* DAP register definitions as used by XDS110 APIs */
117 
118 #define DAP_AP 0 /* DAP AP register type */
119 #define DAP_DP 1 /* DAP DP register type */
120 
121 #define DAP_DP_IDCODE 0x0 /* DAP DP IDCODE register (read only) */
122 #define DAP_DP_ABORT 0x0 /* DAP DP ABORT register (write only) */
123 #define DAP_DP_STAT 0x4 /* DAP DP STAT register (for read only) */
124 #define DAP_DP_CTRL 0x4 /* DAP DP CTRL register (for write only) */
125 #define DAP_DP_ADDR 0x8 /* DAP DP SELECT register (legacy name) */
126 #define DAP_DP_RESEND 0x8 /* DAP DP RESEND register (read only) */
127 #define DAP_DP_SELECT 0x8 /* DAP DP SELECT register (write only) */
128 #define DAP_DP_RDBUFF 0xc /* DAP DP RDBUFF Read Buffer register */
129 
130 #define DAP_AP_CSW 0x00 /* DAP AP Control Status Word */
131 #define DAP_AP_TAR 0x04 /* DAP AP Transfer Address */
132 #define DAP_AP_DRW 0x0C /* DAP AP Data Read/Write */
133 #define DAP_AP_BD0 0x10 /* DAP AP Banked Data 0 */
134 #define DAP_AP_BD1 0x14 /* DAP AP Banked Data 1 */
135 #define DAP_AP_BD2 0x18 /* DAP AP Banked Data 2 */
136 #define DAP_AP_BD3 0x1C /* DAP AP Banked Data 3 */
137 #define DAP_AP_RTBL 0xF8 /* DAP AP Debug ROM Table */
138 #define DAP_AP_IDR 0xFC /* DAP AP Identification Register */
139 
140 /* Command packet definitions */
141 
142 #define XDS_OUT_LEN 1 /* command (byte) */
143 #define XDS_IN_LEN 4 /* error code (int) */
144 
145 /* XDS API Commands */
146 #define XDS_CONNECT 0x01 /* Connect JTAG connection */
147 #define XDS_DISCONNECT 0x02 /* Disconnect JTAG connection */
148 #define XDS_VERSION 0x03 /* Get firmware version and hardware ID */
149 #define XDS_SET_TCK 0x04 /* Set TCK delay (to set TCK frequency) */
150 #define XDS_SET_TRST 0x05 /* Assert or deassert nTRST signal */
151 #define XDS_CYCLE_TCK 0x07 /* Toggle TCK for a number of cycles */
152 #define XDS_GOTO_STATE 0x09 /* Go to requested JTAG state */
153 #define XDS_JTAG_SCAN 0x0c /* Send and receive JTAG scan */
154 #define XDS_SET_SRST 0x0e /* Assert or deassert nSRST signal */
155 #define CMAPI_CONNECT 0x0f /* CMAPI connect */
156 #define CMAPI_DISCONNECT 0x10 /* CMAPI disconnect */
157 #define CMAPI_ACQUIRE 0x11 /* CMAPI acquire */
158 #define CMAPI_RELEASE 0x12 /* CMAPI release */
159 #define CMAPI_REG_READ 0x15 /* CMAPI DAP register read */
160 #define CMAPI_REG_WRITE 0x16 /* CMAPI DAP register write */
161 #define SWD_CONNECT 0x17 /* Switch from JTAG to SWD connection */
162 #define SWD_DISCONNECT 0x18 /* Switch from SWD to JTAG connection */
163 #define CJTAG_CONNECT 0x2b /* Switch from JTAG to cJTAG connection */
164 #define CJTAG_DISCONNECT 0x2c /* Switch from cJTAG to JTAG connection */
165 #define XDS_SET_SUPPLY 0x32 /* Set up stand-alone probe upply voltage */
166 #define OCD_DAP_REQUEST 0x3a /* Handle block of DAP requests */
167 #define OCD_SCAN_REQUEST 0x3b /* Handle block of JTAG scan requests */
168 #define OCD_PATHMOVE 0x3c /* Handle PATHMOVE to navigate JTAG states */
169 
170 #define CMD_IR_SCAN 1
171 #define CMD_DR_SCAN 2
172 #define CMD_RUNTEST 3
173 #define CMD_STABLECLOCKS 4
174 
175 /* Array to convert from OpenOCD enum tap_state to XDS JTAG state */
176 static const uint32_t xds_jtag_state[] = {
177  XDS_JTAG_STATE_EXIT2_DR, /* TAP_DREXIT2 = 0x0 */
178  XDS_JTAG_STATE_EXIT1_DR, /* TAP_DREXIT1 = 0x1 */
179  XDS_JTAG_STATE_SHIFT_DR, /* TAP_DRSHIFT = 0x2 */
180  XDS_JTAG_STATE_PAUSE_DR, /* TAP_DRPAUSE = 0x3 */
181  XDS_JTAG_STATE_SELECT_IR, /* TAP_IRSELECT = 0x4 */
182  XDS_JTAG_STATE_UPDATE_DR, /* TAP_DRUPDATE = 0x5 */
183  XDS_JTAG_STATE_CAPTURE_DR, /* TAP_DRCAPTURE = 0x6 */
184  XDS_JTAG_STATE_SELECT_DR, /* TAP_DRSELECT = 0x7 */
185  XDS_JTAG_STATE_EXIT2_IR, /* TAP_IREXIT2 = 0x8 */
186  XDS_JTAG_STATE_EXIT1_IR, /* TAP_IREXIT1 = 0x9 */
187  XDS_JTAG_STATE_SHIFT_IR, /* TAP_IRSHIFT = 0xa */
188  XDS_JTAG_STATE_PAUSE_IR, /* TAP_IRPAUSE = 0xb */
189  XDS_JTAG_STATE_IDLE, /* TAP_IDLE = 0xc */
190  XDS_JTAG_STATE_UPDATE_IR, /* TAP_IRUPDATE = 0xd */
191  XDS_JTAG_STATE_CAPTURE_IR, /* TAP_IRCAPTURE = 0xe */
192  XDS_JTAG_STATE_RESET, /* TAP_RESET = 0xf */
193 };
194 
195 struct scan_result {
196  bool first;
197  uint8_t *buffer;
198  uint32_t num_bits;
199 };
200 
201 struct xds110_info {
202  /* USB connection handles and data buffers */
203  struct libusb_context *ctx;
204  struct libusb_device_handle *dev;
206  unsigned char write_packet[3];
208  /* Device vid/pid */
209  uint16_t vid;
210  uint16_t pid;
211  /* Debug interface */
212  uint8_t interface;
213  uint8_t endpoint_in;
214  uint8_t endpoint_out;
215  /* Status flags */
221  /* DAP register caches */
222  uint32_t select;
223  uint32_t rdbuff;
225  /* TCK speed and delay count*/
226  uint32_t speed;
227  uint32_t delay_count;
228  /* XDS110 voltage supply setting */
229  uint32_t voltage;
230  /* XDS110 firmware and hardware version */
231  uint32_t firmware;
232  uint16_t hardware;
233  /* Transaction queues */
234  unsigned char txn_requests[MAX_DATA_BLOCK];
238  uint32_t txn_result_size;
240 };
241 
242 static struct xds110_info xds110 = {
243  .ctx = NULL,
244  .dev = NULL,
245  .vid = 0,
246  .pid = 0,
247  .interface = 0,
248  .endpoint_in = 0,
249  .endpoint_out = 0,
250  .is_connected = false,
251  .is_cmapi_connected = false,
252  .is_cmapi_acquired = false,
253  .is_swd_mode = false,
254  .is_ap_dirty = false,
255  .speed = XDS110_DEFAULT_TCK_SPEED,
256  .delay_count = 0,
257  .voltage = 0,
258  .firmware = 0,
259  .hardware = 0,
260  .txn_request_size = 0,
261  .txn_result_size = 0,
262  .txn_result_count = 0
263 };
264 
265 static inline void xds110_set_u32(uint8_t *buffer, uint32_t value)
266 {
267  buffer[3] = (value >> 24) & 0xff;
268  buffer[2] = (value >> 16) & 0xff;
269  buffer[1] = (value >> 8) & 0xff;
270  buffer[0] = (value >> 0) & 0xff;
271 }
272 
273 static inline void xds110_set_u16(uint8_t *buffer, uint16_t value)
274 {
275  buffer[1] = (value >> 8) & 0xff;
276  buffer[0] = (value >> 0) & 0xff;
277 }
278 
279 static inline uint32_t xds110_get_u32(uint8_t *buffer)
280 {
281  uint32_t value = (((uint32_t)buffer[3]) << 24) |
282  (((uint32_t)buffer[2]) << 16) |
283  (((uint32_t)buffer[1]) << 8) |
284  (((uint32_t)buffer[0]) << 0);
285  return value;
286 }
287 
288 static inline uint16_t xds110_get_u16(uint8_t *buffer)
289 {
290  uint16_t value = (((uint32_t)buffer[1]) << 8) |
291  (((uint32_t)buffer[0]) << 0);
292  return value;
293 }
294 
295 /***************************************************************************
296  * usb connection routines *
297  * *
298  * The following functions handle connecting, reading, and writing to *
299  * the XDS110 over USB using the libusb library. *
300  ***************************************************************************/
301 
302 static bool usb_connect(void)
303 {
304  struct libusb_context *ctx = NULL;
305  struct libusb_device **list = NULL;
306  struct libusb_device_handle *dev = NULL;
307 
308  struct libusb_device_descriptor desc;
309 
310  /* The vid/pids of possible XDS110 configurations */
311  uint16_t vids[] = { 0x0451, 0x0451, 0x1cbe };
312  uint16_t pids[] = { 0xbef3, 0xbef4, 0x02a5 };
313  /* Corresponding interface and endpoint numbers for configurations */
314  uint8_t interfaces[] = { 2, 2, 0 };
315  uint8_t endpoints_in[] = { 3, 3, 1 };
316  uint8_t endpoints_out[] = { 2, 2, 1 };
317 
318  ssize_t count = 0;
319  ssize_t i = 0;
320  int result = 0;
321  bool found = false;
322  uint32_t device = 0;
323  bool match = false;
324 
325  /* Initialize libusb context */
326  result = libusb_init(&ctx);
327 
328  if (result == 0) {
329  /* Get list of USB devices attached to system */
330  count = libusb_get_device_list(ctx, &list);
331  if (count <= 0) {
332  result = -1;
333  list = NULL;
334  }
335  }
336 
337  if (result == 0) {
338  /* Scan through list of devices for any XDS110s */
339  for (i = 0; i < count; i++) {
340  /* Check for device vid/pid match */
341  libusb_get_device_descriptor(list[i], &desc);
342  match = false;
343  for (device = 0; device < ARRAY_SIZE(vids); device++) {
344  if (desc.idVendor == vids[device] &&
345  desc.idProduct == pids[device]) {
346  match = true;
347  break;
348  }
349  }
350  if (match) {
351  result = libusb_open(list[i], &dev);
352  if (result == 0) {
353  const int max_data = 256;
354  unsigned char data[max_data + 1];
355  *data = '\0';
356 
357  /* May be the requested device if serial number matches */
359  /* No serial number given; match first XDS110 found */
360  found = true;
361  break;
362  } else {
363  /* Get the device's serial number string */
364  result = libusb_get_string_descriptor_ascii(dev,
365  desc.iSerialNumber, data, max_data);
366  if (result > 0 &&
367  strcmp((char *)data, adapter_get_required_serial()) == 0) {
368  found = true;
369  break;
370  }
371  }
372 
373  /* If we fall though to here, we don't want this device */
374  libusb_close(dev);
375  dev = NULL;
376  } else {
377  const char *err_msg = libusb_error_name(result);
378  LOG_ERROR("libusb_open(): %s", err_msg);
379  }
380  }
381  }
382  }
383 
384  /*
385  * We can fall through the for() loop with two possible exit conditions:
386  * 1) found the right XDS110, and that device is open
387  * 2) didn't find the XDS110, and no devices are currently open
388  */
389 
390  if (list) {
391  /* Free the device list, we're done with it */
392  libusb_free_device_list(list, 1);
393  }
394 
395  if (found) {
396  /* Save the vid/pid of the device we're using */
397  xds110.vid = vids[device];
398  xds110.pid = pids[device];
399 
400  /* Save the debug interface and endpoints for the device */
401  xds110.interface = interfaces[device];
402  xds110.endpoint_in = endpoints_in[device] | LIBUSB_ENDPOINT_IN;
403  xds110.endpoint_out = endpoints_out[device] | LIBUSB_ENDPOINT_OUT;
404 
405  /* Save the context and device handles */
406  xds110.ctx = ctx;
407  xds110.dev = dev;
408 
409  /* Set libusb to auto detach kernel */
410  (void)libusb_set_auto_detach_kernel_driver(dev, 1);
411 
412  /* Claim the debug interface on the XDS110 */
413  result = libusb_claim_interface(dev, xds110.interface);
414  } else {
415  /* Couldn't find an XDS110, flag the error */
416  result = -1;
417  }
418 
419  /* On an error, clean up what we can */
420  if (result != 0) {
421  if (dev) {
422  /* Release the debug and data interface on the XDS110 */
423  (void)libusb_release_interface(dev, xds110.interface);
424  libusb_close(dev);
425  }
426  if (ctx)
427  libusb_exit(ctx);
428  xds110.ctx = NULL;
429  xds110.dev = NULL;
430  }
431 
432  /* Log the results */
433  if (result == 0)
434  LOG_DEBUG("XDS110: connected");
435  else
436  LOG_ERROR("XDS110: failed to connect");
437 
438  return (result == 0) ? true : false;
439 }
440 
441 static void usb_disconnect(void)
442 {
443  if (xds110.dev) {
444  /* Release the debug and data interface on the XDS110 */
445  (void)libusb_release_interface(xds110.dev, xds110.interface);
446  libusb_close(xds110.dev);
447  xds110.dev = NULL;
448  }
449  if (xds110.ctx) {
450  libusb_exit(xds110.ctx);
451  xds110.ctx = NULL;
452  }
453 
454  LOG_DEBUG("XDS110: disconnected");
455 }
456 
457 static bool usb_read(unsigned char *buffer, int size, int *bytes_read,
458  int timeout)
459 {
460  int result;
461 
462  if (!xds110.dev || !buffer || !bytes_read)
463  return false;
464 
465  /* Force a non-zero timeout to prevent blocking */
466  if (timeout == 0)
468 
469  result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_in, buffer, size,
470  bytes_read, timeout);
471 
472  return (result == 0) ? true : false;
473 }
474 
475 static bool usb_write(unsigned char *buffer, int size, int *written)
476 {
477  int bytes_written = 0;
478  int result = LIBUSB_SUCCESS;
479  int retries = 0;
480 
481  if (!xds110.dev || !buffer)
482  return false;
483 
484  result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_out, buffer,
485  size, &bytes_written, 0);
486 
487  while (result == LIBUSB_ERROR_PIPE && retries < 3) {
488  /* Try clearing the pipe stall and retry transfer */
489  libusb_clear_halt(xds110.dev, xds110.endpoint_out);
490  result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_out, buffer,
491  size, &bytes_written, 0);
492  retries++;
493  }
494 
495  if (written)
496  *written = bytes_written;
497 
498  return (result == 0 && size == bytes_written) ? true : false;
499 }
500 
501 static bool usb_get_response(uint32_t *total_bytes_read, uint32_t timeout)
502 {
503  static unsigned char buffer[MAX_PACKET];
504  int bytes_read;
505  uint16_t size;
506  uint16_t count;
507  bool success;
508 
509  size = 0;
510  success = true;
511  while (success) {
512  success = usb_read(buffer, sizeof(buffer), &bytes_read, timeout);
513  if (success) {
514  /*
515  * Validate that this appears to be a good response packet
516  * First check it contains enough data for header and error
517  * code, plus the first character is the start character
518  */
519  if (bytes_read >= 7 && '*' == buffer[0]) {
520  /* Extract the payload size */
521  size = xds110_get_u16(&buffer[1]);
522  /* Sanity test on payload size */
523  if (USB_PAYLOAD_SIZE >= size && 4 <= size) {
524  /* Check we didn't get more data than expected */
525  if ((bytes_read - 3) <= size) {
526  /* Packet appears to be valid, move on */
527  break;
528  }
529  }
530  }
531  }
532  /*
533  * Somehow received an invalid packet, retry till we
534  * time out or a valid response packet is received
535  */
536  }
537 
538  /* Abort now if we didn't receive a valid response */
539  if (!success) {
540  if (total_bytes_read)
541  *total_bytes_read = 0;
542  return false;
543  }
544 
545  /* Build the return payload into xds110.read_payload */
546 
547  /* Copy over payload data from received buffer (skipping header) */
548  count = 0;
549  bytes_read -= 3;
550  memcpy((void *)&xds110.read_payload[count], (void *)&buffer[3], bytes_read);
551  count += bytes_read;
552  /*
553  * Drop timeout to just 1/2 second. Once the XDS110 starts sending
554  * a response, the remaining packets should arrive in short order
555  */
556  if (timeout > 500)
557  timeout = 500; /* ms */
558 
559  /* If there's more data to retrieve, get it now */
560  while ((count < size) && success) {
561  success = usb_read(buffer, sizeof(buffer), &bytes_read, timeout);
562  if (success) {
563  if ((count + bytes_read) > size) {
564  /* Read too much data, not a valid packet, abort */
565  success = false;
566  } else {
567  /* Copy this data over to xds110.read_payload */
568  memcpy((void *)&xds110.read_payload[count], (void *)buffer,
569  bytes_read);
570  count += bytes_read;
571  }
572  }
573  }
574 
575  if (!success)
576  count = 0;
577  if (total_bytes_read)
578  *total_bytes_read = count;
579 
580  return success;
581 }
582 
583 static bool usb_send_command(uint16_t size)
584 {
585  /* Check the packet length */
586  if (size > USB_PAYLOAD_SIZE)
587  return false;
588 
589  /* Place the start character into the packet buffer */
590  xds110.write_packet[0] = '*';
591 
592  /* Place the payload size into the packet buffer */
594 
595  /* Adjust size to include header */
596  size += 3;
597 
598  /* Send the data via the USB connection */
599  return usb_write(xds110.write_packet, (int)size, NULL);
600 }
601 
602 /***************************************************************************
603  * XDS110 firmware API routines *
604  * *
605  * The following functions handle calling into the XDS110 firmware to *
606  * perform requested debug actions. *
607  ***************************************************************************/
608 
609 static bool xds_execute(uint32_t out_length, uint32_t in_length,
610  uint32_t attempts, uint32_t timeout)
611 {
612  bool done = false;
613  bool success = true;
614  int error = 0;
615  uint32_t bytes_read = 0;
616 
617  if (!xds110.dev)
618  return false;
619 
620  while (!done && attempts > 0) {
621  attempts--;
622 
623  /* Send command to XDS110 */
624  success = usb_send_command(out_length);
625 
626  if (success) {
627  /* Get response from XDS110 */
628  success = usb_get_response(&bytes_read, timeout);
629  }
630 
631  if (success) {
632  /* Check for valid response from XDS code handling */
633  if (bytes_read != in_length) {
634  /* Unexpected amount of data returned */
635  success = false;
636  LOG_DEBUG("XDS110: command 0x%02x return %" PRIu32 " bytes, expected %" PRIu32,
637  xds110.write_payload[0], bytes_read, in_length);
638  } else {
639  /* Extract error code from return packet */
640  error = (int)xds110_get_u32(&xds110.read_payload[0]);
641  done = true;
642  if (error != SC_ERR_NONE)
643  LOG_DEBUG("XDS110: command 0x%02x returned error %d",
644  xds110.write_payload[0], error);
645  }
646  }
647  }
648 
649  if (!success)
650  error = SC_ERR_XDS110_FAIL;
651 
652  if (error != 0)
653  success = false;
654 
655  return success;
656 }
657 
658 static bool xds_connect(void)
659 {
661 
664 }
665 
666 static bool xds_disconnect(void)
667 {
669 
672 }
673 
674 static bool xds_version(uint32_t *firmware_id, uint16_t *hardware_id)
675 {
676  uint8_t *fw_id_pntr = &xds110.read_payload[XDS_IN_LEN + 0]; /* 32-bits */
677  uint8_t *hw_id_pntr = &xds110.read_payload[XDS_IN_LEN + 4]; /* 16-bits */
678 
679  bool success;
680 
682 
685 
686  if (success) {
687  if (firmware_id)
688  *firmware_id = xds110_get_u32(fw_id_pntr);
689  if (hardware_id)
690  *hardware_id = xds110_get_u16(hw_id_pntr);
691  }
692 
693  return success;
694 }
695 
696 static bool xds_set_tck_delay(uint32_t delay)
697 {
698  uint8_t *delay_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
699 
701 
702  xds110_set_u32(delay_pntr, delay);
703 
706 }
707 
708 static bool xds_set_trst(uint8_t trst)
709 {
710  uint8_t *trst_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
711 
713 
714  *trst_pntr = trst;
715 
718 }
719 
720 static bool xds_cycle_tck(uint32_t count)
721 {
722  uint8_t *count_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
723 
725 
726  xds110_set_u32(count_pntr, count);
727 
730 }
731 
732 static bool xds_goto_state(uint32_t state)
733 {
734  uint8_t *state_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
735  uint8_t *transit_pntr = &xds110.write_payload[XDS_OUT_LEN+4]; /* 32-bits */
736 
738 
739  xds110_set_u32(state_pntr, state);
741 
744 }
745 
746 static bool xds_jtag_scan(uint32_t shift_state, uint16_t shift_bits,
747  uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
748 {
749  uint8_t *bits_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 16-bits */
750  uint8_t *path_pntr = &xds110.write_payload[XDS_OUT_LEN + 2]; /* 8-bits */
751  uint8_t *trans1_pntr = &xds110.write_payload[XDS_OUT_LEN + 3]; /* 8-bits */
752  uint8_t *end_pntr = &xds110.write_payload[XDS_OUT_LEN + 4]; /* 8-bits */
753  uint8_t *trans2_pntr = &xds110.write_payload[XDS_OUT_LEN + 5]; /* 8-bits */
754  uint8_t *pre_pntr = &xds110.write_payload[XDS_OUT_LEN + 6]; /* 16-bits */
755  uint8_t *pos_pntr = &xds110.write_payload[XDS_OUT_LEN + 8]; /* 16-bits */
756  uint8_t *delay_pntr = &xds110.write_payload[XDS_OUT_LEN + 10]; /* 16-bits */
757  uint8_t *rep_pntr = &xds110.write_payload[XDS_OUT_LEN + 12]; /* 16-bits */
758  uint8_t *out_pntr = &xds110.write_payload[XDS_OUT_LEN + 14]; /* 16-bits */
759  uint8_t *in_pntr = &xds110.write_payload[XDS_OUT_LEN + 16]; /* 16-bits */
760  uint8_t *data_out_pntr = &xds110.write_payload[XDS_OUT_LEN + 18];
761  uint8_t *data_in_pntr = &xds110.read_payload[XDS_IN_LEN+0];
762 
763  uint16_t total_bytes = DIV_ROUND_UP(shift_bits, 8);
764 
765  bool success;
766 
768 
769  xds110_set_u16(bits_pntr, shift_bits); /* bits to scan */
770  *path_pntr = (uint8_t)(shift_state & 0xff); /* IR vs DR path */
771  *trans1_pntr = (uint8_t)XDS_JTAG_TRANSIT_QUICKEST; /* start state route */
772  *end_pntr = (uint8_t)(end_state & 0xff); /* JTAG state after scan */
773  *trans2_pntr = (uint8_t)XDS_JTAG_TRANSIT_QUICKEST; /* end state route */
774  xds110_set_u16(pre_pntr, 0); /* number of preamble bits */
775  xds110_set_u16(pos_pntr, 0); /* number of postamble bits */
776  xds110_set_u16(delay_pntr, 0); /* number of extra TCKs after scan */
777  xds110_set_u16(rep_pntr, 1); /* number of repetitions */
778  xds110_set_u16(out_pntr, total_bytes); /* out buffer offset (if repeats) */
779  xds110_set_u16(in_pntr, total_bytes); /* in buffer offset (if repeats) */
780 
781  memcpy((void *)data_out_pntr, (void *)data_out, total_bytes);
782 
783  success = xds_execute(XDS_OUT_LEN + 18 + total_bytes,
785 
786  if (success)
787  memcpy((void *)data_in, (void *)data_in_pntr, total_bytes);
788 
789  return success;
790 }
791 
792 static bool xds_set_srst(uint8_t srst)
793 {
794  uint8_t *srst_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
795 
797 
798  *srst_pntr = srst;
799 
802 }
803 
804 static bool cmapi_connect(uint32_t *idcode)
805 {
806  uint8_t *idcode_pntr = &xds110.read_payload[XDS_IN_LEN + 0]; /* 32-bits */
807 
808  bool success;
809 
811 
814 
815  if (success) {
816  if (idcode)
817  *idcode = xds110_get_u32(idcode_pntr);
818  }
819 
820  return success;
821 }
822 
823 static bool cmapi_disconnect(void)
824 {
826 
829 }
830 
831 static bool cmapi_acquire(void)
832 {
834 
837 }
838 
839 static bool cmapi_release(void)
840 {
842 
845 }
846 
847 static bool cmapi_read_dap_reg(uint32_t type, uint32_t ap_num,
848  uint32_t address, uint32_t *value)
849 {
850  uint8_t *type_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
851  uint8_t *ap_num_pntr = &xds110.write_payload[XDS_OUT_LEN + 1]; /* 8-bits */
852  uint8_t *address_pntr = &xds110.write_payload[XDS_OUT_LEN + 2]; /* 8-bits */
853  uint8_t *value_pntr = &xds110.read_payload[XDS_IN_LEN + 0]; /* 32-bits */
854 
855  bool success;
856 
858 
859  *type_pntr = (uint8_t)(type & 0xff);
860  *ap_num_pntr = (uint8_t)(ap_num & 0xff);
861  *address_pntr = (uint8_t)(address & 0xff);
862 
865 
866  if (success) {
867  if (value)
868  *value = xds110_get_u32(value_pntr);
869  }
870 
871  return success;
872 }
873 
874 static bool cmapi_write_dap_reg(uint32_t type, uint32_t ap_num,
875  uint32_t address, uint32_t *value)
876 {
877  uint8_t *type_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
878  uint8_t *ap_num_pntr = &xds110.write_payload[XDS_OUT_LEN + 1]; /* 8-bits */
879  uint8_t *address_pntr = &xds110.write_payload[XDS_OUT_LEN + 2]; /* 8-bits */
880  uint8_t *value_pntr = &xds110.write_payload[XDS_OUT_LEN + 3]; /* 32-bits */
881 
882  if (!value)
883  return false;
884 
886 
887  *type_pntr = (uint8_t)(type & 0xff);
888  *ap_num_pntr = (uint8_t)(ap_num & 0xff);
889  *address_pntr = (uint8_t)(address & 0xff);
890  xds110_set_u32(value_pntr, *value);
891 
894 }
895 
896 static bool swd_connect(void)
897 {
899 
902 }
903 
904 static bool swd_disconnect(void)
905 {
907 
910 }
911 
912 static bool cjtag_connect(uint32_t format)
913 {
914  uint8_t *format_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
915 
917 
918  xds110_set_u32(format_pntr, format);
919 
922 }
923 
924 static bool cjtag_disconnect(void)
925 {
927 
930 }
931 
932 static bool xds_set_supply(uint32_t voltage)
933 {
934  uint8_t *volts_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
935  uint8_t *source_pntr = &xds110.write_payload[XDS_OUT_LEN + 4]; /* 8-bits */
936 
938 
939  xds110_set_u32(volts_pntr, voltage);
940  *source_pntr = (uint8_t)(voltage != 0 ? 1 : 0);
941 
944 }
945 
946 static bool ocd_dap_request(uint8_t *dap_requests, uint32_t request_size,
947  uint32_t *dap_results, uint32_t result_count)
948 {
949  uint8_t *request_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
950  uint8_t *result_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
951 
952  bool success;
953 
954  if (!dap_requests || !dap_results)
955  return false;
956 
958 
959  memcpy((void *)request_pntr, (void *)dap_requests, request_size);
960 
961  success = xds_execute(XDS_OUT_LEN + request_size,
962  XDS_IN_LEN + (result_count * 4), DEFAULT_ATTEMPTS,
964 
965  if (success && (result_count > 0))
966  memcpy((void *)dap_results, (void *)result_pntr, result_count * 4);
967 
968  return success;
969 }
970 
971 static bool ocd_scan_request(uint8_t *scan_requests, uint32_t request_size,
972  uint8_t *scan_results, uint32_t result_size)
973 {
974  uint8_t *request_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
975  uint8_t *result_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
976 
977  bool success;
978 
979  if (!scan_requests || !scan_results)
980  return false;
981 
983 
984  memcpy((void *)request_pntr, (void *)scan_requests, request_size);
985 
986  success = xds_execute(XDS_OUT_LEN + request_size,
987  XDS_IN_LEN + result_size, DEFAULT_ATTEMPTS,
989 
990  if (success && (result_size > 0))
991  memcpy((void *)scan_results, (void *)result_pntr, result_size);
992 
993  return success;
994 }
995 
996 static bool ocd_pathmove(uint32_t num_states, uint8_t *path)
997 {
998  uint8_t *num_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
999  uint8_t *path_pntr = &xds110.write_payload[XDS_OUT_LEN + 4];
1000 
1001  if (!path)
1002  return false;
1003 
1005 
1006  xds110_set_u32(num_pntr, num_states);
1007 
1008  memcpy((void *)path_pntr, (void *)path, num_states);
1009 
1010  return xds_execute(XDS_OUT_LEN + 4 + num_states, XDS_IN_LEN,
1012 }
1013 
1014 /***************************************************************************
1015  * swd driver interface *
1016  * *
1017  * The following functions provide SWD support to OpenOCD. *
1018  ***************************************************************************/
1019 
1020 static int xds110_swd_init(void)
1021 {
1022  xds110.is_swd_mode = true;
1023  return ERROR_OK;
1024 }
1025 
1027 {
1028  uint32_t idcode;
1029  bool success;
1030 
1031  switch (seq) {
1032  case LINE_RESET:
1033  LOG_ERROR("Sequence SWD line reset (%d) not supported", seq);
1034  return ERROR_FAIL;
1035  case JTAG_TO_SWD:
1036  LOG_DEBUG("JTAG-to-SWD");
1037  xds110.is_swd_mode = false;
1038  xds110.is_cmapi_connected = false;
1039  xds110.is_cmapi_acquired = false;
1040  /* Run sequence to put target in SWD mode */
1041  success = swd_connect();
1042  /* Re-initialize CMAPI API for DAP access */
1043  if (success) {
1044  xds110.is_swd_mode = true;
1045  success = cmapi_connect(&idcode);
1046  if (success) {
1047  xds110.is_cmapi_connected = true;
1048  success = cmapi_acquire();
1049  }
1050  }
1051  break;
1052  case SWD_TO_JTAG:
1053  LOG_DEBUG("SWD-to-JTAG");
1054  xds110.is_swd_mode = false;
1055  xds110.is_cmapi_connected = false;
1056  xds110.is_cmapi_acquired = false;
1057  /* Run sequence to put target in JTAG mode */
1058  success = swd_disconnect();
1059  if (success) {
1060  /* Re-initialize JTAG interface */
1061  success = cjtag_connect(MODE_JTAG);
1062  }
1063  break;
1064  default:
1065  LOG_ERROR("Sequence %d not supported", seq);
1066  return ERROR_FAIL;
1067  }
1068 
1069  if (success)
1070  return ERROR_OK;
1071  else
1072  return ERROR_FAIL;
1073 }
1074 
1075 static bool xds110_legacy_read_reg(uint8_t cmd, uint32_t *value)
1076 {
1077  /* Make sure this is a read request */
1078  bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
1079  /* Determine whether this is a DP or AP register access */
1080  uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
1081  /* Determine the AP number from cached SELECT value */
1082  uint32_t ap_num = (xds110.select & 0xff000000) >> 24;
1083  /* Extract register address from command */
1084  uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
1085  /* Extract bank address from cached SELECT value */
1086  uint32_t bank = (xds110.select & 0x000000f0);
1087 
1088  uint32_t reg_value = 0;
1089  uint32_t temp_value = 0;
1090 
1091  bool success;
1092 
1093  if (!is_read_request)
1094  return false;
1095 
1096  if (type == DAP_AP) {
1097  /* Add bank address to register address for CMAPI call */
1098  address |= bank;
1099  }
1100 
1101  if (DAP_DP == type && DAP_DP_RDBUFF == address && xds110.use_rdbuff) {
1102  /* If RDBUFF is cached and this is a DP RDBUFF read, use the cache */
1103  reg_value = xds110.rdbuff;
1104  success = true;
1105  } else if (DAP_AP == type && DAP_AP_DRW == address && xds110.use_rdbuff) {
1106  /* If RDBUFF is cached and this is an AP DRW read, use the cache, */
1107  /* but still call into the firmware to get the next read. */
1108  reg_value = xds110.rdbuff;
1109  success = cmapi_read_dap_reg(type, ap_num, address, &temp_value);
1110  } else {
1111  success = cmapi_read_dap_reg(type, ap_num, address, &temp_value);
1112  if (success)
1113  reg_value = temp_value;
1114  }
1115 
1116  /* Mark that we have consumed or invalidated the RDBUFF cache */
1117  xds110.use_rdbuff = false;
1118 
1119  /* Handle result of read attempt */
1120  if (!success)
1121  LOG_ERROR("XDS110: failed to read DAP register");
1122  else if (value)
1123  *value = reg_value;
1124 
1125  if (success && DAP_AP == type) {
1126  /*
1127  * On a successful DAP AP read, we actually have the value from RDBUFF,
1128  * the firmware will have run the AP request and made the RDBUFF read
1129  */
1130  xds110.use_rdbuff = true;
1131  xds110.rdbuff = temp_value;
1132  }
1133 
1134  return success;
1135 }
1136 
1137 static bool xds110_legacy_write_reg(uint8_t cmd, uint32_t value)
1138 {
1139  /* Make sure this isn't a read request */
1140  bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
1141  /* Determine whether this is a DP or AP register access */
1142  uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
1143  /* Determine the AP number from cached SELECT value */
1144  uint32_t ap_num = (xds110.select & 0xff000000) >> 24;
1145  /* Extract register address from command */
1146  uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
1147  /* Extract bank address from cached SELECT value */
1148  uint32_t bank = (xds110.select & 0x000000f0);
1149 
1150  bool success;
1151 
1152  if (is_read_request)
1153  return false;
1154 
1155  /* Invalidate the RDBUFF cache */
1156  xds110.use_rdbuff = false;
1157 
1158  if (type == DAP_AP) {
1159  /* Add bank address to register address for CMAPI call */
1160  address |= bank;
1161  /* Any write to an AP register invalidates the firmware's cache */
1162  xds110.is_ap_dirty = true;
1163  } else if (address == DAP_DP_SELECT) {
1164  /* Any write to the SELECT register invalidates the firmware's cache */
1165  xds110.is_ap_dirty = true;
1166  }
1167 
1168  success = cmapi_write_dap_reg(type, ap_num, address, &value);
1169 
1170  if (!success) {
1171  LOG_ERROR("XDS110: failed to write DAP register");
1172  } else {
1173  /*
1174  * If the debugger wrote to SELECT, cache the value
1175  * to use to build the apNum and address values above
1176  */
1177  if ((type == DAP_DP) && (address == DAP_DP_SELECT))
1178  xds110.select = value;
1179  }
1180 
1181  return success;
1182 }
1183 
1184 static int xds110_swd_run_queue(void)
1185 {
1186  static uint32_t dap_results[MAX_RESULT_QUEUE];
1187  uint8_t cmd;
1188  uint32_t request;
1189  uint32_t result;
1190  uint32_t value;
1191  bool success = true;
1192 
1193  if (xds110.txn_request_size == 0)
1194  return ERROR_OK;
1195 
1196  /* Terminate request queue */
1198 
1200  /* XDS110 firmware has the API to directly handle the queue */
1203  } else {
1204  /* Legacy firmware needs to handle queue via discrete DAP calls */
1205  request = 0;
1206  result = 0;
1207  while (xds110.txn_requests[request] != 0) {
1208  cmd = xds110.txn_requests[request++];
1209  if (0 == (SWD_CMD_RNW & cmd)) {
1210  /* DAP register write command */
1211  value = (uint32_t)(xds110.txn_requests[request++]) << 0;
1212  value |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1213  value |= (uint32_t)(xds110.txn_requests[request++]) << 16;
1214  value |= (uint32_t)(xds110.txn_requests[request++]) << 24;
1215  if (success)
1216  success = xds110_legacy_write_reg(cmd, value);
1217  } else {
1218  /* DAP register read command */
1219  value = 0;
1220  if (success)
1221  success = xds110_legacy_read_reg(cmd, &value);
1222  dap_results[result++] = value;
1223  }
1224  }
1225  }
1226 
1227  /* Transfer results into caller's buffers */
1228  for (result = 0; result < xds110.txn_result_count; result++)
1229  if (xds110.txn_dap_results[result])
1230  *xds110.txn_dap_results[result] = dap_results[result];
1231 
1233  xds110.txn_result_size = 0;
1235 
1236  return (success) ? ERROR_OK : ERROR_FAIL;
1237 }
1238 
1239 static void xds110_swd_queue_cmd(uint8_t cmd, uint32_t *value)
1240 {
1241  /* Check if this is a read or write request */
1242  bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
1243  /* Determine whether this is a DP or AP register access */
1244  uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
1245  /* Extract register address from command */
1246  uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
1247  uint32_t request_size = (is_read_request) ? 1 : 5;
1248 
1249  /* Check if new request would be too large to fit */
1250  if (((xds110.txn_request_size + request_size + 1) > MAX_DATA_BLOCK) ||
1253 
1254  /* Set the START bit in cmd to ensure cmd is not zero */
1255  /* (a value of zero is used to terminate the buffer) */
1256  cmd |= SWD_CMD_START;
1257 
1258  /* Add request to queue; queue is built marshalled for XDS110 call */
1259  if (is_read_request) {
1260  /* Queue read request, save pointer to pass back result */
1263  xds110.txn_result_size += 4;
1264  } else {
1265  /* Check for and prevent sticky overrun detection */
1266  if (DAP_DP == type && DAP_DP_CTRL == address &&
1267  (*value & CORUNDETECT)) {
1268  LOG_DEBUG("XDS110: refusing to enable sticky overrun detection");
1269  *value &= ~CORUNDETECT;
1270  }
1271  /* Queue write request, add value directly to queue buffer */
1273  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 0) & 0xff;
1274  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 8) & 0xff;
1275  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 16) & 0xff;
1276  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 24) & 0xff;
1277  }
1278 }
1279 
1280 static void xds110_swd_read_reg(uint8_t cmd, uint32_t *value,
1281  uint32_t ap_delay_clk)
1282 {
1283  assert(cmd & SWD_CMD_RNW);
1284  xds110_swd_queue_cmd(cmd, value);
1285 }
1286 static void xds110_swd_write_reg(uint8_t cmd, uint32_t value,
1287  uint32_t ap_delay_clk)
1288 {
1289  assert(!(cmd & SWD_CMD_RNW));
1290  xds110_swd_queue_cmd(cmd, &value);
1291 }
1292 
1293 /***************************************************************************
1294  * jtag interface *
1295  * *
1296  * The following functions provide XDS110 interface to OpenOCD. *
1297  ***************************************************************************/
1298 
1299 static void xds110_show_info(void)
1300 {
1301  uint32_t firmware = xds110.firmware;
1302 
1303  LOG_INFO("XDS110: vid/pid = %04x/%04x", xds110.vid, xds110.pid);
1304  LOG_INFO("XDS110: firmware version = %" PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32,
1305  (((firmware >> 28) & 0xf) * 10) + ((firmware >> 24) & 0xf),
1306  (((firmware >> 20) & 0xf) * 10) + ((firmware >> 16) & 0xf),
1307  (((firmware >> 12) & 0xf) * 10) + ((firmware >> 8) & 0xf),
1308  (((firmware >> 4) & 0xf) * 10) + ((firmware >> 0) & 0xf));
1309  LOG_INFO("XDS110: hardware version = 0x%04x", xds110.hardware);
1311  LOG_INFO("XDS110: serial number = %s", adapter_get_required_serial());
1312  if (xds110.is_swd_mode) {
1313  LOG_INFO("XDS110: connected to target via SWD");
1314  LOG_INFO("XDS110: SWCLK set to %" PRIu32 " kHz", xds110.speed);
1315  } else {
1316  LOG_INFO("XDS110: connected to target via JTAG");
1317  LOG_INFO("XDS110: TCK set to %" PRIu32 " kHz", xds110.speed);
1318  }
1319 
1320  /* Alert user that there's a better firmware to use */
1321  if (firmware < OCD_FIRMWARE_VERSION) {
1322  LOG_WARNING("XDS110: the firmware is not optimized for OpenOCD");
1324  }
1325 }
1326 
1327 static int xds110_quit(void)
1328 {
1329  if (xds110.is_cmapi_acquired) {
1330  (void)cmapi_release();
1331  xds110.is_cmapi_acquired = false;
1332  }
1333  if (xds110.is_cmapi_connected) {
1334  (void)cmapi_disconnect();
1335  xds110.is_cmapi_connected = false;
1336  }
1337  if (xds110.is_connected) {
1338  if (xds110.is_swd_mode) {
1339  /* Switch out of SWD mode */
1340  (void)swd_disconnect();
1341  } else {
1342  /* Switch out of cJTAG mode */
1343  (void)cjtag_disconnect();
1344  }
1345  /* Tell firmware we're disconnecting */
1346  (void)xds_disconnect();
1347  xds110.is_connected = false;
1348  }
1349  /* Close down the USB connection to the XDS110 debug probe */
1350  usb_disconnect();
1351 
1352  return ERROR_OK;
1353 }
1354 
1355 static int xds110_init(void)
1356 {
1357  bool success;
1358 
1359  /* Establish USB connection to the XDS110 debug probe */
1360  success = usb_connect();
1361 
1362  if (success) {
1363  /* Send connect message to XDS110 firmware */
1364  success = xds_connect();
1365  if (success)
1366  xds110.is_connected = true;
1367  }
1368 
1369  if (success) {
1370  uint32_t firmware;
1371  uint16_t hardware;
1372 
1373  /* Retrieve version IDs from firmware */
1374  /* Version numbers are stored in BCD format */
1375  success = xds_version(&firmware, &hardware);
1376  if (success) {
1377  /* Save the firmware and hardware version */
1378  xds110.firmware = firmware;
1379  xds110.hardware = hardware;
1380  }
1381  }
1382 
1383  if (success) {
1384  /* Set supply voltage for stand-alone probes */
1386  success = xds_set_supply(xds110.voltage);
1387  /* Allow time for target device to power up */
1388  /* (CC32xx takes up to 1300 ms before debug is enabled) */
1389  alive_sleep(1500);
1390  } else if (xds110.voltage != 0) {
1391  /* Voltage supply not a feature of embedded probes */
1392  LOG_WARNING(
1393  "XDS110: ignoring supply voltage, not supported on this probe");
1394  }
1395  }
1396 
1397  if (success) {
1398  success = xds_set_trst(0);
1399  if (success)
1400  success = xds_cycle_tck(50);
1401  if (success)
1402  success = xds_set_trst(1);
1403  if (success)
1404  success = xds_cycle_tck(50);
1405  }
1406 
1407  if (success) {
1408  if (xds110.is_swd_mode) {
1409  /* Switch to SWD if needed */
1410  success = swd_connect();
1411  } else {
1412  success = cjtag_connect(MODE_JTAG);
1413  }
1414  }
1415 
1416  if (success && xds110.is_swd_mode) {
1417  uint32_t idcode;
1418 
1419  /* Connect to CMAPI interface in XDS110 */
1420  success = cmapi_connect(&idcode);
1421 
1422  /* Acquire exclusive access to CMAPI interface */
1423  if (success) {
1424  xds110.is_cmapi_connected = true;
1425  success = cmapi_acquire();
1426  if (success)
1427  xds110.is_cmapi_acquired = true;
1428  }
1429  }
1430 
1431  if (!success)
1432  xds110_quit();
1433 
1434  if (success)
1435  xds110_show_info();
1436 
1437  return (success) ? ERROR_OK : ERROR_FAIL;
1438 }
1439 
1440 static void xds110_legacy_scan(uint32_t shift_state, uint32_t total_bits,
1441  uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
1442 {
1443  (void)xds_jtag_scan(shift_state, total_bits, end_state, data_out, data_in);
1444 }
1445 
1446 static void xds110_legacy_runtest(uint32_t clocks, uint32_t end_state)
1447 {
1449  xds_cycle_tck(clocks);
1450  xds_goto_state(end_state);
1451 }
1452 
1453 static void xds110_legacy_stableclocks(uint32_t clocks)
1454 {
1455  xds_cycle_tck(clocks);
1456 }
1457 
1458 static void xds110_flush(void)
1459 {
1460  uint8_t command;
1461  uint32_t clocks;
1462  uint32_t shift_state;
1463  uint32_t end_state;
1464  uint32_t bits;
1465  uint32_t bytes;
1466  uint32_t request;
1467  uint32_t result;
1468  uint8_t *data_out;
1469  uint8_t data_in[MAX_DATA_BLOCK];
1470  uint8_t *data_pntr;
1471 
1472  if (xds110.txn_request_size == 0)
1473  return;
1474 
1475  /* Terminate request queue */
1477 
1479  /* Updated firmware has the API to directly handle the queue */
1481  data_in, xds110.txn_result_size);
1482  } else {
1483  /* Legacy firmware needs to handle queue via discrete JTAG calls */
1484  request = 0;
1485  result = 0;
1486  while (xds110.txn_requests[request] != 0) {
1487  command = xds110.txn_requests[request++];
1488  switch (command) {
1489  case CMD_IR_SCAN:
1490  case CMD_DR_SCAN:
1491  if (command == CMD_IR_SCAN)
1492  shift_state = XDS_JTAG_STATE_SHIFT_IR;
1493  else
1494  shift_state = XDS_JTAG_STATE_SHIFT_DR;
1495  end_state = (uint32_t)(xds110.txn_requests[request++]);
1496  bits = (uint32_t)(xds110.txn_requests[request++]) << 0;
1497  bits |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1498  data_out = &xds110.txn_requests[request];
1499  bytes = DIV_ROUND_UP(bits, 8);
1500  xds110_legacy_scan(shift_state, bits, end_state, data_out,
1501  &data_in[result]);
1502  result += bytes;
1503  request += bytes;
1504  break;
1505  case CMD_RUNTEST:
1506  clocks = (uint32_t)(xds110.txn_requests[request++]) << 0;
1507  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1508  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 16;
1509  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 24;
1510  end_state = (uint32_t)xds110.txn_requests[request++];
1511  xds110_legacy_runtest(clocks, end_state);
1512  break;
1513  case CMD_STABLECLOCKS:
1514  clocks = (uint32_t)(xds110.txn_requests[request++]) << 0;
1515  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1516  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 16;
1517  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 24;
1519  break;
1520  default:
1521  LOG_ERROR("BUG: unknown JTAG command type 0x%x encountered",
1522  command);
1523  exit(-1);
1524  break;
1525  }
1526  }
1527  }
1528 
1529  /* Transfer results into caller's buffers from data_in buffer */
1530  bits = 0; /* Bit offset into current scan result */
1531  data_pntr = data_in;
1532  for (result = 0; result < xds110.txn_result_count; result++) {
1533  if (xds110.txn_scan_results[result].first) {
1534  if (bits != 0) {
1535  bytes = DIV_ROUND_UP(bits, 8);
1536  data_pntr += bytes;
1537  }
1538  bits = 0;
1539  }
1540  if (xds110.txn_scan_results[result].buffer)
1541  bit_copy(xds110.txn_scan_results[result].buffer, 0, data_pntr,
1543  bits += xds110.txn_scan_results[result].num_bits;
1544  }
1545 
1547  xds110.txn_result_size = 0;
1549 }
1550 
1551 static int xds110_reset(int trst, int srst)
1552 {
1553  uint8_t value;
1554  bool success;
1555  int retval = ERROR_OK;
1556 
1557  if (trst != -1) {
1558  if (trst == 0) {
1559  /* Deassert nTRST (active low) */
1560  value = 1;
1561  } else {
1562  /* Assert nTRST (active low) */
1563  value = 0;
1564  }
1565  success = xds_set_trst(value);
1566  if (!success)
1567  retval = ERROR_FAIL;
1568  }
1569 
1570  if (srst != -1) {
1571  if (srst == 0) {
1572  /* Deassert nSRST (active low) */
1573  value = 1;
1574  } else {
1575  /* Assert nSRST (active low) */
1576  value = 0;
1577  }
1578  success = xds_set_srst(value);
1579  if (!success)
1580  retval = ERROR_FAIL;
1581 
1582  /* Toggle TCK to trigger HIB on CC13x/CC26x devices */
1583  if (success && !xds110.is_swd_mode) {
1584  /* Toggle TCK for about 50 ms */
1585  success = xds_cycle_tck(xds110.speed * 50);
1586  }
1587 
1588  if (!success)
1589  retval = ERROR_FAIL;
1590  }
1591 
1592  return retval;
1593 }
1594 
1596 {
1597  jtag_sleep(cmd->cmd.sleep->us);
1598 }
1599 
1601 {
1603 }
1604 
1606 {
1607  uint32_t num_states;
1608  uint8_t *path;
1609 
1610  num_states = (uint32_t)cmd->cmd.pathmove->num_states;
1611 
1612  if (num_states == 0)
1613  return;
1614 
1615  path = malloc(num_states * sizeof(uint8_t));
1616  if (!path) {
1617  LOG_ERROR("XDS110: unable to allocate memory");
1618  return;
1619  }
1620 
1621  /* Convert requested path states into XDS API states */
1622  for (unsigned int i = 0; i < num_states; i++)
1623  path[i] = (uint8_t)xds_jtag_state[cmd->cmd.pathmove->path[i]];
1624 
1626  /* Updated firmware fully supports pathmove */
1627  (void)ocd_pathmove(num_states, path);
1628  } else {
1629  /* Notify user that legacy firmware simply cannot handle pathmove */
1630  LOG_ERROR("XDS110: the firmware does not support pathmove command");
1632  /* If pathmove is required, then debug is not possible */
1633  exit(-1);
1634  }
1635 
1636  free((void *)path);
1637 }
1638 
1639 static void xds110_queue_scan(struct jtag_command *cmd)
1640 {
1641  uint32_t offset;
1642  uint32_t total_fields;
1643  uint32_t total_bits;
1644  uint32_t total_bytes;
1645  uint8_t end_state;
1646  uint8_t *buffer;
1647 
1648  /* Calculate the total number of bits to scan */
1649  total_bits = 0;
1650  total_fields = 0;
1651  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++) {
1652  total_fields++;
1653  total_bits += (uint32_t)cmd->cmd.scan->fields[i].num_bits;
1654  }
1655 
1656  if (total_bits == 0)
1657  return;
1658 
1659  total_bytes = DIV_ROUND_UP(total_bits, 8);
1660 
1661  /* Check if new request would be too large to fit */
1662  if (((xds110.txn_request_size + 1 + total_bytes + sizeof(end_state) + 1)
1663  > MAX_DATA_BLOCK) || ((xds110.txn_result_count + total_fields) >
1665  xds110_flush();
1666 
1667  /* Check if this single request is too large to fit */
1668  if ((1 + total_bytes + sizeof(end_state) + 1) > MAX_DATA_BLOCK) {
1669  LOG_ERROR("BUG: JTAG scan request is too large to handle (%" PRIu32 " bits)",
1670  total_bits);
1671  /* Failing to run this scan mucks up debug on this target */
1672  exit(-1);
1673  }
1674 
1675  if (cmd->cmd.scan->ir_scan)
1677  else
1679 
1680  end_state = (uint8_t)xds_jtag_state[cmd->cmd.scan->end_state];
1681  xds110.txn_requests[xds110.txn_request_size++] = end_state;
1682 
1683  xds110.txn_requests[xds110.txn_request_size++] = (total_bits >> 0) & 0xff;
1684  xds110.txn_requests[xds110.txn_request_size++] = (total_bits >> 8) & 0xff;
1685 
1686  /* Build request data by flattening fields into single buffer */
1687  /* also populate the results array to return the results when run */
1688  offset = 0;
1690  /* Clear data out buffer to default value of all zeros */
1691  memset((void *)buffer, 0x00, total_bytes);
1692  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++) {
1693  if (cmd->cmd.scan->fields[i].out_value) {
1694  /* Copy over data to scan out into request buffer */
1695  bit_copy(buffer, offset, cmd->cmd.scan->fields[i].out_value, 0,
1696  cmd->cmd.scan->fields[i].num_bits);
1697  }
1698  offset += cmd->cmd.scan->fields[i].num_bits;
1701  cmd->cmd.scan->fields[i].num_bits;
1703  cmd->cmd.scan->fields[i].in_value;
1704  }
1705  xds110.txn_request_size += total_bytes;
1706  xds110.txn_result_size += total_bytes;
1707 }
1708 
1710 {
1711  uint32_t clocks = cmd->cmd.stableclocks->num_cycles;
1712  uint8_t end_state = (uint8_t)xds_jtag_state[cmd->cmd.runtest->end_state];
1713 
1714  /* Check if new request would be too large to fit */
1715  if ((xds110.txn_request_size + 1 + sizeof(clocks) + sizeof(end_state) + 1)
1716  > MAX_DATA_BLOCK)
1717  xds110_flush();
1718 
1719  /* Queue request and cycle count directly to queue buffer */
1721  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 0) & 0xff;
1722  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 8) & 0xff;
1723  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 16) & 0xff;
1724  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 24) & 0xff;
1725  xds110.txn_requests[xds110.txn_request_size++] = end_state;
1726 }
1727 
1729 {
1730  uint32_t clocks = cmd->cmd.stableclocks->num_cycles;
1731 
1732  /* Check if new request would be too large to fit */
1733  if ((xds110.txn_request_size + 1 + sizeof(clocks) + 1) > MAX_DATA_BLOCK)
1734  xds110_flush();
1735 
1736  /* Queue request and cycle count directly to queue buffer */
1738  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 0) & 0xff;
1739  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 8) & 0xff;
1740  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 16) & 0xff;
1741  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 24) & 0xff;
1742 }
1743 
1745 {
1746  switch (cmd->type) {
1747  case JTAG_SLEEP:
1748  xds110_flush();
1750  break;
1751  case JTAG_TLR_RESET:
1752  xds110_flush();
1754  break;
1755  case JTAG_PATHMOVE:
1756  xds110_flush();
1758  break;
1759  case JTAG_SCAN:
1761  break;
1762  case JTAG_RUNTEST:
1764  break;
1765  case JTAG_STABLECLOCKS:
1767  break;
1768  case JTAG_TMS:
1769  default:
1770  LOG_ERROR("BUG: unknown JTAG command type 0x%x encountered",
1771  cmd->type);
1772  exit(-1);
1773  }
1774 }
1775 
1776 static int xds110_execute_queue(struct jtag_command *cmd_queue)
1777 {
1778  struct jtag_command *cmd = cmd_queue;
1779 
1780  while (cmd) {
1782  cmd = cmd->next;
1783  }
1784 
1785  xds110_flush();
1786 
1787  return ERROR_OK;
1788 }
1789 
1790 static int xds110_speed(int speed)
1791 {
1792  double freq_to_use;
1793  uint32_t delay_count;
1794  bool success;
1795 
1796  if (speed == 0) {
1797  LOG_INFO("XDS110: RTCK not supported");
1799  }
1800 
1801  if (speed < XDS110_MIN_TCK_SPEED) {
1802  LOG_INFO("XDS110: increase speed request: %d kHz to %d kHz minimum",
1803  speed, XDS110_MIN_TCK_SPEED);
1804  speed = XDS110_MIN_TCK_SPEED;
1805  }
1806 
1807  /* Older XDS110 firmware had inefficient scan routines and could only */
1808  /* achieve a peak TCK frequency of about 2500 kHz */
1810 
1811  /* Check for request for top speed or higher */
1812  if (speed >= XDS110_MAX_SLOW_TCK_SPEED) {
1813 
1814  /* Inform user that speed was adjusted down to max possible */
1815  if (speed > XDS110_MAX_SLOW_TCK_SPEED) {
1816  LOG_INFO(
1817  "XDS110: reduce speed request: %d kHz to %d kHz maximum",
1818  speed, XDS110_MAX_SLOW_TCK_SPEED);
1819  speed = XDS110_MAX_SLOW_TCK_SPEED;
1820  }
1821  delay_count = 0;
1822 
1823  } else {
1824 
1825  const double xds110_tck_pulse_increment = 66.0;
1826  freq_to_use = speed * 1000; /* Hz */
1827  delay_count = 0;
1828 
1829  /* Calculate the delay count value */
1830  double one_giga = 1000000000;
1831  /* Get the pulse duration for the max frequency supported in ns */
1832  double max_freq_pulse_duration = one_giga /
1833  (XDS110_MAX_SLOW_TCK_SPEED * 1000);
1834 
1835  /* Convert frequency to pulse duration */
1836  double freq_to_pulse_width_in_ns = one_giga / freq_to_use;
1837 
1838  /*
1839  * Start with the pulse duration for the maximum frequency. Keep
1840  * decrementing time added by each count value till the requested
1841  * frequency pulse is less than the calculated value.
1842  */
1843  double current_value = max_freq_pulse_duration;
1844 
1845  while (current_value < freq_to_pulse_width_in_ns) {
1846  current_value += xds110_tck_pulse_increment;
1847  ++delay_count;
1848  }
1849 
1850  /*
1851  * Determine which delay count yields the best match.
1852  * The one obtained above or one less.
1853  */
1854  if (delay_count) {
1855  double diff_freq_1 = freq_to_use -
1856  (one_giga / (max_freq_pulse_duration +
1857  (xds110_tck_pulse_increment * delay_count)));
1858  double diff_freq_2 = (one_giga / (max_freq_pulse_duration +
1859  (xds110_tck_pulse_increment * (delay_count - 1)))) -
1860  freq_to_use;
1861 
1862  /* One less count value yields a better match */
1863  if (diff_freq_1 > diff_freq_2)
1864  --delay_count;
1865  }
1866  }
1867 
1868  /* Newer firmware has reworked TCK routines that are much more efficient */
1869  /* and can now achieve a peak TCK frequency of 14000 kHz */
1870  } else {
1871 
1872  if (speed >= XDS110_MAX_FAST_TCK_SPEED) {
1873  if (speed > XDS110_MAX_FAST_TCK_SPEED) {
1874  LOG_INFO(
1875  "XDS110: reduce speed request: %d kHz to %d kHz maximum",
1876  speed, XDS110_MAX_FAST_TCK_SPEED);
1877  speed = XDS110_MAX_FAST_TCK_SPEED;
1878  }
1879  delay_count = 0;
1880  } else if (speed >= 12000 && xds110.firmware >=
1882  delay_count = FAST_TCK_DELAY_12000_KHZ;
1883  } else if (speed >= 10000 && xds110.firmware >=
1885  delay_count = FAST_TCK_DELAY_10000_KHZ;
1886  } else if (speed >= 8500) {
1887  delay_count = FAST_TCK_DELAY_8500_KHZ;
1888  } else if (speed >= 5500) {
1889  delay_count = FAST_TCK_DELAY_5500_KHZ;
1890  } else {
1891  /* Calculate the delay count to set the frequency */
1892  /* Formula determined by measuring the waveform on Saeleae logic */
1893  /* analyzer using known values for delay count */
1894  const double m = 17100000.0; /* slope */
1895  const double b = -1.02; /* y-intercept */
1896 
1897  freq_to_use = speed * 1000; /* Hz */
1898  double period = 1.0/freq_to_use;
1899  double delay = m * period + b;
1900 
1901  if (delay < 1.0)
1902  delay_count = 1;
1903  else
1904  delay_count = (uint32_t)delay;
1905  }
1906  }
1907 
1908  /* Send the delay count to the XDS110 firmware */
1909  success = xds_set_tck_delay(delay_count);
1910 
1911  if (success) {
1912  xds110.delay_count = delay_count;
1913  xds110.speed = speed;
1914  }
1915 
1916  return (success) ? ERROR_OK : ERROR_FAIL;
1917 }
1918 
1919 static int xds110_speed_div(int speed, int *khz)
1920 {
1921  *khz = speed;
1922  return ERROR_OK;
1923 }
1924 
1925 static int xds110_khz(int khz, int *jtag_speed)
1926 {
1927  *jtag_speed = khz;
1928  return ERROR_OK;
1929 }
1930 
1931 COMMAND_HANDLER(xds110_handle_info_command)
1932 {
1933  xds110_show_info();
1934  return ERROR_OK;
1935 }
1936 
1937 COMMAND_HANDLER(xds110_handle_supply_voltage_command)
1938 {
1939  uint32_t voltage = 0;
1940 
1941  if (CMD_ARGC == 1) {
1942  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], voltage);
1943  if (voltage == 0 || (voltage >= XDS110_MIN_VOLTAGE && voltage
1944  <= XDS110_MAX_VOLTAGE)) {
1945  /* Requested voltage is in range */
1946  xds110.voltage = voltage;
1947  } else {
1948  LOG_ERROR("XDS110: voltage must be 0 or between %d and %d "
1949  "millivolts", XDS110_MIN_VOLTAGE, XDS110_MAX_VOLTAGE);
1950  return ERROR_FAIL;
1951  }
1952  xds110.voltage = voltage;
1953  } else
1955 
1956  return ERROR_OK;
1957 }
1958 
1959 static const struct command_registration xds110_subcommand_handlers[] = {
1960  {
1961  .name = "info",
1962  .handler = &xds110_handle_info_command,
1963  .mode = COMMAND_EXEC,
1964  .help = "show XDS110 info",
1965  .usage = "",
1966  },
1967  {
1968  .name = "supply",
1969  .handler = &xds110_handle_supply_voltage_command,
1970  .mode = COMMAND_CONFIG,
1971  .help = "set the XDS110 probe supply voltage",
1972  .usage = "voltage_in_millivolts",
1973  },
1975 };
1976 
1977 static const struct command_registration xds110_command_handlers[] = {
1978  {
1979  .name = "xds110",
1980  .mode = COMMAND_ANY,
1981  .help = "perform XDS110 management",
1982  .usage = "",
1983  .chain = xds110_subcommand_handlers,
1984  },
1986 };
1987 
1988 static const struct swd_driver xds110_swd_driver = {
1989  .init = xds110_swd_init,
1990  .switch_seq = xds110_swd_switch_seq,
1991  .read_reg = xds110_swd_read_reg,
1992  .write_reg = xds110_swd_write_reg,
1993  .run = xds110_swd_run_queue,
1994 };
1995 
1996 static struct jtag_interface xds110_interface = {
1998 };
1999 
2001  .name = "xds110",
2002  .transport_ids = TRANSPORT_SWD | TRANSPORT_JTAG,
2003  .transport_preferred_id = TRANSPORT_SWD,
2004  .commands = xds110_command_handlers,
2005 
2006  .init = xds110_init,
2007  .quit = xds110_quit,
2008  .reset = xds110_reset,
2009  .speed = xds110_speed,
2010  .khz = xds110_khz,
2011  .speed_div = xds110_speed_div,
2012 
2013  .jtag_ops = &xds110_interface,
2014  .swd_ops = &xds110_swd_driver,
2015 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:309
#define CORUNDETECT
Definition: arm_adi_v5.h:82
swd_special_seq
Definition: arm_adi_v5.h:236
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
static const struct device_t * device
Definition: at91rm9200.c:94
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
#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 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
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
@ 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
@ JTAG_TMS
Definition: commands.h:143
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 address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
uint8_t type
Definition: esp_usb_jtag.c:0
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1070
#define ERROR_JTAG_NOT_IMPLEMENTED
Definition: jtag.h:554
void alive_sleep(uint64_t ms)
Definition: log.c:478
#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
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
const char * name
Definition: command.h:239
Represents a driver for a debugging interface.
Definition: interface.h:183
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:196
uint8_t * buffer
Definition: xds110.c:197
uint32_t num_bits
Definition: xds110.c:198
bool first
Definition: xds110.c:196
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
Definition: psoc6.c:83
uint32_t firmware
Definition: xds110.c:231
bool is_cmapi_connected
Definition: xds110.c:217
bool is_connected
Definition: xds110.c:216
uint32_t speed
Definition: xds110.c:226
unsigned char txn_requests[MAX_DATA_BLOCK]
Definition: xds110.c:234
bool is_ap_dirty
Definition: xds110.c:220
uint32_t txn_result_count
Definition: xds110.c:239
unsigned char read_payload[USB_PAYLOAD_SIZE]
Definition: xds110.c:205
uint32_t txn_result_size
Definition: xds110.c:238
uint32_t select
Definition: xds110.c:222
bool is_cmapi_acquired
Definition: xds110.c:218
uint32_t txn_request_size
Definition: xds110.c:237
unsigned char write_payload[USB_PAYLOAD_SIZE]
Definition: xds110.c:207
struct libusb_device_handle * dev
Definition: xds110.c:204
struct libusb_context * ctx
Definition: xds110.c:203
struct scan_result txn_scan_results[MAX_DATA_BLOCK/4]
Definition: xds110.c:236
bool use_rdbuff
Definition: xds110.c:224
unsigned char write_packet[3]
Definition: xds110.c:206
bool is_swd_mode
Definition: xds110.c:219
uint8_t interface
Definition: xds110.c:212
uint16_t pid
Definition: xds110.c:210
uint16_t vid
Definition: xds110.c:209
uint32_t rdbuff
Definition: xds110.c:223
uint32_t delay_count
Definition: xds110.c:227
uint32_t * txn_dap_results[MAX_DATA_BLOCK/4]
Definition: xds110.c:235
uint16_t hardware
Definition: xds110.c:232
uint8_t endpoint_in
Definition: xds110.c:213
uint32_t voltage
Definition: xds110.c:229
uint8_t endpoint_out
Definition: xds110.c:214
#define SWD_CMD_A32
Definition: swd.h:19
#define SWD_CMD_APNDP
Definition: swd.h:17
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
#define true
Definition: system.h:59
#define TRANSPORT_SWD
Definition: transport.h:20
#define TRANSPORT_JTAG
Definition: transport.h:19
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22
static bool xds_set_srst(uint8_t srst)
Definition: xds110.c:792
#define XDS_JTAG_STATE_UPDATE_IR
Definition: xds110.c:107
#define OCD_FIRMWARE_UPGRADE
Definition: xds110.c:28
#define XDS_OUT_LEN
Definition: xds110.c:142
#define XDS_GOTO_STATE
Definition: xds110.c:152
#define XDS_JTAG_STATE_IDLE
Definition: xds110.c:95
static void xds110_flush(void)
Definition: xds110.c:1458
#define XDS_JTAG_TRANSIT_QUICKEST
Definition: xds110.c:112
static const struct command_registration xds110_subcommand_handlers[]
Definition: xds110.c:1959
#define OCD_DAP_REQUEST
Definition: xds110.c:166
#define DAP_DP_CTRL
Definition: xds110.c:124
COMMAND_HANDLER(xds110_handle_info_command)
Definition: xds110.c:1931
#define XDS110_MAX_SLOW_TCK_SPEED
Definition: xds110.c:78
#define XDS110_MIN_VOLTAGE
Definition: xds110.c:20
static void xds110_set_u16(uint8_t *buffer, uint16_t value)
Definition: xds110.c:273
static int xds110_quit(void)
Definition: xds110.c:1327
static bool cmapi_disconnect(void)
Definition: xds110.c:823
#define XDS_JTAG_STATE_EXIT1_IR
Definition: xds110.c:101
static bool cmapi_connect(uint32_t *idcode)
Definition: xds110.c:804
static bool usb_send_command(uint16_t size)
Definition: xds110.c:583
#define FAST_TCK_DELAY_10000_KHZ
Definition: xds110.c:84
static int xds110_reset(int trst, int srst)
Definition: xds110.c:1551
static bool cmapi_acquire(void)
Definition: xds110.c:831
static void xds110_execute_sleep(struct jtag_command *cmd)
Definition: xds110.c:1595
static bool xds_connect(void)
Definition: xds110.c:658
#define OCD_PATHMOVE
Definition: xds110.c:168
static void xds110_swd_queue_cmd(uint8_t cmd, uint32_t *value)
Definition: xds110.c:1239
static void xds110_queue_runtest(struct jtag_command *cmd)
Definition: xds110.c:1709
#define XDS110_MIN_TCK_SPEED
Definition: xds110.c:77
#define XDS_JTAG_STATE_SELECT_IR
Definition: xds110.c:105
#define CMAPI_REG_READ
Definition: xds110.c:159
static bool ocd_scan_request(uint8_t *scan_requests, uint32_t request_size, uint8_t *scan_results, uint32_t result_size)
Definition: xds110.c:971
#define SWD_CONNECT
Definition: xds110.c:161
#define MAX_DATA_BLOCK
Definition: xds110.c:48
static void xds110_execute_command(struct jtag_command *cmd)
Definition: xds110.c:1744
static bool usb_connect(void)
Definition: xds110.c:302
#define FAST_TCK_DELAY_5500_KHZ
Definition: xds110.c:87
#define XDS110_STAND_ALONE_ID
Definition: xds110.c:24
static int xds110_swd_init(void)
Definition: xds110.c:1020
static bool xds_version(uint32_t *firmware_id, uint16_t *hardware_id)
Definition: xds110.c:674
#define XDS_JTAG_STATE_EXIT1_DR
Definition: xds110.c:100
#define XDS110_DEFAULT_TCK_SPEED
Definition: xds110.c:80
#define SC_ERR_NONE
Definition: xds110.c:68
static bool ocd_pathmove(uint32_t num_states, uint8_t *path)
Definition: xds110.c:996
static const struct command_registration xds110_command_handlers[]
Definition: xds110.c:1977
#define FAST_TCK_DELAY_8500_KHZ
Definition: xds110.c:86
static void xds110_show_info(void)
Definition: xds110.c:1299
#define OCD_FIRMWARE_VERSION
Definition: xds110.c:27
static const uint32_t xds_jtag_state[]
Definition: xds110.c:176
#define XDS_CYCLE_TCK
Definition: xds110.c:151
#define XDS_JTAG_STATE_CAPTURE_IR
Definition: xds110.c:109
#define DAP_DP
Definition: xds110.c:119
static bool xds_execute(uint32_t out_length, uint32_t in_length, uint32_t attempts, uint32_t timeout)
Definition: xds110.c:609
#define XDS_VERSION
Definition: xds110.c:148
static bool swd_disconnect(void)
Definition: xds110.c:904
#define CMAPI_DISCONNECT
Definition: xds110.c:156
#define XDS110_MAX_FAST_TCK_SPEED
Definition: xds110.c:79
static bool swd_connect(void)
Definition: xds110.c:896
static bool xds_jtag_scan(uint32_t shift_state, uint16_t shift_bits, uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
Definition: xds110.c:746
static bool ocd_dap_request(uint8_t *dap_requests, uint32_t request_size, uint32_t *dap_results, uint32_t result_count)
Definition: xds110.c:946
#define XDS_JTAG_STATE_PAUSE_IR
Definition: xds110.c:99
static void xds110_execute_tlr_reset(struct jtag_command *cmd)
Definition: xds110.c:1600
static int xds110_init(void)
Definition: xds110.c:1355
#define XDS_JTAG_STATE_EXIT2_DR
Definition: xds110.c:102
#define CMD_STABLECLOCKS
Definition: xds110.c:173
static bool usb_get_response(uint32_t *total_bytes_read, uint32_t timeout)
Definition: xds110.c:501
#define MAX_RESULT_QUEUE
Definition: xds110.c:54
#define OCD_SCAN_REQUEST
Definition: xds110.c:167
static struct jtag_interface xds110_interface
Definition: xds110.c:1996
#define XDS_SET_TRST
Definition: xds110.c:150
#define DAP_DP_RDBUFF
Definition: xds110.c:128
#define XDS_JTAG_STATE_CAPTURE_DR
Definition: xds110.c:108
static void usb_disconnect(void)
Definition: xds110.c:441
#define DEFAULT_ATTEMPTS
Definition: xds110.c:64
#define XDS_JTAG_STATE_SELECT_DR
Definition: xds110.c:104
#define FAST_TCK_DELAY_12000_KHZ
Definition: xds110.c:85
#define XDS_JTAG_SCAN
Definition: xds110.c:153
static bool xds110_legacy_read_reg(uint8_t cmd, uint32_t *value)
Definition: xds110.c:1075
struct adapter_driver xds110_adapter_driver
Definition: xds110.c:2000
static void xds110_legacy_scan(uint32_t shift_state, uint32_t total_bits, uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
Definition: xds110.c:1440
static void xds110_legacy_stableclocks(uint32_t clocks)
Definition: xds110.c:1453
#define USB_PAYLOAD_SIZE
Definition: xds110.c:52
static int xds110_speed(int speed)
Definition: xds110.c:1790
static int xds110_execute_queue(struct jtag_command *cmd_queue)
Definition: xds110.c:1776
#define XDS_JTAG_STATE_UPDATE_DR
Definition: xds110.c:106
#define XDS_DISCONNECT
Definition: xds110.c:147
static int xds110_speed_div(int speed, int *khz)
Definition: xds110.c:1919
static struct xds110_info xds110
Definition: xds110.c:242
static bool xds_disconnect(void)
Definition: xds110.c:666
static void xds110_execute_pathmove(struct jtag_command *cmd)
Definition: xds110.c:1605
#define CMD_IR_SCAN
Definition: xds110.c:170
#define XDS_SET_SUPPLY
Definition: xds110.c:165
#define XDS_SET_SRST
Definition: xds110.c:154
#define CMAPI_ACQUIRE
Definition: xds110.c:157
static void xds110_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: xds110.c:1286
#define SWD_DISCONNECT
Definition: xds110.c:162
static void xds110_set_u32(uint8_t *buffer, uint32_t value)
Definition: xds110.c:265
static bool xds110_legacy_write_reg(uint8_t cmd, uint32_t value)
Definition: xds110.c:1137
#define XDS_CONNECT
Definition: xds110.c:146
static bool xds_cycle_tck(uint32_t count)
Definition: xds110.c:720
#define DEFAULT_TIMEOUT
Definition: xds110.c:65
static void xds110_queue_stableclocks(struct jtag_command *cmd)
Definition: xds110.c:1728
#define CMD_RUNTEST
Definition: xds110.c:172
#define CJTAG_DISCONNECT
Definition: xds110.c:164
#define XDS_JTAG_STATE_RESET
Definition: xds110.c:94
#define XDS_JTAG_STATE_PAUSE_DR
Definition: xds110.c:98
static int xds110_swd_run_queue(void)
Definition: xds110.c:1184
static uint16_t xds110_get_u16(uint8_t *buffer)
Definition: xds110.c:288
static bool usb_write(unsigned char *buffer, int size, int *written)
Definition: xds110.c:475
static bool cjtag_connect(uint32_t format)
Definition: xds110.c:912
#define XDS_SET_TCK
Definition: xds110.c:149
static uint32_t xds110_get_u32(uint8_t *buffer)
Definition: xds110.c:279
#define MAX_PACKET
Definition: xds110.c:42
static bool usb_read(unsigned char *buffer, int size, int *bytes_read, int timeout)
Definition: xds110.c:457
static void xds110_legacy_runtest(uint32_t clocks, uint32_t end_state)
Definition: xds110.c:1446
#define XDS_JTAG_STATE_SHIFT_IR
Definition: xds110.c:97
static bool xds_set_trst(uint8_t trst)
Definition: xds110.c:708
static void xds110_queue_scan(struct jtag_command *cmd)
Definition: xds110.c:1639
static void xds110_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: xds110.c:1280
static bool cmapi_release(void)
Definition: xds110.c:839
#define DAP_AP
Definition: xds110.c:118
static int xds110_swd_switch_seq(enum swd_special_seq seq)
Definition: xds110.c:1026
static bool cmapi_read_dap_reg(uint32_t type, uint32_t ap_num, uint32_t address, uint32_t *value)
Definition: xds110.c:847
#define CJTAG_CONNECT
Definition: xds110.c:163
static const struct swd_driver xds110_swd_driver
Definition: xds110.c:1988
static bool xds_set_supply(uint32_t voltage)
Definition: xds110.c:932
#define CMAPI_REG_WRITE
Definition: xds110.c:160
#define XDS_IN_LEN
Definition: xds110.c:143
static bool xds_set_tck_delay(uint32_t delay)
Definition: xds110.c:696
#define XDS_JTAG_STATE_SHIFT_DR
Definition: xds110.c:96
#define SC_ERR_XDS110_FAIL
Definition: xds110.c:69
#define FAST_TCK_PLUS_FIRMWARE_VERSION
Definition: xds110.c:35
#define DAP_DP_SELECT
Definition: xds110.c:127
#define MODE_JTAG
Definition: xds110.c:91
static int xds110_khz(int khz, int *jtag_speed)
Definition: xds110.c:1925
#define CMD_DR_SCAN
Definition: xds110.c:171
#define XDS_JTAG_STATE_EXIT2_IR
Definition: xds110.c:103
static bool xds_goto_state(uint32_t state)
Definition: xds110.c:732
#define CMAPI_CONNECT
Definition: xds110.c:155
#define XDS110_MAX_VOLTAGE
Definition: xds110.c:21
static bool cmapi_write_dap_reg(uint32_t type, uint32_t ap_num, uint32_t address, uint32_t *value)
Definition: xds110.c:874
static bool cjtag_disconnect(void)
Definition: xds110.c:924
#define FAST_TCK_FIRMWARE_VERSION
Definition: xds110.c:32
#define CMAPI_RELEASE
Definition: xds110.c:158
#define DAP_AP_DRW
Definition: xds110.c:132