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.
 
 
 
 
 
 

2040 lines
49 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2004, 2006 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #if IS_CYGWIN == 1
  24. #include "windows.h"
  25. #undef ERROR
  26. #endif
  27. #include "replacements.h"
  28. /* project specific includes */
  29. #include "log.h"
  30. #include "types.h"
  31. #include "jtag.h"
  32. #include "configuration.h"
  33. #include "time_support.h"
  34. /* system includes */
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. /* FT2232 access library includes */
  39. #if BUILD_FT2232_FTD2XX == 1
  40. #include <ftd2xx.h>
  41. #elif BUILD_FT2232_LIBFTDI == 1
  42. #include <ftdi.h>
  43. #endif
  44. #include <sys/time.h>
  45. #include <time.h>
  46. /* enable this to debug io latency
  47. */
  48. #if 0
  49. #define _DEBUG_USB_IO_
  50. #endif
  51. /* enable this to debug communication
  52. */
  53. #if 0
  54. #define _DEBUG_USB_COMMS_
  55. #endif
  56. int ft2232_execute_queue(void);
  57. int ft2232_speed(int speed);
  58. int ft2232_register_commands(struct command_context_s *cmd_ctx);
  59. int ft2232_init(void);
  60. int ft2232_quit(void);
  61. int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  62. int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  63. int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  64. int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  65. char *ft2232_device_desc = NULL;
  66. char *ft2232_serial = NULL;
  67. char *ft2232_layout = NULL;
  68. #define MAX_USB_IDS 8
  69. /* vid = pid = 0 marks the end of the list */
  70. static u16 ft2232_vid[MAX_USB_IDS+1] = { 0x0403, 0 };
  71. static u16 ft2232_pid[MAX_USB_IDS+1] = { 0x6010, 0 };
  72. typedef struct ft2232_layout_s
  73. {
  74. char* name;
  75. int(*init)(void);
  76. void(*reset)(int trst, int srst);
  77. void(*blink)(void);
  78. } ft2232_layout_t;
  79. /* init procedures for supported layouts */
  80. int usbjtag_init(void);
  81. int jtagkey_init(void);
  82. int olimex_jtag_init(void);
  83. int m5960_init(void);
  84. int turtle_init(void);
  85. int comstick_init(void);
  86. /* reset procedures for supported layouts */
  87. void usbjtag_reset(int trst, int srst);
  88. void jtagkey_reset(int trst, int srst);
  89. void olimex_jtag_reset(int trst, int srst);
  90. void m5960_reset(int trst, int srst);
  91. void turtle_reset(int trst, int srst);
  92. void comstick_reset(int trst, int srst);
  93. /* blink procedures for layouts that support a blinking led */
  94. void olimex_jtag_blink(void);
  95. void turtle_jtag_blink(void);
  96. ft2232_layout_t ft2232_layouts[] =
  97. {
  98. {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
  99. {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
  100. {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
  101. {"oocdlink", jtagkey_init, jtagkey_reset, NULL},
  102. {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
  103. {"evb_lm3s811", usbjtag_init, usbjtag_reset, NULL},
  104. {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
  105. {"m5960", m5960_init, m5960_reset, NULL},
  106. {"turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink},
  107. {"comstick", comstick_init, comstick_reset, NULL},
  108. {NULL, NULL, NULL},
  109. };
  110. static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
  111. static ft2232_layout_t *layout;
  112. static u8 low_output = 0x0;
  113. static u8 low_direction = 0x0;
  114. static u8 high_output = 0x0;
  115. static u8 high_direction = 0x0;
  116. #if BUILD_FT2232_FTD2XX == 1
  117. static FT_HANDLE ftdih = NULL;
  118. #elif BUILD_FT2232_LIBFTDI == 1
  119. static struct ftdi_context ftdic;
  120. #endif
  121. static u8 *ft2232_buffer = NULL;
  122. static int ft2232_buffer_size = 0;
  123. static int ft2232_read_pointer = 0;
  124. static int ft2232_expect_read = 0;
  125. #define FT2232_BUFFER_SIZE 131072
  126. #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
  127. #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
  128. jtag_interface_t ft2232_interface =
  129. {
  130. .name = "ft2232",
  131. .execute_queue = ft2232_execute_queue,
  132. .support_pathmove = 1,
  133. .speed = ft2232_speed,
  134. .register_commands = ft2232_register_commands,
  135. .init = ft2232_init,
  136. .quit = ft2232_quit,
  137. };
  138. int ft2232_write(u8 *buf, int size, u32* bytes_written)
  139. {
  140. #if BUILD_FT2232_FTD2XX == 1
  141. FT_STATUS status;
  142. DWORD dw_bytes_written;
  143. if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
  144. {
  145. *bytes_written = dw_bytes_written;
  146. ERROR("FT_Write returned: %lu", status);
  147. return ERROR_JTAG_DEVICE_ERROR;
  148. }
  149. else
  150. {
  151. *bytes_written = dw_bytes_written;
  152. return ERROR_OK;
  153. }
  154. #elif BUILD_FT2232_LIBFTDI == 1
  155. int retval;
  156. if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
  157. {
  158. *bytes_written = 0;
  159. ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
  160. return ERROR_JTAG_DEVICE_ERROR;
  161. }
  162. else
  163. {
  164. *bytes_written = retval;
  165. return ERROR_OK;
  166. }
  167. #endif
  168. }
  169. int ft2232_read(u8* buf, int size, u32* bytes_read)
  170. {
  171. #if BUILD_FT2232_FTD2XX == 1
  172. DWORD dw_bytes_read;
  173. FT_STATUS status;
  174. int timeout = 5;
  175. *bytes_read = 0;
  176. while ((*bytes_read < size) && timeout--)
  177. {
  178. if ((status = FT_Read(ftdih, buf + *bytes_read, size -
  179. *bytes_read, &dw_bytes_read)) != FT_OK)
  180. {
  181. *bytes_read = 0;
  182. ERROR("FT_Read returned: %lu", status);
  183. return ERROR_JTAG_DEVICE_ERROR;
  184. }
  185. *bytes_read += dw_bytes_read;
  186. }
  187. #elif BUILD_FT2232_LIBFTDI == 1
  188. int retval;
  189. int timeout = 100;
  190. *bytes_read = 0;
  191. while ((*bytes_read < size) && timeout--)
  192. {
  193. if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
  194. {
  195. *bytes_read = 0;
  196. ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
  197. return ERROR_JTAG_DEVICE_ERROR;
  198. }
  199. *bytes_read += retval;
  200. }
  201. #endif
  202. if (*bytes_read < size)
  203. {
  204. ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)", *bytes_read, size);
  205. return ERROR_JTAG_DEVICE_ERROR;
  206. }
  207. return ERROR_OK;
  208. }
  209. int ft2232_speed(int speed)
  210. {
  211. u8 buf[3];
  212. int retval;
  213. u32 bytes_written;
  214. buf[0] = 0x86; /* command "set divisor" */
  215. buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
  216. buf[2] = (speed >> 8) & 0xff; /* valueH */
  217. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  218. if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  219. {
  220. ERROR("couldn't set FT2232 TCK speed");
  221. return retval;
  222. }
  223. return ERROR_OK;
  224. }
  225. int ft2232_register_commands(struct command_context_s *cmd_ctx)
  226. {
  227. register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
  228. COMMAND_CONFIG, NULL);
  229. register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
  230. COMMAND_CONFIG, NULL);
  231. register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
  232. COMMAND_CONFIG, NULL);
  233. register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
  234. COMMAND_CONFIG, NULL);
  235. return ERROR_OK;
  236. }
  237. void ft2232_end_state(enum tap_state state)
  238. {
  239. if (tap_move_map[state] != -1)
  240. end_state = state;
  241. else
  242. {
  243. ERROR("BUG: %i is not a valid end state", state);
  244. exit(-1);
  245. }
  246. }
  247. void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
  248. {
  249. int num_bytes = ((scan_size + 7) / 8);
  250. int bits_left = scan_size;
  251. int cur_byte = 0;
  252. while(num_bytes-- > 1)
  253. {
  254. buffer[cur_byte] = BUFFER_READ;
  255. cur_byte++;
  256. bits_left -= 8;
  257. }
  258. buffer[cur_byte] = 0x0;
  259. if (bits_left > 1)
  260. {
  261. buffer[cur_byte] = BUFFER_READ >> 1;
  262. }
  263. buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
  264. }
  265. void ft2232_debug_dump_buffer(void)
  266. {
  267. int i;
  268. char line[256];
  269. char *line_p = line;
  270. for (i = 0; i < ft2232_buffer_size; i++)
  271. {
  272. line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
  273. if (i % 16 == 15)
  274. {
  275. DEBUG("%s", line);
  276. line_p = line;
  277. }
  278. }
  279. if (line_p != line)
  280. DEBUG("%s", line);
  281. }
  282. int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
  283. {
  284. jtag_command_t *cmd;
  285. u8 *buffer;
  286. int scan_size;
  287. enum scan_type type;
  288. int retval;
  289. u32 bytes_written;
  290. u32 bytes_read;
  291. #ifdef _DEBUG_USB_IO_
  292. struct timeval start, inter, inter2, end;
  293. struct timeval d_inter, d_inter2, d_end;
  294. #endif
  295. #ifdef _DEBUG_USB_COMMS_
  296. DEBUG("write buffer (size %i):", ft2232_buffer_size);
  297. ft2232_debug_dump_buffer();
  298. #endif
  299. #ifdef _DEBUG_USB_IO_
  300. gettimeofday(&start, NULL);
  301. #endif
  302. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  303. {
  304. ERROR("couldn't write MPSSE commands to FT2232");
  305. exit(-1);
  306. }
  307. #ifdef _DEBUG_USB_IO_
  308. gettimeofday(&inter, NULL);
  309. #endif
  310. if (ft2232_expect_read)
  311. {
  312. int timeout = 100;
  313. ft2232_buffer_size = 0;
  314. #ifdef _DEBUG_USB_IO_
  315. gettimeofday(&inter2, NULL);
  316. #endif
  317. if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
  318. {
  319. ERROR("couldn't read from FT2232");
  320. exit(-1);
  321. }
  322. #ifdef _DEBUG_USB_IO_
  323. gettimeofday(&end, NULL);
  324. timeval_subtract(&d_inter, &inter, &start);
  325. timeval_subtract(&d_inter2, &inter2, &start);
  326. timeval_subtract(&d_end, &end, &start);
  327. INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
  328. #endif
  329. ft2232_buffer_size = bytes_read;
  330. if (ft2232_expect_read != ft2232_buffer_size)
  331. {
  332. ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
  333. ft2232_debug_dump_buffer();
  334. exit(-1);
  335. }
  336. #ifdef _DEBUG_USB_COMMS_
  337. DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
  338. ft2232_debug_dump_buffer();
  339. #endif
  340. }
  341. ft2232_expect_read = 0;
  342. ft2232_read_pointer = 0;
  343. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  344. * that wasn't handled by a caller-provided error handler
  345. */
  346. retval = ERROR_OK;
  347. cmd = first;
  348. while (cmd != last)
  349. {
  350. switch (cmd->type)
  351. {
  352. case JTAG_SCAN:
  353. type = jtag_scan_type(cmd->cmd.scan);
  354. if (type != SCAN_OUT)
  355. {
  356. scan_size = jtag_scan_size(cmd->cmd.scan);
  357. buffer = calloc(CEIL(scan_size, 8), 1);
  358. ft2232_read_scan(type, buffer, scan_size);
  359. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  360. retval = ERROR_JTAG_QUEUE_FAILED;
  361. free(buffer);
  362. }
  363. break;
  364. default:
  365. break;
  366. }
  367. cmd = cmd->next;
  368. }
  369. ft2232_buffer_size = 0;
  370. return retval;
  371. }
  372. void ft2232_add_pathmove(pathmove_command_t *cmd)
  373. {
  374. int num_states = cmd->num_states;
  375. u8 tms_byte;
  376. int state_count;
  377. state_count = 0;
  378. while (num_states)
  379. {
  380. tms_byte = 0x0;
  381. int bit_count = 0;
  382. /* command "Clock Data to TMS/CS Pin (no Read)" */
  383. BUFFER_ADD = 0x4b;
  384. /* number of states remaining */
  385. BUFFER_ADD = (num_states % 7) - 1;
  386. while (num_states % 7)
  387. {
  388. if (tap_transitions[cur_state].low == cmd->path[state_count])
  389. buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
  390. else if (tap_transitions[cur_state].high == cmd->path[state_count])
  391. buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
  392. else
  393. {
  394. ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
  395. exit(-1);
  396. }
  397. cur_state = cmd->path[state_count];
  398. state_count++;
  399. num_states--;
  400. }
  401. BUFFER_ADD = tms_byte;
  402. }
  403. end_state = cur_state;
  404. }
  405. void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  406. {
  407. int num_bytes = (scan_size + 7) / 8;
  408. int bits_left = scan_size;
  409. int cur_byte = 0;
  410. int last_bit;
  411. if (!((!ir_scan && (cur_state == TAP_SD)) || (ir_scan && (cur_state == TAP_SI))))
  412. {
  413. /* command "Clock Data to TMS/CS Pin (no Read)" */
  414. BUFFER_ADD = 0x4b;
  415. /* scan 7 bit */
  416. BUFFER_ADD = 0x6;
  417. /* TMS data bits */
  418. if (ir_scan)
  419. {
  420. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
  421. cur_state = TAP_SI;
  422. }
  423. else
  424. {
  425. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
  426. cur_state = TAP_SD;
  427. }
  428. //DEBUG("added TMS scan (no read)");
  429. }
  430. /* add command for complete bytes */
  431. while (num_bytes > 1)
  432. {
  433. int thisrun_bytes;
  434. if (type == SCAN_IO)
  435. {
  436. /* Clock Data Bytes In and Out LSB First */
  437. BUFFER_ADD = 0x39;
  438. //DEBUG("added TDI bytes (io %i)", num_bytes);
  439. }
  440. else if (type == SCAN_OUT)
  441. {
  442. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  443. BUFFER_ADD = 0x19;
  444. //DEBUG("added TDI bytes (o)");
  445. }
  446. else if (type == SCAN_IN)
  447. {
  448. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  449. BUFFER_ADD = 0x28;
  450. //DEBUG("added TDI bytes (i %i)", num_bytes);
  451. }
  452. thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
  453. num_bytes -= thisrun_bytes;
  454. BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
  455. BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
  456. if (type != SCAN_IN)
  457. {
  458. /* add complete bytes */
  459. while(thisrun_bytes-- > 0)
  460. {
  461. BUFFER_ADD = buffer[cur_byte];
  462. cur_byte++;
  463. bits_left -= 8;
  464. }
  465. }
  466. else /* (type == SCAN_IN) */
  467. {
  468. bits_left -= 8 * (thisrun_bytes);
  469. }
  470. }
  471. /* the most signifcant bit is scanned during TAP movement */
  472. if (type != SCAN_IN)
  473. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  474. else
  475. last_bit = 0;
  476. /* process remaining bits but the last one */
  477. if (bits_left > 1)
  478. {
  479. if (type == SCAN_IO)
  480. {
  481. /* Clock Data Bits In and Out LSB First */
  482. BUFFER_ADD = 0x3b;
  483. //DEBUG("added TDI bits (io) %i", bits_left - 1);
  484. }
  485. else if (type == SCAN_OUT)
  486. {
  487. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  488. BUFFER_ADD = 0x1b;
  489. //DEBUG("added TDI bits (o)");
  490. }
  491. else if (type == SCAN_IN)
  492. {
  493. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  494. BUFFER_ADD = 0x2a;
  495. //DEBUG("added TDI bits (i %i)", bits_left - 1);
  496. }
  497. BUFFER_ADD = bits_left - 2;
  498. if (type != SCAN_IN)
  499. BUFFER_ADD = buffer[cur_byte];
  500. }
  501. if ((ir_scan && (end_state == TAP_SI)) ||
  502. (!ir_scan && (end_state == TAP_SD)))
  503. {
  504. if (type == SCAN_IO)
  505. {
  506. /* Clock Data Bits In and Out LSB First */
  507. BUFFER_ADD = 0x3b;
  508. //DEBUG("added TDI bits (io) %i", bits_left - 1);
  509. }
  510. else if (type == SCAN_OUT)
  511. {
  512. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  513. BUFFER_ADD = 0x1b;
  514. //DEBUG("added TDI bits (o)");
  515. }
  516. else if (type == SCAN_IN)
  517. {
  518. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  519. BUFFER_ADD = 0x2a;
  520. //DEBUG("added TDI bits (i %i)", bits_left - 1);
  521. }
  522. BUFFER_ADD = 0x0;
  523. BUFFER_ADD = last_bit;
  524. }
  525. else
  526. {
  527. /* move from Shift-IR/DR to end state */
  528. if (type != SCAN_OUT)
  529. {
  530. /* Clock Data to TMS/CS Pin with Read */
  531. BUFFER_ADD = 0x6b;
  532. //DEBUG("added TMS scan (read)");
  533. }
  534. else
  535. {
  536. /* Clock Data to TMS/CS Pin (no Read) */
  537. BUFFER_ADD = 0x4b;
  538. //DEBUG("added TMS scan (no read)");
  539. }
  540. BUFFER_ADD = 0x6;
  541. BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
  542. cur_state = end_state;
  543. }
  544. }
  545. int ft2232_large_scan(scan_command_t *cmd, enum scan_type type, u8 *buffer, int scan_size)
  546. {
  547. int num_bytes = (scan_size + 7) / 8;
  548. int bits_left = scan_size;
  549. int cur_byte = 0;
  550. int last_bit;
  551. u8 *receive_buffer = malloc(CEIL(scan_size, 8));
  552. u8 *receive_pointer = receive_buffer;
  553. u32 bytes_written;
  554. u32 bytes_read;
  555. int retval;
  556. int thisrun_read = 0;
  557. if (cmd->ir_scan)
  558. {
  559. ERROR("BUG: large IR scans are not supported");
  560. exit(-1);
  561. }
  562. if (cur_state != TAP_SD)
  563. {
  564. /* command "Clock Data to TMS/CS Pin (no Read)" */
  565. BUFFER_ADD = 0x4b;
  566. /* scan 7 bit */
  567. BUFFER_ADD = 0x6;
  568. /* TMS data bits */
  569. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
  570. cur_state = TAP_SD;
  571. }
  572. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  573. {
  574. ERROR("couldn't write MPSSE commands to FT2232");
  575. exit(-1);
  576. }
  577. DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
  578. ft2232_buffer_size = 0;
  579. /* add command for complete bytes */
  580. while (num_bytes > 1)
  581. {
  582. int thisrun_bytes;
  583. if (type == SCAN_IO)
  584. {
  585. /* Clock Data Bytes In and Out LSB First */
  586. BUFFER_ADD = 0x39;
  587. //DEBUG("added TDI bytes (io %i)", num_bytes);
  588. }
  589. else if (type == SCAN_OUT)
  590. {
  591. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  592. BUFFER_ADD = 0x19;
  593. //DEBUG("added TDI bytes (o)");
  594. }
  595. else if (type == SCAN_IN)
  596. {
  597. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  598. BUFFER_ADD = 0x28;
  599. //DEBUG("added TDI bytes (i %i)", num_bytes);
  600. }
  601. thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
  602. thisrun_read = thisrun_bytes;
  603. num_bytes -= thisrun_bytes;
  604. BUFFER_ADD = (thisrun_bytes - 1) & 0xff;
  605. BUFFER_ADD = ((thisrun_bytes - 1) >> 8) & 0xff;
  606. if (type != SCAN_IN)
  607. {
  608. /* add complete bytes */
  609. while(thisrun_bytes-- > 0)
  610. {
  611. BUFFER_ADD = buffer[cur_byte];
  612. cur_byte++;
  613. bits_left -= 8;
  614. }
  615. }
  616. else /* (type == SCAN_IN) */
  617. {
  618. bits_left -= 8 * (thisrun_bytes);
  619. }
  620. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  621. {
  622. ERROR("couldn't write MPSSE commands to FT2232");
  623. exit(-1);
  624. }
  625. DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
  626. ft2232_buffer_size = 0;
  627. if (type != SCAN_OUT)
  628. {
  629. if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
  630. {
  631. ERROR("couldn't read from FT2232");
  632. exit(-1);
  633. }
  634. DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
  635. receive_pointer += bytes_read;
  636. }
  637. }
  638. thisrun_read = 0;
  639. /* the most signifcant bit is scanned during TAP movement */
  640. if (type != SCAN_IN)
  641. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  642. else
  643. last_bit = 0;
  644. /* process remaining bits but the last one */
  645. if (bits_left > 1)
  646. {
  647. if (type == SCAN_IO)
  648. {
  649. /* Clock Data Bits In and Out LSB First */
  650. BUFFER_ADD = 0x3b;
  651. //DEBUG("added TDI bits (io) %i", bits_left - 1);
  652. }
  653. else if (type == SCAN_OUT)
  654. {
  655. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  656. BUFFER_ADD = 0x1b;
  657. //DEBUG("added TDI bits (o)");
  658. }
  659. else if (type == SCAN_IN)
  660. {
  661. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  662. BUFFER_ADD = 0x2a;
  663. //DEBUG("added TDI bits (i %i)", bits_left - 1);
  664. }
  665. BUFFER_ADD = bits_left - 2;
  666. if (type != SCAN_IN)
  667. BUFFER_ADD = buffer[cur_byte];
  668. if (type != SCAN_OUT)
  669. thisrun_read += 2;
  670. }
  671. if (end_state == TAP_SD)
  672. {
  673. if (type == SCAN_IO)
  674. {
  675. /* Clock Data Bits In and Out LSB First */
  676. BUFFER_ADD = 0x3b;
  677. //DEBUG("added TDI bits (io) %i", bits_left - 1);
  678. }
  679. else if (type == SCAN_OUT)
  680. {
  681. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  682. BUFFER_ADD = 0x1b;
  683. //DEBUG("added TDI bits (o)");
  684. }
  685. else if (type == SCAN_IN)
  686. {
  687. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  688. BUFFER_ADD = 0x2a;
  689. //DEBUG("added TDI bits (i %i)", bits_left - 1);
  690. }
  691. BUFFER_ADD = 0x0;
  692. BUFFER_ADD = last_bit;
  693. }
  694. else
  695. {
  696. /* move from Shift-IR/DR to end state */
  697. if (type != SCAN_OUT)
  698. {
  699. /* Clock Data to TMS/CS Pin with Read */
  700. BUFFER_ADD = 0x6b;
  701. //DEBUG("added TMS scan (read)");
  702. }
  703. else
  704. {
  705. /* Clock Data to TMS/CS Pin (no Read) */
  706. BUFFER_ADD = 0x4b;
  707. //DEBUG("added TMS scan (no read)");
  708. }
  709. BUFFER_ADD = 0x6;
  710. BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
  711. cur_state = end_state;
  712. }
  713. if (type != SCAN_OUT)
  714. thisrun_read += 1;
  715. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  716. {
  717. ERROR("couldn't write MPSSE commands to FT2232");
  718. exit(-1);
  719. }
  720. DEBUG("ft2232_buffer_size: %i, bytes_written: %i", ft2232_buffer_size, bytes_written);
  721. ft2232_buffer_size = 0;
  722. if (type != SCAN_OUT)
  723. {
  724. if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
  725. {
  726. ERROR("couldn't read from FT2232");
  727. exit(-1);
  728. }
  729. DEBUG("thisrun_read: %i, bytes_read: %i", thisrun_read, bytes_read);
  730. receive_pointer += bytes_read;
  731. }
  732. return ERROR_OK;
  733. }
  734. int ft2232_predict_scan_out(int scan_size, enum scan_type type)
  735. {
  736. int predicted_size = 3;
  737. int num_bytes = (scan_size - 1) / 8;
  738. if (cur_state != TAP_SD)
  739. predicted_size += 3;
  740. if (type == SCAN_IN) /* only from device to host */
  741. {
  742. /* complete bytes */
  743. predicted_size += (CEIL(num_bytes, 65536)) * 3;
  744. /* remaining bits - 1 (up to 7) */
  745. predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
  746. }
  747. else /* host to device, or bidirectional */
  748. {
  749. /* complete bytes */
  750. predicted_size += num_bytes + (CEIL(num_bytes, 65536)) * 3;
  751. /* remaining bits -1 (up to 7) */
  752. predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
  753. }
  754. return predicted_size;
  755. }
  756. int ft2232_predict_scan_in(int scan_size, enum scan_type type)
  757. {
  758. int predicted_size = 0;
  759. if (type != SCAN_OUT)
  760. {
  761. /* complete bytes */
  762. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
  763. /* remaining bits - 1 */
  764. predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
  765. /* last bit (from TMS scan) */
  766. predicted_size += 1;
  767. }
  768. //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
  769. return predicted_size;
  770. }
  771. void usbjtag_reset(int trst, int srst)
  772. {
  773. if (trst == 1)
  774. {
  775. cur_state = TAP_TLR;
  776. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  777. low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
  778. else
  779. low_output &= ~nTRST; /* switch output low */
  780. }
  781. else if (trst == 0)
  782. {
  783. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  784. low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
  785. else
  786. low_output |= nTRST; /* switch output high */
  787. }
  788. if (srst == 1)
  789. {
  790. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  791. low_output &= ~nSRST; /* switch output low */
  792. else
  793. low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
  794. }
  795. else if (srst == 0)
  796. {
  797. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  798. low_output |= nSRST; /* switch output high */
  799. else
  800. low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
  801. }
  802. /* command "set data bits low byte" */
  803. BUFFER_ADD = 0x80;
  804. BUFFER_ADD = low_output;
  805. BUFFER_ADD = low_direction;
  806. }
  807. void jtagkey_reset(int trst, int srst)
  808. {
  809. if (trst == 1)
  810. {
  811. cur_state = TAP_TLR;
  812. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  813. high_output &= ~nTRSTnOE;
  814. else
  815. high_output &= ~nTRST;
  816. }
  817. else if (trst == 0)
  818. {
  819. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  820. high_output |= nTRSTnOE;
  821. else
  822. high_output |= nTRST;
  823. }
  824. if (srst == 1)
  825. {
  826. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  827. high_output &= ~nSRST;
  828. else
  829. high_output &= ~nSRSTnOE;
  830. }
  831. else if (srst == 0)
  832. {
  833. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  834. high_output |= nSRST;
  835. else
  836. high_output |= nSRSTnOE;
  837. }
  838. /* command "set data bits high byte" */
  839. BUFFER_ADD = 0x82;
  840. BUFFER_ADD = high_output;
  841. BUFFER_ADD = high_direction;
  842. DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  843. }
  844. void olimex_jtag_reset(int trst, int srst)
  845. {
  846. if (trst == 1)
  847. {
  848. cur_state = TAP_TLR;
  849. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  850. high_output &= ~nTRSTnOE;
  851. else
  852. high_output &= ~nTRST;
  853. }
  854. else if (trst == 0)
  855. {
  856. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  857. high_output |= nTRSTnOE;
  858. else
  859. high_output |= nTRST;
  860. }
  861. if (srst == 1)
  862. {
  863. high_output |= nSRST;
  864. }
  865. else if (srst == 0)
  866. {
  867. high_output &= ~nSRST;
  868. }
  869. /* command "set data bits high byte" */
  870. BUFFER_ADD = 0x82;
  871. BUFFER_ADD = high_output;
  872. BUFFER_ADD = high_direction;
  873. DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  874. }
  875. void m5960_reset(int trst, int srst)
  876. {
  877. if (trst == 1)
  878. {
  879. cur_state = TAP_TLR;
  880. low_output &= ~nTRST;
  881. }
  882. else if (trst == 0)
  883. {
  884. low_output |= nTRST;
  885. }
  886. if (srst == 1)
  887. {
  888. low_output |= nSRST;
  889. }
  890. else if (srst == 0)
  891. {
  892. low_output &= ~nSRST;
  893. }
  894. /* command "set data bits low byte" */
  895. BUFFER_ADD = 0x80;
  896. BUFFER_ADD = low_output;
  897. BUFFER_ADD = low_direction;
  898. DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
  899. }
  900. void turtle_reset(int trst, int srst)
  901. {
  902. trst = trst;
  903. if (srst == 1)
  904. {
  905. low_output |= nSRST;
  906. }
  907. else if (srst == 0)
  908. {
  909. low_output &= ~nSRST;
  910. }
  911. /* command "set data bits low byte" */
  912. BUFFER_ADD = 0x80;
  913. BUFFER_ADD = low_output;
  914. BUFFER_ADD = low_direction;
  915. DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
  916. }
  917. void comstick_reset(int trst, int srst)
  918. {
  919. if (trst == 1)
  920. {
  921. cur_state = TAP_TLR;
  922. high_output &= ~nTRST;
  923. }
  924. else if (trst == 0)
  925. {
  926. high_output |= nTRST;
  927. }
  928. if (srst == 1)
  929. {
  930. high_output &= ~nSRST;
  931. }
  932. else if (srst == 0)
  933. {
  934. high_output |= nSRST;
  935. }
  936. /* command "set data bits high byte" */
  937. BUFFER_ADD = 0x82;
  938. BUFFER_ADD = high_output;
  939. BUFFER_ADD = high_direction;
  940. DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  941. }
  942. int ft2232_execute_queue()
  943. {
  944. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  945. jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
  946. u8 *buffer;
  947. int scan_size; /* size of IR or DR scan */
  948. enum scan_type type;
  949. int i;
  950. int predicted_size = 0;
  951. int require_send = 0;
  952. int retval;
  953. /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
  954. * that wasn't handled by a caller-provided error handler
  955. */
  956. retval = ERROR_OK;
  957. ft2232_buffer_size = 0;
  958. ft2232_expect_read = 0;
  959. /* blink, if the current layout has that feature */
  960. if (layout->blink)
  961. layout->blink();
  962. while (cmd)
  963. {
  964. switch(cmd->type)
  965. {
  966. case JTAG_END_STATE:
  967. if (cmd->cmd.end_state->end_state != -1)
  968. ft2232_end_state(cmd->cmd.end_state->end_state);
  969. break;
  970. case JTAG_RESET:
  971. /* only send the maximum buffer size that FT2232C can handle */
  972. predicted_size = 3;
  973. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  974. {
  975. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  976. retval = ERROR_JTAG_QUEUE_FAILED;
  977. require_send = 0;
  978. first_unsent = cmd;
  979. }
  980. layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  981. require_send = 1;
  982. #ifdef _DEBUG_JTAG_IO_
  983. DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  984. #endif
  985. break;
  986. case JTAG_RUNTEST:
  987. /* only send the maximum buffer size that FT2232C can handle */
  988. predicted_size = 0;
  989. if (cur_state != TAP_RTI)
  990. predicted_size += 3;
  991. predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
  992. if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
  993. predicted_size += 3;
  994. if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
  995. predicted_size += 3;
  996. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  997. {
  998. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  999. retval = ERROR_JTAG_QUEUE_FAILED;
  1000. require_send = 0;
  1001. first_unsent = cmd;
  1002. }
  1003. if (cur_state != TAP_RTI)
  1004. {
  1005. /* command "Clock Data to TMS/CS Pin (no Read)" */
  1006. BUFFER_ADD = 0x4b;
  1007. /* scan 7 bit */
  1008. BUFFER_ADD = 0x6;
  1009. /* TMS data bits */
  1010. BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
  1011. cur_state = TAP_RTI;
  1012. require_send = 1;
  1013. }
  1014. i = cmd->cmd.runtest->num_cycles;
  1015. while (i > 0)
  1016. {
  1017. /* command "Clock Data to TMS/CS Pin (no Read)" */
  1018. BUFFER_ADD = 0x4b;
  1019. /* scan 7 bit */
  1020. BUFFER_ADD = (i > 7) ? 6 : (i - 1);
  1021. /* TMS data bits */
  1022. BUFFER_ADD = 0x0;
  1023. cur_state = TAP_RTI;
  1024. i -= (i > 7) ? 7 : i;
  1025. //DEBUG("added TMS scan (no read)");
  1026. }
  1027. if (cmd->cmd.runtest->end_state != -1)
  1028. ft2232_end_state(cmd->cmd.runtest->end_state);
  1029. if (cur_state != end_state)
  1030. {
  1031. /* command "Clock Data to TMS/CS Pin (no Read)" */
  1032. BUFFER_ADD = 0x4b;
  1033. /* scan 7 bit */
  1034. BUFFER_ADD = 0x6;
  1035. /* TMS data bits */
  1036. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  1037. cur_state = end_state;
  1038. //DEBUG("added TMS scan (no read)");
  1039. }
  1040. require_send = 1;
  1041. #ifdef _DEBUG_JTAG_IO_
  1042. DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
  1043. #endif
  1044. break;
  1045. case JTAG_STATEMOVE:
  1046. /* only send the maximum buffer size that FT2232C can handle */
  1047. predicted_size = 3;
  1048. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1049. {
  1050. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1051. retval = ERROR_JTAG_QUEUE_FAILED;
  1052. require_send = 0;
  1053. first_unsent = cmd;
  1054. }
  1055. if (cmd->cmd.statemove->end_state != -1)
  1056. ft2232_end_state(cmd->cmd.statemove->end_state);
  1057. /* command "Clock Data to TMS/CS Pin (no Read)" */
  1058. BUFFER_ADD = 0x4b;
  1059. /* scan 7 bit */
  1060. BUFFER_ADD = 0x6;
  1061. /* TMS data bits */
  1062. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  1063. //DEBUG("added TMS scan (no read)");
  1064. cur_state = end_state;
  1065. require_send = 1;
  1066. #ifdef _DEBUG_JTAG_IO_
  1067. DEBUG("statemove: %i", end_state);
  1068. #endif
  1069. break;
  1070. case JTAG_PATHMOVE:
  1071. /* only send the maximum buffer size that FT2232C can handle */
  1072. predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
  1073. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1074. {
  1075. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1076. retval = ERROR_JTAG_QUEUE_FAILED;
  1077. require_send = 0;
  1078. first_unsent = cmd;
  1079. }
  1080. ft2232_add_pathmove(cmd->cmd.pathmove);
  1081. require_send = 1;
  1082. #ifdef _DEBUG_JTAG_IO_
  1083. DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  1084. #endif
  1085. break;
  1086. case JTAG_SCAN:
  1087. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  1088. type = jtag_scan_type(cmd->cmd.scan);
  1089. predicted_size = ft2232_predict_scan_out(scan_size, type);
  1090. if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
  1091. {
  1092. DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
  1093. /* unsent commands before this */
  1094. if (first_unsent != cmd)
  1095. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1096. retval = ERROR_JTAG_QUEUE_FAILED;
  1097. /* current command */
  1098. if (cmd->cmd.scan->end_state != -1)
  1099. ft2232_end_state(cmd->cmd.scan->end_state);
  1100. ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
  1101. require_send = 0;
  1102. first_unsent = cmd->next;
  1103. if (buffer)
  1104. free(buffer);
  1105. break;
  1106. }
  1107. else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1108. {
  1109. DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)", first_unsent, cmd);
  1110. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1111. retval = ERROR_JTAG_QUEUE_FAILED;
  1112. require_send = 0;
  1113. first_unsent = cmd;
  1114. }
  1115. ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
  1116. //DEBUG("new read size: %i", ft2232_expect_read);
  1117. if (cmd->cmd.scan->end_state != -1)
  1118. ft2232_end_state(cmd->cmd.scan->end_state);
  1119. ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  1120. require_send = 1;
  1121. if (buffer)
  1122. free(buffer);
  1123. #ifdef _DEBUG_JTAG_IO_
  1124. DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
  1125. #endif
  1126. break;
  1127. case JTAG_SLEEP:
  1128. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1129. retval = ERROR_JTAG_QUEUE_FAILED;
  1130. first_unsent = cmd->next;
  1131. jtag_sleep(cmd->cmd.sleep->us);
  1132. #ifdef _DEBUG_JTAG_IO_
  1133. DEBUG("sleep %i usec", cmd->cmd.sleep->us);
  1134. #endif
  1135. break;
  1136. default:
  1137. ERROR("BUG: unknown JTAG command type encountered");
  1138. exit(-1);
  1139. }
  1140. cmd = cmd->next;
  1141. }
  1142. if (require_send > 0)
  1143. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1144. retval = ERROR_JTAG_QUEUE_FAILED;
  1145. return retval;
  1146. }
  1147. #if BUILD_FT2232_FTD2XX == 1
  1148. static int ft2232_init_ftd2xx(u16 vid, u16 pid, int more, int *try_more)
  1149. {
  1150. FT_STATUS status;
  1151. DWORD openex_flags = 0;
  1152. char *openex_string = NULL;
  1153. u8 latency_timer;
  1154. DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)",
  1155. ft2232_layout, vid, pid);
  1156. #if IS_WIN32 == 0
  1157. /* Add non-standard Vid/Pid to the linux driver */
  1158. if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
  1159. {
  1160. WARNING("couldn't add %4.4x:%4.4x",
  1161. vid, pid);
  1162. }
  1163. #endif
  1164. if (ft2232_device_desc && ft2232_serial)
  1165. {
  1166. WARNING("can't open by device description and serial number, giving precedence to serial");
  1167. ft2232_device_desc = NULL;
  1168. }
  1169. if (ft2232_device_desc)
  1170. {
  1171. openex_string = ft2232_device_desc;
  1172. openex_flags = FT_OPEN_BY_DESCRIPTION;
  1173. }
  1174. else if (ft2232_serial)
  1175. {
  1176. openex_string = ft2232_serial;
  1177. openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
  1178. }
  1179. else
  1180. {
  1181. ERROR("neither device description nor serial number specified");
  1182. ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
  1183. return ERROR_JTAG_INIT_FAILED;
  1184. }
  1185. if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
  1186. {
  1187. DWORD num_devices;
  1188. if (more) {
  1189. WARNING("unable to open ftdi device (trying more): %lu",
  1190. status);
  1191. *try_more = 1;
  1192. return ERROR_JTAG_INIT_FAILED;
  1193. }
  1194. ERROR("unable to open ftdi device: %lu", status);
  1195. status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
  1196. if (status == FT_OK)
  1197. {
  1198. char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
  1199. int i;
  1200. for (i = 0; i < num_devices; i++)
  1201. desc_array[i] = malloc(64);
  1202. desc_array[num_devices] = NULL;
  1203. status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
  1204. if (status == FT_OK)
  1205. {
  1206. ERROR("ListDevices: %lu\n", num_devices);
  1207. for (i = 0; i < num_devices; i++)
  1208. ERROR("%i: %s", i, desc_array[i]);
  1209. }
  1210. for (i = 0; i < num_devices; i++)
  1211. free(desc_array[i]);
  1212. free(desc_array);
  1213. }
  1214. else
  1215. {
  1216. printf("ListDevices: NONE\n");
  1217. }
  1218. return ERROR_JTAG_INIT_FAILED;
  1219. }
  1220. if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
  1221. {
  1222. ERROR("unable to set latency timer: %lu", status);
  1223. return ERROR_JTAG_INIT_FAILED;
  1224. }
  1225. if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
  1226. {
  1227. ERROR("unable to get latency timer: %lu", status);
  1228. return ERROR_JTAG_INIT_FAILED;
  1229. }
  1230. else
  1231. {
  1232. DEBUG("current latency timer: %i", latency_timer);
  1233. }
  1234. if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
  1235. {
  1236. ERROR("unable to set timeouts: %lu", status);
  1237. return ERROR_JTAG_INIT_FAILED;
  1238. }
  1239. if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
  1240. {
  1241. ERROR("unable to enable bit i/o mode: %lu", status);
  1242. return ERROR_JTAG_INIT_FAILED;
  1243. }
  1244. return ERROR_OK;
  1245. }
  1246. static int ft2232_purge_ftd2xx(void)
  1247. {
  1248. FT_STATUS status;
  1249. if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
  1250. {
  1251. ERROR("error purging ftd2xx device: %lu", status);
  1252. return ERROR_JTAG_INIT_FAILED;
  1253. }
  1254. return ERROR_OK;
  1255. }
  1256. #endif /* BUILD_FT2232_FTD2XX == 1 */
  1257. #if BUILD_FT2232_LIBFTDI == 1
  1258. static int ft2232_init_libftdi(u16 vid, u16 pid, int more, int *try_more)
  1259. {
  1260. u8 latency_timer;
  1261. DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
  1262. ft2232_layout, vid, pid);
  1263. if (ftdi_init(&ftdic) < 0)
  1264. return ERROR_JTAG_INIT_FAILED;
  1265. /* context, vendor id, product id */
  1266. if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
  1267. ft2232_serial) < 0) {
  1268. if (more)
  1269. WARNING("unable to open ftdi device (trying more): %s",
  1270. ftdic.error_str);
  1271. else
  1272. ERROR("unable to open ftdi device: %s", ftdic.error_str);
  1273. *try_more = 1;
  1274. return ERROR_JTAG_INIT_FAILED;
  1275. }
  1276. if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
  1277. {
  1278. ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
  1279. return ERROR_JTAG_INIT_FAILED;
  1280. }
  1281. if (ftdi_usb_reset(&ftdic) < 0)
  1282. {
  1283. ERROR("unable to reset ftdi device");
  1284. return ERROR_JTAG_INIT_FAILED;
  1285. }
  1286. if (ftdi_set_latency_timer(&ftdic, 2) < 0)
  1287. {
  1288. ERROR("unable to set latency timer");
  1289. return ERROR_JTAG_INIT_FAILED;
  1290. }
  1291. if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
  1292. {
  1293. ERROR("unable to get latency timer");
  1294. return ERROR_JTAG_INIT_FAILED;
  1295. }
  1296. else
  1297. {
  1298. DEBUG("current latency timer: %i", latency_timer);
  1299. }
  1300. ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
  1301. return ERROR_OK;
  1302. }
  1303. static int ft2232_purge_libftdi(void)
  1304. {
  1305. if (ftdi_usb_purge_buffers(&ftdic) < 0)
  1306. {
  1307. ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  1308. return ERROR_JTAG_INIT_FAILED;
  1309. }
  1310. return ERROR_OK;
  1311. }
  1312. #endif /* BUILD_FT2232_LIBFTDI == 1 */
  1313. int ft2232_init(void)
  1314. {
  1315. u8 buf[1];
  1316. int retval;
  1317. u32 bytes_written;
  1318. ft2232_layout_t *cur_layout = ft2232_layouts;
  1319. int i;
  1320. if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
  1321. {
  1322. ft2232_layout = "usbjtag";
  1323. WARNING("No ft2232 layout specified, using default 'usbjtag'");
  1324. }
  1325. while (cur_layout->name)
  1326. {
  1327. if (strcmp(cur_layout->name, ft2232_layout) == 0)
  1328. {
  1329. layout = cur_layout;
  1330. break;
  1331. }
  1332. cur_layout++;
  1333. }
  1334. if (!layout)
  1335. {
  1336. ERROR("No matching layout found for %s", ft2232_layout);
  1337. return ERROR_JTAG_INIT_FAILED;
  1338. }
  1339. for (i = 0; 1; i++) {
  1340. /*
  1341. * "more indicates that there are more IDs to try, so we should
  1342. * not print an error for an ID mismatch (but for anything
  1343. * else, we should).
  1344. *
  1345. * try_more indicates that the error code returned indicates an
  1346. * ID mismatch (and nothing else) and that we should proceeed
  1347. * with the next ID pair.
  1348. */
  1349. int more = ft2232_vid[i+1] || ft2232_pid[i+1];
  1350. int try_more = 0;
  1351. #if BUILD_FT2232_FTD2XX == 1
  1352. retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
  1353. more, &try_more);
  1354. #elif BUILD_FT2232_LIBFTDI == 1
  1355. retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
  1356. more, &try_more);
  1357. #endif
  1358. if (retval >= 0)
  1359. break;
  1360. if (!more || !try_more)
  1361. return retval;
  1362. }
  1363. ft2232_buffer_size = 0;
  1364. ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
  1365. if (layout->init() != ERROR_OK)
  1366. return ERROR_JTAG_INIT_FAILED;
  1367. ft2232_speed(jtag_speed);
  1368. buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
  1369. if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
  1370. {
  1371. ERROR("couldn't write to FT2232 to disable loopback");
  1372. return ERROR_JTAG_INIT_FAILED;
  1373. }
  1374. #if BUILD_FT2232_FTD2XX == 1
  1375. return ft2232_purge_ftd2xx();
  1376. #elif BUILD_FT2232_LIBFTDI == 1
  1377. return ft2232_purge_libftdi();
  1378. #endif
  1379. return ERROR_OK;
  1380. }
  1381. int usbjtag_init(void)
  1382. {
  1383. u8 buf[3];
  1384. u32 bytes_written;
  1385. low_output = 0x08;
  1386. low_direction = 0x0b;
  1387. if (strcmp(ft2232_layout, "usbjtag") == 0)
  1388. {
  1389. nTRST = 0x10;
  1390. nTRSTnOE = 0x10;
  1391. nSRST = 0x40;
  1392. nSRSTnOE = 0x40;
  1393. }
  1394. else if (strcmp(ft2232_layout, "signalyzer") == 0)
  1395. {
  1396. nTRST = 0x10;
  1397. nTRSTnOE = 0x10;
  1398. nSRST = 0x20;
  1399. nSRSTnOE = 0x20;
  1400. }
  1401. else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
  1402. {
  1403. nTRST = 0x0;
  1404. nTRSTnOE = 0x00;
  1405. nSRST = 0x20;
  1406. nSRSTnOE = 0x20;
  1407. low_output = 0x88;
  1408. low_direction = 0x8b;
  1409. }
  1410. else
  1411. {
  1412. ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
  1413. return ERROR_JTAG_INIT_FAILED;
  1414. }
  1415. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1416. {
  1417. low_direction &= ~nTRSTnOE; /* nTRST input */
  1418. low_output &= ~nTRST; /* nTRST = 0 */
  1419. }
  1420. else
  1421. {
  1422. low_direction |= nTRSTnOE; /* nTRST output */
  1423. low_output |= nTRST; /* nTRST = 1 */
  1424. }
  1425. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1426. {
  1427. low_direction |= nSRSTnOE; /* nSRST output */
  1428. low_output |= nSRST; /* nSRST = 1 */
  1429. }
  1430. else
  1431. {
  1432. low_direction &= ~nSRSTnOE; /* nSRST input */
  1433. low_output &= ~nSRST; /* nSRST = 0 */
  1434. }
  1435. /* initialize low byte for jtag */
  1436. buf[0] = 0x80; /* command "set data bits low byte" */
  1437. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
  1438. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
  1439. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1440. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1441. {
  1442. ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
  1443. return ERROR_JTAG_INIT_FAILED;
  1444. }
  1445. return ERROR_OK;
  1446. }
  1447. int jtagkey_init(void)
  1448. {
  1449. u8 buf[3];
  1450. u32 bytes_written;
  1451. low_output = 0x08;
  1452. low_direction = 0x1b;
  1453. /* initialize low byte for jtag */
  1454. buf[0] = 0x80; /* command "set data bits low byte" */
  1455. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1456. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
  1457. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1458. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1459. {
  1460. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1461. return ERROR_JTAG_INIT_FAILED;
  1462. }
  1463. if (strcmp(layout->name, "jtagkey") == 0)
  1464. {
  1465. nTRST = 0x01;
  1466. nTRSTnOE = 0x4;
  1467. nSRST = 0x02;
  1468. nSRSTnOE = 0x08;
  1469. }
  1470. else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0) ||
  1471. (strcmp(layout->name, "oocdlink") == 0))
  1472. {
  1473. nTRST = 0x02;
  1474. nTRSTnOE = 0x1;
  1475. nSRST = 0x08;
  1476. nSRSTnOE = 0x04;
  1477. }
  1478. else
  1479. {
  1480. ERROR("BUG: jtagkey_init called for non jtagkey layout");
  1481. exit(-1);
  1482. }
  1483. high_output = 0x0;
  1484. high_direction = 0x0f;
  1485. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1486. {
  1487. high_output |= nTRSTnOE;
  1488. high_output &= ~nTRST;
  1489. }
  1490. else
  1491. {
  1492. high_output &= ~nTRSTnOE;
  1493. high_output |= nTRST;
  1494. }
  1495. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1496. {
  1497. high_output &= ~nSRSTnOE;
  1498. high_output |= nSRST;
  1499. }
  1500. else
  1501. {
  1502. high_output |= nSRSTnOE;
  1503. high_output &= ~nSRST;
  1504. }
  1505. /* initialize high port */
  1506. buf[0] = 0x82; /* command "set data bits high byte" */
  1507. buf[1] = high_output; /* value */
  1508. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1509. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1510. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1511. {
  1512. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1513. return ERROR_JTAG_INIT_FAILED;
  1514. }
  1515. return ERROR_OK;
  1516. }
  1517. int olimex_jtag_init(void)
  1518. {
  1519. u8 buf[3];
  1520. u32 bytes_written;
  1521. low_output = 0x08;
  1522. low_direction = 0x1b;
  1523. /* initialize low byte for jtag */
  1524. buf[0] = 0x80; /* command "set data bits low byte" */
  1525. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1526. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
  1527. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1528. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1529. {
  1530. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1531. return ERROR_JTAG_INIT_FAILED;
  1532. }
  1533. nTRST = 0x01;
  1534. nTRSTnOE = 0x4;
  1535. nSRST = 0x02;
  1536. nSRSTnOE = 0x00; /* no output enable for nSRST */
  1537. high_output = 0x0;
  1538. high_direction = 0x0f;
  1539. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1540. {
  1541. high_output |= nTRSTnOE;
  1542. high_output &= ~nTRST;
  1543. }
  1544. else
  1545. {
  1546. high_output &= ~nTRSTnOE;
  1547. high_output |= nTRST;
  1548. }
  1549. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1550. {
  1551. ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
  1552. }
  1553. else
  1554. {
  1555. high_output &= ~nSRST;
  1556. }
  1557. /* turn red LED on */
  1558. high_output |= 0x08;
  1559. /* initialize high port */
  1560. buf[0] = 0x82; /* command "set data bits high byte" */
  1561. buf[1] = high_output; /* value */
  1562. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1563. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1564. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1565. {
  1566. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1567. return ERROR_JTAG_INIT_FAILED;
  1568. }
  1569. return ERROR_OK;
  1570. }
  1571. int m5960_init(void)
  1572. {
  1573. u8 buf[3];
  1574. u32 bytes_written;
  1575. low_output = 0x18;
  1576. low_direction = 0xfb;
  1577. /* initialize low byte for jtag */
  1578. buf[0] = 0x80; /* command "set data bits low byte" */
  1579. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1580. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
  1581. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1582. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1583. {
  1584. ERROR("couldn't initialize FT2232 with 'm5960' layout");
  1585. return ERROR_JTAG_INIT_FAILED;
  1586. }
  1587. nTRST = 0x10;
  1588. nTRSTnOE = 0x0; /* not output enable for nTRST */
  1589. nSRST = 0x20;
  1590. nSRSTnOE = 0x00; /* no output enable for nSRST */
  1591. high_output = 0x00;
  1592. high_direction = 0x0c;
  1593. /* turn red LED1 on, LED2 off */
  1594. high_output |= 0x08;
  1595. /* initialize high port */
  1596. buf[0] = 0x82; /* command "set data bits high byte" */
  1597. buf[1] = high_output; /* value */
  1598. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1599. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1600. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1601. {
  1602. ERROR("couldn't initialize FT2232 with 'm5960' layout");
  1603. return ERROR_JTAG_INIT_FAILED;
  1604. }
  1605. return ERROR_OK;
  1606. }
  1607. int turtle_init(void)
  1608. {
  1609. u8 buf[3];
  1610. u32 bytes_written;
  1611. low_output = 0x08;
  1612. low_direction = 0x5b;
  1613. /* initialize low byte for jtag */
  1614. buf[0] = 0x80; /* command "set data bits low byte" */
  1615. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1616. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
  1617. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1618. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1619. {
  1620. ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
  1621. return ERROR_JTAG_INIT_FAILED;
  1622. }
  1623. nSRST = 0x40;
  1624. high_output = 0x00;
  1625. high_direction = 0x0C;
  1626. /* initialize high port */
  1627. buf[0] = 0x82; /* command "set data bits high byte" */
  1628. buf[1] = high_output;
  1629. buf[2] = high_direction;
  1630. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1631. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1632. {
  1633. ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
  1634. return ERROR_JTAG_INIT_FAILED;
  1635. }
  1636. return ERROR_OK;
  1637. }
  1638. int comstick_init(void)
  1639. {
  1640. u8 buf[3];
  1641. u32 bytes_written;
  1642. low_output = 0x08;
  1643. low_direction = 0x0b;
  1644. /* initialize low byte for jtag */
  1645. buf[0] = 0x80; /* command "set data bits low byte" */
  1646. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1647. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
  1648. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1649. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1650. {
  1651. ERROR("couldn't initialize FT2232 with 'comstick' layout");
  1652. return ERROR_JTAG_INIT_FAILED;
  1653. }
  1654. nTRST = 0x01;
  1655. nTRSTnOE = 0x00; /* no output enable for nTRST */
  1656. nSRST = 0x02;
  1657. nSRSTnOE = 0x00; /* no output enable for nSRST */
  1658. high_output = 0x03;
  1659. high_direction = 0x03;
  1660. /* initialize high port */
  1661. buf[0] = 0x82; /* command "set data bits high byte" */
  1662. buf[1] = high_output;
  1663. buf[2] = high_direction;
  1664. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1665. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1666. {
  1667. ERROR("couldn't initialize FT2232 with 'comstick' layout");
  1668. return ERROR_JTAG_INIT_FAILED;
  1669. }
  1670. return ERROR_OK;
  1671. }
  1672. void olimex_jtag_blink(void)
  1673. {
  1674. /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
  1675. * ACBUS3 is bit 3 of the GPIOH port
  1676. */
  1677. if (high_output & 0x08)
  1678. {
  1679. /* set port pin high */
  1680. high_output &= 0x07;
  1681. }
  1682. else
  1683. {
  1684. /* set port pin low */
  1685. high_output |= 0x08;
  1686. }
  1687. BUFFER_ADD = 0x82;
  1688. BUFFER_ADD = high_output;
  1689. BUFFER_ADD = high_direction;
  1690. }
  1691. void turtle_jtag_blink(void)
  1692. {
  1693. /*
  1694. * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
  1695. */
  1696. if (high_output & 0x08)
  1697. {
  1698. high_output = 0x04;
  1699. }
  1700. else
  1701. {
  1702. high_output = 0x08;
  1703. }
  1704. BUFFER_ADD = 0x82;
  1705. BUFFER_ADD = high_output;
  1706. BUFFER_ADD = high_direction;
  1707. }
  1708. int ft2232_quit(void)
  1709. {
  1710. #if BUILD_FT2232_FTD2XX == 1
  1711. FT_STATUS status;
  1712. status = FT_Close(ftdih);
  1713. #elif BUILD_FT2232_LIBFTDI == 1
  1714. ftdi_disable_bitbang(&ftdic);
  1715. ftdi_usb_close(&ftdic);
  1716. ftdi_deinit(&ftdic);
  1717. #endif
  1718. free(ft2232_buffer);
  1719. return ERROR_OK;
  1720. }
  1721. int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1722. {
  1723. if (argc == 1)
  1724. {
  1725. ft2232_device_desc = strdup(args[0]);
  1726. }
  1727. else
  1728. {
  1729. ERROR("expected exactly one argument to ft2232_device_desc <description>");
  1730. }
  1731. return ERROR_OK;
  1732. }
  1733. int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1734. {
  1735. if (argc == 1)
  1736. {
  1737. ft2232_serial = strdup(args[0]);
  1738. }
  1739. else
  1740. {
  1741. ERROR("expected exactly one argument to ft2232_serial <serial-number>");
  1742. }
  1743. return ERROR_OK;
  1744. }
  1745. int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1746. {
  1747. if (argc == 0)
  1748. return ERROR_OK;
  1749. ft2232_layout = malloc(strlen(args[0]) + 1);
  1750. strcpy(ft2232_layout, args[0]);
  1751. return ERROR_OK;
  1752. }
  1753. int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1754. {
  1755. int i;
  1756. if (argc > MAX_USB_IDS*2) {
  1757. WARNING("ignoring extra IDs in ft2232_vid_pid "
  1758. "(maximum is %d pairs)", MAX_USB_IDS);
  1759. argc = MAX_USB_IDS*2;
  1760. }
  1761. if (argc < 2 || (argc & 1))
  1762. {
  1763. WARNING("incomplete ft2232_vid_pid configuration directive");
  1764. if (argc < 2)
  1765. return ERROR_OK;
  1766. }
  1767. for (i = 0; i+1 < argc; i += 2) {
  1768. ft2232_vid[i >> 1] = strtol(args[i], NULL, 0);
  1769. ft2232_pid[i >> 1] = strtol(args[i+1], NULL, 0);
  1770. }
  1771. /*
  1772. * Explicitly terminate, in case there are multiples instances of
  1773. * ft2232_vid_pid.
  1774. */
  1775. ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
  1776. return ERROR_OK;
  1777. }