24960a8d99751eba612f14e716cb9c0078edec0f
[fw/openocd] / src / jtag / drivers / openjtag.c
1 /*******************************************************************************
2  *   Driver for OpenJTAG Project (www.openjtag.org)                            *
3  *   Compatible with libftdi and ftd2xx drivers.                               *
4  *                                                                             *
5  *   Cypress CY7C65215 support                                                 *
6  *   Copyright (C) 2015 Vianney le ClĂ©ment de Saint-Marcq, Essensium NV        *
7  *                      <vianney.leclement@essensium.com>                      *
8  *                                                                             *
9  *   Copyright (C) 2010 by Ivan Meleca <mileca@gmail.com>                      *
10  *                                                                             *
11  *   Copyright (C) 2013 by Ryan Corbin, GlueLogix Inc. <corbin.ryan@gmail.com> *
12  *   Updated to work with OpenOCD v0.7.0. Fixed libftdi read speed issue.      *
13  *                                                                             *
14  *   Based on usb_blaster.c                                                    *
15  *   Copyright (C) 2009 Catalin Patulea                                        *
16  *   Copyright (C) 2006 Kolja Waschk                                           *
17  *                                                                             *
18  *   And jlink.c                                                               *
19  *   Copyright (C) 2008 by Spencer Oliver                                      *
20  *   spen@spen-soft.co.uk                                                      *
21  *                                                                             *
22  *   This program is free software; you can redistribute it and/or modify      *
23  *   it under the terms of the GNU General Public License as published by      *
24  *   the Free Software Foundation; either version 2 of the License, or         *
25  *   (at your option) any later version.                                       *
26  *                                                                             *
27  *   This program is distributed in the hope that it will be useful,           *
28  *   but WITHOUT ANY WARRANTY; without even the implied warranty of            *
29  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
30  *   GNU General Public License for more details.                              *
31  *                                                                             *
32  *   You should have received a copy of the GNU General Public License         *
33  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
34  ***************************************************************************/
35
36 /***************************************************************************
37  * Version 1.0  Tested on a MCBSTM32 board using a Cortex-M3 (stm32f103x), *
38  *              GDB and Eclipse under Linux (Ubuntu 10.04)                 *
39  *                                                                         *
40  ***************************************************************************/
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include <jtag/interface.h>
47 #include <jtag/commands.h>
48 #include "libusb_common.h"
49
50 static enum {
51         OPENJTAG_VARIANT_STANDARD,
52         OPENJTAG_VARIANT_CY7C65215,
53 } openjtag_variant = OPENJTAG_VARIANT_STANDARD;
54
55 static const char * const openjtag_variant_names[] = {
56         "standard",
57         "cy7c65215",
58         NULL
59 };
60
61 /*
62  * OpenJTAG-OpenOCD state conversion
63  */
64 typedef enum openjtag_tap_state {
65         OPENJTAG_TAP_INVALID    = -1,
66         OPENJTAG_TAP_RESET  = 0,
67         OPENJTAG_TAP_IDLE   = 1,
68         OPENJTAG_TAP_SELECT_DR  = 2,
69         OPENJTAG_TAP_CAPTURE_DR = 3,
70         OPENJTAG_TAP_SHIFT_DR   = 4,
71         OPENJTAG_TAP_EXIT1_DR   = 5,
72         OPENJTAG_TAP_PAUSE_DR   = 6,
73         OPENJTAG_TAP_EXIT2_DR   = 7,
74         OPENJTAG_TAP_UPDATE_DR  = 8,
75         OPENJTAG_TAP_SELECT_IR  = 9,
76         OPENJTAG_TAP_CAPURE_IR  = 10,
77         OPENJTAG_TAP_SHIFT_IR   = 11,
78         OPENJTAG_TAP_EXIT1_IR   = 12,
79         OPENJTAG_TAP_PAUSE_IR   = 13,
80         OPENJTAG_TAP_EXIT2_IR   = 14,
81         OPENJTAG_TAP_UPDATE_IR  = 15,
82 } openjtag_tap_state_t;
83
84 /* OPENJTAG access library includes */
85 #include <ftdi.h>
86
87 /* OpenJTAG vid/pid */
88 static uint16_t openjtag_vid = 0x0403;
89 static uint16_t openjtag_pid = 0x6001;
90
91 static char *openjtag_device_desc;
92
93 static struct ftdi_context ftdic;
94
95 #define OPENJTAG_BUFFER_SIZE        504
96 #define OPENJTAG_MAX_PENDING_RESULTS    256
97
98 struct openjtag_scan_result {
99         uint32_t bits;          /* Length in bits*/
100         struct scan_command *command;   /* Corresponding scan command */
101         uint8_t *buffer;
102 };
103
104 /* USB RX/TX buffers */
105 static int usb_tx_buf_offs;
106 static uint8_t usb_tx_buf[OPENJTAG_BUFFER_SIZE];
107 static uint32_t usb_rx_buf_len;
108 static uint8_t usb_rx_buf[OPENJTAG_BUFFER_SIZE];
109
110 /* Pending readings */
111 static struct openjtag_scan_result openjtag_scan_result_buffer[OPENJTAG_MAX_PENDING_RESULTS];
112 static int openjtag_scan_result_count;
113
114 static jtag_libusb_device_handle *usbh;
115
116 /* CY7C65215 model only */
117 #define CY7C65215_JTAG_REQUEST  0x40  /* bmRequestType: vendor host-to-device */
118 #define CY7C65215_JTAG_ENABLE   0xD0  /* bRequest: enable JTAG */
119 #define CY7C65215_JTAG_DISABLE  0xD1  /* bRequest: disable JTAG */
120 #define CY7C65215_JTAG_READ     0xD2  /* bRequest: read buffer */
121 #define CY7C65215_JTAG_WRITE    0xD3  /* bRequest: write buffer */
122
123 #define CY7C65215_USB_TIMEOUT   100
124
125 static const uint16_t cy7c65215_vids[] = {0x04b4, 0};
126 static const uint16_t cy7c65215_pids[] = {0x0007, 0};
127
128 #define CY7C65215_JTAG_CLASS     0xff
129 #define CY7C65215_JTAG_SUBCLASS  0x04
130
131 static unsigned int ep_in, ep_out;
132
133 #ifdef _DEBUG_USB_COMMS_
134
135 #define DEBUG_TYPE_READ     0
136 #define DEBUG_TYPE_WRITE    1
137 #define DEBUG_TYPE_OCD_READ 2
138 #define DEBUG_TYPE_BUFFER   3
139
140 #define LINE_LEN  16
141 static void openjtag_debug_buffer(uint8_t *buffer, int length, uint8_t type)
142 {
143         char line[128];
144         char s[4];
145         int i;
146         int j;
147
148         switch (type) {
149                 case DEBUG_TYPE_READ:
150                         sprintf(line, "USB READ %d bytes", length);
151                         break;
152                 case DEBUG_TYPE_WRITE:
153                         sprintf(line, "USB WRITE %d bytes", length);
154                         break;
155                 case DEBUG_TYPE_OCD_READ:
156                         sprintf(line, "TO OpenOCD %d bytes", length);
157                         break;
158                 case DEBUG_TYPE_BUFFER:
159                         sprintf(line, "Buffer %d bytes", length);
160                         break;
161         }
162
163         LOG_DEBUG("%s", line);
164
165         for (i = 0; i < length; i += LINE_LEN) {
166                 switch (type) {
167                         case DEBUG_TYPE_READ:
168                                 sprintf(line, "USB READ: %04x", i);
169                                 break;
170                         case DEBUG_TYPE_WRITE:
171                                 sprintf(line, "USB WRITE: %04x", i);
172                                 break;
173                         case DEBUG_TYPE_OCD_READ:
174                                 sprintf(line, "TO OpenOCD: %04x", i);
175                                 break;
176                         case DEBUG_TYPE_BUFFER:
177                                 sprintf(line, "BUFFER: %04x", i);
178                                 break;
179                 }
180
181                 for (j = i; j < i + LINE_LEN && j < length; j++) {
182                         sprintf(s, " %02x", buffer[j]);
183                         strcat(line, s);
184                 }
185                 LOG_DEBUG("%s", line);
186         }
187
188 }
189
190 #endif
191
192 static int8_t openjtag_get_tap_state(int8_t state)
193 {
194
195         switch (state) {
196                 case TAP_DREXIT2:   return OPENJTAG_TAP_EXIT2_DR;
197                 case TAP_DREXIT1:   return OPENJTAG_TAP_EXIT1_DR;
198                 case TAP_DRSHIFT:   return OPENJTAG_TAP_SHIFT_DR;
199                 case TAP_DRPAUSE:   return OPENJTAG_TAP_PAUSE_DR;
200                 case TAP_IRSELECT:  return OPENJTAG_TAP_SELECT_IR;
201                 case TAP_DRUPDATE:  return OPENJTAG_TAP_UPDATE_DR;
202                 case TAP_DRCAPTURE: return OPENJTAG_TAP_CAPTURE_DR;
203                 case TAP_DRSELECT:  return OPENJTAG_TAP_SELECT_DR;
204                 case TAP_IREXIT2:   return OPENJTAG_TAP_EXIT2_IR;
205                 case TAP_IREXIT1:   return OPENJTAG_TAP_EXIT1_IR;
206                 case TAP_IRSHIFT:   return OPENJTAG_TAP_SHIFT_IR;
207                 case TAP_IRPAUSE:   return OPENJTAG_TAP_PAUSE_IR;
208                 case TAP_IDLE:      return OPENJTAG_TAP_IDLE;
209                 case TAP_IRUPDATE:  return OPENJTAG_TAP_UPDATE_IR;
210                 case TAP_IRCAPTURE: return OPENJTAG_TAP_CAPURE_IR;
211                 case TAP_RESET:     return OPENJTAG_TAP_RESET;
212                 case TAP_INVALID:
213                 default:            return OPENJTAG_TAP_INVALID;
214         }
215 }
216
217 static int openjtag_buf_write_standard(
218         uint8_t *buf, int size, uint32_t *bytes_written)
219 {
220         int retval;
221 #ifdef _DEBUG_USB_COMMS_
222         openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
223 #endif
224
225         retval = ftdi_write_data(&ftdic, buf, size);
226         if (retval < 0) {
227                 *bytes_written = 0;
228                 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
229                 return ERROR_JTAG_DEVICE_ERROR;
230         }
231
232         *bytes_written += retval;
233
234         return ERROR_OK;
235 }
236
237 static int openjtag_buf_write_cy7c65215(
238         uint8_t *buf, int size, uint32_t *bytes_written)
239 {
240         int ret;
241
242 #ifdef _DEBUG_USB_COMMS_
243         openjtag_debug_buffer(buf, size, DEBUG_TYPE_WRITE);
244 #endif
245
246         if (size == 0) {
247                 *bytes_written = 0;
248                 return ERROR_OK;
249         }
250
251         ret = jtag_libusb_control_transfer(usbh, CY7C65215_JTAG_REQUEST,
252                                                                            CY7C65215_JTAG_WRITE, size, 0,
253                                                                            NULL, 0, CY7C65215_USB_TIMEOUT);
254         if (ret < 0) {
255                 LOG_ERROR("vendor command failed, error %d", ret);
256                 return ERROR_JTAG_DEVICE_ERROR;
257         }
258
259         ret = jtag_libusb_bulk_write(usbh, ep_out, (char *)buf, size,
260                                                                  CY7C65215_USB_TIMEOUT);
261         if (ret < 0) {
262                 LOG_ERROR("bulk write failed, error %d", ret);
263                 return ERROR_JTAG_DEVICE_ERROR;
264         }
265         *bytes_written = ret;
266
267         return ERROR_OK;
268 }
269
270 static int openjtag_buf_write(
271         uint8_t *buf, int size, uint32_t *bytes_written)
272 {
273         switch (openjtag_variant) {
274         case OPENJTAG_VARIANT_CY7C65215:
275                 return openjtag_buf_write_cy7c65215(buf, size, bytes_written);
276         default:
277                 return openjtag_buf_write_standard(buf, size, bytes_written);
278         }
279 }
280
281 static int openjtag_buf_read_standard(
282         uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
283 {
284
285         int retval;
286         int timeout = 5;
287
288         *bytes_read = 0;
289
290         while ((*bytes_read < qty) && timeout--) {
291                 retval = ftdi_read_data(&ftdic, buf + *bytes_read,
292                                 qty - *bytes_read);
293                 if (retval < 0) {
294                         *bytes_read = 0;
295                         LOG_DEBUG_IO("ftdi_read_data: %s",
296                                         ftdi_get_error_string(&ftdic));
297                         return ERROR_JTAG_DEVICE_ERROR;
298                 }
299                 *bytes_read += retval;
300         }
301
302 #ifdef _DEBUG_USB_COMMS_
303         openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
304 #endif
305
306         return ERROR_OK;
307 }
308
309 static int openjtag_buf_read_cy7c65215(
310         uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
311 {
312         int ret;
313
314         if (qty == 0) {
315                 *bytes_read = 0;
316                 goto out;
317         }
318
319         ret = jtag_libusb_control_transfer(usbh, CY7C65215_JTAG_REQUEST,
320                                                                            CY7C65215_JTAG_READ, qty, 0,
321                                                                            NULL, 0, CY7C65215_USB_TIMEOUT);
322         if (ret < 0) {
323                 LOG_ERROR("vendor command failed, error %d", ret);
324                 return ERROR_JTAG_DEVICE_ERROR;
325         }
326
327         ret = jtag_libusb_bulk_read(usbh, ep_in, (char *)buf, qty,
328                                                                 CY7C65215_USB_TIMEOUT);
329         if (ret < 0) {
330                 LOG_ERROR("bulk read failed, error %d", ret);
331                 return ERROR_JTAG_DEVICE_ERROR;
332         }
333         *bytes_read = ret;
334
335 out:
336 #ifdef _DEBUG_USB_COMMS_
337         openjtag_debug_buffer(buf, *bytes_read, DEBUG_TYPE_READ);
338 #endif
339
340         return ERROR_OK;
341 }
342
343 static int openjtag_buf_read(uint8_t *buf, uint32_t qty, uint32_t *bytes_read)
344 {
345         switch (openjtag_variant) {
346         case OPENJTAG_VARIANT_CY7C65215:
347                 return openjtag_buf_read_cy7c65215(buf, qty, bytes_read);
348         default:
349                 return openjtag_buf_read_standard(buf, qty, bytes_read);
350         }
351 }
352
353 static int openjtag_sendcommand(uint8_t cmd)
354 {
355         uint32_t written;
356         return openjtag_buf_write(&cmd, 1, &written);
357 }
358
359 static int openjtag_speed(int speed)
360 {
361         int clockcmd;
362         switch (speed) {
363                 case 48000:
364                         clockcmd = 0x00;
365                         break;
366                 case 24000:
367                         clockcmd = 0x20;
368                         break;
369                 case 12000:
370                         clockcmd = 0x40;
371                         break;
372                 case 6000:
373                         clockcmd = 0x60;
374                         break;
375                 case 3000:
376                         clockcmd = 0x80;
377                         break;
378                 case 1500:
379                         clockcmd = 0xA0;
380                         break;
381                 case 750:
382                         clockcmd = 0xC0;
383                         break;
384                 case 375:
385                         clockcmd = 0xE0;
386                         break;
387                 default:
388                         clockcmd = 0xE0;
389                         LOG_WARNING("adapter speed not recognized, reverting to 375 kHz");
390                         break;
391         }
392         openjtag_sendcommand(clockcmd);
393
394         return ERROR_OK;
395 }
396
397 static int openjtag_init_standard(void)
398 {
399         uint8_t latency_timer;
400
401         /* Open by device description */
402         if (openjtag_device_desc == NULL) {
403                 LOG_WARNING("no openjtag device description specified, "
404                                 "using default 'Open JTAG Project'");
405                 openjtag_device_desc = "Open JTAG Project";
406         }
407
408         if (ftdi_init(&ftdic) < 0)
409                 return ERROR_JTAG_INIT_FAILED;
410
411         /* context, vendor id, product id, description, serial id */
412         if (ftdi_usb_open_desc(&ftdic, openjtag_vid, openjtag_pid, openjtag_device_desc, NULL) < 0) {
413                 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
414                 return ERROR_JTAG_INIT_FAILED;
415         }
416
417         if (ftdi_usb_reset(&ftdic) < 0) {
418                 LOG_ERROR("unable to reset ftdi device");
419                 return ERROR_JTAG_INIT_FAILED;
420         }
421
422         if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
423                 LOG_ERROR("unable to set latency timer");
424                 return ERROR_JTAG_INIT_FAILED;
425         }
426
427         if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
428                 LOG_ERROR("unable to get latency timer");
429                 return ERROR_JTAG_INIT_FAILED;
430         }
431         LOG_DEBUG("current latency timer: %u", latency_timer);
432
433         ftdi_disable_bitbang(&ftdic);
434         /* was (3000000 / 4) with a comment about a bug in libftdi when using high baudrate */
435         if (ftdi_set_baudrate(&ftdic, 3000000) < 0) {
436                 LOG_ERROR("Can't set baud rate to max: %s",
437                         ftdi_get_error_string(&ftdic));
438                 return ERROR_JTAG_DEVICE_ERROR;
439         }
440
441         if (ftdi_usb_purge_buffers(&ftdic) < 0) {
442                 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
443                 return ERROR_JTAG_INIT_FAILED;
444         }
445
446         return ERROR_OK;
447 }
448
449 static int openjtag_init_cy7c65215(void)
450 {
451         int ret;
452
453         usbh = NULL;
454         ret = jtag_libusb_open(cy7c65215_vids, cy7c65215_pids, NULL, &usbh);
455         if (ret != ERROR_OK) {
456                 LOG_ERROR("unable to open cy7c65215 device");
457                 goto err;
458         }
459
460         ret = jtag_libusb_choose_interface(usbh, &ep_in, &ep_out,
461                                                                            CY7C65215_JTAG_CLASS,
462                                                                            CY7C65215_JTAG_SUBCLASS, -1, LIBUSB_TRANSFER_TYPE_BULK);
463         if (ret != ERROR_OK) {
464                 LOG_ERROR("unable to claim JTAG interface");
465                 goto err;
466         }
467
468         ret = jtag_libusb_control_transfer(usbh,
469                                                                            CY7C65215_JTAG_REQUEST,
470                                                                            CY7C65215_JTAG_ENABLE,
471                                                                            0, 0, NULL, 0, CY7C65215_USB_TIMEOUT);
472         if (ret < 0) {
473                 LOG_ERROR("could not enable JTAG module");
474                 goto err;
475         }
476
477         return ERROR_OK;
478
479 err:
480         if (usbh != NULL)
481                 jtag_libusb_close(usbh);
482         return ERROR_JTAG_INIT_FAILED;
483 }
484
485 static int openjtag_init(void)
486 {
487         int ret;
488
489         usb_tx_buf_offs = 0;
490         usb_rx_buf_len = 0;
491         openjtag_scan_result_count = 0;
492
493         switch (openjtag_variant) {
494         case OPENJTAG_VARIANT_CY7C65215:
495                 ret = openjtag_init_cy7c65215();
496                 break;
497         default:
498                 ret = openjtag_init_standard();
499         }
500         if (ret != ERROR_OK)
501                 return ret;
502
503         openjtag_speed(375); /* Start at slowest adapter speed */
504         openjtag_sendcommand(0x75); /* MSB */
505
506         return ERROR_OK;
507 }
508
509 static int openjtag_quit_standard(void)
510 {
511         ftdi_usb_close(&ftdic);
512         ftdi_deinit(&ftdic);
513
514         return ERROR_OK;
515 }
516
517 static int openjtag_quit_cy7c65215(void)
518 {
519         int ret;
520
521         ret = jtag_libusb_control_transfer(usbh,
522                                                                            CY7C65215_JTAG_REQUEST,
523                                                                            CY7C65215_JTAG_DISABLE,
524                                                                            0, 0, NULL, 0, CY7C65215_USB_TIMEOUT);
525         if (ret < 0)
526                 LOG_WARNING("could not disable JTAG module");
527
528         jtag_libusb_close(usbh);
529
530         return ERROR_OK;
531 }
532
533 static int openjtag_quit(void)
534 {
535         switch (openjtag_variant) {
536         case OPENJTAG_VARIANT_CY7C65215:
537                 return openjtag_quit_cy7c65215();
538         default:
539                 return openjtag_quit_standard();
540         }
541 }
542
543 static void openjtag_write_tap_buffer(void)
544 {
545         uint32_t written;
546
547         openjtag_buf_write(usb_tx_buf, usb_tx_buf_offs, &written);
548         openjtag_buf_read(usb_rx_buf, usb_tx_buf_offs, &usb_rx_buf_len);
549
550         usb_tx_buf_offs = 0;
551 }
552
553 static int openjtag_execute_tap_queue(void)
554 {
555         openjtag_write_tap_buffer();
556
557         int res_count = 0;
558
559         if (openjtag_scan_result_count && usb_rx_buf_len) {
560
561                 int count;
562                 int rx_offs = 0;
563                 int len;
564
565                 /* for every pending result */
566                 while (res_count < openjtag_scan_result_count) {
567
568                         /* get sent bits */
569                         len = openjtag_scan_result_buffer[res_count].bits;
570
571                         count = 0;
572
573                         uint8_t *buffer = openjtag_scan_result_buffer[res_count].buffer;
574
575                         while (len > 0) {
576                                 if (len <= 8 && openjtag_variant != OPENJTAG_VARIANT_CY7C65215) {
577                                         LOG_DEBUG_IO("bits < 8 buf = 0x%X, will be 0x%X",
578                                                 usb_rx_buf[rx_offs], usb_rx_buf[rx_offs] >> (8 - len));
579                                         buffer[count] = usb_rx_buf[rx_offs] >> (8 - len);
580                                         len = 0;
581                                 } else {
582                                         buffer[count] = usb_rx_buf[rx_offs];
583                                         len -= 8;
584                                 }
585
586                                 rx_offs++;
587                                 count++;
588                         }
589
590 #ifdef _DEBUG_USB_COMMS_
591                         openjtag_debug_buffer(buffer,
592                                 DIV_ROUND_UP(openjtag_scan_result_buffer[res_count].bits, 8), DEBUG_TYPE_OCD_READ);
593 #endif
594                         jtag_read_buffer(buffer, openjtag_scan_result_buffer[res_count].command);
595
596                         if (openjtag_scan_result_buffer[res_count].buffer)
597                                 free(openjtag_scan_result_buffer[res_count].buffer);
598
599                         res_count++;
600                 }
601         }
602
603         openjtag_scan_result_count = 0;
604
605         return ERROR_OK;
606 }
607
608 static void openjtag_add_byte(char buf)
609 {
610
611         if (usb_tx_buf_offs == OPENJTAG_BUFFER_SIZE) {
612                 LOG_DEBUG_IO("Forcing execute_tap_queue");
613                 LOG_DEBUG_IO("TX Buff offs=%d", usb_tx_buf_offs);
614                 openjtag_execute_tap_queue();
615         }
616
617         usb_tx_buf[usb_tx_buf_offs] = buf;
618         usb_tx_buf_offs++;
619 }
620
621 static void openjtag_add_scan(uint8_t *buffer, int length, struct scan_command *scan_cmd)
622 {
623
624         /* Ensure space to send long chains */
625         /* We add two byte for each eight (or less) bits, one for command, one for data */
626         if (usb_tx_buf_offs + (DIV_ROUND_UP(length, 8) * 2) >= OPENJTAG_BUFFER_SIZE) {
627                 LOG_DEBUG_IO("Forcing execute_tap_queue from scan");
628                 LOG_DEBUG_IO("TX Buff offs=%d len=%d", usb_tx_buf_offs, DIV_ROUND_UP(length, 8) * 2);
629                 openjtag_execute_tap_queue();
630         }
631
632         openjtag_scan_result_buffer[openjtag_scan_result_count].bits = length;
633         openjtag_scan_result_buffer[openjtag_scan_result_count].command = scan_cmd;
634         openjtag_scan_result_buffer[openjtag_scan_result_count].buffer = buffer;
635
636         uint8_t command;
637         uint8_t bits;
638         int count = 0;
639         while (length) {
640
641                 /* write command */
642                 command = 6;
643
644                 /* last bits? */
645                 if (length <= 8) {
646                         /* tms high */
647                         command |= (1 << 4);
648
649                         /* bits to transfer */
650                         bits = (length - 1);
651                         command |= bits << 5;
652                         length = 0;
653                 } else {
654                         /* whole byte */
655
656                         /* bits to transfer */
657                         bits = 7;
658                         command |= (7 << 5);
659                         length -= 8;
660                 }
661
662                 openjtag_add_byte(command);
663                 openjtag_add_byte(buffer[count]);
664                 count++;
665         }
666
667         openjtag_scan_result_count++;
668 }
669
670 static void openjtag_execute_reset(struct jtag_command *cmd)
671 {
672
673         LOG_DEBUG_IO("reset trst: %i srst %i",
674                         cmd->cmd.reset->trst, cmd->cmd.reset->srst);
675
676         uint8_t buf = 0x00;
677
678         if (cmd->cmd.reset->trst) {
679                 buf = 0x03;
680         } else {
681                 buf |= 0x04;
682                 buf |= 0x05 << 4;
683         }
684
685         openjtag_add_byte(buf);
686 }
687
688 static void openjtag_execute_sleep(struct jtag_command *cmd)
689 {
690         jtag_sleep(cmd->cmd.sleep->us);
691 }
692
693 static void openjtag_set_state(uint8_t openocd_state)
694 {
695         int8_t state = openjtag_get_tap_state(openocd_state);
696
697         uint8_t buf = 0;
698         buf = 0x01;
699         buf |= state << 4;
700
701         openjtag_add_byte(buf);
702 }
703
704 static void openjtag_execute_statemove(struct jtag_command *cmd)
705 {
706         LOG_DEBUG_IO("state move to %i", cmd->cmd.statemove->end_state);
707
708         tap_set_end_state(cmd->cmd.statemove->end_state);
709
710         openjtag_set_state(cmd->cmd.statemove->end_state);
711
712         tap_set_state(tap_get_end_state());
713 }
714
715
716 static void openjtag_execute_scan(struct jtag_command *cmd)
717 {
718
719         int scan_size, old_state;
720         uint8_t *buffer;
721
722         LOG_DEBUG_IO("scan ends in %s", tap_state_name(cmd->cmd.scan->end_state));
723
724         /* get scan info */
725         tap_set_end_state(cmd->cmd.scan->end_state);
726         scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
727
728 #ifdef _DEBUG_USB_COMMS_
729         openjtag_debug_buffer(buffer, (scan_size + 7) / 8, DEBUG_TYPE_BUFFER);
730 #endif
731         /* set state */
732         old_state = tap_get_end_state();
733         openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
734         tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
735         tap_set_end_state(old_state);
736
737         openjtag_add_scan(buffer, scan_size, cmd->cmd.scan);
738
739         openjtag_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
740         tap_set_state(cmd->cmd.scan->ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
741
742         if (tap_get_state() != tap_get_end_state()) {
743                 openjtag_set_state(tap_get_end_state());
744                 tap_set_state(tap_get_end_state());
745         }
746 }
747
748 static void openjtag_execute_runtest(struct jtag_command *cmd)
749 {
750
751         tap_state_t end_state = cmd->cmd.runtest->end_state;
752         tap_set_end_state(end_state);
753
754         /* only do a state_move when we're not already in IDLE */
755         if (tap_get_state() != TAP_IDLE) {
756                 openjtag_set_state(TAP_IDLE);
757                 tap_set_state(TAP_IDLE);
758         }
759
760         if (cmd->cmd.runtest->num_cycles > 16)
761                 LOG_WARNING("num_cycles > 16 on run test");
762
763         if (openjtag_variant != OPENJTAG_VARIANT_CY7C65215 ||
764                 cmd->cmd.runtest->num_cycles) {
765                 uint8_t command;
766                 command = 7;
767                 command |= ((cmd->cmd.runtest->num_cycles - 1) & 0x0F) << 4;
768
769                 openjtag_add_byte(command);
770         }
771
772         tap_set_end_state(end_state);
773         if (tap_get_end_state() != tap_get_state()) {
774                 openjtag_set_state(end_state);
775                 tap_set_state(end_state);
776         }
777 }
778
779 static void openjtag_execute_command(struct jtag_command *cmd)
780 {
781         LOG_DEBUG_IO("openjtag_execute_command %i", cmd->type);
782         switch (cmd->type) {
783         case JTAG_RESET:
784                         openjtag_execute_reset(cmd);
785                         break;
786         case JTAG_SLEEP:
787                         openjtag_execute_sleep(cmd);
788                         break;
789         case JTAG_TLR_RESET:
790                         openjtag_execute_statemove(cmd);
791                         break;
792         case JTAG_SCAN:
793                         openjtag_execute_scan(cmd);
794                         break;
795         case JTAG_RUNTEST:
796                         openjtag_execute_runtest(cmd);
797                         break;
798         case JTAG_PATHMOVE:
799                 /* jlink_execute_pathmove(cmd); break; */
800         default:
801                 LOG_ERROR("BUG: unknown Open JTAG command type encountered");
802                 exit(-1);
803         }
804 }
805
806 static int openjtag_execute_queue(void)
807 {
808         struct jtag_command *cmd = jtag_command_queue;
809
810         while (cmd != NULL) {
811                 openjtag_execute_command(cmd);
812                 cmd = cmd->next;
813         }
814
815         return openjtag_execute_tap_queue();
816 }
817
818 static int openjtag_speed_div(int speed, int *khz)
819 {
820         *khz = speed;
821
822         return ERROR_OK;
823 }
824
825 static int openjtag_khz(int khz, int *jtag_speed)
826 {
827
828         if (khz >= 48000)
829                 *jtag_speed = 48000;
830         else if (khz >= 24000)
831                 *jtag_speed = 24000;
832         else if (khz >= 12000)
833                 *jtag_speed = 12000;
834         else if (khz >= 6000)
835                 *jtag_speed = 6000;
836         else if (khz >= 3000)
837                 *jtag_speed = 3000;
838         else if (khz >= 1500)
839                 *jtag_speed = 1500;
840         else if (khz >= 750)
841                 *jtag_speed = 750;
842         else
843                 *jtag_speed = 375;
844
845         return ERROR_OK;
846 }
847
848 COMMAND_HANDLER(openjtag_handle_device_desc_command)
849 {
850         if (CMD_ARGC == 1)
851                 openjtag_device_desc = strdup(CMD_ARGV[0]);
852         else
853                 LOG_ERROR("require exactly one argument to "
854                                   "openjtag_device_desc <description>");
855         return ERROR_OK;
856 }
857
858 COMMAND_HANDLER(openjtag_handle_variant_command)
859 {
860         if (CMD_ARGC == 1) {
861                 const char * const *name = openjtag_variant_names;
862                 int variant = 0;
863                 for (; *name; name++, variant++) {
864                         if (strcasecmp(CMD_ARGV[0], *name) == 0) {
865                                 openjtag_variant = variant;
866                                 return ERROR_OK;
867                         }
868                 }
869                 LOG_ERROR("unknown openjtag variant '%s'", CMD_ARGV[0]);
870         } else {
871                 LOG_ERROR("require exactly one argument to "
872                                 "openjtag_variant <variant>");
873         }
874         return ERROR_OK;
875 }
876
877 static const struct command_registration openjtag_command_handlers[] = {
878         {
879                 .name = "openjtag_device_desc",
880                 .handler = openjtag_handle_device_desc_command,
881                 .mode = COMMAND_CONFIG,
882                 .help = "set the USB device description of the OpenJTAG",
883                 .usage = "description-string",
884         },
885         {
886                 .name = "openjtag_variant",
887                 .handler = openjtag_handle_variant_command,
888                 .mode = COMMAND_CONFIG,
889                 .help = "set the OpenJTAG variant",
890                 .usage = "variant-string",
891         },
892         COMMAND_REGISTRATION_DONE
893 };
894
895 static struct jtag_interface openjtag_interface = {
896         .execute_queue = openjtag_execute_queue,
897 };
898
899 struct adapter_driver openjtag_adapter_driver = {
900         .name = "openjtag",
901         .transports = jtag_only,
902         .commands = openjtag_command_handlers,
903
904         .init = openjtag_init,
905         .quit = openjtag_quit,
906         .speed = openjtag_speed,
907         .khz = openjtag_khz,
908         .speed_div = openjtag_speed_div,
909
910         .jtag_ops = &openjtag_interface,
911 };