2 * Driver for USB-JTAG, Altera USB-Blaster and compatibles
4 * Inspired from original code from Kolja Waschk's USB-JTAG project
5 * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
7 * Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
8 * Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
9 * Copyright (C) 2011 Ali Lown ali@lown.me.uk
10 * Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
11 * Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * The following information is originally from Kolja Waschk's USB-JTAG,
30 * where it was obtained by reverse engineering an Altera USB-Blaster.
31 * See http://www.ixo.de/info/usb_jtag/ for USB-Blaster block diagram and
32 * usb_jtag-20080705-1200.zip#usb_jtag/host/openocd for protocol.
34 * The same information is also on the UrJTAG mediawiki, with some additional
35 * notes on bits marked as "unknown" by usb_jtag.
36 * (http://sourceforge.net/apps/mediawiki/urjtag/index.php?
37 * title=Cable_Altera_USB-Blaster)
39 * USB-JTAG, Altera USB-Blaster and compatibles are typically implemented as
40 * an FTDIChip FT245 followed by a CPLD which handles a two-mode protocol:
46 * __|__________ _________
48 * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
49 * |_____________| |_________|
50 * __|__________ _|___________
52 * | 6 MHz XTAL | | 24 MHz Osc. |
53 * |_____________| |_____________|
55 * USB-JTAG, Altera USB-Blaster II are typically implemented as a Cypress
56 * EZ-USB FX2LP followed by a CPLD.
57 * _____________ _________
59 * USB__| EZ-USB FX2 |__| EPM570 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
60 * |_____________| |_________|
76 /* project specific includes */
77 #include <jtag/interface.h>
78 #include <jtag/commands.h>
79 #include <helper/time_support.h>
80 #include "ublast_access.h"
89 /* Size of USB endpoint max packet size, ie. 64 bytes */
90 #define MAX_PACKET_SIZE 64
92 * Size of data buffer that holds bytes in byte-shift mode.
93 * This buffer can hold multiple USB packets aligned to
94 * MAX_PACKET_SIZE bytes boundaries.
95 * BUF_LEN must be grater than or equal MAX_PACKET_SIZE.
99 /* USB-Blaster II specific command */
100 #define CMD_COPY_TDO_BUFFER 0x5F
110 enum gpio_steer pin6;
111 enum gpio_steer pin8;
116 uint8_t buf[BUF_LEN];
120 struct ublast_lowlevel *drv;
121 char *ublast_device_desc;
122 uint16_t ublast_vid, ublast_pid;
123 uint16_t ublast_vid_uninit, ublast_pid_uninit;
129 * Global device control
131 static struct ublast_info info = {
132 .ublast_vid = 0x09fb, /* Altera */
133 .ublast_pid = 0x6001, /* USB-Blaster */
134 .lowlevel_name = NULL,
135 .srst_asserted = false,
136 .trst_asserted = false,
142 * Available lowlevel drivers (FTDI, FTD2xx, ...)
146 struct ublast_lowlevel *(*drv_register)(void);
149 static struct drvs_map lowlevel_drivers_map[] = {
150 #if BUILD_USB_BLASTER
151 { .name = "ftdi", .drv_register = ublast_register_ftdi },
153 #if BUILD_USB_BLASTER_2
154 { .name = "ublast2", .drv_register = ublast2_register_libusb },
160 * Access functions to lowlevel driver, agnostic of libftdi/libftdxx
162 static char *hexdump(uint8_t *buf, unsigned int size)
165 char *str = calloc(size * 2 + 1, 1);
167 for (i = 0; i < size; i++)
168 sprintf(str + 2*i, "%02x", buf[i]);
172 static int ublast_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read)
174 int ret = info.drv->read(info.drv, buf, size, bytes_read);
175 char *str = hexdump(buf, *bytes_read);
177 LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
183 static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
185 int ret = info.drv->write(info.drv, buf, size, bytes_written);
186 char *str = hexdump(buf, *bytes_written);
188 LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
194 static int nb_buf_remaining(void)
196 return BUF_LEN - info.bufidx;
199 static void ublast_flush_buffer(void)
202 int nb = info.bufidx, ret = ERROR_OK;
204 while (ret == ERROR_OK && nb > 0) {
205 ret = ublast_buf_write(info.buf, nb, &retlen);
212 * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
213 * bits (bidirectional) in a single USB packet. A header byte has to be sent as
214 * the first byte in a packet with the following meaning:
216 * Bit 7 (0x80): Must be set to indicate byte-shift mode.
217 * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
218 * Bit 5..0: Define the number N of following bytes
220 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
221 * set, it will afterwards return N bytes with TDO data read while clocking out
222 * the TDI data. LSB of the first byte after the header byte will appear first
226 /* Simple bit banging mode:
228 * Bit 7 (0x80): Must be zero (see byte-shift mode above)
229 * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
231 * Bit 5 (0x20): Output Enable/LED.
232 * Bit 4 (0x10): TDI Output.
233 * Bit 3 (0x08): nCS Output (not used in JTAG mode).
234 * Bit 2 (0x04): nCE Output (not used in JTAG mode).
235 * Bit 1 (0x02): TMS Output.
236 * Bit 0 (0x01): TCK Output.
238 * For transmitting a single data bit, you need to write two bytes (one for
239 * setting up TDI/TMS/TCK=0, and one to trigger TCK high with same TDI/TMS
240 * held). Up to 64 bytes can be combined in a single USB packet.
241 * It isn't possible to read a data without transmitting data.
250 #define READ (1 << 6)
251 #define SHMODE (1 << 7)
252 #define READ_TDO (1 << 0)
255 * ublast_queue_byte - queue one 'bitbang mode' byte for USB Blaster
256 * @abyte: the byte to queue
258 * Queues one byte in 'bitbang mode' to the USB Blaster. The byte is not
259 * actually sent, but stored in a buffer. The write is performed once
260 * the buffer is filled, or if an explicit ublast_flush_buffer() is called.
262 static void ublast_queue_byte(uint8_t abyte)
264 if (nb_buf_remaining() < 1)
265 ublast_flush_buffer();
266 info.buf[info.bufidx++] = abyte;
267 if (nb_buf_remaining() == 0)
268 ublast_flush_buffer();
269 LOG_DEBUG_IO("(byte=0x%02x)", abyte);
273 * ublast_compute_pin - compute if gpio should be asserted
274 * @steer: control (ie. TRST driven, SRST driven, of fixed)
276 * Returns pin value (1 means driven high, 0 mean driven low)
278 bool ublast_compute_pin(enum gpio_steer steer)
286 return !info.srst_asserted;
288 return !info.trst_asserted;
295 * ublast_build_out - build bitbang mode output byte
296 * @type: says if reading back TDO is required
298 * Returns the compute bitbang mode byte
300 static uint8_t ublast_build_out(enum scan_type type)
304 abyte |= info.tms ? TMS : 0;
305 abyte |= ublast_compute_pin(info.pin6) ? NCE : 0;
306 abyte |= ublast_compute_pin(info.pin8) ? NCS : 0;
307 abyte |= info.tdi ? TDI : 0;
309 if (type == SCAN_IN || type == SCAN_IO)
315 * ublast_reset - reset the JTAG device is possible
316 * @trst: 1 if TRST is to be asserted
317 * @srst: 1 if SRST is to be asserted
319 static void ublast_reset(int trst, int srst)
323 info.trst_asserted = trst;
324 info.srst_asserted = srst;
325 out_value = ublast_build_out(SCAN_OUT);
326 ublast_queue_byte(out_value);
327 ublast_flush_buffer();
331 * ublast_clock_tms - clock a TMS transition
332 * @tms: the TMS to be sent
334 * Triggers a TMS transition (ie. one JTAG TAP state move).
336 static void ublast_clock_tms(int tms)
340 LOG_DEBUG_IO("(tms=%d)", !!tms);
343 out = ublast_build_out(SCAN_OUT);
344 ublast_queue_byte(out);
345 ublast_queue_byte(out | TCK);
349 * ublast_idle_clock - put back TCK to low level
351 * See ublast_queue_tdi() comment for the usage of this function.
353 static void ublast_idle_clock(void)
355 uint8_t out = ublast_build_out(SCAN_OUT);
358 ublast_queue_byte(out);
362 * ublast_clock_tdi - Output a TDI with bitbang mode
363 * @tdi: the TDI bit to be shifted out
364 * @type: scan type (ie. does a readback of TDO is required)
366 * Output a TDI bit and assert clock to push it into the JTAG device :
367 * - writing out TCK=0, TMS=<old_state>=0, TDI=<tdi>
368 * - writing out TCK=1, TMS=<new_state>, TDI=<tdi> which triggers the JTAG
369 * device acquiring the data.
371 * If a TDO is to be read back, the required read is requested (bitbang mode),
372 * and the USB Blaster will send back a byte with bit0 representing the TDO.
374 static void ublast_clock_tdi(int tdi, enum scan_type type)
378 LOG_DEBUG_IO("(tdi=%d)", !!tdi);
381 out = ublast_build_out(SCAN_OUT);
382 ublast_queue_byte(out);
384 out = ublast_build_out(type);
385 ublast_queue_byte(out | TCK);
389 * ublast_clock_tdi_flip_tms - Output a TDI with bitbang mode, change JTAG state
390 * @tdi: the TDI bit to be shifted out
391 * @type: scan type (ie. does a readback of TDO is required)
393 * This function is the same as ublast_clock_tdi(), but it changes also the TMS
394 * while output the TDI. This should be the last TDI output of a TDI
395 * sequence, which will change state from :
396 * - IRSHIFT -> IREXIT1
397 * - or DRSHIFT -> DREXIT1
399 static void ublast_clock_tdi_flip_tms(int tdi, enum scan_type type)
403 LOG_DEBUG_IO("(tdi=%d)", !!tdi);
405 info.tms = !info.tms;
407 out = ublast_build_out(SCAN_OUT);
408 ublast_queue_byte(out);
410 out = ublast_build_out(type);
411 ublast_queue_byte(out | TCK);
413 out = ublast_build_out(SCAN_OUT);
414 ublast_queue_byte(out);
418 * ublast_queue_bytes - queue bytes for the USB Blaster
420 * @nb_bytes: number of bytes
422 * Queues bytes to be sent to the USB Blaster. The bytes are not
423 * actually sent, but stored in a buffer. The write is performed once
424 * the buffer is filled, or if an explicit ublast_flush_buffer() is called.
426 static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
428 if (info.bufidx + nb_bytes > BUF_LEN) {
429 LOG_ERROR("buggy code, should never queue more that %d bytes",
430 info.bufidx + nb_bytes);
433 LOG_DEBUG_IO("(nb_bytes=%d, bytes=[0x%02x, ...])", nb_bytes,
434 bytes ? bytes[0] : 0);
436 memcpy(&info.buf[info.bufidx], bytes, nb_bytes);
438 memset(&info.buf[info.bufidx], 0, nb_bytes);
439 info.bufidx += nb_bytes;
440 if (nb_buf_remaining() == 0)
441 ublast_flush_buffer();
445 * ublast_tms_seq - write a TMS sequence transition to JTAG
446 * @bits: TMS bits to be written (bit0, bit1 .. bitN)
447 * @nb_bits: number of TMS bits (between 1 and 8)
448 * @skip: number of TMS bits to skip at the beginning of the series
450 * Write a series of TMS transitions, where each transition consists in :
451 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
452 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
453 * The function ensures that at the end of the sequence, the clock (TCK) is put
456 static void ublast_tms_seq(const uint8_t *bits, int nb_bits, int skip)
460 LOG_DEBUG_IO("(bits=%02x..., nb_bits=%d)", bits[0], nb_bits);
461 for (i = skip; i < nb_bits; i++)
462 ublast_clock_tms((bits[i / 8] >> (i % 8)) & 0x01);
467 * ublast_tms - write a tms command
470 static void ublast_tms(struct tms_command *cmd)
472 LOG_DEBUG_IO("(num_bits=%d)", cmd->num_bits);
473 ublast_tms_seq(cmd->bits, cmd->num_bits, 0);
477 * ublast_path_move - write a TMS sequence transition to JTAG
478 * @cmd: path transition
480 * Write a series of TMS transitions, where each transition consists in :
481 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
482 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
483 * The function ensures that at the end of the sequence, the clock (TCK) is put
486 static void ublast_path_move(struct pathmove_command *cmd)
490 LOG_DEBUG_IO("(num_states=%d, last_state=%d)",
491 cmd->num_states, cmd->path[cmd->num_states - 1]);
492 for (i = 0; i < cmd->num_states; i++) {
493 if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
495 if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
497 tap_set_state(cmd->path[i]);
503 * ublast_state_move - move JTAG state to the target state
504 * @state: the target state
505 * @skip: number of bits to skip at the beginning of the path
507 * Input the correct TMS sequence to the JTAG TAP so that we end up in the
508 * target state. This assumes the current state (tap_get_state()) is correct.
510 static void ublast_state_move(tap_state_t state, int skip)
515 LOG_DEBUG_IO("(from %s to %s)", tap_state_name(tap_get_state()),
516 tap_state_name(state));
517 if (tap_get_state() == state)
519 tms_scan = tap_get_tms_path(tap_get_state(), state);
520 tms_len = tap_get_tms_path_len(tap_get_state(), state);
521 ublast_tms_seq(&tms_scan, tms_len, skip);
522 tap_set_state(state);
526 * ublast_read_byteshifted_tdos - read TDO of byteshift writes
527 * @buf: the buffer to store the bits
528 * @nb_bits: the number of bits
530 * Reads back from USB Blaster TDO bits, triggered by a 'byteshift write', ie. eight
531 * bits per received byte from USB interface, and store them in buffer.
533 * As the USB blaster stores the TDO bits in LSB (ie. first bit in (byte0,
534 * bit0), second bit in (byte0, bit1), ...), which is what we want to return,
535 * simply read bytes from USB interface and store them.
537 * Returns ERROR_OK if OK, ERROR_xxx if a read error occurred
539 static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
544 LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bytes * 8);
545 ublast_flush_buffer();
546 while (ret == ERROR_OK && nb_bytes > 0) {
547 ret = ublast_buf_read(buf, nb_bytes, &retlen);
554 * ublast_read_bitbang_tdos - read TDO of bitbang writes
555 * @buf: the buffer to store the bits
556 * @nb_bits: the number of bits
558 * Reads back from USB Blaster TDO bits, triggered by a 'bitbang write', ie. one
559 * bit per received byte from USB interface, and store them in buffer, where :
560 * - first bit is stored in byte0, bit0 (LSB)
561 * - second bit is stored in byte0, bit 1
563 * - eight bit is sotred in byte0, bit 7
564 * - ninth bit is sotred in byte1, bit 0
567 * Returns ERROR_OK if OK, ERROR_xxx if a read error occurred
569 static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
572 int i, ret = ERROR_OK;
576 LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bits);
579 * Ensure all previous bitbang writes were issued to the dongle, so that
580 * it returns back the read values.
582 ublast_flush_buffer();
584 ret = ublast_buf_read(tmp, nb1, &retlen);
585 for (i = 0; ret == ERROR_OK && i < nb1; i++)
586 if (tmp[i] & READ_TDO)
594 * ublast_queue_tdi - short description
595 * @bits: bits to be queued on TDI (or NULL if 0 are to be queued)
596 * @nb_bits: number of bits
597 * @scan: scan type (ie. if TDO read back is required or not)
599 * Outputs a series of TDI bits on TDI.
600 * As a side effect, the last TDI bit is sent along a TMS=1, and triggers a JTAG
601 * TAP state shift if input bits were non NULL.
603 * In order to not saturate the USB Blaster queues, this method reads back TDO
604 * if the scan type requests it, and stores them back in bits.
606 * As a side note, the state of TCK when entering this function *must* be
607 * low. This is because byteshift mode outputs TDI on rising TCK and reads TDO
608 * on falling TCK if and only if TCK is low before queuing byteshift mode bytes.
609 * If TCK was high, the USB blaster will queue TDI on falling edge, and read TDO
612 static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
614 int nb8 = nb_bits / 8;
615 int nb1 = nb_bits % 8;
616 int nbfree_in_packet, i, trans = 0, read_tdos;
617 uint8_t *tdos = calloc(1, nb_bits / 8 + 1);
618 static uint8_t byte0[BUF_LEN];
621 * As the last TDI bit should always be output in bitbang mode in order
622 * to activate the TMS=1 transition to EXIT_?R state. Therefore a
623 * situation where nb_bits is a multiple of 8 is handled as follows:
624 * - the number of TDI shifted out in "byteshift mode" is 8 less than
627 * This ensures that nb1 is never 0, and allows the TMS transition.
629 if (nb8 > 0 && nb1 == 0) {
634 read_tdos = (scan == SCAN_IN || scan == SCAN_IO);
635 for (i = 0; i < nb8; i += trans) {
637 * Calculate number of bytes to fill USB packet of size MAX_PACKET_SIZE
639 nbfree_in_packet = (MAX_PACKET_SIZE - (info.bufidx%MAX_PACKET_SIZE));
640 trans = MIN(nbfree_in_packet - 1, nb8 - i);
643 * Queue a byte-shift mode transmission, with as many bytes as
644 * is possible with regard to :
645 * - current filling level of write buffer
646 * - remaining bytes to write in byte-shift mode
649 ublast_queue_byte(SHMODE | READ | trans);
651 ublast_queue_byte(SHMODE | trans);
653 ublast_queue_bytes(&bits[i], trans);
655 ublast_queue_bytes(byte0, trans);
657 if (info.flags & COPY_TDO_BUFFER)
658 ublast_queue_byte(CMD_COPY_TDO_BUFFER);
659 ublast_read_byteshifted_tdos(&tdos[i], trans);
664 * Queue the remaining TDI bits in bitbang mode.
666 for (i = 0; i < nb1; i++) {
667 int tdi = bits ? bits[nb8 + i / 8] & (1 << i) : 0;
668 if (bits && i == nb1 - 1)
669 ublast_clock_tdi_flip_tms(tdi, scan);
671 ublast_clock_tdi(tdi, scan);
673 if (nb1 && read_tdos) {
674 if (info.flags & COPY_TDO_BUFFER)
675 ublast_queue_byte(CMD_COPY_TDO_BUFFER);
676 ublast_read_bitbang_tdos(&tdos[nb8], nb1);
680 memcpy(bits, tdos, DIV_ROUND_UP(nb_bits, 8));
684 * Ensure clock is in lower state
689 static void ublast_runtest(int cycles, tap_state_t state)
691 LOG_DEBUG_IO("%s(cycles=%i, end_state=%d)", __func__, cycles, state);
693 ublast_state_move(TAP_IDLE, 0);
694 ublast_queue_tdi(NULL, cycles, SCAN_OUT);
695 ublast_state_move(state, 0);
698 static void ublast_stableclocks(int cycles)
700 LOG_DEBUG_IO("%s(cycles=%i)", __func__, cycles);
701 ublast_queue_tdi(NULL, cycles, SCAN_OUT);
705 * ublast_scan - launches a DR-scan or IR-scan
706 * @cmd: the command to launch
708 * Launch a JTAG IR-scan or DR-scan
710 * Returns ERROR_OK if OK, ERROR_xxx if a read/write error occurred.
712 static int ublast_scan(struct scan_command *cmd)
718 static const char * const type2str[] = { "", "SCAN_IN", "SCAN_OUT", "SCAN_IO" };
719 char *log_buf = NULL;
721 type = jtag_scan_type(cmd);
722 scan_bits = jtag_build_buffer(cmd, &buf);
725 ublast_state_move(TAP_IRSHIFT, 0);
727 ublast_state_move(TAP_DRSHIFT, 0);
729 log_buf = hexdump(buf, DIV_ROUND_UP(scan_bits, 8));
730 LOG_DEBUG_IO("%s(scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d)", __func__,
731 cmd->ir_scan ? "IRSCAN" : "DRSCAN",
733 scan_bits, log_buf, cmd->end_state);
736 ublast_queue_tdi(buf, scan_bits, type);
738 ret = jtag_read_buffer(buf, cmd);
741 * ublast_queue_tdi sends the last bit with TMS=1. We are therefore
742 * already in Exit1-DR/IR and have to skip the first step on our way
745 ublast_state_move(cmd->end_state, 1);
749 static void ublast_usleep(int us)
751 LOG_DEBUG_IO("%s(us=%d)", __func__, us);
755 static void ublast_initial_wipeout(void)
757 static uint8_t tms_reset = 0xff;
762 out_value = ublast_build_out(SCAN_OUT);
763 for (i = 0; i < BUF_LEN; i++)
764 info.buf[i] = out_value | ((i % 2) ? TCK : 0);
767 * Flush USB-Blaster queue fifos
768 * - empty the write FIFO (128 bytes)
769 * - empty the read FIFO (384 bytes)
771 ublast_buf_write(info.buf, BUF_LEN, &retlen);
773 * Put JTAG in RESET state (five 1 on TMS)
775 ublast_tms_seq(&tms_reset, 5, 0);
776 tap_set_state(TAP_RESET);
779 static int ublast_execute_queue(void)
781 struct jtag_command *cmd;
782 static int first_call = 1;
787 ublast_initial_wipeout();
790 for (cmd = jtag_command_queue; ret == ERROR_OK && cmd != NULL;
794 ublast_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
797 ublast_runtest(cmd->cmd.runtest->num_cycles,
798 cmd->cmd.runtest->end_state);
800 case JTAG_STABLECLOCKS:
801 ublast_stableclocks(cmd->cmd.stableclocks->num_cycles);
804 ublast_state_move(cmd->cmd.statemove->end_state, 0);
807 ublast_path_move(cmd->cmd.pathmove);
810 ublast_tms(cmd->cmd.tms);
813 ublast_usleep(cmd->cmd.sleep->us);
816 ret = ublast_scan(cmd->cmd.scan);
819 LOG_ERROR("BUG: unknown JTAG command type 0x%X",
826 ublast_flush_buffer();
831 * ublast_init - Initialize the Altera device
833 * Initialize the device :
834 * - open the USB device
835 * - pretend it's initialized while actual init is delayed until first jtag command
837 * Returns ERROR_OK if USB device found, error if not.
839 static int ublast_init(void)
843 for (i = 0; lowlevel_drivers_map[i].name; i++) {
844 if (info.lowlevel_name) {
845 if (!strcmp(lowlevel_drivers_map[i].name, info.lowlevel_name)) {
846 info.drv = lowlevel_drivers_map[i].drv_register();
848 LOG_ERROR("Error registering lowlevel driver \"%s\"",
850 return ERROR_JTAG_DEVICE_ERROR;
855 info.drv = lowlevel_drivers_map[i].drv_register();
857 info.lowlevel_name = strdup(lowlevel_drivers_map[i].name);
858 LOG_INFO("No lowlevel driver configured, using %s", info.lowlevel_name);
865 LOG_ERROR("No lowlevel driver available");
866 return ERROR_JTAG_DEVICE_ERROR;
870 * Register the lowlevel driver
872 info.drv->ublast_vid = info.ublast_vid;
873 info.drv->ublast_pid = info.ublast_pid;
874 info.drv->ublast_vid_uninit = info.ublast_vid_uninit;
875 info.drv->ublast_pid_uninit = info.ublast_pid_uninit;
876 info.drv->ublast_device_desc = info.ublast_device_desc;
877 info.drv->firmware_path = info.firmware_path;
879 info.flags |= info.drv->flags;
881 ret = info.drv->open(info.drv);
884 * Let lie here : the TAP is in an unknown state, but the first
885 * execute_queue() will trigger a ublast_initial_wipeout(), which will
886 * put the TAP in RESET.
888 tap_set_state(TAP_RESET);
893 * ublast_quit - Release the Altera device
895 * Releases the device :
896 * - put the device pins in 'high impedance' mode
897 * - close the USB device
899 * Returns always ERROR_OK
901 static int ublast_quit(void)
906 ublast_buf_write(&byte0, 1, &retlen);
907 return info.drv->close(info.drv);
910 COMMAND_HANDLER(ublast_handle_device_desc_command)
913 return ERROR_COMMAND_SYNTAX_ERROR;
915 info.ublast_device_desc = strdup(CMD_ARGV[0]);
920 COMMAND_HANDLER(ublast_handle_vid_pid_command)
923 LOG_WARNING("ignoring extra IDs in ublast_vid_pid "
924 "(maximum is 2 pairs)");
929 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], info.ublast_vid);
930 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], info.ublast_pid);
932 LOG_WARNING("incomplete ublast_vid_pid configuration");
936 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[2], info.ublast_vid_uninit);
937 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[3], info.ublast_pid_uninit);
939 LOG_WARNING("incomplete ublast_vid_pid configuration");
945 COMMAND_HANDLER(ublast_handle_pin_command)
948 const char * const pin_name = CMD_ARGV[0];
949 enum gpio_steer *steer = NULL;
950 static const char * const pin_val_str[] = {
953 [SRST] = "SRST driven",
954 [TRST] = "TRST driven",
958 LOG_ERROR("%s takes exactly one or two arguments", CMD_NAME);
959 return ERROR_COMMAND_SYNTAX_ERROR;
962 if (!strcmp(pin_name, "pin6"))
964 if (!strcmp(pin_name, "pin8"))
967 LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
969 return ERROR_COMMAND_SYNTAX_ERROR;
973 LOG_INFO("%s: %s is set as %s\n", CMD_NAME, pin_name,
974 pin_val_str[*steer]);
978 const char * const pin_value = CMD_ARGV[1];
979 char val = pin_value[0];
981 if (strlen(pin_value) > 1)
983 switch (tolower((unsigned char)val)) {
997 LOG_ERROR("%s: pin value must be 0, 1, s (SRST) or t (TRST)",
999 return ERROR_COMMAND_SYNTAX_ERROR;
1003 out_value = ublast_build_out(SCAN_OUT);
1004 ublast_queue_byte(out_value);
1005 ublast_flush_buffer();
1011 COMMAND_HANDLER(ublast_handle_lowlevel_drv_command)
1014 return ERROR_COMMAND_SYNTAX_ERROR;
1016 info.lowlevel_name = strdup(CMD_ARGV[0]);
1021 COMMAND_HANDLER(ublast_firmware_command)
1024 return ERROR_COMMAND_SYNTAX_ERROR;
1026 info.firmware_path = strdup(CMD_ARGV[0]);
1032 static const struct command_registration ublast_command_handlers[] = {
1034 .name = "usb_blaster_device_desc",
1035 .handler = ublast_handle_device_desc_command,
1036 .mode = COMMAND_CONFIG,
1037 .help = "set the USB device description of the USB-Blaster",
1038 .usage = "description-string",
1041 .name = "usb_blaster_vid_pid",
1042 .handler = ublast_handle_vid_pid_command,
1043 .mode = COMMAND_CONFIG,
1044 .help = "the vendor ID and product ID of the USB-Blaster and "
1045 "vendor ID and product ID of the uninitialized device "
1046 "for USB-Blaster II",
1047 .usage = "vid pid vid_uninit pid_uninit",
1050 .name = "usb_blaster_lowlevel_driver",
1051 .handler = ublast_handle_lowlevel_drv_command,
1052 .mode = COMMAND_CONFIG,
1053 .help = "set the lowlevel access for the USB Blaster (ftdi, ublast2)",
1054 .usage = "(ftdi|ublast2)",
1057 .name = "usb_blaster_pin",
1058 .handler = ublast_handle_pin_command,
1059 .mode = COMMAND_ANY,
1060 .help = "show or set pin state for the unused GPIO pins",
1061 .usage = "(pin6|pin8) (0|1|s|t)",
1064 .name = "usb_blaster_firmware",
1065 .handler = &ublast_firmware_command,
1066 .mode = COMMAND_CONFIG,
1067 .help = "configure the USB-Blaster II firmware location",
1068 .usage = "path/to/blaster_xxxx.hex",
1070 COMMAND_REGISTRATION_DONE
1073 static struct jtag_interface usb_blaster_interface = {
1074 .supported = DEBUG_CAP_TMS_SEQ,
1075 .execute_queue = ublast_execute_queue,
1078 struct adapter_driver usb_blaster_adapter_driver = {
1079 .name = "usb_blaster",
1080 .transports = jtag_only,
1081 .commands = ublast_command_handlers,
1083 .init = ublast_init,
1084 .quit = ublast_quit,
1086 .jtag_ops = &usb_blaster_interface,