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.
 
 
 
 
 
 

1724 lines
47 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "arm920t.h"
  24. #include <helper/time_support.h>
  25. #include "target_type.h"
  26. #include "register.h"
  27. #include "arm_opcodes.h"
  28. /*
  29. * For information about the ARM920T, see ARM DDI 0151C especially
  30. * Chapter 9 about debug support, which shows how to manipulate each
  31. * of the different scan chains:
  32. *
  33. * 0 ... ARM920 signals, e.g. to rest of SOC (unused here)
  34. * 1 ... debugging; watchpoint and breakpoint status, etc; also
  35. * MMU and cache access in conjunction with scan chain 15
  36. * 2 ... EmbeddedICE
  37. * 3 ... external boundary scan (SoC-specific, unused here)
  38. * 4 ... access to cache tag RAM
  39. * 6 ... ETM9
  40. * 15 ... access coprocessor 15, "physical" or "interpreted" modes
  41. * "interpreted" works with a few actual MRC/MCR instructions
  42. * "physical" provides register-like behaviors. Section 9.6.7
  43. * covers these details.
  44. *
  45. * The ARM922T is similar, but with smaller caches (8K each, vs 16K).
  46. */
  47. #if 0
  48. #define _DEBUG_INSTRUCTION_EXECUTION_
  49. #endif
  50. /* Table 9-8 shows scan chain 15 format during physical access mode, using a
  51. * dedicated 6-bit address space (encoded in bits 33:38). Writes use one
  52. * JTAG scan, while reads use two.
  53. *
  54. * Table 9-9 lists the thirteen registers which support physical access.
  55. * ARM920T_CP15_PHYS_ADDR() constructs the 6-bit reg_addr parameter passed
  56. * to arm920t_read_cp15_physical() and arm920t_write_cp15_physical().
  57. *
  58. * x == bit[38]
  59. * y == bits[37:34]
  60. * z == bit[33]
  61. */
  62. #define ARM920T_CP15_PHYS_ADDR(x, y, z) ((x << 5) | (y << 1) << (z))
  63. /* Registers supporting physical Read access (from table 9-9) */
  64. #define CP15PHYS_CACHETYPE ARM920T_CP15_PHYS_ADDR(0, 0x0, 1)
  65. #define CP15PHYS_ICACHE_IDX ARM920T_CP15_PHYS_ADDR(1, 0xd, 1)
  66. #define CP15PHYS_DCACHE_IDX ARM920T_CP15_PHYS_ADDR(1, 0xe, 1)
  67. /* NOTE: several more registers support only physical read access */
  68. /* Registers supporting physical Read/Write access (from table 9-9) */
  69. #define CP15PHYS_CTRL ARM920T_CP15_PHYS_ADDR(0, 0x1, 0)
  70. #define CP15PHYS_PID ARM920T_CP15_PHYS_ADDR(0, 0xd, 0)
  71. #define CP15PHYS_TESTSTATE ARM920T_CP15_PHYS_ADDR(0, 0xf, 0)
  72. #define CP15PHYS_ICACHE ARM920T_CP15_PHYS_ADDR(1, 0x1, 1)
  73. #define CP15PHYS_DCACHE ARM920T_CP15_PHYS_ADDR(1, 0x2, 1)
  74. static int arm920t_read_cp15_physical(struct target *target,
  75. int reg_addr, uint32_t *value)
  76. {
  77. struct arm920t_common *arm920t = target_to_arm920(target);
  78. struct arm_jtag *jtag_info;
  79. struct scan_field fields[4];
  80. uint8_t access_type_buf = 1;
  81. uint8_t reg_addr_buf = reg_addr & 0x3f;
  82. uint8_t nr_w_buf = 0;
  83. int retval;
  84. jtag_info = &arm920t->arm7_9_common.jtag_info;
  85. retval = arm_jtag_scann(jtag_info, 0xf, TAP_IDLE);
  86. if (retval != ERROR_OK)
  87. return retval;
  88. retval = arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL, TAP_IDLE);
  89. if (retval != ERROR_OK)
  90. return retval;
  91. fields[0].num_bits = 1;
  92. fields[0].out_value = &access_type_buf;
  93. fields[0].in_value = NULL;
  94. fields[1].num_bits = 32;
  95. fields[1].out_value = NULL;
  96. fields[1].in_value = NULL;
  97. fields[2].num_bits = 6;
  98. fields[2].out_value = &reg_addr_buf;
  99. fields[2].in_value = NULL;
  100. fields[3].num_bits = 1;
  101. fields[3].out_value = &nr_w_buf;
  102. fields[3].in_value = NULL;
  103. jtag_add_dr_scan(jtag_info->tap, 4, fields, TAP_IDLE);
  104. fields[1].in_value = (uint8_t *)value;
  105. jtag_add_dr_scan(jtag_info->tap, 4, fields, TAP_IDLE);
  106. jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)value);
  107. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  108. jtag_execute_queue();
  109. LOG_DEBUG("addr: 0x%x value: %8.8x", reg_addr, *value);
  110. #endif
  111. return ERROR_OK;
  112. }
  113. static int arm920t_write_cp15_physical(struct target *target,
  114. int reg_addr, uint32_t value)
  115. {
  116. struct arm920t_common *arm920t = target_to_arm920(target);
  117. struct arm_jtag *jtag_info;
  118. struct scan_field fields[4];
  119. uint8_t access_type_buf = 1;
  120. uint8_t reg_addr_buf = reg_addr & 0x3f;
  121. uint8_t nr_w_buf = 1;
  122. uint8_t value_buf[4];
  123. int retval;
  124. jtag_info = &arm920t->arm7_9_common.jtag_info;
  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 = 1;
  133. fields[0].out_value = &access_type_buf;
  134. fields[0].in_value = NULL;
  135. fields[1].num_bits = 32;
  136. fields[1].out_value = value_buf;
  137. fields[1].in_value = NULL;
  138. fields[2].num_bits = 6;
  139. fields[2].out_value = &reg_addr_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. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  146. LOG_DEBUG("addr: 0x%x value: %8.8x", reg_addr, value);
  147. #endif
  148. return ERROR_OK;
  149. }
  150. /* See table 9-10 for scan chain 15 format during interpreted access mode.
  151. * If the TESTSTATE register is set for interpreted access, certain CP15
  152. * MRC and MCR instructions may be executed through scan chain 15.
  153. *
  154. * Tables 9-11, 9-12, and 9-13 show which MRC and MCR instructions can be
  155. * executed using scan chain 15 interpreted mode.
  156. */
  157. static int arm920t_execute_cp15(struct target *target, uint32_t cp15_opcode,
  158. uint32_t arm_opcode)
  159. {
  160. int retval;
  161. struct arm920t_common *arm920t = target_to_arm920(target);
  162. struct arm_jtag *jtag_info;
  163. struct scan_field fields[4];
  164. uint8_t access_type_buf = 0; /* interpreted access */
  165. uint8_t reg_addr_buf = 0x0;
  166. uint8_t nr_w_buf = 0;
  167. uint8_t cp15_opcode_buf[4];
  168. jtag_info = &arm920t->arm7_9_common.jtag_info;
  169. retval = arm_jtag_scann(jtag_info, 0xf, TAP_IDLE);
  170. if (retval != ERROR_OK)
  171. return retval;
  172. retval = arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL, TAP_IDLE);
  173. if (retval != ERROR_OK)
  174. return retval;
  175. buf_set_u32(cp15_opcode_buf, 0, 32, cp15_opcode);
  176. fields[0].num_bits = 1;
  177. fields[0].out_value = &access_type_buf;
  178. fields[0].in_value = NULL;
  179. fields[1].num_bits = 32;
  180. fields[1].out_value = cp15_opcode_buf;
  181. fields[1].in_value = NULL;
  182. fields[2].num_bits = 6;
  183. fields[2].out_value = &reg_addr_buf;
  184. fields[2].in_value = NULL;
  185. fields[3].num_bits = 1;
  186. fields[3].out_value = &nr_w_buf;
  187. fields[3].in_value = NULL;
  188. jtag_add_dr_scan(jtag_info->tap, 4, fields, TAP_IDLE);
  189. arm9tdmi_clock_out(jtag_info, arm_opcode, 0, NULL, 0);
  190. arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
  191. retval = arm7_9_execute_sys_speed(target);
  192. if (retval != ERROR_OK)
  193. return retval;
  194. retval = jtag_execute_queue();
  195. if (retval != ERROR_OK) {
  196. LOG_ERROR("failed executing JTAG queue");
  197. return retval;
  198. }
  199. return ERROR_OK;
  200. }
  201. static int arm920t_read_cp15_interpreted(struct target *target,
  202. uint32_t cp15_opcode, uint32_t address, uint32_t *value)
  203. {
  204. struct arm *arm = target_to_arm(target);
  205. uint32_t *regs_p[1];
  206. uint32_t regs[2];
  207. uint32_t cp15c15 = 0x0;
  208. struct reg *r = arm->core_cache->reg_list;
  209. /* load address into R1 */
  210. regs[1] = address;
  211. arm9tdmi_write_core_regs(target, 0x2, regs);
  212. /* read-modify-write CP15 test state register
  213. * to enable interpreted access mode */
  214. arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
  215. jtag_execute_queue();
  216. cp15c15 |= 1; /* set interpret mode */
  217. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  218. /* execute CP15 instruction and ARM load (reading from coprocessor) */
  219. arm920t_execute_cp15(target, cp15_opcode, ARMV4_5_LDR(0, 1));
  220. /* disable interpreted access mode */
  221. cp15c15 &= ~1U; /* clear interpret mode */
  222. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  223. /* retrieve value from R0 */
  224. regs_p[0] = value;
  225. arm9tdmi_read_core_regs(target, 0x1, regs_p);
  226. jtag_execute_queue();
  227. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  228. LOG_DEBUG("cp15_opcode: %8.8x, address: %8.8x, value: %8.8x",
  229. cp15_opcode, address, *value);
  230. #endif
  231. if (!is_arm_mode(arm->core_mode)) {
  232. LOG_ERROR("not a valid arm core mode - communication failure?");
  233. return ERROR_FAIL;
  234. }
  235. r[0].dirty = 1;
  236. r[1].dirty = 1;
  237. return ERROR_OK;
  238. }
  239. static
  240. int arm920t_write_cp15_interpreted(struct target *target,
  241. uint32_t cp15_opcode, uint32_t value, uint32_t address)
  242. {
  243. uint32_t cp15c15 = 0x0;
  244. struct arm *arm = target_to_arm(target);
  245. uint32_t regs[2];
  246. struct reg *r = arm->core_cache->reg_list;
  247. /* load value, address into R0, R1 */
  248. regs[0] = value;
  249. regs[1] = address;
  250. arm9tdmi_write_core_regs(target, 0x3, regs);
  251. /* read-modify-write CP15 test state register
  252. * to enable interpreted access mode */
  253. arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
  254. jtag_execute_queue();
  255. cp15c15 |= 1; /* set interpret mode */
  256. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  257. /* execute CP15 instruction and ARM store (writing to coprocessor) */
  258. arm920t_execute_cp15(target, cp15_opcode, ARMV4_5_STR(0, 1));
  259. /* disable interpreted access mode */
  260. cp15c15 &= ~1U; /* set interpret mode */
  261. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  262. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  263. LOG_DEBUG("cp15_opcode: %8.8x, value: %8.8x, address: %8.8x",
  264. cp15_opcode, value, address);
  265. #endif
  266. if (!is_arm_mode(arm->core_mode)) {
  267. LOG_ERROR("not a valid arm core mode - communication failure?");
  268. return ERROR_FAIL;
  269. }
  270. r[0].dirty = 1;
  271. r[1].dirty = 1;
  272. return ERROR_OK;
  273. }
  274. /* EXPORTED to FA256 */
  275. int arm920t_get_ttb(struct target *target, uint32_t *result)
  276. {
  277. int retval;
  278. uint32_t ttb = 0x0;
  279. retval = arm920t_read_cp15_interpreted(target,
  280. /* FIXME use opcode macro */
  281. 0xeebf0f51, 0x0, &ttb);
  282. if (retval != ERROR_OK)
  283. return retval;
  284. *result = ttb;
  285. return ERROR_OK;
  286. }
  287. /* EXPORTED to FA256 */
  288. int arm920t_disable_mmu_caches(struct target *target, int mmu,
  289. int d_u_cache, int i_cache)
  290. {
  291. uint32_t cp15_control;
  292. int retval;
  293. /* read cp15 control register */
  294. retval = arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_control);
  295. if (retval != ERROR_OK)
  296. return retval;
  297. retval = jtag_execute_queue();
  298. if (retval != ERROR_OK)
  299. return retval;
  300. if (mmu)
  301. cp15_control &= ~0x1U;
  302. if (d_u_cache)
  303. cp15_control &= ~0x4U;
  304. if (i_cache)
  305. cp15_control &= ~0x1000U;
  306. retval = arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_control);
  307. return retval;
  308. }
  309. /* EXPORTED to FA256 */
  310. int arm920t_enable_mmu_caches(struct target *target, int mmu,
  311. int d_u_cache, int i_cache)
  312. {
  313. uint32_t cp15_control;
  314. int retval;
  315. /* read cp15 control register */
  316. retval = arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_control);
  317. if (retval != ERROR_OK)
  318. return retval;
  319. retval = jtag_execute_queue();
  320. if (retval != ERROR_OK)
  321. return retval;
  322. if (mmu)
  323. cp15_control |= 0x1U;
  324. if (d_u_cache)
  325. cp15_control |= 0x4U;
  326. if (i_cache)
  327. cp15_control |= 0x1000U;
  328. retval = arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_control);
  329. return retval;
  330. }
  331. /* EXPORTED to FA256 */
  332. int arm920t_post_debug_entry(struct target *target)
  333. {
  334. uint32_t cp15c15;
  335. struct arm920t_common *arm920t = target_to_arm920(target);
  336. int retval;
  337. /* examine cp15 control reg */
  338. retval = arm920t_read_cp15_physical(target,
  339. CP15PHYS_CTRL, &arm920t->cp15_control_reg);
  340. if (retval != ERROR_OK)
  341. return retval;
  342. retval = jtag_execute_queue();
  343. if (retval != ERROR_OK)
  344. return retval;
  345. LOG_DEBUG("cp15_control_reg: %8.8" PRIx32, arm920t->cp15_control_reg);
  346. if (arm920t->armv4_5_mmu.armv4_5_cache.ctype == -1) {
  347. uint32_t cache_type_reg;
  348. /* identify caches */
  349. retval = arm920t_read_cp15_physical(target,
  350. CP15PHYS_CACHETYPE, &cache_type_reg);
  351. if (retval != ERROR_OK)
  352. return retval;
  353. retval = jtag_execute_queue();
  354. if (retval != ERROR_OK)
  355. return retval;
  356. armv4_5_identify_cache(cache_type_reg,
  357. &arm920t->armv4_5_mmu.armv4_5_cache);
  358. }
  359. arm920t->armv4_5_mmu.mmu_enabled =
  360. (arm920t->cp15_control_reg & 0x1U) ? 1 : 0;
  361. arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled =
  362. (arm920t->cp15_control_reg & 0x4U) ? 1 : 0;
  363. arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled =
  364. (arm920t->cp15_control_reg & 0x1000U) ? 1 : 0;
  365. /* save i/d fault status and address register
  366. * FIXME use opcode macros */
  367. retval = arm920t_read_cp15_interpreted(target, 0xee150f10, 0x0, &arm920t->d_fsr);
  368. if (retval != ERROR_OK)
  369. return retval;
  370. retval = arm920t_read_cp15_interpreted(target, 0xee150f30, 0x0, &arm920t->i_fsr);
  371. if (retval != ERROR_OK)
  372. return retval;
  373. retval = arm920t_read_cp15_interpreted(target, 0xee160f10, 0x0, &arm920t->d_far);
  374. if (retval != ERROR_OK)
  375. return retval;
  376. retval = arm920t_read_cp15_interpreted(target, 0xee160f30, 0x0, &arm920t->i_far);
  377. if (retval != ERROR_OK)
  378. return retval;
  379. LOG_DEBUG("D FSR: 0x%8.8" PRIx32 ", D FAR: 0x%8.8" PRIx32
  380. ", I FSR: 0x%8.8" PRIx32 ", I FAR: 0x%8.8" PRIx32,
  381. arm920t->d_fsr, arm920t->d_far, arm920t->i_fsr, arm920t->i_far);
  382. if (arm920t->preserve_cache) {
  383. /* read-modify-write CP15 test state register
  384. * to disable I/D-cache linefills */
  385. retval = arm920t_read_cp15_physical(target,
  386. CP15PHYS_TESTSTATE, &cp15c15);
  387. if (retval != ERROR_OK)
  388. return retval;
  389. retval = jtag_execute_queue();
  390. if (retval != ERROR_OK)
  391. return retval;
  392. cp15c15 |= 0x600;
  393. retval = arm920t_write_cp15_physical(target,
  394. CP15PHYS_TESTSTATE, cp15c15);
  395. if (retval != ERROR_OK)
  396. return retval;
  397. }
  398. return ERROR_OK;
  399. }
  400. /* EXPORTED to FA256 */
  401. void arm920t_pre_restore_context(struct target *target)
  402. {
  403. uint32_t cp15c15;
  404. struct arm920t_common *arm920t = target_to_arm920(target);
  405. /* restore i/d fault status and address register */
  406. arm920t_write_cp15_interpreted(target, 0xee050f10, arm920t->d_fsr, 0x0);
  407. arm920t_write_cp15_interpreted(target, 0xee050f30, arm920t->i_fsr, 0x0);
  408. arm920t_write_cp15_interpreted(target, 0xee060f10, arm920t->d_far, 0x0);
  409. arm920t_write_cp15_interpreted(target, 0xee060f30, arm920t->i_far, 0x0);
  410. /* read-modify-write CP15 test state register
  411. * to reenable I/D-cache linefills */
  412. if (arm920t->preserve_cache) {
  413. arm920t_read_cp15_physical(target,
  414. CP15PHYS_TESTSTATE, &cp15c15);
  415. jtag_execute_queue();
  416. cp15c15 &= ~0x600U;
  417. arm920t_write_cp15_physical(target,
  418. CP15PHYS_TESTSTATE, cp15c15);
  419. }
  420. }
  421. static const char arm920_not[] = "target is not an ARM920";
  422. static int arm920t_verify_pointer(struct command_context *cmd_ctx,
  423. struct arm920t_common *arm920t)
  424. {
  425. if (arm920t->common_magic != ARM920T_COMMON_MAGIC) {
  426. command_print(cmd_ctx, arm920_not);
  427. return ERROR_TARGET_INVALID;
  428. }
  429. return ERROR_OK;
  430. }
  431. /** Logs summary of ARM920 state for a halted target. */
  432. int arm920t_arch_state(struct target *target)
  433. {
  434. static const char *state[] = {
  435. "disabled", "enabled"
  436. };
  437. struct arm920t_common *arm920t = target_to_arm920(target);
  438. if (arm920t->common_magic != ARM920T_COMMON_MAGIC) {
  439. LOG_ERROR("BUG: %s", arm920_not);
  440. return ERROR_TARGET_INVALID;
  441. }
  442. arm_arch_state(target);
  443. LOG_USER("MMU: %s, D-Cache: %s, I-Cache: %s",
  444. state[arm920t->armv4_5_mmu.mmu_enabled],
  445. state[arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled],
  446. state[arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled]);
  447. return ERROR_OK;
  448. }
  449. static int arm920_mmu(struct target *target, int *enabled)
  450. {
  451. if (target->state != TARGET_HALTED) {
  452. LOG_ERROR("%s: target not halted", __func__);
  453. return ERROR_TARGET_INVALID;
  454. }
  455. *enabled = target_to_arm920(target)->armv4_5_mmu.mmu_enabled;
  456. return ERROR_OK;
  457. }
  458. static int arm920_virt2phys(struct target *target,
  459. uint32_t virt, uint32_t *phys)
  460. {
  461. uint32_t cb;
  462. struct arm920t_common *arm920t = target_to_arm920(target);
  463. uint32_t ret;
  464. int retval = armv4_5_mmu_translate_va(target,
  465. &arm920t->armv4_5_mmu, virt, &cb, &ret);
  466. if (retval != ERROR_OK)
  467. return retval;
  468. *phys = ret;
  469. return ERROR_OK;
  470. }
  471. /** Reads a buffer, in the specified word size, with current MMU settings. */
  472. int arm920t_read_memory(struct target *target, uint32_t address,
  473. uint32_t size, uint32_t count, uint8_t *buffer)
  474. {
  475. int retval;
  476. retval = arm7_9_read_memory(target, address, size, count, buffer);
  477. return retval;
  478. }
  479. static int arm920t_read_phys_memory(struct target *target,
  480. uint32_t address, uint32_t size,
  481. uint32_t count, uint8_t *buffer)
  482. {
  483. struct arm920t_common *arm920t = target_to_arm920(target);
  484. return armv4_5_mmu_read_physical(target, &arm920t->armv4_5_mmu,
  485. address, size, count, buffer);
  486. }
  487. static int arm920t_write_phys_memory(struct target *target,
  488. uint32_t address, uint32_t size,
  489. uint32_t count, const uint8_t *buffer)
  490. {
  491. struct arm920t_common *arm920t = target_to_arm920(target);
  492. return armv4_5_mmu_write_physical(target, &arm920t->armv4_5_mmu,
  493. address, size, count, buffer);
  494. }
  495. /** Writes a buffer, in the specified word size, with current MMU settings. */
  496. int arm920t_write_memory(struct target *target, uint32_t address,
  497. uint32_t size, uint32_t count, const uint8_t *buffer)
  498. {
  499. int retval;
  500. const uint32_t cache_mask = ~0x1f; /* cache line size : 32 byte */
  501. struct arm920t_common *arm920t = target_to_arm920(target);
  502. /* FIX!!!! this should be cleaned up and made much more general. The
  503. * plan is to write up and test on arm920t specifically and
  504. * then generalize and clean up afterwards.
  505. *
  506. * Also it should be moved to the callbacks that handle breakpoints
  507. * specifically and not the generic memory write fn's. See XScale code.
  508. */
  509. if (arm920t->armv4_5_mmu.mmu_enabled && (count == 1) &&
  510. ((size == 2) || (size == 4))) {
  511. /* special case the handling of single word writes to
  512. * bypass MMU, to allow implementation of breakpoints
  513. * in memory marked read only
  514. * by MMU
  515. */
  516. uint32_t cb;
  517. uint32_t pa;
  518. /*
  519. * We need physical address and cb
  520. */
  521. retval = armv4_5_mmu_translate_va(target, &arm920t->armv4_5_mmu,
  522. address, &cb, &pa);
  523. if (retval != ERROR_OK)
  524. return retval;
  525. if (arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled) {
  526. if (cb & 0x1) {
  527. LOG_DEBUG("D-Cache buffered, "
  528. "drain write buffer");
  529. /*
  530. * Buffered ?
  531. * Drain write buffer - MCR p15,0,Rd,c7,c10,4
  532. */
  533. retval = arm920t_write_cp15_interpreted(target,
  534. ARMV4_5_MCR(15, 0, 0, 7, 10, 4),
  535. 0x0, 0);
  536. if (retval != ERROR_OK)
  537. return retval;
  538. }
  539. if (cb == 0x3) {
  540. /*
  541. * Write back memory ? -> clean cache
  542. *
  543. * There is no way to clean cache lines using
  544. * cp15 scan chain, so copy the full cache
  545. * line from cache to physical memory.
  546. */
  547. uint8_t data[32];
  548. LOG_DEBUG("D-Cache in 'write back' mode, "
  549. "flush cache line");
  550. retval = target_read_memory(target,
  551. address & cache_mask, 1,
  552. sizeof(data), &data[0]);
  553. if (retval != ERROR_OK)
  554. return retval;
  555. retval = armv4_5_mmu_write_physical(target,
  556. &arm920t->armv4_5_mmu,
  557. pa & cache_mask, 1,
  558. sizeof(data), &data[0]);
  559. if (retval != ERROR_OK)
  560. return retval;
  561. }
  562. /* Cached ? */
  563. if (cb & 0x2) {
  564. /*
  565. * Cached ? -> Invalidate data cache using MVA
  566. *
  567. * MCR p15,0,Rd,c7,c6,1
  568. */
  569. LOG_DEBUG("D-Cache enabled, "
  570. "invalidate cache line");
  571. retval = arm920t_write_cp15_interpreted(target,
  572. ARMV4_5_MCR(15, 0, 0, 7, 6, 1), 0x0,
  573. address & cache_mask);
  574. if (retval != ERROR_OK)
  575. return retval;
  576. }
  577. }
  578. /* write directly to physical memory,
  579. * bypassing any read only MMU bits, etc.
  580. */
  581. retval = armv4_5_mmu_write_physical(target,
  582. &arm920t->armv4_5_mmu, pa, size,
  583. count, buffer);
  584. if (retval != ERROR_OK)
  585. return retval;
  586. } else {
  587. retval = arm7_9_write_memory(target, address, size, count, buffer);
  588. if (retval != ERROR_OK)
  589. return retval;
  590. }
  591. /* If ICache is enabled, we have to invalidate affected ICache lines
  592. * the DCache is forced to write-through,
  593. * so we don't have to clean it here
  594. */
  595. if (arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled) {
  596. if (count <= 1) {
  597. /* invalidate ICache single entry with MVA
  598. * mcr 15, 0, r0, cr7, cr5, {1}
  599. */
  600. LOG_DEBUG("I-Cache enabled, "
  601. "invalidating affected I-Cache line");
  602. retval = arm920t_write_cp15_interpreted(target,
  603. ARMV4_5_MCR(15, 0, 0, 7, 5, 1),
  604. 0x0, address & cache_mask);
  605. if (retval != ERROR_OK)
  606. return retval;
  607. } else {
  608. /* invalidate ICache
  609. * mcr 15, 0, r0, cr7, cr5, {0}
  610. */
  611. retval = arm920t_write_cp15_interpreted(target,
  612. ARMV4_5_MCR(15, 0, 0, 7, 5, 0),
  613. 0x0, 0x0);
  614. if (retval != ERROR_OK)
  615. return retval;
  616. }
  617. }
  618. return ERROR_OK;
  619. }
  620. /* EXPORTED to FA256 */
  621. int arm920t_soft_reset_halt(struct target *target)
  622. {
  623. int retval = ERROR_OK;
  624. struct arm920t_common *arm920t = target_to_arm920(target);
  625. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  626. struct arm *arm = &arm7_9->arm;
  627. struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  628. retval = target_halt(target);
  629. if (retval != ERROR_OK)
  630. return retval;
  631. long long then = timeval_ms();
  632. int timeout;
  633. while (!(timeout = ((timeval_ms()-then) > 1000))) {
  634. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0) {
  635. embeddedice_read_reg(dbg_stat);
  636. retval = jtag_execute_queue();
  637. if (retval != ERROR_OK)
  638. return retval;
  639. } else
  640. break;
  641. if (debug_level >= 3) {
  642. /* do not eat all CPU, time out after 1 se*/
  643. alive_sleep(100);
  644. } else
  645. keep_alive();
  646. }
  647. if (timeout) {
  648. LOG_ERROR("Failed to halt CPU after 1 sec");
  649. return ERROR_TARGET_TIMEOUT;
  650. }
  651. target->state = TARGET_HALTED;
  652. /* SVC, ARM state, IRQ and FIQ disabled */
  653. uint32_t cpsr;
  654. cpsr = buf_get_u32(arm->cpsr->value, 0, 32);
  655. cpsr &= ~0xff;
  656. cpsr |= 0xd3;
  657. arm_set_cpsr(arm, cpsr);
  658. arm->cpsr->dirty = 1;
  659. /* start fetching from 0x0 */
  660. buf_set_u32(arm->pc->value, 0, 32, 0x0);
  661. arm->pc->dirty = 1;
  662. arm->pc->valid = 1;
  663. arm920t_disable_mmu_caches(target, 1, 1, 1);
  664. arm920t->armv4_5_mmu.mmu_enabled = 0;
  665. arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = 0;
  666. arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled = 0;
  667. return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  668. }
  669. /* FIXME remove forward decls */
  670. static int arm920t_mrc(struct target *target, int cpnum,
  671. uint32_t op1, uint32_t op2,
  672. uint32_t CRn, uint32_t CRm,
  673. uint32_t *value);
  674. static int arm920t_mcr(struct target *target, int cpnum,
  675. uint32_t op1, uint32_t op2,
  676. uint32_t CRn, uint32_t CRm,
  677. uint32_t value);
  678. static int arm920t_init_arch_info(struct target *target,
  679. struct arm920t_common *arm920t, struct jtag_tap *tap)
  680. {
  681. struct arm7_9_common *arm7_9 = &arm920t->arm7_9_common;
  682. arm7_9->arm.mrc = arm920t_mrc;
  683. arm7_9->arm.mcr = arm920t_mcr;
  684. /* initialize arm7/arm9 specific info (including armv4_5) */
  685. arm9tdmi_init_arch_info(target, arm7_9, tap);
  686. arm920t->common_magic = ARM920T_COMMON_MAGIC;
  687. arm7_9->post_debug_entry = arm920t_post_debug_entry;
  688. arm7_9->pre_restore_context = arm920t_pre_restore_context;
  689. arm920t->armv4_5_mmu.armv4_5_cache.ctype = -1;
  690. arm920t->armv4_5_mmu.get_ttb = arm920t_get_ttb;
  691. arm920t->armv4_5_mmu.read_memory = arm7_9_read_memory;
  692. arm920t->armv4_5_mmu.write_memory = arm7_9_write_memory;
  693. arm920t->armv4_5_mmu.disable_mmu_caches = arm920t_disable_mmu_caches;
  694. arm920t->armv4_5_mmu.enable_mmu_caches = arm920t_enable_mmu_caches;
  695. arm920t->armv4_5_mmu.has_tiny_pages = 1;
  696. arm920t->armv4_5_mmu.mmu_enabled = 0;
  697. /* disabling linefills leads to lockups, so keep them enabled for now
  698. * this doesn't affect correctness, but might affect timing issues, if
  699. * important data is evicted from the cache during the debug session
  700. * */
  701. arm920t->preserve_cache = 0;
  702. /* override hw single-step capability from ARM9TDMI */
  703. arm7_9->has_single_step = 1;
  704. return ERROR_OK;
  705. }
  706. static int arm920t_target_create(struct target *target, Jim_Interp *interp)
  707. {
  708. struct arm920t_common *arm920t;
  709. arm920t = calloc(1, sizeof(struct arm920t_common));
  710. return arm920t_init_arch_info(target, arm920t, target->tap);
  711. }
  712. COMMAND_HANDLER(arm920t_handle_read_cache_command)
  713. {
  714. int retval = ERROR_OK;
  715. struct target *target = get_current_target(CMD_CTX);
  716. struct arm920t_common *arm920t = target_to_arm920(target);
  717. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  718. struct arm *arm = &arm7_9->arm;
  719. uint32_t cp15c15;
  720. uint32_t cp15_ctrl, cp15_ctrl_saved;
  721. uint32_t regs[16];
  722. uint32_t *regs_p[16];
  723. uint32_t C15_C_D_Ind, C15_C_I_Ind;
  724. int i;
  725. FILE *output;
  726. int segment, index_t;
  727. struct reg *r;
  728. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  729. if (retval != ERROR_OK)
  730. return retval;
  731. if (CMD_ARGC != 1)
  732. return ERROR_COMMAND_SYNTAX_ERROR;
  733. output = fopen(CMD_ARGV[0], "w");
  734. if (output == NULL) {
  735. LOG_DEBUG("error opening cache content file");
  736. return ERROR_OK;
  737. }
  738. for (i = 0; i < 16; i++)
  739. regs_p[i] = &regs[i];
  740. /* disable MMU and Caches */
  741. arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_ctrl);
  742. retval = jtag_execute_queue();
  743. if (retval != ERROR_OK)
  744. return retval;
  745. cp15_ctrl_saved = cp15_ctrl;
  746. cp15_ctrl &= ~(ARMV4_5_MMU_ENABLED
  747. | ARMV4_5_D_U_CACHE_ENABLED | ARMV4_5_I_CACHE_ENABLED);
  748. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl);
  749. /* read CP15 test state register */
  750. arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
  751. jtag_execute_queue();
  752. /* read DCache content */
  753. fprintf(output, "DCache:\n");
  754. /* go through segments 0 to nsets (8 on ARM920T, 4 on ARM922T) */
  755. for (segment = 0;
  756. segment < arm920t->armv4_5_mmu.armv4_5_cache.d_u_size.nsets;
  757. segment++) {
  758. fprintf(output, "\nsegment: %i\n----------", segment);
  759. /* Ra: r0 = SBZ(31:8):segment(7:5):SBZ(4:0) */
  760. regs[0] = 0x0 | (segment << 5);
  761. arm9tdmi_write_core_regs(target, 0x1, regs);
  762. /* set interpret mode */
  763. cp15c15 |= 0x1;
  764. arm920t_write_cp15_physical(target,
  765. CP15PHYS_TESTSTATE, cp15c15);
  766. /* D CAM Read, loads current victim into C15.C.D.Ind */
  767. arm920t_execute_cp15(target,
  768. ARMV4_5_MCR(15, 2, 0, 15, 6, 2), ARMV4_5_LDR(1, 0));
  769. /* read current victim */
  770. arm920t_read_cp15_physical(target,
  771. CP15PHYS_DCACHE_IDX, &C15_C_D_Ind);
  772. /* clear interpret mode */
  773. cp15c15 &= ~0x1;
  774. arm920t_write_cp15_physical(target,
  775. CP15PHYS_TESTSTATE, cp15c15);
  776. for (index_t = 0; index_t < 64; index_t++) {
  777. /* Ra:
  778. * r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0)
  779. */
  780. regs[0] = 0x0 | (segment << 5) | (index_t << 26);
  781. arm9tdmi_write_core_regs(target, 0x1, regs);
  782. /* set interpret mode */
  783. cp15c15 |= 0x1;
  784. arm920t_write_cp15_physical(target,
  785. CP15PHYS_TESTSTATE, cp15c15);
  786. /* Write DCache victim */
  787. arm920t_execute_cp15(target,
  788. ARMV4_5_MCR(15, 0, 0, 9, 1, 0), ARMV4_5_LDR(1, 0));
  789. /* Read D RAM */
  790. arm920t_execute_cp15(target,
  791. ARMV4_5_MCR(15, 2, 0, 15, 10, 2),
  792. ARMV4_5_LDMIA(0, 0x1fe, 0, 0));
  793. /* Read D CAM */
  794. arm920t_execute_cp15(target,
  795. ARMV4_5_MCR(15, 2, 0, 15, 6, 2),
  796. ARMV4_5_LDR(9, 0));
  797. /* clear interpret mode */
  798. cp15c15 &= ~0x1;
  799. arm920t_write_cp15_physical(target,
  800. CP15PHYS_TESTSTATE, cp15c15);
  801. /* read D RAM and CAM content */
  802. arm9tdmi_read_core_regs(target, 0x3fe, regs_p);
  803. retval = jtag_execute_queue();
  804. if (retval != ERROR_OK)
  805. return retval;
  806. /* mask LFSR[6] */
  807. regs[9] &= 0xfffffffe;
  808. fprintf(output, "\nsegment: %i, index: %i, CAM: 0x%8.8"
  809. PRIx32 ", content (%s):\n",
  810. segment, index_t, regs[9],
  811. (regs[9] & 0x10) ? "valid" : "invalid");
  812. for (i = 1; i < 9; i++) {
  813. fprintf(output, "%i: 0x%8.8" PRIx32 "\n",
  814. i-1, regs[i]);
  815. }
  816. }
  817. /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
  818. regs[0] = 0x0 | (segment << 5) | (C15_C_D_Ind << 26);
  819. arm9tdmi_write_core_regs(target, 0x1, regs);
  820. /* set interpret mode */
  821. cp15c15 |= 0x1;
  822. arm920t_write_cp15_physical(target,
  823. CP15PHYS_TESTSTATE, cp15c15);
  824. /* Write DCache victim */
  825. arm920t_execute_cp15(target,
  826. ARMV4_5_MCR(15, 0, 0, 9, 1, 0), ARMV4_5_LDR(1, 0));
  827. /* clear interpret mode */
  828. cp15c15 &= ~0x1;
  829. arm920t_write_cp15_physical(target,
  830. CP15PHYS_TESTSTATE, cp15c15);
  831. }
  832. /* read ICache content */
  833. fprintf(output, "ICache:\n");
  834. /* go through segments 0 to nsets (8 on ARM920T, 4 on ARM922T) */
  835. for (segment = 0;
  836. segment < arm920t->armv4_5_mmu.armv4_5_cache.d_u_size.nsets;
  837. segment++) {
  838. fprintf(output, "segment: %i\n----------", segment);
  839. /* Ra: r0 = SBZ(31:8):segment(7:5):SBZ(4:0) */
  840. regs[0] = 0x0 | (segment << 5);
  841. arm9tdmi_write_core_regs(target, 0x1, regs);
  842. /* set interpret mode */
  843. cp15c15 |= 0x1;
  844. arm920t_write_cp15_physical(target,
  845. CP15PHYS_TESTSTATE, cp15c15);
  846. /* I CAM Read, loads current victim into C15.C.I.Ind */
  847. arm920t_execute_cp15(target,
  848. ARMV4_5_MCR(15, 2, 0, 15, 5, 2), ARMV4_5_LDR(1, 0));
  849. /* read current victim */
  850. arm920t_read_cp15_physical(target, CP15PHYS_ICACHE_IDX,
  851. &C15_C_I_Ind);
  852. /* clear interpret mode */
  853. cp15c15 &= ~0x1;
  854. arm920t_write_cp15_physical(target,
  855. CP15PHYS_TESTSTATE, cp15c15);
  856. for (index_t = 0; index_t < 64; index_t++) {
  857. /* Ra:
  858. * r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0)
  859. */
  860. regs[0] = 0x0 | (segment << 5) | (index_t << 26);
  861. arm9tdmi_write_core_regs(target, 0x1, regs);
  862. /* set interpret mode */
  863. cp15c15 |= 0x1;
  864. arm920t_write_cp15_physical(target,
  865. CP15PHYS_TESTSTATE, cp15c15);
  866. /* Write ICache victim */
  867. arm920t_execute_cp15(target,
  868. ARMV4_5_MCR(15, 0, 0, 9, 1, 1), ARMV4_5_LDR(1, 0));
  869. /* Read I RAM */
  870. arm920t_execute_cp15(target,
  871. ARMV4_5_MCR(15, 2, 0, 15, 9, 2),
  872. ARMV4_5_LDMIA(0, 0x1fe, 0, 0));
  873. /* Read I CAM */
  874. arm920t_execute_cp15(target,
  875. ARMV4_5_MCR(15, 2, 0, 15, 5, 2),
  876. ARMV4_5_LDR(9, 0));
  877. /* clear interpret mode */
  878. cp15c15 &= ~0x1;
  879. arm920t_write_cp15_physical(target,
  880. CP15PHYS_TESTSTATE, cp15c15);
  881. /* read I RAM and CAM content */
  882. arm9tdmi_read_core_regs(target, 0x3fe, regs_p);
  883. retval = jtag_execute_queue();
  884. if (retval != ERROR_OK)
  885. return retval;
  886. /* mask LFSR[6] */
  887. regs[9] &= 0xfffffffe;
  888. fprintf(output, "\nsegment: %i, index: %i, "
  889. "CAM: 0x%8.8" PRIx32 ", content (%s):\n",
  890. segment, index_t, regs[9],
  891. (regs[9] & 0x10) ? "valid" : "invalid");
  892. for (i = 1; i < 9; i++) {
  893. fprintf(output, "%i: 0x%8.8" PRIx32 "\n",
  894. i-1, regs[i]);
  895. }
  896. }
  897. /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
  898. regs[0] = 0x0 | (segment << 5) | (C15_C_D_Ind << 26);
  899. arm9tdmi_write_core_regs(target, 0x1, regs);
  900. /* set interpret mode */
  901. cp15c15 |= 0x1;
  902. arm920t_write_cp15_physical(target,
  903. CP15PHYS_TESTSTATE, cp15c15);
  904. /* Write ICache victim */
  905. arm920t_execute_cp15(target,
  906. ARMV4_5_MCR(15, 0, 0, 9, 1, 1), ARMV4_5_LDR(1, 0));
  907. /* clear interpret mode */
  908. cp15c15 &= ~0x1;
  909. arm920t_write_cp15_physical(target,
  910. CP15PHYS_TESTSTATE, cp15c15);
  911. }
  912. /* restore CP15 MMU and Cache settings */
  913. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl_saved);
  914. command_print(CMD_CTX, "cache content successfully output to %s",
  915. CMD_ARGV[0]);
  916. fclose(output);
  917. if (!is_arm_mode(arm->core_mode)) {
  918. LOG_ERROR("not a valid arm core mode - communication failure?");
  919. return ERROR_FAIL;
  920. }
  921. /* force writeback of the valid data */
  922. r = arm->core_cache->reg_list;
  923. r[0].dirty = r[0].valid;
  924. r[1].dirty = r[1].valid;
  925. r[2].dirty = r[2].valid;
  926. r[3].dirty = r[3].valid;
  927. r[4].dirty = r[4].valid;
  928. r[5].dirty = r[5].valid;
  929. r[6].dirty = r[6].valid;
  930. r[7].dirty = r[7].valid;
  931. r = arm_reg_current(arm, 8);
  932. r->dirty = r->valid;
  933. r = arm_reg_current(arm, 9);
  934. r->dirty = r->valid;
  935. return ERROR_OK;
  936. }
  937. COMMAND_HANDLER(arm920t_handle_read_mmu_command)
  938. {
  939. int retval = ERROR_OK;
  940. struct target *target = get_current_target(CMD_CTX);
  941. struct arm920t_common *arm920t = target_to_arm920(target);
  942. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  943. struct arm *arm = &arm7_9->arm;
  944. uint32_t cp15c15;
  945. uint32_t cp15_ctrl, cp15_ctrl_saved;
  946. uint32_t regs[16];
  947. uint32_t *regs_p[16];
  948. int i;
  949. FILE *output;
  950. uint32_t Dlockdown, Ilockdown;
  951. struct arm920t_tlb_entry d_tlb[64], i_tlb[64];
  952. int victim;
  953. struct reg *r;
  954. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  955. if (retval != ERROR_OK)
  956. return retval;
  957. if (CMD_ARGC != 1)
  958. return ERROR_COMMAND_SYNTAX_ERROR;
  959. output = fopen(CMD_ARGV[0], "w");
  960. if (output == NULL) {
  961. LOG_DEBUG("error opening mmu content file");
  962. return ERROR_OK;
  963. }
  964. for (i = 0; i < 16; i++)
  965. regs_p[i] = &regs[i];
  966. /* disable MMU and Caches */
  967. arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_ctrl);
  968. retval = jtag_execute_queue();
  969. if (retval != ERROR_OK)
  970. return retval;
  971. cp15_ctrl_saved = cp15_ctrl;
  972. cp15_ctrl &= ~(ARMV4_5_MMU_ENABLED
  973. | ARMV4_5_D_U_CACHE_ENABLED | ARMV4_5_I_CACHE_ENABLED);
  974. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl);
  975. /* read CP15 test state register */
  976. arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
  977. retval = jtag_execute_queue();
  978. if (retval != ERROR_OK)
  979. return retval;
  980. /* prepare reading D TLB content
  981. * */
  982. /* set interpret mode */
  983. cp15c15 |= 0x1;
  984. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  985. /* Read D TLB lockdown */
  986. arm920t_execute_cp15(target,
  987. ARMV4_5_MRC(15, 0, 0, 10, 0, 0), ARMV4_5_LDR(1, 0));
  988. /* clear interpret mode */
  989. cp15c15 &= ~0x1;
  990. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  991. /* read D TLB lockdown stored to r1 */
  992. arm9tdmi_read_core_regs(target, 0x2, regs_p);
  993. retval = jtag_execute_queue();
  994. if (retval != ERROR_OK)
  995. return retval;
  996. Dlockdown = regs[1];
  997. for (victim = 0; victim < 64; victim += 8) {
  998. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  999. * base remains unchanged, victim goes through entries 0 to 63
  1000. */
  1001. regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
  1002. arm9tdmi_write_core_regs(target, 0x2, regs);
  1003. /* set interpret mode */
  1004. cp15c15 |= 0x1;
  1005. arm920t_write_cp15_physical(target,
  1006. CP15PHYS_TESTSTATE, cp15c15);
  1007. /* Write D TLB lockdown */
  1008. arm920t_execute_cp15(target,
  1009. ARMV4_5_MCR(15, 0, 0, 10, 0, 0),
  1010. ARMV4_5_STR(1, 0));
  1011. /* Read D TLB CAM */
  1012. arm920t_execute_cp15(target,
  1013. ARMV4_5_MCR(15, 4, 0, 15, 6, 4),
  1014. ARMV4_5_LDMIA(0, 0x3fc, 0, 0));
  1015. /* clear interpret mode */
  1016. cp15c15 &= ~0x1;
  1017. arm920t_write_cp15_physical(target,
  1018. CP15PHYS_TESTSTATE, cp15c15);
  1019. /* read D TLB CAM content stored to r2-r9 */
  1020. arm9tdmi_read_core_regs(target, 0x3fc, regs_p);
  1021. retval = jtag_execute_queue();
  1022. if (retval != ERROR_OK)
  1023. return retval;
  1024. for (i = 0; i < 8; i++)
  1025. d_tlb[victim + i].cam = regs[i + 2];
  1026. }
  1027. for (victim = 0; victim < 64; victim++) {
  1028. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  1029. * base remains unchanged, victim goes through entries 0 to 63
  1030. */
  1031. regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
  1032. arm9tdmi_write_core_regs(target, 0x2, regs);
  1033. /* set interpret mode */
  1034. cp15c15 |= 0x1;
  1035. arm920t_write_cp15_physical(target,
  1036. CP15PHYS_TESTSTATE, cp15c15);
  1037. /* Write D TLB lockdown */
  1038. arm920t_execute_cp15(target,
  1039. ARMV4_5_MCR(15, 0, 0, 10, 0, 0), ARMV4_5_STR(1, 0));
  1040. /* Read D TLB RAM1 */
  1041. arm920t_execute_cp15(target,
  1042. ARMV4_5_MCR(15, 4, 0, 15, 10, 4), ARMV4_5_LDR(2, 0));
  1043. /* Read D TLB RAM2 */
  1044. arm920t_execute_cp15(target,
  1045. ARMV4_5_MCR(15, 4, 0, 15, 2, 5), ARMV4_5_LDR(3, 0));
  1046. /* clear interpret mode */
  1047. cp15c15 &= ~0x1;
  1048. arm920t_write_cp15_physical(target,
  1049. CP15PHYS_TESTSTATE, cp15c15);
  1050. /* read D TLB RAM content stored to r2 and r3 */
  1051. arm9tdmi_read_core_regs(target, 0xc, regs_p);
  1052. retval = jtag_execute_queue();
  1053. if (retval != ERROR_OK)
  1054. return retval;
  1055. d_tlb[victim].ram1 = regs[2];
  1056. d_tlb[victim].ram2 = regs[3];
  1057. }
  1058. /* restore D TLB lockdown */
  1059. regs[1] = Dlockdown;
  1060. arm9tdmi_write_core_regs(target, 0x2, regs);
  1061. /* Write D TLB lockdown */
  1062. arm920t_execute_cp15(target,
  1063. ARMV4_5_MCR(15, 0, 0, 10, 0, 0), ARMV4_5_STR(1, 0));
  1064. /* prepare reading I TLB content
  1065. * */
  1066. /* set interpret mode */
  1067. cp15c15 |= 0x1;
  1068. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  1069. /* Read I TLB lockdown */
  1070. arm920t_execute_cp15(target,
  1071. ARMV4_5_MRC(15, 0, 0, 10, 0, 1), ARMV4_5_LDR(1, 0));
  1072. /* clear interpret mode */
  1073. cp15c15 &= ~0x1;
  1074. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  1075. /* read I TLB lockdown stored to r1 */
  1076. arm9tdmi_read_core_regs(target, 0x2, regs_p);
  1077. retval = jtag_execute_queue();
  1078. if (retval != ERROR_OK)
  1079. return retval;
  1080. Ilockdown = regs[1];
  1081. for (victim = 0; victim < 64; victim += 8) {
  1082. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  1083. * base remains unchanged, victim goes through entries 0 to 63
  1084. */
  1085. regs[1] = (Ilockdown & 0xfc000000) | (victim << 20);
  1086. arm9tdmi_write_core_regs(target, 0x2, regs);
  1087. /* set interpret mode */
  1088. cp15c15 |= 0x1;
  1089. arm920t_write_cp15_physical(target,
  1090. CP15PHYS_TESTSTATE, cp15c15);
  1091. /* Write I TLB lockdown */
  1092. arm920t_execute_cp15(target,
  1093. ARMV4_5_MCR(15, 0, 0, 10, 0, 1),
  1094. ARMV4_5_STR(1, 0));
  1095. /* Read I TLB CAM */
  1096. arm920t_execute_cp15(target,
  1097. ARMV4_5_MCR(15, 4, 0, 15, 5, 4),
  1098. ARMV4_5_LDMIA(0, 0x3fc, 0, 0));
  1099. /* clear interpret mode */
  1100. cp15c15 &= ~0x1;
  1101. arm920t_write_cp15_physical(target,
  1102. CP15PHYS_TESTSTATE, cp15c15);
  1103. /* read I TLB CAM content stored to r2-r9 */
  1104. arm9tdmi_read_core_regs(target, 0x3fc, regs_p);
  1105. retval = jtag_execute_queue();
  1106. if (retval != ERROR_OK)
  1107. return retval;
  1108. for (i = 0; i < 8; i++)
  1109. i_tlb[i + victim].cam = regs[i + 2];
  1110. }
  1111. for (victim = 0; victim < 64; victim++) {
  1112. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  1113. * base remains unchanged, victim goes through entries 0 to 63
  1114. */
  1115. regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
  1116. arm9tdmi_write_core_regs(target, 0x2, regs);
  1117. /* set interpret mode */
  1118. cp15c15 |= 0x1;
  1119. arm920t_write_cp15_physical(target,
  1120. CP15PHYS_TESTSTATE, cp15c15);
  1121. /* Write I TLB lockdown */
  1122. arm920t_execute_cp15(target,
  1123. ARMV4_5_MCR(15, 0, 0, 10, 0, 1), ARMV4_5_STR(1, 0));
  1124. /* Read I TLB RAM1 */
  1125. arm920t_execute_cp15(target,
  1126. ARMV4_5_MCR(15, 4, 0, 15, 9, 4), ARMV4_5_LDR(2, 0));
  1127. /* Read I TLB RAM2 */
  1128. arm920t_execute_cp15(target,
  1129. ARMV4_5_MCR(15, 4, 0, 15, 1, 5), ARMV4_5_LDR(3, 0));
  1130. /* clear interpret mode */
  1131. cp15c15 &= ~0x1;
  1132. arm920t_write_cp15_physical(target,
  1133. CP15PHYS_TESTSTATE, cp15c15);
  1134. /* read I TLB RAM content stored to r2 and r3 */
  1135. arm9tdmi_read_core_regs(target, 0xc, regs_p);
  1136. retval = jtag_execute_queue();
  1137. if (retval != ERROR_OK)
  1138. return retval;
  1139. i_tlb[victim].ram1 = regs[2];
  1140. i_tlb[victim].ram2 = regs[3];
  1141. }
  1142. /* restore I TLB lockdown */
  1143. regs[1] = Ilockdown;
  1144. arm9tdmi_write_core_regs(target, 0x2, regs);
  1145. /* Write I TLB lockdown */
  1146. arm920t_execute_cp15(target,
  1147. ARMV4_5_MCR(15, 0, 0, 10, 0, 1), ARMV4_5_STR(1, 0));
  1148. /* restore CP15 MMU and Cache settings */
  1149. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl_saved);
  1150. /* output data to file */
  1151. fprintf(output, "D TLB content:\n");
  1152. for (i = 0; i < 64; i++) {
  1153. fprintf(output, "%i: 0x%8.8" PRIx32 " 0x%8.8" PRIx32
  1154. " 0x%8.8" PRIx32 " %s\n",
  1155. i, d_tlb[i].cam, d_tlb[i].ram1, d_tlb[i].ram2,
  1156. (d_tlb[i].cam & 0x20) ? "(valid)" : "(invalid)");
  1157. }
  1158. fprintf(output, "\n\nI TLB content:\n");
  1159. for (i = 0; i < 64; i++) {
  1160. fprintf(output, "%i: 0x%8.8" PRIx32 " 0x%8.8" PRIx32
  1161. " 0x%8.8" PRIx32 " %s\n",
  1162. i, i_tlb[i].cam, i_tlb[i].ram1, i_tlb[i].ram2,
  1163. (i_tlb[i].cam & 0x20) ? "(valid)" : "(invalid)");
  1164. }
  1165. command_print(CMD_CTX, "mmu content successfully output to %s",
  1166. CMD_ARGV[0]);
  1167. fclose(output);
  1168. if (!is_arm_mode(arm->core_mode)) {
  1169. LOG_ERROR("not a valid arm core mode - communication failure?");
  1170. return ERROR_FAIL;
  1171. }
  1172. /* force writeback of the valid data */
  1173. r = arm->core_cache->reg_list;
  1174. r[0].dirty = r[0].valid;
  1175. r[1].dirty = r[1].valid;
  1176. r[2].dirty = r[2].valid;
  1177. r[3].dirty = r[3].valid;
  1178. r[4].dirty = r[4].valid;
  1179. r[5].dirty = r[5].valid;
  1180. r[6].dirty = r[6].valid;
  1181. r[7].dirty = r[7].valid;
  1182. r = arm_reg_current(arm, 8);
  1183. r->dirty = r->valid;
  1184. r = arm_reg_current(arm, 9);
  1185. r->dirty = r->valid;
  1186. return ERROR_OK;
  1187. }
  1188. COMMAND_HANDLER(arm920t_handle_cp15_command)
  1189. {
  1190. int retval;
  1191. struct target *target = get_current_target(CMD_CTX);
  1192. struct arm920t_common *arm920t = target_to_arm920(target);
  1193. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  1194. if (retval != ERROR_OK)
  1195. return retval;
  1196. if (target->state != TARGET_HALTED) {
  1197. command_print(CMD_CTX, "target must be stopped for "
  1198. "\"%s\" command", CMD_NAME);
  1199. return ERROR_OK;
  1200. }
  1201. /* one argument, read a register.
  1202. * two arguments, write it.
  1203. */
  1204. if (CMD_ARGC >= 1) {
  1205. int address;
  1206. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
  1207. if (CMD_ARGC == 1) {
  1208. uint32_t value;
  1209. retval = arm920t_read_cp15_physical(target, address, &value);
  1210. if (retval != ERROR_OK) {
  1211. command_print(CMD_CTX,
  1212. "couldn't access reg %i", address);
  1213. return ERROR_OK;
  1214. }
  1215. retval = jtag_execute_queue();
  1216. if (retval != ERROR_OK)
  1217. return retval;
  1218. command_print(CMD_CTX, "%i: %8.8" PRIx32,
  1219. address, value);
  1220. } else if (CMD_ARGC == 2) {
  1221. uint32_t value;
  1222. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  1223. retval = arm920t_write_cp15_physical(target,
  1224. address, value);
  1225. if (retval != ERROR_OK) {
  1226. command_print(CMD_CTX,
  1227. "couldn't access reg %i", address);
  1228. /* REVISIT why lie? "return retval"? */
  1229. return ERROR_OK;
  1230. }
  1231. command_print(CMD_CTX, "%i: %8.8" PRIx32,
  1232. address, value);
  1233. }
  1234. }
  1235. return ERROR_OK;
  1236. }
  1237. COMMAND_HANDLER(arm920t_handle_cp15i_command)
  1238. {
  1239. int retval;
  1240. struct target *target = get_current_target(CMD_CTX);
  1241. struct arm920t_common *arm920t = target_to_arm920(target);
  1242. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  1243. if (retval != ERROR_OK)
  1244. return retval;
  1245. if (target->state != TARGET_HALTED) {
  1246. command_print(CMD_CTX, "target must be stopped for "
  1247. "\"%s\" command", CMD_NAME);
  1248. return ERROR_OK;
  1249. }
  1250. /* one argument, read a register.
  1251. * two arguments, write it.
  1252. */
  1253. if (CMD_ARGC >= 1) {
  1254. uint32_t opcode;
  1255. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], opcode);
  1256. if (CMD_ARGC == 1) {
  1257. uint32_t value;
  1258. retval = arm920t_read_cp15_interpreted(target,
  1259. opcode, 0x0, &value);
  1260. if (retval != ERROR_OK) {
  1261. command_print(CMD_CTX,
  1262. "couldn't execute %8.8" PRIx32,
  1263. opcode);
  1264. /* REVISIT why lie? "return retval"? */
  1265. return ERROR_OK;
  1266. }
  1267. command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32,
  1268. opcode, value);
  1269. } else if (CMD_ARGC == 2) {
  1270. uint32_t value;
  1271. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  1272. retval = arm920t_write_cp15_interpreted(target,
  1273. opcode, value, 0);
  1274. if (retval != ERROR_OK) {
  1275. command_print(CMD_CTX,
  1276. "couldn't execute %8.8" PRIx32,
  1277. opcode);
  1278. /* REVISIT why lie? "return retval"? */
  1279. return ERROR_OK;
  1280. }
  1281. command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32,
  1282. opcode, value);
  1283. } else if (CMD_ARGC == 3) {
  1284. uint32_t value;
  1285. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  1286. uint32_t address;
  1287. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], address);
  1288. retval = arm920t_write_cp15_interpreted(target,
  1289. opcode, value, address);
  1290. if (retval != ERROR_OK) {
  1291. command_print(CMD_CTX,
  1292. "couldn't execute %8.8" PRIx32, opcode);
  1293. /* REVISIT why lie? "return retval"? */
  1294. return ERROR_OK;
  1295. }
  1296. command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32
  1297. " %8.8" PRIx32, opcode, value, address);
  1298. }
  1299. } else
  1300. return ERROR_COMMAND_SYNTAX_ERROR;
  1301. return ERROR_OK;
  1302. }
  1303. COMMAND_HANDLER(arm920t_handle_cache_info_command)
  1304. {
  1305. int retval;
  1306. struct target *target = get_current_target(CMD_CTX);
  1307. struct arm920t_common *arm920t = target_to_arm920(target);
  1308. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  1309. if (retval != ERROR_OK)
  1310. return retval;
  1311. return armv4_5_handle_cache_info_command(CMD_CTX,
  1312. &arm920t->armv4_5_mmu.armv4_5_cache);
  1313. }
  1314. static int arm920t_mrc(struct target *target, int cpnum,
  1315. uint32_t op1, uint32_t op2,
  1316. uint32_t CRn, uint32_t CRm,
  1317. uint32_t *value)
  1318. {
  1319. if (cpnum != 15) {
  1320. LOG_ERROR("Only cp15 is supported");
  1321. return ERROR_FAIL;
  1322. }
  1323. /* read "to" r0 */
  1324. return arm920t_read_cp15_interpreted(target,
  1325. ARMV4_5_MRC(cpnum, op1, 0, CRn, CRm, op2),
  1326. 0, value);
  1327. }
  1328. static int arm920t_mcr(struct target *target, int cpnum,
  1329. uint32_t op1, uint32_t op2,
  1330. uint32_t CRn, uint32_t CRm,
  1331. uint32_t value)
  1332. {
  1333. if (cpnum != 15) {
  1334. LOG_ERROR("Only cp15 is supported");
  1335. return ERROR_FAIL;
  1336. }
  1337. /* write "from" r0 */
  1338. return arm920t_write_cp15_interpreted(target,
  1339. ARMV4_5_MCR(cpnum, op1, 0, CRn, CRm, op2),
  1340. 0, value);
  1341. }
  1342. static const struct command_registration arm920t_exec_command_handlers[] = {
  1343. {
  1344. .name = "cp15",
  1345. .handler = arm920t_handle_cp15_command,
  1346. .mode = COMMAND_EXEC,
  1347. .help = "display/modify cp15 register",
  1348. .usage = "regnum [value]",
  1349. },
  1350. {
  1351. .name = "cp15i",
  1352. .handler = arm920t_handle_cp15i_command,
  1353. .mode = COMMAND_EXEC,
  1354. /* prefer using less error-prone "arm mcr" or "arm mrc" */
  1355. .help = "display/modify cp15 register using ARM opcode"
  1356. " (DEPRECATED)",
  1357. .usage = "instruction [value [address]]",
  1358. },
  1359. {
  1360. .name = "cache_info",
  1361. .handler = arm920t_handle_cache_info_command,
  1362. .mode = COMMAND_EXEC,
  1363. .usage = "",
  1364. .help = "display information about target caches",
  1365. },
  1366. {
  1367. .name = "read_cache",
  1368. .handler = arm920t_handle_read_cache_command,
  1369. .mode = COMMAND_EXEC,
  1370. .help = "dump I/D cache content to file",
  1371. .usage = "filename",
  1372. },
  1373. {
  1374. .name = "read_mmu",
  1375. .handler = arm920t_handle_read_mmu_command,
  1376. .mode = COMMAND_EXEC,
  1377. .help = "dump I/D mmu content to file",
  1378. .usage = "filename",
  1379. },
  1380. COMMAND_REGISTRATION_DONE
  1381. };
  1382. const struct command_registration arm920t_command_handlers[] = {
  1383. {
  1384. .chain = arm9tdmi_command_handlers,
  1385. },
  1386. {
  1387. .name = "arm920t",
  1388. .mode = COMMAND_ANY,
  1389. .help = "arm920t command group",
  1390. .usage = "",
  1391. .chain = arm920t_exec_command_handlers,
  1392. },
  1393. COMMAND_REGISTRATION_DONE
  1394. };
  1395. /** Holds methods for ARM920 targets. */
  1396. struct target_type arm920t_target = {
  1397. .name = "arm920t",
  1398. .poll = arm7_9_poll,
  1399. .arch_state = arm920t_arch_state,
  1400. .target_request_data = arm7_9_target_request_data,
  1401. .halt = arm7_9_halt,
  1402. .resume = arm7_9_resume,
  1403. .step = arm7_9_step,
  1404. .assert_reset = arm7_9_assert_reset,
  1405. .deassert_reset = arm7_9_deassert_reset,
  1406. .soft_reset_halt = arm920t_soft_reset_halt,
  1407. .get_gdb_reg_list = arm_get_gdb_reg_list,
  1408. .read_memory = arm920t_read_memory,
  1409. .write_memory = arm920t_write_memory,
  1410. .read_phys_memory = arm920t_read_phys_memory,
  1411. .write_phys_memory = arm920t_write_phys_memory,
  1412. .mmu = arm920_mmu,
  1413. .virt2phys = arm920_virt2phys,
  1414. .bulk_write_memory = arm7_9_bulk_write_memory,
  1415. .checksum_memory = arm_checksum_memory,
  1416. .blank_check_memory = arm_blank_check_memory,
  1417. .run_algorithm = armv4_5_run_algorithm,
  1418. .add_breakpoint = arm7_9_add_breakpoint,
  1419. .remove_breakpoint = arm7_9_remove_breakpoint,
  1420. .add_watchpoint = arm7_9_add_watchpoint,
  1421. .remove_watchpoint = arm7_9_remove_watchpoint,
  1422. .commands = arm920t_command_handlers,
  1423. .target_create = arm920t_target_create,
  1424. .init_target = arm9tdmi_init_target,
  1425. .examine = arm7_9_examine,
  1426. .check_reset = arm7_9_check_reset,
  1427. };