Encapsulate the jtag_event_callback list; add helper functions if needed.
[openocd.git] / src / jtag / core.c
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Ø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) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
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 /* note that this is not marked as static as it must be available from outside core.c for those
50 that implement the jtag_xxx() minidriver layer
51 */
52 int jtag_error=ERROR_OK;
53
54 char* jtag_event_strings[] =
55 {
56 "JTAG controller reset (RESET or TRST)"
57 };
58
59 const Jim_Nvp nvp_jtag_tap_event[] = {
60 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
61 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
62
63 { .name = NULL, .value = -1 }
64 };
65
66 static int jtag_trst = 0;
67 static int jtag_srst = 0;
68
69 /**
70 * List all TAPs that have been created.
71 */
72 static jtag_tap_t *__jtag_all_taps = NULL;
73 /**
74 * The number of TAPs in the __jtag_all_taps list, used to track the
75 * assigned chain position to new TAPs
76 */
77 static unsigned jtag_num_taps = 0;
78
79 enum reset_types jtag_reset_config = RESET_NONE;
80 tap_state_t cmd_queue_end_state = TAP_RESET;
81 tap_state_t cmd_queue_cur_state = TAP_RESET;
82
83 int jtag_verify_capture_ir = 1;
84 int jtag_verify = 1;
85
86 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
87 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
88 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
89
90 /* callbacks to inform high-level handlers about JTAG state changes */
91 static jtag_event_callback_t *jtag_event_callbacks;
92
93 /* speed in kHz*/
94 static int speed_khz = 0;
95 /* flag if the kHz speed was defined */
96 bool hasKHz = false;
97
98 struct jtag_interface_s *jtag = NULL;
99
100 /* configuration */
101 jtag_interface_t *jtag_interface = NULL;
102 int jtag_speed = 0;
103
104 jtag_tap_t *jtag_all_taps(void)
105 {
106 return __jtag_all_taps;
107 };
108
109 unsigned jtag_tap_count(void)
110 {
111 return jtag_num_taps;
112 }
113
114 unsigned jtag_tap_count_enabled(void)
115 {
116 jtag_tap_t *t = jtag_all_taps();
117 unsigned n = 0;
118 while(t)
119 {
120 if (t->enabled)
121 n++;
122 t = t->next_tap;
123 }
124 return n;
125 }
126
127 /// Append a new TAP to the chain of all taps.
128 void jtag_tap_add(struct jtag_tap_s *t)
129 {
130 t->abs_chain_position = jtag_num_taps++;
131
132 jtag_tap_t **tap = &__jtag_all_taps;
133 while(*tap != NULL)
134 tap = &(*tap)->next_tap;
135 *tap = t;
136 }
137
138 jtag_tap_t *jtag_tap_by_string(const char *s)
139 {
140 /* try by name first */
141 jtag_tap_t *t = jtag_all_taps();
142 while (t)
143 {
144 if (0 == strcmp(t->dotted_name, s))
145 return t;
146 t = t->next_tap;
147 }
148
149 /* no tap found by name, so try to parse the name as a number */
150 char *cp;
151 unsigned n = strtoul(s, &cp, 0);
152 if ((s == cp) || (*cp != 0))
153 return NULL;
154
155 return jtag_tap_by_position(n);
156 }
157
158 jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
159 {
160 const char *cp = Jim_GetString(o, NULL);
161 jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
162 if (NULL == cp)
163 cp = "(unknown)";
164 if (NULL == t)
165 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
166 return t;
167 }
168
169 /* returns a pointer to the n-th device in the scan chain */
170 jtag_tap_t *jtag_tap_by_position(unsigned n)
171 {
172 jtag_tap_t *t = jtag_all_taps();
173
174 while (t && n-- > 0)
175 t = t->next_tap;
176
177 return t;
178 }
179
180 const char *jtag_tap_name(const jtag_tap_t *tap)
181 {
182 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
183 }
184
185
186 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
187 {
188 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
189
190 if (callback == NULL)
191 {
192 return ERROR_INVALID_ARGUMENTS;
193 }
194
195 if (*callbacks_p)
196 {
197 while ((*callbacks_p)->next)
198 callbacks_p = &((*callbacks_p)->next);
199 callbacks_p = &((*callbacks_p)->next);
200 }
201
202 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
203 (*callbacks_p)->callback = callback;
204 (*callbacks_p)->priv = priv;
205 (*callbacks_p)->next = NULL;
206
207 return ERROR_OK;
208 }
209
210 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
211 {
212 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
213
214 if (callback == NULL)
215 {
216 return ERROR_INVALID_ARGUMENTS;
217 }
218
219 while (*callbacks_p)
220 {
221 jtag_event_callback_t **next = &((*callbacks_p)->next);
222 if ((*callbacks_p)->callback == callback)
223 {
224 free(*callbacks_p);
225 *callbacks_p = *next;
226 }
227 callbacks_p = next;
228 }
229
230 return ERROR_OK;
231 }
232
233 int jtag_call_event_callbacks(enum jtag_event event)
234 {
235 jtag_event_callback_t *callback = jtag_event_callbacks;
236
237 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
238
239 while (callback)
240 {
241 callback->callback(event, callback->priv);
242 callback = callback->next;
243 }
244
245 return ERROR_OK;
246 }
247
248 static void jtag_checks(void)
249 {
250 assert(jtag_trst == 0);
251 }
252
253 static void jtag_prelude(tap_state_t state)
254 {
255 jtag_checks();
256
257 assert(state!=TAP_INVALID);
258
259 cmd_queue_cur_state = state;
260 }
261
262 void jtag_alloc_in_value32(scan_field_t *field)
263 {
264 interface_jtag_alloc_in_value32(field);
265 }
266
267 void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
268 tap_state_t state)
269 {
270 jtag_prelude(state);
271
272 int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
273 jtag_set_error(retval);
274 }
275
276
277 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
278 {
279 if (jtag_verify&&jtag_verify_capture_ir)
280 {
281 /* 8 x 32 bit id's is enough for all invocations */
282
283 for (int j = 0; j < in_num_fields; j++)
284 {
285 /* if we are to run a verification of the ir scan, we need to get the input back.
286 * We may have to allocate space if the caller didn't ask for the input back.
287 */
288 in_fields[j].check_value=in_fields[j].tap->expected;
289 in_fields[j].check_mask=in_fields[j].tap->expected_mask;
290 }
291 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
292 } else
293 {
294 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
295 }
296 }
297
298 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
299 tap_state_t state)
300 {
301 jtag_prelude(state);
302
303 int retval = interface_jtag_add_plain_ir_scan(
304 in_num_fields, in_fields, state);
305 jtag_set_error(retval);
306 }
307
308 void jtag_add_callback(jtag_callback1_t f, u8 *in)
309 {
310 interface_jtag_add_callback(f, in);
311 }
312
313 void jtag_add_callback4(jtag_callback_t f, u8 *in,
314 jtag_callback_data_t data1, jtag_callback_data_t data2,
315 jtag_callback_data_t data3)
316 {
317 interface_jtag_add_callback4(f, in, data1, data2, data3);
318 }
319
320 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);
321
322 static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
323 {
324 return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
325 }
326
327 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
328 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
329 {
330 for (int i = 0; i < in_num_fields; i++)
331 {
332 struct scan_field_s *field = &in_fields[i];
333 field->allocated = 0;
334 field->modified = 0;
335 if (field->check_value || field->in_value)
336 continue;
337 interface_jtag_add_scan_check_alloc(field);
338 field->modified = 1;
339 }
340
341 jtag_add_scan(in_num_fields, in_fields, state);
342
343 for (int i = 0; i < in_num_fields; i++)
344 {
345 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
346 {
347 /* this is synchronous for a minidriver */
348 jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
349 (jtag_callback_data_t)in_fields[i].check_value,
350 (jtag_callback_data_t)in_fields[i].check_mask,
351 (jtag_callback_data_t)in_fields[i].num_bits);
352 }
353 if (in_fields[i].allocated)
354 {
355 free(in_fields[i].in_value);
356 }
357 if (in_fields[i].modified)
358 {
359 in_fields[i].in_value = NULL;
360 }
361 }
362 }
363
364 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
365 {
366 if (jtag_verify)
367 {
368 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
369 } else
370 {
371 jtag_add_dr_scan(in_num_fields, in_fields, state);
372 }
373 }
374
375
376 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
377 tap_state_t state)
378 {
379 jtag_prelude(state);
380
381 int retval;
382 retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
383 jtag_set_error(retval);
384 }
385
386 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
387 tap_state_t state)
388 {
389 jtag_prelude(state);
390
391 int retval;
392 retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
393 jtag_set_error(retval);
394 }
395
396 void jtag_add_dr_out(jtag_tap_t* tap,
397 int num_fields, const int* num_bits, const u32* value,
398 tap_state_t end_state)
399 {
400 assert(end_state != TAP_INVALID);
401
402 cmd_queue_cur_state = end_state;
403
404 interface_jtag_add_dr_out(tap,
405 num_fields, num_bits, value,
406 end_state);
407 }
408
409 void jtag_add_tlr(void)
410 {
411 jtag_prelude(TAP_RESET);
412 jtag_set_error(interface_jtag_add_tlr());
413 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
414 }
415
416 void jtag_add_pathmove(int num_states, const tap_state_t *path)
417 {
418 tap_state_t cur_state = cmd_queue_cur_state;
419
420 /* the last state has to be a stable state */
421 if (!tap_is_state_stable(path[num_states - 1]))
422 {
423 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
424 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
425 return;
426 }
427
428 for (int i = 0; i < num_states; i++)
429 {
430 if (path[i] == TAP_RESET)
431 {
432 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
433 jtag_set_error(ERROR_JTAG_STATE_INVALID);
434 return;
435 }
436
437 if ( tap_state_transition(cur_state, true) != path[i]
438 && tap_state_transition(cur_state, false) != path[i])
439 {
440 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
441 tap_state_name(cur_state), tap_state_name(path[i]));
442 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
443 return;
444 }
445 cur_state = path[i];
446 }
447
448 jtag_checks();
449
450 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
451 cmd_queue_cur_state = path[num_states - 1];
452 }
453
454 void jtag_add_runtest(int num_cycles, tap_state_t state)
455 {
456 jtag_prelude(state);
457 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
458 }
459
460
461 void jtag_add_clocks(int num_cycles)
462 {
463 if (!tap_is_state_stable(cmd_queue_cur_state))
464 {
465 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
466 tap_state_name(cmd_queue_cur_state));
467 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
468 return;
469 }
470
471 if (num_cycles > 0)
472 {
473 jtag_checks();
474 jtag_set_error(interface_jtag_add_clocks(num_cycles));
475 }
476 }
477
478 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
479 {
480 int trst_with_tlr = 0;
481
482 /* FIX!!! there are *many* different cases here. A better
483 * approach is needed for legal combinations of transitions...
484 */
485 if ((jtag_reset_config & RESET_HAS_SRST)&&
486 (jtag_reset_config & RESET_HAS_TRST)&&
487 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
488 {
489 if (((req_tlr_or_trst&&!jtag_trst)||
490 (!req_tlr_or_trst&&jtag_trst))&&
491 ((req_srst&&!jtag_srst)||
492 (!req_srst&&jtag_srst)))
493 {
494 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
495 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
496 }
497 }
498
499 /* Make sure that jtag_reset_config allows the requested reset */
500 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
501 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
502 {
503 LOG_ERROR("BUG: requested reset would assert trst");
504 jtag_set_error(ERROR_FAIL);
505 return;
506 }
507
508 /* if TRST pulls SRST, we reset with TAP T-L-R */
509 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
510 {
511 trst_with_tlr = 1;
512 }
513
514 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
515 {
516 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
517 jtag_set_error(ERROR_FAIL);
518 return;
519 }
520
521 if (req_tlr_or_trst)
522 {
523 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
524 {
525 jtag_trst = 1;
526 } else
527 {
528 trst_with_tlr = 1;
529 }
530 } else
531 {
532 jtag_trst = 0;
533 }
534
535 jtag_srst = req_srst;
536
537 int retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
538 if (retval != ERROR_OK)
539 {
540 jtag_set_error(retval);
541 return;
542 }
543 jtag_execute_queue();
544
545 if (jtag_srst)
546 {
547 LOG_DEBUG("SRST line asserted");
548 }
549 else
550 {
551 LOG_DEBUG("SRST line released");
552 if (jtag_nsrst_delay)
553 jtag_add_sleep(jtag_nsrst_delay * 1000);
554 }
555
556 if (trst_with_tlr)
557 {
558 LOG_DEBUG("JTAG reset with RESET instead of TRST");
559 jtag_set_end_state(TAP_RESET);
560 jtag_add_tlr();
561 return;
562 }
563
564 if (jtag_trst)
565 {
566 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
567 * and inform possible listeners about this
568 */
569 LOG_DEBUG("TRST line asserted");
570 tap_set_state(TAP_RESET);
571 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
572 }
573 else
574 {
575 if (jtag_ntrst_delay)
576 jtag_add_sleep(jtag_ntrst_delay * 1000);
577 }
578 }
579
580 tap_state_t jtag_set_end_state(tap_state_t state)
581 {
582 if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
583 {
584 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
585 }
586
587 if (state!=TAP_INVALID)
588 cmd_queue_end_state = state;
589 return cmd_queue_end_state;
590 }
591
592 tap_state_t jtag_get_end_state(void)
593 {
594 return cmd_queue_end_state;
595 }
596
597 void jtag_add_sleep(u32 us)
598 {
599 /// @todo Here, keep_alive() appears to be a layering violation!!!
600 keep_alive();
601 jtag_set_error(interface_jtag_add_sleep(us));
602 }
603
604 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
605 {
606 int retval = ERROR_OK;
607
608 int compare_failed = 0;
609
610 if (in_check_mask)
611 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
612 else
613 compare_failed = buf_cmp(captured, in_check_value, num_bits);
614
615 if (compare_failed){
616 /* An error handler could have caught the failing check
617 * only report a problem when there wasn't a handler, or if the handler
618 * acknowledged the error
619 */
620 /*
621 LOG_WARNING("TAP %s:",
622 jtag_tap_name(field->tap));
623 */
624 if (compare_failed)
625 {
626 char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
627 char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
628
629 if (in_check_mask)
630 {
631 char *in_check_mask_char;
632 in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
633 LOG_WARNING("value captured during scan didn't pass the requested check:");
634 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
635 captured_char, in_check_value_char, in_check_mask_char);
636 free(in_check_mask_char);
637 }
638 else
639 {
640 LOG_WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s", captured_char, in_check_value_char);
641 }
642
643 free(captured_char);
644 free(in_check_value_char);
645
646 retval = ERROR_JTAG_QUEUE_FAILED;
647 }
648
649 }
650 return retval;
651 }
652
653 void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
654 {
655 assert(field->in_value != NULL);
656
657 if (value==NULL)
658 {
659 /* no checking to do */
660 return;
661 }
662
663 jtag_execute_queue_noclear();
664
665 int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
666 jtag_set_error(retval);
667 }
668
669
670
671 int default_interface_jtag_execute_queue(void)
672 {
673 if (NULL == jtag)
674 {
675 LOG_ERROR("No JTAG interface configured yet. "
676 "Issue 'init' command in startup scripts "
677 "before communicating with targets.");
678 return ERROR_FAIL;
679 }
680
681 return jtag->execute_queue();
682 }
683
684 void jtag_execute_queue_noclear(void)
685 {
686 jtag_flush_queue_count++;
687 jtag_set_error(interface_jtag_execute_queue());
688 }
689
690 int jtag_get_flush_queue_count(void)
691 {
692 return jtag_flush_queue_count;
693 }
694
695 int jtag_execute_queue(void)
696 {
697 jtag_execute_queue_noclear();
698 return jtag_error_clear();
699 }
700
701 static int jtag_reset_callback(enum jtag_event event, void *priv)
702 {
703 jtag_tap_t *tap = priv;
704
705 LOG_DEBUG("-");
706
707 if (event == JTAG_TRST_ASSERTED)
708 {
709 buf_set_ones(tap->cur_instr, tap->ir_length);
710 tap->bypass = 1;
711 }
712
713 return ERROR_OK;
714 }
715
716 void jtag_sleep(u32 us)
717 {
718 alive_sleep(us/1000);
719 }
720
721 /// maximum number of JTAG devices expected in the chain
722 #define JTAG_MAX_CHAIN_SIZE 20
723
724 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
725 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
726 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
727
728 static int jtag_examine_chain_execute(u8 *idcode_buffer, unsigned num_idcode)
729 {
730 scan_field_t field = {
731 .tap = NULL,
732 .num_bits = num_idcode * 32,
733 .out_value = idcode_buffer,
734 .in_value = idcode_buffer,
735 };
736
737 // initialize to the end of chain ID value
738 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
739 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
740
741 jtag_add_plain_dr_scan(1, &field, TAP_RESET);
742 return jtag_execute_queue();
743 }
744
745 static bool jtag_examine_chain_check(u8 *idcodes, unsigned count)
746 {
747 u8 zero_check = 0x0;
748 u8 one_check = 0xff;
749
750 for (unsigned i = 0; i < count * 4; i++)
751 {
752 zero_check |= idcodes[i];
753 one_check &= idcodes[i];
754 }
755
756 /* if there wasn't a single non-zero bit or if all bits were one,
757 * the scan is not valid */
758 if (zero_check == 0x00 || one_check == 0xff)
759 {
760 LOG_ERROR("JTAG communication failure: check connection, "
761 "JTAG interface, target power etc.");
762 return false;
763 }
764 return true;
765 }
766
767 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
768 const char *name, u32 idcode)
769 {
770 log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
771 "JTAG tap: %s %16.16s: 0x%08x "
772 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
773 name, msg, idcode,
774 EXTRACT_MFG(idcode), EXTRACT_PART(idcode), EXTRACT_VER(idcode) );
775 }
776
777 static bool jtag_idcode_is_final(u32 idcode)
778 {
779 return idcode == 0x000000FF || idcode == 0xFFFFFFFF;
780 }
781
782 /**
783 * This helper checks that remaining bits in the examined chain data are
784 * all as expected, but a single JTAG device requires only 64 bits to be
785 * read back correctly. This can help identify and diagnose problems
786 * with the JTAG chain earlier, gives more helpful/explicit error messages.
787 */
788 static void jtag_examine_chain_end(u8 *idcodes, unsigned count, unsigned max)
789 {
790 bool triggered = false;
791 for ( ; count < max - 31; count += 32)
792 {
793 u32 idcode = buf_get_u32(idcodes, count, 32);
794 // do not trigger the warning if the data looks good
795 if (!triggered && jtag_idcode_is_final(idcode))
796 continue;
797 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
798 count, idcode);
799 triggered = true;
800 }
801 }
802
803 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
804 {
805 if (0 == tap->expected_ids_cnt)
806 {
807 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
808 #if 0
809 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap->idcode)
810 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
811 #endif
812 return true;
813 }
814
815 /* Loop over the expected identification codes and test for a match */
816 u8 ii;
817 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
818 {
819 if (tap->idcode == tap->expected_ids[ii])
820 break;
821 }
822
823 /* If none of the expected ids matched, log an error */
824 if (ii != tap->expected_ids_cnt)
825 {
826 LOG_INFO("JTAG Tap/device matched");
827 return true;
828 }
829 jtag_examine_chain_display(LOG_LVL_ERROR, "got",
830 tap->dotted_name, tap->idcode);
831 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
832 {
833 char msg[32];
834 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
835 ii + 1, tap->expected_ids_cnt);
836 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
837 tap->dotted_name, tap->expected_ids[ii]);
838 }
839 return false;
840 }
841
842 /* Try to examine chain layout according to IEEE 1149.1 §12
843 */
844 int jtag_examine_chain(void)
845 {
846 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
847 unsigned device_count = 0;
848
849 jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
850
851 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
852 return ERROR_JTAG_INIT_FAILED;
853
854 /* point at the 1st tap */
855 jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
856 if (tap == NULL)
857 {
858 LOG_ERROR("JTAG: No taps enabled?");
859 return ERROR_JTAG_INIT_FAILED;
860 }
861
862 for (unsigned bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
863 {
864 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
865 if ((idcode & 1) == 0)
866 {
867 /* LSB must not be 0, this indicates a device in bypass */
868 LOG_WARNING("Tap/Device does not have IDCODE");
869 idcode = 0;
870
871 bit_count += 1;
872 }
873 else
874 {
875 /*
876 * End of chain (invalid manufacturer ID) some devices, such
877 * as AVR will output all 1's instead of TDI input value at
878 * end of chain.
879 */
880 if (jtag_idcode_is_final(idcode))
881 {
882 jtag_examine_chain_end(idcode_buffer,
883 bit_count + 32, JTAG_MAX_CHAIN_SIZE * 32);
884 break;
885 }
886
887 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
888 tap ? tap->dotted_name : "(not-named)",
889 idcode);
890
891 bit_count += 32;
892 }
893 device_count++;
894 if (!tap)
895 continue;
896
897 tap->idcode = idcode;
898
899 // ensure the TAP ID does matches what was expected
900 if (!jtag_examine_chain_match_tap(tap))
901 return ERROR_JTAG_INIT_FAILED;
902
903 tap = jtag_tap_next_enabled(tap);
904 }
905
906 /* see if number of discovered devices matches configuration */
907 if (device_count != jtag_tap_count_enabled())
908 {
909 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
910 "does not match (enabled) configuration (%i), total taps: %d",
911 device_count, jtag_tap_count_enabled(), jtag_tap_count());
912 LOG_ERROR("check the config file and ensure proper JTAG communication"
913 " (connections, speed, ...)");
914 return ERROR_JTAG_INIT_FAILED;
915 }
916
917 return ERROR_OK;
918 }
919
920 int jtag_validate_chain(void)
921 {
922 jtag_tap_t *tap;
923 int total_ir_length = 0;
924 u8 *ir_test = NULL;
925 scan_field_t field;
926 int chain_pos = 0;
927
928 tap = NULL;
929 total_ir_length = 0;
930 for(;;){
931 tap = jtag_tap_next_enabled(tap);
932 if( tap == NULL ){
933 break;
934 }
935 total_ir_length += tap->ir_length;
936 }
937
938 total_ir_length += 2;
939 ir_test = malloc(CEIL(total_ir_length, 8));
940 buf_set_ones(ir_test, total_ir_length);
941
942 field.tap = NULL;
943 field.num_bits = total_ir_length;
944 field.out_value = ir_test;
945 field.in_value = ir_test;
946
947
948 jtag_add_plain_ir_scan(1, &field, TAP_RESET);
949 jtag_execute_queue();
950
951 tap = NULL;
952 chain_pos = 0;
953 int val;
954 for(;;){
955 tap = jtag_tap_next_enabled(tap);
956 if( tap == NULL ){
957 break;
958 }
959
960 val = buf_get_u32(ir_test, chain_pos, 2);
961 if (val != 0x1)
962 {
963 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
964 LOG_ERROR("Could not validate JTAG scan chain, IR mismatch, scan returned 0x%s. tap=%s pos=%d expected 0x1 got %0x", cbuf, jtag_tap_name(tap), chain_pos, val);
965 free(cbuf);
966 free(ir_test);
967 return ERROR_JTAG_INIT_FAILED;
968 }
969 chain_pos += tap->ir_length;
970 }
971
972 val = buf_get_u32(ir_test, chain_pos, 2);
973 if (val != 0x3)
974 {
975 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
976 LOG_ERROR("Could not validate end of JTAG scan chain, IR mismatch, scan returned 0x%s. pos=%d expected 0x3 got %0x", cbuf, chain_pos, val);
977 free(cbuf);
978 free(ir_test);
979 return ERROR_JTAG_INIT_FAILED;
980 }
981
982 free(ir_test);
983
984 return ERROR_OK;
985 }
986
987
988 void jtag_tap_init(jtag_tap_t *tap)
989 {
990 assert(0 != tap->ir_length);
991
992 tap->expected = malloc(tap->ir_length);
993 tap->expected_mask = malloc(tap->ir_length);
994 tap->cur_instr = malloc(tap->ir_length);
995
996 buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
997 buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
998 buf_set_ones(tap->cur_instr, tap->ir_length);
999
1000 // place TAP in bypass mode
1001 tap->bypass = 1;
1002 // register the reset callback for the TAP
1003 jtag_register_event_callback(&jtag_reset_callback, tap);
1004
1005 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1006 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1007 tap->abs_chain_position, tap->ir_length,
1008 tap->ir_capture_value, tap->ir_capture_mask);
1009 jtag_tap_add(tap);
1010 }
1011
1012 void jtag_tap_free(jtag_tap_t *tap)
1013 {
1014 /// @todo is anything missing? no memory leaks please
1015 free((void *)tap->expected_ids);
1016 free((void *)tap->chip);
1017 free((void *)tap->tapname);
1018 free((void *)tap->dotted_name);
1019 free(tap);
1020 }
1021
1022 int jtag_interface_init(struct command_context_s *cmd_ctx)
1023 {
1024 if (jtag)
1025 return ERROR_OK;
1026
1027 if (!jtag_interface)
1028 {
1029 /* nothing was previously specified by "interface" command */
1030 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1031 return ERROR_JTAG_INVALID_INTERFACE;
1032 }
1033 if(hasKHz)
1034 {
1035 jtag_interface->khz(jtag_get_speed_khz(), &jtag_speed);
1036 hasKHz = false;
1037 }
1038
1039 if (jtag_interface->init() != ERROR_OK)
1040 return ERROR_JTAG_INIT_FAILED;
1041
1042 jtag = jtag_interface;
1043 return ERROR_OK;
1044 }
1045
1046 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1047 {
1048 jtag_tap_t *tap;
1049 int retval;
1050
1051 LOG_DEBUG("Init JTAG chain");
1052
1053 tap = jtag_tap_next_enabled(NULL);
1054 if( tap == NULL ){
1055 LOG_ERROR("There are no enabled taps?");
1056 return ERROR_JTAG_INIT_FAILED;
1057 }
1058
1059 jtag_add_tlr();
1060 if ((retval=jtag_execute_queue())!=ERROR_OK)
1061 return retval;
1062
1063 /* examine chain first, as this could discover the real chain layout */
1064 if (jtag_examine_chain() != ERROR_OK)
1065 {
1066 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1067 }
1068
1069 if (jtag_validate_chain() != ERROR_OK)
1070 {
1071 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1072 }
1073
1074 return ERROR_OK;
1075 }
1076
1077 int jtag_interface_quit(void)
1078 {
1079 if (!jtag || !jtag->quit)
1080 return ERROR_OK;
1081
1082 // close the JTAG interface
1083 int result = jtag->quit();
1084 if (ERROR_OK != result)
1085 LOG_ERROR("failed: %d", result);
1086
1087 return ERROR_OK;
1088 }
1089
1090
1091 int jtag_init_reset(struct command_context_s *cmd_ctx)
1092 {
1093 int retval;
1094
1095 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1096 return retval;
1097
1098 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
1099
1100 /* Reset can happen after a power cycle.
1101 *
1102 * Ideally we would only assert TRST or run RESET before the target reset.
1103 *
1104 * However w/srst_pulls_trst, trst is asserted together with the target
1105 * reset whether we want it or not.
1106 *
1107 * NB! Some targets have JTAG circuitry disabled until a
1108 * trst & srst has been asserted.
1109 *
1110 * NB! here we assume nsrst/ntrst delay are sufficient!
1111 *
1112 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1113 *
1114 */
1115 jtag_add_reset(1, 0); /* RESET or TRST */
1116 if (jtag_reset_config & RESET_HAS_SRST)
1117 {
1118 jtag_add_reset(1, 1);
1119 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1120 jtag_add_reset(0, 1);
1121 }
1122 jtag_add_reset(0, 0);
1123 if ((retval = jtag_execute_queue()) != ERROR_OK)
1124 return retval;
1125
1126 /* Check that we can communication on the JTAG chain + eventually we want to
1127 * be able to perform enumeration only after OpenOCD has started
1128 * telnet and GDB server
1129 *
1130 * That would allow users to more easily perform any magic they need to before
1131 * reset happens.
1132 */
1133 return jtag_init_inner(cmd_ctx);
1134 }
1135
1136 int jtag_init(struct command_context_s *cmd_ctx)
1137 {
1138 int retval;
1139 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1140 return retval;
1141 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1142 {
1143 return ERROR_OK;
1144 }
1145 return jtag_init_reset(cmd_ctx);
1146 }
1147
1148 void jtag_set_speed_khz(unsigned khz)
1149 {
1150 speed_khz = khz;
1151 }
1152 unsigned jtag_get_speed_khz(void)
1153 {
1154 return speed_khz;
1155 }
1156
1157 void jtag_set_verify(bool enable)
1158 {
1159 jtag_verify = enable;
1160 }
1161
1162 bool jtag_will_verify()
1163 {
1164 return jtag_verify;
1165 }
1166
1167
1168 int jtag_power_dropout(int *dropout)
1169 {
1170 return jtag->power_dropout(dropout);
1171 }
1172
1173 int jtag_srst_asserted(int *srst_asserted)
1174 {
1175 return jtag->srst_asserted(srst_asserted);
1176 }
1177
1178 void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
1179 {
1180 jtag_tap_event_action_t * jteap;
1181 int done;
1182
1183 jteap = tap->event_action;
1184
1185 done = 0;
1186 while (jteap) {
1187 if (jteap->event == e) {
1188 done = 1;
1189 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
1190 tap->dotted_name,
1191 e,
1192 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
1193 Jim_GetString(jteap->body, NULL) );
1194 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
1195 Jim_PrintErrorMessage(interp);
1196 }
1197 }
1198
1199 jteap = jteap->next;
1200 }
1201
1202 if (!done) {
1203 LOG_DEBUG( "event %d %s - no action",
1204 e,
1205 Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
1206 }
1207 }
1208
1209 int jtag_add_statemove(tap_state_t goal_state)
1210 {
1211 tap_state_t cur_state = cmd_queue_cur_state;
1212
1213 LOG_DEBUG( "cur_state=%s goal_state=%s",
1214 tap_state_name(cur_state),
1215 tap_state_name(goal_state) );
1216
1217
1218 if (goal_state==cur_state )
1219 ; /* nothing to do */
1220 else if( goal_state==TAP_RESET )
1221 {
1222 jtag_add_tlr();
1223 }
1224 else if( tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state) )
1225 {
1226 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
1227 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
1228 tap_state_t moves[8];
1229 assert(tms_count < DIM(moves));
1230
1231 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
1232 {
1233 bool bit = tms_bits & 1;
1234
1235 cur_state = tap_state_transition(cur_state, bit);
1236 moves[i] = cur_state;
1237 }
1238
1239 jtag_add_pathmove(tms_count, moves);
1240 }
1241 else if( tap_state_transition(cur_state, true) == goal_state
1242 || tap_state_transition(cur_state, false) == goal_state )
1243 {
1244 jtag_add_pathmove(1, &goal_state);
1245 }
1246
1247 else
1248 return ERROR_FAIL;
1249
1250 return ERROR_OK;
1251 }
1252
1253 int jtag_get_trst(void)
1254 {
1255 return jtag_trst;
1256 }
1257 int jtag_get_srst(void)
1258 {
1259 return jtag_srst;
1260 }
1261
1262 void jtag_set_nsrst_delay(unsigned delay)
1263 {
1264 jtag_nsrst_delay = delay;
1265 }
1266 unsigned jtag_get_nsrst_delay(void)
1267 {
1268 return jtag_nsrst_delay;
1269 }
1270 void jtag_set_ntrst_delay(unsigned delay)
1271 {
1272 jtag_ntrst_delay = delay;
1273 }
1274 unsigned jtag_get_ntrst_delay(void)
1275 {
1276 return jtag_ntrst_delay;
1277 }

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)