1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2009 SoftPLC Corporation *
12 * Copyright (C) 2009 Zachary T Welch *
13 * zw@superlucidity.net *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
20 * This program is distributed in the hope that it will be useful, *
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23 * GNU General Public License for more details. *
25 * You should have received a copy of the GNU General Public License *
26 * along with this program; if not, write to the *
27 * Free Software Foundation, Inc., *
28 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
29 ***************************************************************************/
35 #include "minidriver.h"
36 #include "interface.h"
37 #include "interfaces.h"
43 static const Jim_Nvp nvp_jtag_tap_event
[] = {
44 { .value
= JTAG_TRST_ASSERTED
, .name
= "post-reset" },
45 { .value
= JTAG_TAP_EVENT_SETUP
, .name
= "setup" },
46 { .value
= JTAG_TAP_EVENT_ENABLE
, .name
= "tap-enable" },
47 { .value
= JTAG_TAP_EVENT_DISABLE
, .name
= "tap-disable" },
49 { .name
= NULL
, .value
= -1 }
52 extern struct jtag_interface
*jtag_interface
;
54 enum jtag_tap_cfg_param
{
58 static Jim_Nvp nvp_config_opts
[] = {
59 { .name
= "-event", .value
= JCFG_EVENT
},
61 { .name
= NULL
, .value
= -1 }
64 static int jtag_tap_configure_cmd(Jim_GetOptInfo
*goi
, struct jtag_tap
* tap
)
70 /* parse config or cget options */
71 while (goi
->argc
> 0) {
72 Jim_SetEmptyResult (goi
->interp
);
74 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
76 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
83 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
87 e
= Jim_GetOpt_Nvp(goi
, nvp_jtag_tap_event
, &n
);
89 Jim_GetOpt_NvpUnknown(goi
, nvp_jtag_tap_event
, 1);
93 if (goi
->isconfigure
) {
95 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
100 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
106 struct jtag_tap_event_action
*jteap
;
108 jteap
= tap
->event_action
;
109 /* replace existing? */
111 if (jteap
->event
== (enum jtag_event
)n
->value
) {
117 if (goi
->isconfigure
) {
121 jteap
= calloc(1, sizeof (*jteap
));
124 jteap
->event
= n
->value
;
125 Jim_GetOpt_Obj(goi
, &o
);
127 Jim_DecrRefCount(interp
, jteap
->body
);
129 jteap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
130 Jim_IncrRefCount(jteap
->body
);
134 /* add to head of event list */
135 jteap
->next
= tap
->event_action
;
136 tap
->event_action
= jteap
;
138 Jim_SetEmptyResult(goi
->interp
);
142 Jim_SetEmptyResult(goi
->interp
);
144 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, jteap
->body
));
151 } /* while (goi->argc) */
156 static int is_bad_irval(int ir_length
, jim_wide w
)
166 static int jim_newtap_cmd(Jim_GetOptInfo
*goi
)
168 struct jtag_tap
*pTap
;
174 const Jim_Nvp opts
[] = {
175 #define NTAP_OPT_IRLEN 0
176 { .name
= "-irlen" , .value
= NTAP_OPT_IRLEN
},
177 #define NTAP_OPT_IRMASK 1
178 { .name
= "-irmask" , .value
= NTAP_OPT_IRMASK
},
179 #define NTAP_OPT_IRCAPTURE 2
180 { .name
= "-ircapture" , .value
= NTAP_OPT_IRCAPTURE
},
181 #define NTAP_OPT_ENABLED 3
182 { .name
= "-enable" , .value
= NTAP_OPT_ENABLED
},
183 #define NTAP_OPT_DISABLED 4
184 { .name
= "-disable" , .value
= NTAP_OPT_DISABLED
},
185 #define NTAP_OPT_EXPECTED_ID 5
186 { .name
= "-expected-id" , .value
= NTAP_OPT_EXPECTED_ID
},
187 { .name
= NULL
, .value
= -1 },
190 pTap
= calloc(1, sizeof(struct jtag_tap
));
192 Jim_SetResult_sprintf(goi
->interp
, "no memory");
197 * we expect CHIP + TAP + OPTIONS
200 Jim_SetResult_sprintf(goi
->interp
, "Missing CHIP TAP OPTIONS ....");
204 Jim_GetOpt_String(goi
, &cp
, NULL
);
205 pTap
->chip
= strdup(cp
);
207 Jim_GetOpt_String(goi
, &cp
, NULL
);
208 pTap
->tapname
= strdup(cp
);
210 /* name + dot + name + null */
211 x
= strlen(pTap
->chip
) + 1 + strlen(pTap
->tapname
) + 1;
213 sprintf(cp
, "%s.%s", pTap
->chip
, pTap
->tapname
);
214 pTap
->dotted_name
= cp
;
216 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
217 pTap
->chip
, pTap
->tapname
, pTap
->dotted_name
, goi
->argc
);
219 /* IEEE specifies that the two LSBs of an IR scan are 01, so make
220 * that the default. The "-irlen" and "-irmask" options are only
221 * needed to cope with nonstandard TAPs, or to specify more bits.
223 pTap
->ir_capture_mask
= 0x03;
224 pTap
->ir_capture_value
= 0x01;
227 e
= Jim_GetOpt_Nvp(goi
, opts
, &n
);
229 Jim_GetOpt_NvpUnknown(goi
, opts
, 0);
230 free((void *)pTap
->dotted_name
);
234 LOG_DEBUG("Processing option: %s", n
->name
);
236 case NTAP_OPT_ENABLED
:
237 pTap
->disabled_after_reset
= false;
239 case NTAP_OPT_DISABLED
:
240 pTap
->disabled_after_reset
= true;
242 case NTAP_OPT_EXPECTED_ID
:
244 uint32_t *new_expected_ids
;
246 e
= Jim_GetOpt_Wide(goi
, &w
);
248 Jim_SetResult_sprintf(goi
->interp
, "option: %s bad parameter", n
->name
);
249 free((void *)pTap
->dotted_name
);
254 new_expected_ids
= malloc(sizeof(uint32_t) * (pTap
->expected_ids_cnt
+ 1));
255 if (new_expected_ids
== NULL
) {
256 Jim_SetResult_sprintf(goi
->interp
, "no memory");
257 free((void *)pTap
->dotted_name
);
262 memcpy(new_expected_ids
, pTap
->expected_ids
, sizeof(uint32_t) * pTap
->expected_ids_cnt
);
264 new_expected_ids
[pTap
->expected_ids_cnt
] = w
;
266 free(pTap
->expected_ids
);
267 pTap
->expected_ids
= new_expected_ids
;
268 pTap
->expected_ids_cnt
++;
272 case NTAP_OPT_IRMASK
:
273 case NTAP_OPT_IRCAPTURE
:
274 e
= Jim_GetOpt_Wide(goi
, &w
);
276 Jim_SetResult_sprintf(goi
->interp
, "option: %s bad parameter", n
->name
);
277 free((void *)pTap
->dotted_name
);
283 if (w
> (jim_wide
) (8 * sizeof(pTap
->ir_capture_value
)))
284 LOG_WARNING("%s: huge IR length %d",
289 case NTAP_OPT_IRMASK
:
290 if (is_bad_irval(pTap
->ir_length
, w
)) {
291 LOG_ERROR("%s: IR mask %x too big",
294 free((void *)pTap
->dotted_name
);
299 LOG_WARNING("%s: nonstandard IR mask",
301 pTap
->ir_capture_mask
= w
;
303 case NTAP_OPT_IRCAPTURE
:
304 if (is_bad_irval(pTap
->ir_length
, w
)) {
305 LOG_ERROR("%s: IR capture %x too big",
308 free((void *)pTap
->dotted_name
);
313 LOG_WARNING("%s: nonstandard IR value",
315 pTap
->ir_capture_value
= w
;
318 } /* switch (n->value) */
319 } /* while (goi->argc) */
321 /* default is enabled-after-reset */
322 pTap
->enabled
= !pTap
->disabled_after_reset
;
324 /* Did all the required option bits get cleared? */
325 if (pTap
->ir_length
!= 0)
331 Jim_SetResult_sprintf(goi
->interp
,
332 "newtap: %s missing IR length",
338 static void jtag_tap_handle_event(struct jtag_tap
*tap
, enum jtag_event e
)
340 struct jtag_tap_event_action
* jteap
;
342 for (jteap
= tap
->event_action
; jteap
!= NULL
; jteap
= jteap
->next
) {
343 if (jteap
->event
== e
) {
344 LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
347 Jim_Nvp_value2name_simple(nvp_jtag_tap_event
, e
)->name
,
348 Jim_GetString(jteap
->body
, NULL
));
349 if (Jim_EvalObj(interp
, jteap
->body
) != JIM_OK
) {
350 Jim_PrintErrorMessage(interp
);
352 case JTAG_TAP_EVENT_ENABLE
:
353 case JTAG_TAP_EVENT_DISABLE
:
354 /* NOTE: we currently assume the handlers
355 * can't fail. Right here is where we should
356 * really be verifying the scan chains ...
358 tap
->enabled
= (e
== JTAG_TAP_EVENT_ENABLE
);
359 LOG_INFO("JTAG tap: %s %s", tap
->dotted_name
,
360 tap
->enabled
? "enabled" : "disabled");
370 static int jim_jtag_command(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
376 struct command_context
*context
;
385 JTAG_CMD_TAPISENABLED
,
391 const Jim_Nvp jtag_cmds
[] = {
392 { .name
= "interface" , .value
= JTAG_CMD_INTERFACE
},
393 { .name
= "arp_init" , .value
= JTAG_CMD_INIT
},
394 { .name
= "arp_init-reset", .value
= JTAG_CMD_INIT_RESET
},
395 { .name
= "newtap" , .value
= JTAG_CMD_NEWTAP
},
396 { .name
= "tapisenabled" , .value
= JTAG_CMD_TAPISENABLED
},
397 { .name
= "tapenable" , .value
= JTAG_CMD_TAPENABLE
},
398 { .name
= "tapdisable" , .value
= JTAG_CMD_TAPDISABLE
},
399 { .name
= "configure" , .value
= JTAG_CMD_CONFIGURE
},
400 { .name
= "cget" , .value
= JTAG_CMD_CGET
},
401 { .name
= "names" , .value
= JTAG_CMD_NAMES
},
403 { .name
= NULL
, .value
= -1 },
406 context
= Jim_GetAssocData(interp
, "context");
407 /* go past the command */
408 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
410 e
= Jim_GetOpt_Nvp(&goi
, jtag_cmds
, &n
);
412 Jim_GetOpt_NvpUnknown(&goi
, jtag_cmds
, 0);
415 Jim_SetEmptyResult(goi
.interp
);
417 case JTAG_CMD_INTERFACE
:
418 /* return the name of the interface */
419 /* TCL code might need to know the exact type... */
420 /* FUTURE: we allow this as a means to "set" the interface. */
422 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
425 const char *name
= jtag_interface
? jtag_interface
->name
: NULL
;
426 Jim_SetResultString(goi
.interp
, name
? : "undefined", -1);
430 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
433 e
= jtag_init_inner(context
);
435 Jim_SetResult_sprintf(goi
.interp
, "error: %d", e
);
439 case JTAG_CMD_INIT_RESET
:
441 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
-1, "(no params)");
444 e
= jtag_init_reset(context
);
446 Jim_SetResult_sprintf(goi
.interp
, "error: %d", e
);
450 case JTAG_CMD_NEWTAP
:
451 return jim_newtap_cmd(&goi
);
453 case JTAG_CMD_TAPISENABLED
:
454 case JTAG_CMD_TAPENABLE
:
455 case JTAG_CMD_TAPDISABLE
:
457 Jim_SetResultString(goi
.interp
, "Too many parameters",-1);
464 t
= jtag_tap_by_jim_obj(goi
.interp
, goi
.argv
[0]);
469 case JTAG_CMD_TAPISENABLED
:
471 case JTAG_CMD_TAPENABLE
:
474 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_ENABLE
);
478 /* FIXME add JTAG sanity checks, w/o TLR
479 * - scan chain length grew by one (this)
480 * - IDs and IR lengths are as expected
483 jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE
);
485 case JTAG_CMD_TAPDISABLE
:
488 jtag_tap_handle_event(t
, JTAG_TAP_EVENT_DISABLE
);
492 /* FIXME add JTAG sanity checks, w/o TLR
493 * - scan chain length shrank by one (this)
494 * - IDs and IR lengths are as expected
497 jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE
);
501 Jim_SetResult(goi
.interp
, Jim_NewIntObj(goi
.interp
, e
));
508 Jim_WrongNumArgs(goi
.interp
, 0, NULL
,
509 "cget tap_name queryparm");
516 Jim_GetOpt_Obj(&goi
, &o
);
517 t
= jtag_tap_by_jim_obj(goi
.interp
, o
);
523 return jtag_tap_configure_cmd(&goi
, t
);
527 case JTAG_CMD_CONFIGURE
:
529 Jim_WrongNumArgs(goi
.interp
, 0, NULL
,
530 "configure tap_name attribute value ...");
537 Jim_GetOpt_Obj(&goi
, &o
);
538 t
= jtag_tap_by_jim_obj(goi
.interp
, o
);
544 return jtag_tap_configure_cmd(&goi
, t
);
550 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
553 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
555 struct jtag_tap
*tap
;
557 for (tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
) {
558 Jim_ListAppendElement(goi
.interp
,
559 Jim_GetResult(goi
.interp
),
560 Jim_NewStringObj(goi
.interp
,
561 tap
->dotted_name
, -1));
573 void jtag_notify_event(enum jtag_event event
)
575 struct jtag_tap
*tap
;
577 for (tap
= jtag_all_taps(); tap
; tap
= tap
->next_tap
)
578 jtag_tap_handle_event(tap
, event
);
582 static int default_khz(int khz
, int *jtag_speed
)
584 LOG_ERROR("Translation from khz to jtag_speed not implemented");
588 static int default_speed_div(int speed
, int *khz
)
590 LOG_ERROR("Translation from jtag_speed to khz not implemented");
594 static int default_power_dropout(int *dropout
)
596 *dropout
= 0; /* by default we can't detect power dropout */
600 static int default_srst_asserted(int *srst_asserted
)
602 *srst_asserted
= 0; /* by default we can't detect srst asserted */
606 COMMAND_HANDLER(handle_interface_list_command
)
608 if (strcmp(CMD_NAME
, "interface_list") == 0 && CMD_ARGC
> 0)
609 return ERROR_COMMAND_SYNTAX_ERROR
;
611 command_print(CMD_CTX
, "The following JTAG interfaces are available:");
612 for (unsigned i
= 0; NULL
!= jtag_interfaces
[i
]; i
++)
614 const char *name
= jtag_interfaces
[i
]->name
;
615 command_print(CMD_CTX
, "%u: %s", i
+ 1, name
);
621 COMMAND_HANDLER(handle_interface_command
)
623 /* check whether the interface is already configured */
626 LOG_WARNING("Interface already configured, ignoring");
630 /* interface name is a mandatory argument */
631 if (CMD_ARGC
!= 1 || CMD_ARGV
[0][0] == '\0')
632 return ERROR_COMMAND_SYNTAX_ERROR
;
634 for (unsigned i
= 0; NULL
!= jtag_interfaces
[i
]; i
++)
636 if (strcmp(CMD_ARGV
[0], jtag_interfaces
[i
]->name
) != 0)
639 if (NULL
!= jtag_interfaces
[i
]->register_commands
)
641 int retval
= jtag_interfaces
[i
]->register_commands(CMD_CTX
);
642 if (ERROR_OK
!= retval
)
646 jtag_interface
= jtag_interfaces
[i
];
648 if (jtag_interface
->khz
== NULL
)
649 jtag_interface
->khz
= default_khz
;
650 if (jtag_interface
->speed_div
== NULL
)
651 jtag_interface
->speed_div
= default_speed_div
;
652 if (jtag_interface
->power_dropout
== NULL
)
653 jtag_interface
->power_dropout
= default_power_dropout
;
654 if (jtag_interface
->srst_asserted
== NULL
)
655 jtag_interface
->srst_asserted
= default_srst_asserted
;
660 /* no valid interface was found (i.e. the configuration option,
661 * didn't match one of the compiled-in interfaces
663 LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV
[0]);
664 CALL_COMMAND_HANDLER(handle_interface_list_command
);
665 return ERROR_JTAG_INVALID_INTERFACE
;
668 COMMAND_HANDLER(handle_scan_chain_command
)
670 struct jtag_tap
*tap
;
672 tap
= jtag_all_taps();
673 command_print(CMD_CTX
, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
674 command_print(CMD_CTX
, "---|--------------------|---------|------------|------------|------|------|------|---------");
677 uint32_t expected
, expected_mask
, cur_instr
, ii
;
678 expected
= buf_get_u32(tap
->expected
, 0, tap
->ir_length
);
679 expected_mask
= buf_get_u32(tap
->expected_mask
, 0, tap
->ir_length
);
680 cur_instr
= buf_get_u32(tap
->cur_instr
, 0, tap
->ir_length
);
682 command_print(CMD_CTX
,
683 "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
684 tap
->abs_chain_position
,
686 tap
->enabled
? 'Y' : 'n',
687 (unsigned int)(tap
->idcode
),
688 (unsigned int)(tap
->expected_ids_cnt
> 0 ? tap
->expected_ids
[0] : 0),
689 (unsigned int)(tap
->ir_length
),
690 (unsigned int)(expected
),
691 (unsigned int)(expected_mask
),
692 (unsigned int)(cur_instr
));
694 for (ii
= 1; ii
< tap
->expected_ids_cnt
; ii
++) {
695 command_print(CMD_CTX
, " | | | | 0x%08x | | | | ",
696 (unsigned int)(tap
->expected_ids
[ii
]));
705 COMMAND_HANDLER(handle_reset_config_command
)
710 /* Original versions cared about the order of these tokens:
711 * reset_config signals [combination [trst_type [srst_type]]]
712 * They also clobbered the previous configuration even on error.
714 * Here we don't care about the order, and only change values
715 * which have been explicitly specified.
717 for (; CMD_ARGC
; CMD_ARGC
--, CMD_ARGV
++) {
722 m
= RESET_SRST_NO_GATING
;
723 if (strcmp(*CMD_ARGV
, "srst_gates_jtag") == 0)
724 /* default: don't use JTAG while SRST asserted */;
725 else if (strcmp(*CMD_ARGV
, "srst_nogate") == 0)
726 tmp
= RESET_SRST_NO_GATING
;
730 LOG_ERROR("extra reset_config %s spec (%s)",
731 "gating", *CMD_ARGV
);
732 return ERROR_INVALID_ARGUMENTS
;
738 m
= RESET_HAS_TRST
| RESET_HAS_SRST
;
739 if (strcmp(*CMD_ARGV
, "none") == 0)
741 else if (strcmp(*CMD_ARGV
, "trst_only") == 0)
742 tmp
= RESET_HAS_TRST
;
743 else if (strcmp(*CMD_ARGV
, "srst_only") == 0)
744 tmp
= RESET_HAS_SRST
;
745 else if (strcmp(*CMD_ARGV
, "trst_and_srst") == 0)
746 tmp
= RESET_HAS_TRST
| RESET_HAS_SRST
;
750 LOG_ERROR("extra reset_config %s spec (%s)",
751 "signal", *CMD_ARGV
);
752 return ERROR_INVALID_ARGUMENTS
;
757 /* combination (options for broken wiring) */
758 m
= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
759 if (strcmp(*CMD_ARGV
, "separate") == 0)
760 /* separate reset lines - default */;
761 else if (strcmp(*CMD_ARGV
, "srst_pulls_trst") == 0)
762 tmp
|= RESET_SRST_PULLS_TRST
;
763 else if (strcmp(*CMD_ARGV
, "trst_pulls_srst") == 0)
764 tmp
|= RESET_TRST_PULLS_SRST
;
765 else if (strcmp(*CMD_ARGV
, "combined") == 0)
766 tmp
|= RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
;
770 LOG_ERROR("extra reset_config %s spec (%s)",
771 "combination", *CMD_ARGV
);
772 return ERROR_INVALID_ARGUMENTS
;
777 /* trst_type (NOP without HAS_TRST) */
778 m
= RESET_TRST_OPEN_DRAIN
;
779 if (strcmp(*CMD_ARGV
, "trst_open_drain") == 0)
780 tmp
|= RESET_TRST_OPEN_DRAIN
;
781 else if (strcmp(*CMD_ARGV
, "trst_push_pull") == 0)
782 /* push/pull from adapter - default */;
786 LOG_ERROR("extra reset_config %s spec (%s)",
787 "trst_type", *CMD_ARGV
);
788 return ERROR_INVALID_ARGUMENTS
;
793 /* srst_type (NOP without HAS_SRST) */
794 m
|= RESET_SRST_PUSH_PULL
;
795 if (strcmp(*CMD_ARGV
, "srst_push_pull") == 0)
796 tmp
|= RESET_SRST_PUSH_PULL
;
797 else if (strcmp(*CMD_ARGV
, "srst_open_drain") == 0)
798 /* open drain from adapter - default */;
802 LOG_ERROR("extra reset_config %s spec (%s)",
803 "srst_type", *CMD_ARGV
);
804 return ERROR_INVALID_ARGUMENTS
;
809 /* caller provided nonsense; fail */
810 LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV
);
811 return ERROR_INVALID_ARGUMENTS
;
814 /* Remember the bits which were specified (mask)
815 * and their new values (new_cfg).
821 /* clear previous values of those bits, save new values */
823 int old_cfg
= jtag_get_reset_config();
827 jtag_set_reset_config(new_cfg
);
829 new_cfg
= jtag_get_reset_config();
833 * Display the (now-)current reset mode
837 /* minimal JTAG has neither SRST nor TRST (so that's the default) */
838 switch (new_cfg
& (RESET_HAS_TRST
| RESET_HAS_SRST
)) {
840 modes
[0] = "srst_only";
843 modes
[0] = "trst_only";
845 case RESET_TRST_AND_SRST
:
846 modes
[0] = "trst_and_srst";
853 /* normally SRST and TRST are decoupled; but bugs happen ... */
854 switch (new_cfg
& (RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
)) {
855 case RESET_SRST_PULLS_TRST
:
856 modes
[1] = "srst_pulls_trst";
858 case RESET_TRST_PULLS_SRST
:
859 modes
[1] = "trst_pulls_srst";
861 case RESET_SRST_PULLS_TRST
| RESET_TRST_PULLS_SRST
:
862 modes
[1] = "combined";
865 modes
[1] = "separate";
869 /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
870 if (new_cfg
& RESET_HAS_TRST
) {
871 if (new_cfg
& RESET_TRST_OPEN_DRAIN
)
872 modes
[3] = " trst_open_drain";
874 modes
[3] = " trst_push_pull";
878 /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
879 if (new_cfg
& RESET_HAS_SRST
) {
880 if (new_cfg
& RESET_SRST_NO_GATING
)
881 modes
[2] = " srst_nogate";
883 modes
[2] = " srst_gates_jtag";
885 if (new_cfg
& RESET_SRST_PUSH_PULL
)
886 modes
[4] = " srst_push_pull";
888 modes
[4] = " srst_open_drain";
894 command_print(CMD_CTX
, "%s %s%s%s%s",
896 modes
[2], modes
[3], modes
[4]);
901 COMMAND_HANDLER(handle_jtag_nsrst_delay_command
)
904 return ERROR_COMMAND_SYNTAX_ERROR
;
908 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
910 jtag_set_nsrst_delay(delay
);
912 command_print(CMD_CTX
, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
916 COMMAND_HANDLER(handle_jtag_ntrst_delay_command
)
919 return ERROR_COMMAND_SYNTAX_ERROR
;
923 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
925 jtag_set_ntrst_delay(delay
);
927 command_print(CMD_CTX
, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
931 COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command
)
934 return ERROR_COMMAND_SYNTAX_ERROR
;
938 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
940 jtag_set_nsrst_assert_width(delay
);
942 command_print(CMD_CTX
, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
946 COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command
)
949 return ERROR_COMMAND_SYNTAX_ERROR
;
953 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], delay
);
955 jtag_set_ntrst_assert_width(delay
);
957 command_print(CMD_CTX
, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
961 COMMAND_HANDLER(handle_jtag_khz_command
)
964 return ERROR_COMMAND_SYNTAX_ERROR
;
966 int retval
= ERROR_OK
;
970 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
972 retval
= jtag_config_khz(khz
);
973 if (ERROR_OK
!= retval
)
977 int cur_speed
= jtag_get_speed_khz();
978 retval
= jtag_get_speed_readable(&cur_speed
);
979 if (ERROR_OK
!= retval
)
983 command_print(CMD_CTX
, "%d kHz", cur_speed
);
985 command_print(CMD_CTX
, "RCLK - adaptive");
990 COMMAND_HANDLER(handle_jtag_rclk_command
)
993 return ERROR_COMMAND_SYNTAX_ERROR
;
995 int retval
= ERROR_OK
;
999 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], khz
);
1001 retval
= jtag_config_rclk(khz
);
1002 if (ERROR_OK
!= retval
)
1006 int cur_khz
= jtag_get_speed_khz();
1007 retval
= jtag_get_speed_readable(&cur_khz
);
1008 if (ERROR_OK
!= retval
)
1012 command_print(CMD_CTX
, "RCLK not supported - fallback to %d kHz", cur_khz
);
1014 command_print(CMD_CTX
, "RCLK - adaptive");
1019 COMMAND_HANDLER(handle_jtag_reset_command
)
1022 return ERROR_COMMAND_SYNTAX_ERROR
;
1025 if (CMD_ARGV
[0][0] == '1')
1027 else if (CMD_ARGV
[0][0] == '0')
1030 return ERROR_COMMAND_SYNTAX_ERROR
;
1033 if (CMD_ARGV
[1][0] == '1')
1035 else if (CMD_ARGV
[1][0] == '0')
1038 return ERROR_COMMAND_SYNTAX_ERROR
;
1040 if (jtag_interface_init(CMD_CTX
) != ERROR_OK
)
1041 return ERROR_JTAG_INIT_FAILED
;
1043 jtag_add_reset(trst
, srst
);
1044 return jtag_execute_queue();
1047 COMMAND_HANDLER(handle_runtest_command
)
1050 return ERROR_COMMAND_SYNTAX_ERROR
;
1052 unsigned num_clocks
;
1053 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num_clocks
);
1055 jtag_add_runtest(num_clocks
, TAP_IDLE
);
1056 return jtag_execute_queue();
1060 * For "irscan" or "drscan" commands, the "end" (really, "next") state
1061 * should be stable ... and *NOT* a shift state, otherwise free-running
1062 * jtag clocks could change the values latched by the update state.
1063 * Not surprisingly, this is the same constraint as SVF; the "irscan"
1064 * and "drscan" commands are a write-only subset of what SVF provides.
1066 static bool scan_is_safe(tap_state_t state
)
1081 COMMAND_HANDLER(handle_irscan_command
)
1084 struct scan_field
*fields
;
1085 struct jtag_tap
*tap
;
1086 tap_state_t endstate
;
1088 if ((CMD_ARGC
< 2) || (CMD_ARGC
% 2))
1090 return ERROR_COMMAND_SYNTAX_ERROR
;
1093 /* optional "-endstate" "statename" at the end of the arguments,
1094 * so that e.g. IRPAUSE can let us load the data register before
1095 * entering RUN/IDLE to execute the instruction we load here.
1097 endstate
= TAP_IDLE
;
1099 if (CMD_ARGC
>= 4) {
1100 /* have at least one pair of numbers. */
1101 /* is last pair the magic text? */
1102 if (strcmp("-endstate", CMD_ARGV
[CMD_ARGC
- 2]) == 0) {
1103 endstate
= tap_state_by_name(CMD_ARGV
[CMD_ARGC
- 1]);
1104 if (endstate
== TAP_INVALID
)
1105 return ERROR_COMMAND_SYNTAX_ERROR
;
1106 if (!scan_is_safe(endstate
))
1107 LOG_WARNING("unstable irscan endstate \"%s\"",
1108 CMD_ARGV
[CMD_ARGC
- 1]);
1113 int num_fields
= CMD_ARGC
/ 2;
1114 size_t fields_len
= sizeof(struct scan_field
) * num_fields
;
1115 fields
= malloc(fields_len
);
1116 memset(fields
, 0, fields_len
);
1119 for (i
= 0; i
< num_fields
; i
++)
1121 tap
= jtag_tap_by_string(CMD_ARGV
[i
*2]);
1125 for (j
= 0; j
< i
; j
++)
1126 free(fields
[j
].out_value
);
1128 command_print(CMD_CTX
, "Tap: %s unknown", CMD_ARGV
[i
*2]);
1132 int field_size
= tap
->ir_length
;
1133 fields
[i
].tap
= tap
;
1134 fields
[i
].num_bits
= field_size
;
1135 fields
[i
].out_value
= malloc(DIV_ROUND_UP(field_size
, 8));
1138 retval
= parse_u32(CMD_ARGV
[i
* 2 + 1], &value
);
1139 if (ERROR_OK
!= retval
)
1141 buf_set_u32(fields
[i
].out_value
, 0, field_size
, value
);
1142 fields
[i
].in_value
= NULL
;
1145 /* did we have an endstate? */
1146 jtag_add_ir_scan(num_fields
, fields
, endstate
);
1148 retval
= jtag_execute_queue();
1151 for (i
= 0; i
< num_fields
; i
++)
1153 if (NULL
!= fields
[i
].out_value
)
1154 free(fields
[i
].out_value
);
1162 static int Jim_Command_drscan(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
1165 struct scan_field
*fields
;
1167 int field_count
= 0;
1169 struct jtag_tap
*tap
;
1170 tap_state_t endstate
;
1173 * args[2] = num_bits
1174 * args[3] = hex string
1175 * ... repeat num bits and hex string ...
1178 * args[N-2] = "-endstate"
1179 * args[N-1] = statename
1181 if ((argc
< 4) || ((argc
% 2) != 0))
1183 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
1187 endstate
= TAP_IDLE
;
1189 script_debug(interp
, "drscan", argc
, args
);
1191 /* validate arguments as numbers */
1193 for (i
= 2; i
< argc
; i
+= 2)
1198 e
= Jim_GetLong(interp
, args
[i
], &bits
);
1199 /* If valid - try next arg */
1204 /* Not valid.. are we at the end? */
1205 if (((i
+ 2) != argc
)) {
1206 /* nope, then error */
1210 /* it could be: "-endstate FOO"
1211 * e.g. DRPAUSE so we can issue more instructions
1212 * before entering RUN/IDLE and executing them.
1215 /* get arg as a string. */
1216 cp
= Jim_GetString(args
[i
], NULL
);
1217 /* is it the magic? */
1218 if (0 == strcmp("-endstate", cp
)) {
1219 /* is the statename valid? */
1220 cp
= Jim_GetString(args
[i
+ 1], NULL
);
1222 /* see if it is a valid state name */
1223 endstate
= tap_state_by_name(cp
);
1225 /* update the error message */
1226 Jim_SetResult_sprintf(interp
,"endstate: %s invalid", cp
);
1228 if (!scan_is_safe(endstate
))
1229 LOG_WARNING("drscan with unsafe "
1230 "endstate \"%s\"", cp
);
1232 /* valid - so clear the error */
1234 /* and remove the last 2 args */
1239 /* Still an error? */
1241 return e
; /* too bad */
1243 } /* validate args */
1245 tap
= jtag_tap_by_jim_obj(interp
, args
[1]);
1250 num_fields
= (argc
-2)/2;
1251 fields
= malloc(sizeof(struct scan_field
) * num_fields
);
1252 for (i
= 2; i
< argc
; i
+= 2)
1258 Jim_GetLong(interp
, args
[i
], &bits
);
1259 str
= Jim_GetString(args
[i
+ 1], &len
);
1261 fields
[field_count
].tap
= tap
;
1262 fields
[field_count
].num_bits
= bits
;
1263 fields
[field_count
].out_value
= malloc(DIV_ROUND_UP(bits
, 8));
1264 str_to_buf(str
, len
, fields
[field_count
].out_value
, bits
, 0);
1265 fields
[field_count
].in_value
= fields
[field_count
].out_value
;
1269 jtag_add_dr_scan(num_fields
, fields
, endstate
);
1271 retval
= jtag_execute_queue();
1272 if (retval
!= ERROR_OK
)
1274 Jim_SetResultString(interp
, "drscan: jtag execute failed",-1);
1279 Jim_Obj
*list
= Jim_NewListObj(interp
, NULL
, 0);
1280 for (i
= 2; i
< argc
; i
+= 2)
1285 Jim_GetLong(interp
, args
[i
], &bits
);
1286 str
= buf_to_str(fields
[field_count
].in_value
, bits
, 16);
1287 free(fields
[field_count
].out_value
);
1289 Jim_ListAppendElement(interp
, list
, Jim_NewStringObj(interp
, str
, strlen(str
)));
1294 Jim_SetResult(interp
, list
);
1302 static int Jim_Command_pathmove(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
1304 tap_state_t states
[8];
1306 if ((argc
< 2) || ((size_t)argc
> (sizeof(states
)/sizeof(*states
) + 1)))
1308 Jim_WrongNumArgs(interp
, 1, args
, "wrong arguments");
1312 script_debug(interp
, "pathmove", argc
, args
);
1315 for (i
= 0; i
< argc
-1; i
++)
1318 cp
= Jim_GetString(args
[i
+ 1], NULL
);
1319 states
[i
] = tap_state_by_name(cp
);
1322 /* update the error message */
1323 Jim_SetResult_sprintf(interp
,"endstate: %s invalid", cp
);
1328 if ((jtag_add_statemove(states
[0]) != ERROR_OK
) || (jtag_execute_queue()!= ERROR_OK
))
1330 Jim_SetResultString(interp
, "pathmove: jtag execute failed",-1);
1334 jtag_add_pathmove(argc
-2, states
+ 1);
1336 if (jtag_execute_queue()!= ERROR_OK
)
1338 Jim_SetResultString(interp
, "pathmove: failed",-1);
1346 static int Jim_Command_flush_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *args
)
1348 script_debug(interp
, "flush_count", argc
, args
);
1350 Jim_SetResult(interp
, Jim_NewIntObj(interp
, jtag_get_flush_queue_count()));
1356 COMMAND_HANDLER(handle_verify_ircapture_command
)
1359 return ERROR_COMMAND_SYNTAX_ERROR
;
1364 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1365 jtag_set_verify_capture_ir(enable
);
1368 const char *status
= jtag_will_verify_capture_ir() ? "enabled": "disabled";
1369 command_print(CMD_CTX
, "verify Capture-IR is %s", status
);
1374 COMMAND_HANDLER(handle_verify_jtag_command
)
1377 return ERROR_COMMAND_SYNTAX_ERROR
;
1382 COMMAND_PARSE_ENABLE(CMD_ARGV
[0], enable
);
1383 jtag_set_verify(enable
);
1386 const char *status
= jtag_will_verify() ? "enabled": "disabled";
1387 command_print(CMD_CTX
, "verify jtag capture is %s", status
);
1392 COMMAND_HANDLER(handle_tms_sequence_command
)
1395 return ERROR_COMMAND_SYNTAX_ERROR
;
1400 if (strcmp(CMD_ARGV
[0], "short") == 0)
1401 use_new_table
= true;
1402 else if (strcmp(CMD_ARGV
[0], "long") == 0)
1403 use_new_table
= false;
1405 return ERROR_COMMAND_SYNTAX_ERROR
;
1407 tap_use_new_tms_table(use_new_table
);
1410 command_print(CMD_CTX
, "tms sequence is %s",
1411 tap_uses_new_tms_table() ? "short": "long");
1416 int jtag_register_commands(struct command_context
*cmd_ctx
)
1418 register_jim(cmd_ctx
, "jtag", jim_jtag_command
,
1419 "perform jtag tap actions");
1421 register_command(cmd_ctx
, NULL
, "interface",
1422 handle_interface_command
, COMMAND_CONFIG
,
1423 "try to configure interface");
1424 register_command(cmd_ctx
, NULL
, "interface_list",
1425 &handle_interface_list_command
, COMMAND_ANY
,
1426 "list all built-in interfaces");
1428 register_command(cmd_ctx
, NULL
, "jtag_khz",
1429 handle_jtag_khz_command
, COMMAND_ANY
,
1430 "set maximum jtag speed (if supported); "
1431 "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
1432 register_command(cmd_ctx
, NULL
, "jtag_rclk",
1433 handle_jtag_rclk_command
, COMMAND_ANY
,
1434 "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed");
1435 register_command(cmd_ctx
, NULL
, "reset_config",
1436 handle_reset_config_command
, COMMAND_ANY
,
1438 "[none|trst_only|srst_only|trst_and_srst] "
1439 "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
1440 "[srst_gates_jtag|srst_nogate] "
1441 "[trst_push_pull|trst_open_drain] "
1442 "[srst_push_pull|srst_open_drain]");
1444 register_command(cmd_ctx
, NULL
, "jtag_nsrst_delay",
1445 handle_jtag_nsrst_delay_command
, COMMAND_ANY
,
1446 "jtag_nsrst_delay <ms> "
1447 "- delay after deasserting srst in ms");
1448 register_command(cmd_ctx
, NULL
, "jtag_ntrst_delay",
1449 handle_jtag_ntrst_delay_command
, COMMAND_ANY
,
1450 "jtag_ntrst_delay <ms> "
1451 "- delay after deasserting trst in ms");
1453 register_command(cmd_ctx
, NULL
, "jtag_nsrst_assert_width",
1454 handle_jtag_nsrst_assert_width_command
, COMMAND_ANY
,
1455 "jtag_nsrst_assert_width <ms> "
1456 "- delay after asserting srst in ms");
1457 register_command(cmd_ctx
, NULL
, "jtag_ntrst_assert_width",
1458 handle_jtag_ntrst_assert_width_command
, COMMAND_ANY
,
1459 "jtag_ntrst_assert_width <ms> "
1460 "- delay after asserting trst in ms");
1462 register_command(cmd_ctx
, NULL
, "scan_chain",
1463 handle_scan_chain_command
, COMMAND_EXEC
,
1464 "print current scan chain configuration");
1466 register_command(cmd_ctx
, NULL
, "jtag_reset",
1467 handle_jtag_reset_command
, COMMAND_EXEC
,
1468 "toggle reset lines <trst> <srst>");
1469 register_command(cmd_ctx
, NULL
, "runtest",
1470 handle_runtest_command
, COMMAND_EXEC
,
1471 "move to Run-Test/Idle, and execute <num_cycles>");
1472 register_command(cmd_ctx
, NULL
, "irscan",
1473 handle_irscan_command
, COMMAND_EXEC
,
1474 "execute IR scan <device> <instr> [dev2] [instr2] ...");
1476 register_jim(cmd_ctx
, "drscan", Jim_Command_drscan
,
1477 "execute DR scan <device> "
1478 "<num_bits> <value> <num_bits1> <value2> ...");
1480 register_jim(cmd_ctx
, "flush_count", Jim_Command_flush_count
,
1481 "returns number of times the JTAG queue has been flushed");
1483 register_jim(cmd_ctx
, "pathmove", Jim_Command_pathmove
,
1484 "<state1>,<state2>,<state3>... "
1485 "- move JTAG to state1 then to state2, state3, etc.");
1487 register_command(cmd_ctx
, NULL
, "verify_ircapture",
1488 handle_verify_ircapture_command
, COMMAND_ANY
,
1489 "verify value captured during Capture-IR <enable | disable>");
1490 register_command(cmd_ctx
, NULL
, "verify_jtag",
1491 handle_verify_jtag_command
, COMMAND_ANY
,
1492 "verify value capture <enable | disable>");
1494 register_command(cmd_ctx
, NULL
, "tms_sequence",
1495 handle_tms_sequence_command
, COMMAND_ANY
,
1496 "choose short(default) or long tms_sequence <short | long>");