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