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.
 
 
 
 
 
 

2769 lines
69 KiB

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