arm: error propagation of arm_jtag_set_instr
[fw/openocd] / src / target / arm7_9_common.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007-2009 Ã˜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  *   Copyright (C) 2009 by David Brownell                                  *
15  *                                                                         *
16  *   This program is free software; you can redistribute it and/or modify  *
17  *   it under the terms of the GNU General Public License as published by  *
18  *   the Free Software Foundation; either version 2 of the License, or     *
19  *   (at your option) any later version.                                   *
20  *                                                                         *
21  *   This program is distributed in the hope that it will be useful,       *
22  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
23  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
24  *   GNU General Public License for more details.                          *
25  *                                                                         *
26  *   You should have received a copy of the GNU General Public License     *
27  *   along with this program; if not, write to the                         *
28  *   Free Software Foundation, Inc.,                                       *
29  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
30  ***************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "breakpoints.h"
36 #include "embeddedice.h"
37 #include "target_request.h"
38 #include "etm.h"
39 #include <helper/time_support.h>
40 #include "arm_simulator.h"
41 #include "arm_semihosting.h"
42 #include "algorithm.h"
43 #include "register.h"
44 #include "armv4_5.h"
45
46
47 /**
48  * @file
49  * Hold common code supporting the ARM7 and ARM9 core generations.
50  *
51  * While the ARM core implementations evolved substantially during these
52  * two generations, they look quite similar from the JTAG perspective.
53  * Both have similar debug facilities, based on the same two scan chains
54  * providing access to the core and to an EmbeddedICE module.  Both can
55  * support similar ETM and ETB modules, for tracing.  And both expose
56  * what could be viewed as "ARM Classic", with multiple processor modes,
57  * shadowed registers, and support for the Thumb instruction set.
58  *
59  * Processor differences include things like presence or absence of MMU
60  * and cache, pipeline sizes, use of a modified Harvard Architecure
61  * (with separate instruction and data busses from the CPU), support
62  * for cpu clock gating during idle, and more.
63  */
64
65 static int arm7_9_debug_entry(struct target *target);
66
67 /**
68  * Clear watchpoints for an ARM7/9 target.
69  *
70  * @param arm7_9 Pointer to the common struct for an ARM7/9 target
71  * @return JTAG error status after executing queue
72  */
73 static int arm7_9_clear_watchpoints(struct arm7_9_common *arm7_9)
74 {
75         LOG_DEBUG("-");
76         embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
77         embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
78         arm7_9->sw_breakpoint_count = 0;
79         arm7_9->sw_breakpoints_added = 0;
80         arm7_9->wp0_used = 0;
81         arm7_9->wp1_used = arm7_9->wp1_used_default;
82         arm7_9->wp_available = arm7_9->wp_available_max;
83
84         return jtag_execute_queue();
85 }
86
87 /**
88  * Assign a watchpoint to one of the two available hardware comparators in an
89  * ARM7 or ARM9 target.
90  *
91  * @param arm7_9 Pointer to the common struct for an ARM7/9 target
92  * @param breakpoint Pointer to the breakpoint to be used as a watchpoint
93  */
94 static void arm7_9_assign_wp(struct arm7_9_common *arm7_9, struct breakpoint *breakpoint)
95 {
96         if (!arm7_9->wp0_used)
97         {
98                 arm7_9->wp0_used = 1;
99                 breakpoint->set = 1;
100                 arm7_9->wp_available--;
101         }
102         else if (!arm7_9->wp1_used)
103         {
104                 arm7_9->wp1_used = 1;
105                 breakpoint->set = 2;
106                 arm7_9->wp_available--;
107         }
108         else
109         {
110                 LOG_ERROR("BUG: no hardware comparator available");
111         }
112         LOG_DEBUG("BPID: %d (0x%08" PRIx32 ") using hw wp: %d",
113                           breakpoint->unique_id,
114                           breakpoint->address,
115                           breakpoint->set );
116 }
117
118 /**
119  * Setup an ARM7/9 target's embedded ICE registers for software breakpoints.
120  *
121  * @param arm7_9 Pointer to common struct for ARM7/9 targets
122  * @return Error codes if there is a problem finding a watchpoint or the result
123  *         of executing the JTAG queue
124  */
125 static int arm7_9_set_software_breakpoints(struct arm7_9_common *arm7_9)
126 {
127         if (arm7_9->sw_breakpoints_added)
128         {
129                 return ERROR_OK;
130         }
131         if (arm7_9->wp_available < 1)
132         {
133                 LOG_WARNING("can't enable sw breakpoints with no watchpoint unit available");
134                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
135         }
136         arm7_9->wp_available--;
137
138         /* pick a breakpoint unit */
139         if (!arm7_9->wp0_used)
140         {
141                 arm7_9->sw_breakpoints_added = 1;
142                 arm7_9->wp0_used = 3;
143         } else if (!arm7_9->wp1_used)
144         {
145                 arm7_9->sw_breakpoints_added = 2;
146                 arm7_9->wp1_used = 3;
147         }
148         else
149         {
150                 LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
151                 return ERROR_FAIL;
152         }
153
154         if (arm7_9->sw_breakpoints_added == 1)
155         {
156                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], arm7_9->arm_bkpt);
157                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0x0);
158                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffffu);
159                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
160                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
161         }
162         else if (arm7_9->sw_breakpoints_added == 2)
163         {
164                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], arm7_9->arm_bkpt);
165                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0x0);
166                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0xffffffffu);
167                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
168                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
169         }
170         else
171         {
172                 LOG_ERROR("BUG: both watchpoints used, but wp_available >= 1");
173                 return ERROR_FAIL;
174         }
175         LOG_DEBUG("SW BP using hw wp: %d",
176                           arm7_9->sw_breakpoints_added );
177
178         return jtag_execute_queue();
179 }
180
181 /**
182  * Setup the common pieces for an ARM7/9 target after reset or on startup.
183  *
184  * @param target Pointer to an ARM7/9 target to setup
185  * @return Result of clearing the watchpoints on the target
186  */
187 static int arm7_9_setup(struct target *target)
188 {
189         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
190
191         return arm7_9_clear_watchpoints(arm7_9);
192 }
193
194 /**
195  * Set either a hardware or software breakpoint on an ARM7/9 target.  The
196  * breakpoint is set up even if it is already set.  Some actions, e.g. reset,
197  * might have erased the values in Embedded ICE.
198  *
199  * @param target Pointer to the target device to set the breakpoints on
200  * @param breakpoint Pointer to the breakpoint to be set
201  * @return For hardware breakpoints, this is the result of executing the JTAG
202  *         queue.  For software breakpoints, this will be the status of the
203  *         required memory reads and writes
204  */
205 static int arm7_9_set_breakpoint(struct target *target, struct breakpoint *breakpoint)
206 {
207         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
208         int retval = ERROR_OK;
209
210         LOG_DEBUG("BPID: %d, Address: 0x%08" PRIx32 ", Type: %d" ,
211                           breakpoint->unique_id,
212                           breakpoint->address,
213                           breakpoint->type);
214
215         if (target->state != TARGET_HALTED)
216         {
217                 LOG_WARNING("target not halted");
218                 return ERROR_TARGET_NOT_HALTED;
219         }
220
221         if (breakpoint->type == BKPT_HARD)
222         {
223                 /* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
224                 uint32_t mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
225
226                 /* reassign a hw breakpoint */
227                 if (breakpoint->set == 0)
228                 {
229                         arm7_9_assign_wp(arm7_9, breakpoint);
230                 }
231
232                 if (breakpoint->set == 1)
233                 {
234                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], breakpoint->address);
235                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
236                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffffu);
237                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
238                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
239                 }
240                 else if (breakpoint->set == 2)
241                 {
242                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], breakpoint->address);
243                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
244                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffffu);
245                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
246                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
247                 }
248                 else
249                 {
250                         LOG_ERROR("BUG: no hardware comparator available");
251                         return ERROR_OK;
252                 }
253
254                 retval = jtag_execute_queue();
255         }
256         else if (breakpoint->type == BKPT_SOFT)
257         {
258                 /* did we already set this breakpoint? */
259                 if (breakpoint->set)
260                         return ERROR_OK;
261
262                 if (breakpoint->length == 4)
263                 {
264                         uint32_t verify = 0xffffffff;
265                         /* keep the original instruction in target endianness */
266                         if ((retval = target_read_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
267                         {
268                                 return retval;
269                         }
270                         /* write the breakpoint instruction in target endianness (arm7_9->arm_bkpt is host endian) */
271                         if ((retval = target_write_u32(target, breakpoint->address, arm7_9->arm_bkpt)) != ERROR_OK)
272                         {
273                                 return retval;
274                         }
275
276                         if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
277                         {
278                                 return retval;
279                         }
280                         if (verify != arm7_9->arm_bkpt)
281                         {
282                                 LOG_ERROR("Unable to set 32 bit software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
283                                 return ERROR_OK;
284                         }
285                 }
286                 else
287                 {
288                         uint16_t verify = 0xffff;
289                         /* keep the original instruction in target endianness */
290                         if ((retval = target_read_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
291                         {
292                                 return retval;
293                         }
294                         /* write the breakpoint instruction in target endianness (arm7_9->thumb_bkpt is host endian) */
295                         if ((retval = target_write_u16(target, breakpoint->address, arm7_9->thumb_bkpt)) != ERROR_OK)
296                         {
297                                 return retval;
298                         }
299
300                         if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
301                         {
302                                 return retval;
303                         }
304                         if (verify != arm7_9->thumb_bkpt)
305                         {
306                                 LOG_ERROR("Unable to set thumb software breakpoint at address %08" PRIx32 " - check that memory is read/writable", breakpoint->address);
307                                 return ERROR_OK;
308                         }
309                 }
310
311                 if ((retval = arm7_9_set_software_breakpoints(arm7_9)) != ERROR_OK)
312                         return retval;
313
314                 arm7_9->sw_breakpoint_count++;
315
316                 breakpoint->set = 1;
317         }
318
319         return retval;
320 }
321
322 /**
323  * Unsets an existing breakpoint on an ARM7/9 target.  If it is a hardware
324  * breakpoint, the watchpoint used will be freed and the Embedded ICE registers
325  * will be updated.  Otherwise, the software breakpoint will be restored to its
326  * original instruction if it hasn't already been modified.
327  *
328  * @param target Pointer to ARM7/9 target to unset the breakpoint from
329  * @param breakpoint Pointer to breakpoint to be unset
330  * @return For hardware breakpoints, this is the result of executing the JTAG
331  *         queue.  For software breakpoints, this will be the status of the
332  *         required memory reads and writes
333  */
334 static int arm7_9_unset_breakpoint(struct target *target, struct breakpoint *breakpoint)
335 {
336         int retval = ERROR_OK;
337         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
338
339         LOG_DEBUG("BPID: %d, Address: 0x%08" PRIx32,
340                           breakpoint->unique_id,
341                           breakpoint->address );
342
343         if (!breakpoint->set)
344         {
345                 LOG_WARNING("breakpoint not set");
346                 return ERROR_OK;
347         }
348
349         if (breakpoint->type == BKPT_HARD)
350         {
351                 LOG_DEBUG("BPID: %d Releasing hw wp: %d",
352                                   breakpoint->unique_id,
353                                   breakpoint->set );
354                 if (breakpoint->set == 1)
355                 {
356                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
357                         arm7_9->wp0_used = 0;
358                         arm7_9->wp_available++;
359                 }
360                 else if (breakpoint->set == 2)
361                 {
362                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
363                         arm7_9->wp1_used = 0;
364                         arm7_9->wp_available++;
365                 }
366                 retval = jtag_execute_queue();
367                 breakpoint->set = 0;
368         }
369         else
370         {
371                 /* restore original instruction (kept in target endianness) */
372                 if (breakpoint->length == 4)
373                 {
374                         uint32_t current_instr;
375                         /* check that user program as not modified breakpoint instruction */
376                         if ((retval = target_read_memory(target, breakpoint->address, 4, 1, (uint8_t*)&current_instr)) != ERROR_OK)
377                         {
378                                 return retval;
379                         }
380                         current_instr = target_buffer_get_u32(target, (uint8_t *)&current_instr);
381                         if (current_instr == arm7_9->arm_bkpt)
382                                 if ((retval = target_write_memory(target, breakpoint->address, 4, 1, breakpoint->orig_instr)) != ERROR_OK)
383                                 {
384                                         return retval;
385                                 }
386                 }
387                 else
388                 {
389                         uint16_t current_instr;
390                         /* check that user program as not modified breakpoint instruction */
391                         if ((retval = target_read_memory(target, breakpoint->address, 2, 1, (uint8_t*)&current_instr)) != ERROR_OK)
392                         {
393                                 return retval;
394                         }
395                         if (current_instr == arm7_9->thumb_bkpt)
396                                 if ((retval = target_write_memory(target, breakpoint->address, 2, 1, breakpoint->orig_instr)) != ERROR_OK)
397                                 {
398                                         return retval;
399                                 }
400                 }
401
402                 if (--arm7_9->sw_breakpoint_count==0)
403                 {
404                         /* We have removed the last sw breakpoint, clear the hw breakpoint we used to implement it */
405                         if (arm7_9->sw_breakpoints_added == 1)
406                         {
407                                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0);
408                         }
409                         else if (arm7_9->sw_breakpoints_added == 2)
410                         {
411                                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0);
412                         }
413                 }
414
415                 breakpoint->set = 0;
416         }
417
418         return retval;
419 }
420
421 /**
422  * Add a breakpoint to an ARM7/9 target.  This makes sure that there are no
423  * dangling breakpoints and that the desired breakpoint can be added.
424  *
425  * @param target Pointer to the target ARM7/9 device to add a breakpoint to
426  * @param breakpoint Pointer to the breakpoint to be added
427  * @return An error status if there is a problem adding the breakpoint or the
428  *         result of setting the breakpoint
429  */
430 int arm7_9_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
431 {
432         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
433
434         if (arm7_9->breakpoint_count == 0)
435         {
436                 /* make sure we don't have any dangling breakpoints. This is vital upon
437                  * GDB connect/disconnect
438                  */
439                 arm7_9_clear_watchpoints(arm7_9);
440         }
441
442         if ((breakpoint->type == BKPT_HARD) && (arm7_9->wp_available < 1))
443         {
444                 LOG_INFO("no watchpoint unit available for hardware breakpoint");
445                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
446         }
447
448         if ((breakpoint->length != 2) && (breakpoint->length != 4))
449         {
450                 LOG_INFO("only breakpoints of two (Thumb) or four (ARM) bytes length supported");
451                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
452         }
453
454         if (breakpoint->type == BKPT_HARD)
455         {
456                 arm7_9_assign_wp(arm7_9, breakpoint);
457         }
458
459         arm7_9->breakpoint_count++;
460
461         return arm7_9_set_breakpoint(target, breakpoint);
462 }
463
464 /**
465  * Removes a breakpoint from an ARM7/9 target.  This will make sure there are no
466  * dangling breakpoints and updates available watchpoints if it is a hardware
467  * breakpoint.
468  *
469  * @param target Pointer to the target to have a breakpoint removed
470  * @param breakpoint Pointer to the breakpoint to be removed
471  * @return Error status if there was a problem unsetting the breakpoint or the
472  *         watchpoints could not be cleared
473  */
474 int arm7_9_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
475 {
476         int retval = ERROR_OK;
477         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
478
479         if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
480         {
481                 return retval;
482         }
483
484         if (breakpoint->type == BKPT_HARD)
485                 arm7_9->wp_available++;
486
487         arm7_9->breakpoint_count--;
488         if (arm7_9->breakpoint_count == 0)
489         {
490                 /* make sure we don't have any dangling breakpoints */
491                 if ((retval = arm7_9_clear_watchpoints(arm7_9)) != ERROR_OK)
492                 {
493                         return retval;
494                 }
495         }
496
497         return ERROR_OK;
498 }
499
500 /**
501  * Sets a watchpoint for an ARM7/9 target in one of the watchpoint units.  It is
502  * considered a bug to call this function when there are no available watchpoint
503  * units.
504  *
505  * @param target Pointer to an ARM7/9 target to set a watchpoint on
506  * @param watchpoint Pointer to the watchpoint to be set
507  * @return Error status if watchpoint set fails or the result of executing the
508  *         JTAG queue
509  */
510 static int arm7_9_set_watchpoint(struct target *target, struct watchpoint *watchpoint)
511 {
512         int retval = ERROR_OK;
513         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
514         int rw_mask = 1;
515         uint32_t mask;
516
517         mask = watchpoint->length - 1;
518
519         if (target->state != TARGET_HALTED)
520         {
521                 LOG_WARNING("target not halted");
522                 return ERROR_TARGET_NOT_HALTED;
523         }
524
525         if (watchpoint->rw == WPT_ACCESS)
526                 rw_mask = 0;
527         else
528                 rw_mask = 1;
529
530         if (!arm7_9->wp0_used)
531         {
532                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], watchpoint->address);
533                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], mask);
534                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], watchpoint->mask);
535                 if (watchpoint->mask != 0xffffffffu)
536                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_VALUE], watchpoint->value);
537                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
538                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
539
540                 if ((retval = jtag_execute_queue()) != ERROR_OK)
541                 {
542                         return retval;
543                 }
544                 watchpoint->set = 1;
545                 arm7_9->wp0_used = 2;
546         }
547         else if (!arm7_9->wp1_used)
548         {
549                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], watchpoint->address);
550                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], mask);
551                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], watchpoint->mask);
552                 if (watchpoint->mask != 0xffffffffu)
553                         embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_VALUE], watchpoint->value);
554                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], 0xff & ~EICE_W_CTRL_nOPC & ~rw_mask);
555                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE | EICE_W_CTRL_nOPC | (watchpoint->rw & 1));
556
557                 if ((retval = jtag_execute_queue()) != ERROR_OK)
558                 {
559                         return retval;
560                 }
561                 watchpoint->set = 2;
562                 arm7_9->wp1_used = 2;
563         }
564         else
565         {
566                 LOG_ERROR("BUG: no hardware comparator available");
567                 return ERROR_OK;
568         }
569
570         return ERROR_OK;
571 }
572
573 /**
574  * Unset an existing watchpoint and clear the used watchpoint unit.
575  *
576  * @param target Pointer to the target to have the watchpoint removed
577  * @param watchpoint Pointer to the watchpoint to be removed
578  * @return Error status while trying to unset the watchpoint or the result of
579  *         executing the JTAG queue
580  */
581 static int arm7_9_unset_watchpoint(struct target *target, struct watchpoint *watchpoint)
582 {
583         int retval = ERROR_OK;
584         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
585
586         if (target->state != TARGET_HALTED)
587         {
588                 LOG_WARNING("target not halted");
589                 return ERROR_TARGET_NOT_HALTED;
590         }
591
592         if (!watchpoint->set)
593         {
594                 LOG_WARNING("breakpoint not set");
595                 return ERROR_OK;
596         }
597
598         if (watchpoint->set == 1)
599         {
600                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
601                 if ((retval = jtag_execute_queue()) != ERROR_OK)
602                 {
603                         return retval;
604                 }
605                 arm7_9->wp0_used = 0;
606         }
607         else if (watchpoint->set == 2)
608         {
609                 embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
610                 if ((retval = jtag_execute_queue()) != ERROR_OK)
611                 {
612                         return retval;
613                 }
614                 arm7_9->wp1_used = 0;
615         }
616         watchpoint->set = 0;
617
618         return ERROR_OK;
619 }
620
621 /**
622  * Add a watchpoint to an ARM7/9 target.  If there are no watchpoint units
623  * available, an error response is returned.
624  *
625  * @param target Pointer to the ARM7/9 target to add a watchpoint to
626  * @param watchpoint Pointer to the watchpoint to be added
627  * @return Error status while trying to add the watchpoint
628  */
629 int arm7_9_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
630 {
631         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
632
633         if (arm7_9->wp_available < 1)
634         {
635                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
636         }
637
638         if ((watchpoint->length != 1) && (watchpoint->length != 2) && (watchpoint->length != 4))
639         {
640                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
641         }
642
643         arm7_9->wp_available--;
644
645         return ERROR_OK;
646 }
647
648 /**
649  * Remove a watchpoint from an ARM7/9 target.  The watchpoint will be unset and
650  * the used watchpoint unit will be reopened.
651  *
652  * @param target Pointer to the target to remove a watchpoint from
653  * @param watchpoint Pointer to the watchpoint to be removed
654  * @return Result of trying to unset the watchpoint
655  */
656 int arm7_9_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
657 {
658         int retval = ERROR_OK;
659         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
660
661         if (watchpoint->set)
662         {
663                 if ((retval = arm7_9_unset_watchpoint(target, watchpoint)) != ERROR_OK)
664                 {
665                         return retval;
666                 }
667         }
668
669         arm7_9->wp_available++;
670
671         return ERROR_OK;
672 }
673
674 /**
675  * Restarts the target by sending a RESTART instruction and moving the JTAG
676  * state to IDLE.  This includes a timeout waiting for DBGACK and SYSCOMP to be
677  * asserted by the processor.
678  *
679  * @param target Pointer to target to issue commands to
680  * @return Error status if there is a timeout or a problem while executing the
681  *         JTAG queue
682  */
683 int arm7_9_execute_sys_speed(struct target *target)
684 {
685         int retval;
686         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
687         struct arm_jtag *jtag_info = &arm7_9->jtag_info;
688         struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
689
690         /* set RESTART instruction */
691         if (arm7_9->need_bypass_before_restart) {
692                 arm7_9->need_bypass_before_restart = 0;
693                 retval = arm_jtag_set_instr(jtag_info, 0xf, NULL, TAP_IDLE);
694                 if (retval != ERROR_OK)
695                         return retval;
696         }
697         retval = arm_jtag_set_instr(jtag_info, 0x4, NULL, TAP_IDLE);
698         if (retval != ERROR_OK)
699                 return retval;
700
701         long long then = timeval_ms();
702         int timeout;
703         while (!(timeout = ((timeval_ms()-then) > 1000)))
704         {
705                 /* read debug status register */
706                 embeddedice_read_reg(dbg_stat);
707                 if ((retval = jtag_execute_queue()) != ERROR_OK)
708                         return retval;
709                 if ((buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
710                                    && (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_SYSCOMP, 1)))
711                         break;
712                 if (debug_level >= 3)
713                 {
714                         alive_sleep(100);
715                 } else
716                 {
717                         keep_alive();
718                 }
719         }
720         if (timeout)
721         {
722                 LOG_ERROR("timeout waiting for SYSCOMP & DBGACK, last DBG_STATUS: %" PRIx32 "", buf_get_u32(dbg_stat->value, 0, dbg_stat->size));
723                 return ERROR_TARGET_TIMEOUT;
724         }
725
726         return ERROR_OK;
727 }
728
729 /**
730  * Restarts the target by sending a RESTART instruction and moving the JTAG
731  * state to IDLE.  This validates that DBGACK and SYSCOMP are set without
732  * waiting until they are.
733  *
734  * @param target Pointer to the target to issue commands to
735  * @return Always ERROR_OK
736  */
737 static int arm7_9_execute_fast_sys_speed(struct target *target)
738 {
739         static int set = 0;
740         static uint8_t check_value[4], check_mask[4];
741
742         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
743         struct arm_jtag *jtag_info = &arm7_9->jtag_info;
744         struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
745         int retval;
746
747         /* set RESTART instruction */
748         if (arm7_9->need_bypass_before_restart) {
749                 arm7_9->need_bypass_before_restart = 0;
750                 retval = arm_jtag_set_instr(jtag_info, 0xf, NULL, TAP_IDLE);
751                 if (retval != ERROR_OK)
752                         return retval;
753         }
754         retval = arm_jtag_set_instr(jtag_info, 0x4, NULL, TAP_IDLE);
755         if (retval != ERROR_OK)
756                 return retval;
757
758         if (!set)
759         {
760                 /* check for DBGACK and SYSCOMP set (others don't care) */
761
762                 /* NB! These are constants that must be available until after next jtag_execute() and
763                  * we evaluate the values upon first execution in lieu of setting up these constants
764                  * during early setup.
765                  * */
766                 buf_set_u32(check_value, 0, 32, 0x9);
767                 buf_set_u32(check_mask, 0, 32, 0x9);
768                 set = 1;
769         }
770
771         /* read debug status register */
772         embeddedice_read_reg_w_check(dbg_stat, check_value, check_mask);
773
774         return ERROR_OK;
775 }
776
777 /**
778  * Get some data from the ARM7/9 target.
779  *
780  * @param target Pointer to the ARM7/9 target to read data from
781  * @param size The number of 32bit words to be read
782  * @param buffer Pointer to the buffer that will hold the data
783  * @return The result of receiving data from the Embedded ICE unit
784  */
785 int arm7_9_target_request_data(struct target *target, uint32_t size, uint8_t *buffer)
786 {
787         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
788         struct arm_jtag *jtag_info = &arm7_9->jtag_info;
789         uint32_t *data;
790         int retval = ERROR_OK;
791         uint32_t i;
792
793         data = malloc(size * (sizeof(uint32_t)));
794
795         retval = embeddedice_receive(jtag_info, data, size);
796
797         /* return the 32-bit ints in the 8-bit array */
798         for (i = 0; i < size; i++)
799         {
800                 h_u32_to_le(buffer + (i * 4), data[i]);
801         }
802
803         free(data);
804
805         return retval;
806 }
807
808 /**
809  * Handles requests to an ARM7/9 target.  If debug messaging is enabled, the
810  * target is running and the DCC control register has the W bit high, this will
811  * execute the request on the target.
812  *
813  * @param priv Void pointer expected to be a struct target pointer
814  * @return ERROR_OK unless there are issues with the JTAG queue or when reading
815  *                  from the Embedded ICE unit
816  */
817 static int arm7_9_handle_target_request(void *priv)
818 {
819         int retval = ERROR_OK;
820         struct target *target = priv;
821         if (!target_was_examined(target))
822                 return ERROR_OK;
823         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
824         struct arm_jtag *jtag_info = &arm7_9->jtag_info;
825         struct reg *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
826
827         if (!target->dbg_msg_enabled)
828                 return ERROR_OK;
829
830         if (target->state == TARGET_RUNNING)
831         {
832                 /* read DCC control register */
833                 embeddedice_read_reg(dcc_control);
834                 if ((retval = jtag_execute_queue()) != ERROR_OK)
835                 {
836                         return retval;
837                 }
838
839                 /* check W bit */
840                 if (buf_get_u32(dcc_control->value, 1, 1) == 1)
841                 {
842                         uint32_t request;
843
844                         if ((retval = embeddedice_receive(jtag_info, &request, 1)) != ERROR_OK)
845                         {
846                                 return retval;
847                         }
848                         if ((retval = target_request(target, request)) != ERROR_OK)
849                         {
850                                 return retval;
851                         }
852                 }
853         }
854
855         return ERROR_OK;
856 }
857
858 /**
859  * Polls an ARM7/9 target for its current status.  If DBGACK is set, the target
860  * is manipulated to the right halted state based on its current state.  This is
861  * what happens:
862  *
863  * <table>
864  *              <tr><th > State</th><th > Action</th></tr>
865  *              <tr><td > TARGET_RUNNING | TARGET_RESET</td><td > Enters debug mode.  If TARGET_RESET, pc may be checked</td></tr>
866  *              <tr><td > TARGET_UNKNOWN</td><td > Warning is logged</td></tr>
867  *              <tr><td > TARGET_DEBUG_RUNNING</td><td > Enters debug mode</td></tr>
868  *              <tr><td > TARGET_HALTED</td><td > Nothing</td></tr>
869  * </table>
870  *
871  * If the target does not end up in the halted state, a warning is produced.  If
872  * DBGACK is cleared, then the target is expected to either be running or
873  * running in debug.
874  *
875  * @param target Pointer to the ARM7/9 target to poll
876  * @return ERROR_OK or an error status if a command fails
877  */
878 int arm7_9_poll(struct target *target)
879 {
880         int retval;
881         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
882         struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
883
884         /* read debug status register */
885         embeddedice_read_reg(dbg_stat);
886         if ((retval = jtag_execute_queue()) != ERROR_OK)
887         {
888                 return retval;
889         }
890
891         if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1))
892         {
893 /*              LOG_DEBUG("DBGACK set, dbg_state->value: 0x%x", buf_get_u32(dbg_stat->value, 0, 32));*/
894                 if (target->state == TARGET_UNKNOWN)
895                 {
896                         /* Starting OpenOCD with target in debug-halt */
897                         target->state = TARGET_RUNNING;
898                         LOG_DEBUG("DBGACK already set during server startup.");
899                 }
900                 if ((target->state == TARGET_RUNNING) || (target->state == TARGET_RESET))
901                 {
902                         target->state = TARGET_HALTED;
903
904                         if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
905                                 return retval;
906
907                         if (arm_semihosting(target, &retval) != 0)
908                                 return retval;
909
910                         if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
911                         {
912                                 return retval;
913                         }
914                 }
915                 if (target->state == TARGET_DEBUG_RUNNING)
916                 {
917                         target->state = TARGET_HALTED;
918                         if ((retval = arm7_9_debug_entry(target)) != ERROR_OK)
919                                 return retval;
920
921                         if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED)) != ERROR_OK)
922                         {
923                                 return retval;
924                         }
925                 }
926                 if (target->state != TARGET_HALTED)
927                 {
928                         LOG_WARNING("DBGACK set, but the target did not end up in the halted state %d", target->state);
929                 }
930         }
931         else
932         {
933                 if (target->state != TARGET_DEBUG_RUNNING)
934                         target->state = TARGET_RUNNING;
935         }
936
937         return ERROR_OK;
938 }
939
940 /**
941  * Asserts the reset (SRST) on an ARM7/9 target.  Some -S targets (ARM966E-S in
942  * the STR912 isn't affected, ARM926EJ-S in the LPC3180 and AT91SAM9260 is
943  * affected) completely stop the JTAG clock while the core is held in reset
944  * (SRST).  It isn't possible to program the halt condition once reset is
945  * asserted, hence a hook that allows the target to set up its reset-halt
946  * condition is setup prior to asserting reset.
947  *
948  * @param target Pointer to an ARM7/9 target to assert reset on
949  * @return ERROR_FAIL if the JTAG device does not have SRST, otherwise ERROR_OK
950  */
951 int arm7_9_assert_reset(struct target *target)
952 {
953         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
954         enum reset_types jtag_reset_config = jtag_get_reset_config();
955         bool use_event = false;
956
957         LOG_DEBUG("target->state: %s",
958                   target_state_name(target));
959
960         if (target_has_event_action(target, TARGET_EVENT_RESET_ASSERT))
961                 use_event = true;
962         else if (!(jtag_reset_config & RESET_HAS_SRST)) {
963                 LOG_ERROR("%s: how to reset?", target_name(target));
964                 return ERROR_FAIL;
965         }
966
967         /* At this point trst has been asserted/deasserted once. We would
968          * like to program EmbeddedICE while SRST is asserted, instead of
969          * depending on SRST to leave that module alone.  However, many CPUs
970          * gate the JTAG clock while SRST is asserted; or JTAG may need
971          * clock stability guarantees (adaptive clocking might help).
972          *
973          * So we assume JTAG access during SRST is off the menu unless it's
974          * been specifically enabled.
975          */
976         bool srst_asserted = false;
977
978         if (!use_event
979                         && !(jtag_reset_config & RESET_SRST_PULLS_TRST)
980                         && (jtag_reset_config & RESET_SRST_NO_GATING))
981         {
982                 jtag_add_reset(0, 1);
983                 srst_asserted = true;
984         }
985
986         if (target->reset_halt)
987         {
988                 /*
989                  * For targets that don't support communication while SRST is
990                  * asserted, we need to set up the reset vector catch first.
991                  *
992                  * When we use TRST+SRST and that's equivalent to a power-up
993                  * reset, these settings may well be reset anyway; so setting
994                  * them here won't matter.
995                  */
996                 if (arm7_9->has_vector_catch)
997                 {
998                         /* program vector catch register to catch reset */
999                         embeddedice_write_reg(&arm7_9->eice_cache
1000                                         ->reg_list[EICE_VEC_CATCH], 0x1);
1001
1002                         /* extra runtest added as issues were found with
1003                          * certain ARM9 cores (maybe more) - AT91SAM9260
1004                          * and STR9
1005                          */
1006                         jtag_add_runtest(1, TAP_IDLE);
1007                 }
1008                 else
1009                 {
1010                         /* program watchpoint unit to match on reset vector
1011                          * address
1012                          */
1013                         embeddedice_write_reg(&arm7_9->eice_cache
1014                                         ->reg_list[EICE_W0_ADDR_VALUE], 0x0);
1015                         embeddedice_write_reg(&arm7_9->eice_cache
1016                                         ->reg_list[EICE_W0_ADDR_MASK], 0x3);
1017                         embeddedice_write_reg(&arm7_9->eice_cache
1018                                         ->reg_list[EICE_W0_DATA_MASK],
1019                                                 0xffffffff);
1020                         embeddedice_write_reg(&arm7_9->eice_cache
1021                                         ->reg_list[EICE_W0_CONTROL_VALUE],
1022                                                 EICE_W_CTRL_ENABLE);
1023                         embeddedice_write_reg(&arm7_9->eice_cache
1024                                         ->reg_list[EICE_W0_CONTROL_MASK],
1025                                                 ~EICE_W_CTRL_nOPC & 0xff);
1026                 }
1027         }
1028
1029         if (use_event) {
1030                 target_handle_event(target, TARGET_EVENT_RESET_ASSERT);
1031         } else {
1032                 /* If we use SRST ... we'd like to issue just SRST, but the
1033                  * board or chip may be set up so we have to assert TRST as
1034                  * well.  On some chips that combination is equivalent to a
1035                  * power-up reset, and generally clobbers EICE state.
1036                  */
1037                 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1038                         jtag_add_reset(1, 1);
1039                 else if (!srst_asserted)
1040                         jtag_add_reset(0, 1);
1041                 jtag_add_sleep(50000);
1042         }
1043
1044         target->state = TARGET_RESET;
1045         register_cache_invalidate(arm7_9->armv4_5_common.core_cache);
1046
1047         /* REVISIT why isn't standard debug entry logic sufficient?? */
1048         if (target->reset_halt
1049                         && (!(jtag_reset_config & RESET_SRST_PULLS_TRST)
1050                                 || use_event))
1051         {
1052                 /* debug entry was prepared above */
1053                 target->debug_reason = DBG_REASON_DBGRQ;
1054         }
1055
1056         return ERROR_OK;
1057 }
1058
1059 /**
1060  * Deassert the reset (SRST) signal on an ARM7/9 target.  If SRST pulls TRST
1061  * and the target is being reset into a halt, a warning will be triggered
1062  * because it is not possible to reset into a halted mode in this case.  The
1063  * target is halted using the target's functions.
1064  *
1065  * @param target Pointer to the target to have the reset deasserted
1066  * @return ERROR_OK or an error from polling or halting the target
1067  */
1068 int arm7_9_deassert_reset(struct target *target)
1069 {
1070         int retval = ERROR_OK;
1071         LOG_DEBUG("target->state: %s",
1072                 target_state_name(target));
1073
1074         /* deassert reset lines */
1075         jtag_add_reset(0, 0);
1076
1077         enum reset_types jtag_reset_config = jtag_get_reset_config();
1078         if (target->reset_halt && (jtag_reset_config & RESET_SRST_PULLS_TRST) != 0)
1079         {
1080                 LOG_WARNING("srst pulls trst - can not reset into halted mode. Issuing halt after reset.");
1081                 /* set up embedded ice registers again */
1082                 if ((retval = target_examine_one(target)) != ERROR_OK)
1083                         return retval;
1084
1085                 if ((retval = target_poll(target)) != ERROR_OK)
1086                 {
1087                         return retval;
1088                 }
1089
1090                 if ((retval = target_halt(target)) != ERROR_OK)
1091                 {
1092                         return retval;
1093                 }
1094
1095         }
1096         return retval;
1097 }
1098
1099 /**
1100  * Clears the halt condition for an ARM7/9 target.  If it isn't coming out of
1101  * reset and if DBGRQ is used, it is progammed to be deasserted.  If the reset
1102  * vector catch was used, it is restored.  Otherwise, the control value is
1103  * restored and the watchpoint unit is restored if it was in use.
1104  *
1105  * @param target Pointer to the ARM7/9 target to have halt cleared
1106  * @return Always ERROR_OK
1107  */
1108 static int arm7_9_clear_halt(struct target *target)
1109 {
1110         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1111         struct reg *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1112
1113         /* we used DBGRQ only if we didn't come out of reset */
1114         if (!arm7_9->debug_entry_from_reset && arm7_9->use_dbgrq)
1115         {
1116                 /* program EmbeddedICE Debug Control Register to deassert DBGRQ
1117                  */
1118                 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1119                 embeddedice_store_reg(dbg_ctrl);
1120         }
1121         else
1122         {
1123                 if (arm7_9->debug_entry_from_reset && arm7_9->has_vector_catch)
1124                 {
1125                         /* if we came out of reset, and vector catch is supported, we used
1126                          * vector catch to enter debug state
1127                          * restore the register in that case
1128                          */
1129                         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_VEC_CATCH]);
1130                 }
1131                 else
1132                 {
1133                         /* restore registers if watchpoint unit 0 was in use
1134                          */
1135                         if (arm7_9->wp0_used)
1136                         {
1137                                 if (arm7_9->debug_entry_from_reset)
1138                                 {
1139                                         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE]);
1140                                 }
1141                                 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
1142                                 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
1143                                 embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
1144                         }
1145                         /* control value always has to be restored, as it was either disabled,
1146                          * or enabled with possibly different bits
1147                          */
1148                         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
1149                 }
1150         }
1151
1152         return ERROR_OK;
1153 }
1154
1155 /**
1156  * Issue a software reset and halt to an ARM7/9 target.  The target is halted
1157  * and then there is a wait until the processor shows the halt.  This wait can
1158  * timeout and results in an error being returned.  The software reset involves
1159  * clearing the halt, updating the debug control register, changing to ARM mode,
1160  * reset of the program counter, and reset of all of the registers.
1161  *
1162  * @param target Pointer to the ARM7/9 target to be reset and halted by software
1163  * @return Error status if any of the commands fail, otherwise ERROR_OK
1164  */
1165 int arm7_9_soft_reset_halt(struct target *target)
1166 {
1167         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1168         struct arm *armv4_5 = &arm7_9->armv4_5_common;
1169         struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
1170         struct reg *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1171         int i;
1172         int retval;
1173
1174         /* FIX!!! replace some of this code with tcl commands
1175          *
1176          * halt # the halt command is synchronous
1177          * armv4_5 core_state arm
1178          *
1179          */
1180
1181         if ((retval = target_halt(target)) != ERROR_OK)
1182                 return retval;
1183
1184         long long then = timeval_ms();
1185         int timeout;
1186         while (!(timeout = ((timeval_ms()-then) > 1000)))
1187         {
1188                 if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_DBGACK, 1) != 0)
1189                         break;
1190                 embeddedice_read_reg(dbg_stat);
1191                 if ((retval = jtag_execute_queue()) != ERROR_OK)
1192                         return retval;
1193                 if (debug_level >= 3)
1194                 {
1195                         alive_sleep(100);
1196                 } else
1197                 {
1198                         keep_alive();
1199                 }
1200         }
1201         if (timeout)
1202         {
1203                 LOG_ERROR("Failed to halt CPU after 1 sec");
1204                 return ERROR_TARGET_TIMEOUT;
1205         }
1206         target->state = TARGET_HALTED;
1207
1208         /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
1209          * ensure that DBGRQ is cleared
1210          */
1211         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
1212         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1213         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
1214         embeddedice_store_reg(dbg_ctrl);
1215
1216         if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
1217         {
1218                 return retval;
1219         }
1220
1221         /* if the target is in Thumb state, change to ARM state */
1222         if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
1223         {
1224                 uint32_t r0_thumb, pc_thumb;
1225                 LOG_DEBUG("target entered debug from Thumb state, changing to ARM");
1226                 /* Entered debug from Thumb mode */
1227                 armv4_5->core_state = ARM_STATE_THUMB;
1228                 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
1229         }
1230
1231         /* REVISIT likewise for bit 5 -- switch Jazelle-to-ARM */
1232
1233         /* all register content is now invalid */
1234         register_cache_invalidate(armv4_5->core_cache);
1235
1236         /* SVC, ARM state, IRQ and FIQ disabled */
1237         uint32_t cpsr;
1238
1239         cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 32);
1240         cpsr &= ~0xff;
1241         cpsr |= 0xd3;
1242         arm_set_cpsr(armv4_5, cpsr);
1243         armv4_5->cpsr->dirty = 1;
1244
1245         /* start fetching from 0x0 */
1246         buf_set_u32(armv4_5->pc->value, 0, 32, 0x0);
1247         armv4_5->pc->dirty = 1;
1248         armv4_5->pc->valid = 1;
1249
1250         /* reset registers */
1251         for (i = 0; i <= 14; i++)
1252         {
1253                 struct reg *r = arm_reg_current(armv4_5, i);
1254
1255                 buf_set_u32(r->value, 0, 32, 0xffffffff);
1256                 r->dirty = 1;
1257                 r->valid = 1;
1258         }
1259
1260         if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
1261         {
1262                 return retval;
1263         }
1264
1265         return ERROR_OK;
1266 }
1267
1268 /**
1269  * Halt an ARM7/9 target.  This is accomplished by either asserting the DBGRQ
1270  * line or by programming a watchpoint to trigger on any address.  It is
1271  * considered a bug to call this function while the target is in the
1272  * TARGET_RESET state.
1273  *
1274  * @param target Pointer to the ARM7/9 target to be halted
1275  * @return Always ERROR_OK
1276  */
1277 int arm7_9_halt(struct target *target)
1278 {
1279         if (target->state == TARGET_RESET)
1280         {
1281                 LOG_ERROR("BUG: arm7/9 does not support halt during reset. This is handled in arm7_9_assert_reset()");
1282                 return ERROR_OK;
1283         }
1284
1285         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1286         struct reg *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1287
1288         LOG_DEBUG("target->state: %s",
1289                   target_state_name(target));
1290
1291         if (target->state == TARGET_HALTED)
1292         {
1293                 LOG_DEBUG("target was already halted");
1294                 return ERROR_OK;
1295         }
1296
1297         if (target->state == TARGET_UNKNOWN)
1298         {
1299                 LOG_WARNING("target was in unknown state when halt was requested");
1300         }
1301
1302         if (arm7_9->use_dbgrq)
1303         {
1304                 /* program EmbeddedICE Debug Control Register to assert DBGRQ
1305                  */
1306                 if (arm7_9->set_special_dbgrq) {
1307                         arm7_9->set_special_dbgrq(target);
1308                 } else {
1309                         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 1);
1310                         embeddedice_store_reg(dbg_ctrl);
1311                 }
1312         }
1313         else
1314         {
1315                 /* program watchpoint unit to match on any address
1316                  */
1317                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1318                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1319                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1320                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1321         }
1322
1323         target->debug_reason = DBG_REASON_DBGRQ;
1324
1325         return ERROR_OK;
1326 }
1327
1328 /**
1329  * Handle an ARM7/9 target's entry into debug mode.  The halt is cleared on the
1330  * ARM.  The JTAG queue is then executed and the reason for debug entry is
1331  * examined.  Once done, the target is verified to be halted and the processor
1332  * is forced into ARM mode.  The core registers are saved for the current core
1333  * mode and the program counter (register 15) is updated as needed.  The core
1334  * registers and CPSR and SPSR are saved for restoration later.
1335  *
1336  * @param target Pointer to target that is entering debug mode
1337  * @return Error code if anything fails, otherwise ERROR_OK
1338  */
1339 static int arm7_9_debug_entry(struct target *target)
1340 {
1341         int i;
1342         uint32_t context[16];
1343         uint32_t* context_p[16];
1344         uint32_t r0_thumb, pc_thumb;
1345         uint32_t cpsr, cpsr_mask = 0;
1346         int retval;
1347         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1348         struct arm *armv4_5 = &arm7_9->armv4_5_common;
1349         struct reg *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
1350         struct reg *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1351
1352 #ifdef _DEBUG_ARM7_9_
1353         LOG_DEBUG("-");
1354 #endif
1355
1356         /* program EmbeddedICE Debug Control Register to assert DBGACK and INTDIS
1357          * ensure that DBGRQ is cleared
1358          */
1359         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
1360         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGRQ, 1, 0);
1361         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 1);
1362         embeddedice_store_reg(dbg_ctrl);
1363
1364         if ((retval = arm7_9_clear_halt(target)) != ERROR_OK)
1365         {
1366                 return retval;
1367         }
1368
1369         if ((retval = jtag_execute_queue()) != ERROR_OK)
1370         {
1371                 return retval;
1372         }
1373
1374         if ((retval = arm7_9->examine_debug_reason(target)) != ERROR_OK)
1375                 return retval;
1376
1377
1378         if (target->state != TARGET_HALTED)
1379         {
1380                 LOG_WARNING("target not halted");
1381                 return ERROR_TARGET_NOT_HALTED;
1382         }
1383
1384         /* if the target is in Thumb state, change to ARM state */
1385         if (buf_get_u32(dbg_stat->value, EICE_DBG_STATUS_ITBIT, 1))
1386         {
1387                 LOG_DEBUG("target entered debug from Thumb state");
1388                 /* Entered debug from Thumb mode */
1389                 armv4_5->core_state = ARM_STATE_THUMB;
1390                 cpsr_mask = 1 << 5;
1391                 arm7_9->change_to_arm(target, &r0_thumb, &pc_thumb);
1392                 LOG_DEBUG("r0_thumb: 0x%8.8" PRIx32
1393                         ", pc_thumb: 0x%8.8" PRIx32, r0_thumb, pc_thumb);
1394         } else if (buf_get_u32(dbg_stat->value, 5, 1)) {
1395                 /* \todo Get some vaguely correct handling of Jazelle, if
1396                  * anyone ever uses it and full info becomes available.
1397                  * See ARM9EJS TRM B.7.1 for how to switch J->ARM; and
1398                  * B.7.3 for the reverse.  That'd be the bare minimum...
1399                  */
1400                 LOG_DEBUG("target entered debug from Jazelle state");
1401                 armv4_5->core_state = ARM_STATE_JAZELLE;
1402                 cpsr_mask = 1 << 24;
1403                 LOG_ERROR("Jazelle debug entry -- BROKEN!");
1404         } else {
1405                 LOG_DEBUG("target entered debug from ARM state");
1406                 /* Entered debug from ARM mode */
1407                 armv4_5->core_state = ARM_STATE_ARM;
1408         }
1409
1410         for (i = 0; i < 16; i++)
1411                 context_p[i] = &context[i];
1412         /* save core registers (r0 - r15 of current core mode) */
1413         arm7_9->read_core_regs(target, 0xffff, context_p);
1414
1415         arm7_9->read_xpsr(target, &cpsr, 0);
1416
1417         if ((retval = jtag_execute_queue()) != ERROR_OK)
1418                 return retval;
1419
1420         /* Sync our CPSR copy with J or T bits EICE reported, but
1421          * which we then erased by putting the core into ARM mode.
1422          */
1423         arm_set_cpsr(armv4_5, cpsr | cpsr_mask);
1424
1425         if (!is_arm_mode(armv4_5->core_mode))
1426         {
1427                 target->state = TARGET_UNKNOWN;
1428                 LOG_ERROR("cpsr contains invalid mode value - communication failure");
1429                 return ERROR_TARGET_FAILURE;
1430         }
1431
1432         LOG_DEBUG("target entered debug state in %s mode",
1433                          arm_mode_name(armv4_5->core_mode));
1434
1435         if (armv4_5->core_state == ARM_STATE_THUMB)
1436         {
1437                 LOG_DEBUG("thumb state, applying fixups");
1438                 context[0] = r0_thumb;
1439                 context[15] = pc_thumb;
1440         } else if (armv4_5->core_state == ARM_STATE_ARM)
1441         {
1442                 /* adjust value stored by STM */
1443                 context[15] -= 3 * 4;
1444         }
1445
1446         if ((target->debug_reason != DBG_REASON_DBGRQ) || (!arm7_9->use_dbgrq))
1447                 context[15] -= 3 * ((armv4_5->core_state == ARM_STATE_ARM) ? 4 : 2);
1448         else
1449                 context[15] -= arm7_9->dbgreq_adjust_pc * ((armv4_5->core_state == ARM_STATE_ARM) ? 4 : 2);
1450
1451         for (i = 0; i <= 15; i++)
1452         {
1453                 struct reg *r = arm_reg_current(armv4_5, i);
1454
1455                 LOG_DEBUG("r%i: 0x%8.8" PRIx32 "", i, context[i]);
1456
1457                 buf_set_u32(r->value, 0, 32, context[i]);
1458                 /* r0 and r15 (pc) have to be restored later */
1459                 r->dirty = (i == 0) || (i == 15);
1460                 r->valid = 1;
1461         }
1462
1463         LOG_DEBUG("entered debug state at PC 0x%" PRIx32 "", context[15]);
1464
1465         /* exceptions other than USR & SYS have a saved program status register */
1466         if (armv4_5->spsr) {
1467                 uint32_t spsr;
1468                 arm7_9->read_xpsr(target, &spsr, 1);
1469                 if ((retval = jtag_execute_queue()) != ERROR_OK)
1470                 {
1471                         return retval;
1472                 }
1473                 buf_set_u32(armv4_5->spsr->value, 0, 32, spsr);
1474                 armv4_5->spsr->dirty = 0;
1475                 armv4_5->spsr->valid = 1;
1476         }
1477
1478         if ((retval = jtag_execute_queue()) != ERROR_OK)
1479                 return retval;
1480
1481         if (arm7_9->post_debug_entry)
1482         {
1483                 retval = arm7_9->post_debug_entry(target);
1484                 if (retval != ERROR_OK)
1485                         return retval;
1486         }
1487
1488         return ERROR_OK;
1489 }
1490
1491 /**
1492  * Validate the full context for an ARM7/9 target in all processor modes.  If
1493  * there are any invalid registers for the target, they will all be read.  This
1494  * includes the PSR.
1495  *
1496  * @param target Pointer to the ARM7/9 target to capture the full context from
1497  * @return Error if the target is not halted, has an invalid core mode, or if
1498  *         the JTAG queue fails to execute
1499  */
1500 static int arm7_9_full_context(struct target *target)
1501 {
1502         int i;
1503         int retval;
1504         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1505         struct arm *armv4_5 = &arm7_9->armv4_5_common;
1506
1507         LOG_DEBUG("-");
1508
1509         if (target->state != TARGET_HALTED)
1510         {
1511                 LOG_WARNING("target not halted");
1512                 return ERROR_TARGET_NOT_HALTED;
1513         }
1514
1515         if (!is_arm_mode(armv4_5->core_mode))
1516                 return ERROR_FAIL;
1517
1518         /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1519          * SYS shares registers with User, so we don't touch SYS
1520          */
1521         for (i = 0; i < 6; i++)
1522         {
1523                 uint32_t mask = 0;
1524                 uint32_t* reg_p[16];
1525                 int j;
1526                 int valid = 1;
1527
1528                 /* check if there are invalid registers in the current mode
1529                  */
1530                 for (j = 0; j <= 16; j++)
1531                 {
1532                         if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1533                                 valid = 0;
1534                 }
1535
1536                 if (!valid)
1537                 {
1538                         uint32_t tmp_cpsr;
1539
1540                         /* change processor mode (and mask T bit) */
1541                         tmp_cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 8)
1542                                         & 0xe0;
1543                         tmp_cpsr |= armv4_5_number_to_mode(i);
1544                         tmp_cpsr &= ~0x20;
1545                         arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1546
1547                         for (j = 0; j < 15; j++)
1548                         {
1549                                 if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid == 0)
1550                                 {
1551                                         reg_p[j] = (uint32_t*)ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).value;
1552                                         mask |= 1 << j;
1553                                         ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).valid = 1;
1554                                         ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j).dirty = 0;
1555                                 }
1556                         }
1557
1558                         /* if only the PSR is invalid, mask is all zeroes */
1559                         if (mask)
1560                                 arm7_9->read_core_regs(target, mask, reg_p);
1561
1562                         /* check if the PSR has to be read */
1563                         if (ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid == 0)
1564                         {
1565                                 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);
1566                                 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).valid = 1;
1567                                 ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16).dirty = 0;
1568                         }
1569                 }
1570         }
1571
1572         /* restore processor mode (mask T bit) */
1573         arm7_9->write_xpsr_im8(target,
1574                         buf_get_u32(armv4_5->cpsr->value, 0, 8) & ~0x20,
1575                         0, 0);
1576
1577         if ((retval = jtag_execute_queue()) != ERROR_OK)
1578         {
1579                 return retval;
1580         }
1581         return ERROR_OK;
1582 }
1583
1584 /**
1585  * Restore the processor context on an ARM7/9 target.  The full processor
1586  * context is analyzed to see if any of the registers are dirty on this end, but
1587  * have a valid new value.  If this is the case, the processor is changed to the
1588  * appropriate mode and the new register values are written out to the
1589  * processor.  If there happens to be a dirty register with an invalid value, an
1590  * error will be logged.
1591  *
1592  * @param target Pointer to the ARM7/9 target to have its context restored
1593  * @return Error status if the target is not halted or the core mode in the
1594  *         armv4_5 struct is invalid.
1595  */
1596 static int arm7_9_restore_context(struct target *target)
1597 {
1598         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1599         struct arm *armv4_5 = &arm7_9->armv4_5_common;
1600         struct reg *reg;
1601         struct arm_reg *reg_arch_info;
1602         enum arm_mode current_mode = armv4_5->core_mode;
1603         int i, j;
1604         int dirty;
1605         int mode_change;
1606
1607         LOG_DEBUG("-");
1608
1609         if (target->state != TARGET_HALTED)
1610         {
1611                 LOG_WARNING("target not halted");
1612                 return ERROR_TARGET_NOT_HALTED;
1613         }
1614
1615         if (arm7_9->pre_restore_context)
1616                 arm7_9->pre_restore_context(target);
1617
1618         if (!is_arm_mode(armv4_5->core_mode))
1619                 return ERROR_FAIL;
1620
1621         /* iterate through processor modes (User, FIQ, IRQ, SVC, ABT, UND)
1622          * SYS shares registers with User, so we don't touch SYS
1623          */
1624         for (i = 0; i < 6; i++)
1625         {
1626                 LOG_DEBUG("examining %s mode",
1627                                 arm_mode_name(armv4_5->core_mode));
1628                 dirty = 0;
1629                 mode_change = 0;
1630                 /* check if there are dirty registers in the current mode
1631                 */
1632                 for (j = 0; j <= 16; j++)
1633                 {
1634                         reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), j);
1635                         reg_arch_info = reg->arch_info;
1636                         if (reg->dirty == 1)
1637                         {
1638                                 if (reg->valid == 1)
1639                                 {
1640                                         dirty = 1;
1641                                         LOG_DEBUG("examining dirty reg: %s", reg->name);
1642                                         if ((reg_arch_info->mode != ARM_MODE_ANY)
1643                                                 && (reg_arch_info->mode != current_mode)
1644                                                 && !((reg_arch_info->mode == ARM_MODE_USR) && (armv4_5->core_mode == ARM_MODE_SYS))
1645                                                 && !((reg_arch_info->mode == ARM_MODE_SYS) && (armv4_5->core_mode == ARM_MODE_USR)))
1646                                         {
1647                                                 mode_change = 1;
1648                                                 LOG_DEBUG("require mode change");
1649                                         }
1650                                 }
1651                                 else
1652                                 {
1653                                         LOG_ERROR("BUG: dirty register '%s', but no valid data", reg->name);
1654                                 }
1655                         }
1656                 }
1657
1658                 if (dirty)
1659                 {
1660                         uint32_t mask = 0x0;
1661                         int num_regs = 0;
1662                         uint32_t regs[16];
1663
1664                         if (mode_change)
1665                         {
1666                                 uint32_t tmp_cpsr;
1667
1668                                 /* change processor mode (mask T bit) */
1669                                 tmp_cpsr = buf_get_u32(armv4_5->cpsr->value,
1670                                                 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);
1675                         }
1676
1677                         for (j = 0; j <= 14; j++)
1678                         {
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;
1681
1682
1683                                 if (reg->dirty == 1)
1684                                 {
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 mode %s "
1691                                                 "with value 0x%8.8" PRIx32, j,
1692                                                 arm_mode_name(armv4_5->core_mode),
1693                                                 regs[j]);
1694                                 }
1695                         }
1696
1697                         if (mask)
1698                         {
1699                                 arm7_9->write_core_regs(target, mask, regs);
1700                         }
1701
1702                         reg = &ARMV4_5_CORE_REG_MODE(armv4_5->core_cache, armv4_5_number_to_mode(i), 16);
1703                         reg_arch_info = reg->arch_info;
1704                         if ((reg->dirty) && (reg_arch_info->mode != ARM_MODE_ANY))
1705                         {
1706                                 LOG_DEBUG("writing SPSR of mode %i with value 0x%8.8" PRIx32 "", i, buf_get_u32(reg->value, 0, 32));
1707                                 arm7_9->write_xpsr(target, buf_get_u32(reg->value, 0, 32), 1);
1708                         }
1709                 }
1710         }
1711
1712         if (!armv4_5->cpsr->dirty && (armv4_5->core_mode != current_mode))
1713         {
1714                 /* restore processor mode (mask T bit) */
1715                 uint32_t tmp_cpsr;
1716
1717                 tmp_cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 8) & 0xE0;
1718                 tmp_cpsr |= armv4_5_number_to_mode(i);
1719                 tmp_cpsr &= ~0x20;
1720                 LOG_DEBUG("writing lower 8 bit of cpsr with value 0x%2.2x", (unsigned)(tmp_cpsr));
1721                 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
1722         }
1723         else if (armv4_5->cpsr->dirty)
1724         {
1725                 /* CPSR has been changed, full restore necessary (mask T bit) */
1726                 LOG_DEBUG("writing cpsr with value 0x%8.8" PRIx32,
1727                                 buf_get_u32(armv4_5->cpsr->value, 0, 32));
1728                 arm7_9->write_xpsr(target,
1729                                 buf_get_u32(armv4_5->cpsr->value, 0, 32)
1730                                         & ~0x20, 0);
1731                 armv4_5->cpsr->dirty = 0;
1732                 armv4_5->cpsr->valid = 1;
1733         }
1734
1735         /* restore PC */
1736         LOG_DEBUG("writing PC with value 0x%8.8" PRIx32,
1737                         buf_get_u32(armv4_5->pc->value, 0, 32));
1738         arm7_9->write_pc(target, buf_get_u32(armv4_5->pc->value, 0, 32));
1739         armv4_5->pc->dirty = 0;
1740
1741         return ERROR_OK;
1742 }
1743
1744 /**
1745  * Restart the core of an ARM7/9 target.  A RESTART command is sent to the
1746  * instruction register and the JTAG state is set to TAP_IDLE causing a core
1747  * restart.
1748  *
1749  * @param target Pointer to the ARM7/9 target to be restarted
1750  * @return Result of executing the JTAG queue
1751  */
1752 static int arm7_9_restart_core(struct target *target)
1753 {
1754         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1755         struct arm_jtag *jtag_info = &arm7_9->jtag_info;
1756         int retval;
1757
1758         /* set RESTART instruction */
1759         if (arm7_9->need_bypass_before_restart) {
1760                 arm7_9->need_bypass_before_restart = 0;
1761
1762                 retval = arm_jtag_set_instr(jtag_info, 0xf, NULL, TAP_IDLE);
1763                 if (retval != ERROR_OK)
1764                         return retval;
1765         }
1766         retval = arm_jtag_set_instr(jtag_info, 0x4, NULL, TAP_IDLE);
1767         if (retval != ERROR_OK)
1768                 return retval;
1769
1770         jtag_add_runtest(1, TAP_IDLE);
1771         return jtag_execute_queue();
1772 }
1773
1774 /**
1775  * Enable the watchpoints on an ARM7/9 target.  The target's watchpoints are
1776  * iterated through and are set on the target if they aren't already set.
1777  *
1778  * @param target Pointer to the ARM7/9 target to enable watchpoints on
1779  */
1780 static void arm7_9_enable_watchpoints(struct target *target)
1781 {
1782         struct watchpoint *watchpoint = target->watchpoints;
1783
1784         while (watchpoint)
1785         {
1786                 if (watchpoint->set == 0)
1787                         arm7_9_set_watchpoint(target, watchpoint);
1788                 watchpoint = watchpoint->next;
1789         }
1790 }
1791
1792 /**
1793  * Enable the breakpoints on an ARM7/9 target.  The target's breakpoints are
1794  * iterated through and are set on the target.
1795  *
1796  * @param target Pointer to the ARM7/9 target to enable breakpoints on
1797  */
1798 static void arm7_9_enable_breakpoints(struct target *target)
1799 {
1800         struct breakpoint *breakpoint = target->breakpoints;
1801
1802         /* set any pending breakpoints */
1803         while (breakpoint)
1804         {
1805                 arm7_9_set_breakpoint(target, breakpoint);
1806                 breakpoint = breakpoint->next;
1807         }
1808 }
1809
1810 int arm7_9_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
1811 {
1812         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1813         struct arm *armv4_5 = &arm7_9->armv4_5_common;
1814         struct breakpoint *breakpoint = target->breakpoints;
1815         struct reg *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
1816         int err, retval = ERROR_OK;
1817
1818         LOG_DEBUG("-");
1819
1820         if (target->state != TARGET_HALTED)
1821         {
1822                 LOG_WARNING("target not halted");
1823                 return ERROR_TARGET_NOT_HALTED;
1824         }
1825
1826         if (!debug_execution)
1827         {
1828                 target_free_all_working_areas(target);
1829         }
1830
1831         /* current = 1: continue on current pc, otherwise continue at <address> */
1832         if (!current)
1833                 buf_set_u32(armv4_5->pc->value, 0, 32, address);
1834
1835         uint32_t current_pc;
1836         current_pc = buf_get_u32(armv4_5->pc->value, 0, 32);
1837
1838         /* the front-end may request us not to handle breakpoints */
1839         if (handle_breakpoints)
1840         {
1841                 breakpoint = breakpoint_find(target,
1842                                 buf_get_u32(armv4_5->pc->value, 0, 32));
1843                 if (breakpoint != NULL)
1844                 {
1845                         LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 " (id: %d)", breakpoint->address, breakpoint->unique_id );
1846                         if ((retval = arm7_9_unset_breakpoint(target, breakpoint)) != ERROR_OK)
1847                         {
1848                                 return retval;
1849                         }
1850
1851                         /* calculate PC of next instruction */
1852                         uint32_t next_pc;
1853                         if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
1854                         {
1855                                 uint32_t current_opcode;
1856                                 target_read_u32(target, current_pc, &current_opcode);
1857                                 LOG_ERROR("Couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
1858                                 return retval;
1859                         }
1860
1861                         LOG_DEBUG("enable single-step");
1862                         arm7_9->enable_single_step(target, next_pc);
1863
1864                         target->debug_reason = DBG_REASON_SINGLESTEP;
1865
1866                         if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
1867                         {
1868                                 return retval;
1869                         }
1870
1871                         if (armv4_5->core_state == ARM_STATE_ARM)
1872                                 arm7_9->branch_resume(target);
1873                         else if (armv4_5->core_state == ARM_STATE_THUMB)
1874                         {
1875                                 arm7_9->branch_resume_thumb(target);
1876                         }
1877                         else
1878                         {
1879                                 LOG_ERROR("unhandled core state");
1880                                 return ERROR_FAIL;
1881                         }
1882
1883                         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1884                         embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1885                         err = arm7_9_execute_sys_speed(target);
1886
1887                         LOG_DEBUG("disable single-step");
1888                         arm7_9->disable_single_step(target);
1889
1890                         if (err != ERROR_OK)
1891                         {
1892                                 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
1893                                 {
1894                                         return retval;
1895                                 }
1896                                 target->state = TARGET_UNKNOWN;
1897                                 return err;
1898                         }
1899
1900                         retval = arm7_9_debug_entry(target);
1901                         if (retval != ERROR_OK)
1902                                 return retval;
1903                         LOG_DEBUG("new PC after step: 0x%8.8" PRIx32,
1904                                         buf_get_u32(armv4_5->pc->value, 0, 32));
1905
1906                         LOG_DEBUG("set breakpoint at 0x%8.8" PRIx32 "", breakpoint->address);
1907                         if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
1908                         {
1909                                 return retval;
1910                         }
1911                 }
1912         }
1913
1914         /* enable any pending breakpoints and watchpoints */
1915         arm7_9_enable_breakpoints(target);
1916         arm7_9_enable_watchpoints(target);
1917
1918         if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
1919         {
1920                 return retval;
1921         }
1922
1923         if (armv4_5->core_state == ARM_STATE_ARM)
1924         {
1925                 arm7_9->branch_resume(target);
1926         }
1927         else if (armv4_5->core_state == ARM_STATE_THUMB)
1928         {
1929                 arm7_9->branch_resume_thumb(target);
1930         }
1931         else
1932         {
1933                 LOG_ERROR("unhandled core state");
1934                 return ERROR_FAIL;
1935         }
1936
1937         /* deassert DBGACK and INTDIS */
1938         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
1939         /* INTDIS only when we really resume, not during debug execution */
1940         if (!debug_execution)
1941                 buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_INTDIS, 1, 0);
1942         embeddedice_write_reg(dbg_ctrl, buf_get_u32(dbg_ctrl->value, 0, dbg_ctrl->size));
1943
1944         if ((retval = arm7_9_restart_core(target)) != ERROR_OK)
1945         {
1946                 return retval;
1947         }
1948
1949         target->debug_reason = DBG_REASON_NOTHALTED;
1950
1951         if (!debug_execution)
1952         {
1953                 /* registers are now invalid */
1954                 register_cache_invalidate(armv4_5->core_cache);
1955                 target->state = TARGET_RUNNING;
1956                 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
1957                 {
1958                         return retval;
1959                 }
1960         }
1961         else
1962         {
1963                 target->state = TARGET_DEBUG_RUNNING;
1964                 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED)) != ERROR_OK)
1965                 {
1966                         return retval;
1967                 }
1968         }
1969
1970         LOG_DEBUG("target resumed");
1971
1972         return ERROR_OK;
1973 }
1974
1975 void arm7_9_enable_eice_step(struct target *target, uint32_t next_pc)
1976 {
1977         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
1978         struct arm *armv4_5 = &arm7_9->armv4_5_common;
1979         uint32_t current_pc;
1980         current_pc = buf_get_u32(armv4_5->pc->value, 0, 32);
1981
1982         if (next_pc != current_pc)
1983         {
1984                 /* setup an inverse breakpoint on the current PC
1985                 * - comparator 1 matches the current address
1986                 * - rangeout from comparator 1 is connected to comparator 0 rangein
1987                 * - comparator 0 matches any address, as long as rangein is low */
1988                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
1989                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
1990                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
1991                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], ~(EICE_W_CTRL_RANGE | EICE_W_CTRL_nOPC) & 0xff);
1992                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], current_pc);
1993                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
1994                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
1995                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
1996                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
1997         }
1998         else
1999         {
2000                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK], 0xffffffff);
2001                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK], 0xffffffff);
2002                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
2003                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK], 0xff);
2004                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE], next_pc);
2005                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK], 0);
2006                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK], 0xffffffff);
2007                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], EICE_W_CTRL_ENABLE);
2008                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK], ~EICE_W_CTRL_nOPC & 0xff);
2009         }
2010 }
2011
2012 void arm7_9_disable_eice_step(struct target *target)
2013 {
2014         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2015
2016         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_MASK]);
2017         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_DATA_MASK]);
2018         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE]);
2019         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_MASK]);
2020         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_VALUE]);
2021         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_ADDR_MASK]);
2022         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_DATA_MASK]);
2023         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_MASK]);
2024         embeddedice_store_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE]);
2025 }
2026
2027 int arm7_9_step(struct target *target, int current, uint32_t address, int handle_breakpoints)
2028 {
2029         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2030         struct arm *armv4_5 = &arm7_9->armv4_5_common;
2031         struct breakpoint *breakpoint = NULL;
2032         int err, retval;
2033
2034         if (target->state != TARGET_HALTED)
2035         {
2036                 LOG_WARNING("target not halted");
2037                 return ERROR_TARGET_NOT_HALTED;
2038         }
2039
2040         /* current = 1: continue on current pc, otherwise continue at <address> */
2041         if (!current)
2042                 buf_set_u32(armv4_5->pc->value, 0, 32, address);
2043
2044         uint32_t current_pc = buf_get_u32(armv4_5->pc->value, 0, 32);
2045
2046         /* the front-end may request us not to handle breakpoints */
2047         if (handle_breakpoints)
2048                 breakpoint = breakpoint_find(target, current_pc);
2049         if (breakpoint != NULL) {
2050                 retval = arm7_9_unset_breakpoint(target, breakpoint);
2051                 if (retval != ERROR_OK)
2052                         return retval;
2053         }
2054
2055         target->debug_reason = DBG_REASON_SINGLESTEP;
2056
2057         /* calculate PC of next instruction */
2058         uint32_t next_pc;
2059         if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
2060         {
2061                 uint32_t current_opcode;
2062                 target_read_u32(target, current_pc, &current_opcode);
2063                 LOG_ERROR("Couldn't calculate PC of next instruction, current opcode was 0x%8.8" PRIx32 "", current_opcode);
2064                 return retval;
2065         }
2066
2067         if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
2068         {
2069                 return retval;
2070         }
2071
2072         arm7_9->enable_single_step(target, next_pc);
2073
2074         if (armv4_5->core_state == ARM_STATE_ARM)
2075         {
2076                 arm7_9->branch_resume(target);
2077         }
2078         else if (armv4_5->core_state == ARM_STATE_THUMB)
2079         {
2080                 arm7_9->branch_resume_thumb(target);
2081         }
2082         else
2083         {
2084                 LOG_ERROR("unhandled core state");
2085                 return ERROR_FAIL;
2086         }
2087
2088         if ((retval = target_call_event_callbacks(target, TARGET_EVENT_RESUMED)) != ERROR_OK)
2089         {
2090                 return retval;
2091         }
2092
2093         err = arm7_9_execute_sys_speed(target);
2094         arm7_9->disable_single_step(target);
2095
2096         /* registers are now invalid */
2097         register_cache_invalidate(armv4_5->core_cache);
2098
2099         if (err != ERROR_OK)
2100         {
2101                 target->state = TARGET_UNKNOWN;
2102         } else {
2103                 retval = arm7_9_debug_entry(target);
2104                 if (retval != ERROR_OK)
2105                         return retval;
2106                 if ((retval = target_call_event_callbacks(target, TARGET_EVENT_HALTED)) != ERROR_OK)
2107                 {
2108                         return retval;
2109                 }
2110                 LOG_DEBUG("target stepped");
2111         }
2112
2113         if (breakpoint)
2114                 if ((retval = arm7_9_set_breakpoint(target, breakpoint)) != ERROR_OK)
2115                 {
2116                         return retval;
2117                 }
2118
2119         return err;
2120 }
2121
2122 static int arm7_9_read_core_reg(struct target *target, struct reg *r,
2123                 int num, enum arm_mode mode)
2124 {
2125         uint32_t* reg_p[16];
2126         uint32_t value;
2127         int retval;
2128         struct arm_reg *areg = r->arch_info;
2129         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2130         struct arm *armv4_5 = &arm7_9->armv4_5_common;
2131
2132         if (!is_arm_mode(armv4_5->core_mode))
2133                 return ERROR_FAIL;
2134         if ((num < 0) || (num > 16))
2135                 return ERROR_INVALID_ARGUMENTS;
2136
2137         if ((mode != ARM_MODE_ANY)
2138                         && (mode != armv4_5->core_mode)
2139                         && (areg->mode != ARM_MODE_ANY))
2140         {
2141                 uint32_t tmp_cpsr;
2142
2143                 /* change processor mode (mask T bit) */
2144                 tmp_cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 8) & 0xE0;
2145                 tmp_cpsr |= mode;
2146                 tmp_cpsr &= ~0x20;
2147                 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
2148         }
2149
2150         if ((num >= 0) && (num <= 15))
2151         {
2152                 /* read a normal core register */
2153                 reg_p[num] = &value;
2154
2155                 arm7_9->read_core_regs(target, 1 << num, reg_p);
2156         }
2157         else
2158         {
2159                 /* read a program status register
2160                  * if the register mode is MODE_ANY, we read the cpsr, otherwise a spsr
2161                  */
2162                 arm7_9->read_xpsr(target, &value, areg->mode != ARM_MODE_ANY);
2163         }
2164
2165         if ((retval = jtag_execute_queue()) != ERROR_OK)
2166         {
2167                 return retval;
2168         }
2169
2170         r->valid = 1;
2171         r->dirty = 0;
2172         buf_set_u32(r->value, 0, 32, value);
2173
2174         if ((mode != ARM_MODE_ANY)
2175                         && (mode != armv4_5->core_mode)
2176                         && (areg->mode != ARM_MODE_ANY))        {
2177                 /* restore processor mode (mask T bit) */
2178                 arm7_9->write_xpsr_im8(target,
2179                                 buf_get_u32(armv4_5->cpsr->value, 0, 8)
2180                                         & ~0x20, 0, 0);
2181         }
2182
2183         return ERROR_OK;
2184 }
2185
2186 static int arm7_9_write_core_reg(struct target *target, struct reg *r,
2187                 int num, enum arm_mode mode, uint32_t value)
2188 {
2189         uint32_t reg[16];
2190         struct arm_reg *areg = r->arch_info;
2191         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2192         struct arm *armv4_5 = &arm7_9->armv4_5_common;
2193
2194         if (!is_arm_mode(armv4_5->core_mode))
2195                 return ERROR_FAIL;
2196         if ((num < 0) || (num > 16))
2197                 return ERROR_INVALID_ARGUMENTS;
2198
2199         if ((mode != ARM_MODE_ANY)
2200                         && (mode != armv4_5->core_mode)
2201                         && (areg->mode != ARM_MODE_ANY))        {
2202                 uint32_t tmp_cpsr;
2203
2204                 /* change processor mode (mask T bit) */
2205                 tmp_cpsr = buf_get_u32(armv4_5->cpsr->value, 0, 8) & 0xE0;
2206                 tmp_cpsr |= mode;
2207                 tmp_cpsr &= ~0x20;
2208                 arm7_9->write_xpsr_im8(target, tmp_cpsr & 0xff, 0, 0);
2209         }
2210
2211         if ((num >= 0) && (num <= 15))
2212         {
2213                 /* write a normal core register */
2214                 reg[num] = value;
2215
2216                 arm7_9->write_core_regs(target, 1 << num, reg);
2217         }
2218         else
2219         {
2220                 /* write a program status register
2221                 * if the register mode is MODE_ANY, we write the cpsr, otherwise a spsr
2222                 */
2223                 int spsr = (areg->mode != ARM_MODE_ANY);
2224
2225                 /* if we're writing the CPSR, mask the T bit */
2226                 if (!spsr)
2227                         value &= ~0x20;
2228
2229                 arm7_9->write_xpsr(target, value, spsr);
2230         }
2231
2232         r->valid = 1;
2233         r->dirty = 0;
2234
2235         if ((mode != ARM_MODE_ANY)
2236                         && (mode != armv4_5->core_mode)
2237                         && (areg->mode != ARM_MODE_ANY))        {
2238                 /* restore processor mode (mask T bit) */
2239                 arm7_9->write_xpsr_im8(target,
2240                                 buf_get_u32(armv4_5->cpsr->value, 0, 8)
2241                                         & ~0x20, 0, 0);
2242         }
2243
2244         return jtag_execute_queue();
2245 }
2246
2247 int arm7_9_read_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2248 {
2249         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2250         struct arm *armv4_5 = &arm7_9->armv4_5_common;
2251         uint32_t reg[16];
2252         uint32_t num_accesses = 0;
2253         int thisrun_accesses;
2254         int i;
2255         uint32_t cpsr;
2256         int retval;
2257         int last_reg = 0;
2258
2259         LOG_DEBUG("address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "", address, size, count);
2260
2261         if (target->state != TARGET_HALTED)
2262         {
2263                 LOG_WARNING("target not halted");
2264                 return ERROR_TARGET_NOT_HALTED;
2265         }
2266
2267         /* sanitize arguments */
2268         if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
2269                 return ERROR_INVALID_ARGUMENTS;
2270
2271         if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2272                 return ERROR_TARGET_UNALIGNED_ACCESS;
2273
2274         /* load the base register with the address of the first word */
2275         reg[0] = address;
2276         arm7_9->write_core_regs(target, 0x1, reg);
2277
2278         int j = 0;
2279
2280         switch (size)
2281         {
2282                 case 4:
2283                         while (num_accesses < count)
2284                         {
2285                                 uint32_t reg_list;
2286                                 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2287                                 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2288
2289                                 if (last_reg <= thisrun_accesses)
2290                                         last_reg = thisrun_accesses;
2291
2292                                 arm7_9->load_word_regs(target, reg_list);
2293
2294                                 /* fast memory reads are only safe when the target is running
2295                                  * from a sufficiently high clock (32 kHz is usually too slow)
2296                                  */
2297                                 if (arm7_9->fast_memory_access)
2298                                         retval = arm7_9_execute_fast_sys_speed(target);
2299                                 else
2300                                         retval = arm7_9_execute_sys_speed(target);
2301                                 if (retval != ERROR_OK)
2302                                         return retval;
2303
2304                                 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 4);
2305
2306                                 /* advance buffer, count number of accesses */
2307                                 buffer += thisrun_accesses * 4;
2308                                 num_accesses += thisrun_accesses;
2309
2310                                 if ((j++%1024) == 0)
2311                                 {
2312                                         keep_alive();
2313                                 }
2314                         }
2315                         break;
2316                 case 2:
2317                         while (num_accesses < count)
2318                         {
2319                                 uint32_t reg_list;
2320                                 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2321                                 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2322
2323                                 for (i = 1; i <= thisrun_accesses; i++)
2324                                 {
2325                                         if (i > last_reg)
2326                                                 last_reg = i;
2327                                         arm7_9->load_hword_reg(target, i);
2328                                         /* fast memory reads are only safe when the target is running
2329                                          * from a sufficiently high clock (32 kHz is usually too slow)
2330                                          */
2331                                         if (arm7_9->fast_memory_access)
2332                                                 retval = arm7_9_execute_fast_sys_speed(target);
2333                                         else
2334                                                 retval = arm7_9_execute_sys_speed(target);
2335                                         if (retval != ERROR_OK)
2336                                         {
2337                                                 return retval;
2338                                         }
2339
2340                                 }
2341
2342                                 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 2);
2343
2344                                 /* advance buffer, count number of accesses */
2345                                 buffer += thisrun_accesses * 2;
2346                                 num_accesses += thisrun_accesses;
2347
2348                                 if ((j++%1024) == 0)
2349                                 {
2350                                         keep_alive();
2351                                 }
2352                         }
2353                         break;
2354                 case 1:
2355                         while (num_accesses < count)
2356                         {
2357                                 uint32_t reg_list;
2358                                 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2359                                 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2360
2361                                 for (i = 1; i <= thisrun_accesses; i++)
2362                                 {
2363                                         if (i > last_reg)
2364                                                 last_reg = i;
2365                                         arm7_9->load_byte_reg(target, i);
2366                                         /* fast memory reads are only safe when the target is running
2367                                          * from a sufficiently high clock (32 kHz is usually too slow)
2368                                          */
2369                                         if (arm7_9->fast_memory_access)
2370                                                 retval = arm7_9_execute_fast_sys_speed(target);
2371                                         else
2372                                                 retval = arm7_9_execute_sys_speed(target);
2373                                         if (retval != ERROR_OK)
2374                                         {
2375                                                 return retval;
2376                                         }
2377                                 }
2378
2379                                 arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 1);
2380
2381                                 /* advance buffer, count number of accesses */
2382                                 buffer += thisrun_accesses * 1;
2383                                 num_accesses += thisrun_accesses;
2384
2385                                 if ((j++%1024) == 0)
2386                                 {
2387                                         keep_alive();
2388                                 }
2389                         }
2390                         break;
2391         }
2392
2393         if (!is_arm_mode(armv4_5->core_mode))
2394                 return ERROR_FAIL;
2395
2396         for (i = 0; i <= last_reg; i++) {
2397                 struct reg *r = arm_reg_current(armv4_5, i);
2398
2399                 r->dirty = r->valid;
2400         }
2401
2402         arm7_9->read_xpsr(target, &cpsr, 0);
2403         if ((retval = jtag_execute_queue()) != ERROR_OK)
2404         {
2405                 LOG_ERROR("JTAG error while reading cpsr");
2406                 return ERROR_TARGET_DATA_ABORT;
2407         }
2408
2409         if (((cpsr & 0x1f) == ARM_MODE_ABT) && (armv4_5->core_mode != ARM_MODE_ABT))
2410         {
2411                 LOG_WARNING("memory read caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
2412
2413                 arm7_9->write_xpsr_im8(target,
2414                                 buf_get_u32(armv4_5->cpsr->value, 0, 8)
2415                                         & ~0x20, 0, 0);
2416
2417                 return ERROR_TARGET_DATA_ABORT;
2418         }
2419
2420         return ERROR_OK;
2421 }
2422
2423 int arm7_9_write_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2424 {
2425         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2426         struct arm *armv4_5 = &arm7_9->armv4_5_common;
2427         struct reg *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
2428
2429         uint32_t reg[16];
2430         uint32_t num_accesses = 0;
2431         int thisrun_accesses;
2432         int i;
2433         uint32_t cpsr;
2434         int retval;
2435         int last_reg = 0;
2436
2437 #ifdef _DEBUG_ARM7_9_
2438         LOG_DEBUG("address: 0x%8.8x, size: 0x%8.8x, count: 0x%8.8x", address, size, count);
2439 #endif
2440
2441         if (target->state != TARGET_HALTED)
2442         {
2443                 LOG_WARNING("target not halted");
2444                 return ERROR_TARGET_NOT_HALTED;
2445         }
2446
2447         /* sanitize arguments */
2448         if (((size != 4) && (size != 2) && (size != 1)) || (count == 0) || !(buffer))
2449                 return ERROR_INVALID_ARGUMENTS;
2450
2451         if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
2452                 return ERROR_TARGET_UNALIGNED_ACCESS;
2453
2454         /* load the base register with the address of the first word */
2455         reg[0] = address;
2456         arm7_9->write_core_regs(target, 0x1, reg);
2457
2458         /* Clear DBGACK, to make sure memory fetches work as expected */
2459         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 0);
2460         embeddedice_store_reg(dbg_ctrl);
2461
2462         switch (size)
2463         {
2464                 case 4:
2465                         while (num_accesses < count)
2466                         {
2467                                 uint32_t reg_list;
2468                                 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2469                                 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2470
2471                                 for (i = 1; i <= thisrun_accesses; i++)
2472                                 {
2473                                         if (i > last_reg)
2474                                                 last_reg = i;
2475                                         reg[i] = target_buffer_get_u32(target, buffer);
2476                                         buffer += 4;
2477                                 }
2478
2479                                 arm7_9->write_core_regs(target, reg_list, reg);
2480
2481                                 arm7_9->store_word_regs(target, reg_list);
2482
2483                                 /* fast memory writes are only safe when the target is running
2484                                  * from a sufficiently high clock (32 kHz is usually too slow)
2485                                  */
2486                                 if (arm7_9->fast_memory_access)
2487                                         retval = arm7_9_execute_fast_sys_speed(target);
2488                                 else
2489                                 {
2490                                         retval = arm7_9_execute_sys_speed(target);
2491
2492                                         /*
2493                                          * if memory writes are made when the clock is running slow
2494                                          * (i.e. 32 kHz) which is necessary in some scripts to reconfigure
2495                                          * processor operations after a "reset halt" or "reset init",
2496                                          * need to immediately stroke the keep alive or will end up with
2497                                          * gdb "keep alive not sent error message" problem.
2498                                          */
2499
2500                                         keep_alive();
2501                                 }
2502
2503                                 if (retval != ERROR_OK)
2504                                 {
2505                                         return retval;
2506                                 }
2507
2508                                 num_accesses += thisrun_accesses;
2509                         }
2510                         break;
2511                 case 2:
2512                         while (num_accesses < count)
2513                         {
2514                                 uint32_t reg_list;
2515                                 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2516                                 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2517
2518                                 for (i = 1; i <= thisrun_accesses; i++)
2519                                 {
2520                                         if (i > last_reg)
2521                                                 last_reg = i;
2522                                         reg[i] = target_buffer_get_u16(target, buffer) & 0xffff;
2523                                         buffer += 2;
2524                                 }
2525
2526                                 arm7_9->write_core_regs(target, reg_list, reg);
2527
2528                                 for (i = 1; i <= thisrun_accesses; i++)
2529                                 {
2530                                         arm7_9->store_hword_reg(target, i);
2531
2532                                         /* fast memory writes are only safe when the target is running
2533                                          * from a sufficiently high clock (32 kHz is usually too slow)
2534                                          */
2535                                         if (arm7_9->fast_memory_access)
2536                                                 retval = arm7_9_execute_fast_sys_speed(target);
2537                                         else
2538                                         {
2539                                                 retval = arm7_9_execute_sys_speed(target);
2540
2541                                                 /*
2542                                                  * if memory writes are made when the clock is running slow
2543                                                  * (i.e. 32 kHz) which is necessary in some scripts to reconfigure
2544                                                  * processor operations after a "reset halt" or "reset init",
2545                                                  * need to immediately stroke the keep alive or will end up with
2546                                                  * gdb "keep alive not sent error message" problem.
2547                                                  */     
2548
2549                                                 keep_alive();
2550                                         }
2551
2552                                         if (retval != ERROR_OK)
2553                                         {
2554                                                 return retval;
2555                                         }
2556                                 }
2557
2558                                 num_accesses += thisrun_accesses;
2559                         }
2560                         break;
2561                 case 1:
2562                         while (num_accesses < count)
2563                         {
2564                                 uint32_t reg_list;
2565                                 thisrun_accesses = ((count - num_accesses) >= 14) ? 14 : (count - num_accesses);
2566                                 reg_list = (0xffff >> (15 - thisrun_accesses)) & 0xfffe;
2567
2568                                 for (i = 1; i <= thisrun_accesses; i++)
2569                                 {
2570                                         if (i > last_reg)
2571                                                 last_reg = i;
2572                                         reg[i] = *buffer++ & 0xff;
2573                                 }
2574
2575                                 arm7_9->write_core_regs(target, reg_list, reg);
2576
2577                                 for (i = 1; i <= thisrun_accesses; i++)
2578                                 {
2579                                         arm7_9->store_byte_reg(target, i);
2580                                         /* fast memory writes are only safe when the target is running
2581                                          * from a sufficiently high clock (32 kHz is usually too slow)
2582                                          */
2583                                         if (arm7_9->fast_memory_access)
2584                                                 retval = arm7_9_execute_fast_sys_speed(target);
2585                                         else
2586                                         {
2587                                                 retval = arm7_9_execute_sys_speed(target);
2588
2589                                                 /*
2590                                                  * if memory writes are made when the clock is running slow
2591                                                  * (i.e. 32 kHz) which is necessary in some scripts to reconfigure
2592                                                  * processor operations after a "reset halt" or "reset init",
2593                                                  * need to immediately stroke the keep alive or will end up with
2594                                                  * gdb "keep alive not sent error message" problem.
2595                                                  */
2596
2597                                                 keep_alive();
2598                                         }
2599
2600                                         if (retval != ERROR_OK)
2601                                         {
2602                                                 return retval;
2603                                         }
2604
2605                                 }
2606
2607                                 num_accesses += thisrun_accesses;
2608                         }
2609                         break;
2610         }
2611
2612         /* Re-Set DBGACK */
2613         buf_set_u32(dbg_ctrl->value, EICE_DBG_CONTROL_DBGACK, 1, 1);
2614         embeddedice_store_reg(dbg_ctrl);
2615
2616         if (!is_arm_mode(armv4_5->core_mode))
2617                 return ERROR_FAIL;
2618
2619         for (i = 0; i <= last_reg; i++) {
2620                 struct reg *r = arm_reg_current(armv4_5, i);
2621
2622                 r->dirty = r->valid;
2623         }
2624
2625         arm7_9->read_xpsr(target, &cpsr, 0);
2626         if ((retval = jtag_execute_queue()) != ERROR_OK)
2627         {
2628                 LOG_ERROR("JTAG error while reading cpsr");
2629                 return ERROR_TARGET_DATA_ABORT;
2630         }
2631
2632         if (((cpsr & 0x1f) == ARM_MODE_ABT) && (armv4_5->core_mode != ARM_MODE_ABT))
2633         {
2634                 LOG_WARNING("memory write caused data abort (address: 0x%8.8" PRIx32 ", size: 0x%" PRIx32 ", count: 0x%" PRIx32 ")", address, size, count);
2635
2636                 arm7_9->write_xpsr_im8(target,
2637                                 buf_get_u32(armv4_5->cpsr->value, 0, 8)
2638                                         & ~0x20, 0, 0);
2639
2640                 return ERROR_TARGET_DATA_ABORT;
2641         }
2642
2643         return ERROR_OK;
2644 }
2645
2646 static int dcc_count;
2647 static uint8_t *dcc_buffer;
2648
2649 static int arm7_9_dcc_completion(struct target *target, uint32_t exit_point, int timeout_ms, void *arch_info)
2650 {
2651         int retval = ERROR_OK;
2652         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2653
2654         if ((retval = target_wait_state(target, TARGET_DEBUG_RUNNING, 500)) != ERROR_OK)
2655                 return retval;
2656
2657         int little = target->endianness == TARGET_LITTLE_ENDIAN;
2658         int count = dcc_count;
2659         uint8_t *buffer = dcc_buffer;
2660         if (count > 2)
2661         {
2662                 /* Handle first & last using standard embeddedice_write_reg and the middle ones w/the
2663                  * core function repeated. */
2664                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2665                 buffer += 4;
2666
2667                 struct embeddedice_reg *ice_reg = arm7_9->eice_cache->reg_list[EICE_COMMS_DATA].arch_info;
2668                 uint8_t reg_addr = ice_reg->addr & 0x1f;
2669                 struct jtag_tap *tap;
2670                 tap = ice_reg->jtag_info->tap;
2671
2672                 embeddedice_write_dcc(tap, reg_addr, buffer, little, count-2);
2673                 buffer += (count-2)*4;
2674
2675                 embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2676         } else
2677         {
2678                 int i;
2679                 for (i = 0; i < count; i++)
2680                 {
2681                         embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
2682                         buffer += 4;
2683                 }
2684         }
2685
2686         if ((retval = target_halt(target))!= ERROR_OK)
2687         {
2688                 return retval;
2689         }
2690         return target_wait_state(target, TARGET_HALTED, 500);
2691 }
2692
2693 static const uint32_t dcc_code[] =
2694 {
2695         /* r0 == input, points to memory buffer
2696          * r1 == scratch
2697          */
2698
2699         /* spin until DCC control (c0) reports data arrived */
2700         0xee101e10,     /* w: mrc p14, #0, r1, c0, c0 */
2701         0xe3110001,     /*    tst r1, #1              */
2702         0x0afffffc,     /*    bne w                   */
2703
2704         /* read word from DCC (c1), write to memory */
2705         0xee111e10,     /*    mrc p14, #0, r1, c1, c0 */
2706         0xe4801004,     /*    str r1, [r0], #4        */
2707
2708         /* repeat */
2709         0xeafffff9      /*    b   w                   */
2710 };
2711
2712 int arm7_9_bulk_write_memory(struct target *target, uint32_t address, uint32_t count, uint8_t *buffer)
2713 {
2714         int retval;
2715         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2716         int i;
2717
2718         if (!arm7_9->dcc_downloads)
2719                 return target_write_memory(target, address, 4, count, buffer);
2720
2721         /* regrab previously allocated working_area, or allocate a new one */
2722         if (!arm7_9->dcc_working_area)
2723         {
2724                 uint8_t dcc_code_buf[6 * 4];
2725
2726                 /* make sure we have a working area */
2727                 if (target_alloc_working_area(target, 24, &arm7_9->dcc_working_area) != ERROR_OK)
2728                 {
2729                         LOG_INFO("no working area available, falling back to memory writes");
2730                         return target_write_memory(target, address, 4, count, buffer);
2731                 }
2732
2733                 /* copy target instructions to target endianness */
2734                 for (i = 0; i < 6; i++)
2735                 {
2736                         target_buffer_set_u32(target, dcc_code_buf + i*4, dcc_code[i]);
2737                 }
2738
2739                 /* write DCC code to working area */
2740                 if ((retval = target_write_memory(target, arm7_9->dcc_working_area->address, 4, 6, dcc_code_buf)) != ERROR_OK)
2741                 {
2742                         return retval;
2743                 }
2744         }
2745
2746         struct arm_algorithm armv4_5_info;
2747         struct reg_param reg_params[1];
2748
2749         armv4_5_info.common_magic = ARM_COMMON_MAGIC;
2750         armv4_5_info.core_mode = ARM_MODE_SVC;
2751         armv4_5_info.core_state = ARM_STATE_ARM;
2752
2753         init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT);
2754
2755         buf_set_u32(reg_params[0].value, 0, 32, address);
2756
2757         dcc_count = count;
2758         dcc_buffer = buffer;
2759         retval = armv4_5_run_algorithm_inner(target, 0, NULL, 1, reg_params,
2760                         arm7_9->dcc_working_area->address,
2761                                 arm7_9->dcc_working_area->address + 6*4,
2762                         20*1000, &armv4_5_info, arm7_9_dcc_completion);
2763
2764         if (retval == ERROR_OK)
2765         {
2766                 uint32_t endaddress = buf_get_u32(reg_params[0].value, 0, 32);
2767                 if (endaddress != (address + count*4))
2768                 {
2769                         LOG_ERROR("DCC write failed, expected end address 0x%08" PRIx32 " got 0x%0" PRIx32 "", (address + count*4), endaddress);
2770                         retval = ERROR_FAIL;
2771                 }
2772         }
2773
2774         destroy_reg_param(&reg_params[0]);
2775
2776         return retval;
2777 }
2778
2779 /**
2780  * Perform per-target setup that requires JTAG access.
2781  */
2782 int arm7_9_examine(struct target *target)
2783 {
2784         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2785         int retval;
2786
2787         if (!target_was_examined(target)) {
2788                 struct reg_cache *t, **cache_p;
2789
2790                 t = embeddedice_build_reg_cache(target, arm7_9);
2791                 if (t == NULL)
2792                         return ERROR_FAIL;
2793
2794                 cache_p = register_get_last_cache_p(&target->reg_cache);
2795                 (*cache_p) = t;
2796                 arm7_9->eice_cache = (*cache_p);
2797
2798                 if (arm7_9->armv4_5_common.etm)
2799                         (*cache_p)->next = etm_build_reg_cache(target,
2800                                         &arm7_9->jtag_info,
2801                                         arm7_9->armv4_5_common.etm);
2802
2803                 target_set_examined(target);
2804         }
2805
2806         retval = embeddedice_setup(target);
2807         if (retval == ERROR_OK)
2808                 retval = arm7_9_setup(target);
2809         if (retval == ERROR_OK && arm7_9->armv4_5_common.etm)
2810                 retval = etm_setup(target);
2811         return retval;
2812 }
2813
2814
2815 int arm7_9_check_reset(struct target *target)
2816 {
2817         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2818
2819         if (get_target_reset_nag() && !arm7_9->dcc_downloads)
2820         {
2821                 LOG_WARNING("NOTE! DCC downloads have not been enabled, defaulting to slow memory writes. Type 'help dcc'.");
2822         }
2823
2824         if (get_target_reset_nag() && (target->working_area_size == 0))
2825         {
2826                 LOG_WARNING("NOTE! Severe performance degradation without working memory enabled.");
2827         }
2828
2829         if (get_target_reset_nag() && !arm7_9->fast_memory_access)
2830         {
2831                 LOG_WARNING("NOTE! Severe performance degradation without fast memory access enabled. Type 'help fast'.");
2832         }
2833
2834         return ERROR_OK;
2835 }
2836
2837 COMMAND_HANDLER(handle_arm7_9_dbgrq_command)
2838 {
2839         struct target *target = get_current_target(CMD_CTX);
2840         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2841
2842         if (!is_arm7_9(arm7_9))
2843         {
2844                 command_print(CMD_CTX, "current target isn't an ARM7/ARM9 target");
2845                 return ERROR_TARGET_INVALID;
2846         }
2847
2848         if (CMD_ARGC > 0)
2849                 COMMAND_PARSE_ENABLE(CMD_ARGV[0],arm7_9->use_dbgrq);
2850
2851         command_print(CMD_CTX, "use of EmbeddedICE dbgrq instead of breakpoint for target halt %s", (arm7_9->use_dbgrq) ? "enabled" : "disabled");
2852
2853         return ERROR_OK;
2854 }
2855
2856 COMMAND_HANDLER(handle_arm7_9_fast_memory_access_command)
2857 {
2858         struct target *target = get_current_target(CMD_CTX);
2859         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2860
2861         if (!is_arm7_9(arm7_9))
2862         {
2863                 command_print(CMD_CTX, "current target isn't an ARM7/ARM9 target");
2864                 return ERROR_TARGET_INVALID;
2865         }
2866
2867         if (CMD_ARGC > 0)
2868                 COMMAND_PARSE_ENABLE(CMD_ARGV[0], arm7_9->fast_memory_access);
2869
2870         command_print(CMD_CTX, "fast memory access is %s", (arm7_9->fast_memory_access) ? "enabled" : "disabled");
2871
2872         return ERROR_OK;
2873 }
2874
2875 COMMAND_HANDLER(handle_arm7_9_dcc_downloads_command)
2876 {
2877         struct target *target = get_current_target(CMD_CTX);
2878         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2879
2880         if (!is_arm7_9(arm7_9))
2881         {
2882                 command_print(CMD_CTX, "current target isn't an ARM7/ARM9 target");
2883                 return ERROR_TARGET_INVALID;
2884         }
2885
2886         if (CMD_ARGC > 0)
2887                 COMMAND_PARSE_ENABLE(CMD_ARGV[0], arm7_9->dcc_downloads);
2888
2889         command_print(CMD_CTX, "dcc downloads are %s", (arm7_9->dcc_downloads) ? "enabled" : "disabled");
2890
2891         return ERROR_OK;
2892 }
2893
2894 static int arm7_9_setup_semihosting(struct target *target, int enable)
2895 {
2896         struct arm7_9_common *arm7_9 = target_to_arm7_9(target);
2897
2898         if (!is_arm7_9(arm7_9))
2899         {
2900                 LOG_USER("current target isn't an ARM7/ARM9 target");
2901                 return ERROR_TARGET_INVALID;
2902         }
2903
2904         if (arm7_9->has_vector_catch) {
2905                 struct reg *vector_catch = &arm7_9->eice_cache
2906                                 ->reg_list[EICE_VEC_CATCH];
2907
2908                 if (!vector_catch->valid)
2909                         embeddedice_read_reg(vector_catch);
2910                 buf_set_u32(vector_catch->value, 2, 1, enable);
2911                 embeddedice_store_reg(vector_catch);
2912         } else {
2913                 /* TODO: allow optional high vectors and/or BKPT_HARD */
2914                 if (enable)
2915                         breakpoint_add(target, 8, 4, BKPT_SOFT);
2916                 else
2917                         breakpoint_remove(target, 8);
2918         }
2919
2920         return ERROR_OK;
2921 }
2922
2923 int arm7_9_init_arch_info(struct target *target, struct arm7_9_common *arm7_9)
2924 {
2925         int retval = ERROR_OK;
2926         struct arm *armv4_5 = &arm7_9->armv4_5_common;
2927
2928         arm7_9->common_magic = ARM7_9_COMMON_MAGIC;
2929
2930         if ((retval = arm_jtag_setup_connection(&arm7_9->jtag_info)) != ERROR_OK)
2931                 return retval;
2932
2933         /* caller must have allocated via calloc(), so everything's zeroed */
2934
2935         arm7_9->wp_available_max = 2;
2936
2937         arm7_9->fast_memory_access = false;
2938         arm7_9->dcc_downloads = false;
2939
2940         armv4_5->arch_info = arm7_9;
2941         armv4_5->read_core_reg = arm7_9_read_core_reg;
2942         armv4_5->write_core_reg = arm7_9_write_core_reg;
2943         armv4_5->full_context = arm7_9_full_context;
2944         armv4_5->setup_semihosting = arm7_9_setup_semihosting;
2945
2946         retval = arm_init_arch_info(target, armv4_5);
2947         if (retval != ERROR_OK)
2948                 return retval;
2949
2950         return target_register_timer_callback(arm7_9_handle_target_request,
2951                         1, 1, target);
2952 }
2953
2954 static const struct command_registration arm7_9_any_command_handlers[] = {
2955         {
2956                 "dbgrq",
2957                 .handler = handle_arm7_9_dbgrq_command,
2958                 .mode = COMMAND_ANY,
2959                 .usage = "['enable'|'disable']",
2960                 .help = "use EmbeddedICE dbgrq instead of breakpoint "
2961                         "for target halt requests",
2962         },
2963         {
2964                 "fast_memory_access",
2965                 .handler = handle_arm7_9_fast_memory_access_command,
2966                 .mode = COMMAND_ANY,
2967                 .usage = "['enable'|'disable']",
2968                 .help = "use fast memory accesses instead of slower "
2969                         "but potentially safer accesses",
2970         },
2971         {
2972                 "dcc_downloads",
2973                 .handler = handle_arm7_9_dcc_downloads_command,
2974                 .mode = COMMAND_ANY,
2975                 .usage = "['enable'|'disable']",
2976                 .help = "use DCC downloads for larger memory writes",
2977         },
2978         COMMAND_REGISTRATION_DONE
2979 };
2980 const struct command_registration arm7_9_command_handlers[] = {
2981         {
2982                 .chain = arm_command_handlers,
2983         },
2984         {
2985                 .chain = etm_command_handlers,
2986         },
2987         {
2988                 .name = "arm7_9",
2989                 .mode = COMMAND_ANY,
2990                 .help = "arm7/9 specific commands",
2991                 .chain = arm7_9_any_command_handlers,
2992         },
2993         COMMAND_REGISTRATION_DONE
2994 };