1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * Copyright (C) 2008 by Hongtao Zheng *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
33 #include "embeddedice.h"
34 #include "target_request.h"
35 #include "arm7_9_common.h"
36 #include "time_support.h"
37 #include "arm_simulator.h"
40 int arm7_9_debug_entry(target_t *target);
42 /* command handler forward declarations */
43 int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
44 int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
45 int handle_arm7_9_read_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
47 int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 int handle_arm7_9_etm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 * Clear watchpoints for an ARM7/9 target.
55 * @param arm7_9 Pointer to the common struct for an ARM7/9 target
56 * @return JTAG error status after executing queue
58 static int arm7_9_clear_watchpoints(arm7_9_common_t *arm7_9)
61 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
62 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
63 arm7_9->sw_breakpoint_count = 0;
64 arm7_9->sw_breakpoints_added = 0;
66 arm7_9->wp1_used = arm7_9->wp1_used_default;
67 arm7_9->wp_available = arm7_9->wp_available_max;
69 return jtag_execute_queue();
73 * Assign a watchpoint to one of the two available hardware comparators in an
74 * ARM7 or ARM9 target.
76 * @param arm7_9 Pointer to the common struct for an ARM7/9 target
77 * @param breakpoint Pointer to the breakpoint to be used as a watchpoint
79 static void arm7_9_assign_wp(arm7_9_common_t *arm7_9, breakpoint_t *breakpoint)
81 if (!arm7_9->wp0_used)
85 arm7_9->wp_available--;
87 else if (!arm7_9->wp1_used)
91 arm7_9->wp_available--;
95 LOG_ERROR("BUG: no hardware comparator available");
97 LOG_DEBUG("BPID: %d (0x%08" PRIx32 ") using hw wp: %d",
98 breakpoint->unique_id,
104 * Setup an ARM7/9 target's embedded ICE registers for software breakpoints.
106 * @param arm7_9 Pointer to common struct for ARM7/9 targets
107 * @return Error codes if there is a problem finding a watchpoint or the result
108 * of executing the JTAG queue
110 static int arm7_9_set_software_breakpoints(arm7_9_common_t *arm7_9)
112 if (arm7_9->sw_breakpoints_added)
116 if (arm7_9->wp_available < 1)
118 LOG_WARNING("can't enable sw breakpoints with no watchpoint unit available");
119 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
121 arm7_9->wp_available--;
123 /* pick a breakpoint unit */
124 if (!arm7_9->wp0_used)
126 arm7_9->sw_breakpoints_added = 1;
127 arm7_9->wp0_used = 3;
128 } else if (!arm7_9->wp1_used)
130 arm7_9->sw_breakpoints_added = 2;
131 arm7_9->wp1_used = 3;
135 LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
139 if (arm7_9->sw_breakpoints_added == 1)
141 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], arm7_9->arm_bkpt);
142 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0x0);
143 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffffu);
144 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
145 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
147 else if (arm7_9->sw_breakpoints_added == 2)
149 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], arm7_9->arm_bkpt);
150 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0x0);
151 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0xffffffffu);
152 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
153 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
157 LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
160 LOG_DEBUG("SW BP using hw wp: %d",
161 arm7_9->sw_breakpoints_added );
163 return jtag_execute_queue();
167 * Setup the common pieces for an ARM7/9 target after reset or on startup.
169 * @param target Pointer to an ARM7/9 target to setup
170 * @return Result of clearing the watchpoints on the target
172 int arm7_9_setup(target_t *target)
174 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
176 return arm7_9_clear_watchpoints(arm7_9);
180 * Retrieves the architecture information pointers for ARMv4/5 and ARM7/9
181 * targets. A return of ERROR_OK signifies that the target is a valid target
182 * and that the pointers have been set properly.
184 * @param target Pointer to the target device to get the pointers from
185 * @param armv4_5_p Pointer to be filled in with the common struct for ARMV4/5
187 * @param arm7_9_p Pointer to be filled in with the common struct for ARM7/9
189 * @return ERROR_OK if successful
191 int arm7_9_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p)
193 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
194 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
196 /* FIXME stop using this routine; just target_to_arm7_9() and
197 * verify the resulting pointer using a replacement routine
198 * that emits a usage message.
200 if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
201 return ERROR_TARGET_INVALID;
203 if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
204 return ERROR_TARGET_INVALID;
206 *armv4_5_p = armv4_5;
213 * Set either a hardware or software breakpoint on an ARM7/9 target. The
214 * breakpoint is set up even if it is already set. Some actions, e.g. reset,
215 * might have erased the values in Embedded ICE.
217 * @param target Pointer to the target device to set the breakpoints on
218 * @param breakpoint Pointer to the breakpoint to be set
219 * @return For hardware breakpoints, this is the result of executing the JTAG
220 * queue. For software breakpoints, this will be the status of the
221 * required memory reads and writes
223 int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
225 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
226 int retval = ERROR_OK;
228 LOG_DEBUG("BPID: %d, Address: 0x%08" PRIx32 ", Type: %d" ,
229 breakpoint->unique_id,
233 if (target->state != TARGET_HALTED)
235 LOG_WARNING("target not halted");
236 return ERROR_TARGET_NOT_HALTED;
239 if (breakpoint->type == BKPT_HARD)
241 /* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
242 uint32_t mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
244 /* reassign a hw breakpoint */
245 if (breakpoint->set == 0)
247 arm7_9_assign_wp(arm7_9, breakpoint);
250 if (breakpoint->set == 1)
252 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], breakpoint->address);
253 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
254 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffffu);
255 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
256 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
258 else if (breakpoint->set == 2)
260 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], breakpoint->address);
261 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
262 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffffu);
263 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
264 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
268 LOG_ERROR("BUG: no hardware comparator available");
272 retval = jtag_execute_queue();
274 else if (breakpoint->type == BKPT_SOFT)
276 /* did we already set this breakpoint? */
280 if (breakpoint->length == 4)
282 uint32_t verify = 0xffffffff;
283 /* keep the original instruction in target endianness */
284 if ((retval = target_read_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
288 /* write the breakpoint instruction in target endianness (arm7_9->arm_bkpt is host endian) */
289 if ((retval = target_write_u32(target, breakpoint->address, arm7_9->arm_bkpt)) != ERROR_OK)
294 if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
298 if (verify != arm7_9->arm_bkpt)
300 LOG_ERROR("Unable to set 32 bit software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
306 uint16_t verify = 0xffff;
307 /* keep the original instruction in target endianness */
308 if ((retval = target_read_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
312 /* write the breakpoint instruction in target endianness (arm7_9->thumb_bkpt is host endian) */
313 if ((retval = target_write_u16(target, breakpoint->address, arm7_9->thumb_bkpt)) != ERROR_OK)
318 if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
322 if (verify != arm7_9->thumb_bkpt)
324 LOG_ERROR("Unable to set thumb software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
329 if ((retval = arm7_9_set_software_breakpoints(arm7_9)) != ERROR_OK)
332 arm7_9->sw_breakpoint_count++;
341 * Unsets an existing breakpoint on an ARM7/9 target. If it is a hardware
342 * breakpoint, the watchpoint used will be freed and the Embedded ICE registers
343 * will be updated. Otherwise, the software breakpoint will be restored to its
344 * original instruction if it hasn't already been modified.
346 * @param target Pointer to ARM7/9 target to unset the breakpoint from
347 * @param breakpoint Pointer to breakpoint to be unset
348 * @return For hardware breakpoints, this is the result of executing the JTAG
349 * queue. For software breakpoints, this will be the status of the
350 * required memory reads and writes
352 int arm7_9_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
354 int retval = ERROR_OK;
355 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
357 LOG_DEBUG("BPID: %d, Address: 0x%08" PRIx32,
358 breakpoint->unique_id,
359 breakpoint->address );
361 if (!breakpoint->set)
363 LOG_WARNING("breakpoint not set");
367 if (breakpoint->type == BKPT_HARD)
369 LOG_DEBUG("BPID: %d Releasing hw wp: %d",
370 breakpoint->unique_id,
372 if (breakpoint->set == 1)
374 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
375 arm7_9->wp0_used = 0;
376 arm7_9->wp_available++;
378 else if (breakpoint->set == 2)
380 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
381 arm7_9->wp1_used = 0;
382 arm7_9->wp_available++;
384 retval = jtag_execute_queue();
389 /* restore original instruction (kept in target endianness) */
390 if (breakpoint->length == 4)
392 uint32_t current_instr;
393 /* check that user program as not modified breakpoint instruction */
394 if ((retval = target_read_memory(target, breakpoint->address, 4, 1, (uint8_t*)¤t_instr)) != ERROR_OK)
398 if (current_instr == arm7_9->arm_bkpt)
399 if ((retval = target_write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
406 uint16_t current_instr;
407 /* check that user program as not modified breakpoint instruction */
408 if ((retval = target_read_memory(target, breakpoint->address, 2, 1, (uint8_t*)¤t_instr)) != ERROR_OK)
412 if (current_instr == arm7_9->thumb_bkpt)
413 if ((retval = target_write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
419 if (--arm7_9->sw_breakpoint_count==0)
421 /* We have removed the last sw breakpoint, clear the hw breakpoint we used to implement it */
422 if (arm7_9->sw_breakpoints_added == 1)
424 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0);
426 else if (arm7_9->sw_breakpoints_added == 2)
428 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0);
439 * Add a breakpoint to an ARM7/9 target. This makes sure that there are no
440 * dangling breakpoints and that the desired breakpoint can be added.
442 * @param target Pointer to the target ARM7/9 device to add a breakpoint to
443 * @param breakpoint Pointer to the breakpoint to be added
444 * @return An error status if there is a problem adding the breakpoint or the
445 * result of setting the breakpoint
447 int arm7_9_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
449 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
451 if (target->state != TARGET_HALTED)
453 LOG_WARNING("target not halted");
454 return ERROR_TARGET_NOT_HALTED;
457 if (arm7_9->breakpoint_count == 0)
459 /* make sure we don't have any dangling breakpoints. This is vital upon
460 * GDB connect/disconnect
462 arm7_9_clear_watchpoints(arm7_9);
465 if ((breakpoint->type == BKPT_HARD) && (arm7_9->wp_available < 1))
467 LOG_INFO("no watchpoint unit available for hardware breakpoint");
468 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
471 if ((breakpoint->length != 2) && (breakpoint->length != 4))
473 LOG_INFO("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
474 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
477 if (breakpoint->type == BKPT_HARD)
479 arm7_9_assign_wp(arm7_9, breakpoint);
482 arm7_9->breakpoint_count++;
484 return arm7_9_set_breakpoint(target, breakpoint);
488 * Removes a breakpoint from an ARM7/9 target. This will make sure there are no
489 * dangling breakpoints and updates available watchpoints if it is a hardware
492 * @param target Pointer to the target to have a breakpoint removed
493 * @param breakpoint Pointer to the breakpoint to be removed
494 * @return Error status if there was a problem unsetting the breakpoint or the
495 * watchpoints could not be cleared
497 int arm7_9_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
499 int retval = ERROR_OK;
500 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
502 if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
507 if (breakpoint->type == BKPT_HARD)
508 arm7_9->wp_available++;
510 arm7_9->breakpoint_count--;
511 if (arm7_9->breakpoint_count == 0)
513 /* make sure we don't have any dangling breakpoints */
514 if ((retval = arm7_9_clear_watchpoints(arm7_9)) != ERROR_OK)
524 * Sets a watchpoint for an ARM7/9 target in one of the watchpoint units. It is
525 * considered a bug to call this function when there are no available watchpoint
528 * @param target Pointer to an ARM7/9 target to set a watchpoint on
529 * @param watchpoint Pointer to the watchpoint to be set
530 * @return Error status if watchpoint set fails or the result of executing the
533 int arm7_9_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
535 int retval = ERROR_OK;
536 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
540 mask = watchpoint->length - 1;
542 if (target->state != TARGET_HALTED)
544 LOG_WARNING("target not halted");
545 return ERROR_TARGET_NOT_HALTED;
548 if (watchpoint->rw == WPT_ACCESS)
553 if (!arm7_9->wp0_used)
555 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], watchpoint->address);
556 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
557 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], watchpoint->mask);
558 if (watchpoint->mask != 0xffffffffu)
559 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], watchpoint->value);
560 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
561 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
563 if ((retval = jtag_execute_queue()) != ERROR_OK)
568 arm7_9->wp0_used = 2;
570 else if (!arm7_9->wp1_used)
572 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], watchpoint->address);
573 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
574 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], watchpoint->mask);
575 if (watchpoint->mask != 0xffffffffu)
576 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], watchpoint->value);
577 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
578 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
580 if ((retval = jtag_execute_queue()) != ERROR_OK)
585 arm7_9->wp1_used = 2;
589 LOG_ERROR("BUG: no hardware comparator available");
597 * Unset an existing watchpoint and clear the used watchpoint unit.
599 * @param target Pointer to the target to have the watchpoint removed
600 * @param watchpoint Pointer to the watchpoint to be removed
601 * @return Error status while trying to unset the watchpoint or the result of
602 * executing the JTAG queue
604 int arm7_9_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
606 int retval = ERROR_OK;
607 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
609 if (target->state != TARGET_HALTED)
611 LOG_WARNING("target not halted");
612 return ERROR_TARGET_NOT_HALTED;
615 if (!watchpoint->set)
617 LOG_WARNING("breakpoint not set");
621 if (watchpoint->set == 1)
623 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
624 if ((retval = jtag_execute_queue()) != ERROR_OK)
628 arm7_9->wp0_used = 0;
630 else if (watchpoint->set == 2)
632 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
633 if ((retval = jtag_execute_queue()) != ERROR_OK)
637 arm7_9->wp1_used = 0;
645 * Add a watchpoint to an ARM7/9 target. If there are no watchpoint units
646 * available, an error response is returned.
648 * @param target Pointer to the ARM7/9 target to add a watchpoint to
649 * @param watchpoint Pointer to the watchpoint to be added
650 * @return Error status while trying to add the watchpoint
652 int arm7_9_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
654 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
656 if (target->state != TARGET_HALTED)
658 LOG_WARNING("target not halted");
659 return ERROR_TARGET_NOT_HALTED;
662 if (arm7_9->wp_available < 1)
664 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
667 if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
669 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
672 arm7_9->wp_available--;
678 * Remove a watchpoint from an ARM7/9 target. The watchpoint will be unset and
679 * the used watchpoint unit will be reopened.
681 * @param target Pointer to the target to remove a watchpoint from
682 * @param watchpoint Pointer to the watchpoint to be removed
683 * @return Result of trying to unset the watchpoint
685 int arm7_9_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
687 int retval = ERROR_OK;
688 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
692 if ((retval = arm7_9_unset_watchpoint(target, watchpoint)) != ERROR_OK)
698 arm7_9->wp_available++;
704 * Restarts the target by sending a RESTART instruction and moving the JTAG
705 * state to IDLE. This includes a timeout waiting for DBGACK and SYSCOMP to be
706 * asserted by the processor.
708 * @param target Pointer to target to issue commands to
709 * @return Error status if there is a timeout or a problem while executing the
712 int arm7_9_execute_sys_speed(struct target_s *target)
715 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
716 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
717 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
719 /* set RESTART instruction */
720 jtag_set_end_state(TAP_IDLE);
721 if (arm7_9->need_bypass_before_restart) {
722 arm7_9->need_bypass_before_restart = 0;
723 arm_jtag_set_instr(jtag_info, 0xf, NULL);
725 arm_jtag_set_instr(jtag_info, 0x4, NULL);
727 long long then = timeval_ms();
729 while (!(timeout = ((timeval_ms()-then) > 1000)))
731 /* read debug status register */
732 embeddedice_read_reg(dbg_stat);
733 if ((retval = jtag_execute_queue()) != ERROR_OK)
735 if ((buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
736 && (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_SYSCOMP, 1)))
738 if (debug_level >= 3)
748 LOG_ERROR("timeout waiting for SYSCOMP & DBGACK, last DBG_STATUS: %" PRIx32 "", buf_get_u32(dbg_stat->value, 0, dbg_stat->size));
749 return ERROR_TARGET_TIMEOUT;
756 * Restarts the target by sending a RESTART instruction and moving the JTAG
757 * state to IDLE. This validates that DBGACK and SYSCOMP are set without
758 * waiting until they are.
760 * @param target Pointer to the target to issue commands to
761 * @return Always ERROR_OK
763 int arm7_9_execute_fast_sys_speed(struct target_s *target)
766 static uint8_t check_value[4], check_mask[4];
768 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
769 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
770 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
772 /* set RESTART instruction */
773 jtag_set_end_state(TAP_IDLE);
774 if (arm7_9->need_bypass_before_restart) {
775 arm7_9->need_bypass_before_restart = 0;
776 arm_jtag_set_instr(jtag_info, 0xf, NULL);
778 arm_jtag_set_instr(jtag_info, 0x4, NULL);
782 /* check for DBGACK and SYSCOMP set (others don't care) */
784 /* NB! These are constants that must be available until after next jtag_execute() and
785 * we evaluate the values upon first execution in lieu of setting up these constants
786 * during early setup.
788 buf_set_u32(check_value, 0, 32, 0x9);
789 buf_set_u32(check_mask, 0, 32, 0x9);
793 /* read debug status register */
794 embeddedice_read_reg_w_check(dbg_stat, check_value, check_mask);
800 * Get some data from the ARM7/9 target.
802 * @param target Pointer to the ARM7/9 target to read data from
803 * @param size The number of 32bit words to be read
804 * @param buffer Pointer to the buffer that will hold the data
805 * @return The result of receiving data from the Embedded ICE unit
807 int arm7_9_target_request_data(target_t *target, uint32_t size, uint8_t *buffer)
809 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
810 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
812 int retval = ERROR_OK;
815 data = malloc(size * (sizeof(uint32_t)));
817 retval = embeddedice_receive(jtag_info, data, size);
819 /* return the 32-bit ints in the 8-bit array */
820 for (i = 0; i < size; i++)
822 h_u32_to_le(buffer + (i * 4), data[i]);
831 * Handles requests to an ARM7/9 target. If debug messaging is enabled, the
832 * target is running and the DCC control register has the W bit high, this will
833 * execute the request on the target.
835 * @param priv Void pointer expected to be a target_t pointer
836 * @return ERROR_OK unless there are issues with the JTAG queue or when reading
837 * from the Embedded ICE unit
839 int arm7_9_handle_target_request(void *priv)
841 int retval = ERROR_OK;
842 target_t *target = priv;
843 if (!target_was_examined(target))
845 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
846 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
847 reg_t *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
849 if (!target->dbg_msg_enabled)
852 if (target->state == TARGET_RUNNING)
854 /* read DCC control register */
855 embeddedice_read_reg(dcc_control);
856 if ((retval = jtag_execute_queue()) != ERROR_OK)
862 if (buf_get_u32(dcc_control->value, 1, 1) == 1)
866 if ((retval = embeddedice_receive(jtag_info, &request, 1)) != ERROR_OK)
870 if ((retval = target_request(target, request)) != ERROR_OK)
881 * Polls an ARM7/9 target for its current status. If DBGACK is set, the target
882 * is manipulated to the right halted state based on its current state. This is
886 * <tr><th > State</th><th > Action</th></tr>
887 * <tr><td > TARGET_RUNNING | TARGET_RESET</td><td > Enters debug mode. If TARGET_RESET, pc may be checked</td></tr>
888 * <tr><td > TARGET_UNKNOWN</td><td > Warning is logged</td></tr>
889 * <tr><td > TARGET_DEBUG_RUNNING</td><td > Enters debug mode</td></tr>
890 * <tr><td > TARGET_HALTED</td><td > Nothing</td></tr>
893 * If the target does not end up in the halted state, a warning is produced. If
894 * DBGACK is cleared, then the target is expected to either be running or
897 * @param target Pointer to the ARM7/9 target to poll
898 * @return ERROR_OK or an error status if a command fails
900 int arm7_9_poll(target_t *target)
903 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
904 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
906 /* read debug status register */
907 embeddedice_read_reg(dbg_stat);
908 if ((retval = jtag_execute_queue()) != ERROR_OK)
913 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
915 /* LOG_DEBUG("DBGACK set, dbg_state->value: 0x%x", buf_get_u32(dbg_stat->value, 0, 32));*/
916 if (target->state == TARGET_UNKNOWN)
918 /* Starting OpenOCD with target in debug-halt */
919 target->state = TARGET_RUNNING;
920 LOG_DEBUG("DBGACK already set during server startup.");
922 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
925 if (target->state == TARGET_RESET)
927 if (target->reset_halt)
929 enum reset_types jtag_reset_config = jtag_get_reset_config();
930 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
937 target->state = TARGET_HALTED;
939 if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
944 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
945 uint32_t t=*((uint32_t *)reg->value);
948 LOG_ERROR("PC was not 0. Does this target need srst_pulls_trst?");
952 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
957 if (target->state == TARGET_DEBUG_RUNNING)
959 target->state = TARGET_HALTED;
960 if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
963 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED)) != ERROR_OK)
968 if (target->state != TARGET_HALTED)
970 LOG_WARNING("DBGACK set, but the target did not end up in the halted state %d", target->state);
975 if (target->state != TARGET_DEBUG_RUNNING)
976 target->state = TARGET_RUNNING;
983 * Asserts the reset (SRST) on an ARM7/9 target. Some -S targets (ARM966E-S in
984 * the STR912 isn't affected, ARM926EJ-S in the LPC3180 and AT91SAM9260 is
985 * affected) completely stop the JTAG clock while the core is held in reset
986 * (SRST). It isn't possible to program the halt condition once reset is
987 * asserted, hence a hook that allows the target to set up its reset-halt
988 * condition is setup prior to asserting reset.
990 * @param target Pointer to an ARM7/9 target to assert reset on
991 * @return ERROR_FAIL if the JTAG device does not have SRST, otherwise ERROR_OK
993 int arm7_9_assert_reset(target_t *target)
995 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
997 LOG_DEBUG("target->state: %s",
998 target_state_name(target));
1000 enum reset_types jtag_reset_config = jtag_get_reset_config();
1001 if (!(jtag_reset_config & RESET_HAS_SRST))
1003 LOG_ERROR("Can't assert SRST");
1007 /* At this point trst has been asserted/deasserted once. We would
1008 * like to program EmbeddedICE while SRST is asserted, instead of
1009 * depending on SRST to leave that module alone. However, many CPUs
1010 * gate the JTAG clock while SRST is asserted; or JTAG may need
1011 * clock stability guarantees (adaptive clocking might help).
1013 * So we assume JTAG access during SRST is off the menu unless it's
1014 * been specifically enabled.
1016 bool srst_asserted = false;
1018 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1019 && (jtag_reset_config & RESET_SRST_NO_GATING))
1021 jtag_add_reset(0, 1);
1022 srst_asserted = true;
1025 if (target->reset_halt)
1028 * Some targets do not support communication while SRST is asserted. We need to
1029 * set up the reset vector catch here.
1031 * If TRST is asserted, then these settings will be reset anyway, so setting them
1034 if (arm7_9->has_vector_catch)
1036 /* program vector catch register to catch reset vector */
1037 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH], 0x1);
1039 /* extra runtest added as issues were found with certain ARM9 cores (maybe more) - AT91SAM9260 and STR9 */
1040 jtag_add_runtest(1, jtag_get_end_state());
1044 /* program watchpoint unit to match on reset vector address */
1045 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], 0x0);
1046 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0x3);
1047 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1048 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1049 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1053 /* here we should issue an SRST only, but we may have to assert TRST as well */
1054 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1056 jtag_add_reset(1, 1);
1057 } else if (!srst_asserted)
1059 jtag_add_reset(0, 1);
1062 target->state = TARGET_RESET;
1063 jtag_add_sleep(50000);
1065 armv4_5_invalidate_core_regs(target);
1067 if ((target->reset_halt) && ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0))
1069 /* debug entry was already prepared in arm7_9_assert_reset() */
1070 target->debug_reason = DBG_REASON_DBGRQ;
1077 * Deassert the reset (SRST) signal on an ARM7/9 target. If SRST pulls TRST
1078 * and the target is being reset into a halt, a warning will be triggered
1079 * because it is not possible to reset into a halted mode in this case. The
1080 * target is halted using the target's functions.
1082 * @param target Pointer to the target to have the reset deasserted
1083 * @return ERROR_OK or an error from polling or halting the target
1085 int arm7_9_deassert_reset(target_t *target)
1087 int retval = ERROR_OK;
1088 LOG_DEBUG("target->state: %s",
1089 target_state_name(target));
1091 /* deassert reset lines */
1092 jtag_add_reset(0, 0);
1094 enum reset_types jtag_reset_config = jtag_get_reset_config();
1095 if (target->reset_halt && (jtag_reset_config & RESET_SRST_PULLS_TRST) != 0)
1097 LOG_WARNING("srst pulls trst - can not reset into halted mode. Issuing halt after reset.");
1098 /* set up embedded ice registers again */
1099 if ((retval = target_examine_one(target)) != ERROR_OK)
1102 if ((retval = target_poll(target)) != ERROR_OK)
1107 if ((retval = target_halt(target)) != ERROR_OK)
1117 * Clears the halt condition for an ARM7/9 target. If it isn't coming out of
1118 * reset and if DBGRQ is used, it is progammed to be deasserted. If the reset
1119 * vector catch was used, it is restored. Otherwise, the control value is
1120 * restored and the watchpoint unit is restored if it was in use.
1122 * @param target Pointer to the ARM7/9 target to have halt cleared
1123 * @return Always ERROR_OK
1125 int arm7_9_clear_halt(target_t *target)
1127 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1128 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1130 /* we used DBGRQ only if we didn't come out of reset */
1131 if (!arm7_9->debug_entry_from_reset && arm7_9->use_dbgrq)
1133 /* program EmbeddedICE Debug Control Register to deassert DBGRQ
1135 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1136 embeddedice_store_reg(dbg_ctrl);
1140 if (arm7_9->debug_entry_from_reset && arm7_9->has_vector_catch)
1142 /* if we came out of reset, and vector catch is supported, we used
1143 * vector catch to enter debug state
1144 * restore the register in that case
1146 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH]);
1150 /* restore registers if watchpoint unit 0 was in use
1152 if (arm7_9->wp0_used)
1154 if (arm7_9->debug_entry_from_reset)
1156 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE]);
1158 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
1159 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
1160 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
1162 /* control value always has to be restored, as it was either disabled,
1163 * or enabled with possibly different bits
1165 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
1173 * Issue a software reset and halt to an ARM7/9 target. The target is halted
1174 * and then there is a wait until the processor shows the halt. This wait can
1175 * timeout and results in an error being returned. The software reset involves
1176 * clearing the halt, updating the debug control register, changing to ARM mode,
1177 * reset of the program counter, and reset of all of the registers.
1179 * @param target Pointer to the ARM7/9 target to be reset and halted by software
1180 * @return Error status if any of the commands fail, otherwise ERROR_OK
1182 int arm7_9_soft_reset_halt(struct target_s *target)
1184 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1185 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1186 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
1187 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1191 /* FIX!!! replace some of this code with tcl commands
1193 * halt # the halt command is synchronous
1194 * armv4_5 core_state arm
1198 if ((retval = target_halt(target)) != ERROR_OK)
1201 long long then = timeval_ms();
1203 while (!(timeout = ((timeval_ms()-then) > 1000)))
1205 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
1207 embeddedice_read_reg(dbg_stat);
1208 if ((retval = jtag_execute_queue()) != ERROR_OK)
1210 if (debug_level >= 3)
1220 LOG_ERROR("Failed to halt CPU after 1 sec");
1221 return ERROR_TARGET_TIMEOUT;
1223 target->state = TARGET_HALTED;
1225 /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
1226 * ensure that DBGRQ is cleared
1228 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
1229 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1230 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
1231 embeddedice_store_reg(dbg_ctrl);
1233 if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
1238 /* if the target is in Thumb state, change to ARM state */
1239 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
1241 uint32_t r0_thumb, pc_thumb;
1242 LOG_DEBUG("target entered debug from Thumb state, changing to ARM");
1243 /* Entered debug from Thumb mode */
1244 armv4_5->core_state = ARMV4_5_STATE_THUMB;
1245 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
1248 /* all register content is now invalid */
1249 if ((retval = armv4_5_invalidate_core_regs(target)) != ERROR_OK)
1254 /* SVC, ARM state, IRQ and FIQ disabled */
1255 buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8, 0xd3);
1256 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 1;
1257 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1259 /* start fetching from 0x0 */
1260 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, 0x0);
1261 armv4_5->core_cache->reg_list[15].dirty = 1;
1262 armv4_5->core_cache->reg_list[15].valid = 1;
1264 armv4_5->core_mode = ARMV4_5_MODE_SVC;
1265 armv4_5->core_state = ARMV4_5_STATE_ARM;
1267 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1270 /* reset registers */
1271 for (i = 0; i <= 14; i++)
1273 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, 0xffffffff);
1274 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 1;
1275 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
1278 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
1287 * Halt an ARM7/9 target. This is accomplished by either asserting the DBGRQ
1288 * line or by programming a watchpoint to trigger on any address. It is
1289 * considered a bug to call this function while the target is in the
1290 * TARGET_RESET state.
1292 * @param target Pointer to the ARM7/9 target to be halted
1293 * @return Always ERROR_OK
1295 int arm7_9_halt(target_t *target)
1297 if (target->state == TARGET_RESET)
1299 LOG_ERROR("BUG: arm7/9 does not support halt during reset. This is handled in arm7_9_assert_reset()");
1303 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1304 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1306 LOG_DEBUG("target->state: %s",
1307 target_state_name(target));
1309 if (target->state == TARGET_HALTED)
1311 LOG_DEBUG("target was already halted");
1315 if (target->state == TARGET_UNKNOWN)
1317 LOG_WARNING("target was in unknown state when halt was requested");
1320 if (arm7_9->use_dbgrq)
1322 /* program EmbeddedICE Debug Control Register to assert DBGRQ
1324 if (arm7_9->set_special_dbgrq) {
1325 arm7_9->set_special_dbgrq(target);
1327 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 1);
1328 embeddedice_store_reg(dbg_ctrl);
1333 /* program watchpoint unit to match on any address
1335 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1336 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1337 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1338 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1341 target->debug_reason = DBG_REASON_DBGRQ;
1347 * Handle an ARM7/9 target's entry into debug mode. The halt is cleared on the
1348 * ARM. The JTAG queue is then executed and the reason for debug entry is
1349 * examined. Once done, the target is verified to be halted and the processor
1350 * is forced into ARM mode. The core registers are saved for the current core
1351 * mode and the program counter (register 15) is updated as needed. The core
1352 * registers and CPSR and SPSR are saved for restoration later.
1354 * @param target Pointer to target that is entering debug mode
1355 * @return Error code if anything fails, otherwise ERROR_OK
1357 int arm7_9_debug_entry(target_t *target)
1360 uint32_t context[16];
1361 uint32_t* context_p[16];
1362 uint32_t r0_thumb, pc_thumb;
1365 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1366 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1367 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
1368 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1370 #ifdef _DEBUG_ARM7_9_
1374 /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
1375 * ensure that DBGRQ is cleared
1377 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
1378 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1379 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
1380 embeddedice_store_reg(dbg_ctrl);
1382 if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
1387 if ((retval = jtag_execute_queue()) != ERROR_OK)
1392 if ((retval = arm7_9->examine_debug_reason(target)) != ERROR_OK)
1396 if (target->state != TARGET_HALTED)
1398 LOG_WARNING("target not halted");
1399 return ERROR_TARGET_NOT_HALTED;
1402 /* if the target is in Thumb state, change to ARM state */
1403 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
1405 LOG_DEBUG("target entered debug from Thumb state");
1406 /* Entered debug from Thumb mode */
1407 armv4_5->core_state = ARMV4_5_STATE_THUMB;
1408 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
1409 LOG_DEBUG("r0_thumb: 0x%8.8" PRIx32 ", pc_thumb: 0x%8.8" PRIx32 "", r0_thumb, pc_thumb);
1413 LOG_DEBUG("target entered debug from ARM state");
1414 /* Entered debug from ARM mode */
1415 armv4_5->core_state = ARMV4_5_STATE_ARM;
1418 for (i = 0; i < 16; i++)
1419 context_p[i] = &context[i];
1420 /* save core registers (r0 - r15 of current core mode) */
1421 arm7_9->read_core_regs(target, 0xffff, context_p);
1423 arm7_9->read_xpsr(target, &cpsr, 0);
1425 if ((retval = jtag_execute_queue()) != ERROR_OK)
1428 /* if the core has been executing in Thumb state, set the T bit */
1429 if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1432 buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32, cpsr);
1433 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
1434 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1436 armv4_5->core_mode = cpsr & 0x1f;
1438 if (armv4_5_mode_to_number(armv4_5->core_mode) == -1)
1440 target->state = TARGET_UNKNOWN;
1441 LOG_ERROR("cpsr contains invalid mode value - communication failure");
1442 return ERROR_TARGET_FAILURE;
1445 LOG_DEBUG("target entered debug state in %s mode", armv4_5_mode_strings[armv4_5_mode_to_number(armv4_5->core_mode)]);
1447 if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1449 LOG_DEBUG("thumb state, applying fixups");
1450 context[0] = r0_thumb;
1451 context[15] = pc_thumb;
1452 } else if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1454 /* adjust value stored by STM */
1455 context[15] -= 3 * 4;
1458 if ((target->debug_reason != DBG_REASON_DBGRQ) || (!arm7_9->use_dbgrq))
1459 context[15] -= 3 * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
1461 context[15] -= arm7_9->dbgreq_adjust_pc * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
1463 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1466 for (i = 0; i <= 15; i++)
1468 LOG_DEBUG("r%i: 0x%8.8" PRIx32 "", i, context[i]);
1469 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, context[i]);
1470 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 0;
1471 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
1474 LOG_DEBUG("entered debug state at PC 0x%" PRIx32 "", context[15]);
1476 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1479 /* exceptions other than USR & SYS have a saved program status register */
1480 if ((armv4_5->core_mode != ARMV4_5_MODE_USR) && (armv4_5->core_mode != ARMV4_5_MODE_SYS))
1483 arm7_9->read_xpsr(target, &spsr, 1);
1484 if ((retval = jtag_execute_queue()) != ERROR_OK)
1488 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).value, 0, 32, spsr);
1489 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).dirty = 0;
1490 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).valid = 1;
1493 /* r0 and r15 (pc) have to be restored later */
1494 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 0).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 0).valid;
1495 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 15).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 15).valid;
1497 if ((retval = jtag_execute_queue()) != ERROR_OK)
1500 if (arm7_9->post_debug_entry)
1501 arm7_9->post_debug_entry(target);
1507 * Validate the full context for an ARM7/9 target in all processor modes. If
1508 * there are any invalid registers for the target, they will all be read. This
1511 * @param target Pointer to the ARM7/9 target to capture the full context from
1512 * @return Error if the target is not halted, has an invalid core mode, or if
1513 * the JTAG queue fails to execute
1515 int arm7_9_full_context(target_t *target)
1519 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1520 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1524 if (target->state != TARGET_HALTED)
1526 LOG_WARNING("target not halted");
1527 return ERROR_TARGET_NOT_HALTED;
1530 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1533 /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1534 * SYS shares registers with User, so we don't touch SYS
1536 for (i = 0; i < 6; i++)
1539 uint32_t* reg_p[16];
1543 /* check if there are invalid registers in the current mode
1545 for (j = 0; j <= 16; j++)
1547 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1555 /* change processor mode (and mask T bit) */
1556 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1557 tmp_cpsr |= armv4_5_number_to_mode(i);
1559 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1561 for (j = 0; j < 15; j++)
1563 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1565 reg_p[j] = (uint32_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).value;
1567 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid = 1;
1568 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).dirty = 0;
1572 /* if only the PSR is invalid, mask is all zeroes */
1574 arm7_9->read_core_regs(target, mask, reg_p);
1576 /* check if the PSR has to be read */
1577 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid == 0)
1579 arm7_9->read_xpsr(target, (uint32_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).value, 1);
1580 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid = 1;
1581 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).dirty = 0;
1586 /* restore processor mode (mask T bit) */
1587 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
1589 if ((retval = jtag_execute_queue()) != ERROR_OK)
1597 * Restore the processor context on an ARM7/9 target. The full processor
1598 * context is analyzed to see if any of the registers are dirty on this end, but
1599 * have a valid new value. If this is the case, the processor is changed to the
1600 * appropriate mode and the new register values are written out to the
1601 * processor. If there happens to be a dirty register with an invalid value, an
1602 * error will be logged.
1604 * @param target Pointer to the ARM7/9 target to have its context restored
1605 * @return Error status if the target is not halted or the core mode in the
1606 * armv4_5 struct is invalid.
1608 int arm7_9_restore_context(target_t *target)
1610 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1611 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1613 armv4_5_core_reg_t *reg_arch_info;
1614 enum armv4_5_mode current_mode = armv4_5->core_mode;
1621 if (target->state != TARGET_HALTED)
1623 LOG_WARNING("target not halted");
1624 return ERROR_TARGET_NOT_HALTED;
1627 if (arm7_9->pre_restore_context)
1628 arm7_9->pre_restore_context(target);
1630 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1633 /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1634 * SYS shares registers with User, so we don't touch SYS
1636 for (i = 0; i < 6; i++)
1638 LOG_DEBUG("examining %s mode", armv4_5_mode_strings[i]);
1641 /* check if there are dirty registers in the current mode
1643 for (j = 0; j <= 16; j++)
1645 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
1646 reg_arch_info = reg->arch_info;
1647 if (reg->dirty == 1)
1649 if (reg->valid == 1)
1652 LOG_DEBUG("examining dirty reg: %s", reg->name);
1653 if ((reg_arch_info->mode != ARMV4_5_MODE_ANY)
1654 && (reg_arch_info->mode != current_mode)
1655 && !((reg_arch_info->mode == ARMV4_5_MODE_USR) && (armv4_5->core_mode == ARMV4_5_MODE_SYS))
1656 && !((reg_arch_info->mode == ARMV4_5_MODE_SYS) && (armv4_5->core_mode == ARMV4_5_MODE_USR)))
1659 LOG_DEBUG("require mode change");
1664 LOG_ERROR("BUG: dirty register '%s', but no valid data", reg->name);
1671 uint32_t mask = 0x0;
1679 /* change processor mode (mask T bit) */
1680 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1681 tmp_cpsr |= armv4_5_number_to_mode(i);
1683 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1684 current_mode = armv4_5_number_to_mode(i);
1687 for (j = 0; j <= 14; j++)
1689 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
1690 reg_arch_info = reg->arch_info;
1693 if (reg->dirty == 1)
1695 regs[j] = buf_get_u32(reg->value, 0, 32);
1700 LOG_DEBUG("writing register %i of mode %s with value 0x%8.8" PRIx32 "", j, armv4_5_mode_strings[i], regs[j]);
1706 arm7_9->write_core_regs(target, mask, regs);
1709 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16);
1710 reg_arch_info = reg->arch_info;
1711 if ((reg->dirty) && (reg_arch_info->mode != ARMV4_5_MODE_ANY))
1713 LOG_DEBUG("writing SPSR of mode %i with value 0x%8.8" PRIx32 "", i, buf_get_u32(reg->value, 0, 32));
1714 arm7_9->write_xpsr(target, buf_get_u32(reg->value, 0, 32), 1);
1719 if ((armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 0) && (armv4_5->core_mode != current_mode))
1721 /* restore processor mode (mask T bit) */
1724 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1725 tmp_cpsr |= armv4_5_number_to_mode(i);
1727 LOG_DEBUG("writing lower 8 bit of cpsr with value 0x%2.2x", (unsigned)(tmp_cpsr));
1728 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1730 else if (armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 1)
1732 /* CPSR has been changed, full restore necessary (mask T bit) */
1733 LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32));
1734 arm7_9->write_xpsr(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32) & ~0x20, 0);
1735 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
1736 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1740 LOG_DEBUG("writing PC with value 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1741 arm7_9->write_pc(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1742 armv4_5->core_cache->reg_list[15].dirty = 0;
1744 if (arm7_9->post_restore_context)
1745 arm7_9->post_restore_context(target);
1751 * Restart the core of an ARM7/9 target. A RESTART command is sent to the
1752 * instruction register and the JTAG state is set to TAP_IDLE causing a core
1755 * @param target Pointer to the ARM7/9 target to be restarted
1756 * @return Result of executing the JTAG queue
1758 int arm7_9_restart_core(struct target_s *target)
1760 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1761 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
1763 /* set RESTART instruction */
1764 jtag_set_end_state(TAP_IDLE);
1765 if (arm7_9->need_bypass_before_restart) {
1766 arm7_9->need_bypass_before_restart = 0;
1767 arm_jtag_set_instr(jtag_info, 0xf, NULL);
1769 arm_jtag_set_instr(jtag_info, 0x4, NULL);
1771 jtag_add_runtest(1, jtag_set_end_state(TAP_IDLE));
1772 return jtag_execute_queue();
1776 * Enable the watchpoints on an ARM7/9 target. The target's watchpoints are
1777 * iterated through and are set on the target if they aren't already set.
1779 * @param target Pointer to the ARM7/9 target to enable watchpoints on
1781 void arm7_9_enable_watchpoints(struct target_s *target)
1783 watchpoint_t *watchpoint = target->watchpoints;
1787 if (watchpoint->set == 0)
1788 arm7_9_set_watchpoint(target, watchpoint);
1789 watchpoint = watchpoint->next;
1794 * Enable the breakpoints on an ARM7/9 target. The target's breakpoints are
1795 * iterated through and are set on the target.
1797 * @param target Pointer to the ARM7/9 target to enable breakpoints on
1799 void arm7_9_enable_breakpoints(struct target_s *target)
1801 breakpoint_t *breakpoint = target->breakpoints;
1803 /* set any pending breakpoints */
1806 arm7_9_set_breakpoint(target, breakpoint);
1807 breakpoint = breakpoint->next;
1811 int arm7_9_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
1813 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1814 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1815 breakpoint_t *breakpoint = target->breakpoints;
1816 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1817 int err, retval = ERROR_OK;
1821 if (target->state != TARGET_HALTED)
1823 LOG_WARNING("target not halted");
1824 return ERROR_TARGET_NOT_HALTED;
1827 if (!debug_execution)
1829 target_free_all_working_areas(target);
1832 /* current = 1: continue on current pc, otherwise continue at <address> */
1834 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
1836 uint32_t current_pc;
1837 current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
1839 /* the front-end may request us not to handle breakpoints */
1840 if (handle_breakpoints)
1842 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
1844 LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 " (id: %d)", breakpoint->address, breakpoint->unique_id );
1845 if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
1850 /* calculate PC of next instruction */
1852 if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
1854 uint32_t current_opcode;
1855 target_read_u32(target, current_pc, ¤t_opcode);
1856 LOG_ERROR("Couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
1860 LOG_DEBUG("enable single-step");
1861 arm7_9->enable_single_step(target, next_pc);
1863 target->debug_reason = DBG_REASON_SINGLESTEP;
1865 if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
1870 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1871 arm7_9->branch_resume(target);
1872 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1874 arm7_9->branch_resume_thumb(target);
1878 LOG_ERROR("unhandled core state");
1882 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1883 embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1884 err = arm7_9_execute_sys_speed(target);
1886 LOG_DEBUG("disable single-step");
1887 arm7_9->disable_single_step(target);
1889 if (err != ERROR_OK)
1891 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
1895 target->state = TARGET_UNKNOWN;
1899 arm7_9_debug_entry(target);
1900 LOG_DEBUG("new PC after step: 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1902 LOG_DEBUG("set breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
1903 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
1910 /* enable any pending breakpoints and watchpoints */
1911 arm7_9_enable_breakpoints(target);
1912 arm7_9_enable_watchpoints(target);
1914 if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
1919 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1921 arm7_9->branch_resume(target);
1923 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1925 arm7_9->branch_resume_thumb(target);
1929 LOG_ERROR("unhandled core state");
1933 /* deassert DBGACK and INTDIS */
1934 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1935 /* INTDIS only when we really resume, not during debug execution */
1936 if (!debug_execution)
1937 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 0);
1938 embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1940 if ((retval = arm7_9_restart_core(target)) != ERROR_OK)
1945 target->debug_reason = DBG_REASON_NOTHALTED;
1947 if (!debug_execution)
1949 /* registers are now invalid */
1950 armv4_5_invalidate_core_regs(target);
1951 target->state = TARGET_RUNNING;
1952 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
1959 target->state = TARGET_DEBUG_RUNNING;
1960 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED)) != ERROR_OK)
1966 LOG_DEBUG("target resumed");
1971 void arm7_9_enable_eice_step(target_t *target, uint32_t next_pc)
1973 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1974 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1975 uint32_t current_pc;
1976 current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
1978 if (next_pc != current_pc)
1980 /* setup an inverse breakpoint on the current PC
1981 * - comparator 1 matches the current address
1982 * - rangeout from comparator 1 is connected to comparator 0 rangein
1983 * - comparator 0 matches any address, as long as rangein is low */
1984 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1985 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1986 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1987 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~(EICE_W_CTRL_RANGE | EICE_W_CTRL_nOPC) & 0xff);
1988 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], current_pc);
1989 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
1990 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
1991 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
1992 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1996 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1997 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1998 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
1999 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff);
2000 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], next_pc);
2001 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
2002 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
2003 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
2004 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
2008 void arm7_9_disable_eice_step(target_t *target)
2010 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2012 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
2013 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
2014 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
2015 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
2016 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE]);
2017 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK]);
2018 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK]);
2019 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK]);
2020 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE]);
2023 int arm7_9_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
2025 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2026 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2027 breakpoint_t *breakpoint = NULL;
2030 if (target->state != TARGET_HALTED)
2032 LOG_WARNING("target not halted");
2033 return ERROR_TARGET_NOT_HALTED;
2036 /* current = 1: continue on current pc, otherwise continue at <address> */
2038 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
2040 uint32_t current_pc;
2041 current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
2043 /* the front-end may request us not to handle breakpoints */
2044 if (handle_breakpoints)
2045 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
2046 if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
2051 target->debug_reason = DBG_REASON_SINGLESTEP;
2053 /* calculate PC of next instruction */
2055 if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
2057 uint32_t current_opcode;
2058 target_read_u32(target, current_pc, ¤t_opcode);
2059 LOG_ERROR("Couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
2063 if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
2068 arm7_9->enable_single_step(target, next_pc);
2070 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
2072 arm7_9->branch_resume(target);
2074 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
2076 arm7_9->branch_resume_thumb(target);
2080 LOG_ERROR("unhandled core state");
2084 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
2089 err = arm7_9_execute_sys_speed(target);
2090 arm7_9->disable_single_step(target);
2092 /* registers are now invalid */
2093 armv4_5_invalidate_core_regs(target);
2095 if (err != ERROR_OK)
2097 target->state = TARGET_UNKNOWN;
2099 arm7_9_debug_entry(target);
2100 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
2104 LOG_DEBUG("target stepped");
2108 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
2116 int arm7_9_read_core_reg(struct target_s *target, int num, enum armv4_5_mode mode)
2118 uint32_t* reg_p[16];
2121 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2122 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2124 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2127 enum armv4_5_mode reg_mode = ((armv4_5_core_reg_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info)->mode;
2129 if ((num < 0) || (num > 16))
2130 return ERROR_INVALID_ARGUMENTS;
2132 if ((mode != ARMV4_5_MODE_ANY)
2133 && (mode != armv4_5->core_mode)
2134 && (reg_mode != ARMV4_5_MODE_ANY))
2138 /* change processor mode (mask T bit) */
2139 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
2142 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
2145 if ((num >= 0) && (num <= 15))
2147 /* read a normal core register */
2148 reg_p[num] = &value;
2150 arm7_9->read_core_regs(target, 1 << num, reg_p);
2154 /* read a program status register
2155 * if the register mode is MODE_ANY, we read the cpsr, otherwise a spsr
2157 armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
2158 int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
2160 arm7_9->read_xpsr(target, &value, spsr);
2163 if ((retval = jtag_execute_queue()) != ERROR_OK)
2168 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
2169 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
2170 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).value, 0, 32, value);
2172 if ((mode != ARMV4_5_MODE_ANY)
2173 && (mode != armv4_5->core_mode)
2174 && (reg_mode != ARMV4_5_MODE_ANY)) {
2175 /* restore processor mode (mask T bit) */
2176 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2182 int arm7_9_write_core_reg(struct target_s *target, int num, enum armv4_5_mode mode, uint32_t value)
2185 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2186 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2188 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2191 enum armv4_5_mode reg_mode = ((armv4_5_core_reg_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info)->mode;
2193 if ((num < 0) || (num > 16))
2194 return ERROR_INVALID_ARGUMENTS;
2196 if ((mode != ARMV4_5_MODE_ANY)
2197 && (mode != armv4_5->core_mode)
2198 && (reg_mode != ARMV4_5_MODE_ANY)) {
2201 /* change processor mode (mask T bit) */
2202 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
2205 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
2208 if ((num >= 0) && (num <= 15))
2210 /* write a normal core register */
2213 arm7_9->write_core_regs(target, 1 << num, reg);
2217 /* write a program status register
2218 * if the register mode is MODE_ANY, we write the cpsr, otherwise a spsr
2220 armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
2221 int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
2223 /* if we're writing the CPSR, mask the T bit */
2227 arm7_9->write_xpsr(target, value, spsr);
2230 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
2231 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
2233 if ((mode != ARMV4_5_MODE_ANY)
2234 && (mode != armv4_5->core_mode)
2235 && (reg_mode != ARMV4_5_MODE_ANY)) {
2236 /* restore processor mode (mask T bit) */
2237 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2240 return jtag_execute_queue();
2243 int arm7_9_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2245 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2246 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2248 uint32_t num_accesses = 0;
2249 int thisrun_accesses;
2255 LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
2257 if (target->state != TARGET_HALTED)
2259 LOG_WARNING("target not halted");
2260 return ERROR_TARGET_NOT_HALTED;
2263 /* sanitize arguments */
2264 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
2265 return ERROR_INVALID_ARGUMENTS;
2267 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2268 return ERROR_TARGET_UNALIGNED_ACCESS;
2270 /* load the base register with the address of the first word */
2272 arm7_9->write_core_regs(target, 0x1, reg);
2279 while (num_accesses < count)
2282 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2283 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2285 if (last_reg <= thisrun_accesses)
2286 last_reg = thisrun_accesses;
2288 arm7_9->load_word_regs(target, reg_list);
2290 /* fast memory reads are only safe when the target is running
2291 * from a sufficiently high clock (32 kHz is usually too slow)
2293 if (arm7_9->fast_memory_access)
2294 retval = arm7_9_execute_fast_sys_speed(target);
2296 retval = arm7_9_execute_sys_speed(target);
2297 if (retval != ERROR_OK)
2300 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 4);
2302 /* advance buffer, count number of accesses */
2303 buffer += thisrun_accesses * 4;
2304 num_accesses += thisrun_accesses;
2306 if ((j++%1024) == 0)
2313 while (num_accesses < count)
2316 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2317 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2319 for (i = 1; i <= thisrun_accesses; i++)
2323 arm7_9->load_hword_reg(target, i);
2324 /* fast memory reads are only safe when the target is running
2325 * from a sufficiently high clock (32 kHz is usually too slow)
2327 if (arm7_9->fast_memory_access)
2328 retval = arm7_9_execute_fast_sys_speed(target);
2330 retval = arm7_9_execute_sys_speed(target);
2331 if (retval != ERROR_OK)
2338 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 2);
2340 /* advance buffer, count number of accesses */
2341 buffer += thisrun_accesses * 2;
2342 num_accesses += thisrun_accesses;
2344 if ((j++%1024) == 0)
2351 while (num_accesses < count)
2354 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2355 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2357 for (i = 1; i <= thisrun_accesses; i++)
2361 arm7_9->load_byte_reg(target, i);
2362 /* fast memory reads are only safe when the target is running
2363 * from a sufficiently high clock (32 kHz is usually too slow)
2365 if (arm7_9->fast_memory_access)
2366 retval = arm7_9_execute_fast_sys_speed(target);
2368 retval = arm7_9_execute_sys_speed(target);
2369 if (retval != ERROR_OK)
2375 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 1);
2377 /* advance buffer, count number of accesses */
2378 buffer += thisrun_accesses * 1;
2379 num_accesses += thisrun_accesses;
2381 if ((j++%1024) == 0)
2388 LOG_ERROR("BUG: we shouldn't get here");
2393 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2396 for (i = 0; i <= last_reg; i++)
2397 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid;
2399 arm7_9->read_xpsr(target, &cpsr, 0);
2400 if ((retval = jtag_execute_queue()) != ERROR_OK)
2402 LOG_ERROR("JTAG error while reading cpsr");
2403 return ERROR_TARGET_DATA_ABORT;
2406 if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
2408 LOG_WARNING("memory read caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
2410 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2412 return ERROR_TARGET_DATA_ABORT;
2418 int arm7_9_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2420 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2421 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2422 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
2425 uint32_t num_accesses = 0;
2426 int thisrun_accesses;
2432 #ifdef _DEBUG_ARM7_9_
2433 LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address, size, count);
2436 if (target->state != TARGET_HALTED)
2438 LOG_WARNING("target not halted");
2439 return ERROR_TARGET_NOT_HALTED;
2442 /* sanitize arguments */
2443 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
2444 return ERROR_INVALID_ARGUMENTS;
2446 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2447 return ERROR_TARGET_UNALIGNED_ACCESS;
2449 /* load the base register with the address of the first word */
2451 arm7_9->write_core_regs(target, 0x1, reg);
2453 /* Clear DBGACK, to make sure memory fetches work as expected */
2454 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
2455 embeddedice_store_reg(dbg_ctrl);
2460 while (num_accesses < count)
2463 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2464 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2466 for (i = 1; i <= thisrun_accesses; i++)
2470 reg[i] = target_buffer_get_u32(target, buffer);
2474 arm7_9->write_core_regs(target, reg_list, reg);
2476 arm7_9->store_word_regs(target, reg_list);
2478 /* fast memory writes are only safe when the target is running
2479 * from a sufficiently high clock (32 kHz is usually too slow)
2481 if (arm7_9->fast_memory_access)
2482 retval = arm7_9_execute_fast_sys_speed(target);
2484 retval = arm7_9_execute_sys_speed(target);
2485 if (retval != ERROR_OK)
2490 num_accesses += thisrun_accesses;
2494 while (num_accesses < count)
2497 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2498 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2500 for (i = 1; i <= thisrun_accesses; i++)
2504 reg[i] = target_buffer_get_u16(target, buffer) & 0xffff;
2508 arm7_9->write_core_regs(target, reg_list, reg);
2510 for (i = 1; i <= thisrun_accesses; i++)
2512 arm7_9->store_hword_reg(target, i);
2514 /* fast memory writes are only safe when the target is running
2515 * from a sufficiently high clock (32 kHz is usually too slow)
2517 if (arm7_9->fast_memory_access)
2518 retval = arm7_9_execute_fast_sys_speed(target);
2520 retval = arm7_9_execute_sys_speed(target);
2521 if (retval != ERROR_OK)
2527 num_accesses += thisrun_accesses;
2531 while (num_accesses < count)
2534 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2535 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2537 for (i = 1; i <= thisrun_accesses; i++)
2541 reg[i] = *buffer++ & 0xff;
2544 arm7_9->write_core_regs(target, reg_list, reg);
2546 for (i = 1; i <= thisrun_accesses; i++)
2548 arm7_9->store_byte_reg(target, i);
2549 /* fast memory writes are only safe when the target is running
2550 * from a sufficiently high clock (32 kHz is usually too slow)
2552 if (arm7_9->fast_memory_access)
2553 retval = arm7_9_execute_fast_sys_speed(target);
2555 retval = arm7_9_execute_sys_speed(target);
2556 if (retval != ERROR_OK)
2563 num_accesses += thisrun_accesses;
2567 LOG_ERROR("BUG: we shouldn't get here");
2573 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
2574 embeddedice_store_reg(dbg_ctrl);
2576 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2579 for (i = 0; i <= last_reg; i++)
2580 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid;
2582 arm7_9->read_xpsr(target, &cpsr, 0);
2583 if ((retval = jtag_execute_queue()) != ERROR_OK)
2585 LOG_ERROR("JTAG error while reading cpsr");
2586 return ERROR_TARGET_DATA_ABORT;
2589 if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
2591 LOG_WARNING("memory write caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
2593 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2595 return ERROR_TARGET_DATA_ABORT;
2601 static int dcc_count;
2602 static uint8_t *dcc_buffer;
2604 static int arm7_9_dcc_completion(struct target_s *target, uint32_t exit_point, int timeout_ms, void *arch_info)
2606 int retval = ERROR_OK;
2607 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2609 if ((retval = target_wait_state(target, TARGET_DEBUG_RUNNING, 500)) != ERROR_OK)
2612 int little = target->endianness == TARGET_LITTLE_ENDIAN;
2613 int count = dcc_count;
2614 uint8_t *buffer = dcc_buffer;
2617 /* Handle first & last using standard embeddedice_write_reg and the middle ones w/the
2618 * core function repeated. */
2619 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2622 embeddedice_reg_t *ice_reg = arm7_9->eice_cache->reg_list[EICE_COMMS_DATA].arch_info;
2623 uint8_t reg_addr = ice_reg->addr & 0x1f;
2625 tap = ice_reg->jtag_info->tap;
2627 embeddedice_write_dcc(tap, reg_addr, buffer, little, count-2);
2628 buffer += (count-2)*4;
2630 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2634 for (i = 0; i < count; i++)
2636 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2641 if ((retval = target_halt(target))!= ERROR_OK)
2645 return target_wait_state(target, TARGET_HALTED, 500);
2648 static const uint32_t dcc_code[] =
2650 /* r0 == input, points to memory buffer
2654 /* spin until DCC control (c0) reports data arrived */
2655 0xee101e10, /* w: mrc p14, #0, r1, c0, c0 */
2656 0xe3110001, /* tst r1, #1 */
2657 0x0afffffc, /* bne w */
2659 /* read word from DCC (c1), write to memory */
2660 0xee111e10, /* mrc p14, #0, r1, c1, c0 */
2661 0xe4801004, /* str r1, [r0], #4 */
2664 0xeafffff9 /* b w */
2667 int armv4_5_run_algorithm_inner(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_params, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info, int (*run_it)(struct target_s *target, uint32_t exit_point, int timeout_ms, void *arch_info));
2669 int arm7_9_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
2672 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2675 if (!arm7_9->dcc_downloads)
2676 return target_write_memory(target, address, 4, count, buffer);
2678 /* regrab previously allocated working_area, or allocate a new one */
2679 if (!arm7_9->dcc_working_area)
2681 uint8_t dcc_code_buf[6 * 4];
2683 /* make sure we have a working area */
2684 if (target_alloc_working_area(target, 24, &arm7_9->dcc_working_area) != ERROR_OK)
2686 LOG_INFO("no working area available, falling back to memory writes");
2687 return target_write_memory(target, address, 4, count, buffer);
2690 /* copy target instructions to target endianness */
2691 for (i = 0; i < 6; i++)
2693 target_buffer_set_u32(target, dcc_code_buf + i*4, dcc_code[i]);
2696 /* write DCC code to working area */
2697 if ((retval = target_write_memory(target, arm7_9->dcc_working_area->address, 4, 6, dcc_code_buf)) != ERROR_OK)
2703 armv4_5_algorithm_t armv4_5_info;
2704 reg_param_t reg_params[1];
2706 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
2707 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
2708 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
2710 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT);
2712 buf_set_u32(reg_params[0].value, 0, 32, address);
2715 dcc_buffer = buffer;
2716 retval = armv4_5_run_algorithm_inner(target, 0, NULL, 1, reg_params,
2717 arm7_9->dcc_working_area->address, arm7_9->dcc_working_area->address + 6*4, 20*1000, &armv4_5_info, arm7_9_dcc_completion);
2719 if (retval == ERROR_OK)
2721 uint32_t endaddress = buf_get_u32(reg_params[0].value, 0, 32);
2722 if (endaddress != (address + count*4))
2724 LOG_ERROR("DCC write failed, expected end address 0x%08" PRIx32 " got 0x%0" PRIx32 "", (address + count*4), endaddress);
2725 retval = ERROR_FAIL;
2729 destroy_reg_param(®_params[0]);
2734 int arm7_9_checksum_memory(struct target_s *target, uint32_t address, uint32_t count, uint32_t* checksum)
2736 working_area_t *crc_algorithm;
2737 armv4_5_algorithm_t armv4_5_info;
2738 reg_param_t reg_params[2];
2741 static const uint32_t arm7_9_crc_code[] = {
2742 0xE1A02000, /* mov r2, r0 */
2743 0xE3E00000, /* mov r0, #0xffffffff */
2744 0xE1A03001, /* mov r3, r1 */
2745 0xE3A04000, /* mov r4, #0 */
2746 0xEA00000B, /* b ncomp */
2748 0xE7D21004, /* ldrb r1, [r2, r4] */
2749 0xE59F7030, /* ldr r7, CRC32XOR */
2750 0xE0200C01, /* eor r0, r0, r1, asl 24 */
2751 0xE3A05000, /* mov r5, #0 */
2753 0xE3500000, /* cmp r0, #0 */
2754 0xE1A06080, /* mov r6, r0, asl #1 */
2755 0xE2855001, /* add r5, r5, #1 */
2756 0xE1A00006, /* mov r0, r6 */
2757 0xB0260007, /* eorlt r0, r6, r7 */
2758 0xE3550008, /* cmp r5, #8 */
2759 0x1AFFFFF8, /* bne loop */
2760 0xE2844001, /* add r4, r4, #1 */
2762 0xE1540003, /* cmp r4, r3 */
2763 0x1AFFFFF1, /* bne nbyte */
2765 0xEAFFFFFE, /* b end */
2766 0x04C11DB7 /* CRC32XOR: .word 0x04C11DB7 */
2771 if (target_alloc_working_area(target, sizeof(arm7_9_crc_code), &crc_algorithm) != ERROR_OK)
2773 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2776 /* convert flash writing code into a buffer in target endianness */
2777 for (i = 0; i < (sizeof(arm7_9_crc_code)/sizeof(uint32_t)); i++)
2779 if ((retval = target_write_u32(target, crc_algorithm->address + i*sizeof(uint32_t), arm7_9_crc_code[i])) != ERROR_OK)
2785 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
2786 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
2787 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
2789 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT);
2790 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
2792 buf_set_u32(reg_params[0].value, 0, 32, address);
2793 buf_set_u32(reg_params[1].value, 0, 32, count);
2795 /* 20 second timeout/megabyte */
2796 int timeout = 20000 * (1 + (count / (1024*1024)));
2798 if ((retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
2799 crc_algorithm->address, crc_algorithm->address + (sizeof(arm7_9_crc_code) - 8), timeout, &armv4_5_info)) != ERROR_OK)
2801 LOG_ERROR("error executing arm7_9 crc algorithm");
2802 destroy_reg_param(®_params[0]);
2803 destroy_reg_param(®_params[1]);
2804 target_free_working_area(target, crc_algorithm);
2808 *checksum = buf_get_u32(reg_params[0].value, 0, 32);
2810 destroy_reg_param(®_params[0]);
2811 destroy_reg_param(®_params[1]);
2813 target_free_working_area(target, crc_algorithm);
2818 int arm7_9_blank_check_memory(struct target_s *target, uint32_t address, uint32_t count, uint32_t* blank)
2820 working_area_t *erase_check_algorithm;
2821 reg_param_t reg_params[3];
2822 armv4_5_algorithm_t armv4_5_info;
2826 static const uint32_t erase_check_code[] =
2829 0xe4d03001, /* ldrb r3, [r0], #1 */
2830 0xe0022003, /* and r2, r2, r3 */
2831 0xe2511001, /* subs r1, r1, #1 */
2832 0x1afffffb, /* bne loop */
2834 0xeafffffe /* b end */
2837 /* make sure we have a working area */
2838 if (target_alloc_working_area(target, sizeof(erase_check_code), &erase_check_algorithm) != ERROR_OK)
2840 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2843 /* convert flash writing code into a buffer in target endianness */
2844 for (i = 0; i < (sizeof(erase_check_code)/sizeof(uint32_t)); i++)
2845 if ((retval = target_write_u32(target, erase_check_algorithm->address + i*sizeof(uint32_t), erase_check_code[i])) != ERROR_OK)
2850 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
2851 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
2852 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
2854 init_reg_param(®_params[0], "r0", 32, PARAM_OUT);
2855 buf_set_u32(reg_params[0].value, 0, 32, address);
2857 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
2858 buf_set_u32(reg_params[1].value, 0, 32, count);
2860 init_reg_param(®_params[2], "r2", 32, PARAM_IN_OUT);
2861 buf_set_u32(reg_params[2].value, 0, 32, 0xff);
2863 if ((retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
2864 erase_check_algorithm->address, erase_check_algorithm->address + (sizeof(erase_check_code) - 4), 10000, &armv4_5_info)) != ERROR_OK)
2866 destroy_reg_param(®_params[0]);
2867 destroy_reg_param(®_params[1]);
2868 destroy_reg_param(®_params[2]);
2869 target_free_working_area(target, erase_check_algorithm);
2873 *blank = buf_get_u32(reg_params[2].value, 0, 32);
2875 destroy_reg_param(®_params[0]);
2876 destroy_reg_param(®_params[1]);
2877 destroy_reg_param(®_params[2]);
2879 target_free_working_area(target, erase_check_algorithm);
2884 int arm7_9_register_commands(struct command_context_s *cmd_ctx)
2886 command_t *arm7_9_cmd;
2888 arm7_9_cmd = register_command(cmd_ctx, NULL, "arm7_9", NULL, COMMAND_ANY, "arm7/9 specific commands");
2890 register_command(cmd_ctx, arm7_9_cmd, "write_xpsr", handle_arm7_9_write_xpsr_command, COMMAND_EXEC, "write program status register <value> <not cpsr | spsr>");
2891 register_command(cmd_ctx, arm7_9_cmd, "write_xpsr_im8", handle_arm7_9_write_xpsr_im8_command, COMMAND_EXEC, "write program status register <8bit immediate> <rotate> <not cpsr | spsr>");
2893 register_command(cmd_ctx, arm7_9_cmd, "write_core_reg", handle_arm7_9_write_core_reg_command, COMMAND_EXEC, "write core register <num> <mode> <value>");
2895 register_command(cmd_ctx, arm7_9_cmd, "dbgrq", handle_arm7_9_dbgrq_command,
2896 COMMAND_ANY, "use EmbeddedICE dbgrq instead of breakpoint for target halt requests <enable | disable>");
2897 register_command(cmd_ctx, arm7_9_cmd, "fast_memory_access", handle_arm7_9_fast_memory_access_command,
2898 COMMAND_ANY, "use fast memory accesses instead of slower but potentially safer accesses <enable | disable>");
2899 register_command(cmd_ctx, arm7_9_cmd, "dcc_downloads", handle_arm7_9_dcc_downloads_command,
2900 COMMAND_ANY, "use DCC downloads for larger memory writes <enable | disable>");
2902 armv4_5_register_commands(cmd_ctx);
2904 etm_register_commands(cmd_ctx);
2909 int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2914 target_t *target = get_current_target(cmd_ctx);
2915 armv4_5_common_t *armv4_5;
2916 arm7_9_common_t *arm7_9;
2918 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2920 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2924 if (target->state != TARGET_HALTED)
2926 command_print(cmd_ctx, "can't write registers while running");
2932 command_print(cmd_ctx, "usage: write_xpsr <value> <not cpsr | spsr>");
2936 COMMAND_PARSE_NUMBER(u32, args[0], value);
2937 COMMAND_PARSE_NUMBER(int, args[1], spsr);
2939 /* if we're writing the CPSR, mask the T bit */
2943 arm7_9->write_xpsr(target, value, spsr);
2944 if ((retval = jtag_execute_queue()) != ERROR_OK)
2946 LOG_ERROR("JTAG error while writing to xpsr");
2953 int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2959 target_t *target = get_current_target(cmd_ctx);
2960 armv4_5_common_t *armv4_5;
2961 arm7_9_common_t *arm7_9;
2963 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2965 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2969 if (target->state != TARGET_HALTED)
2971 command_print(cmd_ctx, "can't write registers while running");
2977 command_print(cmd_ctx, "usage: write_xpsr_im8 <im8> <rotate> <not cpsr | spsr>");
2981 COMMAND_PARSE_NUMBER(u32, args[0], value);
2982 COMMAND_PARSE_NUMBER(int, args[1], rotate);
2983 COMMAND_PARSE_NUMBER(int, args[2], spsr);
2985 arm7_9->write_xpsr_im8(target, value, rotate, spsr);
2986 if ((retval = jtag_execute_queue()) != ERROR_OK)
2988 LOG_ERROR("JTAG error while writing 8-bit immediate to xpsr");
2995 int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3000 target_t *target = get_current_target(cmd_ctx);
3001 armv4_5_common_t *armv4_5;
3002 arm7_9_common_t *arm7_9;
3004 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
3006 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
3010 if (target->state != TARGET_HALTED)
3012 command_print(cmd_ctx, "can't write registers while running");
3018 command_print(cmd_ctx, "usage: write_core_reg <num> <mode> <value>");
3022 COMMAND_PARSE_NUMBER(int, args[0], num);
3023 COMMAND_PARSE_NUMBER(u32, args[1], mode);
3024 COMMAND_PARSE_NUMBER(u32, args[2], value);
3026 return arm7_9_write_core_reg(target, num, mode, value);
3029 int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3031 target_t *target = get_current_target(cmd_ctx);
3032 armv4_5_common_t *armv4_5;
3033 arm7_9_common_t *arm7_9;
3035 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
3037 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
3043 if (strcmp("enable", args[0]) == 0)
3045 arm7_9->use_dbgrq = 1;
3047 else if (strcmp("disable", args[0]) == 0)
3049 arm7_9->use_dbgrq = 0;
3053 command_print(cmd_ctx, "usage: arm7_9 dbgrq <enable | disable>");
3057 command_print(cmd_ctx, "use of EmbeddedICE dbgrq instead of breakpoint for target halt %s", (arm7_9->use_dbgrq) ? "enabled" : "disabled");
3062 int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3064 target_t *target = get_current_target(cmd_ctx);
3065 armv4_5_common_t *armv4_5;
3066 arm7_9_common_t *arm7_9;
3068 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
3070 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
3076 if (strcmp("enable", args[0]) == 0)
3078 arm7_9->fast_memory_access = 1;
3080 else if (strcmp("disable", args[0]) == 0)
3082 arm7_9->fast_memory_access = 0;
3086 command_print(cmd_ctx, "usage: arm7_9 fast_memory_access <enable | disable>");
3090 command_print(cmd_ctx, "fast memory access is %s", (arm7_9->fast_memory_access) ? "enabled" : "disabled");
3095 int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3097 target_t *target = get_current_target(cmd_ctx);
3098 armv4_5_common_t *armv4_5;
3099 arm7_9_common_t *arm7_9;
3101 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
3103 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
3109 if (strcmp("enable", args[0]) == 0)
3111 arm7_9->dcc_downloads = 1;
3113 else if (strcmp("disable", args[0]) == 0)
3115 arm7_9->dcc_downloads = 0;
3119 command_print(cmd_ctx, "usage: arm7_9 dcc_downloads <enable | disable>");
3123 command_print(cmd_ctx, "dcc downloads are %s", (arm7_9->dcc_downloads) ? "enabled" : "disabled");
3128 int arm7_9_init_arch_info(target_t *target, arm7_9_common_t *arm7_9)
3130 int retval = ERROR_OK;
3131 armv4_5_common_t *armv4_5 = &arm7_9->armv4_5_common;
3133 arm7_9->common_magic = ARM7_9_COMMON_MAGIC;
3135 if ((retval = arm_jtag_setup_connection(&arm7_9->jtag_info)) != ERROR_OK)
3140 arm7_9->wp_available = 0; /* this is set up in arm7_9_clear_watchpoints() */
3141 arm7_9->wp_available_max = 2;
3142 arm7_9->sw_breakpoints_added = 0;
3143 arm7_9->sw_breakpoint_count = 0;
3144 arm7_9->breakpoint_count = 0;
3145 arm7_9->wp0_used = 0;
3146 arm7_9->wp1_used = 0;
3147 arm7_9->wp1_used_default = 0;
3148 arm7_9->use_dbgrq = 0;
3150 arm7_9->etm_ctx = NULL;
3151 arm7_9->has_single_step = 0;
3152 arm7_9->has_monitor_mode = 0;
3153 arm7_9->has_vector_catch = 0;
3155 arm7_9->debug_entry_from_reset = 0;
3157 arm7_9->dcc_working_area = NULL;
3159 arm7_9->fast_memory_access = fast_and_dangerous;
3160 arm7_9->dcc_downloads = fast_and_dangerous;
3162 arm7_9->need_bypass_before_restart = 0;
3164 armv4_5->arch_info = arm7_9;
3165 armv4_5->read_core_reg = arm7_9_read_core_reg;
3166 armv4_5->write_core_reg = arm7_9_write_core_reg;
3167 armv4_5->full_context = arm7_9_full_context;
3169 if ((retval = armv4_5_init_arch_info(target, armv4_5)) != ERROR_OK)
3174 if ((retval = target_register_timer_callback(arm7_9_handle_target_request, 1, 1, target)) != ERROR_OK)