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.
 
 
 
 
 
 

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