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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)