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

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)