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