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.
 
 
 
 
 
 

441 lines
12 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  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. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "arm.h"
  24. #include "etm.h"
  25. #include "oocd_trace.h"
  26. /*
  27. * This is "proof of concept" code, for prototype hardware:
  28. * https://lists.berlios.de/pipermail/openocd-development/2007-September/000336.html
  29. */
  30. static int oocd_trace_read_reg(struct oocd_trace *oocd_trace, int reg, uint32_t *value)
  31. {
  32. size_t bytes_written, bytes_read, bytes_to_read;
  33. uint8_t cmd;
  34. cmd = 0x10 | (reg & 0x7);
  35. bytes_written = write(oocd_trace->tty_fd, &cmd, 1);
  36. bytes_to_read = 4;
  37. while (bytes_to_read > 0)
  38. {
  39. bytes_read = read(oocd_trace->tty_fd, ((uint8_t*)value) + 4 - bytes_to_read, bytes_to_read);
  40. bytes_to_read -= bytes_read;
  41. }
  42. LOG_DEBUG("reg #%i: 0x%8.8x\n", reg, *value);
  43. return ERROR_OK;
  44. }
  45. static int oocd_trace_write_reg(struct oocd_trace *oocd_trace, int reg, uint32_t value)
  46. {
  47. size_t bytes_written;
  48. uint8_t data[5];
  49. data[0] = 0x18 | (reg & 0x7);
  50. data[1] = value & 0xff;
  51. data[2] = (value & 0xff00) >> 8;
  52. data[3] = (value & 0xff0000) >> 16;
  53. data[4] = (value & 0xff000000) >> 24;
  54. bytes_written = write(oocd_trace->tty_fd, data, 5);
  55. LOG_DEBUG("reg #%i: 0x%8.8x\n", reg, value);
  56. return ERROR_OK;
  57. }
  58. static int oocd_trace_read_memory(struct oocd_trace *oocd_trace, uint8_t *data, uint32_t address, uint32_t size)
  59. {
  60. size_t bytes_written, bytes_to_read;
  61. ssize_t bytes_read;
  62. uint8_t cmd;
  63. oocd_trace_write_reg(oocd_trace, OOCD_TRACE_ADDRESS, address);
  64. oocd_trace_write_reg(oocd_trace, OOCD_TRACE_SDRAM_COUNTER, size);
  65. cmd = 0x20;
  66. bytes_written = write(oocd_trace->tty_fd, &cmd, 1);
  67. bytes_to_read = size * 16;
  68. while (bytes_to_read > 0)
  69. {
  70. if ((bytes_read = read(oocd_trace->tty_fd,
  71. ((uint8_t*)data) + (size * 16) - bytes_to_read, bytes_to_read)) < 0)
  72. {
  73. LOG_DEBUG("read() returned %zi (%s)", bytes_read, strerror(errno));
  74. }
  75. else
  76. bytes_to_read -= bytes_read;
  77. }
  78. return ERROR_OK;
  79. }
  80. static int oocd_trace_init(struct etm_context *etm_ctx)
  81. {
  82. uint8_t trash[256];
  83. struct oocd_trace *oocd_trace = etm_ctx->capture_driver_priv;
  84. size_t bytes_read;
  85. oocd_trace->tty_fd = open(oocd_trace->tty, O_RDWR | O_NOCTTY | O_NONBLOCK);
  86. if (oocd_trace->tty_fd < 0)
  87. {
  88. LOG_ERROR("can't open tty");
  89. return ERROR_ETM_CAPTURE_INIT_FAILED;
  90. }
  91. /* clear input & output buffers, then switch to "blocking mode" */
  92. tcflush(oocd_trace->tty_fd, TCOFLUSH);
  93. tcflush(oocd_trace->tty_fd, TCIFLUSH);
  94. fcntl(oocd_trace->tty_fd, F_SETFL, fcntl(oocd_trace->tty_fd, F_GETFL) & ~O_NONBLOCK);
  95. tcgetattr(oocd_trace->tty_fd, &oocd_trace->oldtio); /* save current port settings */
  96. bzero(&oocd_trace->newtio, sizeof(oocd_trace->newtio));
  97. oocd_trace->newtio.c_cflag = CS8 | CLOCAL | CREAD | B2500000;
  98. oocd_trace->newtio.c_iflag = IGNPAR | IGNBRK | IXON | IXOFF;
  99. oocd_trace->newtio.c_oflag = 0;
  100. /* set input mode (non-canonical, no echo,...) */
  101. oocd_trace->newtio.c_lflag = 0;
  102. cfmakeraw(&oocd_trace->newtio);
  103. oocd_trace->newtio.c_cc[VTIME] = 1; /* inter-character timer used */
  104. oocd_trace->newtio.c_cc[VMIN] = 0; /* blocking read until 0 chars received */
  105. tcflush(oocd_trace->tty_fd, TCIFLUSH);
  106. tcsetattr(oocd_trace->tty_fd, TCSANOW, &oocd_trace->newtio);
  107. /* occasionally one bogus character is left in the input buffer
  108. * read up any leftover characters to ensure communication is in sync */
  109. while ((bytes_read = read(oocd_trace->tty_fd, trash, sizeof(trash))) > 0)
  110. {
  111. LOG_DEBUG("%zi bytes read\n", bytes_read);
  112. };
  113. return ERROR_OK;
  114. }
  115. static trace_status_t oocd_trace_status(struct etm_context *etm_ctx)
  116. {
  117. struct oocd_trace *oocd_trace = etm_ctx->capture_driver_priv;
  118. uint32_t status;
  119. oocd_trace_read_reg(oocd_trace, OOCD_TRACE_STATUS, &status);
  120. /* if tracing is currently idle, return this information */
  121. if (etm_ctx->capture_status == TRACE_IDLE)
  122. {
  123. return etm_ctx->capture_status;
  124. }
  125. else if (etm_ctx->capture_status & TRACE_RUNNING)
  126. {
  127. /* check Full bit to identify an overflow */
  128. if (status & 0x4)
  129. etm_ctx->capture_status |= TRACE_OVERFLOWED;
  130. /* check Triggered bit to identify trigger condition */
  131. if (status & 0x2)
  132. etm_ctx->capture_status |= TRACE_TRIGGERED;
  133. if (status & 0x1)
  134. {
  135. etm_ctx->capture_status &= ~TRACE_RUNNING;
  136. etm_ctx->capture_status |= TRACE_COMPLETED;
  137. }
  138. }
  139. return etm_ctx->capture_status;
  140. }
  141. static int oocd_trace_read_trace(struct etm_context *etm_ctx)
  142. {
  143. struct oocd_trace *oocd_trace = etm_ctx->capture_driver_priv;
  144. uint32_t status, address;
  145. uint32_t first_frame = 0x0;
  146. uint32_t num_frames = 1048576;
  147. uint8_t *trace_data;
  148. uint32_t i;
  149. oocd_trace_read_reg(oocd_trace, OOCD_TRACE_STATUS, &status);
  150. oocd_trace_read_reg(oocd_trace, OOCD_TRACE_ADDRESS, &address);
  151. /* check if we overflowed, and adjust first frame of the trace accordingly
  152. * if we didn't overflow, read only up to the frame that would be written next,
  153. * i.e. don't read invalid entries
  154. */
  155. if (status & 0x4)
  156. first_frame = address;
  157. else
  158. num_frames = address;
  159. /* read data into temporary array for unpacking
  160. * one frame from OpenOCD + trace corresponds to 16 trace cycles
  161. */
  162. trace_data = malloc(sizeof(uint8_t) * num_frames * 16);
  163. oocd_trace_read_memory(oocd_trace, trace_data, first_frame, num_frames);
  164. if (etm_ctx->trace_depth > 0)
  165. {
  166. free(etm_ctx->trace_data);
  167. }
  168. etm_ctx->trace_depth = num_frames * 16;
  169. etm_ctx->trace_data = malloc(sizeof(struct etmv1_trace_data) * etm_ctx->trace_depth);
  170. for (i = 0; i < num_frames * 16; i++)
  171. {
  172. etm_ctx->trace_data[i].pipestat = (trace_data[i] & 0x7);
  173. etm_ctx->trace_data[i].packet = (trace_data[i] & 0x78) >> 3;
  174. etm_ctx->trace_data[i].flags = 0;
  175. if ((trace_data[i] & 0x80) >> 7)
  176. {
  177. etm_ctx->trace_data[i].flags |= ETMV1_TRACESYNC_CYCLE;
  178. }
  179. if (etm_ctx->trace_data[i].pipestat == STAT_TR)
  180. {
  181. etm_ctx->trace_data[i].pipestat = etm_ctx->trace_data[i].packet & 0x7;
  182. etm_ctx->trace_data[i].flags |= ETMV1_TRIGGER_CYCLE;
  183. }
  184. }
  185. free(trace_data);
  186. return ERROR_OK;
  187. }
  188. static int oocd_trace_start_capture(struct etm_context *etm_ctx)
  189. {
  190. struct oocd_trace *oocd_trace = etm_ctx->capture_driver_priv;
  191. uint32_t control = 0x1; /* 0x1: enabled */
  192. uint32_t trigger_count;
  193. if (((etm_ctx->control & ETM_PORT_MODE_MASK) != ETM_PORT_NORMAL)
  194. || ((etm_ctx->control & ETM_PORT_WIDTH_MASK) != ETM_PORT_4BIT))
  195. {
  196. LOG_DEBUG("OpenOCD + trace only supports normal 4-bit ETM mode");
  197. return ERROR_ETM_PORTMODE_NOT_SUPPORTED;
  198. }
  199. if ((etm_ctx->control & ETM_PORT_CLOCK_MASK) == ETM_PORT_HALF_CLOCK)
  200. {
  201. control |= 0x2; /* half rate clock, capture at twice the clock rate */
  202. }
  203. /* OpenOCD + trace holds up to 16 million samples,
  204. * but trigger counts is set in multiples of 16 */
  205. trigger_count = (1048576 * /* trigger_percent */ 50) / 100;
  206. /* capturing always starts at address zero */
  207. oocd_trace_write_reg(oocd_trace, OOCD_TRACE_ADDRESS, 0x0);
  208. oocd_trace_write_reg(oocd_trace, OOCD_TRACE_TRIGGER_COUNTER, trigger_count);
  209. oocd_trace_write_reg(oocd_trace, OOCD_TRACE_CONTROL, control);
  210. /* we're starting a new trace, initialize capture status */
  211. etm_ctx->capture_status = TRACE_RUNNING;
  212. return ERROR_OK;
  213. }
  214. static int oocd_trace_stop_capture(struct etm_context *etm_ctx)
  215. {
  216. struct oocd_trace *oocd_trace = etm_ctx->capture_driver_priv;
  217. /* trace stopped, just clear running flag, but preserve others */
  218. etm_ctx->capture_status &= ~TRACE_RUNNING;
  219. oocd_trace_write_reg(oocd_trace, OOCD_TRACE_CONTROL, 0x0);
  220. return ERROR_OK;
  221. }
  222. COMMAND_HANDLER(handle_oocd_trace_config_command)
  223. {
  224. struct target *target;
  225. struct arm *arm;
  226. if (CMD_ARGC != 2)
  227. {
  228. LOG_ERROR("incomplete 'oocd_trace config <target> <tty>' command");
  229. return ERROR_FAIL;
  230. }
  231. target = get_current_target(CMD_CTX);
  232. arm = target_to_arm(target);
  233. if (!is_arm(arm))
  234. {
  235. command_print(CMD_CTX, "current target isn't an ARM");
  236. return ERROR_FAIL;
  237. }
  238. if (arm->etm)
  239. {
  240. struct oocd_trace *oocd_trace = malloc(sizeof(struct oocd_trace));
  241. arm->etm->capture_driver_priv = oocd_trace;
  242. oocd_trace->etm_ctx = arm->etm;
  243. /* copy name of TTY device used to communicate with OpenOCD + trace */
  244. oocd_trace->tty = strndup(CMD_ARGV[1], 256);
  245. }
  246. else
  247. {
  248. LOG_ERROR("target has no ETM defined, OpenOCD + trace left unconfigured");
  249. }
  250. return ERROR_OK;
  251. }
  252. COMMAND_HANDLER(handle_oocd_trace_status_command)
  253. {
  254. struct target *target;
  255. struct arm *arm;
  256. struct oocd_trace *oocd_trace;
  257. uint32_t status;
  258. target = get_current_target(CMD_CTX);
  259. arm = target_to_arm(target);
  260. if (!is_arm(arm))
  261. {
  262. command_print(CMD_CTX, "current target isn't an ARM");
  263. return ERROR_FAIL;
  264. }
  265. if (!arm->etm)
  266. {
  267. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  268. return ERROR_FAIL;
  269. }
  270. if (strcmp(arm->etm->capture_driver->name, "oocd_trace") != 0)
  271. {
  272. command_print(CMD_CTX, "current target's ETM capture driver isn't 'oocd_trace'");
  273. return ERROR_FAIL;
  274. }
  275. oocd_trace = (struct oocd_trace*)arm->etm->capture_driver_priv;
  276. oocd_trace_read_reg(oocd_trace, OOCD_TRACE_STATUS, &status);
  277. if (status & 0x8)
  278. command_print(CMD_CTX, "trace clock locked");
  279. else
  280. command_print(CMD_CTX, "no trace clock");
  281. return ERROR_OK;
  282. }
  283. COMMAND_HANDLER(handle_oocd_trace_resync_command)
  284. {
  285. struct target *target;
  286. struct arm *arm;
  287. struct oocd_trace *oocd_trace;
  288. size_t bytes_written;
  289. uint8_t cmd_array[1];
  290. target = get_current_target(CMD_CTX);
  291. arm = target_to_arm(target);
  292. if (!is_arm(arm))
  293. {
  294. command_print(CMD_CTX, "current target isn't an ARM");
  295. return ERROR_FAIL;
  296. }
  297. if (!arm->etm)
  298. {
  299. command_print(CMD_CTX, "current target doesn't have an ETM configured");
  300. return ERROR_FAIL;
  301. }
  302. if (strcmp(arm->etm->capture_driver->name, "oocd_trace") != 0)
  303. {
  304. command_print(CMD_CTX, "current target's ETM capture driver isn't 'oocd_trace'");
  305. return ERROR_FAIL;
  306. }
  307. oocd_trace = (struct oocd_trace*)arm->etm->capture_driver_priv;
  308. cmd_array[0] = 0xf0;
  309. bytes_written = write(oocd_trace->tty_fd, cmd_array, 1);
  310. command_print(CMD_CTX, "requesting traceclock resync");
  311. LOG_DEBUG("resyncing traceclk pll");
  312. return ERROR_OK;
  313. }
  314. static const struct command_registration oocd_trace_all_command_handlers[] = {
  315. {
  316. .name = "config",
  317. .handler = handle_oocd_trace_config_command,
  318. .mode = COMMAND_CONFIG,
  319. .usage = "target",
  320. },
  321. {
  322. .name = "status",
  323. .handler = handle_oocd_trace_status_command,
  324. .mode = COMMAND_EXEC,
  325. .help = "display OpenOCD + trace status",
  326. },
  327. {
  328. .name = "resync",
  329. .handler = handle_oocd_trace_resync_command,
  330. .mode = COMMAND_EXEC,
  331. .help = "resync OpenOCD + trace capture clock",
  332. },
  333. COMMAND_REGISTRATION_DONE
  334. };
  335. static const struct command_registration oocd_trace_command_handlers[] = {
  336. {
  337. .name = "oocd_trace",
  338. .mode = COMMAND_ANY,
  339. .help = "OpenOCD trace capture driver command group",
  340. .chain = oocd_trace_all_command_handlers,
  341. },
  342. COMMAND_REGISTRATION_DONE
  343. };
  344. struct etm_capture_driver oocd_trace_capture_driver =
  345. {
  346. .name = "oocd_trace",
  347. .commands = oocd_trace_command_handlers,
  348. .init = oocd_trace_init,
  349. .status = oocd_trace_status,
  350. .start_capture = oocd_trace_start_capture,
  351. .stop_capture = oocd_trace_stop_capture,
  352. .read_trace = oocd_trace_read_trace,
  353. };