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