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

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)