1 /***************************************************************************
2 * Copyright (C) 2008 digenius technology GmbH. *
5 * Copyright (C) 2008,2009 Oyvind Harboe oyvind.harboe@zylin.com *
7 * Copyright (C) 2008 Georg Acher <acher@in.tum.de> *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the *
21 * Free Software Foundation, Inc., *
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23 ***************************************************************************/
30 #include "breakpoints.h"
31 #include "arm11_dbgtap.h"
32 #include "arm_simulator.h"
33 #include <helper/time_support.h>
34 #include "target_type.h"
35 #include "algorithm.h"
40 #define _DEBUG_INSTRUCTION_EXECUTION_
44 /* FIXME none of these flags should be global to all ARM11 cores!
45 * Most of them shouldn't exist at all, once the code works...
47 static bool arm11_config_memwrite_burst
= true;
48 static bool arm11_config_memwrite_error_fatal
= true;
49 static uint32_t arm11_vcr
= 0;
50 static bool arm11_config_step_irq_enable
= false;
51 static bool arm11_config_hardware_step
= false;
53 static int arm11_step(struct target
*target
, int current
,
54 uint32_t address
, int handle_breakpoints
);
57 /** Check and if necessary take control of the system
59 * \param arm11 Target state variable.
61 static int arm11_check_init(struct arm11_common
*arm11
)
63 CHECK_RETVAL(arm11_read_DSCR(arm11
));
64 LOG_DEBUG("DSCR %08x", (unsigned) arm11
->dscr
);
66 if (!(arm11
->dscr
& DSCR_HALT_DBG_MODE
))
68 LOG_DEBUG("Bringing target into debug mode");
70 arm11
->dscr
|= DSCR_HALT_DBG_MODE
;
71 arm11_write_DSCR(arm11
, arm11
->dscr
);
73 /* add further reset initialization here */
75 arm11
->simulate_reset_on_next_halt
= true;
77 if (arm11
->dscr
& DSCR_CORE_HALTED
)
79 /** \todo TODO: this needs further scrutiny because
80 * arm11_debug_entry() never gets called. (WHY NOT?)
81 * As a result we don't read the actual register states from
85 arm11
->arm
.target
->state
= TARGET_HALTED
;
86 arm_dpm_report_dscr(arm11
->arm
.dpm
, arm11
->dscr
);
90 arm11
->arm
.target
->state
= TARGET_RUNNING
;
91 arm11
->arm
.target
->debug_reason
= DBG_REASON_NOTHALTED
;
94 arm11_sc7_clear_vbw(arm11
);
101 * Save processor state. This is called after a HALT instruction
102 * succeeds, and on other occasions the processor enters debug mode
103 * (breakpoint, watchpoint, etc). Caller has updated arm11->dscr.
105 static int arm11_debug_entry(struct arm11_common
*arm11
)
109 arm11
->arm
.target
->state
= TARGET_HALTED
;
110 arm_dpm_report_dscr(arm11
->arm
.dpm
, arm11
->dscr
);
112 /* REVISIT entire cache should already be invalid !!! */
113 register_cache_invalidate(arm11
->arm
.core_cache
);
115 /* See e.g. ARM1136 TRM, "14.8.4 Entering Debug state" */
117 /* maybe save wDTR (pending DCC write to debug SW, e.g. libdcc) */
118 arm11
->is_wdtr_saved
= !!(arm11
->dscr
& DSCR_DTR_TX_FULL
);
119 if (arm11
->is_wdtr_saved
)
121 arm11_add_debug_SCAN_N(arm11
, 0x05, ARM11_TAP_DEFAULT
);
123 arm11_add_IR(arm11
, ARM11_INTEST
, ARM11_TAP_DEFAULT
);
125 struct scan_field chain5_fields
[3];
127 arm11_setup_field(arm11
, 32, NULL
,
128 &arm11
->saved_wdtr
, chain5_fields
+ 0);
129 arm11_setup_field(arm11
, 1, NULL
, NULL
, chain5_fields
+ 1);
130 arm11_setup_field(arm11
, 1, NULL
, NULL
, chain5_fields
+ 2);
132 arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields
), chain5_fields
, TAP_DRPAUSE
);
136 /* DSCR: set the Execute ARM instruction enable bit.
138 * ARM1176 spec says this is needed only for wDTR/rDTR's "ITR mode",
139 * but not to issue ITRs(?). The ARMv7 arch spec says it's required
140 * for executing instructions via ITR.
142 arm11_write_DSCR(arm11
, DSCR_ITR_EN
| arm11
->dscr
);
146 Before executing any instruction in debug state you have to drain the write buffer.
147 This ensures that no imprecise Data Aborts can return at a later point:*/
149 /** \todo TODO: Test drain write buffer. */
154 /* MRC p14,0,R0,c5,c10,0 */
155 // arm11_run_instr_no_data1(arm11, /*0xee150e1a*/0xe320f000);
157 /* mcr 15, 0, r0, cr7, cr10, {4} */
158 arm11_run_instr_no_data1(arm11
, 0xee070f9a);
160 uint32_t dscr
= arm11_read_DSCR(arm11
);
162 LOG_DEBUG("DRAIN, DSCR %08x", dscr
);
164 if (dscr
& ARM11_DSCR_STICKY_IMPRECISE_DATA_ABORT
)
166 arm11_run_instr_no_data1(arm11
, 0xe320f000);
168 dscr
= arm11_read_DSCR(arm11
);
170 LOG_DEBUG("DRAIN, DSCR %08x (DONE)", dscr
);
179 * NOTE: ARM1136 TRM suggests saving just R0 here now, then
180 * CPSR and PC after the rDTR stuff. We do it all at once.
182 retval
= arm_dpm_read_current_registers(&arm11
->dpm
);
183 if (retval
!= ERROR_OK
)
184 LOG_ERROR("DPM REG READ -- fail %d", retval
);
186 retval
= arm11_run_instr_data_prepare(arm11
);
187 if (retval
!= ERROR_OK
)
190 /* maybe save rDTR (pending DCC read from debug SW, e.g. libdcc) */
191 arm11
->is_rdtr_saved
= !!(arm11
->dscr
& DSCR_DTR_RX_FULL
);
192 if (arm11
->is_rdtr_saved
)
194 /* MRC p14,0,R0,c0,c5,0 (move rDTR -> r0 (-> wDTR -> local var)) */
195 retval
= arm11_run_instr_data_from_core_via_r0(arm11
,
196 0xEE100E15, &arm11
->saved_rdtr
);
197 if (retval
!= ERROR_OK
)
201 /* REVISIT Now that we've saved core state, there's may also
202 * be MMU and cache state to care about ...
205 if (arm11
->simulate_reset_on_next_halt
)
207 arm11
->simulate_reset_on_next_halt
= false;
209 LOG_DEBUG("Reset c1 Control Register");
211 /* Write 0 (reset value) to Control register 0 to disable MMU/Cache etc. */
213 /* MCR p15,0,R0,c1,c0,0 */
214 retval
= arm11_run_instr_data_to_core_via_r0(arm11
, 0xee010f10, 0);
215 if (retval
!= ERROR_OK
)
220 retval
= arm11_run_instr_data_finish(arm11
);
221 if (retval
!= ERROR_OK
)
228 * Restore processor state. This is called in preparation for
229 * the RESTART function.
231 static int arm11_leave_debug_state(struct arm11_common
*arm11
, bool bpwp
)
235 /* See e.g. ARM1136 TRM, "14.8.5 Leaving Debug state" */
237 /* NOTE: the ARM1136 TRM suggests restoring all registers
238 * except R0/PC/CPSR right now. Instead, we do them all
239 * at once, just a bit later on.
242 /* REVISIT once we start caring about MMU and cache state,
243 * address it here ...
246 /* spec says clear wDTR and rDTR; we assume they are clear as
247 otherwise our programming would be sloppy */
249 CHECK_RETVAL(arm11_read_DSCR(arm11
));
251 if (arm11
->dscr
& (DSCR_DTR_RX_FULL
| DSCR_DTR_TX_FULL
))
254 The wDTR/rDTR two registers that are used to send/receive data to/from
255 the core in tandem with corresponding instruction codes that are
256 written into the core. The RDTR FULL/WDTR FULL flag indicates that the
257 registers hold data that was written by one side (CPU or JTAG) and not
258 read out by the other side.
260 LOG_ERROR("wDTR/rDTR inconsistent (DSCR %08x)",
261 (unsigned) arm11
->dscr
);
266 /* maybe restore original wDTR */
267 if (arm11
->is_wdtr_saved
)
269 retval
= arm11_run_instr_data_prepare(arm11
);
270 if (retval
!= ERROR_OK
)
273 /* MCR p14,0,R0,c0,c5,0 */
274 retval
= arm11_run_instr_data_to_core_via_r0(arm11
,
275 0xee000e15, arm11
->saved_wdtr
);
276 if (retval
!= ERROR_OK
)
279 retval
= arm11_run_instr_data_finish(arm11
);
280 if (retval
!= ERROR_OK
)
284 /* restore CPSR, PC, and R0 ... after flushing any modified
287 retval
= arm_dpm_write_dirty_registers(&arm11
->dpm
, bpwp
);
289 register_cache_invalidate(arm11
->arm
.core_cache
);
292 arm11_write_DSCR(arm11
, arm11
->dscr
);
294 /* maybe restore rDTR */
295 if (arm11
->is_rdtr_saved
)
297 arm11_add_debug_SCAN_N(arm11
, 0x05, ARM11_TAP_DEFAULT
);
299 arm11_add_IR(arm11
, ARM11_EXTEST
, ARM11_TAP_DEFAULT
);
301 struct scan_field chain5_fields
[3];
303 uint8_t Ready
= 0; /* ignored */
304 uint8_t Valid
= 0; /* ignored */
306 arm11_setup_field(arm11
, 32, &arm11
->saved_rdtr
,
307 NULL
, chain5_fields
+ 0);
308 arm11_setup_field(arm11
, 1, &Ready
, NULL
, chain5_fields
+ 1);
309 arm11_setup_field(arm11
, 1, &Valid
, NULL
, chain5_fields
+ 2);
311 arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields
), chain5_fields
, TAP_DRPAUSE
);
314 /* now processor is ready to RESTART */
319 /* poll current target status */
320 static int arm11_poll(struct target
*target
)
323 struct arm11_common
*arm11
= target_to_arm11(target
);
325 CHECK_RETVAL(arm11_check_init(arm11
));
327 if (arm11
->dscr
& DSCR_CORE_HALTED
)
329 if (target
->state
!= TARGET_HALTED
)
331 enum target_state old_state
= target
->state
;
333 LOG_DEBUG("enter TARGET_HALTED");
334 retval
= arm11_debug_entry(arm11
);
335 if (retval
!= ERROR_OK
)
338 target_call_event_callbacks(target
,
339 old_state
== TARGET_DEBUG_RUNNING
? TARGET_EVENT_DEBUG_HALTED
: TARGET_EVENT_HALTED
);
344 if (target
->state
!= TARGET_RUNNING
&& target
->state
!= TARGET_DEBUG_RUNNING
)
346 LOG_DEBUG("enter TARGET_RUNNING");
347 target
->state
= TARGET_RUNNING
;
348 target
->debug_reason
= DBG_REASON_NOTHALTED
;
354 /* architecture specific status reply */
355 static int arm11_arch_state(struct target
*target
)
359 retval
= armv4_5_arch_state(target
);
361 /* REVISIT also display ARM11-specific MMU and cache status ... */
366 /* target request support */
367 static int arm11_target_request_data(struct target
*target
,
368 uint32_t size
, uint8_t *buffer
)
370 LOG_WARNING("Not implemented: %s", __func__
);
375 /* target execution control */
376 static int arm11_halt(struct target
*target
)
378 struct arm11_common
*arm11
= target_to_arm11(target
);
380 LOG_DEBUG("target->state: %s",
381 target_state_name(target
));
383 if (target
->state
== TARGET_UNKNOWN
)
385 arm11
->simulate_reset_on_next_halt
= true;
388 if (target
->state
== TARGET_HALTED
)
390 LOG_DEBUG("target was already halted");
394 arm11_add_IR(arm11
, ARM11_HALT
, TAP_IDLE
);
396 CHECK_RETVAL(jtag_execute_queue());
402 CHECK_RETVAL(arm11_read_DSCR(arm11
));
404 if (arm11
->dscr
& DSCR_CORE_HALTED
)
415 if ((timeval_ms()-then
) > 1000)
417 LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
424 enum target_state old_state
= target
->state
;
426 arm11_debug_entry(arm11
);
429 target_call_event_callbacks(target
,
430 old_state
== TARGET_DEBUG_RUNNING
? TARGET_EVENT_DEBUG_HALTED
: TARGET_EVENT_HALTED
));
436 arm11_nextpc(struct arm11_common
*arm11
, int current
, uint32_t address
)
438 void *value
= arm11
->arm
.core_cache
->reg_list
[15].value
;
441 buf_set_u32(value
, 0, 32, address
);
443 address
= buf_get_u32(value
, 0, 32);
448 static int arm11_resume(struct target
*target
, int current
,
449 uint32_t address
, int handle_breakpoints
, int debug_execution
)
451 // LOG_DEBUG("current %d address %08x handle_breakpoints %d debug_execution %d",
452 // current, address, handle_breakpoints, debug_execution);
454 struct arm11_common
*arm11
= target_to_arm11(target
);
456 LOG_DEBUG("target->state: %s",
457 target_state_name(target
));
460 if (target
->state
!= TARGET_HALTED
)
462 LOG_ERROR("Target not halted");
463 return ERROR_TARGET_NOT_HALTED
;
466 address
= arm11_nextpc(arm11
, current
, address
);
468 LOG_DEBUG("RESUME PC %08" PRIx32
"%s", address
, !current
? "!" : "");
470 /* clear breakpoints/watchpoints and VCR*/
471 arm11_sc7_clear_vbw(arm11
);
473 if (!debug_execution
)
474 target_free_all_working_areas(target
);
476 /* Set up breakpoints */
477 if (handle_breakpoints
)
479 /* check if one matches PC and step over it if necessary */
481 struct breakpoint
* bp
;
483 for (bp
= target
->breakpoints
; bp
; bp
= bp
->next
)
485 if (bp
->address
== address
)
487 LOG_DEBUG("must step over %08" PRIx32
"", bp
->address
);
488 arm11_step(target
, 1, 0, 0);
493 /* set all breakpoints */
495 unsigned brp_num
= 0;
497 for (bp
= target
->breakpoints
; bp
; bp
= bp
->next
)
499 struct arm11_sc7_action brp
[2];
502 brp
[0].address
= ARM11_SC7_BVR0
+ brp_num
;
503 brp
[0].value
= bp
->address
;
505 brp
[1].address
= ARM11_SC7_BCR0
+ brp_num
;
506 brp
[1].value
= 0x1 | (3 << 1) | (0x0F << 5) | (0 << 14) | (0 << 16) | (0 << 20) | (0 << 21);
508 arm11_sc7_run(arm11
, brp
, ARRAY_SIZE(brp
));
510 LOG_DEBUG("Add BP %d at %08" PRIx32
, brp_num
,
516 arm11_sc7_set_vcr(arm11
, arm11_vcr
);
519 arm11_leave_debug_state(arm11
, handle_breakpoints
);
521 arm11_add_IR(arm11
, ARM11_RESTART
, TAP_IDLE
);
523 CHECK_RETVAL(jtag_execute_queue());
528 CHECK_RETVAL(arm11_read_DSCR(arm11
));
530 LOG_DEBUG("DSCR %08x", (unsigned) arm11
->dscr
);
532 if (arm11
->dscr
& DSCR_CORE_RESTARTED
)
543 if ((timeval_ms()-then
) > 1000)
545 LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
552 target
->debug_reason
= DBG_REASON_NOTHALTED
;
553 if (!debug_execution
)
554 target
->state
= TARGET_RUNNING
;
556 target
->state
= TARGET_DEBUG_RUNNING
;
557 CHECK_RETVAL(target_call_event_callbacks(target
, TARGET_EVENT_RESUMED
));
562 static int arm11_step(struct target
*target
, int current
,
563 uint32_t address
, int handle_breakpoints
)
565 LOG_DEBUG("target->state: %s",
566 target_state_name(target
));
568 if (target
->state
!= TARGET_HALTED
)
570 LOG_WARNING("target was not halted");
571 return ERROR_TARGET_NOT_HALTED
;
574 struct arm11_common
*arm11
= target_to_arm11(target
);
576 address
= arm11_nextpc(arm11
, current
, address
);
578 LOG_DEBUG("STEP PC %08" PRIx32
"%s", address
, !current
? "!" : "");
581 /** \todo TODO: Thumb not supported here */
583 uint32_t next_instruction
;
585 CHECK_RETVAL(arm11_read_memory_word(arm11
, address
, &next_instruction
));
588 if ((next_instruction
& 0xFFF00070) == 0xe1200070)
590 address
= arm11_nextpc(arm11
, 0, address
+ 4);
591 LOG_DEBUG("Skipping BKPT");
593 /* skip over Wait for interrupt / Standby */
594 /* mcr 15, 0, r?, cr7, cr0, {4} */
595 else if ((next_instruction
& 0xFFFF0FFF) == 0xee070f90)
597 address
= arm11_nextpc(arm11
, 0, address
+ 4);
598 LOG_DEBUG("Skipping WFI");
600 /* ignore B to self */
601 else if ((next_instruction
& 0xFEFFFFFF) == 0xeafffffe)
603 LOG_DEBUG("Not stepping jump to self");
607 /** \todo TODO: check if break-/watchpoints make any sense at all in combination
610 /** \todo TODO: check if disabling IRQs might be a good idea here. Alternatively
611 * the VCR might be something worth looking into. */
614 /* Set up breakpoint for stepping */
616 struct arm11_sc7_action brp
[2];
619 brp
[0].address
= ARM11_SC7_BVR0
;
621 brp
[1].address
= ARM11_SC7_BCR0
;
623 if (arm11_config_hardware_step
)
625 /* Hardware single stepping ("instruction address
626 * mismatch") is used if enabled. It's not quite
627 * exactly "run one instruction"; "branch to here"
628 * loops won't break, neither will some other cases,
629 * but it's probably the best default.
631 * Hardware single stepping isn't supported on v6
632 * debug modules. ARM1176 and v7 can support it...
634 * FIXME Thumb stepping likely needs to use 0x03
635 * or 0xc0 byte masks, not 0x0f.
637 brp
[0].value
= address
;
638 brp
[1].value
= 0x1 | (3 << 1) | (0x0F << 5)
639 | (0 << 14) | (0 << 16) | (0 << 20)
643 /* Sets a breakpoint on the next PC, as calculated
644 * by instruction set simulation.
646 * REVISIT stepping Thumb on ARM1156 requires Thumb2
647 * support from the simulator.
652 retval
= arm_simulate_step(target
, &next_pc
);
653 if (retval
!= ERROR_OK
)
656 brp
[0].value
= next_pc
;
657 brp
[1].value
= 0x1 | (3 << 1) | (0x0F << 5)
658 | (0 << 14) | (0 << 16) | (0 << 20)
662 CHECK_RETVAL(arm11_sc7_run(arm11
, brp
, ARRAY_SIZE(brp
)));
667 if (arm11_config_step_irq_enable
)
668 /* this disable should be redundant ... */
669 arm11
->dscr
&= ~DSCR_INT_DIS
;
671 arm11
->dscr
|= DSCR_INT_DIS
;
674 CHECK_RETVAL(arm11_leave_debug_state(arm11
, handle_breakpoints
));
676 arm11_add_IR(arm11
, ARM11_RESTART
, TAP_IDLE
);
678 CHECK_RETVAL(jtag_execute_queue());
685 const uint32_t mask
= DSCR_CORE_RESTARTED
688 CHECK_RETVAL(arm11_read_DSCR(arm11
));
689 LOG_DEBUG("DSCR %08x e", (unsigned) arm11
->dscr
);
691 if ((arm11
->dscr
& mask
) == mask
)
701 if ((timeval_ms()-then
) > 1000)
703 LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
710 /* clear breakpoint */
711 arm11_sc7_clear_vbw(arm11
);
714 CHECK_RETVAL(arm11_debug_entry(arm11
));
716 /* restore default state */
717 arm11
->dscr
&= ~DSCR_INT_DIS
;
721 target
->debug_reason
= DBG_REASON_SINGLESTEP
;
723 CHECK_RETVAL(target_call_event_callbacks(target
, TARGET_EVENT_HALTED
));
728 static int arm11_assert_reset(struct target
*target
)
731 struct arm11_common
*arm11
= target_to_arm11(target
);
733 retval
= arm11_check_init(arm11
);
734 if (retval
!= ERROR_OK
)
737 target
->state
= TARGET_UNKNOWN
;
739 /* we would very much like to reset into the halted, state,
740 * but resetting and halting is second best... */
741 if (target
->reset_halt
)
743 CHECK_RETVAL(target_halt(target
));
747 /* srst is funny. We can not do *anything* else while it's asserted
748 * and it has unkonwn side effects. Make sure no other code runs
751 * Code below assumes srst:
753 * - Causes power-on-reset (but of what parts of the system?). Bug
756 * - Messes us TAP state without asserting trst.
758 * - There is another bug in the arm11 core. When you generate an access to
759 * external logic (for example ddr controller via AHB bus) and that block
760 * is not configured (perhaps it is still held in reset), that transaction
761 * will never complete. This will hang arm11 core but it will also hang
762 * JTAG controller. Nothing, short of srst assertion will bring it out of
767 * - What should the PC be after an srst reset when starting in the halted
771 jtag_add_reset(0, 1);
772 jtag_add_reset(0, 0);
774 /* How long do we have to wait? */
775 jtag_add_sleep(5000);
777 /* un-mess up TAP state */
780 retval
= jtag_execute_queue();
781 if (retval
!= ERROR_OK
)
789 static int arm11_deassert_reset(struct target
*target
)
794 static int arm11_soft_reset_halt(struct target
*target
)
796 LOG_WARNING("Not implemented: %s", __func__
);
801 /* target memory access
802 * size: 1 = byte (8bit), 2 = half-word (16bit), 4 = word (32bit)
803 * count: number of items of <size>
805 * arm11_config_memrw_no_increment - in the future we may want to be able
806 * to read/write a range of data to a "port". a "port" is an action on
807 * read memory address for some peripheral.
809 static int arm11_read_memory_inner(struct target
*target
,
810 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
,
811 bool arm11_config_memrw_no_increment
)
813 /** \todo TODO: check if buffer cast to uint32_t* and uint16_t* might cause alignment problems */
816 if (target
->state
!= TARGET_HALTED
)
818 LOG_WARNING("target was not halted");
819 return ERROR_TARGET_NOT_HALTED
;
822 LOG_DEBUG("ADDR %08" PRIx32
" SIZE %08" PRIx32
" COUNT %08" PRIx32
"", address
, size
, count
);
824 struct arm11_common
*arm11
= target_to_arm11(target
);
826 retval
= arm11_run_instr_data_prepare(arm11
);
827 if (retval
!= ERROR_OK
)
830 /* MRC p14,0,r0,c0,c5,0 */
831 retval
= arm11_run_instr_data_to_core1(arm11
, 0xee100e15, address
);
832 if (retval
!= ERROR_OK
)
838 arm11
->arm
.core_cache
->reg_list
[1].dirty
= true;
840 for (size_t i
= 0; i
< count
; i
++)
842 /* ldrb r1, [r0], #1 */
844 arm11_run_instr_no_data1(arm11
,
845 !arm11_config_memrw_no_increment
? 0xe4d01001 : 0xe5d01000);
848 /* MCR p14,0,R1,c0,c5,0 */
849 arm11_run_instr_data_from_core(arm11
, 0xEE001E15, &res
, 1);
858 arm11
->arm
.core_cache
->reg_list
[1].dirty
= true;
860 for (size_t i
= 0; i
< count
; i
++)
862 /* ldrh r1, [r0], #2 */
863 arm11_run_instr_no_data1(arm11
,
864 !arm11_config_memrw_no_increment
? 0xe0d010b2 : 0xe1d010b0);
868 /* MCR p14,0,R1,c0,c5,0 */
869 arm11_run_instr_data_from_core(arm11
, 0xEE001E15, &res
, 1);
871 uint16_t svalue
= res
;
872 memcpy(buffer
+ i
* sizeof(uint16_t), &svalue
, sizeof(uint16_t));
880 uint32_t instr
= !arm11_config_memrw_no_increment
? 0xecb05e01 : 0xed905e00;
881 /** \todo TODO: buffer cast to uint32_t* causes alignment warnings */
882 uint32_t *words
= (uint32_t *)buffer
;
884 /* LDC p14,c5,[R0],#4 */
885 /* LDC p14,c5,[R0] */
886 arm11_run_instr_data_from_core(arm11
, instr
, words
, count
);
891 return arm11_run_instr_data_finish(arm11
);
894 static int arm11_read_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
896 return arm11_read_memory_inner(target
, address
, size
, count
, buffer
, false);
900 * no_increment - in the future we may want to be able
901 * to read/write a range of data to a "port". a "port" is an action on
902 * read memory address for some peripheral.
904 static int arm11_write_memory_inner(struct target
*target
,
905 uint32_t address
, uint32_t size
,
906 uint32_t count
, uint8_t *buffer
,
911 if (target
->state
!= TARGET_HALTED
)
913 LOG_WARNING("target was not halted");
914 return ERROR_TARGET_NOT_HALTED
;
917 LOG_DEBUG("ADDR %08" PRIx32
" SIZE %08" PRIx32
" COUNT %08" PRIx32
"", address
, size
, count
);
919 struct arm11_common
*arm11
= target_to_arm11(target
);
921 retval
= arm11_run_instr_data_prepare(arm11
);
922 if (retval
!= ERROR_OK
)
925 /* MRC p14,0,r0,c0,c5,0 */
926 retval
= arm11_run_instr_data_to_core1(arm11
, 0xee100e15, address
);
927 if (retval
!= ERROR_OK
)
930 /* burst writes are not used for single words as those may well be
931 * reset init script writes.
933 * The other advantage is that as burst writes are default, we'll
934 * now exercise both burst and non-burst code paths with the
935 * default settings, increasing code coverage.
937 bool burst
= arm11_config_memwrite_burst
&& (count
> 1);
943 arm11
->arm
.core_cache
->reg_list
[1].dirty
= true;
945 for (size_t i
= 0; i
< count
; i
++)
947 /* MRC p14,0,r1,c0,c5,0 */
948 retval
= arm11_run_instr_data_to_core1(arm11
, 0xee101e15, *buffer
++);
949 if (retval
!= ERROR_OK
)
952 /* strb r1, [r0], #1 */
954 retval
= arm11_run_instr_no_data1(arm11
,
958 if (retval
!= ERROR_OK
)
967 arm11
->arm
.core_cache
->reg_list
[1].dirty
= true;
969 for (size_t i
= 0; i
< count
; i
++)
972 memcpy(&value
, buffer
+ i
* sizeof(uint16_t), sizeof(uint16_t));
974 /* MRC p14,0,r1,c0,c5,0 */
975 retval
= arm11_run_instr_data_to_core1(arm11
, 0xee101e15, value
);
976 if (retval
!= ERROR_OK
)
979 /* strh r1, [r0], #2 */
981 retval
= arm11_run_instr_no_data1(arm11
,
985 if (retval
!= ERROR_OK
)
993 uint32_t instr
= !no_increment
? 0xeca05e01 : 0xed805e00;
995 /** \todo TODO: buffer cast to uint32_t* causes alignment warnings */
996 uint32_t *words
= (uint32_t*)buffer
;
1000 /* STC p14,c5,[R0],#4 */
1001 /* STC p14,c5,[R0]*/
1002 retval
= arm11_run_instr_data_to_core(arm11
, instr
, words
, count
);
1003 if (retval
!= ERROR_OK
)
1008 /* STC p14,c5,[R0],#4 */
1009 /* STC p14,c5,[R0]*/
1010 retval
= arm11_run_instr_data_to_core_noack(arm11
, instr
, words
, count
);
1011 if (retval
!= ERROR_OK
)
1019 /* r0 verification */
1024 /* MCR p14,0,R0,c0,c5,0 */
1025 retval
= arm11_run_instr_data_from_core(arm11
, 0xEE000E15, &r0
, 1);
1026 if (retval
!= ERROR_OK
)
1029 if (address
+ size
* count
!= r0
)
1031 LOG_ERROR("Data transfer failed. Expected end "
1032 "address 0x%08x, got 0x%08x",
1033 (unsigned) (address
+ size
* count
),
1037 LOG_ERROR("use 'arm11 memwrite burst disable' to disable fast burst mode");
1039 if (arm11_config_memwrite_error_fatal
)
1044 return arm11_run_instr_data_finish(arm11
);
1047 static int arm11_write_memory(struct target
*target
,
1048 uint32_t address
, uint32_t size
,
1049 uint32_t count
, uint8_t *buffer
)
1051 /* pointer increment matters only for multi-unit writes ...
1052 * not e.g. to a "reset the chip" controller.
1054 return arm11_write_memory_inner(target
, address
, size
,
1055 count
, buffer
, count
== 1);
1058 /* write target memory in multiples of 4 byte, optimized for writing large quantities of data */
1059 static int arm11_bulk_write_memory(struct target
*target
,
1060 uint32_t address
, uint32_t count
, uint8_t *buffer
)
1062 if (target
->state
!= TARGET_HALTED
)
1064 LOG_WARNING("target was not halted");
1065 return ERROR_TARGET_NOT_HALTED
;
1068 return arm11_write_memory(target
, address
, 4, count
, buffer
);
1071 /* target break-/watchpoint control
1072 * rw: 0 = write, 1 = read, 2 = access
1074 static int arm11_add_breakpoint(struct target
*target
,
1075 struct breakpoint
*breakpoint
)
1077 struct arm11_common
*arm11
= target_to_arm11(target
);
1080 if (breakpoint
->type
== BKPT_SOFT
)
1082 LOG_INFO("sw breakpoint requested, but software breakpoints not enabled");
1083 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1087 if (!arm11
->free_brps
)
1089 LOG_DEBUG("no breakpoint unit available for hardware breakpoint");
1090 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1093 if (breakpoint
->length
!= 4)
1095 LOG_DEBUG("only breakpoints of four bytes length supported");
1096 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1104 static int arm11_remove_breakpoint(struct target
*target
,
1105 struct breakpoint
*breakpoint
)
1107 struct arm11_common
*arm11
= target_to_arm11(target
);
1114 static int arm11_target_create(struct target
*target
, Jim_Interp
*interp
)
1116 struct arm11_common
*arm11
;
1118 if (target
->tap
== NULL
)
1121 if (target
->tap
->ir_length
!= 5)
1123 LOG_ERROR("'target arm11' expects IR LENGTH = 5");
1124 return ERROR_COMMAND_SYNTAX_ERROR
;
1127 arm11
= calloc(1, sizeof *arm11
);
1131 armv4_5_init_arch_info(target
, &arm11
->arm
);
1133 arm11
->jtag_info
.tap
= target
->tap
;
1134 arm11
->jtag_info
.scann_size
= 5;
1135 arm11
->jtag_info
.scann_instr
= ARM11_SCAN_N
;
1136 /* cur_scan_chain == 0 */
1137 arm11
->jtag_info
.intest_instr
= ARM11_INTEST
;
1142 static int arm11_init_target(struct command_context
*cmd_ctx
,
1143 struct target
*target
)
1145 /* Initialize anything we can set up without talking to the target */
1149 /* talk to the target and set things up */
1150 static int arm11_examine(struct target
*target
)
1154 struct arm11_common
*arm11
= target_to_arm11(target
);
1155 uint32_t didr
, device_id
;
1156 uint8_t implementor
;
1158 /* FIXME split into do-first-time and do-every-time logic ... */
1162 arm11_add_IR(arm11
, ARM11_IDCODE
, ARM11_TAP_DEFAULT
);
1164 struct scan_field idcode_field
;
1166 arm11_setup_field(arm11
, 32, NULL
, &device_id
, &idcode_field
);
1168 arm11_add_dr_scan_vc(1, &idcode_field
, TAP_DRPAUSE
);
1172 arm11_add_debug_SCAN_N(arm11
, 0x00, ARM11_TAP_DEFAULT
);
1174 arm11_add_IR(arm11
, ARM11_INTEST
, ARM11_TAP_DEFAULT
);
1176 struct scan_field chain0_fields
[2];
1178 arm11_setup_field(arm11
, 32, NULL
, &didr
, chain0_fields
+ 0);
1179 arm11_setup_field(arm11
, 8, NULL
, &implementor
, chain0_fields
+ 1);
1181 arm11_add_dr_scan_vc(ARRAY_SIZE(chain0_fields
), chain0_fields
, TAP_IDLE
);
1183 CHECK_RETVAL(jtag_execute_queue());
1185 switch (device_id
& 0x0FFFF000)
1194 arm11
->arm
.core_type
= ARM_MODE_MON
;
1198 LOG_ERROR("'target arm11' expects IDCODE 0x*7B*7****");
1201 LOG_INFO("found %s", type
);
1203 /* unlikely this could ever fail, but ... */
1204 switch ((didr
>> 16) & 0x0F) {
1205 case ARM11_DEBUG_V6
:
1206 case ARM11_DEBUG_V61
: /* supports security extensions */
1209 LOG_ERROR("Only ARM v6 and v6.1 debug supported.");
1213 arm11
->brp
= ((didr
>> 24) & 0x0F) + 1;
1214 arm11
->wrp
= ((didr
>> 28) & 0x0F) + 1;
1216 /** \todo TODO: reserve one brp slot if we allow breakpoints during step */
1217 arm11
->free_brps
= arm11
->brp
;
1219 LOG_DEBUG("IDCODE %08" PRIx32
" IMPLEMENTOR %02x DIDR %08" PRIx32
,
1220 device_id
, implementor
, didr
);
1222 /* as a side-effect this reads DSCR and thus
1223 * clears the ARM11_DSCR_STICKY_PRECISE_DATA_ABORT / Sticky Precise Data Abort Flag
1224 * as suggested by the spec.
1227 retval
= arm11_check_init(arm11
);
1228 if (retval
!= ERROR_OK
)
1231 /* Build register cache "late", after target_init(), since we
1232 * want to know if this core supports Secure Monitor mode.
1234 if (!target_was_examined(target
))
1235 retval
= arm11_dpm_init(arm11
, didr
);
1237 /* ETM on ARM11 still uses original scanchain 6 access mode */
1238 if (arm11
->arm
.etm
&& !target_was_examined(target
)) {
1239 *register_get_last_cache_p(&target
->reg_cache
) =
1240 etm_build_reg_cache(target
, &arm11
->jtag_info
,
1242 retval
= etm_setup(target
);
1245 target_set_examined(target
);
1251 /* FIXME all these BOOL_WRAPPER things should be modifying
1252 * per-instance state, not shared state; ditto the vector
1253 * catch register support. Scan chains with multiple cores
1254 * should be able to say "work with this core like this,
1255 * that core like that". Example, ARM11 MPCore ...
1258 #define ARM11_BOOL_WRAPPER(name, print_name) \
1259 COMMAND_HANDLER(arm11_handle_bool_##name) \
1261 return CALL_COMMAND_HANDLER(handle_command_parse_bool, \
1262 &arm11_config_##name, print_name); \
1265 ARM11_BOOL_WRAPPER(memwrite_burst
, "memory write burst mode")
1266 ARM11_BOOL_WRAPPER(memwrite_error_fatal
, "fatal error mode for memory writes")
1267 ARM11_BOOL_WRAPPER(step_irq_enable
, "IRQs while stepping")
1268 ARM11_BOOL_WRAPPER(hardware_step
, "hardware single step")
1270 COMMAND_HANDLER(arm11_handle_vcr
)
1276 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], arm11_vcr
);
1279 return ERROR_COMMAND_SYNTAX_ERROR
;
1282 LOG_INFO("VCR 0x%08" PRIx32
"", arm11_vcr
);
1286 static const struct command_registration arm11_mw_command_handlers
[] = {
1289 .handler
= &arm11_handle_bool_memwrite_burst
,
1290 .mode
= COMMAND_ANY
,
1291 .help
= "Enable/Disable non-standard but fast burst mode"
1292 " (default: enabled)",
1295 .name
= "error_fatal",
1296 .handler
= &arm11_handle_bool_memwrite_error_fatal
,
1297 .mode
= COMMAND_ANY
,
1298 .help
= "Terminate program if transfer error was found"
1299 " (default: enabled)",
1301 COMMAND_REGISTRATION_DONE
1303 static const struct command_registration arm11_any_command_handlers
[] = {
1305 /* "hardware_step" is only here to check if the default
1306 * simulate + breakpoint implementation is broken.
1307 * TEMPORARY! NOT DOCUMENTED! */
1308 .name
= "hardware_step",
1309 .handler
= &arm11_handle_bool_hardware_step
,
1310 .mode
= COMMAND_ANY
,
1311 .help
= "DEBUG ONLY - Hardware single stepping"
1312 " (default: disabled)",
1313 .usage
= "(enable|disable)",
1317 .mode
= COMMAND_ANY
,
1318 .help
= "memwrite command group",
1319 .chain
= arm11_mw_command_handlers
,
1322 .name
= "step_irq_enable",
1323 .handler
= &arm11_handle_bool_step_irq_enable
,
1324 .mode
= COMMAND_ANY
,
1325 .help
= "Enable interrupts while stepping"
1326 " (default: disabled)",
1330 .handler
= &arm11_handle_vcr
,
1331 .mode
= COMMAND_ANY
,
1332 .help
= "Control (Interrupt) Vector Catch Register",
1334 COMMAND_REGISTRATION_DONE
1336 static const struct command_registration arm11_command_handlers
[] = {
1338 .chain
= arm_command_handlers
,
1341 .chain
= etm_command_handlers
,
1345 .mode
= COMMAND_ANY
,
1346 .help
= "ARM11 command group",
1347 .chain
= arm11_any_command_handlers
,
1349 COMMAND_REGISTRATION_DONE
1352 /** Holds methods for ARM11xx targets. */
1353 struct target_type arm11_target
= {
1357 .arch_state
= arm11_arch_state
,
1359 .target_request_data
= arm11_target_request_data
,
1362 .resume
= arm11_resume
,
1365 .assert_reset
= arm11_assert_reset
,
1366 .deassert_reset
= arm11_deassert_reset
,
1367 .soft_reset_halt
= arm11_soft_reset_halt
,
1369 .get_gdb_reg_list
= armv4_5_get_gdb_reg_list
,
1371 .read_memory
= arm11_read_memory
,
1372 .write_memory
= arm11_write_memory
,
1374 .bulk_write_memory
= arm11_bulk_write_memory
,
1376 .checksum_memory
= arm_checksum_memory
,
1377 .blank_check_memory
= arm_blank_check_memory
,
1379 .add_breakpoint
= arm11_add_breakpoint
,
1380 .remove_breakpoint
= arm11_remove_breakpoint
,
1382 .run_algorithm
= armv4_5_run_algorithm
,
1384 .commands
= arm11_command_handlers
,
1385 .target_create
= arm11_target_create
,
1386 .init_target
= arm11_init_target
,
1387 .examine
= arm11_examine
,