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.
 
 
 
 
 
 

532 lines
14 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, 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. #include <transport/transport.h>
  38. #ifdef HAVE_STRINGS_H
  39. #include <strings.h>
  40. #endif
  41. /**
  42. * @file
  43. * Holds support for configuring debug adapters from TCl scripts.
  44. */
  45. extern struct jtag_interface *jtag_interface;
  46. static int
  47. jim_adapter_name(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  48. {
  49. Jim_GetOptInfo goi;
  50. Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
  51. /* return the name of the interface */
  52. /* TCL code might need to know the exact type... */
  53. /* FUTURE: we allow this as a means to "set" the interface. */
  54. if (goi.argc != 0) {
  55. Jim_WrongNumArgs(goi.interp, 1, goi.argv-1, "(no params)");
  56. return JIM_ERR;
  57. }
  58. const char *name = jtag_interface ? jtag_interface->name : NULL;
  59. Jim_SetResultString(goi.interp, name ? : "undefined", -1);
  60. return JIM_OK;
  61. }
  62. static int default_khz(int khz, int *jtag_speed)
  63. {
  64. LOG_ERROR("Translation from khz to jtag_speed not implemented");
  65. return ERROR_FAIL;
  66. }
  67. static int default_speed_div(int speed, int *khz)
  68. {
  69. LOG_ERROR("Translation from jtag_speed to khz not implemented");
  70. return ERROR_FAIL;
  71. }
  72. static int default_power_dropout(int *dropout)
  73. {
  74. *dropout = 0; /* by default we can't detect power dropout */
  75. return ERROR_OK;
  76. }
  77. static int default_srst_asserted(int *srst_asserted)
  78. {
  79. *srst_asserted = 0; /* by default we can't detect srst asserted */
  80. return ERROR_OK;
  81. }
  82. const char *jtag_only[] = { "jtag", NULL, };
  83. COMMAND_HANDLER(interface_transport_command)
  84. {
  85. char **transports;
  86. int retval;
  87. retval = CALL_COMMAND_HANDLER(transport_list_parse, &transports);
  88. if (retval != ERROR_OK) {
  89. return retval;
  90. }
  91. retval = allow_transports(CMD_CTX, (const char **)transports);
  92. if (retval != ERROR_OK) {
  93. for (unsigned i = 0; transports[i]; i++)
  94. free(transports[i]);
  95. free(transports);
  96. }
  97. return retval;
  98. }
  99. COMMAND_HANDLER(handle_interface_list_command)
  100. {
  101. if (strcmp(CMD_NAME, "interface_list") == 0 && CMD_ARGC > 0)
  102. return ERROR_COMMAND_SYNTAX_ERROR;
  103. command_print(CMD_CTX, "The following debug interfaces are available:");
  104. for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
  105. {
  106. const char *name = jtag_interfaces[i]->name;
  107. command_print(CMD_CTX, "%u: %s", i + 1, name);
  108. }
  109. return ERROR_OK;
  110. }
  111. COMMAND_HANDLER(handle_interface_command)
  112. {
  113. int retval;
  114. /* check whether the interface is already configured */
  115. if (jtag_interface)
  116. {
  117. LOG_WARNING("Interface already configured, ignoring");
  118. return ERROR_OK;
  119. }
  120. /* interface name is a mandatory argument */
  121. if (CMD_ARGC != 1 || CMD_ARGV[0][0] == '\0')
  122. return ERROR_COMMAND_SYNTAX_ERROR;
  123. for (unsigned i = 0; NULL != jtag_interfaces[i]; i++)
  124. {
  125. if (strcmp(CMD_ARGV[0], jtag_interfaces[i]->name) != 0)
  126. continue;
  127. if (NULL != jtag_interfaces[i]->commands)
  128. {
  129. retval = register_commands(CMD_CTX, NULL,
  130. jtag_interfaces[i]->commands);
  131. if (ERROR_OK != retval)
  132. return retval;
  133. }
  134. jtag_interface = jtag_interfaces[i];
  135. /* LEGACY SUPPORT ... adapter drivers must declare what
  136. * transports they allow. Until they all do so, assume
  137. * the legacy drivers are JTAG-only
  138. */
  139. if (!jtag_interface->transports)
  140. LOG_WARNING("Adapter driver '%s' did not declare "
  141. "which transports it allows; assuming "
  142. "legacy JTAG-only", jtag_interface->name);
  143. retval = allow_transports(CMD_CTX,
  144. jtag_interface->transports
  145. ? : jtag_only);
  146. if (ERROR_OK != retval)
  147. return retval;
  148. if (jtag_interface->khz == NULL)
  149. jtag_interface->khz = default_khz;
  150. if (jtag_interface->speed_div == NULL)
  151. jtag_interface->speed_div = default_speed_div;
  152. if (jtag_interface->power_dropout == NULL)
  153. jtag_interface->power_dropout = default_power_dropout;
  154. if (jtag_interface->srst_asserted == NULL)
  155. jtag_interface->srst_asserted = default_srst_asserted;
  156. return ERROR_OK;
  157. }
  158. /* no valid interface was found (i.e. the configuration option,
  159. * didn't match one of the compiled-in interfaces
  160. */
  161. LOG_ERROR("The specified debug interface was not found (%s)",
  162. CMD_ARGV[0]);
  163. CALL_COMMAND_HANDLER(handle_interface_list_command);
  164. return ERROR_JTAG_INVALID_INTERFACE;
  165. }
  166. COMMAND_HANDLER(handle_reset_config_command)
  167. {
  168. int new_cfg = 0;
  169. int mask = 0;
  170. /* Original versions cared about the order of these tokens:
  171. * reset_config signals [combination [trst_type [srst_type]]]
  172. * They also clobbered the previous configuration even on error.
  173. *
  174. * Here we don't care about the order, and only change values
  175. * which have been explicitly specified.
  176. */
  177. for (; CMD_ARGC; CMD_ARGC--, CMD_ARGV++) {
  178. int tmp = 0;
  179. int m;
  180. /* gating */
  181. m = RESET_SRST_NO_GATING;
  182. if (strcmp(*CMD_ARGV, "srst_gates_jtag") == 0)
  183. /* default: don't use JTAG while SRST asserted */;
  184. else if (strcmp(*CMD_ARGV, "srst_nogate") == 0)
  185. tmp = RESET_SRST_NO_GATING;
  186. else
  187. m = 0;
  188. if (mask & m) {
  189. LOG_ERROR("extra reset_config %s spec (%s)",
  190. "gating", *CMD_ARGV);
  191. return ERROR_INVALID_ARGUMENTS;
  192. }
  193. if (m)
  194. goto next;
  195. /* signals */
  196. m = RESET_HAS_TRST | RESET_HAS_SRST;
  197. if (strcmp(*CMD_ARGV, "none") == 0)
  198. tmp = RESET_NONE;
  199. else if (strcmp(*CMD_ARGV, "trst_only") == 0)
  200. tmp = RESET_HAS_TRST;
  201. else if (strcmp(*CMD_ARGV, "srst_only") == 0)
  202. tmp = RESET_HAS_SRST;
  203. else if (strcmp(*CMD_ARGV, "trst_and_srst") == 0)
  204. tmp = RESET_HAS_TRST | RESET_HAS_SRST;
  205. else
  206. m = 0;
  207. if (mask & m) {
  208. LOG_ERROR("extra reset_config %s spec (%s)",
  209. "signal", *CMD_ARGV);
  210. return ERROR_INVALID_ARGUMENTS;
  211. }
  212. if (m)
  213. goto next;
  214. /* combination (options for broken wiring) */
  215. m = RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  216. if (strcmp(*CMD_ARGV, "separate") == 0)
  217. /* separate reset lines - default */;
  218. else if (strcmp(*CMD_ARGV, "srst_pulls_trst") == 0)
  219. tmp |= RESET_SRST_PULLS_TRST;
  220. else if (strcmp(*CMD_ARGV, "trst_pulls_srst") == 0)
  221. tmp |= RESET_TRST_PULLS_SRST;
  222. else if (strcmp(*CMD_ARGV, "combined") == 0)
  223. tmp |= RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST;
  224. else
  225. m = 0;
  226. if (mask & m) {
  227. LOG_ERROR("extra reset_config %s spec (%s)",
  228. "combination", *CMD_ARGV);
  229. return ERROR_INVALID_ARGUMENTS;
  230. }
  231. if (m)
  232. goto next;
  233. /* trst_type (NOP without HAS_TRST) */
  234. m = RESET_TRST_OPEN_DRAIN;
  235. if (strcmp(*CMD_ARGV, "trst_open_drain") == 0)
  236. tmp |= RESET_TRST_OPEN_DRAIN;
  237. else if (strcmp(*CMD_ARGV, "trst_push_pull") == 0)
  238. /* push/pull from adapter - default */;
  239. else
  240. m = 0;
  241. if (mask & m) {
  242. LOG_ERROR("extra reset_config %s spec (%s)",
  243. "trst_type", *CMD_ARGV);
  244. return ERROR_INVALID_ARGUMENTS;
  245. }
  246. if (m)
  247. goto next;
  248. /* srst_type (NOP without HAS_SRST) */
  249. m |= RESET_SRST_PUSH_PULL;
  250. if (strcmp(*CMD_ARGV, "srst_push_pull") == 0)
  251. tmp |= RESET_SRST_PUSH_PULL;
  252. else if (strcmp(*CMD_ARGV, "srst_open_drain") == 0)
  253. /* open drain from adapter - default */;
  254. else
  255. m = 0;
  256. if (mask & m) {
  257. LOG_ERROR("extra reset_config %s spec (%s)",
  258. "srst_type", *CMD_ARGV);
  259. return ERROR_INVALID_ARGUMENTS;
  260. }
  261. if (m)
  262. goto next;
  263. /* caller provided nonsense; fail */
  264. LOG_ERROR("unknown reset_config flag (%s)", *CMD_ARGV);
  265. return ERROR_INVALID_ARGUMENTS;
  266. next:
  267. /* Remember the bits which were specified (mask)
  268. * and their new values (new_cfg).
  269. */
  270. mask |= m;
  271. new_cfg |= tmp;
  272. }
  273. /* clear previous values of those bits, save new values */
  274. if (mask) {
  275. int old_cfg = jtag_get_reset_config();
  276. old_cfg &= ~mask;
  277. new_cfg |= old_cfg;
  278. jtag_set_reset_config(new_cfg);
  279. } else
  280. new_cfg = jtag_get_reset_config();
  281. /*
  282. * Display the (now-)current reset mode
  283. */
  284. char *modes[5];
  285. /* minimal JTAG has neither SRST nor TRST (so that's the default) */
  286. switch (new_cfg & (RESET_HAS_TRST | RESET_HAS_SRST)) {
  287. case RESET_HAS_SRST:
  288. modes[0] = "srst_only";
  289. break;
  290. case RESET_HAS_TRST:
  291. modes[0] = "trst_only";
  292. break;
  293. case RESET_TRST_AND_SRST:
  294. modes[0] = "trst_and_srst";
  295. break;
  296. default:
  297. modes[0] = "none";
  298. break;
  299. }
  300. /* normally SRST and TRST are decoupled; but bugs happen ... */
  301. switch (new_cfg & (RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST)) {
  302. case RESET_SRST_PULLS_TRST:
  303. modes[1] = "srst_pulls_trst";
  304. break;
  305. case RESET_TRST_PULLS_SRST:
  306. modes[1] = "trst_pulls_srst";
  307. break;
  308. case RESET_SRST_PULLS_TRST | RESET_TRST_PULLS_SRST:
  309. modes[1] = "combined";
  310. break;
  311. default:
  312. modes[1] = "separate";
  313. break;
  314. }
  315. /* TRST-less connectors include Altera, Xilinx, and minimal JTAG */
  316. if (new_cfg & RESET_HAS_TRST) {
  317. if (new_cfg & RESET_TRST_OPEN_DRAIN)
  318. modes[3] = " trst_open_drain";
  319. else
  320. modes[3] = " trst_push_pull";
  321. } else
  322. modes[3] = "";
  323. /* SRST-less connectors include TI-14, Xilinx, and minimal JTAG */
  324. if (new_cfg & RESET_HAS_SRST) {
  325. if (new_cfg & RESET_SRST_NO_GATING)
  326. modes[2] = " srst_nogate";
  327. else
  328. modes[2] = " srst_gates_jtag";
  329. if (new_cfg & RESET_SRST_PUSH_PULL)
  330. modes[4] = " srst_push_pull";
  331. else
  332. modes[4] = " srst_open_drain";
  333. } else {
  334. modes[2] = "";
  335. modes[4] = "";
  336. }
  337. command_print(CMD_CTX, "%s %s%s%s%s",
  338. modes[0], modes[1],
  339. modes[2], modes[3], modes[4]);
  340. return ERROR_OK;
  341. }
  342. COMMAND_HANDLER(handle_adapter_nsrst_delay_command)
  343. {
  344. if (CMD_ARGC > 1)
  345. return ERROR_COMMAND_SYNTAX_ERROR;
  346. if (CMD_ARGC == 1)
  347. {
  348. unsigned delay;
  349. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], delay);
  350. jtag_set_nsrst_delay(delay);
  351. }
  352. command_print(CMD_CTX, "adapter_nsrst_delay: %u", jtag_get_nsrst_delay());
  353. return ERROR_OK;
  354. }
  355. COMMAND_HANDLER(handle_adapter_nsrst_assert_width_command)
  356. {
  357. if (CMD_ARGC > 1)
  358. return ERROR_COMMAND_SYNTAX_ERROR;
  359. if (CMD_ARGC == 1)
  360. {
  361. unsigned width;
  362. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], width);
  363. jtag_set_nsrst_assert_width(width);
  364. }
  365. command_print(CMD_CTX, "adapter_nsrst_assert_width: %u", jtag_get_nsrst_assert_width());
  366. return ERROR_OK;
  367. }
  368. COMMAND_HANDLER(handle_adapter_khz_command)
  369. {
  370. if (CMD_ARGC > 1)
  371. return ERROR_COMMAND_SYNTAX_ERROR;
  372. int retval = ERROR_OK;
  373. if (CMD_ARGC == 1)
  374. {
  375. unsigned khz = 0;
  376. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], khz);
  377. retval = jtag_config_khz(khz);
  378. if (ERROR_OK != retval)
  379. return retval;
  380. }
  381. int cur_speed = jtag_get_speed_khz();
  382. retval = jtag_get_speed_readable(&cur_speed);
  383. if (ERROR_OK != retval)
  384. return retval;
  385. if (cur_speed)
  386. command_print(CMD_CTX, "%d kHz", cur_speed);
  387. else
  388. command_print(CMD_CTX, "RCLK - adaptive");
  389. return retval;
  390. }
  391. static const struct command_registration interface_command_handlers[] = {
  392. {
  393. .name = "adapter_khz",
  394. .handler = handle_adapter_khz_command,
  395. .mode = COMMAND_ANY,
  396. .help = "With an argument, change to the specified maximum "
  397. "jtag speed. For JTAG, 0 KHz signifies adaptive "
  398. " clocking. "
  399. "With or without argument, display current setting.",
  400. .usage = "[khz]",
  401. },
  402. {
  403. .name = "adapter_name",
  404. .mode = COMMAND_ANY,
  405. .jim_handler = jim_adapter_name,
  406. .help = "Returns the name of the currently "
  407. "selected adapter (driver)",
  408. },
  409. {
  410. .name = "adapter_nsrst_delay",
  411. .handler = handle_adapter_nsrst_delay_command,
  412. .mode = COMMAND_ANY,
  413. .help = "delay after deasserting SRST in ms",
  414. .usage = "[milliseconds]",
  415. },
  416. {
  417. .name = "adapter_nsrst_assert_width",
  418. .handler = handle_adapter_nsrst_assert_width_command,
  419. .mode = COMMAND_ANY,
  420. .help = "delay after asserting SRST in ms",
  421. .usage = "[milliseconds]",
  422. },
  423. {
  424. .name = "interface",
  425. .handler = handle_interface_command,
  426. .mode = COMMAND_CONFIG,
  427. .help = "Select a debug adapter interface (driver)",
  428. .usage = "driver_name",
  429. },
  430. {
  431. .name = "interface_transports",
  432. .handler = interface_transport_command,
  433. .mode = COMMAND_CONFIG,
  434. .help = "Declare transports the interface supports.",
  435. .usage = "transport ... ",
  436. },
  437. {
  438. .name = "interface_list",
  439. .handler = handle_interface_list_command,
  440. .mode = COMMAND_ANY,
  441. .help = "List all built-in debug adapter interfaces (drivers)",
  442. },
  443. {
  444. .name = "reset_config",
  445. .handler = handle_reset_config_command,
  446. .mode = COMMAND_ANY,
  447. .help = "configure adapter reset behavior",
  448. .usage = "[none|trst_only|srst_only|trst_and_srst] "
  449. "[srst_pulls_trst|trst_pulls_srst|combined|separate] "
  450. "[srst_gates_jtag|srst_nogate] "
  451. "[trst_push_pull|trst_open_drain] "
  452. "[srst_push_pull|srst_open_drain]",
  453. },
  454. COMMAND_REGISTRATION_DONE
  455. };
  456. /**
  457. * Register the commands which deal with arbitrary debug adapter drivers.
  458. *
  459. * @todo Remove internal assumptions that all debug adapters use JTAG for
  460. * transport. Various types and data structures are not named generically.
  461. */
  462. int interface_register_commands(struct command_context *ctx)
  463. {
  464. return register_commands(ctx, NULL, interface_command_handlers);
  465. }