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.
 
 
 
 
 
 

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