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

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)