1 /***************************************************************************
2 * Copyright (C) 2008 by Spencer Oliver *
3 * spen@spen-soft.co.uk *
5 * Copyright (C) 2008 by David T.L. Wong *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
36 /* forward declarations */
37 int mips_m4k_poll(target_t
*target
);
38 int mips_m4k_halt(struct target_s
*target
);
39 int mips_m4k_soft_reset_halt(struct target_s
*target
);
40 int mips_m4k_resume(struct target_s
*target
, int current
, u32 address
, int handle_breakpoints
, int debug_execution
);
41 int mips_m4k_step(struct target_s
*target
, int current
, u32 address
, int handle_breakpoints
);
42 int mips_m4k_read_memory(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
);
43 int mips_m4k_write_memory(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
);
44 int mips_m4k_register_commands(struct command_context_s
*cmd_ctx
);
45 int mips_m4k_init_target(struct command_context_s
*cmd_ctx
, struct target_s
*target
);
47 int mips_m4k_target_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct target_s
*target
);
49 int mips_m4k_examine(struct target_s
*target
);
50 int mips_m4k_assert_reset(target_t
*target
);
51 int mips_m4k_deassert_reset(target_t
*target
);
53 target_type_t mips_m4k_target
=
57 .poll
= mips_m4k_poll
,
58 .arch_state
= mips32_arch_state
,
60 .target_request_data
= NULL
,
62 .halt
= mips_m4k_halt
,
63 .resume
= mips_m4k_resume
,
64 .step
= mips_m4k_step
,
66 .assert_reset
= mips_m4k_assert_reset
,
67 .deassert_reset
= mips_m4k_deassert_reset
,
68 .soft_reset_halt
= mips_m4k_soft_reset_halt
,
70 .get_gdb_reg_list
= mips32_get_gdb_reg_list
,
72 .read_memory
= mips_m4k_read_memory
,
73 .write_memory
= mips_m4k_write_memory
,
74 .bulk_write_memory
= mips_m4k_bulk_write_memory
,
75 .checksum_memory
= NULL
,
76 .blank_check_memory
= NULL
,
78 .run_algorithm
= mips32_run_algorithm
,
80 .add_breakpoint
= mips_m4k_add_breakpoint
,
81 .remove_breakpoint
= mips_m4k_remove_breakpoint
,
82 .add_watchpoint
= mips_m4k_add_watchpoint
,
83 .remove_watchpoint
= mips_m4k_remove_watchpoint
,
85 .register_commands
= mips_m4k_register_commands
,
86 .target_command
= mips_m4k_target_command
,
87 .init_target
= mips_m4k_init_target
,
88 .examine
= mips_m4k_examine
,
92 int mips_m4k_debug_entry(target_t
*target
)
95 mips32_common_t
*mips32
= target
->arch_info
;
96 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
98 /* read debug register */
99 mips_ejtag_read_debug(ejtag_info
, &debug_reg
);
101 if ((target
->debug_reason
!= DBG_REASON_DBGRQ
)
102 && (target
->debug_reason
!= DBG_REASON_SINGLESTEP
))
104 // if (cortex_m3->nvic_dfsr & DFSR_BKPT)
106 // target->debug_reason = DBG_REASON_BREAKPOINT;
107 // if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
108 // target->debug_reason = DBG_REASON_WPTANDBKPT;
110 // else if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
111 // target->debug_reason = DBG_REASON_WATCHPOINT;
114 if (debug_reg
& EJTAG_DEBUG_DSS
)
116 /* stopped due to single step - clear step bit */
117 mips_ejtag_config_step(ejtag_info
, 0);
120 mips32_save_context(target
);
122 LOG_DEBUG("entered debug state at PC 0x%x, target->state: %s", \
123 *(u32
*)(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
), target_state_strings
[target
->state
]);
128 int mips_m4k_poll(target_t
*target
)
131 mips32_common_t
*mips32
= target
->arch_info
;
132 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
134 /* read ejtag control reg */
135 jtag_add_end_state(TAP_RTI
);
136 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
, NULL
);
137 mips_ejtag_drscan_32(ejtag_info
, &ejtag_info
->ejtag_ctrl
);
139 if (ejtag_info
->ejtag_ctrl
& EJTAG_CTRL_BRKST
)
141 if ((target
->state
== TARGET_RUNNING
) || (target
->state
== TARGET_RESET
))
143 jtag_add_end_state(TAP_RTI
);
144 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_NORMALBOOT
, NULL
);
146 target
->state
= TARGET_HALTED
;
148 if ((retval
= mips_m4k_debug_entry(target
)) != ERROR_OK
)
151 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
153 else if (target
->state
== TARGET_DEBUG_RUNNING
)
155 target
->state
= TARGET_HALTED
;
157 if ((retval
= mips_m4k_debug_entry(target
)) != ERROR_OK
)
160 target_call_event_callbacks(target
, TARGET_EVENT_DEBUG_HALTED
);
165 target
->state
= TARGET_RUNNING
;
168 if (ejtag_info
->ejtag_ctrl
& EJTAG_CTRL_ROCC
)
170 /* we have detected a reset, clear flag
171 * otherwise ejtag will not work */
172 jtag_add_end_state(TAP_RTI
);
173 ejtag_info
->ejtag_ctrl
&= ~EJTAG_CTRL_ROCC
;
175 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_CONTROL
, NULL
);
176 mips_ejtag_drscan_32(ejtag_info
, &ejtag_info
->ejtag_ctrl
);
177 LOG_DEBUG("Reset Detected");
180 // LOG_DEBUG("ctrl=0x%08X", ejtag_info->ejtag_ctrl);
185 int mips_m4k_halt(struct target_s
*target
)
187 mips32_common_t
*mips32
= target
->arch_info
;
188 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
190 LOG_DEBUG("target->state: %s", target_state_strings
[target
->state
]);
192 if (target
->state
== TARGET_HALTED
)
194 LOG_DEBUG("target was already halted");
198 if (target
->state
== TARGET_UNKNOWN
)
200 LOG_WARNING("target was in unknown state when halt was requested");
203 if (target
->state
== TARGET_RESET
)
205 if ((jtag_reset_config
& RESET_SRST_PULLS_TRST
) && jtag_srst
)
207 LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
208 return ERROR_TARGET_FAILURE
;
212 /* we came here in a reset_halt or reset_init sequence
213 * debug entry was already prepared in mips32_prepare_reset_halt()
215 target
->debug_reason
= DBG_REASON_DBGRQ
;
221 /* break processor */
222 mips_ejtag_enter_debug(ejtag_info
);
224 target
->debug_reason
= DBG_REASON_DBGRQ
;
229 int mips_m4k_assert_reset(target_t
*target
)
231 mips32_common_t
*mips32
= target
->arch_info
;
232 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
234 LOG_DEBUG("target->state: %s", target_state_strings
[target
->state
]);
236 if (!(jtag_reset_config
& RESET_HAS_SRST
))
238 LOG_ERROR("Can't assert SRST");
242 if (target
->reset_halt
)
244 /* use hardware to catch reset */
245 jtag_add_end_state(TAP_RTI
);
246 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_EJTAGBOOT
, NULL
);
250 jtag_add_end_state(TAP_RTI
);
251 mips_ejtag_set_instr(ejtag_info
, EJTAG_INST_NORMALBOOT
, NULL
);
254 /* here we should issue a srst only, but we may have to assert trst as well */
255 if (jtag_reset_config
& RESET_SRST_PULLS_TRST
)
257 jtag_add_reset(1, 1);
261 jtag_add_reset(0, 1);
264 target
->state
= TARGET_RESET
;
265 jtag_add_sleep(50000);
267 mips32_invalidate_core_regs(target
);
269 if (target
->reset_halt
)
272 if ((retval
= target_halt(target
))!=ERROR_OK
)
280 int mips_m4k_deassert_reset(target_t
*target
)
282 LOG_DEBUG("target->state: %s", target_state_strings
[target
->state
]);
284 /* deassert reset lines */
285 jtag_add_reset(0, 0);
290 int mips_m4k_soft_reset_halt(struct target_s
*target
)
296 int mips_m4k_resume(struct target_s
*target
, int current
, u32 address
, int handle_breakpoints
, int debug_execution
)
298 mips32_common_t
*mips32
= target
->arch_info
;
299 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
300 breakpoint_t
*breakpoint
= NULL
;
303 if (target
->state
!= TARGET_HALTED
)
305 LOG_WARNING("target not halted");
306 return ERROR_TARGET_NOT_HALTED
;
309 if (!debug_execution
)
311 target_free_all_working_areas(target
);
312 mips_m4k_enable_breakpoints(target
);
313 mips_m4k_enable_watchpoints(target
);
316 /* current = 1: continue on current pc, otherwise continue at <address> */
319 buf_set_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32, address
);
320 mips32
->core_cache
->reg_list
[MIPS32_PC
].dirty
= 1;
321 mips32
->core_cache
->reg_list
[MIPS32_PC
].valid
= 1;
324 resume_pc
= buf_get_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32);
326 mips32_restore_context(target
);
328 /* the front-end may request us not to handle breakpoints */
329 if (handle_breakpoints
)
331 /* Single step past breakpoint at current address */
332 if ((breakpoint
= breakpoint_find(target
, resume_pc
)))
334 LOG_DEBUG("unset breakpoint at 0x%8.8x", breakpoint
->address
);
335 mips_m4k_unset_breakpoint(target
, breakpoint
);
336 //mips_m4k_single_step_core(target);
337 mips_m4k_set_breakpoint(target
, breakpoint
);
341 /* exit debug mode - enable interrupts if required */
342 mips_ejtag_exit_debug(ejtag_info
, !debug_execution
);
344 /* registers are now invalid */
345 mips32_invalidate_core_regs(target
);
347 if (!debug_execution
)
349 target
->state
= TARGET_RUNNING
;
350 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
351 LOG_DEBUG("target resumed at 0x%x", resume_pc
);
355 target
->state
= TARGET_DEBUG_RUNNING
;
356 target_call_event_callbacks(target
, TARGET_EVENT_DEBUG_RESUMED
);
357 LOG_DEBUG("target debug resumed at 0x%x", resume_pc
);
363 int mips_m4k_step(struct target_s
*target
, int current
, u32 address
, int handle_breakpoints
)
365 /* get pointers to arch-specific information */
366 mips32_common_t
*mips32
= target
->arch_info
;
367 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
368 breakpoint_t
*breakpoint
= NULL
;
370 if (target
->state
!= TARGET_HALTED
)
372 LOG_WARNING("target not halted");
373 return ERROR_TARGET_NOT_HALTED
;
376 /* current = 1: continue on current pc, otherwise continue at <address> */
378 buf_set_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32, address
);
380 /* the front-end may request us not to handle breakpoints */
381 if (handle_breakpoints
)
382 if ((breakpoint
= breakpoint_find(target
, buf_get_u32(mips32
->core_cache
->reg_list
[MIPS32_PC
].value
, 0, 32))))
383 mips_m4k_unset_breakpoint(target
, breakpoint
);
385 /* restore context */
386 mips32_restore_context(target
);
388 /* configure single step mode */
389 mips_ejtag_config_step(ejtag_info
, 1);
391 target
->debug_reason
= DBG_REASON_SINGLESTEP
;
393 target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
);
395 /* exit debug mode */
396 mips_ejtag_exit_debug(ejtag_info
, 1);
398 /* registers are now invalid */
399 mips32_invalidate_core_regs(target
);
402 mips_m4k_set_breakpoint(target
, breakpoint
);
404 LOG_DEBUG("target stepped ");
406 mips_m4k_debug_entry(target
);
407 target_call_event_callbacks(target
, TARGET_EVENT_HALTED
);
412 void mips_m4k_enable_breakpoints(struct target_s
*target
)
414 breakpoint_t
*breakpoint
= target
->breakpoints
;
416 /* set any pending breakpoints */
419 if (breakpoint
->set
== 0)
420 mips_m4k_set_breakpoint(target
, breakpoint
);
421 breakpoint
= breakpoint
->next
;
425 int mips_m4k_set_breakpoint(struct target_s
*target
, breakpoint_t
*breakpoint
)
431 int mips_m4k_unset_breakpoint(struct target_s
*target
, breakpoint_t
*breakpoint
)
437 int mips_m4k_add_breakpoint(struct target_s
*target
, breakpoint_t
*breakpoint
)
443 int mips_m4k_remove_breakpoint(struct target_s
*target
, breakpoint_t
*breakpoint
)
449 int mips_m4k_set_watchpoint(struct target_s
*target
, watchpoint_t
*watchpoint
)
455 int mips_m4k_unset_watchpoint(struct target_s
*target
, watchpoint_t
*watchpoint
)
461 int mips_m4k_add_watchpoint(struct target_s
*target
, watchpoint_t
*watchpoint
)
467 int mips_m4k_remove_watchpoint(struct target_s
*target
, watchpoint_t
*watchpoint
)
473 void mips_m4k_enable_watchpoints(struct target_s
*target
)
475 watchpoint_t
*watchpoint
= target
->watchpoints
;
477 /* set any pending watchpoints */
480 if (watchpoint
->set
== 0)
481 mips_m4k_set_watchpoint(target
, watchpoint
);
482 watchpoint
= watchpoint
->next
;
486 int mips_m4k_read_memory(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
488 mips32_common_t
*mips32
= target
->arch_info
;
489 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
491 LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address
, size
, count
);
493 if (target
->state
!= TARGET_HALTED
)
495 LOG_WARNING("target not halted");
496 return ERROR_TARGET_NOT_HALTED
;
499 /* sanitize arguments */
500 if (((size
!= 4) && (size
!= 2) && (size
!= 1)) || (count
== 0) || !(buffer
))
501 return ERROR_INVALID_ARGUMENTS
;
503 if (((size
== 4) && (address
& 0x3u
)) || ((size
== 2) && (address
& 0x1u
)))
504 return ERROR_TARGET_UNALIGNED_ACCESS
;
511 return mips32_pracc_read_mem(ejtag_info
, address
, size
, count
, (void *)buffer
);
513 LOG_ERROR("BUG: we shouldn't get here");
521 int mips_m4k_write_memory(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
523 mips32_common_t
*mips32
= target
->arch_info
;
524 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
526 LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address
, size
, count
);
528 if (target
->state
!= TARGET_HALTED
)
530 LOG_WARNING("target not halted");
531 return ERROR_TARGET_NOT_HALTED
;
534 /* sanitize arguments */
535 if (((size
!= 4) && (size
!= 2) && (size
!= 1)) || (count
== 0) || !(buffer
))
536 return ERROR_INVALID_ARGUMENTS
;
538 if (((size
== 4) && (address
& 0x3u
)) || ((size
== 2) && (address
& 0x1u
)))
539 return ERROR_TARGET_UNALIGNED_ACCESS
;
546 mips32_pracc_write_mem(ejtag_info
, address
, size
, count
, (void *)buffer
);
549 LOG_ERROR("BUG: we shouldn't get here");
557 int mips_m4k_register_commands(struct command_context_s
*cmd_ctx
)
561 retval
= mips32_register_commands(cmd_ctx
);
565 int mips_m4k_init_target(struct command_context_s
*cmd_ctx
, struct target_s
*target
)
567 mips32_build_reg_cache(target
);
577 int mips_m4k_init_arch_info(target_t
*target
, mips_m4k_common_t
*mips_m4k
, int chain_pos
, char *variant
)
579 mips32_common_t
*mips32
= &mips_m4k
->mips32_common
;
583 mips_m4k
->variant
= strdup(variant
);
587 mips_m4k
->variant
= strdup("");
590 mips_m4k
->common_magic
= MIPSM4K_COMMON_MAGIC
;
592 /* initialize mips4k specific info */
593 mips32_init_arch_info(target
, mips32
, chain_pos
, variant
);
594 mips32
->arch_info
= mips_m4k
;
599 int mips_m4k_target_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct target_s
*target
)
602 char *variant
= NULL
;
603 mips_m4k_common_t
*mips_m4k
= malloc(sizeof(mips_m4k_common_t
));
607 LOG_ERROR("'target mips4k' requires at least one additional argument");
611 chain_pos
= strtoul(args
[3], NULL
, 0);
616 mips_m4k_init_arch_info(target
, mips_m4k
, chain_pos
, variant
);
621 int mips_m4k_examine(struct target_s
*target
)
624 mips32_common_t
*mips32
= target
->arch_info
;
625 mips_ejtag_t
*ejtag_info
= &mips32
->ejtag_info
;
628 target
->type
->examined
= 1;
630 mips_ejtag_get_idcode(ejtag_info
, &idcode
, NULL
);
632 if (((idcode
>> 1) & 0x7FF) == 0x29)
634 /* we are using a pic32mx so select ejtag port
635 * as it is not selected by default */
636 mips_ejtag_set_instr(ejtag_info
, 0x05, NULL
);
637 LOG_DEBUG("PIC32MX Detected - using EJTAG Interface");
640 /* init rest of ejtag interface */
641 if ((retval
= mips_ejtag_init(ejtag_info
)) != ERROR_OK
)
647 int mips_m4k_bulk_write_memory(target_t
*target
, u32 address
, u32 count
, u8
*buffer
)
649 return mips_m4k_write_memory(target
, address
, 4, count
, buffer
);