OpenOCD
ft2232.c
Go to the documentation of this file.
1 /***************************************************************************
2 * Copyright (C) 2009 by Øyvind Harboe *
3 * Øyvind Harboe <oyvind.harboe@zylin.com> *
4 * *
5 * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
6 * Dick Hollenbeck <dick@softplc.com> *
7 * *
8 * Copyright (C) 2004, 2006 by Dominic Rath *
9 * Dominic.Rath@gmx.de *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
28 ***************************************************************************/
29 
78 #ifdef HAVE_CONFIG_H
79 #include "config.h"
80 #endif
81 
82 /* project specific includes */
83 #include <jtag/interface.h>
84 #include <transport/transport.h>
85 #include <helper/time_support.h>
86 
87 #if IS_CYGWIN == 1
88 #include <windows.h>
89 #endif
90 
91 #include <assert.h>
92 
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
97 #endif
98 
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
101 #include <ftd2xx.h>
102 #include "ftd2xx_common.h"
103 
104 enum ftdi_interface {
105  INTERFACE_ANY = 0,
106  INTERFACE_A = 1,
107  INTERFACE_B = 2,
108  INTERFACE_C = 3,
109  INTERFACE_D = 4
110 };
111 
112 #elif BUILD_FT2232_LIBFTDI == 1
113 #include <ftdi.h>
114 #endif
115 
116 /* max TCK for the high speed devices 30000 kHz */
117 #define FTDI_x232H_MAX_TCK 30000
118 /* max TCK for the full speed devices 6000 kHz */
119 #define FTDI_2232C_MAX_TCK 6000
120 /* this speed value tells that RTCK is requested */
121 #define RTCK_SPEED -1
122 
123 /*
124  * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
125  * errors with a retry count of 100. Increasing it solves the problem for me.
126  * - Dimitar
127  *
128  * FIXME There's likely an issue with the usb_read_timeout from libftdi.
129  * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
130  * to something sane.
131  */
132 #define LIBFTDI_READ_RETRY_COUNT 2000
133 
134 #ifndef BUILD_FT2232_HIGHSPEED
135  #if BUILD_FT2232_FTD2XX == 1
136  enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H, FT_DEVICE_232H };
137  #elif BUILD_FT2232_LIBFTDI == 1
138  enum ftdi_chip_type { TYPE_2232H = 4, TYPE_4232H = 5, TYPE_232H = 6 };
139  #endif
140 #endif
141 
152 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd);
153 
154 static char *ft2232_device_desc_A;
155 static char *ft2232_device_desc;
156 static char *ft2232_serial;
157 static uint8_t ft2232_latency = 2;
159 static int ft2232_channel = INTERFACE_ANY;
160 
161 #define MAX_USB_IDS 8
162 /* vid = pid = 0 marks the end of the list */
163 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
164 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
165 
167  const char *name;
168  int (*init)(void);
169  void (*reset)(int trst, int srst);
170  void (*blink)(void);
171  int channel;
172 };
173 
174 /* init procedures for supported layouts */
175 static int usbjtag_init(void);
176 static int jtagkey_init(void);
177 static int lm3s811_jtag_init(void);
178 static int icdi_jtag_init(void);
179 static int olimex_jtag_init(void);
180 static int flyswatter1_init(void);
181 static int flyswatter2_init(void);
182 static int minimodule_init(void);
183 static int turtle_init(void);
184 static int comstick_init(void);
185 static int stm32stick_init(void);
186 static int axm0432_jtag_init(void);
187 static int sheevaplug_init(void);
188 static int icebear_jtag_init(void);
189 static int cortino_jtag_init(void);
190 static int signalyzer_init(void);
191 static int signalyzer_h_init(void);
192 static int ktlink_init(void);
193 static int redbee_init(void);
194 static int lisa_l_init(void);
195 static int flossjtag_init(void);
196 static int xds100v2_init(void);
197 static int digilent_hs1_init(void);
198 
199 /* reset procedures for supported layouts */
200 static void ftx23_reset(int trst, int srst);
201 static void jtagkey_reset(int trst, int srst);
202 static void olimex_jtag_reset(int trst, int srst);
203 static void flyswatter1_reset(int trst, int srst);
204 static void flyswatter2_reset(int trst, int srst);
205 static void minimodule_reset(int trst, int srst);
206 static void turtle_reset(int trst, int srst);
207 static void comstick_reset(int trst, int srst);
208 static void stm32stick_reset(int trst, int srst);
209 static void axm0432_jtag_reset(int trst, int srst);
210 static void sheevaplug_reset(int trst, int srst);
211 static void icebear_jtag_reset(int trst, int srst);
212 static void signalyzer_h_reset(int trst, int srst);
213 static void ktlink_reset(int trst, int srst);
214 static void redbee_reset(int trst, int srst);
215 static void xds100v2_reset(int trst, int srst);
216 static void digilent_hs1_reset(int trst, int srst);
217 
218 /* blink procedures for layouts that support a blinking led */
219 static void olimex_jtag_blink(void);
220 static void flyswatter1_jtag_blink(void);
221 static void flyswatter2_jtag_blink(void);
222 static void turtle_jtag_blink(void);
223 static void signalyzer_h_blink(void);
224 static void ktlink_blink(void);
225 static void lisa_l_blink(void);
226 static void flossjtag_blink(void);
227 
228 /* common transport support options */
229 
230 /* static const char *jtag_and_swd[] = { "jtag", "swd", NULL }; */
231 
232 static const struct ft2232_layout ft2232_layouts[] = {
233  { .name = "usbjtag",
234  .init = usbjtag_init,
235  .reset = ftx23_reset,
236  },
237  { .name = "jtagkey",
238  .init = jtagkey_init,
239  .reset = jtagkey_reset,
240  },
241  { .name = "jtagkey_prototype_v1",
242  .init = jtagkey_init,
243  .reset = jtagkey_reset,
244  },
245  { .name = "oocdlink",
246  .init = jtagkey_init,
247  .reset = jtagkey_reset,
248  },
249  { .name = "signalyzer",
250  .init = signalyzer_init,
251  .reset = ftx23_reset,
252  },
253  { .name = "evb_lm3s811",
254  .init = lm3s811_jtag_init,
255  .reset = ftx23_reset,
256  },
257  { .name = "luminary_icdi",
258  .init = icdi_jtag_init,
259  .reset = ftx23_reset,
260  },
261  { .name = "olimex-jtag",
262  .init = olimex_jtag_init,
263  .reset = olimex_jtag_reset,
264  .blink = olimex_jtag_blink
265  },
266  { .name = "flyswatter",
267  .init = flyswatter1_init,
268  .reset = flyswatter1_reset,
269  .blink = flyswatter1_jtag_blink
270  },
271  { .name = "flyswatter2",
272  .init = flyswatter2_init,
273  .reset = flyswatter2_reset,
274  .blink = flyswatter2_jtag_blink
275  },
276  { .name = "minimodule",
277  .init = minimodule_init,
278  .reset = minimodule_reset,
279  },
280  { .name = "turtelizer2",
281  .init = turtle_init,
282  .reset = turtle_reset,
283  .blink = turtle_jtag_blink
284  },
285  { .name = "comstick",
286  .init = comstick_init,
287  .reset = comstick_reset,
288  },
289  { .name = "stm32stick",
290  .init = stm32stick_init,
291  .reset = stm32stick_reset,
292  },
293  { .name = "axm0432_jtag",
294  .init = axm0432_jtag_init,
295  .reset = axm0432_jtag_reset,
296  },
297  { .name = "sheevaplug",
298  .init = sheevaplug_init,
299  .reset = sheevaplug_reset,
300  },
301  { .name = "icebear",
302  .init = icebear_jtag_init,
303  .reset = icebear_jtag_reset,
304  },
305  { .name = "cortino",
306  .init = cortino_jtag_init,
307  .reset = comstick_reset,
308  },
309  { .name = "signalyzer-h",
310  .init = signalyzer_h_init,
311  .reset = signalyzer_h_reset,
312  .blink = signalyzer_h_blink
313  },
314  { .name = "ktlink",
315  .init = ktlink_init,
316  .reset = ktlink_reset,
317  .blink = ktlink_blink
318  },
319  { .name = "redbee-econotag",
320  .init = redbee_init,
321  .reset = redbee_reset,
322  },
323  { .name = "redbee-usb",
324  .init = redbee_init,
325  .reset = redbee_reset,
326  .channel = INTERFACE_B,
327  },
328  { .name = "lisa-l",
329  .init = lisa_l_init,
330  .reset = ftx23_reset,
331  .blink = lisa_l_blink,
332  .channel = INTERFACE_B,
333  },
334  { .name = "flossjtag",
335  .init = flossjtag_init,
336  .reset = ftx23_reset,
337  .blink = flossjtag_blink,
338  },
339  { .name = "xds100v2",
340  .init = xds100v2_init,
341  .reset = xds100v2_reset,
342  },
343  { .name = "digilent-hs1",
344  .init = digilent_hs1_init,
345  .reset = digilent_hs1_reset,
346  .channel = INTERFACE_A,
347  },
348  { .name = NULL, /* END OF TABLE */ },
349 };
350 
351 /* bitmask used to drive nTRST; usually a GPIOLx signal */
352 static uint8_t nTRST;
353 static uint8_t nTRSTnOE;
354 /* bitmask used to drive nSRST; usually a GPIOLx signal */
355 static uint8_t nSRST;
356 static uint8_t nSRSTnOE;
357 
359 static const struct ft2232_layout *layout;
360 
362 static uint8_t low_output;
363 
364 /* note that direction bit == 1 means that signal is an output */
365 
367 static uint8_t low_direction;
369 static uint8_t high_output;
371 static uint8_t high_direction;
372 
373 #if BUILD_FT2232_FTD2XX == 1
374 static FT_HANDLE ftdih;
375 static FT_DEVICE ftdi_device;
376 #elif BUILD_FT2232_LIBFTDI == 1
377 static struct ftdi_context ftdic;
378 static enum ftdi_chip_type ftdi_device;
379 #endif
380 
381 static struct jtag_command *first_unsent; /* next command that has to be sent */
382 static int require_send;
383 
384 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
385 
386  "There is a significant difference between libftdi and libftd2xx. The latter
387  one allows to schedule up to 64*64 bytes of result data while libftdi fails
388  with more than 4*64. As a consequence, the FT2232 driver is forced to
389  perform around 16x more USB transactions for long command streams with TDO
390  capture when running with libftdi."
391 
392  No idea how we get
393  #define FT2232_BUFFER_SIZE 131072
394  a comment would have been nice.
395 */
396 
397 #if BUILD_FT2232_FTD2XX == 1
398 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
399 #else
400 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
401 #endif
402 
403 #define FT2232_BUFFER_SIZE 131072
404 
405 static uint8_t *ft2232_buffer;
409 
415 static inline void buffer_write(uint8_t val)
416 {
417  assert(ft2232_buffer);
418  assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
420 }
421 
426 static inline uint8_t buffer_read(void)
427 {
428  assert(ft2232_buffer);
431 }
432 
450 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
451 {
452  uint8_t tms_byte;
453  int i;
454  int tms_ndx; /* bit index into tms_byte */
455 
456  assert(tms_count > 0);
457 
458  DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
459  mpsse_cmd, tms_bits, tms_count);
460 
461  for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits >>= 1) {
462  bool bit = tms_bits & 1;
463 
464  if (bit)
465  tms_byte |= (1 << tms_ndx);
466 
467  /* always do state transitions in public view */
469 
470  /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
471  * also increment.
472  */
473  ++tms_ndx;
474 
475  if (tms_ndx == 7 || i == tms_count-1) {
476  buffer_write(mpsse_cmd);
477  buffer_write(tms_ndx - 1);
478 
479  /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
480  * TMS/CS and is held static for the duration of TMS/CS clocking.
481  */
482  buffer_write(tms_byte | (tdi_bit << 7));
483  }
484  }
485 }
486 
492 static inline int get_tms_buffer_requirements(int bit_count)
493 {
494  return ((bit_count + 6)/7) * 3;
495 }
496 
505 static void move_to_state(tap_state_t goal_state)
506 {
507  tap_state_t start_state = tap_get_state();
508 
509  /* goal_state is 1/2 of a tuple/pair of states which allow convenient
510  * lookup of the required TMS pattern to move to this state from the start state.
511  */
512 
513  /* do the 2 lookups */
514  int tms_bits = tap_get_tms_path(start_state, goal_state);
515  int tms_count = tap_get_tms_path_len(start_state, goal_state);
516 
517  DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
518 
519  clock_tms(0x4b, tms_bits, tms_count, 0);
520 }
521 
522 static int ft2232_write(uint8_t *buf, int size, uint32_t *bytes_written)
523 {
524 #if BUILD_FT2232_FTD2XX == 1
525  FT_STATUS status;
526  DWORD dw_bytes_written = 0;
527  status = FT_Write(ftdih, buf, size, &dw_bytes_written);
528  if (status != FT_OK) {
529  *bytes_written = dw_bytes_written;
530  LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
532  } else
533  *bytes_written = dw_bytes_written;
534 
535 #elif BUILD_FT2232_LIBFTDI == 1
536  int retval = ftdi_write_data(&ftdic, buf, size);
537  if (retval < 0) {
538  *bytes_written = 0;
539  LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
541  } else
542  *bytes_written = retval;
543 
544 #endif
545 
546  if (*bytes_written != (uint32_t)size)
548 
549  return ERROR_OK;
550 }
551 
552 static int ft2232_read(uint8_t *buf, uint32_t size, uint32_t *bytes_read)
553 {
554 #if BUILD_FT2232_FTD2XX == 1
555  DWORD dw_bytes_read;
556  FT_STATUS status;
557  int timeout = 5;
558  *bytes_read = 0;
559 
560  while ((*bytes_read < size) && timeout--) {
561  status = FT_Read(ftdih, buf + *bytes_read, size -
562  *bytes_read, &dw_bytes_read);
563  if (status != FT_OK) {
564  *bytes_read = 0;
565  LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
567  }
568  *bytes_read += dw_bytes_read;
569  }
570 
571 #elif BUILD_FT2232_LIBFTDI == 1
572  int retval;
573  int timeout = LIBFTDI_READ_RETRY_COUNT;
574  *bytes_read = 0;
575 
576  while ((*bytes_read < size) && timeout--) {
577  retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read);
578  if (retval < 0) {
579  *bytes_read = 0;
580  LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
582  }
583  *bytes_read += retval;
584  }
585 
586 #endif
587 
588  if (*bytes_read < size) {
589  LOG_ERROR("couldn't read enough bytes from "
590  "FT2232 device (%i < %i)",
591  (unsigned)*bytes_read,
592  (unsigned)size);
594  }
595 
596  return ERROR_OK;
597 }
598 
599 static bool ft2232_device_is_highspeed(void)
600 {
601 #if BUILD_FT2232_FTD2XX == 1
602  return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H)
603  #ifdef HAS_ENUM_FT232H
604  || (ftdi_device == FT_DEVICE_232H)
605  #endif
606  ;
607 #elif BUILD_FT2232_LIBFTDI == 1
608  return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H
609  #ifdef HAS_ENUM_FT232H
610  || ftdi_device == TYPE_232H
611  #endif
612  );
613 #endif
614 }
615 
616 /*
617  * Commands that only apply to the highspeed FTx232H devices (FT2232H, FT4232H, FT232H).
618  * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
619  * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
620  */
621 
622 static int ftx232h_adaptive_clocking(bool enable)
623 {
624  uint8_t buf = enable ? 0x96 : 0x97;
625  LOG_DEBUG("%2.2x", buf);
626 
627  uint32_t bytes_written;
628  int retval;
629 
630  retval = ft2232_write(&buf, sizeof(buf), &bytes_written);
631  if (retval != ERROR_OK) {
632  LOG_ERROR("couldn't write command to %s adaptive clocking"
633  , enable ? "enable" : "disable");
634  return retval;
635  }
636 
637  return ERROR_OK;
638 }
639 
645 static int ftx232h_clk_divide_by_5(bool enable)
646 {
647  uint32_t bytes_written;
648  uint8_t buf = enable ? 0x8b : 0x8a;
649 
650  if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK) {
651  LOG_ERROR("couldn't write command to %s clk divide by 5"
652  , enable ? "enable" : "disable");
653  return ERROR_JTAG_INIT_FAILED;
654  }
656  LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
657 
658  return ERROR_OK;
659 }
660 
661 static int ft2232_speed(int speed)
662 {
663  uint8_t buf[3];
664  int retval;
665  uint32_t bytes_written;
666 
667  retval = ERROR_OK;
668  bool enable_adaptive_clocking = (RTCK_SPEED == speed);
670  retval = ftx232h_adaptive_clocking(enable_adaptive_clocking);
671  else if (enable_adaptive_clocking) {
672  LOG_ERROR("ft2232 device %lu does not support RTCK"
673  , (long unsigned int)ftdi_device);
674  return ERROR_FAIL;
675  }
676 
677  if ((enable_adaptive_clocking) || (ERROR_OK != retval))
678  return retval;
679 
680  buf[0] = 0x86; /* command "set divisor" */
681  buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
682  buf[2] = (speed >> 8) & 0xff; /* valueH */
683 
684  LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
685  retval = ft2232_write(buf, sizeof(buf), &bytes_written);
686  if (retval != ERROR_OK) {
687  LOG_ERROR("couldn't set FT2232 TCK speed");
688  return retval;
689  }
690 
691  return ERROR_OK;
692 }
693 
694 static int ft2232_speed_div(int speed, int *khz)
695 {
696  /* Take a look in the FT2232 manual,
697  * AN2232C-01 Command Processor for
698  * MPSSE and MCU Host Bus. Chapter 3.8 */
699 
700  *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
701 
702  return ERROR_OK;
703 }
704 
705 static int ft2232_khz(int khz, int *jtag_speed)
706 {
707  if (khz == 0) {
709  *jtag_speed = RTCK_SPEED;
710  return ERROR_OK;
711  } else {
712  LOG_DEBUG("RCLK not supported");
713  return ERROR_FAIL;
714  }
715  }
716 
717  /* Take a look in the FT2232 manual,
718  * AN2232C-01 Command Processor for
719  * MPSSE and MCU Host Bus. Chapter 3.8
720  *
721  * We will calc here with a multiplier
722  * of 10 for better rounding later. */
723 
724  /* Calc speed, (ft2232_max_tck / khz) - 1
725  * Use 65000 for better rounding */
726  *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
727 
728  /* Add 0.9 for rounding */
729  *jtag_speed += 9;
730 
731  /* Calc real speed */
732  *jtag_speed = *jtag_speed / 10;
733 
734  /* Check if speed is greater than 0 */
735  if (*jtag_speed < 0)
736  *jtag_speed = 0;
737 
738  /* Check max value */
739  if (*jtag_speed > 0xFFFF)
740  *jtag_speed = 0xFFFF;
741 
742  return ERROR_OK;
743 }
744 
745 static void ft2232_end_state(tap_state_t state)
746 {
747  if (tap_is_state_stable(state))
748  tap_set_end_state(state);
749  else {
750  LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
751  exit(-1);
752  }
753 }
754 
755 static void ft2232_read_scan(enum scan_type type, uint8_t *buffer, int scan_size)
756 {
757  int num_bytes = (scan_size + 7) / 8;
758  int bits_left = scan_size;
759  int cur_byte = 0;
760 
761  while (num_bytes-- > 1) {
762  buffer[cur_byte++] = buffer_read();
763  bits_left -= 8;
764  }
765 
766  buffer[cur_byte] = 0x0;
767 
768  /* There is one more partial byte left from the clock data in/out instructions */
769  if (bits_left > 1)
770  buffer[cur_byte] = buffer_read() >> 1;
771  /* This shift depends on the length of the
772  *clock data to tms instruction, insterted
773  *at end of the scan, now fixed to a two
774  *step transition in ft2232_add_scan */
775  buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
776 }
777 
778 static void ft2232_debug_dump_buffer(void)
779 {
780  int i;
781  char line[256];
782  char *line_p = line;
783 
784  for (i = 0; i < ft2232_buffer_size; i++) {
785  line_p += snprintf(line_p,
786  sizeof(line) - (line_p - line),
787  "%2.2x ",
788  ft2232_buffer[i]);
789  if (i % 16 == 15) {
790  LOG_DEBUG("%s", line);
791  line_p = line;
792  }
793  }
794 
795  if (line_p != line)
796  LOG_DEBUG("%s", line);
797 }
798 
799 static int ft2232_send_and_recv(struct jtag_command *first, struct jtag_command *last)
800 {
801  struct jtag_command *cmd;
802  uint8_t *buffer;
803  int scan_size;
804  enum scan_type type;
805  int retval;
806  uint32_t bytes_written = 0;
807  uint32_t bytes_read = 0;
808 
809 #ifdef _DEBUG_USB_IO_
810  struct timeval start, inter, inter2, end;
811  struct timeval d_inter, d_inter2, d_end;
812 #endif
813 
814 #ifdef _DEBUG_USB_COMMS_
815  LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
817 #endif
818 
819 #ifdef _DEBUG_USB_IO_
820  gettimeofday(&start, NULL);
821 #endif
822 
823  retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
824  if (retval != ERROR_OK) {
825  LOG_ERROR("couldn't write MPSSE commands to FT2232");
826  return retval;
827  }
828 
829 #ifdef _DEBUG_USB_IO_
830  gettimeofday(&inter, NULL);
831 #endif
832 
833  if (ft2232_expect_read) {
834  /* FIXME this "timeout" is never changed ... */
835  int timeout = LIBFTDI_READ_RETRY_COUNT;
836  ft2232_buffer_size = 0;
837 
838 #ifdef _DEBUG_USB_IO_
839  gettimeofday(&inter2, NULL);
840 #endif
841 
842  retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read);
843  if (retval != ERROR_OK) {
844  LOG_ERROR("couldn't read from FT2232");
845  return retval;
846  }
847 
848 #ifdef _DEBUG_USB_IO_
849  gettimeofday(&end, NULL);
850 
851  timeval_subtract(&d_inter, &inter, &start);
852  timeval_subtract(&d_inter2, &inter2, &start);
853  timeval_subtract(&d_end, &end, &start);
854 
855  LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
856  (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
857  (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
858  (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
859 #endif
860 
861  ft2232_buffer_size = bytes_read;
862 
864  LOG_ERROR("ft2232_expect_read (%i) != "
865  "ft2232_buffer_size (%i) "
866  "(%i retries)",
869  LIBFTDI_READ_RETRY_COUNT - timeout);
871 
872  exit(-1);
873  }
874 
875 #ifdef _DEBUG_USB_COMMS_
876  LOG_DEBUG("read buffer (%i retries): %i bytes",
877  LIBFTDI_READ_RETRY_COUNT - timeout,
880 #endif
881  }
882 
883  ft2232_expect_read = 0;
885 
886  /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
887  * that wasn't handled by a caller-provided error handler
888  */
889  retval = ERROR_OK;
890 
891  cmd = first;
892  while (cmd != last) {
893  switch (cmd->type) {
894  case JTAG_SCAN:
895  type = jtag_scan_type(cmd->cmd.scan);
896  if (type != SCAN_OUT) {
897  scan_size = jtag_scan_size(cmd->cmd.scan);
898  buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
899  ft2232_read_scan(type, buffer, scan_size);
900  if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
901  retval = ERROR_JTAG_QUEUE_FAILED;
902  free(buffer);
903  }
904  break;
905 
906  default:
907  break;
908  }
909 
910  cmd = cmd->next;
911  }
912 
913  ft2232_buffer_size = 0;
914 
915  return retval;
916 }
917 
927 static void ft2232_add_pathmove(tap_state_t *path, int num_states)
928 {
929  int state_count = 0;
930 
931  assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
932 
933  DEBUG_JTAG_IO("-");
934 
935  /* this loop verifies that the path is legal and logs each state in the path */
936  while (num_states) {
937  unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
938  int bit_count = 0;
939  int num_states_batch = num_states > 7 ? 7 : num_states;
940 
941  /* command "Clock Data to TMS/CS Pin (no Read)" */
942  buffer_write(0x4b);
943 
944  /* number of states remaining */
945  buffer_write(num_states_batch - 1);
946 
947  while (num_states_batch--) {
948  /* either TMS=0 or TMS=1 must work ... */
949  if (tap_state_transition(tap_get_state(), false) == path[state_count])
950  buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
951  else if (tap_state_transition(tap_get_state(), true) == path[state_count])
952  buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
953 
954  /* ... or else the caller goofed BADLY */
955  else {
956  LOG_ERROR("BUG: %s -> %s isn't a valid "
957  "TAP state transition",
959  tap_state_name(path[state_count]));
960  exit(-1);
961  }
962 
963  tap_set_state(path[state_count]);
964  state_count++;
965  num_states--;
966  }
967 
968  buffer_write(tms_byte);
969  }
971 }
972 
973 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size)
974 {
975  int num_bytes = (scan_size + 7) / 8;
976  int bits_left = scan_size;
977  int cur_byte = 0;
978  int last_bit;
979 
980  if (!ir_scan) {
981  if (tap_get_state() != TAP_DRSHIFT)
983  } else {
984  if (tap_get_state() != TAP_IRSHIFT)
986  }
987 
988  /* add command for complete bytes */
989  while (num_bytes > 1) {
990  int thisrun_bytes;
991  if (type == SCAN_IO) {
992  /* Clock Data Bytes In and Out LSB First */
993  buffer_write(0x39);
994  /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
995  } else if (type == SCAN_OUT) {
996  /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
997  buffer_write(0x19);
998  /* LOG_DEBUG("added TDI bytes (o)"); */
999  } else if (type == SCAN_IN) {
1000  /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1001  buffer_write(0x28);
1002  /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1003  }
1004 
1005  thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1006  num_bytes -= thisrun_bytes;
1007 
1008  buffer_write((uint8_t) (thisrun_bytes - 1));
1009  buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1010 
1011  if (type != SCAN_IN) {
1012  /* add complete bytes */
1013  while (thisrun_bytes-- > 0) {
1014  buffer_write(buffer[cur_byte++]);
1015  bits_left -= 8;
1016  }
1017  } else /* (type == SCAN_IN) */
1018  bits_left -= 8 * (thisrun_bytes);
1019  }
1020 
1021  /* the most signifcant bit is scanned during TAP movement */
1022  if (type != SCAN_IN)
1023  last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1024  else
1025  last_bit = 0;
1026 
1027  /* process remaining bits but the last one */
1028  if (bits_left > 1) {
1029  if (type == SCAN_IO) {
1030  /* Clock Data Bits In and Out LSB First */
1031  buffer_write(0x3b);
1032  /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1033  } else if (type == SCAN_OUT) {
1034  /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1035  buffer_write(0x1b);
1036  /* LOG_DEBUG("added TDI bits (o)"); */
1037  } else if (type == SCAN_IN) {
1038  /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1039  buffer_write(0x2a);
1040  /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1041  }
1042 
1043  buffer_write(bits_left - 2);
1044  if (type != SCAN_IN)
1045  buffer_write(buffer[cur_byte]);
1046  }
1047 
1048  if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1049  || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT))) {
1050  if (type == SCAN_IO) {
1051  /* Clock Data Bits In and Out LSB First */
1052  buffer_write(0x3b);
1053  /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1054  } else if (type == SCAN_OUT) {
1055  /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1056  buffer_write(0x1b);
1057  /* LOG_DEBUG("added TDI bits (o)"); */
1058  } else if (type == SCAN_IN) {
1059  /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1060  buffer_write(0x2a);
1061  /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1062  }
1063  buffer_write(0x0);
1064  if (type != SCAN_IN)
1065  buffer_write(last_bit);
1066  } else {
1067  int tms_bits;
1068  int tms_count;
1069  uint8_t mpsse_cmd;
1070 
1071  /* move from Shift-IR/DR to end state */
1072  if (type != SCAN_OUT) {
1073  /* We always go to the PAUSE state in two step at the end of an IN or IO
1074  *scan
1075  * This must be coordinated with the bit shifts in ft2232_read_scan */
1076  tms_bits = 0x01;
1077  tms_count = 2;
1078  /* Clock Data to TMS/CS Pin with Read */
1079  mpsse_cmd = 0x6b;
1080  } else {
1083  /* Clock Data to TMS/CS Pin (no Read) */
1084  mpsse_cmd = 0x4b;
1085  }
1086 
1087  DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1088  clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1089  }
1090 
1091  if (tap_get_state() != tap_get_end_state())
1093 }
1094 
1095 static int ft2232_large_scan(struct scan_command *cmd,
1096  enum scan_type type,
1097  uint8_t *buffer,
1098  int scan_size)
1099 {
1100  int num_bytes = (scan_size + 7) / 8;
1101  int bits_left = scan_size;
1102  int cur_byte = 0;
1103  int last_bit;
1104  uint8_t *receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1105  uint8_t *receive_pointer = receive_buffer;
1106  uint32_t bytes_written;
1107  uint32_t bytes_read;
1108  int retval;
1109  int thisrun_read = 0;
1110 
1111  if (!receive_buffer) {
1112  LOG_ERROR("failed to allocate memory");
1113  exit(-1);
1114  }
1115 
1116  if (cmd->ir_scan) {
1117  LOG_ERROR("BUG: large IR scans are not supported");
1118  exit(-1);
1119  }
1120 
1121  if (tap_get_state() != TAP_DRSHIFT)
1123 
1124  retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1125  if (retval != ERROR_OK) {
1126  LOG_ERROR("couldn't write MPSSE commands to FT2232");
1127  exit(-1);
1128  }
1129  LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1130  ft2232_buffer_size, (int)bytes_written);
1131  ft2232_buffer_size = 0;
1132 
1133  /* add command for complete bytes */
1134  while (num_bytes > 1) {
1135  int thisrun_bytes;
1136 
1137  if (type == SCAN_IO) {
1138  /* Clock Data Bytes In and Out LSB First */
1139  buffer_write(0x39);
1140  /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1141  } else if (type == SCAN_OUT) {
1142  /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1143  buffer_write(0x19);
1144  /* LOG_DEBUG("added TDI bytes (o)"); */
1145  } else if (type == SCAN_IN) {
1146  /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1147  buffer_write(0x28);
1148  /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1149  }
1150 
1151  thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1152  thisrun_read = thisrun_bytes;
1153  num_bytes -= thisrun_bytes;
1154  buffer_write((uint8_t) (thisrun_bytes - 1));
1155  buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1156 
1157  if (type != SCAN_IN) {
1158  /* add complete bytes */
1159  while (thisrun_bytes-- > 0) {
1160  buffer_write(buffer[cur_byte]);
1161  cur_byte++;
1162  bits_left -= 8;
1163  }
1164  } else /* (type == SCAN_IN) */
1165  bits_left -= 8 * (thisrun_bytes);
1166 
1167  retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1168  if (retval != ERROR_OK) {
1169  LOG_ERROR("couldn't write MPSSE commands to FT2232");
1170  exit(-1);
1171  }
1172  LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1174  (int)bytes_written);
1175  ft2232_buffer_size = 0;
1176 
1177  if (type != SCAN_OUT) {
1178  retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1179  if (retval != ERROR_OK) {
1180  LOG_ERROR("couldn't read from FT2232");
1181  exit(-1);
1182  }
1183  LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1184  thisrun_read,
1185  (int)bytes_read);
1186  receive_pointer += bytes_read;
1187  }
1188  }
1189 
1190  thisrun_read = 0;
1191 
1192  /* the most signifcant bit is scanned during TAP movement */
1193  if (type != SCAN_IN)
1194  last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1195  else
1196  last_bit = 0;
1197 
1198  /* process remaining bits but the last one */
1199  if (bits_left > 1) {
1200  if (type == SCAN_IO) {
1201  /* Clock Data Bits In and Out LSB First */
1202  buffer_write(0x3b);
1203  /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1204  } else if (type == SCAN_OUT) {
1205  /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1206  buffer_write(0x1b);
1207  /* LOG_DEBUG("added TDI bits (o)"); */
1208  } else if (type == SCAN_IN) {
1209  /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1210  buffer_write(0x2a);
1211  /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1212  }
1213  buffer_write(bits_left - 2);
1214  if (type != SCAN_IN)
1215  buffer_write(buffer[cur_byte]);
1216 
1217  if (type != SCAN_OUT)
1218  thisrun_read += 2;
1219  }
1220 
1221  if (tap_get_end_state() == TAP_DRSHIFT) {
1222  if (type == SCAN_IO) {
1223  /* Clock Data Bits In and Out LSB First */
1224  buffer_write(0x3b);
1225  /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1226  } else if (type == SCAN_OUT) {
1227  /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1228  buffer_write(0x1b);
1229  /* LOG_DEBUG("added TDI bits (o)"); */
1230  } else if (type == SCAN_IN) {
1231  /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1232  buffer_write(0x2a);
1233  /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1234  }
1235  buffer_write(0x0);
1236  buffer_write(last_bit);
1237  } else {
1238  int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1240  uint8_t mpsse_cmd;
1241 
1242  /* move from Shift-IR/DR to end state */
1243  if (type != SCAN_OUT) {
1244  /* Clock Data to TMS/CS Pin with Read */
1245  mpsse_cmd = 0x6b;
1246  /* LOG_DEBUG("added TMS scan (read)"); */
1247  } else {
1248  /* Clock Data to TMS/CS Pin (no Read) */
1249  mpsse_cmd = 0x4b;
1250  /* LOG_DEBUG("added TMS scan (no read)"); */
1251  }
1252 
1253  DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1254  clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1255  }
1256 
1257  if (type != SCAN_OUT)
1258  thisrun_read += 1;
1259 
1260  retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written);
1261  if (retval != ERROR_OK) {
1262  LOG_ERROR("couldn't write MPSSE commands to FT2232");
1263  exit(-1);
1264  }
1265  LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1267  (int)bytes_written);
1268  ft2232_buffer_size = 0;
1269 
1270  if (type != SCAN_OUT) {
1271  retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read);
1272  if (retval != ERROR_OK) {
1273  LOG_ERROR("couldn't read from FT2232");
1274  exit(-1);
1275  }
1276  LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1277  thisrun_read,
1278  (int)bytes_read);
1279  }
1280 
1281  free(receive_buffer);
1282 
1283  return ERROR_OK;
1284 }
1285 
1286 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1287 {
1288  int predicted_size = 3;
1289  int num_bytes = (scan_size - 1) / 8;
1290 
1291  if (tap_get_state() != TAP_DRSHIFT)
1292  predicted_size += get_tms_buffer_requirements(
1294 
1295  if (type == SCAN_IN) { /* only from device to host */
1296  /* complete bytes */
1297  predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1298 
1299  /* remaining bits - 1 (up to 7) */
1300  predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1301  } else {/* host to device, or bidirectional
1302  * complete bytes */
1303  predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1304 
1305  /* remaining bits -1 (up to 7) */
1306  predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1307  }
1308 
1309  return predicted_size;
1310 }
1311 
1312 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1313 {
1314  int predicted_size = 0;
1315 
1316  if (type != SCAN_OUT) {
1317  /* complete bytes */
1318  predicted_size +=
1319  (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1320 
1321  /* remaining bits - 1 */
1322  predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1323 
1324  /* last bit (from TMS scan) */
1325  predicted_size += 1;
1326  }
1327 
1328  /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1329 
1330  return predicted_size;
1331 }
1332 
1333 /* semi-generic FT2232/FT4232 reset code */
1334 static void ftx23_reset(int trst, int srst)
1335 {
1337  if (trst == 1) {
1338  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1339  low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1340  else
1341  low_output &= ~nTRST; /* switch output low */
1342  } else if (trst == 0) {
1343  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1344  low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal
1345  *and external pullup) */
1346  else
1347  low_output |= nTRST; /* switch output high */
1348  }
1349 
1350  if (srst == 1) {
1351  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1352  low_output &= ~nSRST; /* switch output low */
1353  else
1354  low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1355  } else if (srst == 0) {
1356  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1357  low_output |= nSRST; /* switch output high */
1358  else
1359  low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1360  }
1361 
1362  /* command "set data bits low byte" */
1363  buffer_write(0x80);
1366 }
1367 
1368 static void jtagkey_reset(int trst, int srst)
1369 {
1371  if (trst == 1) {
1372  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1373  high_output &= ~nTRSTnOE;
1374  else
1375  high_output &= ~nTRST;
1376  } else if (trst == 0) {
1377  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1378  high_output |= nTRSTnOE;
1379  else
1380  high_output |= nTRST;
1381  }
1382 
1383  if (srst == 1) {
1384  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1385  high_output &= ~nSRST;
1386  else
1387  high_output &= ~nSRSTnOE;
1388  } else if (srst == 0) {
1389  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1390  high_output |= nSRST;
1391  else
1392  high_output |= nSRSTnOE;
1393  }
1394 
1395  /* command "set data bits high byte" */
1396  buffer_write(0x82);
1399  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1400  trst,
1401  srst,
1402  high_output,
1403  high_direction);
1404 }
1405 
1406 static void olimex_jtag_reset(int trst, int srst)
1407 {
1409  if (trst == 1) {
1410  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1411  high_output &= ~nTRSTnOE;
1412  else
1413  high_output &= ~nTRST;
1414  } else if (trst == 0) {
1415  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1416  high_output |= nTRSTnOE;
1417  else
1418  high_output |= nTRST;
1419  }
1420 
1421  if (srst == 1)
1422  high_output |= nSRST;
1423  else if (srst == 0)
1424  high_output &= ~nSRST;
1425 
1426  /* command "set data bits high byte" */
1427  buffer_write(0x82);
1430  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1431  trst,
1432  srst,
1433  high_output,
1434  high_direction);
1435 }
1436 
1437 static void axm0432_jtag_reset(int trst, int srst)
1438 {
1439  if (trst == 1) {
1441  high_output &= ~nTRST;
1442  } else if (trst == 0)
1443  high_output |= nTRST;
1444 
1445  if (srst == 1)
1446  high_output &= ~nSRST;
1447  else if (srst == 0)
1448  high_output |= nSRST;
1449 
1450  /* command "set data bits low byte" */
1451  buffer_write(0x82);
1454  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1455  trst,
1456  srst,
1457  high_output,
1458  high_direction);
1459 }
1460 
1461 static void flyswatter_reset(int trst, int srst)
1462 {
1463  if (trst == 1)
1464  low_output &= ~nTRST;
1465  else if (trst == 0)
1466  low_output |= nTRST;
1467 
1468  if (srst == 1)
1469  low_output |= nSRST;
1470  else if (srst == 0)
1471  low_output &= ~nSRST;
1472 
1473  /* command "set data bits low byte" */
1474  buffer_write(0x80);
1477  LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1478  trst,
1479  srst,
1480  low_output,
1481  low_direction);
1482 }
1483 
1484 static void flyswatter1_reset(int trst, int srst)
1485 {
1486  flyswatter_reset(trst, srst);
1487 }
1488 
1489 static void flyswatter2_reset(int trst, int srst)
1490 {
1491  flyswatter_reset(trst, !srst);
1492 }
1493 
1494 static void minimodule_reset(int trst, int srst)
1495 {
1496  if (srst == 1)
1497  low_output &= ~nSRST;
1498  else if (srst == 0)
1499  low_output |= nSRST;
1500 
1501  /* command "set data bits low byte" */
1502  buffer_write(0x80);
1505  LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1506  trst,
1507  srst,
1508  low_output,
1509  low_direction);
1510 }
1511 
1512 static void turtle_reset(int trst, int srst)
1513 {
1514  if (trst == 1)
1515  LOG_ERROR("Can't assert TRST: the adapter lacks this signal");
1516 
1517  if (srst == 1)
1518  low_output |= nSRST;
1519  else if (srst == 0)
1520  low_output &= ~nSRST;
1521 
1522  /* command "set data bits low byte" */
1523  buffer_write(0x80);
1526  LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
1527  srst,
1528  low_output,
1529  low_direction);
1530 }
1531 
1532 static void comstick_reset(int trst, int srst)
1533 {
1534  if (trst == 1)
1535  high_output &= ~nTRST;
1536  else if (trst == 0)
1537  high_output |= nTRST;
1538 
1539  if (srst == 1)
1540  high_output &= ~nSRST;
1541  else if (srst == 0)
1542  high_output |= nSRST;
1543 
1544  /* command "set data bits high byte" */
1545  buffer_write(0x82);
1548  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1549  trst,
1550  srst,
1551  high_output,
1552  high_direction);
1553 }
1554 
1555 static void stm32stick_reset(int trst, int srst)
1556 {
1557  if (trst == 1)
1558  high_output &= ~nTRST;
1559  else if (trst == 0)
1560  high_output |= nTRST;
1561 
1562  if (srst == 1)
1563  low_output &= ~nSRST;
1564  else if (srst == 0)
1565  low_output |= nSRST;
1566 
1567  /* command "set data bits low byte" */
1568  buffer_write(0x80);
1571 
1572  /* command "set data bits high byte" */
1573  buffer_write(0x82);
1576  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1577  trst,
1578  srst,
1579  high_output,
1580  high_direction);
1581 }
1582 
1583 static void sheevaplug_reset(int trst, int srst)
1584 {
1585  if (trst == 1)
1586  high_output &= ~nTRST;
1587  else if (trst == 0)
1588  high_output |= nTRST;
1589 
1590  if (srst == 1)
1591  high_output &= ~nSRSTnOE;
1592  else if (srst == 0)
1593  high_output |= nSRSTnOE;
1594 
1595  /* command "set data bits high byte" */
1596  buffer_write(0x82);
1599  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
1600  trst,
1601  srst,
1602  high_output,
1603  high_direction);
1604 }
1605 
1606 static void redbee_reset(int trst, int srst)
1607 {
1608  if (trst == 1) {
1610  high_output &= ~nTRST;
1611  } else if (trst == 0)
1612  high_output |= nTRST;
1613 
1614  if (srst == 1)
1615  high_output &= ~nSRST;
1616  else if (srst == 0)
1617  high_output |= nSRST;
1618 
1619  /* command "set data bits low byte" */
1620  buffer_write(0x82);
1623  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1624  "high_direction: 0x%2.2x", trst, srst, high_output,
1625  high_direction);
1626 }
1627 
1628 static void xds100v2_reset(int trst, int srst)
1629 {
1630  if (trst == 1) {
1632  high_output &= ~nTRST;
1633  } else if (trst == 0)
1634  high_output |= nTRST;
1635 
1636  if (srst == 1)
1637  high_output |= nSRST;
1638  else if (srst == 0)
1639  high_output &= ~nSRST;
1640 
1641  /* command "set data bits low byte" */
1642  buffer_write(0x82);
1645  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1646  "high_direction: 0x%2.2x", trst, srst, high_output,
1647  high_direction);
1648 }
1649 
1650 static int ft2232_execute_runtest(struct jtag_command *cmd)
1651 {
1652  int retval;
1653  int i;
1654  int predicted_size = 0;
1655  retval = ERROR_OK;
1656 
1657  DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1658  cmd->cmd.runtest->num_cycles,
1660 
1661  /* only send the maximum buffer size that FT2232C can handle */
1662  predicted_size = 0;
1663  if (tap_get_state() != TAP_IDLE)
1664  predicted_size += 3;
1665  predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1666  if (cmd->cmd.runtest->end_state != TAP_IDLE)
1667  predicted_size += 3;
1668  if (tap_get_end_state() != TAP_IDLE)
1669  predicted_size += 3;
1670  if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1671  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1672  retval = ERROR_JTAG_QUEUE_FAILED;
1673  require_send = 0;
1674  first_unsent = cmd;
1675  }
1676  if (tap_get_state() != TAP_IDLE) {
1678  require_send = 1;
1679  }
1680  i = cmd->cmd.runtest->num_cycles;
1681  while (i > 0) {
1682  /* there are no state transitions in this code, so omit state tracking */
1683 
1684  /* command "Clock Data to TMS/CS Pin (no Read)" */
1685  buffer_write(0x4b);
1686 
1687  /* scan 7 bits */
1688  buffer_write((i > 7) ? 6 : (i - 1));
1689 
1690  /* TMS data bits */
1691  buffer_write(0x0);
1692 
1693  i -= (i > 7) ? 7 : i;
1694  /* LOG_DEBUG("added TMS scan (no read)"); */
1695  }
1696 
1698 
1699  if (tap_get_state() != tap_get_end_state())
1701 
1702  require_send = 1;
1703  DEBUG_JTAG_IO("runtest: %i, end in %s",
1704  cmd->cmd.runtest->num_cycles,
1706  return retval;
1707 }
1708 
1710 {
1711  int predicted_size = 0;
1712  int retval = ERROR_OK;
1713 
1714  DEBUG_JTAG_IO("statemove end in %s",
1716 
1717  /* only send the maximum buffer size that FT2232C can handle */
1718  predicted_size = 3;
1719  if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1720  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1721  retval = ERROR_JTAG_QUEUE_FAILED;
1722  require_send = 0;
1723  first_unsent = cmd;
1724  }
1726 
1727  /* For TAP_RESET, ignore the current recorded state. It's often
1728  * wrong at server startup, and this transation is critical whenever
1729  * it's requested.
1730  */
1731  if (tap_get_end_state() == TAP_RESET) {
1732  clock_tms(0x4b, 0xff, 5, 0);
1733  require_send = 1;
1734 
1735  /* shortest-path move to desired end state */
1736  } else if (tap_get_state() != tap_get_end_state()) {
1738  require_send = 1;
1739  }
1740 
1741  return retval;
1742 }
1743 
1748 static int ft2232_execute_tms(struct jtag_command *cmd)
1749 {
1750  int retval = ERROR_OK;
1751  unsigned num_bits = cmd->cmd.tms->num_bits;
1752  const uint8_t *bits = cmd->cmd.tms->bits;
1753  unsigned count;
1754 
1755  DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1756 
1757  /* only send the maximum buffer size that FT2232C can handle */
1758  count = 3 * DIV_ROUND_UP(num_bits, 4);
1759  if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1760  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1761  retval = ERROR_JTAG_QUEUE_FAILED;
1762 
1763  require_send = 0;
1764  first_unsent = cmd;
1765  }
1766 
1767  /* Shift out in batches of at most 6 bits; there's a report of an
1768  * FT2232 bug in this area, where shifting exactly 7 bits can make
1769  * problems with TMS signaling for the last clock cycle:
1770  *
1771  * http://developer.intra2net.com/mailarchive/html/
1772  * libftdi/2009/msg00292.html
1773  *
1774  * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1775  *
1776  * Note that pathmoves in JTAG are not often seven bits, so that
1777  * isn't a particularly likely situation outside of "special"
1778  * signaling such as switching between JTAG and SWD modes.
1779  */
1780  while (num_bits) {
1781  if (num_bits <= 6) {
1782  buffer_write(0x4b);
1783  buffer_write(num_bits - 1);
1784  buffer_write(*bits & 0x3f);
1785  break;
1786  }
1787 
1788  /* Yes, this is lazy ... we COULD shift out more data
1789  * bits per operation, but doing it in nybbles is easy
1790  */
1791  buffer_write(0x4b);
1792  buffer_write(3);
1793  buffer_write(*bits & 0xf);
1794  num_bits -= 4;
1795 
1796  count = (num_bits > 4) ? 4 : num_bits;
1797 
1798  buffer_write(0x4b);
1799  buffer_write(count - 1);
1800  buffer_write((*bits >> 4) & 0xf);
1801  num_bits -= count;
1802 
1803  bits++;
1804  }
1805 
1806  require_send = 1;
1807  return retval;
1808 }
1809 
1810 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1811 {
1812  int predicted_size = 0;
1813  int retval = ERROR_OK;
1814 
1815  tap_state_t *path = cmd->cmd.pathmove->path;
1816  int num_states = cmd->cmd.pathmove->num_states;
1817 
1818  DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1820  tap_state_name(path[num_states-1]));
1821 
1822  /* only send the maximum buffer size that FT2232C can handle */
1823  predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1824  if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1825  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1826  retval = ERROR_JTAG_QUEUE_FAILED;
1827 
1828  require_send = 0;
1829  first_unsent = cmd;
1830  }
1831 
1832  ft2232_add_pathmove(path, num_states);
1833  require_send = 1;
1834 
1835  return retval;
1836 }
1837 
1838 static int ft2232_execute_scan(struct jtag_command *cmd)
1839 {
1840  uint8_t *buffer;
1841  int scan_size; /* size of IR or DR scan */
1842  int predicted_size = 0;
1843  int retval = ERROR_OK;
1844 
1845  enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1846 
1847  DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1848 
1849  scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1850 
1851  predicted_size = ft2232_predict_scan_out(scan_size, type);
1852  if ((predicted_size + 1) > FT2232_BUFFER_SIZE) {
1853  LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1854  /* unsent commands before this */
1855  if (first_unsent != cmd)
1856  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1857  retval = ERROR_JTAG_QUEUE_FAILED;
1858 
1859  /* current command */
1861  ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1862  require_send = 0;
1863  first_unsent = cmd->next;
1864  if (buffer)
1865  free(buffer);
1866  return retval;
1867  } else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1868  LOG_DEBUG(
1869  "ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1870  first_unsent,
1871  cmd);
1872  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1873  retval = ERROR_JTAG_QUEUE_FAILED;
1874  require_send = 0;
1875  first_unsent = cmd;
1876  }
1877  ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1878  /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1880  ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1881  require_send = 1;
1882  if (buffer)
1883  free(buffer);
1884  DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1885  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1887  return retval;
1888 
1889 }
1890 
1891 static int ft2232_execute_reset(struct jtag_command *cmd)
1892 {
1893  int retval;
1894  int predicted_size = 0;
1895  retval = ERROR_OK;
1896 
1897  DEBUG_JTAG_IO("reset trst: %i srst %i",
1898  cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1899 
1900  /* only send the maximum buffer size that FT2232C can handle */
1901  predicted_size = 3;
1902  if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE) {
1903  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1904  retval = ERROR_JTAG_QUEUE_FAILED;
1905  require_send = 0;
1906  first_unsent = cmd;
1907  }
1908 
1909  if ((cmd->cmd.reset->trst == 1) ||
1912 
1913  layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1914  require_send = 1;
1915 
1916  DEBUG_JTAG_IO("trst: %i, srst: %i",
1917  cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1918  return retval;
1919 }
1920 
1921 static int ft2232_execute_sleep(struct jtag_command *cmd)
1922 {
1923  int retval;
1924  retval = ERROR_OK;
1925 
1926  DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1927 
1928  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1929  retval = ERROR_JTAG_QUEUE_FAILED;
1930  first_unsent = cmd->next;
1931  jtag_sleep(cmd->cmd.sleep->us);
1932  DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1933  cmd->cmd.sleep->us,
1935  return retval;
1936 }
1937 
1939 {
1940  int retval;
1941  retval = ERROR_OK;
1942 
1943  /* this is only allowed while in a stable state. A check for a stable
1944  * state was done in jtag_add_clocks()
1945  */
1947  retval = ERROR_JTAG_QUEUE_FAILED;
1948  DEBUG_JTAG_IO("clocks %i while in %s",
1949  cmd->cmd.stableclocks->num_cycles,
1951  return retval;
1952 }
1953 
1954 static int ft2232_execute_command(struct jtag_command *cmd)
1955 {
1956  int retval;
1957 
1958  switch (cmd->type) {
1959  case JTAG_RESET:
1960  retval = ft2232_execute_reset(cmd);
1961  break;
1962  case JTAG_RUNTEST:
1963  retval = ft2232_execute_runtest(cmd);
1964  break;
1965  case JTAG_TLR_RESET:
1966  retval = ft2232_execute_statemove(cmd);
1967  break;
1968  case JTAG_PATHMOVE:
1969  retval = ft2232_execute_pathmove(cmd);
1970  break;
1971  case JTAG_SCAN:
1972  retval = ft2232_execute_scan(cmd);
1973  break;
1974  case JTAG_SLEEP:
1975  retval = ft2232_execute_sleep(cmd);
1976  break;
1977  case JTAG_STABLECLOCKS:
1978  retval = ft2232_execute_stableclocks(cmd);
1979  break;
1980  case JTAG_TMS:
1981  retval = ft2232_execute_tms(cmd);
1982  break;
1983  default:
1984  LOG_ERROR("BUG: unknown JTAG command type encountered");
1985  retval = ERROR_JTAG_QUEUE_FAILED;
1986  break;
1987  }
1988  return retval;
1989 }
1990 
1991 static int ft2232_execute_queue(void)
1992 {
1993  struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
1994  int retval;
1995 
1996  first_unsent = cmd; /* next command that has to be sent */
1997  require_send = 0;
1998 
1999  /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2000  * that wasn't handled by a caller-provided error handler
2001  */
2002  retval = ERROR_OK;
2003 
2004  ft2232_buffer_size = 0;
2005  ft2232_expect_read = 0;
2006 
2007  /* blink, if the current layout has that feature */
2008  if (layout->blink)
2009  layout->blink();
2010 
2011  while (cmd) {
2012  /* fill the write buffer with the desired command */
2013  if (ft2232_execute_command(cmd) != ERROR_OK)
2014  retval = ERROR_JTAG_QUEUE_FAILED;
2015  /* Start reading input before FT2232 TX buffer fills up.
2016  * Sometimes this happens because we don't know the
2017  * length of the last command before we execute it. So
2018  * we simple inform the user.
2019  */
2020  cmd = cmd->next;
2021 
2024  LOG_DEBUG("read buffer size looks too high %d/%d",
2027  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2028  retval = ERROR_JTAG_QUEUE_FAILED;
2029  first_unsent = cmd;
2030  }
2031  }
2032 
2033  if (require_send > 0)
2034  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2035  retval = ERROR_JTAG_QUEUE_FAILED;
2036 
2037  return retval;
2038 }
2039 
2040 #if BUILD_FT2232_FTD2XX == 1
2041 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int *try_more)
2042 {
2043  FT_STATUS status;
2044  DWORD deviceID;
2045  char SerialNumber[16];
2046  char Description[64];
2047  DWORD openex_flags = 0;
2048  char *openex_string = NULL;
2049  uint8_t latency_timer;
2050 
2051  if (layout == NULL) {
2052  LOG_WARNING("No ft2232 layout specified'");
2053  return ERROR_JTAG_INIT_FAILED;
2054  }
2055 
2056  LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
2057  layout->name, vid, pid);
2058 
2059 #if IS_WIN32 == 0
2060  /* Add non-standard Vid/Pid to the linux driver */
2061  status = FT_SetVIDPID(vid, pid);
2062  if (status != FT_OK)
2063  LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2064 
2065 #endif
2066 
2068  LOG_WARNING(
2069  "can't open by device description and serial number, giving precedence to serial");
2071  }
2072 
2073  if (ft2232_device_desc) {
2074  openex_string = ft2232_device_desc;
2075  openex_flags = FT_OPEN_BY_DESCRIPTION;
2076  } else if (ft2232_serial) {
2077  openex_string = ft2232_serial;
2078  openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2079  } else {
2080  LOG_ERROR("neither device description nor serial number specified");
2081  LOG_ERROR(
2082  "please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2083 
2084  return ERROR_JTAG_INIT_FAILED;
2085  }
2086 
2087  status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2088  if (status != FT_OK) {
2089  /* under Win32, the FTD2XX driver appends an "A" to the end
2090  * of the description, if we tried by the desc, then
2091  * try by the alternate "A" description. */
2092  if (openex_string == ft2232_device_desc) {
2093  /* Try the alternate method. */
2094  openex_string = ft2232_device_desc_A;
2095  status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2096  if (status == FT_OK) {
2097  /* yea, the "alternate" method worked! */
2098  } else {
2099  /* drat, give the user a meaningfull message.
2100  * telling the use we tried *BOTH* methods. */
2101  LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2104  }
2105  }
2106  }
2107 
2108  if (status != FT_OK) {
2109  DWORD num_devices;
2110 
2111  if (more) {
2112  LOG_WARNING("unable to open ftdi device (trying more): %s",
2113  ftd2xx_status_string(status));
2114  *try_more = 1;
2115  return ERROR_JTAG_INIT_FAILED;
2116  }
2117  LOG_ERROR("unable to open ftdi device: %s",
2118  ftd2xx_status_string(status));
2119  status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2120  if (status == FT_OK) {
2121  char **desc_array = malloc(sizeof(char *) * (num_devices + 1));
2122  uint32_t i;
2123 
2124  for (i = 0; i < num_devices; i++)
2125  desc_array[i] = malloc(64);
2126 
2127  desc_array[num_devices] = NULL;
2128 
2129  status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2130 
2131  if (status == FT_OK) {
2132  LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2133  for (i = 0; i < num_devices; i++)
2134  LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2135  }
2136 
2137  for (i = 0; i < num_devices; i++)
2138  free(desc_array[i]);
2139 
2140  free(desc_array);
2141  } else
2142  LOG_ERROR("ListDevices: NONE");
2143  return ERROR_JTAG_INIT_FAILED;
2144  }
2145 
2146  status = FT_SetLatencyTimer(ftdih, ft2232_latency);
2147  if (status != FT_OK) {
2148  LOG_ERROR("unable to set latency timer: %s",
2149  ftd2xx_status_string(status));
2150  return ERROR_JTAG_INIT_FAILED;
2151  }
2152 
2153  status = FT_GetLatencyTimer(ftdih, &latency_timer);
2154  if (status != FT_OK) {
2155  /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2156  * so ignore errors if using this driver version */
2157  DWORD dw_version;
2158 
2159  status = FT_GetDriverVersion(ftdih, &dw_version);
2160  LOG_ERROR("unable to get latency timer: %s",
2161  ftd2xx_status_string(status));
2162 
2163  if ((status == FT_OK) && (dw_version == 0x10004)) {
2164  LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2165  "with FT_GetLatencyTimer, upgrade to a newer version");
2166  } else
2167  return ERROR_JTAG_INIT_FAILED;
2168  } else
2169  LOG_DEBUG("current latency timer: %i", latency_timer);
2170 
2171  status = FT_SetTimeouts(ftdih, 5000, 5000);
2172  if (status != FT_OK) {
2173  LOG_ERROR("unable to set timeouts: %s",
2174  ftd2xx_status_string(status));
2175  return ERROR_JTAG_INIT_FAILED;
2176  }
2177 
2178  status = FT_SetBitMode(ftdih, 0x0b, 2);
2179  if (status != FT_OK) {
2180  LOG_ERROR("unable to enable bit i/o mode: %s",
2181  ftd2xx_status_string(status));
2182  return ERROR_JTAG_INIT_FAILED;
2183  }
2184 
2185  status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID,
2186  SerialNumber, Description, NULL);
2187  if (status != FT_OK) {
2188  LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2189  ftd2xx_status_string(status));
2190  return ERROR_JTAG_INIT_FAILED;
2191  } else {
2192  static const char *type_str[] = {
2193  "BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H", "232H"
2194  };
2195  unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2196  unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2197  ? ftdi_device : FT_DEVICE_UNKNOWN;
2198  LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2199  LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2200  LOG_INFO("SerialNumber: %s", SerialNumber);
2201  LOG_INFO("Description: %s", Description);
2202  }
2203 
2204  return ERROR_OK;
2205 }
2206 
2207 static int ft2232_purge_ftd2xx(void)
2208 {
2209  FT_STATUS status;
2210 
2211  status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
2212  if (status != FT_OK) {
2213  LOG_ERROR("error purging ftd2xx device: %s",
2214  ftd2xx_status_string(status));
2215  return ERROR_JTAG_INIT_FAILED;
2216  }
2217 
2218  return ERROR_OK;
2219 }
2220 
2221 #endif /* BUILD_FT2232_FTD2XX == 1 */
2222 
2223 #if BUILD_FT2232_LIBFTDI == 1
2224 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int *try_more, int channel)
2225 {
2226  uint8_t latency_timer;
2227 
2228  if (layout == NULL) {
2229  LOG_WARNING("No ft2232 layout specified'");
2230  return ERROR_JTAG_INIT_FAILED;
2231  }
2232 
2233  LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2234  layout->name, vid, pid);
2235 
2236  if (ftdi_init(&ftdic) < 0)
2237  return ERROR_JTAG_INIT_FAILED;
2238 
2239  /* default to INTERFACE_A */
2240  if (channel == INTERFACE_ANY)
2241  channel = INTERFACE_A;
2242  if (ftdi_set_interface(&ftdic, channel) < 0) {
2243  LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2244  return ERROR_JTAG_INIT_FAILED;
2245  }
2246 
2247  /* context, vendor id, product id */
2248  if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc, ft2232_serial) < 0) {
2249  if (more)
2250  LOG_WARNING("unable to open ftdi device (trying more): %s",
2251  ftdic.error_str);
2252  else
2253  LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2254  *try_more = 1;
2255  return ERROR_JTAG_INIT_FAILED;
2256  }
2257 
2258  /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2259  if (ftdi_usb_reset(&ftdic) < 0) {
2260  LOG_ERROR("unable to reset ftdi device");
2261  return ERROR_JTAG_INIT_FAILED;
2262  }
2263 
2264  if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0) {
2265  LOG_ERROR("unable to set latency timer");
2266  return ERROR_JTAG_INIT_FAILED;
2267  }
2268 
2269  if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
2270  LOG_ERROR("unable to get latency timer");
2271  return ERROR_JTAG_INIT_FAILED;
2272  } else
2273  LOG_DEBUG("current latency timer: %i", latency_timer);
2274 
2275  ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2276 
2277  ftdi_device = ftdic.type;
2278  static const char *type_str[] = {
2279  "AM", "BM", "2232C", "R", "2232H", "4232H", "232H", "Unknown"
2280  };
2281  unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2282  unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2283  ? ftdi_device : no_of_known_types;
2284  LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2285  return ERROR_OK;
2286 }
2287 
2288 static int ft2232_purge_libftdi(void)
2289 {
2290  if (ftdi_usb_purge_buffers(&ftdic) < 0) {
2291  LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2292  return ERROR_JTAG_INIT_FAILED;
2293  }
2294 
2295  return ERROR_OK;
2296 }
2297 
2298 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2299 
2300 static int ft2232_set_data_bits_low_byte(uint8_t value, uint8_t direction)
2301 {
2302  uint8_t buf[3];
2303  uint32_t bytes_written;
2304 
2305  buf[0] = 0x80; /* command "set data bits low byte" */
2306  buf[1] = value; /* value */
2307  buf[2] = direction; /* direction */
2308 
2309  LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2310 
2311  if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2312  LOG_ERROR("couldn't initialize data bits low byte");
2313  return ERROR_JTAG_INIT_FAILED;
2314  }
2315 
2316  return ERROR_OK;
2317 }
2318 
2319 static int ft2232_set_data_bits_high_byte(uint8_t value, uint8_t direction)
2320 {
2321  uint8_t buf[3];
2322  uint32_t bytes_written;
2323 
2324  buf[0] = 0x82; /* command "set data bits high byte" */
2325  buf[1] = value; /* value */
2326  buf[2] = direction; /* direction */
2327 
2328  LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2329 
2330  if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK) {
2331  LOG_ERROR("couldn't initialize data bits high byte");
2332  return ERROR_JTAG_INIT_FAILED;
2333  }
2334 
2335  return ERROR_OK;
2336 }
2337 
2338 static int ft2232_init(void)
2339 {
2340  uint8_t buf[1];
2341  int retval;
2342  uint32_t bytes_written;
2343 
2344  LOG_WARNING("Using DEPRECATED interface driver 'ft2232'");
2345 #if BUILD_FTDI
2346  LOG_INFO("Consider using the 'ftdi' interface driver, with configuration files in interface/ftdi/...");
2347 #endif
2348 
2350  LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2351  else
2352  LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2353  if (layout == NULL) {
2354  LOG_WARNING("No ft2232 layout specified'");
2355  return ERROR_JTAG_INIT_FAILED;
2356  }
2357 
2358  for (int i = 0; 1; i++) {
2359  /*
2360  * "more indicates that there are more IDs to try, so we should
2361  * not print an error for an ID mismatch (but for anything
2362  * else, we should).
2363  *
2364  * try_more indicates that the error code returned indicates an
2365  * ID mismatch (and nothing else) and that we should proceeed
2366  * with the next ID pair.
2367  */
2368  int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2369  int try_more = 0;
2370 
2371 #if BUILD_FT2232_FTD2XX == 1
2372  retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2373  more, &try_more);
2374 #elif BUILD_FT2232_LIBFTDI == 1
2375  retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2376  more, &try_more, ft2232_channel);
2377 #endif
2378  if (retval >= 0)
2379  break;
2380  if (!more || !try_more)
2381  return retval;
2382  }
2383 
2384  ft2232_buffer_size = 0;
2386 
2387  if (layout->init() != ERROR_OK)
2388  return ERROR_JTAG_INIT_FAILED;
2389 
2391 #ifndef BUILD_FT2232_HIGHSPEED
2392  #if BUILD_FT2232_FTD2XX == 1
2393  LOG_WARNING(
2394  "High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2395  #elif BUILD_FT2232_LIBFTDI == 1
2396  LOG_WARNING(
2397  "High Speed device found - You need a newer libftdi version (0.16 or later)");
2398  #endif
2399 #endif
2400  /* make sure the legacy mode is disabled */
2401  if (ftx232h_clk_divide_by_5(false) != ERROR_OK)
2402  return ERROR_JTAG_INIT_FAILED;
2403  }
2404 
2405  buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2406  retval = ft2232_write(buf, 1, &bytes_written);
2407  if (retval != ERROR_OK) {
2408  LOG_ERROR("couldn't write to FT2232 to disable loopback");
2409  return ERROR_JTAG_INIT_FAILED;
2410  }
2411 
2412 #if BUILD_FT2232_FTD2XX == 1
2413  return ft2232_purge_ftd2xx();
2414 #elif BUILD_FT2232_LIBFTDI == 1
2415  return ft2232_purge_libftdi();
2416 #endif
2417 
2418  return ERROR_OK;
2419 }
2420 
2424 static inline void ftx232_dbus_init(void)
2425 {
2426  low_output = 0x08;
2427  low_direction = 0x0b;
2428 }
2429 
2434 static int ftx232_dbus_write(void)
2435 {
2437  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2438  low_direction &= ~nTRSTnOE; /* nTRST input */
2439  low_output &= ~nTRST; /* nTRST = 0 */
2440  } else {
2441  low_direction |= nTRSTnOE; /* nTRST output */
2442  low_output |= nTRST; /* nTRST = 1 */
2443  }
2444 
2445  if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2446  low_direction |= nSRSTnOE; /* nSRST output */
2447  low_output |= nSRST; /* nSRST = 1 */
2448  } else {
2449  low_direction &= ~nSRSTnOE; /* nSRST input */
2450  low_output &= ~nSRST; /* nSRST = 0 */
2451  }
2452 
2453  /* initialize low byte for jtag */
2455  LOG_ERROR("couldn't initialize FT2232 DBUS");
2456  return ERROR_JTAG_INIT_FAILED;
2457  }
2458 
2459  return ERROR_OK;
2460 }
2461 
2462 static int usbjtag_init(void)
2463 {
2464  /*
2465  * NOTE: This is now _specific_ to the "usbjtag" layout.
2466  * Don't try cram any more layouts into this.
2467  */
2468  ftx232_dbus_init();
2469 
2470  nTRST = 0x10;
2471  nTRSTnOE = 0x10;
2472  nSRST = 0x40;
2473  nSRSTnOE = 0x40;
2474 
2475  return ftx232_dbus_write();
2476 }
2477 
2478 static int lm3s811_jtag_init(void)
2479 {
2480  ftx232_dbus_init();
2481 
2482  /* There are multiple revisions of LM3S811 eval boards:
2483  * - Rev B (and older?) boards have no SWO trace support.
2484  * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2485  * they should use the "luminary_icdi" layout instead.
2486  */
2487  nTRST = 0x0;
2488  nTRSTnOE = 0x00;
2489  nSRST = 0x20;
2490  nSRSTnOE = 0x20;
2491  low_output = 0x88;
2492  low_direction = 0x8b;
2493 
2494  return ftx232_dbus_write();
2495 }
2496 
2497 static int icdi_jtag_init(void)
2498 {
2499  ftx232_dbus_init();
2500 
2501  /* Most Luminary eval boards support SWO trace output,
2502  * and should use this "luminary_icdi" layout.
2503  *
2504  * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2505  * to switch between JTAG and SWD, or switch the ft2232 UART
2506  * on the second MPSSE channel/interface (BDBUS)
2507  * between (i) the stellaris UART (on Luminary boards)
2508  * or (ii) SWO trace data (generic).
2509  *
2510  * We come up in JTAG mode and may switch to SWD later (with
2511  * SWO/trace option if SWD is active).
2512  *
2513  * DBUS == GPIO-Lx
2514  * CBUS == GPIO-Hx
2515  */
2516 
2517 
2518 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2519 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2520 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2521 
2522 
2523  /* GPIOs on second channel/interface (UART) ... */
2524 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2525 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2526 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2527 
2528  nTRST = 0x0;
2529  nTRSTnOE = 0x00;
2530  nSRST = ICDI_SRST;
2531  nSRSTnOE = ICDI_SRST;
2532 
2536 
2537  return ftx232_dbus_write();
2538 }
2539 
2540 static int signalyzer_init(void)
2541 {
2542  ftx232_dbus_init();
2543 
2544  nTRST = 0x10;
2545  nTRSTnOE = 0x10;
2546  nSRST = 0x20;
2547  nSRSTnOE = 0x20;
2548  return ftx232_dbus_write();
2549 }
2550 
2551 static int axm0432_jtag_init(void)
2552 {
2553  low_output = 0x08;
2554  low_direction = 0x2b;
2555 
2556  /* initialize low byte for jtag */
2558  LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2559  return ERROR_JTAG_INIT_FAILED;
2560  }
2561 
2562  if (strcmp(layout->name, "axm0432_jtag") == 0) {
2563  nTRST = 0x08;
2564  nTRSTnOE = 0x0; /* No output enable for TRST*/
2565  nSRST = 0x04;
2566  nSRSTnOE = 0x0; /* No output enable for SRST*/
2567  } else {
2568  LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2569  exit(-1);
2570  }
2571 
2572  high_output = 0x0;
2573  high_direction = 0x0c;
2574 
2576  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2577  LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2578  else
2579  high_output |= nTRST;
2580 
2581  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2582  LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2583  else
2584  high_output |= nSRST;
2585 
2586  /* initialize high byte for jtag */
2588  LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2589  return ERROR_JTAG_INIT_FAILED;
2590  }
2591 
2592  return ERROR_OK;
2593 }
2594 
2595 static int redbee_init(void)
2596 {
2597  low_output = 0x08;
2598  low_direction = 0x2b;
2599 
2600  /* initialize low byte for jtag */
2602  LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2603  return ERROR_JTAG_INIT_FAILED;
2604  }
2605 
2606  nTRST = 0x08;
2607  nTRSTnOE = 0x0; /* No output enable for TRST*/
2608  nSRST = 0x04;
2609  nSRSTnOE = 0x0; /* No output enable for SRST*/
2610 
2611  high_output = 0x0;
2612  high_direction = 0x0c;
2613 
2615  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2616  LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2617  else
2618  high_output |= nTRST;
2619 
2620  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2621  LOG_ERROR("can't set nSRST to push-pull on redbee");
2622  else
2623  high_output |= nSRST;
2624 
2625  /* initialize high byte for jtag */
2627  LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2628  return ERROR_JTAG_INIT_FAILED;
2629  }
2630 
2631  return ERROR_OK;
2632 }
2633 
2634 static int jtagkey_init(void)
2635 {
2636  low_output = 0x08;
2637  low_direction = 0x1b;
2638 
2639  /* initialize low byte for jtag */
2641  LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2642  return ERROR_JTAG_INIT_FAILED;
2643  }
2644 
2645  if (strcmp(layout->name, "jtagkey") == 0) {
2646  nTRST = 0x01;
2647  nTRSTnOE = 0x4;
2648  nSRST = 0x02;
2649  nSRSTnOE = 0x08;
2650  } else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2651  || (strcmp(layout->name, "oocdlink") == 0)) {
2652  nTRST = 0x02;
2653  nTRSTnOE = 0x1;
2654  nSRST = 0x08;
2655  nSRSTnOE = 0x04;
2656  } else {
2657  LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2658  exit(-1);
2659  }
2660 
2661  high_output = 0x0;
2662  high_direction = 0x0f;
2663 
2665  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2666  high_output |= nTRSTnOE;
2667  high_output &= ~nTRST;
2668  } else {
2669  high_output &= ~nTRSTnOE;
2670  high_output |= nTRST;
2671  }
2672 
2673  if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
2674  high_output &= ~nSRSTnOE;
2675  high_output |= nSRST;
2676  } else {
2677  high_output |= nSRSTnOE;
2678  high_output &= ~nSRST;
2679  }
2680 
2681  /* initialize high byte for jtag */
2683  LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2684  return ERROR_JTAG_INIT_FAILED;
2685  }
2686 
2687  return ERROR_OK;
2688 }
2689 
2690 static int olimex_jtag_init(void)
2691 {
2692  low_output = 0x08;
2693  low_direction = 0x1b;
2694 
2695  /* initialize low byte for jtag */
2697  LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2698  return ERROR_JTAG_INIT_FAILED;
2699  }
2700 
2701  nTRST = 0x01;
2702  nTRSTnOE = 0x4;
2703  nSRST = 0x02;
2704  nSRSTnOE = 0x00;/* no output enable for nSRST */
2705 
2706  high_output = 0x0;
2707  high_direction = 0x0f;
2708 
2710  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
2711  high_output |= nTRSTnOE;
2712  high_output &= ~nTRST;
2713  } else {
2714  high_output &= ~nTRSTnOE;
2715  high_output |= nTRST;
2716  }
2717 
2718  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2719  LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2720  else
2721  high_output &= ~nSRST;
2722 
2723  /* turn red LED on */
2724  high_output |= 0x08;
2725 
2726  /* initialize high byte for jtag */
2728  LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2729  return ERROR_JTAG_INIT_FAILED;
2730  }
2731 
2732  return ERROR_OK;
2733 }
2734 
2735 static int flyswatter_init(int rev)
2736 {
2737  low_output = 0x18;
2738  low_direction = 0x7b;
2739 
2740  if ((rev < 0) || (rev > 3)) {
2741  LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2742  return ERROR_JTAG_INIT_FAILED;
2743  }
2744 
2745  if (rev == 1)
2746  low_direction |= 1 << 7;
2747 
2748  /* initialize low byte for jtag */
2750  LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2751  return ERROR_JTAG_INIT_FAILED;
2752  }
2753 
2754  nTRST = 0x10;
2755  nTRSTnOE = 0x0; /* not output enable for nTRST */
2756  nSRST = 0x20;
2757  nSRSTnOE = 0x00; /* no output enable for nSRST */
2758 
2759  high_output = 0x00;
2760 
2761  if (rev == 1)
2762  high_direction = 0x0c;
2763  else
2764  high_direction = 0x01;
2765 
2766  /* turn red LED3 on, LED2 off */
2767  high_output |= 0x08;
2768 
2769  /* initialize high byte for jtag */
2771  LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2772  return ERROR_JTAG_INIT_FAILED;
2773  }
2774 
2775  return ERROR_OK;
2776 }
2777 
2778 static int flyswatter1_init(void)
2779 {
2780  return flyswatter_init(1);
2781 }
2782 
2783 static int flyswatter2_init(void)
2784 {
2785  return flyswatter_init(2);
2786 }
2787 
2788 static int minimodule_init(void)
2789 {
2790  low_output = 0x18; /* check if srst should be 1 or 0 initially. (0x08) (flyswatter was
2791  * 0x18) */
2792  low_direction = 0xfb; /* 0xfb; */
2793 
2794  /* initialize low byte for jtag */
2796  LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2797  return ERROR_JTAG_INIT_FAILED;
2798  }
2799 
2800 
2801  nSRST = 0x20;
2802 
2803  high_output = 0x00;
2804  high_direction = 0x05;
2805 
2806  /* turn red LED3 on, LED2 off */
2807  /* high_output |= 0x08; */
2808 
2809  /* initialize high byte for jtag */
2811  LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2812  return ERROR_JTAG_INIT_FAILED;
2813  }
2814 
2815  return ERROR_OK;
2816 }
2817 
2818 static int turtle_init(void)
2819 {
2820  low_output = 0x08;
2821  low_direction = 0x5b;
2822 
2823  /* initialize low byte for jtag */
2825  LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2826  return ERROR_JTAG_INIT_FAILED;
2827  }
2828 
2829  nSRST = 0x40;
2830 
2831  high_output = 0x00;
2832  high_direction = 0x0C;
2833 
2834  /* initialize high byte for jtag */
2836  LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2837  return ERROR_JTAG_INIT_FAILED;
2838  }
2839 
2840  return ERROR_OK;
2841 }
2842 
2843 static int comstick_init(void)
2844 {
2845  low_output = 0x08;
2846  low_direction = 0x0b;
2847 
2848  /* initialize low byte for jtag */
2850  LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2851  return ERROR_JTAG_INIT_FAILED;
2852  }
2853 
2854  nTRST = 0x01;
2855  nTRSTnOE = 0x00; /* no output enable for nTRST */
2856  nSRST = 0x02;
2857  nSRSTnOE = 0x00; /* no output enable for nSRST */
2858 
2859  high_output = 0x03;
2860  high_direction = 0x03;
2861 
2862  /* initialize high byte for jtag */
2864  LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2865  return ERROR_JTAG_INIT_FAILED;
2866  }
2867 
2868  return ERROR_OK;
2869 }
2870 
2871 static int stm32stick_init(void)
2872 {
2873  low_output = 0x88;
2874  low_direction = 0x8b;
2875 
2876  /* initialize low byte for jtag */
2878  LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2879  return ERROR_JTAG_INIT_FAILED;
2880  }
2881 
2882  nTRST = 0x01;
2883  nTRSTnOE = 0x00; /* no output enable for nTRST */
2884  nSRST = 0x80;
2885  nSRSTnOE = 0x00; /* no output enable for nSRST */
2886 
2887  high_output = 0x01;
2888  high_direction = 0x03;
2889 
2890  /* initialize high byte for jtag */
2892  LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2893  return ERROR_JTAG_INIT_FAILED;
2894  }
2895 
2896  return ERROR_OK;
2897 }
2898 
2899 static int sheevaplug_init(void)
2900 {
2901  low_output = 0x08;
2902  low_direction = 0x1b;
2903 
2904  /* initialize low byte for jtag */
2906  LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2907  return ERROR_JTAG_INIT_FAILED;
2908  }
2909 
2910  nTRSTnOE = 0x1;
2911  nTRST = 0x02;
2912  nSRSTnOE = 0x4;
2913  nSRST = 0x08;
2914 
2915  high_output = 0x0;
2916  high_direction = 0x0f;
2917 
2918  /* nTRST is always push-pull */
2919  high_output &= ~nTRSTnOE;
2920  high_output |= nTRST;
2921 
2922  /* nSRST is always open-drain */
2923  high_output |= nSRSTnOE;
2924  high_output &= ~nSRST;
2925 
2926  /* initialize high byte for jtag */
2928  LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2929  return ERROR_JTAG_INIT_FAILED;
2930  }
2931 
2932  return ERROR_OK;
2933 }
2934 
2935 static int cortino_jtag_init(void)
2936 {
2937  low_output = 0x08;
2938  low_direction = 0x1b;
2939 
2940  /* initialize low byte for jtag */
2942  LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2943  return ERROR_JTAG_INIT_FAILED;
2944  }
2945 
2946  nTRST = 0x01;
2947  nTRSTnOE = 0x00; /* no output enable for nTRST */
2948  nSRST = 0x02;
2949  nSRSTnOE = 0x00; /* no output enable for nSRST */
2950 
2951  high_output = 0x03;
2952  high_direction = 0x03;
2953 
2954  /* initialize high byte for jtag */
2956  LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2957  return ERROR_JTAG_INIT_FAILED;
2958  }
2959 
2960  return ERROR_OK;
2961 }
2962 
2963 static int lisa_l_init(void)
2964 {
2965  ftx232_dbus_init();
2966 
2967  nTRST = 0x10;
2968  nTRSTnOE = 0x10;
2969  nSRST = 0x40;
2970  nSRSTnOE = 0x40;
2971 
2972  high_output = 0x00;
2973  high_direction = 0x18;
2974 
2975  /* initialize high byte for jtag */
2977  LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
2978  return ERROR_JTAG_INIT_FAILED;
2979  }
2980 
2981  return ftx232_dbus_write();
2982 }
2983 
2984 static int flossjtag_init(void)
2985 {
2986  ftx232_dbus_init();
2987 
2988  nTRST = 0x10;
2989  nTRSTnOE = 0x10;
2990  nSRST = 0x40;
2991  nSRSTnOE = 0x40;
2992 
2993  high_output = 0x00;
2994  high_direction = 0x18;
2995 
2996  /* initialize high byte for jtag */
2998  LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
2999  return ERROR_JTAG_INIT_FAILED;
3000  }
3001 
3002  return ftx232_dbus_write();
3003 }
3004 
3005 /*
3006  * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
3007  * the door for a number of different configurations
3008  *
3009  * Known Implementations:
3010  * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
3011  *
3012  * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3013  * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3014  * * ACBUS3 to transition 0->1 (OE rising edge)
3015  * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3016  * * ADBUS5/GPIOL1 = EMU_EN = 1
3017  * * ADBUS6/GPIOL2 = EMU0 = 0
3018  * * ACBUS4/SPARE0 = EMU1 = 0
3019  * * CPLD logic: Disable loopback
3020  * * ACBUS6/SPARE2 = LOOPBACK = 0
3021  */
3022 #define XDS100_nEMU_EN (1<<5)
3023 #define XDS100_nEMU0 (1<<6)
3024 
3025 #define XDS100_PWR_RST (1<<3)
3026 #define XDS100_nEMU1 (1<<4)
3027 #define XDS100_LOOPBACK (1<<6)
3028 static int xds100v2_init(void)
3029 {
3030  /* These are in the lower byte */
3031  nTRST = 0x10;
3032  nTRSTnOE = 0x10;
3033 
3034  /* These aren't actually used on 14 pin connectors
3035  * These are in the upper byte */
3036  nSRST = 0x01;
3037  nSRSTnOE = 0x01;
3038 
3039  low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3041 
3043  LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3044  return ERROR_JTAG_INIT_FAILED;
3045  }
3046 
3047  high_output = 0;
3049 
3050  /* initialize high byte for jtag */
3052  LOG_ERROR("couldn't put CPLD in to reset with 'xds100v2' layout");
3053  return ERROR_JTAG_INIT_FAILED;
3054  }
3055 
3057 
3058  /* initialize high byte for jtag */
3060  LOG_ERROR("couldn't bring CPLD out of reset with 'xds100v2' layout");
3061  return ERROR_JTAG_INIT_FAILED;
3062  }
3063 
3064  return ERROR_OK;
3065 }
3066 
3067 static void olimex_jtag_blink(void)
3068 {
3069  /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
3070  * ACBUS3 is bit 3 of the GPIOH port
3071  */
3072  high_output ^= 0x08;
3073 
3074  buffer_write(0x82);
3077 }
3078 
3079 static void flyswatter_jtag_blink(unsigned char led)
3080 {
3081  buffer_write(0x82);
3082  buffer_write(high_output ^ led);
3084 }
3085 
3086 static void flyswatter1_jtag_blink(void)
3087 {
3088  /*
3089  * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
3090  */
3091  flyswatter_jtag_blink(0xc);
3092 }
3093 
3094 static void flyswatter2_jtag_blink(void)
3095 {
3096  /*
3097  * Flyswatter2 only has one LED connected to ACBUS2
3098  */
3099  flyswatter_jtag_blink(0x4);
3100 }
3101 
3102 static void turtle_jtag_blink(void)
3103 {
3104  /*
3105  * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
3106  */
3107  if (high_output & 0x08)
3108  high_output = 0x04;
3109  else
3110  high_output = 0x08;
3111 
3112  buffer_write(0x82);
3115 }
3116 
3117 static void lisa_l_blink(void)
3118 {
3119  /*
3120  * Lisa/L has two LEDs connected to BCBUS3 and BCBUS4
3121  */
3122  if (high_output & 0x10)
3123  high_output = 0x08;
3124  else
3125  high_output = 0x10;
3126 
3127  buffer_write(0x82);
3130 }
3131 
3132 static void flossjtag_blink(void)
3133 {
3134  /*
3135  * Floss-JTAG has two LEDs connected to ACBUS3 and ACBUS4
3136  */
3137  if (high_output & 0x10)
3138  high_output = 0x08;
3139  else
3140  high_output = 0x10;
3141 
3142  buffer_write(0x82);
3145 }
3146 
3147 static int ft2232_quit(void)
3148 {
3149 #if BUILD_FT2232_FTD2XX == 1
3150 
3151  FT_Close(ftdih);
3152 #elif BUILD_FT2232_LIBFTDI == 1
3153  ftdi_usb_close(&ftdic);
3154 
3155  ftdi_deinit(&ftdic);
3156 #endif
3157 
3158  free(ft2232_buffer);
3159  ft2232_buffer = NULL;
3160 
3161  return ERROR_OK;
3162 }
3163 
3164 COMMAND_HANDLER(ft2232_handle_device_desc_command)
3165 {
3166  char *cp;
3167  char buf[200];
3168  if (CMD_ARGC == 1) {
3169  ft2232_device_desc = strdup(CMD_ARGV[0]);
3170  cp = strchr(ft2232_device_desc, 0);
3171  /* under Win32, the FTD2XX driver appends an "A" to the end
3172  * of the description, this examines the given desc
3173  * and creates the 'missing' _A or non_A variable. */
3174  if ((cp[-1] == 'A') && (cp[-2] == ' ')) {
3175  /* it was, so make this the "A" version. */
3177  /* and *CREATE* the non-A version. */
3178  strcpy(buf, ft2232_device_desc);
3179  cp = strchr(buf, 0);
3180  cp[-2] = 0;
3181  ft2232_device_desc = strdup(buf);
3182  } else {
3183  /* <space > A not defined
3184  * so create it */
3185  sprintf(buf, "%s A", ft2232_device_desc);
3186  ft2232_device_desc_A = strdup(buf);
3187  }
3188  } else
3189  LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
3190 
3191  return ERROR_OK;
3192 }
3193 
3194 COMMAND_HANDLER(ft2232_handle_serial_command)
3195 {
3196  if (CMD_ARGC == 1)
3197  ft2232_serial = strdup(CMD_ARGV[0]);
3198  else
3200 
3201  return ERROR_OK;
3202 }
3203 
3204 COMMAND_HANDLER(ft2232_handle_layout_command)
3205 {
3206  if (CMD_ARGC != 1)
3208 
3209  if (layout) {
3210  LOG_ERROR("already specified ft2232_layout %s",
3211  layout->name);
3212  return (strcmp(layout->name, CMD_ARGV[0]) != 0)
3213  ? ERROR_FAIL
3214  : ERROR_OK;
3215  }
3216 
3217  for (const struct ft2232_layout *l = ft2232_layouts; l->name; l++) {
3218  if (strcmp(l->name, CMD_ARGV[0]) == 0) {
3219  layout = l;
3220  ft2232_channel = l->channel;
3221  return ERROR_OK;
3222  }
3223  }
3224 
3225  LOG_ERROR("No FT2232 layout '%s' found", CMD_ARGV[0]);
3226  return ERROR_FAIL;
3227 }
3228 
3229 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
3230 {
3231  if (CMD_ARGC > MAX_USB_IDS * 2) {
3232  LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
3233  "(maximum is %d pairs)", MAX_USB_IDS);
3234  CMD_ARGC = MAX_USB_IDS * 2;
3235  }
3236  if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
3237  LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
3238  if (CMD_ARGC < 2)
3240  /* remove the incomplete trailing id */
3241  CMD_ARGC -= 1;
3242  }
3243 
3244  unsigned i;
3245  for (i = 0; i < CMD_ARGC; i += 2) {
3246  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
3247  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
3248  }
3249 
3250  /*
3251  * Explicitly terminate, in case there are multiples instances of
3252  * ft2232_vid_pid.
3253  */
3254  ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
3255 
3256  return ERROR_OK;
3257 }
3258 
3259 COMMAND_HANDLER(ft2232_handle_latency_command)
3260 {
3261  if (CMD_ARGC == 1)
3262  ft2232_latency = atoi(CMD_ARGV[0]);
3263  else
3265 
3266  return ERROR_OK;
3267 }
3268 
3269 COMMAND_HANDLER(ft2232_handle_channel_command)
3270 {
3271  if (CMD_ARGC == 1) {
3272  ft2232_channel = atoi(CMD_ARGV[0]);
3273  if (ft2232_channel < 0 || ft2232_channel > 4)
3274  LOG_ERROR("ft2232_channel must be in the 0 to 4 range");
3275  } else
3276  LOG_ERROR("expected exactly one argument to ft2232_channel <ch>");
3277 
3278  return ERROR_OK;
3279 }
3280 
3281 static int ft2232_stableclocks(int num_cycles, struct jtag_command *cmd)
3282 {
3283  int retval = 0;
3284 
3285  /* 7 bits of either ones or zeros. */
3286  uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
3287 
3288  while (num_cycles > 0) {
3289  /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
3290  * at most 7 bits per invocation. Here we invoke it potentially
3291  * several times.
3292  */
3293  int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
3294 
3296  if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
3297  retval = ERROR_JTAG_QUEUE_FAILED;
3298 
3299  first_unsent = cmd;
3300  }
3301 
3302  /* there are no state transitions in this code, so omit state tracking */
3303 
3304  /* command "Clock Data to TMS/CS Pin (no Read)" */
3305  buffer_write(0x4b);
3306 
3307  /* scan 7 bit */
3308  buffer_write(bitcount_per_command - 1);
3309 
3310  /* TMS data bits are either all zeros or ones to stay in the current stable state */
3311  buffer_write(tms);
3312 
3313  require_send = 1;
3314 
3315  num_cycles -= bitcount_per_command;
3316  }
3317 
3318  return retval;
3319 }
3320 
3321 /* ---------------------------------------------------------------------
3322  * Support for IceBear JTAG adapter from Section5:
3323  * http://section5.ch/icebear
3324  *
3325  * Author: Sten, debian@sansys-electronic.com
3326  */
3327 
3328 /* Icebear pin layout
3329  *
3330  * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
3331  * GND GND | 4 3| n.c.
3332  * ADBUS3 TMS | 6 5| ADBUS6 VCC
3333  * ADBUS0 TCK | 8 7| ADBUS7 (GND)
3334  * ADBUS4 nTRST |10 9| ACBUS0 (GND)
3335  * ADBUS1 TDI |12 11| ACBUS1 (GND)
3336  * ADBUS2 TDO |14 13| GND GND
3337  *
3338  * ADBUS0 O L TCK ACBUS0 GND
3339  * ADBUS1 O L TDI ACBUS1 GND
3340  * ADBUS2 I TDO ACBUS2 n.c.
3341  * ADBUS3 O H TMS ACBUS3 n.c.
3342  * ADBUS4 O H nTRST
3343  * ADBUS5 O H nSRST
3344  * ADBUS6 - VCC
3345  * ADBUS7 - GND
3346  */
3347 static int icebear_jtag_init(void)
3348 {
3349  low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3350  low_output = 0x08; /* high: TMS; low: TCK TDI */
3351  nTRST = 0x10;
3352  nSRST = 0x20;
3353 
3355  if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3356  low_direction &= ~nTRST; /* nTRST high impedance */
3357  else {
3358  low_direction |= nTRST;
3359  low_output |= nTRST;
3360  }
3361 
3362  low_direction |= nSRST;
3363  low_output |= nSRST;
3364 
3365  /* initialize low byte for jtag */
3367  LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3368  return ERROR_JTAG_INIT_FAILED;
3369  }
3370 
3371  high_output = 0x0;
3372  high_direction = 0x00;
3373 
3374  /* initialize high byte for jtag */
3376  LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3377  return ERROR_JTAG_INIT_FAILED;
3378  }
3379 
3380  return ERROR_OK;
3381 }
3382 
3383 static void icebear_jtag_reset(int trst, int srst)
3384 {
3385  if (trst == 1) {
3386  low_direction |= nTRST;
3387  low_output &= ~nTRST;
3388  } else if (trst == 0) {
3390  if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3391  low_direction &= ~nTRST;
3392  else
3393  low_output |= nTRST;
3394  }
3395 
3396  if (srst == 1)
3397  low_output &= ~nSRST;
3398  else if (srst == 0)
3399  low_output |= nSRST;
3400 
3401  /* command "set data bits low byte" */
3402  buffer_write(0x80);
3405 
3406  LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x",
3407  trst,
3408  srst,
3409  low_output,
3410  low_direction);
3411 }
3412 
3413 /* ---------------------------------------------------------------------
3414  * Support for Signalyzer H2 and Signalyzer H4
3415  * JTAG adapter from Xverve Technologies Inc.
3416  * http://www.signalyzer.com or http://www.xverve.com
3417  *
3418  * Author: Oleg Seiljus, oleg@signalyzer.com
3419  */
3420 static unsigned char signalyzer_h_side;
3421 static unsigned int signalyzer_h_adapter_type;
3422 
3423 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3424 
3425 #if BUILD_FT2232_FTD2XX == 1
3426 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3427 #endif
3428 
3429 #define SIGNALYZER_COMMAND_ADDR 128
3430 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3431 
3432 #define SIGNALYZER_COMMAND_VERSION 0x41
3433 #define SIGNALYZER_COMMAND_RESET 0x42
3434 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3435 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3436 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3437 #define SIGNALYZER_COMMAND_LED_SET 0x53
3438 #define SIGNALYZER_COMMAND_ADC 0x54
3439 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3440 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3441 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3442 #define SIGNALYZER_COMMAND_I2C 0x58
3443 
3444 #define SIGNALYZER_CHAN_A 1
3445 #define SIGNALYZER_CHAN_B 2
3446 /* LEDS use channel C */
3447 #define SIGNALYZER_CHAN_C 4
3448 
3449 #define SIGNALYZER_LED_GREEN 1
3450 #define SIGNALYZER_LED_RED 2
3451 
3452 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3453 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3454 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3455 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3456 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3457 
3458 
3459 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3460 {
3461 #if BUILD_FT2232_FTD2XX == 1
3462  return FT_WriteEE(ftdih, address, value);
3463 #elif BUILD_FT2232_LIBFTDI == 1
3464  return 0;
3465 #endif
3466 }
3467 
3468 #if BUILD_FT2232_FTD2XX == 1
3469 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3470 {
3471  return FT_ReadEE(ftdih, address, value);
3472 }
3473 #endif
3474 
3475 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3476  int on_time_ms, int off_time_ms, unsigned char cycles)
3477 {
3478  unsigned char on_time;
3479  unsigned char off_time;
3480 
3481  if (on_time_ms < 0xFFFF)
3482  on_time = (unsigned char)(on_time_ms / 62);
3483  else
3484  on_time = 0xFF;
3485 
3486  off_time = (unsigned char)(off_time_ms / 62);
3487 
3488 #if BUILD_FT2232_FTD2XX == 1
3489  FT_STATUS status;
3490 
3492  ((uint32_t)(channel << 8) | led));
3493  if (status != FT_OK) {
3494  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3495  ftd2xx_status_string(status));
3496  return ERROR_JTAG_DEVICE_ERROR;
3497  }
3498 
3500  ((uint32_t)(on_time << 8) | off_time));
3501  if (status != FT_OK) {
3502  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3503  ftd2xx_status_string(status));
3504  return ERROR_JTAG_DEVICE_ERROR;
3505  }
3506 
3508  ((uint32_t)cycles));
3509  if (status != FT_OK) {
3510  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3511  ftd2xx_status_string(status));
3512  return ERROR_JTAG_DEVICE_ERROR;
3513  }
3514 
3517  if (status != FT_OK) {
3518  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3519  ftd2xx_status_string(status));
3520  return ERROR_JTAG_DEVICE_ERROR;
3521  }
3522 
3523  return ERROR_OK;
3524 #elif BUILD_FT2232_LIBFTDI == 1
3525  int retval;
3526 
3528  ((uint32_t)(channel << 8) | led));
3529  if (retval < 0) {
3530  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3531  ftdi_get_error_string(&ftdic));
3532  return ERROR_JTAG_DEVICE_ERROR;
3533  }
3534 
3536  ((uint32_t)(on_time << 8) | off_time));
3537  if (retval < 0) {
3538  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3539  ftdi_get_error_string(&ftdic));
3540  return ERROR_JTAG_DEVICE_ERROR;
3541  }
3542 
3544  (uint32_t)cycles);
3545  if (retval < 0) {
3546  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3547  ftdi_get_error_string(&ftdic));
3548  return ERROR_JTAG_DEVICE_ERROR;
3549  }
3550 
3553  if (retval < 0) {
3554  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3555  ftdi_get_error_string(&ftdic));
3556  return ERROR_JTAG_DEVICE_ERROR;
3557  }
3558 
3559  return ERROR_OK;
3560 #endif
3561 }
3562 
3563 static int signalyzer_h_init(void)
3564 {
3565 #if BUILD_FT2232_FTD2XX == 1
3566  FT_STATUS status;
3567  int i;
3568 #endif
3569 
3570  char *end_of_desc;
3571 
3572  uint16_t read_buf[12] = { 0 };
3573 
3574  /* turn on center green led */
3576  0xFFFF, 0x00, 0x00);
3577 
3578  /* determine what channel config wants to open
3579  * TODO: change me... current implementation is made to work
3580  * with openocd description parsing.
3581  */
3582  end_of_desc = strrchr(ft2232_device_desc, 0x00);
3583 
3584  if (end_of_desc) {
3585  signalyzer_h_side = *(end_of_desc - 1);
3586  if (signalyzer_h_side == 'B')
3587  signalyzer_h_side = SIGNALYZER_CHAN_B;
3588  else
3589  signalyzer_h_side = SIGNALYZER_CHAN_A;
3590  } else {
3591  LOG_ERROR("No Channel was specified");
3592  return ERROR_FAIL;
3593  }
3594 
3595  signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
3596  1000, 1000, 0xFF);
3597 
3598 #if BUILD_FT2232_FTD2XX == 1
3599  /* read signalyzer versionining information */
3602  if (status != FT_OK) {
3603  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3604  ftd2xx_status_string(status));
3605  return ERROR_JTAG_DEVICE_ERROR;
3606  }
3607 
3608  for (i = 0; i < 10; i++) {
3609  status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i),
3610  &read_buf[i]);
3611  if (status != FT_OK) {
3612  LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3613  ftd2xx_status_string(status));
3614  return ERROR_JTAG_DEVICE_ERROR;
3615  }
3616  }
3617 
3618  LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
3619  read_buf[0], read_buf[1], read_buf[2], read_buf[3],
3620  read_buf[4], read_buf[5], read_buf[6]);
3621 
3622  /* set gpio register */
3624  (uint32_t)(signalyzer_h_side << 8));
3625  if (status != FT_OK) {
3626  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3627  ftd2xx_status_string(status));
3628  return ERROR_JTAG_DEVICE_ERROR;
3629  }
3630 
3632  if (status != FT_OK) {
3633  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3634  ftd2xx_status_string(status));
3635  return ERROR_JTAG_DEVICE_ERROR;
3636  }
3637 
3640  if (status != FT_OK) {
3641  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3642  ftd2xx_status_string(status));
3643  return ERROR_JTAG_DEVICE_ERROR;
3644  }
3645 
3646  /* read adapter type information */
3648  ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3649  if (status != FT_OK) {
3650  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3651  ftd2xx_status_string(status));
3652  return ERROR_JTAG_DEVICE_ERROR;
3653  }
3654 
3655  status = signalyzer_h_ctrl_write(
3656  (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000);
3657  if (status != FT_OK) {
3658  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3659  ftd2xx_status_string(status));
3660  return ERROR_JTAG_DEVICE_ERROR;
3661  }
3662 
3663  status = signalyzer_h_ctrl_write(
3664  (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008);
3665  if (status != FT_OK) {
3666  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3667  ftd2xx_status_string(status));
3668  return ERROR_JTAG_DEVICE_ERROR;
3669  }
3670 
3673  if (status != FT_OK) {
3674  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3675  ftd2xx_status_string(status));
3676  return ERROR_JTAG_DEVICE_ERROR;
3677  }
3678 
3679  usleep(100000);
3680 
3681  status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR, &read_buf[0]);
3682  if (status != FT_OK) {
3683  LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3684  ftd2xx_status_string(status));
3685  return ERROR_JTAG_DEVICE_ERROR;
3686  }
3687 
3688  if (read_buf[0] != 0x0498)
3689  signalyzer_h_adapter_type = 0x0000;
3690  else {
3691  for (i = 0; i < 4; i++) {
3692  status = signalyzer_h_ctrl_read((SIGNALYZER_DATA_BUFFER_ADDR + i), &read_buf[i]);
3693  if (status != FT_OK) {
3694  LOG_ERROR("signalyzer_h_ctrl_read returned: %s",
3695  ftd2xx_status_string(status));
3696  return ERROR_JTAG_DEVICE_ERROR;
3697  }
3698  }
3699 
3700  signalyzer_h_adapter_type = read_buf[0];
3701  }
3702 
3703 #elif BUILD_FT2232_LIBFTDI == 1
3704  /* currently libftdi does not allow reading individual eeprom
3705  * locations, therefore adapter type cannot be detected.
3706  * override with most common type
3707  */
3708  signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
3709 #endif
3710 
3712 
3713  /* ADAPTOR: EM_LT16_A */
3714  if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3715  LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
3716  "detected. (HW: %2x).", (read_buf[1] >> 8));
3717 
3718  nTRST = 0x10;
3719  nTRSTnOE = 0x10;
3720  nSRST = 0x20;
3721  nSRSTnOE = 0x20;
3722 
3723  low_output = 0x08;
3724  low_direction = 0x1b;
3725 
3726  high_output = 0x0;
3727  high_direction = 0x0;
3728 
3729  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3730  low_direction &= ~nTRSTnOE; /* nTRST input */
3731  low_output &= ~nTRST; /* nTRST = 0 */
3732  } else {
3733  low_direction |= nTRSTnOE; /* nTRST output */
3734  low_output |= nTRST; /* nTRST = 1 */
3735  }
3736 
3737  if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3738  low_direction |= nSRSTnOE; /* nSRST output */
3739  low_output |= nSRST; /* nSRST = 1 */
3740  } else {
3741  low_direction &= ~nSRSTnOE; /* nSRST input */
3742  low_output &= ~nSRST; /* nSRST = 0 */
3743  }
3744 
3745 #if BUILD_FT2232_FTD2XX == 1
3746  /* enable power to the module */
3748  ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3749  if (status != FT_OK) {
3750  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3751  ftd2xx_status_string(status));
3752  return ERROR_JTAG_DEVICE_ERROR;
3753  }
3754 
3757  if (status != FT_OK) {
3758  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3759  ftd2xx_status_string(status));
3760  return ERROR_JTAG_DEVICE_ERROR;
3761  }
3762 
3763  /* set gpio mode register */
3765  (uint32_t)(signalyzer_h_side << 8));
3766  if (status != FT_OK) {
3767  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3768  ftd2xx_status_string(status));
3769  return ERROR_JTAG_DEVICE_ERROR;
3770  }
3771 
3773  if (status != FT_OK) {
3774  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3775  ftd2xx_status_string(status));
3776  return ERROR_JTAG_DEVICE_ERROR;
3777  }
3778 
3780  if (status != FT_OK) {
3781  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3782  ftd2xx_status_string(status));
3783  return ERROR_JTAG_DEVICE_ERROR;
3784  }
3785 
3786  /* set gpio register */
3788  (uint32_t)(signalyzer_h_side << 8));
3789  if (status != FT_OK) {
3790  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3791  ftd2xx_status_string(status));
3792  return ERROR_JTAG_DEVICE_ERROR;
3793  }
3794 
3796  if (status != FT_OK) {
3797  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3798  ftd2xx_status_string(status));
3799  return ERROR_JTAG_DEVICE_ERROR;
3800  }
3801 
3804  if (status != FT_OK) {
3805  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3806  ftd2xx_status_string(status));
3807  return ERROR_JTAG_DEVICE_ERROR;
3808  }
3809 #endif
3810  }
3811  /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
3812  else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
3813  (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
3814  (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
3815  (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
3816  if (signalyzer_h_adapter_type
3818  LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
3819  "detected. (HW: %2x).", (read_buf[1] >> 8));
3820  else if (signalyzer_h_adapter_type
3822  LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
3823  "(ARM JTAG with PSU) detected. (HW: %2x).",
3824  (read_buf[1] >> 8));
3825  else if (signalyzer_h_adapter_type
3827  LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
3828  "detected. (HW: %2x).", (read_buf[1] >> 8));
3829  else if (signalyzer_h_adapter_type
3831  LOG_INFO("Signalyzer: EM-JTAG-P "
3832  "(Generic JTAG with PSU) detected. (HW: %2x).",
3833  (read_buf[1] >> 8));
3834 
3835  nTRST = 0x02;
3836  nTRSTnOE = 0x04;
3837  nSRST = 0x08;
3838  nSRSTnOE = 0x10;
3839 
3840  low_output = 0x08;
3841  low_direction = 0x1b;
3842 
3843  high_output = 0x0;
3844  high_direction = 0x1f;
3845 
3846  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3847  high_output |= nTRSTnOE;
3848  high_output &= ~nTRST;
3849  } else {
3850  high_output &= ~nTRSTnOE;
3851  high_output |= nTRST;
3852  }
3853 
3854  if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3855  high_output &= ~nSRSTnOE;
3856  high_output |= nSRST;
3857  } else {
3858  high_output |= nSRSTnOE;
3859  high_output &= ~nSRST;
3860  }
3861 
3862 #if BUILD_FT2232_FTD2XX == 1
3863  /* enable power to the module */
3865  ((uint32_t)(signalyzer_h_side << 8) | 0x01));
3866  if (status != FT_OK) {
3867  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3868  ftd2xx_status_string(status));
3869  return ERROR_JTAG_DEVICE_ERROR;
3870  }
3871 
3874  if (status != FT_OK) {
3875  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3876  ftd2xx_status_string(status));
3877  return ERROR_JTAG_DEVICE_ERROR;
3878  }
3879 
3880  /* set gpio mode register (IO_16 and IO_17 set as analog
3881  * inputs, other is gpio)
3882  */
3884  (uint32_t)(signalyzer_h_side << 8));
3885  if (status != FT_OK) {
3886  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3887  ftd2xx_status_string(status));
3888  return ERROR_JTAG_DEVICE_ERROR;
3889  }
3890 
3892  if (status != FT_OK) {
3893  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3894  ftd2xx_status_string(status));
3895  return ERROR_JTAG_DEVICE_ERROR;
3896  }
3897 
3899  if (status != FT_OK) {
3900  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3901  ftd2xx_status_string(status));
3902  return ERROR_JTAG_DEVICE_ERROR;
3903  }
3904 
3905  /* set gpio register (all inputs, for -P modules,
3906  * PSU will be turned off)
3907  */
3909  (uint32_t)(signalyzer_h_side << 8));
3910  if (status != FT_OK) {
3911  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3912  ftd2xx_status_string(status));
3913  return ERROR_JTAG_DEVICE_ERROR;
3914  }
3915 
3917  if (status != FT_OK) {
3918  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3919  ftd2xx_status_string(status));
3920  return ERROR_JTAG_DEVICE_ERROR;
3921  }
3922 
3924  if (status != FT_OK) {
3925  LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3926  ftd2xx_status_string(status));
3927  return ERROR_JTAG_DEVICE_ERROR;
3928  }
3929 #endif
3930  } else if (signalyzer_h_adapter_type == 0x0000) {
3931  LOG_INFO("Signalyzer: No external modules were detected.");
3932 
3933  nTRST = 0x10;
3934  nTRSTnOE = 0x10;
3935  nSRST = 0x20;
3936  nSRSTnOE = 0x20;
3937 
3938  low_output = 0x08;
3939  low_direction = 0x1b;
3940 
3941  high_output = 0x0;
3942  high_direction = 0x0;
3943 
3944  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
3945  low_direction &= ~nTRSTnOE; /* nTRST input */
3946  low_output &= ~nTRST; /* nTRST = 0 */
3947  } else {
3948  low_direction |= nTRSTnOE; /* nTRST output */
3949  low_output |= nTRST; /* nTRST = 1 */
3950  }
3951 
3952  if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
3953  low_direction |= nSRSTnOE; /* nSRST output */
3954  low_output |= nSRST; /* nSRST = 1 */
3955  } else {
3956  low_direction &= ~nSRSTnOE; /* nSRST input */
3957  low_output &= ~nSRST; /* nSRST = 0 */
3958  }
3959  } else {
3960  LOG_ERROR("Unknown module type is detected: %.4x",
3961  signalyzer_h_adapter_type);
3962  return ERROR_JTAG_DEVICE_ERROR;
3963  }
3964 
3965  /* initialize low byte of controller for jtag operation */
3967  LOG_ERROR("couldn't initialize Signalyzer-H layout");
3968  return ERROR_JTAG_INIT_FAILED;
3969  }
3970 
3971 #if BUILD_FT2232_FTD2XX == 1
3972  if (ftdi_device == FT_DEVICE_2232H) {
3973  /* initialize high byte of controller for jtag operation */
3975  LOG_ERROR("couldn't initialize Signalyzer-H layout");
3976  return ERROR_JTAG_INIT_FAILED;
3977  }
3978  }
3979 #elif BUILD_FT2232_LIBFTDI == 1
3980  if (ftdi_device == TYPE_2232H) {
3981  /* initialize high byte of controller for jtag operation */
3983  LOG_ERROR("couldn't initialize Signalyzer-H layout");
3984  return ERROR_JTAG_INIT_FAILED;
3985  }
3986  }
3987 #endif
3988  return ERROR_OK;
3989 }
3990 
3991 static void signalyzer_h_reset(int trst, int srst)
3992 {
3994 
3995  /* ADAPTOR: EM_LT16_A */
3996  if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A) {
3997  if (trst == 1) {
3998  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
3999  /* switch to output pin (output is low) */
4001  else
4002  /* switch output low */
4003  low_output &= ~nTRST;
4004  } else if (trst == 0) {
4005  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4006  /* switch to input pin (high-Z + internal
4007  * and external pullup) */
4008  low_direction &= ~nTRSTnOE;
4009  else
4010  /* switch output high */
4011  low_output |= nTRST;
4012  }
4013 
4014  if (srst == 1) {
4015  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4016  /* switch output low */
4017  low_output &= ~nSRST;
4018  else
4019  /* switch to output pin (output is low) */
4021  } else if (srst == 0) {
4022  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4023  /* switch output high */
4024  low_output |= nSRST;
4025  else
4026  /* switch to input pin (high-Z) */
4027  low_direction &= ~nSRSTnOE;
4028  }
4029 
4030  /* command "set data bits low byte" */
4031  buffer_write(0x80);
4034  LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4035  "low_direction: 0x%2.2x",
4036  trst, srst, low_output, low_direction);
4037  }
4038  /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
4039  else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
4040  (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
4041  (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
4042  (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)) {
4043  if (trst == 1) {
4044  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4045  high_output &= ~nTRSTnOE;
4046  else
4047  high_output &= ~nTRST;
4048  } else if (trst == 0) {
4049  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4050  high_output |= nTRSTnOE;
4051  else
4052  high_output |= nTRST;
4053  }
4054 
4055  if (srst == 1) {
4056  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4057  high_output &= ~nSRST;
4058  else
4059  high_output &= ~nSRSTnOE;
4060  } else if (srst == 0) {
4061  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4062  high_output |= nSRST;
4063  else
4064  high_output |= nSRSTnOE;
4065  }
4066 
4067  /* command "set data bits high byte" */
4068  buffer_write(0x82);
4071  LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
4072  "high_direction: 0x%2.2x",
4073  trst, srst, high_output, high_direction);
4074  } else if (signalyzer_h_adapter_type == 0x0000) {
4075  if (trst == 1) {
4076  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4077  /* switch to output pin (output is low) */
4079  else
4080  /* switch output low */
4081  low_output &= ~nTRST;
4082  } else if (trst == 0) {
4083  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4084  /* switch to input pin (high-Z + internal
4085  * and external pullup) */
4086  low_direction &= ~nTRSTnOE;
4087  else
4088  /* switch output high */
4089  low_output |= nTRST;
4090  }
4091 
4092  if (srst == 1) {
4093  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4094  /* switch output low */
4095  low_output &= ~nSRST;
4096  else
4097  /* switch to output pin (output is low) */
4099  } else if (srst == 0) {
4100  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4101  /* switch output high */
4102  low_output |= nSRST;
4103  else
4104  /* switch to input pin (high-Z) */
4105  low_direction &= ~nSRSTnOE;
4106  }
4107 
4108  /* command "set data bits low byte" */
4109  buffer_write(0x80);
4112  LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
4113  "low_direction: 0x%2.2x",
4114  trst, srst, low_output, low_direction);
4115  }
4116 }
4117 
4118 static void signalyzer_h_blink(void)
4119 {
4120  signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
4121 }
4122 
4123 /********************************************************************
4124  * Support for KT-LINK
4125  * JTAG adapter from KRISTECH
4126  * http://www.kristech.eu
4127  *******************************************************************/
4128 static int ktlink_init(void)
4129 {
4130  uint8_t swd_en = 0x20; /* 0x20 SWD disable, 0x00 SWD enable (ADBUS5) */
4131 
4132  low_output = 0x08 | swd_en; /* value; TMS=1,TCK=0,TDI=0,SWD=swd_en */
4133  low_direction = 0x3B; /* out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in */
4134 
4135  /* initialize low byte for jtag */
4137  LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4138  return ERROR_JTAG_INIT_FAILED;
4139  }
4140 
4141  nTRST = 0x01;
4142  nSRST = 0x02;
4143  nTRSTnOE = 0x04;
4144  nSRSTnOE = 0x08;
4145 
4146  high_output = 0x80; /* turn LED on */
4147  high_direction = 0xFF; /* all outputs */
4148 
4150 
4151  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
4152  high_output |= nTRSTnOE;
4153  high_output &= ~nTRST;
4154  } else {
4155  high_output &= ~nTRSTnOE;
4156  high_output |= nTRST;
4157  }
4158 
4159  if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
4160  high_output &= ~nSRSTnOE;
4161  high_output |= nSRST;
4162  } else {
4163  high_output |= nSRSTnOE;
4164  high_output &= ~nSRST;
4165  }
4166 
4167  /* initialize high byte for jtag */
4169  LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
4170  return ERROR_JTAG_INIT_FAILED;
4171  }
4172 
4173  return ERROR_OK;
4174 }
4175 
4176 static void ktlink_reset(int trst, int srst)
4177 {
4179 
4180  if (trst == 1) {
4181  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4182  high_output &= ~nTRSTnOE;
4183  else
4184  high_output &= ~nTRST;
4185  } else if (trst == 0) {
4186  if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
4187  high_output |= nTRSTnOE;
4188  else
4189  high_output |= nTRST;
4190  }
4191 
4192  if (srst == 1) {
4193  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4194  high_output &= ~nSRST;
4195  else
4196  high_output &= ~nSRSTnOE;
4197  } else if (srst == 0) {
4198  if (jtag_reset_config & RESET_SRST_PUSH_PULL)
4199  high_output |= nSRST;
4200  else
4201  high_output |= nSRSTnOE;
4202  }
4203 
4204  buffer_write(0x82); /* command "set data bits high byte" */
4207  LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x",
4208  trst,
4209  srst,
4210  high_output,
4211  high_direction);
4212 }
4213 
4214 static void ktlink_blink(void)
4215 {
4216  /* LED connected to ACBUS7 */
4217  high_output ^= 0x80;
4218 
4219  buffer_write(0x82); /* command "set data bits high byte" */
4222 }
4223 
4224 /********************************************************************
4225  * Support for Digilent HS-1
4226  * JTAG adapter from Digilent
4227  * http://www.digilent.com
4228  * Author: Stephane Bonnet bonnetst@hds.utc.fr
4229  *******************************************************************/
4230 
4231 static int digilent_hs1_init(void)
4232 {
4233  /* the adapter only supports the base JTAG signals, no nTRST
4234  nor nSRST */
4235  low_output = 0x88;
4236  low_direction = 0x8b;
4237 
4238  /* initialize low byte for jtag */
4240  LOG_ERROR("couldn't initialize FT2232 with 'digilent_hs1' layout");
4241  return ERROR_JTAG_INIT_FAILED;
4242  }
4243  return ERROR_OK;
4244 }
4245 
4246 static void digilent_hs1_reset(int trst, int srst)
4247 {
4248  /* Dummy function, no reset signals supported. */
4249 }
4250 
4252  {
4253  .name = "ft2232_device_desc",
4254  .handler = &ft2232_handle_device_desc_command,
4255  .mode = COMMAND_CONFIG,
4256  .help = "set the USB device description of the FTDI FT2232 device",
4257  .usage = "description_string",
4258  },
4259  {
4260  .name = "ft2232_serial",
4261  .handler = &ft2232_handle_serial_command,
4262  .mode = COMMAND_CONFIG,
4263  .help = "set the serial number of the FTDI FT2232 device",
4264  .usage = "serial_string",
4265  },
4266  {
4267  .name = "ft2232_layout",
4268  .handler = &ft2232_handle_layout_command,
4269  .mode = COMMAND_CONFIG,
4270  .help = "set the layout of the FT2232 GPIO signals used "
4271  "to control output-enables and reset signals",
4272  .usage = "layout_name",
4273  },
4274  {
4275  .name = "ft2232_vid_pid",
4276  .handler = &ft2232_handle_vid_pid_command,
4277  .mode = COMMAND_CONFIG,
4278  .help = "the vendor ID and product ID of the FTDI FT2232 device",
4279  .usage = "(vid pid)* ",
4280  },
4281  {
4282  .name = "ft2232_latency",
4283  .handler = &ft2232_handle_latency_command,
4284  .mode = COMMAND_CONFIG,
4285  .help = "set the FT2232 latency timer to a new value",
4286  .usage = "value",
4287  },
4288  {
4289  .name = "ft2232_channel",
4290  .handler = &ft2232_handle_channel_command,
4291  .mode = COMMAND_CONFIG,
4292  .help = "set the FT2232 channel to a new value",
4293  .usage = "value",
4294  },
4296 };
4297 
4299  .name = "ft2232",
4300  .supported = DEBUG_CAP_TMS_SEQ,
4301  .commands = ft2232_command_handlers,
4302  .transports = jtag_only,
4303 
4304  .init = ft2232_init,
4305  .quit = ft2232_quit,
4306  .speed = ft2232_speed,
4307  .speed_div = ft2232_speed_div,
4308  .khz = ft2232_khz,
4309  .execute_queue = ft2232_execute_queue,
4310 };