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.
 
 
 
 
 
 

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