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

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)