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