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.
 
 
 
 
 
 

595 lines
17 KiB

  1. /***************************************************************************
  2. * Driver for USB-JTAG, Altera USB-Blaster and compatibles *
  3. * Original code from Kolja Waschk's USB-JTAG project *
  4. * (http://www.ixo.de/info/usb_jtag/). *
  5. * Some updates by Anthony Liu (2006). *
  6. * Minor updates and cleanup by Catalin Patulea (2009). *
  7. * Speed updates by Ali Lown (2011). *
  8. * *
  9. * Copyright (C) 2011 Ali Lown *
  10. * ali@lown.me.uk *
  11. * *
  12. * Copyright (C) 2009 Catalin Patulea *
  13. * cat@vv.carleton.ca *
  14. * *
  15. * Copyright (C) 2006 Kolja Waschk *
  16. * usbjtag@ixo.de *
  17. * *
  18. * Based on ft2232.c and bitbang.c, *
  19. * Copyright (C) 2004,2006 by Dominic Rath *
  20. * *
  21. * This program is free software; you can redistribute it and/or modify *
  22. * it under the terms of the GNU General Public License as published by *
  23. * the Free Software Foundation; either version 2 of the License, or *
  24. * (at your option) any later version. *
  25. * *
  26. * This program is distributed in the hope that it will be useful, *
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  29. * GNU General Public License for more details. *
  30. * *
  31. * You should have received a copy of the GNU General Public License *
  32. * along with this program; if not, write to the *
  33. * Free Software Foundation, Inc., *
  34. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  35. ***************************************************************************/
  36. /*
  37. * The following information is originally from Kolja Waschk's USB-JTAG,
  38. * where it was obtained by reverse engineering an Altera USB-Blaster.
  39. * See http://www.ixo.de/info/usb_jtag/ for USB-Blaster block diagram and
  40. * usb_jtag-20080705-1200.zip#usb_jtag/host/openocd for protocol.
  41. *
  42. * The same information is also on the UrJTAG mediawiki, with some additional
  43. * notes on bits marked as "unknown" by usb_jtag.
  44. * (http://sourceforge.net/apps/mediawiki/urjtag/index.php?
  45. * title=Cable_Altera_USB-Blaster)
  46. *
  47. * USB-JTAG, Altera USB-Blaster and compatibles are typically implemented as
  48. * an FTDIChip FT245 followed by a CPLD which handles a two-mode protocol:
  49. *
  50. * _________
  51. * | |
  52. * | AT93C46 |
  53. * |_________|
  54. * __|__________ _________
  55. * | | | |
  56. * USB__| FTDI 245BM |__| EPM7064 |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
  57. * |_____________| |_________|
  58. * __|__________ _|___________
  59. * | | | |
  60. * | 6 MHz XTAL | | 24 MHz Osc. |
  61. * |_____________| |_____________|
  62. *
  63. * Protocol details are given in the code below.
  64. *
  65. * It is also possible to emulate this configuration using a single-chip USB
  66. * controller like the Cypress FX2 (again, see usb_jtag for details).
  67. */
  68. #ifdef HAVE_CONFIG_H
  69. #include "config.h"
  70. #endif
  71. #if IS_CYGWIN == 1
  72. #include "windows.h"
  73. #undef LOG_ERROR
  74. #endif
  75. /* project specific includes */
  76. #include <jtag/interface.h>
  77. #include <jtag/commands.h>
  78. #include <helper/time_support.h>
  79. /* system includes */
  80. #include <string.h>
  81. #include <stdlib.h>
  82. #include <unistd.h>
  83. #include "bitbang.h"
  84. #if (BUILD_USB_BLASTER_FTD2XX == 1 && BUILD_USB_BLASTER_LIBFTDI == 1)
  85. #error "BUILD_USB_BLASTER_FTD2XX && BUILD_USB_BLASTER_LIBFTDI "
  86. "are mutually exclusive"
  87. #elif (BUILD_USB_BLASTER_FTD2XX != 1 && BUILD_USB_BLASTER_LIBFTDI != 1)
  88. #error "BUILD_USB_BLASTER_FTD2XX || BUILD_USB_BLASTER_LIBFTDI must be chosen"
  89. #endif
  90. /* USB_BLASTER access library includes */
  91. #if BUILD_USB_BLASTER_FTD2XX == 1
  92. #include <ftd2xx.h>
  93. #include "ftd2xx_common.h"
  94. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  95. #include <ftdi.h>
  96. #endif
  97. #include <sys/time.h>
  98. #include <time.h>
  99. static char *usb_blaster_device_desc;
  100. static uint16_t usb_blaster_vid = 0x09fb; /* Altera */
  101. static uint16_t usb_blaster_pid = 0x6001; /* USB-Blaster */
  102. /* last output byte in simple bit banging (legacy) mode */
  103. static uint8_t out_value;
  104. /* global output buffer for bit banging */
  105. #define BUF_LEN 64 /* Size of EP1 */
  106. static uint8_t out_buffer[BUF_LEN];
  107. static uint16_t out_count;
  108. #if BUILD_USB_BLASTER_FTD2XX == 1
  109. static FT_HANDLE ftdih;
  110. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  111. static struct ftdi_context ftdic;
  112. #endif
  113. static int usb_blaster_buf_write(
  114. uint8_t *buf, int size, uint32_t *bytes_written)
  115. {
  116. #if BUILD_USB_BLASTER_FTD2XX == 1
  117. FT_STATUS status;
  118. DWORD dw_bytes_written;
  119. #ifdef _DEBUG_JTAG_IO_
  120. LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf[0], size);
  121. #endif
  122. status = FT_Write(ftdih, buf, size, &dw_bytes_written);
  123. if (status != FT_OK) {
  124. *bytes_written = dw_bytes_written;
  125. LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
  126. return ERROR_JTAG_DEVICE_ERROR;
  127. }
  128. *bytes_written = dw_bytes_written;
  129. return ERROR_OK;
  130. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  131. int retval;
  132. #ifdef _DEBUG_JTAG_IO_
  133. LOG_DEBUG("usb_blaster_buf_write %02X (%d)", buf[0], size);
  134. #endif
  135. retval = ftdi_write_data(&ftdic, buf, size);
  136. if (retval < 0) {
  137. *bytes_written = 0;
  138. LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
  139. return ERROR_JTAG_DEVICE_ERROR;
  140. }
  141. *bytes_written = retval;
  142. return ERROR_OK;
  143. #endif
  144. }
  145. static int usb_blaster_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read)
  146. {
  147. #if BUILD_USB_BLASTER_FTD2XX == 1
  148. DWORD dw_bytes_read;
  149. FT_STATUS status;
  150. status = FT_Read(ftdih, buf, size, &dw_bytes_read);
  151. if (status != FT_OK) {
  152. *bytes_read = dw_bytes_read;
  153. LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
  154. return ERROR_JTAG_DEVICE_ERROR;
  155. }
  156. #ifdef _DEBUG_JTAG_IO_
  157. LOG_DEBUG("usb_blaster_buf_read %02X (%" PRIu32 ")", buf[0], dw_bytes_read);
  158. #endif
  159. *bytes_read = dw_bytes_read;
  160. return ERROR_OK;
  161. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  162. int retval;
  163. int timeout = 100;
  164. *bytes_read = 0;
  165. while ((*bytes_read < size) && timeout--) {
  166. retval = ftdi_read_data(&ftdic, buf + *bytes_read,
  167. size - *bytes_read);
  168. if (retval < 0) {
  169. *bytes_read = 0;
  170. LOG_ERROR("ftdi_read_data: %s",
  171. ftdi_get_error_string(&ftdic));
  172. return ERROR_JTAG_DEVICE_ERROR;
  173. }
  174. *bytes_read += retval;
  175. }
  176. #ifdef _DEBUG_JTAG_IO_
  177. LOG_DEBUG("usb_blaster_buf_read %02X (%d)", buf[0], *bytes_read);
  178. #endif
  179. return ERROR_OK;
  180. #endif
  181. }
  182. /* The following code doesn't fully utilize the possibilities of the
  183. * USB-Blaster. It only buffers data up to the maximum packet size of 64 bytes.
  184. *
  185. * Actually, the USB-Blaster offers a byte-shift mode to transmit up to 504 data
  186. * bits (bidirectional) in a single USB packet. A header byte has to be sent as
  187. * the first byte in a packet with the following meaning:
  188. *
  189. * Bit 7 (0x80): Must be set to indicate byte-shift mode.
  190. * Bit 6 (0x40): If set, the USB-Blaster will also read data, not just write.
  191. * Bit 5..0: Define the number N of following bytes
  192. *
  193. * All N following bytes will then be clocked out serially on TDI. If Bit 6 was
  194. * set, it will afterwards return N bytes with TDO data read while clocking out
  195. * the TDI data. LSB of the first byte after the header byte will appear first
  196. * on TDI.
  197. */
  198. /* Simple bit banging mode:
  199. *
  200. * Bit 7 (0x80): Must be zero (see byte-shift mode above)
  201. * Bit 6 (0x40): If set, you will receive a byte indicating the state of TDO
  202. * in return.
  203. * Bit 5 (0x20): Output Enable/LED.
  204. * Bit 4 (0x10): TDI Output.
  205. * Bit 3 (0x08): nCS Output (not used in JTAG mode).
  206. * Bit 2 (0x04): nCE Output (not used in JTAG mode).
  207. * Bit 1 (0x02): TMS Output.
  208. * Bit 0 (0x01): TCK Output.
  209. *
  210. * For transmitting a single data bit, you need to write two bytes. Up to 64
  211. * bytes can be combined in a single USB packet.
  212. * It isn't possible to read a data without transmitting data.
  213. */
  214. #define TCK (1 << 0)
  215. #define TMS (1 << 1)
  216. #define NCE (1 << 2)
  217. #define NCS (1 << 3)
  218. #define TDI (1 << 4)
  219. #define LED (1 << 5)
  220. #define READ (1 << 6)
  221. #define SHMODE (1 << 7)
  222. #define OTHERS ((1 << 2) | (1 << 3) | (1 << 5))
  223. #define READ_TDO (1 << 0)
  224. static void usb_blaster_write_databuffer(uint8_t *buf, uint16_t len)
  225. {
  226. uint32_t bytes_written;
  227. usb_blaster_buf_write(buf, len, &bytes_written);
  228. out_count = 0;
  229. #ifdef _DEBUG_JTAG_IO_
  230. LOG_DEBUG("---- WROTE %d", bytes_written);
  231. #endif
  232. }
  233. static void usb_blaster_addtowritebuffer(uint8_t value, bool forcewrite)
  234. {
  235. out_buffer[out_count] = value;
  236. out_count += 1;
  237. if (out_count == BUF_LEN || forcewrite)
  238. usb_blaster_write_databuffer(out_buffer, out_count);
  239. }
  240. static int usb_blaster_read_data(void)
  241. {
  242. int status;
  243. uint8_t buf[1];
  244. uint32_t bytes_read;
  245. if (out_count > 0)
  246. usb_blaster_write_databuffer(out_buffer, out_count);
  247. out_value |= READ;
  248. usb_blaster_addtowritebuffer(out_value, true);
  249. out_value &= ~READ;
  250. status = usb_blaster_buf_read(buf, 1, &bytes_read);
  251. if (status < 0)
  252. return 0;
  253. return !!(buf[0] & READ_TDO);
  254. }
  255. static void usb_blaster_write(int tck, int tms, int tdi)
  256. {
  257. #ifdef _DEBUG_JTAG_IO_
  258. LOG_DEBUG("---- usb_blaster_write(%d,%d,%d)", tck, tms, tdi);
  259. #endif
  260. out_value &= ~(TCK | TMS | TDI);
  261. if (tck)
  262. out_value |= TCK;
  263. if (tms)
  264. out_value |= TMS;
  265. if (tdi)
  266. out_value |= TDI;
  267. usb_blaster_addtowritebuffer(out_value, false);
  268. }
  269. static int usb_blaster_speed(int speed)
  270. {
  271. #if BUILD_USB_BLASTER_FTD2XX == 1
  272. LOG_DEBUG("TODO: usb_blaster_speed() isn't implemented for libftd2xx!");
  273. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  274. LOG_DEBUG("TODO: usb_blaster_speed() isn't optimally implemented!");
  275. /* TODO: libftdi's ftdi_set_baudrate chokes on high rates, use lowlevel
  276. * usb function instead! And additionally allow user to throttle.
  277. */
  278. if (ftdi_set_baudrate(&ftdic, 3000000 / 4) < 0) {
  279. LOG_ERROR("Can't set baud rate to max: %s",
  280. ftdi_get_error_string(&ftdic));
  281. return ERROR_JTAG_DEVICE_ERROR;
  282. }
  283. ;
  284. #endif
  285. return ERROR_OK;
  286. }
  287. static void usb_blaster_reset(int trst, int srst)
  288. {
  289. LOG_DEBUG("TODO: usb_blaster_reset(%d,%d) isn't implemented!",
  290. trst, srst);
  291. }
  292. static void usb_blaster_blink(int state)
  293. {
  294. out_value = 0x00;
  295. if (state)
  296. out_value |= LED;
  297. usb_blaster_addtowritebuffer(out_value, true);
  298. }
  299. static struct bitbang_interface usb_blaster_bitbang = {
  300. .read = usb_blaster_read_data,
  301. .write = usb_blaster_write,
  302. .reset = usb_blaster_reset,
  303. .blink = usb_blaster_blink,
  304. };
  305. static int usb_blaster_init(void)
  306. {
  307. uint8_t latency_timer;
  308. #if BUILD_USB_BLASTER_FTD2XX == 1
  309. FT_STATUS status;
  310. #endif
  311. #if BUILD_USB_BLASTER_FTD2XX == 1
  312. LOG_DEBUG("'usb_blaster' interface using FTD2XX");
  313. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  314. LOG_DEBUG("'usb_blaster' interface using libftdi");
  315. #endif
  316. #if BUILD_USB_BLASTER_FTD2XX == 1
  317. /* Open by device description */
  318. if (usb_blaster_device_desc == NULL) {
  319. LOG_WARNING("no usb_blaster device description specified, "
  320. "using default 'USB-Blaster'");
  321. usb_blaster_device_desc = "USB-Blaster";
  322. }
  323. #if IS_WIN32 == 0
  324. /* Add non-standard Vid/Pid to the linux driver */
  325. status = FT_SetVIDPID(usb_blaster_vid, usb_blaster_pid);
  326. if (status != FT_OK) {
  327. LOG_WARNING("couldn't add %4.4x:%4.4x",
  328. usb_blaster_vid, usb_blaster_pid);
  329. }
  330. #endif
  331. status = FT_OpenEx(usb_blaster_device_desc, FT_OPEN_BY_DESCRIPTION,
  332. &ftdih);
  333. if (status != FT_OK) {
  334. DWORD num_devices;
  335. LOG_ERROR("unable to open ftdi device: %s",
  336. ftd2xx_status_string(status));
  337. status = FT_ListDevices(&num_devices, NULL,
  338. FT_LIST_NUMBER_ONLY);
  339. if (status == FT_OK) {
  340. char **desc_array = malloc(sizeof(char *)
  341. * (num_devices + 1));
  342. unsigned int i;
  343. for (i = 0; i < num_devices; i++)
  344. desc_array[i] = malloc(64);
  345. desc_array[num_devices] = NULL;
  346. status = FT_ListDevices(desc_array, &num_devices,
  347. FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
  348. if (status == FT_OK) {
  349. LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
  350. for (i = 0; i < num_devices; i++)
  351. LOG_ERROR("%i: %s", i, desc_array[i]);
  352. }
  353. for (i = 0; i < num_devices; i++)
  354. free(desc_array[i]);
  355. free(desc_array);
  356. } else
  357. printf("ListDevices: NONE\n");
  358. return ERROR_JTAG_INIT_FAILED;
  359. }
  360. status = FT_SetLatencyTimer(ftdih, 2);
  361. if (status != FT_OK) {
  362. LOG_ERROR("unable to set latency timer: %s",
  363. ftd2xx_status_string(status));
  364. return ERROR_JTAG_INIT_FAILED;
  365. }
  366. status = FT_GetLatencyTimer(ftdih, &latency_timer);
  367. if (status != FT_OK) {
  368. LOG_ERROR("unable to get latency timer: %s",
  369. ftd2xx_status_string(status));
  370. return ERROR_JTAG_INIT_FAILED;
  371. }
  372. LOG_DEBUG("current latency timer: %i", latency_timer);
  373. status = FT_SetBitMode(ftdih, 0x00, 0);
  374. if (status != FT_OK) {
  375. LOG_ERROR("unable to disable bit i/o mode: %s",
  376. ftd2xx_status_string(status));
  377. return ERROR_JTAG_INIT_FAILED;
  378. }
  379. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  380. if (ftdi_init(&ftdic) < 0)
  381. return ERROR_JTAG_INIT_FAILED;
  382. /* context, vendor id, product id */
  383. if (ftdi_usb_open(&ftdic, usb_blaster_vid, usb_blaster_pid) < 0) {
  384. LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
  385. return ERROR_JTAG_INIT_FAILED;
  386. }
  387. if (ftdi_usb_reset(&ftdic) < 0) {
  388. LOG_ERROR("unable to reset ftdi device");
  389. return ERROR_JTAG_INIT_FAILED;
  390. }
  391. if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
  392. LOG_ERROR("unable to set latency timer");
  393. return ERROR_JTAG_INIT_FAILED;
  394. }
  395. if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0) {
  396. LOG_ERROR("unable to get latency timer");
  397. return ERROR_JTAG_INIT_FAILED;
  398. }
  399. LOG_DEBUG("current latency timer: %u", latency_timer);
  400. ftdi_disable_bitbang(&ftdic);
  401. #endif
  402. bitbang_interface = &usb_blaster_bitbang;
  403. #if 0
  404. #if BUILD_USB_BLASTER_FTD2XX == 1
  405. status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX);
  406. if (status != FT_OK) {
  407. LOG_ERROR("error purging ftd2xx device: %i", status);
  408. return ERROR_JTAG_INIT_FAILED;
  409. }
  410. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  411. if (ftdi_usb_purge_buffers(&ftdic) < 0) {
  412. LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  413. return ERROR_JTAG_INIT_FAILED;
  414. }
  415. #endif
  416. #endif
  417. return ERROR_OK;
  418. }
  419. static int usb_blaster_quit(void)
  420. {
  421. if (out_count > 0)
  422. usb_blaster_write_databuffer(out_buffer, out_count);
  423. #if BUILD_USB_BLASTER_FTD2XX == 1
  424. FT_STATUS status;
  425. status = FT_Close(ftdih);
  426. #elif BUILD_USB_BLASTER_LIBFTDI == 1
  427. ftdi_usb_close(&ftdic);
  428. ftdi_deinit(&ftdic);
  429. #endif
  430. return ERROR_OK;
  431. }
  432. COMMAND_HANDLER(usb_blaster_handle_device_desc_command)
  433. {
  434. if (CMD_ARGC == 1)
  435. usb_blaster_device_desc = strdup(CMD_ARGV[0]);
  436. else
  437. LOG_ERROR("require exactly one argument to "
  438. "usb_blaster_device_desc <description>");
  439. return ERROR_OK;
  440. }
  441. COMMAND_HANDLER(usb_blaster_handle_vid_pid_command)
  442. {
  443. if (CMD_ARGC > 2) {
  444. LOG_WARNING("ignoring extra IDs in usb_blaster_vid_pid "
  445. "(maximum is 1 pair)");
  446. CMD_ARGC = 2;
  447. }
  448. if (CMD_ARGC == 2) {
  449. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], usb_blaster_vid);
  450. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], usb_blaster_pid);
  451. } else
  452. LOG_WARNING("incomplete usb_blaster_vid_pid configuration");
  453. return ERROR_OK;
  454. }
  455. COMMAND_HANDLER(usb_blaster_handle_pin_command)
  456. {
  457. if (CMD_ARGC == 2) {
  458. const char *const pin_name = CMD_ARGV[0];
  459. uint8_t mask;
  460. unsigned int state;
  461. if (!strcmp(pin_name, "pin6"))
  462. mask = NCE;
  463. else if (!strcmp(pin_name, "pin8"))
  464. mask = NCS;
  465. else {
  466. LOG_ERROR("%s: pin name must be \"pin6\" or \"pin8\"",
  467. CMD_NAME);
  468. return ERROR_COMMAND_SYNTAX_ERROR;
  469. }
  470. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], state);
  471. if (state == 0) {
  472. out_value &= ~mask;
  473. usb_blaster_addtowritebuffer(out_value, true);
  474. } else if (state == 1) {
  475. out_value |= mask;
  476. usb_blaster_addtowritebuffer(out_value, true);
  477. } else {
  478. LOG_ERROR("%s: pin state must be 0 or 1", CMD_NAME);
  479. return ERROR_COMMAND_SYNTAX_ERROR;
  480. }
  481. return ERROR_OK;
  482. } else {
  483. LOG_ERROR("%s takes exactly two arguments", CMD_NAME);
  484. return ERROR_COMMAND_SYNTAX_ERROR;
  485. }
  486. }
  487. static const struct command_registration usb_blaster_command_handlers[] = {
  488. {
  489. .name = "usb_blaster_device_desc",
  490. .handler = usb_blaster_handle_device_desc_command,
  491. .mode = COMMAND_CONFIG,
  492. .help = "set the USB device description of the USB-Blaster",
  493. .usage = "description-string",
  494. },
  495. {
  496. .name = "usb_blaster_vid_pid",
  497. .handler = usb_blaster_handle_vid_pid_command,
  498. .mode = COMMAND_CONFIG,
  499. .help = "the vendor ID and product ID of the USB-Blaster",
  500. .usage = "vid pid",
  501. },
  502. {
  503. .name = "usb_blaster",
  504. .handler = usb_blaster_handle_pin_command,
  505. .mode = COMMAND_ANY,
  506. .help = "set pin state for the unused GPIO pins",
  507. .usage = "(pin6|pin8) (0|1)",
  508. },
  509. COMMAND_REGISTRATION_DONE
  510. };
  511. struct jtag_interface usb_blaster_interface = {
  512. .name = "usb_blaster",
  513. .commands = usb_blaster_command_handlers,
  514. .supported = DEBUG_CAP_TMS_SEQ,
  515. .execute_queue = bitbang_execute_queue,
  516. .speed = usb_blaster_speed,
  517. .init = usb_blaster_init,
  518. .quit = usb_blaster_quit,
  519. };