Use unsigned type for jtag_tap_count and jtag_tap_by_abs_position.
[openocd.git] / src / jtag / jtag.c
blob0d7cdb3dd43280fcb056879fdbb0930f0cbf3b29
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
31 #include "jtag.h"
32 #include "minidriver.h"
33 #include "interface.h"
35 #ifdef HAVE_STRINGS_H
36 #include <strings.h>
37 #endif
40 /// The number of JTAG queue flushes (for profiling and debugging purposes).
41 static int jtag_flush_queue_count;
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);
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
49 int jtag_error=ERROR_OK;
51 char* jtag_event_strings[] =
53 "JTAG controller reset (RESET or TRST)"
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" },
60 { .name = NULL, .value = -1 }
63 int jtag_trst = 0;
64 int jtag_srst = 0;
66 /**
67 * List all TAPs that have been created.
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
74 static unsigned jtag_num_taps = 0;
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;
80 int jtag_verify_capture_ir = 1;
81 int jtag_verify = 1;
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 */
87 /* callbacks to inform high-level handlers about JTAG state changes */
88 jtag_event_callback_t *jtag_event_callbacks;
90 /* speed in kHz*/
91 static int speed_khz = 0;
92 /* flag if the kHz speed was defined */
93 static bool hasKHz = false;
95 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
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
107 #if BUILD_DUMMY == 1
108 extern jtag_interface_t dummy_interface;
109 #endif
111 #if BUILD_FT2232_FTD2XX == 1
112 extern jtag_interface_t ft2232_interface;
113 #endif
115 #if BUILD_FT2232_LIBFTDI == 1
116 extern jtag_interface_t ft2232_interface;
117 #endif
119 #if BUILD_AMTJTAGACCEL == 1
120 extern jtag_interface_t amt_jtagaccel_interface;
121 #endif
123 #if BUILD_EP93XX == 1
124 extern jtag_interface_t ep93xx_interface;
125 #endif
127 #if BUILD_AT91RM9200 == 1
128 extern jtag_interface_t at91rm9200_interface;
129 #endif
131 #if BUILD_GW16012 == 1
132 extern jtag_interface_t gw16012_interface;
133 #endif
135 #if BUILD_PRESTO_LIBFTDI == 1 || BUILD_PRESTO_FTD2XX == 1
136 extern jtag_interface_t presto_interface;
137 #endif
139 #if BUILD_USBPROG == 1
140 extern jtag_interface_t usbprog_interface;
141 #endif
143 #if BUILD_JLINK == 1
144 extern jtag_interface_t jlink_interface;
145 #endif
147 #if BUILD_VSLLINK == 1
148 extern jtag_interface_t vsllink_interface;
149 #endif
151 #if BUILD_RLINK == 1
152 extern jtag_interface_t rlink_interface;
153 #endif
155 #if BUILD_ARMJTAGEW == 1
156 extern jtag_interface_t armjtagew_interface;
157 #endif
158 #endif // standard drivers
161 * The list of built-in JTAG interfaces, containing entries for those
162 * drivers that were enabled by the @c configure script.
164 * The list should be defined to contain either one minidriver interface
165 * or some number of standard driver interfaces, never both.
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,
219 struct jtag_interface_s *jtag = NULL;
221 /* configuration */
222 static jtag_interface_t *jtag_interface = NULL;
223 int jtag_speed = 0;
225 /* jtag commands */
226 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
227 char *cmd, char **args, int argc);
228 static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
229 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
230 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
231 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
232 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
233 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
234 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
236 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
238 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
239 static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
240 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
241 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
242 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
244 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
245 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
246 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
248 jtag_tap_t *jtag_all_taps(void)
250 return __jtag_all_taps;
253 unsigned jtag_tap_count(void)
255 return jtag_num_taps;
258 unsigned jtag_tap_count_enabled(void)
260 jtag_tap_t *t = jtag_all_taps();
261 unsigned n = 0;
262 while(t)
264 if (t->enabled)
265 n++;
266 t = t->next_tap;
268 return n;
271 /// Append a new TAP to the chain of all taps.
272 void jtag_tap_add(struct jtag_tap_s *t)
274 t->abs_chain_position = jtag_num_taps++;
276 jtag_tap_t **tap = &__jtag_all_taps;
277 while(*tap != NULL)
278 tap = &(*tap)->next_tap;
279 *tap = t;
282 jtag_tap_t *jtag_tap_by_string(const char *s)
284 /* try by name first */
285 jtag_tap_t *t = jtag_all_taps();
286 while (t)
288 if (0 == strcmp(t->dotted_name, s))
289 return t;
290 t = t->next_tap;
293 /* no tap found by name, so try to parse the name as a number */
294 char *cp;
295 unsigned n = strtoul(s, &cp, 0);
296 if ((s == cp) || (*cp != 0))
297 return NULL;
299 return jtag_tap_by_abs_position(n);
302 jtag_tap_t *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
304 const char *cp = Jim_GetString(o, NULL);
305 jtag_tap_t *t = cp ? jtag_tap_by_string(cp) : NULL;
306 if (NULL == cp)
307 cp = "(unknown)";
308 if (NULL == t)
309 Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
310 return t;
313 /* returns a pointer to the n-th device in the scan chain */
314 jtag_tap_t *jtag_tap_by_abs_position(unsigned n)
316 jtag_tap_t *t = jtag_all_taps();
318 while (t && n-- > 0)
319 t = t->next_tap;
321 return t;
324 const char *jtag_tap_name(const jtag_tap_t *tap)
326 return (tap == NULL) ? "(unknown)" : tap->dotted_name;
330 int jtag_register_event_callback(int (*callback)(enum jtag_event event, void *priv), void *priv)
332 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
334 if (callback == NULL)
336 return ERROR_INVALID_ARGUMENTS;
339 if (*callbacks_p)
341 while ((*callbacks_p)->next)
342 callbacks_p = &((*callbacks_p)->next);
343 callbacks_p = &((*callbacks_p)->next);
346 (*callbacks_p) = malloc(sizeof(jtag_event_callback_t));
347 (*callbacks_p)->callback = callback;
348 (*callbacks_p)->priv = priv;
349 (*callbacks_p)->next = NULL;
351 return ERROR_OK;
354 int jtag_unregister_event_callback(int (*callback)(enum jtag_event event, void *priv))
356 jtag_event_callback_t **callbacks_p = &jtag_event_callbacks;
358 if (callback == NULL)
360 return ERROR_INVALID_ARGUMENTS;
363 while (*callbacks_p)
365 jtag_event_callback_t **next = &((*callbacks_p)->next);
366 if ((*callbacks_p)->callback == callback)
368 free(*callbacks_p);
369 *callbacks_p = *next;
371 callbacks_p = next;
374 return ERROR_OK;
377 int jtag_call_event_callbacks(enum jtag_event event)
379 jtag_event_callback_t *callback = jtag_event_callbacks;
381 LOG_DEBUG("jtag event: %s", jtag_event_strings[event]);
383 while (callback)
385 callback->callback(event, callback->priv);
386 callback = callback->next;
389 return ERROR_OK;
392 static void jtag_checks(void)
394 assert(jtag_trst == 0);
397 static void jtag_prelude(tap_state_t state)
399 jtag_checks();
401 assert(state!=TAP_INVALID);
403 cmd_queue_cur_state = state;
406 void jtag_alloc_in_value32(scan_field_t *field)
408 interface_jtag_alloc_in_value32(field);
411 void jtag_add_ir_scan_noverify(int in_count, const scan_field_t *in_fields,
412 tap_state_t state)
414 jtag_prelude(state);
416 int retval = interface_jtag_add_ir_scan(in_count, in_fields, state);
417 jtag_set_error(retval);
422 * Generate an IR SCAN with a list of scan fields with one entry for each enabled TAP.
424 * If the input field list contains an instruction value for a TAP then that is used
425 * otherwise the TAP is set to bypass.
427 * TAPs for which no fields are passed are marked as bypassed for subsequent DR SCANs.
430 void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
432 if (jtag_verify&&jtag_verify_capture_ir)
434 /* 8 x 32 bit id's is enough for all invocations */
436 for (int j = 0; j < in_num_fields; j++)
438 /* if we are to run a verification of the ir scan, we need to get the input back.
439 * We may have to allocate space if the caller didn't ask for the input back.
441 in_fields[j].check_value=in_fields[j].tap->expected;
442 in_fields[j].check_mask=in_fields[j].tap->expected_mask;
444 jtag_add_scan_check(jtag_add_ir_scan_noverify, in_num_fields, in_fields, state);
445 } else
447 jtag_add_ir_scan_noverify(in_num_fields, in_fields, state);
452 * Duplicate the scan fields passed into the function into an IR SCAN command
454 * This function assumes that the caller handles extra fields for bypassed TAPs
457 void jtag_add_plain_ir_scan(int in_num_fields, const scan_field_t *in_fields,
458 tap_state_t state)
460 jtag_prelude(state);
462 int retval = interface_jtag_add_plain_ir_scan(
463 in_num_fields, in_fields, state);
464 jtag_set_error(retval);
467 void jtag_add_callback(jtag_callback1_t f, u8 *in)
469 interface_jtag_add_callback(f, in);
472 void jtag_add_callback4(jtag_callback_t f, u8 *in,
473 jtag_callback_data_t data1, jtag_callback_data_t data2,
474 jtag_callback_data_t data3)
476 interface_jtag_add_callback4(f, in, data1, data2, data3);
479 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits);
481 static int jtag_check_value_mask_callback(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
483 return jtag_check_value_inner(in, (u8 *)data1, (u8 *)data2, (int)data3);
486 static void jtag_add_scan_check(void (*jtag_add_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state),
487 int in_num_fields, scan_field_t *in_fields, tap_state_t state)
489 for (int i = 0; i < in_num_fields; i++)
491 struct scan_field_s *field = &in_fields[i];
492 field->allocated = 0;
493 field->modified = 0;
494 if (field->check_value || field->in_value)
495 continue;
496 interface_jtag_add_scan_check_alloc(field);
497 field->modified = 1;
500 jtag_add_scan(in_num_fields, in_fields, state);
502 for (int i = 0; i < in_num_fields; i++)
504 if ((in_fields[i].check_value != NULL) && (in_fields[i].in_value != NULL))
506 /* this is synchronous for a minidriver */
507 jtag_add_callback4(jtag_check_value_mask_callback, in_fields[i].in_value,
508 (jtag_callback_data_t)in_fields[i].check_value,
509 (jtag_callback_data_t)in_fields[i].check_mask,
510 (jtag_callback_data_t)in_fields[i].num_bits);
512 if (in_fields[i].allocated)
514 free(in_fields[i].in_value);
516 if (in_fields[i].modified)
518 in_fields[i].in_value = NULL;
523 void jtag_add_dr_scan_check(int in_num_fields, scan_field_t *in_fields, tap_state_t state)
525 if (jtag_verify)
527 jtag_add_scan_check(jtag_add_dr_scan, in_num_fields, in_fields, state);
528 } else
530 jtag_add_dr_scan(in_num_fields, in_fields, state);
536 * Generate a DR SCAN using the fields passed to the function.
537 * For connected TAPs, the function checks in_fields and uses fields
538 * specified there. For bypassed TAPs, the function generates a dummy
539 * 1-bit field. The bypass status of TAPs is set by jtag_add_ir_scan().
541 void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields,
542 tap_state_t state)
544 jtag_prelude(state);
546 int retval;
547 retval = interface_jtag_add_dr_scan(in_num_fields, in_fields, state);
548 jtag_set_error(retval);
552 * Duplicate the scan fields passed into the function into a DR SCAN
553 * command. Unlike jtag_add_dr_scan(), this function assumes that the
554 * caller handles extra fields for bypassed TAPs.
556 void jtag_add_plain_dr_scan(int in_num_fields, const scan_field_t *in_fields,
557 tap_state_t state)
559 jtag_prelude(state);
561 int retval;
562 retval = interface_jtag_add_plain_dr_scan(in_num_fields, in_fields, state);
563 jtag_set_error(retval);
566 void jtag_add_dr_out(jtag_tap_t* tap,
567 int num_fields, const int* num_bits, const u32* value,
568 tap_state_t end_state)
570 assert(end_state != TAP_INVALID);
572 cmd_queue_cur_state = end_state;
574 interface_jtag_add_dr_out(tap,
575 num_fields, num_bits, value,
576 end_state);
579 void jtag_add_tlr(void)
581 jtag_prelude(TAP_RESET);
582 jtag_set_error(interface_jtag_add_tlr());
583 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
586 void jtag_add_pathmove(int num_states, const tap_state_t *path)
588 tap_state_t cur_state = cmd_queue_cur_state;
590 /* the last state has to be a stable state */
591 if (!tap_is_state_stable(path[num_states - 1]))
593 LOG_ERROR("BUG: TAP path doesn't finish in a stable state");
594 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
595 return;
598 for (int i = 0; i < num_states; i++)
600 if (path[i] == TAP_RESET)
602 LOG_ERROR("BUG: TAP_RESET is not a valid state for pathmove sequences");
603 jtag_set_error(ERROR_JTAG_STATE_INVALID);
604 return;
607 if ( tap_state_transition(cur_state, true) != path[i]
608 && tap_state_transition(cur_state, false) != path[i])
610 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
611 tap_state_name(cur_state), tap_state_name(path[i]));
612 jtag_set_error(ERROR_JTAG_TRANSITION_INVALID);
613 return;
615 cur_state = path[i];
618 jtag_checks();
620 jtag_set_error(interface_jtag_add_pathmove(num_states, path));
621 cmd_queue_cur_state = path[num_states - 1];
624 void jtag_add_runtest(int num_cycles, tap_state_t state)
626 jtag_prelude(state);
627 jtag_set_error(interface_jtag_add_runtest(num_cycles, state));
631 void jtag_add_clocks(int num_cycles)
633 if (!tap_is_state_stable(cmd_queue_cur_state))
635 LOG_ERROR("jtag_add_clocks() called with TAP in unstable state \"%s\"",
636 tap_state_name(cmd_queue_cur_state));
637 jtag_set_error(ERROR_JTAG_NOT_STABLE_STATE);
638 return;
641 if (num_cycles > 0)
643 jtag_checks();
644 jtag_set_error(interface_jtag_add_clocks(num_cycles));
648 void jtag_add_reset(int req_tlr_or_trst, int req_srst)
650 int trst_with_tlr = 0;
652 /* FIX!!! there are *many* different cases here. A better
653 * approach is needed for legal combinations of transitions...
655 if ((jtag_reset_config & RESET_HAS_SRST)&&
656 (jtag_reset_config & RESET_HAS_TRST)&&
657 ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
659 if (((req_tlr_or_trst&&!jtag_trst)||
660 (!req_tlr_or_trst&&jtag_trst))&&
661 ((req_srst&&!jtag_srst)||
662 (!req_srst&&jtag_srst)))
664 /* FIX!!! srst_pulls_trst allows 1,1 => 0,0 transition.... */
665 //LOG_ERROR("BUG: transition of req_tlr_or_trst and req_srst in the same jtag_add_reset() call is undefined");
669 /* Make sure that jtag_reset_config allows the requested reset */
670 /* if SRST pulls TRST, we can't fulfill srst == 1 with trst == 0 */
671 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) && (req_srst == 1)) && (!req_tlr_or_trst))
673 LOG_ERROR("BUG: requested reset would assert trst");
674 jtag_set_error(ERROR_FAIL);
675 return;
678 /* if TRST pulls SRST, we reset with TAP T-L-R */
679 if (((jtag_reset_config & RESET_TRST_PULLS_SRST) && (req_tlr_or_trst)) && (req_srst == 0))
681 trst_with_tlr = 1;
684 if (req_srst && !(jtag_reset_config & RESET_HAS_SRST))
686 LOG_ERROR("BUG: requested SRST assertion, but the current configuration doesn't support this");
687 jtag_set_error(ERROR_FAIL);
688 return;
691 if (req_tlr_or_trst)
693 if (!trst_with_tlr && (jtag_reset_config & RESET_HAS_TRST))
695 jtag_trst = 1;
696 } else
698 trst_with_tlr = 1;
700 } else
702 jtag_trst = 0;
705 jtag_srst = req_srst;
707 int retval = interface_jtag_add_reset(jtag_trst, jtag_srst);
708 if (retval != ERROR_OK)
710 jtag_set_error(retval);
711 return;
713 jtag_execute_queue();
715 if (jtag_srst)
717 LOG_DEBUG("SRST line asserted");
719 else
721 LOG_DEBUG("SRST line released");
722 if (jtag_nsrst_delay)
723 jtag_add_sleep(jtag_nsrst_delay * 1000);
726 if (trst_with_tlr)
728 LOG_DEBUG("JTAG reset with RESET instead of TRST");
729 jtag_set_end_state(TAP_RESET);
730 jtag_add_tlr();
731 return;
734 if (jtag_trst)
736 /* we just asserted nTRST, so we're now in Test-Logic-Reset,
737 * and inform possible listeners about this
739 LOG_DEBUG("TRST line asserted");
740 tap_set_state(TAP_RESET);
741 jtag_call_event_callbacks(JTAG_TRST_ASSERTED);
743 else
745 if (jtag_ntrst_delay)
746 jtag_add_sleep(jtag_ntrst_delay * 1000);
750 tap_state_t jtag_set_end_state(tap_state_t state)
752 if ((state == TAP_DRSHIFT)||(state == TAP_IRSHIFT))
754 LOG_ERROR("BUG: TAP_DRSHIFT/IRSHIFT can't be end state. Calling code should use a larger scan field");
757 if (state!=TAP_INVALID)
758 cmd_queue_end_state = state;
759 return cmd_queue_end_state;
762 tap_state_t jtag_get_end_state(void)
764 return cmd_queue_end_state;
767 void jtag_add_sleep(u32 us)
769 /// @todo Here, keep_alive() appears to be a layering violation!!!
770 keep_alive();
771 jtag_set_error(interface_jtag_add_sleep(us));
774 int jtag_check_value_inner(u8 *captured, u8 *in_check_value, u8 *in_check_mask, int num_bits)
776 int retval = ERROR_OK;
778 int compare_failed = 0;
780 if (in_check_mask)
781 compare_failed = buf_cmp_mask(captured, in_check_value, in_check_mask, num_bits);
782 else
783 compare_failed = buf_cmp(captured, in_check_value, num_bits);
785 if (compare_failed){
786 /* An error handler could have caught the failing check
787 * only report a problem when there wasn't a handler, or if the handler
788 * acknowledged the error
791 LOG_WARNING("TAP %s:",
792 jtag_tap_name(field->tap));
794 if (compare_failed)
796 char *captured_char = buf_to_str(captured, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
797 char *in_check_value_char = buf_to_str(in_check_value, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
799 if (in_check_mask)
801 char *in_check_mask_char;
802 in_check_mask_char = buf_to_str(in_check_mask, (num_bits > DEBUG_JTAG_IOZ) ? DEBUG_JTAG_IOZ : num_bits, 16);
803 LOG_WARNING("value captured during scan didn't pass the requested check:");
804 LOG_WARNING("captured: 0x%s check_value: 0x%s check_mask: 0x%s",
805 captured_char, in_check_value_char, in_check_mask_char);
806 free(in_check_mask_char);
808 else
810 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);
813 free(captured_char);
814 free(in_check_value_char);
816 retval = ERROR_JTAG_QUEUE_FAILED;
820 return retval;
823 void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask)
825 assert(field->in_value != NULL);
827 if (value==NULL)
829 /* no checking to do */
830 return;
833 jtag_execute_queue_noclear();
835 int retval=jtag_check_value_inner(field->in_value, value, mask, field->num_bits);
836 jtag_set_error(retval);
841 int default_interface_jtag_execute_queue(void)
843 if (NULL == jtag)
845 LOG_ERROR("No JTAG interface configured yet. "
846 "Issue 'init' command in startup scripts "
847 "before communicating with targets.");
848 return ERROR_FAIL;
851 return jtag->execute_queue();
854 void jtag_execute_queue_noclear(void)
856 jtag_flush_queue_count++;
857 jtag_set_error(interface_jtag_execute_queue());
860 int jtag_get_flush_queue_count(void)
862 return jtag_flush_queue_count;
865 int jtag_execute_queue(void)
867 jtag_execute_queue_noclear();
868 return jtag_error_clear();
871 static int jtag_reset_callback(enum jtag_event event, void *priv)
873 jtag_tap_t *tap = priv;
875 LOG_DEBUG("-");
877 if (event == JTAG_TRST_ASSERTED)
879 buf_set_ones(tap->cur_instr, tap->ir_length);
880 tap->bypass = 1;
883 return ERROR_OK;
886 void jtag_sleep(u32 us)
888 alive_sleep(us/1000);
891 /// maximum number of JTAG devices expected in the chain
892 #define JTAG_MAX_CHAIN_SIZE 20
894 #define EXTRACT_MFG(X) (((X) & 0xffe) >> 1)
895 #define EXTRACT_PART(X) (((X) & 0xffff000) >> 12)
896 #define EXTRACT_VER(X) (((X) & 0xf0000000) >> 28)
898 static int jtag_examine_chain_execute(u8 *idcode_buffer, unsigned num_idcode)
900 scan_field_t field = {
901 .tap = NULL,
902 .num_bits = num_idcode * 32,
903 .out_value = idcode_buffer,
904 .in_value = idcode_buffer,
907 // initialize to the end of chain ID value
908 for (unsigned i = 0; i < JTAG_MAX_CHAIN_SIZE; i++)
909 buf_set_u32(idcode_buffer, i * 32, 32, 0x000000FF);
911 jtag_add_plain_dr_scan(1, &field, TAP_RESET);
912 return jtag_execute_queue();
915 static bool jtag_examine_chain_check(u8 *idcodes, unsigned count)
917 u8 zero_check = 0x0;
918 u8 one_check = 0xff;
920 for (unsigned i = 0; i < count * 4; i++)
922 zero_check |= idcodes[i];
923 one_check &= idcodes[i];
926 /* if there wasn't a single non-zero bit or if all bits were one,
927 * the scan is not valid */
928 if (zero_check == 0x00 || one_check == 0xff)
930 LOG_ERROR("JTAG communication failure: check connection, "
931 "JTAG interface, target power etc.");
932 return false;
934 return true;
937 static void jtag_examine_chain_display(enum log_levels level, const char *msg,
938 const char *name, u32 idcode)
940 log_printf_lf(level, __FILE__, __LINE__, __FUNCTION__,
941 "JTAG tap: %s %16.16s: 0x%08x "
942 "(mfg: 0x%3.3x, part: 0x%4.4x, ver: 0x%1.1x)",
943 name, msg, idcode,
944 EXTRACT_MFG(idcode), EXTRACT_PART(idcode), EXTRACT_VER(idcode) );
947 static bool jtag_idcode_is_final(u32 idcode)
949 return idcode == 0x000000FF || idcode == 0xFFFFFFFF;
953 * This helper checks that remaining bits in the examined chain data are
954 * all as expected, but a single JTAG device requires only 64 bits to be
955 * read back correctly. This can help identify and diagnose problems
956 * with the JTAG chain earlier, gives more helpful/explicit error messages.
958 static void jtag_examine_chain_end(u8 *idcodes, unsigned count, unsigned max)
960 bool triggered = false;
961 for ( ; count < max - 31; count += 32)
963 u32 idcode = buf_get_u32(idcodes, count, 32);
964 // do not trigger the warning if the data looks good
965 if (!triggered && jtag_idcode_is_final(idcode))
966 continue;
967 LOG_WARNING("Unexpected idcode after end of chain: %d 0x%08x",
968 count, idcode);
969 triggered = true;
973 static bool jtag_examine_chain_match_tap(const struct jtag_tap_s *tap)
975 if (0 == tap->expected_ids_cnt)
977 /// @todo Enable LOG_INFO to ask for reports about unknown TAP IDs.
978 #if 0
979 LOG_INFO("Uknown JTAG TAP ID: 0x%08x", tap->idcode)
980 LOG_INFO("Please report the chip name and reported ID code to the openocd project");
981 #endif
982 return true;
985 /* Loop over the expected identification codes and test for a match */
986 u8 ii;
987 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
989 if (tap->idcode == tap->expected_ids[ii])
990 break;
993 /* If none of the expected ids matched, log an error */
994 if (ii != tap->expected_ids_cnt)
996 LOG_INFO("JTAG Tap/device matched");
997 return true;
999 jtag_examine_chain_display(LOG_LVL_ERROR, "got",
1000 tap->dotted_name, tap->idcode);
1001 for (ii = 0; ii < tap->expected_ids_cnt; ii++)
1003 char msg[32];
1004 snprintf(msg, sizeof(msg), "expected %hhu of %hhu",
1005 ii + 1, tap->expected_ids_cnt);
1006 jtag_examine_chain_display(LOG_LVL_ERROR, msg,
1007 tap->dotted_name, tap->expected_ids[ii]);
1009 return false;
1012 /* Try to examine chain layout according to IEEE 1149.1 §12
1014 static int jtag_examine_chain(void)
1016 u8 idcode_buffer[JTAG_MAX_CHAIN_SIZE * 4];
1017 unsigned device_count = 0;
1019 jtag_examine_chain_execute(idcode_buffer, JTAG_MAX_CHAIN_SIZE);
1021 if (!jtag_examine_chain_check(idcode_buffer, JTAG_MAX_CHAIN_SIZE))
1022 return ERROR_JTAG_INIT_FAILED;
1024 /* point at the 1st tap */
1025 jtag_tap_t *tap = jtag_tap_next_enabled(NULL);
1026 if (tap == NULL)
1028 LOG_ERROR("JTAG: No taps enabled?");
1029 return ERROR_JTAG_INIT_FAILED;
1032 for (unsigned bit_count = 0; bit_count < (JTAG_MAX_CHAIN_SIZE * 32) - 31;)
1034 u32 idcode = buf_get_u32(idcode_buffer, bit_count, 32);
1035 if ((idcode & 1) == 0)
1037 /* LSB must not be 0, this indicates a device in bypass */
1038 LOG_WARNING("Tap/Device does not have IDCODE");
1039 idcode = 0;
1041 bit_count += 1;
1043 else
1046 * End of chain (invalid manufacturer ID) some devices, such
1047 * as AVR will output all 1's instead of TDI input value at
1048 * end of chain.
1050 if (jtag_idcode_is_final(idcode))
1052 jtag_examine_chain_end(idcode_buffer,
1053 bit_count + 32, JTAG_MAX_CHAIN_SIZE * 32);
1054 break;
1057 jtag_examine_chain_display(LOG_LVL_INFO, "tap/device found",
1058 tap ? tap->dotted_name : "(not-named)",
1059 idcode);
1061 bit_count += 32;
1063 device_count++;
1064 if (!tap)
1065 continue;
1067 tap->idcode = idcode;
1069 // ensure the TAP ID does matches what was expected
1070 if (!jtag_examine_chain_match_tap(tap))
1071 return ERROR_JTAG_INIT_FAILED;
1073 tap = jtag_tap_next_enabled(tap);
1076 /* see if number of discovered devices matches configuration */
1077 if (device_count != jtag_tap_count_enabled())
1079 LOG_ERROR("number of discovered devices in JTAG chain (%i) "
1080 "does not match (enabled) configuration (%i), total taps: %d",
1081 device_count, jtag_tap_count_enabled(), jtag_tap_count());
1082 LOG_ERROR("check the config file and ensure proper JTAG communication"
1083 " (connections, speed, ...)");
1084 return ERROR_JTAG_INIT_FAILED;
1087 return ERROR_OK;
1090 static int jtag_validate_chain(void)
1092 jtag_tap_t *tap;
1093 int total_ir_length = 0;
1094 u8 *ir_test = NULL;
1095 scan_field_t field;
1096 int chain_pos = 0;
1098 tap = NULL;
1099 total_ir_length = 0;
1100 for(;;){
1101 tap = jtag_tap_next_enabled(tap);
1102 if( tap == NULL ){
1103 break;
1105 total_ir_length += tap->ir_length;
1108 total_ir_length += 2;
1109 ir_test = malloc(CEIL(total_ir_length, 8));
1110 buf_set_ones(ir_test, total_ir_length);
1112 field.tap = NULL;
1113 field.num_bits = total_ir_length;
1114 field.out_value = ir_test;
1115 field.in_value = ir_test;
1118 jtag_add_plain_ir_scan(1, &field, TAP_RESET);
1119 jtag_execute_queue();
1121 tap = NULL;
1122 chain_pos = 0;
1123 int val;
1124 for(;;){
1125 tap = jtag_tap_next_enabled(tap);
1126 if( tap == NULL ){
1127 break;
1130 val = buf_get_u32(ir_test, chain_pos, 2);
1131 if (val != 0x1)
1133 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1134 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);
1135 free(cbuf);
1136 free(ir_test);
1137 return ERROR_JTAG_INIT_FAILED;
1139 chain_pos += tap->ir_length;
1142 val = buf_get_u32(ir_test, chain_pos, 2);
1143 if (val != 0x3)
1145 char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
1146 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);
1147 free(cbuf);
1148 free(ir_test);
1149 return ERROR_JTAG_INIT_FAILED;
1152 free(ir_test);
1154 return ERROR_OK;
1157 enum jtag_tap_cfg_param {
1158 JCFG_EVENT
1161 static Jim_Nvp nvp_config_opts[] = {
1162 { .name = "-event", .value = JCFG_EVENT },
1164 { .name = NULL, .value = -1 }
1167 static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
1169 Jim_Nvp *n;
1170 Jim_Obj *o;
1171 int e;
1173 /* parse config or cget options */
1174 while (goi->argc > 0) {
1175 Jim_SetEmptyResult (goi->interp);
1177 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
1178 if (e != JIM_OK) {
1179 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
1180 return e;
1183 switch (n->value) {
1184 case JCFG_EVENT:
1185 if (goi->argc == 0) {
1186 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ..." );
1187 return JIM_ERR;
1190 e = Jim_GetOpt_Nvp( goi, nvp_jtag_tap_event, &n );
1191 if (e != JIM_OK) {
1192 Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
1193 return e;
1196 if (goi->isconfigure) {
1197 if (goi->argc != 1) {
1198 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
1199 return JIM_ERR;
1201 } else {
1202 if (goi->argc != 0) {
1203 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
1204 return JIM_ERR;
1209 jtag_tap_event_action_t *jteap;
1211 jteap = tap->event_action;
1212 /* replace existing? */
1213 while (jteap) {
1214 if (jteap->event == (enum jtag_tap_event)n->value) {
1215 break;
1217 jteap = jteap->next;
1220 if (goi->isconfigure) {
1221 if (jteap == NULL) {
1222 /* create new */
1223 jteap = calloc(1, sizeof (*jteap));
1225 jteap->event = n->value;
1226 Jim_GetOpt_Obj( goi, &o);
1227 if (jteap->body) {
1228 Jim_DecrRefCount(interp, jteap->body);
1230 jteap->body = Jim_DuplicateObj(goi->interp, o);
1231 Jim_IncrRefCount(jteap->body);
1233 /* add to head of event list */
1234 jteap->next = tap->event_action;
1235 tap->event_action = jteap;
1236 Jim_SetEmptyResult(goi->interp);
1237 } else {
1238 /* get */
1239 if (jteap == NULL) {
1240 Jim_SetEmptyResult(goi->interp);
1241 } else {
1242 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
1246 /* loop for more */
1247 break;
1249 } /* while (goi->argc) */
1251 return JIM_OK;
1255 static void jtag_tap_init(jtag_tap_t *tap)
1257 assert(0 != tap->ir_length);
1259 tap->expected = malloc(tap->ir_length);
1260 tap->expected_mask = malloc(tap->ir_length);
1261 tap->cur_instr = malloc(tap->ir_length);
1263 buf_set_u32(tap->expected, 0, tap->ir_length, tap->ir_capture_value);
1264 buf_set_u32(tap->expected_mask, 0, tap->ir_length, tap->ir_capture_mask);
1265 buf_set_ones(tap->cur_instr, tap->ir_length);
1267 // place TAP in bypass mode
1268 tap->bypass = 1;
1269 // register the reset callback for the TAP
1270 jtag_register_event_callback(&jtag_reset_callback, tap);
1272 LOG_DEBUG("Created Tap: %s @ abs position %d, "
1273 "irlen %d, capture: 0x%x mask: 0x%x", tap->dotted_name,
1274 tap->abs_chain_position, tap->ir_length,
1275 tap->ir_capture_value, tap->ir_capture_mask);
1276 jtag_tap_add(tap);
1279 static void jtag_tap_free(jtag_tap_t *tap)
1281 /// @todo is anything missing? no memory leaks please
1282 free((void *)tap->expected_ids);
1283 free((void *)tap->chip);
1284 free((void *)tap->tapname);
1285 free((void *)tap->dotted_name);
1286 free(tap);
1289 static int jim_newtap_cmd( Jim_GetOptInfo *goi )
1291 jtag_tap_t *pTap;
1292 jim_wide w;
1293 int x;
1294 int e;
1295 int reqbits;
1296 Jim_Nvp *n;
1297 char *cp;
1298 const Jim_Nvp opts[] = {
1299 #define NTAP_OPT_IRLEN 0
1300 { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
1301 #define NTAP_OPT_IRMASK 1
1302 { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
1303 #define NTAP_OPT_IRCAPTURE 2
1304 { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
1305 #define NTAP_OPT_ENABLED 3
1306 { .name = "-enable" , .value = NTAP_OPT_ENABLED },
1307 #define NTAP_OPT_DISABLED 4
1308 { .name = "-disable" , .value = NTAP_OPT_DISABLED },
1309 #define NTAP_OPT_EXPECTED_ID 5
1310 { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
1311 { .name = NULL , .value = -1 },
1314 pTap = malloc( sizeof(jtag_tap_t) );
1315 memset( pTap, 0, sizeof(*pTap) );
1316 if( !pTap ){
1317 Jim_SetResult_sprintf( goi->interp, "no memory");
1318 return JIM_ERR;
1321 * we expect CHIP + TAP + OPTIONS
1322 * */
1323 if( goi->argc < 3 ){
1324 Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
1325 return JIM_ERR;
1327 Jim_GetOpt_String( goi, &cp, NULL );
1328 pTap->chip = strdup(cp);
1330 Jim_GetOpt_String( goi, &cp, NULL );
1331 pTap->tapname = strdup(cp);
1333 /* name + dot + name + null */
1334 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
1335 cp = malloc( x );
1336 sprintf( cp, "%s.%s", pTap->chip, pTap->tapname );
1337 pTap->dotted_name = cp;
1339 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
1340 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
1342 /* default is enabled */
1343 pTap->enabled = 1;
1345 /* deal with options */
1346 #define NTREQ_IRLEN 1
1347 #define NTREQ_IRCAPTURE 2
1348 #define NTREQ_IRMASK 4
1350 /* clear them as we find them */
1351 reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
1353 while( goi->argc ){
1354 e = Jim_GetOpt_Nvp( goi, opts, &n );
1355 if( e != JIM_OK ){
1356 Jim_GetOpt_NvpUnknown( goi, opts, 0 );
1357 return e;
1359 LOG_DEBUG("Processing option: %s", n->name );
1360 switch( n->value ){
1361 case NTAP_OPT_ENABLED:
1362 pTap->enabled = 1;
1363 break;
1364 case NTAP_OPT_DISABLED:
1365 pTap->enabled = 0;
1366 break;
1367 case NTAP_OPT_EXPECTED_ID:
1369 u32 *new_expected_ids;
1371 e = Jim_GetOpt_Wide( goi, &w );
1372 if( e != JIM_OK) {
1373 Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
1374 return e;
1377 new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
1378 if (new_expected_ids == NULL) {
1379 Jim_SetResult_sprintf( goi->interp, "no memory");
1380 return JIM_ERR;
1383 memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
1385 new_expected_ids[pTap->expected_ids_cnt] = w;
1387 free(pTap->expected_ids);
1388 pTap->expected_ids = new_expected_ids;
1389 pTap->expected_ids_cnt++;
1390 break;
1392 case NTAP_OPT_IRLEN:
1393 case NTAP_OPT_IRMASK:
1394 case NTAP_OPT_IRCAPTURE:
1395 e = Jim_GetOpt_Wide( goi, &w );
1396 if( e != JIM_OK ){
1397 Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
1398 return e;
1400 if( (w < 0) || (w > 0xffff) ){
1401 /* wacky value */
1402 Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
1403 n->name, (int)(w), (int)(w));
1404 return JIM_ERR;
1406 switch(n->value){
1407 case NTAP_OPT_IRLEN:
1408 pTap->ir_length = w;
1409 reqbits &= (~(NTREQ_IRLEN));
1410 break;
1411 case NTAP_OPT_IRMASK:
1412 pTap->ir_capture_mask = w;
1413 reqbits &= (~(NTREQ_IRMASK));
1414 break;
1415 case NTAP_OPT_IRCAPTURE:
1416 pTap->ir_capture_value = w;
1417 reqbits &= (~(NTREQ_IRCAPTURE));
1418 break;
1420 } /* switch(n->value) */
1421 } /* while( goi->argc ) */
1423 /* Did all the required option bits get cleared? */
1424 if (0 == reqbits)
1426 jtag_tap_init(pTap);
1427 return ERROR_OK;
1430 Jim_SetResult_sprintf(goi->interp,
1431 "newtap: %s missing required parameters",
1432 pTap->dotted_name);
1433 jtag_tap_free(pTap);
1434 return JIM_ERR;
1437 static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
1439 Jim_GetOptInfo goi;
1440 int e;
1441 Jim_Nvp *n;
1442 Jim_Obj *o;
1443 struct command_context_s *context;
1445 enum {
1446 JTAG_CMD_INTERFACE,
1447 JTAG_CMD_INIT_RESET,
1448 JTAG_CMD_NEWTAP,
1449 JTAG_CMD_TAPENABLE,
1450 JTAG_CMD_TAPDISABLE,
1451 JTAG_CMD_TAPISENABLED,
1452 JTAG_CMD_CONFIGURE,
1453 JTAG_CMD_CGET
1456 const Jim_Nvp jtag_cmds[] = {
1457 { .name = "interface" , .value = JTAG_CMD_INTERFACE },
1458 { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
1459 { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
1460 { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
1461 { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
1462 { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
1463 { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
1464 { .name = "cget" , .value = JTAG_CMD_CGET },
1466 { .name = NULL, .value = -1 },
1469 context = Jim_GetAssocData(interp, "context");
1470 /* go past the command */
1471 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
1473 e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
1474 if( e != JIM_OK ){
1475 Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
1476 return e;
1478 Jim_SetEmptyResult( goi.interp );
1479 switch( n->value ){
1480 case JTAG_CMD_INTERFACE:
1481 /* return the name of the interface */
1482 /* TCL code might need to know the exact type... */
1483 /* FUTURE: we allow this as a means to "set" the interface. */
1484 if( goi.argc != 0 ){
1485 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1486 return JIM_ERR;
1488 Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
1489 return JIM_OK;
1490 case JTAG_CMD_INIT_RESET:
1491 if( goi.argc != 0 ){
1492 Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
1493 return JIM_ERR;
1495 e = jtag_init_reset(context);
1496 if( e != ERROR_OK ){
1497 Jim_SetResult_sprintf( goi.interp, "error: %d", e);
1498 return JIM_ERR;
1500 return JIM_OK;
1501 case JTAG_CMD_NEWTAP:
1502 return jim_newtap_cmd( &goi );
1503 break;
1504 case JTAG_CMD_TAPISENABLED:
1505 case JTAG_CMD_TAPENABLE:
1506 case JTAG_CMD_TAPDISABLE:
1507 if( goi.argc != 1 ){
1508 Jim_SetResultString( goi.interp, "Too many parameters",-1 );
1509 return JIM_ERR;
1513 jtag_tap_t *t;
1514 t = jtag_tap_by_jim_obj( goi.interp, goi.argv[0] );
1515 if( t == NULL ){
1516 return JIM_ERR;
1518 switch( n->value ){
1519 case JTAG_CMD_TAPISENABLED:
1520 e = t->enabled;
1521 break;
1522 case JTAG_CMD_TAPENABLE:
1523 jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
1524 e = 1;
1525 t->enabled = e;
1526 break;
1527 case JTAG_CMD_TAPDISABLE:
1528 jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
1529 e = 0;
1530 t->enabled = e;
1531 break;
1533 Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
1534 return JIM_OK;
1536 break;
1538 case JTAG_CMD_CGET:
1539 if( goi.argc < 2 ){
1540 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
1541 return JIM_ERR;
1545 jtag_tap_t *t;
1547 Jim_GetOpt_Obj(&goi, &o);
1548 t = jtag_tap_by_jim_obj( goi.interp, o );
1549 if( t == NULL ){
1550 return JIM_ERR;
1553 goi.isconfigure = 0;
1554 return jtag_tap_configure_cmd( &goi, t);
1556 break;
1558 case JTAG_CMD_CONFIGURE:
1559 if( goi.argc < 3 ){
1560 Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
1561 return JIM_ERR;
1565 jtag_tap_t *t;
1567 Jim_GetOpt_Obj(&goi, &o);
1568 t = jtag_tap_by_jim_obj( goi.interp, o );
1569 if( t == NULL ){
1570 return JIM_ERR;
1573 goi.isconfigure = 1;
1574 return jtag_tap_configure_cmd( &goi, t);
1578 return JIM_ERR;
1581 int jtag_register_commands(struct command_context_s *cmd_ctx)
1583 register_jim( cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
1585 register_command(cmd_ctx, NULL, "interface", handle_interface_command,
1586 COMMAND_CONFIG, "try to configure interface");
1587 register_command(cmd_ctx, NULL,
1588 "interface_list", &handle_interface_list_command,
1589 COMMAND_ANY, "list all built-in interfaces");
1590 register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
1591 COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
1592 register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
1593 COMMAND_ANY, "set maximum jtag speed (if supported); "
1594 "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
1595 register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
1596 COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
1597 register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
1598 COMMAND_ANY,
1599 "[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]");
1600 register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
1601 COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
1602 register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
1603 COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
1605 register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
1606 COMMAND_EXEC, "print current scan chain configuration");
1608 register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
1609 COMMAND_EXEC, "toggle reset lines <trst> <srst>");
1610 register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
1611 COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
1612 register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
1613 COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
1614 register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
1615 register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
1617 register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
1618 COMMAND_ANY, "verify value captured during Capture-IR <enable|disable>");
1619 register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
1620 COMMAND_ANY, "verify value capture <enable|disable>");
1621 register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
1622 COMMAND_ANY, "choose short(default) or long tms_sequence <short|long>");
1623 return ERROR_OK;
1626 int jtag_interface_init(struct command_context_s *cmd_ctx)
1628 if (jtag)
1629 return ERROR_OK;
1631 if (!jtag_interface)
1633 /* nothing was previously specified by "interface" command */
1634 LOG_ERROR("JTAG interface has to be specified, see \"interface\" command");
1635 return ERROR_JTAG_INVALID_INTERFACE;
1637 if(hasKHz)
1639 jtag_interface->khz(jtag_get_speed_khz(), &jtag_speed);
1640 hasKHz = false;
1643 if (jtag_interface->init() != ERROR_OK)
1644 return ERROR_JTAG_INIT_FAILED;
1646 jtag = jtag_interface;
1647 return ERROR_OK;
1650 static int jtag_init_inner(struct command_context_s *cmd_ctx)
1652 jtag_tap_t *tap;
1653 int retval;
1655 LOG_DEBUG("Init JTAG chain");
1657 tap = jtag_tap_next_enabled(NULL);
1658 if( tap == NULL ){
1659 LOG_ERROR("There are no enabled taps?");
1660 return ERROR_JTAG_INIT_FAILED;
1663 jtag_add_tlr();
1664 if ((retval=jtag_execute_queue())!=ERROR_OK)
1665 return retval;
1667 /* examine chain first, as this could discover the real chain layout */
1668 if (jtag_examine_chain() != ERROR_OK)
1670 LOG_ERROR("trying to validate configured JTAG chain anyway...");
1673 if (jtag_validate_chain() != ERROR_OK)
1675 LOG_WARNING("Could not validate JTAG chain, continuing anyway...");
1678 return ERROR_OK;
1681 int jtag_interface_quit(void)
1683 if (!jtag || !jtag->quit)
1684 return ERROR_OK;
1686 // close the JTAG interface
1687 int result = jtag->quit();
1688 if (ERROR_OK != result)
1689 LOG_ERROR("failed: %d", result);
1691 return ERROR_OK;
1695 int jtag_init_reset(struct command_context_s *cmd_ctx)
1697 int retval;
1699 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1700 return retval;
1702 LOG_DEBUG("Trying to bring the JTAG controller to life by asserting TRST / RESET");
1704 /* Reset can happen after a power cycle.
1706 * Ideally we would only assert TRST or run RESET before the target reset.
1708 * However w/srst_pulls_trst, trst is asserted together with the target
1709 * reset whether we want it or not.
1711 * NB! Some targets have JTAG circuitry disabled until a
1712 * trst & srst has been asserted.
1714 * NB! here we assume nsrst/ntrst delay are sufficient!
1716 * NB! order matters!!!! srst *can* disconnect JTAG circuitry
1719 jtag_add_reset(1, 0); /* RESET or TRST */
1720 if (jtag_reset_config & RESET_HAS_SRST)
1722 jtag_add_reset(1, 1);
1723 if ((jtag_reset_config & RESET_SRST_PULLS_TRST)==0)
1724 jtag_add_reset(0, 1);
1726 jtag_add_reset(0, 0);
1727 if ((retval = jtag_execute_queue()) != ERROR_OK)
1728 return retval;
1730 /* Check that we can communication on the JTAG chain + eventually we want to
1731 * be able to perform enumeration only after OpenOCD has started
1732 * telnet and GDB server
1734 * That would allow users to more easily perform any magic they need to before
1735 * reset happens.
1737 return jtag_init_inner(cmd_ctx);
1740 int jtag_init(struct command_context_s *cmd_ctx)
1742 int retval;
1743 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
1744 return retval;
1745 if (jtag_init_inner(cmd_ctx)==ERROR_OK)
1747 return ERROR_OK;
1749 return jtag_init_reset(cmd_ctx);
1752 void jtag_set_speed_khz(unsigned khz)
1754 speed_khz = khz;
1756 unsigned jtag_get_speed_khz(void)
1758 return speed_khz;
1761 static int default_khz(int khz, int *jtag_speed)
1763 LOG_ERROR("Translation from khz to jtag_speed not implemented");
1764 return ERROR_FAIL;
1767 static int default_speed_div(int speed, int *khz)
1769 LOG_ERROR("Translation from jtag_speed to khz not implemented");
1770 return ERROR_FAIL;
1773 static int default_power_dropout(int *dropout)
1775 *dropout=0; /* by default we can't detect power dropout */
1776 return ERROR_OK;
1779 static int default_srst_asserted(int *srst_asserted)
1781 *srst_asserted=0; /* by default we can't detect srst asserted */
1782 return ERROR_OK;
1785 static int handle_interface_command(struct command_context_s *cmd_ctx,
1786 char *cmd, char **args, int argc)
1788 /* check whether the interface is already configured */
1789 if (jtag_interface)
1791 LOG_WARNING("Interface already configured, ignoring");
1792 return ERROR_OK;
1795 /* interface name is a mandatory argument */
1796 if (argc != 1 || args[0][0] == '\0')
1797 return ERROR_COMMAND_SYNTAX_ERROR;
1799 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
1801 if (strcmp(args[0], jtag_interfaces[i]->name) != 0)
1802 continue;
1804 int retval = jtag_interfaces[i]->register_commands(cmd_ctx);
1805 if (ERROR_OK != retval)
1806 return retval;
1808 jtag_interface = jtag_interfaces[i];
1810 if (jtag_interface->khz == NULL)
1811 jtag_interface->khz = default_khz;
1812 if (jtag_interface->speed_div == NULL)
1813 jtag_interface->speed_div = default_speed_div;
1814 if (jtag_interface->power_dropout == NULL)
1815 jtag_interface->power_dropout = default_power_dropout;
1816 if (jtag_interface->srst_asserted == NULL)
1817 jtag_interface->srst_asserted = default_srst_asserted;
1819 return ERROR_OK;
1822 /* no valid interface was found (i.e. the configuration option,
1823 * didn't match one of the compiled-in interfaces
1825 LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]);
1826 handle_interface_list_command(cmd_ctx, cmd, args, argc);
1827 return ERROR_JTAG_INVALID_INTERFACE;
1830 static int handle_interface_list_command(struct command_context_s *cmd_ctx,
1831 char *cmd, char **args, int argc)
1833 if (strcmp(cmd, "interface_list") == 0 && argc > 0)
1834 return ERROR_COMMAND_SYNTAX_ERROR;
1836 command_print(cmd_ctx, "The following JTAG interfaces are available:");
1837 for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
1839 const char *name = jtag_interfaces[i]->name;
1840 command_print(cmd_ctx, "%u: %s", i + 1, name);
1843 return ERROR_OK;
1846 static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1848 int e;
1849 char buf[1024];
1850 Jim_Obj *newargs[ 10 ];
1852 * CONVERT SYNTAX
1853 * argv[-1] = command
1854 * argv[ 0] = ir length
1855 * argv[ 1] = ir capture
1856 * argv[ 2] = ir mask
1857 * argv[ 3] = not actually used by anything but in the docs
1860 if( argc < 4 ){
1861 command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
1862 return ERROR_OK;
1864 command_print( cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
1865 command_print( cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
1866 args[0],
1867 args[1],
1868 args[2] );
1869 command_print( cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
1870 command_print( cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
1871 command_print( cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
1872 command_print( cmd_ctx, "And then refer to the taps by the dotted name.");
1874 newargs[0] = Jim_NewStringObj( interp, "jtag", -1 );
1875 newargs[1] = Jim_NewStringObj( interp, "newtap", -1 );
1876 sprintf( buf, "chip%d", jtag_tap_count() );
1877 newargs[2] = Jim_NewStringObj( interp, buf, -1 );
1878 sprintf( buf, "tap%d", jtag_tap_count() );
1879 newargs[3] = Jim_NewStringObj( interp, buf, -1 );
1880 newargs[4] = Jim_NewStringObj( interp, "-irlen", -1 );
1881 newargs[5] = Jim_NewStringObj( interp, args[0], -1 );
1882 newargs[6] = Jim_NewStringObj( interp, "-ircapture", -1 );
1883 newargs[7] = Jim_NewStringObj( interp, args[1], -1 );
1884 newargs[8] = Jim_NewStringObj( interp, "-irmask", -1 );
1885 newargs[9] = Jim_NewStringObj( interp, args[2], -1 );
1887 command_print( cmd_ctx, "NEW COMMAND:");
1888 sprintf( buf, "%s %s %s %s %s %s %s %s %s %s",
1889 Jim_GetString( newargs[0], NULL ),
1890 Jim_GetString( newargs[1], NULL ),
1891 Jim_GetString( newargs[2], NULL ),
1892 Jim_GetString( newargs[3], NULL ),
1893 Jim_GetString( newargs[4], NULL ),
1894 Jim_GetString( newargs[5], NULL ),
1895 Jim_GetString( newargs[6], NULL ),
1896 Jim_GetString( newargs[7], NULL ),
1897 Jim_GetString( newargs[8], NULL ),
1898 Jim_GetString( newargs[9], NULL ) );
1900 e = jim_jtag_command( interp, 10, newargs );
1901 if( e != JIM_OK ){
1902 command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
1904 return e;
1907 static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1909 jtag_tap_t *tap;
1911 tap = jtag_all_taps();
1912 command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
1913 command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
1915 while( tap ){
1916 u32 expected, expected_mask, cur_instr, ii;
1917 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
1918 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
1919 cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
1921 command_print(cmd_ctx,
1922 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
1923 tap->abs_chain_position,
1924 tap->dotted_name,
1925 tap->enabled ? 'Y' : 'n',
1926 tap->idcode,
1927 (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
1928 tap->ir_length,
1929 expected,
1930 expected_mask,
1931 cur_instr);
1933 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
1934 command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
1935 tap->expected_ids[ii]);
1938 tap = tap->next_tap;
1941 return ERROR_OK;
1944 static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1946 int new_cfg = 0;
1947 int mask = 0;
1949 if (argc < 1)
1950 return ERROR_COMMAND_SYNTAX_ERROR;
1952 /* Original versions cared about the order of these tokens:
1953 * reset_config signals [combination [trst_type [srst_type]]]
1954 * They also clobbered the previous configuration even on error.
1956 * Here we don't care about the order, and only change values
1957 * which have been explicitly specified.
1959 for (; argc; argc--, args++) {
1960 int tmp = 0;
1961 int m;
1963 /* signals */
1964 m = RESET_HAS_TRST | RESET_HAS_SRST;
1965 if (strcmp(*args, "none") == 0)
1966 tmp = RESET_NONE;
1967 else if (strcmp(*args, "trst_only") == 0)
1968 tmp = RESET_HAS_TRST;
1969 else if (strcmp(*args, "srst_only") == 0)
1970 tmp = RESET_HAS_SRST;
1971 else if (strcmp(*args, "trst_and_srst") == 0)
1972 tmp = RESET_HAS_TRST | RESET_HAS_SRST;
1973 else
1974 m = 0;
1975 if (mask & m) {
1976 LOG_ERROR("extra reset_config %s spec (%s)",
1977 "signal", *args);
1978 return ERROR_INVALID_ARGUMENTS;
1980 if (m)
1981 goto next;
1983 /* combination (options for broken wiring) */
1984 m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1985 if (strcmp(*args, "separate") == 0)
1986 /* separate reset lines - default */;
1987 else if (strcmp(*args, "srst_pulls_trst") == 0)
1988 tmp |= RESET_SRST_PULLS_TRST;
1989 else if (strcmp(*args, "trst_pulls_srst") == 0)
1990 tmp |= RESET_TRST_PULLS_SRST;
1991 else if (strcmp(*args, "combined") == 0)
1992 tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
1993 else
1994 m = 0;
1995 if (mask & m) {
1996 LOG_ERROR("extra reset_config %s spec (%s)",
1997 "combination", *args);
1998 return ERROR_INVALID_ARGUMENTS;
2000 if (m)
2001 goto next;
2003 /* trst_type (NOP without HAS_TRST) */
2004 m = RESET_TRST_OPEN_DRAIN;
2005 if (strcmp(*args, "trst_open_drain") == 0)
2006 tmp |= RESET_TRST_OPEN_DRAIN;
2007 else if (strcmp(*args, "trst_push_pull") == 0)
2008 /* push/pull from adapter - default */;
2009 else
2010 m = 0;
2011 if (mask & m) {
2012 LOG_ERROR("extra reset_config %s spec (%s)",
2013 "trst_type", *args);
2014 return ERROR_INVALID_ARGUMENTS;
2016 if (m)
2017 goto next;
2019 /* srst_type (NOP without HAS_SRST) */
2020 m |= RESET_SRST_PUSH_PULL;
2021 if (strcmp(*args, "srst_push_pull") == 0)
2022 tmp |= RESET_SRST_PUSH_PULL;
2023 else if (strcmp(*args, "srst_open_drain") == 0)
2024 /* open drain from adapter - default */;
2025 else
2026 m = 0;
2027 if (mask & m) {
2028 LOG_ERROR("extra reset_config %s spec (%s)",
2029 "srst_type", *args);
2030 return ERROR_INVALID_ARGUMENTS;
2032 if (m)
2033 goto next;
2035 /* caller provided nonsense; fail */
2036 LOG_ERROR("unknown reset_config flag (%s)", *args);
2037 return ERROR_INVALID_ARGUMENTS;
2039 next:
2040 /* Remember the bits which were specified (mask)
2041 * and their new values (new_cfg).
2043 mask |= m;
2044 new_cfg |= tmp;
2047 /* clear previous values of those bits, save new values */
2048 jtag_reset_config &= ~mask;
2049 jtag_reset_config |= new_cfg;
2051 return ERROR_OK;
2054 static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
2055 char *cmd, char **args, int argc)
2057 if (argc > 1)
2058 return ERROR_COMMAND_SYNTAX_ERROR;
2059 if (argc == 1)
2060 jtag_set_nsrst_delay(strtoul(args[0], NULL, 0));
2061 command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
2062 return ERROR_OK;
2065 static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
2066 char *cmd, char **args, int argc)
2068 if (argc > 1)
2069 return ERROR_COMMAND_SYNTAX_ERROR;
2070 if (argc == 1)
2071 jtag_set_ntrst_delay(strtoul(args[0], NULL, 0));
2072 command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
2073 return ERROR_OK;
2076 static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2078 int retval = ERROR_OK;
2080 if (argc > 1)
2081 return ERROR_COMMAND_SYNTAX_ERROR;
2082 if (argc == 1)
2084 LOG_DEBUG("handle jtag speed");
2086 int cur_speed = 0;
2087 cur_speed = jtag_speed = strtoul(args[0], NULL, 0);
2089 /* this command can be called during CONFIG,
2090 * in which case jtag isn't initialized */
2091 if (jtag)
2092 retval = jtag->speed(cur_speed);
2094 command_print(cmd_ctx, "jtag_speed: %d", jtag_speed);
2096 return retval;
2099 static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2101 if (argc > 1)
2102 return ERROR_COMMAND_SYNTAX_ERROR;
2104 int retval = ERROR_OK;
2105 int cur_speed = 0;
2106 if (argc == 1)
2108 LOG_DEBUG("handle jtag khz");
2110 jtag_set_speed_khz(strtoul(args[0], NULL, 0));
2111 if (jtag != NULL)
2113 LOG_DEBUG("have interface set up");
2114 int speed_div1;
2115 retval = jtag->khz(jtag_get_speed_khz(), &speed_div1);
2116 if (ERROR_OK != retval)
2118 jtag_set_speed_khz(0);
2119 return retval;
2121 cur_speed = jtag_speed = speed_div1;
2123 retval = jtag->speed(cur_speed);
2125 else
2126 hasKHz = true;
2129 cur_speed = jtag_get_speed_khz();
2130 if (jtag != NULL)
2132 retval = jtag->speed_div(jtag_speed, &cur_speed);
2133 if (ERROR_OK != retval)
2134 return retval;
2137 if (cur_speed)
2138 command_print(cmd_ctx, "%d kHz", cur_speed);
2139 else
2140 command_print(cmd_ctx, "RCLK - adaptive");
2141 return retval;
2145 static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
2146 char *cmd, char **args, int argc)
2148 if (argc != 2)
2149 return ERROR_COMMAND_SYNTAX_ERROR;
2151 int trst = -1;
2152 if (args[0][0] == '1')
2153 trst = 1;
2154 else if (args[0][0] == '0')
2155 trst = 0;
2156 else
2157 return ERROR_COMMAND_SYNTAX_ERROR;
2159 int srst = -1;
2160 if (args[1][0] == '1')
2161 srst = 1;
2162 else if (args[1][0] == '0')
2163 srst = 0;
2164 else
2165 return ERROR_COMMAND_SYNTAX_ERROR;
2167 if (jtag_interface_init(cmd_ctx) != ERROR_OK)
2168 return ERROR_JTAG_INIT_FAILED;
2170 jtag_add_reset(trst, srst);
2171 jtag_execute_queue();
2173 return ERROR_OK;
2176 static int handle_runtest_command(struct command_context_s *cmd_ctx,
2177 char *cmd, char **args, int argc)
2179 if (argc != 1)
2180 return ERROR_COMMAND_SYNTAX_ERROR;
2182 jtag_add_runtest(strtol(args[0], NULL, 0), jtag_get_end_state());
2183 jtag_execute_queue();
2185 return ERROR_OK;
2189 * For "irscan" or "drscan" commands, the "end" (really, "next") state
2190 * should be stable ... and *NOT* a shift state, otherwise free-running
2191 * jtag clocks could change the values latched by the update state.
2193 static bool scan_is_safe(tap_state_t state)
2195 switch (state)
2197 case TAP_RESET:
2198 case TAP_IDLE:
2199 case TAP_DRPAUSE:
2200 case TAP_IRPAUSE:
2201 return true;
2202 default:
2203 return false;
2208 static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2210 int i;
2211 scan_field_t *fields;
2212 jtag_tap_t *tap;
2213 tap_state_t endstate;
2215 if ((argc < 2) || (argc % 2))
2217 return ERROR_COMMAND_SYNTAX_ERROR;
2220 /* optional "-endstate" "statename" at the end of the arguments,
2221 * so that e.g. IRPAUSE can let us load the data register before
2222 * entering RUN/IDLE to execute the instruction we load here.
2224 endstate = TAP_IDLE;
2226 if( argc >= 4 ){
2227 /* have at least one pair of numbers. */
2228 /* is last pair the magic text? */
2229 if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
2230 const char *cpA;
2231 const char *cpS;
2232 cpA = args[ argc-1 ];
2233 for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
2234 cpS = tap_state_name( endstate );
2235 if( 0 == strcmp( cpA, cpS ) ){
2236 break;
2239 if( endstate >= TAP_NUM_STATES ){
2240 return ERROR_COMMAND_SYNTAX_ERROR;
2241 } else {
2242 if (!scan_is_safe(endstate))
2243 LOG_WARNING("irscan with unsafe "
2244 "endstate \"%s\"", cpA);
2245 /* found - remove the last 2 args */
2246 argc -= 2;
2251 int num_fields = argc / 2;
2253 fields = malloc(sizeof(scan_field_t) * num_fields);
2255 for (i = 0; i < num_fields; i++)
2257 tap = jtag_tap_by_string( args[i*2] );
2258 if (tap==NULL)
2260 command_print( cmd_ctx, "Tap: %s unknown", args[i*2] );
2261 return ERROR_FAIL;
2263 int field_size = tap->ir_length;
2264 fields[i].tap = tap;
2265 fields[i].num_bits = field_size;
2266 fields[i].out_value = malloc(CEIL(field_size, 8));
2267 buf_set_u32(fields[i].out_value, 0, field_size, strtoul(args[i*2+1], NULL, 0));
2268 fields[i].in_value = NULL;
2271 /* did we have an endstate? */
2272 jtag_add_ir_scan(num_fields, fields, endstate);
2274 int retval=jtag_execute_queue();
2276 for (i = 0; i < num_fields; i++)
2277 free(fields[i].out_value);
2279 free (fields);
2281 return retval;
2284 static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2286 int retval;
2287 scan_field_t *fields;
2288 int num_fields;
2289 int field_count = 0;
2290 int i, e;
2291 jtag_tap_t *tap;
2292 tap_state_t endstate;
2294 /* args[1] = device
2295 * args[2] = num_bits
2296 * args[3] = hex string
2297 * ... repeat num bits and hex string ...
2299 * .. optionally:
2300 * args[N-2] = "-endstate"
2301 * args[N-1] = statename
2303 if ((argc < 4) || ((argc % 2)!=0))
2305 Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
2306 return JIM_ERR;
2309 endstate = TAP_IDLE;
2311 /* validate arguments as numbers */
2312 e = JIM_OK;
2313 for (i = 2; i < argc; i+=2)
2315 long bits;
2316 const char *cp;
2318 e = Jim_GetLong(interp, args[i], &bits);
2319 /* If valid - try next arg */
2320 if( e == JIM_OK ){
2321 continue;
2324 /* Not valid.. are we at the end? */
2325 if ( ((i+2) != argc) ){
2326 /* nope, then error */
2327 return e;
2330 /* it could be: "-endstate FOO"
2331 * e.g. DRPAUSE so we can issue more instructions
2332 * before entering RUN/IDLE and executing them.
2335 /* get arg as a string. */
2336 cp = Jim_GetString( args[i], NULL );
2337 /* is it the magic? */
2338 if( 0 == strcmp( "-endstate", cp ) ){
2339 /* is the statename valid? */
2340 cp = Jim_GetString( args[i+1], NULL );
2342 /* see if it is a valid state name */
2343 endstate = tap_state_by_name(cp);
2344 if( endstate < 0 ){
2345 /* update the error message */
2346 Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
2347 } else {
2348 if (!scan_is_safe(endstate))
2349 LOG_WARNING("drscan with unsafe "
2350 "endstate \"%s\"", cp);
2352 /* valid - so clear the error */
2353 e = JIM_OK;
2354 /* and remove the last 2 args */
2355 argc -= 2;
2359 /* Still an error? */
2360 if( e != JIM_OK ){
2361 return e; /* too bad */
2363 } /* validate args */
2365 tap = jtag_tap_by_jim_obj( interp, args[1] );
2366 if( tap == NULL ){
2367 return JIM_ERR;
2370 num_fields=(argc-2)/2;
2371 fields = malloc(sizeof(scan_field_t) * num_fields);
2372 for (i = 2; i < argc; i+=2)
2374 long bits;
2375 int len;
2376 const char *str;
2378 Jim_GetLong(interp, args[i], &bits);
2379 str = Jim_GetString(args[i+1], &len);
2381 fields[field_count].tap = tap;
2382 fields[field_count].num_bits = bits;
2383 fields[field_count].out_value = malloc(CEIL(bits, 8));
2384 str_to_buf(str, len, fields[field_count].out_value, bits, 0);
2385 fields[field_count].in_value = fields[field_count].out_value;
2386 field_count++;
2389 jtag_add_dr_scan(num_fields, fields, endstate);
2391 retval = jtag_execute_queue();
2392 if (retval != ERROR_OK)
2394 Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
2395 return JIM_ERR;
2398 field_count=0;
2399 Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
2400 for (i = 2; i < argc; i+=2)
2402 long bits;
2403 char *str;
2405 Jim_GetLong(interp, args[i], &bits);
2406 str = buf_to_str(fields[field_count].in_value, bits, 16);
2407 free(fields[field_count].out_value);
2409 Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
2410 free(str);
2411 field_count++;
2414 Jim_SetResult(interp, list);
2416 free(fields);
2418 return JIM_OK;
2422 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
2424 Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
2426 return JIM_OK;
2430 static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2432 if (argc == 1)
2434 if (strcmp(args[0], "enable") == 0)
2436 jtag_verify_capture_ir = 1;
2438 else if (strcmp(args[0], "disable") == 0)
2440 jtag_verify_capture_ir = 0;
2441 } else
2443 return ERROR_COMMAND_SYNTAX_ERROR;
2445 } else if (argc != 0)
2447 return ERROR_COMMAND_SYNTAX_ERROR;
2450 command_print(cmd_ctx, "verify Capture-IR is %s", (jtag_verify_capture_ir) ? "enabled": "disabled");
2452 return ERROR_OK;
2455 void jtag_set_verify(bool enable)
2457 jtag_verify = enable;
2460 bool jtag_will_verify()
2462 return jtag_verify;
2465 static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2467 if (argc > 1)
2468 return ERROR_COMMAND_SYNTAX_ERROR;
2470 if (argc == 1)
2472 if (strcmp(args[0], "enable") == 0)
2473 jtag_set_verify(true);
2474 else if (strcmp(args[0], "disable") == 0)
2475 jtag_set_verify(false);
2476 else
2477 return ERROR_COMMAND_SYNTAX_ERROR;
2480 const char *status = jtag_will_verify() ? "enabled": "disabled";
2481 command_print(cmd_ctx, "verify jtag capture is %s", status);
2483 return ERROR_OK;
2487 int jtag_power_dropout(int *dropout)
2489 return jtag->power_dropout(dropout);
2492 int jtag_srst_asserted(int *srst_asserted)
2494 return jtag->srst_asserted(srst_asserted);
2497 void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
2499 jtag_tap_event_action_t * jteap;
2500 int done;
2502 jteap = tap->event_action;
2504 done = 0;
2505 while (jteap) {
2506 if (jteap->event == e) {
2507 done = 1;
2508 LOG_DEBUG( "JTAG tap: %s event: %d (%s) action: %s\n",
2509 tap->dotted_name,
2511 Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
2512 Jim_GetString(jteap->body, NULL) );
2513 if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
2514 Jim_PrintErrorMessage(interp);
2518 jteap = jteap->next;
2521 if (!done) {
2522 LOG_DEBUG( "event %d %s - no action",
2524 Jim_Nvp_value2name_simple( nvp_jtag_tap_event, e)->name);
2528 static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2530 if (argc > 1)
2531 return ERROR_COMMAND_SYNTAX_ERROR;
2533 if (argc == 1)
2535 bool use_new_table;
2536 if (strcmp(args[0], "short") == 0)
2537 use_new_table = true;
2538 else if (strcmp(args[0], "long") == 0)
2539 use_new_table = false;
2540 else
2541 return ERROR_COMMAND_SYNTAX_ERROR;
2543 tap_use_new_tms_table(use_new_table);
2546 command_print(cmd_ctx, "tms sequence is %s",
2547 tap_uses_new_tms_table() ? "short": "long");
2549 return ERROR_OK;
2553 * Moves from the current state to the goal \a state. This needs
2554 * to be handled according to the xsvf spec, see the XSTATE command
2555 * description.
2557 * From the XSVF spec, pertaining to XSTATE:
2559 * For special states known as stable states (Test-Logic-Reset,
2560 * Run-Test/Idle, Pause-DR, Pause- IR), an XSVF interpreter follows
2561 * predefined TAP state paths when the starting state is a stable state
2562 * and when the XSTATE specifies a new stable state. See the STATE
2563 * command in the [Ref 5] for the TAP state paths between stable
2564 * states.
2566 * For non-stable states, XSTATE should specify a state that is only one
2567 * TAP state transition distance from the current TAP state to avoid
2568 * undefined TAP state paths. A sequence of multiple XSTATE commands can
2569 * be issued to transition the TAP through a specific state path.
2571 * @note Unless @a tms_bits holds a path that agrees with [Ref 5] in *
2572 * above spec, then this code is not fully conformant to the xsvf spec.
2573 * This puts a burden on tap_get_tms_path() function from the xsvf spec.
2574 * If in doubt, you should confirm that that burden is being met.
2576 * Otherwise, state must be immediately reachable in one clock cycle,
2577 * and does not need to be a stable state.
2579 int jtag_add_statemove(tap_state_t goal_state)
2581 tap_state_t cur_state = cmd_queue_cur_state;
2583 LOG_DEBUG( "cur_state=%s goal_state=%s",
2584 tap_state_name(cur_state),
2585 tap_state_name(goal_state) );
2588 if (goal_state==cur_state )
2589 ; /* nothing to do */
2590 else if( goal_state==TAP_RESET )
2592 jtag_add_tlr();
2594 else if( tap_is_state_stable(cur_state) && tap_is_state_stable(goal_state) )
2596 unsigned tms_bits = tap_get_tms_path(cur_state, goal_state);
2597 unsigned tms_count = tap_get_tms_path_len(cur_state, goal_state);
2598 tap_state_t moves[8];
2599 assert(tms_count < DIM(moves));
2601 for (unsigned i = 0; i < tms_count; i++, tms_bits >>= 1)
2603 bool bit = tms_bits & 1;
2605 cur_state = tap_state_transition(cur_state, bit);
2606 moves[i] = cur_state;
2609 jtag_add_pathmove(tms_count, moves);
2611 else if( tap_state_transition(cur_state, true) == goal_state
2612 || tap_state_transition(cur_state, false) == goal_state )
2614 jtag_add_pathmove(1, &goal_state);
2617 else
2618 return ERROR_FAIL;
2620 return ERROR_OK;
2623 void jtag_set_nsrst_delay(unsigned delay)
2625 jtag_nsrst_delay = delay;
2627 unsigned jtag_get_nsrst_delay(void)
2629 return jtag_nsrst_delay;
2631 void jtag_set_ntrst_delay(unsigned delay)
2633 jtag_ntrst_delay = delay;
2635 unsigned jtag_get_ntrst_delay(void)
2637 return jtag_ntrst_delay;