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