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