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