fd4370f4427438097d1ac6cee219991707d70e83
[openocd.git] / src / jtag / core.c
1 /***************************************************************************
2 * Copyright (C) 2009 Zachary T Welch *
3 * zw@superlucidity.net *
4 * *
5 * Copyright (C) 2007,2008,2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2005 by Dominic Rath *
13 * Dominic.Rath@gmx.de *
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
29 ***************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "jtag.h"
36 #include "swd.h"
37 #include "interface.h"
38 #include <transport/transport.h>
39 #include <helper/jep106.h>
40
41 #ifdef HAVE_STRINGS_H
42 #include <strings.h>
43 #endif
44
45 /* SVF and XSVF are higher level JTAG command sets (for boundary scan) */
46 #include "svf/svf.h"
47 #include "xsvf/xsvf.h"
48
49 /** The number of JTAG queue flushes (for profiling and debugging purposes). */
50 static int jtag_flush_queue_count;
51
52 /* Sleep this # of ms after flushing the queue */
53 static int jtag_flush_queue_sleep;
54
55 static void jtag_add_scan_check(struct jtag_tap *active,
56 void (*jtag_add_scan)(struct jtag_tap *active,
57 int in_num_fields,
58 const struct scan_field *in_fields,
59 tap_state_t state),
60 int in_num_fields, struct scan_field *in_fields, tap_state_t state);
61
62 /**
63 * The jtag_error variable is set when an error occurs while executing
64 * the queue. Application code may set this using jtag_set_error(),
65 * when an error occurs during processing that should be reported during
66 * jtag_execute_queue().
67 *
68 * The value is set and cleared, but never read by normal application code.
69 *
70 * This value is returned (and cleared) by jtag_execute_queue().
71 */
72 static int jtag_error = ERROR_OK;
73
74 static const char *jtag_event_strings[] = {
75 [JTAG_TRST_ASSERTED] = "TAP reset",
76 [JTAG_TAP_EVENT_SETUP] = "TAP setup",
77 [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
78 [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
79 };
80
81 /*
82 * JTAG adapters must initialize with TRST and SRST de-asserted
83 * (they're negative logic, so that means *high*). But some
84 * hardware doesn't necessarily work that way ... so set things
85 * up so that jtag_init() always forces that state.
86 */
87 static int jtag_trst = -1;
88 static int jtag_srst = -1;
89
90 /**
91 * List all TAPs that have been created.
92 */
93 static struct jtag_tap *__jtag_all_taps;
94
95 static enum reset_types jtag_reset_config = RESET_NONE;
96 tap_state_t cmd_queue_cur_state = TAP_RESET;
97
98 static bool jtag_verify_capture_ir = true;
99 static int jtag_verify = 1;
100
101 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines
102 *deasserted (in ms) */
103 static int adapter_nsrst_delay; /* default to no nSRST delay */
104 static int jtag_ntrst_delay;/* default to no nTRST delay */
105 static int adapter_nsrst_assert_width; /* width of assertion */
106 static int jtag_ntrst_assert_width; /* width of assertion */
107
108 /**
109 * Contains a single callback along with a pointer that will be passed
110 * when an event occurs.
111 */
112 struct jtag_event_callback {
113 /** a event callback */
114 jtag_event_handler_t callback;
115 /** the private data to pass to the callback */
116 void *priv;
117 /** the next callback */
118 struct jtag_event_callback *next;
119 };
120
121 /* callbacks to inform high-level handlers about JTAG state changes */
122 static struct jtag_event_callback *jtag_event_callbacks;
123
124 /* speed in kHz*/
125 static int speed_khz;
126 /* speed to fallback to when RCLK is requested but not supported */
127 static int rclk_fallback_speed_khz;
128 static enum {CLOCK_MODE_UNSELECTED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
129 static int jtag_speed;
130
131 static struct jtag_interface *jtag;
132
133 /* configuration */
134 struct jtag_interface *jtag_interface;
135
136 void jtag_set_flush_queue_sleep(int ms)
137 {
138 jtag_flush_queue_sleep = ms;
139 }
140
141 void jtag_set_error(int error)
142 {
143 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
144 return;
145 jtag_error = error;
146 }
147
148 int jtag_error_clear(void)
149 {
150 int temp = jtag_error;
151 jtag_error = ERROR_OK;
152 return temp;
153 }
154
155 /************/
156
157 static bool jtag_poll = 1;
158
159 bool is_jtag_poll_safe(void)
160 {
161 /* Polling can be disabled explicitly with set_enabled(false).
162 * It is also implicitly disabled while TRST is active and
163 * while SRST is gating the JTAG clock.
164 */
165 if (!transport_is_jtag())
166 return jtag_poll;
167
168 if (!jtag_poll || jtag_trst != 0)
169 return false;
170 return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
171 }
172
173 bool jtag_poll_get_enabled(void)
174 {
175 return jtag_poll;
176 }
177
178 void jtag_poll_set_enabled(bool value)
179 {
180 jtag_poll = value;
181 }
182
183 /************/
184
185 struct jtag_tap *jtag_all_taps(void)
186 {
187 return __jtag_all_taps;
188 };
189
190 unsigned jtag_tap_count(void)
191 {
192 struct jtag_tap *t = jtag_all_taps();
193 unsigned n = 0;
194 while (t) {
195 n++;
196 t = t->next_tap;
197 }
198 return n;
199 }
200
201 unsigned jtag_tap_count_enabled(void)
202 {
203 struct jtag_tap *t = jtag_all_taps();
204 unsigned n = 0;
205 while (t) {
206 if (t->enabled)
207 n++;
208 t = t->next_tap;
209 }
210 return n;
211 }
212
213 /** Append a new TAP to the chain of all taps. */
214 void jtag_tap_add(struct jtag_tap *t)
215 {
216 unsigned jtag_num_taps = 0;
217
218 struct jtag_tap **tap = &__jtag_all_taps;
219 while (*tap != NULL) {
220 jtag_num_taps++;
221 tap = &(*tap)->next_tap;
222 }
223 *tap = t;
224 t->abs_chain_position = jtag_num_taps;
225 }
226
227 /* returns a pointer to the n-th device in the scan chain */
228 struct jtag_tap *jtag_tap_by_position(unsigned n)
229 {
230 struct jtag_tap *t = jtag_all_taps();
231
232 while (t && n-- > 0)
233 t = t->next_tap;
234
235 return t;
236 }
237
238 struct jtag_tap *jtag_tap_by_string(const char *s)
239 {
240 /* try by name first */
241 struct jtag_tap *t = jtag_all_taps();
242
243 while (t) {
244 if (0 == strcmp(t->dotted_name, s))
245 return t;
246 t = t->next_tap;
247 }
248
249 /* no tap found by name, so try to parse the name as a number */
250 unsigned n;
251 if (parse_uint(s, &n) != ERROR_OK)
252 return NULL;
253
254 /* FIXME remove this numeric fallback code late June 2010, along
255 * with all info in the User's Guide that TAPs have numeric IDs.
256 * Also update "scan_chain" output to not display the numbers.
257 */
258 t = jtag_tap_by_position(n);
259 if (t)
260 LOG_WARNING("Specify TAP '%s' by name, not number %u",
261 t->dotted_name, n);
262
263 return t;
264 }
265
266 struct jtag_tap *jtag_tap_next_enabled(struct jtag_tap *p)
267 {
268 p = p ? p->next_tap : jtag_all_taps();
269 while (p) {
270 if (p->enabled)
271 return p;
272 p = p->next_tap;
273 }
274 return NULL;
275 }
276
277 const char *jtag_tap_name(const struct jtag_tap *tap)
278 {
279 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
280 }
281
282
283 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
284 {
285 struct jtag_event_callback **callbacks_p = &jtag_event_callbacks;
286
287 if (callback == NULL)
288 return ERROR_COMMAND_SYNTAX_ERROR;
289
290 if (*callbacks_p) {
291 while ((*callbacks_p)->next)
292 callbacks_p = &((*callbacks_p)->next);
293 callbacks_p = &((*callbacks_p)->next);
294 }
295
296 (*callbacks_p) = malloc(sizeof(struct jtag_event_callback));
297 (*callbacks_p)->callback = callback;
298 (*callbacks_p)->priv = priv;
299 (*callbacks_p)->next = NULL;
300
301 return ERROR_OK;
302 }
303
304 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
305 {
306 struct jtag_event_callback **p = &jtag_event_callbacks, *temp;
307
308 if (callback == NULL)
309 return ERROR_COMMAND_SYNTAX_ERROR;
310
311 while (*p) {
312 if (((*p)->priv != priv) || ((*p)->callback != callback)) {
313 p = &(*p)->next;
314 continue;
315 }
316
317 temp = *p;
318 *p = (*p)->next;
319 free(temp);
320 }
321
322 return ERROR_OK;
323 }
324
325 int jtag_call_event_callbacks(enum jtag_event event)
326 {
327 struct jtag_event_callback *callback = jtag_event_callbacks;
328
329 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
330
331 while (callback) {
332 struct jtag_event_callback *next;
333
334 /* callback may remove itself */
335 next = callback->next;
336 callback->callback(event, callback->priv);
337 callback = next;
338 }
339
340 return ERROR_OK;
341 }
342
343 static void jtag_checks(void)
344 {
345 assert(jtag_trst == 0);
346 }
347
348 static void jtag_prelude(tap_state_t state)
349 {
350 jtag_checks();
351
352 assert(state != TAP_INVALID);
353
354 cmd_queue_cur_state = state;
355 }
356
357 void jtag_add_ir_scan_noverify(struct jtag_tap *active, const struct scan_field *in_fields,
358 tap_state_t state)
359 {
360 jtag_prelude(state);
361
362 int retval = interface_jtag_add_ir_scan(active, in_fields, state);
363 jtag_set_error(retval);
364 }
365
366 static void jtag_add_ir_scan_noverify_callback(struct jtag_tap *active,
367 int dummy,
368 const struct scan_field *in_fields,
369 tap_state_t state)
370 {
371 jtag_add_ir_scan_noverify(active, in_fields, state);
372 }
373
374 /* If fields->in_value is filled out, then the captured IR value will be checked */
375 void jtag_add_ir_scan(struct jtag_tap *active, struct scan_field *in_fields, tap_state_t state)
376 {
377 assert(state != TAP_RESET);
378
379 if (jtag_verify && jtag_verify_capture_ir) {
380 /* 8 x 32 bit id's is enough for all invocations */
381
382 /* if we are to run a verification of the ir scan, we need to get the input back.
383 * We may have to allocate space if the caller didn't ask for the input back.
384 */
385 in_fields->check_value = active->expected;
386 in_fields->check_mask = active->expected_mask;
387 jtag_add_scan_check(active, jtag_add_ir_scan_noverify_callback, 1, in_fields,
388 state);
389 } else
390 jtag_add_ir_scan_noverify(active, in_fields, state);
391 }
392
393 void jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
394 tap_state_t state)
395 {
396 assert(out_bits != NULL);
397 assert(state != TAP_RESET);
398
399 jtag_prelude(state);
400
401 int retval = interface_jtag_add_plain_ir_scan(
402 num_bits, out_bits, in_bits, state);
403 jtag_set_error(retval);
404 }
405
406 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
407 uint8_t *in_check_mask, int num_bits);
408
409 static int jtag_check_value_mask_callback(jtag_callback_data_t data0,
410 jtag_callback_data_t data1,
411 jtag_callback_data_t data2,
412 jtag_callback_data_t data3)
413 {
414 return jtag_check_value_inner((uint8_t *)data0,
415 (uint8_t *)data1,
416 (uint8_t *)data2,
417 (int)data3);
418 }
419
420 static void jtag_add_scan_check(struct jtag_tap *active, void (*jtag_add_scan)(
421 struct jtag_tap *active,
422 int in_num_fields,
423 const struct scan_field *in_fields,
424 tap_state_t state),
425 int in_num_fields, struct scan_field *in_fields, tap_state_t state)
426 {
427 jtag_add_scan(active, in_num_fields, in_fields, state);
428
429 for (int i = 0; i < in_num_fields; i++) {
430 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL)) {
431 /* this is synchronous for a minidriver */
432 jtag_add_callback4(jtag_check_value_mask_callback,
433 (jtag_callback_data_t)in_fields[i].in_value,
434 (jtag_callback_data_t)in_fields[i].check_value,
435 (jtag_callback_data_t)in_fields[i].check_mask,
436 (jtag_callback_data_t)in_fields[i].num_bits);
437 }
438 }
439 }
440
441 void jtag_add_dr_scan_check(struct jtag_tap *active,
442 int in_num_fields,
443 struct scan_field *in_fields,
444 tap_state_t state)
445 {
446 if (jtag_verify)
447 jtag_add_scan_check(active, jtag_add_dr_scan, in_num_fields, in_fields, state);
448 else
449 jtag_add_dr_scan(active, in_num_fields, in_fields, state);
450 }
451
452
453 void jtag_add_dr_scan(struct jtag_tap *active,
454 int in_num_fields,
455 const struct scan_field *in_fields,
456 tap_state_t state)
457 {
458 assert(state != TAP_RESET);
459
460 jtag_prelude(state);
461
462 int retval;
463 retval = interface_jtag_add_dr_scan(active, in_num_fields, in_fields, state);
464 jtag_set_error(retval);
465 }
466
467 void jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits,
468 tap_state_t state)
469 {
470 assert(out_bits != NULL);
471 assert(state != TAP_RESET);
472
473 jtag_prelude(state);
474
475 int retval;
476 retval = interface_jtag_add_plain_dr_scan(num_bits, out_bits, in_bits, state);
477 jtag_set_error(retval);
478 }
479
480 void jtag_add_tlr(void)
481 {
482 jtag_prelude(TAP_RESET);
483 jtag_set_error(interface_jtag_add_tlr());
484
485 /* NOTE: order here matches TRST path in jtag_add_reset() */
486 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
487 jtag_notify_event(JTAG_TRST_ASSERTED);
488 }
489
490 /**
491 * If supported by the underlying adapter, this clocks a raw bit sequence
492 * onto TMS for switching betwen JTAG and SWD modes.
493 *
494 * DO NOT use this to bypass the integrity checks and logging provided
495 * by the jtag_add_pathmove() and jtag_add_statemove() calls.
496 *
497 * @param nbits How many bits to clock out.
498 * @param seq The bit sequence. The LSB is bit 0 of seq[0].
499 * @param state The JTAG tap state to record on completion. Use
500 * TAP_INVALID to represent being in in SWD mode.
501 *
502 * @todo Update naming conventions to stop assuming everything is JTAG.
503 */
504 int jtag_add_tms_seq(unsigned nbits, const uint8_t *seq, enum tap_state state)
505 {
506 int retval;
507
508 if (!(jtag->supported & DEBUG_CAP_TMS_SEQ))
509 return ERROR_JTAG_NOT_IMPLEMENTED;
510
511 jtag_checks();
512 cmd_queue_cur_state = state;
513
514 retval = interface_add_tms_seq(nbits, seq, state);
515 jtag_set_error(retval);
516 return retval;
517 }
518
519 void jtag_add_pathmove(int num_states, const tap_state_t *path)
520 {
521 tap_state_t cur_state = cmd_queue_cur_state;
522
523 /* the last state has to be a stable state */
524 if (!tap_is_state_stable(path[num_states - 1])) {
525 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
526 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
527 return;
528 }
529
530 for (int i = 0; i < num_states; i++) {
531 if (path[i] == TAP_RESET) {
532 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
533 jtag_set_error(ERROR_JTAG_STATE_INVALID);
534 return;
535 }
536
537 if (tap_state_transition(cur_state, true) != path[i] &&
538 tap_state_transition(cur_state, false) != path[i]) {
539 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
540 tap_state_name(cur_state), tap_state_name(path[i]));
541 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
542 return;
543 }
544 cur_state = path[i];
545 }
546
547 jtag_checks();
548
549 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
550 cmd_queue_cur_state = path[num_states - 1];
551 }
552
553 int jtag_add_statemove(tap_state_t goal_state)
554 {
555 tap_state_t cur_state = cmd_queue_cur_state;
556
557 if (goal_state != cur_state) {
558 LOG_DEBUG("cur_state=%s goal_state=%s",
559 tap_state_name(cur_state),
560 tap_state_name(goal_state));
561 }
562
563 /* If goal is RESET, be paranoid and force that that transition
564 * (e.g. five TCK cycles, TMS high). Else trust "cur_state".
565 */
566 if (goal_state == TAP_RESET)
567 jtag_add_tlr();
568 else if (goal_state == cur_state)
569 /* nothing to do */;
570
571 else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state)) {
572 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
573 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
574 tap_state_t moves[8];
575 assert(tms_count < ARRAY_SIZE(moves));
576
577 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1) {
578 bool bit = tms_bits & 1;
579
580 cur_state = tap_state_transition(cur_state, bit);
581 moves[i] = cur_state;
582 }
583
584 jtag_add_pathmove(tms_count, moves);
585 } else if (tap_state_transition(cur_state, true) == goal_state
586 || tap_state_transition(cur_state, false) == goal_state)
587 jtag_add_pathmove(1, &goal_state);
588 else
589 return ERROR_FAIL;
590
591 return ERROR_OK;
592 }
593
594 void jtag_add_runtest(int num_cycles, tap_state_t state)
595 {
596 jtag_prelude(state);
597 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
598 }
599
600
601 void jtag_add_clocks(int num_cycles)
602 {
603 if (!tap_is_state_stable(cmd_queue_cur_state)) {
604 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
605 tap_state_name(cmd_queue_cur_state));
606 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
607 return;
608 }
609
610 if (num_cycles > 0) {
611 jtag_checks();
612 jtag_set_error(interface_jtag_add_clocks(num_cycles));
613 }
614 }
615
616 void swd_add_reset(int req_srst)
617 {
618 if (req_srst) {
619 if (!(jtag_reset_config & RESET_HAS_SRST)) {
620 LOG_ERROR("BUG: can't assert SRST");
621 jtag_set_error(ERROR_FAIL);
622 return;
623 }
624 req_srst = 1;
625 }
626
627 /* Maybe change SRST signal state */
628 if (jtag_srst != req_srst) {
629 int retval;
630
631 retval = interface_jtag_add_reset(0, req_srst);
632 if (retval != ERROR_OK)
633 jtag_set_error(retval);
634 else
635 retval = jtag_execute_queue();
636
637 if (retval != ERROR_OK) {
638 LOG_ERROR("TRST/SRST error");
639 return;
640 }
641
642 /* SRST resets everything hooked up to that signal */
643 jtag_srst = req_srst;
644 if (jtag_srst) {
645 LOG_DEBUG("SRST line asserted");
646 if (adapter_nsrst_assert_width)
647 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
648 } else {
649 LOG_DEBUG("SRST line released");
650 if (adapter_nsrst_delay)
651 jtag_add_sleep(adapter_nsrst_delay * 1000);
652 }
653
654 retval = jtag_execute_queue();
655 if (retval != ERROR_OK) {
656 LOG_ERROR("SRST timings error");
657 return;
658 }
659 }
660 }
661
662 void 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) {
676 if (!(jtag_reset_config & RESET_HAS_SRST)) {
677 LOG_ERROR("BUG: can't assert SRST");
678 jtag_set_error(ERROR_FAIL);
679 return;
680 }
681 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
682 && !req_tlr_or_trst) {
683 LOG_ERROR("BUG: can't assert only SRST");
684 jtag_set_error(ERROR_FAIL);
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) {
698 if (!(jtag_reset_config & RESET_HAS_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");
728 if (adapter_nsrst_assert_width)
729 jtag_add_sleep(adapter_nsrst_assert_width * 1000);
730 } else {
731 LOG_DEBUG("SRST line released");
732 if (adapter_nsrst_delay)
733 jtag_add_sleep(adapter_nsrst_delay * 1000);
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");
751 tap_set_state(TAP_RESET);
752 if (jtag_ntrst_assert_width)
753 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
754 } else {
755 LOG_DEBUG("TRST line released");
756 if (jtag_ntrst_delay)
757 jtag_add_sleep(jtag_ntrst_delay * 1000);
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 */
764 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
765 jtag_notify_event(JTAG_TRST_ASSERTED);
766 }
767 }
768 }
769
770 void jtag_add_sleep(uint32_t us)
771 {
772 /** @todo Here, keep_alive() appears to be a layering violation!!! */
773 keep_alive();
774 jtag_set_error(interface_jtag_add_sleep(us));
775 }
776
777 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
778 uint8_t *in_check_mask, int num_bits)
779 {
780 int retval = ERROR_OK;
781 int compare_failed;
782
783 if (in_check_mask)
784 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
785 else
786 compare_failed = buf_cmp(captured, in_check_value, num_bits);
787
788 if (compare_failed) {
789 char *captured_str, *in_check_value_str;
790 int bits = (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits;
791
792 /* NOTE: we've lost diagnostic context here -- 'which tap' */
793
794 captured_str = buf_to_str(captured, bits, 16);
795 in_check_value_str = buf_to_str(in_check_value, bits, 16);
796
797 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
798 captured_str);
799 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
800
801 free(captured_str);
802 free(in_check_value_str);
803
804 if (in_check_mask) {
805 char *in_check_mask_str;
806
807 in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
808 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
809 free(in_check_mask_str);
810 }
811
812 retval = ERROR_JTAG_QUEUE_FAILED;
813 }
814 return retval;
815 }
816
817 void jtag_check_value_mask(struct scan_field *field, uint8_t *value, uint8_t *mask)
818 {
819 assert(field->in_value != NULL);
820
821 if (value == NULL) {
822 /* no checking to do */
823 return;
824 }
825
826 jtag_execute_queue_noclear();
827
828 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
829 jtag_set_error(retval);
830 }
831
832 int default_interface_jtag_execute_queue(void)
833 {
834 if (NULL == jtag) {
835 LOG_ERROR("No JTAG interface configured yet. "
836 "Issue 'init' command in startup scripts "
837 "before communicating with targets.");
838 return ERROR_FAIL;
839 }
840
841 return jtag->execute_queue();
842 }
843
844 void jtag_execute_queue_noclear(void)
845 {
846 jtag_flush_queue_count++;
847 jtag_set_error(interface_jtag_execute_queue());
848
849 if (jtag_flush_queue_sleep > 0) {
850 /* For debug purposes it can be useful to test performance
851 * or behavior when delaying after flushing the queue,
852 * e.g. to simulate long roundtrip times.
853 */
854 usleep(jtag_flush_queue_sleep * 1000);
855 }
856 }
857
858 int jtag_get_flush_queue_count(void)
859 {
860 return jtag_flush_queue_count;
861 }
862
863 int jtag_execute_queue(void)
864 {
865 jtag_execute_queue_noclear();
866 return jtag_error_clear();
867 }
868
869 static int jtag_reset_callback(enum jtag_event event, void *priv)
870 {
871 struct jtag_tap *tap = priv;
872
873 if (event == JTAG_TRST_ASSERTED) {
874 tap->enabled = !tap->disabled_after_reset;
875
876 /* current instruction is either BYPASS or IDCODE */
877 buf_set_ones(tap->cur_instr, tap->ir_length);
878 tap->bypass = 1;
879 }
880
881 return ERROR_OK;
882 }
883
884 /* sleep at least us microseconds. When we sleep more than 1000ms we
885 * do an alive sleep, i.e. keep GDB alive. Note that we could starve
886 * GDB if we slept for <1000ms many times.
887 */
888 void jtag_sleep(uint32_t us)
889 {
890 if (us < 1000)
891 usleep(us);
892 else
893 alive_sleep((us+999)/1000);
894 }
895
896 #define JTAG_MAX_AUTO_TAPS 20
897
898 #define EXTRACT_JEP106_BANK(X) (((X) & 0xf00) >> 8)
899 #define EXTRACT_JEP106_ID(X) (((X) & 0xfe) >> 1)
900 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
901 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
902 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
903
904 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
905 * know that no valid TAP will have it as an IDCODE value.
906 */
907 #define END_OF_CHAIN_FLAG 0xffffffff
908
909 /* a larger IR length than we ever expect to autoprobe */
910 #define JTAG_IRLEN_MAX 60
911
912 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
913 {
914 struct scan_field field = {
915 .num_bits = num_idcode * 32,
916 .out_value = idcode_buffer,
917 .in_value = idcode_buffer,
918 };
919
920 /* initialize to the end of chain ID value */
921 for (unsigned i = 0; i < num_idcode; i++)
922 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
923
924 jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, TAP_DRPAUSE);
925 jtag_add_tlr();
926 return jtag_execute_queue();
927 }
928
929 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
930 {
931 uint8_t zero_check = 0x0;
932 uint8_t one_check = 0xff;
933
934 for (unsigned i = 0; i < count * 4; i++) {
935 zero_check |= idcodes[i];
936 one_check &= idcodes[i];
937 }
938
939 /* if there wasn't a single non-zero bit or if all bits were one,
940 * the scan is not valid. We wrote a mix of both values; either
941 *
942 * - There's a hardware issue (almost certainly):
943 * + all-zeroes can mean a target stuck in JTAG reset
944 * + all-ones tends to mean no target
945 * - The scan chain is WAY longer than we can handle, *AND* either
946 * + there are several hundreds of TAPs in bypass, or
947 * + at least a few dozen TAPs all have an all-ones IDCODE
948 */
949 if (zero_check == 0x00 || one_check == 0xff) {
950 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
951 (zero_check == 0x00) ? "zeroes" : "ones");
952 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
953 return false;
954 }
955 return true;
956 }
957
958 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
959 const char *name, uint32_t idcode)
960 {
961 log_printf_lf(level, __FILE__, __LINE__, __func__,
962 "JTAG tap: %s %16.16s: 0x%08x "
963 "(mfg: 0x%3.3x (%s), part: 0x%4.4x, ver: 0x%1.1x)",
964 name, msg,
965 (unsigned int)idcode,
966 (unsigned int)EXTRACT_MFG(idcode),
967 jep106_manufacturer(EXTRACT_JEP106_BANK(idcode), EXTRACT_JEP106_ID(idcode)),
968 (unsigned int)EXTRACT_PART(idcode),
969 (unsigned int)EXTRACT_VER(idcode));
970 }
971
972 static bool jtag_idcode_is_final(uint32_t idcode)
973 {
974 /*
975 * Some devices, such as AVR8, will output all 1's instead
976 * of TDI input value at end of chain. Allow those values
977 * instead of failing.
978 */
979 return idcode == END_OF_CHAIN_FLAG;
980 }
981
982 /**
983 * This helper checks that remaining bits in the examined chain data are
984 * all as expected, but a single JTAG device requires only 64 bits to be
985 * read back correctly. This can help identify and diagnose problems
986 * with the JTAG chain earlier, gives more helpful/explicit error messages.
987 * Returns TRUE iff garbage was found.
988 */
989 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
990 {
991 bool triggered = false;
992 for (; count < max - 31; count += 32) {
993 uint32_t idcode = buf_get_u32(idcodes, count, 32);
994
995 /* do not trigger the warning if the data looks good */
996 if (jtag_idcode_is_final(idcode))
997 continue;
998 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
999 count, (unsigned int)idcode);
1000 triggered = true;
1001 }
1002 return triggered;
1003 }
1004
1005 static bool jtag_examine_chain_match_tap(const struct jtag_tap *tap)
1006 {
1007
1008 if (tap->expected_ids_cnt == 0 || !tap->hasidcode)
1009 return true;
1010
1011 /* optionally ignore the JTAG version field - bits 28-31 of IDCODE */
1012 uint32_t mask = tap->ignore_version ? ~(0xf << 28) : ~0;
1013 uint32_t idcode = tap->idcode & mask;
1014
1015 /* Loop over the expected identification codes and test for a match */
1016 for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1017 uint32_t expected = tap->expected_ids[ii] & mask;
1018
1019 if (idcode == expected)
1020 return true;
1021
1022 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1023 if (0 == tap->expected_ids[ii])
1024 return true;
1025 }
1026
1027 /* If none of the expected ids matched, warn */
1028 jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
1029 tap->dotted_name, tap->idcode);
1030 for (unsigned ii = 0; ii < tap->expected_ids_cnt; ii++) {
1031 char msg[32];
1032
1033 snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, tap->expected_ids_cnt);
1034 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1035 tap->dotted_name, tap->expected_ids[ii]);
1036 }
1037 return false;
1038 }
1039
1040 /* Try to examine chain layout according to IEEE 1149.1 §12
1041 * This is called a "blind interrogation" of the scan chain.
1042 */
1043 static int jtag_examine_chain(void)
1044 {
1045 int retval;
1046 unsigned max_taps = jtag_tap_count();
1047
1048 /* Autoprobe up to this many. */
1049 if (max_taps < JTAG_MAX_AUTO_TAPS)
1050 max_taps = JTAG_MAX_AUTO_TAPS;
1051
1052 /* Add room for end-of-chain marker. */
1053 max_taps++;
1054
1055 uint8_t *idcode_buffer = malloc(max_taps * 4);
1056 if (idcode_buffer == NULL)
1057 return ERROR_JTAG_INIT_FAILED;
1058
1059 /* DR scan to collect BYPASS or IDCODE register contents.
1060 * Then make sure the scan data has both ones and zeroes.
1061 */
1062 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1063 retval = jtag_examine_chain_execute(idcode_buffer, max_taps);
1064 if (retval != ERROR_OK)
1065 goto out;
1066 if (!jtag_examine_chain_check(idcode_buffer, max_taps)) {
1067 retval = ERROR_JTAG_INIT_FAILED;
1068 goto out;
1069 }
1070
1071 /* Point at the 1st predefined tap, if any */
1072 struct jtag_tap *tap = jtag_tap_next_enabled(NULL);
1073
1074 unsigned bit_count = 0;
1075 unsigned autocount = 0;
1076 for (unsigned i = 0; i < max_taps; i++) {
1077 assert(bit_count < max_taps * 32);
1078 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1079
1080 /* No predefined TAP? Auto-probe. */
1081 if (tap == NULL) {
1082 /* Is there another TAP? */
1083 if (jtag_idcode_is_final(idcode))
1084 break;
1085
1086 /* Default everything in this TAP except IR length.
1087 *
1088 * REVISIT create a jtag_alloc(chip, tap) routine, and
1089 * share it with jim_newtap_cmd().
1090 */
1091 tap = calloc(1, sizeof *tap);
1092 if (!tap) {
1093 retval = ERROR_FAIL;
1094 goto out;
1095 }
1096
1097 tap->chip = alloc_printf("auto%u", autocount++);
1098 tap->tapname = strdup("tap");
1099 tap->dotted_name = alloc_printf("%s.%s", tap->chip, tap->tapname);
1100
1101 tap->ir_length = 0; /* ... signifying irlen autoprobe */
1102 tap->ir_capture_mask = 0x03;
1103 tap->ir_capture_value = 0x01;
1104
1105 tap->enabled = true;
1106
1107 jtag_tap_init(tap);
1108 }
1109
1110 if ((idcode & 1) == 0) {
1111 /* Zero for LSB indicates a device in bypass */
1112 LOG_INFO("TAP %s does not have IDCODE", tap->dotted_name);
1113 tap->hasidcode = false;
1114 tap->idcode = 0;
1115
1116 bit_count += 1;
1117 } else {
1118 /* Friendly devices support IDCODE */
1119 tap->hasidcode = true;
1120 tap->idcode = idcode;
1121 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found", tap->dotted_name, idcode);
1122
1123 bit_count += 32;
1124 }
1125
1126 /* ensure the TAP ID matches what was expected */
1127 if (!jtag_examine_chain_match_tap(tap))
1128 retval = ERROR_JTAG_INIT_SOFT_FAIL;
1129
1130 tap = jtag_tap_next_enabled(tap);
1131 }
1132
1133 /* After those IDCODE or BYPASS register values should be
1134 * only the data we fed into the scan chain.
1135 */
1136 if (jtag_examine_chain_end(idcode_buffer, bit_count, max_taps * 32)) {
1137 LOG_ERROR("double-check your JTAG setup (interface, speed, ...)");
1138 retval = ERROR_JTAG_INIT_FAILED;
1139 goto out;
1140 }
1141
1142 /* Return success or, for backwards compatibility if only
1143 * some IDCODE values mismatched, a soft/continuable fault.
1144 */
1145 out:
1146 free(idcode_buffer);
1147 return retval;
1148 }
1149
1150 /*
1151 * Validate the date loaded by entry to the Capture-IR state, to help
1152 * find errors related to scan chain configuration (wrong IR lengths)
1153 * or communication.
1154 *
1155 * Entry state can be anything. On non-error exit, all TAPs are in
1156 * bypass mode. On error exits, the scan chain is reset.
1157 */
1158 static int jtag_validate_ircapture(void)
1159 {
1160 struct jtag_tap *tap;
1161 int total_ir_length = 0;
1162 uint8_t *ir_test = NULL;
1163 struct scan_field field;
1164 uint64_t val;
1165 int chain_pos = 0;
1166 int retval;
1167
1168 /* when autoprobing, accomodate huge IR lengths */
1169 for (tap = NULL, total_ir_length = 0;
1170 (tap = jtag_tap_next_enabled(tap)) != NULL;
1171 total_ir_length += tap->ir_length) {
1172 if (tap->ir_length == 0)
1173 total_ir_length += JTAG_IRLEN_MAX;
1174 }
1175
1176 /* increase length to add 2 bit sentinel after scan */
1177 total_ir_length += 2;
1178
1179 ir_test = malloc(DIV_ROUND_UP(total_ir_length, 8));
1180 if (ir_test == NULL)
1181 return ERROR_FAIL;
1182
1183 /* after this scan, all TAPs will capture BYPASS instructions */
1184 buf_set_ones(ir_test, total_ir_length);
1185
1186 field.num_bits = total_ir_length;
1187 field.out_value = ir_test;
1188 field.in_value = ir_test;
1189
1190 jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value, TAP_IDLE);
1191
1192 LOG_DEBUG("IR capture validation scan");
1193 retval = jtag_execute_queue();
1194 if (retval != ERROR_OK)
1195 goto done;
1196
1197 tap = NULL;
1198 chain_pos = 0;
1199
1200 for (;; ) {
1201 tap = jtag_tap_next_enabled(tap);
1202 if (tap == NULL)
1203 break;
1204
1205 /* If we're autoprobing, guess IR lengths. They must be at
1206 * least two bits. Guessing will fail if (a) any TAP does
1207 * not conform to the JTAG spec; or (b) when the upper bits
1208 * captured from some conforming TAP are nonzero. Or if
1209 * (c) an IR length is longer than JTAG_IRLEN_MAX bits,
1210 * an implementation limit, which could someday be raised.
1211 *
1212 * REVISIT optimization: if there's a *single* TAP we can
1213 * lift restrictions (a) and (b) by scanning a recognizable
1214 * pattern before the all-ones BYPASS. Check for where the
1215 * pattern starts in the result, instead of an 0...01 value.
1216 *
1217 * REVISIT alternative approach: escape to some tcl code
1218 * which could provide more knowledge, based on IDCODE; and
1219 * only guess when that has no success.
1220 */
1221 if (tap->ir_length == 0) {
1222 tap->ir_length = 2;
1223 while ((val = buf_get_u64(ir_test, chain_pos, tap->ir_length + 1)) == 1
1224 && tap->ir_length < JTAG_IRLEN_MAX) {
1225 tap->ir_length++;
1226 }
1227 LOG_WARNING("AUTO %s - use \"jtag newtap " "%s %s -irlen %d "
1228 "-expected-id 0x%08" PRIx32 "\"",
1229 tap->dotted_name, tap->chip, tap->tapname, tap->ir_length, tap->idcode);
1230 }
1231
1232 /* Validate the two LSBs, which must be 01 per JTAG spec.
1233 *
1234 * Or ... more bits could be provided by TAP declaration.
1235 * Plus, some taps (notably in i.MX series chips) violate
1236 * this part of the JTAG spec, so their capture mask/value
1237 * attributes might disable this test.
1238 */
1239 val = buf_get_u64(ir_test, chain_pos, tap->ir_length);
1240 if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
1241 LOG_ERROR("%s: IR capture error; saw 0x%0*" PRIx64 " not 0x%0*" PRIx32,
1242 jtag_tap_name(tap),
1243 (tap->ir_length + 7) / tap->ir_length, val,
1244 (tap->ir_length + 7) / tap->ir_length, tap->ir_capture_value);
1245
1246 retval = ERROR_JTAG_INIT_FAILED;
1247 goto done;
1248 }
1249 LOG_DEBUG("%s: IR capture 0x%0*" PRIx64, jtag_tap_name(tap),
1250 (tap->ir_length + 7) / tap->ir_length, val);
1251 chain_pos += tap->ir_length;
1252 }
1253
1254 /* verify the '11' sentinel we wrote is returned at the end */
1255 val = buf_get_u64(ir_test, chain_pos, 2);
1256 if (val != 0x3) {
1257 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1258
1259 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1260 chain_pos, cbuf);
1261 free(cbuf);
1262 retval = ERROR_JTAG_INIT_FAILED;
1263 }
1264
1265 done:
1266 free(ir_test);
1267 if (retval != ERROR_OK) {
1268 jtag_add_tlr();
1269 jtag_execute_queue();
1270 }
1271 return retval;
1272 }
1273
1274 void jtag_tap_init(struct jtag_tap *tap)
1275 {
1276 unsigned ir_len_bits;
1277 unsigned ir_len_bytes;
1278
1279 /* if we're autoprobing, cope with potentially huge ir_length */
1280 ir_len_bits = tap->ir_length ? : JTAG_IRLEN_MAX;
1281 ir_len_bytes = DIV_ROUND_UP(ir_len_bits, 8);
1282
1283 tap->expected = calloc(1, ir_len_bytes);
1284 tap->expected_mask = calloc(1, ir_len_bytes);
1285 tap->cur_instr = malloc(ir_len_bytes);
1286
1287 /** @todo cope better with ir_length bigger than 32 bits */
1288 if (ir_len_bits > 32)
1289 ir_len_bits = 32;
1290
1291 buf_set_u32(tap->expected, 0, ir_len_bits, tap->ir_capture_value);
1292 buf_set_u32(tap->expected_mask, 0, ir_len_bits, tap->ir_capture_mask);
1293
1294 /* TAP will be in bypass mode after jtag_validate_ircapture() */
1295 tap->bypass = 1;
1296 buf_set_ones(tap->cur_instr, tap->ir_length);
1297
1298 /* register the reset callback for the TAP */
1299 jtag_register_event_callback(&jtag_reset_callback, tap);
1300 jtag_tap_add(tap);
1301
1302 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1303 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1304 tap->abs_chain_position, tap->ir_length,
1305 (unsigned) tap->ir_capture_value,
1306 (unsigned) tap->ir_capture_mask);
1307 }
1308
1309 void jtag_tap_free(struct jtag_tap *tap)
1310 {
1311 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1312
1313 free(tap->expected);
1314 free(tap->expected_mask);
1315 free(tap->expected_ids);
1316 free(tap->cur_instr);
1317 free(tap->chip);
1318 free(tap->tapname);
1319 free(tap->dotted_name);
1320 free(tap);
1321 }
1322
1323 /**
1324 * Do low-level setup like initializing registers, output signals,
1325 * and clocking.
1326 */
1327 int adapter_init(struct command_context *cmd_ctx)
1328 {
1329 if (jtag)
1330 return ERROR_OK;
1331
1332 if (!jtag_interface) {
1333 /* nothing was previously specified by "interface" command */
1334 LOG_ERROR("Debug Adapter has to be specified, "
1335 "see \"interface\" command");
1336 return ERROR_JTAG_INVALID_INTERFACE;
1337 }
1338
1339 int retval;
1340 retval = jtag_interface->init();
1341 if (retval != ERROR_OK)
1342 return retval;
1343 jtag = jtag_interface;
1344
1345 /* LEGACY SUPPORT ... adapter drivers must declare what
1346 * transports they allow. Until they all do so, assume
1347 * the legacy drivers are JTAG-only
1348 */
1349 if (!transports_are_declared()) {
1350 LOG_ERROR("Adapter driver '%s' did not declare "
1351 "which transports it allows; assuming "
1352 "JTAG-only", jtag->name);
1353 retval = allow_transports(cmd_ctx, jtag_only);
1354 if (retval != ERROR_OK)
1355 return retval;
1356 }
1357
1358 if (jtag->speed == NULL) {
1359 LOG_INFO("This adapter doesn't support configurable speed");
1360 return ERROR_OK;
1361 }
1362
1363 if (CLOCK_MODE_UNSELECTED == clock_mode) {
1364 LOG_ERROR("An adapter speed is not selected in the init script."
1365 " Insert a call to adapter_khz or jtag_rclk to proceed.");
1366 return ERROR_JTAG_INIT_FAILED;
1367 }
1368
1369 int requested_khz = jtag_get_speed_khz();
1370 int actual_khz = requested_khz;
1371 int jtag_speed_var = 0;
1372 retval = jtag_get_speed(&jtag_speed_var);
1373 if (retval != ERROR_OK)
1374 return retval;
1375 retval = jtag->speed(jtag_speed_var);
1376 if (retval != ERROR_OK)
1377 return retval;
1378 retval = jtag_get_speed_readable(&actual_khz);
1379 if (ERROR_OK != retval)
1380 LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var);
1381 else if (actual_khz) {
1382 /* Adaptive clocking -- JTAG-specific */
1383 if ((CLOCK_MODE_RCLK == clock_mode)
1384 || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz)) {
1385 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1386 , actual_khz);
1387 } else
1388 LOG_INFO("clock speed %d kHz", actual_khz);
1389 } else
1390 LOG_INFO("RCLK (adaptive clock speed)");
1391
1392 return ERROR_OK;
1393 }
1394
1395 int jtag_init_inner(struct command_context *cmd_ctx)
1396 {
1397 struct jtag_tap *tap;
1398 int retval;
1399 bool issue_setup = true;
1400
1401 LOG_DEBUG("Init JTAG chain");
1402
1403 tap = jtag_tap_next_enabled(NULL);
1404 if (tap == NULL) {
1405 /* Once JTAG itself is properly set up, and the scan chain
1406 * isn't absurdly large, IDCODE autoprobe should work fine.
1407 *
1408 * But ... IRLEN autoprobe can fail even on systems which
1409 * are fully conformant to JTAG. Also, JTAG setup can be
1410 * quite finicky on some systems.
1411 *
1412 * REVISIT: if TAP autoprobe works OK, then in many cases
1413 * we could escape to tcl code and set up targets based on
1414 * the TAP's IDCODE values.
1415 */
1416 LOG_WARNING("There are no enabled taps. "
1417 "AUTO PROBING MIGHT NOT WORK!!");
1418
1419 /* REVISIT default clock will often be too fast ... */
1420 }
1421
1422 jtag_add_tlr();
1423 retval = jtag_execute_queue();
1424 if (retval != ERROR_OK)
1425 return retval;
1426
1427 /* Examine DR values first. This discovers problems which will
1428 * prevent communication ... hardware issues like TDO stuck, or
1429 * configuring the wrong number of (enabled) TAPs.
1430 */
1431 retval = jtag_examine_chain();
1432 switch (retval) {
1433 case ERROR_OK:
1434 /* complete success */
1435 break;
1436 default:
1437 /* For backward compatibility reasons, try coping with
1438 * configuration errors involving only ID mismatches.
1439 * We might be able to talk to the devices.
1440 *
1441 * Also the device might be powered down during startup.
1442 *
1443 * After OpenOCD starts, we can try to power on the device
1444 * and run a reset.
1445 */
1446 LOG_ERROR("Trying to use configured scan chain anyway...");
1447 issue_setup = false;
1448 break;
1449 }
1450
1451 /* Now look at IR values. Problems here will prevent real
1452 * communication. They mostly mean that the IR length is
1453 * wrong ... or that the IR capture value is wrong. (The
1454 * latter is uncommon, but easily worked around: provide
1455 * ircapture/irmask values during TAP setup.)
1456 */
1457 retval = jtag_validate_ircapture();
1458 if (retval != ERROR_OK) {
1459 /* The target might be powered down. The user
1460 * can power it up and reset it after firing
1461 * up OpenOCD.
1462 */
1463 issue_setup = false;
1464 }
1465
1466 if (issue_setup)
1467 jtag_notify_event(JTAG_TAP_EVENT_SETUP);
1468 else
1469 LOG_WARNING("Bypassing JTAG setup events due to errors");
1470
1471
1472 return ERROR_OK;
1473 }
1474
1475 int adapter_quit(void)
1476 {
1477 if (!jtag || !jtag->quit)
1478 return ERROR_OK;
1479
1480 /* close the JTAG interface */
1481 int result = jtag->quit();
1482 if (ERROR_OK != result)
1483 LOG_ERROR("failed: %d", result);
1484
1485 return ERROR_OK;
1486 }
1487
1488 int swd_init_reset(struct command_context *cmd_ctx)
1489 {
1490 int retval = adapter_init(cmd_ctx);
1491 if (retval != ERROR_OK)
1492 return retval;
1493
1494 LOG_DEBUG("Initializing with hard SRST reset");
1495
1496 if (jtag_reset_config & RESET_HAS_SRST)
1497 swd_add_reset(1);
1498 swd_add_reset(0);
1499 retval = jtag_execute_queue();
1500 return retval;
1501 }
1502
1503 int jtag_init_reset(struct command_context *cmd_ctx)
1504 {
1505 int retval = adapter_init(cmd_ctx);
1506 if (retval != ERROR_OK)
1507 return retval;
1508
1509 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1510
1511 /*
1512 * This procedure is used by default when OpenOCD triggers a reset.
1513 * It's now done through an overridable Tcl "init_reset" wrapper.
1514 *
1515 * This started out as a more powerful "get JTAG working" reset than
1516 * jtag_init_inner(), applying TRST because some chips won't activate
1517 * JTAG without a TRST cycle (presumed to be async, though some of
1518 * those chips synchronize JTAG activation using TCK).
1519 *
1520 * But some chips only activate JTAG as part of an SRST cycle; SRST
1521 * got mixed in. So it became a hard reset routine, which got used
1522 * in more places, and which coped with JTAG reset being forced as
1523 * part of SRST (srst_pulls_trst).
1524 *
1525 * And even more corner cases started to surface: TRST and/or SRST
1526 * assertion timings matter; some chips need other JTAG operations;
1527 * TRST/SRST sequences can need to be different from these, etc.
1528 *
1529 * Systems should override that wrapper to support system-specific
1530 * requirements that this not-fully-generic code doesn't handle.
1531 *
1532 * REVISIT once Tcl code can read the reset_config modes, this won't
1533 * need to be a C routine at all...
1534 */
1535 if (jtag_reset_config & RESET_HAS_SRST) {
1536 jtag_add_reset(1, 1);
1537 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1538 jtag_add_reset(0, 1);
1539 } else {
1540 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1541 }
1542
1543 /* some targets enable us to connect with srst asserted */
1544 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1545 if (jtag_reset_config & RESET_SRST_NO_GATING)
1546 jtag_add_reset(0, 1);
1547 else {
1548 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1549 jtag_add_reset(0, 0);
1550 }
1551 } else
1552 jtag_add_reset(0, 0);
1553 retval = jtag_execute_queue();
1554 if (retval != ERROR_OK)
1555 return retval;
1556
1557 /* Check that we can communication on the JTAG chain + eventually we want to
1558 * be able to perform enumeration only after OpenOCD has started
1559 * telnet and GDB server
1560 *
1561 * That would allow users to more easily perform any magic they need to before
1562 * reset happens.
1563 */
1564 return jtag_init_inner(cmd_ctx);
1565 }
1566
1567 int jtag_init(struct command_context *cmd_ctx)
1568 {
1569 int retval = adapter_init(cmd_ctx);
1570 if (retval != ERROR_OK)
1571 return retval;
1572
1573 /* guard against oddball hardware: force resets to be inactive */
1574 jtag_add_reset(0, 0);
1575
1576 /* some targets enable us to connect with srst asserted */
1577 if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
1578 if (jtag_reset_config & RESET_SRST_NO_GATING)
1579 jtag_add_reset(0, 1);
1580 else
1581 LOG_WARNING("\'srst_nogate\' reset_config option is required");
1582 }
1583 retval = jtag_execute_queue();
1584 if (retval != ERROR_OK)
1585 return retval;
1586
1587 if (Jim_Eval_Named(cmd_ctx->interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1588 return ERROR_FAIL;
1589
1590 return ERROR_OK;
1591 }
1592
1593 unsigned jtag_get_speed_khz(void)
1594 {
1595 return speed_khz;
1596 }
1597
1598 static int adapter_khz_to_speed(unsigned khz, int *speed)
1599 {
1600 LOG_DEBUG("convert khz to interface specific speed value");
1601 speed_khz = khz;
1602 if (jtag != NULL) {
1603 LOG_DEBUG("have interface set up");
1604 int speed_div1;
1605 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1606 if (ERROR_OK != retval)
1607 return retval;
1608 *speed = speed_div1;
1609 }
1610 return ERROR_OK;
1611 }
1612
1613 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int *speed)
1614 {
1615 int retval = adapter_khz_to_speed(0, speed);
1616 if ((ERROR_OK != retval) && fallback_speed_khz) {
1617 LOG_DEBUG("trying fallback speed...");
1618 retval = adapter_khz_to_speed(fallback_speed_khz, speed);
1619 }
1620 return retval;
1621 }
1622
1623 static int jtag_set_speed(int speed)
1624 {
1625 jtag_speed = speed;
1626 /* this command can be called during CONFIG,
1627 * in which case jtag isn't initialized */
1628 return jtag ? jtag->speed(speed) : ERROR_OK;
1629 }
1630
1631 int jtag_config_khz(unsigned khz)
1632 {
1633 LOG_DEBUG("handle jtag khz");
1634 clock_mode = CLOCK_MODE_KHZ;
1635 int speed = 0;
1636 int retval = adapter_khz_to_speed(khz, &speed);
1637 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1638 }
1639
1640 int jtag_config_rclk(unsigned fallback_speed_khz)
1641 {
1642 LOG_DEBUG("handle jtag rclk");
1643 clock_mode = CLOCK_MODE_RCLK;
1644 rclk_fallback_speed_khz = fallback_speed_khz;
1645 int speed = 0;
1646 int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1647 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1648 }
1649
1650 int jtag_get_speed(int *speed)
1651 {
1652 switch (clock_mode) {
1653 case CLOCK_MODE_KHZ:
1654 adapter_khz_to_speed(jtag_get_speed_khz(), speed);
1655 break;
1656 case CLOCK_MODE_RCLK:
1657 jtag_rclk_to_speed(rclk_fallback_speed_khz, speed);
1658 break;
1659 default:
1660 LOG_ERROR("BUG: unknown jtag clock mode");
1661 return ERROR_FAIL;
1662 }
1663 return ERROR_OK;
1664 }
1665
1666 int jtag_get_speed_readable(int *khz)
1667 {
1668 int jtag_speed_var = 0;
1669 int retval = jtag_get_speed(&jtag_speed_var);
1670 if (retval != ERROR_OK)
1671 return retval;
1672 return jtag ? jtag->speed_div(jtag_speed_var, khz) : ERROR_OK;
1673 }
1674
1675 void jtag_set_verify(bool enable)
1676 {
1677 jtag_verify = enable;
1678 }
1679
1680 bool jtag_will_verify()
1681 {
1682 return jtag_verify;
1683 }
1684
1685 void jtag_set_verify_capture_ir(bool enable)
1686 {
1687 jtag_verify_capture_ir = enable;
1688 }
1689
1690 bool jtag_will_verify_capture_ir()
1691 {
1692 return jtag_verify_capture_ir;
1693 }
1694
1695 int jtag_power_dropout(int *dropout)
1696 {
1697 if (jtag == NULL) {
1698 /* TODO: as the jtag interface is not valid all
1699 * we can do at the moment is exit OpenOCD */
1700 LOG_ERROR("No Valid JTAG Interface Configured.");
1701 exit(-1);
1702 }
1703 return jtag->power_dropout(dropout);
1704 }
1705
1706 int jtag_srst_asserted(int *srst_asserted)
1707 {
1708 return jtag->srst_asserted(srst_asserted);
1709 }
1710
1711 enum reset_types jtag_get_reset_config(void)
1712 {
1713 return jtag_reset_config;
1714 }
1715 void jtag_set_reset_config(enum reset_types type)
1716 {
1717 jtag_reset_config = type;
1718 }
1719
1720 int jtag_get_trst(void)
1721 {
1722 return jtag_trst;
1723 }
1724 int jtag_get_srst(void)
1725 {
1726 return jtag_srst;
1727 }
1728
1729 void jtag_set_nsrst_delay(unsigned delay)
1730 {
1731 adapter_nsrst_delay = delay;
1732 }
1733 unsigned jtag_get_nsrst_delay(void)
1734 {
1735 return adapter_nsrst_delay;
1736 }
1737 void jtag_set_ntrst_delay(unsigned delay)
1738 {
1739 jtag_ntrst_delay = delay;
1740 }
1741 unsigned jtag_get_ntrst_delay(void)
1742 {
1743 return jtag_ntrst_delay;
1744 }
1745
1746
1747 void jtag_set_nsrst_assert_width(unsigned delay)
1748 {
1749 adapter_nsrst_assert_width = delay;
1750 }
1751 unsigned jtag_get_nsrst_assert_width(void)
1752 {
1753 return adapter_nsrst_assert_width;
1754 }
1755 void jtag_set_ntrst_assert_width(unsigned delay)
1756 {
1757 jtag_ntrst_assert_width = delay;
1758 }
1759 unsigned jtag_get_ntrst_assert_width(void)
1760 {
1761 return jtag_ntrst_assert_width;
1762 }
1763
1764 static int jtag_select(struct command_context *ctx)
1765 {
1766 int retval;
1767
1768 /* NOTE: interface init must already have been done.
1769 * That works with only C code ... no Tcl glue required.
1770 */
1771
1772 retval = jtag_register_commands(ctx);
1773
1774 if (retval != ERROR_OK)
1775 return retval;
1776
1777 retval = svf_register_commands(ctx);
1778
1779 if (retval != ERROR_OK)
1780 return retval;
1781
1782 return xsvf_register_commands(ctx);
1783 }
1784
1785 static struct transport jtag_transport = {
1786 .name = "jtag",
1787 .select = jtag_select,
1788 .init = jtag_init,
1789 };
1790
1791 static void jtag_constructor(void) __attribute__((constructor));
1792 static void jtag_constructor(void)
1793 {
1794 transport_register(&jtag_transport);
1795 }
1796
1797 /** Returns true if the current debug session
1798 * is using JTAG as its transport.
1799 */
1800 bool transport_is_jtag(void)
1801 {
1802 return get_current_transport() == &jtag_transport;
1803 }
1804
1805 void adapter_assert_reset(void)
1806 {
1807 if (transport_is_jtag()) {
1808 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1809 jtag_add_reset(1, 1);
1810 else
1811 jtag_add_reset(0, 1);
1812 } else if (transport_is_swd())
1813 swd_add_reset(1);
1814 else if (get_current_transport() != NULL)
1815 LOG_ERROR("reset is not supported on %s",
1816 get_current_transport()->name);
1817 else
1818 LOG_ERROR("transport is not selected");
1819 }
1820
1821 void adapter_deassert_reset(void)
1822 {
1823 if (transport_is_jtag())
1824 jtag_add_reset(0, 0);
1825 else if (transport_is_swd())
1826 swd_add_reset(0);
1827 else if (get_current_transport() != NULL)
1828 LOG_ERROR("reset is not supported on %s",
1829 get_current_transport()->name);
1830 else
1831 LOG_ERROR("transport is not selected");
1832 }
1833
1834 int adapter_config_trace(bool enabled, enum tpio_pin_protocol pin_protocol,
1835 uint32_t port_size, unsigned int *trace_freq)
1836 {
1837 if (jtag->config_trace)
1838 return jtag->config_trace(enabled, pin_protocol, port_size,
1839 trace_freq);
1840 else if (enabled) {
1841 LOG_ERROR("The selected interface does not support tracing");
1842 return ERROR_FAIL;
1843 }
1844
1845 return ERROR_OK;
1846 }
1847
1848 int adapter_poll_trace(uint8_t *buf, size_t *size)
1849 {
1850 if (jtag->poll_trace)
1851 return jtag->poll_trace(buf, size);
1852
1853 return ERROR_FAIL;
1854 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)