Audit and eliminate redundant #include directives in arm target files.
[fw/openocd] / src / target / arm11_dbgtap.c
1 /***************************************************************************
2  *   Copyright (C) 2008 digenius technology GmbH.                          *
3  *                                                                         *
4  *   Copyright (C) 2008 Oyvind Harboe oyvind.harboe@zylin.com              *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "arm11.h"
27
28
29 #if 0
30 #define JTAG_DEBUG(expr ...)    DEBUG(expr)
31 #else
32 #define JTAG_DEBUG(expr ...)    do {} while(0)
33 #endif
34
35 /*
36 This pathmove goes from Pause-IR to Shift-IR while avoiding RTI. The
37 behavior of the FTDI driver IIRC was to go via RTI.
38
39 Conversely there may be other places in this code where the ARM11 code relies
40 on the driver to hit through RTI when coming from Update-?R.
41 */
42 tap_state_t arm11_move_pi_to_si_via_ci[] =
43 {
44     TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IRSHIFT
45 };
46
47
48 int arm11_add_ir_scan_vc(int num_fields, scan_field_t *fields, tap_state_t state)
49 {
50         if (cmd_queue_cur_state == TAP_IRPAUSE)
51                 jtag_add_pathmove(asizeof(arm11_move_pi_to_si_via_ci), arm11_move_pi_to_si_via_ci);
52
53         jtag_add_ir_scan(num_fields, fields, state);
54         return ERROR_OK;
55 }
56
57 tap_state_t arm11_move_pd_to_sd_via_cd[] =
58 {
59         TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
60 };
61
62 int arm11_add_dr_scan_vc(int num_fields, scan_field_t *fields, tap_state_t state)
63 {
64         if (cmd_queue_cur_state == TAP_DRPAUSE)
65                 jtag_add_pathmove(asizeof(arm11_move_pd_to_sd_via_cd), arm11_move_pd_to_sd_via_cd);
66
67         jtag_add_dr_scan(num_fields, fields, state);
68         return ERROR_OK;
69 }
70
71
72 /** Code de-clutter: Construct scan_field_t to write out a value
73  *
74  * \param arm11                 Target state variable.
75  * \param num_bits              Length of the data field
76  * \param out_data              pointer to the data that will be sent out
77  *                                              <em>(data is read when it is added to the JTAG queue)</em>
78  * \param in_data               pointer to the memory that will receive data that was clocked in
79  *                                              <em>(data is written when the JTAG queue is executed)</em>
80  * \param field                 target data structure that will be initialized
81  */
82 void arm11_setup_field(arm11_common_t * arm11, int num_bits, void * out_data, void * in_data, scan_field_t * field)
83 {
84         field->tap                      = arm11->jtag_info.tap;
85         field->num_bits                 = num_bits;
86         field->out_value                = out_data;
87         field->in_value                 = in_data;
88 }
89
90
91 /** Write JTAG instruction register
92  *
93  * \param arm11         Target state variable.
94  * \param instr         An ARM11 DBGTAP instruction. Use enum #arm11_instructions.
95  * \param state         Pass the final TAP state or ARM11_TAP_DEFAULT for the default value (Pause-IR).
96  *
97  * \remarks                     This adds to the JTAG command queue but does \em not execute it.
98  */
99 void arm11_add_IR(arm11_common_t * arm11, u8 instr, tap_state_t state)
100 {
101         jtag_tap_t *tap;
102         tap = arm11->jtag_info.tap;
103
104         if (buf_get_u32(tap->cur_instr, 0, 5) == instr)
105         {
106                 JTAG_DEBUG("IR <= 0x%02x SKIPPED", instr);
107                 return;
108         }
109
110         JTAG_DEBUG("IR <= 0x%02x", instr);
111
112         scan_field_t field;
113
114         arm11_setup_field(arm11, 5, &instr, NULL, &field);
115
116         arm11_add_ir_scan_vc(1, &field, state == ARM11_TAP_DEFAULT ? TAP_IRPAUSE : state);
117 }
118
119 /** Verify shifted out data from Scan Chain Register (SCREG)
120  *  Used as parameter to scan_field_t::in_handler in
121  *  arm11_add_debug_SCAN_N().
122  *
123  */
124 static void arm11_in_handler_SCAN_N(u8 *in_value)
125 {
126         /** \todo TODO: clarify why this isnt properly masked in jtag.c jtag_read_buffer() */
127         u8 v = *in_value & 0x1F;
128
129         if (v != 0x10)
130         {
131                 LOG_ERROR("'arm11 target' JTAG communication error SCREG SCAN OUT 0x%02x (expected 0x10)", v);
132                 jtag_set_error(ERROR_FAIL);
133         }
134
135         JTAG_DEBUG("SCREG SCAN OUT 0x%02x", v);
136 }
137
138 /** Select and write to Scan Chain Register (SCREG)
139  *
140  * This function sets the instruction register to SCAN_N and writes
141  * the data register with the selected chain number.
142  *
143  * http://infocenter.arm.com/help/topic/com.arm.doc.ddi0301f/Cacbjhfg.html
144  *
145  * \param arm11     Target state variable.
146  * \param chain     Scan chain that will be selected.
147  * \param state     Pass the final TAP state or ARM11_TAP_DEFAULT for the default
148  *                                      value (Pause-DR).
149  *
150  * The chain takes effect when Update-DR is passed (usually when subsequently
151  * the INTEXT/EXTEST instructions are written).
152  *
153  * \warning                     (Obsolete) Using this twice in a row will \em fail. The first
154  *                                      call will end in Pause-DR. The second call, due to the IR
155  *                                      caching, will not go through Capture-DR when shifting in the
156  *                                      new scan chain number. As a result the verification in
157  *                                      arm11_in_handler_SCAN_N() must fail.
158  *
159  * \remarks                     This adds to the JTAG command queue but does \em not execute it.
160  */
161
162 void arm11_add_debug_SCAN_N(arm11_common_t * arm11, u8 chain, tap_state_t state)
163 {
164         JTAG_DEBUG("SCREG <= 0x%02x", chain);
165
166         arm11_add_IR(arm11, ARM11_SCAN_N, ARM11_TAP_DEFAULT);
167
168         scan_field_t            field;
169
170         u8 tmp[1];
171         arm11_setup_field(arm11, 5, &chain, &tmp, &field);
172
173         arm11_add_dr_scan_vc(1, &field, state == ARM11_TAP_DEFAULT ? TAP_DRPAUSE : state);
174
175         jtag_execute_queue_noclear();
176
177         arm11_in_handler_SCAN_N(tmp);
178 }
179
180 /** Write an instruction into the ITR register
181  *
182  * \param arm11         Target state variable.
183  * \param inst          An ARM11 processor instruction/opcode.
184  * \param flag          Optional parameter to retrieve the InstCompl flag
185  *                                      (this will be written when the JTAG chain is executed).
186  * \param state         Pass the final TAP state or ARM11_TAP_DEFAULT for the default
187  *                                      value (Run-Test/Idle).
188  *
189  * \remarks                     By default this ends with Run-Test/Idle state
190  *                                      and causes the instruction to be executed. If
191  *                                      a subsequent write to DTR is needed before
192  *                                      executing the instruction then TAP_DRPAUSE should be
193  *                                      passed to \p state.
194  *
195  * \remarks                     This adds to the JTAG command queue but does \em not execute it.
196  */
197 void arm11_add_debug_INST(arm11_common_t * arm11, u32 inst, u8 * flag, tap_state_t state)
198 {
199         JTAG_DEBUG("INST <= 0x%08x", inst);
200
201         scan_field_t            itr[2];
202
203         arm11_setup_field(arm11, 32,    &inst,  NULL, itr + 0);
204         arm11_setup_field(arm11, 1,         NULL,       flag, itr + 1);
205
206         arm11_add_dr_scan_vc(asizeof(itr), itr, state == ARM11_TAP_DEFAULT ? TAP_IDLE : state);
207 }
208
209 /** Read the Debug Status and Control Register (DSCR)
210  *
211  * same as CP14 c1
212  *
213  * \param arm11         Target state variable.
214  * \return                      DSCR content
215  *
216  * \remarks                     This is a stand-alone function that executes the JTAG command queue.
217  */
218 int arm11_read_DSCR(arm11_common_t * arm11, u32 *value)
219 {
220         arm11_add_debug_SCAN_N(arm11, 0x01, ARM11_TAP_DEFAULT);
221
222         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
223
224         u32                             dscr;
225         scan_field_t    chain1_field;
226
227         arm11_setup_field(arm11, 32, NULL, &dscr, &chain1_field);
228
229         arm11_add_dr_scan_vc(1, &chain1_field, TAP_DRPAUSE);
230
231         CHECK_RETVAL(jtag_execute_queue());
232
233         if (arm11->last_dscr != dscr)
234                 JTAG_DEBUG("DSCR  = %08x (OLD %08x)", dscr, arm11->last_dscr);
235
236         arm11->last_dscr = dscr;
237
238         *value=dscr;
239
240         return ERROR_OK;
241 }
242
243 /** Write the Debug Status and Control Register (DSCR)
244  *
245  * same as CP14 c1
246  *
247  * \param arm11         Target state variable.
248  * \param dscr          DSCR content
249  *
250  * \remarks                     This is a stand-alone function that executes the JTAG command queue.
251  */
252 int arm11_write_DSCR(arm11_common_t * arm11, u32 dscr)
253 {
254         arm11_add_debug_SCAN_N(arm11, 0x01, ARM11_TAP_DEFAULT);
255
256         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
257
258         scan_field_t                chain1_field;
259
260         arm11_setup_field(arm11, 32, &dscr, NULL, &chain1_field);
261
262         arm11_add_dr_scan_vc(1, &chain1_field, TAP_DRPAUSE);
263
264         CHECK_RETVAL(jtag_execute_queue());
265
266         JTAG_DEBUG("DSCR <= %08x (OLD %08x)", dscr, arm11->last_dscr);
267
268         arm11->last_dscr = dscr;
269
270         return ERROR_OK;
271 }
272
273
274
275 /** Get the debug reason from Debug Status and Control Register (DSCR)
276  *
277  * \param dscr          DSCR value to analyze
278  * \return                      Debug reason
279  *
280  */
281 enum target_debug_reason arm11_get_DSCR_debug_reason(u32 dscr)
282 {
283         switch (dscr & ARM11_DSCR_METHOD_OF_DEBUG_ENTRY_MASK)
284         {
285         case ARM11_DSCR_METHOD_OF_DEBUG_ENTRY_HALT:
286                 LOG_INFO("Debug entry: JTAG HALT");
287                 return DBG_REASON_DBGRQ;
288
289         case ARM11_DSCR_METHOD_OF_DEBUG_ENTRY_BREAKPOINT:
290                 LOG_INFO("Debug entry: breakpoint");
291                 return DBG_REASON_BREAKPOINT;
292
293         case ARM11_DSCR_METHOD_OF_DEBUG_ENTRY_WATCHPOINT:
294                 LOG_INFO("Debug entry: watchpoint");
295                 return DBG_REASON_WATCHPOINT;
296
297         case ARM11_DSCR_METHOD_OF_DEBUG_ENTRY_BKPT_INSTRUCTION:
298                 LOG_INFO("Debug entry: BKPT instruction");
299                 return DBG_REASON_BREAKPOINT;
300
301         case ARM11_DSCR_METHOD_OF_DEBUG_ENTRY_EDBGRQ:
302                 LOG_INFO("Debug entry: EDBGRQ signal");
303                 return DBG_REASON_DBGRQ;
304
305         case ARM11_DSCR_METHOD_OF_DEBUG_ENTRY_VECTOR_CATCH:
306                 LOG_INFO("Debug entry: VCR vector catch");
307                 return DBG_REASON_BREAKPOINT;
308
309         default:
310                 LOG_INFO("Debug entry: unknown");
311                 return DBG_REASON_DBGRQ;
312         }
313 };
314
315
316
317 /** Prepare the stage for ITR/DTR operations
318  * from the arm11_run_instr... group of functions.
319  *
320  * Put arm11_run_instr_data_prepare() and arm11_run_instr_data_finish()
321  * around a block of arm11_run_instr_... calls.
322  *
323  * Select scan chain 5 to allow quick access to DTR. When scan
324  * chain 4 is needed to put in a register the ITRSel instruction
325  * shortcut is used instead of actually changing the Scan_N
326  * register.
327  *
328  * \param arm11         Target state variable.
329  *
330  */
331 void arm11_run_instr_data_prepare(arm11_common_t * arm11)
332 {
333         arm11_add_debug_SCAN_N(arm11, 0x05, ARM11_TAP_DEFAULT);
334 }
335
336 /** Cleanup after ITR/DTR operations
337  * from the arm11_run_instr... group of functions
338  *
339  * Put arm11_run_instr_data_prepare() and arm11_run_instr_data_finish()
340  * around a block of arm11_run_instr_... calls.
341  *
342  * Any IDLE can lead to an instruction execution when
343  * scan chains 4 or 5 are selected and the IR holds
344  * INTEST or EXTEST. So we must disable that before
345  * any following activities lead to an IDLE.
346  *
347  * \param arm11         Target state variable.
348  *
349  */
350 void arm11_run_instr_data_finish(arm11_common_t * arm11)
351 {
352         arm11_add_debug_SCAN_N(arm11, 0x00, ARM11_TAP_DEFAULT);
353 }
354
355
356 /** Execute one or multiple instructions via ITR
357  *
358  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
359  *
360  * \param arm11         Target state variable.
361  * \param opcode        Pointer to sequence of ARM opcodes
362  * \param count         Number of opcodes to execute
363  *
364  */
365 int arm11_run_instr_no_data(arm11_common_t * arm11, u32 * opcode, size_t count)
366 {
367         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
368
369         while (count--)
370         {
371                 arm11_add_debug_INST(arm11, *opcode++, NULL, TAP_IDLE);
372
373                 while (1)
374                 {
375                         u8 flag;
376
377                         arm11_add_debug_INST(arm11, 0, &flag, count ? TAP_IDLE : TAP_DRPAUSE);
378
379                         CHECK_RETVAL(jtag_execute_queue());
380
381                         if (flag)
382                                 break;
383                 }
384         }
385
386         return ERROR_OK;
387 }
388
389 /** Execute one instruction via ITR
390  *
391  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
392  *
393  * \param arm11         Target state variable.
394  * \param opcode        ARM opcode
395  *
396  */
397 void arm11_run_instr_no_data1(arm11_common_t * arm11, u32 opcode)
398 {
399         arm11_run_instr_no_data(arm11, &opcode, 1);
400 }
401
402
403 /** Execute one instruction via ITR repeatedly while
404  *  passing data to the core via DTR on each execution.
405  *
406  *  The executed instruction \em must read data from DTR.
407  *
408  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
409  *
410  * \param arm11         Target state variable.
411  * \param opcode        ARM opcode
412  * \param data          Pointer to the data words to be passed to the core
413  * \param count         Number of data words and instruction repetitions
414  *
415  */
416 int arm11_run_instr_data_to_core(arm11_common_t * arm11, u32 opcode, u32 * data, size_t count)
417 {
418         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
419
420         arm11_add_debug_INST(arm11, opcode, NULL, TAP_DRPAUSE);
421
422         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
423
424         scan_field_t    chain5_fields[3];
425
426         u32                             Data;
427         u8                              Ready;
428         u8                              nRetry;
429
430         arm11_setup_field(arm11, 32,    &Data,  NULL,           chain5_fields + 0);
431         arm11_setup_field(arm11,  1,    NULL,   &Ready,         chain5_fields + 1);
432         arm11_setup_field(arm11,  1,    NULL,   &nRetry,        chain5_fields + 2);
433
434         while (count--)
435         {
436                 do
437                 {
438                         Data        = *data;
439
440                         arm11_add_dr_scan_vc(asizeof(chain5_fields), chain5_fields, TAP_IDLE);
441
442                         CHECK_RETVAL(jtag_execute_queue());
443
444                         JTAG_DEBUG("DTR  Ready %d  nRetry %d", Ready, nRetry);
445                 }
446                 while (!Ready);
447
448                 data++;
449         }
450
451         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
452
453         do
454         {
455                 Data        = 0;
456
457                 arm11_add_dr_scan_vc(asizeof(chain5_fields), chain5_fields, TAP_DRPAUSE);
458
459                 CHECK_RETVAL(jtag_execute_queue());
460
461                 JTAG_DEBUG("DTR  Data %08x  Ready %d  nRetry %d", Data, Ready, nRetry);
462         }
463         while (!Ready);
464
465         return ERROR_OK;
466 }
467
468 /** JTAG path for arm11_run_instr_data_to_core_noack
469  *
470  *  The repeated TAP_IDLE's do not cause a repeated execution
471  *  if passed without leaving the state.
472  *
473  *  Since this is more than 7 bits (adjustable via adding more
474  *  TAP_IDLE's) it produces an artificial delay in the lower
475  *  layer (FT2232) that is long enough to finish execution on
476  *  the core but still shorter than any manually inducible delays.
477  *
478  *  To disable this code, try "memwrite burst false"
479  *
480  */
481 tap_state_t arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay[] =
482 {
483         TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
484 };
485
486
487
488 /** Execute one instruction via ITR repeatedly while
489  *  passing data to the core via DTR on each execution.
490  *
491  *  No Ready check during transmission.
492  *
493  *  The executed instruction \em must read data from DTR.
494  *
495  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
496  *
497  * \param arm11         Target state variable.
498  * \param opcode        ARM opcode
499  * \param data          Pointer to the data words to be passed to the core
500  * \param count         Number of data words and instruction repetitions
501  *
502  */
503 int arm11_run_instr_data_to_core_noack(arm11_common_t * arm11, u32 opcode, u32 * data, size_t count)
504 {
505         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
506
507         arm11_add_debug_INST(arm11, opcode, NULL, TAP_DRPAUSE);
508
509         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
510
511         scan_field_t    chain5_fields[3];
512
513         arm11_setup_field(arm11, 32,    NULL/*&Data*/,  NULL,                           chain5_fields + 0);
514         arm11_setup_field(arm11,  1,    NULL,                   NULL /*&Ready*/,        chain5_fields + 1);
515         arm11_setup_field(arm11,  1,    NULL,                   NULL,                           chain5_fields + 2);
516
517         u8                      Readies[count + 1];
518         u8      *               ReadyPos                        = Readies;
519
520         while (count--)
521         {
522                 chain5_fields[0].out_value      = (void *)(data++);
523                 chain5_fields[1].in_value       = ReadyPos++;
524
525                 if (count)
526                 {
527                         jtag_add_dr_scan(asizeof(chain5_fields), chain5_fields, TAP_DRPAUSE);
528                         jtag_add_pathmove(asizeof(arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay),
529                                 arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay);
530                 }
531                 else
532                 {
533                         jtag_add_dr_scan(asizeof(chain5_fields), chain5_fields, TAP_IDLE);
534                 }
535         }
536
537         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
538
539         chain5_fields[0].out_value      = 0;
540         chain5_fields[1].in_value   = ReadyPos++;
541
542         arm11_add_dr_scan_vc(asizeof(chain5_fields), chain5_fields, TAP_DRPAUSE);
543
544         CHECK_RETVAL(jtag_execute_queue());
545
546         size_t error_count = 0;
547
548         for (size_t i = 0; i < asizeof(Readies); i++)
549         {
550                 if (Readies[i] != 1)
551                 {
552                         error_count++;
553                 }
554         }
555
556         if (error_count)
557                 LOG_ERROR("Transfer errors " ZU, error_count);
558
559         return ERROR_OK;
560 }
561
562
563 /** Execute an instruction via ITR while handing data into the core via DTR.
564  *
565  *  The executed instruction \em must read data from DTR.
566  *
567  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
568  *
569  * \param arm11         Target state variable.
570  * \param opcode        ARM opcode
571  * \param data          Data word to be passed to the core via DTR
572  *
573  */
574 int arm11_run_instr_data_to_core1(arm11_common_t * arm11, u32 opcode, u32 data)
575 {
576         return arm11_run_instr_data_to_core(arm11, opcode, &data, 1);
577 }
578
579
580 /** Execute one instruction via ITR repeatedly while
581  *  reading data from the core via DTR on each execution.
582  *
583  *  The executed instruction \em must write data to DTR.
584  *
585  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
586  *
587  * \param arm11         Target state variable.
588  * \param opcode        ARM opcode
589  * \param data          Pointer to an array that receives the data words from the core
590  * \param count         Number of data words and instruction repetitions
591  *
592  */
593 int arm11_run_instr_data_from_core(arm11_common_t * arm11, u32 opcode, u32 * data, size_t count)
594 {
595         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
596
597         arm11_add_debug_INST(arm11, opcode, NULL, TAP_IDLE);
598
599         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
600
601         scan_field_t    chain5_fields[3];
602
603         u32                     Data;
604         u8                      Ready;
605         u8                      nRetry;
606
607         arm11_setup_field(arm11, 32,    NULL,   &Data,      chain5_fields + 0);
608         arm11_setup_field(arm11,  1,    NULL,   &Ready,     chain5_fields + 1);
609         arm11_setup_field(arm11,  1,    NULL,   &nRetry,    chain5_fields + 2);
610
611         while (count--)
612         {
613                 do
614                 {
615                         arm11_add_dr_scan_vc(asizeof(chain5_fields), chain5_fields, count ? TAP_IDLE : TAP_DRPAUSE);
616
617                         CHECK_RETVAL(jtag_execute_queue());
618
619                         JTAG_DEBUG("DTR  Data %08x  Ready %d  nRetry %d", Data, Ready, nRetry);
620                 }
621                 while (!Ready);
622
623                 *data++ = Data;
624         }
625
626         return ERROR_OK;
627 }
628
629 /** Execute one instruction via ITR
630  *  then load r0 into DTR and read DTR from core.
631  *
632  *  The first executed instruction (\p opcode) should write data to r0.
633  *
634  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
635  *
636  * \param arm11         Target state variable.
637  * \param opcode        ARM opcode to write r0 with the value of interest
638  * \param data          Pointer to a data word that receives the value from r0 after \p opcode was executed.
639  *
640  */
641 void arm11_run_instr_data_from_core_via_r0(arm11_common_t * arm11, u32 opcode, u32 * data)
642 {
643         arm11_run_instr_no_data1(arm11, opcode);
644
645         /* MCR p14,0,R0,c0,c5,0 (move r0 -> wDTR -> local var) */
646         arm11_run_instr_data_from_core(arm11, 0xEE000E15, data, 1);
647 }
648
649 /** Load data into core via DTR then move it to r0 then
650  *  execute one instruction via ITR
651  *
652  *  The final executed instruction (\p opcode) should read data from r0.
653  *
654  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
655  *
656  * \param arm11         Target state variable.
657  * \param opcode        ARM opcode to read r0 act upon it
658  * \param data          Data word that will be written to r0 before \p opcode is executed
659  *
660  */
661 void arm11_run_instr_data_to_core_via_r0(arm11_common_t * arm11, u32 opcode, u32 data)
662 {
663         /* MRC p14,0,r0,c0,c5,0 */
664         arm11_run_instr_data_to_core1(arm11, 0xEE100E15, data);
665
666         arm11_run_instr_no_data1(arm11, opcode);
667 }
668
669 /** Apply reads and writes to scan chain 7
670  *
671  * \see arm11_sc7_action_t
672  *
673  * \param arm11         Target state variable.
674  * \param actions       A list of read and/or write instructions
675  * \param count         Number of instructions in the list.
676  *
677  */
678 int arm11_sc7_run(arm11_common_t * arm11, arm11_sc7_action_t * actions, size_t count)
679 {
680         arm11_add_debug_SCAN_N(arm11, 0x07, ARM11_TAP_DEFAULT);
681
682         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
683
684         scan_field_t    chain7_fields[3];
685
686         u8                              nRW;
687         u32                             DataOut;
688         u8                              AddressOut;
689         u8                              Ready;
690         u32                             DataIn;
691         u8                              AddressIn;
692
693         arm11_setup_field(arm11,  1, &nRW,                      &Ready,         chain7_fields + 0);
694         arm11_setup_field(arm11, 32, &DataOut,          &DataIn,        chain7_fields + 1);
695         arm11_setup_field(arm11,  7, &AddressOut,       &AddressIn,     chain7_fields + 2);
696
697         for (size_t i = 0; i < count + 1; i++)
698         {
699                 if (i < count)
700                 {
701                         nRW                     = actions[i].write ? 1 : 0;
702                         DataOut         = actions[i].value;
703                         AddressOut      = actions[i].address;
704                 }
705                 else
706                 {
707                         nRW                     = 0;
708                         DataOut         = 0;
709                         AddressOut      = 0;
710                 }
711
712                 do
713                 {
714                         JTAG_DEBUG("SC7 <= Address %02x  Data %08x    nRW %d", AddressOut, DataOut, nRW);
715
716                         arm11_add_dr_scan_vc(asizeof(chain7_fields), chain7_fields, TAP_DRPAUSE);
717
718                         CHECK_RETVAL(jtag_execute_queue());
719
720                         JTAG_DEBUG("SC7 => Address %02x  Data %08x  Ready %d", AddressIn, DataIn, Ready);
721                 }
722                 while (!Ready); /* 'nRW' is 'Ready' on read out */
723
724                 if (i > 0)
725                 {
726                         if (actions[i - 1].address != AddressIn)
727                         {
728                                 LOG_WARNING("Scan chain 7 shifted out unexpected address");
729                         }
730
731                         if (!actions[i - 1].write)
732                         {
733                                 actions[i - 1].value = DataIn;
734                         }
735                         else
736                         {
737                                 if (actions[i - 1].value != DataIn)
738                                 {
739                                         LOG_WARNING("Scan chain 7 shifted out unexpected data");
740                                 }
741                         }
742                 }
743         }
744
745         for (size_t i = 0; i < count; i++)
746         {
747                 JTAG_DEBUG("SC7 %02d: %02x %s %08x", i, actions[i].address, actions[i].write ? "<=" : "=>", actions[i].value);
748         }
749
750         return ERROR_OK;
751 }
752
753 /** Clear VCR and all breakpoints and watchpoints via scan chain 7
754  *
755  * \param arm11         Target state variable.
756  *
757  */
758 void arm11_sc7_clear_vbw(arm11_common_t * arm11)
759 {
760         arm11_sc7_action_t              clear_bw[arm11->brp + arm11->wrp + 1];
761         arm11_sc7_action_t *    pos = clear_bw;
762
763         for (size_t i = 0; i < asizeof(clear_bw); i++)
764         {
765                 clear_bw[i].write       = true;
766                 clear_bw[i].value       = 0;
767         }
768
769         for (size_t i = 0; i < arm11->brp; i++)
770                 (pos++)->address = ARM11_SC7_BCR0 + i;
771
772
773         for (size_t i = 0; i < arm11->wrp; i++)
774                 (pos++)->address = ARM11_SC7_WCR0 + i;
775
776
777         (pos++)->address = ARM11_SC7_VCR;
778
779         arm11_sc7_run(arm11, clear_bw, asizeof(clear_bw));
780 }
781
782 /** Write VCR register
783  *
784  * \param arm11         Target state variable.
785  * \param value         Value to be written
786  */
787 void arm11_sc7_set_vcr(arm11_common_t * arm11, u32 value)
788 {
789         arm11_sc7_action_t              set_vcr;
790
791         set_vcr.write           = true;
792         set_vcr.address         = ARM11_SC7_VCR;
793         set_vcr.value           = value;
794
795
796         arm11_sc7_run(arm11, &set_vcr, 1);
797 }
798
799
800
801 /** Read word from address
802  *
803  * \param arm11         Target state variable.
804  * \param address       Memory address to be read
805  * \param result        Pointer where to store result
806  *
807  */
808 int arm11_read_memory_word(arm11_common_t * arm11, u32 address, u32 * result)
809 {
810         arm11_run_instr_data_prepare(arm11);
811
812         /* MRC p14,0,r0,c0,c5,0 (r0 = address) */
813         CHECK_RETVAL(arm11_run_instr_data_to_core1(arm11, 0xee100e15, address));
814
815         /* LDC p14,c5,[R0],#4 (DTR = [r0]) */
816         CHECK_RETVAL(arm11_run_instr_data_from_core(arm11, 0xecb05e01, result, 1));
817
818         arm11_run_instr_data_finish(arm11);
819
820         return ERROR_OK;
821 }
822
823