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.
 
 
 
 
 
 

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