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

Linking to existing account procedure

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

SSH host keys fingerprints

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