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.
 
 
 
 
 
 

1475 lines
40 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_TAP_EVENT_POST_RESET, .name = "post-reset" },
  42. { .value = JTAG_TAP_EVENT_ENABLE, .name = "tap-enable" },
  43. { .value = JTAG_TAP_EVENT_DISABLE, .name = "tap-disable" },
  44. { .name = NULL, .value = -1 }
  45. };
  46. extern jtag_interface_t *jtag_interface;
  47. /* jtag commands */
  48. static int handle_interface_list_command(struct command_context_s *cmd_ctx,
  49. char *cmd, char **args, int argc);
  50. static int handle_interface_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  51. static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  52. static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  53. static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  54. static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  55. static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  56. static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  57. static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  58. static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  59. static int handle_jtag_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  60. static int handle_runtest_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  61. static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  62. static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  63. static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
  64. static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args);
  65. static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  66. static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  67. static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  68. extern int jtag_examine_chain(void);
  69. extern int jtag_validate_chain(void);
  70. enum jtag_tap_cfg_param {
  71. JCFG_EVENT
  72. };
  73. static Jim_Nvp nvp_config_opts[] = {
  74. { .name = "-event", .value = JCFG_EVENT },
  75. { .name = NULL, .value = -1 }
  76. };
  77. static int jtag_tap_configure_cmd(Jim_GetOptInfo *goi, jtag_tap_t * tap)
  78. {
  79. Jim_Nvp *n;
  80. Jim_Obj *o;
  81. int e;
  82. /* parse config or cget options */
  83. while (goi->argc > 0) {
  84. Jim_SetEmptyResult (goi->interp);
  85. e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
  86. if (e != JIM_OK) {
  87. Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
  88. return e;
  89. }
  90. switch (n->value) {
  91. case JCFG_EVENT:
  92. if (goi->argc == 0) {
  93. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
  94. return JIM_ERR;
  95. }
  96. e = Jim_GetOpt_Nvp(goi, nvp_jtag_tap_event, &n);
  97. if (e != JIM_OK) {
  98. Jim_GetOpt_NvpUnknown(goi, nvp_jtag_tap_event, 1);
  99. return e;
  100. }
  101. if (goi->isconfigure) {
  102. if (goi->argc != 1) {
  103. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
  104. return JIM_ERR;
  105. }
  106. } else {
  107. if (goi->argc != 0) {
  108. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
  109. return JIM_ERR;
  110. }
  111. }
  112. {
  113. jtag_tap_event_action_t *jteap;
  114. jteap = tap->event_action;
  115. /* replace existing? */
  116. while (jteap) {
  117. if (jteap->event == (enum jtag_event)n->value) {
  118. break;
  119. }
  120. jteap = jteap->next;
  121. }
  122. if (goi->isconfigure) {
  123. bool replace = true;
  124. if (jteap == NULL) {
  125. /* create new */
  126. jteap = calloc(1, sizeof (*jteap));
  127. replace = false;
  128. }
  129. jteap->event = n->value;
  130. Jim_GetOpt_Obj(goi, &o);
  131. if (jteap->body) {
  132. Jim_DecrRefCount(interp, jteap->body);
  133. }
  134. jteap->body = Jim_DuplicateObj(goi->interp, o);
  135. Jim_IncrRefCount(jteap->body);
  136. if (!replace)
  137. {
  138. /* add to head of event list */
  139. jteap->next = tap->event_action;
  140. tap->event_action = jteap;
  141. }
  142. Jim_SetEmptyResult(goi->interp);
  143. } else {
  144. /* get */
  145. if (jteap == NULL) {
  146. Jim_SetEmptyResult(goi->interp);
  147. } else {
  148. Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, jteap->body));
  149. }
  150. }
  151. }
  152. /* loop for more */
  153. break;
  154. }
  155. } /* while (goi->argc) */
  156. return JIM_OK;
  157. }
  158. static int is_bad_irval(int ir_length, jim_wide w)
  159. {
  160. jim_wide v = 1;
  161. v <<= ir_length;
  162. v -= 1;
  163. v = ~v;
  164. return (w & v) != 0;
  165. }
  166. static int jim_newtap_cmd(Jim_GetOptInfo *goi)
  167. {
  168. jtag_tap_t *pTap;
  169. jim_wide w;
  170. int x;
  171. int e;
  172. int reqbits;
  173. Jim_Nvp *n;
  174. char *cp;
  175. const Jim_Nvp opts[] = {
  176. #define NTAP_OPT_IRLEN 0
  177. { .name = "-irlen" , .value = NTAP_OPT_IRLEN },
  178. #define NTAP_OPT_IRMASK 1
  179. { .name = "-irmask" , .value = NTAP_OPT_IRMASK },
  180. #define NTAP_OPT_IRCAPTURE 2
  181. { .name = "-ircapture" , .value = NTAP_OPT_IRCAPTURE },
  182. #define NTAP_OPT_ENABLED 3
  183. { .name = "-enable" , .value = NTAP_OPT_ENABLED },
  184. #define NTAP_OPT_DISABLED 4
  185. { .name = "-disable" , .value = NTAP_OPT_DISABLED },
  186. #define NTAP_OPT_EXPECTED_ID 5
  187. { .name = "-expected-id" , .value = NTAP_OPT_EXPECTED_ID },
  188. { .name = NULL , .value = -1 },
  189. };
  190. pTap = calloc(1, sizeof(jtag_tap_t));
  191. if (!pTap) {
  192. Jim_SetResult_sprintf(goi->interp, "no memory");
  193. return JIM_ERR;
  194. }
  195. /*
  196. * we expect CHIP + TAP + OPTIONS
  197. * */
  198. if (goi->argc < 3) {
  199. Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
  200. free(pTap);
  201. return JIM_ERR;
  202. }
  203. Jim_GetOpt_String(goi, &cp, NULL);
  204. pTap->chip = strdup(cp);
  205. Jim_GetOpt_String(goi, &cp, NULL);
  206. pTap->tapname = strdup(cp);
  207. /* name + dot + name + null */
  208. x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
  209. cp = malloc(x);
  210. sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
  211. pTap->dotted_name = cp;
  212. LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
  213. pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
  214. /* deal with options */
  215. #define NTREQ_IRLEN 1
  216. #define NTREQ_IRCAPTURE 2
  217. #define NTREQ_IRMASK 4
  218. /* clear them as we find them */
  219. reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
  220. while (goi->argc) {
  221. e = Jim_GetOpt_Nvp(goi, opts, &n);
  222. if (e != JIM_OK) {
  223. Jim_GetOpt_NvpUnknown(goi, opts, 0);
  224. free((void *)pTap->dotted_name);
  225. free(pTap);
  226. return e;
  227. }
  228. LOG_DEBUG("Processing option: %s", n->name);
  229. switch (n->value) {
  230. case NTAP_OPT_ENABLED:
  231. pTap->disabled_after_reset = false;
  232. break;
  233. case NTAP_OPT_DISABLED:
  234. pTap->disabled_after_reset = true;
  235. break;
  236. case NTAP_OPT_EXPECTED_ID:
  237. {
  238. uint32_t *new_expected_ids;
  239. e = Jim_GetOpt_Wide(goi, &w);
  240. if (e != JIM_OK) {
  241. Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
  242. free((void *)pTap->dotted_name);
  243. free(pTap);
  244. return e;
  245. }
  246. new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
  247. if (new_expected_ids == NULL) {
  248. Jim_SetResult_sprintf(goi->interp, "no memory");
  249. free((void *)pTap->dotted_name);
  250. free(pTap);
  251. return JIM_ERR;
  252. }
  253. memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
  254. new_expected_ids[pTap->expected_ids_cnt] = w;
  255. free(pTap->expected_ids);
  256. pTap->expected_ids = new_expected_ids;
  257. pTap->expected_ids_cnt++;
  258. break;
  259. }
  260. case NTAP_OPT_IRLEN:
  261. case NTAP_OPT_IRMASK:
  262. case NTAP_OPT_IRCAPTURE:
  263. e = Jim_GetOpt_Wide(goi, &w);
  264. if (e != JIM_OK) {
  265. Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
  266. free((void *)pTap->dotted_name);
  267. free(pTap);
  268. return e;
  269. }
  270. switch (n->value) {
  271. case NTAP_OPT_IRLEN:
  272. if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
  273. LOG_WARNING("huge IR length %d", (int) w);
  274. pTap->ir_length = w;
  275. reqbits &= (~(NTREQ_IRLEN));
  276. break;
  277. case NTAP_OPT_IRMASK:
  278. if (is_bad_irval(pTap->ir_length, w)) {
  279. LOG_ERROR("IR mask %x too big",
  280. (int) w);
  281. free((void *)pTap->dotted_name);
  282. free(pTap);
  283. return ERROR_FAIL;
  284. }
  285. pTap->ir_capture_mask = w;
  286. reqbits &= (~(NTREQ_IRMASK));
  287. break;
  288. case NTAP_OPT_IRCAPTURE:
  289. if (is_bad_irval(pTap->ir_length, w)) {
  290. LOG_ERROR("IR capture %x too big",
  291. (int) w);
  292. free((void *)pTap->dotted_name);
  293. free(pTap);
  294. return ERROR_FAIL;
  295. }
  296. pTap->ir_capture_value = w;
  297. reqbits &= (~(NTREQ_IRCAPTURE));
  298. break;
  299. }
  300. } /* switch (n->value) */
  301. } /* while (goi->argc) */
  302. /* default is enabled-after-reset */
  303. pTap->enabled = !pTap->disabled_after_reset;
  304. /* Did all the required option bits get cleared? */
  305. if (0 == reqbits)
  306. {
  307. jtag_tap_init(pTap);
  308. return ERROR_OK;
  309. }
  310. Jim_SetResult_sprintf(goi->interp,
  311. "newtap: %s missing required parameters",
  312. pTap->dotted_name);
  313. jtag_tap_free(pTap);
  314. return JIM_ERR;
  315. }
  316. static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e)
  317. {
  318. jtag_tap_event_action_t * jteap;
  319. int done;
  320. jteap = tap->event_action;
  321. done = 0;
  322. while (jteap) {
  323. if (jteap->event == e) {
  324. done = 1;
  325. LOG_DEBUG("JTAG tap: %s event: %d (%s) action: %s\n",
  326. tap->dotted_name,
  327. e,
  328. Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name,
  329. Jim_GetString(jteap->body, NULL));
  330. if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
  331. Jim_PrintErrorMessage(interp);
  332. } else {
  333. /* NOTE: we currently assume the handlers
  334. * can't fail. That presumes later code
  335. * will be verifying the scan chains ...
  336. */
  337. if (e == JTAG_TAP_EVENT_ENABLE)
  338. tap->enabled = true;
  339. }
  340. }
  341. jteap = jteap->next;
  342. }
  343. if (!done) {
  344. LOG_DEBUG("event %d %s - no action",
  345. e,
  346. Jim_Nvp_value2name_simple(nvp_jtag_tap_event, e)->name);
  347. }
  348. }
  349. static int jim_jtag_command(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  350. {
  351. Jim_GetOptInfo goi;
  352. int e;
  353. Jim_Nvp *n;
  354. Jim_Obj *o;
  355. struct command_context_s *context;
  356. enum {
  357. JTAG_CMD_INTERFACE,
  358. JTAG_CMD_INIT_RESET,
  359. JTAG_CMD_NEWTAP,
  360. JTAG_CMD_TAPENABLE,
  361. JTAG_CMD_TAPDISABLE,
  362. JTAG_CMD_TAPISENABLED,
  363. JTAG_CMD_CONFIGURE,
  364. JTAG_CMD_CGET,
  365. JTAG_CMD_NAMES,
  366. };
  367. const Jim_Nvp jtag_cmds[] = {
  368. { .name = "interface" , .value = JTAG_CMD_INTERFACE },
  369. { .name = "arp_init-reset", .value = JTAG_CMD_INIT_RESET },
  370. { .name = "newtap" , .value = JTAG_CMD_NEWTAP },
  371. { .name = "tapisenabled" , .value = JTAG_CMD_TAPISENABLED },
  372. { .name = "tapenable" , .value = JTAG_CMD_TAPENABLE },
  373. { .name = "tapdisable" , .value = JTAG_CMD_TAPDISABLE },
  374. { .name = "configure" , .value = JTAG_CMD_CONFIGURE },
  375. { .name = "cget" , .value = JTAG_CMD_CGET },
  376. { .name = "names" , .value = JTAG_CMD_NAMES },
  377. { .name = NULL, .value = -1 },
  378. };
  379. context = Jim_GetAssocData(interp, "context");
  380. /* go past the command */
  381. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  382. e = Jim_GetOpt_Nvp(&goi, jtag_cmds, &n);
  383. if (e != JIM_OK) {
  384. Jim_GetOpt_NvpUnknown(&goi, jtag_cmds, 0);
  385. return e;
  386. }
  387. Jim_SetEmptyResult(goi.interp);
  388. switch (n->value) {
  389. case JTAG_CMD_INTERFACE:
  390. /* return the name of the interface */
  391. /* TCL code might need to know the exact type... */
  392. /* FUTURE: we allow this as a means to "set" the interface. */
  393. if (goi.argc != 0) {
  394. Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
  395. return JIM_ERR;
  396. }
  397. Jim_SetResultString(goi.interp, jtag_interface->name, -1);
  398. return JIM_OK;
  399. case JTAG_CMD_INIT_RESET:
  400. if (goi.argc != 0) {
  401. Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
  402. return JIM_ERR;
  403. }
  404. e = jtag_init_reset(context);
  405. if (e != ERROR_OK) {
  406. Jim_SetResult_sprintf(goi.interp, "error: %d", e);
  407. return JIM_ERR;
  408. }
  409. return JIM_OK;
  410. case JTAG_CMD_NEWTAP:
  411. return jim_newtap_cmd(&goi);
  412. break;
  413. case JTAG_CMD_TAPISENABLED:
  414. case JTAG_CMD_TAPENABLE:
  415. case JTAG_CMD_TAPDISABLE:
  416. if (goi.argc != 1) {
  417. Jim_SetResultString(goi.interp, "Too many parameters",-1);
  418. return JIM_ERR;
  419. }
  420. {
  421. jtag_tap_t *t;
  422. t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
  423. if (t == NULL)
  424. return JIM_ERR;
  425. switch (n->value) {
  426. case JTAG_CMD_TAPISENABLED:
  427. break;
  428. case JTAG_CMD_TAPENABLE:
  429. if (t->enabled)
  430. break;
  431. jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
  432. if (!t->enabled)
  433. break;
  434. /* FIXME add JTAG sanity checks, w/o TLR
  435. * - scan chain length grew by one (this)
  436. * - IDs and IR lengths are as expected
  437. */
  438. jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
  439. break;
  440. case JTAG_CMD_TAPDISABLE:
  441. if (!t->enabled)
  442. break;
  443. jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
  444. if (t->enabled)
  445. break;
  446. /* FIXME add JTAG sanity checks, w/o TLR
  447. * - scan chain length shrank by one (this)
  448. * - IDs and IR lengths are as expected
  449. */
  450. jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
  451. break;
  452. }
  453. e = t->enabled;
  454. Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
  455. return JIM_OK;
  456. }
  457. break;
  458. case JTAG_CMD_CGET:
  459. if (goi.argc < 2) {
  460. Jim_WrongNumArgs(goi.interp, 0, NULL,
  461. "cget tap_name queryparm");
  462. return JIM_ERR;
  463. }
  464. {
  465. jtag_tap_t *t;
  466. Jim_GetOpt_Obj(&goi, &o);
  467. t = jtag_tap_by_jim_obj(goi.interp, o);
  468. if (t == NULL) {
  469. return JIM_ERR;
  470. }
  471. goi.isconfigure = 0;
  472. return jtag_tap_configure_cmd(&goi, t);
  473. }
  474. break;
  475. case JTAG_CMD_CONFIGURE:
  476. if (goi.argc < 3) {
  477. Jim_WrongNumArgs(goi.interp, 0, NULL,
  478. "configure tap_name attribute value ...");
  479. return JIM_ERR;
  480. }
  481. {
  482. jtag_tap_t *t;
  483. Jim_GetOpt_Obj(&goi, &o);
  484. t = jtag_tap_by_jim_obj(goi.interp, o);
  485. if (t == NULL) {
  486. return JIM_ERR;
  487. }
  488. goi.isconfigure = 1;
  489. return jtag_tap_configure_cmd(&goi, t);
  490. }
  491. break;
  492. case JTAG_CMD_NAMES:
  493. if (goi.argc != 0) {
  494. Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
  495. return JIM_ERR;
  496. }
  497. Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
  498. {
  499. jtag_tap_t *tap;
  500. for (tap = jtag_all_taps(); tap; tap = tap->next_tap) {
  501. Jim_ListAppendElement(goi.interp,
  502. Jim_GetResult(goi.interp),
  503. Jim_NewStringObj(goi.interp,
  504. tap->dotted_name, -1));
  505. }
  506. return JIM_OK;
  507. }
  508. break;
  509. }
  510. return JIM_ERR;
  511. }
  512. void jtag_notify_reset(void)
  513. {
  514. jtag_tap_t *tap;
  515. for (tap = jtag_all_taps(); tap; tap = tap->next_tap)
  516. {
  517. jtag_tap_handle_event(tap, JTAG_TAP_EVENT_POST_RESET);
  518. }
  519. }
  520. int jtag_register_commands(struct command_context_s *cmd_ctx)
  521. {
  522. register_jim(cmd_ctx, "jtag", jim_jtag_command, "perform jtag tap actions");
  523. register_command(cmd_ctx, NULL, "interface", handle_interface_command,
  524. COMMAND_CONFIG, "try to configure interface");
  525. register_command(cmd_ctx, NULL,
  526. "interface_list", &handle_interface_list_command,
  527. COMMAND_ANY, "list all built-in interfaces");
  528. register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
  529. COMMAND_ANY, "(DEPRECATED) set jtag speed (if supported)");
  530. register_command(cmd_ctx, NULL, "jtag_khz", handle_jtag_khz_command,
  531. COMMAND_ANY, "set maximum jtag speed (if supported); "
  532. "parameter is maximum khz, or 0 for adaptive clocking (RTCK).");
  533. register_command(cmd_ctx, NULL, "jtag_rclk", handle_jtag_rclk_command,
  534. COMMAND_ANY, "fallback_speed_khz - set JTAG speed to RCLK or use fallback speed");
  535. register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
  536. COMMAND_CONFIG, "(DEPRECATED) jtag_device <ir_length> <ir_expected> <ir_mask>");
  537. register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
  538. COMMAND_ANY,
  539. "[none/trst_only/srst_only/trst_and_srst] [srst_pulls_trst/trst_pulls_srst] [combined/separate] [trst_push_pull/trst_open_drain] [srst_push_pull/srst_open_drain]");
  540. register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,
  541. COMMAND_ANY, "jtag_nsrst_delay <ms> - delay after deasserting srst in ms");
  542. register_command(cmd_ctx, NULL, "jtag_ntrst_delay", handle_jtag_ntrst_delay_command,
  543. COMMAND_ANY, "jtag_ntrst_delay <ms> - delay after deasserting trst in ms");
  544. register_command(cmd_ctx, NULL, "scan_chain", handle_scan_chain_command,
  545. COMMAND_EXEC, "print current scan chain configuration");
  546. register_command(cmd_ctx, NULL, "jtag_reset", handle_jtag_reset_command,
  547. COMMAND_EXEC, "toggle reset lines <trst> <srst>");
  548. register_command(cmd_ctx, NULL, "runtest", handle_runtest_command,
  549. COMMAND_EXEC, "move to Run-Test/Idle, and execute <num_cycles>");
  550. register_command(cmd_ctx, NULL, "irscan", handle_irscan_command,
  551. COMMAND_EXEC, "execute IR scan <device> <instr> [dev2] [instr2] ...");
  552. register_jim(cmd_ctx, "drscan", Jim_Command_drscan, "execute DR scan <device> <num_bits> <value> <num_bits1> <value2> ...");
  553. register_jim(cmd_ctx, "flush_count", Jim_Command_flush_count, "returns number of times the JTAG queue has been flushed");
  554. register_jim(cmd_ctx, "pathmove", Jim_Command_pathmove, "move JTAG to state1 then to state2, state3, etc. <state1>,<state2>,<stat3>...");
  555. register_command(cmd_ctx, NULL, "verify_ircapture", handle_verify_ircapture_command,
  556. COMMAND_ANY, "verify value captured during Capture-IR <enable | disable>");
  557. register_command(cmd_ctx, NULL, "verify_jtag", handle_verify_jtag_command,
  558. COMMAND_ANY, "verify value capture <enable | disable>");
  559. register_command(cmd_ctx, NULL, "tms_sequence", handle_tms_sequence_command,
  560. COMMAND_ANY, "choose short(default) or long tms_sequence <short | long>");
  561. return ERROR_OK;
  562. }
  563. static int default_khz(int khz, int *jtag_speed)
  564. {
  565. LOG_ERROR("Translation from khz to jtag_speed not implemented");
  566. return ERROR_FAIL;
  567. }
  568. static int default_speed_div(int speed, int *khz)
  569. {
  570. LOG_ERROR("Translation from jtag_speed to khz not implemented");
  571. return ERROR_FAIL;
  572. }
  573. static int default_power_dropout(int *dropout)
  574. {
  575. *dropout = 0; /* by default we can't detect power dropout */
  576. return ERROR_OK;
  577. }
  578. static int default_srst_asserted(int *srst_asserted)
  579. {
  580. *srst_asserted = 0; /* by default we can't detect srst asserted */
  581. return ERROR_OK;
  582. }
  583. static int handle_interface_command(struct command_context_s *cmd_ctx,
  584. char *cmd, char **args, int argc)
  585. {
  586. /* check whether the interface is already configured */
  587. if (jtag_interface)
  588. {
  589. LOG_WARNING("Interface already configured, ignoring");
  590. return ERROR_OK;
  591. }
  592. /* interface name is a mandatory argument */
  593. if (argc != 1 || args[0][0] == '\0')
  594. return ERROR_COMMAND_SYNTAX_ERROR;
  595. for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
  596. {
  597. if (strcmp(args[0], jtag_interfaces[i]->name) != 0)
  598. continue;
  599. int retval = jtag_interfaces[i]->register_commands(cmd_ctx);
  600. if (ERROR_OK != retval)
  601. return retval;
  602. jtag_interface = jtag_interfaces[i];
  603. if (jtag_interface->khz == NULL)
  604. jtag_interface->khz = default_khz;
  605. if (jtag_interface->speed_div == NULL)
  606. jtag_interface->speed_div = default_speed_div;
  607. if (jtag_interface->power_dropout == NULL)
  608. jtag_interface->power_dropout = default_power_dropout;
  609. if (jtag_interface->srst_asserted == NULL)
  610. jtag_interface->srst_asserted = default_srst_asserted;
  611. return ERROR_OK;
  612. }
  613. /* no valid interface was found (i.e. the configuration option,
  614. * didn't match one of the compiled-in interfaces
  615. */
  616. LOG_ERROR("The specified JTAG interface was not found (%s)", args[0]);
  617. handle_interface_list_command(cmd_ctx, cmd, args, argc);
  618. return ERROR_JTAG_INVALID_INTERFACE;
  619. }
  620. static int handle_interface_list_command(struct command_context_s *cmd_ctx,
  621. char *cmd, char **args, int argc)
  622. {
  623. if (strcmp(cmd, "interface_list") == 0 && argc > 0)
  624. return ERROR_COMMAND_SYNTAX_ERROR;
  625. command_print(cmd_ctx, "The following JTAG interfaces are available:");
  626. for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
  627. {
  628. const char *name = jtag_interfaces[i]->name;
  629. command_print(cmd_ctx, "%u: %s", i + 1, name);
  630. }
  631. return ERROR_OK;
  632. }
  633. static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  634. {
  635. int e;
  636. char buf[1024];
  637. Jim_Obj *newargs[ 10 ];
  638. /*
  639. * CONVERT SYNTAX
  640. * argv[-1] = command
  641. * argv[ 0] = ir length
  642. * argv[ 1] = ir capture
  643. * argv[ 2] = ir mask
  644. * argv[ 3] = not actually used by anything but in the docs
  645. */
  646. if (argc < 4) {
  647. command_print(cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
  648. return ERROR_OK;
  649. }
  650. command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - translating to new syntax");
  651. command_print(cmd_ctx, "jtag newtap CHIP TAP -irlen %s -ircapture %s -irvalue %s",
  652. args[0],
  653. args[1],
  654. args[2]);
  655. command_print(cmd_ctx, "Example: STM32 has 2 taps, the cortexM3(len4) + boundaryscan(len5)");
  656. command_print(cmd_ctx, "jtag newtap stm32 cortexm3 ....., thus creating the tap: \"stm32.cortexm3\"");
  657. command_print(cmd_ctx, "jtag newtap stm32 boundary ....., and the tap: \"stm32.boundary\"");
  658. command_print(cmd_ctx, "And then refer to the taps by the dotted name.");
  659. newargs[0] = Jim_NewStringObj(interp, "jtag", -1);
  660. newargs[1] = Jim_NewStringObj(interp, "newtap", -1);
  661. sprintf(buf, "chip%d", jtag_tap_count());
  662. newargs[2] = Jim_NewStringObj(interp, buf, -1);
  663. sprintf(buf, "tap%d", jtag_tap_count());
  664. newargs[3] = Jim_NewStringObj(interp, buf, -1);
  665. newargs[4] = Jim_NewStringObj(interp, "-irlen", -1);
  666. newargs[5] = Jim_NewStringObj(interp, args[0], -1);
  667. newargs[6] = Jim_NewStringObj(interp, "-ircapture", -1);
  668. newargs[7] = Jim_NewStringObj(interp, args[1], -1);
  669. newargs[8] = Jim_NewStringObj(interp, "-irmask", -1);
  670. newargs[9] = Jim_NewStringObj(interp, args[2], -1);
  671. command_print(cmd_ctx, "NEW COMMAND:");
  672. sprintf(buf, "%s %s %s %s %s %s %s %s %s %s",
  673. Jim_GetString(newargs[0], NULL),
  674. Jim_GetString(newargs[1], NULL),
  675. Jim_GetString(newargs[2], NULL),
  676. Jim_GetString(newargs[3], NULL),
  677. Jim_GetString(newargs[4], NULL),
  678. Jim_GetString(newargs[5], NULL),
  679. Jim_GetString(newargs[6], NULL),
  680. Jim_GetString(newargs[7], NULL),
  681. Jim_GetString(newargs[8], NULL),
  682. Jim_GetString(newargs[9], NULL));
  683. e = jim_jtag_command(interp, 10, newargs);
  684. if (e != JIM_OK) {
  685. command_print(cmd_ctx, "%s", Jim_GetString(Jim_GetResult(interp), NULL));
  686. }
  687. return e;
  688. }
  689. static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  690. {
  691. jtag_tap_t *tap;
  692. tap = jtag_all_taps();
  693. command_print(cmd_ctx, " TapName | Enabled | IdCode Expected IrLen IrCap IrMask Instr ");
  694. command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
  695. while (tap) {
  696. uint32_t expected, expected_mask, cur_instr, ii;
  697. expected = buf_get_u32(tap->expected, 0, tap->ir_length);
  698. expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
  699. cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
  700. command_print(cmd_ctx,
  701. "%2d | %-18s | %c | 0x%08x | 0x%08x | 0x%02x | 0x%02x | 0x%02x | 0x%02x",
  702. tap->abs_chain_position,
  703. tap->dotted_name,
  704. tap->enabled ? 'Y' : 'n',
  705. (unsigned int)(tap->idcode),
  706. (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
  707. (unsigned int)(tap->ir_length),
  708. (unsigned int)(expected),
  709. (unsigned int)(expected_mask),
  710. (unsigned int)(cur_instr));
  711. for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
  712. command_print(cmd_ctx, " | | | | 0x%08x | | | | ",
  713. (unsigned int)(tap->expected_ids[ii]));
  714. }
  715. tap = tap->next_tap;
  716. }
  717. return ERROR_OK;
  718. }
  719. static int handle_reset_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  720. {
  721. int new_cfg = 0;
  722. int mask = 0;
  723. if (argc < 1)
  724. return ERROR_COMMAND_SYNTAX_ERROR;
  725. /* Original versions cared about the order of these tokens:
  726. * reset_config signals [combination [trst_type [srst_type]]]
  727. * They also clobbered the previous configuration even on error.
  728. *
  729. * Here we don't care about the order, and only change values
  730. * which have been explicitly specified.
  731. */
  732. for (; argc; argc--, args++) {
  733. int tmp = 0;
  734. int m;
  735. /* signals */
  736. m = RESET_HAS_TRST | RESET_HAS_SRST;
  737. if (strcmp(*args, "none") == 0)
  738. tmp = RESET_NONE;
  739. else if (strcmp(*args, "trst_only") == 0)
  740. tmp = RESET_HAS_TRST;
  741. else if (strcmp(*args, "srst_only") == 0)
  742. tmp = RESET_HAS_SRST;
  743. else if (strcmp(*args, "trst_and_srst") == 0)
  744. tmp = RESET_HAS_TRST | RESET_HAS_SRST;
  745. else
  746. m = 0;
  747. if (mask & m) {
  748. LOG_ERROR("extra reset_config %s spec (%s)",
  749. "signal", *args);
  750. return ERROR_INVALID_ARGUMENTS;
  751. }
  752. if (m)
  753. goto next;
  754. /* combination (options for broken wiring) */
  755. m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  756. if (strcmp(*args, "separate") == 0)
  757. /* separate reset lines - default */;
  758. else if (strcmp(*args, "srst_pulls_trst") == 0)
  759. tmp |= RESET_SRST_PULLS_TRST;
  760. else if (strcmp(*args, "trst_pulls_srst") == 0)
  761. tmp |= RESET_TRST_PULLS_SRST;
  762. else if (strcmp(*args, "combined") == 0)
  763. tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  764. else
  765. m = 0;
  766. if (mask & m) {
  767. LOG_ERROR("extra reset_config %s spec (%s)",
  768. "combination", *args);
  769. return ERROR_INVALID_ARGUMENTS;
  770. }
  771. if (m)
  772. goto next;
  773. /* trst_type (NOP without HAS_TRST) */
  774. m = RESET_TRST_OPEN_DRAIN;
  775. if (strcmp(*args, "trst_open_drain") == 0)
  776. tmp |= RESET_TRST_OPEN_DRAIN;
  777. else if (strcmp(*args, "trst_push_pull") == 0)
  778. /* push/pull from adapter - default */;
  779. else
  780. m = 0;
  781. if (mask & m) {
  782. LOG_ERROR("extra reset_config %s spec (%s)",
  783. "trst_type", *args);
  784. return ERROR_INVALID_ARGUMENTS;
  785. }
  786. if (m)
  787. goto next;
  788. /* srst_type (NOP without HAS_SRST) */
  789. m |= RESET_SRST_PUSH_PULL;
  790. if (strcmp(*args, "srst_push_pull") == 0)
  791. tmp |= RESET_SRST_PUSH_PULL;
  792. else if (strcmp(*args, "srst_open_drain") == 0)
  793. /* open drain from adapter - default */;
  794. else
  795. m = 0;
  796. if (mask & m) {
  797. LOG_ERROR("extra reset_config %s spec (%s)",
  798. "srst_type", *args);
  799. return ERROR_INVALID_ARGUMENTS;
  800. }
  801. if (m)
  802. goto next;
  803. /* caller provided nonsense; fail */
  804. LOG_ERROR("unknown reset_config flag (%s)", *args);
  805. return ERROR_INVALID_ARGUMENTS;
  806. next:
  807. /* Remember the bits which were specified (mask)
  808. * and their new values (new_cfg).
  809. */
  810. mask |= m;
  811. new_cfg |= tmp;
  812. }
  813. /* clear previous values of those bits, save new values */
  814. enum reset_types old_cfg = jtag_get_reset_config();
  815. old_cfg &= ~mask;
  816. new_cfg |= old_cfg;
  817. jtag_set_reset_config(new_cfg);
  818. return ERROR_OK;
  819. }
  820. static int handle_jtag_nsrst_delay_command(struct command_context_s *cmd_ctx,
  821. char *cmd, char **args, int argc)
  822. {
  823. if (argc > 1)
  824. return ERROR_COMMAND_SYNTAX_ERROR;
  825. if (argc == 1)
  826. {
  827. unsigned delay;
  828. int retval = parse_uint(args[0], &delay);
  829. if (ERROR_OK != retval)
  830. return retval;
  831. jtag_set_nsrst_delay(delay);
  832. }
  833. command_print(cmd_ctx, "jtag_nsrst_delay: %u", jtag_get_nsrst_delay());
  834. return ERROR_OK;
  835. }
  836. static int handle_jtag_ntrst_delay_command(struct command_context_s *cmd_ctx,
  837. char *cmd, char **args, int argc)
  838. {
  839. if (argc > 1)
  840. return ERROR_COMMAND_SYNTAX_ERROR;
  841. if (argc == 1)
  842. {
  843. unsigned delay;
  844. int retval = parse_uint(args[0], &delay);
  845. if (ERROR_OK != retval)
  846. return retval;
  847. jtag_set_ntrst_delay(delay);
  848. }
  849. command_print(cmd_ctx, "jtag_ntrst_delay: %u", jtag_get_ntrst_delay());
  850. return ERROR_OK;
  851. }
  852. static int handle_jtag_speed_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  853. {
  854. int retval = ERROR_OK;
  855. command_print(cmd_ctx, "OLD SYNTAX: DEPRECATED - "
  856. "use jtag_khz, not jtag_speed");
  857. if (argc > 1)
  858. return ERROR_COMMAND_SYNTAX_ERROR;
  859. if (argc == 1)
  860. {
  861. LOG_DEBUG("handle jtag speed");
  862. unsigned cur_speed = 0;
  863. int retval = parse_uint(args[0], &cur_speed);
  864. if (ERROR_OK != retval)
  865. return retval;
  866. retval = jtag_config_speed(cur_speed);
  867. }
  868. command_print(cmd_ctx, "jtag_speed: %d", jtag_get_speed());
  869. return retval;
  870. }
  871. static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  872. {
  873. if (argc > 1)
  874. return ERROR_COMMAND_SYNTAX_ERROR;
  875. int retval = ERROR_OK;
  876. if (argc == 1)
  877. {
  878. unsigned khz = 0;
  879. int retval = parse_uint(args[0], &khz);
  880. if (ERROR_OK != retval)
  881. return retval;
  882. retval = jtag_config_khz(khz);
  883. if (ERROR_OK != retval)
  884. return retval;
  885. }
  886. int cur_speed = jtag_get_speed_khz();
  887. retval = jtag_get_speed_readable(&cur_speed);
  888. if (ERROR_OK != retval)
  889. return retval;
  890. if (cur_speed)
  891. command_print(cmd_ctx, "%d kHz", cur_speed);
  892. else
  893. command_print(cmd_ctx, "RCLK - adaptive");
  894. return retval;
  895. }
  896. static int handle_jtag_rclk_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  897. {
  898. if (argc > 1)
  899. return ERROR_COMMAND_SYNTAX_ERROR;
  900. int retval = ERROR_OK;
  901. if (argc == 1)
  902. {
  903. unsigned khz = 0;
  904. int retval = parse_uint(args[0], &khz);
  905. if (ERROR_OK != retval)
  906. return retval;
  907. retval = jtag_config_rclk(khz);
  908. if (ERROR_OK != retval)
  909. return retval;
  910. }
  911. int cur_khz = jtag_get_speed_khz();
  912. retval = jtag_get_speed_readable(&cur_khz);
  913. if (ERROR_OK != retval)
  914. return retval;
  915. if (cur_khz)
  916. command_print(cmd_ctx, "RCLK not supported - fallback to %d kHz", cur_khz);
  917. else
  918. command_print(cmd_ctx, "RCLK - adaptive");
  919. return retval;
  920. }
  921. static int handle_jtag_reset_command(struct command_context_s *cmd_ctx,
  922. char *cmd, char **args, int argc)
  923. {
  924. if (argc != 2)
  925. return ERROR_COMMAND_SYNTAX_ERROR;
  926. int trst = -1;
  927. if (args[0][0] == '1')
  928. trst = 1;
  929. else if (args[0][0] == '0')
  930. trst = 0;
  931. else
  932. return ERROR_COMMAND_SYNTAX_ERROR;
  933. int srst = -1;
  934. if (args[1][0] == '1')
  935. srst = 1;
  936. else if (args[1][0] == '0')
  937. srst = 0;
  938. else
  939. return ERROR_COMMAND_SYNTAX_ERROR;
  940. if (jtag_interface_init(cmd_ctx) != ERROR_OK)
  941. return ERROR_JTAG_INIT_FAILED;
  942. jtag_add_reset(trst, srst);
  943. return jtag_execute_queue();
  944. }
  945. static int handle_runtest_command(struct command_context_s *cmd_ctx,
  946. char *cmd, char **args, int argc)
  947. {
  948. if (argc != 1)
  949. return ERROR_COMMAND_SYNTAX_ERROR;
  950. unsigned num_clocks;
  951. int retval = parse_uint(args[0], &num_clocks);
  952. if (ERROR_OK != retval)
  953. return retval;
  954. jtag_add_runtest(num_clocks, TAP_IDLE);
  955. return jtag_execute_queue();
  956. }
  957. /*
  958. * For "irscan" or "drscan" commands, the "end" (really, "next") state
  959. * should be stable ... and *NOT* a shift state, otherwise free-running
  960. * jtag clocks could change the values latched by the update state.
  961. */
  962. static bool scan_is_safe(tap_state_t state)
  963. {
  964. switch (state)
  965. {
  966. case TAP_RESET:
  967. case TAP_IDLE:
  968. case TAP_DRPAUSE:
  969. case TAP_IRPAUSE:
  970. return true;
  971. default:
  972. return false;
  973. }
  974. }
  975. static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  976. {
  977. int i;
  978. scan_field_t *fields;
  979. jtag_tap_t *tap;
  980. tap_state_t endstate;
  981. if ((argc < 2) || (argc % 2))
  982. {
  983. return ERROR_COMMAND_SYNTAX_ERROR;
  984. }
  985. /* optional "-endstate" "statename" at the end of the arguments,
  986. * so that e.g. IRPAUSE can let us load the data register before
  987. * entering RUN/IDLE to execute the instruction we load here.
  988. */
  989. endstate = TAP_IDLE;
  990. if (argc >= 4) {
  991. /* have at least one pair of numbers. */
  992. /* is last pair the magic text? */
  993. if (0 == strcmp("-endstate", args[ argc - 2 ])) {
  994. const char *cpA;
  995. const char *cpS;
  996. cpA = args[ argc-1 ];
  997. for (endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++) {
  998. cpS = tap_state_name(endstate);
  999. if (0 == strcmp(cpA, cpS)) {
  1000. break;
  1001. }
  1002. }
  1003. if (endstate >= TAP_NUM_STATES) {
  1004. return ERROR_COMMAND_SYNTAX_ERROR;
  1005. } else {
  1006. if (!scan_is_safe(endstate))
  1007. LOG_WARNING("irscan with unsafe "
  1008. "endstate \"%s\"", cpA);
  1009. /* found - remove the last 2 args */
  1010. argc -= 2;
  1011. }
  1012. }
  1013. }
  1014. int num_fields = argc / 2;
  1015. size_t fields_len = sizeof(scan_field_t) * num_fields;
  1016. fields = malloc(fields_len);
  1017. memset(fields, 0, fields_len);
  1018. int retval;
  1019. for (i = 0; i < num_fields; i++)
  1020. {
  1021. tap = jtag_tap_by_string(args[i*2]);
  1022. if (tap == NULL)
  1023. {
  1024. int j;
  1025. for (j = 0; j < i; j++)
  1026. free(fields[j].out_value);
  1027. free(fields);
  1028. command_print(cmd_ctx, "Tap: %s unknown", args[i*2]);
  1029. return ERROR_FAIL;
  1030. }
  1031. int field_size = tap->ir_length;
  1032. fields[i].tap = tap;
  1033. fields[i].num_bits = field_size;
  1034. fields[i].out_value = malloc(CEIL(field_size, 8));
  1035. uint32_t value;
  1036. retval = parse_u32(args[i * 2 + 1], &value);
  1037. if (ERROR_OK != retval)
  1038. goto error_return;
  1039. buf_set_u32(fields[i].out_value, 0, field_size, value);
  1040. fields[i].in_value = NULL;
  1041. }
  1042. /* did we have an endstate? */
  1043. jtag_add_ir_scan(num_fields, fields, endstate);
  1044. retval = jtag_execute_queue();
  1045. error_return:
  1046. for (i = 0; i < num_fields; i++)
  1047. {
  1048. if (NULL != fields[i].out_value)
  1049. free(fields[i].out_value);
  1050. }
  1051. free (fields);
  1052. return retval;
  1053. }
  1054. static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  1055. {
  1056. int retval;
  1057. scan_field_t *fields;
  1058. int num_fields;
  1059. int field_count = 0;
  1060. int i, e;
  1061. jtag_tap_t *tap;
  1062. tap_state_t endstate;
  1063. /* args[1] = device
  1064. * args[2] = num_bits
  1065. * args[3] = hex string
  1066. * ... repeat num bits and hex string ...
  1067. *
  1068. * .. optionally:
  1069. * args[N-2] = "-endstate"
  1070. * args[N-1] = statename
  1071. */
  1072. if ((argc < 4) || ((argc % 2) != 0))
  1073. {
  1074. Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
  1075. return JIM_ERR;
  1076. }
  1077. endstate = TAP_IDLE;
  1078. script_debug(interp, "drscan", argc, args);
  1079. /* validate arguments as numbers */
  1080. e = JIM_OK;
  1081. for (i = 2; i < argc; i += 2)
  1082. {
  1083. long bits;
  1084. const char *cp;
  1085. e = Jim_GetLong(interp, args[i], &bits);
  1086. /* If valid - try next arg */
  1087. if (e == JIM_OK) {
  1088. continue;
  1089. }
  1090. /* Not valid.. are we at the end? */
  1091. if (((i + 2) != argc)) {
  1092. /* nope, then error */
  1093. return e;
  1094. }
  1095. /* it could be: "-endstate FOO"
  1096. * e.g. DRPAUSE so we can issue more instructions
  1097. * before entering RUN/IDLE and executing them.
  1098. */
  1099. /* get arg as a string. */
  1100. cp = Jim_GetString(args[i], NULL);
  1101. /* is it the magic? */
  1102. if (0 == strcmp("-endstate", cp)) {
  1103. /* is the statename valid? */
  1104. cp = Jim_GetString(args[i + 1], NULL);
  1105. /* see if it is a valid state name */
  1106. endstate = tap_state_by_name(cp);
  1107. if (endstate < 0) {
  1108. /* update the error message */
  1109. Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
  1110. } else {
  1111. if (!scan_is_safe(endstate))
  1112. LOG_WARNING("drscan with unsafe "
  1113. "endstate \"%s\"", cp);
  1114. /* valid - so clear the error */
  1115. e = JIM_OK;
  1116. /* and remove the last 2 args */
  1117. argc -= 2;
  1118. }
  1119. }
  1120. /* Still an error? */
  1121. if (e != JIM_OK) {
  1122. return e; /* too bad */
  1123. }
  1124. } /* validate args */
  1125. tap = jtag_tap_by_jim_obj(interp, args[1]);
  1126. if (tap == NULL) {
  1127. return JIM_ERR;
  1128. }
  1129. num_fields = (argc-2)/2;
  1130. fields = malloc(sizeof(scan_field_t) * num_fields);
  1131. for (i = 2; i < argc; i += 2)
  1132. {
  1133. long bits;
  1134. int len;
  1135. const char *str;
  1136. Jim_GetLong(interp, args[i], &bits);
  1137. str = Jim_GetString(args[i + 1], &len);
  1138. fields[field_count].tap = tap;
  1139. fields[field_count].num_bits = bits;
  1140. fields[field_count].out_value = malloc(CEIL(bits, 8));
  1141. str_to_buf(str, len, fields[field_count].out_value, bits, 0);
  1142. fields[field_count].in_value = fields[field_count].out_value;
  1143. field_count++;
  1144. }
  1145. jtag_add_dr_scan(num_fields, fields, endstate);
  1146. retval = jtag_execute_queue();
  1147. if (retval != ERROR_OK)
  1148. {
  1149. Jim_SetResultString(interp, "drscan: jtag execute failed",-1);
  1150. return JIM_ERR;
  1151. }
  1152. field_count = 0;
  1153. Jim_Obj *list = Jim_NewListObj(interp, NULL, 0);
  1154. for (i = 2; i < argc; i += 2)
  1155. {
  1156. long bits;
  1157. char *str;
  1158. Jim_GetLong(interp, args[i], &bits);
  1159. str = buf_to_str(fields[field_count].in_value, bits, 16);
  1160. free(fields[field_count].out_value);
  1161. Jim_ListAppendElement(interp, list, Jim_NewStringObj(interp, str, strlen(str)));
  1162. free(str);
  1163. field_count++;
  1164. }
  1165. Jim_SetResult(interp, list);
  1166. free(fields);
  1167. return JIM_OK;
  1168. }
  1169. static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  1170. {
  1171. tap_state_t states[8];
  1172. if ((argc < 2) || ((size_t)argc > (sizeof(states)/sizeof(*states) + 1)))
  1173. {
  1174. Jim_WrongNumArgs(interp, 1, args, "wrong arguments");
  1175. return JIM_ERR;
  1176. }
  1177. script_debug(interp, "pathmove", argc, args);
  1178. int i;
  1179. for (i = 0; i < argc-1; i++)
  1180. {
  1181. const char *cp;
  1182. cp = Jim_GetString(args[i + 1], NULL);
  1183. states[i] = tap_state_by_name(cp);
  1184. if (states[i] < 0)
  1185. {
  1186. /* update the error message */
  1187. Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp);
  1188. return JIM_ERR;
  1189. }
  1190. }
  1191. if ((jtag_add_statemove(states[0]) != ERROR_OK) || (jtag_execute_queue()!= ERROR_OK))
  1192. {
  1193. Jim_SetResultString(interp, "pathmove: jtag execute failed",-1);
  1194. return JIM_ERR;
  1195. }
  1196. jtag_add_pathmove(argc-2, states + 1);
  1197. if (jtag_execute_queue()!= ERROR_OK)
  1198. {
  1199. Jim_SetResultString(interp, "pathmove: failed",-1);
  1200. return JIM_ERR;
  1201. }
  1202. return JIM_OK;
  1203. }
  1204. static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
  1205. {
  1206. script_debug(interp, "flush_count", argc, args);
  1207. Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
  1208. return JIM_OK;
  1209. }
  1210. static int handle_verify_ircapture_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1211. {
  1212. if (argc > 1)
  1213. return ERROR_COMMAND_SYNTAX_ERROR;
  1214. if (argc == 1)
  1215. {
  1216. if (strcmp(args[0], "enable") == 0)
  1217. jtag_set_verify_capture_ir(true);
  1218. else if (strcmp(args[0], "disable") == 0)
  1219. jtag_set_verify_capture_ir(false);
  1220. else
  1221. return ERROR_COMMAND_SYNTAX_ERROR;
  1222. }
  1223. const char *status = jtag_will_verify_capture_ir() ? "enabled": "disabled";
  1224. command_print(cmd_ctx, "verify Capture-IR is %s", status);
  1225. return ERROR_OK;
  1226. }
  1227. static int handle_verify_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1228. {
  1229. if (argc > 1)
  1230. return ERROR_COMMAND_SYNTAX_ERROR;
  1231. if (argc == 1)
  1232. {
  1233. if (strcmp(args[0], "enable") == 0)
  1234. jtag_set_verify(true);
  1235. else if (strcmp(args[0], "disable") == 0)
  1236. jtag_set_verify(false);
  1237. else
  1238. return ERROR_COMMAND_SYNTAX_ERROR;
  1239. }
  1240. const char *status = jtag_will_verify() ? "enabled": "disabled";
  1241. command_print(cmd_ctx, "verify jtag capture is %s", status);
  1242. return ERROR_OK;
  1243. }
  1244. static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1245. {
  1246. if (argc > 1)
  1247. return ERROR_COMMAND_SYNTAX_ERROR;
  1248. if (argc == 1)
  1249. {
  1250. bool use_new_table;
  1251. if (strcmp(args[0], "short") == 0)
  1252. use_new_table = true;
  1253. else if (strcmp(args[0], "long") == 0)
  1254. use_new_table = false;
  1255. else
  1256. return ERROR_COMMAND_SYNTAX_ERROR;
  1257. tap_use_new_tms_table(use_new_table);
  1258. }
  1259. command_print(cmd_ctx, "tms sequence is %s",
  1260. tap_uses_new_tms_table() ? "short": "long");
  1261. return ERROR_OK;
  1262. }