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

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)