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