]> git.sur5r.net Git - openocd/blob - src/jtag/drivers/stlink_usb.c
hla: add ability to configure read/write buffer size
[openocd] / src / jtag / drivers / stlink_usb.c
1 /***************************************************************************
2  *   Copyright (C) 2011-2012 by Mathias Kuester                            *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   Copyright (C) 2012 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
8  *   This code is based on https://github.com/texane/stlink                *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc.,                                       *
23  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  ***************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 /* project specific includes */
31 #include <helper/binarybuffer.h>
32 #include <jtag/interface.h>
33 #include <jtag/hla/hla_layout.h>
34 #include <jtag/hla/hla_transport.h>
35 #include <jtag/hla/hla_interface.h>
36 #include <target/target.h>
37
38 #include <target/cortex_m.h>
39
40 #include "libusb_common.h"
41
42 #define ENDPOINT_IN  0x80
43 #define ENDPOINT_OUT 0x00
44
45 #define STLINK_NULL_EP     0
46 #define STLINK_RX_EP       (1|ENDPOINT_IN)
47 #define STLINK_TX_EP       (2|ENDPOINT_OUT)
48 #define STLINK_SG_SIZE     (31)
49 #define STLINK_DATA_SIZE   (4*128)
50 #define STLINK_CMD_SIZE_V2 (16)
51 #define STLINK_CMD_SIZE_V1 (10)
52
53 enum stlink_jtag_api_version {
54         STLINK_JTAG_API_V1 = 1,
55         STLINK_JTAG_API_V2,
56 };
57
58 /** */
59 struct stlink_usb_version {
60         /** */
61         int stlink;
62         /** */
63         int jtag;
64         /** */
65         int swim;
66         /** highest supported jtag api version */
67         enum stlink_jtag_api_version jtag_api_max;
68 };
69
70 /** */
71 struct stlink_usb_handle_s {
72         /** */
73         struct jtag_libusb_device_handle *fd;
74         /** */
75         struct libusb_transfer *trans;
76         /** */
77         uint8_t cmdbuf[STLINK_SG_SIZE];
78         /** */
79         uint8_t cmdidx;
80         /** */
81         uint8_t direction;
82         /** */
83         uint8_t databuf[STLINK_DATA_SIZE];
84         /** */
85         enum hl_transports transport;
86         /** */
87         struct stlink_usb_version version;
88         /** */
89         uint16_t vid;
90         /** */
91         uint16_t pid;
92         /** this is the currently used jtag api */
93         enum stlink_jtag_api_version jtag_api;
94 };
95
96 #define STLINK_DEBUG_ERR_OK            0x80
97 #define STLINK_DEBUG_ERR_FAULT         0x81
98 #define STLINK_SWD_AP_WAIT             0x10
99 #define STLINK_SWD_DP_WAIT             0x14
100
101 #define STLINK_CORE_RUNNING            0x80
102 #define STLINK_CORE_HALTED             0x81
103 #define STLINK_CORE_STAT_UNKNOWN       -1
104
105 #define STLINK_GET_VERSION             0xF1
106 #define STLINK_DEBUG_COMMAND           0xF2
107 #define STLINK_DFU_COMMAND             0xF3
108 #define STLINK_SWIM_COMMAND            0xF4
109 #define STLINK_GET_CURRENT_MODE        0xF5
110
111 #define STLINK_DEV_DFU_MODE            0x00
112 #define STLINK_DEV_MASS_MODE           0x01
113 #define STLINK_DEV_DEBUG_MODE          0x02
114 #define STLINK_DEV_SWIM_MODE           0x03
115 #define STLINK_DEV_BOOTLOADER_MODE     0x04
116 #define STLINK_DEV_UNKNOWN_MODE        -1
117
118 #define STLINK_DFU_EXIT                0x07
119
120 #define STLINK_SWIM_ENTER              0x00
121 #define STLINK_SWIM_EXIT               0x01
122
123 #define STLINK_DEBUG_ENTER_JTAG            0x00
124 #define STLINK_DEBUG_GETSTATUS             0x01
125 #define STLINK_DEBUG_FORCEDEBUG            0x02
126 #define STLINK_DEBUG_APIV1_RESETSYS        0x03
127 #define STLINK_DEBUG_APIV1_READALLREGS     0x04
128 #define STLINK_DEBUG_APIV1_READREG         0x05
129 #define STLINK_DEBUG_APIV1_WRITEREG        0x06
130 #define STLINK_DEBUG_READMEM_32BIT         0x07
131 #define STLINK_DEBUG_WRITEMEM_32BIT        0x08
132 #define STLINK_DEBUG_RUNCORE               0x09
133 #define STLINK_DEBUG_STEPCORE              0x0a
134 #define STLINK_DEBUG_APIV1_SETFP           0x0b
135 #define STLINK_DEBUG_READMEM_8BIT          0x0c
136 #define STLINK_DEBUG_WRITEMEM_8BIT         0x0d
137 #define STLINK_DEBUG_APIV1_CLEARFP         0x0e
138 #define STLINK_DEBUG_APIV1_WRITEDEBUGREG   0x0f
139 #define STLINK_DEBUG_APIV1_SETWATCHPOINT   0x10
140
141 #define STLINK_DEBUG_ENTER_JTAG            0x00
142 #define STLINK_DEBUG_ENTER_SWD             0xa3
143
144 #define STLINK_DEBUG_APIV1_ENTER           0x20
145 #define STLINK_DEBUG_EXIT                  0x21
146 #define STLINK_DEBUG_READCOREID            0x22
147
148 #define STLINK_DEBUG_APIV2_ENTER           0x30
149 #define STLINK_DEBUG_APIV2_READ_IDCODES    0x31
150 #define STLINK_DEBUG_APIV2_RESETSYS        0x32
151 #define STLINK_DEBUG_APIV2_READREG         0x33
152 #define STLINK_DEBUG_APIV2_WRITEREG        0x34
153 #define STLINK_DEBUG_APIV2_WRITEDEBUGREG   0x35
154 #define STLINK_DEBUG_APIV2_READDEBUGREG    0x36
155
156 #define STLINK_DEBUG_APIV2_READALLREGS     0x3A
157 #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
158 #define STLINK_DEBUG_APIV2_DRIVE_NRST      0x3C
159
160 #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW   0x00
161 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH  0x01
162 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
163
164 /** */
165 enum stlink_mode {
166         STLINK_MODE_UNKNOWN = 0,
167         STLINK_MODE_DFU,
168         STLINK_MODE_MASS,
169         STLINK_MODE_DEBUG_JTAG,
170         STLINK_MODE_DEBUG_SWD,
171         STLINK_MODE_DEBUG_SWIM
172 };
173
174 #define REQUEST_SENSE        0x03
175 #define REQUEST_SENSE_LENGTH 18
176
177 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
178
179 /** */
180 static int stlink_usb_xfer_v1_get_status(void *handle)
181 {
182         struct stlink_usb_handle_s *h;
183
184         assert(handle != NULL);
185
186         h = (struct stlink_usb_handle_s *)handle;
187
188         /* read status */
189         memset(h->cmdbuf, 0, STLINK_SG_SIZE);
190
191         if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)h->cmdbuf,
192                                 13, 1000) != 13)
193                 return ERROR_FAIL;
194
195         uint32_t t1;
196
197         t1 = buf_get_u32(h->cmdbuf, 0, 32);
198
199         /* check for USBS */
200         if (t1 != 0x53425355)
201                 return ERROR_FAIL;
202         /*
203          * CSW status:
204          * 0 success
205          * 1 command failure
206          * 2 phase error
207          */
208         if (h->cmdbuf[12] != 0)
209                 return ERROR_FAIL;
210
211         return ERROR_OK;
212 }
213
214 /** */
215 static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
216 {
217         struct stlink_usb_handle_s *h;
218
219         assert(handle != NULL);
220
221         h = (struct stlink_usb_handle_s *)handle;
222
223         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)h->cmdbuf, cmdsize,
224                                    1000) != cmdsize) {
225                 return ERROR_FAIL;
226         }
227
228         if (h->direction == STLINK_TX_EP && size) {
229                 if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
230                                           size, 1000) != size) {
231                         LOG_DEBUG("bulk write failed");
232                         return ERROR_FAIL;
233                 }
234         } else if (h->direction == STLINK_RX_EP && size) {
235                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
236                                           size, 1000) != size) {
237                         LOG_DEBUG("bulk read failed");
238                         return ERROR_FAIL;
239                 }
240         }
241
242         return ERROR_OK;
243 }
244
245 /** */
246 static int stlink_usb_xfer_v1_get_sense(void *handle)
247 {
248         int res;
249         struct stlink_usb_handle_s *h;
250
251         assert(handle != NULL);
252
253         h = (struct stlink_usb_handle_s *)handle;
254
255         stlink_usb_init_buffer(handle, STLINK_RX_EP, 16);
256
257         h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
258         h->cmdbuf[h->cmdidx++] = 0;
259         h->cmdbuf[h->cmdidx++] = 0;
260         h->cmdbuf[h->cmdidx++] = 0;
261         h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
262
263         res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
264
265         if (res != ERROR_OK)
266                 return res;
267
268         if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
269                 return ERROR_FAIL;
270
271         return ERROR_OK;
272 }
273
274 /** */
275 static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
276 {
277         int err, cmdsize = STLINK_CMD_SIZE_V2;
278         struct stlink_usb_handle_s *h;
279
280         assert(handle != NULL);
281
282         h = (struct stlink_usb_handle_s *)handle;
283
284         if (h->version.stlink == 1)
285                 cmdsize = STLINK_SG_SIZE;
286
287         err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
288
289         if (err != ERROR_OK)
290                 return err;
291
292         if (h->version.stlink == 1) {
293                 if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
294                         /* check csw status */
295                         if (h->cmdbuf[12] == 1) {
296                                 LOG_DEBUG("get sense");
297                                 if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
298                                         return ERROR_FAIL;
299                         }
300                         return ERROR_FAIL;
301                 }
302         }
303
304         return ERROR_OK;
305 }
306
307 /** */
308 static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
309 {
310         struct stlink_usb_handle_s *h;
311
312         h = (struct stlink_usb_handle_s *)handle;
313
314         /* fill the send buffer */
315         strcpy((char *)h->cmdbuf, "USBC");
316         h->cmdidx += 4;
317         /* csw tag not used */
318         h->cmdidx += 4;
319         buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
320         h->cmdidx += 4;
321         h->cmdbuf[h->cmdidx++] = (direction == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
322         h->cmdbuf[h->cmdidx++] = 0; /* lun */
323         h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
324 }
325
326 /** */
327 static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
328 {
329         struct stlink_usb_handle_s *h;
330
331         h = (struct stlink_usb_handle_s *)handle;
332
333         h->direction = direction;
334
335         h->cmdidx = 0;
336
337         memset(h->cmdbuf, 0, STLINK_SG_SIZE);
338         memset(h->databuf, 0, STLINK_DATA_SIZE);
339
340         if (h->version.stlink == 1)
341                 stlink_usb_xfer_v1_create_cmd(handle, direction, size);
342 }
343
344 static const char * const stlink_usb_error_msg[] = {
345         "unknown"
346 };
347
348 /** */
349 static int stlink_usb_error_check(void *handle)
350 {
351         int res;
352         const char *err_msg = 0;
353         struct stlink_usb_handle_s *h;
354
355         assert(handle != NULL);
356
357         h = (struct stlink_usb_handle_s *)handle;
358
359         /* TODO: no error checking yet on api V1 */
360         if (h->jtag_api == STLINK_JTAG_API_V1)
361                 h->databuf[0] = STLINK_DEBUG_ERR_OK;
362
363         switch (h->databuf[0]) {
364                 case STLINK_DEBUG_ERR_OK:
365                         res = ERROR_OK;
366                         break;
367                 case STLINK_DEBUG_ERR_FAULT:
368                 default:
369                         err_msg = stlink_usb_error_msg[0];
370                         res = ERROR_FAIL;
371                         break;
372         }
373
374         if (res != ERROR_OK)
375                 LOG_DEBUG("status error: %d ('%s')", h->databuf[0], err_msg);
376
377         return res;
378 }
379
380 /** */
381 static int stlink_usb_version(void *handle)
382 {
383         int res;
384         uint16_t v;
385         struct stlink_usb_handle_s *h;
386
387         assert(handle != NULL);
388
389         h = (struct stlink_usb_handle_s *)handle;
390
391         stlink_usb_init_buffer(handle, STLINK_RX_EP, 6);
392
393         h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
394
395         res = stlink_usb_xfer(handle, h->databuf, 6);
396
397         if (res != ERROR_OK)
398                 return res;
399
400         v = (h->databuf[0] << 8) | h->databuf[1];
401
402         h->version.stlink = (v >> 12) & 0x0f;
403         h->version.jtag = (v >> 6) & 0x3f;
404         h->version.swim = v & 0x3f;
405         h->vid = buf_get_u32(h->databuf, 16, 16);
406         h->pid = buf_get_u32(h->databuf, 32, 16);
407
408         /* set the supported jtag api version
409          * API V2 is supported since JTAG V11
410          */
411         if (h->version.jtag >= 11)
412                 h->version.jtag_api_max = STLINK_JTAG_API_V2;
413         else
414                 h->version.jtag_api_max = STLINK_JTAG_API_V1;
415
416         LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
417                 h->version.stlink,
418                 h->version.jtag,
419                 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
420                 h->version.swim,
421                 h->vid,
422                 h->pid);
423
424         return ERROR_OK;
425 }
426
427 /** */
428 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
429 {
430         int res;
431         struct stlink_usb_handle_s *h;
432
433         assert(handle != NULL);
434
435         h = (struct stlink_usb_handle_s *)handle;
436
437         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
438
439         h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
440
441         res = stlink_usb_xfer(handle, h->databuf, 2);
442
443         if (res != ERROR_OK)
444                 return res;
445
446         *mode = h->databuf[0];
447
448         return ERROR_OK;
449 }
450
451 /** */
452 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
453 {
454         int res;
455         int rx_size = 0;
456         struct stlink_usb_handle_s *h;
457
458         assert(handle != NULL);
459
460         h = (struct stlink_usb_handle_s *)handle;
461
462         /* on api V2 we are able the read the latest command
463          * status
464          * TODO: we need the test on api V1 too
465          */
466         if (h->jtag_api == STLINK_JTAG_API_V2)
467                 rx_size = 2;
468
469         stlink_usb_init_buffer(handle, STLINK_RX_EP, rx_size);
470
471         switch (type) {
472                 case STLINK_MODE_DEBUG_JTAG:
473                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
474                         if (h->jtag_api == STLINK_JTAG_API_V1)
475                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
476                         else
477                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
478                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
479                         break;
480                 case STLINK_MODE_DEBUG_SWD:
481                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
482                         if (h->jtag_api == STLINK_JTAG_API_V1)
483                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
484                         else
485                                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
486                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
487                         break;
488                 case STLINK_MODE_DEBUG_SWIM:
489                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
490                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
491                         break;
492                 case STLINK_MODE_DFU:
493                 case STLINK_MODE_MASS:
494                 default:
495                         return ERROR_FAIL;
496         }
497
498         res = stlink_usb_xfer(handle, h->databuf, rx_size);
499
500         if (res != ERROR_OK)
501                 return res;
502
503         res = stlink_usb_error_check(h);
504
505         return res;
506 }
507
508 /** */
509 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
510 {
511         int res;
512         struct stlink_usb_handle_s *h;
513
514         assert(handle != NULL);
515
516         h = (struct stlink_usb_handle_s *)handle;
517
518         stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
519
520         switch (type) {
521                 case STLINK_MODE_DEBUG_JTAG:
522                 case STLINK_MODE_DEBUG_SWD:
523                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
524                         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
525                         break;
526                 case STLINK_MODE_DEBUG_SWIM:
527                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
528                         h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
529                         break;
530                 case STLINK_MODE_DFU:
531                         h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
532                         h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
533                         break;
534                 case STLINK_MODE_MASS:
535                 default:
536                         return ERROR_FAIL;
537         }
538
539         res = stlink_usb_xfer(handle, 0, 0);
540
541         if (res != ERROR_OK)
542                 return res;
543
544         return ERROR_OK;
545 }
546
547 /** */
548 static int stlink_usb_init_mode(void *handle)
549 {
550         int res;
551         uint8_t mode;
552         enum stlink_mode emode;
553         struct stlink_usb_handle_s *h;
554
555         assert(handle != NULL);
556
557         h = (struct stlink_usb_handle_s *)handle;
558
559         res = stlink_usb_current_mode(handle, &mode);
560
561         if (res != ERROR_OK)
562                 return res;
563
564         LOG_DEBUG("MODE: 0x%02X", mode);
565
566         /* try to exit current mode */
567         switch (mode) {
568                 case STLINK_DEV_DFU_MODE:
569                         emode = STLINK_MODE_DFU;
570                         break;
571                 case STLINK_DEV_DEBUG_MODE:
572                         emode = STLINK_MODE_DEBUG_SWD;
573                         break;
574                 case STLINK_DEV_SWIM_MODE:
575                         emode = STLINK_MODE_DEBUG_SWIM;
576                         break;
577                 case STLINK_DEV_BOOTLOADER_MODE:
578                 case STLINK_DEV_MASS_MODE:
579                 default:
580                         emode = STLINK_MODE_UNKNOWN;
581                         break;
582         }
583
584         if (emode != STLINK_MODE_UNKNOWN) {
585                 res = stlink_usb_mode_leave(handle, emode);
586
587                 if (res != ERROR_OK)
588                         return res;
589         }
590
591         res = stlink_usb_current_mode(handle, &mode);
592
593         if (res != ERROR_OK)
594                 return res;
595
596         LOG_DEBUG("MODE: 0x%02X", mode);
597
598         /* set selected mode */
599         switch (h->transport) {
600                 case HL_TRANSPORT_SWD:
601                         emode = STLINK_MODE_DEBUG_SWD;
602                         break;
603                 case HL_TRANSPORT_JTAG:
604                         emode = STLINK_MODE_DEBUG_JTAG;
605                         break;
606                 case HL_TRANSPORT_SWIM:
607                         emode = STLINK_MODE_DEBUG_SWIM;
608                         break;
609                 default:
610                         emode = STLINK_MODE_UNKNOWN;
611                         break;
612         }
613
614         if (emode == STLINK_MODE_UNKNOWN) {
615                 LOG_ERROR("selected mode (transport) not supported");
616                 return ERROR_FAIL;
617         }
618
619         res = stlink_usb_mode_enter(handle, emode);
620
621         if (res != ERROR_OK)
622                 return res;
623
624         res = stlink_usb_current_mode(handle, &mode);
625
626         if (res != ERROR_OK)
627                 return res;
628
629         LOG_DEBUG("MODE: 0x%02X", mode);
630
631         return ERROR_OK;
632 }
633
634 /** */
635 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
636 {
637         int res;
638         struct stlink_usb_handle_s *h;
639
640         assert(handle != NULL);
641
642         h = (struct stlink_usb_handle_s *)handle;
643
644         stlink_usb_init_buffer(handle, STLINK_RX_EP, 4);
645
646         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
647         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
648
649         res = stlink_usb_xfer(handle, h->databuf, 4);
650
651         if (res != ERROR_OK)
652                 return res;
653
654         *idcode = le_to_h_u32(h->databuf);
655
656         LOG_DEBUG("IDCODE: 0x%08X", *idcode);
657
658         return ERROR_OK;
659 }
660
661 static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
662 {
663         struct stlink_usb_handle_s *h;
664         int res;
665
666         assert(handle != NULL);
667
668         h = (struct stlink_usb_handle_s *)handle;
669
670         stlink_usb_init_buffer(handle, STLINK_RX_EP, 8);
671
672         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
673         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
674         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
675         h->cmdidx += 4;
676
677         res = stlink_usb_xfer(handle, h->databuf, 8);
678
679         if (res != ERROR_OK)
680                 return res;
681
682         *val = le_to_h_u32(h->databuf + 4);
683
684         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
685 }
686
687 static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
688 {
689         int res;
690         struct stlink_usb_handle_s *h;
691
692         assert(handle != NULL);
693
694         h = (struct stlink_usb_handle_s *)handle;
695
696         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
697
698         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
699         if (h->jtag_api == STLINK_JTAG_API_V1)
700                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
701         else
702                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
703         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
704         h->cmdidx += 4;
705         h_u32_to_le(h->cmdbuf+h->cmdidx, val);
706         h->cmdidx += 4;
707
708         res = stlink_usb_xfer(handle, h->databuf, 2);
709
710         if (res != ERROR_OK)
711                 return res;
712
713         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
714 }
715
716 static enum target_state stlink_usb_v2_get_status(void *handle)
717 {
718         int result;
719         uint32_t status;
720
721         result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
722         if  (result != ERROR_OK)
723                 return TARGET_UNKNOWN;
724
725         if (status & S_HALT)
726                 return TARGET_HALTED;
727         else if (status & S_RESET_ST)
728                 return TARGET_RESET;
729
730         return TARGET_RUNNING;
731 }
732
733 /** */
734 static enum target_state stlink_usb_state(void *handle)
735 {
736         int res;
737         struct stlink_usb_handle_s *h;
738
739         assert(handle != NULL);
740
741         h = (struct stlink_usb_handle_s *)handle;
742
743         if (h->jtag_api == STLINK_JTAG_API_V2)
744                 return stlink_usb_v2_get_status(handle);
745
746         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
747
748         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
749         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
750
751         res = stlink_usb_xfer(handle, h->databuf, 2);
752
753         if (res != ERROR_OK)
754                 return TARGET_UNKNOWN;
755
756         if (h->databuf[0] == STLINK_CORE_RUNNING)
757                 return TARGET_RUNNING;
758         if (h->databuf[0] == STLINK_CORE_HALTED)
759                 return TARGET_HALTED;
760
761         return TARGET_UNKNOWN;
762 }
763
764 /** */
765 static int stlink_usb_reset(void *handle)
766 {
767         int res;
768         struct stlink_usb_handle_s *h;
769
770         assert(handle != NULL);
771
772         h = (struct stlink_usb_handle_s *)handle;
773
774         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
775
776         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
777
778         if (h->jtag_api == STLINK_JTAG_API_V1)
779                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
780         else
781                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
782
783         res = stlink_usb_xfer(handle, h->databuf, 2);
784
785         if (res != ERROR_OK)
786                 return res;
787
788         LOG_DEBUG("RESET: 0x%08X", h->databuf[0]);
789
790         /* the following is not a error under swd (using hardware srst), so return success */
791         if (h->databuf[0] == STLINK_SWD_AP_WAIT || h->databuf[0] == STLINK_SWD_DP_WAIT)
792                 return ERROR_OK;
793
794         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
795 }
796
797 static int stlink_usb_assert_srst(void *handle, int srst)
798 {
799         int res;
800         struct stlink_usb_handle_s *h;
801
802         assert(handle != NULL);
803
804         h = (struct stlink_usb_handle_s *)handle;
805
806         if (h->jtag_api == STLINK_JTAG_API_V1)
807                 return ERROR_COMMAND_NOTFOUND;
808
809         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
810
811         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
812         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
813         h->cmdbuf[h->cmdidx++] = srst;
814
815         res = stlink_usb_xfer(handle, h->databuf, 2);
816
817         if (res != ERROR_OK)
818                 return res;
819
820         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
821 }
822
823 /** */
824 static int stlink_usb_run(void *handle)
825 {
826         int res;
827         struct stlink_usb_handle_s *h;
828
829         assert(handle != NULL);
830
831         h = (struct stlink_usb_handle_s *)handle;
832
833         if (h->jtag_api == STLINK_JTAG_API_V2)
834                 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
835
836         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
837
838         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
839         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
840
841         res = stlink_usb_xfer(handle, h->databuf, 2);
842
843         if (res != ERROR_OK)
844                 return res;
845
846         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
847 }
848
849 /** */
850 static int stlink_usb_halt(void *handle)
851 {
852         int res;
853         struct stlink_usb_handle_s *h;
854
855         assert(handle != NULL);
856
857         h = (struct stlink_usb_handle_s *)handle;
858
859         if (h->jtag_api == STLINK_JTAG_API_V2)
860                 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
861
862         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
863
864         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
865         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
866
867         res = stlink_usb_xfer(handle, h->databuf, 2);
868
869         if (res != ERROR_OK)
870                 return res;
871
872         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
873 }
874
875 /** */
876 static int stlink_usb_step(void *handle)
877 {
878         int res;
879         struct stlink_usb_handle_s *h;
880
881         assert(handle != NULL);
882
883         h = (struct stlink_usb_handle_s *)handle;
884
885         if (h->jtag_api == STLINK_JTAG_API_V2) {
886                 /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
887                  * that the cortex-m3 currently does. */
888                 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
889                 stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
890                 return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
891         }
892
893         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
894
895         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
896         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
897
898         res = stlink_usb_xfer(handle, h->databuf, 2);
899
900         if (res != ERROR_OK)
901                 return res;
902
903         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
904 }
905
906 /** */
907 static int stlink_usb_read_regs(void *handle)
908 {
909         int res;
910         struct stlink_usb_handle_s *h;
911
912         assert(handle != NULL);
913
914         h = (struct stlink_usb_handle_s *)handle;
915
916         stlink_usb_init_buffer(handle, STLINK_RX_EP, 84);
917
918         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
919         if (h->jtag_api == STLINK_JTAG_API_V1)
920                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
921         else
922                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
923
924         res = stlink_usb_xfer(handle, h->databuf, 84);
925
926         if (res != ERROR_OK)
927                 return res;
928
929         return ERROR_OK;
930 }
931
932 /** */
933 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
934 {
935         int res;
936         struct stlink_usb_handle_s *h;
937
938         assert(handle != NULL);
939
940         h = (struct stlink_usb_handle_s *)handle;
941
942         stlink_usb_init_buffer(handle, STLINK_RX_EP, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
943
944         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
945         if (h->jtag_api == STLINK_JTAG_API_V1)
946                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
947         else
948                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
949         h->cmdbuf[h->cmdidx++] = num;
950
951         res = stlink_usb_xfer(handle, h->databuf, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
952
953         if (res != ERROR_OK)
954                 return res;
955
956         if (h->jtag_api == STLINK_JTAG_API_V1)
957                 *val = le_to_h_u32(h->databuf);
958         else {
959                 *val = le_to_h_u32(h->databuf + 4);
960                 return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
961         }
962
963         return ERROR_OK;
964 }
965
966 /** */
967 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
968 {
969         int res;
970         struct stlink_usb_handle_s *h;
971
972         assert(handle != NULL);
973
974         h = (struct stlink_usb_handle_s *)handle;
975
976         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
977
978         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
979         if (h->jtag_api == STLINK_JTAG_API_V1)
980                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
981         else
982                 h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
983         h->cmdbuf[h->cmdidx++] = num;
984         h_u32_to_le(h->cmdbuf+h->cmdidx, val);
985         h->cmdidx += 4;
986
987         res = stlink_usb_xfer(handle, h->databuf, 2);
988
989         if (res != ERROR_OK)
990                 return res;
991
992         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL;
993 }
994
995 static int stlink_usb_get_rw_status(void *handle)
996 {
997         int res;
998         struct stlink_usb_handle_s *h;
999
1000         assert(handle != NULL);
1001
1002         h = (struct stlink_usb_handle_s *)handle;
1003
1004         if (h->jtag_api == STLINK_JTAG_API_V1)
1005                 return ERROR_OK;
1006
1007         stlink_usb_init_buffer(handle, STLINK_RX_EP, 2);
1008
1009         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1010         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
1011
1012         res = stlink_usb_xfer(handle, h->databuf, 2);
1013
1014         if (res != ERROR_OK)
1015                 return res;
1016
1017         return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : res;
1018 }
1019
1020 /** */
1021 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
1022                           uint8_t *buffer)
1023 {
1024         int res;
1025         uint16_t read_len = len;
1026         struct stlink_usb_handle_s *h;
1027
1028         assert(handle != NULL);
1029
1030         h = (struct stlink_usb_handle_s *)handle;
1031
1032         stlink_usb_init_buffer(handle, STLINK_RX_EP, read_len);
1033
1034         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1035         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
1036         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1037         h->cmdidx += 4;
1038         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1039         h->cmdidx += 2;
1040
1041         /* we need to fix read length for single bytes */
1042         if (read_len == 1)
1043                 read_len++;
1044
1045         res = stlink_usb_xfer(handle, h->databuf, read_len);
1046
1047         if (res != ERROR_OK)
1048                 return res;
1049
1050         memcpy(buffer, h->databuf, len);
1051
1052         return stlink_usb_get_rw_status(handle);
1053 }
1054
1055 /** */
1056 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
1057                            const uint8_t *buffer)
1058 {
1059         int res;
1060         struct stlink_usb_handle_s *h;
1061
1062         assert(handle != NULL);
1063
1064         h = (struct stlink_usb_handle_s *)handle;
1065
1066         stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1067
1068         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1069         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
1070         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1071         h->cmdidx += 4;
1072         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1073         h->cmdidx += 2;
1074
1075         res = stlink_usb_xfer(handle, buffer, len);
1076
1077         if (res != ERROR_OK)
1078                 return res;
1079
1080         return stlink_usb_get_rw_status(handle);
1081 }
1082
1083 /** */
1084 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
1085                           uint8_t *buffer)
1086 {
1087         int res;
1088         struct stlink_usb_handle_s *h;
1089
1090         assert(handle != NULL);
1091
1092         h = (struct stlink_usb_handle_s *)handle;
1093
1094         len *= 4;
1095
1096         stlink_usb_init_buffer(handle, STLINK_RX_EP, len);
1097
1098         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1099         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
1100         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1101         h->cmdidx += 4;
1102         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1103         h->cmdidx += 2;
1104
1105         res = stlink_usb_xfer(handle, h->databuf, len);
1106
1107         if (res != ERROR_OK)
1108                 return res;
1109
1110         memcpy(buffer, h->databuf, len);
1111
1112         return stlink_usb_get_rw_status(handle);
1113 }
1114
1115 /** */
1116 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
1117                            const uint8_t *buffer)
1118 {
1119         int res;
1120         struct stlink_usb_handle_s *h;
1121
1122         assert(handle != NULL);
1123
1124         h = (struct stlink_usb_handle_s *)handle;
1125
1126         len *= 4;
1127
1128         stlink_usb_init_buffer(handle, STLINK_TX_EP, len);
1129
1130         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
1131         h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
1132         h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
1133         h->cmdidx += 4;
1134         h_u16_to_le(h->cmdbuf+h->cmdidx, len);
1135         h->cmdidx += 2;
1136
1137         res = stlink_usb_xfer(handle, buffer, len);
1138
1139         if (res != ERROR_OK)
1140                 return res;
1141
1142         return stlink_usb_get_rw_status(handle);
1143 }
1144
1145 /** */
1146 static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
1147 {
1148         int err;
1149         struct stlink_usb_handle_s *h;
1150         enum stlink_jtag_api_version api;
1151
1152         LOG_DEBUG("stlink_usb_open");
1153
1154         h = malloc(sizeof(struct stlink_usb_handle_s));
1155
1156         if (h == 0) {
1157                 LOG_DEBUG("malloc failed");
1158                 return ERROR_FAIL;
1159         }
1160
1161         h->transport = param->transport;
1162
1163         /* set max read/write buffer size in bytes */
1164         param->max_buffer = 512;
1165
1166         const uint16_t vids[] = { param->vid, 0 };
1167         const uint16_t pids[] = { param->pid, 0 };
1168
1169         LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x", param->transport,
1170                 param->vid, param->pid);
1171
1172         if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
1173                 LOG_ERROR("open failed");
1174                 return ERROR_FAIL;
1175         }
1176
1177         jtag_libusb_set_configuration(h->fd, 0);
1178
1179         if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1180                 LOG_DEBUG("claim interface failed");
1181                 return ERROR_FAIL;
1182         }
1183
1184         /* wrap version for first read */
1185         switch (param->pid) {
1186                 case 0x3744:
1187                         h->version.stlink = 1;
1188                         break;
1189                 default:
1190                         h->version.stlink = 2;
1191                         break;
1192         }
1193
1194         /* get the device version */
1195         err = stlink_usb_version(h);
1196
1197         if (err != ERROR_OK) {
1198                 LOG_ERROR("read version failed");
1199                 jtag_libusb_close(h->fd);
1200                 free(h);
1201                 return err;
1202         }
1203
1204         /* compare usb vid/pid */
1205         if ((param->vid != h->vid) || (param->pid != h->pid))
1206                 LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
1207                         param->vid, param->pid,
1208                         h->vid, h->pid);
1209
1210         /* check if mode is supported */
1211         err = ERROR_OK;
1212
1213         switch (h->transport) {
1214                 case HL_TRANSPORT_SWD:
1215                 case HL_TRANSPORT_JTAG:
1216                         if (h->version.jtag == 0)
1217                                 err = ERROR_FAIL;
1218                         break;
1219                 case HL_TRANSPORT_SWIM:
1220                         if (h->version.swim == 0)
1221                                 err = ERROR_FAIL;
1222                         break;
1223                 default:
1224                         err = ERROR_FAIL;
1225                         break;
1226         }
1227
1228         if (err != ERROR_OK) {
1229                 LOG_ERROR("mode (transport) not supported by device");
1230                 jtag_libusb_close(h->fd);
1231                 free(h);
1232                 return err;
1233         }
1234
1235         api = h->version.jtag_api_max;
1236
1237         /* check that user has not requested certain api version
1238          * and if they have check it is supported */
1239         if ((param->api != 0) && (param->api <= h->version.jtag_api_max)) {
1240                 api = param->api;
1241                 LOG_INFO("using stlink api v%d", api);
1242         }
1243
1244         /* set the used jtag api, this will default to the newest supported version */
1245         h->jtag_api = api;
1246
1247         /* initialize the debug hardware */
1248         err = stlink_usb_init_mode(h);
1249
1250         if (err != ERROR_OK) {
1251                 LOG_ERROR("init mode failed");
1252                 jtag_libusb_close(h->fd);
1253                 free(h);
1254                 return err;
1255         }
1256
1257         *fd = h;
1258
1259         return ERROR_OK;
1260 }
1261
1262 /** */
1263 static int stlink_usb_close(void *fd)
1264 {
1265         return ERROR_OK;
1266 }
1267
1268 /** */
1269 struct hl_layout_api_s stlink_usb_layout_api = {
1270         /** */
1271         .open = stlink_usb_open,
1272         /** */
1273         .close = stlink_usb_close,
1274         /** */
1275         .idcode = stlink_usb_idcode,
1276         /** */
1277         .state = stlink_usb_state,
1278         /** */
1279         .reset = stlink_usb_reset,
1280         /** */
1281         .assert_srst = stlink_usb_assert_srst,
1282         /** */
1283         .run = stlink_usb_run,
1284         /** */
1285         .halt = stlink_usb_halt,
1286         /** */
1287         .step = stlink_usb_step,
1288         /** */
1289         .read_regs = stlink_usb_read_regs,
1290         /** */
1291         .read_reg = stlink_usb_read_reg,
1292         /** */
1293         .write_reg = stlink_usb_write_reg,
1294         /** */
1295         .read_mem8 = stlink_usb_read_mem8,
1296         /** */
1297         .write_mem8 = stlink_usb_write_mem8,
1298         /** */
1299         .read_mem32 = stlink_usb_read_mem32,
1300         /** */
1301         .write_mem32 = stlink_usb_write_mem32,
1302         /** */
1303         .write_debug_reg = stlink_usb_write_debug_reg
1304 };