1 /***************************************************************************
2 * Copyright (C) 2015 Paul Fertser <fercerpav@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
16 ***************************************************************************/
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <target/target.h>
23 #include <target/armv7m.h>
24 #include <target/cortex_m.h>
25 #include <target/armv7m_trace.h>
26 #include <jtag/interface.h>
27
28 #define TRACE_BUF_SIZE 4096
29
armv7m_poll_trace(void * target)30 static int armv7m_poll_trace(void *target)
31 {
32 struct armv7m_common *armv7m = target_to_armv7m(target);
33 uint8_t buf[TRACE_BUF_SIZE];
34 size_t size = sizeof(buf);
35 int retval;
36
37 retval = adapter_poll_trace(buf, &size);
38 if (retval != ERROR_OK || !size)
39 return retval;
40
41 target_call_trace_callbacks(target, size, buf);
42
43 switch (armv7m->trace_config.internal_channel) {
44 case TRACE_INTERNAL_CHANNEL_FILE:
45 if (armv7m->trace_config.trace_file != NULL) {
46 if (fwrite(buf, 1, size, armv7m->trace_config.trace_file) == size)
47 fflush(armv7m->trace_config.trace_file);
48 else {
49 LOG_ERROR("Error writing to the trace destination file");
50 return ERROR_FAIL;
51 }
52 }
53 break;
54 case TRACE_INTERNAL_CHANNEL_TCP:
55 if (armv7m->trace_config.trace_service != NULL) {
56 /* broadcast to all service connections */
57 struct connection *connection = armv7m->trace_config.trace_service->connections;
58 retval = ERROR_OK;
59 while (connection) {
60 if (connection_write(connection, buf, size) != (int) size)
61 retval = ERROR_FAIL;
62
63 connection = connection->next;
64 }
65
66 if (retval != ERROR_OK) {
67 LOG_ERROR("Error streaming the trace to TCP/IP port");
68 return ERROR_FAIL;
69 }
70 }
71 break;
72 case TRACE_INTERNAL_CHANNEL_TCL_ONLY:
73 /* nothing to do :
74 * the trace data is sent to TCL by calling the target_call_trace_callbacks
75 **/
76 break;
77 default:
78 LOG_ERROR("unsupported trace internal channel");
79 return ERROR_FAIL;
80 }
81
82 return ERROR_OK;
83 }
84
armv7m_trace_tpiu_config(struct target * target)85 int armv7m_trace_tpiu_config(struct target *target)
86 {
87 struct armv7m_common *armv7m = target_to_armv7m(target);
88 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
89 uint16_t prescaler;
90 int retval;
91
92 target_unregister_timer_callback(armv7m_poll_trace, target);
93
94 retval = adapter_config_trace(trace_config->config_type == TRACE_CONFIG_TYPE_INTERNAL,
95 trace_config->pin_protocol, trace_config->port_size,
96 &trace_config->trace_freq, trace_config->traceclkin_freq, &prescaler);
97
98 if (retval != ERROR_OK)
99 return retval;
100
101 if (trace_config->config_type == TRACE_CONFIG_TYPE_EXTERNAL) {
102 prescaler = trace_config->traceclkin_freq / trace_config->trace_freq;
103
104 if (trace_config->traceclkin_freq % trace_config->trace_freq) {
105 prescaler++;
106
107 int trace_freq = trace_config->traceclkin_freq / prescaler;
108 LOG_INFO("Can not obtain %u trace port frequency from %u "
109 "TRACECLKIN frequency, using %u instead",
110 trace_config->trace_freq, trace_config->traceclkin_freq,
111 trace_freq);
112
113 trace_config->trace_freq = trace_freq;
114 }
115 }
116
117 if (!trace_config->trace_freq) {
118 LOG_ERROR("Trace port frequency is 0, can't enable TPIU");
119 return ERROR_FAIL;
120 }
121
122 retval = target_write_u32(target, TPIU_CSPSR, 1 << trace_config->port_size);
123 if (retval != ERROR_OK)
124 return retval;
125
126 retval = target_write_u32(target, TPIU_ACPR, prescaler - 1);
127 if (retval != ERROR_OK)
128 return retval;
129
130 retval = target_write_u32(target, TPIU_SPPR, trace_config->pin_protocol);
131 if (retval != ERROR_OK)
132 return retval;
133
134 uint32_t ffcr;
135 retval = target_read_u32(target, TPIU_FFCR, &ffcr);
136 if (retval != ERROR_OK)
137 return retval;
138 if (trace_config->formatter)
139 ffcr |= (1 << 1);
140 else
141 ffcr &= ~(1 << 1);
142 retval = target_write_u32(target, TPIU_FFCR, ffcr);
143 if (retval != ERROR_OK)
144 return retval;
145
146 if (trace_config->config_type == TRACE_CONFIG_TYPE_INTERNAL)
147 target_register_timer_callback(armv7m_poll_trace, 1,
148 TARGET_TIMER_TYPE_PERIODIC, target);
149
150 target_call_event_callbacks(target, TARGET_EVENT_TRACE_CONFIG);
151
152 return ERROR_OK;
153 }
154
armv7m_trace_itm_config(struct target * target)155 int armv7m_trace_itm_config(struct target *target)
156 {
157 struct armv7m_common *armv7m = target_to_armv7m(target);
158 struct armv7m_trace_config *trace_config = &armv7m->trace_config;
159 int retval;
160
161 retval = target_write_u32(target, ITM_LAR, ITM_LAR_KEY);
162 if (retval != ERROR_OK)
163 return retval;
164
165 /* Enable ITM, TXENA, set TraceBusID and other parameters */
166 retval = target_write_u32(target, ITM_TCR, (1 << 0) | (1 << 3) |
167 (trace_config->itm_diff_timestamps << 1) |
168 (trace_config->itm_synchro_packets << 2) |
169 (trace_config->itm_async_timestamps << 4) |
170 (trace_config->itm_ts_prescale << 8) |
171 (trace_config->trace_bus_id << 16));
172 if (retval != ERROR_OK)
173 return retval;
174
175 for (unsigned int i = 0; i < 8; i++) {
176 retval = target_write_u32(target, ITM_TER0 + i * 4,
177 trace_config->itm_ter[i]);
178 if (retval != ERROR_OK)
179 return retval;
180 }
181
182 return ERROR_OK;
183 }
184
close_trace_channel(struct armv7m_common * armv7m)185 static void close_trace_channel(struct armv7m_common *armv7m)
186 {
187 switch (armv7m->trace_config.internal_channel) {
188 case TRACE_INTERNAL_CHANNEL_FILE:
189 if (armv7m->trace_config.trace_file)
190 fclose(armv7m->trace_config.trace_file);
191 armv7m->trace_config.trace_file = NULL;
192 break;
193 case TRACE_INTERNAL_CHANNEL_TCP:
194 if (armv7m->trace_config.trace_service)
195 remove_service(armv7m->trace_config.trace_service->name, armv7m->trace_config.trace_service->port);
196 armv7m->trace_config.trace_service = NULL;
197 break;
198 case TRACE_INTERNAL_CHANNEL_TCL_ONLY:
199 /* nothing to do:
200 * the trace polling is disabled in the beginning of armv7m_trace_tpiu_config
201 **/
202 break;
203 default:
204 LOG_ERROR("unsupported trace internal channel");
205 }
206 }
207
trace_new_connection(struct connection * connection)208 static int trace_new_connection(struct connection *connection)
209 {
210 /* nothing to do */
211 return ERROR_OK;
212 }
213
trace_input(struct connection * connection)214 static int trace_input(struct connection *connection)
215 {
216 /* create a dummy buffer to check if the connection is still active */
217 const int buf_len = 100;
218 unsigned char buf[buf_len];
219 int bytes_read = connection_read(connection, buf, buf_len);
220
221 if (bytes_read == 0)
222 return ERROR_SERVER_REMOTE_CLOSED;
223 else if (bytes_read == -1) {
224 LOG_ERROR("error during read: %s", strerror(errno));
225 return ERROR_SERVER_REMOTE_CLOSED;
226 }
227
228 return ERROR_OK;
229 }
230
trace_connection_closed(struct connection * connection)231 static int trace_connection_closed(struct connection *connection)
232 {
233 /* nothing to do, no connection->priv to free */
234 return ERROR_OK;
235 }
236
237 extern struct command_context *global_cmd_ctx;
238
armv7m_trace_tpiu_exit(struct target * target)239 int armv7m_trace_tpiu_exit(struct target *target)
240 {
241 struct armv7m_common *armv7m = target_to_armv7m(target);
242
243 if (global_cmd_ctx->mode == COMMAND_CONFIG ||
244 armv7m->trace_config.config_type == TRACE_CONFIG_TYPE_DISABLED)
245 return ERROR_OK;
246
247 close_trace_channel(armv7m);
248 armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_DISABLED;
249 return armv7m_trace_tpiu_config(target);
250 }
251
COMMAND_HANDLER(handle_tpiu_config_command)252 COMMAND_HANDLER(handle_tpiu_config_command)
253 {
254 struct target *target = get_current_target(CMD_CTX);
255 struct armv7m_common *armv7m = target_to_armv7m(target);
256
257 unsigned int cmd_idx = 0;
258
259 if (CMD_ARGC == cmd_idx)
260 return ERROR_COMMAND_SYNTAX_ERROR;
261 if (!strcmp(CMD_ARGV[cmd_idx], "disable")) {
262 if (CMD_ARGC == cmd_idx + 1) {
263 close_trace_channel(armv7m);
264
265 armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_DISABLED;
266 if (CMD_CTX->mode == COMMAND_EXEC)
267 return armv7m_trace_tpiu_config(target);
268 else
269 return ERROR_OK;
270 }
271 } else if (!strcmp(CMD_ARGV[cmd_idx], "external") ||
272 !strcmp(CMD_ARGV[cmd_idx], "internal")) {
273 close_trace_channel(armv7m);
274
275 armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_EXTERNAL;
276 if (!strcmp(CMD_ARGV[cmd_idx], "internal")) {
277 cmd_idx++;
278 if (CMD_ARGC == cmd_idx)
279 return ERROR_COMMAND_SYNTAX_ERROR;
280
281 armv7m->trace_config.config_type = TRACE_CONFIG_TYPE_INTERNAL;
282 armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_TCL_ONLY;
283
284 if (strcmp(CMD_ARGV[cmd_idx], "-") != 0) {
285 if (CMD_ARGV[cmd_idx][0] == ':') {
286 armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_TCP;
287
288 int ret = add_service("armv7m_trace", &(CMD_ARGV[cmd_idx][1]),
289 CONNECTION_LIMIT_UNLIMITED, trace_new_connection, trace_input,
290 trace_connection_closed, NULL, &armv7m->trace_config.trace_service);
291 if (ret != ERROR_OK) {
292 LOG_ERROR("Can't configure trace TCP port");
293 return ERROR_FAIL;
294 }
295 } else {
296 armv7m->trace_config.internal_channel = TRACE_INTERNAL_CHANNEL_FILE;
297 armv7m->trace_config.trace_file = fopen(CMD_ARGV[cmd_idx], "ab");
298 if (!armv7m->trace_config.trace_file) {
299 LOG_ERROR("Can't open trace destination file");
300 return ERROR_FAIL;
301 }
302 }
303 }
304 }
305 cmd_idx++;
306 if (CMD_ARGC == cmd_idx)
307 return ERROR_COMMAND_SYNTAX_ERROR;
308
309 if (!strcmp(CMD_ARGV[cmd_idx], "sync")) {
310 armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_SYNC;
311
312 cmd_idx++;
313 if (CMD_ARGC == cmd_idx)
314 return ERROR_COMMAND_SYNTAX_ERROR;
315
316 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[cmd_idx], armv7m->trace_config.port_size);
317 } else {
318 if (!strcmp(CMD_ARGV[cmd_idx], "manchester"))
319 armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_MANCHESTER;
320 else if (!strcmp(CMD_ARGV[cmd_idx], "uart"))
321 armv7m->trace_config.pin_protocol = TPIU_PIN_PROTOCOL_ASYNC_UART;
322 else
323 return ERROR_COMMAND_SYNTAX_ERROR;
324
325 cmd_idx++;
326 if (CMD_ARGC == cmd_idx)
327 return ERROR_COMMAND_SYNTAX_ERROR;
328
329 COMMAND_PARSE_ON_OFF(CMD_ARGV[cmd_idx], armv7m->trace_config.formatter);
330 }
331
332 cmd_idx++;
333 if (CMD_ARGC == cmd_idx)
334 return ERROR_COMMAND_SYNTAX_ERROR;
335
336 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.traceclkin_freq);
337
338 cmd_idx++;
339 if (CMD_ARGC != cmd_idx) {
340 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[cmd_idx], armv7m->trace_config.trace_freq);
341 cmd_idx++;
342 } else {
343 if (armv7m->trace_config.config_type != TRACE_CONFIG_TYPE_INTERNAL) {
344 LOG_ERROR("Trace port frequency can't be omitted in external capture mode");
345 return ERROR_COMMAND_SYNTAX_ERROR;
346 }
347 armv7m->trace_config.trace_freq = 0;
348 }
349
350 if (CMD_ARGC == cmd_idx) {
351 if (CMD_CTX->mode == COMMAND_EXEC)
352 return armv7m_trace_tpiu_config(target);
353 else
354 return ERROR_OK;
355 }
356 }
357
358 return ERROR_COMMAND_SYNTAX_ERROR;
359 }
360
COMMAND_HANDLER(handle_itm_port_command)361 COMMAND_HANDLER(handle_itm_port_command)
362 {
363 struct target *target = get_current_target(CMD_CTX);
364 struct armv7m_common *armv7m = target_to_armv7m(target);
365 unsigned int reg_idx;
366 uint8_t port;
367 bool enable;
368
369 if (CMD_ARGC != 2)
370 return ERROR_COMMAND_SYNTAX_ERROR;
371
372 COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], port);
373 COMMAND_PARSE_ON_OFF(CMD_ARGV[1], enable);
374 reg_idx = port / 32;
375 port = port % 32;
376 if (enable)
377 armv7m->trace_config.itm_ter[reg_idx] |= (1 << port);
378 else
379 armv7m->trace_config.itm_ter[reg_idx] &= ~(1 << port);
380
381 if (CMD_CTX->mode == COMMAND_EXEC)
382 return armv7m_trace_itm_config(target);
383 else
384 return ERROR_OK;
385 }
386
COMMAND_HANDLER(handle_itm_ports_command)387 COMMAND_HANDLER(handle_itm_ports_command)
388 {
389 struct target *target = get_current_target(CMD_CTX);
390 struct armv7m_common *armv7m = target_to_armv7m(target);
391 bool enable;
392
393 if (CMD_ARGC != 1)
394 return ERROR_COMMAND_SYNTAX_ERROR;
395
396 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
397 memset(armv7m->trace_config.itm_ter, enable ? 0xff : 0,
398 sizeof(armv7m->trace_config.itm_ter));
399
400 if (CMD_CTX->mode == COMMAND_EXEC)
401 return armv7m_trace_itm_config(target);
402 else
403 return ERROR_OK;
404 }
405
406 static const struct command_registration tpiu_command_handlers[] = {
407 {
408 .name = "config",
409 .handler = handle_tpiu_config_command,
410 .mode = COMMAND_ANY,
411 .help = "Configure TPIU features",
412 .usage = "(disable | "
413 "((external | internal (<filename> | <:port> | -)) "
414 "(sync <port width> | ((manchester | uart) <formatter enable>)) "
415 "<TRACECLKIN freq> [<trace freq>]))",
416 },
417 COMMAND_REGISTRATION_DONE
418 };
419
420 static const struct command_registration itm_command_handlers[] = {
421 {
422 .name = "port",
423 .handler = handle_itm_port_command,
424 .mode = COMMAND_ANY,
425 .help = "Enable or disable ITM stimulus port",
426 .usage = "<port> (0|1|on|off)",
427 },
428 {
429 .name = "ports",
430 .handler = handle_itm_ports_command,
431 .mode = COMMAND_ANY,
432 .help = "Enable or disable all ITM stimulus ports",
433 .usage = "(0|1|on|off)",
434 },
435 COMMAND_REGISTRATION_DONE
436 };
437
438 const struct command_registration armv7m_trace_command_handlers[] = {
439 {
440 .name = "tpiu",
441 .mode = COMMAND_ANY,
442 .help = "tpiu command group",
443 .usage = "",
444 .chain = tpiu_command_handlers,
445 },
446 {
447 .name = "itm",
448 .mode = COMMAND_ANY,
449 .help = "itm command group",
450 .usage = "",
451 .chain = itm_command_handlers,
452 },
453 COMMAND_REGISTRATION_DONE
454 };
455