91c4836538c2deaf9a236c6dbeb068d19c72af45
[openocd.git] / src / jtag / drivers / usb_blaster.c
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). *
7 * Speed updates by Ali Lown (2011). *
8 * *
9 * Copyright (C) 2011 Ali Lown *
10 * ali@lown.me.uk *
11 * *
12 * Copyright (C) 2009 Catalin Patulea *
13 * cat@vv.carleton.ca *
14 * *
15 * Copyright (C) 2006 Kolja Waschk *
16 * usbjtag@ixo.de *
17 * *
18 * Based on ft2232.c and bitbang.c, *
19 * Copyright (C) 2004,2006 by Dominic Rath *
20 * *
21 * This program is free software; you can redistribute it and/or modify *
22 * it under the terms of the GNU General Public License as published by *
23 * the Free Software Foundation; either version 2 of the License, or *
24 * (at your option) any later version. *
25 * *
26 * This program is distributed in the hope that it will be useful, *
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
29 * GNU General Public License for more details. *
30 * *
31 * You should have received a copy of the GNU General Public License *
32 * along with this program; if not, write to the *
33 * Free Software Foundation, Inc., *
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
35 ***************************************************************************/
36
37 /*
38 * The following information is originally from Kolja Waschk's USB-JTAG,
39 * where it was obtained by reverse engineering an Altera USB-Blaster.
40 * See http://www.ixo.de/info/usb_jtag/ for USB-Blaster block diagram and
41 * usb_jtag-20080705-1200.zip#usb_jtag/host/openocd for protocol.
42 *
43 * The same information is also on the UrJTAG mediawiki, with some additional
44 * notes on bits marked as "unknown" by usb_jtag.
45 * (http://sourceforge.net/apps/mediawiki/urjtag/index.php?
46 * title=Cable_Altera_USB-Blaster)
47 *
48 * USB-JTAG, Altera USB-Blaster and compatibles are typically implemented as
49 * an FTDIChip FT245 followed by a CPLD which handles a two-mode protocol:
50 *
51 * _________
52 * | |
53 * | AT93C46 |
54 * |_________|
55 * __|__________ _________
56 * | | | |
57 * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
58 * |_____________| |_________|
59 * __|__________ _|___________
60 * | | | |
61 * | 6 MHz XTAL | | 24 MHz Osc. |
62 * |_____________| |_____________|
63 *
64 * Protocol details are given in the code below.
65 *
66 * It is also possible to emulate this configuration using a single-chip USB
67 * controller like the Cypress FX2 (again, see usb_jtag for details).
68 */
69 #ifdef HAVE_CONFIG_H
70 #include "config.h"
71 #endif
72
73 #if IS_CYGWIN == 1
74 #include "windows.h"
75 #undef LOG_ERROR
76 #endif
77
78 /* project specific includes */
79 #include <jtag/interface.h>
80 #include <jtag/commands.h>
81 #include <helper/time_support.h>
82
83 /* system includes */
84 #include <string.h>
85 #include <stdlib.h>
86 #include <unistd.h>
87
88 #include "bitbang.h"
89
90 #if (BUILD_USB_BLASTER_FTD2XX == 1 && BUILD_USB_BLASTER_LIBFTDI == 1)
91 #error "BUILD_USB_BLASTER_FTD2XX && BUILD_USB_BLASTER_LIBFTDI "
92 "are mutually exclusive"
93 #elif (BUILD_USB_BLASTER_FTD2XX != 1 && BUILD_USB_BLASTER_LIBFTDI != 1)
94 #error "BUILD_USB_BLASTER_FTD2XX || BUILD_USB_BLASTER_LIBFTDI must be chosen"
95 #endif
96
97 /* USB_BLASTER access library includes */
98 #if BUILD_USB_BLASTER_FTD2XX == 1
99 #include <ftd2xx.h>
100 #include "ftd2xx_common.h"
101 #elif BUILD_USB_BLASTER_LIBFTDI == 1
102 #include <ftdi.h>
103 #endif
104
105 #include <sys/time.h>
106 #include <time.h>
107
108 static char *usb_blaster_device_desc;
109 static uint16_t usb_blaster_vid = 0x09fb; /* Altera */
110 static uint16_t usb_blaster_pid = 0x6001; /* USB-Blaster */
111
112 /* last output byte in simple bit banging (legacy) mode */
113 static uint8_t out_value;
114 /* global output buffer for bit banging */
115 #define BUF_LEN 64 /* Size of EP1 */
116 static uint8_t out_buffer[BUF_LEN];
117 static uint16_t out_count;
118
119 #if BUILD_USB_BLASTER_FTD2XX == 1
120 static FT_HANDLE ftdih;
121 #elif BUILD_USB_BLASTER_LIBFTDI == 1
122 static struct ftdi_context ftdic;
123 #endif
124
125 static int usb_blaster_buf_write(
126 uint8_t *buf, int size, uint32_t *bytes_written)
127 {
128 #if BUILD_USB_BLASTER_FTD2XX == 1
129 FT_STATUS status;
130 DWORD dw_bytes_written;
131
132 #ifdef _DEBUG_JTAG_IO_
133 LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf[0], size);
134 #endif
135 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
136 if (status != FT_OK) {
137 *bytes_written = dw_bytes_written;
138 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
139 return ERROR_JTAG_DEVICE_ERROR;
140 }
141 *bytes_written = dw_bytes_written;
142 return ERROR_OK;
143 #elif BUILD_USB_BLASTER_LIBFTDI == 1
144 int retval;
145 #ifdef _DEBUG_JTAG_IO_
146 LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf[0], size);
147 #endif
148 retval = ftdi_write_data(&ftdic, buf, size);
149 if (retval < 0) {
150 *bytes_written = 0;
151 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
152 return ERROR_JTAG_DEVICE_ERROR;
153 }
154 *bytes_written = retval;
155 return ERROR_OK;
156 #endif
157 }
158
159 static int usb_blaster_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read)
160 {
161 #if BUILD_USB_BLASTER_FTD2XX == 1
162 DWORD dw_bytes_read;
163 FT_STATUS status;
164
165 status = FT_Read(ftdih, buf, size, &dw_bytes_read);
166 if (status != FT_OK) {
167 *bytes_read = dw_bytes_read;
168 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
169 return ERROR_JTAG_DEVICE_ERROR;
170 }
171 #ifdef _DEBUG_JTAG_IO_
172 LOG_DEBUG("usb_blaster_buf_read %02X (%" PRIu32 ")", buf[0], dw_bytes_read);
173 #endif
174 *bytes_read = dw_bytes_read;
175 return ERROR_OK;
176
177 #elif BUILD_USB_BLASTER_LIBFTDI == 1
178 int retval;
179 int timeout = 100;
180
181 *bytes_read = 0;
182 while ((*bytes_read < size) && timeout--) {
183 retval = ftdi_read_data(&ftdic, buf + *bytes_read,
184 size - *bytes_read);
185 if (retval < 0) {
186 *bytes_read = 0;
187 LOG_ERROR("ftdi_read_data: %s",
188 ftdi_get_error_string(&ftdic));
189 return ERROR_JTAG_DEVICE_ERROR;
190 }
191 *bytes_read += retval;
192 }
193 #ifdef _DEBUG_JTAG_IO_
194 LOG_DEBUG("usb_blaster_buf_read %02X (%d)", buf[0], *bytes_read);
195 #endif
196 return ERROR_OK;
197 #endif
198 }
199
200 /* The following code doesn't fully utilize the possibilities of the
201 * USB-Blaster. It only buffers data up to the maximum packet size of 64 bytes.
202 *
203 * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
204 * bits (bidirectional) in a single USB packet. A header byte has to be sent as
205 * the first byte in a packet with the following meaning:
206 *
207 * Bit 7 (0x80): Must be set to indicate byte-shift mode.
208 * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
209 * Bit 5..0: Define the number N of following bytes
210 *
211 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
212 * set, it will afterwards return N bytes with TDO data read while clocking out
213 * the TDI data. LSB of the first byte after the header byte will appear first
214 * on TDI.
215 */
216
217 /* Simple bit banging mode:
218 *
219 * Bit 7 (0x80): Must be zero (see byte-shift mode above)
220 * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
221 * in return.
222 * Bit 5 (0x20): Output Enable/LED.
223 * Bit 4 (0x10): TDI Output.
224 * Bit 3 (0x08): nCS Output (not used in JTAG mode).
225 * Bit 2 (0x04): nCE Output (not used in JTAG mode).
226 * Bit 1 (0x02): TMS Output.
227 * Bit 0 (0x01): TCK Output.
228 *
229 * For transmitting a single data bit, you need to write two bytes. Up to 64
230 * bytes can be combined in a single USB packet.
231 * It isn't possible to read a data without transmitting data.
232 */
233
234 #define TCK (1 << 0)
235 #define TMS (1 << 1)
236 #define NCE (1 << 2)
237 #define NCS (1 << 3)
238 #define TDI (1 << 4)
239 #define LED (1 << 5)
240 #define READ (1 << 6)
241 #define SHMODE (1 << 7)
242 #define OTHERS ((1 << 2) | (1 << 3) | (1 << 5))
243
244 #define READ_TDO (1 << 0)
245
246 static void usb_blaster_write_databuffer(uint8_t *buf, uint16_t len)
247 {
248 uint32_t bytes_written;
249 usb_blaster_buf_write(buf, len, &bytes_written);
250 out_count = 0;
251 #ifdef _DEBUG_JTAG_IO_
252 LOG_DEBUG("---- WROTE %d", bytes_written);
253 #endif
254 }
255
256 static void usb_blaster_addtowritebuffer(uint8_t value, bool forcewrite)
257 {
258 out_buffer[out_count] = value;
259 out_count += 1;
260 if (out_count == BUF_LEN || forcewrite)
261 usb_blaster_write_databuffer(out_buffer, out_count);
262 }
263
264 static int usb_blaster_read_data(void)
265 {
266 int status;
267 uint8_t buf[1];
268 uint32_t bytes_read;
269
270 if (out_count > 0)
271 usb_blaster_write_databuffer(out_buffer, out_count);
272
273 out_value |= READ;
274 usb_blaster_addtowritebuffer(out_value, true);
275 out_value &= ~READ;
276
277 status = usb_blaster_buf_read(buf, 1, &bytes_read);
278 if (status < 0)
279 return 0;
280
281 return !!(buf[0] & READ_TDO);
282 }
283
284 static void usb_blaster_write(int tck, int tms, int tdi)
285 {
286 #ifdef _DEBUG_JTAG_IO_
287 LOG_DEBUG("---- usb_blaster_write(%d,%d,%d)", tck, tms, tdi);
288 #endif
289 out_value &= ~(TCK | TMS | TDI);
290 if (tck)
291 out_value |= TCK;
292 if (tms)
293 out_value |= TMS;
294 if (tdi)
295 out_value |= TDI;
296
297 usb_blaster_addtowritebuffer(out_value, false);
298 }
299
300 static void usb_blaster_reset(int trst, int srst)
301 {
302 LOG_DEBUG("TODO: usb_blaster_reset(%d,%d) isn't implemented!",
303 trst, srst);
304 }
305
306 static void usb_blaster_blink(int state)
307 {
308 out_value = 0x00;
309 if (state)
310 out_value |= LED;
311
312 usb_blaster_addtowritebuffer(out_value, true);
313 }
314
315 static struct bitbang_interface usb_blaster_bitbang = {
316 .read = usb_blaster_read_data,
317 .write = usb_blaster_write,
318 .reset = usb_blaster_reset,
319 .blink = usb_blaster_blink,
320 };
321
322 static int usb_blaster_init(void)
323 {
324 uint8_t latency_timer;
325
326 #if BUILD_USB_BLASTER_FTD2XX == 1
327 FT_STATUS status;
328 #endif
329
330 #if BUILD_USB_BLASTER_FTD2XX == 1
331 LOG_DEBUG("'usb_blaster' interface using FTD2XX");
332 #elif BUILD_USB_BLASTER_LIBFTDI == 1
333 LOG_DEBUG("'usb_blaster' interface using libftdi");
334 #endif
335
336 #if BUILD_USB_BLASTER_FTD2XX == 1
337 /* Open by device description */
338 if (usb_blaster_device_desc == NULL) {
339 LOG_WARNING("no usb_blaster device description specified, "
340 "using default 'USB-Blaster'");
341 usb_blaster_device_desc = "USB-Blaster";
342 }
343
344 #if IS_WIN32 == 0
345 /* Add non-standard Vid/Pid to the linux driver */
346 status = FT_SetVIDPID(usb_blaster_vid, usb_blaster_pid);
347 if (status != FT_OK) {
348 LOG_WARNING("couldn't add %4.4x:%4.4x",
349 usb_blaster_vid, usb_blaster_pid);
350 }
351 #endif
352
353 status = FT_OpenEx(usb_blaster_device_desc, FT_OPEN_BY_DESCRIPTION,
354 &ftdih);
355 if (status != FT_OK) {
356 DWORD num_devices;
357
358 LOG_ERROR("unable to open ftdi device: %s",
359 ftd2xx_status_string(status));
360 status = FT_ListDevices(&num_devices, NULL,
361 FT_LIST_NUMBER_ONLY);
362 if (status == FT_OK) {
363 char **desc_array = malloc(sizeof(char *)
364 * (num_devices + 1));
365 unsigned int i;
366
367 for (i = 0; i < num_devices; i++)
368 desc_array[i] = malloc(64);
369 desc_array[num_devices] = NULL;
370
371 status = FT_ListDevices(desc_array, &num_devices,
372 FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
373
374 if (status == FT_OK) {
375 LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
376 for (i = 0; i < num_devices; i++)
377 LOG_ERROR("%i: %s", i, desc_array[i]);
378 }
379
380 for (i = 0; i < num_devices; i++)
381 free(desc_array[i]);
382 free(desc_array);
383 } else
384 printf("ListDevices: NONE\n");
385 return ERROR_JTAG_INIT_FAILED;
386 }
387
388 status = FT_SetLatencyTimer(ftdih, 2);
389 if (status != FT_OK) {
390 LOG_ERROR("unable to set latency timer: %s",
391 ftd2xx_status_string(status));
392 return ERROR_JTAG_INIT_FAILED;
393 }
394
395 status = FT_GetLatencyTimer(ftdih, &latency_timer);
396 if (status != FT_OK) {
397 LOG_ERROR("unable to get latency timer: %s",
398 ftd2xx_status_string(status));
399 return ERROR_JTAG_INIT_FAILED;
400 }
401 LOG_DEBUG("current latency timer: %i", latency_timer);
402
403 status = FT_SetBitMode(ftdih, 0x00, 0);
404 if (status != FT_OK) {
405 LOG_ERROR("unable to disable bit i/o mode: %s",
406 ftd2xx_status_string(status));
407 return ERROR_JTAG_INIT_FAILED;
408 }
409 #elif BUILD_USB_BLASTER_LIBFTDI == 1
410 if (ftdi_init(&ftdic) < 0)
411 return ERROR_JTAG_INIT_FAILED;
412
413 /* context, vendor id, product id */
414 if (ftdi_usb_open(&ftdic, usb_blaster_vid, usb_blaster_pid) < 0) {
415 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
416 return ERROR_JTAG_INIT_FAILED;
417 }
418
419 if (ftdi_usb_reset(&ftdic) < 0) {
420 LOG_ERROR("unable to reset ftdi device");
421 return ERROR_JTAG_INIT_FAILED;
422 }
423
424 if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
425 LOG_ERROR("unable to set latency timer");
426 return ERROR_JTAG_INIT_FAILED;
427 }
428
429 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
430 LOG_ERROR("unable to get latency timer");
431 return ERROR_JTAG_INIT_FAILED;
432 }
433 LOG_DEBUG("current latency timer: %u", latency_timer);
434
435 ftdi_disable_bitbang(&ftdic);
436 #endif
437
438 bitbang_interface = &usb_blaster_bitbang;
439
440 #if 0
441 #if BUILD_USB_BLASTER_FTD2XX == 1
442 status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
443 if (status != FT_OK) {
444 LOG_ERROR("error purging ftd2xx device: %i", status);
445 return ERROR_JTAG_INIT_FAILED;
446 }
447 #elif BUILD_USB_BLASTER_LIBFTDI == 1
448 if (ftdi_usb_purge_buffers(&ftdic) < 0) {
449 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
450 return ERROR_JTAG_INIT_FAILED;
451 }
452 #endif
453 #endif
454
455 return ERROR_OK;
456 }
457
458 static int usb_blaster_quit(void)
459 {
460 if (out_count > 0)
461 usb_blaster_write_databuffer(out_buffer, out_count);
462
463 #if BUILD_USB_BLASTER_FTD2XX == 1
464 FT_STATUS status;
465
466 status = FT_Close(ftdih);
467 #elif BUILD_USB_BLASTER_LIBFTDI == 1
468 ftdi_usb_close(&ftdic);
469 ftdi_deinit(&ftdic);
470 #endif
471
472 return ERROR_OK;
473 }
474
475 COMMAND_HANDLER(usb_blaster_handle_device_desc_command)
476 {
477 if (CMD_ARGC == 1)
478 usb_blaster_device_desc = strdup(CMD_ARGV[0]);
479 else
480 LOG_ERROR("require exactly one argument to "
481 "usb_blaster_device_desc <description>");
482
483 return ERROR_OK;
484 }
485
486 COMMAND_HANDLER(usb_blaster_handle_vid_pid_command)
487 {
488 if (CMD_ARGC > 2) {
489 LOG_WARNING("ignoring extra IDs in usb_blaster_vid_pid "
490 "(maximum is 1 pair)");
491 CMD_ARGC = 2;
492 }
493 if (CMD_ARGC == 2) {
494 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], usb_blaster_vid);
495 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], usb_blaster_pid);
496 } else
497 LOG_WARNING("incomplete usb_blaster_vid_pid configuration");
498
499 return ERROR_OK;
500 }
501
502 COMMAND_HANDLER(usb_blaster_handle_pin_command)
503 {
504 if (CMD_ARGC == 2) {
505 const char *const pin_name = CMD_ARGV[0];
506 uint8_t mask;
507 unsigned int state;
508
509 if (!strcmp(pin_name, "pin6"))
510 mask = NCE;
511 else if (!strcmp(pin_name, "pin8"))
512 mask = NCS;
513 else {
514 LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
515 CMD_NAME);
516 return ERROR_COMMAND_SYNTAX_ERROR;
517 }
518
519 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], state);
520 if (state == 0) {
521 out_value &= ~mask;
522 usb_blaster_addtowritebuffer(out_value, true);
523 } else if (state == 1) {
524 out_value |= mask;
525 usb_blaster_addtowritebuffer(out_value, true);
526 } else {
527 LOG_ERROR("%s: pin state must be 0 or 1", CMD_NAME);
528 return ERROR_COMMAND_SYNTAX_ERROR;
529 }
530
531 return ERROR_OK;
532 } else {
533 LOG_ERROR("%s takes exactly two arguments", CMD_NAME);
534 return ERROR_COMMAND_SYNTAX_ERROR;
535 }
536 }
537
538 static const struct command_registration usb_blaster_command_handlers[] = {
539 {
540 .name = "usb_blaster_device_desc",
541 .handler = usb_blaster_handle_device_desc_command,
542 .mode = COMMAND_CONFIG,
543 .help = "set the USB device description of the USB-Blaster",
544 .usage = "description-string",
545 },
546 {
547 .name = "usb_blaster_vid_pid",
548 .handler = usb_blaster_handle_vid_pid_command,
549 .mode = COMMAND_CONFIG,
550 .help = "the vendor ID and product ID of the USB-Blaster",
551 .usage = "vid pid",
552 },
553 {
554 .name = "usb_blaster",
555 .handler = usb_blaster_handle_pin_command,
556 .mode = COMMAND_ANY,
557 .help = "set pin state for the unused GPIO pins",
558 .usage = "(pin6|pin8) (0|1)",
559 },
560 COMMAND_REGISTRATION_DONE
561 };
562
563 struct jtag_interface usb_blaster_interface = {
564 .name = "usb_blaster",
565 .commands = usb_blaster_command_handlers,
566 .supported = DEBUG_CAP_TMS_SEQ,
567
568 .execute_queue = bitbang_execute_queue,
569
570 .init = usb_blaster_init,
571 .quit = usb_blaster_quit,
572 };