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.
 
 
 
 
 
 

881 lines
23 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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. /**
  21. * @file
  22. * JTAG adapters based on the FT2232 full and high speed USB parts are
  23. * popular low cost JTAG debug solutions. Many FT2232 based JTAG adapters
  24. * are discrete, but development boards may integrate them as alternatives
  25. * to more capable (and expensive) third party JTAG pods.
  26. *
  27. * JTAG uses only one of the two communications channels ("MPSSE engines")
  28. * on these devices. Adapters based on FT4232 parts have four ports/channels
  29. * (A/B/C/D), instead of just two (A/B).
  30. *
  31. * Especially on development boards integrating one of these chips (as
  32. * opposed to discrete pods/dongles), the additional channels can be used
  33. * for a variety of purposes, but OpenOCD only uses one channel at a time.
  34. *
  35. * - As a USB-to-serial adapter for the target's console UART ...
  36. * which may be able to support ROM boot loaders that load initial
  37. * firmware images to flash (or SRAM).
  38. *
  39. * - On systems which support ARM's SWD in addition to JTAG, or instead
  40. * of it, that second port can be used for reading SWV/SWO trace data.
  41. *
  42. * - Additional JTAG links, e.g. to a CPLD or * FPGA.
  43. *
  44. * FT2232 based JTAG adapters are "dumb" not "smart", because most JTAG
  45. * request/response interactions involve round trips over the USB link.
  46. * A "smart" JTAG adapter has intelligence close to the scan chain, so it
  47. * can for example poll quickly for a status change (usually taking on the
  48. * order of microseconds not milliseconds) before beginning a queued
  49. * transaction which require the previous one to have completed.
  50. *
  51. * There are dozens of adapters of this type, differing in details which
  52. * this driver needs to understand. Those "layout" details are required
  53. * as part of FT2232 driver configuration.
  54. *
  55. * This code uses information contained in the MPSSE specification which was
  56. * found here:
  57. * http://www.ftdichip.com/Documents/AppNotes/AN2232C-01_MPSSE_Cmnd.pdf
  58. * Hereafter this is called the "MPSSE Spec".
  59. *
  60. * The datasheet for the ftdichip.com's FT2232D part is here:
  61. * http://www.ftdichip.com/Documents/DataSheets/DS_FT2232D.pdf
  62. *
  63. * Also note the issue with code 0x4b (clock data to TMS) noted in
  64. * http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00292.html
  65. * which can affect longer JTAG state paths.
  66. */
  67. #ifdef HAVE_CONFIG_H
  68. #include "config.h"
  69. #endif
  70. /* project specific includes */
  71. #include <jtag/interface.h>
  72. #include <transport/transport.h>
  73. #include <helper/time_support.h>
  74. #if IS_CYGWIN == 1
  75. #include <windows.h>
  76. #endif
  77. #include <assert.h>
  78. /* FTDI access library includes */
  79. #include "mpsse.h"
  80. #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
  81. static char *ftdi_device_desc;
  82. static char *ftdi_serial;
  83. static uint8_t ftdi_channel;
  84. #define MAX_USB_IDS 8
  85. /* vid = pid = 0 marks the end of the list */
  86. static uint16_t ftdi_vid[MAX_USB_IDS + 1] = { 0 };
  87. static uint16_t ftdi_pid[MAX_USB_IDS + 1] = { 0 };
  88. static struct mpsse_ctx *mpsse_ctx;
  89. struct signal {
  90. const char *name;
  91. uint16_t data_mask;
  92. uint16_t oe_mask;
  93. bool invert_data;
  94. bool invert_oe;
  95. struct signal *next;
  96. };
  97. static struct signal *signals;
  98. static uint16_t output;
  99. static uint16_t direction;
  100. static struct signal *find_signal_by_name(const char *name)
  101. {
  102. for (struct signal *sig = signals; sig; sig = sig->next) {
  103. if (strcmp(name, sig->name) == 0)
  104. return sig;
  105. }
  106. return NULL;
  107. }
  108. static struct signal *create_signal(const char *name)
  109. {
  110. struct signal **psig = &signals;
  111. while (*psig)
  112. psig = &(*psig)->next;
  113. *psig = calloc(1, sizeof(**psig));
  114. if (*psig)
  115. (*psig)->name = strdup(name);
  116. if ((*psig)->name == NULL) {
  117. free(*psig);
  118. *psig = NULL;
  119. }
  120. return *psig;
  121. }
  122. static int ftdi_set_signal(const struct signal *s, char value)
  123. {
  124. int retval;
  125. bool data;
  126. bool oe;
  127. if (s->data_mask == 0 && s->oe_mask == 0) {
  128. LOG_ERROR("interface doesn't provide signal '%s'", s->name);
  129. return ERROR_FAIL;
  130. }
  131. switch (value) {
  132. case '0':
  133. data = s->invert_data;
  134. oe = !s->invert_oe;
  135. break;
  136. case '1':
  137. if (s->data_mask == 0) {
  138. LOG_ERROR("interface can't drive '%s' high", s->name);
  139. return ERROR_FAIL;
  140. }
  141. data = !s->invert_data;
  142. oe = !s->invert_oe;
  143. break;
  144. case 'z':
  145. case 'Z':
  146. if (s->oe_mask == 0) {
  147. LOG_ERROR("interface can't tri-state '%s'", s->name);
  148. return ERROR_FAIL;
  149. }
  150. data = s->invert_data;
  151. oe = s->invert_oe;
  152. break;
  153. default:
  154. assert(0 && "invalid signal level specifier");
  155. return ERROR_FAIL;
  156. }
  157. output = data ? output | s->data_mask : output & ~s->data_mask;
  158. if (s->oe_mask == s->data_mask)
  159. direction = oe ? direction | s->oe_mask : direction & ~s->oe_mask;
  160. else
  161. output = oe ? output | s->oe_mask : output & ~s->oe_mask;
  162. retval = mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff);
  163. if (retval == ERROR_OK)
  164. retval = mpsse_set_data_bits_high_byte(mpsse_ctx, output >> 8, direction >> 8);
  165. if (retval != ERROR_OK) {
  166. LOG_ERROR("couldn't initialize FTDI GPIO");
  167. return ERROR_JTAG_INIT_FAILED;
  168. }
  169. return ERROR_OK;
  170. }
  171. /**
  172. * Function move_to_state
  173. * moves the TAP controller from the current state to a
  174. * \a goal_state through a path given by tap_get_tms_path(). State transition
  175. * logging is performed by delegation to clock_tms().
  176. *
  177. * @param goal_state is the destination state for the move.
  178. */
  179. static int move_to_state(tap_state_t goal_state)
  180. {
  181. tap_state_t start_state = tap_get_state();
  182. /* goal_state is 1/2 of a tuple/pair of states which allow convenient
  183. lookup of the required TMS pattern to move to this state from the
  184. start state.
  185. */
  186. /* do the 2 lookups */
  187. int tms_bits = tap_get_tms_path(start_state, goal_state);
  188. int tms_count = tap_get_tms_path_len(start_state, goal_state);
  189. DEBUG_JTAG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
  190. /* Track state transitions step by step */
  191. for (int i = 0; i < tms_count; i++)
  192. tap_set_state(tap_state_transition(tap_get_state(), (tms_bits >> i) & 1));
  193. return mpsse_clock_tms_cs_out(mpsse_ctx,
  194. (uint8_t *)&tms_bits,
  195. 0,
  196. tms_count,
  197. false,
  198. JTAG_MODE);
  199. }
  200. static int ftdi_speed(int speed)
  201. {
  202. int retval;
  203. retval = mpsse_set_frequency(mpsse_ctx, speed);
  204. if (retval < 0) {
  205. LOG_ERROR("couldn't set FTDI TCK speed");
  206. return retval;
  207. }
  208. return ERROR_OK;
  209. }
  210. static int ftdi_speed_div(int speed, int *khz)
  211. {
  212. *khz = speed / 1000;
  213. return ERROR_OK;
  214. }
  215. static int ftdi_khz(int khz, int *jtag_speed)
  216. {
  217. *jtag_speed = khz * 1000;
  218. return ERROR_OK;
  219. }
  220. static void ftdi_end_state(tap_state_t state)
  221. {
  222. if (tap_is_state_stable(state))
  223. tap_set_end_state(state);
  224. else {
  225. LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
  226. exit(-1);
  227. }
  228. }
  229. static int ftdi_execute_runtest(struct jtag_command *cmd)
  230. {
  231. int retval = ERROR_OK;
  232. int i;
  233. uint8_t zero = 0;
  234. DEBUG_JTAG_IO("runtest %i cycles, end in %s",
  235. cmd->cmd.runtest->num_cycles,
  236. tap_state_name(cmd->cmd.runtest->end_state));
  237. if (tap_get_state() != TAP_IDLE)
  238. move_to_state(TAP_IDLE);
  239. /* TODO: Reuse ftdi_execute_stableclocks */
  240. i = cmd->cmd.runtest->num_cycles;
  241. while (i > 0 && retval == ERROR_OK) {
  242. /* there are no state transitions in this code, so omit state tracking */
  243. unsigned this_len = i > 7 ? 7 : i;
  244. retval = mpsse_clock_tms_cs_out(mpsse_ctx, &zero, 0, this_len, false, JTAG_MODE);
  245. i -= this_len;
  246. }
  247. ftdi_end_state(cmd->cmd.runtest->end_state);
  248. if (tap_get_state() != tap_get_end_state())
  249. move_to_state(tap_get_end_state());
  250. DEBUG_JTAG_IO("runtest: %i, end in %s",
  251. cmd->cmd.runtest->num_cycles,
  252. tap_state_name(tap_get_end_state()));
  253. return retval;
  254. }
  255. static int ftdi_execute_statemove(struct jtag_command *cmd)
  256. {
  257. int retval = ERROR_OK;
  258. DEBUG_JTAG_IO("statemove end in %s",
  259. tap_state_name(cmd->cmd.statemove->end_state));
  260. ftdi_end_state(cmd->cmd.statemove->end_state);
  261. /* shortest-path move to desired end state */
  262. if (tap_get_state() != tap_get_end_state() || tap_get_end_state() == TAP_RESET)
  263. move_to_state(tap_get_end_state());
  264. return retval;
  265. }
  266. /**
  267. * Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG
  268. * (or SWD) state machine. REVISIT: Not the best method, perhaps.
  269. */
  270. static int ftdi_execute_tms(struct jtag_command *cmd)
  271. {
  272. DEBUG_JTAG_IO("TMS: %d bits", cmd->cmd.tms->num_bits);
  273. /* TODO: Missing tap state tracking, also missing from ft2232.c! */
  274. return mpsse_clock_tms_cs_out(mpsse_ctx,
  275. cmd->cmd.tms->bits,
  276. 0,
  277. cmd->cmd.tms->num_bits,
  278. false,
  279. JTAG_MODE);
  280. }
  281. static int ftdi_execute_pathmove(struct jtag_command *cmd)
  282. {
  283. int retval = ERROR_OK;
  284. tap_state_t *path = cmd->cmd.pathmove->path;
  285. int num_states = cmd->cmd.pathmove->num_states;
  286. DEBUG_JTAG_IO("pathmove: %i states, current: %s end: %s", num_states,
  287. tap_state_name(tap_get_state()),
  288. tap_state_name(path[num_states-1]));
  289. int state_count = 0;
  290. unsigned bit_count = 0;
  291. uint8_t tms_byte = 0;
  292. DEBUG_JTAG_IO("-");
  293. /* this loop verifies that the path is legal and logs each state in the path */
  294. while (num_states-- && retval == ERROR_OK) {
  295. /* either TMS=0 or TMS=1 must work ... */
  296. if (tap_state_transition(tap_get_state(), false)
  297. == path[state_count])
  298. buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
  299. else if (tap_state_transition(tap_get_state(), true)
  300. == path[state_count]) {
  301. buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
  302. /* ... or else the caller goofed BADLY */
  303. } else {
  304. LOG_ERROR("BUG: %s -> %s isn't a valid "
  305. "TAP state transition",
  306. tap_state_name(tap_get_state()),
  307. tap_state_name(path[state_count]));
  308. exit(-1);
  309. }
  310. tap_set_state(path[state_count]);
  311. state_count++;
  312. if (bit_count == 7 || num_states == 0) {
  313. retval = mpsse_clock_tms_cs_out(mpsse_ctx,
  314. &tms_byte,
  315. 0,
  316. bit_count,
  317. false,
  318. JTAG_MODE);
  319. bit_count = 0;
  320. }
  321. }
  322. tap_set_end_state(tap_get_state());
  323. return retval;
  324. }
  325. static int ftdi_execute_scan(struct jtag_command *cmd)
  326. {
  327. int retval = ERROR_OK;
  328. DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
  329. jtag_scan_type(cmd->cmd.scan));
  330. /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
  331. while (cmd->cmd.scan->num_fields > 0
  332. && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
  333. cmd->cmd.scan->num_fields--;
  334. LOG_DEBUG("discarding trailing empty field");
  335. }
  336. if (cmd->cmd.scan->num_fields == 0) {
  337. LOG_DEBUG("empty scan, doing nothing");
  338. return retval;
  339. }
  340. if (cmd->cmd.scan->ir_scan) {
  341. if (tap_get_state() != TAP_IRSHIFT)
  342. move_to_state(TAP_IRSHIFT);
  343. } else {
  344. if (tap_get_state() != TAP_DRSHIFT)
  345. move_to_state(TAP_DRSHIFT);
  346. }
  347. ftdi_end_state(cmd->cmd.scan->end_state);
  348. struct scan_field *field = cmd->cmd.scan->fields;
  349. unsigned scan_size = 0;
  350. for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
  351. scan_size += field->num_bits;
  352. DEBUG_JTAG_IO("%s%s field %d/%d %d bits",
  353. field->in_value ? "in" : "",
  354. field->out_value ? "out" : "",
  355. i,
  356. cmd->cmd.scan->num_fields,
  357. field->num_bits);
  358. if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
  359. /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
  360. * movement. This last field can't have length zero, it was checked above. */
  361. mpsse_clock_data(mpsse_ctx,
  362. field->out_value,
  363. 0,
  364. field->in_value,
  365. 0,
  366. field->num_bits - 1,
  367. JTAG_MODE);
  368. uint8_t last_bit = 0;
  369. if (field->out_value)
  370. bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
  371. uint8_t tms_bits = 0x01;
  372. retval = mpsse_clock_tms_cs(mpsse_ctx,
  373. &tms_bits,
  374. 0,
  375. field->in_value,
  376. field->num_bits - 1,
  377. 1,
  378. last_bit,
  379. JTAG_MODE);
  380. tap_set_state(tap_state_transition(tap_get_state(), 1));
  381. retval = mpsse_clock_tms_cs_out(mpsse_ctx,
  382. &tms_bits,
  383. 1,
  384. 1,
  385. last_bit,
  386. JTAG_MODE);
  387. tap_set_state(tap_state_transition(tap_get_state(), 0));
  388. } else
  389. mpsse_clock_data(mpsse_ctx,
  390. field->out_value,
  391. 0,
  392. field->in_value,
  393. 0,
  394. field->num_bits,
  395. JTAG_MODE);
  396. if (retval != ERROR_OK) {
  397. LOG_ERROR("failed to add field %d in scan", i);
  398. return retval;
  399. }
  400. }
  401. if (tap_get_state() != tap_get_end_state())
  402. move_to_state(tap_get_end_state());
  403. DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
  404. (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
  405. tap_state_name(tap_get_end_state()));
  406. return retval;
  407. }
  408. static int ftdi_execute_reset(struct jtag_command *cmd)
  409. {
  410. DEBUG_JTAG_IO("reset trst: %i srst %i",
  411. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  412. if (cmd->cmd.reset->trst == 1
  413. || (cmd->cmd.reset->srst
  414. && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
  415. tap_set_state(TAP_RESET);
  416. struct signal *trst = find_signal_by_name("nTRST");
  417. if (trst && cmd->cmd.reset->trst == 1) {
  418. ftdi_set_signal(trst, '0');
  419. } else if (trst && cmd->cmd.reset->trst == 0) {
  420. if (jtag_get_reset_config() & RESET_TRST_OPEN_DRAIN)
  421. ftdi_set_signal(trst, 'z');
  422. else
  423. ftdi_set_signal(trst, '1');
  424. }
  425. struct signal *srst = find_signal_by_name("nSRST");
  426. if (srst && cmd->cmd.reset->srst == 1) {
  427. ftdi_set_signal(srst, '0');
  428. } else if (srst && cmd->cmd.reset->srst == 0) {
  429. if (jtag_get_reset_config() & RESET_SRST_PUSH_PULL)
  430. ftdi_set_signal(srst, '1');
  431. else
  432. ftdi_set_signal(srst, 'z');
  433. }
  434. DEBUG_JTAG_IO("trst: %i, srst: %i",
  435. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  436. return ERROR_OK;
  437. }
  438. static int ftdi_execute_sleep(struct jtag_command *cmd)
  439. {
  440. int retval = ERROR_OK;
  441. DEBUG_JTAG_IO("sleep %" PRIi32, cmd->cmd.sleep->us);
  442. retval = mpsse_flush(mpsse_ctx);
  443. jtag_sleep(cmd->cmd.sleep->us);
  444. DEBUG_JTAG_IO("sleep %" PRIi32 " usec while in %s",
  445. cmd->cmd.sleep->us,
  446. tap_state_name(tap_get_state()));
  447. return retval;
  448. }
  449. static int ftdi_execute_stableclocks(struct jtag_command *cmd)
  450. {
  451. int retval = ERROR_OK;
  452. /* this is only allowed while in a stable state. A check for a stable
  453. * state was done in jtag_add_clocks()
  454. */
  455. int num_cycles = cmd->cmd.stableclocks->num_cycles;
  456. /* 7 bits of either ones or zeros. */
  457. uint8_t tms = tap_get_state() == TAP_RESET ? 0x7f : 0x00;
  458. /* TODO: Use mpsse_clock_data with in=out=0 for this, if TMS can be set to
  459. * the correct level and remain there during the scan */
  460. while (num_cycles > 0 && retval == ERROR_OK) {
  461. /* there are no state transitions in this code, so omit state tracking */
  462. unsigned this_len = num_cycles > 7 ? 7 : num_cycles;
  463. retval = mpsse_clock_tms_cs_out(mpsse_ctx, &tms, 0, this_len, false, JTAG_MODE);
  464. num_cycles -= this_len;
  465. }
  466. DEBUG_JTAG_IO("clocks %i while in %s",
  467. cmd->cmd.stableclocks->num_cycles,
  468. tap_state_name(tap_get_state()));
  469. return retval;
  470. }
  471. static int ftdi_execute_command(struct jtag_command *cmd)
  472. {
  473. int retval;
  474. switch (cmd->type) {
  475. case JTAG_RESET:
  476. retval = ftdi_execute_reset(cmd);
  477. break;
  478. case JTAG_RUNTEST:
  479. retval = ftdi_execute_runtest(cmd);
  480. break;
  481. case JTAG_TLR_RESET:
  482. retval = ftdi_execute_statemove(cmd);
  483. break;
  484. case JTAG_PATHMOVE:
  485. retval = ftdi_execute_pathmove(cmd);
  486. break;
  487. case JTAG_SCAN:
  488. retval = ftdi_execute_scan(cmd);
  489. break;
  490. case JTAG_SLEEP:
  491. retval = ftdi_execute_sleep(cmd);
  492. break;
  493. case JTAG_STABLECLOCKS:
  494. retval = ftdi_execute_stableclocks(cmd);
  495. break;
  496. case JTAG_TMS:
  497. retval = ftdi_execute_tms(cmd);
  498. break;
  499. default:
  500. LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
  501. retval = ERROR_JTAG_QUEUE_FAILED;
  502. break;
  503. }
  504. return retval;
  505. }
  506. static int ftdi_execute_queue(void)
  507. {
  508. int retval = ERROR_OK;
  509. /* blink, if the current layout has that feature */
  510. struct signal *led = find_signal_by_name("LED");
  511. if (led)
  512. ftdi_set_signal(led, '1');
  513. for (struct jtag_command *cmd = jtag_command_queue; cmd; cmd = cmd->next) {
  514. /* fill the write buffer with the desired command */
  515. if (ftdi_execute_command(cmd) != ERROR_OK)
  516. retval = ERROR_JTAG_QUEUE_FAILED;
  517. }
  518. if (led)
  519. ftdi_set_signal(led, '0');
  520. retval = mpsse_flush(mpsse_ctx);
  521. if (retval != ERROR_OK)
  522. LOG_ERROR("error while flushing MPSSE queue: %d", retval);
  523. return retval;
  524. }
  525. static int ftdi_initialize(void)
  526. {
  527. int retval;
  528. if (tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRPAUSE) == 7)
  529. LOG_DEBUG("ftdi interface using 7 step jtag state transitions");
  530. else
  531. LOG_DEBUG("ftdi interface using shortest path jtag state transitions");
  532. for (int i = 0; ftdi_vid[i] || ftdi_pid[i]; i++) {
  533. mpsse_ctx = mpsse_open(&ftdi_vid[i], &ftdi_pid[i], ftdi_device_desc,
  534. ftdi_serial, ftdi_channel);
  535. if (mpsse_ctx)
  536. break;
  537. }
  538. if (!mpsse_ctx)
  539. return ERROR_JTAG_INIT_FAILED;
  540. retval = mpsse_set_data_bits_low_byte(mpsse_ctx, output & 0xff, direction & 0xff);
  541. if (retval == ERROR_OK)
  542. retval = mpsse_set_data_bits_high_byte(mpsse_ctx, output >> 8, direction >> 8);
  543. if (retval != ERROR_OK) {
  544. LOG_ERROR("couldn't initialize FTDI with configured layout");
  545. return ERROR_JTAG_INIT_FAILED;
  546. }
  547. retval = mpsse_loopback_config(mpsse_ctx, false);
  548. if (retval != ERROR_OK) {
  549. LOG_ERROR("couldn't write to FTDI to disable loopback");
  550. return ERROR_JTAG_INIT_FAILED;
  551. }
  552. return mpsse_flush(mpsse_ctx);
  553. }
  554. static int ftdi_quit(void)
  555. {
  556. mpsse_close(mpsse_ctx);
  557. return ERROR_OK;
  558. }
  559. COMMAND_HANDLER(ftdi_handle_device_desc_command)
  560. {
  561. if (CMD_ARGC == 1) {
  562. if (ftdi_device_desc)
  563. free(ftdi_device_desc);
  564. ftdi_device_desc = strdup(CMD_ARGV[0]);
  565. } else {
  566. LOG_ERROR("expected exactly one argument to ftdi_device_desc <description>");
  567. }
  568. return ERROR_OK;
  569. }
  570. COMMAND_HANDLER(ftdi_handle_serial_command)
  571. {
  572. if (CMD_ARGC == 1) {
  573. if (ftdi_serial)
  574. free(ftdi_serial);
  575. ftdi_serial = strdup(CMD_ARGV[0]);
  576. } else {
  577. return ERROR_COMMAND_SYNTAX_ERROR;
  578. }
  579. return ERROR_OK;
  580. }
  581. COMMAND_HANDLER(ftdi_handle_channel_command)
  582. {
  583. if (CMD_ARGC == 1)
  584. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], ftdi_channel);
  585. else
  586. return ERROR_COMMAND_SYNTAX_ERROR;
  587. return ERROR_OK;
  588. }
  589. COMMAND_HANDLER(ftdi_handle_layout_init_command)
  590. {
  591. if (CMD_ARGC != 2)
  592. return ERROR_COMMAND_SYNTAX_ERROR;
  593. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], output);
  594. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], direction);
  595. return ERROR_OK;
  596. }
  597. COMMAND_HANDLER(ftdi_handle_layout_signal_command)
  598. {
  599. if (CMD_ARGC < 1)
  600. return ERROR_COMMAND_SYNTAX_ERROR;
  601. bool invert_data = false;
  602. uint16_t data_mask = 0;
  603. bool invert_oe = false;
  604. uint16_t oe_mask = 0;
  605. for (unsigned i = 1; i < CMD_ARGC; i += 2) {
  606. if (strcmp("-data", CMD_ARGV[i]) == 0) {
  607. invert_data = false;
  608. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
  609. } else if (strcmp("-ndata", CMD_ARGV[i]) == 0) {
  610. invert_data = true;
  611. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
  612. } else if (strcmp("-oe", CMD_ARGV[i]) == 0) {
  613. invert_oe = false;
  614. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
  615. } else if (strcmp("-noe", CMD_ARGV[i]) == 0) {
  616. invert_oe = true;
  617. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
  618. } else {
  619. LOG_ERROR("unknown option '%s'", CMD_ARGV[i]);
  620. return ERROR_COMMAND_SYNTAX_ERROR;
  621. }
  622. }
  623. struct signal *sig;
  624. sig = find_signal_by_name(CMD_ARGV[0]);
  625. if (!sig)
  626. sig = create_signal(CMD_ARGV[0]);
  627. if (!sig) {
  628. LOG_ERROR("failed to create signal %s", CMD_ARGV[0]);
  629. return ERROR_FAIL;
  630. }
  631. sig->invert_data = invert_data;
  632. sig->data_mask = data_mask;
  633. sig->invert_oe = invert_oe;
  634. sig->oe_mask = oe_mask;
  635. return ERROR_OK;
  636. }
  637. COMMAND_HANDLER(ftdi_handle_set_signal_command)
  638. {
  639. if (CMD_ARGC < 2)
  640. return ERROR_COMMAND_SYNTAX_ERROR;
  641. struct signal *sig;
  642. sig = find_signal_by_name(CMD_ARGV[0]);
  643. if (!sig) {
  644. LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
  645. return ERROR_FAIL;
  646. }
  647. switch (*CMD_ARGV[1]) {
  648. case '0':
  649. case '1':
  650. case 'z':
  651. case 'Z':
  652. /* single character level specifier only */
  653. if (CMD_ARGV[1][1] == '\0') {
  654. ftdi_set_signal(sig, *CMD_ARGV[1]);
  655. break;
  656. }
  657. default:
  658. LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV[1]);
  659. return ERROR_COMMAND_SYNTAX_ERROR;
  660. }
  661. return mpsse_flush(mpsse_ctx);
  662. }
  663. COMMAND_HANDLER(ftdi_handle_vid_pid_command)
  664. {
  665. if (CMD_ARGC > MAX_USB_IDS * 2) {
  666. LOG_WARNING("ignoring extra IDs in ftdi_vid_pid "
  667. "(maximum is %d pairs)", MAX_USB_IDS);
  668. CMD_ARGC = MAX_USB_IDS * 2;
  669. }
  670. if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
  671. LOG_WARNING("incomplete ftdi_vid_pid configuration directive");
  672. if (CMD_ARGC < 2)
  673. return ERROR_COMMAND_SYNTAX_ERROR;
  674. /* remove the incomplete trailing id */
  675. CMD_ARGC -= 1;
  676. }
  677. unsigned i;
  678. for (i = 0; i < CMD_ARGC; i += 2) {
  679. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], ftdi_vid[i >> 1]);
  680. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], ftdi_pid[i >> 1]);
  681. }
  682. /*
  683. * Explicitly terminate, in case there are multiples instances of
  684. * ftdi_vid_pid.
  685. */
  686. ftdi_vid[i >> 1] = ftdi_pid[i >> 1] = 0;
  687. return ERROR_OK;
  688. }
  689. static const struct command_registration ftdi_command_handlers[] = {
  690. {
  691. .name = "ftdi_device_desc",
  692. .handler = &ftdi_handle_device_desc_command,
  693. .mode = COMMAND_CONFIG,
  694. .help = "set the USB device description of the FTDI device",
  695. .usage = "description_string",
  696. },
  697. {
  698. .name = "ftdi_serial",
  699. .handler = &ftdi_handle_serial_command,
  700. .mode = COMMAND_CONFIG,
  701. .help = "set the serial number of the FTDI device",
  702. .usage = "serial_string",
  703. },
  704. {
  705. .name = "ftdi_channel",
  706. .handler = &ftdi_handle_channel_command,
  707. .mode = COMMAND_CONFIG,
  708. .help = "set the channel of the FTDI device that is used as JTAG",
  709. .usage = "(0-3)",
  710. },
  711. {
  712. .name = "ftdi_layout_init",
  713. .handler = &ftdi_handle_layout_init_command,
  714. .mode = COMMAND_CONFIG,
  715. .help = "initialize the FTDI GPIO signals used "
  716. "to control output-enables and reset signals",
  717. .usage = "data direction",
  718. },
  719. {
  720. .name = "ftdi_layout_signal",
  721. .handler = &ftdi_handle_layout_signal_command,
  722. .mode = COMMAND_ANY,
  723. .help = "define a signal controlled by one or more FTDI GPIO as data "
  724. "and/or output enable",
  725. .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask]",
  726. },
  727. {
  728. .name = "ftdi_set_signal",
  729. .handler = &ftdi_handle_set_signal_command,
  730. .mode = COMMAND_EXEC,
  731. .help = "control a layout-specific signal",
  732. .usage = "name (1|0|z)",
  733. },
  734. {
  735. .name = "ftdi_vid_pid",
  736. .handler = &ftdi_handle_vid_pid_command,
  737. .mode = COMMAND_CONFIG,
  738. .help = "the vendor ID and product ID of the FTDI device",
  739. .usage = "(vid pid)* ",
  740. },
  741. COMMAND_REGISTRATION_DONE
  742. };
  743. struct jtag_interface ftdi_interface = {
  744. .name = "ftdi",
  745. .supported = DEBUG_CAP_TMS_SEQ,
  746. .commands = ftdi_command_handlers,
  747. .transports = jtag_only,
  748. .init = ftdi_initialize,
  749. .quit = ftdi_quit,
  750. .speed = ftdi_speed,
  751. .speed_div = ftdi_speed_div,
  752. .khz = ftdi_khz,
  753. .execute_queue = ftdi_execute_queue,
  754. };