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

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)