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.
 
 
 
 
 
 

1806 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. if ((retval = jtag_execute_queue()) != ERROR_OK)
  195. {
  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 *armv4_5 = target_to_arm(target);
  205. uint32_t* regs_p[1];
  206. uint32_t regs[2];
  207. uint32_t cp15c15 = 0x0;
  208. struct reg *r = armv4_5->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(armv4_5->core_mode))
  232. {
  233. LOG_ERROR("not a valid arm core mode - communication failure?");
  234. return ERROR_FAIL;
  235. }
  236. r[0].dirty = 1;
  237. r[1].dirty = 1;
  238. return ERROR_OK;
  239. }
  240. static
  241. int arm920t_write_cp15_interpreted(struct target *target,
  242. uint32_t cp15_opcode, uint32_t value, uint32_t address)
  243. {
  244. uint32_t cp15c15 = 0x0;
  245. struct arm *armv4_5 = target_to_arm(target);
  246. uint32_t regs[2];
  247. struct reg *r = armv4_5->core_cache->reg_list;
  248. /* load value, address into R0, R1 */
  249. regs[0] = value;
  250. regs[1] = address;
  251. arm9tdmi_write_core_regs(target, 0x3, regs);
  252. /* read-modify-write CP15 test state register
  253. * to enable interpreted access mode */
  254. arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
  255. jtag_execute_queue();
  256. cp15c15 |= 1; /* set interpret mode */
  257. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  258. /* execute CP15 instruction and ARM store (writing to coprocessor) */
  259. arm920t_execute_cp15(target, cp15_opcode, ARMV4_5_STR(0, 1));
  260. /* disable interpreted access mode */
  261. cp15c15 &= ~1U; /* set interpret mode */
  262. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  263. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  264. LOG_DEBUG("cp15_opcode: %8.8x, value: %8.8x, address: %8.8x",
  265. cp15_opcode, value, address);
  266. #endif
  267. if (!is_arm_mode(armv4_5->core_mode))
  268. {
  269. LOG_ERROR("not a valid arm core mode - communication failure?");
  270. return ERROR_FAIL;
  271. }
  272. r[0].dirty = 1;
  273. r[1].dirty = 1;
  274. return ERROR_OK;
  275. }
  276. // EXPORTED to FA256
  277. int arm920t_get_ttb(struct target *target, uint32_t *result)
  278. {
  279. int retval;
  280. uint32_t ttb = 0x0;
  281. if ((retval = arm920t_read_cp15_interpreted(target,
  282. /* FIXME use opcode macro */
  283. 0xeebf0f51, 0x0, &ttb)) != ERROR_OK)
  284. return retval;
  285. *result = ttb;
  286. return ERROR_OK;
  287. }
  288. // EXPORTED to FA256
  289. int arm920t_disable_mmu_caches(struct target *target, int mmu,
  290. int d_u_cache, int i_cache)
  291. {
  292. uint32_t cp15_control;
  293. int retval;
  294. /* read cp15 control register */
  295. retval = arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &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. cp15_control &= ~0x1U;
  303. if (d_u_cache)
  304. cp15_control &= ~0x4U;
  305. if (i_cache)
  306. cp15_control &= ~0x1000U;
  307. retval = arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_control);
  308. return retval;
  309. }
  310. // EXPORTED to FA256
  311. int arm920t_enable_mmu_caches(struct target *target, int mmu,
  312. int d_u_cache, int i_cache)
  313. {
  314. uint32_t cp15_control;
  315. int retval;
  316. /* read cp15 control register */
  317. retval = arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_control);
  318. if (retval != ERROR_OK)
  319. return retval;
  320. retval = jtag_execute_queue();
  321. if (retval != ERROR_OK)
  322. return retval;
  323. if (mmu)
  324. cp15_control |= 0x1U;
  325. if (d_u_cache)
  326. cp15_control |= 0x4U;
  327. if (i_cache)
  328. cp15_control |= 0x1000U;
  329. retval = arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_control);
  330. return retval;
  331. }
  332. // EXPORTED to FA256
  333. int arm920t_post_debug_entry(struct target *target)
  334. {
  335. uint32_t cp15c15;
  336. struct arm920t_common *arm920t = target_to_arm920(target);
  337. int retval;
  338. /* examine cp15 control reg */
  339. retval = arm920t_read_cp15_physical(target,
  340. CP15PHYS_CTRL, &arm920t->cp15_control_reg);
  341. if (retval != ERROR_OK)
  342. return retval;
  343. retval = jtag_execute_queue();
  344. if (retval != ERROR_OK)
  345. return retval;
  346. LOG_DEBUG("cp15_control_reg: %8.8" PRIx32, arm920t->cp15_control_reg);
  347. if (arm920t->armv4_5_mmu.armv4_5_cache.ctype == -1)
  348. {
  349. uint32_t cache_type_reg;
  350. /* identify caches */
  351. retval = arm920t_read_cp15_physical(target,
  352. CP15PHYS_CACHETYPE, &cache_type_reg);
  353. if (retval != ERROR_OK)
  354. return retval;
  355. retval = jtag_execute_queue();
  356. if (retval != ERROR_OK)
  357. return retval;
  358. armv4_5_identify_cache(cache_type_reg,
  359. &arm920t->armv4_5_mmu.armv4_5_cache);
  360. }
  361. arm920t->armv4_5_mmu.mmu_enabled =
  362. (arm920t->cp15_control_reg & 0x1U) ? 1 : 0;
  363. arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled =
  364. (arm920t->cp15_control_reg & 0x4U) ? 1 : 0;
  365. arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled =
  366. (arm920t->cp15_control_reg & 0x1000U) ? 1 : 0;
  367. /* save i/d fault status and address register */
  368. /* FIXME use opcode macros */
  369. retval = arm920t_read_cp15_interpreted(target, 0xee150f10, 0x0, &arm920t->d_fsr);
  370. if (retval != ERROR_OK)
  371. return retval;
  372. retval = arm920t_read_cp15_interpreted(target, 0xee150f30, 0x0, &arm920t->i_fsr);
  373. if (retval != ERROR_OK)
  374. return retval;
  375. retval = arm920t_read_cp15_interpreted(target, 0xee160f10, 0x0, &arm920t->d_far);
  376. if (retval != ERROR_OK)
  377. return retval;
  378. retval = arm920t_read_cp15_interpreted(target, 0xee160f30, 0x0, &arm920t->i_far);
  379. if (retval != ERROR_OK)
  380. return retval;
  381. LOG_DEBUG("D FSR: 0x%8.8" PRIx32 ", D FAR: 0x%8.8" PRIx32
  382. ", I FSR: 0x%8.8" PRIx32 ", I FAR: 0x%8.8" PRIx32,
  383. arm920t->d_fsr, arm920t->d_far, arm920t->i_fsr, arm920t->i_far);
  384. if (arm920t->preserve_cache)
  385. {
  386. /* read-modify-write CP15 test state register
  387. * to disable I/D-cache linefills */
  388. retval = arm920t_read_cp15_physical(target,
  389. CP15PHYS_TESTSTATE, &cp15c15);
  390. if (retval != ERROR_OK)
  391. return retval;
  392. retval = jtag_execute_queue();
  393. if (retval != ERROR_OK)
  394. return retval;
  395. cp15c15 |= 0x600;
  396. retval = arm920t_write_cp15_physical(target,
  397. CP15PHYS_TESTSTATE, cp15c15);
  398. if (retval != ERROR_OK)
  399. return retval;
  400. }
  401. return ERROR_OK;
  402. }
  403. // EXPORTED to FA256
  404. void arm920t_pre_restore_context(struct target *target)
  405. {
  406. uint32_t cp15c15;
  407. struct arm920t_common *arm920t = target_to_arm920(target);
  408. /* restore i/d fault status and address register */
  409. arm920t_write_cp15_interpreted(target, 0xee050f10, arm920t->d_fsr, 0x0);
  410. arm920t_write_cp15_interpreted(target, 0xee050f30, arm920t->i_fsr, 0x0);
  411. arm920t_write_cp15_interpreted(target, 0xee060f10, arm920t->d_far, 0x0);
  412. arm920t_write_cp15_interpreted(target, 0xee060f30, arm920t->i_far, 0x0);
  413. /* read-modify-write CP15 test state register
  414. * to reenable I/D-cache linefills */
  415. if (arm920t->preserve_cache)
  416. {
  417. arm920t_read_cp15_physical(target,
  418. CP15PHYS_TESTSTATE, &cp15c15);
  419. jtag_execute_queue();
  420. cp15c15 &= ~0x600U;
  421. arm920t_write_cp15_physical(target,
  422. CP15PHYS_TESTSTATE, cp15c15);
  423. }
  424. }
  425. static const char arm920_not[] = "target is not an ARM920";
  426. static int arm920t_verify_pointer(struct command_context *cmd_ctx,
  427. struct arm920t_common *arm920t)
  428. {
  429. if (arm920t->common_magic != ARM920T_COMMON_MAGIC) {
  430. command_print(cmd_ctx, arm920_not);
  431. return ERROR_TARGET_INVALID;
  432. }
  433. return ERROR_OK;
  434. }
  435. /** Logs summary of ARM920 state for a halted target. */
  436. int arm920t_arch_state(struct target *target)
  437. {
  438. static const char *state[] =
  439. {
  440. "disabled", "enabled"
  441. };
  442. struct arm920t_common *arm920t = target_to_arm920(target);
  443. if (arm920t->common_magic != ARM920T_COMMON_MAGIC)
  444. {
  445. LOG_ERROR("BUG: %s", arm920_not);
  446. return ERROR_TARGET_INVALID;
  447. }
  448. arm_arch_state(target);
  449. LOG_USER("MMU: %s, D-Cache: %s, I-Cache: %s",
  450. state[arm920t->armv4_5_mmu.mmu_enabled],
  451. state[arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled],
  452. state[arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled]);
  453. return ERROR_OK;
  454. }
  455. static int arm920_mmu(struct target *target, int *enabled)
  456. {
  457. if (target->state != TARGET_HALTED) {
  458. LOG_ERROR("%s: target not halted", __func__);
  459. return ERROR_TARGET_INVALID;
  460. }
  461. *enabled = target_to_arm920(target)->armv4_5_mmu.mmu_enabled;
  462. return ERROR_OK;
  463. }
  464. static int arm920_virt2phys(struct target *target,
  465. uint32_t virt, uint32_t *phys)
  466. {
  467. uint32_t cb;
  468. struct arm920t_common *arm920t = target_to_arm920(target);
  469. uint32_t ret;
  470. int retval = armv4_5_mmu_translate_va(target,
  471. &arm920t->armv4_5_mmu, virt, &cb, &ret);
  472. if (retval != ERROR_OK)
  473. return retval;
  474. *phys = ret;
  475. return ERROR_OK;
  476. }
  477. /** Reads a buffer, in the specified word size, with current MMU settings. */
  478. int arm920t_read_memory(struct target *target, uint32_t address,
  479. uint32_t size, uint32_t count, uint8_t *buffer)
  480. {
  481. int retval;
  482. retval = arm7_9_read_memory(target, address, size, count, buffer);
  483. return retval;
  484. }
  485. static int arm920t_read_phys_memory(struct target *target,
  486. uint32_t address, uint32_t size,
  487. uint32_t count, uint8_t *buffer)
  488. {
  489. struct arm920t_common *arm920t = target_to_arm920(target);
  490. return armv4_5_mmu_read_physical(target, &arm920t->armv4_5_mmu,
  491. address, size, count, buffer);
  492. }
  493. static int arm920t_write_phys_memory(struct target *target,
  494. uint32_t address, uint32_t size,
  495. uint32_t count, const uint8_t *buffer)
  496. {
  497. struct arm920t_common *arm920t = target_to_arm920(target);
  498. return armv4_5_mmu_write_physical(target, &arm920t->armv4_5_mmu,
  499. address, size, count, buffer);
  500. }
  501. /** Writes a buffer, in the specified word size, with current MMU settings. */
  502. int arm920t_write_memory(struct target *target, uint32_t address,
  503. uint32_t size, uint32_t count, const uint8_t *buffer)
  504. {
  505. int retval;
  506. const uint32_t cache_mask = ~0x1f; /* cache line size : 32 byte */
  507. struct arm920t_common *arm920t = target_to_arm920(target);
  508. /* FIX!!!! this should be cleaned up and made much more general. The
  509. * plan is to write up and test on arm920t specifically and
  510. * then generalize and clean up afterwards.
  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 (arm920t->armv4_5_mmu.mmu_enabled && (count == 1) &&
  516. ((size==2) || (size==4)))
  517. {
  518. /* special case the handling of single word writes to
  519. * bypass MMU, to allow implementation of breakpoints
  520. * in memory marked read only
  521. * by MMU
  522. */
  523. uint32_t cb;
  524. uint32_t pa;
  525. /*
  526. * We need physical address and cb
  527. */
  528. retval = armv4_5_mmu_translate_va(target, &arm920t->armv4_5_mmu,
  529. address, &cb, &pa);
  530. if (retval != ERROR_OK)
  531. return retval;
  532. if (arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled)
  533. {
  534. if (cb & 0x1)
  535. {
  536. LOG_DEBUG("D-Cache buffered, "
  537. "drain write buffer");
  538. /*
  539. * Buffered ?
  540. * Drain write buffer - MCR p15,0,Rd,c7,c10,4
  541. */
  542. retval = arm920t_write_cp15_interpreted(target,
  543. ARMV4_5_MCR(15, 0, 0, 7, 10, 4),
  544. 0x0, 0);
  545. if (retval != ERROR_OK)
  546. return retval;
  547. }
  548. if (cb == 0x3)
  549. {
  550. /*
  551. * Write back memory ? -> clean cache
  552. *
  553. * There is no way to clean cache lines using
  554. * cp15 scan chain, so copy the full cache
  555. * line from cache to physical memory.
  556. */
  557. uint8_t data[32];
  558. LOG_DEBUG("D-Cache in 'write back' mode, "
  559. "flush cache line");
  560. retval = target_read_memory(target,
  561. address & cache_mask, 1,
  562. sizeof(data), &data[0]);
  563. if (retval != ERROR_OK)
  564. return retval;
  565. retval = armv4_5_mmu_write_physical(target,
  566. &arm920t->armv4_5_mmu,
  567. pa & cache_mask, 1,
  568. sizeof(data), &data[0]);
  569. if (retval != ERROR_OK)
  570. return retval;
  571. }
  572. /* Cached ? */
  573. if (cb & 0x2)
  574. {
  575. /*
  576. * Cached ? -> Invalidate data cache using MVA
  577. *
  578. * MCR p15,0,Rd,c7,c6,1
  579. */
  580. LOG_DEBUG("D-Cache enabled, "
  581. "invalidate cache line");
  582. retval = arm920t_write_cp15_interpreted(target,
  583. ARMV4_5_MCR(15, 0, 0, 7, 6, 1), 0x0,
  584. address & cache_mask);
  585. if (retval != ERROR_OK)
  586. return retval;
  587. }
  588. }
  589. /* write directly to physical memory,
  590. * bypassing any read only MMU bits, etc.
  591. */
  592. retval = armv4_5_mmu_write_physical(target,
  593. &arm920t->armv4_5_mmu, pa, size,
  594. count, buffer);
  595. if (retval != ERROR_OK)
  596. return retval;
  597. } else
  598. {
  599. if ((retval = arm7_9_write_memory(target, address,
  600. size, count, buffer)) != ERROR_OK)
  601. return retval;
  602. }
  603. /* If ICache is enabled, we have to invalidate affected ICache lines
  604. * the DCache is forced to write-through,
  605. * so we don't have to clean it here
  606. */
  607. if (arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled)
  608. {
  609. if (count <= 1)
  610. {
  611. /* invalidate ICache single entry with MVA
  612. * mcr 15, 0, r0, cr7, cr5, {1}
  613. */
  614. LOG_DEBUG("I-Cache enabled, "
  615. "invalidating affected I-Cache line");
  616. retval = arm920t_write_cp15_interpreted(target,
  617. ARMV4_5_MCR(15, 0, 0, 7, 5, 1),
  618. 0x0, address & cache_mask);
  619. if (retval != ERROR_OK)
  620. return retval;
  621. }
  622. else
  623. {
  624. /* invalidate ICache
  625. * mcr 15, 0, r0, cr7, cr5, {0}
  626. */
  627. retval = arm920t_write_cp15_interpreted(target,
  628. ARMV4_5_MCR(15, 0, 0, 7, 5, 0),
  629. 0x0, 0x0);
  630. if (retval != ERROR_OK)
  631. return retval;
  632. }
  633. }
  634. return ERROR_OK;
  635. }
  636. // EXPORTED to FA256
  637. int arm920t_soft_reset_halt(struct target *target)
  638. {
  639. int retval = ERROR_OK;
  640. struct arm920t_common *arm920t = target_to_arm920(target);
  641. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  642. struct arm *armv4_5 = &arm7_9->armv4_5_common;
  643. struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  644. if ((retval = target_halt(target)) != ERROR_OK)
  645. {
  646. return retval;
  647. }
  648. long long then = timeval_ms();
  649. int timeout;
  650. while (!(timeout = ((timeval_ms()-then) > 1000)))
  651. {
  652. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1)
  653. == 0)
  654. {
  655. embeddedice_read_reg(dbg_stat);
  656. if ((retval = jtag_execute_queue()) != ERROR_OK)
  657. {
  658. return retval;
  659. }
  660. } else
  661. {
  662. break;
  663. }
  664. if (debug_level >= 3)
  665. {
  666. /* do not eat all CPU, time out after 1 se*/
  667. alive_sleep(100);
  668. } else
  669. {
  670. keep_alive();
  671. }
  672. }
  673. if (timeout)
  674. {
  675. LOG_ERROR("Failed to halt CPU after 1 sec");
  676. return ERROR_TARGET_TIMEOUT;
  677. }
  678. target->state = TARGET_HALTED;
  679. /* SVC, ARM state, IRQ and FIQ disabled */
  680. uint32_t cpsr;
  681. cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 32);
  682. cpsr &= ~0xff;
  683. cpsr |= 0xd3;
  684. arm_set_cpsr(armv4_5, cpsr);
  685. armv4_5->cpsr->dirty = 1;
  686. /* start fetching from 0x0 */
  687. buf_set_u32(armv4_5->pc->value, 0, 32, 0x0);
  688. armv4_5->pc->dirty = 1;
  689. armv4_5->pc->valid = 1;
  690. arm920t_disable_mmu_caches(target, 1, 1, 1);
  691. arm920t->armv4_5_mmu.mmu_enabled = 0;
  692. arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = 0;
  693. arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled = 0;
  694. return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  695. }
  696. /* FIXME remove forward decls */
  697. static int arm920t_mrc(struct target *target, int cpnum,
  698. uint32_t op1, uint32_t op2,
  699. uint32_t CRn, uint32_t CRm,
  700. uint32_t *value);
  701. static int arm920t_mcr(struct target *target, int cpnum,
  702. uint32_t op1, uint32_t op2,
  703. uint32_t CRn, uint32_t CRm,
  704. uint32_t value);
  705. static int arm920t_init_arch_info(struct target *target,
  706. struct arm920t_common *arm920t, struct jtag_tap *tap)
  707. {
  708. struct arm7_9_common *arm7_9 = &arm920t->arm7_9_common;
  709. arm7_9->armv4_5_common.mrc = arm920t_mrc;
  710. arm7_9->armv4_5_common.mcr = arm920t_mcr;
  711. /* initialize arm7/arm9 specific info (including armv4_5) */
  712. arm9tdmi_init_arch_info(target, arm7_9, tap);
  713. arm920t->common_magic = ARM920T_COMMON_MAGIC;
  714. arm7_9->post_debug_entry = arm920t_post_debug_entry;
  715. arm7_9->pre_restore_context = arm920t_pre_restore_context;
  716. arm920t->armv4_5_mmu.armv4_5_cache.ctype = -1;
  717. arm920t->armv4_5_mmu.get_ttb = arm920t_get_ttb;
  718. arm920t->armv4_5_mmu.read_memory = arm7_9_read_memory;
  719. arm920t->armv4_5_mmu.write_memory = arm7_9_write_memory;
  720. arm920t->armv4_5_mmu.disable_mmu_caches = arm920t_disable_mmu_caches;
  721. arm920t->armv4_5_mmu.enable_mmu_caches = arm920t_enable_mmu_caches;
  722. arm920t->armv4_5_mmu.has_tiny_pages = 1;
  723. arm920t->armv4_5_mmu.mmu_enabled = 0;
  724. /* disabling linefills leads to lockups, so keep them enabled for now
  725. * this doesn't affect correctness, but might affect timing issues, if
  726. * important data is evicted from the cache during the debug session
  727. * */
  728. arm920t->preserve_cache = 0;
  729. /* override hw single-step capability from ARM9TDMI */
  730. arm7_9->has_single_step = 1;
  731. return ERROR_OK;
  732. }
  733. static int arm920t_target_create(struct target *target, Jim_Interp *interp)
  734. {
  735. struct arm920t_common *arm920t;
  736. arm920t = calloc(1,sizeof(struct arm920t_common));
  737. return arm920t_init_arch_info(target, arm920t, target->tap);
  738. }
  739. COMMAND_HANDLER(arm920t_handle_read_cache_command)
  740. {
  741. int retval = ERROR_OK;
  742. struct target *target = get_current_target(CMD_CTX);
  743. struct arm920t_common *arm920t = target_to_arm920(target);
  744. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  745. struct arm *armv4_5 = &arm7_9->armv4_5_common;
  746. uint32_t cp15c15;
  747. uint32_t cp15_ctrl, cp15_ctrl_saved;
  748. uint32_t regs[16];
  749. uint32_t *regs_p[16];
  750. uint32_t C15_C_D_Ind, C15_C_I_Ind;
  751. int i;
  752. FILE *output;
  753. int segment, index_t;
  754. struct reg *r;
  755. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  756. if (retval != ERROR_OK)
  757. return retval;
  758. if (CMD_ARGC != 1)
  759. {
  760. command_print(CMD_CTX, "usage: arm920t read_cache <filename>");
  761. return ERROR_OK;
  762. }
  763. if ((output = fopen(CMD_ARGV[0], "w")) == NULL)
  764. {
  765. LOG_DEBUG("error opening cache content file");
  766. return ERROR_OK;
  767. }
  768. for (i = 0; i < 16; i++)
  769. regs_p[i] = &regs[i];
  770. /* disable MMU and Caches */
  771. arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_ctrl);
  772. if ((retval = jtag_execute_queue()) != ERROR_OK)
  773. {
  774. return retval;
  775. }
  776. cp15_ctrl_saved = cp15_ctrl;
  777. cp15_ctrl &= ~(ARMV4_5_MMU_ENABLED
  778. | ARMV4_5_D_U_CACHE_ENABLED | ARMV4_5_I_CACHE_ENABLED);
  779. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl);
  780. /* read CP15 test state register */
  781. arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
  782. jtag_execute_queue();
  783. /* read DCache content */
  784. fprintf(output, "DCache:\n");
  785. /* go through segments 0 to nsets (8 on ARM920T, 4 on ARM922T) */
  786. for (segment = 0;
  787. segment < arm920t->armv4_5_mmu.armv4_5_cache.d_u_size.nsets;
  788. segment++)
  789. {
  790. fprintf(output, "\nsegment: %i\n----------", segment);
  791. /* Ra: r0 = SBZ(31:8):segment(7:5):SBZ(4:0) */
  792. regs[0] = 0x0 | (segment << 5);
  793. arm9tdmi_write_core_regs(target, 0x1, regs);
  794. /* set interpret mode */
  795. cp15c15 |= 0x1;
  796. arm920t_write_cp15_physical(target,
  797. CP15PHYS_TESTSTATE, cp15c15);
  798. /* D CAM Read, loads current victim into C15.C.D.Ind */
  799. arm920t_execute_cp15(target,
  800. ARMV4_5_MCR(15,2,0,15,6,2), ARMV4_5_LDR(1, 0));
  801. /* read current victim */
  802. arm920t_read_cp15_physical(target,
  803. CP15PHYS_DCACHE_IDX, &C15_C_D_Ind);
  804. /* clear interpret mode */
  805. cp15c15 &= ~0x1;
  806. arm920t_write_cp15_physical(target,
  807. CP15PHYS_TESTSTATE, cp15c15);
  808. for (index_t = 0; index_t < 64; index_t++)
  809. {
  810. /* Ra:
  811. * r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0)
  812. */
  813. regs[0] = 0x0 | (segment << 5) | (index_t << 26);
  814. arm9tdmi_write_core_regs(target, 0x1, regs);
  815. /* set interpret mode */
  816. cp15c15 |= 0x1;
  817. arm920t_write_cp15_physical(target,
  818. CP15PHYS_TESTSTATE, cp15c15);
  819. /* Write DCache victim */
  820. arm920t_execute_cp15(target,
  821. ARMV4_5_MCR(15,0,0,9,1,0), ARMV4_5_LDR(1, 0));
  822. /* Read D RAM */
  823. arm920t_execute_cp15(target,
  824. ARMV4_5_MCR(15,2,0,15,10,2),
  825. ARMV4_5_LDMIA(0, 0x1fe, 0, 0));
  826. /* Read D CAM */
  827. arm920t_execute_cp15(target,
  828. ARMV4_5_MCR(15,2,0,15,6,2),
  829. ARMV4_5_LDR(9, 0));
  830. /* clear interpret mode */
  831. cp15c15 &= ~0x1;
  832. arm920t_write_cp15_physical(target,
  833. CP15PHYS_TESTSTATE, cp15c15);
  834. /* read D RAM and CAM content */
  835. arm9tdmi_read_core_regs(target, 0x3fe, regs_p);
  836. if ((retval = jtag_execute_queue()) != ERROR_OK)
  837. {
  838. return retval;
  839. }
  840. /* mask LFSR[6] */
  841. regs[9] &= 0xfffffffe;
  842. fprintf(output, "\nsegment: %i, index: %i, CAM: 0x%8.8"
  843. PRIx32 ", content (%s):\n",
  844. segment, index_t, regs[9],
  845. (regs[9] & 0x10) ? "valid" : "invalid");
  846. for (i = 1; i < 9; i++)
  847. {
  848. fprintf(output, "%i: 0x%8.8" PRIx32 "\n",
  849. i-1, regs[i]);
  850. }
  851. }
  852. /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
  853. regs[0] = 0x0 | (segment << 5) | (C15_C_D_Ind << 26);
  854. arm9tdmi_write_core_regs(target, 0x1, regs);
  855. /* set interpret mode */
  856. cp15c15 |= 0x1;
  857. arm920t_write_cp15_physical(target,
  858. CP15PHYS_TESTSTATE, cp15c15);
  859. /* Write DCache victim */
  860. arm920t_execute_cp15(target,
  861. ARMV4_5_MCR(15,0,0,9,1,0), ARMV4_5_LDR(1, 0));
  862. /* clear interpret mode */
  863. cp15c15 &= ~0x1;
  864. arm920t_write_cp15_physical(target,
  865. CP15PHYS_TESTSTATE, cp15c15);
  866. }
  867. /* read ICache content */
  868. fprintf(output, "ICache:\n");
  869. /* go through segments 0 to nsets (8 on ARM920T, 4 on ARM922T) */
  870. for (segment = 0;
  871. segment < arm920t->armv4_5_mmu.armv4_5_cache.d_u_size.nsets;
  872. segment++)
  873. {
  874. fprintf(output, "segment: %i\n----------", segment);
  875. /* Ra: r0 = SBZ(31:8):segment(7:5):SBZ(4:0) */
  876. regs[0] = 0x0 | (segment << 5);
  877. arm9tdmi_write_core_regs(target, 0x1, regs);
  878. /* set interpret mode */
  879. cp15c15 |= 0x1;
  880. arm920t_write_cp15_physical(target,
  881. CP15PHYS_TESTSTATE, cp15c15);
  882. /* I CAM Read, loads current victim into C15.C.I.Ind */
  883. arm920t_execute_cp15(target,
  884. ARMV4_5_MCR(15,2,0,15,5,2), ARMV4_5_LDR(1, 0));
  885. /* read current victim */
  886. arm920t_read_cp15_physical(target, CP15PHYS_ICACHE_IDX,
  887. &C15_C_I_Ind);
  888. /* clear interpret mode */
  889. cp15c15 &= ~0x1;
  890. arm920t_write_cp15_physical(target,
  891. CP15PHYS_TESTSTATE, cp15c15);
  892. for (index_t = 0; index_t < 64; index_t++)
  893. {
  894. /* Ra:
  895. * r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0)
  896. */
  897. regs[0] = 0x0 | (segment << 5) | (index_t << 26);
  898. arm9tdmi_write_core_regs(target, 0x1, regs);
  899. /* set interpret mode */
  900. cp15c15 |= 0x1;
  901. arm920t_write_cp15_physical(target,
  902. CP15PHYS_TESTSTATE, cp15c15);
  903. /* Write ICache victim */
  904. arm920t_execute_cp15(target,
  905. ARMV4_5_MCR(15,0,0,9,1,1), ARMV4_5_LDR(1, 0));
  906. /* Read I RAM */
  907. arm920t_execute_cp15(target,
  908. ARMV4_5_MCR(15,2,0,15,9,2),
  909. ARMV4_5_LDMIA(0, 0x1fe, 0, 0));
  910. /* Read I CAM */
  911. arm920t_execute_cp15(target,
  912. ARMV4_5_MCR(15,2,0,15,5,2),
  913. ARMV4_5_LDR(9, 0));
  914. /* clear interpret mode */
  915. cp15c15 &= ~0x1;
  916. arm920t_write_cp15_physical(target,
  917. CP15PHYS_TESTSTATE, cp15c15);
  918. /* read I RAM and CAM content */
  919. arm9tdmi_read_core_regs(target, 0x3fe, regs_p);
  920. if ((retval = jtag_execute_queue()) != ERROR_OK)
  921. {
  922. return retval;
  923. }
  924. /* mask LFSR[6] */
  925. regs[9] &= 0xfffffffe;
  926. fprintf(output, "\nsegment: %i, index: %i, "
  927. "CAM: 0x%8.8" PRIx32 ", content (%s):\n",
  928. segment, index_t, regs[9],
  929. (regs[9] & 0x10) ? "valid" : "invalid");
  930. for (i = 1; i < 9; i++)
  931. {
  932. fprintf(output, "%i: 0x%8.8" PRIx32 "\n",
  933. i-1, regs[i]);
  934. }
  935. }
  936. /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
  937. regs[0] = 0x0 | (segment << 5) | (C15_C_D_Ind << 26);
  938. arm9tdmi_write_core_regs(target, 0x1, regs);
  939. /* set interpret mode */
  940. cp15c15 |= 0x1;
  941. arm920t_write_cp15_physical(target,
  942. CP15PHYS_TESTSTATE, cp15c15);
  943. /* Write ICache victim */
  944. arm920t_execute_cp15(target,
  945. ARMV4_5_MCR(15,0,0,9,1,1), ARMV4_5_LDR(1, 0));
  946. /* clear interpret mode */
  947. cp15c15 &= ~0x1;
  948. arm920t_write_cp15_physical(target,
  949. CP15PHYS_TESTSTATE, cp15c15);
  950. }
  951. /* restore CP15 MMU and Cache settings */
  952. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl_saved);
  953. command_print(CMD_CTX, "cache content successfully output to %s",
  954. CMD_ARGV[0]);
  955. fclose(output);
  956. if (!is_arm_mode(armv4_5->core_mode))
  957. {
  958. LOG_ERROR("not a valid arm core mode - communication failure?");
  959. return ERROR_FAIL;
  960. }
  961. /* force writeback of the valid data */
  962. r = armv4_5->core_cache->reg_list;
  963. r[0].dirty = r[0].valid;
  964. r[1].dirty = r[1].valid;
  965. r[2].dirty = r[2].valid;
  966. r[3].dirty = r[3].valid;
  967. r[4].dirty = r[4].valid;
  968. r[5].dirty = r[5].valid;
  969. r[6].dirty = r[6].valid;
  970. r[7].dirty = r[7].valid;
  971. r = arm_reg_current(armv4_5, 8);
  972. r->dirty = r->valid;
  973. r = arm_reg_current(armv4_5, 9);
  974. r->dirty = r->valid;
  975. return ERROR_OK;
  976. }
  977. COMMAND_HANDLER(arm920t_handle_read_mmu_command)
  978. {
  979. int retval = ERROR_OK;
  980. struct target *target = get_current_target(CMD_CTX);
  981. struct arm920t_common *arm920t = target_to_arm920(target);
  982. struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
  983. struct arm *armv4_5 = &arm7_9->armv4_5_common;
  984. uint32_t cp15c15;
  985. uint32_t cp15_ctrl, cp15_ctrl_saved;
  986. uint32_t regs[16];
  987. uint32_t *regs_p[16];
  988. int i;
  989. FILE *output;
  990. uint32_t Dlockdown, Ilockdown;
  991. struct arm920t_tlb_entry d_tlb[64], i_tlb[64];
  992. int victim;
  993. struct reg *r;
  994. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  995. if (retval != ERROR_OK)
  996. return retval;
  997. if (CMD_ARGC != 1)
  998. {
  999. command_print(CMD_CTX, "usage: arm920t read_mmu <filename>");
  1000. return ERROR_OK;
  1001. }
  1002. if ((output = fopen(CMD_ARGV[0], "w")) == NULL)
  1003. {
  1004. LOG_DEBUG("error opening mmu content file");
  1005. return ERROR_OK;
  1006. }
  1007. for (i = 0; i < 16; i++)
  1008. regs_p[i] = &regs[i];
  1009. /* disable MMU and Caches */
  1010. arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_ctrl);
  1011. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1012. {
  1013. return retval;
  1014. }
  1015. cp15_ctrl_saved = cp15_ctrl;
  1016. cp15_ctrl &= ~(ARMV4_5_MMU_ENABLED
  1017. | ARMV4_5_D_U_CACHE_ENABLED | ARMV4_5_I_CACHE_ENABLED);
  1018. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl);
  1019. /* read CP15 test state register */
  1020. arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
  1021. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1022. {
  1023. return retval;
  1024. }
  1025. /* prepare reading D TLB content
  1026. * */
  1027. /* set interpret mode */
  1028. cp15c15 |= 0x1;
  1029. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  1030. /* Read D TLB lockdown */
  1031. arm920t_execute_cp15(target,
  1032. ARMV4_5_MRC(15,0,0,10,0,0), ARMV4_5_LDR(1, 0));
  1033. /* clear interpret mode */
  1034. cp15c15 &= ~0x1;
  1035. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  1036. /* read D TLB lockdown stored to r1 */
  1037. arm9tdmi_read_core_regs(target, 0x2, regs_p);
  1038. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1039. {
  1040. return retval;
  1041. }
  1042. Dlockdown = regs[1];
  1043. for (victim = 0; victim < 64; victim += 8)
  1044. {
  1045. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  1046. * base remains unchanged, victim goes through entries 0 to 63
  1047. */
  1048. regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
  1049. arm9tdmi_write_core_regs(target, 0x2, regs);
  1050. /* set interpret mode */
  1051. cp15c15 |= 0x1;
  1052. arm920t_write_cp15_physical(target,
  1053. CP15PHYS_TESTSTATE, cp15c15);
  1054. /* Write D TLB lockdown */
  1055. arm920t_execute_cp15(target,
  1056. ARMV4_5_MCR(15,0,0,10,0,0),
  1057. ARMV4_5_STR(1, 0));
  1058. /* Read D TLB CAM */
  1059. arm920t_execute_cp15(target,
  1060. ARMV4_5_MCR(15,4,0,15,6,4),
  1061. ARMV4_5_LDMIA(0, 0x3fc, 0, 0));
  1062. /* clear interpret mode */
  1063. cp15c15 &= ~0x1;
  1064. arm920t_write_cp15_physical(target,
  1065. CP15PHYS_TESTSTATE, cp15c15);
  1066. /* read D TLB CAM content stored to r2-r9 */
  1067. arm9tdmi_read_core_regs(target, 0x3fc, regs_p);
  1068. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1069. {
  1070. return retval;
  1071. }
  1072. for (i = 0; i < 8; i++)
  1073. d_tlb[victim + i].cam = regs[i + 2];
  1074. }
  1075. for (victim = 0; victim < 64; victim++)
  1076. {
  1077. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  1078. * base remains unchanged, victim goes through entries 0 to 63
  1079. */
  1080. regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
  1081. arm9tdmi_write_core_regs(target, 0x2, regs);
  1082. /* set interpret mode */
  1083. cp15c15 |= 0x1;
  1084. arm920t_write_cp15_physical(target,
  1085. CP15PHYS_TESTSTATE, cp15c15);
  1086. /* Write D TLB lockdown */
  1087. arm920t_execute_cp15(target,
  1088. ARMV4_5_MCR(15,0,0,10,0,0), ARMV4_5_STR(1, 0));
  1089. /* Read D TLB RAM1 */
  1090. arm920t_execute_cp15(target,
  1091. ARMV4_5_MCR(15,4,0,15,10,4), ARMV4_5_LDR(2,0));
  1092. /* Read D TLB RAM2 */
  1093. arm920t_execute_cp15(target,
  1094. ARMV4_5_MCR(15,4,0,15,2,5), ARMV4_5_LDR(3,0));
  1095. /* clear interpret mode */
  1096. cp15c15 &= ~0x1;
  1097. arm920t_write_cp15_physical(target,
  1098. CP15PHYS_TESTSTATE, cp15c15);
  1099. /* read D TLB RAM content stored to r2 and r3 */
  1100. arm9tdmi_read_core_regs(target, 0xc, regs_p);
  1101. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1102. {
  1103. return retval;
  1104. }
  1105. d_tlb[victim].ram1 = regs[2];
  1106. d_tlb[victim].ram2 = regs[3];
  1107. }
  1108. /* restore D TLB lockdown */
  1109. regs[1] = Dlockdown;
  1110. arm9tdmi_write_core_regs(target, 0x2, regs);
  1111. /* Write D TLB lockdown */
  1112. arm920t_execute_cp15(target,
  1113. ARMV4_5_MCR(15,0,0,10,0,0), ARMV4_5_STR(1, 0));
  1114. /* prepare reading I TLB content
  1115. * */
  1116. /* set interpret mode */
  1117. cp15c15 |= 0x1;
  1118. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  1119. /* Read I TLB lockdown */
  1120. arm920t_execute_cp15(target,
  1121. ARMV4_5_MRC(15,0,0,10,0,1), ARMV4_5_LDR(1, 0));
  1122. /* clear interpret mode */
  1123. cp15c15 &= ~0x1;
  1124. arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
  1125. /* read I TLB lockdown stored to r1 */
  1126. arm9tdmi_read_core_regs(target, 0x2, regs_p);
  1127. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1128. {
  1129. return retval;
  1130. }
  1131. Ilockdown = regs[1];
  1132. for (victim = 0; victim < 64; victim += 8)
  1133. {
  1134. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  1135. * base remains unchanged, victim goes through entries 0 to 63
  1136. */
  1137. regs[1] = (Ilockdown & 0xfc000000) | (victim << 20);
  1138. arm9tdmi_write_core_regs(target, 0x2, regs);
  1139. /* set interpret mode */
  1140. cp15c15 |= 0x1;
  1141. arm920t_write_cp15_physical(target,
  1142. CP15PHYS_TESTSTATE, cp15c15);
  1143. /* Write I TLB lockdown */
  1144. arm920t_execute_cp15(target,
  1145. ARMV4_5_MCR(15,0,0,10,0,1),
  1146. ARMV4_5_STR(1, 0));
  1147. /* Read I TLB CAM */
  1148. arm920t_execute_cp15(target,
  1149. ARMV4_5_MCR(15,4,0,15,5,4),
  1150. ARMV4_5_LDMIA(0, 0x3fc, 0, 0));
  1151. /* clear interpret mode */
  1152. cp15c15 &= ~0x1;
  1153. arm920t_write_cp15_physical(target,
  1154. CP15PHYS_TESTSTATE, cp15c15);
  1155. /* read I TLB CAM content stored to r2-r9 */
  1156. arm9tdmi_read_core_regs(target, 0x3fc, regs_p);
  1157. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1158. {
  1159. return retval;
  1160. }
  1161. for (i = 0; i < 8; i++)
  1162. i_tlb[i + victim].cam = regs[i + 2];
  1163. }
  1164. for (victim = 0; victim < 64; victim++)
  1165. {
  1166. /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
  1167. * base remains unchanged, victim goes through entries 0 to 63
  1168. */
  1169. regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
  1170. arm9tdmi_write_core_regs(target, 0x2, regs);
  1171. /* set interpret mode */
  1172. cp15c15 |= 0x1;
  1173. arm920t_write_cp15_physical(target,
  1174. CP15PHYS_TESTSTATE, cp15c15);
  1175. /* Write I TLB lockdown */
  1176. arm920t_execute_cp15(target,
  1177. ARMV4_5_MCR(15,0,0,10,0,1), ARMV4_5_STR(1, 0));
  1178. /* Read I TLB RAM1 */
  1179. arm920t_execute_cp15(target,
  1180. ARMV4_5_MCR(15,4,0,15,9,4), ARMV4_5_LDR(2,0));
  1181. /* Read I TLB RAM2 */
  1182. arm920t_execute_cp15(target,
  1183. ARMV4_5_MCR(15,4,0,15,1,5), ARMV4_5_LDR(3,0));
  1184. /* clear interpret mode */
  1185. cp15c15 &= ~0x1;
  1186. arm920t_write_cp15_physical(target,
  1187. CP15PHYS_TESTSTATE, cp15c15);
  1188. /* read I TLB RAM content stored to r2 and r3 */
  1189. arm9tdmi_read_core_regs(target, 0xc, regs_p);
  1190. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1191. {
  1192. return retval;
  1193. }
  1194. i_tlb[victim].ram1 = regs[2];
  1195. i_tlb[victim].ram2 = regs[3];
  1196. }
  1197. /* restore I TLB lockdown */
  1198. regs[1] = Ilockdown;
  1199. arm9tdmi_write_core_regs(target, 0x2, regs);
  1200. /* Write I TLB lockdown */
  1201. arm920t_execute_cp15(target,
  1202. ARMV4_5_MCR(15,0,0,10,0,1), ARMV4_5_STR(1, 0));
  1203. /* restore CP15 MMU and Cache settings */
  1204. arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl_saved);
  1205. /* output data to file */
  1206. fprintf(output, "D TLB content:\n");
  1207. for (i = 0; i < 64; i++)
  1208. {
  1209. fprintf(output, "%i: 0x%8.8" PRIx32 " 0x%8.8" PRIx32
  1210. " 0x%8.8" PRIx32 " %s\n",
  1211. i, d_tlb[i].cam, d_tlb[i].ram1, d_tlb[i].ram2,
  1212. (d_tlb[i].cam & 0x20) ? "(valid)" : "(invalid)");
  1213. }
  1214. fprintf(output, "\n\nI TLB content:\n");
  1215. for (i = 0; i < 64; i++)
  1216. {
  1217. fprintf(output, "%i: 0x%8.8" PRIx32 " 0x%8.8" PRIx32
  1218. " 0x%8.8" PRIx32 " %s\n",
  1219. i, i_tlb[i].cam, i_tlb[i].ram1, i_tlb[i].ram2,
  1220. (i_tlb[i].cam & 0x20) ? "(valid)" : "(invalid)");
  1221. }
  1222. command_print(CMD_CTX, "mmu content successfully output to %s",
  1223. CMD_ARGV[0]);
  1224. fclose(output);
  1225. if (!is_arm_mode(armv4_5->core_mode))
  1226. {
  1227. LOG_ERROR("not a valid arm core mode - communication failure?");
  1228. return ERROR_FAIL;
  1229. }
  1230. /* force writeback of the valid data */
  1231. r = armv4_5->core_cache->reg_list;
  1232. r[0].dirty = r[0].valid;
  1233. r[1].dirty = r[1].valid;
  1234. r[2].dirty = r[2].valid;
  1235. r[3].dirty = r[3].valid;
  1236. r[4].dirty = r[4].valid;
  1237. r[5].dirty = r[5].valid;
  1238. r[6].dirty = r[6].valid;
  1239. r[7].dirty = r[7].valid;
  1240. r = arm_reg_current(armv4_5, 8);
  1241. r->dirty = r->valid;
  1242. r = arm_reg_current(armv4_5, 9);
  1243. r->dirty = r->valid;
  1244. return ERROR_OK;
  1245. }
  1246. COMMAND_HANDLER(arm920t_handle_cp15_command)
  1247. {
  1248. int retval;
  1249. struct target *target = get_current_target(CMD_CTX);
  1250. struct arm920t_common *arm920t = target_to_arm920(target);
  1251. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  1252. if (retval != ERROR_OK)
  1253. return retval;
  1254. if (target->state != TARGET_HALTED)
  1255. {
  1256. command_print(CMD_CTX, "target must be stopped for "
  1257. "\"%s\" command", CMD_NAME);
  1258. return ERROR_OK;
  1259. }
  1260. /* one argument, read a register.
  1261. * two arguments, write it.
  1262. */
  1263. if (CMD_ARGC >= 1)
  1264. {
  1265. int address;
  1266. COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
  1267. if (CMD_ARGC == 1)
  1268. {
  1269. uint32_t value;
  1270. if ((retval = arm920t_read_cp15_physical(target,
  1271. address, &value)) != ERROR_OK)
  1272. {
  1273. command_print(CMD_CTX,
  1274. "couldn't access reg %i", address);
  1275. return ERROR_OK;
  1276. }
  1277. if ((retval = jtag_execute_queue()) != ERROR_OK)
  1278. {
  1279. return retval;
  1280. }
  1281. command_print(CMD_CTX, "%i: %8.8" PRIx32,
  1282. address, value);
  1283. }
  1284. else if (CMD_ARGC == 2)
  1285. {
  1286. uint32_t value;
  1287. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  1288. retval = arm920t_write_cp15_physical(target,
  1289. address, value);
  1290. if (retval != ERROR_OK)
  1291. {
  1292. command_print(CMD_CTX,
  1293. "couldn't access reg %i", address);
  1294. /* REVISIT why lie? "return retval"? */
  1295. return ERROR_OK;
  1296. }
  1297. command_print(CMD_CTX, "%i: %8.8" PRIx32,
  1298. address, value);
  1299. }
  1300. }
  1301. return ERROR_OK;
  1302. }
  1303. COMMAND_HANDLER(arm920t_handle_cp15i_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. if (target->state != TARGET_HALTED)
  1312. {
  1313. command_print(CMD_CTX, "target must be stopped for "
  1314. "\"%s\" command", CMD_NAME);
  1315. return ERROR_OK;
  1316. }
  1317. /* one argument, read a register.
  1318. * two arguments, write it.
  1319. */
  1320. if (CMD_ARGC >= 1)
  1321. {
  1322. uint32_t opcode;
  1323. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], opcode);
  1324. if (CMD_ARGC == 1)
  1325. {
  1326. uint32_t value;
  1327. retval = arm920t_read_cp15_interpreted(target,
  1328. opcode, 0x0, &value);
  1329. if (retval != ERROR_OK)
  1330. {
  1331. command_print(CMD_CTX,
  1332. "couldn't execute %8.8" PRIx32,
  1333. opcode);
  1334. /* REVISIT why lie? "return retval"? */
  1335. return ERROR_OK;
  1336. }
  1337. command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32,
  1338. opcode, value);
  1339. }
  1340. else if (CMD_ARGC == 2)
  1341. {
  1342. uint32_t value;
  1343. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  1344. retval = arm920t_write_cp15_interpreted(target,
  1345. opcode, value, 0);
  1346. if (retval != ERROR_OK)
  1347. {
  1348. command_print(CMD_CTX,
  1349. "couldn't execute %8.8" PRIx32,
  1350. opcode);
  1351. /* REVISIT why lie? "return retval"? */
  1352. return ERROR_OK;
  1353. }
  1354. command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32,
  1355. opcode, value);
  1356. }
  1357. else if (CMD_ARGC == 3)
  1358. {
  1359. uint32_t value;
  1360. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  1361. uint32_t address;
  1362. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], address);
  1363. retval = arm920t_write_cp15_interpreted(target,
  1364. opcode, value, address);
  1365. if (retval != ERROR_OK)
  1366. {
  1367. command_print(CMD_CTX,
  1368. "couldn't execute %8.8" PRIx32, opcode);
  1369. /* REVISIT why lie? "return retval"? */
  1370. return ERROR_OK;
  1371. }
  1372. command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32
  1373. " %8.8" PRIx32, opcode, value, address);
  1374. }
  1375. }
  1376. else
  1377. {
  1378. command_print(CMD_CTX,
  1379. "usage: arm920t cp15i <opcode> [value] [address]");
  1380. }
  1381. return ERROR_OK;
  1382. }
  1383. COMMAND_HANDLER(arm920t_handle_cache_info_command)
  1384. {
  1385. int retval;
  1386. struct target *target = get_current_target(CMD_CTX);
  1387. struct arm920t_common *arm920t = target_to_arm920(target);
  1388. retval = arm920t_verify_pointer(CMD_CTX, arm920t);
  1389. if (retval != ERROR_OK)
  1390. return retval;
  1391. return armv4_5_handle_cache_info_command(CMD_CTX,
  1392. &arm920t->armv4_5_mmu.armv4_5_cache);
  1393. }
  1394. static int arm920t_mrc(struct target *target, int cpnum,
  1395. uint32_t op1, uint32_t op2,
  1396. uint32_t CRn, uint32_t CRm,
  1397. uint32_t *value)
  1398. {
  1399. if (cpnum!=15)
  1400. {
  1401. LOG_ERROR("Only cp15 is supported");
  1402. return ERROR_FAIL;
  1403. }
  1404. /* read "to" r0 */
  1405. return arm920t_read_cp15_interpreted(target,
  1406. ARMV4_5_MRC(cpnum, op1, 0, CRn, CRm, op2),
  1407. 0, value);
  1408. }
  1409. static int arm920t_mcr(struct target *target, int cpnum,
  1410. uint32_t op1, uint32_t op2,
  1411. uint32_t CRn, uint32_t CRm,
  1412. uint32_t value)
  1413. {
  1414. if (cpnum!=15)
  1415. {
  1416. LOG_ERROR("Only cp15 is supported");
  1417. return ERROR_FAIL;
  1418. }
  1419. /* write "from" r0 */
  1420. return arm920t_write_cp15_interpreted(target,
  1421. ARMV4_5_MCR(cpnum, op1, 0, CRn, CRm, op2),
  1422. 0, value);
  1423. }
  1424. static const struct command_registration arm920t_exec_command_handlers[] = {
  1425. {
  1426. .name = "cp15",
  1427. .handler = arm920t_handle_cp15_command,
  1428. .mode = COMMAND_EXEC,
  1429. .help = "display/modify cp15 register",
  1430. .usage = "regnum [value]",
  1431. },
  1432. {
  1433. .name = "cp15i",
  1434. .handler = arm920t_handle_cp15i_command,
  1435. .mode = COMMAND_EXEC,
  1436. /* prefer using less error-prone "arm mcr" or "arm mrc" */
  1437. .help = "display/modify cp15 register using ARM opcode"
  1438. " (DEPRECATED)",
  1439. .usage = "instruction [value [address]]",
  1440. },
  1441. {
  1442. .name = "cache_info",
  1443. .handler = arm920t_handle_cache_info_command,
  1444. .mode = COMMAND_EXEC,
  1445. .help = "display information about target caches",
  1446. },
  1447. {
  1448. .name = "read_cache",
  1449. .handler = arm920t_handle_read_cache_command,
  1450. .mode = COMMAND_EXEC,
  1451. .help = "dump I/D cache content to file",
  1452. .usage = "filename",
  1453. },
  1454. {
  1455. .name = "read_mmu",
  1456. .handler = arm920t_handle_read_mmu_command,
  1457. .mode = COMMAND_EXEC,
  1458. .help = "dump I/D mmu content to file",
  1459. .usage = "filename",
  1460. },
  1461. COMMAND_REGISTRATION_DONE
  1462. };
  1463. const struct command_registration arm920t_command_handlers[] = {
  1464. {
  1465. .chain = arm9tdmi_command_handlers,
  1466. },
  1467. {
  1468. .name = "arm920t",
  1469. .mode = COMMAND_ANY,
  1470. .help = "arm920t command group",
  1471. .chain = arm920t_exec_command_handlers,
  1472. },
  1473. COMMAND_REGISTRATION_DONE
  1474. };
  1475. /** Holds methods for ARM920 targets. */
  1476. struct target_type arm920t_target =
  1477. {
  1478. .name = "arm920t",
  1479. .poll = arm7_9_poll,
  1480. .arch_state = arm920t_arch_state,
  1481. .target_request_data = arm7_9_target_request_data,
  1482. .halt = arm7_9_halt,
  1483. .resume = arm7_9_resume,
  1484. .step = arm7_9_step,
  1485. .assert_reset = arm7_9_assert_reset,
  1486. .deassert_reset = arm7_9_deassert_reset,
  1487. .soft_reset_halt = arm920t_soft_reset_halt,
  1488. .get_gdb_reg_list = arm_get_gdb_reg_list,
  1489. .read_memory = arm920t_read_memory,
  1490. .write_memory = arm920t_write_memory,
  1491. .read_phys_memory = arm920t_read_phys_memory,
  1492. .write_phys_memory = arm920t_write_phys_memory,
  1493. .mmu = arm920_mmu,
  1494. .virt2phys = arm920_virt2phys,
  1495. .bulk_write_memory = arm7_9_bulk_write_memory,
  1496. .checksum_memory = arm_checksum_memory,
  1497. .blank_check_memory = arm_blank_check_memory,
  1498. .run_algorithm = armv4_5_run_algorithm,
  1499. .add_breakpoint = arm7_9_add_breakpoint,
  1500. .remove_breakpoint = arm7_9_remove_breakpoint,
  1501. .add_watchpoint = arm7_9_add_watchpoint,
  1502. .remove_watchpoint = arm7_9_remove_watchpoint,
  1503. .commands = arm920t_command_handlers,
  1504. .target_create = arm920t_target_create,
  1505. .init_target = arm9tdmi_init_target,
  1506. .examine = arm7_9_examine,
  1507. .check_reset = arm7_9_check_reset,
  1508. };