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.
 
 
 
 
 
 

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