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