ARM DPM: share debug reason logic
[fw/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 <helper/time_support.h>
31
32 #if 0
33 #define JTAG_DEBUG(expr ...)    do { if (1) LOG_DEBUG(expr); } while (0)
34 #else
35 #define JTAG_DEBUG(expr ...)    do { if (0) LOG_DEBUG(expr); } 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 static int arm11_add_ir_scan_vc(int num_fields, struct scan_field *fields,
52                 tap_state_t state)
53 {
54         if (cmd_queue_cur_state == TAP_IRPAUSE)
55                 jtag_add_pathmove(ARRAY_SIZE(arm11_move_pi_to_si_via_ci), arm11_move_pi_to_si_via_ci);
56
57         jtag_add_ir_scan(num_fields, fields, state);
58         return ERROR_OK;
59 }
60
61 static const tap_state_t arm11_move_pd_to_sd_via_cd[] =
62 {
63         TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
64 };
65
66 int arm11_add_dr_scan_vc(int num_fields, struct scan_field *fields, tap_state_t state)
67 {
68         if (cmd_queue_cur_state == TAP_DRPAUSE)
69                 jtag_add_pathmove(ARRAY_SIZE(arm11_move_pd_to_sd_via_cd), arm11_move_pd_to_sd_via_cd);
70
71         jtag_add_dr_scan(num_fields, fields, state);
72         return ERROR_OK;
73 }
74
75
76 /** Code de-clutter: Construct struct scan_field to write out a value
77  *
78  * \param arm11                 Target state variable.
79  * \param num_bits              Length of the data field
80  * \param out_data              pointer to the data that will be sent out
81  *                                              <em > (data is read when it is added to the JTAG queue)</em>
82  * \param in_data               pointer to the memory that will receive data that was clocked in
83  *                                              <em > (data is written when the JTAG queue is executed)</em>
84  * \param field                 target data structure that will be initialized
85  */
86 void arm11_setup_field(struct arm11_common * arm11, int num_bits, void * out_data, void * in_data, struct scan_field * field)
87 {
88         field->tap                      = arm11->arm.target->tap;
89         field->num_bits                 = num_bits;
90         field->out_value                = out_data;
91         field->in_value                 = in_data;
92 }
93
94
95 /** Write JTAG instruction register
96  *
97  * \param arm11         Target state variable.
98  * \param instr         An ARM11 DBGTAP instruction. Use enum #arm11_instructions.
99  * \param state         Pass the final TAP state or ARM11_TAP_DEFAULT for the default value (Pause-IR).
100  *
101  * \remarks                     This adds to the JTAG command queue but does \em not execute it.
102  */
103 void arm11_add_IR(struct arm11_common * arm11, uint8_t instr, tap_state_t state)
104 {
105         struct jtag_tap *tap = arm11->arm.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 static void arm11_add_debug_INST(struct arm11_common * arm11,
205                 uint32_t inst, uint8_t * flag, tap_state_t state)
206 {
207         JTAG_DEBUG("INST <= 0x%08x", (unsigned) inst);
208
209         struct scan_field               itr[2];
210
211         arm11_setup_field(arm11, 32,    &inst,  NULL, itr + 0);
212         arm11_setup_field(arm11, 1,         NULL,       flag, itr + 1);
213
214         arm11_add_dr_scan_vc(ARRAY_SIZE(itr), itr, state == ARM11_TAP_DEFAULT ? TAP_IDLE : state);
215 }
216
217 /**
218  * Read and save the Debug Status and Control Register (DSCR).
219  *
220  * \param arm11         Target state variable.
221  * \return Error status; arm11->dscr is updated on success.
222  *
223  * \remarks This is a stand-alone function that executes the JTAG
224  * command queue.  It does not require the ARM11 debug TAP to be
225  * in any particular state.
226  */
227 int arm11_read_DSCR(struct arm11_common *arm11)
228 {
229         int retval;
230
231         retval = arm11_add_debug_SCAN_N(arm11, 0x01, ARM11_TAP_DEFAULT);
232         if (retval != ERROR_OK)
233                 return retval;
234
235         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
236
237         uint32_t                                dscr;
238         struct scan_field       chain1_field;
239
240         arm11_setup_field(arm11, 32, NULL, &dscr, &chain1_field);
241
242         arm11_add_dr_scan_vc(1, &chain1_field, TAP_DRPAUSE);
243
244         CHECK_RETVAL(jtag_execute_queue());
245
246         if (arm11->dscr != dscr)
247                 JTAG_DEBUG("DSCR  = %08x (OLD %08x)",
248                                 (unsigned) dscr,
249                                 (unsigned) arm11->dscr);
250
251         arm11->dscr = dscr;
252
253         return ERROR_OK;
254 }
255
256 /** Write the Debug Status and Control Register (DSCR)
257  *
258  * same as CP14 c1
259  *
260  * \param arm11         Target state variable.
261  * \param dscr          DSCR content
262  *
263  * \remarks                     This is a stand-alone function that executes the JTAG command queue.
264  */
265 int arm11_write_DSCR(struct arm11_common * arm11, uint32_t dscr)
266 {
267         int retval;
268         retval = arm11_add_debug_SCAN_N(arm11, 0x01, ARM11_TAP_DEFAULT);
269         if (retval != ERROR_OK)
270                 return retval;
271
272         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
273
274         struct scan_field                   chain1_field;
275
276         arm11_setup_field(arm11, 32, &dscr, NULL, &chain1_field);
277
278         arm11_add_dr_scan_vc(1, &chain1_field, TAP_DRPAUSE);
279
280         CHECK_RETVAL(jtag_execute_queue());
281
282         JTAG_DEBUG("DSCR <= %08x (OLD %08x)",
283                         (unsigned) dscr,
284                         (unsigned) arm11->dscr);
285
286         arm11->dscr = dscr;
287
288         return ERROR_OK;
289 }
290
291 /** Prepare the stage for ITR/DTR operations
292  * from the arm11_run_instr... group of functions.
293  *
294  * Put arm11_run_instr_data_prepare() and arm11_run_instr_data_finish()
295  * around a block of arm11_run_instr_... calls.
296  *
297  * Select scan chain 5 to allow quick access to DTR. When scan
298  * chain 4 is needed to put in a register the ITRSel instruction
299  * shortcut is used instead of actually changing the Scan_N
300  * register.
301  *
302  * \param arm11         Target state variable.
303  *
304  */
305 int arm11_run_instr_data_prepare(struct arm11_common * arm11)
306 {
307         return arm11_add_debug_SCAN_N(arm11, 0x05, ARM11_TAP_DEFAULT);
308 }
309
310 /** Cleanup after ITR/DTR operations
311  * from the arm11_run_instr... group of functions
312  *
313  * Put arm11_run_instr_data_prepare() and arm11_run_instr_data_finish()
314  * around a block of arm11_run_instr_... calls.
315  *
316  * Any IDLE can lead to an instruction execution when
317  * scan chains 4 or 5 are selected and the IR holds
318  * INTEST or EXTEST. So we must disable that before
319  * any following activities lead to an IDLE.
320  *
321  * \param arm11         Target state variable.
322  *
323  */
324 int arm11_run_instr_data_finish(struct arm11_common * arm11)
325 {
326         return arm11_add_debug_SCAN_N(arm11, 0x00, ARM11_TAP_DEFAULT);
327 }
328
329
330
331 /** Execute one or multiple instructions via ITR
332  *
333  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
334  *
335  * \param arm11         Target state variable.
336  * \param opcode        Pointer to sequence of ARM opcodes
337  * \param count         Number of opcodes to execute
338  *
339  */
340 static
341 int arm11_run_instr_no_data(struct arm11_common * arm11,
342                 uint32_t * opcode, size_t count)
343 {
344         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
345
346         while (count--)
347         {
348                 arm11_add_debug_INST(arm11, *opcode++, NULL, TAP_IDLE);
349
350                 int i = 0;
351                 while (1)
352                 {
353                         uint8_t flag;
354
355                         arm11_add_debug_INST(arm11, 0, &flag, count ? TAP_IDLE : TAP_DRPAUSE);
356
357                         CHECK_RETVAL(jtag_execute_queue());
358
359                         if (flag)
360                                 break;
361
362                         long long then = 0;
363
364                         if (i == 1000)
365                         {
366                                 then = timeval_ms();
367                         }
368                         if (i >= 1000)
369                         {
370                                 if ((timeval_ms()-then) > 1000)
371                                 {
372                                         LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
373                                         return ERROR_FAIL;
374                                 }
375                         }
376
377                         i++;
378                 }
379         }
380
381         return ERROR_OK;
382 }
383
384 /** Execute one instruction via ITR
385  *
386  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
387  *
388  * \param arm11         Target state variable.
389  * \param opcode        ARM opcode
390  *
391  */
392 int arm11_run_instr_no_data1(struct arm11_common * arm11, uint32_t opcode)
393 {
394         return arm11_run_instr_no_data(arm11, &opcode, 1);
395 }
396
397
398 /** Execute one instruction via ITR repeatedly while
399  *  passing data to the core via DTR on each execution.
400  *
401  *  The executed instruction \em must read data from DTR.
402  *
403  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
404  *
405  * \param arm11         Target state variable.
406  * \param opcode        ARM opcode
407  * \param data          Pointer to the data words to be passed to the core
408  * \param count         Number of data words and instruction repetitions
409  *
410  */
411 int arm11_run_instr_data_to_core(struct arm11_common * arm11, uint32_t opcode, uint32_t * data, size_t count)
412 {
413         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
414
415         arm11_add_debug_INST(arm11, opcode, NULL, TAP_DRPAUSE);
416
417         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
418
419         struct scan_field       chain5_fields[3];
420
421         uint32_t                                Data;
422         uint8_t                         Ready;
423         uint8_t                         nRetry;
424
425         arm11_setup_field(arm11, 32,    &Data,  NULL,           chain5_fields + 0);
426         arm11_setup_field(arm11,  1,    NULL,   &Ready,         chain5_fields + 1);
427         arm11_setup_field(arm11,  1,    NULL,   &nRetry,        chain5_fields + 2);
428
429         while (count--)
430         {
431                 int i = 0;
432                 do
433                 {
434                         Data        = *data;
435
436                         arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields), chain5_fields, jtag_set_end_state(TAP_IDLE));
437
438                         CHECK_RETVAL(jtag_execute_queue());
439
440                         JTAG_DEBUG("DTR  Ready %d  nRetry %d", Ready, nRetry);
441
442                         long long then = 0;
443
444                         if (i == 1000)
445                         {
446                                 then = timeval_ms();
447                         }
448                         if (i >= 1000)
449                         {
450                                 if ((timeval_ms()-then) > 1000)
451                                 {
452                                         LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
453                                         return ERROR_FAIL;
454                                 }
455                         }
456
457                         i++;
458                 }
459                 while (!Ready);
460
461                 data++;
462         }
463
464         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
465
466         int i = 0;
467         do
468         {
469                 Data        = 0;
470
471                 arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields), chain5_fields, TAP_DRPAUSE);
472
473                 CHECK_RETVAL(jtag_execute_queue());
474
475                 JTAG_DEBUG("DTR  Data %08x  Ready %d  nRetry %d",
476                                 (unsigned) Data, Ready, nRetry);
477
478                 long long then = 0;
479
480                 if (i == 1000)
481                 {
482                         then = timeval_ms();
483                 }
484                 if (i >= 1000)
485                 {
486                         if ((timeval_ms()-then) > 1000)
487                         {
488                                 LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
489                                 return ERROR_FAIL;
490                         }
491                 }
492
493                 i++;
494         }
495         while (!Ready);
496
497         return ERROR_OK;
498 }
499
500 /** JTAG path for arm11_run_instr_data_to_core_noack
501  *
502  *  The repeated TAP_IDLE's do not cause a repeated execution
503  *  if passed without leaving the state.
504  *
505  *  Since this is more than 7 bits (adjustable via adding more
506  *  TAP_IDLE's) it produces an artificial delay in the lower
507  *  layer (FT2232) that is long enough to finish execution on
508  *  the core but still shorter than any manually inducible delays.
509  *
510  *  To disable this code, try "memwrite burst false"
511  *
512  *  FIX!!! should we use multiple TAP_IDLE here or not???
513  *
514  *  https://lists.berlios.de/pipermail/openocd-development/2009-July/009698.html
515  *  https://lists.berlios.de/pipermail/openocd-development/2009-August/009865.html
516  */
517 static const tap_state_t arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay[] =
518 {
519         TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
520 };
521
522
523
524 /** Execute one instruction via ITR repeatedly while
525  *  passing data to the core via DTR on each execution.
526  *
527  *  No Ready check during transmission.
528  *
529  *  The executed instruction \em must read data from DTR.
530  *
531  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
532  *
533  * \param arm11         Target state variable.
534  * \param opcode        ARM opcode
535  * \param data          Pointer to the data words to be passed to the core
536  * \param count         Number of data words and instruction repetitions
537  *
538  */
539 int arm11_run_instr_data_to_core_noack(struct arm11_common * arm11, uint32_t opcode, uint32_t * data, size_t count)
540 {
541         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
542
543         arm11_add_debug_INST(arm11, opcode, NULL, TAP_DRPAUSE);
544
545         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
546
547         struct scan_field       chain5_fields[3];
548
549         arm11_setup_field(arm11, 32,    NULL/*&Data*/,  NULL,                           chain5_fields + 0);
550         arm11_setup_field(arm11,  1,    NULL,                   NULL /*&Ready*/,        chain5_fields + 1);
551         arm11_setup_field(arm11,  1,    NULL,                   NULL,                           chain5_fields + 2);
552
553         uint8_t                 *Readies;
554         unsigned readiesNum = count + 1;
555         unsigned bytes = sizeof(*Readies)*readiesNum;
556
557         Readies = (uint8_t *) malloc(bytes);
558         if (Readies == NULL)
559         {
560                 LOG_ERROR("Out of memory allocating %u bytes", bytes);
561                 return ERROR_FAIL;
562         }
563
564         uint8_t *               ReadyPos                        = Readies;
565
566         while (count--)
567         {
568                 chain5_fields[0].out_value      = (void *)(data++);
569                 chain5_fields[1].in_value       = ReadyPos++;
570
571                 if (count)
572                 {
573                         jtag_add_dr_scan(ARRAY_SIZE(chain5_fields), chain5_fields, jtag_set_end_state(TAP_DRPAUSE));
574                         jtag_add_pathmove(ARRAY_SIZE(arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay),
575                                 arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay);
576                 }
577                 else
578                 {
579                         jtag_add_dr_scan(ARRAY_SIZE(chain5_fields), chain5_fields, jtag_set_end_state(TAP_IDLE));
580                 }
581         }
582
583         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
584
585         chain5_fields[0].out_value      = 0;
586         chain5_fields[1].in_value   = ReadyPos++;
587
588         arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields), chain5_fields, TAP_DRPAUSE);
589
590         int retval = jtag_execute_queue();
591         if (retval == ERROR_OK)
592         {
593                 unsigned error_count = 0;
594
595                 for (size_t i = 0; i < readiesNum; i++)
596                 {
597                         if (Readies[i] != 1)
598                         {
599                                 error_count++;
600                         }
601                 }
602
603                 if (error_count > 0 )
604                         LOG_ERROR("%u words out of %u not transferred",
605                                 error_count, readiesNum);
606
607         }
608
609         free(Readies);
610
611         return retval;
612 }
613
614
615 /** Execute an instruction via ITR while handing data into the core via DTR.
616  *
617  *  The executed instruction \em must read data from DTR.
618  *
619  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
620  *
621  * \param arm11         Target state variable.
622  * \param opcode        ARM opcode
623  * \param data          Data word to be passed to the core via DTR
624  *
625  */
626 int arm11_run_instr_data_to_core1(struct arm11_common * arm11, uint32_t opcode, uint32_t data)
627 {
628         return arm11_run_instr_data_to_core(arm11, opcode, &data, 1);
629 }
630
631
632 /** Execute one instruction via ITR repeatedly while
633  *  reading data from the core via DTR on each execution.
634  *
635  *  The executed instruction \em must write data to DTR.
636  *
637  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
638  *
639  * \param arm11         Target state variable.
640  * \param opcode        ARM opcode
641  * \param data          Pointer to an array that receives the data words from the core
642  * \param count         Number of data words and instruction repetitions
643  *
644  */
645 int arm11_run_instr_data_from_core(struct arm11_common * arm11, uint32_t opcode, uint32_t * data, size_t count)
646 {
647         arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
648
649         arm11_add_debug_INST(arm11, opcode, NULL, TAP_IDLE);
650
651         arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
652
653         struct scan_field       chain5_fields[3];
654
655         uint32_t                        Data;
656         uint8_t                 Ready;
657         uint8_t                 nRetry;
658
659         arm11_setup_field(arm11, 32,    NULL,   &Data,      chain5_fields + 0);
660         arm11_setup_field(arm11,  1,    NULL,   &Ready,     chain5_fields + 1);
661         arm11_setup_field(arm11,  1,    NULL,   &nRetry,    chain5_fields + 2);
662
663         while (count--)
664         {
665                 int i = 0;
666                 do
667                 {
668                         arm11_add_dr_scan_vc(ARRAY_SIZE(chain5_fields), chain5_fields, count ? TAP_IDLE : TAP_DRPAUSE);
669
670                         CHECK_RETVAL(jtag_execute_queue());
671
672                         JTAG_DEBUG("DTR  Data %08x  Ready %d  nRetry %d",
673                                         (unsigned) Data, Ready, nRetry);
674
675                         long long then = 0;
676
677                         if (i == 1000)
678                         {
679                                 then = timeval_ms();
680                         }
681                         if (i >= 1000)
682                         {
683                                 if ((timeval_ms()-then) > 1000)
684                                 {
685                                         LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
686                                         return ERROR_FAIL;
687                                 }
688                         }
689
690                         i++;
691                 }
692                 while (!Ready);
693
694                 *data++ = Data;
695         }
696
697         return ERROR_OK;
698 }
699
700 /** Execute one instruction via ITR
701  *  then load r0 into DTR and read DTR from core.
702  *
703  *  The first executed instruction (\p opcode) should write data to r0.
704  *
705  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
706  *
707  * \param arm11         Target state variable.
708  * \param opcode        ARM opcode to write r0 with the value of interest
709  * \param data          Pointer to a data word that receives the value from r0 after \p opcode was executed.
710  *
711  */
712 int arm11_run_instr_data_from_core_via_r0(struct arm11_common * arm11, uint32_t opcode, uint32_t * data)
713 {
714         int retval;
715         retval = arm11_run_instr_no_data1(arm11, opcode);
716         if (retval != ERROR_OK)
717                 return retval;
718
719         /* MCR p14,0,R0,c0,c5,0 (move r0 -> wDTR -> local var) */
720         arm11_run_instr_data_from_core(arm11, 0xEE000E15, data, 1);
721
722         return ERROR_OK;
723 }
724
725 /** Load data into core via DTR then move it to r0 then
726  *  execute one instruction via ITR
727  *
728  *  The final executed instruction (\p opcode) should read data from r0.
729  *
730  * \pre arm11_run_instr_data_prepare() /  arm11_run_instr_data_finish() block
731  *
732  * \param arm11         Target state variable.
733  * \param opcode        ARM opcode to read r0 act upon it
734  * \param data          Data word that will be written to r0 before \p opcode is executed
735  *
736  */
737 int arm11_run_instr_data_to_core_via_r0(struct arm11_common * arm11, uint32_t opcode, uint32_t data)
738 {
739         int retval;
740         /* MRC p14,0,r0,c0,c5,0 */
741         retval = arm11_run_instr_data_to_core1(arm11, 0xEE100E15, data);
742         if (retval != ERROR_OK)
743                 return retval;
744
745         retval = arm11_run_instr_no_data1(arm11, opcode);
746         if (retval != ERROR_OK)
747                 return retval;
748
749         return ERROR_OK;
750 }
751
752 /** Apply reads and writes to scan chain 7
753  *
754  * \see struct arm11_sc7_action
755  *
756  * \param arm11         Target state variable.
757  * \param actions       A list of read and/or write instructions
758  * \param count         Number of instructions in the list.
759  *
760  */
761 int arm11_sc7_run(struct arm11_common * arm11, struct arm11_sc7_action * actions, size_t count)
762 {
763         int retval;
764
765         retval = arm11_add_debug_SCAN_N(arm11, 0x07, ARM11_TAP_DEFAULT);
766         if (retval != ERROR_OK)
767                 return retval;
768
769         arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
770
771         struct scan_field       chain7_fields[3];
772
773         uint8_t                         nRW;
774         uint32_t                                DataOut;
775         uint8_t                         AddressOut;
776         uint8_t                         Ready;
777         uint32_t                                DataIn;
778         uint8_t                         AddressIn;
779
780         arm11_setup_field(arm11,  1, &nRW,                      &Ready,         chain7_fields + 0);
781         arm11_setup_field(arm11, 32, &DataOut,          &DataIn,        chain7_fields + 1);
782         arm11_setup_field(arm11,  7, &AddressOut,       &AddressIn,     chain7_fields + 2);
783
784         for (size_t i = 0; i < count + 1; i++)
785         {
786                 if (i < count)
787                 {
788                         nRW                     = actions[i].write ? 1 : 0;
789                         DataOut         = actions[i].value;
790                         AddressOut      = actions[i].address;
791                 }
792                 else
793                 {
794                         nRW                     = 0;
795                         DataOut         = 0;
796                         AddressOut      = 0;
797                 }
798
799                 do
800                 {
801                         JTAG_DEBUG("SC7 <= Address %02x  Data %08x    nRW %d",
802                                         (unsigned) AddressOut,
803                                         (unsigned) DataOut,
804                                         nRW);
805
806                         arm11_add_dr_scan_vc(ARRAY_SIZE(chain7_fields),
807                                         chain7_fields, TAP_DRPAUSE);
808
809                         CHECK_RETVAL(jtag_execute_queue());
810
811                         JTAG_DEBUG("SC7 => Address %02x  Data %08x  Ready %d",
812                                         (unsigned) AddressIn,
813                                         (unsigned) DataIn,
814                                         Ready);
815                 }
816                 while (!Ready); /* 'nRW' is 'Ready' on read out */
817
818                 if (i > 0)
819                 {
820                         if (actions[i - 1].address != AddressIn)
821                         {
822                                 LOG_WARNING("Scan chain 7 shifted out unexpected address");
823                         }
824
825                         if (!actions[i - 1].write)
826                         {
827                                 actions[i - 1].value = DataIn;
828                         }
829                         else
830                         {
831                                 if (actions[i - 1].value != DataIn)
832                                 {
833                                         LOG_WARNING("Scan chain 7 shifted out unexpected data");
834                                 }
835                         }
836                 }
837         }
838
839         for (size_t i = 0; i < count; i++)
840         {
841                 JTAG_DEBUG("SC7 %02d: %02x %s %08x",
842                         (unsigned) i, actions[i].address,
843                         actions[i].write ? "<=" : "=>",
844                         (unsigned) actions[i].value);
845         }
846
847         return ERROR_OK;
848 }
849
850 /** Clear VCR and all breakpoints and watchpoints via scan chain 7
851  *
852  * \param arm11         Target state variable.
853  *
854  */
855 void arm11_sc7_clear_vbw(struct arm11_common * arm11)
856 {
857         size_t clear_bw_size = arm11->brp + arm11->wrp + 1;
858         struct arm11_sc7_action         *clear_bw = malloc(sizeof(struct arm11_sc7_action) * clear_bw_size);
859         struct arm11_sc7_action *       pos = clear_bw;
860
861         for (size_t i = 0; i < clear_bw_size; i++)
862         {
863                 clear_bw[i].write       = true;
864                 clear_bw[i].value       = 0;
865         }
866
867         for (size_t i = 0; i < arm11->brp; i++)
868                 (pos++)->address = ARM11_SC7_BCR0 + i;
869
870
871         for (size_t i = 0; i < arm11->wrp; i++)
872                 (pos++)->address = ARM11_SC7_WCR0 + i;
873
874
875         (pos++)->address = ARM11_SC7_VCR;
876
877         arm11_sc7_run(arm11, clear_bw, clear_bw_size);
878
879         free (clear_bw);
880 }
881
882 /** Write VCR register
883  *
884  * \param arm11         Target state variable.
885  * \param value         Value to be written
886  */
887 void arm11_sc7_set_vcr(struct arm11_common * arm11, uint32_t value)
888 {
889         struct arm11_sc7_action         set_vcr;
890
891         set_vcr.write           = true;
892         set_vcr.address         = ARM11_SC7_VCR;
893         set_vcr.value           = value;
894
895
896         arm11_sc7_run(arm11, &set_vcr, 1);
897 }
898
899
900
901 /** Read word from address
902  *
903  * \param arm11         Target state variable.
904  * \param address       Memory address to be read
905  * \param result        Pointer where to store result
906  *
907  */
908 int arm11_read_memory_word(struct arm11_common * arm11, uint32_t address, uint32_t * result)
909 {
910         int retval;
911         retval = arm11_run_instr_data_prepare(arm11);
912         if (retval != ERROR_OK)
913                 return retval;
914
915         /* MRC p14,0,r0,c0,c5,0 (r0 = address) */
916         CHECK_RETVAL(arm11_run_instr_data_to_core1(arm11, 0xee100e15, address));
917
918         /* LDC p14,c5,[R0],#4 (DTR = [r0]) */
919         CHECK_RETVAL(arm11_run_instr_data_from_core(arm11, 0xecb05e01, result, 1));
920
921         return arm11_run_instr_data_finish(arm11);
922 }
923
924
925 /************************************************************************/
926
927 /*
928  * ARM11 provider for the OpenOCD implementation of the standard
929  * architectural ARM v6/v7 "Debug Programmer's Model" (DPM).
930  */
931
932 static inline struct arm11_common *dpm_to_arm11(struct arm_dpm *dpm)
933 {
934         return container_of(dpm, struct arm11_common, dpm);
935 }
936
937 static int arm11_dpm_prepare(struct arm_dpm *dpm)
938 {
939         struct arm11_common *arm11 = dpm_to_arm11(dpm);
940
941         arm11 = container_of(dpm->arm, struct arm11_common, arm);
942
943         return arm11_run_instr_data_prepare(dpm_to_arm11(dpm));
944 }
945
946 static int arm11_dpm_finish(struct arm_dpm *dpm)
947 {
948         return arm11_run_instr_data_finish(dpm_to_arm11(dpm));
949 }
950
951 static int arm11_dpm_instr_write_data_dcc(struct arm_dpm *dpm,
952                 uint32_t opcode, uint32_t data)
953 {
954         return arm11_run_instr_data_to_core(dpm_to_arm11(dpm),
955                         opcode, &data, 1);
956 }
957
958 static int arm11_dpm_instr_write_data_r0(struct arm_dpm *dpm,
959                 uint32_t opcode, uint32_t data)
960 {
961         return arm11_run_instr_data_to_core_via_r0(dpm_to_arm11(dpm),
962                         opcode, data);
963 }
964
965 static int arm11_dpm_instr_read_data_dcc(struct arm_dpm *dpm,
966                 uint32_t opcode, uint32_t *data)
967 {
968         return arm11_run_instr_data_from_core(dpm_to_arm11(dpm),
969                         opcode, data, 1);
970 }
971
972 static int arm11_dpm_instr_read_data_r0(struct arm_dpm *dpm,
973                 uint32_t opcode, uint32_t *data)
974 {
975         return arm11_run_instr_data_from_core_via_r0(dpm_to_arm11(dpm),
976                         opcode, data);
977 }
978
979 /** Set up high-level debug module utilities */
980 int arm11_dpm_init(struct arm11_common *arm11, uint32_t didr)
981 {
982         struct arm_dpm *dpm = &arm11->dpm;
983         int retval;
984
985         dpm->arm = &arm11->arm;
986
987         dpm->didr = didr;
988
989         dpm->prepare = arm11_dpm_prepare;
990         dpm->finish = arm11_dpm_finish;
991
992         dpm->instr_write_data_dcc = arm11_dpm_instr_write_data_dcc;
993         dpm->instr_write_data_r0 = arm11_dpm_instr_write_data_r0;
994
995         dpm->instr_read_data_dcc = arm11_dpm_instr_read_data_dcc;
996         dpm->instr_read_data_r0 = arm11_dpm_instr_read_data_r0;
997
998         retval = arm_dpm_setup(dpm);
999         if (retval != ERROR_OK)
1000                 return retval;
1001
1002         retval = arm_dpm_initialize(dpm);
1003
1004         return retval;
1005 }