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.
 
 
 
 
 
 

1284 lines
34 KiB

  1. /**************************************************************************
  2. * Copyright (C) 2012 by Andreas Fritiofson *
  3. * andreas.fritiofson@gmail.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. /**
  19. * @file
  20. * JTAG adapters based on the FT2232 full and high speed USB parts are
  21. * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
  22. * are discrete, but development boards may integrate them as alternatives
  23. * to more capable (and expensive) third party JTAG pods.
  24. *
  25. * JTAG uses only one of the two communications channels ("MPSSE engines")
  26. * on these devices. Adapters based on FT4232 parts have four ports/channels
  27. * (A/B/C/D), instead of just two (A/B).
  28. *
  29. * Especially on development boards integrating one of these chips (as
  30. * opposed to discrete pods/dongles), the additional channels can be used
  31. * for a variety of purposes, but OpenOCD only uses one channel at a time.
  32. *
  33. * - As a USB-to-serial adapter for the target's console UART ...
  34. * which may be able to support ROM boot loaders that load initial
  35. * firmware images to flash (or SRAM).
  36. *
  37. * - On systems which support ARM's SWD in addition to JTAG, or instead
  38. * of it, that second port can be used for reading SWV/SWO trace data.
  39. *
  40. * - Additional JTAG links, e.g. to a CPLD or * FPGA.
  41. *
  42. * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
  43. * request/response interactions involve round trips over the USB link.
  44. * A "smart" JTAG adapter has intelligence close to the scan chain, so it
  45. * can for example poll quickly for a status change (usually taking on the
  46. * order of microseconds not milliseconds) before beginning a queued
  47. * transaction which require the previous one to have completed.
  48. *
  49. * There are dozens of adapters of this type, differing in details which
  50. * this driver needs to understand. Those "layout" details are required
  51. * as part of FT2232 driver configuration.
  52. *
  53. * This code uses information contained in the MPSSE specification which was
  54. * found here:
  55. * https://www.ftdichip.com/Support/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
  56. * Hereafter this is called the "MPSSE Spec".
  57. *
  58. * The datasheet for the ftdichip.com's FT2232H part is here:
  59. * https://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT2232H.pdf
  60. *
  61. * Also note the issue with code 0x4b (clock data to TMS) noted in
  62. * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
  63. * which can affect longer JTAG state paths.
  64. */
  65. #ifdef HAVE_CONFIG_H
  66. #include "config.h"
  67. #endif
  68. /* project specific includes */
  69. #include <jtag/drivers/jtag_usb_common.h>
  70. #include <jtag/interface.h>
  71. #include <jtag/swd.h>
  72. #include <transport/transport.h>
  73. #include <helper/time_support.h>
  74. #include <helper/log.h>
  75. #if IS_CYGWIN == 1
  76. #include <windows.h>
  77. #endif
  78. #include <assert.h>
  79. /* FTDI access library includes */
  80. #include "mpsse.h"
  81. #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
  82. #define JTAG_MODE_ALT (LSB_FIRST | NEG_EDGE_IN | NEG_EDGE_OUT)
  83. #define SWD_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
  84. static char *ftdi_device_desc;
  85. static char *ftdi_serial;
  86. static uint8_t ftdi_channel;
  87. static uint8_t ftdi_jtag_mode = JTAG_MODE;
  88. static bool swd_mode;
  89. #define MAX_USB_IDS 8
  90. /* vid = pid = 0 marks the end of the list */
  91. static uint16_t ftdi_vid[MAX_USB_IDS + 1] = { 0 };
  92. static uint16_t ftdi_pid[MAX_USB_IDS + 1] = { 0 };
  93. static struct mpsse_ctx *mpsse_ctx;
  94. struct signal {
  95. const char *name;
  96. uint16_t data_mask;
  97. uint16_t input_mask;
  98. uint16_t oe_mask;
  99. bool invert_data;
  100. bool invert_input;
  101. bool invert_oe;
  102. struct signal *next;
  103. };
  104. static struct signal *signals;
  105. /* FIXME: Where to store per-instance data? We need an SWD context. */
  106. static struct swd_cmd_queue_entry {
  107. uint8_t cmd;
  108. uint32_t *dst;
  109. uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
  110. } *swd_cmd_queue;
  111. static size_t swd_cmd_queue_length;
  112. static size_t swd_cmd_queue_alloced;
  113. static int queued_retval;
  114. static int freq;
  115. static uint16_t output;
  116. static uint16_t direction;
  117. static uint16_t jtag_output_init;
  118. static uint16_t jtag_direction_init;
  119. static int ftdi_swd_switch_seq(enum swd_special_seq seq);
  120. static struct signal *find_signal_by_name(const char *name)
  121. {
  122. for (struct signal *sig = signals; sig; sig = sig->next) {
  123. if (strcmp(name, sig->name) == 0)
  124. return sig;
  125. }
  126. return NULL;
  127. }
  128. static struct signal *create_signal(const char *name)
  129. {
  130. struct signal **psig = &signals;
  131. while (*psig)
  132. psig = &(*psig)->next;
  133. *psig = calloc(1, sizeof(**psig));
  134. if (!*psig)
  135. return NULL;
  136. (*psig)->name = strdup(name);
  137. if (!(*psig)->name) {
  138. free(*psig);
  139. *psig = NULL;
  140. }
  141. return *psig;
  142. }
  143. static int ftdi_set_signal(const struct signal *s, char value)
  144. {
  145. bool data;
  146. bool oe;
  147. if (s->data_mask == 0 && s->oe_mask == 0) {
  148. LOG_ERROR("interface doesn't provide signal '%s'", s->name);
  149. return ERROR_FAIL;
  150. }
  151. switch (value) {
  152. case '0':
  153. data = s->invert_data;
  154. oe = !s->invert_oe;
  155. break;
  156. case '1':
  157. if (s->data_mask == 0) {
  158. LOG_ERROR("interface can't drive '%s' high", s->name);
  159. return ERROR_FAIL;
  160. }
  161. data = !s->invert_data;
  162. oe = !s->invert_oe;
  163. break;
  164. case 'z':
  165. case 'Z':
  166. if (s->oe_mask == 0) {
  167. LOG_ERROR("interface can't tri-state '%s'", s->name);
  168. return ERROR_FAIL;
  169. }
  170. data = s->invert_data;
  171. oe = s->invert_oe;
  172. break;
  173. default:
  174. assert(0 && "invalid signal level specifier");
  175. return ERROR_FAIL;
  176. }
  177. uint16_t old_output = output;
  178. uint16_t old_direction = direction;
  179. output = data ? output | s->data_mask : output & ~s->data_mask;
  180. if (s->oe_mask == s->data_mask)
  181. direction = oe ? direction | s->oe_mask : direction & ~s->oe_mask;
  182. else
  183. output = oe ? output | s->oe_mask : output & ~s->oe_mask;
  184. if ((output & 0xff) != (old_output & 0xff) || (direction & 0xff) != (old_direction & 0xff))
  185. mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff);
  186. if ((output >> 8 != old_output >> 8) || (direction >> 8 != old_direction >> 8))
  187. mpsse_set_data_bits_high_byte(mpsse_ctx, output >> 8, direction >> 8);
  188. return ERROR_OK;
  189. }
  190. static int ftdi_get_signal(const struct signal *s, uint16_t *value_out)
  191. {
  192. uint8_t data_low = 0;
  193. uint8_t data_high = 0;
  194. if (s->input_mask == 0) {
  195. LOG_ERROR("interface doesn't provide signal '%s'", s->name);
  196. return ERROR_FAIL;
  197. }
  198. if (s->input_mask & 0xff)
  199. mpsse_read_data_bits_low_byte(mpsse_ctx, &data_low);
  200. if (s->input_mask >> 8)
  201. mpsse_read_data_bits_high_byte(mpsse_ctx, &data_high);
  202. mpsse_flush(mpsse_ctx);
  203. *value_out = (((uint16_t)data_high) << 8) | data_low;
  204. if (s->invert_input)
  205. *value_out = ~(*value_out);
  206. *value_out &= s->input_mask;
  207. return ERROR_OK;
  208. }
  209. /**
  210. * Function move_to_state
  211. * moves the TAP controller from the current state to a
  212. * \a goal_state through a path given by tap_get_tms_path(). State transition
  213. * logging is performed by delegation to clock_tms().
  214. *
  215. * @param goal_state is the destination state for the move.
  216. */
  217. static void move_to_state(tap_state_t goal_state)
  218. {
  219. tap_state_t start_state = tap_get_state();
  220. /* goal_state is 1/2 of a tuple/pair of states which allow convenient
  221. lookup of the required TMS pattern to move to this state from the
  222. start state.
  223. */
  224. /* do the 2 lookups */
  225. uint8_t tms_bits = tap_get_tms_path(start_state, goal_state);
  226. int tms_count = tap_get_tms_path_len(start_state, goal_state);
  227. assert(tms_count <= 8);
  228. LOG_DEBUG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
  229. /* Track state transitions step by step */
  230. for (int i = 0; i < tms_count; i++)
  231. tap_set_state(tap_state_transition(tap_get_state(), (tms_bits >> i) & 1));
  232. mpsse_clock_tms_cs_out(mpsse_ctx,
  233. &tms_bits,
  234. 0,
  235. tms_count,
  236. false,
  237. ftdi_jtag_mode);
  238. }
  239. static int ftdi_speed(int speed)
  240. {
  241. int retval;
  242. retval = mpsse_set_frequency(mpsse_ctx, speed);
  243. if (retval < 0) {
  244. LOG_ERROR("couldn't set FTDI TCK speed");
  245. return retval;
  246. }
  247. if (!swd_mode && speed >= 10000000 && ftdi_jtag_mode != JTAG_MODE_ALT)
  248. LOG_INFO("ftdi: if you experience problems at higher adapter clocks, try "
  249. "the command \"ftdi tdo_sample_edge falling\"");
  250. return ERROR_OK;
  251. }
  252. static int ftdi_speed_div(int speed, int *khz)
  253. {
  254. *khz = speed / 1000;
  255. return ERROR_OK;
  256. }
  257. static int ftdi_khz(int khz, int *jtag_speed)
  258. {
  259. if (khz == 0 && !mpsse_is_high_speed(mpsse_ctx)) {
  260. LOG_DEBUG("RCLK not supported");
  261. return ERROR_FAIL;
  262. }
  263. *jtag_speed = khz * 1000;
  264. return ERROR_OK;
  265. }
  266. static void ftdi_end_state(tap_state_t state)
  267. {
  268. if (tap_is_state_stable(state))
  269. tap_set_end_state(state);
  270. else {
  271. LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
  272. exit(-1);
  273. }
  274. }
  275. static void ftdi_execute_runtest(struct jtag_command *cmd)
  276. {
  277. int i;
  278. uint8_t zero = 0;
  279. LOG_DEBUG_IO("runtest %i cycles, end in %s",
  280. cmd->cmd.runtest->num_cycles,
  281. tap_state_name(cmd->cmd.runtest->end_state));
  282. if (tap_get_state() != TAP_IDLE)
  283. move_to_state(TAP_IDLE);
  284. /* TODO: Reuse ftdi_execute_stableclocks */
  285. i = cmd->cmd.runtest->num_cycles;
  286. while (i > 0) {
  287. /* there are no state transitions in this code, so omit state tracking */
  288. unsigned this_len = i > 7 ? 7 : i;
  289. mpsse_clock_tms_cs_out(mpsse_ctx, &zero, 0, this_len, false, ftdi_jtag_mode);
  290. i -= this_len;
  291. }
  292. ftdi_end_state(cmd->cmd.runtest->end_state);
  293. if (tap_get_state() != tap_get_end_state())
  294. move_to_state(tap_get_end_state());
  295. LOG_DEBUG_IO("runtest: %i, end in %s",
  296. cmd->cmd.runtest->num_cycles,
  297. tap_state_name(tap_get_end_state()));
  298. }
  299. static void ftdi_execute_statemove(struct jtag_command *cmd)
  300. {
  301. LOG_DEBUG_IO("statemove end in %s",
  302. tap_state_name(cmd->cmd.statemove->end_state));
  303. ftdi_end_state(cmd->cmd.statemove->end_state);
  304. /* shortest-path move to desired end state */
  305. if (tap_get_state() != tap_get_end_state() || tap_get_end_state() == TAP_RESET)
  306. move_to_state(tap_get_end_state());
  307. }
  308. /**
  309. * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
  310. * (or SWD) state machine. REVISIT: Not the best method, perhaps.
  311. */
  312. static void ftdi_execute_tms(struct jtag_command *cmd)
  313. {
  314. LOG_DEBUG_IO("TMS: %d bits", cmd->cmd.tms->num_bits);
  315. /* TODO: Missing tap state tracking, also missing from ft2232.c! */
  316. mpsse_clock_tms_cs_out(mpsse_ctx,
  317. cmd->cmd.tms->bits,
  318. 0,
  319. cmd->cmd.tms->num_bits,
  320. false,
  321. ftdi_jtag_mode);
  322. }
  323. static void ftdi_execute_pathmove(struct jtag_command *cmd)
  324. {
  325. tap_state_t *path = cmd->cmd.pathmove->path;
  326. int num_states = cmd->cmd.pathmove->num_states;
  327. LOG_DEBUG_IO("pathmove: %i states, current: %s end: %s", num_states,
  328. tap_state_name(tap_get_state()),
  329. tap_state_name(path[num_states-1]));
  330. int state_count = 0;
  331. unsigned bit_count = 0;
  332. uint8_t tms_byte = 0;
  333. LOG_DEBUG_IO("-");
  334. /* this loop verifies that the path is legal and logs each state in the path */
  335. while (num_states--) {
  336. /* either TMS=0 or TMS=1 must work ... */
  337. if (tap_state_transition(tap_get_state(), false)
  338. == path[state_count])
  339. buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
  340. else if (tap_state_transition(tap_get_state(), true)
  341. == path[state_count]) {
  342. buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
  343. /* ... or else the caller goofed BADLY */
  344. } else {
  345. LOG_ERROR("BUG: %s -> %s isn't a valid "
  346. "TAP state transition",
  347. tap_state_name(tap_get_state()),
  348. tap_state_name(path[state_count]));
  349. exit(-1);
  350. }
  351. tap_set_state(path[state_count]);
  352. state_count++;
  353. if (bit_count == 7 || num_states == 0) {
  354. mpsse_clock_tms_cs_out(mpsse_ctx,
  355. &tms_byte,
  356. 0,
  357. bit_count,
  358. false,
  359. ftdi_jtag_mode);
  360. bit_count = 0;
  361. }
  362. }
  363. tap_set_end_state(tap_get_state());
  364. }
  365. static void ftdi_execute_scan(struct jtag_command *cmd)
  366. {
  367. LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
  368. jtag_scan_type(cmd->cmd.scan));
  369. /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
  370. while (cmd->cmd.scan->num_fields > 0
  371. && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
  372. cmd->cmd.scan->num_fields--;
  373. LOG_DEBUG_IO("discarding trailing empty field");
  374. }
  375. if (cmd->cmd.scan->num_fields == 0) {
  376. LOG_DEBUG_IO("empty scan, doing nothing");
  377. return;
  378. }
  379. if (cmd->cmd.scan->ir_scan) {
  380. if (tap_get_state() != TAP_IRSHIFT)
  381. move_to_state(TAP_IRSHIFT);
  382. } else {
  383. if (tap_get_state() != TAP_DRSHIFT)
  384. move_to_state(TAP_DRSHIFT);
  385. }
  386. ftdi_end_state(cmd->cmd.scan->end_state);
  387. struct scan_field *field = cmd->cmd.scan->fields;
  388. unsigned scan_size = 0;
  389. for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
  390. scan_size += field->num_bits;
  391. LOG_DEBUG_IO("%s%s field %d/%d %d bits",
  392. field->in_value ? "in" : "",
  393. field->out_value ? "out" : "",
  394. i,
  395. cmd->cmd.scan->num_fields,
  396. field->num_bits);
  397. if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
  398. /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
  399. * movement. This last field can't have length zero, it was checked above. */
  400. mpsse_clock_data(mpsse_ctx,
  401. field->out_value,
  402. 0,
  403. field->in_value,
  404. 0,
  405. field->num_bits - 1,
  406. ftdi_jtag_mode);
  407. uint8_t last_bit = 0;
  408. if (field->out_value)
  409. bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
  410. /* If endstate is TAP_IDLE, clock out 1-1-0 (->EXIT1 ->UPDATE ->IDLE)
  411. * Otherwise, clock out 1-0 (->EXIT1 ->PAUSE)
  412. */
  413. uint8_t tms_bits = 0x03;
  414. mpsse_clock_tms_cs(mpsse_ctx,
  415. &tms_bits,
  416. 0,
  417. field->in_value,
  418. field->num_bits - 1,
  419. 1,
  420. last_bit,
  421. ftdi_jtag_mode);
  422. tap_set_state(tap_state_transition(tap_get_state(), 1));
  423. if (tap_get_end_state() == TAP_IDLE) {
  424. mpsse_clock_tms_cs_out(mpsse_ctx,
  425. &tms_bits,
  426. 1,
  427. 2,
  428. last_bit,
  429. ftdi_jtag_mode);
  430. tap_set_state(tap_state_transition(tap_get_state(), 1));
  431. tap_set_state(tap_state_transition(tap_get_state(), 0));
  432. } else {
  433. mpsse_clock_tms_cs_out(mpsse_ctx,
  434. &tms_bits,
  435. 2,
  436. 1,
  437. last_bit,
  438. ftdi_jtag_mode);
  439. tap_set_state(tap_state_transition(tap_get_state(), 0));
  440. }
  441. } else
  442. mpsse_clock_data(mpsse_ctx,
  443. field->out_value,
  444. 0,
  445. field->in_value,
  446. 0,
  447. field->num_bits,
  448. ftdi_jtag_mode);
  449. }
  450. if (tap_get_state() != tap_get_end_state())
  451. move_to_state(tap_get_end_state());
  452. LOG_DEBUG_IO("%s scan, %i bits, end in %s",
  453. (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
  454. tap_state_name(tap_get_end_state()));
  455. }
  456. static int ftdi_reset(int trst, int srst)
  457. {
  458. struct signal *sig_ntrst = find_signal_by_name("nTRST");
  459. struct signal *sig_nsrst = find_signal_by_name("nSRST");
  460. LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
  461. if (!swd_mode) {
  462. if (trst == 1) {
  463. if (sig_ntrst)
  464. ftdi_set_signal(sig_ntrst, '0');
  465. else
  466. LOG_ERROR("Can't assert TRST: nTRST signal is not defined");
  467. } else if (sig_ntrst && jtag_get_reset_config() & RESET_HAS_TRST &&
  468. trst == 0) {
  469. if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN)
  470. ftdi_set_signal(sig_ntrst, 'z');
  471. else
  472. ftdi_set_signal(sig_ntrst, '1');
  473. }
  474. }
  475. if (srst == 1) {
  476. if (sig_nsrst)
  477. ftdi_set_signal(sig_nsrst, '0');
  478. else
  479. LOG_ERROR("Can't assert SRST: nSRST signal is not defined");
  480. } else if (sig_nsrst && jtag_get_reset_config() & RESET_HAS_SRST &&
  481. srst == 0) {
  482. if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL)
  483. ftdi_set_signal(sig_nsrst, '1');
  484. else
  485. ftdi_set_signal(sig_nsrst, 'z');
  486. }
  487. return mpsse_flush(mpsse_ctx);
  488. }
  489. static void ftdi_execute_sleep(struct jtag_command *cmd)
  490. {
  491. LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
  492. mpsse_flush(mpsse_ctx);
  493. jtag_sleep(cmd->cmd.sleep->us);
  494. LOG_DEBUG_IO("sleep %" PRIu32 " usec while in %s",
  495. cmd->cmd.sleep->us,
  496. tap_state_name(tap_get_state()));
  497. }
  498. static void ftdi_execute_stableclocks(struct jtag_command *cmd)
  499. {
  500. /* this is only allowed while in a stable state. A check for a stable
  501. * state was done in jtag_add_clocks()
  502. */
  503. int num_cycles = cmd->cmd.stableclocks->num_cycles;
  504. /* 7 bits of either ones or zeros. */
  505. uint8_t tms = tap_get_state() == TAP_RESET ? 0x7f : 0x00;
  506. /* TODO: Use mpsse_clock_data with in=out=0 for this, if TMS can be set to
  507. * the correct level and remain there during the scan */
  508. while (num_cycles > 0) {
  509. /* there are no state transitions in this code, so omit state tracking */
  510. unsigned this_len = num_cycles > 7 ? 7 : num_cycles;
  511. mpsse_clock_tms_cs_out(mpsse_ctx, &tms, 0, this_len, false, ftdi_jtag_mode);
  512. num_cycles -= this_len;
  513. }
  514. LOG_DEBUG_IO("clocks %i while in %s",
  515. cmd->cmd.stableclocks->num_cycles,
  516. tap_state_name(tap_get_state()));
  517. }
  518. static void ftdi_execute_command(struct jtag_command *cmd)
  519. {
  520. switch (cmd->type) {
  521. case JTAG_RUNTEST:
  522. ftdi_execute_runtest(cmd);
  523. break;
  524. case JTAG_TLR_RESET:
  525. ftdi_execute_statemove(cmd);
  526. break;
  527. case JTAG_PATHMOVE:
  528. ftdi_execute_pathmove(cmd);
  529. break;
  530. case JTAG_SCAN:
  531. ftdi_execute_scan(cmd);
  532. break;
  533. case JTAG_SLEEP:
  534. ftdi_execute_sleep(cmd);
  535. break;
  536. case JTAG_STABLECLOCKS:
  537. ftdi_execute_stableclocks(cmd);
  538. break;
  539. case JTAG_TMS:
  540. ftdi_execute_tms(cmd);
  541. break;
  542. default:
  543. LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
  544. break;
  545. }
  546. }
  547. static int ftdi_execute_queue(void)
  548. {
  549. /* blink, if the current layout has that feature */
  550. struct signal *led = find_signal_by_name("LED");
  551. if (led)
  552. ftdi_set_signal(led, '1');
  553. for (struct jtag_command *cmd = jtag_command_queue; cmd; cmd = cmd->next) {
  554. /* fill the write buffer with the desired command */
  555. ftdi_execute_command(cmd);
  556. }
  557. if (led)
  558. ftdi_set_signal(led, '0');
  559. int retval = mpsse_flush(mpsse_ctx);
  560. if (retval != ERROR_OK)
  561. LOG_ERROR("error while flushing MPSSE queue: %d", retval);
  562. return retval;
  563. }
  564. static int ftdi_initialize(void)
  565. {
  566. if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
  567. LOG_DEBUG("ftdi interface using 7 step jtag state transitions");
  568. else
  569. LOG_DEBUG("ftdi interface using shortest path jtag state transitions");
  570. if (!ftdi_vid[0] && !ftdi_pid[0]) {
  571. LOG_ERROR("Please specify ftdi vid_pid");
  572. return ERROR_JTAG_INIT_FAILED;
  573. }
  574. for (int i = 0; ftdi_vid[i] || ftdi_pid[i]; i++) {
  575. mpsse_ctx = mpsse_open(&ftdi_vid[i], &ftdi_pid[i], ftdi_device_desc,
  576. ftdi_serial, jtag_usb_get_location(), ftdi_channel);
  577. if (mpsse_ctx)
  578. break;
  579. }
  580. if (!mpsse_ctx)
  581. return ERROR_JTAG_INIT_FAILED;
  582. output = jtag_output_init;
  583. direction = jtag_direction_init;
  584. if (swd_mode) {
  585. struct signal *sig = find_signal_by_name("SWD_EN");
  586. if (!sig) {
  587. LOG_ERROR("SWD mode is active but SWD_EN signal is not defined");
  588. return ERROR_JTAG_INIT_FAILED;
  589. }
  590. /* A dummy SWD_EN would have zero mask */
  591. if (sig->data_mask)
  592. ftdi_set_signal(sig, '1');
  593. }
  594. mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff);
  595. mpsse_set_data_bits_high_byte(mpsse_ctx, output >> 8, direction >> 8);
  596. mpsse_loopback_config(mpsse_ctx, false);
  597. freq = mpsse_set_frequency(mpsse_ctx, jtag_get_speed_khz() * 1000);
  598. return mpsse_flush(mpsse_ctx);
  599. }
  600. static int ftdi_quit(void)
  601. {
  602. mpsse_close(mpsse_ctx);
  603. struct signal *sig = signals;
  604. while (sig) {
  605. struct signal *next = sig->next;
  606. free((void *)sig->name);
  607. free(sig);
  608. sig = next;
  609. }
  610. free(ftdi_device_desc);
  611. free(ftdi_serial);
  612. free(swd_cmd_queue);
  613. return ERROR_OK;
  614. }
  615. COMMAND_HANDLER(ftdi_handle_device_desc_command)
  616. {
  617. if (CMD_ARGC == 1) {
  618. free(ftdi_device_desc);
  619. ftdi_device_desc = strdup(CMD_ARGV[0]);
  620. } else {
  621. LOG_ERROR("expected exactly one argument to ftdi device_desc <description>");
  622. }
  623. return ERROR_OK;
  624. }
  625. COMMAND_HANDLER(ftdi_handle_serial_command)
  626. {
  627. if (CMD_ARGC == 1) {
  628. free(ftdi_serial);
  629. ftdi_serial = strdup(CMD_ARGV[0]);
  630. } else {
  631. return ERROR_COMMAND_SYNTAX_ERROR;
  632. }
  633. return ERROR_OK;
  634. }
  635. COMMAND_HANDLER(ftdi_handle_channel_command)
  636. {
  637. if (CMD_ARGC == 1)
  638. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], ftdi_channel);
  639. else
  640. return ERROR_COMMAND_SYNTAX_ERROR;
  641. return ERROR_OK;
  642. }
  643. COMMAND_HANDLER(ftdi_handle_layout_init_command)
  644. {
  645. if (CMD_ARGC != 2)
  646. return ERROR_COMMAND_SYNTAX_ERROR;
  647. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], jtag_output_init);
  648. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], jtag_direction_init);
  649. return ERROR_OK;
  650. }
  651. COMMAND_HANDLER(ftdi_handle_layout_signal_command)
  652. {
  653. if (CMD_ARGC < 1)
  654. return ERROR_COMMAND_SYNTAX_ERROR;
  655. bool invert_data = false;
  656. uint16_t data_mask = 0;
  657. bool invert_input = false;
  658. uint16_t input_mask = 0;
  659. bool invert_oe = false;
  660. uint16_t oe_mask = 0;
  661. for (unsigned i = 1; i < CMD_ARGC; i += 2) {
  662. if (strcmp("-data", CMD_ARGV[i]) == 0) {
  663. invert_data = false;
  664. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
  665. } else if (strcmp("-ndata", CMD_ARGV[i]) == 0) {
  666. invert_data = true;
  667. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
  668. } else if (strcmp("-input", CMD_ARGV[i]) == 0) {
  669. invert_input = false;
  670. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
  671. } else if (strcmp("-ninput", CMD_ARGV[i]) == 0) {
  672. invert_input = true;
  673. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
  674. } else if (strcmp("-oe", CMD_ARGV[i]) == 0) {
  675. invert_oe = false;
  676. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
  677. } else if (strcmp("-noe", CMD_ARGV[i]) == 0) {
  678. invert_oe = true;
  679. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
  680. } else if (!strcmp("-alias", CMD_ARGV[i]) ||
  681. !strcmp("-nalias", CMD_ARGV[i])) {
  682. if (!strcmp("-nalias", CMD_ARGV[i])) {
  683. invert_data = true;
  684. invert_input = true;
  685. }
  686. struct signal *sig = find_signal_by_name(CMD_ARGV[i + 1]);
  687. if (!sig) {
  688. LOG_ERROR("signal %s is not defined", CMD_ARGV[i + 1]);
  689. return ERROR_FAIL;
  690. }
  691. data_mask = sig->data_mask;
  692. input_mask = sig->input_mask;
  693. oe_mask = sig->oe_mask;
  694. invert_input ^= sig->invert_input;
  695. invert_oe = sig->invert_oe;
  696. invert_data ^= sig->invert_data;
  697. } else {
  698. LOG_ERROR("unknown option '%s'", CMD_ARGV[i]);
  699. return ERROR_COMMAND_SYNTAX_ERROR;
  700. }
  701. }
  702. struct signal *sig;
  703. sig = find_signal_by_name(CMD_ARGV[0]);
  704. if (!sig)
  705. sig = create_signal(CMD_ARGV[0]);
  706. if (!sig) {
  707. LOG_ERROR("failed to create signal %s", CMD_ARGV[0]);
  708. return ERROR_FAIL;
  709. }
  710. sig->invert_data = invert_data;
  711. sig->data_mask = data_mask;
  712. sig->invert_input = invert_input;
  713. sig->input_mask = input_mask;
  714. sig->invert_oe = invert_oe;
  715. sig->oe_mask = oe_mask;
  716. return ERROR_OK;
  717. }
  718. COMMAND_HANDLER(ftdi_handle_set_signal_command)
  719. {
  720. if (CMD_ARGC < 2)
  721. return ERROR_COMMAND_SYNTAX_ERROR;
  722. struct signal *sig;
  723. sig = find_signal_by_name(CMD_ARGV[0]);
  724. if (!sig) {
  725. LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
  726. return ERROR_FAIL;
  727. }
  728. switch (*CMD_ARGV[1]) {
  729. case '0':
  730. case '1':
  731. case 'z':
  732. case 'Z':
  733. /* single character level specifier only */
  734. if (CMD_ARGV[1][1] == '\0') {
  735. ftdi_set_signal(sig, *CMD_ARGV[1]);
  736. break;
  737. }
  738. /* fallthrough */
  739. default:
  740. LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV[1]);
  741. return ERROR_COMMAND_SYNTAX_ERROR;
  742. }
  743. return mpsse_flush(mpsse_ctx);
  744. }
  745. COMMAND_HANDLER(ftdi_handle_get_signal_command)
  746. {
  747. if (CMD_ARGC < 1)
  748. return ERROR_COMMAND_SYNTAX_ERROR;
  749. struct signal *sig;
  750. uint16_t sig_data = 0;
  751. sig = find_signal_by_name(CMD_ARGV[0]);
  752. if (!sig) {
  753. LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
  754. return ERROR_FAIL;
  755. }
  756. int ret = ftdi_get_signal(sig, &sig_data);
  757. if (ret != ERROR_OK)
  758. return ret;
  759. LOG_USER("Signal %s = %#06x", sig->name, sig_data);
  760. return ERROR_OK;
  761. }
  762. COMMAND_HANDLER(ftdi_handle_vid_pid_command)
  763. {
  764. if (CMD_ARGC > MAX_USB_IDS * 2) {
  765. LOG_WARNING("ignoring extra IDs in ftdi vid_pid "
  766. "(maximum is %d pairs)", MAX_USB_IDS);
  767. CMD_ARGC = MAX_USB_IDS * 2;
  768. }
  769. if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
  770. LOG_WARNING("incomplete ftdi vid_pid configuration directive");
  771. if (CMD_ARGC < 2)
  772. return ERROR_COMMAND_SYNTAX_ERROR;
  773. /* remove the incomplete trailing id */
  774. CMD_ARGC -= 1;
  775. }
  776. unsigned i;
  777. for (i = 0; i < CMD_ARGC; i += 2) {
  778. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ftdi_vid[i >> 1]);
  779. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ftdi_pid[i >> 1]);
  780. }
  781. /*
  782. * Explicitly terminate, in case there are multiples instances of
  783. * ftdi vid_pid.
  784. */
  785. ftdi_vid[i >> 1] = ftdi_pid[i >> 1] = 0;
  786. return ERROR_OK;
  787. }
  788. COMMAND_HANDLER(ftdi_handle_tdo_sample_edge_command)
  789. {
  790. struct jim_nvp *n;
  791. static const struct jim_nvp nvp_ftdi_jtag_modes[] = {
  792. { .name = "rising", .value = JTAG_MODE },
  793. { .name = "falling", .value = JTAG_MODE_ALT },
  794. { .name = NULL, .value = -1 },
  795. };
  796. if (CMD_ARGC > 0) {
  797. n = jim_nvp_name2value_simple(nvp_ftdi_jtag_modes, CMD_ARGV[0]);
  798. if (!n->name)
  799. return ERROR_COMMAND_SYNTAX_ERROR;
  800. ftdi_jtag_mode = n->value;
  801. }
  802. n = jim_nvp_value2name_simple(nvp_ftdi_jtag_modes, ftdi_jtag_mode);
  803. command_print(CMD, "ftdi samples TDO on %s edge of TCK", n->name);
  804. return ERROR_OK;
  805. }
  806. static const struct command_registration ftdi_subcommand_handlers[] = {
  807. {
  808. .name = "device_desc",
  809. .handler = &ftdi_handle_device_desc_command,
  810. .mode = COMMAND_CONFIG,
  811. .help = "set the USB device description of the FTDI device",
  812. .usage = "description_string",
  813. },
  814. {
  815. .name = "serial",
  816. .handler = &ftdi_handle_serial_command,
  817. .mode = COMMAND_CONFIG,
  818. .help = "set the serial number of the FTDI device",
  819. .usage = "serial_string",
  820. },
  821. {
  822. .name = "channel",
  823. .handler = &ftdi_handle_channel_command,
  824. .mode = COMMAND_CONFIG,
  825. .help = "set the channel of the FTDI device that is used as JTAG",
  826. .usage = "(0-3)",
  827. },
  828. {
  829. .name = "layout_init",
  830. .handler = &ftdi_handle_layout_init_command,
  831. .mode = COMMAND_CONFIG,
  832. .help = "initialize the FTDI GPIO signals used "
  833. "to control output-enables and reset signals",
  834. .usage = "data direction",
  835. },
  836. {
  837. .name = "layout_signal",
  838. .handler = &ftdi_handle_layout_signal_command,
  839. .mode = COMMAND_ANY,
  840. .help = "define a signal controlled by one or more FTDI GPIO as data "
  841. "and/or output enable",
  842. .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]",
  843. },
  844. {
  845. .name = "set_signal",
  846. .handler = &ftdi_handle_set_signal_command,
  847. .mode = COMMAND_EXEC,
  848. .help = "control a layout-specific signal",
  849. .usage = "name (1|0|z)",
  850. },
  851. {
  852. .name = "get_signal",
  853. .handler = &ftdi_handle_get_signal_command,
  854. .mode = COMMAND_EXEC,
  855. .help = "read the value of a layout-specific signal",
  856. .usage = "name",
  857. },
  858. {
  859. .name = "vid_pid",
  860. .handler = &ftdi_handle_vid_pid_command,
  861. .mode = COMMAND_CONFIG,
  862. .help = "the vendor ID and product ID of the FTDI device",
  863. .usage = "(vid pid)*",
  864. },
  865. {
  866. .name = "tdo_sample_edge",
  867. .handler = &ftdi_handle_tdo_sample_edge_command,
  868. .mode = COMMAND_ANY,
  869. .help = "set which TCK clock edge is used for sampling TDO "
  870. "- default is rising-edge (Setting to falling-edge may "
  871. "allow signalling speed increase)",
  872. .usage = "(rising|falling)",
  873. },
  874. COMMAND_REGISTRATION_DONE
  875. };
  876. static const struct command_registration ftdi_command_handlers[] = {
  877. {
  878. .name = "ftdi",
  879. .mode = COMMAND_ANY,
  880. .help = "perform ftdi management",
  881. .chain = ftdi_subcommand_handlers,
  882. .usage = "",
  883. },
  884. COMMAND_REGISTRATION_DONE
  885. };
  886. static int create_default_signal(const char *name, uint16_t data_mask)
  887. {
  888. struct signal *sig = create_signal(name);
  889. if (!sig) {
  890. LOG_ERROR("failed to create signal %s", name);
  891. return ERROR_FAIL;
  892. }
  893. sig->invert_data = false;
  894. sig->data_mask = data_mask;
  895. sig->invert_oe = false;
  896. sig->oe_mask = 0;
  897. return ERROR_OK;
  898. }
  899. static int create_signals(void)
  900. {
  901. if (create_default_signal("TCK", 0x01) != ERROR_OK)
  902. return ERROR_FAIL;
  903. if (create_default_signal("TDI", 0x02) != ERROR_OK)
  904. return ERROR_FAIL;
  905. if (create_default_signal("TDO", 0x04) != ERROR_OK)
  906. return ERROR_FAIL;
  907. if (create_default_signal("TMS", 0x08) != ERROR_OK)
  908. return ERROR_FAIL;
  909. return ERROR_OK;
  910. }
  911. static int ftdi_swd_init(void)
  912. {
  913. LOG_INFO("FTDI SWD mode enabled");
  914. swd_mode = true;
  915. if (create_signals() != ERROR_OK)
  916. return ERROR_FAIL;
  917. swd_cmd_queue_alloced = 10;
  918. swd_cmd_queue = malloc(swd_cmd_queue_alloced * sizeof(*swd_cmd_queue));
  919. return swd_cmd_queue ? ERROR_OK : ERROR_FAIL;
  920. }
  921. static void ftdi_swd_swdio_en(bool enable)
  922. {
  923. struct signal *oe = find_signal_by_name("SWDIO_OE");
  924. if (oe) {
  925. if (oe->data_mask)
  926. ftdi_set_signal(oe, enable ? '1' : '0');
  927. else {
  928. /* Sets TDI/DO pin to input during rx when both pins are connected
  929. to SWDIO */
  930. if (enable)
  931. direction |= jtag_direction_init & 0x0002U;
  932. else
  933. direction &= ~0x0002U;
  934. mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff);
  935. }
  936. }
  937. }
  938. /**
  939. * Flush the MPSSE queue and process the SWD transaction queue
  940. * @return
  941. */
  942. static int ftdi_swd_run_queue(void)
  943. {
  944. LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length);
  945. int retval;
  946. struct signal *led = find_signal_by_name("LED");
  947. if (queued_retval != ERROR_OK) {
  948. LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval);
  949. goto skip;
  950. }
  951. /* A transaction must be followed by another transaction or at least 8 idle cycles to
  952. * ensure that data is clocked through the AP. */
  953. mpsse_clock_data_out(mpsse_ctx, NULL, 0, 8, SWD_MODE);
  954. /* Terminate the "blink", if the current layout has that feature */
  955. if (led)
  956. ftdi_set_signal(led, '0');
  957. queued_retval = mpsse_flush(mpsse_ctx);
  958. if (queued_retval != ERROR_OK) {
  959. LOG_ERROR("MPSSE failed");
  960. goto skip;
  961. }
  962. for (size_t i = 0; i < swd_cmd_queue_length; i++) {
  963. int ack = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3);
  964. LOG_DEBUG_IO("%s %s %s reg %X = %08"PRIx32,
  965. ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
  966. swd_cmd_queue[i].cmd & SWD_CMD_APNDP ? "AP" : "DP",
  967. swd_cmd_queue[i].cmd & SWD_CMD_RNW ? "read" : "write",
  968. (swd_cmd_queue[i].cmd & SWD_CMD_A32) >> 1,
  969. buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn,
  970. 1 + 3 + (swd_cmd_queue[i].cmd & SWD_CMD_RNW ? 0 : 1), 32));
  971. if (ack != SWD_ACK_OK) {
  972. queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
  973. goto skip;
  974. } else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
  975. uint32_t data = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3, 32);
  976. int parity = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 32, 1);
  977. if (parity != parity_u32(data)) {
  978. LOG_ERROR("SWD Read data parity mismatch");
  979. queued_retval = ERROR_FAIL;
  980. goto skip;
  981. }
  982. if (swd_cmd_queue[i].dst)
  983. *swd_cmd_queue[i].dst = data;
  984. }
  985. }
  986. skip:
  987. swd_cmd_queue_length = 0;
  988. retval = queued_retval;
  989. queued_retval = ERROR_OK;
  990. /* Queue a new "blink" */
  991. if (led && retval == ERROR_OK)
  992. ftdi_set_signal(led, '1');
  993. return retval;
  994. }
  995. static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
  996. {
  997. if (swd_cmd_queue_length >= swd_cmd_queue_alloced) {
  998. /* Not enough room in the queue. Run the queue and increase its size for next time.
  999. * Note that it's not possible to avoid running the queue here, because mpsse contains
  1000. * pointers into the queue which may be invalid after the realloc. */
  1001. queued_retval = ftdi_swd_run_queue();
  1002. struct swd_cmd_queue_entry *q = realloc(swd_cmd_queue, swd_cmd_queue_alloced * 2 * sizeof(*swd_cmd_queue));
  1003. if (q) {
  1004. swd_cmd_queue = q;
  1005. swd_cmd_queue_alloced *= 2;
  1006. LOG_DEBUG("Increased SWD command queue to %zu elements", swd_cmd_queue_alloced);
  1007. }
  1008. }
  1009. if (queued_retval != ERROR_OK)
  1010. return;
  1011. size_t i = swd_cmd_queue_length++;
  1012. swd_cmd_queue[i].cmd = cmd | SWD_CMD_START | SWD_CMD_PARK;
  1013. mpsse_clock_data_out(mpsse_ctx, &swd_cmd_queue[i].cmd, 0, 8, SWD_MODE);
  1014. if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
  1015. /* Queue a read transaction */
  1016. swd_cmd_queue[i].dst = dst;
  1017. ftdi_swd_swdio_en(false);
  1018. mpsse_clock_data_in(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn,
  1019. 0, 1 + 3 + 32 + 1 + 1, SWD_MODE);
  1020. ftdi_swd_swdio_en(true);
  1021. } else {
  1022. /* Queue a write transaction */
  1023. ftdi_swd_swdio_en(false);
  1024. mpsse_clock_data_in(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn,
  1025. 0, 1 + 3 + 1, SWD_MODE);
  1026. ftdi_swd_swdio_en(true);
  1027. buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32, data);
  1028. buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(data));
  1029. mpsse_clock_data_out(mpsse_ctx, swd_cmd_queue[i].trn_ack_data_parity_trn,
  1030. 1 + 3 + 1, 32 + 1, SWD_MODE);
  1031. }
  1032. /* Insert idle cycles after AP accesses to avoid WAIT */
  1033. if (cmd & SWD_CMD_APNDP)
  1034. mpsse_clock_data_out(mpsse_ctx, NULL, 0, ap_delay_clk, SWD_MODE);
  1035. }
  1036. static void ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
  1037. {
  1038. assert(cmd & SWD_CMD_RNW);
  1039. ftdi_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
  1040. }
  1041. static void ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
  1042. {
  1043. assert(!(cmd & SWD_CMD_RNW));
  1044. ftdi_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
  1045. }
  1046. static int ftdi_swd_switch_seq(enum swd_special_seq seq)
  1047. {
  1048. switch (seq) {
  1049. case LINE_RESET:
  1050. LOG_DEBUG("SWD line reset");
  1051. ftdi_swd_swdio_en(true);
  1052. mpsse_clock_data_out(mpsse_ctx, swd_seq_line_reset, 0, swd_seq_line_reset_len, SWD_MODE);
  1053. break;
  1054. case JTAG_TO_SWD:
  1055. LOG_DEBUG("JTAG-to-SWD");
  1056. ftdi_swd_swdio_en(true);
  1057. mpsse_clock_data_out(mpsse_ctx, swd_seq_jtag_to_swd, 0, swd_seq_jtag_to_swd_len, SWD_MODE);
  1058. break;
  1059. case SWD_TO_JTAG:
  1060. LOG_DEBUG("SWD-to-JTAG");
  1061. ftdi_swd_swdio_en(true);
  1062. mpsse_clock_data_out(mpsse_ctx, swd_seq_swd_to_jtag, 0, swd_seq_swd_to_jtag_len, SWD_MODE);
  1063. break;
  1064. default:
  1065. LOG_ERROR("Sequence %d not supported", seq);
  1066. return ERROR_FAIL;
  1067. }
  1068. return ERROR_OK;
  1069. }
  1070. static const struct swd_driver ftdi_swd = {
  1071. .init = ftdi_swd_init,
  1072. .switch_seq = ftdi_swd_switch_seq,
  1073. .read_reg = ftdi_swd_read_reg,
  1074. .write_reg = ftdi_swd_write_reg,
  1075. .run = ftdi_swd_run_queue,
  1076. };
  1077. static const char * const ftdi_transports[] = { "jtag", "swd", NULL };
  1078. static struct jtag_interface ftdi_interface = {
  1079. .supported = DEBUG_CAP_TMS_SEQ,
  1080. .execute_queue = ftdi_execute_queue,
  1081. };
  1082. struct adapter_driver ftdi_adapter_driver = {
  1083. .name = "ftdi",
  1084. .transports = ftdi_transports,
  1085. .commands = ftdi_command_handlers,
  1086. .init = ftdi_initialize,
  1087. .quit = ftdi_quit,
  1088. .reset = ftdi_reset,
  1089. .speed = ftdi_speed,
  1090. .khz = ftdi_khz,
  1091. .speed_div = ftdi_speed_div,
  1092. .jtag_ops = &ftdi_interface,
  1093. .swd_ops = &ftdi_swd,
  1094. };