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  { 0x575, 0x132, "STAR SCS", "(System Control Space)", },
1598  { 0x575, 0x4d2, "Cortex-M52 ROM", "(ROM Table)", },
1599  { 0x575, 0xd24, "Cortex-M52 Debug", "(Debug Unit)", },
1600 };
1601 
1602 static const struct dap_part_nums *pidr_to_part_num(unsigned int designer_id, unsigned int part_num)
1603 {
1604  static const struct dap_part_nums unknown = {
1605  .type = "Unrecognized",
1606  .full = "",
1607  };
1608 
1609  for (unsigned int i = 0; i < ARRAY_SIZE(dap_part_nums); i++)
1611  return &dap_part_nums[i];
1612 
1613  return &unknown;
1614 }
1615 
1616 static int dap_devtype_display(struct command_invocation *cmd, uint32_t devtype)
1617 {
1618  const char *major = "Reserved", *subtype = "Reserved";
1619  const unsigned int minor = (devtype & ARM_CS_C9_DEVTYPE_SUB_MASK) >> ARM_CS_C9_DEVTYPE_SUB_SHIFT;
1620  const unsigned int devtype_major = (devtype & ARM_CS_C9_DEVTYPE_MAJOR_MASK) >> ARM_CS_C9_DEVTYPE_MAJOR_SHIFT;
1621  switch (devtype_major) {
1622  case 0:
1623  major = "Miscellaneous";
1624  switch (minor) {
1625  case 0:
1626  subtype = "other";
1627  break;
1628  case 4:
1629  subtype = "Validation component";
1630  break;
1631  }
1632  break;
1633  case 1:
1634  major = "Trace Sink";
1635  switch (minor) {
1636  case 0:
1637  subtype = "other";
1638  break;
1639  case 1:
1640  subtype = "Port";
1641  break;
1642  case 2:
1643  subtype = "Buffer";
1644  break;
1645  case 3:
1646  subtype = "Router";
1647  break;
1648  }
1649  break;
1650  case 2:
1651  major = "Trace Link";
1652  switch (minor) {
1653  case 0:
1654  subtype = "other";
1655  break;
1656  case 1:
1657  subtype = "Funnel, router";
1658  break;
1659  case 2:
1660  subtype = "Filter";
1661  break;
1662  case 3:
1663  subtype = "FIFO, buffer";
1664  break;
1665  }
1666  break;
1667  case 3:
1668  major = "Trace Source";
1669  switch (minor) {
1670  case 0:
1671  subtype = "other";
1672  break;
1673  case 1:
1674  subtype = "Processor";
1675  break;
1676  case 2:
1677  subtype = "DSP";
1678  break;
1679  case 3:
1680  subtype = "Engine/Coprocessor";
1681  break;
1682  case 4:
1683  subtype = "Bus";
1684  break;
1685  case 6:
1686  subtype = "Software";
1687  break;
1688  }
1689  break;
1690  case 4:
1691  major = "Debug Control";
1692  switch (minor) {
1693  case 0:
1694  subtype = "other";
1695  break;
1696  case 1:
1697  subtype = "Trigger Matrix";
1698  break;
1699  case 2:
1700  subtype = "Debug Auth";
1701  break;
1702  case 3:
1703  subtype = "Power Requestor";
1704  break;
1705  }
1706  break;
1707  case 5:
1708  major = "Debug Logic";
1709  switch (minor) {
1710  case 0:
1711  subtype = "other";
1712  break;
1713  case 1:
1714  subtype = "Processor";
1715  break;
1716  case 2:
1717  subtype = "DSP";
1718  break;
1719  case 3:
1720  subtype = "Engine/Coprocessor";
1721  break;
1722  case 4:
1723  subtype = "Bus";
1724  break;
1725  case 5:
1726  subtype = "Memory";
1727  break;
1728  }
1729  break;
1730  case 6:
1731  major = "Performance Monitor";
1732  switch (minor) {
1733  case 0:
1734  subtype = "other";
1735  break;
1736  case 1:
1737  subtype = "Processor";
1738  break;
1739  case 2:
1740  subtype = "DSP";
1741  break;
1742  case 3:
1743  subtype = "Engine/Coprocessor";
1744  break;
1745  case 4:
1746  subtype = "Bus";
1747  break;
1748  case 5:
1749  subtype = "Memory";
1750  break;
1751  }
1752  break;
1753  }
1754  command_print(cmd, "\t\tType is 0x%02x, %s, %s",
1755  devtype & ARM_CS_C9_DEVTYPE_MASK,
1756  major, subtype);
1757  return ERROR_OK;
1758 }
1759 
1763 struct rtp_ops {
1771  int (*ap_header)(struct adiv5_ap *ap, int depth, void *priv);
1782  int (*mem_ap_header)(int retval, struct adiv5_ap *ap, uint64_t dbgbase,
1783  uint32_t apid, int depth, void *priv);
1793  int (*cs_component)(int retval, struct cs_component_vals *v, int depth, void *priv);
1804  int (*rom_table_entry)(int retval, int depth, unsigned int offset, uint64_t romentry,
1805  void *priv);
1809  void *priv;
1810 };
1811 
1815 static int rtp_ops_ap_header(const struct rtp_ops *ops,
1816  struct adiv5_ap *ap, int depth)
1817 {
1818  if (ops->ap_header)
1819  return ops->ap_header(ap, depth, ops->priv);
1820 
1821  return ERROR_OK;
1822 }
1823 
1828 static int rtp_ops_mem_ap_header(const struct rtp_ops *ops,
1829  int retval, struct adiv5_ap *ap, uint64_t dbgbase, uint32_t apid, int depth)
1830 {
1831  if (!ops->mem_ap_header)
1832  return retval;
1833 
1834  int retval1 = ops->mem_ap_header(retval, ap, dbgbase, apid, depth, ops->priv);
1835  if (retval != ERROR_OK)
1836  return retval;
1837  return retval1;
1838 }
1839 
1844 static int rtp_ops_cs_component(const struct rtp_ops *ops,
1845  int retval, struct cs_component_vals *v, int depth)
1846 {
1847  if (!ops->cs_component)
1848  return retval;
1849 
1850  int retval1 = ops->cs_component(retval, v, depth, ops->priv);
1851  if (retval != ERROR_OK)
1852  return retval;
1853  return retval1;
1854 }
1855 
1860 static int rtp_ops_rom_table_entry(const struct rtp_ops *ops,
1861  int retval, int depth, unsigned int offset, uint64_t romentry)
1862 {
1863  if (!ops->rom_table_entry)
1864  return retval;
1865 
1866  int retval1 = ops->rom_table_entry(retval, depth, offset, romentry, ops->priv);
1867  if (retval != ERROR_OK)
1868  return retval;
1869  return retval1;
1870 }
1871 
1872 /* Broken ROM tables can have circular references. Stop after a while */
1873 #define ROM_TABLE_MAX_DEPTH (16)
1874 
1881 #define CORESIGHT_COMPONENT_FOUND (1)
1882 
1883 static int rtp_ap(const struct rtp_ops *ops, struct adiv5_ap *ap, int depth);
1884 static int rtp_cs_component(enum coresight_access_mode mode, const struct rtp_ops *ops,
1885  struct adiv5_ap *ap, target_addr_t dbgbase, bool *is_mem_ap, int depth);
1886 
1887 static int rtp_rom_loop(enum coresight_access_mode mode, const struct rtp_ops *ops,
1888  struct adiv5_ap *ap, target_addr_t base_address, int depth,
1889  unsigned int width, unsigned int max_entries)
1890 {
1891  /* ADIv6 AP ROM table provide offset from current AP */
1892  if (mode == CS_ACCESS_AP)
1893  base_address = ap->ap_num;
1894 
1895  assert(IS_ALIGNED(base_address, ARM_CS_ALIGN));
1896 
1897  unsigned int offset = 0;
1898  while (max_entries--) {
1899  uint64_t romentry;
1900  uint32_t romentry_low, romentry_high;
1902  unsigned int saved_offset = offset;
1903 
1904  int retval = dap_queue_read_reg(mode, ap, base_address, offset, &romentry_low);
1905  offset += 4;
1906  if (retval == ERROR_OK && width == 64) {
1907  retval = dap_queue_read_reg(mode, ap, base_address, offset, &romentry_high);
1908  offset += 4;
1909  }
1910  if (retval == ERROR_OK)
1911  retval = dap_run(ap->dap);
1912  if (retval != ERROR_OK) {
1913  LOG_DEBUG("Failed read ROM table entry");
1914  return retval;
1915  }
1916 
1917  if (width == 64) {
1918  romentry = (((uint64_t)romentry_high) << 32) | romentry_low;
1919  component_base = base_address +
1920  ((((uint64_t)romentry_high) << 32) | (romentry_low & ARM_CS_ROMENTRY_OFFSET_MASK));
1921  } else {
1922  romentry = romentry_low;
1923  /* "romentry" is signed */
1924  component_base = base_address + (int32_t)(romentry_low & ARM_CS_ROMENTRY_OFFSET_MASK);
1925  if (!is_64bit_ap(ap))
1926  component_base = (uint32_t)component_base;
1927  }
1928  retval = rtp_ops_rom_table_entry(ops, retval, depth, saved_offset, romentry);
1929  if (retval != ERROR_OK)
1930  return retval;
1931 
1932  if (romentry == 0) {
1933  /* End of ROM table */
1934  break;
1935  }
1936 
1937  if (!(romentry & ARM_CS_ROMENTRY_PRESENT))
1938  continue;
1939 
1940  /* Recurse */
1941  if (mode == CS_ACCESS_AP) {
1942  struct adiv5_ap *next_ap = dap_get_ap(ap->dap, component_base);
1943  if (!next_ap) {
1944  LOG_DEBUG("Wrong AP # 0x%" PRIx64, component_base);
1945  continue;
1946  }
1947  retval = rtp_ap(ops, next_ap, depth + 1);
1948  dap_put_ap(next_ap);
1949  } else {
1950  /* mode == CS_ACCESS_MEM_AP */
1951  retval = rtp_cs_component(mode, ops, ap, component_base, NULL, depth + 1);
1952  }
1953  if (retval == CORESIGHT_COMPONENT_FOUND)
1955  if (retval != ERROR_OK) {
1956  /* TODO: do we need to send an ABORT before continuing? */
1957  LOG_DEBUG("Ignore error parsing CoreSight component");
1958  continue;
1959  }
1960  }
1961 
1962  return ERROR_OK;
1963 }
1964 
1965 static int rtp_cs_component(enum coresight_access_mode mode, const struct rtp_ops *ops,
1966  struct adiv5_ap *ap, target_addr_t base_address, bool *is_mem_ap, int depth)
1967 {
1968  struct cs_component_vals v;
1969  int retval;
1970 
1971  assert(IS_ALIGNED(base_address, ARM_CS_ALIGN));
1972 
1973  if (is_mem_ap)
1974  *is_mem_ap = false;
1975 
1976  if (depth > ROM_TABLE_MAX_DEPTH)
1977  retval = ERROR_FAIL;
1978  else
1979  retval = rtp_read_cs_regs(mode, ap, base_address, &v);
1980 
1981  retval = rtp_ops_cs_component(ops, retval, &v, depth);
1982  if (retval == CORESIGHT_COMPONENT_FOUND)
1984  if (retval != ERROR_OK)
1985  return ERROR_OK; /* Don't abort recursion */
1986 
1987  if (!is_valid_arm_cs_cidr(v.cid))
1988  return ERROR_OK; /* Don't abort recursion */
1989 
1990  const unsigned int class = ARM_CS_CIDR_CLASS(v.cid);
1991 
1992  if (class == ARM_CS_CLASS_0X1_ROM_TABLE)
1993  return rtp_rom_loop(mode, ops, ap, base_address, depth, 32, 960);
1994 
1995  if (class == ARM_CS_CLASS_0X9_CS_COMPONENT) {
1996  if ((v.devarch & ARM_CS_C9_DEVARCH_PRESENT) == 0)
1997  return ERROR_OK;
1998 
1999  if (is_mem_ap) {
2000  if ((v.devarch & DEVARCH_ID_MASK) == DEVARCH_MEM_AP)
2001  *is_mem_ap = true;
2002 
2003  /* SoC-600 APv1 Adapter */
2006  ARM_CS_PIDR_PART(v.pid) == 0x9e5)
2007  *is_mem_ap = true;
2008  }
2009 
2010  /* quit if not ROM table */
2012  return ERROR_OK;
2013 
2015  return rtp_rom_loop(mode, ops, ap, base_address, depth, 64, 256);
2016  else
2017  return rtp_rom_loop(mode, ops, ap, base_address, depth, 32, 512);
2018  }
2019 
2020  /* Class other than 0x1 and 0x9 */
2021  return ERROR_OK;
2022 }
2023 
2024 static int rtp_ap(const struct rtp_ops *ops, struct adiv5_ap *ap, int depth)
2025 {
2026  uint32_t apid;
2027  target_addr_t dbgbase, invalid_entry;
2028 
2029  int retval = rtp_ops_ap_header(ops, ap, depth);
2030  if (retval != ERROR_OK || depth > ROM_TABLE_MAX_DEPTH)
2031  return ERROR_OK; /* Don't abort recursion */
2032 
2033  if (is_adiv6(ap->dap)) {
2034  bool is_mem_ap;
2035  retval = rtp_cs_component(CS_ACCESS_AP, ops, ap, 0, &is_mem_ap, depth);
2036  if (retval == CORESIGHT_COMPONENT_FOUND)
2038  if (retval != ERROR_OK)
2039  return ERROR_OK; /* Don't abort recursion */
2040 
2041  if (!is_mem_ap)
2042  return ERROR_OK;
2043  /* Continue for an ADIv6 MEM-AP or SoC-600 APv1 Adapter */
2044  }
2045 
2046  /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
2047  retval = dap_get_debugbase(ap, &dbgbase, &apid);
2048  if (retval != ERROR_OK)
2049  return retval;
2050  retval = rtp_ops_mem_ap_header(ops, retval, ap, dbgbase, apid, depth);
2051  if (retval != ERROR_OK)
2052  return retval;
2053 
2054  if (apid == 0)
2055  return ERROR_FAIL;
2056 
2057  /* NOTE: a MEM-AP may have a single CoreSight component that's
2058  * not a ROM table ... or have no such components at all.
2059  */
2060  const unsigned int class = (apid & AP_REG_IDR_CLASS_MASK) >> AP_REG_IDR_CLASS_SHIFT;
2061 
2062  if (class == AP_REG_IDR_CLASS_MEM_AP) {
2063  if (is_64bit_ap(ap))
2064  invalid_entry = 0xFFFFFFFFFFFFFFFFull;
2065  else
2066  invalid_entry = 0xFFFFFFFFul;
2067 
2068  if (dbgbase != invalid_entry && (dbgbase & 0x3) != 0x2) {
2069  retval = rtp_cs_component(CS_ACCESS_MEM_AP, ops, ap,
2070  dbgbase & 0xFFFFFFFFFFFFF000ull, NULL, depth);
2071  if (retval == CORESIGHT_COMPONENT_FOUND)
2073  }
2074  }
2075 
2076  return ERROR_OK;
2077 }
2078 
2079 /* Actions for command "dap info" */
2080 
2081 static int dap_info_ap_header(struct adiv5_ap *ap, int depth, void *priv)
2082 {
2083  struct command_invocation *cmd = priv;
2084 
2085  if (depth > ROM_TABLE_MAX_DEPTH) {
2086  command_print(cmd, "\tTables too deep");
2087  return ERROR_FAIL;
2088  }
2089 
2090  command_print(cmd, "%sAP # 0x%" PRIx64, (depth) ? "\t\t" : "", ap->ap_num);
2091  return ERROR_OK;
2092 }
2093 
2094 static int dap_info_mem_ap_header(int retval, struct adiv5_ap *ap,
2095  target_addr_t dbgbase, uint32_t apid, int depth, void *priv)
2096 {
2097  struct command_invocation *cmd = priv;
2098  target_addr_t invalid_entry;
2099  char tabs[17] = "";
2100 
2101  if (retval != ERROR_OK) {
2102  command_print(cmd, "\t\tCan't read MEM-AP, the corresponding core might be turned off");
2103  return retval;
2104  }
2105 
2106  if (depth > ROM_TABLE_MAX_DEPTH) {
2107  command_print(cmd, "\tTables too deep");
2108  return ERROR_FAIL;
2109  }
2110 
2111  if (depth)
2112  snprintf(tabs, sizeof(tabs), "\t[L%02d] ", depth);
2113 
2114  command_print(cmd, "\t\tAP ID register 0x%8.8" PRIx32, apid);
2115  if (apid == 0) {
2116  command_print(cmd, "\t\tNo AP found at this AP#0x%" PRIx64, ap->ap_num);
2117  return ERROR_FAIL;
2118  }
2119 
2120  command_print(cmd, "\t\tType is %s", ap_type_to_description(apid & AP_TYPE_MASK));
2121 
2122  /* NOTE: a MEM-AP may have a single CoreSight component that's
2123  * not a ROM table ... or have no such components at all.
2124  */
2125  const unsigned int class = (apid & AP_REG_IDR_CLASS_MASK) >> AP_REG_IDR_CLASS_SHIFT;
2126 
2127  if (class == AP_REG_IDR_CLASS_MEM_AP) {
2128  if (is_64bit_ap(ap))
2129  invalid_entry = 0xFFFFFFFFFFFFFFFFull;
2130  else
2131  invalid_entry = 0xFFFFFFFFul;
2132 
2133  command_print(cmd, "%sMEM-AP BASE " TARGET_ADDR_FMT, tabs, dbgbase);
2134 
2135  if (dbgbase == invalid_entry || (dbgbase & 0x3) == 0x2) {
2136  command_print(cmd, "\t\tNo ROM table present");
2137  } else {
2138  if (dbgbase & 0x01)
2139  command_print(cmd, "\t\tValid ROM table present");
2140  else
2141  command_print(cmd, "\t\tROM table in legacy format");
2142  }
2143  }
2144 
2145  return ERROR_OK;
2146 }
2147 
2148 static int dap_info_cs_component(int retval, struct cs_component_vals *v, int depth, void *priv)
2149 {
2150  struct command_invocation *cmd = priv;
2151 
2152  if (depth > ROM_TABLE_MAX_DEPTH) {
2153  command_print(cmd, "\tTables too deep");
2154  return ERROR_FAIL;
2155  }
2156 
2157  if (v->mode == CS_ACCESS_MEM_AP)
2158  command_print(cmd, "\t\tComponent base address " TARGET_ADDR_FMT, v->component_base);
2159 
2160  if (retval != ERROR_OK) {
2161  command_print(cmd, "\t\tCan't read component, the corresponding core might be turned off");
2162  return retval;
2163  }
2164 
2165  if (!is_valid_arm_cs_cidr(v->cid)) {
2166  command_print(cmd, "\t\tInvalid CID 0x%08" PRIx32, v->cid);
2167  return ERROR_OK; /* Don't abort recursion */
2168  }
2169 
2170  /* component may take multiple 4K pages */
2171  uint32_t size = ARM_CS_PIDR_SIZE(v->pid);
2172  if (size > 0)
2173  command_print(cmd, "\t\tStart address " TARGET_ADDR_FMT, v->component_base - 0x1000 * size);
2174 
2175  command_print(cmd, "\t\tPeripheral ID 0x%010" PRIx64, v->pid);
2176 
2177  const unsigned int part_num = ARM_CS_PIDR_PART(v->pid);
2178  unsigned int designer_id = ARM_CS_PIDR_DESIGNER(v->pid);
2179 
2180  if (v->pid & ARM_CS_PIDR_JEDEC) {
2181  /* JEP106 code */
2182  command_print(cmd, "\t\tDesigner is 0x%03x, %s",
2183  designer_id, jep106_manufacturer(designer_id));
2184  } else {
2185  /* Legacy ASCII ID, clear invalid bits */
2186  designer_id &= 0x7f;
2187  command_print(cmd, "\t\tDesigner ASCII code 0x%02x, %s",
2188  designer_id, designer_id == 0x41 ? "ARM" : "<unknown>");
2189  }
2190 
2191  const struct dap_part_nums *partnum = pidr_to_part_num(designer_id, part_num);
2192  command_print(cmd, "\t\tPart is 0x%03x, %s %s", part_num, partnum->type, partnum->full);
2193 
2194  const unsigned int class = ARM_CS_CIDR_CLASS(v->cid);
2195  command_print(cmd, "\t\tComponent class is 0x%x, %s", class, class_description[class]);
2196 
2197  if (class == ARM_CS_CLASS_0X1_ROM_TABLE) {
2199  command_print(cmd, "\t\tMEMTYPE system memory present on bus");
2200  else
2201  command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
2202  return ERROR_OK;
2203  }
2204 
2205  if (class == ARM_CS_CLASS_0X9_CS_COMPONENT) {
2207 
2208  /* REVISIT also show ARM_CS_C9_DEVID */
2209 
2210  if ((v->devarch & ARM_CS_C9_DEVARCH_PRESENT) == 0)
2211  return ERROR_OK;
2212 
2213  unsigned int architect_id = ARM_CS_C9_DEVARCH_ARCHITECT(v->devarch);
2214  unsigned int revision = ARM_CS_C9_DEVARCH_REVISION(v->devarch);
2215  command_print(cmd, "\t\tDev Arch is 0x%08" PRIx32 ", %s \"%s\" rev.%u", v->devarch,
2217  revision);
2218 
2219  if ((v->devarch & DEVARCH_ID_MASK) == DEVARCH_ROM_C_0X9) {
2220  command_print(cmd, "\t\tType is ROM table");
2221 
2223  command_print(cmd, "\t\tMEMTYPE system memory present on bus");
2224  else
2225  command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
2226  }
2227  return ERROR_OK;
2228  }
2229 
2230  /* Class other than 0x1 and 0x9 */
2231  return ERROR_OK;
2232 }
2233 
2234 static int dap_info_rom_table_entry(int retval, int depth,
2235  unsigned int offset, uint64_t romentry, void *priv)
2236 {
2237  struct command_invocation *cmd = priv;
2238  char tabs[16] = "";
2239 
2240  if (depth)
2241  snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
2242 
2243  if (retval != ERROR_OK) {
2244  command_print(cmd, "\t%sROMTABLE[0x%x] Read error", tabs, offset);
2245  command_print(cmd, "\t\tUnable to continue");
2246  command_print(cmd, "\t%s\tStop parsing of ROM table", tabs);
2247  return retval;
2248  }
2249 
2250  command_print(cmd, "\t%sROMTABLE[0x%x] = 0x%08" PRIx64,
2251  tabs, offset, romentry);
2252 
2253  if (romentry == 0) {
2254  command_print(cmd, "\t%s\tEnd of ROM table", tabs);
2255  return ERROR_OK;
2256  }
2257 
2258  if (!(romentry & ARM_CS_ROMENTRY_PRESENT)) {
2259  command_print(cmd, "\t\tComponent not present");
2260  return ERROR_OK;
2261  }
2262 
2263  return ERROR_OK;
2264 }
2265 
2267 {
2268  struct rtp_ops dap_info_ops = {
2270  .mem_ap_header = dap_info_mem_ap_header,
2271  .cs_component = dap_info_cs_component,
2272  .rom_table_entry = dap_info_rom_table_entry,
2273  .priv = cmd,
2274  };
2275 
2276  return rtp_ap(&dap_info_ops, ap, 0);
2277 }
2278 
2279 /* Actions for dap_lookup_cs_component() */
2280 
2282  /* input */
2283  unsigned int idx;
2284  unsigned int type;
2285  /* output */
2286  uint64_t component_base;
2287  uint64_t ap_num;
2288 };
2289 
2291  struct cs_component_vals *v, int depth, void *priv)
2292 {
2293  struct dap_lookup_data *lookup = priv;
2294 
2295  if (retval != ERROR_OK)
2296  return retval;
2297 
2298  if (!is_valid_arm_cs_cidr(v->cid))
2299  return ERROR_OK;
2300 
2301  const unsigned int class = ARM_CS_CIDR_CLASS(v->cid);
2302  if (class != ARM_CS_CLASS_0X9_CS_COMPONENT)
2303  return ERROR_OK;
2304 
2305  if ((v->devtype_memtype & ARM_CS_C9_DEVTYPE_MASK) != lookup->type)
2306  return ERROR_OK;
2307 
2308  if (lookup->idx) {
2309  /* search for next one */
2310  --lookup->idx;
2311  return ERROR_OK;
2312  }
2313 
2314  /* Found! */
2315  lookup->component_base = v->component_base;
2316  lookup->ap_num = v->ap->ap_num;
2318 }
2319 
2320 int dap_lookup_cs_component(struct adiv5_ap *ap, uint8_t type,
2321  target_addr_t *addr, int32_t core_id)
2322 {
2323  struct dap_lookup_data lookup = {
2324  .type = type,
2325  .idx = core_id,
2326  };
2327  struct rtp_ops dap_lookup_cs_component_ops = {
2328  .ap_header = NULL,
2329  .mem_ap_header = NULL,
2330  .cs_component = dap_lookup_cs_component_cs_component,
2331  .rom_table_entry = NULL,
2332  .priv = &lookup,
2333  };
2334 
2335  int retval = rtp_ap(&dap_lookup_cs_component_ops, ap, 0);
2336  if (retval == CORESIGHT_COMPONENT_FOUND) {
2337  if (lookup.ap_num != ap->ap_num) {
2338  /* TODO: handle search from root ROM table */
2339  LOG_DEBUG("CS lookup ended in AP # 0x%" PRIx64 ". Ignore it", lookup.ap_num);
2341  }
2342  LOG_DEBUG("CS lookup found at 0x%" PRIx64, lookup.component_base);
2343  *addr = lookup.component_base;
2344  return ERROR_OK;
2345  }
2346  if (retval != ERROR_OK) {
2347  LOG_DEBUG("CS lookup error %d", retval);
2348  return retval;
2349  }
2350  LOG_DEBUG("CS lookup not found");
2352 }
2353 
2358  CFG_CTIBASE, /* DEPRECATED */
2359 };
2360 
2361 static const struct jim_nvp nvp_config_opts[] = {
2362  { .name = "-dap", .value = CFG_DAP },
2363  { .name = "-ap-num", .value = CFG_AP_NUM },
2364  { .name = "-baseaddr", .value = CFG_BASEADDR },
2365  { .name = "-ctibase", .value = CFG_CTIBASE }, /* DEPRECATED */
2366  { .name = NULL, .value = -1 }
2367 };
2368 
2370  struct adiv5_dap **dap_p, uint64_t *ap_num_p, uint32_t *base_p)
2371 {
2372  assert(dap_p && ap_num_p);
2373 
2374  if (!goi->argc)
2375  return JIM_OK;
2376 
2377  Jim_SetEmptyResult(goi->interp);
2378 
2379  struct jim_nvp *n;
2381  goi->argv[0], &n);
2382  if (e != JIM_OK)
2383  return JIM_CONTINUE;
2384 
2385  /* base_p can be NULL, then '-baseaddr' option is treated as unknown */
2386  if (!base_p && (n->value == CFG_BASEADDR || n->value == CFG_CTIBASE))
2387  return JIM_CONTINUE;
2388 
2389  e = jim_getopt_obj(goi, NULL);
2390  if (e != JIM_OK)
2391  return e;
2392 
2393  switch (n->value) {
2394  case CFG_DAP:
2395  if (goi->is_configure) {
2396  Jim_Obj *o_t;
2397  struct adiv5_dap *dap;
2398  e = jim_getopt_obj(goi, &o_t);
2399  if (e != JIM_OK)
2400  return e;
2401  dap = dap_instance_by_jim_obj(goi->interp, o_t);
2402  if (!dap) {
2403  const char *dap_name = Jim_GetString(o_t, NULL);
2404  Jim_SetResultFormatted(goi->interp, "DAP '%s' not found",
2405  dap_name);
2406  return JIM_ERR;
2407  }
2408  if (*dap_p && *dap_p != dap) {
2409  Jim_SetResultString(goi->interp,
2410  "DAP assignment cannot be changed!", -1);
2411  return JIM_ERR;
2412  }
2413  *dap_p = dap;
2414  } else {
2415  if (goi->argc)
2416  goto err_no_param;
2417  if (!*dap_p) {
2418  Jim_SetResultString(goi->interp, "DAP not configured", -1);
2419  return JIM_ERR;
2420  }
2421  Jim_SetResultString(goi->interp, adiv5_dap_name(*dap_p), -1);
2422  }
2423  break;
2424 
2425  case CFG_AP_NUM:
2426  if (goi->is_configure) {
2427  /* jim_wide is a signed 64 bits int, ap_num is unsigned with max 52 bits */
2428  jim_wide ap_num;
2429  e = jim_getopt_wide(goi, &ap_num);
2430  if (e != JIM_OK)
2431  return e;
2432  /* we still don't know dap->adi_version */
2433  if (ap_num < 0 || (ap_num > DP_APSEL_MAX && (ap_num & 0xfff))) {
2434  Jim_SetResultString(goi->interp, "Invalid AP number!", -1);
2435  return JIM_ERR;
2436  }
2437  *ap_num_p = ap_num;
2438  } else {
2439  if (goi->argc)
2440  goto err_no_param;
2441  if (*ap_num_p == DP_APSEL_INVALID) {
2442  Jim_SetResultString(goi->interp, "AP number not configured", -1);
2443  return JIM_ERR;
2444  }
2445  Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, *ap_num_p));
2446  }
2447  break;
2448 
2449  case CFG_CTIBASE:
2450  LOG_WARNING("DEPRECATED! use \'-baseaddr' not \'-ctibase\'");
2451  /* fall through */
2452  case CFG_BASEADDR:
2453  if (goi->is_configure) {
2454  jim_wide base;
2455  e = jim_getopt_wide(goi, &base);
2456  if (e != JIM_OK)
2457  return e;
2458  *base_p = (uint32_t)base;
2459  } else {
2460  if (goi->argc)
2461  goto err_no_param;
2462  Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, *base_p));
2463  }
2464  break;
2465  };
2466 
2467  return JIM_OK;
2468 
2469 err_no_param:
2470  Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
2471  return JIM_ERR;
2472 }
2473 
2475  struct adiv5_private_config *pc, enum adiv5_configure_dap_optional optional)
2476 {
2477  int e;
2478 
2479  if (!pc) {
2480  pc = (struct adiv5_private_config *)target->private_config;
2481  if (!pc) {
2482  pc = calloc(1, sizeof(struct adiv5_private_config));
2483  if (!pc) {
2484  LOG_ERROR("Out of memory");
2485  return JIM_ERR;
2486  }
2487  pc->ap_num = DP_APSEL_INVALID;
2488  target->private_config = pc;
2489  }
2490  }
2491 
2492  if (optional == ADI_CONFIGURE_DAP_COMPULSORY)
2493  target->has_dap = true;
2494 
2495  e = adiv5_jim_spot_configure(goi, &pc->dap, &pc->ap_num, NULL);
2496  if (e != JIM_OK)
2497  return e;
2498 
2499  if (pc->dap && !target->dap_configured) {
2500  if (target->tap_configured) {
2501  pc->dap = NULL;
2502  Jim_SetResultString(goi->interp,
2503  "-chain-position and -dap configparams are mutually exclusive!", -1);
2504  return JIM_ERR;
2505  }
2506  target->tap = pc->dap->tap;
2507  target->dap_configured = true;
2508  target->has_dap = true;
2509  }
2510 
2511  return JIM_OK;
2512 }
2513 
2515 {
2517 }
2518 
2520 {
2521  if (!pc)
2522  return ERROR_FAIL;
2523 
2524  if (!pc->dap)
2525  return ERROR_FAIL;
2526 
2527  return ERROR_OK;
2528 }
2529 
2531  struct jim_getopt_info *goi)
2532 {
2533  return adiv5_jim_spot_configure(goi, &cfg->dap, &cfg->ap_num, &cfg->base);
2534 }
2535 
2537 {
2538  p->dap = NULL;
2539  p->ap_num = DP_APSEL_INVALID;
2540  p->base = 0;
2541  return ERROR_OK;
2542 }
2543 
2544 COMMAND_HANDLER(handle_dap_info_command)
2545 {
2546  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2547  uint64_t apsel;
2548 
2549  switch (CMD_ARGC) {
2550  case 0:
2551  apsel = dap->apsel;
2552  break;
2553  case 1:
2554  if (!strcmp(CMD_ARGV[0], "root")) {
2555  if (!is_adiv6(dap)) {
2556  command_print(CMD, "Option \"root\" not allowed with ADIv5 DAP");
2558  }
2559  int retval = adiv6_dap_read_baseptr(CMD, dap, &apsel);
2560  if (retval != ERROR_OK) {
2561  command_print(CMD, "Failed reading DAP baseptr");
2562  return retval;
2563  }
2564  break;
2565  }
2567  if (!is_ap_num_valid(dap, apsel)) {
2568  command_print(CMD, "Invalid AP number");
2570  }
2571  break;
2572  default:
2574  }
2575 
2576  struct adiv5_ap *ap = dap_get_ap(dap, apsel);
2577  if (!ap) {
2578  command_print(CMD, "Cannot get AP");
2579  return ERROR_FAIL;
2580  }
2581 
2582  int retval = dap_info_command(CMD, ap);
2583  dap_put_ap(ap);
2584  return retval;
2585 }
2586 
2587 COMMAND_HANDLER(dap_baseaddr_command)
2588 {
2589  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2590  uint64_t apsel;
2591  uint32_t baseaddr_lower, baseaddr_upper;
2592  struct adiv5_ap *ap;
2593  target_addr_t baseaddr;
2594  int retval;
2595 
2596  baseaddr_upper = 0;
2597 
2598  switch (CMD_ARGC) {
2599  case 0:
2600  apsel = dap->apsel;
2601  break;
2602  case 1:
2603  COMMAND_PARSE_NUMBER(u64, CMD_ARGV[0], apsel);
2604  if (!is_ap_num_valid(dap, apsel)) {
2605  command_print(CMD, "Invalid AP number");
2607  }
2608  break;
2609  default:
2611  }
2612 
2613  /* NOTE: assumes we're talking to a MEM-AP, which
2614  * has a base address. There are other kinds of AP,
2615  * though they're not common for now. This should
2616  * use the ID register to verify it's a MEM-AP.
2617  */
2618 
2619  ap = dap_get_ap(dap, apsel);
2620  if (!ap) {
2621  command_print(CMD, "Cannot get AP");
2622  return ERROR_FAIL;
2623  }
2624 
2625  retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE(dap), &baseaddr_lower);
2626 
2627  if (retval == ERROR_OK && ap->cfg_reg == MEM_AP_REG_CFG_INVALID)
2628  retval = dap_queue_ap_read(ap, MEM_AP_REG_CFG(dap), &ap->cfg_reg);
2629 
2630  if (retval == ERROR_OK && (ap->cfg_reg == MEM_AP_REG_CFG_INVALID || is_64bit_ap(ap))) {
2631  /* MEM_AP_REG_BASE64 is defined as 'RES0'; can be read and then ignored on 32 bits AP */
2632  retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE64(dap), &baseaddr_upper);
2633  }
2634 
2635  if (retval == ERROR_OK)
2636  retval = dap_run(dap);
2637  dap_put_ap(ap);
2638  if (retval != ERROR_OK)
2639  return retval;
2640 
2641  if (is_64bit_ap(ap)) {
2642  baseaddr = (((target_addr_t)baseaddr_upper) << 32) | baseaddr_lower;
2643  command_print(CMD, "0x%016" PRIx64, baseaddr);
2644  } else
2645  command_print(CMD, "0x%08" PRIx32, baseaddr_lower);
2646 
2647  return ERROR_OK;
2648 }
2649 
2650 COMMAND_HANDLER(dap_memaccess_command)
2651 {
2652  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2653  struct adiv5_ap *ap;
2654  uint32_t memaccess_tck;
2655 
2656  switch (CMD_ARGC) {
2657  case 0:
2658  ap = dap_get_ap(dap, dap->apsel);
2659  if (!ap) {
2660  command_print(CMD, "Cannot get AP");
2661  return ERROR_FAIL;
2662  }
2664  break;
2665  case 1:
2666  ap = dap_get_config_ap(dap, dap->apsel);
2667  if (!ap) {
2668  command_print(CMD, "Cannot get AP");
2669  return ERROR_FAIL;
2670  }
2673  break;
2674  default:
2676  }
2677 
2678  dap_put_ap(ap);
2679 
2680  command_print(CMD, "memory bus access delay set to %" PRIu32 " tck",
2681  memaccess_tck);
2682 
2683  return ERROR_OK;
2684 }
2685 
2686 COMMAND_HANDLER(dap_apsel_command)
2687 {
2688  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2689  uint64_t apsel;
2690 
2691  switch (CMD_ARGC) {
2692  case 0:
2693  command_print(CMD, "0x%" PRIx64, dap->apsel);
2694  return ERROR_OK;
2695  case 1:
2697  if (!is_ap_num_valid(dap, apsel)) {
2698  command_print(CMD, "Invalid AP number");
2700  }
2701  break;
2702  default:
2704  }
2705 
2706  dap->apsel = apsel;
2707  return ERROR_OK;
2708 }
2709 
2710 COMMAND_HANDLER(dap_apcsw_command)
2711 {
2712  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2713  struct adiv5_ap *ap;
2714  uint32_t csw_val, csw_mask;
2715 
2716  switch (CMD_ARGC) {
2717  case 0:
2718  ap = dap_get_ap(dap, dap->apsel);
2719  if (!ap) {
2720  command_print(CMD, "Cannot get AP");
2721  return ERROR_FAIL;
2722  }
2723  command_print(CMD, "AP#0x%" PRIx64 " selected, csw 0x%8.8" PRIx32,
2724  dap->apsel, ap->csw_default);
2725  break;
2726  case 1:
2727  if (strcmp(CMD_ARGV[0], "default") == 0)
2728  csw_val = CSW_AHB_DEFAULT;
2729  else
2730  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], csw_val);
2731 
2732  if (csw_val & (CSW_SIZE_MASK | CSW_ADDRINC_MASK)) {
2733  LOG_ERROR("CSW value cannot include 'Size' and 'AddrInc' bit-fields");
2735  }
2736  ap = dap_get_config_ap(dap, dap->apsel);
2737  if (!ap) {
2738  command_print(CMD, "Cannot get AP");
2739  return ERROR_FAIL;
2740  }
2741  ap->csw_default = csw_val;
2742  break;
2743  case 2:
2744  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], csw_val);
2745  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], csw_mask);
2746  if (csw_mask & (CSW_SIZE_MASK | CSW_ADDRINC_MASK)) {
2747  LOG_ERROR("CSW mask cannot include 'Size' and 'AddrInc' bit-fields");
2749  }
2750  ap = dap_get_config_ap(dap, dap->apsel);
2751  if (!ap) {
2752  command_print(CMD, "Cannot get AP");
2753  return ERROR_FAIL;
2754  }
2755  ap->csw_default = (ap->csw_default & ~csw_mask) | (csw_val & csw_mask);
2756  break;
2757  default:
2759  }
2760  dap_put_ap(ap);
2761 
2762  return ERROR_OK;
2763 }
2764 
2765 
2766 
2767 COMMAND_HANDLER(dap_apid_command)
2768 {
2769  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2770  uint64_t apsel;
2771  uint32_t apid;
2772  int retval;
2773 
2774  switch (CMD_ARGC) {
2775  case 0:
2776  apsel = dap->apsel;
2777  break;
2778  case 1:
2780  if (!is_ap_num_valid(dap, apsel)) {
2781  command_print(CMD, "Invalid AP number");
2783  }
2784  break;
2785  default:
2787  }
2788 
2789  struct adiv5_ap *ap = dap_get_ap(dap, apsel);
2790  if (!ap) {
2791  command_print(CMD, "Cannot get AP");
2792  return ERROR_FAIL;
2793  }
2794  retval = dap_queue_ap_read(ap, AP_REG_IDR(dap), &apid);
2795  if (retval != ERROR_OK) {
2796  dap_put_ap(ap);
2797  return retval;
2798  }
2799  retval = dap_run(dap);
2800  dap_put_ap(ap);
2801  if (retval != ERROR_OK)
2802  return retval;
2803 
2804  command_print(CMD, "0x%8.8" PRIx32, apid);
2805 
2806  return retval;
2807 }
2808 
2809 COMMAND_HANDLER(dap_apreg_command)
2810 {
2811  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2812  uint64_t apsel;
2813  uint32_t reg, value;
2814  int retval;
2815 
2816  if (CMD_ARGC < 2 || CMD_ARGC > 3)
2818 
2820  if (!is_ap_num_valid(dap, apsel)) {
2821  command_print(CMD, "Invalid AP number");
2823  }
2824 
2825  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg);
2826  if (is_adiv6(dap)) {
2827  if (reg >= 4096 || (reg & 3)) {
2828  command_print(CMD, "Invalid reg value (should be less than 4096 and 4 bytes aligned)");
2830  }
2831  } else { /* ADI version 5 */
2832  if (reg >= 256 || (reg & 3)) {
2833  command_print(CMD, "Invalid reg value (should be less than 256 and 4 bytes aligned)");
2835  }
2836  }
2837 
2838  struct adiv5_ap *ap = dap_get_ap(dap, apsel);
2839  if (!ap) {
2840  command_print(CMD, "Cannot get AP");
2841  return ERROR_FAIL;
2842  }
2843 
2844  if (CMD_ARGC == 3) {
2845  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
2846  /* see if user supplied register address is a match for the CSW or TAR register */
2847  if (reg == MEM_AP_REG_CSW(dap)) {
2848  ap->csw_value = 0; /* invalid, in case write fails */
2849  retval = dap_queue_ap_write(ap, reg, value);
2850  if (retval == ERROR_OK)
2851  ap->csw_value = value;
2852  } else if (reg == MEM_AP_REG_TAR(dap)) {
2853  retval = dap_queue_ap_write(ap, reg, value);
2854  if (retval == ERROR_OK)
2855  ap->tar_value = (ap->tar_value & ~0xFFFFFFFFull) | value;
2856  else {
2857  /* To track independent writes to TAR and TAR64, two tar_valid flags */
2858  /* should be used. To keep it simple, tar_valid is only invalidated on a */
2859  /* write fail. This approach causes a later re-write of the TAR and TAR64 */
2860  /* if tar_valid is false. */
2861  ap->tar_valid = false;
2862  }
2863  } else if (reg == MEM_AP_REG_TAR64(dap)) {
2864  retval = dap_queue_ap_write(ap, reg, value);
2865  if (retval == ERROR_OK)
2866  ap->tar_value = (ap->tar_value & 0xFFFFFFFFull) | (((target_addr_t)value) << 32);
2867  else {
2868  /* See above comment for the MEM_AP_REG_TAR failed write case */
2869  ap->tar_valid = false;
2870  }
2871  } else {
2872  retval = dap_queue_ap_write(ap, reg, value);
2873  }
2874  } else {
2875  retval = dap_queue_ap_read(ap, reg, &value);
2876  }
2877  if (retval == ERROR_OK)
2878  retval = dap_run(dap);
2879 
2880  dap_put_ap(ap);
2881 
2882  if (retval != ERROR_OK)
2883  return retval;
2884 
2885  if (CMD_ARGC == 2)
2886  command_print(CMD, "0x%08" PRIx32, value);
2887 
2888  return retval;
2889 }
2890 
2891 COMMAND_HANDLER(dap_dpreg_command)
2892 {
2893  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2894  uint32_t reg, value;
2895  int retval;
2896 
2897  if (CMD_ARGC < 1 || CMD_ARGC > 2)
2899 
2900  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], reg);
2901  if (reg >= 256 || (reg & 3)) {
2902  command_print(CMD, "Invalid reg value (should be less than 256 and 4 bytes aligned)");
2904  }
2905 
2906  if (CMD_ARGC == 2) {
2907  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2908  retval = dap_queue_dp_write(dap, reg, value);
2909  } else {
2910  retval = dap_queue_dp_read(dap, reg, &value);
2911  }
2912  if (retval == ERROR_OK)
2913  retval = dap_run(dap);
2914 
2915  if (retval != ERROR_OK)
2916  return retval;
2917 
2918  if (CMD_ARGC == 1)
2919  command_print(CMD, "0x%08" PRIx32, value);
2920 
2921  return retval;
2922 }
2923 
2924 COMMAND_HANDLER(dap_ti_be_32_quirks_command)
2925 {
2926  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2927  return CALL_COMMAND_HANDLER(handle_command_parse_bool, &dap->ti_be_32_quirks,
2928  "TI BE-32 quirks mode");
2929 }
2930 
2931 COMMAND_HANDLER(dap_nu_npcx_quirks_command)
2932 {
2933  struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
2934  return CALL_COMMAND_HANDLER(handle_command_parse_bool, &dap->nu_npcx_quirks,
2935  "Nuvoton NPCX quirks mode");
2936 }
2937 
2939  {
2940  .name = "info",
2941  .handler = handle_dap_info_command,
2942  .mode = COMMAND_EXEC,
2943  .help = "display ROM table for specified MEM-AP (default currently selected AP) "
2944  "or the ADIv6 root ROM table",
2945  .usage = "[ap_num | 'root']",
2946  },
2947  {
2948  .name = "apsel",
2949  .handler = dap_apsel_command,
2950  .mode = COMMAND_ANY,
2951  .help = "Set the currently selected AP (default 0) "
2952  "and display the result",
2953  .usage = "[ap_num]",
2954  },
2955  {
2956  .name = "apcsw",
2957  .handler = dap_apcsw_command,
2958  .mode = COMMAND_ANY,
2959  .help = "Set CSW default bits",
2960  .usage = "[value [mask]]",
2961  },
2962 
2963  {
2964  .name = "apid",
2965  .handler = dap_apid_command,
2966  .mode = COMMAND_EXEC,
2967  .help = "return ID register from AP "
2968  "(default currently selected AP)",
2969  .usage = "[ap_num]",
2970  },
2971  {
2972  .name = "apreg",
2973  .handler = dap_apreg_command,
2974  .mode = COMMAND_EXEC,
2975  .help = "read/write a register from AP "
2976  "(reg is byte address of a word register, like 0 4 8...)",
2977  .usage = "ap_num reg [value]",
2978  },
2979  {
2980  .name = "dpreg",
2981  .handler = dap_dpreg_command,
2982  .mode = COMMAND_EXEC,
2983  .help = "read/write a register from DP "
2984  "(reg is byte address (bank << 4 | reg) of a word register, like 0 4 8...)",
2985  .usage = "reg [value]",
2986  },
2987  {
2988  .name = "baseaddr",
2989  .handler = dap_baseaddr_command,
2990  .mode = COMMAND_EXEC,
2991  .help = "return debug base address from MEM-AP "
2992  "(default currently selected AP)",
2993  .usage = "[ap_num]",
2994  },
2995  {
2996  .name = "memaccess",
2997  .handler = dap_memaccess_command,
2998  .mode = COMMAND_EXEC,
2999  .help = "set/get number of extra tck for MEM-AP memory "
3000  "bus access [0-255]",
3001  .usage = "[cycles]",
3002  },
3003  {
3004  .name = "ti_be_32_quirks",
3005  .handler = dap_ti_be_32_quirks_command,
3006  .mode = COMMAND_CONFIG,
3007  .help = "set/get quirks mode for TI TMS450/TMS570 processors",
3008  .usage = "[enable]",
3009  },
3010  {
3011  .name = "nu_npcx_quirks",
3012  .handler = dap_nu_npcx_quirks_command,
3013  .mode = COMMAND_CONFIG,
3014  .help = "set/get quirks mode for Nuvoton NPCX controllers",
3015  .usage = "[enable]",
3016  },
3018 };
#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:2094
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:2081
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:2024
static const struct jim_nvp nvp_config_opts[]
Definition: arm_adi_v5.c:2361
COMMAND_HANDLER(handle_dap_info_command)
Definition: arm_adi_v5.c:2544
#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:2320
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:1887
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:2519
#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:2266
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:2536
static int dap_info_cs_component(int retval, struct cs_component_vals *v, int depth, void *priv)
Definition: arm_adi_v5.c:2148
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:2369
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:2234
int adiv5_jim_configure(struct target *target, struct jim_getopt_info *goi)
Definition: arm_adi_v5.c:2514
adiv5_cfg_param
Definition: arm_adi_v5.c:2354
@ CFG_AP_NUM
Definition: arm_adi_v5.c:2356
@ CFG_CTIBASE
Definition: arm_adi_v5.c:2358
@ CFG_BASEADDR
Definition: arm_adi_v5.c:2357
@ CFG_DAP
Definition: arm_adi_v5.c:2355
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:1860
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:2474
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:1873
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:1828
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:1616
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:1815
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:2290
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:1844
#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:1602
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:2938
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:1881
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:2530
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:1965
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:70
static int dap_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
Send an adi-v5 sequence to the DAP.
Definition: arm_adi_v5.h:536
#define AP_REG_IDR_CLASS_MASK
Definition: arm_adi_v5.h:216
adiv5_configure_dap_optional
Definition: arm_adi_v5.h: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:66
#define CSW_8BIT
Definition: arm_adi_v5.h:165
static int dap_queue_dp_read(struct adiv5_dap *dap, unsigned int reg, uint32_t *data)
Queue a DP register read.
Definition: arm_adi_v5.h:555
#define SSTICKYORUN
Definition: arm_adi_v5.h:83
#define CSYSPWRUPACK
Definition: arm_adi_v5.h:96
#define MEM_AP_REG_CFG_LA
Definition: arm_adi_v5.h:207
#define MEM_AP_REG_DRW(dap)
Definition: arm_adi_v5.h:148
#define DP_BASEPTR1
Definition: arm_adi_v5.h:49
static int dap_dp_read_atomic(struct adiv5_dap *dap, unsigned int reg, uint32_t *value)
Definition: arm_adi_v5.h:662
#define MEM_AP_REG_BASE(dap)
Definition: arm_adi_v5.h:156
#define CSW_ADDRINC_OFF
Definition: arm_adi_v5.h:172
static int dap_queue_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
Queue an AP register write.
Definition: arm_adi_v5.h:610
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define CSYSPWRUPREQ
Definition: arm_adi_v5.h:95
#define MEM_AP_REG_BASE64(dap)
Definition: arm_adi_v5.h:154
const char * adiv5_dap_name(struct adiv5_dap *self)
Definition: arm_dap.c:54
#define DP_BASEPTR0
Definition: arm_adi_v5.h:48
#define MEM_AP_REG_BD0(dap)
Definition: arm_adi_v5.h:149
#define CSW_SIZE_MASK
Definition: arm_adi_v5.h:164
static bool is_adiv6(const struct adiv5_dap *dap)
Check if DAP is ADIv6.
Definition: arm_adi_v5.h:523
#define MEM_AP_REG_TAR64(dap)
Definition: arm_adi_v5.h:147
static int dap_run(struct adiv5_dap *dap)
Perform all queued DAP operations, and clear any errors posted in the CTRL_STAT register when they ar...
Definition: arm_adi_v5.h:648
#define AP_REG_IDR(dap)
Definition: arm_adi_v5.h:161
#define MEM_AP_REG_CFG_INVALID
Definition: arm_adi_v5.h:209
#define ARM_CS_CIDR3
Definition: arm_coresight.h:44
#define ARM_CS_CLASS_0X1_ROM_TABLE
Definition: arm_coresight.h:48
#define ARM_CS_PIDR2
Definition: arm_coresight.h:21
#define ARM_CS_C9_DEVID_SYSMEM_MASK
Definition: arm_coresight.h:76
static bool is_valid_arm_cs_cidr(uint32_t cidr)
Definition: arm_coresight.h:51
#define ARM_CS_C9_DEVTYPE_MASK
Definition: arm_coresight.h:87
#define ARM_CS_PIDR3
Definition: arm_coresight.h:22
#define ARM_CS_PIDR_SIZE(pidr)
Definition: arm_coresight.h:39
#define ARM_CS_CLASS_0X9_CS_COMPONENT
Definition: arm_coresight.h:49
#define ARM_CS_C9_DEVID
Definition: arm_coresight.h:71
#define ARM_CS_CIDR1
Definition: arm_coresight.h:42
#define ARM_CS_C9_DEVARCH
Definition: arm_coresight.h:57
#define ARM_CS_PIDR0
Definition: arm_coresight.h:19
#define ARM_CS_C9_DEVTYPE_SUB_SHIFT
Definition: arm_coresight.h:85
#define ARM_CS_CIDR_CLASS(cidr)
Definition: arm_coresight.h:47
#define ARM_CS_C9_DEVARCH_REVISION(devarch)
Definition: arm_coresight.h:66
#define ARM_CS_ROMENTRY_PRESENT
Definition: arm_coresight.h:97
#define ARM_CS_PIDR4
Definition: arm_coresight.h:23
#define ARM_CS_ALIGN
Definition: arm_coresight.h:16
#define ARM_CS_C9_DEVTYPE_MAJOR_SHIFT
Definition: arm_coresight.h:83
#define ARM_CS_C9_DEVTYPE
Definition: arm_coresight.h:80
#define ARM_CS_C9_DEVTYPE_MAJOR_MASK
Definition: arm_coresight.h:82
#define ARM_CS_C1_MEMTYPE_SYSMEM_MASK
Definition: arm_coresight.h:93
#define ARM_CS_C9_DEVID_FORMAT_MASK
Definition: arm_coresight.h:73
#define ARM_CS_ROMENTRY_OFFSET_MASK
Definition: arm_coresight.h:98
#define ARM_CS_C9_DEVARCH_ARCHITECT(devarch)
Definition: arm_coresight.h:68
#define ARM_CS_PIDR_JEDEC
Definition: arm_coresight.h:38
#define ARM_CS_PIDR_PART(pidr)
Definition: arm_coresight.h:32
#define ARM_CS_CIDR0
Definition: arm_coresight.h:41
#define ARM_CS_C9_DEVTYPE_SUB_MASK
Definition: arm_coresight.h:84
#define ARM_CS_C9_DEVID_FORMAT_64BIT
Definition: arm_coresight.h:75
#define ARM_CS_CIDR2
Definition: arm_coresight.h:43
#define ARM_CS_PIDR1
Definition: arm_coresight.h:20
#define ARM_CS_C9_DEVARCH_PRESENT
Definition: arm_coresight.h:63
#define ARM_CS_PIDR_DESIGNER(pidr)
Definition: arm_coresight.h:33
enum arm_mode mode
Definition: armv4_5.c:281
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:382
#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:2284
uint64_t component_base
Definition: arm_adi_v5.c:2286
uint64_t ap_num
Definition: arm_adi_v5.c:2287
unsigned int idx
Definition: arm_adi_v5.c:2283
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:1763
void * priv
Private data.
Definition: arm_adi_v5.c:1809
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:1782
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:1804
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:1793
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:1771
Definition: target.h:119
struct jtag_tap * tap
Definition: target.h:122
void * private_config
Definition: target.h:168
bool dap_configured
Definition: target.h:182
bool has_dap
Definition: target.h:181
bool tap_configured
Definition: target.h:183
#define true
Definition: system.h:59
#define ERROR_TARGET_SIZE_NOT_SUPPORTED
Definition: target.h:805
#define ERROR_TARGET_UNALIGNED_ACCESS
Definition: target.h:797
#define ERROR_TARGET_PACKING_NOT_SUPPORTED
Definition: target.h:806
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:799
#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