You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1089 lines
29 KiB

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