Remove much #ifdeffery around _DEBUG_JTAG_IO_ usage.
[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 tap_set_state(TAP_IDLE);
1556
1557 i -= (i > 7) ? 7 : i;
1558 /* LOG_DEBUG("added TMS scan (no read)"); */
1559 }
1560
1561 ft2232_end_state(cmd->cmd.runtest->end_state);
1562
1563 if (tap_get_state() != tap_get_end_state())
1564 {
1565 move_to_state(tap_get_end_state());
1566 }
1567
1568 require_send = 1;
1569 DEBUG_JTAG_IO("runtest: %i, end in %s",
1570 cmd->cmd.runtest->num_cycles,
1571 tap_state_name(tap_get_end_state()));
1572 return retval;
1573 }
1574
1575 static int ft2232_execute_statemove(jtag_command_t *cmd)
1576 {
1577 int predicted_size = 0;
1578 int retval = ERROR_OK;
1579
1580 DEBUG_JTAG_IO("statemove end in %s",
1581 tap_state_name(cmd->cmd.statemove->end_state));
1582
1583 /* only send the maximum buffer size that FT2232C can handle */
1584 predicted_size = 3;
1585 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1586 {
1587 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1588 retval = ERROR_JTAG_QUEUE_FAILED;
1589 require_send = 0;
1590 first_unsent = cmd;
1591 }
1592 ft2232_end_state(cmd->cmd.statemove->end_state);
1593
1594 /* For TAP_RESET, ignore the current recorded state. It's often
1595 * wrong at server startup, and this transation is critical whenever
1596 * it's requested.
1597 */
1598 if (tap_get_end_state() == TAP_RESET) {
1599 clock_tms(0x4b, 0xff, 5, 0);
1600 require_send = 1;
1601
1602 /* shortest-path move to desired end state */
1603 } else if (tap_get_state() != tap_get_end_state())
1604 {
1605 move_to_state(tap_get_end_state());
1606 require_send = 1;
1607 }
1608
1609 return retval;
1610 }
1611
1612 static int ft2232_execute_pathmove(jtag_command_t *cmd)
1613 {
1614 int predicted_size = 0;
1615 int retval = ERROR_OK;
1616
1617 tap_state_t* path = cmd->cmd.pathmove->path;
1618 int num_states = cmd->cmd.pathmove->num_states;
1619
1620 DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
1621 tap_state_name(tap_get_state()),
1622 tap_state_name(path[num_states-1]));
1623
1624 /* only send the maximum buffer size that FT2232C can handle */
1625 predicted_size = 3 * CEIL(num_states, 7);
1626 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1627 {
1628 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1629 retval = ERROR_JTAG_QUEUE_FAILED;
1630
1631 require_send = 0;
1632 first_unsent = cmd;
1633 }
1634
1635 ft2232_add_pathmove(path, num_states);
1636 require_send = 1;
1637
1638 return retval;
1639 }
1640
1641 static int ft2232_execute_scan(jtag_command_t *cmd)
1642 {
1643 uint8_t* buffer;
1644 int scan_size; /* size of IR or DR scan */
1645 int predicted_size = 0;
1646 int retval = ERROR_OK;
1647
1648 enum scan_type type = jtag_scan_type(cmd->cmd.scan);
1649
1650 DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
1651
1652 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
1653
1654 predicted_size = ft2232_predict_scan_out(scan_size, type);
1655 if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
1656 {
1657 LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
1658 /* unsent commands before this */
1659 if (first_unsent != cmd)
1660 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1661 retval = ERROR_JTAG_QUEUE_FAILED;
1662
1663 /* current command */
1664 ft2232_end_state(cmd->cmd.scan->end_state);
1665 ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
1666 require_send = 0;
1667 first_unsent = cmd->next;
1668 if (buffer)
1669 free(buffer);
1670 return retval;
1671 }
1672 else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1673 {
1674 LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
1675 first_unsent,
1676 cmd);
1677 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1678 retval = ERROR_JTAG_QUEUE_FAILED;
1679 require_send = 0;
1680 first_unsent = cmd;
1681 }
1682 ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
1683 /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
1684 ft2232_end_state(cmd->cmd.scan->end_state);
1685 ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
1686 require_send = 1;
1687 if (buffer)
1688 free(buffer);
1689 DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
1690 (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
1691 tap_state_name(tap_get_end_state()));
1692 return retval;
1693
1694 }
1695
1696 static int ft2232_execute_reset(jtag_command_t *cmd)
1697 {
1698 int retval;
1699 int predicted_size = 0;
1700 retval = ERROR_OK;
1701
1702 DEBUG_JTAG_IO("reset trst: %i srst %i",
1703 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1704
1705 /* only send the maximum buffer size that FT2232C can handle */
1706 predicted_size = 3;
1707 if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
1708 {
1709 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1710 retval = ERROR_JTAG_QUEUE_FAILED;
1711 require_send = 0;
1712 first_unsent = cmd;
1713 }
1714
1715 if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
1716 {
1717 tap_set_state(TAP_RESET);
1718 }
1719
1720 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1721 require_send = 1;
1722
1723 DEBUG_JTAG_IO("trst: %i, srst: %i",
1724 cmd->cmd.reset->trst, cmd->cmd.reset->srst);
1725 return retval;
1726 }
1727
1728 static int ft2232_execute_sleep(jtag_command_t *cmd)
1729 {
1730 int retval;
1731 retval = ERROR_OK;
1732
1733 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
1734
1735 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1736 retval = ERROR_JTAG_QUEUE_FAILED;
1737 first_unsent = cmd->next;
1738 jtag_sleep(cmd->cmd.sleep->us);
1739 DEBUG_JTAG_IO("sleep %i usec while in %s",
1740 cmd->cmd.sleep->us,
1741 tap_state_name(tap_get_state()));
1742 return retval;
1743 }
1744
1745 static int ft2232_execute_stableclocks(jtag_command_t *cmd)
1746 {
1747 int retval;
1748 retval = ERROR_OK;
1749
1750 /* this is only allowed while in a stable state. A check for a stable
1751 * state was done in jtag_add_clocks()
1752 */
1753 if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
1754 retval = ERROR_JTAG_QUEUE_FAILED;
1755 DEBUG_JTAG_IO("clocks %i while in %s",
1756 cmd->cmd.stableclocks->num_cycles,
1757 tap_state_name(tap_get_state()));
1758 return retval;
1759 }
1760
1761 static int ft2232_execute_command(jtag_command_t *cmd)
1762 {
1763 int retval;
1764 retval = ERROR_OK;
1765
1766 switch (cmd->type)
1767 {
1768 case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
1769 case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
1770 case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
1771 case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
1772 case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
1773 case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
1774 case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
1775 default:
1776 LOG_ERROR("BUG: unknown JTAG command type encountered");
1777 exit(-1);
1778 }
1779 return retval;
1780 }
1781
1782 static int ft2232_execute_queue()
1783 {
1784 jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
1785 int retval;
1786
1787 first_unsent = cmd; /* next command that has to be sent */
1788 require_send = 0;
1789
1790 /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
1791 * that wasn't handled by a caller-provided error handler
1792 */
1793 retval = ERROR_OK;
1794
1795 ft2232_buffer_size = 0;
1796 ft2232_expect_read = 0;
1797
1798 /* blink, if the current layout has that feature */
1799 if (layout->blink)
1800 layout->blink();
1801
1802 while (cmd)
1803 {
1804 if (ft2232_execute_command(cmd) != ERROR_OK)
1805 retval = ERROR_JTAG_QUEUE_FAILED;
1806 /* Start reading input before FT2232 TX buffer fills up */
1807 cmd = cmd->next;
1808 if (ft2232_expect_read > 256)
1809 {
1810 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1811 retval = ERROR_JTAG_QUEUE_FAILED;
1812 first_unsent = cmd;
1813 }
1814 }
1815
1816 if (require_send > 0)
1817 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
1818 retval = ERROR_JTAG_QUEUE_FAILED;
1819
1820 return retval;
1821 }
1822
1823 #if BUILD_FT2232_FTD2XX == 1
1824 static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
1825 {
1826 FT_STATUS status;
1827 DWORD deviceID;
1828 char SerialNumber[16];
1829 char Description[64];
1830 DWORD openex_flags = 0;
1831 char* openex_string = NULL;
1832 uint8_t latency_timer;
1833
1834 LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
1835
1836 #if IS_WIN32 == 0
1837 /* Add non-standard Vid/Pid to the linux driver */
1838 if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
1839 {
1840 LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
1841 }
1842 #endif
1843
1844 if (ft2232_device_desc && ft2232_serial)
1845 {
1846 LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
1847 ft2232_device_desc = NULL;
1848 }
1849
1850 if (ft2232_device_desc)
1851 {
1852 openex_string = ft2232_device_desc;
1853 openex_flags = FT_OPEN_BY_DESCRIPTION;
1854 }
1855 else if (ft2232_serial)
1856 {
1857 openex_string = ft2232_serial;
1858 openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
1859 }
1860 else
1861 {
1862 LOG_ERROR("neither device description nor serial number specified");
1863 LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
1864
1865 return ERROR_JTAG_INIT_FAILED;
1866 }
1867
1868 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1869 if (status != FT_OK) {
1870 /* under Win32, the FTD2XX driver appends an "A" to the end
1871 * of the description, if we tried by the desc, then
1872 * try by the alternate "A" description. */
1873 if (openex_string == ft2232_device_desc) {
1874 /* Try the alternate method. */
1875 openex_string = ft2232_device_desc_A;
1876 status = FT_OpenEx(openex_string, openex_flags, &ftdih);
1877 if (status == FT_OK) {
1878 /* yea, the "alternate" method worked! */
1879 } else {
1880 /* drat, give the user a meaningfull message.
1881 * telling the use we tried *BOTH* methods. */
1882 LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
1883 ft2232_device_desc,
1884 ft2232_device_desc_A);
1885 }
1886 }
1887 }
1888
1889 if (status != FT_OK)
1890 {
1891 DWORD num_devices;
1892
1893 if (more)
1894 {
1895 LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
1896 *try_more = 1;
1897 return ERROR_JTAG_INIT_FAILED;
1898 }
1899 LOG_ERROR("unable to open ftdi device: %lu", status);
1900 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
1901 if (status == FT_OK)
1902 {
1903 char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
1904 uint32_t i;
1905
1906 for (i = 0; i < num_devices; i++)
1907 desc_array[i] = malloc(64);
1908
1909 desc_array[num_devices] = NULL;
1910
1911 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
1912
1913 if (status == FT_OK)
1914 {
1915 LOG_ERROR("ListDevices: %lu\n", num_devices);
1916 for (i = 0; i < num_devices; i++)
1917 LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
1918 }
1919
1920 for (i = 0; i < num_devices; i++)
1921 free(desc_array[i]);
1922
1923 free(desc_array);
1924 }
1925 else
1926 {
1927 LOG_ERROR("ListDevices: NONE\n");
1928 }
1929 return ERROR_JTAG_INIT_FAILED;
1930 }
1931
1932 if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
1933 {
1934 LOG_ERROR("unable to set latency timer: %lu", status);
1935 return ERROR_JTAG_INIT_FAILED;
1936 }
1937
1938 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
1939 {
1940 LOG_ERROR("unable to get latency timer: %lu", status);
1941 return ERROR_JTAG_INIT_FAILED;
1942 }
1943 else
1944 {
1945 LOG_DEBUG("current latency timer: %i", latency_timer);
1946 }
1947
1948 if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
1949 {
1950 LOG_ERROR("unable to set timeouts: %lu", status);
1951 return ERROR_JTAG_INIT_FAILED;
1952 }
1953
1954 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
1955 {
1956 LOG_ERROR("unable to enable bit i/o mode: %lu", status);
1957 return ERROR_JTAG_INIT_FAILED;
1958 }
1959
1960 if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
1961 {
1962 LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
1963 return ERROR_JTAG_INIT_FAILED;
1964 }
1965 else
1966 {
1967 static const char* type_str[] =
1968 {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
1969 unsigned no_of_known_types = sizeof(type_str) / sizeof(type_str[0]) - 1;
1970 unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
1971 ? ftdi_device : FT_DEVICE_UNKNOWN;
1972 LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
1973 LOG_INFO("deviceID: %lu", deviceID);
1974 LOG_INFO("SerialNumber: %s", SerialNumber);
1975 LOG_INFO("Description: %s", Description);
1976 }
1977
1978 return ERROR_OK;
1979 }
1980
1981 static int ft2232_purge_ftd2xx(void)
1982 {
1983 FT_STATUS status;
1984
1985 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
1986 {
1987 LOG_ERROR("error purging ftd2xx device: %lu", status);
1988 return ERROR_JTAG_INIT_FAILED;
1989 }
1990
1991 return ERROR_OK;
1992 }
1993
1994 #endif /* BUILD_FT2232_FTD2XX == 1 */
1995
1996 #if BUILD_FT2232_LIBFTDI == 1
1997 static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
1998 {
1999 uint8_t latency_timer;
2000
2001 LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
2002 ft2232_layout, vid, pid);
2003
2004 if (ftdi_init(&ftdic) < 0)
2005 return ERROR_JTAG_INIT_FAILED;
2006
2007 if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
2008 {
2009 LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
2010 return ERROR_JTAG_INIT_FAILED;
2011 }
2012
2013 /* context, vendor id, product id */
2014 if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
2015 ft2232_serial) < 0)
2016 {
2017 if (more)
2018 LOG_WARNING("unable to open ftdi device (trying more): %s",
2019 ftdic.error_str);
2020 else
2021 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
2022 *try_more = 1;
2023 return ERROR_JTAG_INIT_FAILED;
2024 }
2025
2026 /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
2027 if (ftdi_usb_reset(&ftdic) < 0)
2028 {
2029 LOG_ERROR("unable to reset ftdi device");
2030 return ERROR_JTAG_INIT_FAILED;
2031 }
2032
2033 if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
2034 {
2035 LOG_ERROR("unable to set latency timer");
2036 return ERROR_JTAG_INIT_FAILED;
2037 }
2038
2039 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
2040 {
2041 LOG_ERROR("unable to get latency timer");
2042 return ERROR_JTAG_INIT_FAILED;
2043 }
2044 else
2045 {
2046 LOG_DEBUG("current latency timer: %i", latency_timer);
2047 }
2048
2049 ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
2050
2051 ftdi_device = ftdic.type;
2052 static const char* type_str[] =
2053 {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
2054 unsigned no_of_known_types = sizeof(type_str) / sizeof(type_str[0]) - 1;
2055 unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
2056 ? ftdi_device : no_of_known_types;
2057 LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
2058 return ERROR_OK;
2059 }
2060
2061 static int ft2232_purge_libftdi(void)
2062 {
2063 if (ftdi_usb_purge_buffers(&ftdic) < 0)
2064 {
2065 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
2066 return ERROR_JTAG_INIT_FAILED;
2067 }
2068
2069 return ERROR_OK;
2070 }
2071
2072 #endif /* BUILD_FT2232_LIBFTDI == 1 */
2073
2074 static int ft2232_init(void)
2075 {
2076 uint8_t buf[1];
2077 int retval;
2078 uint32_t bytes_written;
2079 const ft2232_layout_t* cur_layout = ft2232_layouts;
2080 int i;
2081
2082 if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
2083 {
2084 LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
2085 }
2086 else
2087 {
2088 LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
2089
2090 }
2091 if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
2092 {
2093 ft2232_layout = "usbjtag";
2094 LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
2095 }
2096
2097 while (cur_layout->name)
2098 {
2099 if (strcmp(cur_layout->name, ft2232_layout) == 0)
2100 {
2101 layout = cur_layout;
2102 break;
2103 }
2104 cur_layout++;
2105 }
2106
2107 if (!layout)
2108 {
2109 LOG_ERROR("No matching layout found for %s", ft2232_layout);
2110 return ERROR_JTAG_INIT_FAILED;
2111 }
2112
2113 for (i = 0; 1; i++)
2114 {
2115 /*
2116 * "more indicates that there are more IDs to try, so we should
2117 * not print an error for an ID mismatch (but for anything
2118 * else, we should).
2119 *
2120 * try_more indicates that the error code returned indicates an
2121 * ID mismatch (and nothing else) and that we should proceeed
2122 * with the next ID pair.
2123 */
2124 int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
2125 int try_more = 0;
2126
2127 #if BUILD_FT2232_FTD2XX == 1
2128 retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
2129 more, &try_more);
2130 #elif BUILD_FT2232_LIBFTDI == 1
2131 retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
2132 more, &try_more);
2133 #endif
2134 if (retval >= 0)
2135 break;
2136 if (!more || !try_more)
2137 return retval;
2138 }
2139
2140 ft2232_buffer_size = 0;
2141 ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
2142
2143 if (layout->init() != ERROR_OK)
2144 return ERROR_JTAG_INIT_FAILED;
2145
2146 if (ft2232_device_is_highspeed())
2147 {
2148 #ifndef BUILD_FT2232_HIGHSPEED
2149 #if BUILD_FT2232_FTD2XX == 1
2150 LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
2151 #elif BUILD_FT2232_LIBFTDI == 1
2152 LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
2153 #endif
2154 #endif
2155 /* make sure the legacy mode is disabled */
2156 if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
2157 return ERROR_JTAG_INIT_FAILED;
2158 }
2159
2160 ft2232_speed(jtag_get_speed());
2161
2162 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
2163 if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
2164 {
2165 LOG_ERROR("couldn't write to FT2232 to disable loopback");
2166 return ERROR_JTAG_INIT_FAILED;
2167 }
2168
2169 #if BUILD_FT2232_FTD2XX == 1
2170 return ft2232_purge_ftd2xx();
2171 #elif BUILD_FT2232_LIBFTDI == 1
2172 return ft2232_purge_libftdi();
2173 #endif
2174
2175 return ERROR_OK;
2176 }
2177
2178 static int usbjtag_init(void)
2179 {
2180 uint8_t buf[3];
2181 uint32_t bytes_written;
2182
2183 low_output = 0x08;
2184 low_direction = 0x0b;
2185
2186 if (strcmp(ft2232_layout, "usbjtag") == 0)
2187 {
2188 nTRST = 0x10;
2189 nTRSTnOE = 0x10;
2190 nSRST = 0x40;
2191 nSRSTnOE = 0x40;
2192 }
2193 else if (strcmp(ft2232_layout, "signalyzer") == 0)
2194 {
2195 nTRST = 0x10;
2196 nTRSTnOE = 0x10;
2197 nSRST = 0x20;
2198 nSRSTnOE = 0x20;
2199 }
2200 else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
2201 {
2202 nTRST = 0x0;
2203 nTRSTnOE = 0x00;
2204 nSRST = 0x20;
2205 nSRSTnOE = 0x20;
2206 low_output = 0x88;
2207 low_direction = 0x8b;
2208 }
2209 else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
2210 {
2211 nTRST = 0x0;
2212 nTRSTnOE = 0x00;
2213 nSRST = 0x20;
2214 nSRSTnOE = 0x20;
2215 low_output = 0x88;
2216 low_direction = 0xcb;
2217 }
2218 else
2219 {
2220 LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
2221 return ERROR_JTAG_INIT_FAILED;
2222 }
2223
2224 enum reset_types jtag_reset_config = jtag_get_reset_config();
2225 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2226 {
2227 low_direction &= ~nTRSTnOE; /* nTRST input */
2228 low_output &= ~nTRST; /* nTRST = 0 */
2229 }
2230 else
2231 {
2232 low_direction |= nTRSTnOE; /* nTRST output */
2233 low_output |= nTRST; /* nTRST = 1 */
2234 }
2235
2236 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2237 {
2238 low_direction |= nSRSTnOE; /* nSRST output */
2239 low_output |= nSRST; /* nSRST = 1 */
2240 }
2241 else
2242 {
2243 low_direction &= ~nSRSTnOE; /* nSRST input */
2244 low_output &= ~nSRST; /* nSRST = 0 */
2245 }
2246
2247 /* initialize low byte for jtag */
2248 buf[0] = 0x80; /* command "set data bits low byte" */
2249 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
2250 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2251 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2252
2253 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2254 {
2255 LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
2256 return ERROR_JTAG_INIT_FAILED;
2257 }
2258
2259 return ERROR_OK;
2260 }
2261
2262 static int axm0432_jtag_init(void)
2263 {
2264 uint8_t buf[3];
2265 uint32_t bytes_written;
2266
2267 low_output = 0x08;
2268 low_direction = 0x2b;
2269
2270 /* initialize low byte for jtag */
2271 buf[0] = 0x80; /* command "set data bits low byte" */
2272 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2273 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2274 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2275
2276 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2277 {
2278 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2279 return ERROR_JTAG_INIT_FAILED;
2280 }
2281
2282 if (strcmp(layout->name, "axm0432_jtag") == 0)
2283 {
2284 nTRST = 0x08;
2285 nTRSTnOE = 0x0; /* No output enable for TRST*/
2286 nSRST = 0x04;
2287 nSRSTnOE = 0x0; /* No output enable for SRST*/
2288 }
2289 else
2290 {
2291 LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
2292 exit(-1);
2293 }
2294
2295 high_output = 0x0;
2296 high_direction = 0x0c;
2297
2298 enum reset_types jtag_reset_config = jtag_get_reset_config();
2299 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2300 {
2301 LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
2302 }
2303 else
2304 {
2305 high_output |= nTRST;
2306 }
2307
2308 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2309 {
2310 LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
2311 }
2312 else
2313 {
2314 high_output |= nSRST;
2315 }
2316
2317 /* initialize high port */
2318 buf[0] = 0x82; /* command "set data bits high byte" */
2319 buf[1] = high_output; /* value */
2320 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2321 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2322
2323 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2324 {
2325 LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
2326 return ERROR_JTAG_INIT_FAILED;
2327 }
2328
2329 return ERROR_OK;
2330 }
2331
2332 static int jtagkey_init(void)
2333 {
2334 uint8_t buf[3];
2335 uint32_t bytes_written;
2336
2337 low_output = 0x08;
2338 low_direction = 0x1b;
2339
2340 /* initialize low byte for jtag */
2341 buf[0] = 0x80; /* command "set data bits low byte" */
2342 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2343 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2344 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2345
2346 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2347 {
2348 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2349 return ERROR_JTAG_INIT_FAILED;
2350 }
2351
2352 if (strcmp(layout->name, "jtagkey") == 0)
2353 {
2354 nTRST = 0x01;
2355 nTRSTnOE = 0x4;
2356 nSRST = 0x02;
2357 nSRSTnOE = 0x08;
2358 }
2359 else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
2360 || (strcmp(layout->name, "oocdlink") == 0))
2361 {
2362 nTRST = 0x02;
2363 nTRSTnOE = 0x1;
2364 nSRST = 0x08;
2365 nSRSTnOE = 0x04;
2366 }
2367 else
2368 {
2369 LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
2370 exit(-1);
2371 }
2372
2373 high_output = 0x0;
2374 high_direction = 0x0f;
2375
2376 enum reset_types jtag_reset_config = jtag_get_reset_config();
2377 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2378 {
2379 high_output |= nTRSTnOE;
2380 high_output &= ~nTRST;
2381 }
2382 else
2383 {
2384 high_output &= ~nTRSTnOE;
2385 high_output |= nTRST;
2386 }
2387
2388 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2389 {
2390 high_output &= ~nSRSTnOE;
2391 high_output |= nSRST;
2392 }
2393 else
2394 {
2395 high_output |= nSRSTnOE;
2396 high_output &= ~nSRST;
2397 }
2398
2399 /* initialize high port */
2400 buf[0] = 0x82; /* command "set data bits high byte" */
2401 buf[1] = high_output; /* value */
2402 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2403 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2404
2405 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2406 {
2407 LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
2408 return ERROR_JTAG_INIT_FAILED;
2409 }
2410
2411 return ERROR_OK;
2412 }
2413
2414 static int olimex_jtag_init(void)
2415 {
2416 uint8_t buf[3];
2417 uint32_t bytes_written;
2418
2419 low_output = 0x08;
2420 low_direction = 0x1b;
2421
2422 /* initialize low byte for jtag */
2423 buf[0] = 0x80; /* command "set data bits low byte" */
2424 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2425 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2426 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2427
2428 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2429 {
2430 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2431 return ERROR_JTAG_INIT_FAILED;
2432 }
2433
2434 nTRST = 0x01;
2435 nTRSTnOE = 0x4;
2436 nSRST = 0x02;
2437 nSRSTnOE = 0x00; /* no output enable for nSRST */
2438
2439 high_output = 0x0;
2440 high_direction = 0x0f;
2441
2442 enum reset_types jtag_reset_config = jtag_get_reset_config();
2443 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
2444 {
2445 high_output |= nTRSTnOE;
2446 high_output &= ~nTRST;
2447 }
2448 else
2449 {
2450 high_output &= ~nTRSTnOE;
2451 high_output |= nTRST;
2452 }
2453
2454 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
2455 {
2456 LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
2457 }
2458 else
2459 {
2460 high_output &= ~nSRST;
2461 }
2462
2463 /* turn red LED on */
2464 high_output |= 0x08;
2465
2466 /* initialize high port */
2467 buf[0] = 0x82; /* command "set data bits high byte" */
2468 buf[1] = high_output; /* value */
2469 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2470 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2471
2472 if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
2473 {
2474 LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
2475 return ERROR_JTAG_INIT_FAILED;
2476 }
2477
2478 return ERROR_OK;
2479 }
2480
2481 static int flyswatter_init(void)
2482 {
2483 uint8_t buf[3];
2484 uint32_t bytes_written;
2485
2486 low_output = 0x18;
2487 low_direction = 0xfb;
2488
2489 /* initialize low byte for jtag */
2490 buf[0] = 0x80; /* command "set data bits low byte" */
2491 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2492 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
2493 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2494
2495 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2496 {
2497 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2498 return ERROR_JTAG_INIT_FAILED;
2499 }
2500
2501 nTRST = 0x10;
2502 nTRSTnOE = 0x0; /* not output enable for nTRST */
2503 nSRST = 0x20;
2504 nSRSTnOE = 0x00; /* no output enable for nSRST */
2505
2506 high_output = 0x00;
2507 high_direction = 0x0c;
2508
2509 /* turn red LED3 on, LED2 off */
2510 high_output |= 0x08;
2511
2512 /* initialize high port */
2513 buf[0] = 0x82; /* command "set data bits high byte" */
2514 buf[1] = high_output; /* value */
2515 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
2516 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2517
2518 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2519 {
2520 LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
2521 return ERROR_JTAG_INIT_FAILED;
2522 }
2523
2524 return ERROR_OK;
2525 }
2526
2527 static int turtle_init(void)
2528 {
2529 uint8_t buf[3];
2530 uint32_t bytes_written;
2531
2532 low_output = 0x08;
2533 low_direction = 0x5b;
2534
2535 /* initialize low byte for jtag */
2536 buf[0] = 0x80; /* command "set data bits low byte" */
2537 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2538 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2539 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2540
2541 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2542 {
2543 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2544 return ERROR_JTAG_INIT_FAILED;
2545 }
2546
2547 nSRST = 0x40;
2548
2549 high_output = 0x00;
2550 high_direction = 0x0C;
2551
2552 /* initialize high port */
2553 buf[0] = 0x82; /* command "set data bits high byte" */
2554 buf[1] = high_output;
2555 buf[2] = high_direction;
2556 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2557
2558 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2559 {
2560 LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
2561 return ERROR_JTAG_INIT_FAILED;
2562 }
2563
2564 return ERROR_OK;
2565 }
2566
2567 static int comstick_init(void)
2568 {
2569 uint8_t buf[3];
2570 uint32_t bytes_written;
2571
2572 low_output = 0x08;
2573 low_direction = 0x0b;
2574
2575 /* initialize low byte for jtag */
2576 buf[0] = 0x80; /* command "set data bits low byte" */
2577 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2578 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2579 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2580
2581 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2582 {
2583 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2584 return ERROR_JTAG_INIT_FAILED;
2585 }
2586
2587 nTRST = 0x01;
2588 nTRSTnOE = 0x00; /* no output enable for nTRST */
2589 nSRST = 0x02;
2590 nSRSTnOE = 0x00; /* no output enable for nSRST */
2591
2592 high_output = 0x03;
2593 high_direction = 0x03;
2594
2595 /* initialize high port */
2596 buf[0] = 0x82; /* command "set data bits high byte" */
2597 buf[1] = high_output;
2598 buf[2] = high_direction;
2599 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2600
2601 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2602 {
2603 LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
2604 return ERROR_JTAG_INIT_FAILED;
2605 }
2606
2607 return ERROR_OK;
2608 }
2609
2610 static int stm32stick_init(void)
2611 {
2612 uint8_t buf[3];
2613 uint32_t bytes_written;
2614
2615 low_output = 0x88;
2616 low_direction = 0x8b;
2617
2618 /* initialize low byte for jtag */
2619 buf[0] = 0x80; /* command "set data bits low byte" */
2620 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2621 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2622 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2623
2624 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2625 {
2626 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2627 return ERROR_JTAG_INIT_FAILED;
2628 }
2629
2630 nTRST = 0x01;
2631 nTRSTnOE = 0x00; /* no output enable for nTRST */
2632 nSRST = 0x80;
2633 nSRSTnOE = 0x00; /* no output enable for nSRST */
2634
2635 high_output = 0x01;
2636 high_direction = 0x03;
2637
2638 /* initialize high port */
2639 buf[0] = 0x82; /* command "set data bits high byte" */
2640 buf[1] = high_output;
2641 buf[2] = high_direction;
2642 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2643
2644 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2645 {
2646 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2647 return ERROR_JTAG_INIT_FAILED;
2648 }
2649
2650 return ERROR_OK;
2651 }
2652
2653 static int sheevaplug_init(void)
2654 {
2655 uint8_t buf[3];
2656 uint32_t bytes_written;
2657
2658 low_output = 0x08;
2659 low_direction = 0x1b;
2660
2661 /* initialize low byte for jtag */
2662 buf[0] = 0x80; /* command "set data bits low byte" */
2663 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2664 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
2665 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2666
2667 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2668 {
2669 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2670 return ERROR_JTAG_INIT_FAILED;
2671 }
2672
2673 nTRSTnOE = 0x1;
2674 nTRST = 0x02;
2675 nSRSTnOE = 0x4;
2676 nSRST = 0x08;
2677
2678 high_output = 0x0;
2679 high_direction = 0x0f;
2680
2681 /* nTRST is always push-pull */
2682 high_output &= ~nTRSTnOE;
2683 high_output |= nTRST;
2684
2685 /* nSRST is always open-drain */
2686 high_output |= nSRSTnOE;
2687 high_output &= ~nSRST;
2688
2689 /* initialize high port */
2690 buf[0] = 0x82; /* command "set data bits high byte" */
2691 buf[1] = high_output; /* value */
2692 buf[2] = high_direction; /* all outputs - xRST */
2693 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2694
2695 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2696 {
2697 LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
2698 return ERROR_JTAG_INIT_FAILED;
2699 }
2700
2701 return ERROR_OK;
2702 }
2703
2704 static int cortino_jtag_init(void)
2705 {
2706 uint8_t buf[3];
2707 uint32_t bytes_written;
2708
2709 low_output = 0x08;
2710 low_direction = 0x1b;
2711
2712 /* initialize low byte for jtag */
2713 buf[0] = 0x80; /* command "set data bits low byte" */
2714 buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
2715 buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
2716 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2717
2718 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2719 {
2720 LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
2721 return ERROR_JTAG_INIT_FAILED;
2722 }
2723
2724 nTRST = 0x01;
2725 nTRSTnOE = 0x00; /* no output enable for nTRST */
2726 nSRST = 0x02;
2727 nSRSTnOE = 0x00; /* no output enable for nSRST */
2728
2729 high_output = 0x03;
2730 high_direction = 0x03;
2731
2732 /* initialize high port */
2733 buf[0] = 0x82; /* command "set data bits high byte" */
2734 buf[1] = high_output;
2735 buf[2] = high_direction;
2736 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
2737
2738 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
2739 {
2740 LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
2741 return ERROR_JTAG_INIT_FAILED;
2742 }
2743
2744 return ERROR_OK;
2745 }
2746
2747 static void olimex_jtag_blink(void)
2748 {
2749 /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
2750 * ACBUS3 is bit 3 of the GPIOH port
2751 */
2752 if (high_output & 0x08)
2753 {
2754 /* set port pin high */
2755 high_output &= 0x07;
2756 }
2757 else
2758 {
2759 /* set port pin low */
2760 high_output |= 0x08;
2761 }
2762
2763 buffer_write(0x82);
2764 buffer_write(high_output);
2765 buffer_write(high_direction);
2766 }
2767
2768 static void flyswatter_jtag_blink(void)
2769 {
2770 /*
2771 * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
2772 */
2773 high_output ^= 0x0c;
2774
2775 buffer_write(0x82);
2776 buffer_write(high_output);
2777 buffer_write(high_direction);
2778 }
2779
2780 static void turtle_jtag_blink(void)
2781 {
2782 /*
2783 * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
2784 */
2785 if (high_output & 0x08)
2786 {
2787 high_output = 0x04;
2788 }
2789 else
2790 {
2791 high_output = 0x08;
2792 }
2793
2794 buffer_write(0x82);
2795 buffer_write(high_output);
2796 buffer_write(high_direction);
2797 }
2798
2799 static int ft2232_quit(void)
2800 {
2801 #if BUILD_FT2232_FTD2XX == 1
2802 FT_STATUS status;
2803
2804 status = FT_Close(ftdih);
2805 #elif BUILD_FT2232_LIBFTDI == 1
2806 ftdi_usb_close(&ftdic);
2807
2808 ftdi_deinit(&ftdic);
2809 #endif
2810
2811 free(ft2232_buffer);
2812 ft2232_buffer = NULL;
2813
2814 return ERROR_OK;
2815 }
2816
2817 static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2818 {
2819 char *cp;
2820 char buf[200];
2821 if (argc == 1)
2822 {
2823 ft2232_device_desc = strdup(args[0]);
2824 cp = strchr(ft2232_device_desc, 0);
2825 /* under Win32, the FTD2XX driver appends an "A" to the end
2826 * of the description, this examines the given desc
2827 * and creates the 'missing' _A or non_A variable. */
2828 if ((cp[-1] == 'A') && (cp[-2]==' ')) {
2829 /* it was, so make this the "A" version. */
2830 ft2232_device_desc_A = ft2232_device_desc;
2831 /* and *CREATE* the non-A version. */
2832 strcpy(buf, ft2232_device_desc);
2833 cp = strchr(buf, 0);
2834 cp[-2] = 0;
2835 ft2232_device_desc = strdup(buf);
2836 } else {
2837 /* <space > A not defined
2838 * so create it */
2839 sprintf(buf, "%s A", ft2232_device_desc);
2840 ft2232_device_desc_A = strdup(buf);
2841 }
2842 }
2843 else
2844 {
2845 LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
2846 }
2847
2848 return ERROR_OK;
2849 }
2850
2851 static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2852 {
2853 if (argc == 1)
2854 {
2855 ft2232_serial = strdup(args[0]);
2856 }
2857 else
2858 {
2859 LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
2860 }
2861
2862 return ERROR_OK;
2863 }
2864
2865 static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2866 {
2867 if (argc == 0)
2868 return ERROR_OK;
2869
2870 ft2232_layout = malloc(strlen(args[0]) + 1);
2871 strcpy(ft2232_layout, args[0]);
2872
2873 return ERROR_OK;
2874 }
2875
2876 static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2877 {
2878 if (argc > MAX_USB_IDS * 2)
2879 {
2880 LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
2881 "(maximum is %d pairs)", MAX_USB_IDS);
2882 argc = MAX_USB_IDS * 2;
2883 }
2884 if (argc < 2 || (argc & 1))
2885 {
2886 LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
2887 if (argc < 2)
2888 return ERROR_COMMAND_SYNTAX_ERROR;
2889 /* remove the incomplete trailing id */
2890 argc -= 1;
2891 }
2892
2893 int i;
2894 int retval = ERROR_OK;
2895 for (i = 0; i < argc; i += 2)
2896 {
2897 retval = parse_u16(args[i], &ft2232_vid[i >> 1]);
2898 if (ERROR_OK != retval)
2899 break;
2900 retval = parse_u16(args[i + 1], &ft2232_pid[i >> 1]);
2901 if (ERROR_OK != retval)
2902 break;
2903 }
2904
2905 /*
2906 * Explicitly terminate, in case there are multiples instances of
2907 * ft2232_vid_pid.
2908 */
2909 ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
2910
2911 return retval;
2912 }
2913
2914 static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
2915 {
2916 if (argc == 1)
2917 {
2918 ft2232_latency = atoi(args[0]);
2919 }
2920 else
2921 {
2922 LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
2923 }
2924
2925 return ERROR_OK;
2926 }
2927
2928 static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
2929 {
2930 int retval = 0;
2931
2932 /* 7 bits of either ones or zeros. */
2933 uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
2934
2935 while (num_cycles > 0)
2936 {
2937 /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
2938 * at most 7 bits per invocation. Here we invoke it potentially
2939 * several times.
2940 */
2941 int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
2942
2943 if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
2944 {
2945 if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
2946 retval = ERROR_JTAG_QUEUE_FAILED;
2947
2948 first_unsent = cmd;
2949 }
2950
2951 /* there are no state transitions in this code, so omit state tracking */
2952
2953 /* command "Clock Data to TMS/CS Pin (no Read)" */
2954 buffer_write(0x4b);
2955
2956 /* scan 7 bit */
2957 buffer_write(bitcount_per_command - 1);
2958
2959 /* TMS data bits are either all zeros or ones to stay in the current stable state */
2960 buffer_write(tms);
2961
2962 require_send = 1;
2963
2964 num_cycles -= bitcount_per_command;
2965 }
2966
2967 return retval;
2968 }
2969
2970 /* ---------------------------------------------------------------------
2971 * Support for IceBear JTAG adapter from Section5:
2972 * http://section5.ch/icebear
2973 *
2974 * Author: Sten, debian@sansys-electronic.com
2975 */
2976
2977 /* Icebear pin layout
2978 *
2979 * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
2980 * GND GND | 4 3| n.c.
2981 * ADBUS3 TMS | 6 5| ADBUS6 VCC
2982 * ADBUS0 TCK | 8 7| ADBUS7 (GND)
2983 * ADBUS4 nTRST |10 9| ACBUS0 (GND)
2984 * ADBUS1 TDI |12 11| ACBUS1 (GND)
2985 * ADBUS2 TDO |14 13| GND GND
2986 *
2987 * ADBUS0 O L TCK ACBUS0 GND
2988 * ADBUS1 O L TDI ACBUS1 GND
2989 * ADBUS2 I TDO ACBUS2 n.c.
2990 * ADBUS3 O H TMS ACBUS3 n.c.
2991 * ADBUS4 O H nTRST
2992 * ADBUS5 O H nSRST
2993 * ADBUS6 - VCC
2994 * ADBUS7 - GND
2995 */
2996 static int icebear_jtag_init(void) {
2997 uint8_t buf[3];
2998 uint32_t bytes_written;
2999
3000 low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
3001 low_output = 0x08; /* high: TMS; low: TCK TDI */
3002 nTRST = 0x10;
3003 nSRST = 0x20;
3004
3005 enum reset_types jtag_reset_config = jtag_get_reset_config();
3006 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
3007 low_direction &= ~nTRST; /* nTRST high impedance */
3008 }
3009 else {
3010 low_direction |= nTRST;
3011 low_output |= nTRST;
3012 }
3013
3014 low_direction |= nSRST;
3015 low_output |= nSRST;
3016
3017 /* initialize low byte for jtag */
3018 buf[0] = 0x80; /* command "set data bits low byte" */
3019 buf[1] = low_output;
3020 buf[2] = low_direction;
3021 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3022
3023 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3024 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
3025 return ERROR_JTAG_INIT_FAILED;
3026 }
3027
3028 high_output = 0x0;
3029 high_direction = 0x00;
3030
3031
3032 /* initialize high port */
3033 buf[0] = 0x82; /* command "set data bits high byte" */
3034 buf[1] = high_output; /* value */
3035 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
3036 LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
3037
3038 if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
3039 LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
3040 return ERROR_JTAG_INIT_FAILED;
3041 }
3042
3043 return ERROR_OK;
3044 }
3045
3046 static void icebear_jtag_reset(int trst, int srst) {
3047
3048 if (trst == 1) {
3049 low_direction |= nTRST;
3050 low_output &= ~nTRST;
3051 }
3052 else if (trst == 0) {
3053 enum reset_types jtag_reset_config = jtag_get_reset_config();
3054 if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
3055 low_direction &= ~nTRST;
3056 else
3057 low_output |= nTRST;
3058 }
3059
3060 if (srst == 1) {
3061 low_output &= ~nSRST;
3062 }
3063 else if (srst == 0) {
3064 low_output |= nSRST;
3065 }
3066
3067 /* command "set data bits low byte" */
3068 buffer_write(0x80);
3069 buffer_write(low_output);
3070 buffer_write(low_direction);
3071
3072 LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
3073 }

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)