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.
 
 
 
 
 
 

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