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