1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2008 by Hongtao Zheng *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include "target_type.h"
32 #include "arm_opcodes.h"
35 * NOTE: this holds code that's used with multiple ARM9 processors:
36 * - ARM9TDMI (ARMv4T) ... in ARM920, ARM922, and ARM940 cores
37 * - ARM9E-S (ARMv5TE) ... in ARM946, ARM966, and ARM968 cores
38 * - ARM9EJS (ARMv5TEJ) ... in ARM926 core
40 * In short, the file name is a misnomer ... it is NOT specific to
41 * that first generation ARM9 processor, or cores using it.
45 #define _DEBUG_INSTRUCTION_EXECUTION_
48 enum arm9tdmi_vector_bit
{
49 ARM9TDMI_RESET_VECTOR
= 0x01,
50 ARM9TDMI_UNDEF_VECTOR
= 0x02,
51 ARM9TDMI_SWI_VECTOR
= 0x04,
52 ARM9TDMI_PABT_VECTOR
= 0x08,
53 ARM9TDMI_DABT_VECTOR
= 0x10,
54 /* BIT(5) reserved -- must be zero */
55 ARM9TDMI_IRQ_VECTOR
= 0x40,
56 ARM9TDMI_FIQ_VECTOR
= 0x80,
59 static const struct arm9tdmi_vector
{
62 } arm9tdmi_vectors
[] = {
63 {"reset", ARM9TDMI_RESET_VECTOR
},
64 {"undef", ARM9TDMI_UNDEF_VECTOR
},
65 {"swi", ARM9TDMI_SWI_VECTOR
},
66 {"pabt", ARM9TDMI_PABT_VECTOR
},
67 {"dabt", ARM9TDMI_DABT_VECTOR
},
68 {"irq", ARM9TDMI_IRQ_VECTOR
},
69 {"fiq", ARM9TDMI_FIQ_VECTOR
},
73 int arm9tdmi_examine_debug_reason(struct target
*target
)
75 int retval
= ERROR_OK
;
76 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
78 /* only check the debug reason if we don't know it already */
79 if ((target
->debug_reason
!= DBG_REASON_DBGRQ
)
80 && (target
->debug_reason
!= DBG_REASON_SINGLESTEP
)) {
81 struct scan_field fields
[3];
83 uint8_t instructionbus
[4];
86 fields
[0].num_bits
= 32;
87 fields
[0].out_value
= NULL
;
88 fields
[0].in_value
= databus
;
90 fields
[1].num_bits
= 3;
91 fields
[1].out_value
= NULL
;
92 fields
[1].in_value
= &debug_reason
;
94 fields
[2].num_bits
= 32;
95 fields
[2].out_value
= NULL
;
96 fields
[2].in_value
= instructionbus
;
98 retval
= arm_jtag_scann(&arm7_9
->jtag_info
, 0x1, TAP_DRPAUSE
);
99 if (retval
!= ERROR_OK
)
101 retval
= arm_jtag_set_instr(arm7_9
->jtag_info
.tap
, arm7_9
->jtag_info
.intest_instr
, NULL
, TAP_DRPAUSE
);
102 if (retval
!= ERROR_OK
)
105 jtag_add_dr_scan(arm7_9
->jtag_info
.tap
, 3, fields
, TAP_DRPAUSE
);
106 retval
= jtag_execute_queue();
107 if (retval
!= ERROR_OK
)
110 fields
[0].in_value
= NULL
;
111 fields
[0].out_value
= databus
;
112 fields
[1].in_value
= NULL
;
113 fields
[1].out_value
= &debug_reason
;
114 fields
[2].in_value
= NULL
;
115 fields
[2].out_value
= instructionbus
;
117 jtag_add_dr_scan(arm7_9
->jtag_info
.tap
, 3, fields
, TAP_DRPAUSE
);
119 if (debug_reason
& 0x4)
120 if (debug_reason
& 0x2)
121 target
->debug_reason
= DBG_REASON_WPTANDBKPT
;
123 target
->debug_reason
= DBG_REASON_WATCHPOINT
;
125 target
->debug_reason
= DBG_REASON_BREAKPOINT
;
131 /* put an instruction in the ARM9TDMI pipeline or write the data bus,
132 * and optionally read data
134 int arm9tdmi_clock_out(struct arm_jtag
*jtag_info
, uint32_t instr
,
135 uint32_t out
, uint32_t *in
, int sysspeed
)
137 int retval
= ERROR_OK
;
138 struct scan_field fields
[3];
140 uint8_t instr_buf
[4];
141 uint8_t sysspeed_buf
= 0x0;
144 buf_set_u32(out_buf
, 0, 32, out
);
146 buf_set_u32(instr_buf
, 0, 32, flip_u32(instr
, 32));
149 buf_set_u32(&sysspeed_buf
, 2, 1, 1);
151 retval
= arm_jtag_scann(jtag_info
, 0x1, TAP_DRPAUSE
);
152 if (retval
!= ERROR_OK
)
155 retval
= arm_jtag_set_instr(jtag_info
->tap
, jtag_info
->intest_instr
, NULL
, TAP_DRPAUSE
);
156 if (retval
!= ERROR_OK
)
159 fields
[0].num_bits
= 32;
160 fields
[0].out_value
= out_buf
;
161 fields
[0].in_value
= NULL
;
163 fields
[1].num_bits
= 3;
164 fields
[1].out_value
= &sysspeed_buf
;
165 fields
[1].in_value
= NULL
;
167 fields
[2].num_bits
= 32;
168 fields
[2].out_value
= instr_buf
;
169 fields
[2].in_value
= NULL
;
172 fields
[0].in_value
= (uint8_t *)in
;
173 jtag_add_dr_scan(jtag_info
->tap
, 3, fields
, TAP_DRPAUSE
);
175 jtag_add_callback(arm_le_to_h_u32
, (jtag_callback_data_t
)in
);
177 jtag_add_dr_scan(jtag_info
->tap
, 3, fields
, TAP_DRPAUSE
);
179 jtag_add_runtest(0, TAP_DRPAUSE
);
181 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
183 retval
= jtag_execute_queue();
184 if (retval
!= ERROR_OK
)
188 LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x, in: 0x%8.8x", instr
, out
, *in
);
190 LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x", instr
, out
);
197 /* just read data (instruction and data-out = don't care) */
198 int arm9tdmi_clock_data_in(struct arm_jtag
*jtag_info
, uint32_t *in
)
200 int retval
= ERROR_OK
;
201 struct scan_field fields
[3];
203 retval
= arm_jtag_scann(jtag_info
, 0x1, TAP_DRPAUSE
);
204 if (retval
!= ERROR_OK
)
207 retval
= arm_jtag_set_instr(jtag_info
->tap
, jtag_info
->intest_instr
, NULL
, TAP_DRPAUSE
);
208 if (retval
!= ERROR_OK
)
211 fields
[0].num_bits
= 32;
212 fields
[0].out_value
= NULL
;
213 fields
[0].in_value
= (uint8_t *)in
;
215 fields
[1].num_bits
= 3;
216 fields
[1].out_value
= NULL
;
217 fields
[1].in_value
= NULL
;
219 fields
[2].num_bits
= 32;
220 fields
[2].out_value
= NULL
;
221 fields
[2].in_value
= NULL
;
223 jtag_add_dr_scan(jtag_info
->tap
, 3, fields
, TAP_DRPAUSE
);
225 jtag_add_callback(arm_le_to_h_u32
, (jtag_callback_data_t
)in
);
227 jtag_add_runtest(0, TAP_DRPAUSE
);
229 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
231 retval
= jtag_execute_queue();
232 if (retval
!= ERROR_OK
)
236 LOG_DEBUG("in: 0x%8.8x", *in
);
238 LOG_ERROR("BUG: called with in == NULL");
245 /* clock the target, and read the databus
246 * the *in pointer points to a buffer where elements of 'size' bytes
247 * are stored in big (be == 1) or little (be == 0) endianness
249 int arm9tdmi_clock_data_in_endianness(struct arm_jtag
*jtag_info
,
250 void *in
, int size
, int be
)
252 int retval
= ERROR_OK
;
253 struct scan_field fields
[2];
255 retval
= arm_jtag_scann(jtag_info
, 0x1, TAP_DRPAUSE
);
256 if (retval
!= ERROR_OK
)
259 retval
= arm_jtag_set_instr(jtag_info
->tap
, jtag_info
->intest_instr
, NULL
, TAP_DRPAUSE
);
260 if (retval
!= ERROR_OK
)
264 fields
[0].num_bits
= 32;
265 fields
[0].out_value
= NULL
;
266 fields
[0].in_value
= in
;
268 fields
[1].num_bits
= 3 + 32;
269 fields
[1].out_value
= NULL
;
270 fields
[1].in_value
= NULL
;
272 /* Discard irrelevant bits of the scan, making sure we don't write more
273 * than size bytes to in */
274 fields
[0].num_bits
= size
* 8;
275 fields
[0].out_value
= NULL
;
276 fields
[0].in_value
= in
;
278 fields
[1].num_bits
= 3 + 32 + 32 - size
* 8;
279 fields
[1].out_value
= NULL
;
280 fields
[1].in_value
= NULL
;
283 jtag_add_dr_scan(jtag_info
->tap
, 2, fields
, TAP_DRPAUSE
);
285 jtag_add_callback4(arm7_9_endianness_callback
,
286 (jtag_callback_data_t
)in
,
287 (jtag_callback_data_t
)size
,
288 (jtag_callback_data_t
)be
,
289 (jtag_callback_data_t
)0);
291 jtag_add_runtest(0, TAP_DRPAUSE
);
293 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
295 retval
= jtag_execute_queue();
296 if (retval
!= ERROR_OK
)
300 LOG_DEBUG("in: 0x%8.8x", *(uint32_t *)in
);
302 LOG_ERROR("BUG: called with in == NULL");
309 static void arm9tdmi_change_to_arm(struct target
*target
,
310 uint32_t *r0
, uint32_t *pc
)
312 int retval
= ERROR_OK
;
313 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
314 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
316 /* save r0 before using it and put system in ARM state
317 * to allow common handling of ARM and THUMB debugging */
319 /* fetch STR r0, [r0] */
320 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_STR(0, 0), 0, NULL
, 0);
321 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
322 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
323 /* STR r0, [r0] in Memory */
324 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, r0
, 0);
326 /* MOV r0, r15 fetched, STR in Decode */
327 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_MOV(0, 15), 0, NULL
, 0);
328 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
329 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_STR(0, 0), 0, NULL
, 0);
330 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
331 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
332 /* nothing fetched, STR r0, [r0] in Memory */
333 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, pc
, 0);
335 /* use pc-relative LDR to clear r0[1:0] (for switch to ARM mode) */
336 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_LDR_PCREL(0), 0, NULL
, 0);
338 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
340 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
341 /* LDR in Memory (to account for interlock) */
342 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
345 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_BX(0), 0, NULL
, 0);
346 /* NOP fetched, BX in Decode, MOV in Execute */
347 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
348 /* NOP fetched, BX in Execute (1) */
349 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
351 retval
= jtag_execute_queue();
352 if (retval
!= ERROR_OK
)
355 /* fix program counter:
356 * MOV r0, r15 was the 5th instruction (+8)
357 * reading PC in Thumb state gives address of instruction + 4
362 void arm9tdmi_read_core_regs(struct target
*target
,
363 uint32_t mask
, uint32_t *core_regs
[16])
366 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
367 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
369 /* STMIA r0-15, [r0] at debug speed
370 * register values will start to appear on 4th DCLK
372 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STMIA(0, mask
& 0xffff, 0, 0), 0, NULL
, 0);
374 /* fetch NOP, STM in DECODE stage */
375 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
376 /* fetch NOP, STM in EXECUTE stage (1st cycle) */
377 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
379 for (i
= 0; i
<= 15; i
++) {
381 /* nothing fetched, STM in MEMORY (i'th cycle) */
382 arm9tdmi_clock_data_in(jtag_info
, core_regs
[i
]);
386 static void arm9tdmi_read_core_regs_target_buffer(struct target
*target
,
387 uint32_t mask
, void *buffer
, int size
)
390 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
391 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
392 int be
= (target
->endianness
== TARGET_BIG_ENDIAN
) ? 1 : 0;
393 uint32_t *buf_u32
= buffer
;
394 uint16_t *buf_u16
= buffer
;
395 uint8_t *buf_u8
= buffer
;
397 /* STMIA r0-15, [r0] at debug speed
398 * register values will start to appear on 4th DCLK
400 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STMIA(0, mask
& 0xffff, 0, 0), 0, NULL
, 0);
402 /* fetch NOP, STM in DECODE stage */
403 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
404 /* fetch NOP, STM in EXECUTE stage (1st cycle) */
405 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
407 for (i
= 0; i
<= 15; i
++) {
409 /* nothing fetched, STM in MEMORY (i'th cycle) */
412 arm9tdmi_clock_data_in_endianness(jtag_info
, buf_u32
++, 4, be
);
415 arm9tdmi_clock_data_in_endianness(jtag_info
, buf_u16
++, 2, be
);
418 arm9tdmi_clock_data_in_endianness(jtag_info
, buf_u8
++, 1, be
);
424 static void arm9tdmi_read_xpsr(struct target
*target
, uint32_t *xpsr
, int spsr
)
426 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
427 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
430 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MRS(0, spsr
& 1), 0, NULL
, 0);
431 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
432 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
433 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
434 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
437 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STR(0, 15), 0, NULL
, 0);
438 /* fetch NOP, STR in DECODE stage */
439 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
440 /* fetch NOP, STR in EXECUTE stage (1st cycle) */
441 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
442 /* nothing fetched, STR in MEMORY */
443 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, xpsr
, 0);
446 static void arm9tdmi_write_xpsr(struct target
*target
, uint32_t xpsr
, int spsr
)
448 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
449 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
451 LOG_DEBUG("xpsr: %8.8" PRIx32
", spsr: %i", xpsr
, spsr
);
454 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM(xpsr
& 0xff, 0, 1, spsr
), 0, NULL
, 0);
455 /* MSR2 fetched, MSR1 in DECODE */
456 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM((xpsr
& 0xff00) >> 8, 0xc, 2, spsr
), 0, NULL
, 0);
457 /* MSR3 fetched, MSR1 in EXECUTE (1), MSR2 in DECODE */
458 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM((xpsr
& 0xff0000) >> 16, 0x8, 4, spsr
), 0, NULL
, 0);
459 /* nothing fetched, MSR1 in EXECUTE (2) */
460 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
461 /* nothing fetched, MSR1 in EXECUTE (3) */
462 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
463 /* MSR4 fetched, MSR2 in EXECUTE (1), MSR3 in DECODE */
464 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM((xpsr
& 0xff000000) >> 24, 0x4, 8, spsr
), 0, NULL
, 0);
465 /* nothing fetched, MSR2 in EXECUTE (2) */
466 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
467 /* nothing fetched, MSR2 in EXECUTE (3) */
468 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
469 /* NOP fetched, MSR3 in EXECUTE (1), MSR4 in DECODE */
470 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
471 /* nothing fetched, MSR3 in EXECUTE (2) */
472 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
473 /* nothing fetched, MSR3 in EXECUTE (3) */
474 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
475 /* NOP fetched, MSR4 in EXECUTE (1) */
476 /* last MSR writes flags, which takes only one cycle */
477 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
480 static void arm9tdmi_write_xpsr_im8(struct target
*target
,
481 uint8_t xpsr_im
, int rot
, int spsr
)
483 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
484 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
486 LOG_DEBUG("xpsr_im: %2.2x, rot: %i, spsr: %i", xpsr_im
, rot
, spsr
);
489 arm9tdmi_clock_out(jtag_info
, ARMV4_5_MSR_IM(xpsr_im
, rot
, 1, spsr
), 0, NULL
, 0);
490 /* NOP fetched, MSR in DECODE */
491 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
492 /* NOP fetched, MSR in EXECUTE (1) */
493 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
495 /* rot == 4 writes flags, which takes only one cycle */
497 /* nothing fetched, MSR in EXECUTE (2) */
498 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
499 /* nothing fetched, MSR in EXECUTE (3) */
500 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
504 void arm9tdmi_write_core_regs(struct target
*target
,
505 uint32_t mask
, uint32_t core_regs
[16])
508 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
509 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
511 /* LDMIA r0-15, [r0] at debug speed
512 * register values will start to appear on 4th DCLK
514 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, mask
& 0xffff, 0, 0), 0, NULL
, 0);
516 /* fetch NOP, LDM in DECODE stage */
517 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
518 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
519 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
521 for (i
= 0; i
<= 15; i
++) {
523 /* nothing fetched, LDM still in EXECUTE (1 + i cycle) */
524 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, core_regs
[i
], NULL
, 0);
526 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
529 void arm9tdmi_load_word_regs(struct target
*target
, uint32_t mask
)
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-multiple into the pipeline */
535 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, mask
& 0xffff, 0, 1), 0, NULL
, 0);
536 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
539 void arm9tdmi_load_hword_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 half-word into the pipeline */
545 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDRH_IP(num
, 0), 0, NULL
, 0);
546 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
549 void arm9tdmi_load_byte_reg(struct target
*target
, int num
)
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 load byte into the pipeline */
555 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDRB_IP(num
, 0), 0, NULL
, 0);
556 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
559 void arm9tdmi_store_word_regs(struct target
*target
, uint32_t mask
)
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-multiple into the pipeline */
565 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STMIA(0, mask
, 0, 1), 0, NULL
, 0);
566 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
569 void arm9tdmi_store_hword_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 half-word into the pipeline */
575 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STRH_IP(num
, 0), 0, NULL
, 0);
576 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
579 void arm9tdmi_store_byte_reg(struct target
*target
, int num
)
581 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
582 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
584 /* put system-speed store byte into the pipeline */
585 arm9tdmi_clock_out(jtag_info
, ARMV4_5_STRB_IP(num
, 0), 0, NULL
, 0);
586 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
589 static void arm9tdmi_write_pc(struct target
*target
, uint32_t pc
)
591 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
592 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
594 /* LDMIA r0-15, [r0] at debug speed
595 * register values will start to appear on 4th DCLK
597 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, 0x8000, 0, 0), 0, NULL
, 0);
599 /* fetch NOP, LDM in DECODE stage */
600 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
601 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
602 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
603 /* nothing fetched, LDM in EXECUTE stage (2nd cycle) (output data) */
604 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, pc
, NULL
, 0);
605 /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
606 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
607 /* fetch NOP, LDM in EXECUTE stage (4th cycle) */
608 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
609 /* fetch NOP, LDM in EXECUTE stage (5th cycle) */
610 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
613 void arm9tdmi_branch_resume(struct target
*target
)
615 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
616 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
618 arm9tdmi_clock_out(jtag_info
, ARMV4_5_B(0xfffffc, 0), 0, NULL
, 0);
619 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 1);
622 static void arm9tdmi_branch_resume_thumb(struct target
*target
)
626 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
627 struct arm
*arm
= &arm7_9
->arm
;
628 struct arm_jtag
*jtag_info
= &arm7_9
->jtag_info
;
629 struct reg
*dbg_stat
= &arm7_9
->eice_cache
->reg_list
[EICE_DBG_STAT
];
631 /* LDMIA r0-15, [r0] at debug speed
632 * register values will start to appear on 4th DCLK
634 arm9tdmi_clock_out(jtag_info
, ARMV4_5_LDMIA(0, 0x1, 0, 0), 0, NULL
, 0);
636 /* fetch NOP, LDM in DECODE stage */
637 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
638 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
639 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
640 /* nothing fetched, LDM in EXECUTE stage (2nd cycle) */
641 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
,
642 buf_get_u32(arm
->pc
->value
, 0, 32) | 1, NULL
, 0);
643 /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
644 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
646 /* Branch and eXchange */
647 arm9tdmi_clock_out(jtag_info
, ARMV4_5_BX(0), 0, NULL
, 0);
649 embeddedice_read_reg(dbg_stat
);
651 /* fetch NOP, BX in DECODE stage */
652 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
654 embeddedice_read_reg(dbg_stat
);
656 /* fetch NOP, BX in EXECUTE stage (1st cycle) */
657 arm9tdmi_clock_out(jtag_info
, ARMV4_5_NOP
, 0, NULL
, 0);
659 /* target is now in Thumb state */
660 embeddedice_read_reg(dbg_stat
);
662 /* load r0 value, MOV_IM in Decode*/
663 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_LDR_PCREL(0), 0, NULL
, 0);
664 /* fetch NOP, LDR in Decode, MOV_IM in Execute */
665 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
666 /* fetch NOP, LDR in Execute */
667 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
668 /* nothing fetched, LDR in EXECUTE stage (2nd cycle) */
669 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
,
670 buf_get_u32(arm
->core_cache
->reg_list
[0].value
, 0, 32), NULL
, 0);
671 /* nothing fetched, LDR in EXECUTE stage (3rd cycle) */
672 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
674 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
675 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
677 embeddedice_read_reg(dbg_stat
);
679 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_B(0x7f7), 0, NULL
, 1);
680 arm9tdmi_clock_out(jtag_info
, ARMV4_5_T_NOP
, 0, NULL
, 0);
683 void arm9tdmi_enable_single_step(struct target
*target
, uint32_t next_pc
)
685 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
687 if (arm7_9
->has_single_step
) {
688 buf_set_u32(arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
].value
, 3, 1, 1);
689 embeddedice_store_reg(&arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
]);
691 arm7_9_enable_eice_step(target
, next_pc
);
694 void arm9tdmi_disable_single_step(struct target
*target
)
696 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
698 if (arm7_9
->has_single_step
) {
699 buf_set_u32(arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
].value
, 3, 1, 0);
700 embeddedice_store_reg(&arm7_9
->eice_cache
->reg_list
[EICE_DBG_CTRL
]);
702 arm7_9_disable_eice_step(target
);
705 static void arm9tdmi_build_reg_cache(struct target
*target
)
707 struct reg_cache
**cache_p
= register_get_last_cache_p(&target
->reg_cache
);
708 struct arm
*arm
= target_to_arm(target
);
710 (*cache_p
) = arm_build_reg_cache(target
, arm
);
713 int arm9tdmi_init_target(struct command_context
*cmd_ctx
,
714 struct target
*target
)
716 arm9tdmi_build_reg_cache(target
);
720 int arm9tdmi_init_arch_info(struct target
*target
,
721 struct arm7_9_common
*arm7_9
, struct jtag_tap
*tap
)
723 /* prepare JTAG information for the new target */
724 arm7_9
->jtag_info
.tap
= tap
;
725 arm7_9
->jtag_info
.scann_size
= 5;
727 /* register arch-specific functions */
728 arm7_9
->examine_debug_reason
= arm9tdmi_examine_debug_reason
;
729 arm7_9
->change_to_arm
= arm9tdmi_change_to_arm
;
730 arm7_9
->read_core_regs
= arm9tdmi_read_core_regs
;
731 arm7_9
->read_core_regs_target_buffer
= arm9tdmi_read_core_regs_target_buffer
;
732 arm7_9
->read_xpsr
= arm9tdmi_read_xpsr
;
734 arm7_9
->write_xpsr
= arm9tdmi_write_xpsr
;
735 arm7_9
->write_xpsr_im8
= arm9tdmi_write_xpsr_im8
;
736 arm7_9
->write_core_regs
= arm9tdmi_write_core_regs
;
738 arm7_9
->load_word_regs
= arm9tdmi_load_word_regs
;
739 arm7_9
->load_hword_reg
= arm9tdmi_load_hword_reg
;
740 arm7_9
->load_byte_reg
= arm9tdmi_load_byte_reg
;
742 arm7_9
->store_word_regs
= arm9tdmi_store_word_regs
;
743 arm7_9
->store_hword_reg
= arm9tdmi_store_hword_reg
;
744 arm7_9
->store_byte_reg
= arm9tdmi_store_byte_reg
;
746 arm7_9
->write_pc
= arm9tdmi_write_pc
;
747 arm7_9
->branch_resume
= arm9tdmi_branch_resume
;
748 arm7_9
->branch_resume_thumb
= arm9tdmi_branch_resume_thumb
;
750 arm7_9
->enable_single_step
= arm9tdmi_enable_single_step
;
751 arm7_9
->disable_single_step
= arm9tdmi_disable_single_step
;
753 arm7_9
->write_memory
= arm7_9_write_memory
;
754 arm7_9
->bulk_write_memory
= arm7_9_bulk_write_memory
;
756 arm7_9
->post_debug_entry
= NULL
;
758 arm7_9
->pre_restore_context
= NULL
;
760 /* initialize arch-specific breakpoint handling */
761 arm7_9
->arm_bkpt
= 0xdeeedeee;
762 arm7_9
->thumb_bkpt
= 0xdeee;
764 arm7_9
->dbgreq_adjust_pc
= 3;
766 arm7_9_init_arch_info(target
, arm7_9
);
768 /* override use of DBGRQ, this is safe on ARM9TDMI */
769 arm7_9
->use_dbgrq
= 1;
771 /* all ARM9s have the vector catch register */
772 arm7_9
->has_vector_catch
= 1;
777 static int arm9tdmi_target_create(struct target
*target
, Jim_Interp
*interp
)
779 struct arm7_9_common
*arm7_9
= calloc(1, sizeof(struct arm7_9_common
));
781 arm9tdmi_init_arch_info(target
, arm7_9
, target
->tap
);
782 arm7_9
->arm
.is_armv4
= true;
787 COMMAND_HANDLER(handle_arm9tdmi_catch_vectors_command
)
789 struct target
*target
= get_current_target(CMD_CTX
);
790 struct arm7_9_common
*arm7_9
= target_to_arm7_9(target
);
791 struct reg
*vector_catch
;
792 uint32_t vector_catch_value
;
794 if (!target_was_examined(target
)) {
795 LOG_ERROR("Target not examined yet");
799 /* it's uncommon, but some ARM7 chips can support this */
800 if (arm7_9
->common_magic
!= ARM7_9_COMMON_MAGIC
801 || !arm7_9
->has_vector_catch
) {
802 command_print(CMD_CTX
, "target doesn't have EmbeddedICE "
803 "with vector_catch");
804 return ERROR_TARGET_INVALID
;
807 vector_catch
= &arm7_9
->eice_cache
->reg_list
[EICE_VEC_CATCH
];
809 /* read the vector catch register if necessary */
810 if (!vector_catch
->valid
)
811 embeddedice_read_reg(vector_catch
);
813 /* get the current setting */
814 vector_catch_value
= buf_get_u32(vector_catch
->value
, 0, 8);
817 vector_catch_value
= 0x0;
818 if (strcmp(CMD_ARGV
[0], "all") == 0)
819 vector_catch_value
= 0xdf;
820 else if (strcmp(CMD_ARGV
[0], "none") == 0) {
823 for (unsigned i
= 0; i
< CMD_ARGC
; i
++) {
824 /* go through list of vectors */
826 for (j
= 0; arm9tdmi_vectors
[j
].name
; j
++) {
827 if (strcmp(CMD_ARGV
[i
], arm9tdmi_vectors
[j
].name
) == 0) {
828 vector_catch_value
|= arm9tdmi_vectors
[j
].value
;
833 /* complain if vector wasn't found */
834 if (!arm9tdmi_vectors
[j
].name
) {
835 command_print(CMD_CTX
, "vector '%s' not found, leaving current setting unchanged", CMD_ARGV
[i
]);
837 /* reread current setting */
838 vector_catch_value
= buf_get_u32(
846 /* store new settings */
847 buf_set_u32(vector_catch
->value
, 0, 8, vector_catch_value
);
848 embeddedice_store_reg(vector_catch
);
851 /* output current settings */
852 for (unsigned i
= 0; arm9tdmi_vectors
[i
].name
; i
++) {
853 command_print(CMD_CTX
, "%s: %s", arm9tdmi_vectors
[i
].name
,
854 (vector_catch_value
& arm9tdmi_vectors
[i
].value
)
855 ? "catch" : "don't catch");
861 static const struct command_registration arm9tdmi_exec_command_handlers
[] = {
863 .name
= "vector_catch",
864 .handler
= handle_arm9tdmi_catch_vectors_command
,
865 .mode
= COMMAND_EXEC
,
866 .help
= "Display, after optionally updating, configuration "
867 "of vector catch unit.",
868 .usage
= "[all|none|(reset|undef|swi|pabt|dabt|irq|fiq)*]",
870 COMMAND_REGISTRATION_DONE
872 const struct command_registration arm9tdmi_command_handlers
[] = {
874 .chain
= arm7_9_command_handlers
,
879 .help
= "arm9 command group",
881 .chain
= arm9tdmi_exec_command_handlers
,
883 COMMAND_REGISTRATION_DONE
886 /** Holds methods for ARM9TDMI targets. */
887 struct target_type arm9tdmi_target
= {
891 .arch_state
= arm_arch_state
,
893 .target_request_data
= arm7_9_target_request_data
,
896 .resume
= arm7_9_resume
,
899 .assert_reset
= arm7_9_assert_reset
,
900 .deassert_reset
= arm7_9_deassert_reset
,
901 .soft_reset_halt
= arm7_9_soft_reset_halt
,
903 .get_gdb_reg_list
= arm_get_gdb_reg_list
,
905 .read_memory
= arm7_9_read_memory
,
906 .write_memory
= arm7_9_write_memory_opt
,
908 .checksum_memory
= arm_checksum_memory
,
909 .blank_check_memory
= arm_blank_check_memory
,
911 .run_algorithm
= armv4_5_run_algorithm
,
913 .add_breakpoint
= arm7_9_add_breakpoint
,
914 .remove_breakpoint
= arm7_9_remove_breakpoint
,
915 .add_watchpoint
= arm7_9_add_watchpoint
,
916 .remove_watchpoint
= arm7_9_remove_watchpoint
,
918 .commands
= arm9tdmi_command_handlers
,
919 .target_create
= arm9tdmi_target_create
,
920 .init_target
= arm9tdmi_init_target
,
921 .examine
= arm7_9_examine
,
922 .check_reset
= arm7_9_check_reset
,