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