use COMMAND_REGISTER macro
[fw/openocd] / src / target / arm11.c
1 /***************************************************************************
2  *   Copyright (C) 2008 digenius technology GmbH.                          *
3  *   Michael Bruck                                                         *
4  *                                                                         *
5  *   Copyright (C) 2008,2009 Oyvind Harboe oyvind.harboe@zylin.com         *
6  *                                                                         *
7  *   Copyright (C) 2008 Georg Acher <acher@in.tum.de>                      *
8  *                                                                         *
9  *   This program is free software; you can redistribute it and/or modify  *
10  *   it under the terms of the GNU General Public License as published by  *
11  *   the Free Software Foundation; either version 2 of the License, or     *
12  *   (at your option) any later version.                                   *
13  *                                                                         *
14  *   This program is distributed in the hope that it will be useful,       *
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
17  *   GNU General Public License for more details.                          *
18  *                                                                         *
19  *   You should have received a copy of the GNU General Public License     *
20  *   along with this program; if not, write to the                         *
21  *   Free Software Foundation, Inc.,                                       *
22  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
23  ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "etm.h"
30 #include "breakpoints.h"
31 #include "arm11_dbgtap.h"
32 #include "arm_simulator.h"
33 #include "time_support.h"
34 #include "target_type.h"
35 #include "algorithm.h"
36 #include "register.h"
37
38
39 #if 0
40 #define _DEBUG_INSTRUCTION_EXECUTION_
41 #endif
42
43 static bool arm11_config_memwrite_burst = true;
44 static bool arm11_config_memwrite_error_fatal = true;
45 static uint32_t arm11_vcr = 0;
46 static bool arm11_config_step_irq_enable = false;
47 static bool arm11_config_hardware_step = false;
48
49 enum arm11_regtype
50 {
51         /* debug regs */
52         ARM11_REGISTER_DSCR,
53         ARM11_REGISTER_WDTR,
54         ARM11_REGISTER_RDTR,
55 };
56
57
58 struct arm11_reg_defs
59 {
60         char *                                  name;
61         uint32_t                                                num;
62         int                                             gdb_num;
63         enum arm11_regtype              type;
64 };
65
66 /* update arm11_regcache_ids when changing this */
67 static const struct arm11_reg_defs arm11_reg_defs[] =
68 {
69         /* Debug Registers */
70         {"dscr",        0,      -1,     ARM11_REGISTER_DSCR},
71         {"wdtr",        0,      -1,     ARM11_REGISTER_WDTR},
72         {"rdtr",        0,      -1,     ARM11_REGISTER_RDTR},
73 };
74
75 enum arm11_regcache_ids
76 {
77         ARM11_RC_DSCR,
78         ARM11_RC_WDTR,
79         ARM11_RC_RDTR,
80
81         ARM11_RC_MAX,
82 };
83
84 static int arm11_on_enter_debug_state(struct arm11_common *arm11);
85 static int arm11_step(struct target *target, int current,
86                 uint32_t address, int handle_breakpoints);
87 /* helpers */
88 static int arm11_build_reg_cache(struct target *target);
89 static int arm11_set_reg(struct reg *reg, uint8_t *buf);
90 static int arm11_get_reg(struct reg *reg);
91
92
93 /** Check and if necessary take control of the system
94  *
95  * \param arm11         Target state variable.
96  * \param dscr          If the current DSCR content is
97  *                                      available a pointer to a word holding the
98  *                                      DSCR can be passed. Otherwise use NULL.
99  */
100 static int arm11_check_init(struct arm11_common *arm11, uint32_t *dscr)
101 {
102         uint32_t                        dscr_local_tmp_copy;
103
104         if (!dscr)
105         {
106                 dscr = &dscr_local_tmp_copy;
107
108                 CHECK_RETVAL(arm11_read_DSCR(arm11, dscr));
109         }
110
111         if (!(*dscr & ARM11_DSCR_MODE_SELECT))
112         {
113                 LOG_DEBUG("Bringing target into debug mode");
114
115                 *dscr |= ARM11_DSCR_MODE_SELECT;                /* Halt debug-mode */
116                 arm11_write_DSCR(arm11, *dscr);
117
118                 /* add further reset initialization here */
119
120                 arm11->simulate_reset_on_next_halt = true;
121
122                 if (*dscr & ARM11_DSCR_CORE_HALTED)
123                 {
124                         /** \todo TODO: this needs further scrutiny because
125                           * arm11_on_enter_debug_state() never gets properly called.
126                           * As a result we don't read the actual register states from
127                           * the target.
128                           */
129
130                         arm11->target->state    = TARGET_HALTED;
131                         arm11->target->debug_reason     = arm11_get_DSCR_debug_reason(*dscr);
132                 }
133                 else
134                 {
135                         arm11->target->state    = TARGET_RUNNING;
136                         arm11->target->debug_reason     = DBG_REASON_NOTHALTED;
137                 }
138
139                 arm11_sc7_clear_vbw(arm11);
140         }
141
142         return ERROR_OK;
143 }
144
145
146
147 #define R(x) \
148         (arm11->reg_values[ARM11_RC_##x])
149
150 /** Save processor state.
151   *
152   * This is called when the HALT instruction has succeeded
153   * or on other occasions that stop the processor.
154   *
155   */
156 static int arm11_on_enter_debug_state(struct arm11_common *arm11)
157 {
158         int retval;
159
160         /* REVISIT entire cache should already be invalid !!! */
161         register_cache_invalidate(arm11->arm.core_cache);
162
163         for (size_t i = 0; i < ARRAY_SIZE(arm11->reg_values); i++)
164         {
165                 arm11->reg_list[i].valid        = 1;
166                 arm11->reg_list[i].dirty        = 0;
167         }
168
169         /* See e.g. ARM1136 TRM, "14.8.4 Entering Debug state" */
170
171         /* Save DSCR */
172         CHECK_RETVAL(arm11_read_DSCR(arm11, &R(DSCR)));
173
174         /* Save wDTR */
175
176         if (R(DSCR) & ARM11_DSCR_WDTR_FULL)
177         {
178                 arm11_add_debug_SCAN_N(arm11, 0x05, ARM11_TAP_DEFAULT);
179
180                 arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
181
182                 struct scan_field       chain5_fields[3];
183
184                 arm11_setup_field(arm11, 32, NULL, &R(WDTR),    chain5_fields + 0);
185                 arm11_setup_field(arm11,  1, NULL, NULL,                chain5_fields + 1);
186                 arm11_setup_field(arm11,  1, NULL, NULL,                chain5_fields + 2);
187
188                 arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields), chain5_fields, TAP_DRPAUSE);
189         }
190         else
191         {
192                 arm11->reg_list[ARM11_RC_WDTR].valid    = 0;
193         }
194
195
196         /* DSCR: set ARM11_DSCR_EXECUTE_ARM_INSTRUCTION_ENABLE
197          *
198          * ARM1176 spec says this is needed only for wDTR/rDTR's "ITR mode",
199          * but not to issue ITRs. ARM1136 seems to require this to issue
200          * ITR's as well...
201          */
202         uint32_t new_dscr = R(DSCR) | ARM11_DSCR_EXECUTE_ARM_INSTRUCTION_ENABLE;
203
204         /* this executes JTAG queue: */
205
206         arm11_write_DSCR(arm11, new_dscr);
207
208
209         /* From the spec:
210            Before executing any instruction in debug state you have to drain the write buffer.
211            This ensures that no imprecise Data Aborts can return at a later point:*/
212
213         /** \todo TODO: Test drain write buffer. */
214
215 #if 0
216         while (1)
217         {
218                 /* MRC p14,0,R0,c5,c10,0 */
219                 //      arm11_run_instr_no_data1(arm11, /*0xee150e1a*/0xe320f000);
220
221                 /* mcr     15, 0, r0, cr7, cr10, {4} */
222                 arm11_run_instr_no_data1(arm11, 0xee070f9a);
223
224                 uint32_t dscr = arm11_read_DSCR(arm11);
225
226                 LOG_DEBUG("DRAIN, DSCR %08x", dscr);
227
228                 if (dscr & ARM11_DSCR_STICKY_IMPRECISE_DATA_ABORT)
229                 {
230                         arm11_run_instr_no_data1(arm11, 0xe320f000);
231
232                         dscr = arm11_read_DSCR(arm11);
233
234                         LOG_DEBUG("DRAIN, DSCR %08x (DONE)", dscr);
235
236                         break;
237                 }
238         }
239 #endif
240
241         /* Save registers.
242          *
243          * NOTE:  ARM1136 TRM suggests saving just R0 here now, then
244          * CPSR and PC after the rDTR stuff.  We do it all at once.
245          */
246         retval = arm_dpm_read_current_registers(&arm11->dpm);
247         if (retval != ERROR_OK)
248                 LOG_ERROR("DPM REG READ -- fail %d", retval);
249
250         retval = arm11_run_instr_data_prepare(arm11);
251         if (retval != ERROR_OK)
252                 return retval;
253
254         /* maybe save rDTR */
255
256         /* check rDTRfull in DSCR */
257
258         if (R(DSCR) & ARM11_DSCR_RDTR_FULL)
259         {
260                 /* MRC p14,0,R0,c0,c5,0 (move rDTR -> r0 (-> wDTR -> local var)) */
261                 retval = arm11_run_instr_data_from_core_via_r0(arm11, 0xEE100E15, &R(RDTR));
262                 if (retval != ERROR_OK)
263                         return retval;
264         }
265         else
266         {
267                 arm11->reg_list[ARM11_RC_RDTR].valid    = 0;
268         }
269
270         /* REVISIT Now that we've saved core state, there's may also
271          * be MMU and cache state to care about ...
272          */
273
274         if (arm11->simulate_reset_on_next_halt)
275         {
276                 arm11->simulate_reset_on_next_halt = false;
277
278                 LOG_DEBUG("Reset c1 Control Register");
279
280                 /* Write 0 (reset value) to Control register 0 to disable MMU/Cache etc. */
281
282                 /* MCR p15,0,R0,c1,c0,0 */
283                 retval = arm11_run_instr_data_to_core_via_r0(arm11, 0xee010f10, 0);
284                 if (retval != ERROR_OK)
285                         return retval;
286
287         }
288
289         retval = arm11_run_instr_data_finish(arm11);
290         if (retval != ERROR_OK)
291                 return retval;
292
293         return ERROR_OK;
294 }
295
296 /** Restore processor state
297   *
298   * This is called in preparation for the RESTART function.
299   *
300   */
301 static int arm11_leave_debug_state(struct arm11_common *arm11)
302 {
303         int retval;
304
305         /* See e.g. ARM1136 TRM, "14.8.5 Leaving Debug state" */
306
307         /* NOTE:  the ARM1136 TRM suggests restoring all registers
308          * except R0/PC/CPSR right now.  Instead, we do them all
309          * at once, just a bit later on.
310          */
311
312         /* REVISIT once we start caring about MMU and cache state,
313          * address it here ...
314          */
315
316         /* spec says clear wDTR and rDTR; we assume they are clear as
317            otherwise our programming would be sloppy */
318         {
319                 uint32_t DSCR;
320
321                 CHECK_RETVAL(arm11_read_DSCR(arm11, &DSCR));
322
323                 if (DSCR & (ARM11_DSCR_RDTR_FULL | ARM11_DSCR_WDTR_FULL))
324                 {
325                         /*
326                         The wDTR/rDTR two registers that are used to send/receive data to/from
327                         the core in tandem with corresponding instruction codes that are
328                         written into the core. The RDTR FULL/WDTR FULL flag indicates that the
329                         registers hold data that was written by one side (CPU or JTAG) and not
330                         read out by the other side.
331                         */
332                         LOG_ERROR("wDTR/rDTR inconsistent (DSCR %08" PRIx32 ")", DSCR);
333                         return ERROR_FAIL;
334                 }
335         }
336
337         /* maybe restore original wDTR */
338         if ((R(DSCR) & ARM11_DSCR_WDTR_FULL) || arm11->reg_list[ARM11_RC_WDTR].dirty)
339         {
340                 retval = arm11_run_instr_data_prepare(arm11);
341                 if (retval != ERROR_OK)
342                         return retval;
343
344                 /* MCR p14,0,R0,c0,c5,0 */
345                 retval = arm11_run_instr_data_to_core_via_r0(arm11, 0xee000e15, R(WDTR));
346                 if (retval != ERROR_OK)
347                         return retval;
348
349                 retval = arm11_run_instr_data_finish(arm11);
350                 if (retval != ERROR_OK)
351                         return retval;
352         }
353
354         /* restore CPSR, PC, and R0 ... after flushing any modified
355          * registers.
356          */
357         retval = arm_dpm_write_dirty_registers(&arm11->dpm);
358
359         register_cache_invalidate(arm11->arm.core_cache);
360
361         /* restore DSCR */
362
363         arm11_write_DSCR(arm11, R(DSCR));
364
365         /* maybe restore rDTR */
366
367         if (R(DSCR) & ARM11_DSCR_RDTR_FULL || arm11->reg_list[ARM11_RC_RDTR].dirty)
368         {
369                 arm11_add_debug_SCAN_N(arm11, 0x05, ARM11_TAP_DEFAULT);
370
371                 arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
372
373                 struct scan_field       chain5_fields[3];
374
375                 uint8_t                 Ready           = 0;    /* ignored */
376                 uint8_t                 Valid           = 0;    /* ignored */
377
378                 arm11_setup_field(arm11, 32, &R(RDTR),  NULL, chain5_fields + 0);
379                 arm11_setup_field(arm11,  1, &Ready,    NULL, chain5_fields + 1);
380                 arm11_setup_field(arm11,  1, &Valid,    NULL, chain5_fields + 2);
381
382                 arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields), chain5_fields, TAP_DRPAUSE);
383         }
384
385         /* now processor is ready to RESTART */
386
387         return ERROR_OK;
388 }
389
390 /* poll current target status */
391 static int arm11_poll(struct target *target)
392 {
393         int retval;
394         struct arm11_common *arm11 = target_to_arm11(target);
395         uint32_t        dscr;
396
397         CHECK_RETVAL(arm11_read_DSCR(arm11, &dscr));
398
399         LOG_DEBUG("DSCR %08" PRIx32 "", dscr);
400
401         CHECK_RETVAL(arm11_check_init(arm11, &dscr));
402
403         if (dscr & ARM11_DSCR_CORE_HALTED)
404         {
405                 if (target->state != TARGET_HALTED)
406                 {
407                         enum target_state old_state = target->state;
408
409                         LOG_DEBUG("enter TARGET_HALTED");
410                         target->state                   = TARGET_HALTED;
411                         target->debug_reason    = arm11_get_DSCR_debug_reason(dscr);
412                         retval = arm11_on_enter_debug_state(arm11);
413                         if (retval != ERROR_OK)
414                                 return retval;
415
416                         target_call_event_callbacks(target,
417                                 old_state == TARGET_DEBUG_RUNNING ? TARGET_EVENT_DEBUG_HALTED : TARGET_EVENT_HALTED);
418                 }
419         }
420         else
421         {
422                 if (target->state != TARGET_RUNNING && target->state != TARGET_DEBUG_RUNNING)
423                 {
424                         LOG_DEBUG("enter TARGET_RUNNING");
425                         target->state                   = TARGET_RUNNING;
426                         target->debug_reason    = DBG_REASON_NOTHALTED;
427                 }
428         }
429
430         return ERROR_OK;
431 }
432 /* architecture specific status reply */
433 static int arm11_arch_state(struct target *target)
434 {
435         int retval;
436
437         retval = armv4_5_arch_state(target);
438
439         /* REVISIT also display ARM11-specific MMU and cache status ... */
440
441         return retval;
442 }
443
444 /* target request support */
445 static int arm11_target_request_data(struct target *target,
446                 uint32_t size, uint8_t *buffer)
447 {
448         LOG_WARNING("Not implemented: %s", __func__);
449
450         return ERROR_FAIL;
451 }
452
453 /* target execution control */
454 static int arm11_halt(struct target *target)
455 {
456         struct arm11_common *arm11 = target_to_arm11(target);
457
458         LOG_DEBUG("target->state: %s",
459                 target_state_name(target));
460
461         if (target->state == TARGET_UNKNOWN)
462         {
463                 arm11->simulate_reset_on_next_halt = true;
464         }
465
466         if (target->state == TARGET_HALTED)
467         {
468                 LOG_DEBUG("target was already halted");
469                 return ERROR_OK;
470         }
471
472         arm11_add_IR(arm11, ARM11_HALT, TAP_IDLE);
473
474         CHECK_RETVAL(jtag_execute_queue());
475
476         uint32_t dscr;
477
478         int i = 0;
479         while (1)
480         {
481                 CHECK_RETVAL(arm11_read_DSCR(arm11, &dscr));
482
483                 if (dscr & ARM11_DSCR_CORE_HALTED)
484                         break;
485
486
487                 long long then = 0;
488                 if (i == 1000)
489                 {
490                         then = timeval_ms();
491                 }
492                 if (i >= 1000)
493                 {
494                         if ((timeval_ms()-then) > 1000)
495                         {
496                                 LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
497                                 return ERROR_FAIL;
498                         }
499                 }
500                 i++;
501         }
502
503         arm11_on_enter_debug_state(arm11);
504
505         enum target_state old_state     = target->state;
506
507         target->state           = TARGET_HALTED;
508         target->debug_reason    = arm11_get_DSCR_debug_reason(dscr);
509
510         CHECK_RETVAL(
511                 target_call_event_callbacks(target,
512                         old_state == TARGET_DEBUG_RUNNING ? TARGET_EVENT_DEBUG_HALTED : TARGET_EVENT_HALTED));
513
514         return ERROR_OK;
515 }
516
517 static uint32_t
518 arm11_nextpc(struct arm11_common *arm11, int current, uint32_t address)
519 {
520         void *value = arm11->arm.core_cache->reg_list[15].value;
521
522         if (!current)
523                 buf_set_u32(value, 0, 32, address);
524         else
525                 address = buf_get_u32(value, 0, 32);
526
527         return address;
528 }
529
530 static int arm11_resume(struct target *target, int current,
531                 uint32_t address, int handle_breakpoints, int debug_execution)
532 {
533         //        LOG_DEBUG("current %d  address %08x  handle_breakpoints %d  debug_execution %d",
534         //      current, address, handle_breakpoints, debug_execution);
535
536         struct arm11_common *arm11 = target_to_arm11(target);
537
538         LOG_DEBUG("target->state: %s",
539                 target_state_name(target));
540
541
542         if (target->state != TARGET_HALTED)
543         {
544                 LOG_ERROR("Target not halted");
545                 return ERROR_TARGET_NOT_HALTED;
546         }
547
548         address = arm11_nextpc(arm11, current, address);
549
550         LOG_DEBUG("RESUME PC %08" PRIx32 "%s", address, !current ? "!" : "");
551
552         /* clear breakpoints/watchpoints and VCR*/
553         arm11_sc7_clear_vbw(arm11);
554
555         if (!debug_execution)
556                 target_free_all_working_areas(target);
557
558         /* Set up breakpoints */
559         if (handle_breakpoints)
560         {
561                 /* check if one matches PC and step over it if necessary */
562
563                 struct breakpoint *     bp;
564
565                 for (bp = target->breakpoints; bp; bp = bp->next)
566                 {
567                         if (bp->address == address)
568                         {
569                                 LOG_DEBUG("must step over %08" PRIx32 "", bp->address);
570                                 arm11_step(target, 1, 0, 0);
571                                 break;
572                         }
573                 }
574
575                 /* set all breakpoints */
576
577                 unsigned brp_num = 0;
578
579                 for (bp = target->breakpoints; bp; bp = bp->next)
580                 {
581                         struct arm11_sc7_action brp[2];
582
583                         brp[0].write    = 1;
584                         brp[0].address  = ARM11_SC7_BVR0 + brp_num;
585                         brp[0].value    = bp->address;
586                         brp[1].write    = 1;
587                         brp[1].address  = ARM11_SC7_BCR0 + brp_num;
588                         brp[1].value    = 0x1 | (3 << 1) | (0x0F << 5) | (0 << 14) | (0 << 16) | (0 << 20) | (0 << 21);
589
590                         arm11_sc7_run(arm11, brp, ARRAY_SIZE(brp));
591
592                         LOG_DEBUG("Add BP %d at %08" PRIx32, brp_num,
593                                         bp->address);
594
595                         brp_num++;
596                 }
597
598                 arm11_sc7_set_vcr(arm11, arm11_vcr);
599         }
600
601         arm11_leave_debug_state(arm11);
602
603         arm11_add_IR(arm11, ARM11_RESTART, TAP_IDLE);
604
605         CHECK_RETVAL(jtag_execute_queue());
606
607         int i = 0;
608         while (1)
609         {
610                 uint32_t dscr;
611
612                 CHECK_RETVAL(arm11_read_DSCR(arm11, &dscr));
613
614                 LOG_DEBUG("DSCR %08" PRIx32 "", dscr);
615
616                 if (dscr & ARM11_DSCR_CORE_RESTARTED)
617                         break;
618
619
620                 long long then = 0;
621                 if (i == 1000)
622                 {
623                         then = timeval_ms();
624                 }
625                 if (i >= 1000)
626                 {
627                         if ((timeval_ms()-then) > 1000)
628                         {
629                                 LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
630                                 return ERROR_FAIL;
631                         }
632                 }
633                 i++;
634         }
635
636         if (!debug_execution)
637         {
638                 target->state                   = TARGET_RUNNING;
639                 target->debug_reason    = DBG_REASON_NOTHALTED;
640
641                 CHECK_RETVAL(target_call_event_callbacks(target, TARGET_EVENT_RESUMED));
642         }
643         else
644         {
645                 target->state                   = TARGET_DEBUG_RUNNING;
646                 target->debug_reason    = DBG_REASON_NOTHALTED;
647
648                 CHECK_RETVAL(target_call_event_callbacks(target, TARGET_EVENT_RESUMED));
649         }
650
651         return ERROR_OK;
652 }
653
654 static int arm11_step(struct target *target, int current,
655                 uint32_t address, int handle_breakpoints)
656 {
657         LOG_DEBUG("target->state: %s",
658                 target_state_name(target));
659
660         if (target->state != TARGET_HALTED)
661         {
662                 LOG_WARNING("target was not halted");
663                 return ERROR_TARGET_NOT_HALTED;
664         }
665
666         struct arm11_common *arm11 = target_to_arm11(target);
667
668         address = arm11_nextpc(arm11, current, address);
669
670         LOG_DEBUG("STEP PC %08" PRIx32 "%s", address, !current ? "!" : "");
671
672
673         /** \todo TODO: Thumb not supported here */
674
675         uint32_t        next_instruction;
676
677         CHECK_RETVAL(arm11_read_memory_word(arm11, address, &next_instruction));
678
679         /* skip over BKPT */
680         if ((next_instruction & 0xFFF00070) == 0xe1200070)
681         {
682                 address = arm11_nextpc(arm11, 0, address + 4);
683                 LOG_DEBUG("Skipping BKPT");
684         }
685         /* skip over Wait for interrupt / Standby */
686         /* mcr  15, 0, r?, cr7, cr0, {4} */
687         else if ((next_instruction & 0xFFFF0FFF) == 0xee070f90)
688         {
689                 address = arm11_nextpc(arm11, 0, address + 4);
690                 LOG_DEBUG("Skipping WFI");
691         }
692         /* ignore B to self */
693         else if ((next_instruction & 0xFEFFFFFF) == 0xeafffffe)
694         {
695                 LOG_DEBUG("Not stepping jump to self");
696         }
697         else
698         {
699                 /** \todo TODO: check if break-/watchpoints make any sense at all in combination
700                 * with this. */
701
702                 /** \todo TODO: check if disabling IRQs might be a good idea here. Alternatively
703                 * the VCR might be something worth looking into. */
704
705
706                 /* Set up breakpoint for stepping */
707
708                 struct arm11_sc7_action brp[2];
709
710                 brp[0].write    = 1;
711                 brp[0].address  = ARM11_SC7_BVR0;
712                 brp[1].write    = 1;
713                 brp[1].address  = ARM11_SC7_BCR0;
714
715                 if (arm11_config_hardware_step)
716                 {
717                         /* Hardware single stepping ("instruction address
718                          * mismatch") is used if enabled.  It's not quite
719                          * exactly "run one instruction"; "branch to here"
720                          * loops won't break, neither will some other cases,
721                          * but it's probably the best default.
722                          *
723                          * Hardware single stepping isn't supported on v6
724                          * debug modules.  ARM1176 and v7 can support it...
725                          *
726                          * FIXME Thumb stepping likely needs to use 0x03
727                          * or 0xc0 byte masks, not 0x0f.
728                          */
729                          brp[0].value   = address;
730                          brp[1].value   = 0x1 | (3 << 1) | (0x0F << 5)
731                                         | (0 << 14) | (0 << 16) | (0 << 20)
732                                         | (2 << 21);
733                 } else
734                 {
735                         /* Sets a breakpoint on the next PC, as calculated
736                          * by instruction set simulation.
737                          *
738                          * REVISIT stepping Thumb on ARM1156 requires Thumb2
739                          * support from the simulator.
740                          */
741                         uint32_t next_pc;
742                         int retval;
743
744                         retval = arm_simulate_step(target, &next_pc);
745                         if (retval != ERROR_OK)
746                                 return retval;
747
748                         brp[0].value    = next_pc;
749                         brp[1].value    = 0x1 | (3 << 1) | (0x0F << 5)
750                                         | (0 << 14) | (0 << 16) | (0 << 20)
751                                         | (0 << 21);
752                 }
753
754                 CHECK_RETVAL(arm11_sc7_run(arm11, brp, ARRAY_SIZE(brp)));
755
756                 /* resume */
757
758
759                 if (arm11_config_step_irq_enable)
760                         R(DSCR) &= ~ARM11_DSCR_INTERRUPTS_DISABLE;              /* should be redundant */
761                 else
762                         R(DSCR) |= ARM11_DSCR_INTERRUPTS_DISABLE;
763
764
765                 CHECK_RETVAL(arm11_leave_debug_state(arm11));
766
767                 arm11_add_IR(arm11, ARM11_RESTART, TAP_IDLE);
768
769                 CHECK_RETVAL(jtag_execute_queue());
770
771                 /* wait for halt */
772                 int i = 0;
773                 while (1)
774                 {
775                         uint32_t dscr;
776
777                         CHECK_RETVAL(arm11_read_DSCR(arm11, &dscr));
778
779                         LOG_DEBUG("DSCR %08" PRIx32 "e", dscr);
780
781                         if ((dscr & (ARM11_DSCR_CORE_RESTARTED | ARM11_DSCR_CORE_HALTED)) ==
782                                 (ARM11_DSCR_CORE_RESTARTED | ARM11_DSCR_CORE_HALTED))
783                                 break;
784
785                         long long then = 0;
786                         if (i == 1000)
787                         {
788                                 then = timeval_ms();
789                         }
790                         if (i >= 1000)
791                         {
792                                 if ((timeval_ms()-then) > 1000)
793                                 {
794                                         LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
795                                         return ERROR_FAIL;
796                                 }
797                         }
798                         i++;
799                 }
800
801                 /* clear breakpoint */
802                 arm11_sc7_clear_vbw(arm11);
803
804                 /* save state */
805                 CHECK_RETVAL(arm11_on_enter_debug_state(arm11));
806
807             /* restore default state */
808                 R(DSCR) &= ~ARM11_DSCR_INTERRUPTS_DISABLE;
809
810         }
811
812         //        target->state         = TARGET_HALTED;
813         target->debug_reason    = DBG_REASON_SINGLESTEP;
814
815         CHECK_RETVAL(target_call_event_callbacks(target, TARGET_EVENT_HALTED));
816
817         return ERROR_OK;
818 }
819
820 static int arm11_assert_reset(struct target *target)
821 {
822         int retval;
823         struct arm11_common *arm11 = target_to_arm11(target);
824
825         retval = arm11_check_init(arm11, NULL);
826         if (retval != ERROR_OK)
827                 return retval;
828
829         target->state = TARGET_UNKNOWN;
830
831         /* we would very much like to reset into the halted, state,
832          * but resetting and halting is second best... */
833         if (target->reset_halt)
834         {
835                 CHECK_RETVAL(target_halt(target));
836         }
837
838
839         /* srst is funny. We can not do *anything* else while it's asserted
840          * and it has unkonwn side effects. Make sure no other code runs
841          * meanwhile.
842          *
843          * Code below assumes srst:
844          *
845          * - Causes power-on-reset (but of what parts of the system?). Bug
846          * in arm11?
847          *
848          * - Messes us TAP state without asserting trst.
849          *
850          * - There is another bug in the arm11 core. When you generate an access to
851          * external logic (for example ddr controller via AHB bus) and that block
852          * is not configured (perhaps it is still held in reset), that transaction
853          * will never complete. This will hang arm11 core but it will also hang
854          * JTAG controller. Nothing, short of srst assertion will bring it out of
855          * this.
856          *
857          * Mysteries:
858          *
859          * - What should the PC be after an srst reset when starting in the halted
860          * state?
861          */
862
863         jtag_add_reset(0, 1);
864         jtag_add_reset(0, 0);
865
866         /* How long do we have to wait? */
867         jtag_add_sleep(5000);
868
869         /* un-mess up TAP state */
870         jtag_add_tlr();
871
872         retval = jtag_execute_queue();
873         if (retval != ERROR_OK)
874         {
875                 return retval;
876         }
877
878         return ERROR_OK;
879 }
880
881 static int arm11_deassert_reset(struct target *target)
882 {
883         return ERROR_OK;
884 }
885
886 static int arm11_soft_reset_halt(struct target *target)
887 {
888         LOG_WARNING("Not implemented: %s", __func__);
889
890         return ERROR_FAIL;
891 }
892
893 /* target memory access
894  * size: 1 = byte (8bit), 2 = half-word (16bit), 4 = word (32bit)
895  * count: number of items of <size>
896  *
897  * arm11_config_memrw_no_increment - in the future we may want to be able
898  * to read/write a range of data to a "port". a "port" is an action on
899  * read memory address for some peripheral.
900  */
901 static int arm11_read_memory_inner(struct target *target,
902                 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer,
903                 bool arm11_config_memrw_no_increment)
904 {
905         /** \todo TODO: check if buffer cast to uint32_t* and uint16_t* might cause alignment problems */
906         int retval;
907
908         if (target->state != TARGET_HALTED)
909         {
910                 LOG_WARNING("target was not halted");
911                 return ERROR_TARGET_NOT_HALTED;
912         }
913
914         LOG_DEBUG("ADDR %08" PRIx32 "  SIZE %08" PRIx32 "  COUNT %08" PRIx32 "", address, size, count);
915
916         struct arm11_common *arm11 = target_to_arm11(target);
917
918         retval = arm11_run_instr_data_prepare(arm11);
919         if (retval != ERROR_OK)
920                 return retval;
921
922         /* MRC p14,0,r0,c0,c5,0 */
923         retval = arm11_run_instr_data_to_core1(arm11, 0xee100e15, address);
924         if (retval != ERROR_OK)
925                 return retval;
926
927         switch (size)
928         {
929         case 1:
930                 arm11->arm.core_cache->reg_list[1].dirty = true;
931
932                 for (size_t i = 0; i < count; i++)
933                 {
934                         /* ldrb    r1, [r0], #1 */
935                         /* ldrb    r1, [r0] */
936                         arm11_run_instr_no_data1(arm11,
937                                         !arm11_config_memrw_no_increment ? 0xe4d01001 : 0xe5d01000);
938
939                         uint32_t res;
940                         /* MCR p14,0,R1,c0,c5,0 */
941                         arm11_run_instr_data_from_core(arm11, 0xEE001E15, &res, 1);
942
943                         *buffer++ = res;
944                 }
945
946                 break;
947
948         case 2:
949                 {
950                         arm11->arm.core_cache->reg_list[1].dirty = true;
951
952                         for (size_t i = 0; i < count; i++)
953                         {
954                                 /* ldrh    r1, [r0], #2 */
955                                 arm11_run_instr_no_data1(arm11,
956                                         !arm11_config_memrw_no_increment ? 0xe0d010b2 : 0xe1d010b0);
957
958                                 uint32_t res;
959
960                                 /* MCR p14,0,R1,c0,c5,0 */
961                                 arm11_run_instr_data_from_core(arm11, 0xEE001E15, &res, 1);
962
963                                 uint16_t svalue = res;
964                                 memcpy(buffer + i * sizeof(uint16_t), &svalue, sizeof(uint16_t));
965                         }
966
967                         break;
968                 }
969
970         case 4:
971                 {
972                 uint32_t instr = !arm11_config_memrw_no_increment ? 0xecb05e01 : 0xed905e00;
973                 /** \todo TODO: buffer cast to uint32_t* causes alignment warnings */
974                 uint32_t *words = (uint32_t *)buffer;
975
976                 /* LDC p14,c5,[R0],#4 */
977                 /* LDC p14,c5,[R0] */
978                 arm11_run_instr_data_from_core(arm11, instr, words, count);
979                 break;
980                 }
981         }
982
983         return arm11_run_instr_data_finish(arm11);
984 }
985
986 static int arm11_read_memory(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
987 {
988         return arm11_read_memory_inner(target, address, size, count, buffer, false);
989 }
990
991 /*
992 * arm11_config_memrw_no_increment - in the future we may want to be able
993 * to read/write a range of data to a "port". a "port" is an action on
994 * read memory address for some peripheral.
995 */
996 static int arm11_write_memory_inner(struct target *target,
997                 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer,
998                 bool arm11_config_memrw_no_increment)
999 {
1000         int retval;
1001
1002         if (target->state != TARGET_HALTED)
1003         {
1004                 LOG_WARNING("target was not halted");
1005                 return ERROR_TARGET_NOT_HALTED;
1006         }
1007
1008         LOG_DEBUG("ADDR %08" PRIx32 "  SIZE %08" PRIx32 "  COUNT %08" PRIx32 "", address, size, count);
1009
1010         struct arm11_common *arm11 = target_to_arm11(target);
1011
1012         retval = arm11_run_instr_data_prepare(arm11);
1013         if (retval != ERROR_OK)
1014                 return retval;
1015
1016         /* MRC p14,0,r0,c0,c5,0 */
1017         retval = arm11_run_instr_data_to_core1(arm11, 0xee100e15, address);
1018         if (retval != ERROR_OK)
1019                 return retval;
1020
1021         /* burst writes are not used for single words as those may well be
1022          * reset init script writes.
1023          *
1024          * The other advantage is that as burst writes are default, we'll
1025          * now exercise both burst and non-burst code paths with the
1026          * default settings, increasing code coverage.
1027          */
1028         bool burst = arm11_config_memwrite_burst && (count > 1);
1029
1030         switch (size)
1031         {
1032         case 1:
1033                 {
1034                         arm11->arm.core_cache->reg_list[1].dirty = true;
1035
1036                         for (size_t i = 0; i < count; i++)
1037                         {
1038                                 /* MRC p14,0,r1,c0,c5,0 */
1039                                 retval = arm11_run_instr_data_to_core1(arm11, 0xee101e15, *buffer++);
1040                                 if (retval != ERROR_OK)
1041                                         return retval;
1042
1043                                 /* strb    r1, [r0], #1 */
1044                                 /* strb    r1, [r0] */
1045                                 retval = arm11_run_instr_no_data1(arm11,
1046                                         !arm11_config_memrw_no_increment ? 0xe4c01001 : 0xe5c01000);
1047                                 if (retval != ERROR_OK)
1048                                         return retval;
1049                         }
1050
1051                         break;
1052                 }
1053
1054         case 2:
1055                 {
1056                         arm11->arm.core_cache->reg_list[1].dirty = true;
1057
1058                         for (size_t i = 0; i < count; i++)
1059                         {
1060                                 uint16_t value;
1061                                 memcpy(&value, buffer + i * sizeof(uint16_t), sizeof(uint16_t));
1062
1063                                 /* MRC p14,0,r1,c0,c5,0 */
1064                                 retval = arm11_run_instr_data_to_core1(arm11, 0xee101e15, value);
1065                                 if (retval != ERROR_OK)
1066                                         return retval;
1067
1068                                 /* strh    r1, [r0], #2 */
1069                                 /* strh    r1, [r0] */
1070                                 retval = arm11_run_instr_no_data1(arm11,
1071                                         !arm11_config_memrw_no_increment ? 0xe0c010b2 : 0xe1c010b0);
1072                                 if (retval != ERROR_OK)
1073                                         return retval;
1074                         }
1075
1076                         break;
1077                 }
1078
1079         case 4: {
1080                 uint32_t instr = !arm11_config_memrw_no_increment ? 0xeca05e01 : 0xed805e00;
1081
1082                 /** \todo TODO: buffer cast to uint32_t* causes alignment warnings */
1083                 uint32_t *words = (uint32_t*)buffer;
1084
1085                 if (!burst)
1086                 {
1087                         /* STC p14,c5,[R0],#4 */
1088                         /* STC p14,c5,[R0]*/
1089                         retval = arm11_run_instr_data_to_core(arm11, instr, words, count);
1090                         if (retval != ERROR_OK)
1091                                 return retval;
1092                 }
1093                 else
1094                 {
1095                         /* STC p14,c5,[R0],#4 */
1096                         /* STC p14,c5,[R0]*/
1097                         retval = arm11_run_instr_data_to_core_noack(arm11, instr, words, count);
1098                         if (retval != ERROR_OK)
1099                                 return retval;
1100                 }
1101
1102                 break;
1103         }
1104         }
1105
1106         /* r0 verification */
1107         if (!arm11_config_memrw_no_increment)
1108         {
1109                 uint32_t r0;
1110
1111                 /* MCR p14,0,R0,c0,c5,0 */
1112                 retval = arm11_run_instr_data_from_core(arm11, 0xEE000E15, &r0, 1);
1113                 if (retval != ERROR_OK)
1114                         return retval;
1115
1116                 if (address + size * count != r0)
1117                 {
1118                         LOG_ERROR("Data transfer failed. Expected end "
1119                                         "address 0x%08x, got 0x%08x",
1120                                         (unsigned) (address + size * count),
1121                                         (unsigned) r0);
1122
1123                         if (burst)
1124                                 LOG_ERROR("use 'arm11 memwrite burst disable' to disable fast burst mode");
1125
1126                         if (arm11_config_memwrite_error_fatal)
1127                                 return ERROR_FAIL;
1128                 }
1129         }
1130
1131         return arm11_run_instr_data_finish(arm11);
1132 }
1133
1134 static int arm11_write_memory(struct target *target,
1135                 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1136 {
1137         return arm11_write_memory_inner(target, address, size, count, buffer, false);
1138 }
1139
1140 /* write target memory in multiples of 4 byte, optimized for writing large quantities of data */
1141 static int arm11_bulk_write_memory(struct target *target,
1142                 uint32_t address, uint32_t count, uint8_t *buffer)
1143 {
1144         if (target->state != TARGET_HALTED)
1145         {
1146                 LOG_WARNING("target was not halted");
1147                 return ERROR_TARGET_NOT_HALTED;
1148         }
1149
1150         return arm11_write_memory(target, address, 4, count, buffer);
1151 }
1152
1153 /* target break-/watchpoint control
1154 * rw: 0 = write, 1 = read, 2 = access
1155 */
1156 static int arm11_add_breakpoint(struct target *target,
1157                 struct breakpoint *breakpoint)
1158 {
1159         struct arm11_common *arm11 = target_to_arm11(target);
1160
1161 #if 0
1162         if (breakpoint->type == BKPT_SOFT)
1163         {
1164                 LOG_INFO("sw breakpoint requested, but software breakpoints not enabled");
1165                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1166         }
1167 #endif
1168
1169         if (!arm11->free_brps)
1170         {
1171                 LOG_DEBUG("no breakpoint unit available for hardware breakpoint");
1172                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1173         }
1174
1175         if (breakpoint->length != 4)
1176         {
1177                 LOG_DEBUG("only breakpoints of four bytes length supported");
1178                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1179         }
1180
1181         arm11->free_brps--;
1182
1183         return ERROR_OK;
1184 }
1185
1186 static int arm11_remove_breakpoint(struct target *target,
1187                 struct breakpoint *breakpoint)
1188 {
1189         struct arm11_common *arm11 = target_to_arm11(target);
1190
1191         arm11->free_brps++;
1192
1193         return ERROR_OK;
1194 }
1195
1196 static int arm11_add_watchpoint(struct target *target,
1197                 struct watchpoint *watchpoint)
1198 {
1199         LOG_WARNING("Not implemented: %s", __func__);
1200
1201         return ERROR_FAIL;
1202 }
1203
1204 static int arm11_remove_watchpoint(struct target *target,
1205                 struct watchpoint *watchpoint)
1206 {
1207         LOG_WARNING("Not implemented: %s", __func__);
1208
1209         return ERROR_FAIL;
1210 }
1211
1212 static int arm11_target_create(struct target *target, Jim_Interp *interp)
1213 {
1214         struct arm11_common *arm11;
1215
1216         if (target->tap == NULL)
1217                 return ERROR_FAIL;
1218
1219         if (target->tap->ir_length != 5)
1220         {
1221                 LOG_ERROR("'target arm11' expects IR LENGTH = 5");
1222                 return ERROR_COMMAND_SYNTAX_ERROR;
1223         }
1224
1225         arm11 = calloc(1, sizeof *arm11);
1226         if (!arm11)
1227                 return ERROR_FAIL;
1228
1229         armv4_5_init_arch_info(target, &arm11->arm);
1230
1231         arm11->target = target;
1232
1233         arm11->jtag_info.tap = target->tap;
1234         arm11->jtag_info.scann_size = 5;
1235         arm11->jtag_info.scann_instr = ARM11_SCAN_N;
1236         /* cur_scan_chain == 0 */
1237         arm11->jtag_info.intest_instr = ARM11_INTEST;
1238
1239         return ERROR_OK;
1240 }
1241
1242 static int arm11_init_target(struct command_context *cmd_ctx,
1243                 struct target *target)
1244 {
1245         /* Initialize anything we can set up without talking to the target */
1246
1247         /* REVISIT do we really want such a debug-registers-only cache?
1248          * If we do, it should probably be handled purely by the DPM code,
1249          * so it works identically on the v7a/v7r cores.
1250          */
1251         return arm11_build_reg_cache(target);
1252 }
1253
1254 /* talk to the target and set things up */
1255 static int arm11_examine(struct target *target)
1256 {
1257         int retval;
1258         char *type;
1259         struct arm11_common *arm11 = target_to_arm11(target);
1260         uint32_t didr, device_id;
1261         uint8_t implementor;
1262
1263         /* FIXME split into do-first-time and do-every-time logic ... */
1264
1265         /* check IDCODE */
1266
1267         arm11_add_IR(arm11, ARM11_IDCODE, ARM11_TAP_DEFAULT);
1268
1269         struct scan_field               idcode_field;
1270
1271         arm11_setup_field(arm11, 32, NULL, &device_id, &idcode_field);
1272
1273         arm11_add_dr_scan_vc(1, &idcode_field, TAP_DRPAUSE);
1274
1275         /* check DIDR */
1276
1277         arm11_add_debug_SCAN_N(arm11, 0x00, ARM11_TAP_DEFAULT);
1278
1279         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
1280
1281         struct scan_field               chain0_fields[2];
1282
1283         arm11_setup_field(arm11, 32, NULL, &didr, chain0_fields + 0);
1284         arm11_setup_field(arm11,  8, NULL, &implementor, chain0_fields + 1);
1285
1286         arm11_add_dr_scan_vc(ARRAY_SIZE(chain0_fields), chain0_fields, TAP_IDLE);
1287
1288         CHECK_RETVAL(jtag_execute_queue());
1289
1290         switch (device_id & 0x0FFFF000)
1291         {
1292         case 0x07B36000:
1293                 type = "ARM1136";
1294                 break;
1295         case 0x07B56000:
1296                 type = "ARM1156";
1297                 break;
1298         case 0x07B76000:
1299                 arm11->arm.core_type = ARM_MODE_MON;
1300                 type = "ARM1176";
1301                 break;
1302         default:
1303                 LOG_ERROR("'target arm11' expects IDCODE 0x*7B*7****");
1304                 return ERROR_FAIL;
1305         }
1306         LOG_INFO("found %s", type);
1307
1308         /* unlikely this could ever fail, but ... */
1309         switch ((didr >> 16) & 0x0F) {
1310         case ARM11_DEBUG_V6:
1311         case ARM11_DEBUG_V61:           /* supports security extensions */
1312                 break;
1313         default:
1314                 LOG_ERROR("Only ARM v6 and v6.1 debug supported.");
1315                 return ERROR_FAIL;
1316         }
1317
1318         arm11->brp = ((didr >> 24) & 0x0F) + 1;
1319         arm11->wrp = ((didr >> 28) & 0x0F) + 1;
1320
1321         /** \todo TODO: reserve one brp slot if we allow breakpoints during step */
1322         arm11->free_brps = arm11->brp;
1323         arm11->free_wrps = arm11->wrp;
1324
1325         LOG_DEBUG("IDCODE %08" PRIx32 " IMPLEMENTOR %02x DIDR %08" PRIx32,
1326                         device_id, implementor, didr);
1327
1328         /* as a side-effect this reads DSCR and thus
1329          * clears the ARM11_DSCR_STICKY_PRECISE_DATA_ABORT / Sticky Precise Data Abort Flag
1330          * as suggested by the spec.
1331          */
1332
1333         retval = arm11_check_init(arm11, NULL);
1334         if (retval != ERROR_OK)
1335                 return retval;
1336
1337         /* Build register cache "late", after target_init(), since we
1338          * want to know if this core supports Secure Monitor mode.
1339          */
1340         if (!target_was_examined(target)) {
1341                 arm11_dpm_init(arm11, didr);
1342                 retval = arm_dpm_setup(&arm11->dpm);
1343         }
1344
1345         /* ETM on ARM11 still uses original scanchain 6 access mode */
1346         if (arm11->arm.etm && !target_was_examined(target)) {
1347                 *register_get_last_cache_p(&target->reg_cache) =
1348                         etm_build_reg_cache(target, &arm11->jtag_info,
1349                                         arm11->arm.etm);
1350                 retval = etm_setup(target);
1351         }
1352
1353         target_set_examined(target);
1354
1355         return ERROR_OK;
1356 }
1357
1358
1359 /** Load a register that is marked !valid in the register cache */
1360 static int arm11_get_reg(struct reg *reg)
1361 {
1362         struct target * target = ((struct arm11_reg_state *)reg->arch_info)->target;
1363
1364         if (target->state != TARGET_HALTED)
1365         {
1366                 LOG_WARNING("target was not halted");
1367                 return ERROR_TARGET_NOT_HALTED;
1368         }
1369
1370         /** \todo TODO: Check this. We assume that all registers are fetched at debug entry. */
1371
1372 #if 0
1373         struct arm11_common *arm11 = target_to_arm11(target);
1374         const struct arm11_reg_defs *arm11_reg_info = arm11_reg_defs + ((struct arm11_reg_state *)reg->arch_info)->def_index;
1375 #endif
1376
1377         return ERROR_OK;
1378 }
1379
1380 /** Change a value in the register cache */
1381 static int arm11_set_reg(struct reg *reg, uint8_t *buf)
1382 {
1383         struct target *target = ((struct arm11_reg_state *)reg->arch_info)->target;
1384         struct arm11_common *arm11 = target_to_arm11(target);
1385 //      const struct arm11_reg_defs *arm11_reg_info = arm11_reg_defs + ((struct arm11_reg_state *)reg->arch_info)->def_index;
1386
1387         arm11->reg_values[((struct arm11_reg_state *)reg->arch_info)->def_index] = buf_get_u32(buf, 0, 32);
1388         reg->valid      = 1;
1389         reg->dirty      = 1;
1390
1391         return ERROR_OK;
1392 }
1393
1394 static const struct reg_arch_type arm11_reg_type = {
1395         .get = arm11_get_reg,
1396         .set = arm11_set_reg,
1397 };
1398
1399 static int arm11_build_reg_cache(struct target *target)
1400 {
1401         struct arm11_common *arm11 = target_to_arm11(target);
1402         struct reg_cache *cache;
1403         struct reg *reg_list;
1404         struct arm11_reg_state *arm11_reg_states;
1405
1406         cache = calloc(1, sizeof *cache);
1407         reg_list = calloc(ARM11_REGCACHE_COUNT, sizeof *reg_list);
1408         arm11_reg_states = calloc(ARM11_REGCACHE_COUNT,
1409                         sizeof *arm11_reg_states);
1410         if (!cache || !reg_list || !arm11_reg_states) {
1411                 free(cache);
1412                 free(reg_list);
1413                 free(arm11_reg_states);
1414                 return ERROR_FAIL;
1415         }
1416
1417         arm11->reg_list = reg_list;
1418
1419         /* build cache for some of the debug registers */
1420         cache->name = "arm11 debug registers";
1421         cache->reg_list = reg_list;
1422         cache->num_regs = ARM11_REGCACHE_COUNT;
1423
1424         struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
1425         (*cache_p) = cache;
1426
1427         arm11->core_cache = cache;
1428
1429         size_t i;
1430
1431         /* Not very elegant assertion */
1432         if (ARM11_REGCACHE_COUNT != ARRAY_SIZE(arm11->reg_values) ||
1433                 ARM11_REGCACHE_COUNT != ARRAY_SIZE(arm11_reg_defs) ||
1434                 ARM11_REGCACHE_COUNT != ARM11_RC_MAX)
1435         {
1436                 LOG_ERROR("BUG: arm11->reg_values inconsistent (%d %u %u %d)",
1437                                 ARM11_REGCACHE_COUNT,
1438                                 (unsigned) ARRAY_SIZE(arm11->reg_values),
1439                                 (unsigned) ARRAY_SIZE(arm11_reg_defs),
1440                                 ARM11_RC_MAX);
1441                 /* FIXME minimally, use a build_bug_on(X) mechanism;
1442                  * runtime exit() here is bad!
1443                  */
1444                 exit(-1);
1445         }
1446
1447         for (i = 0; i < ARM11_REGCACHE_COUNT; i++)
1448         {
1449                 struct reg *                                            r       = reg_list                      + i;
1450                 const struct arm11_reg_defs *   rd      = arm11_reg_defs        + i;
1451                 struct arm11_reg_state *                        rs      = arm11_reg_states      + i;
1452
1453                 r->name                         = rd->name;
1454                 r->size                         = 32;
1455                 r->value                        = (uint8_t *)(arm11->reg_values + i);
1456                 r->dirty                        = 0;
1457                 r->valid                        = 0;
1458                 r->type = &arm11_reg_type;
1459                 r->arch_info            = rs;
1460
1461                 rs->def_index           = i;
1462                 rs->target                      = target;
1463         }
1464
1465         return ERROR_OK;
1466 }
1467
1468 /* FIXME all these BOOL_WRAPPER things should be modifying
1469  * per-instance state, not shared state; ditto the vector
1470  * catch register support.  Scan chains with multiple cores
1471  * should be able to say "work with this core like this,
1472  * that core like that".  Example, ARM11 MPCore ...
1473  */
1474
1475 #define ARM11_BOOL_WRAPPER(name, print_name)    \
1476                 COMMAND_HANDLER(arm11_handle_bool_##name) \
1477                 { \
1478                         return CALL_COMMAND_HANDLER(handle_command_parse_bool, \
1479                                         &arm11_config_##name, print_name); \
1480                 }
1481
1482 ARM11_BOOL_WRAPPER(memwrite_burst, "memory write burst mode")
1483 ARM11_BOOL_WRAPPER(memwrite_error_fatal, "fatal error mode for memory writes")
1484 ARM11_BOOL_WRAPPER(step_irq_enable, "IRQs while stepping")
1485 ARM11_BOOL_WRAPPER(hardware_step, "hardware single step")
1486
1487 COMMAND_HANDLER(arm11_handle_vcr)
1488 {
1489         switch (CMD_ARGC) {
1490         case 0:
1491                 break;
1492         case 1:
1493                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], arm11_vcr);
1494                 break;
1495         default:
1496                 return ERROR_COMMAND_SYNTAX_ERROR;
1497         }
1498
1499         LOG_INFO("VCR 0x%08" PRIx32 "", arm11_vcr);
1500         return ERROR_OK;
1501 }
1502
1503 static const uint32_t arm11_coproc_instruction_limits[] =
1504 {
1505         15,                             /* coprocessor */
1506         7,                              /* opcode 1 */
1507         15,                             /* CRn */
1508         15,                             /* CRm */
1509         7,                              /* opcode 2 */
1510         0xFFFFFFFF,             /* value */
1511 };
1512
1513 static int arm11_mrc_inner(struct target *target, int cpnum,
1514                 uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm,
1515                 uint32_t *value, bool read)
1516 {
1517         int retval;
1518         struct arm11_common *arm11 = target_to_arm11(target);
1519
1520         if (target->state != TARGET_HALTED)
1521         {
1522                 LOG_ERROR("Target not halted");
1523                 return ERROR_FAIL;
1524         }
1525
1526         uint32_t instr = 0xEE000010     |
1527                 (cpnum <<  8) |
1528                 (op1 << 21) |
1529                 (CRn << 16) |
1530                 (CRm <<  0) |
1531                 (op2 <<  5);
1532
1533         if (read)
1534                 instr |= 0x00100000;
1535
1536         retval = arm11_run_instr_data_prepare(arm11);
1537         if (retval != ERROR_OK)
1538                 return retval;
1539
1540         if (read)
1541         {
1542                 retval = arm11_run_instr_data_from_core_via_r0(arm11, instr, value);
1543                 if (retval != ERROR_OK)
1544                         return retval;
1545         }
1546         else
1547         {
1548                 retval = arm11_run_instr_data_to_core_via_r0(arm11, instr, *value);
1549                 if (retval != ERROR_OK)
1550                         return retval;
1551         }
1552
1553         return arm11_run_instr_data_finish(arm11);
1554 }
1555
1556 static int arm11_mrc(struct target *target, int cpnum,
1557                 uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
1558 {
1559         return arm11_mrc_inner(target, cpnum, op1, op2, CRn, CRm, value, true);
1560 }
1561
1562 static int arm11_mcr(struct target *target, int cpnum,
1563                 uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
1564 {
1565         return arm11_mrc_inner(target, cpnum, op1, op2, CRn, CRm, &value, false);
1566 }
1567
1568 static int arm11_register_commands(struct command_context *cmd_ctx)
1569 {
1570         struct command *top_cmd, *mw_cmd;
1571
1572         armv4_5_register_commands(cmd_ctx);
1573
1574         top_cmd = COMMAND_REGISTER(cmd_ctx, NULL, "arm11",
1575                         NULL, COMMAND_ANY, NULL);
1576
1577         /* "hardware_step" is only here to check if the default
1578          * simulate + breakpoint implementation is broken.
1579          * TEMPORARY! NOT DOCUMENTED!
1580          */
1581         COMMAND_REGISTER(cmd_ctx, top_cmd, "hardware_step",
1582                         arm11_handle_bool_hardware_step, COMMAND_ANY,
1583                         "DEBUG ONLY - Hardware single stepping"
1584                                 " (default: disabled)");
1585
1586         mw_cmd = COMMAND_REGISTER(cmd_ctx, top_cmd, "memwrite",
1587                         NULL, COMMAND_ANY, NULL);
1588         COMMAND_REGISTER(cmd_ctx, mw_cmd, "burst",
1589                         arm11_handle_bool_memwrite_burst, COMMAND_ANY,
1590                         "Enable/Disable non-standard but fast burst mode"
1591                                 " (default: enabled)");
1592         COMMAND_REGISTER(cmd_ctx, mw_cmd, "error_fatal",
1593                         arm11_handle_bool_memwrite_error_fatal, COMMAND_ANY,
1594                         "Terminate program if transfer error was found"
1595                                 " (default: enabled)");
1596
1597         COMMAND_REGISTER(cmd_ctx, top_cmd, "step_irq_enable",
1598                         arm11_handle_bool_step_irq_enable, COMMAND_ANY,
1599                         "Enable interrupts while stepping"
1600                                 " (default: disabled)");
1601         COMMAND_REGISTER(cmd_ctx, top_cmd, "vcr",
1602                         arm11_handle_vcr, COMMAND_ANY,
1603                         "Control (Interrupt) Vector Catch Register");
1604
1605         return etm_register_commands(cmd_ctx);
1606 }
1607
1608 /** Holds methods for ARM11xx targets. */
1609 struct target_type arm11_target = {
1610         .name =                 "arm11",
1611
1612         .poll =                 arm11_poll,
1613         .arch_state =           arm11_arch_state,
1614
1615         .target_request_data =  arm11_target_request_data,
1616
1617         .halt =                 arm11_halt,
1618         .resume =               arm11_resume,
1619         .step =                 arm11_step,
1620
1621         .assert_reset =         arm11_assert_reset,
1622         .deassert_reset =       arm11_deassert_reset,
1623         .soft_reset_halt =      arm11_soft_reset_halt,
1624
1625         .get_gdb_reg_list =     armv4_5_get_gdb_reg_list,
1626
1627         .read_memory =          arm11_read_memory,
1628         .write_memory =         arm11_write_memory,
1629
1630         .bulk_write_memory =    arm11_bulk_write_memory,
1631
1632         .checksum_memory =      arm_checksum_memory,
1633         .blank_check_memory =   arm_blank_check_memory,
1634
1635         .add_breakpoint =       arm11_add_breakpoint,
1636         .remove_breakpoint =    arm11_remove_breakpoint,
1637         .add_watchpoint =       arm11_add_watchpoint,
1638         .remove_watchpoint =    arm11_remove_watchpoint,
1639
1640         .run_algorithm =        armv4_5_run_algorithm,
1641
1642         .register_commands =    arm11_register_commands,
1643         .target_create =        arm11_target_create,
1644         .init_target =          arm11_init_target,
1645         .examine =              arm11_examine,
1646
1647         .mrc =                  arm11_mrc,
1648         .mcr =                  arm11_mcr,
1649 };