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.
 
 
 
 
 
 

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