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.
 
 
 
 
 
 

1250 lines
33 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. * Copyright (C) 2009 by David N. Claffey <dnclaffey@gmail.com> *
  8. * *
  9. * Copyright (C) 2011 by Drasko DRASKOVIC *
  10. * drasko.draskovic@gmail.com *
  11. * *
  12. * This program is free software; you can redistribute it and/or modify *
  13. * it under the terms of the GNU General Public License as published by *
  14. * the Free Software Foundation; either version 2 of the License, or *
  15. * (at your option) any later version. *
  16. * *
  17. * This program is distributed in the hope that it will be useful, *
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  20. * GNU General Public License for more details. *
  21. * *
  22. * You should have received a copy of the GNU General Public License *
  23. * along with this program; if not, write to the *
  24. * Free Software Foundation, Inc., *
  25. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  26. ***************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "breakpoints.h"
  31. #include "mips32.h"
  32. #include "mips_m4k.h"
  33. #include "mips32_dmaacc.h"
  34. #include "target_type.h"
  35. #include "register.h"
  36. static void mips_m4k_enable_breakpoints(struct target *target);
  37. static void mips_m4k_enable_watchpoints(struct target *target);
  38. static int mips_m4k_set_breakpoint(struct target *target,
  39. struct breakpoint *breakpoint);
  40. static int mips_m4k_unset_breakpoint(struct target *target,
  41. struct breakpoint *breakpoint);
  42. static int mips_m4k_examine_debug_reason(struct target *target)
  43. {
  44. uint32_t break_status;
  45. int retval;
  46. if ((target->debug_reason != DBG_REASON_DBGRQ)
  47. && (target->debug_reason != DBG_REASON_SINGLESTEP))
  48. {
  49. /* get info about inst breakpoint support */
  50. if ((retval = target_read_u32(target, EJTAG_IBS, &break_status)) != ERROR_OK)
  51. return retval;
  52. if (break_status & 0x1f)
  53. {
  54. /* we have halted on a breakpoint */
  55. if ((retval = target_write_u32(target, EJTAG_IBS, 0)) != ERROR_OK)
  56. return retval;
  57. target->debug_reason = DBG_REASON_BREAKPOINT;
  58. }
  59. /* get info about data breakpoint support */
  60. if ((retval = target_read_u32(target, EJTAG_DBS, &break_status)) != ERROR_OK)
  61. return retval;
  62. if (break_status & 0x1f)
  63. {
  64. /* we have halted on a breakpoint */
  65. if ((retval = target_write_u32(target, EJTAG_DBS, 0)) != ERROR_OK)
  66. return retval;
  67. target->debug_reason = DBG_REASON_WATCHPOINT;
  68. }
  69. }
  70. return ERROR_OK;
  71. }
  72. static int mips_m4k_debug_entry(struct target *target)
  73. {
  74. struct mips32_common *mips32 = target_to_mips32(target);
  75. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  76. uint32_t debug_reg;
  77. /* read debug register */
  78. mips_ejtag_read_debug(ejtag_info, &debug_reg);
  79. /* make sure break unit configured */
  80. mips32_configure_break_unit(target);
  81. /* attempt to find halt reason */
  82. mips_m4k_examine_debug_reason(target);
  83. /* clear single step if active */
  84. if (debug_reg & EJTAG_DEBUG_DSS)
  85. {
  86. /* stopped due to single step - clear step bit */
  87. mips_ejtag_config_step(ejtag_info, 0);
  88. }
  89. mips32_save_context(target);
  90. /* default to mips32 isa, it will be changed below if required */
  91. mips32->isa_mode = MIPS32_ISA_MIPS32;
  92. if (ejtag_info->impcode & EJTAG_IMP_MIPS16) {
  93. mips32->isa_mode = buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 1);
  94. }
  95. LOG_DEBUG("entered debug state at PC 0x%" PRIx32 ", target->state: %s",
  96. buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32),
  97. target_state_name(target));
  98. return ERROR_OK;
  99. }
  100. static int mips_m4k_poll(struct target *target)
  101. {
  102. int retval;
  103. struct mips32_common *mips32 = target_to_mips32(target);
  104. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  105. uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl;
  106. /* read ejtag control reg */
  107. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  108. retval = mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  109. if (retval != ERROR_OK)
  110. return retval;
  111. /* clear this bit before handling polling
  112. * as after reset registers will read zero */
  113. if (ejtag_ctrl & EJTAG_CTRL_ROCC)
  114. {
  115. /* we have detected a reset, clear flag
  116. * otherwise ejtag will not work */
  117. ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_ROCC;
  118. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  119. retval = mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  120. if (retval != ERROR_OK)
  121. return retval;
  122. LOG_DEBUG("Reset Detected");
  123. }
  124. /* check for processor halted */
  125. if (ejtag_ctrl & EJTAG_CTRL_BRKST)
  126. {
  127. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
  128. {
  129. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT);
  130. target->state = TARGET_HALTED;
  131. if ((retval = mips_m4k_debug_entry(target)) != ERROR_OK)
  132. return retval;
  133. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  134. }
  135. else if (target->state == TARGET_DEBUG_RUNNING)
  136. {
  137. target->state = TARGET_HALTED;
  138. if ((retval = mips_m4k_debug_entry(target)) != ERROR_OK)
  139. return retval;
  140. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  141. }
  142. }
  143. else
  144. {
  145. target->state = TARGET_RUNNING;
  146. }
  147. // LOG_DEBUG("ctrl = 0x%08X", ejtag_ctrl);
  148. return ERROR_OK;
  149. }
  150. static int mips_m4k_halt(struct target *target)
  151. {
  152. struct mips32_common *mips32 = target_to_mips32(target);
  153. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  154. LOG_DEBUG("target->state: %s",
  155. target_state_name(target));
  156. if (target->state == TARGET_HALTED)
  157. {
  158. LOG_DEBUG("target was already halted");
  159. return ERROR_OK;
  160. }
  161. if (target->state == TARGET_UNKNOWN)
  162. {
  163. LOG_WARNING("target was in unknown state when halt was requested");
  164. }
  165. if (target->state == TARGET_RESET)
  166. {
  167. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst())
  168. {
  169. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  170. return ERROR_TARGET_FAILURE;
  171. }
  172. else
  173. {
  174. /* we came here in a reset_halt or reset_init sequence
  175. * debug entry was already prepared in mips32_prepare_reset_halt()
  176. */
  177. target->debug_reason = DBG_REASON_DBGRQ;
  178. return ERROR_OK;
  179. }
  180. }
  181. /* break processor */
  182. mips_ejtag_enter_debug(ejtag_info);
  183. target->debug_reason = DBG_REASON_DBGRQ;
  184. return ERROR_OK;
  185. }
  186. static int mips_m4k_assert_reset(struct target *target)
  187. {
  188. struct mips_m4k_common *mips_m4k = target_to_m4k(target);
  189. struct mips_ejtag *ejtag_info = &mips_m4k->mips32.ejtag_info;
  190. int assert_srst = 1;
  191. LOG_DEBUG("target->state: %s",
  192. target_state_name(target));
  193. enum reset_types jtag_reset_config = jtag_get_reset_config();
  194. if (!(jtag_reset_config & RESET_HAS_SRST))
  195. assert_srst = 0;
  196. if (target->reset_halt)
  197. {
  198. /* use hardware to catch reset */
  199. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_EJTAGBOOT);
  200. }
  201. else
  202. {
  203. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT);
  204. }
  205. if (assert_srst)
  206. {
  207. /* here we should issue a srst only, but we may have to assert trst as well */
  208. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  209. {
  210. jtag_add_reset(1, 1);
  211. }
  212. else
  213. {
  214. jtag_add_reset(0, 1);
  215. }
  216. }
  217. else
  218. {
  219. if (mips_m4k->is_pic32mx)
  220. {
  221. LOG_DEBUG("Using MTAP reset to reset processor...");
  222. /* use microchip specific MTAP reset */
  223. mips_ejtag_set_instr(ejtag_info, MTAP_SW_MTAP);
  224. mips_ejtag_set_instr(ejtag_info, MTAP_COMMAND);
  225. mips_ejtag_drscan_8_out(ejtag_info, MCHP_ASERT_RST);
  226. mips_ejtag_drscan_8_out(ejtag_info, MCHP_DE_ASSERT_RST);
  227. mips_ejtag_set_instr(ejtag_info, MTAP_SW_ETAP);
  228. }
  229. else
  230. {
  231. /* use ejtag reset - not supported by all cores */
  232. uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl | EJTAG_CTRL_PRRST | EJTAG_CTRL_PERRST;
  233. LOG_DEBUG("Using EJTAG reset (PRRST) to reset processor...");
  234. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  235. mips_ejtag_drscan_32_out(ejtag_info, ejtag_ctrl);
  236. }
  237. }
  238. target->state = TARGET_RESET;
  239. jtag_add_sleep(50000);
  240. register_cache_invalidate(mips_m4k->mips32.core_cache);
  241. if (target->reset_halt)
  242. {
  243. int retval;
  244. if ((retval = target_halt(target)) != ERROR_OK)
  245. return retval;
  246. }
  247. return ERROR_OK;
  248. }
  249. static int mips_m4k_deassert_reset(struct target *target)
  250. {
  251. LOG_DEBUG("target->state: %s",
  252. target_state_name(target));
  253. /* deassert reset lines */
  254. jtag_add_reset(0, 0);
  255. return ERROR_OK;
  256. }
  257. static int mips_m4k_soft_reset_halt(struct target *target)
  258. {
  259. /* TODO */
  260. return ERROR_OK;
  261. }
  262. static int mips_m4k_single_step_core(struct target *target)
  263. {
  264. struct mips32_common *mips32 = target_to_mips32(target);
  265. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  266. /* configure single step mode */
  267. mips_ejtag_config_step(ejtag_info, 1);
  268. /* disable interrupts while stepping */
  269. mips32_enable_interrupts(target, 0);
  270. /* exit debug mode */
  271. mips_ejtag_exit_debug(ejtag_info);
  272. mips_m4k_debug_entry(target);
  273. return ERROR_OK;
  274. }
  275. static int mips_m4k_resume(struct target *target, int current,
  276. uint32_t address, int handle_breakpoints, int debug_execution)
  277. {
  278. struct mips32_common *mips32 = target_to_mips32(target);
  279. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  280. struct breakpoint *breakpoint = NULL;
  281. uint32_t resume_pc;
  282. if (target->state != TARGET_HALTED)
  283. {
  284. LOG_WARNING("target not halted");
  285. return ERROR_TARGET_NOT_HALTED;
  286. }
  287. if (!debug_execution)
  288. {
  289. target_free_all_working_areas(target);
  290. mips_m4k_enable_breakpoints(target);
  291. mips_m4k_enable_watchpoints(target);
  292. }
  293. /* current = 1: continue on current pc, otherwise continue at <address> */
  294. if (!current)
  295. {
  296. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
  297. mips32->core_cache->reg_list[MIPS32_PC].dirty = 1;
  298. mips32->core_cache->reg_list[MIPS32_PC].valid = 1;
  299. }
  300. if (ejtag_info->impcode & EJTAG_IMP_MIPS16) {
  301. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 1, mips32->isa_mode);
  302. }
  303. resume_pc = buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32);
  304. mips32_restore_context(target);
  305. /* the front-end may request us not to handle breakpoints */
  306. if (handle_breakpoints)
  307. {
  308. /* Single step past breakpoint at current address */
  309. if ((breakpoint = breakpoint_find(target, resume_pc)))
  310. {
  311. LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
  312. mips_m4k_unset_breakpoint(target, breakpoint);
  313. mips_m4k_single_step_core(target);
  314. mips_m4k_set_breakpoint(target, breakpoint);
  315. }
  316. }
  317. /* enable interrupts if we are running */
  318. mips32_enable_interrupts(target, !debug_execution);
  319. /* exit debug mode */
  320. mips_ejtag_exit_debug(ejtag_info);
  321. target->debug_reason = DBG_REASON_NOTHALTED;
  322. /* registers are now invalid */
  323. register_cache_invalidate(mips32->core_cache);
  324. if (!debug_execution)
  325. {
  326. target->state = TARGET_RUNNING;
  327. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  328. LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
  329. }
  330. else
  331. {
  332. target->state = TARGET_DEBUG_RUNNING;
  333. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  334. LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
  335. }
  336. return ERROR_OK;
  337. }
  338. static int mips_m4k_step(struct target *target, int current,
  339. uint32_t address, int handle_breakpoints)
  340. {
  341. /* get pointers to arch-specific information */
  342. struct mips32_common *mips32 = target_to_mips32(target);
  343. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  344. struct breakpoint *breakpoint = NULL;
  345. if (target->state != TARGET_HALTED)
  346. {
  347. LOG_WARNING("target not halted");
  348. return ERROR_TARGET_NOT_HALTED;
  349. }
  350. /* current = 1: continue on current pc, otherwise continue at <address> */
  351. if (!current)
  352. {
  353. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
  354. mips32->core_cache->reg_list[MIPS32_PC].dirty = 1;
  355. mips32->core_cache->reg_list[MIPS32_PC].valid = 1;
  356. }
  357. /* the front-end may request us not to handle breakpoints */
  358. if (handle_breakpoints) {
  359. breakpoint = breakpoint_find(target,
  360. buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32));
  361. if (breakpoint)
  362. mips_m4k_unset_breakpoint(target, breakpoint);
  363. }
  364. /* restore context */
  365. mips32_restore_context(target);
  366. /* configure single step mode */
  367. mips_ejtag_config_step(ejtag_info, 1);
  368. target->debug_reason = DBG_REASON_SINGLESTEP;
  369. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  370. /* disable interrupts while stepping */
  371. mips32_enable_interrupts(target, 0);
  372. /* exit debug mode */
  373. mips_ejtag_exit_debug(ejtag_info);
  374. /* registers are now invalid */
  375. register_cache_invalidate(mips32->core_cache);
  376. if (breakpoint)
  377. mips_m4k_set_breakpoint(target, breakpoint);
  378. LOG_DEBUG("target stepped ");
  379. mips_m4k_debug_entry(target);
  380. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  381. return ERROR_OK;
  382. }
  383. static void mips_m4k_enable_breakpoints(struct target *target)
  384. {
  385. struct breakpoint *breakpoint = target->breakpoints;
  386. /* set any pending breakpoints */
  387. while (breakpoint)
  388. {
  389. if (breakpoint->set == 0)
  390. mips_m4k_set_breakpoint(target, breakpoint);
  391. breakpoint = breakpoint->next;
  392. }
  393. }
  394. static int mips_m4k_set_breakpoint(struct target *target,
  395. struct breakpoint *breakpoint)
  396. {
  397. struct mips32_common *mips32 = target_to_mips32(target);
  398. struct mips32_comparator * comparator_list = mips32->inst_break_list;
  399. int retval;
  400. if (breakpoint->set)
  401. {
  402. LOG_WARNING("breakpoint already set");
  403. return ERROR_OK;
  404. }
  405. if (breakpoint->type == BKPT_HARD)
  406. {
  407. int bp_num = 0;
  408. while (comparator_list[bp_num].used && (bp_num < mips32->num_inst_bpoints))
  409. bp_num++;
  410. if (bp_num >= mips32->num_inst_bpoints)
  411. {
  412. LOG_ERROR("Can not find free FP Comparator(bpid: %d)",
  413. breakpoint->unique_id );
  414. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  415. }
  416. breakpoint->set = bp_num + 1;
  417. comparator_list[bp_num].used = 1;
  418. comparator_list[bp_num].bp_value = breakpoint->address;
  419. target_write_u32(target, comparator_list[bp_num].reg_address, comparator_list[bp_num].bp_value);
  420. target_write_u32(target, comparator_list[bp_num].reg_address + 0x08, 0x00000000);
  421. target_write_u32(target, comparator_list[bp_num].reg_address + 0x18, 1);
  422. LOG_DEBUG("bpid: %d, bp_num %i bp_value 0x%" PRIx32 "",
  423. breakpoint->unique_id,
  424. bp_num, comparator_list[bp_num].bp_value);
  425. }
  426. else if (breakpoint->type == BKPT_SOFT)
  427. {
  428. LOG_DEBUG("bpid: %d", breakpoint->unique_id );
  429. if (breakpoint->length == 4)
  430. {
  431. uint32_t verify = 0xffffffff;
  432. if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1,
  433. breakpoint->orig_instr)) != ERROR_OK)
  434. {
  435. return retval;
  436. }
  437. if ((retval = target_write_u32(target, breakpoint->address, MIPS32_SDBBP)) != ERROR_OK)
  438. {
  439. return retval;
  440. }
  441. if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
  442. {
  443. return retval;
  444. }
  445. if (verify != MIPS32_SDBBP)
  446. {
  447. LOG_ERROR("Unable to set 32bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
  448. return ERROR_OK;
  449. }
  450. }
  451. else
  452. {
  453. uint16_t verify = 0xffff;
  454. if ((retval = target_read_memory(target, breakpoint->address, breakpoint->length, 1,
  455. breakpoint->orig_instr)) != ERROR_OK)
  456. {
  457. return retval;
  458. }
  459. if ((retval = target_write_u16(target, breakpoint->address, MIPS16_SDBBP)) != ERROR_OK)
  460. {
  461. return retval;
  462. }
  463. if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
  464. {
  465. return retval;
  466. }
  467. if (verify != MIPS16_SDBBP)
  468. {
  469. LOG_ERROR("Unable to set 16bit breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
  470. return ERROR_OK;
  471. }
  472. }
  473. breakpoint->set = 20; /* Any nice value but 0 */
  474. }
  475. return ERROR_OK;
  476. }
  477. static int mips_m4k_unset_breakpoint(struct target *target,
  478. struct breakpoint *breakpoint)
  479. {
  480. /* get pointers to arch-specific information */
  481. struct mips32_common *mips32 = target_to_mips32(target);
  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,
  514. (uint8_t*)&current_instr)) != ERROR_OK)
  515. {
  516. return retval;
  517. }
  518. /**
  519. * target_read_memory() gets us data in _target_ endianess.
  520. * If we want to use this data on the host for comparisons with some macros
  521. * we must first transform it to _host_ endianess using target_buffer_get_u32().
  522. */
  523. current_instr = target_buffer_get_u32(target, (uint8_t *)&current_instr);
  524. if (current_instr == MIPS32_SDBBP)
  525. {
  526. if ((retval = target_write_memory(target, breakpoint->address, 4, 1,
  527. breakpoint->orig_instr)) != ERROR_OK)
  528. {
  529. return retval;
  530. }
  531. }
  532. }
  533. else
  534. {
  535. uint16_t current_instr;
  536. /* check that user program has not modified breakpoint instruction */
  537. if ((retval = target_read_memory(target, breakpoint->address, 2, 1,
  538. (uint8_t*)&current_instr)) != ERROR_OK)
  539. {
  540. return retval;
  541. }
  542. current_instr = target_buffer_get_u16(target, (uint8_t *)&current_instr);
  543. if (current_instr == MIPS16_SDBBP)
  544. {
  545. if ((retval = target_write_memory(target, breakpoint->address, 2, 1,
  546. breakpoint->orig_instr)) != ERROR_OK)
  547. {
  548. return retval;
  549. }
  550. }
  551. }
  552. }
  553. breakpoint->set = 0;
  554. return ERROR_OK;
  555. }
  556. static int mips_m4k_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
  557. {
  558. struct mips32_common *mips32 = target_to_mips32(target);
  559. if (breakpoint->type == BKPT_HARD)
  560. {
  561. if (mips32->num_inst_bpoints_avail < 1)
  562. {
  563. LOG_INFO("no hardware breakpoint available");
  564. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  565. }
  566. mips32->num_inst_bpoints_avail--;
  567. }
  568. return mips_m4k_set_breakpoint(target, breakpoint);
  569. }
  570. static int mips_m4k_remove_breakpoint(struct target *target,
  571. struct breakpoint *breakpoint)
  572. {
  573. /* get pointers to arch-specific information */
  574. struct mips32_common *mips32 = target_to_mips32(target);
  575. if (target->state != TARGET_HALTED)
  576. {
  577. LOG_WARNING("target not halted");
  578. return ERROR_TARGET_NOT_HALTED;
  579. }
  580. if (breakpoint->set)
  581. {
  582. mips_m4k_unset_breakpoint(target, breakpoint);
  583. }
  584. if (breakpoint->type == BKPT_HARD)
  585. mips32->num_inst_bpoints_avail++;
  586. return ERROR_OK;
  587. }
  588. static int mips_m4k_set_watchpoint(struct target *target,
  589. struct watchpoint *watchpoint)
  590. {
  591. struct mips32_common *mips32 = target_to_mips32(target);
  592. struct mips32_comparator *comparator_list = mips32->data_break_list;
  593. int wp_num = 0;
  594. /*
  595. * watchpoint enabled, ignore all byte lanes in value register
  596. * and exclude both load and store accesses from watchpoint
  597. * condition evaluation
  598. */
  599. int enable = EJTAG_DBCn_NOSB | EJTAG_DBCn_NOLB | EJTAG_DBCn_BE |
  600. (0xff << EJTAG_DBCn_BLM_SHIFT);
  601. if (watchpoint->set)
  602. {
  603. LOG_WARNING("watchpoint already set");
  604. return ERROR_OK;
  605. }
  606. while(comparator_list[wp_num].used && (wp_num < mips32->num_data_bpoints))
  607. wp_num++;
  608. if (wp_num >= mips32->num_data_bpoints)
  609. {
  610. LOG_ERROR("Can not find free FP Comparator");
  611. return ERROR_FAIL;
  612. }
  613. if (watchpoint->length != 4)
  614. {
  615. LOG_ERROR("Only watchpoints of length 4 are supported");
  616. return ERROR_TARGET_UNALIGNED_ACCESS;
  617. }
  618. if (watchpoint->address % 4)
  619. {
  620. LOG_ERROR("Watchpoints address should be word aligned");
  621. return ERROR_TARGET_UNALIGNED_ACCESS;
  622. }
  623. switch (watchpoint->rw)
  624. {
  625. case WPT_READ:
  626. enable &= ~EJTAG_DBCn_NOLB;
  627. break;
  628. case WPT_WRITE:
  629. enable &= ~EJTAG_DBCn_NOSB;
  630. break;
  631. case WPT_ACCESS:
  632. enable &= ~(EJTAG_DBCn_NOLB | EJTAG_DBCn_NOSB);
  633. break;
  634. default:
  635. LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
  636. }
  637. watchpoint->set = wp_num + 1;
  638. comparator_list[wp_num].used = 1;
  639. comparator_list[wp_num].bp_value = watchpoint->address;
  640. target_write_u32(target, comparator_list[wp_num].reg_address, comparator_list[wp_num].bp_value);
  641. target_write_u32(target, comparator_list[wp_num].reg_address + 0x08, 0x00000000);
  642. target_write_u32(target, comparator_list[wp_num].reg_address + 0x10, 0x00000000);
  643. target_write_u32(target, comparator_list[wp_num].reg_address + 0x18, enable);
  644. target_write_u32(target, comparator_list[wp_num].reg_address + 0x20, 0);
  645. LOG_DEBUG("wp_num %i bp_value 0x%" PRIx32 "", wp_num, comparator_list[wp_num].bp_value);
  646. return ERROR_OK;
  647. }
  648. static int mips_m4k_unset_watchpoint(struct target *target,
  649. struct watchpoint *watchpoint)
  650. {
  651. /* get pointers to arch-specific information */
  652. struct mips32_common *mips32 = target_to_mips32(target);
  653. struct mips32_comparator *comparator_list = mips32->data_break_list;
  654. if (!watchpoint->set)
  655. {
  656. LOG_WARNING("watchpoint not set");
  657. return ERROR_OK;
  658. }
  659. int wp_num = watchpoint->set - 1;
  660. if ((wp_num < 0) || (wp_num >= mips32->num_data_bpoints))
  661. {
  662. LOG_DEBUG("Invalid FP Comparator number in watchpoint");
  663. return ERROR_OK;
  664. }
  665. comparator_list[wp_num].used = 0;
  666. comparator_list[wp_num].bp_value = 0;
  667. target_write_u32(target, comparator_list[wp_num].reg_address + 0x18, 0);
  668. watchpoint->set = 0;
  669. return ERROR_OK;
  670. }
  671. static int mips_m4k_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
  672. {
  673. struct mips32_common *mips32 = target_to_mips32(target);
  674. if (mips32->num_data_bpoints_avail < 1)
  675. {
  676. LOG_INFO("no hardware watchpoints available");
  677. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  678. }
  679. mips32->num_data_bpoints_avail--;
  680. mips_m4k_set_watchpoint(target, watchpoint);
  681. return ERROR_OK;
  682. }
  683. static int mips_m4k_remove_watchpoint(struct target *target,
  684. struct watchpoint *watchpoint)
  685. {
  686. /* get pointers to arch-specific information */
  687. struct mips32_common *mips32 = target_to_mips32(target);
  688. if (target->state != TARGET_HALTED)
  689. {
  690. LOG_WARNING("target not halted");
  691. return ERROR_TARGET_NOT_HALTED;
  692. }
  693. if (watchpoint->set)
  694. {
  695. mips_m4k_unset_watchpoint(target, watchpoint);
  696. }
  697. mips32->num_data_bpoints_avail++;
  698. return ERROR_OK;
  699. }
  700. static void mips_m4k_enable_watchpoints(struct target *target)
  701. {
  702. struct watchpoint *watchpoint = target->watchpoints;
  703. /* set any pending watchpoints */
  704. while (watchpoint)
  705. {
  706. if (watchpoint->set == 0)
  707. mips_m4k_set_watchpoint(target, watchpoint);
  708. watchpoint = watchpoint->next;
  709. }
  710. }
  711. static int mips_m4k_read_memory(struct target *target, uint32_t address,
  712. uint32_t size, uint32_t count, uint8_t *buffer)
  713. {
  714. struct mips32_common *mips32 = target_to_mips32(target);
  715. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  716. LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
  717. if (target->state != TARGET_HALTED)
  718. {
  719. LOG_WARNING("target not halted");
  720. return ERROR_TARGET_NOT_HALTED;
  721. }
  722. /* sanitize arguments */
  723. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  724. return ERROR_INVALID_ARGUMENTS;
  725. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  726. return ERROR_TARGET_UNALIGNED_ACCESS;
  727. /* since we don't know if buffer is aligned, we allocate new mem that is always aligned */
  728. void *t = NULL;
  729. if (size > 1)
  730. {
  731. t = malloc(count * size * sizeof(uint8_t));
  732. if (t == NULL)
  733. {
  734. LOG_ERROR("Out of memory");
  735. return ERROR_FAIL;
  736. }
  737. }
  738. else
  739. {
  740. t = buffer;
  741. }
  742. /* if noDMA off, use DMAACC mode for memory read */
  743. int retval;
  744. if (ejtag_info->impcode & EJTAG_IMP_NODMA)
  745. retval = mips32_pracc_read_mem(ejtag_info, address, size, count, t);
  746. else
  747. retval = mips32_dmaacc_read_mem(ejtag_info, address, size, count, t);
  748. /* mips32_..._read_mem with size 4/2 returns uint32_t/uint16_t in host */
  749. /* endianness, but byte array should represent target endianness */
  750. if (ERROR_OK == retval)
  751. {
  752. switch(size)
  753. {
  754. case 4:
  755. target_buffer_set_u32_array(target,buffer,count,t);
  756. break;
  757. case 2:
  758. target_buffer_set_u16_array(target,buffer,count,t);
  759. break;
  760. }
  761. }
  762. if ((size > 1) && (t != NULL))
  763. free(t);
  764. return retval;
  765. }
  766. static int mips_m4k_write_memory(struct target *target, uint32_t address,
  767. uint32_t size, uint32_t count, const uint8_t *buffer)
  768. {
  769. struct mips32_common *mips32 = target_to_mips32(target);
  770. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  771. LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
  772. address, size, count);
  773. if (target->state != TARGET_HALTED)
  774. {
  775. LOG_WARNING("target not halted");
  776. return ERROR_TARGET_NOT_HALTED;
  777. }
  778. /* sanitize arguments */
  779. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  780. return ERROR_INVALID_ARGUMENTS;
  781. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  782. return ERROR_TARGET_UNALIGNED_ACCESS;
  783. /** correct endianess if we have word or hword access */
  784. void *t = NULL;
  785. if (size > 1)
  786. {
  787. /* mips32_..._write_mem with size 4/2 requires uint32_t/uint16_t in host */
  788. /* endianness, but byte array represents target endianness */
  789. t = malloc(count * size * sizeof(uint8_t));
  790. if (t == NULL)
  791. {
  792. LOG_ERROR("Out of memory");
  793. return ERROR_FAIL;
  794. }
  795. switch(size)
  796. {
  797. case 4:
  798. target_buffer_get_u32_array(target,buffer,count,(uint32_t*)t);
  799. break;
  800. case 2:
  801. target_buffer_get_u16_array(target,buffer,count,(uint16_t*)t);
  802. break;
  803. }
  804. buffer = t;
  805. }
  806. /* if noDMA off, use DMAACC mode for memory write */
  807. int retval;
  808. if (ejtag_info->impcode & EJTAG_IMP_NODMA)
  809. retval = mips32_pracc_write_mem(ejtag_info, address, size, count, (void *)buffer);
  810. else
  811. retval = mips32_dmaacc_write_mem(ejtag_info, address, size, count, (void *)buffer);
  812. if (t != NULL)
  813. free(t);
  814. if (ERROR_OK != retval)
  815. return retval;
  816. return ERROR_OK;
  817. }
  818. static int mips_m4k_init_target(struct command_context *cmd_ctx,
  819. struct target *target)
  820. {
  821. mips32_build_reg_cache(target);
  822. return ERROR_OK;
  823. }
  824. static int mips_m4k_init_arch_info(struct target *target,
  825. struct mips_m4k_common *mips_m4k, struct jtag_tap *tap)
  826. {
  827. struct mips32_common *mips32 = &mips_m4k->mips32;
  828. mips_m4k->common_magic = MIPSM4K_COMMON_MAGIC;
  829. /* initialize mips4k specific info */
  830. mips32_init_arch_info(target, mips32, tap);
  831. mips32->arch_info = mips_m4k;
  832. return ERROR_OK;
  833. }
  834. static int mips_m4k_target_create(struct target *target, Jim_Interp *interp)
  835. {
  836. struct mips_m4k_common *mips_m4k = calloc(1, sizeof(struct mips_m4k_common));
  837. mips_m4k_init_arch_info(target, mips_m4k, target->tap);
  838. return ERROR_OK;
  839. }
  840. static int mips_m4k_examine(struct target *target)
  841. {
  842. int retval;
  843. struct mips_m4k_common *mips_m4k = target_to_m4k(target);
  844. struct mips_ejtag *ejtag_info = &mips_m4k->mips32.ejtag_info;
  845. uint32_t idcode = 0;
  846. if (!target_was_examined(target))
  847. {
  848. retval = mips_ejtag_get_idcode(ejtag_info, &idcode);
  849. if (retval != ERROR_OK)
  850. return retval;
  851. ejtag_info->idcode = idcode;
  852. if (((idcode >> 1) & 0x7FF) == 0x29)
  853. {
  854. /* we are using a pic32mx so select ejtag port
  855. * as it is not selected by default */
  856. mips_ejtag_set_instr(ejtag_info, MTAP_SW_ETAP);
  857. LOG_DEBUG("PIC32MX Detected - using EJTAG Interface");
  858. mips_m4k->is_pic32mx = true;
  859. }
  860. }
  861. /* init rest of ejtag interface */
  862. if ((retval = mips_ejtag_init(ejtag_info)) != ERROR_OK)
  863. return retval;
  864. if ((retval = mips32_examine(target)) != ERROR_OK)
  865. return retval;
  866. return ERROR_OK;
  867. }
  868. static int mips_m4k_bulk_write_memory(struct target *target, uint32_t address,
  869. uint32_t count, const uint8_t *buffer)
  870. {
  871. struct mips32_common *mips32 = target_to_mips32(target);
  872. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  873. int retval;
  874. int write_t = 1;
  875. LOG_DEBUG("address: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, count);
  876. if (target->state != TARGET_HALTED)
  877. {
  878. LOG_WARNING("target not halted");
  879. return ERROR_TARGET_NOT_HALTED;
  880. }
  881. /* check alignment */
  882. if (address & 0x3u)
  883. return ERROR_TARGET_UNALIGNED_ACCESS;
  884. if (mips32->fast_data_area == NULL)
  885. {
  886. /* Get memory for block write handler
  887. * we preserve this area between calls and gain a speed increase
  888. * of about 3kb/sec when writing flash
  889. * this will be released/nulled by the system when the target is resumed or reset */
  890. retval = target_alloc_working_area(target,
  891. MIPS32_FASTDATA_HANDLER_SIZE,
  892. &mips32->fast_data_area);
  893. if (retval != ERROR_OK)
  894. {
  895. LOG_WARNING("No working area available, falling back to non-bulk write");
  896. return mips_m4k_write_memory(target, address, 4, count, buffer);
  897. }
  898. /* reset fastadata state so the algo get reloaded */
  899. ejtag_info->fast_access_save = -1;
  900. }
  901. /* mips32_pracc_fastdata_xfer requires uint32_t in host endianness, */
  902. /* but byte array represents target endianness */
  903. uint32_t *t = NULL;
  904. t = malloc(count * sizeof(uint32_t));
  905. if (t == NULL)
  906. {
  907. LOG_ERROR("Out of memory");
  908. return ERROR_FAIL;
  909. }
  910. target_buffer_get_u32_array(target,buffer,count,t);
  911. retval = mips32_pracc_fastdata_xfer(ejtag_info, mips32->fast_data_area, write_t, address,
  912. count, t);
  913. if (t != NULL)
  914. free(t);
  915. if (retval != ERROR_OK)
  916. {
  917. /* FASTDATA access failed, try normal memory write */
  918. LOG_DEBUG("Fastdata access Failed, falling back to non-bulk write");
  919. retval = mips_m4k_write_memory(target, address, 4, count, buffer);
  920. }
  921. return retval;
  922. }
  923. static int mips_m4k_verify_pointer(struct command_context *cmd_ctx,
  924. struct mips_m4k_common *mips_m4k)
  925. {
  926. if (mips_m4k->common_magic != MIPSM4K_COMMON_MAGIC) {
  927. command_print(cmd_ctx, "target is not an MIPS_M4K");
  928. return ERROR_TARGET_INVALID;
  929. }
  930. return ERROR_OK;
  931. }
  932. COMMAND_HANDLER(mips_m4k_handle_cp0_command)
  933. {
  934. int retval;
  935. struct target *target = get_current_target(CMD_CTX);
  936. struct mips_m4k_common *mips_m4k = target_to_m4k(target);
  937. struct mips_ejtag *ejtag_info = &mips_m4k->mips32.ejtag_info;
  938. retval = mips_m4k_verify_pointer(CMD_CTX, mips_m4k);
  939. if (retval != ERROR_OK)
  940. return retval;
  941. if (target->state != TARGET_HALTED)
  942. {
  943. command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
  944. return ERROR_OK;
  945. }
  946. /* two or more argument, access a single register/select (write if third argument is given) */
  947. if (CMD_ARGC < 2)
  948. {
  949. command_print(CMD_CTX, "command requires more arguments.");
  950. }
  951. else
  952. {
  953. uint32_t cp0_reg, cp0_sel;
  954. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], cp0_reg);
  955. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], cp0_sel);
  956. if (CMD_ARGC == 2)
  957. {
  958. uint32_t value;
  959. if ((retval = mips32_cp0_read(ejtag_info, &value, cp0_reg, cp0_sel)) != ERROR_OK)
  960. {
  961. command_print(CMD_CTX,
  962. "couldn't access reg %" PRIi32,
  963. cp0_reg);
  964. return ERROR_OK;
  965. }
  966. if ((retval = jtag_execute_queue()) != ERROR_OK)
  967. {
  968. return retval;
  969. }
  970. command_print(CMD_CTX, "cp0 reg %" PRIi32 ", select %" PRIi32 ": %8.8" PRIx32,
  971. cp0_reg, cp0_sel, value);
  972. }
  973. else if (CMD_ARGC == 3)
  974. {
  975. uint32_t value;
  976. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
  977. if ((retval = mips32_cp0_write(ejtag_info, value, cp0_reg, cp0_sel)) != ERROR_OK)
  978. {
  979. command_print(CMD_CTX,
  980. "couldn't access cp0 reg %" PRIi32 ", select %" PRIi32,
  981. cp0_reg, cp0_sel);
  982. return ERROR_OK;
  983. }
  984. command_print(CMD_CTX, "cp0 reg %" PRIi32 ", select %" PRIi32 ": %8.8" PRIx32,
  985. cp0_reg, cp0_sel, value);
  986. }
  987. }
  988. return ERROR_OK;
  989. }
  990. static const struct command_registration mips_m4k_exec_command_handlers[] = {
  991. {
  992. .name = "cp0",
  993. .handler = mips_m4k_handle_cp0_command,
  994. .mode = COMMAND_EXEC,
  995. .usage = "regnum [value]",
  996. .help = "display/modify cp0 register",
  997. },
  998. COMMAND_REGISTRATION_DONE
  999. };
  1000. const struct command_registration mips_m4k_command_handlers[] = {
  1001. {
  1002. .chain = mips32_command_handlers,
  1003. },
  1004. {
  1005. .name = "mips_m4k",
  1006. .mode = COMMAND_ANY,
  1007. .help = "mips_m4k command group",
  1008. .chain = mips_m4k_exec_command_handlers,
  1009. },
  1010. COMMAND_REGISTRATION_DONE
  1011. };
  1012. struct target_type mips_m4k_target =
  1013. {
  1014. .name = "mips_m4k",
  1015. .poll = mips_m4k_poll,
  1016. .arch_state = mips32_arch_state,
  1017. .target_request_data = NULL,
  1018. .halt = mips_m4k_halt,
  1019. .resume = mips_m4k_resume,
  1020. .step = mips_m4k_step,
  1021. .assert_reset = mips_m4k_assert_reset,
  1022. .deassert_reset = mips_m4k_deassert_reset,
  1023. .soft_reset_halt = mips_m4k_soft_reset_halt,
  1024. .get_gdb_reg_list = mips32_get_gdb_reg_list,
  1025. .read_memory = mips_m4k_read_memory,
  1026. .write_memory = mips_m4k_write_memory,
  1027. .bulk_write_memory = mips_m4k_bulk_write_memory,
  1028. .checksum_memory = mips32_checksum_memory,
  1029. .blank_check_memory = mips32_blank_check_memory,
  1030. .run_algorithm = mips32_run_algorithm,
  1031. .add_breakpoint = mips_m4k_add_breakpoint,
  1032. .remove_breakpoint = mips_m4k_remove_breakpoint,
  1033. .add_watchpoint = mips_m4k_add_watchpoint,
  1034. .remove_watchpoint = mips_m4k_remove_watchpoint,
  1035. .commands = mips_m4k_command_handlers,
  1036. .target_create = mips_m4k_target_create,
  1037. .init_target = mips_m4k_init_target,
  1038. .examine = mips_m4k_examine,
  1039. };