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.
 
 
 
 
 
 

2297 lines
56 KiB

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