OpenOCD
jtag/core.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2009 Zachary T Welch *
5  * zw@superlucidity.net *
6  * *
7  * Copyright (C) 2007,2008,2009 Øyvind Harboe *
8  * oyvind.harboe@zylin.com *
9  * *
10  * Copyright (C) 2009 SoftPLC Corporation *
11  * http://softplc.com *
12  * dick@softplc.com *
13  * *
14  * Copyright (C) 2005 by Dominic Rath *
15  * Dominic.Rath@gmx.de *
16  ***************************************************************************/
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 
22 #include "adapter.h"
23 #include "jtag.h"
24 #include "swd.h"
25 #include "interface.h"
26 #include <transport/transport.h>
27 #include <helper/jep106.h>
28 #include "helper/system.h"
29 
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33 
34 /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
35 #include "svf/svf.h"
36 #include "xsvf/xsvf.h"
37 
38 /* ipdbg are utilities to debug IP-cores. It uses JTAG for transport. */
39 #include "server/ipdbg.h"
40 
43 
44 /* Sleep this # of ms after flushing the queue */
46 
47 static void jtag_add_scan_check(struct jtag_tap *active,
48  void (*jtag_add_scan)(struct jtag_tap *active,
49  int in_num_fields,
50  const struct scan_field *in_fields,
52  int in_num_fields, struct scan_field *in_fields, tap_state_t state);
53 
64 static int jtag_error = ERROR_OK;
65 
66 static const char *jtag_event_strings[] = {
67  [JTAG_TRST_ASSERTED] = "TAP reset",
68  [JTAG_TAP_EVENT_SETUP] = "TAP setup",
69  [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
70  [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
71 };
72 
73 /*
74  * JTAG adapters must initialize with TRST and SRST de-asserted
75  * (they're negative logic, so that means *high*). But some
76  * hardware doesn't necessarily work that way ... so set things
77  * up so that jtag_init() always forces that state.
78  */
79 static int jtag_trst = -1;
80 static int jtag_srst = -1;
81 
85 static struct jtag_tap *__jtag_all_taps;
86 
89 
90 static bool jtag_verify_capture_ir = true;
91 static int jtag_verify = 1;
92 
93 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines
94  *deasserted (in ms) */
95 static int adapter_nsrst_delay; /* default to no nSRST delay */
96 static int jtag_ntrst_delay;/* default to no nTRST delay */
97 static int adapter_nsrst_assert_width; /* width of assertion */
98 static int jtag_ntrst_assert_width; /* width of assertion */
99 
108  void *priv;
111 };
112 
113 /* callbacks to inform high-level handlers about JTAG state changes */
115 
116 extern struct adapter_driver *adapter_driver;
117 
119 {
121 }
122 
123 void jtag_set_error(int error)
124 {
125  if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
126  return;
127  jtag_error = error;
128 }
129 
131 {
132  int temp = jtag_error;
134  return temp;
135 }
136 
137 /************/
138 
139 static bool jtag_poll = true;
140 static bool jtag_poll_en = true;
141 
143 {
144  /* Polling can be disabled explicitly with set_enabled(false).
145  * It can also be masked with mask().
146  * It is also implicitly disabled while TRST is active and
147  * while SRST is gating the JTAG clock.
148  */
149  if (!jtag_poll_en)
150  return false;
151 
152  if (!transport_is_jtag())
153  return jtag_poll;
154 
155  if (!jtag_poll || jtag_trst != 0)
156  return false;
158 }
159 
161 {
162  return jtag_poll;
163 }
164 
165 void jtag_poll_set_enabled(bool value)
166 {
167  jtag_poll = value;
168 }
169 
170 bool jtag_poll_mask(void)
171 {
172  bool retval = jtag_poll_en;
173  jtag_poll_en = false;
174  return retval;
175 }
176 
177 void jtag_poll_unmask(bool saved)
178 {
179  jtag_poll_en = saved;
180 }
181 
182 /************/
183 
184 struct jtag_tap *jtag_all_taps(void)
185 {
186  return __jtag_all_taps;
187 };
188 
189 unsigned jtag_tap_count(void)
190 {
191  struct jtag_tap *t = jtag_all_taps();
192  unsigned n = 0;
193  while (t) {
194  n++;
195  t = t->next_tap;
196  }
197  return n;
198 }
199 
201 {
202  struct jtag_tap *t = jtag_all_taps();
203  unsigned n = 0;
204  while (t) {
205  if (t->enabled)
206  n++;
207  t = t->next_tap;
208  }
209  return n;
210 }
211 
213 static void jtag_tap_add(struct jtag_tap *t)
214 {
215  unsigned jtag_num_taps = 0;
216 
217  struct jtag_tap **tap = &__jtag_all_taps;
218  while (*tap) {
219  jtag_num_taps++;
220  tap = &(*tap)->next_tap;
221  }
222  *tap = t;
223  t->abs_chain_position = jtag_num_taps;
224 }
225 
226 /* returns a pointer to the n-th device in the scan chain */
227 struct jtag_tap *jtag_tap_by_position(unsigned n)
228 {
229  struct jtag_tap *t = jtag_all_taps();
230 
231  while (t && n-- > 0)
232  t = t->next_tap;
233 
234  return t;
235 }
236 
237 struct jtag_tap *jtag_tap_by_string(const char *s)
238 {
239  /* try by name first */
240  struct jtag_tap *t = jtag_all_taps();
241 
242  while (t) {
243  if (strcmp(t->dotted_name, s) == 0)
244  return t;
245  t = t->next_tap;
246  }
247 
248  /* no tap found by name, so try to parse the name as a number */
249  unsigned n;
250  if (parse_uint(s, &n) != ERROR_OK)
251  return NULL;
252 
253  /* FIXME remove this numeric fallback code late June 2010, along
254  * with all info in the User's Guide that TAPs have numeric IDs.
255  * Also update "scan_chain" output to not display the numbers.
256  */
257  t = jtag_tap_by_position(n);
258  if (t)
259  LOG_WARNING("Specify TAP '%s' by name, not number %u",
260  t->dotted_name, n);
261 
262  return t;
263 }
264 
266 {
267  p = p ? p->next_tap : jtag_all_taps();
268  while (p) {
269  if (p->enabled)
270  return p;
271  p = p->next_tap;
272  }
273  return NULL;
274 }
275 
276 const char *jtag_tap_name(const struct jtag_tap *tap)
277 {
278  return (!tap) ? "(unknown)" : tap->dotted_name;
279 }
280 
281 
283 {
284  struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
285 
286  if (!callback)
288 
289  if (*callbacks_p) {
290  while ((*callbacks_p)->next)
291  callbacks_p = &((*callbacks_p)->next);
292  callbacks_p = &((*callbacks_p)->next);
293  }
294 
295  (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
296  (*callbacks_p)->callback = callback;
297  (*callbacks_p)->priv = priv;
298  (*callbacks_p)->next = NULL;
299 
300  return ERROR_OK;
301 }
302 
304 {
305  struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
306 
307  if (!callback)
309 
310  while (*p) {
311  if (((*p)->priv != priv) || ((*p)->callback != callback)) {
312  p = &(*p)->next;
313  continue;
314  }
315 
316  temp = *p;
317  *p = (*p)->next;
318  free(temp);
319  }
320 
321  return ERROR_OK;
322 }
323 
325 {
327 
328  LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
329 
330  while (callback) {
331  struct jtag_event_callback *next;
332 
333  /* callback may remove itself */
334  next = callback->next;
335  callback->callback(event, callback->priv);
336  callback = next;
337  }
338 
339  return ERROR_OK;
340 }
341 
342 static void jtag_checks(void)
343 {
344  assert(jtag_trst == 0);
345 }
346 
348 {
349  jtag_checks();
350 
351  assert(state != TAP_INVALID);
352 
354 }
355 
356 void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
358 {
360 
361  int retval = interface_jtag_add_ir_scan(active, in_fields, state);
362  jtag_set_error(retval);
363 }
364 
365 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active,
366  int dummy,
367  const struct scan_field *in_fields,
369 {
370  jtag_add_ir_scan_noverify(active, in_fields, state);
371 }
372 
373 /* If fields->in_value is filled out, then the captured IR value will be checked */
374 void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
375 {
376  assert(state != TAP_RESET);
377 
379  /* 8 x 32 bit id's is enough for all invocations */
380 
381  /* if we are to run a verification of the ir scan, we need to get the input back.
382  * We may have to allocate space if the caller didn't ask for the input back.
383  */
384  in_fields->check_value = active->expected;
385  in_fields->check_mask = active->expected_mask;
387  state);
388  } else
389  jtag_add_ir_scan_noverify(active, in_fields, state);
390 }
391 
392 void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
394 {
395  assert(out_bits);
396  assert(state != TAP_RESET);
397 
399 
401  num_bits, out_bits, in_bits, state);
402  jtag_set_error(retval);
403 }
404 
405 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
406  uint8_t *in_check_mask, int num_bits);
407 
409  jtag_callback_data_t data1,
410  jtag_callback_data_t data2,
411  jtag_callback_data_t data3)
412 {
413  return jtag_check_value_inner((uint8_t *)data0,
414  (uint8_t *)data1,
415  (uint8_t *)data2,
416  (int)data3);
417 }
418 
419 static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(
420  struct jtag_tap *active,
421  int in_num_fields,
422  const struct scan_field *in_fields,
424  int in_num_fields, struct scan_field *in_fields, tap_state_t state)
425 {
426  jtag_add_scan(active, in_num_fields, in_fields, state);
427 
428  for (int i = 0; i < in_num_fields; i++) {
429  if ((in_fields[i].check_value) && (in_fields[i].in_value)) {
431  (jtag_callback_data_t)in_fields[i].in_value,
432  (jtag_callback_data_t)in_fields[i].check_value,
433  (jtag_callback_data_t)in_fields[i].check_mask,
434  (jtag_callback_data_t)in_fields[i].num_bits);
435  }
436  }
437 }
438 
439 void jtag_add_dr_scan_check(struct jtag_tap *active,
440  int in_num_fields,
441  struct scan_field *in_fields,
443 {
444  if (jtag_verify)
445  jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
446  else
447  jtag_add_dr_scan(active, in_num_fields, in_fields, state);
448 }
449 
450 
451 void jtag_add_dr_scan(struct jtag_tap *active,
452  int in_num_fields,
453  const struct scan_field *in_fields,
455 {
456  assert(state != TAP_RESET);
457 
459 
460  int retval;
461  retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
462  jtag_set_error(retval);
463 }
464 
465 void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
467 {
468  assert(out_bits);
469  assert(state != TAP_RESET);
470 
472 
473  int retval;
474  retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
475  jtag_set_error(retval);
476 }
477 
478 void jtag_add_tlr(void)
479 {
482 
483  /* NOTE: order here matches TRST path in jtag_add_reset() */
486 }
487 
502 int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
503 {
504  int retval;
505 
508 
509  jtag_checks();
511 
512  retval = interface_add_tms_seq(nbits, seq, state);
513  jtag_set_error(retval);
514  return retval;
515 }
516 
517 void jtag_add_pathmove(int num_states, const tap_state_t *path)
518 {
519  tap_state_t cur_state = cmd_queue_cur_state;
520 
521  /* the last state has to be a stable state */
522  if (!tap_is_state_stable(path[num_states - 1])) {
523  LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
525  return;
526  }
527 
528  for (int i = 0; i < num_states; i++) {
529  if (path[i] == TAP_RESET) {
530  LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
532  return;
533  }
534 
535  if (tap_state_transition(cur_state, true) != path[i] &&
536  tap_state_transition(cur_state, false) != path[i]) {
537  LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
538  tap_state_name(cur_state), tap_state_name(path[i]));
540  return;
541  }
542  cur_state = path[i];
543  }
544 
545  jtag_checks();
546 
547  jtag_set_error(interface_jtag_add_pathmove(num_states, path));
548  cmd_queue_cur_state = path[num_states - 1];
549 }
550 
552 {
553  tap_state_t cur_state = cmd_queue_cur_state;
554 
555  if (goal_state != cur_state) {
556  LOG_DEBUG("cur_state=%s goal_state=%s",
557  tap_state_name(cur_state),
558  tap_state_name(goal_state));
559  }
560 
561  /* If goal is RESET, be paranoid and force that that transition
562  * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
563  */
564  if (goal_state == TAP_RESET)
565  jtag_add_tlr();
566  else if (goal_state == cur_state)
567  /* nothing to do */;
568 
569  else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state)) {
570  unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
571  unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
572  tap_state_t moves[8];
573  assert(tms_count < ARRAY_SIZE(moves));
574 
575  for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1) {
576  bool bit = tms_bits & 1;
577 
578  cur_state = tap_state_transition(cur_state, bit);
579  moves[i] = cur_state;
580  }
581 
582  jtag_add_pathmove(tms_count, moves);
583  } else if (tap_state_transition(cur_state, true) == goal_state
584  || tap_state_transition(cur_state, false) == goal_state)
585  jtag_add_pathmove(1, &goal_state);
586  else
587  return ERROR_FAIL;
588 
589  return ERROR_OK;
590 }
591 
592 void jtag_add_runtest(int num_cycles, tap_state_t state)
593 {
596 }
597 
598 
599 void jtag_add_clocks(int num_cycles)
600 {
602  LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
605  return;
606  }
607 
608  if (num_cycles > 0) {
609  jtag_checks();
611  }
612 }
613 
614 static int adapter_system_reset(int req_srst)
615 {
616  int retval;
617 
618  if (req_srst) {
620  LOG_ERROR("BUG: can't assert SRST");
621  return ERROR_FAIL;
622  }
623  req_srst = 1;
624  }
625 
626  /* Maybe change SRST signal state */
627  if (jtag_srst != req_srst) {
628  retval = adapter_driver->reset(0, req_srst);
629  if (retval != ERROR_OK) {
630  LOG_ERROR("SRST error");
631  return ERROR_FAIL;
632  }
633  jtag_srst = req_srst;
634 
635  if (req_srst) {
636  LOG_DEBUG("SRST line asserted");
639  } else {
640  LOG_DEBUG("SRST line released");
643  }
644  }
645 
646  return ERROR_OK;
647 }
648 
649 static void legacy_jtag_add_reset(int req_tlr_or_trst, int req_srst)
650 {
651  int trst_with_tlr = 0;
652  int new_srst = 0;
653  int new_trst = 0;
654 
655  /* Without SRST, we must use target-specific JTAG operations
656  * on each target; callers should not be requesting SRST when
657  * that signal doesn't exist.
658  *
659  * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
660  * can kick in even if the JTAG adapter can't drive TRST.
661  */
662  if (req_srst) {
664  LOG_ERROR("BUG: can't assert SRST");
666  return;
667  }
669  && !req_tlr_or_trst) {
670  LOG_ERROR("BUG: can't assert only SRST");
672  return;
673  }
674  new_srst = 1;
675  }
676 
677  /* JTAG reset (entry to TAP_RESET state) can always be achieved
678  * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
679  * state first. TRST accelerates it, and bypasses those states.
680  *
681  * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
682  * can kick in even if the JTAG adapter can't drive SRST.
683  */
684  if (req_tlr_or_trst) {
686  trst_with_tlr = 1;
687  else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
688  && !req_srst)
689  trst_with_tlr = 1;
690  else
691  new_trst = 1;
692  }
693 
694  /* Maybe change TRST and/or SRST signal state */
695  if (jtag_srst != new_srst || jtag_trst != new_trst) {
696  int retval;
697 
698  retval = interface_jtag_add_reset(new_trst, new_srst);
699  if (retval != ERROR_OK)
700  jtag_set_error(retval);
701  else
702  retval = jtag_execute_queue();
703 
704  if (retval != ERROR_OK) {
705  LOG_ERROR("TRST/SRST error");
706  return;
707  }
708  }
709 
710  /* SRST resets everything hooked up to that signal */
711  if (jtag_srst != new_srst) {
712  jtag_srst = new_srst;
713  if (jtag_srst) {
714  LOG_DEBUG("SRST line asserted");
717  } else {
718  LOG_DEBUG("SRST line released");
721  }
722  }
723 
724  /* Maybe enter the JTAG TAP_RESET state ...
725  * - using only TMS, TCK, and the JTAG state machine
726  * - or else more directly, using TRST
727  *
728  * TAP_RESET should be invisible to non-debug parts of the system.
729  */
730  if (trst_with_tlr) {
731  LOG_DEBUG("JTAG reset with TLR instead of TRST");
732  jtag_add_tlr();
733 
734  } else if (jtag_trst != new_trst) {
735  jtag_trst = new_trst;
736  if (jtag_trst) {
737  LOG_DEBUG("TRST line asserted");
741  } else {
742  LOG_DEBUG("TRST line released");
743  if (jtag_ntrst_delay)
745 
746  /* We just asserted nTRST, so we're now in TAP_RESET.
747  * Inform possible listeners about this, now that
748  * JTAG instructions and data can be shifted. This
749  * sequence must match jtag_add_tlr().
750  */
753  }
754  }
755 }
756 
757 /* FIXME: name is misleading; we do not plan to "add" reset into jtag queue */
758 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
759 {
760  int retval;
761  int trst_with_tlr = 0;
762  int new_srst = 0;
763  int new_trst = 0;
764 
765  if (!adapter_driver->reset) {
766  legacy_jtag_add_reset(req_tlr_or_trst, req_srst);
767  return;
768  }
769 
770  /* Without SRST, we must use target-specific JTAG operations
771  * on each target; callers should not be requesting SRST when
772  * that signal doesn't exist.
773  *
774  * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
775  * can kick in even if the JTAG adapter can't drive TRST.
776  */
777  if (req_srst) {
779  LOG_ERROR("BUG: can't assert SRST");
781  return;
782  }
784  && !req_tlr_or_trst) {
785  LOG_ERROR("BUG: can't assert only SRST");
787  return;
788  }
789  new_srst = 1;
790  }
791 
792  /* JTAG reset (entry to TAP_RESET state) can always be achieved
793  * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
794  * state first. TRST accelerates it, and bypasses those states.
795  *
796  * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
797  * can kick in even if the JTAG adapter can't drive SRST.
798  */
799  if (req_tlr_or_trst) {
801  trst_with_tlr = 1;
802  else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
803  && !req_srst)
804  trst_with_tlr = 1;
805  else
806  new_trst = 1;
807  }
808 
809  /* Maybe change TRST and/or SRST signal state */
810  if (jtag_srst != new_srst || jtag_trst != new_trst) {
811  /* guarantee jtag queue empty before changing reset status */
813 
814  retval = adapter_driver->reset(new_trst, new_srst);
815  if (retval != ERROR_OK) {
816  jtag_set_error(retval);
817  LOG_ERROR("TRST/SRST error");
818  return;
819  }
820  }
821 
822  /* SRST resets everything hooked up to that signal */
823  if (jtag_srst != new_srst) {
824  jtag_srst = new_srst;
825  if (jtag_srst) {
826  LOG_DEBUG("SRST line asserted");
829  } else {
830  LOG_DEBUG("SRST line released");
833  }
834  }
835 
836  /* Maybe enter the JTAG TAP_RESET state ...
837  * - using only TMS, TCK, and the JTAG state machine
838  * - or else more directly, using TRST
839  *
840  * TAP_RESET should be invisible to non-debug parts of the system.
841  */
842  if (trst_with_tlr) {
843  LOG_DEBUG("JTAG reset with TLR instead of TRST");
844  jtag_add_tlr();
846 
847  } else if (jtag_trst != new_trst) {
848  jtag_trst = new_trst;
849  if (jtag_trst) {
850  LOG_DEBUG("TRST line asserted");
854  } else {
855  LOG_DEBUG("TRST line released");
856  if (jtag_ntrst_delay)
858 
859  /* We just asserted nTRST, so we're now in TAP_RESET.
860  * Inform possible listeners about this, now that
861  * JTAG instructions and data can be shifted. This
862  * sequence must match jtag_add_tlr().
863  */
866  }
867  }
868 }
869 
870 void jtag_add_sleep(uint32_t us)
871 {
873  keep_alive();
875 }
876 
877 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
878  uint8_t *in_check_mask, int num_bits)
879 {
880  int retval = ERROR_OK;
881  int compare_failed;
882 
883  if (in_check_mask)
884  compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
885  else
886  compare_failed = buf_cmp(captured, in_check_value, num_bits);
887 
888  if (compare_failed) {
889  char *captured_str, *in_check_value_str;
890  int bits = (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits;
891 
892  /* NOTE: we've lost diagnostic context here -- 'which tap' */
893 
894  captured_str = buf_to_hex_str(captured, bits);
895  in_check_value_str = buf_to_hex_str(in_check_value, bits);
896 
897  LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
898  captured_str);
899  LOG_WARNING(" check_value: 0x%s", in_check_value_str);
900 
901  free(captured_str);
902  free(in_check_value_str);
903 
904  if (in_check_mask) {
905  char *in_check_mask_str;
906 
907  in_check_mask_str = buf_to_hex_str(in_check_mask, bits);
908  LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
909  free(in_check_mask_str);
910  }
911 
912  retval = ERROR_JTAG_QUEUE_FAILED;
913  }
914  return retval;
915 }
916 
917 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
918 {
919  assert(field->in_value);
920 
921  if (!value) {
922  /* no checking to do */
923  return;
924  }
925 
927 
928  int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
929  jtag_set_error(retval);
930 }
931 
933 {
934  if (!is_adapter_initialized()) {
935  LOG_ERROR("No JTAG interface configured yet. "
936  "Issue 'init' command in startup scripts "
937  "before communicating with targets.");
938  return ERROR_FAIL;
939  }
940 
941  if (!transport_is_jtag()) {
942  /*
943  * FIXME: This should not happen!
944  * There could be old code that queues jtag commands with non jtag interfaces so, for
945  * the moment simply highlight it by log an error and return on empty execute_queue.
946  * We should fix it quitting with assert(0) because it is an internal error.
947  * The fix can be applied immediately after next release (v0.11.0 ?)
948  */
949  LOG_ERROR("JTAG API jtag_execute_queue() called on non JTAG interface");
951  return ERROR_OK;
952  }
953 
955  int result = adapter_driver->jtag_ops->execute_queue(cmd);
956 
957  while (debug_level >= LOG_LVL_DEBUG_IO && cmd) {
958  switch (cmd->type) {
959  case JTAG_SCAN:
960  LOG_DEBUG_IO("JTAG %s SCAN to %s",
961  cmd->cmd.scan->ir_scan ? "IR" : "DR",
962  tap_state_name(cmd->cmd.scan->end_state));
963  for (int i = 0; i < cmd->cmd.scan->num_fields; i++) {
964  struct scan_field *field = cmd->cmd.scan->fields + i;
965  if (field->out_value) {
966  char *str = buf_to_hex_str(field->out_value, field->num_bits);
967  LOG_DEBUG_IO(" %db out: %s", field->num_bits, str);
968  free(str);
969  }
970  if (field->in_value) {
971  char *str = buf_to_hex_str(field->in_value, field->num_bits);
972  LOG_DEBUG_IO(" %db in: %s", field->num_bits, str);
973  free(str);
974  }
975  }
976  break;
977  case JTAG_TLR_RESET:
978  LOG_DEBUG_IO("JTAG TLR RESET to %s",
979  tap_state_name(cmd->cmd.statemove->end_state));
980  break;
981  case JTAG_RUNTEST:
982  LOG_DEBUG_IO("JTAG RUNTEST %d cycles to %s",
983  cmd->cmd.runtest->num_cycles,
984  tap_state_name(cmd->cmd.runtest->end_state));
985  break;
986  case JTAG_RESET:
987  {
988  const char *reset_str[3] = {
989  "leave", "deassert", "assert"
990  };
991  LOG_DEBUG_IO("JTAG RESET %s TRST, %s SRST",
992  reset_str[cmd->cmd.reset->trst + 1],
993  reset_str[cmd->cmd.reset->srst + 1]);
994  }
995  break;
996  case JTAG_PATHMOVE:
997  LOG_DEBUG_IO("JTAG PATHMOVE (TODO)");
998  break;
999  case JTAG_SLEEP:
1000  LOG_DEBUG_IO("JTAG SLEEP (TODO)");
1001  break;
1002  case JTAG_STABLECLOCKS:
1003  LOG_DEBUG_IO("JTAG STABLECLOCKS (TODO)");
1004  break;
1005  case JTAG_TMS:
1006  LOG_DEBUG_IO("JTAG TMS (TODO)");
1007  break;
1008  default:
1009  LOG_ERROR("Unknown JTAG command: %d", cmd->type);
1010  break;
1011  }
1012  cmd = cmd->next;
1013  }
1014 
1015  return result;
1016 }
1017 
1019 {
1022 
1023  if (jtag_flush_queue_sleep > 0) {
1024  /* For debug purposes it can be useful to test performance
1025  * or behavior when delaying after flushing the queue,
1026  * e.g. to simulate long roundtrip times.
1027  */
1028  usleep(jtag_flush_queue_sleep * 1000);
1029  }
1030 }
1031 
1033 {
1034  return jtag_flush_queue_count;
1035 }
1036 
1038 {
1040  return jtag_error_clear();
1041 }
1042 
1043 static int jtag_reset_callback(enum jtag_event event, void *priv)
1044 {
1045  struct jtag_tap *tap = priv;
1046 
1047  if (event == JTAG_TRST_ASSERTED) {
1048  tap->enabled = !tap->disabled_after_reset;
1049 
1050  /* current instruction is either BYPASS or IDCODE */
1051  buf_set_ones(tap->cur_instr, tap->ir_length);
1052  tap->bypass = true;
1053  }
1054 
1055  return ERROR_OK;
1056 }
1057 
1058 /* sleep at least us microseconds. When we sleep more than 1000ms we
1059  * do an alive sleep, i.e. keep GDB alive. Note that we could starve
1060  * GDB if we slept for <1000ms many times.
1061  */
1062 void jtag_sleep(uint32_t us)
1063 {
1064  if (us < 1000)
1065  usleep(us);
1066  else
1067  alive_sleep((us+999)/1000);
1068 }
1069 
1070 #define JTAG_MAX_AUTO_TAPS 20
1071 
1072 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
1073 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
1074 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
1075 
1076 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
1077  * know that no valid TAP will have it as an IDCODE value.
1078  */
1079 #define END_OF_CHAIN_FLAG 0xffffffff
1080 
1081 /* a larger IR length than we ever expect to autoprobe */
1082 #define JTAG_IRLEN_MAX 60
1083 
1084 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
1085 {
1086  struct scan_field field = {
1087  .num_bits = num_idcode * 32,
1088  .out_value = idcode_buffer,
1089  .in_value = idcode_buffer,
1090  };
1091 
1092  /* initialize to the end of chain ID value */
1093  for (unsigned i = 0; i < num_idcode; i++)
1094  buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
1095 
1097  jtag_add_tlr();
1098  return jtag_execute_queue();
1099 }
1100 
1101 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
1102 {
1103  uint8_t zero_check = 0x0;
1104  uint8_t one_check = 0xff;
1105 
1106  for (unsigned i = 0; i < count * 4; i++) {
1107  zero_check |= idcodes[i];
1108  one_check &= idcodes[i];
1109  }
1110 
1111  /* if there wasn't a single non-zero bit or if all bits were one,
1112  * the scan is not valid. We wrote a mix of both values; either
1113  *
1114  * - There's a hardware issue (almost certainly):
1115  * + all-zeroes can mean a target stuck in JTAG reset
1116  * + all-ones tends to mean no target
1117  * - The scan chain is WAY longer than we can handle, *AND* either
1118  * + there are several hundreds of TAPs in bypass, or
1119  * + at least a few dozen TAPs all have an all-ones IDCODE
1120  */
1121  if (zero_check == 0x00 || one_check == 0xff) {
1122  LOG_ERROR("JTAG scan chain interrogation failed: all %s",
1123  (zero_check == 0x00) ? "zeroes" : "ones");
1124  LOG_ERROR("Check JTAG interface, timings, target power, etc.");
1125  return false;
1126  }
1127  return true;
1128 }
1129 
1130 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
1131  const char *name, uint32_t idcode)
1132 {
1133  log_printf_lf(level, __FILE__, __LINE__, __func__,
1134  "JTAG tap: %s %16.16s: 0x%08x "
1135  "(mfg: 0x%3.3x (%s), part: 0x%4.4x, ver: 0x%1.1x)",
1136  name, msg,
1137  (unsigned int)idcode,
1138  (unsigned int)EXTRACT_MFG(idcode),
1140  (unsigned int)EXTRACT_PART(idcode),
1141  (unsigned int)EXTRACT_VER(idcode));
1142 }
1143 
1144 static bool jtag_idcode_is_final(uint32_t idcode)
1145 {
1146  /*
1147  * Some devices, such as AVR8, will output all 1's instead
1148  * of TDI input value at end of chain. Allow those values
1149  * instead of failing.
1150  */
1151  return idcode == END_OF_CHAIN_FLAG;
1152 }
1153 
1161 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
1162 {
1163  bool triggered = false;
1164  for (; count < max - 31; count += 32) {
1165  uint32_t idcode = buf_get_u32(idcodes, count, 32);
1166 
1167  /* do not trigger the warning if the data looks good */
1168  if (jtag_idcode_is_final(idcode))
1169  continue;
1170  LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
1171  count, (unsigned int)idcode);
1172  triggered = true;
1173  }
1174  return triggered;
1175 }
1176 
1177 static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
1178 {
1179 
1180  if (tap->expected_ids_cnt == 0 || !tap->has_idcode)
1181  return true;
1182 
1183  /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
1184  uint32_t mask = tap->ignore_version ? ~(0xfU << 28) : ~0U;
1185  uint32_t idcode = tap->idcode & mask;
1186 
1187  /* Loop over the expected identification codes and test for a match */
1188  for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1189  uint32_t expected = tap->expected_ids[ii] & mask;
1190 
1191  if (idcode == expected)
1192  return true;
1193 
1194  /* treat "-expected-id 0" as a "don't-warn" wildcard */
1195  if (tap->expected_ids[ii] == 0)
1196  return true;
1197  }
1198 
1199  /* If none of the expected ids matched, warn */
1201  tap->dotted_name, tap->idcode);
1202  for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1203  char msg[32];
1204 
1205  snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, tap->expected_ids_cnt);
1207  tap->dotted_name, tap->expected_ids[ii]);
1208  }
1209  return false;
1210 }
1211 
1212 /* Try to examine chain layout according to IEEE 1149.1 §12
1213  * This is called a "blind interrogation" of the scan chain.
1214  */
1215 static int jtag_examine_chain(void)
1216 {
1217  int retval;
1218  unsigned max_taps = jtag_tap_count();
1219 
1220  /* Autoprobe up to this many. */
1221  if (max_taps < JTAG_MAX_AUTO_TAPS)
1222  max_taps = JTAG_MAX_AUTO_TAPS;
1223 
1224  /* Add room for end-of-chain marker. */
1225  max_taps++;
1226 
1227  uint8_t *idcode_buffer = calloc(4, max_taps);
1228  if (!idcode_buffer)
1229  return ERROR_JTAG_INIT_FAILED;
1230 
1231  /* DR scan to collect BYPASS or IDCODE register contents.
1232  * Then make sure the scan data has both ones and zeroes.
1233  */
1234  LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1235  retval = jtag_examine_chain_execute(idcode_buffer, max_taps);
1236  if (retval != ERROR_OK)
1237  goto out;
1238  if (!jtag_examine_chain_check(idcode_buffer, max_taps)) {
1239  retval = ERROR_JTAG_INIT_FAILED;
1240  goto out;
1241  }
1242 
1243  /* Point at the 1st predefined tap, if any */
1244  struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
1245 
1246  unsigned bit_count = 0;
1247  unsigned autocount = 0;
1248  for (unsigned i = 0; i < max_taps; i++) {
1249  assert(bit_count < max_taps * 32);
1250  uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1251 
1252  /* No predefined TAP? Auto-probe. */
1253  if (!tap) {
1254  /* Is there another TAP? */
1256  break;
1257 
1258  /* Default everything in this TAP except IR length.
1259  *
1260  * REVISIT create a jtag_alloc(chip, tap) routine, and
1261  * share it with jim_newtap_cmd().
1262  */
1263  tap = calloc(1, sizeof(*tap));
1264  if (!tap) {
1265  retval = ERROR_FAIL;
1266  goto out;
1267  }
1268 
1269  tap->chip = alloc_printf("auto%u", autocount++);
1270  tap->tapname = strdup("tap");
1271  tap->dotted_name = alloc_printf("%s.%s", tap->chip, tap->tapname);
1272 
1273  tap->ir_length = 0; /* ... signifying irlen autoprobe */
1274  tap->ir_capture_mask = 0x03;
1275  tap->ir_capture_value = 0x01;
1276 
1277  tap->enabled = true;
1278 
1279  jtag_tap_init(tap);
1280  }
1281 
1282  if ((idcode & 1) == 0 && !tap->ignore_bypass) {
1283  /* Zero for LSB indicates a device in bypass */
1284  LOG_INFO("TAP %s does not have valid IDCODE (idcode=0x%" PRIx32 ")",
1285  tap->dotted_name, idcode);
1286  tap->has_idcode = false;
1287  tap->idcode = 0;
1288 
1289  bit_count += 1;
1290  } else {
1291  /* Friendly devices support IDCODE */
1292  tap->has_idcode = true;
1293  tap->idcode = idcode;
1294  jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found", tap->dotted_name, idcode);
1295 
1296  bit_count += 32;
1297  }
1298 
1299  /* ensure the TAP ID matches what was expected */
1300  if (!jtag_examine_chain_match_tap(tap))
1301  retval = ERROR_JTAG_INIT_SOFT_FAIL;
1302 
1303  tap = jtag_tap_next_enabled(tap);
1304  }
1305 
1306  /* After those IDCODE or BYPASS register values should be
1307  * only the data we fed into the scan chain.
1308  */
1309  if (jtag_examine_chain_end(idcode_buffer, bit_count, max_taps * 32)) {
1310  LOG_ERROR("double-check your JTAG setup (interface, speed, ...)");
1311  retval = ERROR_JTAG_INIT_FAILED;
1312  goto out;
1313  }
1314 
1315  /* Return success or, for backwards compatibility if only
1316  * some IDCODE values mismatched, a soft/continuable fault.
1317  */
1318 out:
1319  free(idcode_buffer);
1320  return retval;
1321 }
1322 
1323 /*
1324  * Validate the date loaded by entry to the Capture-IR state, to help
1325  * find errors related to scan chain configuration (wrong IR lengths)
1326  * or communication.
1327  *
1328  * Entry state can be anything. On non-error exit, all TAPs are in
1329  * bypass mode. On error exits, the scan chain is reset.
1330  */
1331 static int jtag_validate_ircapture(void)
1332 {
1333  struct jtag_tap *tap;
1334  uint8_t *ir_test = NULL;
1335  struct scan_field field;
1336  int chain_pos = 0;
1337  int retval;
1338 
1339  /* when autoprobing, accommodate huge IR lengths */
1340  int total_ir_length = 0;
1341  for (tap = jtag_tap_next_enabled(NULL); tap; tap = jtag_tap_next_enabled(tap)) {
1342  if (tap->ir_length == 0)
1343  total_ir_length += JTAG_IRLEN_MAX;
1344  else
1345  total_ir_length += tap->ir_length;
1346  }
1347 
1348  /* increase length to add 2 bit sentinel after scan */
1349  total_ir_length += 2;
1350 
1351  ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
1352  if (!ir_test)
1353  return ERROR_FAIL;
1354 
1355  /* after this scan, all TAPs will capture BYPASS instructions */
1356  buf_set_ones(ir_test, total_ir_length);
1357 
1358  field.num_bits = total_ir_length;
1359  field.out_value = ir_test;
1360  field.in_value = ir_test;
1361 
1363 
1364  LOG_DEBUG("IR capture validation scan");
1365  retval = jtag_execute_queue();
1366  if (retval != ERROR_OK)
1367  goto done;
1368 
1369  tap = NULL;
1370  chain_pos = 0;
1371 
1372  for (;; ) {
1373  tap = jtag_tap_next_enabled(tap);
1374  if (!tap)
1375  break;
1376 
1377  /* If we're autoprobing, guess IR lengths. They must be at
1378  * least two bits. Guessing will fail if (a) any TAP does
1379  * not conform to the JTAG spec; or (b) when the upper bits
1380  * captured from some conforming TAP are nonzero. Or if
1381  * (c) an IR length is longer than JTAG_IRLEN_MAX bits,
1382  * an implementation limit, which could someday be raised.
1383  *
1384  * REVISIT optimization: if there's a *single* TAP we can
1385  * lift restrictions (a) and (b) by scanning a recognizable
1386  * pattern before the all-ones BYPASS. Check for where the
1387  * pattern starts in the result, instead of an 0...01 value.
1388  *
1389  * REVISIT alternative approach: escape to some tcl code
1390  * which could provide more knowledge, based on IDCODE; and
1391  * only guess when that has no success.
1392  */
1393  if (tap->ir_length == 0) {
1394  tap->ir_length = 2;
1395  while (buf_get_u64(ir_test, chain_pos, tap->ir_length + 1) == 1
1396  && tap->ir_length < JTAG_IRLEN_MAX) {
1397  tap->ir_length++;
1398  }
1399  LOG_WARNING("AUTO %s - use \"jtag newtap %s %s -irlen %d "
1400  "-expected-id 0x%08" PRIx32 "\"",
1401  tap->dotted_name, tap->chip, tap->tapname, tap->ir_length, tap->idcode);
1402  }
1403 
1404  /* Validate the two LSBs, which must be 01 per JTAG spec.
1405  *
1406  * Or ... more bits could be provided by TAP declaration.
1407  * Plus, some taps (notably in i.MX series chips) violate
1408  * this part of the JTAG spec, so their capture mask/value
1409  * attributes might disable this test.
1410  */
1411  uint64_t val = buf_get_u64(ir_test, chain_pos, tap->ir_length);
1412  if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
1413  LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64 " not 0x%0*" PRIx32,
1414  jtag_tap_name(tap),
1415  (tap->ir_length + 7) / tap->ir_length, val,
1416  (tap->ir_length + 7) / tap->ir_length, tap->ir_capture_value);
1417 
1418  retval = ERROR_JTAG_INIT_FAILED;
1419  goto done;
1420  }
1421  LOG_DEBUG("%s: IR capture 0x%0*" PRIx64, jtag_tap_name(tap),
1422  (tap->ir_length + 7) / tap->ir_length, val);
1423  chain_pos += tap->ir_length;
1424  }
1425 
1426  /* verify the '11' sentinel we wrote is returned at the end */
1427  uint64_t val = buf_get_u64(ir_test, chain_pos, 2);
1428  if (val != 0x3) {
1429  char *cbuf = buf_to_hex_str(ir_test, total_ir_length);
1430 
1431  LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1432  chain_pos, cbuf);
1433  free(cbuf);
1434  retval = ERROR_JTAG_INIT_FAILED;
1435  }
1436 
1437 done:
1438  free(ir_test);
1439  if (retval != ERROR_OK) {
1440  jtag_add_tlr();
1442  }
1443  return retval;
1444 }
1445 
1446 void jtag_tap_init(struct jtag_tap *tap)
1447 {
1448  unsigned ir_len_bits;
1449  unsigned ir_len_bytes;
1450 
1451  /* if we're autoprobing, cope with potentially huge ir_length */
1452  ir_len_bits = tap->ir_length ? tap->ir_length : JTAG_IRLEN_MAX;
1453  ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
1454 
1455  tap->expected = calloc(1, ir_len_bytes);
1456  tap->expected_mask = calloc(1, ir_len_bytes);
1457  tap->cur_instr = malloc(ir_len_bytes);
1458 
1460  if (ir_len_bits > 32)
1461  ir_len_bits = 32;
1462 
1463  buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
1464  buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
1465 
1466  /* TAP will be in bypass mode after jtag_validate_ircapture() */
1467  tap->bypass = true;
1468  buf_set_ones(tap->cur_instr, tap->ir_length);
1469 
1470  /* register the reset callback for the TAP */
1472  jtag_tap_add(tap);
1473 
1474  LOG_DEBUG("Created Tap: %s @ abs position %d, "
1475  "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1476  tap->abs_chain_position, tap->ir_length,
1477  (unsigned) tap->ir_capture_value,
1478  (unsigned) tap->ir_capture_mask);
1479 }
1480 
1481 void jtag_tap_free(struct jtag_tap *tap)
1482 {
1484 
1485  struct jtag_tap_event_action *jteap = tap->event_action;
1486  while (jteap) {
1487  struct jtag_tap_event_action *next = jteap->next;
1488  Jim_DecrRefCount(jteap->interp, jteap->body);
1489  free(jteap);
1490  jteap = next;
1491  }
1492 
1493  free(tap->expected);
1494  free(tap->expected_mask);
1495  free(tap->expected_ids);
1496  free(tap->cur_instr);
1497  free(tap->chip);
1498  free(tap->tapname);
1499  free(tap->dotted_name);
1500  free(tap);
1501 }
1502 
1503 int jtag_init_inner(struct command_context *cmd_ctx)
1504 {
1505  struct jtag_tap *tap;
1506  int retval;
1507  bool issue_setup = true;
1508 
1509  LOG_DEBUG("Init JTAG chain");
1510 
1511  tap = jtag_tap_next_enabled(NULL);
1512  if (!tap) {
1513  /* Once JTAG itself is properly set up, and the scan chain
1514  * isn't absurdly large, IDCODE autoprobe should work fine.
1515  *
1516  * But ... IRLEN autoprobe can fail even on systems which
1517  * are fully conformant to JTAG. Also, JTAG setup can be
1518  * quite finicky on some systems.
1519  *
1520  * REVISIT: if TAP autoprobe works OK, then in many cases
1521  * we could escape to tcl code and set up targets based on
1522  * the TAP's IDCODE values.
1523  */
1524  LOG_WARNING("There are no enabled taps. "
1525  "AUTO PROBING MIGHT NOT WORK!!");
1526 
1527  /* REVISIT default clock will often be too fast ... */
1528  }
1529 
1530  jtag_add_tlr();
1531  retval = jtag_execute_queue();
1532  if (retval != ERROR_OK)
1533  return retval;
1534 
1535  /* Examine DR values first. This discovers problems which will
1536  * prevent communication ... hardware issues like TDO stuck, or
1537  * configuring the wrong number of (enabled) TAPs.
1538  */
1539  retval = jtag_examine_chain();
1540  switch (retval) {
1541  case ERROR_OK:
1542  /* complete success */
1543  break;
1544  default:
1545  /* For backward compatibility reasons, try coping with
1546  * configuration errors involving only ID mismatches.
1547  * We might be able to talk to the devices.
1548  *
1549  * Also the device might be powered down during startup.
1550  *
1551  * After OpenOCD starts, we can try to power on the device
1552  * and run a reset.
1553  */
1554  LOG_ERROR("Trying to use configured scan chain anyway...");
1555  issue_setup = false;
1556  break;
1557  }
1558 
1559  /* Now look at IR values. Problems here will prevent real
1560  * communication. They mostly mean that the IR length is
1561  * wrong ... or that the IR capture value is wrong. (The
1562  * latter is uncommon, but easily worked around: provide
1563  * ircapture/irmask values during TAP setup.)
1564  */
1565  retval = jtag_validate_ircapture();
1566  if (retval != ERROR_OK) {
1567  /* The target might be powered down. The user
1568  * can power it up and reset it after firing
1569  * up OpenOCD.
1570  */
1571  issue_setup = false;
1572  }
1573 
1574  if (issue_setup)
1576  else
1577  LOG_WARNING("Bypassing JTAG setup events due to errors");
1578 
1579 
1580  return ERROR_OK;
1581 }
1582 
1583 int swd_init_reset(struct command_context *cmd_ctx)
1584 {
1585  int retval, retval1;
1586 
1587  retval = adapter_init(cmd_ctx);
1588  if (retval != ERROR_OK)
1589  return retval;
1590 
1591  LOG_DEBUG("Initializing with hard SRST reset");
1592 
1594  retval = adapter_system_reset(1);
1595  retval1 = adapter_system_reset(0);
1596 
1597  return (retval == ERROR_OK) ? retval1 : retval;
1598 }
1599 
1600 int jtag_init_reset(struct command_context *cmd_ctx)
1601 {
1602  int retval = adapter_init(cmd_ctx);
1603  if (retval != ERROR_OK)
1604  return retval;
1605 
1606  LOG_DEBUG("Initializing with hard TRST+SRST reset");
1607 
1608  /*
1609  * This procedure is used by default when OpenOCD triggers a reset.
1610  * It's now done through an overridable Tcl "init_reset" wrapper.
1611  *
1612  * This started out as a more powerful "get JTAG working" reset than
1613  * jtag_init_inner(), applying TRST because some chips won't activate
1614  * JTAG without a TRST cycle (presumed to be async, though some of
1615  * those chips synchronize JTAG activation using TCK).
1616  *
1617  * But some chips only activate JTAG as part of an SRST cycle; SRST
1618  * got mixed in. So it became a hard reset routine, which got used
1619  * in more places, and which coped with JTAG reset being forced as
1620  * part of SRST (srst_pulls_trst).
1621  *
1622  * And even more corner cases started to surface: TRST and/or SRST
1623  * assertion timings matter; some chips need other JTAG operations;
1624  * TRST/SRST sequences can need to be different from these, etc.
1625  *
1626  * Systems should override that wrapper to support system-specific
1627  * requirements that this not-fully-generic code doesn't handle.
1628  *
1629  * REVISIT once Tcl code can read the reset_config modes, this won't
1630  * need to be a C routine at all...
1631  */
1633  jtag_add_reset(1, 1);
1635  jtag_add_reset(0, 1);
1636  } else {
1637  jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1638  }
1639 
1640  /* some targets enable us to connect with srst asserted */
1643  jtag_add_reset(0, 1);
1644  else {
1645  LOG_WARNING("\'srst_nogate\' reset_config option is required");
1646  jtag_add_reset(0, 0);
1647  }
1648  } else
1649  jtag_add_reset(0, 0);
1650  retval = jtag_execute_queue();
1651  if (retval != ERROR_OK)
1652  return retval;
1653 
1654  /* Check that we can communication on the JTAG chain + eventually we want to
1655  * be able to perform enumeration only after OpenOCD has started
1656  * telnet and GDB server
1657  *
1658  * That would allow users to more easily perform any magic they need to before
1659  * reset happens.
1660  */
1661  return jtag_init_inner(cmd_ctx);
1662 }
1663 
1664 int jtag_init(struct command_context *cmd_ctx)
1665 {
1666  int retval = adapter_init(cmd_ctx);
1667  if (retval != ERROR_OK)
1668  return retval;
1669 
1670  /* guard against oddball hardware: force resets to be inactive */
1671  jtag_add_reset(0, 0);
1672 
1673  /* some targets enable us to connect with srst asserted */
1676  jtag_add_reset(0, 1);
1677  else
1678  LOG_WARNING("\'srst_nogate\' reset_config option is required");
1679  }
1680  retval = jtag_execute_queue();
1681  if (retval != ERROR_OK)
1682  return retval;
1683 
1684  if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1685  return ERROR_FAIL;
1686 
1687  return ERROR_OK;
1688 }
1689 
1690 void jtag_set_verify(bool enable)
1691 {
1692  jtag_verify = enable;
1693 }
1694 
1696 {
1697  return jtag_verify;
1698 }
1699 
1701 {
1702  jtag_verify_capture_ir = enable;
1703 }
1704 
1706 {
1707  return jtag_verify_capture_ir;
1708 }
1709 
1710 int jtag_power_dropout(int *dropout)
1711 {
1712  if (!is_adapter_initialized()) {
1713  /* TODO: as the jtag interface is not valid all
1714  * we can do at the moment is exit OpenOCD */
1715  LOG_ERROR("No Valid JTAG Interface Configured.");
1716  exit(-1);
1717  }
1719  return adapter_driver->power_dropout(dropout);
1720 
1721  *dropout = 0; /* by default we can't detect power dropout */
1722  return ERROR_OK;
1723 }
1724 
1726 {
1729 
1730  *srst_asserted = 0; /* by default we can't detect srst asserted */
1731  return ERROR_OK;
1732 }
1733 
1735 {
1736  return jtag_reset_config;
1737 }
1739 {
1741 }
1742 
1743 int jtag_get_trst(void)
1744 {
1745  return jtag_trst == 1;
1746 }
1747 int jtag_get_srst(void)
1748 {
1749  return jtag_srst == 1;
1750 }
1751 
1752 void jtag_set_nsrst_delay(unsigned delay)
1753 {
1754  adapter_nsrst_delay = delay;
1755 }
1756 unsigned jtag_get_nsrst_delay(void)
1757 {
1758  return adapter_nsrst_delay;
1759 }
1760 void jtag_set_ntrst_delay(unsigned delay)
1761 {
1762  jtag_ntrst_delay = delay;
1763 }
1764 unsigned jtag_get_ntrst_delay(void)
1765 {
1766  return jtag_ntrst_delay;
1767 }
1768 
1769 
1770 void jtag_set_nsrst_assert_width(unsigned delay)
1771 {
1773 }
1775 {
1777 }
1778 void jtag_set_ntrst_assert_width(unsigned delay)
1779 {
1780  jtag_ntrst_assert_width = delay;
1781 }
1783 {
1784  return jtag_ntrst_assert_width;
1785 }
1786 
1787 static int jtag_select(struct command_context *ctx)
1788 {
1789  int retval;
1790 
1791  /* NOTE: interface init must already have been done.
1792  * That works with only C code ... no Tcl glue required.
1793  */
1794 
1795  retval = jtag_register_commands(ctx);
1796 
1797  if (retval != ERROR_OK)
1798  return retval;
1799 
1800  retval = svf_register_commands(ctx);
1801 
1802  if (retval != ERROR_OK)
1803  return retval;
1804 
1805  retval = xsvf_register_commands(ctx);
1806 
1807  if (retval != ERROR_OK)
1808  return retval;
1809 
1810  return ipdbg_register_commands(ctx);
1811 }
1812 
1813 static struct transport jtag_transport = {
1814  .name = "jtag",
1815  .select = jtag_select,
1816  .init = jtag_init,
1817 };
1818 
1819 static void jtag_constructor(void) __attribute__((constructor));
1820 static void jtag_constructor(void)
1821 {
1823 }
1824 
1829 {
1831 }
1832 
1833 int adapter_resets(int trst, int srst)
1834 {
1835  if (!get_current_transport()) {
1836  LOG_ERROR("transport is not selected");
1837  return ERROR_FAIL;
1838  }
1839 
1840  if (transport_is_jtag()) {
1841  if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1842  LOG_ERROR("adapter has no srst signal");
1843  return ERROR_FAIL;
1844  }
1845 
1846  /* adapters without trst signal will eventually use tlr sequence */
1847  jtag_add_reset(trst, srst);
1848  /*
1849  * The jtag queue is still used for reset by some adapter. Flush it!
1850  * FIXME: To be removed when all adapter drivers will be updated!
1851  */
1853  return ERROR_OK;
1854  } else if (transport_is_swd() || transport_is_hla() ||
1856  transport_is_swim()) {
1857  if (trst == TRST_ASSERT) {
1858  LOG_ERROR("transport %s has no trst signal",
1860  return ERROR_FAIL;
1861  }
1862 
1863  if (srst == SRST_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
1864  LOG_ERROR("adapter has no srst signal");
1865  return ERROR_FAIL;
1866  }
1867  adapter_system_reset(srst);
1868  return ERROR_OK;
1869  }
1870 
1871  if (trst == TRST_DEASSERT && srst == SRST_DEASSERT)
1872  return ERROR_OK;
1873 
1874  LOG_ERROR("reset is not supported on transport %s",
1876 
1877  return ERROR_FAIL;
1878 }
1879 
1881 {
1882  if (transport_is_jtag()) {
1884  jtag_add_reset(1, 1);
1885  else
1886  jtag_add_reset(0, 1);
1887  return ERROR_OK;
1888  } else if (transport_is_swd() || transport_is_hla() ||
1891  return adapter_system_reset(1);
1892  else if (get_current_transport())
1893  LOG_ERROR("reset is not supported on %s",
1895  else
1896  LOG_ERROR("transport is not selected");
1897  return ERROR_FAIL;
1898 }
1899 
1901 {
1902  if (transport_is_jtag()) {
1903  jtag_add_reset(0, 0);
1904  return ERROR_OK;
1905  } else if (transport_is_swd() || transport_is_hla() ||
1908  return adapter_system_reset(0);
1909  else if (get_current_transport())
1910  LOG_ERROR("reset is not supported on %s",
1912  else
1913  LOG_ERROR("transport is not selected");
1914  return ERROR_FAIL;
1915 }
1916 
1917 int adapter_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
1918  uint32_t port_size, unsigned int *trace_freq,
1919  unsigned int traceclkin_freq, uint16_t *prescaler)
1920 {
1922  return adapter_driver->config_trace(enabled, pin_protocol, port_size, trace_freq,
1923  traceclkin_freq, prescaler);
1924  } else if (enabled) {
1925  LOG_ERROR("The selected interface does not support tracing");
1926  return ERROR_FAIL;
1927  }
1928 
1929  return ERROR_OK;
1930 }
1931 
1932 int adapter_poll_trace(uint8_t *buf, size_t *size)
1933 {
1935  return adapter_driver->poll_trace(buf, size);
1936 
1937  return ERROR_FAIL;
1938 }
bool is_adapter_initialized(void)
Definition: adapter.c:69
int adapter_init(struct command_context *cmd_ctx)
Do low-level setup like initializing registers, output signals, and clocking.
Definition: adapter.c:120
bool transport_is_dapdirect_swd(void)
Returns true if the current debug session is using SWD as its transport.
bool transport_is_dapdirect_jtag(void)
Returns true if the current debug session is using JTAG as its transport.
bool transport_is_swd(void)
Returns true if the current debug session is using SWD as its transport.
Definition: adi_v5_swd.c:776
tpiu_pin_protocol
Definition: arm_tpiu_swo.h:7
const char * name
Definition: armv4_5.c:76
char * buf_to_hex_str(const void *_buf, unsigned buf_len)
Definition: binarybuffer.c:192
bool buf_cmp(const void *_buf1, const void *_buf2, unsigned size)
Definition: binarybuffer.c:70
bool buf_cmp_mask(const void *_buf1, const void *_buf2, const void *_mask, unsigned size)
Definition: binarybuffer.c:87
void * buf_set_ones(void *_buf, unsigned size)
Set the contents of buf with count bits, all set to 1.
Definition: binarybuffer.c:106
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:99
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:31
static uint64_t buf_get_u64(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 64-bit word.
Definition: binarybuffer.h:128
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
struct jtag_command * jtag_command_queue_get(void)
Definition: commands.c:150
@ 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
int mask
Definition: esirisc.c:1741
uint8_t type
Definition: esp_usb_jtag.c:0
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
bool transport_is_hla(void)
bool tap_is_state_stable(tap_state_t astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
tap_state_t tap_state_transition(tap_state_t 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(tap_state_t 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(tap_state_t from, tap_state_t to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
int tap_get_tms_path_len(tap_state_t from, tap_state_t to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
#define DEBUG_CAP_TMS_SEQ
Definition: interface.h:187
#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:49
int ipdbg_register_commands(struct command_context *cmd_ctx)
Definition: ipdbg.c:1156
static const char * jep106_manufacturer(unsigned int manufacturer)
Definition: jep106.h:21
void jtag_set_error(int error)
Set the current JTAG core execution error, unless one was set by a previous call previously.
Definition: jtag/core.c:123
int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
Definition: jtag/core.c:303
void jtag_poll_unmask(bool saved)
Restore saved mask for polling.
Definition: jtag/core.c:177
static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active, int dummy, const struct scan_field *in_fields, tap_state_t state)
Definition: jtag/core.c:365
struct jtag_tap * jtag_tap_by_string(const char *s)
Definition: jtag/core.c:237
int jtag_init_inner(struct command_context *cmd_ctx)
Definition: jtag/core.c:1503
static void jtag_checks(void)
Definition: jtag/core.c:342
static int jtag_verify
Definition: jtag/core.c:91
void jtag_execute_queue_noclear(void)
same as jtag_execute_queue() but does not clear the error flag
Definition: jtag/core.c:1018
static void jtag_constructor(void)
Definition: jtag/core.c:1819
int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
If supported by the underlying adapter, this clocks a raw bit sequence onto TMS for switching between...
Definition: jtag/core.c:502
static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value, uint8_t *in_check_mask, int num_bits)
Definition: jtag/core.c:877
#define END_OF_CHAIN_FLAG
Definition: jtag/core.c:1079
static int jtag_validate_ircapture(void)
Definition: jtag/core.c:1331
int adapter_resets(int trst, int srst)
Definition: jtag/core.c:1833
void jtag_set_ntrst_assert_width(unsigned delay)
Definition: jtag/core.c:1778
static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
Definition: jtag/core.c:1084
#define EXTRACT_PART(X)
Definition: jtag/core.c:1073
static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
This helper checks that remaining bits in the examined chain data are all as expected,...
Definition: jtag/core.c:1161
int jtag_get_trst(void)
Definition: jtag/core.c:1743
void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
Scan out the bits in ir scan mode.
Definition: jtag/core.c:465
static int adapter_system_reset(int req_srst)
Definition: jtag/core.c:614
static int jtag_flush_queue_sleep
Definition: jtag/core.c:45
void jtag_add_pathmove(int num_states, const tap_state_t *path)
Application code must assume that interfaces will implement transitions between states with different...
Definition: jtag/core.c:517
void jtag_set_ntrst_delay(unsigned delay)
Definition: jtag/core.c:1760
void jtag_add_reset(int req_tlr_or_trst, int req_srst)
A reset of the TAP state machine can be requested.
Definition: jtag/core.c:758
void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
Scan out the bits in ir scan mode.
Definition: jtag/core.c:392
int jtag_init(struct command_context *cmd_ctx)
Initialize JTAG chain using only a RESET reset.
Definition: jtag/core.c:1664
void jtag_poll_set_enabled(bool value)
Assign flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:165
static const char * jtag_event_strings[]
Definition: jtag/core.c:66
static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
Definition: jtag/core.c:1101
int jtag_srst_asserted(int *srst_asserted)
Definition: jtag/core.c:1725
bool transport_is_jtag(void)
Returns true if the current debug session is using JTAG as its transport.
Definition: jtag/core.c:1828
static int jtag_select(struct command_context *ctx)
Definition: jtag/core.c:1787
static int jtag_ntrst_assert_width
Definition: jtag/core.c:98
void jtag_add_clocks(int num_cycles)
Function jtag_add_clocks first checks that the state in which the clocks are to be issued is stable,...
Definition: jtag/core.c:599
#define EXTRACT_MFG(X)
Definition: jtag/core.c:1072
int jtag_execute_queue(void)
For software FIFO implementations, the queued commands can be executed during this call or earlier.
Definition: jtag/core.c:1037
unsigned jtag_tap_count_enabled(void)
Definition: jtag/core.c:200
unsigned jtag_get_ntrst_delay(void)
Definition: jtag/core.c:1764
void jtag_set_flush_queue_sleep(int ms)
Set ms to sleep after jtag_execute_queue() flushes queue.
Definition: jtag/core.c:118
bool is_jtag_poll_safe(void)
Return true if it's safe for a background polling task to access the JTAG scan chain.
Definition: jtag/core.c:142
const char * jtag_tap_name(const struct jtag_tap *tap)
Definition: jtag/core.c:276
void jtag_add_dr_scan_check(struct jtag_tap *active, int in_num_fields, struct scan_field *in_fields, tap_state_t state)
A version of jtag_add_dr_scan() that uses the check_value/mask fields.
Definition: jtag/core.c:439
int adapter_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol, uint32_t port_size, unsigned int *trace_freq, unsigned int traceclkin_freq, uint16_t *prescaler)
Definition: jtag/core.c:1917
static int jtag_examine_chain(void)
Definition: jtag/core.c:1215
struct jtag_tap * jtag_all_taps(void)
Definition: jtag/core.c:184
int jtag_power_dropout(int *dropout)
Definition: jtag/core.c:1710
int adapter_poll_trace(uint8_t *buf, size_t *size)
Definition: jtag/core.c:1932
tap_state_t cmd_queue_cur_state
The current TAP state of the pending JTAG command queue.
Definition: jtag/core.c:88
struct jtag_tap * jtag_tap_by_position(unsigned n)
Definition: jtag/core.c:227
int jtag_add_statemove(tap_state_t goal_state)
jtag_add_statemove() moves from the current state to goal_state.
Definition: jtag/core.c:551
void jtag_set_nsrst_assert_width(unsigned delay)
Definition: jtag/core.c:1770
int swd_init_reset(struct command_context *cmd_ctx)
Definition: jtag/core.c:1583
static int adapter_nsrst_delay
Definition: jtag/core.c:95
static void legacy_jtag_add_reset(int req_tlr_or_trst, int req_srst)
Definition: jtag/core.c:649
int jtag_get_flush_queue_count(void)
Definition: jtag/core.c:1032
unsigned jtag_get_nsrst_delay(void)
Definition: jtag/core.c:1756
static struct transport jtag_transport
Definition: jtag/core.c:1813
int jtag_init_reset(struct command_context *cmd_ctx)
reset, then initialize JTAG chain
Definition: jtag/core.c:1600
int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
Definition: jtag/core.c:282
struct jtag_tap * jtag_tap_next_enabled(struct jtag_tap *p)
Definition: jtag/core.c:265
void jtag_add_tlr(void)
Run a TAP_RESET reset where the end state is TAP_RESET, regardless of the start state.
Definition: jtag/core.c:478
void jtag_set_nsrst_delay(unsigned delay)
Definition: jtag/core.c:1752
unsigned jtag_tap_count(void)
Definition: jtag/core.c:189
static struct jtag_tap * __jtag_all_taps
List all TAPs that have been created.
Definition: jtag/core.c:85
static bool jtag_poll
Definition: jtag/core.c:139
static enum reset_types jtag_reset_config
Definition: jtag/core.c:87
static void jtag_tap_add(struct jtag_tap *t)
Append a new TAP to the chain of all taps.
Definition: jtag/core.c:213
static bool jtag_poll_en
Definition: jtag/core.c:140
bool jtag_poll_get_enabled(void)
Return flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:160
bool jtag_will_verify(void)
Definition: jtag/core.c:1695
int jtag_call_event_callbacks(enum jtag_event event)
Definition: jtag/core.c:324
int jtag_get_srst(void)
Definition: jtag/core.c:1747
unsigned jtag_get_ntrst_assert_width(void)
Definition: jtag/core.c:1782
static int jtag_trst
Definition: jtag/core.c:79
void jtag_set_verify_capture_ir(bool enable)
Enable or disable verification of IR scan checking.
Definition: jtag/core.c:1700
static void jtag_examine_chain_display(enum log_levels level, const char *msg, const char *name, uint32_t idcode)
Definition: jtag/core.c:1130
void jtag_add_sleep(uint32_t us)
Definition: jtag/core.c:870
bool jtag_will_verify_capture_ir(void)
Definition: jtag/core.c:1705
static bool jtag_verify_capture_ir
Definition: jtag/core.c:90
void jtag_tap_free(struct jtag_tap *tap)
Definition: jtag/core.c:1481
static bool jtag_idcode_is_final(uint32_t idcode)
Definition: jtag/core.c:1144
static int jtag_reset_callback(enum jtag_event event, void *priv)
Definition: jtag/core.c:1043
static int jtag_flush_queue_count
The number of JTAG queue flushes (for profiling and debugging purposes).
Definition: jtag/core.c:42
int adapter_deassert_reset(void)
Definition: jtag/core.c:1900
static void jtag_prelude(tap_state_t state)
Definition: jtag/core.c:347
void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
Execute jtag queue and check value with an optional mask.
Definition: jtag/core.c:917
void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
Definition: jtag/core.c:374
static struct jtag_event_callback * jtag_event_callbacks
Definition: jtag/core.c:114
static int jtag_check_value_mask_callback(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
Definition: jtag/core.c:408
#define JTAG_MAX_AUTO_TAPS
Definition: jtag/core.c:1070
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1062
static int jtag_error
The jtag_error variable is set when an error occurs while executing the queue.
Definition: jtag/core.c:64
void jtag_add_runtest(int num_cycles, tap_state_t state)
Goes to TAP_IDLE (if we're not already there), cycle precisely num_cycles in the TAP_IDLE state,...
Definition: jtag/core.c:592
struct adapter_driver * adapter_driver
Definition: adapter.c:26
unsigned jtag_get_nsrst_assert_width(void)
Definition: jtag/core.c:1774
#define JTAG_IRLEN_MAX
Definition: jtag/core.c:1082
void jtag_tap_init(struct jtag_tap *tap)
Definition: jtag/core.c:1446
void jtag_set_reset_config(enum reset_types type)
Definition: jtag/core.c:1738
int jtag_error_clear(void)
Resets jtag_error to ERROR_OK, returning its previous value.
Definition: jtag/core.c:130
int default_interface_jtag_execute_queue(void)
Calls the interface callback to execute the queue.
Definition: jtag/core.c:932
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1734
void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields, tap_state_t state)
The same as jtag_add_ir_scan except no verification is performed out the output values.
Definition: jtag/core.c:356
static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
Definition: jtag/core.c:1177
int adapter_assert_reset(void)
Definition: jtag/core.c:1880
#define EXTRACT_VER(X)
Definition: jtag/core.c:1074
static int jtag_ntrst_delay
Definition: jtag/core.c:96
static int adapter_nsrst_assert_width
Definition: jtag/core.c:97
void jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
Generate a DR SCAN using the fields passed to the function.
Definition: jtag/core.c:451
static int jtag_srst
Definition: jtag/core.c:80
bool jtag_poll_mask(void)
Mask (disable) polling and return the current mask status that should be feed to jtag_poll_unmask() t...
Definition: jtag/core.c:170
void jtag_set_verify(bool enable)
Enable or disable data scan verification checking.
Definition: jtag/core.c:1690
static void jtag_add_scan_check(struct jtag_tap *active, void(*jtag_add_scan)(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state), int in_num_fields, struct scan_field *in_fields, tap_state_t state)
Definition: jtag/core.c:419
int interface_jtag_add_clocks(int num_cycles)
int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state state)
int interface_jtag_execute_queue(void)
int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
int interface_jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields, const struct scan_field *in_fields, tap_state_t state)
see jtag_add_dr_scan()
int interface_jtag_add_reset(int req_trst, int req_srst)
This drives the actual srst and trst pins.
int interface_jtag_add_ir_scan(struct jtag_tap *active, const struct scan_field *in_fields, tap_state_t state)
see jtag_add_ir_scan()
int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
int interface_jtag_add_sleep(uint32_t us)
int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
int interface_jtag_add_tlr(void)
The JTAG interface can be implemented with a software or hardware fifo.
int jtag_register_commands(struct command_context *cmd_ctx)
Definition: jtag/tcl.c:1208
int(* jtag_event_handler_t)(enum jtag_event event, void *priv)
Defines the function signature required for JTAG event callback functions, which are added with jtag_...
Definition: jtag.h:209
#define DEBUG_JTAG_IOZ
Definition: jtag.h:20
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_INVALID
Definition: jtag.h:38
#define ERROR_JTAG_NOT_STABLE_STATE
Definition: jtag.h:558
#define ERROR_JTAG_QUEUE_FAILED
Definition: jtag.h:557
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:553
#define TRST_DEASSERT
Definition: jtag.h:64
jtag_event
Definition: jtag.h:180
@ JTAG_TAP_EVENT_ENABLE
Definition: jtag.h:183
@ JTAG_TAP_EVENT_SETUP
Definition: jtag.h:182
@ JTAG_TRST_ASSERTED
Definition: jtag.h:181
@ JTAG_TAP_EVENT_DISABLE
Definition: jtag.h:184
#define ERROR_JTAG_TRANSITION_INVALID
Definition: jtag.h:561
#define ERROR_JTAG_INIT_SOFT_FAIL
Definition: jtag.h:562
#define TRST_ASSERT
Definition: jtag.h:65
reset_types
Definition: jtag.h:216
@ RESET_NONE
Definition: jtag.h:217
@ RESET_SRST_NO_GATING
Definition: jtag.h:225
@ RESET_HAS_SRST
Definition: jtag.h:219
@ RESET_HAS_TRST
Definition: jtag.h:218
@ RESET_TRST_PULLS_SRST
Definition: jtag.h:222
@ RESET_CNCT_UNDER_SRST
Definition: jtag.h:226
@ RESET_SRST_PULLS_TRST
Definition: jtag.h:221
enum tap_state tap_state_t
Defines JTAG Test Access Port states.
#define ERROR_JTAG_STATE_INVALID
Definition: jtag.h:560
#define ERROR_JTAG_NOT_IMPLEMENTED
Definition: jtag.h:555
#define SRST_ASSERT
Definition: jtag.h:63
void jtag_notify_event(enum jtag_event)
Report Tcl event to all TAPs.
Definition: jtag/tcl.c:779
#define SRST_DEASSERT
Defines arguments for reset functions.
Definition: jtag.h:62
intptr_t jtag_callback_data_t
Defines the type of data passed to the jtag_callback_t interface.
Definition: jtag.h:337
void log_printf_lf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format,...)
Definition: log.c:183
int debug_level
Definition: log.c:35
void alive_sleep(uint64_t ms)
Definition: log.c:456
void keep_alive(void)
Definition: log.c:415
char * alloc_printf(const char *format,...)
Definition: log.c:364
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:101
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
log_levels
Definition: log.h:40
@ LOG_LVL_INFO
Definition: log.h:46
@ LOG_LVL_DEBUG_IO
Definition: log.h:48
@ LOG_LVL_WARNING
Definition: log.h:45
@ LOG_LVL_ERROR
Definition: log.h:44
static uint32_t bit(uint32_t value, unsigned int b)
Definition: opcodes.h:15
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
Definition: qn908x.c:0
struct qn908x_flash_bank __attribute__
Definition: armv8.c:932
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
Represents a driver for a debugging interface.
Definition: interface.h:207
struct jtag_interface * jtag_ops
Low-level JTAG APIs.
Definition: interface.h:342
int(* config_trace)(bool enabled, enum tpiu_pin_protocol pin_protocol, uint32_t port_size, unsigned int *trace_freq, unsigned int traceclkin_freq, uint16_t *prescaler)
Configure trace parameters for the adapter.
Definition: interface.h:326
int(* reset)(int srst, int trst)
Control (assert/deassert) the signals SRST and TRST on the interface.
Definition: interface.h:255
int(* power_dropout)(int *power_dropout)
Read and clear the power dropout flag.
Definition: interface.h:296
int(* poll_trace)(uint8_t *buf, size_t *size)
Poll for new trace data.
Definition: interface.h:339
int(* srst_asserted)(int *srst_asserted)
Read and clear the srst asserted detection flag.
Definition: interface.h:310
Jim_Interp * interp
Definition: command.h:53
Contains a single callback along with a pointer that will be passed when an event occurs.
Definition: jtag/core.c:104
void * priv
the private data to pass to the callback
Definition: jtag/core.c:108
struct jtag_event_callback * next
the next callback
Definition: jtag/core.c:110
jtag_event_handler_t callback
a event callback
Definition: jtag/core.c:106
unsigned supported
Bit vector listing capabilities exposed by this driver.
Definition: interface.h:186
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:195
struct jtag_tap_event_action * next
Definition: jtag.h:195
Jim_Obj * body
Contains a script to 'eval' when the event is triggered.
Definition: jtag.h:193
Jim_Interp * interp
The interpreter to use for evaluating the body.
Definition: jtag.h:191
Definition: jtag.h:101
uint32_t ir_capture_value
Definition: jtag.h:111
int abs_chain_position
Definition: jtag.h:105
bool bypass
Bypass register selected.
Definition: jtag.h:134
uint8_t * expected_mask
Capture-IR expected mask.
Definition: jtag.h:114
char * chip
Definition: jtag.h:102
bool ignore_version
Flag saying whether to ignore version field in expected_ids[].
Definition: jtag.h:126
bool disabled_after_reset
Is this TAP disabled after JTAG reset?
Definition: jtag.h:107
struct jtag_tap_event_action * event_action
Definition: jtag.h:139
uint8_t * cur_instr
current instruction
Definition: jtag.h:132
int ir_length
size of instruction register
Definition: jtag.h:110
uint8_t * expected
Capture-IR expected value.
Definition: jtag.h:112
uint32_t ir_capture_mask
Definition: jtag.h:113
uint8_t expected_ids_cnt
Number of expected identification codes.
Definition: jtag.h:123
bool has_idcode
not all devices have idcode, we'll discover this during chain examination
Definition: jtag.h:118
char * tapname
Definition: jtag.h:103
bool ignore_bypass
Flag saying whether to ignore the bypass bit in the code.
Definition: jtag.h:129
bool enabled
Is this TAP currently enabled?
Definition: jtag.h:109
uint32_t * expected_ids
Array of expected identification codes.
Definition: jtag.h:121
struct jtag_tap * next_tap
Definition: jtag.h:141
uint32_t idcode
device identification code
Definition: jtag.h:115
char * dotted_name
Definition: jtag.h:104
This structure defines a single scan field in the scan.
Definition: jtag.h:87
int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
uint8_t * check_value
The value used to check the data scanned out.
Definition: jtag.h:96
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
uint8_t * check_mask
The mask to go with check_value.
Definition: jtag.h:98
Wrapper for transport lifecycle operations.
Definition: transport.h:35
const char * name
Each transport has a unique name, used to select it from among the alternatives.
Definition: transport.h:41
int svf_register_commands(struct command_context *cmd_ctx)
Definition: svf.c:1643
bool transport_is_swim(void)
Definition: swim.c:150
static int srst_asserted
Definition: target.c:2849
struct transport * get_current_transport(void)
Returns the transport currently being used by this debug or programming session.
Definition: transport.c:157
int transport_register(struct transport *new_transport)
Registers a transport.
Definition: transport.c:129
#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
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t dummy[96]
Definition: vdebug.c:23
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22
int xsvf_register_commands(struct command_context *cmd_ctx)
Definition: xsvf.c:1042