target: remove unused interface fn that clutters code
[openocd.git] / src / target / arm9tdmi.c
blob72a24b4c8bb38d412c8637dd3f4ec0f0d6901fd6
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * Copyright (C) 2008 by Hongtao Zheng *
9 * hontor@126.com *
10 * *
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. *
15 * *
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. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include "arm9tdmi.h"
31 #include "target_type.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.
44 #if 0
45 #define _DEBUG_INSTRUCTION_EXECUTION_
46 #endif
48 /* forward declarations */
49 static int arm9tdmi_target_create(struct target_s *target, Jim_Interp *interp);
51 target_type_t arm9tdmi_target =
53 .name = "arm9tdmi",
55 .poll = arm7_9_poll,
56 .arch_state = armv4_5_arch_state,
58 .target_request_data = arm7_9_target_request_data,
60 .halt = arm7_9_halt,
61 .resume = arm7_9_resume,
62 .step = arm7_9_step,
64 .assert_reset = arm7_9_assert_reset,
65 .deassert_reset = arm7_9_deassert_reset,
66 .soft_reset_halt = arm7_9_soft_reset_halt,
68 .get_gdb_reg_list = armv4_5_get_gdb_reg_list,
70 .read_memory = arm7_9_read_memory,
71 .write_memory = arm7_9_write_memory,
72 .bulk_write_memory = arm7_9_bulk_write_memory,
73 .checksum_memory = arm7_9_checksum_memory,
74 .blank_check_memory = arm7_9_blank_check_memory,
76 .run_algorithm = armv4_5_run_algorithm,
78 .add_breakpoint = arm7_9_add_breakpoint,
79 .remove_breakpoint = arm7_9_remove_breakpoint,
80 .add_watchpoint = arm7_9_add_watchpoint,
81 .remove_watchpoint = arm7_9_remove_watchpoint,
83 .register_commands = arm9tdmi_register_commands,
84 .target_create = arm9tdmi_target_create,
85 .init_target = arm9tdmi_init_target,
86 .examine = arm9tdmi_examine,
89 static arm9tdmi_vector_t arm9tdmi_vectors[] =
91 {"reset", ARM9TDMI_RESET_VECTOR},
92 {"undef", ARM9TDMI_UNDEF_VECTOR},
93 {"swi", ARM9TDMI_SWI_VECTOR},
94 {"pabt", ARM9TDMI_PABT_VECTOR},
95 {"dabt", ARM9TDMI_DABT_VECTOR},
96 {"irq", ARM9TDMI_IRQ_VECTOR},
97 {"fiq", ARM9TDMI_FIQ_VECTOR},
98 {0, 0},
101 int arm9tdmi_examine_debug_reason(target_t *target)
103 int retval = ERROR_OK;
104 /* get pointers to arch-specific information */
105 armv4_5_common_t *armv4_5 = target->arch_info;
106 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
108 /* only check the debug reason if we don't know it already */
109 if ((target->debug_reason != DBG_REASON_DBGRQ)
110 && (target->debug_reason != DBG_REASON_SINGLESTEP))
112 scan_field_t fields[3];
113 uint8_t databus[4];
114 uint8_t instructionbus[4];
115 uint8_t debug_reason;
117 jtag_set_end_state(TAP_DRPAUSE);
119 fields[0].tap = arm7_9->jtag_info.tap;
120 fields[0].num_bits = 32;
121 fields[0].out_value = NULL;
122 fields[0].in_value = databus;
124 fields[1].tap = arm7_9->jtag_info.tap;
125 fields[1].num_bits = 3;
126 fields[1].out_value = NULL;
127 fields[1].in_value = &debug_reason;
129 fields[2].tap = arm7_9->jtag_info.tap;
130 fields[2].num_bits = 32;
131 fields[2].out_value = NULL;
132 fields[2].in_value = instructionbus;
134 if ((retval = arm_jtag_scann(&arm7_9->jtag_info, 0x1)) != ERROR_OK)
136 return retval;
138 arm_jtag_set_instr(&arm7_9->jtag_info, arm7_9->jtag_info.intest_instr, NULL);
140 jtag_add_dr_scan(3, fields, jtag_set_end_state(TAP_DRPAUSE));
141 if ((retval = jtag_execute_queue()) != ERROR_OK)
143 return retval;
146 fields[0].in_value = NULL;
147 fields[0].out_value = databus;
148 fields[1].in_value = NULL;
149 fields[1].out_value = &debug_reason;
150 fields[2].in_value = NULL;
151 fields[2].out_value = instructionbus;
153 jtag_add_dr_scan(3, fields, jtag_set_end_state(TAP_DRPAUSE));
155 if (debug_reason & 0x4)
156 if (debug_reason & 0x2)
157 target->debug_reason = DBG_REASON_WPTANDBKPT;
158 else
159 target->debug_reason = DBG_REASON_WATCHPOINT;
160 else
161 target->debug_reason = DBG_REASON_BREAKPOINT;
164 return ERROR_OK;
167 /* put an instruction in the ARM9TDMI pipeline or write the data bus, and optionally read data */
168 int arm9tdmi_clock_out(arm_jtag_t *jtag_info, uint32_t instr, uint32_t out, uint32_t *in, int sysspeed)
170 int retval = ERROR_OK;
171 scan_field_t fields[3];
172 uint8_t out_buf[4];
173 uint8_t instr_buf[4];
174 uint8_t sysspeed_buf = 0x0;
176 /* prepare buffer */
177 buf_set_u32(out_buf, 0, 32, out);
179 buf_set_u32(instr_buf, 0, 32, flip_u32(instr, 32));
181 if (sysspeed)
182 buf_set_u32(&sysspeed_buf, 2, 1, 1);
184 jtag_set_end_state(TAP_DRPAUSE);
185 if ((retval = arm_jtag_scann(jtag_info, 0x1)) != ERROR_OK)
187 return retval;
190 arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
192 fields[0].tap = jtag_info->tap;
193 fields[0].num_bits = 32;
194 fields[0].out_value = out_buf;
195 fields[0].in_value = NULL;
197 fields[1].tap = jtag_info->tap;
198 fields[1].num_bits = 3;
199 fields[1].out_value = &sysspeed_buf;
200 fields[1].in_value = NULL;
202 fields[2].tap = jtag_info->tap;
203 fields[2].num_bits = 32;
204 fields[2].out_value = instr_buf;
205 fields[2].in_value = NULL;
207 if (in)
209 fields[0].in_value = (uint8_t *)in;
210 jtag_add_dr_scan(3, fields, jtag_get_end_state());
212 jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)in);
214 else
216 jtag_add_dr_scan(3, fields, jtag_get_end_state());
219 jtag_add_runtest(0, jtag_get_end_state());
221 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
223 if ((retval = jtag_execute_queue()) != ERROR_OK)
225 return retval;
228 if (in)
230 LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x, in: 0x%8.8x", instr, out, *in);
232 else
233 LOG_DEBUG("instr: 0x%8.8x, out: 0x%8.8x", instr, out);
235 #endif
237 return ERROR_OK;
240 /* just read data (instruction and data-out = don't care) */
241 int arm9tdmi_clock_data_in(arm_jtag_t *jtag_info, uint32_t *in)
243 int retval = ERROR_OK;;
244 scan_field_t fields[3];
246 jtag_set_end_state(TAP_DRPAUSE);
247 if ((retval = arm_jtag_scann(jtag_info, 0x1)) != ERROR_OK)
249 return retval;
252 arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
254 fields[0].tap = jtag_info->tap;
255 fields[0].num_bits = 32;
256 fields[0].out_value = NULL;
257 fields[0].in_value = (uint8_t *)in;
259 fields[1].tap = jtag_info->tap;
260 fields[1].num_bits = 3;
261 fields[1].out_value = NULL;
262 fields[1].in_value = NULL;
264 fields[2].tap = jtag_info->tap;
265 fields[2].num_bits = 32;
266 fields[2].out_value = NULL;
267 fields[2].in_value = NULL;
269 jtag_add_dr_scan(3, fields, jtag_get_end_state());
271 jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)in);
273 jtag_add_runtest(0, jtag_get_end_state());
275 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
277 if ((retval = jtag_execute_queue()) != ERROR_OK)
279 return retval;
282 if (in)
284 LOG_DEBUG("in: 0x%8.8x", *in);
286 else
288 LOG_ERROR("BUG: called with in == NULL");
291 #endif
293 return ERROR_OK;
296 extern void arm_endianness(uint8_t *tmp, void *in, int size, int be, int flip);
298 static int arm9endianness(jtag_callback_data_t arg, jtag_callback_data_t size, jtag_callback_data_t be, jtag_callback_data_t captured)
300 uint8_t *in = (uint8_t *)arg;
301 arm_endianness((uint8_t *)captured, in, (int)size, (int)be, 0);
302 return ERROR_OK;
305 /* clock the target, and read the databus
306 * the *in pointer points to a buffer where elements of 'size' bytes
307 * are stored in big (be == 1) or little (be == 0) endianness
309 int arm9tdmi_clock_data_in_endianness(arm_jtag_t *jtag_info, void *in, int size, int be)
311 int retval = ERROR_OK;
312 scan_field_t fields[3];
314 jtag_set_end_state(TAP_DRPAUSE);
315 if ((retval = arm_jtag_scann(jtag_info, 0x1)) != ERROR_OK)
317 return retval;
320 arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
322 fields[0].tap = jtag_info->tap;
323 fields[0].num_bits = 32;
324 fields[0].out_value = NULL;
325 jtag_alloc_in_value32(&fields[0]);
327 fields[1].tap = jtag_info->tap;
328 fields[1].num_bits = 3;
329 fields[1].out_value = NULL;
330 fields[1].in_value = NULL;
332 fields[2].tap = jtag_info->tap;
333 fields[2].num_bits = 32;
334 fields[2].out_value = NULL;
335 fields[2].in_value = NULL;
337 jtag_add_dr_scan(3, fields, jtag_get_end_state());
339 jtag_add_callback4(arm9endianness, (jtag_callback_data_t)in, (jtag_callback_data_t)size, (jtag_callback_data_t)be, (jtag_callback_data_t)fields[0].in_value);
341 jtag_add_runtest(0, jtag_get_end_state());
343 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
345 if ((retval = jtag_execute_queue()) != ERROR_OK)
347 return retval;
350 if (in)
352 LOG_DEBUG("in: 0x%8.8x", *(uint32_t*)in);
354 else
356 LOG_ERROR("BUG: called with in == NULL");
359 #endif
361 return ERROR_OK;
364 static void arm9tdmi_change_to_arm(target_t *target,
365 uint32_t *r0, uint32_t *pc)
367 int retval = ERROR_OK;
368 /* get pointers to arch-specific information */
369 armv4_5_common_t *armv4_5 = target->arch_info;
370 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
371 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
373 /* save r0 before using it and put system in ARM state
374 * to allow common handling of ARM and THUMB debugging */
376 /* fetch STR r0, [r0] */
377 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_STR(0, 0), 0, NULL, 0);
378 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
379 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
380 /* STR r0, [r0] in Memory */
381 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, r0, 0);
383 /* MOV r0, r15 fetched, STR in Decode */
384 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_MOV(0, 15), 0, NULL, 0);
385 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
386 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_STR(0, 0), 0, NULL, 0);
387 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
388 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
389 /* nothing fetched, STR r0, [r0] in Memory */
390 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, pc, 0);
392 /* use pc-relative LDR to clear r0[1:0] (for switch to ARM mode) */
393 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_LDR_PCREL(0), 0, NULL, 0);
394 /* LDR in Decode */
395 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
396 /* LDR in Execute */
397 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
398 /* LDR in Memory (to account for interlock) */
399 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
401 /* fetch BX */
402 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_BX(0), 0, NULL, 0);
403 /* NOP fetched, BX in Decode, MOV in Execute */
404 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
405 /* NOP fetched, BX in Execute (1) */
406 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
408 if ((retval = jtag_execute_queue()) != ERROR_OK)
410 return;
413 /* fix program counter:
414 * MOV r0, r15 was the 5th instruction (+8)
415 * reading PC in Thumb state gives address of instruction + 4
417 *pc -= 0xc;
420 void arm9tdmi_read_core_regs(target_t *target, uint32_t mask, uint32_t* core_regs[16])
422 int i;
423 /* get pointers to arch-specific information */
424 armv4_5_common_t *armv4_5 = target->arch_info;
425 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
426 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
428 /* STMIA r0-15, [r0] at debug speed
429 * register values will start to appear on 4th DCLK
431 arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
433 /* fetch NOP, STM in DECODE stage */
434 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
435 /* fetch NOP, STM in EXECUTE stage (1st cycle) */
436 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
438 for (i = 0; i <= 15; i++)
440 if (mask & (1 << i))
441 /* nothing fetched, STM in MEMORY (i'th cycle) */
442 arm9tdmi_clock_data_in(jtag_info, core_regs[i]);
446 static void arm9tdmi_read_core_regs_target_buffer(target_t *target,
447 uint32_t mask, void* buffer, int size)
449 int i;
450 /* get pointers to arch-specific information */
451 armv4_5_common_t *armv4_5 = target->arch_info;
452 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
453 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
454 int be = (target->endianness == TARGET_BIG_ENDIAN) ? 1 : 0;
455 uint32_t *buf_u32 = buffer;
456 uint16_t *buf_u16 = buffer;
457 uint8_t *buf_u8 = buffer;
459 /* STMIA r0-15, [r0] at debug speed
460 * register values will start to appear on 4th DCLK
462 arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
464 /* fetch NOP, STM in DECODE stage */
465 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
466 /* fetch NOP, STM in EXECUTE stage (1st cycle) */
467 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
469 for (i = 0; i <= 15; i++)
471 if (mask & (1 << i))
472 /* nothing fetched, STM in MEMORY (i'th cycle) */
473 switch (size)
475 case 4:
476 arm9tdmi_clock_data_in_endianness(jtag_info, buf_u32++, 4, be);
477 break;
478 case 2:
479 arm9tdmi_clock_data_in_endianness(jtag_info, buf_u16++, 2, be);
480 break;
481 case 1:
482 arm9tdmi_clock_data_in_endianness(jtag_info, buf_u8++, 1, be);
483 break;
488 static void arm9tdmi_read_xpsr(target_t *target, uint32_t *xpsr, int spsr)
490 /* get pointers to arch-specific information */
491 armv4_5_common_t *armv4_5 = target->arch_info;
492 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
493 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
495 /* MRS r0, cpsr */
496 arm9tdmi_clock_out(jtag_info, ARMV4_5_MRS(0, spsr & 1), 0, NULL, 0);
497 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
498 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
499 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
500 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
502 /* STR r0, [r15] */
503 arm9tdmi_clock_out(jtag_info, ARMV4_5_STR(0, 15), 0, NULL, 0);
504 /* fetch NOP, STR in DECODE stage */
505 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
506 /* fetch NOP, STR in EXECUTE stage (1st cycle) */
507 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
508 /* nothing fetched, STR in MEMORY */
509 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, xpsr, 0);
512 static void arm9tdmi_write_xpsr(target_t *target, uint32_t xpsr, int spsr)
514 /* get pointers to arch-specific information */
515 armv4_5_common_t *armv4_5 = target->arch_info;
516 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
517 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
519 LOG_DEBUG("xpsr: %8.8" PRIx32 ", spsr: %i", xpsr, spsr);
521 /* MSR1 fetched */
522 arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM(xpsr & 0xff, 0, 1, spsr), 0, NULL, 0);
523 /* MSR2 fetched, MSR1 in DECODE */
524 arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff00) >> 8, 0xc, 2, spsr), 0, NULL, 0);
525 /* MSR3 fetched, MSR1 in EXECUTE (1), MSR2 in DECODE */
526 arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff0000) >> 16, 0x8, 4, spsr), 0, NULL, 0);
527 /* nothing fetched, MSR1 in EXECUTE (2) */
528 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
529 /* nothing fetched, MSR1 in EXECUTE (3) */
530 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
531 /* MSR4 fetched, MSR2 in EXECUTE (1), MSR3 in DECODE */
532 arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM((xpsr & 0xff000000) >> 24, 0x4, 8, spsr), 0, NULL, 0);
533 /* nothing fetched, MSR2 in EXECUTE (2) */
534 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
535 /* nothing fetched, MSR2 in EXECUTE (3) */
536 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
537 /* NOP fetched, MSR3 in EXECUTE (1), MSR4 in DECODE */
538 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
539 /* nothing fetched, MSR3 in EXECUTE (2) */
540 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
541 /* nothing fetched, MSR3 in EXECUTE (3) */
542 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
543 /* NOP fetched, MSR4 in EXECUTE (1) */
544 /* last MSR writes flags, which takes only one cycle */
545 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
548 static void arm9tdmi_write_xpsr_im8(target_t *target,
549 uint8_t xpsr_im, int rot, int spsr)
551 /* get pointers to arch-specific information */
552 armv4_5_common_t *armv4_5 = target->arch_info;
553 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
554 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
556 LOG_DEBUG("xpsr_im: %2.2x, rot: %i, spsr: %i", xpsr_im, rot, spsr);
558 /* MSR fetched */
559 arm9tdmi_clock_out(jtag_info, ARMV4_5_MSR_IM(xpsr_im, rot, 1, spsr), 0, NULL, 0);
560 /* NOP fetched, MSR in DECODE */
561 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
562 /* NOP fetched, MSR in EXECUTE (1) */
563 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
565 /* rot == 4 writes flags, which takes only one cycle */
566 if (rot != 4)
568 /* nothing fetched, MSR in EXECUTE (2) */
569 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
570 /* nothing fetched, MSR in EXECUTE (3) */
571 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
575 void arm9tdmi_write_core_regs(target_t *target, uint32_t mask, uint32_t core_regs[16])
577 int i;
578 /* get pointers to arch-specific information */
579 armv4_5_common_t *armv4_5 = target->arch_info;
580 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
581 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
583 /* LDMIA r0-15, [r0] at debug speed
584 * register values will start to appear on 4th DCLK
586 arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, mask & 0xffff, 0, 0), 0, NULL, 0);
588 /* fetch NOP, LDM in DECODE stage */
589 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
590 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
591 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
593 for (i = 0; i <= 15; i++)
595 if (mask & (1 << i))
596 /* nothing fetched, LDM still in EXECUTE (1 + i cycle) */
597 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, core_regs[i], NULL, 0);
599 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
602 void arm9tdmi_load_word_regs(target_t *target, uint32_t mask)
604 /* get pointers to arch-specific information */
605 armv4_5_common_t *armv4_5 = target->arch_info;
606 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
607 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
609 /* put system-speed load-multiple into the pipeline */
610 arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, mask & 0xffff, 0, 1), 0, NULL, 0);
611 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
614 void arm9tdmi_load_hword_reg(target_t *target, int num)
616 /* get pointers to arch-specific information */
617 armv4_5_common_t *armv4_5 = target->arch_info;
618 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
619 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
621 /* put system-speed load half-word into the pipeline */
622 arm9tdmi_clock_out(jtag_info, ARMV4_5_LDRH_IP(num, 0), 0, NULL, 0);
623 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
626 void arm9tdmi_load_byte_reg(target_t *target, int num)
628 /* get pointers to arch-specific information */
629 armv4_5_common_t *armv4_5 = target->arch_info;
630 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
631 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
633 /* put system-speed load byte into the pipeline */
634 arm9tdmi_clock_out(jtag_info, ARMV4_5_LDRB_IP(num, 0), 0, NULL, 0);
635 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
638 void arm9tdmi_store_word_regs(target_t *target, uint32_t mask)
640 /* get pointers to arch-specific information */
641 armv4_5_common_t *armv4_5 = target->arch_info;
642 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
643 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
645 /* put system-speed store-multiple into the pipeline */
646 arm9tdmi_clock_out(jtag_info, ARMV4_5_STMIA(0, mask, 0, 1), 0, NULL, 0);
647 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
650 void arm9tdmi_store_hword_reg(target_t *target, int num)
652 /* get pointers to arch-specific information */
653 armv4_5_common_t *armv4_5 = target->arch_info;
654 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
655 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
657 /* put system-speed store half-word into the pipeline */
658 arm9tdmi_clock_out(jtag_info, ARMV4_5_STRH_IP(num, 0), 0, NULL, 0);
659 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
662 void arm9tdmi_store_byte_reg(target_t *target, int num)
664 /* get pointers to arch-specific information */
665 armv4_5_common_t *armv4_5 = target->arch_info;
666 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
667 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
669 /* put system-speed store byte into the pipeline */
670 arm9tdmi_clock_out(jtag_info, ARMV4_5_STRB_IP(num, 0), 0, NULL, 0);
671 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
674 static void arm9tdmi_write_pc(target_t *target, uint32_t pc)
676 /* get pointers to arch-specific information */
677 armv4_5_common_t *armv4_5 = target->arch_info;
678 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
679 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
681 /* LDMIA r0-15, [r0] at debug speed
682 * register values will start to appear on 4th DCLK
684 arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, 0x8000, 0, 0), 0, NULL, 0);
686 /* fetch NOP, LDM in DECODE stage */
687 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
688 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
689 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
690 /* nothing fetched, LDM in EXECUTE stage (2nd cycle) (output data) */
691 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, pc, NULL, 0);
692 /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
693 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
694 /* fetch NOP, LDM in EXECUTE stage (4th cycle) */
695 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
696 /* fetch NOP, LDM in EXECUTE stage (5th cycle) */
697 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
700 void arm9tdmi_branch_resume(target_t *target)
702 /* get pointers to arch-specific information */
703 armv4_5_common_t *armv4_5 = target->arch_info;
704 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
705 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
707 arm9tdmi_clock_out(jtag_info, ARMV4_5_B(0xfffffc, 0), 0, NULL, 0);
708 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
711 static void arm9tdmi_branch_resume_thumb(target_t *target)
713 LOG_DEBUG("-");
715 /* get pointers to arch-specific information */
716 armv4_5_common_t *armv4_5 = target->arch_info;
717 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
718 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
719 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
721 /* LDMIA r0-15, [r0] at debug speed
722 * register values will start to appear on 4th DCLK
724 arm9tdmi_clock_out(jtag_info, ARMV4_5_LDMIA(0, 0x1, 0, 0), 0, NULL, 0);
726 /* fetch NOP, LDM in DECODE stage */
727 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
728 /* fetch NOP, LDM in EXECUTE stage (1st cycle) */
729 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
730 /* nothing fetched, LDM in EXECUTE stage (2nd cycle) */
731 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32) | 1, NULL, 0);
732 /* nothing fetched, LDM in EXECUTE stage (3rd cycle) */
733 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
735 /* Branch and eXchange */
736 arm9tdmi_clock_out(jtag_info, ARMV4_5_BX(0), 0, NULL, 0);
738 embeddedice_read_reg(dbg_stat);
740 /* fetch NOP, BX in DECODE stage */
741 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
743 embeddedice_read_reg(dbg_stat);
745 /* fetch NOP, BX in EXECUTE stage (1st cycle) */
746 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 0);
748 /* target is now in Thumb state */
749 embeddedice_read_reg(dbg_stat);
751 /* load r0 value, MOV_IM in Decode*/
752 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_LDR_PCREL(0), 0, NULL, 0);
753 /* fetch NOP, LDR in Decode, MOV_IM in Execute */
754 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
755 /* fetch NOP, LDR in Execute */
756 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
757 /* nothing fetched, LDR in EXECUTE stage (2nd cycle) */
758 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, buf_get_u32(armv4_5->core_cache->reg_list[0].value, 0, 32), NULL, 0);
759 /* nothing fetched, LDR in EXECUTE stage (3rd cycle) */
760 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
762 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
763 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
765 embeddedice_read_reg(dbg_stat);
767 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_B(0x7f7), 0, NULL, 1);
768 arm9tdmi_clock_out(jtag_info, ARMV4_5_T_NOP, 0, NULL, 0);
771 void arm9tdmi_enable_single_step(target_t *target, uint32_t next_pc)
773 /* get pointers to arch-specific information */
774 armv4_5_common_t *armv4_5 = target->arch_info;
775 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
777 if (arm7_9->has_single_step)
779 buf_set_u32(arm7_9->eice_cache->reg_list[EICE_DBG_CTRL].value, 3, 1, 1);
780 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_DBG_CTRL]);
782 else
784 arm7_9_enable_eice_step(target, next_pc);
788 void arm9tdmi_disable_single_step(target_t *target)
790 /* get pointers to arch-specific information */
791 armv4_5_common_t *armv4_5 = target->arch_info;
792 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
794 if (arm7_9->has_single_step)
796 buf_set_u32(arm7_9->eice_cache->reg_list[EICE_DBG_CTRL].value, 3, 1, 0);
797 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_DBG_CTRL]);
799 else
801 arm7_9_disable_eice_step(target);
805 static void arm9tdmi_build_reg_cache(target_t *target)
807 reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
808 /* get pointers to arch-specific information */
809 armv4_5_common_t *armv4_5 = target->arch_info;
811 (*cache_p) = armv4_5_build_reg_cache(target, armv4_5);
812 armv4_5->core_cache = (*cache_p);
815 int arm9tdmi_examine(struct target_s *target)
817 /* get pointers to arch-specific information */
818 int retval;
819 armv4_5_common_t *armv4_5 = target->arch_info;
820 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
821 if (!target_was_examined(target))
823 reg_cache_t **cache_p = register_get_last_cache_p(&target->reg_cache);
824 reg_cache_t *t;
825 /* one extra register (vector catch) */
826 t = embeddedice_build_reg_cache(target, arm7_9);
827 if (t == NULL)
828 return ERROR_FAIL;
829 (*cache_p) = t;
830 arm7_9->eice_cache = (*cache_p);
832 if (arm7_9->etm_ctx)
834 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
835 (*cache_p)->next = etm_build_reg_cache(target, jtag_info, arm7_9->etm_ctx);
836 arm7_9->etm_ctx->reg_cache = (*cache_p)->next;
838 target_set_examined(target);
840 if ((retval = embeddedice_setup(target)) != ERROR_OK)
841 return retval;
842 if ((retval = arm7_9_setup(target)) != ERROR_OK)
843 return retval;
844 if (arm7_9->etm_ctx)
846 if ((retval = etm_setup(target)) != ERROR_OK)
847 return retval;
849 return ERROR_OK;
852 int arm9tdmi_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
855 arm9tdmi_build_reg_cache(target);
857 return ERROR_OK;
860 int arm9tdmi_init_arch_info(target_t *target, arm9tdmi_common_t *arm9tdmi, jtag_tap_t *tap)
862 armv4_5_common_t *armv4_5;
863 arm7_9_common_t *arm7_9;
865 arm7_9 = &arm9tdmi->arm7_9_common;
866 armv4_5 = &arm7_9->armv4_5_common;
868 /* prepare JTAG information for the new target */
869 arm7_9->jtag_info.tap = tap;
870 arm7_9->jtag_info.scann_size = 5;
872 /* register arch-specific functions */
873 arm7_9->examine_debug_reason = arm9tdmi_examine_debug_reason;
874 arm7_9->change_to_arm = arm9tdmi_change_to_arm;
875 arm7_9->read_core_regs = arm9tdmi_read_core_regs;
876 arm7_9->read_core_regs_target_buffer = arm9tdmi_read_core_regs_target_buffer;
877 arm7_9->read_xpsr = arm9tdmi_read_xpsr;
879 arm7_9->write_xpsr = arm9tdmi_write_xpsr;
880 arm7_9->write_xpsr_im8 = arm9tdmi_write_xpsr_im8;
881 arm7_9->write_core_regs = arm9tdmi_write_core_regs;
883 arm7_9->load_word_regs = arm9tdmi_load_word_regs;
884 arm7_9->load_hword_reg = arm9tdmi_load_hword_reg;
885 arm7_9->load_byte_reg = arm9tdmi_load_byte_reg;
887 arm7_9->store_word_regs = arm9tdmi_store_word_regs;
888 arm7_9->store_hword_reg = arm9tdmi_store_hword_reg;
889 arm7_9->store_byte_reg = arm9tdmi_store_byte_reg;
891 arm7_9->write_pc = arm9tdmi_write_pc;
892 arm7_9->branch_resume = arm9tdmi_branch_resume;
893 arm7_9->branch_resume_thumb = arm9tdmi_branch_resume_thumb;
895 arm7_9->enable_single_step = arm9tdmi_enable_single_step;
896 arm7_9->disable_single_step = arm9tdmi_disable_single_step;
898 arm7_9->post_debug_entry = NULL;
900 arm7_9->pre_restore_context = NULL;
901 arm7_9->post_restore_context = NULL;
903 /* initialize arch-specific breakpoint handling */
904 arm7_9->arm_bkpt = 0xdeeedeee;
905 arm7_9->thumb_bkpt = 0xdeee;
907 arm7_9->dbgreq_adjust_pc = 3;
908 arm7_9->arch_info = arm9tdmi;
910 arm9tdmi->common_magic = ARM9TDMI_COMMON_MAGIC;
911 arm9tdmi->arch_info = NULL;
913 arm7_9_init_arch_info(target, arm7_9);
915 /* override use of DBGRQ, this is safe on ARM9TDMI */
916 arm7_9->use_dbgrq = 1;
918 /* all ARM9s have the vector catch register */
919 arm7_9->has_vector_catch = 1;
921 return ERROR_OK;
924 static int arm9tdmi_get_arch_pointers(target_t *target,
925 armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p,
926 arm9tdmi_common_t **arm9tdmi_p)
928 armv4_5_common_t *armv4_5 = target->arch_info;
929 arm7_9_common_t *arm7_9;
930 arm9tdmi_common_t *arm9tdmi;
932 if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
934 return -1;
937 arm7_9 = armv4_5->arch_info;
938 if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
940 return -1;
943 arm9tdmi = arm7_9->arch_info;
944 if (arm9tdmi->common_magic != ARM9TDMI_COMMON_MAGIC)
946 return -1;
949 *armv4_5_p = armv4_5;
950 *arm7_9_p = arm7_9;
951 *arm9tdmi_p = arm9tdmi;
953 return ERROR_OK;
956 static int arm9tdmi_target_create(struct target_s *target, Jim_Interp *interp)
958 arm9tdmi_common_t *arm9tdmi = calloc(1,sizeof(arm9tdmi_common_t));
960 arm9tdmi_init_arch_info(target, arm9tdmi, target->tap);
961 arm9tdmi->arm7_9_common.armv4_5_common.is_armv4 = true;
963 return ERROR_OK;
966 static int handle_arm9tdmi_catch_vectors_command(
967 struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
969 target_t *target = get_current_target(cmd_ctx);
970 armv4_5_common_t *armv4_5;
971 arm7_9_common_t *arm7_9;
972 arm9tdmi_common_t *arm9tdmi;
973 reg_t *vector_catch;
974 uint32_t vector_catch_value;
975 int i, j;
977 if (arm9tdmi_get_arch_pointers(target, &armv4_5, &arm7_9, &arm9tdmi) != ERROR_OK)
979 command_print(cmd_ctx, "current target isn't an ARM9 based target");
980 return ERROR_OK;
983 vector_catch = &arm7_9->eice_cache->reg_list[EICE_VEC_CATCH];
985 /* read the vector catch register if necessary */
986 if (!vector_catch->valid)
987 embeddedice_read_reg(vector_catch);
989 /* get the current setting */
990 vector_catch_value = buf_get_u32(vector_catch->value, 0, 8);
992 if (argc > 0)
994 vector_catch_value = 0x0;
995 if (strcmp(args[0], "all") == 0)
997 vector_catch_value = 0xdf;
999 else if (strcmp(args[0], "none") == 0)
1001 /* do nothing */
1003 else
1005 for (i = 0; i < argc; i++)
1007 /* go through list of vectors */
1008 for (j = 0; arm9tdmi_vectors[j].name; j++)
1010 if (strcmp(args[i], arm9tdmi_vectors[j].name) == 0)
1012 vector_catch_value |= arm9tdmi_vectors[j].value;
1013 break;
1017 /* complain if vector wasn't found */
1018 if (!arm9tdmi_vectors[j].name)
1020 command_print(cmd_ctx, "vector '%s' not found, leaving current setting unchanged", args[i]);
1022 /* reread current setting */
1023 vector_catch_value = buf_get_u32(
1024 vector_catch->value,
1025 0, 8);
1027 break;
1032 /* store new settings */
1033 buf_set_u32(vector_catch->value, 0, 8, vector_catch_value);
1034 embeddedice_store_reg(vector_catch);
1037 /* output current settings */
1038 for (i = 0; arm9tdmi_vectors[i].name; i++) {
1039 command_print(cmd_ctx, "%s: %s", arm9tdmi_vectors[i].name,
1040 (vector_catch_value & arm9tdmi_vectors[i].value)
1041 ? "catch" : "don't catch");
1044 return ERROR_OK;
1047 int arm9tdmi_register_commands(struct command_context_s *cmd_ctx)
1049 int retval;
1050 command_t *arm9tdmi_cmd;
1052 retval = arm7_9_register_commands(cmd_ctx);
1053 arm9tdmi_cmd = register_command(cmd_ctx, NULL, "arm9",
1054 NULL, COMMAND_ANY,
1055 "arm9 specific commands");
1056 register_command(cmd_ctx, arm9tdmi_cmd, "vector_catch",
1057 handle_arm9tdmi_catch_vectors_command, COMMAND_EXEC,
1058 "arm9 vector_catch [all|none|reset|undef|swi|pabt|dabt|irq|fiq] ...");
1060 return retval;