1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2013 by Andes Technology *
5 * Hsiangkai Wang <hkwang@andestech.com> *
6 ***************************************************************************/
12 /* project specific includes */
13 #include <jtag/interface.h>
15 #include <transport/transport.h>
16 #include <target/target.h>
17 #include <jtag/aice/aice_interface.h>
18 #include <jtag/aice/aice_transport.h>
22 static int jim_newtap_expected_id(struct jim_nvp *n, struct jim_getopt_info *goi,
26 int e = jim_getopt_wide(goi, &w);
28 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter",
33 unsigned expected_len = sizeof(uint32_t) * tap->expected_ids_cnt;
34 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
35 if (!new_expected_ids) {
36 Jim_SetResultFormatted(goi->interp, "no memory");
40 assert(tap->expected_ids);
41 memcpy(new_expected_ids, tap->expected_ids, expected_len);
43 new_expected_ids[tap->expected_ids_cnt] = w;
45 free(tap->expected_ids);
46 tap->expected_ids = new_expected_ids;
47 tap->expected_ids_cnt++;
52 #define NTAP_OPT_EXPECTED_ID 0
55 static int jim_aice_newtap_cmd(struct jim_getopt_info *goi)
62 const struct jim_nvp opts[] = {
63 {.name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID},
64 {.name = NULL, .value = -1},
67 tap = calloc(1, sizeof(struct jtag_tap));
69 Jim_SetResultFormatted(goi->interp, "no memory");
74 * we expect CHIP + TAP + OPTIONS
77 Jim_SetResultFormatted(goi->interp,
78 "Missing CHIP TAP OPTIONS ....");
84 jim_getopt_string(goi, &tmp, NULL);
85 tap->chip = strdup(tmp);
87 jim_getopt_string(goi, &tmp, NULL);
88 tap->tapname = strdup(tmp);
90 /* name + dot + name + null */
91 x = strlen(tap->chip) + 1 + strlen(tap->tapname) + 1;
93 sprintf(cp, "%s.%s", tap->chip, tap->tapname);
94 tap->dotted_name = cp;
96 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
97 tap->chip, tap->tapname, tap->dotted_name, goi->argc);
100 e = jim_getopt_nvp(goi, opts, &n);
102 jim_getopt_nvp_unknown(goi, opts, 0);
107 LOG_DEBUG("Processing option: %s", n->name);
109 case NTAP_OPT_EXPECTED_ID:
110 e = jim_newtap_expected_id(n, goi, tap);
117 } /* switch (n->value) */
118 } /* while (goi->argc) */
120 /* default is enabled-after-reset */
121 tap->enabled = !tap->disabled_after_reset;
128 static int jim_aice_newtap(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
130 struct jim_getopt_info goi;
131 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
132 return jim_aice_newtap_cmd(&goi);
136 COMMAND_HANDLER(handle_aice_init_command)
139 return ERROR_COMMAND_SYNTAX_ERROR;
141 static bool jtag_initialized;
142 if (jtag_initialized) {
143 LOG_INFO("'jtag init' has already been called");
146 jtag_initialized = true;
148 LOG_DEBUG("Initializing jtag devices...");
149 return jtag_init(CMD_CTX);
152 COMMAND_HANDLER(handle_scan_chain_command)
154 struct jtag_tap *tap;
155 char expected_id[12];
157 aice_scan_jtag_chain();
158 tap = jtag_all_taps();
160 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
162 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
165 uint32_t expected, expected_mask, ii;
167 snprintf(expected_id, sizeof(expected_id), "0x%08x",
168 (unsigned)((tap->expected_ids_cnt > 0)
169 ? tap->expected_ids[0]
171 if (tap->ignore_version)
172 expected_id[2] = '*';
174 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
175 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
178 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
179 tap->abs_chain_position,
181 tap->enabled ? 'Y' : 'n',
182 (unsigned int)(tap->idcode),
184 (unsigned int)(tap->ir_length),
185 (unsigned int)(expected),
186 (unsigned int)(expected_mask));
188 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
189 snprintf(expected_id, sizeof(expected_id), "0x%08x",
190 (unsigned) tap->expected_ids[ii]);
191 if (tap->ignore_version)
192 expected_id[2] = '*';
205 static int jim_aice_arp_init(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
207 LOG_DEBUG("No implement: jim_aice_arp_init");
213 static int aice_init_reset(struct command_context *cmd_ctx)
215 LOG_DEBUG("Initializing with hard TRST+SRST reset");
218 enum reset_types jtag_reset_config = jtag_get_reset_config();
220 jtag_add_reset(1, 0); /* TAP_RESET */
221 if (jtag_reset_config & RESET_HAS_SRST) {
222 jtag_add_reset(1, 1);
223 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
224 jtag_add_reset(0, 1);
226 jtag_add_reset(0, 0);
227 retval = jtag_execute_queue();
228 if (retval != ERROR_OK)
235 static int jim_aice_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
238 struct jim_getopt_info goi;
239 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
241 Jim_WrongNumArgs(goi.interp, 1, goi.argv - 1, "(no params)");
244 struct command_context *context = current_command_context(interp);
245 e = aice_init_reset(context);
248 Jim_Obj *obj = Jim_NewIntObj(goi.interp, e);
249 Jim_SetResultFormatted(goi.interp, "error: %#s", obj);
255 static int jim_aice_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
257 struct jim_getopt_info goi;
258 jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
260 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
263 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
264 struct jtag_tap *tap;
266 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
267 Jim_ListAppendElement(goi.interp,
268 Jim_GetResult(goi.interp),
269 Jim_NewStringObj(goi.interp,
270 tap->dotted_name, -1));
276 static const struct command_registration aice_transport_jtag_subcommand_handlers[] = {
280 .handler = handle_aice_init_command,
281 .help = "initialize jtag scan chain",
287 .jim_handler = jim_aice_arp_init,
288 .help = "Validates JTAG scan chain against the list of "
292 .name = "arp_init-reset",
294 .jim_handler = jim_aice_arp_init_reset,
295 .help = "Uses TRST and SRST to try resetting everything on "
296 "the JTAG scan chain, then performs 'jtag arp_init'."
300 .mode = COMMAND_CONFIG,
301 .jim_handler = jim_aice_newtap,
302 .help = "Create a new TAP instance named basename.tap_type, "
303 "and appends it to the scan chain.",
304 .usage = "basename tap_type ['-expected_id' number]"
307 .name = "tapisenabled",
308 .mode = COMMAND_EXEC,
309 .jim_handler = jim_jtag_tap_enabler,
310 .help = "Returns a Tcl boolean (0/1) indicating whether "
311 "the TAP is enabled (1) or not (0).",
316 .mode = COMMAND_EXEC,
317 .jim_handler = jim_jtag_tap_enabler,
318 .help = "Try to enable the specified TAP using the "
319 "'tap-enable' TAP event.",
323 .name = "tapdisable",
324 .mode = COMMAND_EXEC,
325 .jim_handler = jim_jtag_tap_enabler,
326 .help = "Try to disable the specified TAP using the "
327 "'tap-disable' TAP event.",
333 .jim_handler = jim_jtag_configure,
334 .help = "Provide a Tcl handler for the specified "
336 .usage = "tap_name '-event' event_name handler",
340 .mode = COMMAND_EXEC,
341 .jim_handler = jim_jtag_configure,
342 .help = "Return any Tcl handler for the specified "
344 .usage = "tap_name '-event' event_name",
349 .jim_handler = jim_aice_names,
350 .help = "Returns list of all JTAG tap names.",
353 .name = "scan_chain",
354 .handler = handle_scan_chain_command,
356 .help = "print current scan chain configuration",
360 COMMAND_REGISTRATION_DONE
364 static const struct command_registration aice_transport_command_handlers[] = {
369 .chain = aice_transport_jtag_subcommand_handlers,
371 COMMAND_REGISTRATION_DONE
376 static int aice_transport_register_commands(struct command_context *cmd_ctx)
378 return register_commands(cmd_ctx, NULL, aice_transport_command_handlers);
382 static int aice_transport_init(struct command_context *cmd_ctx)
384 LOG_DEBUG("aice_transport_init");
385 struct target *t = get_current_target(cmd_ctx);
386 struct transport *transport;
389 LOG_ERROR("no current target");
393 transport = get_current_transport();
396 LOG_ERROR("no transport selected");
400 LOG_DEBUG("current transport %s", transport->name);
402 return aice_init_targets();
406 static int aice_transport_select(struct command_context *ctx)
408 LOG_DEBUG("aice_transport_select");
412 retval = aice_transport_register_commands(ctx);
414 if (retval != ERROR_OK)
420 static struct transport aice_jtag_transport = {
422 .select = aice_transport_select,
423 .init = aice_transport_init,
426 const char *aice_transports[] = { "aice_jtag", NULL };
428 static void aice_constructor(void) __attribute__((constructor));
429 static void aice_constructor(void)
431 transport_register(&aice_jtag_transport);