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

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)