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.
 
 
 
 
 
 

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