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.
 
 
 
 
 
 

1229 lines
30 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_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  63. int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  64. char *ft2232_device_desc = NULL;
  65. char *ft2232_layout = NULL;
  66. u16 ft2232_vid = 0x0403;
  67. u16 ft2232_pid = 0x6010;
  68. typedef struct ft2232_layout_s
  69. {
  70. char* name;
  71. int(*init)(void);
  72. void(*reset)(int trst, int srst);
  73. } ft2232_layout_t;
  74. int usbjtag_init(void);
  75. int jtagkey_init(void);
  76. void usbjtag_reset(int trst, int srst);
  77. void jtagkey_reset(int trst, int srst);
  78. ft2232_layout_t ft2232_layouts[] =
  79. {
  80. {"usbjtag", usbjtag_init, usbjtag_reset},
  81. {"jtagkey", jtagkey_init, jtagkey_reset},
  82. {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset},
  83. {NULL, NULL, NULL},
  84. };
  85. static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
  86. static ft2232_layout_t *layout;
  87. static u8 low_output = 0x0;
  88. static u8 low_direction = 0x0;
  89. static u8 high_output = 0x0;
  90. static u8 high_direction = 0x0;
  91. #if BUILD_FT2232_FTD2XX == 1
  92. static FT_HANDLE ftdih = NULL;
  93. #elif BUILD_FT2232_LIBFTDI == 1
  94. static struct ftdi_context ftdic;
  95. #endif
  96. static u8 *ft2232_buffer = NULL;
  97. static int ft2232_buffer_size = 0;
  98. static int ft2232_read_pointer = 0;
  99. static int ft2232_expect_read = 0;
  100. #define FT2232_BUFFER_SIZE 131072
  101. #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
  102. #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
  103. jtag_interface_t ft2232_interface =
  104. {
  105. .name = "ft2232",
  106. .execute_queue = ft2232_execute_queue,
  107. .support_statemove = 1,
  108. .speed = ft2232_speed,
  109. .register_commands = ft2232_register_commands,
  110. .init = ft2232_init,
  111. .quit = ft2232_quit,
  112. };
  113. int ft2232_write(u8 *buf, int size, u32* bytes_written)
  114. {
  115. #if BUILD_FT2232_FTD2XX == 1
  116. FT_STATUS status;
  117. DWORD dw_bytes_written;
  118. if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
  119. {
  120. *bytes_written = dw_bytes_written;
  121. ERROR("FT_Write returned: %i", status);
  122. return ERROR_JTAG_DEVICE_ERROR;
  123. }
  124. else
  125. {
  126. *bytes_written = dw_bytes_written;
  127. return ERROR_OK;
  128. }
  129. #elif BUILD_FT2232_LIBFTDI == 1
  130. int retval;
  131. if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
  132. {
  133. *bytes_written = 0;
  134. ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
  135. return ERROR_JTAG_DEVICE_ERROR;
  136. }
  137. else
  138. {
  139. *bytes_written = retval;
  140. return ERROR_OK;
  141. }
  142. #endif
  143. }
  144. int ft2232_read(u8* buf, int size, u32* bytes_read)
  145. {
  146. #if BUILD_FT2232_FTD2XX == 1
  147. DWORD dw_bytes_read;
  148. FT_STATUS status;
  149. if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
  150. {
  151. *bytes_read = dw_bytes_read;
  152. ERROR("FT_Read returned: %i", status);
  153. return ERROR_JTAG_DEVICE_ERROR;
  154. }
  155. *bytes_read = dw_bytes_read;
  156. return ERROR_OK;
  157. #elif BUILD_FT2232_LIBFTDI == 1
  158. int retval;
  159. int timeout = 100;
  160. *bytes_read = 0;
  161. while ((*bytes_read < size) && timeout--)
  162. {
  163. if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
  164. {
  165. *bytes_read = 0;
  166. ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
  167. return ERROR_JTAG_DEVICE_ERROR;
  168. }
  169. *bytes_read += retval;
  170. }
  171. return ERROR_OK;
  172. #endif
  173. }
  174. int ft2232_speed(int speed)
  175. {
  176. u8 buf[3];
  177. int retval;
  178. u32 bytes_written;
  179. buf[0] = 0x86; /* command "set divisor" */
  180. buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
  181. buf[2] = (speed >> 8) & 0xff; /* valueH */
  182. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  183. if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  184. {
  185. ERROR("couldn't set FT2232 TCK speed");
  186. return retval;
  187. }
  188. return ERROR_OK;
  189. }
  190. int ft2232_register_commands(struct command_context_s *cmd_ctx)
  191. {
  192. register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
  193. COMMAND_CONFIG, NULL);
  194. register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
  195. COMMAND_CONFIG, NULL);
  196. register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
  197. COMMAND_CONFIG, NULL);
  198. return ERROR_OK;
  199. }
  200. void ft2232_end_state(state)
  201. {
  202. if (tap_move_map[state] != -1)
  203. end_state = state;
  204. else
  205. {
  206. ERROR("BUG: %i is not a valid end state", state);
  207. exit(-1);
  208. }
  209. }
  210. void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
  211. {
  212. int num_bytes = ((scan_size + 7) / 8);
  213. int bits_left = scan_size;
  214. int cur_byte = 0;
  215. while(num_bytes-- > 1)
  216. {
  217. buffer[cur_byte] = BUFFER_READ;
  218. cur_byte++;
  219. bits_left -= 8;
  220. }
  221. buffer[cur_byte] = 0x0;
  222. if (bits_left > 1)
  223. {
  224. buffer[cur_byte] = BUFFER_READ >> 1;
  225. }
  226. buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
  227. }
  228. void ft2232_debug_dump_buffer(void)
  229. {
  230. int i;
  231. char line[256];
  232. char *line_p = line;
  233. for (i = 0; i < ft2232_buffer_size; i++)
  234. {
  235. line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
  236. if (i % 16 == 15)
  237. {
  238. DEBUG("%s", line);
  239. line_p = line;
  240. }
  241. }
  242. if (line_p != line)
  243. DEBUG("%s", line);
  244. }
  245. int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
  246. {
  247. jtag_command_t *cmd;
  248. u8 *buffer;
  249. int scan_size;
  250. enum scan_type type;
  251. int retval;
  252. u32 bytes_written;
  253. u32 bytes_read;
  254. #ifdef _DEBUG_USB_IO_
  255. struct timeval start, inter, inter2, end;
  256. struct timeval d_inter, d_inter2, d_end;
  257. #endif
  258. #ifdef _DEBUG_USB_COMMS_
  259. DEBUG("write buffer (size %i):", ft2232_buffer_size);
  260. ft2232_debug_dump_buffer();
  261. #endif
  262. #ifdef _DEBUG_USB_IO_
  263. gettimeofday(&start, NULL);
  264. #endif
  265. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  266. {
  267. ERROR("couldn't write MPSSE commands to FT2232");
  268. exit(-1);
  269. }
  270. #ifdef _DEBUG_USB_IO_
  271. gettimeofday(&inter, NULL);
  272. #endif
  273. if (ft2232_expect_read)
  274. {
  275. int timeout = 100;
  276. ft2232_buffer_size = 0;
  277. #ifdef _DEBUG_USB_IO_
  278. gettimeofday(&inter2, NULL);
  279. #endif
  280. if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
  281. {
  282. ERROR("couldn't read from FT2232");
  283. exit(-1);
  284. }
  285. #ifdef _DEBUG_USB_IO_
  286. gettimeofday(&end, NULL);
  287. timeval_subtract(&d_inter, &inter, &start);
  288. timeval_subtract(&d_inter2, &inter2, &start);
  289. timeval_subtract(&d_end, &end, &start);
  290. 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);
  291. #endif
  292. ft2232_buffer_size = bytes_read;
  293. if (ft2232_expect_read != ft2232_buffer_size)
  294. {
  295. ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
  296. ft2232_debug_dump_buffer();
  297. exit(-1);
  298. }
  299. #ifdef _DEBUG_USB_COMMS_
  300. DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
  301. ft2232_debug_dump_buffer();
  302. #endif
  303. }
  304. ft2232_expect_read = 0;
  305. ft2232_read_pointer = 0;
  306. cmd = first;
  307. while (cmd != last)
  308. {
  309. switch (cmd->type)
  310. {
  311. case JTAG_SCAN:
  312. type = jtag_scan_type(cmd->cmd.scan);
  313. if (type != SCAN_OUT)
  314. {
  315. scan_size = jtag_scan_size(cmd->cmd.scan);
  316. buffer = calloc(CEIL(scan_size, 8), 1);
  317. ft2232_read_scan(type, buffer, scan_size);
  318. jtag_read_buffer(buffer, cmd->cmd.scan);
  319. free(buffer);
  320. }
  321. break;
  322. default:
  323. break;
  324. }
  325. cmd = cmd->next;
  326. }
  327. ft2232_buffer_size = 0;
  328. return ERROR_OK;
  329. }
  330. void ft2232_add_pathmove(pathmove_command_t *cmd)
  331. {
  332. int num_states = cmd->num_states;
  333. u8 tms_byte;
  334. int state_count;
  335. state_count = 0;
  336. while (num_states)
  337. {
  338. tms_byte = 0x0;
  339. int bit_count = 0;
  340. /* command "Clock Data to TMS/CS Pin (no Read)" */
  341. BUFFER_ADD = 0x4b;
  342. /* number of states remaining */
  343. BUFFER_ADD = (num_states % 7) - 1;
  344. while (num_states % 7)
  345. {
  346. if (tap_transitions[cur_state].low == cmd->path[state_count])
  347. buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
  348. else if (tap_transitions[cur_state].high == cmd->path[state_count])
  349. buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
  350. else
  351. {
  352. ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
  353. exit(-1);
  354. }
  355. cur_state = cmd->path[state_count];
  356. state_count++;
  357. num_states--;
  358. }
  359. BUFFER_ADD = tms_byte;
  360. }
  361. end_state = cur_state;
  362. }
  363. void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  364. {
  365. int num_bytes = (scan_size + 7) / 8;
  366. int bits_left = scan_size;
  367. int cur_byte = 0;
  368. int last_bit;
  369. if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
  370. {
  371. /* command "Clock Data to TMS/CS Pin (no Read)" */
  372. BUFFER_ADD = 0x4b;
  373. /* scan 7 bit */
  374. BUFFER_ADD = 0x6;
  375. /* TMS data bits */
  376. if (ir_scan)
  377. {
  378. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
  379. cur_state = TAP_SI;
  380. }
  381. else
  382. {
  383. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
  384. cur_state = TAP_SD;
  385. }
  386. //DEBUG("added TMS scan (no read)");
  387. }
  388. /* add command for complete bytes */
  389. if (num_bytes > 1)
  390. {
  391. if (type == SCAN_IO)
  392. {
  393. /* Clock Data Bytes In and Out LSB First */
  394. BUFFER_ADD = 0x39;
  395. //DEBUG("added TDI bytes (io %i)", num_bytes);
  396. }
  397. else if (type == SCAN_OUT)
  398. {
  399. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  400. BUFFER_ADD = 0x19;
  401. //DEBUG("added TDI bytes (o)");
  402. }
  403. else if (type == SCAN_IN)
  404. {
  405. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  406. BUFFER_ADD = 0x28;
  407. //DEBUG("added TDI bytes (i %i)", num_bytes);
  408. }
  409. BUFFER_ADD = (num_bytes-2) & 0xff;
  410. BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
  411. }
  412. if (type != SCAN_IN)
  413. {
  414. /* add complete bytes */
  415. while(num_bytes-- > 1)
  416. {
  417. BUFFER_ADD = buffer[cur_byte];
  418. cur_byte++;
  419. bits_left -= 8;
  420. }
  421. }
  422. if (type == SCAN_IN)
  423. {
  424. bits_left -= 8 * (num_bytes - 1);
  425. }
  426. /* the most signifcant bit is scanned during TAP movement */
  427. if (type != SCAN_IN)
  428. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  429. else
  430. last_bit = 0;
  431. /* process remaining bits but the last one */
  432. if (bits_left > 1)
  433. {
  434. if (type == SCAN_IO)
  435. {
  436. /* Clock Data Bits In and Out LSB First */
  437. BUFFER_ADD = 0x3b;
  438. //DEBUG("added TDI bits (io) %i", bits_left - 1);
  439. }
  440. else if (type == SCAN_OUT)
  441. {
  442. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  443. BUFFER_ADD = 0x1b;
  444. //DEBUG("added TDI bits (o)");
  445. }
  446. else if (type == SCAN_IN)
  447. {
  448. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  449. BUFFER_ADD = 0x2a;
  450. //DEBUG("added TDI bits (i %i)", bits_left - 1);
  451. }
  452. BUFFER_ADD = bits_left - 2;
  453. if (type != SCAN_IN)
  454. BUFFER_ADD = buffer[cur_byte];
  455. }
  456. /* move from Shift-IR/DR to end state */
  457. if (type != SCAN_OUT)
  458. {
  459. /* Clock Data to TMS/CS Pin with Read */
  460. BUFFER_ADD = 0x6b;
  461. //DEBUG("added TMS scan (read)");
  462. }
  463. else
  464. {
  465. /* Clock Data to TMS/CS Pin (no Read) */
  466. BUFFER_ADD = 0x4b;
  467. //DEBUG("added TMS scan (no read)");
  468. }
  469. BUFFER_ADD = 0x6;
  470. BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
  471. cur_state = end_state;
  472. }
  473. int ft2232_predict_scan_out(int scan_size, enum scan_type type)
  474. {
  475. int predicted_size = 3;
  476. if (cur_state != TAP_SD)
  477. predicted_size += 3;
  478. if (type == SCAN_IN) /* only from device to host */
  479. {
  480. /* complete bytes */
  481. predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
  482. /* remaining bits - 1 (up to 7) */
  483. predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
  484. }
  485. else /* host to device, or bidirectional */
  486. {
  487. /* complete bytes */
  488. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
  489. /* remaining bits -1 (up to 7) */
  490. predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
  491. }
  492. return predicted_size;
  493. }
  494. int ft2232_predict_scan_in(int scan_size, enum scan_type type)
  495. {
  496. int predicted_size = 0;
  497. if (type != SCAN_OUT)
  498. {
  499. /* complete bytes */
  500. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
  501. /* remaining bits - 1 */
  502. predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
  503. /* last bit (from TMS scan) */
  504. predicted_size += 1;
  505. }
  506. //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
  507. return predicted_size;
  508. }
  509. void usbjtag_reset(int trst, int srst)
  510. {
  511. if (trst == 1)
  512. {
  513. cur_state = TAP_TLR;
  514. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  515. low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
  516. else
  517. low_output &= ~nTRST; /* switch output low */
  518. }
  519. else if (trst == 0)
  520. {
  521. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  522. low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
  523. else
  524. low_output |= nTRST; /* switch output high */
  525. }
  526. if (srst == 1)
  527. {
  528. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  529. low_output &= ~nSRST; /* switch output low */
  530. else
  531. low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
  532. }
  533. else if (srst == 0)
  534. {
  535. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  536. low_output |= nSRST; /* switch output high */
  537. else
  538. low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
  539. }
  540. /* command "set data bits low byte" */
  541. BUFFER_ADD = 0x80;
  542. BUFFER_ADD = low_output;
  543. BUFFER_ADD = low_direction;
  544. }
  545. void jtagkey_reset(int trst, int srst)
  546. {
  547. if (trst == 1)
  548. {
  549. cur_state = TAP_TLR;
  550. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  551. high_output &= ~nTRSTnOE;
  552. else
  553. high_output &= ~nTRST;
  554. }
  555. else if (trst == 0)
  556. {
  557. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  558. high_output |= nTRSTnOE;
  559. else
  560. high_output |= nTRST;
  561. }
  562. if (srst == 1)
  563. {
  564. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  565. high_output &= ~nSRST;
  566. else
  567. high_output &= ~nSRSTnOE;
  568. }
  569. else if (srst == 0)
  570. {
  571. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  572. high_output |= nSRST;
  573. else
  574. high_output |= nSRSTnOE;
  575. }
  576. /* command "set data bits high byte" */
  577. BUFFER_ADD = 0x82;
  578. BUFFER_ADD = high_output;
  579. BUFFER_ADD = high_direction;
  580. DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  581. }
  582. int ft2232_execute_queue()
  583. {
  584. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  585. jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
  586. u8 *buffer;
  587. int scan_size; /* size of IR or DR scan */
  588. enum scan_type type;
  589. int i;
  590. int predicted_size = 0;
  591. int require_send = 0;
  592. ft2232_buffer_size = 0;
  593. ft2232_expect_read = 0;
  594. while (cmd)
  595. {
  596. switch(cmd->type)
  597. {
  598. case JTAG_END_STATE:
  599. if (cmd->cmd.end_state->end_state != -1)
  600. ft2232_end_state(cmd->cmd.end_state->end_state);
  601. break;
  602. case JTAG_RESET:
  603. /* only send the maximum buffer size that FT2232C can handle */
  604. predicted_size = 3;
  605. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  606. {
  607. ft2232_send_and_recv(first_unsent, cmd);
  608. require_send = 0;
  609. first_unsent = cmd;
  610. }
  611. layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  612. require_send = 1;
  613. #ifdef _DEBUG_JTAG_IO_
  614. DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  615. #endif
  616. break;
  617. case JTAG_RUNTEST:
  618. /* only send the maximum buffer size that FT2232C can handle */
  619. predicted_size = 0;
  620. if (cur_state != TAP_RTI)
  621. predicted_size += 3;
  622. predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
  623. if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
  624. predicted_size += 3;
  625. if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
  626. predicted_size += 3;
  627. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  628. {
  629. ft2232_send_and_recv(first_unsent, cmd);
  630. require_send = 0;
  631. first_unsent = cmd;
  632. }
  633. if (cur_state != TAP_RTI)
  634. {
  635. /* command "Clock Data to TMS/CS Pin (no Read)" */
  636. BUFFER_ADD = 0x4b;
  637. /* scan 7 bit */
  638. BUFFER_ADD = 0x6;
  639. /* TMS data bits */
  640. BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
  641. cur_state = TAP_RTI;
  642. require_send = 1;
  643. }
  644. i = cmd->cmd.runtest->num_cycles;
  645. while (i > 0)
  646. {
  647. /* command "Clock Data to TMS/CS Pin (no Read)" */
  648. BUFFER_ADD = 0x4b;
  649. /* scan 7 bit */
  650. BUFFER_ADD = (i > 7) ? 6 : (i - 1);
  651. /* TMS data bits */
  652. BUFFER_ADD = 0x0;
  653. cur_state = TAP_RTI;
  654. i -= (i > 7) ? 7 : i;
  655. //DEBUG("added TMS scan (no read)");
  656. }
  657. if (cmd->cmd.runtest->end_state != -1)
  658. ft2232_end_state(cmd->cmd.runtest->end_state);
  659. if (cur_state != end_state)
  660. {
  661. /* command "Clock Data to TMS/CS Pin (no Read)" */
  662. BUFFER_ADD = 0x4b;
  663. /* scan 7 bit */
  664. BUFFER_ADD = 0x6;
  665. /* TMS data bits */
  666. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  667. cur_state = end_state;
  668. //DEBUG("added TMS scan (no read)");
  669. }
  670. require_send = 1;
  671. #ifdef _DEBUG_JTAG_IO_
  672. DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
  673. #endif
  674. break;
  675. case JTAG_STATEMOVE:
  676. /* only send the maximum buffer size that FT2232C can handle */
  677. predicted_size = 3;
  678. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  679. {
  680. ft2232_send_and_recv(first_unsent, cmd);
  681. require_send = 0;
  682. first_unsent = cmd;
  683. }
  684. if (cmd->cmd.statemove->end_state != -1)
  685. ft2232_end_state(cmd->cmd.statemove->end_state);
  686. /* command "Clock Data to TMS/CS Pin (no Read)" */
  687. BUFFER_ADD = 0x4b;
  688. /* scan 7 bit */
  689. BUFFER_ADD = 0x6;
  690. /* TMS data bits */
  691. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  692. //DEBUG("added TMS scan (no read)");
  693. cur_state = end_state;
  694. require_send = 1;
  695. #ifdef _DEBUG_JTAG_IO_
  696. DEBUG("statemove: %i", end_state);
  697. #endif
  698. break;
  699. case JTAG_PATHMOVE:
  700. /* only send the maximum buffer size that FT2232C can handle */
  701. predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
  702. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  703. {
  704. ft2232_send_and_recv(first_unsent, cmd);
  705. require_send = 0;
  706. first_unsent = cmd;
  707. }
  708. ft2232_add_pathmove(cmd->cmd.pathmove);
  709. require_send = 1;
  710. #ifdef _DEBUG_JTAG_IO_
  711. DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  712. #endif
  713. break;
  714. case JTAG_SCAN:
  715. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  716. type = jtag_scan_type(cmd->cmd.scan);
  717. predicted_size = ft2232_predict_scan_out(scan_size, type);
  718. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  719. {
  720. DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
  721. ft2232_send_and_recv(first_unsent, cmd);
  722. require_send = 0;
  723. first_unsent = cmd;
  724. }
  725. ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
  726. //DEBUG("new read size: %i", ft2232_expect_read);
  727. if (cmd->cmd.scan->end_state != -1)
  728. ft2232_end_state(cmd->cmd.scan->end_state);
  729. ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  730. require_send = 1;
  731. if (buffer)
  732. free(buffer);
  733. #ifdef _DEBUG_JTAG_IO_
  734. DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
  735. #endif
  736. break;
  737. case JTAG_SLEEP:
  738. ft2232_send_and_recv(first_unsent, cmd);
  739. first_unsent = cmd->next;
  740. jtag_sleep(cmd->cmd.sleep->us);
  741. #ifdef _DEBUG_JTAG_IO_
  742. DEBUG("sleep %i usec", cmd->cmd.sleep->us);
  743. #endif
  744. break;
  745. default:
  746. ERROR("BUG: unknown JTAG command type encountered");
  747. exit(-1);
  748. }
  749. cmd = cmd->next;
  750. }
  751. if (require_send > 0)
  752. ft2232_send_and_recv(first_unsent, cmd);
  753. return ERROR_OK;
  754. }
  755. int ft2232_init(void)
  756. {
  757. u8 latency_timer;
  758. u8 buf[1];
  759. int retval;
  760. u32 bytes_written;
  761. #if BUILD_FT2232_FTD2XX == 1
  762. FT_STATUS status;
  763. #endif
  764. ft2232_layout_t *cur_layout = ft2232_layouts;
  765. if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
  766. {
  767. ft2232_layout = "usbjtag";
  768. WARNING("No ft2232 layout specified, using default 'usbjtag'");
  769. }
  770. while (cur_layout->name)
  771. {
  772. if (strcmp(cur_layout->name, ft2232_layout) == 0)
  773. {
  774. layout = cur_layout;
  775. break;
  776. }
  777. cur_layout++;
  778. }
  779. if (!layout)
  780. {
  781. ERROR("No matching layout found for %s", ft2232_layout);
  782. return ERROR_JTAG_INIT_FAILED;
  783. }
  784. #if BUILD_FT2232_FTD2XX == 1
  785. DEBUG("'ft2232' interface using FTD2XX with '%s' layout", ft2232_layout);
  786. #elif BUILD_FT2232_LIBFTDI == 1
  787. DEBUG("'ft2232' interface using libftdi with '%s' layout", ft2232_layout);
  788. #endif
  789. if (ft2232_device_desc == NULL)
  790. {
  791. WARNING("no ftd2xx device description specified, using default 'Dual RS232'");
  792. ft2232_device_desc = "Dual RS232";
  793. }
  794. #if BUILD_FT2232_FTD2XX == 1
  795. #if IS_WIN32 == 0
  796. /* Add non-standard Vid/Pid to the linux driver */
  797. if ((status = FT_SetVIDPID(ft2232_vid, ft2232_pid)) != FT_OK)
  798. {
  799. WARNING("couldn't add %4.4x:%4.4x", ft2232_vid, ft2232_pid);
  800. }
  801. #endif
  802. if ((status = FT_OpenEx(ft2232_device_desc, FT_OPEN_BY_DESCRIPTION, &ftdih)) != FT_OK)
  803. {
  804. DWORD num_devices;
  805. ERROR("unable to open ftdi device: %i", status);
  806. status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
  807. if (status == FT_OK)
  808. {
  809. char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
  810. int i;
  811. for (i = 0; i < num_devices; i++)
  812. desc_array[i] = malloc(64);
  813. desc_array[num_devices] = NULL;
  814. status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
  815. if (status == FT_OK)
  816. {
  817. ERROR("ListDevices: %d\n", num_devices);
  818. for (i = 0; i < num_devices; i++)
  819. ERROR("%i: %s", i, desc_array[i]);
  820. }
  821. for (i = 0; i < num_devices; i++)
  822. free(desc_array[i]);
  823. free(desc_array);
  824. }
  825. else
  826. {
  827. printf("ListDevices: NONE\n");
  828. }
  829. return ERROR_JTAG_INIT_FAILED;
  830. }
  831. if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
  832. {
  833. ERROR("unable to set latency timer: %i", status);
  834. return ERROR_JTAG_INIT_FAILED;
  835. }
  836. if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
  837. {
  838. ERROR("unable to get latency timer: %i", status);
  839. return ERROR_JTAG_INIT_FAILED;
  840. }
  841. else
  842. {
  843. DEBUG("current latency timer: %i", latency_timer);
  844. }
  845. if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
  846. {
  847. ERROR("unable to enable bit i/o mode: %i", status);
  848. return ERROR_JTAG_INIT_FAILED;
  849. }
  850. #elif BUILD_FT2232_LIBFTDI == 1
  851. if (ftdi_init(&ftdic) < 0)
  852. return ERROR_JTAG_INIT_FAILED;
  853. /* context, vendor id, product id */
  854. if (ftdi_usb_open(&ftdic, ft2232_vid, ft2232_pid) < 0)
  855. {
  856. ERROR("unable to open ftdi device: %s", ftdic.error_str);
  857. return ERROR_JTAG_INIT_FAILED;
  858. }
  859. if (ftdi_usb_reset(&ftdic) < 0)
  860. {
  861. ERROR("unable to reset ftdi device");
  862. return ERROR_JTAG_INIT_FAILED;
  863. }
  864. if (ftdi_set_latency_timer(&ftdic, 2) < 0)
  865. {
  866. ERROR("unable to set latency timer");
  867. return ERROR_JTAG_INIT_FAILED;
  868. }
  869. if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
  870. {
  871. ERROR("unable to get latency timer");
  872. return ERROR_JTAG_INIT_FAILED;
  873. }
  874. else
  875. {
  876. DEBUG("current latency timer: %i", latency_timer);
  877. }
  878. ftdic.bitbang_mode = 0; /* Reset controller */
  879. ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
  880. ftdic.bitbang_mode = 2; /* MPSSE mode */
  881. ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
  882. #endif
  883. ft2232_buffer_size = 0;
  884. ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
  885. if (layout->init() != ERROR_OK)
  886. return ERROR_JTAG_INIT_FAILED;
  887. ft2232_speed(jtag_speed);
  888. buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
  889. if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
  890. {
  891. ERROR("couldn't write to FT2232 to disable loopback");
  892. return ERROR_JTAG_INIT_FAILED;
  893. }
  894. #if BUILD_FT2232_FTD2XX == 1
  895. if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
  896. {
  897. ERROR("error purging ftd2xx device: %i", status);
  898. return ERROR_JTAG_INIT_FAILED;
  899. }
  900. #elif BUILD_FT2232_LIBFTDI == 1
  901. if (ftdi_usb_purge_buffers(&ftdic) < 0)
  902. {
  903. ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  904. return ERROR_JTAG_INIT_FAILED;
  905. }
  906. #endif
  907. return ERROR_OK;
  908. }
  909. int usbjtag_init(void)
  910. {
  911. u8 buf[3];
  912. u32 bytes_written;
  913. low_output = 0x08;
  914. low_direction = 0x0b;
  915. nTRST = 0x10;
  916. nTRSTnOE = 0x10;
  917. nSRST = 0x40;
  918. nSRSTnOE = 0x40;
  919. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  920. {
  921. low_direction &= ~nTRSTnOE; /* nTRST input */
  922. low_output &= ~nTRST; /* nTRST = 0 */
  923. }
  924. else
  925. {
  926. low_direction |= nTRSTnOE; /* nTRST output */
  927. low_output |= nTRST; /* nTRST = 1 */
  928. }
  929. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  930. {
  931. low_direction |= nSRSTnOE; /* nSRST output */
  932. low_output |= nSRST; /* nSRST = 1 */
  933. }
  934. else
  935. {
  936. low_direction &= ~nSRSTnOE; /* nSRST input */
  937. low_output &= ~nSRST; /* nSRST = 0 */
  938. }
  939. /* initialize low byte for jtag */
  940. buf[0] = 0x80; /* command "set data bits low byte" */
  941. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
  942. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
  943. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  944. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  945. {
  946. ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
  947. return ERROR_JTAG_INIT_FAILED;
  948. }
  949. return ERROR_OK;
  950. }
  951. int jtagkey_init(void)
  952. {
  953. u8 buf[3];
  954. u32 bytes_written;
  955. low_output = 0x08;
  956. low_direction = 0x1b;
  957. /* initialize low byte for jtag */
  958. buf[0] = 0x80; /* command "set data bits low byte" */
  959. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  960. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
  961. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  962. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  963. {
  964. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  965. return ERROR_JTAG_INIT_FAILED;
  966. }
  967. if (strcmp(layout->name, "jtagkey") == 0)
  968. {
  969. nTRST = 0x01;
  970. nTRSTnOE = 0x4;
  971. nSRST = 0x02;
  972. nSRSTnOE = 0x08;
  973. }
  974. else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
  975. {
  976. nTRST = 0x02;
  977. nTRSTnOE = 0x1;
  978. nSRST = 0x08;
  979. nSRSTnOE = 0x04;
  980. }
  981. else
  982. {
  983. ERROR("BUG: jtagkey_init called for non jtagkey layout");
  984. exit(-1);
  985. }
  986. high_output = 0x0;
  987. high_direction = 0x0f;
  988. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  989. {
  990. high_output |= nTRSTnOE;
  991. high_output &= ~nTRST;
  992. }
  993. else
  994. {
  995. high_output &= ~nTRSTnOE;
  996. high_output |= nTRST;
  997. }
  998. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  999. {
  1000. high_output &= ~nSRSTnOE;
  1001. high_output |= nSRST;
  1002. }
  1003. else
  1004. {
  1005. high_output |= nSRSTnOE;
  1006. high_output &= ~nSRST;
  1007. }
  1008. /* initialize high port */
  1009. buf[0] = 0x82; /* command "set data bits low byte" */
  1010. buf[1] = high_output; /* value */
  1011. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1012. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1013. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1014. {
  1015. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1016. return ERROR_JTAG_INIT_FAILED;
  1017. }
  1018. return ERROR_OK;
  1019. }
  1020. int ft2232_quit(void)
  1021. {
  1022. #if BUILD_FT2232_FTD2XX == 1
  1023. FT_STATUS status;
  1024. status = FT_Close(ftdih);
  1025. #elif BUILD_FT2232_LIBFTDI == 1
  1026. ftdi_disable_bitbang(&ftdic);
  1027. ftdi_usb_close(&ftdic);
  1028. ftdi_deinit(&ftdic);
  1029. #endif
  1030. free(ft2232_buffer);
  1031. return ERROR_OK;
  1032. }
  1033. int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1034. {
  1035. if (argc == 1)
  1036. {
  1037. ft2232_device_desc = strdup(args[0]);
  1038. }
  1039. else
  1040. {
  1041. ERROR("expected exactly one argument to ft2232_device_desc <description>");
  1042. }
  1043. return ERROR_OK;
  1044. }
  1045. int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1046. {
  1047. if (argc == 0)
  1048. return ERROR_OK;
  1049. ft2232_layout = malloc(strlen(args[0]) + 1);
  1050. strcpy(ft2232_layout, args[0]);
  1051. return ERROR_OK;
  1052. }
  1053. int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1054. {
  1055. if (argc >= 2)
  1056. {
  1057. ft2232_vid = strtol(args[0], NULL, 0);
  1058. ft2232_pid = strtol(args[1], NULL, 0);
  1059. }
  1060. else
  1061. {
  1062. WARNING("incomplete ft2232_vid_pid configuration directive");
  1063. }
  1064. return ERROR_OK;
  1065. }