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