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.
 
 
 
 
 
 

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