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.
 
 
 
 
 
 

2932 lines
89 KiB

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