8f3f327b96a01f34a9cc8dc1eaa36191fd526c19
[openocd.git] / src / jtag / drivers / usb_blaster / usb_blaster.c
1 /*
2 * Driver for USB-JTAG, Altera USB-Blaster and compatibles
3 *
4 * Inspired from original code from Kolja Waschk's USB-JTAG project
5 * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
6 *
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
12 *
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.
17 *
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.
22 *
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/>.
25 *
26 */
27
28 /*
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.
33 *
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)
38 *
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:
41 *
42 * _________
43 * | |
44 * | AT93C46 |
45 * |_________|
46 * __|__________ _________
47 * | | | |
48 * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
49 * |_____________| |_________|
50 * __|__________ _|___________
51 * | | | |
52 * | 6 MHz XTAL | | 24 MHz Osc. |
53 * |_____________| |_____________|
54 *
55 * USB-JTAG, Altera USB-Blaster II are typically implemented as a Cypress
56 * EZ-USB FX2LP followed by a CPLD.
57 * _____________ _________
58 * | | | |
59 * USB__| EZ-USB FX2 |__| EPM570 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
60 * |_____________| |_________|
61 * __|__________
62 * | |
63 * | 24 MHz XTAL |
64 * |_____________|
65 */
66
67 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70
71 #if IS_CYGWIN == 1
72 #include "windows.h"
73 #undef LOG_ERROR
74 #endif
75
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"
81
82 /* system includes */
83 #include <string.h>
84 #include <stdlib.h>
85 #include <unistd.h>
86 #include <sys/time.h>
87 #include <time.h>
88
89 /* Size of USB endpoint max packet size, ie. 64 bytes */
90 #define MAX_PACKET_SIZE 64
91 /*
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.
96 */
97 #define BUF_LEN 4096
98
99 /* USB-Blaster II specific command */
100 #define CMD_COPY_TDO_BUFFER 0x5F
101
102 enum gpio_steer {
103 FIXED_0 = 0,
104 FIXED_1,
105 SRST,
106 TRST,
107 };
108
109 struct ublast_info {
110 enum gpio_steer pin6;
111 enum gpio_steer pin8;
112 int tms;
113 int tdi;
114 bool trst_asserted;
115 bool srst_asserted;
116 uint8_t buf[BUF_LEN];
117 int bufidx;
118
119 char *lowlevel_name;
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;
124 int flags;
125 char *firmware_path;
126 };
127
128 /*
129 * Global device control
130 */
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,
137 .pin6 = FIXED_1,
138 .pin8 = FIXED_1,
139 };
140
141 /*
142 * Available lowlevel drivers (FTDI, FTD2xx, ...)
143 */
144 struct drvs_map {
145 char *name;
146 struct ublast_lowlevel *(*drv_register)(void);
147 };
148
149 static struct drvs_map lowlevel_drivers_map[] = {
150 #if BUILD_USB_BLASTER_LIBFTDI
151 { .name = "ftdi", .drv_register = ublast_register_ftdi },
152 #endif
153 #if BUILD_USB_BLASTER_FTD2XX
154 { .name = "ftd2xx", .drv_register = ublast_register_ftd2xx },
155 #endif
156 #if BUILD_USB_BLASTER_2
157 { .name = "ublast2", .drv_register = ublast2_register_libusb },
158 #endif
159 { NULL, NULL },
160 };
161
162 /*
163 * Access functions to lowlevel driver, agnostic of libftdi/libftdxx
164 */
165 static char *hexdump(uint8_t *buf, unsigned int size)
166 {
167 unsigned int i;
168 char *str = calloc(size * 2 + 1, 1);
169
170 for (i = 0; i < size; i++)
171 sprintf(str + 2*i, "%02x", buf[i]);
172 return str;
173 }
174
175 static int ublast_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read)
176 {
177 int ret = info.drv->read(info.drv, buf, size, bytes_read);
178 char *str = hexdump(buf, *bytes_read);
179
180 DEBUG_JTAG_IO("(size=%d, buf=[%s]) -> %u", size, str,
181 *bytes_read);
182 free(str);
183 return ret;
184 }
185
186 static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
187 {
188 int ret = info.drv->write(info.drv, buf, size, bytes_written);
189 char *str = hexdump(buf, *bytes_written);
190
191 DEBUG_JTAG_IO("(size=%d, buf=[%s]) -> %u", size, str,
192 *bytes_written);
193 free(str);
194 return ret;
195 }
196
197 static int nb_buf_remaining(void)
198 {
199 return BUF_LEN - info.bufidx;
200 }
201
202 static void ublast_flush_buffer(void)
203 {
204 unsigned int retlen;
205 int nb = info.bufidx, ret = ERROR_OK;
206
207 while (ret == ERROR_OK && nb > 0) {
208 ret = ublast_buf_write(info.buf, nb, &retlen);
209 nb -= retlen;
210 }
211 info.bufidx = 0;
212 }
213
214 /*
215 * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
216 * bits (bidirectional) in a single USB packet. A header byte has to be sent as
217 * the first byte in a packet with the following meaning:
218 *
219 * Bit 7 (0x80): Must be set to indicate byte-shift mode.
220 * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
221 * Bit 5..0: Define the number N of following bytes
222 *
223 * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
224 * set, it will afterwards return N bytes with TDO data read while clocking out
225 * the TDI data. LSB of the first byte after the header byte will appear first
226 * on TDI.
227 */
228
229 /* Simple bit banging mode:
230 *
231 * Bit 7 (0x80): Must be zero (see byte-shift mode above)
232 * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
233 * in return.
234 * Bit 5 (0x20): Output Enable/LED.
235 * Bit 4 (0x10): TDI Output.
236 * Bit 3 (0x08): nCS Output (not used in JTAG mode).
237 * Bit 2 (0x04): nCE Output (not used in JTAG mode).
238 * Bit 1 (0x02): TMS Output.
239 * Bit 0 (0x01): TCK Output.
240 *
241 * For transmitting a single data bit, you need to write two bytes (one for
242 * setting up TDI/TMS/TCK=0, and one to trigger TCK high with same TDI/TMS
243 * held). Up to 64 bytes can be combined in a single USB packet.
244 * It isn't possible to read a data without transmitting data.
245 */
246
247 #define TCK (1 << 0)
248 #define TMS (1 << 1)
249 #define NCE (1 << 2)
250 #define NCS (1 << 3)
251 #define TDI (1 << 4)
252 #define LED (1 << 5)
253 #define READ (1 << 6)
254 #define SHMODE (1 << 7)
255 #define READ_TDO (1 << 0)
256
257 /**
258 * ublast_queue_byte - queue one 'bitbang mode' byte for USB Blaster
259 * @abyte: the byte to queue
260 *
261 * Queues one byte in 'bitbang mode' to the USB Blaster. The byte is not
262 * actually sent, but stored in a buffer. The write is performed once
263 * the buffer is filled, or if an explicit ublast_flush_buffer() is called.
264 */
265 static void ublast_queue_byte(uint8_t abyte)
266 {
267 if (nb_buf_remaining() < 1)
268 ublast_flush_buffer();
269 info.buf[info.bufidx++] = abyte;
270 if (nb_buf_remaining() == 0)
271 ublast_flush_buffer();
272 DEBUG_JTAG_IO("(byte=0x%02x)", abyte);
273 }
274
275 /**
276 * ublast_compute_pin - compute if gpio should be asserted
277 * @steer: control (ie. TRST driven, SRST driven, of fixed)
278 *
279 * Returns pin value (1 means driven high, 0 mean driven low)
280 */
281 bool ublast_compute_pin(enum gpio_steer steer)
282 {
283 switch (steer) {
284 case FIXED_0:
285 return 0;
286 case FIXED_1:
287 return 1;
288 case SRST:
289 return !info.srst_asserted;
290 case TRST:
291 return !info.trst_asserted;
292 default:
293 return 1;
294 }
295 }
296
297 /**
298 * ublast_build_out - build bitbang mode output byte
299 * @type: says if reading back TDO is required
300 *
301 * Returns the compute bitbang mode byte
302 */
303 static uint8_t ublast_build_out(enum scan_type type)
304 {
305 uint8_t abyte = 0;
306
307 abyte |= info.tms ? TMS : 0;
308 abyte |= ublast_compute_pin(info.pin6) ? NCE : 0;
309 abyte |= ublast_compute_pin(info.pin8) ? NCS : 0;
310 abyte |= info.tdi ? TDI : 0;
311 abyte |= LED;
312 if (type == SCAN_IN || type == SCAN_IO)
313 abyte |= READ;
314 return abyte;
315 }
316
317 /**
318 * ublast_reset - reset the JTAG device is possible
319 * @trst: 1 if TRST is to be asserted
320 * @srst: 1 if SRST is to be asserted
321 */
322 static void ublast_reset(int trst, int srst)
323 {
324 uint8_t out_value;
325
326 info.trst_asserted = trst;
327 info.srst_asserted = srst;
328 out_value = ublast_build_out(SCAN_OUT);
329 ublast_queue_byte(out_value);
330 ublast_flush_buffer();
331 }
332
333 /**
334 * ublast_clock_tms - clock a TMS transition
335 * @tms: the TMS to be sent
336 *
337 * Triggers a TMS transition (ie. one JTAG TAP state move).
338 */
339 static void ublast_clock_tms(int tms)
340 {
341 uint8_t out;
342
343 DEBUG_JTAG_IO("(tms=%d)", !!tms);
344 info.tms = !!tms;
345 info.tdi = 0;
346 out = ublast_build_out(SCAN_OUT);
347 ublast_queue_byte(out);
348 ublast_queue_byte(out | TCK);
349 }
350
351 /**
352 * ublast_idle_clock - put back TCK to low level
353 *
354 * See ublast_queue_tdi() comment for the usage of this function.
355 */
356 static void ublast_idle_clock(void)
357 {
358 uint8_t out = ublast_build_out(SCAN_OUT);
359
360 DEBUG_JTAG_IO(".");
361 ublast_queue_byte(out);
362 }
363
364 /**
365 * ublast_clock_tdi - Output a TDI with bitbang mode
366 * @tdi: the TDI bit to be shifted out
367 * @type: scan type (ie. does a readback of TDO is required)
368 *
369 * Output a TDI bit and assert clock to push it into the JTAG device :
370 * - writing out TCK=0, TMS=<old_state>=0, TDI=<tdi>
371 * - writing out TCK=1, TMS=<new_state>, TDI=<tdi> which triggers the JTAG
372 * device aquiring the data.
373 *
374 * If a TDO is to be read back, the required read is requested (bitbang mode),
375 * and the USB Blaster will send back a byte with bit0 reprensenting the TDO.
376 */
377 static void ublast_clock_tdi(int tdi, enum scan_type type)
378 {
379 uint8_t out;
380
381 DEBUG_JTAG_IO("(tdi=%d)", !!tdi);
382 info.tdi = !!tdi;
383
384 out = ublast_build_out(SCAN_OUT);
385 ublast_queue_byte(out);
386
387 out = ublast_build_out(type);
388 ublast_queue_byte(out | TCK);
389 }
390
391 /**
392 * ublast_clock_tdi_flip_tms - Output a TDI with bitbang mode, change JTAG state
393 * @tdi: the TDI bit to be shifted out
394 * @type: scan type (ie. does a readback of TDO is required)
395 *
396 * This function is the same as ublast_clock_tdi(), but it changes also the TMS
397 * while outputing the TDI. This should be the last TDI output of a TDI
398 * sequence, which will change state from :
399 * - IRSHIFT -> IREXIT1
400 * - or DRSHIFT -> DREXIT1
401 */
402 static void ublast_clock_tdi_flip_tms(int tdi, enum scan_type type)
403 {
404 uint8_t out;
405
406 DEBUG_JTAG_IO("(tdi=%d)", !!tdi);
407 info.tdi = !!tdi;
408 info.tms = !info.tms;
409
410 out = ublast_build_out(SCAN_OUT);
411 ublast_queue_byte(out);
412
413 out = ublast_build_out(type);
414 ublast_queue_byte(out | TCK);
415
416 out = ublast_build_out(SCAN_OUT);
417 ublast_queue_byte(out);
418 }
419
420 /**
421 * ublast_queue_bytes - queue bytes for the USB Blaster
422 * @bytes: byte array
423 * @nb_bytes: number of bytes
424 *
425 * Queues bytes to be sent to the USB Blaster. The bytes are not
426 * actually sent, but stored in a buffer. The write is performed once
427 * the buffer is filled, or if an explicit ublast_flush_buffer() is called.
428 */
429 static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
430 {
431 if (info.bufidx + nb_bytes > BUF_LEN) {
432 LOG_ERROR("buggy code, should never queue more that %d bytes",
433 info.bufidx + nb_bytes);
434 exit(-1);
435 }
436 DEBUG_JTAG_IO("(nb_bytes=%d, bytes=[0x%02x, ...])", nb_bytes,
437 bytes ? bytes[0] : 0);
438 if (bytes)
439 memcpy(&info.buf[info.bufidx], bytes, nb_bytes);
440 else
441 memset(&info.buf[info.bufidx], 0, nb_bytes);
442 info.bufidx += nb_bytes;
443 if (nb_buf_remaining() == 0)
444 ublast_flush_buffer();
445 }
446
447 /**
448 * ublast_tms_seq - write a TMS sequence transition to JTAG
449 * @bits: TMS bits to be written (bit0, bit1 .. bitN)
450 * @nb_bits: number of TMS bits (between 1 and 8)
451 *
452 * Write a serie of TMS transitions, where each transition consists in :
453 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
454 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
455 * The function ensures that at the end of the sequence, the clock (TCK) is put
456 * low.
457 */
458 static void ublast_tms_seq(const uint8_t *bits, int nb_bits)
459 {
460 int i;
461
462 DEBUG_JTAG_IO("(bits=%02x..., nb_bits=%d)", bits[0], nb_bits);
463 for (i = 0; i < nb_bits; i++)
464 ublast_clock_tms((bits[i / 8] >> (i % 8)) & 0x01);
465 ublast_idle_clock();
466 }
467
468 /**
469 * ublast_tms - write a tms command
470 * @cmd: tms command
471 */
472 static void ublast_tms(struct tms_command *cmd)
473 {
474 DEBUG_JTAG_IO("(num_bits=%d)", cmd->num_bits);
475 ublast_tms_seq(cmd->bits, cmd->num_bits);
476 }
477
478 /**
479 * ublast_path_move - write a TMS sequence transition to JTAG
480 * @cmd: path transition
481 *
482 * Write a serie of TMS transitions, where each transition consists in :
483 * - writing out TCK=0, TMS=<new_state>, TDI=<???>
484 * - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
485 * The function ensures that at the end of the sequence, the clock (TCK) is put
486 * low.
487 */
488 static void ublast_path_move(struct pathmove_command *cmd)
489 {
490 int i;
491
492 DEBUG_JTAG_IO("(num_states=%d, last_state=%d)",
493 cmd->num_states, cmd->path[cmd->num_states - 1]);
494 for (i = 0; i < cmd->num_states; i++) {
495 if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
496 ublast_clock_tms(0);
497 if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
498 ublast_clock_tms(1);
499 tap_set_state(cmd->path[i]);
500 }
501 ublast_idle_clock();
502 }
503
504 /**
505 * ublast_state_move - move JTAG state to the target state
506 * @state: the target state
507 *
508 * Input the correct TMS sequence to the JTAG TAP so that we end up in the
509 * target state. This assumes the current state (tap_get_state()) is correct.
510 */
511 static void ublast_state_move(tap_state_t state)
512 {
513 uint8_t tms_scan;
514 int tms_len;
515
516 DEBUG_JTAG_IO("(from %s to %s)", tap_state_name(tap_get_state()),
517 tap_state_name(state));
518 if (tap_get_state() == state)
519 return;
520 tms_scan = tap_get_tms_path(tap_get_state(), state);
521 tms_len = tap_get_tms_path_len(tap_get_state(), state);
522 ublast_tms_seq(&tms_scan, tms_len);
523 tap_set_state(state);
524 }
525
526 /**
527 * ublast_read_byteshifted_tdos - read TDO of byteshift writes
528 * @buf: the buffer to store the bits
529 * @nb_bits: the number of bits
530 *
531 * Reads back from USB Blaster TDO bits, triggered by a 'byteshift write', ie. eight
532 * bits per received byte from USB interface, and store them in buffer.
533 *
534 * As the USB blaster stores the TDO bits in LSB (ie. first bit in (byte0,
535 * bit0), second bit in (byte0, bit1), ...), which is what we want to return,
536 * simply read bytes from USB interface and store them.
537 *
538 * Returns ERROR_OK if OK, ERROR_xxx if a read error occured
539 */
540 static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
541 {
542 unsigned int retlen;
543 int ret = ERROR_OK;
544
545 DEBUG_JTAG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bytes * 8);
546 ublast_flush_buffer();
547 while (ret == ERROR_OK && nb_bytes > 0) {
548 ret = ublast_buf_read(buf, nb_bytes, &retlen);
549 nb_bytes -= retlen;
550 }
551 return ret;
552 }
553
554 /**
555 * ublast_read_bitbang_tdos - read TDO of bitbang writes
556 * @buf: the buffer to store the bits
557 * @nb_bits: the number of bits
558 *
559 * Reads back from USB Blaster TDO bits, triggered by a 'bitbang write', ie. one
560 * bit per received byte from USB interface, and store them in buffer, where :
561 * - first bit is stored in byte0, bit0 (LSB)
562 * - second bit is stored in byte0, bit 1
563 * ...
564 * - eight bit is sotred in byte0, bit 7
565 * - ninth bit is sotred in byte1, bit 0
566 * - etc ...
567 *
568 * Returns ERROR_OK if OK, ERROR_xxx if a read error occured
569 */
570 static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
571 {
572 int nb1 = nb_bits;
573 int i, ret = ERROR_OK;
574 unsigned int retlen;
575 uint8_t tmp[8];
576
577 DEBUG_JTAG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bits);
578
579 /*
580 * Ensure all previous bitbang writes were issued to the dongle, so that
581 * it returns back the read values.
582 */
583 ublast_flush_buffer();
584
585 ret = ublast_buf_read(tmp, nb1, &retlen);
586 for (i = 0; ret == ERROR_OK && i < nb1; i++)
587 if (tmp[i] & READ_TDO)
588 *buf |= (1 << i);
589 else
590 *buf &= ~(1 << i);
591 return ret;
592 }
593
594 /**
595 * ublast_queue_tdi - short description
596 * @bits: bits to be queued on TDI (or NULL if 0 are to be queued)
597 * @nb_bits: number of bits
598 * @scan: scan type (ie. if TDO read back is required or not)
599 *
600 * Outputs a serie of TDI bits on TDI.
601 * As a side effect, the last TDI bit is sent along a TMS=1, and triggers a JTAG
602 * TAP state shift if input bits were non NULL.
603 *
604 * In order to not saturate the USB Blaster queues, this method reads back TDO
605 * if the scan type requests it, and stores them back in bits.
606 *
607 * As a side note, the state of TCK when entering this function *must* be
608 * low. This is because byteshift mode outputs TDI on rising TCK and reads TDO
609 * on falling TCK if and only if TCK is low before queuing byteshift mode bytes.
610 * If TCK was high, the USB blaster will queue TDI on falling edge, and read TDO
611 * on rising edge !!!
612 */
613 static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
614 {
615 int nb8 = nb_bits / 8;
616 int nb1 = nb_bits % 8;
617 int nbfree_in_packet, i, trans = 0, read_tdos;
618 uint8_t *tdos = calloc(1, nb_bits / 8 + 1);
619 static uint8_t byte0[BUF_LEN];
620
621 /*
622 * As the last TDI bit should always be output in bitbang mode in order
623 * to activate the TMS=1 transition to EXIT_?R state. Therefore a
624 * situation where nb_bits is a multiple of 8 is handled as follows:
625 * - the number of TDI shifted out in "byteshift mode" is 8 less than
626 * nb_bits
627 * - nb1 = 8
628 * This ensures that nb1 is never 0, and allows the TMS transition.
629 */
630 if (nb8 > 0 && nb1 == 0) {
631 nb8--;
632 nb1 = 8;
633 }
634
635 read_tdos = (scan == SCAN_IN || scan == SCAN_IO);
636 for (i = 0; i < nb8; i += trans) {
637 /*
638 * Calculate number of bytes to fill USB packet of size MAX_PACKET_SIZE
639 */
640 nbfree_in_packet = (MAX_PACKET_SIZE - (info.bufidx%MAX_PACKET_SIZE));
641 trans = MIN(nbfree_in_packet - 1, nb8 - i);
642
643 /*
644 * Queue a byte-shift mode transmission, with as many bytes as
645 * is possible with regard to :
646 * - current filling level of write buffer
647 * - remaining bytes to write in byte-shift mode
648 */
649 if (read_tdos)
650 ublast_queue_byte(SHMODE | READ | trans);
651 else
652 ublast_queue_byte(SHMODE | trans);
653 if (bits)
654 ublast_queue_bytes(&bits[i], trans);
655 else
656 ublast_queue_bytes(byte0, trans);
657 if (read_tdos) {
658 if (info.flags & COPY_TDO_BUFFER)
659 ublast_queue_byte(CMD_COPY_TDO_BUFFER);
660 ublast_read_byteshifted_tdos(&tdos[i], trans);
661 }
662 }
663
664 /*
665 * Queue the remaining TDI bits in bitbang mode.
666 */
667 for (i = 0; i < nb1; i++) {
668 int tdi = bits ? bits[nb8 + i / 8] & (1 << i) : 0;
669 if (bits && i == nb1 - 1)
670 ublast_clock_tdi_flip_tms(tdi, scan);
671 else
672 ublast_clock_tdi(tdi, scan);
673 }
674 if (nb1 && read_tdos) {
675 if (info.flags & COPY_TDO_BUFFER)
676 ublast_queue_byte(CMD_COPY_TDO_BUFFER);
677 ublast_read_bitbang_tdos(&tdos[nb8], nb1);
678 }
679
680 if (bits)
681 memcpy(bits, tdos, DIV_ROUND_UP(nb_bits, 8));
682 free(tdos);
683
684 /*
685 * Ensure clock is in lower state
686 */
687 ublast_idle_clock();
688 }
689
690 static void ublast_runtest(int cycles, tap_state_t state)
691 {
692 DEBUG_JTAG_IO("%s(cycles=%i, end_state=%d)", __func__, cycles, state);
693
694 ublast_state_move(TAP_IDLE);
695 ublast_queue_tdi(NULL, cycles, SCAN_OUT);
696 ublast_state_move(state);
697 }
698
699 static void ublast_stableclocks(int cycles)
700 {
701 DEBUG_JTAG_IO("%s(cycles=%i)", __func__, cycles);
702 ublast_queue_tdi(NULL, cycles, SCAN_OUT);
703 }
704
705 /**
706 * ublast_scan - launches a DR-scan or IR-scan
707 * @cmd: the command to launch
708 *
709 * Launch a JTAG IR-scan or DR-scan
710 *
711 * Returns ERROR_OK if OK, ERROR_xxx if a read/write error occured.
712 */
713 static int ublast_scan(struct scan_command *cmd)
714 {
715 int scan_bits;
716 uint8_t *buf = NULL;
717 enum scan_type type;
718 int ret = ERROR_OK;
719 static const char * const type2str[] = { "", "SCAN_IN", "SCAN_OUT", "SCAN_IO" };
720 char *log_buf = NULL;
721
722 type = jtag_scan_type(cmd);
723 scan_bits = jtag_build_buffer(cmd, &buf);
724
725 if (cmd->ir_scan)
726 ublast_state_move(TAP_IRSHIFT);
727 else
728 ublast_state_move(TAP_DRSHIFT);
729
730 log_buf = hexdump(buf, DIV_ROUND_UP(scan_bits, 8));
731 DEBUG_JTAG_IO("%s(scan=%s, type=%s, bits=%d, buf=[%s], end_state=%d)", __func__,
732 cmd->ir_scan ? "IRSCAN" : "DRSCAN",
733 type2str[type],
734 scan_bits, log_buf, cmd->end_state);
735 free(log_buf);
736
737 ublast_queue_tdi(buf, scan_bits, type);
738
739 /*
740 * As our JTAG is in an unstable state (IREXIT1 or DREXIT1), move it
741 * forward to a stable IRPAUSE or DRPAUSE.
742 */
743 ublast_clock_tms(0);
744 if (cmd->ir_scan)
745 tap_set_state(TAP_IRPAUSE);
746 else
747 tap_set_state(TAP_DRPAUSE);
748
749 ret = jtag_read_buffer(buf, cmd);
750 if (buf)
751 free(buf);
752 ublast_state_move(cmd->end_state);
753 return ret;
754 }
755
756 static void ublast_usleep(int us)
757 {
758 DEBUG_JTAG_IO("%s(us=%d)", __func__, us);
759 jtag_sleep(us);
760 }
761
762 static void ublast_initial_wipeout(void)
763 {
764 static uint8_t tms_reset = 0xff;
765 uint8_t out_value;
766 uint32_t retlen;
767 int i;
768
769 out_value = ublast_build_out(SCAN_OUT);
770 for (i = 0; i < BUF_LEN; i++)
771 info.buf[i] = out_value | ((i % 2) ? TCK : 0);
772
773 /*
774 * Flush USB-Blaster queue fifos
775 * - empty the write FIFO (128 bytes)
776 * - empty the read FIFO (384 bytes)
777 */
778 ublast_buf_write(info.buf, BUF_LEN, &retlen);
779 /*
780 * Put JTAG in RESET state (five 1 on TMS)
781 */
782 ublast_tms_seq(&tms_reset, 5);
783 tap_set_state(TAP_RESET);
784 }
785
786 static int ublast_execute_queue(void)
787 {
788 struct jtag_command *cmd;
789 static int first_call = 1;
790 int ret = ERROR_OK;
791
792 if (first_call) {
793 first_call--;
794 ublast_initial_wipeout();
795 }
796
797 for (cmd = jtag_command_queue; ret == ERROR_OK && cmd != NULL;
798 cmd = cmd->next) {
799 switch (cmd->type) {
800 case JTAG_RESET:
801 ublast_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
802 break;
803 case JTAG_RUNTEST:
804 ublast_runtest(cmd->cmd.runtest->num_cycles,
805 cmd->cmd.runtest->end_state);
806 break;
807 case JTAG_STABLECLOCKS:
808 ublast_stableclocks(cmd->cmd.stableclocks->num_cycles);
809 break;
810 case JTAG_TLR_RESET:
811 ublast_state_move(cmd->cmd.statemove->end_state);
812 break;
813 case JTAG_PATHMOVE:
814 ublast_path_move(cmd->cmd.pathmove);
815 break;
816 case JTAG_TMS:
817 ublast_tms(cmd->cmd.tms);
818 break;
819 case JTAG_SLEEP:
820 ublast_usleep(cmd->cmd.sleep->us);
821 break;
822 case JTAG_SCAN:
823 ret = ublast_scan(cmd->cmd.scan);
824 break;
825 }
826 }
827
828 ublast_flush_buffer();
829 return ret;
830 }
831
832 /**
833 * ublast_init - Initialize the Altera device
834 *
835 * Initialize the device :
836 * - open the USB device
837 * - pretend it's initialized while actual init is delayed until first jtag command
838 *
839 * Returns ERROR_OK if USB device found, error if not.
840 */
841 static int ublast_init(void)
842 {
843 int ret, i;
844
845 if (info.lowlevel_name) {
846 for (i = 0; lowlevel_drivers_map[i].name; i++)
847 if (!strcmp(lowlevel_drivers_map[i].name, info.lowlevel_name))
848 break;
849 if (lowlevel_drivers_map[i].name)
850 info.drv = lowlevel_drivers_map[i].drv_register();
851 if (!info.drv) {
852 LOG_ERROR("no lowlevel driver found for %s or lowlevel driver opening error",
853 info.lowlevel_name);
854 return ERROR_JTAG_DEVICE_ERROR;
855 }
856 } else {
857 LOG_INFO("No lowlevel driver configured, will try them all");
858 for (i = 0; !info.drv && lowlevel_drivers_map[i].name; i++)
859 info.drv = lowlevel_drivers_map[i].drv_register();
860 if (!info.drv) {
861 LOG_ERROR("no lowlevel driver found");
862 return ERROR_JTAG_DEVICE_ERROR;
863 }
864 info.lowlevel_name = strdup(lowlevel_drivers_map[i-1].name);
865 }
866
867 /*
868 * Register the lowlevel driver
869 */
870 info.drv->ublast_vid = info.ublast_vid;
871 info.drv->ublast_pid = info.ublast_pid;
872 info.drv->ublast_vid_uninit = info.ublast_vid_uninit;
873 info.drv->ublast_pid_uninit = info.ublast_pid_uninit;
874 info.drv->ublast_device_desc = info.ublast_device_desc;
875 info.drv->firmware_path = info.firmware_path;
876
877 info.flags |= info.drv->flags;
878
879 ret = info.drv->open(info.drv);
880
881 /*
882 * Let lie here : the TAP is in an unknown state, but the first
883 * execute_queue() will trigger a ublast_initial_wipeout(), which will
884 * put the TAP in RESET.
885 */
886 tap_set_state(TAP_RESET);
887 return ret;
888 }
889
890 /**
891 * ublast_quit - Release the Altera device
892 *
893 * Releases the device :
894 * - put the device pins in 'high impedance' mode
895 * - close the USB device
896 *
897 * Returns always ERROR_OK
898 */
899 static int ublast_quit(void)
900 {
901 uint8_t byte0 = 0;
902 unsigned int retlen;
903
904 ublast_buf_write(&byte0, 1, &retlen);
905 return info.drv->close(info.drv);
906 }
907
908 COMMAND_HANDLER(ublast_handle_device_desc_command)
909 {
910 if (CMD_ARGC != 1)
911 return ERROR_COMMAND_SYNTAX_ERROR;
912
913 info.ublast_device_desc = strdup(CMD_ARGV[0]);
914
915 return ERROR_OK;
916 }
917
918 COMMAND_HANDLER(ublast_handle_vid_pid_command)
919 {
920 if (CMD_ARGC > 4) {
921 LOG_WARNING("ignoring extra IDs in ublast_vid_pid "
922 "(maximum is 2 pairs)");
923 CMD_ARGC = 4;
924 }
925
926 if (CMD_ARGC >= 2) {
927 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], info.ublast_vid);
928 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], info.ublast_pid);
929 } else {
930 LOG_WARNING("incomplete ublast_vid_pid configuration");
931 }
932
933 if (CMD_ARGC == 4) {
934 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[2], info.ublast_vid_uninit);
935 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[3], info.ublast_pid_uninit);
936 } else {
937 LOG_WARNING("incomplete ublast_vid_pid configuration");
938 }
939
940 return ERROR_OK;
941 }
942
943 COMMAND_HANDLER(ublast_handle_pin_command)
944 {
945 uint8_t out_value;
946 const char * const pin_name = CMD_ARGV[0];
947 enum gpio_steer *steer = NULL;
948 static const char * const pin_val_str[] = {
949 [FIXED_0] = "0",
950 [FIXED_1] = "1",
951 [SRST] = "SRST driven",
952 [TRST] = "TRST driven",
953 };
954
955 if (CMD_ARGC > 2) {
956 LOG_ERROR("%s takes exactly one or two arguments", CMD_NAME);
957 return ERROR_COMMAND_SYNTAX_ERROR;
958 }
959
960 if (!strcmp(pin_name, "pin6"))
961 steer = &info.pin6;
962 if (!strcmp(pin_name, "pin8"))
963 steer = &info.pin8;
964 if (!steer) {
965 LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
966 CMD_NAME);
967 return ERROR_COMMAND_SYNTAX_ERROR;
968 }
969
970 if (CMD_ARGC == 1) {
971 LOG_INFO("%s: %s is set as %s\n", CMD_NAME, pin_name,
972 pin_val_str[*steer]);
973 }
974
975 if (CMD_ARGC == 2) {
976 const char * const pin_value = CMD_ARGV[1];
977 char val = pin_value[0];
978
979 if (strlen(pin_value) > 1)
980 val = '?';
981 switch (tolower((unsigned char)val)) {
982 case '0':
983 *steer = FIXED_0;
984 break;
985 case '1':
986 *steer = FIXED_1;
987 break;
988 case 't':
989 *steer = TRST;
990 break;
991 case 's':
992 *steer = SRST;
993 break;
994 default:
995 LOG_ERROR("%s: pin value must be 0, 1, s (SRST) or t (TRST)",
996 pin_value);
997 return ERROR_COMMAND_SYNTAX_ERROR;
998 }
999
1000 if (info.drv) {
1001 out_value = ublast_build_out(SCAN_OUT);
1002 ublast_queue_byte(out_value);
1003 ublast_flush_buffer();
1004 }
1005 }
1006 return ERROR_OK;
1007 }
1008
1009 COMMAND_HANDLER(ublast_handle_lowlevel_drv_command)
1010 {
1011 if (CMD_ARGC != 1)
1012 return ERROR_COMMAND_SYNTAX_ERROR;
1013
1014 info.lowlevel_name = strdup(CMD_ARGV[0]);
1015
1016 return ERROR_OK;
1017 }
1018
1019 COMMAND_HANDLER(ublast_firmware_command)
1020 {
1021 if (CMD_ARGC != 1)
1022 return ERROR_COMMAND_SYNTAX_ERROR;
1023
1024 info.firmware_path = strdup(CMD_ARGV[0]);
1025
1026 return ERROR_OK;
1027 }
1028
1029
1030 static const struct command_registration ublast_command_handlers[] = {
1031 {
1032 .name = "usb_blaster_device_desc",
1033 .handler = ublast_handle_device_desc_command,
1034 .mode = COMMAND_CONFIG,
1035 .help = "set the USB device description of the USB-Blaster",
1036 .usage = "description-string",
1037 },
1038 {
1039 .name = "usb_blaster_vid_pid",
1040 .handler = ublast_handle_vid_pid_command,
1041 .mode = COMMAND_CONFIG,
1042 .help = "the vendor ID and product ID of the USB-Blaster and " \
1043 "vendor ID and product ID of the uninitialized device " \
1044 "for USB-Blaster II",
1045 .usage = "vid pid vid_uninit pid_uninit",
1046 },
1047 {
1048 .name = "usb_blaster_lowlevel_driver",
1049 .handler = ublast_handle_lowlevel_drv_command,
1050 .mode = COMMAND_CONFIG,
1051 .help = "set the lowlevel access for the USB Blaster (ftdi, ftd2xx, ublast2)",
1052 .usage = "(ftdi|ftd2xx|ublast2)",
1053 },
1054 {
1055 .name = "usb_blaster_pin",
1056 .handler = ublast_handle_pin_command,
1057 .mode = COMMAND_ANY,
1058 .help = "show or set pin state for the unused GPIO pins",
1059 .usage = "(pin6|pin8) (0|1|s|t)",
1060 },
1061 {
1062 .name = "usb_blaster_firmware",
1063 .handler = &ublast_firmware_command,
1064 .mode = COMMAND_CONFIG,
1065 .help = "configure the USB-Blaster II firmware location",
1066 .usage = "path/to/blaster_xxxx.hex",
1067 },
1068 COMMAND_REGISTRATION_DONE
1069 };
1070
1071 struct jtag_interface usb_blaster_interface = {
1072 .name = "usb_blaster",
1073 .commands = ublast_command_handlers,
1074 .supported = DEBUG_CAP_TMS_SEQ,
1075
1076 .execute_queue = ublast_execute_queue,
1077 .init = ublast_init,
1078 .quit = ublast_quit,
1079 };