OpenOCD
arm_adi_v5.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Magnus Lundin *
5  * lundin@mlu.mine.nu *
6  * *
7  * Copyright (C) 2008 by Spencer Oliver *
8  * spen@spen-soft.co.uk *
9  * *
10  * Copyright (C) 2009-2010 by Oyvind Harboe *
11  * oyvind.harboe@zylin.com *
12  * *
13  * Copyright (C) 2009-2010 by David Brownell *
14  * *
15  * Copyright (C) 2013 by Andreas Fritiofson *
16  * andreas.fritiofson@gmail.com *
17  * *
18  * Copyright (C) 2019-2021, Ampere Computing LLC *
19  ***************************************************************************/
20 
50 /*
51  * Relevant specifications from ARM include:
52  *
53  * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031F
54  * ARM(tm) Debug Interface v6 Architecture Specification ARM IHI 0074C
55  * CoreSight(tm) v1.0 Architecture Specification ARM IHI 0029B
56  *
57  * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316D
58  * Cortex-M3(tm) TRM, ARM DDI 0337G
59  */
60 
61 #ifdef HAVE_CONFIG_H
62 #include "config.h"
63 #endif
64 
65 #include "jtag/interface.h"
66 #include "arm.h"
67 #include "arm_adi_v5.h"
68 #include "arm_coresight.h"
69 #include "jtag/swd.h"
70 #include "transport/transport.h"
71 #include <helper/align.h>
72 #include <helper/jep106.h>
73 #include <helper/time_support.h>
74 #include <helper/list.h>
75 #include <helper/jim-nvp.h>
76 
77 /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
78 
79 /*
80  uint32_t tar_block_size(uint32_t address)
81  Return the largest block starting at address that does not cross a tar block size alignment boundary
82 */
83 static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, target_addr_t address)
84 {
85  return tar_autoincr_block - ((tar_autoincr_block - 1) & address);
86 }
87 
88 /***************************************************************************
89  * *
90  * DP and MEM-AP register access through APACC and DPACC *
91  * *
92 ***************************************************************************/
93 
94 static int mem_ap_setup_csw(struct adiv5_ap *ap, uint32_t csw)
95 {
96  csw |= ap->csw_default;
97 
98  if (csw != ap->csw_value) {
99  /* LOG_DEBUG("DAP: Set CSW %x",csw); */
100  int retval = dap_queue_ap_write(ap, MEM_AP_REG_CSW(ap->dap), csw);
101  if (retval != ERROR_OK) {
102  ap->csw_value = 0;
103  return retval;
104  }
105  ap->csw_value = csw;
106  }
107  return ERROR_OK;
108 }
109 
110 static int mem_ap_setup_tar(struct adiv5_ap *ap, target_addr_t tar)
111 {
112  if (!ap->tar_valid || tar != ap->tar_value) {
113  /* LOG_DEBUG("DAP: Set TAR %x",tar); */
114  int retval = dap_queue_ap_write(ap, MEM_AP_REG_TAR(ap->dap), (uint32_t)(tar & 0xffffffffUL));
115  if (retval == ERROR_OK && is_64bit_ap(ap)) {
116  /* See if bits 63:32 of tar is different from last setting */
117  if (!ap->tar_valid || (ap->tar_value >> 32) != (tar >> 32))
118  retval = dap_queue_ap_write(ap, MEM_AP_REG_TAR64(ap->dap), (uint32_t)(tar >> 32));
119  }
120  if (retval != ERROR_OK) {
121  ap->tar_valid = false;
122  return retval;
123  }
124  ap->tar_value = tar;
125  ap->tar_valid = true;
126  }
127  return ERROR_OK;
128 }
129 
130 static int mem_ap_read_tar(struct adiv5_ap *ap, target_addr_t *tar)
131 {
132  uint32_t lower;
133  uint32_t upper = 0;
134 
135  int retval = dap_queue_ap_read(ap, MEM_AP_REG_TAR(ap->dap), &lower);
136  if (retval == ERROR_OK && is_64bit_ap(ap))
137  retval = dap_queue_ap_read(ap, MEM_AP_REG_TAR64(ap->dap), &upper);
138 
139  if (retval != ERROR_OK) {
140  ap->tar_valid = false;
141  return retval;
142  }
143 
144  retval = dap_run(ap->dap);
145  if (retval != ERROR_OK) {
146  ap->tar_valid = false;
147  return retval;
148  }
149 
150  *tar = (((target_addr_t)upper) << 32) | (target_addr_t)lower;
151 
152  ap->tar_value = *tar;
153  ap->tar_valid = true;
154  return ERROR_OK;
155 }
156 
157 static uint32_t mem_ap_get_tar_increment(struct adiv5_ap *ap)
158 {
159  switch (ap->csw_value & CSW_ADDRINC_MASK) {
160  case CSW_ADDRINC_SINGLE:
161  switch (ap->csw_value & CSW_SIZE_MASK) {
162  case CSW_8BIT:
163  return 1;
164  case CSW_16BIT:
165  return 2;
166  case CSW_32BIT:
167  return 4;
168  case CSW_64BIT:
169  return 8;
170  case CSW_128BIT:
171  return 16;
172  case CSW_256BIT:
173  return 32;
174  default:
175  return 0;
176  }
177  case CSW_ADDRINC_PACKED:
178  return 4;
179  }
180  return 0;
181 }
182 
183 /* mem_ap_update_tar_cache is called after an access to MEM_AP_REG_DRW
184  */
185 static void mem_ap_update_tar_cache(struct adiv5_ap *ap)
186 {
187  if (!ap->tar_valid)
188  return;
189 
190  uint32_t inc = mem_ap_get_tar_increment(ap);
191  if (inc >= max_tar_block_size(ap->tar_autoincr_block, ap->tar_value))
192  ap->tar_valid = false;
193  else
194  ap->tar_value += inc;
195 }
196 
214 static int mem_ap_setup_transfer(struct adiv5_ap *ap, uint32_t csw, target_addr_t tar)
215 {
216  int retval;
217  retval = mem_ap_setup_csw(ap, csw);
218  if (retval != ERROR_OK)
219  return retval;
220  retval = mem_ap_setup_tar(ap, tar);
221  if (retval != ERROR_OK)
222  return retval;
223  return ERROR_OK;
224 }
225 
237 int mem_ap_read_u32(struct adiv5_ap *ap, target_addr_t address,
238  uint32_t *value)
239 {
240  int retval;
241 
242  /* Use banked addressing (REG_BDx) to avoid some link traffic
243  * (updating TAR) when reading several consecutive addresses.
244  */
245  retval = mem_ap_setup_transfer(ap,
247  address & 0xFFFFFFFFFFFFFFF0ull);
248  if (retval != ERROR_OK)
249  return retval;
250 
251  return dap_queue_ap_read(ap, MEM_AP_REG_BD0(ap->dap) | (address & 0xC), value);
252 }
253 
267  uint32_t *value)
268 {
269  int retval;
270 
271  retval = mem_ap_read_u32(ap, address, value);
272  if (retval != ERROR_OK)
273  return retval;
274 
275  return dap_run(ap->dap);
276 }
277 
289 int mem_ap_write_u32(struct adiv5_ap *ap, target_addr_t address,
290  uint32_t value)
291 {
292  int retval;
293 
294  /* Use banked addressing (REG_BDx) to avoid some link traffic
295  * (updating TAR) when writing several consecutive addresses.
296  */
297  retval = mem_ap_setup_transfer(ap,
299  address & 0xFFFFFFFFFFFFFFF0ull);
300  if (retval != ERROR_OK)
301  return retval;
302 
303  return dap_queue_ap_write(ap, MEM_AP_REG_BD0(ap->dap) | (address & 0xC),
304  value);
305 }
306 
319  uint32_t value)
320 {
321  int retval = mem_ap_write_u32(ap, address, value);
322 
323  if (retval != ERROR_OK)
324  return retval;
325 
326  return dap_run(ap->dap);
327 }
328 
346  unsigned int size, target_addr_t address,
347  bool addrinc, bool pack, unsigned int *this_size)
348 {
349  int retval;
350  uint32_t csw_size;
351 
352  switch (size) {
353  case 1:
354  csw_size = CSW_8BIT;
355  break;
356  case 2:
357  csw_size = CSW_16BIT;
358  break;
359  case 4:
360  csw_size = CSW_32BIT;
361  break;
362  case 8:
363  csw_size = CSW_64BIT;
364  break;
365  case 16:
366  csw_size = CSW_128BIT;
367  break;
368  case 32:
369  csw_size = CSW_256BIT;
370  break;
371  default:
372  LOG_ERROR("Size %u not supported", size);
374  }
375 
376  if (!addrinc || size >= 4
378  || max_tar_block_size(ap->tar_autoincr_block, address) < 4)
379  pack = false;
380 
381  uint32_t csw_addrinc = pack ? CSW_ADDRINC_PACKED :
383  retval = mem_ap_setup_csw(ap, csw_size | csw_addrinc);
384  if (retval != ERROR_OK)
385  return retval;
386 
387  bool do_probe = !(ap->csw_size_probed_mask & size)
388  || (pack && !ap->packed_transfers_probed);
389  if (do_probe) {
390  uint32_t csw_readback;
391  retval = dap_queue_ap_read(ap, MEM_AP_REG_CSW(ap->dap), &csw_readback);
392  if (retval != ERROR_OK)
393  return retval;
394 
395  retval = dap_run(ap->dap);
396  if (retval != ERROR_OK)
397  return retval;
398 
399  bool size_supported = ((csw_readback & CSW_SIZE_MASK) == csw_size);
400  LOG_DEBUG("AP#0x%" PRIx64 " probed size %u: %s", ap->ap_num, size,
401  size_supported ? "supported" : "not supported");
402  ap->csw_size_probed_mask |= size;
403  if (size_supported) {
405  if (pack && !ap->packed_transfers_probed) {
406  ap->packed_transfers_probed = true;
408  ((csw_readback & CSW_ADDRINC_MASK) == csw_addrinc);
409  LOG_DEBUG("probed packing: %s",
410  ap->packed_transfers_supported ? "supported" : "not supported");
411  }
412  }
413  }
414 
415  if (!(ap->csw_size_supported_mask & size)) {
416  LOG_ERROR("Size %u not supported", size);
418  }
419 
420  if (pack && !ap->packed_transfers_supported)
422 
423  *this_size = pack ? 4 : size;
424 
425  return mem_ap_setup_tar(ap, address);
426 }
427 
446  unsigned int size, target_addr_t address,
447  bool addrinc, bool pack, unsigned int *this_size)
448 {
450  size, address,
451  addrinc, pack, this_size);
452  if (retval == ERROR_TARGET_PACKING_NOT_SUPPORTED) {
453  /* Retry without packing */
455  size, address,
456  addrinc, false, this_size);
457  }
458  return retval;
459 }
460 
474 static int mem_ap_write(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count,
475  target_addr_t address, bool addrinc)
476 {
477  struct adiv5_dap *dap = ap->dap;
478  size_t nbytes = size * count;
479  int retval = ERROR_OK;
480 
481  /* TI BE-32 Quirks mode:
482  * Writes on big-endian TMS570 behave very strangely. Observed behavior:
483  * size write address bytes written in order
484  * 4 TAR ^ 0 (val >> 24), (val >> 16), (val >> 8), (val)
485  * 2 TAR ^ 2 (val >> 8), (val)
486  * 1 TAR ^ 3 (val)
487  * For example, if you attempt to write a single byte to address 0, the processor
488  * will actually write a byte to address 3.
489  *
490  * To make writes of size < 4 work as expected, we xor a value with the address before
491  * setting the TAP, and we set the TAP after every transfer rather then relying on
492  * address increment. */
493  target_addr_t ti_be_addr_xor = 0;
494  target_addr_t ti_be_lane_xor = 0;
495  if (dap->ti_be_32_quirks) {
496  ti_be_lane_xor = 3;
497  switch (size) {
498  case 1:
499  ti_be_addr_xor = 3;
500  break;
501  case 2:
502  ti_be_addr_xor = 2;
503  break;
504  case 4:
505  break;
506  default:
507  LOG_ERROR("Write more than 32 bits not supported with ti_be_32_quirks");
509  }
510  }
511 
512  if (ap->unaligned_access_bad && (address % size != 0))
514 
515  /* Nuvoton NPCX quirks prevent packed writes */
516  bool pack = !dap->nu_npcx_quirks;
517 
518  while (nbytes > 0) {
519  unsigned int this_size;
521  size, address ^ ti_be_addr_xor,
522  addrinc, pack && nbytes >= 4, &this_size);
523  if (retval != ERROR_OK)
524  return retval;
525 
526  /* How many source bytes each transfer will consume, and their location in the DRW,
527  * depends on the type of transfer and alignment. See ARM document IHI0031C. */
528  uint32_t drw_byte_idx = address;
529  unsigned int drw_ops = DIV_ROUND_UP(this_size, 4);
530 
531  while (drw_ops--) {
532  uint32_t outvalue = 0;
533  if (dap->nu_npcx_quirks && this_size <= 2) {
534  switch (this_size) {
535  case 2:
536  {
537  /* Alternate low and high byte to all byte lanes */
538  uint32_t low = *buffer++;
539  uint32_t high = *buffer++;
540  outvalue |= low << 8 * (drw_byte_idx++ & 3);
541  outvalue |= high << 8 * (drw_byte_idx++ & 3);
542  outvalue |= low << 8 * (drw_byte_idx++ & 3);
543  outvalue |= high << 8 * (drw_byte_idx & 3);
544  }
545  break;
546  case 1:
547  {
548  /* Mirror output byte to all byte lanes */
549  uint32_t data = *buffer++;
550  outvalue |= data;
551  outvalue |= data << 8;
552  outvalue |= data << 16;
553  outvalue |= data << 24;
554  }
555  }
556  } else {
557  unsigned int drw_bytes = MIN(this_size, 4);
558  while (drw_bytes--)
559  outvalue |= (uint32_t)*buffer++ <<
560  8 * ((drw_byte_idx++ & 3) ^ ti_be_lane_xor);
561  }
562 
563  retval = dap_queue_ap_write(ap, MEM_AP_REG_DRW(dap), outvalue);
564  if (retval != ERROR_OK)
565  break;
566  }
567  if (retval != ERROR_OK)
568  break;
569 
571  nbytes -= this_size;
572  if (addrinc)
573  address += this_size;
574  }
575 
576  /* REVISIT: Might want to have a queued version of this function that does not run. */
577  if (retval == ERROR_OK)
578  retval = dap_run(dap);
579 
580  if (retval != ERROR_OK) {
581  target_addr_t tar;
582  if (mem_ap_read_tar(ap, &tar) == ERROR_OK)
583  LOG_ERROR("Failed to write memory at " TARGET_ADDR_FMT, tar);
584  else
585  LOG_ERROR("Failed to write memory and, additionally, failed to find out where");
586  }
587 
588  return retval;
589 }
590 
604 static int mem_ap_read(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count,
605  target_addr_t adr, bool addrinc)
606 {
607  struct adiv5_dap *dap = ap->dap;
608  size_t nbytes = size * count;
609  target_addr_t address = adr;
610  int retval = ERROR_OK;
611 
612  /* TI BE-32 Quirks mode:
613  * Reads on big-endian TMS570 behave strangely differently than writes.
614  * They read from the physical address requested, but with DRW byte-reversed.
615  * For example, a byte read from address 0 will place the result in the high bytes of DRW.
616  * Also, packed 8-bit and 16-bit transfers seem to sometimes return garbage in some bytes,
617  * so avoid them (ap->packed_transfers is forced to false in mem_ap_init). */
618 
619  if (dap->ti_be_32_quirks && size > 4) {
620  LOG_ERROR("Read more than 32 bits not supported with ti_be_32_quirks");
622  }
623 
624  if (ap->unaligned_access_bad && (adr % size != 0))
626 
627  /* Allocate buffer to hold the sequence of DRW reads that will be made. This is a significant
628  * over-allocation if packed transfers are going to be used, but determining the real need at
629  * this point would be messy. */
630  uint32_t *read_buf = calloc(count, MAX(sizeof(uint32_t), size));
631 
632  /* Multiplication count * sizeof(uint32_t) may overflow, calloc() is safe */
633  uint32_t *read_ptr = read_buf;
634  if (!read_buf) {
635  LOG_ERROR("Failed to allocate read buffer");
636  return ERROR_FAIL;
637  }
638 
639  /* Queue up all reads. Each read will store the entire DRW word in the read buffer. How many
640  * useful bytes it contains, and their location in the word, depends on the type of transfer
641  * and alignment. */
642  while (nbytes > 0) {
643  unsigned int this_size;
645  size, address,
646  addrinc, nbytes >= 4, &this_size);
647  if (retval != ERROR_OK)
648  break;
649 
650 
651  unsigned int drw_ops = DIV_ROUND_UP(this_size, 4);
652  while (drw_ops--) {
653  retval = dap_queue_ap_read(ap, MEM_AP_REG_DRW(dap), read_ptr++);
654  if (retval != ERROR_OK)
655  break;
656  }
657 
658  nbytes -= this_size;
659  if (addrinc)
660  address += this_size;
661 
663  }
664 
665  if (retval == ERROR_OK)
666  retval = dap_run(dap);
667 
668  /* Restore state */
669  address = adr;
670  nbytes = size * count;
671  read_ptr = read_buf;
672 
673  /* If something failed, read TAR to find out how much data was successfully read, so we can
674  * at least give the caller what we have. */
675  if (retval == ERROR_TARGET_SIZE_NOT_SUPPORTED) {
676  nbytes = 0;
677  } else if (retval != ERROR_OK) {
678  target_addr_t tar;
679  if (mem_ap_read_tar(ap, &tar) == ERROR_OK) {
680  /* TAR is incremented after failed transfer on some devices (eg Cortex-M4) */
681  LOG_ERROR("Failed to read memory at " TARGET_ADDR_FMT, tar);
682  if (nbytes > tar - address)
683  nbytes = tar - address;
684  } else {
685  LOG_ERROR("Failed to read memory and, additionally, failed to find out where");
686  nbytes = 0;
687  }
688  }
689 
690  target_addr_t ti_be_lane_xor = dap->ti_be_32_quirks ? 3 : 0;
691 
692  /* Replay loop to populate caller's buffer from the correct word and byte lane */
693  while (nbytes > 0) {
694  /* Convert transfers longer than 32-bit on word-at-a-time basis */
695  unsigned int this_size = MIN(size, 4);
696 
697  if (size < 4 && addrinc && ap->packed_transfers_supported && nbytes >= 4
698  && max_tar_block_size(ap->tar_autoincr_block, address) >= 4) {
699  this_size = 4; /* Packed read of 4 bytes or 2 halfwords */
700  }
701 
702  switch (this_size) {
703  case 4:
704  *buffer++ = *read_ptr >> 8 * ((address++ & 3) ^ ti_be_lane_xor);
705  *buffer++ = *read_ptr >> 8 * ((address++ & 3) ^ ti_be_lane_xor);
706  /* fallthrough */
707  case 2:
708  *buffer++ = *read_ptr >> 8 * ((address++ & 3) ^ ti_be_lane_xor);
709  /* fallthrough */
710  case 1:
711  *buffer++ = *read_ptr >> 8 * ((address++ & 3) ^ ti_be_lane_xor);
712  }
713 
714  read_ptr++;
715  nbytes -= this_size;
716  }
717 
718  free(read_buf);
719  return retval;
720 }
721 
723  uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
724 {
725  return mem_ap_read(ap, buffer, size, count, address, true);
726 }
727 
729  const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
730 {
731  return mem_ap_write(ap, buffer, size, count, address, true);
732 }
733 
735  uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
736 {
737  return mem_ap_read(ap, buffer, size, count, address, false);
738 }
739 
741  const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
742 {
743  return mem_ap_write(ap, buffer, size, count, address, false);
744 }
745 
746 /*--------------------------------------------------------------------------*/
747 
748 
749 #define DAP_POWER_DOMAIN_TIMEOUT (10)
750 
751 /*--------------------------------------------------------------------------*/
752 
757 {
758  dap->select = 0; /* speculate the first AP access will select AP 0, bank 0 */
759  dap->select_valid = false;
760  dap->select1_valid = false;
761  dap->select_dpbanksel_valid = false;
762 
763  dap->last_read = NULL;
764 
765  int i;
766  for (i = 0; i <= DP_APSEL_MAX; i++) {
767  /* force csw and tar write on the next mem-ap access */
768  dap->ap[i].tar_valid = false;
769  dap->ap[i].csw_value = 0;
770  }
771 }
772 
779 int dap_dp_init(struct adiv5_dap *dap)
780 {
781  int retval;
782 
783  LOG_DEBUG("%s", adiv5_dap_name(dap));
784 
785  dap->do_reconnect = false;
787 
788  /*
789  * Early initialize dap->dp_ctrl_stat.
790  * In jtag mode only, if the following queue run (in dap_dp_poll_register)
791  * fails and sets the sticky error, it will trigger the clearing
792  * of the sticky. Without this initialization system and debug power
793  * would be disabled while clearing the sticky error bit.
794  */
796 
797  /*
798  * This write operation clears the sticky error and overrun bits in jtag
799  * mode only and is ignored in swd mode. It also powers-up system and
800  * debug domains in both jtag and swd modes, if not done before.
801  */
802  retval = dap_queue_dp_write(dap, DP_CTRL_STAT,
804  if (retval != ERROR_OK)
805  return retval;
806 
807  retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
808  if (retval != ERROR_OK)
809  return retval;
810 
811  retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
812  if (retval != ERROR_OK)
813  return retval;
814 
815  /* Check that we have debug power domains activated */
816  LOG_DEBUG("DAP: wait CDBGPWRUPACK");
817  retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
820  if (retval != ERROR_OK)
821  return retval;
822 
823  if (!dap->ignore_syspwrupack) {
824  LOG_DEBUG("DAP: wait CSYSPWRUPACK");
825  retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
828  if (retval != ERROR_OK)
829  return retval;
830  }
831 
832  retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
833  if (retval != ERROR_OK)
834  return retval;
835 
836  /* With debug power on we can activate OVERRUN checking */
838  retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
839  if (retval != ERROR_OK)
840  return retval;
841  retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
842  if (retval != ERROR_OK)
843  return retval;
844 
845  retval = dap_run(dap);
846  if (retval != ERROR_OK)
847  return retval;
848 
849  return retval;
850 }
851 
858 {
859  LOG_DEBUG("%s", adiv5_dap_name(dap));
860 
861  /*
862  * Early initialize dap->dp_ctrl_stat.
863  * In jtag mode only, if the following atomic reads fail and set the
864  * sticky error, it will trigger the clearing of the sticky. Without this
865  * initialization system and debug power would be disabled while clearing
866  * the sticky error bit.
867  */
869 
870  dap->do_reconnect = false;
871 
873  if (dap->do_reconnect) {
874  /* dap connect calls dap_dp_init() after transport dependent initialization */
875  return dap->ops->connect(dap);
876  } else {
877  return dap_dp_init(dap);
878  }
879 }
880 
888 int mem_ap_init(struct adiv5_ap *ap)
889 {
890  /* check that we support packed transfers */
891  uint32_t cfg;
892  int retval;
893  struct adiv5_dap *dap = ap->dap;
894 
895  /* Set ap->cfg_reg before calling mem_ap_setup_transfer(). */
896  /* mem_ap_setup_transfer() needs to know if the MEM_AP supports LPAE. */
897  retval = dap_queue_ap_read(ap, MEM_AP_REG_CFG(dap), &cfg);
898  if (retval != ERROR_OK)
899  return retval;
900 
901  retval = dap_run(dap);
902  if (retval != ERROR_OK)
903  return retval;
904 
905  ap->cfg_reg = cfg;
906  ap->tar_valid = false;
907  ap->csw_value = 0; /* force csw and tar write */
908 
909  /* CSW 32-bit size must be supported (IHI 0031F and 0074D). */
912 
913  /* Suppress probing sizes longer than 32 bit if AP has no large data extension */
914  if (!(cfg & MEM_AP_REG_CFG_LD))
916 
917  /* Both IHI 0031F and 0074D state: Implementations that support transfers
918  * smaller than a word must support packed transfers. Unfortunately at least
919  * Cortex-M0 and Cortex-M0+ do not comply with this rule.
920  * Probe for packed transfers except we know they are broken.
921  * Packed transfers on TI BE-32 processors do not work correctly in
922  * many cases. */
925 
926  /* The ARM ADI spec leaves implementation-defined whether unaligned
927  * memory accesses work, only work partially, or cause a sticky error.
928  * On TI BE-32 processors, reads seem to return garbage in some bytes
929  * and unaligned writes seem to cause a sticky error.
930  * TODO: it would be nice to have a way to detect whether unaligned
931  * operations are supported on other processors. */
933 
934  LOG_DEBUG("MEM_AP CFG: large data %d, long address %d, big-endian %d",
935  !!(cfg & MEM_AP_REG_CFG_LD), !!(cfg & MEM_AP_REG_CFG_LA), !!(cfg & MEM_AP_REG_CFG_BE));
936 
937  return ERROR_OK;
938 }
939 
952 int dap_to_swd(struct adiv5_dap *dap)
953 {
954  LOG_DEBUG("Enter SWD mode");
955 
956  return dap_send_sequence(dap, JTAG_TO_SWD);
957 }
958 
970 int dap_to_jtag(struct adiv5_dap *dap)
971 {
972  LOG_DEBUG("Enter JTAG mode");
973 
974  return dap_send_sequence(dap, SWD_TO_JTAG);
975 }
976 
977 /* CID interpretation -- see ARM IHI 0029E table B2-7
978  * and ARM IHI 0031E table D1-2.
979  *
980  * From 2009/11/25 commit 21378f58b604:
981  * "OptimoDE DESS" is ARM's semicustom DSPish stuff.
982  * Let's keep it as is, for the time being
983  */
984 static const char *class_description[16] = {
985  [0x0] = "Generic verification component",
986  [0x1] = "ROM table",
987  [0x2] = "Reserved",
988  [0x3] = "Reserved",
989  [0x4] = "Reserved",
990  [0x5] = "Reserved",
991  [0x6] = "Reserved",
992  [0x7] = "Reserved",
993  [0x8] = "Reserved",
994  [0x9] = "CoreSight component",
995  [0xA] = "Reserved",
996  [0xB] = "Peripheral Test Block",
997  [0xC] = "Reserved",
998  [0xD] = "OptimoDE DESS", /* see above */
999  [0xE] = "Generic IP component",
1000  [0xF] = "CoreLink, PrimeCell or System component",
1001 };
1002 
1003 #define ARCH_ID(architect, archid) ( \
1004  (((architect) << ARM_CS_C9_DEVARCH_ARCHITECT_SHIFT) & ARM_CS_C9_DEVARCH_ARCHITECT_MASK) | \
1005  (((archid) << ARM_CS_C9_DEVARCH_ARCHID_SHIFT) & ARM_CS_C9_DEVARCH_ARCHID_MASK) \
1006 )
1007 
1008 static const struct {
1009  uint32_t arch_id;
1010  const char *description;
1011 } class0x9_devarch[] = {
1012  /* keep same unsorted order as in ARM IHI0029E */
1013  { ARCH_ID(ARM_ID, 0x0A00), "RAS architecture" },
1014  { ARCH_ID(ARM_ID, 0x1A01), "Instrumentation Trace Macrocell (ITM) architecture" },
1015  { ARCH_ID(ARM_ID, 0x1A02), "DWT architecture" },
1016  { ARCH_ID(ARM_ID, 0x1A03), "Flash Patch and Breakpoint unit (FPB) architecture" },
1017  { ARCH_ID(ARM_ID, 0x2A04), "Processor debug architecture (ARMv8-M)" },
1018  { ARCH_ID(ARM_ID, 0x6A05), "Processor debug architecture (ARMv8-R)" },
1019  { ARCH_ID(ARM_ID, 0x0A10), "PC sample-based profiling" },
1020  { ARCH_ID(ARM_ID, 0x4A13), "Embedded Trace Macrocell (ETM) architecture" },
1021  { ARCH_ID(ARM_ID, 0x1A14), "Cross Trigger Interface (CTI) architecture" },
1022  { ARCH_ID(ARM_ID, 0x6A15), "Processor debug architecture (v8.0-A)" },
1023  { ARCH_ID(ARM_ID, 0x7A15), "Processor debug architecture (v8.1-A)" },
1024  { ARCH_ID(ARM_ID, 0x8A15), "Processor debug architecture (v8.2-A)" },
1025  { ARCH_ID(ARM_ID, 0x2A16), "Processor Performance Monitor (PMU) architecture" },
1026  { ARCH_ID(ARM_ID, 0x0A17), "Memory Access Port v2 architecture" },
1027  { ARCH_ID(ARM_ID, 0x0A27), "JTAG Access Port v2 architecture" },
1028  { ARCH_ID(ARM_ID, 0x0A31), "Basic trace router" },
1029  { ARCH_ID(ARM_ID, 0x0A37), "Power requestor" },
1030  { ARCH_ID(ARM_ID, 0x0A47), "Unknown Access Port v2 architecture" },
1031  { ARCH_ID(ARM_ID, 0x0A50), "HSSTP architecture" },
1032  { ARCH_ID(ARM_ID, 0x0A63), "System Trace Macrocell (STM) architecture" },
1033  { ARCH_ID(ARM_ID, 0x0A75), "CoreSight ELA architecture" },
1034  { ARCH_ID(ARM_ID, 0x0AF7), "CoreSight ROM architecture" },
1035 };
1036 
1037 #define DEVARCH_ID_MASK (ARM_CS_C9_DEVARCH_ARCHITECT_MASK | ARM_CS_C9_DEVARCH_ARCHID_MASK)
1038 #define DEVARCH_MEM_AP ARCH_ID(ARM_ID, 0x0A17)
1039 #define DEVARCH_ROM_C_0X9 ARCH_ID(ARM_ID, 0x0AF7)
1040 #define DEVARCH_UNKNOWN_V2 ARCH_ID(ARM_ID, 0x0A47)
1041 
1042 static const char *class0x9_devarch_description(uint32_t devarch)
1043 {
1044  if (!(devarch & ARM_CS_C9_DEVARCH_PRESENT))
1045  return "not present";
1046 
1047  for (unsigned int i = 0; i < ARRAY_SIZE(class0x9_devarch); i++)
1048  if ((devarch & DEVARCH_ID_MASK) == class0x9_devarch[i].arch_id)
1049  return class0x9_devarch[i].description;
1050 
1051  return "unknown";
1052 }
1053 
1054 static const struct {
1055  enum ap_type type;
1056  const char *description;
1057 } ap_types[] = {
1058  { AP_TYPE_JTAG_AP, "JTAG-AP" },
1059  { AP_TYPE_COM_AP, "COM-AP" },
1060  { AP_TYPE_AHB3_AP, "MEM-AP AHB3" },
1061  { AP_TYPE_APB_AP, "MEM-AP APB2 or APB3" },
1062  { AP_TYPE_AXI_AP, "MEM-AP AXI3 or AXI4" },
1063  { AP_TYPE_AHB5_AP, "MEM-AP AHB5" },
1064  { AP_TYPE_APB4_AP, "MEM-AP APB4" },
1065  { AP_TYPE_AXI5_AP, "MEM-AP AXI5" },
1066  { AP_TYPE_AHB5H_AP, "MEM-AP AHB5 with enhanced HPROT" },
1067 };
1068 
1069 static const char *ap_type_to_description(enum ap_type type)
1070 {
1071  for (unsigned int i = 0; i < ARRAY_SIZE(ap_types); i++)
1072  if (type == ap_types[i].type)
1073  return ap_types[i].description;
1074 
1075  return "Unknown";
1076 }
1077 
1078 bool is_ap_num_valid(struct adiv5_dap *dap, uint64_t ap_num)
1079 {
1080  if (!dap)
1081  return false;
1082 
1083  /* no autodetection, by now, so uninitialized is equivalent to ADIv5 for
1084  * backward compatibility */
1085  if (!is_adiv6(dap)) {
1086  if (ap_num > DP_APSEL_MAX)
1087  return false;
1088  return true;
1089  }
1090 
1091  if (is_adiv6(dap)) {
1092  if (ap_num & 0x0fffULL)
1093  return false;
1094  if (dap->asize != 0)
1095  if (ap_num & ((~0ULL) << dap->asize))
1096  return false;
1097  return true;
1098  }
1099 
1100  return false;
1101 }
1102 
1103 /*
1104  * This function checks the ID for each access port to find the requested Access Port type
1105  * It also calls dap_get_ap() to increment the AP refcount
1106  */
1107 int dap_find_get_ap(struct adiv5_dap *dap, enum ap_type type_to_find, struct adiv5_ap **ap_out)
1108 {
1109  if (is_adiv6(dap)) {
1110  /* TODO: scan the ROM table and detect the AP available */
1111  LOG_DEBUG("On ADIv6 we cannot scan all the possible AP");
1112  return ERROR_FAIL;
1113  }
1114 
1115  /* Maximum AP number is 255 since the SELECT register is 8 bits */
1116  for (unsigned int ap_num = 0; ap_num <= DP_APSEL_MAX; ap_num++) {
1117  struct adiv5_ap *ap = dap_get_ap(dap, ap_num);
1118  if (!ap)
1119  continue;
1120 
1121  /* read the IDR register of the Access Port */
1122  uint32_t id_val = 0;
1123 
1124  int retval = dap_queue_ap_read(ap, AP_REG_IDR(dap), &id_val);
1125  if (retval != ERROR_OK) {
1126  dap_put_ap(ap);
1127  return retval;
1128  }
1129 
1130  retval = dap_run(dap);
1131 
1132  /* Reading register for a non-existent AP should not cause an error,
1133  * but just to be sure, try to continue searching if an error does happen.
1134  */
1135  if (retval == ERROR_OK && (id_val & AP_TYPE_MASK) == type_to_find) {
1136  LOG_DEBUG("Found %s at AP index: %d (IDR=0x%08" PRIX32 ")",
1137  ap_type_to_description(type_to_find),
1138  ap_num, id_val);
1139 
1140  *ap_out = ap;
1141  return ERROR_OK;
1142  }
1143  dap_put_ap(ap);
1144  }
1145 
1146  LOG_DEBUG("No %s found", ap_type_to_description(type_to_find));
1147  return ERROR_FAIL;
1148 }
1149 
1150 static inline bool is_ap_in_use(struct adiv5_ap *ap)
1151 {
1152  return ap->refcount > 0 || ap->config_ap_never_release;
1153 }
1154 
1155 static struct adiv5_ap *_dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
1156 {
1157  if (!is_ap_num_valid(dap, ap_num)) {
1158  LOG_ERROR("Invalid AP#0x%" PRIx64, ap_num);
1159  return NULL;
1160  }
1161  if (is_adiv6(dap)) {
1162  for (unsigned int i = 0; i <= DP_APSEL_MAX; i++) {
1163  struct adiv5_ap *ap = &dap->ap[i];
1164  if (is_ap_in_use(ap) && ap->ap_num == ap_num) {
1165  ++ap->refcount;
1166  return ap;
1167  }
1168  }
1169  for (unsigned int i = 0; i <= DP_APSEL_MAX; i++) {
1170  struct adiv5_ap *ap = &dap->ap[i];
1171  if (!is_ap_in_use(ap)) {
1172  ap->ap_num = ap_num;
1173  ++ap->refcount;
1174  return ap;
1175  }
1176  }
1177  LOG_ERROR("No more AP available!");
1178  return NULL;
1179  }
1180 
1181  /* ADIv5 */
1182  struct adiv5_ap *ap = &dap->ap[ap_num];
1183  ap->ap_num = ap_num;
1184  ++ap->refcount;
1185  return ap;
1186 }
1187 
1188 /* Return AP with specified ap_num. Increment AP refcount */
1189 struct adiv5_ap *dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
1190 {
1191  struct adiv5_ap *ap = _dap_get_ap(dap, ap_num);
1192  if (ap)
1193  LOG_DEBUG("refcount AP#0x%" PRIx64 " get %u", ap_num, ap->refcount);
1194  return ap;
1195 }
1196 
1197 /* Return AP with specified ap_num. Increment AP refcount and keep it non-zero */
1198 struct adiv5_ap *dap_get_config_ap(struct adiv5_dap *dap, uint64_t ap_num)
1199 {
1200  struct adiv5_ap *ap = _dap_get_ap(dap, ap_num);
1201  if (ap) {
1202  ap->config_ap_never_release = true;
1203  LOG_DEBUG("refcount AP#0x%" PRIx64 " get_config %u", ap_num, ap->refcount);
1204  }
1205  return ap;
1206 }
1207 
1208 /* Decrement AP refcount and release the AP when refcount reaches zero */
1209 int dap_put_ap(struct adiv5_ap *ap)
1210 {
1211  if (ap->refcount == 0) {
1212  LOG_ERROR("BUG: refcount AP#0x%" PRIx64 " put underflow", ap->ap_num);
1213  return ERROR_FAIL;
1214  }
1215 
1216  --ap->refcount;
1217 
1218  LOG_DEBUG("refcount AP#0x%" PRIx64 " put %u", ap->ap_num, ap->refcount);
1219  if (!is_ap_in_use(ap)) {
1220  /* defaults from dap_instance_init() */
1221  ap->ap_num = DP_APSEL_INVALID;
1222  ap->memaccess_tck = 255;
1223  ap->tar_autoincr_block = (1 << 10);
1226  }
1227  return ERROR_OK;
1228 }
1229 
1230 static int dap_get_debugbase(struct adiv5_ap *ap,
1231  target_addr_t *dbgbase, uint32_t *apid)
1232 {
1233  struct adiv5_dap *dap = ap->dap;
1234  int retval;
1235  uint32_t baseptr_upper, baseptr_lower;
1236 
1237  if (ap->cfg_reg == MEM_AP_REG_CFG_INVALID) {
1238  retval = dap_queue_ap_read(ap, MEM_AP_REG_CFG(dap), &ap->cfg_reg);
1239  if (retval != ERROR_OK)
1240  return retval;
1241  }
1242  retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE(dap), &baseptr_lower);
1243  if (retval != ERROR_OK)
1244  return retval;
1245  retval = dap_queue_ap_read(ap, AP_REG_IDR(dap), apid);
1246  if (retval != ERROR_OK)
1247  return retval;
1248  /* MEM_AP_REG_BASE64 is defined as 'RES0'; can be read and then ignored on 32 bits AP */
1250  retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE64(dap), &baseptr_upper);
1251  if (retval != ERROR_OK)
1252  return retval;
1253  }
1254 
1255  retval = dap_run(dap);
1256  if (retval != ERROR_OK)
1257  return retval;
1258 
1259  if (!is_64bit_ap(ap))
1260  baseptr_upper = 0;
1261  *dbgbase = (((target_addr_t)baseptr_upper) << 32) | baseptr_lower;
1262 
1263  return ERROR_OK;
1264 }
1265 
1266 int adiv6_dap_read_baseptr(struct command_invocation *cmd, struct adiv5_dap *dap, uint64_t *baseptr)
1267 {
1268  uint32_t baseptr_lower, baseptr_upper = 0;
1269  int retval;
1270 
1271  if (dap->asize > 32) {
1272  retval = dap_queue_dp_read(dap, DP_BASEPTR1, &baseptr_upper);
1273  if (retval != ERROR_OK)
1274  return retval;
1275  }
1276 
1277  retval = dap_dp_read_atomic(dap, DP_BASEPTR0, &baseptr_lower);
1278  if (retval != ERROR_OK)
1279  return retval;
1280 
1281  if ((baseptr_lower & DP_BASEPTR0_VALID) != DP_BASEPTR0_VALID) {
1282  command_print(cmd, "System root table not present");
1283  return ERROR_FAIL;
1284  }
1285 
1286  baseptr_lower &= ~0x0fff;
1287  *baseptr = (((uint64_t)baseptr_upper) << 32) | baseptr_lower;
1288 
1289  return ERROR_OK;
1290 }
1291 
1301 };
1302 
1305  struct adiv5_ap *ap;
1307  uint64_t pid;
1308  uint32_t cid;
1309  uint32_t devarch;
1310  uint32_t devid;
1313 };
1314 
1328  uint64_t component_base, unsigned int reg, uint32_t *value)
1329 {
1330  if (mode == CS_ACCESS_AP)
1331  return dap_queue_ap_read(ap, reg, value);
1332 
1333  /* mode == CS_ACCESS_MEM_AP */
1334  return mem_ap_read_u32(ap, component_base + reg, value);
1335 }
1336 
1348  target_addr_t component_base, struct cs_component_vals *v)
1349 {
1350  assert(IS_ALIGNED(component_base, ARM_CS_ALIGN));
1351  assert(ap && v);
1352 
1353  uint32_t cid0, cid1, cid2, cid3;
1354  uint32_t pid0, pid1, pid2, pid3, pid4;
1355  int retval = ERROR_OK;
1356 
1357  v->ap = ap;
1358  v->component_base = component_base;
1359  v->mode = mode;
1360 
1361  /* sort by offset to gain speed */
1362 
1363  /*
1364  * Registers DEVARCH, DEVID and DEVTYPE are valid on Class 0x9 devices
1365  * only, but are at offset above 0xf00, so can be read on any device
1366  * without triggering error. Read them for eventual use on Class 0x9.
1367  */
1368  if (retval == ERROR_OK)
1369  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_C9_DEVARCH, &v->devarch);
1370 
1371  if (retval == ERROR_OK)
1372  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_C9_DEVID, &v->devid);
1373 
1374  /* Same address as ARM_CS_C1_MEMTYPE */
1375  if (retval == ERROR_OK)
1376  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_C9_DEVTYPE, &v->devtype_memtype);
1377 
1378  if (retval == ERROR_OK)
1379  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_PIDR4, &pid4);
1380 
1381  if (retval == ERROR_OK)
1382  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_PIDR0, &pid0);
1383  if (retval == ERROR_OK)
1384  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_PIDR1, &pid1);
1385  if (retval == ERROR_OK)
1386  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_PIDR2, &pid2);
1387  if (retval == ERROR_OK)
1388  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_PIDR3, &pid3);
1389 
1390  if (retval == ERROR_OK)
1391  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_CIDR0, &cid0);
1392  if (retval == ERROR_OK)
1393  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_CIDR1, &cid1);
1394  if (retval == ERROR_OK)
1395  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_CIDR2, &cid2);
1396  if (retval == ERROR_OK)
1397  retval = dap_queue_read_reg(mode, ap, component_base, ARM_CS_CIDR3, &cid3);
1398 
1399  if (retval == ERROR_OK)
1400  retval = dap_run(ap->dap);
1401  if (retval != ERROR_OK) {
1402  LOG_DEBUG("Failed read CoreSight registers");
1403  return retval;
1404  }
1405 
1406  v->cid = (cid3 & 0xff) << 24
1407  | (cid2 & 0xff) << 16
1408  | (cid1 & 0xff) << 8
1409  | (cid0 & 0xff);
1410  v->pid = (uint64_t)(pid4 & 0xff) << 32
1411  | (pid3 & 0xff) << 24
1412  | (pid2 & 0xff) << 16
1413  | (pid1 & 0xff) << 8
1414  | (pid0 & 0xff);
1415 
1416  return ERROR_OK;
1417 }
1418 
1419 /* Part number interpretations are from Cortex
1420  * core specs, the CoreSight components TRM
1421  * (ARM DDI 0314H), CoreSight System Design
1422  * Guide (ARM DGI 0012D) and ETM specs; also
1423  * from chip observation (e.g. TI SDTI).
1424  */
1425 
1426 static const struct dap_part_nums {
1427  uint16_t designer_id;
1428  uint16_t part_num;
1429  const char *type;
1430  const char *full;
1431 } dap_part_nums[] = {
1432  { ARM_ID, 0x000, "Cortex-M3 SCS", "(System Control Space)", },
1433  { ARM_ID, 0x001, "Cortex-M3 ITM", "(Instrumentation Trace Module)", },
1434  { ARM_ID, 0x002, "Cortex-M3 DWT", "(Data Watchpoint and Trace)", },
1435  { ARM_ID, 0x003, "Cortex-M3 FPB", "(Flash Patch and Breakpoint)", },
1436  { ARM_ID, 0x008, "Cortex-M0 SCS", "(System Control Space)", },
1437  { ARM_ID, 0x00a, "Cortex-M0 DWT", "(Data Watchpoint and Trace)", },
1438  { ARM_ID, 0x00b, "Cortex-M0 BPU", "(Breakpoint Unit)", },
1439  { ARM_ID, 0x00c, "Cortex-M4 SCS", "(System Control Space)", },
1440  { ARM_ID, 0x00d, "CoreSight ETM11", "(Embedded Trace)", },
1441  { ARM_ID, 0x00e, "Cortex-M7 FPB", "(Flash Patch and Breakpoint)", },
1442  { ARM_ID, 0x193, "SoC-600 TSGEN", "(Timestamp Generator)", },
1443  { ARM_ID, 0x470, "Cortex-M1 ROM", "(ROM Table)", },
1444  { ARM_ID, 0x471, "Cortex-M0 ROM", "(ROM Table)", },
1445  { ARM_ID, 0x490, "Cortex-A15 GIC", "(Generic Interrupt Controller)", },
1446  { ARM_ID, 0x492, "Cortex-R52 GICD", "(Distributor)", },
1447  { ARM_ID, 0x493, "Cortex-R52 GICR", "(Redistributor)", },
1448  { ARM_ID, 0x4a1, "Cortex-A53 ROM", "(v8 Memory Map ROM Table)", },
1449  { ARM_ID, 0x4a2, "Cortex-A57 ROM", "(ROM Table)", },
1450  { ARM_ID, 0x4a3, "Cortex-A53 ROM", "(v7 Memory Map ROM Table)", },
1451  { ARM_ID, 0x4a4, "Cortex-A72 ROM", "(ROM Table)", },
1452  { ARM_ID, 0x4a9, "Cortex-A9 ROM", "(ROM Table)", },
1453  { ARM_ID, 0x4aa, "Cortex-A35 ROM", "(v8 Memory Map ROM Table)", },
1454  { ARM_ID, 0x4af, "Cortex-A15 ROM", "(ROM Table)", },
1455  { ARM_ID, 0x4b5, "Cortex-R5 ROM", "(ROM Table)", },
1456  { ARM_ID, 0x4b8, "Cortex-R52 ROM", "(ROM Table)", },
1457  { ARM_ID, 0x4bd, "Cortex-R52+ ROM", "(ROM Table)", },
1458  { ARM_ID, 0x4c0, "Cortex-M0+ ROM", "(ROM Table)", },
1459  { ARM_ID, 0x4c3, "Cortex-M3 ROM", "(ROM Table)", },
1460  { ARM_ID, 0x4c4, "Cortex-M4 ROM", "(ROM Table)", },
1461  { ARM_ID, 0x4c7, "Cortex-M7 PPB ROM", "(Private Peripheral Bus ROM Table)", },
1462  { ARM_ID, 0x4c8, "Cortex-M7 ROM", "(ROM Table)", },
1463  { ARM_ID, 0x4c9, "STAR ROM", "(ROM Table)", },
1464  { ARM_ID, 0x4e0, "Cortex-A35 ROM", "(v7 Memory Map ROM Table)", },
1465  { ARM_ID, 0x4e4, "Cortex-A76 ROM", "(ROM Table)", },
1466  { ARM_ID, 0x906, "CoreSight CTI", "(Cross Trigger)", },
1467  { ARM_ID, 0x907, "CoreSight ETB", "(Trace Buffer)", },
1468  { ARM_ID, 0x908, "CoreSight CSTF", "(Trace Funnel)", },
1469  { ARM_ID, 0x909, "CoreSight ATBR", "(Advanced Trace Bus Replicator)", },
1470  { ARM_ID, 0x910, "CoreSight ETM9", "(Embedded Trace)", },
1471  { ARM_ID, 0x912, "CoreSight TPIU", "(Trace Port Interface Unit)", },
1472  { ARM_ID, 0x913, "CoreSight ITM", "(Instrumentation Trace Macrocell)", },
1473  { ARM_ID, 0x914, "CoreSight SWO", "(Single Wire Output)", },
1474  { ARM_ID, 0x917, "CoreSight HTM", "(AHB Trace Macrocell)", },
1475  { ARM_ID, 0x920, "CoreSight ETM11", "(Embedded Trace)", },
1476  { ARM_ID, 0x921, "Cortex-A8 ETM", "(Embedded Trace)", },
1477  { ARM_ID, 0x922, "Cortex-A8 CTI", "(Cross Trigger)", },
1478  { ARM_ID, 0x923, "Cortex-M3 TPIU", "(Trace Port Interface Unit)", },
1479  { ARM_ID, 0x924, "Cortex-M3 ETM", "(Embedded Trace)", },
1480  { ARM_ID, 0x925, "Cortex-M4 ETM", "(Embedded Trace)", },
1481  { ARM_ID, 0x930, "Cortex-R4 ETM", "(Embedded Trace)", },
1482  { ARM_ID, 0x931, "Cortex-R5 ETM", "(Embedded Trace)", },
1483  { ARM_ID, 0x932, "CoreSight MTB-M0+", "(Micro Trace Buffer)", },
1484  { ARM_ID, 0x941, "CoreSight TPIU-Lite", "(Trace Port Interface Unit)", },
1485  { ARM_ID, 0x950, "Cortex-A9 PTM", "(Program Trace Macrocell)", },
1486  { ARM_ID, 0x955, "Cortex-A5 ETM", "(Embedded Trace)", },
1487  { ARM_ID, 0x95a, "Cortex-A72 ETM", "(Embedded Trace)", },
1488  { ARM_ID, 0x95b, "Cortex-A17 PTM", "(Program Trace Macrocell)", },
1489  { ARM_ID, 0x95d, "Cortex-A53 ETM", "(Embedded Trace)", },
1490  { ARM_ID, 0x95e, "Cortex-A57 ETM", "(Embedded Trace)", },
1491  { ARM_ID, 0x95f, "Cortex-A15 PTM", "(Program Trace Macrocell)", },
1492  { ARM_ID, 0x961, "CoreSight TMC", "(Trace Memory Controller)", },
1493  { ARM_ID, 0x962, "CoreSight STM", "(System Trace Macrocell)", },
1494  { ARM_ID, 0x975, "Cortex-M7 ETM", "(Embedded Trace)", },
1495  { ARM_ID, 0x9a0, "CoreSight PMU", "(Performance Monitoring Unit)", },
1496  { ARM_ID, 0x9a1, "Cortex-M4 TPIU", "(Trace Port Interface Unit)", },
1497  { ARM_ID, 0x9a4, "CoreSight GPR", "(Granular Power Requester)", },
1498  { ARM_ID, 0x9a5, "Cortex-A5 PMU", "(Performance Monitor Unit)", },
1499  { ARM_ID, 0x9a7, "Cortex-A7 PMU", "(Performance Monitor Unit)", },
1500  { ARM_ID, 0x9a8, "Cortex-A53 CTI", "(Cross Trigger)", },
1501  { ARM_ID, 0x9a9, "Cortex-M7 TPIU", "(Trace Port Interface Unit)", },
1502  { ARM_ID, 0x9ae, "Cortex-A17 PMU", "(Performance Monitor Unit)", },
1503  { ARM_ID, 0x9af, "Cortex-A15 PMU", "(Performance Monitor Unit)", },
1504  { ARM_ID, 0x9b6, "Cortex-R52 PMU/CTI/ETM", "(Performance Monitor Unit/Cross Trigger/ETM)", },
1505  { ARM_ID, 0x9bb, "Cortex-R52+ PMU/CTI/ETM", "(Performance Monitor Unit/Cross Trigger/ETM)", },
1506  { ARM_ID, 0x9b7, "Cortex-R7 PMU", "(Performance Monitor Unit)", },
1507  { ARM_ID, 0x9d3, "Cortex-A53 PMU", "(Performance Monitor Unit)", },
1508  { ARM_ID, 0x9d7, "Cortex-A57 PMU", "(Performance Monitor Unit)", },
1509  { ARM_ID, 0x9d8, "Cortex-A72 PMU", "(Performance Monitor Unit)", },
1510  { ARM_ID, 0x9da, "Cortex-A35 PMU/CTI/ETM", "(Performance Monitor Unit/Cross Trigger/ETM)", },
1511  { ARM_ID, 0x9e2, "SoC-600 APB-AP", "(APB4 Memory Access Port)", },
1512  { ARM_ID, 0x9e3, "SoC-600 AHB-AP", "(AHB5 Memory Access Port)", },
1513  { ARM_ID, 0x9e4, "SoC-600 AXI-AP", "(AXI Memory Access Port)", },
1514  { ARM_ID, 0x9e5, "SoC-600 APv1 Adapter", "(Access Port v1 Adapter)", },
1515  { ARM_ID, 0x9e6, "SoC-600 JTAG-AP", "(JTAG Access Port)", },
1516  { ARM_ID, 0x9e7, "SoC-600 TPIU", "(Trace Port Interface Unit)", },
1517  { ARM_ID, 0x9e8, "SoC-600 TMC ETR/ETS", "(Embedded Trace Router/Streamer)", },
1518  { ARM_ID, 0x9e9, "SoC-600 TMC ETB", "(Embedded Trace Buffer)", },
1519  { ARM_ID, 0x9ea, "SoC-600 TMC ETF", "(Embedded Trace FIFO)", },
1520  { ARM_ID, 0x9eb, "SoC-600 ATB Funnel", "(Trace Funnel)", },
1521  { ARM_ID, 0x9ec, "SoC-600 ATB Replicator", "(Trace Replicator)", },
1522  { ARM_ID, 0x9ed, "SoC-600 CTI", "(Cross Trigger)", },
1523  { ARM_ID, 0x9ee, "SoC-600 CATU", "(Address Translation Unit)", },
1524  { ARM_ID, 0xc05, "Cortex-A5 Debug", "(Debug Unit)", },
1525  { ARM_ID, 0xc07, "Cortex-A7 Debug", "(Debug Unit)", },
1526  { ARM_ID, 0xc08, "Cortex-A8 Debug", "(Debug Unit)", },
1527  { ARM_ID, 0xc09, "Cortex-A9 Debug", "(Debug Unit)", },
1528  { ARM_ID, 0xc0e, "Cortex-A17 Debug", "(Debug Unit)", },
1529  { ARM_ID, 0xc0f, "Cortex-A15 Debug", "(Debug Unit)", },
1530  { ARM_ID, 0xc14, "Cortex-R4 Debug", "(Debug Unit)", },
1531  { ARM_ID, 0xc15, "Cortex-R5 Debug", "(Debug Unit)", },
1532  { ARM_ID, 0xc17, "Cortex-R7 Debug", "(Debug Unit)", },
1533  { ARM_ID, 0xd03, "Cortex-A53 Debug", "(Debug Unit)", },
1534  { ARM_ID, 0xd04, "Cortex-A35 Debug", "(Debug Unit)", },
1535  { ARM_ID, 0xd05, "Cortex-A55 Debug", "(Debug Unit)", },
1536  { ARM_ID, 0xd07, "Cortex-A57 Debug", "(Debug Unit)", },
1537  { ARM_ID, 0xd08, "Cortex-A72 Debug", "(Debug Unit)", },
1538  { ARM_ID, 0xd0b, "Cortex-A76 Debug", "(Debug Unit)", },
1539  { ARM_ID, 0xd0c, "Neoverse N1", "(Debug Unit)", },
1540  { ARM_ID, 0xd13, "Cortex-R52 Debug", "(Debug Unit)", },
1541  { ARM_ID, 0xd16, "Cortex-R52+ Debug", "(Debug Unit)", },
1542  { ARM_ID, 0xd21, "STAR Debug", "(Debug Unit)", },
1543  { ARM_ID, 0xd22, "Cortex-M55 Debug", "(Debug Unit)", },
1544  { ARM_ID, 0xd43, "Cortex-A65AE Debug", "(Debug Unit)", },
1545  { ARM_ID, 0xd49, "Neoverse N2", "(Debug Unit)", },
1546  { 0x017, 0x120, "TI SDTI", "(System Debug Trace Interface)", }, /* from OMAP3 memmap */
1547  { 0x017, 0x343, "TI DAPCTL", "", }, /* from OMAP3 memmap */
1548  { 0x017, 0x9af, "MSP432 ROM", "(ROM Table)" },
1549  { 0x01f, 0xcd0, "Atmel CPU with DSU", "(CPU)" },
1550  { 0x041, 0x1db, "XMC4500 ROM", "(ROM Table)" },
1551  { 0x041, 0x1df, "XMC4700/4800 ROM", "(ROM Table)" },
1552  { 0x041, 0x1ed, "XMC1000 ROM", "(ROM Table)" },
1553  { 0x065, 0x000, "SHARC+/Blackfin+", "", },
1554  { 0x070, 0x440, "Qualcomm QDSS Component v1", "(Qualcomm Designed CoreSight Component v1)", },
1555  { 0x0bf, 0x100, "Brahma-B53 Debug", "(Debug Unit)", },
1556  { 0x0bf, 0x9d3, "Brahma-B53 PMU", "(Performance Monitor Unit)", },
1557  { 0x0bf, 0x4a1, "Brahma-B53 ROM", "(ROM Table)", },
1558  { 0x0bf, 0x721, "Brahma-B53 ROM", "(ROM Table)", },
1559  { 0x1eb, 0x181, "Tegra 186 ROM", "(ROM Table)", },
1560  { 0x1eb, 0x202, "Denver ETM", "(Denver Embedded Trace)", },
1561  { 0x1eb, 0x211, "Tegra 210 ROM", "(ROM Table)", },
1562  { 0x1eb, 0x302, "Denver Debug", "(Debug Unit)", },
1563  { 0x1eb, 0x402, "Denver PMU", "(Performance Monitor Unit)", },
1564  { 0x575, 0x132, "STAR SCS", "(System Control Space)", },
1565  { 0x575, 0x4d2, "Cortex-M52 ROM", "(ROM Table)", },
1566  { 0x575, 0xd24, "Cortex-M52 Debug", "(Debug Unit)", },
1567 };
1568 
1569 static const struct dap_part_nums *pidr_to_part_num(unsigned int designer_id, unsigned int part_num)
1570 {
1571  static const struct dap_part_nums unknown = {
1572  .type = "Unrecognized",
1573  .full = "",
1574  };
1575 
1576  for (unsigned int i = 0; i < ARRAY_SIZE(dap_part_nums); i++)
1578  return &dap_part_nums[i];
1579 
1580  return &unknown;
1581 }
1582 
1583 static int dap_devtype_display(struct command_invocation *cmd, uint32_t devtype)
1584 {
1585  const char *major = "Reserved", *subtype = "Reserved";
1586  const unsigned int minor = (devtype & ARM_CS_C9_DEVTYPE_SUB_MASK) >> ARM_CS_C9_DEVTYPE_SUB_SHIFT;
1587  const unsigned int devtype_major = (devtype & ARM_CS_C9_DEVTYPE_MAJOR_MASK) >> ARM_CS_C9_DEVTYPE_MAJOR_SHIFT;
1588  switch (devtype_major) {
1589  case 0:
1590  major = "Miscellaneous";
1591  switch (minor) {
1592  case 0:
1593  subtype = "other";
1594  break;
1595  case 4:
1596  subtype = "Validation component";
1597  break;
1598  }
1599  break;
1600  case 1:
1601  major = "Trace Sink";
1602  switch (minor) {
1603  case 0:
1604  subtype = "other";
1605  break;
1606  case 1:
1607  subtype = "Port";
1608  break;
1609  case 2:
1610  subtype = "Buffer";
1611  break;
1612  case 3:
1613  subtype = "Router";
1614  break;
1615  }
1616  break;
1617  case 2:
1618  major = "Trace Link";
1619  switch (minor) {
1620  case 0:
1621  subtype = "other";
1622  break;
1623  case 1:
1624  subtype = "Funnel, router";
1625  break;
1626  case 2:
1627  subtype = "Filter";
1628  break;
1629  case 3:
1630  subtype = "FIFO, buffer";
1631  break;
1632  }
1633  break;
1634  case 3:
1635  major = "Trace Source";
1636  switch (minor) {
1637  case 0:
1638  subtype = "other";
1639  break;
1640  case 1:
1641  subtype = "Processor";
1642  break;
1643  case 2:
1644  subtype = "DSP";
1645  break;
1646  case 3:
1647  subtype = "Engine/Coprocessor";
1648  break;
1649  case 4:
1650  subtype = "Bus";
1651  break;
1652  case 6:
1653  subtype = "Software";
1654  break;
1655  }
1656  break;
1657  case 4:
1658  major = "Debug Control";
1659  switch (minor) {
1660  case 0:
1661  subtype = "other";
1662  break;
1663  case 1:
1664  subtype = "Trigger Matrix";
1665  break;
1666  case 2:
1667  subtype = "Debug Auth";
1668  break;
1669  case 3:
1670  subtype = "Power Requestor";
1671  break;
1672  }
1673  break;
1674  case 5:
1675  major = "Debug Logic";
1676  switch (minor) {
1677  case 0:
1678  subtype = "other";
1679  break;
1680  case 1:
1681  subtype = "Processor";
1682  break;
1683  case 2:
1684  subtype = "DSP";
1685  break;
1686  case 3:
1687  subtype = "Engine/Coprocessor";
1688  break;
1689  case 4:
1690  subtype = "Bus";
1691  break;
1692  case 5:
1693  subtype = "Memory";
1694  break;
1695  }
1696  break;
1697  case 6:
1698  major = "Performance Monitor";
1699  switch (minor) {
1700  case 0:
1701  subtype = "other";
1702  break;
1703  case 1:
1704  subtype = "Processor";
1705  break;
1706  case 2:
1707  subtype = "DSP";
1708  break;
1709  case 3:
1710  subtype = "Engine/Coprocessor";
1711  break;
1712  case 4:
1713  subtype = "Bus";
1714  break;
1715  case 5:
1716  subtype = "Memory";
1717  break;
1718  }
1719  break;
1720  }
1721  command_print(cmd, "\t\tType is 0x%02x, %s, %s",
1722  devtype & ARM_CS_C9_DEVTYPE_MASK,
1723  major, subtype);
1724  return ERROR_OK;
1725 }
1726 
1730 struct rtp_ops {
1738  int (*ap_header)(struct adiv5_ap *ap, int depth, void *priv);
1749  int (*mem_ap_header)(int retval, struct adiv5_ap *ap, uint64_t dbgbase,
1750  uint32_t apid, int depth, void *priv);
1760  int (*cs_component)(int retval, struct cs_component_vals *v, int depth, void *priv);
1771  int (*rom_table_entry)(int retval, int depth, unsigned int offset, uint64_t romentry,
1772  void *priv);
1776  void *priv;
1777 };
1778 
1782 static int rtp_ops_ap_header(const struct rtp_ops *ops,
1783  struct adiv5_ap *ap, int depth)
1784 {
1785  if (ops->ap_header)
1786  return ops->ap_header(ap, depth, ops->priv);
1787 
1788  return ERROR_OK;
1789 }
1790 
1795 static int rtp_ops_mem_ap_header(const struct rtp_ops *ops,
1796  int retval, struct adiv5_ap *ap, uint64_t dbgbase, uint32_t apid, int depth)
1797 {
1798  if (!ops->mem_ap_header)
1799  return retval;
1800 
1801  int retval1 = ops->mem_ap_header(retval, ap, dbgbase, apid, depth, ops->priv);
1802  if (retval != ERROR_OK)
1803  return retval;
1804  return retval1;
1805 }
1806 
1811 static int rtp_ops_cs_component(const struct rtp_ops *ops,
1812  int retval, struct cs_component_vals *v, int depth)
1813 {
1814  if (!ops->cs_component)
1815  return retval;
1816 
1817  int retval1 = ops->cs_component(retval, v, depth, ops->priv);
1818  if (retval != ERROR_OK)
1819  return retval;
1820  return retval1;
1821 }
1822 
1827 static int rtp_ops_rom_table_entry(const struct rtp_ops *ops,
1828  int retval, int depth, unsigned int offset, uint64_t romentry)
1829 {
1830  if (!ops->rom_table_entry)
1831  return retval;
1832 
1833  int retval1 = ops->rom_table_entry(retval, depth, offset, romentry, ops->priv);
1834  if (retval != ERROR_OK)
1835  return retval;
1836  return retval1;
1837 }
1838 
1839 /* Broken ROM tables can have circular references. Stop after a while */
1840 #define ROM_TABLE_MAX_DEPTH (16)
1841 
1848 #define CORESIGHT_COMPONENT_FOUND (1)
1849 
1850 static int rtp_ap(const struct rtp_ops *ops, struct adiv5_ap *ap, int depth);
1851 static int rtp_cs_component(enum coresight_access_mode mode, const struct rtp_ops *ops,
1852  struct adiv5_ap *ap, target_addr_t dbgbase, bool *is_mem_ap, int depth);
1853 
1854 static int rtp_rom_loop(enum coresight_access_mode mode, const struct rtp_ops *ops,
1855  struct adiv5_ap *ap, target_addr_t base_address, int depth,
1856  unsigned int width, unsigned int max_entries)
1857 {
1858  /* ADIv6 AP ROM table provide offset from current AP */
1859  if (mode == CS_ACCESS_AP)
1860  base_address = ap->ap_num;
1861 
1862  assert(IS_ALIGNED(base_address, ARM_CS_ALIGN));
1863 
1864  unsigned int offset = 0;
1865  while (max_entries--) {
1866  uint64_t romentry;
1867  uint32_t romentry_low, romentry_high;
1869  unsigned int saved_offset = offset;
1870 
1871  int retval = dap_queue_read_reg(mode, ap, base_address, offset, &romentry_low);
1872  offset += 4;
1873  if (retval == ERROR_OK && width == 64) {
1874  retval = dap_queue_read_reg(mode, ap, base_address, offset, &romentry_high);
1875  offset += 4;
1876  }
1877  if (retval == ERROR_OK)
1878  retval = dap_run(ap->dap);
1879  if (retval != ERROR_OK) {
1880  LOG_DEBUG("Failed read ROM table entry");
1881  return retval;
1882  }
1883 
1884  if (width == 64) {
1885  romentry = (((uint64_t)romentry_high) << 32) | romentry_low;
1886  component_base = base_address +
1887  ((((uint64_t)romentry_high) << 32) | (romentry_low & ARM_CS_ROMENTRY_OFFSET_MASK));
1888  } else {
1889  romentry = romentry_low;
1890  /* "romentry" is signed */
1891  component_base = base_address + (int32_t)(romentry_low & ARM_CS_ROMENTRY_OFFSET_MASK);
1892  if (!is_64bit_ap(ap))
1893  component_base = (uint32_t)component_base;
1894  }
1895  retval = rtp_ops_rom_table_entry(ops, retval, depth, saved_offset, romentry);
1896  if (retval != ERROR_OK)
1897  return retval;
1898 
1899  if (romentry == 0) {
1900  /* End of ROM table */
1901  break;
1902  }
1903 
1904  if (!(romentry & ARM_CS_ROMENTRY_PRESENT))
1905  continue;
1906 
1907  /* Recurse */
1908  if (mode == CS_ACCESS_AP) {
1909  struct adiv5_ap *next_ap = dap_get_ap(ap->dap, component_base);
1910  if (!next_ap) {
1911  LOG_DEBUG("Wrong AP # 0x%" PRIx64, component_base);
1912  continue;
1913  }
1914  retval = rtp_ap(ops, next_ap, depth + 1);
1915  dap_put_ap(next_ap);
1916  } else {
1917  /* mode == CS_ACCESS_MEM_AP */
1918  retval = rtp_cs_component(mode, ops, ap, component_base, NULL, depth + 1);
1919  }
1920  if (retval == CORESIGHT_COMPONENT_FOUND)
1922  if (retval != ERROR_OK) {
1923  /* TODO: do we need to send an ABORT before continuing? */
1924  LOG_DEBUG("Ignore error parsing CoreSight component");
1925  continue;
1926  }
1927  }
1928 
1929  return ERROR_OK;
1930 }
1931 
1932 static int rtp_cs_component(enum coresight_access_mode mode, const struct rtp_ops *ops,
1933  struct adiv5_ap *ap, target_addr_t base_address, bool *is_mem_ap, int depth)
1934 {
1935  struct cs_component_vals v;
1936  int retval;
1937 
1938  assert(IS_ALIGNED(base_address, ARM_CS_ALIGN));
1939 
1940  if (is_mem_ap)
1941  *is_mem_ap = false;
1942 
1943  if (depth > ROM_TABLE_MAX_DEPTH)
1944  retval = ERROR_FAIL;
1945  else
1946  retval = rtp_read_cs_regs(mode, ap, base_address, &v);
1947 
1948  retval = rtp_ops_cs_component(ops, retval, &v, depth);
1949  if (retval == CORESIGHT_COMPONENT_FOUND)
1951  if (retval != ERROR_OK)
1952  return ERROR_OK; /* Don't abort recursion */
1953 
1954  if (!is_valid_arm_cs_cidr(v.cid))
1955  return ERROR_OK; /* Don't abort recursion */
1956 
1957  const unsigned int class = ARM_CS_CIDR_CLASS(v.cid);
1958 
1959  if (class == ARM_CS_CLASS_0X1_ROM_TABLE)
1960  return rtp_rom_loop(mode, ops, ap, base_address, depth, 32, 960);
1961 
1962  if (class == ARM_CS_CLASS_0X9_CS_COMPONENT) {
1963  if ((v.devarch & ARM_CS_C9_DEVARCH_PRESENT) == 0)
1964  return ERROR_OK;
1965 
1966  if (is_mem_ap) {
1967  if ((v.devarch & DEVARCH_ID_MASK) == DEVARCH_MEM_AP)
1968  *is_mem_ap = true;
1969 
1970  /* SoC-600 APv1 Adapter */
1973  ARM_CS_PIDR_PART(v.pid) == 0x9e5)
1974  *is_mem_ap = true;
1975  }
1976 
1977  /* quit if not ROM table */
1979  return ERROR_OK;
1980 
1982  return rtp_rom_loop(mode, ops, ap, base_address, depth, 64, 256);
1983  else
1984  return rtp_rom_loop(mode, ops, ap, base_address, depth, 32, 512);
1985  }
1986 
1987  /* Class other than 0x1 and 0x9 */
1988  return ERROR_OK;
1989 }
1990 
1991 static int rtp_ap(const struct rtp_ops *ops, struct adiv5_ap *ap, int depth)
1992 {
1993  uint32_t apid;
1994  target_addr_t dbgbase, invalid_entry;
1995 
1996  int retval = rtp_ops_ap_header(ops, ap, depth);
1997  if (retval != ERROR_OK || depth > ROM_TABLE_MAX_DEPTH)
1998  return ERROR_OK; /* Don't abort recursion */
1999 
2000  if (is_adiv6(ap->dap)) {
2001  bool is_mem_ap;
2002  retval = rtp_cs_component(CS_ACCESS_AP, ops, ap, 0, &is_mem_ap, depth);
2003  if (retval == CORESIGHT_COMPONENT_FOUND)
2005  if (retval != ERROR_OK)
2006  return ERROR_OK; /* Don't abort recursion */
2007 
2008  if (!is_mem_ap)
2009  return ERROR_OK;
2010  /* Continue for an ADIv6 MEM-AP or SoC-600 APv1 Adapter */
2011  }
2012 
2013  /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
2014  retval = dap_get_debugbase(ap, &dbgbase, &apid);
2015  if (retval != ERROR_OK)
2016  return retval;
2017  retval = rtp_ops_mem_ap_header(ops, retval, ap, dbgbase, apid, depth);
2018  if (retval != ERROR_OK)
2019  return retval;
2020 
2021  if (apid == 0)
2022  return ERROR_FAIL;
2023 
2024  /* NOTE: a MEM-AP may have a single CoreSight component that's
2025  * not a ROM table ... or have no such components at all.
2026  */
2027  const unsigned int class = (apid & AP_REG_IDR_CLASS_MASK) >> AP_REG_IDR_CLASS_SHIFT;
2028 
2029  if (class == AP_REG_IDR_CLASS_MEM_AP) {
2030  if (is_64bit_ap(ap))
2031  invalid_entry = 0xFFFFFFFFFFFFFFFFull;
2032  else
2033  invalid_entry = 0xFFFFFFFFul;
2034 
2035  if (dbgbase != invalid_entry && (dbgbase & 0x3) != 0x2) {
2036  retval = rtp_cs_component(CS_ACCESS_MEM_AP, ops, ap,
2037  dbgbase & 0xFFFFFFFFFFFFF000ull, NULL, depth);
2038  if (retval == CORESIGHT_COMPONENT_FOUND)
2040  }
2041  }
2042 
2043  return ERROR_OK;
2044 }
2045 
2046 /* Actions for command "dap info" */
2047 
2048 static int dap_info_ap_header(struct adiv5_ap *ap, int depth, void *priv)
2049 {
2050  struct command_invocation *cmd = priv;
2051 
2052  if (depth > ROM_TABLE_MAX_DEPTH) {
2053  command_print(cmd, "\tTables too deep");
2054  return ERROR_FAIL;
2055  }
2056 
2057  command_print(cmd, "%sAP # 0x%" PRIx64, (depth) ? "\t\t" : "", ap->ap_num);
2058  return ERROR_OK;
2059 }
2060 
2061 static int dap_info_mem_ap_header(int retval, struct adiv5_ap *ap,
2062  target_addr_t dbgbase, uint32_t apid, int depth, void *priv)
2063 {
2064  struct command_invocation *cmd = priv;
2065  target_addr_t invalid_entry;
2066  char tabs[17] = "";
2067 
2068  if (retval != ERROR_OK) {
2069  command_print(cmd, "\t\tCan't read MEM-AP, the corresponding core might be turned off");
2070  return retval;
2071  }
2072 
2073  if (depth > ROM_TABLE_MAX_DEPTH) {
2074  command_print(cmd, "\tTables too deep");
2075  return ERROR_FAIL;
2076  }
2077 
2078  if (depth)
2079  snprintf(tabs, sizeof(tabs), "\t[L%02d] ", depth);
2080 
2081  command_print(cmd, "\t\tAP ID register 0x%8.8" PRIx32, apid);
2082  if (apid == 0) {
2083  command_print(cmd, "\t\tNo AP found at this AP#0x%" PRIx64, ap->ap_num);
2084  return ERROR_FAIL;
2085  }
2086 
2087  command_print(cmd, "\t\tType is %s", ap_type_to_description(apid & AP_TYPE_MASK));
2088 
2089  /* NOTE: a MEM-AP may have a single CoreSight component that's
2090  * not a ROM table ... or have no such components at all.
2091  */
2092  const unsigned int class = (apid & AP_REG_IDR_CLASS_MASK) >> AP_REG_IDR_CLASS_SHIFT;
2093 
2094  if (class == AP_REG_IDR_CLASS_MEM_AP) {
2095  if (is_64bit_ap(ap))
2096  invalid_entry = 0xFFFFFFFFFFFFFFFFull;
2097  else
2098  invalid_entry = 0xFFFFFFFFul;
2099 
2100  command_print(cmd, "%sMEM-AP BASE " TARGET_ADDR_FMT, tabs, dbgbase);
2101 
2102  if (dbgbase == invalid_entry || (dbgbase & 0x3) == 0x2) {
2103  command_print(cmd, "\t\tNo ROM table present");
2104  } else {
2105  if (dbgbase & 0x01)
2106  command_print(cmd, "\t\tValid ROM table present");
2107  else
2108  command_print(cmd, "\t\tROM table in legacy format");
2109  }
2110  }
2111 
2112  return ERROR_OK;
2113 }
2114 
2115 static int dap_info_cs_component(int retval, struct cs_component_vals *v, int depth, void *priv)
2116 {
2117  struct command_invocation *cmd = priv;
2118 
2119  if (depth > ROM_TABLE_MAX_DEPTH) {
2120  command_print(cmd, "\tTables too deep");
2121  return ERROR_FAIL;
2122  }
2123 
2124  if (v->mode == CS_ACCESS_MEM_AP)
2125  command_print(cmd, "\t\tComponent base address " TARGET_ADDR_FMT, v->component_base);
2126 
2127  if (retval != ERROR_OK) {
2128  command_print(cmd, "\t\tCan't read component, the corresponding core might be turned off");
2129  return retval;
2130  }
2131 
2132  if (!is_valid_arm_cs_cidr(v->cid)) {
2133  command_print(cmd, "\t\tInvalid CID 0x%08" PRIx32, v->cid);
2134  return ERROR_OK; /* Don't abort recursion */
2135  }
2136 
2137  /* component may take multiple 4K pages */
2138  uint32_t size = ARM_CS_PIDR_SIZE(v->pid);
2139  if (size > 0)
2140  command_print(cmd, "\t\tStart address " TARGET_ADDR_FMT, v->component_base - 0x1000 * size);
2141 
2142  command_print(cmd, "\t\tPeripheral ID 0x%010" PRIx64, v->pid);
2143 
2144  const unsigned int part_num = ARM_CS_PIDR_PART(v->pid);
2145  unsigned int designer_id = ARM_CS_PIDR_DESIGNER(v->pid);
2146 
2147  if (v->pid & ARM_CS_PIDR_JEDEC) {
2148  /* JEP106 code */
2149  command_print(cmd, "\t\tDesigner is 0x%03x, %s",
2150  designer_id, jep106_manufacturer(designer_id));
2151  } else {
2152  /* Legacy ASCII ID, clear invalid bits */
2153  designer_id &= 0x7f;
2154  command_print(cmd, "\t\tDesigner ASCII code 0x%02x, %s",
2155  designer_id, designer_id == 0x41 ? "ARM" : "<unknown>");
2156  }
2157 
2158  const struct dap_part_nums *partnum = pidr_to_part_num(designer_id, part_num);
2159  command_print(cmd, "\t\tPart is 0x%03x, %s %s", part_num, partnum->type, partnum->full);
2160 
2161  const unsigned int class = ARM_CS_CIDR_CLASS(v->cid);
2162  command_print(cmd, "\t\tComponent class is 0x%x, %s", class, class_description[class]);
2163 
2164  if (class == ARM_CS_CLASS_0X1_ROM_TABLE) {
2166  command_print(cmd, "\t\tMEMTYPE system memory present on bus");
2167  else
2168  command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
2169  return ERROR_OK;
2170  }
2171 
2172  if (class == ARM_CS_CLASS_0X9_CS_COMPONENT) {
2174 
2175  /* REVISIT also show ARM_CS_C9_DEVID */
2176 
2177  if ((v->devarch & ARM_CS_C9_DEVARCH_PRESENT) == 0)
2178  return ERROR_OK;
2179 
2180  unsigned int architect_id = ARM_CS_C9_DEVARCH_ARCHITECT(v->devarch);
2181  unsigned int revision = ARM_CS_C9_DEVARCH_REVISION(v->devarch);
2182  command_print(cmd, "\t\tDev Arch is 0x%08" PRIx32 ", %s \"%s\" rev.%u", v->devarch,
2184  revision);
2185 
2186  if ((v->devarch & DEVARCH_ID_MASK) == DEVARCH_ROM_C_0X9) {
2187  command_print(cmd, "\t\tType is ROM table");
2188 
2190  command_print(cmd, "\t\tMEMTYPE system memory present on bus");
2191  else
2192  command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
2193  }
2194  return ERROR_OK;
2195  }
2196 
2197  /* Class other than 0x1 and 0x9 */
2198  return ERROR_OK;
2199 }
2200 
2201 static int dap_info_rom_table_entry(int retval, int depth,
2202  unsigned int offset, uint64_t romentry, void *priv)
2203 {
2204  struct command_invocation *cmd = priv;
2205  char tabs[16] = "";
2206 
2207  if (depth)
2208  snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
2209 
2210  if (retval != ERROR_OK) {
2211  command_print(cmd, "\t%sROMTABLE[0x%x] Read error", tabs, offset);
2212  command_print(cmd, "\t\tUnable to continue");
2213  command_print(cmd, "\t%s\tStop parsing of ROM table", tabs);
2214  return retval;
2215  }
2216 
2217  command_print(cmd, "\t%sROMTABLE[0x%x] = 0x%08" PRIx64,
2218  tabs, offset, romentry);
2219 
2220  if (romentry == 0) {
2221  command_print(cmd, "\t%s\tEnd of ROM table", tabs);
2222  return ERROR_OK;
2223  }
2224 
2225  if (!(romentry & ARM_CS_ROMENTRY_PRESENT)) {
2226  command_print(cmd, "\t\tComponent not present");
2227  return ERROR_OK;
2228  }
2229 
2230  return ERROR_OK;
2231 }
2232 
2234 {
2235  struct rtp_ops dap_info_ops = {
2237  .mem_ap_header = dap_info_mem_ap_header,
2238  .cs_component = dap_info_cs_component,
2239  .rom_table_entry = dap_info_rom_table_entry,
2240  .priv = cmd,
2241  };
2242 
2243  return rtp_ap(&dap_info_ops, ap, 0);
2244 }
2245 
2246 /* Actions for dap_lookup_cs_component() */
2247 
2249  /* input */
2250  unsigned int idx;
2251  unsigned int type;
2252  /* output */
2253  uint64_t component_base;
2254  uint64_t ap_num;
2255 };
2256 
2258  struct cs_component_vals *v, int depth, void *priv)
2259 {
2260  struct dap_lookup_data *lookup = priv;
2261 
2262  if (retval != ERROR_OK)
2263  return retval;
2264 
2265  if (!is_valid_arm_cs_cidr(v->cid))
2266  return ERROR_OK;
2267 
2268  const unsigned int class = ARM_CS_CIDR_CLASS(v->cid);
2269  if (class != ARM_CS_CLASS_0X9_CS_COMPONENT)
2270  return ERROR_OK;
2271 
2272  if ((v->devtype_memtype & ARM_CS_C9_DEVTYPE_MASK) != lookup->type)
2273  return ERROR_OK;
2274 
2275  if (lookup->idx) {
2276  /* search for next one */
2277  --lookup->idx;
2278  return ERROR_OK;
2279  }
2280 
2281  /* Found! */
2282  lookup->component_base = v->component_base;
2283  lookup->ap_num = v->ap->ap_num;
2285 }
2286 
2287 int dap_lookup_cs_component(struct adiv5_ap *ap, uint8_t type,
2288  target_addr_t *addr, int32_t core_id)
2289 {
2290  struct dap_lookup_data lookup = {
2291  .type = type,
2292  .idx = core_id,
2293  };
2294  struct rtp_ops dap_lookup_cs_component_ops = {
2295  .ap_header = NULL,
2296  .mem_ap_header = NULL,
2297  .cs_component = dap_lookup_cs_component_cs_component,
2298  .rom_table_entry = NULL,
2299  .priv = &lookup,
2300  };
2301 
2302  int retval = rtp_ap(&dap_lookup_cs_component_ops, ap, 0);
2303  if (retval == CORESIGHT_COMPONENT_FOUND) {
2304  if (lookup.ap_num != ap->ap_num) {
2305  /* TODO: handle search from root ROM table */
2306  LOG_DEBUG("CS lookup ended in AP # 0x%" PRIx64 ". Ignore it", lookup.ap_num);
2308  }
2309  LOG_DEBUG("CS lookup found at 0x%" PRIx64, lookup.component_base);
2310  *addr = lookup.component_base;
2311  return ERROR_OK;
2312  }
2313  if (retval != ERROR_OK) {
2314  LOG_DEBUG("CS lookup error %d", retval);
2315  return retval;
2316  }
2317  LOG_DEBUG("CS lookup not found");
2319 }
2320 
2325  CFG_CTIBASE, /* DEPRECATED */
2326 };
2327 
2328 static const struct jim_nvp nvp_config_opts[] = {
2329  { .name = "-dap", .value = CFG_DAP },
2330  { .name = "-ap-num", .value = CFG_AP_NUM },
2331  { .name = "-baseaddr", .value = CFG_BASEADDR },
2332  { .name = "-ctibase", .value = CFG_CTIBASE }, /* DEPRECATED */
2333  { .name = NULL, .value = -1 }
2334 };
2335 
2337  struct adiv5_dap **dap_p, uint64_t *ap_num_p, uint32_t *base_p)
2338 {
2339  assert(dap_p && ap_num_p);
2340 
2341  if (!goi->argc)
2342  return JIM_OK;
2343 
2344  Jim_SetEmptyResult(goi->interp);
2345 
2346  struct jim_nvp *n;
2348  goi->argv[0], &n);
2349  if (e != JIM_OK)
2350  return JIM_CONTINUE;
2351 
2352  /* base_p can be NULL, then '-baseaddr' option is treated as unknown */
2353  if (!base_p && (n->value == CFG_BASEADDR || n->value == CFG_CTIBASE))
2354  return JIM_CONTINUE;
2355 
2356  e = jim_getopt_obj(goi, NULL);
2357  if (e != JIM_OK)
2358  return e;
2359 
2360  switch (n->value) {
2361  case CFG_DAP:
2362  if (goi->isconfigure) {
2363  Jim_Obj *o_t;
2364  struct adiv5_dap *dap;
2365  e = jim_getopt_obj(goi, &o_t);
2366  if (e != JIM_OK)
2367  return e;
2368  dap = dap_instance_by_jim_obj(goi->interp, o_t);
2369  if (!dap) {
2370  Jim_SetResultString(goi->interp, "DAP name invalid!", -1);
2371  return JIM_ERR;
2372  }
2373  if (*dap_p && *dap_p != dap) {
2374  Jim_SetResultString(goi->interp,
2375  "DAP assignment cannot be changed!", -1);
2376  return JIM_ERR;
2377  }
2378  *dap_p = dap;
2379  } else {
2380  if (goi->argc)
2381  goto err_no_param;
2382  if (!*dap_p) {
2383  Jim_SetResultString(goi->interp, "DAP not configured", -1);
2384  return JIM_ERR;
2385  }
2386  Jim_SetResultString(goi->interp, adiv5_dap_name(*dap_p), -1);
2387  }
2388  break;
2389 
2390  case CFG_AP_NUM:
2391  if (goi->isconfigure) {
2392  /* jim_wide is a signed 64 bits int, ap_num is unsigned with max 52 bits */
2393  jim_wide ap_num;
2394  e = jim_getopt_wide(goi, &ap_num);
2395  if (e != JIM_OK)
2396  return e;
2397  /* we still don't know dap->adi_version */
2398  if (ap_num < 0 || (ap_num > DP_APSEL_MAX && (ap_num & 0xfff))) {
2399  Jim_SetResultString(goi->interp, "Invalid AP number!", -1);
2400  return JIM_ERR;
2401  }
2402  *ap_num_p = ap_num;
2403  } else {
2404  if (goi->argc)
2405  goto err_no_param;
2406  if (*ap_num_p == DP_APSEL_INVALID) {
2407  Jim_SetResultString(goi->interp, "AP number not configured", -1);
2408  return JIM_ERR;
2409  }
2410  Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, *ap_num_p));
2411  }
2412  break;
2413 
2414  case CFG_CTIBASE:
2415  LOG_WARNING("DEPRECATED! use \'-baseaddr' not \'-ctibase\'");
2416  /* fall through */
2417  case CFG_BASEADDR:
2418  if (goi->isconfigure) {
2419  jim_wide base;
2420  e = jim_getopt_wide(goi, &base);
2421  if (e != JIM_OK)
2422  return e;
2423  *base_p = (uint32_t)base;
2424  } else {
2425  if (goi->argc)
2426  goto err_no_param;
2427  Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, *base_p));
2428  }
2429  break;
2430  };
2431 
2432  return JIM_OK;
2433 
2434 err_no_param:
2435  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
2436  return JIM_ERR;
2437 }
2438 
2440  struct adiv5_private_config *pc, enum adiv5_configure_dap_optional optional)
2441 {
2442  int e;
2443 
2444  if (!pc) {
2445  pc = (struct adiv5_private_config *)target->private_config;
2446  if (!pc) {
2447  pc = calloc(1, sizeof(struct adiv5_private_config));
2448  if (!pc) {
2449  LOG_ERROR("Out of memory");
2450  return JIM_ERR;
2451  }
2452  pc->ap_num = DP_APSEL_INVALID;
2453  target->private_config = pc;
2454  }
2455  }
2456 
2457  if (optional == ADI_CONFIGURE_DAP_COMPULSORY)
2458  target->has_dap = true;
2459 
2460  e = adiv5_jim_spot_configure(goi, &pc->dap, &pc->ap_num, NULL);
2461  if (e != JIM_OK)
2462  return e;
2463 
2464  if (pc->dap && !target->dap_configured) {
2465  if (target->tap_configured) {
2466  pc->dap = NULL;
2467  Jim_SetResultString(goi->interp,
2468  "-chain-position and -dap configparams are mutually exclusive!", -1);
2469  return JIM_ERR;
2470  }
2471  target->tap = pc->dap->tap;
2472  target->dap_configured = true;
2473  target->has_dap = true;
2474  }
2475 
2476  return JIM_OK;
2477 }
2478 
2480 {
2482 }
2483 
2485 {
2486  if (!pc)
2487  return ERROR_FAIL;
2488 
2489  if (!pc->dap)
2490  return ERROR_FAIL;
2491 
2492  return ERROR_OK;
2493 }
2494 
2496  struct jim_getopt_info *goi)
2497 {
2498  return adiv5_jim_spot_configure(goi, &cfg->dap, &cfg->ap_num, &cfg->base);
2499 }
2500 
2502 {
2503  p->dap = NULL;
2504  p->ap_num = DP_APSEL_INVALID;
2505  p->base = 0;
2506  return ERROR_OK;
2507 }
2508 
2509 COMMAND_HANDLER(handle_dap_info_command)
2510 {
2511  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2512  uint64_t apsel;
2513 
2514  switch (CMD_ARGC) {
2515  case 0:
2516  apsel = dap->apsel;
2517  break;
2518  case 1:
2519  if (!strcmp(CMD_ARGV[0], "root")) {
2520  if (!is_adiv6(dap)) {
2521  command_print(CMD, "Option \"root\" not allowed with ADIv5 DAP");
2523  }
2524  int retval = adiv6_dap_read_baseptr(CMD, dap, &apsel);
2525  if (retval != ERROR_OK) {
2526  command_print(CMD, "Failed reading DAP baseptr");
2527  return retval;
2528  }
2529  break;
2530  }
2532  if (!is_ap_num_valid(dap, apsel)) {
2533  command_print(CMD, "Invalid AP number");
2535  }
2536  break;
2537  default:
2539  }
2540 
2541  struct adiv5_ap *ap = dap_get_ap(dap, apsel);
2542  if (!ap) {
2543  command_print(CMD, "Cannot get AP");
2544  return ERROR_FAIL;
2545  }
2546 
2547  int retval = dap_info_command(CMD, ap);
2548  dap_put_ap(ap);
2549  return retval;
2550 }
2551 
2552 COMMAND_HANDLER(dap_baseaddr_command)
2553 {
2554  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2555  uint64_t apsel;
2556  uint32_t baseaddr_lower, baseaddr_upper;
2557  struct adiv5_ap *ap;
2558  target_addr_t baseaddr;
2559  int retval;
2560 
2561  baseaddr_upper = 0;
2562 
2563  switch (CMD_ARGC) {
2564  case 0:
2565  apsel = dap->apsel;
2566  break;
2567  case 1:
2568  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], apsel);
2569  if (!is_ap_num_valid(dap, apsel)) {
2570  command_print(CMD, "Invalid AP number");
2572  }
2573  break;
2574  default:
2576  }
2577 
2578  /* NOTE: assumes we're talking to a MEM-AP, which
2579  * has a base address. There are other kinds of AP,
2580  * though they're not common for now. This should
2581  * use the ID register to verify it's a MEM-AP.
2582  */
2583 
2584  ap = dap_get_ap(dap, apsel);
2585  if (!ap) {
2586  command_print(CMD, "Cannot get AP");
2587  return ERROR_FAIL;
2588  }
2589 
2590  retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE(dap), &baseaddr_lower);
2591 
2592  if (retval == ERROR_OK && ap->cfg_reg == MEM_AP_REG_CFG_INVALID)
2593  retval = dap_queue_ap_read(ap, MEM_AP_REG_CFG(dap), &ap->cfg_reg);
2594 
2595  if (retval == ERROR_OK && (ap->cfg_reg == MEM_AP_REG_CFG_INVALID || is_64bit_ap(ap))) {
2596  /* MEM_AP_REG_BASE64 is defined as 'RES0'; can be read and then ignored on 32 bits AP */
2597  retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE64(dap), &baseaddr_upper);
2598  }
2599 
2600  if (retval == ERROR_OK)
2601  retval = dap_run(dap);
2602  dap_put_ap(ap);
2603  if (retval != ERROR_OK)
2604  return retval;
2605 
2606  if (is_64bit_ap(ap)) {
2607  baseaddr = (((target_addr_t)baseaddr_upper) << 32) | baseaddr_lower;
2608  command_print(CMD, "0x%016" PRIx64, baseaddr);
2609  } else
2610  command_print(CMD, "0x%08" PRIx32, baseaddr_lower);
2611 
2612  return ERROR_OK;
2613 }
2614 
2615 COMMAND_HANDLER(dap_memaccess_command)
2616 {
2617  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2618  struct adiv5_ap *ap;
2619  uint32_t memaccess_tck;
2620 
2621  switch (CMD_ARGC) {
2622  case 0:
2623  ap = dap_get_ap(dap, dap->apsel);
2624  if (!ap) {
2625  command_print(CMD, "Cannot get AP");
2626  return ERROR_FAIL;
2627  }
2629  break;
2630  case 1:
2631  ap = dap_get_config_ap(dap, dap->apsel);
2632  if (!ap) {
2633  command_print(CMD, "Cannot get AP");
2634  return ERROR_FAIL;
2635  }
2638  break;
2639  default:
2641  }
2642 
2643  dap_put_ap(ap);
2644 
2645  command_print(CMD, "memory bus access delay set to %" PRIu32 " tck",
2646  memaccess_tck);
2647 
2648  return ERROR_OK;
2649 }
2650 
2651 COMMAND_HANDLER(dap_apsel_command)
2652 {
2653  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2654  uint64_t apsel;
2655 
2656  switch (CMD_ARGC) {
2657  case 0:
2658  command_print(CMD, "0x%" PRIx64, dap->apsel);
2659  return ERROR_OK;
2660  case 1:
2662  if (!is_ap_num_valid(dap, apsel)) {
2663  command_print(CMD, "Invalid AP number");
2665  }
2666  break;
2667  default:
2669  }
2670 
2671  dap->apsel = apsel;
2672  return ERROR_OK;
2673 }
2674 
2675 COMMAND_HANDLER(dap_apcsw_command)
2676 {
2677  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2678  struct adiv5_ap *ap;
2679  uint32_t csw_val, csw_mask;
2680 
2681  switch (CMD_ARGC) {
2682  case 0:
2683  ap = dap_get_ap(dap, dap->apsel);
2684  if (!ap) {
2685  command_print(CMD, "Cannot get AP");
2686  return ERROR_FAIL;
2687  }
2688  command_print(CMD, "AP#0x%" PRIx64 " selected, csw 0x%8.8" PRIx32,
2689  dap->apsel, ap->csw_default);
2690  break;
2691  case 1:
2692  if (strcmp(CMD_ARGV[0], "default") == 0)
2693  csw_val = CSW_AHB_DEFAULT;
2694  else
2695  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], csw_val);
2696 
2697  if (csw_val & (CSW_SIZE_MASK | CSW_ADDRINC_MASK)) {
2698  LOG_ERROR("CSW value cannot include 'Size' and 'AddrInc' bit-fields");
2700  }
2701  ap = dap_get_config_ap(dap, dap->apsel);
2702  if (!ap) {
2703  command_print(CMD, "Cannot get AP");
2704  return ERROR_FAIL;
2705  }
2706  ap->csw_default = csw_val;
2707  break;
2708  case 2:
2709  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], csw_val);
2710  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], csw_mask);
2711  if (csw_mask & (CSW_SIZE_MASK | CSW_ADDRINC_MASK)) {
2712  LOG_ERROR("CSW mask cannot include 'Size' and 'AddrInc' bit-fields");
2714  }
2715  ap = dap_get_config_ap(dap, dap->apsel);
2716  if (!ap) {
2717  command_print(CMD, "Cannot get AP");
2718  return ERROR_FAIL;
2719  }
2720  ap->csw_default = (ap->csw_default & ~csw_mask) | (csw_val & csw_mask);
2721  break;
2722  default:
2724  }
2725  dap_put_ap(ap);
2726 
2727  return ERROR_OK;
2728 }
2729 
2730 
2731 
2732 COMMAND_HANDLER(dap_apid_command)
2733 {
2734  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2735  uint64_t apsel;
2736  uint32_t apid;
2737  int retval;
2738 
2739  switch (CMD_ARGC) {
2740  case 0:
2741  apsel = dap->apsel;
2742  break;
2743  case 1:
2745  if (!is_ap_num_valid(dap, apsel)) {
2746  command_print(CMD, "Invalid AP number");
2748  }
2749  break;
2750  default:
2752  }
2753 
2754  struct adiv5_ap *ap = dap_get_ap(dap, apsel);
2755  if (!ap) {
2756  command_print(CMD, "Cannot get AP");
2757  return ERROR_FAIL;
2758  }
2759  retval = dap_queue_ap_read(ap, AP_REG_IDR(dap), &apid);
2760  if (retval != ERROR_OK) {
2761  dap_put_ap(ap);
2762  return retval;
2763  }
2764  retval = dap_run(dap);
2765  dap_put_ap(ap);
2766  if (retval != ERROR_OK)
2767  return retval;
2768 
2769  command_print(CMD, "0x%8.8" PRIx32, apid);
2770 
2771  return retval;
2772 }
2773 
2774 COMMAND_HANDLER(dap_apreg_command)
2775 {
2776  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2777  uint64_t apsel;
2778  uint32_t reg, value;
2779  int retval;
2780 
2781  if (CMD_ARGC < 2 || CMD_ARGC > 3)
2783 
2785  if (!is_ap_num_valid(dap, apsel)) {
2786  command_print(CMD, "Invalid AP number");
2788  }
2789 
2790  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg);
2791  if (is_adiv6(dap)) {
2792  if (reg >= 4096 || (reg & 3)) {
2793  command_print(CMD, "Invalid reg value (should be less than 4096 and 4 bytes aligned)");
2795  }
2796  } else { /* ADI version 5 */
2797  if (reg >= 256 || (reg & 3)) {
2798  command_print(CMD, "Invalid reg value (should be less than 256 and 4 bytes aligned)");
2800  }
2801  }
2802 
2803  struct adiv5_ap *ap = dap_get_ap(dap, apsel);
2804  if (!ap) {
2805  command_print(CMD, "Cannot get AP");
2806  return ERROR_FAIL;
2807  }
2808 
2809  if (CMD_ARGC == 3) {
2810  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
2811  /* see if user supplied register address is a match for the CSW or TAR register */
2812  if (reg == MEM_AP_REG_CSW(dap)) {
2813  ap->csw_value = 0; /* invalid, in case write fails */
2814  retval = dap_queue_ap_write(ap, reg, value);
2815  if (retval == ERROR_OK)
2816  ap->csw_value = value;
2817  } else if (reg == MEM_AP_REG_TAR(dap)) {
2818  retval = dap_queue_ap_write(ap, reg, value);
2819  if (retval == ERROR_OK)
2820  ap->tar_value = (ap->tar_value & ~0xFFFFFFFFull) | value;
2821  else {
2822  /* To track independent writes to TAR and TAR64, two tar_valid flags */
2823  /* should be used. To keep it simple, tar_valid is only invalidated on a */
2824  /* write fail. This approach causes a later re-write of the TAR and TAR64 */
2825  /* if tar_valid is false. */
2826  ap->tar_valid = false;
2827  }
2828  } else if (reg == MEM_AP_REG_TAR64(dap)) {
2829  retval = dap_queue_ap_write(ap, reg, value);
2830  if (retval == ERROR_OK)
2831  ap->tar_value = (ap->tar_value & 0xFFFFFFFFull) | (((target_addr_t)value) << 32);
2832  else {
2833  /* See above comment for the MEM_AP_REG_TAR failed write case */
2834  ap->tar_valid = false;
2835  }
2836  } else {
2837  retval = dap_queue_ap_write(ap, reg, value);
2838  }
2839  } else {
2840  retval = dap_queue_ap_read(ap, reg, &value);
2841  }
2842  if (retval == ERROR_OK)
2843  retval = dap_run(dap);
2844 
2845  dap_put_ap(ap);
2846 
2847  if (retval != ERROR_OK)
2848  return retval;
2849 
2850  if (CMD_ARGC == 2)
2851  command_print(CMD, "0x%08" PRIx32, value);
2852 
2853  return retval;
2854 }
2855 
2856 COMMAND_HANDLER(dap_dpreg_command)
2857 {
2858  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2859  uint32_t reg, value;
2860  int retval;
2861 
2862  if (CMD_ARGC < 1 || CMD_ARGC > 2)
2864 
2865  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], reg);
2866  if (reg >= 256 || (reg & 3)) {
2867  command_print(CMD, "Invalid reg value (should be less than 256 and 4 bytes aligned)");
2869  }
2870 
2871  if (CMD_ARGC == 2) {
2872  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2873  retval = dap_queue_dp_write(dap, reg, value);
2874  } else {
2875  retval = dap_queue_dp_read(dap, reg, &value);
2876  }
2877  if (retval == ERROR_OK)
2878  retval = dap_run(dap);
2879 
2880  if (retval != ERROR_OK)
2881  return retval;
2882 
2883  if (CMD_ARGC == 1)
2884  command_print(CMD, "0x%08" PRIx32, value);
2885 
2886  return retval;
2887 }
2888 
2889 COMMAND_HANDLER(dap_ti_be_32_quirks_command)
2890 {
2891  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2892  return CALL_COMMAND_HANDLER(handle_command_parse_bool, &dap->ti_be_32_quirks,
2893  "TI BE-32 quirks mode");
2894 }
2895 
2896 COMMAND_HANDLER(dap_nu_npcx_quirks_command)
2897 {
2898  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2899  return CALL_COMMAND_HANDLER(handle_command_parse_bool, &dap->nu_npcx_quirks,
2900  "Nuvoton NPCX quirks mode");
2901 }
2902 
2904  {
2905  .name = "info",
2906  .handler = handle_dap_info_command,
2907  .mode = COMMAND_EXEC,
2908  .help = "display ROM table for specified MEM-AP (default currently selected AP) "
2909  "or the ADIv6 root ROM table",
2910  .usage = "[ap_num | 'root']",
2911  },
2912  {
2913  .name = "apsel",
2914  .handler = dap_apsel_command,
2915  .mode = COMMAND_ANY,
2916  .help = "Set the currently selected AP (default 0) "
2917  "and display the result",
2918  .usage = "[ap_num]",
2919  },
2920  {
2921  .name = "apcsw",
2922  .handler = dap_apcsw_command,
2923  .mode = COMMAND_ANY,
2924  .help = "Set CSW default bits",
2925  .usage = "[value [mask]]",
2926  },
2927 
2928  {
2929  .name = "apid",
2930  .handler = dap_apid_command,
2931  .mode = COMMAND_EXEC,
2932  .help = "return ID register from AP "
2933  "(default currently selected AP)",
2934  .usage = "[ap_num]",
2935  },
2936  {
2937  .name = "apreg",
2938  .handler = dap_apreg_command,
2939  .mode = COMMAND_EXEC,
2940  .help = "read/write a register from AP "
2941  "(reg is byte address of a word register, like 0 4 8...)",
2942  .usage = "ap_num reg [value]",
2943  },
2944  {
2945  .name = "dpreg",
2946  .handler = dap_dpreg_command,
2947  .mode = COMMAND_EXEC,
2948  .help = "read/write a register from DP "
2949  "(reg is byte address (bank << 4 | reg) of a word register, like 0 4 8...)",
2950  .usage = "reg [value]",
2951  },
2952  {
2953  .name = "baseaddr",
2954  .handler = dap_baseaddr_command,
2955  .mode = COMMAND_EXEC,
2956  .help = "return debug base address from MEM-AP "
2957  "(default currently selected AP)",
2958  .usage = "[ap_num]",
2959  },
2960  {
2961  .name = "memaccess",
2962  .handler = dap_memaccess_command,
2963  .mode = COMMAND_EXEC,
2964  .help = "set/get number of extra tck for MEM-AP memory "
2965  "bus access [0-255]",
2966  .usage = "[cycles]",
2967  },
2968  {
2969  .name = "ti_be_32_quirks",
2970  .handler = dap_ti_be_32_quirks_command,
2971  .mode = COMMAND_CONFIG,
2972  .help = "set/get quirks mode for TI TMS450/TMS570 processors",
2973  .usage = "[enable]",
2974  },
2975  {
2976  .name = "nu_npcx_quirks",
2977  .handler = dap_nu_npcx_quirks_command,
2978  .mode = COMMAND_CONFIG,
2979  .help = "set/get quirks mode for Nuvoton NPCX controllers",
2980  .usage = "[enable]",
2981  },
2983 };
#define IS_ALIGNED(x, a)
Definition: align.h:22
Holds the interface to ARM cores.
#define DEVARCH_MEM_AP
Definition: arm_adi_v5.c:1038
struct adiv5_ap * dap_get_config_ap(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1198
static int mem_ap_write(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address, bool addrinc)
Synchronous write of a block of memory, using a specific access size.
Definition: arm_adi_v5.c:474
static int dap_info_mem_ap_header(int retval, struct adiv5_ap *ap, target_addr_t dbgbase, uint32_t apid, int depth, void *priv)
Definition: arm_adi_v5.c:2061
int mem_ap_read_buf(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:722
static int dap_info_ap_header(struct adiv5_ap *ap, int depth, void *priv)
Definition: arm_adi_v5.c:2048
static int dap_queue_read_reg(enum coresight_access_mode mode, struct adiv5_ap *ap, uint64_t component_base, unsigned int reg, uint32_t *value)
Helper to read CoreSight component's registers, either on the bus behind a MEM-AP or directly in the ...
Definition: arm_adi_v5.c:1327
static const char * class_description[16]
Definition: arm_adi_v5.c:984
static int rtp_ap(const struct rtp_ops *ops, struct adiv5_ap *ap, int depth)
Definition: arm_adi_v5.c:1991
static const struct jim_nvp nvp_config_opts[]
Definition: arm_adi_v5.c:2328
COMMAND_HANDLER(handle_dap_info_command)
Definition: arm_adi_v5.c:2509
#define DEVARCH_ID_MASK
Definition: arm_adi_v5.c:1037
int dap_lookup_cs_component(struct adiv5_ap *ap, uint8_t type, target_addr_t *addr, int32_t core_id)
Definition: arm_adi_v5.c:2287
static int rtp_rom_loop(enum coresight_access_mode mode, const struct rtp_ops *ops, struct adiv5_ap *ap, target_addr_t base_address, int depth, unsigned int width, unsigned int max_entries)
Definition: arm_adi_v5.c:1854
int mem_ap_read_buf_noincr(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:734
static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, target_addr_t address)
Definition: arm_adi_v5.c:83
static int mem_ap_setup_tar(struct adiv5_ap *ap, target_addr_t tar)
Definition: arm_adi_v5.c:110
int adiv5_verify_config(struct adiv5_private_config *pc)
Definition: arm_adi_v5.c:2484
#define DEVARCH_UNKNOWN_V2
Definition: arm_adi_v5.c:1040
static int mem_ap_setup_transfer(struct adiv5_ap *ap, uint32_t csw, target_addr_t tar)
Queue transactions setting up transfer parameters for the currently selected MEM-AP.
Definition: arm_adi_v5.c:214
int dap_info_command(struct command_invocation *cmd, struct adiv5_ap *ap)
Definition: arm_adi_v5.c:2233
int mem_ap_read_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t *value)
Asynchronous (queued) read of a word from memory or a system register.
Definition: arm_adi_v5.c:237
static const char * ap_type_to_description(enum ap_type type)
Definition: arm_adi_v5.c:1069
int adiv5_mem_ap_spot_init(struct adiv5_mem_ap_spot *p)
Definition: arm_adi_v5.c:2501
static int dap_info_cs_component(int retval, struct cs_component_vals *v, int depth, void *priv)
Definition: arm_adi_v5.c:2115
static bool is_ap_in_use(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:1150
int mem_ap_write_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t value)
Asynchronous (queued) write of a word to memory or a system register.
Definition: arm_adi_v5.c:289
bool is_ap_num_valid(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1078
static int adiv5_jim_spot_configure(struct jim_getopt_info *goi, struct adiv5_dap **dap_p, uint64_t *ap_num_p, uint32_t *base_p)
Definition: arm_adi_v5.c:2336
const char * description
Definition: arm_adi_v5.c:1010
enum ap_type type
Definition: arm_adi_v5.c:1055
int adiv6_dap_read_baseptr(struct command_invocation *cmd, struct adiv5_dap *dap, uint64_t *baseptr)
Definition: arm_adi_v5.c:1266
static const char * class0x9_devarch_description(uint32_t devarch)
Definition: arm_adi_v5.c:1042
uint32_t arch_id
Definition: arm_adi_v5.c:1009
static int dap_info_rom_table_entry(int retval, int depth, unsigned int offset, uint64_t romentry, void *priv)
Definition: arm_adi_v5.c:2201
int adiv5_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: arm_adi_v5.c:2479
int dap_find_get_ap(struct adiv5_dap *dap, enum ap_type type_to_find, struct adiv5_ap **ap_out)
Definition: arm_adi_v5.c:1107
adiv5_cfg_param
Definition: arm_adi_v5.c:2321
@ CFG_AP_NUM
Definition: arm_adi_v5.c:2323
@ CFG_CTIBASE
Definition: arm_adi_v5.c:2325
@ CFG_BASEADDR
Definition: arm_adi_v5.c:2324
@ CFG_DAP
Definition: arm_adi_v5.c:2322
static int mem_ap_read_tar(struct adiv5_ap *ap, target_addr_t *tar)
Definition: arm_adi_v5.c:130
int mem_ap_write_buf_noincr(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:740
int dap_to_jtag(struct adiv5_dap *dap)
Put the debug link into JTAG mode, if the target supports it.
Definition: arm_adi_v5.c:970
static int rtp_ops_rom_table_entry(const struct rtp_ops *ops, int retval, int depth, unsigned int offset, uint64_t romentry)
Wrapper around struct rtp_ops::rom_table_entry.
Definition: arm_adi_v5.c:1827
int adiv5_jim_configure_ext(struct target *target, struct jim_getopt_info *goi, struct adiv5_private_config *pc, enum adiv5_configure_dap_optional optional)
Definition: arm_adi_v5.c:2439
int dap_dp_init_or_reconnect(struct adiv5_dap *dap)
Initialize a DAP or do reconnect if DAP is not accessible.
Definition: arm_adi_v5.c:857
static int dap_get_debugbase(struct adiv5_ap *ap, target_addr_t *dbgbase, uint32_t *apid)
Definition: arm_adi_v5.c:1230
int dap_dp_init(struct adiv5_dap *dap)
Initialize a DAP.
Definition: arm_adi_v5.c:779
#define ROM_TABLE_MAX_DEPTH
Definition: arm_adi_v5.c:1840
static int mem_ap_setup_transfer_verify_size_packing(struct adiv5_ap *ap, unsigned int size, target_addr_t address, bool addrinc, bool pack, unsigned int *this_size)
Queue transactions setting up transfer parameters for the currently selected MEM-AP.
Definition: arm_adi_v5.c:345
static int mem_ap_setup_csw(struct adiv5_ap *ap, uint32_t csw)
Definition: arm_adi_v5.c:94
int mem_ap_read_atomic_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t *value)
Synchronous read of a word from memory or a system register.
Definition: arm_adi_v5.c:266
int dap_to_swd(struct adiv5_dap *dap)
Put the debug link into SWD mode, if the target supports it.
Definition: arm_adi_v5.c:952
#define DAP_POWER_DOMAIN_TIMEOUT
Definition: arm_adi_v5.c:749
struct adiv5_ap * dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1189
int dap_put_ap(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:1209
int mem_ap_init(struct adiv5_ap *ap)
Initialize a DAP.
Definition: arm_adi_v5.c:888
static int rtp_ops_mem_ap_header(const struct rtp_ops *ops, int retval, struct adiv5_ap *ap, uint64_t dbgbase, uint32_t apid, int depth)
Wrapper around struct rtp_ops::mem_ap_header.
Definition: arm_adi_v5.c:1795
coresight_access_mode
Method to access the CoreSight component.
Definition: arm_adi_v5.c:1298
@ CS_ACCESS_MEM_AP
Definition: arm_adi_v5.c:1300
@ CS_ACCESS_AP
Definition: arm_adi_v5.c:1299
int mem_ap_write_buf(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
Definition: arm_adi_v5.c:728
static const struct @62 ap_types[]
static int dap_devtype_display(struct command_invocation *cmd, uint32_t devtype)
Definition: arm_adi_v5.c:1583
static int rtp_ops_ap_header(const struct rtp_ops *ops, struct adiv5_ap *ap, int depth)
Wrapper around struct rtp_ops::ap_header.
Definition: arm_adi_v5.c:1782
static int mem_ap_read(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t adr, bool addrinc)
Synchronous read of a block of memory, using a specific access size.
Definition: arm_adi_v5.c:604
#define ARCH_ID(architect, archid)
Definition: arm_adi_v5.c:1003
void dap_invalidate_cache(struct adiv5_dap *dap)
Invalidate cached DP select and cached TAR and CSW of all APs.
Definition: arm_adi_v5.c:756
static int dap_lookup_cs_component_cs_component(int retval, struct cs_component_vals *v, int depth, void *priv)
Definition: arm_adi_v5.c:2257
static int rtp_ops_cs_component(const struct rtp_ops *ops, int retval, struct cs_component_vals *v, int depth)
Wrapper around struct rtp_ops::cs_component.
Definition: arm_adi_v5.c:1811
#define DEVARCH_ROM_C_0X9
Definition: arm_adi_v5.c:1039
static const struct dap_part_nums * pidr_to_part_num(unsigned int designer_id, unsigned int part_num)
Definition: arm_adi_v5.c:1569
static void mem_ap_update_tar_cache(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:185
const struct command_registration dap_instance_commands[]
Definition: arm_adi_v5.c:2903
static int rtp_read_cs_regs(enum coresight_access_mode mode, struct adiv5_ap *ap, target_addr_t component_base, struct cs_component_vals *v)
Read the CoreSight registers needed during ROM Table Parsing (RTP).
Definition: arm_adi_v5.c:1347
#define CORESIGHT_COMPONENT_FOUND
Value used only during lookup of a CoreSight component in ROM table.
Definition: arm_adi_v5.c:1848
static uint32_t mem_ap_get_tar_increment(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:157
static struct adiv5_ap * _dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1155
int adiv5_jim_mem_ap_spot_configure(struct adiv5_mem_ap_spot *cfg, struct jim_getopt_info *goi)
Definition: arm_adi_v5.c:2495
static const struct @61 class0x9_devarch[]
static int rtp_cs_component(enum coresight_access_mode mode, const struct rtp_ops *ops, struct adiv5_ap *ap, target_addr_t dbgbase, bool *is_mem_ap, int depth)
Definition: arm_adi_v5.c:1932
static int mem_ap_setup_transfer_verify_size_packing_fallback(struct adiv5_ap *ap, unsigned int size, target_addr_t address, bool addrinc, bool pack, unsigned int *this_size)
Queue transactions setting up transfer parameters for the currently selected MEM-AP.
Definition: arm_adi_v5.c:445
int mem_ap_write_atomic_u32(struct adiv5_ap *ap, target_addr_t address, uint32_t value)
Synchronous write of a word to memory or a system register.
Definition: arm_adi_v5.c:318
This defines formats and data structures used to talk to ADIv5 entities.
static int dap_dp_poll_register(struct adiv5_dap *dap, unsigned int reg, uint32_t mask, uint32_t value, int timeout)
Definition: arm_adi_v5.h:674
#define AP_TYPE_MASK
Definition: arm_adi_v5.h:233
#define CSW_ADDRINC_MASK
Definition: arm_adi_v5.h:171
#define MEM_AP_REG_CSW(dap)
Definition: arm_adi_v5.h:145
#define CSW_256BIT
Definition: arm_adi_v5.h:170
#define CSW_ADDRINC_PACKED
Definition: arm_adi_v5.h:174
#define SSTICKYERR
Definition: arm_adi_v5.h:86
#define CSW_32BIT
Definition: arm_adi_v5.h:167
static bool is_64bit_ap(struct adiv5_ap *ap)
Definition: arm_adi_v5.h:511
static int dap_queue_dp_write(struct adiv5_dap *dap, unsigned int reg, uint32_t data)
Queue a DP register write.
Definition: arm_adi_v5.h:573
#define AP_REG_IDR_CLASS_SHIFT
Definition: arm_adi_v5.h:217
#define MEM_AP_REG_CFG(dap)
Definition: arm_adi_v5.h:155
#define CDBGPWRUPREQ
Definition: arm_adi_v5.h:93
ap_type
Definition: arm_adi_v5.h:487
@ AP_TYPE_APB_AP
Definition: arm_adi_v5.h:491
@ AP_TYPE_AXI_AP
Definition: arm_adi_v5.h:492
@ AP_TYPE_APB4_AP
Definition: arm_adi_v5.h:494
@ AP_TYPE_AHB3_AP
Definition: arm_adi_v5.h:490
@ AP_TYPE_COM_AP
Definition: arm_adi_v5.h:489
@ AP_TYPE_AHB5H_AP
Definition: arm_adi_v5.h:496
@ AP_TYPE_JTAG_AP
Definition: arm_adi_v5.h:488
@ AP_TYPE_AXI5_AP
Definition: arm_adi_v5.h:495
@ AP_TYPE_AHB5_AP
Definition: arm_adi_v5.h:493
#define CSW_64BIT
Definition: arm_adi_v5.h:168
static int dap_queue_ap_read(struct adiv5_ap *ap, unsigned int reg, uint32_t *data)
Queue an AP register read.
Definition: arm_adi_v5.h:590
#define CSW_AHB_DEFAULT
Definition: arm_adi_v5.h:193
#define MEM_AP_REG_TAR(dap)
Definition: arm_adi_v5.h:146
#define MEM_AP_REG_CFG_LD
Definition: arm_adi_v5.h:208
#define CSW_16BIT
Definition: arm_adi_v5.h:166
#define ARM_ID
Definition: arm_adi_v5.h:28
struct adiv5_dap * dap_instance_by_jim_obj(Jim_Interp *interp, Jim_Obj *o)
Definition: arm_dap.c:70
static int dap_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
Send an adi-v5 sequence to the DAP.
Definition: arm_adi_v5.h:536
#define AP_REG_IDR_CLASS_MASK
Definition: arm_adi_v5.h:216
adiv5_configure_dap_optional
Definition: arm_adi_v5.h:792
@ ADI_CONFIGURE_DAP_COMPULSORY
Definition: arm_adi_v5.h:793
#define CSW_ADDRINC_SINGLE
Definition: arm_adi_v5.h:173
#define AP_REG_IDR_CLASS_MEM_AP
Definition: arm_adi_v5.h:225
#define DP_APSEL_INVALID
Definition: arm_adi_v5.h:110
#define MEM_AP_REG_CFG_BE
Definition: arm_adi_v5.h:206
#define DP_CTRL_STAT
Definition: arm_adi_v5.h:50
#define DP_BASEPTR0_VALID
Definition: arm_adi_v5.h:79
#define DP_APSEL_MAX
Definition: arm_adi_v5.h:109
#define CORUNDETECT
Definition: arm_adi_v5.h:82
#define CDBGPWRUPACK
Definition: arm_adi_v5.h:94
#define CSW_128BIT
Definition: arm_adi_v5.h:169
struct adiv5_dap * adiv5_get_dap(struct arm_dap_object *obj)
Definition: arm_dap.c:66
#define CSW_8BIT
Definition: arm_adi_v5.h:165
static int dap_queue_dp_read(struct adiv5_dap *dap, unsigned int reg, uint32_t *data)
Queue a DP register read.
Definition: arm_adi_v5.h:555
#define SSTICKYORUN
Definition: arm_adi_v5.h:83
#define CSYSPWRUPACK
Definition: arm_adi_v5.h:96
#define MEM_AP_REG_CFG_LA
Definition: arm_adi_v5.h:207
#define MEM_AP_REG_DRW(dap)
Definition: arm_adi_v5.h:148
#define DP_BASEPTR1
Definition: arm_adi_v5.h:49
static int dap_dp_read_atomic(struct adiv5_dap *dap, unsigned int reg, uint32_t *value)
Definition: arm_adi_v5.h:662
#define MEM_AP_REG_BASE(dap)
Definition: arm_adi_v5.h:156
#define CSW_ADDRINC_OFF
Definition: arm_adi_v5.h:172
static int dap_queue_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
Queue an AP register write.
Definition: arm_adi_v5.h:610
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define CSYSPWRUPREQ
Definition: arm_adi_v5.h:95
#define MEM_AP_REG_BASE64(dap)
Definition: arm_adi_v5.h:154
const char * adiv5_dap_name(struct adiv5_dap *self)
Definition: arm_dap.c:54
#define DP_BASEPTR0
Definition: arm_adi_v5.h:48
#define MEM_AP_REG_BD0(dap)
Definition: arm_adi_v5.h:149
#define CSW_SIZE_MASK
Definition: arm_adi_v5.h:164
static bool is_adiv6(const struct adiv5_dap *dap)
Check if DAP is ADIv6.
Definition: arm_adi_v5.h:523
#define MEM_AP_REG_TAR64(dap)
Definition: arm_adi_v5.h:147
static int dap_run(struct adiv5_dap *dap)
Perform all queued DAP operations, and clear any errors posted in the CTRL_STAT register when they ar...
Definition: arm_adi_v5.h:648
#define AP_REG_IDR(dap)
Definition: arm_adi_v5.h:161
#define MEM_AP_REG_CFG_INVALID
Definition: arm_adi_v5.h:209
#define ARM_CS_CIDR3
Definition: arm_coresight.h:44
#define ARM_CS_CLASS_0X1_ROM_TABLE
Definition: arm_coresight.h:48
#define ARM_CS_PIDR2
Definition: arm_coresight.h:21
#define ARM_CS_C9_DEVID_SYSMEM_MASK
Definition: arm_coresight.h:76
static bool is_valid_arm_cs_cidr(uint32_t cidr)
Definition: arm_coresight.h:51
#define ARM_CS_C9_DEVTYPE_MASK
Definition: arm_coresight.h:87
#define ARM_CS_PIDR3
Definition: arm_coresight.h:22
#define ARM_CS_PIDR_SIZE(pidr)
Definition: arm_coresight.h:39
#define ARM_CS_CLASS_0X9_CS_COMPONENT
Definition: arm_coresight.h:49
#define ARM_CS_C9_DEVID
Definition: arm_coresight.h:71
#define ARM_CS_CIDR1
Definition: arm_coresight.h:42
#define ARM_CS_C9_DEVARCH
Definition: arm_coresight.h:57
#define ARM_CS_PIDR0
Definition: arm_coresight.h:19
#define ARM_CS_C9_DEVTYPE_SUB_SHIFT
Definition: arm_coresight.h:85
#define ARM_CS_CIDR_CLASS(cidr)
Definition: arm_coresight.h:47
#define ARM_CS_C9_DEVARCH_REVISION(devarch)
Definition: arm_coresight.h:66
#define ARM_CS_ROMENTRY_PRESENT
Definition: arm_coresight.h:97
#define ARM_CS_PIDR4
Definition: arm_coresight.h:23
#define ARM_CS_ALIGN
Definition: arm_coresight.h:16
#define ARM_CS_C9_DEVTYPE_MAJOR_SHIFT
Definition: arm_coresight.h:83
#define ARM_CS_C9_DEVTYPE
Definition: arm_coresight.h:80
#define ARM_CS_C9_DEVTYPE_MAJOR_MASK
Definition: arm_coresight.h:82
#define ARM_CS_C1_MEMTYPE_SYSMEM_MASK
Definition: arm_coresight.h:93
#define ARM_CS_C9_DEVID_FORMAT_MASK
Definition: arm_coresight.h:73
#define ARM_CS_ROMENTRY_OFFSET_MASK
Definition: arm_coresight.h:98
#define ARM_CS_C9_DEVARCH_ARCHITECT(devarch)
Definition: arm_coresight.h:68
#define ARM_CS_PIDR_JEDEC
Definition: arm_coresight.h:38
#define ARM_CS_PIDR_PART(pidr)
Definition: arm_coresight.h:32
#define ARM_CS_CIDR0
Definition: arm_coresight.h:41
#define ARM_CS_C9_DEVTYPE_SUB_MASK
Definition: arm_coresight.h:84
#define ARM_CS_C9_DEVID_FORMAT_64BIT
Definition: arm_coresight.h:75
#define ARM_CS_CIDR2
Definition: arm_coresight.h:43
#define ARM_CS_PIDR1
Definition: arm_coresight.h:20
#define ARM_CS_C9_DEVARCH_PRESENT
Definition: arm_coresight.h:63
#define ARM_CS_PIDR_DESIGNER(pidr)
Definition: arm_coresight.h:33
enum arm_mode mode
Definition: armv4_5.c:277
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_DATA
Use this macro to access the invoked command handler's data pointer, rather than accessing the variab...
Definition: command.h:176
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:442
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
unsigned short width
Definition: embeddedice.c:47
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
static const char * jep106_manufacturer(unsigned int manufacturer)
Definition: jep106.h:21
int jim_getopt_wide(struct jim_getopt_info *goi, jim_wide *puthere)
Remove argv[0] as wide.
Definition: jim-nvp.c:222
int jim_nvp_name2value_obj(Jim_Interp *interp, const struct jim_nvp *p, Jim_Obj *o, struct jim_nvp **result)
Definition: jim-nvp.c:66
int jim_getopt_obj(struct jim_getopt_info *goi, Jim_Obj **puthere)
Remove argv[0] from the list.
Definition: jim-nvp.c:169
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
#define MIN(a, b)
Definition: replacements.h:22
#define MAX(a, b)
Definition: replacements.h:25
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
#define BIT(nr)
Definition: stm32l4x.h:18
This represents an ARM Debug Interface (v5) Access Port (AP).
Definition: arm_adi_v5.h:250
uint32_t csw_size_supported_mask
Save the supported CSW.Size data types for the MEM-AP.
Definition: arm_adi_v5.h:286
bool unaligned_access_bad
Definition: arm_adi_v5.h:316
bool config_ap_never_release
Definition: arm_adi_v5.h:328
bool packed_transfers_probed
Definition: arm_adi_v5.h:313
bool tar_valid
Definition: arm_adi_v5.h:319
bool packed_transfers_supported
Definition: arm_adi_v5.h:312
uint32_t tar_autoincr_block
Definition: arm_adi_v5.h:309
unsigned int refcount
Definition: arm_adi_v5.h:325
uint32_t csw_size_probed_mask
Probed CSW.Size data types for the MEM-AP.
Definition: arm_adi_v5.h:293
uint64_t ap_num
ADIv5: Number of this AP (0~255) ADIv6: Base address of this AP (4k aligned) TODO: to be more coheren...
Definition: arm_adi_v5.h:261
struct adiv5_dap * dap
DAP this AP belongs to.
Definition: arm_adi_v5.h:254
uint32_t memaccess_tck
Configures how many extra tck clocks are added after starting a MEM-AP access before we try to read i...
Definition: arm_adi_v5.h:306
uint32_t cfg_reg
Definition: arm_adi_v5.h:322
uint32_t csw_default
Default value for (MEM-AP) AP_REG_CSW register.
Definition: arm_adi_v5.h:266
target_addr_t tar_value
Cache for (MEM-AP) AP_REG_TAR register value This is written to configure the address being read or w...
Definition: arm_adi_v5.h:300
uint32_t csw_value
Cache for (MEM-AP) AP_REG_CSW register value.
Definition: arm_adi_v5.h:273
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
Definition: arm_adi_v5.h:348
bool ti_be_32_quirks
Definition: arm_adi_v5.h:398
bool select_valid
Validity of DP SELECT cache.
Definition: arm_adi_v5.h:372
bool select1_valid
Definition: arm_adi_v5.h:373
struct adiv5_ap ap[DP_APSEL_MAX+1]
Definition: arm_adi_v5.h:364
bool select_dpbanksel_valid
Partial DPBANKSEL validity for SWD only.
Definition: arm_adi_v5.h:383
uint32_t dp_ctrl_stat
Definition: arm_adi_v5.h:362
bool do_reconnect
Signals that an attempt to reestablish communication afresh should be performed before the next acces...
Definition: arm_adi_v5.h:414
const struct dap_ops * ops
Definition: arm_adi_v5.h:349
uint32_t * last_read
Holds the pointer to the destination word for the last queued read, for use with posted AP read seque...
Definition: arm_adi_v5.h:392
uint64_t apsel
Definition: arm_adi_v5.h:367
struct jtag_tap * tap
Definition: arm_adi_v5.h:360
uint64_t select
Cache for DP SELECT and SELECT1 (ADIv6) register.
Definition: arm_adi_v5.h:370
unsigned int asize
Definition: arm_adi_v5.h:436
bool ignore_syspwrupack
Flag saying whether to ignore the syspwrupack flag in DAP.
Definition: arm_adi_v5.h:418
bool nu_npcx_quirks
Definition: arm_adi_v5.h:402
struct adiv5_dap * dap
Definition: arm_adi_v5.h:803
struct adiv5_dap * dap
Definition: arm_adi_v5.h:787
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:235
Holds registers and coordinates of a CoreSight component.
Definition: arm_adi_v5.c:1304
struct adiv5_ap * ap
Definition: arm_adi_v5.c:1305
enum coresight_access_mode mode
Definition: arm_adi_v5.c:1312
target_addr_t component_base
Definition: arm_adi_v5.c:1306
uint32_t devtype_memtype
Definition: arm_adi_v5.c:1311
unsigned int type
Definition: arm_adi_v5.c:2251
uint64_t component_base
Definition: arm_adi_v5.c:2253
uint64_t ap_num
Definition: arm_adi_v5.c:2254
unsigned int idx
Definition: arm_adi_v5.c:2250
int(* connect)(struct adiv5_dap *dap)
connect operation for SWD
Definition: arm_adi_v5.h:451
uint16_t part_num
Definition: arm_adi_v5.c:1428
const char * type
Definition: arm_adi_v5.c:1429
uint16_t designer_id
Definition: arm_adi_v5.c:1427
const char * full
Definition: arm_adi_v5.c:1430
A TCL -ish GetOpt like code.
Definition: jim-nvp.h:135
Jim_Interp * interp
Definition: jim-nvp.h:136
Jim_Obj *const * argv
Definition: jim-nvp.h:138
Name Value Pairs, aka: NVP.
Definition: jim-nvp.h:59
const char * name
Definition: jim-nvp.h:60
int value
Definition: jim-nvp.h:61
Definition: register.h:111
Actions/operations to be executed while parsing ROM tables.
Definition: arm_adi_v5.c:1730
void * priv
Private data.
Definition: arm_adi_v5.c:1776
int(* mem_ap_header)(int retval, struct adiv5_ap *ap, uint64_t dbgbase, uint32_t apid, int depth, void *priv)
Executed at the start of a new MEM-AP, typically to print the MEM-AP header.
Definition: arm_adi_v5.c:1749
int(* rom_table_entry)(int retval, int depth, unsigned int offset, uint64_t romentry, void *priv)
Executed for each entry of a ROM table, typically to print the entry and information about validity o...
Definition: arm_adi_v5.c:1771
int(* cs_component)(int retval, struct cs_component_vals *v, int depth, void *priv)
Executed when a CoreSight component is parsed, typically to print information on the component.
Definition: arm_adi_v5.c:1760
int(* ap_header)(struct adiv5_ap *ap, int depth, void *priv)
Executed at the start of a new AP, typically to print the AP header.
Definition: arm_adi_v5.c:1738
Definition: target.h:116
struct jtag_tap * tap
Definition: target.h:119
void * private_config
Definition: target.h:165
bool dap_configured
Definition: target.h:179
bool has_dap
Definition: target.h:178
bool tap_configured
Definition: target.h:180
#define true
Definition: system.h:66
#define ERROR_TARGET_SIZE_NOT_SUPPORTED
Definition: target.h:800
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:792
#define ERROR_TARGET_PACKING_NOT_SUPPORTED
Definition: target.h:801
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:794
#define TARGET_ADDR_FMT
Definition: types.h:342
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
uint64_t target_addr_t
Definition: types.h:335
static struct ublast_lowlevel low
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22