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.
 
 
 
 
 
 

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