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

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)