1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
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. *
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. *
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 ***************************************************************************/
24 #include "replacements.h"
26 #include "embeddedice.h"
28 #include "target_request.h"
33 #include "arm7_9_common.h"
34 #include "breakpoints.h"
40 #include <sys/types.h>
45 int arm7_9_debug_entry(target_t *target);
46 int arm7_9_enable_sw_bkpts(struct target_s *target);
48 /* command handler forward declarations */
49 int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 int handle_arm7_9_read_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 int handle_arm7_9_sw_bkpts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 int handle_arm7_9_force_hw_bkpts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
58 int handle_arm7_9_etm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 int arm7_9_reinit_embeddedice(target_t *target)
62 armv4_5_common_t *armv4_5 = target->arch_info;
63 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
65 breakpoint_t *breakpoint = target->breakpoints;
67 arm7_9->wp_available = 2;
71 /* mark all hardware breakpoints as unset */
74 if (breakpoint->type == BKPT_HARD)
78 breakpoint = breakpoint->next;
81 if (arm7_9->sw_bkpts_enabled && arm7_9->sw_bkpts_use_wp)
83 arm7_9->sw_bkpts_enabled = 0;
84 arm7_9_enable_sw_bkpts(target);
87 arm7_9->reinit_embeddedice = 0;
92 int arm7_9_jtag_callback(enum jtag_event event, void *priv)
94 target_t *target = priv;
95 armv4_5_common_t *armv4_5 = target->arch_info;
96 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
98 /* a test-logic reset occured
99 * the EmbeddedICE registers have been reset
100 * hardware breakpoints have been cleared
102 if (event == JTAG_TRST_ASSERTED)
104 arm7_9->reinit_embeddedice = 1;
110 int arm7_9_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p)
112 armv4_5_common_t *armv4_5 = target->arch_info;
113 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
115 if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
120 if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
125 *armv4_5_p = armv4_5;
131 int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
133 armv4_5_common_t *armv4_5 = target->arch_info;
134 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
136 if (target->state != TARGET_HALTED)
138 LOG_WARNING("target not halted");
139 return ERROR_TARGET_NOT_HALTED;
142 if (arm7_9->force_hw_bkpts)
143 breakpoint->type = BKPT_HARD;
147 LOG_WARNING("breakpoint already set");
151 if (breakpoint->type == BKPT_HARD)
153 /* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
154 u32 mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
155 if (!arm7_9->wp0_used)
157 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], breakpoint->address);
158 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
159 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffffu);
160 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
161 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
163 jtag_execute_queue();
164 arm7_9->wp0_used = 1;
167 else if (!arm7_9->wp1_used)
169 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], breakpoint->address);
170 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
171 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffffu);
172 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
173 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
175 jtag_execute_queue();
176 arm7_9->wp1_used = 1;
181 LOG_ERROR("BUG: no hardware comparator available");
185 else if (breakpoint->type == BKPT_SOFT)
187 if (breakpoint->length == 4)
189 u32 verify = 0xffffffff;
190 /* keep the original instruction in target endianness */
191 target->type->read_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr);
192 /* write the breakpoint instruction in target endianness (arm7_9->arm_bkpt is host endian) */
193 target_write_u32(target, breakpoint->address, arm7_9->arm_bkpt);
195 target->type->read_memory(target, breakpoint->address, 4, 1, (u8 *)&verify);
196 if (verify != arm7_9->arm_bkpt)
198 LOG_ERROR("Unable to set 32 bit software breakpoint at address %08x", breakpoint->address);
205 /* keep the original instruction in target endianness */
206 target->type->read_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr);
207 /* write the breakpoint instruction in target endianness (arm7_9->thumb_bkpt is host endian) */
208 target_write_u16(target, breakpoint->address, arm7_9->thumb_bkpt);
210 target->type->read_memory(target, breakpoint->address, 2, 1, (u8 *)&verify);
211 if (verify != arm7_9->thumb_bkpt)
213 LOG_ERROR("Unable to set thumb software breakpoint at address %08x", breakpoint->address);
224 int arm7_9_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
226 armv4_5_common_t *armv4_5 = target->arch_info;
227 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
229 if (target->state != TARGET_HALTED)
231 LOG_WARNING("target not halted");
232 return ERROR_TARGET_NOT_HALTED;
235 if (!breakpoint->set)
237 LOG_WARNING("breakpoint not set");
241 if (breakpoint->type == BKPT_HARD)
243 if (breakpoint->set == 1)
245 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
246 jtag_execute_queue();
247 arm7_9->wp0_used = 0;
249 else if (breakpoint->set == 2)
251 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
252 jtag_execute_queue();
253 arm7_9->wp1_used = 0;
259 /* restore original instruction (kept in target endianness) */
260 if (breakpoint->length == 4)
263 /* check that user program as not modified breakpoint instruction */
264 target->type->read_memory(target, breakpoint->address, 4, 1, (u8*)¤t_instr);
265 if (current_instr==arm7_9->arm_bkpt)
266 target->type->write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr);
271 /* check that user program as not modified breakpoint instruction */
272 target->type->read_memory(target, breakpoint->address, 2, 1, (u8*)¤t_instr);
273 if (current_instr==arm7_9->thumb_bkpt)
274 target->type->write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr);
282 int arm7_9_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
284 armv4_5_common_t *armv4_5 = target->arch_info;
285 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
287 if (target->state != TARGET_HALTED)
289 LOG_WARNING("target not halted");
290 return ERROR_TARGET_NOT_HALTED;
293 if (arm7_9->force_hw_bkpts)
295 LOG_DEBUG("forcing use of hardware breakpoint at address 0x%8.8x", breakpoint->address);
296 breakpoint->type = BKPT_HARD;
299 if ((breakpoint->type == BKPT_SOFT) && (arm7_9->sw_bkpts_enabled == 0))
301 LOG_INFO("sw breakpoint requested, but software breakpoints not enabled");
302 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
305 if ((breakpoint->type == BKPT_HARD) && (arm7_9->wp_available < 1))
307 LOG_INFO("no watchpoint unit available for hardware breakpoint");
308 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
311 if ((breakpoint->length != 2) && (breakpoint->length != 4))
313 LOG_INFO("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
314 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
317 if (breakpoint->type == BKPT_HARD)
318 arm7_9->wp_available--;
323 int arm7_9_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
325 armv4_5_common_t *armv4_5 = target->arch_info;
326 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
328 if (target->state != TARGET_HALTED)
330 LOG_WARNING("target not halted");
331 return ERROR_TARGET_NOT_HALTED;
336 arm7_9_unset_breakpoint(target, breakpoint);
339 if (breakpoint->type == BKPT_HARD)
340 arm7_9->wp_available++;
345 int arm7_9_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
347 armv4_5_common_t *armv4_5 = target->arch_info;
348 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
352 mask = watchpoint->length - 1;
354 if (target->state != TARGET_HALTED)
356 LOG_WARNING("target not halted");
357 return ERROR_TARGET_NOT_HALTED;
360 if (watchpoint->rw == WPT_ACCESS)
365 if (!arm7_9->wp0_used)
367 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], watchpoint->address);
368 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
369 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], watchpoint->mask);
370 if( watchpoint->mask != 0xffffffffu )
371 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], watchpoint->value);
372 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
373 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
375 jtag_execute_queue();
377 arm7_9->wp0_used = 2;
379 else if (!arm7_9->wp1_used)
381 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], watchpoint->address);
382 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
383 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], watchpoint->mask);
384 if( watchpoint->mask != 0xffffffffu )
385 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], watchpoint->value);
386 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
387 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
389 jtag_execute_queue();
391 arm7_9->wp1_used = 2;
395 LOG_ERROR("BUG: no hardware comparator available");
402 int arm7_9_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
404 armv4_5_common_t *armv4_5 = target->arch_info;
405 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
407 if (target->state != TARGET_HALTED)
409 LOG_WARNING("target not halted");
410 return ERROR_TARGET_NOT_HALTED;
413 if (!watchpoint->set)
415 LOG_WARNING("breakpoint not set");
419 if (watchpoint->set == 1)
421 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
422 jtag_execute_queue();
423 arm7_9->wp0_used = 0;
425 else if (watchpoint->set == 2)
427 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
428 jtag_execute_queue();
429 arm7_9->wp1_used = 0;
436 int arm7_9_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
438 armv4_5_common_t *armv4_5 = target->arch_info;
439 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
441 if (target->state != TARGET_HALTED)
443 LOG_WARNING("target not halted");
444 return ERROR_TARGET_NOT_HALTED;
447 if (arm7_9->wp_available < 1)
449 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
452 if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
454 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
457 arm7_9->wp_available--;
462 int arm7_9_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
464 armv4_5_common_t *armv4_5 = target->arch_info;
465 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
467 if (target->state != TARGET_HALTED)
469 LOG_WARNING("target not halted");
470 return ERROR_TARGET_NOT_HALTED;
475 arm7_9_unset_watchpoint(target, watchpoint);
478 arm7_9->wp_available++;
483 int arm7_9_enable_sw_bkpts(struct target_s *target)
485 armv4_5_common_t *armv4_5 = target->arch_info;
486 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
489 if (arm7_9->sw_bkpts_enabled)
492 if (arm7_9->wp_available < 1)
494 LOG_WARNING("can't enable sw breakpoints with no watchpoint unit available");
495 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
497 arm7_9->wp_available--;
499 if (!arm7_9->wp0_used)
501 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], arm7_9->arm_bkpt);
502 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0x0);
503 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffffu);
504 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
505 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
506 arm7_9->sw_bkpts_enabled = 1;
507 arm7_9->wp0_used = 3;
509 else if (!arm7_9->wp1_used)
511 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], arm7_9->arm_bkpt);
512 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0x0);
513 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0xffffffffu);
514 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
515 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
516 arm7_9->sw_bkpts_enabled = 2;
517 arm7_9->wp1_used = 3;
521 LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
525 if ((retval = jtag_execute_queue()) != ERROR_OK)
527 LOG_ERROR("error writing EmbeddedICE registers to enable sw breakpoints");
534 int arm7_9_disable_sw_bkpts(struct target_s *target)
536 armv4_5_common_t *armv4_5 = target->arch_info;
537 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
539 if (!arm7_9->sw_bkpts_enabled)
542 if (arm7_9->sw_bkpts_enabled == 1)
544 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
545 arm7_9->sw_bkpts_enabled = 0;
546 arm7_9->wp0_used = 0;
547 arm7_9->wp_available++;
549 else if (arm7_9->sw_bkpts_enabled == 2)
551 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
552 arm7_9->sw_bkpts_enabled = 0;
553 arm7_9->wp1_used = 0;
554 arm7_9->wp_available++;
560 int arm7_9_execute_sys_speed(struct target_s *target)
565 armv4_5_common_t *armv4_5 = target->arch_info;
566 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
567 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
568 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
570 /* set RESTART instruction */
571 jtag_add_end_state(TAP_RTI);
572 arm_jtag_set_instr(jtag_info, 0x4, NULL);
574 for (timeout=0; timeout<50; timeout++)
576 /* read debug status register */
577 embeddedice_read_reg(dbg_stat);
578 if ((retval = jtag_execute_queue()) != ERROR_OK)
580 if ((buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
581 && (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_SYSCOMP, 1)))
587 LOG_ERROR("timeout waiting for SYSCOMP & DBGACK, last DBG_STATUS: %x", buf_get_u32(dbg_stat->value, 0, dbg_stat->size));
588 return ERROR_TARGET_TIMEOUT;
594 int arm7_9_execute_fast_sys_speed(struct target_s *target)
597 static u8 check_value[4], check_mask[4];
599 armv4_5_common_t *armv4_5 = target->arch_info;
600 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
601 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
602 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
604 /* set RESTART instruction */
605 jtag_add_end_state(TAP_RTI);
606 arm_jtag_set_instr(jtag_info, 0x4, NULL);
610 /* check for DBGACK and SYSCOMP set (others don't care) */
612 /* NB! These are constants that must be available until after next jtag_execute() and
613 we evaluate the values upon first execution in lieu of setting up these constants
616 buf_set_u32(check_value, 0, 32, 0x9);
617 buf_set_u32(check_mask, 0, 32, 0x9);
621 /* read debug status register */
622 embeddedice_read_reg_w_check(dbg_stat, check_value, check_value);
627 int arm7_9_target_request_data(target_t *target, u32 size, u8 *buffer)
629 armv4_5_common_t *armv4_5 = target->arch_info;
630 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
631 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
635 data = malloc(size * (sizeof(u32)));
637 embeddedice_receive(jtag_info, data, size);
639 for (i = 0; i < size; i++)
641 h_u32_to_le(buffer + (i * 4), data[i]);
649 int arm7_9_handle_target_request(void *priv)
651 target_t *target = priv;
652 armv4_5_common_t *armv4_5 = target->arch_info;
653 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
654 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
655 reg_t *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
657 if (!target->dbg_msg_enabled)
660 if (target->state == TARGET_RUNNING)
662 /* read DCC control register */
663 embeddedice_read_reg(dcc_control);
664 jtag_execute_queue();
667 if (buf_get_u32(dcc_control->value, 1, 1) == 1)
671 embeddedice_receive(jtag_info, &request, 1);
672 target_request(target, request);
679 int arm7_9_poll(target_t *target)
682 armv4_5_common_t *armv4_5 = target->arch_info;
683 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
684 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
686 if (arm7_9->reinit_embeddedice)
688 arm7_9_reinit_embeddedice(target);
691 /* read debug status register */
692 embeddedice_read_reg(dbg_stat);
693 if ((retval = jtag_execute_queue()) != ERROR_OK)
698 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
700 LOG_DEBUG("DBGACK set, dbg_state->value: 0x%x", buf_get_u32(dbg_stat->value, 0, 32));
701 if (target->state == TARGET_UNKNOWN)
703 target->state = TARGET_RUNNING;
704 LOG_WARNING("DBGACK set while target was in unknown state. Reset or initialize target.");
706 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
708 target->state = TARGET_HALTED;
709 if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
712 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
714 if (target->state == TARGET_DEBUG_RUNNING)
716 target->state = TARGET_HALTED;
717 if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
720 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
722 if (target->state != TARGET_HALTED)
724 LOG_WARNING("DBGACK set, but the target did not end up in the halted stated %d", target->state);
729 if (target->state != TARGET_DEBUG_RUNNING)
730 target->state = TARGET_RUNNING;
736 int arm7_9_assert_reset(target_t *target)
738 LOG_DEBUG("target->state: %s", target_state_strings[target->state]);
740 if (!(jtag_reset_config & RESET_HAS_SRST))
742 LOG_ERROR("Can't assert SRST");
746 if (target->state == TARGET_HALTED || target->state == TARGET_UNKNOWN)
748 /* if the target wasn't running, there might be working areas allocated */
749 target_free_all_working_areas(target);
751 /* assert SRST and TRST */
752 /* system would get ouf sync if we didn't reset test-logic, too */
753 jtag_add_reset(1, 1);
755 jtag_add_sleep(5000);
759 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
761 jtag_add_reset(1, 1);
764 jtag_add_reset(0, 1);
767 target->state = TARGET_RESET;
768 jtag_add_sleep(50000);
770 armv4_5_invalidate_core_regs(target);
776 int arm7_9_deassert_reset(target_t *target)
778 LOG_DEBUG("target->state: %s", target_state_strings[target->state]);
780 /* deassert reset lines */
781 jtag_add_reset(0, 0);
786 int arm7_9_clear_halt(target_t *target)
788 armv4_5_common_t *armv4_5 = target->arch_info;
789 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
790 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
792 /* we used DBGRQ only if we didn't come out of reset */
793 if (!arm7_9->debug_entry_from_reset && arm7_9->use_dbgrq)
795 /* program EmbeddedICE Debug Control Register to deassert DBGRQ
797 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
798 embeddedice_store_reg(dbg_ctrl);
802 if (arm7_9->debug_entry_from_reset && arm7_9->has_vector_catch)
804 /* if we came out of reset, and vector catch is supported, we used
805 * vector catch to enter debug state
806 * restore the register in that case
808 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH]);
812 /* restore registers if watchpoint unit 0 was in use
814 if (arm7_9->wp0_used)
816 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
817 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
818 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
820 /* control value always has to be restored, as it was either disabled,
821 * or enabled with possibly different bits
823 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
830 int arm7_9_soft_reset_halt(struct target_s *target)
832 armv4_5_common_t *armv4_5 = target->arch_info;
833 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
834 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
835 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
839 if ((retval=target->type->halt(target))!=ERROR_OK)
844 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
846 embeddedice_read_reg(dbg_stat);
847 if ((retval=jtag_execute_queue())!=ERROR_OK)
849 /* do not eat all CPU, time out after 1 se*/
855 LOG_ERROR("Failed to halt CPU after 1 sec");
856 return ERROR_TARGET_TIMEOUT;
858 target->state = TARGET_HALTED;
860 /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
861 * ensure that DBGRQ is cleared
863 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
864 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
865 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
866 embeddedice_store_reg(dbg_ctrl);
868 arm7_9_clear_halt(target);
870 /* if the target is in Thumb state, change to ARM state */
871 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
873 u32 r0_thumb, pc_thumb;
874 LOG_DEBUG("target entered debug from Thumb state, changing to ARM");
875 /* Entered debug from Thumb mode */
876 armv4_5->core_state = ARMV4_5_STATE_THUMB;
877 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
880 /* all register content is now invalid */
881 armv4_5_invalidate_core_regs(target);
883 /* SVC, ARM state, IRQ and FIQ disabled */
884 buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8, 0xd3);
885 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 1;
886 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
888 /* start fetching from 0x0 */
889 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, 0x0);
890 armv4_5->core_cache->reg_list[15].dirty = 1;
891 armv4_5->core_cache->reg_list[15].valid = 1;
893 armv4_5->core_mode = ARMV4_5_MODE_SVC;
894 armv4_5->core_state = ARMV4_5_STATE_ARM;
896 /* reset registers */
897 for (i = 0; i <= 14; i++)
899 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, 0xffffffff);
900 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 1;
901 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
904 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
909 int arm7_9_prepare_reset_halt(target_t *target)
911 armv4_5_common_t *armv4_5 = target->arch_info;
912 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
914 /* poll the target, and resume if it was currently halted */
916 if (target->state == TARGET_HALTED)
918 arm7_9_resume(target, 1, 0x0, 0, 1);
921 if (arm7_9->has_vector_catch)
923 /* program vector catch register to catch reset vector */
924 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH], 0x1);
928 /* program watchpoint unit to match on reset vector address */
929 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0x3);
930 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0x0);
931 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x100);
932 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xf7);
938 int arm7_9_halt(target_t *target)
940 armv4_5_common_t *armv4_5 = target->arch_info;
941 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
942 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
944 LOG_DEBUG("target->state: %s", target_state_strings[target->state]);
946 if (target->state == TARGET_HALTED)
948 LOG_WARNING("target was already halted");
952 if (target->state == TARGET_UNKNOWN)
954 LOG_WARNING("target was in unknown state when halt was requested");
957 if (target->state == TARGET_RESET)
959 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) && jtag_srst)
961 LOG_ERROR("can't request a halt while in reset if nSRST pulls nTRST");
962 return ERROR_TARGET_FAILURE;
966 /* we came here in a reset_halt or reset_init sequence
967 * debug entry was already prepared in arm7_9_prepare_reset_halt()
969 target->debug_reason = DBG_REASON_DBGRQ;
975 if (arm7_9->use_dbgrq)
977 /* program EmbeddedICE Debug Control Register to assert DBGRQ
979 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 1);
980 embeddedice_store_reg(dbg_ctrl);
984 /* program watchpoint unit to match on any address
986 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
987 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
988 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x100);
989 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xf7);
992 target->debug_reason = DBG_REASON_DBGRQ;
997 int arm7_9_debug_entry(target_t *target)
1002 u32 r0_thumb, pc_thumb;
1005 /* get pointers to arch-specific information */
1006 armv4_5_common_t *armv4_5 = target->arch_info;
1007 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1008 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
1009 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1011 #ifdef _DEBUG_ARM7_9_
1015 if (arm7_9->pre_debug_entry)
1016 arm7_9->pre_debug_entry(target);
1018 /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
1019 * ensure that DBGRQ is cleared
1021 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
1022 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1023 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
1024 embeddedice_store_reg(dbg_ctrl);
1026 arm7_9_clear_halt(target);
1028 if ((retval = jtag_execute_queue()) != ERROR_OK)
1032 case ERROR_JTAG_QUEUE_FAILED:
1033 LOG_ERROR("JTAG queue failed while writing EmbeddedICE control register");
1041 if ((retval = arm7_9->examine_debug_reason(target)) != ERROR_OK)
1045 if (target->state != TARGET_HALTED)
1047 LOG_WARNING("target not halted");
1048 return ERROR_TARGET_NOT_HALTED;
1051 /* if the target is in Thumb state, change to ARM state */
1052 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
1054 LOG_DEBUG("target entered debug from Thumb state");
1055 /* Entered debug from Thumb mode */
1056 armv4_5->core_state = ARMV4_5_STATE_THUMB;
1057 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
1058 LOG_DEBUG("r0_thumb: 0x%8.8x, pc_thumb: 0x%8.8x", r0_thumb, pc_thumb);
1062 LOG_DEBUG("target entered debug from ARM state");
1063 /* Entered debug from ARM mode */
1064 armv4_5->core_state = ARMV4_5_STATE_ARM;
1067 for (i = 0; i < 16; i++)
1068 context_p[i] = &context[i];
1069 /* save core registers (r0 - r15 of current core mode) */
1070 arm7_9->read_core_regs(target, 0xffff, context_p);
1072 arm7_9->read_xpsr(target, &cpsr, 0);
1074 if ((retval = jtag_execute_queue()) != ERROR_OK)
1077 /* if the core has been executing in Thumb state, set the T bit */
1078 if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1081 buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32, cpsr);
1082 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
1083 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1085 armv4_5->core_mode = cpsr & 0x1f;
1087 if (armv4_5_mode_to_number(armv4_5->core_mode) == -1)
1089 target->state = TARGET_UNKNOWN;
1090 LOG_ERROR("cpsr contains invalid mode value - communication failure");
1091 return ERROR_TARGET_FAILURE;
1094 LOG_DEBUG("target entered debug state in %s mode", armv4_5_mode_strings[armv4_5_mode_to_number(armv4_5->core_mode)]);
1096 if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1098 LOG_DEBUG("thumb state, applying fixups");
1099 context[0] = r0_thumb;
1100 context[15] = pc_thumb;
1101 } else if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1103 /* adjust value stored by STM */
1104 context[15] -= 3 * 4;
1107 if ((target->debug_reason == DBG_REASON_BREAKPOINT)
1108 || (target->debug_reason == DBG_REASON_SINGLESTEP)
1109 || (target->debug_reason == DBG_REASON_WATCHPOINT)
1110 || (target->debug_reason == DBG_REASON_WPTANDBKPT)
1111 || ((target->debug_reason == DBG_REASON_DBGRQ) && (arm7_9->use_dbgrq == 0)))
1112 context[15] -= 3 * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
1113 else if (target->debug_reason == DBG_REASON_DBGRQ)
1114 context[15] -= arm7_9->dbgreq_adjust_pc * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
1117 LOG_ERROR("unknown debug reason: %i", target->debug_reason);
1121 for (i=0; i<=15; i++)
1123 LOG_DEBUG("r%i: 0x%8.8x", i, context[i]);
1124 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, context[i]);
1125 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 0;
1126 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
1129 LOG_DEBUG("entered debug state at PC 0x%x", context[15]);
1131 /* exceptions other than USR & SYS have a saved program status register */
1132 if ((armv4_5_mode_to_number(armv4_5->core_mode) != ARMV4_5_MODE_USR) && (armv4_5_mode_to_number(armv4_5->core_mode) != ARMV4_5_MODE_SYS))
1135 arm7_9->read_xpsr(target, &spsr, 1);
1136 jtag_execute_queue();
1137 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).value, 0, 32, spsr);
1138 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).dirty = 0;
1139 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).valid = 1;
1142 /* r0 and r15 (pc) have to be restored later */
1143 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;
1144 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;
1146 if ((retval = jtag_execute_queue()) != ERROR_OK)
1149 if (arm7_9->post_debug_entry)
1150 arm7_9->post_debug_entry(target);
1155 int arm7_9_full_context(target_t *target)
1159 armv4_5_common_t *armv4_5 = target->arch_info;
1160 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1164 if (target->state != TARGET_HALTED)
1166 LOG_WARNING("target not halted");
1167 return ERROR_TARGET_NOT_HALTED;
1170 /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1171 * SYS shares registers with User, so we don't touch SYS
1173 for(i = 0; i < 6; i++)
1180 /* check if there are invalid registers in the current mode
1182 for (j = 0; j <= 16; j++)
1184 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1192 /* change processor mode (and mask T bit) */
1193 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1194 tmp_cpsr |= armv4_5_number_to_mode(i);
1196 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1198 for (j = 0; j < 15; j++)
1200 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1202 reg_p[j] = (u32*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).value;
1204 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid = 1;
1205 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).dirty = 0;
1209 /* if only the PSR is invalid, mask is all zeroes */
1211 arm7_9->read_core_regs(target, mask, reg_p);
1213 /* check if the PSR has to be read */
1214 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid == 0)
1216 arm7_9->read_xpsr(target, (u32*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).value, 1);
1217 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid = 1;
1218 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).dirty = 0;
1223 /* restore processor mode (mask T bit) */
1224 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
1226 if ((retval = jtag_execute_queue()) != ERROR_OK)
1228 LOG_ERROR("JTAG failure");
1234 int arm7_9_restore_context(target_t *target)
1236 armv4_5_common_t *armv4_5 = target->arch_info;
1237 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1239 armv4_5_core_reg_t *reg_arch_info;
1240 enum armv4_5_mode current_mode = armv4_5->core_mode;
1247 if (target->state != TARGET_HALTED)
1249 LOG_WARNING("target not halted");
1250 return ERROR_TARGET_NOT_HALTED;
1253 if (arm7_9->pre_restore_context)
1254 arm7_9->pre_restore_context(target);
1256 /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1257 * SYS shares registers with User, so we don't touch SYS
1259 for (i = 0; i < 6; i++)
1261 LOG_DEBUG("examining %s mode", armv4_5_mode_strings[i]);
1264 /* check if there are dirty registers in the current mode
1266 for (j = 0; j <= 16; j++)
1268 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
1269 reg_arch_info = reg->arch_info;
1270 if (reg->dirty == 1)
1272 if (reg->valid == 1)
1275 LOG_DEBUG("examining dirty reg: %s", reg->name);
1276 if ((reg_arch_info->mode != ARMV4_5_MODE_ANY)
1277 && (reg_arch_info->mode != current_mode)
1278 && !((reg_arch_info->mode == ARMV4_5_MODE_USR) && (armv4_5->core_mode == ARMV4_5_MODE_SYS))
1279 && !((reg_arch_info->mode == ARMV4_5_MODE_SYS) && (armv4_5->core_mode == ARMV4_5_MODE_USR)))
1282 LOG_DEBUG("require mode change");
1287 LOG_ERROR("BUG: dirty register '%s', but no valid data", reg->name);
1302 /* change processor mode (mask T bit) */
1303 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1304 tmp_cpsr |= armv4_5_number_to_mode(i);
1306 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1307 current_mode = armv4_5_number_to_mode(i);
1310 for (j = 0; j <= 14; j++)
1312 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
1313 reg_arch_info = reg->arch_info;
1316 if (reg->dirty == 1)
1318 regs[j] = buf_get_u32(reg->value, 0, 32);
1323 LOG_DEBUG("writing register %i of mode %s with value 0x%8.8x", j, armv4_5_mode_strings[i], regs[j]);
1329 arm7_9->write_core_regs(target, mask, regs);
1332 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16);
1333 reg_arch_info = reg->arch_info;
1334 if ((reg->dirty) && (reg_arch_info->mode != ARMV4_5_MODE_ANY))
1336 LOG_DEBUG("writing SPSR of mode %i with value 0x%8.8x", i, buf_get_u32(reg->value, 0, 32));
1337 arm7_9->write_xpsr(target, buf_get_u32(reg->value, 0, 32), 1);
1342 if ((armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 0) && (armv4_5->core_mode != current_mode))
1344 /* restore processor mode (mask T bit) */
1347 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1348 tmp_cpsr |= armv4_5_number_to_mode(i);
1350 LOG_DEBUG("writing lower 8 bit of cpsr with value 0x%2.2x", tmp_cpsr);
1351 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1353 else if (armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 1)
1355 /* CPSR has been changed, full restore necessary (mask T bit) */
1356 LOG_DEBUG("writing cpsr with value 0x%8.8x", buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32));
1357 arm7_9->write_xpsr(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32) & ~0x20, 0);
1358 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
1359 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1363 LOG_DEBUG("writing PC with value 0x%8.8x", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1364 arm7_9->write_pc(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1365 armv4_5->core_cache->reg_list[15].dirty = 0;
1367 if (arm7_9->post_restore_context)
1368 arm7_9->post_restore_context(target);
1373 int arm7_9_restart_core(struct target_s *target)
1375 armv4_5_common_t *armv4_5 = target->arch_info;
1376 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1377 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
1379 /* set RESTART instruction */
1380 jtag_add_end_state(TAP_RTI);
1381 arm_jtag_set_instr(jtag_info, 0x4, NULL);
1383 jtag_add_runtest(1, TAP_RTI);
1384 if ((jtag_execute_queue()) != ERROR_OK)
1392 void arm7_9_enable_watchpoints(struct target_s *target)
1394 watchpoint_t *watchpoint = target->watchpoints;
1398 if (watchpoint->set == 0)
1399 arm7_9_set_watchpoint(target, watchpoint);
1400 watchpoint = watchpoint->next;
1404 void arm7_9_enable_breakpoints(struct target_s *target)
1406 breakpoint_t *breakpoint = target->breakpoints;
1408 /* set any pending breakpoints */
1411 if (breakpoint->set == 0)
1412 arm7_9_set_breakpoint(target, breakpoint);
1413 breakpoint = breakpoint->next;
1417 void arm7_9_disable_bkpts_and_wpts(struct target_s *target)
1419 breakpoint_t *breakpoint = target->breakpoints;
1420 watchpoint_t *watchpoint = target->watchpoints;
1422 /* set any pending breakpoints */
1425 if (breakpoint->set != 0)
1426 arm7_9_unset_breakpoint(target, breakpoint);
1427 breakpoint = breakpoint->next;
1432 if (watchpoint->set != 0)
1433 arm7_9_unset_watchpoint(target, watchpoint);
1434 watchpoint = watchpoint->next;
1438 int arm7_9_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
1440 armv4_5_common_t *armv4_5 = target->arch_info;
1441 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1442 breakpoint_t *breakpoint = target->breakpoints;
1443 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1448 if (target->state != TARGET_HALTED)
1450 LOG_WARNING("target not halted");
1451 return ERROR_TARGET_NOT_HALTED;
1454 if (!debug_execution)
1456 target_free_all_working_areas(target);
1459 /* current = 1: continue on current pc, otherwise continue at <address> */
1461 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
1463 /* the front-end may request us not to handle breakpoints */
1464 if (handle_breakpoints)
1466 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
1468 LOG_DEBUG("unset breakpoint at 0x%8.8x", breakpoint->address);
1469 arm7_9_unset_breakpoint(target, breakpoint);
1471 LOG_DEBUG("enable single-step");
1472 arm7_9->enable_single_step(target);
1474 target->debug_reason = DBG_REASON_SINGLESTEP;
1476 arm7_9_restore_context(target);
1478 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1479 arm7_9->branch_resume(target);
1480 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1482 arm7_9->branch_resume_thumb(target);
1486 LOG_ERROR("unhandled core state");
1490 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1491 embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1492 err = arm7_9_execute_sys_speed(target);
1494 LOG_DEBUG("disable single-step");
1495 arm7_9->disable_single_step(target);
1497 if (err != ERROR_OK)
1499 arm7_9_set_breakpoint(target, breakpoint);
1500 target->state = TARGET_UNKNOWN;
1504 arm7_9_debug_entry(target);
1505 LOG_DEBUG("new PC after step: 0x%8.8x", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1507 LOG_DEBUG("set breakpoint at 0x%8.8x", breakpoint->address);
1508 arm7_9_set_breakpoint(target, breakpoint);
1512 /* enable any pending breakpoints and watchpoints */
1513 arm7_9_enable_breakpoints(target);
1514 arm7_9_enable_watchpoints(target);
1516 arm7_9_restore_context(target);
1518 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1520 arm7_9->branch_resume(target);
1522 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1524 arm7_9->branch_resume_thumb(target);
1528 LOG_ERROR("unhandled core state");
1532 /* deassert DBGACK and INTDIS */
1533 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1534 /* INTDIS only when we really resume, not during debug execution */
1535 if (!debug_execution)
1536 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 0);
1537 embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1539 arm7_9_restart_core(target);
1541 target->debug_reason = DBG_REASON_NOTHALTED;
1543 if (!debug_execution)
1545 /* registers are now invalid */
1546 armv4_5_invalidate_core_regs(target);
1547 target->state = TARGET_RUNNING;
1548 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1552 target->state = TARGET_DEBUG_RUNNING;
1553 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
1556 LOG_DEBUG("target resumed");
1561 void arm7_9_enable_eice_step(target_t *target)
1563 armv4_5_common_t *armv4_5 = target->arch_info;
1564 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1566 /* setup an inverse breakpoint on the current PC
1567 * - comparator 1 matches the current address
1568 * - rangeout from comparator 1 is connected to comparator 0 rangein
1569 * - comparator 0 matches any address, as long as rangein is low */
1570 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1571 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1572 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x100);
1573 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0x77);
1574 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1575 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
1576 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
1577 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
1578 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], 0xf7);
1581 void arm7_9_disable_eice_step(target_t *target)
1583 armv4_5_common_t *armv4_5 = target->arch_info;
1584 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1586 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
1587 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
1588 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
1589 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
1590 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE]);
1591 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK]);
1592 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK]);
1593 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK]);
1594 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE]);
1597 int arm7_9_step(struct target_s *target, int current, u32 address, int handle_breakpoints)
1599 armv4_5_common_t *armv4_5 = target->arch_info;
1600 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1601 breakpoint_t *breakpoint = NULL;
1604 if (target->state != TARGET_HALTED)
1606 LOG_WARNING("target not halted");
1607 return ERROR_TARGET_NOT_HALTED;
1610 /* current = 1: continue on current pc, otherwise continue at <address> */
1612 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
1614 /* the front-end may request us not to handle breakpoints */
1615 if (handle_breakpoints)
1616 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
1617 arm7_9_unset_breakpoint(target, breakpoint);
1619 target->debug_reason = DBG_REASON_SINGLESTEP;
1621 arm7_9_restore_context(target);
1623 arm7_9->enable_single_step(target);
1625 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1627 arm7_9->branch_resume(target);
1629 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1631 arm7_9->branch_resume_thumb(target);
1635 LOG_ERROR("unhandled core state");
1639 target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
1641 err = arm7_9_execute_sys_speed(target);
1642 arm7_9->disable_single_step(target);
1644 /* registers are now invalid */
1645 armv4_5_invalidate_core_regs(target);
1647 if (err != ERROR_OK)
1649 target->state = TARGET_UNKNOWN;
1651 arm7_9_debug_entry(target);
1652 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1653 LOG_DEBUG("target stepped");
1657 arm7_9_set_breakpoint(target, breakpoint);
1663 int arm7_9_read_core_reg(struct target_s *target, int num, enum armv4_5_mode mode)
1668 armv4_5_common_t *armv4_5 = target->arch_info;
1669 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1670 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;
1672 if ((num < 0) || (num > 16))
1673 return ERROR_INVALID_ARGUMENTS;
1675 if ((mode != ARMV4_5_MODE_ANY)
1676 && (mode != armv4_5->core_mode)
1677 && (reg_mode != ARMV4_5_MODE_ANY))
1681 /* change processor mode (mask T bit) */
1682 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1685 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1688 if ((num >= 0) && (num <= 15))
1690 /* read a normal core register */
1691 reg_p[num] = &value;
1693 arm7_9->read_core_regs(target, 1 << num, reg_p);
1697 /* read a program status register
1698 * if the register mode is MODE_ANY, we read the cpsr, otherwise a spsr
1700 armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
1701 int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
1703 arm7_9->read_xpsr(target, &value, spsr);
1706 if ((retval = jtag_execute_queue()) != ERROR_OK)
1708 LOG_ERROR("JTAG failure");
1712 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
1713 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
1714 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).value, 0, 32, value);
1716 if ((mode != ARMV4_5_MODE_ANY)
1717 && (mode != armv4_5->core_mode)
1718 && (reg_mode != ARMV4_5_MODE_ANY)) {
1719 /* restore processor mode (mask T bit) */
1720 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
1727 int arm7_9_write_core_reg(struct target_s *target, int num, enum armv4_5_mode mode, u32 value)
1730 armv4_5_common_t *armv4_5 = target->arch_info;
1731 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1732 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;
1734 if ((num < 0) || (num > 16))
1735 return ERROR_INVALID_ARGUMENTS;
1737 if ((mode != ARMV4_5_MODE_ANY)
1738 && (mode != armv4_5->core_mode)
1739 && (reg_mode != ARMV4_5_MODE_ANY)) {
1742 /* change processor mode (mask T bit) */
1743 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1746 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1749 if ((num >= 0) && (num <= 15))
1751 /* write a normal core register */
1754 arm7_9->write_core_regs(target, 1 << num, reg);
1758 /* write a program status register
1759 * if the register mode is MODE_ANY, we write the cpsr, otherwise a spsr
1761 armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
1762 int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
1764 /* if we're writing the CPSR, mask the T bit */
1768 arm7_9->write_xpsr(target, value, spsr);
1771 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
1772 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
1774 if ((mode != ARMV4_5_MODE_ANY)
1775 && (mode != armv4_5->core_mode)
1776 && (reg_mode != ARMV4_5_MODE_ANY)) {
1777 /* restore processor mode (mask T bit) */
1778 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
1781 return jtag_execute_queue();
1784 int arm7_9_read_memory(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
1786 armv4_5_common_t *armv4_5 = target->arch_info;
1787 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1790 int num_accesses = 0;
1791 int thisrun_accesses;
1797 LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address, size, count);
1799 if (target->state != TARGET_HALTED)
1801 LOG_WARNING("target not halted");
1802 return ERROR_TARGET_NOT_HALTED;
1805 /* sanitize arguments */
1806 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1807 return ERROR_INVALID_ARGUMENTS;
1809 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1810 return ERROR_TARGET_UNALIGNED_ACCESS;
1812 /* load the base register with the address of the first word */
1814 arm7_9->write_core_regs(target, 0x1, reg);
1819 while (num_accesses < count)
1822 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
1823 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
1825 if (last_reg <= thisrun_accesses)
1826 last_reg = thisrun_accesses;
1828 arm7_9->load_word_regs(target, reg_list);
1830 /* fast memory reads are only safe when the target is running
1831 * from a sufficiently high clock (32 kHz is usually too slow)
1833 if (arm7_9->fast_memory_access)
1834 arm7_9_execute_fast_sys_speed(target);
1836 arm7_9_execute_sys_speed(target);
1838 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 4);
1840 /* advance buffer, count number of accesses */
1841 buffer += thisrun_accesses * 4;
1842 num_accesses += thisrun_accesses;
1846 while (num_accesses < count)
1849 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
1850 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
1852 for (i = 1; i <= thisrun_accesses; i++)
1856 arm7_9->load_hword_reg(target, i);
1857 /* fast memory reads are only safe when the target is running
1858 * from a sufficiently high clock (32 kHz is usually too slow)
1860 if (arm7_9->fast_memory_access)
1861 arm7_9_execute_fast_sys_speed(target);
1863 arm7_9_execute_sys_speed(target);
1866 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 2);
1868 /* advance buffer, count number of accesses */
1869 buffer += thisrun_accesses * 2;
1870 num_accesses += thisrun_accesses;
1874 while (num_accesses < count)
1877 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
1878 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
1880 for (i = 1; i <= thisrun_accesses; i++)
1884 arm7_9->load_byte_reg(target, i);
1885 /* fast memory reads are only safe when the target is running
1886 * from a sufficiently high clock (32 kHz is usually too slow)
1888 if (arm7_9->fast_memory_access)
1889 arm7_9_execute_fast_sys_speed(target);
1891 arm7_9_execute_sys_speed(target);
1894 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 1);
1896 /* advance buffer, count number of accesses */
1897 buffer += thisrun_accesses * 1;
1898 num_accesses += thisrun_accesses;
1902 LOG_ERROR("BUG: we shouldn't get here");
1907 for (i=0; i<=last_reg; i++)
1908 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;
1910 arm7_9->read_xpsr(target, &cpsr, 0);
1911 if ((retval = jtag_execute_queue()) != ERROR_OK)
1913 LOG_ERROR("JTAG error while reading cpsr");
1914 return ERROR_TARGET_DATA_ABORT;
1917 if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
1919 LOG_WARNING("memory read caused data abort (address: 0x%8.8x, size: 0x%x, count: 0x%x)", address, size, count);
1921 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
1923 return ERROR_TARGET_DATA_ABORT;
1929 int arm7_9_write_memory(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
1931 armv4_5_common_t *armv4_5 = target->arch_info;
1932 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
1933 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1936 int num_accesses = 0;
1937 int thisrun_accesses;
1943 #ifdef _DEBUG_ARM7_9_
1944 LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address, size, count);
1947 if (target->state != TARGET_HALTED)
1949 LOG_WARNING("target not halted");
1950 return ERROR_TARGET_NOT_HALTED;
1953 /* sanitize arguments */
1954 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
1955 return ERROR_INVALID_ARGUMENTS;
1957 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
1958 return ERROR_TARGET_UNALIGNED_ACCESS;
1960 /* load the base register with the address of the first word */
1962 arm7_9->write_core_regs(target, 0x1, reg);
1964 /* Clear DBGACK, to make sure memory fetches work as expected */
1965 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1966 embeddedice_store_reg(dbg_ctrl);
1971 while (num_accesses < count)
1974 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
1975 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
1977 for (i = 1; i <= thisrun_accesses; i++)
1981 reg[i] = target_buffer_get_u32(target, buffer);
1985 arm7_9->write_core_regs(target, reg_list, reg);
1987 arm7_9->store_word_regs(target, reg_list);
1989 /* fast memory writes are only safe when the target is running
1990 * from a sufficiently high clock (32 kHz is usually too slow)
1992 if (arm7_9->fast_memory_access)
1993 arm7_9_execute_fast_sys_speed(target);
1995 arm7_9_execute_sys_speed(target);
1997 num_accesses += thisrun_accesses;
2001 while (num_accesses < count)
2004 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2005 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2007 for (i = 1; i <= thisrun_accesses; i++)
2011 reg[i] = target_buffer_get_u16(target, buffer) & 0xffff;
2015 arm7_9->write_core_regs(target, reg_list, reg);
2017 for (i = 1; i <= thisrun_accesses; i++)
2019 arm7_9->store_hword_reg(target, i);
2021 /* fast memory writes are only safe when the target is running
2022 * from a sufficiently high clock (32 kHz is usually too slow)
2024 if (arm7_9->fast_memory_access)
2025 arm7_9_execute_fast_sys_speed(target);
2027 arm7_9_execute_sys_speed(target);
2030 num_accesses += thisrun_accesses;
2034 while (num_accesses < count)
2037 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2038 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2040 for (i = 1; i <= thisrun_accesses; i++)
2044 reg[i] = *buffer++ & 0xff;
2047 arm7_9->write_core_regs(target, reg_list, reg);
2049 for (i = 1; i <= thisrun_accesses; i++)
2051 arm7_9->store_byte_reg(target, i);
2052 /* fast memory writes are only safe when the target is running
2053 * from a sufficiently high clock (32 kHz is usually too slow)
2055 if (arm7_9->fast_memory_access)
2056 arm7_9_execute_fast_sys_speed(target);
2058 arm7_9_execute_sys_speed(target);
2061 num_accesses += thisrun_accesses;
2065 LOG_ERROR("BUG: we shouldn't get here");
2071 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
2072 embeddedice_store_reg(dbg_ctrl);
2074 for (i=0; i<=last_reg; i++)
2075 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;
2077 arm7_9->read_xpsr(target, &cpsr, 0);
2078 if ((retval = jtag_execute_queue()) != ERROR_OK)
2080 LOG_ERROR("JTAG error while reading cpsr");
2081 return ERROR_TARGET_DATA_ABORT;
2084 if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
2086 LOG_WARNING("memory write caused data abort (address: 0x%8.8x, size: 0x%x, count: 0x%x)", address, size, count);
2088 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2090 return ERROR_TARGET_DATA_ABORT;
2096 int arm7_9_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffer)
2098 armv4_5_common_t *armv4_5 = target->arch_info;
2099 arm7_9_common_t *arm7_9 = armv4_5->arch_info;
2100 enum armv4_5_state core_state = armv4_5->core_state;
2101 u32 r0 = buf_get_u32(armv4_5->core_cache->reg_list[0].value, 0, 32);
2102 u32 r1 = buf_get_u32(armv4_5->core_cache->reg_list[1].value, 0, 32);
2103 u32 pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
2108 /* MRC TST BNE MRC STR B */
2109 0xee101e10, 0xe3110001, 0x0afffffc, 0xee111e10, 0xe4801004, 0xeafffff9
2112 if (!arm7_9->dcc_downloads)
2113 return target->type->write_memory(target, address, 4, count, buffer);
2115 /* regrab previously allocated working_area, or allocate a new one */
2116 if (!arm7_9->dcc_working_area)
2118 u8 dcc_code_buf[6 * 4];
2120 /* make sure we have a working area */
2121 if (target_alloc_working_area(target, 24, &arm7_9->dcc_working_area) != ERROR_OK)
2123 LOG_INFO("no working area available, falling back to memory writes");
2124 return target->type->write_memory(target, address, 4, count, buffer);
2127 /* copy target instructions to target endianness */
2128 for (i = 0; i < 6; i++)
2130 target_buffer_set_u32(target, dcc_code_buf + i*4, dcc_code[i]);
2133 /* write DCC code to working area */
2134 target->type->write_memory(target, arm7_9->dcc_working_area->address, 4, 6, dcc_code_buf);
2137 buf_set_u32(armv4_5->core_cache->reg_list[0].value, 0, 32, address);
2138 armv4_5->core_cache->reg_list[0].valid = 1;
2139 armv4_5->core_cache->reg_list[0].dirty = 1;
2140 armv4_5->core_state = ARMV4_5_STATE_ARM;
2142 arm7_9_resume(target, 0, arm7_9->dcc_working_area->address, 1, 1);
2144 int little=target->endianness==TARGET_LITTLE_ENDIAN;
2147 /* Handle first & last using standard embeddedice_write_reg and the middle ones w/the
2148 core function repeated.
2150 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2153 embeddedice_reg_t *ice_reg = arm7_9->eice_cache->reg_list[EICE_COMMS_DATA].arch_info;
2154 u8 reg_addr = ice_reg->addr & 0x1f;
2155 int chain_pos = ice_reg->jtag_info->chain_pos;
2156 /* we want the compiler to duplicate the code, which it does not
2161 for (i = 1; i < count - 1; i++)
2163 embeddedice_write_reg_inner(chain_pos, reg_addr, fast_target_buffer_get_u32(buffer, little));
2168 for (i = 1; i < count - 1; i++)
2170 embeddedice_write_reg_inner(chain_pos, reg_addr, fast_target_buffer_get_u32(buffer, little));
2174 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2177 for (i = 0; i < count; i++)
2179 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2184 target->type->halt(target);
2186 for (i=0; i<100; i++)
2188 target->type->poll(target);
2189 if (target->state == TARGET_HALTED)
2191 usleep(1000); /* sleep 1ms */
2195 LOG_ERROR("bulk write timed out, target not halted");
2196 return ERROR_TARGET_TIMEOUT;
2199 /* restore target state */
2200 buf_set_u32(armv4_5->core_cache->reg_list[0].value, 0, 32, r0);
2201 armv4_5->core_cache->reg_list[0].valid = 1;
2202 armv4_5->core_cache->reg_list[0].dirty = 1;
2203 buf_set_u32(armv4_5->core_cache->reg_list[1].value, 0, 32, r1);
2204 armv4_5->core_cache->reg_list[1].valid = 1;
2205 armv4_5->core_cache->reg_list[1].dirty = 1;
2206 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, pc);
2207 armv4_5->core_cache->reg_list[15].valid = 1;
2208 armv4_5->core_cache->reg_list[15].dirty = 1;
2209 armv4_5->core_state = core_state;
2214 int arm7_9_checksum_memory(struct target_s *target, u32 address, u32 count, u32* checksum)
2216 working_area_t *crc_algorithm;
2217 armv4_5_algorithm_t armv4_5_info;
2218 reg_param_t reg_params[2];
2221 u32 arm7_9_crc_code[] = {
2222 0xE1A02000, /* mov r2, r0 */
2223 0xE3E00000, /* mov r0, #0xffffffff */
2224 0xE1A03001, /* mov r3, r1 */
2225 0xE3A04000, /* mov r4, #0 */
2226 0xEA00000B, /* b ncomp */
2228 0xE7D21004, /* ldrb r1, [r2, r4] */
2229 0xE59F7030, /* ldr r7, CRC32XOR */
2230 0xE0200C01, /* eor r0, r0, r1, asl 24 */
2231 0xE3A05000, /* mov r5, #0 */
2233 0xE3500000, /* cmp r0, #0 */
2234 0xE1A06080, /* mov r6, r0, asl #1 */
2235 0xE2855001, /* add r5, r5, #1 */
2236 0xE1A00006, /* mov r0, r6 */
2237 0xB0260007, /* eorlt r0, r6, r7 */
2238 0xE3550008, /* cmp r5, #8 */
2239 0x1AFFFFF8, /* bne loop */
2240 0xE2844001, /* add r4, r4, #1 */
2242 0xE1540003, /* cmp r4, r3 */
2243 0x1AFFFFF1, /* bne nbyte */
2245 0xEAFFFFFE, /* b end */
2246 0x04C11DB7 /* CRC32XOR: .word 0x04C11DB7 */
2251 if (target_alloc_working_area(target, sizeof(arm7_9_crc_code), &crc_algorithm) != ERROR_OK)
2253 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2256 /* convert flash writing code into a buffer in target endianness */
2257 for (i = 0; i < (sizeof(arm7_9_crc_code)/sizeof(u32)); i++)
2258 target_write_u32(target, crc_algorithm->address + i*sizeof(u32), arm7_9_crc_code[i]);
2260 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
2261 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
2262 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
2264 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT);
2265 init_reg_param(®_params[1], "r1", 32, PARAM_OUT);
2267 buf_set_u32(reg_params[0].value, 0, 32, address);
2268 buf_set_u32(reg_params[1].value, 0, 32, count);
2270 if ((retval = target->type->run_algorithm(target, 0, NULL, 2, reg_params,
2271 crc_algorithm->address, crc_algorithm->address + (sizeof(arm7_9_crc_code) - 8), 20000, &armv4_5_info)) != ERROR_OK)
2273 LOG_ERROR("error executing arm7_9 crc algorithm");
2274 destroy_reg_param(®_params[0]);
2275 destroy_reg_param(®_params[1]);
2276 target_free_working_area(target, crc_algorithm);
2280 *checksum = buf_get_u32(reg_params[0].value, 0, 32);
2282 destroy_reg_param(®_params[0]);
2283 destroy_reg_param(®_params[1]);
2285 target_free_working_area(target, crc_algorithm);
2290 int arm7_9_register_commands(struct command_context_s *cmd_ctx)
2292 command_t *arm7_9_cmd;
2294 arm7_9_cmd = register_command(cmd_ctx, NULL, "arm7_9", NULL, COMMAND_ANY, "arm7/9 specific commands");
2296 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>");
2297 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>");
2299 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>");
2301 register_command(cmd_ctx, arm7_9_cmd, "sw_bkpts", handle_arm7_9_sw_bkpts_command, COMMAND_EXEC, "support for software breakpoints <enable|disable>");
2302 register_command(cmd_ctx, arm7_9_cmd, "force_hw_bkpts", handle_arm7_9_force_hw_bkpts_command, COMMAND_EXEC, "use hardware breakpoints for all breakpoints (disables sw breakpoint support) <enable|disable>");
2303 register_command(cmd_ctx, arm7_9_cmd, "dbgrq", handle_arm7_9_dbgrq_command,
2304 COMMAND_ANY, "use EmbeddedICE dbgrq instead of breakpoint for target halt requests <enable|disable>");
2305 register_command(cmd_ctx, arm7_9_cmd, "fast_writes", handle_arm7_9_fast_memory_access_command,
2306 COMMAND_ANY, "(deprecated, see: arm7_9 fast_memory_access)");
2307 register_command(cmd_ctx, arm7_9_cmd, "fast_memory_access", handle_arm7_9_fast_memory_access_command,
2308 COMMAND_ANY, "use fast memory accesses instead of slower but potentially unsafe slow accesses <enable|disable>");
2309 register_command(cmd_ctx, arm7_9_cmd, "dcc_downloads", handle_arm7_9_dcc_downloads_command,
2310 COMMAND_ANY, "use DCC downloads for larger memory writes <enable|disable>");
2312 armv4_5_register_commands(cmd_ctx);
2314 etm_register_commands(cmd_ctx);
2319 int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2324 target_t *target = get_current_target(cmd_ctx);
2325 armv4_5_common_t *armv4_5;
2326 arm7_9_common_t *arm7_9;
2328 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2330 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2334 if (target->state != TARGET_HALTED)
2336 command_print(cmd_ctx, "can't write registers while running");
2342 command_print(cmd_ctx, "usage: write_xpsr <value> <not cpsr|spsr>");
2346 value = strtoul(args[0], NULL, 0);
2347 spsr = strtol(args[1], NULL, 0);
2349 /* if we're writing the CPSR, mask the T bit */
2353 arm7_9->write_xpsr(target, value, spsr);
2354 if ((retval = jtag_execute_queue()) != ERROR_OK)
2356 LOG_ERROR("JTAG error while writing to xpsr");
2363 int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2369 target_t *target = get_current_target(cmd_ctx);
2370 armv4_5_common_t *armv4_5;
2371 arm7_9_common_t *arm7_9;
2373 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2375 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2379 if (target->state != TARGET_HALTED)
2381 command_print(cmd_ctx, "can't write registers while running");
2387 command_print(cmd_ctx, "usage: write_xpsr_im8 <im8> <rotate> <not cpsr|spsr>");
2391 value = strtoul(args[0], NULL, 0);
2392 rotate = strtol(args[1], NULL, 0);
2393 spsr = strtol(args[2], NULL, 0);
2395 arm7_9->write_xpsr_im8(target, value, rotate, spsr);
2396 if ((retval = jtag_execute_queue()) != ERROR_OK)
2398 LOG_ERROR("JTAG error while writing 8-bit immediate to xpsr");
2405 int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2410 target_t *target = get_current_target(cmd_ctx);
2411 armv4_5_common_t *armv4_5;
2412 arm7_9_common_t *arm7_9;
2414 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2416 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2420 if (target->state != TARGET_HALTED)
2422 command_print(cmd_ctx, "can't write registers while running");
2428 command_print(cmd_ctx, "usage: write_core_reg <num> <mode> <value>");
2432 num = strtol(args[0], NULL, 0);
2433 mode = strtoul(args[1], NULL, 0);
2434 value = strtoul(args[2], NULL, 0);
2436 arm7_9_write_core_reg(target, num, mode, value);
2441 int handle_arm7_9_sw_bkpts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2443 target_t *target = get_current_target(cmd_ctx);
2444 armv4_5_common_t *armv4_5;
2445 arm7_9_common_t *arm7_9;
2447 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2449 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2455 command_print(cmd_ctx, "software breakpoints %s", (arm7_9->sw_bkpts_enabled) ? "enabled" : "disabled");
2459 if (strcmp("enable", args[0]) == 0)
2461 if (arm7_9->sw_bkpts_use_wp)
2463 arm7_9_enable_sw_bkpts(target);
2467 arm7_9->sw_bkpts_enabled = 1;
2470 else if (strcmp("disable", args[0]) == 0)
2472 if (arm7_9->sw_bkpts_use_wp)
2474 arm7_9_disable_sw_bkpts(target);
2478 arm7_9->sw_bkpts_enabled = 0;
2483 command_print(cmd_ctx, "usage: arm7_9 sw_bkpts <enable|disable>");
2486 command_print(cmd_ctx, "software breakpoints %s", (arm7_9->sw_bkpts_enabled) ? "enabled" : "disabled");
2491 int handle_arm7_9_force_hw_bkpts_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2493 target_t *target = get_current_target(cmd_ctx);
2494 armv4_5_common_t *armv4_5;
2495 arm7_9_common_t *arm7_9;
2497 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2499 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2503 if ((argc >= 1) && (strcmp("enable", args[0]) == 0))
2505 arm7_9->force_hw_bkpts = 1;
2506 if (arm7_9->sw_bkpts_use_wp)
2508 arm7_9_disable_sw_bkpts(target);
2511 else if ((argc >= 1) && (strcmp("disable", args[0]) == 0))
2513 arm7_9->force_hw_bkpts = 0;
2517 command_print(cmd_ctx, "usage: arm7_9 force_hw_bkpts <enable|disable>");
2520 command_print(cmd_ctx, "force hardware breakpoints %s", (arm7_9->force_hw_bkpts) ? "enabled" : "disabled");
2525 int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2527 target_t *target = get_current_target(cmd_ctx);
2528 armv4_5_common_t *armv4_5;
2529 arm7_9_common_t *arm7_9;
2531 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2533 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2539 if (strcmp("enable", args[0]) == 0)
2541 arm7_9->use_dbgrq = 1;
2543 else if (strcmp("disable", args[0]) == 0)
2545 arm7_9->use_dbgrq = 0;
2549 command_print(cmd_ctx, "usage: arm7_9 dbgrq <enable|disable>");
2553 command_print(cmd_ctx, "use of EmbeddedICE dbgrq instead of breakpoint for target halt %s", (arm7_9->use_dbgrq) ? "enabled" : "disabled");
2558 int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2560 target_t *target = get_current_target(cmd_ctx);
2561 armv4_5_common_t *armv4_5;
2562 arm7_9_common_t *arm7_9;
2564 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2566 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2572 if (strcmp("enable", args[0]) == 0)
2574 arm7_9->fast_memory_access = 1;
2576 else if (strcmp("disable", args[0]) == 0)
2578 arm7_9->fast_memory_access = 0;
2582 command_print(cmd_ctx, "usage: arm7_9 fast_memory_access <enable|disable>");
2586 command_print(cmd_ctx, "fast memory access is %s", (arm7_9->fast_memory_access) ? "enabled" : "disabled");
2591 int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2593 target_t *target = get_current_target(cmd_ctx);
2594 armv4_5_common_t *armv4_5;
2595 arm7_9_common_t *arm7_9;
2597 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2599 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2605 if (strcmp("enable", args[0]) == 0)
2607 arm7_9->dcc_downloads = 1;
2609 else if (strcmp("disable", args[0]) == 0)
2611 arm7_9->dcc_downloads = 0;
2615 command_print(cmd_ctx, "usage: arm7_9 dcc_downloads <enable|disable>");
2619 command_print(cmd_ctx, "dcc downloads are %s", (arm7_9->dcc_downloads) ? "enabled" : "disabled");
2624 int arm7_9_init_arch_info(target_t *target, arm7_9_common_t *arm7_9)
2626 armv4_5_common_t *armv4_5 = &arm7_9->armv4_5_common;
2628 arm7_9->common_magic = ARM7_9_COMMON_MAGIC;
2630 arm_jtag_setup_connection(&arm7_9->jtag_info);
2631 arm7_9->wp_available = 2;
2632 arm7_9->wp0_used = 0;
2633 arm7_9->wp1_used = 0;
2634 arm7_9->force_hw_bkpts = 0;
2635 arm7_9->use_dbgrq = 0;
2637 arm7_9->etm_ctx = NULL;
2638 arm7_9->has_single_step = 0;
2639 arm7_9->has_monitor_mode = 0;
2640 arm7_9->has_vector_catch = 0;
2642 arm7_9->reinit_embeddedice = 0;
2644 arm7_9->debug_entry_from_reset = 0;
2646 arm7_9->dcc_working_area = NULL;
2648 arm7_9->fast_memory_access = 0;
2649 arm7_9->dcc_downloads = 0;
2651 jtag_register_event_callback(arm7_9_jtag_callback, target);
2653 armv4_5->arch_info = arm7_9;
2654 armv4_5->read_core_reg = arm7_9_read_core_reg;
2655 armv4_5->write_core_reg = arm7_9_write_core_reg;
2656 armv4_5->full_context = arm7_9_full_context;
2658 armv4_5_init_arch_info(target, armv4_5);
2660 target_register_timer_callback(arm7_9_handle_target_request, 1, 1, target);