portability updates
[openocd.git] / src / jtag / core.c
1 /***************************************************************************
2 * Copyright (C) 2009 Zachary T Welch *
3 * zw@superlucidity.net *
4 * *
5 * Copyright (C) 2007,2008,2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2009 SoftPLC Corporation *
9 * http://softplc.com *
10 * dick@softplc.com *
11 * *
12 * Copyright (C) 2005 by Dominic Rath *
13 * Dominic.Rath@gmx.de *
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
24 * *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "jtag.h"
35 #include "minidriver.h"
36 #include "interface.h"
37
38 #ifdef HAVE_STRINGS_H
39 #include <strings.h>
40 #endif
41
42
43 /// The number of JTAG queue flushes (for profiling and debugging purposes).
44 static int jtag_flush_queue_count;
45
46 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
47 int in_num_fields, scan_field_t *in_fields, tap_state_t state);
48
49 /**
50 * The jtag_error variable is set when an error occurs while executing
51 * the queue. Application code may set this using jtag_set_error(),
52 * when an error occurs during processing that should be reported during
53 * jtag_execute_queue().
54 *
55 * Tts value may be checked with jtag_get_error() and cleared with
56 * jtag_error_clear(). This value is returned (and cleared) by
57 * jtag_execute_queue().
58 */
59 static int jtag_error = ERROR_OK;
60
61 static const char *jtag_event_strings[] =
62 {
63 [JTAG_TRST_ASSERTED] = "TAP reset",
64 [JTAG_TAP_EVENT_SETUP] = "TAP setup",
65 [JTAG_TAP_EVENT_ENABLE] = "TAP enabled",
66 [JTAG_TAP_EVENT_DISABLE] = "TAP disabled",
67 };
68
69 /*
70 * JTAG adapters must initialize with TRST and SRST de-asserted
71 * (they're negative logic, so that means *high*). But some
72 * hardware doesn't necessarily work that way ... so set things
73 * up so that jtag_init() always forces that state.
74 */
75 static int jtag_trst = -1;
76 static int jtag_srst = -1;
77
78 /**
79 * List all TAPs that have been created.
80 */
81 static jtag_tap_t *__jtag_all_taps = NULL;
82 /**
83 * The number of TAPs in the __jtag_all_taps list, used to track the
84 * assigned chain position to new TAPs
85 */
86 static unsigned jtag_num_taps = 0;
87
88 static enum reset_types jtag_reset_config = RESET_NONE;
89 static tap_state_t cmd_queue_end_state = TAP_RESET;
90 tap_state_t cmd_queue_cur_state = TAP_RESET;
91
92 static bool jtag_verify_capture_ir = true;
93 static int jtag_verify = 1;
94
95 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
96 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
97 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
98 static int jtag_nsrst_assert_width = 0; /* width of assertion */
99 static int jtag_ntrst_assert_width = 0; /* width of assertion */
100
101 typedef struct jtag_event_callback_s
102 {
103 jtag_event_handler_t callback;
104 void* priv;
105 struct jtag_event_callback_s* next;
106 } jtag_event_callback_t;
107
108 /* callbacks to inform high-level handlers about JTAG state changes */
109 static jtag_event_callback_t *jtag_event_callbacks;
110
111 /* speed in kHz*/
112 static int speed_khz = 0;
113 /* speed to fallback to when RCLK is requested but not supported */
114 static int rclk_fallback_speed_khz = 0;
115 static enum {CLOCK_MODE_SPEED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
116 static int jtag_speed = 0;
117
118 static struct jtag_interface_s *jtag = NULL;
119
120 /* configuration */
121 jtag_interface_t *jtag_interface = NULL;
122
123 void jtag_set_error(int error)
124 {
125 if ((error == ERROR_OK) || (jtag_error != ERROR_OK))
126 return;
127 jtag_error = error;
128 }
129 int jtag_get_error(void)
130 {
131 return jtag_error;
132 }
133 int jtag_error_clear(void)
134 {
135 int temp = jtag_error;
136 jtag_error = ERROR_OK;
137 return temp;
138 }
139
140 /************/
141
142 static bool jtag_poll = 1;
143
144 bool is_jtag_poll_safe(void)
145 {
146 /* Polling can be disabled explicitly with set_enabled(false).
147 * It is also implicitly disabled while TRST is active and
148 * while SRST is gating the JTAG clock.
149 */
150 if (!jtag_poll || jtag_trst != 0)
151 return false;
152 return jtag_srst == 0 || (jtag_reset_config & RESET_SRST_NO_GATING);
153 }
154
155 bool jtag_poll_get_enabled(void)
156 {
157 return jtag_poll;
158 }
159
160 void jtag_poll_set_enabled(bool value)
161 {
162 jtag_poll = value;
163 }
164
165 /************/
166
167 jtag_tap_t *jtag_all_taps(void)
168 {
169 return __jtag_all_taps;
170 };
171
172 unsigned jtag_tap_count(void)
173 {
174 return jtag_num_taps;
175 }
176
177 unsigned jtag_tap_count_enabled(void)
178 {
179 jtag_tap_t *t = jtag_all_taps();
180 unsigned n = 0;
181 while (t)
182 {
183 if (t->enabled)
184 n++;
185 t = t->next_tap;
186 }
187 return n;
188 }
189
190 /// Append a new TAP to the chain of all taps.
191 void jtag_tap_add(struct jtag_tap_s *t)
192 {
193 t->abs_chain_position = jtag_num_taps++;
194
195 jtag_tap_t **tap = &__jtag_all_taps;
196 while (*tap != NULL)
197 tap = &(*tap)->next_tap;
198 *tap = t;
199 }
200
201 /* returns a pointer to the n-th device in the scan chain */
202 static inline jtag_tap_t *jtag_tap_by_position(unsigned n)
203 {
204 jtag_tap_t *t = jtag_all_taps();
205
206 while (t && n-- > 0)
207 t = t->next_tap;
208
209 return t;
210 }
211
212 jtag_tap_t *jtag_tap_by_string(const char *s)
213 {
214 /* try by name first */
215 jtag_tap_t *t = jtag_all_taps();
216
217 while (t)
218 {
219 if (0 == strcmp(t->dotted_name, s))
220 return t;
221 t = t->next_tap;
222 }
223
224 /* no tap found by name, so try to parse the name as a number */
225 unsigned n;
226 if (parse_uint(s, &n) != ERROR_OK)
227 return NULL;
228
229 /* FIXME remove this numeric fallback code late June 2010, along
230 * with all info in the User's Guide that TAPs have numeric IDs.
231 * Also update "scan_chain" output to not display the numbers.
232 */
233 t = jtag_tap_by_position(n);
234 if (t)
235 LOG_WARNING("Specify TAP '%s' by name, not number %u",
236 t->dotted_name, n);
237
238 return t;
239 }
240
241 jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
242 {
243 const char *cp = Jim_GetString(o, NULL);
244 jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
245 if (NULL == cp)
246 cp = "(unknown)";
247 if (NULL == t)
248 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
249 return t;
250 }
251
252 jtag_tap_t* jtag_tap_next_enabled(jtag_tap_t* p)
253 {
254 p = p ? p->next_tap : jtag_all_taps();
255 while (p)
256 {
257 if (p->enabled)
258 return p;
259 p = p->next_tap;
260 }
261 return NULL;
262 }
263
264 const char *jtag_tap_name(const jtag_tap_t *tap)
265 {
266 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
267 }
268
269
270 int jtag_register_event_callback(jtag_event_handler_t callback, void *priv)
271 {
272 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
273
274 if (callback == NULL)
275 {
276 return ERROR_INVALID_ARGUMENTS;
277 }
278
279 if (*callbacks_p)
280 {
281 while ((*callbacks_p)->next)
282 callbacks_p = &((*callbacks_p)->next);
283 callbacks_p = &((*callbacks_p)->next);
284 }
285
286 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
287 (*callbacks_p)->callback = callback;
288 (*callbacks_p)->priv = priv;
289 (*callbacks_p)->next = NULL;
290
291 return ERROR_OK;
292 }
293
294 int jtag_unregister_event_callback(jtag_event_handler_t callback, void *priv)
295 {
296 jtag_event_callback_t **callbacks_p;
297 jtag_event_callback_t **next;
298
299 if (callback == NULL)
300 {
301 return ERROR_INVALID_ARGUMENTS;
302 }
303
304 for (callbacks_p = &jtag_event_callbacks;
305 *callbacks_p != NULL;
306 callbacks_p = next)
307 {
308 next = &((*callbacks_p)->next);
309
310 if ((*callbacks_p)->priv != priv)
311 continue;
312
313 if ((*callbacks_p)->callback == callback)
314 {
315 free(*callbacks_p);
316 *callbacks_p = *next;
317 }
318 }
319
320 return ERROR_OK;
321 }
322
323 int jtag_call_event_callbacks(enum jtag_event event)
324 {
325 jtag_event_callback_t *callback = jtag_event_callbacks;
326
327 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
328
329 while (callback)
330 {
331 jtag_event_callback_t *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_alloc_in_value32(scan_field_t *field)
357 {
358 interface_jtag_alloc_in_value32(field);
359 }
360
361 void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
362 tap_state_t state)
363 {
364 jtag_prelude(state);
365
366 int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
367 jtag_set_error(retval);
368 }
369
370
371 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
372 {
373 assert(state != TAP_RESET);
374
375 if (jtag_verify && jtag_verify_capture_ir)
376 {
377 /* 8 x 32 bit id's is enough for all invocations */
378
379 for (int j = 0; j < in_num_fields; j++)
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[j].check_value = in_fields[j].tap->expected;
385 in_fields[j].check_mask = in_fields[j].tap->expected_mask;
386 }
387 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
388 } else
389 {
390 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
391 }
392 }
393
394 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
395 tap_state_t state)
396 {
397 assert(state != TAP_RESET);
398
399 jtag_prelude(state);
400
401 int retval = interface_jtag_add_plain_ir_scan(
402 in_num_fields, in_fields, state);
403 jtag_set_error(retval);
404 }
405
406 void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
407 {
408 interface_jtag_add_callback(f, data0);
409 }
410
411 void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
412 jtag_callback_data_t data1, jtag_callback_data_t data2,
413 jtag_callback_data_t data3)
414 {
415 interface_jtag_add_callback4(f, data0, data1, data2, data3);
416 }
417
418 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
419 uint8_t *in_check_mask, int num_bits);
420
421 static int jtag_check_value_mask_callback(jtag_callback_data_t data0, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
422 {
423 return jtag_check_value_inner((uint8_t *)data0, (uint8_t *)data1, (uint8_t *)data2, (int)data3);
424 }
425
426 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
427 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
428 {
429 for (int i = 0; i < in_num_fields; i++)
430 {
431 struct scan_field_s *field = &in_fields[i];
432 field->allocated = 0;
433 field->modified = 0;
434 if (field->check_value || field->in_value)
435 continue;
436 interface_jtag_add_scan_check_alloc(field);
437 field->modified = 1;
438 }
439
440 jtag_add_scan(in_num_fields, in_fields, state);
441
442 for (int i = 0; i < in_num_fields; i++)
443 {
444 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
445 {
446 /* this is synchronous for a minidriver */
447 jtag_add_callback4(jtag_check_value_mask_callback, (jtag_callback_data_t)in_fields[i].in_value,
448 (jtag_callback_data_t)in_fields[i].check_value,
449 (jtag_callback_data_t)in_fields[i].check_mask,
450 (jtag_callback_data_t)in_fields[i].num_bits);
451 }
452 if (in_fields[i].allocated)
453 {
454 free(in_fields[i].in_value);
455 }
456 if (in_fields[i].modified)
457 {
458 in_fields[i].in_value = NULL;
459 }
460 }
461 }
462
463 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
464 {
465 if (jtag_verify)
466 {
467 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
468 } else
469 {
470 jtag_add_dr_scan(in_num_fields, in_fields, state);
471 }
472 }
473
474
475 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
476 tap_state_t state)
477 {
478 assert(state != TAP_RESET);
479
480 jtag_prelude(state);
481
482 int retval;
483 retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
484 jtag_set_error(retval);
485 }
486
487 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
488 tap_state_t state)
489 {
490 assert(state != TAP_RESET);
491
492 jtag_prelude(state);
493
494 int retval;
495 retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
496 jtag_set_error(retval);
497 }
498
499 void jtag_add_dr_out(jtag_tap_t* tap,
500 int num_fields, const int* num_bits, const uint32_t* value,
501 tap_state_t end_state)
502 {
503 assert(end_state != TAP_RESET);
504 assert(end_state != TAP_INVALID);
505
506 cmd_queue_cur_state = end_state;
507
508 interface_jtag_add_dr_out(tap,
509 num_fields, num_bits, value,
510 end_state);
511 }
512
513 void jtag_add_tlr(void)
514 {
515 jtag_prelude(TAP_RESET);
516 jtag_set_error(interface_jtag_add_tlr());
517
518 /* NOTE: order here matches TRST path in jtag_add_reset() */
519 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
520 jtag_notify_event(JTAG_TRST_ASSERTED);
521 }
522
523 void jtag_add_pathmove(int num_states, const tap_state_t *path)
524 {
525 tap_state_t cur_state = cmd_queue_cur_state;
526
527 /* the last state has to be a stable state */
528 if (!tap_is_state_stable(path[num_states - 1]))
529 {
530 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
531 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
532 return;
533 }
534
535 for (int i = 0; i < num_states; i++)
536 {
537 if (path[i] == TAP_RESET)
538 {
539 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
540 jtag_set_error(ERROR_JTAG_STATE_INVALID);
541 return;
542 }
543
544 if (tap_state_transition(cur_state, true) != path[i]
545 && tap_state_transition(cur_state, false) != path[i])
546 {
547 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
548 tap_state_name(cur_state), tap_state_name(path[i]));
549 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
550 return;
551 }
552 cur_state = path[i];
553 }
554
555 jtag_checks();
556
557 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
558 cmd_queue_cur_state = path[num_states - 1];
559 }
560
561 int jtag_add_statemove(tap_state_t goal_state)
562 {
563 tap_state_t cur_state = cmd_queue_cur_state;
564
565 LOG_DEBUG("cur_state=%s goal_state=%s",
566 tap_state_name(cur_state),
567 tap_state_name(goal_state));
568
569
570 if (goal_state == cur_state)
571 ; /* nothing to do */
572 else if (goal_state == TAP_RESET)
573 {
574 jtag_add_tlr();
575 }
576 else if (tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state))
577 {
578 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
579 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
580 tap_state_t moves[8];
581 assert(tms_count < DIM(moves));
582
583 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
584 {
585 bool bit = tms_bits & 1;
586
587 cur_state = tap_state_transition(cur_state, bit);
588 moves[i] = cur_state;
589 }
590
591 jtag_add_pathmove(tms_count, moves);
592 }
593 else if (tap_state_transition(cur_state, true) == goal_state
594 || tap_state_transition(cur_state, false) == goal_state)
595 {
596 jtag_add_pathmove(1, &goal_state);
597 }
598
599 else
600 return ERROR_FAIL;
601
602 return ERROR_OK;
603 }
604
605 void jtag_add_runtest(int num_cycles, tap_state_t state)
606 {
607 jtag_prelude(state);
608 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
609 }
610
611
612 void jtag_add_clocks(int num_cycles)
613 {
614 if (!tap_is_state_stable(cmd_queue_cur_state))
615 {
616 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
617 tap_state_name(cmd_queue_cur_state));
618 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
619 return;
620 }
621
622 if (num_cycles > 0)
623 {
624 jtag_checks();
625 jtag_set_error(interface_jtag_add_clocks(num_cycles));
626 }
627 }
628
629 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
630 {
631 int trst_with_tlr = 0;
632 int new_srst = 0;
633 int new_trst = 0;
634
635 /* Without SRST, we must use target-specific JTAG operations
636 * on each target; callers should not be requesting SRST when
637 * that signal doesn't exist.
638 *
639 * RESET_SRST_PULLS_TRST is a board or chip level quirk, which
640 * can kick in even if the JTAG adapter can't drive TRST.
641 */
642 if (req_srst) {
643 if (!(jtag_reset_config & RESET_HAS_SRST)) {
644 LOG_ERROR("BUG: can't assert SRST");
645 jtag_set_error(ERROR_FAIL);
646 return;
647 }
648 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) != 0
649 && !req_tlr_or_trst) {
650 LOG_ERROR("BUG: can't assert only SRST");
651 jtag_set_error(ERROR_FAIL);
652 return;
653 }
654 new_srst = 1;
655 }
656
657 /* JTAG reset (entry to TAP_RESET state) can always be achieved
658 * using TCK and TMS; that may go through a TAP_{IR,DR}UPDATE
659 * state first. TRST accelerates it, and bypasses those states.
660 *
661 * RESET_TRST_PULLS_SRST is a board or chip level quirk, which
662 * can kick in even if the JTAG adapter can't drive SRST.
663 */
664 if (req_tlr_or_trst) {
665 if (!(jtag_reset_config & RESET_HAS_TRST))
666 trst_with_tlr = 1;
667 else if ((jtag_reset_config & RESET_TRST_PULLS_SRST) != 0
668 && !req_srst)
669 trst_with_tlr = 1;
670 else
671 new_trst = 1;
672 }
673
674 /* Maybe change TRST and/or SRST signal state */
675 if (jtag_srst != new_srst || jtag_trst != new_trst) {
676 int retval;
677
678 retval = interface_jtag_add_reset(new_trst, new_srst);
679 if (retval != ERROR_OK)
680 jtag_set_error(retval);
681 else
682 retval = jtag_execute_queue();
683
684 if (retval != ERROR_OK) {
685 LOG_ERROR("TRST/SRST error %d", retval);
686 return;
687 }
688 }
689
690 /* SRST resets everything hooked up to that signal */
691 if (jtag_srst != new_srst) {
692 jtag_srst = new_srst;
693 if (jtag_srst)
694 {
695 LOG_DEBUG("SRST line asserted");
696 if (jtag_nsrst_assert_width)
697 jtag_add_sleep(jtag_nsrst_assert_width * 1000);
698 }
699 else {
700 LOG_DEBUG("SRST line released");
701 if (jtag_nsrst_delay)
702 jtag_add_sleep(jtag_nsrst_delay * 1000);
703 }
704 }
705
706 /* Maybe enter the JTAG TAP_RESET state ...
707 * - using only TMS, TCK, and the JTAG state machine
708 * - or else more directly, using TRST
709 *
710 * TAP_RESET should be invisible to non-debug parts of the system.
711 */
712 if (trst_with_tlr) {
713 LOG_DEBUG("JTAG reset with TLR instead of TRST");
714 jtag_set_end_state(TAP_RESET);
715 jtag_add_tlr();
716
717 } else if (jtag_trst != new_trst) {
718 jtag_trst = new_trst;
719 if (jtag_trst) {
720 LOG_DEBUG("TRST line asserted");
721 tap_set_state(TAP_RESET);
722 if (jtag_ntrst_assert_width)
723 jtag_add_sleep(jtag_ntrst_assert_width * 1000);
724 } else {
725 LOG_DEBUG("TRST line released");
726 if (jtag_ntrst_delay)
727 jtag_add_sleep(jtag_ntrst_delay * 1000);
728
729 /* We just asserted nTRST, so we're now in TAP_RESET.
730 * Inform possible listeners about this, now that
731 * JTAG instructions and data can be shifted. This
732 * sequence must match jtag_add_tlr().
733 */
734 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
735 jtag_notify_event(JTAG_TRST_ASSERTED);
736 }
737 }
738 }
739
740 tap_state_t jtag_set_end_state(tap_state_t state)
741 {
742 if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
743 {
744 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
745 }
746
747 if (state != TAP_INVALID)
748 cmd_queue_end_state = state;
749 return cmd_queue_end_state;
750 }
751
752 tap_state_t jtag_get_end_state(void)
753 {
754 return cmd_queue_end_state;
755 }
756
757 void jtag_add_sleep(uint32_t us)
758 {
759 /// @todo Here, keep_alive() appears to be a layering violation!!!
760 keep_alive();
761 jtag_set_error(interface_jtag_add_sleep(us));
762 }
763
764 static int jtag_check_value_inner(uint8_t *captured, uint8_t *in_check_value,
765 uint8_t *in_check_mask, int num_bits)
766 {
767 int retval = ERROR_OK;
768
769 int compare_failed = 0;
770
771 if (in_check_mask)
772 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
773 else
774 compare_failed = buf_cmp(captured, in_check_value, num_bits);
775
776 if (compare_failed) {
777 char *captured_str, *in_check_value_str;
778 int bits = (num_bits > DEBUG_JTAG_IOZ)
779 ? DEBUG_JTAG_IOZ
780 : num_bits;
781
782 /* NOTE: we've lost diagnostic context here -- 'which tap' */
783
784 captured_str = buf_to_str(captured, bits, 16);
785 in_check_value_str = buf_to_str(in_check_value, bits, 16);
786
787 LOG_WARNING("Bad value '%s' captured during DR or IR scan:",
788 captured_str);
789 LOG_WARNING(" check_value: 0x%s", in_check_value_str);
790
791 free(captured_str);
792 free(in_check_value_str);
793
794 if (in_check_mask) {
795 char *in_check_mask_str;
796
797 in_check_mask_str = buf_to_str(in_check_mask, bits, 16);
798 LOG_WARNING(" check_mask: 0x%s", in_check_mask_str);
799 free(in_check_mask_str);
800 }
801
802 retval = ERROR_JTAG_QUEUE_FAILED;
803 }
804 return retval;
805 }
806
807 void jtag_check_value_mask(scan_field_t *field, uint8_t *value, uint8_t *mask)
808 {
809 assert(field->in_value != NULL);
810
811 if (value == NULL)
812 {
813 /* no checking to do */
814 return;
815 }
816
817 jtag_execute_queue_noclear();
818
819 int retval = jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
820 jtag_set_error(retval);
821 }
822
823
824
825 int default_interface_jtag_execute_queue(void)
826 {
827 if (NULL == jtag)
828 {
829 LOG_ERROR("No JTAG interface configured yet. "
830 "Issue 'init' command in startup scripts "
831 "before communicating with targets.");
832 return ERROR_FAIL;
833 }
834
835 return jtag->execute_queue();
836 }
837
838 void jtag_execute_queue_noclear(void)
839 {
840 jtag_flush_queue_count++;
841 jtag_set_error(interface_jtag_execute_queue());
842 }
843
844 int jtag_get_flush_queue_count(void)
845 {
846 return jtag_flush_queue_count;
847 }
848
849 int jtag_execute_queue(void)
850 {
851 jtag_execute_queue_noclear();
852 return jtag_error_clear();
853 }
854
855 static int jtag_reset_callback(enum jtag_event event, void *priv)
856 {
857 jtag_tap_t *tap = priv;
858
859 if (event == JTAG_TRST_ASSERTED)
860 {
861 tap->enabled = !tap->disabled_after_reset;
862
863 /* current instruction is either BYPASS or IDCODE */
864 buf_set_ones(tap->cur_instr, tap->ir_length);
865 tap->bypass = 1;
866 }
867
868 return ERROR_OK;
869 }
870
871 void jtag_sleep(uint32_t us)
872 {
873 alive_sleep(us/1000);
874 }
875
876 /* Maximum number of enabled JTAG devices we expect in the scan chain,
877 * plus one (to detect garbage at the end). Devices that don't support
878 * IDCODE take up fewer bits, possibly allowing a few more devices.
879 */
880 #define JTAG_MAX_CHAIN_SIZE 20
881
882 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
883 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
884 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
885
886 /* A reserved manufacturer ID is used in END_OF_CHAIN_FLAG, so we
887 * know that no valid TAP will have it as an IDCODE value.
888 */
889 #define END_OF_CHAIN_FLAG 0x000000ff
890
891 static int jtag_examine_chain_execute(uint8_t *idcode_buffer, unsigned num_idcode)
892 {
893 scan_field_t field = {
894 .tap = NULL,
895 .num_bits = num_idcode * 32,
896 .out_value = idcode_buffer,
897 .in_value = idcode_buffer,
898 };
899
900 // initialize to the end of chain ID value
901 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
902 buf_set_u32(idcode_buffer, i * 32, 32, END_OF_CHAIN_FLAG);
903
904 jtag_add_plain_dr_scan(1, &field, TAP_DRPAUSE);
905 jtag_add_tlr();
906 return jtag_execute_queue();
907 }
908
909 static bool jtag_examine_chain_check(uint8_t *idcodes, unsigned count)
910 {
911 uint8_t zero_check = 0x0;
912 uint8_t one_check = 0xff;
913
914 for (unsigned i = 0; i < count * 4; i++)
915 {
916 zero_check |= idcodes[i];
917 one_check &= idcodes[i];
918 }
919
920 /* if there wasn't a single non-zero bit or if all bits were one,
921 * the scan is not valid. We wrote a mix of both values; either
922 *
923 * - There's a hardware issue (almost certainly):
924 * + all-zeroes can mean a target stuck in JTAG reset
925 * + all-ones tends to mean no target
926 * - The scan chain is WAY longer than we can handle, *AND* either
927 * + there are several hundreds of TAPs in bypass, or
928 * + at least a few dozen TAPs all have an all-ones IDCODE
929 */
930 if (zero_check == 0x00 || one_check == 0xff)
931 {
932 LOG_ERROR("JTAG scan chain interrogation failed: all %s",
933 (zero_check == 0x00) ? "zeroes" : "ones");
934 LOG_ERROR("Check JTAG interface, timings, target power, etc.");
935 return false;
936 }
937 return true;
938 }
939
940 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
941 const char *name, uint32_t idcode)
942 {
943 log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
944 "JTAG tap: %s %16.16s: 0x%08x "
945 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
946 name, msg,
947 (unsigned int)idcode,
948 (unsigned int)EXTRACT_MFG(idcode),
949 (unsigned int)EXTRACT_PART(idcode),
950 (unsigned int)EXTRACT_VER(idcode));
951 }
952
953 static bool jtag_idcode_is_final(uint32_t idcode)
954 {
955 /*
956 * Some devices, such as AVR8, will output all 1's instead
957 * of TDI input value at end of chain. Allow those values
958 * instead of failing.
959 */
960 return idcode == END_OF_CHAIN_FLAG || idcode == 0xFFFFFFFF;
961 }
962
963 /**
964 * This helper checks that remaining bits in the examined chain data are
965 * all as expected, but a single JTAG device requires only 64 bits to be
966 * read back correctly. This can help identify and diagnose problems
967 * with the JTAG chain earlier, gives more helpful/explicit error messages.
968 * Returns TRUE iff garbage was found.
969 */
970 static bool jtag_examine_chain_end(uint8_t *idcodes, unsigned count, unsigned max)
971 {
972 bool triggered = false;
973 for (; count < max - 31; count += 32)
974 {
975 uint32_t idcode = buf_get_u32(idcodes, count, 32);
976
977 /* do not trigger the warning if the data looks good */
978 if (jtag_idcode_is_final(idcode))
979 continue;
980 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
981 count, (unsigned int)idcode);
982 triggered = true;
983 }
984 return triggered;
985 }
986
987 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
988 {
989 /* ignore expected BYPASS codes; warn otherwise */
990 if (0 == tap->expected_ids_cnt && !tap->idcode)
991 return true;
992
993 /* Loop over the expected identification codes and test for a match */
994 unsigned ii, limit = tap->expected_ids_cnt;
995
996 for (ii = 0; ii < limit; ii++)
997 {
998 if (tap->idcode == tap->expected_ids[ii])
999 return true;
1000
1001 /* treat "-expected-id 0" as a "don't-warn" wildcard */
1002 if (0 == tap->expected_ids[ii])
1003 return true;
1004 }
1005
1006 /* If none of the expected ids matched, warn */
1007 jtag_examine_chain_display(LOG_LVL_WARNING, "UNEXPECTED",
1008 tap->dotted_name, tap->idcode);
1009 for (ii = 0; ii < limit; ii++)
1010 {
1011 char msg[32];
1012
1013 snprintf(msg, sizeof(msg), "expected %u of %u", ii + 1, limit);
1014 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1015 tap->dotted_name, tap->expected_ids[ii]);
1016 }
1017 return false;
1018 }
1019
1020 /* Try to examine chain layout according to IEEE 1149.1 §12
1021 * This is called a "blind interrogation" of the scan chain.
1022 */
1023 static int jtag_examine_chain(void)
1024 {
1025 uint8_t idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1026 unsigned bit_count;
1027 int retval;
1028
1029 /* DR scan to collect BYPASS or IDCODE register contents.
1030 * Then make sure the scan data has both ones and zeroes.
1031 */
1032 LOG_DEBUG("DR scan interrogation for IDCODE/BYPASS");
1033 retval = jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
1034 if (retval != ERROR_OK)
1035 return retval;
1036 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
1037 return ERROR_JTAG_INIT_FAILED;
1038
1039 /* point at the 1st tap */
1040 jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
1041 if (tap == NULL)
1042 {
1043 LOG_ERROR("JTAG: No taps enabled?");
1044 return ERROR_JTAG_INIT_FAILED;
1045 }
1046
1047 for (bit_count = 0;
1048 tap && bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;
1049 tap = jtag_tap_next_enabled(tap))
1050 {
1051 uint32_t idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1052
1053 if ((idcode & 1) == 0)
1054 {
1055 /* Zero for LSB indicates a device in bypass */
1056 LOG_WARNING("TAP %s does not have IDCODE",
1057 tap->dotted_name);
1058 idcode = 0;
1059 tap->hasidcode = false;
1060
1061 bit_count += 1;
1062 }
1063 else
1064 {
1065 /* Friendly devices support IDCODE */
1066 tap->hasidcode = true;
1067 jtag_examine_chain_display(LOG_LVL_INFO,
1068 "tap/device found",
1069 tap->dotted_name, idcode);
1070
1071 bit_count += 32;
1072 }
1073 tap->idcode = idcode;
1074
1075 /* ensure the TAP ID matches what was expected */
1076 if (!jtag_examine_chain_match_tap(tap))
1077 retval = ERROR_JTAG_INIT_SOFT_FAIL;
1078 }
1079
1080 /* Fail if too many TAPs were enabled for us to verify them all. */
1081 if (tap) {
1082 LOG_ERROR("Too many TAPs enabled; '%s' ignored.",
1083 tap->dotted_name);
1084 return ERROR_JTAG_INIT_FAILED;
1085 }
1086
1087 /* After those IDCODE or BYPASS register values should be
1088 * only the data we fed into the scan chain.
1089 */
1090 if (jtag_examine_chain_end(idcode_buffer, bit_count,
1091 8 * sizeof(idcode_buffer))) {
1092 LOG_ERROR("double-check your JTAG setup (interface, "
1093 "speed, missing TAPs, ...)");
1094 return ERROR_JTAG_INIT_FAILED;
1095 }
1096
1097 /* Return success or, for backwards compatibility if only
1098 * some IDCODE values mismatched, a soft/continuable fault.
1099 */
1100 return retval;
1101 }
1102
1103 /*
1104 * Validate the date loaded by entry to the Capture-IR state, to help
1105 * find errors related to scan chain configuration (wrong IR lengths)
1106 * or communication.
1107 *
1108 * Entry state can be anything. On non-error exit, all TAPs are in
1109 * bypass mode. On error exits, the scan chain is reset.
1110 */
1111 static int jtag_validate_ircapture(void)
1112 {
1113 jtag_tap_t *tap;
1114 int total_ir_length = 0;
1115 uint8_t *ir_test = NULL;
1116 scan_field_t field;
1117 int val;
1118 int chain_pos = 0;
1119 int retval;
1120
1121 for (tap = NULL, total_ir_length = 0;
1122 (tap = jtag_tap_next_enabled(tap)) != NULL;
1123 total_ir_length += tap->ir_length)
1124 continue;
1125
1126 /* increase length to add 2 bit sentinel after scan */
1127 total_ir_length += 2;
1128
1129 ir_test = malloc(CEIL(total_ir_length, 8));
1130 if (ir_test == NULL)
1131 return ERROR_FAIL;
1132
1133 /* after this scan, all TAPs will capture BYPASS instructions */
1134 buf_set_ones(ir_test, total_ir_length);
1135
1136 field.tap = NULL;
1137 field.num_bits = total_ir_length;
1138 field.out_value = ir_test;
1139 field.in_value = ir_test;
1140
1141 jtag_add_plain_ir_scan(1, &field, TAP_IDLE);
1142
1143 LOG_DEBUG("IR capture validation scan");
1144 retval = jtag_execute_queue();
1145 if (retval != ERROR_OK)
1146 goto done;
1147
1148 tap = NULL;
1149 chain_pos = 0;
1150
1151 for (;;) {
1152 tap = jtag_tap_next_enabled(tap);
1153 if (tap == NULL) {
1154 break;
1155 }
1156
1157 /* Validate the two LSBs, which must be 01 per JTAG spec.
1158 *
1159 * Or ... more bits could be provided by TAP declaration.
1160 * Plus, some taps (notably in i.MX series chips) violate
1161 * this part of the JTAG spec, so their capture mask/value
1162 * attributes might disable this test.
1163 */
1164 val = buf_get_u32(ir_test, chain_pos, tap->ir_length);
1165 if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
1166 LOG_ERROR("%s: IR capture error; saw 0x%0*x not 0x%0*x",
1167 jtag_tap_name(tap),
1168 (tap->ir_length + 7) / tap->ir_length,
1169 val,
1170 (tap->ir_length + 7) / tap->ir_length,
1171 (unsigned) tap->ir_capture_value);
1172
1173 retval = ERROR_JTAG_INIT_FAILED;
1174 goto done;
1175 }
1176 LOG_DEBUG("%s: IR capture 0x%0*x", jtag_tap_name(tap),
1177 (tap->ir_length + 7) / tap->ir_length, val);
1178 chain_pos += tap->ir_length;
1179 }
1180
1181 /* verify the '11' sentinel we wrote is returned at the end */
1182 val = buf_get_u32(ir_test, chain_pos, 2);
1183 if (val != 0x3)
1184 {
1185 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1186
1187 LOG_ERROR("IR capture error at bit %d, saw 0x%s not 0x...3",
1188 chain_pos, cbuf);
1189 free(cbuf);
1190 retval = ERROR_JTAG_INIT_FAILED;
1191 }
1192
1193 done:
1194 free(ir_test);
1195 if (retval != ERROR_OK) {
1196 jtag_add_tlr();
1197 jtag_execute_queue();
1198 }
1199 return retval;
1200 }
1201
1202
1203 void jtag_tap_init(jtag_tap_t *tap)
1204 {
1205 assert(0 != tap->ir_length);
1206
1207 /// @todo fix, this allocates one byte per bit for all three fields!
1208 tap->expected = malloc(tap->ir_length);
1209 tap->expected_mask = malloc(tap->ir_length);
1210 tap->cur_instr = malloc(tap->ir_length);
1211
1212 /// @todo cope sanely with ir_length bigger than 32 bits
1213 buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1214 buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1215 buf_set_ones(tap->cur_instr, tap->ir_length);
1216
1217 // place TAP in bypass mode
1218 tap->bypass = 1;
1219 // register the reset callback for the TAP
1220 jtag_register_event_callback(&jtag_reset_callback, tap);
1221
1222 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1223 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1224 tap->abs_chain_position, tap->ir_length,
1225 (unsigned) tap->ir_capture_value,
1226 (unsigned) tap->ir_capture_mask);
1227 jtag_tap_add(tap);
1228 }
1229
1230 void jtag_tap_free(jtag_tap_t *tap)
1231 {
1232 jtag_unregister_event_callback(&jtag_reset_callback, tap);
1233
1234 /// @todo is anything missing? no memory leaks please
1235 free((void *)tap->expected);
1236 free((void *)tap->expected_ids);
1237 free((void *)tap->chip);
1238 free((void *)tap->tapname);
1239 free((void *)tap->dotted_name);
1240 free(tap);
1241 }
1242
1243 int jtag_interface_init(struct command_context_s *cmd_ctx)
1244 {
1245 if (jtag)
1246 return ERROR_OK;
1247
1248 if (!jtag_interface)
1249 {
1250 /* nothing was previously specified by "interface" command */
1251 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1252 return ERROR_JTAG_INVALID_INTERFACE;
1253 }
1254
1255 jtag = jtag_interface;
1256 if (jtag_interface->init() != ERROR_OK)
1257 {
1258 jtag = NULL;
1259 return ERROR_JTAG_INIT_FAILED;
1260 }
1261
1262 int requested_khz = jtag_get_speed_khz();
1263 int actual_khz = requested_khz;
1264 int retval = jtag_get_speed_readable(&actual_khz);
1265 if (ERROR_OK != retval)
1266 LOG_INFO("interface specific clock speed value %d", jtag_get_speed());
1267 else if (actual_khz)
1268 {
1269 if ((CLOCK_MODE_RCLK == clock_mode)
1270 || ((CLOCK_MODE_KHZ == clock_mode) && !requested_khz))
1271 {
1272 LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
1273 , actual_khz);
1274 }
1275 else
1276 LOG_INFO("clock speed %d kHz", actual_khz);
1277 }
1278 else
1279 LOG_INFO("RCLK (adaptive clock speed)");
1280
1281 return ERROR_OK;
1282 }
1283
1284 int jtag_init_inner(struct command_context_s *cmd_ctx)
1285 {
1286 jtag_tap_t *tap;
1287 int retval;
1288 bool issue_setup = true;
1289
1290 LOG_DEBUG("Init JTAG chain");
1291
1292 tap = jtag_tap_next_enabled(NULL);
1293 if (tap == NULL) {
1294 LOG_ERROR("There are no enabled taps?");
1295 return ERROR_JTAG_INIT_FAILED;
1296 }
1297
1298 jtag_add_tlr();
1299 if ((retval = jtag_execute_queue()) != ERROR_OK)
1300 return retval;
1301
1302 /* Examine DR values first. This discovers problems which will
1303 * prevent communication ... hardware issues like TDO stuck, or
1304 * configuring the wrong number of (enabled) TAPs.
1305 */
1306 retval = jtag_examine_chain();
1307 switch (retval) {
1308 case ERROR_OK:
1309 /* complete success */
1310 break;
1311 case ERROR_JTAG_INIT_SOFT_FAIL:
1312 /* For backward compatibility reasons, try coping with
1313 * configuration errors involving only ID mismatches.
1314 * We might be able to talk to the devices.
1315 */
1316 LOG_ERROR("Trying to use configured scan chain anyway...");
1317 issue_setup = false;
1318 break;
1319 default:
1320 /* some hard error; already issued diagnostics */
1321 return retval;
1322 }
1323
1324 /* Now look at IR values. Problems here will prevent real
1325 * communication. They mostly mean that the IR length is
1326 * wrong ... or that the IR capture value is wrong. (The
1327 * latter is uncommon, but easily worked around: provide
1328 * ircapture/irmask values during TAP setup.)
1329 */
1330 retval = jtag_validate_ircapture();
1331 if (retval != ERROR_OK)
1332 return retval;
1333
1334 if (issue_setup)
1335 jtag_notify_event(JTAG_TAP_EVENT_SETUP);
1336 else
1337 LOG_WARNING("Bypassing JTAG setup events due to errors");
1338
1339
1340 return ERROR_OK;
1341 }
1342
1343 int jtag_interface_quit(void)
1344 {
1345 if (!jtag || !jtag->quit)
1346 return ERROR_OK;
1347
1348 // close the JTAG interface
1349 int result = jtag->quit();
1350 if (ERROR_OK != result)
1351 LOG_ERROR("failed: %d", result);
1352
1353 return ERROR_OK;
1354 }
1355
1356
1357 int jtag_init_reset(struct command_context_s *cmd_ctx)
1358 {
1359 int retval;
1360
1361 if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1362 return retval;
1363
1364 LOG_DEBUG("Initializing with hard TRST+SRST reset");
1365
1366 /*
1367 * This procedure is used by default when OpenOCD triggers a reset.
1368 * It's now done through an overridable Tcl "init_reset" wrapper.
1369 *
1370 * This started out as a more powerful "get JTAG working" reset than
1371 * jtag_init_inner(), applying TRST because some chips won't activate
1372 * JTAG without a TRST cycle (presumed to be async, though some of
1373 * those chips synchronize JTAG activation using TCK).
1374 *
1375 * But some chips only activate JTAG as part of an SRST cycle; SRST
1376 * got mixed in. So it became a hard reset routine, which got used
1377 * in more places, and which coped with JTAG reset being forced as
1378 * part of SRST (srst_pulls_trst).
1379 *
1380 * And even more corner cases started to surface: TRST and/or SRST
1381 * assertion timings matter; some chips need other JTAG operations;
1382 * TRST/SRST sequences can need to be different from these, etc.
1383 *
1384 * Systems should override that wrapper to support system-specific
1385 * requirements that this not-fully-generic code doesn't handle.
1386 *
1387 * REVISIT once Tcl code can read the reset_config modes, this won't
1388 * need to be a C routine at all...
1389 */
1390 jtag_add_reset(1, 0); /* TAP_RESET, using TMS+TCK or TRST */
1391 if (jtag_reset_config & RESET_HAS_SRST)
1392 {
1393 jtag_add_reset(1, 1);
1394 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1395 jtag_add_reset(0, 1);
1396 }
1397 jtag_add_reset(0, 0);
1398 if ((retval = jtag_execute_queue()) != ERROR_OK)
1399 return retval;
1400
1401 /* Check that we can communication on the JTAG chain + eventually we want to
1402 * be able to perform enumeration only after OpenOCD has started
1403 * telnet and GDB server
1404 *
1405 * That would allow users to more easily perform any magic they need to before
1406 * reset happens.
1407 */
1408 return jtag_init_inner(cmd_ctx);
1409 }
1410
1411 int jtag_init(struct command_context_s *cmd_ctx)
1412 {
1413 int retval;
1414
1415 if ((retval = jtag_interface_init(cmd_ctx)) != ERROR_OK)
1416 return retval;
1417
1418 /* guard against oddball hardware: force resets to be inactive */
1419 jtag_add_reset(0, 0);
1420 if ((retval = jtag_execute_queue()) != ERROR_OK)
1421 return retval;
1422
1423 if (Jim_Eval_Named(interp, "jtag_init", __FILE__, __LINE__) != JIM_OK)
1424 return ERROR_FAIL;
1425
1426 return ERROR_OK;
1427 }
1428
1429 unsigned jtag_get_speed_khz(void)
1430 {
1431 return speed_khz;
1432 }
1433
1434 static int jtag_khz_to_speed(unsigned khz, int* speed)
1435 {
1436 LOG_DEBUG("convert khz to interface specific speed value");
1437 speed_khz = khz;
1438 if (jtag != NULL)
1439 {
1440 LOG_DEBUG("have interface set up");
1441 int speed_div1;
1442 int retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
1443 if (ERROR_OK != retval)
1444 {
1445 return retval;
1446 }
1447 *speed = speed_div1;
1448 }
1449 return ERROR_OK;
1450 }
1451
1452 static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int* speed)
1453 {
1454 int retval = jtag_khz_to_speed(0, speed);
1455 if ((ERROR_OK != retval) && fallback_speed_khz)
1456 {
1457 LOG_DEBUG("trying fallback speed...");
1458 retval = jtag_khz_to_speed(fallback_speed_khz, speed);
1459 }
1460 return retval;
1461 }
1462
1463 static int jtag_set_speed(int speed)
1464 {
1465 jtag_speed = speed;
1466 /* this command can be called during CONFIG,
1467 * in which case jtag isn't initialized */
1468 return jtag ? jtag->speed(speed) : ERROR_OK;
1469 }
1470
1471 int jtag_config_speed(int speed)
1472 {
1473 LOG_DEBUG("handle jtag speed");
1474 clock_mode = CLOCK_MODE_SPEED;
1475 return jtag_set_speed(speed);
1476 }
1477
1478 int jtag_config_khz(unsigned khz)
1479 {
1480 LOG_DEBUG("handle jtag khz");
1481 clock_mode = CLOCK_MODE_KHZ;
1482 int speed = 0;
1483 int retval = jtag_khz_to_speed(khz, &speed);
1484 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1485 }
1486
1487 int jtag_config_rclk(unsigned fallback_speed_khz)
1488 {
1489 LOG_DEBUG("handle jtag rclk");
1490 clock_mode = CLOCK_MODE_RCLK;
1491 rclk_fallback_speed_khz = fallback_speed_khz;
1492 int speed = 0;
1493 int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
1494 return (ERROR_OK != retval) ? retval : jtag_set_speed(speed);
1495 }
1496
1497 int jtag_get_speed(void)
1498 {
1499 int speed;
1500 switch(clock_mode)
1501 {
1502 case CLOCK_MODE_SPEED:
1503 speed = jtag_speed;
1504 break;
1505 case CLOCK_MODE_KHZ:
1506 jtag_khz_to_speed(jtag_get_speed_khz(), &speed);
1507 break;
1508 case CLOCK_MODE_RCLK:
1509 jtag_rclk_to_speed(rclk_fallback_speed_khz, &speed);
1510 break;
1511 default:
1512 LOG_ERROR("BUG: unknown jtag clock mode");
1513 speed = 0;
1514 break;
1515 }
1516 return speed;
1517 }
1518
1519 int jtag_get_speed_readable(int *khz)
1520 {
1521 return jtag ? jtag->speed_div(jtag_get_speed(), khz) : ERROR_OK;
1522 }
1523
1524 void jtag_set_verify(bool enable)
1525 {
1526 jtag_verify = enable;
1527 }
1528
1529 bool jtag_will_verify()
1530 {
1531 return jtag_verify;
1532 }
1533
1534 void jtag_set_verify_capture_ir(bool enable)
1535 {
1536 jtag_verify_capture_ir = enable;
1537 }
1538
1539 bool jtag_will_verify_capture_ir()
1540 {
1541 return jtag_verify_capture_ir;
1542 }
1543
1544 int jtag_power_dropout(int *dropout)
1545 {
1546 return jtag->power_dropout(dropout);
1547 }
1548
1549 int jtag_srst_asserted(int *srst_asserted)
1550 {
1551 return jtag->srst_asserted(srst_asserted);
1552 }
1553
1554 enum reset_types jtag_get_reset_config(void)
1555 {
1556 return jtag_reset_config;
1557 }
1558 void jtag_set_reset_config(enum reset_types type)
1559 {
1560 jtag_reset_config = type;
1561 }
1562
1563 int jtag_get_trst(void)
1564 {
1565 return jtag_trst;
1566 }
1567 int jtag_get_srst(void)
1568 {
1569 return jtag_srst;
1570 }
1571
1572 void jtag_set_nsrst_delay(unsigned delay)
1573 {
1574 jtag_nsrst_delay = delay;
1575 }
1576 unsigned jtag_get_nsrst_delay(void)
1577 {
1578 return jtag_nsrst_delay;
1579 }
1580 void jtag_set_ntrst_delay(unsigned delay)
1581 {
1582 jtag_ntrst_delay = delay;
1583 }
1584 unsigned jtag_get_ntrst_delay(void)
1585 {
1586 return jtag_ntrst_delay;
1587 }
1588
1589
1590 void jtag_set_nsrst_assert_width(unsigned delay)
1591 {
1592 jtag_nsrst_assert_width = delay;
1593 }
1594 unsigned jtag_get_nsrst_assert_width(void)
1595 {
1596 return jtag_nsrst_assert_width;
1597 }
1598 void jtag_set_ntrst_assert_width(unsigned delay)
1599 {
1600 jtag_ntrst_assert_width = delay;
1601 }
1602 unsigned jtag_get_ntrst_assert_width(void)
1603 {
1604 return jtag_ntrst_assert_width;
1605 }

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)