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.
 
 
 
 
 
 

3073 lines
80 KiB

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