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.
 
 
 
 
 
 

3712 lines
102 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006, 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2009 Michael Schwingen *
  9. * michael@schwingen.org *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "breakpoints.h"
  30. #include "xscale.h"
  31. #include "target_type.h"
  32. #include "arm_jtag.h"
  33. #include "arm_simulator.h"
  34. #include "arm_disassembler.h"
  35. #include <helper/time_support.h>
  36. #include "register.h"
  37. #include "image.h"
  38. #include "arm_opcodes.h"
  39. #include "armv4_5.h"
  40. /*
  41. * Important XScale documents available as of October 2009 include:
  42. *
  43. * Intel XScale® Core Developer’s Manual, January 2004
  44. * Order Number: 273473-002
  45. * This has a chapter detailing debug facilities, and punts some
  46. * details to chip-specific microarchitecture documents.
  47. *
  48. * Hot-Debug for Intel XScale® Core Debug White Paper, May 2005
  49. * Document Number: 273539-005
  50. * Less detailed than the developer's manual, but summarizes those
  51. * missing details (for most XScales) and gives LOTS of notes about
  52. * debugger/handler interaction issues. Presents a simpler reset
  53. * and load-handler sequence than the arch doc. (Note, OpenOCD
  54. * doesn't currently support "Hot-Debug" as defined there.)
  55. *
  56. * Chip-specific microarchitecture documents may also be useful.
  57. */
  58. /* forward declarations */
  59. static int xscale_resume(struct target *, int current,
  60. uint32_t address, int handle_breakpoints, int debug_execution);
  61. static int xscale_debug_entry(struct target *);
  62. static int xscale_restore_banked(struct target *);
  63. static int xscale_get_reg(struct reg *reg);
  64. static int xscale_set_reg(struct reg *reg, uint8_t *buf);
  65. static int xscale_set_breakpoint(struct target *, struct breakpoint *);
  66. static int xscale_set_watchpoint(struct target *, struct watchpoint *);
  67. static int xscale_unset_breakpoint(struct target *, struct breakpoint *);
  68. static int xscale_read_trace(struct target *);
  69. /* This XScale "debug handler" is loaded into the processor's
  70. * mini-ICache, which is 2K of code writable only via JTAG.
  71. *
  72. * FIXME the OpenOCD "bin2char" utility currently doesn't handle
  73. * binary files cleanly. It's string oriented, and terminates them
  74. * with a NUL character. Better would be to generate the constants
  75. * and let other code decide names, scoping, and other housekeeping.
  76. */
  77. static /* unsigned const char xscale_debug_handler[] = ... */
  78. #include "xscale_debug.h"
  79. static char *const xscale_reg_list[] = {
  80. "XSCALE_MAINID", /* 0 */
  81. "XSCALE_CACHETYPE",
  82. "XSCALE_CTRL",
  83. "XSCALE_AUXCTRL",
  84. "XSCALE_TTB",
  85. "XSCALE_DAC",
  86. "XSCALE_FSR",
  87. "XSCALE_FAR",
  88. "XSCALE_PID",
  89. "XSCALE_CPACCESS",
  90. "XSCALE_IBCR0", /* 10 */
  91. "XSCALE_IBCR1",
  92. "XSCALE_DBR0",
  93. "XSCALE_DBR1",
  94. "XSCALE_DBCON",
  95. "XSCALE_TBREG",
  96. "XSCALE_CHKPT0",
  97. "XSCALE_CHKPT1",
  98. "XSCALE_DCSR",
  99. "XSCALE_TX",
  100. "XSCALE_RX", /* 20 */
  101. "XSCALE_TXRXCTRL",
  102. };
  103. static const struct xscale_reg xscale_reg_arch_info[] = {
  104. {XSCALE_MAINID, NULL},
  105. {XSCALE_CACHETYPE, NULL},
  106. {XSCALE_CTRL, NULL},
  107. {XSCALE_AUXCTRL, NULL},
  108. {XSCALE_TTB, NULL},
  109. {XSCALE_DAC, NULL},
  110. {XSCALE_FSR, NULL},
  111. {XSCALE_FAR, NULL},
  112. {XSCALE_PID, NULL},
  113. {XSCALE_CPACCESS, NULL},
  114. {XSCALE_IBCR0, NULL},
  115. {XSCALE_IBCR1, NULL},
  116. {XSCALE_DBR0, NULL},
  117. {XSCALE_DBR1, NULL},
  118. {XSCALE_DBCON, NULL},
  119. {XSCALE_TBREG, NULL},
  120. {XSCALE_CHKPT0, NULL},
  121. {XSCALE_CHKPT1, NULL},
  122. {XSCALE_DCSR, NULL}, /* DCSR accessed via JTAG or SW */
  123. {-1, NULL}, /* TX accessed via JTAG */
  124. {-1, NULL}, /* RX accessed via JTAG */
  125. {-1, NULL}, /* TXRXCTRL implicit access via JTAG */
  126. };
  127. /* convenience wrapper to access XScale specific registers */
  128. static int xscale_set_reg_u32(struct reg *reg, uint32_t value)
  129. {
  130. uint8_t buf[4];
  131. buf_set_u32(buf, 0, 32, value);
  132. return xscale_set_reg(reg, buf);
  133. }
  134. static const char xscale_not[] = "target is not an XScale";
  135. static int xscale_verify_pointer(struct command_context *cmd_ctx,
  136. struct xscale_common *xscale)
  137. {
  138. if (xscale->common_magic != XSCALE_COMMON_MAGIC) {
  139. command_print(cmd_ctx, xscale_not);
  140. return ERROR_TARGET_INVALID;
  141. }
  142. return ERROR_OK;
  143. }
  144. static int xscale_jtag_set_instr(struct jtag_tap *tap, uint32_t new_instr, tap_state_t end_state)
  145. {
  146. assert(tap != NULL);
  147. if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
  148. struct scan_field field;
  149. uint8_t scratch[4];
  150. memset(&field, 0, sizeof field);
  151. field.num_bits = tap->ir_length;
  152. field.out_value = scratch;
  153. buf_set_u32(scratch, 0, field.num_bits, new_instr);
  154. jtag_add_ir_scan(tap, &field, end_state);
  155. }
  156. return ERROR_OK;
  157. }
  158. static int xscale_read_dcsr(struct target *target)
  159. {
  160. struct xscale_common *xscale = target_to_xscale(target);
  161. int retval;
  162. struct scan_field fields[3];
  163. uint8_t field0 = 0x0;
  164. uint8_t field0_check_value = 0x2;
  165. uint8_t field0_check_mask = 0x7;
  166. uint8_t field2 = 0x0;
  167. uint8_t field2_check_value = 0x0;
  168. uint8_t field2_check_mask = 0x1;
  169. xscale_jtag_set_instr(target->tap,
  170. XSCALE_SELDCSR << xscale->xscale_variant,
  171. TAP_DRPAUSE);
  172. buf_set_u32(&field0, 1, 1, xscale->hold_rst);
  173. buf_set_u32(&field0, 2, 1, xscale->external_debug_break);
  174. memset(&fields, 0, sizeof fields);
  175. fields[0].num_bits = 3;
  176. fields[0].out_value = &field0;
  177. uint8_t tmp;
  178. fields[0].in_value = &tmp;
  179. fields[1].num_bits = 32;
  180. fields[1].in_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
  181. fields[2].num_bits = 1;
  182. fields[2].out_value = &field2;
  183. uint8_t tmp2;
  184. fields[2].in_value = &tmp2;
  185. jtag_add_dr_scan(target->tap, 3, fields, TAP_DRPAUSE);
  186. jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
  187. jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
  188. retval = jtag_execute_queue();
  189. if (retval != ERROR_OK) {
  190. LOG_ERROR("JTAG error while reading DCSR");
  191. return retval;
  192. }
  193. xscale->reg_cache->reg_list[XSCALE_DCSR].dirty = 0;
  194. xscale->reg_cache->reg_list[XSCALE_DCSR].valid = 1;
  195. /* write the register with the value we just read
  196. * on this second pass, only the first bit of field0 is guaranteed to be 0)
  197. */
  198. field0_check_mask = 0x1;
  199. fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
  200. fields[1].in_value = NULL;
  201. jtag_add_dr_scan(target->tap, 3, fields, TAP_DRPAUSE);
  202. /* DANGER!!! this must be here. It will make sure that the arguments
  203. * to jtag_set_check_value() does not go out of scope! */
  204. return jtag_execute_queue();
  205. }
  206. static void xscale_getbuf(jtag_callback_data_t arg)
  207. {
  208. uint8_t *in = (uint8_t *)arg;
  209. *((uint32_t *)arg) = buf_get_u32(in, 0, 32);
  210. }
  211. static int xscale_receive(struct target *target, uint32_t *buffer, int num_words)
  212. {
  213. if (num_words == 0)
  214. return ERROR_COMMAND_SYNTAX_ERROR;
  215. struct xscale_common *xscale = target_to_xscale(target);
  216. int retval = ERROR_OK;
  217. tap_state_t path[3];
  218. struct scan_field fields[3];
  219. uint8_t *field0 = malloc(num_words * 1);
  220. uint8_t field0_check_value = 0x2;
  221. uint8_t field0_check_mask = 0x6;
  222. uint32_t *field1 = malloc(num_words * 4);
  223. uint8_t field2_check_value = 0x0;
  224. uint8_t field2_check_mask = 0x1;
  225. int words_done = 0;
  226. int words_scheduled = 0;
  227. int i;
  228. path[0] = TAP_DRSELECT;
  229. path[1] = TAP_DRCAPTURE;
  230. path[2] = TAP_DRSHIFT;
  231. memset(&fields, 0, sizeof fields);
  232. fields[0].num_bits = 3;
  233. uint8_t tmp;
  234. fields[0].in_value = &tmp;
  235. fields[0].check_value = &field0_check_value;
  236. fields[0].check_mask = &field0_check_mask;
  237. fields[1].num_bits = 32;
  238. fields[2].num_bits = 1;
  239. uint8_t tmp2;
  240. fields[2].in_value = &tmp2;
  241. fields[2].check_value = &field2_check_value;
  242. fields[2].check_mask = &field2_check_mask;
  243. xscale_jtag_set_instr(target->tap,
  244. XSCALE_DBGTX << xscale->xscale_variant,
  245. TAP_IDLE);
  246. jtag_add_runtest(1, TAP_IDLE); /* ensures that we're in the TAP_IDLE state as the above
  247. *could be a no-op */
  248. /* repeat until all words have been collected */
  249. int attempts = 0;
  250. while (words_done < num_words) {
  251. /* schedule reads */
  252. words_scheduled = 0;
  253. for (i = words_done; i < num_words; i++) {
  254. fields[0].in_value = &field0[i];
  255. jtag_add_pathmove(3, path);
  256. fields[1].in_value = (uint8_t *)(field1 + i);
  257. jtag_add_dr_scan_check(target->tap, 3, fields, TAP_IDLE);
  258. jtag_add_callback(xscale_getbuf, (jtag_callback_data_t)(field1 + i));
  259. words_scheduled++;
  260. }
  261. retval = jtag_execute_queue();
  262. if (retval != ERROR_OK) {
  263. LOG_ERROR("JTAG error while receiving data from debug handler");
  264. break;
  265. }
  266. /* examine results */
  267. for (i = words_done; i < num_words; i++) {
  268. if (!(field0[i] & 1)) {
  269. /* move backwards if necessary */
  270. int j;
  271. for (j = i; j < num_words - 1; j++) {
  272. field0[j] = field0[j + 1];
  273. field1[j] = field1[j + 1];
  274. }
  275. words_scheduled--;
  276. }
  277. }
  278. if (words_scheduled == 0) {
  279. if (attempts++ == 1000) {
  280. LOG_ERROR(
  281. "Failed to receiving data from debug handler after 1000 attempts");
  282. retval = ERROR_TARGET_TIMEOUT;
  283. break;
  284. }
  285. }
  286. words_done += words_scheduled;
  287. }
  288. for (i = 0; i < num_words; i++)
  289. *(buffer++) = buf_get_u32((uint8_t *)&field1[i], 0, 32);
  290. free(field1);
  291. return retval;
  292. }
  293. static int xscale_read_tx(struct target *target, int consume)
  294. {
  295. struct xscale_common *xscale = target_to_xscale(target);
  296. tap_state_t path[3];
  297. tap_state_t noconsume_path[6];
  298. int retval;
  299. struct timeval timeout, now;
  300. struct scan_field fields[3];
  301. uint8_t field0_in = 0x0;
  302. uint8_t field0_check_value = 0x2;
  303. uint8_t field0_check_mask = 0x6;
  304. uint8_t field2_check_value = 0x0;
  305. uint8_t field2_check_mask = 0x1;
  306. xscale_jtag_set_instr(target->tap,
  307. XSCALE_DBGTX << xscale->xscale_variant,
  308. TAP_IDLE);
  309. path[0] = TAP_DRSELECT;
  310. path[1] = TAP_DRCAPTURE;
  311. path[2] = TAP_DRSHIFT;
  312. noconsume_path[0] = TAP_DRSELECT;
  313. noconsume_path[1] = TAP_DRCAPTURE;
  314. noconsume_path[2] = TAP_DREXIT1;
  315. noconsume_path[3] = TAP_DRPAUSE;
  316. noconsume_path[4] = TAP_DREXIT2;
  317. noconsume_path[5] = TAP_DRSHIFT;
  318. memset(&fields, 0, sizeof fields);
  319. fields[0].num_bits = 3;
  320. fields[0].in_value = &field0_in;
  321. fields[1].num_bits = 32;
  322. fields[1].in_value = xscale->reg_cache->reg_list[XSCALE_TX].value;
  323. fields[2].num_bits = 1;
  324. uint8_t tmp;
  325. fields[2].in_value = &tmp;
  326. gettimeofday(&timeout, NULL);
  327. timeval_add_time(&timeout, 1, 0);
  328. for (;; ) {
  329. /* if we want to consume the register content (i.e. clear TX_READY),
  330. * we have to go straight from Capture-DR to Shift-DR
  331. * otherwise, we go from Capture-DR to Exit1-DR to Pause-DR
  332. */
  333. if (consume)
  334. jtag_add_pathmove(3, path);
  335. else
  336. jtag_add_pathmove(ARRAY_SIZE(noconsume_path), noconsume_path);
  337. jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
  338. jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
  339. jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
  340. retval = jtag_execute_queue();
  341. if (retval != ERROR_OK) {
  342. LOG_ERROR("JTAG error while reading TX");
  343. return ERROR_TARGET_TIMEOUT;
  344. }
  345. gettimeofday(&now, NULL);
  346. if ((now.tv_sec > timeout.tv_sec) ||
  347. ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec))) {
  348. LOG_ERROR("time out reading TX register");
  349. return ERROR_TARGET_TIMEOUT;
  350. }
  351. if (!((!(field0_in & 1)) && consume))
  352. goto done;
  353. if (debug_level >= 3) {
  354. LOG_DEBUG("waiting 100ms");
  355. alive_sleep(100); /* avoid flooding the logs */
  356. } else
  357. keep_alive();
  358. }
  359. done:
  360. if (!(field0_in & 1))
  361. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  362. return ERROR_OK;
  363. }
  364. static int xscale_write_rx(struct target *target)
  365. {
  366. struct xscale_common *xscale = target_to_xscale(target);
  367. int retval;
  368. struct timeval timeout, now;
  369. struct scan_field fields[3];
  370. uint8_t field0_out = 0x0;
  371. uint8_t field0_in = 0x0;
  372. uint8_t field0_check_value = 0x2;
  373. uint8_t field0_check_mask = 0x6;
  374. uint8_t field2 = 0x0;
  375. uint8_t field2_check_value = 0x0;
  376. uint8_t field2_check_mask = 0x1;
  377. xscale_jtag_set_instr(target->tap,
  378. XSCALE_DBGRX << xscale->xscale_variant,
  379. TAP_IDLE);
  380. memset(&fields, 0, sizeof fields);
  381. fields[0].num_bits = 3;
  382. fields[0].out_value = &field0_out;
  383. fields[0].in_value = &field0_in;
  384. fields[1].num_bits = 32;
  385. fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_RX].value;
  386. fields[2].num_bits = 1;
  387. fields[2].out_value = &field2;
  388. uint8_t tmp;
  389. fields[2].in_value = &tmp;
  390. gettimeofday(&timeout, NULL);
  391. timeval_add_time(&timeout, 1, 0);
  392. /* poll until rx_read is low */
  393. LOG_DEBUG("polling RX");
  394. for (;;) {
  395. jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
  396. jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
  397. jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
  398. retval = jtag_execute_queue();
  399. if (retval != ERROR_OK) {
  400. LOG_ERROR("JTAG error while writing RX");
  401. return retval;
  402. }
  403. gettimeofday(&now, NULL);
  404. if ((now.tv_sec > timeout.tv_sec) ||
  405. ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec))) {
  406. LOG_ERROR("time out writing RX register");
  407. return ERROR_TARGET_TIMEOUT;
  408. }
  409. if (!(field0_in & 1))
  410. goto done;
  411. if (debug_level >= 3) {
  412. LOG_DEBUG("waiting 100ms");
  413. alive_sleep(100); /* avoid flooding the logs */
  414. } else
  415. keep_alive();
  416. }
  417. done:
  418. /* set rx_valid */
  419. field2 = 0x1;
  420. jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
  421. retval = jtag_execute_queue();
  422. if (retval != ERROR_OK) {
  423. LOG_ERROR("JTAG error while writing RX");
  424. return retval;
  425. }
  426. return ERROR_OK;
  427. }
  428. /* send count elements of size byte to the debug handler */
  429. static int xscale_send(struct target *target, const uint8_t *buffer, int count, int size)
  430. {
  431. struct xscale_common *xscale = target_to_xscale(target);
  432. uint32_t t[3];
  433. int bits[3];
  434. int retval;
  435. int done_count = 0;
  436. xscale_jtag_set_instr(target->tap,
  437. XSCALE_DBGRX << xscale->xscale_variant,
  438. TAP_IDLE);
  439. bits[0] = 3;
  440. t[0] = 0;
  441. bits[1] = 32;
  442. t[2] = 1;
  443. bits[2] = 1;
  444. int endianness = target->endianness;
  445. while (done_count++ < count) {
  446. switch (size) {
  447. case 4:
  448. if (endianness == TARGET_LITTLE_ENDIAN)
  449. t[1] = le_to_h_u32(buffer);
  450. else
  451. t[1] = be_to_h_u32(buffer);
  452. break;
  453. case 2:
  454. if (endianness == TARGET_LITTLE_ENDIAN)
  455. t[1] = le_to_h_u16(buffer);
  456. else
  457. t[1] = be_to_h_u16(buffer);
  458. break;
  459. case 1:
  460. t[1] = buffer[0];
  461. break;
  462. default:
  463. LOG_ERROR("BUG: size neither 4, 2 nor 1");
  464. return ERROR_COMMAND_SYNTAX_ERROR;
  465. }
  466. jtag_add_dr_out(target->tap,
  467. 3,
  468. bits,
  469. t,
  470. TAP_IDLE);
  471. buffer += size;
  472. }
  473. retval = jtag_execute_queue();
  474. if (retval != ERROR_OK) {
  475. LOG_ERROR("JTAG error while sending data to debug handler");
  476. return retval;
  477. }
  478. return ERROR_OK;
  479. }
  480. static int xscale_send_u32(struct target *target, uint32_t value)
  481. {
  482. struct xscale_common *xscale = target_to_xscale(target);
  483. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_RX].value, 0, 32, value);
  484. return xscale_write_rx(target);
  485. }
  486. static int xscale_write_dcsr(struct target *target, int hold_rst, int ext_dbg_brk)
  487. {
  488. struct xscale_common *xscale = target_to_xscale(target);
  489. int retval;
  490. struct scan_field fields[3];
  491. uint8_t field0 = 0x0;
  492. uint8_t field0_check_value = 0x2;
  493. uint8_t field0_check_mask = 0x7;
  494. uint8_t field2 = 0x0;
  495. uint8_t field2_check_value = 0x0;
  496. uint8_t field2_check_mask = 0x1;
  497. if (hold_rst != -1)
  498. xscale->hold_rst = hold_rst;
  499. if (ext_dbg_brk != -1)
  500. xscale->external_debug_break = ext_dbg_brk;
  501. xscale_jtag_set_instr(target->tap,
  502. XSCALE_SELDCSR << xscale->xscale_variant,
  503. TAP_IDLE);
  504. buf_set_u32(&field0, 1, 1, xscale->hold_rst);
  505. buf_set_u32(&field0, 2, 1, xscale->external_debug_break);
  506. memset(&fields, 0, sizeof fields);
  507. fields[0].num_bits = 3;
  508. fields[0].out_value = &field0;
  509. uint8_t tmp;
  510. fields[0].in_value = &tmp;
  511. fields[1].num_bits = 32;
  512. fields[1].out_value = xscale->reg_cache->reg_list[XSCALE_DCSR].value;
  513. fields[2].num_bits = 1;
  514. fields[2].out_value = &field2;
  515. uint8_t tmp2;
  516. fields[2].in_value = &tmp2;
  517. jtag_add_dr_scan(target->tap, 3, fields, TAP_IDLE);
  518. jtag_check_value_mask(fields + 0, &field0_check_value, &field0_check_mask);
  519. jtag_check_value_mask(fields + 2, &field2_check_value, &field2_check_mask);
  520. retval = jtag_execute_queue();
  521. if (retval != ERROR_OK) {
  522. LOG_ERROR("JTAG error while writing DCSR");
  523. return retval;
  524. }
  525. xscale->reg_cache->reg_list[XSCALE_DCSR].dirty = 0;
  526. xscale->reg_cache->reg_list[XSCALE_DCSR].valid = 1;
  527. return ERROR_OK;
  528. }
  529. /* parity of the number of bits 0 if even; 1 if odd. for 32 bit words */
  530. static unsigned int parity(unsigned int v)
  531. {
  532. /* unsigned int ov = v; */
  533. v ^= v >> 16;
  534. v ^= v >> 8;
  535. v ^= v >> 4;
  536. v &= 0xf;
  537. /* LOG_DEBUG("parity of 0x%x is %i", ov, (0x6996 >> v) & 1); */
  538. return (0x6996 >> v) & 1;
  539. }
  540. static int xscale_load_ic(struct target *target, uint32_t va, uint32_t buffer[8])
  541. {
  542. struct xscale_common *xscale = target_to_xscale(target);
  543. uint8_t packet[4];
  544. uint8_t cmd;
  545. int word;
  546. struct scan_field fields[2];
  547. LOG_DEBUG("loading miniIC at 0x%8.8" PRIx32 "", va);
  548. /* LDIC into IR */
  549. xscale_jtag_set_instr(target->tap,
  550. XSCALE_LDIC << xscale->xscale_variant,
  551. TAP_IDLE);
  552. /* CMD is b011 to load a cacheline into the Mini ICache.
  553. * Loading into the main ICache is deprecated, and unused.
  554. * It's followed by three zero bits, and 27 address bits.
  555. */
  556. buf_set_u32(&cmd, 0, 6, 0x3);
  557. /* virtual address of desired cache line */
  558. buf_set_u32(packet, 0, 27, va >> 5);
  559. memset(&fields, 0, sizeof fields);
  560. fields[0].num_bits = 6;
  561. fields[0].out_value = &cmd;
  562. fields[1].num_bits = 27;
  563. fields[1].out_value = packet;
  564. jtag_add_dr_scan(target->tap, 2, fields, TAP_IDLE);
  565. /* rest of packet is a cacheline: 8 instructions, with parity */
  566. fields[0].num_bits = 32;
  567. fields[0].out_value = packet;
  568. fields[1].num_bits = 1;
  569. fields[1].out_value = &cmd;
  570. for (word = 0; word < 8; word++) {
  571. buf_set_u32(packet, 0, 32, buffer[word]);
  572. uint32_t value;
  573. memcpy(&value, packet, sizeof(uint32_t));
  574. cmd = parity(value);
  575. jtag_add_dr_scan(target->tap, 2, fields, TAP_IDLE);
  576. }
  577. return jtag_execute_queue();
  578. }
  579. static int xscale_invalidate_ic_line(struct target *target, uint32_t va)
  580. {
  581. struct xscale_common *xscale = target_to_xscale(target);
  582. uint8_t packet[4];
  583. uint8_t cmd;
  584. struct scan_field fields[2];
  585. xscale_jtag_set_instr(target->tap,
  586. XSCALE_LDIC << xscale->xscale_variant,
  587. TAP_IDLE);
  588. /* CMD for invalidate IC line b000, bits [6:4] b000 */
  589. buf_set_u32(&cmd, 0, 6, 0x0);
  590. /* virtual address of desired cache line */
  591. buf_set_u32(packet, 0, 27, va >> 5);
  592. memset(&fields, 0, sizeof fields);
  593. fields[0].num_bits = 6;
  594. fields[0].out_value = &cmd;
  595. fields[1].num_bits = 27;
  596. fields[1].out_value = packet;
  597. jtag_add_dr_scan(target->tap, 2, fields, TAP_IDLE);
  598. return ERROR_OK;
  599. }
  600. static int xscale_update_vectors(struct target *target)
  601. {
  602. struct xscale_common *xscale = target_to_xscale(target);
  603. int i;
  604. int retval;
  605. uint32_t low_reset_branch, high_reset_branch;
  606. for (i = 1; i < 8; i++) {
  607. /* if there's a static vector specified for this exception, override */
  608. if (xscale->static_high_vectors_set & (1 << i))
  609. xscale->high_vectors[i] = xscale->static_high_vectors[i];
  610. else {
  611. retval = target_read_u32(target, 0xffff0000 + 4*i, &xscale->high_vectors[i]);
  612. if (retval == ERROR_TARGET_TIMEOUT)
  613. return retval;
  614. if (retval != ERROR_OK) {
  615. /* Some of these reads will fail as part of normal execution */
  616. xscale->high_vectors[i] = ARMV4_5_B(0xfffffe, 0);
  617. }
  618. }
  619. }
  620. for (i = 1; i < 8; i++) {
  621. if (xscale->static_low_vectors_set & (1 << i))
  622. xscale->low_vectors[i] = xscale->static_low_vectors[i];
  623. else {
  624. retval = target_read_u32(target, 0x0 + 4*i, &xscale->low_vectors[i]);
  625. if (retval == ERROR_TARGET_TIMEOUT)
  626. return retval;
  627. if (retval != ERROR_OK) {
  628. /* Some of these reads will fail as part of normal execution */
  629. xscale->low_vectors[i] = ARMV4_5_B(0xfffffe, 0);
  630. }
  631. }
  632. }
  633. /* calculate branches to debug handler */
  634. low_reset_branch = (xscale->handler_address + 0x20 - 0x0 - 0x8) >> 2;
  635. high_reset_branch = (xscale->handler_address + 0x20 - 0xffff0000 - 0x8) >> 2;
  636. xscale->low_vectors[0] = ARMV4_5_B((low_reset_branch & 0xffffff), 0);
  637. xscale->high_vectors[0] = ARMV4_5_B((high_reset_branch & 0xffffff), 0);
  638. /* invalidate and load exception vectors in mini i-cache */
  639. xscale_invalidate_ic_line(target, 0x0);
  640. xscale_invalidate_ic_line(target, 0xffff0000);
  641. xscale_load_ic(target, 0x0, xscale->low_vectors);
  642. xscale_load_ic(target, 0xffff0000, xscale->high_vectors);
  643. return ERROR_OK;
  644. }
  645. static int xscale_arch_state(struct target *target)
  646. {
  647. struct xscale_common *xscale = target_to_xscale(target);
  648. struct arm *arm = &xscale->arm;
  649. static const char *state[] = {
  650. "disabled", "enabled"
  651. };
  652. static const char *arch_dbg_reason[] = {
  653. "", "\n(processor reset)", "\n(trace buffer full)"
  654. };
  655. if (arm->common_magic != ARM_COMMON_MAGIC) {
  656. LOG_ERROR("BUG: called for a non-ARMv4/5 target");
  657. return ERROR_COMMAND_SYNTAX_ERROR;
  658. }
  659. arm_arch_state(target);
  660. LOG_USER("MMU: %s, D-Cache: %s, I-Cache: %s%s",
  661. state[xscale->armv4_5_mmu.mmu_enabled],
  662. state[xscale->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled],
  663. state[xscale->armv4_5_mmu.armv4_5_cache.i_cache_enabled],
  664. arch_dbg_reason[xscale->arch_debug_reason]);
  665. return ERROR_OK;
  666. }
  667. static int xscale_poll(struct target *target)
  668. {
  669. int retval = ERROR_OK;
  670. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING)) {
  671. enum target_state previous_state = target->state;
  672. retval = xscale_read_tx(target, 0);
  673. if (retval == ERROR_OK) {
  674. /* there's data to read from the tx register, we entered debug state */
  675. target->state = TARGET_HALTED;
  676. /* process debug entry, fetching current mode regs */
  677. retval = xscale_debug_entry(target);
  678. } else if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
  679. LOG_USER("error while polling TX register, reset CPU");
  680. /* here we "lie" so GDB won't get stuck and a reset can be perfomed */
  681. target->state = TARGET_HALTED;
  682. }
  683. /* debug_entry could have overwritten target state (i.e. immediate resume)
  684. * don't signal event handlers in that case
  685. */
  686. if (target->state != TARGET_HALTED)
  687. return ERROR_OK;
  688. /* if target was running, signal that we halted
  689. * otherwise we reentered from debug execution */
  690. if (previous_state == TARGET_RUNNING)
  691. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  692. else
  693. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  694. }
  695. return retval;
  696. }
  697. static int xscale_debug_entry(struct target *target)
  698. {
  699. struct xscale_common *xscale = target_to_xscale(target);
  700. struct arm *arm = &xscale->arm;
  701. uint32_t pc;
  702. uint32_t buffer[10];
  703. unsigned i;
  704. int retval;
  705. uint32_t moe;
  706. /* clear external dbg break (will be written on next DCSR read) */
  707. xscale->external_debug_break = 0;
  708. retval = xscale_read_dcsr(target);
  709. if (retval != ERROR_OK)
  710. return retval;
  711. /* get r0, pc, r1 to r7 and cpsr */
  712. retval = xscale_receive(target, buffer, 10);
  713. if (retval != ERROR_OK)
  714. return retval;
  715. /* move r0 from buffer to register cache */
  716. buf_set_u32(arm->core_cache->reg_list[0].value, 0, 32, buffer[0]);
  717. arm->core_cache->reg_list[0].dirty = 1;
  718. arm->core_cache->reg_list[0].valid = 1;
  719. LOG_DEBUG("r0: 0x%8.8" PRIx32 "", buffer[0]);
  720. /* move pc from buffer to register cache */
  721. buf_set_u32(arm->pc->value, 0, 32, buffer[1]);
  722. arm->pc->dirty = 1;
  723. arm->pc->valid = 1;
  724. LOG_DEBUG("pc: 0x%8.8" PRIx32 "", buffer[1]);
  725. /* move data from buffer to register cache */
  726. for (i = 1; i <= 7; i++) {
  727. buf_set_u32(arm->core_cache->reg_list[i].value, 0, 32, buffer[1 + i]);
  728. arm->core_cache->reg_list[i].dirty = 1;
  729. arm->core_cache->reg_list[i].valid = 1;
  730. LOG_DEBUG("r%i: 0x%8.8" PRIx32 "", i, buffer[i + 1]);
  731. }
  732. arm_set_cpsr(arm, buffer[9]);
  733. LOG_DEBUG("cpsr: 0x%8.8" PRIx32 "", buffer[9]);
  734. if (!is_arm_mode(arm->core_mode)) {
  735. target->state = TARGET_UNKNOWN;
  736. LOG_ERROR("cpsr contains invalid mode value - communication failure");
  737. return ERROR_TARGET_FAILURE;
  738. }
  739. LOG_DEBUG("target entered debug state in %s mode",
  740. arm_mode_name(arm->core_mode));
  741. /* get banked registers, r8 to r14, and spsr if not in USR/SYS mode */
  742. if (arm->spsr) {
  743. xscale_receive(target, buffer, 8);
  744. buf_set_u32(arm->spsr->value, 0, 32, buffer[7]);
  745. arm->spsr->dirty = false;
  746. arm->spsr->valid = true;
  747. } else {
  748. /* r8 to r14, but no spsr */
  749. xscale_receive(target, buffer, 7);
  750. }
  751. /* move data from buffer to right banked register in cache */
  752. for (i = 8; i <= 14; i++) {
  753. struct reg *r = arm_reg_current(arm, i);
  754. buf_set_u32(r->value, 0, 32, buffer[i - 8]);
  755. r->dirty = false;
  756. r->valid = true;
  757. }
  758. /* mark xscale regs invalid to ensure they are retrieved from the
  759. * debug handler if requested */
  760. for (i = 0; i < xscale->reg_cache->num_regs; i++)
  761. xscale->reg_cache->reg_list[i].valid = 0;
  762. /* examine debug reason */
  763. xscale_read_dcsr(target);
  764. moe = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 2, 3);
  765. /* stored PC (for calculating fixup) */
  766. pc = buf_get_u32(arm->pc->value, 0, 32);
  767. switch (moe) {
  768. case 0x0: /* Processor reset */
  769. target->debug_reason = DBG_REASON_DBGRQ;
  770. xscale->arch_debug_reason = XSCALE_DBG_REASON_RESET;
  771. pc -= 4;
  772. break;
  773. case 0x1: /* Instruction breakpoint hit */
  774. target->debug_reason = DBG_REASON_BREAKPOINT;
  775. xscale->arch_debug_reason = XSCALE_DBG_REASON_GENERIC;
  776. pc -= 4;
  777. break;
  778. case 0x2: /* Data breakpoint hit */
  779. target->debug_reason = DBG_REASON_WATCHPOINT;
  780. xscale->arch_debug_reason = XSCALE_DBG_REASON_GENERIC;
  781. pc -= 4;
  782. break;
  783. case 0x3: /* BKPT instruction executed */
  784. target->debug_reason = DBG_REASON_BREAKPOINT;
  785. xscale->arch_debug_reason = XSCALE_DBG_REASON_GENERIC;
  786. pc -= 4;
  787. break;
  788. case 0x4: /* Ext. debug event */
  789. target->debug_reason = DBG_REASON_DBGRQ;
  790. xscale->arch_debug_reason = XSCALE_DBG_REASON_GENERIC;
  791. pc -= 4;
  792. break;
  793. case 0x5: /* Vector trap occured */
  794. target->debug_reason = DBG_REASON_BREAKPOINT;
  795. xscale->arch_debug_reason = XSCALE_DBG_REASON_GENERIC;
  796. pc -= 4;
  797. break;
  798. case 0x6: /* Trace buffer full break */
  799. target->debug_reason = DBG_REASON_DBGRQ;
  800. xscale->arch_debug_reason = XSCALE_DBG_REASON_TB_FULL;
  801. pc -= 4;
  802. break;
  803. case 0x7: /* Reserved (may flag Hot-Debug support) */
  804. default:
  805. LOG_ERROR("Method of Entry is 'Reserved'");
  806. exit(-1);
  807. break;
  808. }
  809. /* apply PC fixup */
  810. buf_set_u32(arm->pc->value, 0, 32, pc);
  811. /* on the first debug entry, identify cache type */
  812. if (xscale->armv4_5_mmu.armv4_5_cache.ctype == -1) {
  813. uint32_t cache_type_reg;
  814. /* read cp15 cache type register */
  815. xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_CACHETYPE]);
  816. cache_type_reg = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CACHETYPE].value,
  817. 0,
  818. 32);
  819. armv4_5_identify_cache(cache_type_reg, &xscale->armv4_5_mmu.armv4_5_cache);
  820. }
  821. /* examine MMU and Cache settings
  822. * read cp15 control register */
  823. xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_CTRL]);
  824. xscale->cp15_control_reg =
  825. buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CTRL].value, 0, 32);
  826. xscale->armv4_5_mmu.mmu_enabled = (xscale->cp15_control_reg & 0x1U) ? 1 : 0;
  827. xscale->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled =
  828. (xscale->cp15_control_reg & 0x4U) ? 1 : 0;
  829. xscale->armv4_5_mmu.armv4_5_cache.i_cache_enabled =
  830. (xscale->cp15_control_reg & 0x1000U) ? 1 : 0;
  831. /* tracing enabled, read collected trace data */
  832. if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
  833. xscale_read_trace(target);
  834. /* Resume if entered debug due to buffer fill and we're still collecting
  835. * trace data. Note that a debug exception due to trace buffer full
  836. * can only happen in fill mode. */
  837. if (xscale->arch_debug_reason == XSCALE_DBG_REASON_TB_FULL) {
  838. if (--xscale->trace.fill_counter > 0)
  839. xscale_resume(target, 1, 0x0, 1, 0);
  840. } else /* entered debug for other reason; reset counter */
  841. xscale->trace.fill_counter = 0;
  842. }
  843. return ERROR_OK;
  844. }
  845. static int xscale_halt(struct target *target)
  846. {
  847. struct xscale_common *xscale = target_to_xscale(target);
  848. LOG_DEBUG("target->state: %s",
  849. target_state_name(target));
  850. if (target->state == TARGET_HALTED) {
  851. LOG_DEBUG("target was already halted");
  852. return ERROR_OK;
  853. } else if (target->state == TARGET_UNKNOWN) {
  854. /* this must not happen for a xscale target */
  855. LOG_ERROR("target was in unknown state when halt was requested");
  856. return ERROR_TARGET_INVALID;
  857. } else if (target->state == TARGET_RESET)
  858. LOG_DEBUG("target->state == TARGET_RESET");
  859. else {
  860. /* assert external dbg break */
  861. xscale->external_debug_break = 1;
  862. xscale_read_dcsr(target);
  863. target->debug_reason = DBG_REASON_DBGRQ;
  864. }
  865. return ERROR_OK;
  866. }
  867. static int xscale_enable_single_step(struct target *target, uint32_t next_pc)
  868. {
  869. struct xscale_common *xscale = target_to_xscale(target);
  870. struct reg *ibcr0 = &xscale->reg_cache->reg_list[XSCALE_IBCR0];
  871. int retval;
  872. if (xscale->ibcr0_used) {
  873. struct breakpoint *ibcr0_bp =
  874. breakpoint_find(target, buf_get_u32(ibcr0->value, 0, 32) & 0xfffffffe);
  875. if (ibcr0_bp)
  876. xscale_unset_breakpoint(target, ibcr0_bp);
  877. else {
  878. LOG_ERROR(
  879. "BUG: xscale->ibcr0_used is set, but no breakpoint with that address found");
  880. exit(-1);
  881. }
  882. }
  883. retval = xscale_set_reg_u32(ibcr0, next_pc | 0x1);
  884. if (retval != ERROR_OK)
  885. return retval;
  886. return ERROR_OK;
  887. }
  888. static int xscale_disable_single_step(struct target *target)
  889. {
  890. struct xscale_common *xscale = target_to_xscale(target);
  891. struct reg *ibcr0 = &xscale->reg_cache->reg_list[XSCALE_IBCR0];
  892. int retval;
  893. retval = xscale_set_reg_u32(ibcr0, 0x0);
  894. if (retval != ERROR_OK)
  895. return retval;
  896. return ERROR_OK;
  897. }
  898. static void xscale_enable_watchpoints(struct target *target)
  899. {
  900. struct watchpoint *watchpoint = target->watchpoints;
  901. while (watchpoint) {
  902. if (watchpoint->set == 0)
  903. xscale_set_watchpoint(target, watchpoint);
  904. watchpoint = watchpoint->next;
  905. }
  906. }
  907. static void xscale_enable_breakpoints(struct target *target)
  908. {
  909. struct breakpoint *breakpoint = target->breakpoints;
  910. /* set any pending breakpoints */
  911. while (breakpoint) {
  912. if (breakpoint->set == 0)
  913. xscale_set_breakpoint(target, breakpoint);
  914. breakpoint = breakpoint->next;
  915. }
  916. }
  917. static void xscale_free_trace_data(struct xscale_common *xscale)
  918. {
  919. struct xscale_trace_data *td = xscale->trace.data;
  920. while (td) {
  921. struct xscale_trace_data *next_td = td->next;
  922. if (td->entries)
  923. free(td->entries);
  924. free(td);
  925. td = next_td;
  926. }
  927. xscale->trace.data = NULL;
  928. }
  929. static int xscale_resume(struct target *target, int current,
  930. uint32_t address, int handle_breakpoints, int debug_execution)
  931. {
  932. struct xscale_common *xscale = target_to_xscale(target);
  933. struct arm *arm = &xscale->arm;
  934. uint32_t current_pc;
  935. int retval;
  936. int i;
  937. LOG_DEBUG("-");
  938. if (target->state != TARGET_HALTED) {
  939. LOG_WARNING("target not halted");
  940. return ERROR_TARGET_NOT_HALTED;
  941. }
  942. if (!debug_execution)
  943. target_free_all_working_areas(target);
  944. /* update vector tables */
  945. retval = xscale_update_vectors(target);
  946. if (retval != ERROR_OK)
  947. return retval;
  948. /* current = 1: continue on current pc, otherwise continue at <address> */
  949. if (!current)
  950. buf_set_u32(arm->pc->value, 0, 32, address);
  951. current_pc = buf_get_u32(arm->pc->value, 0, 32);
  952. /* if we're at the reset vector, we have to simulate the branch */
  953. if (current_pc == 0x0) {
  954. arm_simulate_step(target, NULL);
  955. current_pc = buf_get_u32(arm->pc->value, 0, 32);
  956. }
  957. /* the front-end may request us not to handle breakpoints */
  958. if (handle_breakpoints) {
  959. struct breakpoint *breakpoint;
  960. breakpoint = breakpoint_find(target,
  961. buf_get_u32(arm->pc->value, 0, 32));
  962. if (breakpoint != NULL) {
  963. uint32_t next_pc;
  964. enum trace_mode saved_trace_mode;
  965. /* there's a breakpoint at the current PC, we have to step over it */
  966. LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
  967. xscale_unset_breakpoint(target, breakpoint);
  968. /* calculate PC of next instruction */
  969. retval = arm_simulate_step(target, &next_pc);
  970. if (retval != ERROR_OK) {
  971. uint32_t current_opcode;
  972. target_read_u32(target, current_pc, &current_opcode);
  973. LOG_ERROR(
  974. "BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "",
  975. current_opcode);
  976. }
  977. LOG_DEBUG("enable single-step");
  978. xscale_enable_single_step(target, next_pc);
  979. /* restore banked registers */
  980. retval = xscale_restore_banked(target);
  981. if (retval != ERROR_OK)
  982. return retval;
  983. /* send resume request */
  984. xscale_send_u32(target, 0x30);
  985. /* send CPSR */
  986. xscale_send_u32(target,
  987. buf_get_u32(arm->cpsr->value, 0, 32));
  988. LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32,
  989. buf_get_u32(arm->cpsr->value, 0, 32));
  990. for (i = 7; i >= 0; i--) {
  991. /* send register */
  992. xscale_send_u32(target,
  993. buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
  994. LOG_DEBUG("writing r%i with value 0x%8.8" PRIx32 "",
  995. i, buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
  996. }
  997. /* send PC */
  998. xscale_send_u32(target,
  999. buf_get_u32(arm->pc->value, 0, 32));
  1000. LOG_DEBUG("writing PC with value 0x%8.8" PRIx32,
  1001. buf_get_u32(arm->pc->value, 0, 32));
  1002. /* disable trace data collection in xscale_debug_entry() */
  1003. saved_trace_mode = xscale->trace.mode;
  1004. xscale->trace.mode = XSCALE_TRACE_DISABLED;
  1005. /* wait for and process debug entry */
  1006. xscale_debug_entry(target);
  1007. /* re-enable trace buffer, if enabled previously */
  1008. xscale->trace.mode = saved_trace_mode;
  1009. LOG_DEBUG("disable single-step");
  1010. xscale_disable_single_step(target);
  1011. LOG_DEBUG("set breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
  1012. xscale_set_breakpoint(target, breakpoint);
  1013. }
  1014. }
  1015. /* enable any pending breakpoints and watchpoints */
  1016. xscale_enable_breakpoints(target);
  1017. xscale_enable_watchpoints(target);
  1018. /* restore banked registers */
  1019. retval = xscale_restore_banked(target);
  1020. if (retval != ERROR_OK)
  1021. return retval;
  1022. /* send resume request (command 0x30 or 0x31)
  1023. * clean the trace buffer if it is to be enabled (0x62) */
  1024. if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
  1025. if (xscale->trace.mode == XSCALE_TRACE_FILL) {
  1026. /* If trace enabled in fill mode and starting collection of new set
  1027. * of buffers, initialize buffer counter and free previous buffers */
  1028. if (xscale->trace.fill_counter == 0) {
  1029. xscale->trace.fill_counter = xscale->trace.buffer_fill;
  1030. xscale_free_trace_data(xscale);
  1031. }
  1032. } else /* wrap mode; free previous buffer */
  1033. xscale_free_trace_data(xscale);
  1034. xscale_send_u32(target, 0x62);
  1035. xscale_send_u32(target, 0x31);
  1036. } else
  1037. xscale_send_u32(target, 0x30);
  1038. /* send CPSR */
  1039. xscale_send_u32(target, buf_get_u32(arm->cpsr->value, 0, 32));
  1040. LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32,
  1041. buf_get_u32(arm->cpsr->value, 0, 32));
  1042. for (i = 7; i >= 0; i--) {
  1043. /* send register */
  1044. xscale_send_u32(target, buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
  1045. LOG_DEBUG("writing r%i with value 0x%8.8" PRIx32 "",
  1046. i, buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
  1047. }
  1048. /* send PC */
  1049. xscale_send_u32(target, buf_get_u32(arm->pc->value, 0, 32));
  1050. LOG_DEBUG("wrote PC with value 0x%8.8" PRIx32,
  1051. buf_get_u32(arm->pc->value, 0, 32));
  1052. target->debug_reason = DBG_REASON_NOTHALTED;
  1053. if (!debug_execution) {
  1054. /* registers are now invalid */
  1055. register_cache_invalidate(arm->core_cache);
  1056. target->state = TARGET_RUNNING;
  1057. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  1058. } else {
  1059. target->state = TARGET_DEBUG_RUNNING;
  1060. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  1061. }
  1062. LOG_DEBUG("target resumed");
  1063. return ERROR_OK;
  1064. }
  1065. static int xscale_step_inner(struct target *target, int current,
  1066. uint32_t address, int handle_breakpoints)
  1067. {
  1068. struct xscale_common *xscale = target_to_xscale(target);
  1069. struct arm *arm = &xscale->arm;
  1070. uint32_t next_pc;
  1071. int retval;
  1072. int i;
  1073. target->debug_reason = DBG_REASON_SINGLESTEP;
  1074. /* calculate PC of next instruction */
  1075. retval = arm_simulate_step(target, &next_pc);
  1076. if (retval != ERROR_OK) {
  1077. uint32_t current_opcode, current_pc;
  1078. current_pc = buf_get_u32(arm->pc->value, 0, 32);
  1079. target_read_u32(target, current_pc, &current_opcode);
  1080. LOG_ERROR(
  1081. "BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "",
  1082. current_opcode);
  1083. return retval;
  1084. }
  1085. LOG_DEBUG("enable single-step");
  1086. retval = xscale_enable_single_step(target, next_pc);
  1087. if (retval != ERROR_OK)
  1088. return retval;
  1089. /* restore banked registers */
  1090. retval = xscale_restore_banked(target);
  1091. if (retval != ERROR_OK)
  1092. return retval;
  1093. /* send resume request (command 0x30 or 0x31)
  1094. * clean the trace buffer if it is to be enabled (0x62) */
  1095. if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
  1096. retval = xscale_send_u32(target, 0x62);
  1097. if (retval != ERROR_OK)
  1098. return retval;
  1099. retval = xscale_send_u32(target, 0x31);
  1100. if (retval != ERROR_OK)
  1101. return retval;
  1102. } else {
  1103. retval = xscale_send_u32(target, 0x30);
  1104. if (retval != ERROR_OK)
  1105. return retval;
  1106. }
  1107. /* send CPSR */
  1108. retval = xscale_send_u32(target,
  1109. buf_get_u32(arm->cpsr->value, 0, 32));
  1110. if (retval != ERROR_OK)
  1111. return retval;
  1112. LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32,
  1113. buf_get_u32(arm->cpsr->value, 0, 32));
  1114. for (i = 7; i >= 0; i--) {
  1115. /* send register */
  1116. retval = xscale_send_u32(target,
  1117. buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
  1118. if (retval != ERROR_OK)
  1119. return retval;
  1120. LOG_DEBUG("writing r%i with value 0x%8.8" PRIx32 "", i,
  1121. buf_get_u32(arm->core_cache->reg_list[i].value, 0, 32));
  1122. }
  1123. /* send PC */
  1124. retval = xscale_send_u32(target,
  1125. buf_get_u32(arm->pc->value, 0, 32));
  1126. if (retval != ERROR_OK)
  1127. return retval;
  1128. LOG_DEBUG("wrote PC with value 0x%8.8" PRIx32,
  1129. buf_get_u32(arm->pc->value, 0, 32));
  1130. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  1131. /* registers are now invalid */
  1132. register_cache_invalidate(arm->core_cache);
  1133. /* wait for and process debug entry */
  1134. retval = xscale_debug_entry(target);
  1135. if (retval != ERROR_OK)
  1136. return retval;
  1137. LOG_DEBUG("disable single-step");
  1138. retval = xscale_disable_single_step(target);
  1139. if (retval != ERROR_OK)
  1140. return retval;
  1141. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  1142. return ERROR_OK;
  1143. }
  1144. static int xscale_step(struct target *target, int current,
  1145. uint32_t address, int handle_breakpoints)
  1146. {
  1147. struct arm *arm = target_to_arm(target);
  1148. struct breakpoint *breakpoint = NULL;
  1149. uint32_t current_pc;
  1150. int retval;
  1151. if (target->state != TARGET_HALTED) {
  1152. LOG_WARNING("target not halted");
  1153. return ERROR_TARGET_NOT_HALTED;
  1154. }
  1155. /* current = 1: continue on current pc, otherwise continue at <address> */
  1156. if (!current)
  1157. buf_set_u32(arm->pc->value, 0, 32, address);
  1158. current_pc = buf_get_u32(arm->pc->value, 0, 32);
  1159. /* if we're at the reset vector, we have to simulate the step */
  1160. if (current_pc == 0x0) {
  1161. retval = arm_simulate_step(target, NULL);
  1162. if (retval != ERROR_OK)
  1163. return retval;
  1164. current_pc = buf_get_u32(arm->pc->value, 0, 32);
  1165. LOG_DEBUG("current pc %" PRIx32, current_pc);
  1166. target->debug_reason = DBG_REASON_SINGLESTEP;
  1167. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  1168. return ERROR_OK;
  1169. }
  1170. /* the front-end may request us not to handle breakpoints */
  1171. if (handle_breakpoints)
  1172. breakpoint = breakpoint_find(target,
  1173. buf_get_u32(arm->pc->value, 0, 32));
  1174. if (breakpoint != NULL) {
  1175. retval = xscale_unset_breakpoint(target, breakpoint);
  1176. if (retval != ERROR_OK)
  1177. return retval;
  1178. }
  1179. retval = xscale_step_inner(target, current, address, handle_breakpoints);
  1180. if (retval != ERROR_OK)
  1181. return retval;
  1182. if (breakpoint)
  1183. xscale_set_breakpoint(target, breakpoint);
  1184. LOG_DEBUG("target stepped");
  1185. return ERROR_OK;
  1186. }
  1187. static int xscale_assert_reset(struct target *target)
  1188. {
  1189. struct xscale_common *xscale = target_to_xscale(target);
  1190. LOG_DEBUG("target->state: %s",
  1191. target_state_name(target));
  1192. /* select DCSR instruction (set endstate to R-T-I to ensure we don't
  1193. * end up in T-L-R, which would reset JTAG
  1194. */
  1195. xscale_jtag_set_instr(target->tap,
  1196. XSCALE_SELDCSR << xscale->xscale_variant,
  1197. TAP_IDLE);
  1198. /* set Hold reset, Halt mode and Trap Reset */
  1199. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 30, 1, 0x1);
  1200. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 16, 1, 0x1);
  1201. xscale_write_dcsr(target, 1, 0);
  1202. /* select BYPASS, because having DCSR selected caused problems on the PXA27x */
  1203. xscale_jtag_set_instr(target->tap, ~0, TAP_IDLE);
  1204. jtag_execute_queue();
  1205. /* assert reset */
  1206. jtag_add_reset(0, 1);
  1207. /* sleep 1ms, to be sure we fulfill any requirements */
  1208. jtag_add_sleep(1000);
  1209. jtag_execute_queue();
  1210. target->state = TARGET_RESET;
  1211. if (target->reset_halt) {
  1212. int retval = target_halt(target);
  1213. if (retval != ERROR_OK)
  1214. return retval;
  1215. }
  1216. return ERROR_OK;
  1217. }
  1218. static int xscale_deassert_reset(struct target *target)
  1219. {
  1220. struct xscale_common *xscale = target_to_xscale(target);
  1221. struct breakpoint *breakpoint = target->breakpoints;
  1222. LOG_DEBUG("-");
  1223. xscale->ibcr_available = 2;
  1224. xscale->ibcr0_used = 0;
  1225. xscale->ibcr1_used = 0;
  1226. xscale->dbr_available = 2;
  1227. xscale->dbr0_used = 0;
  1228. xscale->dbr1_used = 0;
  1229. /* mark all hardware breakpoints as unset */
  1230. while (breakpoint) {
  1231. if (breakpoint->type == BKPT_HARD)
  1232. breakpoint->set = 0;
  1233. breakpoint = breakpoint->next;
  1234. }
  1235. xscale->trace.mode = XSCALE_TRACE_DISABLED;
  1236. xscale_free_trace_data(xscale);
  1237. register_cache_invalidate(xscale->arm.core_cache);
  1238. /* FIXME mark hardware watchpoints got unset too. Also,
  1239. * at least some of the XScale registers are invalid...
  1240. */
  1241. /*
  1242. * REVISIT: *assumes* we had a SRST+TRST reset so the mini-icache
  1243. * contents got invalidated. Safer to force that, so writing new
  1244. * contents can't ever fail..
  1245. */
  1246. {
  1247. uint32_t address;
  1248. unsigned buf_cnt;
  1249. const uint8_t *buffer = xscale_debug_handler;
  1250. int retval;
  1251. /* release SRST */
  1252. jtag_add_reset(0, 0);
  1253. /* wait 300ms; 150 and 100ms were not enough */
  1254. jtag_add_sleep(300*1000);
  1255. jtag_add_runtest(2030, TAP_IDLE);
  1256. jtag_execute_queue();
  1257. /* set Hold reset, Halt mode and Trap Reset */
  1258. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 30, 1, 0x1);
  1259. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 16, 1, 0x1);
  1260. xscale_write_dcsr(target, 1, 0);
  1261. /* Load the debug handler into the mini-icache. Since
  1262. * it's using halt mode (not monitor mode), it runs in
  1263. * "Special Debug State" for access to registers, memory,
  1264. * coprocessors, trace data, etc.
  1265. */
  1266. address = xscale->handler_address;
  1267. for (unsigned binary_size = sizeof xscale_debug_handler - 1;
  1268. binary_size > 0;
  1269. binary_size -= buf_cnt, buffer += buf_cnt) {
  1270. uint32_t cache_line[8];
  1271. unsigned i;
  1272. buf_cnt = binary_size;
  1273. if (buf_cnt > 32)
  1274. buf_cnt = 32;
  1275. for (i = 0; i < buf_cnt; i += 4) {
  1276. /* convert LE buffer to host-endian uint32_t */
  1277. cache_line[i / 4] = le_to_h_u32(&buffer[i]);
  1278. }
  1279. for (; i < 32; i += 4)
  1280. cache_line[i / 4] = 0xe1a08008;
  1281. /* only load addresses other than the reset vectors */
  1282. if ((address % 0x400) != 0x0) {
  1283. retval = xscale_load_ic(target, address,
  1284. cache_line);
  1285. if (retval != ERROR_OK)
  1286. return retval;
  1287. }
  1288. address += buf_cnt;
  1289. }
  1290. ;
  1291. retval = xscale_load_ic(target, 0x0,
  1292. xscale->low_vectors);
  1293. if (retval != ERROR_OK)
  1294. return retval;
  1295. retval = xscale_load_ic(target, 0xffff0000,
  1296. xscale->high_vectors);
  1297. if (retval != ERROR_OK)
  1298. return retval;
  1299. jtag_add_runtest(30, TAP_IDLE);
  1300. jtag_add_sleep(100000);
  1301. /* set Hold reset, Halt mode and Trap Reset */
  1302. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 30, 1, 0x1);
  1303. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 16, 1, 0x1);
  1304. xscale_write_dcsr(target, 1, 0);
  1305. /* clear Hold reset to let the target run (should enter debug handler) */
  1306. xscale_write_dcsr(target, 0, 1);
  1307. target->state = TARGET_RUNNING;
  1308. if (!target->reset_halt) {
  1309. jtag_add_sleep(10000);
  1310. /* we should have entered debug now */
  1311. xscale_debug_entry(target);
  1312. target->state = TARGET_HALTED;
  1313. /* resume the target */
  1314. xscale_resume(target, 1, 0x0, 1, 0);
  1315. }
  1316. }
  1317. return ERROR_OK;
  1318. }
  1319. static int xscale_read_core_reg(struct target *target, struct reg *r,
  1320. int num, enum arm_mode mode)
  1321. {
  1322. /** \todo add debug handler support for core register reads */
  1323. LOG_ERROR("not implemented");
  1324. return ERROR_OK;
  1325. }
  1326. static int xscale_write_core_reg(struct target *target, struct reg *r,
  1327. int num, enum arm_mode mode, uint32_t value)
  1328. {
  1329. /** \todo add debug handler support for core register writes */
  1330. LOG_ERROR("not implemented");
  1331. return ERROR_OK;
  1332. }
  1333. static int xscale_full_context(struct target *target)
  1334. {
  1335. struct arm *arm = target_to_arm(target);
  1336. uint32_t *buffer;
  1337. int i, j;
  1338. LOG_DEBUG("-");
  1339. if (target->state != TARGET_HALTED) {
  1340. LOG_WARNING("target not halted");
  1341. return ERROR_TARGET_NOT_HALTED;
  1342. }
  1343. buffer = malloc(4 * 8);
  1344. /* iterate through processor modes (FIQ, IRQ, SVC, ABT, UND and SYS)
  1345. * we can't enter User mode on an XScale (unpredictable),
  1346. * but User shares registers with SYS
  1347. */
  1348. for (i = 1; i < 7; i++) {
  1349. enum arm_mode mode = armv4_5_number_to_mode(i);
  1350. bool valid = true;
  1351. struct reg *r;
  1352. if (mode == ARM_MODE_USR)
  1353. continue;
  1354. /* check if there are invalid registers in the current mode
  1355. */
  1356. for (j = 0; valid && j <= 16; j++) {
  1357. if (!ARMV4_5_CORE_REG_MODE(arm->core_cache,
  1358. mode, j).valid)
  1359. valid = false;
  1360. }
  1361. if (valid)
  1362. continue;
  1363. /* request banked registers */
  1364. xscale_send_u32(target, 0x0);
  1365. /* send CPSR for desired bank mode */
  1366. xscale_send_u32(target, mode | 0xc0 /* I/F bits */);
  1367. /* get banked registers: r8 to r14; and SPSR
  1368. * except in USR/SYS mode
  1369. */
  1370. if (mode != ARM_MODE_SYS) {
  1371. /* SPSR */
  1372. r = &ARMV4_5_CORE_REG_MODE(arm->core_cache,
  1373. mode, 16);
  1374. xscale_receive(target, buffer, 8);
  1375. buf_set_u32(r->value, 0, 32, buffer[7]);
  1376. r->dirty = false;
  1377. r->valid = true;
  1378. } else
  1379. xscale_receive(target, buffer, 7);
  1380. /* move data from buffer to register cache */
  1381. for (j = 8; j <= 14; j++) {
  1382. r = &ARMV4_5_CORE_REG_MODE(arm->core_cache,
  1383. mode, j);
  1384. buf_set_u32(r->value, 0, 32, buffer[j - 8]);
  1385. r->dirty = false;
  1386. r->valid = true;
  1387. }
  1388. }
  1389. free(buffer);
  1390. return ERROR_OK;
  1391. }
  1392. static int xscale_restore_banked(struct target *target)
  1393. {
  1394. struct arm *arm = target_to_arm(target);
  1395. int i, j;
  1396. if (target->state != TARGET_HALTED) {
  1397. LOG_WARNING("target not halted");
  1398. return ERROR_TARGET_NOT_HALTED;
  1399. }
  1400. /* iterate through processor modes (FIQ, IRQ, SVC, ABT, UND and SYS)
  1401. * and check if any banked registers need to be written. Ignore
  1402. * USR mode (number 0) in favor of SYS; we can't enter User mode on
  1403. * an XScale (unpredictable), but they share all registers.
  1404. */
  1405. for (i = 1; i < 7; i++) {
  1406. enum arm_mode mode = armv4_5_number_to_mode(i);
  1407. struct reg *r;
  1408. if (mode == ARM_MODE_USR)
  1409. continue;
  1410. /* check if there are dirty registers in this mode */
  1411. for (j = 8; j <= 14; j++) {
  1412. if (ARMV4_5_CORE_REG_MODE(arm->core_cache,
  1413. mode, j).dirty)
  1414. goto dirty;
  1415. }
  1416. /* if not USR/SYS, check if the SPSR needs to be written */
  1417. if (mode != ARM_MODE_SYS) {
  1418. if (ARMV4_5_CORE_REG_MODE(arm->core_cache,
  1419. mode, 16).dirty)
  1420. goto dirty;
  1421. }
  1422. /* there's nothing to flush for this mode */
  1423. continue;
  1424. dirty:
  1425. /* command 0x1: "send banked registers" */
  1426. xscale_send_u32(target, 0x1);
  1427. /* send CPSR for desired mode */
  1428. xscale_send_u32(target, mode | 0xc0 /* I/F bits */);
  1429. /* send r8 to r14/lr ... only FIQ needs more than r13..r14,
  1430. * but this protocol doesn't understand that nuance.
  1431. */
  1432. for (j = 8; j <= 14; j++) {
  1433. r = &ARMV4_5_CORE_REG_MODE(arm->core_cache,
  1434. mode, j);
  1435. xscale_send_u32(target, buf_get_u32(r->value, 0, 32));
  1436. r->dirty = false;
  1437. }
  1438. /* send spsr if not in USR/SYS mode */
  1439. if (mode != ARM_MODE_SYS) {
  1440. r = &ARMV4_5_CORE_REG_MODE(arm->core_cache,
  1441. mode, 16);
  1442. xscale_send_u32(target, buf_get_u32(r->value, 0, 32));
  1443. r->dirty = false;
  1444. }
  1445. }
  1446. return ERROR_OK;
  1447. }
  1448. static int xscale_read_memory(struct target *target, uint32_t address,
  1449. uint32_t size, uint32_t count, uint8_t *buffer)
  1450. {
  1451. struct xscale_common *xscale = target_to_xscale(target);
  1452. uint32_t *buf32;
  1453. uint32_t i;
  1454. int retval;
  1455. LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32,
  1456. address,
  1457. size,
  1458. count);
  1459. if (target->state != TARGET_HALTED) {
  1460. LOG_WARNING("target not halted");
  1461. return ERROR_TARGET_NOT_HALTED;
  1462. }
  1463. /* sanitize arguments */
  1464. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  1465. return ERROR_COMMAND_SYNTAX_ERROR;
  1466. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  1467. return ERROR_TARGET_UNALIGNED_ACCESS;
  1468. /* send memory read request (command 0x1n, n: access size) */
  1469. retval = xscale_send_u32(target, 0x10 | size);
  1470. if (retval != ERROR_OK)
  1471. return retval;
  1472. /* send base address for read request */
  1473. retval = xscale_send_u32(target, address);
  1474. if (retval != ERROR_OK)
  1475. return retval;
  1476. /* send number of requested data words */
  1477. retval = xscale_send_u32(target, count);
  1478. if (retval != ERROR_OK)
  1479. return retval;
  1480. /* receive data from target (count times 32-bit words in host endianness) */
  1481. buf32 = malloc(4 * count);
  1482. retval = xscale_receive(target, buf32, count);
  1483. if (retval != ERROR_OK)
  1484. return retval;
  1485. /* extract data from host-endian buffer into byte stream */
  1486. for (i = 0; i < count; i++) {
  1487. switch (size) {
  1488. case 4:
  1489. target_buffer_set_u32(target, buffer, buf32[i]);
  1490. buffer += 4;
  1491. break;
  1492. case 2:
  1493. target_buffer_set_u16(target, buffer, buf32[i] & 0xffff);
  1494. buffer += 2;
  1495. break;
  1496. case 1:
  1497. *buffer++ = buf32[i] & 0xff;
  1498. break;
  1499. default:
  1500. LOG_ERROR("invalid read size");
  1501. return ERROR_COMMAND_SYNTAX_ERROR;
  1502. }
  1503. }
  1504. free(buf32);
  1505. /* examine DCSR, to see if Sticky Abort (SA) got set */
  1506. retval = xscale_read_dcsr(target);
  1507. if (retval != ERROR_OK)
  1508. return retval;
  1509. if (buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 5, 1) == 1) {
  1510. /* clear SA bit */
  1511. retval = xscale_send_u32(target, 0x60);
  1512. if (retval != ERROR_OK)
  1513. return retval;
  1514. return ERROR_TARGET_DATA_ABORT;
  1515. }
  1516. return ERROR_OK;
  1517. }
  1518. static int xscale_read_phys_memory(struct target *target, uint32_t address,
  1519. uint32_t size, uint32_t count, uint8_t *buffer)
  1520. {
  1521. struct xscale_common *xscale = target_to_xscale(target);
  1522. /* with MMU inactive, there are only physical addresses */
  1523. if (!xscale->armv4_5_mmu.mmu_enabled)
  1524. return xscale_read_memory(target, address, size, count, buffer);
  1525. /** \todo: provide a non-stub implementation of this routine. */
  1526. LOG_ERROR("%s: %s is not implemented. Disable MMU?",
  1527. target_name(target), __func__);
  1528. return ERROR_FAIL;
  1529. }
  1530. static int xscale_write_memory(struct target *target, uint32_t address,
  1531. uint32_t size, uint32_t count, const uint8_t *buffer)
  1532. {
  1533. struct xscale_common *xscale = target_to_xscale(target);
  1534. int retval;
  1535. LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32,
  1536. address,
  1537. size,
  1538. count);
  1539. if (target->state != TARGET_HALTED) {
  1540. LOG_WARNING("target not halted");
  1541. return ERROR_TARGET_NOT_HALTED;
  1542. }
  1543. /* sanitize arguments */
  1544. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  1545. return ERROR_COMMAND_SYNTAX_ERROR;
  1546. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  1547. return ERROR_TARGET_UNALIGNED_ACCESS;
  1548. /* send memory write request (command 0x2n, n: access size) */
  1549. retval = xscale_send_u32(target, 0x20 | size);
  1550. if (retval != ERROR_OK)
  1551. return retval;
  1552. /* send base address for read request */
  1553. retval = xscale_send_u32(target, address);
  1554. if (retval != ERROR_OK)
  1555. return retval;
  1556. /* send number of requested data words to be written*/
  1557. retval = xscale_send_u32(target, count);
  1558. if (retval != ERROR_OK)
  1559. return retval;
  1560. /* extract data from host-endian buffer into byte stream */
  1561. #if 0
  1562. for (i = 0; i < count; i++) {
  1563. switch (size) {
  1564. case 4:
  1565. value = target_buffer_get_u32(target, buffer);
  1566. xscale_send_u32(target, value);
  1567. buffer += 4;
  1568. break;
  1569. case 2:
  1570. value = target_buffer_get_u16(target, buffer);
  1571. xscale_send_u32(target, value);
  1572. buffer += 2;
  1573. break;
  1574. case 1:
  1575. value = *buffer;
  1576. xscale_send_u32(target, value);
  1577. buffer += 1;
  1578. break;
  1579. default:
  1580. LOG_ERROR("should never get here");
  1581. exit(-1);
  1582. }
  1583. }
  1584. #endif
  1585. retval = xscale_send(target, buffer, count, size);
  1586. if (retval != ERROR_OK)
  1587. return retval;
  1588. /* examine DCSR, to see if Sticky Abort (SA) got set */
  1589. retval = xscale_read_dcsr(target);
  1590. if (retval != ERROR_OK)
  1591. return retval;
  1592. if (buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 5, 1) == 1) {
  1593. /* clear SA bit */
  1594. retval = xscale_send_u32(target, 0x60);
  1595. if (retval != ERROR_OK)
  1596. return retval;
  1597. LOG_ERROR("data abort writing memory");
  1598. return ERROR_TARGET_DATA_ABORT;
  1599. }
  1600. return ERROR_OK;
  1601. }
  1602. static int xscale_write_phys_memory(struct target *target, uint32_t address,
  1603. uint32_t size, uint32_t count, const uint8_t *buffer)
  1604. {
  1605. struct xscale_common *xscale = target_to_xscale(target);
  1606. /* with MMU inactive, there are only physical addresses */
  1607. if (!xscale->armv4_5_mmu.mmu_enabled)
  1608. return xscale_write_memory(target, address, size, count, buffer);
  1609. /** \todo: provide a non-stub implementation of this routine. */
  1610. LOG_ERROR("%s: %s is not implemented. Disable MMU?",
  1611. target_name(target), __func__);
  1612. return ERROR_FAIL;
  1613. }
  1614. static int xscale_get_ttb(struct target *target, uint32_t *result)
  1615. {
  1616. struct xscale_common *xscale = target_to_xscale(target);
  1617. uint32_t ttb;
  1618. int retval;
  1619. retval = xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_TTB]);
  1620. if (retval != ERROR_OK)
  1621. return retval;
  1622. ttb = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_TTB].value, 0, 32);
  1623. *result = ttb;
  1624. return ERROR_OK;
  1625. }
  1626. static int xscale_disable_mmu_caches(struct target *target, int mmu,
  1627. int d_u_cache, int i_cache)
  1628. {
  1629. struct xscale_common *xscale = target_to_xscale(target);
  1630. uint32_t cp15_control;
  1631. int retval;
  1632. /* read cp15 control register */
  1633. retval = xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_CTRL]);
  1634. if (retval != ERROR_OK)
  1635. return retval;
  1636. cp15_control = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CTRL].value, 0, 32);
  1637. if (mmu)
  1638. cp15_control &= ~0x1U;
  1639. if (d_u_cache) {
  1640. /* clean DCache */
  1641. retval = xscale_send_u32(target, 0x50);
  1642. if (retval != ERROR_OK)
  1643. return retval;
  1644. retval = xscale_send_u32(target, xscale->cache_clean_address);
  1645. if (retval != ERROR_OK)
  1646. return retval;
  1647. /* invalidate DCache */
  1648. retval = xscale_send_u32(target, 0x51);
  1649. if (retval != ERROR_OK)
  1650. return retval;
  1651. cp15_control &= ~0x4U;
  1652. }
  1653. if (i_cache) {
  1654. /* invalidate ICache */
  1655. retval = xscale_send_u32(target, 0x52);
  1656. if (retval != ERROR_OK)
  1657. return retval;
  1658. cp15_control &= ~0x1000U;
  1659. }
  1660. /* write new cp15 control register */
  1661. retval = xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_CTRL], cp15_control);
  1662. if (retval != ERROR_OK)
  1663. return retval;
  1664. /* execute cpwait to ensure outstanding operations complete */
  1665. retval = xscale_send_u32(target, 0x53);
  1666. return retval;
  1667. }
  1668. static int xscale_enable_mmu_caches(struct target *target, int mmu,
  1669. int d_u_cache, int i_cache)
  1670. {
  1671. struct xscale_common *xscale = target_to_xscale(target);
  1672. uint32_t cp15_control;
  1673. int retval;
  1674. /* read cp15 control register */
  1675. retval = xscale_get_reg(&xscale->reg_cache->reg_list[XSCALE_CTRL]);
  1676. if (retval != ERROR_OK)
  1677. return retval;
  1678. cp15_control = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_CTRL].value, 0, 32);
  1679. if (mmu)
  1680. cp15_control |= 0x1U;
  1681. if (d_u_cache)
  1682. cp15_control |= 0x4U;
  1683. if (i_cache)
  1684. cp15_control |= 0x1000U;
  1685. /* write new cp15 control register */
  1686. retval = xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_CTRL], cp15_control);
  1687. if (retval != ERROR_OK)
  1688. return retval;
  1689. /* execute cpwait to ensure outstanding operations complete */
  1690. retval = xscale_send_u32(target, 0x53);
  1691. return retval;
  1692. }
  1693. static int xscale_set_breakpoint(struct target *target,
  1694. struct breakpoint *breakpoint)
  1695. {
  1696. int retval;
  1697. struct xscale_common *xscale = target_to_xscale(target);
  1698. if (target->state != TARGET_HALTED) {
  1699. LOG_WARNING("target not halted");
  1700. return ERROR_TARGET_NOT_HALTED;
  1701. }
  1702. if (breakpoint->set) {
  1703. LOG_WARNING("breakpoint already set");
  1704. return ERROR_OK;
  1705. }
  1706. if (breakpoint->type == BKPT_HARD) {
  1707. uint32_t value = breakpoint->address | 1;
  1708. if (!xscale->ibcr0_used) {
  1709. xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_IBCR0], value);
  1710. xscale->ibcr0_used = 1;
  1711. breakpoint->set = 1; /* breakpoint set on first breakpoint register */
  1712. } else if (!xscale->ibcr1_used) {
  1713. xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_IBCR1], value);
  1714. xscale->ibcr1_used = 1;
  1715. breakpoint->set = 2; /* breakpoint set on second breakpoint register */
  1716. } else {/* bug: availability previously verified in xscale_add_breakpoint() */
  1717. LOG_ERROR("BUG: no hardware comparator available");
  1718. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1719. }
  1720. } else if (breakpoint->type == BKPT_SOFT) {
  1721. if (breakpoint->length == 4) {
  1722. /* keep the original instruction in target endianness */
  1723. retval = target_read_memory(target, breakpoint->address, 4, 1,
  1724. breakpoint->orig_instr);
  1725. if (retval != ERROR_OK)
  1726. return retval;
  1727. /* write the bkpt instruction in target endianness
  1728. *(arm7_9->arm_bkpt is host endian) */
  1729. retval = target_write_u32(target, breakpoint->address,
  1730. xscale->arm_bkpt);
  1731. if (retval != ERROR_OK)
  1732. return retval;
  1733. } else {
  1734. /* keep the original instruction in target endianness */
  1735. retval = target_read_memory(target, breakpoint->address, 2, 1,
  1736. breakpoint->orig_instr);
  1737. if (retval != ERROR_OK)
  1738. return retval;
  1739. /* write the bkpt instruction in target endianness
  1740. *(arm7_9->arm_bkpt is host endian) */
  1741. retval = target_write_u16(target, breakpoint->address,
  1742. xscale->thumb_bkpt);
  1743. if (retval != ERROR_OK)
  1744. return retval;
  1745. }
  1746. breakpoint->set = 1;
  1747. xscale_send_u32(target, 0x50); /* clean dcache */
  1748. xscale_send_u32(target, xscale->cache_clean_address);
  1749. xscale_send_u32(target, 0x51); /* invalidate dcache */
  1750. xscale_send_u32(target, 0x52); /* invalidate icache and flush fetch buffers */
  1751. }
  1752. return ERROR_OK;
  1753. }
  1754. static int xscale_add_breakpoint(struct target *target,
  1755. struct breakpoint *breakpoint)
  1756. {
  1757. struct xscale_common *xscale = target_to_xscale(target);
  1758. if ((breakpoint->type == BKPT_HARD) && (xscale->ibcr_available < 1)) {
  1759. LOG_ERROR("no breakpoint unit available for hardware breakpoint");
  1760. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1761. }
  1762. if ((breakpoint->length != 2) && (breakpoint->length != 4)) {
  1763. LOG_ERROR("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
  1764. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1765. }
  1766. if (breakpoint->type == BKPT_HARD)
  1767. xscale->ibcr_available--;
  1768. return xscale_set_breakpoint(target, breakpoint);
  1769. }
  1770. static int xscale_unset_breakpoint(struct target *target,
  1771. struct breakpoint *breakpoint)
  1772. {
  1773. int retval;
  1774. struct xscale_common *xscale = target_to_xscale(target);
  1775. if (target->state != TARGET_HALTED) {
  1776. LOG_WARNING("target not halted");
  1777. return ERROR_TARGET_NOT_HALTED;
  1778. }
  1779. if (!breakpoint->set) {
  1780. LOG_WARNING("breakpoint not set");
  1781. return ERROR_OK;
  1782. }
  1783. if (breakpoint->type == BKPT_HARD) {
  1784. if (breakpoint->set == 1) {
  1785. xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_IBCR0], 0x0);
  1786. xscale->ibcr0_used = 0;
  1787. } else if (breakpoint->set == 2) {
  1788. xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_IBCR1], 0x0);
  1789. xscale->ibcr1_used = 0;
  1790. }
  1791. breakpoint->set = 0;
  1792. } else {
  1793. /* restore original instruction (kept in target endianness) */
  1794. if (breakpoint->length == 4) {
  1795. retval = target_write_memory(target, breakpoint->address, 4, 1,
  1796. breakpoint->orig_instr);
  1797. if (retval != ERROR_OK)
  1798. return retval;
  1799. } else {
  1800. retval = target_write_memory(target, breakpoint->address, 2, 1,
  1801. breakpoint->orig_instr);
  1802. if (retval != ERROR_OK)
  1803. return retval;
  1804. }
  1805. breakpoint->set = 0;
  1806. xscale_send_u32(target, 0x50); /* clean dcache */
  1807. xscale_send_u32(target, xscale->cache_clean_address);
  1808. xscale_send_u32(target, 0x51); /* invalidate dcache */
  1809. xscale_send_u32(target, 0x52); /* invalidate icache and flush fetch buffers */
  1810. }
  1811. return ERROR_OK;
  1812. }
  1813. static int xscale_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
  1814. {
  1815. struct xscale_common *xscale = target_to_xscale(target);
  1816. if (target->state != TARGET_HALTED) {
  1817. LOG_ERROR("target not halted");
  1818. return ERROR_TARGET_NOT_HALTED;
  1819. }
  1820. if (breakpoint->set)
  1821. xscale_unset_breakpoint(target, breakpoint);
  1822. if (breakpoint->type == BKPT_HARD)
  1823. xscale->ibcr_available++;
  1824. return ERROR_OK;
  1825. }
  1826. static int xscale_set_watchpoint(struct target *target,
  1827. struct watchpoint *watchpoint)
  1828. {
  1829. struct xscale_common *xscale = target_to_xscale(target);
  1830. uint32_t enable = 0;
  1831. struct reg *dbcon = &xscale->reg_cache->reg_list[XSCALE_DBCON];
  1832. uint32_t dbcon_value = buf_get_u32(dbcon->value, 0, 32);
  1833. if (target->state != TARGET_HALTED) {
  1834. LOG_ERROR("target not halted");
  1835. return ERROR_TARGET_NOT_HALTED;
  1836. }
  1837. switch (watchpoint->rw) {
  1838. case WPT_READ:
  1839. enable = 0x3;
  1840. break;
  1841. case WPT_ACCESS:
  1842. enable = 0x2;
  1843. break;
  1844. case WPT_WRITE:
  1845. enable = 0x1;
  1846. break;
  1847. default:
  1848. LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
  1849. }
  1850. /* For watchpoint across more than one word, both DBR registers must
  1851. be enlisted, with the second used as a mask. */
  1852. if (watchpoint->length > 4) {
  1853. if (xscale->dbr0_used || xscale->dbr1_used) {
  1854. LOG_ERROR("BUG: sufficient hardware comparators unavailable");
  1855. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1856. }
  1857. /* Write mask value to DBR1, based on the length argument.
  1858. * Address bits ignored by the comparator are those set in mask. */
  1859. xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_DBR1],
  1860. watchpoint->length - 1);
  1861. xscale->dbr1_used = 1;
  1862. enable |= 0x100; /* DBCON[M] */
  1863. }
  1864. if (!xscale->dbr0_used) {
  1865. xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_DBR0], watchpoint->address);
  1866. dbcon_value |= enable;
  1867. xscale_set_reg_u32(dbcon, dbcon_value);
  1868. watchpoint->set = 1;
  1869. xscale->dbr0_used = 1;
  1870. } else if (!xscale->dbr1_used) {
  1871. xscale_set_reg_u32(&xscale->reg_cache->reg_list[XSCALE_DBR1], watchpoint->address);
  1872. dbcon_value |= enable << 2;
  1873. xscale_set_reg_u32(dbcon, dbcon_value);
  1874. watchpoint->set = 2;
  1875. xscale->dbr1_used = 1;
  1876. } else {
  1877. LOG_ERROR("BUG: no hardware comparator available");
  1878. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1879. }
  1880. return ERROR_OK;
  1881. }
  1882. static int xscale_add_watchpoint(struct target *target,
  1883. struct watchpoint *watchpoint)
  1884. {
  1885. struct xscale_common *xscale = target_to_xscale(target);
  1886. if (xscale->dbr_available < 1) {
  1887. LOG_ERROR("no more watchpoint registers available");
  1888. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1889. }
  1890. if (watchpoint->value)
  1891. LOG_WARNING("xscale does not support value, mask arguments; ignoring");
  1892. /* check that length is a power of two */
  1893. for (uint32_t len = watchpoint->length; len != 1; len /= 2) {
  1894. if (len % 2) {
  1895. LOG_ERROR("xscale requires that watchpoint length is a power of two");
  1896. return ERROR_COMMAND_ARGUMENT_INVALID;
  1897. }
  1898. }
  1899. if (watchpoint->length == 4) { /* single word watchpoint */
  1900. xscale->dbr_available--;/* one DBR reg used */
  1901. return ERROR_OK;
  1902. }
  1903. /* watchpoints across multiple words require both DBR registers */
  1904. if (xscale->dbr_available < 2) {
  1905. LOG_ERROR("insufficient watchpoint registers available");
  1906. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  1907. }
  1908. if (watchpoint->length > watchpoint->address) {
  1909. LOG_ERROR("xscale does not support watchpoints with length "
  1910. "greater than address");
  1911. return ERROR_COMMAND_ARGUMENT_INVALID;
  1912. }
  1913. xscale->dbr_available = 0;
  1914. return ERROR_OK;
  1915. }
  1916. static int xscale_unset_watchpoint(struct target *target,
  1917. struct watchpoint *watchpoint)
  1918. {
  1919. struct xscale_common *xscale = target_to_xscale(target);
  1920. struct reg *dbcon = &xscale->reg_cache->reg_list[XSCALE_DBCON];
  1921. uint32_t dbcon_value = buf_get_u32(dbcon->value, 0, 32);
  1922. if (target->state != TARGET_HALTED) {
  1923. LOG_WARNING("target not halted");
  1924. return ERROR_TARGET_NOT_HALTED;
  1925. }
  1926. if (!watchpoint->set) {
  1927. LOG_WARNING("breakpoint not set");
  1928. return ERROR_OK;
  1929. }
  1930. if (watchpoint->set == 1) {
  1931. if (watchpoint->length > 4) {
  1932. dbcon_value &= ~0x103; /* clear DBCON[M] as well */
  1933. xscale->dbr1_used = 0; /* DBR1 was used for mask */
  1934. } else
  1935. dbcon_value &= ~0x3;
  1936. xscale_set_reg_u32(dbcon, dbcon_value);
  1937. xscale->dbr0_used = 0;
  1938. } else if (watchpoint->set == 2) {
  1939. dbcon_value &= ~0xc;
  1940. xscale_set_reg_u32(dbcon, dbcon_value);
  1941. xscale->dbr1_used = 0;
  1942. }
  1943. watchpoint->set = 0;
  1944. return ERROR_OK;
  1945. }
  1946. static int xscale_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
  1947. {
  1948. struct xscale_common *xscale = target_to_xscale(target);
  1949. if (target->state != TARGET_HALTED) {
  1950. LOG_ERROR("target not halted");
  1951. return ERROR_TARGET_NOT_HALTED;
  1952. }
  1953. if (watchpoint->set)
  1954. xscale_unset_watchpoint(target, watchpoint);
  1955. if (watchpoint->length > 4)
  1956. xscale->dbr_available++;/* both DBR regs now available */
  1957. xscale->dbr_available++;
  1958. return ERROR_OK;
  1959. }
  1960. static int xscale_get_reg(struct reg *reg)
  1961. {
  1962. struct xscale_reg *arch_info = reg->arch_info;
  1963. struct target *target = arch_info->target;
  1964. struct xscale_common *xscale = target_to_xscale(target);
  1965. /* DCSR, TX and RX are accessible via JTAG */
  1966. if (strcmp(reg->name, "XSCALE_DCSR") == 0)
  1967. return xscale_read_dcsr(arch_info->target);
  1968. else if (strcmp(reg->name, "XSCALE_TX") == 0) {
  1969. /* 1 = consume register content */
  1970. return xscale_read_tx(arch_info->target, 1);
  1971. } else if (strcmp(reg->name, "XSCALE_RX") == 0) {
  1972. /* can't read from RX register (host -> debug handler) */
  1973. return ERROR_OK;
  1974. } else if (strcmp(reg->name, "XSCALE_TXRXCTRL") == 0) {
  1975. /* can't (explicitly) read from TXRXCTRL register */
  1976. return ERROR_OK;
  1977. } else {/* Other DBG registers have to be transfered by the debug handler
  1978. * send CP read request (command 0x40) */
  1979. xscale_send_u32(target, 0x40);
  1980. /* send CP register number */
  1981. xscale_send_u32(target, arch_info->dbg_handler_number);
  1982. /* read register value */
  1983. xscale_read_tx(target, 1);
  1984. buf_cpy(xscale->reg_cache->reg_list[XSCALE_TX].value, reg->value, 32);
  1985. reg->dirty = 0;
  1986. reg->valid = 1;
  1987. }
  1988. return ERROR_OK;
  1989. }
  1990. static int xscale_set_reg(struct reg *reg, uint8_t *buf)
  1991. {
  1992. struct xscale_reg *arch_info = reg->arch_info;
  1993. struct target *target = arch_info->target;
  1994. struct xscale_common *xscale = target_to_xscale(target);
  1995. uint32_t value = buf_get_u32(buf, 0, 32);
  1996. /* DCSR, TX and RX are accessible via JTAG */
  1997. if (strcmp(reg->name, "XSCALE_DCSR") == 0) {
  1998. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 0, 32, value);
  1999. return xscale_write_dcsr(arch_info->target, -1, -1);
  2000. } else if (strcmp(reg->name, "XSCALE_RX") == 0) {
  2001. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_RX].value, 0, 32, value);
  2002. return xscale_write_rx(arch_info->target);
  2003. } else if (strcmp(reg->name, "XSCALE_TX") == 0) {
  2004. /* can't write to TX register (debug-handler -> host) */
  2005. return ERROR_OK;
  2006. } else if (strcmp(reg->name, "XSCALE_TXRXCTRL") == 0) {
  2007. /* can't (explicitly) write to TXRXCTRL register */
  2008. return ERROR_OK;
  2009. } else {/* Other DBG registers have to be transfered by the debug handler
  2010. * send CP write request (command 0x41) */
  2011. xscale_send_u32(target, 0x41);
  2012. /* send CP register number */
  2013. xscale_send_u32(target, arch_info->dbg_handler_number);
  2014. /* send CP register value */
  2015. xscale_send_u32(target, value);
  2016. buf_set_u32(reg->value, 0, 32, value);
  2017. }
  2018. return ERROR_OK;
  2019. }
  2020. static int xscale_write_dcsr_sw(struct target *target, uint32_t value)
  2021. {
  2022. struct xscale_common *xscale = target_to_xscale(target);
  2023. struct reg *dcsr = &xscale->reg_cache->reg_list[XSCALE_DCSR];
  2024. struct xscale_reg *dcsr_arch_info = dcsr->arch_info;
  2025. /* send CP write request (command 0x41) */
  2026. xscale_send_u32(target, 0x41);
  2027. /* send CP register number */
  2028. xscale_send_u32(target, dcsr_arch_info->dbg_handler_number);
  2029. /* send CP register value */
  2030. xscale_send_u32(target, value);
  2031. buf_set_u32(dcsr->value, 0, 32, value);
  2032. return ERROR_OK;
  2033. }
  2034. static int xscale_read_trace(struct target *target)
  2035. {
  2036. struct xscale_common *xscale = target_to_xscale(target);
  2037. struct arm *arm = &xscale->arm;
  2038. struct xscale_trace_data **trace_data_p;
  2039. /* 258 words from debug handler
  2040. * 256 trace buffer entries
  2041. * 2 checkpoint addresses
  2042. */
  2043. uint32_t trace_buffer[258];
  2044. int is_address[256];
  2045. int i, j;
  2046. unsigned int num_checkpoints = 0;
  2047. if (target->state != TARGET_HALTED) {
  2048. LOG_WARNING("target must be stopped to read trace data");
  2049. return ERROR_TARGET_NOT_HALTED;
  2050. }
  2051. /* send read trace buffer command (command 0x61) */
  2052. xscale_send_u32(target, 0x61);
  2053. /* receive trace buffer content */
  2054. xscale_receive(target, trace_buffer, 258);
  2055. /* parse buffer backwards to identify address entries */
  2056. for (i = 255; i >= 0; i--) {
  2057. /* also count number of checkpointed entries */
  2058. if ((trace_buffer[i] & 0xe0) == 0xc0)
  2059. num_checkpoints++;
  2060. is_address[i] = 0;
  2061. if (((trace_buffer[i] & 0xf0) == 0x90) ||
  2062. ((trace_buffer[i] & 0xf0) == 0xd0)) {
  2063. if (i > 0)
  2064. is_address[--i] = 1;
  2065. if (i > 0)
  2066. is_address[--i] = 1;
  2067. if (i > 0)
  2068. is_address[--i] = 1;
  2069. if (i > 0)
  2070. is_address[--i] = 1;
  2071. }
  2072. }
  2073. /* search first non-zero entry that is not part of an address */
  2074. for (j = 0; (j < 256) && (trace_buffer[j] == 0) && (!is_address[j]); j++)
  2075. ;
  2076. if (j == 256) {
  2077. LOG_DEBUG("no trace data collected");
  2078. return ERROR_XSCALE_NO_TRACE_DATA;
  2079. }
  2080. /* account for possible partial address at buffer start (wrap mode only) */
  2081. if (is_address[0]) { /* first entry is address; complete set of 4? */
  2082. i = 1;
  2083. while (i < 4)
  2084. if (!is_address[i++])
  2085. break;
  2086. if (i < 4)
  2087. j += i; /* partial address; can't use it */
  2088. }
  2089. /* if first valid entry is indirect branch, can't use that either (no address) */
  2090. if (((trace_buffer[j] & 0xf0) == 0x90) || ((trace_buffer[j] & 0xf0) == 0xd0))
  2091. j++;
  2092. /* walk linked list to terminating entry */
  2093. for (trace_data_p = &xscale->trace.data; *trace_data_p;
  2094. trace_data_p = &(*trace_data_p)->next)
  2095. ;
  2096. *trace_data_p = malloc(sizeof(struct xscale_trace_data));
  2097. (*trace_data_p)->next = NULL;
  2098. (*trace_data_p)->chkpt0 = trace_buffer[256];
  2099. (*trace_data_p)->chkpt1 = trace_buffer[257];
  2100. (*trace_data_p)->last_instruction = buf_get_u32(arm->pc->value, 0, 32);
  2101. (*trace_data_p)->entries = malloc(sizeof(struct xscale_trace_entry) * (256 - j));
  2102. (*trace_data_p)->depth = 256 - j;
  2103. (*trace_data_p)->num_checkpoints = num_checkpoints;
  2104. for (i = j; i < 256; i++) {
  2105. (*trace_data_p)->entries[i - j].data = trace_buffer[i];
  2106. if (is_address[i])
  2107. (*trace_data_p)->entries[i - j].type = XSCALE_TRACE_ADDRESS;
  2108. else
  2109. (*trace_data_p)->entries[i - j].type = XSCALE_TRACE_MESSAGE;
  2110. }
  2111. return ERROR_OK;
  2112. }
  2113. static int xscale_read_instruction(struct target *target, uint32_t pc,
  2114. struct arm_instruction *instruction)
  2115. {
  2116. struct xscale_common *const xscale = target_to_xscale(target);
  2117. int i;
  2118. int section = -1;
  2119. size_t size_read;
  2120. uint32_t opcode;
  2121. int retval;
  2122. if (!xscale->trace.image)
  2123. return ERROR_TRACE_IMAGE_UNAVAILABLE;
  2124. /* search for the section the current instruction belongs to */
  2125. for (i = 0; i < xscale->trace.image->num_sections; i++) {
  2126. if ((xscale->trace.image->sections[i].base_address <= pc) &&
  2127. (xscale->trace.image->sections[i].base_address +
  2128. xscale->trace.image->sections[i].size > pc)) {
  2129. section = i;
  2130. break;
  2131. }
  2132. }
  2133. if (section == -1) {
  2134. /* current instruction couldn't be found in the image */
  2135. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  2136. }
  2137. if (xscale->trace.core_state == ARM_STATE_ARM) {
  2138. uint8_t buf[4];
  2139. retval = image_read_section(xscale->trace.image, section,
  2140. pc - xscale->trace.image->sections[section].base_address,
  2141. 4, buf, &size_read);
  2142. if (retval != ERROR_OK) {
  2143. LOG_ERROR("error while reading instruction");
  2144. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  2145. }
  2146. opcode = target_buffer_get_u32(target, buf);
  2147. arm_evaluate_opcode(opcode, pc, instruction);
  2148. } else if (xscale->trace.core_state == ARM_STATE_THUMB) {
  2149. uint8_t buf[2];
  2150. retval = image_read_section(xscale->trace.image, section,
  2151. pc - xscale->trace.image->sections[section].base_address,
  2152. 2, buf, &size_read);
  2153. if (retval != ERROR_OK) {
  2154. LOG_ERROR("error while reading instruction");
  2155. return ERROR_TRACE_INSTRUCTION_UNAVAILABLE;
  2156. }
  2157. opcode = target_buffer_get_u16(target, buf);
  2158. thumb_evaluate_opcode(opcode, pc, instruction);
  2159. } else {
  2160. LOG_ERROR("BUG: unknown core state encountered");
  2161. exit(-1);
  2162. }
  2163. return ERROR_OK;
  2164. }
  2165. /* Extract address encoded into trace data.
  2166. * Write result to address referenced by argument 'target', or 0 if incomplete. */
  2167. static inline void xscale_branch_address(struct xscale_trace_data *trace_data,
  2168. int i, uint32_t *target)
  2169. {
  2170. /* if there are less than four entries prior to the indirect branch message
  2171. * we can't extract the address */
  2172. if (i < 4)
  2173. *target = 0;
  2174. else {
  2175. *target = (trace_data->entries[i-1].data) | (trace_data->entries[i-2].data << 8) |
  2176. (trace_data->entries[i-3].data << 16) | (trace_data->entries[i-4].data << 24);
  2177. }
  2178. }
  2179. static inline void xscale_display_instruction(struct target *target, uint32_t pc,
  2180. struct arm_instruction *instruction,
  2181. struct command_context *cmd_ctx)
  2182. {
  2183. int retval = xscale_read_instruction(target, pc, instruction);
  2184. if (retval == ERROR_OK)
  2185. command_print(cmd_ctx, "%s", instruction->text);
  2186. else
  2187. command_print(cmd_ctx, "0x%8.8" PRIx32 "\t<not found in image>", pc);
  2188. }
  2189. static int xscale_analyze_trace(struct target *target, struct command_context *cmd_ctx)
  2190. {
  2191. struct xscale_common *xscale = target_to_xscale(target);
  2192. struct xscale_trace_data *trace_data = xscale->trace.data;
  2193. int i, retval;
  2194. uint32_t breakpoint_pc;
  2195. struct arm_instruction instruction;
  2196. uint32_t current_pc = 0;/* initialized when address determined */
  2197. if (!xscale->trace.image)
  2198. LOG_WARNING("No trace image loaded; use 'xscale trace_image'");
  2199. /* loop for each trace buffer that was loaded from target */
  2200. while (trace_data) {
  2201. int chkpt = 0; /* incremented as checkpointed entries found */
  2202. int j;
  2203. /* FIXME: set this to correct mode when trace buffer is first enabled */
  2204. xscale->trace.core_state = ARM_STATE_ARM;
  2205. /* loop for each entry in this trace buffer */
  2206. for (i = 0; i < trace_data->depth; i++) {
  2207. int exception = 0;
  2208. uint32_t chkpt_reg = 0x0;
  2209. uint32_t branch_target = 0;
  2210. int count;
  2211. /* trace entry type is upper nybble of 'message byte' */
  2212. int trace_msg_type = (trace_data->entries[i].data & 0xf0) >> 4;
  2213. /* Target addresses of indirect branches are written into buffer
  2214. * before the message byte representing the branch. Skip past it */
  2215. if (trace_data->entries[i].type == XSCALE_TRACE_ADDRESS)
  2216. continue;
  2217. switch (trace_msg_type) {
  2218. case 0: /* Exceptions */
  2219. case 1:
  2220. case 2:
  2221. case 3:
  2222. case 4:
  2223. case 5:
  2224. case 6:
  2225. case 7:
  2226. exception = (trace_data->entries[i].data & 0x70) >> 4;
  2227. /* FIXME: vector table may be at ffff0000 */
  2228. branch_target = (trace_data->entries[i].data & 0xf0) >> 2;
  2229. break;
  2230. case 8: /* Direct Branch */
  2231. break;
  2232. case 9: /* Indirect Branch */
  2233. xscale_branch_address(trace_data, i, &branch_target);
  2234. break;
  2235. case 13: /* Checkpointed Indirect Branch */
  2236. xscale_branch_address(trace_data, i, &branch_target);
  2237. if ((trace_data->num_checkpoints == 2) && (chkpt == 0))
  2238. chkpt_reg = trace_data->chkpt1; /* 2 chkpts, this is
  2239. *oldest */
  2240. else
  2241. chkpt_reg = trace_data->chkpt0; /* 1 chkpt, or 2 and
  2242. *newest */
  2243. chkpt++;
  2244. break;
  2245. case 12: /* Checkpointed Direct Branch */
  2246. if ((trace_data->num_checkpoints == 2) && (chkpt == 0))
  2247. chkpt_reg = trace_data->chkpt1; /* 2 chkpts, this is
  2248. *oldest */
  2249. else
  2250. chkpt_reg = trace_data->chkpt0; /* 1 chkpt, or 2 and
  2251. *newest */
  2252. /* if no current_pc, checkpoint will be starting point */
  2253. if (current_pc == 0)
  2254. branch_target = chkpt_reg;
  2255. chkpt++;
  2256. break;
  2257. case 15:/* Roll-over */
  2258. break;
  2259. default:/* Reserved */
  2260. LOG_WARNING("trace is suspect: invalid trace message byte");
  2261. continue;
  2262. }
  2263. /* If we don't have the current_pc yet, but we did get the branch target
  2264. * (either from the trace buffer on indirect branch, or from a checkpoint reg),
  2265. * then we can start displaying instructions at the next iteration, with
  2266. * branch_target as the starting point.
  2267. */
  2268. if (current_pc == 0) {
  2269. current_pc = branch_target; /* remains 0 unless branch_target *obtained */
  2270. continue;
  2271. }
  2272. /* We have current_pc. Read and display the instructions from the image.
  2273. * First, display count instructions (lower nybble of message byte). */
  2274. count = trace_data->entries[i].data & 0x0f;
  2275. for (j = 0; j < count; j++) {
  2276. xscale_display_instruction(target, current_pc, &instruction,
  2277. cmd_ctx);
  2278. current_pc += xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2;
  2279. }
  2280. /* An additional instruction is implicitly added to count for
  2281. * rollover and some exceptions: undef, swi, prefetch abort. */
  2282. if ((trace_msg_type == 15) || (exception > 0 && exception < 4)) {
  2283. xscale_display_instruction(target, current_pc, &instruction,
  2284. cmd_ctx);
  2285. current_pc += xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2;
  2286. }
  2287. if (trace_msg_type == 15) /* rollover */
  2288. continue;
  2289. if (exception) {
  2290. command_print(cmd_ctx, "--- exception %i ---", exception);
  2291. continue;
  2292. }
  2293. /* not exception or rollover; next instruction is a branch and is
  2294. * not included in the count */
  2295. xscale_display_instruction(target, current_pc, &instruction, cmd_ctx);
  2296. /* for direct branches, extract branch destination from instruction */
  2297. if ((trace_msg_type == 8) || (trace_msg_type == 12)) {
  2298. retval = xscale_read_instruction(target, current_pc, &instruction);
  2299. if (retval == ERROR_OK)
  2300. current_pc = instruction.info.b_bl_bx_blx.target_address;
  2301. else
  2302. current_pc = 0; /* branch destination unknown */
  2303. /* direct branch w/ checkpoint; can also get from checkpoint reg */
  2304. if (trace_msg_type == 12) {
  2305. if (current_pc == 0)
  2306. current_pc = chkpt_reg;
  2307. else if (current_pc != chkpt_reg) /* sanity check */
  2308. LOG_WARNING("trace is suspect: checkpoint register "
  2309. "inconsistent with adddress from image");
  2310. }
  2311. if (current_pc == 0)
  2312. command_print(cmd_ctx, "address unknown");
  2313. continue;
  2314. }
  2315. /* indirect branch; the branch destination was read from trace buffer */
  2316. if ((trace_msg_type == 9) || (trace_msg_type == 13)) {
  2317. current_pc = branch_target;
  2318. /* sanity check (checkpoint reg is redundant) */
  2319. if ((trace_msg_type == 13) && (chkpt_reg != branch_target))
  2320. LOG_WARNING("trace is suspect: checkpoint register "
  2321. "inconsistent with address from trace buffer");
  2322. }
  2323. } /* END: for (i = 0; i < trace_data->depth; i++) */
  2324. breakpoint_pc = trace_data->last_instruction; /* used below */
  2325. trace_data = trace_data->next;
  2326. } /* END: while (trace_data) */
  2327. /* Finally... display all instructions up to the value of the pc when the
  2328. * debug break occurred (saved when trace data was collected from target).
  2329. * This is necessary because the trace only records execution branches and 16
  2330. * consecutive instructions (rollovers), so last few typically missed.
  2331. */
  2332. if (current_pc == 0)
  2333. return ERROR_OK;/* current_pc was never found */
  2334. /* how many instructions remaining? */
  2335. int gap_count = (breakpoint_pc - current_pc) /
  2336. (xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2);
  2337. /* should never be negative or over 16, but verify */
  2338. if (gap_count < 0 || gap_count > 16) {
  2339. LOG_WARNING("trace is suspect: excessive gap at end of trace");
  2340. return ERROR_OK;/* bail; large number or negative value no good */
  2341. }
  2342. /* display remaining instructions */
  2343. for (i = 0; i < gap_count; i++) {
  2344. xscale_display_instruction(target, current_pc, &instruction, cmd_ctx);
  2345. current_pc += xscale->trace.core_state == ARM_STATE_ARM ? 4 : 2;
  2346. }
  2347. return ERROR_OK;
  2348. }
  2349. static const struct reg_arch_type xscale_reg_type = {
  2350. .get = xscale_get_reg,
  2351. .set = xscale_set_reg,
  2352. };
  2353. static void xscale_build_reg_cache(struct target *target)
  2354. {
  2355. struct xscale_common *xscale = target_to_xscale(target);
  2356. struct arm *arm = &xscale->arm;
  2357. struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
  2358. struct xscale_reg *arch_info = malloc(sizeof(xscale_reg_arch_info));
  2359. int i;
  2360. int num_regs = ARRAY_SIZE(xscale_reg_arch_info);
  2361. (*cache_p) = arm_build_reg_cache(target, arm);
  2362. (*cache_p)->next = malloc(sizeof(struct reg_cache));
  2363. cache_p = &(*cache_p)->next;
  2364. /* fill in values for the xscale reg cache */
  2365. (*cache_p)->name = "XScale registers";
  2366. (*cache_p)->next = NULL;
  2367. (*cache_p)->reg_list = malloc(num_regs * sizeof(struct reg));
  2368. (*cache_p)->num_regs = num_regs;
  2369. for (i = 0; i < num_regs; i++) {
  2370. (*cache_p)->reg_list[i].name = xscale_reg_list[i];
  2371. (*cache_p)->reg_list[i].value = calloc(4, 1);
  2372. (*cache_p)->reg_list[i].dirty = 0;
  2373. (*cache_p)->reg_list[i].valid = 0;
  2374. (*cache_p)->reg_list[i].size = 32;
  2375. (*cache_p)->reg_list[i].arch_info = &arch_info[i];
  2376. (*cache_p)->reg_list[i].type = &xscale_reg_type;
  2377. arch_info[i] = xscale_reg_arch_info[i];
  2378. arch_info[i].target = target;
  2379. }
  2380. xscale->reg_cache = (*cache_p);
  2381. }
  2382. static int xscale_init_target(struct command_context *cmd_ctx,
  2383. struct target *target)
  2384. {
  2385. xscale_build_reg_cache(target);
  2386. return ERROR_OK;
  2387. }
  2388. static int xscale_init_arch_info(struct target *target,
  2389. struct xscale_common *xscale, struct jtag_tap *tap, const char *variant)
  2390. {
  2391. struct arm *arm;
  2392. uint32_t high_reset_branch, low_reset_branch;
  2393. int i;
  2394. arm = &xscale->arm;
  2395. /* store architecture specfic data */
  2396. xscale->common_magic = XSCALE_COMMON_MAGIC;
  2397. /* we don't really *need* a variant param ... */
  2398. if (variant) {
  2399. int ir_length = 0;
  2400. if (strcmp(variant, "pxa250") == 0
  2401. || strcmp(variant, "pxa255") == 0
  2402. || strcmp(variant, "pxa26x") == 0)
  2403. ir_length = 5;
  2404. else if (strcmp(variant, "pxa27x") == 0
  2405. || strcmp(variant, "ixp42x") == 0
  2406. || strcmp(variant, "ixp45x") == 0
  2407. || strcmp(variant, "ixp46x") == 0)
  2408. ir_length = 7;
  2409. else if (strcmp(variant, "pxa3xx") == 0)
  2410. ir_length = 11;
  2411. else
  2412. LOG_WARNING("%s: unrecognized variant %s",
  2413. tap->dotted_name, variant);
  2414. if (ir_length && ir_length != tap->ir_length) {
  2415. LOG_WARNING("%s: IR length for %s is %d; fixing",
  2416. tap->dotted_name, variant, ir_length);
  2417. tap->ir_length = ir_length;
  2418. }
  2419. }
  2420. /* PXA3xx shifts the JTAG instructions */
  2421. if (tap->ir_length == 11)
  2422. xscale->xscale_variant = XSCALE_PXA3XX;
  2423. else
  2424. xscale->xscale_variant = XSCALE_IXP4XX_PXA2XX;
  2425. /* the debug handler isn't installed (and thus not running) at this time */
  2426. xscale->handler_address = 0xfe000800;
  2427. /* clear the vectors we keep locally for reference */
  2428. memset(xscale->low_vectors, 0, sizeof(xscale->low_vectors));
  2429. memset(xscale->high_vectors, 0, sizeof(xscale->high_vectors));
  2430. /* no user-specified vectors have been configured yet */
  2431. xscale->static_low_vectors_set = 0x0;
  2432. xscale->static_high_vectors_set = 0x0;
  2433. /* calculate branches to debug handler */
  2434. low_reset_branch = (xscale->handler_address + 0x20 - 0x0 - 0x8) >> 2;
  2435. high_reset_branch = (xscale->handler_address + 0x20 - 0xffff0000 - 0x8) >> 2;
  2436. xscale->low_vectors[0] = ARMV4_5_B((low_reset_branch & 0xffffff), 0);
  2437. xscale->high_vectors[0] = ARMV4_5_B((high_reset_branch & 0xffffff), 0);
  2438. for (i = 1; i <= 7; i++) {
  2439. xscale->low_vectors[i] = ARMV4_5_B(0xfffffe, 0);
  2440. xscale->high_vectors[i] = ARMV4_5_B(0xfffffe, 0);
  2441. }
  2442. /* 64kB aligned region used for DCache cleaning */
  2443. xscale->cache_clean_address = 0xfffe0000;
  2444. xscale->hold_rst = 0;
  2445. xscale->external_debug_break = 0;
  2446. xscale->ibcr_available = 2;
  2447. xscale->ibcr0_used = 0;
  2448. xscale->ibcr1_used = 0;
  2449. xscale->dbr_available = 2;
  2450. xscale->dbr0_used = 0;
  2451. xscale->dbr1_used = 0;
  2452. LOG_INFO("%s: hardware has 2 breakpoints and 2 watchpoints",
  2453. target_name(target));
  2454. xscale->arm_bkpt = ARMV5_BKPT(0x0);
  2455. xscale->thumb_bkpt = ARMV5_T_BKPT(0x0) & 0xffff;
  2456. xscale->vector_catch = 0x1;
  2457. xscale->trace.data = NULL;
  2458. xscale->trace.image = NULL;
  2459. xscale->trace.mode = XSCALE_TRACE_DISABLED;
  2460. xscale->trace.buffer_fill = 0;
  2461. xscale->trace.fill_counter = 0;
  2462. /* prepare ARMv4/5 specific information */
  2463. arm->arch_info = xscale;
  2464. arm->read_core_reg = xscale_read_core_reg;
  2465. arm->write_core_reg = xscale_write_core_reg;
  2466. arm->full_context = xscale_full_context;
  2467. arm_init_arch_info(target, arm);
  2468. xscale->armv4_5_mmu.armv4_5_cache.ctype = -1;
  2469. xscale->armv4_5_mmu.get_ttb = xscale_get_ttb;
  2470. xscale->armv4_5_mmu.read_memory = xscale_read_memory;
  2471. xscale->armv4_5_mmu.write_memory = xscale_write_memory;
  2472. xscale->armv4_5_mmu.disable_mmu_caches = xscale_disable_mmu_caches;
  2473. xscale->armv4_5_mmu.enable_mmu_caches = xscale_enable_mmu_caches;
  2474. xscale->armv4_5_mmu.has_tiny_pages = 1;
  2475. xscale->armv4_5_mmu.mmu_enabled = 0;
  2476. return ERROR_OK;
  2477. }
  2478. static int xscale_target_create(struct target *target, Jim_Interp *interp)
  2479. {
  2480. struct xscale_common *xscale;
  2481. if (sizeof xscale_debug_handler - 1 > 0x800) {
  2482. LOG_ERROR("debug_handler.bin: larger than 2kb");
  2483. return ERROR_FAIL;
  2484. }
  2485. xscale = calloc(1, sizeof(*xscale));
  2486. if (!xscale)
  2487. return ERROR_FAIL;
  2488. return xscale_init_arch_info(target, xscale, target->tap,
  2489. target->variant);
  2490. }
  2491. COMMAND_HANDLER(xscale_handle_debug_handler_command)
  2492. {
  2493. struct target *target = NULL;
  2494. struct xscale_common *xscale;
  2495. int retval;
  2496. uint32_t handler_address;
  2497. if (CMD_ARGC < 2)
  2498. return ERROR_COMMAND_SYNTAX_ERROR;
  2499. target = get_target(CMD_ARGV[0]);
  2500. if (target == NULL) {
  2501. LOG_ERROR("target '%s' not defined", CMD_ARGV[0]);
  2502. return ERROR_FAIL;
  2503. }
  2504. xscale = target_to_xscale(target);
  2505. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2506. if (retval != ERROR_OK)
  2507. return retval;
  2508. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], handler_address);
  2509. if (((handler_address >= 0x800) && (handler_address <= 0x1fef800)) ||
  2510. ((handler_address >= 0xfe000800) && (handler_address <= 0xfffff800)))
  2511. xscale->handler_address = handler_address;
  2512. else {
  2513. LOG_ERROR(
  2514. "xscale debug_handler <address> must be between 0x800 and 0x1fef800 or between 0xfe000800 and 0xfffff800");
  2515. return ERROR_FAIL;
  2516. }
  2517. return ERROR_OK;
  2518. }
  2519. COMMAND_HANDLER(xscale_handle_cache_clean_address_command)
  2520. {
  2521. struct target *target = NULL;
  2522. struct xscale_common *xscale;
  2523. int retval;
  2524. uint32_t cache_clean_address;
  2525. if (CMD_ARGC < 2)
  2526. return ERROR_COMMAND_SYNTAX_ERROR;
  2527. target = get_target(CMD_ARGV[0]);
  2528. if (target == NULL) {
  2529. LOG_ERROR("target '%s' not defined", CMD_ARGV[0]);
  2530. return ERROR_FAIL;
  2531. }
  2532. xscale = target_to_xscale(target);
  2533. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2534. if (retval != ERROR_OK)
  2535. return retval;
  2536. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], cache_clean_address);
  2537. if (cache_clean_address & 0xffff)
  2538. LOG_ERROR("xscale cache_clean_address <address> must be 64kb aligned");
  2539. else
  2540. xscale->cache_clean_address = cache_clean_address;
  2541. return ERROR_OK;
  2542. }
  2543. COMMAND_HANDLER(xscale_handle_cache_info_command)
  2544. {
  2545. struct target *target = get_current_target(CMD_CTX);
  2546. struct xscale_common *xscale = target_to_xscale(target);
  2547. int retval;
  2548. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2549. if (retval != ERROR_OK)
  2550. return retval;
  2551. return armv4_5_handle_cache_info_command(CMD_CTX, &xscale->armv4_5_mmu.armv4_5_cache);
  2552. }
  2553. static int xscale_virt2phys(struct target *target,
  2554. uint32_t virtual, uint32_t *physical)
  2555. {
  2556. struct xscale_common *xscale = target_to_xscale(target);
  2557. uint32_t cb;
  2558. if (xscale->common_magic != XSCALE_COMMON_MAGIC) {
  2559. LOG_ERROR(xscale_not);
  2560. return ERROR_TARGET_INVALID;
  2561. }
  2562. uint32_t ret;
  2563. int retval = armv4_5_mmu_translate_va(target, &xscale->armv4_5_mmu,
  2564. virtual, &cb, &ret);
  2565. if (retval != ERROR_OK)
  2566. return retval;
  2567. *physical = ret;
  2568. return ERROR_OK;
  2569. }
  2570. static int xscale_mmu(struct target *target, int *enabled)
  2571. {
  2572. struct xscale_common *xscale = target_to_xscale(target);
  2573. if (target->state != TARGET_HALTED) {
  2574. LOG_ERROR("Target not halted");
  2575. return ERROR_TARGET_INVALID;
  2576. }
  2577. *enabled = xscale->armv4_5_mmu.mmu_enabled;
  2578. return ERROR_OK;
  2579. }
  2580. COMMAND_HANDLER(xscale_handle_mmu_command)
  2581. {
  2582. struct target *target = get_current_target(CMD_CTX);
  2583. struct xscale_common *xscale = target_to_xscale(target);
  2584. int retval;
  2585. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2586. if (retval != ERROR_OK)
  2587. return retval;
  2588. if (target->state != TARGET_HALTED) {
  2589. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  2590. return ERROR_OK;
  2591. }
  2592. if (CMD_ARGC >= 1) {
  2593. bool enable;
  2594. COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
  2595. if (enable)
  2596. xscale_enable_mmu_caches(target, 1, 0, 0);
  2597. else
  2598. xscale_disable_mmu_caches(target, 1, 0, 0);
  2599. xscale->armv4_5_mmu.mmu_enabled = enable;
  2600. }
  2601. command_print(CMD_CTX, "mmu %s",
  2602. (xscale->armv4_5_mmu.mmu_enabled) ? "enabled" : "disabled");
  2603. return ERROR_OK;
  2604. }
  2605. COMMAND_HANDLER(xscale_handle_idcache_command)
  2606. {
  2607. struct target *target = get_current_target(CMD_CTX);
  2608. struct xscale_common *xscale = target_to_xscale(target);
  2609. int retval = xscale_verify_pointer(CMD_CTX, xscale);
  2610. if (retval != ERROR_OK)
  2611. return retval;
  2612. if (target->state != TARGET_HALTED) {
  2613. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  2614. return ERROR_OK;
  2615. }
  2616. bool icache = false;
  2617. if (strcmp(CMD_NAME, "icache") == 0)
  2618. icache = true;
  2619. if (CMD_ARGC >= 1) {
  2620. bool enable;
  2621. COMMAND_PARSE_ENABLE(CMD_ARGV[0], enable);
  2622. if (icache) {
  2623. xscale->armv4_5_mmu.armv4_5_cache.i_cache_enabled = enable;
  2624. if (enable)
  2625. xscale_enable_mmu_caches(target, 0, 0, 1);
  2626. else
  2627. xscale_disable_mmu_caches(target, 0, 0, 1);
  2628. } else {
  2629. xscale->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = enable;
  2630. if (enable)
  2631. xscale_enable_mmu_caches(target, 0, 1, 0);
  2632. else
  2633. xscale_disable_mmu_caches(target, 0, 1, 0);
  2634. }
  2635. }
  2636. bool enabled = icache ?
  2637. xscale->armv4_5_mmu.armv4_5_cache.i_cache_enabled :
  2638. xscale->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled;
  2639. const char *msg = enabled ? "enabled" : "disabled";
  2640. command_print(CMD_CTX, "%s %s", CMD_NAME, msg);
  2641. return ERROR_OK;
  2642. }
  2643. COMMAND_HANDLER(xscale_handle_vector_catch_command)
  2644. {
  2645. struct target *target = get_current_target(CMD_CTX);
  2646. struct xscale_common *xscale = target_to_xscale(target);
  2647. int retval;
  2648. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2649. if (retval != ERROR_OK)
  2650. return retval;
  2651. if (CMD_ARGC < 1)
  2652. return ERROR_COMMAND_SYNTAX_ERROR;
  2653. else {
  2654. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0], xscale->vector_catch);
  2655. buf_set_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value,
  2656. 16,
  2657. 8,
  2658. xscale->vector_catch);
  2659. xscale_write_dcsr(target, -1, -1);
  2660. }
  2661. command_print(CMD_CTX, "vector catch mask: 0x%2.2x", xscale->vector_catch);
  2662. return ERROR_OK;
  2663. }
  2664. COMMAND_HANDLER(xscale_handle_vector_table_command)
  2665. {
  2666. struct target *target = get_current_target(CMD_CTX);
  2667. struct xscale_common *xscale = target_to_xscale(target);
  2668. int err = 0;
  2669. int retval;
  2670. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2671. if (retval != ERROR_OK)
  2672. return retval;
  2673. if (CMD_ARGC == 0) { /* print current settings */
  2674. int idx;
  2675. command_print(CMD_CTX, "active user-set static vectors:");
  2676. for (idx = 1; idx < 8; idx++)
  2677. if (xscale->static_low_vectors_set & (1 << idx))
  2678. command_print(CMD_CTX,
  2679. "low %d: 0x%" PRIx32,
  2680. idx,
  2681. xscale->static_low_vectors[idx]);
  2682. for (idx = 1; idx < 8; idx++)
  2683. if (xscale->static_high_vectors_set & (1 << idx))
  2684. command_print(CMD_CTX,
  2685. "high %d: 0x%" PRIx32,
  2686. idx,
  2687. xscale->static_high_vectors[idx]);
  2688. return ERROR_OK;
  2689. }
  2690. if (CMD_ARGC != 3)
  2691. err = 1;
  2692. else {
  2693. int idx;
  2694. COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], idx);
  2695. uint32_t vec;
  2696. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], vec);
  2697. if (idx < 1 || idx >= 8)
  2698. err = 1;
  2699. if (!err && strcmp(CMD_ARGV[0], "low") == 0) {
  2700. xscale->static_low_vectors_set |= (1<<idx);
  2701. xscale->static_low_vectors[idx] = vec;
  2702. } else if (!err && (strcmp(CMD_ARGV[0], "high") == 0)) {
  2703. xscale->static_high_vectors_set |= (1<<idx);
  2704. xscale->static_high_vectors[idx] = vec;
  2705. } else
  2706. err = 1;
  2707. }
  2708. if (err)
  2709. return ERROR_COMMAND_SYNTAX_ERROR;
  2710. return ERROR_OK;
  2711. }
  2712. COMMAND_HANDLER(xscale_handle_trace_buffer_command)
  2713. {
  2714. struct target *target = get_current_target(CMD_CTX);
  2715. struct xscale_common *xscale = target_to_xscale(target);
  2716. uint32_t dcsr_value;
  2717. int retval;
  2718. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2719. if (retval != ERROR_OK)
  2720. return retval;
  2721. if (target->state != TARGET_HALTED) {
  2722. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  2723. return ERROR_OK;
  2724. }
  2725. if (CMD_ARGC >= 1) {
  2726. if (strcmp("enable", CMD_ARGV[0]) == 0)
  2727. xscale->trace.mode = XSCALE_TRACE_WRAP; /* default */
  2728. else if (strcmp("disable", CMD_ARGV[0]) == 0)
  2729. xscale->trace.mode = XSCALE_TRACE_DISABLED;
  2730. else
  2731. return ERROR_COMMAND_SYNTAX_ERROR;
  2732. }
  2733. if (CMD_ARGC >= 2 && xscale->trace.mode != XSCALE_TRACE_DISABLED) {
  2734. if (strcmp("fill", CMD_ARGV[1]) == 0) {
  2735. int buffcount = 1; /* default */
  2736. if (CMD_ARGC >= 3)
  2737. COMMAND_PARSE_NUMBER(int, CMD_ARGV[2], buffcount);
  2738. if (buffcount < 1) { /* invalid */
  2739. command_print(CMD_CTX, "fill buffer count must be > 0");
  2740. xscale->trace.mode = XSCALE_TRACE_DISABLED;
  2741. return ERROR_COMMAND_SYNTAX_ERROR;
  2742. }
  2743. xscale->trace.buffer_fill = buffcount;
  2744. xscale->trace.mode = XSCALE_TRACE_FILL;
  2745. } else if (strcmp("wrap", CMD_ARGV[1]) == 0)
  2746. xscale->trace.mode = XSCALE_TRACE_WRAP;
  2747. else {
  2748. xscale->trace.mode = XSCALE_TRACE_DISABLED;
  2749. return ERROR_COMMAND_SYNTAX_ERROR;
  2750. }
  2751. }
  2752. if (xscale->trace.mode != XSCALE_TRACE_DISABLED) {
  2753. char fill_string[12];
  2754. sprintf(fill_string, "fill %" PRId32, xscale->trace.buffer_fill);
  2755. command_print(CMD_CTX, "trace buffer enabled (%s)",
  2756. (xscale->trace.mode == XSCALE_TRACE_FILL)
  2757. ? fill_string : "wrap");
  2758. } else
  2759. command_print(CMD_CTX, "trace buffer disabled");
  2760. dcsr_value = buf_get_u32(xscale->reg_cache->reg_list[XSCALE_DCSR].value, 0, 32);
  2761. if (xscale->trace.mode == XSCALE_TRACE_FILL)
  2762. xscale_write_dcsr_sw(target, (dcsr_value & 0xfffffffc) | 2);
  2763. else
  2764. xscale_write_dcsr_sw(target, dcsr_value & 0xfffffffc);
  2765. return ERROR_OK;
  2766. }
  2767. COMMAND_HANDLER(xscale_handle_trace_image_command)
  2768. {
  2769. struct target *target = get_current_target(CMD_CTX);
  2770. struct xscale_common *xscale = target_to_xscale(target);
  2771. int retval;
  2772. if (CMD_ARGC < 1)
  2773. return ERROR_COMMAND_SYNTAX_ERROR;
  2774. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2775. if (retval != ERROR_OK)
  2776. return retval;
  2777. if (xscale->trace.image) {
  2778. image_close(xscale->trace.image);
  2779. free(xscale->trace.image);
  2780. command_print(CMD_CTX, "previously loaded image found and closed");
  2781. }
  2782. xscale->trace.image = malloc(sizeof(struct image));
  2783. xscale->trace.image->base_address_set = 0;
  2784. xscale->trace.image->start_address_set = 0;
  2785. /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
  2786. if (CMD_ARGC >= 2) {
  2787. xscale->trace.image->base_address_set = 1;
  2788. COMMAND_PARSE_NUMBER(llong, CMD_ARGV[1], xscale->trace.image->base_address);
  2789. } else
  2790. xscale->trace.image->base_address_set = 0;
  2791. if (image_open(xscale->trace.image, CMD_ARGV[0],
  2792. (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK) {
  2793. free(xscale->trace.image);
  2794. xscale->trace.image = NULL;
  2795. return ERROR_OK;
  2796. }
  2797. return ERROR_OK;
  2798. }
  2799. COMMAND_HANDLER(xscale_handle_dump_trace_command)
  2800. {
  2801. struct target *target = get_current_target(CMD_CTX);
  2802. struct xscale_common *xscale = target_to_xscale(target);
  2803. struct xscale_trace_data *trace_data;
  2804. struct fileio file;
  2805. int retval;
  2806. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2807. if (retval != ERROR_OK)
  2808. return retval;
  2809. if (target->state != TARGET_HALTED) {
  2810. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  2811. return ERROR_OK;
  2812. }
  2813. if (CMD_ARGC < 1)
  2814. return ERROR_COMMAND_SYNTAX_ERROR;
  2815. trace_data = xscale->trace.data;
  2816. if (!trace_data) {
  2817. command_print(CMD_CTX, "no trace data collected");
  2818. return ERROR_OK;
  2819. }
  2820. if (fileio_open(&file, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  2821. return ERROR_OK;
  2822. while (trace_data) {
  2823. int i;
  2824. fileio_write_u32(&file, trace_data->chkpt0);
  2825. fileio_write_u32(&file, trace_data->chkpt1);
  2826. fileio_write_u32(&file, trace_data->last_instruction);
  2827. fileio_write_u32(&file, trace_data->depth);
  2828. for (i = 0; i < trace_data->depth; i++)
  2829. fileio_write_u32(&file, trace_data->entries[i].data |
  2830. ((trace_data->entries[i].type & 0xffff) << 16));
  2831. trace_data = trace_data->next;
  2832. }
  2833. fileio_close(&file);
  2834. return ERROR_OK;
  2835. }
  2836. COMMAND_HANDLER(xscale_handle_analyze_trace_buffer_command)
  2837. {
  2838. struct target *target = get_current_target(CMD_CTX);
  2839. struct xscale_common *xscale = target_to_xscale(target);
  2840. int retval;
  2841. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2842. if (retval != ERROR_OK)
  2843. return retval;
  2844. xscale_analyze_trace(target, CMD_CTX);
  2845. return ERROR_OK;
  2846. }
  2847. COMMAND_HANDLER(xscale_handle_cp15)
  2848. {
  2849. struct target *target = get_current_target(CMD_CTX);
  2850. struct xscale_common *xscale = target_to_xscale(target);
  2851. int retval;
  2852. retval = xscale_verify_pointer(CMD_CTX, xscale);
  2853. if (retval != ERROR_OK)
  2854. return retval;
  2855. if (target->state != TARGET_HALTED) {
  2856. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  2857. return ERROR_OK;
  2858. }
  2859. uint32_t reg_no = 0;
  2860. struct reg *reg = NULL;
  2861. if (CMD_ARGC > 0) {
  2862. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], reg_no);
  2863. /*translate from xscale cp15 register no to openocd register*/
  2864. switch (reg_no) {
  2865. case 0:
  2866. reg_no = XSCALE_MAINID;
  2867. break;
  2868. case 1:
  2869. reg_no = XSCALE_CTRL;
  2870. break;
  2871. case 2:
  2872. reg_no = XSCALE_TTB;
  2873. break;
  2874. case 3:
  2875. reg_no = XSCALE_DAC;
  2876. break;
  2877. case 5:
  2878. reg_no = XSCALE_FSR;
  2879. break;
  2880. case 6:
  2881. reg_no = XSCALE_FAR;
  2882. break;
  2883. case 13:
  2884. reg_no = XSCALE_PID;
  2885. break;
  2886. case 15:
  2887. reg_no = XSCALE_CPACCESS;
  2888. break;
  2889. default:
  2890. command_print(CMD_CTX, "invalid register number");
  2891. return ERROR_COMMAND_SYNTAX_ERROR;
  2892. }
  2893. reg = &xscale->reg_cache->reg_list[reg_no];
  2894. }
  2895. if (CMD_ARGC == 1) {
  2896. uint32_t value;
  2897. /* read cp15 control register */
  2898. xscale_get_reg(reg);
  2899. value = buf_get_u32(reg->value, 0, 32);
  2900. command_print(CMD_CTX, "%s (/%i): 0x%" PRIx32 "", reg->name, (int)(reg->size),
  2901. value);
  2902. } else if (CMD_ARGC == 2) {
  2903. uint32_t value;
  2904. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  2905. /* send CP write request (command 0x41) */
  2906. xscale_send_u32(target, 0x41);
  2907. /* send CP register number */
  2908. xscale_send_u32(target, reg_no);
  2909. /* send CP register value */
  2910. xscale_send_u32(target, value);
  2911. /* execute cpwait to ensure outstanding operations complete */
  2912. xscale_send_u32(target, 0x53);
  2913. } else
  2914. return ERROR_COMMAND_SYNTAX_ERROR;
  2915. return ERROR_OK;
  2916. }
  2917. static const struct command_registration xscale_exec_command_handlers[] = {
  2918. {
  2919. .name = "cache_info",
  2920. .handler = xscale_handle_cache_info_command,
  2921. .mode = COMMAND_EXEC,
  2922. .help = "display information about CPU caches",
  2923. },
  2924. {
  2925. .name = "mmu",
  2926. .handler = xscale_handle_mmu_command,
  2927. .mode = COMMAND_EXEC,
  2928. .help = "enable or disable the MMU",
  2929. .usage = "['enable'|'disable']",
  2930. },
  2931. {
  2932. .name = "icache",
  2933. .handler = xscale_handle_idcache_command,
  2934. .mode = COMMAND_EXEC,
  2935. .help = "display ICache state, optionally enabling or "
  2936. "disabling it",
  2937. .usage = "['enable'|'disable']",
  2938. },
  2939. {
  2940. .name = "dcache",
  2941. .handler = xscale_handle_idcache_command,
  2942. .mode = COMMAND_EXEC,
  2943. .help = "display DCache state, optionally enabling or "
  2944. "disabling it",
  2945. .usage = "['enable'|'disable']",
  2946. },
  2947. {
  2948. .name = "vector_catch",
  2949. .handler = xscale_handle_vector_catch_command,
  2950. .mode = COMMAND_EXEC,
  2951. .help = "set or display 8-bit mask of vectors "
  2952. "that should trigger debug entry",
  2953. .usage = "[mask]",
  2954. },
  2955. {
  2956. .name = "vector_table",
  2957. .handler = xscale_handle_vector_table_command,
  2958. .mode = COMMAND_EXEC,
  2959. .help = "set vector table entry in mini-ICache, "
  2960. "or display current tables",
  2961. .usage = "[('high'|'low') index code]",
  2962. },
  2963. {
  2964. .name = "trace_buffer",
  2965. .handler = xscale_handle_trace_buffer_command,
  2966. .mode = COMMAND_EXEC,
  2967. .help = "display trace buffer status, enable or disable "
  2968. "tracing, and optionally reconfigure trace mode",
  2969. .usage = "['enable'|'disable' ['fill' [number]|'wrap']]",
  2970. },
  2971. {
  2972. .name = "dump_trace",
  2973. .handler = xscale_handle_dump_trace_command,
  2974. .mode = COMMAND_EXEC,
  2975. .help = "dump content of trace buffer to file",
  2976. .usage = "filename",
  2977. },
  2978. {
  2979. .name = "analyze_trace",
  2980. .handler = xscale_handle_analyze_trace_buffer_command,
  2981. .mode = COMMAND_EXEC,
  2982. .help = "analyze content of trace buffer",
  2983. .usage = "",
  2984. },
  2985. {
  2986. .name = "trace_image",
  2987. .handler = xscale_handle_trace_image_command,
  2988. .mode = COMMAND_EXEC,
  2989. .help = "load image from file to address (default 0)",
  2990. .usage = "filename [offset [filetype]]",
  2991. },
  2992. {
  2993. .name = "cp15",
  2994. .handler = xscale_handle_cp15,
  2995. .mode = COMMAND_EXEC,
  2996. .help = "Read or write coprocessor 15 register.",
  2997. .usage = "register [value]",
  2998. },
  2999. COMMAND_REGISTRATION_DONE
  3000. };
  3001. static const struct command_registration xscale_any_command_handlers[] = {
  3002. {
  3003. .name = "debug_handler",
  3004. .handler = xscale_handle_debug_handler_command,
  3005. .mode = COMMAND_ANY,
  3006. .help = "Change address used for debug handler.",
  3007. .usage = "<target> <address>",
  3008. },
  3009. {
  3010. .name = "cache_clean_address",
  3011. .handler = xscale_handle_cache_clean_address_command,
  3012. .mode = COMMAND_ANY,
  3013. .help = "Change address used for cleaning data cache.",
  3014. .usage = "address",
  3015. },
  3016. {
  3017. .chain = xscale_exec_command_handlers,
  3018. },
  3019. COMMAND_REGISTRATION_DONE
  3020. };
  3021. static const struct command_registration xscale_command_handlers[] = {
  3022. {
  3023. .chain = arm_command_handlers,
  3024. },
  3025. {
  3026. .name = "xscale",
  3027. .mode = COMMAND_ANY,
  3028. .help = "xscale command group",
  3029. .usage = "",
  3030. .chain = xscale_any_command_handlers,
  3031. },
  3032. COMMAND_REGISTRATION_DONE
  3033. };
  3034. struct target_type xscale_target = {
  3035. .name = "xscale",
  3036. .poll = xscale_poll,
  3037. .arch_state = xscale_arch_state,
  3038. .target_request_data = NULL,
  3039. .halt = xscale_halt,
  3040. .resume = xscale_resume,
  3041. .step = xscale_step,
  3042. .assert_reset = xscale_assert_reset,
  3043. .deassert_reset = xscale_deassert_reset,
  3044. .soft_reset_halt = NULL,
  3045. /* REVISIT on some cores, allow exporting iwmmxt registers ... */
  3046. .get_gdb_reg_list = arm_get_gdb_reg_list,
  3047. .read_memory = xscale_read_memory,
  3048. .read_phys_memory = xscale_read_phys_memory,
  3049. .write_memory = xscale_write_memory,
  3050. .write_phys_memory = xscale_write_phys_memory,
  3051. .checksum_memory = arm_checksum_memory,
  3052. .blank_check_memory = arm_blank_check_memory,
  3053. .run_algorithm = armv4_5_run_algorithm,
  3054. .add_breakpoint = xscale_add_breakpoint,
  3055. .remove_breakpoint = xscale_remove_breakpoint,
  3056. .add_watchpoint = xscale_add_watchpoint,
  3057. .remove_watchpoint = xscale_remove_watchpoint,
  3058. .commands = xscale_command_handlers,
  3059. .target_create = xscale_target_create,
  3060. .init_target = xscale_init_target,
  3061. .virt2phys = xscale_virt2phys,
  3062. .mmu = xscale_mmu
  3063. };