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