OpenOCD
ftdi.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /**************************************************************************
4 * Copyright (C) 2012 by Andreas Fritiofson *
5 * andreas.fritiofson@gmail.com *
6 ***************************************************************************/
7 
56 #ifdef HAVE_CONFIG_H
57 #include "config.h"
58 #endif
59 
60 /* project specific includes */
61 #include <jtag/adapter.h>
62 #include <jtag/interface.h>
63 #include <jtag/swd.h>
64 #include <transport/transport.h>
65 #include <helper/time_support.h>
66 #include <helper/log.h>
67 #include <helper/nvp.h>
68 
69 #if IS_CYGWIN == 1
70 #include <windows.h>
71 #endif
72 
73 #include <assert.h>
74 
75 /* FTDI access library includes */
76 #include "mpsse.h"
77 
78 #if BUILD_FTDI_CJTAG == 1
79 #define DO_CLOCK_DATA clock_data
80 #define DO_CLOCK_TMS_CS clock_tms_cs
81 #define DO_CLOCK_TMS_CS_OUT clock_tms_cs_out
82 #else
83 #define DO_CLOCK_DATA mpsse_clock_data
84 #define DO_CLOCK_TMS_CS mpsse_clock_tms_cs
85 #define DO_CLOCK_TMS_CS_OUT mpsse_clock_tms_cs_out
86 #endif
87 
88 #define JTAG_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
89 #define JTAG_MODE_ALT (LSB_FIRST | NEG_EDGE_IN | NEG_EDGE_OUT)
90 #define SWD_MODE (LSB_FIRST | POS_EDGE_IN | NEG_EDGE_OUT)
91 
92 static uint8_t ftdi_channel;
93 static uint8_t ftdi_jtag_mode = JTAG_MODE;
94 
95 static bool swd_mode;
96 
97 #if BUILD_FTDI_CJTAG == 1
98 #define ESCAPE_SEQ_OAC_BIT2 28
99 
100 static void cjtag_reset_online_activate(void);
101 
102 /*
103  The cJTAG 2-wire OScan1 protocol, in lieu of 4-wire JTAG, is a configuration option
104  for some SoCs. An FTDI-based adapter that can be configured to appropriately drive
105  the bidirectional pin TMSC is able to drive OScan1 protocol. For example, an Olimex
106  ARM-USB-TINY-H with the ARM-JTAG-SWD adapter, connected to a cJTAG-enabled
107  target board is such a topology. A TCK cycle with TMS=1/TDI=N translates to a TMSC
108  output of N, and a TCK cycle with TMS=0 translates to a TMSC input from the target back
109  to the adapter/probe. The OScan1 protocol uses 3 TCK cycles to generate the data flow
110  that is equivalent to that of a single TCK cycle in 4-wire JTAG. The OScan1-related
111  code in this module translates IR/DR scan commanads and JTAG state traversal commands
112  to the two-wire clocking and signaling of OScan1 protocol, if placed into OScan1 mode
113  during initialization.
114 */
115 static void oscan1_mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
116  unsigned int in_offset, unsigned int length, uint8_t mode);
117 static void oscan1_mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
118  unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode);
119 static void oscan1_mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset,
120  unsigned int length, bool tdi, uint8_t mode);
121 
122 static bool oscan1_mode;
123 
124 /*
125  The cJTAG 4-wire JScan3 allows to use standard JTAG protocol with cJTAG hardware
126 */
127 static bool jscan3_mode;
128 #endif
129 
130 static struct mpsse_ctx *mpsse_ctx;
131 
132 struct signal {
133  const char *name;
134  uint16_t data_mask;
135  uint16_t input_mask;
136  uint16_t oe_mask;
139  bool invert_oe;
140  struct signal *next;
141 };
142 
143 static struct signal *signals;
144 
145 /* FIXME: Where to store per-instance data? We need an SWD context. */
146 static struct swd_cmd_queue_entry {
147  uint8_t cmd;
148  uint32_t *dst;
149  uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4 + 3 + 32 + 1 + 4, 8)];
151 static size_t swd_cmd_queue_length;
152 static size_t swd_cmd_queue_alloced;
153 static int queued_retval;
154 static int freq;
155 
156 static uint16_t output;
157 static uint16_t direction;
158 static uint16_t jtag_output_init;
159 static uint16_t jtag_direction_init;
160 
161 static int ftdi_swd_switch_seq(enum swd_special_seq seq);
162 
163 static struct signal *find_signal_by_name(const char *name)
164 {
165  for (struct signal *sig = signals; sig; sig = sig->next) {
166  if (strcmp(name, sig->name) == 0)
167  return sig;
168  }
169  return NULL;
170 }
171 
172 static struct signal *create_signal(const char *name)
173 {
174  struct signal **psig = &signals;
175  while (*psig)
176  psig = &(*psig)->next;
177 
178  *psig = calloc(1, sizeof(**psig));
179  if (!*psig)
180  return NULL;
181 
182  (*psig)->name = strdup(name);
183  if (!(*psig)->name) {
184  free(*psig);
185  *psig = NULL;
186  }
187  return *psig;
188 }
189 
190 static int ftdi_set_signal(const struct signal *s, char value)
191 {
192  bool data;
193  bool oe;
194 
195  if (s->data_mask == 0 && s->oe_mask == 0) {
196  LOG_ERROR("interface doesn't provide signal '%s'", s->name);
197  return ERROR_FAIL;
198  }
199  switch (value) {
200  case '0':
201  data = s->invert_data;
202  oe = !s->invert_oe;
203  break;
204  case '1':
205  if (s->data_mask == 0) {
206  LOG_ERROR("interface can't drive '%s' high", s->name);
207  return ERROR_FAIL;
208  }
209  data = !s->invert_data;
210  oe = !s->invert_oe;
211  break;
212  case 'z':
213  case 'Z':
214  if (s->oe_mask == 0) {
215  LOG_ERROR("interface can't tri-state '%s'", s->name);
216  return ERROR_FAIL;
217  }
218  data = s->invert_data;
219  oe = s->invert_oe;
220  break;
221  default:
222  LOG_ERROR("invalid signal level specifier \'%c\'(0x%02x)", value, value);
223  return ERROR_FAIL;
224  }
225 
226  uint16_t old_output = output;
227  uint16_t old_direction = direction;
228 
229  output = data ? output | s->data_mask : output & ~s->data_mask;
230  if (s->oe_mask == s->data_mask)
231  direction = oe ? direction | s->oe_mask : direction & ~s->oe_mask;
232  else
233  output = oe ? output | s->oe_mask : output & ~s->oe_mask;
234 
235  if ((output & 0xff) != (old_output & 0xff) || (direction & 0xff) != (old_direction & 0xff))
237  if ((output >> 8 != old_output >> 8) || (direction >> 8 != old_direction >> 8))
239 
240  return ERROR_OK;
241 }
242 
243 static int ftdi_get_signal(const struct signal *s, uint16_t *value_out)
244 {
245  uint8_t data_low = 0;
246  uint8_t data_high = 0;
247 
248  if (s->input_mask == 0) {
249  LOG_ERROR("interface doesn't provide signal '%s'", s->name);
250  return ERROR_FAIL;
251  }
252 
253  if (s->input_mask & 0xff)
255  if (s->input_mask >> 8)
257 
259 
260  *value_out = (((uint16_t)data_high) << 8) | data_low;
261 
262  if (s->invert_input)
263  *value_out = ~(*value_out);
264 
265  *value_out &= s->input_mask;
266 
267  return ERROR_OK;
268 }
269 
270 #if BUILD_FTDI_CJTAG == 1
271 static void clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
272  unsigned int in_offset, unsigned int length, uint8_t mode)
273 {
274  if (oscan1_mode)
275  oscan1_mpsse_clock_data(ctx, out, out_offset, in, in_offset, length, mode);
276  else
277  mpsse_clock_data(ctx, out, out_offset, in, in_offset, length, mode);
278 }
279 
280 static void clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
281  unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
282 {
283  if (oscan1_mode)
284  oscan1_mpsse_clock_tms_cs(ctx, out, out_offset, in, in_offset, length, tdi, mode);
285  else
286  mpsse_clock_tms_cs(ctx, out, out_offset, in, in_offset, length, tdi, mode);
287 }
288 
289 static void clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset,
290  unsigned int length, bool tdi, uint8_t mode)
291 {
292  if (oscan1_mode)
293  oscan1_mpsse_clock_tms_cs_out(ctx, out, out_offset, length, tdi, mode);
294  else
295  mpsse_clock_tms_cs_out(ctx, out, out_offset, length, tdi, mode);
296 }
297 #endif
298 
307 static void move_to_state(enum tap_state goal_state)
308 {
309  enum tap_state start_state = tap_get_state();
310 
311  /* goal_state is 1/2 of a tuple/pair of states which allow convenient
312  lookup of the required TMS pattern to move to this state from the
313  start state.
314  */
315 
316  /* do the 2 lookups */
317  uint8_t tms_bits = tap_get_tms_path(start_state, goal_state);
318  int tms_count = tap_get_tms_path_len(start_state, goal_state);
319  assert(tms_count <= 8);
320 
321  LOG_DEBUG_IO("start=%s goal=%s", tap_state_name(start_state), tap_state_name(goal_state));
322 
323  /* Track state transitions step by step */
324  for (int i = 0; i < tms_count; i++)
325  tap_set_state(tap_state_transition(tap_get_state(), (tms_bits >> i) & 1));
326 
328  &tms_bits,
329  0,
330  tms_count,
331  false,
333 }
334 
335 static int ftdi_speed(int speed)
336 {
337  int retval;
338  retval = mpsse_set_frequency(mpsse_ctx, speed);
339 
340  if (retval < 0) {
341  LOG_ERROR("couldn't set FTDI TCK speed");
342  return retval;
343  }
344 
345  if (!swd_mode && speed >= 10000000 && ftdi_jtag_mode != JTAG_MODE_ALT)
346  LOG_INFO("ftdi: if you experience problems at higher adapter clocks, try "
347  "the command \"ftdi tdo_sample_edge falling\"");
348  return ERROR_OK;
349 }
350 
351 static int ftdi_speed_div(int speed, int *khz)
352 {
353  *khz = speed / 1000;
354  return ERROR_OK;
355 }
356 
357 static int ftdi_khz(int khz, int *jtag_speed)
358 {
359  if (khz == 0 && !mpsse_is_high_speed(mpsse_ctx)) {
360  LOG_DEBUG("RCLK not supported");
361  return ERROR_FAIL;
362  }
363 
364  *jtag_speed = khz * 1000;
365  return ERROR_OK;
366 }
367 
368 static void ftdi_end_state(enum tap_state state)
369 {
372  else {
373  LOG_ERROR("BUG: %s is not a stable end state", tap_state_name(state));
374  exit(-1);
375  }
376 }
377 
379 {
380  uint8_t zero = 0;
381 
382  LOG_DEBUG_IO("runtest %u cycles, end in %s",
383  cmd->cmd.runtest->num_cycles,
384  tap_state_name(cmd->cmd.runtest->end_state));
385 
386  if (tap_get_state() != TAP_IDLE)
388 
389  /* TODO: Reuse ftdi_execute_stableclocks */
390  unsigned int i = cmd->cmd.runtest->num_cycles;
391  while (i > 0) {
392  /* there are no state transitions in this code, so omit state tracking */
393  unsigned int this_len = i > 7 ? 7 : i;
394  DO_CLOCK_TMS_CS_OUT(mpsse_ctx, &zero, 0, this_len, false, ftdi_jtag_mode);
395  i -= this_len;
396  }
397 
398  ftdi_end_state(cmd->cmd.runtest->end_state);
399 
400  if (tap_get_state() != tap_get_end_state())
402 
403  LOG_DEBUG_IO("runtest: %u, end in %s",
404  cmd->cmd.runtest->num_cycles,
406 }
407 
409 {
410  LOG_DEBUG_IO("statemove end in %s",
411  tap_state_name(cmd->cmd.statemove->end_state));
412 
413  ftdi_end_state(cmd->cmd.statemove->end_state);
414 
415  /* shortest-path move to desired end state */
418 }
419 
424 static void ftdi_execute_tms(struct jtag_command *cmd)
425 {
426  LOG_DEBUG_IO("TMS: %u bits", cmd->cmd.tms->num_bits);
427 
428  /* TODO: Missing tap state tracking, also missing from ft2232.c! */
430  cmd->cmd.tms->bits,
431  0,
432  cmd->cmd.tms->num_bits,
433  false,
435 }
436 
438 {
439  enum tap_state *path = cmd->cmd.pathmove->path;
440  unsigned int num_states = cmd->cmd.pathmove->num_states;
441 
442  LOG_DEBUG_IO("pathmove: %u states, current: %s end: %s", num_states,
444  tap_state_name(path[num_states-1]));
445 
446  int state_count = 0;
447  unsigned int bit_count = 0;
448  uint8_t tms_byte = 0;
449 
450  LOG_DEBUG_IO("-");
451 
452  /* this loop verifies that the path is legal and logs each state in the path */
453  while (num_states--) {
454 
455  /* either TMS=0 or TMS=1 must work ... */
456  if (tap_state_transition(tap_get_state(), false)
457  == path[state_count])
458  buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
459  else if (tap_state_transition(tap_get_state(), true)
460  == path[state_count]) {
461  buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
462 
463  /* ... or else the caller goofed BADLY */
464  } else {
465  LOG_ERROR("BUG: %s -> %s isn't a valid "
466  "TAP state transition",
468  tap_state_name(path[state_count]));
469  exit(-1);
470  }
471 
472  tap_set_state(path[state_count]);
473  state_count++;
474 
475  if (bit_count == 7 || num_states == 0) {
477  &tms_byte,
478  0,
479  bit_count,
480  false,
482  bit_count = 0;
483  }
484  }
486 }
487 
488 static void ftdi_execute_scan(struct jtag_command *cmd)
489 {
490  LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
491  jtag_scan_type(cmd->cmd.scan));
492 
493  /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
494  while (cmd->cmd.scan->num_fields > 0
495  && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
496  cmd->cmd.scan->num_fields--;
497  LOG_DEBUG_IO("discarding trailing empty field");
498  }
499 
500  if (!cmd->cmd.scan->num_fields) {
501  LOG_DEBUG_IO("empty scan, doing nothing");
502  return;
503  }
504 
505  if (cmd->cmd.scan->ir_scan) {
506  if (tap_get_state() != TAP_IRSHIFT)
508  } else {
509  if (tap_get_state() != TAP_DRSHIFT)
511  }
512 
513  ftdi_end_state(cmd->cmd.scan->end_state);
514 
515  struct scan_field *field = cmd->cmd.scan->fields;
516  unsigned int scan_size = 0;
517 
518  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
519  scan_size += field->num_bits;
520  LOG_DEBUG_IO("%s%s field %u/%u %u bits",
521  field->in_value ? "in" : "",
522  field->out_value ? "out" : "",
523  i,
524  cmd->cmd.scan->num_fields,
525  field->num_bits);
526 
527  if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
528  /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
529  * movement. This last field can't have length zero, it was checked above. */
531  field->out_value,
532  0,
533  field->in_value,
534  0,
535  field->num_bits - 1,
537  uint8_t last_bit = 0;
538  if (field->out_value)
539  bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
540 
541  /* If endstate is TAP_IDLE, clock out 1-1-0 (->EXIT1 ->UPDATE ->IDLE)
542  * Otherwise, clock out 1-0 (->EXIT1 ->PAUSE)
543  */
544  uint8_t tms_bits = 0x03;
546  &tms_bits,
547  0,
548  field->in_value,
549  field->num_bits - 1,
550  1,
551  last_bit,
554  if (tap_get_end_state() == TAP_IDLE) {
556  &tms_bits,
557  1,
558  2,
559  last_bit,
563  } else {
565  &tms_bits,
566  2,
567  1,
568  last_bit,
571  }
572  } else
574  field->out_value,
575  0,
576  field->in_value,
577  0,
578  field->num_bits,
580  }
581 
582  if (tap_get_state() != tap_get_end_state())
584 
585  LOG_DEBUG_IO("%s scan, %i bits, end in %s",
586  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
588 }
589 
590 static int ftdi_reset(int trst, int srst)
591 {
592  struct signal *sig_ntrst = find_signal_by_name("nTRST");
593  struct signal *sig_nsrst = find_signal_by_name("nSRST");
594 
595  LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
596 
597  if (!swd_mode) {
598  if (trst == 1) {
599  if (sig_ntrst)
600  ftdi_set_signal(sig_ntrst, '0');
601  else
602  LOG_ERROR("Can't assert TRST: nTRST signal is not defined");
603  } else if (sig_ntrst && jtag_get_reset_config() & RESET_HAS_TRST &&
604  trst == 0) {
606  ftdi_set_signal(sig_ntrst, 'z');
607  else
608  ftdi_set_signal(sig_ntrst, '1');
609  }
610  }
611 
612  if (srst == 1) {
613  if (sig_nsrst)
614  ftdi_set_signal(sig_nsrst, '0');
615  else
616  LOG_ERROR("Can't assert SRST: nSRST signal is not defined");
617  } else if (sig_nsrst && jtag_get_reset_config() & RESET_HAS_SRST &&
618  srst == 0) {
620  ftdi_set_signal(sig_nsrst, '1');
621  else
622  ftdi_set_signal(sig_nsrst, 'z');
623  }
624 
625  return mpsse_flush(mpsse_ctx);
626 }
627 
628 static void ftdi_execute_sleep(struct jtag_command *cmd)
629 {
630  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
631 
633  jtag_sleep(cmd->cmd.sleep->us);
634  LOG_DEBUG_IO("sleep %" PRIu32 " usec while in %s",
635  cmd->cmd.sleep->us,
637 }
638 
640 {
641  /* this is only allowed while in a stable state. A check for a stable
642  * state was done in jtag_add_clocks()
643  */
644  unsigned int num_cycles = cmd->cmd.stableclocks->num_cycles;
645 
646  /* 7 bits of either ones or zeros. */
647  uint8_t tms = tap_get_state() == TAP_RESET ? 0x7f : 0x00;
648 
649  /* TODO: Use mpsse_clock_data with in=out=0 for this, if TMS can be set to
650  * the correct level and remain there during the scan */
651  while (num_cycles > 0) {
652  /* there are no state transitions in this code, so omit state tracking */
653  unsigned int this_len = num_cycles > 7 ? 7 : num_cycles;
654  DO_CLOCK_TMS_CS_OUT(mpsse_ctx, &tms, 0, this_len, false, ftdi_jtag_mode);
655  num_cycles -= this_len;
656  }
657 
658  LOG_DEBUG_IO("clocks %u while in %s",
659  cmd->cmd.stableclocks->num_cycles,
661 }
662 
664 {
665  switch (cmd->type) {
666 #if BUILD_FTDI_CJTAG == 1
667  case JTAG_RESET:
668  if (cmd->cmd.reset->trst)
669  cjtag_reset_online_activate(); /* put the target (back) into selected cJTAG mode */
670  break;
671 #endif
672  case JTAG_RUNTEST:
674  break;
675  case JTAG_TLR_RESET:
676 #if BUILD_FTDI_CJTAG == 1
677  cjtag_reset_online_activate(); /* put the target (back) into selected cJTAG mode */
678 #endif
680  break;
681  case JTAG_PATHMOVE:
683  break;
684  case JTAG_SCAN:
686  break;
687  case JTAG_SLEEP:
689  break;
690  case JTAG_STABLECLOCKS:
692  break;
693  case JTAG_TMS:
695  break;
696  default:
697  LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
698  break;
699  }
700 }
701 
702 static int ftdi_execute_queue(struct jtag_command *cmd_queue)
703 {
704  /* blink, if the current layout has that feature */
705  struct signal *led = find_signal_by_name("LED");
706  if (led)
707  ftdi_set_signal(led, '1');
708 
709  for (struct jtag_command *cmd = cmd_queue; cmd; cmd = cmd->next) {
710  /* fill the write buffer with the desired command */
712  }
713 
714  if (led)
715  ftdi_set_signal(led, '0');
716 
717  int retval = mpsse_flush(mpsse_ctx);
718  if (retval != ERROR_OK)
719  LOG_ERROR("error while flushing MPSSE queue: %d", retval);
720 
721  return retval;
722 }
723 
724 static int ftdi_initialize(void)
725 {
727  LOG_DEBUG("ftdi interface using 7 step jtag state transitions");
728  else
729  LOG_DEBUG("ftdi interface using shortest path jtag state transitions");
730 
731  if (!adapter_usb_get_vids()[0] && !adapter_usb_get_pids()[0]) {
732  LOG_ERROR("Please specify 'adapter usb vid_pid'");
733  return ERROR_JTAG_INIT_FAILED;
734  }
735 
739  if (!mpsse_ctx)
740  return ERROR_JTAG_INIT_FAILED;
741 
744 
745  if (swd_mode) {
746  struct signal *sig = find_signal_by_name("SWD_EN");
747  if (!sig) {
748  LOG_ERROR("SWD mode is active but SWD_EN signal is not defined");
749  return ERROR_JTAG_INIT_FAILED;
750  }
751  /* A dummy SWD_EN would have zero mask */
752  if (sig->data_mask)
753  ftdi_set_signal(sig, '1');
754 #if BUILD_FTDI_CJTAG == 1
755  } else if (oscan1_mode || jscan3_mode) {
756  struct signal *sig = find_signal_by_name("JTAG_SEL");
757  if (!sig) {
758  LOG_ERROR("A cJTAG mode is active but JTAG_SEL signal is not defined");
759  return ERROR_JTAG_INIT_FAILED;
760  }
761  /* A dummy JTAG_SEL would have zero mask */
762  if (sig->data_mask) {
763  ftdi_set_signal(sig, '0');
764  } else if (jscan3_mode) {
765  LOG_ERROR("In JScan3 mode JTAG_SEL signal cannot be dummy, data mask needed");
766  return ERROR_JTAG_INIT_FAILED;
767  }
768 #endif
769  }
770 
773 
775 
777 
778  return mpsse_flush(mpsse_ctx);
779 }
780 
781 static int ftdi_quit(void)
782 {
784 
785  struct signal *sig = signals;
786  while (sig) {
787  struct signal *next = sig->next;
788  free((void *)sig->name);
789  free(sig);
790  sig = next;
791  }
792 
793  free(swd_cmd_queue);
794 
795  return ERROR_OK;
796 }
797 
798 #if BUILD_FTDI_CJTAG == 1
799 static void oscan1_mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
800  unsigned int in_offset, unsigned int length, uint8_t mode)
801 {
802  static const uint8_t zero;
803  static const uint8_t one = 1;
804 
805  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
806 
807  LOG_DEBUG_IO("%sout %d bits", in ? "in" : "", length);
808 
809  for (unsigned int i = 0; i < length; i++) {
810  int bitnum;
811  uint8_t bit;
812 
813  /* OScan1 uses 3 separate clocks */
814 
815  /* drive TMSC to the *negation* of the desired TDI value */
816  bitnum = out_offset + i;
817  bit = out ? ((out[bitnum / 8] >> (bitnum % 8)) & 0x1) : 0;
818 
819  /* Try optimized case first: if desired TDI bit is 1, then we
820  can fuse what would otherwise be the first two MPSSE commands */
821  if (bit) {
822  const uint8_t tmsbits = 0x3; /* 1, 1 */
823  mpsse_clock_tms_cs_out(mpsse_ctx, &tmsbits, 0, 2, false, mode);
824  } else {
825  /* Can't fuse because TDI varies; less efficient */
826  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, bit ? 0 : 1, mode);
827 
828  /* drive TMSC to desired TMS value (always zero in this context) */
829  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, false, mode);
830  }
831 
832  if (tmsc_en)
833  ftdi_set_signal(tmsc_en, '0'); /* put TMSC in high impedance */
834 
835  /* drive another TCK without driving TMSC (TDO cycle) */
836  mpsse_clock_tms_cs(mpsse_ctx, &zero, 0, in, in_offset + i, 1, false, mode);
837 
838  if (tmsc_en)
839  ftdi_set_signal(tmsc_en, '1'); /* drive again TMSC */
840  }
841 }
842 
843 static void oscan1_mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
844  unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
845 {
846  static const uint8_t zero;
847  static const uint8_t one = 1;
848 
849  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
850 
851  LOG_DEBUG_IO("%sout %d bits, tdi=%d", in ? "in" : "", length, tdi);
852 
853  for (unsigned int i = 0; i < length; i++) {
854  int bitnum;
855  uint8_t tmsbit;
856  uint8_t tdibit;
857 
858  /* OScan1 uses 3 separate clocks */
859 
860  /* drive TMSC to the *negation* of the desired TDI value */
861  tdibit = tdi ? 0 : 1;
862 
863  /* drive TMSC to desired TMS value */
864  bitnum = out_offset + i;
865  tmsbit = ((out[bitnum / 8] >> (bitnum % 8)) & 0x1);
866 
867  if (tdibit == tmsbit) {
868  /* Can squash into a single MPSSE command */
869  const uint8_t tmsbits = 0x3;
870  mpsse_clock_tms_cs_out(mpsse_ctx, &tmsbits, 0, 2, tdibit, mode);
871  } else {
872  /* Unoptimized case, can't formulate with a single command */
873  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, tdibit, mode);
874  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, (tmsbit != 0), mode);
875  }
876 
877  if (tmsc_en)
878  ftdi_set_signal(tmsc_en, '0'); /* put TMSC in high impedance */
879 
880  /* drive another TCK without driving TMSC (TDO cycle) */
881  mpsse_clock_tms_cs(mpsse_ctx, &zero, 0, in, in_offset + i, 1, false, mode);
882 
883  if (tmsc_en)
884  ftdi_set_signal(tmsc_en, '1'); /* drive again TMSC */
885  }
886 }
887 
888 static void oscan1_mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset,
889  unsigned int length, bool tdi, uint8_t mode)
890 {
891  oscan1_mpsse_clock_tms_cs(ctx, out, out_offset, 0, 0, length, tdi, mode);
892 }
893 
894 static void cjtag_set_tck_tms_tdi(struct signal *tck, char tckvalue, struct signal *tms,
895  char tmsvalue, struct signal *tdi, char tdivalue)
896 {
897  ftdi_set_signal(tms, tmsvalue);
898  ftdi_set_signal(tdi, tdivalue);
899  ftdi_set_signal(tck, tckvalue);
900 }
901 
902 static void cjtag_reset_online_activate(void)
903 {
904  /* After TAP reset, the cJTAG-to-JTAG adapter is in offline and
905  non-activated state. Escape sequences are needed to bring the
906  TAP online and activated into the desired working mode. */
907 
908  struct signal *tck = find_signal_by_name("TCK");
909  struct signal *tdi = find_signal_by_name("TDI");
910  struct signal *tms = find_signal_by_name("TMS");
911  struct signal *tdo = find_signal_by_name("TDO");
912  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
913  uint16_t tdovalue;
914 
915  static struct {
916  int8_t tck;
917  int8_t tms;
918  int8_t tdi;
919  } sequence[] = {
920  /* TCK=0, TMS=1, TDI=0 (drive TMSC to 0 baseline) */
921  {'0', '1', '0'},
922 
923  /* Drive cJTAG escape sequence for TAP reset - 8 TMSC edges */
924  /* TCK=1, TMS=1, TDI=0 (rising edge of TCK with TMSC still 0) */
925  {'1', '1', '0'},
926  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
927  {'1', '1', '1'},
928  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
929  {'1', '1', '0'},
930  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
931  {'1', '1', '1'},
932  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
933  {'1', '1', '0'},
934  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
935  {'1', '1', '1'},
936  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
937  {'1', '1', '0'},
938  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
939  {'1', '1', '1'},
940  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
941  {'1', '1', '0'},
942  /* TCK=0, TMS=1, TDI=0 (falling edge TCK with TMSC still 0) */
943  {'0', '1', '0'},
944 
945  /* 3 TCK pulses for padding */
946  /* TCK=1, TMS=1, TDI=0 (drive rising TCK edge) */
947  {'1', '1', '0'},
948  /* TCK=0, TMS=1, TDI=0 (drive falling TCK edge) */
949  {'0', '1', '0'},
950  /* TCK=1, TMS=1, TDI=0 (drive rising TCK edge) */
951  {'1', '1', '0'},
952  /* TCK=0, TMS=1, TDI=0 (drive falling TCK edge) */
953  {'0', '1', '0'},
954  /* TCK=1, TMS=1, TDI=0 (drive rising TCK edge) */
955  {'1', '1', '0'},
956  /* TCK=0, TMS=1, TDI=0 (drive falling TCK edge) */
957  {'0', '1', '0'},
958 
959  /* Drive cJTAG escape sequence for SELECT */
960  /* TCK=1, TMS=1, TDI=0 (rising edge of TCK with TMSC still 0, TAP reset that was just setup occurs here too) */
961  {'1', '1', '0'},
962  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
963  {'1', '1', '1'},
964  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
965  {'1', '1', '0'},
966  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
967  {'1', '1', '1'},
968  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
969  {'1', '1', '0'},
970  /* TCK=1, TMS=1, TDI=1 (drive rising TMSC edge) */
971  {'1', '1', '1'},
972  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
973  {'1', '1', '0'},
974  /* TCK=0, TMS=1, TDI=0 (falling edge TCK with TMSC still 0) */
975  {'0', '1', '0'},
976 
977  /* Drive cJTAG escape sequence for OScan1 activation -- OAC = 1100 -> 2 wires -- */
978  /* TCK=1, TMS=1, TDI=0 (rising edge TCK with TMSC still 0... online mode activated... also OAC bit0==0) */
979  {'1', '1', '0'},
980  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
981  {'0', '1', '0'},
982  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... OAC bit1==0) */
983  {'1', '1', '0'},
984  /* TCK=0, TMS=1, TDI=1 (falling edge TCK) */
985  {'0', '1', '1'},
986  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... OAC bit2==1) */
987  {'1', '1', '1'},
988  /* TCK=0, TMS=1, TDI=1 (falling edge TCK, TMSC stays high) */
989  {'0', '1', '1'},
990  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... OAC bit3==1) */
991  {'1', '1', '1'},
992  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
993  {'0', '1', '0'},
994  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... EC bit0==0) */
995  {'1', '1', '0'},
996  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
997  {'0', '1', '0'},
998  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... EC bit1==0) */
999  {'1', '1', '0'},
1000  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1001  {'0', '1', '0'},
1002  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... EC bit2==0) */
1003  {'1', '1', '0'},
1004  /* TCK=0, TMS=1, TDI=1 (falling edge TCK) */
1005  {'0', '1', '1'},
1006  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... EC bit3==1) */
1007  {'1', '1', '1'},
1008  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1009  {'0', '1', '0'},
1010  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit0==0) */
1011  {'1', '1', '0'},
1012  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1013  {'0', '1', '0'},
1014  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit1==0) */
1015  {'1', '1', '0'},
1016  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1017  {'0', '1', '0'},
1018  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit2==0) */
1019  {'1', '1', '0'},
1020  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1021  {'0', '1', '0'},
1022  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit3==0) */
1023  {'1', '1', '0'},
1024  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1025  {'0', '1', '0'},
1026  };
1027 
1028  if (!oscan1_mode && !jscan3_mode)
1029  return; /* Nothing to do */
1030 
1031  if (oscan1_mode && jscan3_mode) {
1032  LOG_ERROR("Both oscan1_mode and jscan3_mode are \"on\". At most one of them can be enabled.");
1033  return;
1034  }
1035 
1036  if (!tck) {
1037  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TCK signal is not defined");
1038  return;
1039  }
1040 
1041  if (!tdi) {
1042  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TDI signal is not defined");
1043  return;
1044  }
1045 
1046  if (!tms) {
1047  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TMS signal is not defined");
1048  return;
1049  }
1050 
1051  if (!tdo) {
1052  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TDO signal is not defined");
1053  return;
1054  }
1055 
1056  if (jscan3_mode) {
1057  /* Update the sequence above to enable JScan3 instead of OScan1 */
1058  sequence[ESCAPE_SEQ_OAC_BIT2].tdi = '0';
1059  sequence[ESCAPE_SEQ_OAC_BIT2 + 1].tdi = '0';
1060  }
1061 
1062  /* if defined TMSC_EN, replace tms with it */
1063  if (tmsc_en)
1064  tms = tmsc_en;
1065 
1066  /* Send the sequence to the adapter */
1067  for (size_t i = 0; i < ARRAY_SIZE(sequence); i++)
1068  cjtag_set_tck_tms_tdi(tck, sequence[i].tck, tms, sequence[i].tms, tdi, sequence[i].tdi);
1069 
1070  /* If JScan3 mode, configure cJTAG adapter to 4-wire */
1071  if (jscan3_mode)
1072  ftdi_set_signal(find_signal_by_name("JTAG_SEL"), '1');
1073 
1074  ftdi_get_signal(tdo, &tdovalue); /* Just to force a flush */
1075 }
1076 #endif /* #if BUILD_FTDI_CJTAG == 1 */
1077 
1078 COMMAND_HANDLER(ftdi_handle_channel_command)
1079 {
1080  if (CMD_ARGC == 1)
1082  else
1084 
1085  return ERROR_OK;
1086 }
1087 
1088 COMMAND_HANDLER(ftdi_handle_layout_init_command)
1089 {
1090  if (CMD_ARGC != 2)
1092 
1095 
1096  return ERROR_OK;
1097 }
1098 
1099 COMMAND_HANDLER(ftdi_handle_layout_signal_command)
1100 {
1101  if (CMD_ARGC < 1)
1103 
1104  bool invert_data = false;
1105  uint16_t data_mask = 0;
1106  bool invert_input = false;
1107  uint16_t input_mask = 0;
1108  bool invert_oe = false;
1109  uint16_t oe_mask = 0;
1110  for (unsigned int i = 1; i < CMD_ARGC; i += 2) {
1111  if (strcmp("-data", CMD_ARGV[i]) == 0) {
1112  invert_data = false;
1113  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
1114  } else if (strcmp("-ndata", CMD_ARGV[i]) == 0) {
1115  invert_data = true;
1116  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
1117  } else if (strcmp("-input", CMD_ARGV[i]) == 0) {
1118  invert_input = false;
1119  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
1120  } else if (strcmp("-ninput", CMD_ARGV[i]) == 0) {
1121  invert_input = true;
1122  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
1123  } else if (strcmp("-oe", CMD_ARGV[i]) == 0) {
1124  invert_oe = false;
1125  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
1126  } else if (strcmp("-noe", CMD_ARGV[i]) == 0) {
1127  invert_oe = true;
1128  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
1129  } else if (!strcmp("-alias", CMD_ARGV[i]) ||
1130  !strcmp("-nalias", CMD_ARGV[i])) {
1131  if (!strcmp("-nalias", CMD_ARGV[i])) {
1132  invert_data = true;
1133  invert_input = true;
1134  }
1135  struct signal *sig = find_signal_by_name(CMD_ARGV[i + 1]);
1136  if (!sig) {
1137  LOG_ERROR("signal %s is not defined", CMD_ARGV[i + 1]);
1138  return ERROR_FAIL;
1139  }
1140  data_mask = sig->data_mask;
1141  input_mask = sig->input_mask;
1142  oe_mask = sig->oe_mask;
1143  invert_input ^= sig->invert_input;
1144  invert_oe = sig->invert_oe;
1145  invert_data ^= sig->invert_data;
1146  } else {
1147  LOG_ERROR("unknown option '%s'", CMD_ARGV[i]);
1149  }
1150  }
1151 
1152  struct signal *sig;
1153  sig = find_signal_by_name(CMD_ARGV[0]);
1154  if (!sig)
1155  sig = create_signal(CMD_ARGV[0]);
1156  if (!sig) {
1157  LOG_ERROR("failed to create signal %s", CMD_ARGV[0]);
1158  return ERROR_FAIL;
1159  }
1160 
1161  sig->invert_data = invert_data;
1162  sig->data_mask = data_mask;
1163  sig->invert_input = invert_input;
1164  sig->input_mask = input_mask;
1165  sig->invert_oe = invert_oe;
1166  sig->oe_mask = oe_mask;
1167 
1168  return ERROR_OK;
1169 }
1170 
1171 COMMAND_HANDLER(ftdi_handle_set_signal_command)
1172 {
1173  if (CMD_ARGC < 2)
1175 
1176  struct signal *sig;
1177  sig = find_signal_by_name(CMD_ARGV[0]);
1178  if (!sig) {
1179  LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
1180  return ERROR_FAIL;
1181  }
1182 
1183  switch (*CMD_ARGV[1]) {
1184  case '0':
1185  case '1':
1186  case 'z':
1187  case 'Z':
1188  /* single character level specifier only */
1189  if (CMD_ARGV[1][1] == '\0') {
1190  ftdi_set_signal(sig, *CMD_ARGV[1]);
1191  break;
1192  }
1193  /* fallthrough */
1194  default:
1195  LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV[1]);
1197  }
1198 
1199  return mpsse_flush(mpsse_ctx);
1200 }
1201 
1202 COMMAND_HANDLER(ftdi_handle_get_signal_command)
1203 {
1204  if (CMD_ARGC < 1)
1206 
1207  struct signal *sig;
1208  uint16_t sig_data = 0;
1209  sig = find_signal_by_name(CMD_ARGV[0]);
1210  if (!sig) {
1211  command_print(CMD, "interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
1212  return ERROR_FAIL;
1213  }
1214 
1215  int ret = ftdi_get_signal(sig, &sig_data);
1216  if (ret != ERROR_OK)
1217  return ret;
1218 
1219  command_print(CMD, "%#06x", sig_data);
1220 
1221  return ERROR_OK;
1222 }
1223 
1224 COMMAND_HANDLER(ftdi_handle_tdo_sample_edge_command)
1225 {
1226  const struct nvp *n;
1227  static const struct nvp nvp_ftdi_jtag_modes[] = {
1228  { .name = "rising", .value = JTAG_MODE },
1229  { .name = "falling", .value = JTAG_MODE_ALT },
1230  { .name = NULL, .value = -1 },
1231  };
1232 
1233  if (CMD_ARGC > 0) {
1234  n = nvp_name2value(nvp_ftdi_jtag_modes, CMD_ARGV[0]);
1235  if (!n->name)
1237  ftdi_jtag_mode = n->value;
1238 
1239  }
1240 
1241  n = nvp_value2name(nvp_ftdi_jtag_modes, ftdi_jtag_mode);
1242  command_print(CMD, "ftdi samples TDO on %s edge of TCK", n->name);
1243 
1244  return ERROR_OK;
1245 }
1246 
1247 #if BUILD_FTDI_CJTAG == 1
1248 COMMAND_HANDLER(ftdi_handle_oscan1_mode_command)
1249 {
1250  if (CMD_ARGC > 1)
1252 
1253  if (CMD_ARGC == 1)
1254  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], oscan1_mode);
1255 
1256  command_print(CMD, "oscan1 mode: %s.", oscan1_mode ? "on" : "off");
1257  return ERROR_OK;
1258 }
1259 
1260 COMMAND_HANDLER(ftdi_handle_jscan3_mode_command)
1261 {
1262  if (CMD_ARGC > 1)
1264 
1265  if (CMD_ARGC == 1)
1266  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], jscan3_mode);
1267 
1268  command_print(CMD, "jscan3 mode: %s.", jscan3_mode ? "on" : "off");
1269  return ERROR_OK;
1270 }
1271 #endif
1272 
1273 static const struct command_registration ftdi_subcommand_handlers[] = {
1274  {
1275  .name = "channel",
1276  .handler = &ftdi_handle_channel_command,
1277  .mode = COMMAND_CONFIG,
1278  .help = "set the channel of the FTDI device that is used as JTAG",
1279  .usage = "(0-3)",
1280  },
1281  {
1282  .name = "layout_init",
1283  .handler = &ftdi_handle_layout_init_command,
1284  .mode = COMMAND_CONFIG,
1285  .help = "initialize the FTDI GPIO signals used "
1286  "to control output-enables and reset signals",
1287  .usage = "data direction",
1288  },
1289  {
1290  .name = "layout_signal",
1291  .handler = &ftdi_handle_layout_signal_command,
1292  .mode = COMMAND_ANY,
1293  .help = "define a signal controlled by one or more FTDI GPIO as data "
1294  "and/or output enable",
1295  .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]",
1296  },
1297  {
1298  .name = "set_signal",
1299  .handler = &ftdi_handle_set_signal_command,
1300  .mode = COMMAND_EXEC,
1301  .help = "control a layout-specific signal",
1302  .usage = "name (1|0|z)",
1303  },
1304  {
1305  .name = "get_signal",
1306  .handler = &ftdi_handle_get_signal_command,
1307  .mode = COMMAND_EXEC,
1308  .help = "read the value of a layout-specific signal",
1309  .usage = "name",
1310  },
1311  {
1312  .name = "tdo_sample_edge",
1313  .handler = &ftdi_handle_tdo_sample_edge_command,
1314  .mode = COMMAND_ANY,
1315  .help = "set which TCK clock edge is used for sampling TDO "
1316  "- default is rising-edge (Setting to falling-edge may "
1317  "allow signalling speed increase)",
1318  .usage = "(rising|falling)",
1319  },
1320 #if BUILD_FTDI_CJTAG == 1
1321  {
1322  .name = "oscan1_mode",
1323  .handler = &ftdi_handle_oscan1_mode_command,
1324  .mode = COMMAND_ANY,
1325  .help = "set to 'on' to use OScan1 mode for signaling, otherwise 'off' (default is 'off')",
1326  .usage = "(on|off)",
1327  },
1328  {
1329  .name = "jscan3_mode",
1330  .handler = &ftdi_handle_jscan3_mode_command,
1331  .mode = COMMAND_ANY,
1332  .help = "set to 'on' to use JScan3 mode for signaling, otherwise 'off' (default is 'off')",
1333  .usage = "(on|off)",
1334  },
1335 #endif
1337 };
1338 
1339 static const struct command_registration ftdi_command_handlers[] = {
1340  {
1341  .name = "ftdi",
1342  .mode = COMMAND_ANY,
1343  .help = "perform ftdi management",
1344  .chain = ftdi_subcommand_handlers,
1345  .usage = "",
1346  },
1348 };
1349 
1350 static int create_default_signal(const char *name, uint16_t data_mask)
1351 {
1352  struct signal *sig = create_signal(name);
1353  if (!sig) {
1354  LOG_ERROR("failed to create signal %s", name);
1355  return ERROR_FAIL;
1356  }
1357  sig->invert_data = false;
1358  sig->data_mask = data_mask;
1359  sig->invert_oe = false;
1360  sig->oe_mask = 0;
1361 
1362  return ERROR_OK;
1363 }
1364 
1365 static int create_signals(void)
1366 {
1367  if (create_default_signal("TCK", 0x01) != ERROR_OK)
1368  return ERROR_FAIL;
1369  if (create_default_signal("TDI", 0x02) != ERROR_OK)
1370  return ERROR_FAIL;
1371  if (create_default_signal("TDO", 0x04) != ERROR_OK)
1372  return ERROR_FAIL;
1373  if (create_default_signal("TMS", 0x08) != ERROR_OK)
1374  return ERROR_FAIL;
1375  return ERROR_OK;
1376 }
1377 
1378 static int ftdi_swd_init(void)
1379 {
1380  LOG_INFO("FTDI SWD mode enabled");
1381  swd_mode = true;
1382 
1383  if (create_signals() != ERROR_OK)
1384  return ERROR_FAIL;
1385 
1386  swd_cmd_queue_alloced = 10;
1387  swd_cmd_queue = malloc(swd_cmd_queue_alloced * sizeof(*swd_cmd_queue));
1388 
1389  return swd_cmd_queue ? ERROR_OK : ERROR_FAIL;
1390 }
1391 
1392 static void ftdi_swd_swdio_en(bool enable)
1393 {
1394  struct signal *oe = find_signal_by_name("SWDIO_OE");
1395  if (oe) {
1396  if (oe->data_mask)
1397  ftdi_set_signal(oe, enable ? '1' : '0');
1398  else {
1399  /* Sets TDI/DO pin to input during rx when both pins are connected
1400  to SWDIO */
1401  if (enable)
1402  direction |= jtag_direction_init & 0x0002U;
1403  else
1404  direction &= ~0x0002U;
1406  }
1407  }
1408 }
1409 
1414 static int ftdi_swd_run_queue(void)
1415 {
1416  LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length);
1417  int retval;
1418  struct signal *led = find_signal_by_name("LED");
1419 
1420  if (queued_retval != ERROR_OK) {
1421  LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval);
1422  goto skip;
1423  }
1424 
1425  /* A transaction must be followed by another transaction or at least 8 idle cycles to
1426  * ensure that data is clocked through the AP. */
1428 
1429  /* Terminate the "blink", if the current layout has that feature */
1430  if (led)
1431  ftdi_set_signal(led, '0');
1432 
1434  if (queued_retval != ERROR_OK) {
1435  LOG_ERROR("MPSSE failed");
1436  goto skip;
1437  }
1438 
1439  for (size_t i = 0; i < swd_cmd_queue_length; i++) {
1440  int ack = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3);
1441 
1442  /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
1443  bool check_ack = swd_cmd_returns_ack(swd_cmd_queue[i].cmd);
1444 
1445  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK) ? LOG_LVL_DEBUG : LOG_LVL_DEBUG_IO,
1446  "%s%s %s %s reg %X = %08" PRIx32,
1447  check_ack ? "" : "ack ignored ",
1448  ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
1449  swd_cmd_queue[i].cmd & SWD_CMD_APNDP ? "AP" : "DP",
1450  swd_cmd_queue[i].cmd & SWD_CMD_RNW ? "read" : "write",
1451  (swd_cmd_queue[i].cmd & SWD_CMD_A32) >> 1,
1452  buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn,
1453  1 + 3 + (swd_cmd_queue[i].cmd & SWD_CMD_RNW ? 0 : 1), 32));
1454 
1455  if (ack != SWD_ACK_OK && check_ack) {
1457  goto skip;
1458 
1459  } else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
1460  uint32_t data = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3, 32);
1461  int parity = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 32, 1);
1462 
1463  if (parity != parity_u32(data)) {
1464  LOG_ERROR("SWD Read data parity mismatch");
1466  goto skip;
1467  }
1468 
1469  if (swd_cmd_queue[i].dst)
1470  *swd_cmd_queue[i].dst = data;
1471  }
1472  }
1473 
1474 skip:
1476  retval = queued_retval;
1478 
1479  /* Queue a new "blink" */
1480  if (led && retval == ERROR_OK)
1481  ftdi_set_signal(led, '1');
1482 
1483  return retval;
1484 }
1485 
1486 static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
1487 {
1489  /* Not enough room in the queue. Run the queue and increase its size for next time.
1490  * Note that it's not possible to avoid running the queue here, because mpsse contains
1491  * pointers into the queue which may be invalid after the realloc. */
1493  struct swd_cmd_queue_entry *q = realloc(swd_cmd_queue, swd_cmd_queue_alloced * 2 * sizeof(*swd_cmd_queue));
1494  if (q) {
1495  swd_cmd_queue = q;
1496  swd_cmd_queue_alloced *= 2;
1497  LOG_DEBUG("Increased SWD command queue to %zu elements", swd_cmd_queue_alloced);
1498  }
1499  }
1500 
1501  if (queued_retval != ERROR_OK)
1502  return;
1503 
1504  size_t i = swd_cmd_queue_length++;
1506 
1508 
1509  if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
1510  /* Queue a read transaction */
1511  swd_cmd_queue[i].dst = dst;
1512 
1513  ftdi_swd_swdio_en(false);
1515  0, 1 + 3 + 32 + 1 + 1, SWD_MODE);
1516  ftdi_swd_swdio_en(true);
1517  } else {
1518  /* Queue a write transaction */
1519  ftdi_swd_swdio_en(false);
1520 
1522  0, 1 + 3 + 1, SWD_MODE);
1523 
1524  ftdi_swd_swdio_en(true);
1525 
1526  buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32, data);
1527  buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(data));
1528 
1530  1 + 3 + 1, 32 + 1, SWD_MODE);
1531  }
1532 
1533  /* Insert idle cycles after AP accesses to avoid WAIT */
1534  if (cmd & SWD_CMD_APNDP)
1535  mpsse_clock_data_out(mpsse_ctx, NULL, 0, ap_delay_clk, SWD_MODE);
1536 
1537 }
1538 
1539 static void ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1540 {
1541  assert(cmd & SWD_CMD_RNW);
1542  ftdi_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1543 }
1544 
1545 static void ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1546 {
1547  assert(!(cmd & SWD_CMD_RNW));
1548  ftdi_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1549 }
1550 
1552 {
1553  switch (seq) {
1554  case LINE_RESET:
1555  LOG_DEBUG("SWD line reset");
1556  ftdi_swd_swdio_en(true);
1558  break;
1559  case JTAG_TO_SWD:
1560  LOG_DEBUG("JTAG-to-SWD");
1561  ftdi_swd_swdio_en(true);
1563  break;
1564  case JTAG_TO_DORMANT:
1565  LOG_DEBUG("JTAG-to-DORMANT");
1566  ftdi_swd_swdio_en(true);
1568  break;
1569  case SWD_TO_JTAG:
1570  LOG_DEBUG("SWD-to-JTAG");
1571  ftdi_swd_swdio_en(true);
1573  break;
1574  case SWD_TO_DORMANT:
1575  LOG_DEBUG("SWD-to-DORMANT");
1576  ftdi_swd_swdio_en(true);
1578  break;
1579  case DORMANT_TO_SWD:
1580  LOG_DEBUG("DORMANT-to-SWD");
1581  ftdi_swd_swdio_en(true);
1583  break;
1584  case DORMANT_TO_JTAG:
1585  LOG_DEBUG("DORMANT-to-JTAG");
1586  ftdi_swd_swdio_en(true);
1588  break;
1589  default:
1590  LOG_ERROR("Sequence %d not supported", seq);
1591  return ERROR_FAIL;
1592  }
1593 
1594  return ERROR_OK;
1595 }
1596 
1597 static const struct swd_driver ftdi_swd = {
1598  .init = ftdi_swd_init,
1599  .switch_seq = ftdi_swd_switch_seq,
1600  .read_reg = ftdi_swd_read_reg,
1601  .write_reg = ftdi_swd_write_reg,
1602  .run = ftdi_swd_run_queue,
1603 };
1604 
1605 static struct jtag_interface ftdi_interface = {
1607  .execute_queue = ftdi_execute_queue,
1608 };
1609 
1611  .name = "ftdi",
1612  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
1613  .transport_preferred_id = TRANSPORT_JTAG,
1614  .commands = ftdi_command_handlers,
1615 
1616  .init = ftdi_initialize,
1617  .quit = ftdi_quit,
1618  .reset = ftdi_reset,
1619  .speed = ftdi_speed,
1620  .khz = ftdi_khz,
1621  .speed_div = ftdi_speed_div,
1622 
1623  .jtag_ops = &ftdi_interface,
1624  .swd_ops = &ftdi_swd,
1625 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:309
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:340
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
Definition: adapter.c:217
const char * adapter_usb_get_product_name(void)
Definition: adapter.c:350
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:335
const char * adapter_usb_get_location(void)
Definition: adapter.c:345
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
swd_special_seq
Definition: arm_adi_v5.h:236
@ DORMANT_TO_JTAG
Definition: arm_adi_v5.h:243
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ DORMANT_TO_SWD
Definition: arm_adi_v5.h:242
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ JTAG_TO_DORMANT
Definition: arm_adi_v5.h:239
@ SWD_TO_DORMANT
Definition: arm_adi_v5.h:241
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define SWD_ACK_WAIT
Definition: arm_adi_v5.h:32
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
enum arm_mode mode
Definition: armv4_5.c:281
const char * name
Definition: armv4_5.c:76
static void bit_copy(uint8_t *dst, unsigned int dst_offset, const uint8_t *src, unsigned int src_offset, unsigned int bit_count)
Definition: binarybuffer.h:218
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:533
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_STABLECLOCKS
Definition: commands.h:142
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_RESET
Definition: commands.h:139
@ JTAG_TMS
Definition: commands.h:143
uint8_t length
Definition: esp_usb_jtag.c:1
static uint16_t output
Definition: ftdi.c:156
static void move_to_state(enum tap_state goal_state)
Function move_to_state moves the TAP controller from the current state to a goal_state through a path...
Definition: ftdi.c:307
static uint8_t ftdi_channel
Definition: ftdi.c:92
static const struct command_registration ftdi_command_handlers[]
Definition: ftdi.c:1339
static void ftdi_swd_swdio_en(bool enable)
Definition: ftdi.c:1392
#define DO_CLOCK_TMS_CS_OUT
Definition: ftdi.c:85
static int queued_retval
Definition: ftdi.c:153
static void ftdi_execute_sleep(struct jtag_command *cmd)
Definition: ftdi.c:628
static struct signal * create_signal(const char *name)
Definition: ftdi.c:172
static void ftdi_execute_stableclocks(struct jtag_command *cmd)
Definition: ftdi.c:639
static void ftdi_execute_pathmove(struct jtag_command *cmd)
Definition: ftdi.c:437
static int ftdi_swd_init(void)
Definition: ftdi.c:1378
static void ftdi_end_state(enum tap_state state)
Definition: ftdi.c:368
static uint16_t direction
Definition: ftdi.c:157
static void ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: ftdi.c:1539
static struct jtag_interface ftdi_interface
Definition: ftdi.c:1605
#define JTAG_MODE
Definition: ftdi.c:88
static uint16_t jtag_output_init
Definition: ftdi.c:158
static int ftdi_speed(int speed)
Definition: ftdi.c:335
static int ftdi_get_signal(const struct signal *s, uint16_t *value_out)
Definition: ftdi.c:243
static bool swd_mode
Definition: ftdi.c:95
static size_t swd_cmd_queue_length
Definition: ftdi.c:151
struct adapter_driver ftdi_adapter_driver
Definition: ftdi.c:1610
static const struct swd_driver ftdi_swd
Definition: ftdi.c:1597
static struct mpsse_ctx * mpsse_ctx
Definition: ftdi.c:130
static void ftdi_execute_tms(struct jtag_command *cmd)
Clock a bunch of TMS (or SWDIO) transitions, to change the JTAG (or SWD) state machine.
Definition: ftdi.c:424
static size_t swd_cmd_queue_alloced
Definition: ftdi.c:152
static int ftdi_reset(int trst, int srst)
Definition: ftdi.c:590
static int ftdi_khz(int khz, int *jtag_speed)
Definition: ftdi.c:357
static int ftdi_speed_div(int speed, int *khz)
Definition: ftdi.c:351
static int ftdi_execute_queue(struct jtag_command *cmd_queue)
Definition: ftdi.c:702
static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
Definition: ftdi.c:1486
static int ftdi_swd_run_queue(void)
Flush the MPSSE queue and process the SWD transaction queue.
Definition: ftdi.c:1414
#define JTAG_MODE_ALT
Definition: ftdi.c:89
static uint16_t jtag_direction_init
Definition: ftdi.c:159
static struct signal * find_signal_by_name(const char *name)
Definition: ftdi.c:163
static int ftdi_initialize(void)
Definition: ftdi.c:724
static int ftdi_quit(void)
Definition: ftdi.c:781
#define SWD_MODE
Definition: ftdi.c:90
static void ftdi_execute_statemove(struct jtag_command *cmd)
Definition: ftdi.c:408
COMMAND_HANDLER(ftdi_handle_channel_command)
Definition: ftdi.c:1078
static int create_default_signal(const char *name, uint16_t data_mask)
Definition: ftdi.c:1350
static void ftdi_execute_scan(struct jtag_command *cmd)
Definition: ftdi.c:488
#define DO_CLOCK_DATA
Definition: ftdi.c:83
static uint8_t ftdi_jtag_mode
Definition: ftdi.c:93
static void ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: ftdi.c:1545
static int ftdi_swd_switch_seq(enum swd_special_seq seq)
Definition: ftdi.c:1551
static void ftdi_execute_runtest(struct jtag_command *cmd)
Definition: ftdi.c:378
#define DO_CLOCK_TMS_CS
Definition: ftdi.c:84
static int freq
Definition: ftdi.c:154
static int create_signals(void)
Definition: ftdi.c:1365
static const struct command_registration ftdi_subcommand_handlers[]
Definition: ftdi.c:1273
static int ftdi_set_signal(const struct signal *s, char value)
Definition: ftdi.c:190
static struct signal * signals
Definition: ftdi.c:143
static struct swd_cmd_queue_entry * swd_cmd_queue
static void ftdi_execute_command(struct jtag_command *cmd)
Definition: ftdi.c:663
enum tap_state tap_get_end_state(void)
For more information,.
Definition: interface.c:56
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
Definition: interface.c:223
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
Definition: interface.c:48
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
Definition: interface.c:37
bool tap_is_state_stable(enum tap_state astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:188
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
Definition: interface.h:50
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1070
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1742
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
@ TAP_IRPAUSE
Definition: jtag.h:52
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
@ RESET_HAS_SRST
Definition: jtag.h:218
@ RESET_HAS_TRST
Definition: jtag.h:217
@ RESET_TRST_OPEN_DRAIN
Definition: jtag.h:222
@ RESET_SRST_PUSH_PULL
Definition: jtag.h:223
#define LOG_CUSTOM_LEVEL(level, expr ...)
Definition: log.h:132
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
@ LOG_LVL_DEBUG
Definition: log.h:55
@ LOG_LVL_DEBUG_IO
Definition: log.h:56
#define zero
Definition: mips32.c:181
int mpsse_flush(struct mpsse_ctx *ctx)
Definition: mpsse.c:850
void mpsse_read_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t *data)
Definition: mpsse.c:678
void mpsse_set_data_bits_high_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
Definition: mpsse.c:645
int mpsse_set_frequency(struct mpsse_ctx *ctx, int frequency)
Definition: mpsse.c:753
void mpsse_read_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t *data)
Definition: mpsse.c:662
void mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, bool tdi, uint8_t mode)
Definition: mpsse.c:571
void mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
Definition: mpsse.c:500
void mpsse_clock_data_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, unsigned int length, uint8_t mode)
Definition: mpsse.c:488
void mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in, unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
Definition: mpsse.c:577
struct mpsse_ctx * mpsse_open(const uint16_t vids[], const uint16_t pids[], const char *description, const char *serial, const char *location, int channel)
Definition: mpsse.c:332
bool mpsse_is_high_speed(struct mpsse_ctx *ctx)
Definition: mpsse.c:421
void mpsse_close(struct mpsse_ctx *ctx)
Definition: mpsse.c:407
void mpsse_clock_data_in(struct mpsse_ctx *ctx, uint8_t *in, unsigned int in_offset, unsigned int length, uint8_t mode)
Definition: mpsse.c:494
void mpsse_loopback_config(struct mpsse_ctx *ctx, bool enable)
Definition: mpsse.c:708
void mpsse_set_data_bits_low_byte(struct mpsse_ctx *ctx, uint8_t data, uint8_t dir)
Definition: mpsse.c:628
const struct nvp * nvp_name2value(const struct nvp *p, const char *name)
Definition: nvp.c:29
const struct nvp * nvp_value2name(const struct nvp *p, int value)
Definition: nvp.c:39
static uint32_t bit(uint32_t value, unsigned int b)
Definition: opcodes.h:39
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Represents a driver for a debugging interface.
Definition: interface.h:183
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:187
Name Value Pairs, aka: NVP.
Definition: nvp.h:61
int value
Definition: nvp.h:63
const char * name
Definition: nvp.h:62
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
Definition: osbdm.c:17
const void * tdi
Definition: osbdm.c:21
Definition: ftdi.c:132
struct signal * next
Definition: ftdi.c:140
bool invert_oe
Definition: ftdi.c:139
const char * name
Definition: ftdi.c:133
bool invert_data
Definition: ftdi.c:137
uint16_t input_mask
Definition: ftdi.c:135
uint16_t data_mask
Definition: ftdi.c:134
bool invert_input
Definition: ftdi.c:138
uint16_t oe_mask
Definition: ftdi.c:136
Definition: ftdi.c:146
uint8_t trn_ack_data_parity_trn[DIV_ROUND_UP(4+3+32+1+4, 8)]
Definition: ftdi.c:149
uint8_t cmd
Definition: ftdi.c:147
uint32_t * dst
Definition: ftdi.c:148
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
static const unsigned int swd_seq_dormant_to_swd_len
Definition: swd.h:190
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
Definition: swd.h:230
#define SWD_CMD_A32
Definition: swd.h:19
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
Definition: swd.h:171
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
Definition: swd.h:199
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
Definition: swd.h:58
#define SWD_CMD_PARK
Definition: swd.h:22
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
Definition: swd.h:72
static const unsigned int swd_seq_jtag_to_swd_len
Definition: swd.h:125
static const unsigned int swd_seq_line_reset_len
Definition: swd.h:104
static const unsigned int swd_seq_dormant_to_jtag_len
Definition: swd.h:244
#define SWD_CMD_APNDP
Definition: swd.h:17
static const unsigned int swd_seq_swd_to_dormant_len
Definition: swd.h:159
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
Definition: swd.h:98
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
Definition: swd.h:115
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
Definition: swd.h:136
static const unsigned int swd_seq_swd_to_jtag_len
Definition: swd.h:144
static const unsigned int swd_seq_jtag_to_dormant_len
Definition: swd.h:211
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
Definition: swd.h:153
#define TRANSPORT_SWD
Definition: transport.h:20
#define TRANSPORT_JTAG
Definition: transport.h:19
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t state[4]
Definition: vdebug.c:21
static unsigned int parity(unsigned int v)
Definition: xscale.c:624