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 tap_state_t 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  }
377  }
378  }
379  }
380 
381  /*
382  * We can fall through the for() loop with two possible exit conditions:
383  * 1) found the right XDS110, and that device is open
384  * 2) didn't find the XDS110, and no devices are currently open
385  */
386 
387  if (list) {
388  /* Free the device list, we're done with it */
389  libusb_free_device_list(list, 1);
390  }
391 
392  if (found) {
393  /* Save the vid/pid of the device we're using */
394  xds110.vid = vids[device];
395  xds110.pid = pids[device];
396 
397  /* Save the debug interface and endpoints for the device */
398  xds110.interface = interfaces[device];
399  xds110.endpoint_in = endpoints_in[device] | LIBUSB_ENDPOINT_IN;
400  xds110.endpoint_out = endpoints_out[device] | LIBUSB_ENDPOINT_OUT;
401 
402  /* Save the context and device handles */
403  xds110.ctx = ctx;
404  xds110.dev = dev;
405 
406  /* Set libusb to auto detach kernel */
407  (void)libusb_set_auto_detach_kernel_driver(dev, 1);
408 
409  /* Claim the debug interface on the XDS110 */
410  result = libusb_claim_interface(dev, xds110.interface);
411  } else {
412  /* Couldn't find an XDS110, flag the error */
413  result = -1;
414  }
415 
416  /* On an error, clean up what we can */
417  if (result != 0) {
418  if (dev) {
419  /* Release the debug and data interface on the XDS110 */
420  (void)libusb_release_interface(dev, xds110.interface);
421  libusb_close(dev);
422  }
423  if (ctx)
424  libusb_exit(ctx);
425  xds110.ctx = NULL;
426  xds110.dev = NULL;
427  }
428 
429  /* Log the results */
430  if (result == 0)
431  LOG_INFO("XDS110: connected");
432  else
433  LOG_ERROR("XDS110: failed to connect");
434 
435  return (result == 0) ? true : false;
436 }
437 
438 static void usb_disconnect(void)
439 {
440  if (xds110.dev) {
441  /* Release the debug and data interface on the XDS110 */
442  (void)libusb_release_interface(xds110.dev, xds110.interface);
443  libusb_close(xds110.dev);
444  xds110.dev = NULL;
445  }
446  if (xds110.ctx) {
447  libusb_exit(xds110.ctx);
448  xds110.ctx = NULL;
449  }
450 
451  LOG_INFO("XDS110: disconnected");
452 }
453 
454 static bool usb_read(unsigned char *buffer, int size, int *bytes_read,
455  int timeout)
456 {
457  int result;
458 
459  if (!xds110.dev || !buffer || !bytes_read)
460  return false;
461 
462  /* Force a non-zero timeout to prevent blocking */
463  if (timeout == 0)
465 
466  result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_in, buffer, size,
467  bytes_read, timeout);
468 
469  return (result == 0) ? true : false;
470 }
471 
472 static bool usb_write(unsigned char *buffer, int size, int *written)
473 {
474  int bytes_written = 0;
475  int result = LIBUSB_SUCCESS;
476  int retries = 0;
477 
478  if (!xds110.dev || !buffer)
479  return false;
480 
481  result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_out, buffer,
482  size, &bytes_written, 0);
483 
484  while (result == LIBUSB_ERROR_PIPE && retries < 3) {
485  /* Try clearing the pipe stall and retry transfer */
486  libusb_clear_halt(xds110.dev, xds110.endpoint_out);
487  result = libusb_bulk_transfer(xds110.dev, xds110.endpoint_out, buffer,
488  size, &bytes_written, 0);
489  retries++;
490  }
491 
492  if (written)
493  *written = bytes_written;
494 
495  return (result == 0 && size == bytes_written) ? true : false;
496 }
497 
498 static bool usb_get_response(uint32_t *total_bytes_read, uint32_t timeout)
499 {
500  static unsigned char buffer[MAX_PACKET];
501  int bytes_read;
502  uint16_t size;
503  uint16_t count;
504  bool success;
505 
506  size = 0;
507  success = true;
508  while (success) {
509  success = usb_read(buffer, sizeof(buffer), &bytes_read, timeout);
510  if (success) {
511  /*
512  * Validate that this appears to be a good response packet
513  * First check it contains enough data for header and error
514  * code, plus the first character is the start character
515  */
516  if (bytes_read >= 7 && '*' == buffer[0]) {
517  /* Extract the payload size */
518  size = xds110_get_u16(&buffer[1]);
519  /* Sanity test on payload size */
520  if (USB_PAYLOAD_SIZE >= size && 4 <= size) {
521  /* Check we didn't get more data than expected */
522  if ((bytes_read - 3) <= size) {
523  /* Packet appears to be valid, move on */
524  break;
525  }
526  }
527  }
528  }
529  /*
530  * Somehow received an invalid packet, retry till we
531  * time out or a valid response packet is received
532  */
533  }
534 
535  /* Abort now if we didn't receive a valid response */
536  if (!success) {
537  if (total_bytes_read)
538  *total_bytes_read = 0;
539  return false;
540  }
541 
542  /* Build the return payload into xds110.read_payload */
543 
544  /* Copy over payload data from received buffer (skipping header) */
545  count = 0;
546  bytes_read -= 3;
547  memcpy((void *)&xds110.read_payload[count], (void *)&buffer[3], bytes_read);
548  count += bytes_read;
549  /*
550  * Drop timeout to just 1/2 second. Once the XDS110 starts sending
551  * a response, the remaining packets should arrive in short order
552  */
553  if (timeout > 500)
554  timeout = 500; /* ms */
555 
556  /* If there's more data to retrieve, get it now */
557  while ((count < size) && success) {
558  success = usb_read(buffer, sizeof(buffer), &bytes_read, timeout);
559  if (success) {
560  if ((count + bytes_read) > size) {
561  /* Read too much data, not a valid packet, abort */
562  success = false;
563  } else {
564  /* Copy this data over to xds110.read_payload */
565  memcpy((void *)&xds110.read_payload[count], (void *)buffer,
566  bytes_read);
567  count += bytes_read;
568  }
569  }
570  }
571 
572  if (!success)
573  count = 0;
574  if (total_bytes_read)
575  *total_bytes_read = count;
576 
577  return success;
578 }
579 
580 static bool usb_send_command(uint16_t size)
581 {
582  int written;
583  bool success = true;
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  success = usb_write(xds110.write_packet, (int)size, &written);
600 
601  /* Check if the correct number of bytes was written */
602  if (written != (int)size)
603  success = false;
604 
605  return success;
606 }
607 
608 /***************************************************************************
609  * XDS110 firmware API routines *
610  * *
611  * The following functions handle calling into the XDS110 firmware to *
612  * perform requested debug actions. *
613  ***************************************************************************/
614 
615 static bool xds_execute(uint32_t out_length, uint32_t in_length,
616  uint32_t attempts, uint32_t timeout)
617 {
618  bool done = false;
619  bool success = true;
620  int error = 0;
621  uint32_t bytes_read = 0;
622 
623  if (!xds110.dev)
624  return false;
625 
626  while (!done && attempts > 0) {
627  attempts--;
628 
629  /* Send command to XDS110 */
630  success = usb_send_command(out_length);
631 
632  if (success) {
633  /* Get response from XDS110 */
634  success = usb_get_response(&bytes_read, timeout);
635  }
636 
637  if (success) {
638  /* Check for valid response from XDS code handling */
639  if (bytes_read != in_length) {
640  /* Unexpected amount of data returned */
641  success = false;
642  LOG_DEBUG("XDS110: command 0x%02x return %" PRIu32 " bytes, expected %" PRIu32,
643  xds110.write_payload[0], bytes_read, in_length);
644  } else {
645  /* Extract error code from return packet */
646  error = (int)xds110_get_u32(&xds110.read_payload[0]);
647  done = true;
648  if (error != SC_ERR_NONE)
649  LOG_DEBUG("XDS110: command 0x%02x returned error %d",
650  xds110.write_payload[0], error);
651  }
652  }
653  }
654 
655  if (!success)
656  error = SC_ERR_XDS110_FAIL;
657 
658  if (error != 0)
659  success = false;
660 
661  return success;
662 }
663 
664 static bool xds_connect(void)
665 {
666  bool success;
667 
669 
672 
673  return success;
674 }
675 
676 static bool xds_disconnect(void)
677 {
678  bool success;
679 
681 
684 
685  return success;
686 }
687 
688 static bool xds_version(uint32_t *firmware_id, uint16_t *hardware_id)
689 {
690  uint8_t *fw_id_pntr = &xds110.read_payload[XDS_IN_LEN + 0]; /* 32-bits */
691  uint8_t *hw_id_pntr = &xds110.read_payload[XDS_IN_LEN + 4]; /* 16-bits */
692 
693  bool success;
694 
696 
699 
700  if (success) {
701  if (firmware_id)
702  *firmware_id = xds110_get_u32(fw_id_pntr);
703  if (hardware_id)
704  *hardware_id = xds110_get_u16(hw_id_pntr);
705  }
706 
707  return success;
708 }
709 
710 static bool xds_set_tck_delay(uint32_t delay)
711 {
712  uint8_t *delay_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
713 
714  bool success;
715 
717 
718  xds110_set_u32(delay_pntr, delay);
719 
722 
723  return success;
724 }
725 
726 static bool xds_set_trst(uint8_t trst)
727 {
728  uint8_t *trst_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
729 
730  bool success;
731 
733 
734  *trst_pntr = trst;
735 
738 
739  return success;
740 }
741 
742 static bool xds_cycle_tck(uint32_t count)
743 {
744  uint8_t *count_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
745 
746  bool success;
747 
749 
750  xds110_set_u32(count_pntr, count);
751 
754 
755  return success;
756 }
757 
758 static bool xds_goto_state(uint32_t state)
759 {
760  uint8_t *state_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
761  uint8_t *transit_pntr = &xds110.write_payload[XDS_OUT_LEN+4]; /* 32-bits */
762 
763  bool success;
764 
766 
767  xds110_set_u32(state_pntr, state);
769 
772 
773  return success;
774 }
775 
776 static bool xds_jtag_scan(uint32_t shift_state, uint16_t shift_bits,
777  uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
778 {
779  uint8_t *bits_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 16-bits */
780  uint8_t *path_pntr = &xds110.write_payload[XDS_OUT_LEN + 2]; /* 8-bits */
781  uint8_t *trans1_pntr = &xds110.write_payload[XDS_OUT_LEN + 3]; /* 8-bits */
782  uint8_t *end_pntr = &xds110.write_payload[XDS_OUT_LEN + 4]; /* 8-bits */
783  uint8_t *trans2_pntr = &xds110.write_payload[XDS_OUT_LEN + 5]; /* 8-bits */
784  uint8_t *pre_pntr = &xds110.write_payload[XDS_OUT_LEN + 6]; /* 16-bits */
785  uint8_t *pos_pntr = &xds110.write_payload[XDS_OUT_LEN + 8]; /* 16-bits */
786  uint8_t *delay_pntr = &xds110.write_payload[XDS_OUT_LEN + 10]; /* 16-bits */
787  uint8_t *rep_pntr = &xds110.write_payload[XDS_OUT_LEN + 12]; /* 16-bits */
788  uint8_t *out_pntr = &xds110.write_payload[XDS_OUT_LEN + 14]; /* 16-bits */
789  uint8_t *in_pntr = &xds110.write_payload[XDS_OUT_LEN + 16]; /* 16-bits */
790  uint8_t *data_out_pntr = &xds110.write_payload[XDS_OUT_LEN + 18];
791  uint8_t *data_in_pntr = &xds110.read_payload[XDS_IN_LEN+0];
792 
793  uint16_t total_bytes = DIV_ROUND_UP(shift_bits, 8);
794 
795  bool success;
796 
798 
799  xds110_set_u16(bits_pntr, shift_bits); /* bits to scan */
800  *path_pntr = (uint8_t)(shift_state & 0xff); /* IR vs DR path */
801  *trans1_pntr = (uint8_t)XDS_JTAG_TRANSIT_QUICKEST; /* start state route */
802  *end_pntr = (uint8_t)(end_state & 0xff); /* JTAG state after scan */
803  *trans2_pntr = (uint8_t)XDS_JTAG_TRANSIT_QUICKEST; /* end state route */
804  xds110_set_u16(pre_pntr, 0); /* number of preamble bits */
805  xds110_set_u16(pos_pntr, 0); /* number of postamble bits */
806  xds110_set_u16(delay_pntr, 0); /* number of extra TCKs after scan */
807  xds110_set_u16(rep_pntr, 1); /* number of repetitions */
808  xds110_set_u16(out_pntr, total_bytes); /* out buffer offset (if repeats) */
809  xds110_set_u16(in_pntr, total_bytes); /* in buffer offset (if repeats) */
810 
811  memcpy((void *)data_out_pntr, (void *)data_out, total_bytes);
812 
813  success = xds_execute(XDS_OUT_LEN + 18 + total_bytes,
815 
816  if (success)
817  memcpy((void *)data_in, (void *)data_in_pntr, total_bytes);
818 
819  return success;
820 }
821 
822 static bool xds_set_srst(uint8_t srst)
823 {
824  uint8_t *srst_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
825 
826  bool success;
827 
829 
830  *srst_pntr = srst;
831 
834 
835  return success;
836 }
837 
838 static bool cmapi_connect(uint32_t *idcode)
839 {
840  uint8_t *idcode_pntr = &xds110.read_payload[XDS_IN_LEN + 0]; /* 32-bits */
841 
842  bool success;
843 
845 
848 
849  if (success) {
850  if (idcode)
851  *idcode = xds110_get_u32(idcode_pntr);
852  }
853 
854  return success;
855 }
856 
857 static bool cmapi_disconnect(void)
858 {
859  bool success;
860 
862 
865 
866  return success;
867 }
868 
869 static bool cmapi_acquire(void)
870 {
871  bool success;
872 
874 
877 
878  return success;
879 }
880 
881 static bool cmapi_release(void)
882 {
883  bool success;
884 
886 
889 
890  return success;
891 }
892 
893 static bool cmapi_read_dap_reg(uint32_t type, uint32_t ap_num,
894  uint32_t address, uint32_t *value)
895 {
896  uint8_t *type_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
897  uint8_t *ap_num_pntr = &xds110.write_payload[XDS_OUT_LEN + 1]; /* 8-bits */
898  uint8_t *address_pntr = &xds110.write_payload[XDS_OUT_LEN + 2]; /* 8-bits */
899  uint8_t *value_pntr = &xds110.read_payload[XDS_IN_LEN + 0]; /* 32-bits */
900 
901  bool success;
902 
904 
905  *type_pntr = (uint8_t)(type & 0xff);
906  *ap_num_pntr = (uint8_t)(ap_num & 0xff);
907  *address_pntr = (uint8_t)(address & 0xff);
908 
911 
912  if (success) {
913  if (value)
914  *value = xds110_get_u32(value_pntr);
915  }
916 
917  return success;
918 }
919 
920 static bool cmapi_write_dap_reg(uint32_t type, uint32_t ap_num,
921  uint32_t address, uint32_t *value)
922 {
923  uint8_t *type_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 8-bits */
924  uint8_t *ap_num_pntr = &xds110.write_payload[XDS_OUT_LEN + 1]; /* 8-bits */
925  uint8_t *address_pntr = &xds110.write_payload[XDS_OUT_LEN + 2]; /* 8-bits */
926  uint8_t *value_pntr = &xds110.write_payload[XDS_OUT_LEN + 3]; /* 32-bits */
927 
928  bool success;
929 
930  if (!value)
931  return false;
932 
934 
935  *type_pntr = (uint8_t)(type & 0xff);
936  *ap_num_pntr = (uint8_t)(ap_num & 0xff);
937  *address_pntr = (uint8_t)(address & 0xff);
938  xds110_set_u32(value_pntr, *value);
939 
942 
943  return success;
944 }
945 
946 static bool swd_connect(void)
947 {
948  bool success;
949 
951 
954 
955  return success;
956 }
957 
958 static bool swd_disconnect(void)
959 {
960  bool success;
961 
963 
966 
967  return success;
968 }
969 
970 static bool cjtag_connect(uint32_t format)
971 {
972  uint8_t *format_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
973 
974  bool success;
975 
977 
978  xds110_set_u32(format_pntr, format);
979 
982 
983  return success;
984 }
985 
986 static bool cjtag_disconnect(void)
987 {
988  bool success;
989 
991 
994 
995  return success;
996 }
997 
998 static bool xds_set_supply(uint32_t voltage)
999 {
1000  uint8_t *volts_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
1001  uint8_t *source_pntr = &xds110.write_payload[XDS_OUT_LEN + 4]; /* 8-bits */
1002 
1003  bool success;
1004 
1006 
1007  xds110_set_u32(volts_pntr, voltage);
1008  *source_pntr = (uint8_t)(voltage != 0 ? 1 : 0);
1009 
1011  DEFAULT_TIMEOUT);
1012 
1013  return success;
1014 }
1015 
1016 static bool ocd_dap_request(uint8_t *dap_requests, uint32_t request_size,
1017  uint32_t *dap_results, uint32_t result_count)
1018 {
1019  uint8_t *request_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
1020  uint8_t *result_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
1021 
1022  bool success;
1023 
1024  if (!dap_requests || !dap_results)
1025  return false;
1026 
1028 
1029  memcpy((void *)request_pntr, (void *)dap_requests, request_size);
1030 
1031  success = xds_execute(XDS_OUT_LEN + request_size,
1032  XDS_IN_LEN + (result_count * 4), DEFAULT_ATTEMPTS,
1033  DEFAULT_TIMEOUT);
1034 
1035  if (success && (result_count > 0))
1036  memcpy((void *)dap_results, (void *)result_pntr, result_count * 4);
1037 
1038  return success;
1039 }
1040 
1041 static bool ocd_scan_request(uint8_t *scan_requests, uint32_t request_size,
1042  uint8_t *scan_results, uint32_t result_size)
1043 {
1044  uint8_t *request_pntr = &xds110.write_payload[XDS_OUT_LEN + 0];
1045  uint8_t *result_pntr = &xds110.read_payload[XDS_IN_LEN + 0];
1046 
1047  bool success;
1048 
1049  if (!scan_requests || !scan_results)
1050  return false;
1051 
1053 
1054  memcpy((void *)request_pntr, (void *)scan_requests, request_size);
1055 
1056  success = xds_execute(XDS_OUT_LEN + request_size,
1057  XDS_IN_LEN + result_size, DEFAULT_ATTEMPTS,
1058  DEFAULT_TIMEOUT);
1059 
1060  if (success && (result_size > 0))
1061  memcpy((void *)scan_results, (void *)result_pntr, result_size);
1062 
1063  return success;
1064 }
1065 
1066 static bool ocd_pathmove(uint32_t num_states, uint8_t *path)
1067 {
1068  uint8_t *num_pntr = &xds110.write_payload[XDS_OUT_LEN + 0]; /* 32-bits */
1069  uint8_t *path_pntr = &xds110.write_payload[XDS_OUT_LEN + 4];
1070 
1071  bool success;
1072 
1073  if (!path)
1074  return false;
1075 
1077 
1078  xds110_set_u32(num_pntr, num_states);
1079 
1080  memcpy((void *)path_pntr, (void *)path, num_states);
1081 
1082  success = xds_execute(XDS_OUT_LEN + 4 + num_states, XDS_IN_LEN,
1084 
1085  return success;
1086 }
1087 
1088 /***************************************************************************
1089  * swd driver interface *
1090  * *
1091  * The following functions provide SWD support to OpenOCD. *
1092  ***************************************************************************/
1093 
1094 static int xds110_swd_init(void)
1095 {
1096  xds110.is_swd_mode = true;
1097  return ERROR_OK;
1098 }
1099 
1101 {
1102  uint32_t idcode;
1103  bool success;
1104 
1105  switch (seq) {
1106  case LINE_RESET:
1107  LOG_ERROR("Sequence SWD line reset (%d) not supported", seq);
1108  return ERROR_FAIL;
1109  case JTAG_TO_SWD:
1110  LOG_DEBUG("JTAG-to-SWD");
1111  xds110.is_swd_mode = false;
1112  xds110.is_cmapi_connected = false;
1113  xds110.is_cmapi_acquired = false;
1114  /* Run sequence to put target in SWD mode */
1115  success = swd_connect();
1116  /* Re-initialize CMAPI API for DAP access */
1117  if (success) {
1118  xds110.is_swd_mode = true;
1119  success = cmapi_connect(&idcode);
1120  if (success) {
1121  xds110.is_cmapi_connected = true;
1122  success = cmapi_acquire();
1123  }
1124  }
1125  break;
1126  case SWD_TO_JTAG:
1127  LOG_DEBUG("SWD-to-JTAG");
1128  xds110.is_swd_mode = false;
1129  xds110.is_cmapi_connected = false;
1130  xds110.is_cmapi_acquired = false;
1131  /* Run sequence to put target in JTAG mode */
1132  success = swd_disconnect();
1133  if (success) {
1134  /* Re-initialize JTAG interface */
1135  success = cjtag_connect(MODE_JTAG);
1136  }
1137  break;
1138  default:
1139  LOG_ERROR("Sequence %d not supported", seq);
1140  return ERROR_FAIL;
1141  }
1142 
1143  if (success)
1144  return ERROR_OK;
1145  else
1146  return ERROR_FAIL;
1147 }
1148 
1149 static bool xds110_legacy_read_reg(uint8_t cmd, uint32_t *value)
1150 {
1151  /* Make sure this is a read request */
1152  bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
1153  /* Determine whether this is a DP or AP register access */
1154  uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
1155  /* Determine the AP number from cached SELECT value */
1156  uint32_t ap_num = (xds110.select & 0xff000000) >> 24;
1157  /* Extract register address from command */
1158  uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
1159  /* Extract bank address from cached SELECT value */
1160  uint32_t bank = (xds110.select & 0x000000f0);
1161 
1162  uint32_t reg_value = 0;
1163  uint32_t temp_value = 0;
1164 
1165  bool success;
1166 
1167  if (!is_read_request)
1168  return false;
1169 
1170  if (type == DAP_AP) {
1171  /* Add bank address to register address for CMAPI call */
1172  address |= bank;
1173  }
1174 
1175  if (DAP_DP == type && DAP_DP_RDBUFF == address && xds110.use_rdbuff) {
1176  /* If RDBUFF is cached and this is a DP RDBUFF read, use the cache */
1177  reg_value = xds110.rdbuff;
1178  success = true;
1179  } else if (DAP_AP == type && DAP_AP_DRW == address && xds110.use_rdbuff) {
1180  /* If RDBUFF is cached and this is an AP DRW read, use the cache, */
1181  /* but still call into the firmware to get the next read. */
1182  reg_value = xds110.rdbuff;
1183  success = cmapi_read_dap_reg(type, ap_num, address, &temp_value);
1184  } else {
1185  success = cmapi_read_dap_reg(type, ap_num, address, &temp_value);
1186  if (success)
1187  reg_value = temp_value;
1188  }
1189 
1190  /* Mark that we have consumed or invalidated the RDBUFF cache */
1191  xds110.use_rdbuff = false;
1192 
1193  /* Handle result of read attempt */
1194  if (!success)
1195  LOG_ERROR("XDS110: failed to read DAP register");
1196  else if (value)
1197  *value = reg_value;
1198 
1199  if (success && DAP_AP == type) {
1200  /*
1201  * On a successful DAP AP read, we actually have the value from RDBUFF,
1202  * the firmware will have run the AP request and made the RDBUFF read
1203  */
1204  xds110.use_rdbuff = true;
1205  xds110.rdbuff = temp_value;
1206  }
1207 
1208  return success;
1209 }
1210 
1211 static bool xds110_legacy_write_reg(uint8_t cmd, uint32_t value)
1212 {
1213  /* Make sure this isn't a read request */
1214  bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
1215  /* Determine whether this is a DP or AP register access */
1216  uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
1217  /* Determine the AP number from cached SELECT value */
1218  uint32_t ap_num = (xds110.select & 0xff000000) >> 24;
1219  /* Extract register address from command */
1220  uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
1221  /* Extract bank address from cached SELECT value */
1222  uint32_t bank = (xds110.select & 0x000000f0);
1223 
1224  bool success;
1225 
1226  if (is_read_request)
1227  return false;
1228 
1229  /* Invalidate the RDBUFF cache */
1230  xds110.use_rdbuff = false;
1231 
1232  if (type == DAP_AP) {
1233  /* Add bank address to register address for CMAPI call */
1234  address |= bank;
1235  /* Any write to an AP register invalidates the firmware's cache */
1236  xds110.is_ap_dirty = true;
1237  } else if (address == DAP_DP_SELECT) {
1238  /* Any write to the SELECT register invalidates the firmware's cache */
1239  xds110.is_ap_dirty = true;
1240  }
1241 
1242  success = cmapi_write_dap_reg(type, ap_num, address, &value);
1243 
1244  if (!success) {
1245  LOG_ERROR("XDS110: failed to write DAP register");
1246  } else {
1247  /*
1248  * If the debugger wrote to SELECT, cache the value
1249  * to use to build the apNum and address values above
1250  */
1251  if ((type == DAP_DP) && (address == DAP_DP_SELECT))
1252  xds110.select = value;
1253  }
1254 
1255  return success;
1256 }
1257 
1258 static int xds110_swd_run_queue(void)
1259 {
1260  static uint32_t dap_results[MAX_RESULT_QUEUE];
1261  uint8_t cmd;
1262  uint32_t request;
1263  uint32_t result;
1264  uint32_t value;
1265  bool success = true;
1266 
1267  if (xds110.txn_request_size == 0)
1268  return ERROR_OK;
1269 
1270  /* Terminate request queue */
1272 
1274  /* XDS110 firmware has the API to directly handle the queue */
1277  } else {
1278  /* Legacy firmware needs to handle queue via discrete DAP calls */
1279  request = 0;
1280  result = 0;
1281  while (xds110.txn_requests[request] != 0) {
1282  cmd = xds110.txn_requests[request++];
1283  if (0 == (SWD_CMD_RNW & cmd)) {
1284  /* DAP register write command */
1285  value = (uint32_t)(xds110.txn_requests[request++]) << 0;
1286  value |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1287  value |= (uint32_t)(xds110.txn_requests[request++]) << 16;
1288  value |= (uint32_t)(xds110.txn_requests[request++]) << 24;
1289  if (success)
1290  success = xds110_legacy_write_reg(cmd, value);
1291  } else {
1292  /* DAP register read command */
1293  value = 0;
1294  if (success)
1295  success = xds110_legacy_read_reg(cmd, &value);
1296  dap_results[result++] = value;
1297  }
1298  }
1299  }
1300 
1301  /* Transfer results into caller's buffers */
1302  for (result = 0; result < xds110.txn_result_count; result++)
1303  if (xds110.txn_dap_results[result] != 0)
1304  *xds110.txn_dap_results[result] = dap_results[result];
1305 
1307  xds110.txn_result_size = 0;
1309 
1310  return (success) ? ERROR_OK : ERROR_FAIL;
1311 }
1312 
1313 static void xds110_swd_queue_cmd(uint8_t cmd, uint32_t *value)
1314 {
1315  /* Check if this is a read or write request */
1316  bool is_read_request = (0 != (SWD_CMD_RNW & cmd));
1317  /* Determine whether this is a DP or AP register access */
1318  uint32_t type = (0 != (SWD_CMD_APNDP & cmd)) ? DAP_AP : DAP_DP;
1319  /* Extract register address from command */
1320  uint32_t address = ((cmd & SWD_CMD_A32) >> 1);
1321  uint32_t request_size = (is_read_request) ? 1 : 5;
1322 
1323  /* Check if new request would be too large to fit */
1324  if (((xds110.txn_request_size + request_size + 1) > MAX_DATA_BLOCK) ||
1327 
1328  /* Set the START bit in cmd to ensure cmd is not zero */
1329  /* (a value of zero is used to terminate the buffer) */
1330  cmd |= SWD_CMD_START;
1331 
1332  /* Add request to queue; queue is built marshalled for XDS110 call */
1333  if (is_read_request) {
1334  /* Queue read request, save pointer to pass back result */
1337  xds110.txn_result_size += 4;
1338  } else {
1339  /* Check for and prevent sticky overrun detection */
1340  if (DAP_DP == type && DAP_DP_CTRL == address &&
1341  (*value & CORUNDETECT)) {
1342  LOG_DEBUG("XDS110: refusing to enable sticky overrun detection");
1343  *value &= ~CORUNDETECT;
1344  }
1345  /* Queue write request, add value directly to queue buffer */
1347  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 0) & 0xff;
1348  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 8) & 0xff;
1349  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 16) & 0xff;
1350  xds110.txn_requests[xds110.txn_request_size++] = (*value >> 24) & 0xff;
1351  }
1352 }
1353 
1354 static void xds110_swd_read_reg(uint8_t cmd, uint32_t *value,
1355  uint32_t ap_delay_clk)
1356 {
1357  assert(cmd & SWD_CMD_RNW);
1358  xds110_swd_queue_cmd(cmd, value);
1359 }
1360 static void xds110_swd_write_reg(uint8_t cmd, uint32_t value,
1361  uint32_t ap_delay_clk)
1362 {
1363  assert(!(cmd & SWD_CMD_RNW));
1364  xds110_swd_queue_cmd(cmd, &value);
1365 }
1366 
1367 /***************************************************************************
1368  * jtag interface *
1369  * *
1370  * The following functions provide XDS110 interface to OpenOCD. *
1371  ***************************************************************************/
1372 
1373 static void xds110_show_info(void)
1374 {
1375  uint32_t firmware = xds110.firmware;
1376 
1377  LOG_INFO("XDS110: vid/pid = %04x/%04x", xds110.vid, xds110.pid);
1378  LOG_INFO("XDS110: firmware version = %" PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32,
1379  (((firmware >> 28) & 0xf) * 10) + ((firmware >> 24) & 0xf),
1380  (((firmware >> 20) & 0xf) * 10) + ((firmware >> 16) & 0xf),
1381  (((firmware >> 12) & 0xf) * 10) + ((firmware >> 8) & 0xf),
1382  (((firmware >> 4) & 0xf) * 10) + ((firmware >> 0) & 0xf));
1383  LOG_INFO("XDS110: hardware version = 0x%04x", xds110.hardware);
1385  LOG_INFO("XDS110: serial number = %s", adapter_get_required_serial());
1386  if (xds110.is_swd_mode) {
1387  LOG_INFO("XDS110: connected to target via SWD");
1388  LOG_INFO("XDS110: SWCLK set to %" PRIu32 " kHz", xds110.speed);
1389  } else {
1390  LOG_INFO("XDS110: connected to target via JTAG");
1391  LOG_INFO("XDS110: TCK set to %" PRIu32 " kHz", xds110.speed);
1392  }
1393 
1394  /* Alert user that there's a better firmware to use */
1395  if (firmware < OCD_FIRMWARE_VERSION) {
1396  LOG_WARNING("XDS110: the firmware is not optimized for OpenOCD");
1398  }
1399 }
1400 
1401 static int xds110_quit(void)
1402 {
1403  if (xds110.is_cmapi_acquired) {
1404  (void)cmapi_release();
1405  xds110.is_cmapi_acquired = false;
1406  }
1407  if (xds110.is_cmapi_connected) {
1408  (void)cmapi_disconnect();
1409  xds110.is_cmapi_connected = false;
1410  }
1411  if (xds110.is_connected) {
1412  if (xds110.is_swd_mode) {
1413  /* Switch out of SWD mode */
1414  (void)swd_disconnect();
1415  } else {
1416  /* Switch out of cJTAG mode */
1417  (void)cjtag_disconnect();
1418  }
1419  /* Tell firmware we're disconnecting */
1420  (void)xds_disconnect();
1421  xds110.is_connected = false;
1422  }
1423  /* Close down the USB connection to the XDS110 debug probe */
1424  usb_disconnect();
1425 
1426  return ERROR_OK;
1427 }
1428 
1429 static int xds110_init(void)
1430 {
1431  bool success;
1432 
1433  /* Establish USB connection to the XDS110 debug probe */
1434  success = usb_connect();
1435 
1436  if (success) {
1437  /* Send connect message to XDS110 firmware */
1438  success = xds_connect();
1439  if (success)
1440  xds110.is_connected = true;
1441  }
1442 
1443  if (success) {
1444  uint32_t firmware;
1445  uint16_t hardware;
1446 
1447  /* Retrieve version IDs from firmware */
1448  /* Version numbers are stored in BCD format */
1449  success = xds_version(&firmware, &hardware);
1450  if (success) {
1451  /* Save the firmware and hardware version */
1452  xds110.firmware = firmware;
1453  xds110.hardware = hardware;
1454  }
1455  }
1456 
1457  if (success) {
1458  /* Set supply voltage for stand-alone probes */
1460  success = xds_set_supply(xds110.voltage);
1461  /* Allow time for target device to power up */
1462  /* (CC32xx takes up to 1300 ms before debug is enabled) */
1463  alive_sleep(1500);
1464  } else if (xds110.voltage != 0) {
1465  /* Voltage supply not a feature of embedded probes */
1466  LOG_WARNING(
1467  "XDS110: ignoring supply voltage, not supported on this probe");
1468  }
1469  }
1470 
1471  if (success) {
1472  success = xds_set_trst(0);
1473  if (success)
1474  success = xds_cycle_tck(50);
1475  if (success)
1476  success = xds_set_trst(1);
1477  if (success)
1478  success = xds_cycle_tck(50);
1479  }
1480 
1481  if (success) {
1482  if (xds110.is_swd_mode) {
1483  /* Switch to SWD if needed */
1484  success = swd_connect();
1485  } else {
1486  success = cjtag_connect(MODE_JTAG);
1487  }
1488  }
1489 
1490  if (success && xds110.is_swd_mode) {
1491  uint32_t idcode;
1492 
1493  /* Connect to CMAPI interface in XDS110 */
1494  success = cmapi_connect(&idcode);
1495 
1496  /* Acquire exclusive access to CMAPI interface */
1497  if (success) {
1498  xds110.is_cmapi_connected = true;
1499  success = cmapi_acquire();
1500  if (success)
1501  xds110.is_cmapi_acquired = true;
1502  }
1503  }
1504 
1505  if (!success)
1506  xds110_quit();
1507 
1508  if (success)
1509  xds110_show_info();
1510 
1511  return (success) ? ERROR_OK : ERROR_FAIL;
1512 }
1513 
1514 static void xds110_legacy_scan(uint32_t shift_state, uint32_t total_bits,
1515  uint32_t end_state, uint8_t *data_out, uint8_t *data_in)
1516 {
1517  (void)xds_jtag_scan(shift_state, total_bits, end_state, data_out, data_in);
1518 }
1519 
1520 static void xds110_legacy_runtest(uint32_t clocks, uint32_t end_state)
1521 {
1523  xds_cycle_tck(clocks);
1524  xds_goto_state(end_state);
1525 }
1526 
1527 static void xds110_legacy_stableclocks(uint32_t clocks)
1528 {
1529  xds_cycle_tck(clocks);
1530 }
1531 
1532 static void xds110_flush(void)
1533 {
1534  uint8_t command;
1535  uint32_t clocks;
1536  uint32_t shift_state;
1537  uint32_t end_state;
1538  uint32_t bits;
1539  uint32_t bytes;
1540  uint32_t request;
1541  uint32_t result;
1542  uint8_t *data_out;
1543  uint8_t data_in[MAX_DATA_BLOCK];
1544  uint8_t *data_pntr;
1545 
1546  if (xds110.txn_request_size == 0)
1547  return;
1548 
1549  /* Terminate request queue */
1551 
1553  /* Updated firmware has the API to directly handle the queue */
1555  data_in, xds110.txn_result_size);
1556  } else {
1557  /* Legacy firmware needs to handle queue via discrete JTAG calls */
1558  request = 0;
1559  result = 0;
1560  while (xds110.txn_requests[request] != 0) {
1561  command = xds110.txn_requests[request++];
1562  switch (command) {
1563  case CMD_IR_SCAN:
1564  case CMD_DR_SCAN:
1565  if (command == CMD_IR_SCAN)
1566  shift_state = XDS_JTAG_STATE_SHIFT_IR;
1567  else
1568  shift_state = XDS_JTAG_STATE_SHIFT_DR;
1569  end_state = (uint32_t)(xds110.txn_requests[request++]);
1570  bits = (uint32_t)(xds110.txn_requests[request++]) << 0;
1571  bits |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1572  data_out = &xds110.txn_requests[request];
1573  bytes = DIV_ROUND_UP(bits, 8);
1574  xds110_legacy_scan(shift_state, bits, end_state, data_out,
1575  &data_in[result]);
1576  result += bytes;
1577  request += bytes;
1578  break;
1579  case CMD_RUNTEST:
1580  clocks = (uint32_t)(xds110.txn_requests[request++]) << 0;
1581  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1582  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 16;
1583  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 24;
1584  end_state = (uint32_t)xds110.txn_requests[request++];
1585  xds110_legacy_runtest(clocks, end_state);
1586  break;
1587  case CMD_STABLECLOCKS:
1588  clocks = (uint32_t)(xds110.txn_requests[request++]) << 0;
1589  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 8;
1590  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 16;
1591  clocks |= (uint32_t)(xds110.txn_requests[request++]) << 24;
1593  break;
1594  default:
1595  LOG_ERROR("BUG: unknown JTAG command type 0x%x encountered",
1596  command);
1597  exit(-1);
1598  break;
1599  }
1600  }
1601  }
1602 
1603  /* Transfer results into caller's buffers from data_in buffer */
1604  bits = 0; /* Bit offset into current scan result */
1605  data_pntr = data_in;
1606  for (result = 0; result < xds110.txn_result_count; result++) {
1607  if (xds110.txn_scan_results[result].first) {
1608  if (bits != 0) {
1609  bytes = DIV_ROUND_UP(bits, 8);
1610  data_pntr += bytes;
1611  }
1612  bits = 0;
1613  }
1614  if (xds110.txn_scan_results[result].buffer != 0)
1615  bit_copy(xds110.txn_scan_results[result].buffer, 0, data_pntr,
1617  bits += xds110.txn_scan_results[result].num_bits;
1618  }
1619 
1621  xds110.txn_result_size = 0;
1623 }
1624 
1625 static int xds110_reset(int trst, int srst)
1626 {
1627  uint8_t value;
1628  bool success;
1629  int retval = ERROR_OK;
1630 
1631  if (trst != -1) {
1632  if (trst == 0) {
1633  /* Deassert nTRST (active low) */
1634  value = 1;
1635  } else {
1636  /* Assert nTRST (active low) */
1637  value = 0;
1638  }
1639  success = xds_set_trst(value);
1640  if (!success)
1641  retval = ERROR_FAIL;
1642  }
1643 
1644  if (srst != -1) {
1645  if (srst == 0) {
1646  /* Deassert nSRST (active low) */
1647  value = 1;
1648  } else {
1649  /* Assert nSRST (active low) */
1650  value = 0;
1651  }
1652  success = xds_set_srst(value);
1653  if (!success)
1654  retval = ERROR_FAIL;
1655 
1656  /* Toggle TCK to trigger HIB on CC13x/CC26x devices */
1657  if (success && !xds110.is_swd_mode) {
1658  /* Toggle TCK for about 50 ms */
1659  success = xds_cycle_tck(xds110.speed * 50);
1660  }
1661 
1662  if (!success)
1663  retval = ERROR_FAIL;
1664  }
1665 
1666  return retval;
1667 }
1668 
1670 {
1671  jtag_sleep(cmd->cmd.sleep->us);
1672 }
1673 
1675 {
1677 }
1678 
1680 {
1681  uint32_t i;
1682  uint32_t num_states;
1683  uint8_t *path;
1684 
1685  num_states = (uint32_t)cmd->cmd.pathmove->num_states;
1686 
1687  if (num_states == 0)
1688  return;
1689 
1690  path = (uint8_t *)malloc(num_states * sizeof(uint8_t));
1691  if (path == 0) {
1692  LOG_ERROR("XDS110: unable to allocate memory");
1693  return;
1694  }
1695 
1696  /* Convert requested path states into XDS API states */
1697  for (i = 0; i < num_states; i++)
1698  path[i] = (uint8_t)xds_jtag_state[cmd->cmd.pathmove->path[i]];
1699 
1701  /* Updated firmware fully supports pathmove */
1702  (void)ocd_pathmove(num_states, path);
1703  } else {
1704  /* Notify user that legacy firmware simply cannot handle pathmove */
1705  LOG_ERROR("XDS110: the firmware does not support pathmove command");
1707  /* If pathmove is required, then debug is not possible */
1708  exit(-1);
1709  }
1710 
1711  free((void *)path);
1712 }
1713 
1714 static void xds110_queue_scan(struct jtag_command *cmd)
1715 {
1716  int i;
1717  uint32_t offset;
1718  uint32_t total_fields;
1719  uint32_t total_bits;
1720  uint32_t total_bytes;
1721  uint8_t end_state;
1722  uint8_t *buffer;
1723 
1724  /* Calculate the total number of bits to scan */
1725  total_bits = 0;
1726  total_fields = 0;
1727  for (i = 0; i < cmd->cmd.scan->num_fields; i++) {
1728  total_fields++;
1729  total_bits += (uint32_t)cmd->cmd.scan->fields[i].num_bits;
1730  }
1731 
1732  if (total_bits == 0)
1733  return;
1734 
1735  total_bytes = DIV_ROUND_UP(total_bits, 8);
1736 
1737  /* Check if new request would be too large to fit */
1738  if (((xds110.txn_request_size + 1 + total_bytes + sizeof(end_state) + 1)
1739  > MAX_DATA_BLOCK) || ((xds110.txn_result_count + total_fields) >
1741  xds110_flush();
1742 
1743  /* Check if this single request is too large to fit */
1744  if ((1 + total_bytes + sizeof(end_state) + 1) > MAX_DATA_BLOCK) {
1745  LOG_ERROR("BUG: JTAG scan request is too large to handle (%" PRIu32 " bits)",
1746  total_bits);
1747  /* Failing to run this scan mucks up debug on this target */
1748  exit(-1);
1749  }
1750 
1751  if (cmd->cmd.scan->ir_scan)
1753  else
1755 
1756  end_state = (uint8_t)xds_jtag_state[cmd->cmd.scan->end_state];
1757  xds110.txn_requests[xds110.txn_request_size++] = end_state;
1758 
1759  xds110.txn_requests[xds110.txn_request_size++] = (total_bits >> 0) & 0xff;
1760  xds110.txn_requests[xds110.txn_request_size++] = (total_bits >> 8) & 0xff;
1761 
1762  /* Build request data by flattening fields into single buffer */
1763  /* also populate the results array to return the results when run */
1764  offset = 0;
1766  /* Clear data out buffer to default value of all zeros */
1767  memset((void *)buffer, 0x00, total_bytes);
1768  for (i = 0; i < cmd->cmd.scan->num_fields; i++) {
1769  if (cmd->cmd.scan->fields[i].out_value != 0) {
1770  /* Copy over data to scan out into request buffer */
1771  bit_copy(buffer, offset, cmd->cmd.scan->fields[i].out_value, 0,
1772  cmd->cmd.scan->fields[i].num_bits);
1773  }
1774  offset += cmd->cmd.scan->fields[i].num_bits;
1777  cmd->cmd.scan->fields[i].num_bits;
1779  cmd->cmd.scan->fields[i].in_value;
1780  }
1781  xds110.txn_request_size += total_bytes;
1782  xds110.txn_result_size += total_bytes;
1783 }
1784 
1786 {
1787  uint32_t clocks = (uint32_t)cmd->cmd.stableclocks->num_cycles;
1788  uint8_t end_state = (uint8_t)xds_jtag_state[cmd->cmd.runtest->end_state];
1789 
1790  /* Check if new request would be too large to fit */
1791  if ((xds110.txn_request_size + 1 + sizeof(clocks) + sizeof(end_state) + 1)
1792  > MAX_DATA_BLOCK)
1793  xds110_flush();
1794 
1795  /* Queue request and cycle count directly to queue buffer */
1797  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 0) & 0xff;
1798  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 8) & 0xff;
1799  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 16) & 0xff;
1800  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 24) & 0xff;
1801  xds110.txn_requests[xds110.txn_request_size++] = end_state;
1802 }
1803 
1805 {
1806  uint32_t clocks = (uint32_t)cmd->cmd.stableclocks->num_cycles;
1807 
1808  /* Check if new request would be too large to fit */
1809  if ((xds110.txn_request_size + 1 + sizeof(clocks) + 1) > MAX_DATA_BLOCK)
1810  xds110_flush();
1811 
1812  /* Queue request and cycle count directly to queue buffer */
1814  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 0) & 0xff;
1815  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 8) & 0xff;
1816  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 16) & 0xff;
1817  xds110.txn_requests[xds110.txn_request_size++] = (clocks >> 24) & 0xff;
1818 }
1819 
1821 {
1822  switch (cmd->type) {
1823  case JTAG_SLEEP:
1824  xds110_flush();
1826  break;
1827  case JTAG_TLR_RESET:
1828  xds110_flush();
1830  break;
1831  case JTAG_PATHMOVE:
1832  xds110_flush();
1834  break;
1835  case JTAG_SCAN:
1837  break;
1838  case JTAG_RUNTEST:
1840  break;
1841  case JTAG_STABLECLOCKS:
1843  break;
1844  case JTAG_TMS:
1845  default:
1846  LOG_ERROR("BUG: unknown JTAG command type 0x%x encountered",
1847  cmd->type);
1848  exit(-1);
1849  }
1850 }
1851 
1852 static int xds110_execute_queue(void)
1853 {
1855 
1856  while (cmd) {
1858  cmd = cmd->next;
1859  }
1860 
1861  xds110_flush();
1862 
1863  return ERROR_OK;
1864 }
1865 
1866 static int xds110_speed(int speed)
1867 {
1868  double freq_to_use;
1869  uint32_t delay_count;
1870  bool success;
1871 
1872  if (speed == 0) {
1873  LOG_INFO("XDS110: RTCK not supported");
1875  }
1876 
1877  if (speed < XDS110_MIN_TCK_SPEED) {
1878  LOG_INFO("XDS110: increase speed request: %d kHz to %d kHz minimum",
1879  speed, XDS110_MIN_TCK_SPEED);
1880  speed = XDS110_MIN_TCK_SPEED;
1881  }
1882 
1883  /* Older XDS110 firmware had inefficient scan routines and could only */
1884  /* achieve a peak TCK frequency of about 2500 kHz */
1886 
1887  /* Check for request for top speed or higher */
1888  if (speed >= XDS110_MAX_SLOW_TCK_SPEED) {
1889 
1890  /* Inform user that speed was adjusted down to max possible */
1891  if (speed > XDS110_MAX_SLOW_TCK_SPEED) {
1892  LOG_INFO(
1893  "XDS110: reduce speed request: %d kHz to %d kHz maximum",
1894  speed, XDS110_MAX_SLOW_TCK_SPEED);
1895  speed = XDS110_MAX_SLOW_TCK_SPEED;
1896  }
1897  delay_count = 0;
1898 
1899  } else {
1900 
1901  const double XDS110_TCK_PULSE_INCREMENT = 66.0;
1902  freq_to_use = speed * 1000; /* Hz */
1903  delay_count = 0;
1904 
1905  /* Calculate the delay count value */
1906  double one_giga = 1000000000;
1907  /* Get the pulse duration for the max frequency supported in ns */
1908  double max_freq_pulse_duration = one_giga /
1909  (XDS110_MAX_SLOW_TCK_SPEED * 1000);
1910 
1911  /* Convert frequency to pulse duration */
1912  double freq_to_pulse_width_in_ns = one_giga / freq_to_use;
1913 
1914  /*
1915  * Start with the pulse duration for the maximum frequency. Keep
1916  * decrementing time added by each count value till the requested
1917  * frequency pulse is less than the calculated value.
1918  */
1919  double current_value = max_freq_pulse_duration;
1920 
1921  while (current_value < freq_to_pulse_width_in_ns) {
1922  current_value += XDS110_TCK_PULSE_INCREMENT;
1923  ++delay_count;
1924  }
1925 
1926  /*
1927  * Determine which delay count yields the best match.
1928  * The one obtained above or one less.
1929  */
1930  if (delay_count) {
1931  double diff_freq_1 = freq_to_use -
1932  (one_giga / (max_freq_pulse_duration +
1933  (XDS110_TCK_PULSE_INCREMENT * delay_count)));
1934  double diff_freq_2 = (one_giga / (max_freq_pulse_duration +
1935  (XDS110_TCK_PULSE_INCREMENT * (delay_count - 1)))) -
1936  freq_to_use;
1937 
1938  /* One less count value yields a better match */
1939  if (diff_freq_1 > diff_freq_2)
1940  --delay_count;
1941  }
1942  }
1943 
1944  /* Newer firmware has reworked TCK routines that are much more efficient */
1945  /* and can now achieve a peak TCK frequency of 14000 kHz */
1946  } else {
1947 
1948  if (speed >= XDS110_MAX_FAST_TCK_SPEED) {
1949  if (speed > XDS110_MAX_FAST_TCK_SPEED) {
1950  LOG_INFO(
1951  "XDS110: reduce speed request: %d kHz to %d kHz maximum",
1952  speed, XDS110_MAX_FAST_TCK_SPEED);
1953  speed = XDS110_MAX_FAST_TCK_SPEED;
1954  }
1955  delay_count = 0;
1956  } else if (speed >= 12000 && xds110.firmware >=
1958  delay_count = FAST_TCK_DELAY_12000_KHZ;
1959  } else if (speed >= 10000 && xds110.firmware >=
1961  delay_count = FAST_TCK_DELAY_10000_KHZ;
1962  } else if (speed >= 8500) {
1963  delay_count = FAST_TCK_DELAY_8500_KHZ;
1964  } else if (speed >= 5500) {
1965  delay_count = FAST_TCK_DELAY_5500_KHZ;
1966  } else {
1967  /* Calculate the delay count to set the frequency */
1968  /* Formula determined by measuring the waveform on Saeleae logic */
1969  /* analyzer using known values for delay count */
1970  const double m = 17100000.0; /* slope */
1971  const double b = -1.02; /* y-intercept */
1972 
1973  freq_to_use = speed * 1000; /* Hz */
1974  double period = 1.0/freq_to_use;
1975  double delay = m * period + b;
1976 
1977  if (delay < 1.0)
1978  delay_count = 1;
1979  else
1980  delay_count = (uint32_t)delay;
1981  }
1982  }
1983 
1984  /* Send the delay count to the XDS110 firmware */
1985  success = xds_set_tck_delay(delay_count);
1986 
1987  if (success) {
1988  xds110.delay_count = delay_count;
1989  xds110.speed = speed;
1990  }
1991 
1992  return (success) ? ERROR_OK : ERROR_FAIL;
1993 }
1994 
1995 static int xds110_speed_div(int speed, int *khz)
1996 {
1997  *khz = speed;
1998  return ERROR_OK;
1999 }
2000 
2001 static int xds110_khz(int khz, int *jtag_speed)
2002 {
2003  *jtag_speed = khz;
2004  return ERROR_OK;
2005 }
2006 
2007 COMMAND_HANDLER(xds110_handle_info_command)
2008 {
2009  xds110_show_info();
2010  return ERROR_OK;
2011 }
2012 
2013 COMMAND_HANDLER(xds110_handle_supply_voltage_command)
2014 {
2015  uint32_t voltage = 0;
2016 
2017  if (CMD_ARGC == 1) {
2018  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], voltage);
2019  if (voltage == 0 || (voltage >= XDS110_MIN_VOLTAGE && voltage
2020  <= XDS110_MAX_VOLTAGE)) {
2021  /* Requested voltage is in range */
2022  xds110.voltage = voltage;
2023  } else {
2024  LOG_ERROR("XDS110: voltage must be 0 or between %d and %d "
2025  "millivolts", XDS110_MIN_VOLTAGE, XDS110_MAX_VOLTAGE);
2026  return ERROR_FAIL;
2027  }
2028  xds110.voltage = voltage;
2029  } else
2031 
2032  return ERROR_OK;
2033 }
2034 
2035 static const struct command_registration xds110_subcommand_handlers[] = {
2036  {
2037  .name = "info",
2038  .handler = &xds110_handle_info_command,
2039  .mode = COMMAND_EXEC,
2040  .help = "show XDS110 info",
2041  .usage = "",
2042  },
2043  {
2044  .name = "supply",
2045  .handler = &xds110_handle_supply_voltage_command,
2046  .mode = COMMAND_CONFIG,
2047  .help = "set the XDS110 probe supply voltage",
2048  .usage = "voltage_in_millivolts",
2049  },
2051 };
2052 
2053 static const struct command_registration xds110_command_handlers[] = {
2054  {
2055  .name = "xds110",
2056  .mode = COMMAND_ANY,
2057  .help = "perform XDS110 management",
2058  .usage = "",
2059  .chain = xds110_subcommand_handlers,
2060  },
2062 };
2063 
2064 static const struct swd_driver xds110_swd_driver = {
2065  .init = xds110_swd_init,
2066  .switch_seq = xds110_swd_switch_seq,
2067  .read_reg = xds110_swd_read_reg,
2068  .write_reg = xds110_swd_write_reg,
2069  .run = xds110_swd_run_queue,
2070 };
2071 
2072 static const char * const xds110_transport[] = { "swd", "jtag", NULL };
2073 
2074 static struct jtag_interface xds110_interface = {
2076 };
2077 
2079  .name = "xds110",
2080  .transports = xds110_transport,
2081  .commands = xds110_command_handlers,
2082 
2083  .init = xds110_init,
2084  .quit = xds110_quit,
2085  .reset = xds110_reset,
2086  .speed = xds110_speed,
2087  .khz = xds110_khz,
2088  .speed_div = xds110_speed_div,
2089 
2090  .jtag_ops = &xds110_interface,
2091  .swd_ops = &xds110_swd_driver,
2092 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:299
#define CORUNDETECT
Definition: arm_adi_v5.h:82
swd_special_seq
Definition: arm_adi_v5.h:229
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:231
@ LINE_RESET
Definition: arm_adi_v5.h:230
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:233
uint32_t bits
Definition: armv4_5.c:359
static const struct device_t * device
Definition: at91rm9200.c:94
static void bit_copy(uint8_t *dst, unsigned dst_offset, const uint8_t *src, unsigned src_offset, unsigned bit_count)
Definition: binarybuffer.h:201
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#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:425
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
struct jtag_command * jtag_command_queue
The current queue of jtag_command_s structures.
Definition: commands.c:36
@ 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
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:1062
#define ERROR_JTAG_NOT_IMPLEMENTED
Definition: jtag.h:551
void alive_sleep(uint64_t ms)
Definition: log.c:460
#define LOG_WARNING(expr ...)
Definition: log.h:120
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_INFO(expr ...)
Definition: log.h:117
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
Represents a driver for a debugging interface.
Definition: interface.h:207
const char *const name
The name of the interface driver.
Definition: interface.h:209
const char * name
Definition: command.h:229
Represents a driver for a debugging interface.
Definition: interface.h:184
int(* execute_queue)(void)
Execute queued commands.
Definition: interface.h:195
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:84
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:66
#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:822
#define XDS_JTAG_STATE_UPDATE_IR
Definition: xds110.c:107
static int xds110_execute_queue(void)
Definition: xds110.c:1852
#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:1532
#define XDS_JTAG_TRANSIT_QUICKEST
Definition: xds110.c:112
static const struct command_registration xds110_subcommand_handlers[]
Definition: xds110.c:2035
#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:2007
#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:1401
static bool cmapi_disconnect(void)
Definition: xds110.c:857
#define XDS_JTAG_STATE_EXIT1_IR
Definition: xds110.c:101
static bool cmapi_connect(uint32_t *idcode)
Definition: xds110.c:838
static bool usb_send_command(uint16_t size)
Definition: xds110.c:580
#define FAST_TCK_DELAY_10000_KHZ
Definition: xds110.c:84
static int xds110_reset(int trst, int srst)
Definition: xds110.c:1625
static bool cmapi_acquire(void)
Definition: xds110.c:869
static void xds110_execute_sleep(struct jtag_command *cmd)
Definition: xds110.c:1669
static bool xds_connect(void)
Definition: xds110.c:664
#define OCD_PATHMOVE
Definition: xds110.c:168
static void xds110_swd_queue_cmd(uint8_t cmd, uint32_t *value)
Definition: xds110.c:1313
static void xds110_queue_runtest(struct jtag_command *cmd)
Definition: xds110.c:1785
#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:1041
#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:1820
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:1094
static bool xds_version(uint32_t *firmware_id, uint16_t *hardware_id)
Definition: xds110.c:688
#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:1066
static const struct command_registration xds110_command_handlers[]
Definition: xds110.c:2053
#define FAST_TCK_DELAY_8500_KHZ
Definition: xds110.c:86
static void xds110_show_info(void)
Definition: xds110.c:1373
#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:615
#define XDS_VERSION
Definition: xds110.c:148
static bool swd_disconnect(void)
Definition: xds110.c:958
#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:946
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:776
static bool ocd_dap_request(uint8_t *dap_requests, uint32_t request_size, uint32_t *dap_results, uint32_t result_count)
Definition: xds110.c:1016
#define XDS_JTAG_STATE_PAUSE_IR
Definition: xds110.c:99
static void xds110_execute_tlr_reset(struct jtag_command *cmd)
Definition: xds110.c:1674
static int xds110_init(void)
Definition: xds110.c:1429
#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:498
#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:2074
#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:438
#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:1149
struct adapter_driver xds110_adapter_driver
Definition: xds110.c:2078
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:1514
static void xds110_legacy_stableclocks(uint32_t clocks)
Definition: xds110.c:1527
#define USB_PAYLOAD_SIZE
Definition: xds110.c:52
static int xds110_speed(int speed)
Definition: xds110.c:1866
#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:1995
static struct xds110_info xds110
Definition: xds110.c:242
static bool xds_disconnect(void)
Definition: xds110.c:676
static void xds110_execute_pathmove(struct jtag_command *cmd)
Definition: xds110.c:1679
#define CMD_IR_SCAN
Definition: xds110.c:170
#define XDS_SET_SUPPLY
Definition: xds110.c:165
static const char *const xds110_transport[]
Definition: xds110.c:2072
#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:1360
#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:1211
#define XDS_CONNECT
Definition: xds110.c:146
static bool xds_cycle_tck(uint32_t count)
Definition: xds110.c:742
#define DEFAULT_TIMEOUT
Definition: xds110.c:65
static void xds110_queue_stableclocks(struct jtag_command *cmd)
Definition: xds110.c:1804
#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:1258
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:472
static bool cjtag_connect(uint32_t format)
Definition: xds110.c:970
#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:454
static void xds110_legacy_runtest(uint32_t clocks, uint32_t end_state)
Definition: xds110.c:1520
#define XDS_JTAG_STATE_SHIFT_IR
Definition: xds110.c:97
static bool xds_set_trst(uint8_t trst)
Definition: xds110.c:726
static void xds110_queue_scan(struct jtag_command *cmd)
Definition: xds110.c:1714
static void xds110_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: xds110.c:1354
static bool cmapi_release(void)
Definition: xds110.c:881
#define DAP_AP
Definition: xds110.c:118
static int xds110_swd_switch_seq(enum swd_special_seq seq)
Definition: xds110.c:1100
static bool cmapi_read_dap_reg(uint32_t type, uint32_t ap_num, uint32_t address, uint32_t *value)
Definition: xds110.c:893
#define CJTAG_CONNECT
Definition: xds110.c:163
static const struct swd_driver xds110_swd_driver
Definition: xds110.c:2064
static bool xds_set_supply(uint32_t voltage)
Definition: xds110.c:998
#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:710
#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:2001
#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:758
#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:920
static bool cjtag_disconnect(void)
Definition: xds110.c:986
#define FAST_TCK_FIRMWARE_VERSION
Definition: xds110.c:32
#define CMAPI_RELEASE
Definition: xds110.c:158
#define DAP_AP_DRW
Definition: xds110.c:132