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.
 
 
 
 
 
 

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