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 
369 {
370  uint8_t zero = 0;
371 
372  LOG_DEBUG_IO("runtest %u cycles, end in %s",
373  cmd->cmd.runtest->num_cycles,
374  tap_state_name(cmd->cmd.runtest->end_state));
375 
376  if (tap_get_state() != TAP_IDLE)
378 
379  /* TODO: Reuse ftdi_execute_stableclocks */
380  unsigned int i = cmd->cmd.runtest->num_cycles;
381  while (i > 0) {
382  /* there are no state transitions in this code, so omit state tracking */
383  unsigned int this_len = i > 7 ? 7 : i;
384  DO_CLOCK_TMS_CS_OUT(mpsse_ctx, &zero, 0, this_len, false, ftdi_jtag_mode);
385  i -= this_len;
386  }
387 
388  int retval = tap_set_end_state_stable(cmd->cmd.runtest->end_state);
389  if (retval != ERROR_OK)
390  return retval;
391 
392  if (tap_get_state() != tap_get_end_state())
394 
395  LOG_DEBUG_IO("runtest: %u, end in %s",
396  cmd->cmd.runtest->num_cycles,
398  return ERROR_OK;
399 }
400 
402 {
403  LOG_DEBUG_IO("statemove end in %s",
404  tap_state_name(cmd->cmd.statemove->end_state));
405 
406  int retval = tap_set_end_state_stable(cmd->cmd.statemove->end_state);
407  if (retval != ERROR_OK)
408  return retval;
409 
410  /* shortest-path move to desired end state */
413 
414  return ERROR_OK;
415 }
416 
421 static int ftdi_execute_tms(struct jtag_command *cmd)
422 {
423  LOG_DEBUG_IO("TMS: %u bits", cmd->cmd.tms->num_bits);
424 
425  /* TODO: Missing tap state tracking, also missing from ft2232.c! */
427  cmd->cmd.tms->bits,
428  0,
429  cmd->cmd.tms->num_bits,
430  false,
432  return ERROR_OK;
433 }
434 
436 {
437  enum tap_state *path = cmd->cmd.pathmove->path;
438  unsigned int num_states = cmd->cmd.pathmove->num_states;
439 
440  LOG_DEBUG_IO("pathmove: %u states, current: %s end: %s", num_states,
442  tap_state_name(path[num_states-1]));
443 
444  int state_count = 0;
445  unsigned int bit_count = 0;
446  uint8_t tms_byte = 0;
447 
448  LOG_DEBUG_IO("-");
449 
450  /* this loop verifies that the path is legal and logs each state in the path */
451  while (num_states--) {
452 
453  /* either TMS=0 or TMS=1 must work ... */
454  if (tap_state_transition(tap_get_state(), false)
455  == path[state_count])
456  buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
457  else if (tap_state_transition(tap_get_state(), true)
458  == path[state_count]) {
459  buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
460 
461  /* ... or else the caller goofed BADLY */
462  } else {
463  LOG_ERROR("BUG: %s -> %s isn't a valid "
464  "TAP state transition",
466  tap_state_name(path[state_count]));
468  }
469 
470  tap_set_state(path[state_count]);
471  state_count++;
472 
473  if (bit_count == 7 || num_states == 0) {
475  &tms_byte,
476  0,
477  bit_count,
478  false,
480  bit_count = 0;
481  }
482  }
484  return ERROR_OK;
485 }
486 
487 static int ftdi_execute_scan(struct jtag_command *cmd)
488 {
489  LOG_DEBUG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
490  jtag_scan_type(cmd->cmd.scan));
491 
492  /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
493  while (cmd->cmd.scan->num_fields > 0
494  && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
495  cmd->cmd.scan->num_fields--;
496  LOG_DEBUG_IO("discarding trailing empty field");
497  }
498 
499  if (!cmd->cmd.scan->num_fields) {
500  LOG_DEBUG_IO("empty scan, doing nothing");
501  return ERROR_OK;
502  }
503 
504  if (cmd->cmd.scan->ir_scan) {
505  if (tap_get_state() != TAP_IRSHIFT)
507  } else {
508  if (tap_get_state() != TAP_DRSHIFT)
510  }
511 
512  int retval = tap_set_end_state_stable(cmd->cmd.scan->end_state);
513  if (retval != ERROR_OK)
514  return retval;
515 
516  struct scan_field *field = cmd->cmd.scan->fields;
517  unsigned int scan_size = 0;
518 
519  for (unsigned int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
520  scan_size += field->num_bits;
521  LOG_DEBUG_IO("%s%s field %u/%u %u bits",
522  field->in_value ? "in" : "",
523  field->out_value ? "out" : "",
524  i,
525  cmd->cmd.scan->num_fields,
526  field->num_bits);
527 
528  if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
529  /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
530  * movement. This last field can't have length zero, it was checked above. */
532  field->out_value,
533  0,
534  field->in_value,
535  0,
536  field->num_bits - 1,
538  uint8_t last_bit = 0;
539  if (field->out_value)
540  bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
541 
542  /* If endstate is TAP_IDLE, clock out 1-1-0 (->EXIT1 ->UPDATE ->IDLE)
543  * Otherwise, clock out 1-0 (->EXIT1 ->PAUSE)
544  */
545  uint8_t tms_bits = 0x03;
547  &tms_bits,
548  0,
549  field->in_value,
550  field->num_bits - 1,
551  1,
552  last_bit,
555  if (tap_get_end_state() == TAP_IDLE) {
557  &tms_bits,
558  1,
559  2,
560  last_bit,
564  } else {
566  &tms_bits,
567  2,
568  1,
569  last_bit,
572  }
573  } else
575  field->out_value,
576  0,
577  field->in_value,
578  0,
579  field->num_bits,
581  }
582 
583  if (tap_get_state() != tap_get_end_state())
585 
586  LOG_DEBUG_IO("%s scan, %i bits, end in %s",
587  (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
589  return ERROR_OK;
590 }
591 
592 static int ftdi_reset(int trst, int srst)
593 {
594  struct signal *sig_ntrst = find_signal_by_name("nTRST");
595  struct signal *sig_nsrst = find_signal_by_name("nSRST");
596 
597  LOG_DEBUG_IO("reset trst: %i srst %i", trst, srst);
598 
599  if (!swd_mode) {
600  if (trst == 1) {
601  if (sig_ntrst)
602  ftdi_set_signal(sig_ntrst, '0');
603  else
604  LOG_ERROR("Can't assert TRST: nTRST signal is not defined");
605  } else if (sig_ntrst && jtag_get_reset_config() & RESET_HAS_TRST &&
606  trst == 0) {
608  ftdi_set_signal(sig_ntrst, 'z');
609  else
610  ftdi_set_signal(sig_ntrst, '1');
611  }
612  }
613 
614  if (srst == 1) {
615  if (sig_nsrst)
616  ftdi_set_signal(sig_nsrst, '0');
617  else
618  LOG_ERROR("Can't assert SRST: nSRST signal is not defined");
619  } else if (sig_nsrst && jtag_get_reset_config() & RESET_HAS_SRST &&
620  srst == 0) {
622  ftdi_set_signal(sig_nsrst, '1');
623  else
624  ftdi_set_signal(sig_nsrst, 'z');
625  }
626 
627  return mpsse_flush(mpsse_ctx);
628 }
629 
631 {
632  LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
633 
634  int retval = mpsse_flush(mpsse_ctx);
635  if (retval != ERROR_OK)
636  return retval;
637  jtag_sleep(cmd->cmd.sleep->us);
638  LOG_DEBUG_IO("sleep %" PRIu32 " usec while in %s",
639  cmd->cmd.sleep->us,
641  return ERROR_OK;
642 }
643 
645 {
646  /* this is only allowed while in a stable state. A check for a stable
647  * state was done in jtag_add_clocks()
648  */
649  unsigned int num_cycles = cmd->cmd.stableclocks->num_cycles;
650 
651  /* 7 bits of either ones or zeros. */
652  uint8_t tms = tap_get_state() == TAP_RESET ? 0x7f : 0x00;
653 
654  /* TODO: Use mpsse_clock_data with in=out=0 for this, if TMS can be set to
655  * the correct level and remain there during the scan */
656  while (num_cycles > 0) {
657  /* there are no state transitions in this code, so omit state tracking */
658  unsigned int this_len = num_cycles > 7 ? 7 : num_cycles;
659  DO_CLOCK_TMS_CS_OUT(mpsse_ctx, &tms, 0, this_len, false, ftdi_jtag_mode);
660  num_cycles -= this_len;
661  }
662 
663  LOG_DEBUG_IO("clocks %u while in %s",
664  cmd->cmd.stableclocks->num_cycles,
666  return ERROR_OK;
667 }
668 
670 {
671  switch (cmd->type) {
672 #if BUILD_FTDI_CJTAG == 1
673  case JTAG_RESET:
674  if (cmd->cmd.reset->trst)
675  cjtag_reset_online_activate(); /* put the target (back) into selected cJTAG mode */
676  return ERROR_OK;
677 #endif
678  case JTAG_RUNTEST:
679  return ftdi_execute_runtest(cmd);
680  case JTAG_TLR_RESET:
681 #if BUILD_FTDI_CJTAG == 1
682  cjtag_reset_online_activate(); /* put the target (back) into selected cJTAG mode */
683 #endif
684  return ftdi_execute_statemove(cmd);
685  case JTAG_PATHMOVE:
686  return ftdi_execute_pathmove(cmd);
687  case JTAG_SCAN:
688  return ftdi_execute_scan(cmd);
689  case JTAG_SLEEP:
690  return ftdi_execute_sleep(cmd);
691  case JTAG_STABLECLOCKS:
693  case JTAG_TMS:
694  return ftdi_execute_tms(cmd);
695  default:
696  LOG_ERROR("BUG: unknown JTAG command type encountered: %d", cmd->type);
698  }
699 }
700 
701 static int ftdi_execute_queue(struct jtag_command *cmd_queue)
702 {
703  /* blink, if the current layout has that feature */
704  struct signal *led = find_signal_by_name("LED");
705  if (led)
706  ftdi_set_signal(led, '1');
707 
708  int retval = ERROR_OK;
709  for (struct jtag_command *cmd = cmd_queue; cmd; cmd = cmd->next) {
710  /* fill the write buffer with the desired command */
711  retval = ftdi_execute_command(cmd);
712  if (retval != ERROR_OK)
713  break;
714  }
715 
716  if (led)
717  ftdi_set_signal(led, '0');
718 
719  int retval2 = mpsse_flush(mpsse_ctx);
720  if (retval2 != ERROR_OK)
721  LOG_ERROR("error while flushing MPSSE queue: %d", retval2);
722 
723  if (retval != ERROR_OK)
724  return retval;
725  return retval2;
726 }
727 
728 static int ftdi_initialize(void)
729 {
731  LOG_DEBUG("ftdi interface using 7 step jtag state transitions");
732  else
733  LOG_DEBUG("ftdi interface using shortest path jtag state transitions");
734 
735  if (!adapter_usb_get_vids()[0] && !adapter_usb_get_pids()[0]) {
736  LOG_ERROR("Please specify 'adapter usb vid_pid'");
737  return ERROR_JTAG_INIT_FAILED;
738  }
739 
743  if (!mpsse_ctx)
744  return ERROR_JTAG_INIT_FAILED;
745 
748 
749  if (swd_mode) {
750  struct signal *sig = find_signal_by_name("SWD_EN");
751  if (!sig) {
752  LOG_ERROR("SWD mode is active but SWD_EN signal is not defined");
753  return ERROR_JTAG_INIT_FAILED;
754  }
755  /* A dummy SWD_EN would have zero mask */
756  if (sig->data_mask)
757  ftdi_set_signal(sig, '1');
758 #if BUILD_FTDI_CJTAG == 1
759  } else if (oscan1_mode || jscan3_mode) {
760  struct signal *sig = find_signal_by_name("JTAG_SEL");
761  if (!sig) {
762  LOG_ERROR("A cJTAG mode is active but JTAG_SEL signal is not defined");
763  return ERROR_JTAG_INIT_FAILED;
764  }
765  /* A dummy JTAG_SEL would have zero mask */
766  if (sig->data_mask) {
767  ftdi_set_signal(sig, '0');
768  } else if (jscan3_mode) {
769  LOG_ERROR("In JScan3 mode JTAG_SEL signal cannot be dummy, data mask needed");
770  return ERROR_JTAG_INIT_FAILED;
771  }
772 #endif
773  }
774 
777 
779 
781 
782  return mpsse_flush(mpsse_ctx);
783 }
784 
785 static int ftdi_quit(void)
786 {
788 
789  struct signal *sig = signals;
790  while (sig) {
791  struct signal *next = sig->next;
792  free((void *)sig->name);
793  free(sig);
794  sig = next;
795  }
796 
797  free(swd_cmd_queue);
798 
799  return ERROR_OK;
800 }
801 
802 #if BUILD_FTDI_CJTAG == 1
803 static void oscan1_mpsse_clock_data(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
804  unsigned int in_offset, unsigned int length, uint8_t mode)
805 {
806  static const uint8_t zero;
807  static const uint8_t one = 1;
808 
809  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
810 
811  LOG_DEBUG_IO("%sout %d bits", in ? "in" : "", length);
812 
813  for (unsigned int i = 0; i < length; i++) {
814  int bitnum;
815  uint8_t bit;
816 
817  /* OScan1 uses 3 separate clocks */
818 
819  /* drive TMSC to the *negation* of the desired TDI value */
820  bitnum = out_offset + i;
821  bit = out ? ((out[bitnum / 8] >> (bitnum % 8)) & 0x1) : 0;
822 
823  /* Try optimized case first: if desired TDI bit is 1, then we
824  can fuse what would otherwise be the first two MPSSE commands */
825  if (bit) {
826  const uint8_t tmsbits = 0x3; /* 1, 1 */
827  mpsse_clock_tms_cs_out(mpsse_ctx, &tmsbits, 0, 2, false, mode);
828  } else {
829  /* Can't fuse because TDI varies; less efficient */
830  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, bit ? 0 : 1, mode);
831 
832  /* drive TMSC to desired TMS value (always zero in this context) */
833  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, false, mode);
834  }
835 
836  if (tmsc_en)
837  ftdi_set_signal(tmsc_en, '0'); /* put TMSC in high impedance */
838 
839  /* drive another TCK without driving TMSC (TDO cycle) */
840  mpsse_clock_tms_cs(mpsse_ctx, &zero, 0, in, in_offset + i, 1, false, mode);
841 
842  if (tmsc_en)
843  ftdi_set_signal(tmsc_en, '1'); /* drive again TMSC */
844  }
845 }
846 
847 static void oscan1_mpsse_clock_tms_cs(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset, uint8_t *in,
848  unsigned int in_offset, unsigned int length, bool tdi, uint8_t mode)
849 {
850  static const uint8_t zero;
851  static const uint8_t one = 1;
852 
853  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
854 
855  LOG_DEBUG_IO("%sout %d bits, tdi=%d", in ? "in" : "", length, tdi);
856 
857  for (unsigned int i = 0; i < length; i++) {
858  int bitnum;
859  uint8_t tmsbit;
860  uint8_t tdibit;
861 
862  /* OScan1 uses 3 separate clocks */
863 
864  /* drive TMSC to the *negation* of the desired TDI value */
865  tdibit = tdi ? 0 : 1;
866 
867  /* drive TMSC to desired TMS value */
868  bitnum = out_offset + i;
869  tmsbit = ((out[bitnum / 8] >> (bitnum % 8)) & 0x1);
870 
871  if (tdibit == tmsbit) {
872  /* Can squash into a single MPSSE command */
873  const uint8_t tmsbits = 0x3;
874  mpsse_clock_tms_cs_out(mpsse_ctx, &tmsbits, 0, 2, tdibit, mode);
875  } else {
876  /* Unoptimized case, can't formulate with a single command */
877  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, tdibit, mode);
878  mpsse_clock_tms_cs_out(mpsse_ctx, &one, 0, 1, (tmsbit != 0), mode);
879  }
880 
881  if (tmsc_en)
882  ftdi_set_signal(tmsc_en, '0'); /* put TMSC in high impedance */
883 
884  /* drive another TCK without driving TMSC (TDO cycle) */
885  mpsse_clock_tms_cs(mpsse_ctx, &zero, 0, in, in_offset + i, 1, false, mode);
886 
887  if (tmsc_en)
888  ftdi_set_signal(tmsc_en, '1'); /* drive again TMSC */
889  }
890 }
891 
892 static void oscan1_mpsse_clock_tms_cs_out(struct mpsse_ctx *ctx, const uint8_t *out, unsigned int out_offset,
893  unsigned int length, bool tdi, uint8_t mode)
894 {
895  oscan1_mpsse_clock_tms_cs(ctx, out, out_offset, 0, 0, length, tdi, mode);
896 }
897 
898 static void cjtag_set_tck_tms_tdi(struct signal *tck, char tckvalue, struct signal *tms,
899  char tmsvalue, struct signal *tdi, char tdivalue)
900 {
901  ftdi_set_signal(tms, tmsvalue);
902  ftdi_set_signal(tdi, tdivalue);
903  ftdi_set_signal(tck, tckvalue);
904 }
905 
906 static void cjtag_reset_online_activate(void)
907 {
908  /* After TAP reset, the cJTAG-to-JTAG adapter is in offline and
909  non-activated state. Escape sequences are needed to bring the
910  TAP online and activated into the desired working mode. */
911 
912  struct signal *tck = find_signal_by_name("TCK");
913  struct signal *tdi = find_signal_by_name("TDI");
914  struct signal *tms = find_signal_by_name("TMS");
915  struct signal *tdo = find_signal_by_name("TDO");
916  struct signal *tmsc_en = find_signal_by_name("TMSC_EN");
917  uint16_t tdovalue;
918 
919  static struct {
920  int8_t tck;
921  int8_t tms;
922  int8_t tdi;
923  } sequence[] = {
924  /* TCK=0, TMS=1, TDI=0 (drive TMSC to 0 baseline) */
925  {'0', '1', '0'},
926 
927  /* Drive cJTAG escape sequence for TAP reset - 8 TMSC edges */
928  /* TCK=1, TMS=1, TDI=0 (rising edge of TCK with TMSC still 0) */
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=1, TMS=1, TDI=1 (drive rising TMSC edge) */
943  {'1', '1', '1'},
944  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
945  {'1', '1', '0'},
946  /* TCK=0, TMS=1, TDI=0 (falling edge TCK with TMSC still 0) */
947  {'0', '1', '0'},
948 
949  /* 3 TCK pulses for padding */
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  /* TCK=1, TMS=1, TDI=0 (drive rising TCK edge) */
959  {'1', '1', '0'},
960  /* TCK=0, TMS=1, TDI=0 (drive falling TCK edge) */
961  {'0', '1', '0'},
962 
963  /* Drive cJTAG escape sequence for SELECT */
964  /* TCK=1, TMS=1, TDI=0 (rising edge of TCK with TMSC still 0, TAP reset that was just setup occurs here too) */
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=1, TMS=1, TDI=1 (drive rising TMSC edge) */
975  {'1', '1', '1'},
976  /* TCK=1, TMS=1, TDI=0 (drive falling TMSC edge) */
977  {'1', '1', '0'},
978  /* TCK=0, TMS=1, TDI=0 (falling edge TCK with TMSC still 0) */
979  {'0', '1', '0'},
980 
981  /* Drive cJTAG escape sequence for OScan1 activation -- OAC = 1100 -> 2 wires -- */
982  /* TCK=1, TMS=1, TDI=0 (rising edge TCK with TMSC still 0... online mode activated... also OAC bit0==0) */
983  {'1', '1', '0'},
984  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
985  {'0', '1', '0'},
986  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... OAC bit1==0) */
987  {'1', '1', '0'},
988  /* TCK=0, TMS=1, TDI=1 (falling edge TCK) */
989  {'0', '1', '1'},
990  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... OAC bit2==1) */
991  {'1', '1', '1'},
992  /* TCK=0, TMS=1, TDI=1 (falling edge TCK, TMSC stays high) */
993  {'0', '1', '1'},
994  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... OAC bit3==1) */
995  {'1', '1', '1'},
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 bit0==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 bit1==0) */
1003  {'1', '1', '0'},
1004  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1005  {'0', '1', '0'},
1006  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... EC bit2==0) */
1007  {'1', '1', '0'},
1008  /* TCK=0, TMS=1, TDI=1 (falling edge TCK) */
1009  {'0', '1', '1'},
1010  /* TCK=1, TMS=1, TDI=1 (rising edge TCK... EC bit3==1) */
1011  {'1', '1', '1'},
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 bit0==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 bit1==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 bit2==0) */
1023  {'1', '1', '0'},
1024  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1025  {'0', '1', '0'},
1026  /* TCK=1, TMS=1, TDI=0 (rising edge TCK... CP bit3==0) */
1027  {'1', '1', '0'},
1028  /* TCK=0, TMS=1, TDI=0 (falling edge TCK) */
1029  {'0', '1', '0'},
1030  };
1031 
1032  if (!oscan1_mode && !jscan3_mode)
1033  return; /* Nothing to do */
1034 
1035  if (oscan1_mode && jscan3_mode) {
1036  LOG_ERROR("Both oscan1_mode and jscan3_mode are \"on\". At most one of them can be enabled.");
1037  return;
1038  }
1039 
1040  if (!tck) {
1041  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TCK signal is not defined");
1042  return;
1043  }
1044 
1045  if (!tdi) {
1046  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TDI signal is not defined");
1047  return;
1048  }
1049 
1050  if (!tms) {
1051  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TMS signal is not defined");
1052  return;
1053  }
1054 
1055  if (!tdo) {
1056  LOG_ERROR("Can't run cJTAG online/activate escape sequences: TDO signal is not defined");
1057  return;
1058  }
1059 
1060  if (jscan3_mode) {
1061  /* Update the sequence above to enable JScan3 instead of OScan1 */
1062  sequence[ESCAPE_SEQ_OAC_BIT2].tdi = '0';
1063  sequence[ESCAPE_SEQ_OAC_BIT2 + 1].tdi = '0';
1064  }
1065 
1066  /* if defined TMSC_EN, replace tms with it */
1067  if (tmsc_en)
1068  tms = tmsc_en;
1069 
1070  /* Send the sequence to the adapter */
1071  for (size_t i = 0; i < ARRAY_SIZE(sequence); i++)
1072  cjtag_set_tck_tms_tdi(tck, sequence[i].tck, tms, sequence[i].tms, tdi, sequence[i].tdi);
1073 
1074  /* If JScan3 mode, configure cJTAG adapter to 4-wire */
1075  if (jscan3_mode)
1076  ftdi_set_signal(find_signal_by_name("JTAG_SEL"), '1');
1077 
1078  ftdi_get_signal(tdo, &tdovalue); /* Just to force a flush */
1079 }
1080 #endif /* #if BUILD_FTDI_CJTAG == 1 */
1081 
1082 COMMAND_HANDLER(ftdi_handle_channel_command)
1083 {
1084  if (CMD_ARGC == 1)
1086  else
1088 
1089  return ERROR_OK;
1090 }
1091 
1092 COMMAND_HANDLER(ftdi_handle_layout_init_command)
1093 {
1094  if (CMD_ARGC != 2)
1096 
1099 
1100  return ERROR_OK;
1101 }
1102 
1103 COMMAND_HANDLER(ftdi_handle_layout_signal_command)
1104 {
1105  if (CMD_ARGC < 1)
1107 
1108  bool invert_data = false;
1109  uint16_t data_mask = 0;
1110  bool invert_input = false;
1111  uint16_t input_mask = 0;
1112  bool invert_oe = false;
1113  uint16_t oe_mask = 0;
1114  for (unsigned int i = 1; i < CMD_ARGC; i += 2) {
1115  if (strcmp("-data", CMD_ARGV[i]) == 0) {
1116  invert_data = false;
1117  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
1118  } else if (strcmp("-ndata", CMD_ARGV[i]) == 0) {
1119  invert_data = true;
1120  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], data_mask);
1121  } else if (strcmp("-input", CMD_ARGV[i]) == 0) {
1122  invert_input = false;
1123  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
1124  } else if (strcmp("-ninput", CMD_ARGV[i]) == 0) {
1125  invert_input = true;
1126  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], input_mask);
1127  } else if (strcmp("-oe", CMD_ARGV[i]) == 0) {
1128  invert_oe = false;
1129  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
1130  } else if (strcmp("-noe", CMD_ARGV[i]) == 0) {
1131  invert_oe = true;
1132  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], oe_mask);
1133  } else if (!strcmp("-alias", CMD_ARGV[i]) ||
1134  !strcmp("-nalias", CMD_ARGV[i])) {
1135  if (!strcmp("-nalias", CMD_ARGV[i])) {
1136  invert_data = true;
1137  invert_input = true;
1138  }
1139  struct signal *sig = find_signal_by_name(CMD_ARGV[i + 1]);
1140  if (!sig) {
1141  LOG_ERROR("signal %s is not defined", CMD_ARGV[i + 1]);
1142  return ERROR_FAIL;
1143  }
1144  data_mask = sig->data_mask;
1145  input_mask = sig->input_mask;
1146  oe_mask = sig->oe_mask;
1147  invert_input ^= sig->invert_input;
1148  invert_oe = sig->invert_oe;
1149  invert_data ^= sig->invert_data;
1150  } else {
1151  LOG_ERROR("unknown option '%s'", CMD_ARGV[i]);
1153  }
1154  }
1155 
1156  struct signal *sig;
1157  sig = find_signal_by_name(CMD_ARGV[0]);
1158  if (!sig)
1159  sig = create_signal(CMD_ARGV[0]);
1160  if (!sig) {
1161  LOG_ERROR("failed to create signal %s", CMD_ARGV[0]);
1162  return ERROR_FAIL;
1163  }
1164 
1165  sig->invert_data = invert_data;
1166  sig->data_mask = data_mask;
1167  sig->invert_input = invert_input;
1168  sig->input_mask = input_mask;
1169  sig->invert_oe = invert_oe;
1170  sig->oe_mask = oe_mask;
1171 
1172  return ERROR_OK;
1173 }
1174 
1175 COMMAND_HANDLER(ftdi_handle_set_signal_command)
1176 {
1177  if (CMD_ARGC < 2)
1179 
1180  struct signal *sig;
1181  sig = find_signal_by_name(CMD_ARGV[0]);
1182  if (!sig) {
1183  LOG_ERROR("interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
1184  return ERROR_FAIL;
1185  }
1186 
1187  switch (*CMD_ARGV[1]) {
1188  case '0':
1189  case '1':
1190  case 'z':
1191  case 'Z':
1192  /* single character level specifier only */
1193  if (CMD_ARGV[1][1] == '\0') {
1194  ftdi_set_signal(sig, *CMD_ARGV[1]);
1195  break;
1196  }
1197  /* fallthrough */
1198  default:
1199  LOG_ERROR("unknown signal level '%s', use 0, 1 or z", CMD_ARGV[1]);
1201  }
1202 
1203  return mpsse_flush(mpsse_ctx);
1204 }
1205 
1206 COMMAND_HANDLER(ftdi_handle_get_signal_command)
1207 {
1208  if (CMD_ARGC < 1)
1210 
1211  struct signal *sig;
1212  uint16_t sig_data = 0;
1213  sig = find_signal_by_name(CMD_ARGV[0]);
1214  if (!sig) {
1215  command_print(CMD, "interface configuration doesn't define signal '%s'", CMD_ARGV[0]);
1216  return ERROR_FAIL;
1217  }
1218 
1219  int ret = ftdi_get_signal(sig, &sig_data);
1220  if (ret != ERROR_OK)
1221  return ret;
1222 
1223  command_print(CMD, "%#06x", sig_data);
1224 
1225  return ERROR_OK;
1226 }
1227 
1228 COMMAND_HANDLER(ftdi_handle_tdo_sample_edge_command)
1229 {
1230  const struct nvp *n;
1231  static const struct nvp nvp_ftdi_jtag_modes[] = {
1232  { .name = "rising", .value = JTAG_MODE },
1233  { .name = "falling", .value = JTAG_MODE_ALT },
1234  { .name = NULL, .value = -1 },
1235  };
1236 
1237  if (CMD_ARGC > 0) {
1238  n = nvp_name2value(nvp_ftdi_jtag_modes, CMD_ARGV[0]);
1239  if (!n->name)
1241  ftdi_jtag_mode = n->value;
1242 
1243  }
1244 
1245  n = nvp_value2name(nvp_ftdi_jtag_modes, ftdi_jtag_mode);
1246  command_print(CMD, "ftdi samples TDO on %s edge of TCK", n->name);
1247 
1248  return ERROR_OK;
1249 }
1250 
1251 #if BUILD_FTDI_CJTAG == 1
1252 COMMAND_HANDLER(ftdi_handle_oscan1_mode_command)
1253 {
1254  if (CMD_ARGC > 1)
1256 
1257  if (CMD_ARGC == 1)
1258  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], oscan1_mode);
1259 
1260  command_print(CMD, "oscan1 mode: %s.", oscan1_mode ? "on" : "off");
1261  return ERROR_OK;
1262 }
1263 
1264 COMMAND_HANDLER(ftdi_handle_jscan3_mode_command)
1265 {
1266  if (CMD_ARGC > 1)
1268 
1269  if (CMD_ARGC == 1)
1270  COMMAND_PARSE_ON_OFF(CMD_ARGV[0], jscan3_mode);
1271 
1272  command_print(CMD, "jscan3 mode: %s.", jscan3_mode ? "on" : "off");
1273  return ERROR_OK;
1274 }
1275 #endif
1276 
1277 static const struct command_registration ftdi_subcommand_handlers[] = {
1278  {
1279  .name = "channel",
1280  .handler = &ftdi_handle_channel_command,
1281  .mode = COMMAND_CONFIG,
1282  .help = "set the channel of the FTDI device that is used as JTAG",
1283  .usage = "(0-3)",
1284  },
1285  {
1286  .name = "layout_init",
1287  .handler = &ftdi_handle_layout_init_command,
1288  .mode = COMMAND_CONFIG,
1289  .help = "initialize the FTDI GPIO signals used "
1290  "to control output-enables and reset signals",
1291  .usage = "data direction",
1292  },
1293  {
1294  .name = "layout_signal",
1295  .handler = &ftdi_handle_layout_signal_command,
1296  .mode = COMMAND_ANY,
1297  .help = "define a signal controlled by one or more FTDI GPIO as data "
1298  "and/or output enable",
1299  .usage = "name [-data mask|-ndata mask] [-oe mask|-noe mask] [-alias|-nalias name]",
1300  },
1301  {
1302  .name = "set_signal",
1303  .handler = &ftdi_handle_set_signal_command,
1304  .mode = COMMAND_EXEC,
1305  .help = "control a layout-specific signal",
1306  .usage = "name (1|0|z)",
1307  },
1308  {
1309  .name = "get_signal",
1310  .handler = &ftdi_handle_get_signal_command,
1311  .mode = COMMAND_EXEC,
1312  .help = "read the value of a layout-specific signal",
1313  .usage = "name",
1314  },
1315  {
1316  .name = "tdo_sample_edge",
1317  .handler = &ftdi_handle_tdo_sample_edge_command,
1318  .mode = COMMAND_ANY,
1319  .help = "set which TCK clock edge is used for sampling TDO "
1320  "- default is rising-edge (Setting to falling-edge may "
1321  "allow signalling speed increase)",
1322  .usage = "(rising|falling)",
1323  },
1324 #if BUILD_FTDI_CJTAG == 1
1325  {
1326  .name = "oscan1_mode",
1327  .handler = &ftdi_handle_oscan1_mode_command,
1328  .mode = COMMAND_ANY,
1329  .help = "set to 'on' to use OScan1 mode for signaling, otherwise 'off' (default is 'off')",
1330  .usage = "(on|off)",
1331  },
1332  {
1333  .name = "jscan3_mode",
1334  .handler = &ftdi_handle_jscan3_mode_command,
1335  .mode = COMMAND_ANY,
1336  .help = "set to 'on' to use JScan3 mode for signaling, otherwise 'off' (default is 'off')",
1337  .usage = "(on|off)",
1338  },
1339 #endif
1341 };
1342 
1343 static const struct command_registration ftdi_command_handlers[] = {
1344  {
1345  .name = "ftdi",
1346  .mode = COMMAND_ANY,
1347  .help = "perform ftdi management",
1348  .chain = ftdi_subcommand_handlers,
1349  .usage = "",
1350  },
1352 };
1353 
1354 static int create_default_signal(const char *name, uint16_t data_mask)
1355 {
1356  struct signal *sig = create_signal(name);
1357  if (!sig) {
1358  LOG_ERROR("failed to create signal %s", name);
1359  return ERROR_FAIL;
1360  }
1361  sig->invert_data = false;
1362  sig->data_mask = data_mask;
1363  sig->invert_oe = false;
1364  sig->oe_mask = 0;
1365 
1366  return ERROR_OK;
1367 }
1368 
1369 static int create_signals(void)
1370 {
1371  if (create_default_signal("TCK", 0x01) != ERROR_OK)
1372  return ERROR_FAIL;
1373  if (create_default_signal("TDI", 0x02) != ERROR_OK)
1374  return ERROR_FAIL;
1375  if (create_default_signal("TDO", 0x04) != ERROR_OK)
1376  return ERROR_FAIL;
1377  if (create_default_signal("TMS", 0x08) != ERROR_OK)
1378  return ERROR_FAIL;
1379  return ERROR_OK;
1380 }
1381 
1382 static int ftdi_swd_init(void)
1383 {
1384  LOG_INFO("FTDI SWD mode enabled");
1385  swd_mode = true;
1386 
1387  if (create_signals() != ERROR_OK)
1388  return ERROR_FAIL;
1389 
1390  swd_cmd_queue_alloced = 10;
1391  swd_cmd_queue = malloc(swd_cmd_queue_alloced * sizeof(*swd_cmd_queue));
1392 
1393  return swd_cmd_queue ? ERROR_OK : ERROR_FAIL;
1394 }
1395 
1396 static void ftdi_swd_swdio_en(bool enable)
1397 {
1398  struct signal *oe = find_signal_by_name("SWDIO_OE");
1399  if (oe) {
1400  if (oe->data_mask)
1401  ftdi_set_signal(oe, enable ? '1' : '0');
1402  else {
1403  /* Sets TDI/DO pin to input during rx when both pins are connected
1404  to SWDIO */
1405  if (enable)
1406  direction |= jtag_direction_init & 0x0002U;
1407  else
1408  direction &= ~0x0002U;
1410  }
1411  }
1412 }
1413 
1418 static int ftdi_swd_run_queue(void)
1419 {
1420  LOG_DEBUG_IO("Executing %zu queued transactions", swd_cmd_queue_length);
1421  int retval;
1422  struct signal *led = find_signal_by_name("LED");
1423 
1424  if (queued_retval != ERROR_OK) {
1425  LOG_DEBUG_IO("Skipping due to previous errors: %d", queued_retval);
1426  goto skip;
1427  }
1428 
1429  /* A transaction must be followed by another transaction or at least 8 idle cycles to
1430  * ensure that data is clocked through the AP. */
1432 
1433  /* Terminate the "blink", if the current layout has that feature */
1434  if (led)
1435  ftdi_set_signal(led, '0');
1436 
1438  if (queued_retval != ERROR_OK) {
1439  LOG_ERROR("MPSSE failed");
1440  goto skip;
1441  }
1442 
1443  for (size_t i = 0; i < swd_cmd_queue_length; i++) {
1444  int ack = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1, 3);
1445 
1446  /* Devices do not reply to DP_TARGETSEL write cmd, ignore received ack */
1447  bool check_ack = swd_cmd_returns_ack(swd_cmd_queue[i].cmd);
1448 
1449  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK) ? LOG_LVL_DEBUG : LOG_LVL_DEBUG_IO,
1450  "%s%s %s %s reg %X = %08" PRIx32,
1451  check_ack ? "" : "ack ignored ",
1452  ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
1453  swd_cmd_queue[i].cmd & SWD_CMD_APNDP ? "AP" : "DP",
1454  swd_cmd_queue[i].cmd & SWD_CMD_RNW ? "read" : "write",
1455  (swd_cmd_queue[i].cmd & SWD_CMD_A32) >> 1,
1456  buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn,
1457  1 + 3 + (swd_cmd_queue[i].cmd & SWD_CMD_RNW ? 0 : 1), 32));
1458 
1459  if (ack != SWD_ACK_OK && check_ack) {
1461  goto skip;
1462 
1463  } else if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
1464  uint32_t data = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3, 32);
1465  int parity = buf_get_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 32, 1);
1466 
1467  if (parity != parity_u32(data)) {
1468  LOG_ERROR("SWD Read data parity mismatch");
1470  goto skip;
1471  }
1472 
1473  if (swd_cmd_queue[i].dst)
1474  *swd_cmd_queue[i].dst = data;
1475  }
1476  }
1477 
1478 skip:
1480  retval = queued_retval;
1482 
1483  /* Queue a new "blink" */
1484  if (led && retval == ERROR_OK)
1485  ftdi_set_signal(led, '1');
1486 
1487  return retval;
1488 }
1489 
1490 static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
1491 {
1493  /* Not enough room in the queue. Run the queue and increase its size for next time.
1494  * Note that it's not possible to avoid running the queue here, because mpsse contains
1495  * pointers into the queue which may be invalid after the realloc. */
1497  struct swd_cmd_queue_entry *q = realloc(swd_cmd_queue, swd_cmd_queue_alloced * 2 * sizeof(*swd_cmd_queue));
1498  if (q) {
1499  swd_cmd_queue = q;
1500  swd_cmd_queue_alloced *= 2;
1501  LOG_DEBUG("Increased SWD command queue to %zu elements", swd_cmd_queue_alloced);
1502  }
1503  }
1504 
1505  if (queued_retval != ERROR_OK)
1506  return;
1507 
1508  size_t i = swd_cmd_queue_length++;
1510 
1512 
1513  if (swd_cmd_queue[i].cmd & SWD_CMD_RNW) {
1514  /* Queue a read transaction */
1515  swd_cmd_queue[i].dst = dst;
1516 
1517  ftdi_swd_swdio_en(false);
1519  0, 1 + 3 + 32 + 1 + 1, SWD_MODE);
1520  ftdi_swd_swdio_en(true);
1521  } else {
1522  /* Queue a write transaction */
1523  ftdi_swd_swdio_en(false);
1524 
1526  0, 1 + 3 + 1, SWD_MODE);
1527 
1528  ftdi_swd_swdio_en(true);
1529 
1530  buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1, 32, data);
1531  buf_set_u32(swd_cmd_queue[i].trn_ack_data_parity_trn, 1 + 3 + 1 + 32, 1, parity_u32(data));
1532 
1534  1 + 3 + 1, 32 + 1, SWD_MODE);
1535  }
1536 
1537  /* Insert idle cycles after AP accesses to avoid WAIT */
1538  if (cmd & SWD_CMD_APNDP)
1539  mpsse_clock_data_out(mpsse_ctx, NULL, 0, ap_delay_clk, SWD_MODE);
1540 
1541 }
1542 
1543 static int ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
1544 {
1545  assert(cmd & SWD_CMD_RNW);
1546  ftdi_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
1547 
1548  return ERROR_OK; /* TODO: return error instead of queuing it */
1549 }
1550 
1551 static int ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
1552 {
1553  assert(!(cmd & SWD_CMD_RNW));
1554  ftdi_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
1555 
1556  return ERROR_OK; /* TODO: return error instead of queuing it */
1557 }
1558 
1560 {
1561  switch (seq) {
1562  case LINE_RESET:
1563  LOG_DEBUG("SWD line reset");
1564  ftdi_swd_swdio_en(true);
1566  break;
1567  case JTAG_TO_SWD:
1568  LOG_DEBUG("JTAG-to-SWD");
1569  ftdi_swd_swdio_en(true);
1571  break;
1572  case JTAG_TO_DORMANT:
1573  LOG_DEBUG("JTAG-to-DORMANT");
1574  ftdi_swd_swdio_en(true);
1576  break;
1577  case SWD_TO_JTAG:
1578  LOG_DEBUG("SWD-to-JTAG");
1579  ftdi_swd_swdio_en(true);
1581  break;
1582  case SWD_TO_DORMANT:
1583  LOG_DEBUG("SWD-to-DORMANT");
1584  ftdi_swd_swdio_en(true);
1586  break;
1587  case DORMANT_TO_SWD:
1588  LOG_DEBUG("DORMANT-to-SWD");
1589  ftdi_swd_swdio_en(true);
1591  break;
1592  case DORMANT_TO_JTAG:
1593  LOG_DEBUG("DORMANT-to-JTAG");
1594  ftdi_swd_swdio_en(true);
1596  break;
1597  default:
1598  LOG_ERROR("Sequence %d not supported", seq);
1599  return ERROR_FAIL;
1600  }
1601 
1602  return ERROR_OK;
1603 }
1604 
1605 static const struct swd_driver ftdi_swd = {
1606  .init = ftdi_swd_init,
1607  .switch_seq = ftdi_swd_switch_seq,
1608  .read_reg = ftdi_swd_read_reg,
1609  .write_reg = ftdi_swd_write_reg,
1610  .run = ftdi_swd_run_queue,
1611 };
1612 
1613 static struct jtag_interface ftdi_interface = {
1615  .execute_queue = ftdi_execute_queue,
1616 };
1617 
1619  .name = "ftdi",
1620  .transport_ids = TRANSPORT_JTAG | TRANSPORT_SWD,
1621  .transport_preferred_id = TRANSPORT_JTAG,
1622  .commands = ftdi_command_handlers,
1623 
1624  .init = ftdi_initialize,
1625  .quit = ftdi_quit,
1626  .reset = ftdi_reset,
1627  .speed = ftdi_speed,
1628  .khz = ftdi_khz,
1629  .speed_div = ftdi_speed_div,
1630 
1631  .jtag_ops = &ftdi_interface,
1632  .swd_ops = &ftdi_swd,
1633 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:313
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:344
unsigned int adapter_get_speed_khz(void)
Retrieves the clock speed of the adapter in kHz.
Definition: adapter.c:221
const char * adapter_usb_get_product_name(void)
Definition: adapter.c:354
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:339
const char * adapter_usb_get_location(void)
Definition: adapter.c:349
const char * name
Definition: am13e230x.c:167
unsigned char bit
Definition: am13e230x.c:166
#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:280
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:1343
static void ftdi_swd_swdio_en(bool enable)
Definition: ftdi.c:1396
#define DO_CLOCK_TMS_CS_OUT
Definition: ftdi.c:85
static int queued_retval
Definition: ftdi.c:153
static struct signal * create_signal(const char *name)
Definition: ftdi.c:172
static int ftdi_swd_init(void)
Definition: ftdi.c:1382
static uint16_t direction
Definition: ftdi.c:157
static int ftdi_execute_pathmove(struct jtag_command *cmd)
Definition: ftdi.c:435
static struct jtag_interface ftdi_interface
Definition: ftdi.c:1613
#define JTAG_MODE
Definition: ftdi.c:88
static int ftdi_execute_stableclocks(struct jtag_command *cmd)
Definition: ftdi.c:644
static int ftdi_execute_sleep(struct jtag_command *cmd)
Definition: ftdi.c:630
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:1618
static const struct swd_driver ftdi_swd
Definition: ftdi.c:1605
static struct mpsse_ctx * mpsse_ctx
Definition: ftdi.c:130
static size_t swd_cmd_queue_alloced
Definition: ftdi.c:152
static int ftdi_reset(int trst, int srst)
Definition: ftdi.c:592
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:701
static int ftdi_execute_scan(struct jtag_command *cmd)
Definition: ftdi.c:487
static int ftdi_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: ftdi.c:1543
static int 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:421
static void ftdi_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
Definition: ftdi.c:1490
static int ftdi_swd_run_queue(void)
Flush the MPSSE queue and process the SWD transaction queue.
Definition: ftdi.c:1418
static int ftdi_execute_statemove(struct jtag_command *cmd)
Definition: ftdi.c:401
#define JTAG_MODE_ALT
Definition: ftdi.c:89
static uint16_t jtag_direction_init
Definition: ftdi.c:159
static int ftdi_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: ftdi.c:1551
static struct signal * find_signal_by_name(const char *name)
Definition: ftdi.c:163
static int ftdi_initialize(void)
Definition: ftdi.c:728
static int ftdi_quit(void)
Definition: ftdi.c:785
#define SWD_MODE
Definition: ftdi.c:90
COMMAND_HANDLER(ftdi_handle_channel_command)
Definition: ftdi.c:1082
static int create_default_signal(const char *name, uint16_t data_mask)
Definition: ftdi.c:1354
#define DO_CLOCK_DATA
Definition: ftdi.c:83
static uint8_t ftdi_jtag_mode
Definition: ftdi.c:93
static int ftdi_execute_runtest(struct jtag_command *cmd)
Definition: ftdi.c:368
static int ftdi_execute_command(struct jtag_command *cmd)
Definition: ftdi.c:669
static int ftdi_swd_switch_seq(enum swd_special_seq seq)
Definition: ftdi.c:1559
#define DO_CLOCK_TMS_CS
Definition: ftdi.c:84
static int freq
Definition: ftdi.c:154
static int create_signals(void)
Definition: ftdi.c:1369
static const struct command_registration ftdi_subcommand_handlers[]
Definition: ftdi.c:1277
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
enum tap_state tap_get_end_state(void)
For more information,.
Definition: interface.c:67
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:234
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:355
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:206
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
int tap_set_end_state_stable(enum tap_state new_end_state)
Similar as tap_set_end_state(), checks new_end_state is stable.
Definition: interface.c:56
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
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:201
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:199
#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:1058
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1730
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_QUEUE_FAILED
Definition: jtag.h:556
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
#define ERROR_JTAG_TRANSITION_INVALID
Definition: jtag.h:560
@ 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
Represents a driver for a debugging interface.
Definition: interface.h:219
const char *const name
The name of the interface driver.
Definition: interface.h:221
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:194
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:198
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
static unsigned int parity(unsigned int v)
Definition: xscale.c:619