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.
 
 
 
 
 
 

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