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.
 
 
 
 
 
 

362 lines
9.1 KiB

  1. /***************************************************************************
  2. *
  3. * Copyright (C) 2010 by David Brownell
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the
  17. * Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. ***************************************************************************/
  20. /**
  21. * @file
  22. * Utilities to support ARM "Serial Wire Debug" (SWD), a low pin-count debug
  23. * link protocol used in cases where JTAG is not wanted. This is coupled to
  24. * recent versions of ARM's "CoreSight" debug framework. This specific code
  25. * is a transport level interface, with "target/arm_adi_v5.[hc]" code
  26. * understanding operation semantics, shared with the JTAG transport.
  27. *
  28. * Single-DAP support only.
  29. *
  30. * for details, see "ARM IHI 0031A"
  31. * ARM Debug Interface v5 Architecture Specification
  32. * especially section 5.3 for SWD protocol
  33. *
  34. * On many chips (most current Cortex-M3 parts) SWD is a run-time alternative
  35. * to JTAG. Boards may support one or both. There are also SWD-only chips,
  36. * (using SW-DP not SWJ-DP).
  37. *
  38. * Even boards that also support JTAG can benefit from SWD support, because
  39. * usually there's no way to access the SWO trace view mechanism in JTAG mode.
  40. * That is, trace access may require SWD support.
  41. *
  42. */
  43. #ifdef HAVE_CONFIG_H
  44. #include "config.h"
  45. #endif
  46. #include "arm.h"
  47. #include "arm_adi_v5.h"
  48. #include <helper/time_support.h>
  49. #include <transport/transport.h>
  50. #include <jtag/interface.h>
  51. #include <jtag/swd.h>
  52. static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
  53. uint32_t *data)
  54. {
  55. // REVISIT status return vs ack ...
  56. return swd->read_reg(swd_cmd(true, false, reg), data);
  57. }
  58. static int swd_queue_idcode_read(struct adiv5_dap *dap,
  59. uint8_t *ack, uint32_t *data)
  60. {
  61. int status = swd_queue_dp_read(dap, DP_IDCODE, data);
  62. if (status < 0)
  63. return status;
  64. *ack = status;
  65. // ??
  66. return ERROR_OK;
  67. }
  68. static int (swd_queue_dp_write)(struct adiv5_dap *dap, unsigned reg,
  69. uint32_t data)
  70. {
  71. // REVISIT status return vs ack ...
  72. return swd->write_reg(swd_cmd(false, false, reg), data);
  73. }
  74. static int (swd_queue_ap_read)(struct adiv5_dap *dap, unsigned reg,
  75. uint32_t *data)
  76. {
  77. // REVISIT APSEL ...
  78. // REVISIT status return ...
  79. return swd->read_reg(swd_cmd(true, true, reg), data);
  80. }
  81. static int (swd_queue_ap_write)(struct adiv5_dap *dap, unsigned reg,
  82. uint32_t data)
  83. {
  84. // REVISIT APSEL ...
  85. // REVISIT status return ...
  86. return swd->write_reg(swd_cmd(false, true, reg), data);
  87. }
  88. static int (swd_queue_ap_abort)(struct adiv5_dap *dap, uint8_t *ack)
  89. {
  90. return ERROR_FAIL;
  91. }
  92. /** Executes all queued DAP operations. */
  93. static int swd_run(struct adiv5_dap *dap)
  94. {
  95. /* for now the SWD interface hard-wires a zero-size queue. */
  96. /* FIXME but we still need to check and scrub
  97. * any hardware errors ...
  98. */
  99. return ERROR_OK;
  100. }
  101. const struct dap_ops swd_dap_ops = {
  102. .is_swd = true,
  103. .queue_idcode_read = swd_queue_idcode_read,
  104. .queue_dp_read = swd_queue_dp_read,
  105. .queue_dp_write = swd_queue_dp_write,
  106. .queue_ap_read = swd_queue_ap_read,
  107. .queue_ap_write = swd_queue_ap_write,
  108. .queue_ap_abort = swd_queue_ap_abort,
  109. .run = swd_run,
  110. };
  111. /*
  112. * This represents the bits which must be sent out on TMS/SWDIO to
  113. * switch a DAP implemented using an SWJ-DP module into SWD mode.
  114. * These bits are stored (and transmitted) LSB-first.
  115. *
  116. * See the DAP-Lite specification, section 2.2.5 for information
  117. * about making the debug link select SWD or JTAG. (Similar info
  118. * is in a few other ARM documents.)
  119. */
  120. static const uint8_t jtag2swd_bitseq[] = {
  121. /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
  122. * putting both JTAG and SWD logic into reset state.
  123. */
  124. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  125. /* Switching sequence enables SWD and disables JTAG
  126. * NOTE: bits in the DP's IDCODE may expose the need for
  127. * an old/obsolete/deprecated sequence (0xb6 0xed).
  128. */
  129. 0x9e, 0xe7,
  130. /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
  131. * putting both JTAG and SWD logic into reset state.
  132. */
  133. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  134. };
  135. /**
  136. * Put the debug link into SWD mode, if the target supports it.
  137. * The link's initial mode may be either JTAG (for example,
  138. * with SWJ-DP after reset) or SWD.
  139. *
  140. * @param target Enters SWD mode (if possible).
  141. *
  142. * Note that targets using the JTAG-DP do not support SWD, and that
  143. * some targets which could otherwise support it may have have been
  144. * configured to disable SWD signaling
  145. *
  146. * @return ERROR_OK or else a fault code.
  147. */
  148. int dap_to_swd(struct target *target)
  149. {
  150. struct arm *arm = target_to_arm(target);
  151. int retval;
  152. LOG_DEBUG("Enter SWD mode");
  153. /* REVISIT it's ugly to need to make calls to a "jtag"
  154. * subsystem if the link may not be in JTAG mode...
  155. */
  156. retval = jtag_add_tms_seq(8 * sizeof(jtag2swd_bitseq),
  157. jtag2swd_bitseq, TAP_INVALID);
  158. if (retval == ERROR_OK)
  159. retval = jtag_execute_queue();
  160. /* set up the DAP's ops vector for SWD mode. */
  161. arm->dap->ops = &swd_dap_ops;
  162. return retval;
  163. }
  164. COMMAND_HANDLER(handle_swd_wcr)
  165. {
  166. int retval;
  167. struct target *target = get_current_target(CMD_CTX);
  168. struct arm *arm = target_to_arm(target);
  169. struct adiv5_dap *dap = arm->dap;
  170. uint32_t wcr;
  171. unsigned trn, scale = 0;
  172. switch (CMD_ARGC) {
  173. /* no-args: just dump state */
  174. case 0:
  175. //retval = swd_queue_dp_read(dap, DP_WCR, &wcr);
  176. retval = dap_queue_dp_read(dap, DP_WCR, &wcr);
  177. if (retval == ERROR_OK)
  178. dap->ops->run(dap);
  179. if (retval != ERROR_OK) {
  180. LOG_ERROR("can't read WCR?");
  181. return retval;
  182. }
  183. command_print(CMD_CTX,
  184. "turnaround=%d, prescale=%d",
  185. WCR_TO_TRN(wcr),
  186. WCR_TO_PRESCALE(wcr));
  187. return ERROR_OK;
  188. case 2: /* TRN and prescale */
  189. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], scale);
  190. if (scale > 7) {
  191. LOG_ERROR("prescale %d is too big", scale);
  192. return ERROR_FAIL;
  193. }
  194. /* FALL THROUGH */
  195. case 1: /* TRN only */
  196. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], trn);
  197. if (trn < 1 || trn > 4) {
  198. LOG_ERROR("turnaround %d is invalid", trn);
  199. return ERROR_FAIL;
  200. }
  201. wcr = ((trn - 1) << 8) | scale;
  202. /* FIXME
  203. * write WCR ...
  204. * then, re-init adapter with new TRN
  205. */
  206. LOG_ERROR("can't yet modify WCR");
  207. return ERROR_FAIL;
  208. default: /* too many arguments */
  209. return ERROR_COMMAND_SYNTAX_ERROR;
  210. }
  211. }
  212. static const struct command_registration swd_commands[] = {
  213. {
  214. /*
  215. * Set up SWD and JTAG targets identically, unless/until
  216. * infrastructure improves ... meanwhile, ignore all
  217. * JTAG-specific stuff like IR length for SWD.
  218. *
  219. * REVISIT can we verify "just one SWD DAP" here/early?
  220. */
  221. .name = "newdap",
  222. .jim_handler = jim_jtag_newtap,
  223. .mode = COMMAND_CONFIG,
  224. .help = "declare a new SWD DAP"
  225. },
  226. {
  227. .name = "wcr",
  228. .handler = handle_swd_wcr,
  229. .mode = COMMAND_ANY,
  230. .help = "display or update DAP's WCR register",
  231. .usage = "turnaround (1..4), prescale (0..7)",
  232. },
  233. /* REVISIT -- add a command for SWV trace on/off */
  234. COMMAND_REGISTRATION_DONE
  235. };
  236. static const struct command_registration swd_handlers[] = {
  237. {
  238. .name = "swd",
  239. .mode = COMMAND_ANY,
  240. .help = "SWD command group",
  241. .chain = swd_commands,
  242. },
  243. COMMAND_REGISTRATION_DONE
  244. };
  245. static int swd_select(struct command_context *ctx)
  246. {
  247. struct target *target = get_current_target(ctx);
  248. int retval;
  249. retval = register_commands(ctx, NULL, swd_handlers);
  250. if (retval != ERROR_OK)
  251. return retval;
  252. /* be sure driver is in SWD mode; start
  253. * with hardware default TRN (1), it can be changed later
  254. */
  255. if (!swd || !swd->read_reg || !swd->write_reg || !swd->init) {
  256. LOG_DEBUG("no SWD driver?");
  257. return ERROR_FAIL;
  258. }
  259. retval = swd->init(1);
  260. if (retval != ERROR_OK) {
  261. LOG_DEBUG("can't init SWD driver");
  262. return retval;
  263. }
  264. /* force DAP into SWD mode (not JTAG) */
  265. retval = dap_to_swd(target);
  266. return retval;
  267. }
  268. static int swd_init(struct command_context *ctx)
  269. {
  270. struct target *target = get_current_target(ctx);
  271. struct arm *arm = target_to_arm(target);
  272. struct adiv5_dap *dap = arm->dap;
  273. uint32_t idcode;
  274. int status;
  275. /* FIXME validate transport config ... is the
  276. * configured DAP present (check IDCODE)?
  277. * Is *only* one DAP configured?
  278. *
  279. * MUST READ IDCODE
  280. */
  281. /* Note, debugport_init() does setup too */
  282. uint8_t ack;
  283. status = swd_queue_idcode_read(dap, &ack, &idcode);
  284. if (status == ERROR_OK)
  285. LOG_INFO("SWD IDCODE %#8.8x", idcode);
  286. return status;
  287. }
  288. static struct transport swd_transport = {
  289. .name = "swd",
  290. .select = swd_select,
  291. .init = swd_init,
  292. };
  293. static void swd_constructor(void) __attribute__((constructor));
  294. static void swd_constructor(void)
  295. {
  296. transport_register(&swd_transport);
  297. }
  298. /** Returns true if the current debug session
  299. * is using SWD as its transport.
  300. */
  301. bool transport_is_swd(void)
  302. {
  303. return get_current_transport() == &swd_transport;
  304. }