1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2008 by Spencer Oliver *
8 * spen@spen-soft.co.uk *
10 * Copyright (C) 2008 by Hongtao Zheng *
12 ***************************************************************************/
19 #include "target_type.h"
21 #include "arm_opcodes.h"
22 #include "arm_semihosting.h"
25 * NOTE: this holds code that's used with multiple ARM9 processors:
26 * - ARM9TDMI (ARMv4T) ... in ARM920, ARM922, and ARM940 cores
27 * - ARM9E-S (ARMv5TE) ... in ARM946, ARM966, and ARM968 cores
28 * - ARM9EJS (ARMv5TEJ) ... in ARM926 core
30 * In short, the file name is a misnomer ... it is NOT specific to
31 * that first generation ARM9 processor, or cores using it.
35 #define _DEBUG_INSTRUCTION_EXECUTION_
38 enum arm9tdmi_vector_bit
{
39 ARM9TDMI_RESET_VECTOR
= 0x01,
40 ARM9TDMI_UNDEF_VECTOR
= 0x02,
41 ARM9TDMI_SWI_VECTOR
= 0x04,
42 ARM9TDMI_PABT_VECTOR
= 0x08,
43 ARM9TDMI_DABT_VECTOR
= 0x10,
44 /* BIT(5) reserved -- must be zero */
45 ARM9TDMI_IRQ_VECTOR
= 0x40,
46 ARM9TDMI_FIQ_VECTOR
= 0x80,
49 static const struct arm9tdmi_vector
{
52 } arm9tdmi_vectors
[] = {
53 {"reset", ARM9TDMI_RESET_VECTOR
},
54 {"undef", ARM9TDMI_UNDEF_VECTOR
},
55 {"swi", ARM9TDMI_SWI_VECTOR
},
56 {"pabt", ARM9TDMI_PABT_VECTOR
},
57 {"dabt", ARM9TDMI_DABT_VECTOR
},
58 {"irq", ARM9TDMI_IRQ_VECTOR
},
59 {"fiq", ARM9TDMI_FIQ_VECTOR
},
63 int arm9tdmi_examine_debug_reason(struct target
*target
)
65 int retval
= ERROR_OK
;
66 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
68 /* only check the debug reason if we don't know it already */
69 if ((target
->debug_reason
!= DBG_REASON_DBGRQ
)
70 && (target
->debug_reason
!= DBG_REASON_SINGLESTEP
)) {
71 struct scan_field fields
[3];
73 uint8_t instructionbus
[4];
76 fields
[0].num_bits
= 32;
77 fields
[0].out_value
= NULL
;
78 fields
[0].in_value
= databus
;
80 fields
[1].num_bits
= 3;
81 fields
[1].out_value
= NULL
;
82 fields
[1].in_value
= &debug_reason
;
84 fields
[2].num_bits
= 32;
85 fields
[2].out_value
= NULL
;
86 fields
[2].in_value
= instructionbus
;
88 retval
= arm_jtag_scann(&arm7_9
->jtag_info
, 0x1, TAP_DRPAUSE
);
89 if (retval
!= ERROR_OK
)
91 retval
= arm_jtag_set_instr(arm7_9
->jtag_info
.tap
, arm7_9
->jtag_info
.intest_instr
, NULL
, TAP_DRPAUSE
);
92 if (retval
!= ERROR_OK
)
95 jtag_add_dr_scan(arm7_9
->jtag_info
.tap
, 3, fields
, TAP_DRPAUSE
);
96 retval
= jtag_execute_queue();
97 if (retval
!= ERROR_OK
)
100 fields
[0].in_value
= NULL
;
101 fields
[0].out_value
= databus
;
102 fields
[1].in_value
= NULL
;
103 fields
[1].out_value
= &debug_reason
;
104 fields
[2].in_value
= NULL
;
105 fields
[2].out_value
= instructionbus
;
107 jtag_add_dr_scan(arm7_9
->jtag_info
.tap
, 3, fields
, TAP_DRPAUSE
);
109 if (debug_reason
& 0x4)
110 if (debug_reason
& 0x2)
111 target
->debug_reason
= DBG_REASON_WPTANDBKPT
;
113 target
->debug_reason
= DBG_REASON_WATCHPOINT
;
115 target
->debug_reason
= DBG_REASON_BREAKPOINT
;
121 /* put an instruction in the ARM9TDMI pipeline or write the data bus,
122 * and optionally read data
124 int arm9tdmi_clock_out(struct arm_jtag
*jtag_info
, uint32_t instr
,
125 uint32_t out
, uint32_t *in
, int sysspeed
)
127 int retval
= ERROR_OK
;
128 struct scan_field fields
[3];
130 uint8_t instr_buf
[4];
131 uint8_t sysspeed_buf
= 0x0;
134 buf_set_u32(out_buf
, 0, 32, out
);
136 buf_set_u32(instr_buf
, 0, 32, flip_u32(instr
, 32));
139 buf_set_u32(&sysspeed_buf
, 2, 1, 1);
141 retval
= arm_jtag_scann(jtag_info
, 0x1, TAP_DRPAUSE
);
142 if (retval
!= ERROR_OK
)
145 retval
= arm_jtag_set_instr(jtag_info
->tap
, jtag_info
->intest_instr
, NULL
, TAP_DRPAUSE
);
146 if (retval
!= ERROR_OK
)
149 fields
[0].num_bits
= 32;
150 fields
[0].out_value
= out_buf
;
151 fields
[0].in_value
= NULL
;
153 fields
[1].num_bits
= 3;
154 fields
[1].out_value
= &sysspeed_buf
;
155 fields
[1].in_value
= NULL
;
157 fields
[2].num_bits
= 32;
158 fields
[2].out_value
= instr_buf
;
159 fields
[2].in_value
= NULL
;
162 fields
[0].in_value
= (uint8_t *)in
;
163 jtag_add_dr_scan(jtag_info
->tap
, 3, fields
, TAP_DRPAUSE
);
165 jtag_add_callback(arm_le_to_h_u32
, (jtag_callback_data_t
)in
);
167 jtag_add_dr_scan(jtag_info
->tap
, 3, fields
, TAP_DRPAUSE
);
169 jtag_add_runtest(0, TAP_DRPAUSE
);
171 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
173 retval
= jtag_execute_queue();
174 if (retval
!= ERROR_OK
)
178 LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x, in: 0x%8.8x", instr
, out
, *in
);
180 LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x", instr
, out
);
187 /* just read data (instruction and data-out = don't care) */
188 int arm9tdmi_clock_data_in(struct arm_jtag
*jtag_info
, uint32_t *in
)
190 int retval
= ERROR_OK
;
191 struct scan_field fields
[3];
193 retval
= arm_jtag_scann(jtag_info
, 0x1, TAP_DRPAUSE
);
194 if (retval
!= ERROR_OK
)
197 retval
= arm_jtag_set_instr(jtag_info
->tap
, jtag_info
->intest_instr
, NULL
, TAP_DRPAUSE
);
198 if (retval
!= ERROR_OK
)
201 fields
[0].num_bits
= 32;
202 fields
[0].out_value
= NULL
;
203 fields
[0].in_value
= (uint8_t *)in
;
205 fields
[1].num_bits
= 3;
206 fields
[1].out_value
= NULL
;
207 fields
[1].in_value
= NULL
;
209 fields
[2].num_bits
= 32;
210 fields
[2].out_value
= NULL
;
211 fields
[2].in_value
= NULL
;
213 jtag_add_dr_scan(jtag_info
->tap
, 3, fields
, TAP_DRPAUSE
);
215 jtag_add_callback(arm_le_to_h_u32
, (jtag_callback_data_t
)in
);
217 jtag_add_runtest(0, TAP_DRPAUSE
);
219 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
221 retval
= jtag_execute_queue();
222 if (retval
!= ERROR_OK
)
226 LOG_DEBUG("in: 0x%8.8x", *in
);
228 LOG_ERROR("BUG: called with in == NULL");
235 /* clock the target, and read the databus
236 * the *in pointer points to a buffer where elements of 'size' bytes
237 * are stored in big (be == 1) or little (be == 0) endianness
239 int arm9tdmi_clock_data_in_endianness(struct arm_jtag
*jtag_info
,
240 void *in
, int size
, int be
)
242 int retval
= ERROR_OK
;
243 struct scan_field fields
[2];
245 retval
= arm_jtag_scann(jtag_info
, 0x1, TAP_DRPAUSE
);
246 if (retval
!= ERROR_OK
)
249 retval
= arm_jtag_set_instr(jtag_info
->tap
, jtag_info
->intest_instr
, NULL
, TAP_DRPAUSE
);
250 if (retval
!= ERROR_OK
)
254 fields
[0].num_bits
= 32;
255 fields
[0].out_value
= NULL
;
256 fields
[0].in_value
= in
;
258 fields
[1].num_bits
= 3 + 32;
259 fields
[1].out_value
= NULL
;
260 fields
[1].in_value
= NULL
;
262 /* Discard irrelevant bits of the scan, making sure we don't write more
263 * than size bytes to in */
264 fields
[0].num_bits
= size
* 8;
265 fields
[0].out_value
= NULL
;
266 fields
[0].in_value
= in
;
268 fields
[1].num_bits
= 3 + 32 + 32 - size
* 8;
269 fields
[1].out_value
= NULL
;
270 fields
[1].in_value
= NULL
;
273 jtag_add_dr_scan(jtag_info
->tap
, 2, fields
, TAP_DRPAUSE
);
275 jtag_add_callback4(arm7_9_endianness_callback
,
276 (jtag_callback_data_t
)in
,
277 (jtag_callback_data_t
)size
,
278 (jtag_callback_data_t
)be
,
279 (jtag_callback_data_t
)0);
281 jtag_add_runtest(0, TAP_DRPAUSE
);
283 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
285 retval
= jtag_execute_queue();
286 if (retval
!= ERROR_OK
)
290 LOG_DEBUG("in: 0x%8.8x", *(uint32_t *)in
);
292 LOG_ERROR("BUG: called with in == NULL");
299 static void arm9tdmi_change_to_arm(struct target
*target
,
300 uint32_t *r0
, uint32_t *pc
)
302 int retval
= ERROR_OK
;
303 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
304 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
306 /* save r0 before using it and put system in ARM state
307 * to allow common handling of ARM and THUMB debugging */
309 /* fetch STR r0, [r0] */
310 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_STR(0, 0), 0, NULL
, 0);
311 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
312 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
313 /* STR r0, [r0] in Memory */
314 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, r0
, 0);
316 /* MOV r0, r15 fetched, STR in Decode */
317 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_MOV(0, 15), 0, NULL
, 0);
318 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
319 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_STR(0, 0), 0, NULL
, 0);
320 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
321 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
322 /* nothing fetched, STR r0, [r0] in Memory */
323 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, pc
, 0);
325 /* use pc-relative LDR to clear r0[1:0] (for switch to ARM mode) */
326 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_LDR_PCREL(0), 0, NULL
, 0);
328 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
330 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
331 /* LDR in Memory (to account for interlock) */
332 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
335 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_BX(0), 0, NULL
, 0);
336 /* NOP fetched, BX in Decode, MOV in Execute */
337 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
338 /* NOP fetched, BX in Execute (1) */
339 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
341 retval
= jtag_execute_queue();
342 if (retval
!= ERROR_OK
)
345 /* fix program counter:
346 * MOV r0, r15 was the 5th instruction (+8)
347 * reading PC in Thumb state gives address of instruction + 4
352 void arm9tdmi_read_core_regs(struct target
*target
,
353 uint32_t mask
, uint32_t *core_regs
[16])
356 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
357 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
359 /* STMIA r0-15, [r0] at debug speed
360 * register values will start to appear on 4th DCLK
362 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STMIA(0, mask
& 0xffff, 0, 0), 0, NULL
, 0);
364 /* fetch NOP, STM in DECODE stage */
365 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
366 /* fetch NOP, STM in EXECUTE stage (1st cycle) */
367 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
369 for (i
= 0; i
<= 15; i
++) {
371 /* nothing fetched, STM in MEMORY (i'th cycle) */
372 arm9tdmi_clock_data_in(jtag_info
, core_regs
[i
]);
376 static void arm9tdmi_read_core_regs_target_buffer(struct target
*target
,
377 uint32_t mask
, void *buffer
, int size
)
380 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
381 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
382 int be
= (target
->endianness
== TARGET_BIG_ENDIAN
) ? 1 : 0;
383 uint32_t *buf_u32
= buffer
;
384 uint16_t *buf_u16
= buffer
;
385 uint8_t *buf_u8
= buffer
;
387 /* STMIA r0-15, [r0] at debug speed
388 * register values will start to appear on 4th DCLK
390 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STMIA(0, mask
& 0xffff, 0, 0), 0, NULL
, 0);
392 /* fetch NOP, STM in DECODE stage */
393 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
394 /* fetch NOP, STM in EXECUTE stage (1st cycle) */
395 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
397 for (i
= 0; i
<= 15; i
++) {
399 /* nothing fetched, STM in MEMORY (i'th cycle) */
402 arm9tdmi_clock_data_in_endianness(jtag_info
, buf_u32
++, 4, be
);
405 arm9tdmi_clock_data_in_endianness(jtag_info
, buf_u16
++, 2, be
);
408 arm9tdmi_clock_data_in_endianness(jtag_info
, buf_u8
++, 1, be
);
414 static void arm9tdmi_read_xpsr(struct target
*target
, uint32_t *xpsr
, int spsr
)
416 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
417 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
420 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MRS(0, spsr
& 1), 0, NULL
, 0);
421 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
422 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
423 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
424 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
427 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STR(0, 15), 0, NULL
, 0);
428 /* fetch NOP, STR in DECODE stage */
429 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
430 /* fetch NOP, STR in EXECUTE stage (1st cycle) */
431 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
432 /* nothing fetched, STR in MEMORY */
433 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, xpsr
, 0);
436 static void arm9tdmi_write_xpsr(struct target
*target
, uint32_t xpsr
, int spsr
)
438 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
439 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
441 LOG_DEBUG("xpsr: %8.8" PRIx32
", spsr: %i", xpsr
, spsr
);
444 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM(xpsr
& 0xff, 0, 1, spsr
), 0, NULL
, 0);
445 /* MSR2 fetched, MSR1 in DECODE */
446 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM((xpsr
& 0xff00) >> 8, 0xc, 2, spsr
), 0, NULL
, 0);
447 /* MSR3 fetched, MSR1 in EXECUTE (1), MSR2 in DECODE */
448 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM((xpsr
& 0xff0000) >> 16, 0x8, 4, spsr
), 0, NULL
, 0);
449 /* nothing fetched, MSR1 in EXECUTE (2) */
450 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
451 /* nothing fetched, MSR1 in EXECUTE (3) */
452 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
453 /* MSR4 fetched, MSR2 in EXECUTE (1), MSR3 in DECODE */
454 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM((xpsr
& 0xff000000) >> 24, 0x4, 8, spsr
), 0, NULL
, 0);
455 /* nothing fetched, MSR2 in EXECUTE (2) */
456 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
457 /* nothing fetched, MSR2 in EXECUTE (3) */
458 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
459 /* NOP fetched, MSR3 in EXECUTE (1), MSR4 in DECODE */
460 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
461 /* nothing fetched, MSR3 in EXECUTE (2) */
462 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
463 /* nothing fetched, MSR3 in EXECUTE (3) */
464 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
465 /* NOP fetched, MSR4 in EXECUTE (1) */
466 /* last MSR writes flags, which takes only one cycle */
467 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
470 static void arm9tdmi_write_xpsr_im8(struct target
*target
,
471 uint8_t xpsr_im
, int rot
, int spsr
)
473 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
474 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
476 LOG_DEBUG("xpsr_im: %2.2x, rot: %i, spsr: %i", xpsr_im
, rot
, spsr
);
479 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM(xpsr_im
, rot
, 1, spsr
), 0, NULL
, 0);
480 /* NOP fetched, MSR in DECODE */
481 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
482 /* NOP fetched, MSR in EXECUTE (1) */
483 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
485 /* rot == 4 writes flags, which takes only one cycle */
487 /* nothing fetched, MSR in EXECUTE (2) */
488 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
489 /* nothing fetched, MSR in EXECUTE (3) */
490 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
494 void arm9tdmi_write_core_regs(struct target
*target
,
495 uint32_t mask
, uint32_t core_regs
[16])
498 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
499 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
501 /* LDMIA r0-15, [r0] at debug speed
502 * register values will start to appear on 4th DCLK
504 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, mask
& 0xffff, 0, 0), 0, NULL
, 0);
506 /* fetch NOP, LDM in DECODE stage */
507 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
508 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
509 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
511 for (i
= 0; i
<= 15; i
++) {
513 /* nothing fetched, LDM still in EXECUTE (1 + i cycle) */
514 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, core_regs
[i
], NULL
, 0);
516 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
519 void arm9tdmi_load_word_regs(struct target
*target
, uint32_t mask
)
521 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
522 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
524 /* put system-speed load-multiple into the pipeline */
525 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, mask
& 0xffff, 0, 1), 0, NULL
, 0);
526 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
529 void arm9tdmi_load_hword_reg(struct target
*target
, int num
)
531 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
532 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
534 /* put system-speed load half-word into the pipeline */
535 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDRH_IP(num
, 0), 0, NULL
, 0);
536 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
539 void arm9tdmi_load_byte_reg(struct target
*target
, int num
)
541 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
542 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
544 /* put system-speed load byte into the pipeline */
545 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDRB_IP(num
, 0), 0, NULL
, 0);
546 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
549 void arm9tdmi_store_word_regs(struct target
*target
, uint32_t mask
)
551 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
552 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
554 /* put system-speed store-multiple into the pipeline */
555 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STMIA(0, mask
, 0, 1), 0, NULL
, 0);
556 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
559 void arm9tdmi_store_hword_reg(struct target
*target
, int num
)
561 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
562 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
564 /* put system-speed store half-word into the pipeline */
565 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STRH_IP(num
, 0), 0, NULL
, 0);
566 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
569 void arm9tdmi_store_byte_reg(struct target
*target
, int num
)
571 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
572 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
574 /* put system-speed store byte into the pipeline */
575 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STRB_IP(num
, 0), 0, NULL
, 0);
576 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
579 static void arm9tdmi_write_pc(struct target
*target
, uint32_t pc
)
581 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
582 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
584 /* LDMIA r0-15, [r0] at debug speed
585 * register values will start to appear on 4th DCLK
587 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, 0x8000, 0, 0), 0, NULL
, 0);
589 /* fetch NOP, LDM in DECODE stage */
590 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
591 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
592 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
593 /* nothing fetched, LDM in EXECUTE stage (2nd cycle) (output data) */
594 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, pc
, NULL
, 0);
595 /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
596 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
597 /* fetch NOP, LDM in EXECUTE stage (4th cycle) */
598 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
599 /* fetch NOP, LDM in EXECUTE stage (5th cycle) */
600 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
603 void arm9tdmi_branch_resume(struct target
*target
)
605 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
606 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
608 arm9tdmi_clock_out(jtag_info
, ARMV4_5_B(0xfffffc, 0), 0, NULL
, 0);
609 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
612 static void arm9tdmi_branch_resume_thumb(struct target
*target
)
616 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
617 struct arm
*arm
= &arm7_9
->arm
;
618 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
619 struct reg
*dbg_stat
= &arm7_9
->eice_cache
->reg_list
[EICE_DBG_STAT
];
621 /* LDMIA r0-15, [r0] at debug speed
622 * register values will start to appear on 4th DCLK
624 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, 0x1, 0, 0), 0, NULL
, 0);
626 /* fetch NOP, LDM in DECODE stage */
627 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
628 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
629 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
630 /* nothing fetched, LDM in EXECUTE stage (2nd cycle) */
631 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
,
632 buf_get_u32(arm
->pc
->value
, 0, 32) | 1, NULL
, 0);
633 /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
634 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
636 /* Branch and eXchange */
637 arm9tdmi_clock_out(jtag_info
, ARMV4_5_BX(0), 0, NULL
, 0);
639 embeddedice_read_reg(dbg_stat
);
641 /* fetch NOP, BX in DECODE stage */
642 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
644 embeddedice_read_reg(dbg_stat
);
646 /* fetch NOP, BX in EXECUTE stage (1st cycle) */
647 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
649 /* target is now in Thumb state */
650 embeddedice_read_reg(dbg_stat
);
652 /* load r0 value, MOV_IM in Decode*/
653 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_LDR_PCREL(0), 0, NULL
, 0);
654 /* fetch NOP, LDR in Decode, MOV_IM in Execute */
655 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
656 /* fetch NOP, LDR in Execute */
657 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
658 /* nothing fetched, LDR in EXECUTE stage (2nd cycle) */
659 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
,
660 buf_get_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32), NULL
, 0);
661 /* nothing fetched, LDR in EXECUTE stage (3rd cycle) */
662 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
664 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
665 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
667 embeddedice_read_reg(dbg_stat
);
669 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_B(0x7f7), 0, NULL
, 1);
670 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
673 void arm9tdmi_enable_single_step(struct target
*target
, uint32_t next_pc
)
675 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
677 if (arm7_9
->has_single_step
) {
678 buf_set_u32(arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
].value
, 3, 1, 1);
679 embeddedice_store_reg(&arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
]);
681 arm7_9_enable_eice_step(target
, next_pc
);
684 void arm9tdmi_disable_single_step(struct target
*target
)
686 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
688 if (arm7_9
->has_single_step
) {
689 buf_set_u32(arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
].value
, 3, 1, 0);
690 embeddedice_store_reg(&arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
]);
692 arm7_9_disable_eice_step(target
);
695 static void arm9tdmi_build_reg_cache(struct target
*target
)
697 struct reg_cache
**cache_p
= register_get_last_cache_p(&target
->reg_cache
);
698 struct arm
*arm
= target_to_arm(target
);
700 (*cache_p
) = arm_build_reg_cache(target
, arm
);
703 int arm9tdmi_init_target(struct command_context
*cmd_ctx
,
704 struct target
*target
)
706 arm9tdmi_build_reg_cache(target
);
707 arm_semihosting_init(target
);
711 int arm9tdmi_init_arch_info(struct target
*target
,
712 struct arm7_9_common
*arm7_9
, struct jtag_tap
*tap
)
714 /* prepare JTAG information for the new target */
715 arm7_9
->jtag_info
.tap
= tap
;
716 arm7_9
->jtag_info
.scann_size
= 5;
718 /* register arch-specific functions */
719 arm7_9
->examine_debug_reason
= arm9tdmi_examine_debug_reason
;
720 arm7_9
->change_to_arm
= arm9tdmi_change_to_arm
;
721 arm7_9
->read_core_regs
= arm9tdmi_read_core_regs
;
722 arm7_9
->read_core_regs_target_buffer
= arm9tdmi_read_core_regs_target_buffer
;
723 arm7_9
->read_xpsr
= arm9tdmi_read_xpsr
;
725 arm7_9
->write_xpsr
= arm9tdmi_write_xpsr
;
726 arm7_9
->write_xpsr_im8
= arm9tdmi_write_xpsr_im8
;
727 arm7_9
->write_core_regs
= arm9tdmi_write_core_regs
;
729 arm7_9
->load_word_regs
= arm9tdmi_load_word_regs
;
730 arm7_9
->load_hword_reg
= arm9tdmi_load_hword_reg
;
731 arm7_9
->load_byte_reg
= arm9tdmi_load_byte_reg
;
733 arm7_9
->store_word_regs
= arm9tdmi_store_word_regs
;
734 arm7_9
->store_hword_reg
= arm9tdmi_store_hword_reg
;
735 arm7_9
->store_byte_reg
= arm9tdmi_store_byte_reg
;
737 arm7_9
->write_pc
= arm9tdmi_write_pc
;
738 arm7_9
->branch_resume
= arm9tdmi_branch_resume
;
739 arm7_9
->branch_resume_thumb
= arm9tdmi_branch_resume_thumb
;
741 arm7_9
->enable_single_step
= arm9tdmi_enable_single_step
;
742 arm7_9
->disable_single_step
= arm9tdmi_disable_single_step
;
744 arm7_9
->write_memory
= arm7_9_write_memory
;
745 arm7_9
->bulk_write_memory
= arm7_9_bulk_write_memory
;
747 arm7_9
->post_debug_entry
= NULL
;
749 arm7_9
->pre_restore_context
= NULL
;
751 /* initialize arch-specific breakpoint handling */
752 arm7_9
->arm_bkpt
= 0xdeeedeee;
753 arm7_9
->thumb_bkpt
= 0xdeee;
755 arm7_9
->dbgreq_adjust_pc
= 3;
757 arm7_9_init_arch_info(target
, arm7_9
);
759 /* override use of DBGRQ, this is safe on ARM9TDMI */
760 arm7_9
->use_dbgrq
= 1;
762 /* all ARM9s have the vector catch register */
763 arm7_9
->has_vector_catch
= 1;
768 static int arm9tdmi_target_create(struct target
*target
, Jim_Interp
*interp
)
770 struct arm7_9_common
*arm7_9
= calloc(1, sizeof(struct arm7_9_common
));
772 arm9tdmi_init_arch_info(target
, arm7_9
, target
->tap
);
773 arm7_9
->arm
.arch
= ARM_ARCH_V4
;
778 void arm9tdmi_deinit_target(struct target
*target
)
780 struct arm
*arm
= target_to_arm(target
);
781 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
783 arm7_9_deinit(target
);
784 arm_free_reg_cache(arm
);
788 COMMAND_HANDLER(handle_arm9tdmi_catch_vectors_command
)
790 struct target
*target
= get_current_target(CMD_CTX
);
791 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
792 struct reg
*vector_catch
;
793 uint32_t vector_catch_value
;
795 if (!target_was_examined(target
)) {
796 LOG_ERROR("Target not examined yet");
800 /* it's uncommon, but some ARM7 chips can support this */
801 if (arm7_9
->common_magic
!= ARM7_9_COMMON_MAGIC
802 || !arm7_9
->has_vector_catch
) {
803 command_print(CMD
, "target doesn't have EmbeddedICE "
804 "with vector_catch");
805 return ERROR_TARGET_INVALID
;
808 vector_catch
= &arm7_9
->eice_cache
->reg_list
[EICE_VEC_CATCH
];
810 /* read the vector catch register if necessary */
811 if (!vector_catch
->valid
)
812 embeddedice_read_reg(vector_catch
);
814 /* get the current setting */
815 vector_catch_value
= buf_get_u32(vector_catch
->value
, 0, 8);
818 vector_catch_value
= 0x0;
819 if (strcmp(CMD_ARGV
[0], "all") == 0)
820 vector_catch_value
= 0xdf;
821 else if (strcmp(CMD_ARGV
[0], "none") == 0) {
824 for (unsigned i
= 0; i
< CMD_ARGC
; i
++) {
825 /* go through list of vectors */
827 for (j
= 0; arm9tdmi_vectors
[j
].name
; j
++) {
828 if (strcmp(CMD_ARGV
[i
], arm9tdmi_vectors
[j
].name
) == 0) {
829 vector_catch_value
|= arm9tdmi_vectors
[j
].value
;
834 /* complain if vector wasn't found */
835 if (!arm9tdmi_vectors
[j
].name
) {
836 command_print(CMD
, "vector '%s' not found, leaving current setting unchanged", CMD_ARGV
[i
]);
838 /* reread current setting */
839 vector_catch_value
= buf_get_u32(
847 /* store new settings */
848 buf_set_u32(vector_catch
->value
, 0, 8, vector_catch_value
);
849 embeddedice_store_reg(vector_catch
);
852 /* output current settings */
853 for (unsigned i
= 0; arm9tdmi_vectors
[i
].name
; i
++) {
854 command_print(CMD
, "%s: %s", arm9tdmi_vectors
[i
].name
,
855 (vector_catch_value
& arm9tdmi_vectors
[i
].value
)
856 ? "catch" : "don't catch");
862 static const struct command_registration arm9tdmi_exec_command_handlers
[] = {
864 .name
= "vector_catch",
865 .handler
= handle_arm9tdmi_catch_vectors_command
,
866 .mode
= COMMAND_EXEC
,
867 .help
= "Display, after optionally updating, configuration "
868 "of vector catch unit.",
869 .usage
= "[all|none|(reset|undef|swi|pabt|dabt|irq|fiq)*]",
871 COMMAND_REGISTRATION_DONE
873 const struct command_registration arm9tdmi_command_handlers
[] = {
875 .chain
= arm7_9_command_handlers
,
880 .help
= "arm9 command group",
882 .chain
= arm9tdmi_exec_command_handlers
,
884 COMMAND_REGISTRATION_DONE
887 /** Holds methods for ARM9TDMI targets. */
888 struct target_type arm9tdmi_target
= {
892 .arch_state
= arm_arch_state
,
894 .target_request_data
= arm7_9_target_request_data
,
897 .resume
= arm7_9_resume
,
900 .assert_reset
= arm7_9_assert_reset
,
901 .deassert_reset
= arm7_9_deassert_reset
,
902 .soft_reset_halt
= arm7_9_soft_reset_halt
,
904 .get_gdb_arch
= arm_get_gdb_arch
,
905 .get_gdb_reg_list
= arm_get_gdb_reg_list
,
907 .read_memory
= arm7_9_read_memory
,
908 .write_memory
= arm7_9_write_memory_opt
,
910 .checksum_memory
= arm_checksum_memory
,
911 .blank_check_memory
= arm_blank_check_memory
,
913 .run_algorithm
= armv4_5_run_algorithm
,
915 .add_breakpoint
= arm7_9_add_breakpoint
,
916 .remove_breakpoint
= arm7_9_remove_breakpoint
,
917 .add_watchpoint
= arm7_9_add_watchpoint
,
918 .remove_watchpoint
= arm7_9_remove_watchpoint
,
920 .commands
= arm9tdmi_command_handlers
,
921 .target_create
= arm9tdmi_target_create
,
922 .init_target
= arm9tdmi_init_target
,
923 .deinit_target
= arm9tdmi_deinit_target
,
924 .examine
= arm7_9_examine
,
925 .check_reset
= arm7_9_check_reset
,