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

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)