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