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.
 
 
 
 
 
 

3050 lines
77 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. * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
  9. * Dick Hollenbeck <dick@softplc.com> *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. /* This code uses information contained in the MPSSE specification which was
  27. * found here:
  28. * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
  29. * Hereafter this is called the "MPSSE Spec".
  30. *
  31. * The datasheet for the ftdichip.com's FT2232D part is here:
  32. * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
  33. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. /* project specific includes */
  38. #include "interface.h"
  39. #include "commands.h"
  40. #include "time_support.h"
  41. #if IS_CYGWIN == 1
  42. #include <windows.h>
  43. #endif
  44. #include <assert.h>
  45. #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
  46. #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
  47. #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
  48. #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
  49. #endif
  50. /* FT2232 access library includes */
  51. #if BUILD_FT2232_FTD2XX == 1
  52. #include <ftd2xx.h>
  53. #elif BUILD_FT2232_LIBFTDI == 1
  54. #include <ftdi.h>
  55. #endif
  56. /* max TCK for the high speed devices 30000 kHz */
  57. #define FTDI_2232H_4232H_MAX_TCK 30000
  58. static int ft2232_execute_queue(void);
  59. static int ft2232_speed(int speed);
  60. static int ft2232_speed_div(int speed, int* khz);
  61. static int ft2232_khz(int khz, int* jtag_speed);
  62. static int ft2232_register_commands(struct command_context_s* cmd_ctx);
  63. static int ft2232_init(void);
  64. static int ft2232_quit(void);
  65. static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
  66. static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
  67. static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
  68. static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
  69. static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc);
  70. /**
  71. * Send out \a num_cycles on the TCK line while the TAP(s) are in a
  72. * stable state. Calling code must ensure that current state is stable,
  73. * that verification is not done in here.
  74. *
  75. * @param num_cycles The number of clocks cycles to send.
  76. * @param cmd The command to send.
  77. *
  78. * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
  79. */
  80. static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd);
  81. /* max TCK for the high speed devices 30000 kHz */
  82. #define FTDI_2232H_4232H_MAX_TCK 30000
  83. static char * ft2232_device_desc_A = NULL;
  84. static char* ft2232_device_desc = NULL;
  85. static char* ft2232_serial = NULL;
  86. static char* ft2232_layout = NULL;
  87. static uint8_t ft2232_latency = 2;
  88. static unsigned ft2232_max_tck = 6000;
  89. #define MAX_USB_IDS 8
  90. /* vid = pid = 0 marks the end of the list */
  91. static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
  92. static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
  93. typedef struct ft2232_layout_s
  94. {
  95. char* name;
  96. int (*init)(void);
  97. void (*reset)(int trst, int srst);
  98. void (*blink)(void);
  99. } ft2232_layout_t;
  100. /* init procedures for supported layouts */
  101. static int usbjtag_init(void);
  102. static int jtagkey_init(void);
  103. static int olimex_jtag_init(void);
  104. static int flyswatter_init(void);
  105. static int turtle_init(void);
  106. static int comstick_init(void);
  107. static int stm32stick_init(void);
  108. static int axm0432_jtag_init(void);
  109. static int sheevaplug_init(void);
  110. static int icebear_jtag_init(void);
  111. static int cortino_jtag_init(void);
  112. /* reset procedures for supported layouts */
  113. static void usbjtag_reset(int trst, int srst);
  114. static void jtagkey_reset(int trst, int srst);
  115. static void olimex_jtag_reset(int trst, int srst);
  116. static void flyswatter_reset(int trst, int srst);
  117. static void turtle_reset(int trst, int srst);
  118. static void comstick_reset(int trst, int srst);
  119. static void stm32stick_reset(int trst, int srst);
  120. static void axm0432_jtag_reset(int trst, int srst);
  121. static void sheevaplug_reset(int trst, int srst);
  122. static void icebear_jtag_reset(int trst, int srst);
  123. /* blink procedures for layouts that support a blinking led */
  124. static void olimex_jtag_blink(void);
  125. static void flyswatter_jtag_blink(void);
  126. static void turtle_jtag_blink(void);
  127. static const ft2232_layout_t ft2232_layouts[] =
  128. {
  129. { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
  130. { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
  131. { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
  132. { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
  133. { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
  134. { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
  135. { "luminary_icdi", usbjtag_init, usbjtag_reset, NULL },
  136. { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
  137. { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
  138. { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
  139. { "comstick", comstick_init, comstick_reset, NULL },
  140. { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
  141. { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
  142. { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
  143. { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
  144. { "cortino", cortino_jtag_init, comstick_reset, NULL },
  145. { NULL, NULL, NULL, NULL },
  146. };
  147. static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
  148. static const ft2232_layout_t *layout;
  149. static uint8_t low_output = 0x0;
  150. static uint8_t low_direction = 0x0;
  151. static uint8_t high_output = 0x0;
  152. static uint8_t high_direction = 0x0;
  153. #if BUILD_FT2232_FTD2XX == 1
  154. static FT_HANDLE ftdih = NULL;
  155. static FT_DEVICE ftdi_device = 0;
  156. #elif BUILD_FT2232_LIBFTDI == 1
  157. static struct ftdi_context ftdic;
  158. #endif
  159. static jtag_command_t* first_unsent; /* next command that has to be sent */
  160. static int require_send;
  161. /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
  162. "There is a significant difference between libftdi and libftd2xx. The latter
  163. one allows to schedule up to 64*64 bytes of result data while libftdi fails
  164. with more than 4*64. As a consequence, the FT2232 driver is forced to
  165. perform around 16x more USB transactions for long command streams with TDO
  166. capture when running with libftdi."
  167. No idea how we get
  168. #define FT2232_BUFFER_SIZE 131072
  169. a comment would have been nice.
  170. */
  171. #define FT2232_BUFFER_SIZE 131072
  172. static uint8_t* ft2232_buffer = NULL;
  173. static int ft2232_buffer_size = 0;
  174. static int ft2232_read_pointer = 0;
  175. static int ft2232_expect_read = 0;
  176. /**
  177. * Function buffer_write
  178. * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
  179. * @param val is the byte to send.
  180. */
  181. static inline void buffer_write(uint8_t val)
  182. {
  183. assert(ft2232_buffer);
  184. assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
  185. ft2232_buffer[ft2232_buffer_size++] = val;
  186. }
  187. /**
  188. * Function buffer_read
  189. * returns a byte from the byte buffer.
  190. */
  191. static inline uint8_t buffer_read(void)
  192. {
  193. assert(ft2232_buffer);
  194. assert(ft2232_read_pointer < ft2232_buffer_size);
  195. return ft2232_buffer[ft2232_read_pointer++];
  196. }
  197. /**
  198. * Clocks out \a bit_count bits on the TMS line, starting with the least
  199. * significant bit of tms_bits and progressing to more significant bits.
  200. * Rigorous state transition logging is done here via tap_set_state().
  201. *
  202. * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
  203. * 0x4b or 0x6b. See the MPSSE spec referenced above for their
  204. * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
  205. * is often used for this, 0x4b.
  206. *
  207. * @param tms_bits Holds the sequence of bits to send.
  208. * @param tms_count Tells how many bits in the sequence.
  209. * @param tdi_bit A single bit to pass on to TDI before the first TCK
  210. * cycle and held static for the duration of TMS clocking.
  211. *
  212. * See the MPSSE spec referenced above.
  213. */
  214. static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
  215. {
  216. uint8_t tms_byte;
  217. int i;
  218. int tms_ndx; /* bit index into tms_byte */
  219. assert(tms_count > 0);
  220. // LOG_DEBUG("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d", mpsse_cmd, tms_bits, tms_count);
  221. for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
  222. {
  223. bool bit = tms_bits & 1;
  224. if (bit)
  225. tms_byte |= (1 << tms_ndx);
  226. /* always do state transitions in public view */
  227. tap_set_state(tap_state_transition(tap_get_state(), bit));
  228. /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
  229. also increment.
  230. */
  231. ++tms_ndx;
  232. if (tms_ndx == 7 || i == tms_count-1)
  233. {
  234. buffer_write(mpsse_cmd);
  235. buffer_write(tms_ndx - 1);
  236. /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
  237. TMS/CS and is held static for the duration of TMS/CS clocking.
  238. */
  239. buffer_write(tms_byte | (tdi_bit << 7));
  240. }
  241. }
  242. }
  243. /**
  244. * Function get_tms_buffer_requirements
  245. * returns what clock_tms() will consume if called with
  246. * same \a bit_count.
  247. */
  248. static inline int get_tms_buffer_requirements(int bit_count)
  249. {
  250. return ((bit_count + 6)/7) * 3;
  251. }
  252. /**
  253. * Function move_to_state
  254. * moves the TAP controller from the current state to a
  255. * \a goal_state through a path given by tap_get_tms_path(). State transition
  256. * logging is performed by delegation to clock_tms().
  257. *
  258. * @param goal_state is the destination state for the move.
  259. */
  260. static void move_to_state(tap_state_t goal_state)
  261. {
  262. tap_state_t start_state = tap_get_state();
  263. /* goal_state is 1/2 of a tuple/pair of states which allow convenient
  264. lookup of the required TMS pattern to move to this state from the
  265. start state.
  266. */
  267. /* do the 2 lookups */
  268. int tms_bits = tap_get_tms_path(start_state, goal_state);
  269. int tms_count = tap_get_tms_path_len(start_state, goal_state);
  270. DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
  271. clock_tms(0x4b, tms_bits, tms_count, 0);
  272. }
  273. jtag_interface_t ft2232_interface =
  274. {
  275. .name = "ft2232",
  276. .execute_queue = ft2232_execute_queue,
  277. .speed = ft2232_speed,
  278. .speed_div = ft2232_speed_div,
  279. .khz = ft2232_khz,
  280. .register_commands = ft2232_register_commands,
  281. .init = ft2232_init,
  282. .quit = ft2232_quit,
  283. };
  284. static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
  285. {
  286. #if BUILD_FT2232_FTD2XX == 1
  287. FT_STATUS status;
  288. DWORD dw_bytes_written;
  289. if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
  290. {
  291. *bytes_written = dw_bytes_written;
  292. LOG_ERROR("FT_Write returned: %lu", status);
  293. return ERROR_JTAG_DEVICE_ERROR;
  294. }
  295. else
  296. {
  297. *bytes_written = dw_bytes_written;
  298. return ERROR_OK;
  299. }
  300. #elif BUILD_FT2232_LIBFTDI == 1
  301. int retval;
  302. if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
  303. {
  304. *bytes_written = 0;
  305. LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
  306. return ERROR_JTAG_DEVICE_ERROR;
  307. }
  308. else
  309. {
  310. *bytes_written = retval;
  311. return ERROR_OK;
  312. }
  313. #endif
  314. }
  315. static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
  316. {
  317. #if BUILD_FT2232_FTD2XX == 1
  318. DWORD dw_bytes_read;
  319. FT_STATUS status;
  320. int timeout = 5;
  321. *bytes_read = 0;
  322. while ((*bytes_read < size) && timeout--)
  323. {
  324. if ((status = FT_Read(ftdih, buf + *bytes_read, size -
  325. *bytes_read, &dw_bytes_read)) != FT_OK)
  326. {
  327. *bytes_read = 0;
  328. LOG_ERROR("FT_Read returned: %lu", status);
  329. return ERROR_JTAG_DEVICE_ERROR;
  330. }
  331. *bytes_read += dw_bytes_read;
  332. }
  333. #elif BUILD_FT2232_LIBFTDI == 1
  334. int retval;
  335. int timeout = 100;
  336. *bytes_read = 0;
  337. while ((*bytes_read < size) && timeout--)
  338. {
  339. if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
  340. {
  341. *bytes_read = 0;
  342. LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
  343. return ERROR_JTAG_DEVICE_ERROR;
  344. }
  345. *bytes_read += retval;
  346. }
  347. #endif
  348. if (*bytes_read < size)
  349. {
  350. LOG_ERROR("couldn't read the requested number of bytes from FT2232 device (%i < %i)",
  351. (unsigned int)(*bytes_read),
  352. (unsigned int)size);
  353. return ERROR_JTAG_DEVICE_ERROR;
  354. }
  355. return ERROR_OK;
  356. }
  357. #ifdef BUILD_FTD2XX_HIGHSPEED
  358. static bool ft2232_device_is_highspeed(void)
  359. {
  360. return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
  361. }
  362. static int ft2232_adaptive_clocking(int speed)
  363. {
  364. bool use_adaptive_clocking = FALSE;
  365. if (0 == speed)
  366. {
  367. if (ft2232_device_is_highspeed())
  368. use_adaptive_clocking = TRUE;
  369. else
  370. {
  371. LOG_ERROR("ft2232 device %lu does not support RTCK", ftdi_device);
  372. return ERROR_OK;
  373. }
  374. }
  375. uint8_t buf = use_adaptive_clocking ? 0x96 : 0x97;
  376. LOG_DEBUG("%2.2x", buf);
  377. uint32_t bytes_written;
  378. int retval = ft2232_write(&buf, 1, &bytes_written);
  379. if (ERROR_OK != retval || bytes_written != 1)
  380. {
  381. LOG_ERROR("unable to set adative clocking: %d", retval);
  382. return retval;
  383. }
  384. return ERROR_OK;
  385. }
  386. #else
  387. static int ft2232_adaptive_clocking(int speed)
  388. {
  389. // not implemented on low-speed devices
  390. return speed ? ERROR_OK : -1234;
  391. }
  392. #endif
  393. static int ft2232_speed(int speed)
  394. {
  395. uint8_t buf[3];
  396. int retval;
  397. uint32_t bytes_written;
  398. ft2232_adaptive_clocking(speed);
  399. buf[0] = 0x86; /* command "set divisor" */
  400. buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
  401. buf[2] = (speed >> 8) & 0xff; /* valueH */
  402. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  403. if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  404. {
  405. LOG_ERROR("couldn't set FT2232 TCK speed");
  406. return retval;
  407. }
  408. return ERROR_OK;
  409. }
  410. static int ft2232_speed_div(int speed, int* khz)
  411. {
  412. /* Take a look in the FT2232 manual,
  413. * AN2232C-01 Command Processor for
  414. * MPSSE and MCU Host Bus. Chapter 3.8 */
  415. *khz = ft2232_max_tck / (1 + speed);
  416. return ERROR_OK;
  417. }
  418. static int ft2232_khz(int khz, int* jtag_speed)
  419. {
  420. if (khz == 0)
  421. {
  422. #ifdef BUILD_FTD2XX_HIGHSPEED
  423. *jtag_speed = 0;
  424. return ERROR_OK;
  425. #else
  426. LOG_DEBUG("RCLK not supported");
  427. LOG_DEBUG("If you have a high-speed FTDI device, then "
  428. "OpenOCD may be built with --enable-ftd2xx-highspeed.");
  429. return ERROR_FAIL;
  430. #endif
  431. }
  432. /* Take a look in the FT2232 manual,
  433. * AN2232C-01 Command Processor for
  434. * MPSSE and MCU Host Bus. Chapter 3.8
  435. *
  436. * We will calc here with a multiplier
  437. * of 10 for better rounding later. */
  438. /* Calc speed, (ft2232_max_tck / khz) - 1 */
  439. /* Use 65000 for better rounding */
  440. *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
  441. /* Add 0.9 for rounding */
  442. *jtag_speed += 9;
  443. /* Calc real speed */
  444. *jtag_speed = *jtag_speed / 10;
  445. /* Check if speed is greater than 0 */
  446. if (*jtag_speed < 0)
  447. {
  448. *jtag_speed = 0;
  449. }
  450. /* Check max value */
  451. if (*jtag_speed > 0xFFFF)
  452. {
  453. *jtag_speed = 0xFFFF;
  454. }
  455. return ERROR_OK;
  456. }
  457. static int ft2232_register_commands(struct command_context_s* cmd_ctx)
  458. {
  459. register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
  460. COMMAND_CONFIG, "the USB device description of the FTDI FT2232 device");
  461. register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
  462. COMMAND_CONFIG, "the serial number of the FTDI FT2232 device");
  463. register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
  464. COMMAND_CONFIG, "the layout of the FT2232 GPIO signals used to control output-enables and reset signals");
  465. register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
  466. COMMAND_CONFIG, "the vendor ID and product ID of the FTDI FT2232 device");
  467. register_command(cmd_ctx, NULL, "ft2232_latency", ft2232_handle_latency_command,
  468. COMMAND_CONFIG, "set the FT2232 latency timer to a new value");
  469. return ERROR_OK;
  470. }
  471. static void ft2232_end_state(tap_state_t state)
  472. {
  473. if (tap_is_state_stable(state))
  474. tap_set_end_state(state);
  475. else
  476. {
  477. LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
  478. exit(-1);
  479. }
  480. }
  481. static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
  482. {
  483. int num_bytes = (scan_size + 7) / 8;
  484. int bits_left = scan_size;
  485. int cur_byte = 0;
  486. while (num_bytes-- > 1)
  487. {
  488. buffer[cur_byte++] = buffer_read();
  489. bits_left -= 8;
  490. }
  491. buffer[cur_byte] = 0x0;
  492. /* There is one more partial byte left from the clock data in/out instructions */
  493. if (bits_left > 1)
  494. {
  495. buffer[cur_byte] = buffer_read() >> 1;
  496. }
  497. /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
  498. buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
  499. }
  500. static void ft2232_debug_dump_buffer(void)
  501. {
  502. int i;
  503. char line[256];
  504. char* line_p = line;
  505. for (i = 0; i < ft2232_buffer_size; i++)
  506. {
  507. line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
  508. if (i % 16 == 15)
  509. {
  510. LOG_DEBUG("%s", line);
  511. line_p = line;
  512. }
  513. }
  514. if (line_p != line)
  515. LOG_DEBUG("%s", line);
  516. }
  517. static int ft2232_send_and_recv(jtag_command_t* first, jtag_command_t* last)
  518. {
  519. jtag_command_t* cmd;
  520. uint8_t* buffer;
  521. int scan_size;
  522. enum scan_type type;
  523. int retval;
  524. uint32_t bytes_written = 0;
  525. uint32_t bytes_read = 0;
  526. #ifdef _DEBUG_USB_IO_
  527. struct timeval start, inter, inter2, end;
  528. struct timeval d_inter, d_inter2, d_end;
  529. #endif
  530. #ifdef _DEBUG_USB_COMMS_
  531. LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
  532. ft2232_debug_dump_buffer();
  533. #endif
  534. #ifdef _DEBUG_USB_IO_
  535. gettimeofday(&start, NULL);
  536. #endif
  537. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  538. {
  539. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  540. return retval;
  541. }
  542. #ifdef _DEBUG_USB_IO_
  543. gettimeofday(&inter, NULL);
  544. #endif
  545. if (ft2232_expect_read)
  546. {
  547. int timeout = 100;
  548. ft2232_buffer_size = 0;
  549. #ifdef _DEBUG_USB_IO_
  550. gettimeofday(&inter2, NULL);
  551. #endif
  552. if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
  553. {
  554. LOG_ERROR("couldn't read from FT2232");
  555. return retval;
  556. }
  557. #ifdef _DEBUG_USB_IO_
  558. gettimeofday(&end, NULL);
  559. timeval_subtract(&d_inter, &inter, &start);
  560. timeval_subtract(&d_inter2, &inter2, &start);
  561. timeval_subtract(&d_end, &end, &start);
  562. LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
  563. (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
  564. (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
  565. (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
  566. #endif
  567. ft2232_buffer_size = bytes_read;
  568. if (ft2232_expect_read != ft2232_buffer_size)
  569. {
  570. LOG_ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read,
  571. ft2232_buffer_size,
  572. 100 - timeout);
  573. ft2232_debug_dump_buffer();
  574. exit(-1);
  575. }
  576. #ifdef _DEBUG_USB_COMMS_
  577. LOG_DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
  578. ft2232_debug_dump_buffer();
  579. #endif
  580. }
  581. ft2232_expect_read = 0;
  582. ft2232_read_pointer = 0;
  583. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  584. * that wasn't handled by a caller-provided error handler
  585. */
  586. retval = ERROR_OK;
  587. cmd = first;
  588. while (cmd != last)
  589. {
  590. switch (cmd->type)
  591. {
  592. case JTAG_SCAN:
  593. type = jtag_scan_type(cmd->cmd.scan);
  594. if (type != SCAN_OUT)
  595. {
  596. scan_size = jtag_scan_size(cmd->cmd.scan);
  597. buffer = calloc(CEIL(scan_size, 8), 1);
  598. ft2232_read_scan(type, buffer, scan_size);
  599. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  600. retval = ERROR_JTAG_QUEUE_FAILED;
  601. free(buffer);
  602. }
  603. break;
  604. default:
  605. break;
  606. }
  607. cmd = cmd->next;
  608. }
  609. ft2232_buffer_size = 0;
  610. return retval;
  611. }
  612. /**
  613. * Function ft2232_add_pathmove
  614. * moves the TAP controller from the current state to a new state through the
  615. * given path, where path is an array of tap_state_t's.
  616. *
  617. * @param path is an array of tap_stat_t which gives the states to traverse through
  618. * ending with the last state at path[num_states-1]
  619. * @param num_states is the count of state steps to move through
  620. */
  621. static void ft2232_add_pathmove(tap_state_t* path, int num_states)
  622. {
  623. int tms_bits = 0;
  624. int state_ndx;
  625. tap_state_t walker = tap_get_state();
  626. assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
  627. /* this loop verifies that the path is legal and logs each state in the path */
  628. for (state_ndx = 0; state_ndx < num_states; ++state_ndx)
  629. {
  630. tap_state_t desired_next_state = path[state_ndx];
  631. if (tap_state_transition(walker, false) == desired_next_state)
  632. ; /* bit within tms_bits at index state_ndx is already zero */
  633. else if (tap_state_transition(walker, true) == desired_next_state)
  634. tms_bits |= (1 << state_ndx);
  635. else
  636. {
  637. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  638. tap_state_name(walker), tap_state_name(desired_next_state));
  639. exit(-1);
  640. }
  641. walker = desired_next_state;
  642. }
  643. clock_tms(0x4b, tms_bits, num_states, 0);
  644. tap_set_end_state(tap_get_state());
  645. }
  646. static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
  647. {
  648. int num_bytes = (scan_size + 7) / 8;
  649. int bits_left = scan_size;
  650. int cur_byte = 0;
  651. int last_bit;
  652. if (!ir_scan)
  653. {
  654. if (tap_get_state() != TAP_DRSHIFT)
  655. {
  656. move_to_state(TAP_DRSHIFT);
  657. }
  658. }
  659. else
  660. {
  661. if (tap_get_state() != TAP_IRSHIFT)
  662. {
  663. move_to_state(TAP_IRSHIFT);
  664. }
  665. }
  666. /* add command for complete bytes */
  667. while (num_bytes > 1)
  668. {
  669. int thisrun_bytes;
  670. if (type == SCAN_IO)
  671. {
  672. /* Clock Data Bytes In and Out LSB First */
  673. buffer_write(0x39);
  674. /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
  675. }
  676. else if (type == SCAN_OUT)
  677. {
  678. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  679. buffer_write(0x19);
  680. /* LOG_DEBUG("added TDI bytes (o)"); */
  681. }
  682. else if (type == SCAN_IN)
  683. {
  684. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  685. buffer_write(0x28);
  686. /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
  687. }
  688. thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
  689. num_bytes -= thisrun_bytes;
  690. buffer_write((uint8_t) (thisrun_bytes - 1));
  691. buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
  692. if (type != SCAN_IN)
  693. {
  694. /* add complete bytes */
  695. while (thisrun_bytes-- > 0)
  696. {
  697. buffer_write(buffer[cur_byte++]);
  698. bits_left -= 8;
  699. }
  700. }
  701. else /* (type == SCAN_IN) */
  702. {
  703. bits_left -= 8 * (thisrun_bytes);
  704. }
  705. }
  706. /* the most signifcant bit is scanned during TAP movement */
  707. if (type != SCAN_IN)
  708. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  709. else
  710. last_bit = 0;
  711. /* process remaining bits but the last one */
  712. if (bits_left > 1)
  713. {
  714. if (type == SCAN_IO)
  715. {
  716. /* Clock Data Bits In and Out LSB First */
  717. buffer_write(0x3b);
  718. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  719. }
  720. else if (type == SCAN_OUT)
  721. {
  722. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  723. buffer_write(0x1b);
  724. /* LOG_DEBUG("added TDI bits (o)"); */
  725. }
  726. else if (type == SCAN_IN)
  727. {
  728. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  729. buffer_write(0x2a);
  730. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  731. }
  732. buffer_write(bits_left - 2);
  733. if (type != SCAN_IN)
  734. buffer_write(buffer[cur_byte]);
  735. }
  736. if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
  737. || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
  738. {
  739. if (type == SCAN_IO)
  740. {
  741. /* Clock Data Bits In and Out LSB First */
  742. buffer_write(0x3b);
  743. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  744. }
  745. else if (type == SCAN_OUT)
  746. {
  747. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  748. buffer_write(0x1b);
  749. /* LOG_DEBUG("added TDI bits (o)"); */
  750. }
  751. else if (type == SCAN_IN)
  752. {
  753. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  754. buffer_write(0x2a);
  755. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  756. }
  757. buffer_write(0x0);
  758. buffer_write(last_bit);
  759. }
  760. else
  761. {
  762. int tms_bits;
  763. int tms_count;
  764. uint8_t mpsse_cmd;
  765. /* move from Shift-IR/DR to end state */
  766. if (type != SCAN_OUT)
  767. {
  768. /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
  769. /* This must be coordinated with the bit shifts in ft2232_read_scan */
  770. tms_bits = 0x01;
  771. tms_count = 2;
  772. /* Clock Data to TMS/CS Pin with Read */
  773. mpsse_cmd = 0x6b;
  774. /* LOG_DEBUG("added TMS scan (read)"); */
  775. }
  776. else
  777. {
  778. tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  779. tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  780. /* Clock Data to TMS/CS Pin (no Read) */
  781. mpsse_cmd = 0x4b;
  782. /* LOG_DEBUG("added TMS scan (no read)"); */
  783. }
  784. clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
  785. }
  786. if (tap_get_state() != tap_get_end_state())
  787. {
  788. move_to_state(tap_get_end_state());
  789. }
  790. }
  791. static int ft2232_large_scan(scan_command_t* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
  792. {
  793. int num_bytes = (scan_size + 7) / 8;
  794. int bits_left = scan_size;
  795. int cur_byte = 0;
  796. int last_bit;
  797. uint8_t* receive_buffer = malloc(CEIL(scan_size, 8));
  798. uint8_t* receive_pointer = receive_buffer;
  799. uint32_t bytes_written;
  800. uint32_t bytes_read;
  801. int retval;
  802. int thisrun_read = 0;
  803. if (cmd->ir_scan)
  804. {
  805. LOG_ERROR("BUG: large IR scans are not supported");
  806. exit(-1);
  807. }
  808. if (tap_get_state() != TAP_DRSHIFT)
  809. {
  810. move_to_state(TAP_DRSHIFT);
  811. }
  812. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  813. {
  814. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  815. exit(-1);
  816. }
  817. LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
  818. ft2232_buffer_size, (int)bytes_written);
  819. ft2232_buffer_size = 0;
  820. /* add command for complete bytes */
  821. while (num_bytes > 1)
  822. {
  823. int thisrun_bytes;
  824. if (type == SCAN_IO)
  825. {
  826. /* Clock Data Bytes In and Out LSB First */
  827. buffer_write(0x39);
  828. /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
  829. }
  830. else if (type == SCAN_OUT)
  831. {
  832. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  833. buffer_write(0x19);
  834. /* LOG_DEBUG("added TDI bytes (o)"); */
  835. }
  836. else if (type == SCAN_IN)
  837. {
  838. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  839. buffer_write(0x28);
  840. /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
  841. }
  842. thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
  843. thisrun_read = thisrun_bytes;
  844. num_bytes -= thisrun_bytes;
  845. buffer_write((uint8_t) (thisrun_bytes - 1));
  846. buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
  847. if (type != SCAN_IN)
  848. {
  849. /* add complete bytes */
  850. while (thisrun_bytes-- > 0)
  851. {
  852. buffer_write(buffer[cur_byte]);
  853. cur_byte++;
  854. bits_left -= 8;
  855. }
  856. }
  857. else /* (type == SCAN_IN) */
  858. {
  859. bits_left -= 8 * (thisrun_bytes);
  860. }
  861. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  862. {
  863. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  864. exit(-1);
  865. }
  866. LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
  867. ft2232_buffer_size,
  868. (int)bytes_written);
  869. ft2232_buffer_size = 0;
  870. if (type != SCAN_OUT)
  871. {
  872. if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
  873. {
  874. LOG_ERROR("couldn't read from FT2232");
  875. exit(-1);
  876. }
  877. LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
  878. thisrun_read,
  879. (int)bytes_read);
  880. receive_pointer += bytes_read;
  881. }
  882. }
  883. thisrun_read = 0;
  884. /* the most signifcant bit is scanned during TAP movement */
  885. if (type != SCAN_IN)
  886. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  887. else
  888. last_bit = 0;
  889. /* process remaining bits but the last one */
  890. if (bits_left > 1)
  891. {
  892. if (type == SCAN_IO)
  893. {
  894. /* Clock Data Bits In and Out LSB First */
  895. buffer_write(0x3b);
  896. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  897. }
  898. else if (type == SCAN_OUT)
  899. {
  900. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  901. buffer_write(0x1b);
  902. /* LOG_DEBUG("added TDI bits (o)"); */
  903. }
  904. else if (type == SCAN_IN)
  905. {
  906. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  907. buffer_write(0x2a);
  908. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  909. }
  910. buffer_write(bits_left - 2);
  911. if (type != SCAN_IN)
  912. buffer_write(buffer[cur_byte]);
  913. if (type != SCAN_OUT)
  914. thisrun_read += 2;
  915. }
  916. if (tap_get_end_state() == TAP_DRSHIFT)
  917. {
  918. if (type == SCAN_IO)
  919. {
  920. /* Clock Data Bits In and Out LSB First */
  921. buffer_write(0x3b);
  922. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  923. }
  924. else if (type == SCAN_OUT)
  925. {
  926. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  927. buffer_write(0x1b);
  928. /* LOG_DEBUG("added TDI bits (o)"); */
  929. }
  930. else if (type == SCAN_IN)
  931. {
  932. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  933. buffer_write(0x2a);
  934. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  935. }
  936. buffer_write(0x0);
  937. buffer_write(last_bit);
  938. }
  939. else
  940. {
  941. int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  942. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  943. uint8_t mpsse_cmd;
  944. /* move from Shift-IR/DR to end state */
  945. if (type != SCAN_OUT)
  946. {
  947. /* Clock Data to TMS/CS Pin with Read */
  948. mpsse_cmd = 0x6b;
  949. /* LOG_DEBUG("added TMS scan (read)"); */
  950. }
  951. else
  952. {
  953. /* Clock Data to TMS/CS Pin (no Read) */
  954. mpsse_cmd = 0x4b;
  955. /* LOG_DEBUG("added TMS scan (no read)"); */
  956. }
  957. clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
  958. }
  959. if (type != SCAN_OUT)
  960. thisrun_read += 1;
  961. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  962. {
  963. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  964. exit(-1);
  965. }
  966. LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
  967. ft2232_buffer_size,
  968. (int)bytes_written);
  969. ft2232_buffer_size = 0;
  970. if (type != SCAN_OUT)
  971. {
  972. if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
  973. {
  974. LOG_ERROR("couldn't read from FT2232");
  975. exit(-1);
  976. }
  977. LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
  978. thisrun_read,
  979. (int)bytes_read);
  980. receive_pointer += bytes_read;
  981. }
  982. return ERROR_OK;
  983. }
  984. static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
  985. {
  986. int predicted_size = 3;
  987. int num_bytes = (scan_size - 1) / 8;
  988. if (tap_get_state() != TAP_DRSHIFT)
  989. predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
  990. if (type == SCAN_IN) /* only from device to host */
  991. {
  992. /* complete bytes */
  993. predicted_size += CEIL(num_bytes, 65536) * 3;
  994. /* remaining bits - 1 (up to 7) */
  995. predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
  996. }
  997. else /* host to device, or bidirectional */
  998. {
  999. /* complete bytes */
  1000. predicted_size += num_bytes + CEIL(num_bytes, 65536) * 3;
  1001. /* remaining bits -1 (up to 7) */
  1002. predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
  1003. }
  1004. return predicted_size;
  1005. }
  1006. static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
  1007. {
  1008. int predicted_size = 0;
  1009. if (type != SCAN_OUT)
  1010. {
  1011. /* complete bytes */
  1012. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
  1013. /* remaining bits - 1 */
  1014. predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
  1015. /* last bit (from TMS scan) */
  1016. predicted_size += 1;
  1017. }
  1018. /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
  1019. return predicted_size;
  1020. }
  1021. static void usbjtag_reset(int trst, int srst)
  1022. {
  1023. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1024. if (trst == 1)
  1025. {
  1026. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1027. low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
  1028. else
  1029. low_output &= ~nTRST; /* switch output low */
  1030. }
  1031. else if (trst == 0)
  1032. {
  1033. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1034. low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
  1035. else
  1036. low_output |= nTRST; /* switch output high */
  1037. }
  1038. if (srst == 1)
  1039. {
  1040. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1041. low_output &= ~nSRST; /* switch output low */
  1042. else
  1043. low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
  1044. }
  1045. else if (srst == 0)
  1046. {
  1047. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1048. low_output |= nSRST; /* switch output high */
  1049. else
  1050. low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
  1051. }
  1052. /* command "set data bits low byte" */
  1053. buffer_write(0x80);
  1054. buffer_write(low_output);
  1055. buffer_write(low_direction);
  1056. }
  1057. static void jtagkey_reset(int trst, int srst)
  1058. {
  1059. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1060. if (trst == 1)
  1061. {
  1062. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1063. high_output &= ~nTRSTnOE;
  1064. else
  1065. high_output &= ~nTRST;
  1066. }
  1067. else if (trst == 0)
  1068. {
  1069. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1070. high_output |= nTRSTnOE;
  1071. else
  1072. high_output |= nTRST;
  1073. }
  1074. if (srst == 1)
  1075. {
  1076. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1077. high_output &= ~nSRST;
  1078. else
  1079. high_output &= ~nSRSTnOE;
  1080. }
  1081. else if (srst == 0)
  1082. {
  1083. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1084. high_output |= nSRST;
  1085. else
  1086. high_output |= nSRSTnOE;
  1087. }
  1088. /* command "set data bits high byte" */
  1089. buffer_write(0x82);
  1090. buffer_write(high_output);
  1091. buffer_write(high_direction);
  1092. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1093. high_direction);
  1094. }
  1095. static void olimex_jtag_reset(int trst, int srst)
  1096. {
  1097. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1098. if (trst == 1)
  1099. {
  1100. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1101. high_output &= ~nTRSTnOE;
  1102. else
  1103. high_output &= ~nTRST;
  1104. }
  1105. else if (trst == 0)
  1106. {
  1107. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1108. high_output |= nTRSTnOE;
  1109. else
  1110. high_output |= nTRST;
  1111. }
  1112. if (srst == 1)
  1113. {
  1114. high_output |= nSRST;
  1115. }
  1116. else if (srst == 0)
  1117. {
  1118. high_output &= ~nSRST;
  1119. }
  1120. /* command "set data bits high byte" */
  1121. buffer_write(0x82);
  1122. buffer_write(high_output);
  1123. buffer_write(high_direction);
  1124. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1125. high_direction);
  1126. }
  1127. static void axm0432_jtag_reset(int trst, int srst)
  1128. {
  1129. if (trst == 1)
  1130. {
  1131. tap_set_state(TAP_RESET);
  1132. high_output &= ~nTRST;
  1133. }
  1134. else if (trst == 0)
  1135. {
  1136. high_output |= nTRST;
  1137. }
  1138. if (srst == 1)
  1139. {
  1140. high_output &= ~nSRST;
  1141. }
  1142. else if (srst == 0)
  1143. {
  1144. high_output |= nSRST;
  1145. }
  1146. /* command "set data bits low byte" */
  1147. buffer_write(0x82);
  1148. buffer_write(high_output);
  1149. buffer_write(high_direction);
  1150. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1151. high_direction);
  1152. }
  1153. static void flyswatter_reset(int trst, int srst)
  1154. {
  1155. if (trst == 1)
  1156. {
  1157. low_output &= ~nTRST;
  1158. }
  1159. else if (trst == 0)
  1160. {
  1161. low_output |= nTRST;
  1162. }
  1163. if (srst == 1)
  1164. {
  1165. low_output |= nSRST;
  1166. }
  1167. else if (srst == 0)
  1168. {
  1169. low_output &= ~nSRST;
  1170. }
  1171. /* command "set data bits low byte" */
  1172. buffer_write(0x80);
  1173. buffer_write(low_output);
  1174. buffer_write(low_direction);
  1175. LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
  1176. }
  1177. static void turtle_reset(int trst, int srst)
  1178. {
  1179. trst = trst;
  1180. if (srst == 1)
  1181. {
  1182. low_output |= nSRST;
  1183. }
  1184. else if (srst == 0)
  1185. {
  1186. low_output &= ~nSRST;
  1187. }
  1188. /* command "set data bits low byte" */
  1189. buffer_write(0x80);
  1190. buffer_write(low_output);
  1191. buffer_write(low_direction);
  1192. LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
  1193. }
  1194. static void comstick_reset(int trst, int srst)
  1195. {
  1196. if (trst == 1)
  1197. {
  1198. high_output &= ~nTRST;
  1199. }
  1200. else if (trst == 0)
  1201. {
  1202. high_output |= nTRST;
  1203. }
  1204. if (srst == 1)
  1205. {
  1206. high_output &= ~nSRST;
  1207. }
  1208. else if (srst == 0)
  1209. {
  1210. high_output |= nSRST;
  1211. }
  1212. /* command "set data bits high byte" */
  1213. buffer_write(0x82);
  1214. buffer_write(high_output);
  1215. buffer_write(high_direction);
  1216. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1217. high_direction);
  1218. }
  1219. static void stm32stick_reset(int trst, int srst)
  1220. {
  1221. if (trst == 1)
  1222. {
  1223. high_output &= ~nTRST;
  1224. }
  1225. else if (trst == 0)
  1226. {
  1227. high_output |= nTRST;
  1228. }
  1229. if (srst == 1)
  1230. {
  1231. low_output &= ~nSRST;
  1232. }
  1233. else if (srst == 0)
  1234. {
  1235. low_output |= nSRST;
  1236. }
  1237. /* command "set data bits low byte" */
  1238. buffer_write(0x80);
  1239. buffer_write(low_output);
  1240. buffer_write(low_direction);
  1241. /* command "set data bits high byte" */
  1242. buffer_write(0x82);
  1243. buffer_write(high_output);
  1244. buffer_write(high_direction);
  1245. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1246. high_direction);
  1247. }
  1248. static void sheevaplug_reset(int trst, int srst)
  1249. {
  1250. if (trst == 1)
  1251. high_output &= ~nTRST;
  1252. else if (trst == 0)
  1253. high_output |= nTRST;
  1254. if (srst == 1)
  1255. high_output &= ~nSRSTnOE;
  1256. else if (srst == 0)
  1257. high_output |= nSRSTnOE;
  1258. /* command "set data bits high byte" */
  1259. buffer_write(0x82);
  1260. buffer_write(high_output);
  1261. buffer_write(high_direction);
  1262. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  1263. }
  1264. static int ft2232_execute_runtest(jtag_command_t *cmd)
  1265. {
  1266. int retval;
  1267. int i;
  1268. int predicted_size = 0;
  1269. retval = ERROR_OK;
  1270. DEBUG_JTAG_IO("runtest %i cycles, end in %s",
  1271. cmd->cmd.runtest->num_cycles,
  1272. tap_state_name(cmd->cmd.runtest->end_state));
  1273. /* only send the maximum buffer size that FT2232C can handle */
  1274. predicted_size = 0;
  1275. if (tap_get_state() != TAP_IDLE)
  1276. predicted_size += 3;
  1277. predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
  1278. if (cmd->cmd.runtest->end_state != TAP_IDLE)
  1279. predicted_size += 3;
  1280. if (tap_get_end_state() != TAP_IDLE)
  1281. predicted_size += 3;
  1282. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1283. {
  1284. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1285. retval = ERROR_JTAG_QUEUE_FAILED;
  1286. require_send = 0;
  1287. first_unsent = cmd;
  1288. }
  1289. if (tap_get_state() != TAP_IDLE)
  1290. {
  1291. move_to_state(TAP_IDLE);
  1292. require_send = 1;
  1293. }
  1294. i = cmd->cmd.runtest->num_cycles;
  1295. while (i > 0)
  1296. {
  1297. /* there are no state transitions in this code, so omit state tracking */
  1298. /* command "Clock Data to TMS/CS Pin (no Read)" */
  1299. buffer_write(0x4b);
  1300. /* scan 7 bits */
  1301. buffer_write((i > 7) ? 6 : (i - 1));
  1302. /* TMS data bits */
  1303. buffer_write(0x0);
  1304. tap_set_state(TAP_IDLE);
  1305. i -= (i > 7) ? 7 : i;
  1306. /* LOG_DEBUG("added TMS scan (no read)"); */
  1307. }
  1308. ft2232_end_state(cmd->cmd.runtest->end_state);
  1309. if (tap_get_state() != tap_get_end_state())
  1310. {
  1311. move_to_state(tap_get_end_state());
  1312. }
  1313. require_send = 1;
  1314. #ifdef _DEBUG_JTAG_IO_
  1315. LOG_DEBUG("runtest: %i, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name(tap_get_end_state()));
  1316. #endif
  1317. return retval;
  1318. }
  1319. static int ft2232_execute_statemove(jtag_command_t *cmd)
  1320. {
  1321. int predicted_size = 0;
  1322. int retval = ERROR_OK;
  1323. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  1324. /* only send the maximum buffer size that FT2232C can handle */
  1325. predicted_size = 3;
  1326. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1327. {
  1328. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1329. retval = ERROR_JTAG_QUEUE_FAILED;
  1330. require_send = 0;
  1331. first_unsent = cmd;
  1332. }
  1333. ft2232_end_state(cmd->cmd.statemove->end_state);
  1334. /* move to end state */
  1335. if (tap_get_state() != tap_get_end_state())
  1336. {
  1337. move_to_state(tap_get_end_state());
  1338. require_send = 1;
  1339. }
  1340. return retval;
  1341. }
  1342. static int ft2232_execute_pathmove(jtag_command_t *cmd)
  1343. {
  1344. int predicted_size = 0;
  1345. int retval = ERROR_OK;
  1346. tap_state_t* path = cmd->cmd.pathmove->path;
  1347. int num_states = cmd->cmd.pathmove->num_states;
  1348. DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
  1349. tap_state_name(tap_get_state()),
  1350. tap_state_name(path[num_states-1])
  1351. );
  1352. /* only send the maximum buffer size that FT2232C can handle */
  1353. predicted_size = 3 * CEIL(num_states, 7);
  1354. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1355. {
  1356. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1357. retval = ERROR_JTAG_QUEUE_FAILED;
  1358. require_send = 0;
  1359. first_unsent = cmd;
  1360. }
  1361. ft2232_add_pathmove(path, num_states);
  1362. require_send = 1;
  1363. return retval;
  1364. }
  1365. static int ft2232_execute_scan(jtag_command_t *cmd)
  1366. {
  1367. uint8_t* buffer;
  1368. int scan_size; /* size of IR or DR scan */
  1369. int predicted_size = 0;
  1370. int retval = ERROR_OK;
  1371. enum scan_type type = jtag_scan_type(cmd->cmd.scan);
  1372. DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
  1373. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  1374. predicted_size = ft2232_predict_scan_out(scan_size, type);
  1375. if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
  1376. {
  1377. LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
  1378. /* unsent commands before this */
  1379. if (first_unsent != cmd)
  1380. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1381. retval = ERROR_JTAG_QUEUE_FAILED;
  1382. /* current command */
  1383. ft2232_end_state(cmd->cmd.scan->end_state);
  1384. ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
  1385. require_send = 0;
  1386. first_unsent = cmd->next;
  1387. if (buffer)
  1388. free(buffer);
  1389. return retval;
  1390. }
  1391. else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1392. {
  1393. LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
  1394. first_unsent,
  1395. cmd);
  1396. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1397. retval = ERROR_JTAG_QUEUE_FAILED;
  1398. require_send = 0;
  1399. first_unsent = cmd;
  1400. }
  1401. ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
  1402. /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
  1403. ft2232_end_state(cmd->cmd.scan->end_state);
  1404. ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  1405. require_send = 1;
  1406. if (buffer)
  1407. free(buffer);
  1408. #ifdef _DEBUG_JTAG_IO_
  1409. LOG_DEBUG("%s scan, %i bits, end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
  1410. tap_state_name(tap_get_end_state()));
  1411. #endif
  1412. return retval;
  1413. }
  1414. static int ft2232_execute_reset(jtag_command_t *cmd)
  1415. {
  1416. int retval;
  1417. int predicted_size = 0;
  1418. retval = ERROR_OK;
  1419. DEBUG_JTAG_IO("reset trst: %i srst %i",
  1420. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1421. /* only send the maximum buffer size that FT2232C can handle */
  1422. predicted_size = 3;
  1423. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1424. {
  1425. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1426. retval = ERROR_JTAG_QUEUE_FAILED;
  1427. require_send = 0;
  1428. first_unsent = cmd;
  1429. }
  1430. layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1431. require_send = 1;
  1432. #ifdef _DEBUG_JTAG_IO_
  1433. LOG_DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1434. #endif
  1435. return retval;
  1436. }
  1437. static int ft2232_execute_sleep(jtag_command_t *cmd)
  1438. {
  1439. int retval;
  1440. retval = ERROR_OK;
  1441. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  1442. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1443. retval = ERROR_JTAG_QUEUE_FAILED;
  1444. first_unsent = cmd->next;
  1445. jtag_sleep(cmd->cmd.sleep->us);
  1446. #ifdef _DEBUG_JTAG_IO_
  1447. LOG_DEBUG("sleep %i usec while in %s", cmd->cmd.sleep->us, tap_state_name(tap_get_state()));
  1448. #endif
  1449. return retval;
  1450. }
  1451. static int ft2232_execute_stableclocks(jtag_command_t *cmd)
  1452. {
  1453. int retval;
  1454. retval = ERROR_OK;
  1455. /* this is only allowed while in a stable state. A check for a stable
  1456. * state was done in jtag_add_clocks()
  1457. */
  1458. if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
  1459. retval = ERROR_JTAG_QUEUE_FAILED;
  1460. #ifdef _DEBUG_JTAG_IO_
  1461. LOG_DEBUG("clocks %i while in %s", cmd->cmd.stableclocks->num_cycles, tap_state_name(tap_get_state()));
  1462. #endif
  1463. return retval;
  1464. }
  1465. static int ft2232_execute_command(jtag_command_t *cmd)
  1466. {
  1467. int retval;
  1468. retval = ERROR_OK;
  1469. switch (cmd->type)
  1470. {
  1471. case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
  1472. case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
  1473. case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
  1474. case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
  1475. case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
  1476. case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
  1477. case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
  1478. default:
  1479. LOG_ERROR("BUG: unknown JTAG command type encountered");
  1480. exit(-1);
  1481. }
  1482. return retval;
  1483. }
  1484. static int ft2232_execute_queue()
  1485. {
  1486. jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
  1487. int retval;
  1488. first_unsent = cmd; /* next command that has to be sent */
  1489. require_send = 0;
  1490. /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
  1491. * that wasn't handled by a caller-provided error handler
  1492. */
  1493. retval = ERROR_OK;
  1494. ft2232_buffer_size = 0;
  1495. ft2232_expect_read = 0;
  1496. /* blink, if the current layout has that feature */
  1497. if (layout->blink)
  1498. layout->blink();
  1499. while (cmd)
  1500. {
  1501. if (ft2232_execute_command(cmd) != ERROR_OK)
  1502. retval = ERROR_JTAG_QUEUE_FAILED;
  1503. /* Start reading input before FT2232 TX buffer fills up */
  1504. cmd = cmd->next;
  1505. if (ft2232_expect_read > 256)
  1506. {
  1507. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1508. retval = ERROR_JTAG_QUEUE_FAILED;
  1509. first_unsent = cmd;
  1510. }
  1511. }
  1512. if (require_send > 0)
  1513. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1514. retval = ERROR_JTAG_QUEUE_FAILED;
  1515. return retval;
  1516. }
  1517. #if BUILD_FT2232_FTD2XX == 1
  1518. static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
  1519. {
  1520. FT_STATUS status;
  1521. DWORD deviceID;
  1522. char SerialNumber[16];
  1523. char Description[64];
  1524. DWORD openex_flags = 0;
  1525. char* openex_string = NULL;
  1526. uint8_t latency_timer;
  1527. LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
  1528. #if IS_WIN32 == 0
  1529. /* Add non-standard Vid/Pid to the linux driver */
  1530. if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
  1531. {
  1532. LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
  1533. }
  1534. #endif
  1535. if (ft2232_device_desc && ft2232_serial)
  1536. {
  1537. LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
  1538. ft2232_device_desc = NULL;
  1539. }
  1540. if (ft2232_device_desc)
  1541. {
  1542. openex_string = ft2232_device_desc;
  1543. openex_flags = FT_OPEN_BY_DESCRIPTION;
  1544. }
  1545. else if (ft2232_serial)
  1546. {
  1547. openex_string = ft2232_serial;
  1548. openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
  1549. }
  1550. else
  1551. {
  1552. LOG_ERROR("neither device description nor serial number specified");
  1553. LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
  1554. return ERROR_JTAG_INIT_FAILED;
  1555. }
  1556. status = FT_OpenEx(openex_string, openex_flags, &ftdih);
  1557. if (status != FT_OK) {
  1558. // under Win32, the FTD2XX driver appends an "A" to the end
  1559. // of the description, if we tried by the desc, then
  1560. // try by the alternate "A" description.
  1561. if (openex_string == ft2232_device_desc) {
  1562. // Try the alternate method.
  1563. openex_string = ft2232_device_desc_A;
  1564. status = FT_OpenEx(openex_string, openex_flags, &ftdih);
  1565. if (status == FT_OK) {
  1566. // yea, the "alternate" method worked!
  1567. } else {
  1568. // drat, give the user a meaningfull message.
  1569. // telling the use we tried *BOTH* methods.
  1570. LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
  1571. ft2232_device_desc,
  1572. ft2232_device_desc_A);
  1573. }
  1574. }
  1575. }
  1576. if (status != FT_OK)
  1577. {
  1578. DWORD num_devices;
  1579. if (more)
  1580. {
  1581. LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
  1582. *try_more = 1;
  1583. return ERROR_JTAG_INIT_FAILED;
  1584. }
  1585. LOG_ERROR("unable to open ftdi device: %lu", status);
  1586. status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
  1587. if (status == FT_OK)
  1588. {
  1589. char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
  1590. uint32_t i;
  1591. for (i = 0; i < num_devices; i++)
  1592. desc_array[i] = malloc(64);
  1593. desc_array[num_devices] = NULL;
  1594. status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
  1595. if (status == FT_OK)
  1596. {
  1597. LOG_ERROR("ListDevices: %lu\n", num_devices);
  1598. for (i = 0; i < num_devices; i++)
  1599. LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
  1600. }
  1601. for (i = 0; i < num_devices; i++)
  1602. free(desc_array[i]);
  1603. free(desc_array);
  1604. }
  1605. else
  1606. {
  1607. LOG_ERROR("ListDevices: NONE\n");
  1608. }
  1609. return ERROR_JTAG_INIT_FAILED;
  1610. }
  1611. if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
  1612. {
  1613. LOG_ERROR("unable to set latency timer: %lu", status);
  1614. return ERROR_JTAG_INIT_FAILED;
  1615. }
  1616. if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
  1617. {
  1618. LOG_ERROR("unable to get latency timer: %lu", status);
  1619. return ERROR_JTAG_INIT_FAILED;
  1620. }
  1621. else
  1622. {
  1623. LOG_DEBUG("current latency timer: %i", latency_timer);
  1624. }
  1625. if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
  1626. {
  1627. LOG_ERROR("unable to set timeouts: %lu", status);
  1628. return ERROR_JTAG_INIT_FAILED;
  1629. }
  1630. if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
  1631. {
  1632. LOG_ERROR("unable to enable bit i/o mode: %lu", status);
  1633. return ERROR_JTAG_INIT_FAILED;
  1634. }
  1635. if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
  1636. {
  1637. LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
  1638. return ERROR_JTAG_INIT_FAILED;
  1639. }
  1640. else
  1641. {
  1642. LOG_INFO("device: %lu", ftdi_device);
  1643. LOG_INFO("deviceID: %lu", deviceID);
  1644. LOG_INFO("SerialNumber: %s", SerialNumber);
  1645. LOG_INFO("Description: %s", Description);
  1646. #ifdef BUILD_FTD2XX_HIGHSPEED
  1647. if (ft2232_device_is_highspeed())
  1648. {
  1649. ft2232_max_tck = FTDI_2232H_4232H_MAX_TCK;
  1650. LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
  1651. }
  1652. #endif
  1653. }
  1654. return ERROR_OK;
  1655. }
  1656. static int ft2232_purge_ftd2xx(void)
  1657. {
  1658. FT_STATUS status;
  1659. if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
  1660. {
  1661. LOG_ERROR("error purging ftd2xx device: %lu", status);
  1662. return ERROR_JTAG_INIT_FAILED;
  1663. }
  1664. return ERROR_OK;
  1665. }
  1666. #endif /* BUILD_FT2232_FTD2XX == 1 */
  1667. #if BUILD_FT2232_LIBFTDI == 1
  1668. static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
  1669. {
  1670. uint8_t latency_timer;
  1671. LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
  1672. ft2232_layout, vid, pid);
  1673. if (ftdi_init(&ftdic) < 0)
  1674. return ERROR_JTAG_INIT_FAILED;
  1675. if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
  1676. {
  1677. LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
  1678. return ERROR_JTAG_INIT_FAILED;
  1679. }
  1680. /* context, vendor id, product id */
  1681. if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
  1682. ft2232_serial) < 0)
  1683. {
  1684. if (more)
  1685. LOG_WARNING("unable to open ftdi device (trying more): %s",
  1686. ftdic.error_str);
  1687. else
  1688. LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
  1689. *try_more = 1;
  1690. return ERROR_JTAG_INIT_FAILED;
  1691. }
  1692. /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
  1693. if (ftdi_usb_reset(&ftdic) < 0)
  1694. {
  1695. LOG_ERROR("unable to reset ftdi device");
  1696. return ERROR_JTAG_INIT_FAILED;
  1697. }
  1698. if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
  1699. {
  1700. LOG_ERROR("unable to set latency timer");
  1701. return ERROR_JTAG_INIT_FAILED;
  1702. }
  1703. if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
  1704. {
  1705. LOG_ERROR("unable to get latency timer");
  1706. return ERROR_JTAG_INIT_FAILED;
  1707. }
  1708. else
  1709. {
  1710. LOG_DEBUG("current latency timer: %i", latency_timer);
  1711. }
  1712. ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
  1713. return ERROR_OK;
  1714. }
  1715. static int ft2232_purge_libftdi(void)
  1716. {
  1717. if (ftdi_usb_purge_buffers(&ftdic) < 0)
  1718. {
  1719. LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  1720. return ERROR_JTAG_INIT_FAILED;
  1721. }
  1722. return ERROR_OK;
  1723. }
  1724. #endif /* BUILD_FT2232_LIBFTDI == 1 */
  1725. static int ft2232_init(void)
  1726. {
  1727. uint8_t buf[1];
  1728. int retval;
  1729. uint32_t bytes_written;
  1730. const ft2232_layout_t* cur_layout = ft2232_layouts;
  1731. int i;
  1732. if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
  1733. {
  1734. LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
  1735. }
  1736. else
  1737. {
  1738. LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
  1739. }
  1740. if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
  1741. {
  1742. ft2232_layout = "usbjtag";
  1743. LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
  1744. }
  1745. while (cur_layout->name)
  1746. {
  1747. if (strcmp(cur_layout->name, ft2232_layout) == 0)
  1748. {
  1749. layout = cur_layout;
  1750. break;
  1751. }
  1752. cur_layout++;
  1753. }
  1754. if (!layout)
  1755. {
  1756. LOG_ERROR("No matching layout found for %s", ft2232_layout);
  1757. return ERROR_JTAG_INIT_FAILED;
  1758. }
  1759. for (i = 0; 1; i++)
  1760. {
  1761. /*
  1762. * "more indicates that there are more IDs to try, so we should
  1763. * not print an error for an ID mismatch (but for anything
  1764. * else, we should).
  1765. *
  1766. * try_more indicates that the error code returned indicates an
  1767. * ID mismatch (and nothing else) and that we should proceeed
  1768. * with the next ID pair.
  1769. */
  1770. int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
  1771. int try_more = 0;
  1772. #if BUILD_FT2232_FTD2XX == 1
  1773. retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
  1774. more, &try_more);
  1775. #elif BUILD_FT2232_LIBFTDI == 1
  1776. retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
  1777. more, &try_more);
  1778. #endif
  1779. if (retval >= 0)
  1780. break;
  1781. if (!more || !try_more)
  1782. return retval;
  1783. }
  1784. ft2232_buffer_size = 0;
  1785. ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
  1786. if (layout->init() != ERROR_OK)
  1787. return ERROR_JTAG_INIT_FAILED;
  1788. ft2232_speed(jtag_get_speed());
  1789. buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
  1790. if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
  1791. {
  1792. LOG_ERROR("couldn't write to FT2232 to disable loopback");
  1793. return ERROR_JTAG_INIT_FAILED;
  1794. }
  1795. #if BUILD_FT2232_FTD2XX == 1
  1796. return ft2232_purge_ftd2xx();
  1797. #elif BUILD_FT2232_LIBFTDI == 1
  1798. return ft2232_purge_libftdi();
  1799. #endif
  1800. return ERROR_OK;
  1801. }
  1802. static int usbjtag_init(void)
  1803. {
  1804. uint8_t buf[3];
  1805. uint32_t bytes_written;
  1806. low_output = 0x08;
  1807. low_direction = 0x0b;
  1808. if (strcmp(ft2232_layout, "usbjtag") == 0)
  1809. {
  1810. nTRST = 0x10;
  1811. nTRSTnOE = 0x10;
  1812. nSRST = 0x40;
  1813. nSRSTnOE = 0x40;
  1814. }
  1815. else if (strcmp(ft2232_layout, "signalyzer") == 0)
  1816. {
  1817. nTRST = 0x10;
  1818. nTRSTnOE = 0x10;
  1819. nSRST = 0x20;
  1820. nSRSTnOE = 0x20;
  1821. }
  1822. else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
  1823. {
  1824. nTRST = 0x0;
  1825. nTRSTnOE = 0x00;
  1826. nSRST = 0x20;
  1827. nSRSTnOE = 0x20;
  1828. low_output = 0x88;
  1829. low_direction = 0x8b;
  1830. }
  1831. else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
  1832. {
  1833. nTRST = 0x0;
  1834. nTRSTnOE = 0x00;
  1835. nSRST = 0x20;
  1836. nSRSTnOE = 0x20;
  1837. low_output = 0x88;
  1838. low_direction = 0xcb;
  1839. }
  1840. else
  1841. {
  1842. LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
  1843. return ERROR_JTAG_INIT_FAILED;
  1844. }
  1845. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1846. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1847. {
  1848. low_direction &= ~nTRSTnOE; /* nTRST input */
  1849. low_output &= ~nTRST; /* nTRST = 0 */
  1850. }
  1851. else
  1852. {
  1853. low_direction |= nTRSTnOE; /* nTRST output */
  1854. low_output |= nTRST; /* nTRST = 1 */
  1855. }
  1856. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1857. {
  1858. low_direction |= nSRSTnOE; /* nSRST output */
  1859. low_output |= nSRST; /* nSRST = 1 */
  1860. }
  1861. else
  1862. {
  1863. low_direction &= ~nSRSTnOE; /* nSRST input */
  1864. low_output &= ~nSRST; /* nSRST = 0 */
  1865. }
  1866. /* initialize low byte for jtag */
  1867. buf[0] = 0x80; /* command "set data bits low byte" */
  1868. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
  1869. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
  1870. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1871. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1872. {
  1873. LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
  1874. return ERROR_JTAG_INIT_FAILED;
  1875. }
  1876. return ERROR_OK;
  1877. }
  1878. static int axm0432_jtag_init(void)
  1879. {
  1880. uint8_t buf[3];
  1881. uint32_t bytes_written;
  1882. low_output = 0x08;
  1883. low_direction = 0x2b;
  1884. /* initialize low byte for jtag */
  1885. buf[0] = 0x80; /* command "set data bits low byte" */
  1886. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  1887. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  1888. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1889. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1890. {
  1891. LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1892. return ERROR_JTAG_INIT_FAILED;
  1893. }
  1894. if (strcmp(layout->name, "axm0432_jtag") == 0)
  1895. {
  1896. nTRST = 0x08;
  1897. nTRSTnOE = 0x0; /* No output enable for TRST*/
  1898. nSRST = 0x04;
  1899. nSRSTnOE = 0x0; /* No output enable for SRST*/
  1900. }
  1901. else
  1902. {
  1903. LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
  1904. exit(-1);
  1905. }
  1906. high_output = 0x0;
  1907. high_direction = 0x0c;
  1908. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1909. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1910. {
  1911. LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
  1912. }
  1913. else
  1914. {
  1915. high_output |= nTRST;
  1916. }
  1917. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1918. {
  1919. LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
  1920. }
  1921. else
  1922. {
  1923. high_output |= nSRST;
  1924. }
  1925. /* initialize high port */
  1926. buf[0] = 0x82; /* command "set data bits high byte" */
  1927. buf[1] = high_output; /* value */
  1928. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1929. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1930. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1931. {
  1932. LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
  1933. return ERROR_JTAG_INIT_FAILED;
  1934. }
  1935. return ERROR_OK;
  1936. }
  1937. static int jtagkey_init(void)
  1938. {
  1939. uint8_t buf[3];
  1940. uint32_t bytes_written;
  1941. low_output = 0x08;
  1942. low_direction = 0x1b;
  1943. /* initialize low byte for jtag */
  1944. buf[0] = 0x80; /* command "set data bits low byte" */
  1945. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  1946. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  1947. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1948. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1949. {
  1950. LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1951. return ERROR_JTAG_INIT_FAILED;
  1952. }
  1953. if (strcmp(layout->name, "jtagkey") == 0)
  1954. {
  1955. nTRST = 0x01;
  1956. nTRSTnOE = 0x4;
  1957. nSRST = 0x02;
  1958. nSRSTnOE = 0x08;
  1959. }
  1960. else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
  1961. || (strcmp(layout->name, "oocdlink") == 0))
  1962. {
  1963. nTRST = 0x02;
  1964. nTRSTnOE = 0x1;
  1965. nSRST = 0x08;
  1966. nSRSTnOE = 0x04;
  1967. }
  1968. else
  1969. {
  1970. LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
  1971. exit(-1);
  1972. }
  1973. high_output = 0x0;
  1974. high_direction = 0x0f;
  1975. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1976. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1977. {
  1978. high_output |= nTRSTnOE;
  1979. high_output &= ~nTRST;
  1980. }
  1981. else
  1982. {
  1983. high_output &= ~nTRSTnOE;
  1984. high_output |= nTRST;
  1985. }
  1986. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1987. {
  1988. high_output &= ~nSRSTnOE;
  1989. high_output |= nSRST;
  1990. }
  1991. else
  1992. {
  1993. high_output |= nSRSTnOE;
  1994. high_output &= ~nSRST;
  1995. }
  1996. /* initialize high port */
  1997. buf[0] = 0x82; /* command "set data bits high byte" */
  1998. buf[1] = high_output; /* value */
  1999. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2000. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2001. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2002. {
  2003. LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  2004. return ERROR_JTAG_INIT_FAILED;
  2005. }
  2006. return ERROR_OK;
  2007. }
  2008. static int olimex_jtag_init(void)
  2009. {
  2010. uint8_t buf[3];
  2011. uint32_t bytes_written;
  2012. low_output = 0x08;
  2013. low_direction = 0x1b;
  2014. /* initialize low byte for jtag */
  2015. buf[0] = 0x80; /* command "set data bits low byte" */
  2016. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2017. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2018. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2019. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2020. {
  2021. LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
  2022. return ERROR_JTAG_INIT_FAILED;
  2023. }
  2024. nTRST = 0x01;
  2025. nTRSTnOE = 0x4;
  2026. nSRST = 0x02;
  2027. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2028. high_output = 0x0;
  2029. high_direction = 0x0f;
  2030. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2031. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  2032. {
  2033. high_output |= nTRSTnOE;
  2034. high_output &= ~nTRST;
  2035. }
  2036. else
  2037. {
  2038. high_output &= ~nTRSTnOE;
  2039. high_output |= nTRST;
  2040. }
  2041. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  2042. {
  2043. LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
  2044. }
  2045. else
  2046. {
  2047. high_output &= ~nSRST;
  2048. }
  2049. /* turn red LED on */
  2050. high_output |= 0x08;
  2051. /* initialize high port */
  2052. buf[0] = 0x82; /* command "set data bits high byte" */
  2053. buf[1] = high_output; /* value */
  2054. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2055. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2056. if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
  2057. {
  2058. LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
  2059. return ERROR_JTAG_INIT_FAILED;
  2060. }
  2061. return ERROR_OK;
  2062. }
  2063. static int flyswatter_init(void)
  2064. {
  2065. uint8_t buf[3];
  2066. uint32_t bytes_written;
  2067. low_output = 0x18;
  2068. low_direction = 0xfb;
  2069. /* initialize low byte for jtag */
  2070. buf[0] = 0x80; /* command "set data bits low byte" */
  2071. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2072. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
  2073. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2074. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2075. {
  2076. LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
  2077. return ERROR_JTAG_INIT_FAILED;
  2078. }
  2079. nTRST = 0x10;
  2080. nTRSTnOE = 0x0; /* not output enable for nTRST */
  2081. nSRST = 0x20;
  2082. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2083. high_output = 0x00;
  2084. high_direction = 0x0c;
  2085. /* turn red LED3 on, LED2 off */
  2086. high_output |= 0x08;
  2087. /* initialize high port */
  2088. buf[0] = 0x82; /* command "set data bits high byte" */
  2089. buf[1] = high_output; /* value */
  2090. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2091. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2092. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2093. {
  2094. LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
  2095. return ERROR_JTAG_INIT_FAILED;
  2096. }
  2097. return ERROR_OK;
  2098. }
  2099. static int turtle_init(void)
  2100. {
  2101. uint8_t buf[3];
  2102. uint32_t bytes_written;
  2103. low_output = 0x08;
  2104. low_direction = 0x5b;
  2105. /* initialize low byte for jtag */
  2106. buf[0] = 0x80; /* command "set data bits low byte" */
  2107. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2108. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2109. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2110. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2111. {
  2112. LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
  2113. return ERROR_JTAG_INIT_FAILED;
  2114. }
  2115. nSRST = 0x40;
  2116. high_output = 0x00;
  2117. high_direction = 0x0C;
  2118. /* initialize high port */
  2119. buf[0] = 0x82; /* command "set data bits high byte" */
  2120. buf[1] = high_output;
  2121. buf[2] = high_direction;
  2122. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2123. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2124. {
  2125. LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
  2126. return ERROR_JTAG_INIT_FAILED;
  2127. }
  2128. return ERROR_OK;
  2129. }
  2130. static int comstick_init(void)
  2131. {
  2132. uint8_t buf[3];
  2133. uint32_t bytes_written;
  2134. low_output = 0x08;
  2135. low_direction = 0x0b;
  2136. /* initialize low byte for jtag */
  2137. buf[0] = 0x80; /* command "set data bits low byte" */
  2138. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2139. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2140. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2141. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2142. {
  2143. LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
  2144. return ERROR_JTAG_INIT_FAILED;
  2145. }
  2146. nTRST = 0x01;
  2147. nTRSTnOE = 0x00; /* no output enable for nTRST */
  2148. nSRST = 0x02;
  2149. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2150. high_output = 0x03;
  2151. high_direction = 0x03;
  2152. /* initialize high port */
  2153. buf[0] = 0x82; /* command "set data bits high byte" */
  2154. buf[1] = high_output;
  2155. buf[2] = high_direction;
  2156. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2157. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2158. {
  2159. LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
  2160. return ERROR_JTAG_INIT_FAILED;
  2161. }
  2162. return ERROR_OK;
  2163. }
  2164. static int stm32stick_init(void)
  2165. {
  2166. uint8_t buf[3];
  2167. uint32_t bytes_written;
  2168. low_output = 0x88;
  2169. low_direction = 0x8b;
  2170. /* initialize low byte for jtag */
  2171. buf[0] = 0x80; /* command "set data bits low byte" */
  2172. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2173. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2174. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2175. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2176. {
  2177. LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
  2178. return ERROR_JTAG_INIT_FAILED;
  2179. }
  2180. nTRST = 0x01;
  2181. nTRSTnOE = 0x00; /* no output enable for nTRST */
  2182. nSRST = 0x80;
  2183. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2184. high_output = 0x01;
  2185. high_direction = 0x03;
  2186. /* initialize high port */
  2187. buf[0] = 0x82; /* command "set data bits high byte" */
  2188. buf[1] = high_output;
  2189. buf[2] = high_direction;
  2190. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2191. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2192. {
  2193. LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
  2194. return ERROR_JTAG_INIT_FAILED;
  2195. }
  2196. return ERROR_OK;
  2197. }
  2198. static int sheevaplug_init(void)
  2199. {
  2200. uint8_t buf[3];
  2201. uint32_t bytes_written;
  2202. low_output = 0x08;
  2203. low_direction = 0x1b;
  2204. /* initialize low byte for jtag */
  2205. buf[0] = 0x80; /* command "set data bits low byte" */
  2206. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2207. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
  2208. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2209. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2210. {
  2211. LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
  2212. return ERROR_JTAG_INIT_FAILED;
  2213. }
  2214. nTRSTnOE = 0x1;
  2215. nTRST = 0x02;
  2216. nSRSTnOE = 0x4;
  2217. nSRST = 0x08;
  2218. high_output = 0x0;
  2219. high_direction = 0x0f;
  2220. /* nTRST is always push-pull */
  2221. high_output &= ~nTRSTnOE;
  2222. high_output |= nTRST;
  2223. /* nSRST is always open-drain */
  2224. high_output |= nSRSTnOE;
  2225. high_output &= ~nSRST;
  2226. /* initialize high port */
  2227. buf[0] = 0x82; /* command "set data bits high byte" */
  2228. buf[1] = high_output; /* value */
  2229. buf[2] = high_direction; /* all outputs - xRST */
  2230. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2231. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2232. {
  2233. LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
  2234. return ERROR_JTAG_INIT_FAILED;
  2235. }
  2236. return ERROR_OK;
  2237. }
  2238. static int cortino_jtag_init(void)
  2239. {
  2240. uint8_t buf[3];
  2241. uint32_t bytes_written;
  2242. low_output = 0x08;
  2243. low_direction = 0x1b;
  2244. /* initialize low byte for jtag */
  2245. buf[0] = 0x80; /* command "set data bits low byte" */
  2246. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2247. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2248. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2249. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2250. {
  2251. LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
  2252. return ERROR_JTAG_INIT_FAILED;
  2253. }
  2254. nTRST = 0x01;
  2255. nTRSTnOE = 0x00; /* no output enable for nTRST */
  2256. nSRST = 0x02;
  2257. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2258. high_output = 0x03;
  2259. high_direction = 0x03;
  2260. /* initialize high port */
  2261. buf[0] = 0x82; /* command "set data bits high byte" */
  2262. buf[1] = high_output;
  2263. buf[2] = high_direction;
  2264. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2265. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2266. {
  2267. LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
  2268. return ERROR_JTAG_INIT_FAILED;
  2269. }
  2270. return ERROR_OK;
  2271. }
  2272. static void olimex_jtag_blink(void)
  2273. {
  2274. /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
  2275. * ACBUS3 is bit 3 of the GPIOH port
  2276. */
  2277. if (high_output & 0x08)
  2278. {
  2279. /* set port pin high */
  2280. high_output &= 0x07;
  2281. }
  2282. else
  2283. {
  2284. /* set port pin low */
  2285. high_output |= 0x08;
  2286. }
  2287. buffer_write(0x82);
  2288. buffer_write(high_output);
  2289. buffer_write(high_direction);
  2290. }
  2291. static void flyswatter_jtag_blink(void)
  2292. {
  2293. /*
  2294. * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
  2295. */
  2296. high_output ^= 0x0c;
  2297. buffer_write(0x82);
  2298. buffer_write(high_output);
  2299. buffer_write(high_direction);
  2300. }
  2301. static void turtle_jtag_blink(void)
  2302. {
  2303. /*
  2304. * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
  2305. */
  2306. if (high_output & 0x08)
  2307. {
  2308. high_output = 0x04;
  2309. }
  2310. else
  2311. {
  2312. high_output = 0x08;
  2313. }
  2314. buffer_write(0x82);
  2315. buffer_write(high_output);
  2316. buffer_write(high_direction);
  2317. }
  2318. static int ft2232_quit(void)
  2319. {
  2320. #if BUILD_FT2232_FTD2XX == 1
  2321. FT_STATUS status;
  2322. status = FT_Close(ftdih);
  2323. #elif BUILD_FT2232_LIBFTDI == 1
  2324. ftdi_usb_close(&ftdic);
  2325. ftdi_deinit(&ftdic);
  2326. #endif
  2327. free(ft2232_buffer);
  2328. ft2232_buffer = NULL;
  2329. return ERROR_OK;
  2330. }
  2331. static int ft2232_handle_device_desc_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
  2332. {
  2333. char *cp;
  2334. char buf[200];
  2335. if (argc == 1)
  2336. {
  2337. ft2232_device_desc = strdup(args[0]);
  2338. cp = strchr(ft2232_device_desc, 0);
  2339. // under Win32, the FTD2XX driver appends an "A" to the end
  2340. // of the description, this examines the given desc
  2341. // and creates the 'missing' _A or non_A variable.
  2342. if ((cp[-1] == 'A') && (cp[-2]==' ')) {
  2343. // it was, so make this the "A" version.
  2344. ft2232_device_desc_A = ft2232_device_desc;
  2345. // and *CREATE* the non-A version.
  2346. strcpy(buf, ft2232_device_desc);
  2347. cp = strchr(buf, 0);
  2348. cp[-2] = 0;
  2349. ft2232_device_desc = strdup(buf);
  2350. } else {
  2351. // <space > A not defined
  2352. // so create it
  2353. sprintf(buf, "%s A", ft2232_device_desc);
  2354. ft2232_device_desc_A = strdup(buf);
  2355. }
  2356. }
  2357. else
  2358. {
  2359. LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
  2360. }
  2361. return ERROR_OK;
  2362. }
  2363. static int ft2232_handle_serial_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
  2364. {
  2365. if (argc == 1)
  2366. {
  2367. ft2232_serial = strdup(args[0]);
  2368. }
  2369. else
  2370. {
  2371. LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
  2372. }
  2373. return ERROR_OK;
  2374. }
  2375. static int ft2232_handle_layout_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
  2376. {
  2377. if (argc == 0)
  2378. return ERROR_OK;
  2379. ft2232_layout = malloc(strlen(args[0]) + 1);
  2380. strcpy(ft2232_layout, args[0]);
  2381. return ERROR_OK;
  2382. }
  2383. static int ft2232_handle_vid_pid_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
  2384. {
  2385. if (argc > MAX_USB_IDS * 2)
  2386. {
  2387. LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
  2388. "(maximum is %d pairs)", MAX_USB_IDS);
  2389. argc = MAX_USB_IDS * 2;
  2390. }
  2391. if (argc < 2 || (argc & 1))
  2392. {
  2393. LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
  2394. if (argc < 2)
  2395. return ERROR_COMMAND_SYNTAX_ERROR;
  2396. // remove the incomplete trailing id
  2397. argc -= 1;
  2398. }
  2399. int i;
  2400. int retval = ERROR_OK;
  2401. for (i = 0; i < argc; i += 2)
  2402. {
  2403. retval = parse_u16(args[i], &ft2232_vid[i >> 1]);
  2404. if (ERROR_OK != retval)
  2405. break;
  2406. retval = parse_u16(args[i + 1], &ft2232_pid[i >> 1]);
  2407. if (ERROR_OK != retval)
  2408. break;
  2409. }
  2410. /*
  2411. * Explicitly terminate, in case there are multiples instances of
  2412. * ft2232_vid_pid.
  2413. */
  2414. ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
  2415. return retval;
  2416. }
  2417. static int ft2232_handle_latency_command(struct command_context_s* cmd_ctx, char* cmd, char** args, int argc)
  2418. {
  2419. if (argc == 1)
  2420. {
  2421. ft2232_latency = atoi(args[0]);
  2422. }
  2423. else
  2424. {
  2425. LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
  2426. }
  2427. return ERROR_OK;
  2428. }
  2429. static int ft2232_stableclocks(int num_cycles, jtag_command_t* cmd)
  2430. {
  2431. int retval = 0;
  2432. /* 7 bits of either ones or zeros. */
  2433. uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
  2434. while (num_cycles > 0)
  2435. {
  2436. /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
  2437. * at most 7 bits per invocation. Here we invoke it potentially
  2438. * several times.
  2439. */
  2440. int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
  2441. if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
  2442. {
  2443. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  2444. retval = ERROR_JTAG_QUEUE_FAILED;
  2445. first_unsent = cmd;
  2446. }
  2447. /* there are no state transitions in this code, so omit state tracking */
  2448. /* command "Clock Data to TMS/CS Pin (no Read)" */
  2449. buffer_write(0x4b);
  2450. /* scan 7 bit */
  2451. buffer_write(bitcount_per_command - 1);
  2452. /* TMS data bits are either all zeros or ones to stay in the current stable state */
  2453. buffer_write(tms);
  2454. require_send = 1;
  2455. num_cycles -= bitcount_per_command;
  2456. }
  2457. return retval;
  2458. }
  2459. /* ---------------------------------------------------------------------
  2460. * Support for IceBear JTAG adapter from Section5:
  2461. * http://section5.ch/icebear
  2462. *
  2463. * Author: Sten, debian@sansys-electronic.com
  2464. */
  2465. /* Icebear pin layout
  2466. *
  2467. * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
  2468. * GND GND | 4 3| n.c.
  2469. * ADBUS3 TMS | 6 5| ADBUS6 VCC
  2470. * ADBUS0 TCK | 8 7| ADBUS7 (GND)
  2471. * ADBUS4 nTRST |10 9| ACBUS0 (GND)
  2472. * ADBUS1 TDI |12 11| ACBUS1 (GND)
  2473. * ADBUS2 TDO |14 13| GND GND
  2474. *
  2475. * ADBUS0 O L TCK ACBUS0 GND
  2476. * ADBUS1 O L TDI ACBUS1 GND
  2477. * ADBUS2 I TDO ACBUS2 n.c.
  2478. * ADBUS3 O H TMS ACBUS3 n.c.
  2479. * ADBUS4 O H nTRST
  2480. * ADBUS5 O H nSRST
  2481. * ADBUS6 - VCC
  2482. * ADBUS7 - GND
  2483. */
  2484. static int icebear_jtag_init(void) {
  2485. uint8_t buf[3];
  2486. uint32_t bytes_written;
  2487. low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
  2488. low_output = 0x08; /* high: TMS; low: TCK TDI */
  2489. nTRST = 0x10;
  2490. nSRST = 0x20;
  2491. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2492. if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
  2493. low_direction &= ~nTRST; /* nTRST high impedance */
  2494. }
  2495. else {
  2496. low_direction |= nTRST;
  2497. low_output |= nTRST;
  2498. }
  2499. low_direction |= nSRST;
  2500. low_output |= nSRST;
  2501. /* initialize low byte for jtag */
  2502. buf[0] = 0x80; /* command "set data bits low byte" */
  2503. buf[1] = low_output;
  2504. buf[2] = low_direction;
  2505. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2506. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
  2507. LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
  2508. return ERROR_JTAG_INIT_FAILED;
  2509. }
  2510. high_output = 0x0;
  2511. high_direction = 0x00;
  2512. /* initialize high port */
  2513. buf[0] = 0x82; /* command "set data bits high byte" */
  2514. buf[1] = high_output; /* value */
  2515. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2516. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2517. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
  2518. LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
  2519. return ERROR_JTAG_INIT_FAILED;
  2520. }
  2521. return ERROR_OK;
  2522. }
  2523. static void icebear_jtag_reset(int trst, int srst) {
  2524. if (trst == 1) {
  2525. low_direction |= nTRST;
  2526. low_output &= ~nTRST;
  2527. }
  2528. else if (trst == 0) {
  2529. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2530. if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
  2531. low_direction &= ~nTRST;
  2532. else
  2533. low_output |= nTRST;
  2534. }
  2535. if (srst == 1) {
  2536. low_output &= ~nSRST;
  2537. }
  2538. else if (srst == 0) {
  2539. low_output |= nSRST;
  2540. }
  2541. /* command "set data bits low byte" */
  2542. buffer_write(0x80);
  2543. buffer_write(low_output);
  2544. buffer_write(low_direction);
  2545. LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
  2546. }