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.
 
 
 
 
 
 

646 lines
18 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007-2010 Ø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, see <http://www.gnu.org/licenses/>. *
  27. ***************************************************************************/
  28. #ifdef HAVE_CONFIG_H
  29. #include "config.h"
  30. #endif
  31. #include "jtag.h"
  32. #include "minidriver.h"
  33. #include "interface.h"
  34. #include "interfaces.h"
  35. #include <transport/transport.h>
  36. #include <jtag/drivers/jtag_usb_common.h>
  37. #ifdef HAVE_STRINGS_H
  38. #include <strings.h>
  39. #endif
  40. /**
  41. * @file
  42. * Holds support for configuring debug adapters from TCl scripts.
  43. */
  44. struct adapter_driver *adapter_driver;
  45. const char * const jtag_only[] = { "jtag", NULL };
  46. static int jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
  47. {
  48. Jim_GetOptInfo goi;
  49. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  50. /* return the name of the interface */
  51. /* TCL code might need to know the exact type... */
  52. /* FUTURE: we allow this as a means to "set" the interface. */
  53. if (goi.argc != 0) {
  54. Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
  55. return JIM_ERR;
  56. }
  57. const char *name = adapter_driver ? adapter_driver->name : NULL;
  58. Jim_SetResultString(goi.interp, name ? : "undefined", -1);
  59. return JIM_OK;
  60. }
  61. COMMAND_HANDLER(adapter_transports_command)
  62. {
  63. char **transports;
  64. int retval;
  65. retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
  66. if (retval != ERROR_OK)
  67. return retval;
  68. retval = allow_transports(CMD_CTX, (const char **)transports);
  69. if (retval != ERROR_OK) {
  70. for (unsigned i = 0; transports[i]; i++)
  71. free(transports[i]);
  72. free(transports);
  73. }
  74. return retval;
  75. }
  76. COMMAND_HANDLER(handle_adapter_list_command)
  77. {
  78. if (strcmp(CMD_NAME, "list") == 0 && CMD_ARGC > 0)
  79. return ERROR_COMMAND_SYNTAX_ERROR;
  80. command_print(CMD, "The following debug adapters are available:");
  81. for (unsigned i = 0; NULL != adapter_drivers[i]; i++) {
  82. const char *name = adapter_drivers[i]->name;
  83. command_print(CMD, "%u: %s", i + 1, name);
  84. }
  85. return ERROR_OK;
  86. }
  87. COMMAND_HANDLER(handle_adapter_driver_command)
  88. {
  89. int retval;
  90. /* check whether the interface is already configured */
  91. if (adapter_driver) {
  92. LOG_WARNING("Interface already configured, ignoring");
  93. return ERROR_OK;
  94. }
  95. /* interface name is a mandatory argument */
  96. if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
  97. return ERROR_COMMAND_SYNTAX_ERROR;
  98. for (unsigned i = 0; NULL != adapter_drivers[i]; i++) {
  99. if (strcmp(CMD_ARGV[0], adapter_drivers[i]->name) != 0)
  100. continue;
  101. if (NULL != adapter_drivers[i]->commands) {
  102. retval = register_commands(CMD_CTX, NULL,
  103. adapter_drivers[i]->commands);
  104. if (ERROR_OK != retval)
  105. return retval;
  106. }
  107. adapter_driver = adapter_drivers[i];
  108. return allow_transports(CMD_CTX, adapter_driver->transports);
  109. }
  110. /* no valid interface was found (i.e. the configuration option,
  111. * didn't match one of the compiled-in interfaces
  112. */
  113. LOG_ERROR("The specified debug interface was not found (%s)",
  114. CMD_ARGV[0]);
  115. CALL_COMMAND_HANDLER(handle_adapter_list_command);
  116. return ERROR_JTAG_INVALID_INTERFACE;
  117. }
  118. COMMAND_HANDLER(handle_reset_config_command)
  119. {
  120. int new_cfg = 0;
  121. int mask = 0;
  122. /* Original versions cared about the order of these tokens:
  123. * reset_config signals [combination [trst_type [srst_type]]]
  124. * They also clobbered the previous configuration even on error.
  125. *
  126. * Here we don't care about the order, and only change values
  127. * which have been explicitly specified.
  128. */
  129. for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
  130. int tmp = 0;
  131. int m;
  132. /* gating */
  133. m = RESET_SRST_NO_GATING;
  134. if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
  135. /* default: don't use JTAG while SRST asserted */;
  136. else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
  137. tmp = RESET_SRST_NO_GATING;
  138. else
  139. m = 0;
  140. if (mask & m) {
  141. LOG_ERROR("extra reset_config %s spec (%s)",
  142. "gating", *CMD_ARGV);
  143. return ERROR_COMMAND_SYNTAX_ERROR;
  144. }
  145. if (m)
  146. goto next;
  147. /* signals */
  148. m = RESET_HAS_TRST | RESET_HAS_SRST;
  149. if (strcmp(*CMD_ARGV, "none") == 0)
  150. tmp = RESET_NONE;
  151. else if (strcmp(*CMD_ARGV, "trst_only") == 0)
  152. tmp = RESET_HAS_TRST;
  153. else if (strcmp(*CMD_ARGV, "srst_only") == 0)
  154. tmp = RESET_HAS_SRST;
  155. else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
  156. tmp = RESET_HAS_TRST | RESET_HAS_SRST;
  157. else
  158. m = 0;
  159. if (mask & m) {
  160. LOG_ERROR("extra reset_config %s spec (%s)",
  161. "signal", *CMD_ARGV);
  162. return ERROR_COMMAND_SYNTAX_ERROR;
  163. }
  164. if (m)
  165. goto next;
  166. /* combination (options for broken wiring) */
  167. m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  168. if (strcmp(*CMD_ARGV, "separate") == 0)
  169. /* separate reset lines - default */;
  170. else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
  171. tmp |= RESET_SRST_PULLS_TRST;
  172. else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
  173. tmp |= RESET_TRST_PULLS_SRST;
  174. else if (strcmp(*CMD_ARGV, "combined") == 0)
  175. tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  176. else
  177. m = 0;
  178. if (mask & m) {
  179. LOG_ERROR("extra reset_config %s spec (%s)",
  180. "combination", *CMD_ARGV);
  181. return ERROR_COMMAND_SYNTAX_ERROR;
  182. }
  183. if (m)
  184. goto next;
  185. /* trst_type (NOP without HAS_TRST) */
  186. m = RESET_TRST_OPEN_DRAIN;
  187. if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
  188. tmp |= RESET_TRST_OPEN_DRAIN;
  189. else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
  190. /* push/pull from adapter - default */;
  191. else
  192. m = 0;
  193. if (mask & m) {
  194. LOG_ERROR("extra reset_config %s spec (%s)",
  195. "trst_type", *CMD_ARGV);
  196. return ERROR_COMMAND_SYNTAX_ERROR;
  197. }
  198. if (m)
  199. goto next;
  200. /* srst_type (NOP without HAS_SRST) */
  201. m = RESET_SRST_PUSH_PULL;
  202. if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
  203. tmp |= RESET_SRST_PUSH_PULL;
  204. else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
  205. /* open drain from adapter - default */;
  206. else
  207. m = 0;
  208. if (mask & m) {
  209. LOG_ERROR("extra reset_config %s spec (%s)",
  210. "srst_type", *CMD_ARGV);
  211. return ERROR_COMMAND_SYNTAX_ERROR;
  212. }
  213. if (m)
  214. goto next;
  215. /* connect_type - only valid when srst_nogate */
  216. m = RESET_CNCT_UNDER_SRST;
  217. if (strcmp(*CMD_ARGV, "connect_assert_srst") == 0)
  218. tmp |= RESET_CNCT_UNDER_SRST;
  219. else if (strcmp(*CMD_ARGV, "connect_deassert_srst") == 0)
  220. /* connect normally - default */;
  221. else
  222. m = 0;
  223. if (mask & m) {
  224. LOG_ERROR("extra reset_config %s spec (%s)",
  225. "connect_type", *CMD_ARGV);
  226. return ERROR_COMMAND_SYNTAX_ERROR;
  227. }
  228. if (m)
  229. goto next;
  230. /* caller provided nonsense; fail */
  231. LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
  232. return ERROR_COMMAND_SYNTAX_ERROR;
  233. next:
  234. /* Remember the bits which were specified (mask)
  235. * and their new values (new_cfg).
  236. */
  237. mask |= m;
  238. new_cfg |= tmp;
  239. }
  240. /* clear previous values of those bits, save new values */
  241. if (mask) {
  242. int old_cfg = jtag_get_reset_config();
  243. old_cfg &= ~mask;
  244. new_cfg |= old_cfg;
  245. jtag_set_reset_config(new_cfg);
  246. } else
  247. new_cfg = jtag_get_reset_config();
  248. /*
  249. * Display the (now-)current reset mode
  250. */
  251. char *modes[6];
  252. /* minimal JTAG has neither SRST nor TRST (so that's the default) */
  253. switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
  254. case RESET_HAS_SRST:
  255. modes[0] = "srst_only";
  256. break;
  257. case RESET_HAS_TRST:
  258. modes[0] = "trst_only";
  259. break;
  260. case RESET_TRST_AND_SRST:
  261. modes[0] = "trst_and_srst";
  262. break;
  263. default:
  264. modes[0] = "none";
  265. break;
  266. }
  267. /* normally SRST and TRST are decoupled; but bugs happen ... */
  268. switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
  269. case RESET_SRST_PULLS_TRST:
  270. modes[1] = "srst_pulls_trst";
  271. break;
  272. case RESET_TRST_PULLS_SRST:
  273. modes[1] = "trst_pulls_srst";
  274. break;
  275. case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
  276. modes[1] = "combined";
  277. break;
  278. default:
  279. modes[1] = "separate";
  280. break;
  281. }
  282. /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
  283. if (new_cfg & RESET_HAS_TRST) {
  284. if (new_cfg & RESET_TRST_OPEN_DRAIN)
  285. modes[3] = " trst_open_drain";
  286. else
  287. modes[3] = " trst_push_pull";
  288. } else
  289. modes[3] = "";
  290. /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
  291. if (new_cfg & RESET_HAS_SRST) {
  292. if (new_cfg & RESET_SRST_NO_GATING)
  293. modes[2] = " srst_nogate";
  294. else
  295. modes[2] = " srst_gates_jtag";
  296. if (new_cfg & RESET_SRST_PUSH_PULL)
  297. modes[4] = " srst_push_pull";
  298. else
  299. modes[4] = " srst_open_drain";
  300. if (new_cfg & RESET_CNCT_UNDER_SRST)
  301. modes[5] = " connect_assert_srst";
  302. else
  303. modes[5] = " connect_deassert_srst";
  304. } else {
  305. modes[2] = "";
  306. modes[4] = "";
  307. modes[5] = "";
  308. }
  309. command_print(CMD, "%s %s%s%s%s%s",
  310. modes[0], modes[1],
  311. modes[2], modes[3], modes[4], modes[5]);
  312. return ERROR_OK;
  313. }
  314. COMMAND_HANDLER(handle_adapter_srst_delay_command)
  315. {
  316. if (CMD_ARGC > 1)
  317. return ERROR_COMMAND_SYNTAX_ERROR;
  318. if (CMD_ARGC == 1) {
  319. unsigned delay;
  320. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
  321. jtag_set_nsrst_delay(delay);
  322. }
  323. command_print(CMD, "adapter srst delay: %u", jtag_get_nsrst_delay());
  324. return ERROR_OK;
  325. }
  326. COMMAND_HANDLER(handle_adapter_srst_pulse_width_command)
  327. {
  328. if (CMD_ARGC > 1)
  329. return ERROR_COMMAND_SYNTAX_ERROR;
  330. if (CMD_ARGC == 1) {
  331. unsigned width;
  332. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
  333. jtag_set_nsrst_assert_width(width);
  334. }
  335. command_print(CMD, "adapter srst pulse_width: %u", jtag_get_nsrst_assert_width());
  336. return ERROR_OK;
  337. }
  338. COMMAND_HANDLER(handle_adapter_speed_command)
  339. {
  340. if (CMD_ARGC > 1)
  341. return ERROR_COMMAND_SYNTAX_ERROR;
  342. int retval = ERROR_OK;
  343. if (CMD_ARGC == 1) {
  344. unsigned khz = 0;
  345. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
  346. retval = jtag_config_khz(khz);
  347. if (ERROR_OK != retval)
  348. return retval;
  349. }
  350. int cur_speed = jtag_get_speed_khz();
  351. retval = jtag_get_speed_readable(&cur_speed);
  352. if (ERROR_OK != retval)
  353. return retval;
  354. if (cur_speed)
  355. command_print(CMD, "adapter speed: %d kHz", cur_speed);
  356. else
  357. command_print(CMD, "adapter speed: RCLK - adaptive");
  358. return retval;
  359. }
  360. COMMAND_HANDLER(handle_adapter_reset_de_assert)
  361. {
  362. enum values {
  363. VALUE_UNDEFINED = -1,
  364. VALUE_DEASSERT = 0,
  365. VALUE_ASSERT = 1,
  366. };
  367. enum values value;
  368. enum values srst = VALUE_UNDEFINED;
  369. enum values trst = VALUE_UNDEFINED;
  370. enum reset_types jtag_reset_config = jtag_get_reset_config();
  371. char *signal;
  372. if (CMD_ARGC == 0) {
  373. if (transport_is_jtag()) {
  374. if (jtag_reset_config & RESET_HAS_TRST)
  375. signal = jtag_get_trst() ? "asserted" : "deasserted";
  376. else
  377. signal = "not present";
  378. command_print(CMD, "trst %s", signal);
  379. }
  380. if (jtag_reset_config & RESET_HAS_SRST)
  381. signal = jtag_get_srst() ? "asserted" : "deasserted";
  382. else
  383. signal = "not present";
  384. command_print(CMD, "srst %s", signal);
  385. return ERROR_OK;
  386. }
  387. if (CMD_ARGC != 1 && CMD_ARGC != 3)
  388. return ERROR_COMMAND_SYNTAX_ERROR;
  389. value = (strcmp(CMD_NAME, "assert") == 0) ? VALUE_ASSERT : VALUE_DEASSERT;
  390. if (strcmp(CMD_ARGV[0], "srst") == 0)
  391. srst = value;
  392. else if (strcmp(CMD_ARGV[0], "trst") == 0)
  393. trst = value;
  394. else
  395. return ERROR_COMMAND_SYNTAX_ERROR;
  396. if (CMD_ARGC == 3) {
  397. if (strcmp(CMD_ARGV[1], "assert") == 0)
  398. value = VALUE_ASSERT;
  399. else if (strcmp(CMD_ARGV[1], "deassert") == 0)
  400. value = VALUE_DEASSERT;
  401. else
  402. return ERROR_COMMAND_SYNTAX_ERROR;
  403. if (strcmp(CMD_ARGV[2], "srst") == 0 && srst == VALUE_UNDEFINED)
  404. srst = value;
  405. else if (strcmp(CMD_ARGV[2], "trst") == 0 && trst == VALUE_UNDEFINED)
  406. trst = value;
  407. else
  408. return ERROR_COMMAND_SYNTAX_ERROR;
  409. }
  410. if (trst == VALUE_UNDEFINED) {
  411. if (transport_is_jtag())
  412. trst = jtag_get_trst() ? VALUE_ASSERT : VALUE_DEASSERT;
  413. else
  414. trst = VALUE_DEASSERT; /* unused, safe value */
  415. }
  416. if (srst == VALUE_UNDEFINED) {
  417. if (jtag_reset_config & RESET_HAS_SRST)
  418. srst = jtag_get_srst() ? VALUE_ASSERT : VALUE_DEASSERT;
  419. else
  420. srst = VALUE_DEASSERT; /* unused, safe value */
  421. }
  422. if (trst == VALUE_ASSERT && !transport_is_jtag()) {
  423. LOG_ERROR("transport has no trst signal");
  424. return ERROR_FAIL;
  425. }
  426. if (srst == VALUE_ASSERT && !(jtag_reset_config & RESET_HAS_SRST)) {
  427. LOG_ERROR("adapter has no srst signal");
  428. return ERROR_FAIL;
  429. }
  430. return adapter_resets((trst == VALUE_DEASSERT) ? TRST_DEASSERT : TRST_ASSERT,
  431. (srst == VALUE_DEASSERT) ? SRST_DEASSERT : SRST_ASSERT);
  432. }
  433. #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
  434. COMMAND_HANDLER(handle_usb_location_command)
  435. {
  436. if (CMD_ARGC == 1)
  437. jtag_usb_set_location(CMD_ARGV[0]);
  438. command_print(CMD, "adapter usb location: %s", jtag_usb_get_location());
  439. return ERROR_OK;
  440. }
  441. #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
  442. static const struct command_registration adapter_usb_command_handlers[] = {
  443. #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
  444. {
  445. .name = "location",
  446. .handler = &handle_usb_location_command,
  447. .mode = COMMAND_CONFIG,
  448. .help = "display or set the USB bus location of the USB device",
  449. .usage = "[<bus>-port[.port]...]",
  450. },
  451. #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
  452. COMMAND_REGISTRATION_DONE
  453. };
  454. static const struct command_registration adapter_srst_command_handlers[] = {
  455. {
  456. .name = "delay",
  457. .handler = handle_adapter_srst_delay_command,
  458. .mode = COMMAND_ANY,
  459. .help = "delay after deasserting SRST in ms",
  460. .usage = "[milliseconds]",
  461. },
  462. {
  463. .name = "pulse_width",
  464. .handler = handle_adapter_srst_pulse_width_command,
  465. .mode = COMMAND_ANY,
  466. .help = "SRST assertion pulse width in ms",
  467. .usage = "[milliseconds]",
  468. },
  469. COMMAND_REGISTRATION_DONE
  470. };
  471. static const struct command_registration adapter_command_handlers[] = {
  472. {
  473. .name = "driver",
  474. .handler = handle_adapter_driver_command,
  475. .mode = COMMAND_CONFIG,
  476. .help = "Select a debug adapter driver",
  477. .usage = "driver_name",
  478. },
  479. {
  480. .name = "speed",
  481. .handler = handle_adapter_speed_command,
  482. .mode = COMMAND_ANY,
  483. .help = "With an argument, change to the specified maximum "
  484. "jtag speed. For JTAG, 0 KHz signifies adaptive "
  485. "clocking. "
  486. "With or without argument, display current setting.",
  487. .usage = "[khz]",
  488. },
  489. {
  490. .name = "list",
  491. .handler = handle_adapter_list_command,
  492. .mode = COMMAND_ANY,
  493. .help = "List all built-in debug adapter drivers",
  494. .usage = "",
  495. },
  496. {
  497. .name = "name",
  498. .mode = COMMAND_ANY,
  499. .jim_handler = jim_adapter_name,
  500. .help = "Returns the name of the currently "
  501. "selected adapter (driver)",
  502. },
  503. {
  504. .name = "srst",
  505. .mode = COMMAND_ANY,
  506. .help = "srst adapter command group",
  507. .usage = "",
  508. .chain = adapter_srst_command_handlers,
  509. },
  510. {
  511. .name = "transports",
  512. .handler = adapter_transports_command,
  513. .mode = COMMAND_CONFIG,
  514. .help = "Declare transports the adapter supports.",
  515. .usage = "transport ... ",
  516. },
  517. {
  518. .name = "usb",
  519. .mode = COMMAND_ANY,
  520. .help = "usb adapter command group",
  521. .usage = "",
  522. .chain = adapter_usb_command_handlers,
  523. },
  524. {
  525. .name = "assert",
  526. .handler = handle_adapter_reset_de_assert,
  527. .mode = COMMAND_EXEC,
  528. .help = "Controls SRST and TRST lines.",
  529. .usage = "|deassert [srst|trst [assert|deassert srst|trst]]",
  530. },
  531. {
  532. .name = "deassert",
  533. .handler = handle_adapter_reset_de_assert,
  534. .mode = COMMAND_EXEC,
  535. .help = "Controls SRST and TRST lines.",
  536. .usage = "|assert [srst|trst [deassert|assert srst|trst]]",
  537. },
  538. COMMAND_REGISTRATION_DONE
  539. };
  540. static const struct command_registration interface_command_handlers[] = {
  541. {
  542. .name = "adapter",
  543. .mode = COMMAND_ANY,
  544. .help = "adapter command group",
  545. .usage = "",
  546. .chain = adapter_command_handlers,
  547. },
  548. {
  549. .name = "reset_config",
  550. .handler = handle_reset_config_command,
  551. .mode = COMMAND_ANY,
  552. .help = "configure adapter reset behavior",
  553. .usage = "[none|trst_only|srst_only|trst_and_srst] "
  554. "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
  555. "[srst_gates_jtag|srst_nogate] "
  556. "[trst_push_pull|trst_open_drain] "
  557. "[srst_push_pull|srst_open_drain] "
  558. "[connect_deassert_srst|connect_assert_srst]",
  559. },
  560. COMMAND_REGISTRATION_DONE
  561. };
  562. /**
  563. * Register the commands which deal with arbitrary debug adapter drivers.
  564. *
  565. * @todo Remove internal assumptions that all debug adapters use JTAG for
  566. * transport. Various types and data structures are not named generically.
  567. */
  568. int interface_register_commands(struct command_context *ctx)
  569. {
  570. return register_commands(ctx, NULL, interface_command_handlers);
  571. }