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.
 
 
 
 
 
 

990 lines
30 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 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 "arm926ejs.h"
  24. #include "time_support.h"
  25. #include "target_type.h"
  26. #if 0
  27. #define _DEBUG_INSTRUCTION_EXECUTION_
  28. #endif
  29. /* cli handling */
  30. int arm926ejs_handle_cp15_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  31. int arm926ejs_handle_cp15i_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  32. int arm926ejs_handle_virt2phys_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  33. int arm926ejs_handle_cache_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  34. int arm926ejs_handle_md_phys_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  35. int arm926ejs_handle_mw_phys_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  36. int arm926ejs_handle_read_cache_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  37. int arm926ejs_handle_read_mmu_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  38. /* forward declarations */
  39. int arm926ejs_target_create(struct target_s *target, Jim_Interp *interp);
  40. int arm926ejs_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
  41. int arm926ejs_quit(void);
  42. int arm926ejs_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
  43. static int arm926ejs_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical);
  44. static int arm926ejs_mmu(struct target_s *target, int *enabled);
  45. target_type_t arm926ejs_target =
  46. {
  47. .name = "arm926ejs",
  48. .poll = arm7_9_poll,
  49. .arch_state = arm926ejs_arch_state,
  50. .target_request_data = arm7_9_target_request_data,
  51. .halt = arm7_9_halt,
  52. .resume = arm7_9_resume,
  53. .step = arm7_9_step,
  54. .assert_reset = arm7_9_assert_reset,
  55. .deassert_reset = arm7_9_deassert_reset,
  56. .soft_reset_halt = arm926ejs_soft_reset_halt,
  57. .get_gdb_reg_list = armv4_5_get_gdb_reg_list,
  58. .read_memory = arm7_9_read_memory,
  59. .write_memory = arm926ejs_write_memory,
  60. .bulk_write_memory = arm7_9_bulk_write_memory,
  61. .checksum_memory = arm7_9_checksum_memory,
  62. .blank_check_memory = arm7_9_blank_check_memory,
  63. .run_algorithm = armv4_5_run_algorithm,
  64. .add_breakpoint = arm7_9_add_breakpoint,
  65. .remove_breakpoint = arm7_9_remove_breakpoint,
  66. .add_watchpoint = arm7_9_add_watchpoint,
  67. .remove_watchpoint = arm7_9_remove_watchpoint,
  68. .register_commands = arm926ejs_register_commands,
  69. .target_create = arm926ejs_target_create,
  70. .init_target = arm926ejs_init_target,
  71. .examine = arm9tdmi_examine,
  72. .quit = arm926ejs_quit,
  73. .virt2phys = arm926ejs_virt2phys,
  74. .mmu = arm926ejs_mmu
  75. };
  76. int arm926ejs_catch_broken_irscan(uint8_t *captured, void *priv, scan_field_t *field)
  77. {
  78. /* FIX!!!! this code should be reenabled. For now it does not check
  79. * the queue...*/
  80. return 0;
  81. #if 0
  82. /* The ARM926EJ-S' instruction register is 4 bits wide */
  83. uint8_t t = *captured & 0xf;
  84. uint8_t t2 = *field->in_check_value & 0xf;
  85. if (t == t2)
  86. {
  87. return ERROR_OK;
  88. }
  89. else if ((t == 0x0f) || (t == 0x00))
  90. {
  91. LOG_DEBUG("caught ARM926EJ-S invalid Capture-IR result after CP15 access");
  92. return ERROR_OK;
  93. }
  94. return ERROR_JTAG_QUEUE_FAILED;;
  95. #endif
  96. }
  97. #define ARM926EJS_CP15_ADDR(opcode_1, opcode_2, CRn, CRm) ((opcode_1 << 11) | (opcode_2 << 8) | (CRn << 4) | (CRm << 0))
  98. int arm926ejs_cp15_read(target_t *target, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
  99. {
  100. int retval = ERROR_OK;
  101. armv4_5_common_t *armv4_5 = target->arch_info;
  102. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  103. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  104. uint32_t address = ARM926EJS_CP15_ADDR(op1, op2, CRn, CRm);
  105. scan_field_t fields[4];
  106. uint8_t address_buf[2];
  107. uint8_t nr_w_buf = 0;
  108. uint8_t access = 1;
  109. buf_set_u32(address_buf, 0, 14, address);
  110. jtag_set_end_state(TAP_IDLE);
  111. if ((retval = arm_jtag_scann(jtag_info, 0xf)) != ERROR_OK)
  112. {
  113. return retval;
  114. }
  115. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  116. fields[0].tap = jtag_info->tap;
  117. fields[0].num_bits = 32;
  118. fields[0].out_value = NULL;
  119. fields[0].in_value = (uint8_t *)value;
  120. fields[1].tap = jtag_info->tap;
  121. fields[1].num_bits = 1;
  122. fields[1].out_value = &access;
  123. fields[1].in_value = &access;
  124. fields[2].tap = jtag_info->tap;
  125. fields[2].num_bits = 14;
  126. fields[2].out_value = address_buf;
  127. fields[2].in_value = NULL;
  128. fields[3].tap = jtag_info->tap;
  129. fields[3].num_bits = 1;
  130. fields[3].out_value = &nr_w_buf;
  131. fields[3].in_value = NULL;
  132. jtag_add_dr_scan(4, fields, jtag_get_end_state());
  133. long long then = timeval_ms();
  134. for (;;)
  135. {
  136. /* rescan with NOP, to wait for the access to complete */
  137. access = 0;
  138. nr_w_buf = 0;
  139. jtag_add_dr_scan(4, fields, jtag_get_end_state());
  140. jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)value);
  141. if ((retval = jtag_execute_queue()) != ERROR_OK)
  142. {
  143. return retval;
  144. }
  145. if (buf_get_u32(&access, 0, 1) == 1)
  146. {
  147. break;
  148. }
  149. /* 10ms timeout */
  150. if ((timeval_ms()-then)>10)
  151. {
  152. LOG_ERROR("cp15 read operation timed out");
  153. return ERROR_FAIL;
  154. }
  155. }
  156. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  157. LOG_DEBUG("addr: 0x%x value: %8.8x", address, *value);
  158. #endif
  159. arm_jtag_set_instr(jtag_info, 0xc, &arm926ejs_catch_broken_irscan);
  160. return ERROR_OK;
  161. }
  162. int arm926ejs_cp15_write(target_t *target, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
  163. {
  164. int retval = ERROR_OK;
  165. armv4_5_common_t *armv4_5 = target->arch_info;
  166. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  167. arm_jtag_t *jtag_info = &arm7_9->jtag_info;
  168. uint32_t address = ARM926EJS_CP15_ADDR(op1, op2, CRn, CRm);
  169. scan_field_t fields[4];
  170. uint8_t value_buf[4];
  171. uint8_t address_buf[2];
  172. uint8_t nr_w_buf = 1;
  173. uint8_t access = 1;
  174. buf_set_u32(address_buf, 0, 14, address);
  175. buf_set_u32(value_buf, 0, 32, value);
  176. jtag_set_end_state(TAP_IDLE);
  177. if ((retval = arm_jtag_scann(jtag_info, 0xf)) != ERROR_OK)
  178. {
  179. return retval;
  180. }
  181. arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
  182. fields[0].tap = jtag_info->tap;
  183. fields[0].num_bits = 32;
  184. fields[0].out_value = value_buf;
  185. fields[0].in_value = NULL;
  186. fields[1].tap = jtag_info->tap;
  187. fields[1].num_bits = 1;
  188. fields[1].out_value = &access;
  189. fields[1].in_value = &access;
  190. fields[2].tap = jtag_info->tap;
  191. fields[2].num_bits = 14;
  192. fields[2].out_value = address_buf;
  193. fields[2].in_value = NULL;
  194. fields[3].tap = jtag_info->tap;
  195. fields[3].num_bits = 1;
  196. fields[3].out_value = &nr_w_buf;
  197. fields[3].in_value = NULL;
  198. jtag_add_dr_scan(4, fields, jtag_get_end_state());
  199. long long then = timeval_ms();
  200. for (;;)
  201. {
  202. /* rescan with NOP, to wait for the access to complete */
  203. access = 0;
  204. nr_w_buf = 0;
  205. jtag_add_dr_scan(4, fields, jtag_get_end_state());
  206. if ((retval = jtag_execute_queue()) != ERROR_OK)
  207. {
  208. return retval;
  209. }
  210. if (buf_get_u32(&access, 0, 1) == 1)
  211. {
  212. break;
  213. }
  214. /* 10ms timeout */
  215. if ((timeval_ms()-then)>10)
  216. {
  217. LOG_ERROR("cp15 write operation timed out");
  218. return ERROR_FAIL;
  219. }
  220. }
  221. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  222. LOG_DEBUG("addr: 0x%x value: %8.8x", address, value);
  223. #endif
  224. arm_jtag_set_instr(jtag_info, 0xf, &arm926ejs_catch_broken_irscan);
  225. return ERROR_OK;
  226. }
  227. static int arm926ejs_examine_debug_reason(target_t *target)
  228. {
  229. armv4_5_common_t *armv4_5 = target->arch_info;
  230. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  231. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  232. int debug_reason;
  233. int retval;
  234. embeddedice_read_reg(dbg_stat);
  235. if ((retval = jtag_execute_queue()) != ERROR_OK)
  236. return retval;
  237. /* Method-Of-Entry (MOE) field */
  238. debug_reason = buf_get_u32(dbg_stat->value, 6, 4);
  239. switch (debug_reason)
  240. {
  241. case 0:
  242. LOG_DEBUG("no *NEW* debug entry (?missed one?)");
  243. /* ... since last restart or debug reset ... */
  244. target->debug_reason = DBG_REASON_DBGRQ;
  245. break;
  246. case 1:
  247. LOG_DEBUG("breakpoint from EICE unit 0");
  248. target->debug_reason = DBG_REASON_BREAKPOINT;
  249. break;
  250. case 2:
  251. LOG_DEBUG("breakpoint from EICE unit 1");
  252. target->debug_reason = DBG_REASON_BREAKPOINT;
  253. break;
  254. case 3:
  255. LOG_DEBUG("soft breakpoint (BKPT instruction)");
  256. target->debug_reason = DBG_REASON_BREAKPOINT;
  257. break;
  258. case 4:
  259. LOG_DEBUG("vector catch breakpoint");
  260. target->debug_reason = DBG_REASON_BREAKPOINT;
  261. break;
  262. case 5:
  263. LOG_DEBUG("external breakpoint");
  264. target->debug_reason = DBG_REASON_BREAKPOINT;
  265. break;
  266. case 6:
  267. LOG_DEBUG("watchpoint from EICE unit 0");
  268. target->debug_reason = DBG_REASON_WATCHPOINT;
  269. break;
  270. case 7:
  271. LOG_DEBUG("watchpoint from EICE unit 1");
  272. target->debug_reason = DBG_REASON_WATCHPOINT;
  273. break;
  274. case 8:
  275. LOG_DEBUG("external watchpoint");
  276. target->debug_reason = DBG_REASON_WATCHPOINT;
  277. break;
  278. case 9:
  279. LOG_DEBUG("internal debug request");
  280. target->debug_reason = DBG_REASON_DBGRQ;
  281. break;
  282. case 10:
  283. LOG_DEBUG("external debug request");
  284. target->debug_reason = DBG_REASON_DBGRQ;
  285. break;
  286. case 11:
  287. LOG_DEBUG("debug re-entry from system speed access");
  288. /* This is normal when connecting to something that's
  289. * already halted, or in some related code paths, but
  290. * otherwise is surprising (and presumably wrong).
  291. */
  292. switch (target->debug_reason) {
  293. case DBG_REASON_DBGRQ:
  294. break;
  295. default:
  296. LOG_ERROR("unexpected -- debug re-entry");
  297. /* FALLTHROUGH */
  298. case DBG_REASON_UNDEFINED:
  299. target->debug_reason = DBG_REASON_DBGRQ;
  300. break;
  301. }
  302. break;
  303. case 12:
  304. /* FIX!!!! here be dragons!!! We need to fail here so
  305. * the target will interpreted as halted but we won't
  306. * try to talk to it right now... a resume + halt seems
  307. * to sync things up again. Please send an email to
  308. * openocd development mailing list if you have hardware
  309. * to donate to look into this problem....
  310. */
  311. LOG_WARNING("WARNING: mystery debug reason MOE = 0xc. Try issuing a resume + halt.");
  312. target->debug_reason = DBG_REASON_DBGRQ;
  313. break;
  314. default:
  315. LOG_WARNING("WARNING: unknown debug reason: 0x%x", debug_reason);
  316. /* Oh agony! should we interpret this as a halt request or
  317. * that the target stopped on it's own accord?
  318. */
  319. target->debug_reason = DBG_REASON_DBGRQ;
  320. /* if we fail here, we won't talk to the target and it will
  321. * be reported to be in the halted state */
  322. break;
  323. }
  324. return ERROR_OK;
  325. }
  326. uint32_t arm926ejs_get_ttb(target_t *target)
  327. {
  328. armv4_5_common_t *armv4_5 = target->arch_info;
  329. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  330. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  331. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  332. int retval;
  333. uint32_t ttb = 0x0;
  334. if ((retval = arm926ejs->read_cp15(target, 0, 0, 2, 0, &ttb)) != ERROR_OK)
  335. return retval;
  336. return ttb;
  337. }
  338. void arm926ejs_disable_mmu_caches(target_t *target, int mmu, int d_u_cache, int i_cache)
  339. {
  340. armv4_5_common_t *armv4_5 = target->arch_info;
  341. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  342. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  343. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  344. uint32_t cp15_control;
  345. /* read cp15 control register */
  346. arm926ejs->read_cp15(target, 0, 0, 1, 0, &cp15_control);
  347. jtag_execute_queue();
  348. if (mmu)
  349. {
  350. /* invalidate TLB */
  351. arm926ejs->write_cp15(target, 0, 0, 8, 7, 0x0);
  352. cp15_control &= ~0x1U;
  353. }
  354. if (d_u_cache)
  355. {
  356. uint32_t debug_override;
  357. /* read-modify-write CP15 debug override register
  358. * to enable "test and clean all" */
  359. arm926ejs->read_cp15(target, 0, 0, 15, 0, &debug_override);
  360. debug_override |= 0x80000;
  361. arm926ejs->write_cp15(target, 0, 0, 15, 0, debug_override);
  362. /* clean and invalidate DCache */
  363. arm926ejs->write_cp15(target, 0, 0, 7, 5, 0x0);
  364. /* write CP15 debug override register
  365. * to disable "test and clean all" */
  366. debug_override &= ~0x80000;
  367. arm926ejs->write_cp15(target, 0, 0, 15, 0, debug_override);
  368. cp15_control &= ~0x4U;
  369. }
  370. if (i_cache)
  371. {
  372. /* invalidate ICache */
  373. arm926ejs->write_cp15(target, 0, 0, 7, 5, 0x0);
  374. cp15_control &= ~0x1000U;
  375. }
  376. arm926ejs->write_cp15(target, 0, 0, 1, 0, cp15_control);
  377. }
  378. void arm926ejs_enable_mmu_caches(target_t *target, int mmu, int d_u_cache, int i_cache)
  379. {
  380. armv4_5_common_t *armv4_5 = target->arch_info;
  381. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  382. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  383. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  384. uint32_t cp15_control;
  385. /* read cp15 control register */
  386. arm926ejs->read_cp15(target, 0, 0, 1, 0, &cp15_control);
  387. jtag_execute_queue();
  388. if (mmu)
  389. cp15_control |= 0x1U;
  390. if (d_u_cache)
  391. cp15_control |= 0x4U;
  392. if (i_cache)
  393. cp15_control |= 0x1000U;
  394. arm926ejs->write_cp15(target, 0, 0, 1, 0, cp15_control);
  395. }
  396. void arm926ejs_post_debug_entry(target_t *target)
  397. {
  398. armv4_5_common_t *armv4_5 = target->arch_info;
  399. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  400. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  401. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  402. /* examine cp15 control reg */
  403. arm926ejs->read_cp15(target, 0, 0, 1, 0, &arm926ejs->cp15_control_reg);
  404. jtag_execute_queue();
  405. LOG_DEBUG("cp15_control_reg: %8.8" PRIx32 "", arm926ejs->cp15_control_reg);
  406. if (arm926ejs->armv4_5_mmu.armv4_5_cache.ctype == -1)
  407. {
  408. uint32_t cache_type_reg;
  409. /* identify caches */
  410. arm926ejs->read_cp15(target, 0, 1, 0, 0, &cache_type_reg);
  411. jtag_execute_queue();
  412. armv4_5_identify_cache(cache_type_reg, &arm926ejs->armv4_5_mmu.armv4_5_cache);
  413. }
  414. arm926ejs->armv4_5_mmu.mmu_enabled = (arm926ejs->cp15_control_reg & 0x1U) ? 1 : 0;
  415. arm926ejs->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = (arm926ejs->cp15_control_reg & 0x4U) ? 1 : 0;
  416. arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled = (arm926ejs->cp15_control_reg & 0x1000U) ? 1 : 0;
  417. /* save i/d fault status and address register */
  418. arm926ejs->read_cp15(target, 0, 0, 5, 0, &arm926ejs->d_fsr);
  419. arm926ejs->read_cp15(target, 0, 1, 5, 0, &arm926ejs->i_fsr);
  420. arm926ejs->read_cp15(target, 0, 0, 6, 0, &arm926ejs->d_far);
  421. LOG_DEBUG("D FSR: 0x%8.8" PRIx32 ", D FAR: 0x%8.8" PRIx32 ", I FSR: 0x%8.8" PRIx32 "",
  422. arm926ejs->d_fsr, arm926ejs->d_far, arm926ejs->i_fsr);
  423. uint32_t cache_dbg_ctrl;
  424. /* read-modify-write CP15 cache debug control register
  425. * to disable I/D-cache linefills and force WT */
  426. arm926ejs->read_cp15(target, 7, 0, 15, 0, &cache_dbg_ctrl);
  427. cache_dbg_ctrl |= 0x7;
  428. arm926ejs->write_cp15(target, 7, 0, 15, 0, cache_dbg_ctrl);
  429. }
  430. void arm926ejs_pre_restore_context(target_t *target)
  431. {
  432. armv4_5_common_t *armv4_5 = target->arch_info;
  433. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  434. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  435. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  436. /* restore i/d fault status and address register */
  437. arm926ejs->write_cp15(target, 0, 0, 5, 0, arm926ejs->d_fsr);
  438. arm926ejs->write_cp15(target, 0, 1, 5, 0, arm926ejs->i_fsr);
  439. arm926ejs->write_cp15(target, 0, 0, 6, 0, arm926ejs->d_far);
  440. uint32_t cache_dbg_ctrl;
  441. /* read-modify-write CP15 cache debug control register
  442. * to reenable I/D-cache linefills and disable WT */
  443. arm926ejs->read_cp15(target, 7, 0, 15, 0, &cache_dbg_ctrl);
  444. cache_dbg_ctrl &= ~0x7;
  445. arm926ejs->write_cp15(target, 7, 0, 15, 0, cache_dbg_ctrl);
  446. }
  447. int arm926ejs_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p, arm9tdmi_common_t **arm9tdmi_p, arm926ejs_common_t **arm926ejs_p)
  448. {
  449. armv4_5_common_t *armv4_5 = target->arch_info;
  450. arm7_9_common_t *arm7_9;
  451. arm9tdmi_common_t *arm9tdmi;
  452. arm926ejs_common_t *arm926ejs;
  453. if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
  454. {
  455. return -1;
  456. }
  457. arm7_9 = armv4_5->arch_info;
  458. if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
  459. {
  460. return -1;
  461. }
  462. arm9tdmi = arm7_9->arch_info;
  463. if (arm9tdmi->common_magic != ARM9TDMI_COMMON_MAGIC)
  464. {
  465. return -1;
  466. }
  467. arm926ejs = arm9tdmi->arch_info;
  468. if (arm926ejs->common_magic != ARM926EJS_COMMON_MAGIC)
  469. {
  470. return -1;
  471. }
  472. *armv4_5_p = armv4_5;
  473. *arm7_9_p = arm7_9;
  474. *arm9tdmi_p = arm9tdmi;
  475. *arm926ejs_p = arm926ejs;
  476. return ERROR_OK;
  477. }
  478. int arm926ejs_arch_state(struct target_s *target)
  479. {
  480. armv4_5_common_t *armv4_5 = target->arch_info;
  481. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  482. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  483. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  484. char *state[] =
  485. {
  486. "disabled", "enabled"
  487. };
  488. if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
  489. {
  490. LOG_ERROR("BUG: called for a non-ARMv4/5 target");
  491. exit(-1);
  492. }
  493. LOG_USER(
  494. "target halted in %s state due to %s, current mode: %s\n"
  495. "cpsr: 0x%8.8" PRIx32 " pc: 0x%8.8" PRIx32 "\n"
  496. "MMU: %s, D-Cache: %s, I-Cache: %s",
  497. armv4_5_state_strings[armv4_5->core_state],
  498. Jim_Nvp_value2name_simple(nvp_target_debug_reason,target->debug_reason)->name,
  499. armv4_5_mode_strings[armv4_5_mode_to_number(armv4_5->core_mode)],
  500. buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32),
  501. buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32),
  502. state[arm926ejs->armv4_5_mmu.mmu_enabled],
  503. state[arm926ejs->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled],
  504. state[arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled]);
  505. return ERROR_OK;
  506. }
  507. int arm926ejs_soft_reset_halt(struct target_s *target)
  508. {
  509. int retval = ERROR_OK;
  510. armv4_5_common_t *armv4_5 = target->arch_info;
  511. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  512. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  513. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  514. reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
  515. if ((retval = target_halt(target)) != ERROR_OK)
  516. {
  517. return retval;
  518. }
  519. long long then = timeval_ms();
  520. int timeout;
  521. while (!(timeout = ((timeval_ms()-then) > 1000)))
  522. {
  523. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0)
  524. {
  525. embeddedice_read_reg(dbg_stat);
  526. if ((retval = jtag_execute_queue()) != ERROR_OK)
  527. {
  528. return retval;
  529. }
  530. } else
  531. {
  532. break;
  533. }
  534. if (debug_level >= 1)
  535. {
  536. /* do not eat all CPU, time out after 1 se*/
  537. alive_sleep(100);
  538. } else
  539. {
  540. keep_alive();
  541. }
  542. }
  543. if (timeout)
  544. {
  545. LOG_ERROR("Failed to halt CPU after 1 sec");
  546. return ERROR_TARGET_TIMEOUT;
  547. }
  548. target->state = TARGET_HALTED;
  549. /* SVC, ARM state, IRQ and FIQ disabled */
  550. buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8, 0xd3);
  551. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 1;
  552. armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
  553. /* start fetching from 0x0 */
  554. buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, 0x0);
  555. armv4_5->core_cache->reg_list[15].dirty = 1;
  556. armv4_5->core_cache->reg_list[15].valid = 1;
  557. armv4_5->core_mode = ARMV4_5_MODE_SVC;
  558. armv4_5->core_state = ARMV4_5_STATE_ARM;
  559. arm926ejs_disable_mmu_caches(target, 1, 1, 1);
  560. arm926ejs->armv4_5_mmu.mmu_enabled = 0;
  561. arm926ejs->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = 0;
  562. arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled = 0;
  563. return target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  564. }
  565. int arm926ejs_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  566. {
  567. int retval;
  568. armv4_5_common_t *armv4_5 = target->arch_info;
  569. arm7_9_common_t *arm7_9 = armv4_5->arch_info;
  570. arm9tdmi_common_t *arm9tdmi = arm7_9->arch_info;
  571. arm926ejs_common_t *arm926ejs = arm9tdmi->arch_info;
  572. if ((retval = arm7_9_write_memory(target, address, size, count, buffer)) != ERROR_OK)
  573. return retval;
  574. /* If ICache is enabled, we have to invalidate affected ICache lines
  575. * the DCache is forced to write-through, so we don't have to clean it here
  576. */
  577. if (arm926ejs->armv4_5_mmu.armv4_5_cache.i_cache_enabled)
  578. {
  579. if (count <= 1)
  580. {
  581. /* invalidate ICache single entry with MVA */
  582. arm926ejs->write_cp15(target, 0, 1, 7, 5, address);
  583. }
  584. else
  585. {
  586. /* invalidate ICache */
  587. arm926ejs->write_cp15(target, 0, 0, 7, 5, address);
  588. }
  589. }
  590. return retval;
  591. }
  592. int arm926ejs_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
  593. {
  594. arm9tdmi_init_target(cmd_ctx, target);
  595. return ERROR_OK;
  596. }
  597. int arm926ejs_quit(void)
  598. {
  599. return ERROR_OK;
  600. }
  601. int arm926ejs_init_arch_info(target_t *target, arm926ejs_common_t *arm926ejs, jtag_tap_t *tap)
  602. {
  603. arm9tdmi_common_t *arm9tdmi = &arm926ejs->arm9tdmi_common;
  604. arm7_9_common_t *arm7_9 = &arm9tdmi->arm7_9_common;
  605. /* initialize arm9tdmi specific info (including arm7_9 and armv4_5)
  606. */
  607. arm9tdmi_init_arch_info(target, arm9tdmi, tap);
  608. arm9tdmi->arch_info = arm926ejs;
  609. arm926ejs->common_magic = ARM926EJS_COMMON_MAGIC;
  610. arm7_9->post_debug_entry = arm926ejs_post_debug_entry;
  611. arm7_9->pre_restore_context = arm926ejs_pre_restore_context;
  612. arm926ejs->read_cp15 = arm926ejs_cp15_read;
  613. arm926ejs->write_cp15 = arm926ejs_cp15_write;
  614. arm926ejs->armv4_5_mmu.armv4_5_cache.ctype = -1;
  615. arm926ejs->armv4_5_mmu.get_ttb = arm926ejs_get_ttb;
  616. arm926ejs->armv4_5_mmu.read_memory = arm7_9_read_memory;
  617. arm926ejs->armv4_5_mmu.write_memory = arm7_9_write_memory;
  618. arm926ejs->armv4_5_mmu.disable_mmu_caches = arm926ejs_disable_mmu_caches;
  619. arm926ejs->armv4_5_mmu.enable_mmu_caches = arm926ejs_enable_mmu_caches;
  620. arm926ejs->armv4_5_mmu.has_tiny_pages = 1;
  621. arm926ejs->armv4_5_mmu.mmu_enabled = 0;
  622. arm7_9->examine_debug_reason = arm926ejs_examine_debug_reason;
  623. /* The ARM926EJ-S implements the ARMv5TE architecture which
  624. * has the BKPT instruction, so we don't have to use a watchpoint comparator
  625. */
  626. arm7_9->arm_bkpt = ARMV5_BKPT(0x0);
  627. arm7_9->thumb_bkpt = ARMV5_T_BKPT(0x0) & 0xffff;
  628. return ERROR_OK;
  629. }
  630. int arm926ejs_target_create(struct target_s *target, Jim_Interp *interp)
  631. {
  632. arm926ejs_common_t *arm926ejs = calloc(1,sizeof(arm926ejs_common_t));
  633. arm926ejs_init_arch_info(target, arm926ejs, target->tap);
  634. return ERROR_OK;
  635. }
  636. int arm926ejs_register_commands(struct command_context_s *cmd_ctx)
  637. {
  638. int retval;
  639. command_t *arm926ejs_cmd;
  640. retval = arm9tdmi_register_commands(cmd_ctx);
  641. arm926ejs_cmd = register_command(cmd_ctx, NULL, "arm926ejs", NULL, COMMAND_ANY, "arm926ejs specific commands");
  642. register_command(cmd_ctx, arm926ejs_cmd, "cp15", arm926ejs_handle_cp15_command, COMMAND_EXEC, "display/modify cp15 register <opcode_1> <opcode_2> <CRn> <CRm> [value]");
  643. register_command(cmd_ctx, arm926ejs_cmd, "cache_info", arm926ejs_handle_cache_info_command, COMMAND_EXEC, "display information about target caches");
  644. register_command(cmd_ctx, arm926ejs_cmd, "virt2phys", arm926ejs_handle_virt2phys_command, COMMAND_EXEC, "translate va to pa <va>");
  645. register_command(cmd_ctx, arm926ejs_cmd, "mdw_phys", arm926ejs_handle_md_phys_command, COMMAND_EXEC, "display memory words <physical addr> [count]");
  646. register_command(cmd_ctx, arm926ejs_cmd, "mdh_phys", arm926ejs_handle_md_phys_command, COMMAND_EXEC, "display memory half-words <physical addr> [count]");
  647. register_command(cmd_ctx, arm926ejs_cmd, "mdb_phys", arm926ejs_handle_md_phys_command, COMMAND_EXEC, "display memory bytes <physical addr> [count]");
  648. register_command(cmd_ctx, arm926ejs_cmd, "mww_phys", arm926ejs_handle_mw_phys_command, COMMAND_EXEC, "write memory word <physical addr> <value>");
  649. register_command(cmd_ctx, arm926ejs_cmd, "mwh_phys", arm926ejs_handle_mw_phys_command, COMMAND_EXEC, "write memory half-word <physical addr> <value>");
  650. register_command(cmd_ctx, arm926ejs_cmd, "mwb_phys", arm926ejs_handle_mw_phys_command, COMMAND_EXEC, "write memory byte <physical addr> <value>");
  651. return retval;
  652. }
  653. int arm926ejs_handle_cp15_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  654. {
  655. int retval;
  656. target_t *target = get_current_target(cmd_ctx);
  657. armv4_5_common_t *armv4_5;
  658. arm7_9_common_t *arm7_9;
  659. arm9tdmi_common_t *arm9tdmi;
  660. arm926ejs_common_t *arm926ejs;
  661. int opcode_1;
  662. int opcode_2;
  663. int CRn;
  664. int CRm;
  665. if ((argc < 4) || (argc > 5))
  666. {
  667. command_print(cmd_ctx, "usage: arm926ejs cp15 <opcode_1> <opcode_2> <CRn> <CRm> [value]");
  668. return ERROR_OK;
  669. }
  670. opcode_1 = strtoul(args[0], NULL, 0);
  671. opcode_2 = strtoul(args[1], NULL, 0);
  672. CRn = strtoul(args[2], NULL, 0);
  673. CRm = strtoul(args[3], NULL, 0);
  674. if (arm926ejs_get_arch_pointers(target, &armv4_5, &arm7_9, &arm9tdmi, &arm926ejs) != ERROR_OK)
  675. {
  676. command_print(cmd_ctx, "current target isn't an ARM926EJ-S target");
  677. return ERROR_OK;
  678. }
  679. if (target->state != TARGET_HALTED)
  680. {
  681. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  682. return ERROR_OK;
  683. }
  684. if (argc == 4)
  685. {
  686. uint32_t value;
  687. if ((retval = arm926ejs->read_cp15(target, opcode_1, opcode_2, CRn, CRm, &value)) != ERROR_OK)
  688. {
  689. command_print(cmd_ctx, "couldn't access register");
  690. return ERROR_OK;
  691. }
  692. if ((retval = jtag_execute_queue()) != ERROR_OK)
  693. {
  694. return retval;
  695. }
  696. command_print(cmd_ctx, "%i %i %i %i: %8.8" PRIx32 "", opcode_1, opcode_2, CRn, CRm, value);
  697. }
  698. else
  699. {
  700. uint32_t value = strtoul(args[4], NULL, 0);
  701. if ((retval = arm926ejs->write_cp15(target, opcode_1, opcode_2, CRn, CRm, value)) != ERROR_OK)
  702. {
  703. command_print(cmd_ctx, "couldn't access register");
  704. return ERROR_OK;
  705. }
  706. command_print(cmd_ctx, "%i %i %i %i: %8.8" PRIx32 "", opcode_1, opcode_2, CRn, CRm, value);
  707. }
  708. return ERROR_OK;
  709. }
  710. int arm926ejs_handle_cache_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  711. {
  712. target_t *target = get_current_target(cmd_ctx);
  713. armv4_5_common_t *armv4_5;
  714. arm7_9_common_t *arm7_9;
  715. arm9tdmi_common_t *arm9tdmi;
  716. arm926ejs_common_t *arm926ejs;
  717. if (arm926ejs_get_arch_pointers(target, &armv4_5, &arm7_9, &arm9tdmi, &arm926ejs) != ERROR_OK)
  718. {
  719. command_print(cmd_ctx, "current target isn't an ARM926EJ-S target");
  720. return ERROR_OK;
  721. }
  722. return armv4_5_handle_cache_info_command(cmd_ctx, &arm926ejs->armv4_5_mmu.armv4_5_cache);
  723. }
  724. int arm926ejs_handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
  725. {
  726. target_t *target = get_current_target(cmd_ctx);
  727. armv4_5_common_t *armv4_5;
  728. arm7_9_common_t *arm7_9;
  729. arm9tdmi_common_t *arm9tdmi;
  730. arm926ejs_common_t *arm926ejs;
  731. arm_jtag_t *jtag_info;
  732. if (arm926ejs_get_arch_pointers(target, &armv4_5, &arm7_9, &arm9tdmi, &arm926ejs) != ERROR_OK)
  733. {
  734. command_print(cmd_ctx, "current target isn't an ARM926EJ-S target");
  735. return ERROR_OK;
  736. }
  737. jtag_info = &arm7_9->jtag_info;
  738. if (target->state != TARGET_HALTED)
  739. {
  740. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  741. return ERROR_OK;
  742. }
  743. return armv4_5_mmu_handle_virt2phys_command(cmd_ctx, cmd, args, argc, target, &arm926ejs->armv4_5_mmu);
  744. }
  745. int arm926ejs_handle_md_phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
  746. {
  747. target_t *target = get_current_target(cmd_ctx);
  748. armv4_5_common_t *armv4_5;
  749. arm7_9_common_t *arm7_9;
  750. arm9tdmi_common_t *arm9tdmi;
  751. arm926ejs_common_t *arm926ejs;
  752. arm_jtag_t *jtag_info;
  753. if (arm926ejs_get_arch_pointers(target, &armv4_5, &arm7_9, &arm9tdmi, &arm926ejs) != ERROR_OK)
  754. {
  755. command_print(cmd_ctx, "current target isn't an ARM926EJ-S target");
  756. return ERROR_OK;
  757. }
  758. jtag_info = &arm7_9->jtag_info;
  759. if (target->state != TARGET_HALTED)
  760. {
  761. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  762. return ERROR_OK;
  763. }
  764. return armv4_5_mmu_handle_md_phys_command(cmd_ctx, cmd, args, argc, target, &arm926ejs->armv4_5_mmu);
  765. }
  766. int arm926ejs_handle_mw_phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
  767. {
  768. target_t *target = get_current_target(cmd_ctx);
  769. armv4_5_common_t *armv4_5;
  770. arm7_9_common_t *arm7_9;
  771. arm9tdmi_common_t *arm9tdmi;
  772. arm926ejs_common_t *arm926ejs;
  773. arm_jtag_t *jtag_info;
  774. if (arm926ejs_get_arch_pointers(target, &armv4_5, &arm7_9, &arm9tdmi, &arm926ejs) != ERROR_OK)
  775. {
  776. command_print(cmd_ctx, "current target isn't an ARM926EJ-S target");
  777. return ERROR_OK;
  778. }
  779. jtag_info = &arm7_9->jtag_info;
  780. if (target->state != TARGET_HALTED)
  781. {
  782. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  783. return ERROR_OK;
  784. }
  785. return armv4_5_mmu_handle_mw_phys_command(cmd_ctx, cmd, args, argc, target, &arm926ejs->armv4_5_mmu);
  786. }
  787. static int arm926ejs_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical)
  788. {
  789. int retval;
  790. int type;
  791. uint32_t cb;
  792. int domain;
  793. uint32_t ap;
  794. armv4_5_common_t *armv4_5;
  795. arm7_9_common_t *arm7_9;
  796. arm9tdmi_common_t *arm9tdmi;
  797. arm926ejs_common_t *arm926ejs;
  798. retval= arm926ejs_get_arch_pointers(target, &armv4_5, &arm7_9, &arm9tdmi, &arm926ejs);
  799. if (retval != ERROR_OK)
  800. {
  801. return retval;
  802. }
  803. uint32_t ret = armv4_5_mmu_translate_va(target, &arm926ejs->armv4_5_mmu, virtual, &type, &cb, &domain, &ap);
  804. if (type == -1)
  805. {
  806. return ret;
  807. }
  808. *physical = ret;
  809. return ERROR_OK;
  810. }
  811. static int arm926ejs_mmu(struct target_s *target, int *enabled)
  812. {
  813. armv4_5_common_t *armv4_5 = target->arch_info;
  814. arm926ejs_common_t *arm926ejs = armv4_5->arch_info;
  815. if (target->state != TARGET_HALTED)
  816. {
  817. LOG_ERROR("Target not halted");
  818. return ERROR_TARGET_INVALID;
  819. }
  820. *enabled = arm926ejs->armv4_5_mmu.mmu_enabled;
  821. return ERROR_OK;
  822. }