- remove target specific variant and use target->variant member
[openocd.git] / src / target / oocd_trace.c
blob7c0f7ab0c5938668b848178f0d7dcdd20027acf8
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
24 #define _GNU_SOURCE
25 #include <string.h>
26 #include <errno.h>
28 #include "oocd_trace.h"
29 #include "etm.h"
31 #include "log.h"
32 #include "types.h"
33 #include "binarybuffer.h"
34 #include "target.h"
35 #include "register.h"
36 #include "jtag.h"
37 #include "arm7_9_common.h"
38 #include "replacements.h"
40 #include <stdlib.h>
42 int oocd_trace_read_reg(oocd_trace_t *oocd_trace, int reg, u32 *value)
44 size_t bytes_written, bytes_read, bytes_to_read;
45 u8 cmd;
47 cmd = 0x10 | (reg & 0x7);
48 bytes_written = write(oocd_trace->tty_fd, &cmd, 1);
50 bytes_to_read = 4;
51 while (bytes_to_read > 0)
53 bytes_read = read(oocd_trace->tty_fd, ((u8*)value) + 4 - bytes_to_read, bytes_to_read);
54 bytes_to_read -= bytes_read;
57 LOG_DEBUG("reg #%i: 0x%8.8x\n", reg, *value);
59 return ERROR_OK;
62 int oocd_trace_write_reg(oocd_trace_t *oocd_trace, int reg, u32 value)
64 size_t bytes_written;
65 u8 data[5];
67 data[0] = 0x18 | (reg & 0x7);
68 data[1] = value & 0xff;
69 data[2] = (value & 0xff00) >> 8;
70 data[3] = (value & 0xff0000) >> 16;
71 data[4] = (value & 0xff000000) >> 24;
73 bytes_written = write(oocd_trace->tty_fd, data, 5);
74 LOG_DEBUG("reg #%i: 0x%8.8x\n", reg, value);
76 return ERROR_OK;
79 int oocd_trace_read_memory(oocd_trace_t *oocd_trace, u8 *data, u32 address, u32 size)
81 size_t bytes_written, bytes_read, bytes_to_read;
82 u8 cmd;
84 oocd_trace_write_reg(oocd_trace, OOCD_TRACE_ADDRESS, address);
85 oocd_trace_write_reg(oocd_trace, OOCD_TRACE_SDRAM_COUNTER, size);
87 cmd = 0x20;
88 bytes_written = write(oocd_trace->tty_fd, &cmd, 1);
90 bytes_to_read = size * 16;
91 while (bytes_to_read > 0)
93 if ((bytes_read = read(oocd_trace->tty_fd,
94 ((u8*)data) + (size * 16) - bytes_to_read, bytes_to_read)) < 0)
96 LOG_DEBUG("read() returned %i (%s)", bytes_read, strerror(errno));
98 else
99 bytes_to_read -= bytes_read;
102 return ERROR_OK;
105 int oocd_trace_init(etm_context_t *etm_ctx)
107 u8 trash[256];
108 oocd_trace_t *oocd_trace = etm_ctx->capture_driver_priv;
109 size_t bytes_read;
111 oocd_trace->tty_fd = open(oocd_trace->tty, O_RDWR | O_NOCTTY | O_NONBLOCK);
113 if(oocd_trace->tty_fd < 0)
115 LOG_ERROR("can't open tty");
116 return ERROR_ETM_CAPTURE_INIT_FAILED;
119 /* clear input & output buffers, then switch to "blocking mode" */
120 tcflush(oocd_trace->tty_fd, TCOFLUSH);
121 tcflush(oocd_trace->tty_fd, TCIFLUSH);
122 fcntl(oocd_trace->tty_fd, F_SETFL, fcntl(oocd_trace->tty_fd, F_GETFL) & ~O_NONBLOCK);
124 tcgetattr(oocd_trace->tty_fd, &oocd_trace->oldtio); /* save current port settings */
126 bzero(&oocd_trace->newtio, sizeof(oocd_trace->newtio));
127 oocd_trace->newtio.c_cflag = CS8 | CLOCAL | CREAD | B2500000;
129 oocd_trace->newtio.c_iflag = IGNPAR | IGNBRK | IXON | IXOFF;
130 oocd_trace->newtio.c_oflag = 0;
132 /* set input mode (non-canonical, no echo,...) */
133 oocd_trace->newtio.c_lflag = 0;
135 cfmakeraw(&oocd_trace->newtio);
136 oocd_trace->newtio.c_cc[VTIME] = 1; /* inter-character timer used */
137 oocd_trace->newtio.c_cc[VMIN] = 0; /* blocking read until 0 chars received */
139 tcflush(oocd_trace->tty_fd, TCIFLUSH);
140 tcsetattr(oocd_trace->tty_fd, TCSANOW, &oocd_trace->newtio);
142 /* occasionally one bogus character is left in the input buffer
143 * read up any leftover characters to ensure communication is in sync */
144 while ((bytes_read = read(oocd_trace->tty_fd, trash, sizeof(trash))) > 0)
146 LOG_DEBUG("%i bytes read\n", bytes_read);
149 return ERROR_OK;
152 trace_status_t oocd_trace_status(etm_context_t *etm_ctx)
154 oocd_trace_t *oocd_trace = etm_ctx->capture_driver_priv;
155 u32 status;
157 oocd_trace_read_reg(oocd_trace, OOCD_TRACE_STATUS, &status);
159 /* if tracing is currently idle, return this information */
160 if (etm_ctx->capture_status == TRACE_IDLE)
162 return etm_ctx->capture_status;
164 else if (etm_ctx->capture_status & TRACE_RUNNING)
166 /* check Full bit to identify an overflow */
167 if (status & 0x4)
168 etm_ctx->capture_status |= TRACE_OVERFLOWED;
170 /* check Triggered bit to identify trigger condition */
171 if (status & 0x2)
172 etm_ctx->capture_status |= TRACE_TRIGGERED;
174 if (status & 0x1)
176 etm_ctx->capture_status &= ~TRACE_RUNNING;
177 etm_ctx->capture_status |= TRACE_COMPLETED;
181 return etm_ctx->capture_status;
184 int oocd_trace_read_trace(etm_context_t *etm_ctx)
186 oocd_trace_t *oocd_trace = etm_ctx->capture_driver_priv;
187 u32 status, address;
188 u32 first_frame = 0x0;
189 u32 num_frames = 1048576;
190 u8 *trace_data;
191 int i;
193 oocd_trace_read_reg(oocd_trace, OOCD_TRACE_STATUS, &status);
194 oocd_trace_read_reg(oocd_trace, OOCD_TRACE_ADDRESS, &address);
196 /* check if we overflowed, and adjust first frame of the trace accordingly
197 * if we didn't overflow, read only up to the frame that would be written next,
198 * i.e. don't read invalid entries
200 if (status & 0x4)
201 first_frame = address;
202 else
203 num_frames = address;
205 /* read data into temporary array for unpacking
206 * one frame from OpenOCD+trace corresponds to 16 trace cycles
208 trace_data = malloc(sizeof(u8) * num_frames * 16);
209 oocd_trace_read_memory(oocd_trace, trace_data, first_frame, num_frames);
211 if (etm_ctx->trace_depth > 0)
213 free(etm_ctx->trace_data);
216 etm_ctx->trace_depth = num_frames * 16;
217 etm_ctx->trace_data = malloc(sizeof(etmv1_trace_data_t) * etm_ctx->trace_depth);
219 for (i = 0; i < num_frames * 16; i++)
221 etm_ctx->trace_data[i].pipestat = (trace_data[i] & 0x7);
222 etm_ctx->trace_data[i].packet = (trace_data[i] & 0x78) >> 3;
223 etm_ctx->trace_data[i].flags = 0;
225 if ((trace_data[i] & 0x80) >> 7)
227 etm_ctx->trace_data[i].flags |= ETMV1_TRACESYNC_CYCLE;
230 if (etm_ctx->trace_data[i].pipestat == STAT_TR)
232 etm_ctx->trace_data[i].pipestat = etm_ctx->trace_data[i].packet & 0x7;
233 etm_ctx->trace_data[i].flags |= ETMV1_TRIGGER_CYCLE;
237 free(trace_data);
239 return ERROR_OK;
242 int oocd_trace_start_capture(etm_context_t *etm_ctx)
244 oocd_trace_t *oocd_trace = etm_ctx->capture_driver_priv;
245 u32 control = 0x1; /* 0x1: enabled */
246 u32 trigger_count;
248 if (((etm_ctx->portmode & ETM_PORT_MODE_MASK) != ETM_PORT_NORMAL)
249 || ((etm_ctx->portmode & ETM_PORT_WIDTH_MASK) != ETM_PORT_4BIT))
251 LOG_DEBUG("OpenOCD+trace only supports normal 4-bit ETM mode");
252 return ERROR_ETM_PORTMODE_NOT_SUPPORTED;
255 if ((etm_ctx->portmode & ETM_PORT_CLOCK_MASK) == ETM_PORT_HALF_CLOCK)
257 control |= 0x2; /* half rate clock, capture at twice the clock rate */
260 /* OpenOCD+trace holds up to 16 million samples,
261 * but trigger counts is set in multiples of 16 */
262 trigger_count = (1048576 * etm_ctx->trigger_percent) / 100;
264 /* capturing always starts at address zero */
265 oocd_trace_write_reg(oocd_trace, OOCD_TRACE_ADDRESS, 0x0);
266 oocd_trace_write_reg(oocd_trace, OOCD_TRACE_TRIGGER_COUNTER, trigger_count);
267 oocd_trace_write_reg(oocd_trace, OOCD_TRACE_CONTROL, control);
269 /* we're starting a new trace, initialize capture status */
270 etm_ctx->capture_status = TRACE_RUNNING;
272 return ERROR_OK;
275 int oocd_trace_stop_capture(etm_context_t *etm_ctx)
277 oocd_trace_t *oocd_trace = etm_ctx->capture_driver_priv;
279 /* trace stopped, just clear running flag, but preserve others */
280 etm_ctx->capture_status &= ~TRACE_RUNNING;
282 oocd_trace_write_reg(oocd_trace, OOCD_TRACE_CONTROL, 0x0);
284 return ERROR_OK;
287 etm_capture_driver_t oocd_trace_capture_driver =
289 .name = "oocd_trace",
290 .register_commands = oocd_trace_register_commands,
291 .init = oocd_trace_init,
292 .status = oocd_trace_status,
293 .start_capture = oocd_trace_start_capture,
294 .stop_capture = oocd_trace_stop_capture,
295 .read_trace = oocd_trace_read_trace,
298 int handle_oocd_trace_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
300 target_t *target;
301 armv4_5_common_t *armv4_5;
302 arm7_9_common_t *arm7_9;
304 if (argc != 2)
306 LOG_ERROR("incomplete 'oocd_trace config <target> <tty>' command");
307 exit(-1);
310 target = get_current_target(cmd_ctx);
312 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
314 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
315 return ERROR_OK;
318 if (arm7_9->etm_ctx)
320 oocd_trace_t *oocd_trace = malloc(sizeof(oocd_trace_t));
322 arm7_9->etm_ctx->capture_driver_priv = oocd_trace;
323 oocd_trace->etm_ctx = arm7_9->etm_ctx;
325 /* copy name of TTY device used to communicate with OpenOCD+trace */
326 oocd_trace->tty = strndup(args[1], 256);
328 else
330 LOG_ERROR("target has no ETM defined, OpenOCD+trace left unconfigured");
333 return ERROR_OK;
336 int handle_oocd_trace_status_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
338 target_t *target;
339 armv4_5_common_t *armv4_5;
340 arm7_9_common_t *arm7_9;
341 oocd_trace_t *oocd_trace;
342 u32 status;
344 target = get_current_target(cmd_ctx);
346 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
348 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
349 return ERROR_OK;
352 if (!arm7_9->etm_ctx)
354 command_print(cmd_ctx, "current target doesn't have an ETM configured");
355 return ERROR_OK;
358 if (strcmp(arm7_9->etm_ctx->capture_driver->name, "oocd_trace") != 0)
360 command_print(cmd_ctx, "current target's ETM capture driver isn't 'oocd_trace'");
361 return ERROR_OK;
364 oocd_trace = (oocd_trace_t*)arm7_9->etm_ctx->capture_driver_priv;
366 oocd_trace_read_reg(oocd_trace, OOCD_TRACE_STATUS, &status);
368 if (status & 0x8)
369 command_print(cmd_ctx, "trace clock locked");
370 else
371 command_print(cmd_ctx, "no trace clock");
373 return ERROR_OK;
376 int handle_oocd_trace_resync_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
378 target_t *target;
379 armv4_5_common_t *armv4_5;
380 arm7_9_common_t *arm7_9;
381 oocd_trace_t *oocd_trace;
382 size_t bytes_written;
383 u8 cmd_array[1];
385 target = get_current_target(cmd_ctx);
387 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
389 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
390 return ERROR_OK;
393 if (!arm7_9->etm_ctx)
395 command_print(cmd_ctx, "current target doesn't have an ETM configured");
396 return ERROR_OK;
399 if (strcmp(arm7_9->etm_ctx->capture_driver->name, "oocd_trace") != 0)
401 command_print(cmd_ctx, "current target's ETM capture driver isn't 'oocd_trace'");
402 return ERROR_OK;
405 oocd_trace = (oocd_trace_t*)arm7_9->etm_ctx->capture_driver_priv;
407 cmd_array[0] = 0xf0;
409 bytes_written = write(oocd_trace->tty_fd, cmd_array, 1);
411 command_print(cmd_ctx, "requesting traceclock resync");
412 LOG_DEBUG("resyncing traceclk pll");
414 return ERROR_OK;
417 int oocd_trace_register_commands(struct command_context_s *cmd_ctx)
419 command_t *oocd_trace_cmd;
421 oocd_trace_cmd = register_command(cmd_ctx, NULL, "oocd_trace", NULL, COMMAND_ANY, "OpenOCD+trace");
423 register_command(cmd_ctx, oocd_trace_cmd, "config", handle_oocd_trace_config_command, COMMAND_CONFIG, NULL);
425 register_command(cmd_ctx, oocd_trace_cmd, "status", handle_oocd_trace_status_command, COMMAND_EXEC, "display OpenOCD+trace status");
426 register_command(cmd_ctx, oocd_trace_cmd, "resync", handle_oocd_trace_resync_command, COMMAND_EXEC, "resync OpenOCD+trace capture clock");
428 return ERROR_OK;