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.
 
 
 
 
 
 

3014 lines
76 KiB

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