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.
 
 
 
 
 
 

1500 lines
36 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2004, 2006 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #if IS_CYGWIN == 1
  24. #include "windows.h"
  25. #undef ERROR
  26. #endif
  27. #include "replacements.h"
  28. /* project specific includes */
  29. #include "log.h"
  30. #include "types.h"
  31. #include "jtag.h"
  32. #include "configuration.h"
  33. #include "time_support.h"
  34. /* system includes */
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. /* FT2232 access library includes */
  39. #if BUILD_FT2232_FTD2XX == 1
  40. #include <ftd2xx.h>
  41. #elif BUILD_FT2232_LIBFTDI == 1
  42. #include <ftdi.h>
  43. #endif
  44. #include <sys/time.h>
  45. #include <time.h>
  46. /* enable this to debug io latency
  47. */
  48. #if 0
  49. #define _DEBUG_USB_IO_
  50. #endif
  51. /* enable this to debug communication
  52. */
  53. #if 0
  54. #define _DEBUG_USB_COMMS_
  55. #endif
  56. int ft2232_execute_queue(void);
  57. int ft2232_speed(int speed);
  58. int ft2232_register_commands(struct command_context_s *cmd_ctx);
  59. int ft2232_init(void);
  60. int ft2232_quit(void);
  61. int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  62. int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  63. int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  64. int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  65. char *ft2232_device_desc = NULL;
  66. char *ft2232_serial = NULL;
  67. char *ft2232_layout = NULL;
  68. u16 ft2232_vid = 0x0403;
  69. u16 ft2232_pid = 0x6010;
  70. typedef struct ft2232_layout_s
  71. {
  72. char* name;
  73. int(*init)(void);
  74. void(*reset)(int trst, int srst);
  75. void(*blink)(void);
  76. } ft2232_layout_t;
  77. /* init procedures for supported layouts */
  78. int usbjtag_init(void);
  79. int jtagkey_init(void);
  80. int olimex_jtag_init(void);
  81. int m5960_init(void);
  82. /* reset procedures for supported layouts */
  83. void usbjtag_reset(int trst, int srst);
  84. void jtagkey_reset(int trst, int srst);
  85. void olimex_jtag_reset(int trst, int srst);
  86. void m5960_reset(int trst, int srst);
  87. /* blink procedures for layouts that support a blinking led */
  88. void olimex_jtag_blink(void);
  89. ft2232_layout_t ft2232_layouts[] =
  90. {
  91. {"usbjtag", usbjtag_init, usbjtag_reset, NULL},
  92. {"jtagkey", jtagkey_init, jtagkey_reset, NULL},
  93. {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL},
  94. {"signalyzer", usbjtag_init, usbjtag_reset, NULL},
  95. {"olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink},
  96. {"m5960", m5960_init, m5960_reset, NULL},
  97. {NULL, NULL, NULL},
  98. };
  99. static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
  100. static ft2232_layout_t *layout;
  101. static u8 low_output = 0x0;
  102. static u8 low_direction = 0x0;
  103. static u8 high_output = 0x0;
  104. static u8 high_direction = 0x0;
  105. #if BUILD_FT2232_FTD2XX == 1
  106. static FT_HANDLE ftdih = NULL;
  107. #elif BUILD_FT2232_LIBFTDI == 1
  108. static struct ftdi_context ftdic;
  109. #endif
  110. static u8 *ft2232_buffer = NULL;
  111. static int ft2232_buffer_size = 0;
  112. static int ft2232_read_pointer = 0;
  113. static int ft2232_expect_read = 0;
  114. #define FT2232_BUFFER_SIZE 131072
  115. #define BUFFER_ADD ft2232_buffer[ft2232_buffer_size++]
  116. #define BUFFER_READ ft2232_buffer[ft2232_read_pointer++]
  117. jtag_interface_t ft2232_interface =
  118. {
  119. .name = "ft2232",
  120. .execute_queue = ft2232_execute_queue,
  121. .support_pathmove = 1,
  122. .speed = ft2232_speed,
  123. .register_commands = ft2232_register_commands,
  124. .init = ft2232_init,
  125. .quit = ft2232_quit,
  126. };
  127. int ft2232_write(u8 *buf, int size, u32* bytes_written)
  128. {
  129. #if BUILD_FT2232_FTD2XX == 1
  130. FT_STATUS status;
  131. DWORD dw_bytes_written;
  132. if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
  133. {
  134. *bytes_written = dw_bytes_written;
  135. ERROR("FT_Write returned: %i", status);
  136. return ERROR_JTAG_DEVICE_ERROR;
  137. }
  138. else
  139. {
  140. *bytes_written = dw_bytes_written;
  141. return ERROR_OK;
  142. }
  143. #elif BUILD_FT2232_LIBFTDI == 1
  144. int retval;
  145. if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
  146. {
  147. *bytes_written = 0;
  148. ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
  149. return ERROR_JTAG_DEVICE_ERROR;
  150. }
  151. else
  152. {
  153. *bytes_written = retval;
  154. return ERROR_OK;
  155. }
  156. #endif
  157. }
  158. int ft2232_read(u8* buf, int size, u32* bytes_read)
  159. {
  160. #if BUILD_FT2232_FTD2XX == 1
  161. DWORD dw_bytes_read;
  162. FT_STATUS status;
  163. if ((status = FT_Read(ftdih, buf, size, &dw_bytes_read)) != FT_OK)
  164. {
  165. *bytes_read = dw_bytes_read;
  166. ERROR("FT_Read returned: %i", status);
  167. return ERROR_JTAG_DEVICE_ERROR;
  168. }
  169. *bytes_read = dw_bytes_read;
  170. return ERROR_OK;
  171. #elif BUILD_FT2232_LIBFTDI == 1
  172. int retval;
  173. int timeout = 100;
  174. *bytes_read = 0;
  175. while ((*bytes_read < size) && timeout--)
  176. {
  177. if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
  178. {
  179. *bytes_read = 0;
  180. ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
  181. return ERROR_JTAG_DEVICE_ERROR;
  182. }
  183. *bytes_read += retval;
  184. }
  185. return ERROR_OK;
  186. #endif
  187. }
  188. int ft2232_speed(int speed)
  189. {
  190. u8 buf[3];
  191. int retval;
  192. u32 bytes_written;
  193. buf[0] = 0x86; /* command "set divisor" */
  194. buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
  195. buf[2] = (speed >> 8) & 0xff; /* valueH */
  196. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  197. if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  198. {
  199. ERROR("couldn't set FT2232 TCK speed");
  200. return retval;
  201. }
  202. return ERROR_OK;
  203. }
  204. int ft2232_register_commands(struct command_context_s *cmd_ctx)
  205. {
  206. register_command(cmd_ctx, NULL, "ft2232_device_desc", ft2232_handle_device_desc_command,
  207. COMMAND_CONFIG, NULL);
  208. register_command(cmd_ctx, NULL, "ft2232_serial", ft2232_handle_serial_command,
  209. COMMAND_CONFIG, NULL);
  210. register_command(cmd_ctx, NULL, "ft2232_layout", ft2232_handle_layout_command,
  211. COMMAND_CONFIG, NULL);
  212. register_command(cmd_ctx, NULL, "ft2232_vid_pid", ft2232_handle_vid_pid_command,
  213. COMMAND_CONFIG, NULL);
  214. return ERROR_OK;
  215. }
  216. void ft2232_end_state(state)
  217. {
  218. if (tap_move_map[state] != -1)
  219. end_state = state;
  220. else
  221. {
  222. ERROR("BUG: %i is not a valid end state", state);
  223. exit(-1);
  224. }
  225. }
  226. void ft2232_read_scan(enum scan_type type, u8* buffer, int scan_size)
  227. {
  228. int num_bytes = ((scan_size + 7) / 8);
  229. int bits_left = scan_size;
  230. int cur_byte = 0;
  231. while(num_bytes-- > 1)
  232. {
  233. buffer[cur_byte] = BUFFER_READ;
  234. cur_byte++;
  235. bits_left -= 8;
  236. }
  237. buffer[cur_byte] = 0x0;
  238. if (bits_left > 1)
  239. {
  240. buffer[cur_byte] = BUFFER_READ >> 1;
  241. }
  242. buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
  243. }
  244. void ft2232_debug_dump_buffer(void)
  245. {
  246. int i;
  247. char line[256];
  248. char *line_p = line;
  249. for (i = 0; i < ft2232_buffer_size; i++)
  250. {
  251. line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
  252. if (i % 16 == 15)
  253. {
  254. DEBUG("%s", line);
  255. line_p = line;
  256. }
  257. }
  258. if (line_p != line)
  259. DEBUG("%s", line);
  260. }
  261. int ft2232_send_and_recv(jtag_command_t *first, jtag_command_t *last)
  262. {
  263. jtag_command_t *cmd;
  264. u8 *buffer;
  265. int scan_size;
  266. enum scan_type type;
  267. int retval;
  268. u32 bytes_written;
  269. u32 bytes_read;
  270. #ifdef _DEBUG_USB_IO_
  271. struct timeval start, inter, inter2, end;
  272. struct timeval d_inter, d_inter2, d_end;
  273. #endif
  274. #ifdef _DEBUG_USB_COMMS_
  275. DEBUG("write buffer (size %i):", ft2232_buffer_size);
  276. ft2232_debug_dump_buffer();
  277. #endif
  278. #ifdef _DEBUG_USB_IO_
  279. gettimeofday(&start, NULL);
  280. #endif
  281. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  282. {
  283. ERROR("couldn't write MPSSE commands to FT2232");
  284. exit(-1);
  285. }
  286. #ifdef _DEBUG_USB_IO_
  287. gettimeofday(&inter, NULL);
  288. #endif
  289. if (ft2232_expect_read)
  290. {
  291. int timeout = 100;
  292. ft2232_buffer_size = 0;
  293. #ifdef _DEBUG_USB_IO_
  294. gettimeofday(&inter2, NULL);
  295. #endif
  296. if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
  297. {
  298. ERROR("couldn't read from FT2232");
  299. exit(-1);
  300. }
  301. #ifdef _DEBUG_USB_IO_
  302. gettimeofday(&end, NULL);
  303. timeval_subtract(&d_inter, &inter, &start);
  304. timeval_subtract(&d_inter2, &inter2, &start);
  305. timeval_subtract(&d_end, &end, &start);
  306. INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
  307. #endif
  308. ft2232_buffer_size = bytes_read;
  309. if (ft2232_expect_read != ft2232_buffer_size)
  310. {
  311. ERROR("ft2232_expect_read (%i) != ft2232_buffer_size (%i) (%i retries)", ft2232_expect_read, ft2232_buffer_size, 100 - timeout);
  312. ft2232_debug_dump_buffer();
  313. exit(-1);
  314. }
  315. #ifdef _DEBUG_USB_COMMS_
  316. DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ft2232_buffer_size);
  317. ft2232_debug_dump_buffer();
  318. #endif
  319. }
  320. ft2232_expect_read = 0;
  321. ft2232_read_pointer = 0;
  322. cmd = first;
  323. while (cmd != last)
  324. {
  325. switch (cmd->type)
  326. {
  327. case JTAG_SCAN:
  328. type = jtag_scan_type(cmd->cmd.scan);
  329. if (type != SCAN_OUT)
  330. {
  331. scan_size = jtag_scan_size(cmd->cmd.scan);
  332. buffer = calloc(CEIL(scan_size, 8), 1);
  333. ft2232_read_scan(type, buffer, scan_size);
  334. jtag_read_buffer(buffer, cmd->cmd.scan);
  335. free(buffer);
  336. }
  337. break;
  338. default:
  339. break;
  340. }
  341. cmd = cmd->next;
  342. }
  343. ft2232_buffer_size = 0;
  344. return ERROR_OK;
  345. }
  346. void ft2232_add_pathmove(pathmove_command_t *cmd)
  347. {
  348. int num_states = cmd->num_states;
  349. u8 tms_byte;
  350. int state_count;
  351. state_count = 0;
  352. while (num_states)
  353. {
  354. tms_byte = 0x0;
  355. int bit_count = 0;
  356. /* command "Clock Data to TMS/CS Pin (no Read)" */
  357. BUFFER_ADD = 0x4b;
  358. /* number of states remaining */
  359. BUFFER_ADD = (num_states % 7) - 1;
  360. while (num_states % 7)
  361. {
  362. if (tap_transitions[cur_state].low == cmd->path[state_count])
  363. buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
  364. else if (tap_transitions[cur_state].high == cmd->path[state_count])
  365. buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
  366. else
  367. {
  368. ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
  369. exit(-1);
  370. }
  371. cur_state = cmd->path[state_count];
  372. state_count++;
  373. num_states--;
  374. }
  375. BUFFER_ADD = tms_byte;
  376. }
  377. end_state = cur_state;
  378. }
  379. void ft2232_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  380. {
  381. int num_bytes = (scan_size + 7) / 8;
  382. int bits_left = scan_size;
  383. int cur_byte = 0;
  384. int last_bit;
  385. if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
  386. {
  387. /* command "Clock Data to TMS/CS Pin (no Read)" */
  388. BUFFER_ADD = 0x4b;
  389. /* scan 7 bit */
  390. BUFFER_ADD = 0x6;
  391. /* TMS data bits */
  392. if (ir_scan)
  393. {
  394. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
  395. cur_state = TAP_SI;
  396. }
  397. else
  398. {
  399. BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
  400. cur_state = TAP_SD;
  401. }
  402. //DEBUG("added TMS scan (no read)");
  403. }
  404. /* add command for complete bytes */
  405. if (num_bytes > 1)
  406. {
  407. if (type == SCAN_IO)
  408. {
  409. /* Clock Data Bytes In and Out LSB First */
  410. BUFFER_ADD = 0x39;
  411. //DEBUG("added TDI bytes (io %i)", num_bytes);
  412. }
  413. else if (type == SCAN_OUT)
  414. {
  415. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  416. BUFFER_ADD = 0x19;
  417. //DEBUG("added TDI bytes (o)");
  418. }
  419. else if (type == SCAN_IN)
  420. {
  421. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  422. BUFFER_ADD = 0x28;
  423. //DEBUG("added TDI bytes (i %i)", num_bytes);
  424. }
  425. BUFFER_ADD = (num_bytes-2) & 0xff;
  426. BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
  427. }
  428. if (type != SCAN_IN)
  429. {
  430. /* add complete bytes */
  431. while(num_bytes-- > 1)
  432. {
  433. BUFFER_ADD = buffer[cur_byte];
  434. cur_byte++;
  435. bits_left -= 8;
  436. }
  437. }
  438. if (type == SCAN_IN)
  439. {
  440. bits_left -= 8 * (num_bytes - 1);
  441. }
  442. /* the most signifcant bit is scanned during TAP movement */
  443. if (type != SCAN_IN)
  444. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  445. else
  446. last_bit = 0;
  447. /* process remaining bits but the last one */
  448. if (bits_left > 1)
  449. {
  450. if (type == SCAN_IO)
  451. {
  452. /* Clock Data Bits In and Out LSB First */
  453. BUFFER_ADD = 0x3b;
  454. //DEBUG("added TDI bits (io) %i", bits_left - 1);
  455. }
  456. else if (type == SCAN_OUT)
  457. {
  458. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  459. BUFFER_ADD = 0x1b;
  460. //DEBUG("added TDI bits (o)");
  461. }
  462. else if (type == SCAN_IN)
  463. {
  464. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  465. BUFFER_ADD = 0x2a;
  466. //DEBUG("added TDI bits (i %i)", bits_left - 1);
  467. }
  468. BUFFER_ADD = bits_left - 2;
  469. if (type != SCAN_IN)
  470. BUFFER_ADD = buffer[cur_byte];
  471. }
  472. /* move from Shift-IR/DR to end state */
  473. if (type != SCAN_OUT)
  474. {
  475. /* Clock Data to TMS/CS Pin with Read */
  476. BUFFER_ADD = 0x6b;
  477. //DEBUG("added TMS scan (read)");
  478. }
  479. else
  480. {
  481. /* Clock Data to TMS/CS Pin (no Read) */
  482. BUFFER_ADD = 0x4b;
  483. //DEBUG("added TMS scan (no read)");
  484. }
  485. BUFFER_ADD = 0x6;
  486. BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
  487. cur_state = end_state;
  488. }
  489. int ft2232_predict_scan_out(int scan_size, enum scan_type type)
  490. {
  491. int predicted_size = 3;
  492. if (cur_state != TAP_SD)
  493. predicted_size += 3;
  494. if (type == SCAN_IN) /* only from device to host */
  495. {
  496. /* complete bytes */
  497. predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
  498. /* remaining bits - 1 (up to 7) */
  499. predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
  500. }
  501. else /* host to device, or bidirectional */
  502. {
  503. /* complete bytes */
  504. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
  505. /* remaining bits -1 (up to 7) */
  506. predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
  507. }
  508. return predicted_size;
  509. }
  510. int ft2232_predict_scan_in(int scan_size, enum scan_type type)
  511. {
  512. int predicted_size = 0;
  513. if (type != SCAN_OUT)
  514. {
  515. /* complete bytes */
  516. predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
  517. /* remaining bits - 1 */
  518. predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
  519. /* last bit (from TMS scan) */
  520. predicted_size += 1;
  521. }
  522. //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
  523. return predicted_size;
  524. }
  525. void usbjtag_reset(int trst, int srst)
  526. {
  527. if (trst == 1)
  528. {
  529. cur_state = TAP_TLR;
  530. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  531. low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
  532. else
  533. low_output &= ~nTRST; /* switch output low */
  534. }
  535. else if (trst == 0)
  536. {
  537. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  538. low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
  539. else
  540. low_output |= nTRST; /* switch output high */
  541. }
  542. if (srst == 1)
  543. {
  544. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  545. low_output &= ~nSRST; /* switch output low */
  546. else
  547. low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
  548. }
  549. else if (srst == 0)
  550. {
  551. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  552. low_output |= nSRST; /* switch output high */
  553. else
  554. low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
  555. }
  556. /* command "set data bits low byte" */
  557. BUFFER_ADD = 0x80;
  558. BUFFER_ADD = low_output;
  559. BUFFER_ADD = low_direction;
  560. }
  561. void jtagkey_reset(int trst, int srst)
  562. {
  563. if (trst == 1)
  564. {
  565. cur_state = TAP_TLR;
  566. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  567. high_output &= ~nTRSTnOE;
  568. else
  569. high_output &= ~nTRST;
  570. }
  571. else if (trst == 0)
  572. {
  573. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  574. high_output |= nTRSTnOE;
  575. else
  576. high_output |= nTRST;
  577. }
  578. if (srst == 1)
  579. {
  580. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  581. high_output &= ~nSRST;
  582. else
  583. high_output &= ~nSRSTnOE;
  584. }
  585. else if (srst == 0)
  586. {
  587. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  588. high_output |= nSRST;
  589. else
  590. high_output |= nSRSTnOE;
  591. }
  592. /* command "set data bits high byte" */
  593. BUFFER_ADD = 0x82;
  594. BUFFER_ADD = high_output;
  595. BUFFER_ADD = high_direction;
  596. DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  597. }
  598. void olimex_jtag_reset(int trst, int srst)
  599. {
  600. if (trst == 1)
  601. {
  602. cur_state = TAP_TLR;
  603. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  604. high_output &= ~nTRSTnOE;
  605. else
  606. high_output &= ~nTRST;
  607. }
  608. else if (trst == 0)
  609. {
  610. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  611. high_output |= nTRSTnOE;
  612. else
  613. high_output |= nTRST;
  614. }
  615. if (srst == 1)
  616. {
  617. high_output |= nSRST;
  618. }
  619. else if (srst == 0)
  620. {
  621. high_output &= ~nSRST;
  622. }
  623. /* command "set data bits high byte" */
  624. BUFFER_ADD = 0x82;
  625. BUFFER_ADD = high_output;
  626. BUFFER_ADD = high_direction;
  627. DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  628. }
  629. void m5960_reset(int trst, int srst)
  630. {
  631. if (trst == 1)
  632. {
  633. cur_state = TAP_TLR;
  634. low_output &= ~nTRST;
  635. }
  636. else if (trst == 0)
  637. {
  638. low_output |= nTRST;
  639. }
  640. if (srst == 1)
  641. {
  642. low_output |= nSRST;
  643. }
  644. else if (srst == 0)
  645. {
  646. low_output &= ~nSRST;
  647. }
  648. /* command "set data bits low byte" */
  649. BUFFER_ADD = 0x80;
  650. BUFFER_ADD = low_output;
  651. BUFFER_ADD = low_direction;
  652. DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  653. }
  654. int ft2232_execute_queue()
  655. {
  656. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  657. jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
  658. u8 *buffer;
  659. int scan_size; /* size of IR or DR scan */
  660. enum scan_type type;
  661. int i;
  662. int predicted_size = 0;
  663. int require_send = 0;
  664. ft2232_buffer_size = 0;
  665. ft2232_expect_read = 0;
  666. /* blink, if the current layout has that feature */
  667. if (layout->blink)
  668. layout->blink();
  669. while (cmd)
  670. {
  671. switch(cmd->type)
  672. {
  673. case JTAG_END_STATE:
  674. if (cmd->cmd.end_state->end_state != -1)
  675. ft2232_end_state(cmd->cmd.end_state->end_state);
  676. break;
  677. case JTAG_RESET:
  678. /* only send the maximum buffer size that FT2232C can handle */
  679. predicted_size = 3;
  680. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  681. {
  682. ft2232_send_and_recv(first_unsent, cmd);
  683. require_send = 0;
  684. first_unsent = cmd;
  685. }
  686. layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  687. require_send = 1;
  688. #ifdef _DEBUG_JTAG_IO_
  689. DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  690. #endif
  691. break;
  692. case JTAG_RUNTEST:
  693. /* only send the maximum buffer size that FT2232C can handle */
  694. predicted_size = 0;
  695. if (cur_state != TAP_RTI)
  696. predicted_size += 3;
  697. predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
  698. if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
  699. predicted_size += 3;
  700. if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
  701. predicted_size += 3;
  702. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  703. {
  704. ft2232_send_and_recv(first_unsent, cmd);
  705. require_send = 0;
  706. first_unsent = cmd;
  707. }
  708. if (cur_state != TAP_RTI)
  709. {
  710. /* command "Clock Data to TMS/CS Pin (no Read)" */
  711. BUFFER_ADD = 0x4b;
  712. /* scan 7 bit */
  713. BUFFER_ADD = 0x6;
  714. /* TMS data bits */
  715. BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
  716. cur_state = TAP_RTI;
  717. require_send = 1;
  718. }
  719. i = cmd->cmd.runtest->num_cycles;
  720. while (i > 0)
  721. {
  722. /* command "Clock Data to TMS/CS Pin (no Read)" */
  723. BUFFER_ADD = 0x4b;
  724. /* scan 7 bit */
  725. BUFFER_ADD = (i > 7) ? 6 : (i - 1);
  726. /* TMS data bits */
  727. BUFFER_ADD = 0x0;
  728. cur_state = TAP_RTI;
  729. i -= (i > 7) ? 7 : i;
  730. //DEBUG("added TMS scan (no read)");
  731. }
  732. if (cmd->cmd.runtest->end_state != -1)
  733. ft2232_end_state(cmd->cmd.runtest->end_state);
  734. if (cur_state != end_state)
  735. {
  736. /* command "Clock Data to TMS/CS Pin (no Read)" */
  737. BUFFER_ADD = 0x4b;
  738. /* scan 7 bit */
  739. BUFFER_ADD = 0x6;
  740. /* TMS data bits */
  741. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  742. cur_state = end_state;
  743. //DEBUG("added TMS scan (no read)");
  744. }
  745. require_send = 1;
  746. #ifdef _DEBUG_JTAG_IO_
  747. DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
  748. #endif
  749. break;
  750. case JTAG_STATEMOVE:
  751. /* only send the maximum buffer size that FT2232C can handle */
  752. predicted_size = 3;
  753. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  754. {
  755. ft2232_send_and_recv(first_unsent, cmd);
  756. require_send = 0;
  757. first_unsent = cmd;
  758. }
  759. if (cmd->cmd.statemove->end_state != -1)
  760. ft2232_end_state(cmd->cmd.statemove->end_state);
  761. /* command "Clock Data to TMS/CS Pin (no Read)" */
  762. BUFFER_ADD = 0x4b;
  763. /* scan 7 bit */
  764. BUFFER_ADD = 0x6;
  765. /* TMS data bits */
  766. BUFFER_ADD = TAP_MOVE(cur_state, end_state);
  767. //DEBUG("added TMS scan (no read)");
  768. cur_state = end_state;
  769. require_send = 1;
  770. #ifdef _DEBUG_JTAG_IO_
  771. DEBUG("statemove: %i", end_state);
  772. #endif
  773. break;
  774. case JTAG_PATHMOVE:
  775. /* only send the maximum buffer size that FT2232C can handle */
  776. predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
  777. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  778. {
  779. ft2232_send_and_recv(first_unsent, cmd);
  780. require_send = 0;
  781. first_unsent = cmd;
  782. }
  783. ft2232_add_pathmove(cmd->cmd.pathmove);
  784. require_send = 1;
  785. #ifdef _DEBUG_JTAG_IO_
  786. DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  787. #endif
  788. break;
  789. case JTAG_SCAN:
  790. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  791. type = jtag_scan_type(cmd->cmd.scan);
  792. predicted_size = ft2232_predict_scan_out(scan_size, type);
  793. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  794. {
  795. DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
  796. ft2232_send_and_recv(first_unsent, cmd);
  797. require_send = 0;
  798. first_unsent = cmd;
  799. }
  800. ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
  801. //DEBUG("new read size: %i", ft2232_expect_read);
  802. if (cmd->cmd.scan->end_state != -1)
  803. ft2232_end_state(cmd->cmd.scan->end_state);
  804. ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  805. require_send = 1;
  806. if (buffer)
  807. free(buffer);
  808. #ifdef _DEBUG_JTAG_IO_
  809. DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
  810. #endif
  811. break;
  812. case JTAG_SLEEP:
  813. ft2232_send_and_recv(first_unsent, cmd);
  814. first_unsent = cmd->next;
  815. jtag_sleep(cmd->cmd.sleep->us);
  816. #ifdef _DEBUG_JTAG_IO_
  817. DEBUG("sleep %i usec", cmd->cmd.sleep->us);
  818. #endif
  819. break;
  820. default:
  821. ERROR("BUG: unknown JTAG command type encountered");
  822. exit(-1);
  823. }
  824. cmd = cmd->next;
  825. }
  826. if (require_send > 0)
  827. ft2232_send_and_recv(first_unsent, cmd);
  828. return ERROR_OK;
  829. }
  830. int ft2232_init(void)
  831. {
  832. u8 latency_timer;
  833. u8 buf[1];
  834. int retval;
  835. u32 bytes_written;
  836. #if BUILD_FT2232_FTD2XX == 1
  837. FT_STATUS status;
  838. DWORD openex_flags = 0;
  839. char *openex_string = NULL;
  840. #endif
  841. ft2232_layout_t *cur_layout = ft2232_layouts;
  842. if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
  843. {
  844. ft2232_layout = "usbjtag";
  845. WARNING("No ft2232 layout specified, using default 'usbjtag'");
  846. }
  847. while (cur_layout->name)
  848. {
  849. if (strcmp(cur_layout->name, ft2232_layout) == 0)
  850. {
  851. layout = cur_layout;
  852. break;
  853. }
  854. cur_layout++;
  855. }
  856. if (!layout)
  857. {
  858. ERROR("No matching layout found for %s", ft2232_layout);
  859. return ERROR_JTAG_INIT_FAILED;
  860. }
  861. #if BUILD_FT2232_FTD2XX == 1
  862. DEBUG("'ft2232' interface using FTD2XX with '%s' layout", ft2232_layout);
  863. #elif BUILD_FT2232_LIBFTDI == 1
  864. DEBUG("'ft2232' interface using libftdi with '%s' layout", ft2232_layout);
  865. #endif
  866. #if BUILD_FT2232_FTD2XX == 1
  867. #if IS_WIN32 == 0
  868. /* Add non-standard Vid/Pid to the linux driver */
  869. if ((status = FT_SetVIDPID(ft2232_vid, ft2232_pid)) != FT_OK)
  870. {
  871. WARNING("couldn't add %4.4x:%4.4x", ft2232_vid, ft2232_pid);
  872. }
  873. #endif
  874. if (ft2232_device_desc && ft2232_serial)
  875. {
  876. WARNING("can't open by device description and serial number, giving precedence to serial");
  877. ft2232_device_desc = NULL;
  878. }
  879. if (ft2232_device_desc)
  880. {
  881. openex_string = ft2232_device_desc;
  882. openex_flags = FT_OPEN_BY_DESCRIPTION;
  883. }
  884. else if (ft2232_serial)
  885. {
  886. openex_string = ft2232_serial;
  887. openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
  888. }
  889. else
  890. {
  891. ERROR("neither device description nor serial number specified");
  892. ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
  893. return ERROR_JTAG_INIT_FAILED;
  894. }
  895. if ((status = FT_OpenEx(openex_string, openex_flags, &ftdih)) != FT_OK)
  896. {
  897. DWORD num_devices;
  898. ERROR("unable to open ftdi device: %i", status);
  899. status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
  900. if (status == FT_OK)
  901. {
  902. char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
  903. int i;
  904. for (i = 0; i < num_devices; i++)
  905. desc_array[i] = malloc(64);
  906. desc_array[num_devices] = NULL;
  907. status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
  908. if (status == FT_OK)
  909. {
  910. ERROR("ListDevices: %d\n", num_devices);
  911. for (i = 0; i < num_devices; i++)
  912. ERROR("%i: %s", i, desc_array[i]);
  913. }
  914. for (i = 0; i < num_devices; i++)
  915. free(desc_array[i]);
  916. free(desc_array);
  917. }
  918. else
  919. {
  920. printf("ListDevices: NONE\n");
  921. }
  922. return ERROR_JTAG_INIT_FAILED;
  923. }
  924. if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
  925. {
  926. ERROR("unable to set latency timer: %i", status);
  927. return ERROR_JTAG_INIT_FAILED;
  928. }
  929. if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
  930. {
  931. ERROR("unable to get latency timer: %i", status);
  932. return ERROR_JTAG_INIT_FAILED;
  933. }
  934. else
  935. {
  936. DEBUG("current latency timer: %i", latency_timer);
  937. }
  938. if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
  939. {
  940. ERROR("unable to set timeouts: %i", status);
  941. return ERROR_JTAG_INIT_FAILED;
  942. }
  943. if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
  944. {
  945. ERROR("unable to enable bit i/o mode: %i", status);
  946. return ERROR_JTAG_INIT_FAILED;
  947. }
  948. #elif BUILD_FT2232_LIBFTDI == 1
  949. if (ftdi_init(&ftdic) < 0)
  950. return ERROR_JTAG_INIT_FAILED;
  951. /* context, vendor id, product id */
  952. if (ftdi_usb_open(&ftdic, ft2232_vid, ft2232_pid) < 0)
  953. {
  954. ERROR("unable to open ftdi device: %s", ftdic.error_str);
  955. return ERROR_JTAG_INIT_FAILED;
  956. }
  957. if (ftdi_usb_reset(&ftdic) < 0)
  958. {
  959. ERROR("unable to reset ftdi device");
  960. return ERROR_JTAG_INIT_FAILED;
  961. }
  962. if (ftdi_set_latency_timer(&ftdic, 2) < 0)
  963. {
  964. ERROR("unable to set latency timer");
  965. return ERROR_JTAG_INIT_FAILED;
  966. }
  967. if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
  968. {
  969. ERROR("unable to get latency timer");
  970. return ERROR_JTAG_INIT_FAILED;
  971. }
  972. else
  973. {
  974. DEBUG("current latency timer: %i", latency_timer);
  975. }
  976. ftdic.bitbang_mode = 0; /* Reset controller */
  977. ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
  978. ftdic.bitbang_mode = 2; /* MPSSE mode */
  979. ftdi_enable_bitbang(&ftdic, 0x0b); /* ctx, JTAG I/O mask */
  980. #endif
  981. ft2232_buffer_size = 0;
  982. ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
  983. if (layout->init() != ERROR_OK)
  984. return ERROR_JTAG_INIT_FAILED;
  985. ft2232_speed(jtag_speed);
  986. buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
  987. if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
  988. {
  989. ERROR("couldn't write to FT2232 to disable loopback");
  990. return ERROR_JTAG_INIT_FAILED;
  991. }
  992. #if BUILD_FT2232_FTD2XX == 1
  993. if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
  994. {
  995. ERROR("error purging ftd2xx device: %i", status);
  996. return ERROR_JTAG_INIT_FAILED;
  997. }
  998. #elif BUILD_FT2232_LIBFTDI == 1
  999. if (ftdi_usb_purge_buffers(&ftdic) < 0)
  1000. {
  1001. ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  1002. return ERROR_JTAG_INIT_FAILED;
  1003. }
  1004. #endif
  1005. return ERROR_OK;
  1006. }
  1007. int usbjtag_init(void)
  1008. {
  1009. u8 buf[3];
  1010. u32 bytes_written;
  1011. low_output = 0x08;
  1012. low_direction = 0x0b;
  1013. if (strcmp(ft2232_layout, "usbjtag") == 0)
  1014. {
  1015. nTRST = 0x10;
  1016. nTRSTnOE = 0x10;
  1017. nSRST = 0x40;
  1018. nSRSTnOE = 0x40;
  1019. }
  1020. else if (strcmp(ft2232_layout, "signalyzer") == 0)
  1021. {
  1022. nTRST = 0x10;
  1023. nTRSTnOE = 0x10;
  1024. nSRST = 0x20;
  1025. nSRSTnOE = 0x20;
  1026. }
  1027. else
  1028. {
  1029. ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
  1030. return ERROR_JTAG_INIT_FAILED;
  1031. }
  1032. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1033. {
  1034. low_direction &= ~nTRSTnOE; /* nTRST input */
  1035. low_output &= ~nTRST; /* nTRST = 0 */
  1036. }
  1037. else
  1038. {
  1039. low_direction |= nTRSTnOE; /* nTRST output */
  1040. low_output |= nTRST; /* nTRST = 1 */
  1041. }
  1042. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1043. {
  1044. low_direction |= nSRSTnOE; /* nSRST output */
  1045. low_output |= nSRST; /* nSRST = 1 */
  1046. }
  1047. else
  1048. {
  1049. low_direction &= ~nSRSTnOE; /* nSRST input */
  1050. low_output &= ~nSRST; /* nSRST = 0 */
  1051. }
  1052. /* initialize low byte for jtag */
  1053. buf[0] = 0x80; /* command "set data bits low byte" */
  1054. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
  1055. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
  1056. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1057. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1058. {
  1059. ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
  1060. return ERROR_JTAG_INIT_FAILED;
  1061. }
  1062. return ERROR_OK;
  1063. }
  1064. int jtagkey_init(void)
  1065. {
  1066. u8 buf[3];
  1067. u32 bytes_written;
  1068. low_output = 0x08;
  1069. low_direction = 0x1b;
  1070. /* initialize low byte for jtag */
  1071. buf[0] = 0x80; /* command "set data bits low byte" */
  1072. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1073. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
  1074. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1075. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1076. {
  1077. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1078. return ERROR_JTAG_INIT_FAILED;
  1079. }
  1080. if (strcmp(layout->name, "jtagkey") == 0)
  1081. {
  1082. nTRST = 0x01;
  1083. nTRSTnOE = 0x4;
  1084. nSRST = 0x02;
  1085. nSRSTnOE = 0x08;
  1086. }
  1087. else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
  1088. {
  1089. nTRST = 0x02;
  1090. nTRSTnOE = 0x1;
  1091. nSRST = 0x08;
  1092. nSRSTnOE = 0x04;
  1093. }
  1094. else
  1095. {
  1096. ERROR("BUG: jtagkey_init called for non jtagkey layout");
  1097. exit(-1);
  1098. }
  1099. high_output = 0x0;
  1100. high_direction = 0x0f;
  1101. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1102. {
  1103. high_output |= nTRSTnOE;
  1104. high_output &= ~nTRST;
  1105. }
  1106. else
  1107. {
  1108. high_output &= ~nTRSTnOE;
  1109. high_output |= nTRST;
  1110. }
  1111. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1112. {
  1113. high_output &= ~nSRSTnOE;
  1114. high_output |= nSRST;
  1115. }
  1116. else
  1117. {
  1118. high_output |= nSRSTnOE;
  1119. high_output &= ~nSRST;
  1120. }
  1121. /* initialize high port */
  1122. buf[0] = 0x82; /* command "set data bits high byte" */
  1123. buf[1] = high_output; /* value */
  1124. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1125. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1126. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1127. {
  1128. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1129. return ERROR_JTAG_INIT_FAILED;
  1130. }
  1131. return ERROR_OK;
  1132. }
  1133. int olimex_jtag_init(void)
  1134. {
  1135. u8 buf[3];
  1136. u32 bytes_written;
  1137. low_output = 0x08;
  1138. low_direction = 0x1b;
  1139. /* initialize low byte for jtag */
  1140. buf[0] = 0x80; /* command "set data bits low byte" */
  1141. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1142. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
  1143. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1144. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1145. {
  1146. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1147. return ERROR_JTAG_INIT_FAILED;
  1148. }
  1149. nTRST = 0x01;
  1150. nTRSTnOE = 0x4;
  1151. nSRST = 0x02;
  1152. nSRSTnOE = 0x00; /* no output enable for nSRST */
  1153. high_output = 0x0;
  1154. high_direction = 0x0f;
  1155. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1156. {
  1157. high_output |= nTRSTnOE;
  1158. high_output &= ~nTRST;
  1159. }
  1160. else
  1161. {
  1162. high_output &= ~nTRSTnOE;
  1163. high_output |= nTRST;
  1164. }
  1165. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1166. {
  1167. ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
  1168. }
  1169. else
  1170. {
  1171. high_output &= ~nSRST;
  1172. }
  1173. /* turn red LED on */
  1174. high_output |= 0x08;
  1175. /* initialize high port */
  1176. buf[0] = 0x82; /* command "set data bits high byte" */
  1177. buf[1] = high_output; /* value */
  1178. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1179. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1180. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1181. {
  1182. ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1183. return ERROR_JTAG_INIT_FAILED;
  1184. }
  1185. return ERROR_OK;
  1186. }
  1187. int m5960_init(void)
  1188. {
  1189. u8 buf[3];
  1190. u32 bytes_written;
  1191. low_output = 0x18;
  1192. low_direction = 0xfb;
  1193. /* initialize low byte for jtag */
  1194. buf[0] = 0x80; /* command "set data bits low byte" */
  1195. buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
  1196. buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE[12]=out, n[ST]srst=out */
  1197. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1198. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1199. {
  1200. ERROR("couldn't initialize FT2232 with 'm5960' layout");
  1201. return ERROR_JTAG_INIT_FAILED;
  1202. }
  1203. nTRST = 0x10;
  1204. nTRSTnOE = 0x0; /* not output enable for nTRST */
  1205. nSRST = 0x20;
  1206. nSRSTnOE = 0x00; /* no output enable for nSRST */
  1207. high_output = 0x00;
  1208. high_direction = 0x0c;
  1209. /* turn red LED1 on, LED2 off */
  1210. high_output |= 0x08;
  1211. /* initialize high port */
  1212. buf[0] = 0x82; /* command "set data bits high byte" */
  1213. buf[1] = high_output; /* value */
  1214. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  1215. DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1216. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1217. {
  1218. ERROR("couldn't initialize FT2232 with 'm5960' layout");
  1219. return ERROR_JTAG_INIT_FAILED;
  1220. }
  1221. return ERROR_OK;
  1222. }
  1223. void olimex_jtag_blink(void)
  1224. {
  1225. /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
  1226. * ACBUS3 is bit 3 of the GPIOH port
  1227. */
  1228. if (high_output & 0x08)
  1229. {
  1230. /* set port pin high */
  1231. high_output &= 0x07;
  1232. }
  1233. else
  1234. {
  1235. /* set port pin low */
  1236. high_output |= 0x08;
  1237. }
  1238. BUFFER_ADD = 0x82;
  1239. BUFFER_ADD = high_output;
  1240. BUFFER_ADD = high_direction;
  1241. }
  1242. int ft2232_quit(void)
  1243. {
  1244. #if BUILD_FT2232_FTD2XX == 1
  1245. FT_STATUS status;
  1246. status = FT_Close(ftdih);
  1247. #elif BUILD_FT2232_LIBFTDI == 1
  1248. ftdi_disable_bitbang(&ftdic);
  1249. ftdi_usb_close(&ftdic);
  1250. ftdi_deinit(&ftdic);
  1251. #endif
  1252. free(ft2232_buffer);
  1253. return ERROR_OK;
  1254. }
  1255. int ft2232_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1256. {
  1257. if (argc == 1)
  1258. {
  1259. ft2232_device_desc = strdup(args[0]);
  1260. }
  1261. else
  1262. {
  1263. ERROR("expected exactly one argument to ft2232_device_desc <description>");
  1264. }
  1265. return ERROR_OK;
  1266. }
  1267. int ft2232_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1268. {
  1269. if (argc == 1)
  1270. {
  1271. ft2232_serial = strdup(args[0]);
  1272. }
  1273. else
  1274. {
  1275. ERROR("expected exactly one argument to ft2232_serial <serial-number>");
  1276. }
  1277. return ERROR_OK;
  1278. }
  1279. int ft2232_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1280. {
  1281. if (argc == 0)
  1282. return ERROR_OK;
  1283. ft2232_layout = malloc(strlen(args[0]) + 1);
  1284. strcpy(ft2232_layout, args[0]);
  1285. return ERROR_OK;
  1286. }
  1287. int ft2232_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1288. {
  1289. if (argc >= 2)
  1290. {
  1291. ft2232_vid = strtol(args[0], NULL, 0);
  1292. ft2232_pid = strtol(args[1], NULL, 0);
  1293. }
  1294. else
  1295. {
  1296. WARNING("incomplete ft2232_vid_pid configuration directive");
  1297. }
  1298. return ERROR_OK;
  1299. }