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

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)