Fix Luminary FT2232 layout docs/configs
[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 /* This code uses information contained in the MPSSE specification which was
31 * found here:
32 * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
33 * Hereafter this is called the "MPSSE Spec".
34 *
35 * The datasheet for the ftdichip.com's FT2232D part is here:
36 * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 /* project specific includes */
44 #include <jtag/interface.h>
45 #include <jtag/commands.h>
46 #include <helper/time_support.h>
47
48 #if IS_CYGWIN == 1
49 #include <windows.h>
50 #endif
51
52 #include <assert.h>
53
54 #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
55 #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
56 #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
57 #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
58 #endif
59
60 /* FT2232 access library includes */
61 #if BUILD_FT2232_FTD2XX == 1
62 #include <ftd2xx.h>
63 #elif BUILD_FT2232_LIBFTDI == 1
64 #include <ftdi.h>
65 #endif
66
67 /* max TCK for the high speed devices 30000 kHz */
68 #define FTDI_2232H_4232H_MAX_TCK 30000
69 /* max TCK for the full speed devices 6000 kHz */
70 #define FTDI_2232C_MAX_TCK 6000
71 /* this speed value tells that RTCK is requested */
72 #define RTCK_SPEED -1
73
74 /*
75 * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
76 * errors with a retry count of 100. Increasing it solves the problem for me.
77 * - Dimitar
78 *
79 * FIXME There's likely an issue with the usb_read_timeout from libftdi.
80 * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
81 * to something sane.
82 */
83 #define LIBFTDI_READ_RETRY_COUNT 2000
84
85 #ifndef BUILD_FT2232_HIGHSPEED
86 #if BUILD_FT2232_FTD2XX == 1
87 enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
88 #elif BUILD_FT2232_LIBFTDI == 1
89 enum { TYPE_2232H = 4, TYPE_4232H = 5 };
90 #endif
91 #endif
92
93 /**
94 * Send out \a num_cycles on the TCK line while the TAP(s) are in a
95 * stable state. Calling code must ensure that current state is stable,
96 * that verification is not done in here.
97 *
98 * @param num_cycles The number of clocks cycles to send.
99 * @param cmd The command to send.
100 *
101 * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
102 */
103 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
104
105 static char * ft2232_device_desc_A = NULL;
106 static char* ft2232_device_desc = NULL;
107 static char* ft2232_serial = NULL;
108 static char* ft2232_layout = NULL;
109 static uint8_t ft2232_latency = 2;
110 static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
111
112 #define MAX_USB_IDS 8
113 /* vid = pid = 0 marks the end of the list */
114 static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
115 static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
116
117 struct ft2232_layout {
118 char* name;
119 int (*init)(void);
120 void (*reset)(int trst, int srst);
121 void (*blink)(void);
122 };
123
124 /* init procedures for supported layouts */
125 static int usbjtag_init(void);
126 static int jtagkey_init(void);
127 static int olimex_jtag_init(void);
128 static int flyswatter_init(void);
129 static int turtle_init(void);
130 static int comstick_init(void);
131 static int stm32stick_init(void);
132 static int axm0432_jtag_init(void);
133 static int sheevaplug_init(void);
134 static int icebear_jtag_init(void);
135 static int cortino_jtag_init(void);
136 static int signalyzer_h_init(void);
137 static int ktlink_init(void);
138
139 /* reset procedures for supported layouts */
140 static void usbjtag_reset(int trst, int srst);
141 static void jtagkey_reset(int trst, int srst);
142 static void olimex_jtag_reset(int trst, int srst);
143 static void flyswatter_reset(int trst, int srst);
144 static void turtle_reset(int trst, int srst);
145 static void comstick_reset(int trst, int srst);
146 static void stm32stick_reset(int trst, int srst);
147 static void axm0432_jtag_reset(int trst, int srst);
148 static void sheevaplug_reset(int trst, int srst);
149 static void icebear_jtag_reset(int trst, int srst);
150 static void signalyzer_h_reset(int trst, int srst);
151 static void ktlink_reset(int trst, int srst);
152
153 /* blink procedures for layouts that support a blinking led */
154 static void olimex_jtag_blink(void);
155 static void flyswatter_jtag_blink(void);
156 static void turtle_jtag_blink(void);
157 static void signalyzer_h_blink(void);
158 static void ktlink_blink(void);
159
160 static const struct ft2232_layout ft2232_layouts[] =
161 {
162 { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
163 { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
164 { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
165 { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
166 { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
167 { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
168 { "luminary_icdi", usbjtag_init, usbjtag_reset, NULL },
169 { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
170 { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
171 { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
172 { "comstick", comstick_init, comstick_reset, NULL },
173 { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
174 { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
175 { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
176 { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
177 { "cortino", cortino_jtag_init, comstick_reset, NULL },
178 { "signalyzer-h", signalyzer_h_init, signalyzer_h_reset, signalyzer_h_blink },
179 { "ktlink", ktlink_init, ktlink_reset, ktlink_blink },
180 { NULL, NULL, NULL, NULL },
181 };
182
183 static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
184
185 static const struct ft2232_layout *layout;
186 static uint8_t low_output = 0x0;
187 static uint8_t low_direction = 0x0;
188 static uint8_t high_output = 0x0;
189 static uint8_t high_direction = 0x0;
190
191 #if BUILD_FT2232_FTD2XX == 1
192 static FT_HANDLE ftdih = NULL;
193 static FT_DEVICE ftdi_device = 0;
194 #elif BUILD_FT2232_LIBFTDI == 1
195 static struct ftdi_context ftdic;
196 static enum ftdi_chip_type ftdi_device;
197 #endif
198
199 static struct jtag_command* first_unsent; /* next command that has to be sent */
200 static int require_send;
201
202 /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
203
204 "There is a significant difference between libftdi and libftd2xx. The latter
205 one allows to schedule up to 64*64 bytes of result data while libftdi fails
206 with more than 4*64. As a consequence, the FT2232 driver is forced to
207 perform around 16x more USB transactions for long command streams with TDO
208 capture when running with libftdi."
209
210 No idea how we get
211 #define FT2232_BUFFER_SIZE 131072
212 a comment would have been nice.
213 */
214
215 #define FT2232_BUFFER_SIZE 131072
216
217 static uint8_t* ft2232_buffer = NULL;
218 static int ft2232_buffer_size = 0;
219 static int ft2232_read_pointer = 0;
220 static int ft2232_expect_read = 0;
221
222 /**
223 * Function buffer_write
224 * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
225 * @param val is the byte to send.
226 */
227 static inline void buffer_write(uint8_t val)
228 {
229 assert(ft2232_buffer);
230 assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
231 ft2232_buffer[ft2232_buffer_size++] = val;
232 }
233
234 /**
235 * Function buffer_read
236 * returns a byte from the byte buffer.
237 */
238 static inline uint8_t buffer_read(void)
239 {
240 assert(ft2232_buffer);
241 assert(ft2232_read_pointer < ft2232_buffer_size);
242 return ft2232_buffer[ft2232_read_pointer++];
243 }
244
245 /**
246 * Clocks out \a bit_count bits on the TMS line, starting with the least
247 * significant bit of tms_bits and progressing to more significant bits.
248 * Rigorous state transition logging is done here via tap_set_state().
249 *
250 * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
251 * 0x4b or 0x6b. See the MPSSE spec referenced above for their
252 * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
253 * is often used for this, 0x4b.
254 *
255 * @param tms_bits Holds the sequence of bits to send.
256 * @param tms_count Tells how many bits in the sequence.
257 * @param tdi_bit A single bit to pass on to TDI before the first TCK
258 * cycle and held static for the duration of TMS clocking.
259 *
260 * See the MPSSE spec referenced above.
261 */
262 static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
263 {
264 uint8_t tms_byte;
265 int i;
266 int tms_ndx; /* bit index into tms_byte */
267
268 assert(tms_count > 0);
269
270 DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
271 mpsse_cmd, tms_bits, tms_count);
272
273 for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
274 {
275 bool bit = tms_bits & 1;
276
277 if (bit)
278 tms_byte |= (1 << tms_ndx);
279
280 /* always do state transitions in public view */
281 tap_set_state(tap_state_transition(tap_get_state(), bit));
282
283 /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
284 also increment.
285 */
286 ++tms_ndx;
287
288 if (tms_ndx == 7 || i == tms_count-1)
289 {
290 buffer_write(mpsse_cmd);
291 buffer_write(tms_ndx - 1);
292
293 /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
294 TMS/CS and is held static for the duration of TMS/CS clocking.
295 */
296 buffer_write(tms_byte | (tdi_bit << 7));
297 }
298 }
299 }
300
301 /**
302 * Function get_tms_buffer_requirements
303 * returns what clock_tms() will consume if called with
304 * same \a bit_count.
305 */
306 static inline int get_tms_buffer_requirements(int bit_count)
307 {
308 return ((bit_count + 6)/7) * 3;
309 }
310
311 /**
312 * Function move_to_state
313 * moves the TAP controller from the current state to a
314 * \a goal_state through a path given by tap_get_tms_path(). State transition
315 * logging is performed by delegation to clock_tms().
316 *
317 * @param goal_state is the destination state for the move.
318 */
319 static void move_to_state(tap_state_t goal_state)
320 {
321 tap_state_t start_state = tap_get_state();
322
323 /* goal_state is 1/2 of a tuple/pair of states which allow convenient
324 lookup of the required TMS pattern to move to this state from the
325 start state.
326 */
327
328 /* do the 2 lookups */
329 int tms_bits = tap_get_tms_path(start_state, goal_state);
330 int tms_count = tap_get_tms_path_len(start_state, goal_state);
331
332 DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
333
334 clock_tms(0x4b, tms_bits, tms_count, 0);
335 }
336
337 static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
338 {
339 #if BUILD_FT2232_FTD2XX == 1
340 FT_STATUS status;
341 DWORD dw_bytes_written;
342 if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
343 {
344 *bytes_written = dw_bytes_written;
345 LOG_ERROR("FT_Write returned: %lu", status);
346 return ERROR_JTAG_DEVICE_ERROR;
347 }
348 else
349 {
350 *bytes_written = dw_bytes_written;
351 return ERROR_OK;
352 }
353 #elif BUILD_FT2232_LIBFTDI == 1
354 int retval;
355 if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
356 {
357 *bytes_written = 0;
358 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
359 return ERROR_JTAG_DEVICE_ERROR;
360 }
361 else
362 {
363 *bytes_written = retval;
364 return ERROR_OK;
365 }
366 #endif
367 }
368
369 static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
370 {
371 #if BUILD_FT2232_FTD2XX == 1
372 DWORD dw_bytes_read;
373 FT_STATUS status;
374 int timeout = 5;
375 *bytes_read = 0;
376
377 while ((*bytes_read < size) && timeout--)
378 {
379 if ((status = FT_Read(ftdih, buf + *bytes_read, size -
380 *bytes_read, &dw_bytes_read)) != FT_OK)
381 {
382 *bytes_read = 0;
383 LOG_ERROR("FT_Read returned: %lu", status);
384 return ERROR_JTAG_DEVICE_ERROR;
385 }
386 *bytes_read += dw_bytes_read;
387 }
388
389 #elif BUILD_FT2232_LIBFTDI == 1
390 int retval;
391 int timeout = LIBFTDI_READ_RETRY_COUNT;
392 *bytes_read = 0;
393
394 while ((*bytes_read < size) && timeout--)
395 {
396 if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
397 {
398 *bytes_read = 0;
399 LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
400 return ERROR_JTAG_DEVICE_ERROR;
401 }
402 *bytes_read += retval;
403 }
404
405 #endif
406
407 if (*bytes_read < size)
408 {
409 LOG_ERROR("couldn't read enough bytes from "
410 "FT2232 device (%i < %i)",
411 (unsigned)*bytes_read,
412 (unsigned)size);
413 return ERROR_JTAG_DEVICE_ERROR;
414 }
415
416 return ERROR_OK;
417 }
418
419 static bool ft2232_device_is_highspeed(void)
420 {
421 #if BUILD_FT2232_FTD2XX == 1
422 return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
423 #elif BUILD_FT2232_LIBFTDI == 1
424 return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
425 #endif
426 }
427
428 /*
429 * Commands that only apply to the FT2232H and FT4232H devices.
430 * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
431 * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
432 */
433
434 static int ft2232h_ft4232h_adaptive_clocking(bool enable)
435 {
436 uint8_t buf = enable ? 0x96 : 0x97;
437 LOG_DEBUG("%2.2x", buf);
438
439 uint32_t bytes_written;
440 int retval = ft2232_write(&buf, 1, &bytes_written);
441 if ((ERROR_OK != retval) || (bytes_written != 1))
442 {
443 LOG_ERROR("couldn't write command to %s adaptive clocking"
444 , enable ? "enable" : "disable");
445 return retval;
446 }
447
448 return ERROR_OK;
449 }
450
451 /**
452 * Enable/disable the clk divide by 5 of the 60MHz master clock.
453 * This result in a JTAG clock speed range of 91.553Hz-6MHz
454 * respective 457.763Hz-30MHz.
455 */
456 static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
457 {
458 uint32_t bytes_written;
459 uint8_t buf = enable ? 0x8b : 0x8a;
460 int retval = ft2232_write(&buf, 1, &bytes_written);
461 if ((ERROR_OK != retval) || (bytes_written != 1))
462 {
463 LOG_ERROR("couldn't write command to %s clk divide by 5"
464 , enable ? "enable" : "disable");
465 return ERROR_JTAG_INIT_FAILED;
466 }
467 ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
468 LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
469
470 return ERROR_OK;
471 }
472
473 static int ft2232_speed(int speed)
474 {
475 uint8_t buf[3];
476 int retval;
477 uint32_t bytes_written;
478
479 retval = ERROR_OK;
480 bool enable_adaptive_clocking = (RTCK_SPEED == speed);
481 if (ft2232_device_is_highspeed())
482 retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
483 else if (enable_adaptive_clocking)
484 {
485 LOG_ERROR("ft2232 device %lu does not support RTCK"
486 , (long unsigned int)ftdi_device);
487 return ERROR_FAIL;
488 }
489
490 if ((enable_adaptive_clocking) || (ERROR_OK != retval))
491 return retval;
492
493 buf[0] = 0x86; /* command "set divisor" */
494 buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
495 buf[2] = (speed >> 8) & 0xff; /* valueH */
496
497 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
498 if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
499 {
500 LOG_ERROR("couldn't set FT2232 TCK speed");
501 return retval;
502 }
503
504 return ERROR_OK;
505 }
506
507 static int ft2232_speed_div(int speed, int* khz)
508 {
509 /* Take a look in the FT2232 manual,
510 * AN2232C-01 Command Processor for
511 * MPSSE and MCU Host Bus. Chapter 3.8 */
512
513 *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
514
515 return ERROR_OK;
516 }
517
518 static int ft2232_khz(int khz, int* jtag_speed)
519 {
520 if (khz == 0)
521 {
522 if (ft2232_device_is_highspeed())
523 {
524 *jtag_speed = RTCK_SPEED;
525 return ERROR_OK;
526 }
527 else
528 {
529 LOG_DEBUG("RCLK not supported");
530 return ERROR_FAIL;
531 }
532 }
533
534 /* Take a look in the FT2232 manual,
535 * AN2232C-01 Command Processor for
536 * MPSSE and MCU Host Bus. Chapter 3.8
537 *
538 * We will calc here with a multiplier
539 * of 10 for better rounding later. */
540
541 /* Calc speed, (ft2232_max_tck / khz) - 1 */
542 /* Use 65000 for better rounding */
543 *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
544
545 /* Add 0.9 for rounding */
546 *jtag_speed += 9;
547
548 /* Calc real speed */
549 *jtag_speed = *jtag_speed / 10;
550
551 /* Check if speed is greater than 0 */
552 if (*jtag_speed < 0)
553 {
554 *jtag_speed = 0;
555 }
556
557 /* Check max value */
558 if (*jtag_speed > 0xFFFF)
559 {
560 *jtag_speed = 0xFFFF;
561 }
562
563 return ERROR_OK;
564 }
565
566 static void ft2232_end_state(tap_state_t state)
567 {
568 if (tap_is_state_stable(state))
569 tap_set_end_state(state);
570 else
571 {
572 LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
573 exit(-1);
574 }
575 }
576
577 static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
578 {
579 int num_bytes = (scan_size + 7) / 8;
580 int bits_left = scan_size;
581 int cur_byte = 0;
582
583 while (num_bytes-- > 1)
584 {
585 buffer[cur_byte++] = buffer_read();
586 bits_left -= 8;
587 }
588
589 buffer[cur_byte] = 0x0;
590
591 /* There is one more partial byte left from the clock data in/out instructions */
592 if (bits_left > 1)
593 {
594 buffer[cur_byte] = buffer_read() >> 1;
595 }
596 /* 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 */
597 buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
598 }
599
600 static void ft2232_debug_dump_buffer(void)
601 {
602 int i;
603 char line[256];
604 char* line_p = line;
605
606 for (i = 0; i < ft2232_buffer_size; i++)
607 {
608 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
609 if (i % 16 == 15)
610 {
611 LOG_DEBUG("%s", line);
612 line_p = line;
613 }
614 }
615
616 if (line_p != line)
617 LOG_DEBUG("%s", line);
618 }
619
620 static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
621 {
622 struct jtag_command* cmd;
623 uint8_t* buffer;
624 int scan_size;
625 enum scan_type type;
626 int retval;
627 uint32_t bytes_written = 0;
628 uint32_t bytes_read = 0;
629
630 #ifdef _DEBUG_USB_IO_
631 struct timeval start, inter, inter2, end;
632 struct timeval d_inter, d_inter2, d_end;
633 #endif
634
635 #ifdef _DEBUG_USB_COMMS_
636 LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
637 ft2232_debug_dump_buffer();
638 #endif
639
640 #ifdef _DEBUG_USB_IO_
641 gettimeofday(&start, NULL);
642 #endif
643
644 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
645 {
646 LOG_ERROR("couldn't write MPSSE commands to FT2232");
647 return retval;
648 }
649
650 #ifdef _DEBUG_USB_IO_
651 gettimeofday(&inter, NULL);
652 #endif
653
654 if (ft2232_expect_read)
655 {
656 /* FIXME this "timeout" is never changed ... */
657 int timeout = LIBFTDI_READ_RETRY_COUNT;
658 ft2232_buffer_size = 0;
659
660 #ifdef _DEBUG_USB_IO_
661 gettimeofday(&inter2, NULL);
662 #endif
663
664 if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
665 {
666 LOG_ERROR("couldn't read from FT2232");
667 return retval;
668 }
669
670 #ifdef _DEBUG_USB_IO_
671 gettimeofday(&end, NULL);
672
673 timeval_subtract(&d_inter, &inter, &start);
674 timeval_subtract(&d_inter2, &inter2, &start);
675 timeval_subtract(&d_end, &end, &start);
676
677 LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
678 (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
679 (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
680 (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
681 #endif
682
683 ft2232_buffer_size = bytes_read;
684
685 if (ft2232_expect_read != ft2232_buffer_size)
686 {
687 LOG_ERROR("ft2232_expect_read (%i) != "
688 "ft2232_buffer_size (%i) "
689 "(%i retries)",
690 ft2232_expect_read,
691 ft2232_buffer_size,
692 LIBFTDI_READ_RETRY_COUNT - timeout);
693 ft2232_debug_dump_buffer();
694
695 exit(-1);
696 }
697
698 #ifdef _DEBUG_USB_COMMS_
699 LOG_DEBUG("read buffer (%i retries): %i bytes",
700 LIBFTDI_READ_RETRY_COUNT - timeout,
701 ft2232_buffer_size);
702 ft2232_debug_dump_buffer();
703 #endif
704 }
705
706 ft2232_expect_read = 0;
707 ft2232_read_pointer = 0;
708
709 /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
710 * that wasn't handled by a caller-provided error handler
711 */
712 retval = ERROR_OK;
713
714 cmd = first;
715 while (cmd != last)
716 {
717 switch (cmd->type)
718 {
719 case JTAG_SCAN:
720 type = jtag_scan_type(cmd->cmd.scan);
721 if (type != SCAN_OUT)
722 {
723 scan_size = jtag_scan_size(cmd->cmd.scan);
724 buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
725 ft2232_read_scan(type, buffer, scan_size);
726 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
727 retval = ERROR_JTAG_QUEUE_FAILED;
728 free(buffer);
729 }
730 break;
731
732 default:
733 break;
734 }
735
736 cmd = cmd->next;
737 }
738
739 ft2232_buffer_size = 0;
740
741 return retval;
742 }
743
744 /**
745 * Function ft2232_add_pathmove
746 * moves the TAP controller from the current state to a new state through the
747 * given path, where path is an array of tap_state_t's.
748 *
749 * @param path is an array of tap_stat_t which gives the states to traverse through
750 * ending with the last state at path[num_states-1]
751 * @param num_states is the count of state steps to move through
752 */
753 static void ft2232_add_pathmove(tap_state_t* path, int num_states)
754 {
755 int state_count = 0;
756
757 assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
758
759 DEBUG_JTAG_IO("-");
760
761 /* this loop verifies that the path is legal and logs each state in the path */
762 while (num_states)
763 {
764 unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
765 int bit_count = 0;
766 int num_states_batch = num_states > 7 ? 7 : num_states;
767
768 /* command "Clock Data to TMS/CS Pin (no Read)" */
769 buffer_write(0x4b);
770
771 /* number of states remaining */
772 buffer_write(num_states_batch - 1);
773
774 while (num_states_batch--) {
775 /* either TMS=0 or TMS=1 must work ... */
776 if (tap_state_transition(tap_get_state(), false)
777 == path[state_count])
778 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
779 else if (tap_state_transition(tap_get_state(), true)
780 == path[state_count])
781 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
782
783 /* ... or else the caller goofed BADLY */
784 else {
785 LOG_ERROR("BUG: %s -> %s isn't a valid "
786 "TAP state transition",
787 tap_state_name(tap_get_state()),
788 tap_state_name(path[state_count]));
789 exit(-1);
790 }
791
792 tap_set_state(path[state_count]);
793 state_count++;
794 num_states--;
795 }
796
797 buffer_write(tms_byte);
798 }
799 tap_set_end_state(tap_get_state());
800 }
801
802 static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
803 {
804 int num_bytes = (scan_size + 7) / 8;
805 int bits_left = scan_size;
806 int cur_byte = 0;
807 int last_bit;
808
809 if (!ir_scan)
810 {
811 if (tap_get_state() != TAP_DRSHIFT)
812 {
813 move_to_state(TAP_DRSHIFT);
814 }
815 }
816 else
817 {
818 if (tap_get_state() != TAP_IRSHIFT)
819 {
820 move_to_state(TAP_IRSHIFT);
821 }
822 }
823
824 /* add command for complete bytes */
825 while (num_bytes > 1)
826 {
827 int thisrun_bytes;
828 if (type == SCAN_IO)
829 {
830 /* Clock Data Bytes In and Out LSB First */
831 buffer_write(0x39);
832 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
833 }
834 else if (type == SCAN_OUT)
835 {
836 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
837 buffer_write(0x19);
838 /* LOG_DEBUG("added TDI bytes (o)"); */
839 }
840 else if (type == SCAN_IN)
841 {
842 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
843 buffer_write(0x28);
844 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
845 }
846
847 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
848 num_bytes -= thisrun_bytes;
849
850 buffer_write((uint8_t) (thisrun_bytes - 1));
851 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
852
853 if (type != SCAN_IN)
854 {
855 /* add complete bytes */
856 while (thisrun_bytes-- > 0)
857 {
858 buffer_write(buffer[cur_byte++]);
859 bits_left -= 8;
860 }
861 }
862 else /* (type == SCAN_IN) */
863 {
864 bits_left -= 8 * (thisrun_bytes);
865 }
866 }
867
868 /* the most signifcant bit is scanned during TAP movement */
869 if (type != SCAN_IN)
870 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
871 else
872 last_bit = 0;
873
874 /* process remaining bits but the last one */
875 if (bits_left > 1)
876 {
877 if (type == SCAN_IO)
878 {
879 /* Clock Data Bits In and Out LSB First */
880 buffer_write(0x3b);
881 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
882 }
883 else if (type == SCAN_OUT)
884 {
885 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
886 buffer_write(0x1b);
887 /* LOG_DEBUG("added TDI bits (o)"); */
888 }
889 else if (type == SCAN_IN)
890 {
891 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
892 buffer_write(0x2a);
893 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
894 }
895
896 buffer_write(bits_left - 2);
897 if (type != SCAN_IN)
898 buffer_write(buffer[cur_byte]);
899 }
900
901 if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
902 || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
903 {
904 if (type == SCAN_IO)
905 {
906 /* Clock Data Bits In and Out LSB First */
907 buffer_write(0x3b);
908 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
909 }
910 else if (type == SCAN_OUT)
911 {
912 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
913 buffer_write(0x1b);
914 /* LOG_DEBUG("added TDI bits (o)"); */
915 }
916 else if (type == SCAN_IN)
917 {
918 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
919 buffer_write(0x2a);
920 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
921 }
922 buffer_write(0x0);
923 buffer_write(last_bit);
924 }
925 else
926 {
927 int tms_bits;
928 int tms_count;
929 uint8_t mpsse_cmd;
930
931 /* move from Shift-IR/DR to end state */
932 if (type != SCAN_OUT)
933 {
934 /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
935 /* This must be coordinated with the bit shifts in ft2232_read_scan */
936 tms_bits = 0x01;
937 tms_count = 2;
938 /* Clock Data to TMS/CS Pin with Read */
939 mpsse_cmd = 0x6b;
940 }
941 else
942 {
943 tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
944 tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
945 /* Clock Data to TMS/CS Pin (no Read) */
946 mpsse_cmd = 0x4b;
947 }
948
949 DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
950 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
951 }
952
953 if (tap_get_state() != tap_get_end_state())
954 {
955 move_to_state(tap_get_end_state());
956 }
957 }
958
959 static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
960 {
961 int num_bytes = (scan_size + 7) / 8;
962 int bits_left = scan_size;
963 int cur_byte = 0;
964 int last_bit;
965 uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
966 uint8_t* receive_pointer = receive_buffer;
967 uint32_t bytes_written;
968 uint32_t bytes_read;
969 int retval;
970 int thisrun_read = 0;
971
972 if (cmd->ir_scan)
973 {
974 LOG_ERROR("BUG: large IR scans are not supported");
975 exit(-1);
976 }
977
978 if (tap_get_state() != TAP_DRSHIFT)
979 {
980 move_to_state(TAP_DRSHIFT);
981 }
982
983 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
984 {
985 LOG_ERROR("couldn't write MPSSE commands to FT2232");
986 exit(-1);
987 }
988 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
989 ft2232_buffer_size, (int)bytes_written);
990 ft2232_buffer_size = 0;
991
992 /* add command for complete bytes */
993 while (num_bytes > 1)
994 {
995 int thisrun_bytes;
996
997 if (type == SCAN_IO)
998 {
999 /* Clock Data Bytes In and Out LSB First */
1000 buffer_write(0x39);
1001 /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
1002 }
1003 else if (type == SCAN_OUT)
1004 {
1005 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
1006 buffer_write(0x19);
1007 /* LOG_DEBUG("added TDI bytes (o)"); */
1008 }
1009 else if (type == SCAN_IN)
1010 {
1011 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
1012 buffer_write(0x28);
1013 /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
1014 }
1015
1016 thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
1017 thisrun_read = thisrun_bytes;
1018 num_bytes -= thisrun_bytes;
1019 buffer_write((uint8_t) (thisrun_bytes - 1));
1020 buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
1021
1022 if (type != SCAN_IN)
1023 {
1024 /* add complete bytes */
1025 while (thisrun_bytes-- > 0)
1026 {
1027 buffer_write(buffer[cur_byte]);
1028 cur_byte++;
1029 bits_left -= 8;
1030 }
1031 }
1032 else /* (type == SCAN_IN) */
1033 {
1034 bits_left -= 8 * (thisrun_bytes);
1035 }
1036
1037 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1038 {
1039 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1040 exit(-1);
1041 }
1042 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1043 ft2232_buffer_size,
1044 (int)bytes_written);
1045 ft2232_buffer_size = 0;
1046
1047 if (type != SCAN_OUT)
1048 {
1049 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1050 {
1051 LOG_ERROR("couldn't read from FT2232");
1052 exit(-1);
1053 }
1054 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1055 thisrun_read,
1056 (int)bytes_read);
1057 receive_pointer += bytes_read;
1058 }
1059 }
1060
1061 thisrun_read = 0;
1062
1063 /* the most signifcant bit is scanned during TAP movement */
1064 if (type != SCAN_IN)
1065 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
1066 else
1067 last_bit = 0;
1068
1069 /* process remaining bits but the last one */
1070 if (bits_left > 1)
1071 {
1072 if (type == SCAN_IO)
1073 {
1074 /* Clock Data Bits In and Out LSB First */
1075 buffer_write(0x3b);
1076 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1077 }
1078 else if (type == SCAN_OUT)
1079 {
1080 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1081 buffer_write(0x1b);
1082 /* LOG_DEBUG("added TDI bits (o)"); */
1083 }
1084 else if (type == SCAN_IN)
1085 {
1086 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1087 buffer_write(0x2a);
1088 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1089 }
1090 buffer_write(bits_left - 2);
1091 if (type != SCAN_IN)
1092 buffer_write(buffer[cur_byte]);
1093
1094 if (type != SCAN_OUT)
1095 thisrun_read += 2;
1096 }
1097
1098 if (tap_get_end_state() == TAP_DRSHIFT)
1099 {
1100 if (type == SCAN_IO)
1101 {
1102 /* Clock Data Bits In and Out LSB First */
1103 buffer_write(0x3b);
1104 /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
1105 }
1106 else if (type == SCAN_OUT)
1107 {
1108 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
1109 buffer_write(0x1b);
1110 /* LOG_DEBUG("added TDI bits (o)"); */
1111 }
1112 else if (type == SCAN_IN)
1113 {
1114 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
1115 buffer_write(0x2a);
1116 /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
1117 }
1118 buffer_write(0x0);
1119 buffer_write(last_bit);
1120 }
1121 else
1122 {
1123 int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1124 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1125 uint8_t mpsse_cmd;
1126
1127 /* move from Shift-IR/DR to end state */
1128 if (type != SCAN_OUT)
1129 {
1130 /* Clock Data to TMS/CS Pin with Read */
1131 mpsse_cmd = 0x6b;
1132 /* LOG_DEBUG("added TMS scan (read)"); */
1133 }
1134 else
1135 {
1136 /* Clock Data to TMS/CS Pin (no Read) */
1137 mpsse_cmd = 0x4b;
1138 /* LOG_DEBUG("added TMS scan (no read)"); */
1139 }
1140
1141 DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
1142 clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
1143 }
1144
1145 if (type != SCAN_OUT)
1146 thisrun_read += 1;
1147
1148 if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
1149 {
1150 LOG_ERROR("couldn't write MPSSE commands to FT2232");
1151 exit(-1);
1152 }
1153 LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
1154 ft2232_buffer_size,
1155 (int)bytes_written);
1156 ft2232_buffer_size = 0;
1157
1158 if (type != SCAN_OUT)
1159 {
1160 if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
1161 {
1162 LOG_ERROR("couldn't read from FT2232");
1163 exit(-1);
1164 }
1165 LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
1166 thisrun_read,
1167 (int)bytes_read);
1168 receive_pointer += bytes_read;
1169 }
1170
1171 return ERROR_OK;
1172 }
1173
1174 static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
1175 {
1176 int predicted_size = 3;
1177 int num_bytes = (scan_size - 1) / 8;
1178
1179 if (tap_get_state() != TAP_DRSHIFT)
1180 predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
1181
1182 if (type == SCAN_IN) /* only from device to host */
1183 {
1184 /* complete bytes */
1185 predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
1186
1187 /* remaining bits - 1 (up to 7) */
1188 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
1189 }
1190 else /* host to device, or bidirectional */
1191 {
1192 /* complete bytes */
1193 predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
1194
1195 /* remaining bits -1 (up to 7) */
1196 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
1197 }
1198
1199 return predicted_size;
1200 }
1201
1202 static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
1203 {
1204 int predicted_size = 0;
1205
1206 if (type != SCAN_OUT)
1207 {
1208 /* complete bytes */
1209 predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
1210
1211 /* remaining bits - 1 */
1212 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
1213
1214 /* last bit (from TMS scan) */
1215 predicted_size += 1;
1216 }
1217
1218 /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
1219
1220 return predicted_size;
1221 }
1222
1223 static void usbjtag_reset(int trst, int srst)
1224 {
1225 enum reset_types jtag_reset_config = jtag_get_reset_config();
1226 if (trst == 1)
1227 {
1228 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1229 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
1230 else
1231 low_output &= ~nTRST; /* switch output low */
1232 }
1233 else if (trst == 0)
1234 {
1235 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1236 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
1237 else
1238 low_output |= nTRST; /* switch output high */
1239 }
1240
1241 if (srst == 1)
1242 {
1243 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1244 low_output &= ~nSRST; /* switch output low */
1245 else
1246 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
1247 }
1248 else if (srst == 0)
1249 {
1250 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1251 low_output |= nSRST; /* switch output high */
1252 else
1253 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
1254 }
1255
1256 /* command "set data bits low byte" */
1257 buffer_write(0x80);
1258 buffer_write(low_output);
1259 buffer_write(low_direction);
1260 }
1261
1262 static void jtagkey_reset(int trst, int srst)
1263 {
1264 enum reset_types jtag_reset_config = jtag_get_reset_config();
1265 if (trst == 1)
1266 {
1267 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1268 high_output &= ~nTRSTnOE;
1269 else
1270 high_output &= ~nTRST;
1271 }
1272 else if (trst == 0)
1273 {
1274 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1275 high_output |= nTRSTnOE;
1276 else
1277 high_output |= nTRST;
1278 }
1279
1280 if (srst == 1)
1281 {
1282 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1283 high_output &= ~nSRST;
1284 else
1285 high_output &= ~nSRSTnOE;
1286 }
1287 else if (srst == 0)
1288 {
1289 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1290 high_output |= nSRST;
1291 else
1292 high_output |= nSRSTnOE;
1293 }
1294
1295 /* command "set data bits high byte" */
1296 buffer_write(0x82);
1297 buffer_write(high_output);
1298 buffer_write(high_direction);
1299 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1300 high_direction);
1301 }
1302
1303 static void olimex_jtag_reset(int trst, int srst)
1304 {
1305 enum reset_types jtag_reset_config = jtag_get_reset_config();
1306 if (trst == 1)
1307 {
1308 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1309 high_output &= ~nTRSTnOE;
1310 else
1311 high_output &= ~nTRST;
1312 }
1313 else if (trst == 0)
1314 {
1315 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1316 high_output |= nTRSTnOE;
1317 else
1318 high_output |= nTRST;
1319 }
1320
1321 if (srst == 1)
1322 {
1323 high_output |= nSRST;
1324 }
1325 else if (srst == 0)
1326 {
1327 high_output &= ~nSRST;
1328 }
1329
1330 /* command "set data bits high byte" */
1331 buffer_write(0x82);
1332 buffer_write(high_output);
1333 buffer_write(high_direction);
1334 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1335 high_direction);
1336 }
1337
1338 static void axm0432_jtag_reset(int trst, int srst)
1339 {
1340 if (trst == 1)
1341 {
1342 tap_set_state(TAP_RESET);
1343 high_output &= ~nTRST;
1344 }
1345 else if (trst == 0)
1346 {
1347 high_output |= nTRST;
1348 }
1349
1350 if (srst == 1)
1351 {
1352 high_output &= ~nSRST;
1353 }
1354 else if (srst == 0)
1355 {
1356 high_output |= nSRST;
1357 }
1358
1359 /* command "set data bits low byte" */
1360 buffer_write(0x82);
1361 buffer_write(high_output);
1362 buffer_write(high_direction);
1363 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1364 high_direction);
1365 }
1366
1367 static void flyswatter_reset(int trst, int srst)
1368 {
1369 if (trst == 1)
1370 {
1371 low_output &= ~nTRST;
1372 }
1373 else if (trst == 0)
1374 {
1375 low_output |= nTRST;
1376 }
1377
1378 if (srst == 1)
1379 {
1380 low_output |= nSRST;
1381 }
1382 else if (srst == 0)
1383 {
1384 low_output &= ~nSRST;
1385 }
1386
1387 /* command "set data bits low byte" */
1388 buffer_write(0x80);
1389 buffer_write(low_output);
1390 buffer_write(low_direction);
1391 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
1392 }
1393
1394 static void turtle_reset(int trst, int srst)
1395 {
1396 trst = trst;
1397
1398 if (srst == 1)
1399 {
1400 low_output |= nSRST;
1401 }
1402 else if (srst == 0)
1403 {
1404 low_output &= ~nSRST;
1405 }
1406
1407 /* command "set data bits low byte" */
1408 buffer_write(0x80);
1409 buffer_write(low_output);
1410 buffer_write(low_direction);
1411 LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
1412 }
1413
1414 static void comstick_reset(int trst, int srst)
1415 {
1416 if (trst == 1)
1417 {
1418 high_output &= ~nTRST;
1419 }
1420 else if (trst == 0)
1421 {
1422 high_output |= nTRST;
1423 }
1424
1425 if (srst == 1)
1426 {
1427 high_output &= ~nSRST;
1428 }
1429 else if (srst == 0)
1430 {
1431 high_output |= nSRST;
1432 }
1433
1434 /* command "set data bits high byte" */
1435 buffer_write(0x82);
1436 buffer_write(high_output);
1437 buffer_write(high_direction);
1438 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1439 high_direction);
1440 }
1441
1442 static void stm32stick_reset(int trst, int srst)
1443 {
1444 if (trst == 1)
1445 {
1446 high_output &= ~nTRST;
1447 }
1448 else if (trst == 0)
1449 {
1450 high_output |= nTRST;
1451 }
1452
1453 if (srst == 1)
1454 {
1455 low_output &= ~nSRST;
1456 }
1457 else if (srst == 0)
1458 {
1459 low_output |= nSRST;
1460 }
1461
1462 /* command "set data bits low byte" */
1463 buffer_write(0x80);
1464 buffer_write(low_output);
1465 buffer_write(low_direction);
1466
1467 /* command "set data bits high byte" */
1468 buffer_write(0x82);
1469 buffer_write(high_output);
1470 buffer_write(high_direction);
1471 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
1472 high_direction);
1473 }
1474
1475 static void sheevaplug_reset(int trst, int srst)
1476 {
1477 if (trst == 1)
1478 high_output &= ~nTRST;
1479 else if (trst == 0)
1480 high_output |= nTRST;
1481
1482 if (srst == 1)
1483 high_output &= ~nSRSTnOE;
1484 else if (srst == 0)
1485 high_output |= nSRSTnOE;
1486
1487 /* command "set data bits high byte" */
1488 buffer_write(0x82);
1489 buffer_write(high_output);
1490 buffer_write(high_direction);
1491 LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
1492 }
1493
1494 static int ft2232_execute_runtest(struct jtag_command *cmd)
1495 {
1496 int retval;
1497 int i;
1498 int predicted_size = 0;
1499 retval = ERROR_OK;
1500
1501 DEBUG_JTAG_IO("runtest %i cycles, end in %s",
1502 cmd->cmd.runtest->num_cycles,
1503 tap_state_name(cmd->cmd.runtest->end_state));
1504
1505 /* only send the maximum buffer size that FT2232C can handle */
1506 predicted_size = 0;
1507 if (tap_get_state() != TAP_IDLE)
1508 predicted_size += 3;
1509 predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
1510 if (cmd->cmd.runtest->end_state != TAP_IDLE)
1511 predicted_size += 3;
1512 if (tap_get_end_state() != TAP_IDLE)
1513 predicted_size += 3;
1514 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1515 {
1516 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1517 retval = ERROR_JTAG_QUEUE_FAILED;
1518 require_send = 0;
1519 first_unsent = cmd;
1520 }
1521 if (tap_get_state() != TAP_IDLE)
1522 {
1523 move_to_state(TAP_IDLE);
1524 require_send = 1;
1525 }
1526 i = cmd->cmd.runtest->num_cycles;
1527 while (i > 0)
1528 {
1529 /* there are no state transitions in this code, so omit state tracking */
1530
1531 /* command "Clock Data to TMS/CS Pin (no Read)" */
1532 buffer_write(0x4b);
1533
1534 /* scan 7 bits */
1535 buffer_write((i > 7) ? 6 : (i - 1));
1536
1537 /* TMS data bits */
1538 buffer_write(0x0);
1539
1540 i -= (i > 7) ? 7 : i;
1541 /* LOG_DEBUG("added TMS scan (no read)"); */
1542 }
1543
1544 ft2232_end_state(cmd->cmd.runtest->end_state);
1545
1546 if (tap_get_state() != tap_get_end_state())
1547 {
1548 move_to_state(tap_get_end_state());
1549 }
1550
1551 require_send = 1;
1552 DEBUG_JTAG_IO("runtest: %i, end in %s",
1553 cmd->cmd.runtest->num_cycles,
1554 tap_state_name(tap_get_end_state()));
1555 return retval;
1556 }
1557
1558 static int ft2232_execute_statemove(struct jtag_command *cmd)
1559 {
1560 int predicted_size = 0;
1561 int retval = ERROR_OK;
1562
1563 DEBUG_JTAG_IO("statemove end in %s",
1564 tap_state_name(cmd->cmd.statemove->end_state));
1565
1566 /* only send the maximum buffer size that FT2232C can handle */
1567 predicted_size = 3;
1568 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1569 {
1570 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1571 retval = ERROR_JTAG_QUEUE_FAILED;
1572 require_send = 0;
1573 first_unsent = cmd;
1574 }
1575 ft2232_end_state(cmd->cmd.statemove->end_state);
1576
1577 /* For TAP_RESET, ignore the current recorded state. It's often
1578 * wrong at server startup, and this transation is critical whenever
1579 * it's requested.
1580 */
1581 if (tap_get_end_state() == TAP_RESET) {
1582 clock_tms(0x4b, 0xff, 5, 0);
1583 require_send = 1;
1584
1585 /* shortest-path move to desired end state */
1586 } else if (tap_get_state() != tap_get_end_state())
1587 {
1588 move_to_state(tap_get_end_state());
1589 require_send = 1;
1590 }
1591
1592 return retval;
1593 }
1594
1595 static int ft2232_execute_pathmove(struct jtag_command *cmd)
1596 {
1597 int predicted_size = 0;
1598 int retval = ERROR_OK;
1599
1600 tap_state_t* path = cmd->cmd.pathmove->path;
1601 int num_states = cmd->cmd.pathmove->num_states;
1602
1603 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1604 tap_state_name(tap_get_state()),
1605 tap_state_name(path[num_states-1]));
1606
1607 /* only send the maximum buffer size that FT2232C can handle */
1608 predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
1609 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1610 {
1611 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1612 retval = ERROR_JTAG_QUEUE_FAILED;
1613
1614 require_send = 0;
1615 first_unsent = cmd;
1616 }
1617
1618 ft2232_add_pathmove(path, num_states);
1619 require_send = 1;
1620
1621 return retval;
1622 }
1623
1624 static int ft2232_execute_scan(struct jtag_command *cmd)
1625 {
1626 uint8_t* buffer;
1627 int scan_size; /* size of IR or DR scan */
1628 int predicted_size = 0;
1629 int retval = ERROR_OK;
1630
1631 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1632
1633 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1634
1635 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1636
1637 predicted_size = ft2232_predict_scan_out(scan_size, type);
1638 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1639 {
1640 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1641 /* unsent commands before this */
1642 if (first_unsent != cmd)
1643 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1644 retval = ERROR_JTAG_QUEUE_FAILED;
1645
1646 /* current command */
1647 ft2232_end_state(cmd->cmd.scan->end_state);
1648 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1649 require_send = 0;
1650 first_unsent = cmd->next;
1651 if (buffer)
1652 free(buffer);
1653 return retval;
1654 }
1655 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1656 {
1657 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1658 first_unsent,
1659 cmd);
1660 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1661 retval = ERROR_JTAG_QUEUE_FAILED;
1662 require_send = 0;
1663 first_unsent = cmd;
1664 }
1665 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1666 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1667 ft2232_end_state(cmd->cmd.scan->end_state);
1668 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1669 require_send = 1;
1670 if (buffer)
1671 free(buffer);
1672 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1673 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1674 tap_state_name(tap_get_end_state()));
1675 return retval;
1676
1677 }
1678
1679 static int ft2232_execute_reset(struct jtag_command *cmd)
1680 {
1681 int retval;
1682 int predicted_size = 0;
1683 retval = ERROR_OK;
1684
1685 DEBUG_JTAG_IO("reset trst: %i srst %i",
1686 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1687
1688 /* only send the maximum buffer size that FT2232C can handle */
1689 predicted_size = 3;
1690 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1691 {
1692 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1693 retval = ERROR_JTAG_QUEUE_FAILED;
1694 require_send = 0;
1695 first_unsent = cmd;
1696 }
1697
1698 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1699 {
1700 tap_set_state(TAP_RESET);
1701 }
1702
1703 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1704 require_send = 1;
1705
1706 DEBUG_JTAG_IO("trst: %i, srst: %i",
1707 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1708 return retval;
1709 }
1710
1711 static int ft2232_execute_sleep(struct jtag_command *cmd)
1712 {
1713 int retval;
1714 retval = ERROR_OK;
1715
1716 DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
1717
1718 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1719 retval = ERROR_JTAG_QUEUE_FAILED;
1720 first_unsent = cmd->next;
1721 jtag_sleep(cmd->cmd.sleep->us);
1722 DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
1723 cmd->cmd.sleep->us,
1724 tap_state_name(tap_get_state()));
1725 return retval;
1726 }
1727
1728 static int ft2232_execute_stableclocks(struct jtag_command *cmd)
1729 {
1730 int retval;
1731 retval = ERROR_OK;
1732
1733 /* this is only allowed while in a stable state. A check for a stable
1734 * state was done in jtag_add_clocks()
1735 */
1736 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1737 retval = ERROR_JTAG_QUEUE_FAILED;
1738 DEBUG_JTAG_IO("clocks %i while in %s",
1739 cmd->cmd.stableclocks->num_cycles,
1740 tap_state_name(tap_get_state()));
1741 return retval;
1742 }
1743
1744 static int ft2232_execute_command(struct jtag_command *cmd)
1745 {
1746 int retval;
1747 retval = ERROR_OK;
1748
1749 switch (cmd->type)
1750 {
1751 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1752 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1753 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1754 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1755 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1756 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1757 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1758 default:
1759 LOG_ERROR("BUG: unknown JTAG command type encountered");
1760 exit(-1);
1761 }
1762 return retval;
1763 }
1764
1765 static int ft2232_execute_queue(void)
1766 {
1767 struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
1768 int retval;
1769
1770 first_unsent = cmd; /* next command that has to be sent */
1771 require_send = 0;
1772
1773 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1774 * that wasn't handled by a caller-provided error handler
1775 */
1776 retval = ERROR_OK;
1777
1778 ft2232_buffer_size = 0;
1779 ft2232_expect_read = 0;
1780
1781 /* blink, if the current layout has that feature */
1782 if (layout->blink)
1783 layout->blink();
1784
1785 while (cmd)
1786 {
1787 if (ft2232_execute_command(cmd) != ERROR_OK)
1788 retval = ERROR_JTAG_QUEUE_FAILED;
1789 /* Start reading input before FT2232 TX buffer fills up */
1790 cmd = cmd->next;
1791 if (ft2232_expect_read > 256)
1792 {
1793 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1794 retval = ERROR_JTAG_QUEUE_FAILED;
1795 first_unsent = cmd;
1796 }
1797 }
1798
1799 if (require_send > 0)
1800 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1801 retval = ERROR_JTAG_QUEUE_FAILED;
1802
1803 return retval;
1804 }
1805
1806 #if BUILD_FT2232_FTD2XX == 1
1807 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1808 {
1809 FT_STATUS status;
1810 DWORD deviceID;
1811 char SerialNumber[16];
1812 char Description[64];
1813 DWORD openex_flags = 0;
1814 char* openex_string = NULL;
1815 uint8_t latency_timer;
1816
1817 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1818
1819 #if IS_WIN32 == 0
1820 /* Add non-standard Vid/Pid to the linux driver */
1821 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1822 {
1823 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1824 }
1825 #endif
1826
1827 if (ft2232_device_desc && ft2232_serial)
1828 {
1829 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1830 ft2232_device_desc = NULL;
1831 }
1832
1833 if (ft2232_device_desc)
1834 {
1835 openex_string = ft2232_device_desc;
1836 openex_flags = FT_OPEN_BY_DESCRIPTION;
1837 }
1838 else if (ft2232_serial)
1839 {
1840 openex_string = ft2232_serial;
1841 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1842 }
1843 else
1844 {
1845 LOG_ERROR("neither device description nor serial number specified");
1846 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1847
1848 return ERROR_JTAG_INIT_FAILED;
1849 }
1850
1851 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1852 if (status != FT_OK) {
1853 /* under Win32, the FTD2XX driver appends an "A" to the end
1854 * of the description, if we tried by the desc, then
1855 * try by the alternate "A" description. */
1856 if (openex_string == ft2232_device_desc) {
1857 /* Try the alternate method. */
1858 openex_string = ft2232_device_desc_A;
1859 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1860 if (status == FT_OK) {
1861 /* yea, the "alternate" method worked! */
1862 } else {
1863 /* drat, give the user a meaningfull message.
1864 * telling the use we tried *BOTH* methods. */
1865 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1866 ft2232_device_desc,
1867 ft2232_device_desc_A);
1868 }
1869 }
1870 }
1871
1872 if (status != FT_OK)
1873 {
1874 DWORD num_devices;
1875
1876 if (more)
1877 {
1878 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1879 *try_more = 1;
1880 return ERROR_JTAG_INIT_FAILED;
1881 }
1882 LOG_ERROR("unable to open ftdi device: %lu", status);
1883 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1884 if (status == FT_OK)
1885 {
1886 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1887 uint32_t i;
1888
1889 for (i = 0; i < num_devices; i++)
1890 desc_array[i] = malloc(64);
1891
1892 desc_array[num_devices] = NULL;
1893
1894 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1895
1896 if (status == FT_OK)
1897 {
1898 LOG_ERROR("ListDevices: %lu\n", num_devices);
1899 for (i = 0; i < num_devices; i++)
1900 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
1901 }
1902
1903 for (i = 0; i < num_devices; i++)
1904 free(desc_array[i]);
1905
1906 free(desc_array);
1907 }
1908 else
1909 {
1910 LOG_ERROR("ListDevices: NONE\n");
1911 }
1912 return ERROR_JTAG_INIT_FAILED;
1913 }
1914
1915 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1916 {
1917 LOG_ERROR("unable to set latency timer: %lu", status);
1918 return ERROR_JTAG_INIT_FAILED;
1919 }
1920
1921 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1922 {
1923 LOG_ERROR("unable to get latency timer: %lu", status);
1924 return ERROR_JTAG_INIT_FAILED;
1925 }
1926 else
1927 {
1928 LOG_DEBUG("current latency timer: %i", latency_timer);
1929 }
1930
1931 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1932 {
1933 LOG_ERROR("unable to set timeouts: %lu", status);
1934 return ERROR_JTAG_INIT_FAILED;
1935 }
1936
1937 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1938 {
1939 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1940 return ERROR_JTAG_INIT_FAILED;
1941 }
1942
1943 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
1944 {
1945 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
1946 return ERROR_JTAG_INIT_FAILED;
1947 }
1948 else
1949 {
1950 static const char* type_str[] =
1951 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1952 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
1953 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
1954 ? ftdi_device : FT_DEVICE_UNKNOWN;
1955 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
1956 LOG_INFO("deviceID: %lu", deviceID);
1957 LOG_INFO("SerialNumber: %s", SerialNumber);
1958 LOG_INFO("Description: %s", Description);
1959 }
1960
1961 return ERROR_OK;
1962 }
1963
1964 static int ft2232_purge_ftd2xx(void)
1965 {
1966 FT_STATUS status;
1967
1968 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1969 {
1970 LOG_ERROR("error purging ftd2xx device: %lu", status);
1971 return ERROR_JTAG_INIT_FAILED;
1972 }
1973
1974 return ERROR_OK;
1975 }
1976
1977 #endif /* BUILD_FT2232_FTD2XX == 1 */
1978
1979 #if BUILD_FT2232_LIBFTDI == 1
1980 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
1981 {
1982 uint8_t latency_timer;
1983
1984 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
1985 ft2232_layout, vid, pid);
1986
1987 if (ftdi_init(&ftdic) < 0)
1988 return ERROR_JTAG_INIT_FAILED;
1989
1990 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
1991 {
1992 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
1993 return ERROR_JTAG_INIT_FAILED;
1994 }
1995
1996 /* context, vendor id, product id */
1997 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
1998 ft2232_serial) < 0)
1999 {
2000 if (more)
2001 LOG_WARNING("unable to open ftdi device (trying more): %s",
2002 ftdic.error_str);
2003 else
2004 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2005 *try_more = 1;
2006 return ERROR_JTAG_INIT_FAILED;
2007 }
2008
2009 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2010 if (ftdi_usb_reset(&ftdic) < 0)
2011 {
2012 LOG_ERROR("unable to reset ftdi device");
2013 return ERROR_JTAG_INIT_FAILED;
2014 }
2015
2016 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2017 {
2018 LOG_ERROR("unable to set latency timer");
2019 return ERROR_JTAG_INIT_FAILED;
2020 }
2021
2022 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2023 {
2024 LOG_ERROR("unable to get latency timer");
2025 return ERROR_JTAG_INIT_FAILED;
2026 }
2027 else
2028 {
2029 LOG_DEBUG("current latency timer: %i", latency_timer);
2030 }
2031
2032 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2033
2034 ftdi_device = ftdic.type;
2035 static const char* type_str[] =
2036 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2037 unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
2038 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2039 ? ftdi_device : no_of_known_types;
2040 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2041 return ERROR_OK;
2042 }
2043
2044 static int ft2232_purge_libftdi(void)
2045 {
2046 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2047 {
2048 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2049 return ERROR_JTAG_INIT_FAILED;
2050 }
2051
2052 return ERROR_OK;
2053 }
2054
2055 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2056
2057 static int ft2232_init(void)
2058 {
2059 uint8_t buf[1];
2060 int retval;
2061 uint32_t bytes_written;
2062 const struct ft2232_layout* cur_layout = ft2232_layouts;
2063 int i;
2064
2065 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2066 {
2067 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2068 }
2069 else
2070 {
2071 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2072
2073 }
2074 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2075 {
2076 ft2232_layout = "usbjtag";
2077 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2078 }
2079
2080 while (cur_layout->name)
2081 {
2082 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2083 {
2084 layout = cur_layout;
2085 break;
2086 }
2087 cur_layout++;
2088 }
2089
2090 if (!layout)
2091 {
2092 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2093 return ERROR_JTAG_INIT_FAILED;
2094 }
2095
2096 for (i = 0; 1; i++)
2097 {
2098 /*
2099 * "more indicates that there are more IDs to try, so we should
2100 * not print an error for an ID mismatch (but for anything
2101 * else, we should).
2102 *
2103 * try_more indicates that the error code returned indicates an
2104 * ID mismatch (and nothing else) and that we should proceeed
2105 * with the next ID pair.
2106 */
2107 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2108 int try_more = 0;
2109
2110 #if BUILD_FT2232_FTD2XX == 1
2111 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2112 more, &try_more);
2113 #elif BUILD_FT2232_LIBFTDI == 1
2114 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2115 more, &try_more);
2116 #endif
2117 if (retval >= 0)
2118 break;
2119 if (!more || !try_more)
2120 return retval;
2121 }
2122
2123 ft2232_buffer_size = 0;
2124 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2125
2126 if (layout->init() != ERROR_OK)
2127 return ERROR_JTAG_INIT_FAILED;
2128
2129 if (ft2232_device_is_highspeed())
2130 {
2131 #ifndef BUILD_FT2232_HIGHSPEED
2132 #if BUILD_FT2232_FTD2XX == 1
2133 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2134 #elif BUILD_FT2232_LIBFTDI == 1
2135 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2136 #endif
2137 #endif
2138 /* make sure the legacy mode is disabled */
2139 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2140 return ERROR_JTAG_INIT_FAILED;
2141 }
2142
2143 ft2232_speed(jtag_get_speed());
2144
2145 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2146 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2147 {
2148 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2149 return ERROR_JTAG_INIT_FAILED;
2150 }
2151
2152 #if BUILD_FT2232_FTD2XX == 1
2153 return ft2232_purge_ftd2xx();
2154 #elif BUILD_FT2232_LIBFTDI == 1
2155 return ft2232_purge_libftdi();
2156 #endif
2157
2158 return ERROR_OK;
2159 }
2160
2161 static int usbjtag_init(void)
2162 {
2163 uint8_t buf[3];
2164 uint32_t bytes_written;
2165
2166 low_output = 0x08;
2167 low_direction = 0x0b;
2168
2169 if (strcmp(ft2232_layout, "usbjtag") == 0)
2170 {
2171 nTRST = 0x10;
2172 nTRSTnOE = 0x10;
2173 nSRST = 0x40;
2174 nSRSTnOE = 0x40;
2175 }
2176 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2177 {
2178 nTRST = 0x10;
2179 nTRSTnOE = 0x10;
2180 nSRST = 0x20;
2181 nSRSTnOE = 0x20;
2182 }
2183 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2184 {
2185 /* There are multiple revisions of LM3S811 eval boards:
2186 * - Rev B (and older?) boards have no SWO trace support.
2187 * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
2188 * they should use the "luminary_icdi" layout instead.
2189 */
2190 nTRST = 0x0;
2191 nTRSTnOE = 0x00;
2192 nSRST = 0x20;
2193 nSRSTnOE = 0x20;
2194 low_output = 0x88;
2195 low_direction = 0x8b;
2196 }
2197 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2198 {
2199 /* Most Luminary eval boards support SWO trace output,
2200 * and should use this "luminary_icdi" layout.
2201 */
2202 nTRST = 0x0;
2203 nTRSTnOE = 0x00;
2204 nSRST = 0x20;
2205 nSRSTnOE = 0x20;
2206 low_output = 0x88;
2207 low_direction = 0xcb;
2208 }
2209 else
2210 {
2211 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2212 return ERROR_JTAG_INIT_FAILED;
2213 }
2214
2215 enum reset_types jtag_reset_config = jtag_get_reset_config();
2216 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2217 {
2218 low_direction &= ~nTRSTnOE; /* nTRST input */
2219 low_output &= ~nTRST; /* nTRST = 0 */
2220 }
2221 else
2222 {
2223 low_direction |= nTRSTnOE; /* nTRST output */
2224 low_output |= nTRST; /* nTRST = 1 */
2225 }
2226
2227 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2228 {
2229 low_direction |= nSRSTnOE; /* nSRST output */
2230 low_output |= nSRST; /* nSRST = 1 */
2231 }
2232 else
2233 {
2234 low_direction &= ~nSRSTnOE; /* nSRST input */
2235 low_output &= ~nSRST; /* nSRST = 0 */
2236 }
2237
2238 /* initialize low byte for jtag */
2239 buf[0] = 0x80; /* command "set data bits low byte" */
2240 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2241 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2242 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2243
2244 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2245 {
2246 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2247 return ERROR_JTAG_INIT_FAILED;
2248 }
2249
2250 return ERROR_OK;
2251 }
2252
2253 static int axm0432_jtag_init(void)
2254 {
2255 uint8_t buf[3];
2256 uint32_t bytes_written;
2257
2258 low_output = 0x08;
2259 low_direction = 0x2b;
2260
2261 /* initialize low byte for jtag */
2262 buf[0] = 0x80; /* command "set data bits low byte" */
2263 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2264 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2265 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2266
2267 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2268 {
2269 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2270 return ERROR_JTAG_INIT_FAILED;
2271 }
2272
2273 if (strcmp(layout->name, "axm0432_jtag") == 0)
2274 {
2275 nTRST = 0x08;
2276 nTRSTnOE = 0x0; /* No output enable for TRST*/
2277 nSRST = 0x04;
2278 nSRSTnOE = 0x0; /* No output enable for SRST*/
2279 }
2280 else
2281 {
2282 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2283 exit(-1);
2284 }
2285
2286 high_output = 0x0;
2287 high_direction = 0x0c;
2288
2289 enum reset_types jtag_reset_config = jtag_get_reset_config();
2290 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2291 {
2292 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2293 }
2294 else
2295 {
2296 high_output |= nTRST;
2297 }
2298
2299 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2300 {
2301 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2302 }
2303 else
2304 {
2305 high_output |= nSRST;
2306 }
2307
2308 /* initialize high port */
2309 buf[0] = 0x82; /* command "set data bits high byte" */
2310 buf[1] = high_output; /* value */
2311 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2312 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2313
2314 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2315 {
2316 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2317 return ERROR_JTAG_INIT_FAILED;
2318 }
2319
2320 return ERROR_OK;
2321 }
2322
2323 static int jtagkey_init(void)
2324 {
2325 uint8_t buf[3];
2326 uint32_t bytes_written;
2327
2328 low_output = 0x08;
2329 low_direction = 0x1b;
2330
2331 /* initialize low byte for jtag */
2332 buf[0] = 0x80; /* command "set data bits low byte" */
2333 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2334 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2335 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2336
2337 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2338 {
2339 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2340 return ERROR_JTAG_INIT_FAILED;
2341 }
2342
2343 if (strcmp(layout->name, "jtagkey") == 0)
2344 {
2345 nTRST = 0x01;
2346 nTRSTnOE = 0x4;
2347 nSRST = 0x02;
2348 nSRSTnOE = 0x08;
2349 }
2350 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2351 || (strcmp(layout->name, "oocdlink") == 0))
2352 {
2353 nTRST = 0x02;
2354 nTRSTnOE = 0x1;
2355 nSRST = 0x08;
2356 nSRSTnOE = 0x04;
2357 }
2358 else
2359 {
2360 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2361 exit(-1);
2362 }
2363
2364 high_output = 0x0;
2365 high_direction = 0x0f;
2366
2367 enum reset_types jtag_reset_config = jtag_get_reset_config();
2368 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2369 {
2370 high_output |= nTRSTnOE;
2371 high_output &= ~nTRST;
2372 }
2373 else
2374 {
2375 high_output &= ~nTRSTnOE;
2376 high_output |= nTRST;
2377 }
2378
2379 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2380 {
2381 high_output &= ~nSRSTnOE;
2382 high_output |= nSRST;
2383 }
2384 else
2385 {
2386 high_output |= nSRSTnOE;
2387 high_output &= ~nSRST;
2388 }
2389
2390 /* initialize high port */
2391 buf[0] = 0x82; /* command "set data bits high byte" */
2392 buf[1] = high_output; /* value */
2393 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2394 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2395
2396 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2397 {
2398 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2399 return ERROR_JTAG_INIT_FAILED;
2400 }
2401
2402 return ERROR_OK;
2403 }
2404
2405 static int olimex_jtag_init(void)
2406 {
2407 uint8_t buf[3];
2408 uint32_t bytes_written;
2409
2410 low_output = 0x08;
2411 low_direction = 0x1b;
2412
2413 /* initialize low byte for jtag */
2414 buf[0] = 0x80; /* command "set data bits low byte" */
2415 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2416 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2417 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2418
2419 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2420 {
2421 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2422 return ERROR_JTAG_INIT_FAILED;
2423 }
2424
2425 nTRST = 0x01;
2426 nTRSTnOE = 0x4;
2427 nSRST = 0x02;
2428 nSRSTnOE = 0x00; /* no output enable for nSRST */
2429
2430 high_output = 0x0;
2431 high_direction = 0x0f;
2432
2433 enum reset_types jtag_reset_config = jtag_get_reset_config();
2434 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2435 {
2436 high_output |= nTRSTnOE;
2437 high_output &= ~nTRST;
2438 }
2439 else
2440 {
2441 high_output &= ~nTRSTnOE;
2442 high_output |= nTRST;
2443 }
2444
2445 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2446 {
2447 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2448 }
2449 else
2450 {
2451 high_output &= ~nSRST;
2452 }
2453
2454 /* turn red LED on */
2455 high_output |= 0x08;
2456
2457 /* initialize high port */
2458 buf[0] = 0x82; /* command "set data bits high byte" */
2459 buf[1] = high_output; /* value */
2460 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2461 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2462
2463 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2464 {
2465 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2466 return ERROR_JTAG_INIT_FAILED;
2467 }
2468
2469 return ERROR_OK;
2470 }
2471
2472 static int flyswatter_init(void)
2473 {
2474 uint8_t buf[3];
2475 uint32_t bytes_written;
2476
2477 low_output = 0x18;
2478 low_direction = 0xfb;
2479
2480 /* initialize low byte for jtag */
2481 buf[0] = 0x80; /* command "set data bits low byte" */
2482 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2483 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2484 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2485
2486 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2487 {
2488 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2489 return ERROR_JTAG_INIT_FAILED;
2490 }
2491
2492 nTRST = 0x10;
2493 nTRSTnOE = 0x0; /* not output enable for nTRST */
2494 nSRST = 0x20;
2495 nSRSTnOE = 0x00; /* no output enable for nSRST */
2496
2497 high_output = 0x00;
2498 high_direction = 0x0c;
2499
2500 /* turn red LED3 on, LED2 off */
2501 high_output |= 0x08;
2502
2503 /* initialize high port */
2504 buf[0] = 0x82; /* command "set data bits high byte" */
2505 buf[1] = high_output; /* value */
2506 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2507 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2508
2509 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2510 {
2511 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2512 return ERROR_JTAG_INIT_FAILED;
2513 }
2514
2515 return ERROR_OK;
2516 }
2517
2518 static int turtle_init(void)
2519 {
2520 uint8_t buf[3];
2521 uint32_t bytes_written;
2522
2523 low_output = 0x08;
2524 low_direction = 0x5b;
2525
2526 /* initialize low byte for jtag */
2527 buf[0] = 0x80; /* command "set data bits low byte" */
2528 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2529 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2530 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2531
2532 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2533 {
2534 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2535 return ERROR_JTAG_INIT_FAILED;
2536 }
2537
2538 nSRST = 0x40;
2539
2540 high_output = 0x00;
2541 high_direction = 0x0C;
2542
2543 /* initialize high port */
2544 buf[0] = 0x82; /* command "set data bits high byte" */
2545 buf[1] = high_output;
2546 buf[2] = high_direction;
2547 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2548
2549 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2550 {
2551 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2552 return ERROR_JTAG_INIT_FAILED;
2553 }
2554
2555 return ERROR_OK;
2556 }
2557
2558 static int comstick_init(void)
2559 {
2560 uint8_t buf[3];
2561 uint32_t bytes_written;
2562
2563 low_output = 0x08;
2564 low_direction = 0x0b;
2565
2566 /* initialize low byte for jtag */
2567 buf[0] = 0x80; /* command "set data bits low byte" */
2568 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2569 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2570 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2571
2572 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2573 {
2574 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2575 return ERROR_JTAG_INIT_FAILED;
2576 }
2577
2578 nTRST = 0x01;
2579 nTRSTnOE = 0x00; /* no output enable for nTRST */
2580 nSRST = 0x02;
2581 nSRSTnOE = 0x00; /* no output enable for nSRST */
2582
2583 high_output = 0x03;
2584 high_direction = 0x03;
2585
2586 /* initialize high port */
2587 buf[0] = 0x82; /* command "set data bits high byte" */
2588 buf[1] = high_output;
2589 buf[2] = high_direction;
2590 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2591
2592 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2593 {
2594 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2595 return ERROR_JTAG_INIT_FAILED;
2596 }
2597
2598 return ERROR_OK;
2599 }
2600
2601 static int stm32stick_init(void)
2602 {
2603 uint8_t buf[3];
2604 uint32_t bytes_written;
2605
2606 low_output = 0x88;
2607 low_direction = 0x8b;
2608
2609 /* initialize low byte for jtag */
2610 buf[0] = 0x80; /* command "set data bits low byte" */
2611 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2612 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2613 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2614
2615 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2616 {
2617 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2618 return ERROR_JTAG_INIT_FAILED;
2619 }
2620
2621 nTRST = 0x01;
2622 nTRSTnOE = 0x00; /* no output enable for nTRST */
2623 nSRST = 0x80;
2624 nSRSTnOE = 0x00; /* no output enable for nSRST */
2625
2626 high_output = 0x01;
2627 high_direction = 0x03;
2628
2629 /* initialize high port */
2630 buf[0] = 0x82; /* command "set data bits high byte" */
2631 buf[1] = high_output;
2632 buf[2] = high_direction;
2633 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2634
2635 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2636 {
2637 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2638 return ERROR_JTAG_INIT_FAILED;
2639 }
2640
2641 return ERROR_OK;
2642 }
2643
2644 static int sheevaplug_init(void)
2645 {
2646 uint8_t buf[3];
2647 uint32_t bytes_written;
2648
2649 low_output = 0x08;
2650 low_direction = 0x1b;
2651
2652 /* initialize low byte for jtag */
2653 buf[0] = 0x80; /* command "set data bits low byte" */
2654 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2655 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2656 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2657
2658 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2659 {
2660 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2661 return ERROR_JTAG_INIT_FAILED;
2662 }
2663
2664 nTRSTnOE = 0x1;
2665 nTRST = 0x02;
2666 nSRSTnOE = 0x4;
2667 nSRST = 0x08;
2668
2669 high_output = 0x0;
2670 high_direction = 0x0f;
2671
2672 /* nTRST is always push-pull */
2673 high_output &= ~nTRSTnOE;
2674 high_output |= nTRST;
2675
2676 /* nSRST is always open-drain */
2677 high_output |= nSRSTnOE;
2678 high_output &= ~nSRST;
2679
2680 /* initialize high port */
2681 buf[0] = 0x82; /* command "set data bits high byte" */
2682 buf[1] = high_output; /* value */
2683 buf[2] = high_direction; /* all outputs - xRST */
2684 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2685
2686 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2687 {
2688 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2689 return ERROR_JTAG_INIT_FAILED;
2690 }
2691
2692 return ERROR_OK;
2693 }
2694
2695 static int cortino_jtag_init(void)
2696 {
2697 uint8_t buf[3];
2698 uint32_t bytes_written;
2699
2700 low_output = 0x08;
2701 low_direction = 0x1b;
2702
2703 /* initialize low byte for jtag */
2704 buf[0] = 0x80; /* command "set data bits low byte" */
2705 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2706 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2707 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2708
2709 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2710 {
2711 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2712 return ERROR_JTAG_INIT_FAILED;
2713 }
2714
2715 nTRST = 0x01;
2716 nTRSTnOE = 0x00; /* no output enable for nTRST */
2717 nSRST = 0x02;
2718 nSRSTnOE = 0x00; /* no output enable for nSRST */
2719
2720 high_output = 0x03;
2721 high_direction = 0x03;
2722
2723 /* initialize high port */
2724 buf[0] = 0x82; /* command "set data bits high byte" */
2725 buf[1] = high_output;
2726 buf[2] = high_direction;
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 'stm32stick' layout");
2732 return ERROR_JTAG_INIT_FAILED;
2733 }
2734
2735 return ERROR_OK;
2736 }
2737
2738 static void olimex_jtag_blink(void)
2739 {
2740 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2741 * ACBUS3 is bit 3 of the GPIOH port
2742 */
2743 if (high_output & 0x08)
2744 {
2745 /* set port pin high */
2746 high_output &= 0x07;
2747 }
2748 else
2749 {
2750 /* set port pin low */
2751 high_output |= 0x08;
2752 }
2753
2754 buffer_write(0x82);
2755 buffer_write(high_output);
2756 buffer_write(high_direction);
2757 }
2758
2759 static void flyswatter_jtag_blink(void)
2760 {
2761 /*
2762 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2763 */
2764 high_output ^= 0x0c;
2765
2766 buffer_write(0x82);
2767 buffer_write(high_output);
2768 buffer_write(high_direction);
2769 }
2770
2771 static void turtle_jtag_blink(void)
2772 {
2773 /*
2774 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2775 */
2776 if (high_output & 0x08)
2777 {
2778 high_output = 0x04;
2779 }
2780 else
2781 {
2782 high_output = 0x08;
2783 }
2784
2785 buffer_write(0x82);
2786 buffer_write(high_output);
2787 buffer_write(high_direction);
2788 }
2789
2790 static int ft2232_quit(void)
2791 {
2792 #if BUILD_FT2232_FTD2XX == 1
2793 FT_STATUS status;
2794
2795 status = FT_Close(ftdih);
2796 #elif BUILD_FT2232_LIBFTDI == 1
2797 ftdi_usb_close(&ftdic);
2798
2799 ftdi_deinit(&ftdic);
2800 #endif
2801
2802 free(ft2232_buffer);
2803 ft2232_buffer = NULL;
2804
2805 return ERROR_OK;
2806 }
2807
2808 COMMAND_HANDLER(ft2232_handle_device_desc_command)
2809 {
2810 char *cp;
2811 char buf[200];
2812 if (CMD_ARGC == 1)
2813 {
2814 ft2232_device_desc = strdup(CMD_ARGV[0]);
2815 cp = strchr(ft2232_device_desc, 0);
2816 /* under Win32, the FTD2XX driver appends an "A" to the end
2817 * of the description, this examines the given desc
2818 * and creates the 'missing' _A or non_A variable. */
2819 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2820 /* it was, so make this the "A" version. */
2821 ft2232_device_desc_A = ft2232_device_desc;
2822 /* and *CREATE* the non-A version. */
2823 strcpy(buf, ft2232_device_desc);
2824 cp = strchr(buf, 0);
2825 cp[-2] = 0;
2826 ft2232_device_desc = strdup(buf);
2827 } else {
2828 /* <space > A not defined
2829 * so create it */
2830 sprintf(buf, "%s A", ft2232_device_desc);
2831 ft2232_device_desc_A = strdup(buf);
2832 }
2833 }
2834 else
2835 {
2836 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2837 }
2838
2839 return ERROR_OK;
2840 }
2841
2842 COMMAND_HANDLER(ft2232_handle_serial_command)
2843 {
2844 if (CMD_ARGC == 1)
2845 {
2846 ft2232_serial = strdup(CMD_ARGV[0]);
2847 }
2848 else
2849 {
2850 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2851 }
2852
2853 return ERROR_OK;
2854 }
2855
2856 COMMAND_HANDLER(ft2232_handle_layout_command)
2857 {
2858 if (CMD_ARGC == 0)
2859 return ERROR_OK;
2860
2861 ft2232_layout = malloc(strlen(CMD_ARGV[0]) + 1);
2862 strcpy(ft2232_layout, CMD_ARGV[0]);
2863
2864 return ERROR_OK;
2865 }
2866
2867 COMMAND_HANDLER(ft2232_handle_vid_pid_command)
2868 {
2869 if (CMD_ARGC > MAX_USB_IDS * 2)
2870 {
2871 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2872 "(maximum is %d pairs)", MAX_USB_IDS);
2873 CMD_ARGC = MAX_USB_IDS * 2;
2874 }
2875 if (CMD_ARGC < 2 || (CMD_ARGC & 1))
2876 {
2877 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2878 if (CMD_ARGC < 2)
2879 return ERROR_COMMAND_SYNTAX_ERROR;
2880 /* remove the incomplete trailing id */
2881 CMD_ARGC -= 1;
2882 }
2883
2884 unsigned i;
2885 for (i = 0; i < CMD_ARGC; i += 2)
2886 {
2887 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
2888 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
2889 }
2890
2891 /*
2892 * Explicitly terminate, in case there are multiples instances of
2893 * ft2232_vid_pid.
2894 */
2895 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2896
2897 return ERROR_OK;
2898 }
2899
2900 COMMAND_HANDLER(ft2232_handle_latency_command)
2901 {
2902 if (CMD_ARGC == 1)
2903 {
2904 ft2232_latency = atoi(CMD_ARGV[0]);
2905 }
2906 else
2907 {
2908 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2909 }
2910
2911 return ERROR_OK;
2912 }
2913
2914 static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
2915 {
2916 int retval = 0;
2917
2918 /* 7 bits of either ones or zeros. */
2919 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2920
2921 while (num_cycles > 0)
2922 {
2923 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2924 * at most 7 bits per invocation. Here we invoke it potentially
2925 * several times.
2926 */
2927 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2928
2929 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2930 {
2931 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2932 retval = ERROR_JTAG_QUEUE_FAILED;
2933
2934 first_unsent = cmd;
2935 }
2936
2937 /* there are no state transitions in this code, so omit state tracking */
2938
2939 /* command "Clock Data to TMS/CS Pin (no Read)" */
2940 buffer_write(0x4b);
2941
2942 /* scan 7 bit */
2943 buffer_write(bitcount_per_command - 1);
2944
2945 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2946 buffer_write(tms);
2947
2948 require_send = 1;
2949
2950 num_cycles -= bitcount_per_command;
2951 }
2952
2953 return retval;
2954 }
2955
2956 /* ---------------------------------------------------------------------
2957 * Support for IceBear JTAG adapter from Section5:
2958 * http://section5.ch/icebear
2959 *
2960 * Author: Sten, debian@sansys-electronic.com
2961 */
2962
2963 /* Icebear pin layout
2964 *
2965 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2966 * GND GND | 4 3| n.c.
2967 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2968 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2969 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2970 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2971 * ADBUS2 TDO |14 13| GND GND
2972 *
2973 * ADBUS0 O L TCK ACBUS0 GND
2974 * ADBUS1 O L TDI ACBUS1 GND
2975 * ADBUS2 I TDO ACBUS2 n.c.
2976 * ADBUS3 O H TMS ACBUS3 n.c.
2977 * ADBUS4 O H nTRST
2978 * ADBUS5 O H nSRST
2979 * ADBUS6 - VCC
2980 * ADBUS7 - GND
2981 */
2982 static int icebear_jtag_init(void) {
2983 uint8_t buf[3];
2984 uint32_t bytes_written;
2985
2986 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
2987 low_output = 0x08; /* high: TMS; low: TCK TDI */
2988 nTRST = 0x10;
2989 nSRST = 0x20;
2990
2991 enum reset_types jtag_reset_config = jtag_get_reset_config();
2992 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
2993 low_direction &= ~nTRST; /* nTRST high impedance */
2994 }
2995 else {
2996 low_direction |= nTRST;
2997 low_output |= nTRST;
2998 }
2999
3000 low_direction |= nSRST;
3001 low_output |= nSRST;
3002
3003 /* initialize low byte for jtag */
3004 buf[0] = 0x80; /* command "set data bits low byte" */
3005 buf[1] = low_output;
3006 buf[2] = low_direction;
3007 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3008
3009 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3010 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3011 return ERROR_JTAG_INIT_FAILED;
3012 }
3013
3014 high_output = 0x0;
3015 high_direction = 0x00;
3016
3017
3018 /* initialize high port */
3019 buf[0] = 0x82; /* command "set data bits high byte" */
3020 buf[1] = high_output; /* value */
3021 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3022 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3023
3024 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3025 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3026 return ERROR_JTAG_INIT_FAILED;
3027 }
3028
3029 return ERROR_OK;
3030 }
3031
3032 static void icebear_jtag_reset(int trst, int srst) {
3033
3034 if (trst == 1) {
3035 low_direction |= nTRST;
3036 low_output &= ~nTRST;
3037 }
3038 else if (trst == 0) {
3039 enum reset_types jtag_reset_config = jtag_get_reset_config();
3040 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3041 low_direction &= ~nTRST;
3042 else
3043 low_output |= nTRST;
3044 }
3045
3046 if (srst == 1) {
3047 low_output &= ~nSRST;
3048 }
3049 else if (srst == 0) {
3050 low_output |= nSRST;
3051 }
3052
3053 /* command "set data bits low byte" */
3054 buffer_write(0x80);
3055 buffer_write(low_output);
3056 buffer_write(low_direction);
3057
3058 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3059 }
3060
3061 /* ---------------------------------------------------------------------
3062 * Support for Signalyzer H2 and Signalyzer H4
3063 * JTAG adapter from Xverve Technologies Inc.
3064 * http://www.signalyzer.com or http://www.xverve.com
3065 *
3066 * Author: Oleg Seiljus, oleg@signalyzer.com
3067 */
3068 static unsigned char signalyzer_h_side;
3069 static unsigned int signalyzer_h_adapter_type;
3070
3071 static int signalyzer_h_ctrl_write(int address, unsigned short value);
3072
3073 #if BUILD_FT2232_FTD2XX == 1
3074 static int signalyzer_h_ctrl_read(int address, unsigned short *value);
3075 #endif
3076
3077 #define SIGNALYZER_COMMAND_ADDR 128
3078 #define SIGNALYZER_DATA_BUFFER_ADDR 129
3079
3080 #define SIGNALYZER_COMMAND_VERSION 0x41
3081 #define SIGNALYZER_COMMAND_RESET 0x42
3082 #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
3083 #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
3084 #define SIGNALYZER_COMMAND_PWM_SET 0x52
3085 #define SIGNALYZER_COMMAND_LED_SET 0x53
3086 #define SIGNALYZER_COMMAND_ADC 0x54
3087 #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
3088 #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
3089 #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
3090 #define SIGNALYZER_COMMAND_I2C 0x58
3091
3092 #define SIGNALYZER_CHAN_A 1
3093 #define SIGNALYZER_CHAN_B 2
3094 /* LEDS use channel C */
3095 #define SIGNALYZER_CHAN_C 4
3096
3097 #define SIGNALYZER_LED_GREEN 1
3098 #define SIGNALYZER_LED_RED 2
3099
3100 #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
3101 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
3102 #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
3103 #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
3104 #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
3105
3106
3107 static int signalyzer_h_ctrl_write(int address, unsigned short value)
3108 {
3109 #if BUILD_FT2232_FTD2XX == 1
3110 return FT_WriteEE(ftdih, address, value);
3111 #elif BUILD_FT2232_LIBFTDI == 1
3112 return 0;
3113 #endif
3114 }
3115
3116 #if BUILD_FT2232_FTD2XX == 1
3117 static int signalyzer_h_ctrl_read(int address, unsigned short *value)
3118 {
3119 return FT_ReadEE(ftdih, address, value);
3120 }
3121 #endif
3122
3123 static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
3124 int on_time_ms, int off_time_ms, unsigned char cycles)
3125 {
3126 unsigned char on_time;
3127 unsigned char off_time;
3128
3129 if (on_time_ms < 0xFFFF)
3130 on_time = (unsigned char)(on_time_ms / 62);
3131 else
3132 on_time = 0xFF;
3133
3134 off_time = (unsigned char)(off_time_ms / 62);
3135
3136 #if BUILD_FT2232_FTD2XX == 1
3137 FT_STATUS status;
3138
3139 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3140 ((uint32_t)(channel << 8) | led))) != FT_OK)
3141 {
3142 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3143 return ERROR_JTAG_DEVICE_ERROR;
3144 }
3145
3146 if ((status = signalyzer_h_ctrl_write(
3147 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3148 ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
3149 {
3150 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3151 return ERROR_JTAG_DEVICE_ERROR;
3152 }
3153
3154 if ((status = signalyzer_h_ctrl_write(
3155 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3156 ((uint32_t)cycles))) != FT_OK)
3157 {
3158 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3159 return ERROR_JTAG_DEVICE_ERROR;
3160 }
3161
3162 if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3163 SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
3164 {
3165 LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
3166 return ERROR_JTAG_DEVICE_ERROR;
3167 }
3168
3169 return ERROR_OK;
3170 #elif BUILD_FT2232_LIBFTDI == 1
3171 int retval;
3172
3173 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
3174 ((uint32_t)(channel << 8) | led))) < 0)
3175 {
3176 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3177 ftdi_get_error_string(&ftdic));
3178 return ERROR_JTAG_DEVICE_ERROR;
3179 }
3180
3181 if ((retval = signalyzer_h_ctrl_write(
3182 (SIGNALYZER_DATA_BUFFER_ADDR + 1),
3183 ((uint32_t)(on_time << 8) | off_time))) < 0)
3184 {
3185 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3186 ftdi_get_error_string(&ftdic));
3187 return ERROR_JTAG_DEVICE_ERROR;
3188 }
3189
3190 if ((retval = signalyzer_h_ctrl_write(
3191 (SIGNALYZER_DATA_BUFFER_ADDR + 2),
3192 (uint32_t)cycles)) < 0)
3193 {
3194 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
3195 ftdi_get_error_string(&ftdic));
3196 return ERROR_JTAG_DEVICE_ERROR;
3197 }
3198
3199 if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
3200 SIGNALYZER_COMMAND_LED_SET)) < 0)
3201 {
3202 LOG_ERROR("signalyzer_h_ctrl_write returned: %s",