define resetting the target into the halted or running
[openocd.git] / src / target / mips_m4k.c
blob2a34bd0b9fce07ed926648bdf70736149db3e099
1 /***************************************************************************
2 * Copyright (C) 2008 by Spencer Oliver *
3 * spen@spen-soft.co.uk *
4 * *
5 * Copyright (C) 2008 by David T.L. Wong *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #include "mips32.h"
27 #include "mips_m4k.h"
28 #include "jtag.h"
29 #include "log.h"
31 #include <stdlib.h>
32 #include <string.h>
34 /* cli handling */
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);
46 int mips_m4k_quit();
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 =
55 .name = "mips_m4k",
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,
89 .quit = mips_m4k_quit
92 int mips_m4k_debug_entry(target_t *target)
94 u32 debug_reg;
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)
105 // {
106 // target->debug_reason = DBG_REASON_BREAKPOINT;
107 // if (cortex_m3->nvic_dfsr & DFSR_DWTTRAP)
108 // target->debug_reason = DBG_REASON_WPTANDBKPT;
109 // }
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]);
125 return ERROR_OK;
128 int mips_m4k_poll(target_t *target)
130 int retval;
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)
149 return retval;
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)
158 return retval;
160 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
163 else
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);
182 return ERROR_OK;
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");
195 return ERROR_OK;
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;
210 else
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;
217 return ERROR_OK;
221 /* break processor */
222 mips_ejtag_enter_debug(ejtag_info);
224 target->debug_reason = DBG_REASON_DBGRQ;
226 return ERROR_OK;
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");
239 return ERROR_FAIL;
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);
248 else
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);
259 else
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)
271 int retval;
272 if ((retval = target_halt(target))!=ERROR_OK)
273 return retval;
277 return 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);
287 return ERROR_OK;
290 int mips_m4k_soft_reset_halt(struct target_s *target)
292 /* TODO */
293 return ERROR_OK;
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;
301 u32 resume_pc;
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> */
317 if (!current)
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);
353 else
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);
360 return ERROR_OK;
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> */
377 if (!current)
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);
401 if (breakpoint)
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);
409 return ERROR_OK;
412 void mips_m4k_enable_breakpoints(struct target_s *target)
414 breakpoint_t *breakpoint = target->breakpoints;
416 /* set any pending breakpoints */
417 while (breakpoint)
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)
427 /* TODO */
428 return ERROR_OK;
431 int mips_m4k_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
433 /* TODO */
434 return ERROR_OK;
437 int mips_m4k_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
439 /* TODO */
440 return ERROR_OK;
443 int mips_m4k_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
445 /* TODO */
446 return ERROR_OK;
449 int mips_m4k_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
451 /* TODO */
452 return ERROR_OK;
455 int mips_m4k_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
457 /* TODO */
458 return ERROR_OK;
461 int mips_m4k_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
463 /* TODO */
464 return ERROR_OK;
467 int mips_m4k_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
469 /* TODO */
470 return ERROR_OK;
473 void mips_m4k_enable_watchpoints(struct target_s *target)
475 watchpoint_t *watchpoint = target->watchpoints;
477 /* set any pending watchpoints */
478 while (watchpoint)
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;
506 switch (size)
508 case 4:
509 case 2:
510 case 1:
511 return mips32_pracc_read_mem(ejtag_info, address, size, count, (void *)buffer);
512 default:
513 LOG_ERROR("BUG: we shouldn't get here");
514 exit(-1);
515 break;
518 return ERROR_OK;
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;
541 switch (size)
543 case 4:
544 case 2:
545 case 1:
546 mips32_pracc_write_mem(ejtag_info, address, size, count, (void *)buffer);
547 break;
548 default:
549 LOG_ERROR("BUG: we shouldn't get here");
550 exit(-1);
551 break;
554 return ERROR_OK;
557 int mips_m4k_register_commands(struct command_context_s *cmd_ctx)
559 int retval;
561 retval = mips32_register_commands(cmd_ctx);
562 return retval;
565 int mips_m4k_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
567 mips32_build_reg_cache(target);
569 return ERROR_OK;
572 int mips_m4k_quit()
574 return ERROR_OK;
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;
581 if (variant)
583 mips_m4k->variant = strdup(variant);
585 else
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;
596 return ERROR_OK;
599 int mips_m4k_target_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, struct target_s *target)
601 int chain_pos;
602 char *variant = NULL;
603 mips_m4k_common_t *mips_m4k = malloc(sizeof(mips_m4k_common_t));
605 if (argc < 4)
607 LOG_ERROR("'target mips4k' requires at least one additional argument");
608 exit(-1);
611 chain_pos = strtoul(args[3], NULL, 0);
613 if (argc >= 5)
614 variant = args[4];
616 mips_m4k_init_arch_info(target, mips_m4k, chain_pos, variant);
618 return ERROR_OK;
621 int mips_m4k_examine(struct target_s *target)
623 int retval;
624 mips32_common_t *mips32 = target->arch_info;
625 mips_ejtag_t *ejtag_info = &mips32->ejtag_info;
626 u32 idcode = 0;
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)
642 return retval;
644 return 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);