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.
 
 
 
 
 
 

748 lines
20 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 "arm7_9_common.h"
  25. #include "etb.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 <stdlib.h>
  34. char* etb_reg_list[] =
  35. {
  36. "ETB_identification",
  37. "ETB_ram_depth",
  38. "ETB_ram_width",
  39. "ETB_status",
  40. "ETB_ram_data",
  41. "ETB_ram_read_pointer",
  42. "ETB_ram_write_pointer",
  43. "ETB_trigger_counter",
  44. "ETB_control",
  45. };
  46. int etb_reg_arch_type = -1;
  47. int etb_get_reg(reg_t *reg);
  48. int etb_set_reg(reg_t *reg, u32 value);
  49. int etb_set_reg_w_exec(reg_t *reg, u8 *buf);
  50. int etb_write_reg(reg_t *reg, u32 value);
  51. int etb_read_reg(reg_t *reg);
  52. int handle_etb_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  53. int etb_set_instr(etb_t *etb, u32 new_instr)
  54. {
  55. jtag_tap_t *tap;
  56. tap = etb->tap;
  57. if (tap==NULL)
  58. return ERROR_FAIL;
  59. if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr)
  60. {
  61. scan_field_t field;
  62. field.tap = tap;
  63. field.num_bits = tap->ir_length;
  64. field.out_value = calloc(CEIL(field.num_bits, 8), 1);
  65. buf_set_u32(field.out_value, 0, field.num_bits, new_instr);
  66. field.out_mask = NULL;
  67. field.in_value = NULL;
  68. field.in_check_value = NULL;
  69. field.in_check_mask = NULL;
  70. field.in_handler = NULL;
  71. field.in_handler_priv = NULL;
  72. jtag_add_ir_scan(1, &field, -1);
  73. free(field.out_value);
  74. }
  75. return ERROR_OK;
  76. }
  77. int etb_scann(etb_t *etb, u32 new_scan_chain)
  78. {
  79. if(etb->cur_scan_chain != new_scan_chain)
  80. {
  81. scan_field_t field;
  82. field.tap = etb->tap;
  83. field.num_bits = 5;
  84. field.out_value = calloc(CEIL(field.num_bits, 8), 1);
  85. buf_set_u32(field.out_value, 0, field.num_bits, new_scan_chain);
  86. field.out_mask = NULL;
  87. field.in_value = NULL;
  88. field.in_check_value = NULL;
  89. field.in_check_mask = NULL;
  90. field.in_handler = NULL;
  91. field.in_handler_priv = NULL;
  92. /* select INTEST instruction */
  93. etb_set_instr(etb, 0x2);
  94. jtag_add_dr_scan(1, &field, -1);
  95. etb->cur_scan_chain = new_scan_chain;
  96. free(field.out_value);
  97. }
  98. return ERROR_OK;
  99. }
  100. reg_cache_t* etb_build_reg_cache(etb_t *etb)
  101. {
  102. reg_cache_t *reg_cache = malloc(sizeof(reg_cache_t));
  103. reg_t *reg_list = NULL;
  104. etb_reg_t *arch_info = NULL;
  105. int num_regs = 9;
  106. int i;
  107. /* register a register arch-type for etm registers only once */
  108. if (etb_reg_arch_type == -1)
  109. etb_reg_arch_type = register_reg_arch_type(etb_get_reg, etb_set_reg_w_exec);
  110. /* the actual registers are kept in two arrays */
  111. reg_list = calloc(num_regs, sizeof(reg_t));
  112. arch_info = calloc(num_regs, sizeof(etb_reg_t));
  113. /* fill in values for the reg cache */
  114. reg_cache->name = "etb registers";
  115. reg_cache->next = NULL;
  116. reg_cache->reg_list = reg_list;
  117. reg_cache->num_regs = num_regs;
  118. /* set up registers */
  119. for (i = 0; i < num_regs; i++)
  120. {
  121. reg_list[i].name = etb_reg_list[i];
  122. reg_list[i].size = 32;
  123. reg_list[i].dirty = 0;
  124. reg_list[i].valid = 0;
  125. reg_list[i].bitfield_desc = NULL;
  126. reg_list[i].num_bitfields = 0;
  127. reg_list[i].value = calloc(1, 4);
  128. reg_list[i].arch_info = &arch_info[i];
  129. reg_list[i].arch_type = etb_reg_arch_type;
  130. reg_list[i].size = 32;
  131. arch_info[i].addr = i;
  132. arch_info[i].etb = etb;
  133. }
  134. return reg_cache;
  135. }
  136. int etb_get_reg(reg_t *reg)
  137. {
  138. int retval;
  139. if ((retval = etb_read_reg(reg)) != ERROR_OK)
  140. {
  141. LOG_ERROR("BUG: error scheduling etm register read");
  142. return retval;
  143. }
  144. if ((retval = jtag_execute_queue()) != ERROR_OK)
  145. {
  146. LOG_ERROR("register read failed");
  147. return retval;
  148. }
  149. return ERROR_OK;
  150. }
  151. int etb_read_ram(etb_t *etb, u32 *data, int num_frames)
  152. {
  153. scan_field_t fields[3];
  154. int i;
  155. jtag_add_end_state(TAP_IDLE);
  156. etb_scann(etb, 0x0);
  157. etb_set_instr(etb, 0xc);
  158. fields[0].tap = etb->tap;
  159. fields[0].num_bits = 32;
  160. fields[0].out_value = NULL;
  161. fields[0].out_mask = NULL;
  162. fields[0].in_value = NULL;
  163. fields[0].in_check_value = NULL;
  164. fields[0].in_check_mask = NULL;
  165. fields[0].in_handler = NULL;
  166. fields[0].in_handler_priv = NULL;
  167. fields[1].tap = etb->tap;
  168. fields[1].num_bits = 7;
  169. fields[1].out_value = malloc(1);
  170. buf_set_u32(fields[1].out_value, 0, 7, 4);
  171. fields[1].out_mask = NULL;
  172. fields[1].in_value = NULL;
  173. fields[1].in_check_value = NULL;
  174. fields[1].in_check_mask = NULL;
  175. fields[1].in_handler = NULL;
  176. fields[1].in_handler_priv = NULL;
  177. fields[2].tap = etb->tap;
  178. fields[2].num_bits = 1;
  179. fields[2].out_value = malloc(1);
  180. buf_set_u32(fields[2].out_value, 0, 1, 0);
  181. fields[2].out_mask = NULL;
  182. fields[2].in_value = NULL;
  183. fields[2].in_check_value = NULL;
  184. fields[2].in_check_mask = NULL;
  185. fields[2].in_handler = NULL;
  186. fields[2].in_handler_priv = NULL;
  187. jtag_add_dr_scan(3, fields, -1);
  188. fields[0].in_handler = buf_to_u32_handler;
  189. for (i = 0; i < num_frames; i++)
  190. {
  191. /* ensure nR/W reamins set to read */
  192. buf_set_u32(fields[2].out_value, 0, 1, 0);
  193. /* address remains set to 0x4 (RAM data) until we read the last frame */
  194. if (i < num_frames - 1)
  195. buf_set_u32(fields[1].out_value, 0, 7, 4);
  196. else
  197. buf_set_u32(fields[1].out_value, 0, 7, 0);
  198. fields[0].in_handler_priv = &data[i];
  199. jtag_add_dr_scan(3, fields, -1);
  200. }
  201. jtag_execute_queue();
  202. free(fields[1].out_value);
  203. free(fields[2].out_value);
  204. return ERROR_OK;
  205. }
  206. int etb_read_reg_w_check(reg_t *reg, u8* check_value, u8* check_mask)
  207. {
  208. etb_reg_t *etb_reg = reg->arch_info;
  209. u8 reg_addr = etb_reg->addr & 0x7f;
  210. scan_field_t fields[3];
  211. LOG_DEBUG("%i", etb_reg->addr);
  212. jtag_add_end_state(TAP_IDLE);
  213. etb_scann(etb_reg->etb, 0x0);
  214. etb_set_instr(etb_reg->etb, 0xc);
  215. fields[0].tap = etb_reg->etb->tap;
  216. fields[0].num_bits = 32;
  217. fields[0].out_value = reg->value;
  218. fields[0].out_mask = NULL;
  219. fields[0].in_value = NULL;
  220. fields[0].in_check_value = NULL;
  221. fields[0].in_check_mask = NULL;
  222. fields[0].in_handler = NULL;
  223. fields[0].in_handler_priv = NULL;
  224. fields[1].tap = etb_reg->etb->tap;
  225. fields[1].num_bits = 7;
  226. fields[1].out_value = malloc(1);
  227. buf_set_u32(fields[1].out_value, 0, 7, reg_addr);
  228. fields[1].out_mask = NULL;
  229. fields[1].in_value = NULL;
  230. fields[1].in_check_value = NULL;
  231. fields[1].in_check_mask = NULL;
  232. fields[1].in_handler = NULL;
  233. fields[1].in_handler_priv = NULL;
  234. fields[2].tap = etb_reg->etb->tap;
  235. fields[2].num_bits = 1;
  236. fields[2].out_value = malloc(1);
  237. buf_set_u32(fields[2].out_value, 0, 1, 0);
  238. fields[2].out_mask = NULL;
  239. fields[2].in_value = NULL;
  240. fields[2].in_check_value = NULL;
  241. fields[2].in_check_mask = NULL;
  242. fields[2].in_handler = NULL;
  243. fields[2].in_handler_priv = NULL;
  244. jtag_add_dr_scan(3, fields, -1);
  245. /* read the identification register in the second run, to make sure we
  246. * don't read the ETB data register twice, skipping every second entry
  247. */
  248. buf_set_u32(fields[1].out_value, 0, 7, 0x0);
  249. fields[0].in_value = reg->value;
  250. jtag_set_check_value(fields+0, check_value, check_mask, NULL);
  251. jtag_add_dr_scan(3, fields, -1);
  252. free(fields[1].out_value);
  253. free(fields[2].out_value);
  254. return ERROR_OK;
  255. }
  256. int etb_read_reg(reg_t *reg)
  257. {
  258. return etb_read_reg_w_check(reg, NULL, NULL);
  259. }
  260. int etb_set_reg(reg_t *reg, u32 value)
  261. {
  262. int retval;
  263. if ((retval = etb_write_reg(reg, value)) != ERROR_OK)
  264. {
  265. LOG_ERROR("BUG: error scheduling etm register write");
  266. return retval;
  267. }
  268. buf_set_u32(reg->value, 0, reg->size, value);
  269. reg->valid = 1;
  270. reg->dirty = 0;
  271. return ERROR_OK;
  272. }
  273. int etb_set_reg_w_exec(reg_t *reg, u8 *buf)
  274. {
  275. int retval;
  276. etb_set_reg(reg, buf_get_u32(buf, 0, reg->size));
  277. if ((retval = jtag_execute_queue()) != ERROR_OK)
  278. {
  279. LOG_ERROR("register write failed");
  280. return retval;
  281. }
  282. return ERROR_OK;
  283. }
  284. int etb_write_reg(reg_t *reg, u32 value)
  285. {
  286. etb_reg_t *etb_reg = reg->arch_info;
  287. u8 reg_addr = etb_reg->addr & 0x7f;
  288. scan_field_t fields[3];
  289. LOG_DEBUG("%i: 0x%8.8x", etb_reg->addr, value);
  290. jtag_add_end_state(TAP_IDLE);
  291. etb_scann(etb_reg->etb, 0x0);
  292. etb_set_instr(etb_reg->etb, 0xc);
  293. fields[0].tap = etb_reg->etb->tap;
  294. fields[0].num_bits = 32;
  295. fields[0].out_value = malloc(4);
  296. buf_set_u32(fields[0].out_value, 0, 32, value);
  297. fields[0].out_mask = NULL;
  298. fields[0].in_value = NULL;
  299. fields[0].in_check_value = NULL;
  300. fields[0].in_check_mask = NULL;
  301. fields[0].in_handler = NULL;
  302. fields[0].in_handler_priv = NULL;
  303. fields[1].tap = etb_reg->etb->tap;
  304. fields[1].num_bits = 7;
  305. fields[1].out_value = malloc(1);
  306. buf_set_u32(fields[1].out_value, 0, 7, reg_addr);
  307. fields[1].out_mask = NULL;
  308. fields[1].in_value = NULL;
  309. fields[1].in_check_value = NULL;
  310. fields[1].in_check_mask = NULL;
  311. fields[1].in_handler = NULL;
  312. fields[1].in_handler_priv = NULL;
  313. fields[2].tap = etb_reg->etb->tap;
  314. fields[2].num_bits = 1;
  315. fields[2].out_value = malloc(1);
  316. buf_set_u32(fields[2].out_value, 0, 1, 1);
  317. fields[2].out_mask = NULL;
  318. fields[2].in_value = NULL;
  319. fields[2].in_check_value = NULL;
  320. fields[2].in_check_mask = NULL;
  321. fields[2].in_handler = NULL;
  322. fields[2].in_handler_priv = NULL;
  323. jtag_add_dr_scan(3, fields, -1);
  324. free(fields[0].out_value);
  325. free(fields[1].out_value);
  326. free(fields[2].out_value);
  327. return ERROR_OK;
  328. }
  329. int etb_store_reg(reg_t *reg)
  330. {
  331. return etb_write_reg(reg, buf_get_u32(reg->value, 0, reg->size));
  332. }
  333. int etb_register_commands(struct command_context_s *cmd_ctx)
  334. {
  335. command_t *etb_cmd;
  336. etb_cmd = register_command(cmd_ctx, NULL, "etb", NULL, COMMAND_ANY, "Embedded Trace Buffer");
  337. register_command(cmd_ctx, etb_cmd, "config", handle_etb_config_command, COMMAND_CONFIG, NULL);
  338. return ERROR_OK;
  339. }
  340. int handle_etb_config_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  341. {
  342. target_t *target;
  343. jtag_tap_t *tap;
  344. armv4_5_common_t *armv4_5;
  345. arm7_9_common_t *arm7_9;
  346. if (argc != 2)
  347. {
  348. return ERROR_COMMAND_SYNTAX_ERROR;
  349. }
  350. target = get_target_by_num(strtoul(args[0], NULL, 0));
  351. if (!target)
  352. {
  353. LOG_ERROR("target number '%s' not defined", args[0]);
  354. return ERROR_FAIL;
  355. }
  356. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  357. {
  358. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  359. return ERROR_FAIL;
  360. }
  361. tap = jtag_TapByString( args[1] );
  362. if( tap == NULL ){
  363. command_print(cmd_ctx, "Tap: %s does not exist", args[1] );
  364. return ERROR_FAIL;
  365. }
  366. if (arm7_9->etm_ctx)
  367. {
  368. etb_t *etb = malloc(sizeof(etb_t));
  369. arm7_9->etm_ctx->capture_driver_priv = etb;
  370. etb->tap = tap;
  371. etb->cur_scan_chain = -1;
  372. etb->reg_cache = NULL;
  373. etb->ram_width = 0;
  374. etb->ram_depth = 0;
  375. }
  376. else
  377. {
  378. LOG_ERROR("target has no ETM defined, ETB left unconfigured");
  379. return ERROR_FAIL;
  380. }
  381. return ERROR_OK;
  382. }
  383. int etb_init(etm_context_t *etm_ctx)
  384. {
  385. etb_t *etb = etm_ctx->capture_driver_priv;
  386. etb->etm_ctx = etm_ctx;
  387. /* identify ETB RAM depth and width */
  388. etb_read_reg(&etb->reg_cache->reg_list[ETB_RAM_DEPTH]);
  389. etb_read_reg(&etb->reg_cache->reg_list[ETB_RAM_WIDTH]);
  390. jtag_execute_queue();
  391. etb->ram_depth = buf_get_u32(etb->reg_cache->reg_list[ETB_RAM_DEPTH].value, 0, 32);
  392. etb->ram_width = buf_get_u32(etb->reg_cache->reg_list[ETB_RAM_WIDTH].value, 0, 32);
  393. return ERROR_OK;
  394. }
  395. trace_status_t etb_status(etm_context_t *etm_ctx)
  396. {
  397. etb_t *etb = etm_ctx->capture_driver_priv;
  398. etb->etm_ctx = etm_ctx;
  399. /* if tracing is currently idle, return this information */
  400. if (etm_ctx->capture_status == TRACE_IDLE)
  401. {
  402. return etm_ctx->capture_status;
  403. }
  404. else if (etm_ctx->capture_status & TRACE_RUNNING)
  405. {
  406. reg_t *etb_status_reg = &etb->reg_cache->reg_list[ETB_STATUS];
  407. int etb_timeout = 100;
  408. /* trace is running, check the ETB status flags */
  409. etb_get_reg(etb_status_reg);
  410. /* check Full bit to identify an overflow */
  411. if (buf_get_u32(etb_status_reg->value, 0, 1) == 1)
  412. etm_ctx->capture_status |= TRACE_OVERFLOWED;
  413. /* check Triggered bit to identify trigger condition */
  414. if (buf_get_u32(etb_status_reg->value, 1, 1) == 1)
  415. etm_ctx->capture_status |= TRACE_TRIGGERED;
  416. /* check AcqComp to identify trace completion */
  417. if (buf_get_u32(etb_status_reg->value, 2, 1) == 1)
  418. {
  419. while (etb_timeout-- && (buf_get_u32(etb_status_reg->value, 3, 1) == 0))
  420. {
  421. /* wait for data formatter idle */
  422. etb_get_reg(etb_status_reg);
  423. }
  424. if (etb_timeout == 0)
  425. {
  426. LOG_ERROR("AcqComp set but DFEmpty won't go high, ETB status: 0x%x",
  427. buf_get_u32(etb_status_reg->value, 0, etb_status_reg->size));
  428. }
  429. if (!(etm_ctx->capture_status && TRACE_TRIGGERED))
  430. {
  431. LOG_ERROR("trace completed, but no trigger condition detected");
  432. }
  433. etm_ctx->capture_status &= ~TRACE_RUNNING;
  434. etm_ctx->capture_status |= TRACE_COMPLETED;
  435. }
  436. }
  437. return etm_ctx->capture_status;
  438. }
  439. int etb_read_trace(etm_context_t *etm_ctx)
  440. {
  441. etb_t *etb = etm_ctx->capture_driver_priv;
  442. int first_frame = 0;
  443. int num_frames = etb->ram_depth;
  444. u32 *trace_data = NULL;
  445. int i, j;
  446. etb_read_reg(&etb->reg_cache->reg_list[ETB_STATUS]);
  447. etb_read_reg(&etb->reg_cache->reg_list[ETB_RAM_WRITE_POINTER]);
  448. jtag_execute_queue();
  449. /* check if we overflowed, and adjust first frame of the trace accordingly
  450. * if we didn't overflow, read only up to the frame that would be written next,
  451. * i.e. don't read invalid entries
  452. */
  453. if (buf_get_u32(etb->reg_cache->reg_list[ETB_STATUS].value, 0, 1))
  454. {
  455. first_frame = buf_get_u32(etb->reg_cache->reg_list[ETB_RAM_WRITE_POINTER].value, 0, 32);
  456. }
  457. else
  458. {
  459. num_frames = buf_get_u32(etb->reg_cache->reg_list[ETB_RAM_WRITE_POINTER].value, 0, 32);
  460. }
  461. etb_write_reg(&etb->reg_cache->reg_list[ETB_RAM_READ_POINTER], first_frame);
  462. /* read data into temporary array for unpacking */
  463. trace_data = malloc(sizeof(u32) * num_frames);
  464. etb_read_ram(etb, trace_data, num_frames);
  465. if (etm_ctx->trace_depth > 0)
  466. {
  467. free(etm_ctx->trace_data);
  468. }
  469. if ((etm_ctx->portmode & ETM_PORT_WIDTH_MASK) == ETM_PORT_4BIT)
  470. etm_ctx->trace_depth = num_frames * 3;
  471. else if ((etm_ctx->portmode & ETM_PORT_WIDTH_MASK) == ETM_PORT_8BIT)
  472. etm_ctx->trace_depth = num_frames * 2;
  473. else
  474. etm_ctx->trace_depth = num_frames;
  475. etm_ctx->trace_data = malloc(sizeof(etmv1_trace_data_t) * etm_ctx->trace_depth);
  476. for (i = 0, j = 0; i < num_frames; i++)
  477. {
  478. if ((etm_ctx->portmode & ETM_PORT_WIDTH_MASK) == ETM_PORT_4BIT)
  479. {
  480. /* trace word j */
  481. etm_ctx->trace_data[j].pipestat = trace_data[i] & 0x7;
  482. etm_ctx->trace_data[j].packet = (trace_data[i] & 0x78) >> 3;
  483. etm_ctx->trace_data[j].flags = 0;
  484. if ((trace_data[i] & 0x80) >> 7)
  485. {
  486. etm_ctx->trace_data[j].flags |= ETMV1_TRACESYNC_CYCLE;
  487. }
  488. if (etm_ctx->trace_data[j].pipestat == STAT_TR)
  489. {
  490. etm_ctx->trace_data[j].pipestat = etm_ctx->trace_data[j].packet & 0x7;
  491. etm_ctx->trace_data[j].flags |= ETMV1_TRIGGER_CYCLE;
  492. }
  493. /* trace word j+1 */
  494. etm_ctx->trace_data[j+1].pipestat = (trace_data[i] & 0x100) >> 8;
  495. etm_ctx->trace_data[j+1].packet = (trace_data[i] & 0x7800) >> 11;
  496. etm_ctx->trace_data[j+1].flags = 0;
  497. if ((trace_data[i] & 0x8000) >> 15)
  498. {
  499. etm_ctx->trace_data[j+1].flags |= ETMV1_TRACESYNC_CYCLE;
  500. }
  501. if (etm_ctx->trace_data[j+1].pipestat == STAT_TR)
  502. {
  503. etm_ctx->trace_data[j+1].pipestat = etm_ctx->trace_data[j+1].packet & 0x7;
  504. etm_ctx->trace_data[j+1].flags |= ETMV1_TRIGGER_CYCLE;
  505. }
  506. /* trace word j+2 */
  507. etm_ctx->trace_data[j+2].pipestat = (trace_data[i] & 0x10000) >> 16;
  508. etm_ctx->trace_data[j+2].packet = (trace_data[i] & 0x780000) >> 19;
  509. etm_ctx->trace_data[j+2].flags = 0;
  510. if ((trace_data[i] & 0x800000) >> 23)
  511. {
  512. etm_ctx->trace_data[j+2].flags |= ETMV1_TRACESYNC_CYCLE;
  513. }
  514. if (etm_ctx->trace_data[j+2].pipestat == STAT_TR)
  515. {
  516. etm_ctx->trace_data[j+2].pipestat = etm_ctx->trace_data[j+2].packet & 0x7;
  517. etm_ctx->trace_data[j+2].flags |= ETMV1_TRIGGER_CYCLE;
  518. }
  519. j += 3;
  520. }
  521. else if ((etm_ctx->portmode & ETM_PORT_WIDTH_MASK) == ETM_PORT_8BIT)
  522. {
  523. /* trace word j */
  524. etm_ctx->trace_data[j].pipestat = trace_data[i] & 0x7;
  525. etm_ctx->trace_data[j].packet = (trace_data[i] & 0x7f8) >> 3;
  526. etm_ctx->trace_data[j].flags = 0;
  527. if ((trace_data[i] & 0x800) >> 11)
  528. {
  529. etm_ctx->trace_data[j].flags |= ETMV1_TRACESYNC_CYCLE;
  530. }
  531. if (etm_ctx->trace_data[j].pipestat == STAT_TR)
  532. {
  533. etm_ctx->trace_data[j].pipestat = etm_ctx->trace_data[j].packet & 0x7;
  534. etm_ctx->trace_data[j].flags |= ETMV1_TRIGGER_CYCLE;
  535. }
  536. /* trace word j+1 */
  537. etm_ctx->trace_data[j+1].pipestat = (trace_data[i] & 0x7000) >> 12;
  538. etm_ctx->trace_data[j+1].packet = (trace_data[i] & 0x7f8000) >> 15;
  539. etm_ctx->trace_data[j+1].flags = 0;
  540. if ((trace_data[i] & 0x800000) >> 23)
  541. {
  542. etm_ctx->trace_data[j+1].flags |= ETMV1_TRACESYNC_CYCLE;
  543. }
  544. if (etm_ctx->trace_data[j+1].pipestat == STAT_TR)
  545. {
  546. etm_ctx->trace_data[j+1].pipestat = etm_ctx->trace_data[j+1].packet & 0x7;
  547. etm_ctx->trace_data[j+1].flags |= ETMV1_TRIGGER_CYCLE;
  548. }
  549. j += 2;
  550. }
  551. else
  552. {
  553. /* trace word j */
  554. etm_ctx->trace_data[j].pipestat = trace_data[i] & 0x7;
  555. etm_ctx->trace_data[j].packet = (trace_data[i] & 0x7fff8) >> 3;
  556. etm_ctx->trace_data[j].flags = 0;
  557. if ((trace_data[i] & 0x80000) >> 19)
  558. {
  559. etm_ctx->trace_data[j].flags |= ETMV1_TRACESYNC_CYCLE;
  560. }
  561. if (etm_ctx->trace_data[j].pipestat == STAT_TR)
  562. {
  563. etm_ctx->trace_data[j].pipestat = etm_ctx->trace_data[j].packet & 0x7;
  564. etm_ctx->trace_data[j].flags |= ETMV1_TRIGGER_CYCLE;
  565. }
  566. j += 1;
  567. }
  568. }
  569. free(trace_data);
  570. return ERROR_OK;
  571. }
  572. int etb_start_capture(etm_context_t *etm_ctx)
  573. {
  574. etb_t *etb = etm_ctx->capture_driver_priv;
  575. u32 etb_ctrl_value = 0x1;
  576. u32 trigger_count;
  577. if ((etm_ctx->portmode & ETM_PORT_MODE_MASK) == ETM_PORT_DEMUXED)
  578. {
  579. if ((etm_ctx->portmode & ETM_PORT_WIDTH_MASK) != ETM_PORT_8BIT)
  580. {
  581. LOG_ERROR("ETB can't run in demultiplexed mode with a 4 or 16 bit port");
  582. return ERROR_ETM_PORTMODE_NOT_SUPPORTED;
  583. }
  584. etb_ctrl_value |= 0x2;
  585. }
  586. if ((etm_ctx->portmode & ETM_PORT_MODE_MASK) == ETM_PORT_MUXED)
  587. return ERROR_ETM_PORTMODE_NOT_SUPPORTED;
  588. trigger_count = (etb->ram_depth * etm_ctx->trigger_percent) / 100;
  589. etb_write_reg(&etb->reg_cache->reg_list[ETB_TRIGGER_COUNTER], trigger_count);
  590. etb_write_reg(&etb->reg_cache->reg_list[ETB_RAM_WRITE_POINTER], 0x0);
  591. etb_write_reg(&etb->reg_cache->reg_list[ETB_CTRL], etb_ctrl_value);
  592. jtag_execute_queue();
  593. /* we're starting a new trace, initialize capture status */
  594. etm_ctx->capture_status = TRACE_RUNNING;
  595. return ERROR_OK;
  596. }
  597. int etb_stop_capture(etm_context_t *etm_ctx)
  598. {
  599. etb_t *etb = etm_ctx->capture_driver_priv;
  600. reg_t *etb_ctrl_reg = &etb->reg_cache->reg_list[ETB_CTRL];
  601. etb_write_reg(etb_ctrl_reg, 0x0);
  602. jtag_execute_queue();
  603. /* trace stopped, just clear running flag, but preserve others */
  604. etm_ctx->capture_status &= ~TRACE_RUNNING;
  605. return ERROR_OK;
  606. }
  607. etm_capture_driver_t etb_capture_driver =
  608. {
  609. .name = "etb",
  610. .register_commands = etb_register_commands,
  611. .init = etb_init,
  612. .status = etb_status,
  613. .start_capture = etb_start_capture,
  614. .stop_capture = etb_stop_capture,
  615. .read_trace = etb_read_trace,
  616. };