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.
 
 
 
 
 
 

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