You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1705 lines
41 KiB

  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. * Copyright (C) 2009 Zachary T Welch *
  13. * zw@superlucidity.net *
  14. * *
  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. *
  19. * *
  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. *
  24. * *
  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. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include "jtag.h"
  34. #include "minidriver.h"
  35. #include "interface.h"
  36. #include "interfaces.h"
  37. #ifdef HAVE_STRINGS_H
  38. #include <strings.h>
  39. #endif
  40. static const Jim_Nvp nvp_jtag_tap_event[] = {
  41. { .value = JTAG_TRST_ASSERTED, .name = "post-reset" },
  42. { .value = JTAG_TAP_EVENT_SETUP, .name = "setup" },
  43. { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
  44. { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
  45. { .name = NULL, .value = -1 }
  46. };
  47. extern struct jtag_interface *jtag_interface;
  48. struct jtag_tap *jtag_tap_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
  49. {
  50. const char *cp = Jim_GetString(o, NULL);
  51. struct jtag_tap *t = cp ? jtag_tap_by_string(cp) : NULL;
  52. if (NULL == cp)
  53. cp = "(unknown)";
  54. if (NULL == t)
  55. Jim_SetResult_sprintf(interp, "Tap '%s' could not be found", cp);
  56. return t;
  57. }
  58. static bool scan_is_safe(tap_state_t state)
  59. {
  60. switch (state)
  61. {
  62. case TAP_RESET:
  63. case TAP_IDLE:
  64. case TAP_DRPAUSE:
  65. case TAP_IRPAUSE:
  66. return true;
  67. default:
  68. return false;
  69. }
  70. }
  71. static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  72. {
  73. int retval;
  74. struct scan_field *fields;
  75. int num_fields;
  76. int field_count = 0;
  77. int i, e;
  78. struct jtag_tap *tap;
  79. tap_state_t endstate;
  80. /* args[1] = device
  81. * args[2] = num_bits
  82. * args[3] = hex string
  83. * ... repeat num bits and hex string ...
  84. *
  85. * .. optionally:
  86. * args[N-2] = "-endstate"
  87. * args[N-1] = statename
  88. */
  89. if ((argc < 4) || ((argc % 2) != 0))
  90. {
  91. Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
  92. return JIM_ERR;
  93. }
  94. endstate = TAP_IDLE;
  95. script_debug(interp, "drscan", argc, args);
  96. /* validate arguments as numbers */
  97. e = JIM_OK;
  98. for (i = 2; i < argc; i += 2)
  99. {
  100. long bits;
  101. const char *cp;
  102. e = Jim_GetLong(interp, args[i], &bits);
  103. /* If valid - try next arg */
  104. if (e == JIM_OK) {
  105. continue;
  106. }
  107. /* Not valid.. are we at the end? */
  108. if (((i + 2) != argc)) {
  109. /* nope, then error */
  110. return e;
  111. }
  112. /* it could be: "-endstate FOO"
  113. * e.g. DRPAUSE so we can issue more instructions
  114. * before entering RUN/IDLE and executing them.
  115. */
  116. /* get arg as a string. */
  117. cp = Jim_GetString(args[i], NULL);
  118. /* is it the magic? */
  119. if (0 == strcmp("-endstate", cp)) {
  120. /* is the statename valid? */
  121. cp = Jim_GetString(args[i + 1], NULL);
  122. /* see if it is a valid state name */
  123. endstate = tap_state_by_name(cp);
  124. if (endstate < 0) {
  125. /* update the error message */
  126. Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
  127. } else {
  128. if (!scan_is_safe(endstate))
  129. LOG_WARNING("drscan with unsafe "
  130. "endstate \"%s\"", cp);
  131. /* valid - so clear the error */
  132. e = JIM_OK;
  133. /* and remove the last 2 args */
  134. argc -= 2;
  135. }
  136. }
  137. /* Still an error? */
  138. if (e != JIM_OK) {
  139. return e; /* too bad */
  140. }
  141. } /* validate args */
  142. tap = jtag_tap_by_jim_obj(interp, args[1]);
  143. if (tap == NULL) {
  144. return JIM_ERR;
  145. }
  146. num_fields = (argc-2)/2;
  147. fields = malloc(sizeof(struct scan_field) * num_fields);
  148. for (i = 2; i < argc; i += 2)
  149. {
  150. long bits;
  151. int len;
  152. const char *str;
  153. Jim_GetLong(interp, args[i], &bits);
  154. str = Jim_GetString(args[i + 1], &len);
  155. fields[field_count].tap = tap;
  156. fields[field_count].num_bits = bits;
  157. fields[field_count].out_value = malloc(DIV_ROUND_UP(bits, 8));
  158. str_to_buf(str, len, fields[field_count].out_value, bits, 0);
  159. fields[field_count].in_value = fields[field_count].out_value;
  160. field_count++;
  161. }
  162. jtag_add_dr_scan(num_fields, fields, endstate);
  163. retval = jtag_execute_queue();
  164. if (retval != ERROR_OK)
  165. {
  166. Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
  167. return JIM_ERR;
  168. }
  169. field_count = 0;
  170. Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
  171. for (i = 2; i < argc; i += 2)
  172. {
  173. long bits;
  174. char *str;
  175. Jim_GetLong(interp, args[i], &bits);
  176. str = buf_to_str(fields[field_count].in_value, bits, 16);
  177. free(fields[field_count].out_value);
  178. Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
  179. free(str);
  180. field_count++;
  181. }
  182. Jim_SetResult(interp, list);
  183. free(fields);
  184. return JIM_OK;
  185. }
  186. static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  187. {
  188. tap_state_t states[8];
  189. if ((argc < 2) || ((size_t)argc > (ARRAY_SIZE(states) + 1)))
  190. {
  191. Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
  192. return JIM_ERR;
  193. }
  194. script_debug(interp, "pathmove", argc, args);
  195. int i;
  196. for (i = 0; i < argc-1; i++)
  197. {
  198. const char *cp;
  199. cp = Jim_GetString(args[i + 1], NULL);
  200. states[i] = tap_state_by_name(cp);
  201. if (states[i] < 0)
  202. {
  203. /* update the error message */
  204. Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
  205. return JIM_ERR;
  206. }
  207. }
  208. if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
  209. {
  210. Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
  211. return JIM_ERR;
  212. }
  213. jtag_add_pathmove(argc-2, states + 1);
  214. if (jtag_execute_queue()!= ERROR_OK)
  215. {
  216. Jim_SetResultString(interp, "pathmove: failed",-1);
  217. return JIM_ERR;
  218. }
  219. return JIM_OK;
  220. }
  221. static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  222. {
  223. script_debug(interp, "flush_count", argc, args);
  224. Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
  225. return JIM_OK;
  226. }
  227. static const struct command_registration jtag_command_handlers_to_move[] = {
  228. {
  229. .name = "drscan",
  230. .mode = COMMAND_EXEC,
  231. .jim_handler = &Jim_Command_drscan,
  232. .help = "execute DR scan <device> "
  233. "<num_bits> <value> <num_bits1> <value2> ...",
  234. },
  235. {
  236. .name = "flush_count",
  237. .mode = COMMAND_EXEC,
  238. .jim_handler = &Jim_Command_flush_count,
  239. .help = "returns number of times the JTAG queue has been flushed",
  240. },
  241. {
  242. .name = "pathmove",
  243. .mode = COMMAND_EXEC,
  244. .jim_handler = &Jim_Command_pathmove,
  245. .usage = "<state1>,<state2>,<state3>... ",
  246. .help = "move JTAG to state1 then to state2, state3, etc.",
  247. },
  248. COMMAND_REGISTRATION_DONE
  249. };
  250. enum jtag_tap_cfg_param {
  251. JCFG_EVENT
  252. };
  253. static Jim_Nvp nvp_config_opts[] = {
  254. { .name = "-event", .value = JCFG_EVENT },
  255. { .name = NULL, .value = -1 }
  256. };
  257. static int jtag_tap_configure_event(Jim_GetOptInfo *goi, struct jtag_tap * tap)
  258. {
  259. if (goi->argc == 0)
  260. {
  261. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> ...");
  262. return JIM_ERR;
  263. }
  264. Jim_Nvp *n;
  265. int e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
  266. if (e != JIM_OK)
  267. {
  268. Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
  269. return e;
  270. }
  271. if (goi->isconfigure) {
  272. if (goi->argc != 1) {
  273. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name> <event-body>");
  274. return JIM_ERR;
  275. }
  276. } else {
  277. if (goi->argc != 0) {
  278. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event <event-name>");
  279. return JIM_ERR;
  280. }
  281. }
  282. struct jtag_tap_event_action *jteap = tap->event_action;
  283. /* replace existing event body */
  284. bool found = false;
  285. while (jteap)
  286. {
  287. if (jteap->event == (enum jtag_event)n->value)
  288. {
  289. found = true;
  290. break;
  291. }
  292. jteap = jteap->next;
  293. }
  294. Jim_SetEmptyResult(goi->interp);
  295. if (goi->isconfigure)
  296. {
  297. if (!found)
  298. jteap = calloc(1, sizeof(*jteap));
  299. else if (NULL != jteap->body)
  300. Jim_DecrRefCount(goi->interp, jteap->body);
  301. jteap->interp = goi->interp;
  302. jteap->event = n->value;
  303. Jim_Obj *o;
  304. Jim_GetOpt_Obj(goi, &o);
  305. jteap->body = Jim_DuplicateObj(goi->interp, o);
  306. Jim_IncrRefCount(jteap->body);
  307. if (!found)
  308. {
  309. /* add to head of event list */
  310. jteap->next = tap->event_action;
  311. tap->event_action = jteap;
  312. }
  313. }
  314. else if (found)
  315. {
  316. jteap->interp = goi->interp;
  317. Jim_SetResult(goi->interp,
  318. Jim_DuplicateObj(goi->interp, jteap->body));
  319. }
  320. return JIM_OK;
  321. }
  322. static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, struct jtag_tap * tap)
  323. {
  324. /* parse config or cget options */
  325. while (goi->argc > 0)
  326. {
  327. Jim_SetEmptyResult (goi->interp);
  328. Jim_Nvp *n;
  329. int e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
  330. if (e != JIM_OK)
  331. {
  332. Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
  333. return e;
  334. }
  335. switch (n->value)
  336. {
  337. case JCFG_EVENT:
  338. e = jtag_tap_configure_event(goi, tap);
  339. if (e != JIM_OK)
  340. return e;
  341. break;
  342. default:
  343. Jim_SetResult_sprintf(goi->interp, "unknown event: %s", n->name);
  344. return JIM_ERR;
  345. }
  346. }
  347. return JIM_OK;
  348. }
  349. static int is_bad_irval(int ir_length, jim_wide w)
  350. {
  351. jim_wide v = 1;
  352. v <<= ir_length;
  353. v -= 1;
  354. v = ~v;
  355. return (w & v) != 0;
  356. }
  357. static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
  358. struct jtag_tap *pTap)
  359. {
  360. jim_wide w;
  361. int e = Jim_GetOpt_Wide(goi, &w);
  362. if (e != JIM_OK) {
  363. Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
  364. return e;
  365. }
  366. unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
  367. uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
  368. if (new_expected_ids == NULL)
  369. {
  370. Jim_SetResult_sprintf(goi->interp, "no memory");
  371. return JIM_ERR;
  372. }
  373. memcpy(new_expected_ids, pTap->expected_ids, expected_len);
  374. new_expected_ids[pTap->expected_ids_cnt] = w;
  375. free(pTap->expected_ids);
  376. pTap->expected_ids = new_expected_ids;
  377. pTap->expected_ids_cnt++;
  378. return JIM_OK;
  379. }
  380. #define NTAP_OPT_IRLEN 0
  381. #define NTAP_OPT_IRMASK 1
  382. #define NTAP_OPT_IRCAPTURE 2
  383. #define NTAP_OPT_ENABLED 3
  384. #define NTAP_OPT_DISABLED 4
  385. #define NTAP_OPT_EXPECTED_ID 5
  386. #define NTAP_OPT_VERSION 6
  387. static int jim_newtap_ir_param(Jim_Nvp *n, Jim_GetOptInfo *goi,
  388. struct jtag_tap *pTap)
  389. {
  390. jim_wide w;
  391. int e = Jim_GetOpt_Wide(goi, &w);
  392. if (e != JIM_OK)
  393. {
  394. Jim_SetResult_sprintf(goi->interp,
  395. "option: %s bad parameter", n->name);
  396. free((void *)pTap->dotted_name);
  397. return e;
  398. }
  399. switch (n->value) {
  400. case NTAP_OPT_IRLEN:
  401. if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
  402. {
  403. LOG_WARNING("%s: huge IR length %d",
  404. pTap->dotted_name, (int) w);
  405. }
  406. pTap->ir_length = w;
  407. break;
  408. case NTAP_OPT_IRMASK:
  409. if (is_bad_irval(pTap->ir_length, w))
  410. {
  411. LOG_ERROR("%s: IR mask %x too big",
  412. pTap->dotted_name,
  413. (int) w);
  414. return JIM_ERR;
  415. }
  416. if ((w & 3) != 3)
  417. LOG_WARNING("%s: nonstandard IR mask", pTap->dotted_name);
  418. pTap->ir_capture_mask = w;
  419. break;
  420. case NTAP_OPT_IRCAPTURE:
  421. if (is_bad_irval(pTap->ir_length, w))
  422. {
  423. LOG_ERROR("%s: IR capture %x too big",
  424. pTap->dotted_name, (int) w);
  425. return JIM_ERR;
  426. }
  427. if ((w & 3) != 1)
  428. LOG_WARNING("%s: nonstandard IR value",
  429. pTap->dotted_name);
  430. pTap->ir_capture_value = w;
  431. break;
  432. default:
  433. return JIM_ERR;
  434. }
  435. return JIM_OK;
  436. }
  437. static int jim_newtap_cmd(Jim_GetOptInfo *goi)
  438. {
  439. struct jtag_tap *pTap;
  440. int x;
  441. int e;
  442. Jim_Nvp *n;
  443. char *cp;
  444. const Jim_Nvp opts[] = {
  445. { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
  446. { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
  447. { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
  448. { .name = "-enable" , .value = NTAP_OPT_ENABLED },
  449. { .name = "-disable" , .value = NTAP_OPT_DISABLED },
  450. { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
  451. { .name = "-ignore-version" , .value = NTAP_OPT_VERSION },
  452. { .name = NULL , .value = -1 },
  453. };
  454. pTap = calloc(1, sizeof(struct jtag_tap));
  455. if (!pTap) {
  456. Jim_SetResult_sprintf(goi->interp, "no memory");
  457. return JIM_ERR;
  458. }
  459. /*
  460. * we expect CHIP + TAP + OPTIONS
  461. * */
  462. if (goi->argc < 3) {
  463. Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
  464. free(pTap);
  465. return JIM_ERR;
  466. }
  467. Jim_GetOpt_String(goi, &cp, NULL);
  468. pTap->chip = strdup(cp);
  469. Jim_GetOpt_String(goi, &cp, NULL);
  470. pTap->tapname = strdup(cp);
  471. /* name + dot + name + null */
  472. x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
  473. cp = malloc(x);
  474. sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
  475. pTap->dotted_name = cp;
  476. LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
  477. pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
  478. /* IEEE specifies that the two LSBs of an IR scan are 01, so make
  479. * that the default. The "-irlen" and "-irmask" options are only
  480. * needed to cope with nonstandard TAPs, or to specify more bits.
  481. */
  482. pTap->ir_capture_mask = 0x03;
  483. pTap->ir_capture_value = 0x01;
  484. while (goi->argc) {
  485. e = Jim_GetOpt_Nvp(goi, opts, &n);
  486. if (e != JIM_OK) {
  487. Jim_GetOpt_NvpUnknown(goi, opts, 0);
  488. free((void *)pTap->dotted_name);
  489. free(pTap);
  490. return e;
  491. }
  492. LOG_DEBUG("Processing option: %s", n->name);
  493. switch (n->value) {
  494. case NTAP_OPT_ENABLED:
  495. pTap->disabled_after_reset = false;
  496. break;
  497. case NTAP_OPT_DISABLED:
  498. pTap->disabled_after_reset = true;
  499. break;
  500. case NTAP_OPT_EXPECTED_ID:
  501. e = jim_newtap_expected_id(n, goi, pTap);
  502. if (JIM_OK != e)
  503. {
  504. free((void *)pTap->dotted_name);
  505. free(pTap);
  506. return e;
  507. }
  508. break;
  509. case NTAP_OPT_IRLEN:
  510. case NTAP_OPT_IRMASK:
  511. case NTAP_OPT_IRCAPTURE:
  512. e = jim_newtap_ir_param(n, goi, pTap);
  513. if (JIM_OK != e)
  514. {
  515. free((void *)pTap->dotted_name);
  516. free(pTap);
  517. return e;
  518. }
  519. break;
  520. case NTAP_OPT_VERSION:
  521. pTap->ignore_version = true;
  522. break;
  523. } /* switch (n->value) */
  524. } /* while (goi->argc) */
  525. /* default is enabled-after-reset */
  526. pTap->enabled = !pTap->disabled_after_reset;
  527. /* Did all the required option bits get cleared? */
  528. if (pTap->ir_length != 0)
  529. {
  530. jtag_tap_init(pTap);
  531. return ERROR_OK;
  532. }
  533. Jim_SetResult_sprintf(goi->interp,
  534. "newtap: %s missing IR length",
  535. pTap->dotted_name);
  536. jtag_tap_free(pTap);
  537. return JIM_ERR;
  538. }
  539. static void jtag_tap_handle_event(struct jtag_tap *tap, enum jtag_event e)
  540. {
  541. struct jtag_tap_event_action * jteap;
  542. for (jteap = tap->event_action; jteap != NULL; jteap = jteap->next)
  543. {
  544. if (jteap->event != e)
  545. continue;
  546. Jim_Nvp *nvp = Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e);
  547. LOG_DEBUG("JTAG tap: %s event: %d (%s)\n\taction: %s",
  548. tap->dotted_name, e, nvp->name,
  549. Jim_GetString(jteap->body, NULL));
  550. if (Jim_EvalObj(jteap->interp, jteap->body) != JIM_OK)
  551. {
  552. Jim_PrintErrorMessage(jteap->interp);
  553. continue;
  554. }
  555. switch (e)
  556. {
  557. case JTAG_TAP_EVENT_ENABLE:
  558. case JTAG_TAP_EVENT_DISABLE:
  559. /* NOTE: we currently assume the handlers
  560. * can't fail. Right here is where we should
  561. * really be verifying the scan chains ...
  562. */
  563. tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
  564. LOG_INFO("JTAG tap: %s %s", tap->dotted_name,
  565. tap->enabled ? "enabled" : "disabled");
  566. break;
  567. default:
  568. break;
  569. }
  570. }
  571. }
  572. static int jim_jtag_interface(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  573. {
  574. Jim_GetOptInfo goi;
  575. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  576. /* return the name of the interface */
  577. /* TCL code might need to know the exact type... */
  578. /* FUTURE: we allow this as a means to "set" the interface. */
  579. if (goi.argc != 0) {
  580. Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
  581. return JIM_ERR;
  582. }
  583. const char *name = jtag_interface ? jtag_interface->name : NULL;
  584. Jim_SetResultString(goi.interp, name ? : "undefined", -1);
  585. return JIM_OK;
  586. }
  587. static int jim_jtag_arp_init(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  588. {
  589. Jim_GetOptInfo goi;
  590. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  591. if (goi.argc != 0) {
  592. Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
  593. return JIM_ERR;
  594. }
  595. struct command_context *context = Jim_GetAssocData(interp, "context");
  596. int e = jtag_init_inner(context);
  597. if (e != ERROR_OK) {
  598. Jim_SetResult_sprintf(goi.interp, "error: %d", e);
  599. return JIM_ERR;
  600. }
  601. return JIM_OK;
  602. }
  603. static int jim_jtag_arp_init_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  604. {
  605. Jim_GetOptInfo goi;
  606. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  607. if (goi.argc != 0) {
  608. Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
  609. return JIM_ERR;
  610. }
  611. struct command_context *context = Jim_GetAssocData(interp, "context");
  612. int e = jtag_init_reset(context);
  613. if (e != ERROR_OK) {
  614. Jim_SetResult_sprintf(goi.interp, "error: %d", e);
  615. return JIM_ERR;
  616. }
  617. return JIM_OK;
  618. }
  619. static int jim_jtag_newtap(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  620. {
  621. Jim_GetOptInfo goi;
  622. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  623. return jim_newtap_cmd(&goi);
  624. }
  625. static bool jtag_tap_enable(struct jtag_tap *t)
  626. {
  627. if (t->enabled)
  628. return false;
  629. jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
  630. if (!t->enabled)
  631. return false;
  632. /* FIXME add JTAG sanity checks, w/o TLR
  633. * - scan chain length grew by one (this)
  634. * - IDs and IR lengths are as expected
  635. */
  636. jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
  637. return true;
  638. }
  639. static bool jtag_tap_disable(struct jtag_tap *t)
  640. {
  641. if (!t->enabled)
  642. return false;
  643. jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
  644. if (t->enabled)
  645. return false;
  646. /* FIXME add JTAG sanity checks, w/o TLR
  647. * - scan chain length shrank by one (this)
  648. * - IDs and IR lengths are as expected
  649. */
  650. jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
  651. return true;
  652. }
  653. static int jim_jtag_tap_enabler(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  654. {
  655. const char *cmd_name = Jim_GetString(argv[0], NULL);
  656. Jim_GetOptInfo goi;
  657. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  658. if (goi.argc != 1) {
  659. Jim_SetResult_sprintf(goi.interp, "usage: %s <name>", cmd_name);
  660. return JIM_ERR;
  661. }
  662. struct jtag_tap *t;
  663. t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
  664. if (t == NULL)
  665. return JIM_ERR;
  666. if (strcasecmp(cmd_name, "tapisenabled") == 0) {
  667. // do nothing, just return the value
  668. } else if (strcasecmp(cmd_name, "tapenable") == 0) {
  669. if (!jtag_tap_enable(t))
  670. LOG_WARNING("failed to disable tap");
  671. } else if (strcasecmp(cmd_name, "tapdisable") == 0) {
  672. if (!jtag_tap_disable(t))
  673. LOG_WARNING("failed to disable tap");
  674. } else {
  675. LOG_ERROR("command '%s' unknown", cmd_name);
  676. return JIM_ERR;
  677. }
  678. bool e = t->enabled;
  679. Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
  680. return JIM_OK;
  681. }
  682. static int jim_jtag_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  683. {
  684. const char *cmd_name = Jim_GetString(argv[0], NULL);
  685. Jim_GetOptInfo goi;
  686. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  687. goi.isconfigure = !strcmp(cmd_name, "configure");
  688. if (goi.argc < 2 + goi.isconfigure) {
  689. Jim_WrongNumArgs(goi.interp, 0, NULL,
  690. "<tap_name> <attribute> ...");
  691. return JIM_ERR;
  692. }
  693. struct jtag_tap *t;
  694. Jim_Obj *o;
  695. Jim_GetOpt_Obj(&goi, &o);
  696. t = jtag_tap_by_jim_obj(goi.interp, o);
  697. if (t == NULL) {
  698. return JIM_ERR;
  699. }
  700. return jtag_tap_configure_cmd(&goi, t);
  701. }
  702. static int jim_jtag_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  703. {
  704. Jim_GetOptInfo goi;
  705. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  706. if (goi.argc != 0) {
  707. Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
  708. return JIM_ERR;
  709. }
  710. Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
  711. struct jtag_tap *tap;
  712. for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
  713. Jim_ListAppendElement(goi.interp,
  714. Jim_GetResult(goi.interp),
  715. Jim_NewStringObj(goi.interp,
  716. tap->dotted_name, -1));
  717. }
  718. return JIM_OK;
  719. }
  720. COMMAND_HANDLER(handle_jtag_init_command)
  721. {
  722. if (CMD_ARGC != 0)
  723. return ERROR_COMMAND_SYNTAX_ERROR;
  724. static bool jtag_initialized = false;
  725. if (jtag_initialized)
  726. {
  727. LOG_INFO("'jtag init' has already been called");
  728. return ERROR_OK;
  729. }
  730. jtag_initialized = true;
  731. LOG_DEBUG("Initializing jtag devices...");
  732. return jtag_init(CMD_CTX);
  733. }
  734. static const struct command_registration jtag_subcommand_handlers[] = {
  735. {
  736. .name = "init",
  737. .mode = COMMAND_ANY,
  738. .handler = &handle_jtag_init_command,
  739. .help = "initialize jtag scan chain",
  740. },
  741. {
  742. .name = "interface",
  743. .mode = COMMAND_ANY,
  744. .jim_handler = &jim_jtag_interface,
  745. .help = "Returns the selected interface",
  746. },
  747. {
  748. .name = "arp_init",
  749. .mode = COMMAND_ANY,
  750. .jim_handler = &jim_jtag_arp_init,
  751. },
  752. {
  753. .name = "arp_init-reset",
  754. .mode = COMMAND_ANY,
  755. .jim_handler = &jim_jtag_arp_init_reset,
  756. },
  757. {
  758. .name = "newtap",
  759. .mode = COMMAND_CONFIG,
  760. .jim_handler = &jim_jtag_newtap,
  761. .help = "Create a new TAP instance",
  762. .usage = "<name> <type> -irlen <count> [-ircapture <count>] "
  763. "[-irmask <count>] [-enable|-disable]",
  764. },
  765. {
  766. .name = "tapisenabled",
  767. .mode = COMMAND_EXEC,
  768. .jim_handler = &jim_jtag_tap_enabler,
  769. .help = "Returns a integer indicating TAP state (0/1)",
  770. .usage = "<name>",
  771. },
  772. {
  773. .name = "tapenable",
  774. .mode = COMMAND_EXEC,
  775. .jim_handler = &jim_jtag_tap_enabler,
  776. .help = "Enable the specified TAP",
  777. .usage = "<name>",
  778. },
  779. {
  780. .name = "tapdisable",
  781. .mode = COMMAND_EXEC,
  782. .jim_handler = &jim_jtag_tap_enabler,
  783. .help = "Enable the specified TAP",
  784. .usage = "<name>",
  785. },
  786. {
  787. .name = "configure",
  788. .mode = COMMAND_EXEC,
  789. .jim_handler = &jim_jtag_configure,
  790. .help = "Enable the specified TAP",
  791. .usage = "<name> [<key> <value> ...]",
  792. },
  793. {
  794. .name = "cget",
  795. .mode = COMMAND_EXEC,
  796. .jim_handler = &jim_jtag_configure,
  797. .help = "Enable the specified TAP",
  798. .usage = "<name> [<key> <value> ...]",
  799. },
  800. {
  801. .name = "names",
  802. .mode = COMMAND_ANY,
  803. .jim_handler = &jim_jtag_names,
  804. .help = "Returns list of all JTAG tap names",
  805. },
  806. {
  807. .chain = jtag_command_handlers_to_move,
  808. },
  809. COMMAND_REGISTRATION_DONE
  810. };
  811. void jtag_notify_event(enum jtag_event event)
  812. {
  813. struct jtag_tap *tap;
  814. for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
  815. jtag_tap_handle_event(tap, event);
  816. }
  817. static int default_khz(int khz, int *jtag_speed)
  818. {
  819. LOG_ERROR("Translation from khz to jtag_speed not implemented");
  820. return ERROR_FAIL;
  821. }
  822. static int default_speed_div(int speed, int *khz)
  823. {
  824. LOG_ERROR("Translation from jtag_speed to khz not implemented");
  825. return ERROR_FAIL;
  826. }
  827. static int default_power_dropout(int *dropout)
  828. {
  829. *dropout = 0; /* by default we can't detect power dropout */
  830. return ERROR_OK;
  831. }
  832. static int default_srst_asserted(int *srst_asserted)
  833. {
  834. *srst_asserted = 0; /* by default we can't detect srst asserted */
  835. return ERROR_OK;
  836. }
  837. COMMAND_HANDLER(handle_interface_list_command)
  838. {
  839. if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
  840. return ERROR_COMMAND_SYNTAX_ERROR;
  841. command_print(CMD_CTX, "The following JTAG interfaces are available:");
  842. for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
  843. {
  844. const char *name = jtag_interfaces[i]->name;
  845. command_print(CMD_CTX, "%u: %s", i + 1, name);
  846. }
  847. return ERROR_OK;
  848. }
  849. COMMAND_HANDLER(handle_interface_command)
  850. {
  851. /* check whether the interface is already configured */
  852. if (jtag_interface)
  853. {
  854. LOG_WARNING("Interface already configured, ignoring");
  855. return ERROR_OK;
  856. }
  857. /* interface name is a mandatory argument */
  858. if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
  859. return ERROR_COMMAND_SYNTAX_ERROR;
  860. for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
  861. {
  862. if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
  863. continue;
  864. if (NULL != jtag_interfaces[i]->commands)
  865. {
  866. int retval = register_commands(CMD_CTX, NULL,
  867. jtag_interfaces[i]->commands);
  868. if (ERROR_OK != retval)
  869. return retval;
  870. }
  871. jtag_interface = jtag_interfaces[i];
  872. if (jtag_interface->khz == NULL)
  873. jtag_interface->khz = default_khz;
  874. if (jtag_interface->speed_div == NULL)
  875. jtag_interface->speed_div = default_speed_div;
  876. if (jtag_interface->power_dropout == NULL)
  877. jtag_interface->power_dropout = default_power_dropout;
  878. if (jtag_interface->srst_asserted == NULL)
  879. jtag_interface->srst_asserted = default_srst_asserted;
  880. return ERROR_OK;
  881. }
  882. /* no valid interface was found (i.e. the configuration option,
  883. * didn't match one of the compiled-in interfaces
  884. */
  885. LOG_ERROR("The specified JTAG interface was not found (%s)", CMD_ARGV[0]);
  886. CALL_COMMAND_HANDLER(handle_interface_list_command);
  887. return ERROR_JTAG_INVALID_INTERFACE;
  888. }
  889. COMMAND_HANDLER(handle_scan_chain_command)
  890. {
  891. struct jtag_tap *tap;
  892. char expected_id[12];
  893. tap = jtag_all_taps();
  894. command_print(CMD_CTX,
  895. " TapName Enabled IdCode Expected IrLen IrCap IrMask");
  896. command_print(CMD_CTX,
  897. "-- ------------------- -------- ---------- ---------- ----- ----- ------");
  898. while (tap) {
  899. uint32_t expected, expected_mask, ii;
  900. snprintf(expected_id, sizeof expected_id, "0x%08x",
  901. (unsigned)((tap->expected_ids_cnt > 0)
  902. ? tap->expected_ids[0]
  903. : 0));
  904. if (tap->ignore_version)
  905. expected_id[2] = '*';
  906. expected = buf_get_u32(tap->expected, 0, tap->ir_length);
  907. expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
  908. command_print(CMD_CTX,
  909. "%2d %-18s %c 0x%08x %s %5d 0x%02x 0x%02x",
  910. tap->abs_chain_position,
  911. tap->dotted_name,
  912. tap->enabled ? 'Y' : 'n',
  913. (unsigned int)(tap->idcode),
  914. expected_id,
  915. (unsigned int)(tap->ir_length),
  916. (unsigned int)(expected),
  917. (unsigned int)(expected_mask));
  918. for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
  919. snprintf(expected_id, sizeof expected_id, "0x%08x",
  920. (unsigned) tap->expected_ids[1]);
  921. if (tap->ignore_version)
  922. expected_id[2] = '*';
  923. command_print(CMD_CTX,
  924. " %s",
  925. expected_id);
  926. }
  927. tap = tap->next_tap;
  928. }
  929. return ERROR_OK;
  930. }
  931. COMMAND_HANDLER(handle_reset_config_command)
  932. {
  933. int new_cfg = 0;
  934. int mask = 0;
  935. /* Original versions cared about the order of these tokens:
  936. * reset_config signals [combination [trst_type [srst_type]]]
  937. * They also clobbered the previous configuration even on error.
  938. *
  939. * Here we don't care about the order, and only change values
  940. * which have been explicitly specified.
  941. */
  942. for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
  943. int tmp = 0;
  944. int m;
  945. /* gating */
  946. m = RESET_SRST_NO_GATING;
  947. if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
  948. /* default: don't use JTAG while SRST asserted */;
  949. else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
  950. tmp = RESET_SRST_NO_GATING;
  951. else
  952. m = 0;
  953. if (mask & m) {
  954. LOG_ERROR("extra reset_config %s spec (%s)",
  955. "gating", *CMD_ARGV);
  956. return ERROR_INVALID_ARGUMENTS;
  957. }
  958. if (m)
  959. goto next;
  960. /* signals */
  961. m = RESET_HAS_TRST | RESET_HAS_SRST;
  962. if (strcmp(*CMD_ARGV, "none") == 0)
  963. tmp = RESET_NONE;
  964. else if (strcmp(*CMD_ARGV, "trst_only") == 0)
  965. tmp = RESET_HAS_TRST;
  966. else if (strcmp(*CMD_ARGV, "srst_only") == 0)
  967. tmp = RESET_HAS_SRST;
  968. else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
  969. tmp = RESET_HAS_TRST | RESET_HAS_SRST;
  970. else
  971. m = 0;
  972. if (mask & m) {
  973. LOG_ERROR("extra reset_config %s spec (%s)",
  974. "signal", *CMD_ARGV);
  975. return ERROR_INVALID_ARGUMENTS;
  976. }
  977. if (m)
  978. goto next;
  979. /* combination (options for broken wiring) */
  980. m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  981. if (strcmp(*CMD_ARGV, "separate") == 0)
  982. /* separate reset lines - default */;
  983. else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
  984. tmp |= RESET_SRST_PULLS_TRST;
  985. else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
  986. tmp |= RESET_TRST_PULLS_SRST;
  987. else if (strcmp(*CMD_ARGV, "combined") == 0)
  988. tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  989. else
  990. m = 0;
  991. if (mask & m) {
  992. LOG_ERROR("extra reset_config %s spec (%s)",
  993. "combination", *CMD_ARGV);
  994. return ERROR_INVALID_ARGUMENTS;
  995. }
  996. if (m)
  997. goto next;
  998. /* trst_type (NOP without HAS_TRST) */
  999. m = RESET_TRST_OPEN_DRAIN;
  1000. if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
  1001. tmp |= RESET_TRST_OPEN_DRAIN;
  1002. else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
  1003. /* push/pull from adapter - default */;
  1004. else
  1005. m = 0;
  1006. if (mask & m) {
  1007. LOG_ERROR("extra reset_config %s spec (%s)",
  1008. "trst_type", *CMD_ARGV);
  1009. return ERROR_INVALID_ARGUMENTS;
  1010. }
  1011. if (m)
  1012. goto next;
  1013. /* srst_type (NOP without HAS_SRST) */
  1014. m |= RESET_SRST_PUSH_PULL;
  1015. if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
  1016. tmp |= RESET_SRST_PUSH_PULL;
  1017. else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
  1018. /* open drain from adapter - default */;
  1019. else
  1020. m = 0;
  1021. if (mask & m) {
  1022. LOG_ERROR("extra reset_config %s spec (%s)",
  1023. "srst_type", *CMD_ARGV);
  1024. return ERROR_INVALID_ARGUMENTS;
  1025. }
  1026. if (m)
  1027. goto next;
  1028. /* caller provided nonsense; fail */
  1029. LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
  1030. return ERROR_INVALID_ARGUMENTS;
  1031. next:
  1032. /* Remember the bits which were specified (mask)
  1033. * and their new values (new_cfg).
  1034. */
  1035. mask |= m;
  1036. new_cfg |= tmp;
  1037. }
  1038. /* clear previous values of those bits, save new values */
  1039. if (mask) {
  1040. int old_cfg = jtag_get_reset_config();
  1041. old_cfg &= ~mask;
  1042. new_cfg |= old_cfg;
  1043. jtag_set_reset_config(new_cfg);
  1044. } else
  1045. new_cfg = jtag_get_reset_config();
  1046. /*
  1047. * Display the (now-)current reset mode
  1048. */
  1049. char *modes[5];
  1050. /* minimal JTAG has neither SRST nor TRST (so that's the default) */
  1051. switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
  1052. case RESET_HAS_SRST:
  1053. modes[0] = "srst_only";
  1054. break;
  1055. case RESET_HAS_TRST:
  1056. modes[0] = "trst_only";
  1057. break;
  1058. case RESET_TRST_AND_SRST:
  1059. modes[0] = "trst_and_srst";
  1060. break;
  1061. default:
  1062. modes[0] = "none";
  1063. break;
  1064. }
  1065. /* normally SRST and TRST are decoupled; but bugs happen ... */
  1066. switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
  1067. case RESET_SRST_PULLS_TRST:
  1068. modes[1] = "srst_pulls_trst";
  1069. break;
  1070. case RESET_TRST_PULLS_SRST:
  1071. modes[1] = "trst_pulls_srst";
  1072. break;
  1073. case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
  1074. modes[1] = "combined";
  1075. break;
  1076. default:
  1077. modes[1] = "separate";
  1078. break;
  1079. }
  1080. /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
  1081. if (new_cfg & RESET_HAS_TRST) {
  1082. if (new_cfg & RESET_TRST_OPEN_DRAIN)
  1083. modes[3] = " trst_open_drain";
  1084. else
  1085. modes[3] = " trst_push_pull";
  1086. } else
  1087. modes[3] = "";
  1088. /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
  1089. if (new_cfg & RESET_HAS_SRST) {
  1090. if (new_cfg & RESET_SRST_NO_GATING)
  1091. modes[2] = " srst_nogate";
  1092. else
  1093. modes[2] = " srst_gates_jtag";
  1094. if (new_cfg & RESET_SRST_PUSH_PULL)
  1095. modes[4] = " srst_push_pull";
  1096. else
  1097. modes[4] = " srst_open_drain";
  1098. } else {
  1099. modes[2] = "";
  1100. modes[4] = "";
  1101. }
  1102. command_print(CMD_CTX, "%s %s%s%s%s",
  1103. modes[0], modes[1],
  1104. modes[2], modes[3], modes[4]);
  1105. return ERROR_OK;
  1106. }
  1107. COMMAND_HANDLER(handle_jtag_nsrst_delay_command)
  1108. {
  1109. if (CMD_ARGC > 1)
  1110. return ERROR_COMMAND_SYNTAX_ERROR;
  1111. if (CMD_ARGC == 1)
  1112. {
  1113. unsigned delay;
  1114. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
  1115. jtag_set_nsrst_delay(delay);
  1116. }
  1117. command_print(CMD_CTX, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
  1118. return ERROR_OK;
  1119. }
  1120. COMMAND_HANDLER(handle_jtag_ntrst_delay_command)
  1121. {
  1122. if (CMD_ARGC > 1)
  1123. return ERROR_COMMAND_SYNTAX_ERROR;
  1124. if (CMD_ARGC == 1)
  1125. {
  1126. unsigned delay;
  1127. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
  1128. jtag_set_ntrst_delay(delay);
  1129. }
  1130. command_print(CMD_CTX, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
  1131. return ERROR_OK;
  1132. }
  1133. COMMAND_HANDLER(handle_jtag_nsrst_assert_width_command)
  1134. {
  1135. if (CMD_ARGC > 1)
  1136. return ERROR_COMMAND_SYNTAX_ERROR;
  1137. if (CMD_ARGC == 1)
  1138. {
  1139. unsigned delay;
  1140. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
  1141. jtag_set_nsrst_assert_width(delay);
  1142. }
  1143. command_print(CMD_CTX, "jtag_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
  1144. return ERROR_OK;
  1145. }
  1146. COMMAND_HANDLER(handle_jtag_ntrst_assert_width_command)
  1147. {
  1148. if (CMD_ARGC > 1)
  1149. return ERROR_COMMAND_SYNTAX_ERROR;
  1150. if (CMD_ARGC == 1)
  1151. {
  1152. unsigned delay;
  1153. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
  1154. jtag_set_ntrst_assert_width(delay);
  1155. }
  1156. command_print(CMD_CTX, "jtag_ntrst_assert_width: %u", jtag_get_ntrst_assert_width());
  1157. return ERROR_OK;
  1158. }
  1159. COMMAND_HANDLER(handle_jtag_khz_command)
  1160. {
  1161. if (CMD_ARGC > 1)
  1162. return ERROR_COMMAND_SYNTAX_ERROR;
  1163. int retval = ERROR_OK;
  1164. if (CMD_ARGC == 1)
  1165. {
  1166. unsigned khz = 0;
  1167. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
  1168. retval = jtag_config_khz(khz);
  1169. if (ERROR_OK != retval)
  1170. return retval;
  1171. }
  1172. int cur_speed = jtag_get_speed_khz();
  1173. retval = jtag_get_speed_readable(&cur_speed);
  1174. if (ERROR_OK != retval)
  1175. return retval;
  1176. if (cur_speed)
  1177. command_print(CMD_CTX, "%d kHz", cur_speed);
  1178. else
  1179. command_print(CMD_CTX, "RCLK - adaptive");
  1180. return retval;
  1181. }
  1182. COMMAND_HANDLER(handle_jtag_rclk_command)
  1183. {
  1184. if (CMD_ARGC > 1)
  1185. return ERROR_COMMAND_SYNTAX_ERROR;
  1186. int retval = ERROR_OK;
  1187. if (CMD_ARGC == 1)
  1188. {
  1189. unsigned khz = 0;
  1190. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
  1191. retval = jtag_config_rclk(khz);
  1192. if (ERROR_OK != retval)
  1193. return retval;
  1194. }
  1195. int cur_khz = jtag_get_speed_khz();
  1196. retval = jtag_get_speed_readable(&cur_khz);
  1197. if (ERROR_OK != retval)
  1198. return retval;
  1199. if (cur_khz)
  1200. command_print(CMD_CTX, "RCLK not supported - fallback to %d kHz", cur_khz);
  1201. else
  1202. command_print(CMD_CTX, "RCLK - adaptive");
  1203. return retval;
  1204. }
  1205. COMMAND_HANDLER(handle_jtag_reset_command)
  1206. {
  1207. if (CMD_ARGC != 2)
  1208. return ERROR_COMMAND_SYNTAX_ERROR;
  1209. int trst = -1;
  1210. if (CMD_ARGV[0][0] == '1')
  1211. trst = 1;
  1212. else if (CMD_ARGV[0][0] == '0')
  1213. trst = 0;
  1214. else
  1215. return ERROR_COMMAND_SYNTAX_ERROR;
  1216. int srst = -1;
  1217. if (CMD_ARGV[1][0] == '1')
  1218. srst = 1;
  1219. else if (CMD_ARGV[1][0] == '0')
  1220. srst = 0;
  1221. else
  1222. return ERROR_COMMAND_SYNTAX_ERROR;
  1223. if (jtag_interface_init(CMD_CTX) != ERROR_OK)
  1224. return ERROR_JTAG_INIT_FAILED;
  1225. jtag_add_reset(trst, srst);
  1226. return jtag_execute_queue();
  1227. }
  1228. COMMAND_HANDLER(handle_runtest_command)
  1229. {
  1230. if (CMD_ARGC != 1)
  1231. return ERROR_COMMAND_SYNTAX_ERROR;
  1232. unsigned num_clocks;
  1233. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num_clocks);
  1234. jtag_add_runtest(num_clocks, TAP_IDLE);
  1235. return jtag_execute_queue();
  1236. }
  1237. /*
  1238. * For "irscan" or "drscan" commands, the "end" (really, "next") state
  1239. * should be stable ... and *NOT* a shift state, otherwise free-running
  1240. * jtag clocks could change the values latched by the update state.
  1241. * Not surprisingly, this is the same constraint as SVF; the "irscan"
  1242. * and "drscan" commands are a write-only subset of what SVF provides.
  1243. */
  1244. COMMAND_HANDLER(handle_irscan_command)
  1245. {
  1246. int i;
  1247. struct scan_field *fields;
  1248. struct jtag_tap *tap;
  1249. tap_state_t endstate;
  1250. if ((CMD_ARGC < 2) || (CMD_ARGC % 2))
  1251. {
  1252. return ERROR_COMMAND_SYNTAX_ERROR;
  1253. }
  1254. /* optional "-endstate" "statename" at the end of the arguments,
  1255. * so that e.g. IRPAUSE can let us load the data register before
  1256. * entering RUN/IDLE to execute the instruction we load here.
  1257. */
  1258. endstate = TAP_IDLE;
  1259. if (CMD_ARGC >= 4) {
  1260. /* have at least one pair of numbers. */
  1261. /* is last pair the magic text? */
  1262. if (strcmp("-endstate", CMD_ARGV[CMD_ARGC - 2]) == 0) {
  1263. endstate = tap_state_by_name(CMD_ARGV[CMD_ARGC - 1]);
  1264. if (endstate == TAP_INVALID)
  1265. return ERROR_COMMAND_SYNTAX_ERROR;
  1266. if (!scan_is_safe(endstate))
  1267. LOG_WARNING("unstable irscan endstate \"%s\"",
  1268. CMD_ARGV[CMD_ARGC - 1]);
  1269. CMD_ARGC -= 2;
  1270. }
  1271. }
  1272. int num_fields = CMD_ARGC / 2;
  1273. size_t fields_len = sizeof(struct scan_field) * num_fields;
  1274. fields = malloc(fields_len);
  1275. memset(fields, 0, fields_len);
  1276. int retval;
  1277. for (i = 0; i < num_fields; i++)
  1278. {
  1279. tap = jtag_tap_by_string(CMD_ARGV[i*2]);
  1280. if (tap == NULL)
  1281. {
  1282. int j;
  1283. for (j = 0; j < i; j++)
  1284. free(fields[j].out_value);
  1285. free(fields);
  1286. command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i*2]);
  1287. return ERROR_FAIL;
  1288. }
  1289. int field_size = tap->ir_length;
  1290. fields[i].tap = tap;
  1291. fields[i].num_bits = field_size;
  1292. fields[i].out_value = malloc(DIV_ROUND_UP(field_size, 8));
  1293. uint32_t value;
  1294. retval = parse_u32(CMD_ARGV[i * 2 + 1], &value);
  1295. if (ERROR_OK != retval)
  1296. goto error_return;
  1297. buf_set_u32(fields[i].out_value, 0, field_size, value);
  1298. fields[i].in_value = NULL;
  1299. }
  1300. /* did we have an endstate? */
  1301. jtag_add_ir_scan(num_fields, fields, endstate);
  1302. retval = jtag_execute_queue();
  1303. error_return:
  1304. for (i = 0; i < num_fields; i++)
  1305. {
  1306. if (NULL != fields[i].out_value)
  1307. free(fields[i].out_value);
  1308. }
  1309. free (fields);
  1310. return retval;
  1311. }
  1312. COMMAND_HANDLER(handle_verify_ircapture_command)
  1313. {
  1314. if (CMD_ARGC > 1)
  1315. return ERROR_COMMAND_SYNTAX_ERROR;
  1316. if (CMD_ARGC == 1)
  1317. {
  1318. bool enable;
  1319. COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
  1320. jtag_set_verify_capture_ir(enable);
  1321. }
  1322. const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
  1323. command_print(CMD_CTX, "verify Capture-IR is %s", status);
  1324. return ERROR_OK;
  1325. }
  1326. COMMAND_HANDLER(handle_verify_jtag_command)
  1327. {
  1328. if (CMD_ARGC > 1)
  1329. return ERROR_COMMAND_SYNTAX_ERROR;
  1330. if (CMD_ARGC == 1)
  1331. {
  1332. bool enable;
  1333. COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
  1334. jtag_set_verify(enable);
  1335. }
  1336. const char *status = jtag_will_verify() ? "enabled": "disabled";
  1337. command_print(CMD_CTX, "verify jtag capture is %s", status);
  1338. return ERROR_OK;
  1339. }
  1340. COMMAND_HANDLER(handle_tms_sequence_command)
  1341. {
  1342. if (CMD_ARGC > 1)
  1343. return ERROR_COMMAND_SYNTAX_ERROR;
  1344. if (CMD_ARGC == 1)
  1345. {
  1346. bool use_new_table;
  1347. if (strcmp(CMD_ARGV[0], "short") == 0)
  1348. use_new_table = true;
  1349. else if (strcmp(CMD_ARGV[0], "long") == 0)
  1350. use_new_table = false;
  1351. else
  1352. return ERROR_COMMAND_SYNTAX_ERROR;
  1353. tap_use_new_tms_table(use_new_table);
  1354. }
  1355. command_print(CMD_CTX, "tms sequence is %s",
  1356. tap_uses_new_tms_table() ? "short": "long");
  1357. return ERROR_OK;
  1358. }
  1359. static const struct command_registration jtag_command_handlers[] = {
  1360. {
  1361. .name = "interface",
  1362. .handler = &handle_interface_command,
  1363. .mode = COMMAND_CONFIG,
  1364. .help = "select a JTAG interface",
  1365. .usage = "<driver_name>",
  1366. },
  1367. {
  1368. .name = "interface_list",
  1369. .handler = &handle_interface_list_command,
  1370. .mode = COMMAND_ANY,
  1371. .help = "list all built-in interfaces",
  1372. },
  1373. {
  1374. .name = "jtag_khz",
  1375. .handler = &handle_jtag_khz_command,
  1376. .mode = COMMAND_ANY,
  1377. .help = "set maximum jtag speed (if supported)",
  1378. .usage = "<khz:0=rtck>",
  1379. },
  1380. {
  1381. .name = "jtag_rclk",
  1382. .handler = &handle_jtag_rclk_command,
  1383. .mode = COMMAND_ANY,
  1384. .help = "set JTAG speed to RCLK or use fallback speed",
  1385. .usage = "<fallback_speed_khz>",
  1386. },
  1387. {
  1388. .name = "reset_config",
  1389. .handler = &handle_reset_config_command,
  1390. .mode = COMMAND_ANY,
  1391. .help = "configure JTAG reset behavior",
  1392. .usage = "[none|trst_only|srst_only|trst_and_srst] "
  1393. "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
  1394. "[srst_gates_jtag|srst_nogate] "
  1395. "[trst_push_pull|trst_open_drain] "
  1396. "[srst_push_pull|srst_open_drain]",
  1397. },
  1398. {
  1399. .name = "jtag_nsrst_delay",
  1400. .handler = &handle_jtag_nsrst_delay_command,
  1401. .mode = COMMAND_ANY,
  1402. .help = "delay after deasserting srst in ms",
  1403. .usage = "<ms>",
  1404. },
  1405. {
  1406. .name = "jtag_ntrst_delay",
  1407. .handler = &handle_jtag_ntrst_delay_command,
  1408. .mode = COMMAND_ANY,
  1409. .help = "delay after deasserting trst in ms",
  1410. .usage = "<ms>"
  1411. },
  1412. {
  1413. .name = "jtag_nsrst_assert_width",
  1414. .handler = &handle_jtag_nsrst_assert_width_command,
  1415. .mode = COMMAND_ANY,
  1416. .help = "delay after asserting srst in ms",
  1417. .usage = "<ms>"
  1418. },
  1419. {
  1420. .name = "jtag_ntrst_assert_width",
  1421. .handler = &handle_jtag_ntrst_assert_width_command,
  1422. .mode = COMMAND_ANY,
  1423. .help = "delay after asserting trst in ms",
  1424. .usage = "<ms>"
  1425. },
  1426. {
  1427. .name = "scan_chain",
  1428. .handler = &handle_scan_chain_command,
  1429. .mode = COMMAND_EXEC,
  1430. .help = "print current scan chain configuration",
  1431. },
  1432. {
  1433. .name = "jtag_reset",
  1434. .handler = &handle_jtag_reset_command,
  1435. .mode = COMMAND_EXEC,
  1436. .help = "toggle reset lines",
  1437. .usage = "<trst> <srst>",
  1438. },
  1439. {
  1440. .name = "runtest",
  1441. .handler = &handle_runtest_command,
  1442. .mode = COMMAND_EXEC,
  1443. .help = "move to Run-Test/Idle, and execute <num_cycles>",
  1444. .usage = "<num_cycles>"
  1445. },
  1446. {
  1447. .name = "irscan",
  1448. .handler = &handle_irscan_command,
  1449. .mode = COMMAND_EXEC,
  1450. .help = "execute IR scan",
  1451. .usage = "<device> <instr> [dev2] [instr2] ...",
  1452. },
  1453. {
  1454. .name = "verify_ircapture",
  1455. .handler = &handle_verify_ircapture_command,
  1456. .mode = COMMAND_ANY,
  1457. .help = "verify value captured during Capture-IR",
  1458. .usage = "<enable | disable>",
  1459. },
  1460. {
  1461. .name = "verify_jtag",
  1462. .handler = &handle_verify_jtag_command,
  1463. .mode = COMMAND_ANY,
  1464. .help = "verify value capture",
  1465. .usage = "<enable | disable>",
  1466. },
  1467. {
  1468. .name = "tms_sequence",
  1469. .handler = &handle_tms_sequence_command,
  1470. .mode = COMMAND_ANY,
  1471. .help = "choose short(default) or long tms_sequence",
  1472. .usage = "<short | long>",
  1473. },
  1474. {
  1475. .name = "jtag",
  1476. .mode = COMMAND_ANY,
  1477. .help = "perform jtag tap actions",
  1478. .chain = jtag_subcommand_handlers,
  1479. },
  1480. {
  1481. .chain = jtag_command_handlers_to_move,
  1482. },
  1483. COMMAND_REGISTRATION_DONE
  1484. };
  1485. int jtag_register_commands(struct command_context *cmd_ctx)
  1486. {
  1487. return register_commands(cmd_ctx, NULL, jtag_command_handlers);
  1488. }