adi_v5_jtag: implement DAP WAIT support
[fw/openocd] / src / target / arm_adi_v5.c
1 /***************************************************************************
2  *   Copyright (C) 2006 by Magnus Lundin                                   *
3  *   lundin@mlu.mine.nu                                                    *
4  *                                                                         *
5  *   Copyright (C) 2008 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
8  *   Copyright (C) 2009-2010 by Oyvind Harboe                              *
9  *   oyvind.harboe@zylin.com                                               *
10  *                                                                         *
11  *   Copyright (C) 2009-2010 by David Brownell                             *
12  *                                                                         *
13  *   Copyright (C) 2013 by Andreas Fritiofson                              *
14  *   andreas.fritiofson@gmail.com                                          *
15  *                                                                         *
16  *   This program is free software; you can redistribute it and/or modify  *
17  *   it under the terms of the GNU General Public License as published by  *
18  *   the Free Software Foundation; either version 2 of the License, or     *
19  *   (at your option) any later version.                                   *
20  *                                                                         *
21  *   This program is distributed in the hope that it will be useful,       *
22  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
23  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
24  *   GNU General Public License for more details.                          *
25  *                                                                         *
26  *   You should have received a copy of the GNU General Public License     *
27  *   along with this program; if not, write to the                         *
28  *   Free Software Foundation, Inc.,                                       *
29  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
30  ***************************************************************************/
31
32 /**
33  * @file
34  * This file implements support for the ARM Debug Interface version 5 (ADIv5)
35  * debugging architecture.  Compared with previous versions, this includes
36  * a low pin-count Serial Wire Debug (SWD) alternative to JTAG for message
37  * transport, and focusses on memory mapped resources as defined by the
38  * CoreSight architecture.
39  *
40  * A key concept in ADIv5 is the Debug Access Port, or DAP.  A DAP has two
41  * basic components:  a Debug Port (DP) transporting messages to and from a
42  * debugger, and an Access Port (AP) accessing resources.  Three types of DP
43  * are defined.  One uses only JTAG for communication, and is called JTAG-DP.
44  * One uses only SWD for communication, and is called SW-DP.  The third can
45  * use either SWD or JTAG, and is called SWJ-DP.  The most common type of AP
46  * is used to access memory mapped resources and is called a MEM-AP.  Also a
47  * JTAG-AP is also defined, bridging to JTAG resources; those are uncommon.
48  *
49  * This programming interface allows DAP pipelined operations through a
50  * transaction queue.  This primarily affects AP operations (such as using
51  * a MEM-AP to access memory or registers).  If the current transaction has
52  * not finished by the time the next one must begin, and the ORUNDETECT bit
53  * is set in the DP_CTRL_STAT register, the SSTICKYORUN status is set and
54  * further AP operations will fail.  There are two basic methods to avoid
55  * such overrun errors.  One involves polling for status instead of using
56  * transaction piplining.  The other involves adding delays to ensure the
57  * AP has enough time to complete one operation before starting the next
58  * one.  (For JTAG these delays are controlled by memaccess_tck.)
59  */
60
61 /*
62  * Relevant specifications from ARM include:
63  *
64  * ARM(tm) Debug Interface v5 Architecture Specification    ARM IHI 0031A
65  * CoreSight(tm) v1.0 Architecture Specification            ARM IHI 0029B
66  *
67  * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316D
68  * Cortex-M3(tm) TRM, ARM DDI 0337G
69  */
70
71 #ifdef HAVE_CONFIG_H
72 #include "config.h"
73 #endif
74
75 #include "jtag/interface.h"
76 #include "arm.h"
77 #include "arm_adi_v5.h"
78 #include <helper/time_support.h>
79 #include <helper/list.h>
80
81 /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement  */
82
83 /*
84         uint32_t tar_block_size(uint32_t address)
85         Return the largest block starting at address that does not cross a tar block size alignment boundary
86 */
87 static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
88 {
89         return tar_autoincr_block - ((tar_autoincr_block - 1) & address);
90 }
91
92 /***************************************************************************
93  *                                                                         *
94  * DP and MEM-AP  register access  through APACC and DPACC                 *
95  *                                                                         *
96 ***************************************************************************/
97
98 static int mem_ap_setup_csw(struct adiv5_ap *ap, uint32_t csw)
99 {
100         csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT |
101                 ap->csw_default;
102
103         if (csw != ap->csw_value) {
104                 /* LOG_DEBUG("DAP: Set CSW %x",csw); */
105                 int retval = dap_queue_ap_write(ap, MEM_AP_REG_CSW, csw);
106                 if (retval != ERROR_OK)
107                         return retval;
108                 ap->csw_value = csw;
109         }
110         return ERROR_OK;
111 }
112
113 static int mem_ap_setup_tar(struct adiv5_ap *ap, uint32_t tar)
114 {
115         if (tar != ap->tar_value ||
116                         (ap->csw_value & CSW_ADDRINC_MASK)) {
117                 /* LOG_DEBUG("DAP: Set TAR %x",tar); */
118                 int retval = dap_queue_ap_write(ap, MEM_AP_REG_TAR, tar);
119                 if (retval != ERROR_OK)
120                         return retval;
121                 ap->tar_value = tar;
122         }
123         return ERROR_OK;
124 }
125
126 /**
127  * Queue transactions setting up transfer parameters for the
128  * currently selected MEM-AP.
129  *
130  * Subsequent transfers using registers like MEM_AP_REG_DRW or MEM_AP_REG_BD2
131  * initiate data reads or writes using memory or peripheral addresses.
132  * If the CSW is configured for it, the TAR may be automatically
133  * incremented after each transfer.
134  *
135  * @param ap The MEM-AP.
136  * @param csw MEM-AP Control/Status Word (CSW) register to assign.  If this
137  *      matches the cached value, the register is not changed.
138  * @param tar MEM-AP Transfer Address Register (TAR) to assign.  If this
139  *      matches the cached address, the register is not changed.
140  *
141  * @return ERROR_OK if the transaction was properly queued, else a fault code.
142  */
143 static int mem_ap_setup_transfer(struct adiv5_ap *ap, uint32_t csw, uint32_t tar)
144 {
145         int retval;
146         retval = mem_ap_setup_csw(ap, csw);
147         if (retval != ERROR_OK)
148                 return retval;
149         retval = mem_ap_setup_tar(ap, tar);
150         if (retval != ERROR_OK)
151                 return retval;
152         return ERROR_OK;
153 }
154
155 /**
156  * Asynchronous (queued) read of a word from memory or a system register.
157  *
158  * @param ap The MEM-AP to access.
159  * @param address Address of the 32-bit word to read; it must be
160  *      readable by the currently selected MEM-AP.
161  * @param value points to where the word will be stored when the
162  *      transaction queue is flushed (assuming no errors).
163  *
164  * @return ERROR_OK for success.  Otherwise a fault code.
165  */
166 int mem_ap_read_u32(struct adiv5_ap *ap, uint32_t address,
167                 uint32_t *value)
168 {
169         int retval;
170
171         /* Use banked addressing (REG_BDx) to avoid some link traffic
172          * (updating TAR) when reading several consecutive addresses.
173          */
174         retval = mem_ap_setup_transfer(ap, CSW_32BIT | CSW_ADDRINC_OFF,
175                         address & 0xFFFFFFF0);
176         if (retval != ERROR_OK)
177                 return retval;
178
179         return dap_queue_ap_read(ap, MEM_AP_REG_BD0 | (address & 0xC), value);
180 }
181
182 /**
183  * Synchronous read of a word from memory or a system register.
184  * As a side effect, this flushes any queued transactions.
185  *
186  * @param ap The MEM-AP to access.
187  * @param address Address of the 32-bit word to read; it must be
188  *      readable by the currently selected MEM-AP.
189  * @param value points to where the result will be stored.
190  *
191  * @return ERROR_OK for success; *value holds the result.
192  * Otherwise a fault code.
193  */
194 int mem_ap_read_atomic_u32(struct adiv5_ap *ap, uint32_t address,
195                 uint32_t *value)
196 {
197         int retval;
198
199         retval = mem_ap_read_u32(ap, address, value);
200         if (retval != ERROR_OK)
201                 return retval;
202
203         return dap_run(ap->dap);
204 }
205
206 /**
207  * Asynchronous (queued) write of a word to memory or a system register.
208  *
209  * @param ap The MEM-AP to access.
210  * @param address Address to be written; it must be writable by
211  *      the currently selected MEM-AP.
212  * @param value Word that will be written to the address when transaction
213  *      queue is flushed (assuming no errors).
214  *
215  * @return ERROR_OK for success.  Otherwise a fault code.
216  */
217 int mem_ap_write_u32(struct adiv5_ap *ap, uint32_t address,
218                 uint32_t value)
219 {
220         int retval;
221
222         /* Use banked addressing (REG_BDx) to avoid some link traffic
223          * (updating TAR) when writing several consecutive addresses.
224          */
225         retval = mem_ap_setup_transfer(ap, CSW_32BIT | CSW_ADDRINC_OFF,
226                         address & 0xFFFFFFF0);
227         if (retval != ERROR_OK)
228                 return retval;
229
230         return dap_queue_ap_write(ap, MEM_AP_REG_BD0 | (address & 0xC),
231                         value);
232 }
233
234 /**
235  * Synchronous write of a word to memory or a system register.
236  * As a side effect, this flushes any queued transactions.
237  *
238  * @param ap The MEM-AP to access.
239  * @param address Address to be written; it must be writable by
240  *      the currently selected MEM-AP.
241  * @param value Word that will be written.
242  *
243  * @return ERROR_OK for success; the data was written.  Otherwise a fault code.
244  */
245 int mem_ap_write_atomic_u32(struct adiv5_ap *ap, uint32_t address,
246                 uint32_t value)
247 {
248         int retval = mem_ap_write_u32(ap, address, value);
249
250         if (retval != ERROR_OK)
251                 return retval;
252
253         return dap_run(ap->dap);
254 }
255
256 /**
257  * Synchronous write of a block of memory, using a specific access size.
258  *
259  * @param ap The MEM-AP to access.
260  * @param buffer The data buffer to write. No particular alignment is assumed.
261  * @param size Which access size to use, in bytes. 1, 2 or 4.
262  * @param count The number of writes to do (in size units, not bytes).
263  * @param address Address to be written; it must be writable by the currently selected MEM-AP.
264  * @param addrinc Whether the target address should be increased for each write or not. This
265  *  should normally be true, except when writing to e.g. a FIFO.
266  * @return ERROR_OK on success, otherwise an error code.
267  */
268 static int mem_ap_write(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count,
269                 uint32_t address, bool addrinc)
270 {
271         struct adiv5_dap *dap = ap->dap;
272         size_t nbytes = size * count;
273         const uint32_t csw_addrincr = addrinc ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
274         uint32_t csw_size;
275         uint32_t addr_xor;
276         int retval;
277
278         /* TI BE-32 Quirks mode:
279          * Writes on big-endian TMS570 behave very strangely. Observed behavior:
280          *   size   write address   bytes written in order
281          *   4      TAR ^ 0         (val >> 24), (val >> 16), (val >> 8), (val)
282          *   2      TAR ^ 2         (val >> 8), (val)
283          *   1      TAR ^ 3         (val)
284          * For example, if you attempt to write a single byte to address 0, the processor
285          * will actually write a byte to address 3.
286          *
287          * To make writes of size < 4 work as expected, we xor a value with the address before
288          * setting the TAP, and we set the TAP after every transfer rather then relying on
289          * address increment. */
290
291         if (size == 4) {
292                 csw_size = CSW_32BIT;
293                 addr_xor = 0;
294         } else if (size == 2) {
295                 csw_size = CSW_16BIT;
296                 addr_xor = dap->ti_be_32_quirks ? 2 : 0;
297         } else if (size == 1) {
298                 csw_size = CSW_8BIT;
299                 addr_xor = dap->ti_be_32_quirks ? 3 : 0;
300         } else {
301                 return ERROR_TARGET_UNALIGNED_ACCESS;
302         }
303
304         if (ap->unaligned_access_bad && (address % size != 0))
305                 return ERROR_TARGET_UNALIGNED_ACCESS;
306
307         retval = mem_ap_setup_tar(ap, address ^ addr_xor);
308         if (retval != ERROR_OK)
309                 return retval;
310
311         while (nbytes > 0) {
312                 uint32_t this_size = size;
313
314                 /* Select packed transfer if possible */
315                 if (addrinc && ap->packed_transfers && nbytes >= 4
316                                 && max_tar_block_size(ap->tar_autoincr_block, address) >= 4) {
317                         this_size = 4;
318                         retval = mem_ap_setup_csw(ap, csw_size | CSW_ADDRINC_PACKED);
319                 } else {
320                         retval = mem_ap_setup_csw(ap, csw_size | csw_addrincr);
321                 }
322
323                 if (retval != ERROR_OK)
324                         break;
325
326                 /* How many source bytes each transfer will consume, and their location in the DRW,
327                  * depends on the type of transfer and alignment. See ARM document IHI0031C. */
328                 uint32_t outvalue = 0;
329                 if (dap->ti_be_32_quirks) {
330                         switch (this_size) {
331                         case 4:
332                                 outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
333                                 outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
334                                 outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
335                                 outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
336                                 break;
337                         case 2:
338                                 outvalue |= (uint32_t)*buffer++ << 8 * (1 ^ (address++ & 3) ^ addr_xor);
339                                 outvalue |= (uint32_t)*buffer++ << 8 * (1 ^ (address++ & 3) ^ addr_xor);
340                                 break;
341                         case 1:
342                                 outvalue |= (uint32_t)*buffer++ << 8 * (0 ^ (address++ & 3) ^ addr_xor);
343                                 break;
344                         }
345                 } else {
346                         switch (this_size) {
347                         case 4:
348                                 outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
349                                 outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
350                         case 2:
351                                 outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
352                         case 1:
353                                 outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
354                         }
355                 }
356
357                 nbytes -= this_size;
358
359                 retval = dap_queue_ap_write(ap, MEM_AP_REG_DRW, outvalue);
360                 if (retval != ERROR_OK)
361                         break;
362
363                 /* Rewrite TAR if it wrapped or we're xoring addresses */
364                 if (addrinc && (addr_xor || (address % ap->tar_autoincr_block < size && nbytes > 0))) {
365                         retval = mem_ap_setup_tar(ap, address ^ addr_xor);
366                         if (retval != ERROR_OK)
367                                 break;
368                 }
369         }
370
371         /* REVISIT: Might want to have a queued version of this function that does not run. */
372         if (retval == ERROR_OK)
373                 retval = dap_run(dap);
374
375         if (retval != ERROR_OK) {
376                 uint32_t tar;
377                 if (dap_queue_ap_read(ap, MEM_AP_REG_TAR, &tar) == ERROR_OK
378                                 && dap_run(dap) == ERROR_OK)
379                         LOG_ERROR("Failed to write memory at 0x%08"PRIx32, tar);
380                 else
381                         LOG_ERROR("Failed to write memory and, additionally, failed to find out where");
382         }
383
384         return retval;
385 }
386
387 /**
388  * Synchronous read of a block of memory, using a specific access size.
389  *
390  * @param ap The MEM-AP to access.
391  * @param buffer The data buffer to receive the data. No particular alignment is assumed.
392  * @param size Which access size to use, in bytes. 1, 2 or 4.
393  * @param count The number of reads to do (in size units, not bytes).
394  * @param address Address to be read; it must be readable by the currently selected MEM-AP.
395  * @param addrinc Whether the target address should be increased after each read or not. This
396  *  should normally be true, except when reading from e.g. a FIFO.
397  * @return ERROR_OK on success, otherwise an error code.
398  */
399 static int mem_ap_read(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count,
400                 uint32_t adr, bool addrinc)
401 {
402         struct adiv5_dap *dap = ap->dap;
403         size_t nbytes = size * count;
404         const uint32_t csw_addrincr = addrinc ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
405         uint32_t csw_size;
406         uint32_t address = adr;
407         int retval;
408
409         /* TI BE-32 Quirks mode:
410          * Reads on big-endian TMS570 behave strangely differently than writes.
411          * They read from the physical address requested, but with DRW byte-reversed.
412          * For example, a byte read from address 0 will place the result in the high bytes of DRW.
413          * Also, packed 8-bit and 16-bit transfers seem to sometimes return garbage in some bytes,
414          * so avoid them. */
415
416         if (size == 4)
417                 csw_size = CSW_32BIT;
418         else if (size == 2)
419                 csw_size = CSW_16BIT;
420         else if (size == 1)
421                 csw_size = CSW_8BIT;
422         else
423                 return ERROR_TARGET_UNALIGNED_ACCESS;
424
425         if (ap->unaligned_access_bad && (adr % size != 0))
426                 return ERROR_TARGET_UNALIGNED_ACCESS;
427
428         /* Allocate buffer to hold the sequence of DRW reads that will be made. This is a significant
429          * over-allocation if packed transfers are going to be used, but determining the real need at
430          * this point would be messy. */
431         uint32_t *read_buf = malloc(count * sizeof(uint32_t));
432         uint32_t *read_ptr = read_buf;
433         if (read_buf == NULL) {
434                 LOG_ERROR("Failed to allocate read buffer");
435                 return ERROR_FAIL;
436         }
437
438         retval = mem_ap_setup_tar(ap, address);
439         if (retval != ERROR_OK) {
440                 free(read_buf);
441                 return retval;
442         }
443
444         /* Queue up all reads. Each read will store the entire DRW word in the read buffer. How many
445          * useful bytes it contains, and their location in the word, depends on the type of transfer
446          * and alignment. */
447         while (nbytes > 0) {
448                 uint32_t this_size = size;
449
450                 /* Select packed transfer if possible */
451                 if (addrinc && ap->packed_transfers && nbytes >= 4
452                                 && max_tar_block_size(ap->tar_autoincr_block, address) >= 4) {
453                         this_size = 4;
454                         retval = mem_ap_setup_csw(ap, csw_size | CSW_ADDRINC_PACKED);
455                 } else {
456                         retval = mem_ap_setup_csw(ap, csw_size | csw_addrincr);
457                 }
458                 if (retval != ERROR_OK)
459                         break;
460
461                 retval = dap_queue_ap_read(ap, MEM_AP_REG_DRW, read_ptr++);
462                 if (retval != ERROR_OK)
463                         break;
464
465                 nbytes -= this_size;
466                 address += this_size;
467
468                 /* Rewrite TAR if it wrapped */
469                 if (addrinc && address % ap->tar_autoincr_block < size && nbytes > 0) {
470                         retval = mem_ap_setup_tar(ap, address);
471                         if (retval != ERROR_OK)
472                                 break;
473                 }
474         }
475
476         if (retval == ERROR_OK)
477                 retval = dap_run(dap);
478
479         /* Restore state */
480         address = adr;
481         nbytes = size * count;
482         read_ptr = read_buf;
483
484         /* If something failed, read TAR to find out how much data was successfully read, so we can
485          * at least give the caller what we have. */
486         if (retval != ERROR_OK) {
487                 uint32_t tar;
488                 if (dap_queue_ap_read(ap, MEM_AP_REG_TAR, &tar) == ERROR_OK
489                                 && dap_run(dap) == ERROR_OK) {
490                         LOG_ERROR("Failed to read memory at 0x%08"PRIx32, tar);
491                         if (nbytes > tar - address)
492                                 nbytes = tar - address;
493                 } else {
494                         LOG_ERROR("Failed to read memory and, additionally, failed to find out where");
495                         nbytes = 0;
496                 }
497         }
498
499         /* Replay loop to populate caller's buffer from the correct word and byte lane */
500         while (nbytes > 0) {
501                 uint32_t this_size = size;
502
503                 if (addrinc && ap->packed_transfers && nbytes >= 4
504                                 && max_tar_block_size(ap->tar_autoincr_block, address) >= 4) {
505                         this_size = 4;
506                 }
507
508                 if (dap->ti_be_32_quirks) {
509                         switch (this_size) {
510                         case 4:
511                                 *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
512                                 *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
513                         case 2:
514                                 *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
515                         case 1:
516                                 *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
517                         }
518                 } else {
519                         switch (this_size) {
520                         case 4:
521                                 *buffer++ = *read_ptr >> 8 * (address++ & 3);
522                                 *buffer++ = *read_ptr >> 8 * (address++ & 3);
523                         case 2:
524                                 *buffer++ = *read_ptr >> 8 * (address++ & 3);
525                         case 1:
526                                 *buffer++ = *read_ptr >> 8 * (address++ & 3);
527                         }
528                 }
529
530                 read_ptr++;
531                 nbytes -= this_size;
532         }
533
534         free(read_buf);
535         return retval;
536 }
537
538 int mem_ap_read_buf(struct adiv5_ap *ap,
539                 uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
540 {
541         return mem_ap_read(ap, buffer, size, count, address, true);
542 }
543
544 int mem_ap_write_buf(struct adiv5_ap *ap,
545                 const uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
546 {
547         return mem_ap_write(ap, buffer, size, count, address, true);
548 }
549
550 int mem_ap_read_buf_noincr(struct adiv5_ap *ap,
551                 uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
552 {
553         return mem_ap_read(ap, buffer, size, count, address, false);
554 }
555
556 int mem_ap_write_buf_noincr(struct adiv5_ap *ap,
557                 const uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
558 {
559         return mem_ap_write(ap, buffer, size, count, address, false);
560 }
561
562 /*--------------------------------------------------------------------------*/
563
564
565 #define DAP_POWER_DOMAIN_TIMEOUT (10)
566
567 /* FIXME don't import ... just initialize as
568  * part of DAP transport setup
569 */
570 extern const struct dap_ops jtag_dp_ops;
571
572 /*--------------------------------------------------------------------------*/
573
574 /**
575  * Create a new DAP
576  */
577 struct adiv5_dap *dap_init(void)
578 {
579         struct adiv5_dap *dap = calloc(1, sizeof(struct adiv5_dap));
580         int i;
581         /* Set up with safe defaults */
582         for (i = 0; i <= 255; i++) {
583                 dap->ap[i].dap = dap;
584                 dap->ap[i].ap_num = i;
585                 /* memaccess_tck max is 255 */
586                 dap->ap[i].memaccess_tck = 255;
587                 /* Number of bits for tar autoincrement, impl. dep. at least 10 */
588                 dap->ap[i].tar_autoincr_block = (1<<10);
589         }
590         INIT_LIST_HEAD(&dap->cmd_journal);
591         return dap;
592 }
593
594 /**
595  * Initialize a DAP.  This sets up the power domains, prepares the DP
596  * for further use and activates overrun checking.
597  *
598  * @param dap The DAP being initialized.
599  */
600 int dap_dp_init(struct adiv5_dap *dap)
601 {
602         int retval;
603
604         LOG_DEBUG(" ");
605         /* JTAG-DP or SWJ-DP, in JTAG mode
606          * ... for SWD mode this is patched as part
607          * of link switchover
608          * FIXME: This should already be setup by the respective transport specific DAP creation.
609          */
610         if (!dap->ops)
611                 dap->ops = &jtag_dp_ops;
612
613         dap->select = DP_SELECT_INVALID;
614         dap->last_read = NULL;
615
616         for (size_t i = 0; i < 10; i++) {
617                 /* DP initialization */
618
619                 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
620                 if (retval != ERROR_OK)
621                         continue;
622
623                 retval = dap_queue_dp_write(dap, DP_CTRL_STAT, SSTICKYERR);
624                 if (retval != ERROR_OK)
625                         continue;
626
627                 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
628                 if (retval != ERROR_OK)
629                         continue;
630
631                 dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
632                 retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
633                 if (retval != ERROR_OK)
634                         continue;
635
636                 /* Check that we have debug power domains activated */
637                 LOG_DEBUG("DAP: wait CDBGPWRUPACK");
638                 retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
639                                               CDBGPWRUPACK, CDBGPWRUPACK,
640                                               DAP_POWER_DOMAIN_TIMEOUT);
641                 if (retval != ERROR_OK)
642                         continue;
643
644                 LOG_DEBUG("DAP: wait CSYSPWRUPACK");
645                 retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
646                                               CSYSPWRUPACK, CSYSPWRUPACK,
647                                               DAP_POWER_DOMAIN_TIMEOUT);
648                 if (retval != ERROR_OK)
649                         continue;
650
651                 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
652                 if (retval != ERROR_OK)
653                         continue;
654
655                 /* With debug power on we can activate OVERRUN checking */
656                 dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
657                 retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
658                 if (retval != ERROR_OK)
659                         continue;
660                 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
661                 if (retval != ERROR_OK)
662                         continue;
663
664                 retval = dap_run(dap);
665                 if (retval != ERROR_OK)
666                         continue;
667
668                 break;
669         }
670
671         return retval;
672 }
673
674 /**
675  * Initialize a DAP.  This sets up the power domains, prepares the DP
676  * for further use, and arranges to use AP #0 for all AP operations
677  * until dap_ap-select() changes that policy.
678  *
679  * @param ap The MEM-AP being initialized.
680  */
681 int mem_ap_init(struct adiv5_ap *ap)
682 {
683         /* check that we support packed transfers */
684         uint32_t csw, cfg;
685         int retval;
686         struct adiv5_dap *dap = ap->dap;
687
688         retval = mem_ap_setup_transfer(ap, CSW_8BIT | CSW_ADDRINC_PACKED, 0);
689         if (retval != ERROR_OK)
690                 return retval;
691
692         retval = dap_queue_ap_read(ap, MEM_AP_REG_CSW, &csw);
693         if (retval != ERROR_OK)
694                 return retval;
695
696         retval = dap_queue_ap_read(ap, MEM_AP_REG_CFG, &cfg);
697         if (retval != ERROR_OK)
698                 return retval;
699
700         retval = dap_run(dap);
701         if (retval != ERROR_OK)
702                 return retval;
703
704         if (csw & CSW_ADDRINC_PACKED)
705                 ap->packed_transfers = true;
706         else
707                 ap->packed_transfers = false;
708
709         /* Packed transfers on TI BE-32 processors do not work correctly in
710          * many cases. */
711         if (dap->ti_be_32_quirks)
712                 ap->packed_transfers = false;
713
714         LOG_DEBUG("MEM_AP Packed Transfers: %s",
715                         ap->packed_transfers ? "enabled" : "disabled");
716
717         /* The ARM ADI spec leaves implementation-defined whether unaligned
718          * memory accesses work, only work partially, or cause a sticky error.
719          * On TI BE-32 processors, reads seem to return garbage in some bytes
720          * and unaligned writes seem to cause a sticky error.
721          * TODO: it would be nice to have a way to detect whether unaligned
722          * operations are supported on other processors. */
723         ap->unaligned_access_bad = dap->ti_be_32_quirks;
724
725         LOG_DEBUG("MEM_AP CFG: large data %d, long address %d, big-endian %d",
726                         !!(cfg & 0x04), !!(cfg & 0x02), !!(cfg & 0x01));
727
728         return ERROR_OK;
729 }
730
731 /* CID interpretation -- see ARM IHI 0029B section 3
732  * and ARM IHI 0031A table 13-3.
733  */
734 static const char *class_description[16] = {
735         "Reserved", "ROM table", "Reserved", "Reserved",
736         "Reserved", "Reserved", "Reserved", "Reserved",
737         "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
738         "Reserved", "OptimoDE DESS",
739         "Generic IP component", "PrimeCell or System component"
740 };
741
742 static bool is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
743 {
744         return cid3 == 0xb1 && cid2 == 0x05
745                         && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
746 }
747
748 /*
749  * This function checks the ID for each access port to find the requested Access Port type
750  */
751 int dap_find_ap(struct adiv5_dap *dap, enum ap_type type_to_find, struct adiv5_ap **ap_out)
752 {
753         int ap_num;
754
755         /* Maximum AP number is 255 since the SELECT register is 8 bits */
756         for (ap_num = 0; ap_num <= 255; ap_num++) {
757
758                 /* read the IDR register of the Access Port */
759                 uint32_t id_val = 0;
760
761                 int retval = dap_queue_ap_read(dap_ap(dap, ap_num), AP_REG_IDR, &id_val);
762                 if (retval != ERROR_OK)
763                         return retval;
764
765                 retval = dap_run(dap);
766
767                 /* IDR bits:
768                  * 31-28 : Revision
769                  * 27-24 : JEDEC bank (0x4 for ARM)
770                  * 23-17 : JEDEC code (0x3B for ARM)
771                  * 16-13 : Class (0b1000=Mem-AP)
772                  * 12-8  : Reserved
773                  *  7-4  : AP Variant (non-zero for JTAG-AP)
774                  *  3-0  : AP Type (0=JTAG-AP 1=AHB-AP 2=APB-AP 4=AXI-AP)
775                  */
776
777                 /* Reading register for a non-existant AP should not cause an error,
778                  * but just to be sure, try to continue searching if an error does happen.
779                  */
780                 if ((retval == ERROR_OK) &&                  /* Register read success */
781                         ((id_val & IDR_JEP106) == IDR_JEP106_ARM) && /* Jedec codes match */
782                         ((id_val & IDR_TYPE) == type_to_find)) {      /* type matches*/
783
784                         LOG_DEBUG("Found %s at AP index: %d (IDR=0x%08" PRIX32 ")",
785                                                 (type_to_find == AP_TYPE_AHB_AP)  ? "AHB-AP"  :
786                                                 (type_to_find == AP_TYPE_APB_AP)  ? "APB-AP"  :
787                                                 (type_to_find == AP_TYPE_AXI_AP)  ? "AXI-AP"  :
788                                                 (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown",
789                                                 ap_num, id_val);
790
791                         *ap_out = &dap->ap[ap_num];
792                         return ERROR_OK;
793                 }
794         }
795
796         LOG_DEBUG("No %s found",
797                                 (type_to_find == AP_TYPE_AHB_AP)  ? "AHB-AP"  :
798                                 (type_to_find == AP_TYPE_APB_AP)  ? "APB-AP"  :
799                                 (type_to_find == AP_TYPE_AXI_AP)  ? "AXI-AP"  :
800                                 (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown");
801         return ERROR_FAIL;
802 }
803
804 int dap_get_debugbase(struct adiv5_ap *ap,
805                         uint32_t *dbgbase, uint32_t *apid)
806 {
807         struct adiv5_dap *dap = ap->dap;
808         int retval;
809
810         retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE, dbgbase);
811         if (retval != ERROR_OK)
812                 return retval;
813         retval = dap_queue_ap_read(ap, AP_REG_IDR, apid);
814         if (retval != ERROR_OK)
815                 return retval;
816         retval = dap_run(dap);
817         if (retval != ERROR_OK)
818                 return retval;
819
820         return ERROR_OK;
821 }
822
823 int dap_lookup_cs_component(struct adiv5_ap *ap,
824                         uint32_t dbgbase, uint8_t type, uint32_t *addr, int32_t *idx)
825 {
826         uint32_t romentry, entry_offset = 0, component_base, devtype;
827         int retval;
828
829         *addr = 0;
830
831         do {
832                 retval = mem_ap_read_atomic_u32(ap, (dbgbase&0xFFFFF000) |
833                                                 entry_offset, &romentry);
834                 if (retval != ERROR_OK)
835                         return retval;
836
837                 component_base = (dbgbase & 0xFFFFF000)
838                         + (romentry & 0xFFFFF000);
839
840                 if (romentry & 0x1) {
841                         uint32_t c_cid1;
842                         retval = mem_ap_read_atomic_u32(ap, component_base | 0xff4, &c_cid1);
843                         if (retval != ERROR_OK) {
844                                 LOG_ERROR("Can't read component with base address 0x%" PRIx32
845                                           ", the corresponding core might be turned off", component_base);
846                                 return retval;
847                         }
848                         if (((c_cid1 >> 4) & 0x0f) == 1) {
849                                 retval = dap_lookup_cs_component(ap, component_base,
850                                                         type, addr, idx);
851                                 if (retval == ERROR_OK)
852                                         break;
853                                 if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
854                                         return retval;
855                         }
856
857                         retval = mem_ap_read_atomic_u32(ap,
858                                         (component_base & 0xfffff000) | 0xfcc,
859                                         &devtype);
860                         if (retval != ERROR_OK)
861                                 return retval;
862                         if ((devtype & 0xff) == type) {
863                                 if (!*idx) {
864                                         *addr = component_base;
865                                         break;
866                                 } else
867                                         (*idx)--;
868                         }
869                 }
870                 entry_offset += 4;
871         } while (romentry > 0);
872
873         if (!*addr)
874                 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
875
876         return ERROR_OK;
877 }
878
879 /* The designer identity code is encoded as:
880  * bits 11:8 : JEP106 Bank (number of continuation codes), only valid when bit 7 is 1.
881  * bit 7     : Set when bits 6:0 represent a JEP106 ID and cleared when bits 6:0 represent
882  *             a legacy ASCII Identity Code.
883  * bits 6:0  : JEP106 Identity Code (without parity) or legacy ASCII code according to bit 7.
884  * JEP106 is a standard available from jedec.org
885  */
886
887 /* Part number interpretations are from Cortex
888  * core specs, the CoreSight components TRM
889  * (ARM DDI 0314H), CoreSight System Design
890  * Guide (ARM DGI 0012D) and ETM specs; also
891  * from chip observation (e.g. TI SDTI).
892  */
893
894 /* The legacy code only used the part number field to identify CoreSight peripherals.
895  * This meant that the same part number from two different manufacturers looked the same.
896  * It is desirable for all future additions to identify with both part number and JEP106.
897  * "ANY_ID" is a wildcard (any JEP106) only to preserve legacy behavior for legacy entries.
898  */
899
900 #define ANY_ID 0x1000
901
902 #define ARM_ID 0x4BB
903
904 static const struct {
905         uint16_t designer_id;
906         uint16_t part_num;
907         const char *type;
908         const char *full;
909 } dap_partnums[] = {
910         { ARM_ID, 0x000, "Cortex-M3 SCS",    "(System Control Space)", },
911         { ARM_ID, 0x001, "Cortex-M3 ITM",    "(Instrumentation Trace Module)", },
912         { ARM_ID, 0x002, "Cortex-M3 DWT",    "(Data Watchpoint and Trace)", },
913         { ARM_ID, 0x003, "Cortex-M3 FBP",    "(Flash Patch and Breakpoint)", },
914         { ARM_ID, 0x008, "Cortex-M0 SCS",    "(System Control Space)", },
915         { ARM_ID, 0x00a, "Cortex-M0 DWT",    "(Data Watchpoint and Trace)", },
916         { ARM_ID, 0x00b, "Cortex-M0 BPU",    "(Breakpoint Unit)", },
917         { ARM_ID, 0x00c, "Cortex-M4 SCS",    "(System Control Space)", },
918         { ARM_ID, 0x00d, "CoreSight ETM11",  "(Embedded Trace)", },
919         { ARM_ID, 0x490, "Cortex-A15 GIC",   "(Generic Interrupt Controller)", },
920         { ARM_ID, 0x4c7, "Cortex-M7 PPB",    "(Private Peripheral Bus ROM Table)", },
921         { ARM_ID, 0x906, "CoreSight CTI",    "(Cross Trigger)", },
922         { ARM_ID, 0x907, "CoreSight ETB",    "(Trace Buffer)", },
923         { ARM_ID, 0x908, "CoreSight CSTF",   "(Trace Funnel)", },
924         { ARM_ID, 0x910, "CoreSight ETM9",   "(Embedded Trace)", },
925         { ARM_ID, 0x912, "CoreSight TPIU",   "(Trace Port Interface Unit)", },
926         { ARM_ID, 0x913, "CoreSight ITM",    "(Instrumentation Trace Macrocell)", },
927         { ARM_ID, 0x914, "CoreSight SWO",    "(Single Wire Output)", },
928         { ARM_ID, 0x917, "CoreSight HTM",    "(AHB Trace Macrocell)", },
929         { ARM_ID, 0x920, "CoreSight ETM11",  "(Embedded Trace)", },
930         { ARM_ID, 0x921, "Cortex-A8 ETM",    "(Embedded Trace)", },
931         { ARM_ID, 0x922, "Cortex-A8 CTI",    "(Cross Trigger)", },
932         { ARM_ID, 0x923, "Cortex-M3 TPIU",   "(Trace Port Interface Unit)", },
933         { ARM_ID, 0x924, "Cortex-M3 ETM",    "(Embedded Trace)", },
934         { ARM_ID, 0x925, "Cortex-M4 ETM",    "(Embedded Trace)", },
935         { ARM_ID, 0x930, "Cortex-R4 ETM",    "(Embedded Trace)", },
936         { ARM_ID, 0x941, "CoreSight TPIU-Lite", "(Trace Port Interface Unit)", },
937         { ARM_ID, 0x950, "CoreSight Component", "(unidentified Cortex-A9 component)", },
938         { ARM_ID, 0x955, "CoreSight Component", "(unidentified Cortex-A5 component)", },
939         { ARM_ID, 0x95f, "Cortex-A15 PTM",   "(Program Trace Macrocell)", },
940         { ARM_ID, 0x961, "CoreSight TMC",    "(Trace Memory Controller)", },
941         { ARM_ID, 0x962, "CoreSight STM",    "(System Trace Macrocell)", },
942         { ARM_ID, 0x9a0, "CoreSight PMU",    "(Performance Monitoring Unit)", },
943         { ARM_ID, 0x9a1, "Cortex-M4 TPIU",   "(Trace Port Interface Unit)", },
944         { ARM_ID, 0x9a5, "Cortex-A5 ETM",    "(Embedded Trace)", },
945         { ARM_ID, 0x9a7, "Cortex-A7 PMU",    "(Performance Monitor Unit)", },
946         { ARM_ID, 0x9af, "Cortex-A15 PMU",   "(Performance Monitor Unit)", },
947         { ARM_ID, 0xc05, "Cortex-A5 Debug",  "(Debug Unit)", },
948         { ARM_ID, 0xc07, "Cortex-A7 Debug",  "(Debug Unit)", },
949         { ARM_ID, 0xc08, "Cortex-A8 Debug",  "(Debug Unit)", },
950         { ARM_ID, 0xc09, "Cortex-A9 Debug",  "(Debug Unit)", },
951         { ARM_ID, 0xc0f, "Cortex-A15 Debug", "(Debug Unit)", },
952         { ARM_ID, 0xc14, "Cortex-R4 Debug",  "(Debug Unit)", },
953         { 0x0E5,  0x000, "SHARC+/Blackfin+", "", },
954         /* legacy comment: 0x113: what? */
955         { ANY_ID,  0x120, "TI SDTI",         "(System Debug Trace Interface)", }, /* from OMAP3 memmap */
956         { ANY_ID,  0x343, "TI DAPCTL",       "", }, /* from OMAP3 memmap */
957 };
958
959 static int dap_rom_display(struct command_context *cmd_ctx,
960                                 struct adiv5_ap *ap, uint32_t dbgbase, int depth)
961 {
962         struct adiv5_dap *dap = ap->dap;
963         int retval;
964         uint32_t cid0, cid1, cid2, cid3, memtype, romentry;
965         uint16_t entry_offset;
966         char tabs[7] = "";
967
968         if (depth > 16) {
969                 command_print(cmd_ctx, "\tTables too deep");
970                 return ERROR_FAIL;
971         }
972
973         if (depth)
974                 snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
975
976         /* bit 16 of apid indicates a memory access port */
977         if (dbgbase & 0x02)
978                 command_print(cmd_ctx, "\t%sValid ROM table present", tabs);
979         else
980                 command_print(cmd_ctx, "\t%sROM table in legacy format", tabs);
981
982         /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec  */
983         retval = mem_ap_read_u32(ap, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
984         if (retval != ERROR_OK)
985                 return retval;
986         retval = mem_ap_read_u32(ap, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
987         if (retval != ERROR_OK)
988                 return retval;
989         retval = mem_ap_read_u32(ap, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
990         if (retval != ERROR_OK)
991                 return retval;
992         retval = mem_ap_read_u32(ap, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
993         if (retval != ERROR_OK)
994                 return retval;
995         retval = mem_ap_read_u32(ap, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
996         if (retval != ERROR_OK)
997                 return retval;
998         retval = dap_run(dap);
999         if (retval != ERROR_OK)
1000                 return retval;
1001
1002         if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1003                 command_print(cmd_ctx, "\t%sCID3 0x%02x"
1004                                 ", CID2 0x%02x"
1005                                 ", CID1 0x%02x"
1006                                 ", CID0 0x%02x",
1007                                 tabs,
1008                                 (unsigned)cid3, (unsigned)cid2,
1009                                 (unsigned)cid1, (unsigned)cid0);
1010         if (memtype & 0x01)
1011                 command_print(cmd_ctx, "\t%sMEMTYPE system memory present on bus", tabs);
1012         else
1013                 command_print(cmd_ctx, "\t%sMEMTYPE system memory not present: dedicated debug bus", tabs);
1014
1015         /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
1016         for (entry_offset = 0; ; entry_offset += 4) {
1017                 retval = mem_ap_read_atomic_u32(ap, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
1018                 if (retval != ERROR_OK)
1019                         return retval;
1020                 command_print(cmd_ctx, "\t%sROMTABLE[0x%x] = 0x%" PRIx32 "",
1021                                 tabs, entry_offset, romentry);
1022                 if (romentry & 0x01) {
1023                         uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
1024                         uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
1025                         uint32_t component_base;
1026                         uint16_t part_num, designer_id;
1027                         const char *type, *full;
1028
1029                         component_base = (dbgbase & 0xFFFFF000) + (romentry & 0xFFFFF000);
1030
1031                         /* IDs are in last 4K section */
1032                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFE0, &c_pid0);
1033                         if (retval != ERROR_OK) {
1034                                 command_print(cmd_ctx, "\t%s\tCan't read component with base address 0x%" PRIx32
1035                                               ", the corresponding core might be turned off", tabs, component_base);
1036                                 continue;
1037                         }
1038                         c_pid0 &= 0xff;
1039                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFE4, &c_pid1);
1040                         if (retval != ERROR_OK)
1041                                 return retval;
1042                         c_pid1 &= 0xff;
1043                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFE8, &c_pid2);
1044                         if (retval != ERROR_OK)
1045                                 return retval;
1046                         c_pid2 &= 0xff;
1047                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFEC, &c_pid3);
1048                         if (retval != ERROR_OK)
1049                                 return retval;
1050                         c_pid3 &= 0xff;
1051                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFD0, &c_pid4);
1052                         if (retval != ERROR_OK)
1053                                 return retval;
1054                         c_pid4 &= 0xff;
1055
1056                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFF0, &c_cid0);
1057                         if (retval != ERROR_OK)
1058                                 return retval;
1059                         c_cid0 &= 0xff;
1060                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFF4, &c_cid1);
1061                         if (retval != ERROR_OK)
1062                                 return retval;
1063                         c_cid1 &= 0xff;
1064                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFF8, &c_cid2);
1065                         if (retval != ERROR_OK)
1066                                 return retval;
1067                         c_cid2 &= 0xff;
1068                         retval = mem_ap_read_atomic_u32(ap, component_base + 0xFFC, &c_cid3);
1069                         if (retval != ERROR_OK)
1070                                 return retval;
1071                         c_cid3 &= 0xff;
1072
1073                         command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32 ", "
1074                                       "start address 0x%" PRIx32, component_base,
1075                                       /* component may take multiple 4K pages */
1076                                       (uint32_t)(component_base - 0x1000*(c_pid4 >> 4)));
1077                         command_print(cmd_ctx, "\t\tComponent class is 0x%" PRIx8 ", %s",
1078                                         (uint8_t)((c_cid1 >> 4) & 0xf),
1079                                         /* See ARM IHI 0029B Table 3-3 */
1080                                         class_description[(c_cid1 >> 4) & 0xf]);
1081
1082                         /* CoreSight component? */
1083                         if (((c_cid1 >> 4) & 0x0f) == 9) {
1084                                 uint32_t devtype;
1085                                 unsigned minor;
1086                                 const char *major = "Reserved", *subtype = "Reserved";
1087
1088                                 retval = mem_ap_read_atomic_u32(ap,
1089                                                 (component_base & 0xfffff000) | 0xfcc,
1090                                                 &devtype);
1091                                 if (retval != ERROR_OK)
1092                                         return retval;
1093                                 minor = (devtype >> 4) & 0x0f;
1094                                 switch (devtype & 0x0f) {
1095                                 case 0:
1096                                         major = "Miscellaneous";
1097                                         switch (minor) {
1098                                         case 0:
1099                                                 subtype = "other";
1100                                                 break;
1101                                         case 4:
1102                                                 subtype = "Validation component";
1103                                                 break;
1104                                         }
1105                                         break;
1106                                 case 1:
1107                                         major = "Trace Sink";
1108                                         switch (minor) {
1109                                         case 0:
1110                                                 subtype = "other";
1111                                                 break;
1112                                         case 1:
1113                                                 subtype = "Port";
1114                                                 break;
1115                                         case 2:
1116                                                 subtype = "Buffer";
1117                                                 break;
1118                                         case 3:
1119                                                 subtype = "Router";
1120                                                 break;
1121                                         }
1122                                         break;
1123                                 case 2:
1124                                         major = "Trace Link";
1125                                         switch (minor) {
1126                                         case 0:
1127                                                 subtype = "other";
1128                                                 break;
1129                                         case 1:
1130                                                 subtype = "Funnel, router";
1131                                                 break;
1132                                         case 2:
1133                                                 subtype = "Filter";
1134                                                 break;
1135                                         case 3:
1136                                                 subtype = "FIFO, buffer";
1137                                                 break;
1138                                         }
1139                                         break;
1140                                 case 3:
1141                                         major = "Trace Source";
1142                                         switch (minor) {
1143                                         case 0:
1144                                                 subtype = "other";
1145                                                 break;
1146                                         case 1:
1147                                                 subtype = "Processor";
1148                                                 break;
1149                                         case 2:
1150                                                 subtype = "DSP";
1151                                                 break;
1152                                         case 3:
1153                                                 subtype = "Engine/Coprocessor";
1154                                                 break;
1155                                         case 4:
1156                                                 subtype = "Bus";
1157                                                 break;
1158                                         case 6:
1159                                                 subtype = "Software";
1160                                                 break;
1161                                         }
1162                                         break;
1163                                 case 4:
1164                                         major = "Debug Control";
1165                                         switch (minor) {
1166                                         case 0:
1167                                                 subtype = "other";
1168                                                 break;
1169                                         case 1:
1170                                                 subtype = "Trigger Matrix";
1171                                                 break;
1172                                         case 2:
1173                                                 subtype = "Debug Auth";
1174                                                 break;
1175                                         case 3:
1176                                                 subtype = "Power Requestor";
1177                                                 break;
1178                                         }
1179                                         break;
1180                                 case 5:
1181                                         major = "Debug Logic";
1182                                         switch (minor) {
1183                                         case 0:
1184                                                 subtype = "other";
1185                                                 break;
1186                                         case 1:
1187                                                 subtype = "Processor";
1188                                                 break;
1189                                         case 2:
1190                                                 subtype = "DSP";
1191                                                 break;
1192                                         case 3:
1193                                                 subtype = "Engine/Coprocessor";
1194                                                 break;
1195                                         case 4:
1196                                                 subtype = "Bus";
1197                                                 break;
1198                                         case 5:
1199                                                 subtype = "Memory";
1200                                                 break;
1201                                         }
1202                                         break;
1203                                 case 6:
1204                                         major = "Perfomance Monitor";
1205                                         switch (minor) {
1206                                         case 0:
1207                                                 subtype = "other";
1208                                                 break;
1209                                         case 1:
1210                                                 subtype = "Processor";
1211                                                 break;
1212                                         case 2:
1213                                                 subtype = "DSP";
1214                                                 break;
1215                                         case 3:
1216                                                 subtype = "Engine/Coprocessor";
1217                                                 break;
1218                                         case 4:
1219                                                 subtype = "Bus";
1220                                                 break;
1221                                         case 5:
1222                                                 subtype = "Memory";
1223                                                 break;
1224                                         }
1225                                         break;
1226                                 }
1227                                 command_print(cmd_ctx, "\t\tType is 0x%02" PRIx8 ", %s, %s",
1228                                                 (uint8_t)(devtype & 0xff),
1229                                                 major, subtype);
1230                                 /* REVISIT also show 0xfc8 DevId */
1231                         }
1232
1233                         if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1234                                 command_print(cmd_ctx,
1235                                                 "\t\tCID3 0%02x"
1236                                                 ", CID2 0%02x"
1237                                                 ", CID1 0%02x"
1238                                                 ", CID0 0%02x",
1239                                                 (int)c_cid3,
1240                                                 (int)c_cid2,
1241                                                 (int)c_cid1,
1242                                                 (int)c_cid0);
1243                         command_print(cmd_ctx,
1244                                 "\t\tPeripheral ID[4..0] = hex "
1245                                 "%02x %02x %02x %02x %02x",
1246                                 (int)c_pid4, (int)c_pid3, (int)c_pid2,
1247                                 (int)c_pid1, (int)c_pid0);
1248
1249                         part_num = (c_pid0 & 0xff);
1250                         part_num |= (c_pid1 & 0x0f) << 8;
1251                         designer_id = (c_pid1 & 0xf0) >> 4;
1252                         designer_id |= (c_pid2 & 0x0f) << 4;
1253                         designer_id |= (c_pid4 & 0x0f) << 8;
1254                         if ((designer_id & 0x80) == 0) {
1255                                 /* Legacy ASCII ID, clear invalid bits */
1256                                 designer_id &= 0x7f;
1257                         }
1258
1259                         /* default values to be overwritten upon finding a match */
1260                         type = NULL;
1261                         full = "";
1262
1263                         /* search dap_partnums[] array for a match */
1264                         unsigned entry;
1265                         for (entry = 0; entry < ARRAY_SIZE(dap_partnums); entry++) {
1266
1267                                 if ((dap_partnums[entry].designer_id != designer_id) && (dap_partnums[entry].designer_id != ANY_ID))
1268                                         continue;
1269
1270                                 if (dap_partnums[entry].part_num != part_num)
1271                                         continue;
1272
1273                                 type = dap_partnums[entry].type;
1274                                 full = dap_partnums[entry].full;
1275                                 break;
1276                         }
1277
1278                         if (type) {
1279                                 command_print(cmd_ctx, "\t\tPart is %s %s",
1280                                                 type, full);
1281                         } else {
1282                                 command_print(cmd_ctx, "\t\tUnrecognized (Part 0x%" PRIx16 ", designer 0x%" PRIx16 ")",
1283                                                 part_num, designer_id);
1284                         }
1285
1286                         /* ROM Table? */
1287                         if (((c_cid1 >> 4) & 0x0f) == 1) {
1288                                 retval = dap_rom_display(cmd_ctx, ap, component_base, depth + 1);
1289                                 if (retval != ERROR_OK)
1290                                         return retval;
1291                         }
1292                 } else {
1293                         if (romentry)
1294                                 command_print(cmd_ctx, "\t\tComponent not present");
1295                         else
1296                                 break;
1297                 }
1298         }
1299         command_print(cmd_ctx, "\t%s\tEnd of ROM table", tabs);
1300         return ERROR_OK;
1301 }
1302
1303 static int dap_info_command(struct command_context *cmd_ctx,
1304                 struct adiv5_ap *ap)
1305 {
1306         int retval;
1307         uint32_t dbgbase, apid;
1308         int romtable_present = 0;
1309         uint8_t mem_ap;
1310
1311         /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec  */
1312         retval = dap_get_debugbase(ap, &dbgbase, &apid);
1313         if (retval != ERROR_OK)
1314                 return retval;
1315
1316         command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
1317         if (apid == 0) {
1318                 command_print(cmd_ctx, "No AP found at this ap 0x%x", ap->ap_num);
1319                 return ERROR_FAIL;
1320         }
1321
1322         switch (apid & (IDR_JEP106 | IDR_TYPE)) {
1323         case IDR_JEP106_ARM | AP_TYPE_JTAG_AP:
1324                 command_print(cmd_ctx, "\tType is JTAG-AP");
1325                 break;
1326         case IDR_JEP106_ARM | AP_TYPE_AHB_AP:
1327                 command_print(cmd_ctx, "\tType is MEM-AP AHB");
1328                 break;
1329         case IDR_JEP106_ARM | AP_TYPE_APB_AP:
1330                 command_print(cmd_ctx, "\tType is MEM-AP APB");
1331                 break;
1332         case IDR_JEP106_ARM | AP_TYPE_AXI_AP:
1333                 command_print(cmd_ctx, "\tType is MEM-AP AXI");
1334                 break;
1335         default:
1336                 command_print(cmd_ctx, "\tUnknown AP type");
1337                 break;
1338         }
1339
1340         /* NOTE: a MEM-AP may have a single CoreSight component that's
1341          * not a ROM table ... or have no such components at all.
1342          */
1343         mem_ap = (apid & IDR_CLASS) == AP_CLASS_MEM_AP;
1344         if (mem_ap) {
1345                 command_print(cmd_ctx, "MEM-AP BASE 0x%8.8" PRIx32, dbgbase);
1346
1347                 romtable_present = dbgbase != 0xFFFFFFFF;
1348                 if (romtable_present)
1349                         dap_rom_display(cmd_ctx, ap, dbgbase, 0);
1350                 else
1351                         command_print(cmd_ctx, "\tNo ROM table present");
1352         }
1353
1354         return ERROR_OK;
1355 }
1356
1357 COMMAND_HANDLER(handle_dap_info_command)
1358 {
1359         struct target *target = get_current_target(CMD_CTX);
1360         struct arm *arm = target_to_arm(target);
1361         struct adiv5_dap *dap = arm->dap;
1362         uint32_t apsel;
1363
1364         switch (CMD_ARGC) {
1365         case 0:
1366                 apsel = dap->apsel;
1367                 break;
1368         case 1:
1369                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1370                 if (apsel >= 256)
1371                         return ERROR_COMMAND_SYNTAX_ERROR;
1372                 break;
1373         default:
1374                 return ERROR_COMMAND_SYNTAX_ERROR;
1375         }
1376
1377         return dap_info_command(CMD_CTX, &dap->ap[apsel]);
1378 }
1379
1380 COMMAND_HANDLER(dap_baseaddr_command)
1381 {
1382         struct target *target = get_current_target(CMD_CTX);
1383         struct arm *arm = target_to_arm(target);
1384         struct adiv5_dap *dap = arm->dap;
1385
1386         uint32_t apsel, baseaddr;
1387         int retval;
1388
1389         switch (CMD_ARGC) {
1390         case 0:
1391                 apsel = dap->apsel;
1392                 break;
1393         case 1:
1394                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1395                 /* AP address is in bits 31:24 of DP_SELECT */
1396                 if (apsel >= 256)
1397                         return ERROR_COMMAND_SYNTAX_ERROR;
1398                 break;
1399         default:
1400                 return ERROR_COMMAND_SYNTAX_ERROR;
1401         }
1402
1403         /* NOTE:  assumes we're talking to a MEM-AP, which
1404          * has a base address.  There are other kinds of AP,
1405          * though they're not common for now.  This should
1406          * use the ID register to verify it's a MEM-AP.
1407          */
1408         retval = dap_queue_ap_read(dap_ap(dap, apsel), MEM_AP_REG_BASE, &baseaddr);
1409         if (retval != ERROR_OK)
1410                 return retval;
1411         retval = dap_run(dap);
1412         if (retval != ERROR_OK)
1413                 return retval;
1414
1415         command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
1416
1417         return retval;
1418 }
1419
1420 COMMAND_HANDLER(dap_memaccess_command)
1421 {
1422         struct target *target = get_current_target(CMD_CTX);
1423         struct arm *arm = target_to_arm(target);
1424         struct adiv5_dap *dap = arm->dap;
1425
1426         uint32_t memaccess_tck;
1427
1428         switch (CMD_ARGC) {
1429         case 0:
1430                 memaccess_tck = dap->ap[dap->apsel].memaccess_tck;
1431                 break;
1432         case 1:
1433                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
1434                 break;
1435         default:
1436                 return ERROR_COMMAND_SYNTAX_ERROR;
1437         }
1438         dap->ap[dap->apsel].memaccess_tck = memaccess_tck;
1439
1440         command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
1441                         dap->ap[dap->apsel].memaccess_tck);
1442
1443         return ERROR_OK;
1444 }
1445
1446 COMMAND_HANDLER(dap_apsel_command)
1447 {
1448         struct target *target = get_current_target(CMD_CTX);
1449         struct arm *arm = target_to_arm(target);
1450         struct adiv5_dap *dap = arm->dap;
1451
1452         uint32_t apsel, apid;
1453         int retval;
1454
1455         switch (CMD_ARGC) {
1456         case 0:
1457                 apsel = dap->apsel;
1458                 break;
1459         case 1:
1460                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1461                 /* AP address is in bits 31:24 of DP_SELECT */
1462                 if (apsel >= 256)
1463                         return ERROR_COMMAND_SYNTAX_ERROR;
1464                 break;
1465         default:
1466                 return ERROR_COMMAND_SYNTAX_ERROR;
1467         }
1468
1469         dap->apsel = apsel;
1470
1471         retval = dap_queue_ap_read(dap_ap(dap, apsel), AP_REG_IDR, &apid);
1472         if (retval != ERROR_OK)
1473                 return retval;
1474         retval = dap_run(dap);
1475         if (retval != ERROR_OK)
1476                 return retval;
1477
1478         command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
1479                         apsel, apid);
1480
1481         return retval;
1482 }
1483
1484 COMMAND_HANDLER(dap_apcsw_command)
1485 {
1486         struct target *target = get_current_target(CMD_CTX);
1487         struct arm *arm = target_to_arm(target);
1488         struct adiv5_dap *dap = arm->dap;
1489
1490         uint32_t apcsw = dap->ap[dap->apsel].csw_default, sprot = 0;
1491
1492         switch (CMD_ARGC) {
1493         case 0:
1494                 command_print(CMD_CTX, "apsel %" PRIi32 " selected, csw 0x%8.8" PRIx32,
1495                         (dap->apsel), apcsw);
1496                 break;
1497         case 1:
1498                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], sprot);
1499                 /* AP address is in bits 31:24 of DP_SELECT */
1500                 if (sprot > 1)
1501                         return ERROR_COMMAND_SYNTAX_ERROR;
1502                 if (sprot)
1503                         apcsw |= CSW_SPROT;
1504                 else
1505                         apcsw &= ~CSW_SPROT;
1506                 break;
1507         default:
1508                 return ERROR_COMMAND_SYNTAX_ERROR;
1509         }
1510         dap->ap[dap->apsel].csw_default = apcsw;
1511
1512         return 0;
1513 }
1514
1515
1516
1517 COMMAND_HANDLER(dap_apid_command)
1518 {
1519         struct target *target = get_current_target(CMD_CTX);
1520         struct arm *arm = target_to_arm(target);
1521         struct adiv5_dap *dap = arm->dap;
1522
1523         uint32_t apsel, apid;
1524         int retval;
1525
1526         switch (CMD_ARGC) {
1527         case 0:
1528                 apsel = dap->apsel;
1529                 break;
1530         case 1:
1531                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1532                 /* AP address is in bits 31:24 of DP_SELECT */
1533                 if (apsel >= 256)
1534                         return ERROR_COMMAND_SYNTAX_ERROR;
1535                 break;
1536         default:
1537                 return ERROR_COMMAND_SYNTAX_ERROR;
1538         }
1539
1540         retval = dap_queue_ap_read(dap_ap(dap, apsel), AP_REG_IDR, &apid);
1541         if (retval != ERROR_OK)
1542                 return retval;
1543         retval = dap_run(dap);
1544         if (retval != ERROR_OK)
1545                 return retval;
1546
1547         command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
1548
1549         return retval;
1550 }
1551
1552 COMMAND_HANDLER(dap_ti_be_32_quirks_command)
1553 {
1554         struct target *target = get_current_target(CMD_CTX);
1555         struct arm *arm = target_to_arm(target);
1556         struct adiv5_dap *dap = arm->dap;
1557
1558         uint32_t enable = dap->ti_be_32_quirks;
1559
1560         switch (CMD_ARGC) {
1561         case 0:
1562                 break;
1563         case 1:
1564                 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], enable);
1565                 if (enable > 1)
1566                         return ERROR_COMMAND_SYNTAX_ERROR;
1567                 break;
1568         default:
1569                 return ERROR_COMMAND_SYNTAX_ERROR;
1570         }
1571         dap->ti_be_32_quirks = enable;
1572         command_print(CMD_CTX, "TI BE-32 quirks mode %s",
1573                 enable ? "enabled" : "disabled");
1574
1575         return 0;
1576 }
1577
1578 static const struct command_registration dap_commands[] = {
1579         {
1580                 .name = "info",
1581                 .handler = handle_dap_info_command,
1582                 .mode = COMMAND_EXEC,
1583                 .help = "display ROM table for MEM-AP "
1584                         "(default currently selected AP)",
1585                 .usage = "[ap_num]",
1586         },
1587         {
1588                 .name = "apsel",
1589                 .handler = dap_apsel_command,
1590                 .mode = COMMAND_EXEC,
1591                 .help = "Set the currently selected AP (default 0) "
1592                         "and display the result",
1593                 .usage = "[ap_num]",
1594         },
1595         {
1596                 .name = "apcsw",
1597                 .handler = dap_apcsw_command,
1598                 .mode = COMMAND_EXEC,
1599                 .help = "Set csw access bit ",
1600                 .usage = "[sprot]",
1601         },
1602
1603         {
1604                 .name = "apid",
1605                 .handler = dap_apid_command,
1606                 .mode = COMMAND_EXEC,
1607                 .help = "return ID register from AP "
1608                         "(default currently selected AP)",
1609                 .usage = "[ap_num]",
1610         },
1611         {
1612                 .name = "baseaddr",
1613                 .handler = dap_baseaddr_command,
1614                 .mode = COMMAND_EXEC,
1615                 .help = "return debug base address from MEM-AP "
1616                         "(default currently selected AP)",
1617                 .usage = "[ap_num]",
1618         },
1619         {
1620                 .name = "memaccess",
1621                 .handler = dap_memaccess_command,
1622                 .mode = COMMAND_EXEC,
1623                 .help = "set/get number of extra tck for MEM-AP memory "
1624                         "bus access [0-255]",
1625                 .usage = "[cycles]",
1626         },
1627         {
1628                 .name = "ti_be_32_quirks",
1629                 .handler = dap_ti_be_32_quirks_command,
1630                 .mode = COMMAND_CONFIG,
1631                 .help = "set/get quirks mode for TI TMS450/TMS570 processors",
1632                 .usage = "[enable]",
1633         },
1634         COMMAND_REGISTRATION_DONE
1635 };
1636
1637 const struct command_registration dap_command_handlers[] = {
1638         {
1639                 .name = "dap",
1640                 .mode = COMMAND_EXEC,
1641                 .help = "DAP command group",
1642                 .usage = "",
1643                 .chain = dap_commands,
1644         },
1645         COMMAND_REGISTRATION_DONE
1646 };