drivers/cmsis-dap: add multidrop capability
[fw/openocd] / src / jtag / drivers / cmsis_dap.c
1 /***************************************************************************
2  *   Copyright (C) 2018 by MickaĆ«l Thomas                                  *
3  *   mickael9@gmail.com                                                    *
4  *                                                                         *
5  *   Copyright (C) 2016 by Maksym Hilliaka                                 *
6  *   oter@frozen-team.com                                                  *
7  *                                                                         *
8  *   Copyright (C) 2016 by Phillip Pearson                                 *
9  *   pp@myelin.co.nz                                                       *
10  *                                                                         *
11  *   Copyright (C) 2014 by Paul Fertser                                    *
12  *   fercerpav@gmail.com                                                   *
13  *                                                                         *
14  *   Copyright (C) 2013 by mike brown                                      *
15  *   mike@theshedworks.org.uk                                              *
16  *                                                                         *
17  *   Copyright (C) 2013 by Spencer Oliver                                  *
18  *   spen@spen-soft.co.uk                                                  *
19  *                                                                         *
20  *   This program is free software; you can redistribute it and/or modify  *
21  *   it under the terms of the GNU General Public License as published by  *
22  *   the Free Software Foundation; either version 2 of the License, or     *
23  *   (at your option) any later version.                                   *
24  *                                                                         *
25  *   This program is distributed in the hope that it will be useful,       *
26  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
27  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
28  *   GNU General Public License for more details.                          *
29  *                                                                         *
30  *   You should have received a copy of the GNU General Public License     *
31  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
32  ***************************************************************************/
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <transport/transport.h>
39 #include <jtag/swd.h>
40 #include <jtag/interface.h>
41 #include <jtag/commands.h>
42 #include <jtag/tcl.h>
43
44 #include "cmsis_dap.h"
45
46 static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
47 #if BUILD_CMSIS_DAP_USB == 1
48         &cmsis_dap_usb_backend,
49 #endif
50
51 #if BUILD_CMSIS_DAP_HID == 1
52         &cmsis_dap_hid_backend,
53 #endif
54 };
55
56 /* USB Config */
57
58 /* Known vid/pid pairs:
59  * VID 0xc251: Keil Software
60  * PID 0xf001: LPC-Link-II CMSIS_DAP
61  * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
62  * PID 0x2722: Keil ULINK2 CMSIS-DAP
63  * PID 0x2750: Keil ULINKplus CMSIS-DAP
64  *
65  * VID 0x0d28: mbed Software
66  * PID 0x0204: MBED CMSIS-DAP
67  */
68
69 #define MAX_USB_IDS 8
70 /* vid = pid = 0 marks the end of the list */
71 static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
72 static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
73 static char *cmsis_dap_serial;
74 static int cmsis_dap_backend = -1;
75 static bool swd_mode;
76
77 #define USB_TIMEOUT       1000
78
79 /* CMSIS-DAP General Commands */
80 #define CMD_DAP_INFO              0x00
81 #define CMD_DAP_LED               0x01
82 #define CMD_DAP_CONNECT           0x02
83 #define CMD_DAP_DISCONNECT        0x03
84 #define CMD_DAP_WRITE_ABORT       0x08
85 #define CMD_DAP_DELAY             0x09
86 #define CMD_DAP_RESET_TARGET      0x0A
87
88 /* CMD_INFO */
89 #define INFO_ID_VENDOR            0x01      /* string */
90 #define INFO_ID_PRODUCT           0x02      /* string */
91 #define INFO_ID_SERNUM            0x03      /* string */
92 #define INFO_ID_FW_VER            0x04      /* string */
93 #define INFO_ID_TD_VEND           0x05      /* string */
94 #define INFO_ID_TD_NAME           0x06      /* string */
95 #define INFO_ID_CAPS              0xf0      /* byte */
96 #define INFO_ID_PKT_CNT           0xfe      /* byte */
97 #define INFO_ID_PKT_SZ            0xff      /* short */
98
99 #define INFO_CAPS_SWD             0x01
100 #define INFO_CAPS_JTAG            0x02
101
102 /* CMD_LED */
103 #define LED_ID_CONNECT            0x00
104 #define LED_ID_RUN                0x01
105
106 #define LED_OFF                   0x00
107 #define LED_ON                    0x01
108
109 /* CMD_CONNECT */
110 #define CONNECT_DEFAULT           0x00
111 #define CONNECT_SWD               0x01
112 #define CONNECT_JTAG              0x02
113
114 /* CMSIS-DAP Common SWD/JTAG Commands */
115 #define CMD_DAP_DELAY             0x09
116 #define CMD_DAP_SWJ_PINS          0x10
117 #define CMD_DAP_SWJ_CLOCK         0x11
118 #define CMD_DAP_SWJ_SEQ           0x12
119
120 /*
121  * PINS
122  * Bit 0: SWCLK/TCK
123  * Bit 1: SWDIO/TMS
124  * Bit 2: TDI
125  * Bit 3: TDO
126  * Bit 5: nTRST
127  * Bit 7: nRESET
128  */
129
130 #define SWJ_PIN_TCK               (1<<0)
131 #define SWJ_PIN_TMS               (1<<1)
132 #define SWJ_PIN_TDI               (1<<2)
133 #define SWJ_PIN_TDO               (1<<3)
134 #define SWJ_PIN_TRST              (1<<5)
135 #define SWJ_PIN_SRST              (1<<7)
136
137 /* CMSIS-DAP SWD Commands */
138 #define CMD_DAP_SWD_CONFIGURE     0x13
139 #define CMD_DAP_SWD_SEQUENCE      0x1D
140
141 /* CMSIS-DAP JTAG Commands */
142 #define CMD_DAP_JTAG_SEQ          0x14
143 #define CMD_DAP_JTAG_CONFIGURE    0x15
144 #define CMD_DAP_JTAG_IDCODE       0x16
145
146 /* CMSIS-DAP JTAG sequence info masks */
147 /* Number of bits to clock through (0 means 64) */
148 #define DAP_JTAG_SEQ_TCK          0x3F
149 /* TMS will be set during the sequence if this bit is set */
150 #define DAP_JTAG_SEQ_TMS          0x40
151 /* TDO output will be captured if this bit is set */
152 #define DAP_JTAG_SEQ_TDO          0x80
153
154
155 /* CMSIS-DAP Transfer Commands */
156 #define CMD_DAP_TFER_CONFIGURE    0x04
157 #define CMD_DAP_TFER              0x05
158 #define CMD_DAP_TFER_BLOCK        0x06
159 #define CMD_DAP_TFER_ABORT        0x07
160
161 /* DAP Status Code */
162 #define DAP_OK                    0
163 #define DAP_ERROR                 0xFF
164
165 /* CMSIS-DAP Vendor Commands
166  * None as yet... */
167
168 static const char * const info_caps_str[] = {
169         "SWD  Supported",
170         "JTAG Supported"
171 };
172
173 struct pending_transfer_result {
174         uint8_t cmd;
175         uint32_t data;
176         void *buffer;
177 };
178
179 struct pending_request_block {
180         struct pending_transfer_result *transfers;
181         int transfer_count;
182 };
183
184 struct pending_scan_result {
185         /** Offset in bytes in the CMD_DAP_JTAG_SEQ response buffer. */
186         unsigned first;
187         /** Number of bits to read. */
188         unsigned length;
189         /** Location to store the result */
190         uint8_t *buffer;
191         /** Offset in the destination buffer */
192         unsigned buffer_offset;
193 };
194
195 /* Up to MIN(packet_count, MAX_PENDING_REQUESTS) requests may be issued
196  * until the first response arrives */
197 #define MAX_PENDING_REQUESTS 3
198
199 /* Pending requests are organized as a FIFO - circular buffer */
200 /* Each block in FIFO can contain up to pending_queue_len transfers */
201 static int pending_queue_len;
202 static struct pending_request_block pending_fifo[MAX_PENDING_REQUESTS];
203 static int pending_fifo_put_idx, pending_fifo_get_idx;
204 static int pending_fifo_block_count;
205
206 /* pointers to buffers that will receive jtag scan results on the next flush */
207 #define MAX_PENDING_SCAN_RESULTS 256
208 static int pending_scan_result_count;
209 static struct pending_scan_result pending_scan_results[MAX_PENDING_SCAN_RESULTS];
210
211 /* queued JTAG sequences that will be executed on the next flush */
212 #define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_size - 3)
213 static int queued_seq_count;
214 static int queued_seq_buf_end;
215 static int queued_seq_tdo_ptr;
216 static uint8_t queued_seq_buf[1024]; /* TODO: make dynamic / move into cmsis object */
217
218 static int queued_retval;
219
220 static uint8_t output_pins = SWJ_PIN_SRST | SWJ_PIN_TRST;
221
222 static struct cmsis_dap *cmsis_dap_handle;
223
224
225 static int cmsis_dap_quit(void);
226
227 static int cmsis_dap_open(void)
228 {
229         const struct cmsis_dap_backend *backend = NULL;
230
231         struct cmsis_dap *dap = calloc(1, sizeof(struct cmsis_dap));
232         if (dap == NULL) {
233                 LOG_ERROR("unable to allocate memory");
234                 return ERROR_FAIL;
235         }
236
237         if (cmsis_dap_backend >= 0) {
238                 /* Use forced backend */
239                 backend = cmsis_dap_backends[cmsis_dap_backend];
240                 if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, cmsis_dap_serial) != ERROR_OK)
241                         backend = NULL;
242         } else {
243                 /* Try all backends */
244                 for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
245                         backend = cmsis_dap_backends[i];
246                         if (backend->open(dap, cmsis_dap_vid, cmsis_dap_pid, cmsis_dap_serial) == ERROR_OK)
247                                 break;
248                         else
249                                 backend = NULL;
250                 }
251         }
252
253         if (backend == NULL) {
254                 LOG_ERROR("unable to find a matching CMSIS-DAP device");
255                 free(dap);
256                 return ERROR_FAIL;
257         }
258
259         dap->backend = backend;
260
261         cmsis_dap_handle = dap;
262
263         return ERROR_OK;
264 }
265
266 static void cmsis_dap_close(struct cmsis_dap *dap)
267 {
268         if (dap->backend) {
269                 dap->backend->close(dap);
270                 dap->backend = NULL;
271         }
272
273         free(cmsis_dap_handle->packet_buffer);
274         free(cmsis_dap_handle);
275         cmsis_dap_handle = NULL;
276         free(cmsis_dap_serial);
277         cmsis_dap_serial = NULL;
278
279         for (int i = 0; i < MAX_PENDING_REQUESTS; i++) {
280                 free(pending_fifo[i].transfers);
281                 pending_fifo[i].transfers = NULL;
282         }
283 }
284
285 static void cmsis_dap_flush_read(struct cmsis_dap *dap)
286 {
287         unsigned int i;
288         /* Some CMSIS-DAP adapters keep buffered packets over
289          * USB close/open so we need to flush up to 64 old packets
290          * to be sure all buffers are empty */
291         for (i = 0; i < 64; i++) {
292                 int retval = dap->backend->read(dap, 10);
293                 if (retval == ERROR_TIMEOUT_REACHED)
294                         break;
295         }
296         if (i)
297                 LOG_DEBUG("Flushed %u packets", i);
298 }
299
300 /* Send a message and receive the reply */
301 static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
302 {
303         if (pending_fifo_block_count) {
304                 LOG_ERROR("pending %d blocks, flushing", pending_fifo_block_count);
305                 while (pending_fifo_block_count) {
306                         dap->backend->read(dap, 10);
307                         pending_fifo_block_count--;
308                 }
309                 pending_fifo_put_idx = 0;
310                 pending_fifo_get_idx = 0;
311         }
312
313         uint8_t current_cmd = cmsis_dap_handle->command[0];
314         int retval = dap->backend->write(dap, txlen, USB_TIMEOUT);
315         if (retval < 0)
316                 return retval;
317
318         /* get reply */
319         retval = dap->backend->read(dap, USB_TIMEOUT);
320         if (retval < 0)
321                 return retval;
322
323         uint8_t *resp = cmsis_dap_handle->response;
324         if (resp[0] == DAP_ERROR) {
325                 LOG_ERROR("CMSIS-DAP command 0x%" PRIx8 " not implemented", current_cmd);
326                 return ERROR_NOT_IMPLEMENTED;
327         }
328
329         if (resp[0] != current_cmd) {
330                 LOG_ERROR("CMSIS-DAP command mismatch. Sent 0x%" PRIx8
331                          " received 0x%" PRIx8, current_cmd, resp[0]);
332
333                 cmsis_dap_flush_read(dap);
334                 return ERROR_FAIL;
335         }
336
337         return ERROR_OK;
338 }
339
340 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
341 {
342         int retval;
343         uint8_t *command = cmsis_dap_handle->command;
344
345         command[0] = CMD_DAP_SWJ_PINS;
346         command[1] = pins;
347         command[2] = mask;
348         h_u32_to_le(&command[3], delay);
349
350         retval = cmsis_dap_xfer(cmsis_dap_handle, 7);
351
352         if (retval != ERROR_OK) {
353                 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
354                 return ERROR_JTAG_DEVICE_ERROR;
355         }
356
357         if (input)
358                 *input = cmsis_dap_handle->response[1];
359
360         return ERROR_OK;
361 }
362
363 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
364 {
365         int retval;
366         uint8_t *command = cmsis_dap_handle->command;
367
368         /* set clock in Hz */
369         swj_clock *= 1000;
370
371         command[0] = CMD_DAP_SWJ_CLOCK;
372         h_u32_to_le(&command[1], swj_clock);
373
374         retval = cmsis_dap_xfer(cmsis_dap_handle, 5);
375
376         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
377                 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
378                 return ERROR_JTAG_DEVICE_ERROR;
379         }
380
381         return ERROR_OK;
382 }
383
384 /* clock a sequence of bits out on TMS, to change JTAG states */
385 static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence)
386 {
387         int retval;
388         uint8_t *command = cmsis_dap_handle->command;
389
390 #ifdef CMSIS_DAP_JTAG_DEBUG
391         LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
392         for (int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
393                 printf("%02X ", sequence[i]);
394
395         printf("\n");
396 #endif
397
398         command[0] = CMD_DAP_SWJ_SEQ;
399         command[1] = s_len;
400         bit_copy(&command[2], 0, sequence, 0, s_len);
401
402         retval = cmsis_dap_xfer(cmsis_dap_handle, 2 + DIV_ROUND_UP(s_len, 8));
403         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK)
404                 return ERROR_FAIL;
405
406         return ERROR_OK;
407 }
408
409 static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
410 {
411         int retval;
412         uint8_t *command = cmsis_dap_handle->command;
413
414         command[0] = CMD_DAP_INFO;
415         command[1] = info;
416
417         retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
418
419         if (retval != ERROR_OK) {
420                 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
421                 return ERROR_JTAG_DEVICE_ERROR;
422         }
423
424         *data = &cmsis_dap_handle->response[1];
425
426         return ERROR_OK;
427 }
428
429 static int cmsis_dap_cmd_DAP_LED(uint8_t led, uint8_t state)
430 {
431         int retval;
432         uint8_t *command = cmsis_dap_handle->command;
433
434         command[0] = CMD_DAP_LED;
435         command[1] = led;
436         command[2] = state;
437
438         retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
439
440         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
441                 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
442                 return ERROR_JTAG_DEVICE_ERROR;
443         }
444
445         return ERROR_OK;
446 }
447
448 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
449 {
450         int retval;
451         uint8_t *command = cmsis_dap_handle->command;
452
453         command[0] = CMD_DAP_CONNECT;
454         command[1] = mode;
455
456         retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
457
458         if (retval != ERROR_OK) {
459                 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
460                 return ERROR_JTAG_DEVICE_ERROR;
461         }
462
463         if (cmsis_dap_handle->response[1] != mode) {
464                 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
465                 return ERROR_JTAG_DEVICE_ERROR;
466         }
467
468         return ERROR_OK;
469 }
470
471 static int cmsis_dap_cmd_DAP_Disconnect(void)
472 {
473         int retval;
474         uint8_t *command = cmsis_dap_handle->command;
475
476         command[0] = CMD_DAP_DISCONNECT;
477
478         retval = cmsis_dap_xfer(cmsis_dap_handle, 1);
479
480         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
481                 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
482                 return ERROR_JTAG_DEVICE_ERROR;
483         }
484
485         return ERROR_OK;
486 }
487
488 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
489 {
490         int retval;
491         uint8_t *command = cmsis_dap_handle->command;
492
493         command[0] = CMD_DAP_TFER_CONFIGURE;
494         command[1] = idle;
495         h_u16_to_le(&command[2], retry_count);
496         h_u16_to_le(&command[4], match_retry);
497
498         retval = cmsis_dap_xfer(cmsis_dap_handle, 6);
499
500         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
501                 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
502                 return ERROR_JTAG_DEVICE_ERROR;
503         }
504
505         return ERROR_OK;
506 }
507
508 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
509 {
510         int retval;
511         uint8_t *command = cmsis_dap_handle->command;
512
513         command[0] = CMD_DAP_SWD_CONFIGURE;
514         command[1] = cfg;
515
516         retval = cmsis_dap_xfer(cmsis_dap_handle, 2);
517
518         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
519                 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
520                 return ERROR_JTAG_DEVICE_ERROR;
521         }
522
523         return ERROR_OK;
524 }
525
526 #if 0
527 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
528 {
529         int retval;
530         uint8_t *command = cmsis_dap_handle->command;
531
532         command[0] = CMD_DAP_DELAY;
533         h_u16_to_le(&command[1], delay_us);
534
535         retval = cmsis_dap_xfer(cmsis_dap_handle, 3);
536
537         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
538                 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
539                 return ERROR_JTAG_DEVICE_ERROR;
540         }
541
542         return ERROR_OK;
543 }
544 #endif
545
546 static int cmsis_dap_metacmd_targetsel(uint32_t instance_id)
547 {
548         int retval;
549         uint8_t *command = cmsis_dap_handle->command;
550         const uint32_t SEQ_RD = 0x80, SEQ_WR = 0x00;
551
552         /* SWD multi-drop requires a transfer ala CMD_DAP_TFER,
553         but with no expectation of an SWD ACK response.  In
554         CMSIS-DAP v1.20 and v2.00, CMD_DAP_SWD_SEQUENCE was
555         added to allow this special sequence to be generated.
556         The purpose of this operation is to select the target
557         corresponding to the instance_id that is written */
558
559         size_t idx = 0;
560         command[idx++] = CMD_DAP_SWD_SEQUENCE;
561         command[idx++] = 3;     /* sequence count */
562
563         /* sequence 0: packet request for TARGETSEL */
564         command[idx++] = SEQ_WR | 8;
565         command[idx++] = SWD_CMD_START | swd_cmd(false, false, DP_TARGETSEL) | SWD_CMD_STOP | SWD_CMD_PARK;
566
567         /* sequence 1: read Trn ACK Trn, no expectation for target to ACK  */
568         command[idx++] = SEQ_RD | 5;
569
570         /* sequence 2: WDATA plus parity */
571         command[idx++] = SEQ_WR | (32 + 1);
572         h_u32_to_le(command + idx, instance_id);
573         idx += 4;
574         command[idx++] = parity_u32(instance_id);
575
576         retval = cmsis_dap_xfer(cmsis_dap_handle, idx);
577
578         if (retval != ERROR_OK || cmsis_dap_handle->response[1] != DAP_OK) {
579                 LOG_ERROR("CMSIS-DAP command SWD_Sequence failed.");
580                 return ERROR_JTAG_DEVICE_ERROR;
581         }
582         return ERROR_OK;
583 }
584
585 static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
586 {
587         uint8_t *command = cmsis_dap_handle->command;
588         struct pending_request_block *block = &pending_fifo[pending_fifo_put_idx];
589
590         LOG_DEBUG_IO("Executing %d queued transactions from FIFO index %d", block->transfer_count, pending_fifo_put_idx);
591
592         if (queued_retval != ERROR_OK) {
593                 LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
594                 goto skip;
595         }
596
597         if (block->transfer_count == 0)
598                 goto skip;
599
600         command[0] = CMD_DAP_TFER;
601         command[1] = 0x00;      /* DAP Index */
602         command[2] = block->transfer_count;
603         size_t idx = 3;
604
605         for (int i = 0; i < block->transfer_count; i++) {
606                 struct pending_transfer_result *transfer = &(block->transfers[i]);
607                 uint8_t cmd = transfer->cmd;
608                 uint32_t data = transfer->data;
609
610                 LOG_DEBUG_IO("%s %s reg %x %"PRIx32,
611                                 cmd & SWD_CMD_APnDP ? "AP" : "DP",
612                                 cmd & SWD_CMD_RnW ? "read" : "write",
613                           (cmd & SWD_CMD_A32) >> 1, data);
614
615                 /* When proper WAIT handling is implemented in the
616                  * common SWD framework, this kludge can be
617                  * removed. However, this might lead to minor
618                  * performance degradation as the adapter wouldn't be
619                  * able to automatically retry anything (because ARM
620                  * has forgotten to implement sticky error flags
621                  * clearing). See also comments regarding
622                  * cmsis_dap_cmd_DAP_TFER_Configure() and
623                  * cmsis_dap_cmd_DAP_SWD_Configure() in
624                  * cmsis_dap_init().
625                  */
626                 if (!(cmd & SWD_CMD_RnW) &&
627                     !(cmd & SWD_CMD_APnDP) &&
628                     (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
629                     (data & CORUNDETECT)) {
630                         LOG_DEBUG("refusing to enable sticky overrun detection");
631                         data &= ~CORUNDETECT;
632                 }
633
634                 command[idx++] = (cmd >> 1) & 0x0f;
635                 if (!(cmd & SWD_CMD_RnW)) {
636                         h_u32_to_le(&command[idx], data);
637                         idx += 4;
638                 }
639         }
640
641         int retval = dap->backend->write(dap, idx, USB_TIMEOUT);
642
643         if (retval < 0) {
644                 queued_retval = retval;
645                 goto skip;
646         } else {
647                 queued_retval = ERROR_OK;
648         }
649
650         pending_fifo_put_idx = (pending_fifo_put_idx + 1) % dap->packet_count;
651         pending_fifo_block_count++;
652         if (pending_fifo_block_count > dap->packet_count)
653                 LOG_ERROR("too much pending writes %d", pending_fifo_block_count);
654
655         return;
656
657 skip:
658         block->transfer_count = 0;
659 }
660
661 static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
662 {
663         struct pending_request_block *block = &pending_fifo[pending_fifo_get_idx];
664
665         if (pending_fifo_block_count == 0)
666                 LOG_ERROR("no pending write");
667
668         /* get reply */
669         int retval = dap->backend->read(dap, timeout_ms);
670         if (retval == ERROR_TIMEOUT_REACHED && timeout_ms < USB_TIMEOUT)
671                 return;
672
673         if (retval <= 0) {
674                 LOG_DEBUG("error reading data");
675                 queued_retval = ERROR_FAIL;
676                 goto skip;
677         }
678
679         uint8_t *resp = dap->response;
680         if (resp[0] != CMD_DAP_TFER) {
681                 LOG_ERROR("CMSIS-DAP command mismatch. Expected 0x%" PRIx8
682                          " received 0x%" PRIx8, CMD_DAP_TFER, resp[0]);
683                 queued_retval = ERROR_FAIL;
684                 goto skip;
685         }
686
687         uint8_t transfer_count = resp[1];
688         uint8_t ack = resp[2] & 0x07;
689         if (resp[2] & 0x08) {
690                 LOG_DEBUG("CMSIS-DAP Protocol Error @ %d (wrong parity)", transfer_count);
691                 queued_retval = ERROR_FAIL;
692                 goto skip;
693         }
694         if (ack != SWD_ACK_OK) {
695                 LOG_DEBUG("SWD ack not OK @ %d %s", transfer_count,
696                           ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
697                 queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
698                 /* TODO: use results of transfers completed before the error occurred? */
699                 goto skip;
700         }
701
702         if (block->transfer_count != transfer_count)
703                 LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
704                           block->transfer_count, transfer_count);
705
706         LOG_DEBUG_IO("Received results of %d queued transactions FIFO index %d",
707                  transfer_count, pending_fifo_get_idx);
708         size_t idx = 3;
709         for (int i = 0; i < transfer_count; i++) {
710                 struct pending_transfer_result *transfer = &(block->transfers[i]);
711                 if (transfer->cmd & SWD_CMD_RnW) {
712                         static uint32_t last_read;
713                         uint32_t data = le_to_h_u32(&resp[idx]);
714                         uint32_t tmp = data;
715                         idx += 4;
716
717                         LOG_DEBUG_IO("Read result: %"PRIx32, data);
718
719                         /* Imitate posted AP reads */
720                         if ((transfer->cmd & SWD_CMD_APnDP) ||
721                             ((transfer->cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) {
722                                 tmp = last_read;
723                                 last_read = data;
724                         }
725
726                         if (transfer->buffer)
727                                 *(uint32_t *)(transfer->buffer) = tmp;
728                 }
729         }
730
731 skip:
732         block->transfer_count = 0;
733         pending_fifo_get_idx = (pending_fifo_get_idx + 1) % dap->packet_count;
734         pending_fifo_block_count--;
735 }
736
737 static int cmsis_dap_swd_run_queue(void)
738 {
739         if (pending_fifo_block_count)
740                 cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
741
742         cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
743
744         while (pending_fifo_block_count)
745                 cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
746
747         pending_fifo_put_idx = 0;
748         pending_fifo_get_idx = 0;
749
750         int retval = queued_retval;
751         queued_retval = ERROR_OK;
752
753         return retval;
754 }
755
756 static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
757 {
758         bool targetsel_cmd = swd_cmd(false, false, DP_TARGETSEL) == cmd;
759
760         if (pending_fifo[pending_fifo_put_idx].transfer_count == pending_queue_len
761                          || targetsel_cmd) {
762                 if (pending_fifo_block_count)
763                         cmsis_dap_swd_read_process(cmsis_dap_handle, 0);
764
765                 /* Not enough room in the queue. Run the queue. */
766                 cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
767
768                 if (pending_fifo_block_count >= cmsis_dap_handle->packet_count)
769                         cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
770         }
771
772         if (queued_retval != ERROR_OK)
773                 return;
774
775         if (targetsel_cmd) {
776                 cmsis_dap_metacmd_targetsel(data);
777                 return;
778         }
779
780         struct pending_request_block *block = &pending_fifo[pending_fifo_put_idx];
781         struct pending_transfer_result *transfer = &(block->transfers[block->transfer_count]);
782         transfer->data = data;
783         transfer->cmd = cmd;
784         if (cmd & SWD_CMD_RnW) {
785                 /* Queue a read transaction */
786                 transfer->buffer = dst;
787         }
788         block->transfer_count++;
789 }
790
791 static void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
792 {
793         assert(!(cmd & SWD_CMD_RnW));
794         cmsis_dap_swd_queue_cmd(cmd, NULL, value);
795 }
796
797 static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
798 {
799         assert(cmd & SWD_CMD_RnW);
800         cmsis_dap_swd_queue_cmd(cmd, value, 0);
801 }
802
803 static int cmsis_dap_get_serial_info(void)
804 {
805         uint8_t *data;
806
807         int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_SERNUM, &data);
808         if (retval != ERROR_OK)
809                 return retval;
810
811         if (data[0]) /* strlen */
812                 LOG_INFO("CMSIS-DAP: Serial# = %s", &data[1]);
813
814         return ERROR_OK;
815 }
816
817 static int cmsis_dap_get_version_info(void)
818 {
819         uint8_t *data;
820
821         /* INFO_ID_FW_VER - string */
822         int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
823         if (retval != ERROR_OK)
824                 return retval;
825
826         if (data[0]) /* strlen */
827                 LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
828
829         return ERROR_OK;
830 }
831
832 static int cmsis_dap_get_caps_info(void)
833 {
834         uint8_t *data;
835
836         /* INFO_ID_CAPS - byte */
837         int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
838         if (retval != ERROR_OK)
839                 return retval;
840
841         if (data[0] == 1) {
842                 uint8_t caps = data[1];
843
844                 cmsis_dap_handle->caps = caps;
845
846                 if (caps & INFO_CAPS_SWD)
847                         LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
848                 if (caps & INFO_CAPS_JTAG)
849                         LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
850         }
851
852         return ERROR_OK;
853 }
854
855 static int cmsis_dap_get_status(void)
856 {
857         uint8_t d;
858
859         int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
860
861         if (retval == ERROR_OK) {
862                 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
863                         (d & SWJ_PIN_TCK) ? 1 : 0,
864                         (d & SWJ_PIN_TMS) ? 1 : 0,
865                         (d & SWJ_PIN_TDI) ? 1 : 0,
866                         (d & SWJ_PIN_TDO) ? 1 : 0,
867                         (d & SWJ_PIN_TRST) ? 1 : 0,
868                         (d & SWJ_PIN_SRST) ? 1 : 0);
869         }
870
871         return retval;
872 }
873
874 static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
875 {
876         const uint8_t *s;
877         unsigned int s_len;
878         int retval;
879
880         if ((output_pins & (SWJ_PIN_SRST | SWJ_PIN_TRST)) == (SWJ_PIN_SRST | SWJ_PIN_TRST)) {
881                 /* Following workaround deasserts reset on most adapters.
882                  * Do not reconnect if a reset line is active!
883                  * Reconnecting would break connecting under reset. */
884
885                 /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
886                 cmsis_dap_cmd_DAP_Disconnect();
887
888                 /* When we are reconnecting, DAP_Connect needs to be rerun, at
889                  * least on Keil ULINK-ME */
890                 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
891                 if (retval != ERROR_OK)
892                         return retval;
893         }
894
895         switch (seq) {
896         case LINE_RESET:
897                 LOG_DEBUG_IO("SWD line reset");
898                 s = swd_seq_line_reset;
899                 s_len = swd_seq_line_reset_len;
900                 break;
901         case JTAG_TO_SWD:
902                 LOG_DEBUG("JTAG-to-SWD");
903                 s = swd_seq_jtag_to_swd;
904                 s_len = swd_seq_jtag_to_swd_len;
905                 break;
906         case JTAG_TO_DORMANT:
907                 LOG_DEBUG("JTAG-to-DORMANT");
908                 s = swd_seq_jtag_to_dormant;
909                 s_len = swd_seq_jtag_to_dormant_len;
910                 break;
911         case SWD_TO_JTAG:
912                 LOG_DEBUG("SWD-to-JTAG");
913                 s = swd_seq_swd_to_jtag;
914                 s_len = swd_seq_swd_to_jtag_len;
915                 break;
916         case SWD_TO_DORMANT:
917                 LOG_DEBUG("SWD-to-DORMANT");
918                 s = swd_seq_swd_to_dormant;
919                 s_len = swd_seq_swd_to_dormant_len;
920                 break;
921         case DORMANT_TO_SWD:
922                 LOG_DEBUG("DORMANT-to-SWD");
923                 s = swd_seq_dormant_to_swd;
924                 s_len = swd_seq_dormant_to_swd_len;
925                 break;
926         default:
927                 LOG_ERROR("Sequence %d not supported", seq);
928                 return ERROR_FAIL;
929         }
930
931         retval = cmsis_dap_cmd_DAP_SWJ_Sequence(s_len, s);
932         if (retval != ERROR_OK)
933                 return retval;
934
935         /* Atmel EDBG needs renew clock setting after SWJ_Sequence
936          * otherwise default frequency is used */
937         return cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
938 }
939
940 static int cmsis_dap_swd_open(void)
941 {
942         int retval;
943
944         if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
945                 LOG_ERROR("CMSIS-DAP: SWD not supported");
946                 return ERROR_JTAG_DEVICE_ERROR;
947         }
948
949         retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
950         if (retval != ERROR_OK)
951                 return retval;
952
953         /* Add more setup here.??... */
954
955         LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
956         return ERROR_OK;
957 }
958
959 static int cmsis_dap_init(void)
960 {
961         int retval;
962         uint8_t *data;
963
964         retval = cmsis_dap_open();
965         if (retval != ERROR_OK)
966                 return retval;
967
968         cmsis_dap_flush_read(cmsis_dap_handle);
969
970         retval = cmsis_dap_get_caps_info();
971         if (retval != ERROR_OK)
972                 return retval;
973
974         retval = cmsis_dap_get_version_info();
975         if (retval != ERROR_OK)
976                 return retval;
977
978         retval = cmsis_dap_get_serial_info();
979         if (retval != ERROR_OK)
980                 return retval;
981
982         if (swd_mode) {
983                 retval = cmsis_dap_swd_open();
984                 if (retval != ERROR_OK)
985                         return retval;
986         } else {
987                 /* Connect in JTAG mode */
988                 if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
989                         LOG_ERROR("CMSIS-DAP: JTAG not supported");
990                         return ERROR_JTAG_DEVICE_ERROR;
991                 }
992
993                 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
994                 if (retval != ERROR_OK)
995                         return retval;
996
997                 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
998         }
999
1000         /* Be conservative and suppress submitting multiple HID requests
1001          * until we get packet count info from the adaptor */
1002         cmsis_dap_handle->packet_count = 1;
1003         pending_queue_len = 12;
1004
1005         /* INFO_ID_PKT_SZ - short */
1006         retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
1007         if (retval != ERROR_OK)
1008                 goto init_err;
1009
1010         if (data[0] == 2) {  /* short */
1011                 uint16_t pkt_sz = data[1] + (data[2] << 8);
1012                 if (pkt_sz != cmsis_dap_handle->packet_size) {
1013
1014                         /* 4 bytes of command header + 5 bytes per register
1015                          * write. For bulk read sequences just 4 bytes are
1016                          * needed per transfer, so this is suboptimal. */
1017                         pending_queue_len = (pkt_sz - 4) / 5;
1018
1019                         free(cmsis_dap_handle->packet_buffer);
1020                         retval = cmsis_dap_handle->backend->packet_buffer_alloc(cmsis_dap_handle, pkt_sz);
1021                         if (retval != ERROR_OK)
1022                                 goto init_err;
1023
1024                         LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRIu16, pkt_sz);
1025                 }
1026         }
1027
1028         /* INFO_ID_PKT_CNT - byte */
1029         retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
1030         if (retval != ERROR_OK)
1031                 goto init_err;
1032
1033         if (data[0] == 1) { /* byte */
1034                 int pkt_cnt = data[1];
1035                 if (pkt_cnt > 1)
1036                         cmsis_dap_handle->packet_count = MIN(MAX_PENDING_REQUESTS, pkt_cnt);
1037
1038                 LOG_DEBUG("CMSIS-DAP: Packet Count = %d", pkt_cnt);
1039         }
1040
1041         LOG_DEBUG("Allocating FIFO for %d pending packets", cmsis_dap_handle->packet_count);
1042         for (int i = 0; i < cmsis_dap_handle->packet_count; i++) {
1043                 pending_fifo[i].transfers = malloc(pending_queue_len * sizeof(struct pending_transfer_result));
1044                 if (!pending_fifo[i].transfers) {
1045                         LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
1046                         retval = ERROR_FAIL;
1047                         goto init_err;
1048                 }
1049         }
1050
1051         /* Intentionally not checked for error, just logs an info message
1052          * not vital for further debugging */
1053         (void)cmsis_dap_get_status();
1054
1055         /* Now try to connect to the target
1056          * TODO: This is all SWD only @ present */
1057         retval = cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
1058         if (retval != ERROR_OK)
1059                 goto init_err;
1060
1061         /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
1062          * up to 64 times. This must be changed to 0 if sticky
1063          * overrun detection is enabled. */
1064         retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
1065         if (retval != ERROR_OK)
1066                 goto init_err;
1067
1068         if (swd_mode) {
1069                 /* Data Phase (bit 2) must be set to 1 if sticky overrun
1070                  * detection is enabled */
1071                 retval = cmsis_dap_cmd_DAP_SWD_Configure(0);    /* 1 TRN, no Data Phase */
1072                 if (retval != ERROR_OK)
1073                         goto init_err;
1074         }
1075         /* Both LEDs on */
1076         /* Intentionally not checked for error, debugging will work
1077          * without LEDs */
1078         (void)cmsis_dap_cmd_DAP_LED(LED_ID_CONNECT, LED_ON);
1079         (void)cmsis_dap_cmd_DAP_LED(LED_ID_RUN, LED_ON);
1080
1081         /* support connecting with srst asserted */
1082         enum reset_types jtag_reset_config = jtag_get_reset_config();
1083
1084         if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1085                 if (jtag_reset_config & RESET_SRST_NO_GATING) {
1086                         retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, SWJ_PIN_SRST, 0, NULL);
1087                         if (retval != ERROR_OK)
1088                                 goto init_err;
1089                         LOG_INFO("Connecting under reset");
1090                 }
1091         }
1092         LOG_INFO("CMSIS-DAP: Interface ready");
1093         return ERROR_OK;
1094
1095 init_err:
1096         cmsis_dap_quit();
1097         return retval;
1098 }
1099
1100 static int cmsis_dap_swd_init(void)
1101 {
1102         swd_mode = true;
1103         return ERROR_OK;
1104 }
1105
1106 static int cmsis_dap_quit(void)
1107 {
1108         cmsis_dap_cmd_DAP_Disconnect();
1109         /* Both LEDs off */
1110         cmsis_dap_cmd_DAP_LED(LED_ID_RUN, LED_OFF);
1111         cmsis_dap_cmd_DAP_LED(LED_ID_CONNECT, LED_OFF);
1112
1113         cmsis_dap_close(cmsis_dap_handle);
1114
1115         return ERROR_OK;
1116 }
1117
1118 static int cmsis_dap_reset(int trst, int srst)
1119 {
1120         /* Set both TRST and SRST even if they're not enabled as
1121          * there's no way to tristate them */
1122
1123         output_pins = 0;
1124         if (!srst)
1125                 output_pins |= SWJ_PIN_SRST;
1126         if (!trst)
1127                 output_pins |= SWJ_PIN_TRST;
1128
1129         int retval = cmsis_dap_cmd_DAP_SWJ_Pins(output_pins,
1130                         SWJ_PIN_TRST | SWJ_PIN_SRST, 0, NULL);
1131         if (retval != ERROR_OK)
1132                 LOG_ERROR("CMSIS-DAP: Interface reset failed");
1133         return retval;
1134 }
1135
1136 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
1137 {
1138 #if 0
1139         int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
1140         if (retval != ERROR_OK)
1141 #endif
1142                 jtag_sleep(cmd->cmd.sleep->us);
1143 }
1144
1145 /* Set TMS high for five TCK clocks, to move the TAP to the Test-Logic-Reset state */
1146 static int cmsis_dap_execute_tlr_reset(struct jtag_command *cmd)
1147 {
1148         LOG_INFO("cmsis-dap JTAG TLR_RESET");
1149         uint8_t seq = 0xff;
1150         int ret = cmsis_dap_cmd_DAP_SWJ_Sequence(8, &seq);
1151         if (ret == ERROR_OK)
1152                 tap_set_state(TAP_RESET);
1153         return ret;
1154 }
1155
1156 /* Set new end state */
1157 static void cmsis_dap_end_state(tap_state_t state)
1158 {
1159         if (tap_is_state_stable(state))
1160                 tap_set_end_state(state);
1161         else {
1162                 LOG_ERROR("BUG: %i is not a valid end state", state);
1163                 exit(-1);
1164         }
1165 }
1166
1167 #ifdef SPRINT_BINARY
1168 static void sprint_binary(char *s, const uint8_t *buf, int offset, int len)
1169 {
1170         if (!len)
1171                 return;
1172
1173         /*
1174         buf = { 0x18 } len=5 should result in: 11000
1175         buf = { 0xff 0x18 } len=13 should result in: 11111111 11000
1176         buf = { 0xc0 0x18 } offset=3 len=10 should result in: 11000 11000
1177                 i=3 there means i/8 = 0 so c = 0xFF, and
1178         */
1179         for (int i = offset; i < offset + len; ++i) {
1180                 uint8_t c = buf[i / 8], mask = 1 << (i % 8);
1181                 if ((i != offset) && !(i % 8))
1182                         putchar(' ');
1183                 *s++ = (c & mask) ? '1' : '0';
1184         }
1185         *s = 0;
1186 }
1187 #endif
1188
1189 #ifdef CMSIS_DAP_JTAG_DEBUG
1190 static void debug_parse_cmsis_buf(const uint8_t *cmd, int cmdlen)
1191 {
1192         /* cmd is a usb packet to go to the cmsis-dap interface */
1193         printf("cmsis-dap buffer (%d b): ", cmdlen);
1194         for (int i = 0; i < cmdlen; ++i)
1195                 printf(" %02x", cmd[i]);
1196         printf("\n");
1197         switch (cmd[1]) {
1198                 case CMD_DAP_JTAG_SEQ: {
1199                         printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[1], cmd[2]);
1200                         /*
1201                          * #2 = number of sequences
1202                          * #3 = sequence info 1
1203                          * #4...4+n_bytes-1 = sequence 1
1204                          * #4+n_bytes = sequence info 2
1205                          * #5+n_bytes = sequence 2 (single bit)
1206                          */
1207                         int pos = 3;
1208                         for (int seq = 0; seq < cmd[2]; ++seq) {
1209                                 uint8_t info = cmd[pos++];
1210                                 int len = info & DAP_JTAG_SEQ_TCK;
1211                                 if (len == 0)
1212                                         len = 64;
1213                                 printf("  sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
1214                                         seq, pos, info, len, info & DAP_JTAG_SEQ_TMS, info & DAP_JTAG_SEQ_TDO);
1215                                 for (int i = 0; i < DIV_ROUND_UP(len, 8); ++i)
1216                                         printf(" %02x", cmd[pos+i]);
1217                                 pos += DIV_ROUND_UP(len, 8);
1218                                 printf("\n");
1219                         }
1220                         if (pos != cmdlen) {
1221                                 printf("BUFFER LENGTH MISMATCH looks like %d but %d specified", pos, cmdlen);
1222                                 exit(-1);
1223                         }
1224
1225                         break;
1226                 }
1227                 default:
1228                         LOG_DEBUG("unknown cmsis-dap command %02x", cmd[1]);
1229                         break;
1230         }
1231 }
1232 #endif
1233
1234 static void cmsis_dap_flush(void)
1235 {
1236         if (!queued_seq_count)
1237                 return;
1238
1239         LOG_DEBUG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
1240                 queued_seq_count, queued_seq_buf_end, pending_scan_result_count);
1241
1242         /* prepare CMSIS-DAP packet */
1243         uint8_t *command = cmsis_dap_handle->command;
1244         command[0] = CMD_DAP_JTAG_SEQ;
1245         command[1] = queued_seq_count;
1246         memcpy(&command[2], queued_seq_buf, queued_seq_buf_end);
1247
1248 #ifdef CMSIS_DAP_JTAG_DEBUG
1249         debug_parse_cmsis_buf(command, queued_seq_buf_end + 2);
1250 #endif
1251
1252         /* send command to USB device */
1253         int retval = cmsis_dap_xfer(cmsis_dap_handle, queued_seq_buf_end + 2);
1254
1255         uint8_t *resp = cmsis_dap_handle->response;
1256         if (retval != ERROR_OK || resp[1] != DAP_OK) {
1257                 LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
1258                 exit(-1);
1259         }
1260
1261 #ifdef CMSIS_DAP_JTAG_DEBUG
1262         LOG_DEBUG_IO("USB response buf:");
1263         for (int c = 0; c < queued_seq_buf_end + 3; ++c)
1264                 printf("%02X ", resp[c]);
1265         printf("\n");
1266 #endif
1267
1268         /* copy scan results into client buffers */
1269         for (int i = 0; i < pending_scan_result_count; ++i) {
1270                 struct pending_scan_result *scan = &pending_scan_results[i];
1271                 LOG_DEBUG_IO("Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
1272                         i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
1273 #ifdef CMSIS_DAP_JTAG_DEBUG
1274                 for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b)
1275                         printf("%02X ", resp[2+scan->first+b]);
1276                 printf("\n");
1277 #endif
1278                 bit_copy(scan->buffer, scan->buffer_offset, &resp[2 + scan->first], 0, scan->length);
1279         }
1280
1281         /* reset */
1282         queued_seq_count = 0;
1283         queued_seq_buf_end = 0;
1284         queued_seq_tdo_ptr = 0;
1285         pending_scan_result_count = 0;
1286 }
1287
1288 /* queue a sequence of bits to clock out TDI / in TDO, executing if the buffer is full.
1289  *
1290  * sequence=NULL means clock out zeros on TDI
1291  * tdo_buffer=NULL means don't capture TDO
1292  */
1293 static void cmsis_dap_add_jtag_sequence(int s_len, const uint8_t *sequence, int s_offset,
1294                                         bool tms, uint8_t *tdo_buffer, int tdo_buffer_offset)
1295 {
1296         LOG_DEBUG_IO("[at %d] %d bits, tms %s, seq offset %d, tdo buf %p, tdo offset %d",
1297                 queued_seq_buf_end,
1298                 s_len, tms ? "HIGH" : "LOW", s_offset, tdo_buffer, tdo_buffer_offset);
1299
1300         if (s_len == 0)
1301                 return;
1302
1303         if (s_len > 64) {
1304                 LOG_DEBUG_IO("START JTAG SEQ SPLIT");
1305                 for (int offset = 0; offset < s_len; offset += 64) {
1306                         int len = s_len - offset;
1307                         if (len > 64)
1308                                 len = 64;
1309                         LOG_DEBUG_IO("Splitting long jtag sequence: %d-bit chunk starting at offset %d", len, offset);
1310                         cmsis_dap_add_jtag_sequence(
1311                                 len,
1312                                 sequence,
1313                                 s_offset + offset,
1314                                 tms,
1315                                 tdo_buffer,
1316                                 tdo_buffer == NULL ? 0 : (tdo_buffer_offset + offset)
1317                                 );
1318                 }
1319                 LOG_DEBUG_IO("END JTAG SEQ SPLIT");
1320                 return;
1321         }
1322
1323         int cmd_len = 1 + DIV_ROUND_UP(s_len, 8);
1324         if (queued_seq_count >= 255 || queued_seq_buf_end + cmd_len > QUEUED_SEQ_BUF_LEN)
1325                 /* empty out the buffer */
1326                 cmsis_dap_flush();
1327
1328         ++queued_seq_count;
1329
1330         /* control byte */
1331         queued_seq_buf[queued_seq_buf_end] =
1332                 (tms ? DAP_JTAG_SEQ_TMS : 0) |
1333                 (tdo_buffer != NULL ? DAP_JTAG_SEQ_TDO : 0) |
1334                 (s_len == 64 ? 0 : s_len);
1335
1336         if (sequence != NULL)
1337                 bit_copy(&queued_seq_buf[queued_seq_buf_end + 1], 0, sequence, s_offset, s_len);
1338         else
1339                 memset(&queued_seq_buf[queued_seq_buf_end + 1], 0, DIV_ROUND_UP(s_len, 8));
1340
1341         queued_seq_buf_end += cmd_len;
1342
1343         if (tdo_buffer != NULL) {
1344                 struct pending_scan_result *scan = &pending_scan_results[pending_scan_result_count++];
1345                 scan->first = queued_seq_tdo_ptr;
1346                 queued_seq_tdo_ptr += DIV_ROUND_UP(s_len, 8);
1347                 scan->length = s_len;
1348                 scan->buffer = tdo_buffer;
1349                 scan->buffer_offset = tdo_buffer_offset;
1350         }
1351 }
1352
1353 /* queue a sequence of bits to clock out TMS, executing if the buffer is full */
1354 static void cmsis_dap_add_tms_sequence(const uint8_t *sequence, int s_len)
1355 {
1356         LOG_DEBUG_IO("%d bits: %02X", s_len, *sequence);
1357         /* we use a series of CMD_DAP_JTAG_SEQ commands to toggle TMS,
1358            because even though it seems ridiculously inefficient, it
1359            allows us to combine TMS and scan sequences into the same
1360            USB packet. */
1361         /* TODO: combine runs of the same tms value */
1362         for (int i = 0; i < s_len; ++i) {
1363                 bool bit = (sequence[i / 8] & (1 << (i % 8))) != 0;
1364                 cmsis_dap_add_jtag_sequence(1, NULL, 0, bit, NULL, 0);
1365         }
1366 }
1367
1368 /* Move to the end state by queuing a sequence to clock into TMS */
1369 static void cmsis_dap_state_move(void)
1370 {
1371         uint8_t tms_scan;
1372         uint8_t tms_scan_bits;
1373
1374         tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1375         tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1376
1377         LOG_DEBUG_IO("state move from %s to %s: %d clocks, %02X on tms",
1378                 tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()),
1379                 tms_scan_bits, tms_scan);
1380         cmsis_dap_add_tms_sequence(&tms_scan, tms_scan_bits);
1381
1382         tap_set_state(tap_get_end_state());
1383 }
1384
1385
1386 /* Execute a JTAG scan operation by queueing TMS and TDI/TDO sequences */
1387 static void cmsis_dap_execute_scan(struct jtag_command *cmd)
1388 {
1389         LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
1390                 jtag_scan_type(cmd->cmd.scan));
1391
1392         /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
1393         while (cmd->cmd.scan->num_fields > 0
1394                         && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
1395                 cmd->cmd.scan->num_fields--;
1396                 LOG_DEBUG("discarding trailing empty field");
1397         }
1398
1399         if (cmd->cmd.scan->num_fields == 0) {
1400                 LOG_DEBUG("empty scan, doing nothing");
1401                 return;
1402         }
1403
1404         if (cmd->cmd.scan->ir_scan) {
1405                 if (tap_get_state() != TAP_IRSHIFT) {
1406                         cmsis_dap_end_state(TAP_IRSHIFT);
1407                         cmsis_dap_state_move();
1408                 }
1409         } else {
1410                 if (tap_get_state() != TAP_DRSHIFT) {
1411                         cmsis_dap_end_state(TAP_DRSHIFT);
1412                         cmsis_dap_state_move();
1413                 }
1414         }
1415
1416         cmsis_dap_end_state(cmd->cmd.scan->end_state);
1417
1418         struct scan_field *field = cmd->cmd.scan->fields;
1419         unsigned scan_size = 0;
1420
1421         for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
1422                 scan_size += field->num_bits;
1423                 LOG_DEBUG_IO("%s%s field %d/%d %d bits",
1424                         field->in_value ? "in" : "",
1425                         field->out_value ? "out" : "",
1426                         i,
1427                         cmd->cmd.scan->num_fields,
1428                         field->num_bits);
1429
1430                 if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
1431                         LOG_DEBUG_IO("Last field and have to move out of SHIFT state");
1432                         /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
1433                          * movement. This last field can't have length zero, it was checked above. */
1434                         cmsis_dap_add_jtag_sequence(
1435                                 field->num_bits - 1, /* number of bits to clock */
1436                                 field->out_value, /* output sequence */
1437                                 0, /* output offset */
1438                                 false, /* TMS low */
1439                                 field->in_value,
1440                                 0);
1441
1442                         /* Clock the last bit out, with TMS high */
1443                         uint8_t last_bit = 0;
1444                         if (field->out_value)
1445                                 bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
1446                         cmsis_dap_add_jtag_sequence(
1447                                 1,
1448                                 &last_bit,
1449                                 0,
1450                                 true,
1451                                 field->in_value,
1452                                 field->num_bits - 1);
1453                         tap_set_state(tap_state_transition(tap_get_state(), 1));
1454
1455                         /* Now clock one more cycle, with TMS low, to get us into a PAUSE state */
1456                         cmsis_dap_add_jtag_sequence(
1457                                 1,
1458                                 &last_bit,
1459                                 0,
1460                                 false,
1461                                 NULL,
1462                                 0);
1463                         tap_set_state(tap_state_transition(tap_get_state(), 0));
1464                 } else {
1465                         LOG_DEBUG_IO("Internal field, staying in SHIFT state afterwards");
1466                         /* Clocking part of a sequence into DR or IR with TMS=0,
1467                            leaving TMS=0 at the end so we can continue later */
1468                         cmsis_dap_add_jtag_sequence(
1469                                 field->num_bits,
1470                                 field->out_value,
1471                                 0,
1472                                 false,
1473                                 field->in_value,
1474                                 0);
1475                 }
1476         }
1477
1478         if (tap_get_state() != tap_get_end_state()) {
1479                 cmsis_dap_end_state(tap_get_end_state());
1480                 cmsis_dap_state_move();
1481         }
1482
1483         LOG_DEBUG_IO("%s scan, %i bits, end in %s",
1484                 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1485                 tap_state_name(tap_get_end_state()));
1486 }
1487
1488 static void cmsis_dap_pathmove(int num_states, tap_state_t *path)
1489 {
1490         int i;
1491         uint8_t tms0 = 0x00;
1492         uint8_t tms1 = 0xff;
1493
1494         for (i = 0; i < num_states; i++) {
1495                 if (path[i] == tap_state_transition(tap_get_state(), false))
1496                         cmsis_dap_add_tms_sequence(&tms0, 1);
1497                 else if (path[i] == tap_state_transition(tap_get_state(), true))
1498                         cmsis_dap_add_tms_sequence(&tms1, 1);
1499                 else {
1500                         LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
1501                                   tap_state_name(tap_get_state()), tap_state_name(path[i]));
1502                         exit(-1);
1503                 }
1504
1505                 tap_set_state(path[i]);
1506         }
1507
1508         cmsis_dap_end_state(tap_get_state());
1509 }
1510
1511 static void cmsis_dap_execute_pathmove(struct jtag_command *cmd)
1512 {
1513         LOG_DEBUG_IO("pathmove: %i states, end in %i",
1514                       cmd->cmd.pathmove->num_states,
1515                cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
1516
1517         cmsis_dap_pathmove(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
1518 }
1519
1520 static void cmsis_dap_stableclocks(int num_cycles)
1521 {
1522         int i;
1523
1524         uint8_t tms = tap_get_state() == TAP_RESET;
1525         /* TODO: Perform optimizations? */
1526         /* Execute num_cycles. */
1527         for (i = 0; i < num_cycles; i++)
1528                 cmsis_dap_add_tms_sequence(&tms, 1);
1529 }
1530
1531 static void cmsis_dap_runtest(int num_cycles)
1532 {
1533         tap_state_t saved_end_state = tap_get_end_state();
1534
1535         /* Only do a state_move when we're not already in IDLE. */
1536         if (tap_get_state() != TAP_IDLE) {
1537                 cmsis_dap_end_state(TAP_IDLE);
1538                 cmsis_dap_state_move();
1539         }
1540         cmsis_dap_stableclocks(num_cycles);
1541
1542         /* Finish in end_state. */
1543         cmsis_dap_end_state(saved_end_state);
1544
1545         if (tap_get_state() != tap_get_end_state())
1546                 cmsis_dap_state_move();
1547 }
1548
1549 static void cmsis_dap_execute_runtest(struct jtag_command *cmd)
1550 {
1551         LOG_DEBUG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
1552                       cmd->cmd.runtest->end_state);
1553
1554         cmsis_dap_end_state(cmd->cmd.runtest->end_state);
1555         cmsis_dap_runtest(cmd->cmd.runtest->num_cycles);
1556 }
1557
1558 static void cmsis_dap_execute_stableclocks(struct jtag_command *cmd)
1559 {
1560         LOG_DEBUG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
1561         cmsis_dap_stableclocks(cmd->cmd.runtest->num_cycles);
1562 }
1563
1564 static void cmsis_dap_execute_tms(struct jtag_command *cmd)
1565 {
1566         LOG_DEBUG_IO("TMS: %d bits", cmd->cmd.tms->num_bits);
1567         cmsis_dap_cmd_DAP_SWJ_Sequence(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
1568 }
1569
1570 /* TODO: Is there need to call cmsis_dap_flush() for the JTAG_PATHMOVE,
1571  * JTAG_RUNTEST, JTAG_STABLECLOCKS? */
1572 static void cmsis_dap_execute_command(struct jtag_command *cmd)
1573 {
1574         switch (cmd->type) {
1575                 case JTAG_SLEEP:
1576                         cmsis_dap_flush();
1577                         cmsis_dap_execute_sleep(cmd);
1578                         break;
1579                 case JTAG_TLR_RESET:
1580                         cmsis_dap_flush();
1581                         cmsis_dap_execute_tlr_reset(cmd);
1582                         break;
1583                 case JTAG_SCAN:
1584                         cmsis_dap_execute_scan(cmd);
1585                         break;
1586                 case JTAG_PATHMOVE:
1587                         cmsis_dap_execute_pathmove(cmd);
1588                         break;
1589                 case JTAG_RUNTEST:
1590                         cmsis_dap_execute_runtest(cmd);
1591                         break;
1592                 case JTAG_STABLECLOCKS:
1593                         cmsis_dap_execute_stableclocks(cmd);
1594                         break;
1595                 case JTAG_TMS:
1596                         cmsis_dap_execute_tms(cmd);
1597                         break;
1598                 default:
1599                         LOG_ERROR("BUG: unknown JTAG command type 0x%X encountered", cmd->type);
1600                         exit(-1);
1601         }
1602 }
1603
1604 static int cmsis_dap_execute_queue(void)
1605 {
1606         struct jtag_command *cmd = jtag_command_queue;
1607
1608         while (cmd != NULL) {
1609                 cmsis_dap_execute_command(cmd);
1610                 cmd = cmd->next;
1611         }
1612
1613         cmsis_dap_flush();
1614
1615         return ERROR_OK;
1616 }
1617
1618 static int cmsis_dap_speed(int speed)
1619 {
1620         if (speed == 0) {
1621                 LOG_ERROR("RTCK not supported. Set nonzero \"adapter speed\".");
1622                 return ERROR_JTAG_NOT_IMPLEMENTED;
1623         }
1624
1625         return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
1626 }
1627
1628 static int cmsis_dap_speed_div(int speed, int *khz)
1629 {
1630         *khz = speed;
1631         return ERROR_OK;
1632 }
1633
1634 static int cmsis_dap_khz(int khz, int *jtag_speed)
1635 {
1636         *jtag_speed = khz;
1637         return ERROR_OK;
1638 }
1639
1640 COMMAND_HANDLER(cmsis_dap_handle_info_command)
1641 {
1642         if (cmsis_dap_get_version_info() == ERROR_OK)
1643                 cmsis_dap_get_status();
1644
1645         return ERROR_OK;
1646 }
1647
1648 COMMAND_HANDLER(cmsis_dap_handle_cmd_command)
1649 {
1650         int retval;
1651         unsigned i;
1652         uint8_t *command = cmsis_dap_handle->command;
1653
1654         for (i = 0; i < CMD_ARGC; i++)
1655                 command[i] = strtoul(CMD_ARGV[i], NULL, 16);
1656
1657         retval = cmsis_dap_xfer(cmsis_dap_handle, CMD_ARGC);
1658
1659         if (retval != ERROR_OK) {
1660                 LOG_ERROR("CMSIS-DAP command failed.");
1661                 return ERROR_JTAG_DEVICE_ERROR;
1662         }
1663
1664         uint8_t *resp = cmsis_dap_handle->response;
1665         LOG_INFO("Returned data %02" PRIx8 " %02" PRIx8 " %02" PRIx8 " %02" PRIx8,
1666                 resp[1], resp[2], resp[3], resp[4]);
1667
1668         return ERROR_OK;
1669 }
1670
1671 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
1672 {
1673         if (CMD_ARGC > MAX_USB_IDS * 2) {
1674                 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
1675                         "(maximum is %d pairs)", MAX_USB_IDS);
1676                 CMD_ARGC = MAX_USB_IDS * 2;
1677         }
1678         if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
1679                 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
1680                 if (CMD_ARGC < 2)
1681                         return ERROR_COMMAND_SYNTAX_ERROR;
1682                 /* remove the incomplete trailing id */
1683                 CMD_ARGC -= 1;
1684         }
1685
1686         unsigned i;
1687         for (i = 0; i < CMD_ARGC; i += 2) {
1688                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
1689                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
1690         }
1691
1692         /*
1693          * Explicitly terminate, in case there are multiples instances of
1694          * cmsis_dap_vid_pid.
1695          */
1696         cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
1697
1698         return ERROR_OK;
1699 }
1700
1701 COMMAND_HANDLER(cmsis_dap_handle_serial_command)
1702 {
1703         if (CMD_ARGC == 1)
1704                 cmsis_dap_serial = strdup(CMD_ARGV[0]);
1705         else
1706                 LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
1707
1708         return ERROR_OK;
1709 }
1710
1711 COMMAND_HANDLER(cmsis_dap_handle_backend_command)
1712 {
1713         if (CMD_ARGC == 1) {
1714                 if (strcmp(CMD_ARGV[0], "auto") == 0) {
1715                         cmsis_dap_backend = -1; /* autoselect */
1716                 } else {
1717                         for (unsigned int i = 0; i < ARRAY_SIZE(cmsis_dap_backends); i++) {
1718                                 if (strcasecmp(cmsis_dap_backends[i]->name, CMD_ARGV[0]) == 0) {
1719                                         cmsis_dap_backend = i;
1720                                         return ERROR_OK;
1721                                 }
1722                         }
1723
1724                         LOG_ERROR("invalid backend argument to cmsis_dap_backend <backend>");
1725                 }
1726         } else {
1727                 LOG_ERROR("expected exactly one argument to cmsis_dap_backend <backend>");
1728         }
1729
1730         return ERROR_OK;
1731 }
1732
1733 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
1734         {
1735                 .name = "info",
1736                 .handler = &cmsis_dap_handle_info_command,
1737                 .mode = COMMAND_EXEC,
1738                 .usage = "",
1739                 .help = "show cmsis-dap info",
1740         },
1741         {
1742                 .name = "cmd",
1743                 .handler = &cmsis_dap_handle_cmd_command,
1744                 .mode = COMMAND_EXEC,
1745                 .usage = "",
1746                 .help = "issue cmsis-dap command",
1747         },
1748         COMMAND_REGISTRATION_DONE
1749 };
1750
1751
1752 static const struct command_registration cmsis_dap_command_handlers[] = {
1753         {
1754                 .name = "cmsis-dap",
1755                 .mode = COMMAND_ANY,
1756                 .help = "perform CMSIS-DAP management",
1757                 .usage = "<cmd>",
1758                 .chain = cmsis_dap_subcommand_handlers,
1759         },
1760         {
1761                 .name = "cmsis_dap_vid_pid",
1762                 .handler = &cmsis_dap_handle_vid_pid_command,
1763                 .mode = COMMAND_CONFIG,
1764                 .help = "the vendor ID and product ID of the CMSIS-DAP device",
1765                 .usage = "(vid pid)* ",
1766         },
1767         {
1768                 .name = "cmsis_dap_serial",
1769                 .handler = &cmsis_dap_handle_serial_command,
1770                 .mode = COMMAND_CONFIG,
1771                 .help = "set the serial number of the adapter",
1772                 .usage = "serial_string",
1773         },
1774         {
1775                 .name = "cmsis_dap_backend",
1776                 .handler = &cmsis_dap_handle_backend_command,
1777                 .mode = COMMAND_CONFIG,
1778                 .help = "set the communication backend to use (USB bulk or HID).",
1779                 .usage = "(auto | usb_bulk | hid)",
1780         },
1781 #if BUILD_CMSIS_DAP_USB
1782         {
1783                 .name = "cmsis_dap_usb",
1784                 .chain = cmsis_dap_usb_subcommand_handlers,
1785                 .mode = COMMAND_ANY,
1786                 .help = "USB bulk backend-specific commands",
1787                 .usage = "<cmd>",
1788         },
1789 #endif
1790         COMMAND_REGISTRATION_DONE
1791 };
1792
1793 static const struct swd_driver cmsis_dap_swd_driver = {
1794         .init = cmsis_dap_swd_init,
1795         .switch_seq = cmsis_dap_swd_switch_seq,
1796         .read_reg = cmsis_dap_swd_read_reg,
1797         .write_reg = cmsis_dap_swd_write_reg,
1798         .run = cmsis_dap_swd_run_queue,
1799 };
1800
1801 static const char * const cmsis_dap_transport[] = { "swd", "jtag", NULL };
1802
1803 static struct jtag_interface cmsis_dap_interface = {
1804         .supported = DEBUG_CAP_TMS_SEQ,
1805         .execute_queue = cmsis_dap_execute_queue,
1806 };
1807
1808 struct adapter_driver cmsis_dap_adapter_driver = {
1809         .name = "cmsis-dap",
1810         .transports = cmsis_dap_transport,
1811         .commands = cmsis_dap_command_handlers,
1812
1813         .init = cmsis_dap_init,
1814         .quit = cmsis_dap_quit,
1815         .reset = cmsis_dap_reset,
1816         .speed = cmsis_dap_speed,
1817         .khz = cmsis_dap_khz,
1818         .speed_div = cmsis_dap_speed_div,
1819
1820         .jtag_ops = &cmsis_dap_interface,
1821         .swd_ops = &cmsis_dap_swd_driver,
1822 };