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.
 
 
 
 
 
 

634 lines
16 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2004 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. /* project specific includes */
  24. #include "log.h"
  25. #include "types.h"
  26. #include "jtag.h"
  27. #include "configuration.h"
  28. #include "command.h"
  29. /* system includes */
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <usb.h>
  34. #include <ftdi.h>
  35. #include <sys/time.h>
  36. #include <time.h>
  37. /* enable this to debug io latency
  38. */
  39. #if 0
  40. #define _DEBUG_USB_IO_
  41. #endif
  42. int ftdi2232_execute_queue(void);
  43. int ftdi2232_speed(int speed);
  44. int ftdi2232_register_commands(struct command_context_s *cmd_ctx);
  45. int ftdi2232_init(void);
  46. int ftdi2232_quit(void);
  47. enum { FTDI2232_TRST = 0x10, FTDI2232_SRST = 0x40 };
  48. static u8 discrete_output = 0x0 | FTDI2232_TRST | FTDI2232_SRST;
  49. static struct ftdi_context ftdic;
  50. static u8 *ftdi2232_buffer = NULL;
  51. static int ftdi2232_buffer_size = 0;
  52. static int ftdi2232_read_pointer = 0;
  53. static int ftdi2232_expect_read = 0;
  54. #define FTDI2232_BUFFER_SIZE 131072
  55. #define BUFFER_ADD ftdi2232_buffer[ftdi2232_buffer_size++]
  56. #define BUFFER_READ ftdi2232_buffer[ftdi2232_read_pointer++]
  57. #define FTDI2232_SAVE_SIZE 1024
  58. int ftdi2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  59. static u16 ftdi2232_vid = 0x0403;
  60. static u16 ftdi2232_pid = 0x6010;
  61. jtag_interface_t ftdi2232_interface =
  62. {
  63. .name = "ftdi2232",
  64. .execute_queue = ftdi2232_execute_queue,
  65. .support_statemove = 1,
  66. .speed = ftdi2232_speed,
  67. .register_commands = ftdi2232_register_commands,
  68. .init = ftdi2232_init,
  69. .quit = ftdi2232_quit,
  70. };
  71. int ftdi2232_speed(int speed)
  72. {
  73. u8 buf[3];
  74. buf[0] = 0x86; /* command "set divisor" */
  75. buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
  76. buf[2] = (speed >> 8) & 0xff; /* valueH */
  77. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  78. ftdi_write_data(&ftdic, buf, 3);
  79. return ERROR_OK;
  80. }
  81. int ftdi2232_register_commands(struct command_context_s *cmd_ctx)
  82. {
  83. register_command(cmd_ctx, NULL, "ftdi2232_vid_pid", ftdi2232_handle_vid_pid_command,
  84. COMMAND_CONFIG, NULL);
  85. return ERROR_OK;
  86. }
  87. void ftdi2232_end_state(state)
  88. {
  89. if (tap_move_map[state] != -1)
  90. end_state = state;
  91. else
  92. {
  93. ERROR("BUG: %i is not a valid end state", state);
  94. exit(-1);
  95. }
  96. }
  97. void ftdi2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
  98. {
  99. int num_bytes = ((scan_size + 7) / 8);
  100. int bits_left = scan_size;
  101. int cur_byte = 0;
  102. while(num_bytes-- > 1)
  103. {
  104. buffer[cur_byte] = BUFFER_READ;
  105. cur_byte++;
  106. bits_left -= 8;
  107. }
  108. buffer[cur_byte] = 0x0;
  109. if (bits_left > 1)
  110. {
  111. buffer[cur_byte] = BUFFER_READ >> 1;
  112. }
  113. buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
  114. }
  115. void ftdi2232_debug_dump_buffer(void)
  116. {
  117. int i;
  118. for (i = 0; i < ftdi2232_buffer_size; i++)
  119. {
  120. printf("%2.2x ", ftdi2232_buffer[i]);
  121. if (i % 16 == 15)
  122. printf("\n");
  123. }
  124. printf("\n");
  125. fflush(stdout);
  126. }
  127. int ftdi2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
  128. {
  129. jtag_command_t *cmd;
  130. u8 *buffer;
  131. int scan_size;
  132. enum scan_type type;
  133. int retval;
  134. BUFFER_ADD = 0x87; /* send immediate command */
  135. if (ftdi2232_buffer_size > FTDI2232_SAVE_SIZE)
  136. {
  137. ERROR("BUG: ftdi2232_buffer grew beyond %i byte (%i) - this is going to fail", FTDI2232_SAVE_SIZE, ftdi2232_buffer_size);
  138. }
  139. #ifdef _DEBUG_USB_IO_
  140. DEBUG("write buffer (size %i):", ftdi2232_buffer_size);
  141. ftdi2232_debug_dump_buffer();
  142. #endif
  143. if ((retval = ftdi_write_data(&ftdic, ftdi2232_buffer, ftdi2232_buffer_size)) < 0)
  144. {
  145. ERROR("ftdi_write_data returned %i", retval);
  146. exit(-1);
  147. }
  148. if (ftdi2232_expect_read)
  149. {
  150. int timeout = 100;
  151. ftdi2232_buffer_size = 0;
  152. while ((ftdi2232_buffer_size < ftdi2232_expect_read) && timeout)
  153. {
  154. ftdi2232_buffer_size += ftdi_read_data(&ftdic, ftdi2232_buffer + ftdi2232_buffer_size, FTDI2232_BUFFER_SIZE - ftdi2232_buffer_size);
  155. timeout--;
  156. }
  157. if (ftdi2232_expect_read != ftdi2232_buffer_size)
  158. {
  159. ERROR("ftdi2232_expect_read (%i) != ftdi2232_buffer_size (%i) (%i retries)", ftdi2232_expect_read, ftdi2232_buffer_size, 100 - timeout);
  160. ftdi2232_debug_dump_buffer();
  161. exit(-1);
  162. }
  163. #ifdef _DEBUG_USB_IO_
  164. DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ftdi2232_buffer_size);
  165. ftdi2232_debug_dump_buffer();
  166. #endif
  167. }
  168. ftdi2232_expect_read = 0;
  169. ftdi2232_read_pointer = 0;
  170. cmd = first;
  171. while (cmd != last)
  172. {
  173. switch (cmd->type)
  174. {
  175. case JTAG_SCAN:
  176. type = jtag_scan_type(cmd->cmd.scan);
  177. if (type != SCAN_OUT)
  178. {
  179. scan_size = jtag_scan_size(cmd->cmd.scan);
  180. buffer = calloc(CEIL(scan_size, 8), 1);
  181. ftdi2232_read_scan(type, buffer, scan_size);
  182. jtag_read_buffer(buffer, cmd->cmd.scan);
  183. free(buffer);
  184. }
  185. break;
  186. default:
  187. break;
  188. }
  189. cmd = cmd->next;
  190. }
  191. ftdi2232_buffer_size = 0;
  192. return ERROR_OK;
  193. }
  194. void ftdi2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  195. {
  196. int num_bytes = (scan_size + 7) / 8;
  197. int bits_left = scan_size;
  198. int cur_byte = 0;
  199. int last_bit;
  200. /* command "Clock Data to TMS/CS Pin (no Read)" */
  201. BUFFER_ADD = 0x4b;
  202. /* scan 7 bit */
  203. BUFFER_ADD = 0x6;
  204. /* TMS data bits */
  205. if (ir_scan)
  206. {
  207. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
  208. cur_state = TAP_SI;
  209. }
  210. else
  211. {
  212. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
  213. cur_state = TAP_SD;
  214. }
  215. //DEBUG("added TMS scan (no read)");
  216. /* add command for complete bytes */
  217. if (num_bytes > 1)
  218. {
  219. if (type == SCAN_IO)
  220. {
  221. /* Clock Data Bytes In and Out LSB First */
  222. BUFFER_ADD = 0x39;
  223. //DEBUG("added TDI bytes (io %i)", num_bytes);
  224. }
  225. else if (type == SCAN_OUT)
  226. {
  227. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  228. BUFFER_ADD = 0x19;
  229. //DEBUG("added TDI bytes (o)");
  230. }
  231. else if (type == SCAN_IN)
  232. {
  233. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  234. BUFFER_ADD = 0x28;
  235. //DEBUG("added TDI bytes (i %i)", num_bytes);
  236. }
  237. BUFFER_ADD = (num_bytes-2) & 0xff;
  238. BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
  239. }
  240. if (type != SCAN_IN)
  241. {
  242. /* add complete bytes */
  243. while(num_bytes-- > 1)
  244. {
  245. BUFFER_ADD = buffer[cur_byte];
  246. cur_byte++;
  247. bits_left -= 8;
  248. }
  249. }
  250. if (type == SCAN_IN)
  251. {
  252. bits_left -= 8 * (num_bytes - 1);
  253. }
  254. /* the most signifcant bit is scanned during TAP movement */
  255. if (type != SCAN_IN)
  256. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  257. else
  258. last_bit = 0;
  259. /* process remaining bits but the last one */
  260. if (bits_left > 1)
  261. {
  262. if (type == SCAN_IO)
  263. {
  264. /* Clock Data Bits In and Out LSB First */
  265. BUFFER_ADD = 0x3b;
  266. //DEBUG("added TDI bits (io) %i", bits_left - 1);
  267. }
  268. else if (type == SCAN_OUT)
  269. {
  270. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  271. BUFFER_ADD = 0x1b;
  272. //DEBUG("added TDI bits (o)");
  273. }
  274. else if (type == SCAN_IN)
  275. {
  276. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  277. BUFFER_ADD = 0x2a;
  278. //DEBUG("added TDI bits (i %i)", bits_left - 1);
  279. }
  280. BUFFER_ADD = bits_left - 2;
  281. if (type != SCAN_IN)
  282. BUFFER_ADD = buffer[cur_byte];
  283. }
  284. /* move from Shift-IR/DR to end state */
  285. if (type != SCAN_OUT)
  286. {
  287. /* Clock Data to TMS/CS Pin with Read */
  288. BUFFER_ADD = 0x6b;
  289. //DEBUG("added TMS scan (read)");
  290. }
  291. else
  292. {
  293. /* Clock Data to TMS/CS Pin (no Read) */
  294. BUFFER_ADD = 0x4b;
  295. //DEBUG("added TMS scan (no read)");
  296. }
  297. BUFFER_ADD = 0x6;
  298. BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
  299. cur_state = end_state;
  300. }
  301. int ftdi2232_predict_scan_out(int scan_size, enum scan_type type)
  302. {
  303. int predicted_size = 6;
  304. if (type == SCAN_IN) /* only from device to host */
  305. {
  306. predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
  307. predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
  308. }
  309. else /* host to device, or bidirectional */
  310. {
  311. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
  312. predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
  313. }
  314. return predicted_size;
  315. }
  316. int ftdi2232_predict_scan_in(int scan_size, enum scan_type type)
  317. {
  318. int predicted_size = 0;
  319. if (type != SCAN_OUT)
  320. {
  321. /* complete bytes */
  322. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
  323. /* remaining bits - 1 */
  324. predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
  325. /* last bit (from TMS scan) */
  326. predicted_size += 1;
  327. }
  328. //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
  329. return predicted_size;
  330. }
  331. int ftdi2232_execute_queue()
  332. {
  333. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  334. jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
  335. u8 *buffer;
  336. int scan_size; /* size of IR or DR scan */
  337. enum scan_type type;
  338. int i;
  339. int predicted_size = 0;
  340. int require_send = 0;
  341. ftdi2232_buffer_size = 0;
  342. ftdi2232_expect_read = 0;
  343. while (cmd)
  344. {
  345. switch(cmd->type)
  346. {
  347. case JTAG_END_STATE:
  348. if (cmd->cmd.end_state->end_state != -1)
  349. ftdi2232_end_state(cmd->cmd.end_state->end_state);
  350. break;
  351. case JTAG_RESET:
  352. /* only send the maximum buffer size that FT2232C can handle */
  353. predicted_size = 3;
  354. if (ftdi2232_buffer_size + predicted_size + 1 > FTDI2232_SAVE_SIZE)
  355. {
  356. ftdi2232_send_and_recv(first_unsent, cmd);
  357. require_send = 0;
  358. first_unsent = cmd;
  359. }
  360. if (cmd->cmd.reset->trst == 1)
  361. {
  362. cur_state = TAP_TLR;
  363. discrete_output &= ~FTDI2232_TRST;
  364. }
  365. else if (cmd->cmd.reset->trst == 0)
  366. {
  367. discrete_output |= FTDI2232_TRST;
  368. }
  369. if (cmd->cmd.reset->srst == 1)
  370. discrete_output &= ~FTDI2232_SRST;
  371. else if (cmd->cmd.reset->srst == 0)
  372. discrete_output |= FTDI2232_SRST;
  373. /* command "set data bits low byte" */
  374. BUFFER_ADD = 0x80;
  375. /* value (TMS=1,TCK=0, TDI=0, TRST/SRST */
  376. BUFFER_ADD = 0x08 | discrete_output;
  377. /* dir (output=1), TCK/TDI/TMS=out, TDO=in, TRST/SRST=out */
  378. BUFFER_ADD = 0x0b | FTDI2232_SRST | FTDI2232_TRST;
  379. require_send = 1;
  380. break;
  381. case JTAG_RUNTEST:
  382. /* only send the maximum buffer size that FT2232C can handle */
  383. predicted_size = 0;
  384. if (cur_state != TAP_RTI)
  385. predicted_size += 3;
  386. predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
  387. if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
  388. predicted_size += 3;
  389. if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
  390. predicted_size += 3;
  391. if (ftdi2232_buffer_size + predicted_size + 1 > FTDI2232_SAVE_SIZE)
  392. {
  393. ftdi2232_send_and_recv(first_unsent, cmd);
  394. require_send = 0;
  395. first_unsent = cmd;
  396. }
  397. if (cur_state != TAP_RTI)
  398. {
  399. /* command "Clock Data to TMS/CS Pin (no Read)" */
  400. BUFFER_ADD = 0x4b;
  401. /* scan 7 bit */
  402. BUFFER_ADD = 0x6;
  403. /* TMS data bits */
  404. BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
  405. cur_state = TAP_RTI;
  406. require_send = 1;
  407. }
  408. i = cmd->cmd.runtest->num_cycles;
  409. while (i > 0)
  410. {
  411. /* command "Clock Data to TMS/CS Pin (no Read)" */
  412. BUFFER_ADD = 0x4b;
  413. /* scan 7 bit */
  414. BUFFER_ADD = (i > 7) ? 6 : (i - 1);
  415. /* TMS data bits */
  416. BUFFER_ADD = 0x0;
  417. cur_state = TAP_RTI;
  418. i -= (i > 7) ? 7 : i;
  419. //DEBUG("added TMS scan (no read)");
  420. }
  421. if (cmd->cmd.runtest->end_state != -1)
  422. ftdi2232_end_state(cmd->cmd.runtest->end_state);
  423. if (cur_state != end_state)
  424. {
  425. /* command "Clock Data to TMS/CS Pin (no Read)" */
  426. BUFFER_ADD = 0x4b;
  427. /* scan 7 bit */
  428. BUFFER_ADD = 0x6;
  429. /* TMS data bits */
  430. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  431. cur_state = end_state;
  432. //DEBUG("added TMS scan (no read)");
  433. }
  434. require_send = 1;
  435. break;
  436. case JTAG_STATEMOVE:
  437. /* only send the maximum buffer size that FT2232C can handle */
  438. predicted_size = 3;
  439. if (ftdi2232_buffer_size + predicted_size + 1 > FTDI2232_SAVE_SIZE)
  440. {
  441. ftdi2232_send_and_recv(first_unsent, cmd);
  442. require_send = 0;
  443. first_unsent = cmd;
  444. }
  445. if (cmd->cmd.statemove->end_state != -1)
  446. ftdi2232_end_state(cmd->cmd.statemove->end_state);
  447. /* command "Clock Data to TMS/CS Pin (no Read)" */
  448. BUFFER_ADD = 0x4b;
  449. /* scan 7 bit */
  450. BUFFER_ADD = 0x6;
  451. /* TMS data bits */
  452. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  453. //DEBUG("added TMS scan (no read)");
  454. cur_state = end_state;
  455. require_send = 1;
  456. break;
  457. case JTAG_SCAN:
  458. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  459. type = jtag_scan_type(cmd->cmd.scan);
  460. predicted_size = ftdi2232_predict_scan_out(scan_size, type);
  461. if (ftdi2232_buffer_size + predicted_size + 1 > FTDI2232_SAVE_SIZE)
  462. {
  463. ftdi2232_send_and_recv(first_unsent, cmd);
  464. require_send = 0;
  465. first_unsent = cmd;
  466. }
  467. ftdi2232_expect_read += ftdi2232_predict_scan_in(scan_size, type);
  468. //DEBUG("new read size: %i", ftdi2232_expect_read);
  469. if (cmd->cmd.scan->end_state != -1)
  470. ftdi2232_end_state(cmd->cmd.scan->end_state);
  471. ftdi2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  472. require_send = 1;
  473. if (buffer)
  474. free(buffer);
  475. break;
  476. case JTAG_SLEEP:
  477. jtag_sleep(cmd->cmd.sleep->us);
  478. break;
  479. default:
  480. ERROR("BUG: unknown JTAG command type encountered");
  481. exit(-1);
  482. }
  483. cmd = cmd->next;
  484. }
  485. if (require_send > 0)
  486. ftdi2232_send_and_recv(first_unsent, cmd);
  487. return ERROR_OK;
  488. }
  489. int ftdi2232_init(void)
  490. {
  491. if (ftdi_init(&ftdic) < 0)
  492. return ERROR_JTAG_INIT_FAILED;
  493. /* context, vendor id, product id */
  494. if (ftdi_usb_open(&ftdic, ftdi2232_vid, ftdi2232_pid) < 0)
  495. {
  496. ERROR("unable to open ftdi device: %s", ftdic.error_str);
  497. return ERROR_JTAG_INIT_FAILED;
  498. }
  499. if (ftdi_usb_reset(&ftdic) < 0)
  500. {
  501. ERROR("unable to reset ftdi device");
  502. return ERROR_JTAG_INIT_FAILED;
  503. }
  504. if (ftdi_set_latency_timer(&ftdic, 1) < 0)
  505. {
  506. ERROR("unable to set latency timer");
  507. return ERROR_JTAG_INIT_FAILED;
  508. }
  509. ftdi2232_buffer_size = 0;
  510. ftdi2232_buffer = malloc(FTDI2232_BUFFER_SIZE);
  511. ftdic.bitbang_mode = 0; /* Reset controller */
  512. ftdi_enable_bitbang(&ftdic, 0x0b | FTDI2232_SRST | FTDI2232_TRST); /* ctx, i/o mask (out=1, in=0) */
  513. ftdic.bitbang_mode = 2; /* MPSSE mode */
  514. ftdi_enable_bitbang(&ftdic, 0x0b | FTDI2232_SRST | FTDI2232_TRST); /* ctx, i/o mask (out=1, in=0) */
  515. if (ftdi_usb_purge_buffers(&ftdic) < 0)
  516. {
  517. ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  518. return ERROR_JTAG_INIT_FAILED;
  519. }
  520. /* initialize low byte for jtag */
  521. BUFFER_ADD = 0x80; /* command "set data bits low byte" */
  522. BUFFER_ADD = 0x08 | FTDI2232_SRST | FTDI2232_TRST; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
  523. BUFFER_ADD = 0x0b | FTDI2232_SRST | FTDI2232_TRST; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
  524. BUFFER_ADD = 0x85; /* command "Disconnect TDI/DO to TDO/DI for Loopback" */
  525. ftdi2232_debug_dump_buffer();
  526. if (ftdi_write_data(&ftdic, ftdi2232_buffer, ftdi2232_buffer_size) != 4)
  527. return ERROR_JTAG_INIT_FAILED;
  528. ftdi2232_speed(jtag_speed);
  529. return ERROR_OK;
  530. }
  531. int ftdi2232_quit(void)
  532. {
  533. ftdi_disable_bitbang(&ftdic);
  534. ftdi_usb_close(&ftdic);
  535. ftdi_deinit(&ftdic);
  536. free(ftdi2232_buffer);
  537. return ERROR_OK;
  538. }
  539. int ftdi2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  540. {
  541. if (argc >= 2)
  542. {
  543. ftdi2232_vid = strtol(args[0], NULL, 0);
  544. ftdi2232_pid = strtol(args[1], NULL, 0);
  545. }
  546. else
  547. {
  548. WARNING("incomplete ftdi2232_vid_pid configuration directive");
  549. }
  550. return ERROR_OK;
  551. }