Simplify jtag_add_reset:
[openocd.git] / src / jtag / jtag.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 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 * This program is distributed in the hope that it will be useful, *
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20 * GNU General Public License for more details. *
21 * *
22 * You should have received a copy of the GNU General Public License *
23 * along with this program; if not, write to the *
24 * Free Software Foundation, Inc., *
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
26 ***************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "jtag.h"
32 #include "minidriver.h"
33 #include "interface.h"
34
35 #ifdef HAVE_STRINGS_H
36 #include <strings.h>
37 #endif
38
39
40 /// The number of JTAG queue flushes (for profiling and debugging purposes).
41 static int jtag_flush_queue_count;
42
43 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
44 int in_num_fields, scan_field_t *in_fields, tap_state_t state);
45
46 /* note that this is not marked as static as it must be available from outside jtag.c for those
47 that implement the jtag_xxx() minidriver layer
48 */
49 int jtag_error=ERROR_OK;
50
51 char* jtag_event_strings[] =
52 {
53 "JTAG controller reset (RESET or TRST)"
54 };
55
56 const Jim_Nvp nvp_jtag_tap_event[] = {
57 { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
58 { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
59
60 { .name = NULL, .value = -1 }
61 };
62
63 int jtag_trst = 0;
64 int jtag_srst = 0;
65
66 /**
67 * List all TAPs that have been created.
68 */
69 static jtag_tap_t *__jtag_all_taps = NULL;
70 /**
71 * The number of TAPs in the __jtag_all_taps list, used to track the
72 * assigned chain position to new TAPs
73 */
74 static int jtag_num_taps = 0;
75
76 enum reset_types jtag_reset_config = RESET_NONE;
77 tap_state_t cmd_queue_end_state = TAP_RESET;
78 tap_state_t cmd_queue_cur_state = TAP_RESET;
79
80 int jtag_verify_capture_ir = 1;
81 int jtag_verify = 1;
82
83 /* how long the OpenOCD should wait before attempting JTAG communication after reset lines deasserted (in ms) */
84 static int jtag_nsrst_delay = 0; /* default to no nSRST delay */
85 static int jtag_ntrst_delay = 0; /* default to no nTRST delay */
86
87 /* callbacks to inform high-level handlers about JTAG state changes */
88 jtag_event_callback_t *jtag_event_callbacks;
89
90 /* speed in kHz*/
91 static int speed_khz = 0;
92 /* flag if the kHz speed was defined */
93 static bool hasKHz = false;
94
95 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
96 */
97
98 #if BUILD_ECOSBOARD == 1
99 extern jtag_interface_t zy1000_interface;
100 #elif defined(BUILD_MINIDRIVER_DUMMY)
101 extern jtag_interface_t minidummy_interface;
102 #else // standard drivers
103 #if BUILD_PARPORT == 1
104 extern jtag_interface_t parport_interface;
105 #endif
106
107 #if BUILD_DUMMY == 1
108 extern jtag_interface_t dummy_interface;
109 #endif
110
111 #if BUILD_FT2232_FTD2XX == 1
112 extern jtag_interface_t ft2232_interface;
113 #endif
114
115 #if BUILD_FT2232_LIBFTDI == 1
116 extern jtag_interface_t ft2232_interface;
117 #endif
118
119 #if BUILD_AMTJTAGACCEL == 1
120 extern jtag_interface_t amt_jtagaccel_interface;
121 #endif
122
123 #if BUILD_EP93XX == 1
124 extern jtag_interface_t ep93xx_interface;
125 #endif
126
127 #if BUILD_AT91RM9200 == 1
128 extern jtag_interface_t at91rm9200_interface;
129 #endif
130
131 #if BUILD_GW16012 == 1
132 extern jtag_interface_t gw16012_interface;
133 #endif
134
135 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
136 extern jtag_interface_t presto_interface;
137 #endif
138
139 #if BUILD_USBPROG == 1
140 extern jtag_interface_t usbprog_interface;
141 #endif
142
143 #if BUILD_JLINK == 1
144 extern jtag_interface_t jlink_interface;
145 #endif
146
147 #if BUILD_VSLLINK == 1
148 extern jtag_interface_t vsllink_interface;
149 #endif
150
151 #if BUILD_RLINK == 1
152 extern jtag_interface_t rlink_interface;
153 #endif
154
155 #if BUILD_ARMJTAGEW == 1
156 extern jtag_interface_t armjtagew_interface;
157 #endif
158 #endif // standard drivers
159
160 /**
161 * The list of built-in JTAG interfaces, containing entries for those
162 * drivers that were enabled by the @c configure script.
163 *
164 * The list should be defined to contain either one minidriver interface
165 * or some number of standard driver interfaces, never both.
166 */
167 jtag_interface_t *jtag_interfaces[] = {
168 #if BUILD_ECOSBOARD == 1
169 &zy1000_interface,
170 #elif defined(BUILD_MINIDRIVER_DUMMY)
171 &minidummy_interface,
172 #else // standard drivers
173 #if BUILD_PARPORT == 1
174 &parport_interface,
175 #endif
176 #if BUILD_DUMMY == 1
177 &dummy_interface,
178 #endif
179 #if BUILD_FT2232_FTD2XX == 1
180 &ft2232_interface,
181 #endif
182 #if BUILD_FT2232_LIBFTDI == 1
183 &ft2232_interface,
184 #endif
185 #if BUILD_AMTJTAGACCEL == 1
186 &amt_jtagaccel_interface,
187 #endif
188 #if BUILD_EP93XX == 1
189 &ep93xx_interface,
190 #endif
191 #if BUILD_AT91RM9200 == 1
192 &at91rm9200_interface,
193 #endif
194 #if BUILD_GW16012 == 1
195 &gw16012_interface,
196 #endif
197 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
198 &presto_interface,
199 #endif
200 #if BUILD_USBPROG == 1
201 &usbprog_interface,
202 #endif
203 #if BUILD_JLINK == 1
204 &jlink_interface,
205 #endif
206 #if BUILD_VSLLINK == 1
207 &vsllink_interface,
208 #endif
209 #if BUILD_RLINK == 1
210 &rlink_interface,
211 #endif
212 #if BUILD_ARMJTAGEW == 1
213 &armjtagew_interface,
214 #endif
215 #endif // standard drivers
216 NULL,
217 };
218
219 struct jtag_interface_s *jtag = NULL;
220
221 /* configuration */
222 static jtag_interface_t *jtag_interface = NULL;
223 int jtag_speed = 0;
224
225 /* jtag commands */
226 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
227 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
228 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
229 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
230 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
231 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
232 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
233
234 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
235
236 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
237 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
238 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
240 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
241
242 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
243 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
244 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245
246 jtag_tap_t *jtag_all_taps(void)
247 {
248 return __jtag_all_taps;
249 };
250
251 int jtag_tap_count(void)
252 {
253 return jtag_num_taps;
254 }
255
256 unsigned jtag_tap_count_enabled(void)
257 {
258 jtag_tap_t *t;
259 unsigned n;
260
261 n = 0;
262 t = jtag_all_taps();
263 while(t){
264 if( t->enabled ){
265 n++;
266 }
267 t = t->next_tap;
268 }
269 return n;
270 }
271
272 /// Append a new TAP to the chain of all taps.
273 static void jtag_tap_add(struct jtag_tap_s *t)
274 {
275 t->abs_chain_position = jtag_num_taps++;
276
277 jtag_tap_t **tap = &__jtag_all_taps;
278 while(*tap != NULL)
279 tap = &(*tap)->next_tap;
280 *tap = t;
281 }
282
283 jtag_tap_t *jtag_tap_by_string( const char *s )
284 {
285 jtag_tap_t *t;
286 char *cp;
287
288 t = jtag_all_taps();
289 /* try name first */
290 while(t){
291 if( 0 == strcmp( t->dotted_name, s ) ){
292 break;
293 } else {
294 t = t->next_tap;
295 }
296 }
297 /* backup plan is by number */
298 if( t == NULL ){
299 /* ok - is "s" a number? */
300 int n;
301 n = strtol( s, &cp, 0 );
302 if( (s != cp) && (*cp == 0) ){
303 /* Then it is... */
304 t = jtag_tap_by_abs_position(n);
305 }
306 }
307 return t;
308 }
309
310 jtag_tap_t * jtag_tap_by_jim_obj( Jim_Interp *interp, Jim_Obj *o )
311 {
312 jtag_tap_t *t;
313 const char *cp;
314
315 cp = Jim_GetString( o, NULL );
316 if(cp == NULL){
317 cp = "(unknown)";
318 t = NULL;
319 } else {
320 t = jtag_tap_by_string( cp );
321 }
322 if( t == NULL ){
323 Jim_SetResult_sprintf(interp,"Tap: %s is unknown", cp );
324 }
325 return t;
326 }
327
328 /* returns a pointer to the n-th device in the scan chain */
329 jtag_tap_t * jtag_tap_by_abs_position( int n )
330 {
331 int orig_n;
332 jtag_tap_t *t;
333
334 orig_n = n;
335 t = jtag_all_taps();
336
337 while( t && (n > 0)) {
338 n--;
339 t = t->next_tap;
340 }
341 return t;
342 }
343
344 const char *jtag_tap_name(const jtag_tap_t *tap)
345 {
346 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
347 }
348
349
350 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
351 {
352 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
353
354 if (callback == NULL)
355 {
356 return ERROR_INVALID_ARGUMENTS;
357 }
358
359 if (*callbacks_p)
360 {
361 while ((*callbacks_p)->next)
362 callbacks_p = &((*callbacks_p)->next);
363 callbacks_p = &((*callbacks_p)->next);
364 }
365
366 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
367 (*callbacks_p)->callback = callback;
368 (*callbacks_p)->priv = priv;
369 (*callbacks_p)->next = NULL;
370
371 return ERROR_OK;
372 }
373
374 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
375 {
376 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
377
378 if (callback == NULL)
379 {
380 return ERROR_INVALID_ARGUMENTS;
381 }
382
383 while (*callbacks_p)
384 {
385 jtag_event_callback_t **next = &((*callbacks_p)->next);
386 if ((*callbacks_p)->callback == callback)
387 {
388 free(*callbacks_p);
389 *callbacks_p = *next;
390 }
391 callbacks_p = next;
392 }
393
394 return ERROR_OK;
395 }
396
397 int jtag_call_event_callbacks(enum jtag_event event)
398 {
399 jtag_event_callback_t *callback = jtag_event_callbacks;
400
401 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
402
403 while (callback)
404 {
405 callback->callback(event, callback->priv);
406 callback = callback->next;
407 }
408
409 return ERROR_OK;
410 }
411
412 static void jtag_checks(void)
413 {
414 assert(jtag_trst == 0);
415 }
416
417 static void jtag_prelude(tap_state_t state)
418 {
419 jtag_checks();
420
421 assert(state!=TAP_INVALID);
422
423 cmd_queue_cur_state = state;
424 }
425
426 void jtag_alloc_in_value32(scan_field_t *field)
427 {
428 interface_jtag_alloc_in_value32(field);
429 }
430
431 void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
432 tap_state_t state)
433 {
434 jtag_prelude(state);
435
436 int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
437 jtag_set_error(retval);
438 }
439
440
441 /**
442 * Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
443 *
444 * If the input field list contains an instruction value for a TAP then that is used
445 * otherwise the TAP is set to bypass.
446 *
447 * TAPs for which no fields are passed are marked as bypassed for subsequent DR SCANs.
448 *
449 */
450 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
451 {
452 if (jtag_verify&&jtag_verify_capture_ir)
453 {
454 /* 8 x 32 bit id's is enough for all invocations */
455
456 for (int j = 0; j < in_num_fields; j++)
457 {
458 /* if we are to run a verification of the ir scan, we need to get the input back.
459 * We may have to allocate space if the caller didn't ask for the input back.
460 */
461 in_fields[j].check_value=in_fields[j].tap->expected;
462 in_fields[j].check_mask=in_fields[j].tap->expected_mask;
463 }
464 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
465 } else
466 {
467 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
468 }
469 }
470
471 /**
472 * Duplicate the scan fields passed into the function into an IR SCAN command
473 *
474 * This function assumes that the caller handles extra fields for bypassed TAPs
475 *
476 */
477 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
478 tap_state_t state)
479 {
480 jtag_prelude(state);
481
482 int retval = interface_jtag_add_plain_ir_scan(
483 in_num_fields, in_fields, state);
484 jtag_set_error(retval);
485 }
486
487 void jtag_add_callback(jtag_callback1_t f, u8 *in)
488 {
489 interface_jtag_add_callback(f, in);
490 }
491
492 void jtag_add_callback4(jtag_callback_t f, u8 *in,
493 jtag_callback_data_t data1, jtag_callback_data_t data2,
494 jtag_callback_data_t data3)
495 {
496 interface_jtag_add_callback4(f, in, data1, data2, data3);
497 }
498
499 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);
500
501 static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
502 {
503 return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
504 }
505
506 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
507 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
508 {
509 for (int i = 0; i < in_num_fields; i++)
510 {
511 struct scan_field_s *field = &in_fields[i];
512 field->allocated = 0;
513 field->modified = 0;
514 if (field->check_value || field->in_value)
515 continue;
516 interface_jtag_add_scan_check_alloc(field);
517 field->modified = 1;
518 }
519
520 jtag_add_scan(in_num_fields, in_fields, state);
521
522 for (int i = 0; i < in_num_fields; i++)
523 {
524 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
525 {
526 /* this is synchronous for a minidriver */
527 jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
528 (jtag_callback_data_t)in_fields[i].check_value,
529 (jtag_callback_data_t)in_fields[i].check_mask,
530 (jtag_callback_data_t)in_fields[i].num_bits);
531 }
532 if (in_fields[i].allocated)
533 {
534 free(in_fields[i].in_value);
535 }
536 if (in_fields[i].modified)
537 {
538 in_fields[i].in_value = NULL;
539 }
540 }
541 }
542
543 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
544 {
545 if (jtag_verify)
546 {
547 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
548 } else
549 {
550 jtag_add_dr_scan(in_num_fields, in_fields, state);
551 }
552 }
553
554
555 /**
556 * Generate a DR SCAN using the fields passed to the function.
557 * For connected TAPs, the function checks in_fields and uses fields
558 * specified there. For bypassed TAPs, the function generates a dummy
559 * 1-bit field. The bypass status of TAPs is set by jtag_add_ir_scan().
560 */
561 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
562 tap_state_t state)
563 {
564 jtag_prelude(state);
565
566 int retval;
567 retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
568 jtag_set_error(retval);
569 }
570
571 /**
572 * Duplicate the scan fields passed into the function into a DR SCAN
573 * command. Unlike jtag_add_dr_scan(), this function assumes that the
574 * caller handles extra fields for bypassed TAPs.
575 */
576 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
577 tap_state_t state)
578 {
579 jtag_prelude(state);
580
581 int retval;
582 retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
583 jtag_set_error(retval);
584 }
585
586 void jtag_add_dr_out(jtag_tap_t* tap,
587 int num_fields, const int* num_bits, const u32* value,
588 tap_state_t end_state)
589 {
590 assert(end_state != TAP_INVALID);
591
592 cmd_queue_cur_state = end_state;
593
594 interface_jtag_add_dr_out(tap,
595 num_fields, num_bits, value,
596 end_state);
597 }
598
599 void jtag_add_tlr(void)
600 {
601 jtag_prelude(TAP_RESET);
602 jtag_set_error(interface_jtag_add_tlr());
603 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
604 }
605
606 void jtag_add_pathmove(int num_states, const tap_state_t *path)
607 {
608 tap_state_t cur_state = cmd_queue_cur_state;
609
610 /* the last state has to be a stable state */
611 if (!tap_is_state_stable(path[num_states - 1]))
612 {
613 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
614 exit(-1);
615 }
616
617 for (int i = 0; i < num_states; i++)
618 {
619 if (path[i] == TAP_RESET)
620 {
621 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
622 exit(-1);
623 }
624
625 if ( tap_state_transition(cur_state, true) != path[i]
626 && tap_state_transition(cur_state, false) != path[i])
627 {
628 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
629 tap_state_name(cur_state), tap_state_name(path[i]));
630 exit(-1);
631 }
632 cur_state = path[i];
633 }
634
635 jtag_checks();
636
637 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
638 cmd_queue_cur_state = path[num_states - 1];
639 }
640
641 void jtag_add_runtest(int num_cycles, tap_state_t state)
642 {
643 jtag_prelude(state);
644 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
645 }
646
647
648 void jtag_add_clocks(int num_cycles)
649 {
650 if (!tap_is_state_stable(cmd_queue_cur_state))
651 {
652 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
653 tap_state_name(cmd_queue_cur_state));
654 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
655 return;
656 }
657
658 if (num_cycles > 0)
659 {
660 jtag_checks();
661 jtag_set_error(interface_jtag_add_clocks(num_cycles));
662 }
663 }
664
665 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
666 {
667 int trst_with_tlr = 0;
668
669 /* FIX!!! there are *many* different cases here. A better
670 * approach is needed for legal combinations of transitions...
671 */
672 if ((jtag_reset_config & RESET_HAS_SRST)&&
673 (jtag_reset_config & RESET_HAS_TRST)&&
674 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
675 {
676 if (((req_tlr_or_trst&&!jtag_trst)||
677 (!req_tlr_or_trst&&jtag_trst))&&
678 ((req_srst&&!jtag_srst)||
679 (!req_srst&&jtag_srst)))
680 {
681 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
682 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
683 }
684 }
685
686 /* Make sure that jtag_reset_config allows the requested reset */
687 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
688 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
689 {
690 LOG_ERROR("BUG: requested reset would assert trst");
691 jtag_set_error(ERROR_FAIL);
692 return;
693 }
694
695 /* if TRST pulls SRST, we reset with TAP T-L-R */
696 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
697 {
698 trst_with_tlr = 1;
699 }
700
701 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
702 {
703 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
704 jtag_set_error(ERROR_FAIL);
705 return;
706 }
707
708 if (req_tlr_or_trst)
709 {
710 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
711 {
712 jtag_trst = 1;
713 } else
714 {
715 trst_with_tlr = 1;
716 }
717 } else
718 {
719 jtag_trst = 0;
720 }
721
722 jtag_srst = req_srst;
723
724 int retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
725 if (retval != ERROR_OK)
726 {
727 jtag_set_error(retval);
728 return;
729 }
730 jtag_execute_queue();
731
732 if (jtag_srst)
733 {
734 LOG_DEBUG("SRST line asserted");
735 }
736 else
737 {
738 LOG_DEBUG("SRST line released");
739 if (jtag_nsrst_delay)
740 jtag_add_sleep(jtag_nsrst_delay * 1000);
741 }
742
743 if (trst_with_tlr)
744 {
745 LOG_DEBUG("JTAG reset with RESET instead of TRST");
746 jtag_set_end_state(TAP_RESET);
747 jtag_add_tlr();
748 return;
749 }
750
751 if (jtag_trst)
752 {
753 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
754 * and inform possible listeners about this
755 */
756 LOG_DEBUG("TRST line asserted");
757 tap_set_state(TAP_RESET);
758 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
759 }
760 else
761 {
762 if (jtag_ntrst_delay)
763 jtag_add_sleep(jtag_ntrst_delay * 1000);
764 }
765 }
766
767 tap_state_t jtag_set_end_state(tap_state_t state)
768 {
769 if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
770 {
771 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
772 }
773
774 if (state!=TAP_INVALID)
775 cmd_queue_end_state = state;
776 return cmd_queue_end_state;
777 }
778
779 tap_state_t jtag_get_end_state(void)
780 {
781 return cmd_queue_end_state;
782 }
783
784 void jtag_add_sleep(u32 us)
785 {
786 keep_alive(); /* we might be running on a very slow JTAG clk */
787 int retval=interface_jtag_add_sleep(us);
788 if (retval!=ERROR_OK)
789 jtag_error=retval;
790 return;
791 }
792
793 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
794 {
795 int retval = ERROR_OK;
796
797 int compare_failed = 0;
798
799 if (in_check_mask)
800 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
801 else
802 compare_failed = buf_cmp(captured, in_check_value, num_bits);
803
804 if (compare_failed){
805 /* An error handler could have caught the failing check
806 * only report a problem when there wasn't a handler, or if the handler
807 * acknowledged the error
808 */
809 /*
810 LOG_WARNING("TAP %s:",
811 jtag_tap_name(field->tap));
812 */
813 if (compare_failed)
814 {
815 char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
816 char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
817
818 if (in_check_mask)
819 {
820 char *in_check_mask_char;
821 in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
822 LOG_WARNING("value captured during scan didn't pass the requested check:");
823 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
824 captured_char, in_check_value_char, in_check_mask_char);
825 free(in_check_mask_char);
826 }
827 else
828 {
829 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);
830 }
831
832 free(captured_char);
833 free(in_check_value_char);
834
835 retval = ERROR_JTAG_QUEUE_FAILED;
836 }
837
838 }
839 return retval;
840 }
841
842 void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
843 {
844 assert(field->in_value != NULL);
845
846 if (value==NULL)
847 {
848 /* no checking to do */
849 return;
850 }
851
852 jtag_execute_queue_noclear();
853
854 int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
855 jtag_set_error(retval);
856 }
857
858
859
860 int default_interface_jtag_execute_queue(void)
861 {
862 if (NULL == jtag)
863 {
864 LOG_ERROR("No JTAG interface configured yet. "
865 "Issue 'init' command in startup scripts "
866 "before communicating with targets.");
867 return ERROR_FAIL;
868 }
869
870 return jtag->execute_queue();
871 }
872
873 void jtag_execute_queue_noclear(void)
874 {
875 jtag_flush_queue_count++;
876 jtag_set_error(interface_jtag_execute_queue());
877 }
878
879 int jtag_get_flush_queue_count(void)
880 {
881 return jtag_flush_queue_count;
882 }
883
884 int jtag_execute_queue(void)
885 {
886 jtag_execute_queue_noclear();
887 return jtag_error_clear();
888 }
889
890 static int jtag_reset_callback(enum jtag_event event, void *priv)
891 {
892 jtag_tap_t *tap = priv;
893
894 LOG_DEBUG("-");
895
896 if (event == JTAG_TRST_ASSERTED)
897 {
898 buf_set_ones(tap->cur_instr, tap->ir_length);
899 tap->bypass = 1;
900 }
901
902 return ERROR_OK;
903 }
904
905 void jtag_sleep(u32 us)
906 {
907 alive_sleep(us/1000);
908 }
909
910 /// maximum number of JTAG devices expected in the chain
911 #define JTAG_MAX_CHAIN_SIZE 20
912
913 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
914 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
915 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
916
917 static int jtag_examine_chain_execute(u8 *idcode_buffer, unsigned num_idcode)
918 {
919 scan_field_t field = {
920 .tap = NULL,
921 .num_bits = num_idcode * 32,
922 .out_value = idcode_buffer,
923 .in_value = idcode_buffer,
924 };
925
926 // initialize to the end of chain ID value
927 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
928 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
929
930 jtag_add_plain_dr_scan(1, &field, TAP_RESET);
931 return jtag_execute_queue();
932 }
933
934 static bool jtag_examine_chain_check(u8 *idcodes, unsigned count)
935 {
936 u8 zero_check = 0x0;
937 u8 one_check = 0xff;
938
939 for (unsigned i = 0; i < count * 4; i++)
940 {
941 zero_check |= idcodes[i];
942 one_check &= idcodes[i];
943 }
944
945 /* if there wasn't a single non-zero bit or if all bits were one,
946 * the scan is not valid */
947 if (zero_check == 0x00 || one_check == 0xff)
948 {
949 LOG_ERROR("JTAG communication failure: check connection, "
950 "JTAG interface, target power etc.");
951 return false;
952 }
953 return true;
954 }
955
956 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
957 const char *name, u32 idcode)
958 {
959 log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
960 "JTAG tap: %s %16.16s: 0x%08x "
961 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
962 name, msg, idcode,
963 EXTRACT_MFG(idcode), EXTRACT_PART(idcode), EXTRACT_VER(idcode) );
964 }
965
966 static bool jtag_idcode_is_final(u32 idcode)
967 {
968 return idcode == 0x000000FF || idcode == 0xFFFFFFFF;
969 }
970
971 /**
972 * This helper checks that remaining bits in the examined chain data are
973 * all as expected, but a single JTAG device requires only 64 bits to be
974 * read back correctly. This can help identify and diagnose problems
975 * with the JTAG chain earlier, gives more helpful/explicit error messages.
976 */
977 static void jtag_examine_chain_end(u8 *idcodes, unsigned count, unsigned max)
978 {
979 bool triggered = false;
980 for ( ; count < max - 31; count += 32)
981 {
982 u32 idcode = buf_get_u32(idcodes, count, 32);
983 // do not trigger the warning if the data looks good
984 if (!triggered && jtag_idcode_is_final(idcode))
985 continue;
986 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
987 count, idcode);
988 triggered = true;
989 }
990 }
991
992 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
993 {
994 if (0 == tap->expected_ids_cnt)
995 {
996 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
997 #if 0
998 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap->idcode)
999 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
1000 #endif
1001 return true;
1002 }
1003
1004 /* Loop over the expected identification codes and test for a match */
1005 u8 ii;
1006 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
1007 {
1008 if (tap->idcode == tap->expected_ids[ii])
1009 break;
1010 }
1011
1012 /* If none of the expected ids matched, log an error */
1013 if (ii != tap->expected_ids_cnt)
1014 {
1015 LOG_INFO("JTAG Tap/device matched");
1016 return true;
1017 }
1018 jtag_examine_chain_display(LOG_LVL_ERROR, "got",
1019 tap->dotted_name, tap->idcode);
1020 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
1021 {
1022 char msg[32];
1023 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
1024 ii + 1, tap->expected_ids_cnt);
1025 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1026 tap->dotted_name, tap->expected_ids[ii]);
1027 }
1028 return false;
1029 }
1030
1031 /* Try to examine chain layout according to IEEE 1149.1 §12
1032 */
1033 static int jtag_examine_chain(void)
1034 {
1035 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1036 unsigned device_count = 0;
1037
1038 jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
1039
1040 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
1041 return ERROR_JTAG_INIT_FAILED;
1042
1043 /* point at the 1st tap */
1044 jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
1045 if (tap == NULL)
1046 {
1047 LOG_ERROR("JTAG: No taps enabled?");
1048 return ERROR_JTAG_INIT_FAILED;
1049 }
1050
1051 for (unsigned bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1052 {
1053 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1054 if ((idcode & 1) == 0)
1055 {
1056 /* LSB must not be 0, this indicates a device in bypass */
1057 LOG_WARNING("Tap/Device does not have IDCODE");
1058 idcode = 0;
1059
1060 bit_count += 1;
1061 }
1062 else
1063 {
1064 /*
1065 * End of chain (invalid manufacturer ID) some devices, such
1066 * as AVR will output all 1's instead of TDI input value at
1067 * end of chain.
1068 */
1069 if (jtag_idcode_is_final(idcode))
1070 {
1071 jtag_examine_chain_end(idcode_buffer,
1072 bit_count + 32, JTAG_MAX_CHAIN_SIZE * 32);
1073 break;
1074 }
1075
1076 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
1077 tap ? tap->dotted_name : "(not-named)",
1078 idcode);
1079
1080 bit_count += 32;
1081 }
1082 device_count++;
1083 if (!tap)
1084 continue;
1085
1086 tap->idcode = idcode;
1087
1088 // ensure the TAP ID does matches what was expected
1089 if (!jtag_examine_chain_match_tap(tap))
1090 return ERROR_JTAG_INIT_FAILED;
1091
1092 tap = jtag_tap_next_enabled(tap);
1093 }
1094
1095 /* see if number of discovered devices matches configuration */
1096 if (device_count != jtag_tap_count_enabled())
1097 {
1098 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
1099 "does not match (enabled) configuration (%i), total taps: %d",
1100 device_count, jtag_tap_count_enabled(), jtag_tap_count());
1101 LOG_ERROR("check the config file and ensure proper JTAG communication"
1102 " (connections, speed, ...)");
1103 return ERROR_JTAG_INIT_FAILED;
1104 }
1105
1106 return ERROR_OK;
1107 }
1108
1109 static int jtag_validate_chain(void)
1110 {
1111 jtag_tap_t *tap;
1112 int total_ir_length = 0;
1113 u8 *ir_test = NULL;
1114 scan_field_t field;
1115 int chain_pos = 0;
1116
1117 tap = NULL;
1118 total_ir_length = 0;
1119 for(;;){
1120 tap = jtag_tap_next_enabled(tap);
1121 if( tap == NULL ){
1122 break;
1123 }
1124 total_ir_length += tap->ir_length;
1125 }
1126
1127 total_ir_length += 2;
1128 ir_test = malloc(CEIL(total_ir_length, 8));
1129 buf_set_ones(ir_test, total_ir_length);
1130
1131 field.tap = NULL;
1132 field.num_bits = total_ir_length;
1133 field.out_value = ir_test;
1134 field.in_value = ir_test;
1135
1136
1137 jtag_add_plain_ir_scan(1, &field, TAP_RESET);
1138 jtag_execute_queue();
1139
1140 tap = NULL;
1141 chain_pos = 0;
1142 int val;
1143 for(;;){
1144 tap = jtag_tap_next_enabled(tap);
1145 if( tap == NULL ){
1146 break;
1147 }
1148
1149 val = buf_get_u32(ir_test, chain_pos, 2);
1150 if (val != 0x1)
1151 {
1152 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1153 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);
1154 free(cbuf);
1155 free(ir_test);
1156 return ERROR_JTAG_INIT_FAILED;
1157 }
1158 chain_pos += tap->ir_length;
1159 }
1160
1161 val = buf_get_u32(ir_test, chain_pos, 2);
1162 if (val != 0x3)
1163 {
1164 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1165 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);
1166 free(cbuf);
1167 free(ir_test);
1168 return ERROR_JTAG_INIT_FAILED;
1169 }
1170
1171 free(ir_test);
1172
1173 return ERROR_OK;
1174 }
1175
1176 enum jtag_tap_cfg_param {
1177 JCFG_EVENT
1178 };
1179
1180 static Jim_Nvp nvp_config_opts[] = {
1181 { .name = "-event", .value = JCFG_EVENT },
1182
1183 { .name = NULL, .value = -1 }
1184 };
1185
1186 static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
1187 {
1188 Jim_Nvp *n;
1189 Jim_Obj *o;
1190 int e;
1191
1192 /* parse config or cget options */
1193 while (goi->argc > 0) {
1194 Jim_SetEmptyResult (goi->interp);
1195
1196 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
1197 if (e != JIM_OK) {
1198 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
1199 return e;
1200 }
1201
1202 switch (n->value) {
1203 case JCFG_EVENT:
1204 if (goi->argc == 0) {
1205 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ..." );
1206 return JIM_ERR;
1207 }
1208
1209 e = Jim_GetOpt_Nvp( goi, nvp_jtag_tap_event, &n );
1210 if (e != JIM_OK) {
1211 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
1212 return e;
1213 }
1214
1215 if (goi->isconfigure) {
1216 if (goi->argc != 1) {
1217 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
1218 return JIM_ERR;
1219 }
1220 } else {
1221 if (goi->argc != 0) {
1222 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
1223 return JIM_ERR;
1224 }
1225 }
1226
1227 {
1228 jtag_tap_event_action_t *jteap;
1229
1230 jteap = tap->event_action;
1231 /* replace existing? */
1232 while (jteap) {
1233 if (jteap->event == (enum jtag_tap_event)n->value) {
1234 break;
1235 }
1236 jteap = jteap->next;
1237 }
1238
1239 if (goi->isconfigure) {
1240 if (jteap == NULL) {
1241 /* create new */
1242 jteap = calloc(1, sizeof (*jteap));
1243 }
1244 jteap->event = n->value;
1245 Jim_GetOpt_Obj( goi, &o);
1246 if (jteap->body) {
1247 Jim_DecrRefCount(interp, jteap->body);
1248 }
1249 jteap->body = Jim_DuplicateObj(goi->interp, o);
1250 Jim_IncrRefCount(jteap->body);
1251
1252 /* add to head of event list */
1253 jteap->next = tap->event_action;
1254 tap->event_action = jteap;
1255 Jim_SetEmptyResult(goi->interp);
1256 } else {
1257 /* get */
1258 if (jteap == NULL) {
1259 Jim_SetEmptyResult(goi->interp);
1260 } else {
1261 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
1262 }
1263 }
1264 }
1265 /* loop for more */
1266 break;
1267 }
1268 } /* while (goi->argc) */
1269
1270 return JIM_OK;
1271 }
1272
1273
1274 static void jtag_tap_init(jtag_tap_t *tap)
1275 {
1276 assert(0 != tap->ir_length);
1277
1278 tap->expected = malloc(tap->ir_length);
1279 tap->expected_mask = malloc(tap->ir_length);
1280 tap->cur_instr = malloc(tap->ir_length);
1281
1282 buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1283 buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1284 buf_set_ones(tap->cur_instr, tap->ir_length);
1285
1286 // place TAP in bypass mode
1287 tap->bypass = 1;
1288 // register the reset callback for the TAP
1289 jtag_register_event_callback(&jtag_reset_callback, tap);
1290
1291 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1292 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1293 tap->abs_chain_position, tap->ir_length,
1294 tap->ir_capture_value, tap->ir_capture_mask);
1295 jtag_tap_add(tap);
1296 }
1297
1298 static void jtag_tap_free(jtag_tap_t *tap)
1299 {
1300 /// @todo is anything missing? no memory leaks please
1301 free((void *)tap->expected_ids);
1302 free((void *)tap->chip);
1303 free((void *)tap->tapname);
1304 free((void *)tap->dotted_name);
1305 free(tap);
1306 }
1307
1308 static int jim_newtap_cmd( Jim_GetOptInfo *goi )
1309 {
1310 jtag_tap_t *pTap;
1311 jim_wide w;
1312 int x;
1313 int e;
1314 int reqbits;
1315 Jim_Nvp *n;
1316 char *cp;
1317 const Jim_Nvp opts[] = {
1318 #define NTAP_OPT_IRLEN 0
1319 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
1320 #define NTAP_OPT_IRMASK 1
1321 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
1322 #define NTAP_OPT_IRCAPTURE 2
1323 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
1324 #define NTAP_OPT_ENABLED 3
1325 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
1326 #define NTAP_OPT_DISABLED 4
1327 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
1328 #define NTAP_OPT_EXPECTED_ID 5
1329 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
1330 { .name = NULL , .value = -1 },
1331 };
1332
1333 pTap = malloc( sizeof(jtag_tap_t) );
1334 memset( pTap, 0, sizeof(*pTap) );
1335 if( !pTap ){
1336 Jim_SetResult_sprintf( goi->interp, "no memory");
1337 return JIM_ERR;
1338 }
1339 /*
1340 * we expect CHIP + TAP + OPTIONS
1341 * */
1342 if( goi->argc < 3 ){
1343 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
1344 return JIM_ERR;
1345 }
1346 Jim_GetOpt_String( goi, &cp, NULL );
1347 pTap->chip = strdup(cp);
1348
1349 Jim_GetOpt_String( goi, &cp, NULL );
1350 pTap->tapname = strdup(cp);
1351
1352 /* name + dot + name + null */
1353 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
1354 cp = malloc( x );
1355 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
1356 pTap->dotted_name = cp;
1357
1358 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1359 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
1360
1361 /* default is enabled */
1362 pTap->enabled = 1;
1363
1364 /* deal with options */
1365 #define NTREQ_IRLEN 1
1366 #define NTREQ_IRCAPTURE 2
1367 #define NTREQ_IRMASK 4
1368
1369 /* clear them as we find them */
1370 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
1371
1372 while( goi->argc ){
1373 e = Jim_GetOpt_Nvp( goi, opts, &n );
1374 if( e != JIM_OK ){
1375 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
1376 return e;
1377 }
1378 LOG_DEBUG("Processing option: %s", n->name );
1379 switch( n->value ){
1380 case NTAP_OPT_ENABLED:
1381 pTap->enabled = 1;
1382 break;
1383 case NTAP_OPT_DISABLED:
1384 pTap->enabled = 0;
1385 break;
1386 case NTAP_OPT_EXPECTED_ID:
1387 {
1388 u32 *new_expected_ids;
1389
1390 e = Jim_GetOpt_Wide( goi, &w );
1391 if( e != JIM_OK) {
1392 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
1393 return e;
1394 }
1395
1396 new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
1397 if (new_expected_ids == NULL) {
1398 Jim_SetResult_sprintf( goi->interp, "no memory");
1399 return JIM_ERR;
1400 }
1401
1402 memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
1403
1404 new_expected_ids[pTap->expected_ids_cnt] = w;
1405
1406 free(pTap->expected_ids);
1407 pTap->expected_ids = new_expected_ids;
1408 pTap->expected_ids_cnt++;
1409 break;
1410 }
1411 case NTAP_OPT_IRLEN:
1412 case NTAP_OPT_IRMASK:
1413 case NTAP_OPT_IRCAPTURE:
1414 e = Jim_GetOpt_Wide( goi, &w );
1415 if( e != JIM_OK ){
1416 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
1417 return e;
1418 }
1419 if( (w < 0) || (w > 0xffff) ){
1420 /* wacky value */
1421 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
1422 n->name, (int)(w), (int)(w));
1423 return JIM_ERR;
1424 }
1425 switch(n->value){
1426 case NTAP_OPT_IRLEN:
1427 pTap->ir_length = w;
1428 reqbits &= (~(NTREQ_IRLEN));
1429 break;
1430 case NTAP_OPT_IRMASK:
1431 pTap->ir_capture_mask = w;
1432 reqbits &= (~(NTREQ_IRMASK));
1433 break;
1434 case NTAP_OPT_IRCAPTURE:
1435 pTap->ir_capture_value = w;
1436 reqbits &= (~(NTREQ_IRCAPTURE));
1437 break;
1438 }
1439 } /* switch(n->value) */
1440 } /* while( goi->argc ) */
1441
1442 /* Did all the required option bits get cleared? */
1443 if (0 == reqbits)
1444 {
1445 jtag_tap_init(pTap);
1446 return ERROR_OK;
1447 }
1448
1449 Jim_SetResult_sprintf(goi->interp,
1450 "newtap: %s missing required parameters",
1451 pTap->dotted_name);
1452 jtag_tap_free(pTap);
1453 return JIM_ERR;
1454 }
1455
1456 static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
1457 {
1458 Jim_GetOptInfo goi;
1459 int e;
1460 Jim_Nvp *n;
1461 Jim_Obj *o;
1462 struct command_context_s *context;
1463
1464 enum {
1465 JTAG_CMD_INTERFACE,
1466 JTAG_CMD_INIT_RESET,
1467 JTAG_CMD_NEWTAP,
1468 JTAG_CMD_TAPENABLE,
1469 JTAG_CMD_TAPDISABLE,
1470 JTAG_CMD_TAPISENABLED,
1471 JTAG_CMD_CONFIGURE,
1472 JTAG_CMD_CGET
1473 };
1474
1475 const Jim_Nvp jtag_cmds[] = {
1476 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
1477 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
1478 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
1479 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
1480 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
1481 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
1482 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
1483 { .name = "cget" , .value = JTAG_CMD_CGET },
1484
1485 { .name = NULL, .value = -1 },
1486 };
1487
1488 context = Jim_GetAssocData(interp, "context");
1489 /* go past the command */
1490 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
1491
1492 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
1493 if( e != JIM_OK ){
1494 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
1495 return e;
1496 }
1497 Jim_SetEmptyResult( goi.interp );
1498 switch( n->value ){
1499 case JTAG_CMD_INTERFACE:
1500 /* return the name of the interface */
1501 /* TCL code might need to know the exact type... */
1502 /* FUTURE: we allow this as a means to "set" the interface. */
1503 if( goi.argc != 0 ){
1504 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1505 return JIM_ERR;
1506 }
1507 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
1508 return JIM_OK;
1509 case JTAG_CMD_INIT_RESET:
1510 if( goi.argc != 0 ){
1511 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1512 return JIM_ERR;
1513 }
1514 e = jtag_init_reset(context);
1515 if( e != ERROR_OK ){
1516 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
1517 return JIM_ERR;
1518 }
1519 return JIM_OK;
1520 case JTAG_CMD_NEWTAP:
1521 return jim_newtap_cmd( &goi );
1522 break;
1523 case JTAG_CMD_TAPISENABLED:
1524 case JTAG_CMD_TAPENABLE:
1525 case JTAG_CMD_TAPDISABLE:
1526 if( goi.argc != 1 ){
1527 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
1528 return JIM_ERR;
1529 }
1530
1531 {
1532 jtag_tap_t *t;
1533 t = jtag_tap_by_jim_obj( goi.interp, goi.argv[0] );
1534 if( t == NULL ){
1535 return JIM_ERR;
1536 }
1537 switch( n->value ){
1538 case JTAG_CMD_TAPISENABLED:
1539 e = t->enabled;
1540 break;
1541 case JTAG_CMD_TAPENABLE:
1542 jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
1543 e = 1;
1544 t->enabled = e;
1545 break;
1546 case JTAG_CMD_TAPDISABLE:
1547 jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
1548 e = 0;
1549 t->enabled = e;
1550 break;
1551 }
1552 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
1553 return JIM_OK;
1554 }
1555 break;
1556
1557 case JTAG_CMD_CGET:
1558 if( goi.argc < 2 ){
1559 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
1560 return JIM_ERR;
1561 }
1562
1563 {
1564 jtag_tap_t *t;
1565
1566 Jim_GetOpt_Obj(&goi, &o);
1567 t = jtag_tap_by_jim_obj( goi.interp, o );
1568 if( t == NULL ){
1569 return JIM_ERR;
1570 }
1571
1572 goi.isconfigure = 0;
1573 return jtag_tap_configure_cmd( &goi, t);
1574 }
1575 break;
1576
1577 case JTAG_CMD_CONFIGURE:
1578 if( goi.argc < 3 ){
1579 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
1580 return JIM_ERR;
1581 }
1582
1583 {
1584 jtag_tap_t *t;
1585
1586 Jim_GetOpt_Obj(&goi, &o);
1587 t = jtag_tap_by_jim_obj( goi.interp, o );
1588 if( t == NULL ){
1589 return JIM_ERR;
1590 }
1591
1592 goi.isconfigure = 1;
1593 return jtag_tap_configure_cmd( &goi, t);
1594 }
1595 }
1596
1597 return JIM_ERR;
1598 }
1599
1600 int jtag_register_commands(struct command_context_s *cmd_ctx)
1601 {
1602 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
1603
1604 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1605 COMMAND_CONFIG, "try to configure interface");
1606 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1607 COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
1608 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1609 COMMAND_ANY, "set maximum jtag speed (if supported); "
1610 "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
1611 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1612 COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
1613 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1614 COMMAND_ANY,
1615 "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
1616 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1617 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1618 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1619 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1620
1621 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1622 COMMAND_EXEC, "print current scan chain configuration");
1623
1624 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1625 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1626 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1627 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1628 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1629 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1630 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
1631 register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
1632
1633 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1634 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1635 register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
1636 COMMAND_ANY, "verify value capture <enable|disable>");
1637 register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
1638 COMMAND_ANY, "choose short(default) or long tms_sequence <short|long>");
1639 return ERROR_OK;
1640 }
1641
1642 int jtag_interface_init(struct command_context_s *cmd_ctx)
1643 {
1644 if (jtag)
1645 return ERROR_OK;
1646
1647 if (!jtag_interface)
1648 {
1649 /* nothing was previously specified by "interface" command */
1650 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1651 return ERROR_JTAG_INVALID_INTERFACE;
1652 }
1653 if(hasKHz)
1654 {
1655 jtag_interface->khz(jtag_get_speed_khz(), &jtag_speed);
1656 hasKHz = false;
1657 }
1658
1659 if (jtag_interface->init() != ERROR_OK)
1660 return ERROR_JTAG_INIT_FAILED;
1661
1662 jtag = jtag_interface;
1663 return ERROR_OK;
1664 }
1665
1666 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1667 {
1668 jtag_tap_t *tap;
1669 int retval;
1670
1671 LOG_DEBUG("Init JTAG chain");
1672
1673 tap = jtag_tap_next_enabled(NULL);
1674 if( tap == NULL ){
1675 LOG_ERROR("There are no enabled taps?");
1676 return ERROR_JTAG_INIT_FAILED;
1677 }
1678
1679 jtag_add_tlr();
1680 if ((retval=jtag_execute_queue())!=ERROR_OK)
1681 return retval;
1682
1683 /* examine chain first, as this could discover the real chain layout */
1684 if (jtag_examine_chain() != ERROR_OK)
1685 {
1686 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1687 }
1688
1689 if (jtag_validate_chain() != ERROR_OK)
1690 {
1691 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1692 }
1693
1694 return ERROR_OK;
1695 }
1696
1697 int jtag_interface_quit(void)
1698 {
1699 if (!jtag || !jtag->quit)
1700 return ERROR_OK;
1701
1702 // close the JTAG interface
1703 int result = jtag->quit();
1704 if (ERROR_OK != result)
1705 LOG_ERROR("failed: %d", result);
1706
1707 return ERROR_OK;
1708 }
1709
1710
1711 int jtag_init_reset(struct command_context_s *cmd_ctx)
1712 {
1713 int retval;
1714
1715 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1716 return retval;
1717
1718 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
1719
1720 /* Reset can happen after a power cycle.
1721 *
1722 * Ideally we would only assert TRST or run RESET before the target reset.
1723 *
1724 * However w/srst_pulls_trst, trst is asserted together with the target
1725 * reset whether we want it or not.
1726 *
1727 * NB! Some targets have JTAG circuitry disabled until a
1728 * trst & srst has been asserted.
1729 *
1730 * NB! here we assume nsrst/ntrst delay are sufficient!
1731 *
1732 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1733 *
1734 */
1735 jtag_add_reset(1, 0); /* RESET or TRST */
1736 if (jtag_reset_config & RESET_HAS_SRST)
1737 {
1738 jtag_add_reset(1, 1);
1739 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1740 jtag_add_reset(0, 1);
1741 }
1742 jtag_add_reset(0, 0);
1743 if ((retval = jtag_execute_queue()) != ERROR_OK)
1744 return retval;
1745
1746 /* Check that we can communication on the JTAG chain + eventually we want to
1747 * be able to perform enumeration only after OpenOCD has started
1748 * telnet and GDB server
1749 *
1750 * That would allow users to more easily perform any magic they need to before
1751 * reset happens.
1752 */
1753 return jtag_init_inner(cmd_ctx);
1754 }
1755
1756 int jtag_init(struct command_context_s *cmd_ctx)
1757 {
1758 int retval;
1759 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1760 return retval;
1761 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1762 {
1763 return ERROR_OK;
1764 }
1765 return jtag_init_reset(cmd_ctx);
1766 }
1767
1768 void jtag_set_speed_khz(unsigned khz)
1769 {
1770 speed_khz = khz;
1771 }
1772 unsigned jtag_get_speed_khz(void)
1773 {
1774 return speed_khz;
1775 }
1776
1777 static int default_khz(int khz, int *jtag_speed)
1778 {
1779 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1780 return ERROR_FAIL;
1781 }
1782
1783 static int default_speed_div(int speed, int *khz)
1784 {
1785 LOG_ERROR("Translation from jtag_speed to khz not implemented");
1786 return ERROR_FAIL;
1787 }
1788
1789 static int default_power_dropout(int *dropout)
1790 {
1791 *dropout=0; /* by default we can't detect power dropout */
1792 return ERROR_OK;
1793 }
1794
1795 static int default_srst_asserted(int *srst_asserted)
1796 {
1797 *srst_asserted=0; /* by default we can't detect srst asserted */
1798 return ERROR_OK;
1799 }
1800
1801 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1802 {
1803 int i;
1804 int retval;
1805
1806 /* check whether the interface is already configured */
1807 if (jtag_interface)
1808 {
1809 LOG_WARNING("Interface already configured, ignoring");
1810 return ERROR_OK;
1811 }
1812
1813 /* interface name is a mandatory argument */
1814 if (argc < 1 || args[0][0] == '\0')
1815 {
1816 return ERROR_COMMAND_SYNTAX_ERROR;
1817 }
1818
1819 for (i=0; jtag_interfaces[i]; i++)
1820 {
1821 if (strcmp(args[0], jtag_interfaces[i]->name) == 0)
1822 {
1823 if ((retval = jtag_interfaces[i]->register_commands(cmd_ctx)) != ERROR_OK)
1824 {
1825 return retval;
1826 }
1827
1828 jtag_interface = jtag_interfaces[i];
1829
1830 if (jtag_interface->khz == NULL)
1831 {
1832 jtag_interface->khz = default_khz;
1833 }
1834 if (jtag_interface->speed_div == NULL)
1835 {
1836 jtag_interface->speed_div = default_speed_div;
1837 }
1838 if (jtag_interface->power_dropout == NULL)
1839 {
1840 jtag_interface->power_dropout = default_power_dropout;
1841 }
1842 if (jtag_interface->srst_asserted == NULL)
1843 {
1844 jtag_interface->srst_asserted = default_srst_asserted;
1845 }
1846
1847 return ERROR_OK;
1848 }
1849 }
1850
1851 /* no valid interface was found (i.e. the configuration option,
1852 * didn't match one of the compiled-in interfaces
1853 */
1854 LOG_ERROR("No valid jtag interface found (%s)", args[0]);
1855 LOG_ERROR("compiled-in jtag interfaces:");
1856 for (i = 0; jtag_interfaces[i]; i++)
1857 {
1858 LOG_ERROR("%i: %s", i, jtag_interfaces[i]->name);
1859 }
1860
1861 return ERROR_JTAG_INVALID_INTERFACE;
1862 }
1863
1864 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1865 {
1866 int e;
1867 char buf[1024];
1868 Jim_Obj *newargs[ 10 ];
1869 /*
1870 * CONVERT SYNTAX
1871 * argv[-1] = command
1872 * argv[ 0] = ir length
1873 * argv[ 1] = ir capture
1874 * argv[ 2] = ir mask
1875 * argv[ 3] = not actually used by anything but in the docs
1876 */
1877
1878 if( argc < 4 ){
1879 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
1880 return ERROR_OK;
1881 }
1882 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
1883 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
1884 args[0],
1885 args[1],
1886 args[2] );
1887 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
1888 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
1889 command_print( cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
1890 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
1891
1892 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
1893 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
1894 sprintf( buf, "chip%d", jtag_tap_count() );
1895 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
1896 sprintf( buf, "tap%d", jtag_tap_count() );
1897 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
1898 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
1899 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
1900 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
1901 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
1902 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
1903 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
1904
1905 command_print( cmd_ctx, "NEW COMMAND:");
1906 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
1907 Jim_GetString( newargs[0], NULL ),
1908 Jim_GetString( newargs[1], NULL ),
1909 Jim_GetString( newargs[2], NULL ),
1910 Jim_GetString( newargs[3], NULL ),
1911 Jim_GetString( newargs[4], NULL ),
1912 Jim_GetString( newargs[5], NULL ),
1913 Jim_GetString( newargs[6], NULL ),
1914 Jim_GetString( newargs[7], NULL ),
1915 Jim_GetString( newargs[8], NULL ),
1916 Jim_GetString( newargs[9], NULL ) );
1917
1918 e = jim_jtag_command( interp, 10, newargs );
1919 if( e != JIM_OK ){
1920 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
1921 }
1922 return e;
1923 }
1924
1925 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1926 {
1927 jtag_tap_t *tap;
1928
1929 tap = jtag_all_taps();
1930 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
1931 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
1932
1933 while( tap ){
1934 u32 expected, expected_mask, cur_instr, ii;
1935 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
1936 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
1937 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
1938
1939 command_print(cmd_ctx,
1940 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
1941 tap->abs_chain_position,
1942 tap->dotted_name,
1943 tap->enabled ? 'Y' : 'n',
1944 tap->idcode,
1945 (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
1946 tap->ir_length,
1947 expected,
1948 expected_mask,
1949 cur_instr);
1950
1951 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
1952 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
1953 tap->expected_ids[ii]);
1954 }
1955
1956 tap = tap->next_tap;
1957 }
1958
1959 return ERROR_OK;
1960 }
1961
1962 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1963 {
1964 int new_cfg = 0;
1965 int mask = 0;
1966
1967 if (argc < 1)
1968 return ERROR_COMMAND_SYNTAX_ERROR;
1969
1970 /* Original versions cared about the order of these tokens:
1971 * reset_config signals [combination [trst_type [srst_type]]]
1972 * They also clobbered the previous configuration even on error.
1973 *
1974 * Here we don't care about the order, and only change values
1975 * which have been explicitly specified.
1976 */
1977 for (; argc; argc--, args++) {
1978 int tmp = 0;
1979 int m;
1980
1981 /* signals */
1982 m = RESET_HAS_TRST | RESET_HAS_SRST;
1983 if (strcmp(*args, "none") == 0)
1984 tmp = RESET_NONE;
1985 else if (strcmp(*args, "trst_only") == 0)
1986 tmp = RESET_HAS_TRST;
1987 else if (strcmp(*args, "srst_only") == 0)
1988 tmp = RESET_HAS_SRST;
1989 else if (strcmp(*args, "trst_and_srst") == 0)
1990 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1991 else
1992 m = 0;
1993 if (mask & m) {
1994 LOG_ERROR("extra reset_config %s spec (%s)",
1995 "signal", *args);
1996 return ERROR_INVALID_ARGUMENTS;
1997 }
1998 if (m)
1999 goto next;
2000
2001 /* combination (options for broken wiring) */
2002 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
2003 if (strcmp(*args, "separate") == 0)
2004 /* separate reset lines - default */;
2005 else if (strcmp(*args, "srst_pulls_trst") == 0)
2006 tmp |= RESET_SRST_PULLS_TRST;
2007 else if (strcmp(*args, "trst_pulls_srst") == 0)
2008 tmp |= RESET_TRST_PULLS_SRST;
2009 else if (strcmp(*args, "combined") == 0)
2010 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
2011 else
2012 m = 0;
2013 if (mask & m) {
2014 LOG_ERROR("extra reset_config %s spec (%s)",
2015 "combination", *args);
2016 return ERROR_INVALID_ARGUMENTS;
2017 }
2018 if (m)
2019 goto next;
2020
2021 /* trst_type (NOP without HAS_TRST) */
2022 m = RESET_TRST_OPEN_DRAIN;
2023 if (strcmp(*args, "trst_open_drain") == 0)
2024 tmp |= RESET_TRST_OPEN_DRAIN;
2025 else if (strcmp(*args, "trst_push_pull") == 0)
2026 /* push/pull from adapter - default */;
2027 else
2028 m = 0;
2029 if (mask & m) {
2030 LOG_ERROR("extra reset_config %s spec (%s)",
2031 "trst_type", *args);
2032 return ERROR_INVALID_ARGUMENTS;
2033 }
2034 if (m)
2035 goto next;
2036
2037 /* srst_type (NOP without HAS_SRST) */
2038 m |= RESET_SRST_PUSH_PULL;
2039 if (strcmp(*args, "srst_push_pull") == 0)
2040 tmp |= RESET_SRST_PUSH_PULL;
2041 else if (strcmp(*args, "srst_open_drain") == 0)
2042 /* open drain from adapter - default */;
2043 else
2044 m = 0;
2045 if (mask & m) {
2046 LOG_ERROR("extra reset_config %s spec (%s)",
2047 "srst_type", *args);
2048 return ERROR_INVALID_ARGUMENTS;
2049 }
2050 if (m)
2051 goto next;
2052
2053 /* caller provided nonsense; fail */
2054 LOG_ERROR("unknown reset_config flag (%s)", *args);
2055 return ERROR_INVALID_ARGUMENTS;
2056
2057 next:
2058 /* Remember the bits which were specified (mask)
2059 * and their new values (new_cfg).
2060 */
2061 mask |= m;
2062 new_cfg |= tmp;
2063 }
2064
2065 /* clear previous values of those bits, save new values */
2066 jtag_reset_config &= ~mask;
2067 jtag_reset_config |= new_cfg;
2068
2069 return ERROR_OK;
2070 }
2071
2072 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
2073 char *cmd, char **args, int argc)
2074 {
2075 if (argc > 1)
2076 return ERROR_COMMAND_SYNTAX_ERROR;
2077 if (argc == 1)
2078 jtag_set_nsrst_delay(strtoul(args[0], NULL, 0));
2079 command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
2080 return ERROR_OK;
2081 }
2082
2083 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
2084 char *cmd, char **args, int argc)
2085 {
2086 if (argc > 1)
2087 return ERROR_COMMAND_SYNTAX_ERROR;
2088 if (argc == 1)
2089 jtag_set_ntrst_delay(strtoul(args[0], NULL, 0));
2090 command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
2091 return ERROR_OK;
2092 }
2093
2094 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2095 {
2096 int retval = ERROR_OK;
2097
2098 if (argc > 1)
2099 return ERROR_COMMAND_SYNTAX_ERROR;
2100 if (argc == 1)
2101 {
2102 LOG_DEBUG("handle jtag speed");
2103
2104 int cur_speed = 0;
2105 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
2106
2107 /* this command can be called during CONFIG,
2108 * in which case jtag isn't initialized */
2109 if (jtag)
2110 retval = jtag->speed(cur_speed);
2111 }
2112 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
2113
2114 return retval;
2115 }
2116
2117 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2118 {
2119 if (argc > 1)
2120 return ERROR_COMMAND_SYNTAX_ERROR;
2121
2122 int retval = ERROR_OK;
2123 int cur_speed = 0;
2124 if (argc == 1)
2125 {
2126 LOG_DEBUG("handle jtag khz");
2127
2128 jtag_set_speed_khz(strtoul(args[0], NULL, 0));
2129 if (jtag != NULL)
2130 {
2131 LOG_DEBUG("have interface set up");
2132 int speed_div1;
2133 retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
2134 if (ERROR_OK != retval)
2135 {
2136 jtag_set_speed_khz(0);
2137 return retval;
2138 }
2139 cur_speed = jtag_speed = speed_div1;
2140
2141 retval = jtag->speed(cur_speed);
2142 }
2143 else
2144 hasKHz = true;
2145 }
2146
2147 cur_speed = jtag_get_speed_khz();
2148 if (jtag != NULL)
2149 {
2150 retval = jtag->speed_div(jtag_speed, &cur_speed);
2151 if (ERROR_OK != retval)
2152 return retval;
2153 }
2154
2155 if (cur_speed)
2156 command_print(cmd_ctx, "%d kHz", cur_speed);
2157 else
2158 command_print(cmd_ctx, "RCLK - adaptive");
2159 return retval;
2160
2161 }
2162
2163 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
2164 char *cmd, char **args, int argc)
2165 {
2166 if (argc != 2)
2167 return ERROR_COMMAND_SYNTAX_ERROR;
2168
2169 int trst = -1;
2170 if (args[0][0] == '1')
2171 trst = 1;
2172 else if (args[0][0] == '0')
2173 trst = 0;
2174 else
2175 return ERROR_COMMAND_SYNTAX_ERROR;
2176
2177 int srst = -1;
2178 if (args[1][0] == '1')
2179 srst = 1;
2180 else if (args[1][0] == '0')
2181 srst = 0;
2182 else
2183 return ERROR_COMMAND_SYNTAX_ERROR;
2184
2185 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
2186 return ERROR_JTAG_INIT_FAILED;
2187
2188 jtag_add_reset(trst, srst);
2189 jtag_execute_queue();
2190
2191 return ERROR_OK;
2192 }
2193
2194 static int handle_runtest_command(struct command_context_s *cmd_ctx,
2195 char *cmd, char **args, int argc)
2196 {
2197 if (argc != 1)
2198 return ERROR_COMMAND_SYNTAX_ERROR;
2199
2200 jtag_add_runtest(strtol(args[0], NULL, 0), jtag_get_end_state());
2201 jtag_execute_queue();
2202
2203 return ERROR_OK;
2204 }
2205
2206 /*
2207 * For "irscan" or "drscan" commands, the "end" (really, "next") state
2208 * should be stable ... and *NOT* a shift state, otherwise free-running
2209 * jtag clocks could change the values latched by the update state.
2210 */
2211 static bool scan_is_safe(tap_state_t state)
2212 {
2213 switch (state)
2214 {
2215 case TAP_RESET:
2216 case TAP_IDLE:
2217 case TAP_DRPAUSE:
2218 case TAP_IRPAUSE:
2219 return true;
2220 default:
2221 return false;
2222 }
2223 }
2224
2225
2226 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2227 {
2228 int i;
2229 scan_field_t *fields;
2230 jtag_tap_t *tap;
2231 tap_state_t endstate;
2232
2233 if ((argc < 2) || (argc % 2))
2234 {
2235 return ERROR_COMMAND_SYNTAX_ERROR;
2236 }
2237
2238 /* optional "-endstate" "statename" at the end of the arguments,
2239 * so that e.g. IRPAUSE can let us load the data register before
2240 * entering RUN/IDLE to execute the instruction we load here.
2241 */
2242 endstate = TAP_IDLE;
2243
2244 if( argc >= 4 ){
2245 /* have at least one pair of numbers. */
2246 /* is last pair the magic text? */
2247 if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
2248 const char *cpA;
2249 const char *cpS;
2250 cpA = args[ argc-1 ];
2251 for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
2252 cpS = tap_state_name( endstate );
2253 if( 0 == strcmp( cpA, cpS ) ){
2254 break;
2255 }
2256 }
2257 if( endstate >= TAP_NUM_STATES ){
2258 return ERROR_COMMAND_SYNTAX_ERROR;
2259 } else {
2260 if (!scan_is_safe(endstate))
2261 LOG_WARNING("irscan with unsafe "
2262 "endstate \"%s\"", cpA);
2263 /* found - remove the last 2 args */
2264 argc -= 2;
2265 }
2266 }
2267 }
2268
2269 int num_fields = argc / 2;
2270
2271 fields = malloc(sizeof(scan_field_t) * num_fields);
2272
2273 for (i = 0; i < num_fields; i++)
2274 {
2275 tap = jtag_tap_by_string( args[i*2] );
2276 if (tap==NULL)
2277 {
2278 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
2279 return ERROR_FAIL;
2280 }
2281 int field_size = tap->ir_length;
2282 fields[i].tap = tap;
2283 fields[i].num_bits = field_size;
2284 fields[i].out_value = malloc(CEIL(field_size, 8));
2285 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2286 fields[i].in_value = NULL;
2287 }
2288
2289 /* did we have an endstate? */
2290 jtag_add_ir_scan(num_fields, fields, endstate);
2291
2292 int retval=jtag_execute_queue();
2293
2294 for (i = 0; i < num_fields; i++)
2295 free(fields[i].out_value);
2296
2297 free (fields);
2298
2299 return retval;
2300 }
2301
2302 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2303 {
2304 int retval;
2305 scan_field_t *fields;
2306 int num_fields;
2307 int field_count = 0;
2308 int i, e;
2309 jtag_tap_t *tap;
2310 tap_state_t endstate;
2311
2312 /* args[1] = device
2313 * args[2] = num_bits
2314 * args[3] = hex string
2315 * ... repeat num bits and hex string ...
2316 *
2317 * .. optionally:
2318 * args[N-2] = "-endstate"
2319 * args[N-1] = statename
2320 */
2321 if ((argc < 4) || ((argc % 2)!=0))
2322 {
2323 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2324 return JIM_ERR;
2325 }
2326
2327 endstate = TAP_IDLE;
2328
2329 /* validate arguments as numbers */
2330 e = JIM_OK;
2331 for (i = 2; i < argc; i+=2)
2332 {
2333 long bits;
2334 const char *cp;
2335
2336 e = Jim_GetLong(interp, args[i], &bits);
2337 /* If valid - try next arg */
2338 if( e == JIM_OK ){
2339 continue;
2340 }
2341
2342 /* Not valid.. are we at the end? */
2343 if ( ((i+2) != argc) ){
2344 /* nope, then error */
2345 return e;
2346 }
2347
2348 /* it could be: "-endstate FOO"
2349 * e.g. DRPAUSE so we can issue more instructions
2350 * before entering RUN/IDLE and executing them.
2351 */
2352
2353 /* get arg as a string. */
2354 cp = Jim_GetString( args[i], NULL );
2355 /* is it the magic? */
2356 if( 0 == strcmp( "-endstate", cp ) ){
2357 /* is the statename valid? */
2358 cp = Jim_GetString( args[i+1], NULL );
2359
2360 /* see if it is a valid state name */
2361 endstate = tap_state_by_name(cp);
2362 if( endstate < 0 ){
2363 /* update the error message */
2364 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
2365 } else {
2366 if (!scan_is_safe(endstate))
2367 LOG_WARNING("drscan with unsafe "
2368 "endstate \"%s\"", cp);
2369
2370 /* valid - so clear the error */
2371 e = JIM_OK;
2372 /* and remove the last 2 args */
2373 argc -= 2;
2374 }
2375 }
2376
2377 /* Still an error? */
2378 if( e != JIM_OK ){
2379 return e; /* too bad */
2380 }
2381 } /* validate args */
2382
2383 tap = jtag_tap_by_jim_obj( interp, args[1] );
2384 if( tap == NULL ){
2385 return JIM_ERR;
2386 }
2387
2388 num_fields=(argc-2)/2;
2389 fields = malloc(sizeof(scan_field_t) * num_fields);
2390 for (i = 2; i < argc; i+=2)
2391 {
2392 long bits;
2393 int len;
2394 const char *str;
2395
2396 Jim_GetLong(interp, args[i], &bits);
2397 str = Jim_GetString(args[i+1], &len);
2398
2399 fields[field_count].tap = tap;
2400 fields[field_count].num_bits = bits;
2401 fields[field_count].out_value = malloc(CEIL(bits, 8));
2402 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2403 fields[field_count].in_value = fields[field_count].out_value;
2404 field_count++;
2405 }
2406
2407 jtag_add_dr_scan(num_fields, fields, endstate);
2408
2409 retval = jtag_execute_queue();
2410 if (retval != ERROR_OK)
2411 {
2412 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
2413 return JIM_ERR;
2414 }
2415
2416 field_count=0;
2417 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2418 for (i = 2; i < argc; i+=2)
2419 {
2420 long bits;
2421 char *str;
2422
2423 Jim_GetLong(interp, args[i], &bits);
2424 str = buf_to_str(fields[field_count].in_value, bits, 16);
2425 free(fields[field_count].out_value);
2426
2427 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2428 free(str);
2429 field_count++;
2430 }
2431
2432 Jim_SetResult(interp, list);
2433
2434 free(fields);
2435
2436 return JIM_OK;
2437 }
2438
2439
2440 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2441 {
2442 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
2443
2444 return JIM_OK;
2445 }
2446
2447
2448 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2449 {
2450 if (argc == 1)
2451 {
2452 if (strcmp(args[0], "enable") == 0)
2453 {
2454 jtag_verify_capture_ir = 1;
2455 }
2456 else if (strcmp(args[0], "disable") == 0)
2457 {
2458 jtag_verify_capture_ir = 0;
2459 } else
2460 {
2461 return ERROR_COMMAND_SYNTAX_ERROR;
2462 }
2463 } else if (argc != 0)
2464 {
2465 return ERROR_COMMAND_SYNTAX_ERROR;
2466 }
2467
2468 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
2469
2470 return ERROR_OK;
2471 }
2472
2473 void jtag_set_verify(bool enable)
2474 {
2475 jtag_verify = enable;
2476 }
2477
2478 bool jtag_will_verify()
2479 {
2480 return jtag_verify;
2481 }
2482
2483 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2484 {
2485 if (argc > 1)
2486 return ERROR_COMMAND_SYNTAX_ERROR;
2487
2488 if (argc == 1)
2489 {
2490 if (strcmp(args[0], "enable") == 0)
2491 jtag_set_verify(true);
2492 else if (strcmp(args[0], "disable") == 0)
2493 jtag_set_verify(false);
2494 else
2495 return ERROR_COMMAND_SYNTAX_ERROR;
2496 }
2497
2498 const char *status = jtag_will_verify() ? "enabled": "disabled";
2499 command_print(cmd_ctx, "verify jtag capture is %s", status);
2500
2501 return ERROR_OK;
2502 }
2503
2504
2505 int jtag_power_dropout(int *dropout)
2506 {
2507 return jtag->power_dropout(dropout);
2508 }
2509
2510 int jtag_srst_asserted(int *srst_asserted)
2511 {
2512 return jtag->srst_asserted(srst_asserted);
2513 }
2514
2515 void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
2516 {
2517 jtag_tap_event_action_t * jteap;
2518 int done;
2519
2520 jteap = tap->event_action;
2521
2522 done = 0;
2523 while (jteap) {
2524 if (jteap->event == e) {
2525 done = 1;
2526 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
2527 tap->dotted_name,
2528 e,
2529 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
2530 Jim_GetString(jteap->body, NULL) );
2531 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
2532 Jim_PrintErrorMessage(interp);
2533 }
2534 }
2535
2536 jteap = jteap->next;
2537 }
2538
2539 if (!done) {
2540 LOG_DEBUG( "event %d %s - no action",
2541 e,
2542 Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
2543 }
2544 }
2545
2546 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2547 {
2548 if (argc > 1)
2549 return ERROR_COMMAND_SYNTAX_ERROR;
2550
2551 if (argc == 1)
2552 {
2553 bool use_new_table;
2554 if (strcmp(args[0], "short") == 0)
2555 use_new_table = true;
2556 else if (strcmp(args[0], "long") == 0)
2557 use_new_table = false;
2558 else
2559 return ERROR_COMMAND_SYNTAX_ERROR;
2560
2561 tap_use_new_tms_table(use_new_table);
2562 }
2563
2564 command_print(cmd_ctx, "tms sequence is %s",
2565 tap_uses_new_tms_table() ? "short": "long");
2566
2567 return ERROR_OK;
2568 }
2569
2570 /**
2571 * Function jtag_add_statemove
2572 * moves from the current state to the goal \a state. This needs
2573 * to be handled according to the xsvf spec, see the XSTATE command
2574 * description.
2575 */
2576 int jtag_add_statemove(tap_state_t goal_state)
2577 {
2578 int retval = ERROR_OK;
2579
2580 tap_state_t moves[8];
2581 tap_state_t cur_state = cmd_queue_cur_state;
2582 int i;
2583 int tms_bits;
2584 int tms_count;
2585
2586 LOG_DEBUG( "cur_state=%s goal_state=%s",
2587 tap_state_name(cur_state),
2588 tap_state_name(goal_state) );
2589
2590
2591 /* From the XSVF spec, pertaining to XSTATE:
2592
2593 For special states known as stable states (Test-Logic-Reset,
2594 Run-Test/Idle, Pause-DR, Pause- IR), an XSVF interpreter follows
2595 predefined TAP state paths when the starting state is a stable state and
2596 when the XSTATE specifies a new stable state (see the STATE command in
2597 the [Ref 5] for the TAP state paths between stable states). For
2598 non-stable states, XSTATE should specify a state that is only one TAP
2599 state transition distance from the current TAP state to avoid undefined
2600 TAP state paths. A sequence of multiple XSTATE commands can be issued to
2601 transition the TAP through a specific state path.
2602 */
2603
2604 if (goal_state==cur_state )
2605 ; /* nothing to do */
2606
2607 else if( goal_state==TAP_RESET )
2608 {
2609 jtag_add_tlr();
2610 }
2611
2612 else if( tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state) )
2613 {
2614 /* note: unless tms_bits holds a path that agrees with [Ref 5] in above
2615 spec, then this code is not fully conformant to the xsvf spec. This
2616 puts a burden on tap_get_tms_path() function from the xsvf spec.
2617 If in doubt, you should confirm that that burden is being met.
2618 */
2619
2620 tms_bits = tap_get_tms_path(cur_state, goal_state);
2621 tms_count = tap_get_tms_path_len(cur_state, goal_state);
2622
2623 assert( (unsigned) tms_count < DIM(moves) );
2624
2625 for (i=0; i<tms_count; i++, tms_bits>>=1)
2626 {
2627 bool bit = tms_bits & 1;
2628
2629 cur_state = tap_state_transition(cur_state, bit);
2630 moves[i] = cur_state;
2631 }
2632
2633 jtag_add_pathmove(tms_count, moves);
2634 }
2635
2636 /* else state must be immediately reachable in one clock cycle, and does not
2637 need to be a stable state.
2638 */
2639 else if( tap_state_transition(cur_state, true) == goal_state
2640 || tap_state_transition(cur_state, false) == goal_state )
2641 {
2642 /* move a single state */
2643 moves[0] = goal_state;
2644 jtag_add_pathmove( 1, moves );
2645 }
2646
2647 else
2648 {
2649 retval = ERROR_FAIL;
2650 }
2651
2652 return retval;
2653 }
2654
2655 void jtag_set_nsrst_delay(unsigned delay)
2656 {
2657 jtag_nsrst_delay = delay;
2658 }
2659 unsigned jtag_get_nsrst_delay(void)
2660 {
2661 return jtag_nsrst_delay;
2662 }
2663 void jtag_set_ntrst_delay(unsigned delay)
2664 {
2665 jtag_ntrst_delay = delay;
2666 }
2667 unsigned jtag_get_ntrst_delay(void)
2668 {
2669 return jtag_ntrst_delay;
2670 }

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)