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.
 
 
 
 
 
 

592 lines
16 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2009 by Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "arm720t.h"
  27. #include <helper/time_support.h>
  28. #include "target_type.h"
  29. #include "register.h"
  30. #include "arm_opcodes.h"
  31. /*
  32. * ARM720 is an ARM7TDMI-S with MMU and ETM7. For information, see
  33. * ARM DDI 0229C especially Chapter 9 about debug support.
  34. */
  35. #if 0
  36. #define _DEBUG_INSTRUCTION_EXECUTION_
  37. #endif
  38. static int arm720t_scan_cp15(struct target *target,
  39. uint32_t out, uint32_t *in, int instruction, int clock_arg)
  40. {
  41. int retval;
  42. struct arm720t_common *arm720t = target_to_arm720(target);
  43. struct arm_jtag *jtag_info;
  44. struct scan_field fields[2];
  45. uint8_t out_buf[4];
  46. uint8_t instruction_buf = instruction;
  47. jtag_info = &arm720t->arm7_9_common.jtag_info;
  48. buf_set_u32(out_buf, 0, 32, flip_u32(out, 32));
  49. retval = arm_jtag_scann(jtag_info, 0xf, TAP_DRPAUSE);
  50. if (retval != ERROR_OK)
  51. return retval;
  52. retval = arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL, TAP_DRPAUSE);
  53. if (retval != ERROR_OK)
  54. return retval;
  55. fields[0].num_bits = 1;
  56. fields[0].out_value = &instruction_buf;
  57. fields[0].in_value = NULL;
  58. fields[1].num_bits = 32;
  59. fields[1].out_value = out_buf;
  60. fields[1].in_value = NULL;
  61. if (in) {
  62. fields[1].in_value = (uint8_t *)in;
  63. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_DRPAUSE);
  64. jtag_add_callback(arm7flip32, (jtag_callback_data_t)in);
  65. } else
  66. jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_DRPAUSE);
  67. if (clock_arg)
  68. jtag_add_runtest(0, TAP_DRPAUSE);
  69. #ifdef _DEBUG_INSTRUCTION_EXECUTION_
  70. retval = jtag_execute_queue();
  71. if (retval != ERROR_OK)
  72. return retval;
  73. if (in)
  74. LOG_DEBUG("out: %8.8x, in: %8.8x, instruction: %i, clock: %i", out, *in, instruction, clock);
  75. else
  76. LOG_DEBUG("out: %8.8x, instruction: %i, clock: %i", out, instruction, clock_arg);
  77. #else
  78. LOG_DEBUG("out: %8.8" PRIx32 ", instruction: %i, clock: %i", out, instruction, clock_arg);
  79. #endif
  80. return ERROR_OK;
  81. }
  82. static int arm720t_read_cp15(struct target *target, uint32_t opcode, uint32_t *value)
  83. {
  84. /* fetch CP15 opcode */
  85. arm720t_scan_cp15(target, opcode, NULL, 1, 1);
  86. /* "DECODE" stage */
  87. arm720t_scan_cp15(target, ARMV4_5_NOP, NULL, 1, 1);
  88. /* "EXECUTE" stage (1) */
  89. arm720t_scan_cp15(target, ARMV4_5_NOP, NULL, 1, 0);
  90. arm720t_scan_cp15(target, 0x0, NULL, 0, 1);
  91. /* "EXECUTE" stage (2) */
  92. arm720t_scan_cp15(target, 0x0, NULL, 0, 1);
  93. /* "EXECUTE" stage (3), CDATA is read */
  94. arm720t_scan_cp15(target, ARMV4_5_NOP, value, 1, 1);
  95. return ERROR_OK;
  96. }
  97. static int arm720t_write_cp15(struct target *target, uint32_t opcode, uint32_t value)
  98. {
  99. /* fetch CP15 opcode */
  100. arm720t_scan_cp15(target, opcode, NULL, 1, 1);
  101. /* "DECODE" stage */
  102. arm720t_scan_cp15(target, ARMV4_5_NOP, NULL, 1, 1);
  103. /* "EXECUTE" stage (1) */
  104. arm720t_scan_cp15(target, ARMV4_5_NOP, NULL, 1, 0);
  105. arm720t_scan_cp15(target, 0x0, NULL, 0, 1);
  106. /* "EXECUTE" stage (2) */
  107. arm720t_scan_cp15(target, value, NULL, 0, 1);
  108. arm720t_scan_cp15(target, ARMV4_5_NOP, NULL, 1, 1);
  109. return ERROR_OK;
  110. }
  111. static int arm720t_get_ttb(struct target *target, uint32_t *result)
  112. {
  113. uint32_t ttb = 0x0;
  114. int retval;
  115. retval = arm720t_read_cp15(target, 0xee120f10, &ttb);
  116. if (retval != ERROR_OK)
  117. return retval;
  118. retval = jtag_execute_queue();
  119. if (retval != ERROR_OK)
  120. return retval;
  121. ttb &= 0xffffc000;
  122. *result = ttb;
  123. return ERROR_OK;
  124. }
  125. static int arm720t_disable_mmu_caches(struct target *target,
  126. int mmu, int d_u_cache, int i_cache)
  127. {
  128. uint32_t cp15_control;
  129. int retval;
  130. /* read cp15 control register */
  131. retval = arm720t_read_cp15(target, 0xee110f10, &cp15_control);
  132. if (retval != ERROR_OK)
  133. return retval;
  134. retval = jtag_execute_queue();
  135. if (retval != ERROR_OK)
  136. return retval;
  137. if (mmu)
  138. cp15_control &= ~0x1U;
  139. if (d_u_cache || i_cache)
  140. cp15_control &= ~0x4U;
  141. retval = arm720t_write_cp15(target, 0xee010f10, cp15_control);
  142. return retval;
  143. }
  144. static int arm720t_enable_mmu_caches(struct target *target,
  145. int mmu, int d_u_cache, int i_cache)
  146. {
  147. uint32_t cp15_control;
  148. int retval;
  149. /* read cp15 control register */
  150. retval = arm720t_read_cp15(target, 0xee110f10, &cp15_control);
  151. if (retval != ERROR_OK)
  152. return retval;
  153. retval = jtag_execute_queue();
  154. if (retval != ERROR_OK)
  155. return retval;
  156. if (mmu)
  157. cp15_control |= 0x1U;
  158. if (d_u_cache || i_cache)
  159. cp15_control |= 0x4U;
  160. retval = arm720t_write_cp15(target, 0xee010f10, cp15_control);
  161. return retval;
  162. }
  163. static int arm720t_post_debug_entry(struct target *target)
  164. {
  165. struct arm720t_common *arm720t = target_to_arm720(target);
  166. int retval;
  167. /* examine cp15 control reg */
  168. retval = arm720t_read_cp15(target, 0xee110f10, &arm720t->cp15_control_reg);
  169. if (retval != ERROR_OK)
  170. return retval;
  171. retval = jtag_execute_queue();
  172. if (retval != ERROR_OK)
  173. return retval;
  174. LOG_DEBUG("cp15_control_reg: %8.8" PRIx32 "", arm720t->cp15_control_reg);
  175. arm720t->armv4_5_mmu.mmu_enabled = (arm720t->cp15_control_reg & 0x1U) ? 1 : 0;
  176. arm720t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = (arm720t->cp15_control_reg & 0x4U) ? 1 : 0;
  177. arm720t->armv4_5_mmu.armv4_5_cache.i_cache_enabled = 0;
  178. /* save i/d fault status and address register */
  179. retval = arm720t_read_cp15(target, 0xee150f10, &arm720t->fsr_reg);
  180. if (retval != ERROR_OK)
  181. return retval;
  182. retval = arm720t_read_cp15(target, 0xee160f10, &arm720t->far_reg);
  183. if (retval != ERROR_OK)
  184. return retval;
  185. retval = jtag_execute_queue();
  186. return retval;
  187. }
  188. static void arm720t_pre_restore_context(struct target *target)
  189. {
  190. struct arm720t_common *arm720t = target_to_arm720(target);
  191. /* restore i/d fault status and address register */
  192. arm720t_write_cp15(target, 0xee050f10, arm720t->fsr_reg);
  193. arm720t_write_cp15(target, 0xee060f10, arm720t->far_reg);
  194. }
  195. static int arm720t_verify_pointer(struct command_context *cmd_ctx,
  196. struct arm720t_common *arm720t)
  197. {
  198. if (arm720t->common_magic != ARM720T_COMMON_MAGIC) {
  199. command_print(cmd_ctx, "target is not an ARM720");
  200. return ERROR_TARGET_INVALID;
  201. }
  202. return ERROR_OK;
  203. }
  204. static int arm720t_arch_state(struct target *target)
  205. {
  206. struct arm720t_common *arm720t = target_to_arm720(target);
  207. static const char *state[] = {
  208. "disabled", "enabled"
  209. };
  210. arm_arch_state(target);
  211. LOG_USER("MMU: %s, Cache: %s",
  212. state[arm720t->armv4_5_mmu.mmu_enabled],
  213. state[arm720t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled]);
  214. return ERROR_OK;
  215. }
  216. static int arm720_mmu(struct target *target, int *enabled)
  217. {
  218. if (target->state != TARGET_HALTED) {
  219. LOG_ERROR("%s: target not halted", __func__);
  220. return ERROR_TARGET_INVALID;
  221. }
  222. *enabled = target_to_arm720(target)->armv4_5_mmu.mmu_enabled;
  223. return ERROR_OK;
  224. }
  225. static int arm720_virt2phys(struct target *target,
  226. uint32_t virtual, uint32_t *physical)
  227. {
  228. uint32_t cb;
  229. struct arm720t_common *arm720t = target_to_arm720(target);
  230. uint32_t ret;
  231. int retval = armv4_5_mmu_translate_va(target,
  232. &arm720t->armv4_5_mmu, virtual, &cb, &ret);
  233. if (retval != ERROR_OK)
  234. return retval;
  235. *physical = ret;
  236. return ERROR_OK;
  237. }
  238. static int arm720t_read_memory(struct target *target,
  239. uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  240. {
  241. int retval;
  242. struct arm720t_common *arm720t = target_to_arm720(target);
  243. /* disable cache, but leave MMU enabled */
  244. if (arm720t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled) {
  245. retval = arm720t_disable_mmu_caches(target, 0, 1, 0);
  246. if (retval != ERROR_OK)
  247. return retval;
  248. }
  249. retval = arm7_9_read_memory(target, address, size, count, buffer);
  250. if (arm720t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled) {
  251. retval = arm720t_enable_mmu_caches(target, 0, 1, 0);
  252. if (retval != ERROR_OK)
  253. return retval;
  254. }
  255. return retval;
  256. }
  257. static int arm720t_read_phys_memory(struct target *target,
  258. uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  259. {
  260. struct arm720t_common *arm720t = target_to_arm720(target);
  261. return armv4_5_mmu_read_physical(target, &arm720t->armv4_5_mmu, address, size, count, buffer);
  262. }
  263. static int arm720t_write_phys_memory(struct target *target,
  264. uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
  265. {
  266. struct arm720t_common *arm720t = target_to_arm720(target);
  267. return armv4_5_mmu_write_physical(target, &arm720t->armv4_5_mmu, address, size, count, buffer);
  268. }
  269. static int arm720t_soft_reset_halt(struct target *target)
  270. {
  271. int retval = ERROR_OK;
  272. struct arm720t_common *arm720t = target_to_arm720(target);
  273. struct reg *dbg_stat = &arm720t->arm7_9_common
  274. .eice_cache->reg_list[EICE_DBG_STAT];
  275. struct arm *arm = &arm720t->arm7_9_common.arm;
  276. retval = target_halt(target);
  277. if (retval != ERROR_OK)
  278. return retval;
  279. long long then = timeval_ms();
  280. int timeout;
  281. while (!(timeout = ((timeval_ms()-then) > 1000))) {
  282. if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0) {
  283. embeddedice_read_reg(dbg_stat);
  284. retval = jtag_execute_queue();
  285. if (retval != ERROR_OK)
  286. return retval;
  287. } else
  288. break;
  289. if (debug_level >= 3)
  290. alive_sleep(100);
  291. else
  292. keep_alive();
  293. }
  294. if (timeout) {
  295. LOG_ERROR("Failed to halt CPU after 1 sec");
  296. return ERROR_TARGET_TIMEOUT;
  297. }
  298. target->state = TARGET_HALTED;
  299. /* SVC, ARM state, IRQ and FIQ disabled */
  300. uint32_t cpsr;
  301. cpsr = buf_get_u32(arm->cpsr->value, 0, 32);
  302. cpsr &= ~0xff;
  303. cpsr |= 0xd3;
  304. arm_set_cpsr(arm, cpsr);
  305. arm->cpsr->dirty = 1;
  306. /* start fetching from 0x0 */
  307. buf_set_u32(arm->pc->value, 0, 32, 0x0);
  308. arm->pc->dirty = 1;
  309. arm->pc->valid = 1;
  310. retval = arm720t_disable_mmu_caches(target, 1, 1, 1);
  311. if (retval != ERROR_OK)
  312. return retval;
  313. arm720t->armv4_5_mmu.mmu_enabled = 0;
  314. arm720t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = 0;
  315. arm720t->armv4_5_mmu.armv4_5_cache.i_cache_enabled = 0;
  316. retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  317. if (retval != ERROR_OK)
  318. return retval;
  319. return ERROR_OK;
  320. }
  321. static int arm720t_init_target(struct command_context *cmd_ctx, struct target *target)
  322. {
  323. return arm7tdmi_init_target(cmd_ctx, target);
  324. }
  325. /* FIXME remove forward decls */
  326. static int arm720t_mrc(struct target *target, int cpnum,
  327. uint32_t op1, uint32_t op2,
  328. uint32_t CRn, uint32_t CRm,
  329. uint32_t *value);
  330. static int arm720t_mcr(struct target *target, int cpnum,
  331. uint32_t op1, uint32_t op2,
  332. uint32_t CRn, uint32_t CRm,
  333. uint32_t value);
  334. static int arm720t_init_arch_info(struct target *target,
  335. struct arm720t_common *arm720t, struct jtag_tap *tap)
  336. {
  337. struct arm7_9_common *arm7_9 = &arm720t->arm7_9_common;
  338. arm7_9->arm.mrc = arm720t_mrc;
  339. arm7_9->arm.mcr = arm720t_mcr;
  340. arm7tdmi_init_arch_info(target, arm7_9, tap);
  341. arm720t->common_magic = ARM720T_COMMON_MAGIC;
  342. arm7_9->post_debug_entry = arm720t_post_debug_entry;
  343. arm7_9->pre_restore_context = arm720t_pre_restore_context;
  344. arm720t->armv4_5_mmu.armv4_5_cache.ctype = -1;
  345. arm720t->armv4_5_mmu.get_ttb = arm720t_get_ttb;
  346. arm720t->armv4_5_mmu.read_memory = arm7_9_read_memory;
  347. arm720t->armv4_5_mmu.write_memory = arm7_9_write_memory;
  348. arm720t->armv4_5_mmu.disable_mmu_caches = arm720t_disable_mmu_caches;
  349. arm720t->armv4_5_mmu.enable_mmu_caches = arm720t_enable_mmu_caches;
  350. arm720t->armv4_5_mmu.has_tiny_pages = 0;
  351. arm720t->armv4_5_mmu.mmu_enabled = 0;
  352. return ERROR_OK;
  353. }
  354. static int arm720t_target_create(struct target *target, Jim_Interp *interp)
  355. {
  356. struct arm720t_common *arm720t = calloc(1, sizeof(*arm720t));
  357. arm720t->arm7_9_common.arm.is_armv4 = true;
  358. return arm720t_init_arch_info(target, arm720t, target->tap);
  359. }
  360. COMMAND_HANDLER(arm720t_handle_cp15_command)
  361. {
  362. int retval;
  363. struct target *target = get_current_target(CMD_CTX);
  364. struct arm720t_common *arm720t = target_to_arm720(target);
  365. retval = arm720t_verify_pointer(CMD_CTX, arm720t);
  366. if (retval != ERROR_OK)
  367. return retval;
  368. if (target->state != TARGET_HALTED) {
  369. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  370. return ERROR_OK;
  371. }
  372. /* one or more argument, access a single register (write if second argument is given */
  373. if (CMD_ARGC >= 1) {
  374. uint32_t opcode;
  375. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], opcode);
  376. if (CMD_ARGC == 1) {
  377. uint32_t value;
  378. retval = arm720t_read_cp15(target, opcode, &value);
  379. if (retval != ERROR_OK) {
  380. command_print(CMD_CTX, "couldn't access cp15 with opcode 0x%8.8" PRIx32 "", opcode);
  381. return ERROR_OK;
  382. }
  383. retval = jtag_execute_queue();
  384. if (retval != ERROR_OK)
  385. return retval;
  386. command_print(CMD_CTX, "0x%8.8" PRIx32 ": 0x%8.8" PRIx32 "", opcode, value);
  387. } else if (CMD_ARGC == 2) {
  388. uint32_t value;
  389. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  390. retval = arm720t_write_cp15(target, opcode, value);
  391. if (retval != ERROR_OK) {
  392. command_print(CMD_CTX, "couldn't access cp15 with opcode 0x%8.8" PRIx32 "", opcode);
  393. return ERROR_OK;
  394. }
  395. command_print(CMD_CTX, "0x%8.8" PRIx32 ": 0x%8.8" PRIx32 "", opcode, value);
  396. }
  397. }
  398. return ERROR_OK;
  399. }
  400. static int arm720t_mrc(struct target *target, int cpnum,
  401. uint32_t op1, uint32_t op2,
  402. uint32_t CRn, uint32_t CRm,
  403. uint32_t *value)
  404. {
  405. if (cpnum != 15) {
  406. LOG_ERROR("Only cp15 is supported");
  407. return ERROR_FAIL;
  408. }
  409. /* read "to" r0 */
  410. return arm720t_read_cp15(target,
  411. ARMV4_5_MRC(cpnum, op1, 0, CRn, CRm, op2),
  412. value);
  413. }
  414. static int arm720t_mcr(struct target *target, int cpnum,
  415. uint32_t op1, uint32_t op2,
  416. uint32_t CRn, uint32_t CRm,
  417. uint32_t value)
  418. {
  419. if (cpnum != 15) {
  420. LOG_ERROR("Only cp15 is supported");
  421. return ERROR_FAIL;
  422. }
  423. /* write "from" r0 */
  424. return arm720t_write_cp15(target,
  425. ARMV4_5_MCR(cpnum, op1, 0, CRn, CRm, op2),
  426. value);
  427. }
  428. static const struct command_registration arm720t_exec_command_handlers[] = {
  429. {
  430. .name = "cp15",
  431. .handler = arm720t_handle_cp15_command,
  432. .mode = COMMAND_EXEC,
  433. /* prefer using less error-prone "arm mcr" or "arm mrc" */
  434. .help = "display/modify cp15 register using ARM opcode"
  435. " (DEPRECATED)",
  436. .usage = "instruction [value]",
  437. },
  438. COMMAND_REGISTRATION_DONE
  439. };
  440. static const struct command_registration arm720t_command_handlers[] = {
  441. {
  442. .chain = arm7_9_command_handlers,
  443. },
  444. {
  445. .name = "arm720t",
  446. .mode = COMMAND_ANY,
  447. .help = "arm720t command group",
  448. .usage = "",
  449. .chain = arm720t_exec_command_handlers,
  450. },
  451. COMMAND_REGISTRATION_DONE
  452. };
  453. /** Holds methods for ARM720 targets. */
  454. struct target_type arm720t_target = {
  455. .name = "arm720t",
  456. .poll = arm7_9_poll,
  457. .arch_state = arm720t_arch_state,
  458. .halt = arm7_9_halt,
  459. .resume = arm7_9_resume,
  460. .step = arm7_9_step,
  461. .assert_reset = arm7_9_assert_reset,
  462. .deassert_reset = arm7_9_deassert_reset,
  463. .soft_reset_halt = arm720t_soft_reset_halt,
  464. .get_gdb_reg_list = arm_get_gdb_reg_list,
  465. .read_memory = arm720t_read_memory,
  466. .write_memory = arm7_9_write_memory,
  467. .read_phys_memory = arm720t_read_phys_memory,
  468. .write_phys_memory = arm720t_write_phys_memory,
  469. .mmu = arm720_mmu,
  470. .virt2phys = arm720_virt2phys,
  471. .bulk_write_memory = arm7_9_bulk_write_memory,
  472. .checksum_memory = arm_checksum_memory,
  473. .blank_check_memory = arm_blank_check_memory,
  474. .run_algorithm = armv4_5_run_algorithm,
  475. .add_breakpoint = arm7_9_add_breakpoint,
  476. .remove_breakpoint = arm7_9_remove_breakpoint,
  477. .add_watchpoint = arm7_9_add_watchpoint,
  478. .remove_watchpoint = arm7_9_remove_watchpoint,
  479. .commands = arm720t_command_handlers,
  480. .target_create = arm720t_target_create,
  481. .init_target = arm720t_init_target,
  482. .examine = arm7_9_examine,
  483. .check_reset = arm7_9_check_reset,
  484. };