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

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)