Add bootloader mode.
[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_OK                               0x80
88 #define STLINK_FALSE                            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
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 enum stlink_mode {
145         STLINK_MODE_UNKNOWN = 0,
146         STLINK_MODE_DFU,
147         STLINK_MODE_MASS,
148         STLINK_MODE_DEBUG_JTAG,
149         STLINK_MODE_DEBUG_SWD,
150         STLINK_MODE_DEBUG_SWIM
151 };
152
153 /** */
154 static void stlink_usb_recv_v1_create_cmd(char *b, int s, uint32_t tag, uint32_t rxsize,
155                         uint8_t flag, uint8_t lun, uint8_t length)
156 {
157         int i = 0;
158
159         memset(b, 0x00, s);
160
161         /* fill the send buffer */
162         strcpy(b, "USBC");
163         i += 4;
164
165         buf_set_u32(b+i, 0, 32, tag);
166         i += 4;
167         buf_set_u32(b+i, 0, 32, rxsize);
168         i += 4;
169         b[i++] = flag;
170         b[i++] = lun;
171         b[i++] = length;
172 }
173
174 /** */
175 static int stlink_usb_recv_v1_mass_storage_cmd(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
176                     int rxsize)
177 {
178         char sg_buffer[31];
179         struct stlink_usb_handle_s *h;
180
181         assert(handle != NULL);
182
183         h = (struct stlink_usb_handle_s *)handle;
184         h->sg_tag = (h->sg_tag + 1) & 1;
185
186         stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, rxsize, STLINK_TX_EP, 0x00, txsize);
187
188         memcpy(sg_buffer+15, txbuf, 10);
189
190         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 31,
191                                    1000) != 31) {
192                 printf("send failed\n");
193                 return ERROR_FAIL;
194         }
195
196         return ERROR_OK;
197 }
198
199 #define REQUEST_SENSE           0x03
200 #define REQUEST_SENSE_LENGTH    18
201
202 /** */
203 static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
204 {
205         struct stlink_usb_handle_s *h;
206
207         assert(handle != NULL);
208
209         h = (struct stlink_usb_handle_s *)handle;
210
211         /* read status */
212         memset(sg_buffer, 0x00, len);
213
214         if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)sg_buffer,
215                                 len, 1000) != len)
216                 return ERROR_FAIL;
217
218         uint32_t t1, t2;
219
220         t1 = buf_get_u32(sg_buffer+0, 0, 32);
221         t2 = buf_get_u32(sg_buffer+4, 0, 32);
222
223         /* check for USBS */
224         if (t1 != 0x53425355)
225                 return ERROR_FAIL;
226
227         return ERROR_OK;
228 }
229
230 /** */
231 static int stlink_usb_recv_v1_get_sense(void *handle)
232 {
233         struct stlink_usb_handle_s *h;
234         char cdb[16];
235         char sg_buffer[31];
236
237         assert(handle != NULL);
238
239         h = (struct stlink_usb_handle_s *)handle;
240         h->sg_tag = (h->sg_tag + 1) & 1;
241
242         cdb[0] = REQUEST_SENSE;
243         cdb[4] = REQUEST_SENSE_LENGTH;
244
245         stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, REQUEST_SENSE_LENGTH, STLINK_TX_EP,
246                         0x00, 16);
247
248         memcpy(sg_buffer+15, cdb, 16);
249
250         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 16,
251                                    1000) != 16)
252                 return ERROR_FAIL;
253
254         if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)cdb,
255                                   16, 1000) != 16)
256                 return ERROR_FAIL;
257
258         if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
259                 return ERROR_FAIL;
260         /* check for sense */
261         if (sg_buffer[12] != 0)
262                 return ERROR_FAIL;
263
264         /* if (sense[0] != 0x70 && sense[0] != 0x71) */
265
266         return ERROR_OK;
267 }
268
269 /** */
270 static int stlink_usb_recv_v1(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
271                     int rxsize)
272 {
273         int err;
274         char sg_buffer[31];
275         struct stlink_usb_handle_s *h;
276
277         assert(handle != NULL);
278
279         h = (struct stlink_usb_handle_s *)handle;
280
281         err = stlink_usb_recv_v1_mass_storage_cmd(handle, txbuf, txsize, rxbuf, rxsize);
282
283         if (err != ERROR_OK)
284                 return err;
285
286         if (rxsize && rxbuf) {
287                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
288                                           rxsize, 1000) != rxsize) {
289                         LOG_DEBUG("jtag_libusb_bulk_read");
290                         return ERROR_FAIL;
291                 }
292         }
293
294         if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
295                 return ERROR_FAIL;
296         /* check for sense */
297         if (sg_buffer[12] == 1) {
298                 LOG_DEBUG("get sense");
299                 err = stlink_usb_recv_v1_get_sense(handle);
300         }
301         return err;
302 }
303
304 /** */
305 static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
306                     int rxsize)
307 {
308         struct stlink_usb_handle_s *h;
309
310         assert(handle != NULL);
311
312         h = (struct stlink_usb_handle_s *)handle;
313
314         if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
315                                    1000) != txsize) {
316                 return ERROR_FAIL;
317         }
318         if (rxsize && rxbuf) {
319                 if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
320                                           rxsize, 1000) != rxsize) {
321                         return ERROR_FAIL;
322                 }
323         }
324         return ERROR_OK;
325 }
326
327 /** */
328 static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
329                     int rxsize)
330 {
331         struct stlink_usb_handle_s *h;
332
333         assert(handle != NULL);
334
335         h = (struct stlink_usb_handle_s *)handle;
336
337         if (h->version.stlink == 1) {
338                 return stlink_usb_recv_v1(handle, txbuf, txsize, rxbuf, rxsize);
339         } else {
340                 if (txsize < STLINK_CMD_SIZE)
341                         txsize = STLINK_CMD_SIZE;
342                 return stlink_usb_recv_v2(handle, txbuf, txsize, rxbuf, rxsize);
343         }
344 }
345
346 /** */
347 static void stlink_usb_init_buffer(void *handle)
348 {
349         struct stlink_usb_handle_s *h;
350
351         assert(handle != NULL);
352
353         h = (struct stlink_usb_handle_s *)handle;
354
355         memset(h->txbuf, 0, STLINK_CMD_SIZE);
356 }
357
358 /** */
359 static int stlink_usb_version(void *handle)
360 {
361         int res;
362         uint16_t v;
363         struct stlink_usb_handle_s *h;
364
365         assert(handle != NULL);
366
367         h = (struct stlink_usb_handle_s *)handle;
368
369         stlink_usb_init_buffer(handle);
370
371         h->txbuf[0] = STLINK_GET_VERSION;
372
373         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
374
375         if (res != ERROR_OK)
376                 return res;
377
378         v = (h->rxbuf[0] << 8) | h->rxbuf[1];
379
380         h->version.stlink = (v >> 12) & 0x0f;
381         h->version.jtag = (v >> 6) & 0x3f;
382         h->version.swim = v & 0x3f;
383         h->vid = buf_get_u32(h->rxbuf, 16, 16);
384         h->pid = buf_get_u32(h->rxbuf, 32, 16);
385
386         /* set the supported jtag api version
387          * V1 doesn't support API V2 at all
388          * V2 support API V2 since JTAG V13
389          */
390         if ((h->version.stlink == 2) && (h->version.jtag > 12))
391                 h->version.jtag_api_max = STLINK_JTAG_API_V2;
392         else
393                 h->version.jtag_api_max = STLINK_JTAG_API_V1;
394
395         LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
396                 h->version.stlink,
397                 h->version.jtag,
398                 (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
399                 h->version.swim,
400                 h->vid,
401                 h->pid);
402
403         return ERROR_OK;
404 }
405
406 /** */
407 static int stlink_usb_current_mode(void *handle, uint8_t *mode)
408 {
409         int res;
410         struct stlink_usb_handle_s *h;
411
412         assert(handle != NULL);
413
414         h = (struct stlink_usb_handle_s *)handle;
415
416         stlink_usb_init_buffer(handle);
417
418         h->txbuf[0] = STLINK_GET_CURRENT_MODE;
419
420         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
421
422         if (res != ERROR_OK)
423                 return res;
424
425         *mode = h->rxbuf[0];
426
427         return ERROR_OK;
428 }
429
430 /** */
431 static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
432 {
433         int res;
434         struct stlink_usb_handle_s *h;
435
436         assert(handle != NULL);
437
438         h = (struct stlink_usb_handle_s *)handle;
439
440         stlink_usb_init_buffer(handle);
441
442         switch (type) {
443                 case STLINK_MODE_DEBUG_JTAG:
444                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
445                         if (h->jtag_api == STLINK_JTAG_API_V1)
446                                 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
447                         else
448                                 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
449                         h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
450                         break;
451                 case STLINK_MODE_DEBUG_SWD:
452                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
453                         if (h->jtag_api == STLINK_JTAG_API_V1)
454                                 h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
455                         else
456                                 h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
457                         h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
458                         break;
459                 case STLINK_MODE_DEBUG_SWIM:
460                         h->txbuf[0] = STLINK_SWIM_COMMAND;
461                         h->txbuf[1] = STLINK_SWIM_ENTER;
462                         break;
463                 case STLINK_MODE_DFU:
464                 case STLINK_MODE_MASS:
465                 default:
466                         return ERROR_FAIL;
467         }
468
469         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
470         if (res != ERROR_OK)
471                 return res;
472
473         return ERROR_OK;
474 }
475
476 /** */
477 static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
478 {
479         int res;
480         struct stlink_usb_handle_s *h;
481
482         assert(handle != NULL);
483
484         h = (struct stlink_usb_handle_s *)handle;
485
486         stlink_usb_init_buffer(handle);
487
488         switch (type) {
489                 case STLINK_MODE_DEBUG_JTAG:
490                 case STLINK_MODE_DEBUG_SWD:
491                         h->txbuf[0] = STLINK_DEBUG_COMMAND;
492                         h->txbuf[1] = STLINK_DEBUG_EXIT;
493                         break;
494                 case STLINK_MODE_DEBUG_SWIM:
495                         h->txbuf[0] = STLINK_SWIM_COMMAND;
496                         h->txbuf[1] = STLINK_SWIM_EXIT;
497                         break;
498                 case STLINK_MODE_DFU:
499                         h->txbuf[0] = STLINK_DFU_COMMAND;
500                         h->txbuf[1] = STLINK_DFU_EXIT;
501                         break;
502                 case STLINK_MODE_MASS:
503                 default:
504                         return ERROR_FAIL;
505         }
506
507         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
508         if (res != ERROR_OK)
509                 return res;
510
511         return ERROR_OK;
512 }
513
514 /** */
515 static int stlink_usb_init_mode(void *handle)
516 {
517         int res;
518         uint8_t mode;
519         enum stlink_mode emode;
520         struct stlink_usb_handle_s *h;
521
522         assert(handle != NULL);
523
524         h = (struct stlink_usb_handle_s *)handle;
525
526         res = stlink_usb_current_mode(handle, &mode);
527
528         if (res != ERROR_OK)
529                 return res;
530
531         LOG_DEBUG("MODE: %02X", mode);
532
533         /* try to exit current mode */
534         switch (mode) {
535                 case STLINK_DEV_DFU_MODE:
536                         emode = STLINK_MODE_DFU;
537                         break;
538                 case STLINK_DEV_DEBUG_MODE:
539                         emode = STLINK_MODE_DEBUG_SWD;
540                         break;
541                 case STLINK_DEV_SWIM_MODE:
542                         emode = STLINK_MODE_DEBUG_SWIM;
543                         break;
544                 case STLINK_DEV_BOOTLOADER_MODE:
545                 default:
546                         emode = STLINK_MODE_UNKNOWN;
547                         break;
548         }
549
550         if (emode != STLINK_MODE_UNKNOWN) {
551                 res = stlink_usb_mode_leave(handle, emode);
552
553                 if (res != ERROR_OK)
554                         return res;
555         }
556
557         res = stlink_usb_current_mode(handle, &mode);
558
559         if (res != ERROR_OK)
560                 return res;
561
562         LOG_DEBUG("MODE: %02X", mode);
563
564         /* set selected mode */
565         switch (h->transport) {
566                 case STLINK_TRANSPORT_SWD:
567                         emode = STLINK_MODE_DEBUG_SWD;
568                         break;
569                 case STLINK_TRANSPORT_JTAG:
570                         emode = STLINK_MODE_DEBUG_JTAG;
571                         break;
572                 case STLINK_TRANSPORT_SWIM:
573                         emode = STLINK_MODE_DEBUG_SWIM;
574                         break;
575                 default:
576                         emode = STLINK_MODE_UNKNOWN;
577                         break;
578         }
579
580         if (emode == STLINK_MODE_UNKNOWN) {
581                 LOG_ERROR("selected mode (transport) not supported");
582                 return ERROR_FAIL;
583         }
584
585         res = stlink_usb_mode_enter(handle, emode);
586
587         if (res != ERROR_OK)
588                 return res;
589
590         res = stlink_usb_current_mode(handle, &mode);
591
592         if (res != ERROR_OK)
593                 return res;
594
595         LOG_DEBUG("MODE: %02X", mode);
596
597         return ERROR_OK;
598 }
599
600 /** */
601 static int stlink_usb_idcode(void *handle, uint32_t *idcode)
602 {
603         int res;
604         struct stlink_usb_handle_s *h;
605
606         assert(handle != NULL);
607
608         h = (struct stlink_usb_handle_s *)handle;
609
610         stlink_usb_init_buffer(handle);
611
612         h->txbuf[0] = STLINK_DEBUG_COMMAND;
613         h->txbuf[1] = STLINK_DEBUG_READCOREID;
614
615         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
616
617         if (res != ERROR_OK)
618                 return res;
619
620         *idcode = le_to_h_u32(h->rxbuf);
621
622         LOG_DEBUG("IDCODE: %08X", *idcode);
623
624         return ERROR_OK;
625 }
626
627 /** */
628 static enum target_state stlink_usb_state(void *handle)
629 {
630         int res;
631         struct stlink_usb_handle_s *h;
632
633         assert(handle != NULL);
634
635         h = (struct stlink_usb_handle_s *)handle;
636
637         if (h->jtag_api == STLINK_JTAG_API_V2)
638                 return TARGET_UNKNOWN;
639
640         stlink_usb_init_buffer(handle);
641
642         h->txbuf[0] = STLINK_DEBUG_COMMAND;
643         h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
644
645         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
646
647         if (res != ERROR_OK)
648                 return TARGET_UNKNOWN;
649
650         if (h->rxbuf[0] == STLINK_CORE_RUNNING)
651                 return TARGET_RUNNING;
652         if (h->rxbuf[0] == STLINK_CORE_HALTED)
653                 return TARGET_HALTED;
654
655         return TARGET_UNKNOWN;
656 }
657
658 /** */
659 static int stlink_usb_reset(void *handle)
660 {
661         int res;
662         struct stlink_usb_handle_s *h;
663
664         assert(handle != NULL);
665
666         h = (struct stlink_usb_handle_s *)handle;
667
668         stlink_usb_init_buffer(handle);
669
670         h->txbuf[0] = STLINK_DEBUG_COMMAND;
671
672         if (h->jtag_api == STLINK_JTAG_API_V1)
673                 h->txbuf[1] = STLINK_DEBUG_APIV1_RESETSYS;
674         else
675                 h->txbuf[1] = STLINK_DEBUG_APIV2_RESETSYS;
676
677         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
678
679         if (res != ERROR_OK)
680                 return res;
681
682         LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
683
684         return ERROR_OK;
685 }
686
687 /** */
688 static int stlink_usb_run(void *handle)
689 {
690         int res;
691         struct stlink_usb_handle_s *h;
692
693         assert(handle != NULL);
694
695         h = (struct stlink_usb_handle_s *)handle;
696
697         if (h->jtag_api == STLINK_JTAG_API_V2)
698                 return ERROR_FAIL;
699
700         stlink_usb_init_buffer(handle);
701
702         h->txbuf[0] = STLINK_DEBUG_COMMAND;
703         h->txbuf[1] = STLINK_DEBUG_RUNCORE;
704
705         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
706
707         if (res != ERROR_OK)
708                 return res;
709
710         return ERROR_OK;
711 }
712
713 /** */
714 static int stlink_usb_halt(void *handle)
715 {
716         int res;
717         struct stlink_usb_handle_s *h;
718
719         assert(handle != NULL);
720
721         h = (struct stlink_usb_handle_s *)handle;
722
723         if (h->jtag_api == STLINK_JTAG_API_V2)
724                 return ERROR_FAIL;
725
726         stlink_usb_init_buffer(handle);
727
728         h->txbuf[0] = STLINK_DEBUG_COMMAND;
729         h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
730
731         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
732
733         if (res != ERROR_OK)
734                 return res;
735
736         return ERROR_OK;
737 }
738
739 /** */
740 static int stlink_usb_step(void *handle)
741 {
742         int res;
743         struct stlink_usb_handle_s *h;
744
745         assert(handle != NULL);
746
747         h = (struct stlink_usb_handle_s *)handle;
748
749         if (h->jtag_api == STLINK_JTAG_API_V2)
750                 return ERROR_FAIL;
751
752         stlink_usb_init_buffer(handle);
753
754         h->txbuf[0] = STLINK_DEBUG_COMMAND;
755         h->txbuf[1] = STLINK_DEBUG_STEPCORE;
756
757         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
758
759         if (res != ERROR_OK)
760                 return res;
761
762         return ERROR_OK;
763 }
764
765 /** */
766 static int stlink_usb_read_regs(void *handle)
767 {
768         int res;
769         struct stlink_usb_handle_s *h;
770
771         assert(handle != NULL);
772
773         h = (struct stlink_usb_handle_s *)handle;
774
775         stlink_usb_init_buffer(handle);
776
777         h->txbuf[0] = STLINK_DEBUG_COMMAND;
778         if (h->jtag_api == STLINK_JTAG_API_V1)
779                 h->txbuf[1] = STLINK_DEBUG_APIV1_READALLREGS;
780         else
781                 h->txbuf[1] = STLINK_DEBUG_APIV2_READALLREGS;
782
783         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
784
785         if (res != ERROR_OK)
786                 return res;
787
788         return ERROR_OK;
789 }
790
791 /** */
792 static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
793 {
794         int res;
795         struct stlink_usb_handle_s *h;
796
797         assert(handle != NULL);
798
799         h = (struct stlink_usb_handle_s *)handle;
800
801         stlink_usb_init_buffer(handle);
802
803         h->txbuf[0] = STLINK_DEBUG_COMMAND;
804         if (h->jtag_api == STLINK_JTAG_API_V1)
805                 h->txbuf[1] = STLINK_DEBUG_APIV1_READREG;
806         else
807                 h->txbuf[1] = STLINK_DEBUG_APIV2_READREG;
808         h->txbuf[2] = num;
809
810         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
811
812         if (res != ERROR_OK)
813                 return res;
814
815         *val = le_to_h_u32(h->rxbuf);
816
817         return ERROR_OK;
818 }
819
820 /** */
821 static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
822 {
823         int res;
824         struct stlink_usb_handle_s *h;
825
826         assert(handle != NULL);
827
828         h = (struct stlink_usb_handle_s *)handle;
829
830         stlink_usb_init_buffer(handle);
831
832         h->txbuf[0] = STLINK_DEBUG_COMMAND;
833         if (h->jtag_api == STLINK_JTAG_API_V1)
834                 h->txbuf[1] = STLINK_DEBUG_APIV1_WRITEREG;
835         else
836                 h->txbuf[1] = STLINK_DEBUG_APIV2_WRITEREG;
837         h->txbuf[2] = num;
838         h_u32_to_le(h->txbuf + 3, val);
839
840         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
841
842         if (res != ERROR_OK)
843                 return res;
844
845         return ERROR_OK;
846 }
847
848 /** */
849 static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
850                           uint8_t *buffer)
851 {
852         int res;
853         uint16_t read_len = len;
854         struct stlink_usb_handle_s *h;
855
856         assert(handle != NULL);
857
858         h = (struct stlink_usb_handle_s *)handle;
859
860         stlink_usb_init_buffer(handle);
861
862         h->txbuf[0] = STLINK_DEBUG_COMMAND;
863         h->txbuf[1] = STLINK_DEBUG_READMEM_8BIT;
864         h_u32_to_le(h->txbuf + 2, addr);
865         h_u16_to_le(h->txbuf + 2 + 4, len);
866
867         /* we need to fix read length for single bytes */
868         if (read_len == 1)
869                 read_len++;
870
871         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, read_len);
872
873         if (res != ERROR_OK)
874                 return res;
875
876         memcpy(buffer, h->rxbuf, len);
877
878         return ERROR_OK;
879 }
880
881 /** */
882 static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
883                            const uint8_t *buffer)
884 {
885         int res;
886         struct stlink_usb_handle_s *h;
887
888         assert(handle != NULL);
889
890         h = (struct stlink_usb_handle_s *)handle;
891
892         stlink_usb_init_buffer(handle);
893
894         h->txbuf[0] = STLINK_DEBUG_COMMAND;
895         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_8BIT;
896         h_u32_to_le(h->txbuf + 2, addr);
897         h_u16_to_le(h->txbuf + 2 + 4, len);
898
899         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
900
901         if (res != ERROR_OK)
902                 return res;
903
904         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
905
906         if (res != ERROR_OK)
907                 return res;
908
909         return ERROR_OK;
910 }
911
912 /** */
913 static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
914                           uint32_t *buffer)
915 {
916         int res;
917         struct stlink_usb_handle_s *h;
918
919         assert(handle != NULL);
920
921         h = (struct stlink_usb_handle_s *)handle;
922
923         stlink_usb_init_buffer(handle);
924
925         len *= 4;
926
927         h->txbuf[0] = STLINK_DEBUG_COMMAND;
928         h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
929         h_u32_to_le(h->txbuf + 2, addr);
930         h_u16_to_le(h->txbuf + 2 + 4, len);
931
932         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
933
934         if (res != ERROR_OK)
935                 return res;
936
937         memcpy(buffer, h->rxbuf, len);
938
939         return ERROR_OK;
940 }
941
942 /** */
943 static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
944                            const uint32_t *buffer)
945 {
946         int res;
947         struct stlink_usb_handle_s *h;
948
949         assert(handle != NULL);
950
951         h = (struct stlink_usb_handle_s *)handle;
952
953         stlink_usb_init_buffer(handle);
954
955         len *= 4;
956
957         h->txbuf[0] = STLINK_DEBUG_COMMAND;
958         h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
959         h_u32_to_le(h->txbuf + 2, addr);
960         h_u16_to_le(h->txbuf + 2 + 4, len);
961
962         res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
963
964         if (res != ERROR_OK)
965                 return res;
966
967         res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
968
969         if (res != ERROR_OK)
970                 return res;
971
972         return ERROR_OK;
973 }
974
975 /** */
976 static int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
977 {
978         int err;
979         struct stlink_usb_handle_s *h;
980
981         LOG_DEBUG("stlink_usb_open");
982
983         h = malloc(sizeof(struct stlink_usb_handle_s));
984
985         if (h == 0) {
986                 LOG_DEBUG("malloc failed");
987                 return ERROR_FAIL;
988         }
989
990         h->transport = param->transport;
991
992         const uint16_t vids[] = { param->vid, 0 };
993         const uint16_t pids[] = { param->pid, 0 };
994
995         LOG_DEBUG("transport: %d vid: %04x pid: %04x", param->transport,
996                 param->vid, param->pid);
997
998         if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
999                 LOG_ERROR("open failed");
1000                 return ERROR_FAIL;
1001         }
1002
1003         jtag_libusb_set_configuration(h->fd, 0);
1004
1005         if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
1006                 LOG_DEBUG("claim interface failed");
1007                 return ERROR_FAIL;
1008         }
1009
1010         /* wrap version for first read */
1011         switch (param->pid) {
1012                 case 0x3744:
1013                         h->version.stlink = 1;
1014                         break;
1015                 default:
1016                         h->version.stlink = 2;
1017                         break;
1018         }
1019
1020         /* get the device version */
1021         err = stlink_usb_version(h);
1022
1023         if (err != ERROR_OK) {
1024                 LOG_ERROR("read version failed");
1025                 jtag_libusb_close(h->fd);
1026                 free(h);
1027                 return err;
1028         }
1029
1030         /* compare usb vid/pid */
1031         if ((param->vid != h->vid) || (param->pid != h->pid))
1032                 LOG_INFO("vid/pid are not identical: %04X/%04X %04X/%04X",
1033                         param->vid, param->pid,
1034                         h->vid, h->pid);
1035
1036         /* check if mode is supported */
1037         err = ERROR_OK;
1038
1039         switch (h->transport) {
1040                 case STLINK_TRANSPORT_SWD:
1041                 case STLINK_TRANSPORT_JTAG:
1042                         if (h->version.jtag == 0)
1043                                 err = ERROR_FAIL;
1044                         break;
1045                 case STLINK_TRANSPORT_SWIM:
1046                         if (h->version.swim == 0)
1047                                 err = ERROR_FAIL;
1048                         break;
1049                 default:
1050                         err = ERROR_FAIL;
1051                         break;
1052         }
1053
1054         if (err != ERROR_OK) {
1055                 LOG_ERROR("mode (transport) not supported by device");
1056                 jtag_libusb_close(h->fd);
1057                 free(h);
1058                 return err;
1059         }
1060
1061         /* set the used jtag api */
1062         h->jtag_api = STLINK_JTAG_API_V1;
1063
1064         /* initialize the debug hardware */
1065         err = stlink_usb_init_mode(h);
1066
1067         if (err != ERROR_OK) {
1068                 LOG_ERROR("init mode failed");
1069                 jtag_libusb_close(h->fd);
1070                 free(h);
1071                 return err;
1072         }
1073
1074         *fd = h;
1075
1076         return ERROR_OK;
1077 }
1078
1079 /** */
1080 static int stlink_usb_close(void *fd)
1081 {
1082         return ERROR_OK;
1083 }
1084
1085 /** */
1086 struct stlink_layout_api_s stlink_usb_layout_api = {
1087         /** */
1088         .open = stlink_usb_open,
1089         /** */
1090         .close = stlink_usb_close,
1091         /** */
1092         .idcode = stlink_usb_idcode,
1093         /** */
1094         .state = stlink_usb_state,
1095         /** */
1096         .reset = stlink_usb_reset,
1097         /** */
1098         .run = stlink_usb_run,
1099         /** */
1100         .halt = stlink_usb_halt,
1101         /** */
1102         .step = stlink_usb_step,
1103         /** */
1104         .read_regs = stlink_usb_read_regs,
1105         /** */
1106         .read_reg = stlink_usb_read_reg,
1107         /** */
1108         .write_reg = stlink_usb_write_reg,
1109         /** */
1110         .read_mem8 = stlink_usb_read_mem8,
1111         /** */
1112         .write_mem8 = stlink_usb_write_mem8,
1113         /** */
1114         .read_mem32 = stlink_usb_read_mem32,
1115         /** */
1116         .write_mem32 = stlink_usb_write_mem32,
1117 };