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

Linking to existing account procedure

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

SSH host keys fingerprints

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