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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)