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.
 
 
 
 
 
 

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