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.
 
 
 
 
 
 

3135 lines
94 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 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) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * Copyright (C) 2008 by Hongtao Zheng *
  12. * hontor@126.com *
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. * This program is distributed in the hope that it will be useful, *
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  22. * GNU General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU General Public License *
  25. * along with this program; if not, write to the *
  26. * Free Software Foundation, Inc., *
  27. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  28. ***************************************************************************/
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include "embeddedice.h"
  33. #include "target_request.h"
  34. #include "arm7_9_common.h"
  35. #include "time_support.h"
  36. #include "arm_simulator.h"
  37. int arm7_9_debug_entry(target_t *target);
  38. int arm7_9_enable_sw_bkpts(struct target_s *target);
  39. /* command handler forward declarations */
  40. int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  42. int handle_arm7_9_read_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  43. int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  45. int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  46. int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  47. int handle_arm7_9_etm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  48. /**
  49. * Clear watchpoints for an ARM7/9 target.
  50. *
  51. * @param arm7_9 Pointer to the common struct for an ARM7/9 target
  52. * @return JTAG error status after executing queue
  53. */
  54. static int arm7_9_clear_watchpoints(arm7_9_common_t *arm7_9)
  55. {
  56. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
  57. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
  58. arm7_9->sw_breakpoints_added = 0;
  59. arm7_9->wp0_used = 0;
  60. arm7_9->wp1_used = arm7_9->wp1_used_default;
  61. arm7_9->wp_available = arm7_9->wp_available_max;
  62. return jtag_execute_queue();
  63. }
  64. /**
  65. * Assign a watchpoint to one of the two available hardware comparators in an
  66. * ARM7 or ARM9 target.
  67. *
  68. * @param arm7_9 Pointer to the common struct for an ARM7/9 target
  69. * @param breakpoint Pointer to the breakpoint to be used as a watchpoint
  70. */
  71. static void arm7_9_assign_wp(arm7_9_common_t *arm7_9, breakpoint_t *breakpoint)
  72. {
  73. if (!arm7_9->wp0_used)
  74. {
  75. arm7_9->wp0_used = 1;
  76. breakpoint->set = 1;
  77. arm7_9->wp_available--;
  78. }
  79. else if (!arm7_9->wp1_used)
  80. {
  81. arm7_9->wp1_used = 1;
  82. breakpoint->set = 2;
  83. arm7_9->wp_available--;
  84. }
  85. else
  86. {
  87. LOG_ERROR("BUG: no hardware comparator available");
  88. }
  89. }
  90. /**
  91. * Setup an ARM7/9 target's embedded ICE registers for software breakpoints.
  92. *
  93. * @param arm7_9 Pointer to common struct for ARM7/9 targets
  94. * @return Error codes if there is a problem finding a watchpoint or the result
  95. * of executing the JTAG queue
  96. */
  97. static int arm7_9_set_software_breakpoints(arm7_9_common_t *arm7_9)
  98. {
  99. if (arm7_9->sw_breakpoints_added)
  100. {
  101. return ERROR_OK;
  102. }
  103. if (arm7_9->wp_available < 1)
  104. {
  105. LOG_WARNING("can't enable sw breakpoints with no watchpoint unit available");
  106. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  107. }
  108. arm7_9->wp_available--;
  109. /* pick a breakpoint unit */
  110. if (!arm7_9->wp0_used)
  111. {
  112. arm7_9->sw_breakpoints_added = 1;
  113. arm7_9->wp0_used = 3;
  114. } else if (!arm7_9->wp1_used)
  115. {
  116. arm7_9->sw_breakpoints_added = 2;
  117. arm7_9->wp1_used = 3;
  118. }
  119. else
  120. {
  121. LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
  122. return ERROR_FAIL;
  123. }
  124. if (arm7_9->sw_breakpoints_added == 1)
  125. {
  126. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], arm7_9->arm_bkpt);
  127. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0x0);
  128. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffffu);
  129. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  130. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  131. }
  132. else if (arm7_9->sw_breakpoints_added == 2)
  133. {
  134. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], arm7_9->arm_bkpt);
  135. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0x0);
  136. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0xffffffffu);
  137. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  138. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  139. }
  140. else
  141. {
  142. LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
  143. return ERROR_FAIL;
  144. }
  145. return jtag_execute_queue();
  146. }
  147. /**
  148. * Setup the common pieces for an ARM7/9 target after reset or on startup.
  149. *
  150. * @param target Pointer to an ARM7/9 target to setup
  151. * @return Result of clearing the watchpoints on the target
  152. */
  153. int arm7_9_setup(target_t *target)
  154. {
  155. armv4_5_common_t *armv4_5 = target->arch_info;
  156. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  157. return arm7_9_clear_watchpoints(arm7_9);
  158. }
  159. /**
  160. * Retrieves the architecture information pointers for ARMv4/5 and ARM7/9
  161. * targets. A return of ERROR_OK signifies that the target is a valid target
  162. * and that the pointers have been set properly.
  163. *
  164. * @param target Pointer to the target device to get the pointers from
  165. * @param armv4_5_p Pointer to be filled in with the common struct for ARMV4/5
  166. * targets
  167. * @param arm7_9_p Pointer to be filled in with the common struct for ARM7/9
  168. * targets
  169. * @return ERROR_OK if successful
  170. */
  171. int arm7_9_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p)
  172. {
  173. armv4_5_common_t *armv4_5 = target->arch_info;
  174. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  175. if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
  176. {
  177. return -1;
  178. }
  179. if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
  180. {
  181. return -1;
  182. }
  183. *armv4_5_p = armv4_5;
  184. *arm7_9_p = arm7_9;
  185. return ERROR_OK;
  186. }
  187. /**
  188. * Set either a hardware or software breakpoint on an ARM7/9 target. The
  189. * breakpoint is set up even if it is already set. Some actions, e.g. reset,
  190. * might have erased the values in Embedded ICE.
  191. *
  192. * @param target Pointer to the target device to set the breakpoints on
  193. * @param breakpoint Pointer to the breakpoint to be set
  194. * @return For hardware breakpoints, this is the result of executing the JTAG
  195. * queue. For software breakpoints, this will be the status of the
  196. * required memory reads and writes
  197. */
  198. int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  199. {
  200. armv4_5_common_t *armv4_5 = target->arch_info;
  201. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  202. int retval = ERROR_OK;
  203. if (target->state != TARGET_HALTED)
  204. {
  205. LOG_WARNING("target not halted");
  206. return ERROR_TARGET_NOT_HALTED;
  207. }
  208. if (breakpoint->type == BKPT_HARD)
  209. {
  210. /* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
  211. uint32_t mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
  212. /* reassign a hw breakpoint */
  213. if (breakpoint->set == 0)
  214. {
  215. arm7_9_assign_wp(arm7_9, breakpoint);
  216. }
  217. if (breakpoint->set == 1)
  218. {
  219. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], breakpoint->address);
  220. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
  221. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffffu);
  222. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  223. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  224. }
  225. else if (breakpoint->set == 2)
  226. {
  227. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], breakpoint->address);
  228. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
  229. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffffu);
  230. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  231. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  232. }
  233. else
  234. {
  235. LOG_ERROR("BUG: no hardware comparator available");
  236. return ERROR_OK;
  237. }
  238. retval = jtag_execute_queue();
  239. }
  240. else if (breakpoint->type == BKPT_SOFT)
  241. {
  242. if ((retval = arm7_9_set_software_breakpoints(arm7_9)) != ERROR_OK)
  243. return retval;
  244. /* did we already set this breakpoint? */
  245. if (breakpoint->set)
  246. return ERROR_OK;
  247. if (breakpoint->length == 4)
  248. {
  249. uint32_t verify = 0xffffffff;
  250. /* keep the original instruction in target endianness */
  251. if ((retval = target_read_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
  252. {
  253. return retval;
  254. }
  255. /* write the breakpoint instruction in target endianness (arm7_9->arm_bkpt is host endian) */
  256. if ((retval = target_write_u32(target, breakpoint->address, arm7_9->arm_bkpt)) != ERROR_OK)
  257. {
  258. return retval;
  259. }
  260. if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
  261. {
  262. return retval;
  263. }
  264. if (verify != arm7_9->arm_bkpt)
  265. {
  266. LOG_ERROR("Unable to set 32 bit software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
  267. return ERROR_OK;
  268. }
  269. }
  270. else
  271. {
  272. uint16_t verify = 0xffff;
  273. /* keep the original instruction in target endianness */
  274. if ((retval = target_read_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
  275. {
  276. return retval;
  277. }
  278. /* write the breakpoint instruction in target endianness (arm7_9->thumb_bkpt is host endian) */
  279. if ((retval = target_write_u16(target, breakpoint->address, arm7_9->thumb_bkpt)) != ERROR_OK)
  280. {
  281. return retval;
  282. }
  283. if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
  284. {
  285. return retval;
  286. }
  287. if (verify != arm7_9->thumb_bkpt)
  288. {
  289. LOG_ERROR("Unable to set thumb software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
  290. return ERROR_OK;
  291. }
  292. }
  293. breakpoint->set = 1;
  294. }
  295. return retval;
  296. }
  297. /**
  298. * Unsets an existing breakpoint on an ARM7/9 target. If it is a hardware
  299. * breakpoint, the watchpoint used will be freed and the Embedded ICE registers
  300. * will be updated. Otherwise, the software breakpoint will be restored to its
  301. * original instruction if it hasn't already been modified.
  302. *
  303. * @param target Pointer to ARM7/9 target to unset the breakpoint from
  304. * @param breakpoint Pointer to breakpoint to be unset
  305. * @return For hardware breakpoints, this is the result of executing the JTAG
  306. * queue. For software breakpoints, this will be the status of the
  307. * required memory reads and writes
  308. */
  309. int arm7_9_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  310. {
  311. int retval = ERROR_OK;
  312. armv4_5_common_t *armv4_5 = target->arch_info;
  313. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  314. if (!breakpoint->set)
  315. {
  316. LOG_WARNING("breakpoint not set");
  317. return ERROR_OK;
  318. }
  319. if (breakpoint->type == BKPT_HARD)
  320. {
  321. if (breakpoint->set == 1)
  322. {
  323. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
  324. arm7_9->wp0_used = 0;
  325. arm7_9->wp_available++;
  326. }
  327. else if (breakpoint->set == 2)
  328. {
  329. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
  330. arm7_9->wp1_used = 0;
  331. arm7_9->wp_available++;
  332. }
  333. retval = jtag_execute_queue();
  334. breakpoint->set = 0;
  335. }
  336. else
  337. {
  338. /* restore original instruction (kept in target endianness) */
  339. if (breakpoint->length == 4)
  340. {
  341. uint32_t current_instr;
  342. /* check that user program as not modified breakpoint instruction */
  343. if ((retval = target_read_memory(target, breakpoint->address, 4, 1, (uint8_t*)&current_instr)) != ERROR_OK)
  344. {
  345. return retval;
  346. }
  347. if (current_instr == arm7_9->arm_bkpt)
  348. if ((retval = target_write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
  349. {
  350. return retval;
  351. }
  352. }
  353. else
  354. {
  355. uint16_t current_instr;
  356. /* check that user program as not modified breakpoint instruction */
  357. if ((retval = target_read_memory(target, breakpoint->address, 2, 1, (uint8_t*)&current_instr)) != ERROR_OK)
  358. {
  359. return retval;
  360. }
  361. if (current_instr == arm7_9->thumb_bkpt)
  362. if ((retval = target_write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
  363. {
  364. return retval;
  365. }
  366. }
  367. breakpoint->set = 0;
  368. }
  369. return retval;
  370. }
  371. /**
  372. * Add a breakpoint to an ARM7/9 target. This makes sure that there are no
  373. * dangling breakpoints and that the desired breakpoint can be added.
  374. *
  375. * @param target Pointer to the target ARM7/9 device to add a breakpoint to
  376. * @param breakpoint Pointer to the breakpoint to be added
  377. * @return An error status if there is a problem adding the breakpoint or the
  378. * result of setting the breakpoint
  379. */
  380. int arm7_9_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  381. {
  382. armv4_5_common_t *armv4_5 = target->arch_info;
  383. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  384. if (target->state != TARGET_HALTED)
  385. {
  386. LOG_WARNING("target not halted");
  387. return ERROR_TARGET_NOT_HALTED;
  388. }
  389. if (arm7_9->breakpoint_count == 0)
  390. {
  391. /* make sure we don't have any dangling breakpoints. This is vital upon
  392. * GDB connect/disconnect
  393. */
  394. arm7_9_clear_watchpoints(arm7_9);
  395. }
  396. if ((breakpoint->type == BKPT_HARD) && (arm7_9->wp_available < 1))
  397. {
  398. LOG_INFO("no watchpoint unit available for hardware breakpoint");
  399. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  400. }
  401. if ((breakpoint->length != 2) && (breakpoint->length != 4))
  402. {
  403. LOG_INFO("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
  404. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  405. }
  406. if (breakpoint->type == BKPT_HARD)
  407. {
  408. arm7_9_assign_wp(arm7_9, breakpoint);
  409. }
  410. arm7_9->breakpoint_count++;
  411. return arm7_9_set_breakpoint(target, breakpoint);
  412. }
  413. /**
  414. * Removes a breakpoint from an ARM7/9 target. This will make sure there are no
  415. * dangling breakpoints and updates available watchpoints if it is a hardware
  416. * breakpoint.
  417. *
  418. * @param target Pointer to the target to have a breakpoint removed
  419. * @param breakpoint Pointer to the breakpoint to be removed
  420. * @return Error status if there was a problem unsetting the breakpoint or the
  421. * watchpoints could not be cleared
  422. */
  423. int arm7_9_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  424. {
  425. int retval = ERROR_OK;
  426. armv4_5_common_t *armv4_5 = target->arch_info;
  427. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  428. if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
  429. {
  430. return retval;
  431. }
  432. if (breakpoint->type == BKPT_HARD)
  433. arm7_9->wp_available++;
  434. arm7_9->breakpoint_count--;
  435. if (arm7_9->breakpoint_count == 0)
  436. {
  437. /* make sure we don't have any dangling breakpoints */
  438. if ((retval = arm7_9_clear_watchpoints(arm7_9)) != ERROR_OK)
  439. {
  440. return retval;
  441. }
  442. }
  443. return ERROR_OK;
  444. }
  445. /**
  446. * Sets a watchpoint for an ARM7/9 target in one of the watchpoint units. It is
  447. * considered a bug to call this function when there are no available watchpoint
  448. * units.
  449. *
  450. * @param target Pointer to an ARM7/9 target to set a watchpoint on
  451. * @param watchpoint Pointer to the watchpoint to be set
  452. * @return Error status if watchpoint set fails or the result of executing the
  453. * JTAG queue
  454. */
  455. int arm7_9_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  456. {
  457. int retval = ERROR_OK;
  458. armv4_5_common_t *armv4_5 = target->arch_info;
  459. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  460. int rw_mask = 1;
  461. uint32_t mask;
  462. mask = watchpoint->length - 1;
  463. if (target->state != TARGET_HALTED)
  464. {
  465. LOG_WARNING("target not halted");
  466. return ERROR_TARGET_NOT_HALTED;
  467. }
  468. if (watchpoint->rw == WPT_ACCESS)
  469. rw_mask = 0;
  470. else
  471. rw_mask = 1;
  472. if (!arm7_9->wp0_used)
  473. {
  474. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], watchpoint->address);
  475. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
  476. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], watchpoint->mask);
  477. if ( watchpoint->mask != 0xffffffffu )
  478. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], watchpoint->value);
  479. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
  480. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
  481. if ((retval = jtag_execute_queue()) != ERROR_OK)
  482. {
  483. return retval;
  484. }
  485. watchpoint->set = 1;
  486. arm7_9->wp0_used = 2;
  487. }
  488. else if (!arm7_9->wp1_used)
  489. {
  490. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], watchpoint->address);
  491. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
  492. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], watchpoint->mask);
  493. if ( watchpoint->mask != 0xffffffffu )
  494. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], watchpoint->value);
  495. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
  496. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
  497. if ((retval = jtag_execute_queue()) != ERROR_OK)
  498. {
  499. return retval;
  500. }
  501. watchpoint->set = 2;
  502. arm7_9->wp1_used = 2;
  503. }
  504. else
  505. {
  506. LOG_ERROR("BUG: no hardware comparator available");
  507. return ERROR_OK;
  508. }
  509. return ERROR_OK;
  510. }
  511. /**
  512. * Unset an existing watchpoint and clear the used watchpoint unit.
  513. *
  514. * @param target Pointer to the target to have the watchpoint removed
  515. * @param watchpoint Pointer to the watchpoint to be removed
  516. * @return Error status while trying to unset the watchpoint or the result of
  517. * executing the JTAG queue
  518. */
  519. int arm7_9_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  520. {
  521. int retval = ERROR_OK;
  522. armv4_5_common_t *armv4_5 = target->arch_info;
  523. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  524. if (target->state != TARGET_HALTED)
  525. {
  526. LOG_WARNING("target not halted");
  527. return ERROR_TARGET_NOT_HALTED;
  528. }
  529. if (!watchpoint->set)
  530. {
  531. LOG_WARNING("breakpoint not set");
  532. return ERROR_OK;
  533. }
  534. if (watchpoint->set == 1)
  535. {
  536. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
  537. if ((retval = jtag_execute_queue()) != ERROR_OK)
  538. {
  539. return retval;
  540. }
  541. arm7_9->wp0_used = 0;
  542. }
  543. else if (watchpoint->set == 2)
  544. {
  545. embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
  546. if ((retval = jtag_execute_queue()) != ERROR_OK)
  547. {
  548. return retval;
  549. }
  550. arm7_9->wp1_used = 0;
  551. }
  552. watchpoint->set = 0;
  553. return ERROR_OK;
  554. }
  555. /**
  556. * Add a watchpoint to an ARM7/9 target. If there are no watchpoint units
  557. * available, an error response is returned.
  558. *
  559. * @param target Pointer to the ARM7/9 target to add a watchpoint to
  560. * @param watchpoint Pointer to the watchpoint to be added
  561. * @return Error status while trying to add the watchpoint
  562. */
  563. int arm7_9_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  564. {
  565. armv4_5_common_t *armv4_5 = target->arch_info;
  566. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  567. if (target->state != TARGET_HALTED)
  568. {
  569. LOG_WARNING("target not halted");
  570. return ERROR_TARGET_NOT_HALTED;
  571. }
  572. if (arm7_9->wp_available < 1)
  573. {
  574. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  575. }
  576. if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
  577. {
  578. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  579. }
  580. arm7_9->wp_available--;
  581. return ERROR_OK;
  582. }
  583. /**
  584. * Remove a watchpoint from an ARM7/9 target. The watchpoint will be unset and
  585. * the used watchpoint unit will be reopened.
  586. *
  587. * @param target Pointer to the target to remove a watchpoint from
  588. * @param watchpoint Pointer to the watchpoint to be removed
  589. * @return Result of trying to unset the watchpoint
  590. */
  591. int arm7_9_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  592. {
  593. int retval = ERROR_OK;
  594. armv4_5_common_t *armv4_5 = target->arch_info;
  595. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  596. if (watchpoint->set)
  597. {
  598. if ((retval = arm7_9_unset_watchpoint(target, watchpoint)) != ERROR_OK)
  599. {
  600. return retval;
  601. }
  602. }
  603. arm7_9->wp_available++;
  604. return ERROR_OK;
  605. }
  606. /**
  607. * Restarts the target by sending a RESTART instruction and moving the JTAG
  608. * state to IDLE. This includes a timeout waiting for DBGACK and SYSCOMP to be
  609. * asserted by the processor.
  610. *
  611. * @param target Pointer to target to issue commands to
  612. * @return Error status if there is a timeout or a problem while executing the
  613. * JTAG queue
  614. */
  615. int arm7_9_execute_sys_speed(struct target_s *target)
  616. {
  617. int retval;
  618. armv4_5_common_t *armv4_5 = target->arch_info;
  619. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  620. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  621. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  622. /* set RESTART instruction */
  623. jtag_set_end_state(TAP_IDLE);
  624. if (arm7_9->need_bypass_before_restart) {
  625. arm7_9->need_bypass_before_restart = 0;
  626. arm_jtag_set_instr(jtag_info, 0xf, NULL);
  627. }
  628. arm_jtag_set_instr(jtag_info, 0x4, NULL);
  629. long long then = timeval_ms();
  630. int timeout;
  631. while (!(timeout = ((timeval_ms()-then) > 1000)))
  632. {
  633. /* read debug status register */
  634. embeddedice_read_reg(dbg_stat);
  635. if ((retval = jtag_execute_queue()) != ERROR_OK)
  636. return retval;
  637. if ((buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
  638. && (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_SYSCOMP, 1)))
  639. break;
  640. if (debug_level >= 3)
  641. {
  642. alive_sleep(100);
  643. } else
  644. {
  645. keep_alive();
  646. }
  647. }
  648. if (timeout)
  649. {
  650. LOG_ERROR("timeout waiting for SYSCOMP & DBGACK, last DBG_STATUS: %" PRIx32 "", buf_get_u32(dbg_stat->value, 0, dbg_stat->size));
  651. return ERROR_TARGET_TIMEOUT;
  652. }
  653. return ERROR_OK;
  654. }
  655. /**
  656. * Restarts the target by sending a RESTART instruction and moving the JTAG
  657. * state to IDLE. This validates that DBGACK and SYSCOMP are set without
  658. * waiting until they are.
  659. *
  660. * @param target Pointer to the target to issue commands to
  661. * @return Always ERROR_OK
  662. */
  663. int arm7_9_execute_fast_sys_speed(struct target_s *target)
  664. {
  665. static int set = 0;
  666. static uint8_t check_value[4], check_mask[4];
  667. armv4_5_common_t *armv4_5 = target->arch_info;
  668. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  669. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  670. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  671. /* set RESTART instruction */
  672. jtag_set_end_state(TAP_IDLE);
  673. if (arm7_9->need_bypass_before_restart) {
  674. arm7_9->need_bypass_before_restart = 0;
  675. arm_jtag_set_instr(jtag_info, 0xf, NULL);
  676. }
  677. arm_jtag_set_instr(jtag_info, 0x4, NULL);
  678. if (!set)
  679. {
  680. /* check for DBGACK and SYSCOMP set (others don't care) */
  681. /* NB! These are constants that must be available until after next jtag_execute() and
  682. * we evaluate the values upon first execution in lieu of setting up these constants
  683. * during early setup.
  684. * */
  685. buf_set_u32(check_value, 0, 32, 0x9);
  686. buf_set_u32(check_mask, 0, 32, 0x9);
  687. set = 1;
  688. }
  689. /* read debug status register */
  690. embeddedice_read_reg_w_check(dbg_stat, check_value, check_mask);
  691. return ERROR_OK;
  692. }
  693. /**
  694. * Get some data from the ARM7/9 target.
  695. *
  696. * @param target Pointer to the ARM7/9 target to read data from
  697. * @param size The number of 32bit words to be read
  698. * @param buffer Pointer to the buffer that will hold the data
  699. * @return The result of receiving data from the Embedded ICE unit
  700. */
  701. int arm7_9_target_request_data(target_t *target, uint32_t size, uint8_t *buffer)
  702. {
  703. armv4_5_common_t *armv4_5 = target->arch_info;
  704. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  705. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  706. uint32_t *data;
  707. int retval = ERROR_OK;
  708. uint32_t i;
  709. data = malloc(size * (sizeof(uint32_t)));
  710. retval = embeddedice_receive(jtag_info, data, size);
  711. /* return the 32-bit ints in the 8-bit array */
  712. for (i = 0; i < size; i++)
  713. {
  714. h_u32_to_le(buffer + (i * 4), data[i]);
  715. }
  716. free(data);
  717. return retval;
  718. }
  719. /**
  720. * Handles requests to an ARM7/9 target. If debug messaging is enabled, the
  721. * target is running and the DCC control register has the W bit high, this will
  722. * execute the request on the target.
  723. *
  724. * @param priv Void pointer expected to be a target_t pointer
  725. * @return ERROR_OK unless there are issues with the JTAG queue or when reading
  726. * from the Embedded ICE unit
  727. */
  728. int arm7_9_handle_target_request(void *priv)
  729. {
  730. int retval = ERROR_OK;
  731. target_t *target = priv;
  732. if (!target_was_examined(target))
  733. return ERROR_OK;
  734. armv4_5_common_t *armv4_5 = target->arch_info;
  735. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  736. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  737. reg_t *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
  738. if (!target->dbg_msg_enabled)
  739. return ERROR_OK;
  740. if (target->state == TARGET_RUNNING)
  741. {
  742. /* read DCC control register */
  743. embeddedice_read_reg(dcc_control);
  744. if ((retval = jtag_execute_queue()) != ERROR_OK)
  745. {
  746. return retval;
  747. }
  748. /* check W bit */
  749. if (buf_get_u32(dcc_control->value, 1, 1) == 1)
  750. {
  751. uint32_t request;
  752. if ((retval = embeddedice_receive(jtag_info, &request, 1)) != ERROR_OK)
  753. {
  754. return retval;
  755. }
  756. if ((retval = target_request(target, request)) != ERROR_OK)
  757. {
  758. return retval;
  759. }
  760. }
  761. }
  762. return ERROR_OK;
  763. }
  764. /**
  765. * Polls an ARM7/9 target for its current status. If DBGACK is set, the target
  766. * is manipulated to the right halted state based on its current state. This is
  767. * what happens:
  768. *
  769. * <table>
  770. * <tr><th > State</th><th > Action</th></tr>
  771. * <tr><td > TARGET_RUNNING | TARGET_RESET</td><td > Enters debug mode. If TARGET_RESET, pc may be checked</td></tr>
  772. * <tr><td > TARGET_UNKNOWN</td><td > Warning is logged</td></tr>
  773. * <tr><td > TARGET_DEBUG_RUNNING</td><td > Enters debug mode</td></tr>
  774. * <tr><td > TARGET_HALTED</td><td > Nothing</td></tr>
  775. * </table>
  776. *
  777. * If the target does not end up in the halted state, a warning is produced. If
  778. * DBGACK is cleared, then the target is expected to either be running or
  779. * running in debug.
  780. *
  781. * @param target Pointer to the ARM7/9 target to poll
  782. * @return ERROR_OK or an error status if a command fails
  783. */
  784. int arm7_9_poll(target_t *target)
  785. {
  786. int retval;
  787. armv4_5_common_t *armv4_5 = target->arch_info;
  788. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  789. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  790. /* read debug status register */
  791. embeddedice_read_reg(dbg_stat);
  792. if ((retval = jtag_execute_queue()) != ERROR_OK)
  793. {
  794. return retval;
  795. }
  796. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
  797. {
  798. /* LOG_DEBUG("DBGACK set, dbg_state->value: 0x%x", buf_get_u32(dbg_stat->value, 0, 32));*/
  799. if (target->state == TARGET_UNKNOWN)
  800. {
  801. target->state = TARGET_RUNNING;
  802. LOG_WARNING("DBGACK set while target was in unknown state. Reset or initialize target.");
  803. }
  804. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
  805. {
  806. int check_pc = 0;
  807. if (target->state == TARGET_RESET)
  808. {
  809. if (target->reset_halt)
  810. {
  811. enum reset_types jtag_reset_config = jtag_get_reset_config();
  812. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
  813. {
  814. check_pc = 1;
  815. }
  816. }
  817. }
  818. target->state = TARGET_HALTED;
  819. if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
  820. return retval;
  821. if (check_pc)
  822. {
  823. reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
  824. uint32_t t=*((uint32_t *)reg->value);
  825. if (t != 0)
  826. {
  827. LOG_ERROR("PC was not 0. Does this target need srst_pulls_trst?");
  828. }
  829. }
  830. if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
  831. {
  832. return retval;
  833. }
  834. }
  835. if (target->state == TARGET_DEBUG_RUNNING)
  836. {
  837. target->state = TARGET_HALTED;
  838. if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
  839. return retval;
  840. if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED)) != ERROR_OK)
  841. {
  842. return retval;
  843. }
  844. }
  845. if (target->state != TARGET_HALTED)
  846. {
  847. LOG_WARNING("DBGACK set, but the target did not end up in the halted state %d", target->state);
  848. }
  849. }
  850. else
  851. {
  852. if (target->state != TARGET_DEBUG_RUNNING)
  853. target->state = TARGET_RUNNING;
  854. }
  855. return ERROR_OK;
  856. }
  857. /**
  858. * Asserts the reset (SRST) on an ARM7/9 target. Some -S targets (ARM966E-S in
  859. * the STR912 isn't affected, ARM926EJ-S in the LPC3180 and AT91SAM9260 is
  860. * affected) completely stop the JTAG clock while the core is held in reset
  861. * (SRST). It isn't possible to program the halt condition once reset is
  862. * asserted, hence a hook that allows the target to set up its reset-halt
  863. * condition is setup prior to asserting reset.
  864. *
  865. * @param target Pointer to an ARM7/9 target to assert reset on
  866. * @return ERROR_FAIL if the JTAG device does not have SRST, otherwise ERROR_OK
  867. */
  868. int arm7_9_assert_reset(target_t *target)
  869. {
  870. armv4_5_common_t *armv4_5 = target->arch_info;
  871. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  872. LOG_DEBUG("target->state: %s",
  873. Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
  874. enum reset_types jtag_reset_config = jtag_get_reset_config();
  875. if (!(jtag_reset_config & RESET_HAS_SRST))
  876. {
  877. LOG_ERROR("Can't assert SRST");
  878. return ERROR_FAIL;
  879. }
  880. if (target->reset_halt)
  881. {
  882. /*
  883. * Some targets do not support communication while SRST is asserted. We need to
  884. * set up the reset vector catch here.
  885. *
  886. * If TRST is asserted, then these settings will be reset anyway, so setting them
  887. * here is harmless.
  888. */
  889. if (arm7_9->has_vector_catch)
  890. {
  891. /* program vector catch register to catch reset vector */
  892. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH], 0x1);
  893. }
  894. else
  895. {
  896. /* program watchpoint unit to match on reset vector address */
  897. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], 0x0);
  898. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0x3);
  899. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
  900. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  901. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  902. }
  903. }
  904. /* here we should issue an SRST only, but we may have to assert TRST as well */
  905. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  906. {
  907. jtag_add_reset(1, 1);
  908. } else
  909. {
  910. jtag_add_reset(0, 1);
  911. }
  912. target->state = TARGET_RESET;
  913. jtag_add_sleep(50000);
  914. armv4_5_invalidate_core_regs(target);
  915. if ((target->reset_halt) && ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0))
  916. {
  917. /* debug entry was already prepared in arm7_9_assert_reset() */
  918. target->debug_reason = DBG_REASON_DBGRQ;
  919. }
  920. return ERROR_OK;
  921. }
  922. /**
  923. * Deassert the reset (SRST) signal on an ARM7/9 target. If SRST pulls TRST
  924. * and the target is being reset into a halt, a warning will be triggered
  925. * because it is not possible to reset into a halted mode in this case. The
  926. * target is halted using the target's functions.
  927. *
  928. * @param target Pointer to the target to have the reset deasserted
  929. * @return ERROR_OK or an error from polling or halting the target
  930. */
  931. int arm7_9_deassert_reset(target_t *target)
  932. {
  933. int retval = ERROR_OK;
  934. LOG_DEBUG("target->state: %s",
  935. Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
  936. /* deassert reset lines */
  937. jtag_add_reset(0, 0);
  938. enum reset_types jtag_reset_config = jtag_get_reset_config();
  939. if (target->reset_halt && (jtag_reset_config & RESET_SRST_PULLS_TRST) != 0)
  940. {
  941. LOG_WARNING("srst pulls trst - can not reset into halted mode. Issuing halt after reset.");
  942. /* set up embedded ice registers again */
  943. if ((retval = target_examine_one(target)) != ERROR_OK)
  944. return retval;
  945. if ((retval = target_poll(target)) != ERROR_OK)
  946. {
  947. return retval;
  948. }
  949. if ((retval = target_halt(target)) != ERROR_OK)
  950. {
  951. return retval;
  952. }
  953. }
  954. return retval;
  955. }
  956. /**
  957. * Clears the halt condition for an ARM7/9 target. If it isn't coming out of
  958. * reset and if DBGRQ is used, it is progammed to be deasserted. If the reset
  959. * vector catch was used, it is restored. Otherwise, the control value is
  960. * restored and the watchpoint unit is restored if it was in use.
  961. *
  962. * @param target Pointer to the ARM7/9 target to have halt cleared
  963. * @return Always ERROR_OK
  964. */
  965. int arm7_9_clear_halt(target_t *target)
  966. {
  967. armv4_5_common_t *armv4_5 = target->arch_info;
  968. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  969. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  970. /* we used DBGRQ only if we didn't come out of reset */
  971. if (!arm7_9->debug_entry_from_reset && arm7_9->use_dbgrq)
  972. {
  973. /* program EmbeddedICE Debug Control Register to deassert DBGRQ
  974. */
  975. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
  976. embeddedice_store_reg(dbg_ctrl);
  977. }
  978. else
  979. {
  980. if (arm7_9->debug_entry_from_reset && arm7_9->has_vector_catch)
  981. {
  982. /* if we came out of reset, and vector catch is supported, we used
  983. * vector catch to enter debug state
  984. * restore the register in that case
  985. */
  986. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH]);
  987. }
  988. else
  989. {
  990. /* restore registers if watchpoint unit 0 was in use
  991. */
  992. if (arm7_9->wp0_used)
  993. {
  994. if (arm7_9->debug_entry_from_reset)
  995. {
  996. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE]);
  997. }
  998. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
  999. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
  1000. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
  1001. }
  1002. /* control value always has to be restored, as it was either disabled,
  1003. * or enabled with possibly different bits
  1004. */
  1005. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
  1006. }
  1007. }
  1008. return ERROR_OK;
  1009. }
  1010. /**
  1011. * Issue a software reset and halt to an ARM7/9 target. The target is halted
  1012. * and then there is a wait until the processor shows the halt. This wait can
  1013. * timeout and results in an error being returned. The software reset involves
  1014. * clearing the halt, updating the debug control register, changing to ARM mode,
  1015. * reset of the program counter, and reset of all of the registers.
  1016. *
  1017. * @param target Pointer to the ARM7/9 target to be reset and halted by software
  1018. * @return Error status if any of the commands fail, otherwise ERROR_OK
  1019. */
  1020. int arm7_9_soft_reset_halt(struct target_s *target)
  1021. {
  1022. armv4_5_common_t *armv4_5 = target->arch_info;
  1023. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1024. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  1025. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  1026. int i;
  1027. int retval;
  1028. if ((retval = target_halt(target)) != ERROR_OK)
  1029. return retval;
  1030. long long then = timeval_ms();
  1031. int timeout;
  1032. while (!(timeout = ((timeval_ms()-then) > 1000)))
  1033. {
  1034. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
  1035. break;
  1036. embeddedice_read_reg(dbg_stat);
  1037. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1038. return retval;
  1039. if (debug_level >= 3)
  1040. {
  1041. alive_sleep(100);
  1042. } else
  1043. {
  1044. keep_alive();
  1045. }
  1046. }
  1047. if (timeout)
  1048. {
  1049. LOG_ERROR("Failed to halt CPU after 1 sec");
  1050. return ERROR_TARGET_TIMEOUT;
  1051. }
  1052. target->state = TARGET_HALTED;
  1053. /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
  1054. * ensure that DBGRQ is cleared
  1055. */
  1056. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
  1057. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
  1058. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
  1059. embeddedice_store_reg(dbg_ctrl);
  1060. if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
  1061. {
  1062. return retval;
  1063. }
  1064. /* if the target is in Thumb state, change to ARM state */
  1065. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
  1066. {
  1067. uint32_t r0_thumb, pc_thumb;
  1068. LOG_DEBUG("target entered debug from Thumb state, changing to ARM");
  1069. /* Entered debug from Thumb mode */
  1070. armv4_5->core_state = ARMV4_5_STATE_THUMB;
  1071. arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
  1072. }
  1073. /* all register content is now invalid */
  1074. if ((retval = armv4_5_invalidate_core_regs(target)) != ERROR_OK)
  1075. {
  1076. return retval;
  1077. }
  1078. /* SVC, ARM state, IRQ and FIQ disabled */
  1079. buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8, 0xd3);
  1080. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 1;
  1081. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
  1082. /* start fetching from 0x0 */
  1083. buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, 0x0);
  1084. armv4_5->core_cache->reg_list[15].dirty = 1;
  1085. armv4_5->core_cache->reg_list[15].valid = 1;
  1086. armv4_5->core_mode = ARMV4_5_MODE_SVC;
  1087. armv4_5->core_state = ARMV4_5_STATE_ARM;
  1088. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  1089. return ERROR_FAIL;
  1090. /* reset registers */
  1091. for (i = 0; i <= 14; i++)
  1092. {
  1093. buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, 0xffffffff);
  1094. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 1;
  1095. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
  1096. }
  1097. if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
  1098. {
  1099. return retval;
  1100. }
  1101. return ERROR_OK;
  1102. }
  1103. /**
  1104. * Halt an ARM7/9 target. This is accomplished by either asserting the DBGRQ
  1105. * line or by programming a watchpoint to trigger on any address. It is
  1106. * considered a bug to call this function while the target is in the
  1107. * TARGET_RESET state.
  1108. *
  1109. * @param target Pointer to the ARM7/9 target to be halted
  1110. * @return Always ERROR_OK
  1111. */
  1112. int arm7_9_halt(target_t *target)
  1113. {
  1114. if (target->state == TARGET_RESET)
  1115. {
  1116. LOG_ERROR("BUG: arm7/9 does not support halt during reset. This is handled in arm7_9_assert_reset()");
  1117. return ERROR_OK;
  1118. }
  1119. armv4_5_common_t *armv4_5 = target->arch_info;
  1120. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1121. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  1122. LOG_DEBUG("target->state: %s",
  1123. Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
  1124. if (target->state == TARGET_HALTED)
  1125. {
  1126. LOG_DEBUG("target was already halted");
  1127. return ERROR_OK;
  1128. }
  1129. if (target->state == TARGET_UNKNOWN)
  1130. {
  1131. LOG_WARNING("target was in unknown state when halt was requested");
  1132. }
  1133. if (arm7_9->use_dbgrq)
  1134. {
  1135. /* program EmbeddedICE Debug Control Register to assert DBGRQ
  1136. */
  1137. if (arm7_9->set_special_dbgrq) {
  1138. arm7_9->set_special_dbgrq(target);
  1139. } else {
  1140. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 1);
  1141. embeddedice_store_reg(dbg_ctrl);
  1142. }
  1143. }
  1144. else
  1145. {
  1146. /* program watchpoint unit to match on any address
  1147. */
  1148. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
  1149. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
  1150. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  1151. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  1152. }
  1153. target->debug_reason = DBG_REASON_DBGRQ;
  1154. return ERROR_OK;
  1155. }
  1156. /**
  1157. * Handle an ARM7/9 target's entry into debug mode. The halt is cleared on the
  1158. * ARM. The JTAG queue is then executed and the reason for debug entry is
  1159. * examined. Once done, the target is verified to be halted and the processor
  1160. * is forced into ARM mode. The core registers are saved for the current core
  1161. * mode and the program counter (register 15) is updated as needed. The core
  1162. * registers and CPSR and SPSR are saved for restoration later.
  1163. *
  1164. * @param target Pointer to target that is entering debug mode
  1165. * @return Error code if anything fails, otherwise ERROR_OK
  1166. */
  1167. int arm7_9_debug_entry(target_t *target)
  1168. {
  1169. int i;
  1170. uint32_t context[16];
  1171. uint32_t* context_p[16];
  1172. uint32_t r0_thumb, pc_thumb;
  1173. uint32_t cpsr;
  1174. int retval;
  1175. /* get pointers to arch-specific information */
  1176. armv4_5_common_t *armv4_5 = target->arch_info;
  1177. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1178. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  1179. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  1180. #ifdef _DEBUG_ARM7_9_
  1181. LOG_DEBUG("-");
  1182. #endif
  1183. if (arm7_9->pre_debug_entry)
  1184. arm7_9->pre_debug_entry(target);
  1185. /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
  1186. * ensure that DBGRQ is cleared
  1187. */
  1188. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
  1189. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
  1190. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
  1191. embeddedice_store_reg(dbg_ctrl);
  1192. if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
  1193. {
  1194. return retval;
  1195. }
  1196. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1197. {
  1198. return retval;
  1199. }
  1200. if ((retval = arm7_9->examine_debug_reason(target)) != ERROR_OK)
  1201. return retval;
  1202. if (target->state != TARGET_HALTED)
  1203. {
  1204. LOG_WARNING("target not halted");
  1205. return ERROR_TARGET_NOT_HALTED;
  1206. }
  1207. /* if the target is in Thumb state, change to ARM state */
  1208. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
  1209. {
  1210. LOG_DEBUG("target entered debug from Thumb state");
  1211. /* Entered debug from Thumb mode */
  1212. armv4_5->core_state = ARMV4_5_STATE_THUMB;
  1213. arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
  1214. LOG_DEBUG("r0_thumb: 0x%8.8" PRIx32 ", pc_thumb: 0x%8.8" PRIx32 "", r0_thumb, pc_thumb);
  1215. }
  1216. else
  1217. {
  1218. LOG_DEBUG("target entered debug from ARM state");
  1219. /* Entered debug from ARM mode */
  1220. armv4_5->core_state = ARMV4_5_STATE_ARM;
  1221. }
  1222. for (i = 0; i < 16; i++)
  1223. context_p[i] = &context[i];
  1224. /* save core registers (r0 - r15 of current core mode) */
  1225. arm7_9->read_core_regs(target, 0xffff, context_p);
  1226. arm7_9->read_xpsr(target, &cpsr, 0);
  1227. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1228. return retval;
  1229. /* if the core has been executing in Thumb state, set the T bit */
  1230. if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
  1231. cpsr |= 0x20;
  1232. buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32, cpsr);
  1233. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
  1234. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
  1235. armv4_5->core_mode = cpsr & 0x1f;
  1236. if (armv4_5_mode_to_number(armv4_5->core_mode) == -1)
  1237. {
  1238. target->state = TARGET_UNKNOWN;
  1239. LOG_ERROR("cpsr contains invalid mode value - communication failure");
  1240. return ERROR_TARGET_FAILURE;
  1241. }
  1242. LOG_DEBUG("target entered debug state in %s mode", armv4_5_mode_strings[armv4_5_mode_to_number(armv4_5->core_mode)]);
  1243. if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
  1244. {
  1245. LOG_DEBUG("thumb state, applying fixups");
  1246. context[0] = r0_thumb;
  1247. context[15] = pc_thumb;
  1248. } else if (armv4_5->core_state == ARMV4_5_STATE_ARM)
  1249. {
  1250. /* adjust value stored by STM */
  1251. context[15] -= 3 * 4;
  1252. }
  1253. if ((target->debug_reason == DBG_REASON_BREAKPOINT)
  1254. || (target->debug_reason == DBG_REASON_SINGLESTEP)
  1255. || (target->debug_reason == DBG_REASON_WATCHPOINT)
  1256. || (target->debug_reason == DBG_REASON_WPTANDBKPT)
  1257. || ((target->debug_reason == DBG_REASON_DBGRQ) && (arm7_9->use_dbgrq == 0)))
  1258. context[15] -= 3 * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
  1259. else if (target->debug_reason == DBG_REASON_DBGRQ)
  1260. context[15] -= arm7_9->dbgreq_adjust_pc * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
  1261. else
  1262. {
  1263. LOG_ERROR("unknown debug reason: %i", target->debug_reason);
  1264. }
  1265. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  1266. return ERROR_FAIL;
  1267. for (i = 0; i <= 15; i++)
  1268. {
  1269. LOG_DEBUG("r%i: 0x%8.8" PRIx32 "", i, context[i]);
  1270. buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, context[i]);
  1271. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 0;
  1272. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
  1273. }
  1274. LOG_DEBUG("entered debug state at PC 0x%" PRIx32 "", context[15]);
  1275. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  1276. return ERROR_FAIL;
  1277. /* exceptions other than USR & SYS have a saved program status register */
  1278. if ((armv4_5->core_mode != ARMV4_5_MODE_USR) && (armv4_5->core_mode != ARMV4_5_MODE_SYS))
  1279. {
  1280. uint32_t spsr;
  1281. arm7_9->read_xpsr(target, &spsr, 1);
  1282. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1283. {
  1284. return retval;
  1285. }
  1286. buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).value, 0, 32, spsr);
  1287. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).dirty = 0;
  1288. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).valid = 1;
  1289. }
  1290. /* r0 and r15 (pc) have to be restored later */
  1291. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 0).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 0).valid;
  1292. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 15).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 15).valid;
  1293. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1294. return retval;
  1295. if (arm7_9->post_debug_entry)
  1296. arm7_9->post_debug_entry(target);
  1297. return ERROR_OK;
  1298. }
  1299. /**
  1300. * Validate the full context for an ARM7/9 target in all processor modes. If
  1301. * there are any invalid registers for the target, they will all be read. This
  1302. * includes the PSR.
  1303. *
  1304. * @param target Pointer to the ARM7/9 target to capture the full context from
  1305. * @return Error if the target is not halted, has an invalid core mode, or if
  1306. * the JTAG queue fails to execute
  1307. */
  1308. int arm7_9_full_context(target_t *target)
  1309. {
  1310. int i;
  1311. int retval;
  1312. armv4_5_common_t *armv4_5 = target->arch_info;
  1313. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1314. LOG_DEBUG("-");
  1315. if (target->state != TARGET_HALTED)
  1316. {
  1317. LOG_WARNING("target not halted");
  1318. return ERROR_TARGET_NOT_HALTED;
  1319. }
  1320. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  1321. return ERROR_FAIL;
  1322. /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
  1323. * SYS shares registers with User, so we don't touch SYS
  1324. */
  1325. for (i = 0; i < 6; i++)
  1326. {
  1327. uint32_t mask = 0;
  1328. uint32_t* reg_p[16];
  1329. int j;
  1330. int valid = 1;
  1331. /* check if there are invalid registers in the current mode
  1332. */
  1333. for (j = 0; j <= 16; j++)
  1334. {
  1335. if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
  1336. valid = 0;
  1337. }
  1338. if (!valid)
  1339. {
  1340. uint32_t tmp_cpsr;
  1341. /* change processor mode (and mask T bit) */
  1342. tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
  1343. tmp_cpsr |= armv4_5_number_to_mode(i);
  1344. tmp_cpsr &= ~0x20;
  1345. arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
  1346. for (j = 0; j < 15; j++)
  1347. {
  1348. if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
  1349. {
  1350. reg_p[j] = (uint32_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).value;
  1351. mask |= 1 << j;
  1352. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid = 1;
  1353. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).dirty = 0;
  1354. }
  1355. }
  1356. /* if only the PSR is invalid, mask is all zeroes */
  1357. if (mask)
  1358. arm7_9->read_core_regs(target, mask, reg_p);
  1359. /* check if the PSR has to be read */
  1360. if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid == 0)
  1361. {
  1362. arm7_9->read_xpsr(target, (uint32_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).value, 1);
  1363. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid = 1;
  1364. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).dirty = 0;
  1365. }
  1366. }
  1367. }
  1368. /* restore processor mode (mask T bit) */
  1369. arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
  1370. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1371. {
  1372. return retval;
  1373. }
  1374. return ERROR_OK;
  1375. }
  1376. /**
  1377. * Restore the processor context on an ARM7/9 target. The full processor
  1378. * context is analyzed to see if any of the registers are dirty on this end, but
  1379. * have a valid new value. If this is the case, the processor is changed to the
  1380. * appropriate mode and the new register values are written out to the
  1381. * processor. If there happens to be a dirty register with an invalid value, an
  1382. * error will be logged.
  1383. *
  1384. * @param target Pointer to the ARM7/9 target to have its context restored
  1385. * @return Error status if the target is not halted or the core mode in the
  1386. * armv4_5 struct is invalid.
  1387. */
  1388. int arm7_9_restore_context(target_t *target)
  1389. {
  1390. armv4_5_common_t *armv4_5 = target->arch_info;
  1391. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1392. reg_t *reg;
  1393. armv4_5_core_reg_t *reg_arch_info;
  1394. enum armv4_5_mode current_mode = armv4_5->core_mode;
  1395. int i, j;
  1396. int dirty;
  1397. int mode_change;
  1398. LOG_DEBUG("-");
  1399. if (target->state != TARGET_HALTED)
  1400. {
  1401. LOG_WARNING("target not halted");
  1402. return ERROR_TARGET_NOT_HALTED;
  1403. }
  1404. if (arm7_9->pre_restore_context)
  1405. arm7_9->pre_restore_context(target);
  1406. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  1407. return ERROR_FAIL;
  1408. /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
  1409. * SYS shares registers with User, so we don't touch SYS
  1410. */
  1411. for (i = 0; i < 6; i++)
  1412. {
  1413. LOG_DEBUG("examining %s mode", armv4_5_mode_strings[i]);
  1414. dirty = 0;
  1415. mode_change = 0;
  1416. /* check if there are dirty registers in the current mode
  1417. */
  1418. for (j = 0; j <= 16; j++)
  1419. {
  1420. reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
  1421. reg_arch_info = reg->arch_info;
  1422. if (reg->dirty == 1)
  1423. {
  1424. if (reg->valid == 1)
  1425. {
  1426. dirty = 1;
  1427. LOG_DEBUG("examining dirty reg: %s", reg->name);
  1428. if ((reg_arch_info->mode != ARMV4_5_MODE_ANY)
  1429. && (reg_arch_info->mode != current_mode)
  1430. && !((reg_arch_info->mode == ARMV4_5_MODE_USR) && (armv4_5->core_mode == ARMV4_5_MODE_SYS))
  1431. && !((reg_arch_info->mode == ARMV4_5_MODE_SYS) && (armv4_5->core_mode == ARMV4_5_MODE_USR)))
  1432. {
  1433. mode_change = 1;
  1434. LOG_DEBUG("require mode change");
  1435. }
  1436. }
  1437. else
  1438. {
  1439. LOG_ERROR("BUG: dirty register '%s', but no valid data", reg->name);
  1440. }
  1441. }
  1442. }
  1443. if (dirty)
  1444. {
  1445. uint32_t mask = 0x0;
  1446. int num_regs = 0;
  1447. uint32_t regs[16];
  1448. if (mode_change)
  1449. {
  1450. uint32_t tmp_cpsr;
  1451. /* change processor mode (mask T bit) */
  1452. tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
  1453. tmp_cpsr |= armv4_5_number_to_mode(i);
  1454. tmp_cpsr &= ~0x20;
  1455. arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
  1456. current_mode = armv4_5_number_to_mode(i);
  1457. }
  1458. for (j = 0; j <= 14; j++)
  1459. {
  1460. reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
  1461. reg_arch_info = reg->arch_info;
  1462. if (reg->dirty == 1)
  1463. {
  1464. regs[j] = buf_get_u32(reg->value, 0, 32);
  1465. mask |= 1 << j;
  1466. num_regs++;
  1467. reg->dirty = 0;
  1468. reg->valid = 1;
  1469. LOG_DEBUG("writing register %i of mode %s with value 0x%8.8" PRIx32 "", j, armv4_5_mode_strings[i], regs[j]);
  1470. }
  1471. }
  1472. if (mask)
  1473. {
  1474. arm7_9->write_core_regs(target, mask, regs);
  1475. }
  1476. reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16);
  1477. reg_arch_info = reg->arch_info;
  1478. if ((reg->dirty) && (reg_arch_info->mode != ARMV4_5_MODE_ANY))
  1479. {
  1480. LOG_DEBUG("writing SPSR of mode %i with value 0x%8.8" PRIx32 "", i, buf_get_u32(reg->value, 0, 32));
  1481. arm7_9->write_xpsr(target, buf_get_u32(reg->value, 0, 32), 1);
  1482. }
  1483. }
  1484. }
  1485. if ((armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 0) && (armv4_5->core_mode != current_mode))
  1486. {
  1487. /* restore processor mode (mask T bit) */
  1488. uint32_t tmp_cpsr;
  1489. tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
  1490. tmp_cpsr |= armv4_5_number_to_mode(i);
  1491. tmp_cpsr &= ~0x20;
  1492. LOG_DEBUG("writing lower 8 bit of cpsr with value 0x%2.2x", (unsigned)(tmp_cpsr));
  1493. arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
  1494. }
  1495. else if (armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 1)
  1496. {
  1497. /* CPSR has been changed, full restore necessary (mask T bit) */
  1498. LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32));
  1499. arm7_9->write_xpsr(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32) & ~0x20, 0);
  1500. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
  1501. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
  1502. }
  1503. /* restore PC */
  1504. LOG_DEBUG("writing PC with value 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
  1505. arm7_9->write_pc(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
  1506. armv4_5->core_cache->reg_list[15].dirty = 0;
  1507. if (arm7_9->post_restore_context)
  1508. arm7_9->post_restore_context(target);
  1509. return ERROR_OK;
  1510. }
  1511. /**
  1512. * Restart the core of an ARM7/9 target. A RESTART command is sent to the
  1513. * instruction register and the JTAG state is set to TAP_IDLE causing a core
  1514. * restart.
  1515. *
  1516. * @param target Pointer to the ARM7/9 target to be restarted
  1517. * @return Result of executing the JTAG queue
  1518. */
  1519. int arm7_9_restart_core(struct target_s *target)
  1520. {
  1521. armv4_5_common_t *armv4_5 = target->arch_info;
  1522. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1523. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  1524. /* set RESTART instruction */
  1525. jtag_set_end_state(TAP_IDLE);
  1526. if (arm7_9->need_bypass_before_restart) {
  1527. arm7_9->need_bypass_before_restart = 0;
  1528. arm_jtag_set_instr(jtag_info, 0xf, NULL);
  1529. }
  1530. arm_jtag_set_instr(jtag_info, 0x4, NULL);
  1531. jtag_add_runtest(1, jtag_set_end_state(TAP_IDLE));
  1532. return jtag_execute_queue();
  1533. }
  1534. /**
  1535. * Enable the watchpoints on an ARM7/9 target. The target's watchpoints are
  1536. * iterated through and are set on the target if they aren't already set.
  1537. *
  1538. * @param target Pointer to the ARM7/9 target to enable watchpoints on
  1539. */
  1540. void arm7_9_enable_watchpoints(struct target_s *target)
  1541. {
  1542. watchpoint_t *watchpoint = target->watchpoints;
  1543. while (watchpoint)
  1544. {
  1545. if (watchpoint->set == 0)
  1546. arm7_9_set_watchpoint(target, watchpoint);
  1547. watchpoint = watchpoint->next;
  1548. }
  1549. }
  1550. /**
  1551. * Enable the breakpoints on an ARM7/9 target. The target's breakpoints are
  1552. * iterated through and are set on the target.
  1553. *
  1554. * @param target Pointer to the ARM7/9 target to enable breakpoints on
  1555. */
  1556. void arm7_9_enable_breakpoints(struct target_s *target)
  1557. {
  1558. breakpoint_t *breakpoint = target->breakpoints;
  1559. /* set any pending breakpoints */
  1560. while (breakpoint)
  1561. {
  1562. arm7_9_set_breakpoint(target, breakpoint);
  1563. breakpoint = breakpoint->next;
  1564. }
  1565. }
  1566. int arm7_9_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
  1567. {
  1568. armv4_5_common_t *armv4_5 = target->arch_info;
  1569. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1570. breakpoint_t *breakpoint = target->breakpoints;
  1571. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  1572. int err, retval = ERROR_OK;
  1573. LOG_DEBUG("-");
  1574. if (target->state != TARGET_HALTED)
  1575. {
  1576. LOG_WARNING("target not halted");
  1577. return ERROR_TARGET_NOT_HALTED;
  1578. }
  1579. if (!debug_execution)
  1580. {
  1581. target_free_all_working_areas(target);
  1582. }
  1583. /* current = 1: continue on current pc, otherwise continue at <address> */
  1584. if (!current)
  1585. buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
  1586. uint32_t current_pc;
  1587. current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
  1588. /* the front-end may request us not to handle breakpoints */
  1589. if (handle_breakpoints)
  1590. {
  1591. if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
  1592. {
  1593. LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
  1594. if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
  1595. {
  1596. return retval;
  1597. }
  1598. /* calculate PC of next instruction */
  1599. uint32_t next_pc;
  1600. if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
  1601. {
  1602. uint32_t current_opcode;
  1603. target_read_u32(target, current_pc, &current_opcode);
  1604. LOG_ERROR("BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
  1605. return retval;
  1606. }
  1607. LOG_DEBUG("enable single-step");
  1608. arm7_9->enable_single_step(target, next_pc);
  1609. target->debug_reason = DBG_REASON_SINGLESTEP;
  1610. if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
  1611. {
  1612. return retval;
  1613. }
  1614. if (armv4_5->core_state == ARMV4_5_STATE_ARM)
  1615. arm7_9->branch_resume(target);
  1616. else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
  1617. {
  1618. arm7_9->branch_resume_thumb(target);
  1619. }
  1620. else
  1621. {
  1622. LOG_ERROR("unhandled core state");
  1623. return ERROR_FAIL;
  1624. }
  1625. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
  1626. embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
  1627. err = arm7_9_execute_sys_speed(target);
  1628. LOG_DEBUG("disable single-step");
  1629. arm7_9->disable_single_step(target);
  1630. if (err != ERROR_OK)
  1631. {
  1632. if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
  1633. {
  1634. return retval;
  1635. }
  1636. target->state = TARGET_UNKNOWN;
  1637. return err;
  1638. }
  1639. arm7_9_debug_entry(target);
  1640. LOG_DEBUG("new PC after step: 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
  1641. LOG_DEBUG("set breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
  1642. if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
  1643. {
  1644. return retval;
  1645. }
  1646. }
  1647. }
  1648. /* enable any pending breakpoints and watchpoints */
  1649. arm7_9_enable_breakpoints(target);
  1650. arm7_9_enable_watchpoints(target);
  1651. if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
  1652. {
  1653. return retval;
  1654. }
  1655. if (armv4_5->core_state == ARMV4_5_STATE_ARM)
  1656. {
  1657. arm7_9->branch_resume(target);
  1658. }
  1659. else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
  1660. {
  1661. arm7_9->branch_resume_thumb(target);
  1662. }
  1663. else
  1664. {
  1665. LOG_ERROR("unhandled core state");
  1666. return ERROR_FAIL;
  1667. }
  1668. /* deassert DBGACK and INTDIS */
  1669. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
  1670. /* INTDIS only when we really resume, not during debug execution */
  1671. if (!debug_execution)
  1672. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 0);
  1673. embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
  1674. if ((retval = arm7_9_restart_core(target)) != ERROR_OK)
  1675. {
  1676. return retval;
  1677. }
  1678. target->debug_reason = DBG_REASON_NOTHALTED;
  1679. if (!debug_execution)
  1680. {
  1681. /* registers are now invalid */
  1682. armv4_5_invalidate_core_regs(target);
  1683. target->state = TARGET_RUNNING;
  1684. if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
  1685. {
  1686. return retval;
  1687. }
  1688. }
  1689. else
  1690. {
  1691. target->state = TARGET_DEBUG_RUNNING;
  1692. if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED)) != ERROR_OK)
  1693. {
  1694. return retval;
  1695. }
  1696. }
  1697. LOG_DEBUG("target resumed");
  1698. return ERROR_OK;
  1699. }
  1700. void arm7_9_enable_eice_step(target_t *target, uint32_t next_pc)
  1701. {
  1702. armv4_5_common_t *armv4_5 = target->arch_info;
  1703. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1704. uint32_t current_pc;
  1705. current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
  1706. if (next_pc != current_pc)
  1707. {
  1708. /* setup an inverse breakpoint on the current PC
  1709. * - comparator 1 matches the current address
  1710. * - rangeout from comparator 1 is connected to comparator 0 rangein
  1711. * - comparator 0 matches any address, as long as rangein is low */
  1712. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
  1713. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
  1714. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  1715. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~(EICE_W_CTRL_RANGE | EICE_W_CTRL_nOPC) & 0xff);
  1716. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], current_pc);
  1717. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
  1718. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
  1719. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
  1720. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  1721. }
  1722. else
  1723. {
  1724. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
  1725. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
  1726. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
  1727. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff);
  1728. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], next_pc);
  1729. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
  1730. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
  1731. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
  1732. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
  1733. }
  1734. }
  1735. void arm7_9_disable_eice_step(target_t *target)
  1736. {
  1737. armv4_5_common_t *armv4_5 = target->arch_info;
  1738. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1739. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
  1740. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
  1741. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
  1742. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
  1743. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE]);
  1744. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK]);
  1745. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK]);
  1746. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK]);
  1747. embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE]);
  1748. }
  1749. int arm7_9_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
  1750. {
  1751. armv4_5_common_t *armv4_5 = target->arch_info;
  1752. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1753. breakpoint_t *breakpoint = NULL;
  1754. int err, retval;
  1755. if (target->state != TARGET_HALTED)
  1756. {
  1757. LOG_WARNING("target not halted");
  1758. return ERROR_TARGET_NOT_HALTED;
  1759. }
  1760. /* current = 1: continue on current pc, otherwise continue at <address> */
  1761. if (!current)
  1762. buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
  1763. uint32_t current_pc;
  1764. current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
  1765. /* the front-end may request us not to handle breakpoints */
  1766. if (handle_breakpoints)
  1767. if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
  1768. if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
  1769. {
  1770. return retval;
  1771. }
  1772. target->debug_reason = DBG_REASON_SINGLESTEP;
  1773. /* calculate PC of next instruction */
  1774. uint32_t next_pc;
  1775. if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
  1776. {
  1777. uint32_t current_opcode;
  1778. target_read_u32(target, current_pc, &current_opcode);
  1779. LOG_ERROR("BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
  1780. return retval;
  1781. }
  1782. if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
  1783. {
  1784. return retval;
  1785. }
  1786. arm7_9->enable_single_step(target, next_pc);
  1787. if (armv4_5->core_state == ARMV4_5_STATE_ARM)
  1788. {
  1789. arm7_9->branch_resume(target);
  1790. }
  1791. else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
  1792. {
  1793. arm7_9->branch_resume_thumb(target);
  1794. }
  1795. else
  1796. {
  1797. LOG_ERROR("unhandled core state");
  1798. return ERROR_FAIL;
  1799. }
  1800. if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
  1801. {
  1802. return retval;
  1803. }
  1804. err = arm7_9_execute_sys_speed(target);
  1805. arm7_9->disable_single_step(target);
  1806. /* registers are now invalid */
  1807. armv4_5_invalidate_core_regs(target);
  1808. if (err != ERROR_OK)
  1809. {
  1810. target->state = TARGET_UNKNOWN;
  1811. } else {
  1812. arm7_9_debug_entry(target);
  1813. if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
  1814. {
  1815. return retval;
  1816. }
  1817. LOG_DEBUG("target stepped");
  1818. }
  1819. if (breakpoint)
  1820. if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
  1821. {
  1822. return retval;
  1823. }
  1824. return err;
  1825. }
  1826. int arm7_9_read_core_reg(struct target_s *target, int num, enum armv4_5_mode mode)
  1827. {
  1828. uint32_t* reg_p[16];
  1829. uint32_t value;
  1830. int retval;
  1831. armv4_5_common_t *armv4_5 = target->arch_info;
  1832. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1833. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  1834. return ERROR_FAIL;
  1835. enum armv4_5_mode reg_mode = ((armv4_5_core_reg_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info)->mode;
  1836. if ((num < 0) || (num > 16))
  1837. return ERROR_INVALID_ARGUMENTS;
  1838. if ((mode != ARMV4_5_MODE_ANY)
  1839. && (mode != armv4_5->core_mode)
  1840. && (reg_mode != ARMV4_5_MODE_ANY))
  1841. {
  1842. uint32_t tmp_cpsr;
  1843. /* change processor mode (mask T bit) */
  1844. tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
  1845. tmp_cpsr |= mode;
  1846. tmp_cpsr &= ~0x20;
  1847. arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
  1848. }
  1849. if ((num >= 0) && (num <= 15))
  1850. {
  1851. /* read a normal core register */
  1852. reg_p[num] = &value;
  1853. arm7_9->read_core_regs(target, 1 << num, reg_p);
  1854. }
  1855. else
  1856. {
  1857. /* read a program status register
  1858. * if the register mode is MODE_ANY, we read the cpsr, otherwise a spsr
  1859. */
  1860. armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
  1861. int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
  1862. arm7_9->read_xpsr(target, &value, spsr);
  1863. }
  1864. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1865. {
  1866. return retval;
  1867. }
  1868. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
  1869. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
  1870. buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).value, 0, 32, value);
  1871. if ((mode != ARMV4_5_MODE_ANY)
  1872. && (mode != armv4_5->core_mode)
  1873. && (reg_mode != ARMV4_5_MODE_ANY)) {
  1874. /* restore processor mode (mask T bit) */
  1875. arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
  1876. }
  1877. return ERROR_OK;
  1878. }
  1879. int arm7_9_write_core_reg(struct target_s *target, int num, enum armv4_5_mode mode, uint32_t value)
  1880. {
  1881. uint32_t reg[16];
  1882. armv4_5_common_t *armv4_5 = target->arch_info;
  1883. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1884. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  1885. return ERROR_FAIL;
  1886. enum armv4_5_mode reg_mode = ((armv4_5_core_reg_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info)->mode;
  1887. if ((num < 0) || (num > 16))
  1888. return ERROR_INVALID_ARGUMENTS;
  1889. if ((mode != ARMV4_5_MODE_ANY)
  1890. && (mode != armv4_5->core_mode)
  1891. && (reg_mode != ARMV4_5_MODE_ANY)) {
  1892. uint32_t tmp_cpsr;
  1893. /* change processor mode (mask T bit) */
  1894. tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
  1895. tmp_cpsr |= mode;
  1896. tmp_cpsr &= ~0x20;
  1897. arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
  1898. }
  1899. if ((num >= 0) && (num <= 15))
  1900. {
  1901. /* write a normal core register */
  1902. reg[num] = value;
  1903. arm7_9->write_core_regs(target, 1 << num, reg);
  1904. }
  1905. else
  1906. {
  1907. /* write a program status register
  1908. * if the register mode is MODE_ANY, we write the cpsr, otherwise a spsr
  1909. */
  1910. armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
  1911. int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
  1912. /* if we're writing the CPSR, mask the T bit */
  1913. if (!spsr)
  1914. value &= ~0x20;
  1915. arm7_9->write_xpsr(target, value, spsr);
  1916. }
  1917. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
  1918. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
  1919. if ((mode != ARMV4_5_MODE_ANY)
  1920. && (mode != armv4_5->core_mode)
  1921. && (reg_mode != ARMV4_5_MODE_ANY)) {
  1922. /* restore processor mode (mask T bit) */
  1923. arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
  1924. }
  1925. return jtag_execute_queue();
  1926. }
  1927. int arm7_9_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  1928. {
  1929. armv4_5_common_t *armv4_5 = target->arch_info;
  1930. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  1931. uint32_t reg[16];
  1932. uint32_t num_accesses = 0;
  1933. int thisrun_accesses;
  1934. int i;
  1935. uint32_t cpsr;
  1936. int retval;
  1937. int last_reg = 0;
  1938. LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
  1939. if (target->state != TARGET_HALTED)
  1940. {
  1941. LOG_WARNING("target not halted");
  1942. return ERROR_TARGET_NOT_HALTED;
  1943. }
  1944. /* sanitize arguments */
  1945. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  1946. return ERROR_INVALID_ARGUMENTS;
  1947. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  1948. return ERROR_TARGET_UNALIGNED_ACCESS;
  1949. /* load the base register with the address of the first word */
  1950. reg[0] = address;
  1951. arm7_9->write_core_regs(target, 0x1, reg);
  1952. int j = 0;
  1953. switch (size)
  1954. {
  1955. case 4:
  1956. while (num_accesses < count)
  1957. {
  1958. uint32_t reg_list;
  1959. thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
  1960. reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
  1961. if (last_reg <= thisrun_accesses)
  1962. last_reg = thisrun_accesses;
  1963. arm7_9->load_word_regs(target, reg_list);
  1964. /* fast memory reads are only safe when the target is running
  1965. * from a sufficiently high clock (32 kHz is usually too slow)
  1966. */
  1967. if (arm7_9->fast_memory_access)
  1968. retval = arm7_9_execute_fast_sys_speed(target);
  1969. else
  1970. retval = arm7_9_execute_sys_speed(target);
  1971. if (retval != ERROR_OK)
  1972. return retval;
  1973. arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 4);
  1974. /* advance buffer, count number of accesses */
  1975. buffer += thisrun_accesses * 4;
  1976. num_accesses += thisrun_accesses;
  1977. if ((j++%1024) == 0)
  1978. {
  1979. keep_alive();
  1980. }
  1981. }
  1982. break;
  1983. case 2:
  1984. while (num_accesses < count)
  1985. {
  1986. uint32_t reg_list;
  1987. thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
  1988. reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
  1989. for (i = 1; i <= thisrun_accesses; i++)
  1990. {
  1991. if (i > last_reg)
  1992. last_reg = i;
  1993. arm7_9->load_hword_reg(target, i);
  1994. /* fast memory reads are only safe when the target is running
  1995. * from a sufficiently high clock (32 kHz is usually too slow)
  1996. */
  1997. if (arm7_9->fast_memory_access)
  1998. retval = arm7_9_execute_fast_sys_speed(target);
  1999. else
  2000. retval = arm7_9_execute_sys_speed(target);
  2001. if (retval != ERROR_OK)
  2002. {
  2003. return retval;
  2004. }
  2005. }
  2006. arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 2);
  2007. /* advance buffer, count number of accesses */
  2008. buffer += thisrun_accesses * 2;
  2009. num_accesses += thisrun_accesses;
  2010. if ((j++%1024) == 0)
  2011. {
  2012. keep_alive();
  2013. }
  2014. }
  2015. break;
  2016. case 1:
  2017. while (num_accesses < count)
  2018. {
  2019. uint32_t reg_list;
  2020. thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
  2021. reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
  2022. for (i = 1; i <= thisrun_accesses; i++)
  2023. {
  2024. if (i > last_reg)
  2025. last_reg = i;
  2026. arm7_9->load_byte_reg(target, i);
  2027. /* fast memory reads are only safe when the target is running
  2028. * from a sufficiently high clock (32 kHz is usually too slow)
  2029. */
  2030. if (arm7_9->fast_memory_access)
  2031. retval = arm7_9_execute_fast_sys_speed(target);
  2032. else
  2033. retval = arm7_9_execute_sys_speed(target);
  2034. if (retval != ERROR_OK)
  2035. {
  2036. return retval;
  2037. }
  2038. }
  2039. arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 1);
  2040. /* advance buffer, count number of accesses */
  2041. buffer += thisrun_accesses * 1;
  2042. num_accesses += thisrun_accesses;
  2043. if ((j++%1024) == 0)
  2044. {
  2045. keep_alive();
  2046. }
  2047. }
  2048. break;
  2049. default:
  2050. LOG_ERROR("BUG: we shouldn't get here");
  2051. exit(-1);
  2052. break;
  2053. }
  2054. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  2055. return ERROR_FAIL;
  2056. for (i = 0; i <= last_reg; i++)
  2057. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid;
  2058. arm7_9->read_xpsr(target, &cpsr, 0);
  2059. if ((retval = jtag_execute_queue()) != ERROR_OK)
  2060. {
  2061. LOG_ERROR("JTAG error while reading cpsr");
  2062. return ERROR_TARGET_DATA_ABORT;
  2063. }
  2064. if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
  2065. {
  2066. LOG_WARNING("memory read caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
  2067. arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
  2068. return ERROR_TARGET_DATA_ABORT;
  2069. }
  2070. return ERROR_OK;
  2071. }
  2072. int arm7_9_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  2073. {
  2074. armv4_5_common_t *armv4_5 = target->arch_info;
  2075. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  2076. reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
  2077. uint32_t reg[16];
  2078. uint32_t num_accesses = 0;
  2079. int thisrun_accesses;
  2080. int i;
  2081. uint32_t cpsr;
  2082. int retval;
  2083. int last_reg = 0;
  2084. #ifdef _DEBUG_ARM7_9_
  2085. LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address, size, count);
  2086. #endif
  2087. if (target->state != TARGET_HALTED)
  2088. {
  2089. LOG_WARNING("target not halted");
  2090. return ERROR_TARGET_NOT_HALTED;
  2091. }
  2092. /* sanitize arguments */
  2093. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  2094. return ERROR_INVALID_ARGUMENTS;
  2095. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  2096. return ERROR_TARGET_UNALIGNED_ACCESS;
  2097. /* load the base register with the address of the first word */
  2098. reg[0] = address;
  2099. arm7_9->write_core_regs(target, 0x1, reg);
  2100. /* Clear DBGACK, to make sure memory fetches work as expected */
  2101. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
  2102. embeddedice_store_reg(dbg_ctrl);
  2103. switch (size)
  2104. {
  2105. case 4:
  2106. while (num_accesses < count)
  2107. {
  2108. uint32_t reg_list;
  2109. thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
  2110. reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
  2111. for (i = 1; i <= thisrun_accesses; i++)
  2112. {
  2113. if (i > last_reg)
  2114. last_reg = i;
  2115. reg[i] = target_buffer_get_u32(target, buffer);
  2116. buffer += 4;
  2117. }
  2118. arm7_9->write_core_regs(target, reg_list, reg);
  2119. arm7_9->store_word_regs(target, reg_list);
  2120. /* fast memory writes are only safe when the target is running
  2121. * from a sufficiently high clock (32 kHz is usually too slow)
  2122. */
  2123. if (arm7_9->fast_memory_access)
  2124. retval = arm7_9_execute_fast_sys_speed(target);
  2125. else
  2126. retval = arm7_9_execute_sys_speed(target);
  2127. if (retval != ERROR_OK)
  2128. {
  2129. return retval;
  2130. }
  2131. num_accesses += thisrun_accesses;
  2132. }
  2133. break;
  2134. case 2:
  2135. while (num_accesses < count)
  2136. {
  2137. uint32_t reg_list;
  2138. thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
  2139. reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
  2140. for (i = 1; i <= thisrun_accesses; i++)
  2141. {
  2142. if (i > last_reg)
  2143. last_reg = i;
  2144. reg[i] = target_buffer_get_u16(target, buffer) & 0xffff;
  2145. buffer += 2;
  2146. }
  2147. arm7_9->write_core_regs(target, reg_list, reg);
  2148. for (i = 1; i <= thisrun_accesses; i++)
  2149. {
  2150. arm7_9->store_hword_reg(target, i);
  2151. /* fast memory writes are only safe when the target is running
  2152. * from a sufficiently high clock (32 kHz is usually too slow)
  2153. */
  2154. if (arm7_9->fast_memory_access)
  2155. retval = arm7_9_execute_fast_sys_speed(target);
  2156. else
  2157. retval = arm7_9_execute_sys_speed(target);
  2158. if (retval != ERROR_OK)
  2159. {
  2160. return retval;
  2161. }
  2162. }
  2163. num_accesses += thisrun_accesses;
  2164. }
  2165. break;
  2166. case 1:
  2167. while (num_accesses < count)
  2168. {
  2169. uint32_t reg_list;
  2170. thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
  2171. reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
  2172. for (i = 1; i <= thisrun_accesses; i++)
  2173. {
  2174. if (i > last_reg)
  2175. last_reg = i;
  2176. reg[i] = *buffer++ & 0xff;
  2177. }
  2178. arm7_9->write_core_regs(target, reg_list, reg);
  2179. for (i = 1; i <= thisrun_accesses; i++)
  2180. {
  2181. arm7_9->store_byte_reg(target, i);
  2182. /* fast memory writes are only safe when the target is running
  2183. * from a sufficiently high clock (32 kHz is usually too slow)
  2184. */
  2185. if (arm7_9->fast_memory_access)
  2186. retval = arm7_9_execute_fast_sys_speed(target);
  2187. else
  2188. retval = arm7_9_execute_sys_speed(target);
  2189. if (retval != ERROR_OK)
  2190. {
  2191. return retval;
  2192. }
  2193. }
  2194. num_accesses += thisrun_accesses;
  2195. }
  2196. break;
  2197. default:
  2198. LOG_ERROR("BUG: we shouldn't get here");
  2199. exit(-1);
  2200. break;
  2201. }
  2202. /* Re-Set DBGACK */
  2203. buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
  2204. embeddedice_store_reg(dbg_ctrl);
  2205. if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
  2206. return ERROR_FAIL;
  2207. for (i = 0; i <= last_reg; i++)
  2208. ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid;
  2209. arm7_9->read_xpsr(target, &cpsr, 0);
  2210. if ((retval = jtag_execute_queue()) != ERROR_OK)
  2211. {
  2212. LOG_ERROR("JTAG error while reading cpsr");
  2213. return ERROR_TARGET_DATA_ABORT;
  2214. }
  2215. if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
  2216. {
  2217. LOG_WARNING("memory write caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
  2218. arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
  2219. return ERROR_TARGET_DATA_ABORT;
  2220. }
  2221. return ERROR_OK;
  2222. }
  2223. static int dcc_count;
  2224. static uint8_t *dcc_buffer;
  2225. static int arm7_9_dcc_completion(struct target_s *target, uint32_t exit_point, int timeout_ms, void *arch_info)
  2226. {
  2227. int retval = ERROR_OK;
  2228. armv4_5_common_t *armv4_5 = target->arch_info;
  2229. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  2230. if ((retval = target_wait_state(target, TARGET_DEBUG_RUNNING, 500)) != ERROR_OK)
  2231. return retval;
  2232. int little = target->endianness == TARGET_LITTLE_ENDIAN;
  2233. int count = dcc_count;
  2234. uint8_t *buffer = dcc_buffer;
  2235. if (count > 2)
  2236. {
  2237. /* Handle first & last using standard embeddedice_write_reg and the middle ones w/the
  2238. * core function repeated. */
  2239. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
  2240. buffer += 4;
  2241. embeddedice_reg_t *ice_reg = arm7_9->eice_cache->reg_list[EICE_COMMS_DATA].arch_info;
  2242. uint8_t reg_addr = ice_reg->addr & 0x1f;
  2243. jtag_tap_t *tap;
  2244. tap = ice_reg->jtag_info->tap;
  2245. embeddedice_write_dcc(tap, reg_addr, buffer, little, count-2);
  2246. buffer += (count-2)*4;
  2247. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
  2248. } else
  2249. {
  2250. int i;
  2251. for (i = 0; i < count; i++)
  2252. {
  2253. embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
  2254. buffer += 4;
  2255. }
  2256. }
  2257. if ((retval = target_halt(target))!= ERROR_OK)
  2258. {
  2259. return retval;
  2260. }
  2261. return target_wait_state(target, TARGET_HALTED, 500);
  2262. }
  2263. static const uint32_t dcc_code[] =
  2264. {
  2265. /* MRC TST BNE MRC STR B */
  2266. 0xee101e10, 0xe3110001, 0x0afffffc, 0xee111e10, 0xe4801004, 0xeafffff9
  2267. };
  2268. int armv4_5_run_algorithm_inner(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_params, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info, int (*run_it)(struct target_s *target, uint32_t exit_point, int timeout_ms, void *arch_info));
  2269. int arm7_9_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
  2270. {
  2271. int retval;
  2272. armv4_5_common_t *armv4_5 = target->arch_info;
  2273. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  2274. int i;
  2275. if (!arm7_9->dcc_downloads)
  2276. return target_write_memory(target, address, 4, count, buffer);
  2277. /* regrab previously allocated working_area, or allocate a new one */
  2278. if (!arm7_9->dcc_working_area)
  2279. {
  2280. uint8_t dcc_code_buf[6 * 4];
  2281. /* make sure we have a working area */
  2282. if (target_alloc_working_area(target, 24, &arm7_9->dcc_working_area) != ERROR_OK)
  2283. {
  2284. LOG_INFO("no working area available, falling back to memory writes");
  2285. return target_write_memory(target, address, 4, count, buffer);
  2286. }
  2287. /* copy target instructions to target endianness */
  2288. for (i = 0; i < 6; i++)
  2289. {
  2290. target_buffer_set_u32(target, dcc_code_buf + i*4, dcc_code[i]);
  2291. }
  2292. /* write DCC code to working area */
  2293. if ((retval = target_write_memory(target, arm7_9->dcc_working_area->address, 4, 6, dcc_code_buf)) != ERROR_OK)
  2294. {
  2295. return retval;
  2296. }
  2297. }
  2298. armv4_5_algorithm_t armv4_5_info;
  2299. reg_param_t reg_params[1];
  2300. armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
  2301. armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
  2302. armv4_5_info.core_state = ARMV4_5_STATE_ARM;
  2303. init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
  2304. buf_set_u32(reg_params[0].value, 0, 32, address);
  2305. dcc_count = count;
  2306. dcc_buffer = buffer;
  2307. retval = armv4_5_run_algorithm_inner(target, 0, NULL, 1, reg_params,
  2308. arm7_9->dcc_working_area->address, arm7_9->dcc_working_area->address + 6*4, 20*1000, &armv4_5_info, arm7_9_dcc_completion);
  2309. if (retval == ERROR_OK)
  2310. {
  2311. uint32_t endaddress = buf_get_u32(reg_params[0].value, 0, 32);
  2312. if (endaddress != (address + count*4))
  2313. {
  2314. LOG_ERROR("DCC write failed, expected end address 0x%08" PRIx32 " got 0x%0" PRIx32 "", (address + count*4), endaddress);
  2315. retval = ERROR_FAIL;
  2316. }
  2317. }
  2318. destroy_reg_param(&reg_params[0]);
  2319. return retval;
  2320. }
  2321. int arm7_9_checksum_memory(struct target_s *target, uint32_t address, uint32_t count, uint32_t* checksum)
  2322. {
  2323. working_area_t *crc_algorithm;
  2324. armv4_5_algorithm_t armv4_5_info;
  2325. reg_param_t reg_params[2];
  2326. int retval;
  2327. uint32_t arm7_9_crc_code[] = {
  2328. 0xE1A02000, /* mov r2, r0 */
  2329. 0xE3E00000, /* mov r0, #0xffffffff */
  2330. 0xE1A03001, /* mov r3, r1 */
  2331. 0xE3A04000, /* mov r4, #0 */
  2332. 0xEA00000B, /* b ncomp */
  2333. /* nbyte: */
  2334. 0xE7D21004, /* ldrb r1, [r2, r4] */
  2335. 0xE59F7030, /* ldr r7, CRC32XOR */
  2336. 0xE0200C01, /* eor r0, r0, r1, asl 24 */
  2337. 0xE3A05000, /* mov r5, #0 */
  2338. /* loop: */
  2339. 0xE3500000, /* cmp r0, #0 */
  2340. 0xE1A06080, /* mov r6, r0, asl #1 */
  2341. 0xE2855001, /* add r5, r5, #1 */
  2342. 0xE1A00006, /* mov r0, r6 */
  2343. 0xB0260007, /* eorlt r0, r6, r7 */
  2344. 0xE3550008, /* cmp r5, #8 */
  2345. 0x1AFFFFF8, /* bne loop */
  2346. 0xE2844001, /* add r4, r4, #1 */
  2347. /* ncomp: */
  2348. 0xE1540003, /* cmp r4, r3 */
  2349. 0x1AFFFFF1, /* bne nbyte */
  2350. /* end: */
  2351. 0xEAFFFFFE, /* b end */
  2352. 0x04C11DB7 /* CRC32XOR: .word 0x04C11DB7 */
  2353. };
  2354. uint32_t i;
  2355. if (target_alloc_working_area(target, sizeof(arm7_9_crc_code), &crc_algorithm) != ERROR_OK)
  2356. {
  2357. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  2358. }
  2359. /* convert flash writing code into a buffer in target endianness */
  2360. for (i = 0; i < (sizeof(arm7_9_crc_code)/sizeof(uint32_t)); i++)
  2361. {
  2362. if ((retval = target_write_u32(target, crc_algorithm->address + i*sizeof(uint32_t), arm7_9_crc_code[i])) != ERROR_OK)
  2363. {
  2364. return retval;
  2365. }
  2366. }
  2367. armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
  2368. armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
  2369. armv4_5_info.core_state = ARMV4_5_STATE_ARM;
  2370. init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
  2371. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  2372. buf_set_u32(reg_params[0].value, 0, 32, address);
  2373. buf_set_u32(reg_params[1].value, 0, 32, count);
  2374. if ((retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
  2375. crc_algorithm->address, crc_algorithm->address + (sizeof(arm7_9_crc_code) - 8), 20000, &armv4_5_info)) != ERROR_OK)
  2376. {
  2377. LOG_ERROR("error executing arm7_9 crc algorithm");
  2378. destroy_reg_param(&reg_params[0]);
  2379. destroy_reg_param(&reg_params[1]);
  2380. target_free_working_area(target, crc_algorithm);
  2381. return retval;
  2382. }
  2383. *checksum = buf_get_u32(reg_params[0].value, 0, 32);
  2384. destroy_reg_param(&reg_params[0]);
  2385. destroy_reg_param(&reg_params[1]);
  2386. target_free_working_area(target, crc_algorithm);
  2387. return ERROR_OK;
  2388. }
  2389. int arm7_9_blank_check_memory(struct target_s *target, uint32_t address, uint32_t count, uint32_t* blank)
  2390. {
  2391. working_area_t *erase_check_algorithm;
  2392. reg_param_t reg_params[3];
  2393. armv4_5_algorithm_t armv4_5_info;
  2394. int retval;
  2395. uint32_t i;
  2396. uint32_t erase_check_code[] =
  2397. {
  2398. /* loop: */
  2399. 0xe4d03001, /* ldrb r3, [r0], #1 */
  2400. 0xe0022003, /* and r2, r2, r3 */
  2401. 0xe2511001, /* subs r1, r1, #1 */
  2402. 0x1afffffb, /* bne loop */
  2403. /* end: */
  2404. 0xeafffffe /* b end */
  2405. };
  2406. /* make sure we have a working area */
  2407. if (target_alloc_working_area(target, sizeof(erase_check_code), &erase_check_algorithm) != ERROR_OK)
  2408. {
  2409. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  2410. }
  2411. /* convert flash writing code into a buffer in target endianness */
  2412. for (i = 0; i < (sizeof(erase_check_code)/sizeof(uint32_t)); i++)
  2413. if ((retval = target_write_u32(target, erase_check_algorithm->address + i*sizeof(uint32_t), erase_check_code[i])) != ERROR_OK)
  2414. {
  2415. return retval;
  2416. }
  2417. armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
  2418. armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
  2419. armv4_5_info.core_state = ARMV4_5_STATE_ARM;
  2420. init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
  2421. buf_set_u32(reg_params[0].value, 0, 32, address);
  2422. init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
  2423. buf_set_u32(reg_params[1].value, 0, 32, count);
  2424. init_reg_param(&reg_params[2], "r2", 32, PARAM_IN_OUT);
  2425. buf_set_u32(reg_params[2].value, 0, 32, 0xff);
  2426. if ((retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
  2427. erase_check_algorithm->address, erase_check_algorithm->address + (sizeof(erase_check_code) - 4), 10000, &armv4_5_info)) != ERROR_OK)
  2428. {
  2429. destroy_reg_param(&reg_params[0]);
  2430. destroy_reg_param(&reg_params[1]);
  2431. destroy_reg_param(&reg_params[2]);
  2432. target_free_working_area(target, erase_check_algorithm);
  2433. return 0;
  2434. }
  2435. *blank = buf_get_u32(reg_params[2].value, 0, 32);
  2436. destroy_reg_param(&reg_params[0]);
  2437. destroy_reg_param(&reg_params[1]);
  2438. destroy_reg_param(&reg_params[2]);
  2439. target_free_working_area(target, erase_check_algorithm);
  2440. return ERROR_OK;
  2441. }
  2442. int arm7_9_register_commands(struct command_context_s *cmd_ctx)
  2443. {
  2444. command_t *arm7_9_cmd;
  2445. arm7_9_cmd = register_command(cmd_ctx, NULL, "arm7_9", NULL, COMMAND_ANY, "arm7/9 specific commands");
  2446. register_command(cmd_ctx, arm7_9_cmd, "write_xpsr", handle_arm7_9_write_xpsr_command, COMMAND_EXEC, "write program status register <value> <not cpsr | spsr>");
  2447. register_command(cmd_ctx, arm7_9_cmd, "write_xpsr_im8", handle_arm7_9_write_xpsr_im8_command, COMMAND_EXEC, "write program status register <8bit immediate> <rotate> <not cpsr | spsr>");
  2448. register_command(cmd_ctx, arm7_9_cmd, "write_core_reg", handle_arm7_9_write_core_reg_command, COMMAND_EXEC, "write core register <num> <mode> <value>");
  2449. register_command(cmd_ctx, arm7_9_cmd, "dbgrq", handle_arm7_9_dbgrq_command,
  2450. COMMAND_ANY, "use EmbeddedICE dbgrq instead of breakpoint for target halt requests <enable | disable>");
  2451. register_command(cmd_ctx, arm7_9_cmd, "fast_memory_access", handle_arm7_9_fast_memory_access_command,
  2452. COMMAND_ANY, "use fast memory accesses instead of slower but potentially safer accesses <enable | disable>");
  2453. register_command(cmd_ctx, arm7_9_cmd, "dcc_downloads", handle_arm7_9_dcc_downloads_command,
  2454. COMMAND_ANY, "use DCC downloads for larger memory writes <enable | disable>");
  2455. armv4_5_register_commands(cmd_ctx);
  2456. etm_register_commands(cmd_ctx);
  2457. return ERROR_OK;
  2458. }
  2459. int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2460. {
  2461. uint32_t value;
  2462. int spsr;
  2463. int retval;
  2464. target_t *target = get_current_target(cmd_ctx);
  2465. armv4_5_common_t *armv4_5;
  2466. arm7_9_common_t *arm7_9;
  2467. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  2468. {
  2469. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  2470. return ERROR_OK;
  2471. }
  2472. if (target->state != TARGET_HALTED)
  2473. {
  2474. command_print(cmd_ctx, "can't write registers while running");
  2475. return ERROR_OK;
  2476. }
  2477. if (argc < 2)
  2478. {
  2479. command_print(cmd_ctx, "usage: write_xpsr <value> <not cpsr | spsr>");
  2480. return ERROR_OK;
  2481. }
  2482. value = strtoul(args[0], NULL, 0);
  2483. spsr = strtol(args[1], NULL, 0);
  2484. /* if we're writing the CPSR, mask the T bit */
  2485. if (!spsr)
  2486. value &= ~0x20;
  2487. arm7_9->write_xpsr(target, value, spsr);
  2488. if ((retval = jtag_execute_queue()) != ERROR_OK)
  2489. {
  2490. LOG_ERROR("JTAG error while writing to xpsr");
  2491. return retval;
  2492. }
  2493. return ERROR_OK;
  2494. }
  2495. int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2496. {
  2497. uint32_t value;
  2498. int rotate;
  2499. int spsr;
  2500. int retval;
  2501. target_t *target = get_current_target(cmd_ctx);
  2502. armv4_5_common_t *armv4_5;
  2503. arm7_9_common_t *arm7_9;
  2504. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  2505. {
  2506. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  2507. return ERROR_OK;
  2508. }
  2509. if (target->state != TARGET_HALTED)
  2510. {
  2511. command_print(cmd_ctx, "can't write registers while running");
  2512. return ERROR_OK;
  2513. }
  2514. if (argc < 3)
  2515. {
  2516. command_print(cmd_ctx, "usage: write_xpsr_im8 <im8> <rotate> <not cpsr | spsr>");
  2517. return ERROR_OK;
  2518. }
  2519. value = strtoul(args[0], NULL, 0);
  2520. rotate = strtol(args[1], NULL, 0);
  2521. spsr = strtol(args[2], NULL, 0);
  2522. arm7_9->write_xpsr_im8(target, value, rotate, spsr);
  2523. if ((retval = jtag_execute_queue()) != ERROR_OK)
  2524. {
  2525. LOG_ERROR("JTAG error while writing 8-bit immediate to xpsr");
  2526. return retval;
  2527. }
  2528. return ERROR_OK;
  2529. }
  2530. int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2531. {
  2532. uint32_t value;
  2533. uint32_t mode;
  2534. int num;
  2535. target_t *target = get_current_target(cmd_ctx);
  2536. armv4_5_common_t *armv4_5;
  2537. arm7_9_common_t *arm7_9;
  2538. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  2539. {
  2540. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  2541. return ERROR_OK;
  2542. }
  2543. if (target->state != TARGET_HALTED)
  2544. {
  2545. command_print(cmd_ctx, "can't write registers while running");
  2546. return ERROR_OK;
  2547. }
  2548. if (argc < 3)
  2549. {
  2550. command_print(cmd_ctx, "usage: write_core_reg <num> <mode> <value>");
  2551. return ERROR_OK;
  2552. }
  2553. num = strtol(args[0], NULL, 0);
  2554. mode = strtoul(args[1], NULL, 0);
  2555. value = strtoul(args[2], NULL, 0);
  2556. return arm7_9_write_core_reg(target, num, mode, value);
  2557. }
  2558. int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2559. {
  2560. target_t *target = get_current_target(cmd_ctx);
  2561. armv4_5_common_t *armv4_5;
  2562. arm7_9_common_t *arm7_9;
  2563. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  2564. {
  2565. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  2566. return ERROR_OK;
  2567. }
  2568. if (argc > 0)
  2569. {
  2570. if (strcmp("enable", args[0]) == 0)
  2571. {
  2572. arm7_9->use_dbgrq = 1;
  2573. }
  2574. else if (strcmp("disable", args[0]) == 0)
  2575. {
  2576. arm7_9->use_dbgrq = 0;
  2577. }
  2578. else
  2579. {
  2580. command_print(cmd_ctx, "usage: arm7_9 dbgrq <enable | disable>");
  2581. }
  2582. }
  2583. command_print(cmd_ctx, "use of EmbeddedICE dbgrq instead of breakpoint for target halt %s", (arm7_9->use_dbgrq) ? "enabled" : "disabled");
  2584. return ERROR_OK;
  2585. }
  2586. int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2587. {
  2588. target_t *target = get_current_target(cmd_ctx);
  2589. armv4_5_common_t *armv4_5;
  2590. arm7_9_common_t *arm7_9;
  2591. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  2592. {
  2593. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  2594. return ERROR_OK;
  2595. }
  2596. if (argc > 0)
  2597. {
  2598. if (strcmp("enable", args[0]) == 0)
  2599. {
  2600. arm7_9->fast_memory_access = 1;
  2601. }
  2602. else if (strcmp("disable", args[0]) == 0)
  2603. {
  2604. arm7_9->fast_memory_access = 0;
  2605. }
  2606. else
  2607. {
  2608. command_print(cmd_ctx, "usage: arm7_9 fast_memory_access <enable | disable>");
  2609. }
  2610. }
  2611. command_print(cmd_ctx, "fast memory access is %s", (arm7_9->fast_memory_access) ? "enabled" : "disabled");
  2612. return ERROR_OK;
  2613. }
  2614. int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  2615. {
  2616. target_t *target = get_current_target(cmd_ctx);
  2617. armv4_5_common_t *armv4_5;
  2618. arm7_9_common_t *arm7_9;
  2619. if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
  2620. {
  2621. command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
  2622. return ERROR_OK;
  2623. }
  2624. if (argc > 0)
  2625. {
  2626. if (strcmp("enable", args[0]) == 0)
  2627. {
  2628. arm7_9->dcc_downloads = 1;
  2629. }
  2630. else if (strcmp("disable", args[0]) == 0)
  2631. {
  2632. arm7_9->dcc_downloads = 0;
  2633. }
  2634. else
  2635. {
  2636. command_print(cmd_ctx, "usage: arm7_9 dcc_downloads <enable | disable>");
  2637. }
  2638. }
  2639. command_print(cmd_ctx, "dcc downloads are %s", (arm7_9->dcc_downloads) ? "enabled" : "disabled");
  2640. return ERROR_OK;
  2641. }
  2642. int arm7_9_init_arch_info(target_t *target, arm7_9_common_t *arm7_9)
  2643. {
  2644. int retval = ERROR_OK;
  2645. armv4_5_common_t *armv4_5 = &arm7_9->armv4_5_common;
  2646. arm7_9->common_magic = ARM7_9_COMMON_MAGIC;
  2647. if ((retval = arm_jtag_setup_connection(&arm7_9->jtag_info)) != ERROR_OK)
  2648. {
  2649. return retval;
  2650. }
  2651. arm7_9->wp_available = 0; /* this is set up in arm7_9_clear_watchpoints() */
  2652. arm7_9->wp_available_max = 2;
  2653. arm7_9->sw_breakpoints_added = 0;
  2654. arm7_9->breakpoint_count = 0;
  2655. arm7_9->wp0_used = 0;
  2656. arm7_9->wp1_used = 0;
  2657. arm7_9->wp1_used_default = 0;
  2658. arm7_9->use_dbgrq = 0;
  2659. arm7_9->etm_ctx = NULL;
  2660. arm7_9->has_single_step = 0;
  2661. arm7_9->has_monitor_mode = 0;
  2662. arm7_9->has_vector_catch = 0;
  2663. arm7_9->debug_entry_from_reset = 0;
  2664. arm7_9->dcc_working_area = NULL;
  2665. arm7_9->fast_memory_access = fast_and_dangerous;
  2666. arm7_9->dcc_downloads = fast_and_dangerous;
  2667. arm7_9->need_bypass_before_restart = 0;
  2668. armv4_5->arch_info = arm7_9;
  2669. armv4_5->read_core_reg = arm7_9_read_core_reg;
  2670. armv4_5->write_core_reg = arm7_9_write_core_reg;
  2671. armv4_5->full_context = arm7_9_full_context;
  2672. if ((retval = armv4_5_init_arch_info(target, armv4_5)) != ERROR_OK)
  2673. {
  2674. return retval;
  2675. }
  2676. if ((retval = target_register_timer_callback(arm7_9_handle_target_request, 1, 1, target)) != ERROR_OK)
  2677. {
  2678. return retval;
  2679. }
  2680. return ERROR_OK;
  2681. }