Change return value on error.
[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 <transport/transport.h>
85 #include <helper/time_support.h>
86
87 #if IS_CYGWIN == 1
88 #include <windows.h>
89 #endif
90
91 #include <assert.h>
92
93 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
94 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
95 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
96 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
97 #endif
98
99 /* FT2232 access library includes */
100 #if BUILD_FT2232_FTD2XX == 1
101 #include <ftd2xx.h>
102 #include "ftd2xx_common.h"
103
104 enum ftdi_interface
105 {
106 INTERFACE_ANY = 0,
107 INTERFACE_A = 1,
108 INTERFACE_B = 2,
109 INTERFACE_C = 3,
110 INTERFACE_D = 4
111 };
112
113 #elif BUILD_FT2232_LIBFTDI == 1
114 #include <ftdi.h>
115 #endif
116
117 /* max TCK for the high speed devices 30000 kHz */
118 #define FTDI_2232H_4232H_MAX_TCK 30000
119 /* max TCK for the full speed devices 6000 kHz */
120 #define FTDI_2232C_MAX_TCK 6000
121 /* this speed value tells that RTCK is requested */
122 #define RTCK_SPEED -1
123
124 /*
125 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
126 * errors with a retry count of 100. Increasing it solves the problem for me.
127 * - Dimitar
128 *
129 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
130 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
131 * to something sane.
132 */
133 #define LIBFTDI_READ_RETRY_COUNT 2000
134
135 #ifndef BUILD_FT2232_HIGHSPEED
136 #if BUILD_FT2232_FTD2XX == 1
137 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
138 #elif BUILD_FT2232_LIBFTDI == 1
139 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
140 #endif
141 #endif
142
143 /**
144 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
145 * stable state. Calling code must ensure that current state is stable,
146 * that verification is not done in here.
147 *
148 * @param num_cycles The number of clocks cycles to send.
149 * @param cmd The command to send.
150 *
151 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
152 */
153 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
154
155 static char * ft2232_device_desc_A = NULL;
156 static char* ft2232_device_desc = NULL;
157 static char* ft2232_serial = NULL;
158 static uint8_t ft2232_latency = 2;
159 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
160
161 #define MAX_USB_IDS 8
162 /* vid = pid = 0 marks the end of the list */
163 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
164 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
165
166 struct ft2232_layout {
167 char* name;
168 int (*init)(void);
169 void (*reset)(int trst, int srst);
170 void (*blink)(void);
171 int channel;
172 };
173
174 /* init procedures for supported layouts */
175 static int usbjtag_init(void);
176 static int jtagkey_init(void);
177 static int lm3s811_jtag_init(void);
178 static int icdi_jtag_init(void);
179 static int olimex_jtag_init(void);
180 static int flyswatter1_init(void);
181 static int flyswatter2_init(void);
182 static int minimodule_init(void);
183 static int turtle_init(void);
184 static int comstick_init(void);
185 static int stm32stick_init(void);
186 static int axm0432_jtag_init(void);
187 static int sheevaplug_init(void);
188 static int icebear_jtag_init(void);
189 static int cortino_jtag_init(void);
190 static int signalyzer_init(void);
191 static int signalyzer_h_init(void);
192 static int ktlink_init(void);
193 static int redbee_init(void);
194 static int lisa_l_init(void);
195 static int flossjtag_init(void);
196 static int xds100v2_init(void);
197
198 /* reset procedures for supported layouts */
199 static void ftx23_reset(int trst, int srst);
200 static void jtagkey_reset(int trst, int srst);
201 static void olimex_jtag_reset(int trst, int srst);
202 static void flyswatter1_reset(int trst, int srst);
203 static void flyswatter2_reset(int trst, int srst);
204 static void minimodule_reset(int trst, int srst);
205 static void turtle_reset(int trst, int srst);
206 static void comstick_reset(int trst, int srst);
207 static void stm32stick_reset(int trst, int srst);
208 static void axm0432_jtag_reset(int trst, int srst);
209 static void sheevaplug_reset(int trst, int srst);
210 static void icebear_jtag_reset(int trst, int srst);
211 static void signalyzer_h_reset(int trst, int srst);
212 static void ktlink_reset(int trst, int srst);
213 static void redbee_reset(int trst, int srst);
214 static void xds100v2_reset(int trst, int srst);
215
216 /* blink procedures for layouts that support a blinking led */
217 static void olimex_jtag_blink(void);
218 static void flyswatter1_jtag_blink(void);
219 static void flyswatter2_jtag_blink(void);
220 static void turtle_jtag_blink(void);
221 static void signalyzer_h_blink(void);
222 static void ktlink_blink(void);
223 static void lisa_l_blink(void);
224 static void flossjtag_blink(void);
225
226 /* common transport support options */
227
228 //static const char *jtag_and_swd[] = { "jtag", "swd", NULL };
229
230 static const struct ft2232_layout ft2232_layouts[] =
231 {
232 { .name = "usbjtag",
233 .init = usbjtag_init,
234 .reset = ftx23_reset,
235 },
236 { .name = "jtagkey",
237 .init = jtagkey_init,
238 .reset = jtagkey_reset,
239 },
240 { .name = "jtagkey_prototype_v1",
241 .init = jtagkey_init,
242 .reset = jtagkey_reset,
243 },
244 { .name = "oocdlink",
245 .init = jtagkey_init,
246 .reset = jtagkey_reset,
247 },
248 { .name = "signalyzer",
249 .init = signalyzer_init,
250 .reset = ftx23_reset,
251 },
252 { .name = "evb_lm3s811",
253 .init = lm3s811_jtag_init,
254 .reset = ftx23_reset,
255 },
256 { .name = "luminary_icdi",
257 .init = icdi_jtag_init,
258 .reset = ftx23_reset,
259 },
260 { .name = "olimex-jtag",
261 .init = olimex_jtag_init,
262 .reset = olimex_jtag_reset,
263 .blink = olimex_jtag_blink
264 },
265 { .name = "flyswatter",
266 .init = flyswatter1_init,
267 .reset = flyswatter1_reset,
268 .blink = flyswatter1_jtag_blink
269 },
270 { .name = "flyswatter2",
271 .init = flyswatter2_init,
272 .reset = flyswatter2_reset,
273 .blink = flyswatter2_jtag_blink
274 },
275 { .name = "minimodule",
276 .init = minimodule_init,
277 .reset = minimodule_reset,
278 },
279 { .name = "turtelizer2",
280 .init = turtle_init,
281 .reset = turtle_reset,
282 .blink = turtle_jtag_blink
283 },
284 { .name = "comstick",
285 .init = comstick_init,
286 .reset = comstick_reset,
287 },
288 { .name = "stm32stick",
289 .init = stm32stick_init,
290 .reset = stm32stick_reset,
291 },
292 { .name = "axm0432_jtag",
293 .init = axm0432_jtag_init,
294 .reset = axm0432_jtag_reset,
295 },
296 { .name = "sheevaplug",
297 .init = sheevaplug_init,
298 .reset = sheevaplug_reset,
299 },
300 { .name = "icebear",
301 .init = icebear_jtag_init,
302 .reset = icebear_jtag_reset,
303 },
304 { .name = "cortino",
305 .init = cortino_jtag_init,
306 .reset = comstick_reset,
307 },
308 { .name = "signalyzer-h",
309 .init = signalyzer_h_init,
310 .reset = signalyzer_h_reset,
311 .blink = signalyzer_h_blink
312 },
313 { .name = "ktlink",
314 .init = ktlink_init,
315 .reset = ktlink_reset,
316 .blink = ktlink_blink
317 },
318 { .name = "redbee-econotag",
319 .init = redbee_init,
320 .reset = redbee_reset,
321 },
322 { .name = "redbee-usb",
323 .init = redbee_init,
324 .reset = redbee_reset,
325 .channel = INTERFACE_B,
326 },
327 { .name = "lisa-l",
328 .init = lisa_l_init,
329 .reset = ftx23_reset,
330 .blink = lisa_l_blink,
331 .channel = INTERFACE_B,
332 },
333 { .name = "flossjtag",
334 .init = flossjtag_init,
335 .reset = ftx23_reset,
336 .blink = flossjtag_blink,
337 },
338 { .name = "xds100v2",
339 .init = xds100v2_init,
340 .reset = xds100v2_reset,
341 },
342 { .name = NULL, /* END OF TABLE */ },
343 };
344
345 /* bitmask used to drive nTRST; usually a GPIOLx signal */
346 static uint8_t nTRST;
347 static uint8_t nTRSTnOE;
348 /* bitmask used to drive nSRST; usually a GPIOLx signal */
349 static uint8_t nSRST;
350 static uint8_t nSRSTnOE;
351
352 /** the layout being used with this debug session */
353 static const struct ft2232_layout *layout;
354
355 /** default bitmask values driven on DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
356 static uint8_t low_output = 0x0;
357
358 /* note that direction bit == 1 means that signal is an output */
359
360 /** default direction bitmask for DBUS: TCK/TDI/TDO/TMS and GPIOL(0..4) */
361 static uint8_t low_direction = 0x0;
362 /** default value bitmask for CBUS GPIOH(0..4) */
363 static uint8_t high_output = 0x0;
364 /** default direction bitmask for CBUS GPIOH(0..4) */
365 static uint8_t high_direction = 0x0;
366
367 #if BUILD_FT2232_FTD2XX == 1
368 static FT_HANDLE ftdih = NULL;
369 static FT_DEVICE ftdi_device = 0;
370 #elif BUILD_FT2232_LIBFTDI == 1
371 static struct ftdi_context ftdic;
372 static enum ftdi_chip_type ftdi_device;
373 #endif
374
375 static struct jtag_command* first_unsent; /* next command that has to be sent */
376 static int require_send;
377
378 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
379
380 "There is a significant difference between libftdi and libftd2xx. The latter
381 one allows to schedule up to 64*64 bytes of result data while libftdi fails
382 with more than 4*64. As a consequence, the FT2232 driver is forced to
383 perform around 16x more USB transactions for long command streams with TDO
384 capture when running with libftdi."
385
386 No idea how we get
387 #define FT2232_BUFFER_SIZE 131072
388 a comment would have been nice.
389 */
390
391 #if BUILD_FT2232_FTD2XX == 1
392 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*64)
393 #else
394 #define FT2232_BUFFER_READ_QUEUE_SIZE (64*4)
395 #endif
396
397 #define FT2232_BUFFER_SIZE 131072
398
399 static uint8_t* ft2232_buffer = NULL;
400 static int ft2232_buffer_size = 0;
401 static int ft2232_read_pointer = 0;
402 static int ft2232_expect_read = 0;
403
404 /**
405 * Function buffer_write
406 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
407 * @param val is the byte to send.
408 */
409 static inline void buffer_write(uint8_t val)
410 {
411 assert(ft2232_buffer);
412 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
413 ft2232_buffer[ft2232_buffer_size++] = val;
414 }
415
416 /**
417 * Function buffer_read
418 * returns a byte from the byte buffer.
419 */
420 static inline uint8_t buffer_read(void)
421 {
422 assert(ft2232_buffer);
423 assert(ft2232_read_pointer < ft2232_buffer_size);
424 return ft2232_buffer[ft2232_read_pointer++];
425 }
426
427 /**
428 * Clocks out \a bit_count bits on the TMS line, starting with the least
429 * significant bit of tms_bits and progressing to more significant bits.
430 * Rigorous state transition logging is done here via tap_set_state().
431 *
432 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
433 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
434 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
435 * is often used for this, 0x4b.
436 *
437 * @param tms_bits Holds the sequence of bits to send.
438 * @param tms_count Tells how many bits in the sequence.
439 * @param tdi_bit A single bit to pass on to TDI before the first TCK
440 * cycle and held static for the duration of TMS clocking.
441 *
442 * See the MPSSE spec referenced above.
443 */
444 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
445 {
446 uint8_t tms_byte;
447 int i;
448 int tms_ndx; /* bit index into tms_byte */
449
450 assert(tms_count > 0);
451
452 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
453 mpsse_cmd, tms_bits, tms_count);
454
455 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
456 {
457 bool bit = tms_bits & 1;
458
459 if (bit)
460 tms_byte |= (1 << tms_ndx);
461
462 /* always do state transitions in public view */
463 tap_set_state(tap_state_transition(tap_get_state(), bit));
464
465 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
466 also increment.
467 */
468 ++tms_ndx;
469
470 if (tms_ndx == 7 || i == tms_count-1)
471 {
472 buffer_write(mpsse_cmd);
473 buffer_write(tms_ndx - 1);
474
475 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
476 TMS/CS and is held static for the duration of TMS/CS clocking.
477 */
478 buffer_write(tms_byte | (tdi_bit << 7));
479 }
480 }
481 }
482
483 /**
484 * Function get_tms_buffer_requirements
485 * returns what clock_tms() will consume if called with
486 * same \a bit_count.
487 */
488 static inline int get_tms_buffer_requirements(int bit_count)
489 {
490 return ((bit_count + 6)/7) * 3;
491 }
492
493 /**
494 * Function move_to_state
495 * moves the TAP controller from the current state to a
496 * \a goal_state through a path given by tap_get_tms_path(). State transition
497 * logging is performed by delegation to clock_tms().
498 *
499 * @param goal_state is the destination state for the move.
500 */
501 static void move_to_state(tap_state_t goal_state)
502 {
503 tap_state_t start_state = tap_get_state();
504
505 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
506 lookup of the required TMS pattern to move to this state from the
507 start state.
508 */
509
510 /* do the 2 lookups */
511 int tms_bits = tap_get_tms_path(start_state, goal_state);
512 int tms_count = tap_get_tms_path_len(start_state, goal_state);
513
514 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
515
516 clock_tms(0x4b, tms_bits, tms_count, 0);
517 }
518
519 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
520 {
521 #if BUILD_FT2232_FTD2XX == 1
522 FT_STATUS status;
523 DWORD dw_bytes_written = 0;
524 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
525 {
526 *bytes_written = dw_bytes_written;
527 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
528 return ERROR_JTAG_DEVICE_ERROR;
529 }
530 else
531 {
532 *bytes_written = dw_bytes_written;
533 }
534 #elif BUILD_FT2232_LIBFTDI == 1
535 int retval;
536 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
537 {
538 *bytes_written = 0;
539 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
540 return ERROR_JTAG_DEVICE_ERROR;
541 }
542 else
543 {
544 *bytes_written = retval;
545 }
546 #endif
547
548 if (*bytes_written != (uint32_t)size)
549 {
550 return ERROR_JTAG_DEVICE_ERROR;
551 }
552
553 return ERROR_OK;
554 }
555
556 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
557 {
558 #if BUILD_FT2232_FTD2XX == 1
559 DWORD dw_bytes_read;
560 FT_STATUS status;
561 int timeout = 5;
562 *bytes_read = 0;
563
564 while ((*bytes_read < size) && timeout--)
565 {
566 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
567 *bytes_read, &dw_bytes_read)) != FT_OK)
568 {
569 *bytes_read = 0;
570 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
571 return ERROR_JTAG_DEVICE_ERROR;
572 }
573 *bytes_read += dw_bytes_read;
574 }
575
576 #elif BUILD_FT2232_LIBFTDI == 1
577 int retval;
578 int timeout = LIBFTDI_READ_RETRY_COUNT;
579 *bytes_read = 0;
580
581 while ((*bytes_read < size) && timeout--)
582 {
583 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
584 {
585 *bytes_read = 0;
586 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
587 return ERROR_JTAG_DEVICE_ERROR;
588 }
589 *bytes_read += retval;
590 }
591
592 #endif
593
594 if (*bytes_read < size)
595 {
596 LOG_ERROR("couldn't read enough bytes from "
597 "FT2232 device (%i < %i)",
598 (unsigned)*bytes_read,
599 (unsigned)size);
600 return ERROR_JTAG_DEVICE_ERROR;
601 }
602
603 return ERROR_OK;
604 }
605
606 static bool ft2232_device_is_highspeed(void)
607 {
608 #if BUILD_FT2232_FTD2XX == 1
609 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
610 #elif BUILD_FT2232_LIBFTDI == 1
611 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
612 #endif
613 }
614
615 /*
616 * Commands that only apply to the FT2232H and FT4232H devices.
617 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
618 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
619 */
620
621 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
622 {
623 uint8_t buf = enable ? 0x96 : 0x97;
624 LOG_DEBUG("%2.2x", buf);
625
626 uint32_t bytes_written;
627 int retval;
628
629 if ((retval = ft2232_write(&buf, sizeof(buf), &bytes_written)) != ERROR_OK)
630 {
631 LOG_ERROR("couldn't write command to %s adaptive clocking"
632 , enable ? "enable" : "disable");
633 return retval;
634 }
635
636 return ERROR_OK;
637 }
638
639 /**
640 * Enable/disable the clk divide by 5 of the 60MHz master clock.
641 * This result in a JTAG clock speed range of 91.553Hz-6MHz
642 * respective 457.763Hz-30MHz.
643 */
644 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
645 {
646 uint32_t bytes_written;
647 uint8_t buf = enable ? 0x8b : 0x8a;
648
649 if (ft2232_write(&buf, sizeof(buf), &bytes_written) != ERROR_OK)
650 {
651 LOG_ERROR("couldn't write command to %s clk divide by 5"
652 , enable ? "enable" : "disable");
653 return ERROR_JTAG_INIT_FAILED;
654 }
655 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
656 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
657
658 return ERROR_OK;
659 }
660
661 static int ft2232_speed(int speed)
662 {
663 uint8_t buf[3];
664 int retval;
665 uint32_t bytes_written;
666
667 retval = ERROR_OK;
668 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
669 if (ft2232_device_is_highspeed())
670 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
671 else if (enable_adaptive_clocking)
672 {
673 LOG_ERROR("ft2232 device %lu does not support RTCK"
674 , (long unsigned int)ftdi_device);
675 return ERROR_FAIL;
676 }
677
678 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
679 return retval;
680
681 buf[0] = 0x86; /* command "set divisor" */
682 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
683 buf[2] = (speed >> 8) & 0xff; /* valueH */
684
685 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
686 if ((retval = ft2232_write(buf, sizeof(buf), &bytes_written)) != ERROR_OK)
687 {
688 LOG_ERROR("couldn't set FT2232 TCK speed");
689 return retval;
690 }
691
692 return ERROR_OK;
693 }
694
695 static int ft2232_speed_div(int speed, int* khz)
696 {
697 /* Take a look in the FT2232 manual,
698 * AN2232C-01 Command Processor for
699 * MPSSE and MCU Host Bus. Chapter 3.8 */
700
701 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
702
703 return ERROR_OK;
704 }
705
706 static int ft2232_khz(int khz, int* jtag_speed)
707 {
708 if (khz == 0)
709 {
710 if (ft2232_device_is_highspeed())
711 {
712 *jtag_speed = RTCK_SPEED;
713 return ERROR_OK;
714 }
715 else
716 {
717 LOG_DEBUG("RCLK not supported");
718 return ERROR_FAIL;
719 }
720 }
721
722 /* Take a look in the FT2232 manual,
723 * AN2232C-01 Command Processor for
724 * MPSSE and MCU Host Bus. Chapter 3.8
725 *
726 * We will calc here with a multiplier
727 * of 10 for better rounding later. */
728
729 /* Calc speed, (ft2232_max_tck / khz) - 1 */
730 /* Use 65000 for better rounding */
731 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
732
733 /* Add 0.9 for rounding */
734 *jtag_speed += 9;
735
736 /* Calc real speed */
737 *jtag_speed = *jtag_speed / 10;
738
739 /* Check if speed is greater than 0 */
740 if (*jtag_speed < 0)
741 {
742 *jtag_speed = 0;
743 }
744
745 /* Check max value */
746 if (*jtag_speed > 0xFFFF)
747 {
748 *jtag_speed = 0xFFFF;
749 }
750
751 return ERROR_OK;
752 }
753
754 static void ft2232_end_state(tap_state_t state)
755 {
756 if (tap_is_state_stable(state))
757 tap_set_end_state(state);
758 else
759 {
760 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
761 exit(-1);
762 }
763 }
764
765 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
766 {
767 int num_bytes = (scan_size + 7) / 8;
768 int bits_left = scan_size;
769 int cur_byte = 0;
770
771 while (num_bytes-- > 1)
772 {
773 buffer[cur_byte++] = buffer_read();
774 bits_left -= 8;
775 }
776
777 buffer[cur_byte] = 0x0;
778
779 /* There is one more partial byte left from the clock data in/out instructions */
780 if (bits_left > 1)
781 {
782 buffer[cur_byte] = buffer_read() >> 1;
783 }
784 /* 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 */
785 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
786 }
787
788 static void ft2232_debug_dump_buffer(void)
789 {
790 int i;
791 char line[256];
792 char* line_p = line;
793
794 for (i = 0; i < ft2232_buffer_size; i++)
795 {
796 line_p += snprintf(line_p, sizeof(line) - (line_p - line), "%2.2x ", ft2232_buffer[i]);
797 if (i % 16 == 15)
798 {
799 LOG_DEBUG("%s", line);
800 line_p = line;
801 }
802 }
803
804 if (line_p != line)
805 LOG_DEBUG("%s", line);
806 }
807
808 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
809 {
810 struct jtag_command* cmd;
811 uint8_t* buffer;
812 int scan_size;
813 enum scan_type type;
814 int retval;
815 uint32_t bytes_written = 0;
816 uint32_t bytes_read = 0;
817
818 #ifdef _DEBUG_USB_IO_
819 struct timeval start, inter, inter2, end;
820 struct timeval d_inter, d_inter2, d_end;
821 #endif
822
823 #ifdef _DEBUG_USB_COMMS_
824 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
825 ft2232_debug_dump_buffer();
826 #endif
827
828 #ifdef _DEBUG_USB_IO_
829 gettimeofday(&start, NULL);
830 #endif
831
832 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
833 {
834 LOG_ERROR("couldn't write MPSSE commands to FT2232");
835 return retval;
836 }
837
838 #ifdef _DEBUG_USB_IO_
839 gettimeofday(&inter, NULL);
840 #endif
841
842 if (ft2232_expect_read)
843 {
844 /* FIXME this "timeout" is never changed ... */
845 int timeout = LIBFTDI_READ_RETRY_COUNT;
846 ft2232_buffer_size = 0;
847
848 #ifdef _DEBUG_USB_IO_
849 gettimeofday(&inter2, NULL);
850 #endif
851
852 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
853 {
854 LOG_ERROR("couldn't read from FT2232");
855 return retval;
856 }
857
858 #ifdef _DEBUG_USB_IO_
859 gettimeofday(&end, NULL);
860
861 timeval_subtract(&d_inter, &inter, &start);
862 timeval_subtract(&d_inter2, &inter2, &start);
863 timeval_subtract(&d_end, &end, &start);
864
865 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
866 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
867 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
868 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
869 #endif
870
871 ft2232_buffer_size = bytes_read;
872
873 if (ft2232_expect_read != ft2232_buffer_size)
874 {
875 LOG_ERROR("ft2232_expect_read (%i) != "
876 "ft2232_buffer_size (%i) "
877 "(%i retries)",
878 ft2232_expect_read,
879 ft2232_buffer_size,
880 LIBFTDI_READ_RETRY_COUNT - timeout);
881 ft2232_debug_dump_buffer();
882
883 exit(-1);
884 }
885
886 #ifdef _DEBUG_USB_COMMS_
887 LOG_DEBUG("read buffer (%i retries): %i bytes",
888 LIBFTDI_READ_RETRY_COUNT - timeout,
889 ft2232_buffer_size);
890 ft2232_debug_dump_buffer();
891 #endif
892 }
893
894 ft2232_expect_read = 0;
895 ft2232_read_pointer = 0;
896
897 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
898 * that wasn't handled by a caller-provided error handler
899 */
900 retval = ERROR_OK;
901
902 cmd = first;
903 while (cmd != last)
904 {
905 switch (cmd->type)
906 {
907 case JTAG_SCAN:
908 type = jtag_scan_type(cmd->cmd.scan);
909 if (type != SCAN_OUT)
910 {
911 scan_size = jtag_scan_size(cmd->cmd.scan);
912 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
913 ft2232_read_scan(type, buffer, scan_size);
914 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
915 retval = ERROR_JTAG_QUEUE_FAILED;
916 free(buffer);
917 }
918 break;
919
920 default:
921 break;
922 }
923
924 cmd = cmd->next;
925 }
926
927 ft2232_buffer_size = 0;
928
929 return retval;
930 }
931
932 /**
933 * Function ft2232_add_pathmove
934 * moves the TAP controller from the current state to a new state through the
935 * given path, where path is an array of tap_state_t's.
936 *
937 * @param path is an array of tap_stat_t which gives the states to traverse through
938 * ending with the last state at path[num_states-1]
939 * @param num_states is the count of state steps to move through
940 */
941 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
942 {
943 int state_count = 0;
944
945 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
946
947 DEBUG_JTAG_IO("-");
948
949 /* this loop verifies that the path is legal and logs each state in the path */
950 while (num_states)
951 {
952 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
953 int bit_count = 0;
954 int num_states_batch = num_states > 7 ? 7 : num_states;
955
956 /* command "Clock Data to TMS/CS Pin (no Read)" */
957 buffer_write(0x4b);
958
959 /* number of states remaining */
960 buffer_write(num_states_batch - 1);
961
962 while (num_states_batch--) {
963 /* either TMS=0 or TMS=1 must work ... */
964 if (tap_state_transition(tap_get_state(), false)
965 == path[state_count])
966 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
967 else if (tap_state_transition(tap_get_state(), true)
968 == path[state_count])
969 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
970
971 /* ... or else the caller goofed BADLY */
972 else {
973 LOG_ERROR("BUG: %s -> %s isn't a valid "
974 "TAP state transition",
975 tap_state_name(tap_get_state()),
976 tap_state_name(path[state_count]));
977 exit(-1);
978 }
979
980 tap_set_state(path[state_count]);
981 state_count++;
982 num_states--;
983 }
984
985 buffer_write(tms_byte);
986 }
987 tap_set_end_state(tap_get_state());
988 }
989
990 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
991 {
992 int num_bytes = (scan_size + 7) / 8;
993 int bits_left = scan_size;
994 int cur_byte = 0;
995 int last_bit;
996
997 if (!ir_scan)
998 {
999 if (tap_get_state() != TAP_DRSHIFT)
1000 {
1001 move_to_state(TAP_DRSHIFT);
1002 }
1003 }
1004 else
1005 {
1006 if (tap_get_state() != TAP_IRSHIFT)
1007 {
1008 move_to_state(TAP_IRSHIFT);
1009 }
1010 }
1011
1012 /* add command for complete bytes */
1013 while (num_bytes > 1)
1014 {
1015 int thisrun_bytes;
1016 if (type == SCAN_IO)
1017 {
1018 /* Clock Data Bytes In and Out LSB First */
1019 buffer_write(0x39);
1020 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1021 }
1022 else if (type == SCAN_OUT)
1023 {
1024 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1025 buffer_write(0x19);
1026 /* LOG_DEBUG("added TDI bytes (o)"); */
1027 }
1028 else if (type == SCAN_IN)
1029 {
1030 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1031 buffer_write(0x28);
1032 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1033 }
1034
1035 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1036 num_bytes -= thisrun_bytes;
1037
1038 buffer_write((uint8_t) (thisrun_bytes - 1));
1039 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1040
1041 if (type != SCAN_IN)
1042 {
1043 /* add complete bytes */
1044 while (thisrun_bytes-- > 0)
1045 {
1046 buffer_write(buffer[cur_byte++]);
1047 bits_left -= 8;
1048 }
1049 }
1050 else /* (type == SCAN_IN) */
1051 {
1052 bits_left -= 8 * (thisrun_bytes);
1053 }
1054 }
1055
1056 /* the most signifcant bit is scanned during TAP movement */
1057 if (type != SCAN_IN)
1058 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1059 else
1060 last_bit = 0;
1061
1062 /* process remaining bits but the last one */
1063 if (bits_left > 1)
1064 {
1065 if (type == SCAN_IO)
1066 {
1067 /* Clock Data Bits In and Out LSB First */
1068 buffer_write(0x3b);
1069 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1070 }
1071 else if (type == SCAN_OUT)
1072 {
1073 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1074 buffer_write(0x1b);
1075 /* LOG_DEBUG("added TDI bits (o)"); */
1076 }
1077 else if (type == SCAN_IN)
1078 {
1079 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1080 buffer_write(0x2a);
1081 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1082 }
1083
1084 buffer_write(bits_left - 2);
1085 if (type != SCAN_IN)
1086 buffer_write(buffer[cur_byte]);
1087 }
1088
1089 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
1090 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
1091 {
1092 if (type == SCAN_IO)
1093 {
1094 /* Clock Data Bits In and Out LSB First */
1095 buffer_write(0x3b);
1096 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1097 }
1098 else if (type == SCAN_OUT)
1099 {
1100 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1101 buffer_write(0x1b);
1102 /* LOG_DEBUG("added TDI bits (o)"); */
1103 }
1104 else if (type == SCAN_IN)
1105 {
1106 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1107 buffer_write(0x2a);
1108 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1109 }
1110 buffer_write(0x0);
1111 buffer_write(last_bit);
1112 }
1113 else
1114 {
1115 int tms_bits;
1116 int tms_count;
1117 uint8_t mpsse_cmd;
1118
1119 /* move from Shift-IR/DR to end state */
1120 if (type != SCAN_OUT)
1121 {
1122 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
1123 /* This must be coordinated with the bit shifts in ft2232_read_scan */
1124 tms_bits = 0x01;
1125 tms_count = 2;
1126 /* Clock Data to TMS/CS Pin with Read */
1127 mpsse_cmd = 0x6b;
1128 }
1129 else
1130 {
1131 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1132 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1133 /* Clock Data to TMS/CS Pin (no Read) */
1134 mpsse_cmd = 0x4b;
1135 }
1136
1137 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
1138 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1139 }
1140
1141 if (tap_get_state() != tap_get_end_state())
1142 {
1143 move_to_state(tap_get_end_state());
1144 }
1145 }
1146
1147 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
1148 {
1149 int num_bytes = (scan_size + 7) / 8;
1150 int bits_left = scan_size;
1151 int cur_byte = 0;
1152 int last_bit;
1153 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
1154 uint8_t* receive_pointer = receive_buffer;
1155 uint32_t bytes_written;
1156 uint32_t bytes_read;
1157 int retval;
1158 int thisrun_read = 0;
1159
1160 if (cmd->ir_scan)
1161 {
1162 LOG_ERROR("BUG: large IR scans are not supported");
1163 exit(-1);
1164 }
1165
1166 if (tap_get_state() != TAP_DRSHIFT)
1167 {
1168 move_to_state(TAP_DRSHIFT);
1169 }
1170
1171 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1172 {
1173 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1174 exit(-1);
1175 }
1176 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1177 ft2232_buffer_size, (int)bytes_written);
1178 ft2232_buffer_size = 0;
1179
1180 /* add command for complete bytes */
1181 while (num_bytes > 1)
1182 {
1183 int thisrun_bytes;
1184
1185 if (type == SCAN_IO)
1186 {
1187 /* Clock Data Bytes In and Out LSB First */
1188 buffer_write(0x39);
1189 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1190 }
1191 else if (type == SCAN_OUT)
1192 {
1193 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1194 buffer_write(0x19);
1195 /* LOG_DEBUG("added TDI bytes (o)"); */
1196 }
1197 else if (type == SCAN_IN)
1198 {
1199 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1200 buffer_write(0x28);
1201 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1202 }
1203
1204 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1205 thisrun_read = thisrun_bytes;
1206 num_bytes -= thisrun_bytes;
1207 buffer_write((uint8_t) (thisrun_bytes - 1));
1208 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1209
1210 if (type != SCAN_IN)
1211 {
1212 /* add complete bytes */
1213 while (thisrun_bytes-- > 0)
1214 {
1215 buffer_write(buffer[cur_byte]);
1216 cur_byte++;
1217 bits_left -= 8;
1218 }
1219 }
1220 else /* (type == SCAN_IN) */
1221 {
1222 bits_left -= 8 * (thisrun_bytes);
1223 }
1224
1225 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1226 {
1227 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1228 exit(-1);
1229 }
1230 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1231 ft2232_buffer_size,
1232 (int)bytes_written);
1233 ft2232_buffer_size = 0;
1234
1235 if (type != SCAN_OUT)
1236 {
1237 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1238 {
1239 LOG_ERROR("couldn't read from FT2232");
1240 exit(-1);
1241 }
1242 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1243 thisrun_read,
1244 (int)bytes_read);
1245 receive_pointer += bytes_read;
1246 }
1247 }
1248
1249 thisrun_read = 0;
1250
1251 /* the most signifcant bit is scanned during TAP movement */
1252 if (type != SCAN_IN)
1253 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1254 else
1255 last_bit = 0;
1256
1257 /* process remaining bits but the last one */
1258 if (bits_left > 1)
1259 {
1260 if (type == SCAN_IO)
1261 {
1262 /* Clock Data Bits In and Out LSB First */
1263 buffer_write(0x3b);
1264 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1265 }
1266 else if (type == SCAN_OUT)
1267 {
1268 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1269 buffer_write(0x1b);
1270 /* LOG_DEBUG("added TDI bits (o)"); */
1271 }
1272 else if (type == SCAN_IN)
1273 {
1274 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1275 buffer_write(0x2a);
1276 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1277 }
1278 buffer_write(bits_left - 2);
1279 if (type != SCAN_IN)
1280 buffer_write(buffer[cur_byte]);
1281
1282 if (type != SCAN_OUT)
1283 thisrun_read += 2;
1284 }
1285
1286 if (tap_get_end_state() == TAP_DRSHIFT)
1287 {
1288 if (type == SCAN_IO)
1289 {
1290 /* Clock Data Bits In and Out LSB First */
1291 buffer_write(0x3b);
1292 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1293 }
1294 else if (type == SCAN_OUT)
1295 {
1296 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1297 buffer_write(0x1b);
1298 /* LOG_DEBUG("added TDI bits (o)"); */
1299 }
1300 else if (type == SCAN_IN)
1301 {
1302 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1303 buffer_write(0x2a);
1304 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1305 }
1306 buffer_write(0x0);
1307 buffer_write(last_bit);
1308 }
1309 else
1310 {
1311 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1312 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1313 uint8_t mpsse_cmd;
1314
1315 /* move from Shift-IR/DR to end state */
1316 if (type != SCAN_OUT)
1317 {
1318 /* Clock Data to TMS/CS Pin with Read */
1319 mpsse_cmd = 0x6b;
1320 /* LOG_DEBUG("added TMS scan (read)"); */
1321 }
1322 else
1323 {
1324 /* Clock Data to TMS/CS Pin (no Read) */
1325 mpsse_cmd = 0x4b;
1326 /* LOG_DEBUG("added TMS scan (no read)"); */
1327 }
1328
1329 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1330 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1331 }
1332
1333 if (type != SCAN_OUT)
1334 thisrun_read += 1;
1335
1336 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1337 {
1338 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1339 exit(-1);
1340 }
1341 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1342 ft2232_buffer_size,
1343 (int)bytes_written);
1344 ft2232_buffer_size = 0;
1345
1346 if (type != SCAN_OUT)
1347 {
1348 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1349 {
1350 LOG_ERROR("couldn't read from FT2232");
1351 exit(-1);
1352 }
1353 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1354 thisrun_read,
1355 (int)bytes_read);
1356 }
1357
1358 return ERROR_OK;
1359 }
1360
1361 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1362 {
1363 int predicted_size = 3;
1364 int num_bytes = (scan_size - 1) / 8;
1365
1366 if (tap_get_state() != TAP_DRSHIFT)
1367 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1368
1369 if (type == SCAN_IN) /* only from device to host */
1370 {
1371 /* complete bytes */
1372 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1373
1374 /* remaining bits - 1 (up to 7) */
1375 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1376 }
1377 else /* host to device, or bidirectional */
1378 {
1379 /* complete bytes */
1380 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1381
1382 /* remaining bits -1 (up to 7) */
1383 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1384 }
1385
1386 return predicted_size;
1387 }
1388
1389 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1390 {
1391 int predicted_size = 0;
1392
1393 if (type != SCAN_OUT)
1394 {
1395 /* complete bytes */
1396 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1397
1398 /* remaining bits - 1 */
1399 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1400
1401 /* last bit (from TMS scan) */
1402 predicted_size += 1;
1403 }
1404
1405 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1406
1407 return predicted_size;
1408 }
1409
1410 /* semi-generic FT2232/FT4232 reset code */
1411 static void ftx23_reset(int trst, int srst)
1412 {
1413 enum reset_types jtag_reset_config = jtag_get_reset_config();
1414 if (trst == 1)
1415 {
1416 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1417 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1418 else
1419 low_output &= ~nTRST; /* switch output low */
1420 }
1421 else if (trst == 0)
1422 {
1423 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1424 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1425 else
1426 low_output |= nTRST; /* switch output high */
1427 }
1428
1429 if (srst == 1)
1430 {
1431 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1432 low_output &= ~nSRST; /* switch output low */
1433 else
1434 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1435 }
1436 else if (srst == 0)
1437 {
1438 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1439 low_output |= nSRST; /* switch output high */
1440 else
1441 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1442 }
1443
1444 /* command "set data bits low byte" */
1445 buffer_write(0x80);
1446 buffer_write(low_output);
1447 buffer_write(low_direction);
1448 }
1449
1450 static void jtagkey_reset(int trst, int srst)
1451 {
1452 enum reset_types jtag_reset_config = jtag_get_reset_config();
1453 if (trst == 1)
1454 {
1455 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1456 high_output &= ~nTRSTnOE;
1457 else
1458 high_output &= ~nTRST;
1459 }
1460 else if (trst == 0)
1461 {
1462 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1463 high_output |= nTRSTnOE;
1464 else
1465 high_output |= nTRST;
1466 }
1467
1468 if (srst == 1)
1469 {
1470 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1471 high_output &= ~nSRST;
1472 else
1473 high_output &= ~nSRSTnOE;
1474 }
1475 else if (srst == 0)
1476 {
1477 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1478 high_output |= nSRST;
1479 else
1480 high_output |= nSRSTnOE;
1481 }
1482
1483 /* command "set data bits high byte" */
1484 buffer_write(0x82);
1485 buffer_write(high_output);
1486 buffer_write(high_direction);
1487 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1488 high_direction);
1489 }
1490
1491 static void olimex_jtag_reset(int trst, int srst)
1492 {
1493 enum reset_types jtag_reset_config = jtag_get_reset_config();
1494 if (trst == 1)
1495 {
1496 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1497 high_output &= ~nTRSTnOE;
1498 else
1499 high_output &= ~nTRST;
1500 }
1501 else if (trst == 0)
1502 {
1503 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1504 high_output |= nTRSTnOE;
1505 else
1506 high_output |= nTRST;
1507 }
1508
1509 if (srst == 1)
1510 {
1511 high_output |= nSRST;
1512 }
1513 else if (srst == 0)
1514 {
1515 high_output &= ~nSRST;
1516 }
1517
1518 /* command "set data bits high byte" */
1519 buffer_write(0x82);
1520 buffer_write(high_output);
1521 buffer_write(high_direction);
1522 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1523 high_direction);
1524 }
1525
1526 static void axm0432_jtag_reset(int trst, int srst)
1527 {
1528 if (trst == 1)
1529 {
1530 tap_set_state(TAP_RESET);
1531 high_output &= ~nTRST;
1532 }
1533 else if (trst == 0)
1534 {
1535 high_output |= nTRST;
1536 }
1537
1538 if (srst == 1)
1539 {
1540 high_output &= ~nSRST;
1541 }
1542 else if (srst == 0)
1543 {
1544 high_output |= nSRST;
1545 }
1546
1547 /* command "set data bits low byte" */
1548 buffer_write(0x82);
1549 buffer_write(high_output);
1550 buffer_write(high_direction);
1551 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1552 high_direction);
1553 }
1554
1555 static void flyswatter_reset(int trst, int srst)
1556 {
1557 if (trst == 1)
1558 {
1559 low_output &= ~nTRST;
1560 }
1561 else if (trst == 0)
1562 {
1563 low_output |= nTRST;
1564 }
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("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1580 }
1581
1582 static void flyswatter1_reset(int trst, int srst)
1583 {
1584 flyswatter_reset(trst, srst);
1585 }
1586
1587 static void flyswatter2_reset(int trst, int srst)
1588 {
1589 flyswatter_reset(trst, !srst);
1590 }
1591
1592 static void minimodule_reset(int trst, int srst)
1593 {
1594 if (srst == 1)
1595 {
1596 low_output &= ~nSRST;
1597 }
1598 else if (srst == 0)
1599 {
1600 low_output |= nSRST;
1601 }
1602
1603 /* command "set data bits low byte" */
1604 buffer_write(0x80);
1605 buffer_write(low_output);
1606 buffer_write(low_direction);
1607 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1608 }
1609
1610 static void turtle_reset(int trst, int srst)
1611 {
1612 trst = trst;
1613
1614 if (srst == 1)
1615 {
1616 low_output |= nSRST;
1617 }
1618 else if (srst == 0)
1619 {
1620 low_output &= ~nSRST;
1621 }
1622
1623 /* command "set data bits low byte" */
1624 buffer_write(0x80);
1625 buffer_write(low_output);
1626 buffer_write(low_direction);
1627 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1628 }
1629
1630 static void comstick_reset(int trst, int srst)
1631 {
1632 if (trst == 1)
1633 {
1634 high_output &= ~nTRST;
1635 }
1636 else if (trst == 0)
1637 {
1638 high_output |= nTRST;
1639 }
1640
1641 if (srst == 1)
1642 {
1643 high_output &= ~nSRST;
1644 }
1645 else if (srst == 0)
1646 {
1647 high_output |= nSRST;
1648 }
1649
1650 /* command "set data bits high byte" */
1651 buffer_write(0x82);
1652 buffer_write(high_output);
1653 buffer_write(high_direction);
1654 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1655 high_direction);
1656 }
1657
1658 static void stm32stick_reset(int trst, int srst)
1659 {
1660 if (trst == 1)
1661 {
1662 high_output &= ~nTRST;
1663 }
1664 else if (trst == 0)
1665 {
1666 high_output |= nTRST;
1667 }
1668
1669 if (srst == 1)
1670 {
1671 low_output &= ~nSRST;
1672 }
1673 else if (srst == 0)
1674 {
1675 low_output |= nSRST;
1676 }
1677
1678 /* command "set data bits low byte" */
1679 buffer_write(0x80);
1680 buffer_write(low_output);
1681 buffer_write(low_direction);
1682
1683 /* command "set data bits high 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, high_direction: 0x%2.2x", trst, srst, high_output,
1688 high_direction);
1689 }
1690
1691 static void sheevaplug_reset(int trst, int srst)
1692 {
1693 if (trst == 1)
1694 high_output &= ~nTRST;
1695 else if (trst == 0)
1696 high_output |= nTRST;
1697
1698 if (srst == 1)
1699 high_output &= ~nSRSTnOE;
1700 else if (srst == 0)
1701 high_output |= nSRSTnOE;
1702
1703 /* command "set data bits high byte" */
1704 buffer_write(0x82);
1705 buffer_write(high_output);
1706 buffer_write(high_direction);
1707 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1708 }
1709
1710 static void redbee_reset(int trst, int srst)
1711 {
1712 if (trst == 1)
1713 {
1714 tap_set_state(TAP_RESET);
1715 high_output &= ~nTRST;
1716 }
1717 else if (trst == 0)
1718 {
1719 high_output |= nTRST;
1720 }
1721
1722 if (srst == 1)
1723 {
1724 high_output &= ~nSRST;
1725 }
1726 else if (srst == 0)
1727 {
1728 high_output |= nSRST;
1729 }
1730
1731 /* command "set data bits low byte" */
1732 buffer_write(0x82);
1733 buffer_write(high_output);
1734 buffer_write(high_direction);
1735 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1736 "high_direction: 0x%2.2x", trst, srst, high_output,
1737 high_direction);
1738 }
1739
1740 static void xds100v2_reset(int trst, int srst)
1741 {
1742 if (trst == 1)
1743 {
1744 tap_set_state(TAP_RESET);
1745 high_output &= ~nTRST;
1746 }
1747 else if (trst == 0)
1748 {
1749 high_output |= nTRST;
1750 }
1751
1752 if (srst == 1)
1753 {
1754 high_output |= nSRST;
1755 }
1756 else if (srst == 0)
1757 {
1758 high_output &= ~nSRST;
1759 }
1760
1761 /* command "set data bits low byte" */
1762 buffer_write(0x82);
1763 buffer_write(high_output);
1764 buffer_write(high_direction);
1765 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, "
1766 "high_direction: 0x%2.2x", trst, srst, high_output,
1767 high_direction);
1768 }
1769
1770 static int ft2232_execute_runtest(struct jtag_command *cmd)
1771 {
1772 int retval;
1773 int i;
1774 int predicted_size = 0;
1775 retval = ERROR_OK;
1776
1777 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1778 cmd->cmd.runtest->num_cycles,
1779 tap_state_name(cmd->cmd.runtest->end_state));
1780
1781 /* only send the maximum buffer size that FT2232C can handle */
1782 predicted_size = 0;
1783 if (tap_get_state() != TAP_IDLE)
1784 predicted_size += 3;
1785 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1786 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1787 predicted_size += 3;
1788 if (tap_get_end_state() != TAP_IDLE)
1789 predicted_size += 3;
1790 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1791 {
1792 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1793 retval = ERROR_JTAG_QUEUE_FAILED;
1794 require_send = 0;
1795 first_unsent = cmd;
1796 }
1797 if (tap_get_state() != TAP_IDLE)
1798 {
1799 move_to_state(TAP_IDLE);
1800 require_send = 1;
1801 }
1802 i = cmd->cmd.runtest->num_cycles;
1803 while (i > 0)
1804 {
1805 /* there are no state transitions in this code, so omit state tracking */
1806
1807 /* command "Clock Data to TMS/CS Pin (no Read)" */
1808 buffer_write(0x4b);
1809
1810 /* scan 7 bits */
1811 buffer_write((i > 7) ? 6 : (i - 1));
1812
1813 /* TMS data bits */
1814 buffer_write(0x0);
1815
1816 i -= (i > 7) ? 7 : i;
1817 /* LOG_DEBUG("added TMS scan (no read)"); */
1818 }
1819
1820 ft2232_end_state(cmd->cmd.runtest->end_state);
1821
1822 if (tap_get_state() != tap_get_end_state())
1823 {
1824 move_to_state(tap_get_end_state());
1825 }
1826
1827 require_send = 1;
1828 DEBUG_JTAG_IO("runtest: %i, end in %s",
1829 cmd->cmd.runtest->num_cycles,
1830 tap_state_name(tap_get_end_state()));
1831 return retval;
1832 }
1833
1834 static int ft2232_execute_statemove(struct jtag_command *cmd)
1835 {
1836 int predicted_size = 0;
1837 int retval = ERROR_OK;
1838
1839 DEBUG_JTAG_IO("statemove end in %s",
1840 tap_state_name(cmd->cmd.statemove->end_state));
1841
1842 /* only send the maximum buffer size that FT2232C can handle */
1843 predicted_size = 3;
1844 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1845 {
1846 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1847 retval = ERROR_JTAG_QUEUE_FAILED;
1848 require_send = 0;
1849 first_unsent = cmd;
1850 }
1851 ft2232_end_state(cmd->cmd.statemove->end_state);
1852
1853 /* For TAP_RESET, ignore the current recorded state. It's often
1854 * wrong at server startup, and this transation is critical whenever
1855 * it's requested.
1856 */
1857 if (tap_get_end_state() == TAP_RESET) {
1858 clock_tms(0x4b, 0xff, 5, 0);
1859 require_send = 1;
1860
1861 /* shortest-path move to desired end state */
1862 } else if (tap_get_state() != tap_get_end_state())
1863 {
1864 move_to_state(tap_get_end_state());
1865 require_send = 1;
1866 }
1867
1868 return retval;
1869 }
1870
1871 /**
1872 * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
1873 * (or SWD) state machine.
1874 */
1875 static int ft2232_execute_tms(struct jtag_command *cmd)
1876 {
1877 int retval = ERROR_OK;
1878 unsigned num_bits = cmd->cmd.tms->num_bits;
1879 const uint8_t *bits = cmd->cmd.tms->bits;
1880 unsigned count;
1881
1882 DEBUG_JTAG_IO("TMS: %d bits", num_bits);
1883
1884 /* only send the maximum buffer size that FT2232C can handle */
1885 count = 3 * DIV_ROUND_UP(num_bits, 4);
1886 if (ft2232_buffer_size + 3*count + 1 > FT2232_BUFFER_SIZE) {
1887 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1888 retval = ERROR_JTAG_QUEUE_FAILED;
1889
1890 require_send = 0;
1891 first_unsent = cmd;
1892 }
1893
1894 /* Shift out in batches of at most 6 bits; there's a report of an
1895 * FT2232 bug in this area, where shifting exactly 7 bits can make
1896 * problems with TMS signaling for the last clock cycle:
1897 *
1898 * http://developer.intra2net.com/mailarchive/html/
1899 * libftdi/2009/msg00292.html
1900 *
1901 * Command 0x4b is: "Clock Data to TMS/CS Pin (no Read)"
1902 *
1903 * Note that pathmoves in JTAG are not often seven bits, so that
1904 * isn't a particularly likely situation outside of "special"
1905 * signaling such as switching between JTAG and SWD modes.
1906 */
1907 while (num_bits) {
1908 if (num_bits <= 6) {
1909 buffer_write(0x4b);
1910 buffer_write(num_bits - 1);
1911 buffer_write(*bits & 0x3f);
1912 break;
1913 }
1914
1915 /* Yes, this is lazy ... we COULD shift out more data
1916 * bits per operation, but doing it in nybbles is easy
1917 */
1918 buffer_write(0x4b);
1919 buffer_write(3);
1920 buffer_write(*bits & 0xf);
1921 num_bits -= 4;
1922
1923 count = (num_bits > 4) ? 4 : num_bits;
1924
1925 buffer_write(0x4b);
1926 buffer_write(count - 1);
1927 buffer_write((*bits >> 4) & 0xf);
1928 num_bits -= count;
1929
1930 bits++;
1931 }
1932
1933 require_send = 1;
1934 return retval;
1935 }
1936
1937 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1938 {
1939 int predicted_size = 0;
1940 int retval = ERROR_OK;
1941
1942 tap_state_t* path = cmd->cmd.pathmove->path;
1943 int num_states = cmd->cmd.pathmove->num_states;
1944
1945 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1946 tap_state_name(tap_get_state()),
1947 tap_state_name(path[num_states-1]));
1948
1949 /* only send the maximum buffer size that FT2232C can handle */
1950 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1951 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1952 {
1953 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1954 retval = ERROR_JTAG_QUEUE_FAILED;
1955
1956 require_send = 0;
1957 first_unsent = cmd;
1958 }
1959
1960 ft2232_add_pathmove(path, num_states);
1961 require_send = 1;
1962
1963 return retval;
1964 }
1965
1966 static int ft2232_execute_scan(struct jtag_command *cmd)
1967 {
1968 uint8_t* buffer;
1969 int scan_size; /* size of IR or DR scan */
1970 int predicted_size = 0;
1971 int retval = ERROR_OK;
1972
1973 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1974
1975 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1976
1977 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1978
1979 predicted_size = ft2232_predict_scan_out(scan_size, type);
1980 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1981 {
1982 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1983 /* unsent commands before this */
1984 if (first_unsent != cmd)
1985 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1986 retval = ERROR_JTAG_QUEUE_FAILED;
1987
1988 /* current command */
1989 ft2232_end_state(cmd->cmd.scan->end_state);
1990 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1991 require_send = 0;
1992 first_unsent = cmd->next;
1993 if (buffer)
1994 free(buffer);
1995 return retval;
1996 }
1997 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1998 {
1999 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
2000 first_unsent,
2001 cmd);
2002 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2003 retval = ERROR_JTAG_QUEUE_FAILED;
2004 require_send = 0;
2005 first_unsent = cmd;
2006 }
2007 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
2008 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
2009 ft2232_end_state(cmd->cmd.scan->end_state);
2010 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
2011 require_send = 1;
2012 if (buffer)
2013 free(buffer);
2014 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
2015 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
2016 tap_state_name(tap_get_end_state()));
2017 return retval;
2018
2019 }
2020
2021 static int ft2232_execute_reset(struct jtag_command *cmd)
2022 {
2023 int retval;
2024 int predicted_size = 0;
2025 retval = ERROR_OK;
2026
2027 DEBUG_JTAG_IO("reset trst: %i srst %i",
2028 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2029
2030 /* only send the maximum buffer size that FT2232C can handle */
2031 predicted_size = 3;
2032 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
2033 {
2034 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2035 retval = ERROR_JTAG_QUEUE_FAILED;
2036 require_send = 0;
2037 first_unsent = cmd;
2038 }
2039
2040 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
2041 {
2042 tap_set_state(TAP_RESET);
2043 }
2044
2045 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2046 require_send = 1;
2047
2048 DEBUG_JTAG_IO("trst: %i, srst: %i",
2049 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
2050 return retval;
2051 }
2052
2053 static int ft2232_execute_sleep(struct jtag_command *cmd)
2054 {
2055 int retval;
2056 retval = ERROR_OK;
2057
2058 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
2059
2060 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2061 retval = ERROR_JTAG_QUEUE_FAILED;
2062 first_unsent = cmd->next;
2063 jtag_sleep(cmd->cmd.sleep->us);
2064 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
2065 cmd->cmd.sleep->us,
2066 tap_state_name(tap_get_state()));
2067 return retval;
2068 }
2069
2070 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
2071 {
2072 int retval;
2073 retval = ERROR_OK;
2074
2075 /* this is only allowed while in a stable state. A check for a stable
2076 * state was done in jtag_add_clocks()
2077 */
2078 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
2079 retval = ERROR_JTAG_QUEUE_FAILED;
2080 DEBUG_JTAG_IO("clocks %i while in %s",
2081 cmd->cmd.stableclocks->num_cycles,
2082 tap_state_name(tap_get_state()));
2083 return retval;
2084 }
2085
2086 static int ft2232_execute_command(struct jtag_command *cmd)
2087 {
2088 int retval;
2089
2090 switch (cmd->type)
2091 {
2092 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
2093 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
2094 case JTAG_TLR_RESET: retval = ft2232_execute_statemove(cmd); break;
2095 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
2096 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
2097 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
2098 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
2099 case JTAG_TMS:
2100 retval = ft2232_execute_tms(cmd);
2101 break;
2102 default:
2103 LOG_ERROR("BUG: unknown JTAG command type encountered");
2104 retval = ERROR_JTAG_QUEUE_FAILED;
2105 break;
2106 }
2107 return retval;
2108 }
2109
2110 static int ft2232_execute_queue(void)
2111 {
2112 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
2113 int retval;
2114
2115 first_unsent = cmd; /* next command that has to be sent */
2116 require_send = 0;
2117
2118 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
2119 * that wasn't handled by a caller-provided error handler
2120 */
2121 retval = ERROR_OK;
2122
2123 ft2232_buffer_size = 0;
2124 ft2232_expect_read = 0;
2125
2126 /* blink, if the current layout has that feature */
2127 if (layout->blink)
2128 layout->blink();
2129
2130 while (cmd)
2131 {
2132 /* fill the write buffer with the desired command */
2133 if (ft2232_execute_command(cmd) != ERROR_OK)
2134 retval = ERROR_JTAG_QUEUE_FAILED;
2135 /* Start reading input before FT2232 TX buffer fills up.
2136 * Sometimes this happens because we don't know the
2137 * length of the last command before we execute it. So
2138 * we simple inform the user.
2139 */
2140 cmd = cmd->next;
2141
2142 if (ft2232_expect_read >= FT2232_BUFFER_READ_QUEUE_SIZE )
2143 {
2144 if (ft2232_expect_read > (FT2232_BUFFER_READ_QUEUE_SIZE+1) )
2145 LOG_DEBUG("read buffer size looks too high %d/%d",ft2232_expect_read,(FT2232_BUFFER_READ_QUEUE_SIZE+1));
2146 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2147 retval = ERROR_JTAG_QUEUE_FAILED;
2148 first_unsent = cmd;
2149 }
2150 }
2151
2152 if (require_send > 0)
2153 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2154 retval = ERROR_JTAG_QUEUE_FAILED;
2155
2156 return retval;
2157 }
2158
2159 #if BUILD_FT2232_FTD2XX == 1
2160 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
2161 {
2162 FT_STATUS status;
2163 DWORD deviceID;
2164 char SerialNumber[16];
2165 char Description[64];
2166 DWORD openex_flags = 0;
2167 char* openex_string = NULL;
2168 uint8_t latency_timer;
2169
2170 if (layout == NULL) {
2171 LOG_WARNING("No ft2232 layout specified'");
2172 return ERROR_JTAG_INIT_FAILED;
2173 }
2174
2175 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", layout->name, vid, pid);
2176
2177 #if IS_WIN32 == 0
2178 /* Add non-standard Vid/Pid to the linux driver */
2179 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
2180 {
2181 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
2182 }
2183 #endif
2184
2185 if (ft2232_device_desc && ft2232_serial)
2186 {
2187 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
2188 ft2232_device_desc = NULL;
2189 }
2190
2191 if (ft2232_device_desc)
2192 {
2193 openex_string = ft2232_device_desc;
2194 openex_flags = FT_OPEN_BY_DESCRIPTION;
2195 }
2196 else if (ft2232_serial)
2197 {
2198 openex_string = ft2232_serial;
2199 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
2200 }
2201 else
2202 {
2203 LOG_ERROR("neither device description nor serial number specified");
2204 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
2205
2206 return ERROR_JTAG_INIT_FAILED;
2207 }
2208
2209 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2210 if (status != FT_OK) {
2211 /* under Win32, the FTD2XX driver appends an "A" to the end
2212 * of the description, if we tried by the desc, then
2213 * try by the alternate "A" description. */
2214 if (openex_string == ft2232_device_desc) {
2215 /* Try the alternate method. */
2216 openex_string = ft2232_device_desc_A;
2217 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
2218 if (status == FT_OK) {
2219 /* yea, the "alternate" method worked! */
2220 } else {
2221 /* drat, give the user a meaningfull message.
2222 * telling the use we tried *BOTH* methods. */
2223 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'",
2224 ft2232_device_desc,
2225 ft2232_device_desc_A);
2226 }
2227 }
2228 }
2229
2230 if (status != FT_OK)
2231 {
2232 DWORD num_devices;
2233
2234 if (more)
2235 {
2236 LOG_WARNING("unable to open ftdi device (trying more): %s",
2237 ftd2xx_status_string(status));
2238 *try_more = 1;
2239 return ERROR_JTAG_INIT_FAILED;
2240 }
2241 LOG_ERROR("unable to open ftdi device: %s",
2242 ftd2xx_status_string(status));
2243 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
2244 if (status == FT_OK)
2245 {
2246 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
2247 uint32_t i;
2248
2249 for (i = 0; i < num_devices; i++)
2250 desc_array[i] = malloc(64);
2251
2252 desc_array[num_devices] = NULL;
2253
2254 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
2255
2256 if (status == FT_OK)
2257 {
2258 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
2259 for (i = 0; i < num_devices; i++)
2260 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
2261 }
2262
2263 for (i = 0; i < num_devices; i++)
2264 free(desc_array[i]);
2265
2266 free(desc_array);
2267 }
2268 else
2269 {
2270 LOG_ERROR("ListDevices: NONE");
2271 }
2272 return ERROR_JTAG_INIT_FAILED;
2273 }
2274
2275 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
2276 {
2277 LOG_ERROR("unable to set latency timer: %s",
2278 ftd2xx_status_string(status));
2279 return ERROR_JTAG_INIT_FAILED;
2280 }
2281
2282 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
2283 {
2284 /* ftd2xx 1.04 (linux) has a bug when calling FT_GetLatencyTimer
2285 * so ignore errors if using this driver version */
2286 DWORD dw_version;
2287
2288 status = FT_GetDriverVersion(ftdih, &dw_version);
2289 LOG_ERROR("unable to get latency timer: %s",
2290 ftd2xx_status_string(status));
2291
2292 if ((status == FT_OK) && (dw_version == 0x10004)) {
2293 LOG_ERROR("ftd2xx 1.04 detected - this has known issues " \
2294 "with FT_GetLatencyTimer, upgrade to a newer version");
2295 }
2296 else {
2297 return ERROR_JTAG_INIT_FAILED;
2298 }
2299 }
2300 else
2301 {
2302 LOG_DEBUG("current latency timer: %i", latency_timer);
2303 }
2304
2305 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
2306 {
2307 LOG_ERROR("unable to set timeouts: %s",
2308 ftd2xx_status_string(status));
2309 return ERROR_JTAG_INIT_FAILED;
2310 }
2311
2312 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
2313 {
2314 LOG_ERROR("unable to enable bit i/o mode: %s",
2315 ftd2xx_status_string(status));
2316 return ERROR_JTAG_INIT_FAILED;
2317 }
2318
2319 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
2320 {
2321 LOG_ERROR("unable to get FT_GetDeviceInfo: %s",
2322 ftd2xx_status_string(status));
2323 return ERROR_JTAG_INIT_FAILED;
2324 }
2325 else
2326 {
2327 static const char* type_str[] =
2328 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
2329 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2330 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
2331 ? ftdi_device : FT_DEVICE_UNKNOWN;
2332 LOG_INFO("device: %" PRIu32 " \"%s\"", (uint32_t)ftdi_device, type_str[type_index]);
2333 LOG_INFO("deviceID: %" PRIu32, (uint32_t)deviceID);
2334 LOG_INFO("SerialNumber: %s", SerialNumber);
2335 LOG_INFO("Description: %s", Description);
2336 }
2337
2338 return ERROR_OK;
2339 }
2340
2341 static int ft2232_purge_ftd2xx(void)
2342 {
2343 FT_STATUS status;
2344
2345 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
2346 {
2347 LOG_ERROR("error purging ftd2xx device: %s",
2348 ftd2xx_status_string(status));
2349 return ERROR_JTAG_INIT_FAILED;
2350 }
2351
2352 return ERROR_OK;
2353 }
2354
2355 #endif /* BUILD_FT2232_FTD2XX == 1 */
2356
2357 #if BUILD_FT2232_LIBFTDI == 1
2358 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more, int channel)
2359 {
2360 uint8_t latency_timer;
2361
2362 if (layout == NULL) {
2363 LOG_WARNING("No ft2232 layout specified'");
2364 return ERROR_JTAG_INIT_FAILED;
2365 }
2366
2367 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2368 layout->name, vid, pid);
2369
2370 if (ftdi_init(&ftdic) < 0)
2371 return ERROR_JTAG_INIT_FAILED;
2372
2373 /* default to INTERFACE_A */
2374 if(channel == INTERFACE_ANY) { channel = INTERFACE_A; }
2375
2376 if (ftdi_set_interface(&ftdic, channel) < 0)
2377 {
2378 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2379 return ERROR_JTAG_INIT_FAILED;
2380 }
2381
2382 /* context, vendor id, product id */
2383 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2384 ft2232_serial) < 0)
2385 {
2386 if (more)
2387 LOG_WARNING("unable to open ftdi device (trying more): %s",
2388 ftdic.error_str);
2389 else
2390 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2391 *try_more = 1;
2392 return ERROR_JTAG_INIT_FAILED;
2393 }
2394
2395 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2396 if (ftdi_usb_reset(&ftdic) < 0)
2397 {
2398 LOG_ERROR("unable to reset ftdi device");
2399 return ERROR_JTAG_INIT_FAILED;
2400 }
2401
2402 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2403 {
2404 LOG_ERROR("unable to set latency timer");
2405 return ERROR_JTAG_INIT_FAILED;
2406 }
2407
2408 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2409 {
2410 LOG_ERROR("unable to get latency timer");
2411 return ERROR_JTAG_INIT_FAILED;
2412 }
2413 else
2414 {
2415 LOG_DEBUG("current latency timer: %i", latency_timer);
2416 }
2417
2418 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2419
2420 ftdi_device = ftdic.type;
2421 static const char* type_str[] =
2422 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2423 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2424 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2425 ? ftdi_device : no_of_known_types;
2426 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2427 return ERROR_OK;
2428 }
2429
2430 static int ft2232_purge_libftdi(void)
2431 {
2432 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2433 {
2434 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2435 return ERROR_JTAG_INIT_FAILED;
2436 }
2437
2438 return ERROR_OK;
2439 }
2440
2441 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2442
2443 static int ft2232_set_data_bits_low_byte( uint8_t value, uint8_t direction )
2444 {
2445 uint8_t buf[3];
2446 uint32_t bytes_written;
2447
2448 buf[0] = 0x80; /* command "set data bits low byte" */
2449 buf[1] = value; /* value */
2450 buf[2] = direction; /* direction */
2451
2452 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2453
2454 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2455 {
2456 LOG_ERROR("couldn't initialize data bits low byte");
2457 return ERROR_JTAG_INIT_FAILED;
2458 }
2459
2460 return ERROR_OK;
2461 }
2462
2463 static int ft2232_set_data_bits_high_byte( uint8_t value, uint8_t direction )
2464 {
2465 uint8_t buf[3];
2466 uint32_t bytes_written;
2467
2468 buf[0] = 0x82; /* command "set data bits high byte" */
2469 buf[1] = value; /* value */
2470 buf[2] = direction; /* direction */
2471
2472 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2473
2474 if (ft2232_write(buf, sizeof(buf), &bytes_written) != ERROR_OK)
2475 {
2476 LOG_ERROR("couldn't initialize data bits high byte");
2477 return ERROR_JTAG_INIT_FAILED;
2478 }
2479
2480 return ERROR_OK;
2481 }
2482
2483 static int ft2232_init(void)
2484 {
2485 uint8_t buf[1];
2486 int retval;
2487 uint32_t bytes_written;
2488
2489 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2490 {
2491 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2492 }
2493 else
2494 {
2495 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2496
2497 }
2498 if (layout == NULL) {
2499 LOG_WARNING("No ft2232 layout specified'");
2500 return ERROR_JTAG_INIT_FAILED;
2501 }
2502
2503 for (int i = 0; 1; i++)
2504 {
2505 /*
2506 * "more indicates that there are more IDs to try, so we should
2507 * not print an error for an ID mismatch (but for anything
2508 * else, we should).
2509 *
2510 * try_more indicates that the error code returned indicates an
2511 * ID mismatch (and nothing else) and that we should proceeed
2512 * with the next ID pair.
2513 */
2514 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2515 int try_more = 0;
2516
2517 #if BUILD_FT2232_FTD2XX == 1
2518 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2519 more, &try_more);
2520 #elif BUILD_FT2232_LIBFTDI == 1
2521 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2522 more, &try_more, layout->channel);
2523 #endif
2524 if (retval >= 0)
2525 break;
2526 if (!more || !try_more)
2527 return retval;
2528 }
2529
2530 ft2232_buffer_size = 0;
2531 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2532
2533 if (layout->init() != ERROR_OK)
2534 return ERROR_JTAG_INIT_FAILED;
2535
2536 if (ft2232_device_is_highspeed())
2537 {
2538 #ifndef BUILD_FT2232_HIGHSPEED
2539 #if BUILD_FT2232_FTD2XX == 1
2540 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2541 #elif BUILD_FT2232_LIBFTDI == 1
2542 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2543 #endif
2544 #endif
2545 /* make sure the legacy mode is disabled */
2546 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2547 return ERROR_JTAG_INIT_FAILED;
2548 }
2549
2550 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2551 if ((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK)
2552 {
2553 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2554 return ERROR_JTAG_INIT_FAILED;
2555 }
2556
2557 #if BUILD_FT2232_FTD2XX == 1
2558 return ft2232_purge_ftd2xx();
2559 #elif BUILD_FT2232_LIBFTDI == 1
2560 return ft2232_purge_libftdi();
2561 #endif
2562
2563 return ERROR_OK;
2564 }
2565
2566 /** Updates defaults for DBUS signals: the four JTAG signals
2567 * (TCK, TDI, TDO, TMS) and * the four GPIOL signals.
2568 */
2569 static inline void ftx232_dbus_init(void)
2570 {
2571 low_output = 0x08;
2572 low_direction = 0x0b;
2573 }
2574
2575 /** Initializes DBUS signals: the four JTAG signals (TCK, TDI, TDO, TMS),
2576 * the four GPIOL signals. Initialization covers value and direction,
2577 * as customized for each layout.
2578 */
2579 static int ftx232_dbus_write(void)
2580 {
2581 enum reset_types jtag_reset_config = jtag_get_reset_config();
2582 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2583 {
2584 low_direction &= ~nTRSTnOE; /* nTRST input */
2585 low_output &= ~nTRST; /* nTRST = 0 */
2586 }
2587 else
2588 {
2589 low_direction |= nTRSTnOE; /* nTRST output */
2590 low_output |= nTRST; /* nTRST = 1 */
2591 }
2592
2593 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2594 {
2595 low_direction |= nSRSTnOE; /* nSRST output */
2596 low_output |= nSRST; /* nSRST = 1 */
2597 }
2598 else
2599 {
2600 low_direction &= ~nSRSTnOE; /* nSRST input */
2601 low_output &= ~nSRST; /* nSRST = 0 */
2602 }
2603
2604 /* initialize low byte for jtag */
2605 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2606 {
2607 LOG_ERROR("couldn't initialize FT2232 DBUS");
2608 return ERROR_JTAG_INIT_FAILED;
2609 }
2610
2611 return ERROR_OK;
2612 }
2613
2614 static int usbjtag_init(void)
2615 {
2616 /*
2617 * NOTE: This is now _specific_ to the "usbjtag" layout.
2618 * Don't try cram any more layouts into this.
2619 */
2620 ftx232_dbus_init();
2621
2622 nTRST = 0x10;
2623 nTRSTnOE = 0x10;
2624 nSRST = 0x40;
2625 nSRSTnOE = 0x40;
2626
2627 return ftx232_dbus_write();
2628 }
2629
2630 static int lm3s811_jtag_init(void)
2631 {
2632 ftx232_dbus_init();
2633
2634 /* There are multiple revisions of LM3S811 eval boards:
2635 * - Rev B (and older?) boards have no SWO trace support.
2636 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2637 * they should use the "luminary_icdi" layout instead.
2638 */
2639 nTRST = 0x0;
2640 nTRSTnOE = 0x00;
2641 nSRST = 0x20;
2642 nSRSTnOE = 0x20;
2643 low_output = 0x88;
2644 low_direction = 0x8b;
2645
2646 return ftx232_dbus_write();
2647 }
2648
2649 static int icdi_jtag_init(void)
2650 {
2651 ftx232_dbus_init();
2652
2653 /* Most Luminary eval boards support SWO trace output,
2654 * and should use this "luminary_icdi" layout.
2655 *
2656 * ADBUS 0..3 are used for JTAG as usual. GPIOs are used
2657 * to switch between JTAG and SWD, or switch the ft2232 UART
2658 * on the second MPSSE channel/interface (BDBUS)
2659 * between (i) the stellaris UART (on Luminary boards)
2660 * or (ii) SWO trace data (generic).
2661 *
2662 * We come up in JTAG mode and may switch to SWD later (with
2663 * SWO/trace option if SWD is active).
2664 *
2665 * DBUS == GPIO-Lx
2666 * CBUS == GPIO-Hx
2667 */
2668
2669
2670 #define ICDI_JTAG_EN (1 << 7) /* ADBUS 7 (a.k.a. DBGMOD) */
2671 #define ICDI_DBG_ENn (1 << 6) /* ADBUS 6 */
2672 #define ICDI_SRST (1 << 5) /* ADBUS 5 */
2673
2674
2675 /* GPIOs on second channel/interface (UART) ... */
2676 #define ICDI_SWO_EN (1 << 4) /* BDBUS 4 */
2677 #define ICDI_TX_SWO (1 << 1) /* BDBUS 1 */
2678 #define ICDI_VCP_RX (1 << 0) /* BDBUS 0 (to stellaris UART) */
2679
2680 nTRST = 0x0;
2681 nTRSTnOE = 0x00;
2682 nSRST = ICDI_SRST;
2683 nSRSTnOE = ICDI_SRST;
2684
2685 low_direction |= ICDI_JTAG_EN | ICDI_DBG_ENn;
2686 low_output |= ICDI_JTAG_EN;
2687 low_output &= ~ICDI_DBG_ENn;
2688
2689 return ftx232_dbus_write();
2690 }
2691
2692 static int signalyzer_init(void)
2693 {
2694 ftx232_dbus_init();
2695
2696 nTRST = 0x10;
2697 nTRSTnOE = 0x10;
2698 nSRST = 0x20;
2699 nSRSTnOE = 0x20;
2700 return ftx232_dbus_write();
2701 }
2702
2703 static int axm0432_jtag_init(void)
2704 {
2705 low_output = 0x08;
2706 low_direction = 0x2b;
2707
2708 /* initialize low byte for jtag */
2709 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2710 {
2711 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2712 return ERROR_JTAG_INIT_FAILED;
2713 }
2714
2715 if (strcmp(layout->name, "axm0432_jtag") == 0)
2716 {
2717 nTRST = 0x08;
2718 nTRSTnOE = 0x0; /* No output enable for TRST*/
2719 nSRST = 0x04;
2720 nSRSTnOE = 0x0; /* No output enable for SRST*/
2721 }
2722 else
2723 {
2724 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2725 exit(-1);
2726 }
2727
2728 high_output = 0x0;
2729 high_direction = 0x0c;
2730
2731 enum reset_types jtag_reset_config = jtag_get_reset_config();
2732 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2733 {
2734 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2735 }
2736 else
2737 {
2738 high_output |= nTRST;
2739 }
2740
2741 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2742 {
2743 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2744 }
2745 else
2746 {
2747 high_output |= nSRST;
2748 }
2749
2750 /* initialize high byte for jtag */
2751 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2752 {
2753 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2754 return ERROR_JTAG_INIT_FAILED;
2755 }
2756
2757 return ERROR_OK;
2758 }
2759
2760 static int redbee_init(void)
2761 {
2762 low_output = 0x08;
2763 low_direction = 0x2b;
2764
2765 /* initialize low byte for jtag */
2766 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2767 {
2768 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2769 return ERROR_JTAG_INIT_FAILED;
2770 }
2771
2772 nTRST = 0x08;
2773 nTRSTnOE = 0x0; /* No output enable for TRST*/
2774 nSRST = 0x04;
2775 nSRSTnOE = 0x0; /* No output enable for SRST*/
2776
2777 high_output = 0x0;
2778 high_direction = 0x0c;
2779
2780 enum reset_types jtag_reset_config = jtag_get_reset_config();
2781 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2782 {
2783 LOG_ERROR("can't set nTRSTOE to push-pull on redbee");
2784 }
2785 else
2786 {
2787 high_output |= nTRST;
2788 }
2789
2790 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2791 {
2792 LOG_ERROR("can't set nSRST to push-pull on redbee");
2793 }
2794 else
2795 {
2796 high_output |= nSRST;
2797 }
2798
2799 /* initialize high byte for jtag */
2800 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2801 {
2802 LOG_ERROR("couldn't initialize FT2232 with 'redbee' layout");
2803 return ERROR_JTAG_INIT_FAILED;
2804 }
2805
2806 return ERROR_OK;
2807 }
2808
2809 static int jtagkey_init(void)
2810 {
2811 low_output = 0x08;
2812 low_direction = 0x1b;
2813
2814 /* initialize low byte for jtag */
2815 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2816 {
2817 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2818 return ERROR_JTAG_INIT_FAILED;
2819 }
2820
2821 if (strcmp(layout->name, "jtagkey") == 0)
2822 {
2823 nTRST = 0x01;
2824 nTRSTnOE = 0x4;
2825 nSRST = 0x02;
2826 nSRSTnOE = 0x08;
2827 }
2828 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2829 || (strcmp(layout->name, "oocdlink") == 0))
2830 {
2831 nTRST = 0x02;
2832 nTRSTnOE = 0x1;
2833 nSRST = 0x08;
2834 nSRSTnOE = 0x04;
2835 }
2836 else
2837 {
2838 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2839 exit(-1);
2840 }
2841
2842 high_output = 0x0;
2843 high_direction = 0x0f;
2844
2845 enum reset_types jtag_reset_config = jtag_get_reset_config();
2846 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2847 {
2848 high_output |= nTRSTnOE;
2849 high_output &= ~nTRST;
2850 }
2851 else
2852 {
2853 high_output &= ~nTRSTnOE;
2854 high_output |= nTRST;
2855 }
2856
2857 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2858 {
2859 high_output &= ~nSRSTnOE;
2860 high_output |= nSRST;
2861 }
2862 else
2863 {
2864 high_output |= nSRSTnOE;
2865 high_output &= ~nSRST;
2866 }
2867
2868 /* initialize high byte for jtag */
2869 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2870 {
2871 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2872 return ERROR_JTAG_INIT_FAILED;
2873 }
2874
2875 return ERROR_OK;
2876 }
2877
2878 static int olimex_jtag_init(void)
2879 {
2880 low_output = 0x08;
2881 low_direction = 0x1b;
2882
2883 /* initialize low byte for jtag */
2884 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2885 {
2886 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2887 return ERROR_JTAG_INIT_FAILED;
2888 }
2889
2890 nTRST = 0x01;
2891 nTRSTnOE = 0x4;
2892 nSRST = 0x02;
2893 nSRSTnOE = 0x00; /* no output enable for nSRST */
2894
2895 high_output = 0x0;
2896 high_direction = 0x0f;
2897
2898 enum reset_types jtag_reset_config = jtag_get_reset_config();
2899 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2900 {
2901 high_output |= nTRSTnOE;
2902 high_output &= ~nTRST;
2903 }
2904 else
2905 {
2906 high_output &= ~nTRSTnOE;
2907 high_output |= nTRST;
2908 }
2909
2910 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2911 {
2912 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2913 }
2914 else
2915 {
2916 high_output &= ~nSRST;
2917 }
2918
2919 /* turn red LED on */
2920 high_output |= 0x08;
2921
2922 /* initialize high byte for jtag */
2923 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2924 {
2925 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2926 return ERROR_JTAG_INIT_FAILED;
2927 }
2928
2929 return ERROR_OK;
2930 }
2931
2932 static int flyswatter_init(int rev)
2933 {
2934 low_output = 0x18;
2935 low_direction = 0x7b;
2936
2937 if ((rev < 0) || (rev > 3)) {
2938 LOG_ERROR("bogus 'flyswatter' revision supplied (%i)", rev);
2939 return ERROR_JTAG_INIT_FAILED;
2940 }
2941
2942 if (rev == 1)
2943 low_direction |= 1 << 7;
2944
2945 /* initialize low byte for jtag */
2946 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2947 {
2948 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2949 return ERROR_JTAG_INIT_FAILED;
2950 }
2951
2952 nTRST = 0x10;
2953 nTRSTnOE = 0x0; /* not output enable for nTRST */
2954 nSRST = 0x20;
2955 nSRSTnOE = 0x00; /* no output enable for nSRST */
2956
2957 high_output = 0x00;
2958
2959 if (rev == 1)
2960 high_direction = 0x0c;
2961 else
2962 high_direction = 0x01;
2963
2964 /* turn red LED3 on, LED2 off */
2965 high_output |= 0x08;
2966
2967 /* initialize high byte for jtag */
2968 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
2969 {
2970 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2971 return ERROR_JTAG_INIT_FAILED;
2972 }
2973
2974 return ERROR_OK;
2975 }
2976
2977 static int flyswatter1_init(void)
2978 {
2979 return flyswatter_init(1);
2980 }
2981
2982 static int flyswatter2_init(void)
2983 {
2984 return flyswatter_init(2);
2985 }
2986
2987 static int minimodule_init(void)
2988 {
2989 low_output = 0x18;//check if srst should be 1 or 0 initially. (0x08) (flyswatter was 0x18)
2990 low_direction = 0xfb;//0xfb;
2991
2992 /* initialize low byte for jtag */
2993 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
2994 {
2995 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
2996 return ERROR_JTAG_INIT_FAILED;
2997 }
2998
2999
3000 nSRST = 0x20;
3001
3002 high_output = 0x00;
3003 high_direction = 0x05;
3004
3005 /* turn red LED3 on, LED2 off */
3006 //high_output |= 0x08;
3007
3008 /* initialize high byte for jtag */
3009 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3010 {
3011 LOG_ERROR("couldn't initialize FT2232 with 'minimodule' layout");
3012 return ERROR_JTAG_INIT_FAILED;
3013 }
3014
3015 return ERROR_OK;
3016 }
3017
3018 static int turtle_init(void)
3019 {
3020 low_output = 0x08;
3021 low_direction = 0x5b;
3022
3023 /* initialize low byte for jtag */
3024 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3025 {
3026 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
3027 return ERROR_JTAG_INIT_FAILED;
3028 }
3029
3030 nSRST = 0x40;
3031
3032 high_output = 0x00;
3033 high_direction = 0x0C;
3034
3035 /* initialize high byte for jtag */
3036 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3037 {
3038 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
3039 return ERROR_JTAG_INIT_FAILED;
3040 }
3041
3042 return ERROR_OK;
3043 }
3044
3045 static int comstick_init(void)
3046 {
3047 low_output = 0x08;
3048 low_direction = 0x0b;
3049
3050 /* initialize low byte for jtag */
3051 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3052 {
3053 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3054 return ERROR_JTAG_INIT_FAILED;
3055 }
3056
3057 nTRST = 0x01;
3058 nTRSTnOE = 0x00; /* no output enable for nTRST */
3059 nSRST = 0x02;
3060 nSRSTnOE = 0x00; /* no output enable for nSRST */
3061
3062 high_output = 0x03;
3063 high_direction = 0x03;
3064
3065 /* initialize high byte for jtag */
3066 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3067 {
3068 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
3069 return ERROR_JTAG_INIT_FAILED;
3070 }
3071
3072 return ERROR_OK;
3073 }
3074
3075 static int stm32stick_init(void)
3076 {
3077 low_output = 0x88;
3078 low_direction = 0x8b;
3079
3080 /* initialize low byte for jtag */
3081 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3082 {
3083 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3084 return ERROR_JTAG_INIT_FAILED;
3085 }
3086
3087 nTRST = 0x01;
3088 nTRSTnOE = 0x00; /* no output enable for nTRST */
3089 nSRST = 0x80;
3090 nSRSTnOE = 0x00; /* no output enable for nSRST */
3091
3092 high_output = 0x01;
3093 high_direction = 0x03;
3094
3095 /* initialize high byte for jtag */
3096 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3097 {
3098 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
3099 return ERROR_JTAG_INIT_FAILED;
3100 }
3101
3102 return ERROR_OK;
3103 }
3104
3105 static int sheevaplug_init(void)
3106 {
3107 low_output = 0x08;
3108 low_direction = 0x1b;
3109
3110 /* initialize low byte for jtag */
3111 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3112 {
3113 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3114 return ERROR_JTAG_INIT_FAILED;
3115 }
3116
3117 nTRSTnOE = 0x1;
3118 nTRST = 0x02;
3119 nSRSTnOE = 0x4;
3120 nSRST = 0x08;
3121
3122 high_output = 0x0;
3123 high_direction = 0x0f;
3124
3125 /* nTRST is always push-pull */
3126 high_output &= ~nTRSTnOE;
3127 high_output |= nTRST;
3128
3129 /* nSRST is always open-drain */
3130 high_output |= nSRSTnOE;
3131 high_output &= ~nSRST;
3132
3133 /* initialize high byte for jtag */
3134 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3135 {
3136 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
3137 return ERROR_JTAG_INIT_FAILED;
3138 }
3139
3140 return ERROR_OK;
3141 }
3142
3143 static int cortino_jtag_init(void)
3144 {
3145 low_output = 0x08;
3146 low_direction = 0x1b;
3147
3148 /* initialize low byte for jtag */
3149 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3150 {
3151 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3152 return ERROR_JTAG_INIT_FAILED;
3153 }
3154
3155 nTRST = 0x01;
3156 nTRSTnOE = 0x00; /* no output enable for nTRST */
3157 nSRST = 0x02;
3158 nSRSTnOE = 0x00; /* no output enable for nSRST */
3159
3160 high_output = 0x03;
3161 high_direction = 0x03;
3162
3163 /* initialize high byte for jtag */
3164 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3165 {
3166 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
3167 return ERROR_JTAG_INIT_FAILED;
3168 }
3169
3170 return ERROR_OK;
3171 }
3172
3173 static int lisa_l_init(void)
3174 {
3175 ftx232_dbus_init();
3176
3177 nTRST = 0x10;
3178 nTRSTnOE = 0x10;
3179 nSRST = 0x40;
3180 nSRSTnOE = 0x40;
3181
3182 high_output = 0x00;
3183 high_direction = 0x18;
3184
3185 /* initialize high byte for jtag */
3186 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3187 {
3188 LOG_ERROR("couldn't initialize FT2232 with 'lisa_l' layout");
3189 return ERROR_JTAG_INIT_FAILED;
3190 }
3191
3192 return ftx232_dbus_write();
3193 }
3194
3195 static int flossjtag_init(void)
3196 {
3197 ftx232_dbus_init();
3198
3199 nTRST = 0x10;
3200 nTRSTnOE = 0x10;
3201 nSRST = 0x40;
3202 nSRSTnOE = 0x40;
3203
3204 high_output = 0x00;
3205 high_direction = 0x18;
3206
3207 /* initialize high byte for jtag */
3208 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3209 {
3210 LOG_ERROR("couldn't initialize FT2232 with 'Floss-JTAG' layout");
3211 return ERROR_JTAG_INIT_FAILED;
3212 }
3213
3214 return ftx232_dbus_write();
3215 }
3216
3217 /*
3218 * The reference schematic from TI for the XDS100v2 has a CPLD on which opens
3219 * the door for a number of different configurations
3220 *
3221 * Known Implementations:
3222 * http://processors.wiki.ti.com/images/9/93/TMS570LS20216_USB_STICK_Schematic.pdf
3223 *
3224 * http://processors.wiki.ti.com/index.php/XDS100 (rev2)
3225 * * CLPD logic: Rising edge to enable outputs (XDS100_PWR_RST)
3226 * * ACBUS3 to transition 0->1 (OE rising edge)
3227 * * CPLD logic: Put the EMU0/1 pins in Hi-Z:
3228 * * ADBUS5/GPIOL1 = EMU_EN = 1
3229 * * ADBUS6/GPIOL2 = EMU0 = 0
3230 * * ACBUS4/SPARE0 = EMU1 = 0
3231 * * CPLD logic: Disable loopback
3232 * * ACBUS6/SPARE2 = LOOPBACK = 0
3233 */
3234 #define XDS100_nEMU_EN (1<<5)
3235 #define XDS100_nEMU0 (1<<6)
3236
3237 #define XDS100_PWR_RST (1<<3)
3238 #define XDS100_nEMU1 (1<<4)
3239 #define XDS100_LOOPBACK (1<<6)
3240 static int xds100v2_init(void)
3241 {
3242 /* These are in the lower byte */
3243 nTRST = 0x10;
3244 nTRSTnOE = 0x10;
3245
3246 /* These aren't actually used on 14 pin connectors */
3247 /* These are in the upper byte */
3248 nSRST = 0x01;
3249 nSRSTnOE = 0x01;
3250
3251 low_output = 0x08 | nTRST | XDS100_nEMU_EN;
3252 low_direction = 0x0b | nTRSTnOE | XDS100_nEMU_EN | XDS100_nEMU0;
3253
3254 if (ft2232_set_data_bits_low_byte(low_output,low_direction) != ERROR_OK)
3255 {
3256 LOG_ERROR("couldn't initialize FT2232 with 'xds100v2' layout");
3257 return ERROR_JTAG_INIT_FAILED;
3258 }
3259
3260 high_output = 0;
3261 high_direction = nSRSTnOE | XDS100_LOOPBACK | XDS100_PWR_RST | XDS100_nEMU1;
3262
3263 /* initialize high byte for jtag */
3264 if (ft2232_set_data_bits_high_byte(high_output,high_direction) != ERROR_OK)
3265 {