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