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.
 
 
 
 
 
 

833 lines
24 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008,2009 by Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  20. ***************************************************************************/
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "arm926ejs.h"
  25. #include <helper/time_support.h>
  26. #include "target_type.h"
  27. #include "register.h"
  28. #include "arm_opcodes.h"
  29. /*
  30. * The ARM926 is built around the ARM9EJ-S core, and most JTAG docs
  31. * are in the ARM9EJ-S Technical Reference Manual (ARM DDI 0222B) not
  32. * the ARM926 manual (ARM DDI 0198E). The scan chains are:
  33. *
  34. * 1 ... core debugging
  35. * 2 ... EmbeddedICE
  36. * 3 ... external boundary scan (SoC-specific, unused here)
  37. * 6 ... ETM
  38. * 15 ... coprocessor 15
  39. */
  40. #if 0
  41. #define _DEBUG_INSTRUCTION_EXECUTION_
  42. #endif
  43. #define ARM926EJS_CP15_ADDR(opcode_1, opcode_2, CRn, CRm) ((opcode_1 << 11) | (opcode_2 << 8) | (CRn << 4) | (CRm << 0))
  44. static int arm926ejs_cp15_read(struct target *target, uint32_t op1, uint32_t op2,
  45. uint32_t CRn, uint32_t CRm, uint32_t *value)
  46. {
  47. int retval = ERROR_OK;
  48. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  49. struct arm_jtag *jtag_info = &arm7_9->jtag_info;
  50. uint32_t address = ARM926EJS_CP15_ADDR(op1, op2, CRn, CRm);
  51. struct scan_field fields[4];
  52. uint8_t address_buf[2] = {0, 0};
  53. uint8_t nr_w_buf = 0;
  54. uint8_t access_t = 1;
  55. buf_set_u32(address_buf, 0, 14, address);
  56. retval = arm_jtag_scann(jtag_info, 0xf, TAP_IDLE);
  57. if (retval != ERROR_OK)
  58. return retval;
  59. retval = arm_jtag_set_instr(jtag_info->tap, jtag_info->intest_instr, NULL, TAP_IDLE);
  60. if (retval != ERROR_OK)
  61. return retval;
  62. fields[0].num_bits = 32;
  63. fields[0].out_value = NULL;
  64. fields[0].in_value = (uint8_t *)value;
  65. fields[1].num_bits = 1;
  66. fields[1].out_value = &access_t;
  67. fields[1].in_value = &access_t;
  68. fields[2].num_bits = 14;
  69. fields[2].out_value = address_buf;
  70. fields[2].in_value = NULL;
  71. fields[3].num_bits = 1;
  72. fields[3].out_value = &nr_w_buf;
  73. fields[3].in_value = NULL;
  74. jtag_add_dr_scan(jtag_info->tap, 4, fields, TAP_IDLE);
  75. int64_t then = timeval_ms();
  76. for (;;) {
  77. /* rescan with NOP, to wait for the access to complete */
  78. access_t = 0;
  79. nr_w_buf = 0;
  80. jtag_add_dr_scan(jtag_info->tap, 4, fields, TAP_IDLE);
  81. jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)value);
  82. retval = jtag_execute_queue();
  83. if (retval != ERROR_OK)
  84. return retval;
  85. if (buf_get_u32(&access_t, 0, 1) == 1)
  86. break;
  87. /* 10ms timeout */
  88. if ((timeval_ms()-then) > 10) {
  89. LOG_ERROR("cp15 read operation timed out");
  90. return ERROR_FAIL;
  91. }
  92. }
  93. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  94. LOG_DEBUG("addr: 0x%x value: %8.8x", address, *value);
  95. #endif
  96. retval = arm_jtag_set_instr(jtag_info->tap, 0xc, NULL, TAP_IDLE);
  97. if (retval != ERROR_OK)
  98. return retval;
  99. return ERROR_OK;
  100. }
  101. static int arm926ejs_mrc(struct target *target, int cpnum, uint32_t op1,
  102. uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
  103. {
  104. if (cpnum != 15) {
  105. LOG_ERROR("Only cp15 is supported");
  106. return ERROR_FAIL;
  107. }
  108. return arm926ejs_cp15_read(target, op1, op2, CRn, CRm, value);
  109. }
  110. static int arm926ejs_cp15_write(struct target *target, uint32_t op1, uint32_t op2,
  111. uint32_t CRn, uint32_t CRm, uint32_t value)
  112. {
  113. int retval = ERROR_OK;
  114. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  115. struct arm_jtag *jtag_info = &arm7_9->jtag_info;
  116. uint32_t address = ARM926EJS_CP15_ADDR(op1, op2, CRn, CRm);
  117. struct scan_field fields[4];
  118. uint8_t value_buf[4];
  119. uint8_t address_buf[2] = {0, 0};
  120. uint8_t nr_w_buf = 1;
  121. uint8_t access_t = 1;
  122. buf_set_u32(address_buf, 0, 14, address);
  123. buf_set_u32(value_buf, 0, 32, value);
  124. retval = arm_jtag_scann(jtag_info, 0xf, TAP_IDLE);
  125. if (retval != ERROR_OK)
  126. return retval;
  127. retval = arm_jtag_set_instr(jtag_info->tap, jtag_info->intest_instr, NULL, TAP_IDLE);
  128. if (retval != ERROR_OK)
  129. return retval;
  130. fields[0].num_bits = 32;
  131. fields[0].out_value = value_buf;
  132. fields[0].in_value = NULL;
  133. fields[1].num_bits = 1;
  134. fields[1].out_value = &access_t;
  135. fields[1].in_value = &access_t;
  136. fields[2].num_bits = 14;
  137. fields[2].out_value = address_buf;
  138. fields[2].in_value = NULL;
  139. fields[3].num_bits = 1;
  140. fields[3].out_value = &nr_w_buf;
  141. fields[3].in_value = NULL;
  142. jtag_add_dr_scan(jtag_info->tap, 4, fields, TAP_IDLE);
  143. int64_t then = timeval_ms();
  144. for (;;) {
  145. /* rescan with NOP, to wait for the access to complete */
  146. access_t = 0;
  147. nr_w_buf = 0;
  148. jtag_add_dr_scan(jtag_info->tap, 4, fields, TAP_IDLE);
  149. retval = jtag_execute_queue();
  150. if (retval != ERROR_OK)
  151. return retval;
  152. if (buf_get_u32(&access_t, 0, 1) == 1)
  153. break;
  154. /* 10ms timeout */
  155. if ((timeval_ms()-then) > 10) {
  156. LOG_ERROR("cp15 write operation timed out");
  157. return ERROR_FAIL;
  158. }
  159. }
  160. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  161. LOG_DEBUG("addr: 0x%x value: %8.8x", address, value);
  162. #endif
  163. retval = arm_jtag_set_instr(jtag_info->tap, 0xf, NULL, TAP_IDLE);
  164. if (retval != ERROR_OK)
  165. return retval;
  166. return ERROR_OK;
  167. }
  168. static int arm926ejs_mcr(struct target *target, int cpnum, uint32_t op1,
  169. uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
  170. {
  171. if (cpnum != 15) {
  172. LOG_ERROR("Only cp15 is supported");
  173. return ERROR_FAIL;
  174. }
  175. return arm926ejs_cp15_write(target, op1, op2, CRn, CRm, value);
  176. }
  177. static int arm926ejs_examine_debug_reason(struct target *target)
  178. {
  179. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  180. struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  181. int debug_reason;
  182. int retval;
  183. embeddedice_read_reg(dbg_stat);
  184. retval = jtag_execute_queue();
  185. if (retval != ERROR_OK)
  186. return retval;
  187. /* Method-Of-Entry (MOE) field */
  188. debug_reason = buf_get_u32(dbg_stat->value, 6, 4);
  189. switch (debug_reason) {
  190. case 0:
  191. LOG_DEBUG("no *NEW* debug entry (?missed one?)");
  192. /* ... since last restart or debug reset ... */
  193. target->debug_reason = DBG_REASON_DBGRQ;
  194. break;
  195. case 1:
  196. LOG_DEBUG("breakpoint from EICE unit 0");
  197. target->debug_reason = DBG_REASON_BREAKPOINT;
  198. break;
  199. case 2:
  200. LOG_DEBUG("breakpoint from EICE unit 1");
  201. target->debug_reason = DBG_REASON_BREAKPOINT;
  202. break;
  203. case 3:
  204. LOG_DEBUG("soft breakpoint (BKPT instruction)");
  205. target->debug_reason = DBG_REASON_BREAKPOINT;
  206. break;
  207. case 4:
  208. LOG_DEBUG("vector catch breakpoint");
  209. target->debug_reason = DBG_REASON_BREAKPOINT;
  210. break;
  211. case 5:
  212. LOG_DEBUG("external breakpoint");
  213. target->debug_reason = DBG_REASON_BREAKPOINT;
  214. break;
  215. case 6:
  216. LOG_DEBUG("watchpoint from EICE unit 0");
  217. target->debug_reason = DBG_REASON_WATCHPOINT;
  218. break;
  219. case 7:
  220. LOG_DEBUG("watchpoint from EICE unit 1");
  221. target->debug_reason = DBG_REASON_WATCHPOINT;
  222. break;
  223. case 8:
  224. LOG_DEBUG("external watchpoint");
  225. target->debug_reason = DBG_REASON_WATCHPOINT;
  226. break;
  227. case 9:
  228. LOG_DEBUG("internal debug request");
  229. target->debug_reason = DBG_REASON_DBGRQ;
  230. break;
  231. case 10:
  232. LOG_DEBUG("external debug request");
  233. target->debug_reason = DBG_REASON_DBGRQ;
  234. break;
  235. case 11:
  236. LOG_DEBUG("debug re-entry from system speed access");
  237. /* This is normal when connecting to something that's
  238. * already halted, or in some related code paths, but
  239. * otherwise is surprising (and presumably wrong).
  240. */
  241. switch (target->debug_reason) {
  242. case DBG_REASON_DBGRQ:
  243. break;
  244. default:
  245. LOG_ERROR("unexpected -- debug re-entry");
  246. /* FALLTHROUGH */
  247. case DBG_REASON_UNDEFINED:
  248. target->debug_reason = DBG_REASON_DBGRQ;
  249. break;
  250. }
  251. break;
  252. case 12:
  253. /* FIX!!!! here be dragons!!! We need to fail here so
  254. * the target will interpreted as halted but we won't
  255. * try to talk to it right now... a resume + halt seems
  256. * to sync things up again. Please send an email to
  257. * openocd development mailing list if you have hardware
  258. * to donate to look into this problem....
  259. */
  260. LOG_WARNING("WARNING: mystery debug reason MOE = 0xc. Try issuing a resume + halt.");
  261. target->debug_reason = DBG_REASON_DBGRQ;
  262. break;
  263. default:
  264. LOG_WARNING("WARNING: unknown debug reason: 0x%x", debug_reason);
  265. /* Oh agony! should we interpret this as a halt request or
  266. * that the target stopped on it's own accord?
  267. */
  268. target->debug_reason = DBG_REASON_DBGRQ;
  269. /* if we fail here, we won't talk to the target and it will
  270. * be reported to be in the halted state */
  271. break;
  272. }
  273. return ERROR_OK;
  274. }
  275. static int arm926ejs_get_ttb(struct target *target, uint32_t *result)
  276. {
  277. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  278. int retval;
  279. uint32_t ttb = 0x0;
  280. retval = arm926ejs->read_cp15(target, 0, 0, 2, 0, &ttb);
  281. if (retval != ERROR_OK)
  282. return retval;
  283. *result = ttb;
  284. return ERROR_OK;
  285. }
  286. static int arm926ejs_disable_mmu_caches(struct target *target, int mmu,
  287. int d_u_cache, int i_cache)
  288. {
  289. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  290. uint32_t cp15_control;
  291. int retval;
  292. /* read cp15 control register */
  293. retval = arm926ejs->read_cp15(target, 0, 0, 1, 0, &cp15_control);
  294. if (retval != ERROR_OK)
  295. return retval;
  296. retval = jtag_execute_queue();
  297. if (retval != ERROR_OK)
  298. return retval;
  299. if (mmu) {
  300. /* invalidate TLB */
  301. retval = arm926ejs->write_cp15(target, 0, 0, 8, 7, 0x0);
  302. if (retval != ERROR_OK)
  303. return retval;
  304. cp15_control &= ~0x1U;
  305. }
  306. if (d_u_cache) {
  307. uint32_t debug_override;
  308. /* read-modify-write CP15 debug override register
  309. * to enable "test and clean all" */
  310. retval = arm926ejs->read_cp15(target, 0, 0, 15, 0, &debug_override);
  311. if (retval != ERROR_OK)
  312. return retval;
  313. debug_override |= 0x80000;
  314. retval = arm926ejs->write_cp15(target, 0, 0, 15, 0, debug_override);
  315. if (retval != ERROR_OK)
  316. return retval;
  317. /* clean and invalidate DCache */
  318. retval = arm926ejs->write_cp15(target, 0, 0, 7, 5, 0x0);
  319. if (retval != ERROR_OK)
  320. return retval;
  321. /* write CP15 debug override register
  322. * to disable "test and clean all" */
  323. debug_override &= ~0x80000;
  324. retval = arm926ejs->write_cp15(target, 0, 0, 15, 0, debug_override);
  325. if (retval != ERROR_OK)
  326. return retval;
  327. cp15_control &= ~0x4U;
  328. }
  329. if (i_cache) {
  330. /* invalidate ICache */
  331. retval = arm926ejs->write_cp15(target, 0, 0, 7, 5, 0x0);
  332. if (retval != ERROR_OK)
  333. return retval;
  334. cp15_control &= ~0x1000U;
  335. }
  336. retval = arm926ejs->write_cp15(target, 0, 0, 1, 0, cp15_control);
  337. return retval;
  338. }
  339. static int arm926ejs_enable_mmu_caches(struct target *target, int mmu,
  340. int d_u_cache, int i_cache)
  341. {
  342. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  343. uint32_t cp15_control;
  344. int retval;
  345. /* read cp15 control register */
  346. retval = arm926ejs->read_cp15(target, 0, 0, 1, 0, &cp15_control);
  347. if (retval != ERROR_OK)
  348. return retval;
  349. retval = jtag_execute_queue();
  350. if (retval != ERROR_OK)
  351. return retval;
  352. if (mmu)
  353. cp15_control |= 0x1U;
  354. if (d_u_cache)
  355. cp15_control |= 0x4U;
  356. if (i_cache)
  357. cp15_control |= 0x1000U;
  358. retval = arm926ejs->write_cp15(target, 0, 0, 1, 0, cp15_control);
  359. return retval;
  360. }
  361. static int arm926ejs_post_debug_entry(struct target *target)
  362. {
  363. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  364. int retval;
  365. /* examine cp15 control reg */
  366. retval = arm926ejs->read_cp15(target, 0, 0, 1, 0, &arm926ejs->cp15_control_reg);
  367. if (retval != ERROR_OK)
  368. return retval;
  369. retval = jtag_execute_queue();
  370. if (retval != ERROR_OK)
  371. return retval;
  372. LOG_DEBUG("cp15_control_reg: %8.8" PRIx32 "", arm926ejs->cp15_control_reg);
  373. if (arm926ejs->armv4_5_mmu.armv4_5_cache.ctype == -1) {
  374. uint32_t cache_type_reg;
  375. /* identify caches */
  376. retval = arm926ejs->read_cp15(target, 0, 1, 0, 0, &cache_type_reg);
  377. if (retval != ERROR_OK)
  378. return retval;
  379. retval = jtag_execute_queue();
  380. if (retval != ERROR_OK)
  381. return retval;
  382. armv4_5_identify_cache(cache_type_reg, &arm926ejs->armv4_5_mmu.armv4_5_cache);
  383. }
  384. arm926ejs->armv4_5_mmu.mmu_enabled = (arm926ejs->cp15_control_reg & 0x1U) ? 1 : 0;
  385. arm926ejs->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = (arm926ejs->cp15_control_reg & 0x4U) ? 1 : 0;
  386. arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled = (arm926ejs->cp15_control_reg & 0x1000U) ? 1 : 0;
  387. /* save i/d fault status and address register */
  388. retval = arm926ejs->read_cp15(target, 0, 0, 5, 0, &arm926ejs->d_fsr);
  389. if (retval != ERROR_OK)
  390. return retval;
  391. retval = arm926ejs->read_cp15(target, 0, 1, 5, 0, &arm926ejs->i_fsr);
  392. if (retval != ERROR_OK)
  393. return retval;
  394. retval = arm926ejs->read_cp15(target, 0, 0, 6, 0, &arm926ejs->d_far);
  395. if (retval != ERROR_OK)
  396. return retval;
  397. LOG_DEBUG("D FSR: 0x%8.8" PRIx32 ", D FAR: 0x%8.8" PRIx32 ", I FSR: 0x%8.8" PRIx32 "",
  398. arm926ejs->d_fsr, arm926ejs->d_far, arm926ejs->i_fsr);
  399. uint32_t cache_dbg_ctrl;
  400. /* read-modify-write CP15 cache debug control register
  401. * to disable I/D-cache linefills and force WT */
  402. retval = arm926ejs->read_cp15(target, 7, 0, 15, 0, &cache_dbg_ctrl);
  403. if (retval != ERROR_OK)
  404. return retval;
  405. cache_dbg_ctrl |= 0x7;
  406. retval = arm926ejs->write_cp15(target, 7, 0, 15, 0, cache_dbg_ctrl);
  407. return retval;
  408. }
  409. static void arm926ejs_pre_restore_context(struct target *target)
  410. {
  411. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  412. /* restore i/d fault status and address register */
  413. arm926ejs->write_cp15(target, 0, 0, 5, 0, arm926ejs->d_fsr);
  414. arm926ejs->write_cp15(target, 0, 1, 5, 0, arm926ejs->i_fsr);
  415. arm926ejs->write_cp15(target, 0, 0, 6, 0, arm926ejs->d_far);
  416. uint32_t cache_dbg_ctrl;
  417. /* read-modify-write CP15 cache debug control register
  418. * to reenable I/D-cache linefills and disable WT */
  419. arm926ejs->read_cp15(target, 7, 0, 15, 0, &cache_dbg_ctrl);
  420. cache_dbg_ctrl &= ~0x7;
  421. arm926ejs->write_cp15(target, 7, 0, 15, 0, cache_dbg_ctrl);
  422. }
  423. static const char arm926_not[] = "target is not an ARM926";
  424. static int arm926ejs_verify_pointer(struct command_context *cmd_ctx,
  425. struct arm926ejs_common *arm926)
  426. {
  427. if (arm926->common_magic != ARM926EJS_COMMON_MAGIC) {
  428. command_print(cmd_ctx, arm926_not);
  429. return ERROR_TARGET_INVALID;
  430. }
  431. return ERROR_OK;
  432. }
  433. /** Logs summary of ARM926 state for a halted target. */
  434. int arm926ejs_arch_state(struct target *target)
  435. {
  436. static const char *state[] = {
  437. "disabled", "enabled"
  438. };
  439. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  440. if (arm926ejs->common_magic != ARM926EJS_COMMON_MAGIC) {
  441. LOG_ERROR("BUG: %s", arm926_not);
  442. return ERROR_TARGET_INVALID;
  443. }
  444. arm_arch_state(target);
  445. LOG_USER("MMU: %s, D-Cache: %s, I-Cache: %s",
  446. state[arm926ejs->armv4_5_mmu.mmu_enabled],
  447. state[arm926ejs->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled],
  448. state[arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled]);
  449. return ERROR_OK;
  450. }
  451. int arm926ejs_soft_reset_halt(struct target *target)
  452. {
  453. int retval = ERROR_OK;
  454. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  455. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  456. struct arm *arm = &arm7_9->arm;
  457. struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  458. retval = target_halt(target);
  459. if (retval != ERROR_OK)
  460. return retval;
  461. int64_t then = timeval_ms();
  462. int timeout;
  463. while (!(timeout = ((timeval_ms()-then) > 1000))) {
  464. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0) {
  465. embeddedice_read_reg(dbg_stat);
  466. retval = jtag_execute_queue();
  467. if (retval != ERROR_OK)
  468. return retval;
  469. } else
  470. break;
  471. if (debug_level >= 1) {
  472. /* do not eat all CPU, time out after 1 se*/
  473. alive_sleep(100);
  474. } else
  475. keep_alive();
  476. }
  477. if (timeout) {
  478. LOG_ERROR("Failed to halt CPU after 1 sec");
  479. return ERROR_TARGET_TIMEOUT;
  480. }
  481. target->state = TARGET_HALTED;
  482. /* SVC, ARM state, IRQ and FIQ disabled */
  483. uint32_t cpsr;
  484. cpsr = buf_get_u32(arm->cpsr->value, 0, 32);
  485. cpsr &= ~0xff;
  486. cpsr |= 0xd3;
  487. arm_set_cpsr(arm, cpsr);
  488. arm->cpsr->dirty = 1;
  489. /* start fetching from 0x0 */
  490. buf_set_u32(arm->pc->value, 0, 32, 0x0);
  491. arm->pc->dirty = 1;
  492. arm->pc->valid = 1;
  493. retval = arm926ejs_disable_mmu_caches(target, 1, 1, 1);
  494. if (retval != ERROR_OK)
  495. return retval;
  496. arm926ejs->armv4_5_mmu.mmu_enabled = 0;
  497. arm926ejs->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = 0;
  498. arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled = 0;
  499. return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  500. }
  501. /** Writes a buffer, in the specified word size, with current MMU settings. */
  502. int arm926ejs_write_memory(struct target *target, target_addr_t address,
  503. uint32_t size, uint32_t count, const uint8_t *buffer)
  504. {
  505. int retval;
  506. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  507. /* FIX!!!! this should be cleaned up and made much more general. The
  508. * plan is to write up and test on arm926ejs specifically and
  509. * then generalize and clean up afterwards.
  510. *
  511. *
  512. * Also it should be moved to the callbacks that handle breakpoints
  513. * specifically and not the generic memory write fn's. See XScale code.
  514. **/
  515. if (arm926ejs->armv4_5_mmu.mmu_enabled && (count == 1) && ((size == 2) || (size == 4))) {
  516. /* special case the handling of single word writes to bypass MMU
  517. * to allow implementation of breakpoints in memory marked read only
  518. * by MMU */
  519. if (arm926ejs->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled) {
  520. /* flush and invalidate data cache
  521. *
  522. * MCR p15,0,p,c7,c10,1 - clean cache line using virtual address
  523. *
  524. */
  525. retval = arm926ejs->write_cp15(target, 0, 1, 7, 10, address&~0x3);
  526. if (retval != ERROR_OK)
  527. return retval;
  528. }
  529. target_addr_t pa;
  530. retval = target->type->virt2phys(target, address, &pa);
  531. if (retval != ERROR_OK)
  532. return retval;
  533. /* write directly to physical memory bypassing any read only MMU bits, etc. */
  534. retval = armv4_5_mmu_write_physical(target, &arm926ejs->armv4_5_mmu, pa, size, count, buffer);
  535. if (retval != ERROR_OK)
  536. return retval;
  537. } else {
  538. retval = arm7_9_write_memory(target, address, size, count, buffer);
  539. if (retval != ERROR_OK)
  540. return retval;
  541. }
  542. /* If ICache is enabled, we have to invalidate affected ICache lines
  543. * the DCache is forced to write-through, so we don't have to clean it here
  544. */
  545. if (arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled) {
  546. if (count <= 1) {
  547. /* invalidate ICache single entry with MVA */
  548. arm926ejs->write_cp15(target, 0, 1, 7, 5, address);
  549. } else {
  550. /* invalidate ICache */
  551. arm926ejs->write_cp15(target, 0, 0, 7, 5, address);
  552. }
  553. }
  554. return retval;
  555. }
  556. static int arm926ejs_write_phys_memory(struct target *target,
  557. target_addr_t address, uint32_t size,
  558. uint32_t count, const uint8_t *buffer)
  559. {
  560. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  561. return armv4_5_mmu_write_physical(target, &arm926ejs->armv4_5_mmu,
  562. address, size, count, buffer);
  563. }
  564. static int arm926ejs_read_phys_memory(struct target *target,
  565. target_addr_t address, uint32_t size,
  566. uint32_t count, uint8_t *buffer)
  567. {
  568. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  569. return armv4_5_mmu_read_physical(target, &arm926ejs->armv4_5_mmu,
  570. address, size, count, buffer);
  571. }
  572. int arm926ejs_init_arch_info(struct target *target, struct arm926ejs_common *arm926ejs,
  573. struct jtag_tap *tap)
  574. {
  575. struct arm7_9_common *arm7_9 = &arm926ejs->arm7_9_common;
  576. arm7_9->arm.mrc = arm926ejs_mrc;
  577. arm7_9->arm.mcr = arm926ejs_mcr;
  578. /* initialize arm7/arm9 specific info (including armv4_5) */
  579. arm9tdmi_init_arch_info(target, arm7_9, tap);
  580. arm926ejs->common_magic = ARM926EJS_COMMON_MAGIC;
  581. arm7_9->post_debug_entry = arm926ejs_post_debug_entry;
  582. arm7_9->pre_restore_context = arm926ejs_pre_restore_context;
  583. arm7_9->write_memory = arm926ejs_write_memory;
  584. arm926ejs->read_cp15 = arm926ejs_cp15_read;
  585. arm926ejs->write_cp15 = arm926ejs_cp15_write;
  586. arm926ejs->armv4_5_mmu.armv4_5_cache.ctype = -1;
  587. arm926ejs->armv4_5_mmu.get_ttb = arm926ejs_get_ttb;
  588. arm926ejs->armv4_5_mmu.read_memory = arm7_9_read_memory;
  589. arm926ejs->armv4_5_mmu.write_memory = arm7_9_write_memory;
  590. arm926ejs->armv4_5_mmu.disable_mmu_caches = arm926ejs_disable_mmu_caches;
  591. arm926ejs->armv4_5_mmu.enable_mmu_caches = arm926ejs_enable_mmu_caches;
  592. arm926ejs->armv4_5_mmu.has_tiny_pages = 1;
  593. arm926ejs->armv4_5_mmu.mmu_enabled = 0;
  594. arm7_9->examine_debug_reason = arm926ejs_examine_debug_reason;
  595. /* The ARM926EJ-S implements the ARMv5TE architecture which
  596. * has the BKPT instruction, so we don't have to use a watchpoint comparator
  597. */
  598. arm7_9->arm_bkpt = ARMV5_BKPT(0x0);
  599. arm7_9->thumb_bkpt = ARMV5_T_BKPT(0x0) & 0xffff;
  600. return ERROR_OK;
  601. }
  602. static int arm926ejs_target_create(struct target *target, Jim_Interp *interp)
  603. {
  604. struct arm926ejs_common *arm926ejs = calloc(1, sizeof(struct arm926ejs_common));
  605. /* ARM9EJ-S core always reports 0x1 in Capture-IR */
  606. target->tap->ir_capture_mask = 0x0f;
  607. return arm926ejs_init_arch_info(target, arm926ejs, target->tap);
  608. }
  609. COMMAND_HANDLER(arm926ejs_handle_cache_info_command)
  610. {
  611. int retval;
  612. struct target *target = get_current_target(CMD_CTX);
  613. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  614. retval = arm926ejs_verify_pointer(CMD_CTX, arm926ejs);
  615. if (retval != ERROR_OK)
  616. return retval;
  617. return armv4_5_handle_cache_info_command(CMD_CTX, &arm926ejs->armv4_5_mmu.armv4_5_cache);
  618. }
  619. static int arm926ejs_virt2phys(struct target *target, target_addr_t virtual, target_addr_t *physical)
  620. {
  621. uint32_t cb;
  622. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  623. uint32_t ret;
  624. int retval = armv4_5_mmu_translate_va(target, &arm926ejs->armv4_5_mmu,
  625. virtual, &cb, &ret);
  626. if (retval != ERROR_OK)
  627. return retval;
  628. *physical = ret;
  629. return ERROR_OK;
  630. }
  631. static int arm926ejs_mmu(struct target *target, int *enabled)
  632. {
  633. struct arm926ejs_common *arm926ejs = target_to_arm926(target);
  634. if (target->state != TARGET_HALTED) {
  635. LOG_ERROR("Target not halted");
  636. return ERROR_TARGET_INVALID;
  637. }
  638. *enabled = arm926ejs->armv4_5_mmu.mmu_enabled;
  639. return ERROR_OK;
  640. }
  641. static const struct command_registration arm926ejs_exec_command_handlers[] = {
  642. {
  643. .name = "cache_info",
  644. .handler = arm926ejs_handle_cache_info_command,
  645. .mode = COMMAND_EXEC,
  646. .usage = "",
  647. .help = "display information about target caches",
  648. },
  649. COMMAND_REGISTRATION_DONE
  650. };
  651. const struct command_registration arm926ejs_command_handlers[] = {
  652. {
  653. .chain = arm9tdmi_command_handlers,
  654. },
  655. {
  656. .name = "arm926ejs",
  657. .mode = COMMAND_ANY,
  658. .help = "arm926ejs command group",
  659. .usage = "",
  660. .chain = arm926ejs_exec_command_handlers,
  661. },
  662. COMMAND_REGISTRATION_DONE
  663. };
  664. /** Holds methods for ARM926 targets. */
  665. struct target_type arm926ejs_target = {
  666. .name = "arm926ejs",
  667. .poll = arm7_9_poll,
  668. .arch_state = arm926ejs_arch_state,
  669. .target_request_data = arm7_9_target_request_data,
  670. .halt = arm7_9_halt,
  671. .resume = arm7_9_resume,
  672. .step = arm7_9_step,
  673. .assert_reset = arm7_9_assert_reset,
  674. .deassert_reset = arm7_9_deassert_reset,
  675. .soft_reset_halt = arm926ejs_soft_reset_halt,
  676. .get_gdb_reg_list = arm_get_gdb_reg_list,
  677. .read_memory = arm7_9_read_memory,
  678. .write_memory = arm7_9_write_memory_opt,
  679. .checksum_memory = arm_checksum_memory,
  680. .blank_check_memory = arm_blank_check_memory,
  681. .run_algorithm = armv4_5_run_algorithm,
  682. .add_breakpoint = arm7_9_add_breakpoint,
  683. .remove_breakpoint = arm7_9_remove_breakpoint,
  684. .add_watchpoint = arm7_9_add_watchpoint,
  685. .remove_watchpoint = arm7_9_remove_watchpoint,
  686. .commands = arm926ejs_command_handlers,
  687. .target_create = arm926ejs_target_create,
  688. .init_target = arm9tdmi_init_target,
  689. .examine = arm7_9_examine,
  690. .check_reset = arm7_9_check_reset,
  691. .virt2phys = arm926ejs_virt2phys,
  692. .mmu = arm926ejs_mmu,
  693. .read_phys_memory = arm926ejs_read_phys_memory,
  694. .write_phys_memory = arm926ejs_write_phys_memory,
  695. };