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