1 /***************************************************************************
2 * Driver for USB-JTAG, Altera USB-Blaster and compatibles *
3 * Original code from Kolja Waschk's USB-JTAG project *
4 * (http://www.ixo.de/info/usb_jtag/). *
5 * Some updates by Anthony Liu (2006). *
6 * Minor updates and cleanup by Catalin Patulea (2009). *
8 * Copyright (C) 2009 Catalin Patulea *
11 * Copyright (C) 2006 Kolja Waschk *
14 * Based on ft2232.c and bitbang.c, *
15 * Copyright (C) 2004,2006 by Dominic Rath *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
34 * The following information is originally from Kolja Waschk's USB-JTAG,
35 * where it was obtained by reverse engineering an Altera USB-Blaster.
36 * See http://www.ixo.de/info/usb_jtag/ for USB-Blaster block diagram and
37 * usb_jtag-20080705-1200.zip#usb_jtag/host/openocd for protocol.
39 * The same information is also on the UrJTAG mediawiki, with some additional
40 * notes on bits marked as "unknown" by usb_jtag.
41 * (http://sourceforge.net/apps/mediawiki/urjtag/index.php?
42 * title=Cable_Altera_USB-Blaster)
44 * USB-JTAG, Altera USB-Blaster and compatibles are typically implemented as
45 * an FTDIChip FT245 followed by a CPLD which handles a two-mode protocol:
51 * __|__________ _________
53 * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
54 * |_____________| |_________|
55 * __|__________ _|___________
57 * | 6 MHz XTAL | | 24 MHz Osc. |
58 * |_____________| |_____________|
60 * Protocol details are given in the code below.
62 * It is also possible to emulate this configuration using a single-chip USB
63 * controller like the Cypress FX2 (again, see usb_jtag for details).
74 /* project specific includes */
75 #include <jtag/interface.h>
76 #include <jtag/commands.h>
77 #include <helper/time_support.h>
86 #if (BUILD_USB_BLASTER_FTD2XX == 1 && BUILD_USB_BLASTER_LIBFTDI == 1)
87 #error "BUILD_USB_BLASTER_FTD2XX && BUILD_USB_BLASTER_LIBFTDI "
88 "are mutually exclusive"
89 #elif (BUILD_USB_BLASTER_FTD2XX != 1 && BUILD_USB_BLASTER_LIBFTDI != 1)
90 #error "BUILD_USB_BLASTER_FTD2XX || BUILD_USB_BLASTER_LIBFTDI must be chosen"
93 /* USB_BLASTER access library includes */
94 #if BUILD_USB_BLASTER_FTD2XX == 1
96 #elif BUILD_USB_BLASTER_LIBFTDI == 1
100 #include <sys/time.h>
103 static char *usb_blaster_device_desc
;
104 static uint16_t usb_blaster_vid
= 0x09fb; /* Altera */
105 static uint16_t usb_blaster_pid
= 0x6001; /* USB-Blaster */
107 /* last output byte in simple bit banging mode */
108 static uint8_t out_value
;
110 #if BUILD_USB_BLASTER_FTD2XX == 1
111 static FT_HANDLE ftdih
;
112 #elif BUILD_USB_BLASTER_LIBFTDI == 1
113 static struct ftdi_context ftdic
;
116 static int usb_blaster_buf_write(
117 uint8_t *buf
, int size
, uint32_t *bytes_written
)
119 #if BUILD_USB_BLASTER_FTD2XX == 1
121 DWORD dw_bytes_written
;
123 #ifdef _DEBUG_JTAG_IO_
124 LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf
[0], size
);
126 status
= FT_Write(ftdih
, buf
, size
, &dw_bytes_written
);
129 *bytes_written
= dw_bytes_written
;
130 LOG_ERROR("FT_Write returned: %lu", status
);
131 return ERROR_JTAG_DEVICE_ERROR
;
133 *bytes_written
= dw_bytes_written
;
135 #elif BUILD_USB_BLASTER_LIBFTDI == 1
137 #ifdef _DEBUG_JTAG_IO_
138 LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf
[0], size
);
140 retval
= ftdi_write_data(&ftdic
, buf
, size
);
144 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic
));
145 return ERROR_JTAG_DEVICE_ERROR
;
147 *bytes_written
= retval
;
153 usb_blaster_buf_read(uint8_t *buf
, unsigned size
, uint32_t *bytes_read
)
155 #if BUILD_USB_BLASTER_FTD2XX == 1
159 status
= FT_Read(ftdih
, buf
, size
, &dw_bytes_read
);
162 *bytes_read
= dw_bytes_read
;
163 LOG_ERROR("FT_Read returned: %lu", status
);
164 return ERROR_JTAG_DEVICE_ERROR
;
166 #ifdef _DEBUG_JTAG_IO_
167 LOG_DEBUG("usb_blaster_buf_read %02X (%lu)", buf
[0], dw_bytes_read
);
169 *bytes_read
= dw_bytes_read
;
172 #elif BUILD_USB_BLASTER_LIBFTDI == 1
177 while ((*bytes_read
< size
) && timeout
--)
179 retval
= ftdi_read_data(&ftdic
, buf
+ *bytes_read
,
184 LOG_ERROR("ftdi_read_data: %s",
185 ftdi_get_error_string(&ftdic
));
186 return ERROR_JTAG_DEVICE_ERROR
;
188 *bytes_read
+= retval
;
190 #ifdef _DEBUG_JTAG_IO_
191 LOG_DEBUG("usb_blaster_buf_read %02X (%d)", buf
[0], *bytes_read
);
197 /* The following code doesn't fully utilize the possibilities of the
198 * USB-Blaster. It writes one byte per JTAG pin state change at a time; it
199 * doesn't even try to buffer data up to the maximum packet size of 64 bytes.
201 * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
202 * bits (bidirectional) in a single USB packet. A header byte has to be sent as
203 * the first byte in a packet with the following meaning:
205 * Bit 7 (0x80): Must be set to indicate byte-shift mode.
206 * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
207 * Bit 5..0: Define the number N of following bytes
209 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
210 * set, it will afterwards return N bytes with TDO data read while clocking out
211 * the TDI data. LSB of the first byte after the header byte will appear first
215 /* Simple bit banging mode:
217 * Bit 7 (0x80): Must be zero (see byte-shift mode above)
218 * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
220 * Bit 5 (0x20): Output Enable/LED.
221 * Bit 4 (0x10): TDI Output.
222 * Bit 3 (0x08): nCS Output (not used in JTAG mode).
223 * Bit 2 (0x04): nCE Output (not used in JTAG mode).
224 * Bit 1 (0x02): TMS Output.
225 * Bit 0 (0x01): TCK Output.
227 * For transmitting a single data bit, you need to write two bytes. Up to 64
228 * bytes can be combined in a single USB packet (but this is not done in the
229 * code below). It isn't possible to read a data without transmitting data.
238 #define READ (1 << 6)
239 #define SHMODE (1 << 7)
240 #define OTHERS ((1 << 2) | (1 << 3) | (1 << 5))
242 #define READ_TDO (1 << 0)
244 static void usb_blaster_write_data(void)
246 uint32_t bytes_written
;
247 usb_blaster_buf_write(&out_value
, 1, &bytes_written
);
250 static int usb_blaster_read_data(void)
257 usb_blaster_write_data();
260 status
= usb_blaster_buf_read(buf
, 1, &bytes_read
);
264 return !!(buf
[0] & READ_TDO
);
267 static void usb_blaster_write(int tck
, int tms
, int tdi
)
269 #ifdef _DEBUG_JTAG_IO_
270 LOG_DEBUG("---- usb_blaster_write(%d,%d,%d)", tck
, tms
, tdi
);
272 out_value
&= ~(TCK
| TMS
| TDI
);
280 usb_blaster_write_data();
283 static int usb_blaster_speed(int speed
)
285 #if BUILD_USB_BLASTER_FTD2XX == 1
286 LOG_DEBUG("TODO: usb_blaster_speed() isn't implemented for libftd2xx!");
287 #elif BUILD_USB_BLASTER_LIBFTDI == 1
288 LOG_DEBUG("TODO: usb_blaster_speed() isn't optimally implemented!");
290 /* TODO: libftdi's ftdi_set_baudrate chokes on high rates, use lowlevel
291 * usb function instead! And additionally allow user to throttle.
293 if (ftdi_set_baudrate(&ftdic
, 3000000 / 4) < 0)
295 LOG_ERROR("Can't set baud rate to max: %s",
296 ftdi_get_error_string(&ftdic
));
297 return ERROR_JTAG_DEVICE_ERROR
;
304 static void usb_blaster_reset(int trst
, int srst
)
306 LOG_DEBUG("TODO: usb_blaster_reset(%d,%d) isn't implemented!",
310 static void usb_blaster_blink(int state
)
315 usb_blaster_write_data();
318 static struct bitbang_interface usb_blaster_bitbang
= {
319 .read
= usb_blaster_read_data
,
320 .write
= usb_blaster_write
,
321 .reset
= usb_blaster_reset
,
322 .blink
= usb_blaster_blink
,
325 static int usb_blaster_init(void)
327 uint8_t latency_timer
;
329 #if BUILD_USB_BLASTER_FTD2XX == 1
333 #if BUILD_USB_BLASTER_FTD2XX == 1
334 LOG_DEBUG("'usb_blaster' interface using FTD2XX");
335 #elif BUILD_USB_BLASTER_LIBFTDI == 1
336 LOG_DEBUG("'usb_blaster' interface using libftdi");
339 #if BUILD_USB_BLASTER_FTD2XX == 1
340 /* Open by device description */
341 if (usb_blaster_device_desc
== NULL
)
343 LOG_WARNING("no usb_blaster device description specified, "
344 "using default 'USB-Blaster'");
345 usb_blaster_device_desc
= "USB-Blaster";
349 /* Add non-standard Vid/Pid to the linux driver */
350 status
= FT_SetVIDPID(usb_blaster_vid
, usb_blaster_pid
);
353 LOG_WARNING("couldn't add %4.4x:%4.4x",
354 usb_blaster_vid
, usb_blaster_pid
);
358 status
= FT_OpenEx(usb_blaster_device_desc
, FT_OPEN_BY_DESCRIPTION
,
364 LOG_ERROR("unable to open ftdi device: %lu", status
);
365 status
= FT_ListDevices(&num_devices
, NULL
,
366 FT_LIST_NUMBER_ONLY
);
369 char **desc_array
= malloc(sizeof(char *)
370 * (num_devices
+ 1));
373 for (i
= 0; i
< num_devices
; i
++)
374 desc_array
[i
] = malloc(64);
375 desc_array
[num_devices
] = NULL
;
377 status
= FT_ListDevices(desc_array
, &num_devices
,
378 FT_LIST_ALL
| FT_OPEN_BY_DESCRIPTION
);
382 LOG_ERROR("ListDevices: %lu", num_devices
);
383 for (i
= 0; i
< num_devices
; i
++)
384 LOG_ERROR("%i: %s", i
, desc_array
[i
]);
387 for (i
= 0; i
< num_devices
; i
++)
393 printf("ListDevices: NONE\n");
395 return ERROR_JTAG_INIT_FAILED
;
398 status
= FT_SetLatencyTimer(ftdih
, 2);
401 LOG_ERROR("unable to set latency timer: %lu", status
);
402 return ERROR_JTAG_INIT_FAILED
;
405 status
= FT_GetLatencyTimer(ftdih
, &latency_timer
);
408 LOG_ERROR("unable to get latency timer: %lu", status
);
409 return ERROR_JTAG_INIT_FAILED
;
411 LOG_DEBUG("current latency timer: %i", latency_timer
);
413 status
= FT_SetBitMode(ftdih
, 0x00, 0);
416 LOG_ERROR("unable to disable bit i/o mode: %lu", status
);
417 return ERROR_JTAG_INIT_FAILED
;
419 #elif BUILD_USB_BLASTER_LIBFTDI == 1
420 if (ftdi_init(&ftdic
) < 0)
421 return ERROR_JTAG_INIT_FAILED
;
423 /* context, vendor id, product id */
424 if (ftdi_usb_open(&ftdic
, usb_blaster_vid
, usb_blaster_pid
) < 0)
426 LOG_ERROR("unable to open ftdi device: %s", ftdic
.error_str
);
427 return ERROR_JTAG_INIT_FAILED
;
430 if (ftdi_usb_reset(&ftdic
) < 0)
432 LOG_ERROR("unable to reset ftdi device");
433 return ERROR_JTAG_INIT_FAILED
;
436 if (ftdi_set_latency_timer(&ftdic
, 2) < 0)
438 LOG_ERROR("unable to set latency timer");
439 return ERROR_JTAG_INIT_FAILED
;
442 if (ftdi_get_latency_timer(&ftdic
, &latency_timer
) < 0)
444 LOG_ERROR("unable to get latency timer");
445 return ERROR_JTAG_INIT_FAILED
;
447 LOG_DEBUG("current latency timer: %u", latency_timer
);
449 ftdi_disable_bitbang(&ftdic
);
452 bitbang_interface
= &usb_blaster_bitbang
;
455 int retval
= jtag_get_speed(&jtag_speed_var
);
456 if (retval
!= ERROR_OK
)
458 usb_blaster_speed(jtag_speed_var
);
461 #if BUILD_USB_BLASTER_FTD2XX == 1
462 if ((status
= FT_Purge(ftdih
, FT_PURGE_RX
| FT_PURGE_TX
)) != FT_OK
)
464 LOG_ERROR("error purging ftd2xx device: %i", status
);
465 return ERROR_JTAG_INIT_FAILED
;
467 #elif BUILD_USB_BLASTER_LIBFTDI == 1
468 if (ftdi_usb_purge_buffers(&ftdic
) < 0)
470 LOG_ERROR("ftdi_purge_buffers: %s", ftdic
.error_str
);
471 return ERROR_JTAG_INIT_FAILED
;
479 static int usb_blaster_quit(void)
481 #if BUILD_USB_BLASTER_FTD2XX == 1
484 status
= FT_Close(ftdih
);
485 #elif BUILD_USB_BLASTER_LIBFTDI == 1
486 ftdi_usb_close(&ftdic
);
493 COMMAND_HANDLER(usb_blaster_handle_device_desc_command
)
496 usb_blaster_device_desc
= strdup(CMD_ARGV
[0]);
498 LOG_ERROR("require exactly one argument to "
499 "usb_blaster_device_desc <description>");
504 COMMAND_HANDLER(usb_blaster_handle_vid_pid_command
)
508 LOG_WARNING("ignoring extra IDs in usb_blaster_vid_pid "
509 "(maximum is 1 pair)");
514 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[0], usb_blaster_vid
);
515 COMMAND_PARSE_NUMBER(u16
, CMD_ARGV
[1], usb_blaster_pid
);
518 LOG_WARNING("incomplete usb_blaster_vid_pid configuration");
523 COMMAND_HANDLER(usb_blaster_handle_pin_command
)
527 const char * const pin_name
= CMD_ARGV
[0];
531 if (!strcmp(pin_name
, "pin6"))
533 else if (!strcmp(pin_name
, "pin8"))
537 LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
539 return ERROR_COMMAND_SYNTAX_ERROR
;
542 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], state
);
546 usb_blaster_write_data();
551 usb_blaster_write_data();
555 LOG_ERROR("%s: pin state must be 0 or 1", CMD_NAME
);
556 return ERROR_COMMAND_SYNTAX_ERROR
;
563 LOG_ERROR("%s takes exactly two arguments", CMD_NAME
);
564 return ERROR_COMMAND_SYNTAX_ERROR
;
568 static const struct command_registration usb_blaster_command_handlers
[] = {
570 .name
= "usb_blaster_device_desc",
571 .handler
= usb_blaster_handle_device_desc_command
,
572 .mode
= COMMAND_CONFIG
,
573 .help
= "set the USB device description of the USB-Blaster",
574 .usage
= "description-string",
577 .name
= "usb_blaster_vid_pid",
578 .handler
= usb_blaster_handle_vid_pid_command
,
579 .mode
= COMMAND_CONFIG
,
580 .help
= "the vendor ID and product ID of the USB-Blaster",
584 .name
= "usb_blaster",
585 .handler
= usb_blaster_handle_pin_command
,
587 .help
= "set pin state for the unused GPIO pins",
588 .usage
= "(pin6|pin8) (0|1)",
590 COMMAND_REGISTRATION_DONE
593 struct jtag_interface usb_blaster_interface
= {
594 .name
= "usb_blaster",
595 .commands
= usb_blaster_command_handlers
,
596 .supported
= DEBUG_CAP_TMS_SEQ
,
598 .execute_queue
= bitbang_execute_queue
,
600 .speed
= usb_blaster_speed
,
601 .init
= usb_blaster_init
,
602 .quit
= usb_blaster_quit
,