1 /***************************************************************************
2 * Copyright (C) 2013 by Andes Technology *
3 * Hsiangkai Wang <hkwang@andestech.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 ***************************************************************************/
23 /* project specific includes */
24 #include <jtag/interface.h>
26 #include <transport/transport.h>
27 #include <target/target.h>
28 #include <jtag/aice/aice_interface.h>
29 #include <jtag/aice/aice_transport.h>
33 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
34 struct jtag_tap *pTap)
37 int e = Jim_GetOpt_Wide(goi, &w);
39 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter",
44 unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
45 uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
46 if (new_expected_ids == NULL) {
47 Jim_SetResultFormatted(goi->interp, "no memory");
51 assert(pTap->expected_ids);
52 memcpy(new_expected_ids, pTap->expected_ids, expected_len);
54 new_expected_ids[pTap->expected_ids_cnt] = w;
56 free(pTap->expected_ids);
57 pTap->expected_ids = new_expected_ids;
58 pTap->expected_ids_cnt++;
63 #define NTAP_OPT_EXPECTED_ID 0
66 static int jim_aice_newtap_cmd(Jim_GetOptInfo *goi)
68 struct jtag_tap *pTap;
73 const Jim_Nvp opts[] = {
74 {.name = "-expected-id", .value = NTAP_OPT_EXPECTED_ID},
75 {.name = NULL, .value = -1},
78 pTap = calloc(1, sizeof(struct jtag_tap));
80 Jim_SetResultFormatted(goi->interp, "no memory");
85 * we expect CHIP + TAP + OPTIONS
88 Jim_SetResultFormatted(goi->interp,
89 "Missing CHIP TAP OPTIONS ....");
95 Jim_GetOpt_String(goi, &tmp, NULL);
96 pTap->chip = strdup(tmp);
98 Jim_GetOpt_String(goi, &tmp, NULL);
99 pTap->tapname = strdup(tmp);
101 /* name + dot + name + null */
102 x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
104 sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
105 pTap->dotted_name = cp;
107 LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
108 pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
111 e = Jim_GetOpt_Nvp(goi, opts, &n);
113 Jim_GetOpt_NvpUnknown(goi, opts, 0);
118 LOG_DEBUG("Processing option: %s", n->name);
120 case NTAP_OPT_EXPECTED_ID:
121 e = jim_newtap_expected_id(n, goi, pTap);
128 } /* switch (n->value) */
129 } /* while (goi->argc) */
131 /* default is enabled-after-reset */
132 pTap->enabled = !pTap->disabled_after_reset;
139 static int jim_aice_newtap(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
142 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
143 return jim_aice_newtap_cmd(&goi);
147 COMMAND_HANDLER(handle_aice_init_command)
150 return ERROR_COMMAND_SYNTAX_ERROR;
152 static bool jtag_initialized;
153 if (jtag_initialized) {
154 LOG_INFO("'jtag init' has already been called");
157 jtag_initialized = true;
159 LOG_DEBUG("Initializing jtag devices...");
160 return jtag_init(CMD_CTX);
163 COMMAND_HANDLER(handle_scan_chain_command)
165 struct jtag_tap *tap;
166 char expected_id[12];
168 aice_scan_jtag_chain();
169 tap = jtag_all_taps();
171 " TapName Enabled IdCode Expected IrLen IrCap IrMask");
173 "-- ------------------- -------- ---------- ---------- ----- ----- ------");
176 uint32_t expected, expected_mask, ii;
178 snprintf(expected_id, sizeof(expected_id), "0x%08x",
179 (unsigned)((tap->expected_ids_cnt > 0)
180 ? tap->expected_ids[0]
182 if (tap->ignore_version)
183 expected_id[2] = '*';
185 expected = buf_get_u32(tap->expected, 0, tap->ir_length);
186 expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
189 "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
190 tap->abs_chain_position,
192 tap->enabled ? 'Y' : 'n',
193 (unsigned int)(tap->idcode),
195 (unsigned int)(tap->ir_length),
196 (unsigned int)(expected),
197 (unsigned int)(expected_mask));
199 for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
200 snprintf(expected_id, sizeof(expected_id), "0x%08x",
201 (unsigned) tap->expected_ids[ii]);
202 if (tap->ignore_version)
203 expected_id[2] = '*';
216 static int jim_aice_arp_init(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
218 LOG_DEBUG("No implement: jim_aice_arp_init");
224 static int aice_init_reset(struct command_context *cmd_ctx)
226 LOG_DEBUG("Initializing with hard TRST+SRST reset");
229 enum reset_types jtag_reset_config = jtag_get_reset_config();
231 jtag_add_reset(1, 0); /* TAP_RESET */
232 if (jtag_reset_config & RESET_HAS_SRST) {
233 jtag_add_reset(1, 1);
234 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
235 jtag_add_reset(0, 1);
237 jtag_add_reset(0, 0);
238 retval = jtag_execute_queue();
239 if (retval != ERROR_OK)
246 static int jim_aice_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
250 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
252 Jim_WrongNumArgs(goi.interp, 1, goi.argv - 1, "(no params)");
255 struct command_context *context = current_command_context(interp);
256 e = aice_init_reset(context);
259 Jim_Obj *obj = Jim_NewIntObj(goi.interp, e);
260 Jim_SetResultFormatted(goi.interp, "error: %#s", obj);
266 static int jim_aice_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
269 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
271 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
274 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
275 struct jtag_tap *tap;
277 for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
278 Jim_ListAppendElement(goi.interp,
279 Jim_GetResult(goi.interp),
280 Jim_NewStringObj(goi.interp,
281 tap->dotted_name, -1));
287 static const struct command_registration
288 aice_transport_jtag_subcommand_handlers[] = {
292 .handler = handle_aice_init_command,
293 .help = "initialize jtag scan chain",
299 .jim_handler = jim_aice_arp_init,
300 .help = "Validates JTAG scan chain against the list of "
304 .name = "arp_init-reset",
306 .jim_handler = jim_aice_arp_init_reset,
307 .help = "Uses TRST and SRST to try resetting everything on "
308 "the JTAG scan chain, then performs 'jtag arp_init'."
312 .mode = COMMAND_CONFIG,
313 .jim_handler = jim_aice_newtap,
314 .help = "Create a new TAP instance named basename.tap_type, "
315 "and appends it to the scan chain.",
316 .usage = "basename tap_type ['-expected_id' number]"
319 .name = "tapisenabled",
320 .mode = COMMAND_EXEC,
321 .jim_handler = jim_jtag_tap_enabler,
322 .help = "Returns a Tcl boolean (0/1) indicating whether "
323 "the TAP is enabled (1) or not (0).",
328 .mode = COMMAND_EXEC,
329 .jim_handler = jim_jtag_tap_enabler,
330 .help = "Try to enable the specified TAP using the "
331 "'tap-enable' TAP event.",
335 .name = "tapdisable",
336 .mode = COMMAND_EXEC,
337 .jim_handler = jim_jtag_tap_enabler,
338 .help = "Try to disable the specified TAP using the "
339 "'tap-disable' TAP event.",
345 .jim_handler = jim_jtag_configure,
346 .help = "Provide a Tcl handler for the specified "
348 .usage = "tap_name '-event' event_name handler",
352 .mode = COMMAND_EXEC,
353 .jim_handler = jim_jtag_configure,
354 .help = "Return any Tcl handler for the specified "
356 .usage = "tap_name '-event' event_name",
361 .jim_handler = jim_aice_names,
362 .help = "Returns list of all JTAG tap names.",
365 .name = "scan_chain",
366 .handler = handle_scan_chain_command,
368 .help = "print current scan chain configuration",
372 COMMAND_REGISTRATION_DONE
376 static const struct command_registration aice_transport_command_handlers[] = {
381 .chain = aice_transport_jtag_subcommand_handlers,
383 COMMAND_REGISTRATION_DONE
388 static int aice_transport_register_commands(struct command_context *cmd_ctx)
390 return register_commands(cmd_ctx, NULL,
391 aice_transport_command_handlers);
395 static int aice_transport_init(struct command_context *cmd_ctx)
397 LOG_DEBUG("aice_transport_init");
398 struct target *t = get_current_target(cmd_ctx);
399 struct transport *transport;
402 LOG_ERROR("no current target");
406 transport = get_current_transport();
409 LOG_ERROR("no transport selected");
413 LOG_DEBUG("current transport %s", transport->name);
415 return aice_init_targets();
419 static int aice_transport_select(struct command_context *ctx)
421 LOG_DEBUG("aice_transport_select");
425 retval = aice_transport_register_commands(ctx);
427 if (retval != ERROR_OK)
433 static struct transport aice_jtag_transport = {
435 .select = aice_transport_select,
436 .init = aice_transport_init,
439 const char *aice_transports[] = { "aice_jtag", NULL };
441 static void aice_constructor(void) __attribute__((constructor));
442 static void aice_constructor(void)
444 transport_register(&aice_jtag_transport);