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.
 
 
 
 
 
 

4057 lines
105 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by Øyvind Harboe *
  3. * Øyvind Harboe <oyvind.harboe@zylin.com> *
  4. * *
  5. * Copyright (C) 2009 by SoftPLC Corporation. http://softplc.com *
  6. * Dick Hollenbeck <dick@softplc.com> *
  7. * *
  8. * Copyright (C) 2004, 2006 by Dominic Rath *
  9. * Dominic.Rath@gmx.de *
  10. * *
  11. * Copyright (C) 2008 by Spencer Oliver *
  12. * spen@spen-soft.co.uk *
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. * This program is distributed in the hope that it will be useful, *
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  22. * GNU General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU General Public License *
  25. * along with this program; if not, write to the *
  26. * Free Software Foundation, Inc., *
  27. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  28. ***************************************************************************/
  29. /**
  30. * @file
  31. * JTAG adapters based on the FT2232 full and high speed USB parts are
  32. * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
  33. * are discrete, but development boards may integrate them as alternatives
  34. * to more capable (and expensive) third party JTAG pods. Since JTAG uses
  35. * only one of the two ports on these devices, on integrated boards the
  36. * second port often serves as a USB-to-serial adapter for the target's
  37. * console UART even when the JTAG port is not in use. (Systems which
  38. * support ARM's SWD in addition to JTAG, or instead of it, may use that
  39. * second port for reading SWV trace data.)
  40. *
  41. * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
  42. * request/response interactions involve round trips over the USB link.
  43. * A "smart" JTAG adapter has intelligence close to the scan chain, so it
  44. * can for example poll quickly for a status change (usually taking on the
  45. * order of microseconds not milliseconds) before beginning a queued
  46. * transaction which require the previous one to have completed.
  47. *
  48. * There are dozens of adapters of this type, differing in details which
  49. * this driver needs to understand. Those "layout" details are required
  50. * as part of FT2232 driver configuration.
  51. *
  52. * This code uses information contained in the MPSSE specification which was
  53. * found here:
  54. * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
  55. * Hereafter this is called the "MPSSE Spec".
  56. *
  57. * The datasheet for the ftdichip.com's FT2232D part is here:
  58. * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
  59. *
  60. * Also note the issue with code 0x4b (clock data to TMS) noted in
  61. * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
  62. * which can affect longer JTAG state paths.
  63. */
  64. #ifdef HAVE_CONFIG_H
  65. #include "config.h"
  66. #endif
  67. /* project specific includes */
  68. #include <jtag/interface.h>
  69. #include <helper/time_support.h>
  70. #if IS_CYGWIN == 1
  71. #include <windows.h>
  72. #endif
  73. #include <assert.h>
  74. #if (BUILD_FT2232_FTD2XX == 1 && BUILD_FT2232_LIBFTDI == 1)
  75. #error "BUILD_FT2232_FTD2XX && BUILD_FT2232_LIBFTDI are mutually exclusive"
  76. #elif (BUILD_FT2232_FTD2XX != 1 && BUILD_FT2232_LIBFTDI != 1)
  77. #error "BUILD_FT2232_FTD2XX || BUILD_FT2232_LIBFTDI must be chosen"
  78. #endif
  79. /* FT2232 access library includes */
  80. #if BUILD_FT2232_FTD2XX == 1
  81. #include <ftd2xx.h>
  82. #elif BUILD_FT2232_LIBFTDI == 1
  83. #include <ftdi.h>
  84. #endif
  85. /* max TCK for the high speed devices 30000 kHz */
  86. #define FTDI_2232H_4232H_MAX_TCK 30000
  87. /* max TCK for the full speed devices 6000 kHz */
  88. #define FTDI_2232C_MAX_TCK 6000
  89. /* this speed value tells that RTCK is requested */
  90. #define RTCK_SPEED -1
  91. /*
  92. * On my Athlon XP 1900+ EHCI host with FT2232H JTAG dongle I get read timeout
  93. * errors with a retry count of 100. Increasing it solves the problem for me.
  94. * - Dimitar
  95. *
  96. * FIXME There's likely an issue with the usb_read_timeout from libftdi.
  97. * Fix that (libusb? kernel? libftdi? here?) and restore the retry count
  98. * to something sane.
  99. */
  100. #define LIBFTDI_READ_RETRY_COUNT 2000
  101. #ifndef BUILD_FT2232_HIGHSPEED
  102. #if BUILD_FT2232_FTD2XX == 1
  103. enum { FT_DEVICE_2232H = 6, FT_DEVICE_4232H };
  104. #elif BUILD_FT2232_LIBFTDI == 1
  105. enum { TYPE_2232H = 4, TYPE_4232H = 5 };
  106. #endif
  107. #endif
  108. /**
  109. * Send out \a num_cycles on the TCK line while the TAP(s) are in a
  110. * stable state. Calling code must ensure that current state is stable,
  111. * that verification is not done in here.
  112. *
  113. * @param num_cycles The number of clocks cycles to send.
  114. * @param cmd The command to send.
  115. *
  116. * @returns ERROR_OK on success, or ERROR_JTAG_QUEUE_FAILED on failure.
  117. */
  118. static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd);
  119. static char * ft2232_device_desc_A = NULL;
  120. static char* ft2232_device_desc = NULL;
  121. static char* ft2232_serial = NULL;
  122. static char* ft2232_layout = NULL;
  123. static uint8_t ft2232_latency = 2;
  124. static unsigned ft2232_max_tck = FTDI_2232C_MAX_TCK;
  125. #define MAX_USB_IDS 8
  126. /* vid = pid = 0 marks the end of the list */
  127. static uint16_t ft2232_vid[MAX_USB_IDS + 1] = { 0x0403, 0 };
  128. static uint16_t ft2232_pid[MAX_USB_IDS + 1] = { 0x6010, 0 };
  129. struct ft2232_layout {
  130. char* name;
  131. int (*init)(void);
  132. void (*reset)(int trst, int srst);
  133. void (*blink)(void);
  134. };
  135. /* init procedures for supported layouts */
  136. static int usbjtag_init(void);
  137. static int jtagkey_init(void);
  138. static int olimex_jtag_init(void);
  139. static int flyswatter_init(void);
  140. static int turtle_init(void);
  141. static int comstick_init(void);
  142. static int stm32stick_init(void);
  143. static int axm0432_jtag_init(void);
  144. static int sheevaplug_init(void);
  145. static int icebear_jtag_init(void);
  146. static int cortino_jtag_init(void);
  147. static int signalyzer_h_init(void);
  148. static int ktlink_init(void);
  149. /* reset procedures for supported layouts */
  150. static void usbjtag_reset(int trst, int srst);
  151. static void jtagkey_reset(int trst, int srst);
  152. static void olimex_jtag_reset(int trst, int srst);
  153. static void flyswatter_reset(int trst, int srst);
  154. static void turtle_reset(int trst, int srst);
  155. static void comstick_reset(int trst, int srst);
  156. static void stm32stick_reset(int trst, int srst);
  157. static void axm0432_jtag_reset(int trst, int srst);
  158. static void sheevaplug_reset(int trst, int srst);
  159. static void icebear_jtag_reset(int trst, int srst);
  160. static void signalyzer_h_reset(int trst, int srst);
  161. static void ktlink_reset(int trst, int srst);
  162. /* blink procedures for layouts that support a blinking led */
  163. static void olimex_jtag_blink(void);
  164. static void flyswatter_jtag_blink(void);
  165. static void turtle_jtag_blink(void);
  166. static void signalyzer_h_blink(void);
  167. static void ktlink_blink(void);
  168. static const struct ft2232_layout ft2232_layouts[] =
  169. {
  170. { "usbjtag", usbjtag_init, usbjtag_reset, NULL },
  171. { "jtagkey", jtagkey_init, jtagkey_reset, NULL },
  172. { "jtagkey_prototype_v1", jtagkey_init, jtagkey_reset, NULL },
  173. { "oocdlink", jtagkey_init, jtagkey_reset, NULL },
  174. { "signalyzer", usbjtag_init, usbjtag_reset, NULL },
  175. { "evb_lm3s811", usbjtag_init, usbjtag_reset, NULL },
  176. { "luminary_icdi", usbjtag_init, usbjtag_reset, NULL },
  177. { "olimex-jtag", olimex_jtag_init, olimex_jtag_reset, olimex_jtag_blink },
  178. { "flyswatter", flyswatter_init, flyswatter_reset, flyswatter_jtag_blink },
  179. { "turtelizer2", turtle_init, turtle_reset, turtle_jtag_blink },
  180. { "comstick", comstick_init, comstick_reset, NULL },
  181. { "stm32stick", stm32stick_init, stm32stick_reset, NULL },
  182. { "axm0432_jtag", axm0432_jtag_init, axm0432_jtag_reset, NULL },
  183. { "sheevaplug", sheevaplug_init, sheevaplug_reset, NULL },
  184. { "icebear", icebear_jtag_init, icebear_jtag_reset, NULL },
  185. { "cortino", cortino_jtag_init, comstick_reset, NULL },
  186. { "signalyzer-h", signalyzer_h_init, signalyzer_h_reset, signalyzer_h_blink },
  187. { "ktlink", ktlink_init, ktlink_reset, ktlink_blink },
  188. { NULL, NULL, NULL, NULL },
  189. };
  190. static uint8_t nTRST, nTRSTnOE, nSRST, nSRSTnOE;
  191. static const struct ft2232_layout *layout;
  192. static uint8_t low_output = 0x0;
  193. static uint8_t low_direction = 0x0;
  194. static uint8_t high_output = 0x0;
  195. static uint8_t high_direction = 0x0;
  196. #if BUILD_FT2232_FTD2XX == 1
  197. static FT_HANDLE ftdih = NULL;
  198. static FT_DEVICE ftdi_device = 0;
  199. #elif BUILD_FT2232_LIBFTDI == 1
  200. static struct ftdi_context ftdic;
  201. static enum ftdi_chip_type ftdi_device;
  202. #endif
  203. static struct jtag_command* first_unsent; /* next command that has to be sent */
  204. static int require_send;
  205. /* http://urjtag.wiki.sourceforge.net/Cable + FT2232 says:
  206. "There is a significant difference between libftdi and libftd2xx. The latter
  207. one allows to schedule up to 64*64 bytes of result data while libftdi fails
  208. with more than 4*64. As a consequence, the FT2232 driver is forced to
  209. perform around 16x more USB transactions for long command streams with TDO
  210. capture when running with libftdi."
  211. No idea how we get
  212. #define FT2232_BUFFER_SIZE 131072
  213. a comment would have been nice.
  214. */
  215. #define FT2232_BUFFER_SIZE 131072
  216. static uint8_t* ft2232_buffer = NULL;
  217. static int ft2232_buffer_size = 0;
  218. static int ft2232_read_pointer = 0;
  219. static int ft2232_expect_read = 0;
  220. /**
  221. * Function buffer_write
  222. * writes a byte into the byte buffer, "ft2232_buffer", which must be sent later.
  223. * @param val is the byte to send.
  224. */
  225. static inline void buffer_write(uint8_t val)
  226. {
  227. assert(ft2232_buffer);
  228. assert((unsigned) ft2232_buffer_size < (unsigned) FT2232_BUFFER_SIZE);
  229. ft2232_buffer[ft2232_buffer_size++] = val;
  230. }
  231. /**
  232. * Function buffer_read
  233. * returns a byte from the byte buffer.
  234. */
  235. static inline uint8_t buffer_read(void)
  236. {
  237. assert(ft2232_buffer);
  238. assert(ft2232_read_pointer < ft2232_buffer_size);
  239. return ft2232_buffer[ft2232_read_pointer++];
  240. }
  241. /**
  242. * Clocks out \a bit_count bits on the TMS line, starting with the least
  243. * significant bit of tms_bits and progressing to more significant bits.
  244. * Rigorous state transition logging is done here via tap_set_state().
  245. *
  246. * @param mpsse_cmd One of the MPSSE TMS oriented commands such as
  247. * 0x4b or 0x6b. See the MPSSE spec referenced above for their
  248. * functionality. The MPSSE command "Clock Data to TMS/CS Pin (no Read)"
  249. * is often used for this, 0x4b.
  250. *
  251. * @param tms_bits Holds the sequence of bits to send.
  252. * @param tms_count Tells how many bits in the sequence.
  253. * @param tdi_bit A single bit to pass on to TDI before the first TCK
  254. * cycle and held static for the duration of TMS clocking.
  255. *
  256. * See the MPSSE spec referenced above.
  257. */
  258. static void clock_tms(uint8_t mpsse_cmd, int tms_bits, int tms_count, bool tdi_bit)
  259. {
  260. uint8_t tms_byte;
  261. int i;
  262. int tms_ndx; /* bit index into tms_byte */
  263. assert(tms_count > 0);
  264. DEBUG_JTAG_IO("mpsse cmd=%02x, tms_bits = 0x%08x, bit_count=%d",
  265. mpsse_cmd, tms_bits, tms_count);
  266. for (tms_byte = tms_ndx = i = 0; i < tms_count; ++i, tms_bits>>=1)
  267. {
  268. bool bit = tms_bits & 1;
  269. if (bit)
  270. tms_byte |= (1 << tms_ndx);
  271. /* always do state transitions in public view */
  272. tap_set_state(tap_state_transition(tap_get_state(), bit));
  273. /* we wrote a bit to tms_byte just above, increment bit index. if bit was zero
  274. also increment.
  275. */
  276. ++tms_ndx;
  277. if (tms_ndx == 7 || i == tms_count-1)
  278. {
  279. buffer_write(mpsse_cmd);
  280. buffer_write(tms_ndx - 1);
  281. /* Bit 7 of the byte is passed on to TDI/DO before the first TCK/SK of
  282. TMS/CS and is held static for the duration of TMS/CS clocking.
  283. */
  284. buffer_write(tms_byte | (tdi_bit << 7));
  285. }
  286. }
  287. }
  288. /**
  289. * Function get_tms_buffer_requirements
  290. * returns what clock_tms() will consume if called with
  291. * same \a bit_count.
  292. */
  293. static inline int get_tms_buffer_requirements(int bit_count)
  294. {
  295. return ((bit_count + 6)/7) * 3;
  296. }
  297. /**
  298. * Function move_to_state
  299. * moves the TAP controller from the current state to a
  300. * \a goal_state through a path given by tap_get_tms_path(). State transition
  301. * logging is performed by delegation to clock_tms().
  302. *
  303. * @param goal_state is the destination state for the move.
  304. */
  305. static void move_to_state(tap_state_t goal_state)
  306. {
  307. tap_state_t start_state = tap_get_state();
  308. /* goal_state is 1/2 of a tuple/pair of states which allow convenient
  309. lookup of the required TMS pattern to move to this state from the
  310. start state.
  311. */
  312. /* do the 2 lookups */
  313. int tms_bits = tap_get_tms_path(start_state, goal_state);
  314. int tms_count = tap_get_tms_path_len(start_state, goal_state);
  315. DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
  316. clock_tms(0x4b, tms_bits, tms_count, 0);
  317. }
  318. static int ft2232_write(uint8_t* buf, int size, uint32_t* bytes_written)
  319. {
  320. #if BUILD_FT2232_FTD2XX == 1
  321. FT_STATUS status;
  322. DWORD dw_bytes_written;
  323. if ((status = FT_Write(ftdih, buf, size, &dw_bytes_written)) != FT_OK)
  324. {
  325. *bytes_written = dw_bytes_written;
  326. LOG_ERROR("FT_Write returned: %lu", status);
  327. return ERROR_JTAG_DEVICE_ERROR;
  328. }
  329. else
  330. {
  331. *bytes_written = dw_bytes_written;
  332. return ERROR_OK;
  333. }
  334. #elif BUILD_FT2232_LIBFTDI == 1
  335. int retval;
  336. if ((retval = ftdi_write_data(&ftdic, buf, size)) < 0)
  337. {
  338. *bytes_written = 0;
  339. LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&ftdic));
  340. return ERROR_JTAG_DEVICE_ERROR;
  341. }
  342. else
  343. {
  344. *bytes_written = retval;
  345. return ERROR_OK;
  346. }
  347. #endif
  348. }
  349. static int ft2232_read(uint8_t* buf, uint32_t size, uint32_t* bytes_read)
  350. {
  351. #if BUILD_FT2232_FTD2XX == 1
  352. DWORD dw_bytes_read;
  353. FT_STATUS status;
  354. int timeout = 5;
  355. *bytes_read = 0;
  356. while ((*bytes_read < size) && timeout--)
  357. {
  358. if ((status = FT_Read(ftdih, buf + *bytes_read, size -
  359. *bytes_read, &dw_bytes_read)) != FT_OK)
  360. {
  361. *bytes_read = 0;
  362. LOG_ERROR("FT_Read returned: %lu", status);
  363. return ERROR_JTAG_DEVICE_ERROR;
  364. }
  365. *bytes_read += dw_bytes_read;
  366. }
  367. #elif BUILD_FT2232_LIBFTDI == 1
  368. int retval;
  369. int timeout = LIBFTDI_READ_RETRY_COUNT;
  370. *bytes_read = 0;
  371. while ((*bytes_read < size) && timeout--)
  372. {
  373. if ((retval = ftdi_read_data(&ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
  374. {
  375. *bytes_read = 0;
  376. LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&ftdic));
  377. return ERROR_JTAG_DEVICE_ERROR;
  378. }
  379. *bytes_read += retval;
  380. }
  381. #endif
  382. if (*bytes_read < size)
  383. {
  384. LOG_ERROR("couldn't read enough bytes from "
  385. "FT2232 device (%i < %i)",
  386. (unsigned)*bytes_read,
  387. (unsigned)size);
  388. return ERROR_JTAG_DEVICE_ERROR;
  389. }
  390. return ERROR_OK;
  391. }
  392. static bool ft2232_device_is_highspeed(void)
  393. {
  394. #if BUILD_FT2232_FTD2XX == 1
  395. return (ftdi_device == FT_DEVICE_2232H) || (ftdi_device == FT_DEVICE_4232H);
  396. #elif BUILD_FT2232_LIBFTDI == 1
  397. return (ftdi_device == TYPE_2232H || ftdi_device == TYPE_4232H);
  398. #endif
  399. }
  400. /*
  401. * Commands that only apply to the FT2232H and FT4232H devices.
  402. * See chapter 6 in http://www.ftdichip.com/Documents/AppNotes/
  403. * AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
  404. */
  405. static int ft2232h_ft4232h_adaptive_clocking(bool enable)
  406. {
  407. uint8_t buf = enable ? 0x96 : 0x97;
  408. LOG_DEBUG("%2.2x", buf);
  409. uint32_t bytes_written;
  410. int retval = ft2232_write(&buf, 1, &bytes_written);
  411. if ((ERROR_OK != retval) || (bytes_written != 1))
  412. {
  413. LOG_ERROR("couldn't write command to %s adaptive clocking"
  414. , enable ? "enable" : "disable");
  415. return retval;
  416. }
  417. return ERROR_OK;
  418. }
  419. /**
  420. * Enable/disable the clk divide by 5 of the 60MHz master clock.
  421. * This result in a JTAG clock speed range of 91.553Hz-6MHz
  422. * respective 457.763Hz-30MHz.
  423. */
  424. static int ft2232h_ft4232h_clk_divide_by_5(bool enable)
  425. {
  426. uint32_t bytes_written;
  427. uint8_t buf = enable ? 0x8b : 0x8a;
  428. int retval = ft2232_write(&buf, 1, &bytes_written);
  429. if ((ERROR_OK != retval) || (bytes_written != 1))
  430. {
  431. LOG_ERROR("couldn't write command to %s clk divide by 5"
  432. , enable ? "enable" : "disable");
  433. return ERROR_JTAG_INIT_FAILED;
  434. }
  435. ft2232_max_tck = enable ? FTDI_2232C_MAX_TCK : FTDI_2232H_4232H_MAX_TCK;
  436. LOG_INFO("max TCK change to: %u kHz", ft2232_max_tck);
  437. return ERROR_OK;
  438. }
  439. static int ft2232_speed(int speed)
  440. {
  441. uint8_t buf[3];
  442. int retval;
  443. uint32_t bytes_written;
  444. retval = ERROR_OK;
  445. bool enable_adaptive_clocking = (RTCK_SPEED == speed);
  446. if (ft2232_device_is_highspeed())
  447. retval = ft2232h_ft4232h_adaptive_clocking(enable_adaptive_clocking);
  448. else if (enable_adaptive_clocking)
  449. {
  450. LOG_ERROR("ft2232 device %lu does not support RTCK"
  451. , (long unsigned int)ftdi_device);
  452. return ERROR_FAIL;
  453. }
  454. if ((enable_adaptive_clocking) || (ERROR_OK != retval))
  455. return retval;
  456. buf[0] = 0x86; /* command "set divisor" */
  457. buf[1] = speed & 0xff; /* valueL (0 = 6MHz, 1 = 3MHz, 2 = 2.0MHz, ...*/
  458. buf[2] = (speed >> 8) & 0xff; /* valueH */
  459. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  460. if (((retval = ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  461. {
  462. LOG_ERROR("couldn't set FT2232 TCK speed");
  463. return retval;
  464. }
  465. return ERROR_OK;
  466. }
  467. static int ft2232_speed_div(int speed, int* khz)
  468. {
  469. /* Take a look in the FT2232 manual,
  470. * AN2232C-01 Command Processor for
  471. * MPSSE and MCU Host Bus. Chapter 3.8 */
  472. *khz = (RTCK_SPEED == speed) ? 0 : ft2232_max_tck / (1 + speed);
  473. return ERROR_OK;
  474. }
  475. static int ft2232_khz(int khz, int* jtag_speed)
  476. {
  477. if (khz == 0)
  478. {
  479. if (ft2232_device_is_highspeed())
  480. {
  481. *jtag_speed = RTCK_SPEED;
  482. return ERROR_OK;
  483. }
  484. else
  485. {
  486. LOG_DEBUG("RCLK not supported");
  487. return ERROR_FAIL;
  488. }
  489. }
  490. /* Take a look in the FT2232 manual,
  491. * AN2232C-01 Command Processor for
  492. * MPSSE and MCU Host Bus. Chapter 3.8
  493. *
  494. * We will calc here with a multiplier
  495. * of 10 for better rounding later. */
  496. /* Calc speed, (ft2232_max_tck / khz) - 1 */
  497. /* Use 65000 for better rounding */
  498. *jtag_speed = ((ft2232_max_tck*10) / khz) - 10;
  499. /* Add 0.9 for rounding */
  500. *jtag_speed += 9;
  501. /* Calc real speed */
  502. *jtag_speed = *jtag_speed / 10;
  503. /* Check if speed is greater than 0 */
  504. if (*jtag_speed < 0)
  505. {
  506. *jtag_speed = 0;
  507. }
  508. /* Check max value */
  509. if (*jtag_speed > 0xFFFF)
  510. {
  511. *jtag_speed = 0xFFFF;
  512. }
  513. return ERROR_OK;
  514. }
  515. static void ft2232_end_state(tap_state_t state)
  516. {
  517. if (tap_is_state_stable(state))
  518. tap_set_end_state(state);
  519. else
  520. {
  521. LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
  522. exit(-1);
  523. }
  524. }
  525. static void ft2232_read_scan(enum scan_type type, uint8_t* buffer, int scan_size)
  526. {
  527. int num_bytes = (scan_size + 7) / 8;
  528. int bits_left = scan_size;
  529. int cur_byte = 0;
  530. while (num_bytes-- > 1)
  531. {
  532. buffer[cur_byte++] = buffer_read();
  533. bits_left -= 8;
  534. }
  535. buffer[cur_byte] = 0x0;
  536. /* There is one more partial byte left from the clock data in/out instructions */
  537. if (bits_left > 1)
  538. {
  539. buffer[cur_byte] = buffer_read() >> 1;
  540. }
  541. /* This shift depends on the length of the clock data to tms instruction, insterted at end of the scan, now fixed to a two step transition in ft2232_add_scan */
  542. buffer[cur_byte] = (buffer[cur_byte] | (((buffer_read()) << 1) & 0x80)) >> (8 - bits_left);
  543. }
  544. static void ft2232_debug_dump_buffer(void)
  545. {
  546. int i;
  547. char line[256];
  548. char* line_p = line;
  549. for (i = 0; i < ft2232_buffer_size; i++)
  550. {
  551. line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ft2232_buffer[i]);
  552. if (i % 16 == 15)
  553. {
  554. LOG_DEBUG("%s", line);
  555. line_p = line;
  556. }
  557. }
  558. if (line_p != line)
  559. LOG_DEBUG("%s", line);
  560. }
  561. static int ft2232_send_and_recv(struct jtag_command* first, struct jtag_command* last)
  562. {
  563. struct jtag_command* cmd;
  564. uint8_t* buffer;
  565. int scan_size;
  566. enum scan_type type;
  567. int retval;
  568. uint32_t bytes_written = 0;
  569. uint32_t bytes_read = 0;
  570. #ifdef _DEBUG_USB_IO_
  571. struct timeval start, inter, inter2, end;
  572. struct timeval d_inter, d_inter2, d_end;
  573. #endif
  574. #ifdef _DEBUG_USB_COMMS_
  575. LOG_DEBUG("write buffer (size %i):", ft2232_buffer_size);
  576. ft2232_debug_dump_buffer();
  577. #endif
  578. #ifdef _DEBUG_USB_IO_
  579. gettimeofday(&start, NULL);
  580. #endif
  581. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  582. {
  583. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  584. return retval;
  585. }
  586. #ifdef _DEBUG_USB_IO_
  587. gettimeofday(&inter, NULL);
  588. #endif
  589. if (ft2232_expect_read)
  590. {
  591. /* FIXME this "timeout" is never changed ... */
  592. int timeout = LIBFTDI_READ_RETRY_COUNT;
  593. ft2232_buffer_size = 0;
  594. #ifdef _DEBUG_USB_IO_
  595. gettimeofday(&inter2, NULL);
  596. #endif
  597. if ((retval = ft2232_read(ft2232_buffer, ft2232_expect_read, &bytes_read)) != ERROR_OK)
  598. {
  599. LOG_ERROR("couldn't read from FT2232");
  600. return retval;
  601. }
  602. #ifdef _DEBUG_USB_IO_
  603. gettimeofday(&end, NULL);
  604. timeval_subtract(&d_inter, &inter, &start);
  605. timeval_subtract(&d_inter2, &inter2, &start);
  606. timeval_subtract(&d_end, &end, &start);
  607. LOG_INFO("inter: %u.%06u, inter2: %u.%06u end: %u.%06u",
  608. (unsigned)d_inter.tv_sec, (unsigned)d_inter.tv_usec,
  609. (unsigned)d_inter2.tv_sec, (unsigned)d_inter2.tv_usec,
  610. (unsigned)d_end.tv_sec, (unsigned)d_end.tv_usec);
  611. #endif
  612. ft2232_buffer_size = bytes_read;
  613. if (ft2232_expect_read != ft2232_buffer_size)
  614. {
  615. LOG_ERROR("ft2232_expect_read (%i) != "
  616. "ft2232_buffer_size (%i) "
  617. "(%i retries)",
  618. ft2232_expect_read,
  619. ft2232_buffer_size,
  620. LIBFTDI_READ_RETRY_COUNT - timeout);
  621. ft2232_debug_dump_buffer();
  622. exit(-1);
  623. }
  624. #ifdef _DEBUG_USB_COMMS_
  625. LOG_DEBUG("read buffer (%i retries): %i bytes",
  626. LIBFTDI_READ_RETRY_COUNT - timeout,
  627. ft2232_buffer_size);
  628. ft2232_debug_dump_buffer();
  629. #endif
  630. }
  631. ft2232_expect_read = 0;
  632. ft2232_read_pointer = 0;
  633. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  634. * that wasn't handled by a caller-provided error handler
  635. */
  636. retval = ERROR_OK;
  637. cmd = first;
  638. while (cmd != last)
  639. {
  640. switch (cmd->type)
  641. {
  642. case JTAG_SCAN:
  643. type = jtag_scan_type(cmd->cmd.scan);
  644. if (type != SCAN_OUT)
  645. {
  646. scan_size = jtag_scan_size(cmd->cmd.scan);
  647. buffer = calloc(DIV_ROUND_UP(scan_size, 8), 1);
  648. ft2232_read_scan(type, buffer, scan_size);
  649. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  650. retval = ERROR_JTAG_QUEUE_FAILED;
  651. free(buffer);
  652. }
  653. break;
  654. default:
  655. break;
  656. }
  657. cmd = cmd->next;
  658. }
  659. ft2232_buffer_size = 0;
  660. return retval;
  661. }
  662. /**
  663. * Function ft2232_add_pathmove
  664. * moves the TAP controller from the current state to a new state through the
  665. * given path, where path is an array of tap_state_t's.
  666. *
  667. * @param path is an array of tap_stat_t which gives the states to traverse through
  668. * ending with the last state at path[num_states-1]
  669. * @param num_states is the count of state steps to move through
  670. */
  671. static void ft2232_add_pathmove(tap_state_t* path, int num_states)
  672. {
  673. int state_count = 0;
  674. assert((unsigned) num_states <= 32u); /* tms_bits only holds 32 bits */
  675. DEBUG_JTAG_IO("-");
  676. /* this loop verifies that the path is legal and logs each state in the path */
  677. while (num_states)
  678. {
  679. unsigned char tms_byte = 0; /* zero this on each MPSSE batch */
  680. int bit_count = 0;
  681. int num_states_batch = num_states > 7 ? 7 : num_states;
  682. /* command "Clock Data to TMS/CS Pin (no Read)" */
  683. buffer_write(0x4b);
  684. /* number of states remaining */
  685. buffer_write(num_states_batch - 1);
  686. while (num_states_batch--) {
  687. /* either TMS=0 or TMS=1 must work ... */
  688. if (tap_state_transition(tap_get_state(), false)
  689. == path[state_count])
  690. buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
  691. else if (tap_state_transition(tap_get_state(), true)
  692. == path[state_count])
  693. buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
  694. /* ... or else the caller goofed BADLY */
  695. else {
  696. LOG_ERROR("BUG: %s -> %s isn't a valid "
  697. "TAP state transition",
  698. tap_state_name(tap_get_state()),
  699. tap_state_name(path[state_count]));
  700. exit(-1);
  701. }
  702. tap_set_state(path[state_count]);
  703. state_count++;
  704. num_states--;
  705. }
  706. buffer_write(tms_byte);
  707. }
  708. tap_set_end_state(tap_get_state());
  709. }
  710. static void ft2232_add_scan(bool ir_scan, enum scan_type type, uint8_t* buffer, int scan_size)
  711. {
  712. int num_bytes = (scan_size + 7) / 8;
  713. int bits_left = scan_size;
  714. int cur_byte = 0;
  715. int last_bit;
  716. if (!ir_scan)
  717. {
  718. if (tap_get_state() != TAP_DRSHIFT)
  719. {
  720. move_to_state(TAP_DRSHIFT);
  721. }
  722. }
  723. else
  724. {
  725. if (tap_get_state() != TAP_IRSHIFT)
  726. {
  727. move_to_state(TAP_IRSHIFT);
  728. }
  729. }
  730. /* add command for complete bytes */
  731. while (num_bytes > 1)
  732. {
  733. int thisrun_bytes;
  734. if (type == SCAN_IO)
  735. {
  736. /* Clock Data Bytes In and Out LSB First */
  737. buffer_write(0x39);
  738. /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
  739. }
  740. else if (type == SCAN_OUT)
  741. {
  742. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  743. buffer_write(0x19);
  744. /* LOG_DEBUG("added TDI bytes (o)"); */
  745. }
  746. else if (type == SCAN_IN)
  747. {
  748. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  749. buffer_write(0x28);
  750. /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
  751. }
  752. thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
  753. num_bytes -= thisrun_bytes;
  754. buffer_write((uint8_t) (thisrun_bytes - 1));
  755. buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
  756. if (type != SCAN_IN)
  757. {
  758. /* add complete bytes */
  759. while (thisrun_bytes-- > 0)
  760. {
  761. buffer_write(buffer[cur_byte++]);
  762. bits_left -= 8;
  763. }
  764. }
  765. else /* (type == SCAN_IN) */
  766. {
  767. bits_left -= 8 * (thisrun_bytes);
  768. }
  769. }
  770. /* the most signifcant bit is scanned during TAP movement */
  771. if (type != SCAN_IN)
  772. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  773. else
  774. last_bit = 0;
  775. /* process remaining bits but the last one */
  776. if (bits_left > 1)
  777. {
  778. if (type == SCAN_IO)
  779. {
  780. /* Clock Data Bits In and Out LSB First */
  781. buffer_write(0x3b);
  782. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  783. }
  784. else if (type == SCAN_OUT)
  785. {
  786. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  787. buffer_write(0x1b);
  788. /* LOG_DEBUG("added TDI bits (o)"); */
  789. }
  790. else if (type == SCAN_IN)
  791. {
  792. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  793. buffer_write(0x2a);
  794. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  795. }
  796. buffer_write(bits_left - 2);
  797. if (type != SCAN_IN)
  798. buffer_write(buffer[cur_byte]);
  799. }
  800. if ((ir_scan && (tap_get_end_state() == TAP_IRSHIFT))
  801. || (!ir_scan && (tap_get_end_state() == TAP_DRSHIFT)))
  802. {
  803. if (type == SCAN_IO)
  804. {
  805. /* Clock Data Bits In and Out LSB First */
  806. buffer_write(0x3b);
  807. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  808. }
  809. else if (type == SCAN_OUT)
  810. {
  811. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  812. buffer_write(0x1b);
  813. /* LOG_DEBUG("added TDI bits (o)"); */
  814. }
  815. else if (type == SCAN_IN)
  816. {
  817. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  818. buffer_write(0x2a);
  819. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  820. }
  821. buffer_write(0x0);
  822. buffer_write(last_bit);
  823. }
  824. else
  825. {
  826. int tms_bits;
  827. int tms_count;
  828. uint8_t mpsse_cmd;
  829. /* move from Shift-IR/DR to end state */
  830. if (type != SCAN_OUT)
  831. {
  832. /* We always go to the PAUSE state in two step at the end of an IN or IO scan */
  833. /* This must be coordinated with the bit shifts in ft2232_read_scan */
  834. tms_bits = 0x01;
  835. tms_count = 2;
  836. /* Clock Data to TMS/CS Pin with Read */
  837. mpsse_cmd = 0x6b;
  838. }
  839. else
  840. {
  841. tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  842. tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  843. /* Clock Data to TMS/CS Pin (no Read) */
  844. mpsse_cmd = 0x4b;
  845. }
  846. DEBUG_JTAG_IO("finish %s", (type == SCAN_OUT) ? "without read" : "via PAUSE");
  847. clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
  848. }
  849. if (tap_get_state() != tap_get_end_state())
  850. {
  851. move_to_state(tap_get_end_state());
  852. }
  853. }
  854. static int ft2232_large_scan(struct scan_command* cmd, enum scan_type type, uint8_t* buffer, int scan_size)
  855. {
  856. int num_bytes = (scan_size + 7) / 8;
  857. int bits_left = scan_size;
  858. int cur_byte = 0;
  859. int last_bit;
  860. uint8_t* receive_buffer = malloc(DIV_ROUND_UP(scan_size, 8));
  861. uint8_t* receive_pointer = receive_buffer;
  862. uint32_t bytes_written;
  863. uint32_t bytes_read;
  864. int retval;
  865. int thisrun_read = 0;
  866. if (cmd->ir_scan)
  867. {
  868. LOG_ERROR("BUG: large IR scans are not supported");
  869. exit(-1);
  870. }
  871. if (tap_get_state() != TAP_DRSHIFT)
  872. {
  873. move_to_state(TAP_DRSHIFT);
  874. }
  875. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  876. {
  877. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  878. exit(-1);
  879. }
  880. LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
  881. ft2232_buffer_size, (int)bytes_written);
  882. ft2232_buffer_size = 0;
  883. /* add command for complete bytes */
  884. while (num_bytes > 1)
  885. {
  886. int thisrun_bytes;
  887. if (type == SCAN_IO)
  888. {
  889. /* Clock Data Bytes In and Out LSB First */
  890. buffer_write(0x39);
  891. /* LOG_DEBUG("added TDI bytes (io %i)", num_bytes); */
  892. }
  893. else if (type == SCAN_OUT)
  894. {
  895. /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
  896. buffer_write(0x19);
  897. /* LOG_DEBUG("added TDI bytes (o)"); */
  898. }
  899. else if (type == SCAN_IN)
  900. {
  901. /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
  902. buffer_write(0x28);
  903. /* LOG_DEBUG("added TDI bytes (i %i)", num_bytes); */
  904. }
  905. thisrun_bytes = (num_bytes > 65537) ? 65536 : (num_bytes - 1);
  906. thisrun_read = thisrun_bytes;
  907. num_bytes -= thisrun_bytes;
  908. buffer_write((uint8_t) (thisrun_bytes - 1));
  909. buffer_write((uint8_t) ((thisrun_bytes - 1) >> 8));
  910. if (type != SCAN_IN)
  911. {
  912. /* add complete bytes */
  913. while (thisrun_bytes-- > 0)
  914. {
  915. buffer_write(buffer[cur_byte]);
  916. cur_byte++;
  917. bits_left -= 8;
  918. }
  919. }
  920. else /* (type == SCAN_IN) */
  921. {
  922. bits_left -= 8 * (thisrun_bytes);
  923. }
  924. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  925. {
  926. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  927. exit(-1);
  928. }
  929. LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
  930. ft2232_buffer_size,
  931. (int)bytes_written);
  932. ft2232_buffer_size = 0;
  933. if (type != SCAN_OUT)
  934. {
  935. if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
  936. {
  937. LOG_ERROR("couldn't read from FT2232");
  938. exit(-1);
  939. }
  940. LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
  941. thisrun_read,
  942. (int)bytes_read);
  943. receive_pointer += bytes_read;
  944. }
  945. }
  946. thisrun_read = 0;
  947. /* the most signifcant bit is scanned during TAP movement */
  948. if (type != SCAN_IN)
  949. last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
  950. else
  951. last_bit = 0;
  952. /* process remaining bits but the last one */
  953. if (bits_left > 1)
  954. {
  955. if (type == SCAN_IO)
  956. {
  957. /* Clock Data Bits In and Out LSB First */
  958. buffer_write(0x3b);
  959. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  960. }
  961. else if (type == SCAN_OUT)
  962. {
  963. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  964. buffer_write(0x1b);
  965. /* LOG_DEBUG("added TDI bits (o)"); */
  966. }
  967. else if (type == SCAN_IN)
  968. {
  969. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  970. buffer_write(0x2a);
  971. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  972. }
  973. buffer_write(bits_left - 2);
  974. if (type != SCAN_IN)
  975. buffer_write(buffer[cur_byte]);
  976. if (type != SCAN_OUT)
  977. thisrun_read += 2;
  978. }
  979. if (tap_get_end_state() == TAP_DRSHIFT)
  980. {
  981. if (type == SCAN_IO)
  982. {
  983. /* Clock Data Bits In and Out LSB First */
  984. buffer_write(0x3b);
  985. /* LOG_DEBUG("added TDI bits (io) %i", bits_left - 1); */
  986. }
  987. else if (type == SCAN_OUT)
  988. {
  989. /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
  990. buffer_write(0x1b);
  991. /* LOG_DEBUG("added TDI bits (o)"); */
  992. }
  993. else if (type == SCAN_IN)
  994. {
  995. /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
  996. buffer_write(0x2a);
  997. /* LOG_DEBUG("added TDI bits (i %i)", bits_left - 1); */
  998. }
  999. buffer_write(0x0);
  1000. buffer_write(last_bit);
  1001. }
  1002. else
  1003. {
  1004. int tms_bits = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  1005. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  1006. uint8_t mpsse_cmd;
  1007. /* move from Shift-IR/DR to end state */
  1008. if (type != SCAN_OUT)
  1009. {
  1010. /* Clock Data to TMS/CS Pin with Read */
  1011. mpsse_cmd = 0x6b;
  1012. /* LOG_DEBUG("added TMS scan (read)"); */
  1013. }
  1014. else
  1015. {
  1016. /* Clock Data to TMS/CS Pin (no Read) */
  1017. mpsse_cmd = 0x4b;
  1018. /* LOG_DEBUG("added TMS scan (no read)"); */
  1019. }
  1020. DEBUG_JTAG_IO("finish, %s", (type == SCAN_OUT) ? "no read" : "read");
  1021. clock_tms(mpsse_cmd, tms_bits, tms_count, last_bit);
  1022. }
  1023. if (type != SCAN_OUT)
  1024. thisrun_read += 1;
  1025. if ((retval = ft2232_write(ft2232_buffer, ft2232_buffer_size, &bytes_written)) != ERROR_OK)
  1026. {
  1027. LOG_ERROR("couldn't write MPSSE commands to FT2232");
  1028. exit(-1);
  1029. }
  1030. LOG_DEBUG("ft2232_buffer_size: %i, bytes_written: %i",
  1031. ft2232_buffer_size,
  1032. (int)bytes_written);
  1033. ft2232_buffer_size = 0;
  1034. if (type != SCAN_OUT)
  1035. {
  1036. if ((retval = ft2232_read(receive_pointer, thisrun_read, &bytes_read)) != ERROR_OK)
  1037. {
  1038. LOG_ERROR("couldn't read from FT2232");
  1039. exit(-1);
  1040. }
  1041. LOG_DEBUG("thisrun_read: %i, bytes_read: %i",
  1042. thisrun_read,
  1043. (int)bytes_read);
  1044. receive_pointer += bytes_read;
  1045. }
  1046. return ERROR_OK;
  1047. }
  1048. static int ft2232_predict_scan_out(int scan_size, enum scan_type type)
  1049. {
  1050. int predicted_size = 3;
  1051. int num_bytes = (scan_size - 1) / 8;
  1052. if (tap_get_state() != TAP_DRSHIFT)
  1053. predicted_size += get_tms_buffer_requirements(tap_get_tms_path_len(tap_get_state(), TAP_DRSHIFT));
  1054. if (type == SCAN_IN) /* only from device to host */
  1055. {
  1056. /* complete bytes */
  1057. predicted_size += DIV_ROUND_UP(num_bytes, 65536) * 3;
  1058. /* remaining bits - 1 (up to 7) */
  1059. predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
  1060. }
  1061. else /* host to device, or bidirectional */
  1062. {
  1063. /* complete bytes */
  1064. predicted_size += num_bytes + DIV_ROUND_UP(num_bytes, 65536) * 3;
  1065. /* remaining bits -1 (up to 7) */
  1066. predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
  1067. }
  1068. return predicted_size;
  1069. }
  1070. static int ft2232_predict_scan_in(int scan_size, enum scan_type type)
  1071. {
  1072. int predicted_size = 0;
  1073. if (type != SCAN_OUT)
  1074. {
  1075. /* complete bytes */
  1076. predicted_size += (DIV_ROUND_UP(scan_size, 8) > 1) ? (DIV_ROUND_UP(scan_size, 8) - 1) : 0;
  1077. /* remaining bits - 1 */
  1078. predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
  1079. /* last bit (from TMS scan) */
  1080. predicted_size += 1;
  1081. }
  1082. /* LOG_DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size); */
  1083. return predicted_size;
  1084. }
  1085. static void usbjtag_reset(int trst, int srst)
  1086. {
  1087. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1088. if (trst == 1)
  1089. {
  1090. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1091. low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
  1092. else
  1093. low_output &= ~nTRST; /* switch output low */
  1094. }
  1095. else if (trst == 0)
  1096. {
  1097. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1098. low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
  1099. else
  1100. low_output |= nTRST; /* switch output high */
  1101. }
  1102. if (srst == 1)
  1103. {
  1104. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1105. low_output &= ~nSRST; /* switch output low */
  1106. else
  1107. low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
  1108. }
  1109. else if (srst == 0)
  1110. {
  1111. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1112. low_output |= nSRST; /* switch output high */
  1113. else
  1114. low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
  1115. }
  1116. /* command "set data bits low byte" */
  1117. buffer_write(0x80);
  1118. buffer_write(low_output);
  1119. buffer_write(low_direction);
  1120. }
  1121. static void jtagkey_reset(int trst, int srst)
  1122. {
  1123. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1124. if (trst == 1)
  1125. {
  1126. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1127. high_output &= ~nTRSTnOE;
  1128. else
  1129. high_output &= ~nTRST;
  1130. }
  1131. else if (trst == 0)
  1132. {
  1133. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1134. high_output |= nTRSTnOE;
  1135. else
  1136. high_output |= nTRST;
  1137. }
  1138. if (srst == 1)
  1139. {
  1140. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1141. high_output &= ~nSRST;
  1142. else
  1143. high_output &= ~nSRSTnOE;
  1144. }
  1145. else if (srst == 0)
  1146. {
  1147. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1148. high_output |= nSRST;
  1149. else
  1150. high_output |= nSRSTnOE;
  1151. }
  1152. /* command "set data bits high byte" */
  1153. buffer_write(0x82);
  1154. buffer_write(high_output);
  1155. buffer_write(high_direction);
  1156. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1157. high_direction);
  1158. }
  1159. static void olimex_jtag_reset(int trst, int srst)
  1160. {
  1161. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1162. if (trst == 1)
  1163. {
  1164. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1165. high_output &= ~nTRSTnOE;
  1166. else
  1167. high_output &= ~nTRST;
  1168. }
  1169. else if (trst == 0)
  1170. {
  1171. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1172. high_output |= nTRSTnOE;
  1173. else
  1174. high_output |= nTRST;
  1175. }
  1176. if (srst == 1)
  1177. {
  1178. high_output |= nSRST;
  1179. }
  1180. else if (srst == 0)
  1181. {
  1182. high_output &= ~nSRST;
  1183. }
  1184. /* command "set data bits high byte" */
  1185. buffer_write(0x82);
  1186. buffer_write(high_output);
  1187. buffer_write(high_direction);
  1188. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1189. high_direction);
  1190. }
  1191. static void axm0432_jtag_reset(int trst, int srst)
  1192. {
  1193. if (trst == 1)
  1194. {
  1195. tap_set_state(TAP_RESET);
  1196. high_output &= ~nTRST;
  1197. }
  1198. else if (trst == 0)
  1199. {
  1200. high_output |= nTRST;
  1201. }
  1202. if (srst == 1)
  1203. {
  1204. high_output &= ~nSRST;
  1205. }
  1206. else if (srst == 0)
  1207. {
  1208. high_output |= nSRST;
  1209. }
  1210. /* command "set data bits low byte" */
  1211. buffer_write(0x82);
  1212. buffer_write(high_output);
  1213. buffer_write(high_direction);
  1214. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1215. high_direction);
  1216. }
  1217. static void flyswatter_reset(int trst, int srst)
  1218. {
  1219. if (trst == 1)
  1220. {
  1221. low_output &= ~nTRST;
  1222. }
  1223. else if (trst == 0)
  1224. {
  1225. low_output |= nTRST;
  1226. }
  1227. if (srst == 1)
  1228. {
  1229. low_output |= nSRST;
  1230. }
  1231. else if (srst == 0)
  1232. {
  1233. low_output &= ~nSRST;
  1234. }
  1235. /* command "set data bits low byte" */
  1236. buffer_write(0x80);
  1237. buffer_write(low_output);
  1238. buffer_write(low_direction);
  1239. LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
  1240. }
  1241. static void turtle_reset(int trst, int srst)
  1242. {
  1243. trst = trst;
  1244. if (srst == 1)
  1245. {
  1246. low_output |= nSRST;
  1247. }
  1248. else if (srst == 0)
  1249. {
  1250. low_output &= ~nSRST;
  1251. }
  1252. /* command "set data bits low byte" */
  1253. buffer_write(0x80);
  1254. buffer_write(low_output);
  1255. buffer_write(low_direction);
  1256. LOG_DEBUG("srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", srst, low_output, low_direction);
  1257. }
  1258. static void comstick_reset(int trst, int srst)
  1259. {
  1260. if (trst == 1)
  1261. {
  1262. high_output &= ~nTRST;
  1263. }
  1264. else if (trst == 0)
  1265. {
  1266. high_output |= nTRST;
  1267. }
  1268. if (srst == 1)
  1269. {
  1270. high_output &= ~nSRST;
  1271. }
  1272. else if (srst == 0)
  1273. {
  1274. high_output |= nSRST;
  1275. }
  1276. /* command "set data bits high byte" */
  1277. buffer_write(0x82);
  1278. buffer_write(high_output);
  1279. buffer_write(high_direction);
  1280. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1281. high_direction);
  1282. }
  1283. static void stm32stick_reset(int trst, int srst)
  1284. {
  1285. if (trst == 1)
  1286. {
  1287. high_output &= ~nTRST;
  1288. }
  1289. else if (trst == 0)
  1290. {
  1291. high_output |= nTRST;
  1292. }
  1293. if (srst == 1)
  1294. {
  1295. low_output &= ~nSRST;
  1296. }
  1297. else if (srst == 0)
  1298. {
  1299. low_output |= nSRST;
  1300. }
  1301. /* command "set data bits low byte" */
  1302. buffer_write(0x80);
  1303. buffer_write(low_output);
  1304. buffer_write(low_direction);
  1305. /* command "set data bits high byte" */
  1306. buffer_write(0x82);
  1307. buffer_write(high_output);
  1308. buffer_write(high_direction);
  1309. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,
  1310. high_direction);
  1311. }
  1312. static void sheevaplug_reset(int trst, int srst)
  1313. {
  1314. if (trst == 1)
  1315. high_output &= ~nTRST;
  1316. else if (trst == 0)
  1317. high_output |= nTRST;
  1318. if (srst == 1)
  1319. high_output &= ~nSRSTnOE;
  1320. else if (srst == 0)
  1321. high_output |= nSRSTnOE;
  1322. /* command "set data bits high byte" */
  1323. buffer_write(0x82);
  1324. buffer_write(high_output);
  1325. buffer_write(high_direction);
  1326. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
  1327. }
  1328. static int ft2232_execute_runtest(struct jtag_command *cmd)
  1329. {
  1330. int retval;
  1331. int i;
  1332. int predicted_size = 0;
  1333. retval = ERROR_OK;
  1334. DEBUG_JTAG_IO("runtest %i cycles, end in %s",
  1335. cmd->cmd.runtest->num_cycles,
  1336. tap_state_name(cmd->cmd.runtest->end_state));
  1337. /* only send the maximum buffer size that FT2232C can handle */
  1338. predicted_size = 0;
  1339. if (tap_get_state() != TAP_IDLE)
  1340. predicted_size += 3;
  1341. predicted_size += 3 * DIV_ROUND_UP(cmd->cmd.runtest->num_cycles, 7);
  1342. if (cmd->cmd.runtest->end_state != TAP_IDLE)
  1343. predicted_size += 3;
  1344. if (tap_get_end_state() != TAP_IDLE)
  1345. predicted_size += 3;
  1346. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1347. {
  1348. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1349. retval = ERROR_JTAG_QUEUE_FAILED;
  1350. require_send = 0;
  1351. first_unsent = cmd;
  1352. }
  1353. if (tap_get_state() != TAP_IDLE)
  1354. {
  1355. move_to_state(TAP_IDLE);
  1356. require_send = 1;
  1357. }
  1358. i = cmd->cmd.runtest->num_cycles;
  1359. while (i > 0)
  1360. {
  1361. /* there are no state transitions in this code, so omit state tracking */
  1362. /* command "Clock Data to TMS/CS Pin (no Read)" */
  1363. buffer_write(0x4b);
  1364. /* scan 7 bits */
  1365. buffer_write((i > 7) ? 6 : (i - 1));
  1366. /* TMS data bits */
  1367. buffer_write(0x0);
  1368. i -= (i > 7) ? 7 : i;
  1369. /* LOG_DEBUG("added TMS scan (no read)"); */
  1370. }
  1371. ft2232_end_state(cmd->cmd.runtest->end_state);
  1372. if (tap_get_state() != tap_get_end_state())
  1373. {
  1374. move_to_state(tap_get_end_state());
  1375. }
  1376. require_send = 1;
  1377. DEBUG_JTAG_IO("runtest: %i, end in %s",
  1378. cmd->cmd.runtest->num_cycles,
  1379. tap_state_name(tap_get_end_state()));
  1380. return retval;
  1381. }
  1382. static int ft2232_execute_statemove(struct jtag_command *cmd)
  1383. {
  1384. int predicted_size = 0;
  1385. int retval = ERROR_OK;
  1386. DEBUG_JTAG_IO("statemove end in %s",
  1387. tap_state_name(cmd->cmd.statemove->end_state));
  1388. /* only send the maximum buffer size that FT2232C can handle */
  1389. predicted_size = 3;
  1390. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1391. {
  1392. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1393. retval = ERROR_JTAG_QUEUE_FAILED;
  1394. require_send = 0;
  1395. first_unsent = cmd;
  1396. }
  1397. ft2232_end_state(cmd->cmd.statemove->end_state);
  1398. /* For TAP_RESET, ignore the current recorded state. It's often
  1399. * wrong at server startup, and this transation is critical whenever
  1400. * it's requested.
  1401. */
  1402. if (tap_get_end_state() == TAP_RESET) {
  1403. clock_tms(0x4b, 0xff, 5, 0);
  1404. require_send = 1;
  1405. /* shortest-path move to desired end state */
  1406. } else if (tap_get_state() != tap_get_end_state())
  1407. {
  1408. move_to_state(tap_get_end_state());
  1409. require_send = 1;
  1410. }
  1411. return retval;
  1412. }
  1413. static int ft2232_execute_pathmove(struct jtag_command *cmd)
  1414. {
  1415. int predicted_size = 0;
  1416. int retval = ERROR_OK;
  1417. tap_state_t* path = cmd->cmd.pathmove->path;
  1418. int num_states = cmd->cmd.pathmove->num_states;
  1419. DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
  1420. tap_state_name(tap_get_state()),
  1421. tap_state_name(path[num_states-1]));
  1422. /* only send the maximum buffer size that FT2232C can handle */
  1423. predicted_size = 3 * DIV_ROUND_UP(num_states, 7);
  1424. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1425. {
  1426. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1427. retval = ERROR_JTAG_QUEUE_FAILED;
  1428. require_send = 0;
  1429. first_unsent = cmd;
  1430. }
  1431. ft2232_add_pathmove(path, num_states);
  1432. require_send = 1;
  1433. return retval;
  1434. }
  1435. static int ft2232_execute_scan(struct jtag_command *cmd)
  1436. {
  1437. uint8_t* buffer;
  1438. int scan_size; /* size of IR or DR scan */
  1439. int predicted_size = 0;
  1440. int retval = ERROR_OK;
  1441. enum scan_type type = jtag_scan_type(cmd->cmd.scan);
  1442. DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN", type);
  1443. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  1444. predicted_size = ft2232_predict_scan_out(scan_size, type);
  1445. if ((predicted_size + 1) > FT2232_BUFFER_SIZE)
  1446. {
  1447. LOG_DEBUG("oversized ft2232 scan (predicted_size > FT2232_BUFFER_SIZE)");
  1448. /* unsent commands before this */
  1449. if (first_unsent != cmd)
  1450. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1451. retval = ERROR_JTAG_QUEUE_FAILED;
  1452. /* current command */
  1453. ft2232_end_state(cmd->cmd.scan->end_state);
  1454. ft2232_large_scan(cmd->cmd.scan, type, buffer, scan_size);
  1455. require_send = 0;
  1456. first_unsent = cmd->next;
  1457. if (buffer)
  1458. free(buffer);
  1459. return retval;
  1460. }
  1461. else if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1462. {
  1463. LOG_DEBUG("ft2232 buffer size reached, sending queued commands (first_unsent: %p, cmd: %p)",
  1464. first_unsent,
  1465. cmd);
  1466. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1467. retval = ERROR_JTAG_QUEUE_FAILED;
  1468. require_send = 0;
  1469. first_unsent = cmd;
  1470. }
  1471. ft2232_expect_read += ft2232_predict_scan_in(scan_size, type);
  1472. /* LOG_DEBUG("new read size: %i", ft2232_expect_read); */
  1473. ft2232_end_state(cmd->cmd.scan->end_state);
  1474. ft2232_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  1475. require_send = 1;
  1476. if (buffer)
  1477. free(buffer);
  1478. DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
  1479. (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
  1480. tap_state_name(tap_get_end_state()));
  1481. return retval;
  1482. }
  1483. static int ft2232_execute_reset(struct jtag_command *cmd)
  1484. {
  1485. int retval;
  1486. int predicted_size = 0;
  1487. retval = ERROR_OK;
  1488. DEBUG_JTAG_IO("reset trst: %i srst %i",
  1489. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1490. /* only send the maximum buffer size that FT2232C can handle */
  1491. predicted_size = 3;
  1492. if (ft2232_buffer_size + predicted_size + 1 > FT2232_BUFFER_SIZE)
  1493. {
  1494. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1495. retval = ERROR_JTAG_QUEUE_FAILED;
  1496. require_send = 0;
  1497. first_unsent = cmd;
  1498. }
  1499. if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
  1500. {
  1501. tap_set_state(TAP_RESET);
  1502. }
  1503. layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1504. require_send = 1;
  1505. DEBUG_JTAG_IO("trst: %i, srst: %i",
  1506. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  1507. return retval;
  1508. }
  1509. static int ft2232_execute_sleep(struct jtag_command *cmd)
  1510. {
  1511. int retval;
  1512. retval = ERROR_OK;
  1513. DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
  1514. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1515. retval = ERROR_JTAG_QUEUE_FAILED;
  1516. first_unsent = cmd->next;
  1517. jtag_sleep(cmd->cmd.sleep->us);
  1518. DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
  1519. cmd->cmd.sleep->us,
  1520. tap_state_name(tap_get_state()));
  1521. return retval;
  1522. }
  1523. static int ft2232_execute_stableclocks(struct jtag_command *cmd)
  1524. {
  1525. int retval;
  1526. retval = ERROR_OK;
  1527. /* this is only allowed while in a stable state. A check for a stable
  1528. * state was done in jtag_add_clocks()
  1529. */
  1530. if (ft2232_stableclocks(cmd->cmd.stableclocks->num_cycles, cmd) != ERROR_OK)
  1531. retval = ERROR_JTAG_QUEUE_FAILED;
  1532. DEBUG_JTAG_IO("clocks %i while in %s",
  1533. cmd->cmd.stableclocks->num_cycles,
  1534. tap_state_name(tap_get_state()));
  1535. return retval;
  1536. }
  1537. static int ft2232_execute_command(struct jtag_command *cmd)
  1538. {
  1539. int retval;
  1540. retval = ERROR_OK;
  1541. switch (cmd->type)
  1542. {
  1543. case JTAG_RESET: retval = ft2232_execute_reset(cmd); break;
  1544. case JTAG_RUNTEST: retval = ft2232_execute_runtest(cmd); break;
  1545. case JTAG_STATEMOVE: retval = ft2232_execute_statemove(cmd); break;
  1546. case JTAG_PATHMOVE: retval = ft2232_execute_pathmove(cmd); break;
  1547. case JTAG_SCAN: retval = ft2232_execute_scan(cmd); break;
  1548. case JTAG_SLEEP: retval = ft2232_execute_sleep(cmd); break;
  1549. case JTAG_STABLECLOCKS: retval = ft2232_execute_stableclocks(cmd); break;
  1550. default:
  1551. LOG_ERROR("BUG: unknown JTAG command type encountered");
  1552. exit(-1);
  1553. }
  1554. return retval;
  1555. }
  1556. static int ft2232_execute_queue(void)
  1557. {
  1558. struct jtag_command* cmd = jtag_command_queue; /* currently processed command */
  1559. int retval;
  1560. first_unsent = cmd; /* next command that has to be sent */
  1561. require_send = 0;
  1562. /* return ERROR_OK, unless ft2232_send_and_recv reports a failed check
  1563. * that wasn't handled by a caller-provided error handler
  1564. */
  1565. retval = ERROR_OK;
  1566. ft2232_buffer_size = 0;
  1567. ft2232_expect_read = 0;
  1568. /* blink, if the current layout has that feature */
  1569. if (layout->blink)
  1570. layout->blink();
  1571. while (cmd)
  1572. {
  1573. if (ft2232_execute_command(cmd) != ERROR_OK)
  1574. retval = ERROR_JTAG_QUEUE_FAILED;
  1575. /* Start reading input before FT2232 TX buffer fills up */
  1576. cmd = cmd->next;
  1577. if (ft2232_expect_read > 256)
  1578. {
  1579. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1580. retval = ERROR_JTAG_QUEUE_FAILED;
  1581. first_unsent = cmd;
  1582. }
  1583. }
  1584. if (require_send > 0)
  1585. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  1586. retval = ERROR_JTAG_QUEUE_FAILED;
  1587. return retval;
  1588. }
  1589. #if BUILD_FT2232_FTD2XX == 1
  1590. static int ft2232_init_ftd2xx(uint16_t vid, uint16_t pid, int more, int* try_more)
  1591. {
  1592. FT_STATUS status;
  1593. DWORD deviceID;
  1594. char SerialNumber[16];
  1595. char Description[64];
  1596. DWORD openex_flags = 0;
  1597. char* openex_string = NULL;
  1598. uint8_t latency_timer;
  1599. LOG_DEBUG("'ft2232' interface using FTD2XX with '%s' layout (%4.4x:%4.4x)", ft2232_layout, vid, pid);
  1600. #if IS_WIN32 == 0
  1601. /* Add non-standard Vid/Pid to the linux driver */
  1602. if ((status = FT_SetVIDPID(vid, pid)) != FT_OK)
  1603. {
  1604. LOG_WARNING("couldn't add %4.4x:%4.4x", vid, pid);
  1605. }
  1606. #endif
  1607. if (ft2232_device_desc && ft2232_serial)
  1608. {
  1609. LOG_WARNING("can't open by device description and serial number, giving precedence to serial");
  1610. ft2232_device_desc = NULL;
  1611. }
  1612. if (ft2232_device_desc)
  1613. {
  1614. openex_string = ft2232_device_desc;
  1615. openex_flags = FT_OPEN_BY_DESCRIPTION;
  1616. }
  1617. else if (ft2232_serial)
  1618. {
  1619. openex_string = ft2232_serial;
  1620. openex_flags = FT_OPEN_BY_SERIAL_NUMBER;
  1621. }
  1622. else
  1623. {
  1624. LOG_ERROR("neither device description nor serial number specified");
  1625. LOG_ERROR("please add \"ft2232_device_desc <string>\" or \"ft2232_serial <string>\" to your .cfg file");
  1626. return ERROR_JTAG_INIT_FAILED;
  1627. }
  1628. status = FT_OpenEx(openex_string, openex_flags, &ftdih);
  1629. if (status != FT_OK) {
  1630. /* under Win32, the FTD2XX driver appends an "A" to the end
  1631. * of the description, if we tried by the desc, then
  1632. * try by the alternate "A" description. */
  1633. if (openex_string == ft2232_device_desc) {
  1634. /* Try the alternate method. */
  1635. openex_string = ft2232_device_desc_A;
  1636. status = FT_OpenEx(openex_string, openex_flags, &ftdih);
  1637. if (status == FT_OK) {
  1638. /* yea, the "alternate" method worked! */
  1639. } else {
  1640. /* drat, give the user a meaningfull message.
  1641. * telling the use we tried *BOTH* methods. */
  1642. LOG_WARNING("Unable to open FTDI Device tried: '%s' and '%s'\n",
  1643. ft2232_device_desc,
  1644. ft2232_device_desc_A);
  1645. }
  1646. }
  1647. }
  1648. if (status != FT_OK)
  1649. {
  1650. DWORD num_devices;
  1651. if (more)
  1652. {
  1653. LOG_WARNING("unable to open ftdi device (trying more): %lu", status);
  1654. *try_more = 1;
  1655. return ERROR_JTAG_INIT_FAILED;
  1656. }
  1657. LOG_ERROR("unable to open ftdi device: %lu", status);
  1658. status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
  1659. if (status == FT_OK)
  1660. {
  1661. char** desc_array = malloc(sizeof(char*) * (num_devices + 1));
  1662. uint32_t i;
  1663. for (i = 0; i < num_devices; i++)
  1664. desc_array[i] = malloc(64);
  1665. desc_array[num_devices] = NULL;
  1666. status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | openex_flags);
  1667. if (status == FT_OK)
  1668. {
  1669. LOG_ERROR("ListDevices: %lu\n", num_devices);
  1670. for (i = 0; i < num_devices; i++)
  1671. LOG_ERROR("%" PRIu32 ": \"%s\"", i, desc_array[i]);
  1672. }
  1673. for (i = 0; i < num_devices; i++)
  1674. free(desc_array[i]);
  1675. free(desc_array);
  1676. }
  1677. else
  1678. {
  1679. LOG_ERROR("ListDevices: NONE\n");
  1680. }
  1681. return ERROR_JTAG_INIT_FAILED;
  1682. }
  1683. if ((status = FT_SetLatencyTimer(ftdih, ft2232_latency)) != FT_OK)
  1684. {
  1685. LOG_ERROR("unable to set latency timer: %lu", status);
  1686. return ERROR_JTAG_INIT_FAILED;
  1687. }
  1688. if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
  1689. {
  1690. LOG_ERROR("unable to get latency timer: %lu", status);
  1691. return ERROR_JTAG_INIT_FAILED;
  1692. }
  1693. else
  1694. {
  1695. LOG_DEBUG("current latency timer: %i", latency_timer);
  1696. }
  1697. if ((status = FT_SetTimeouts(ftdih, 5000, 5000)) != FT_OK)
  1698. {
  1699. LOG_ERROR("unable to set timeouts: %lu", status);
  1700. return ERROR_JTAG_INIT_FAILED;
  1701. }
  1702. if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
  1703. {
  1704. LOG_ERROR("unable to enable bit i/o mode: %lu", status);
  1705. return ERROR_JTAG_INIT_FAILED;
  1706. }
  1707. if ((status = FT_GetDeviceInfo(ftdih, &ftdi_device, &deviceID, SerialNumber, Description, NULL)) != FT_OK)
  1708. {
  1709. LOG_ERROR("unable to get FT_GetDeviceInfo: %lu", status);
  1710. return ERROR_JTAG_INIT_FAILED;
  1711. }
  1712. else
  1713. {
  1714. static const char* type_str[] =
  1715. {"BM", "AM", "100AX", "UNKNOWN", "2232C", "232R", "2232H", "4232H"};
  1716. unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
  1717. unsigned type_index = ((unsigned)ftdi_device <= no_of_known_types)
  1718. ? ftdi_device : FT_DEVICE_UNKNOWN;
  1719. LOG_INFO("device: %lu \"%s\"", ftdi_device, type_str[type_index]);
  1720. LOG_INFO("deviceID: %lu", deviceID);
  1721. LOG_INFO("SerialNumber: %s", SerialNumber);
  1722. LOG_INFO("Description: %s", Description);
  1723. }
  1724. return ERROR_OK;
  1725. }
  1726. static int ft2232_purge_ftd2xx(void)
  1727. {
  1728. FT_STATUS status;
  1729. if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
  1730. {
  1731. LOG_ERROR("error purging ftd2xx device: %lu", status);
  1732. return ERROR_JTAG_INIT_FAILED;
  1733. }
  1734. return ERROR_OK;
  1735. }
  1736. #endif /* BUILD_FT2232_FTD2XX == 1 */
  1737. #if BUILD_FT2232_LIBFTDI == 1
  1738. static int ft2232_init_libftdi(uint16_t vid, uint16_t pid, int more, int* try_more)
  1739. {
  1740. uint8_t latency_timer;
  1741. LOG_DEBUG("'ft2232' interface using libftdi with '%s' layout (%4.4x:%4.4x)",
  1742. ft2232_layout, vid, pid);
  1743. if (ftdi_init(&ftdic) < 0)
  1744. return ERROR_JTAG_INIT_FAILED;
  1745. if (ftdi_set_interface(&ftdic, INTERFACE_A) < 0)
  1746. {
  1747. LOG_ERROR("unable to select FT2232 channel A: %s", ftdic.error_str);
  1748. return ERROR_JTAG_INIT_FAILED;
  1749. }
  1750. /* context, vendor id, product id */
  1751. if (ftdi_usb_open_desc(&ftdic, vid, pid, ft2232_device_desc,
  1752. ft2232_serial) < 0)
  1753. {
  1754. if (more)
  1755. LOG_WARNING("unable to open ftdi device (trying more): %s",
  1756. ftdic.error_str);
  1757. else
  1758. LOG_ERROR("unable to open ftdi device: %s", ftdic.error_str);
  1759. *try_more = 1;
  1760. return ERROR_JTAG_INIT_FAILED;
  1761. }
  1762. /* There is already a reset in ftdi_usb_open_desc, this should be redundant */
  1763. if (ftdi_usb_reset(&ftdic) < 0)
  1764. {
  1765. LOG_ERROR("unable to reset ftdi device");
  1766. return ERROR_JTAG_INIT_FAILED;
  1767. }
  1768. if (ftdi_set_latency_timer(&ftdic, ft2232_latency) < 0)
  1769. {
  1770. LOG_ERROR("unable to set latency timer");
  1771. return ERROR_JTAG_INIT_FAILED;
  1772. }
  1773. if (ftdi_get_latency_timer(&ftdic, &latency_timer) < 0)
  1774. {
  1775. LOG_ERROR("unable to get latency timer");
  1776. return ERROR_JTAG_INIT_FAILED;
  1777. }
  1778. else
  1779. {
  1780. LOG_DEBUG("current latency timer: %i", latency_timer);
  1781. }
  1782. ftdi_set_bitmode(&ftdic, 0x0b, 2); /* ctx, JTAG I/O mask */
  1783. ftdi_device = ftdic.type;
  1784. static const char* type_str[] =
  1785. {"AM", "BM", "2232C", "R", "2232H", "4232H", "Unknown"};
  1786. unsigned no_of_known_types = ARRAY_SIZE(type_str) - 1;
  1787. unsigned type_index = ((unsigned)ftdi_device < no_of_known_types)
  1788. ? ftdi_device : no_of_known_types;
  1789. LOG_DEBUG("FTDI chip type: %i \"%s\"", (int)ftdi_device, type_str[type_index]);
  1790. return ERROR_OK;
  1791. }
  1792. static int ft2232_purge_libftdi(void)
  1793. {
  1794. if (ftdi_usb_purge_buffers(&ftdic) < 0)
  1795. {
  1796. LOG_ERROR("ftdi_purge_buffers: %s", ftdic.error_str);
  1797. return ERROR_JTAG_INIT_FAILED;
  1798. }
  1799. return ERROR_OK;
  1800. }
  1801. #endif /* BUILD_FT2232_LIBFTDI == 1 */
  1802. static int ft2232_init(void)
  1803. {
  1804. uint8_t buf[1];
  1805. int retval;
  1806. uint32_t bytes_written;
  1807. const struct ft2232_layout* cur_layout = ft2232_layouts;
  1808. int i;
  1809. if (tap_get_tms_path_len(TAP_IRPAUSE,TAP_IRPAUSE) == 7)
  1810. {
  1811. LOG_DEBUG("ft2232 interface using 7 step jtag state transitions");
  1812. }
  1813. else
  1814. {
  1815. LOG_DEBUG("ft2232 interface using shortest path jtag state transitions");
  1816. }
  1817. if ((ft2232_layout == NULL) || (ft2232_layout[0] == 0))
  1818. {
  1819. ft2232_layout = "usbjtag";
  1820. LOG_WARNING("No ft2232 layout specified, using default 'usbjtag'");
  1821. }
  1822. while (cur_layout->name)
  1823. {
  1824. if (strcmp(cur_layout->name, ft2232_layout) == 0)
  1825. {
  1826. layout = cur_layout;
  1827. break;
  1828. }
  1829. cur_layout++;
  1830. }
  1831. if (!layout)
  1832. {
  1833. LOG_ERROR("No matching layout found for %s", ft2232_layout);
  1834. return ERROR_JTAG_INIT_FAILED;
  1835. }
  1836. for (i = 0; 1; i++)
  1837. {
  1838. /*
  1839. * "more indicates that there are more IDs to try, so we should
  1840. * not print an error for an ID mismatch (but for anything
  1841. * else, we should).
  1842. *
  1843. * try_more indicates that the error code returned indicates an
  1844. * ID mismatch (and nothing else) and that we should proceeed
  1845. * with the next ID pair.
  1846. */
  1847. int more = ft2232_vid[i + 1] || ft2232_pid[i + 1];
  1848. int try_more = 0;
  1849. #if BUILD_FT2232_FTD2XX == 1
  1850. retval = ft2232_init_ftd2xx(ft2232_vid[i], ft2232_pid[i],
  1851. more, &try_more);
  1852. #elif BUILD_FT2232_LIBFTDI == 1
  1853. retval = ft2232_init_libftdi(ft2232_vid[i], ft2232_pid[i],
  1854. more, &try_more);
  1855. #endif
  1856. if (retval >= 0)
  1857. break;
  1858. if (!more || !try_more)
  1859. return retval;
  1860. }
  1861. ft2232_buffer_size = 0;
  1862. ft2232_buffer = malloc(FT2232_BUFFER_SIZE);
  1863. if (layout->init() != ERROR_OK)
  1864. return ERROR_JTAG_INIT_FAILED;
  1865. if (ft2232_device_is_highspeed())
  1866. {
  1867. #ifndef BUILD_FT2232_HIGHSPEED
  1868. #if BUILD_FT2232_FTD2XX == 1
  1869. LOG_WARNING("High Speed device found - You need a newer FTD2XX driver (version 2.04.16 or later)");
  1870. #elif BUILD_FT2232_LIBFTDI == 1
  1871. LOG_WARNING("High Speed device found - You need a newer libftdi version (0.16 or later)");
  1872. #endif
  1873. #endif
  1874. /* make sure the legacy mode is disabled */
  1875. if (ft2232h_ft4232h_clk_divide_by_5(false) != ERROR_OK)
  1876. return ERROR_JTAG_INIT_FAILED;
  1877. }
  1878. ft2232_speed(jtag_get_speed());
  1879. buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
  1880. if (((retval = ft2232_write(buf, 1, &bytes_written)) != ERROR_OK) || (bytes_written != 1))
  1881. {
  1882. LOG_ERROR("couldn't write to FT2232 to disable loopback");
  1883. return ERROR_JTAG_INIT_FAILED;
  1884. }
  1885. #if BUILD_FT2232_FTD2XX == 1
  1886. return ft2232_purge_ftd2xx();
  1887. #elif BUILD_FT2232_LIBFTDI == 1
  1888. return ft2232_purge_libftdi();
  1889. #endif
  1890. return ERROR_OK;
  1891. }
  1892. static int usbjtag_init(void)
  1893. {
  1894. uint8_t buf[3];
  1895. uint32_t bytes_written;
  1896. low_output = 0x08;
  1897. low_direction = 0x0b;
  1898. if (strcmp(ft2232_layout, "usbjtag") == 0)
  1899. {
  1900. nTRST = 0x10;
  1901. nTRSTnOE = 0x10;
  1902. nSRST = 0x40;
  1903. nSRSTnOE = 0x40;
  1904. }
  1905. else if (strcmp(ft2232_layout, "signalyzer") == 0)
  1906. {
  1907. nTRST = 0x10;
  1908. nTRSTnOE = 0x10;
  1909. nSRST = 0x20;
  1910. nSRSTnOE = 0x20;
  1911. }
  1912. else if (strcmp(ft2232_layout, "evb_lm3s811") == 0)
  1913. {
  1914. /* There are multiple revisions of LM3S811 eval boards:
  1915. * - Rev B (and older?) boards have no SWO trace support.
  1916. * - Rev C boards add ADBUS_6 DBG_ENn and BDBUS_4 SWO_EN;
  1917. * they should use the "luminary_icdi" layout instead.
  1918. */
  1919. nTRST = 0x0;
  1920. nTRSTnOE = 0x00;
  1921. nSRST = 0x20;
  1922. nSRSTnOE = 0x20;
  1923. low_output = 0x88;
  1924. low_direction = 0x8b;
  1925. }
  1926. else if (strcmp(ft2232_layout, "luminary_icdi") == 0)
  1927. {
  1928. /* Most Luminary eval boards support SWO trace output,
  1929. * and should use this "luminary_icdi" layout.
  1930. */
  1931. nTRST = 0x0;
  1932. nTRSTnOE = 0x00;
  1933. nSRST = 0x20;
  1934. nSRSTnOE = 0x20;
  1935. low_output = 0x88;
  1936. low_direction = 0xcb;
  1937. }
  1938. else
  1939. {
  1940. LOG_ERROR("BUG: usbjtag_init called for unknown layout '%s'", ft2232_layout);
  1941. return ERROR_JTAG_INIT_FAILED;
  1942. }
  1943. enum reset_types jtag_reset_config = jtag_get_reset_config();
  1944. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  1945. {
  1946. low_direction &= ~nTRSTnOE; /* nTRST input */
  1947. low_output &= ~nTRST; /* nTRST = 0 */
  1948. }
  1949. else
  1950. {
  1951. low_direction |= nTRSTnOE; /* nTRST output */
  1952. low_output |= nTRST; /* nTRST = 1 */
  1953. }
  1954. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  1955. {
  1956. low_direction |= nSRSTnOE; /* nSRST output */
  1957. low_output |= nSRST; /* nSRST = 1 */
  1958. }
  1959. else
  1960. {
  1961. low_direction &= ~nSRSTnOE; /* nSRST input */
  1962. low_output &= ~nSRST; /* nSRST = 0 */
  1963. }
  1964. /* initialize low byte for jtag */
  1965. buf[0] = 0x80; /* command "set data bits low byte" */
  1966. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, xRST high) */
  1967. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
  1968. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1969. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1970. {
  1971. LOG_ERROR("couldn't initialize FT2232 with 'USBJTAG' layout");
  1972. return ERROR_JTAG_INIT_FAILED;
  1973. }
  1974. return ERROR_OK;
  1975. }
  1976. static int axm0432_jtag_init(void)
  1977. {
  1978. uint8_t buf[3];
  1979. uint32_t bytes_written;
  1980. low_output = 0x08;
  1981. low_direction = 0x2b;
  1982. /* initialize low byte for jtag */
  1983. buf[0] = 0x80; /* command "set data bits low byte" */
  1984. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  1985. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  1986. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  1987. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  1988. {
  1989. LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  1990. return ERROR_JTAG_INIT_FAILED;
  1991. }
  1992. if (strcmp(layout->name, "axm0432_jtag") == 0)
  1993. {
  1994. nTRST = 0x08;
  1995. nTRSTnOE = 0x0; /* No output enable for TRST*/
  1996. nSRST = 0x04;
  1997. nSRSTnOE = 0x0; /* No output enable for SRST*/
  1998. }
  1999. else
  2000. {
  2001. LOG_ERROR("BUG: axm0432_jtag_init called for non axm0432 layout");
  2002. exit(-1);
  2003. }
  2004. high_output = 0x0;
  2005. high_direction = 0x0c;
  2006. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2007. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  2008. {
  2009. LOG_ERROR("can't set nTRSTOE to push-pull on the Dicarlo jtag");
  2010. }
  2011. else
  2012. {
  2013. high_output |= nTRST;
  2014. }
  2015. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  2016. {
  2017. LOG_ERROR("can't set nSRST to push-pull on the Dicarlo jtag");
  2018. }
  2019. else
  2020. {
  2021. high_output |= nSRST;
  2022. }
  2023. /* initialize high port */
  2024. buf[0] = 0x82; /* command "set data bits high byte" */
  2025. buf[1] = high_output; /* value */
  2026. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2027. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2028. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2029. {
  2030. LOG_ERROR("couldn't initialize FT2232 with 'Dicarlo' layout");
  2031. return ERROR_JTAG_INIT_FAILED;
  2032. }
  2033. return ERROR_OK;
  2034. }
  2035. static int jtagkey_init(void)
  2036. {
  2037. uint8_t buf[3];
  2038. uint32_t bytes_written;
  2039. low_output = 0x08;
  2040. low_direction = 0x1b;
  2041. /* initialize low byte for jtag */
  2042. buf[0] = 0x80; /* command "set data bits low byte" */
  2043. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2044. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2045. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2046. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2047. {
  2048. LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  2049. return ERROR_JTAG_INIT_FAILED;
  2050. }
  2051. if (strcmp(layout->name, "jtagkey") == 0)
  2052. {
  2053. nTRST = 0x01;
  2054. nTRSTnOE = 0x4;
  2055. nSRST = 0x02;
  2056. nSRSTnOE = 0x08;
  2057. }
  2058. else if ((strcmp(layout->name, "jtagkey_prototype_v1") == 0)
  2059. || (strcmp(layout->name, "oocdlink") == 0))
  2060. {
  2061. nTRST = 0x02;
  2062. nTRSTnOE = 0x1;
  2063. nSRST = 0x08;
  2064. nSRSTnOE = 0x04;
  2065. }
  2066. else
  2067. {
  2068. LOG_ERROR("BUG: jtagkey_init called for non jtagkey layout");
  2069. exit(-1);
  2070. }
  2071. high_output = 0x0;
  2072. high_direction = 0x0f;
  2073. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2074. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  2075. {
  2076. high_output |= nTRSTnOE;
  2077. high_output &= ~nTRST;
  2078. }
  2079. else
  2080. {
  2081. high_output &= ~nTRSTnOE;
  2082. high_output |= nTRST;
  2083. }
  2084. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  2085. {
  2086. high_output &= ~nSRSTnOE;
  2087. high_output |= nSRST;
  2088. }
  2089. else
  2090. {
  2091. high_output |= nSRSTnOE;
  2092. high_output &= ~nSRST;
  2093. }
  2094. /* initialize high port */
  2095. buf[0] = 0x82; /* command "set data bits high byte" */
  2096. buf[1] = high_output; /* value */
  2097. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2098. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2099. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2100. {
  2101. LOG_ERROR("couldn't initialize FT2232 with 'JTAGkey' layout");
  2102. return ERROR_JTAG_INIT_FAILED;
  2103. }
  2104. return ERROR_OK;
  2105. }
  2106. static int olimex_jtag_init(void)
  2107. {
  2108. uint8_t buf[3];
  2109. uint32_t bytes_written;
  2110. low_output = 0x08;
  2111. low_direction = 0x1b;
  2112. /* initialize low byte for jtag */
  2113. buf[0] = 0x80; /* command "set data bits low byte" */
  2114. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2115. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2116. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2117. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2118. {
  2119. LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
  2120. return ERROR_JTAG_INIT_FAILED;
  2121. }
  2122. nTRST = 0x01;
  2123. nTRSTnOE = 0x4;
  2124. nSRST = 0x02;
  2125. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2126. high_output = 0x0;
  2127. high_direction = 0x0f;
  2128. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2129. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  2130. {
  2131. high_output |= nTRSTnOE;
  2132. high_output &= ~nTRST;
  2133. }
  2134. else
  2135. {
  2136. high_output &= ~nTRSTnOE;
  2137. high_output |= nTRST;
  2138. }
  2139. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  2140. {
  2141. LOG_ERROR("can't set nSRST to push-pull on the Olimex ARM-USB-OCD");
  2142. }
  2143. else
  2144. {
  2145. high_output &= ~nSRST;
  2146. }
  2147. /* turn red LED on */
  2148. high_output |= 0x08;
  2149. /* initialize high port */
  2150. buf[0] = 0x82; /* command "set data bits high byte" */
  2151. buf[1] = high_output; /* value */
  2152. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2153. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2154. if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK) || (bytes_written != 3))
  2155. {
  2156. LOG_ERROR("couldn't initialize FT2232 with 'Olimex' layout");
  2157. return ERROR_JTAG_INIT_FAILED;
  2158. }
  2159. return ERROR_OK;
  2160. }
  2161. static int flyswatter_init(void)
  2162. {
  2163. uint8_t buf[3];
  2164. uint32_t bytes_written;
  2165. low_output = 0x18;
  2166. low_direction = 0xfb;
  2167. /* initialize low byte for jtag */
  2168. buf[0] = 0x80; /* command "set data bits low byte" */
  2169. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2170. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE[12]=out, n[ST]srst = out */
  2171. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2172. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2173. {
  2174. LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
  2175. return ERROR_JTAG_INIT_FAILED;
  2176. }
  2177. nTRST = 0x10;
  2178. nTRSTnOE = 0x0; /* not output enable for nTRST */
  2179. nSRST = 0x20;
  2180. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2181. high_output = 0x00;
  2182. high_direction = 0x0c;
  2183. /* turn red LED3 on, LED2 off */
  2184. high_output |= 0x08;
  2185. /* initialize high port */
  2186. buf[0] = 0x82; /* command "set data bits high byte" */
  2187. buf[1] = high_output; /* value */
  2188. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2189. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2190. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2191. {
  2192. LOG_ERROR("couldn't initialize FT2232 with 'flyswatter' layout");
  2193. return ERROR_JTAG_INIT_FAILED;
  2194. }
  2195. return ERROR_OK;
  2196. }
  2197. static int turtle_init(void)
  2198. {
  2199. uint8_t buf[3];
  2200. uint32_t bytes_written;
  2201. low_output = 0x08;
  2202. low_direction = 0x5b;
  2203. /* initialize low byte for jtag */
  2204. buf[0] = 0x80; /* command "set data bits low byte" */
  2205. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2206. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2207. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2208. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2209. {
  2210. LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
  2211. return ERROR_JTAG_INIT_FAILED;
  2212. }
  2213. nSRST = 0x40;
  2214. high_output = 0x00;
  2215. high_direction = 0x0C;
  2216. /* initialize high port */
  2217. buf[0] = 0x82; /* command "set data bits high byte" */
  2218. buf[1] = high_output;
  2219. buf[2] = high_direction;
  2220. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2221. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2222. {
  2223. LOG_ERROR("couldn't initialize FT2232 with 'turtelizer2' layout");
  2224. return ERROR_JTAG_INIT_FAILED;
  2225. }
  2226. return ERROR_OK;
  2227. }
  2228. static int comstick_init(void)
  2229. {
  2230. uint8_t buf[3];
  2231. uint32_t bytes_written;
  2232. low_output = 0x08;
  2233. low_direction = 0x0b;
  2234. /* initialize low byte for jtag */
  2235. buf[0] = 0x80; /* command "set data bits low byte" */
  2236. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2237. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2238. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2239. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2240. {
  2241. LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
  2242. return ERROR_JTAG_INIT_FAILED;
  2243. }
  2244. nTRST = 0x01;
  2245. nTRSTnOE = 0x00; /* no output enable for nTRST */
  2246. nSRST = 0x02;
  2247. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2248. high_output = 0x03;
  2249. high_direction = 0x03;
  2250. /* initialize high port */
  2251. buf[0] = 0x82; /* command "set data bits high byte" */
  2252. buf[1] = high_output;
  2253. buf[2] = high_direction;
  2254. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2255. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2256. {
  2257. LOG_ERROR("couldn't initialize FT2232 with 'comstick' layout");
  2258. return ERROR_JTAG_INIT_FAILED;
  2259. }
  2260. return ERROR_OK;
  2261. }
  2262. static int stm32stick_init(void)
  2263. {
  2264. uint8_t buf[3];
  2265. uint32_t bytes_written;
  2266. low_output = 0x88;
  2267. low_direction = 0x8b;
  2268. /* initialize low byte for jtag */
  2269. buf[0] = 0x80; /* command "set data bits low byte" */
  2270. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2271. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2272. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2273. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2274. {
  2275. LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
  2276. return ERROR_JTAG_INIT_FAILED;
  2277. }
  2278. nTRST = 0x01;
  2279. nTRSTnOE = 0x00; /* no output enable for nTRST */
  2280. nSRST = 0x80;
  2281. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2282. high_output = 0x01;
  2283. high_direction = 0x03;
  2284. /* initialize high port */
  2285. buf[0] = 0x82; /* command "set data bits high byte" */
  2286. buf[1] = high_output;
  2287. buf[2] = high_direction;
  2288. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2289. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2290. {
  2291. LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
  2292. return ERROR_JTAG_INIT_FAILED;
  2293. }
  2294. return ERROR_OK;
  2295. }
  2296. static int sheevaplug_init(void)
  2297. {
  2298. uint8_t buf[3];
  2299. uint32_t bytes_written;
  2300. low_output = 0x08;
  2301. low_direction = 0x1b;
  2302. /* initialize low byte for jtag */
  2303. buf[0] = 0x80; /* command "set data bits low byte" */
  2304. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2305. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in */
  2306. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2307. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2308. {
  2309. LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
  2310. return ERROR_JTAG_INIT_FAILED;
  2311. }
  2312. nTRSTnOE = 0x1;
  2313. nTRST = 0x02;
  2314. nSRSTnOE = 0x4;
  2315. nSRST = 0x08;
  2316. high_output = 0x0;
  2317. high_direction = 0x0f;
  2318. /* nTRST is always push-pull */
  2319. high_output &= ~nTRSTnOE;
  2320. high_output |= nTRST;
  2321. /* nSRST is always open-drain */
  2322. high_output |= nSRSTnOE;
  2323. high_output &= ~nSRST;
  2324. /* initialize high port */
  2325. buf[0] = 0x82; /* command "set data bits high byte" */
  2326. buf[1] = high_output; /* value */
  2327. buf[2] = high_direction; /* all outputs - xRST */
  2328. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2329. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2330. {
  2331. LOG_ERROR("couldn't initialize FT2232 with 'sheevaplug' layout");
  2332. return ERROR_JTAG_INIT_FAILED;
  2333. }
  2334. return ERROR_OK;
  2335. }
  2336. static int cortino_jtag_init(void)
  2337. {
  2338. uint8_t buf[3];
  2339. uint32_t bytes_written;
  2340. low_output = 0x08;
  2341. low_direction = 0x1b;
  2342. /* initialize low byte for jtag */
  2343. buf[0] = 0x80; /* command "set data bits low byte" */
  2344. buf[1] = low_output; /* value (TMS = 1,TCK = 0, TDI = 0, nOE = 0) */
  2345. buf[2] = low_direction; /* dir (output = 1), TCK/TDI/TMS = out, TDO = in, nOE = out */
  2346. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2347. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2348. {
  2349. LOG_ERROR("couldn't initialize FT2232 with 'cortino' layout");
  2350. return ERROR_JTAG_INIT_FAILED;
  2351. }
  2352. nTRST = 0x01;
  2353. nTRSTnOE = 0x00; /* no output enable for nTRST */
  2354. nSRST = 0x02;
  2355. nSRSTnOE = 0x00; /* no output enable for nSRST */
  2356. high_output = 0x03;
  2357. high_direction = 0x03;
  2358. /* initialize high port */
  2359. buf[0] = 0x82; /* command "set data bits high byte" */
  2360. buf[1] = high_output;
  2361. buf[2] = high_direction;
  2362. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2363. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  2364. {
  2365. LOG_ERROR("couldn't initialize FT2232 with 'stm32stick' layout");
  2366. return ERROR_JTAG_INIT_FAILED;
  2367. }
  2368. return ERROR_OK;
  2369. }
  2370. static void olimex_jtag_blink(void)
  2371. {
  2372. /* Olimex ARM-USB-OCD has a LED connected to ACBUS3
  2373. * ACBUS3 is bit 3 of the GPIOH port
  2374. */
  2375. if (high_output & 0x08)
  2376. {
  2377. /* set port pin high */
  2378. high_output &= 0x07;
  2379. }
  2380. else
  2381. {
  2382. /* set port pin low */
  2383. high_output |= 0x08;
  2384. }
  2385. buffer_write(0x82);
  2386. buffer_write(high_output);
  2387. buffer_write(high_direction);
  2388. }
  2389. static void flyswatter_jtag_blink(void)
  2390. {
  2391. /*
  2392. * Flyswatter has two LEDs connected to ACBUS2 and ACBUS3
  2393. */
  2394. high_output ^= 0x0c;
  2395. buffer_write(0x82);
  2396. buffer_write(high_output);
  2397. buffer_write(high_direction);
  2398. }
  2399. static void turtle_jtag_blink(void)
  2400. {
  2401. /*
  2402. * Turtelizer2 has two LEDs connected to ACBUS2 and ACBUS3
  2403. */
  2404. if (high_output & 0x08)
  2405. {
  2406. high_output = 0x04;
  2407. }
  2408. else
  2409. {
  2410. high_output = 0x08;
  2411. }
  2412. buffer_write(0x82);
  2413. buffer_write(high_output);
  2414. buffer_write(high_direction);
  2415. }
  2416. static int ft2232_quit(void)
  2417. {
  2418. #if BUILD_FT2232_FTD2XX == 1
  2419. FT_STATUS status;
  2420. status = FT_Close(ftdih);
  2421. #elif BUILD_FT2232_LIBFTDI == 1
  2422. ftdi_usb_close(&ftdic);
  2423. ftdi_deinit(&ftdic);
  2424. #endif
  2425. free(ft2232_buffer);
  2426. ft2232_buffer = NULL;
  2427. return ERROR_OK;
  2428. }
  2429. COMMAND_HANDLER(ft2232_handle_device_desc_command)
  2430. {
  2431. char *cp;
  2432. char buf[200];
  2433. if (CMD_ARGC == 1)
  2434. {
  2435. ft2232_device_desc = strdup(CMD_ARGV[0]);
  2436. cp = strchr(ft2232_device_desc, 0);
  2437. /* under Win32, the FTD2XX driver appends an "A" to the end
  2438. * of the description, this examines the given desc
  2439. * and creates the 'missing' _A or non_A variable. */
  2440. if ((cp[-1] == 'A') && (cp[-2]==' ')) {
  2441. /* it was, so make this the "A" version. */
  2442. ft2232_device_desc_A = ft2232_device_desc;
  2443. /* and *CREATE* the non-A version. */
  2444. strcpy(buf, ft2232_device_desc);
  2445. cp = strchr(buf, 0);
  2446. cp[-2] = 0;
  2447. ft2232_device_desc = strdup(buf);
  2448. } else {
  2449. /* <space > A not defined
  2450. * so create it */
  2451. sprintf(buf, "%s A", ft2232_device_desc);
  2452. ft2232_device_desc_A = strdup(buf);
  2453. }
  2454. }
  2455. else
  2456. {
  2457. LOG_ERROR("expected exactly one argument to ft2232_device_desc <description>");
  2458. }
  2459. return ERROR_OK;
  2460. }
  2461. COMMAND_HANDLER(ft2232_handle_serial_command)
  2462. {
  2463. if (CMD_ARGC == 1)
  2464. {
  2465. ft2232_serial = strdup(CMD_ARGV[0]);
  2466. }
  2467. else
  2468. {
  2469. LOG_ERROR("expected exactly one argument to ft2232_serial <serial-number>");
  2470. }
  2471. return ERROR_OK;
  2472. }
  2473. COMMAND_HANDLER(ft2232_handle_layout_command)
  2474. {
  2475. if (CMD_ARGC == 0)
  2476. return ERROR_OK;
  2477. ft2232_layout = malloc(strlen(CMD_ARGV[0]) + 1);
  2478. strcpy(ft2232_layout, CMD_ARGV[0]);
  2479. return ERROR_OK;
  2480. }
  2481. COMMAND_HANDLER(ft2232_handle_vid_pid_command)
  2482. {
  2483. if (CMD_ARGC > MAX_USB_IDS * 2)
  2484. {
  2485. LOG_WARNING("ignoring extra IDs in ft2232_vid_pid "
  2486. "(maximum is %d pairs)", MAX_USB_IDS);
  2487. CMD_ARGC = MAX_USB_IDS * 2;
  2488. }
  2489. if (CMD_ARGC < 2 || (CMD_ARGC & 1))
  2490. {
  2491. LOG_WARNING("incomplete ft2232_vid_pid configuration directive");
  2492. if (CMD_ARGC < 2)
  2493. return ERROR_COMMAND_SYNTAX_ERROR;
  2494. /* remove the incomplete trailing id */
  2495. CMD_ARGC -= 1;
  2496. }
  2497. unsigned i;
  2498. for (i = 0; i < CMD_ARGC; i += 2)
  2499. {
  2500. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ft2232_vid[i >> 1]);
  2501. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ft2232_pid[i >> 1]);
  2502. }
  2503. /*
  2504. * Explicitly terminate, in case there are multiples instances of
  2505. * ft2232_vid_pid.
  2506. */
  2507. ft2232_vid[i >> 1] = ft2232_pid[i >> 1] = 0;
  2508. return ERROR_OK;
  2509. }
  2510. COMMAND_HANDLER(ft2232_handle_latency_command)
  2511. {
  2512. if (CMD_ARGC == 1)
  2513. {
  2514. ft2232_latency = atoi(CMD_ARGV[0]);
  2515. }
  2516. else
  2517. {
  2518. LOG_ERROR("expected exactly one argument to ft2232_latency <ms>");
  2519. }
  2520. return ERROR_OK;
  2521. }
  2522. static int ft2232_stableclocks(int num_cycles, struct jtag_command* cmd)
  2523. {
  2524. int retval = 0;
  2525. /* 7 bits of either ones or zeros. */
  2526. uint8_t tms = (tap_get_state() == TAP_RESET ? 0x7F : 0x00);
  2527. while (num_cycles > 0)
  2528. {
  2529. /* the command 0x4b, "Clock Data to TMS/CS Pin (no Read)" handles
  2530. * at most 7 bits per invocation. Here we invoke it potentially
  2531. * several times.
  2532. */
  2533. int bitcount_per_command = (num_cycles > 7) ? 7 : num_cycles;
  2534. if (ft2232_buffer_size + 3 >= FT2232_BUFFER_SIZE)
  2535. {
  2536. if (ft2232_send_and_recv(first_unsent, cmd) != ERROR_OK)
  2537. retval = ERROR_JTAG_QUEUE_FAILED;
  2538. first_unsent = cmd;
  2539. }
  2540. /* there are no state transitions in this code, so omit state tracking */
  2541. /* command "Clock Data to TMS/CS Pin (no Read)" */
  2542. buffer_write(0x4b);
  2543. /* scan 7 bit */
  2544. buffer_write(bitcount_per_command - 1);
  2545. /* TMS data bits are either all zeros or ones to stay in the current stable state */
  2546. buffer_write(tms);
  2547. require_send = 1;
  2548. num_cycles -= bitcount_per_command;
  2549. }
  2550. return retval;
  2551. }
  2552. /* ---------------------------------------------------------------------
  2553. * Support for IceBear JTAG adapter from Section5:
  2554. * http://section5.ch/icebear
  2555. *
  2556. * Author: Sten, debian@sansys-electronic.com
  2557. */
  2558. /* Icebear pin layout
  2559. *
  2560. * ADBUS5 (nEMU) nSRST | 2 1| GND (10k->VCC)
  2561. * GND GND | 4 3| n.c.
  2562. * ADBUS3 TMS | 6 5| ADBUS6 VCC
  2563. * ADBUS0 TCK | 8 7| ADBUS7 (GND)
  2564. * ADBUS4 nTRST |10 9| ACBUS0 (GND)
  2565. * ADBUS1 TDI |12 11| ACBUS1 (GND)
  2566. * ADBUS2 TDO |14 13| GND GND
  2567. *
  2568. * ADBUS0 O L TCK ACBUS0 GND
  2569. * ADBUS1 O L TDI ACBUS1 GND
  2570. * ADBUS2 I TDO ACBUS2 n.c.
  2571. * ADBUS3 O H TMS ACBUS3 n.c.
  2572. * ADBUS4 O H nTRST
  2573. * ADBUS5 O H nSRST
  2574. * ADBUS6 - VCC
  2575. * ADBUS7 - GND
  2576. */
  2577. static int icebear_jtag_init(void) {
  2578. uint8_t buf[3];
  2579. uint32_t bytes_written;
  2580. low_direction = 0x0b; /* output: TCK TDI TMS; input: TDO */
  2581. low_output = 0x08; /* high: TMS; low: TCK TDI */
  2582. nTRST = 0x10;
  2583. nSRST = 0x20;
  2584. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2585. if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0) {
  2586. low_direction &= ~nTRST; /* nTRST high impedance */
  2587. }
  2588. else {
  2589. low_direction |= nTRST;
  2590. low_output |= nTRST;
  2591. }
  2592. low_direction |= nSRST;
  2593. low_output |= nSRST;
  2594. /* initialize low byte for jtag */
  2595. buf[0] = 0x80; /* command "set data bits low byte" */
  2596. buf[1] = low_output;
  2597. buf[2] = low_direction;
  2598. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2599. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
  2600. LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (low)");
  2601. return ERROR_JTAG_INIT_FAILED;
  2602. }
  2603. high_output = 0x0;
  2604. high_direction = 0x00;
  2605. /* initialize high port */
  2606. buf[0] = 0x82; /* command "set data bits high byte" */
  2607. buf[1] = high_output; /* value */
  2608. buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
  2609. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  2610. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3)) {
  2611. LOG_ERROR("couldn't initialize FT2232 with 'IceBear' layout (high)");
  2612. return ERROR_JTAG_INIT_FAILED;
  2613. }
  2614. return ERROR_OK;
  2615. }
  2616. static void icebear_jtag_reset(int trst, int srst) {
  2617. if (trst == 1) {
  2618. low_direction |= nTRST;
  2619. low_output &= ~nTRST;
  2620. }
  2621. else if (trst == 0) {
  2622. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2623. if ((jtag_reset_config & RESET_TRST_OPEN_DRAIN) != 0)
  2624. low_direction &= ~nTRST;
  2625. else
  2626. low_output |= nTRST;
  2627. }
  2628. if (srst == 1) {
  2629. low_output &= ~nSRST;
  2630. }
  2631. else if (srst == 0) {
  2632. low_output |= nSRST;
  2633. }
  2634. /* command "set data bits low byte" */
  2635. buffer_write(0x80);
  2636. buffer_write(low_output);
  2637. buffer_write(low_direction);
  2638. LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, low_direction: 0x%2.2x", trst, srst, low_output, low_direction);
  2639. }
  2640. /* ---------------------------------------------------------------------
  2641. * Support for Signalyzer H2 and Signalyzer H4
  2642. * JTAG adapter from Xverve Technologies Inc.
  2643. * http://www.signalyzer.com or http://www.xverve.com
  2644. *
  2645. * Author: Oleg Seiljus, oleg@signalyzer.com
  2646. */
  2647. static unsigned char signalyzer_h_side;
  2648. static unsigned int signalyzer_h_adapter_type;
  2649. static int signalyzer_h_ctrl_write(int address, unsigned short value);
  2650. #if BUILD_FT2232_FTD2XX == 1
  2651. static int signalyzer_h_ctrl_read(int address, unsigned short *value);
  2652. #endif
  2653. #define SIGNALYZER_COMMAND_ADDR 128
  2654. #define SIGNALYZER_DATA_BUFFER_ADDR 129
  2655. #define SIGNALYZER_COMMAND_VERSION 0x41
  2656. #define SIGNALYZER_COMMAND_RESET 0x42
  2657. #define SIGNALYZER_COMMAND_POWERCONTROL_GET 0x50
  2658. #define SIGNALYZER_COMMAND_POWERCONTROL_SET 0x51
  2659. #define SIGNALYZER_COMMAND_PWM_SET 0x52
  2660. #define SIGNALYZER_COMMAND_LED_SET 0x53
  2661. #define SIGNALYZER_COMMAND_ADC 0x54
  2662. #define SIGNALYZER_COMMAND_GPIO_STATE 0x55
  2663. #define SIGNALYZER_COMMAND_GPIO_MODE 0x56
  2664. #define SIGNALYZER_COMMAND_GPIO_PORT 0x57
  2665. #define SIGNALYZER_COMMAND_I2C 0x58
  2666. #define SIGNALYZER_CHAN_A 1
  2667. #define SIGNALYZER_CHAN_B 2
  2668. /* LEDS use channel C */
  2669. #define SIGNALYZER_CHAN_C 4
  2670. #define SIGNALYZER_LED_GREEN 1
  2671. #define SIGNALYZER_LED_RED 2
  2672. #define SIGNALYZER_MODULE_TYPE_EM_LT16_A 0x0301
  2673. #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG 0x0302
  2674. #define SIGNALYZER_MODULE_TYPE_EM_JTAG 0x0303
  2675. #define SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P 0x0304
  2676. #define SIGNALYZER_MODULE_TYPE_EM_JTAG_P 0x0305
  2677. static int signalyzer_h_ctrl_write(int address, unsigned short value)
  2678. {
  2679. #if BUILD_FT2232_FTD2XX == 1
  2680. return FT_WriteEE(ftdih, address, value);
  2681. #elif BUILD_FT2232_LIBFTDI == 1
  2682. return 0;
  2683. #endif
  2684. }
  2685. #if BUILD_FT2232_FTD2XX == 1
  2686. static int signalyzer_h_ctrl_read(int address, unsigned short *value)
  2687. {
  2688. return FT_ReadEE(ftdih, address, value);
  2689. }
  2690. #endif
  2691. static int signalyzer_h_led_set(unsigned char channel, unsigned char led,
  2692. int on_time_ms, int off_time_ms, unsigned char cycles)
  2693. {
  2694. unsigned char on_time;
  2695. unsigned char off_time;
  2696. if (on_time_ms < 0xFFFF)
  2697. on_time = (unsigned char)(on_time_ms / 62);
  2698. else
  2699. on_time = 0xFF;
  2700. off_time = (unsigned char)(off_time_ms / 62);
  2701. #if BUILD_FT2232_FTD2XX == 1
  2702. FT_STATUS status;
  2703. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
  2704. ((uint32_t)(channel << 8) | led))) != FT_OK)
  2705. {
  2706. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2707. return ERROR_JTAG_DEVICE_ERROR;
  2708. }
  2709. if ((status = signalyzer_h_ctrl_write(
  2710. (SIGNALYZER_DATA_BUFFER_ADDR + 1),
  2711. ((uint32_t)(on_time << 8) | off_time))) != FT_OK)
  2712. {
  2713. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2714. return ERROR_JTAG_DEVICE_ERROR;
  2715. }
  2716. if ((status = signalyzer_h_ctrl_write(
  2717. (SIGNALYZER_DATA_BUFFER_ADDR + 2),
  2718. ((uint32_t)cycles))) != FT_OK)
  2719. {
  2720. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2721. return ERROR_JTAG_DEVICE_ERROR;
  2722. }
  2723. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
  2724. SIGNALYZER_COMMAND_LED_SET)) != FT_OK)
  2725. {
  2726. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2727. return ERROR_JTAG_DEVICE_ERROR;
  2728. }
  2729. return ERROR_OK;
  2730. #elif BUILD_FT2232_LIBFTDI == 1
  2731. int retval;
  2732. if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
  2733. ((uint32_t)(channel << 8) | led))) < 0)
  2734. {
  2735. LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
  2736. ftdi_get_error_string(&ftdic));
  2737. return ERROR_JTAG_DEVICE_ERROR;
  2738. }
  2739. if ((retval = signalyzer_h_ctrl_write(
  2740. (SIGNALYZER_DATA_BUFFER_ADDR + 1),
  2741. ((uint32_t)(on_time << 8) | off_time))) < 0)
  2742. {
  2743. LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
  2744. ftdi_get_error_string(&ftdic));
  2745. return ERROR_JTAG_DEVICE_ERROR;
  2746. }
  2747. if ((retval = signalyzer_h_ctrl_write(
  2748. (SIGNALYZER_DATA_BUFFER_ADDR + 2),
  2749. (uint32_t)cycles)) < 0)
  2750. {
  2751. LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
  2752. ftdi_get_error_string(&ftdic));
  2753. return ERROR_JTAG_DEVICE_ERROR;
  2754. }
  2755. if ((retval = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
  2756. SIGNALYZER_COMMAND_LED_SET)) < 0)
  2757. {
  2758. LOG_ERROR("signalyzer_h_ctrl_write returned: %s",
  2759. ftdi_get_error_string(&ftdic));
  2760. return ERROR_JTAG_DEVICE_ERROR;
  2761. }
  2762. return ERROR_OK;
  2763. #endif
  2764. }
  2765. static int signalyzer_h_init(void)
  2766. {
  2767. #if BUILD_FT2232_FTD2XX == 1
  2768. FT_STATUS status;
  2769. int i;
  2770. #endif
  2771. char *end_of_desc;
  2772. uint16_t read_buf[12] = { 0 };
  2773. uint8_t buf[3];
  2774. uint32_t bytes_written;
  2775. /* turn on center green led */
  2776. signalyzer_h_led_set(SIGNALYZER_CHAN_C, SIGNALYZER_LED_GREEN,
  2777. 0xFFFF, 0x00, 0x00);
  2778. /* determine what channel config wants to open
  2779. * TODO: change me... current implementation is made to work
  2780. * with openocd description parsing.
  2781. */
  2782. end_of_desc = strrchr(ft2232_device_desc, 0x00);
  2783. if (end_of_desc)
  2784. {
  2785. signalyzer_h_side = *(end_of_desc - 1);
  2786. if (signalyzer_h_side == 'B')
  2787. signalyzer_h_side = SIGNALYZER_CHAN_B;
  2788. else
  2789. signalyzer_h_side = SIGNALYZER_CHAN_A;
  2790. }
  2791. else
  2792. {
  2793. LOG_ERROR("No Channel was specified");
  2794. return ERROR_FAIL;
  2795. }
  2796. signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_GREEN,
  2797. 1000, 1000, 0xFF);
  2798. #if BUILD_FT2232_FTD2XX == 1
  2799. /* read signalyzer versionining information */
  2800. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
  2801. SIGNALYZER_COMMAND_VERSION)) != FT_OK)
  2802. {
  2803. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2804. return ERROR_JTAG_DEVICE_ERROR;
  2805. }
  2806. for (i = 0; i < 10; i++)
  2807. {
  2808. if ((status = signalyzer_h_ctrl_read(
  2809. (SIGNALYZER_DATA_BUFFER_ADDR + i),
  2810. &read_buf[i])) != FT_OK)
  2811. {
  2812. LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
  2813. status);
  2814. return ERROR_JTAG_DEVICE_ERROR;
  2815. }
  2816. }
  2817. LOG_INFO("Signalyzer: ID info: { %.4x %.4x %.4x %.4x %.4x %.4x %.4x }",
  2818. read_buf[0], read_buf[1], read_buf[2], read_buf[3],
  2819. read_buf[4], read_buf[5], read_buf[6]);
  2820. /* set gpio register */
  2821. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
  2822. (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
  2823. {
  2824. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2825. return ERROR_JTAG_DEVICE_ERROR;
  2826. }
  2827. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR + 1,
  2828. 0x0404)) != FT_OK)
  2829. {
  2830. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2831. return ERROR_JTAG_DEVICE_ERROR;
  2832. }
  2833. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
  2834. SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
  2835. {
  2836. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2837. return ERROR_JTAG_DEVICE_ERROR;
  2838. }
  2839. /* read adapter type information */
  2840. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_DATA_BUFFER_ADDR,
  2841. ((uint32_t)(signalyzer_h_side << 8) | 0x01))) != FT_OK)
  2842. {
  2843. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2844. return ERROR_JTAG_DEVICE_ERROR;
  2845. }
  2846. if ((status = signalyzer_h_ctrl_write(
  2847. (SIGNALYZER_DATA_BUFFER_ADDR + 1), 0xA000)) != FT_OK)
  2848. {
  2849. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2850. return ERROR_JTAG_DEVICE_ERROR;
  2851. }
  2852. if ((status = signalyzer_h_ctrl_write(
  2853. (SIGNALYZER_DATA_BUFFER_ADDR + 2), 0x0008)) != FT_OK)
  2854. {
  2855. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2856. return ERROR_JTAG_DEVICE_ERROR;
  2857. }
  2858. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
  2859. SIGNALYZER_COMMAND_I2C)) != FT_OK)
  2860. {
  2861. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu", status);
  2862. return ERROR_JTAG_DEVICE_ERROR;
  2863. }
  2864. usleep(100000);
  2865. if ((status = signalyzer_h_ctrl_read(SIGNALYZER_COMMAND_ADDR,
  2866. &read_buf[0])) != FT_OK)
  2867. {
  2868. LOG_ERROR("signalyzer_h_ctrl_read returned: %lu", status);
  2869. return ERROR_JTAG_DEVICE_ERROR;
  2870. }
  2871. if (read_buf[0] != 0x0498)
  2872. signalyzer_h_adapter_type = 0x0000;
  2873. else
  2874. {
  2875. for (i = 0; i < 4; i++)
  2876. {
  2877. if ((status = signalyzer_h_ctrl_read(
  2878. (SIGNALYZER_DATA_BUFFER_ADDR + i),
  2879. &read_buf[i])) != FT_OK)
  2880. {
  2881. LOG_ERROR("signalyzer_h_ctrl_read returned: %lu",
  2882. status);
  2883. return ERROR_JTAG_DEVICE_ERROR;
  2884. }
  2885. }
  2886. signalyzer_h_adapter_type = read_buf[0];
  2887. }
  2888. #elif BUILD_FT2232_LIBFTDI == 1
  2889. /* currently libftdi does not allow reading individual eeprom
  2890. * locations, therefore adapter type cannot be detected.
  2891. * override with most common type
  2892. */
  2893. signalyzer_h_adapter_type = SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG;
  2894. #endif
  2895. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2896. /* ADAPTOR: EM_LT16_A */
  2897. if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
  2898. {
  2899. LOG_INFO("Signalyzer: EM-LT (16-channel level translator) "
  2900. "detected. (HW: %2x).", (read_buf[1] >> 8));
  2901. nTRST = 0x10;
  2902. nTRSTnOE = 0x10;
  2903. nSRST = 0x20;
  2904. nSRSTnOE = 0x20;
  2905. low_output = 0x08;
  2906. low_direction = 0x1b;
  2907. high_output = 0x0;
  2908. high_direction = 0x0;
  2909. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  2910. {
  2911. low_direction &= ~nTRSTnOE; /* nTRST input */
  2912. low_output &= ~nTRST; /* nTRST = 0 */
  2913. }
  2914. else
  2915. {
  2916. low_direction |= nTRSTnOE; /* nTRST output */
  2917. low_output |= nTRST; /* nTRST = 1 */
  2918. }
  2919. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  2920. {
  2921. low_direction |= nSRSTnOE; /* nSRST output */
  2922. low_output |= nSRST; /* nSRST = 1 */
  2923. }
  2924. else
  2925. {
  2926. low_direction &= ~nSRSTnOE; /* nSRST input */
  2927. low_output &= ~nSRST; /* nSRST = 0 */
  2928. }
  2929. #if BUILD_FT2232_FTD2XX == 1
  2930. /* enable power to the module */
  2931. if ((status = signalyzer_h_ctrl_write(
  2932. SIGNALYZER_DATA_BUFFER_ADDR,
  2933. ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
  2934. != FT_OK)
  2935. {
  2936. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2937. status);
  2938. return ERROR_JTAG_DEVICE_ERROR;
  2939. }
  2940. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
  2941. SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
  2942. {
  2943. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2944. status);
  2945. return ERROR_JTAG_DEVICE_ERROR;
  2946. }
  2947. /* set gpio mode register */
  2948. if ((status = signalyzer_h_ctrl_write(
  2949. SIGNALYZER_DATA_BUFFER_ADDR,
  2950. (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
  2951. {
  2952. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2953. status);
  2954. return ERROR_JTAG_DEVICE_ERROR;
  2955. }
  2956. if ((status = signalyzer_h_ctrl_write(
  2957. SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
  2958. != FT_OK)
  2959. {
  2960. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2961. status);
  2962. return ERROR_JTAG_DEVICE_ERROR;
  2963. }
  2964. if ((status = signalyzer_h_ctrl_write(SIGNALYZER_COMMAND_ADDR,
  2965. SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
  2966. {
  2967. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2968. status);
  2969. return ERROR_JTAG_DEVICE_ERROR;
  2970. }
  2971. /* set gpio register */
  2972. if ((status = signalyzer_h_ctrl_write(
  2973. SIGNALYZER_DATA_BUFFER_ADDR,
  2974. (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
  2975. {
  2976. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2977. status);
  2978. return ERROR_JTAG_DEVICE_ERROR;
  2979. }
  2980. if ((status = signalyzer_h_ctrl_write(
  2981. SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x4040))
  2982. != FT_OK)
  2983. {
  2984. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2985. status);
  2986. return ERROR_JTAG_DEVICE_ERROR;
  2987. }
  2988. if ((status = signalyzer_h_ctrl_write(
  2989. SIGNALYZER_COMMAND_ADDR,
  2990. SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
  2991. {
  2992. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  2993. status);
  2994. return ERROR_JTAG_DEVICE_ERROR;
  2995. }
  2996. #endif
  2997. }
  2998. /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
  2999. else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
  3000. (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
  3001. (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
  3002. (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
  3003. {
  3004. if (signalyzer_h_adapter_type
  3005. == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG)
  3006. LOG_INFO("Signalyzer: EM-ARM-JTAG (ARM JTAG) "
  3007. "detected. (HW: %2x).", (read_buf[1] >> 8));
  3008. else if (signalyzer_h_adapter_type
  3009. == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P)
  3010. LOG_INFO("Signalyzer: EM-ARM-JTAG_P "
  3011. "(ARM JTAG with PSU) detected. (HW: %2x).",
  3012. (read_buf[1] >> 8));
  3013. else if (signalyzer_h_adapter_type
  3014. == SIGNALYZER_MODULE_TYPE_EM_JTAG)
  3015. LOG_INFO("Signalyzer: EM-JTAG (Generic JTAG) "
  3016. "detected. (HW: %2x).", (read_buf[1] >> 8));
  3017. else if (signalyzer_h_adapter_type
  3018. == SIGNALYZER_MODULE_TYPE_EM_JTAG_P)
  3019. LOG_INFO("Signalyzer: EM-JTAG-P "
  3020. "(Generic JTAG with PSU) detected. (HW: %2x).",
  3021. (read_buf[1] >> 8));
  3022. nTRST = 0x02;
  3023. nTRSTnOE = 0x04;
  3024. nSRST = 0x08;
  3025. nSRSTnOE = 0x10;
  3026. low_output = 0x08;
  3027. low_direction = 0x1b;
  3028. high_output = 0x0;
  3029. high_direction = 0x1f;
  3030. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3031. {
  3032. high_output |= nTRSTnOE;
  3033. high_output &= ~nTRST;
  3034. }
  3035. else
  3036. {
  3037. high_output &= ~nTRSTnOE;
  3038. high_output |= nTRST;
  3039. }
  3040. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3041. {
  3042. high_output &= ~nSRSTnOE;
  3043. high_output |= nSRST;
  3044. }
  3045. else
  3046. {
  3047. high_output |= nSRSTnOE;
  3048. high_output &= ~nSRST;
  3049. }
  3050. #if BUILD_FT2232_FTD2XX == 1
  3051. /* enable power to the module */
  3052. if ((status = signalyzer_h_ctrl_write(
  3053. SIGNALYZER_DATA_BUFFER_ADDR,
  3054. ((uint32_t)(signalyzer_h_side << 8) | 0x01)))
  3055. != FT_OK)
  3056. {
  3057. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3058. status);
  3059. return ERROR_JTAG_DEVICE_ERROR;
  3060. }
  3061. if ((status = signalyzer_h_ctrl_write(
  3062. SIGNALYZER_COMMAND_ADDR,
  3063. SIGNALYZER_COMMAND_POWERCONTROL_SET)) != FT_OK)
  3064. {
  3065. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3066. status);
  3067. return ERROR_JTAG_DEVICE_ERROR;
  3068. }
  3069. /* set gpio mode register (IO_16 and IO_17 set as analog
  3070. * inputs, other is gpio)
  3071. */
  3072. if ((status = signalyzer_h_ctrl_write(
  3073. SIGNALYZER_DATA_BUFFER_ADDR,
  3074. (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
  3075. {
  3076. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3077. status);
  3078. return ERROR_JTAG_DEVICE_ERROR;
  3079. }
  3080. if ((status = signalyzer_h_ctrl_write(
  3081. SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0060))
  3082. != FT_OK)
  3083. {
  3084. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3085. status);
  3086. return ERROR_JTAG_DEVICE_ERROR;
  3087. }
  3088. if ((status = signalyzer_h_ctrl_write(
  3089. SIGNALYZER_COMMAND_ADDR,
  3090. SIGNALYZER_COMMAND_GPIO_MODE)) != FT_OK)
  3091. {
  3092. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3093. status);
  3094. return ERROR_JTAG_DEVICE_ERROR;
  3095. }
  3096. /* set gpio register (all inputs, for -P modules,
  3097. * PSU will be turned off)
  3098. */
  3099. if ((status = signalyzer_h_ctrl_write(
  3100. SIGNALYZER_DATA_BUFFER_ADDR,
  3101. (uint32_t)(signalyzer_h_side << 8))) != FT_OK)
  3102. {
  3103. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3104. status);
  3105. return ERROR_JTAG_DEVICE_ERROR;
  3106. }
  3107. if ((status = signalyzer_h_ctrl_write(
  3108. SIGNALYZER_DATA_BUFFER_ADDR + 1, 0x0000))
  3109. != FT_OK)
  3110. {
  3111. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3112. status);
  3113. return ERROR_JTAG_DEVICE_ERROR;
  3114. }
  3115. if ((status = signalyzer_h_ctrl_write(
  3116. SIGNALYZER_COMMAND_ADDR,
  3117. SIGNALYZER_COMMAND_GPIO_STATE)) != FT_OK)
  3118. {
  3119. LOG_ERROR("signalyzer_h_ctrl_write returned: %lu",
  3120. status);
  3121. return ERROR_JTAG_DEVICE_ERROR;
  3122. }
  3123. #endif
  3124. }
  3125. else if (signalyzer_h_adapter_type == 0x0000)
  3126. {
  3127. LOG_INFO("Signalyzer: No external modules were detected.");
  3128. nTRST = 0x10;
  3129. nTRSTnOE = 0x10;
  3130. nSRST = 0x20;
  3131. nSRSTnOE = 0x20;
  3132. low_output = 0x08;
  3133. low_direction = 0x1b;
  3134. high_output = 0x0;
  3135. high_direction = 0x0;
  3136. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3137. {
  3138. low_direction &= ~nTRSTnOE; /* nTRST input */
  3139. low_output &= ~nTRST; /* nTRST = 0 */
  3140. }
  3141. else
  3142. {
  3143. low_direction |= nTRSTnOE; /* nTRST output */
  3144. low_output |= nTRST; /* nTRST = 1 */
  3145. }
  3146. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3147. {
  3148. low_direction |= nSRSTnOE; /* nSRST output */
  3149. low_output |= nSRST; /* nSRST = 1 */
  3150. }
  3151. else
  3152. {
  3153. low_direction &= ~nSRSTnOE; /* nSRST input */
  3154. low_output &= ~nSRST; /* nSRST = 0 */
  3155. }
  3156. }
  3157. else
  3158. {
  3159. LOG_ERROR("Unknown module type is detected: %.4x",
  3160. signalyzer_h_adapter_type);
  3161. return ERROR_JTAG_DEVICE_ERROR;
  3162. }
  3163. /* initialize low byte of controller for jtag operation */
  3164. buf[0] = 0x80;
  3165. buf[1] = low_output;
  3166. buf[2] = low_direction;
  3167. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK)
  3168. || (bytes_written != 3))
  3169. {
  3170. LOG_ERROR("couldn't initialize Signalyzer-H layout");
  3171. return ERROR_JTAG_INIT_FAILED;
  3172. }
  3173. #if BUILD_FT2232_FTD2XX == 1
  3174. if (ftdi_device == FT_DEVICE_2232H)
  3175. {
  3176. /* initialize high byte of controller for jtag operation */
  3177. buf[0] = 0x82;
  3178. buf[1] = high_output;
  3179. buf[2] = high_direction;
  3180. if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
  3181. || (bytes_written != 3))
  3182. {
  3183. LOG_ERROR("couldn't initialize Signalyzer-H layout");
  3184. return ERROR_JTAG_INIT_FAILED;
  3185. }
  3186. }
  3187. #elif BUILD_FT2232_LIBFTDI == 1
  3188. if (ftdi_device == TYPE_2232H)
  3189. {
  3190. /* initialize high byte of controller for jtag operation */
  3191. buf[0] = 0x82;
  3192. buf[1] = high_output;
  3193. buf[2] = high_direction;
  3194. if ((ft2232_write(buf, 3, &bytes_written) != ERROR_OK)
  3195. || (bytes_written != 3))
  3196. {
  3197. LOG_ERROR("couldn't initialize Signalyzer-H layout");
  3198. return ERROR_JTAG_INIT_FAILED;
  3199. }
  3200. }
  3201. #endif
  3202. return ERROR_OK;
  3203. }
  3204. static void signalyzer_h_reset(int trst, int srst)
  3205. {
  3206. enum reset_types jtag_reset_config = jtag_get_reset_config();
  3207. /* ADAPTOR: EM_LT16_A */
  3208. if (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_LT16_A)
  3209. {
  3210. if (trst == 1)
  3211. {
  3212. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3213. /* switch to output pin (output is low) */
  3214. low_direction |= nTRSTnOE;
  3215. else
  3216. /* switch output low */
  3217. low_output &= ~nTRST;
  3218. }
  3219. else if (trst == 0)
  3220. {
  3221. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3222. /* switch to input pin (high-Z + internal
  3223. * and external pullup) */
  3224. low_direction &= ~nTRSTnOE;
  3225. else
  3226. /* switch output high */
  3227. low_output |= nTRST;
  3228. }
  3229. if (srst == 1)
  3230. {
  3231. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3232. /* switch output low */
  3233. low_output &= ~nSRST;
  3234. else
  3235. /* switch to output pin (output is low) */
  3236. low_direction |= nSRSTnOE;
  3237. }
  3238. else if (srst == 0)
  3239. {
  3240. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3241. /* switch output high */
  3242. low_output |= nSRST;
  3243. else
  3244. /* switch to input pin (high-Z) */
  3245. low_direction &= ~nSRSTnOE;
  3246. }
  3247. /* command "set data bits low byte" */
  3248. buffer_write(0x80);
  3249. buffer_write(low_output);
  3250. buffer_write(low_direction);
  3251. LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
  3252. "low_direction: 0x%2.2x",
  3253. trst, srst, low_output, low_direction);
  3254. }
  3255. /* ADAPTOR: EM_ARM_JTAG, EM_ARM_JTAG_P, EM_JTAG, EM_JTAG_P */
  3256. else if ((signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG) ||
  3257. (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_ARM_JTAG_P) ||
  3258. (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG) ||
  3259. (signalyzer_h_adapter_type == SIGNALYZER_MODULE_TYPE_EM_JTAG_P))
  3260. {
  3261. if (trst == 1)
  3262. {
  3263. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3264. high_output &= ~nTRSTnOE;
  3265. else
  3266. high_output &= ~nTRST;
  3267. }
  3268. else if (trst == 0)
  3269. {
  3270. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3271. high_output |= nTRSTnOE;
  3272. else
  3273. high_output |= nTRST;
  3274. }
  3275. if (srst == 1)
  3276. {
  3277. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3278. high_output &= ~nSRST;
  3279. else
  3280. high_output &= ~nSRSTnOE;
  3281. }
  3282. else if (srst == 0)
  3283. {
  3284. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3285. high_output |= nSRST;
  3286. else
  3287. high_output |= nSRSTnOE;
  3288. }
  3289. /* command "set data bits high byte" */
  3290. buffer_write(0x82);
  3291. buffer_write(high_output);
  3292. buffer_write(high_direction);
  3293. LOG_INFO("trst: %i, srst: %i, high_output: 0x%2.2x, "
  3294. "high_direction: 0x%2.2x",
  3295. trst, srst, high_output, high_direction);
  3296. }
  3297. else if (signalyzer_h_adapter_type == 0x0000)
  3298. {
  3299. if (trst == 1)
  3300. {
  3301. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3302. /* switch to output pin (output is low) */
  3303. low_direction |= nTRSTnOE;
  3304. else
  3305. /* switch output low */
  3306. low_output &= ~nTRST;
  3307. }
  3308. else if (trst == 0)
  3309. {
  3310. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3311. /* switch to input pin (high-Z + internal
  3312. * and external pullup) */
  3313. low_direction &= ~nTRSTnOE;
  3314. else
  3315. /* switch output high */
  3316. low_output |= nTRST;
  3317. }
  3318. if (srst == 1)
  3319. {
  3320. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3321. /* switch output low */
  3322. low_output &= ~nSRST;
  3323. else
  3324. /* switch to output pin (output is low) */
  3325. low_direction |= nSRSTnOE;
  3326. }
  3327. else if (srst == 0)
  3328. {
  3329. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3330. /* switch output high */
  3331. low_output |= nSRST;
  3332. else
  3333. /* switch to input pin (high-Z) */
  3334. low_direction &= ~nSRSTnOE;
  3335. }
  3336. /* command "set data bits low byte" */
  3337. buffer_write(0x80);
  3338. buffer_write(low_output);
  3339. buffer_write(low_direction);
  3340. LOG_DEBUG("trst: %i, srst: %i, low_output: 0x%2.2x, "
  3341. "low_direction: 0x%2.2x",
  3342. trst, srst, low_output, low_direction);
  3343. }
  3344. }
  3345. static void signalyzer_h_blink(void)
  3346. {
  3347. signalyzer_h_led_set(signalyzer_h_side, SIGNALYZER_LED_RED, 100, 0, 1);
  3348. }
  3349. /********************************************************************
  3350. * Support for KT-LINK
  3351. * JTAG adapter from KRISTECH
  3352. * http://www.kristech.eu
  3353. *******************************************************************/
  3354. static int ktlink_init(void)
  3355. {
  3356. uint8_t buf[3];
  3357. uint32_t bytes_written;
  3358. uint8_t swd_en = 0x20; //0x20 SWD disable, 0x00 SWD enable (ADBUS5)
  3359. low_output = 0x08 | swd_en; // value; TMS=1,TCK=0,TDI=0,SWD=swd_en
  3360. low_direction = 0x3B; // out=1; TCK/TDI/TMS=out,TDO=in,SWD=out,RTCK=in,SRSTIN=in
  3361. // initialize low port
  3362. buf[0] = 0x80; // command "set data bits low byte"
  3363. buf[1] = low_output;
  3364. buf[2] = low_direction;
  3365. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  3366. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  3367. {
  3368. LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
  3369. return ERROR_JTAG_INIT_FAILED;
  3370. }
  3371. nTRST = 0x01;
  3372. nSRST = 0x02;
  3373. nTRSTnOE = 0x04;
  3374. nSRSTnOE = 0x08;
  3375. high_output = 0x80; // turn LED on
  3376. high_direction = 0xFF; // all outputs
  3377. enum reset_types jtag_reset_config = jtag_get_reset_config();
  3378. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN) {
  3379. high_output |= nTRSTnOE;
  3380. high_output &= ~nTRST;
  3381. } else {
  3382. high_output &= ~nTRSTnOE;
  3383. high_output |= nTRST;
  3384. }
  3385. if (jtag_reset_config & RESET_SRST_PUSH_PULL) {
  3386. high_output &= ~nSRSTnOE;
  3387. high_output |= nSRST;
  3388. } else {
  3389. high_output |= nSRSTnOE;
  3390. high_output &= ~nSRST;
  3391. }
  3392. // initialize high port
  3393. buf[0] = 0x82; // command "set data bits high byte"
  3394. buf[1] = high_output; // value
  3395. buf[2] = high_direction;
  3396. LOG_DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
  3397. if (((ft2232_write(buf, 3, &bytes_written)) != ERROR_OK) || (bytes_written != 3))
  3398. {
  3399. LOG_ERROR("couldn't initialize FT2232 with 'ktlink' layout");
  3400. return ERROR_JTAG_INIT_FAILED;
  3401. }
  3402. return ERROR_OK;
  3403. }
  3404. static void ktlink_reset(int trst, int srst)
  3405. {
  3406. enum reset_types jtag_reset_config = jtag_get_reset_config();
  3407. if (trst == 1) {
  3408. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3409. high_output &= ~nTRSTnOE;
  3410. else
  3411. high_output &= ~nTRST;
  3412. } else if (trst == 0) {
  3413. if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
  3414. high_output |= nTRSTnOE;
  3415. else
  3416. high_output |= nTRST;
  3417. }
  3418. if (srst == 1) {
  3419. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3420. high_output &= ~nSRST;
  3421. else
  3422. high_output &= ~nSRSTnOE;
  3423. } else if (srst == 0) {
  3424. if (jtag_reset_config & RESET_SRST_PUSH_PULL)
  3425. high_output |= nSRST;
  3426. else
  3427. high_output |= nSRSTnOE;
  3428. }
  3429. buffer_write(0x82); // command "set data bits high byte"
  3430. buffer_write(high_output);
  3431. buffer_write(high_direction);
  3432. LOG_DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output,high_direction);
  3433. }
  3434. static void ktlink_blink(void)
  3435. {
  3436. /* LED connected to ACBUS7 */
  3437. if (high_output & 0x80)
  3438. high_output &= 0x7F;
  3439. else
  3440. high_output |= 0x80;
  3441. buffer_write(0x82); // command "set data bits high byte"
  3442. buffer_write(high_output);
  3443. buffer_write(high_direction);
  3444. }
  3445. static const struct command_registration ft2232_command_handlers[] = {
  3446. {
  3447. .name = "ft2232_device_desc",
  3448. .handler = &ft2232_handle_device_desc_command,
  3449. .mode = COMMAND_CONFIG,
  3450. .help = "set the USB device description of the FTDI FT2232 device",
  3451. .usage = "description_string",
  3452. },
  3453. {
  3454. .name = "ft2232_serial",
  3455. .handler = &ft2232_handle_serial_command,
  3456. .mode = COMMAND_CONFIG,
  3457. .help = "set the serial number of the FTDI FT2232 device",
  3458. .usage = "serial_string",
  3459. },
  3460. {
  3461. .name = "ft2232_layout",
  3462. .handler = &ft2232_handle_layout_command,
  3463. .mode = COMMAND_CONFIG,
  3464. .help = "set the layout of the FT2232 GPIO signals used "
  3465. "to control output-enables and reset signals",
  3466. .usage = "layout_name",
  3467. },
  3468. {
  3469. .name = "ft2232_vid_pid",
  3470. .handler = &ft2232_handle_vid_pid_command,
  3471. .mode = COMMAND_CONFIG,
  3472. .help = "the vendor ID and product ID of the FTDI FT2232 device",
  3473. .usage = "(vid pid)* ",
  3474. },
  3475. {
  3476. .name = "ft2232_latency",
  3477. .handler = &ft2232_handle_latency_command,
  3478. .mode = COMMAND_CONFIG,
  3479. .help = "set the FT2232 latency timer to a new value",
  3480. .usage = "value",
  3481. },
  3482. COMMAND_REGISTRATION_DONE
  3483. };
  3484. struct jtag_interface ft2232_interface = {
  3485. .name = "ft2232",
  3486. .commands = ft2232_command_handlers,
  3487. .init = ft2232_init,
  3488. .quit = ft2232_quit,
  3489. .speed = ft2232_speed,
  3490. .speed_div = ft2232_speed_div,
  3491. .khz = ft2232_khz,
  3492. .execute_queue = ft2232_execute_queue,
  3493. };