atm920t : fix breakpoints and data cache handling
[openocd.git] / src / target / arm920t.c
blob7cc228d06690e34a860b73a5a47ca997dc28c116
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "arm920t.h"
25 #include <helper/time_support.h>
26 #include "target_type.h"
27 #include "register.h"
28 #include "arm_opcodes.h"
32 * For information about the ARM920T, see ARM DDI 0151C especially
33 * Chapter 9 about debug support, which shows how to manipulate each
34 * of the different scan chains:
36 * 0 ... ARM920 signals, e.g. to rest of SOC (unused here)
37 * 1 ... debugging; watchpoint and breakpoint status, etc; also
38 * MMU and cache access in conjunction with scan chain 15
39 * 2 ... EmbeddedICE
40 * 3 ... external boundary scan (SoC-specific, unused here)
41 * 4 ... access to cache tag RAM
42 * 6 ... ETM9
43 * 15 ... access coprocessor 15, "physical" or "interpreted" modes
44 * "interpreted" works with a few actual MRC/MCR instructions
45 * "physical" provides register-like behaviors. Section 9.6.7
46 * covers these details.
48 * The ARM922T is similar, but with smaller caches (8K each, vs 16K).
51 #if 0
52 #define _DEBUG_INSTRUCTION_EXECUTION_
53 #endif
55 /* Table 9-8 shows scan chain 15 format during physical access mode, using a
56 * dedicated 6-bit address space (encoded in bits 33:38). Writes use one
57 * JTAG scan, while reads use two.
59 * Table 9-9 lists the thirteen registers which support physical access.
60 * ARM920T_CP15_PHYS_ADDR() constructs the 6-bit reg_addr parameter passed
61 * to arm920t_read_cp15_physical() and arm920t_write_cp15_physical().
63 * x == bit[38]
64 * y == bits[37:34]
65 * z == bit[33]
67 #define ARM920T_CP15_PHYS_ADDR(x, y, z) ((x << 5) | (y << 1) << (z))
69 /* Registers supporting physical Read access (from table 9-9) */
70 #define CP15PHYS_CACHETYPE ARM920T_CP15_PHYS_ADDR(0, 0x0, 1)
71 #define CP15PHYS_ICACHE_IDX ARM920T_CP15_PHYS_ADDR(1, 0xd, 1)
72 #define CP15PHYS_DCACHE_IDX ARM920T_CP15_PHYS_ADDR(1, 0xe, 1)
73 /* NOTE: several more registers support only physical read access */
75 /* Registers supporting physical Read/Write access (from table 9-9) */
76 #define CP15PHYS_CTRL ARM920T_CP15_PHYS_ADDR(0, 0x1, 0)
77 #define CP15PHYS_PID ARM920T_CP15_PHYS_ADDR(0, 0xd, 0)
78 #define CP15PHYS_TESTSTATE ARM920T_CP15_PHYS_ADDR(0, 0xf, 0)
79 #define CP15PHYS_ICACHE ARM920T_CP15_PHYS_ADDR(1, 0x1, 1)
80 #define CP15PHYS_DCACHE ARM920T_CP15_PHYS_ADDR(1, 0x2, 1)
82 static int arm920t_read_cp15_physical(struct target *target,
83 int reg_addr, uint32_t *value)
85 struct arm920t_common *arm920t = target_to_arm920(target);
86 struct arm_jtag *jtag_info;
87 struct scan_field fields[4];
88 uint8_t access_type_buf = 1;
89 uint8_t reg_addr_buf = reg_addr & 0x3f;
90 uint8_t nr_w_buf = 0;
92 jtag_info = &arm920t->arm7_9_common.jtag_info;
94 jtag_set_end_state(TAP_IDLE);
95 arm_jtag_scann(jtag_info, 0xf);
96 arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
98 fields[0].tap = jtag_info->tap;
99 fields[0].num_bits = 1;
100 fields[0].out_value = &access_type_buf;
101 fields[0].in_value = NULL;
103 fields[1].tap = jtag_info->tap;
104 fields[1].num_bits = 32;
105 fields[1].out_value = NULL;
106 fields[1].in_value = NULL;
108 fields[2].tap = jtag_info->tap;
109 fields[2].num_bits = 6;
110 fields[2].out_value = &reg_addr_buf;
111 fields[2].in_value = NULL;
113 fields[3].tap = jtag_info->tap;
114 fields[3].num_bits = 1;
115 fields[3].out_value = &nr_w_buf;
116 fields[3].in_value = NULL;
118 jtag_add_dr_scan(4, fields, jtag_get_end_state());
120 fields[1].in_value = (uint8_t *)value;
122 jtag_add_dr_scan(4, fields, jtag_get_end_state());
124 jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t)value);
126 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
127 jtag_execute_queue();
128 LOG_DEBUG("addr: 0x%x value: %8.8x", reg_addr, *value);
129 #endif
131 return ERROR_OK;
134 static int arm920t_write_cp15_physical(struct target *target,
135 int reg_addr, uint32_t value)
137 struct arm920t_common *arm920t = target_to_arm920(target);
138 struct arm_jtag *jtag_info;
139 struct scan_field fields[4];
140 uint8_t access_type_buf = 1;
141 uint8_t reg_addr_buf = reg_addr & 0x3f;
142 uint8_t nr_w_buf = 1;
143 uint8_t value_buf[4];
145 jtag_info = &arm920t->arm7_9_common.jtag_info;
147 buf_set_u32(value_buf, 0, 32, value);
149 jtag_set_end_state(TAP_IDLE);
150 arm_jtag_scann(jtag_info, 0xf);
151 arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
153 fields[0].tap = jtag_info->tap;
154 fields[0].num_bits = 1;
155 fields[0].out_value = &access_type_buf;
156 fields[0].in_value = NULL;
158 fields[1].tap = jtag_info->tap;
159 fields[1].num_bits = 32;
160 fields[1].out_value = value_buf;
161 fields[1].in_value = NULL;
163 fields[2].tap = jtag_info->tap;
164 fields[2].num_bits = 6;
165 fields[2].out_value = &reg_addr_buf;
166 fields[2].in_value = NULL;
168 fields[3].tap = jtag_info->tap;
169 fields[3].num_bits = 1;
170 fields[3].out_value = &nr_w_buf;
171 fields[3].in_value = NULL;
173 jtag_add_dr_scan(4, fields, jtag_get_end_state());
175 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
176 LOG_DEBUG("addr: 0x%x value: %8.8x", reg_addr, value);
177 #endif
179 return ERROR_OK;
182 /* See table 9-10 for scan chain 15 format during interpreted access mode.
183 * If the TESTSTATE register is set for interpreted access, certain CP15
184 * MRC and MCR instructions may be executed through scan chain 15.
186 * Tables 9-11, 9-12, and 9-13 show which MRC and MCR instructions can be
187 * executed using scan chain 15 interpreted mode.
189 static int arm920t_execute_cp15(struct target *target, uint32_t cp15_opcode,
190 uint32_t arm_opcode)
192 int retval;
193 struct arm920t_common *arm920t = target_to_arm920(target);
194 struct arm_jtag *jtag_info;
195 struct scan_field fields[4];
196 uint8_t access_type_buf = 0; /* interpreted access */
197 uint8_t reg_addr_buf = 0x0;
198 uint8_t nr_w_buf = 0;
199 uint8_t cp15_opcode_buf[4];
201 jtag_info = &arm920t->arm7_9_common.jtag_info;
203 jtag_set_end_state(TAP_IDLE);
204 arm_jtag_scann(jtag_info, 0xf);
205 arm_jtag_set_instr(jtag_info, jtag_info->intest_instr, NULL);
207 buf_set_u32(cp15_opcode_buf, 0, 32, cp15_opcode);
209 fields[0].tap = jtag_info->tap;
210 fields[0].num_bits = 1;
211 fields[0].out_value = &access_type_buf;
212 fields[0].in_value = NULL;
214 fields[1].tap = jtag_info->tap;
215 fields[1].num_bits = 32;
216 fields[1].out_value = cp15_opcode_buf;
217 fields[1].in_value = NULL;
219 fields[2].tap = jtag_info->tap;
220 fields[2].num_bits = 6;
221 fields[2].out_value = &reg_addr_buf;
222 fields[2].in_value = NULL;
224 fields[3].tap = jtag_info->tap;
225 fields[3].num_bits = 1;
226 fields[3].out_value = &nr_w_buf;
227 fields[3].in_value = NULL;
229 jtag_add_dr_scan(4, fields, jtag_get_end_state());
231 arm9tdmi_clock_out(jtag_info, arm_opcode, 0, NULL, 0);
232 arm9tdmi_clock_out(jtag_info, ARMV4_5_NOP, 0, NULL, 1);
233 retval = arm7_9_execute_sys_speed(target);
234 if (retval != ERROR_OK)
235 return retval;
237 if ((retval = jtag_execute_queue()) != ERROR_OK)
239 LOG_ERROR("failed executing JTAG queue");
240 return retval;
243 return ERROR_OK;
246 static int arm920t_read_cp15_interpreted(struct target *target,
247 uint32_t cp15_opcode, uint32_t address, uint32_t *value)
249 struct arm *armv4_5 = target_to_arm(target);
250 uint32_t* regs_p[1];
251 uint32_t regs[2];
252 uint32_t cp15c15 = 0x0;
253 struct reg *r = armv4_5->core_cache->reg_list;
255 /* load address into R1 */
256 regs[1] = address;
257 arm9tdmi_write_core_regs(target, 0x2, regs);
259 /* read-modify-write CP15 test state register
260 * to enable interpreted access mode */
261 arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
262 jtag_execute_queue();
263 cp15c15 |= 1; /* set interpret mode */
264 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
266 /* execute CP15 instruction and ARM load (reading from coprocessor) */
267 arm920t_execute_cp15(target, cp15_opcode, ARMV4_5_LDR(0, 1));
269 /* disable interpreted access mode */
270 cp15c15 &= ~1U; /* clear interpret mode */
271 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
273 /* retrieve value from R0 */
274 regs_p[0] = value;
275 arm9tdmi_read_core_regs(target, 0x1, regs_p);
276 jtag_execute_queue();
278 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
279 LOG_DEBUG("cp15_opcode: %8.8x, address: %8.8x, value: %8.8x", cp15_opcode, address, *value);
280 #endif
282 if (!is_arm_mode(armv4_5->core_mode))
283 return ERROR_FAIL;
285 r[0].dirty = 1;
286 r[1].dirty = 1;
288 return ERROR_OK;
291 static
292 int arm920t_write_cp15_interpreted(struct target *target,
293 uint32_t cp15_opcode, uint32_t value, uint32_t address)
295 uint32_t cp15c15 = 0x0;
296 struct arm *armv4_5 = target_to_arm(target);
297 uint32_t regs[2];
298 struct reg *r = armv4_5->core_cache->reg_list;
300 /* load value, address into R0, R1 */
301 regs[0] = value;
302 regs[1] = address;
303 arm9tdmi_write_core_regs(target, 0x3, regs);
305 /* read-modify-write CP15 test state register
306 * to enable interpreted access mode */
307 arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
308 jtag_execute_queue();
309 cp15c15 |= 1; /* set interpret mode */
310 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
312 /* execute CP15 instruction and ARM store (writing to coprocessor) */
313 arm920t_execute_cp15(target, cp15_opcode, ARMV4_5_STR(0, 1));
315 /* disable interpreted access mode */
316 cp15c15 &= ~1U; /* set interpret mode */
317 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
319 #ifdef _DEBUG_INSTRUCTION_EXECUTION_
320 LOG_DEBUG("cp15_opcode: %8.8x, value: %8.8x, address: %8.8x", cp15_opcode, value, address);
321 #endif
323 if (!is_arm_mode(armv4_5->core_mode))
324 return ERROR_FAIL;
326 r[0].dirty = 1;
327 r[1].dirty = 1;
329 return ERROR_OK;
332 // EXPORTED to FA256
333 uint32_t arm920t_get_ttb(struct target *target)
335 int retval;
336 uint32_t ttb = 0x0;
338 if ((retval = arm920t_read_cp15_interpreted(target, 0xeebf0f51, 0x0, &ttb)) != ERROR_OK)
339 return retval;
341 return ttb;
344 // EXPORTED to FA256
345 void arm920t_disable_mmu_caches(struct target *target, int mmu, int d_u_cache, int i_cache)
347 uint32_t cp15_control;
349 /* read cp15 control register */
350 arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_control);
351 jtag_execute_queue();
353 if (mmu)
354 cp15_control &= ~0x1U;
356 if (d_u_cache)
357 cp15_control &= ~0x4U;
359 if (i_cache)
360 cp15_control &= ~0x1000U;
362 arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_control);
365 // EXPORTED to FA256
366 void arm920t_enable_mmu_caches(struct target *target, int mmu, int d_u_cache, int i_cache)
368 uint32_t cp15_control;
370 /* read cp15 control register */
371 arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_control);
372 jtag_execute_queue();
374 if (mmu)
375 cp15_control |= 0x1U;
377 if (d_u_cache)
378 cp15_control |= 0x4U;
380 if (i_cache)
381 cp15_control |= 0x1000U;
383 arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_control);
386 // EXPORTED to FA256
387 void arm920t_post_debug_entry(struct target *target)
389 uint32_t cp15c15;
390 struct arm920t_common *arm920t = target_to_arm920(target);
392 /* examine cp15 control reg */
393 arm920t_read_cp15_physical(target,
394 CP15PHYS_CTRL, &arm920t->cp15_control_reg);
395 jtag_execute_queue();
396 LOG_DEBUG("cp15_control_reg: %8.8" PRIx32 "", arm920t->cp15_control_reg);
398 if (arm920t->armv4_5_mmu.armv4_5_cache.ctype == -1)
400 uint32_t cache_type_reg;
401 /* identify caches */
402 arm920t_read_cp15_physical(target,
403 CP15PHYS_CACHETYPE, &cache_type_reg);
404 jtag_execute_queue();
405 armv4_5_identify_cache(cache_type_reg, &arm920t->armv4_5_mmu.armv4_5_cache);
408 arm920t->armv4_5_mmu.mmu_enabled = (arm920t->cp15_control_reg & 0x1U) ? 1 : 0;
409 arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = (arm920t->cp15_control_reg & 0x4U) ? 1 : 0;
410 arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled = (arm920t->cp15_control_reg & 0x1000U) ? 1 : 0;
412 /* save i/d fault status and address register */
413 arm920t_read_cp15_interpreted(target, 0xee150f10, 0x0, &arm920t->d_fsr);
414 arm920t_read_cp15_interpreted(target, 0xee150f30, 0x0, &arm920t->i_fsr);
415 arm920t_read_cp15_interpreted(target, 0xee160f10, 0x0, &arm920t->d_far);
416 arm920t_read_cp15_interpreted(target, 0xee160f30, 0x0, &arm920t->i_far);
418 LOG_DEBUG("D FSR: 0x%8.8" PRIx32 ", D FAR: 0x%8.8" PRIx32 ", I FSR: 0x%8.8" PRIx32 ", I FAR: 0x%8.8" PRIx32 "",
419 arm920t->d_fsr, arm920t->d_far, arm920t->i_fsr, arm920t->i_far);
421 if (arm920t->preserve_cache)
423 /* read-modify-write CP15 test state register
424 * to disable I/D-cache linefills */
425 arm920t_read_cp15_physical(target,
426 CP15PHYS_TESTSTATE, &cp15c15);
427 jtag_execute_queue();
428 cp15c15 |= 0x600;
429 arm920t_write_cp15_physical(target,
430 CP15PHYS_TESTSTATE, cp15c15);
434 // EXPORTED to FA256
435 void arm920t_pre_restore_context(struct target *target)
437 uint32_t cp15c15;
438 struct arm920t_common *arm920t = target_to_arm920(target);
440 /* restore i/d fault status and address register */
441 arm920t_write_cp15_interpreted(target, 0xee050f10, arm920t->d_fsr, 0x0);
442 arm920t_write_cp15_interpreted(target, 0xee050f30, arm920t->i_fsr, 0x0);
443 arm920t_write_cp15_interpreted(target, 0xee060f10, arm920t->d_far, 0x0);
444 arm920t_write_cp15_interpreted(target, 0xee060f30, arm920t->i_far, 0x0);
446 /* read-modify-write CP15 test state register
447 * to reenable I/D-cache linefills */
448 if (arm920t->preserve_cache)
450 arm920t_read_cp15_physical(target,
451 CP15PHYS_TESTSTATE, &cp15c15);
452 jtag_execute_queue();
453 cp15c15 &= ~0x600U;
454 arm920t_write_cp15_physical(target,
455 CP15PHYS_TESTSTATE, cp15c15);
459 static const char arm920_not[] = "target is not an ARM920";
461 static int arm920t_verify_pointer(struct command_context *cmd_ctx,
462 struct arm920t_common *arm920t)
464 if (arm920t->common_magic != ARM920T_COMMON_MAGIC) {
465 command_print(cmd_ctx, arm920_not);
466 return ERROR_TARGET_INVALID;
469 return ERROR_OK;
472 /** Logs summary of ARM920 state for a halted target. */
473 int arm920t_arch_state(struct target *target)
475 static const char *state[] =
477 "disabled", "enabled"
480 struct arm920t_common *arm920t = target_to_arm920(target);
481 struct arm *armv4_5;
483 if (arm920t->common_magic != ARM920T_COMMON_MAGIC)
485 LOG_ERROR("BUG: %s", arm920_not);
486 return ERROR_TARGET_INVALID;
489 armv4_5 = &arm920t->arm7_9_common.armv4_5_common;
491 arm_arch_state(target);
492 LOG_USER("MMU: %s, D-Cache: %s, I-Cache: %s",
493 state[arm920t->armv4_5_mmu.mmu_enabled],
494 state[arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled],
495 state[arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled]);
497 return ERROR_OK;
500 static int arm920_mmu(struct target *target, int *enabled)
502 if (target->state != TARGET_HALTED) {
503 LOG_ERROR("%s: target not halted", __func__);
504 return ERROR_TARGET_INVALID;
507 *enabled = target_to_arm920(target)->armv4_5_mmu.mmu_enabled;
508 return ERROR_OK;
511 static int arm920_virt2phys(struct target *target,
512 uint32_t virt, uint32_t *phys)
514 int type;
515 uint32_t cb;
516 int domain;
517 uint32_t ap;
518 struct arm920t_common *arm920t = target_to_arm920(target);
520 uint32_t ret = armv4_5_mmu_translate_va(target, &arm920t->armv4_5_mmu, virt, &type, &cb, &domain, &ap);
521 if (type == -1)
523 return ret;
525 *phys = ret;
526 return ERROR_OK;
529 /** Reads a buffer, in the specified word size, with current MMU settings. */
530 int arm920t_read_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
532 int retval;
534 retval = arm7_9_read_memory(target, address, size, count, buffer);
536 return retval;
540 static int arm920t_read_phys_memory(struct target *target,
541 uint32_t address, uint32_t size,
542 uint32_t count, uint8_t *buffer)
544 struct arm920t_common *arm920t = target_to_arm920(target);
546 return armv4_5_mmu_read_physical(target, &arm920t->armv4_5_mmu,
547 address, size, count, buffer);
550 static int arm920t_write_phys_memory(struct target *target,
551 uint32_t address, uint32_t size,
552 uint32_t count, uint8_t *buffer)
554 struct arm920t_common *arm920t = target_to_arm920(target);
556 return armv4_5_mmu_write_physical(target, &arm920t->armv4_5_mmu,
557 address, size, count, buffer);
561 /** Writes a buffer, in the specified word size, with current MMU settings. */
562 int arm920t_write_memory(struct target *target, uint32_t address,
563 uint32_t size, uint32_t count, uint8_t *buffer)
565 int retval;
566 const uint32_t cache_mask = ~0x1f; /* cache line size : 32 byte */
567 struct arm920t_common *arm920t = target_to_arm920(target);
569 /* FIX!!!! this should be cleaned up and made much more general. The
570 * plan is to write up and test on arm920t specifically and
571 * then generalize and clean up afterwards. */
572 if (arm920t->armv4_5_mmu.mmu_enabled && (count == 1) && ((size==2) || (size==4)))
574 /* special case the handling of single word writes to bypass MMU
575 * to allow implementation of breakpoints in memory marked read only
576 * by MMU */
577 int type;
578 uint32_t cb;
579 int domain;
580 uint32_t ap;
581 uint32_t pa;
584 * We need physical address and cb
586 pa = armv4_5_mmu_translate_va(target, &arm920t->armv4_5_mmu, address, &type, &cb, &domain, &ap);
587 if (type == -1)
589 return pa;
592 if (arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled)
594 if (cb & 0x1)
596 LOG_DEBUG("D-Cache buffered, drain write buffer");
598 * Buffered ?
599 * Drain write buffer - MCR p15,0,Rd,c7,c10,4
602 retval = arm920t_write_cp15_interpreted(target, ARMV4_5_MCR(15, 0, 0, 7, 10, 4), 0x0, 0);
603 if (retval != ERROR_OK)
604 return retval;
607 if (cb == 0x3)
610 * Write back memory ? -> clean cache
612 * There is no way for cleaning a data cache line using
613 * cp15 scan chain, so copy the full cache line from
614 * cache to physical memory.
616 uint8_t data[32];
618 LOG_DEBUG("D-Cache in 'write back' mode, flush cache line");
620 retval = target_read_memory(target, address & cache_mask, 1, sizeof(data), &data[0]);
621 if (retval != ERROR_OK)
622 return retval;
624 retval = armv4_5_mmu_write_physical(target, &arm920t->armv4_5_mmu, pa & cache_mask, 1, sizeof(data), &data[0]);
625 if (retval != ERROR_OK)
626 return retval;
629 /* Cached ? */
630 if (cb & 0x2)
633 * Cached ? -> Invalidate data cache using MVA
635 * MCR p15,0,Rd,c7,c6,1
637 LOG_DEBUG("D-Cache enabled, invalidate cache line");
639 retval = arm920t_write_cp15_interpreted(target, ARMV4_5_MCR(15, 0, 0, 7, 6, 1), 0x0, address & cache_mask);
640 if (retval != ERROR_OK)
641 return retval;
645 /* write directly to physical memory bypassing any read only MMU bits, etc. */
646 retval = armv4_5_mmu_write_physical(target, &arm920t->armv4_5_mmu, pa, size, count, buffer);
647 if (retval != ERROR_OK)
648 return retval;
649 } else
651 if ((retval = arm7_9_write_memory(target, address, size, count, buffer)) != ERROR_OK)
652 return retval;
655 /* If ICache is enabled, we have to invalidate affected ICache lines
656 * the DCache is forced to write-through, so we don't have to clean it here
658 if (arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled)
660 if (count <= 1)
662 /* invalidate ICache single entry with MVA
663 * ee070f35 mcr 15, 0, r0, cr7, cr5, {1}
665 LOG_DEBUG("I-Cache enabled, invalidating affected I-Cache line");
666 retval = arm920t_write_cp15_interpreted(target, ARMV4_5_MCR(15, 0, 0, 7, 5, 1), 0x0, address & cache_mask);
667 if (retval != ERROR_OK)
668 return retval;
670 else
672 /* invalidate ICache
673 * 8: ee070f15 mcr 15, 0, r0, cr7, cr5, {0}
674 * */
675 retval = arm920t_write_cp15_interpreted(target, ARMV4_5_MCR(15, 0, 0, 7, 5, 0), 0x0, 0x0);
676 if (retval != ERROR_OK)
677 return retval;
681 return retval;
684 // EXPORTED to FA256
685 int arm920t_soft_reset_halt(struct target *target)
687 int retval = ERROR_OK;
688 struct arm920t_common *arm920t = target_to_arm920(target);
689 struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
690 struct arm *armv4_5 = &arm7_9->armv4_5_common;
691 struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
693 if ((retval = target_halt(target)) != ERROR_OK)
695 return retval;
698 long long then = timeval_ms();
699 int timeout;
700 while (!(timeout = ((timeval_ms()-then) > 1000)))
702 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) == 0)
704 embeddedice_read_reg(dbg_stat);
705 if ((retval = jtag_execute_queue()) != ERROR_OK)
707 return retval;
709 } else
711 break;
713 if (debug_level >= 3)
715 /* do not eat all CPU, time out after 1 se*/
716 alive_sleep(100);
717 } else
719 keep_alive();
722 if (timeout)
724 LOG_ERROR("Failed to halt CPU after 1 sec");
725 return ERROR_TARGET_TIMEOUT;
728 target->state = TARGET_HALTED;
730 /* SVC, ARM state, IRQ and FIQ disabled */
731 uint32_t cpsr;
733 cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 32);
734 cpsr &= ~0xff;
735 cpsr |= 0xd3;
736 arm_set_cpsr(armv4_5, cpsr);
737 armv4_5->cpsr->dirty = 1;
739 /* start fetching from 0x0 */
740 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, 0x0);
741 armv4_5->core_cache->reg_list[15].dirty = 1;
742 armv4_5->core_cache->reg_list[15].valid = 1;
744 arm920t_disable_mmu_caches(target, 1, 1, 1);
745 arm920t->armv4_5_mmu.mmu_enabled = 0;
746 arm920t->armv4_5_mmu.armv4_5_cache.d_u_cache_enabled = 0;
747 arm920t->armv4_5_mmu.armv4_5_cache.i_cache_enabled = 0;
749 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
751 return retval;
754 return ERROR_OK;
757 /* FIXME remove forward decls */
758 static int arm920t_mrc(struct target *target, int cpnum,
759 uint32_t op1, uint32_t op2,
760 uint32_t CRn, uint32_t CRm,
761 uint32_t *value);
762 static int arm920t_mcr(struct target *target, int cpnum,
763 uint32_t op1, uint32_t op2,
764 uint32_t CRn, uint32_t CRm,
765 uint32_t value);
767 int arm920t_init_arch_info(struct target *target, struct arm920t_common *arm920t, struct jtag_tap *tap)
769 struct arm7_9_common *arm7_9 = &arm920t->arm7_9_common;
771 arm7_9->armv4_5_common.mrc = arm920t_mrc;
772 arm7_9->armv4_5_common.mcr = arm920t_mcr;
774 /* initialize arm7/arm9 specific info (including armv4_5) */
775 arm9tdmi_init_arch_info(target, arm7_9, tap);
777 arm920t->common_magic = ARM920T_COMMON_MAGIC;
779 arm7_9->post_debug_entry = arm920t_post_debug_entry;
780 arm7_9->pre_restore_context = arm920t_pre_restore_context;
782 arm920t->armv4_5_mmu.armv4_5_cache.ctype = -1;
783 arm920t->armv4_5_mmu.get_ttb = arm920t_get_ttb;
784 arm920t->armv4_5_mmu.read_memory = arm7_9_read_memory;
785 arm920t->armv4_5_mmu.write_memory = arm7_9_write_memory;
786 arm920t->armv4_5_mmu.disable_mmu_caches = arm920t_disable_mmu_caches;
787 arm920t->armv4_5_mmu.enable_mmu_caches = arm920t_enable_mmu_caches;
788 arm920t->armv4_5_mmu.has_tiny_pages = 1;
789 arm920t->armv4_5_mmu.mmu_enabled = 0;
791 /* disabling linefills leads to lockups, so keep them enabled for now
792 * this doesn't affect correctness, but might affect timing issues, if
793 * important data is evicted from the cache during the debug session
794 * */
795 arm920t->preserve_cache = 0;
797 /* override hw single-step capability from ARM9TDMI */
798 arm7_9->has_single_step = 1;
800 return ERROR_OK;
803 static int arm920t_target_create(struct target *target, Jim_Interp *interp)
805 struct arm920t_common *arm920t = calloc(1,sizeof(struct arm920t_common));
807 return arm920t_init_arch_info(target, arm920t, target->tap);
810 COMMAND_HANDLER(arm920t_handle_read_cache_command)
812 int retval = ERROR_OK;
813 struct target *target = get_current_target(CMD_CTX);
814 struct arm920t_common *arm920t = target_to_arm920(target);
815 struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
816 struct arm *armv4_5 = &arm7_9->armv4_5_common;
817 uint32_t cp15c15;
818 uint32_t cp15_ctrl, cp15_ctrl_saved;
819 uint32_t regs[16];
820 uint32_t *regs_p[16];
821 uint32_t C15_C_D_Ind, C15_C_I_Ind;
822 int i;
823 FILE *output;
824 struct arm920t_cache_line d_cache[8][64], i_cache[8][64];
825 int segment, index;
826 struct reg *r;
828 retval = arm920t_verify_pointer(CMD_CTX, arm920t);
829 if (retval != ERROR_OK)
830 return retval;
832 if (CMD_ARGC != 1)
834 command_print(CMD_CTX, "usage: arm920t read_cache <filename>");
835 return ERROR_OK;
838 if ((output = fopen(CMD_ARGV[0], "w")) == NULL)
840 LOG_DEBUG("error opening cache content file");
841 return ERROR_OK;
844 for (i = 0; i < 16; i++)
845 regs_p[i] = &regs[i];
847 /* disable MMU and Caches */
848 arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_ctrl);
849 if ((retval = jtag_execute_queue()) != ERROR_OK)
851 return retval;
853 cp15_ctrl_saved = cp15_ctrl;
854 cp15_ctrl &= ~(ARMV4_5_MMU_ENABLED | ARMV4_5_D_U_CACHE_ENABLED | ARMV4_5_I_CACHE_ENABLED);
855 arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl);
857 /* read CP15 test state register */
858 arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
859 jtag_execute_queue();
861 /* read DCache content */
862 fprintf(output, "DCache:\n");
864 /* go through segments 0 to nsets (8 on ARM920T, 4 on ARM922T) */
865 for (segment = 0; segment < arm920t->armv4_5_mmu.armv4_5_cache.d_u_size.nsets; segment++)
867 fprintf(output, "\nsegment: %i\n----------", segment);
869 /* Ra: r0 = SBZ(31:8):segment(7:5):SBZ(4:0) */
870 regs[0] = 0x0 | (segment << 5);
871 arm9tdmi_write_core_regs(target, 0x1, regs);
873 /* set interpret mode */
874 cp15c15 |= 0x1;
875 arm920t_write_cp15_physical(target,
876 CP15PHYS_TESTSTATE, cp15c15);
878 /* D CAM Read, loads current victim into C15.C.D.Ind */
879 arm920t_execute_cp15(target, ARMV4_5_MCR(15,2,0,15,6,2), ARMV4_5_LDR(1, 0));
881 /* read current victim */
882 arm920t_read_cp15_physical(target,
883 CP15PHYS_DCACHE_IDX, &C15_C_D_Ind);
885 /* clear interpret mode */
886 cp15c15 &= ~0x1;
887 arm920t_write_cp15_physical(target,
888 CP15PHYS_TESTSTATE, cp15c15);
890 for (index = 0; index < 64; index++)
892 /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
893 regs[0] = 0x0 | (segment << 5) | (index << 26);
894 arm9tdmi_write_core_regs(target, 0x1, regs);
896 /* set interpret mode */
897 cp15c15 |= 0x1;
898 arm920t_write_cp15_physical(target,
899 CP15PHYS_TESTSTATE, cp15c15);
901 /* Write DCache victim */
902 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,9,1,0), ARMV4_5_LDR(1, 0));
904 /* Read D RAM */
905 arm920t_execute_cp15(target, ARMV4_5_MCR(15,2,0,15,10,2), ARMV4_5_LDMIA(0, 0x1fe, 0, 0));
907 /* Read D CAM */
908 arm920t_execute_cp15(target, ARMV4_5_MCR(15,2,0,15,6,2), ARMV4_5_LDR(9, 0));
910 /* clear interpret mode */
911 cp15c15 &= ~0x1;
912 arm920t_write_cp15_physical(target,
913 CP15PHYS_TESTSTATE, cp15c15);
915 /* read D RAM and CAM content */
916 arm9tdmi_read_core_regs(target, 0x3fe, regs_p);
917 if ((retval = jtag_execute_queue()) != ERROR_OK)
919 return retval;
922 d_cache[segment][index].cam = regs[9];
924 /* mask LFSR[6] */
925 regs[9] &= 0xfffffffe;
926 fprintf(output, "\nsegment: %i, index: %i, CAM: 0x%8.8" PRIx32 ", content (%s):\n", segment, index, regs[9], (regs[9] & 0x10) ? "valid" : "invalid");
928 for (i = 1; i < 9; i++)
930 d_cache[segment][index].data[i] = regs[i];
931 fprintf(output, "%i: 0x%8.8" PRIx32 "\n", i-1, regs[i]);
936 /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
937 regs[0] = 0x0 | (segment << 5) | (C15_C_D_Ind << 26);
938 arm9tdmi_write_core_regs(target, 0x1, regs);
940 /* set interpret mode */
941 cp15c15 |= 0x1;
942 arm920t_write_cp15_physical(target,
943 CP15PHYS_TESTSTATE, cp15c15);
945 /* Write DCache victim */
946 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,9,1,0), ARMV4_5_LDR(1, 0));
948 /* clear interpret mode */
949 cp15c15 &= ~0x1;
950 arm920t_write_cp15_physical(target,
951 CP15PHYS_TESTSTATE, cp15c15);
954 /* read ICache content */
955 fprintf(output, "ICache:\n");
957 /* go through segments 0 to nsets (8 on ARM920T, 4 on ARM922T) */
958 for (segment = 0; segment < arm920t->armv4_5_mmu.armv4_5_cache.d_u_size.nsets; segment++)
960 fprintf(output, "segment: %i\n----------", segment);
962 /* Ra: r0 = SBZ(31:8):segment(7:5):SBZ(4:0) */
963 regs[0] = 0x0 | (segment << 5);
964 arm9tdmi_write_core_regs(target, 0x1, regs);
966 /* set interpret mode */
967 cp15c15 |= 0x1;
968 arm920t_write_cp15_physical(target,
969 CP15PHYS_TESTSTATE, cp15c15);
971 /* I CAM Read, loads current victim into C15.C.I.Ind */
972 arm920t_execute_cp15(target, ARMV4_5_MCR(15,2,0,15,5,2), ARMV4_5_LDR(1, 0));
974 /* read current victim */
975 arm920t_read_cp15_physical(target, CP15PHYS_ICACHE_IDX,
976 &C15_C_I_Ind);
978 /* clear interpret mode */
979 cp15c15 &= ~0x1;
980 arm920t_write_cp15_physical(target,
981 CP15PHYS_TESTSTATE, cp15c15);
983 for (index = 0; index < 64; index++)
985 /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
986 regs[0] = 0x0 | (segment << 5) | (index << 26);
987 arm9tdmi_write_core_regs(target, 0x1, regs);
989 /* set interpret mode */
990 cp15c15 |= 0x1;
991 arm920t_write_cp15_physical(target,
992 CP15PHYS_TESTSTATE, cp15c15);
994 /* Write ICache victim */
995 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,9,1,1), ARMV4_5_LDR(1, 0));
997 /* Read I RAM */
998 arm920t_execute_cp15(target, ARMV4_5_MCR(15,2,0,15,9,2), ARMV4_5_LDMIA(0, 0x1fe, 0, 0));
1000 /* Read I CAM */
1001 arm920t_execute_cp15(target, ARMV4_5_MCR(15,2,0,15,5,2), ARMV4_5_LDR(9, 0));
1003 /* clear interpret mode */
1004 cp15c15 &= ~0x1;
1005 arm920t_write_cp15_physical(target,
1006 CP15PHYS_TESTSTATE, cp15c15);
1008 /* read I RAM and CAM content */
1009 arm9tdmi_read_core_regs(target, 0x3fe, regs_p);
1010 if ((retval = jtag_execute_queue()) != ERROR_OK)
1012 return retval;
1015 i_cache[segment][index].cam = regs[9];
1017 /* mask LFSR[6] */
1018 regs[9] &= 0xfffffffe;
1019 fprintf(output, "\nsegment: %i, index: %i, CAM: 0x%8.8" PRIx32 ", content (%s):\n", segment, index, regs[9], (regs[9] & 0x10) ? "valid" : "invalid");
1021 for (i = 1; i < 9; i++)
1023 i_cache[segment][index].data[i] = regs[i];
1024 fprintf(output, "%i: 0x%8.8" PRIx32 "\n", i-1, regs[i]);
1028 /* Ra: r0 = index(31:26):SBZ(25:8):segment(7:5):SBZ(4:0) */
1029 regs[0] = 0x0 | (segment << 5) | (C15_C_D_Ind << 26);
1030 arm9tdmi_write_core_regs(target, 0x1, regs);
1032 /* set interpret mode */
1033 cp15c15 |= 0x1;
1034 arm920t_write_cp15_physical(target,
1035 CP15PHYS_TESTSTATE, cp15c15);
1037 /* Write ICache victim */
1038 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,9,1,1), ARMV4_5_LDR(1, 0));
1040 /* clear interpret mode */
1041 cp15c15 &= ~0x1;
1042 arm920t_write_cp15_physical(target,
1043 CP15PHYS_TESTSTATE, cp15c15);
1046 /* restore CP15 MMU and Cache settings */
1047 arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl_saved);
1049 command_print(CMD_CTX, "cache content successfully output to %s", CMD_ARGV[0]);
1051 fclose(output);
1053 if (!is_arm_mode(armv4_5->core_mode))
1054 return ERROR_FAIL;
1056 /* force writeback of the valid data */
1057 r = armv4_5->core_cache->reg_list;
1058 r[0].dirty = r[0].valid;
1059 r[1].dirty = r[1].valid;
1060 r[2].dirty = r[2].valid;
1061 r[3].dirty = r[3].valid;
1062 r[4].dirty = r[4].valid;
1063 r[5].dirty = r[5].valid;
1064 r[6].dirty = r[6].valid;
1065 r[7].dirty = r[7].valid;
1067 r = arm_reg_current(armv4_5, 8);
1068 r->dirty = r->valid;
1070 r = arm_reg_current(armv4_5, 9);
1071 r->dirty = r->valid;
1073 return ERROR_OK;
1076 COMMAND_HANDLER(arm920t_handle_read_mmu_command)
1078 int retval = ERROR_OK;
1079 struct target *target = get_current_target(CMD_CTX);
1080 struct arm920t_common *arm920t = target_to_arm920(target);
1081 struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1082 struct arm *armv4_5 = &arm7_9->armv4_5_common;
1083 uint32_t cp15c15;
1084 uint32_t cp15_ctrl, cp15_ctrl_saved;
1085 uint32_t regs[16];
1086 uint32_t *regs_p[16];
1087 int i;
1088 FILE *output;
1089 uint32_t Dlockdown, Ilockdown;
1090 struct arm920t_tlb_entry d_tlb[64], i_tlb[64];
1091 int victim;
1092 struct reg *r;
1094 retval = arm920t_verify_pointer(CMD_CTX, arm920t);
1095 if (retval != ERROR_OK)
1096 return retval;
1098 if (CMD_ARGC != 1)
1100 command_print(CMD_CTX, "usage: arm920t read_mmu <filename>");
1101 return ERROR_OK;
1104 if ((output = fopen(CMD_ARGV[0], "w")) == NULL)
1106 LOG_DEBUG("error opening mmu content file");
1107 return ERROR_OK;
1110 for (i = 0; i < 16; i++)
1111 regs_p[i] = &regs[i];
1113 /* disable MMU and Caches */
1114 arm920t_read_cp15_physical(target, CP15PHYS_CTRL, &cp15_ctrl);
1115 if ((retval = jtag_execute_queue()) != ERROR_OK)
1117 return retval;
1119 cp15_ctrl_saved = cp15_ctrl;
1120 cp15_ctrl &= ~(ARMV4_5_MMU_ENABLED | ARMV4_5_D_U_CACHE_ENABLED | ARMV4_5_I_CACHE_ENABLED);
1121 arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl);
1123 /* read CP15 test state register */
1124 arm920t_read_cp15_physical(target, CP15PHYS_TESTSTATE, &cp15c15);
1125 if ((retval = jtag_execute_queue()) != ERROR_OK)
1127 return retval;
1130 /* prepare reading D TLB content
1131 * */
1133 /* set interpret mode */
1134 cp15c15 |= 0x1;
1135 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
1137 /* Read D TLB lockdown */
1138 arm920t_execute_cp15(target, ARMV4_5_MRC(15,0,0,10,0,0), ARMV4_5_LDR(1, 0));
1140 /* clear interpret mode */
1141 cp15c15 &= ~0x1;
1142 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
1144 /* read D TLB lockdown stored to r1 */
1145 arm9tdmi_read_core_regs(target, 0x2, regs_p);
1146 if ((retval = jtag_execute_queue()) != ERROR_OK)
1148 return retval;
1150 Dlockdown = regs[1];
1152 for (victim = 0; victim < 64; victim += 8)
1154 /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
1155 * base remains unchanged, victim goes through entries 0 to 63 */
1156 regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
1157 arm9tdmi_write_core_regs(target, 0x2, regs);
1159 /* set interpret mode */
1160 cp15c15 |= 0x1;
1161 arm920t_write_cp15_physical(target,
1162 CP15PHYS_TESTSTATE, cp15c15);
1164 /* Write D TLB lockdown */
1165 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,10,0,0), ARMV4_5_STR(1, 0));
1167 /* Read D TLB CAM */
1168 arm920t_execute_cp15(target, ARMV4_5_MCR(15,4,0,15,6,4), ARMV4_5_LDMIA(0, 0x3fc, 0, 0));
1170 /* clear interpret mode */
1171 cp15c15 &= ~0x1;
1172 arm920t_write_cp15_physical(target,
1173 CP15PHYS_TESTSTATE, cp15c15);
1175 /* read D TLB CAM content stored to r2-r9 */
1176 arm9tdmi_read_core_regs(target, 0x3fc, regs_p);
1177 if ((retval = jtag_execute_queue()) != ERROR_OK)
1179 return retval;
1182 for (i = 0; i < 8; i++)
1183 d_tlb[victim + i].cam = regs[i + 2];
1186 for (victim = 0; victim < 64; victim++)
1188 /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
1189 * base remains unchanged, victim goes through entries 0 to 63 */
1190 regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
1191 arm9tdmi_write_core_regs(target, 0x2, regs);
1193 /* set interpret mode */
1194 cp15c15 |= 0x1;
1195 arm920t_write_cp15_physical(target,
1196 CP15PHYS_TESTSTATE, cp15c15);
1198 /* Write D TLB lockdown */
1199 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,10,0,0), ARMV4_5_STR(1, 0));
1201 /* Read D TLB RAM1 */
1202 arm920t_execute_cp15(target, ARMV4_5_MCR(15,4,0,15,10,4), ARMV4_5_LDR(2,0));
1204 /* Read D TLB RAM2 */
1205 arm920t_execute_cp15(target, ARMV4_5_MCR(15,4,0,15,2,5), ARMV4_5_LDR(3,0));
1207 /* clear interpret mode */
1208 cp15c15 &= ~0x1;
1209 arm920t_write_cp15_physical(target,
1210 CP15PHYS_TESTSTATE, cp15c15);
1212 /* read D TLB RAM content stored to r2 and r3 */
1213 arm9tdmi_read_core_regs(target, 0xc, regs_p);
1214 if ((retval = jtag_execute_queue()) != ERROR_OK)
1216 return retval;
1219 d_tlb[victim].ram1 = regs[2];
1220 d_tlb[victim].ram2 = regs[3];
1223 /* restore D TLB lockdown */
1224 regs[1] = Dlockdown;
1225 arm9tdmi_write_core_regs(target, 0x2, regs);
1227 /* Write D TLB lockdown */
1228 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,10,0,0), ARMV4_5_STR(1, 0));
1230 /* prepare reading I TLB content
1231 * */
1233 /* set interpret mode */
1234 cp15c15 |= 0x1;
1235 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
1237 /* Read I TLB lockdown */
1238 arm920t_execute_cp15(target, ARMV4_5_MRC(15,0,0,10,0,1), ARMV4_5_LDR(1, 0));
1240 /* clear interpret mode */
1241 cp15c15 &= ~0x1;
1242 arm920t_write_cp15_physical(target, CP15PHYS_TESTSTATE, cp15c15);
1244 /* read I TLB lockdown stored to r1 */
1245 arm9tdmi_read_core_regs(target, 0x2, regs_p);
1246 if ((retval = jtag_execute_queue()) != ERROR_OK)
1248 return retval;
1250 Ilockdown = regs[1];
1252 for (victim = 0; victim < 64; victim += 8)
1254 /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
1255 * base remains unchanged, victim goes through entries 0 to 63 */
1256 regs[1] = (Ilockdown & 0xfc000000) | (victim << 20);
1257 arm9tdmi_write_core_regs(target, 0x2, regs);
1259 /* set interpret mode */
1260 cp15c15 |= 0x1;
1261 arm920t_write_cp15_physical(target,
1262 CP15PHYS_TESTSTATE, cp15c15);
1264 /* Write I TLB lockdown */
1265 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,10,0,1), ARMV4_5_STR(1, 0));
1267 /* Read I TLB CAM */
1268 arm920t_execute_cp15(target, ARMV4_5_MCR(15,4,0,15,5,4), ARMV4_5_LDMIA(0, 0x3fc, 0, 0));
1270 /* clear interpret mode */
1271 cp15c15 &= ~0x1;
1272 arm920t_write_cp15_physical(target,
1273 CP15PHYS_TESTSTATE, cp15c15);
1275 /* read I TLB CAM content stored to r2-r9 */
1276 arm9tdmi_read_core_regs(target, 0x3fc, regs_p);
1277 if ((retval = jtag_execute_queue()) != ERROR_OK)
1279 return retval;
1282 for (i = 0; i < 8; i++)
1283 i_tlb[i + victim].cam = regs[i + 2];
1286 for (victim = 0; victim < 64; victim++)
1288 /* new lockdown value: base[31:26]:victim[25:20]:SBZ[19:1]:p[0]
1289 * base remains unchanged, victim goes through entries 0 to 63 */
1290 regs[1] = (Dlockdown & 0xfc000000) | (victim << 20);
1291 arm9tdmi_write_core_regs(target, 0x2, regs);
1293 /* set interpret mode */
1294 cp15c15 |= 0x1;
1295 arm920t_write_cp15_physical(target,
1296 CP15PHYS_TESTSTATE, cp15c15);
1298 /* Write I TLB lockdown */
1299 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,10,0,1), ARMV4_5_STR(1, 0));
1301 /* Read I TLB RAM1 */
1302 arm920t_execute_cp15(target, ARMV4_5_MCR(15,4,0,15,9,4), ARMV4_5_LDR(2,0));
1304 /* Read I TLB RAM2 */
1305 arm920t_execute_cp15(target, ARMV4_5_MCR(15,4,0,15,1,5), ARMV4_5_LDR(3,0));
1307 /* clear interpret mode */
1308 cp15c15 &= ~0x1;
1309 arm920t_write_cp15_physical(target,
1310 CP15PHYS_TESTSTATE, cp15c15);
1312 /* read I TLB RAM content stored to r2 and r3 */
1313 arm9tdmi_read_core_regs(target, 0xc, regs_p);
1314 if ((retval = jtag_execute_queue()) != ERROR_OK)
1316 return retval;
1319 i_tlb[victim].ram1 = regs[2];
1320 i_tlb[victim].ram2 = regs[3];
1323 /* restore I TLB lockdown */
1324 regs[1] = Ilockdown;
1325 arm9tdmi_write_core_regs(target, 0x2, regs);
1327 /* Write I TLB lockdown */
1328 arm920t_execute_cp15(target, ARMV4_5_MCR(15,0,0,10,0,1), ARMV4_5_STR(1, 0));
1330 /* restore CP15 MMU and Cache settings */
1331 arm920t_write_cp15_physical(target, CP15PHYS_CTRL, cp15_ctrl_saved);
1333 /* output data to file */
1334 fprintf(output, "D TLB content:\n");
1335 for (i = 0; i < 64; i++)
1337 fprintf(output, "%i: 0x%8.8" PRIx32 " 0x%8.8" PRIx32 " 0x%8.8" PRIx32 " %s\n", i, d_tlb[i].cam, d_tlb[i].ram1, d_tlb[i].ram2, (d_tlb[i].cam & 0x20) ? "(valid)" : "(invalid)");
1340 fprintf(output, "\n\nI TLB content:\n");
1341 for (i = 0; i < 64; i++)
1343 fprintf(output, "%i: 0x%8.8" PRIx32 " 0x%8.8" PRIx32 " 0x%8.8" PRIx32 " %s\n", i, i_tlb[i].cam, i_tlb[i].ram1, i_tlb[i].ram2, (i_tlb[i].cam & 0x20) ? "(valid)" : "(invalid)");
1346 command_print(CMD_CTX, "mmu content successfully output to %s", CMD_ARGV[0]);
1348 fclose(output);
1350 if (!is_arm_mode(armv4_5->core_mode))
1351 return ERROR_FAIL;
1353 /* force writeback of the valid data */
1354 r = armv4_5->core_cache->reg_list;
1355 r[0].dirty = r[0].valid;
1356 r[1].dirty = r[1].valid;
1357 r[2].dirty = r[2].valid;
1358 r[3].dirty = r[3].valid;
1359 r[4].dirty = r[4].valid;
1360 r[5].dirty = r[5].valid;
1361 r[6].dirty = r[6].valid;
1362 r[7].dirty = r[7].valid;
1364 r = arm_reg_current(armv4_5, 8);
1365 r->dirty = r->valid;
1367 r = arm_reg_current(armv4_5, 9);
1368 r->dirty = r->valid;
1370 return ERROR_OK;
1373 COMMAND_HANDLER(arm920t_handle_cp15_command)
1375 int retval;
1376 struct target *target = get_current_target(CMD_CTX);
1377 struct arm920t_common *arm920t = target_to_arm920(target);
1379 retval = arm920t_verify_pointer(CMD_CTX, arm920t);
1380 if (retval != ERROR_OK)
1381 return retval;
1383 if (target->state != TARGET_HALTED)
1385 command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
1386 return ERROR_OK;
1389 /* one or more argument, access a single register (write if second argument is given */
1390 if (CMD_ARGC >= 1)
1392 int address;
1393 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], address);
1395 if (CMD_ARGC == 1)
1397 uint32_t value;
1398 if ((retval = arm920t_read_cp15_physical(target, address, &value)) != ERROR_OK)
1400 command_print(CMD_CTX, "couldn't access reg %i", address);
1401 return ERROR_OK;
1403 if ((retval = jtag_execute_queue()) != ERROR_OK)
1405 return retval;
1408 command_print(CMD_CTX, "%i: %8.8" PRIx32 "", address, value);
1410 else if (CMD_ARGC == 2)
1412 uint32_t value;
1413 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
1414 if ((retval = arm920t_write_cp15_physical(target, address, value)) != ERROR_OK)
1416 command_print(CMD_CTX, "couldn't access reg %i", address);
1417 return ERROR_OK;
1419 command_print(CMD_CTX, "%i: %8.8" PRIx32 "", address, value);
1423 return ERROR_OK;
1426 COMMAND_HANDLER(arm920t_handle_cp15i_command)
1428 int retval;
1429 struct target *target = get_current_target(CMD_CTX);
1430 struct arm920t_common *arm920t = target_to_arm920(target);
1432 retval = arm920t_verify_pointer(CMD_CTX, arm920t);
1433 if (retval != ERROR_OK)
1434 return retval;
1437 if (target->state != TARGET_HALTED)
1439 command_print(CMD_CTX, "target must be stopped for \"%s\" command", CMD_NAME);
1440 return ERROR_OK;
1443 /* one or more argument, access a single register (write if second argument is given */
1444 if (CMD_ARGC >= 1)
1446 uint32_t opcode;
1447 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], opcode);
1449 if (CMD_ARGC == 1)
1451 uint32_t value;
1452 if ((retval = arm920t_read_cp15_interpreted(target, opcode, 0x0, &value)) != ERROR_OK)
1454 command_print(CMD_CTX, "couldn't execute %8.8" PRIx32 "", opcode);
1455 return ERROR_OK;
1458 command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32 "", opcode, value);
1460 else if (CMD_ARGC == 2)
1462 uint32_t value;
1463 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
1464 if ((retval = arm920t_write_cp15_interpreted(target, opcode, value, 0)) != ERROR_OK)
1466 command_print(CMD_CTX, "couldn't execute %8.8" PRIx32 "", opcode);
1467 return ERROR_OK;
1469 command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32 "", opcode, value);
1471 else if (CMD_ARGC == 3)
1473 uint32_t value;
1474 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
1475 uint32_t address;
1476 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], address);
1477 if ((retval = arm920t_write_cp15_interpreted(target, opcode, value, address)) != ERROR_OK)
1479 command_print(CMD_CTX, "couldn't execute %8.8" PRIx32 "", opcode);
1480 return ERROR_OK;
1482 command_print(CMD_CTX, "%8.8" PRIx32 ": %8.8" PRIx32 " %8.8" PRIx32 "", opcode, value, address);
1485 else
1487 command_print(CMD_CTX, "usage: arm920t cp15i <opcode> [value] [address]");
1490 return ERROR_OK;
1493 COMMAND_HANDLER(arm920t_handle_cache_info_command)
1495 int retval;
1496 struct target *target = get_current_target(CMD_CTX);
1497 struct arm920t_common *arm920t = target_to_arm920(target);
1499 retval = arm920t_verify_pointer(CMD_CTX, arm920t);
1500 if (retval != ERROR_OK)
1501 return retval;
1503 return armv4_5_handle_cache_info_command(CMD_CTX, &arm920t->armv4_5_mmu.armv4_5_cache);
1507 static int arm920t_mrc(struct target *target, int cpnum,
1508 uint32_t op1, uint32_t op2,
1509 uint32_t CRn, uint32_t CRm,
1510 uint32_t *value)
1512 if (cpnum!=15)
1514 LOG_ERROR("Only cp15 is supported");
1515 return ERROR_FAIL;
1518 /* read "to" r0 */
1519 return arm920t_read_cp15_interpreted(target,
1520 ARMV4_5_MRC(cpnum, op1, 0, CRn, CRm, op2),
1521 0, value);
1524 static int arm920t_mcr(struct target *target, int cpnum,
1525 uint32_t op1, uint32_t op2,
1526 uint32_t CRn, uint32_t CRm,
1527 uint32_t value)
1529 if (cpnum!=15)
1531 LOG_ERROR("Only cp15 is supported");
1532 return ERROR_FAIL;
1535 /* write "from" r0 */
1536 return arm920t_write_cp15_interpreted(target,
1537 ARMV4_5_MCR(cpnum, op1, 0, CRn, CRm, op2),
1538 0, value);
1541 static const struct command_registration arm920t_exec_command_handlers[] = {
1543 .name = "cp15",
1544 .handler = arm920t_handle_cp15_command,
1545 .mode = COMMAND_EXEC,
1546 .help = "display/modify cp15 register",
1547 .usage = "regnum [value]",
1550 .name = "cp15i",
1551 .handler = arm920t_handle_cp15i_command,
1552 .mode = COMMAND_EXEC,
1553 /* prefer using less error-prone "arm mcr" or "arm mrc" */
1554 .help = "display/modify cp15 register using ARM opcode"
1555 " (DEPRECATED)",
1556 .usage = "instruction [value [address]]",
1559 .name = "cache_info",
1560 .handler = arm920t_handle_cache_info_command,
1561 .mode = COMMAND_EXEC,
1562 .help = "display information about target caches",
1565 .name = "read_cache",
1566 .handler = arm920t_handle_read_cache_command,
1567 .mode = COMMAND_EXEC,
1568 .help = "dump I/D cache content to file",
1569 .usage = "filename",
1572 .name = "read_mmu",
1573 .handler = arm920t_handle_read_mmu_command,
1574 .mode = COMMAND_EXEC,
1575 .help = "dump I/D mmu content to file",
1576 .usage = "filename",
1578 COMMAND_REGISTRATION_DONE
1580 const struct command_registration arm920t_command_handlers[] = {
1582 .chain = arm9tdmi_command_handlers,
1585 .name = "arm920t",
1586 .mode = COMMAND_ANY,
1587 .help = "arm920t command group",
1588 .chain = arm920t_exec_command_handlers,
1590 COMMAND_REGISTRATION_DONE
1593 /** Holds methods for ARM920 targets. */
1594 struct target_type arm920t_target =
1596 .name = "arm920t",
1598 .poll = arm7_9_poll,
1599 .arch_state = arm920t_arch_state,
1601 .target_request_data = arm7_9_target_request_data,
1603 .halt = arm7_9_halt,
1604 .resume = arm7_9_resume,
1605 .step = arm7_9_step,
1607 .assert_reset = arm7_9_assert_reset,
1608 .deassert_reset = arm7_9_deassert_reset,
1609 .soft_reset_halt = arm920t_soft_reset_halt,
1611 .get_gdb_reg_list = arm_get_gdb_reg_list,
1613 .read_memory = arm920t_read_memory,
1614 .write_memory = arm920t_write_memory,
1615 .read_phys_memory = arm920t_read_phys_memory,
1616 .write_phys_memory = arm920t_write_phys_memory,
1617 .mmu = arm920_mmu,
1618 .virt2phys = arm920_virt2phys,
1620 .bulk_write_memory = arm7_9_bulk_write_memory,
1622 .checksum_memory = arm_checksum_memory,
1623 .blank_check_memory = arm_blank_check_memory,
1625 .run_algorithm = armv4_5_run_algorithm,
1627 .add_breakpoint = arm7_9_add_breakpoint,
1628 .remove_breakpoint = arm7_9_remove_breakpoint,
1629 .add_watchpoint = arm7_9_add_watchpoint,
1630 .remove_watchpoint = arm7_9_remove_watchpoint,
1632 .commands = arm920t_command_handlers,
1633 .target_create = arm920t_target_create,
1634 .init_target = arm9tdmi_init_target,
1635 .examine = arm7_9_examine,
1636 .check_reset = arm7_9_check_reset,