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.
 
 
 
 
 
 

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