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.
 
 
 
 
 
 

982 lines
27 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2008 by Spencer Oliver *
  3. * spen@spen-soft.co.uk *
  4. * *
  5. * Copyright (C) 2008 by David T.L. Wong *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  21. ***************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "mips32.h"
  26. #include "mips_m4k.h"
  27. #include "mips32_dmaacc.h"
  28. #include "target_type.h"
  29. /* cli handling */
  30. /* forward declarations */
  31. int mips_m4k_poll(target_t *target);
  32. int mips_m4k_halt(struct target_s *target);
  33. int mips_m4k_soft_reset_halt(struct target_s *target);
  34. int mips_m4k_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution);
  35. int mips_m4k_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints);
  36. int mips_m4k_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
  37. int mips_m4k_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
  38. int mips_m4k_register_commands(struct command_context_s *cmd_ctx);
  39. int mips_m4k_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
  40. int mips_m4k_target_create(struct target_s *target, Jim_Interp *interp);
  41. int mips_m4k_examine(struct target_s *target);
  42. int mips_m4k_assert_reset(target_t *target);
  43. int mips_m4k_deassert_reset(target_t *target);
  44. int mips_m4k_checksum_memory(target_t *target, uint32_t address, uint32_t size, uint32_t *checksum);
  45. target_type_t mips_m4k_target =
  46. {
  47. .name = "mips_m4k",
  48. .poll = mips_m4k_poll,
  49. .arch_state = mips32_arch_state,
  50. .target_request_data = NULL,
  51. .halt = mips_m4k_halt,
  52. .resume = mips_m4k_resume,
  53. .step = mips_m4k_step,
  54. .assert_reset = mips_m4k_assert_reset,
  55. .deassert_reset = mips_m4k_deassert_reset,
  56. .soft_reset_halt = mips_m4k_soft_reset_halt,
  57. .get_gdb_reg_list = mips32_get_gdb_reg_list,
  58. .read_memory = mips_m4k_read_memory,
  59. .write_memory = mips_m4k_write_memory,
  60. .bulk_write_memory = mips_m4k_bulk_write_memory,
  61. .checksum_memory = mips_m4k_checksum_memory,
  62. .blank_check_memory = NULL,
  63. .run_algorithm = mips32_run_algorithm,
  64. .add_breakpoint = mips_m4k_add_breakpoint,
  65. .remove_breakpoint = mips_m4k_remove_breakpoint,
  66. .add_watchpoint = mips_m4k_add_watchpoint,
  67. .remove_watchpoint = mips_m4k_remove_watchpoint,
  68. .register_commands = mips_m4k_register_commands,
  69. .target_create = mips_m4k_target_create,
  70. .init_target = mips_m4k_init_target,
  71. .examine = mips_m4k_examine,
  72. };
  73. int mips_m4k_examine_debug_reason(target_t *target)
  74. {
  75. uint32_t break_status;
  76. int retval;
  77. if ((target->debug_reason != DBG_REASON_DBGRQ)
  78. && (target->debug_reason != DBG_REASON_SINGLESTEP))
  79. {
  80. /* get info about inst breakpoint support */
  81. if ((retval = target_read_u32(target, EJTAG_IBS, &break_status)) != ERROR_OK)
  82. return retval;
  83. if (break_status & 0x1f)
  84. {
  85. /* we have halted on a breakpoint */
  86. if ((retval = target_write_u32(target, EJTAG_IBS, 0)) != ERROR_OK)
  87. return retval;
  88. target->debug_reason = DBG_REASON_BREAKPOINT;
  89. }
  90. /* get info about data breakpoint support */
  91. if ((retval = target_read_u32(target, 0xFF302000, &break_status)) != ERROR_OK)
  92. return retval;
  93. if (break_status & 0x1f)
  94. {
  95. /* we have halted on a breakpoint */
  96. if ((retval = target_write_u32(target, 0xFF302000, 0)) != ERROR_OK)
  97. return retval;
  98. target->debug_reason = DBG_REASON_WATCHPOINT;
  99. }
  100. }
  101. return ERROR_OK;
  102. }
  103. int mips_m4k_debug_entry(target_t *target)
  104. {
  105. struct mips32_common *mips32 = target->arch_info;
  106. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  107. uint32_t debug_reg;
  108. /* read debug register */
  109. mips_ejtag_read_debug(ejtag_info, &debug_reg);
  110. /* make sure break uit configured */
  111. mips32_configure_break_unit(target);
  112. /* attempt to find halt reason */
  113. mips_m4k_examine_debug_reason(target);
  114. /* clear single step if active */
  115. if (debug_reg & EJTAG_DEBUG_DSS)
  116. {
  117. /* stopped due to single step - clear step bit */
  118. mips_ejtag_config_step(ejtag_info, 0);
  119. }
  120. mips32_save_context(target);
  121. LOG_DEBUG("entered debug state at PC 0x%" PRIx32 ", target->state: %s",
  122. *(uint32_t*)(mips32->core_cache->reg_list[MIPS32_PC].value),
  123. target_state_name(target));
  124. return ERROR_OK;
  125. }
  126. int mips_m4k_poll(target_t *target)
  127. {
  128. int retval;
  129. struct mips32_common *mips32 = target->arch_info;
  130. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  131. uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl;
  132. /* read ejtag control reg */
  133. jtag_set_end_state(TAP_IDLE);
  134. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
  135. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  136. /* clear this bit before handling polling
  137. * as after reset registers will read zero */
  138. if (ejtag_ctrl & EJTAG_CTRL_ROCC)
  139. {
  140. /* we have detected a reset, clear flag
  141. * otherwise ejtag will not work */
  142. jtag_set_end_state(TAP_IDLE);
  143. ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_ROCC;
  144. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
  145. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  146. LOG_DEBUG("Reset Detected");
  147. }
  148. /* check for processor halted */
  149. if (ejtag_ctrl & EJTAG_CTRL_BRKST)
  150. {
  151. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
  152. {
  153. jtag_set_end_state(TAP_IDLE);
  154. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT, NULL);
  155. target->state = TARGET_HALTED;
  156. if ((retval = mips_m4k_debug_entry(target)) != ERROR_OK)
  157. return retval;
  158. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  159. }
  160. else if (target->state == TARGET_DEBUG_RUNNING)
  161. {
  162. target->state = TARGET_HALTED;
  163. if ((retval = mips_m4k_debug_entry(target)) != ERROR_OK)
  164. return retval;
  165. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  166. }
  167. }
  168. else
  169. {
  170. target->state = TARGET_RUNNING;
  171. }
  172. // LOG_DEBUG("ctrl = 0x%08X", ejtag_ctrl);
  173. return ERROR_OK;
  174. }
  175. int mips_m4k_halt(struct target_s *target)
  176. {
  177. struct mips32_common *mips32 = target->arch_info;
  178. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  179. LOG_DEBUG("target->state: %s",
  180. target_state_name(target));
  181. if (target->state == TARGET_HALTED)
  182. {
  183. LOG_DEBUG("target was already halted");
  184. return ERROR_OK;
  185. }
  186. if (target->state == TARGET_UNKNOWN)
  187. {
  188. LOG_WARNING("target was in unknown state when halt was requested");
  189. }
  190. if (target->state == TARGET_RESET)
  191. {
  192. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
  193. {
  194. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  195. return ERROR_TARGET_FAILURE;
  196. }
  197. else
  198. {
  199. /* we came here in a reset_halt or reset_init sequence
  200. * debug entry was already prepared in mips32_prepare_reset_halt()
  201. */
  202. target->debug_reason = DBG_REASON_DBGRQ;
  203. return ERROR_OK;
  204. }
  205. }
  206. /* break processor */
  207. mips_ejtag_enter_debug(ejtag_info);
  208. target->debug_reason = DBG_REASON_DBGRQ;
  209. return ERROR_OK;
  210. }
  211. int mips_m4k_assert_reset(target_t *target)
  212. {
  213. struct mips32_common *mips32 = target->arch_info;
  214. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  215. LOG_DEBUG("target->state: %s",
  216. target_state_name(target));
  217. enum reset_types jtag_reset_config = jtag_get_reset_config();
  218. if (!(jtag_reset_config & RESET_HAS_SRST))
  219. {
  220. LOG_ERROR("Can't assert SRST");
  221. return ERROR_FAIL;
  222. }
  223. if (target->reset_halt)
  224. {
  225. /* use hardware to catch reset */
  226. jtag_set_end_state(TAP_IDLE);
  227. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_EJTAGBOOT, NULL);
  228. }
  229. else
  230. {
  231. jtag_set_end_state(TAP_IDLE);
  232. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT, NULL);
  233. }
  234. if (strcmp(target->variant, "ejtag_srst") == 0)
  235. {
  236. uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl | EJTAG_CTRL_PRRST | EJTAG_CTRL_PERRST;
  237. LOG_DEBUG("Using EJTAG reset (PRRST) to reset processor...");
  238. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL, NULL);
  239. mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  240. }
  241. else
  242. {
  243. /* here we should issue a srst only, but we may have to assert trst as well */
  244. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  245. {
  246. jtag_add_reset(1, 1);
  247. }
  248. else
  249. {
  250. jtag_add_reset(0, 1);
  251. }
  252. }
  253. target->state = TARGET_RESET;
  254. jtag_add_sleep(50000);
  255. mips32_invalidate_core_regs(target);
  256. if (target->reset_halt)
  257. {
  258. int retval;
  259. if ((retval = target_halt(target)) != ERROR_OK)
  260. return retval;
  261. }
  262. return ERROR_OK;
  263. }
  264. int mips_m4k_deassert_reset(target_t *target)
  265. {
  266. LOG_DEBUG("target->state: %s",
  267. target_state_name(target));
  268. /* deassert reset lines */
  269. jtag_add_reset(0, 0);
  270. return ERROR_OK;
  271. }
  272. int mips_m4k_soft_reset_halt(struct target_s *target)
  273. {
  274. /* TODO */
  275. return ERROR_OK;
  276. }
  277. int mips_m4k_single_step_core(target_t *target)
  278. {
  279. struct mips32_common *mips32 = target->arch_info;
  280. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  281. /* configure single step mode */
  282. mips_ejtag_config_step(ejtag_info, 1);
  283. /* disable interrupts while stepping */
  284. mips32_enable_interrupts(target, 0);
  285. /* exit debug mode */
  286. mips_ejtag_exit_debug(ejtag_info);
  287. mips_m4k_debug_entry(target);
  288. return ERROR_OK;
  289. }
  290. int mips_m4k_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
  291. {
  292. struct mips32_common *mips32 = target->arch_info;
  293. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  294. struct breakpoint *breakpoint = NULL;
  295. uint32_t resume_pc;
  296. if (target->state != TARGET_HALTED)
  297. {
  298. LOG_WARNING("target not halted");
  299. return ERROR_TARGET_NOT_HALTED;
  300. }
  301. if (!debug_execution)
  302. {
  303. target_free_all_working_areas(target);
  304. mips_m4k_enable_breakpoints(target);
  305. mips_m4k_enable_watchpoints(target);
  306. }
  307. /* current = 1: continue on current pc, otherwise continue at <address> */
  308. if (!current)
  309. {
  310. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
  311. mips32->core_cache->reg_list[MIPS32_PC].dirty = 1;
  312. mips32->core_cache->reg_list[MIPS32_PC].valid = 1;
  313. }
  314. resume_pc = buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32);
  315. mips32_restore_context(target);
  316. /* the front-end may request us not to handle breakpoints */
  317. if (handle_breakpoints)
  318. {
  319. /* Single step past breakpoint at current address */
  320. if ((breakpoint = breakpoint_find(target, resume_pc)))
  321. {
  322. LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
  323. mips_m4k_unset_breakpoint(target, breakpoint);
  324. mips_m4k_single_step_core(target);
  325. mips_m4k_set_breakpoint(target, breakpoint);
  326. }
  327. }
  328. /* enable interrupts if we are running */
  329. mips32_enable_interrupts(target, !debug_execution);
  330. /* exit debug mode */
  331. mips_ejtag_exit_debug(ejtag_info);
  332. target->debug_reason = DBG_REASON_NOTHALTED;
  333. /* registers are now invalid */
  334. mips32_invalidate_core_regs(target);
  335. if (!debug_execution)
  336. {
  337. target->state = TARGET_RUNNING;
  338. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  339. LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
  340. }
  341. else
  342. {
  343. target->state = TARGET_DEBUG_RUNNING;
  344. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  345. LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
  346. }
  347. return ERROR_OK;
  348. }
  349. int mips_m4k_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
  350. {
  351. /* get pointers to arch-specific information */
  352. struct mips32_common *mips32 = target->arch_info;
  353. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  354. struct breakpoint *breakpoint = NULL;
  355. if (target->state != TARGET_HALTED)
  356. {
  357. LOG_WARNING("target not halted");
  358. return ERROR_TARGET_NOT_HALTED;
  359. }
  360. /* current = 1: continue on current pc, otherwise continue at <address> */
  361. if (!current)
  362. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
  363. /* the front-end may request us not to handle breakpoints */
  364. if (handle_breakpoints)
  365. if ((breakpoint = breakpoint_find(target, buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32))))
  366. mips_m4k_unset_breakpoint(target, breakpoint);
  367. /* restore context */
  368. mips32_restore_context(target);
  369. /* configure single step mode */
  370. mips_ejtag_config_step(ejtag_info, 1);
  371. target->debug_reason = DBG_REASON_SINGLESTEP;
  372. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  373. /* disable interrupts while stepping */
  374. mips32_enable_interrupts(target, 0);
  375. /* exit debug mode */
  376. mips_ejtag_exit_debug(ejtag_info);
  377. /* registers are now invalid */
  378. mips32_invalidate_core_regs(target);
  379. if (breakpoint)
  380. mips_m4k_set_breakpoint(target, breakpoint);
  381. LOG_DEBUG("target stepped ");
  382. mips_m4k_debug_entry(target);
  383. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  384. return ERROR_OK;
  385. }
  386. void mips_m4k_enable_breakpoints(struct target_s *target)
  387. {
  388. struct breakpoint *breakpoint = target->breakpoints;
  389. /* set any pending breakpoints */
  390. while (breakpoint)
  391. {
  392. if (breakpoint->set == 0)
  393. mips_m4k_set_breakpoint(target, breakpoint);
  394. breakpoint = breakpoint->next;
  395. }
  396. }
  397. int mips_m4k_set_breakpoint(struct target_s *target, struct breakpoint *breakpoint)
  398. {
  399. struct mips32_common *mips32 = target->arch_info;
  400. struct mips32_comparator * comparator_list = mips32->inst_break_list;
  401. int retval;
  402. if (breakpoint->set)
  403. {
  404. LOG_WARNING("breakpoint already set");
  405. return ERROR_OK;
  406. }
  407. if (breakpoint->type == BKPT_HARD)
  408. {
  409. int bp_num = 0;
  410. while (comparator_list[bp_num].used && (bp_num < mips32->num_inst_bpoints))
  411. bp_num++;
  412. if (bp_num >= mips32->num_inst_bpoints)
  413. {
  414. LOG_DEBUG("ERROR Can not find free FP Comparator(bpid: %d)",
  415. breakpoint->unique_id );
  416. LOG_WARNING("ERROR Can not find free FP Comparator");
  417. exit(-1);
  418. }
  419. breakpoint->set = bp_num + 1;
  420. comparator_list[bp_num].used = 1;
  421. comparator_list[bp_num].bp_value = breakpoint->address;
  422. target_write_u32(target, comparator_list[bp_num].reg_address, comparator_list[bp_num].bp_value);
  423. target_write_u32(target, comparator_list[bp_num].reg_address + 0x08, 0x00000000);
  424. target_write_u32(target, comparator_list[bp_num].reg_address + 0x18, 1);
  425. LOG_DEBUG("bpid: %d, bp_num %i bp_value 0x%" PRIx32 "",
  426. breakpoint->unique_id,
  427. bp_num, comparator_list[bp_num].bp_value);
  428. }
  429. else if (breakpoint->type == BKPT_SOFT)
  430. {
  431. LOG_DEBUG("bpid: %d", breakpoint->unique_id );
  432. if (breakpoint->length == 4)
  433. {
  434. uint32_t verify = 0xffffffff;
  435. if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
  436. {
  437. return retval;
  438. }
  439. if ((retval = target_write_u32(target, breakpoint->address, MIPS32_SDBBP)) != ERROR_OK)
  440. {
  441. return retval;
  442. }
  443. if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
  444. {
  445. return retval;
  446. }
  447. if (verify != MIPS32_SDBBP)
  448. {
  449. LOG_ERROR("Unable to set 32bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
  450. return ERROR_OK;
  451. }
  452. }
  453. else
  454. {
  455. uint16_t verify = 0xffff;
  456. if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
  457. {
  458. return retval;
  459. }
  460. if ((retval = target_write_u16(target, breakpoint->address, MIPS16_SDBBP)) != ERROR_OK)
  461. {
  462. return retval;
  463. }
  464. if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
  465. {
  466. return retval;
  467. }
  468. if (verify != MIPS16_SDBBP)
  469. {
  470. LOG_ERROR("Unable to set 16bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
  471. return ERROR_OK;
  472. }
  473. }
  474. breakpoint->set = 20; /* Any nice value but 0 */
  475. }
  476. return ERROR_OK;
  477. }
  478. int mips_m4k_unset_breakpoint(struct target_s *target, struct breakpoint *breakpoint)
  479. {
  480. /* get pointers to arch-specific information */
  481. struct mips32_common *mips32 = target->arch_info;
  482. struct mips32_comparator * comparator_list = mips32->inst_break_list;
  483. int retval;
  484. if (!breakpoint->set)
  485. {
  486. LOG_WARNING("breakpoint not set");
  487. return ERROR_OK;
  488. }
  489. if (breakpoint->type == BKPT_HARD)
  490. {
  491. int bp_num = breakpoint->set - 1;
  492. if ((bp_num < 0) || (bp_num >= mips32->num_inst_bpoints))
  493. {
  494. LOG_DEBUG("Invalid FP Comparator number in breakpoint (bpid: %d)",
  495. breakpoint->unique_id);
  496. return ERROR_OK;
  497. }
  498. LOG_DEBUG("bpid: %d - releasing hw: %d",
  499. breakpoint->unique_id,
  500. bp_num );
  501. comparator_list[bp_num].used = 0;
  502. comparator_list[bp_num].bp_value = 0;
  503. target_write_u32(target, comparator_list[bp_num].reg_address + 0x18, 0);
  504. }
  505. else
  506. {
  507. /* restore original instruction (kept in target endianness) */
  508. LOG_DEBUG("bpid: %d", breakpoint->unique_id);
  509. if (breakpoint->length == 4)
  510. {
  511. uint32_t current_instr;
  512. /* check that user program has not modified breakpoint instruction */
  513. if ((retval = target_read_memory(target, breakpoint->address, 4, 1, (uint8_t*)&current_instr)) != ERROR_OK)
  514. {
  515. return retval;
  516. }
  517. if (current_instr == MIPS32_SDBBP)
  518. {
  519. if ((retval = target_write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
  520. {
  521. return retval;
  522. }
  523. }
  524. }
  525. else
  526. {
  527. uint16_t current_instr;
  528. /* check that user program has not modified breakpoint instruction */
  529. if ((retval = target_read_memory(target, breakpoint->address, 2, 1, (uint8_t*)&current_instr)) != ERROR_OK)
  530. {
  531. return retval;
  532. }
  533. if (current_instr == MIPS16_SDBBP)
  534. {
  535. if ((retval = target_write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
  536. {
  537. return retval;
  538. }
  539. }
  540. }
  541. }
  542. breakpoint->set = 0;
  543. return ERROR_OK;
  544. }
  545. int mips_m4k_add_breakpoint(struct target_s *target, struct breakpoint *breakpoint)
  546. {
  547. struct mips32_common *mips32 = target->arch_info;
  548. if (breakpoint->type == BKPT_HARD)
  549. {
  550. if (mips32->num_inst_bpoints_avail < 1)
  551. {
  552. LOG_INFO("no hardware breakpoint available");
  553. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  554. }
  555. mips32->num_inst_bpoints_avail--;
  556. }
  557. mips_m4k_set_breakpoint(target, breakpoint);
  558. return ERROR_OK;
  559. }
  560. int mips_m4k_remove_breakpoint(struct target_s *target, struct breakpoint *breakpoint)
  561. {
  562. /* get pointers to arch-specific information */
  563. struct mips32_common *mips32 = target->arch_info;
  564. if (target->state != TARGET_HALTED)
  565. {
  566. LOG_WARNING("target not halted");
  567. return ERROR_TARGET_NOT_HALTED;
  568. }
  569. if (breakpoint->set)
  570. {
  571. mips_m4k_unset_breakpoint(target, breakpoint);
  572. }
  573. if (breakpoint->type == BKPT_HARD)
  574. mips32->num_inst_bpoints_avail++;
  575. return ERROR_OK;
  576. }
  577. int mips_m4k_set_watchpoint(struct target_s *target, struct watchpoint *watchpoint)
  578. {
  579. struct mips32_common *mips32 = target->arch_info;
  580. struct mips32_comparator * comparator_list = mips32->data_break_list;
  581. int wp_num = 0;
  582. /*
  583. * watchpoint enabled, ignore all byte lanes in value register
  584. * and exclude both load and store accesses from watchpoint
  585. * condition evaluation
  586. */
  587. int enable = EJTAG_DBCn_NOSB | EJTAG_DBCn_NOLB | EJTAG_DBCn_BE |
  588. (0xff << EJTAG_DBCn_BLM_SHIFT);
  589. if (watchpoint->set)
  590. {
  591. LOG_WARNING("watchpoint already set");
  592. return ERROR_OK;
  593. }
  594. while(comparator_list[wp_num].used && (wp_num < mips32->num_data_bpoints))
  595. wp_num++;
  596. if (wp_num >= mips32->num_data_bpoints)
  597. {
  598. LOG_DEBUG("ERROR Can not find free FP Comparator");
  599. LOG_WARNING("ERROR Can not find free FP Comparator");
  600. exit(-1);
  601. }
  602. if (watchpoint->length != 4)
  603. {
  604. LOG_ERROR("Only watchpoints of length 4 are supported");
  605. return ERROR_TARGET_UNALIGNED_ACCESS;
  606. }
  607. if (watchpoint->address % 4)
  608. {
  609. LOG_ERROR("Watchpoints address should be word aligned");
  610. return ERROR_TARGET_UNALIGNED_ACCESS;
  611. }
  612. switch (watchpoint->rw)
  613. {
  614. case WPT_READ:
  615. enable &= ~EJTAG_DBCn_NOLB;
  616. break;
  617. case WPT_WRITE:
  618. enable &= ~EJTAG_DBCn_NOSB;
  619. break;
  620. case WPT_ACCESS:
  621. enable &= ~(EJTAG_DBCn_NOLB | EJTAG_DBCn_NOSB);
  622. break;
  623. default:
  624. LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
  625. }
  626. watchpoint->set = wp_num + 1;
  627. comparator_list[wp_num].used = 1;
  628. comparator_list[wp_num].bp_value = watchpoint->address;
  629. target_write_u32(target, comparator_list[wp_num].reg_address, comparator_list[wp_num].bp_value);
  630. target_write_u32(target, comparator_list[wp_num].reg_address + 0x08, 0x00000000);
  631. target_write_u32(target, comparator_list[wp_num].reg_address + 0x10, 0x00000000);
  632. target_write_u32(target, comparator_list[wp_num].reg_address + 0x18, enable);
  633. target_write_u32(target, comparator_list[wp_num].reg_address + 0x20, 0);
  634. LOG_DEBUG("wp_num %i bp_value 0x%" PRIx32 "", wp_num, comparator_list[wp_num].bp_value);
  635. return ERROR_OK;
  636. }
  637. int mips_m4k_unset_watchpoint(struct target_s *target, struct watchpoint *watchpoint)
  638. {
  639. /* get pointers to arch-specific information */
  640. struct mips32_common *mips32 = target->arch_info;
  641. struct mips32_comparator * comparator_list = mips32->data_break_list;
  642. if (!watchpoint->set)
  643. {
  644. LOG_WARNING("watchpoint not set");
  645. return ERROR_OK;
  646. }
  647. int wp_num = watchpoint->set - 1;
  648. if ((wp_num < 0) || (wp_num >= mips32->num_data_bpoints))
  649. {
  650. LOG_DEBUG("Invalid FP Comparator number in watchpoint");
  651. return ERROR_OK;
  652. }
  653. comparator_list[wp_num].used = 0;
  654. comparator_list[wp_num].bp_value = 0;
  655. target_write_u32(target, comparator_list[wp_num].reg_address + 0x18, 0);
  656. watchpoint->set = 0;
  657. return ERROR_OK;
  658. }
  659. int mips_m4k_add_watchpoint(struct target_s *target, struct watchpoint *watchpoint)
  660. {
  661. struct mips32_common *mips32 = target->arch_info;
  662. if (mips32->num_data_bpoints_avail < 1)
  663. {
  664. LOG_INFO("no hardware watchpoints available");
  665. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  666. }
  667. mips32->num_data_bpoints_avail--;
  668. mips_m4k_set_watchpoint(target, watchpoint);
  669. return ERROR_OK;
  670. }
  671. int mips_m4k_remove_watchpoint(struct target_s *target, struct watchpoint *watchpoint)
  672. {
  673. /* get pointers to arch-specific information */
  674. struct mips32_common *mips32 = target->arch_info;
  675. if (target->state != TARGET_HALTED)
  676. {
  677. LOG_WARNING("target not halted");
  678. return ERROR_TARGET_NOT_HALTED;
  679. }
  680. if (watchpoint->set)
  681. {
  682. mips_m4k_unset_watchpoint(target, watchpoint);
  683. }
  684. mips32->num_data_bpoints_avail++;
  685. return ERROR_OK;
  686. }
  687. void mips_m4k_enable_watchpoints(struct target_s *target)
  688. {
  689. struct watchpoint *watchpoint = target->watchpoints;
  690. /* set any pending watchpoints */
  691. while (watchpoint)
  692. {
  693. if (watchpoint->set == 0)
  694. mips_m4k_set_watchpoint(target, watchpoint);
  695. watchpoint = watchpoint->next;
  696. }
  697. }
  698. int mips_m4k_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  699. {
  700. struct mips32_common *mips32 = target->arch_info;
  701. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  702. LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
  703. if (target->state != TARGET_HALTED)
  704. {
  705. LOG_WARNING("target not halted");
  706. return ERROR_TARGET_NOT_HALTED;
  707. }
  708. /* sanitize arguments */
  709. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  710. return ERROR_INVALID_ARGUMENTS;
  711. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  712. return ERROR_TARGET_UNALIGNED_ACCESS;
  713. /* if noDMA off, use DMAACC mode for memory read */
  714. int retval;
  715. if (ejtag_info->impcode & EJTAG_IMP_NODMA)
  716. retval = mips32_pracc_read_mem(ejtag_info, address, size, count, (void *)buffer);
  717. else
  718. retval = mips32_dmaacc_read_mem(ejtag_info, address, size, count, (void *)buffer);
  719. if (ERROR_OK != retval)
  720. return retval;
  721. return ERROR_OK;
  722. }
  723. int mips_m4k_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  724. {
  725. struct mips32_common *mips32 = target->arch_info;
  726. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  727. LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
  728. if (target->state != TARGET_HALTED)
  729. {
  730. LOG_WARNING("target not halted");
  731. return ERROR_TARGET_NOT_HALTED;
  732. }
  733. /* sanitize arguments */
  734. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  735. return ERROR_INVALID_ARGUMENTS;
  736. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  737. return ERROR_TARGET_UNALIGNED_ACCESS;
  738. /* if noDMA off, use DMAACC mode for memory write */
  739. if (ejtag_info->impcode & EJTAG_IMP_NODMA)
  740. return mips32_pracc_write_mem(ejtag_info, address, size, count, (void *)buffer);
  741. else
  742. return mips32_dmaacc_write_mem(ejtag_info, address, size, count, (void *)buffer);
  743. }
  744. int mips_m4k_register_commands(struct command_context_s *cmd_ctx)
  745. {
  746. int retval;
  747. retval = mips32_register_commands(cmd_ctx);
  748. return retval;
  749. }
  750. int mips_m4k_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
  751. {
  752. mips32_build_reg_cache(target);
  753. return ERROR_OK;
  754. }
  755. int mips_m4k_init_arch_info(target_t *target, struct mips_m4k_common *mips_m4k, struct jtag_tap *tap)
  756. {
  757. struct mips32_common *mips32 = &mips_m4k->mips32_common;
  758. mips_m4k->common_magic = MIPSM4K_COMMON_MAGIC;
  759. /* initialize mips4k specific info */
  760. mips32_init_arch_info(target, mips32, tap);
  761. mips32->arch_info = mips_m4k;
  762. return ERROR_OK;
  763. }
  764. int mips_m4k_target_create(struct target_s *target, Jim_Interp *interp)
  765. {
  766. struct mips_m4k_common *mips_m4k = calloc(1,sizeof(struct mips_m4k_common));
  767. mips_m4k_init_arch_info(target, mips_m4k, target->tap);
  768. return ERROR_OK;
  769. }
  770. int mips_m4k_examine(struct target_s *target)
  771. {
  772. int retval;
  773. struct mips32_common *mips32 = target->arch_info;
  774. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  775. uint32_t idcode = 0;
  776. if (!target_was_examined(target))
  777. {
  778. mips_ejtag_get_idcode(ejtag_info, &idcode);
  779. ejtag_info->idcode = idcode;
  780. if (((idcode >> 1) & 0x7FF) == 0x29)
  781. {
  782. /* we are using a pic32mx so select ejtag port
  783. * as it is not selected by default */
  784. mips_ejtag_set_instr(ejtag_info, 0x05, NULL);
  785. LOG_DEBUG("PIC32MX Detected - using EJTAG Interface");
  786. }
  787. }
  788. /* init rest of ejtag interface */
  789. if ((retval = mips_ejtag_init(ejtag_info)) != ERROR_OK)
  790. return retval;
  791. if ((retval = mips32_examine(target)) != ERROR_OK)
  792. return retval;
  793. return ERROR_OK;
  794. }
  795. int mips_m4k_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
  796. {
  797. return mips_m4k_write_memory(target, address, 4, count, buffer);
  798. }
  799. int mips_m4k_checksum_memory(target_t *target, uint32_t address, uint32_t size, uint32_t *checksum)
  800. {
  801. return ERROR_FAIL; /* use bulk read method */
  802. }