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

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)