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.
 
 
 
 
 
 

1666 lines
47 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2006 by Magnus Lundin *
  6. * lundin@mlu.mine.nu *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. * *
  26. * *
  27. * Cortex-M3(tm) TRM, ARM DDI 0337C *
  28. * *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include "cortex_m3.h"
  34. #include "target_request.h"
  35. #include "target_type.h"
  36. /* cli handling */
  37. int cortex_m3_register_commands(struct command_context_s *cmd_ctx);
  38. int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  39. /* forward declarations */
  40. void cortex_m3_enable_breakpoints(struct target_s *target);
  41. void cortex_m3_enable_watchpoints(struct target_s *target);
  42. int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp);
  43. int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
  44. int cortex_m3_quit(void);
  45. int cortex_m3_load_core_reg_u32(target_t *target, enum armv7m_regtype type, u32 num, u32 *value);
  46. int cortex_m3_store_core_reg_u32(target_t *target, enum armv7m_regtype type, u32 num, u32 value);
  47. int cortex_m3_target_request_data(target_t *target, u32 size, u8 *buffer);
  48. int cortex_m3_examine(struct target_s *target);
  49. #ifdef ARMV7_GDB_HACKS
  50. extern u8 armv7m_gdb_dummy_cpsr_value[];
  51. extern reg_t armv7m_gdb_dummy_cpsr_reg;
  52. #endif
  53. target_type_t cortexm3_target =
  54. {
  55. .name = "cortex_m3",
  56. .poll = cortex_m3_poll,
  57. .arch_state = armv7m_arch_state,
  58. .target_request_data = cortex_m3_target_request_data,
  59. .halt = cortex_m3_halt,
  60. .resume = cortex_m3_resume,
  61. .step = cortex_m3_step,
  62. .assert_reset = cortex_m3_assert_reset,
  63. .deassert_reset = cortex_m3_deassert_reset,
  64. .soft_reset_halt = cortex_m3_soft_reset_halt,
  65. .get_gdb_reg_list = armv7m_get_gdb_reg_list,
  66. .read_memory = cortex_m3_read_memory,
  67. .write_memory = cortex_m3_write_memory,
  68. .bulk_write_memory = cortex_m3_bulk_write_memory,
  69. .checksum_memory = armv7m_checksum_memory,
  70. .blank_check_memory = armv7m_blank_check_memory,
  71. .run_algorithm = armv7m_run_algorithm,
  72. .add_breakpoint = cortex_m3_add_breakpoint,
  73. .remove_breakpoint = cortex_m3_remove_breakpoint,
  74. .add_watchpoint = cortex_m3_add_watchpoint,
  75. .remove_watchpoint = cortex_m3_remove_watchpoint,
  76. .register_commands = cortex_m3_register_commands,
  77. .target_create = cortex_m3_target_create,
  78. .init_target = cortex_m3_init_target,
  79. .examine = cortex_m3_examine,
  80. .quit = cortex_m3_quit
  81. };
  82. int cortexm3_dap_read_coreregister_u32(swjdp_common_t *swjdp, u32 *value, int regnum)
  83. {
  84. int retval;
  85. u32 dcrdr;
  86. /* because the DCB_DCRDR is used for the emulated dcc channel
  87. * we gave to save/restore the DCB_DCRDR when used */
  88. mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
  89. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  90. /* mem_ap_write_u32(swjdp, DCB_DCRSR, regnum); */
  91. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  92. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum );
  93. /* mem_ap_read_u32(swjdp, DCB_DCRDR, value); */
  94. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  95. dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value );
  96. mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
  97. retval = swjdp_transaction_endcheck(swjdp);
  98. return retval;
  99. }
  100. int cortexm3_dap_write_coreregister_u32(swjdp_common_t *swjdp, u32 value, int regnum)
  101. {
  102. int retval;
  103. u32 dcrdr;
  104. /* because the DCB_DCRDR is used for the emulated dcc channel
  105. * we gave to save/restore the DCB_DCRDR when used */
  106. mem_ap_read_u32(swjdp, DCB_DCRDR, &dcrdr);
  107. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  108. /* mem_ap_write_u32(swjdp, DCB_DCRDR, core_regs[i]); */
  109. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  110. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRDR & 0xC), value );
  111. /* mem_ap_write_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR ); */
  112. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  113. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (DCB_DCRSR & 0xC), regnum | DCRSR_WnR );
  114. mem_ap_write_u32(swjdp, DCB_DCRDR, dcrdr);
  115. retval = swjdp_transaction_endcheck(swjdp);
  116. return retval;
  117. }
  118. int cortex_m3_write_debug_halt_mask(target_t *target, u32 mask_on, u32 mask_off)
  119. {
  120. /* get pointers to arch-specific information */
  121. armv7m_common_t *armv7m = target->arch_info;
  122. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  123. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  124. /* mask off status bits */
  125. cortex_m3->dcb_dhcsr &= ~((0xFFFF << 16) | mask_off);
  126. /* create new register mask */
  127. cortex_m3->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
  128. return mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, cortex_m3->dcb_dhcsr);
  129. }
  130. int cortex_m3_clear_halt(target_t *target)
  131. {
  132. /* get pointers to arch-specific information */
  133. armv7m_common_t *armv7m = target->arch_info;
  134. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  135. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  136. /* clear step if any */
  137. cortex_m3_write_debug_halt_mask(target, C_HALT, C_STEP);
  138. /* Read Debug Fault Status Register */
  139. mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
  140. /* Write Debug Fault Status Register to enable processing to resume ?? Try with and without this !! */
  141. mem_ap_write_atomic_u32(swjdp, NVIC_DFSR, cortex_m3->nvic_dfsr);
  142. LOG_DEBUG(" NVIC_DFSR 0x%x", cortex_m3->nvic_dfsr);
  143. return ERROR_OK;
  144. }
  145. int cortex_m3_single_step_core(target_t *target)
  146. {
  147. /* get pointers to arch-specific information */
  148. armv7m_common_t *armv7m = target->arch_info;
  149. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  150. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  151. u32 dhcsr_save;
  152. /* backup dhcsr reg */
  153. dhcsr_save = cortex_m3->dcb_dhcsr;
  154. /* mask interrupts if not done already */
  155. if (!(cortex_m3->dcb_dhcsr & C_MASKINTS))
  156. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_HALT | C_DEBUGEN);
  157. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_MASKINTS | C_STEP | C_DEBUGEN);
  158. LOG_DEBUG(" ");
  159. /* restore dhcsr reg */
  160. cortex_m3->dcb_dhcsr = dhcsr_save;
  161. cortex_m3_clear_halt(target);
  162. return ERROR_OK;
  163. }
  164. int cortex_m3_exec_opcode(target_t *target,u32 opcode, int len /* MODE, r0_invalue, &r0_outvalue */ )
  165. {
  166. /* get pointers to arch-specific information */
  167. armv7m_common_t *armv7m = target->arch_info;
  168. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  169. u32 savedram;
  170. int retvalue;
  171. mem_ap_read_u32(swjdp, 0x20000000, &savedram);
  172. mem_ap_write_u32(swjdp, 0x20000000, opcode);
  173. cortexm3_dap_write_coreregister_u32(swjdp, 0x20000000, 15);
  174. cortex_m3_single_step_core(target);
  175. armv7m->core_cache->reg_list[15].dirty = armv7m->core_cache->reg_list[15].valid;
  176. retvalue = mem_ap_write_atomic_u32(swjdp, 0x20000000, savedram);
  177. return retvalue;
  178. }
  179. #if 0
  180. /* Enable interrupts */
  181. int cortex_m3_cpsie(target_t *target, u32 IF)
  182. {
  183. return cortex_m3_exec_opcode(target, ARMV7M_T_CPSIE(IF), 2);
  184. }
  185. /* Disable interrupts */
  186. int cortex_m3_cpsid(target_t *target, u32 IF)
  187. {
  188. return cortex_m3_exec_opcode(target, ARMV7M_T_CPSID(IF), 2);
  189. }
  190. #endif
  191. int cortex_m3_endreset_event(target_t *target)
  192. {
  193. int i;
  194. u32 dcb_demcr;
  195. /* get pointers to arch-specific information */
  196. armv7m_common_t *armv7m = target->arch_info;
  197. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  198. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  199. cortex_m3_fp_comparator_t *fp_list = cortex_m3->fp_comparator_list;
  200. cortex_m3_dwt_comparator_t *dwt_list = cortex_m3->dwt_comparator_list;
  201. mem_ap_read_atomic_u32(swjdp, DCB_DEMCR, &dcb_demcr);
  202. LOG_DEBUG("DCB_DEMCR = 0x%8.8x",dcb_demcr);
  203. /* this regsiter is used for emulated dcc channel */
  204. mem_ap_write_u32(swjdp, DCB_DCRDR, 0);
  205. /* Enable debug requests */
  206. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  207. if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
  208. mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  209. /* clear any interrupt masking */
  210. cortex_m3_write_debug_halt_mask(target, 0, C_MASKINTS);
  211. /* Enable trace and dwt */
  212. mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
  213. /* Monitor bus faults */
  214. mem_ap_write_u32(swjdp, NVIC_SHCSR, SHCSR_BUSFAULTENA);
  215. /* Enable FPB */
  216. target_write_u32(target, FP_CTRL, 3);
  217. cortex_m3->fpb_enabled = 1;
  218. /* Restore FPB registers */
  219. for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
  220. {
  221. target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
  222. }
  223. /* Restore DWT registers */
  224. for (i = 0; i < cortex_m3->dwt_num_comp; i++)
  225. {
  226. target_write_u32(target, dwt_list[i].dwt_comparator_address, dwt_list[i].comp);
  227. target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x4, dwt_list[i].mask);
  228. target_write_u32(target, dwt_list[i].dwt_comparator_address | 0x8, dwt_list[i].function);
  229. }
  230. swjdp_transaction_endcheck(swjdp);
  231. armv7m_invalidate_core_regs(target);
  232. /* make sure we have latest dhcsr flags */
  233. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  234. return ERROR_OK;
  235. }
  236. int cortex_m3_examine_debug_reason(target_t *target)
  237. {
  238. /* get pointers to arch-specific information */
  239. armv7m_common_t *armv7m = target->arch_info;
  240. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  241. /* THIS IS NOT GOOD, TODO - better logic for detection of debug state reason */
  242. /* only check the debug reason if we don't know it already */
  243. if ((target->debug_reason != DBG_REASON_DBGRQ)
  244. && (target->debug_reason != DBG_REASON_SINGLESTEP))
  245. {
  246. /* INCOMPLETE */
  247. if (cortex_m3->nvic_dfsr & DFSR_BKPT)
  248. {
  249. target->debug_reason = DBG_REASON_BREAKPOINT;
  250. if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
  251. target->debug_reason = DBG_REASON_WPTANDBKPT;
  252. }
  253. else if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
  254. target->debug_reason = DBG_REASON_WATCHPOINT;
  255. }
  256. return ERROR_OK;
  257. }
  258. int cortex_m3_examine_exception_reason(target_t *target)
  259. {
  260. u32 shcsr, except_sr, cfsr = -1, except_ar = -1;
  261. /* get pointers to arch-specific information */
  262. armv7m_common_t *armv7m = target->arch_info;
  263. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  264. mem_ap_read_u32(swjdp, NVIC_SHCSR, &shcsr);
  265. switch (armv7m->exception_number)
  266. {
  267. case 2: /* NMI */
  268. break;
  269. case 3: /* Hard Fault */
  270. mem_ap_read_atomic_u32(swjdp, NVIC_HFSR, &except_sr);
  271. if (except_sr & 0x40000000)
  272. {
  273. mem_ap_read_u32(swjdp, NVIC_CFSR, &cfsr);
  274. }
  275. break;
  276. case 4: /* Memory Management */
  277. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  278. mem_ap_read_u32(swjdp, NVIC_MMFAR, &except_ar);
  279. break;
  280. case 5: /* Bus Fault */
  281. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  282. mem_ap_read_u32(swjdp, NVIC_BFAR, &except_ar);
  283. break;
  284. case 6: /* Usage Fault */
  285. mem_ap_read_u32(swjdp, NVIC_CFSR, &except_sr);
  286. break;
  287. case 11: /* SVCall */
  288. break;
  289. case 12: /* Debug Monitor */
  290. mem_ap_read_u32(swjdp, NVIC_DFSR, &except_sr);
  291. break;
  292. case 14: /* PendSV */
  293. break;
  294. case 15: /* SysTick */
  295. break;
  296. default:
  297. except_sr = 0;
  298. break;
  299. }
  300. swjdp_transaction_endcheck(swjdp);
  301. LOG_DEBUG("%s SHCSR 0x%x, SR 0x%x, CFSR 0x%x, AR 0x%x", armv7m_exception_string(armv7m->exception_number), \
  302. shcsr, except_sr, cfsr, except_ar);
  303. return ERROR_OK;
  304. }
  305. int cortex_m3_debug_entry(target_t *target)
  306. {
  307. int i;
  308. u32 xPSR;
  309. int retval;
  310. /* get pointers to arch-specific information */
  311. armv7m_common_t *armv7m = target->arch_info;
  312. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  313. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  314. LOG_DEBUG(" ");
  315. if (armv7m->pre_debug_entry)
  316. armv7m->pre_debug_entry(target);
  317. cortex_m3_clear_halt(target);
  318. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  319. if ((retval = armv7m->examine_debug_reason(target)) != ERROR_OK)
  320. return retval;
  321. /* Examine target state and mode */
  322. /* First load register acessible through core debug port*/
  323. for (i = 0; i < ARMV7M_PRIMASK; i++)
  324. {
  325. if (!armv7m->core_cache->reg_list[i].valid)
  326. armv7m->read_core_reg(target, i);
  327. }
  328. xPSR = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32);
  329. #ifdef ARMV7_GDB_HACKS
  330. /* copy real xpsr reg for gdb, setting thumb bit */
  331. buf_set_u32(armv7m_gdb_dummy_cpsr_value, 0, 32, xPSR);
  332. buf_set_u32(armv7m_gdb_dummy_cpsr_value, 5, 1, 1);
  333. armv7m_gdb_dummy_cpsr_reg.valid = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
  334. armv7m_gdb_dummy_cpsr_reg.dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty;
  335. #endif
  336. /* For IT instructions xPSR must be reloaded on resume and clear on debug exec */
  337. if (xPSR & 0xf00)
  338. {
  339. armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = armv7m->core_cache->reg_list[ARMV7M_xPSR].valid;
  340. cortex_m3_store_core_reg_u32(target, ARMV7M_REGISTER_CORE_GP, 16, xPSR &~ 0xff);
  341. }
  342. /* Now we can load SP core registers */
  343. for (i = ARMV7M_PRIMASK; i < ARMV7NUMCOREREGS; i++)
  344. {
  345. if (!armv7m->core_cache->reg_list[i].valid)
  346. armv7m->read_core_reg(target, i);
  347. }
  348. /* Are we in an exception handler */
  349. if (xPSR & 0x1FF)
  350. {
  351. armv7m->core_mode = ARMV7M_MODE_HANDLER;
  352. armv7m->exception_number = (xPSR & 0x1FF);
  353. }
  354. else
  355. {
  356. armv7m->core_mode = buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_CONTROL].value, 0, 1);
  357. armv7m->exception_number = 0;
  358. }
  359. if (armv7m->exception_number)
  360. {
  361. cortex_m3_examine_exception_reason(target);
  362. }
  363. LOG_DEBUG("entered debug state in core mode: %s at PC 0x%x, target->state: %s",
  364. armv7m_mode_strings[armv7m->core_mode],
  365. *(u32*)(armv7m->core_cache->reg_list[15].value),
  366. Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name);
  367. if (armv7m->post_debug_entry)
  368. armv7m->post_debug_entry(target);
  369. return ERROR_OK;
  370. }
  371. int cortex_m3_poll(target_t *target)
  372. {
  373. int retval;
  374. enum target_state prev_target_state = target->state;
  375. /* get pointers to arch-specific information */
  376. armv7m_common_t *armv7m = target->arch_info;
  377. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  378. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  379. /* Read from Debug Halting Control and Status Register */
  380. retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  381. if (retval != ERROR_OK)
  382. {
  383. target->state = TARGET_UNKNOWN;
  384. return retval;
  385. }
  386. if (cortex_m3->dcb_dhcsr & S_RESET_ST)
  387. {
  388. /* check if still in reset */
  389. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  390. if (cortex_m3->dcb_dhcsr & S_RESET_ST)
  391. {
  392. target->state = TARGET_RESET;
  393. return ERROR_OK;
  394. }
  395. }
  396. if (target->state == TARGET_RESET)
  397. {
  398. /* Cannot switch context while running so endreset is called with target->state == TARGET_RESET */
  399. LOG_DEBUG("Exit from reset with dcb_dhcsr 0x%x", cortex_m3->dcb_dhcsr);
  400. cortex_m3_endreset_event(target);
  401. target->state = TARGET_RUNNING;
  402. prev_target_state = TARGET_RUNNING;
  403. }
  404. if (cortex_m3->dcb_dhcsr & S_HALT)
  405. {
  406. target->state = TARGET_HALTED;
  407. if ((prev_target_state == TARGET_RUNNING) || (prev_target_state == TARGET_RESET))
  408. {
  409. if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
  410. return retval;
  411. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  412. }
  413. if (prev_target_state == TARGET_DEBUG_RUNNING)
  414. {
  415. LOG_DEBUG(" ");
  416. if ((retval = cortex_m3_debug_entry(target)) != ERROR_OK)
  417. return retval;
  418. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  419. }
  420. }
  421. /*
  422. if (cortex_m3->dcb_dhcsr & S_SLEEP)
  423. target->state = TARGET_SLEEP;
  424. */
  425. #if 0
  426. /* Read Debug Fault Status Register, added to figure out the lockup when running flashtest.script */
  427. mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
  428. LOG_DEBUG("dcb_dhcsr 0x%x, nvic_dfsr 0x%x, target->state: %s", cortex_m3->dcb_dhcsr, cortex_m3->nvic_dfsr, Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
  429. #endif
  430. return ERROR_OK;
  431. }
  432. int cortex_m3_halt(target_t *target)
  433. {
  434. LOG_DEBUG("target->state: %s",
  435. Jim_Nvp_value2name_simple(nvp_target_state, target->state )->name);
  436. if (target->state == TARGET_HALTED)
  437. {
  438. LOG_DEBUG("target was already halted");
  439. return ERROR_OK;
  440. }
  441. if (target->state == TARGET_UNKNOWN)
  442. {
  443. LOG_WARNING("target was in unknown state when halt was requested");
  444. }
  445. if (target->state == TARGET_RESET)
  446. {
  447. if ((jtag_reset_config & RESET_SRST_PULLS_TRST) && jtag_srst)
  448. {
  449. LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
  450. return ERROR_TARGET_FAILURE;
  451. }
  452. else
  453. {
  454. /* we came here in a reset_halt or reset_init sequence
  455. * debug entry was already prepared in cortex_m3_prepare_reset_halt()
  456. */
  457. target->debug_reason = DBG_REASON_DBGRQ;
  458. return ERROR_OK;
  459. }
  460. }
  461. /* Write to Debug Halting Control and Status Register */
  462. cortex_m3_write_debug_halt_mask(target, C_HALT, 0);
  463. target->debug_reason = DBG_REASON_DBGRQ;
  464. return ERROR_OK;
  465. }
  466. int cortex_m3_soft_reset_halt(struct target_s *target)
  467. {
  468. /* get pointers to arch-specific information */
  469. armv7m_common_t *armv7m = target->arch_info;
  470. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  471. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  472. u32 dcb_dhcsr = 0;
  473. int retval, timeout = 0;
  474. /* Enter debug state on reset, cf. end_reset_event() */
  475. mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  476. /* Request a reset */
  477. mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_VECTRESET);
  478. target->state = TARGET_RESET;
  479. /* registers are now invalid */
  480. armv7m_invalidate_core_regs(target);
  481. while (timeout < 100)
  482. {
  483. retval = mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
  484. if (retval == ERROR_OK)
  485. {
  486. mem_ap_read_atomic_u32(swjdp, NVIC_DFSR, &cortex_m3->nvic_dfsr);
  487. if ((dcb_dhcsr & S_HALT) && (cortex_m3->nvic_dfsr & DFSR_VCATCH))
  488. {
  489. LOG_DEBUG("system reset-halted, dcb_dhcsr 0x%x, nvic_dfsr 0x%x", dcb_dhcsr, cortex_m3->nvic_dfsr);
  490. cortex_m3_poll(target);
  491. return ERROR_OK;
  492. }
  493. else
  494. LOG_DEBUG("waiting for system reset-halt, dcb_dhcsr 0x%x, %i ms", dcb_dhcsr, timeout);
  495. }
  496. timeout++;
  497. alive_sleep(1);
  498. }
  499. return ERROR_OK;
  500. }
  501. int cortex_m3_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
  502. {
  503. /* get pointers to arch-specific information */
  504. armv7m_common_t *armv7m = target->arch_info;
  505. breakpoint_t *breakpoint = NULL;
  506. u32 resume_pc;
  507. if (target->state != TARGET_HALTED)
  508. {
  509. LOG_WARNING("target not halted");
  510. return ERROR_TARGET_NOT_HALTED;
  511. }
  512. if (!debug_execution)
  513. {
  514. target_free_all_working_areas(target);
  515. cortex_m3_enable_breakpoints(target);
  516. cortex_m3_enable_watchpoints(target);
  517. }
  518. if (debug_execution)
  519. {
  520. /* Disable interrupts */
  521. /* We disable interrupts in the PRIMASK register instead of masking with C_MASKINTS,
  522. * This is probably the same issue as Cortex-M3 Errata 377493:
  523. * C_MASKINTS in parallel with disabled interrupts can cause local faults to not be taken. */
  524. buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_PRIMASK].value, 0, 32, 1);
  525. armv7m->core_cache->reg_list[ARMV7M_PRIMASK].dirty = 1;
  526. armv7m->core_cache->reg_list[ARMV7M_PRIMASK].valid = 1;
  527. /* Make sure we are in Thumb mode */
  528. buf_set_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32,
  529. buf_get_u32(armv7m->core_cache->reg_list[ARMV7M_xPSR].value, 0, 32) | (1<<24));
  530. armv7m->core_cache->reg_list[ARMV7M_xPSR].dirty = 1;
  531. armv7m->core_cache->reg_list[ARMV7M_xPSR].valid = 1;
  532. }
  533. /* current = 1: continue on current pc, otherwise continue at <address> */
  534. if (!current)
  535. {
  536. buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
  537. armv7m->core_cache->reg_list[15].dirty = 1;
  538. armv7m->core_cache->reg_list[15].valid = 1;
  539. }
  540. resume_pc = buf_get_u32(armv7m->core_cache->reg_list[15].value, 0, 32);
  541. armv7m_restore_context(target);
  542. /* the front-end may request us not to handle breakpoints */
  543. if (handle_breakpoints)
  544. {
  545. /* Single step past breakpoint at current address */
  546. if ((breakpoint = breakpoint_find(target, resume_pc)))
  547. {
  548. LOG_DEBUG("unset breakpoint at 0x%8.8x", breakpoint->address);
  549. cortex_m3_unset_breakpoint(target, breakpoint);
  550. cortex_m3_single_step_core(target);
  551. cortex_m3_set_breakpoint(target, breakpoint);
  552. }
  553. }
  554. /* Restart core */
  555. cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
  556. target->debug_reason = DBG_REASON_NOTHALTED;
  557. /* registers are now invalid */
  558. armv7m_invalidate_core_regs(target);
  559. if (!debug_execution)
  560. {
  561. target->state = TARGET_RUNNING;
  562. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  563. LOG_DEBUG("target resumed at 0x%x", resume_pc);
  564. }
  565. else
  566. {
  567. target->state = TARGET_DEBUG_RUNNING;
  568. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  569. LOG_DEBUG("target debug resumed at 0x%x", resume_pc);
  570. }
  571. return ERROR_OK;
  572. }
  573. /* int irqstepcount=0; */
  574. int cortex_m3_step(struct target_s *target, int current, u32 address, int handle_breakpoints)
  575. {
  576. /* get pointers to arch-specific information */
  577. armv7m_common_t *armv7m = target->arch_info;
  578. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  579. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  580. breakpoint_t *breakpoint = NULL;
  581. if (target->state != TARGET_HALTED)
  582. {
  583. LOG_WARNING("target not halted");
  584. return ERROR_TARGET_NOT_HALTED;
  585. }
  586. /* current = 1: continue on current pc, otherwise continue at <address> */
  587. if (!current)
  588. buf_set_u32(armv7m->core_cache->reg_list[15].value, 0, 32, address);
  589. /* the front-end may request us not to handle breakpoints */
  590. if (handle_breakpoints)
  591. if ((breakpoint = breakpoint_find(target, buf_get_u32(armv7m->core_cache->reg_list[15].value, 0, 32))))
  592. cortex_m3_unset_breakpoint(target, breakpoint);
  593. target->debug_reason = DBG_REASON_SINGLESTEP;
  594. armv7m_restore_context(target);
  595. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  596. /* set step and clear halt */
  597. cortex_m3_write_debug_halt_mask(target, C_STEP, C_HALT);
  598. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  599. /* registers are now invalid */
  600. armv7m_invalidate_core_regs(target);
  601. if (breakpoint)
  602. cortex_m3_set_breakpoint(target, breakpoint);
  603. LOG_DEBUG("target stepped dcb_dhcsr = 0x%x nvic_icsr = 0x%x", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
  604. cortex_m3_debug_entry(target);
  605. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  606. LOG_DEBUG("target stepped dcb_dhcsr = 0x%x nvic_icsr = 0x%x", cortex_m3->dcb_dhcsr, cortex_m3->nvic_icsr);
  607. return ERROR_OK;
  608. }
  609. int cortex_m3_assert_reset(target_t *target)
  610. {
  611. armv7m_common_t *armv7m = target->arch_info;
  612. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  613. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  614. int assert_srst = 1;
  615. LOG_DEBUG("target->state: %s",
  616. Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
  617. if (!(jtag_reset_config & RESET_HAS_SRST))
  618. {
  619. LOG_ERROR("Can't assert SRST");
  620. return ERROR_FAIL;
  621. }
  622. /* Enable debug requests */
  623. mem_ap_read_atomic_u32(swjdp, DCB_DHCSR, &cortex_m3->dcb_dhcsr);
  624. if (!(cortex_m3->dcb_dhcsr & C_DEBUGEN))
  625. mem_ap_write_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN);
  626. mem_ap_write_u32(swjdp, DCB_DCRDR, 0 );
  627. if (!target->reset_halt)
  628. {
  629. /* Set/Clear C_MASKINTS in a separate operation */
  630. if (cortex_m3->dcb_dhcsr & C_MASKINTS)
  631. mem_ap_write_atomic_u32(swjdp, DCB_DHCSR, DBGKEY | C_DEBUGEN | C_HALT);
  632. /* clear any debug flags before resuming */
  633. cortex_m3_clear_halt(target);
  634. /* clear C_HALT in dhcsr reg */
  635. cortex_m3_write_debug_halt_mask(target, 0, C_HALT);
  636. /* Enter debug state on reset, cf. end_reset_event() */
  637. mem_ap_write_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR);
  638. }
  639. else
  640. {
  641. /* Enter debug state on reset, cf. end_reset_event() */
  642. mem_ap_write_atomic_u32(swjdp, DCB_DEMCR, TRCENA | VC_HARDERR | VC_BUSERR | VC_CORERESET);
  643. }
  644. /* following hack is to handle luminary reset
  645. * when srst is asserted the luminary device seesm to also clear the debug registers
  646. * which does not match the armv7 debug TRM */
  647. if (strcmp(target->variant, "lm3s") == 0)
  648. {
  649. /* get revision of lm3s target, only early silicon has this issue
  650. * Fury Rev B, DustDevil Rev B, Tempest all ok */
  651. u32 did0;
  652. if (target_read_u32(target, 0x400fe000, &did0) == ERROR_OK)
  653. {
  654. switch ((did0 >> 16) & 0xff)
  655. {
  656. case 0:
  657. /* all Sandstorm suffer issue */
  658. assert_srst = 0;
  659. break;
  660. case 1:
  661. case 3:
  662. /* only Fury/DustDevil rev A suffer reset problems */
  663. if (((did0 >> 8) & 0xff) == 0)
  664. assert_srst = 0;
  665. break;
  666. }
  667. }
  668. }
  669. if (assert_srst)
  670. {
  671. /* default to asserting srst */
  672. if (jtag_reset_config & RESET_SRST_PULLS_TRST)
  673. {
  674. jtag_add_reset(1, 1);
  675. }
  676. else
  677. {
  678. jtag_add_reset(0, 1);
  679. }
  680. }
  681. else
  682. {
  683. /* this causes the luminary device to reset using the watchdog */
  684. mem_ap_write_atomic_u32(swjdp, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
  685. LOG_DEBUG("Using Luminary Reset: SYSRESETREQ");
  686. {
  687. /* I do not know why this is necessary, but it fixes strange effects
  688. * (step/resume cause a NMI after reset) on LM3S6918 -- Michael Schwingen */
  689. u32 tmp;
  690. mem_ap_read_atomic_u32(swjdp, NVIC_AIRCR, &tmp);
  691. }
  692. }
  693. target->state = TARGET_RESET;
  694. jtag_add_sleep(50000);
  695. armv7m_invalidate_core_regs(target);
  696. if (target->reset_halt)
  697. {
  698. int retval;
  699. if ((retval = target_halt(target))!=ERROR_OK)
  700. return retval;
  701. }
  702. return ERROR_OK;
  703. }
  704. int cortex_m3_deassert_reset(target_t *target)
  705. {
  706. LOG_DEBUG("target->state: %s",
  707. Jim_Nvp_value2name_simple(nvp_target_state, target->state )->name);
  708. /* deassert reset lines */
  709. jtag_add_reset(0, 0);
  710. return ERROR_OK;
  711. }
  712. void cortex_m3_enable_breakpoints(struct target_s *target)
  713. {
  714. breakpoint_t *breakpoint = target->breakpoints;
  715. /* set any pending breakpoints */
  716. while (breakpoint)
  717. {
  718. if (breakpoint->set == 0)
  719. cortex_m3_set_breakpoint(target, breakpoint);
  720. breakpoint = breakpoint->next;
  721. }
  722. }
  723. int cortex_m3_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  724. {
  725. int retval;
  726. int fp_num=0;
  727. u32 hilo;
  728. /* get pointers to arch-specific information */
  729. armv7m_common_t *armv7m = target->arch_info;
  730. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  731. cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
  732. if (breakpoint->set)
  733. {
  734. LOG_WARNING("breakpoint already set");
  735. return ERROR_OK;
  736. }
  737. if (cortex_m3->auto_bp_type)
  738. {
  739. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  740. }
  741. if (breakpoint->type == BKPT_HARD)
  742. {
  743. while(comparator_list[fp_num].used && (fp_num < cortex_m3->fp_num_code))
  744. fp_num++;
  745. if (fp_num >= cortex_m3->fp_num_code)
  746. {
  747. LOG_DEBUG("ERROR Can not find free FP Comparator");
  748. LOG_WARNING("ERROR Can not find free FP Comparator");
  749. exit(-1);
  750. }
  751. breakpoint->set = fp_num + 1;
  752. hilo = (breakpoint->address & 0x2) ? FPCR_REPLACE_BKPT_HIGH : FPCR_REPLACE_BKPT_LOW;
  753. comparator_list[fp_num].used = 1;
  754. comparator_list[fp_num].fpcr_value = (breakpoint->address & 0x1FFFFFFC) | hilo | 1;
  755. target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
  756. LOG_DEBUG("fpc_num %i fpcr_value 0x%x", fp_num, comparator_list[fp_num].fpcr_value);
  757. if (!cortex_m3->fpb_enabled)
  758. {
  759. LOG_DEBUG("FPB wasn't enabled, do it now");
  760. target_write_u32(target, FP_CTRL, 3);
  761. }
  762. }
  763. else if (breakpoint->type == BKPT_SOFT)
  764. {
  765. u8 code[4];
  766. buf_set_u32(code, 0, 32, ARMV7M_T_BKPT(0x11));
  767. if((retval = target_read_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, breakpoint->orig_instr)) != ERROR_OK)
  768. {
  769. return retval;
  770. }
  771. if((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, breakpoint->length, 1, code)) != ERROR_OK)
  772. {
  773. return retval;
  774. }
  775. breakpoint->set = 0x11; /* Any nice value but 0 */
  776. }
  777. return ERROR_OK;
  778. }
  779. int cortex_m3_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  780. {
  781. int retval;
  782. /* get pointers to arch-specific information */
  783. armv7m_common_t *armv7m = target->arch_info;
  784. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  785. cortex_m3_fp_comparator_t * comparator_list = cortex_m3->fp_comparator_list;
  786. if (!breakpoint->set)
  787. {
  788. LOG_WARNING("breakpoint not set");
  789. return ERROR_OK;
  790. }
  791. if (breakpoint->type == BKPT_HARD)
  792. {
  793. int fp_num = breakpoint->set - 1;
  794. if ((fp_num < 0) || (fp_num >= cortex_m3->fp_num_code))
  795. {
  796. LOG_DEBUG("Invalid FP Comparator number in breakpoint");
  797. return ERROR_OK;
  798. }
  799. comparator_list[fp_num].used = 0;
  800. comparator_list[fp_num].fpcr_value = 0;
  801. target_write_u32(target, comparator_list[fp_num].fpcr_address, comparator_list[fp_num].fpcr_value);
  802. }
  803. else
  804. {
  805. /* restore original instruction (kept in target endianness) */
  806. if (breakpoint->length == 4)
  807. {
  808. if((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
  809. {
  810. return retval;
  811. }
  812. }
  813. else
  814. {
  815. if((retval = target_write_memory(target, breakpoint->address & 0xFFFFFFFE, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
  816. {
  817. return retval;
  818. }
  819. }
  820. }
  821. breakpoint->set = 0;
  822. return ERROR_OK;
  823. }
  824. int cortex_m3_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  825. {
  826. /* get pointers to arch-specific information */
  827. armv7m_common_t *armv7m = target->arch_info;
  828. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  829. if (cortex_m3->auto_bp_type)
  830. {
  831. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  832. #ifdef ARMV7_GDB_HACKS
  833. if (breakpoint->length != 2) {
  834. /* XXX Hack: Replace all breakpoints with length != 2 with
  835. * a hardware breakpoint. */
  836. breakpoint->type = BKPT_HARD;
  837. breakpoint->length = 2;
  838. }
  839. #endif
  840. }
  841. if ((breakpoint->type == BKPT_HARD) && (breakpoint->address >= 0x20000000))
  842. {
  843. LOG_INFO("flash patch comparator requested outside code memory region");
  844. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  845. }
  846. if ((breakpoint->type == BKPT_SOFT) && (breakpoint->address < 0x20000000))
  847. {
  848. LOG_INFO("soft breakpoint requested in code (flash) memory region");
  849. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  850. }
  851. if ((breakpoint->type == BKPT_HARD) && (cortex_m3->fp_code_available < 1))
  852. {
  853. LOG_INFO("no flash patch comparator unit available for hardware breakpoint");
  854. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  855. }
  856. if ((breakpoint->length != 2))
  857. {
  858. LOG_INFO("only breakpoints of two bytes length supported");
  859. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  860. }
  861. if (breakpoint->type == BKPT_HARD)
  862. cortex_m3->fp_code_available--;
  863. cortex_m3_set_breakpoint(target, breakpoint);
  864. return ERROR_OK;
  865. }
  866. int cortex_m3_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
  867. {
  868. /* get pointers to arch-specific information */
  869. armv7m_common_t *armv7m = target->arch_info;
  870. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  871. if (target->state != TARGET_HALTED)
  872. {
  873. LOG_WARNING("target not halted");
  874. return ERROR_TARGET_NOT_HALTED;
  875. }
  876. if (cortex_m3->auto_bp_type)
  877. {
  878. breakpoint->type = (breakpoint->address < 0x20000000) ? BKPT_HARD : BKPT_SOFT;
  879. }
  880. if (breakpoint->set)
  881. {
  882. cortex_m3_unset_breakpoint(target, breakpoint);
  883. }
  884. if (breakpoint->type == BKPT_HARD)
  885. cortex_m3->fp_code_available++;
  886. return ERROR_OK;
  887. }
  888. int cortex_m3_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  889. {
  890. int dwt_num=0;
  891. u32 mask, temp;
  892. /* get pointers to arch-specific information */
  893. armv7m_common_t *armv7m = target->arch_info;
  894. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  895. cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
  896. if (watchpoint->set)
  897. {
  898. LOG_WARNING("watchpoint already set");
  899. return ERROR_OK;
  900. }
  901. if (watchpoint->mask == 0xffffffffu)
  902. {
  903. while(comparator_list[dwt_num].used && (dwt_num < cortex_m3->dwt_num_comp))
  904. dwt_num++;
  905. if (dwt_num >= cortex_m3->dwt_num_comp)
  906. {
  907. LOG_DEBUG("ERROR Can not find free DWT Comparator");
  908. LOG_WARNING("ERROR Can not find free DWT Comparator");
  909. return -1;
  910. }
  911. watchpoint->set = dwt_num + 1;
  912. mask = 0;
  913. temp = watchpoint->length;
  914. while (temp > 1)
  915. {
  916. temp = temp / 2;
  917. mask++;
  918. }
  919. comparator_list[dwt_num].used = 1;
  920. comparator_list[dwt_num].comp = watchpoint->address;
  921. comparator_list[dwt_num].mask = mask;
  922. comparator_list[dwt_num].function = watchpoint->rw + 5;
  923. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address, comparator_list[dwt_num].comp);
  924. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address|0x4, comparator_list[dwt_num].mask);
  925. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address|0x8, comparator_list[dwt_num].function);
  926. LOG_DEBUG("dwt_num %i 0x%x 0x%x 0x%x", dwt_num, comparator_list[dwt_num].comp, comparator_list[dwt_num].mask, comparator_list[dwt_num].function);
  927. }
  928. else
  929. {
  930. LOG_WARNING("Cannot watch data values"); /* Move this test to add_watchpoint */
  931. return ERROR_OK;
  932. }
  933. return ERROR_OK;
  934. }
  935. int cortex_m3_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  936. {
  937. /* get pointers to arch-specific information */
  938. armv7m_common_t *armv7m = target->arch_info;
  939. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  940. cortex_m3_dwt_comparator_t * comparator_list = cortex_m3->dwt_comparator_list;
  941. int dwt_num;
  942. if (!watchpoint->set)
  943. {
  944. LOG_WARNING("watchpoint not set");
  945. return ERROR_OK;
  946. }
  947. dwt_num = watchpoint->set - 1;
  948. if ((dwt_num < 0) || (dwt_num >= cortex_m3->dwt_num_comp))
  949. {
  950. LOG_DEBUG("Invalid DWT Comparator number in watchpoint");
  951. return ERROR_OK;
  952. }
  953. comparator_list[dwt_num].used = 0;
  954. comparator_list[dwt_num].function = 0;
  955. target_write_u32(target, comparator_list[dwt_num].dwt_comparator_address|0x8, comparator_list[dwt_num].function);
  956. watchpoint->set = 0;
  957. return ERROR_OK;
  958. }
  959. int cortex_m3_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  960. {
  961. /* get pointers to arch-specific information */
  962. armv7m_common_t *armv7m = target->arch_info;
  963. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  964. if (target->state != TARGET_HALTED)
  965. {
  966. LOG_WARNING("target not halted");
  967. return ERROR_TARGET_NOT_HALTED;
  968. }
  969. if (cortex_m3->dwt_comp_available < 1)
  970. {
  971. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  972. }
  973. if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
  974. {
  975. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  976. }
  977. cortex_m3->dwt_comp_available--;
  978. return ERROR_OK;
  979. }
  980. int cortex_m3_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
  981. {
  982. /* get pointers to arch-specific information */
  983. armv7m_common_t *armv7m = target->arch_info;
  984. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  985. if (target->state != TARGET_HALTED)
  986. {
  987. LOG_WARNING("target not halted");
  988. return ERROR_TARGET_NOT_HALTED;
  989. }
  990. if (watchpoint->set)
  991. {
  992. cortex_m3_unset_watchpoint(target, watchpoint);
  993. }
  994. cortex_m3->dwt_comp_available++;
  995. return ERROR_OK;
  996. }
  997. void cortex_m3_enable_watchpoints(struct target_s *target)
  998. {
  999. watchpoint_t *watchpoint = target->watchpoints;
  1000. /* set any pending watchpoints */
  1001. while (watchpoint)
  1002. {
  1003. if (watchpoint->set == 0)
  1004. cortex_m3_set_watchpoint(target, watchpoint);
  1005. watchpoint = watchpoint->next;
  1006. }
  1007. }
  1008. int cortex_m3_load_core_reg_u32(struct target_s *target, enum armv7m_regtype type, u32 num, u32 * value)
  1009. {
  1010. int retval;
  1011. /* get pointers to arch-specific information */
  1012. armv7m_common_t *armv7m = target->arch_info;
  1013. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1014. if ((type == ARMV7M_REGISTER_CORE_GP) && (num <= ARMV7M_PSP))
  1015. {
  1016. /* read a normal core register */
  1017. retval = cortexm3_dap_read_coreregister_u32(swjdp, value, num);
  1018. if (retval != ERROR_OK)
  1019. {
  1020. LOG_ERROR("JTAG failure %i",retval);
  1021. return ERROR_JTAG_DEVICE_ERROR;
  1022. }
  1023. LOG_DEBUG("load from core reg %i value 0x%x",num,*value);
  1024. }
  1025. else if (type == ARMV7M_REGISTER_CORE_SP) /* Special purpose core register */
  1026. {
  1027. /* read other registers */
  1028. cortexm3_dap_read_coreregister_u32(swjdp, value, 20);
  1029. switch (num)
  1030. {
  1031. case 19:
  1032. *value = buf_get_u32((u8*)value, 0, 8);
  1033. break;
  1034. case 20:
  1035. *value = buf_get_u32((u8*)value, 8, 8);
  1036. break;
  1037. case 21:
  1038. *value = buf_get_u32((u8*)value, 16, 8);
  1039. break;
  1040. case 22:
  1041. *value = buf_get_u32((u8*)value, 24, 8);
  1042. break;
  1043. }
  1044. LOG_DEBUG("load from special reg %i value 0x%x", num, *value);
  1045. }
  1046. else
  1047. {
  1048. return ERROR_INVALID_ARGUMENTS;
  1049. }
  1050. return ERROR_OK;
  1051. }
  1052. int cortex_m3_store_core_reg_u32(struct target_s *target, enum armv7m_regtype type, u32 num, u32 value)
  1053. {
  1054. int retval;
  1055. u32 reg;
  1056. /* get pointers to arch-specific information */
  1057. armv7m_common_t *armv7m = target->arch_info;
  1058. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1059. #ifdef ARMV7_GDB_HACKS
  1060. /* If the LR register is being modified, make sure it will put us
  1061. * in "thumb" mode, or an INVSTATE exception will occur. This is a
  1062. * hack to deal with the fact that gdb will sometimes "forge"
  1063. * return addresses, and doesn't set the LSB correctly (i.e., when
  1064. * printing expressions containing function calls, it sets LR=0.) */
  1065. if (num == 14)
  1066. value |= 0x01;
  1067. #endif
  1068. if ((type == ARMV7M_REGISTER_CORE_GP) && (num <= ARMV7M_PSP))
  1069. {
  1070. retval = cortexm3_dap_write_coreregister_u32(swjdp, value, num);
  1071. if (retval != ERROR_OK)
  1072. {
  1073. LOG_ERROR("JTAG failure %i", retval);
  1074. armv7m->core_cache->reg_list[num].dirty = armv7m->core_cache->reg_list[num].valid;
  1075. return ERROR_JTAG_DEVICE_ERROR;
  1076. }
  1077. LOG_DEBUG("write core reg %i value 0x%x", num, value);
  1078. }
  1079. else if (type == ARMV7M_REGISTER_CORE_SP) /* Special purpose core register */
  1080. {
  1081. /* write other registers */
  1082. cortexm3_dap_read_coreregister_u32(swjdp, &reg, 20);
  1083. switch (num)
  1084. {
  1085. case 19:
  1086. buf_set_u32((u8*)&reg, 0, 8, value);
  1087. break;
  1088. case 20:
  1089. buf_set_u32((u8*)&reg, 8, 8, value);
  1090. break;
  1091. case 21:
  1092. buf_set_u32((u8*)&reg, 16, 8, value);
  1093. break;
  1094. case 22:
  1095. buf_set_u32((u8*)&reg, 24, 8, value);
  1096. break;
  1097. }
  1098. cortexm3_dap_write_coreregister_u32(swjdp, reg, 20);
  1099. LOG_DEBUG("write special reg %i value 0x%x ", num, value);
  1100. }
  1101. else
  1102. {
  1103. return ERROR_INVALID_ARGUMENTS;
  1104. }
  1105. return ERROR_OK;
  1106. }
  1107. int cortex_m3_read_memory(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
  1108. {
  1109. /* get pointers to arch-specific information */
  1110. armv7m_common_t *armv7m = target->arch_info;
  1111. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1112. int retval;
  1113. /* sanitize arguments */
  1114. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  1115. return ERROR_INVALID_ARGUMENTS;
  1116. /* cortex_m3 handles unaligned memory access */
  1117. switch (size)
  1118. {
  1119. case 4:
  1120. retval = mem_ap_read_buf_u32(swjdp, buffer, 4 * count, address);
  1121. break;
  1122. case 2:
  1123. retval = mem_ap_read_buf_u16(swjdp, buffer, 2 * count, address);
  1124. break;
  1125. case 1:
  1126. retval = mem_ap_read_buf_u8(swjdp, buffer, count, address);
  1127. break;
  1128. default:
  1129. LOG_ERROR("BUG: we shouldn't get here");
  1130. exit(-1);
  1131. }
  1132. return retval;
  1133. }
  1134. int cortex_m3_write_memory(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
  1135. {
  1136. /* get pointers to arch-specific information */
  1137. armv7m_common_t *armv7m = target->arch_info;
  1138. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1139. int retval;
  1140. /* sanitize arguments */
  1141. if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
  1142. return ERROR_INVALID_ARGUMENTS;
  1143. switch (size)
  1144. {
  1145. case 4:
  1146. retval = mem_ap_write_buf_u32(swjdp, buffer, 4 * count, address);
  1147. break;
  1148. case 2:
  1149. retval = mem_ap_write_buf_u16(swjdp, buffer, 2 * count, address);
  1150. break;
  1151. case 1:
  1152. retval = mem_ap_write_buf_u8(swjdp, buffer, count, address);
  1153. break;
  1154. default:
  1155. LOG_ERROR("BUG: we shouldn't get here");
  1156. exit(-1);
  1157. }
  1158. return retval;
  1159. }
  1160. int cortex_m3_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffer)
  1161. {
  1162. return cortex_m3_write_memory(target, address, 4, count, buffer);
  1163. }
  1164. void cortex_m3_build_reg_cache(target_t *target)
  1165. {
  1166. armv7m_build_reg_cache(target);
  1167. }
  1168. int cortex_m3_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
  1169. {
  1170. cortex_m3_build_reg_cache(target);
  1171. return ERROR_OK;
  1172. }
  1173. int cortex_m3_examine(struct target_s *target)
  1174. {
  1175. int retval;
  1176. u32 cpuid, fpcr, dwtcr, ictr;
  1177. int i;
  1178. /* get pointers to arch-specific information */
  1179. armv7m_common_t *armv7m = target->arch_info;
  1180. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  1181. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1182. if ((retval = ahbap_debugport_init(swjdp)) != ERROR_OK)
  1183. return retval;
  1184. if (!target_was_examined(target))
  1185. {
  1186. target_set_examined(target);
  1187. /* Read from Device Identification Registers */
  1188. if ((retval = target_read_u32(target, CPUID, &cpuid)) != ERROR_OK)
  1189. return retval;
  1190. if (((cpuid >> 4) & 0xc3f) == 0xc23)
  1191. LOG_DEBUG("CORTEX-M3 processor detected");
  1192. LOG_DEBUG("cpuid: 0x%8.8x", cpuid);
  1193. target_read_u32(target, NVIC_ICTR, &ictr);
  1194. cortex_m3->intlinesnum = (ictr & 0x1F) + 1;
  1195. cortex_m3->intsetenable = calloc(cortex_m3->intlinesnum, 4);
  1196. for (i = 0; i < cortex_m3->intlinesnum; i++)
  1197. {
  1198. target_read_u32(target, NVIC_ISE0 + 4 * i, cortex_m3->intsetenable + i);
  1199. LOG_DEBUG("interrupt enable[%i] = 0x%8.8x", i, cortex_m3->intsetenable[i]);
  1200. }
  1201. /* Setup FPB */
  1202. target_read_u32(target, FP_CTRL, &fpcr);
  1203. cortex_m3->auto_bp_type = 1;
  1204. cortex_m3->fp_num_code = ((fpcr >> 8) & 0x70) | ((fpcr >> 4) & 0xF); /* bits [14:12] and [7:4] */
  1205. cortex_m3->fp_num_lit = (fpcr >> 8) & 0xF;
  1206. cortex_m3->fp_code_available = cortex_m3->fp_num_code;
  1207. cortex_m3->fp_comparator_list = calloc(cortex_m3->fp_num_code + cortex_m3->fp_num_lit, sizeof(cortex_m3_fp_comparator_t));
  1208. cortex_m3->fpb_enabled = fpcr & 1;
  1209. for (i = 0; i < cortex_m3->fp_num_code + cortex_m3->fp_num_lit; i++)
  1210. {
  1211. cortex_m3->fp_comparator_list[i].type = (i < cortex_m3->fp_num_code) ? FPCR_CODE : FPCR_LITERAL;
  1212. cortex_m3->fp_comparator_list[i].fpcr_address = FP_COMP0 + 4 * i;
  1213. }
  1214. LOG_DEBUG("FPB fpcr 0x%x, numcode %i, numlit %i", fpcr, cortex_m3->fp_num_code, cortex_m3->fp_num_lit);
  1215. /* Setup DWT */
  1216. target_read_u32(target, DWT_CTRL, &dwtcr);
  1217. cortex_m3->dwt_num_comp = (dwtcr >> 28) & 0xF;
  1218. cortex_m3->dwt_comp_available = cortex_m3->dwt_num_comp;
  1219. cortex_m3->dwt_comparator_list = calloc(cortex_m3->dwt_num_comp, sizeof(cortex_m3_dwt_comparator_t));
  1220. for (i = 0; i < cortex_m3->dwt_num_comp; i++)
  1221. {
  1222. cortex_m3->dwt_comparator_list[i].dwt_comparator_address = DWT_COMP0 + 0x10 * i;
  1223. }
  1224. }
  1225. return ERROR_OK;
  1226. }
  1227. int cortex_m3_quit(void)
  1228. {
  1229. return ERROR_OK;
  1230. }
  1231. int cortex_m3_dcc_read(swjdp_common_t *swjdp, u8 *value, u8 *ctrl)
  1232. {
  1233. u16 dcrdr;
  1234. mem_ap_read_buf_u16( swjdp, (u8*)&dcrdr, 1, DCB_DCRDR);
  1235. *ctrl = (u8)dcrdr;
  1236. *value = (u8)(dcrdr >> 8);
  1237. LOG_DEBUG("data 0x%x ctrl 0x%x", *value, *ctrl);
  1238. /* write ack back to software dcc register
  1239. * signify we have read data */
  1240. if (dcrdr & (1 << 0))
  1241. {
  1242. dcrdr = 0;
  1243. mem_ap_write_buf_u16( swjdp, (u8*)&dcrdr, 1, DCB_DCRDR);
  1244. }
  1245. return ERROR_OK;
  1246. }
  1247. int cortex_m3_target_request_data(target_t *target, u32 size, u8 *buffer)
  1248. {
  1249. armv7m_common_t *armv7m = target->arch_info;
  1250. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1251. u8 data;
  1252. u8 ctrl;
  1253. u32 i;
  1254. for (i = 0; i < (size * 4); i++)
  1255. {
  1256. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1257. buffer[i] = data;
  1258. }
  1259. return ERROR_OK;
  1260. }
  1261. int cortex_m3_handle_target_request(void *priv)
  1262. {
  1263. target_t *target = priv;
  1264. if (!target_was_examined(target))
  1265. return ERROR_OK;
  1266. armv7m_common_t *armv7m = target->arch_info;
  1267. swjdp_common_t *swjdp = &armv7m->swjdp_info;
  1268. if (!target->dbg_msg_enabled)
  1269. return ERROR_OK;
  1270. if (target->state == TARGET_RUNNING)
  1271. {
  1272. u8 data;
  1273. u8 ctrl;
  1274. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1275. /* check if we have data */
  1276. if (ctrl & (1 << 0))
  1277. {
  1278. u32 request;
  1279. /* we assume target is quick enough */
  1280. request = data;
  1281. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1282. request |= (data << 8);
  1283. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1284. request |= (data << 16);
  1285. cortex_m3_dcc_read(swjdp, &data, &ctrl);
  1286. request |= (data << 24);
  1287. target_request(target, request);
  1288. }
  1289. }
  1290. return ERROR_OK;
  1291. }
  1292. int cortex_m3_init_arch_info(target_t *target, cortex_m3_common_t *cortex_m3, jtag_tap_t *tap)
  1293. {
  1294. int retval;
  1295. armv7m_common_t *armv7m;
  1296. armv7m = &cortex_m3->armv7m;
  1297. armv7m_init_arch_info(target, armv7m);
  1298. /* prepare JTAG information for the new target */
  1299. cortex_m3->jtag_info.tap = tap;
  1300. cortex_m3->jtag_info.scann_size = 4;
  1301. armv7m->swjdp_info.dp_select_value = -1;
  1302. armv7m->swjdp_info.ap_csw_value = -1;
  1303. armv7m->swjdp_info.ap_tar_value = -1;
  1304. armv7m->swjdp_info.jtag_info = &cortex_m3->jtag_info;
  1305. armv7m->swjdp_info.memaccess_tck = 8;
  1306. /* initialize arch-specific breakpoint handling */
  1307. cortex_m3->common_magic = CORTEX_M3_COMMON_MAGIC;
  1308. cortex_m3->arch_info = NULL;
  1309. /* register arch-specific functions */
  1310. armv7m->examine_debug_reason = cortex_m3_examine_debug_reason;
  1311. armv7m->pre_debug_entry = NULL;
  1312. armv7m->post_debug_entry = NULL;
  1313. armv7m->pre_restore_context = NULL;
  1314. armv7m->post_restore_context = NULL;
  1315. armv7m->arch_info = cortex_m3;
  1316. armv7m->load_core_reg_u32 = cortex_m3_load_core_reg_u32;
  1317. armv7m->store_core_reg_u32 = cortex_m3_store_core_reg_u32;
  1318. target_register_timer_callback(cortex_m3_handle_target_request, 1, 1, target);
  1319. if ((retval = arm_jtag_setup_connection(&cortex_m3->jtag_info)) != ERROR_OK)
  1320. {
  1321. return retval;
  1322. }
  1323. return ERROR_OK;
  1324. }
  1325. int cortex_m3_target_create(struct target_s *target, Jim_Interp *interp)
  1326. {
  1327. cortex_m3_common_t *cortex_m3 = calloc(1,sizeof(cortex_m3_common_t));
  1328. cortex_m3_init_arch_info(target, cortex_m3, target->tap);
  1329. return ERROR_OK;
  1330. }
  1331. int cortex_m3_register_commands(struct command_context_s *cmd_ctx)
  1332. {
  1333. int retval;
  1334. command_t *cortex_m3_cmd;
  1335. retval = armv7m_register_commands(cmd_ctx);
  1336. cortex_m3_cmd = register_command(cmd_ctx, NULL, "cortex_m3", NULL, COMMAND_ANY, "cortex_m3 specific commands");
  1337. register_command(cmd_ctx, cortex_m3_cmd, "maskisr", handle_cortex_m3_mask_interrupts_command, COMMAND_EXEC, "mask cortex_m3 interrupts ['on'|'off']");
  1338. return retval;
  1339. }
  1340. int handle_cortex_m3_mask_interrupts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1341. {
  1342. target_t *target = get_current_target(cmd_ctx);
  1343. armv7m_common_t *armv7m = target->arch_info;
  1344. cortex_m3_common_t *cortex_m3 = armv7m->arch_info;
  1345. if (target->state != TARGET_HALTED)
  1346. {
  1347. command_print(cmd_ctx, "target must be stopped for \"%s\" command", cmd);
  1348. return ERROR_OK;
  1349. }
  1350. if (argc > 0)
  1351. {
  1352. if (!strcmp(args[0], "on"))
  1353. {
  1354. cortex_m3_write_debug_halt_mask(target, C_HALT|C_MASKINTS, 0);
  1355. }
  1356. else if (!strcmp(args[0], "off"))
  1357. {
  1358. cortex_m3_write_debug_halt_mask(target, C_HALT, C_MASKINTS);
  1359. }
  1360. else
  1361. {
  1362. command_print(cmd_ctx, "usage: cortex_m3 maskisr ['on'|'off']");
  1363. }
  1364. }
  1365. command_print(cmd_ctx, "cortex_m3 interrupt mask %s",
  1366. (cortex_m3->dcb_dhcsr & C_MASKINTS) ? "on" : "off");
  1367. return ERROR_OK;
  1368. }