jtag: getting the JTAG speed can fail
[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 * *
8 * Copyright (C) 2009 Catalin Patulea *
9 * cat@vv.carleton.ca *
10 * *
11 * Copyright (C) 2006 Kolja Waschk *
12 * usbjtag@ixo.de *
13 * *
14 * Based on ft2232.c and bitbang.c, *
15 * Copyright (C) 2004,2006 by Dominic Rath *
16 * *
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. *
21 * *
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. *
26 * *
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 ***************************************************************************/
32
33 /*
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.
38 *
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)
43 *
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:
46 *
47 * _________
48 * | |
49 * | AT93C46 |
50 * |_________|
51 * __|__________ _________
52 * | | | |
53 * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
54 * |_____________| |_________|
55 * __|__________ _|___________
56 * | | | |
57 * | 6 MHz XTAL | | 24 MHz Osc. |
58 * |_____________| |_____________|
59 *
60 * Protocol details are given in the code below.
61 *
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).
64 */
65 #ifdef HAVE_CONFIG_H
66 #include "config.h"
67 #endif
68
69 #if IS_CYGWIN == 1
70 #include "windows.h"
71 #undef LOG_ERROR
72 #endif
73
74 /* project specific includes */
75 #include <jtag/interface.h>
76 #include <jtag/commands.h>
77 #include <helper/time_support.h>
78
79 /* system includes */
80 #include <string.h>
81 #include <stdlib.h>
82 #include <unistd.h>
83
84 #include "bitbang.h"
85
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"
91 #endif
92
93 /* USB_BLASTER access library includes */
94 #if BUILD_USB_BLASTER_FTD2XX == 1
95 #include <ftd2xx.h>
96 #elif BUILD_USB_BLASTER_LIBFTDI == 1
97 #include <ftdi.h>
98 #endif
99
100 #include <sys/time.h>
101 #include <time.h>
102
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 */
106
107 /* last output byte in simple bit banging mode */
108 static uint8_t out_value;
109
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;
114 #endif
115
116 static int usb_blaster_buf_write(
117 uint8_t *buf, int size, uint32_t *bytes_written)
118 {
119 #if BUILD_USB_BLASTER_FTD2XX == 1
120 FT_STATUS status;
121 DWORD dw_bytes_written;
122
123 #ifdef _DEBUG_JTAG_IO_
124 LOG_DEBUG("usb_blaster_buf_write %02X (%d)\n", buf[0], size);
125 #endif
126 status = FT_Write(ftdih, buf, size, &dw_bytes_written);
127 if (status != FT_OK)
128 {
129 *bytes_written = dw_bytes_written;
130 LOG_ERROR("FT_Write returned: %lu", status);
131 return ERROR_JTAG_DEVICE_ERROR;
132 }
133 *bytes_written = dw_bytes_written;
134 return ERROR_OK;
135 #elif BUILD_USB_BLASTER_LIBFTDI == 1
136 int retval;
137 #ifdef _DEBUG_JTAG_IO_
138 LOG_DEBUG("usb_blaster_buf_write %02X (%d)\n", buf[0], size);
139 #endif
140 retval = ftdi_write_data(&ftdic, buf, size);
141 if (retval < 0)
142 {
143 *bytes_written = 0;
144 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
145 return ERROR_JTAG_DEVICE_ERROR;
146 }
147 *bytes_written = retval;
148 return ERROR_OK;
149 #endif
150 }
151
152 static int
153 usb_blaster_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read)
154 {
155 #if BUILD_USB_BLASTER_FTD2XX == 1
156 DWORD dw_bytes_read;
157 FT_STATUS status;
158
159 status = FT_Read(ftdih, buf, size, &dw_bytes_read);
160 if (status != FT_OK)
161 {
162 *bytes_read = dw_bytes_read;
163 LOG_ERROR("FT_Read returned: %lu", status);
164 return ERROR_JTAG_DEVICE_ERROR;
165 }
166 #ifdef _DEBUG_JTAG_IO_
167 LOG_DEBUG("usb_blaster_buf_read %02X (%lu)\n", buf[0], dw_bytes_read);
168 #endif
169 *bytes_read = dw_bytes_read;
170 return ERROR_OK;
171
172 #elif BUILD_USB_BLASTER_LIBFTDI == 1
173 int retval;
174 int timeout = 100;
175
176 *bytes_read = 0;
177 while ((*bytes_read < size) && timeout--)
178 {
179 retval = ftdi_read_data(&ftdic, buf + *bytes_read,
180 size - *bytes_read);
181 if (retval < 0)
182 {
183 *bytes_read = 0;
184 LOG_ERROR("ftdi_read_data: %s",
185 ftdi_get_error_string(&ftdic));
186 return ERROR_JTAG_DEVICE_ERROR;
187 }
188 *bytes_read += retval;
189 }
190 #ifdef _DEBUG_JTAG_IO_
191 LOG_DEBUG("usb_blaster_buf_read %02X (%d)\n", buf[0], *bytes_read);
192 #endif
193 return ERROR_OK;
194 #endif
195 }
196
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.
200 *
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:
204 *
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
208 *
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
212 * on TDI.
213 */
214
215 /* Simple bit banging mode:
216 *
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
219 * in return.
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.
226 *
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.
230 */
231
232 #define TCK (1 << 0)
233 #define TMS (1 << 1)
234 #define NCE (1 << 2)
235 #define NCS (1 << 3)
236 #define TDI (1 << 4)
237 #define LED (1 << 5)
238 #define READ (1 << 6)
239 #define SHMODE (1 << 7)
240 #define OTHERS ((1 << 2) | (1 << 3) | (1 << 5))
241
242 #define READ_TDO (1 << 0)
243
244 static void usb_blaster_write_data(void)
245 {
246 uint32_t bytes_written;
247 usb_blaster_buf_write(&out_value, 1, &bytes_written);
248 }
249
250 static int usb_blaster_read_data(void)
251 {
252 int status;
253 uint8_t buf[1];
254 uint32_t bytes_read;
255
256 out_value |= READ;
257 usb_blaster_write_data();
258 out_value &= ~READ;
259
260 status = usb_blaster_buf_read(buf, 1, &bytes_read);
261 if (status < 0)
262 return 0;
263
264 return !!(buf[0] & READ_TDO);
265 }
266
267 static void usb_blaster_write(int tck, int tms, int tdi)
268 {
269 #ifdef _DEBUG_JTAG_IO_
270 LOG_DEBUG("---- usb_blaster_write(%d,%d,%d)\n", tck, tms, tdi);
271 #endif
272 out_value &= ~(TCK | TMS | TDI);
273 if (tck)
274 out_value |= TCK;
275 if (tms)
276 out_value |= TMS;
277 if (tdi)
278 out_value |= TDI;
279
280 usb_blaster_write_data();
281 }
282
283 static int usb_blaster_speed(int speed)
284 {
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!");
289
290 /* TODO: libftdi's ftdi_set_baudrate chokes on high rates, use lowlevel
291 * usb function instead! And additionally allow user to throttle.
292 */
293 if (ftdi_set_baudrate(&ftdic, 3000000 / 4) < 0)
294 {
295 LOG_ERROR("Can't set baud rate to max: %s",
296 ftdi_get_error_string(&ftdic));
297 return ERROR_JTAG_DEVICE_ERROR;
298 };
299 #endif
300
301 return ERROR_OK;
302 }
303
304 static void usb_blaster_reset(int trst, int srst)
305 {
306 LOG_DEBUG("TODO: usb_blaster_reset(%d,%d) isn't implemented!",
307 trst, srst);
308 }
309
310 static struct bitbang_interface usb_blaster_bitbang = {
311 .read = usb_blaster_read_data,
312 .write = usb_blaster_write,
313 .reset = usb_blaster_reset,
314 };
315
316 static int usb_blaster_init(void)
317 {
318 uint8_t latency_timer;
319
320 #if BUILD_USB_BLASTER_FTD2XX == 1
321 FT_STATUS status;
322 #endif
323
324 #if BUILD_USB_BLASTER_FTD2XX == 1
325 LOG_DEBUG("'usb_blaster' interface using FTD2XX");
326 #elif BUILD_USB_BLASTER_LIBFTDI == 1
327 LOG_DEBUG("'usb_blaster' interface using libftdi");
328 #endif
329
330 #if BUILD_USB_BLASTER_FTD2XX == 1
331 /* Open by device description */
332 if (usb_blaster_device_desc == NULL)
333 {
334 LOG_WARNING("no usb_blaster device description specified, "
335 "using default 'USB-Blaster'");
336 usb_blaster_device_desc = "USB-Blaster";
337 }
338
339 #if IS_WIN32 == 0
340 /* Add non-standard Vid/Pid to the linux driver */
341 status = FT_SetVIDPID(usb_blaster_vid, usb_blaster_pid);
342 if (status != FT_OK)
343 {
344 LOG_WARNING("couldn't add %4.4x:%4.4x",
345 usb_blaster_vid, usb_blaster_pid);
346 }
347 #endif
348
349 status = FT_OpenEx(usb_blaster_device_desc, FT_OPEN_BY_DESCRIPTION,
350 &ftdih);
351 if (status != FT_OK)
352 {
353 DWORD num_devices;
354
355 LOG_ERROR("unable to open ftdi device: %lu", status);
356 status = FT_ListDevices(&num_devices, NULL,
357 FT_LIST_NUMBER_ONLY);
358 if (status == FT_OK)
359 {
360 char **desc_array = malloc(sizeof(char *)
361 * (num_devices + 1));
362 unsigned int i;
363
364 for (i = 0; i < num_devices; i++)
365 desc_array[i] = malloc(64);
366 desc_array[num_devices] = NULL;
367
368 status = FT_ListDevices(desc_array, &num_devices,
369 FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
370
371 if (status == FT_OK)
372 {
373 LOG_ERROR("ListDevices: %lu\n", num_devices);
374 for (i = 0; i < num_devices; i++)
375 LOG_ERROR("%i: %s", i, desc_array[i]);
376 }
377
378 for (i = 0; i < num_devices; i++)
379 free(desc_array[i]);
380 free(desc_array);
381 }
382 else
383 {
384 printf("ListDevices: NONE\n");
385 }
386 return ERROR_JTAG_INIT_FAILED;
387 }
388
389 status = FT_SetLatencyTimer(ftdih, 2);
390 if (status != FT_OK)
391 {
392 LOG_ERROR("unable to set latency timer: %lu", status);
393 return ERROR_JTAG_INIT_FAILED;
394 }
395
396 status = FT_GetLatencyTimer(ftdih, &latency_timer);
397 if (status != FT_OK)
398 {
399 LOG_ERROR("unable to get latency timer: %lu", status);
400 return ERROR_JTAG_INIT_FAILED;
401 }
402 LOG_DEBUG("current latency timer: %i", latency_timer);
403
404 status = FT_SetBitMode(ftdih, 0x00, 0);
405 if (status != FT_OK)
406 {
407 LOG_ERROR("unable to disable bit i/o mode: %lu", status);
408 return ERROR_JTAG_INIT_FAILED;
409 }
410 #elif BUILD_USB_BLASTER_LIBFTDI == 1
411 if (ftdi_init(&ftdic) < 0)
412 return ERROR_JTAG_INIT_FAILED;
413
414 /* context, vendor id, product id */
415 if (ftdi_usb_open(&ftdic, usb_blaster_vid, usb_blaster_pid) < 0)
416 {
417 LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
418 return ERROR_JTAG_INIT_FAILED;
419 }
420
421 if (ftdi_usb_reset(&ftdic) < 0)
422 {
423 LOG_ERROR("unable to reset ftdi device");
424 return ERROR_JTAG_INIT_FAILED;
425 }
426
427 if (ftdi_set_latency_timer(&ftdic, 2) < 0)
428 {
429 LOG_ERROR("unable to set latency timer");
430 return ERROR_JTAG_INIT_FAILED;
431 }
432
433 if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
434 {
435 LOG_ERROR("unable to get latency timer");
436 return ERROR_JTAG_INIT_FAILED;
437 }
438 LOG_DEBUG("current latency timer: %u", latency_timer);
439
440 ftdi_disable_bitbang(&ftdic);
441 #endif
442
443 bitbang_interface = &usb_blaster_bitbang;
444
445 int jtag_speed_var;
446 int retval = jtag_get_speed(&jtag_speed_var);
447 if (retval != ERROR_OK)
448 return retval;
449 usb_blaster_speed(jtag_speed_var);
450
451 #if 0
452 #if BUILD_USB_BLASTER_FTD2XX == 1
453 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
454 {
455 LOG_ERROR("error purging ftd2xx device: %i", status);
456 return ERROR_JTAG_INIT_FAILED;
457 }
458 #elif BUILD_USB_BLASTER_LIBFTDI == 1
459 if (ftdi_usb_purge_buffers(&ftdic) < 0)
460 {
461 LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
462 return ERROR_JTAG_INIT_FAILED;
463 }
464 #endif
465 #endif
466
467 return ERROR_OK;
468 }
469
470 static int usb_blaster_quit(void)
471 {
472 #if BUILD_USB_BLASTER_FTD2XX == 1
473 FT_STATUS status;
474
475 status = FT_Close(ftdih);
476 #elif BUILD_USB_BLASTER_LIBFTDI == 1
477 ftdi_usb_close(&ftdic);
478 ftdi_deinit(&ftdic);
479 #endif
480
481 return ERROR_OK;
482 }
483
484 COMMAND_HANDLER(usb_blaster_handle_device_desc_command)
485 {
486 if (CMD_ARGC == 1)
487 usb_blaster_device_desc = strdup(CMD_ARGV[0]);
488 else
489 LOG_ERROR("require exactly one argument to "
490 "usb_blaster_device_desc <description>");
491
492 return ERROR_OK;
493 }
494
495 COMMAND_HANDLER(usb_blaster_handle_vid_pid_command)
496 {
497 if (CMD_ARGC > 2)
498 {
499 LOG_WARNING("ignoring extra IDs in usb_blaster_vid_pid "
500 "(maximum is 1 pair)");
501 CMD_ARGC = 2;
502 }
503 if (CMD_ARGC == 2)
504 {
505 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], usb_blaster_vid);
506 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], usb_blaster_pid);
507 }
508 else
509 LOG_WARNING("incomplete usb_blaster_vid_pid configuration");
510
511 return ERROR_OK;
512 }
513
514 COMMAND_HANDLER(usb_blaster_handle_pin_command)
515 {
516 if (CMD_ARGC == 2)
517 {
518 const char * const pin_name = CMD_ARGV[0];
519 uint8_t mask;
520 unsigned int state;
521
522 if (!strcmp(pin_name, "pin6"))
523 mask = NCE;
524 else if (!strcmp(pin_name, "pin8"))
525 mask = NCS;
526 else
527 {
528 LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
529 CMD_NAME);
530 return ERROR_COMMAND_SYNTAX_ERROR;
531 }
532
533 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], state);
534 if (state == 0)
535 {
536 out_value &= ~mask;
537 usb_blaster_write_data();
538 }
539 else if (state == 1)
540 {
541 out_value |= mask;
542 usb_blaster_write_data();
543 }
544 else
545 {
546 LOG_ERROR("%s: pin state must be 0 or 1", CMD_NAME);
547 return ERROR_COMMAND_SYNTAX_ERROR;
548 }
549
550 return ERROR_OK;
551 }
552 else
553 {
554 LOG_ERROR("%s takes exactly two arguments", CMD_NAME);
555 return ERROR_COMMAND_SYNTAX_ERROR;
556 }
557 }
558
559 static const struct command_registration usb_blaster_command_handlers[] = {
560 {
561 .name = "usb_blaster_device_desc",
562 .handler = usb_blaster_handle_device_desc_command,
563 .mode = COMMAND_CONFIG,
564 .help = "set the USB device description of the USB-Blaster",
565 .usage = "description-string",
566 },
567 {
568 .name = "usb_blaster_vid_pid",
569 .handler = usb_blaster_handle_vid_pid_command,
570 .mode = COMMAND_CONFIG,
571 .help = "the vendor ID and product ID of the USB-Blaster",
572 .usage = "vid pid",
573 },
574 {
575 .name = "usb_blaster",
576 .handler = usb_blaster_handle_pin_command,
577 .mode = COMMAND_ANY,
578 .help = "set pin state for the unused GPIO pins",
579 .usage = "(pin6|pin8) (0|1)",
580 },
581 COMMAND_REGISTRATION_DONE
582 };
583
584 struct jtag_interface usb_blaster_interface = {
585 .name = "usb_blaster",
586 .commands = usb_blaster_command_handlers,
587 .supported = DEBUG_CAP_TMS_SEQ,
588
589 .execute_queue = bitbang_execute_queue,
590
591 .speed = usb_blaster_speed,
592 .init = usb_blaster_init,
593 .quit = usb_blaster_quit,
594 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)