ETM cleanup
[openocd.git] / src / target / arm7_9_common.c
blobb6606a17de748465eb437d7367c04e17aed82cbe
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * Copyright (C) 2008 by Hongtao Zheng *
12 * hontor@126.com *
13 * *
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. *
18 * *
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. *
23 * *
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 ***************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
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 /**
43 * Clear watchpoints for an ARM7/9 target.
45 * @param arm7_9 Pointer to the common struct for an ARM7/9 target
46 * @return JTAG error status after executing queue
48 static int arm7_9_clear_watchpoints(arm7_9_common_t *arm7_9)
50 LOG_DEBUG("-");
51 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
52 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
53 arm7_9->sw_breakpoint_count = 0;
54 arm7_9->sw_breakpoints_added = 0;
55 arm7_9->wp0_used = 0;
56 arm7_9->wp1_used = arm7_9->wp1_used_default;
57 arm7_9->wp_available = arm7_9->wp_available_max;
59 return jtag_execute_queue();
62 /**
63 * Assign a watchpoint to one of the two available hardware comparators in an
64 * ARM7 or ARM9 target.
66 * @param arm7_9 Pointer to the common struct for an ARM7/9 target
67 * @param breakpoint Pointer to the breakpoint to be used as a watchpoint
69 static void arm7_9_assign_wp(arm7_9_common_t *arm7_9, breakpoint_t *breakpoint)
71 if (!arm7_9->wp0_used)
73 arm7_9->wp0_used = 1;
74 breakpoint->set = 1;
75 arm7_9->wp_available--;
77 else if (!arm7_9->wp1_used)
79 arm7_9->wp1_used = 1;
80 breakpoint->set = 2;
81 arm7_9->wp_available--;
83 else
85 LOG_ERROR("BUG: no hardware comparator available");
87 LOG_DEBUG("BPID: %d (0x%08" PRIx32 ") using hw wp: %d",
88 breakpoint->unique_id,
89 breakpoint->address,
90 breakpoint->set );
93 /**
94 * Setup an ARM7/9 target's embedded ICE registers for software breakpoints.
96 * @param arm7_9 Pointer to common struct for ARM7/9 targets
97 * @return Error codes if there is a problem finding a watchpoint or the result
98 * of executing the JTAG queue
100 static int arm7_9_set_software_breakpoints(arm7_9_common_t *arm7_9)
102 if (arm7_9->sw_breakpoints_added)
104 return ERROR_OK;
106 if (arm7_9->wp_available < 1)
108 LOG_WARNING("can't enable sw breakpoints with no watchpoint unit available");
109 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
111 arm7_9->wp_available--;
113 /* pick a breakpoint unit */
114 if (!arm7_9->wp0_used)
116 arm7_9->sw_breakpoints_added = 1;
117 arm7_9->wp0_used = 3;
118 } else if (!arm7_9->wp1_used)
120 arm7_9->sw_breakpoints_added = 2;
121 arm7_9->wp1_used = 3;
123 else
125 LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
126 return ERROR_FAIL;
129 if (arm7_9->sw_breakpoints_added == 1)
131 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], arm7_9->arm_bkpt);
132 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0x0);
133 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffffu);
134 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
135 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
137 else if (arm7_9->sw_breakpoints_added == 2)
139 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], arm7_9->arm_bkpt);
140 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0x0);
141 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0xffffffffu);
142 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
143 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
145 else
147 LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
148 return ERROR_FAIL;
150 LOG_DEBUG("SW BP using hw wp: %d",
151 arm7_9->sw_breakpoints_added );
153 return jtag_execute_queue();
157 * Setup the common pieces for an ARM7/9 target after reset or on startup.
159 * @param target Pointer to an ARM7/9 target to setup
160 * @return Result of clearing the watchpoints on the target
162 int arm7_9_setup(target_t *target)
164 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
166 return arm7_9_clear_watchpoints(arm7_9);
170 * Retrieves the architecture information pointers for ARMv4/5 and ARM7/9
171 * targets. A return of ERROR_OK signifies that the target is a valid target
172 * and that the pointers have been set properly.
174 * @param target Pointer to the target device to get the pointers from
175 * @param armv4_5_p Pointer to be filled in with the common struct for ARMV4/5
176 * targets
177 * @param arm7_9_p Pointer to be filled in with the common struct for ARM7/9
178 * targets
179 * @return ERROR_OK if successful
181 int arm7_9_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p)
183 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
184 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
186 /* FIXME stop using this routine; just target_to_arm7_9() and
187 * verify the resulting pointer using a replacement routine
188 * that emits a usage message.
190 if (armv4_5->common_magic != ARMV4_5_COMMON_MAGIC)
191 return ERROR_TARGET_INVALID;
193 if (arm7_9->common_magic != ARM7_9_COMMON_MAGIC)
194 return ERROR_TARGET_INVALID;
196 *armv4_5_p = armv4_5;
197 *arm7_9_p = arm7_9;
199 return ERROR_OK;
203 * Set either a hardware or software breakpoint on an ARM7/9 target. The
204 * breakpoint is set up even if it is already set. Some actions, e.g. reset,
205 * might have erased the values in Embedded ICE.
207 * @param target Pointer to the target device to set the breakpoints on
208 * @param breakpoint Pointer to the breakpoint to be set
209 * @return For hardware breakpoints, this is the result of executing the JTAG
210 * queue. For software breakpoints, this will be the status of the
211 * required memory reads and writes
213 int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
215 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
216 int retval = ERROR_OK;
218 LOG_DEBUG("BPID: %d, Address: 0x%08" PRIx32 ", Type: %d" ,
219 breakpoint->unique_id,
220 breakpoint->address,
221 breakpoint->type);
223 if (target->state != TARGET_HALTED)
225 LOG_WARNING("target not halted");
226 return ERROR_TARGET_NOT_HALTED;
229 if (breakpoint->type == BKPT_HARD)
231 /* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
232 uint32_t mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
234 /* reassign a hw breakpoint */
235 if (breakpoint->set == 0)
237 arm7_9_assign_wp(arm7_9, breakpoint);
240 if (breakpoint->set == 1)
242 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], breakpoint->address);
243 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
244 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffffu);
245 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
246 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
248 else if (breakpoint->set == 2)
250 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], breakpoint->address);
251 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
252 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffffu);
253 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
254 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
256 else
258 LOG_ERROR("BUG: no hardware comparator available");
259 return ERROR_OK;
262 retval = jtag_execute_queue();
264 else if (breakpoint->type == BKPT_SOFT)
266 /* did we already set this breakpoint? */
267 if (breakpoint->set)
268 return ERROR_OK;
270 if (breakpoint->length == 4)
272 uint32_t verify = 0xffffffff;
273 /* keep the original instruction in target endianness */
274 if ((retval = target_read_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
276 return retval;
278 /* write the breakpoint instruction in target endianness (arm7_9->arm_bkpt is host endian) */
279 if ((retval = target_write_u32(target, breakpoint->address, arm7_9->arm_bkpt)) != ERROR_OK)
281 return retval;
284 if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
286 return retval;
288 if (verify != arm7_9->arm_bkpt)
290 LOG_ERROR("Unable to set 32 bit software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
291 return ERROR_OK;
294 else
296 uint16_t verify = 0xffff;
297 /* keep the original instruction in target endianness */
298 if ((retval = target_read_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
300 return retval;
302 /* write the breakpoint instruction in target endianness (arm7_9->thumb_bkpt is host endian) */
303 if ((retval = target_write_u16(target, breakpoint->address, arm7_9->thumb_bkpt)) != ERROR_OK)
305 return retval;
308 if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
310 return retval;
312 if (verify != arm7_9->thumb_bkpt)
314 LOG_ERROR("Unable to set thumb software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
315 return ERROR_OK;
319 if ((retval = arm7_9_set_software_breakpoints(arm7_9)) != ERROR_OK)
320 return retval;
322 arm7_9->sw_breakpoint_count++;
324 breakpoint->set = 1;
327 return retval;
331 * Unsets an existing breakpoint on an ARM7/9 target. If it is a hardware
332 * breakpoint, the watchpoint used will be freed and the Embedded ICE registers
333 * will be updated. Otherwise, the software breakpoint will be restored to its
334 * original instruction if it hasn't already been modified.
336 * @param target Pointer to ARM7/9 target to unset the breakpoint from
337 * @param breakpoint Pointer to breakpoint to be unset
338 * @return For hardware breakpoints, this is the result of executing the JTAG
339 * queue. For software breakpoints, this will be the status of the
340 * required memory reads and writes
342 int arm7_9_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
344 int retval = ERROR_OK;
345 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
347 LOG_DEBUG("BPID: %d, Address: 0x%08" PRIx32,
348 breakpoint->unique_id,
349 breakpoint->address );
351 if (!breakpoint->set)
353 LOG_WARNING("breakpoint not set");
354 return ERROR_OK;
357 if (breakpoint->type == BKPT_HARD)
359 LOG_DEBUG("BPID: %d Releasing hw wp: %d",
360 breakpoint->unique_id,
361 breakpoint->set );
362 if (breakpoint->set == 1)
364 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
365 arm7_9->wp0_used = 0;
366 arm7_9->wp_available++;
368 else if (breakpoint->set == 2)
370 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
371 arm7_9->wp1_used = 0;
372 arm7_9->wp_available++;
374 retval = jtag_execute_queue();
375 breakpoint->set = 0;
377 else
379 /* restore original instruction (kept in target endianness) */
380 if (breakpoint->length == 4)
382 uint32_t current_instr;
383 /* check that user program as not modified breakpoint instruction */
384 if ((retval = target_read_memory(target, breakpoint->address, 4, 1, (uint8_t*)&current_instr)) != ERROR_OK)
386 return retval;
388 if (current_instr == arm7_9->arm_bkpt)
389 if ((retval = target_write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
391 return retval;
394 else
396 uint16_t current_instr;
397 /* check that user program as not modified breakpoint instruction */
398 if ((retval = target_read_memory(target, breakpoint->address, 2, 1, (uint8_t*)&current_instr)) != ERROR_OK)
400 return retval;
402 if (current_instr == arm7_9->thumb_bkpt)
403 if ((retval = target_write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
405 return retval;
409 if (--arm7_9->sw_breakpoint_count==0)
411 /* We have removed the last sw breakpoint, clear the hw breakpoint we used to implement it */
412 if (arm7_9->sw_breakpoints_added == 1)
414 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0);
416 else if (arm7_9->sw_breakpoints_added == 2)
418 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0);
422 breakpoint->set = 0;
425 return retval;
429 * Add a breakpoint to an ARM7/9 target. This makes sure that there are no
430 * dangling breakpoints and that the desired breakpoint can be added.
432 * @param target Pointer to the target ARM7/9 device to add a breakpoint to
433 * @param breakpoint Pointer to the breakpoint to be added
434 * @return An error status if there is a problem adding the breakpoint or the
435 * result of setting the breakpoint
437 int arm7_9_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
439 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
441 if (target->state != TARGET_HALTED)
443 LOG_WARNING("target not halted");
444 return ERROR_TARGET_NOT_HALTED;
447 if (arm7_9->breakpoint_count == 0)
449 /* make sure we don't have any dangling breakpoints. This is vital upon
450 * GDB connect/disconnect
452 arm7_9_clear_watchpoints(arm7_9);
455 if ((breakpoint->type == BKPT_HARD) && (arm7_9->wp_available < 1))
457 LOG_INFO("no watchpoint unit available for hardware breakpoint");
458 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
461 if ((breakpoint->length != 2) && (breakpoint->length != 4))
463 LOG_INFO("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
464 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
467 if (breakpoint->type == BKPT_HARD)
469 arm7_9_assign_wp(arm7_9, breakpoint);
472 arm7_9->breakpoint_count++;
474 return arm7_9_set_breakpoint(target, breakpoint);
478 * Removes a breakpoint from an ARM7/9 target. This will make sure there are no
479 * dangling breakpoints and updates available watchpoints if it is a hardware
480 * breakpoint.
482 * @param target Pointer to the target to have a breakpoint removed
483 * @param breakpoint Pointer to the breakpoint to be removed
484 * @return Error status if there was a problem unsetting the breakpoint or the
485 * watchpoints could not be cleared
487 int arm7_9_remove_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
489 int retval = ERROR_OK;
490 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
492 if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
494 return retval;
497 if (breakpoint->type == BKPT_HARD)
498 arm7_9->wp_available++;
500 arm7_9->breakpoint_count--;
501 if (arm7_9->breakpoint_count == 0)
503 /* make sure we don't have any dangling breakpoints */
504 if ((retval = arm7_9_clear_watchpoints(arm7_9)) != ERROR_OK)
506 return retval;
510 return ERROR_OK;
514 * Sets a watchpoint for an ARM7/9 target in one of the watchpoint units. It is
515 * considered a bug to call this function when there are no available watchpoint
516 * units.
518 * @param target Pointer to an ARM7/9 target to set a watchpoint on
519 * @param watchpoint Pointer to the watchpoint to be set
520 * @return Error status if watchpoint set fails or the result of executing the
521 * JTAG queue
523 int arm7_9_set_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
525 int retval = ERROR_OK;
526 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
527 int rw_mask = 1;
528 uint32_t mask;
530 mask = watchpoint->length - 1;
532 if (target->state != TARGET_HALTED)
534 LOG_WARNING("target not halted");
535 return ERROR_TARGET_NOT_HALTED;
538 if (watchpoint->rw == WPT_ACCESS)
539 rw_mask = 0;
540 else
541 rw_mask = 1;
543 if (!arm7_9->wp0_used)
545 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], watchpoint->address);
546 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
547 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], watchpoint->mask);
548 if (watchpoint->mask != 0xffffffffu)
549 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], watchpoint->value);
550 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
551 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
553 if ((retval = jtag_execute_queue()) != ERROR_OK)
555 return retval;
557 watchpoint->set = 1;
558 arm7_9->wp0_used = 2;
560 else if (!arm7_9->wp1_used)
562 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], watchpoint->address);
563 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
564 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], watchpoint->mask);
565 if (watchpoint->mask != 0xffffffffu)
566 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], watchpoint->value);
567 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
568 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
570 if ((retval = jtag_execute_queue()) != ERROR_OK)
572 return retval;
574 watchpoint->set = 2;
575 arm7_9->wp1_used = 2;
577 else
579 LOG_ERROR("BUG: no hardware comparator available");
580 return ERROR_OK;
583 return ERROR_OK;
587 * Unset an existing watchpoint and clear the used watchpoint unit.
589 * @param target Pointer to the target to have the watchpoint removed
590 * @param watchpoint Pointer to the watchpoint to be removed
591 * @return Error status while trying to unset the watchpoint or the result of
592 * executing the JTAG queue
594 int arm7_9_unset_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
596 int retval = ERROR_OK;
597 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
599 if (target->state != TARGET_HALTED)
601 LOG_WARNING("target not halted");
602 return ERROR_TARGET_NOT_HALTED;
605 if (!watchpoint->set)
607 LOG_WARNING("breakpoint not set");
608 return ERROR_OK;
611 if (watchpoint->set == 1)
613 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
614 if ((retval = jtag_execute_queue()) != ERROR_OK)
616 return retval;
618 arm7_9->wp0_used = 0;
620 else if (watchpoint->set == 2)
622 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
623 if ((retval = jtag_execute_queue()) != ERROR_OK)
625 return retval;
627 arm7_9->wp1_used = 0;
629 watchpoint->set = 0;
631 return ERROR_OK;
635 * Add a watchpoint to an ARM7/9 target. If there are no watchpoint units
636 * available, an error response is returned.
638 * @param target Pointer to the ARM7/9 target to add a watchpoint to
639 * @param watchpoint Pointer to the watchpoint to be added
640 * @return Error status while trying to add the watchpoint
642 int arm7_9_add_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
644 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
646 if (target->state != TARGET_HALTED)
648 LOG_WARNING("target not halted");
649 return ERROR_TARGET_NOT_HALTED;
652 if (arm7_9->wp_available < 1)
654 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
657 if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
659 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
662 arm7_9->wp_available--;
664 return ERROR_OK;
668 * Remove a watchpoint from an ARM7/9 target. The watchpoint will be unset and
669 * the used watchpoint unit will be reopened.
671 * @param target Pointer to the target to remove a watchpoint from
672 * @param watchpoint Pointer to the watchpoint to be removed
673 * @return Result of trying to unset the watchpoint
675 int arm7_9_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
677 int retval = ERROR_OK;
678 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
680 if (watchpoint->set)
682 if ((retval = arm7_9_unset_watchpoint(target, watchpoint)) != ERROR_OK)
684 return retval;
688 arm7_9->wp_available++;
690 return ERROR_OK;
694 * Restarts the target by sending a RESTART instruction and moving the JTAG
695 * state to IDLE. This includes a timeout waiting for DBGACK and SYSCOMP to be
696 * asserted by the processor.
698 * @param target Pointer to target to issue commands to
699 * @return Error status if there is a timeout or a problem while executing the
700 * JTAG queue
702 int arm7_9_execute_sys_speed(struct target_s *target)
704 int retval;
705 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
706 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
707 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
709 /* set RESTART instruction */
710 jtag_set_end_state(TAP_IDLE);
711 if (arm7_9->need_bypass_before_restart) {
712 arm7_9->need_bypass_before_restart = 0;
713 arm_jtag_set_instr(jtag_info, 0xf, NULL);
715 arm_jtag_set_instr(jtag_info, 0x4, NULL);
717 long long then = timeval_ms();
718 int timeout;
719 while (!(timeout = ((timeval_ms()-then) > 1000)))
721 /* read debug status register */
722 embeddedice_read_reg(dbg_stat);
723 if ((retval = jtag_execute_queue()) != ERROR_OK)
724 return retval;
725 if ((buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
726 && (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_SYSCOMP, 1)))
727 break;
728 if (debug_level >= 3)
730 alive_sleep(100);
731 } else
733 keep_alive();
736 if (timeout)
738 LOG_ERROR("timeout waiting for SYSCOMP & DBGACK, last DBG_STATUS: %" PRIx32 "", buf_get_u32(dbg_stat->value, 0, dbg_stat->size));
739 return ERROR_TARGET_TIMEOUT;
742 return ERROR_OK;
746 * Restarts the target by sending a RESTART instruction and moving the JTAG
747 * state to IDLE. This validates that DBGACK and SYSCOMP are set without
748 * waiting until they are.
750 * @param target Pointer to the target to issue commands to
751 * @return Always ERROR_OK
753 int arm7_9_execute_fast_sys_speed(struct target_s *target)
755 static int set = 0;
756 static uint8_t check_value[4], check_mask[4];
758 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
759 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
760 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
762 /* set RESTART instruction */
763 jtag_set_end_state(TAP_IDLE);
764 if (arm7_9->need_bypass_before_restart) {
765 arm7_9->need_bypass_before_restart = 0;
766 arm_jtag_set_instr(jtag_info, 0xf, NULL);
768 arm_jtag_set_instr(jtag_info, 0x4, NULL);
770 if (!set)
772 /* check for DBGACK and SYSCOMP set (others don't care) */
774 /* NB! These are constants that must be available until after next jtag_execute() and
775 * we evaluate the values upon first execution in lieu of setting up these constants
776 * during early setup.
777 * */
778 buf_set_u32(check_value, 0, 32, 0x9);
779 buf_set_u32(check_mask, 0, 32, 0x9);
780 set = 1;
783 /* read debug status register */
784 embeddedice_read_reg_w_check(dbg_stat, check_value, check_mask);
786 return ERROR_OK;
790 * Get some data from the ARM7/9 target.
792 * @param target Pointer to the ARM7/9 target to read data from
793 * @param size The number of 32bit words to be read
794 * @param buffer Pointer to the buffer that will hold the data
795 * @return The result of receiving data from the Embedded ICE unit
797 int arm7_9_target_request_data(target_t *target, uint32_t size, uint8_t *buffer)
799 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
800 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
801 uint32_t *data;
802 int retval = ERROR_OK;
803 uint32_t i;
805 data = malloc(size * (sizeof(uint32_t)));
807 retval = embeddedice_receive(jtag_info, data, size);
809 /* return the 32-bit ints in the 8-bit array */
810 for (i = 0; i < size; i++)
812 h_u32_to_le(buffer + (i * 4), data[i]);
815 free(data);
817 return retval;
821 * Handles requests to an ARM7/9 target. If debug messaging is enabled, the
822 * target is running and the DCC control register has the W bit high, this will
823 * execute the request on the target.
825 * @param priv Void pointer expected to be a target_t pointer
826 * @return ERROR_OK unless there are issues with the JTAG queue or when reading
827 * from the Embedded ICE unit
829 int arm7_9_handle_target_request(void *priv)
831 int retval = ERROR_OK;
832 target_t *target = priv;
833 if (!target_was_examined(target))
834 return ERROR_OK;
835 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
836 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
837 reg_t *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
839 if (!target->dbg_msg_enabled)
840 return ERROR_OK;
842 if (target->state == TARGET_RUNNING)
844 /* read DCC control register */
845 embeddedice_read_reg(dcc_control);
846 if ((retval = jtag_execute_queue()) != ERROR_OK)
848 return retval;
851 /* check W bit */
852 if (buf_get_u32(dcc_control->value, 1, 1) == 1)
854 uint32_t request;
856 if ((retval = embeddedice_receive(jtag_info, &request, 1)) != ERROR_OK)
858 return retval;
860 if ((retval = target_request(target, request)) != ERROR_OK)
862 return retval;
867 return ERROR_OK;
871 * Polls an ARM7/9 target for its current status. If DBGACK is set, the target
872 * is manipulated to the right halted state based on its current state. This is
873 * what happens:
875 * <table>
876 * <tr><th > State</th><th > Action</th></tr>
877 * <tr><td > TARGET_RUNNING | TARGET_RESET</td><td > Enters debug mode. If TARGET_RESET, pc may be checked</td></tr>
878 * <tr><td > TARGET_UNKNOWN</td><td > Warning is logged</td></tr>
879 * <tr><td > TARGET_DEBUG_RUNNING</td><td > Enters debug mode</td></tr>
880 * <tr><td > TARGET_HALTED</td><td > Nothing</td></tr>
881 * </table>
883 * If the target does not end up in the halted state, a warning is produced. If
884 * DBGACK is cleared, then the target is expected to either be running or
885 * running in debug.
887 * @param target Pointer to the ARM7/9 target to poll
888 * @return ERROR_OK or an error status if a command fails
890 int arm7_9_poll(target_t *target)
892 int retval;
893 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
894 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
896 /* read debug status register */
897 embeddedice_read_reg(dbg_stat);
898 if ((retval = jtag_execute_queue()) != ERROR_OK)
900 return retval;
903 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
905 /* LOG_DEBUG("DBGACK set, dbg_state->value: 0x%x", buf_get_u32(dbg_stat->value, 0, 32));*/
906 if (target->state == TARGET_UNKNOWN)
908 /* Starting OpenOCD with target in debug-halt */
909 target->state = TARGET_RUNNING;
910 LOG_DEBUG("DBGACK already set during server startup.");
912 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
914 int check_pc = 0;
915 if (target->state == TARGET_RESET)
917 if (target->reset_halt)
919 enum reset_types jtag_reset_config = jtag_get_reset_config();
920 if ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
922 check_pc = 1;
927 target->state = TARGET_HALTED;
929 if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
930 return retval;
932 if (check_pc)
934 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
935 uint32_t t=*((uint32_t *)reg->value);
936 if (t != 0)
938 LOG_ERROR("PC was not 0. Does this target need srst_pulls_trst?");
942 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
944 return retval;
947 if (target->state == TARGET_DEBUG_RUNNING)
949 target->state = TARGET_HALTED;
950 if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
951 return retval;
953 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED)) != ERROR_OK)
955 return retval;
958 if (target->state != TARGET_HALTED)
960 LOG_WARNING("DBGACK set, but the target did not end up in the halted state %d", target->state);
963 else
965 if (target->state != TARGET_DEBUG_RUNNING)
966 target->state = TARGET_RUNNING;
969 return ERROR_OK;
973 * Asserts the reset (SRST) on an ARM7/9 target. Some -S targets (ARM966E-S in
974 * the STR912 isn't affected, ARM926EJ-S in the LPC3180 and AT91SAM9260 is
975 * affected) completely stop the JTAG clock while the core is held in reset
976 * (SRST). It isn't possible to program the halt condition once reset is
977 * asserted, hence a hook that allows the target to set up its reset-halt
978 * condition is setup prior to asserting reset.
980 * @param target Pointer to an ARM7/9 target to assert reset on
981 * @return ERROR_FAIL if the JTAG device does not have SRST, otherwise ERROR_OK
983 int arm7_9_assert_reset(target_t *target)
985 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
987 LOG_DEBUG("target->state: %s",
988 target_state_name(target));
990 enum reset_types jtag_reset_config = jtag_get_reset_config();
991 if (!(jtag_reset_config & RESET_HAS_SRST))
993 LOG_ERROR("Can't assert SRST");
994 return ERROR_FAIL;
997 /* At this point trst has been asserted/deasserted once. We would
998 * like to program EmbeddedICE while SRST is asserted, instead of
999 * depending on SRST to leave that module alone. However, many CPUs
1000 * gate the JTAG clock while SRST is asserted; or JTAG may need
1001 * clock stability guarantees (adaptive clocking might help).
1003 * So we assume JTAG access during SRST is off the menu unless it's
1004 * been specifically enabled.
1006 bool srst_asserted = false;
1008 if (((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0)
1009 && (jtag_reset_config & RESET_SRST_NO_GATING))
1011 jtag_add_reset(0, 1);
1012 srst_asserted = true;
1015 if (target->reset_halt)
1018 * Some targets do not support communication while SRST is asserted. We need to
1019 * set up the reset vector catch here.
1021 * If TRST is asserted, then these settings will be reset anyway, so setting them
1022 * here is harmless.
1024 if (arm7_9->has_vector_catch)
1026 /* program vector catch register to catch reset vector */
1027 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH], 0x1);
1029 /* extra runtest added as issues were found with certain ARM9 cores (maybe more) - AT91SAM9260 and STR9 */
1030 jtag_add_runtest(1, jtag_get_end_state());
1032 else
1034 /* program watchpoint unit to match on reset vector address */
1035 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], 0x0);
1036 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0x3);
1037 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1038 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1039 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1043 /* here we should issue an SRST only, but we may have to assert TRST as well */
1044 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1046 jtag_add_reset(1, 1);
1047 } else if (!srst_asserted)
1049 jtag_add_reset(0, 1);
1052 target->state = TARGET_RESET;
1053 jtag_add_sleep(50000);
1055 armv4_5_invalidate_core_regs(target);
1057 if ((target->reset_halt) && ((jtag_reset_config & RESET_SRST_PULLS_TRST) == 0))
1059 /* debug entry was already prepared in arm7_9_assert_reset() */
1060 target->debug_reason = DBG_REASON_DBGRQ;
1063 return ERROR_OK;
1067 * Deassert the reset (SRST) signal on an ARM7/9 target. If SRST pulls TRST
1068 * and the target is being reset into a halt, a warning will be triggered
1069 * because it is not possible to reset into a halted mode in this case. The
1070 * target is halted using the target's functions.
1072 * @param target Pointer to the target to have the reset deasserted
1073 * @return ERROR_OK or an error from polling or halting the target
1075 int arm7_9_deassert_reset(target_t *target)
1077 int retval = ERROR_OK;
1078 LOG_DEBUG("target->state: %s",
1079 target_state_name(target));
1081 /* deassert reset lines */
1082 jtag_add_reset(0, 0);
1084 enum reset_types jtag_reset_config = jtag_get_reset_config();
1085 if (target->reset_halt && (jtag_reset_config & RESET_SRST_PULLS_TRST) != 0)
1087 LOG_WARNING("srst pulls trst - can not reset into halted mode. Issuing halt after reset.");
1088 /* set up embedded ice registers again */
1089 if ((retval = target_examine_one(target)) != ERROR_OK)
1090 return retval;
1092 if ((retval = target_poll(target)) != ERROR_OK)
1094 return retval;
1097 if ((retval = target_halt(target)) != ERROR_OK)
1099 return retval;
1103 return retval;
1107 * Clears the halt condition for an ARM7/9 target. If it isn't coming out of
1108 * reset and if DBGRQ is used, it is progammed to be deasserted. If the reset
1109 * vector catch was used, it is restored. Otherwise, the control value is
1110 * restored and the watchpoint unit is restored if it was in use.
1112 * @param target Pointer to the ARM7/9 target to have halt cleared
1113 * @return Always ERROR_OK
1115 int arm7_9_clear_halt(target_t *target)
1117 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1118 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1120 /* we used DBGRQ only if we didn't come out of reset */
1121 if (!arm7_9->debug_entry_from_reset && arm7_9->use_dbgrq)
1123 /* program EmbeddedICE Debug Control Register to deassert DBGRQ
1125 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1126 embeddedice_store_reg(dbg_ctrl);
1128 else
1130 if (arm7_9->debug_entry_from_reset && arm7_9->has_vector_catch)
1132 /* if we came out of reset, and vector catch is supported, we used
1133 * vector catch to enter debug state
1134 * restore the register in that case
1136 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH]);
1138 else
1140 /* restore registers if watchpoint unit 0 was in use
1142 if (arm7_9->wp0_used)
1144 if (arm7_9->debug_entry_from_reset)
1146 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE]);
1148 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
1149 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
1150 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
1152 /* control value always has to be restored, as it was either disabled,
1153 * or enabled with possibly different bits
1155 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
1159 return ERROR_OK;
1163 * Issue a software reset and halt to an ARM7/9 target. The target is halted
1164 * and then there is a wait until the processor shows the halt. This wait can
1165 * timeout and results in an error being returned. The software reset involves
1166 * clearing the halt, updating the debug control register, changing to ARM mode,
1167 * reset of the program counter, and reset of all of the registers.
1169 * @param target Pointer to the ARM7/9 target to be reset and halted by software
1170 * @return Error status if any of the commands fail, otherwise ERROR_OK
1172 int arm7_9_soft_reset_halt(struct target_s *target)
1174 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1175 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1176 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
1177 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1178 int i;
1179 int retval;
1181 /* FIX!!! replace some of this code with tcl commands
1183 * halt # the halt command is synchronous
1184 * armv4_5 core_state arm
1188 if ((retval = target_halt(target)) != ERROR_OK)
1189 return retval;
1191 long long then = timeval_ms();
1192 int timeout;
1193 while (!(timeout = ((timeval_ms()-then) > 1000)))
1195 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
1196 break;
1197 embeddedice_read_reg(dbg_stat);
1198 if ((retval = jtag_execute_queue()) != ERROR_OK)
1199 return retval;
1200 if (debug_level >= 3)
1202 alive_sleep(100);
1203 } else
1205 keep_alive();
1208 if (timeout)
1210 LOG_ERROR("Failed to halt CPU after 1 sec");
1211 return ERROR_TARGET_TIMEOUT;
1213 target->state = TARGET_HALTED;
1215 /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
1216 * ensure that DBGRQ is cleared
1218 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
1219 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1220 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
1221 embeddedice_store_reg(dbg_ctrl);
1223 if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
1225 return retval;
1228 /* if the target is in Thumb state, change to ARM state */
1229 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
1231 uint32_t r0_thumb, pc_thumb;
1232 LOG_DEBUG("target entered debug from Thumb state, changing to ARM");
1233 /* Entered debug from Thumb mode */
1234 armv4_5->core_state = ARMV4_5_STATE_THUMB;
1235 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
1238 /* all register content is now invalid */
1239 if ((retval = armv4_5_invalidate_core_regs(target)) != ERROR_OK)
1241 return retval;
1244 /* SVC, ARM state, IRQ and FIQ disabled */
1245 buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8, 0xd3);
1246 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 1;
1247 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1249 /* start fetching from 0x0 */
1250 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, 0x0);
1251 armv4_5->core_cache->reg_list[15].dirty = 1;
1252 armv4_5->core_cache->reg_list[15].valid = 1;
1254 armv4_5->core_mode = ARMV4_5_MODE_SVC;
1255 armv4_5->core_state = ARMV4_5_STATE_ARM;
1257 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1258 return ERROR_FAIL;
1260 /* reset registers */
1261 for (i = 0; i <= 14; i++)
1263 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, 0xffffffff);
1264 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 1;
1265 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
1268 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
1270 return retval;
1273 return ERROR_OK;
1277 * Halt an ARM7/9 target. This is accomplished by either asserting the DBGRQ
1278 * line or by programming a watchpoint to trigger on any address. It is
1279 * considered a bug to call this function while the target is in the
1280 * TARGET_RESET state.
1282 * @param target Pointer to the ARM7/9 target to be halted
1283 * @return Always ERROR_OK
1285 int arm7_9_halt(target_t *target)
1287 if (target->state == TARGET_RESET)
1289 LOG_ERROR("BUG: arm7/9 does not support halt during reset. This is handled in arm7_9_assert_reset()");
1290 return ERROR_OK;
1293 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1294 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1296 LOG_DEBUG("target->state: %s",
1297 target_state_name(target));
1299 if (target->state == TARGET_HALTED)
1301 LOG_DEBUG("target was already halted");
1302 return ERROR_OK;
1305 if (target->state == TARGET_UNKNOWN)
1307 LOG_WARNING("target was in unknown state when halt was requested");
1310 if (arm7_9->use_dbgrq)
1312 /* program EmbeddedICE Debug Control Register to assert DBGRQ
1314 if (arm7_9->set_special_dbgrq) {
1315 arm7_9->set_special_dbgrq(target);
1316 } else {
1317 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 1);
1318 embeddedice_store_reg(dbg_ctrl);
1321 else
1323 /* program watchpoint unit to match on any address
1325 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1326 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1327 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1328 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1331 target->debug_reason = DBG_REASON_DBGRQ;
1333 return ERROR_OK;
1337 * Handle an ARM7/9 target's entry into debug mode. The halt is cleared on the
1338 * ARM. The JTAG queue is then executed and the reason for debug entry is
1339 * examined. Once done, the target is verified to be halted and the processor
1340 * is forced into ARM mode. The core registers are saved for the current core
1341 * mode and the program counter (register 15) is updated as needed. The core
1342 * registers and CPSR and SPSR are saved for restoration later.
1344 * @param target Pointer to target that is entering debug mode
1345 * @return Error code if anything fails, otherwise ERROR_OK
1347 int arm7_9_debug_entry(target_t *target)
1349 int i;
1350 uint32_t context[16];
1351 uint32_t* context_p[16];
1352 uint32_t r0_thumb, pc_thumb;
1353 uint32_t cpsr;
1354 int retval;
1355 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1356 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1357 reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
1358 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1360 #ifdef _DEBUG_ARM7_9_
1361 LOG_DEBUG("-");
1362 #endif
1364 /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
1365 * ensure that DBGRQ is cleared
1367 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
1368 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1369 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
1370 embeddedice_store_reg(dbg_ctrl);
1372 if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
1374 return retval;
1377 if ((retval = jtag_execute_queue()) != ERROR_OK)
1379 return retval;
1382 if ((retval = arm7_9->examine_debug_reason(target)) != ERROR_OK)
1383 return retval;
1386 if (target->state != TARGET_HALTED)
1388 LOG_WARNING("target not halted");
1389 return ERROR_TARGET_NOT_HALTED;
1392 /* if the target is in Thumb state, change to ARM state */
1393 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
1395 LOG_DEBUG("target entered debug from Thumb state");
1396 /* Entered debug from Thumb mode */
1397 armv4_5->core_state = ARMV4_5_STATE_THUMB;
1398 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
1399 LOG_DEBUG("r0_thumb: 0x%8.8" PRIx32 ", pc_thumb: 0x%8.8" PRIx32 "", r0_thumb, pc_thumb);
1401 else
1403 LOG_DEBUG("target entered debug from ARM state");
1404 /* Entered debug from ARM mode */
1405 armv4_5->core_state = ARMV4_5_STATE_ARM;
1408 for (i = 0; i < 16; i++)
1409 context_p[i] = &context[i];
1410 /* save core registers (r0 - r15 of current core mode) */
1411 arm7_9->read_core_regs(target, 0xffff, context_p);
1413 arm7_9->read_xpsr(target, &cpsr, 0);
1415 if ((retval = jtag_execute_queue()) != ERROR_OK)
1416 return retval;
1418 /* if the core has been executing in Thumb state, set the T bit */
1419 if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1420 cpsr |= 0x20;
1422 buf_set_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32, cpsr);
1423 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
1424 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1426 armv4_5->core_mode = cpsr & 0x1f;
1428 if (armv4_5_mode_to_number(armv4_5->core_mode) == -1)
1430 target->state = TARGET_UNKNOWN;
1431 LOG_ERROR("cpsr contains invalid mode value - communication failure");
1432 return ERROR_TARGET_FAILURE;
1435 LOG_DEBUG("target entered debug state in %s mode", armv4_5_mode_strings[armv4_5_mode_to_number(armv4_5->core_mode)]);
1437 if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1439 LOG_DEBUG("thumb state, applying fixups");
1440 context[0] = r0_thumb;
1441 context[15] = pc_thumb;
1442 } else if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1444 /* adjust value stored by STM */
1445 context[15] -= 3 * 4;
1448 if ((target->debug_reason != DBG_REASON_DBGRQ) || (!arm7_9->use_dbgrq))
1449 context[15] -= 3 * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
1450 else
1451 context[15] -= arm7_9->dbgreq_adjust_pc * ((armv4_5->core_state == ARMV4_5_STATE_ARM) ? 4 : 2);
1453 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1454 return ERROR_FAIL;
1456 for (i = 0; i <= 15; i++)
1458 LOG_DEBUG("r%i: 0x%8.8" PRIx32 "", i, context[i]);
1459 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).value, 0, 32, context[i]);
1460 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).dirty = 0;
1461 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, i).valid = 1;
1464 LOG_DEBUG("entered debug state at PC 0x%" PRIx32 "", context[15]);
1466 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1467 return ERROR_FAIL;
1469 /* exceptions other than USR & SYS have a saved program status register */
1470 if ((armv4_5->core_mode != ARMV4_5_MODE_USR) && (armv4_5->core_mode != ARMV4_5_MODE_SYS))
1472 uint32_t spsr;
1473 arm7_9->read_xpsr(target, &spsr, 1);
1474 if ((retval = jtag_execute_queue()) != ERROR_OK)
1476 return retval;
1478 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).value, 0, 32, spsr);
1479 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).dirty = 0;
1480 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5->core_mode, 16).valid = 1;
1483 /* r0 and r15 (pc) have to be restored later */
1484 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;
1485 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;
1487 if ((retval = jtag_execute_queue()) != ERROR_OK)
1488 return retval;
1490 if (arm7_9->post_debug_entry)
1491 arm7_9->post_debug_entry(target);
1493 return ERROR_OK;
1497 * Validate the full context for an ARM7/9 target in all processor modes. If
1498 * there are any invalid registers for the target, they will all be read. This
1499 * includes the PSR.
1501 * @param target Pointer to the ARM7/9 target to capture the full context from
1502 * @return Error if the target is not halted, has an invalid core mode, or if
1503 * the JTAG queue fails to execute
1505 int arm7_9_full_context(target_t *target)
1507 int i;
1508 int retval;
1509 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1510 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1512 LOG_DEBUG("-");
1514 if (target->state != TARGET_HALTED)
1516 LOG_WARNING("target not halted");
1517 return ERROR_TARGET_NOT_HALTED;
1520 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1521 return ERROR_FAIL;
1523 /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1524 * SYS shares registers with User, so we don't touch SYS
1526 for (i = 0; i < 6; i++)
1528 uint32_t mask = 0;
1529 uint32_t* reg_p[16];
1530 int j;
1531 int valid = 1;
1533 /* check if there are invalid registers in the current mode
1535 for (j = 0; j <= 16; j++)
1537 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1538 valid = 0;
1541 if (!valid)
1543 uint32_t tmp_cpsr;
1545 /* change processor mode (and mask T bit) */
1546 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1547 tmp_cpsr |= armv4_5_number_to_mode(i);
1548 tmp_cpsr &= ~0x20;
1549 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1551 for (j = 0; j < 15; j++)
1553 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1555 reg_p[j] = (uint32_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).value;
1556 mask |= 1 << j;
1557 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid = 1;
1558 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).dirty = 0;
1562 /* if only the PSR is invalid, mask is all zeroes */
1563 if (mask)
1564 arm7_9->read_core_regs(target, mask, reg_p);
1566 /* check if the PSR has to be read */
1567 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid == 0)
1569 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);
1570 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid = 1;
1571 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).dirty = 0;
1576 /* restore processor mode (mask T bit) */
1577 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
1579 if ((retval = jtag_execute_queue()) != ERROR_OK)
1581 return retval;
1583 return ERROR_OK;
1587 * Restore the processor context on an ARM7/9 target. The full processor
1588 * context is analyzed to see if any of the registers are dirty on this end, but
1589 * have a valid new value. If this is the case, the processor is changed to the
1590 * appropriate mode and the new register values are written out to the
1591 * processor. If there happens to be a dirty register with an invalid value, an
1592 * error will be logged.
1594 * @param target Pointer to the ARM7/9 target to have its context restored
1595 * @return Error status if the target is not halted or the core mode in the
1596 * armv4_5 struct is invalid.
1598 int arm7_9_restore_context(target_t *target)
1600 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1601 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1602 reg_t *reg;
1603 armv4_5_core_reg_t *reg_arch_info;
1604 enum armv4_5_mode current_mode = armv4_5->core_mode;
1605 int i, j;
1606 int dirty;
1607 int mode_change;
1609 LOG_DEBUG("-");
1611 if (target->state != TARGET_HALTED)
1613 LOG_WARNING("target not halted");
1614 return ERROR_TARGET_NOT_HALTED;
1617 if (arm7_9->pre_restore_context)
1618 arm7_9->pre_restore_context(target);
1620 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
1621 return ERROR_FAIL;
1623 /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1624 * SYS shares registers with User, so we don't touch SYS
1626 for (i = 0; i < 6; i++)
1628 LOG_DEBUG("examining %s mode", armv4_5_mode_strings[i]);
1629 dirty = 0;
1630 mode_change = 0;
1631 /* check if there are dirty registers in the current mode
1633 for (j = 0; j <= 16; j++)
1635 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
1636 reg_arch_info = reg->arch_info;
1637 if (reg->dirty == 1)
1639 if (reg->valid == 1)
1641 dirty = 1;
1642 LOG_DEBUG("examining dirty reg: %s", reg->name);
1643 if ((reg_arch_info->mode != ARMV4_5_MODE_ANY)
1644 && (reg_arch_info->mode != current_mode)
1645 && !((reg_arch_info->mode == ARMV4_5_MODE_USR) && (armv4_5->core_mode == ARMV4_5_MODE_SYS))
1646 && !((reg_arch_info->mode == ARMV4_5_MODE_SYS) && (armv4_5->core_mode == ARMV4_5_MODE_USR)))
1648 mode_change = 1;
1649 LOG_DEBUG("require mode change");
1652 else
1654 LOG_ERROR("BUG: dirty register '%s', but no valid data", reg->name);
1659 if (dirty)
1661 uint32_t mask = 0x0;
1662 int num_regs = 0;
1663 uint32_t regs[16];
1665 if (mode_change)
1667 uint32_t tmp_cpsr;
1669 /* change processor mode (mask T bit) */
1670 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1671 tmp_cpsr |= armv4_5_number_to_mode(i);
1672 tmp_cpsr &= ~0x20;
1673 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1674 current_mode = armv4_5_number_to_mode(i);
1677 for (j = 0; j <= 14; j++)
1679 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
1680 reg_arch_info = reg->arch_info;
1683 if (reg->dirty == 1)
1685 regs[j] = buf_get_u32(reg->value, 0, 32);
1686 mask |= 1 << j;
1687 num_regs++;
1688 reg->dirty = 0;
1689 reg->valid = 1;
1690 LOG_DEBUG("writing register %i of mode %s with value 0x%8.8" PRIx32 "", j, armv4_5_mode_strings[i], regs[j]);
1694 if (mask)
1696 arm7_9->write_core_regs(target, mask, regs);
1699 reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16);
1700 reg_arch_info = reg->arch_info;
1701 if ((reg->dirty) && (reg_arch_info->mode != ARMV4_5_MODE_ANY))
1703 LOG_DEBUG("writing SPSR of mode %i with value 0x%8.8" PRIx32 "", i, buf_get_u32(reg->value, 0, 32));
1704 arm7_9->write_xpsr(target, buf_get_u32(reg->value, 0, 32), 1);
1709 if ((armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 0) && (armv4_5->core_mode != current_mode))
1711 /* restore processor mode (mask T bit) */
1712 uint32_t tmp_cpsr;
1714 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
1715 tmp_cpsr |= armv4_5_number_to_mode(i);
1716 tmp_cpsr &= ~0x20;
1717 LOG_DEBUG("writing lower 8 bit of cpsr with value 0x%2.2x", (unsigned)(tmp_cpsr));
1718 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1720 else if (armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty == 1)
1722 /* CPSR has been changed, full restore necessary (mask T bit) */
1723 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));
1724 arm7_9->write_xpsr(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 32) & ~0x20, 0);
1725 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].dirty = 0;
1726 armv4_5->core_cache->reg_list[ARMV4_5_CPSR].valid = 1;
1729 /* restore PC */
1730 LOG_DEBUG("writing PC with value 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1731 arm7_9->write_pc(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1732 armv4_5->core_cache->reg_list[15].dirty = 0;
1734 if (arm7_9->post_restore_context)
1735 arm7_9->post_restore_context(target);
1737 return ERROR_OK;
1741 * Restart the core of an ARM7/9 target. A RESTART command is sent to the
1742 * instruction register and the JTAG state is set to TAP_IDLE causing a core
1743 * restart.
1745 * @param target Pointer to the ARM7/9 target to be restarted
1746 * @return Result of executing the JTAG queue
1748 int arm7_9_restart_core(struct target_s *target)
1750 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1751 arm_jtag_t *jtag_info = &arm7_9->jtag_info;
1753 /* set RESTART instruction */
1754 jtag_set_end_state(TAP_IDLE);
1755 if (arm7_9->need_bypass_before_restart) {
1756 arm7_9->need_bypass_before_restart = 0;
1757 arm_jtag_set_instr(jtag_info, 0xf, NULL);
1759 arm_jtag_set_instr(jtag_info, 0x4, NULL);
1761 jtag_add_runtest(1, jtag_set_end_state(TAP_IDLE));
1762 return jtag_execute_queue();
1766 * Enable the watchpoints on an ARM7/9 target. The target's watchpoints are
1767 * iterated through and are set on the target if they aren't already set.
1769 * @param target Pointer to the ARM7/9 target to enable watchpoints on
1771 void arm7_9_enable_watchpoints(struct target_s *target)
1773 watchpoint_t *watchpoint = target->watchpoints;
1775 while (watchpoint)
1777 if (watchpoint->set == 0)
1778 arm7_9_set_watchpoint(target, watchpoint);
1779 watchpoint = watchpoint->next;
1784 * Enable the breakpoints on an ARM7/9 target. The target's breakpoints are
1785 * iterated through and are set on the target.
1787 * @param target Pointer to the ARM7/9 target to enable breakpoints on
1789 void arm7_9_enable_breakpoints(struct target_s *target)
1791 breakpoint_t *breakpoint = target->breakpoints;
1793 /* set any pending breakpoints */
1794 while (breakpoint)
1796 arm7_9_set_breakpoint(target, breakpoint);
1797 breakpoint = breakpoint->next;
1801 int arm7_9_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
1803 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1804 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1805 breakpoint_t *breakpoint = target->breakpoints;
1806 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1807 int err, retval = ERROR_OK;
1809 LOG_DEBUG("-");
1811 if (target->state != TARGET_HALTED)
1813 LOG_WARNING("target not halted");
1814 return ERROR_TARGET_NOT_HALTED;
1817 if (!debug_execution)
1819 target_free_all_working_areas(target);
1822 /* current = 1: continue on current pc, otherwise continue at <address> */
1823 if (!current)
1824 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
1826 uint32_t current_pc;
1827 current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
1829 /* the front-end may request us not to handle breakpoints */
1830 if (handle_breakpoints)
1832 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
1834 LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 " (id: %d)", breakpoint->address, breakpoint->unique_id );
1835 if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
1837 return retval;
1840 /* calculate PC of next instruction */
1841 uint32_t next_pc;
1842 if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
1844 uint32_t current_opcode;
1845 target_read_u32(target, current_pc, &current_opcode);
1846 LOG_ERROR("Couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
1847 return retval;
1850 LOG_DEBUG("enable single-step");
1851 arm7_9->enable_single_step(target, next_pc);
1853 target->debug_reason = DBG_REASON_SINGLESTEP;
1855 if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
1857 return retval;
1860 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1861 arm7_9->branch_resume(target);
1862 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1864 arm7_9->branch_resume_thumb(target);
1866 else
1868 LOG_ERROR("unhandled core state");
1869 return ERROR_FAIL;
1872 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1873 embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1874 err = arm7_9_execute_sys_speed(target);
1876 LOG_DEBUG("disable single-step");
1877 arm7_9->disable_single_step(target);
1879 if (err != ERROR_OK)
1881 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
1883 return retval;
1885 target->state = TARGET_UNKNOWN;
1886 return err;
1889 arm7_9_debug_entry(target);
1890 LOG_DEBUG("new PC after step: 0x%8.8" PRIx32 "", buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32));
1892 LOG_DEBUG("set breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
1893 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
1895 return retval;
1900 /* enable any pending breakpoints and watchpoints */
1901 arm7_9_enable_breakpoints(target);
1902 arm7_9_enable_watchpoints(target);
1904 if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
1906 return retval;
1909 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
1911 arm7_9->branch_resume(target);
1913 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
1915 arm7_9->branch_resume_thumb(target);
1917 else
1919 LOG_ERROR("unhandled core state");
1920 return ERROR_FAIL;
1923 /* deassert DBGACK and INTDIS */
1924 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1925 /* INTDIS only when we really resume, not during debug execution */
1926 if (!debug_execution)
1927 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 0);
1928 embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1930 if ((retval = arm7_9_restart_core(target)) != ERROR_OK)
1932 return retval;
1935 target->debug_reason = DBG_REASON_NOTHALTED;
1937 if (!debug_execution)
1939 /* registers are now invalid */
1940 armv4_5_invalidate_core_regs(target);
1941 target->state = TARGET_RUNNING;
1942 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
1944 return retval;
1947 else
1949 target->state = TARGET_DEBUG_RUNNING;
1950 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED)) != ERROR_OK)
1952 return retval;
1956 LOG_DEBUG("target resumed");
1958 return ERROR_OK;
1961 void arm7_9_enable_eice_step(target_t *target, uint32_t next_pc)
1963 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
1964 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
1965 uint32_t current_pc;
1966 current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
1968 if (next_pc != current_pc)
1970 /* setup an inverse breakpoint on the current PC
1971 * - comparator 1 matches the current address
1972 * - rangeout from comparator 1 is connected to comparator 0 rangein
1973 * - comparator 0 matches any address, as long as rangein is low */
1974 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1975 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1976 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1977 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~(EICE_W_CTRL_RANGE | EICE_W_CTRL_nOPC) & 0xff);
1978 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], current_pc);
1979 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
1980 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
1981 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
1982 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1984 else
1986 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1987 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1988 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
1989 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff);
1990 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], next_pc);
1991 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
1992 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
1993 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1994 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1998 void arm7_9_disable_eice_step(target_t *target)
2000 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2002 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
2003 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
2004 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
2005 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
2006 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE]);
2007 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK]);
2008 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK]);
2009 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK]);
2010 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE]);
2013 int arm7_9_step(struct target_s *target, int current, uint32_t address, int handle_breakpoints)
2015 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2016 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2017 breakpoint_t *breakpoint = NULL;
2018 int err, retval;
2020 if (target->state != TARGET_HALTED)
2022 LOG_WARNING("target not halted");
2023 return ERROR_TARGET_NOT_HALTED;
2026 /* current = 1: continue on current pc, otherwise continue at <address> */
2027 if (!current)
2028 buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
2030 uint32_t current_pc;
2031 current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
2033 /* the front-end may request us not to handle breakpoints */
2034 if (handle_breakpoints)
2035 if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
2036 if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
2038 return retval;
2041 target->debug_reason = DBG_REASON_SINGLESTEP;
2043 /* calculate PC of next instruction */
2044 uint32_t next_pc;
2045 if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
2047 uint32_t current_opcode;
2048 target_read_u32(target, current_pc, &current_opcode);
2049 LOG_ERROR("Couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
2050 return retval;
2053 if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
2055 return retval;
2058 arm7_9->enable_single_step(target, next_pc);
2060 if (armv4_5->core_state == ARMV4_5_STATE_ARM)
2062 arm7_9->branch_resume(target);
2064 else if (armv4_5->core_state == ARMV4_5_STATE_THUMB)
2066 arm7_9->branch_resume_thumb(target);
2068 else
2070 LOG_ERROR("unhandled core state");
2071 return ERROR_FAIL;
2074 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
2076 return retval;
2079 err = arm7_9_execute_sys_speed(target);
2080 arm7_9->disable_single_step(target);
2082 /* registers are now invalid */
2083 armv4_5_invalidate_core_regs(target);
2085 if (err != ERROR_OK)
2087 target->state = TARGET_UNKNOWN;
2088 } else {
2089 arm7_9_debug_entry(target);
2090 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
2092 return retval;
2094 LOG_DEBUG("target stepped");
2097 if (breakpoint)
2098 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
2100 return retval;
2103 return err;
2106 int arm7_9_read_core_reg(struct target_s *target, int num, enum armv4_5_mode mode)
2108 uint32_t* reg_p[16];
2109 uint32_t value;
2110 int retval;
2111 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2112 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2114 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2115 return ERROR_FAIL;
2117 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;
2119 if ((num < 0) || (num > 16))
2120 return ERROR_INVALID_ARGUMENTS;
2122 if ((mode != ARMV4_5_MODE_ANY)
2123 && (mode != armv4_5->core_mode)
2124 && (reg_mode != ARMV4_5_MODE_ANY))
2126 uint32_t tmp_cpsr;
2128 /* change processor mode (mask T bit) */
2129 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
2130 tmp_cpsr |= mode;
2131 tmp_cpsr &= ~0x20;
2132 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
2135 if ((num >= 0) && (num <= 15))
2137 /* read a normal core register */
2138 reg_p[num] = &value;
2140 arm7_9->read_core_regs(target, 1 << num, reg_p);
2142 else
2144 /* read a program status register
2145 * if the register mode is MODE_ANY, we read the cpsr, otherwise a spsr
2147 armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
2148 int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
2150 arm7_9->read_xpsr(target, &value, spsr);
2153 if ((retval = jtag_execute_queue()) != ERROR_OK)
2155 return retval;
2158 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
2159 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
2160 buf_set_u32(ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).value, 0, 32, value);
2162 if ((mode != ARMV4_5_MODE_ANY)
2163 && (mode != armv4_5->core_mode)
2164 && (reg_mode != ARMV4_5_MODE_ANY)) {
2165 /* restore processor mode (mask T bit) */
2166 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2169 return ERROR_OK;
2172 int arm7_9_write_core_reg(struct target_s *target, int num, enum armv4_5_mode mode, uint32_t value)
2174 uint32_t reg[16];
2175 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2176 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2178 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2179 return ERROR_FAIL;
2181 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;
2183 if ((num < 0) || (num > 16))
2184 return ERROR_INVALID_ARGUMENTS;
2186 if ((mode != ARMV4_5_MODE_ANY)
2187 && (mode != armv4_5->core_mode)
2188 && (reg_mode != ARMV4_5_MODE_ANY)) {
2189 uint32_t tmp_cpsr;
2191 /* change processor mode (mask T bit) */
2192 tmp_cpsr = buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & 0xE0;
2193 tmp_cpsr |= mode;
2194 tmp_cpsr &= ~0x20;
2195 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
2198 if ((num >= 0) && (num <= 15))
2200 /* write a normal core register */
2201 reg[num] = value;
2203 arm7_9->write_core_regs(target, 1 << num, reg);
2205 else
2207 /* write a program status register
2208 * if the register mode is MODE_ANY, we write the cpsr, otherwise a spsr
2210 armv4_5_core_reg_t *arch_info = ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).arch_info;
2211 int spsr = (arch_info->mode == ARMV4_5_MODE_ANY) ? 0 : 1;
2213 /* if we're writing the CPSR, mask the T bit */
2214 if (!spsr)
2215 value &= ~0x20;
2217 arm7_9->write_xpsr(target, value, spsr);
2220 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).valid = 1;
2221 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, mode, num).dirty = 0;
2223 if ((mode != ARMV4_5_MODE_ANY)
2224 && (mode != armv4_5->core_mode)
2225 && (reg_mode != ARMV4_5_MODE_ANY)) {
2226 /* restore processor mode (mask T bit) */
2227 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2230 return jtag_execute_queue();
2233 int arm7_9_read_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2235 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2236 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2237 uint32_t reg[16];
2238 uint32_t num_accesses = 0;
2239 int thisrun_accesses;
2240 int i;
2241 uint32_t cpsr;
2242 int retval;
2243 int last_reg = 0;
2245 LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
2247 if (target->state != TARGET_HALTED)
2249 LOG_WARNING("target not halted");
2250 return ERROR_TARGET_NOT_HALTED;
2253 /* sanitize arguments */
2254 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
2255 return ERROR_INVALID_ARGUMENTS;
2257 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2258 return ERROR_TARGET_UNALIGNED_ACCESS;
2260 /* load the base register with the address of the first word */
2261 reg[0] = address;
2262 arm7_9->write_core_regs(target, 0x1, reg);
2264 int j = 0;
2266 switch (size)
2268 case 4:
2269 while (num_accesses < count)
2271 uint32_t reg_list;
2272 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2273 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2275 if (last_reg <= thisrun_accesses)
2276 last_reg = thisrun_accesses;
2278 arm7_9->load_word_regs(target, reg_list);
2280 /* fast memory reads are only safe when the target is running
2281 * from a sufficiently high clock (32 kHz is usually too slow)
2283 if (arm7_9->fast_memory_access)
2284 retval = arm7_9_execute_fast_sys_speed(target);
2285 else
2286 retval = arm7_9_execute_sys_speed(target);
2287 if (retval != ERROR_OK)
2288 return retval;
2290 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 4);
2292 /* advance buffer, count number of accesses */
2293 buffer += thisrun_accesses * 4;
2294 num_accesses += thisrun_accesses;
2296 if ((j++%1024) == 0)
2298 keep_alive();
2301 break;
2302 case 2:
2303 while (num_accesses < count)
2305 uint32_t reg_list;
2306 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2307 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2309 for (i = 1; i <= thisrun_accesses; i++)
2311 if (i > last_reg)
2312 last_reg = i;
2313 arm7_9->load_hword_reg(target, i);
2314 /* fast memory reads are only safe when the target is running
2315 * from a sufficiently high clock (32 kHz is usually too slow)
2317 if (arm7_9->fast_memory_access)
2318 retval = arm7_9_execute_fast_sys_speed(target);
2319 else
2320 retval = arm7_9_execute_sys_speed(target);
2321 if (retval != ERROR_OK)
2323 return retval;
2328 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 2);
2330 /* advance buffer, count number of accesses */
2331 buffer += thisrun_accesses * 2;
2332 num_accesses += thisrun_accesses;
2334 if ((j++%1024) == 0)
2336 keep_alive();
2339 break;
2340 case 1:
2341 while (num_accesses < count)
2343 uint32_t reg_list;
2344 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2345 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2347 for (i = 1; i <= thisrun_accesses; i++)
2349 if (i > last_reg)
2350 last_reg = i;
2351 arm7_9->load_byte_reg(target, i);
2352 /* fast memory reads are only safe when the target is running
2353 * from a sufficiently high clock (32 kHz is usually too slow)
2355 if (arm7_9->fast_memory_access)
2356 retval = arm7_9_execute_fast_sys_speed(target);
2357 else
2358 retval = arm7_9_execute_sys_speed(target);
2359 if (retval != ERROR_OK)
2361 return retval;
2365 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 1);
2367 /* advance buffer, count number of accesses */
2368 buffer += thisrun_accesses * 1;
2369 num_accesses += thisrun_accesses;
2371 if ((j++%1024) == 0)
2373 keep_alive();
2376 break;
2377 default:
2378 LOG_ERROR("BUG: we shouldn't get here");
2379 exit(-1);
2380 break;
2383 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2384 return ERROR_FAIL;
2386 for (i = 0; i <= last_reg; i++)
2387 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;
2389 arm7_9->read_xpsr(target, &cpsr, 0);
2390 if ((retval = jtag_execute_queue()) != ERROR_OK)
2392 LOG_ERROR("JTAG error while reading cpsr");
2393 return ERROR_TARGET_DATA_ABORT;
2396 if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
2398 LOG_WARNING("memory read caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
2400 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2402 return ERROR_TARGET_DATA_ABORT;
2405 return ERROR_OK;
2408 int arm7_9_write_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2410 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2411 struct armv4_5_common_s *armv4_5 = &arm7_9->armv4_5_common;
2412 reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
2414 uint32_t reg[16];
2415 uint32_t num_accesses = 0;
2416 int thisrun_accesses;
2417 int i;
2418 uint32_t cpsr;
2419 int retval;
2420 int last_reg = 0;
2422 #ifdef _DEBUG_ARM7_9_
2423 LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address, size, count);
2424 #endif
2426 if (target->state != TARGET_HALTED)
2428 LOG_WARNING("target not halted");
2429 return ERROR_TARGET_NOT_HALTED;
2432 /* sanitize arguments */
2433 if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
2434 return ERROR_INVALID_ARGUMENTS;
2436 if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2437 return ERROR_TARGET_UNALIGNED_ACCESS;
2439 /* load the base register with the address of the first word */
2440 reg[0] = address;
2441 arm7_9->write_core_regs(target, 0x1, reg);
2443 /* Clear DBGACK, to make sure memory fetches work as expected */
2444 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
2445 embeddedice_store_reg(dbg_ctrl);
2447 switch (size)
2449 case 4:
2450 while (num_accesses < count)
2452 uint32_t reg_list;
2453 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2454 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2456 for (i = 1; i <= thisrun_accesses; i++)
2458 if (i > last_reg)
2459 last_reg = i;
2460 reg[i] = target_buffer_get_u32(target, buffer);
2461 buffer += 4;
2464 arm7_9->write_core_regs(target, reg_list, reg);
2466 arm7_9->store_word_regs(target, reg_list);
2468 /* fast memory writes are only safe when the target is running
2469 * from a sufficiently high clock (32 kHz is usually too slow)
2471 if (arm7_9->fast_memory_access)
2472 retval = arm7_9_execute_fast_sys_speed(target);
2473 else
2474 retval = arm7_9_execute_sys_speed(target);
2475 if (retval != ERROR_OK)
2477 return retval;
2480 num_accesses += thisrun_accesses;
2482 break;
2483 case 2:
2484 while (num_accesses < count)
2486 uint32_t reg_list;
2487 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2488 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2490 for (i = 1; i <= thisrun_accesses; i++)
2492 if (i > last_reg)
2493 last_reg = i;
2494 reg[i] = target_buffer_get_u16(target, buffer) & 0xffff;
2495 buffer += 2;
2498 arm7_9->write_core_regs(target, reg_list, reg);
2500 for (i = 1; i <= thisrun_accesses; i++)
2502 arm7_9->store_hword_reg(target, i);
2504 /* fast memory writes are only safe when the target is running
2505 * from a sufficiently high clock (32 kHz is usually too slow)
2507 if (arm7_9->fast_memory_access)
2508 retval = arm7_9_execute_fast_sys_speed(target);
2509 else
2510 retval = arm7_9_execute_sys_speed(target);
2511 if (retval != ERROR_OK)
2513 return retval;
2517 num_accesses += thisrun_accesses;
2519 break;
2520 case 1:
2521 while (num_accesses < count)
2523 uint32_t reg_list;
2524 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2525 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2527 for (i = 1; i <= thisrun_accesses; i++)
2529 if (i > last_reg)
2530 last_reg = i;
2531 reg[i] = *buffer++ & 0xff;
2534 arm7_9->write_core_regs(target, reg_list, reg);
2536 for (i = 1; i <= thisrun_accesses; i++)
2538 arm7_9->store_byte_reg(target, i);
2539 /* fast memory writes are only safe when the target is running
2540 * from a sufficiently high clock (32 kHz is usually too slow)
2542 if (arm7_9->fast_memory_access)
2543 retval = arm7_9_execute_fast_sys_speed(target);
2544 else
2545 retval = arm7_9_execute_sys_speed(target);
2546 if (retval != ERROR_OK)
2548 return retval;
2553 num_accesses += thisrun_accesses;
2555 break;
2556 default:
2557 LOG_ERROR("BUG: we shouldn't get here");
2558 exit(-1);
2559 break;
2562 /* Re-Set DBGACK */
2563 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
2564 embeddedice_store_reg(dbg_ctrl);
2566 if (armv4_5_mode_to_number(armv4_5->core_mode)==-1)
2567 return ERROR_FAIL;
2569 for (i = 0; i <= last_reg; i++)
2570 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;
2572 arm7_9->read_xpsr(target, &cpsr, 0);
2573 if ((retval = jtag_execute_queue()) != ERROR_OK)
2575 LOG_ERROR("JTAG error while reading cpsr");
2576 return ERROR_TARGET_DATA_ABORT;
2579 if (((cpsr & 0x1f) == ARMV4_5_MODE_ABT) && (armv4_5->core_mode != ARMV4_5_MODE_ABT))
2581 LOG_WARNING("memory write caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
2583 arm7_9->write_xpsr_im8(target, buf_get_u32(armv4_5->core_cache->reg_list[ARMV4_5_CPSR].value, 0, 8) & ~0x20, 0, 0);
2585 return ERROR_TARGET_DATA_ABORT;
2588 return ERROR_OK;
2591 static int dcc_count;
2592 static uint8_t *dcc_buffer;
2594 static int arm7_9_dcc_completion(struct target_s *target, uint32_t exit_point, int timeout_ms, void *arch_info)
2596 int retval = ERROR_OK;
2597 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2599 if ((retval = target_wait_state(target, TARGET_DEBUG_RUNNING, 500)) != ERROR_OK)
2600 return retval;
2602 int little = target->endianness == TARGET_LITTLE_ENDIAN;
2603 int count = dcc_count;
2604 uint8_t *buffer = dcc_buffer;
2605 if (count > 2)
2607 /* Handle first & last using standard embeddedice_write_reg and the middle ones w/the
2608 * core function repeated. */
2609 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2610 buffer += 4;
2612 embeddedice_reg_t *ice_reg = arm7_9->eice_cache->reg_list[EICE_COMMS_DATA].arch_info;
2613 uint8_t reg_addr = ice_reg->addr & 0x1f;
2614 jtag_tap_t *tap;
2615 tap = ice_reg->jtag_info->tap;
2617 embeddedice_write_dcc(tap, reg_addr, buffer, little, count-2);
2618 buffer += (count-2)*4;
2620 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2621 } else
2623 int i;
2624 for (i = 0; i < count; i++)
2626 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2627 buffer += 4;
2631 if ((retval = target_halt(target))!= ERROR_OK)
2633 return retval;
2635 return target_wait_state(target, TARGET_HALTED, 500);
2638 static const uint32_t dcc_code[] =
2640 /* r0 == input, points to memory buffer
2641 * r1 == scratch
2644 /* spin until DCC control (c0) reports data arrived */
2645 0xee101e10, /* w: mrc p14, #0, r1, c0, c0 */
2646 0xe3110001, /* tst r1, #1 */
2647 0x0afffffc, /* bne w */
2649 /* read word from DCC (c1), write to memory */
2650 0xee111e10, /* mrc p14, #0, r1, c1, c0 */
2651 0xe4801004, /* str r1, [r0], #4 */
2653 /* repeat */
2654 0xeafffff9 /* b w */
2657 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));
2659 int arm7_9_bulk_write_memory(target_t *target, uint32_t address, uint32_t count, uint8_t *buffer)
2661 int retval;
2662 struct arm7_9_common_s *arm7_9 = target_to_arm7_9(target);
2663 int i;
2665 if (!arm7_9->dcc_downloads)
2666 return target_write_memory(target, address, 4, count, buffer);
2668 /* regrab previously allocated working_area, or allocate a new one */
2669 if (!arm7_9->dcc_working_area)
2671 uint8_t dcc_code_buf[6 * 4];
2673 /* make sure we have a working area */
2674 if (target_alloc_working_area(target, 24, &arm7_9->dcc_working_area) != ERROR_OK)
2676 LOG_INFO("no working area available, falling back to memory writes");
2677 return target_write_memory(target, address, 4, count, buffer);
2680 /* copy target instructions to target endianness */
2681 for (i = 0; i < 6; i++)
2683 target_buffer_set_u32(target, dcc_code_buf + i*4, dcc_code[i]);
2686 /* write DCC code to working area */
2687 if ((retval = target_write_memory(target, arm7_9->dcc_working_area->address, 4, 6, dcc_code_buf)) != ERROR_OK)
2689 return retval;
2693 armv4_5_algorithm_t armv4_5_info;
2694 reg_param_t reg_params[1];
2696 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
2697 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
2698 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
2700 init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
2702 buf_set_u32(reg_params[0].value, 0, 32, address);
2704 dcc_count = count;
2705 dcc_buffer = buffer;
2706 retval = armv4_5_run_algorithm_inner(target, 0, NULL, 1, reg_params,
2707 arm7_9->dcc_working_area->address, arm7_9->dcc_working_area->address + 6*4, 20*1000, &armv4_5_info, arm7_9_dcc_completion);
2709 if (retval == ERROR_OK)
2711 uint32_t endaddress = buf_get_u32(reg_params[0].value, 0, 32);
2712 if (endaddress != (address + count*4))
2714 LOG_ERROR("DCC write failed, expected end address 0x%08" PRIx32 " got 0x%0" PRIx32 "", (address + count*4), endaddress);
2715 retval = ERROR_FAIL;
2719 destroy_reg_param(&reg_params[0]);
2721 return retval;
2724 int arm7_9_checksum_memory(struct target_s *target, uint32_t address, uint32_t count, uint32_t* checksum)
2726 working_area_t *crc_algorithm;
2727 armv4_5_algorithm_t armv4_5_info;
2728 reg_param_t reg_params[2];
2729 int retval;
2731 static const uint32_t arm7_9_crc_code[] = {
2732 0xE1A02000, /* mov r2, r0 */
2733 0xE3E00000, /* mov r0, #0xffffffff */
2734 0xE1A03001, /* mov r3, r1 */
2735 0xE3A04000, /* mov r4, #0 */
2736 0xEA00000B, /* b ncomp */
2737 /* nbyte: */
2738 0xE7D21004, /* ldrb r1, [r2, r4] */
2739 0xE59F7030, /* ldr r7, CRC32XOR */
2740 0xE0200C01, /* eor r0, r0, r1, asl 24 */
2741 0xE3A05000, /* mov r5, #0 */
2742 /* loop: */
2743 0xE3500000, /* cmp r0, #0 */
2744 0xE1A06080, /* mov r6, r0, asl #1 */
2745 0xE2855001, /* add r5, r5, #1 */
2746 0xE1A00006, /* mov r0, r6 */
2747 0xB0260007, /* eorlt r0, r6, r7 */
2748 0xE3550008, /* cmp r5, #8 */
2749 0x1AFFFFF8, /* bne loop */
2750 0xE2844001, /* add r4, r4, #1 */
2751 /* ncomp: */
2752 0xE1540003, /* cmp r4, r3 */
2753 0x1AFFFFF1, /* bne nbyte */
2754 /* end: */
2755 0xEAFFFFFE, /* b end */
2756 0x04C11DB7 /* CRC32XOR: .word 0x04C11DB7 */
2759 uint32_t i;
2761 if (target_alloc_working_area(target, sizeof(arm7_9_crc_code), &crc_algorithm) != ERROR_OK)
2763 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2766 /* convert flash writing code into a buffer in target endianness */
2767 for (i = 0; i < (sizeof(arm7_9_crc_code)/sizeof(uint32_t)); i++)
2769 if ((retval = target_write_u32(target, crc_algorithm->address + i*sizeof(uint32_t), arm7_9_crc_code[i])) != ERROR_OK)
2771 return retval;
2775 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
2776 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
2777 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
2779 init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
2780 init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
2782 buf_set_u32(reg_params[0].value, 0, 32, address);
2783 buf_set_u32(reg_params[1].value, 0, 32, count);
2785 /* 20 second timeout/megabyte */
2786 int timeout = 20000 * (1 + (count / (1024*1024)));
2788 if ((retval = target_run_algorithm(target, 0, NULL, 2, reg_params,
2789 crc_algorithm->address, crc_algorithm->address + (sizeof(arm7_9_crc_code) - 8), timeout, &armv4_5_info)) != ERROR_OK)
2791 LOG_ERROR("error executing arm7_9 crc algorithm");
2792 destroy_reg_param(&reg_params[0]);
2793 destroy_reg_param(&reg_params[1]);
2794 target_free_working_area(target, crc_algorithm);
2795 return retval;
2798 *checksum = buf_get_u32(reg_params[0].value, 0, 32);
2800 destroy_reg_param(&reg_params[0]);
2801 destroy_reg_param(&reg_params[1]);
2803 target_free_working_area(target, crc_algorithm);
2805 return ERROR_OK;
2808 int arm7_9_blank_check_memory(struct target_s *target, uint32_t address, uint32_t count, uint32_t* blank)
2810 working_area_t *erase_check_algorithm;
2811 reg_param_t reg_params[3];
2812 armv4_5_algorithm_t armv4_5_info;
2813 int retval;
2814 uint32_t i;
2816 static const uint32_t erase_check_code[] =
2818 /* loop: */
2819 0xe4d03001, /* ldrb r3, [r0], #1 */
2820 0xe0022003, /* and r2, r2, r3 */
2821 0xe2511001, /* subs r1, r1, #1 */
2822 0x1afffffb, /* bne loop */
2823 /* end: */
2824 0xeafffffe /* b end */
2827 /* make sure we have a working area */
2828 if (target_alloc_working_area(target, sizeof(erase_check_code), &erase_check_algorithm) != ERROR_OK)
2830 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2833 /* convert flash writing code into a buffer in target endianness */
2834 for (i = 0; i < (sizeof(erase_check_code)/sizeof(uint32_t)); i++)
2835 if ((retval = target_write_u32(target, erase_check_algorithm->address + i*sizeof(uint32_t), erase_check_code[i])) != ERROR_OK)
2837 return retval;
2840 armv4_5_info.common_magic = ARMV4_5_COMMON_MAGIC;
2841 armv4_5_info.core_mode = ARMV4_5_MODE_SVC;
2842 armv4_5_info.core_state = ARMV4_5_STATE_ARM;
2844 init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
2845 buf_set_u32(reg_params[0].value, 0, 32, address);
2847 init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
2848 buf_set_u32(reg_params[1].value, 0, 32, count);
2850 init_reg_param(&reg_params[2], "r2", 32, PARAM_IN_OUT);
2851 buf_set_u32(reg_params[2].value, 0, 32, 0xff);
2853 if ((retval = target_run_algorithm(target, 0, NULL, 3, reg_params,
2854 erase_check_algorithm->address, erase_check_algorithm->address + (sizeof(erase_check_code) - 4), 10000, &armv4_5_info)) != ERROR_OK)
2856 destroy_reg_param(&reg_params[0]);
2857 destroy_reg_param(&reg_params[1]);
2858 destroy_reg_param(&reg_params[2]);
2859 target_free_working_area(target, erase_check_algorithm);
2860 return 0;
2863 *blank = buf_get_u32(reg_params[2].value, 0, 32);
2865 destroy_reg_param(&reg_params[0]);
2866 destroy_reg_param(&reg_params[1]);
2867 destroy_reg_param(&reg_params[2]);
2869 target_free_working_area(target, erase_check_algorithm);
2871 return ERROR_OK;
2874 int handle_arm7_9_write_xpsr_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2876 uint32_t value;
2877 int spsr;
2878 int retval;
2879 target_t *target = get_current_target(cmd_ctx);
2880 armv4_5_common_t *armv4_5;
2881 arm7_9_common_t *arm7_9;
2883 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2885 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2886 return ERROR_OK;
2889 if (target->state != TARGET_HALTED)
2891 command_print(cmd_ctx, "can't write registers while running");
2892 return ERROR_OK;
2895 if (argc < 2)
2897 command_print(cmd_ctx, "usage: write_xpsr <value> <not cpsr | spsr>");
2898 return ERROR_OK;
2901 COMMAND_PARSE_NUMBER(u32, args[0], value);
2902 COMMAND_PARSE_NUMBER(int, args[1], spsr);
2904 /* if we're writing the CPSR, mask the T bit */
2905 if (!spsr)
2906 value &= ~0x20;
2908 arm7_9->write_xpsr(target, value, spsr);
2909 if ((retval = jtag_execute_queue()) != ERROR_OK)
2911 LOG_ERROR("JTAG error while writing to xpsr");
2912 return retval;
2915 return ERROR_OK;
2918 int handle_arm7_9_write_xpsr_im8_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2920 uint32_t value;
2921 int rotate;
2922 int spsr;
2923 int retval;
2924 target_t *target = get_current_target(cmd_ctx);
2925 armv4_5_common_t *armv4_5;
2926 arm7_9_common_t *arm7_9;
2928 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2930 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2931 return ERROR_OK;
2934 if (target->state != TARGET_HALTED)
2936 command_print(cmd_ctx, "can't write registers while running");
2937 return ERROR_OK;
2940 if (argc < 3)
2942 command_print(cmd_ctx, "usage: write_xpsr_im8 <im8> <rotate> <not cpsr | spsr>");
2943 return ERROR_OK;
2946 COMMAND_PARSE_NUMBER(u32, args[0], value);
2947 COMMAND_PARSE_NUMBER(int, args[1], rotate);
2948 COMMAND_PARSE_NUMBER(int, args[2], spsr);
2950 arm7_9->write_xpsr_im8(target, value, rotate, spsr);
2951 if ((retval = jtag_execute_queue()) != ERROR_OK)
2953 LOG_ERROR("JTAG error while writing 8-bit immediate to xpsr");
2954 return retval;
2957 return ERROR_OK;
2960 int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2962 uint32_t value;
2963 uint32_t mode;
2964 int num;
2965 target_t *target = get_current_target(cmd_ctx);
2966 armv4_5_common_t *armv4_5;
2967 arm7_9_common_t *arm7_9;
2969 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
2971 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
2972 return ERROR_OK;
2975 if (target->state != TARGET_HALTED)
2977 command_print(cmd_ctx, "can't write registers while running");
2978 return ERROR_OK;
2981 if (argc < 3)
2983 command_print(cmd_ctx, "usage: write_core_reg <num> <mode> <value>");
2984 return ERROR_OK;
2987 COMMAND_PARSE_NUMBER(int, args[0], num);
2988 COMMAND_PARSE_NUMBER(u32, args[1], mode);
2989 COMMAND_PARSE_NUMBER(u32, args[2], value);
2991 return arm7_9_write_core_reg(target, num, mode, value);
2994 int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2996 target_t *target = get_current_target(cmd_ctx);
2997 armv4_5_common_t *armv4_5;
2998 arm7_9_common_t *arm7_9;
3000 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
3002 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
3003 return ERROR_OK;
3006 if (argc > 0)
3008 if (strcmp("enable", args[0]) == 0)
3010 arm7_9->use_dbgrq = 1;
3012 else if (strcmp("disable", args[0]) == 0)
3014 arm7_9->use_dbgrq = 0;
3016 else
3018 command_print(cmd_ctx, "usage: arm7_9 dbgrq <enable | disable>");
3022 command_print(cmd_ctx, "use of EmbeddedICE dbgrq instead of breakpoint for target halt %s", (arm7_9->use_dbgrq) ? "enabled" : "disabled");
3024 return ERROR_OK;
3027 int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3029 target_t *target = get_current_target(cmd_ctx);
3030 armv4_5_common_t *armv4_5;
3031 arm7_9_common_t *arm7_9;
3033 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
3035 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
3036 return ERROR_OK;
3039 if (argc > 0)
3041 if (strcmp("enable", args[0]) == 0)
3043 arm7_9->fast_memory_access = 1;
3045 else if (strcmp("disable", args[0]) == 0)
3047 arm7_9->fast_memory_access = 0;
3049 else
3051 command_print(cmd_ctx, "usage: arm7_9 fast_memory_access <enable | disable>");
3055 command_print(cmd_ctx, "fast memory access is %s", (arm7_9->fast_memory_access) ? "enabled" : "disabled");
3057 return ERROR_OK;
3060 int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3062 target_t *target = get_current_target(cmd_ctx);
3063 armv4_5_common_t *armv4_5;
3064 arm7_9_common_t *arm7_9;
3066 if (arm7_9_get_arch_pointers(target, &armv4_5, &arm7_9) != ERROR_OK)
3068 command_print(cmd_ctx, "current target isn't an ARM7/ARM9 target");
3069 return ERROR_OK;
3072 if (argc > 0)
3074 if (strcmp("enable", args[0]) == 0)
3076 arm7_9->dcc_downloads = 1;
3078 else if (strcmp("disable", args[0]) == 0)
3080 arm7_9->dcc_downloads = 0;
3082 else
3084 command_print(cmd_ctx, "usage: arm7_9 dcc_downloads <enable | disable>");
3088 command_print(cmd_ctx, "dcc downloads are %s", (arm7_9->dcc_downloads) ? "enabled" : "disabled");
3090 return ERROR_OK;
3093 int arm7_9_init_arch_info(target_t *target, arm7_9_common_t *arm7_9)
3095 int retval = ERROR_OK;
3096 armv4_5_common_t *armv4_5 = &arm7_9->armv4_5_common;
3098 arm7_9->common_magic = ARM7_9_COMMON_MAGIC;
3100 if ((retval = arm_jtag_setup_connection(&arm7_9->jtag_info)) != ERROR_OK)
3101 return retval;
3103 /* caller must have allocated via calloc(), so everything's zeroed */
3105 arm7_9->wp_available_max = 2;
3107 arm7_9->fast_memory_access = fast_and_dangerous;
3108 arm7_9->dcc_downloads = fast_and_dangerous;
3110 armv4_5->arch_info = arm7_9;
3111 armv4_5->read_core_reg = arm7_9_read_core_reg;
3112 armv4_5->write_core_reg = arm7_9_write_core_reg;
3113 armv4_5->full_context = arm7_9_full_context;
3115 if ((retval = armv4_5_init_arch_info(target, armv4_5)) != ERROR_OK)
3116 return retval;
3118 return target_register_timer_callback(arm7_9_handle_target_request,
3119 1, 1, target);
3122 int arm7_9_register_commands(struct command_context_s *cmd_ctx)
3124 command_t *arm7_9_cmd;
3126 arm7_9_cmd = register_command(cmd_ctx, NULL, "arm7_9",
3127 NULL, COMMAND_ANY, "arm7/9 specific commands");
3129 register_command(cmd_ctx, arm7_9_cmd, "write_xpsr",
3130 handle_arm7_9_write_xpsr_command, COMMAND_EXEC,
3131 "write program status register <value> <not cpsr | spsr>");
3132 register_command(cmd_ctx, arm7_9_cmd, "write_xpsr_im8",
3133 handle_arm7_9_write_xpsr_im8_command, COMMAND_EXEC,
3134 "write program status register "
3135 "<8bit immediate> <rotate> <not cpsr | spsr>");
3137 register_command(cmd_ctx, arm7_9_cmd, "write_core_reg",
3138 handle_arm7_9_write_core_reg_command, COMMAND_EXEC,
3139 "write core register <num> <mode> <value>");
3141 register_command(cmd_ctx, arm7_9_cmd, "dbgrq",
3142 handle_arm7_9_dbgrq_command, COMMAND_ANY,
3143 "use EmbeddedICE dbgrq instead of breakpoint "
3144 "for target halt requests <enable | disable>");
3145 register_command(cmd_ctx, arm7_9_cmd, "fast_memory_access",
3146 handle_arm7_9_fast_memory_access_command, COMMAND_ANY,
3147 "use fast memory accesses instead of slower "
3148 "but potentially safer accesses <enable | disable>");
3149 register_command(cmd_ctx, arm7_9_cmd, "dcc_downloads",
3150 handle_arm7_9_dcc_downloads_command, COMMAND_ANY,
3151 "use DCC downloads for larger memory writes <enable | disable>");
3153 armv4_5_register_commands(cmd_ctx);
3155 etm_register_commands(cmd_ctx);
3157 return ERROR_OK;