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.
 
 
 
 
 
 

1425 lines
42 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, see <http://www.gnu.org/licenses/>. *
  24. ***************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "breakpoints.h"
  29. #include "mips32.h"
  30. #include "mips_m4k.h"
  31. #include "mips32_dmaacc.h"
  32. #include "target_type.h"
  33. #include "register.h"
  34. #include "smp.h"
  35. static void mips_m4k_enable_breakpoints(struct target *target);
  36. static void mips_m4k_enable_watchpoints(struct target *target);
  37. static int mips_m4k_set_breakpoint(struct target *target,
  38. struct breakpoint *breakpoint);
  39. static int mips_m4k_unset_breakpoint(struct target *target,
  40. struct breakpoint *breakpoint);
  41. static int mips_m4k_internal_restore(struct target *target, int current,
  42. target_addr_t address, int handle_breakpoints,
  43. int debug_execution);
  44. static int mips_m4k_halt(struct target *target);
  45. static int mips_m4k_bulk_write_memory(struct target *target, target_addr_t address,
  46. uint32_t count, const uint8_t *buffer);
  47. static int mips_m4k_examine_debug_reason(struct target *target)
  48. {
  49. struct mips32_common *mips32 = target_to_mips32(target);
  50. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  51. uint32_t break_status;
  52. int retval;
  53. if ((target->debug_reason != DBG_REASON_DBGRQ)
  54. && (target->debug_reason != DBG_REASON_SINGLESTEP)) {
  55. if (ejtag_info->debug_caps & EJTAG_DCR_IB) {
  56. /* get info about inst breakpoint support */
  57. retval = target_read_u32(target,
  58. ejtag_info->ejtag_ibs_addr, &break_status);
  59. if (retval != ERROR_OK)
  60. return retval;
  61. if (break_status & 0x1f) {
  62. /* we have halted on a breakpoint */
  63. retval = target_write_u32(target,
  64. ejtag_info->ejtag_ibs_addr, 0);
  65. if (retval != ERROR_OK)
  66. return retval;
  67. target->debug_reason = DBG_REASON_BREAKPOINT;
  68. }
  69. }
  70. if (ejtag_info->debug_caps & EJTAG_DCR_DB) {
  71. /* get info about data breakpoint support */
  72. retval = target_read_u32(target,
  73. ejtag_info->ejtag_dbs_addr, &break_status);
  74. if (retval != ERROR_OK)
  75. return retval;
  76. if (break_status & 0x1f) {
  77. /* we have halted on a breakpoint */
  78. retval = target_write_u32(target,
  79. ejtag_info->ejtag_dbs_addr, 0);
  80. if (retval != ERROR_OK)
  81. return retval;
  82. target->debug_reason = DBG_REASON_WATCHPOINT;
  83. }
  84. }
  85. }
  86. return ERROR_OK;
  87. }
  88. static int mips_m4k_debug_entry(struct target *target)
  89. {
  90. struct mips32_common *mips32 = target_to_mips32(target);
  91. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  92. mips32_save_context(target);
  93. /* make sure stepping disabled, SSt bit in CP0 debug register cleared */
  94. mips_ejtag_config_step(ejtag_info, 0);
  95. /* make sure break unit configured */
  96. mips32_configure_break_unit(target);
  97. /* attempt to find halt reason */
  98. mips_m4k_examine_debug_reason(target);
  99. mips32_read_config_regs(target);
  100. /* default to mips32 isa, it will be changed below if required */
  101. mips32->isa_mode = MIPS32_ISA_MIPS32;
  102. /* other than mips32 only and isa bit set ? */
  103. if (mips32->isa_imp && buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 1))
  104. mips32->isa_mode = mips32->isa_imp == 2 ? MIPS32_ISA_MIPS16E : MIPS32_ISA_MMIPS32;
  105. LOG_DEBUG("entered debug state at PC 0x%" PRIx32 ", target->state: %s",
  106. buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32),
  107. target_state_name(target));
  108. return ERROR_OK;
  109. }
  110. static struct target *get_mips_m4k(struct target *target, int32_t coreid)
  111. {
  112. struct target_list *head;
  113. struct target *curr;
  114. head = target->head;
  115. while (head != (struct target_list *)NULL) {
  116. curr = head->target;
  117. if ((curr->coreid == coreid) && (curr->state == TARGET_HALTED))
  118. return curr;
  119. head = head->next;
  120. }
  121. return target;
  122. }
  123. static int mips_m4k_halt_smp(struct target *target)
  124. {
  125. int retval = ERROR_OK;
  126. struct target_list *head;
  127. struct target *curr;
  128. head = target->head;
  129. while (head != (struct target_list *)NULL) {
  130. int ret = ERROR_OK;
  131. curr = head->target;
  132. if ((curr != target) && (curr->state != TARGET_HALTED))
  133. ret = mips_m4k_halt(curr);
  134. if (ret != ERROR_OK) {
  135. LOG_ERROR("halt failed target->coreid: %" PRId32, curr->coreid);
  136. retval = ret;
  137. }
  138. head = head->next;
  139. }
  140. return retval;
  141. }
  142. static int update_halt_gdb(struct target *target)
  143. {
  144. int retval = ERROR_OK;
  145. if (target->gdb_service->core[0] == -1) {
  146. target->gdb_service->target = target;
  147. target->gdb_service->core[0] = target->coreid;
  148. retval = mips_m4k_halt_smp(target);
  149. }
  150. return retval;
  151. }
  152. static int mips_m4k_poll(struct target *target)
  153. {
  154. int retval = ERROR_OK;
  155. struct mips32_common *mips32 = target_to_mips32(target);
  156. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  157. uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl;
  158. enum target_state prev_target_state = target->state;
  159. /* toggle to another core is done by gdb as follow */
  160. /* maint packet J core_id */
  161. /* continue */
  162. /* the next polling trigger an halt event sent to gdb */
  163. if ((target->state == TARGET_HALTED) && (target->smp) &&
  164. (target->gdb_service) &&
  165. (!target->gdb_service->target)) {
  166. target->gdb_service->target =
  167. get_mips_m4k(target, target->gdb_service->core[1]);
  168. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  169. return retval;
  170. }
  171. /* read ejtag control reg */
  172. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  173. retval = mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  174. if (retval != ERROR_OK)
  175. return retval;
  176. ejtag_info->isa = (ejtag_ctrl & EJTAG_CTRL_DBGISA) ? 1 : 0;
  177. /* clear this bit before handling polling
  178. * as after reset registers will read zero */
  179. if (ejtag_ctrl & EJTAG_CTRL_ROCC) {
  180. /* we have detected a reset, clear flag
  181. * otherwise ejtag will not work */
  182. ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_ROCC;
  183. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  184. retval = mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl);
  185. if (retval != ERROR_OK)
  186. return retval;
  187. LOG_DEBUG("Reset Detected");
  188. }
  189. /* check for processor halted */
  190. if (ejtag_ctrl & EJTAG_CTRL_BRKST) {
  191. if ((target->state != TARGET_HALTED)
  192. && (target->state != TARGET_DEBUG_RUNNING)) {
  193. if (target->state == TARGET_UNKNOWN)
  194. LOG_DEBUG("EJTAG_CTRL_BRKST already set during server startup.");
  195. /* OpenOCD was was probably started on the board with EJTAG_CTRL_BRKST already set
  196. * (maybe put on by HALT-ing the board in the previous session).
  197. *
  198. * Force enable debug entry for this session.
  199. */
  200. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT);
  201. target->state = TARGET_HALTED;
  202. retval = mips_m4k_debug_entry(target);
  203. if (retval != ERROR_OK)
  204. return retval;
  205. if (target->smp &&
  206. ((prev_target_state == TARGET_RUNNING)
  207. || (prev_target_state == TARGET_RESET))) {
  208. retval = update_halt_gdb(target);
  209. if (retval != ERROR_OK)
  210. return retval;
  211. }
  212. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  213. } else if (target->state == TARGET_DEBUG_RUNNING) {
  214. target->state = TARGET_HALTED;
  215. retval = mips_m4k_debug_entry(target);
  216. if (retval != ERROR_OK)
  217. return retval;
  218. if (target->smp) {
  219. retval = update_halt_gdb(target);
  220. if (retval != ERROR_OK)
  221. return retval;
  222. }
  223. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  224. }
  225. } else
  226. target->state = TARGET_RUNNING;
  227. /* LOG_DEBUG("ctrl = 0x%08X", ejtag_ctrl); */
  228. return ERROR_OK;
  229. }
  230. static int mips_m4k_halt(struct target *target)
  231. {
  232. struct mips32_common *mips32 = target_to_mips32(target);
  233. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  234. LOG_DEBUG("target->state: %s", target_state_name(target));
  235. if (target->state == TARGET_HALTED) {
  236. LOG_DEBUG("target was already halted");
  237. return ERROR_OK;
  238. }
  239. if (target->state == TARGET_UNKNOWN)
  240. LOG_WARNING("target was in unknown state when halt was requested");
  241. if (target->state == TARGET_RESET) {
  242. if ((jtag_get_reset_config() & RESET_SRST_PULLS_TRST) && jtag_get_srst()) {
  243. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  244. return ERROR_TARGET_FAILURE;
  245. } else {
  246. /* we came here in a reset_halt or reset_init sequence
  247. * debug entry was already prepared in mips_m4k_assert_reset()
  248. */
  249. target->debug_reason = DBG_REASON_DBGRQ;
  250. return ERROR_OK;
  251. }
  252. }
  253. /* break processor */
  254. mips_ejtag_enter_debug(ejtag_info);
  255. target->debug_reason = DBG_REASON_DBGRQ;
  256. return ERROR_OK;
  257. }
  258. static int mips_m4k_assert_reset(struct target *target)
  259. {
  260. struct mips_m4k_common *mips_m4k = target_to_m4k(target);
  261. struct mips_ejtag *ejtag_info = &mips_m4k->mips32.ejtag_info;
  262. /* TODO: apply hw reset signal in not examined state */
  263. if (!(target_was_examined(target))) {
  264. LOG_WARNING("Reset is not asserted because the target is not examined.");
  265. LOG_WARNING("Use a reset button or power cycle the target.");
  266. return ERROR_TARGET_NOT_EXAMINED;
  267. }
  268. LOG_DEBUG("target->state: %s",
  269. target_state_name(target));
  270. enum reset_types jtag_reset_config = jtag_get_reset_config();
  271. /* some cores support connecting while srst is asserted
  272. * use that mode is it has been configured */
  273. bool srst_asserted = false;
  274. if (!(jtag_reset_config & RESET_SRST_PULLS_TRST) &&
  275. (jtag_reset_config & RESET_SRST_NO_GATING)) {
  276. jtag_add_reset(0, 1);
  277. srst_asserted = true;
  278. }
  279. /* EJTAG before v2.5/2.6 does not support EJTAGBOOT or NORMALBOOT */
  280. if (ejtag_info->ejtag_version != EJTAG_VERSION_20) {
  281. if (target->reset_halt) {
  282. /* use hardware to catch reset */
  283. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_EJTAGBOOT);
  284. } else
  285. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_NORMALBOOT);
  286. }
  287. if (jtag_reset_config & RESET_HAS_SRST) {
  288. /* here we should issue a srst only, but we may have to assert trst as well */
  289. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  290. jtag_add_reset(1, 1);
  291. else if (!srst_asserted)
  292. jtag_add_reset(0, 1);
  293. } else if (target_has_event_action(target, TARGET_EVENT_RESET_ASSERT)) {
  294. target_handle_event(target, TARGET_EVENT_RESET_ASSERT);
  295. } else {
  296. if (mips_m4k->is_pic32mx) {
  297. LOG_DEBUG("Using MTAP reset to reset processor...");
  298. /* use microchip specific MTAP reset */
  299. mips_ejtag_set_instr(ejtag_info, MTAP_SW_MTAP);
  300. mips_ejtag_set_instr(ejtag_info, MTAP_COMMAND);
  301. mips_ejtag_drscan_8_out(ejtag_info, MCHP_ASERT_RST);
  302. mips_ejtag_drscan_8_out(ejtag_info, MCHP_DE_ASSERT_RST);
  303. mips_ejtag_set_instr(ejtag_info, MTAP_SW_ETAP);
  304. } else {
  305. /* use ejtag reset - not supported by all cores */
  306. uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl | EJTAG_CTRL_PRRST | EJTAG_CTRL_PERRST;
  307. LOG_DEBUG("Using EJTAG reset (PRRST) to reset processor...");
  308. mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL);
  309. mips_ejtag_drscan_32_out(ejtag_info, ejtag_ctrl);
  310. }
  311. }
  312. target->state = TARGET_RESET;
  313. jtag_add_sleep(50000);
  314. register_cache_invalidate(mips_m4k->mips32.core_cache);
  315. if (target->reset_halt) {
  316. int retval = target_halt(target);
  317. if (retval != ERROR_OK)
  318. return retval;
  319. }
  320. return ERROR_OK;
  321. }
  322. static int mips_m4k_deassert_reset(struct target *target)
  323. {
  324. LOG_DEBUG("target->state: %s", target_state_name(target));
  325. /* deassert reset lines */
  326. jtag_add_reset(0, 0);
  327. return ERROR_OK;
  328. }
  329. static int mips_m4k_single_step_core(struct target *target)
  330. {
  331. struct mips32_common *mips32 = target_to_mips32(target);
  332. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  333. /* configure single step mode */
  334. mips_ejtag_config_step(ejtag_info, 1);
  335. /* disable interrupts while stepping */
  336. mips32_enable_interrupts(target, 0);
  337. /* exit debug mode */
  338. mips_ejtag_exit_debug(ejtag_info);
  339. mips_m4k_debug_entry(target);
  340. return ERROR_OK;
  341. }
  342. static int mips_m4k_restore_smp(struct target *target, uint32_t address, int handle_breakpoints)
  343. {
  344. int retval = ERROR_OK;
  345. struct target_list *head;
  346. struct target *curr;
  347. head = target->head;
  348. while (head != (struct target_list *)NULL) {
  349. int ret = ERROR_OK;
  350. curr = head->target;
  351. if ((curr != target) && (curr->state != TARGET_RUNNING)) {
  352. /* resume current address , not in step mode */
  353. ret = mips_m4k_internal_restore(curr, 1, address,
  354. handle_breakpoints, 0);
  355. if (ret != ERROR_OK) {
  356. LOG_ERROR("target->coreid :%" PRId32 " failed to resume at address :0x%" PRIx32,
  357. curr->coreid, address);
  358. retval = ret;
  359. }
  360. }
  361. head = head->next;
  362. }
  363. return retval;
  364. }
  365. static int mips_m4k_internal_restore(struct target *target, int current,
  366. target_addr_t address, int handle_breakpoints, int debug_execution)
  367. {
  368. struct mips32_common *mips32 = target_to_mips32(target);
  369. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  370. struct breakpoint *breakpoint = NULL;
  371. uint32_t resume_pc;
  372. if (target->state != TARGET_HALTED) {
  373. LOG_WARNING("target not halted");
  374. return ERROR_TARGET_NOT_HALTED;
  375. }
  376. if (!debug_execution) {
  377. target_free_all_working_areas(target);
  378. mips_m4k_enable_breakpoints(target);
  379. mips_m4k_enable_watchpoints(target);
  380. }
  381. /* current = 1: continue on current pc, otherwise continue at <address> */
  382. if (!current) {
  383. mips_m4k_isa_filter(mips32->isa_imp, &address);
  384. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
  385. mips32->core_cache->reg_list[MIPS32_PC].dirty = true;
  386. mips32->core_cache->reg_list[MIPS32_PC].valid = true;
  387. }
  388. if ((mips32->isa_imp > 1) && debug_execution) /* if more than one isa supported */
  389. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 1, mips32->isa_mode);
  390. if (!current)
  391. resume_pc = address;
  392. else
  393. resume_pc = buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32);
  394. mips32_restore_context(target);
  395. /* the front-end may request us not to handle breakpoints */
  396. if (handle_breakpoints) {
  397. /* Single step past breakpoint at current address */
  398. breakpoint = breakpoint_find(target, resume_pc);
  399. if (breakpoint) {
  400. LOG_DEBUG("unset breakpoint at " TARGET_ADDR_FMT "",
  401. breakpoint->address);
  402. mips_m4k_unset_breakpoint(target, breakpoint);
  403. mips_m4k_single_step_core(target);
  404. mips_m4k_set_breakpoint(target, breakpoint);
  405. }
  406. }
  407. /* enable interrupts if we are running */
  408. mips32_enable_interrupts(target, !debug_execution);
  409. /* exit debug mode */
  410. mips_ejtag_exit_debug(ejtag_info);
  411. target->debug_reason = DBG_REASON_NOTHALTED;
  412. /* registers are now invalid */
  413. register_cache_invalidate(mips32->core_cache);
  414. if (!debug_execution) {
  415. target->state = TARGET_RUNNING;
  416. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  417. LOG_DEBUG("target resumed at 0x%" PRIx32 "", resume_pc);
  418. } else {
  419. target->state = TARGET_DEBUG_RUNNING;
  420. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  421. LOG_DEBUG("target debug resumed at 0x%" PRIx32 "", resume_pc);
  422. }
  423. return ERROR_OK;
  424. }
  425. static int mips_m4k_resume(struct target *target, int current,
  426. target_addr_t address, int handle_breakpoints, int debug_execution)
  427. {
  428. int retval = ERROR_OK;
  429. /* dummy resume for smp toggle in order to reduce gdb impact */
  430. if ((target->smp) && (target->gdb_service->core[1] != -1)) {
  431. /* simulate a start and halt of target */
  432. target->gdb_service->target = NULL;
  433. target->gdb_service->core[0] = target->gdb_service->core[1];
  434. /* fake resume at next poll we play the target core[1], see poll*/
  435. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  436. return retval;
  437. }
  438. retval = mips_m4k_internal_restore(target, current, address,
  439. handle_breakpoints,
  440. debug_execution);
  441. if (retval == ERROR_OK && target->smp) {
  442. target->gdb_service->core[0] = -1;
  443. retval = mips_m4k_restore_smp(target, address, handle_breakpoints);
  444. }
  445. return retval;
  446. }
  447. static int mips_m4k_step(struct target *target, int current,
  448. target_addr_t address, int handle_breakpoints)
  449. {
  450. /* get pointers to arch-specific information */
  451. struct mips32_common *mips32 = target_to_mips32(target);
  452. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  453. struct breakpoint *breakpoint = NULL;
  454. if (target->state != TARGET_HALTED) {
  455. LOG_WARNING("target not halted");
  456. return ERROR_TARGET_NOT_HALTED;
  457. }
  458. /* current = 1: continue on current pc, otherwise continue at <address> */
  459. if (!current) {
  460. mips_m4k_isa_filter(mips32->isa_imp, &address);
  461. buf_set_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32, address);
  462. mips32->core_cache->reg_list[MIPS32_PC].dirty = true;
  463. mips32->core_cache->reg_list[MIPS32_PC].valid = true;
  464. }
  465. /* the front-end may request us not to handle breakpoints */
  466. if (handle_breakpoints) {
  467. breakpoint = breakpoint_find(target,
  468. buf_get_u32(mips32->core_cache->reg_list[MIPS32_PC].value, 0, 32));
  469. if (breakpoint)
  470. mips_m4k_unset_breakpoint(target, breakpoint);
  471. }
  472. /* restore context */
  473. mips32_restore_context(target);
  474. /* configure single step mode */
  475. mips_ejtag_config_step(ejtag_info, 1);
  476. target->debug_reason = DBG_REASON_SINGLESTEP;
  477. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  478. /* disable interrupts while stepping */
  479. mips32_enable_interrupts(target, 0);
  480. /* exit debug mode */
  481. mips_ejtag_exit_debug(ejtag_info);
  482. /* registers are now invalid */
  483. register_cache_invalidate(mips32->core_cache);
  484. LOG_DEBUG("target stepped ");
  485. mips_m4k_debug_entry(target);
  486. if (breakpoint)
  487. mips_m4k_set_breakpoint(target, breakpoint);
  488. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  489. return ERROR_OK;
  490. }
  491. static void mips_m4k_enable_breakpoints(struct target *target)
  492. {
  493. struct breakpoint *breakpoint = target->breakpoints;
  494. /* set any pending breakpoints */
  495. while (breakpoint) {
  496. if (breakpoint->set == 0)
  497. mips_m4k_set_breakpoint(target, breakpoint);
  498. breakpoint = breakpoint->next;
  499. }
  500. }
  501. static int mips_m4k_set_breakpoint(struct target *target,
  502. struct breakpoint *breakpoint)
  503. {
  504. struct mips32_common *mips32 = target_to_mips32(target);
  505. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  506. struct mips32_comparator *comparator_list = mips32->inst_break_list;
  507. int retval;
  508. if (breakpoint->set) {
  509. LOG_WARNING("breakpoint already set");
  510. return ERROR_OK;
  511. }
  512. if (breakpoint->type == BKPT_HARD) {
  513. int bp_num = 0;
  514. while (comparator_list[bp_num].used && (bp_num < mips32->num_inst_bpoints))
  515. bp_num++;
  516. if (bp_num >= mips32->num_inst_bpoints) {
  517. LOG_ERROR("Can not find free FP Comparator(bpid: %" PRIu32 ")",
  518. breakpoint->unique_id);
  519. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  520. }
  521. breakpoint->set = bp_num + 1;
  522. comparator_list[bp_num].used = 1;
  523. comparator_list[bp_num].bp_value = breakpoint->address;
  524. if (breakpoint->length != 4) /* make sure isa bit set */
  525. comparator_list[bp_num].bp_value |= 1;
  526. else /* make sure isa bit cleared */
  527. comparator_list[bp_num].bp_value &= ~1;
  528. /* EJTAG 2.0 uses 30bit IBA. First 2 bits are reserved.
  529. * Warning: there is no IB ASID registers in 2.0.
  530. * Do not set it! :) */
  531. if (ejtag_info->ejtag_version == EJTAG_VERSION_20)
  532. comparator_list[bp_num].bp_value &= 0xFFFFFFFC;
  533. target_write_u32(target, comparator_list[bp_num].reg_address,
  534. comparator_list[bp_num].bp_value);
  535. target_write_u32(target, comparator_list[bp_num].reg_address +
  536. ejtag_info->ejtag_ibm_offs, 0x00000000);
  537. target_write_u32(target, comparator_list[bp_num].reg_address +
  538. ejtag_info->ejtag_ibc_offs, 1);
  539. LOG_DEBUG("bpid: %" PRIu32 ", bp_num %i bp_value 0x%" PRIx32 "",
  540. breakpoint->unique_id,
  541. bp_num, comparator_list[bp_num].bp_value);
  542. } else if (breakpoint->type == BKPT_SOFT) {
  543. LOG_DEBUG("bpid: %" PRIu32, breakpoint->unique_id);
  544. uint32_t isa_req = breakpoint->length & 1; /* micro mips request bit */
  545. uint32_t bplength = breakpoint->length & ~1; /* drop micro mips request bit for length */
  546. uint32_t bpaddr = breakpoint->address & ~1; /* drop isa bit from address, if set */
  547. if (bplength == 4) {
  548. uint32_t verify = 0xffffffff;
  549. uint32_t sdbbp32_instr = MIPS32_SDBBP(isa_req);
  550. if (ejtag_info->endianness && isa_req)
  551. sdbbp32_instr = SWAP16(sdbbp32_instr);
  552. if ((breakpoint->address & 3) == 0) { /* word aligned */
  553. retval = target_read_memory(target, bpaddr, bplength, 1, breakpoint->orig_instr);
  554. if (retval != ERROR_OK)
  555. return retval;
  556. retval = target_write_u32(target, bpaddr, sdbbp32_instr);
  557. if (retval != ERROR_OK)
  558. return retval;
  559. retval = target_read_u32(target, bpaddr, &verify);
  560. if (retval != ERROR_OK)
  561. return retval;
  562. if (verify != sdbbp32_instr)
  563. verify = 0;
  564. } else { /* 16 bit aligned */
  565. retval = target_read_memory(target, bpaddr, 2, 2, breakpoint->orig_instr);
  566. if (retval != ERROR_OK)
  567. return retval;
  568. uint8_t sdbbp_buf[4];
  569. target_buffer_set_u32(target, sdbbp_buf, sdbbp32_instr);
  570. retval = target_write_memory(target, bpaddr, 2, 2, sdbbp_buf);
  571. if (retval != ERROR_OK)
  572. return retval;
  573. retval = target_read_memory(target, bpaddr, 2, 2, sdbbp_buf);
  574. if (retval != ERROR_OK)
  575. return retval;
  576. if (target_buffer_get_u32(target, sdbbp_buf) != sdbbp32_instr)
  577. verify = 0;
  578. }
  579. if (verify == 0) {
  580. LOG_ERROR("Unable to set 32bit breakpoint at address %08" TARGET_PRIxADDR
  581. " - check that memory is read/writable", breakpoint->address);
  582. return ERROR_OK;
  583. }
  584. } else {
  585. uint16_t verify = 0xffff;
  586. retval = target_read_memory(target, bpaddr, bplength, 1, breakpoint->orig_instr);
  587. if (retval != ERROR_OK)
  588. return retval;
  589. retval = target_write_u16(target, bpaddr, MIPS16_SDBBP(isa_req));
  590. if (retval != ERROR_OK)
  591. return retval;
  592. retval = target_read_u16(target, bpaddr, &verify);
  593. if (retval != ERROR_OK)
  594. return retval;
  595. if (verify != MIPS16_SDBBP(isa_req)) {
  596. LOG_ERROR("Unable to set 16bit breakpoint at address %08" TARGET_PRIxADDR
  597. " - check that memory is read/writable", breakpoint->address);
  598. return ERROR_OK;
  599. }
  600. }
  601. breakpoint->set = 20; /* Any nice value but 0 */
  602. }
  603. return ERROR_OK;
  604. }
  605. static int mips_m4k_unset_breakpoint(struct target *target,
  606. struct breakpoint *breakpoint)
  607. {
  608. /* get pointers to arch-specific information */
  609. struct mips32_common *mips32 = target_to_mips32(target);
  610. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  611. struct mips32_comparator *comparator_list = mips32->inst_break_list;
  612. int retval;
  613. if (!breakpoint->set) {
  614. LOG_WARNING("breakpoint not set");
  615. return ERROR_OK;
  616. }
  617. if (breakpoint->type == BKPT_HARD) {
  618. int bp_num = breakpoint->set - 1;
  619. if ((bp_num < 0) || (bp_num >= mips32->num_inst_bpoints)) {
  620. LOG_DEBUG("Invalid FP Comparator number in breakpoint (bpid: %" PRIu32 ")",
  621. breakpoint->unique_id);
  622. return ERROR_OK;
  623. }
  624. LOG_DEBUG("bpid: %" PRIu32 " - releasing hw: %d",
  625. breakpoint->unique_id,
  626. bp_num);
  627. comparator_list[bp_num].used = 0;
  628. comparator_list[bp_num].bp_value = 0;
  629. target_write_u32(target, comparator_list[bp_num].reg_address +
  630. ejtag_info->ejtag_ibc_offs, 0);
  631. } else {
  632. /* restore original instruction (kept in target endianness) */
  633. uint32_t isa_req = breakpoint->length & 1;
  634. uint32_t bplength = breakpoint->length & ~1;
  635. uint8_t current_instr[4];
  636. LOG_DEBUG("bpid: %" PRIu32, breakpoint->unique_id);
  637. if (bplength == 4) {
  638. uint32_t sdbbp32_instr = MIPS32_SDBBP(isa_req);
  639. if (ejtag_info->endianness && isa_req)
  640. sdbbp32_instr = SWAP16(sdbbp32_instr);
  641. if ((breakpoint->address & 3) == 0) { /* 32bit aligned */
  642. /* check that user program has not modified breakpoint instruction */
  643. retval = target_read_memory(target, breakpoint->address, 4, 1, current_instr);
  644. if (retval != ERROR_OK)
  645. return retval;
  646. /**
  647. * target_read_memory() gets us data in _target_ endianness.
  648. * If we want to use this data on the host for comparisons with some macros
  649. * we must first transform it to _host_ endianness using target_buffer_get_u16().
  650. */
  651. if (sdbbp32_instr == target_buffer_get_u32(target, current_instr)) {
  652. retval = target_write_memory(target, breakpoint->address, 4, 1,
  653. breakpoint->orig_instr);
  654. if (retval != ERROR_OK)
  655. return retval;
  656. }
  657. } else { /* 16bit aligned */
  658. retval = target_read_memory(target, breakpoint->address, 2, 2, current_instr);
  659. if (retval != ERROR_OK)
  660. return retval;
  661. if (sdbbp32_instr == target_buffer_get_u32(target, current_instr)) {
  662. retval = target_write_memory(target, breakpoint->address, 2, 2,
  663. breakpoint->orig_instr);
  664. if (retval != ERROR_OK)
  665. return retval;
  666. }
  667. }
  668. } else {
  669. /* check that user program has not modified breakpoint instruction */
  670. retval = target_read_memory(target, breakpoint->address, 2, 1, current_instr);
  671. if (retval != ERROR_OK)
  672. return retval;
  673. if (target_buffer_get_u16(target, current_instr) == MIPS16_SDBBP(isa_req)) {
  674. retval = target_write_memory(target, breakpoint->address, 2, 1,
  675. breakpoint->orig_instr);
  676. if (retval != ERROR_OK)
  677. return retval;
  678. }
  679. }
  680. }
  681. breakpoint->set = 0;
  682. return ERROR_OK;
  683. }
  684. static int mips_m4k_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
  685. {
  686. struct mips32_common *mips32 = target_to_mips32(target);
  687. if ((breakpoint->length > 5 || breakpoint->length < 2) || /* out of range */
  688. (breakpoint->length == 4 && (breakpoint->address & 2)) || /* mips32 unaligned */
  689. (mips32->isa_imp == MIPS32_ONLY && breakpoint->length != 4) || /* misp32 specific */
  690. ((mips32->isa_imp & 1) != (breakpoint->length & 1))) /* isa not implemented */
  691. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  692. if (breakpoint->type == BKPT_HARD) {
  693. if (mips32->num_inst_bpoints_avail < 1) {
  694. LOG_INFO("no hardware breakpoint available");
  695. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  696. }
  697. mips32->num_inst_bpoints_avail--;
  698. }
  699. return mips_m4k_set_breakpoint(target, breakpoint);
  700. }
  701. static int mips_m4k_remove_breakpoint(struct target *target,
  702. struct breakpoint *breakpoint)
  703. {
  704. /* get pointers to arch-specific information */
  705. struct mips32_common *mips32 = target_to_mips32(target);
  706. if (target->state != TARGET_HALTED) {
  707. LOG_WARNING("target not halted");
  708. return ERROR_TARGET_NOT_HALTED;
  709. }
  710. if (breakpoint->set)
  711. mips_m4k_unset_breakpoint(target, breakpoint);
  712. if (breakpoint->type == BKPT_HARD)
  713. mips32->num_inst_bpoints_avail++;
  714. return ERROR_OK;
  715. }
  716. static int mips_m4k_set_watchpoint(struct target *target,
  717. struct watchpoint *watchpoint)
  718. {
  719. struct mips32_common *mips32 = target_to_mips32(target);
  720. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  721. struct mips32_comparator *comparator_list = mips32->data_break_list;
  722. int wp_num = 0;
  723. /*
  724. * watchpoint enabled, ignore all byte lanes in value register
  725. * and exclude both load and store accesses from watchpoint
  726. * condition evaluation
  727. */
  728. int enable = EJTAG_DBCN_NOSB | EJTAG_DBCN_NOLB | EJTAG_DBCN_BE |
  729. (0xff << EJTAG_DBCN_BLM_SHIFT);
  730. if (watchpoint->set) {
  731. LOG_WARNING("watchpoint already set");
  732. return ERROR_OK;
  733. }
  734. while (comparator_list[wp_num].used && (wp_num < mips32->num_data_bpoints))
  735. wp_num++;
  736. if (wp_num >= mips32->num_data_bpoints) {
  737. LOG_ERROR("Can not find free FP Comparator");
  738. return ERROR_FAIL;
  739. }
  740. if (watchpoint->length != 4) {
  741. LOG_ERROR("Only watchpoints of length 4 are supported");
  742. return ERROR_TARGET_UNALIGNED_ACCESS;
  743. }
  744. if (watchpoint->address % 4) {
  745. LOG_ERROR("Watchpoints address should be word aligned");
  746. return ERROR_TARGET_UNALIGNED_ACCESS;
  747. }
  748. switch (watchpoint->rw) {
  749. case WPT_READ:
  750. enable &= ~EJTAG_DBCN_NOLB;
  751. break;
  752. case WPT_WRITE:
  753. enable &= ~EJTAG_DBCN_NOSB;
  754. break;
  755. case WPT_ACCESS:
  756. enable &= ~(EJTAG_DBCN_NOLB | EJTAG_DBCN_NOSB);
  757. break;
  758. default:
  759. LOG_ERROR("BUG: watchpoint->rw neither read, write nor access");
  760. }
  761. watchpoint->set = wp_num + 1;
  762. comparator_list[wp_num].used = 1;
  763. comparator_list[wp_num].bp_value = watchpoint->address;
  764. /* EJTAG 2.0 uses 29bit DBA. First 3 bits are reserved.
  765. * There is as well no ASID register support. */
  766. if (ejtag_info->ejtag_version == EJTAG_VERSION_20)
  767. comparator_list[wp_num].bp_value &= 0xFFFFFFF8;
  768. else
  769. target_write_u32(target, comparator_list[wp_num].reg_address +
  770. ejtag_info->ejtag_dbasid_offs, 0x00000000);
  771. target_write_u32(target, comparator_list[wp_num].reg_address,
  772. comparator_list[wp_num].bp_value);
  773. target_write_u32(target, comparator_list[wp_num].reg_address +
  774. ejtag_info->ejtag_dbm_offs, 0x00000000);
  775. target_write_u32(target, comparator_list[wp_num].reg_address +
  776. ejtag_info->ejtag_dbc_offs, enable);
  777. /* TODO: probably this value is ignored on 2.0 */
  778. target_write_u32(target, comparator_list[wp_num].reg_address +
  779. ejtag_info->ejtag_dbv_offs, 0);
  780. LOG_DEBUG("wp_num %i bp_value 0x%" PRIx32 "", wp_num, comparator_list[wp_num].bp_value);
  781. return ERROR_OK;
  782. }
  783. static int mips_m4k_unset_watchpoint(struct target *target,
  784. struct watchpoint *watchpoint)
  785. {
  786. /* get pointers to arch-specific information */
  787. struct mips32_common *mips32 = target_to_mips32(target);
  788. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  789. struct mips32_comparator *comparator_list = mips32->data_break_list;
  790. if (!watchpoint->set) {
  791. LOG_WARNING("watchpoint not set");
  792. return ERROR_OK;
  793. }
  794. int wp_num = watchpoint->set - 1;
  795. if ((wp_num < 0) || (wp_num >= mips32->num_data_bpoints)) {
  796. LOG_DEBUG("Invalid FP Comparator number in watchpoint");
  797. return ERROR_OK;
  798. }
  799. comparator_list[wp_num].used = 0;
  800. comparator_list[wp_num].bp_value = 0;
  801. target_write_u32(target, comparator_list[wp_num].reg_address +
  802. ejtag_info->ejtag_dbc_offs, 0);
  803. watchpoint->set = 0;
  804. return ERROR_OK;
  805. }
  806. static int mips_m4k_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
  807. {
  808. struct mips32_common *mips32 = target_to_mips32(target);
  809. if (mips32->num_data_bpoints_avail < 1) {
  810. LOG_INFO("no hardware watchpoints available");
  811. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  812. }
  813. mips32->num_data_bpoints_avail--;
  814. mips_m4k_set_watchpoint(target, watchpoint);
  815. return ERROR_OK;
  816. }
  817. static int mips_m4k_remove_watchpoint(struct target *target,
  818. struct watchpoint *watchpoint)
  819. {
  820. /* get pointers to arch-specific information */
  821. struct mips32_common *mips32 = target_to_mips32(target);
  822. if (target->state != TARGET_HALTED) {
  823. LOG_WARNING("target not halted");
  824. return ERROR_TARGET_NOT_HALTED;
  825. }
  826. if (watchpoint->set)
  827. mips_m4k_unset_watchpoint(target, watchpoint);
  828. mips32->num_data_bpoints_avail++;
  829. return ERROR_OK;
  830. }
  831. static void mips_m4k_enable_watchpoints(struct target *target)
  832. {
  833. struct watchpoint *watchpoint = target->watchpoints;
  834. /* set any pending watchpoints */
  835. while (watchpoint) {
  836. if (watchpoint->set == 0)
  837. mips_m4k_set_watchpoint(target, watchpoint);
  838. watchpoint = watchpoint->next;
  839. }
  840. }
  841. static int mips_m4k_read_memory(struct target *target, target_addr_t address,
  842. uint32_t size, uint32_t count, uint8_t *buffer)
  843. {
  844. struct mips32_common *mips32 = target_to_mips32(target);
  845. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  846. LOG_DEBUG("address: " TARGET_ADDR_FMT ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
  847. address, size, count);
  848. if (target->state != TARGET_HALTED) {
  849. LOG_WARNING("target not halted");
  850. return ERROR_TARGET_NOT_HALTED;
  851. }
  852. /* sanitize arguments */
  853. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  854. return ERROR_COMMAND_SYNTAX_ERROR;
  855. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  856. return ERROR_TARGET_UNALIGNED_ACCESS;
  857. /* since we don't know if buffer is aligned, we allocate new mem that is always aligned */
  858. void *t = NULL;
  859. if (size > 1) {
  860. t = malloc(count * size * sizeof(uint8_t));
  861. if (!t) {
  862. LOG_ERROR("Out of memory");
  863. return ERROR_FAIL;
  864. }
  865. } else
  866. t = buffer;
  867. /* if noDMA off, use DMAACC mode for memory read */
  868. int retval;
  869. if (ejtag_info->impcode & EJTAG_IMP_NODMA)
  870. retval = mips32_pracc_read_mem(ejtag_info, address, size, count, t);
  871. else
  872. retval = mips32_dmaacc_read_mem(ejtag_info, address, size, count, t);
  873. /* mips32_..._read_mem with size 4/2 returns uint32_t/uint16_t in host */
  874. /* endianness, but byte array should represent target endianness */
  875. if (retval == ERROR_OK) {
  876. switch (size) {
  877. case 4:
  878. target_buffer_set_u32_array(target, buffer, count, t);
  879. break;
  880. case 2:
  881. target_buffer_set_u16_array(target, buffer, count, t);
  882. break;
  883. }
  884. }
  885. if (size > 1)
  886. free(t);
  887. return retval;
  888. }
  889. static int mips_m4k_write_memory(struct target *target, target_addr_t address,
  890. uint32_t size, uint32_t count, const uint8_t *buffer)
  891. {
  892. struct mips32_common *mips32 = target_to_mips32(target);
  893. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  894. LOG_DEBUG("address: " TARGET_ADDR_FMT ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
  895. address, size, count);
  896. if (target->state != TARGET_HALTED) {
  897. LOG_WARNING("target not halted");
  898. return ERROR_TARGET_NOT_HALTED;
  899. }
  900. if (size == 4 && count > 32) {
  901. int retval = mips_m4k_bulk_write_memory(target, address, count, buffer);
  902. if (retval == ERROR_OK)
  903. return ERROR_OK;
  904. LOG_WARNING("Falling back to non-bulk write");
  905. }
  906. /* sanitize arguments */
  907. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  908. return ERROR_COMMAND_SYNTAX_ERROR;
  909. if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
  910. return ERROR_TARGET_UNALIGNED_ACCESS;
  911. /** correct endianness if we have word or hword access */
  912. void *t = NULL;
  913. if (size > 1) {
  914. /* mips32_..._write_mem with size 4/2 requires uint32_t/uint16_t in host */
  915. /* endianness, but byte array represents target endianness */
  916. t = malloc(count * size * sizeof(uint8_t));
  917. if (!t) {
  918. LOG_ERROR("Out of memory");
  919. return ERROR_FAIL;
  920. }
  921. switch (size) {
  922. case 4:
  923. target_buffer_get_u32_array(target, buffer, count, (uint32_t *)t);
  924. break;
  925. case 2:
  926. target_buffer_get_u16_array(target, buffer, count, (uint16_t *)t);
  927. break;
  928. }
  929. buffer = t;
  930. }
  931. /* if noDMA off, use DMAACC mode for memory write */
  932. int retval;
  933. if (ejtag_info->impcode & EJTAG_IMP_NODMA)
  934. retval = mips32_pracc_write_mem(ejtag_info, address, size, count, buffer);
  935. else
  936. retval = mips32_dmaacc_write_mem(ejtag_info, address, size, count, buffer);
  937. free(t);
  938. if (retval != ERROR_OK)
  939. return retval;
  940. return ERROR_OK;
  941. }
  942. static int mips_m4k_init_target(struct command_context *cmd_ctx,
  943. struct target *target)
  944. {
  945. mips32_build_reg_cache(target);
  946. return ERROR_OK;
  947. }
  948. static int mips_m4k_init_arch_info(struct target *target,
  949. struct mips_m4k_common *mips_m4k, struct jtag_tap *tap)
  950. {
  951. struct mips32_common *mips32 = &mips_m4k->mips32;
  952. mips_m4k->common_magic = MIPSM4K_COMMON_MAGIC;
  953. /* initialize mips4k specific info */
  954. mips32_init_arch_info(target, mips32, tap);
  955. mips32->arch_info = mips_m4k;
  956. return ERROR_OK;
  957. }
  958. static int mips_m4k_target_create(struct target *target, Jim_Interp *interp)
  959. {
  960. struct mips_m4k_common *mips_m4k = calloc(1, sizeof(struct mips_m4k_common));
  961. mips_m4k_init_arch_info(target, mips_m4k, target->tap);
  962. return ERROR_OK;
  963. }
  964. static int mips_m4k_examine(struct target *target)
  965. {
  966. struct mips_m4k_common *mips_m4k = target_to_m4k(target);
  967. struct mips_ejtag *ejtag_info = &mips_m4k->mips32.ejtag_info;
  968. if (!target_was_examined(target)) {
  969. int retval = mips_ejtag_get_idcode(ejtag_info);
  970. if (retval != ERROR_OK) {
  971. LOG_ERROR("idcode read failed");
  972. return retval;
  973. }
  974. if (((ejtag_info->idcode >> 1) & 0x7FF) == 0x29) {
  975. /* we are using a pic32mx so select ejtag port
  976. * as it is not selected by default */
  977. mips_ejtag_set_instr(ejtag_info, MTAP_SW_ETAP);
  978. LOG_DEBUG("PIC32 Detected - using EJTAG Interface");
  979. mips_m4k->is_pic32mx = true;
  980. }
  981. }
  982. /* init rest of ejtag interface */
  983. int retval = mips_ejtag_init(ejtag_info);
  984. if (retval != ERROR_OK)
  985. return retval;
  986. return mips32_examine(target);
  987. }
  988. static int mips_m4k_bulk_write_memory(struct target *target, target_addr_t address,
  989. uint32_t count, const uint8_t *buffer)
  990. {
  991. struct mips32_common *mips32 = target_to_mips32(target);
  992. struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
  993. struct working_area *fast_data_area;
  994. int retval;
  995. int write_t = 1;
  996. LOG_DEBUG("address: " TARGET_ADDR_FMT ", count: 0x%8.8" PRIx32 "",
  997. address, count);
  998. /* check alignment */
  999. if (address & 0x3u)
  1000. return ERROR_TARGET_UNALIGNED_ACCESS;
  1001. if (!mips32->fast_data_area) {
  1002. /* Get memory for block write handler
  1003. * we preserve this area between calls and gain a speed increase
  1004. * of about 3kb/sec when writing flash
  1005. * this will be released/nulled by the system when the target is resumed or reset */
  1006. retval = target_alloc_working_area(target,
  1007. MIPS32_FASTDATA_HANDLER_SIZE,
  1008. &mips32->fast_data_area);
  1009. if (retval != ERROR_OK) {
  1010. LOG_ERROR("No working area available");
  1011. return retval;
  1012. }
  1013. /* reset fastadata state so the algo get reloaded */
  1014. ejtag_info->fast_access_save = -1;
  1015. }
  1016. fast_data_area = mips32->fast_data_area;
  1017. if (address <= fast_data_area->address + fast_data_area->size &&
  1018. fast_data_area->address <= address + count) {
  1019. LOG_ERROR("fast_data (" TARGET_ADDR_FMT ") is within write area "
  1020. "(" TARGET_ADDR_FMT "-" TARGET_ADDR_FMT ").",
  1021. fast_data_area->address, address, address + count);
  1022. LOG_ERROR("Change work-area-phys or load_image address!");
  1023. return ERROR_FAIL;
  1024. }
  1025. /* mips32_pracc_fastdata_xfer requires uint32_t in host endianness, */
  1026. /* but byte array represents target endianness */
  1027. uint32_t *t = NULL;
  1028. t = malloc(count * sizeof(uint32_t));
  1029. if (!t) {
  1030. LOG_ERROR("Out of memory");
  1031. return ERROR_FAIL;
  1032. }
  1033. target_buffer_get_u32_array(target, buffer, count, t);
  1034. retval = mips32_pracc_fastdata_xfer(ejtag_info, mips32->fast_data_area, write_t, address,
  1035. count, t);
  1036. free(t);
  1037. if (retval != ERROR_OK)
  1038. LOG_ERROR("Fastdata access Failed");
  1039. return retval;
  1040. }
  1041. static int mips_m4k_verify_pointer(struct command_invocation *cmd,
  1042. struct mips_m4k_common *mips_m4k)
  1043. {
  1044. if (mips_m4k->common_magic != MIPSM4K_COMMON_MAGIC) {
  1045. command_print(cmd, "target is not an MIPS_M4K");
  1046. return ERROR_TARGET_INVALID;
  1047. }
  1048. return ERROR_OK;
  1049. }
  1050. COMMAND_HANDLER(mips_m4k_handle_cp0_command)
  1051. {
  1052. int retval;
  1053. struct target *target = get_current_target(CMD_CTX);
  1054. struct mips_m4k_common *mips_m4k = target_to_m4k(target);
  1055. struct mips_ejtag *ejtag_info = &mips_m4k->mips32.ejtag_info;
  1056. retval = mips_m4k_verify_pointer(CMD, mips_m4k);
  1057. if (retval != ERROR_OK)
  1058. return retval;
  1059. if (target->state != TARGET_HALTED) {
  1060. command_print(CMD, "target must be stopped for \"%s\" command", CMD_NAME);
  1061. return ERROR_OK;
  1062. }
  1063. /* two or more argument, access a single register/select (write if third argument is given) */
  1064. if (CMD_ARGC < 2)
  1065. return ERROR_COMMAND_SYNTAX_ERROR;
  1066. else {
  1067. uint32_t cp0_reg, cp0_sel;
  1068. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], cp0_reg);
  1069. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], cp0_sel);
  1070. if (CMD_ARGC == 2) {
  1071. uint32_t value;
  1072. retval = mips32_cp0_read(ejtag_info, &value, cp0_reg, cp0_sel);
  1073. if (retval != ERROR_OK) {
  1074. command_print(CMD,
  1075. "couldn't access reg %" PRIu32,
  1076. cp0_reg);
  1077. return ERROR_OK;
  1078. }
  1079. command_print(CMD, "cp0 reg %" PRIu32 ", select %" PRIu32 ": %8.8" PRIx32,
  1080. cp0_reg, cp0_sel, value);
  1081. } else if (CMD_ARGC == 3) {
  1082. uint32_t value;
  1083. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
  1084. retval = mips32_cp0_write(ejtag_info, value, cp0_reg, cp0_sel);
  1085. if (retval != ERROR_OK) {
  1086. command_print(CMD,
  1087. "couldn't access cp0 reg %" PRIu32 ", select %" PRIu32,
  1088. cp0_reg, cp0_sel);
  1089. return ERROR_OK;
  1090. }
  1091. command_print(CMD, "cp0 reg %" PRIu32 ", select %" PRIu32 ": %8.8" PRIx32,
  1092. cp0_reg, cp0_sel, value);
  1093. }
  1094. }
  1095. return ERROR_OK;
  1096. }
  1097. COMMAND_HANDLER(mips_m4k_handle_scan_delay_command)
  1098. {
  1099. struct target *target = get_current_target(CMD_CTX);
  1100. struct mips_m4k_common *mips_m4k = target_to_m4k(target);
  1101. struct mips_ejtag *ejtag_info = &mips_m4k->mips32.ejtag_info;
  1102. if (CMD_ARGC == 1)
  1103. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], ejtag_info->scan_delay);
  1104. else if (CMD_ARGC > 1)
  1105. return ERROR_COMMAND_SYNTAX_ERROR;
  1106. command_print(CMD, "scan delay: %d nsec", ejtag_info->scan_delay);
  1107. if (ejtag_info->scan_delay >= MIPS32_SCAN_DELAY_LEGACY_MODE) {
  1108. ejtag_info->mode = 0;
  1109. command_print(CMD, "running in legacy mode");
  1110. } else {
  1111. ejtag_info->mode = 1;
  1112. command_print(CMD, "running in fast queued mode");
  1113. }
  1114. return ERROR_OK;
  1115. }
  1116. static const struct command_registration mips_m4k_exec_command_handlers[] = {
  1117. {
  1118. .name = "cp0",
  1119. .handler = mips_m4k_handle_cp0_command,
  1120. .mode = COMMAND_EXEC,
  1121. .usage = "regnum [value]",
  1122. .help = "display/modify cp0 register",
  1123. },
  1124. {
  1125. .name = "scan_delay",
  1126. .handler = mips_m4k_handle_scan_delay_command,
  1127. .mode = COMMAND_ANY,
  1128. .help = "display/set scan delay in nano seconds",
  1129. .usage = "[value]",
  1130. },
  1131. {
  1132. .chain = smp_command_handlers,
  1133. },
  1134. COMMAND_REGISTRATION_DONE
  1135. };
  1136. const struct command_registration mips_m4k_command_handlers[] = {
  1137. {
  1138. .chain = mips32_command_handlers,
  1139. },
  1140. {
  1141. .name = "mips_m4k",
  1142. .mode = COMMAND_ANY,
  1143. .help = "mips_m4k command group",
  1144. .usage = "",
  1145. .chain = mips_m4k_exec_command_handlers,
  1146. },
  1147. COMMAND_REGISTRATION_DONE
  1148. };
  1149. struct target_type mips_m4k_target = {
  1150. .name = "mips_m4k",
  1151. .poll = mips_m4k_poll,
  1152. .arch_state = mips32_arch_state,
  1153. .halt = mips_m4k_halt,
  1154. .resume = mips_m4k_resume,
  1155. .step = mips_m4k_step,
  1156. .assert_reset = mips_m4k_assert_reset,
  1157. .deassert_reset = mips_m4k_deassert_reset,
  1158. .get_gdb_reg_list = mips32_get_gdb_reg_list,
  1159. .read_memory = mips_m4k_read_memory,
  1160. .write_memory = mips_m4k_write_memory,
  1161. .checksum_memory = mips32_checksum_memory,
  1162. .blank_check_memory = mips32_blank_check_memory,
  1163. .run_algorithm = mips32_run_algorithm,
  1164. .add_breakpoint = mips_m4k_add_breakpoint,
  1165. .remove_breakpoint = mips_m4k_remove_breakpoint,
  1166. .add_watchpoint = mips_m4k_add_watchpoint,
  1167. .remove_watchpoint = mips_m4k_remove_watchpoint,
  1168. .commands = mips_m4k_command_handlers,
  1169. .target_create = mips_m4k_target_create,
  1170. .init_target = mips_m4k_init_target,
  1171. .examine = mips_m4k_examine,
  1172. };