jtag/drivers/cmsis_dap: fix check for hardcoded vids/pids
[fw/openocd] / src / jtag / drivers / cmsis_dap_usb.c
1 /***************************************************************************
2  *   Copyright (C) 2013 by mike brown                                      *
3  *   mike@theshedworks.org.uk                                              *
4  *                                                                         *
5  *   Copyright (C) 2013 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
22  ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <transport/transport.h>
29 #include <jtag/swd.h>
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
32 #include <jtag/tcl.h>
33
34 #include <hidapi.h>
35
36 #ifdef _DEBUG_JTAG_IO_
37  #define DEBUG_IO(expr...) LOG_DEBUG(expr)
38 #else
39  #define DEBUG_IO(expr...) do {} while (0)
40 #endif
41
42 /*
43  * See CMSIS-DAP documentation:
44  * Version 0.01 - Beta.
45  */
46
47 /* USB Config */
48
49 /* Known vid/pid pairs:
50  * VID 0xc251: Keil Software
51  * PID 0xf001: LPC-Link-II CMSIS_DAP
52  * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
53  * PID 0x2722: Keil ULINK2 CMSIS-DAP
54  *
55  * VID 0x0d28: mbed Software
56  * PID 0x0204: MBED CMSIS-DAP
57  */
58
59 #define MAX_USB_IDS 8
60 /* vid = pid = 0 marks the end of the list */
61 static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
62 static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
63
64 #define PACKET_SIZE       (64 + 1)      /* 64 bytes plus report id */
65 #define USB_TIMEOUT       1000
66
67 /* CMSIS-DAP General Commands */
68 #define CMD_DAP_INFO              0x00
69 #define CMD_DAP_LED               0x01
70 #define CMD_DAP_CONNECT           0x02
71 #define CMD_DAP_DISCONNECT        0x03
72 #define CMD_DAP_WRITE_ABORT       0x08
73 #define CMD_DAP_DELAY             0x09
74 #define CMD_DAP_RESET_TARGET      0x0A
75
76 /* CMD_INFO */
77 #define INFO_ID_VID               0x00      /* string */
78 #define INFO_ID_PID               0x02      /* string */
79 #define INFO_ID_SERNUM            0x03      /* string */
80 #define INFO_ID_FW_VER            0x04      /* string */
81 #define INFO_ID_TD_VEND           0x05      /* string */
82 #define INFO_ID_TD_NAME           0x06      /* string */
83 #define INFO_ID_CAPS              0xf0      /* byte */
84 #define INFO_ID_PKT_CNT           0xfe      /* byte */
85 #define INFO_ID_PKT_SZ            0xff      /* short */
86
87 #define INFO_CAPS_SWD             0x01
88 #define INFO_CAPS_JTAG            0x02
89
90 /* CMD_LED */
91 #define LED_ID_CONNECT            0x00
92 #define LED_ID_RUN                0x01
93
94 #define LED_OFF                   0x00
95 #define LED_ON                    0x01
96
97 /* CMD_CONNECT */
98 #define CONNECT_DEFAULT           0x00
99 #define CONNECT_SWD               0x01
100 #define CONNECT_JTAG              0x02
101
102 /* CMSIS-DAP Common SWD/JTAG Commands */
103 #define CMD_DAP_DELAY             0x09
104 #define CMD_DAP_SWJ_PINS          0x10
105 #define CMD_DAP_SWJ_CLOCK         0x11
106 #define CMD_DAP_SWJ_SEQ           0x12
107
108 /*
109  * PINS
110  * Bit 0: SWCLK/TCK
111  * Bit 1: SWDIO/TMS
112  * Bit 2: TDI
113  * Bit 3: TDO
114  * Bit 5: nTRST
115  * Bit 7: nRESET
116  */
117
118 /* CMSIS-DAP SWD Commands */
119 #define CMD_DAP_SWD_CONFIGURE     0x13
120
121 /* CMSIS-DAP JTAG Commands */
122 #define CMD_DAP_JTAG_SEQ          0x14
123 #define CMD_DAP_JTAG_CONFIGURE    0x15
124 #define CMD_DAP_JTAG_IDCODE       0x16
125
126 /* CMSIS-DAP Transfer Commands */
127 #define CMD_DAP_TFER_CONFIGURE    0x04
128 #define CMD_DAP_TFER              0x05
129 #define CMD_DAP_TFER_BLOCK        0x06
130 #define CMD_DAP_TFER_ABORT        0x07
131
132 /* DAP Status Code */
133 #define DAP_OK                    0
134 #define DAP_ERROR                 0xFF
135
136 /* CMSIS-DAP Vendor Commands
137  * None as yet... */
138
139 static char *info_caps_str[] = {
140         "SWD  Supported",
141         "JTAG Supported"
142 };
143
144 /* max clock speed (kHz) */
145 #define DAP_MAX_CLOCK             5000
146
147 struct cmsis_dap {
148         hid_device *dev_handle;
149         uint16_t packet_size;
150         uint16_t packet_count;
151         uint8_t *packet_buffer;
152         uint8_t caps;
153         uint8_t mode;
154 };
155
156 static struct cmsis_dap *cmsis_dap_handle;
157
158 static int cmsis_dap_usb_open(void)
159 {
160         hid_device *dev = NULL;
161         int i;
162         struct hid_device_info *devs, *cur_dev;
163         unsigned short target_vid, target_pid;
164
165         target_vid = 0;
166         target_pid = 0;
167
168         /*
169         The CMSIS-DAP specification stipulates:
170         "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
171         debuggers to idenify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
172         */
173         devs = hid_enumerate(0x0, 0x0);
174         cur_dev = devs;
175         while (NULL != cur_dev) {
176                 if (0 == cmsis_dap_vid[0]) {
177                         if (NULL == cur_dev->product_string) {
178                                 LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
179                                           cur_dev->vendor_id, cur_dev->product_id);
180                         } else {
181                                 if (wcsstr(cur_dev->product_string, L"CMSIS-DAP"))
182                                         /*
183                                         if the user hasn't specified VID:PID *and*
184                                         product string contains "CMSIS-DAP", pick it
185                                         */
186                                         break;
187                         }
188                 } else {
189                         /*
190                         otherwise, exhaustively compare against all VID:PID in list
191                         */
192                         for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
193                                 if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
194                                         break;
195                         }
196                         if (cmsis_dap_vid[i] || cmsis_dap_pid[i])
197                                 break;
198                 }
199
200                 cur_dev = cur_dev->next;
201         }
202
203         if (NULL != cur_dev) {
204                 target_vid = cur_dev->vendor_id;
205                 target_pid = cur_dev->product_id;
206         }
207
208         hid_free_enumeration(devs);
209
210         if (hid_init() != 0) {
211                 LOG_ERROR("unable to open HIDAPI");
212                 return ERROR_FAIL;
213         }
214
215         dev = hid_open(target_vid, target_pid, NULL);
216
217         if (dev == NULL) {
218                 LOG_ERROR("unable to open CMSIS-DAP device");
219                 return ERROR_FAIL;
220         }
221
222         struct cmsis_dap *dap = malloc(sizeof(struct cmsis_dap));
223         if (dap == NULL) {
224                 LOG_ERROR("unable to allocate memory");
225                 return ERROR_FAIL;
226         }
227
228         dap->dev_handle = dev;
229         dap->caps = 0;
230         dap->mode = 0;
231
232         cmsis_dap_handle = dap;
233
234         /* allocate default packet buffer, may be changed later.
235          * currently with HIDAPI we have no way of getting the output report length
236          * without this info we cannot communicate with the adapter.
237          * For the moment we ahve to hard code the packet size */
238
239         int packet_size = PACKET_SIZE;
240
241         /* atmel cmsis-dap uses 512 byte reports */
242         if (target_vid == 0x03eb)
243                 packet_size = 512 + 1;
244
245         cmsis_dap_handle->packet_buffer = malloc(packet_size);
246         cmsis_dap_handle->packet_size = packet_size;
247
248         if (cmsis_dap_handle->packet_buffer == NULL) {
249                 LOG_ERROR("unable to allocate memory");
250                 return ERROR_FAIL;
251         }
252
253         return ERROR_OK;
254 }
255
256 static void cmsis_dap_usb_close(struct cmsis_dap *dap)
257 {
258         hid_close(dap->dev_handle);
259         hid_exit();
260
261         if (cmsis_dap_handle->packet_buffer)
262                 free(cmsis_dap_handle->packet_buffer);
263
264         if (cmsis_dap_handle) {
265                 free(cmsis_dap_handle);
266                 cmsis_dap_handle = NULL;
267         }
268
269         return;
270 }
271
272 /* Send a message and receive the reply */
273 static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
274 {
275         /* Pad the rest of the TX buffer with 0's */
276         memset(dap->packet_buffer + txlen, 0, dap->packet_size - 1 - txlen);
277
278         /* write data to device */
279         int retval = hid_write(dap->dev_handle, dap->packet_buffer, dap->packet_size);
280         if (retval == -1) {
281                 LOG_ERROR("error writing data: %ls", hid_error(dap->dev_handle));
282                 return ERROR_FAIL;
283         }
284
285         /* get reply */
286         retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, USB_TIMEOUT);
287         if (retval == -1 || retval == 0) {
288                 LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
289                 return ERROR_FAIL;
290         }
291
292         return ERROR_OK;
293 }
294
295 static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
296 {
297         int retval;
298         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
299
300         buffer[0] = 0;  /* report number */
301         buffer[1] = CMD_DAP_SWJ_PINS;
302         buffer[2] = pins;
303         buffer[3] = mask;
304         buffer[4] = delay & 0xff;
305         buffer[5] = (delay >> 8) & 0xff;
306         buffer[6] = (delay >> 16) & 0xff;
307         buffer[7] = (delay >> 24) & 0xff;
308         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
309
310         if (retval != ERROR_OK) {
311                 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
312                 return ERROR_JTAG_DEVICE_ERROR;
313         }
314
315         if (input)
316                 *input = buffer[1];
317
318         return ERROR_OK;
319 }
320
321 static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
322 {
323         int retval;
324         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
325
326         /* set clock in Hz */
327         swj_clock *= 1000;
328         buffer[0] = 0;  /* report number */
329         buffer[1] = CMD_DAP_SWJ_CLOCK;
330         buffer[2] = swj_clock & 0xff;
331         buffer[3] = (swj_clock >> 8) & 0xff;
332         buffer[4] = (swj_clock >> 16) & 0xff;
333         buffer[5] = (swj_clock >> 24) & 0xff;
334         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
335
336         if (retval != ERROR_OK || buffer[1] != DAP_OK) {
337                 LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
338                 return ERROR_JTAG_DEVICE_ERROR;
339         }
340
341         return ERROR_OK;
342 }
343
344 static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
345 {
346         int retval;
347         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
348
349         buffer[0] = 0;  /* report number */
350         buffer[1] = CMD_DAP_INFO;
351         buffer[2] = info;
352         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
353
354         if (retval != ERROR_OK) {
355                 LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
356                 return ERROR_JTAG_DEVICE_ERROR;
357         }
358
359         *data = &(buffer[1]);
360
361         return ERROR_OK;
362 }
363
364 static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
365 {
366         int retval;
367         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
368
369         buffer[0] = 0;  /* report number */
370         buffer[1] = CMD_DAP_LED;
371         buffer[2] = 0x00;
372         buffer[3] = leds;
373         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
374
375         if (retval != ERROR_OK || buffer[1] != 0x00) {
376                 LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
377                 return ERROR_JTAG_DEVICE_ERROR;
378         }
379
380         return ERROR_OK;
381 }
382
383 static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
384 {
385         int retval;
386         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
387
388         buffer[0] = 0;  /* report number */
389         buffer[1] = CMD_DAP_CONNECT;
390         buffer[2] = mode;
391         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
392
393         if (retval != ERROR_OK) {
394                 LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
395                 return ERROR_JTAG_DEVICE_ERROR;
396         }
397
398         if (buffer[1] != mode) {
399                 LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
400                 return ERROR_JTAG_DEVICE_ERROR;
401         }
402
403         return ERROR_OK;
404 }
405
406 static int cmsis_dap_cmd_DAP_Disconnect(void)
407 {
408         int retval;
409         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
410
411         buffer[0] = 0;  /* report number */
412         buffer[1] = CMD_DAP_DISCONNECT;
413         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
414
415         if (retval != ERROR_OK || buffer[1] != DAP_OK) {
416                 LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
417                 return ERROR_JTAG_DEVICE_ERROR;
418         }
419
420         return ERROR_OK;
421 }
422
423 static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t delay, uint16_t retry)
424 {
425         int retval;
426         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
427
428         buffer[0] = 0;  /* report number */
429         buffer[1] = CMD_DAP_TFER_CONFIGURE;
430         buffer[2] = idle;
431         buffer[3] = delay & 0xff;
432         buffer[4] = (delay >> 8) & 0xff;
433         buffer[5] = retry & 0xff;
434         buffer[6] = (retry >> 8) & 0xff;
435         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
436
437         if (retval != ERROR_OK || buffer[1] != DAP_OK) {
438                 LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
439                 return ERROR_JTAG_DEVICE_ERROR;
440         }
441
442         return ERROR_OK;
443 }
444
445 static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
446 {
447         int retval;
448         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
449
450         buffer[0] = 0;  /* report number */
451         buffer[1] = CMD_DAP_SWD_CONFIGURE;
452         buffer[2] = cfg;
453         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
454
455         if (retval != ERROR_OK || buffer[1] != DAP_OK) {
456                 LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
457                 return ERROR_JTAG_DEVICE_ERROR;
458         }
459
460         return ERROR_OK;
461 }
462
463 #if 0
464 static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
465 {
466         int retval;
467         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
468
469         buffer[0] = 0;  /* report number */
470         buffer[1] = CMD_DAP_DELAY;
471         buffer[2] = delay_us & 0xff;
472         buffer[3] = (delay_us >> 8) & 0xff;
473         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
474
475         if (retval != ERROR_OK || buffer[1] != DAP_OK) {
476                 LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
477                 return ERROR_JTAG_DEVICE_ERROR;
478         }
479
480         return ERROR_OK;
481 }
482 #endif
483
484 static int queued_retval;
485
486 static void cmsis_dap_swd_read_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t *value)
487 {
488         if (queued_retval != ERROR_OK)
489                 return;
490
491         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
492         int retval;
493         uint32_t val;
494
495         DEBUG_IO("CMSIS-DAP: Read  Reg 0x%02" PRIx8, cmd);
496
497         buffer[0] = 0;  /* report number */
498         buffer[1] = CMD_DAP_TFER;
499         buffer[2] = 0x00;
500         buffer[3] = 0x01;
501         buffer[4] = cmd;
502         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
503
504         /* TODO - need better response checking */
505         if (retval != ERROR_OK || buffer[1] != 0x01) {
506                 LOG_ERROR("CMSIS-DAP: Read Error (0x%02" PRIx8 ")", buffer[2]);
507                 queued_retval = buffer[2];
508                 return;
509         }
510
511         val = le_to_h_u32(&buffer[3]);
512         DEBUG_IO("0x%08" PRIx32, val);
513
514         if (value)
515                 *value = val;
516
517         queued_retval = retval;
518 }
519
520 static void cmsis_dap_swd_write_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t value)
521 {
522         if (queued_retval != ERROR_OK)
523                 return;
524
525         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
526
527         DEBUG_IO("CMSIS-DAP: Write Reg 0x%02" PRIx8 " 0x%08" PRIx32, cmd, value);
528
529         buffer[0] = 0;  /* report number */
530         buffer[1] = CMD_DAP_TFER;
531         buffer[2] = 0x00;
532         buffer[3] = 0x01;
533         buffer[4] = cmd;
534         buffer[5] = (value) & 0xff;
535         buffer[6] = (value >> 8) & 0xff;
536         buffer[7] = (value >> 16) & 0xff;
537         buffer[8] = (value >> 24) & 0xff;
538         int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 9);
539
540         if (buffer[1] != 0x01) {
541                 LOG_ERROR("CMSIS-DAP: Write Error (0x%02" PRIx8 ")", buffer[2]);
542                 retval = buffer[2];
543         }
544
545         queued_retval = retval;
546 }
547
548 static int cmsis_dap_swd_run(struct adiv5_dap *dap)
549 {
550         int retval = queued_retval;
551         queued_retval = ERROR_OK;
552         return retval;
553 }
554
555 static int cmsis_dap_get_version_info(void)
556 {
557         uint8_t *data;
558
559         /* INFO_ID_FW_VER - string */
560         int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
561         if (retval != ERROR_OK)
562                 return retval;
563
564         if (data[0]) /* strlen */
565                 LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
566
567         return ERROR_OK;
568 }
569
570 static int cmsis_dap_get_caps_info(void)
571 {
572         uint8_t *data;
573
574         /* INFO_ID_CAPS - byte */
575         int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
576         if (retval != ERROR_OK)
577                 return retval;
578
579         if (data[0] == 1) {
580                 uint8_t caps = data[1];
581
582                 cmsis_dap_handle->caps = caps;
583
584                 if (caps & INFO_CAPS_SWD)
585                         LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
586                 if (caps & INFO_CAPS_JTAG)
587                         LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
588         }
589
590         return ERROR_OK;
591 }
592
593 static int cmsis_dap_get_status(void)
594 {
595         uint8_t d;
596
597         int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
598
599         if (retval == ERROR_OK) {
600                 LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
601                         (d & (0x01 << 0)) ? 1 : 0,      /* Bit 0: SWCLK/TCK */
602                         (d & (0x01 << 1)) ? 1 : 0,      /* Bit 1: SWDIO/TMS */
603                         (d & (0x01 << 2)) ? 1 : 0,      /* Bit 2: TDI */
604                         (d & (0x01 << 3)) ? 1 : 0,      /* Bit 3: TDO */
605                         (d & (0x01 << 5)) ? 1 : 0,      /* Bit 5: nTRST */
606                         (d & (0x01 << 7)) ? 1 : 0);     /* Bit 7: nRESET */
607         }
608
609         return retval;
610 }
611
612 static int cmsis_dap_reset_link(void)
613 {
614         uint8_t *buffer = cmsis_dap_handle->packet_buffer;
615
616         LOG_DEBUG("CMSIS-DAP: cmsis_dap_reset_link");
617         LOG_INFO("DAP_SWJ Sequence (reset: 50+ '1' followed by 0)");
618
619         /* reset line with SWDIO high for >50 cycles */
620         buffer[0] = 0;  /* report number */
621         buffer[1] = CMD_DAP_SWJ_SEQ;
622         buffer[2] = 7 * 8;
623         buffer[3] = 0xff;
624         buffer[4] = 0xff;
625         buffer[5] = 0xff;
626         buffer[6] = 0xff;
627         buffer[7] = 0xff;
628         buffer[8] = 0xff;
629         buffer[9] = 0xff;
630         int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
631
632         if (retval != ERROR_OK || buffer[1] != DAP_OK)
633                 return ERROR_FAIL;
634
635         /* 16bit JTAG-SWD sequence */
636         buffer[0] = 0;  /* report number */
637         buffer[1] = CMD_DAP_SWJ_SEQ;
638         buffer[2] = 2 * 8;
639         buffer[3] = 0x9e;
640         buffer[4] = 0xe7;
641         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
642
643         if (retval != ERROR_OK || buffer[1] != DAP_OK)
644                 return ERROR_FAIL;
645
646         /* another reset just incase */
647         buffer[0] = 0;  /* report number */
648         buffer[1] = CMD_DAP_SWJ_SEQ;
649         buffer[2] = 7 * 8;
650         buffer[3] = 0xff;
651         buffer[4] = 0xff;
652         buffer[5] = 0xff;
653         buffer[6] = 0xff;
654         buffer[7] = 0xff;
655         buffer[8] = 0xff;
656         buffer[9] = 0xff;
657         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
658
659         if (retval != ERROR_OK || buffer[1] != DAP_OK)
660                 return ERROR_FAIL;
661
662         /* 16 cycle idle period */
663         buffer[0] = 0;  /* report number */
664         buffer[1] = CMD_DAP_SWJ_SEQ;
665         buffer[2] = 2 * 8;
666         buffer[3] = 0x00;
667         buffer[4] = 0x00;
668         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
669
670         if (retval != ERROR_OK || buffer[1] != DAP_OK)
671                 return ERROR_FAIL;
672
673         DEBUG_IO("DAP Read IDCODE");
674
675         /* read the id code is always the next sequence */
676         buffer[0] = 0;  /* report number */
677         buffer[1] = CMD_DAP_TFER;
678         buffer[2] = 0x00;
679         buffer[3] = 0x01;
680         buffer[4] = 0x02;
681         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
682
683         if (retval != ERROR_OK)
684                 return retval;
685
686         if (buffer[1] == 0) {
687                 LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
688
689                 LOG_DEBUG("DAP Reset Target");
690                 buffer[0] = 0;  /* report number */
691                 buffer[1] = CMD_DAP_RESET_TARGET;
692                 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
693                 LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
694
695                 LOG_DEBUG("DAP Write Abort");
696                 buffer[0] = 0;  /* report number */
697                 buffer[1] = CMD_DAP_WRITE_ABORT;
698                 buffer[2] = 0x00;
699                 buffer[3] = 0x1e/*0x1f*/;
700                 buffer[4] = 0x00;
701                 buffer[5] = 0x00;
702                 buffer[6] = 0x00;
703                 retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
704                 LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
705
706                 return 0x80 + buffer[1];
707         }
708
709         LOG_DEBUG("DAP Write Abort");
710         buffer[0] = 0;  /* report number */
711         buffer[1] = CMD_DAP_WRITE_ABORT;
712         buffer[2] = 0x00;
713         buffer[3] = 0x1e;
714         buffer[4] = 0x00;
715         buffer[5] = 0x00;
716         buffer[6] = 0x00;
717         retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
718         LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
719
720         return retval;
721 }
722
723 static int cmsis_dap_init(void)
724 {
725         int retval;
726         uint8_t *data;
727
728         if (cmsis_dap_handle == NULL) {
729
730                 /* JTAG init */
731                 retval = cmsis_dap_usb_open();
732                 if (retval != ERROR_OK)
733                         return retval;
734
735                 retval = cmsis_dap_get_caps_info();
736                 if (retval != ERROR_OK)
737                         return retval;
738
739                 /* Connect in JTAG mode */
740                 if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
741                         LOG_ERROR("CMSIS-DAP: JTAG not supported");
742                         return ERROR_JTAG_DEVICE_ERROR;
743                 }
744
745                 retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
746                 if (retval != ERROR_OK)
747                         return retval;
748
749                 LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
750         }
751
752         retval = cmsis_dap_get_version_info();
753         if (retval != ERROR_OK)
754                 return retval;
755
756         /* INFO_ID_PKT_SZ - short */
757         retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
758         if (retval != ERROR_OK)
759                 return retval;
760
761         if (data[0] == 2) {  /* short */
762                 uint16_t pkt_sz = data[1] + (data[2] << 8);
763
764                 if (cmsis_dap_handle->packet_size != pkt_sz + 1) {
765                         /* reallocate buffer */
766                         cmsis_dap_handle->packet_size = pkt_sz + 1;
767                         cmsis_dap_handle->packet_buffer = realloc(cmsis_dap_handle->packet_buffer,
768                                         cmsis_dap_handle->packet_size);
769                         if (cmsis_dap_handle->packet_buffer == NULL) {
770                                 LOG_ERROR("unable to reallocate memory");
771                                 return ERROR_FAIL;
772                         }
773                 }
774
775                 LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
776         }
777
778         /* INFO_ID_PKT_CNT - byte */
779         retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
780         if (retval != ERROR_OK)
781                 return retval;
782
783         if (data[0] == 1) { /* byte */
784                 uint16_t pkt_cnt = data[1];
785                 cmsis_dap_handle->packet_count = pkt_cnt;
786                 LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16, pkt_cnt);
787         }
788
789         retval = cmsis_dap_get_status();
790         if (retval != ERROR_OK)
791                 return ERROR_FAIL;
792
793         /* Now try to connect to the target
794          * TODO: This is all SWD only @ present */
795         retval = cmsis_dap_cmd_DAP_SWJ_Clock(100);              /* 100kHz */
796         if (retval != ERROR_OK)
797                 return ERROR_FAIL;
798
799         retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
800         if (retval != ERROR_OK)
801                 return ERROR_FAIL;
802         retval = cmsis_dap_cmd_DAP_SWD_Configure(0x00);
803         if (retval != ERROR_OK)
804                 return ERROR_FAIL;
805
806         retval = cmsis_dap_cmd_DAP_LED(0x03);           /* Both LEDs on */
807         if (retval != ERROR_OK)
808                 return ERROR_FAIL;
809
810         /* support connecting with srst asserted */
811         enum reset_types jtag_reset_config = jtag_get_reset_config();
812
813         if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
814                 if (jtag_reset_config & RESET_SRST_NO_GATING) {
815                         retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL);
816                         if (retval != ERROR_OK)
817                                 return ERROR_FAIL;
818                         LOG_INFO("Connecting under reset");
819                 }
820         }
821
822         retval = cmsis_dap_reset_link();
823         if (retval != ERROR_OK)
824                 return ERROR_FAIL;
825
826         cmsis_dap_cmd_DAP_LED(0x00);                            /* Both LEDs off */
827
828         LOG_INFO("CMSIS-DAP: Interface ready");
829
830         return ERROR_OK;
831 }
832
833 static int cmsis_dap_swd_init(void)
834 {
835         int retval;
836
837         DEBUG_IO("CMSIS-DAP: cmsis_dap_swd_init");
838
839         if (cmsis_dap_handle == NULL) {
840
841                 /* SWD init */
842                 retval = cmsis_dap_usb_open();
843                 if (retval != ERROR_OK)
844                         return retval;
845
846                 retval = cmsis_dap_get_caps_info();
847                 if (retval != ERROR_OK)
848                         return retval;
849         }
850
851         if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
852                 LOG_ERROR("CMSIS-DAP: SWD not supported");
853                 return ERROR_JTAG_DEVICE_ERROR;
854         }
855
856         retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
857         if (retval != ERROR_OK)
858                 return retval;
859
860         /* Add more setup here.??... */
861
862         LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
863         return ERROR_OK;
864 }
865
866 static int cmsis_dap_quit(void)
867 {
868         cmsis_dap_cmd_DAP_Disconnect();
869         cmsis_dap_cmd_DAP_LED(0x00);            /* Both LEDs off */
870
871         cmsis_dap_usb_close(cmsis_dap_handle);
872
873         return ERROR_OK;
874 }
875
876 static void cmsis_dap_execute_reset(struct jtag_command *cmd)
877 {
878         int retval = cmsis_dap_cmd_DAP_SWJ_Pins(cmd->cmd.reset->srst ? 0 : (1 << 7), \
879                         (1 << 7), 0, NULL);
880         if (retval != ERROR_OK)
881                 LOG_ERROR("CMSIS-DAP: Interface reset failed");
882 }
883
884 static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
885 {
886 #if 0
887         int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
888         if (retval != ERROR_OK)
889 #endif
890                 jtag_sleep(cmd->cmd.sleep->us);
891 }
892
893 static void cmsis_dap_execute_command(struct jtag_command *cmd)
894 {
895         switch (cmd->type) {
896                 case JTAG_RESET:
897                         cmsis_dap_execute_reset(cmd);
898                         break;
899                 case JTAG_SLEEP:
900                         cmsis_dap_execute_sleep(cmd);
901                         break;
902                 default:
903                         LOG_ERROR("BUG: unknown JTAG command type encountered");
904                         exit(-1);
905         }
906 }
907
908 static int cmsis_dap_execute_queue(void)
909 {
910         struct jtag_command *cmd = jtag_command_queue;
911
912         while (cmd != NULL) {
913                 cmsis_dap_execute_command(cmd);
914                 cmd = cmd->next;
915         }
916
917         return ERROR_OK;
918 }
919
920 static int cmsis_dap_speed(int speed)
921 {
922         if (speed > DAP_MAX_CLOCK) {
923                 LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed, DAP_MAX_CLOCK);
924                 speed = DAP_MAX_CLOCK;
925         }
926
927         if (speed == 0) {
928                 LOG_INFO("RTCK not supported");
929                 return ERROR_JTAG_NOT_IMPLEMENTED;
930         }
931
932         return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
933 }
934
935 static int cmsis_dap_speed_div(int speed, int *khz)
936 {
937         *khz = speed;
938         return ERROR_OK;
939 }
940
941 static int cmsis_dap_khz(int khz, int *jtag_speed)
942 {
943         *jtag_speed = khz;
944         return ERROR_OK;
945 }
946
947 COMMAND_HANDLER(cmsis_dap_handle_info_command)
948 {
949         if (cmsis_dap_get_version_info() == ERROR_OK)
950                 cmsis_dap_get_status();
951
952         return ERROR_OK;
953 }
954
955 COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
956 {
957         if (CMD_ARGC > MAX_USB_IDS * 2) {
958                 LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
959                         "(maximum is %d pairs)", MAX_USB_IDS);
960                 CMD_ARGC = MAX_USB_IDS * 2;
961         }
962         if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
963                 LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
964                 if (CMD_ARGC < 2)
965                         return ERROR_COMMAND_SYNTAX_ERROR;
966                 /* remove the incomplete trailing id */
967                 CMD_ARGC -= 1;
968         }
969
970         unsigned i;
971         for (i = 0; i < CMD_ARGC; i += 2) {
972                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
973                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
974         }
975
976         /*
977          * Explicitly terminate, in case there are multiples instances of
978          * cmsis_dap_vid_pid.
979          */
980         cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
981
982         return ERROR_OK;
983 }
984
985 static const struct command_registration cmsis_dap_subcommand_handlers[] = {
986         {
987                 .name = "info",
988                 .handler = &cmsis_dap_handle_info_command,
989                 .mode = COMMAND_EXEC,
990                 .usage = "",
991                 .help = "show cmsis-dap info",
992         },
993         COMMAND_REGISTRATION_DONE
994 };
995
996 static const struct command_registration cmsis_dap_command_handlers[] = {
997         {
998                 .name = "cmsis-dap",
999                 .mode = COMMAND_ANY,
1000                 .help = "perform CMSIS-DAP management",
1001                 .usage = "<cmd>",
1002                 .chain = cmsis_dap_subcommand_handlers,
1003         },
1004         {
1005                 .name = "cmsis_dap_vid_pid",
1006                 .handler = &cmsis_dap_handle_vid_pid_command,
1007                 .mode = COMMAND_CONFIG,
1008                 .help = "the vendor ID and product ID of the CMSIS-DAP device",
1009                 .usage = "(vid pid)* ",
1010         },
1011         COMMAND_REGISTRATION_DONE
1012 };
1013
1014 static const struct swd_driver cmsis_dap_swd_driver = {
1015         .init = cmsis_dap_swd_init,
1016         .read_reg = cmsis_dap_swd_read_reg,
1017         .write_reg = cmsis_dap_swd_write_reg,
1018         .run = cmsis_dap_swd_run,
1019 };
1020
1021 const char *cmsis_dap_transport[] = {"cmsis-dap", NULL};
1022
1023 struct jtag_interface cmsis_dap_interface = {
1024         .name = "cmsis-dap",
1025         .commands = cmsis_dap_command_handlers,
1026         .swd = &cmsis_dap_swd_driver,
1027         .transports = cmsis_dap_transport,
1028
1029         .execute_queue = cmsis_dap_execute_queue,
1030         .speed = cmsis_dap_speed,
1031         .speed_div = cmsis_dap_speed_div,
1032         .khz = cmsis_dap_khz,
1033         .init = cmsis_dap_init,
1034         .quit = cmsis_dap_quit,
1035 };