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