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.
 
 
 
 
 
 

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