Updated submodules/libswd to use new git.code.sf.net host and v0.5 release branch.
[openocd/libswd.git] / src / target / arm_adi_v5.c
blob73b0136486ed6a9625ee89db23e943fc76aff2c9
1 /***************************************************************************
2 * Copyright (C) 2006 by Magnus Lundin *
3 * lundin@mlu.mine.nu *
4 * *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
7 * *
8 * Copyright (C) 2009-2010 by Oyvind Harboe *
9 * oyvind.harboe@zylin.com *
10 * *
11 * Copyright (C) 2009-2010 by David Brownell *
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the *
25 * Free Software Foundation, Inc., *
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
27 ***************************************************************************/
29 /**
30 * @file
31 * This file implements support for the ARM Debug Interface version 5 (ADIv5)
32 * debugging architecture. Compared with previous versions, this includes
33 * a low pin-count Serial Wire Debug (SWD) alternative to JTAG for message
34 * transport, and focusses on memory mapped resources as defined by the
35 * CoreSight architecture.
37 * A key concept in ADIv5 is the Debug Access Port, or DAP. A DAP has two
38 * basic components: a Debug Port (DP) transporting messages to and from a
39 * debugger, and an Access Port (AP) accessing resources. Three types of DP
40 * are defined. One uses only JTAG for communication, and is called JTAG-DP.
41 * One uses only SWD for communication, and is called SW-DP. The third can
42 * use either SWD or JTAG, and is called SWJ-DP. The most common type of AP
43 * is used to access memory mapped resources and is called a MEM-AP. Also a
44 * JTAG-AP is also defined, bridging to JTAG resources; those are uncommon.
46 * This programming interface allows DAP pipelined operations through a
47 * transaction queue. This primarily affects AP operations (such as using
48 * a MEM-AP to access memory or registers). If the current transaction has
49 * not finished by the time the next one must begin, and the ORUNDETECT bit
50 * is set in the DP_CTRL_STAT register, the SSTICKYORUN status is set and
51 * further AP operations will fail. There are two basic methods to avoid
52 * such overrun errors. One involves polling for status instead of using
53 * transaction piplining. The other involves adding delays to ensure the
54 * AP has enough time to complete one operation before starting the next
55 * one. (For JTAG these delays are controlled by memaccess_tck.)
59 * Relevant specifications from ARM include:
61 * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031A
62 * CoreSight(tm) v1.0 Architecture Specification ARM IHI 0029B
64 * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316D
65 * Cortex-M3(tm) TRM, ARM DDI 0337G
68 #ifdef HAVE_CONFIG_H
69 #include "config.h"
70 #endif
72 #include "jtag/interface.h"
73 #include "arm.h"
74 #include "arm_adi_v5.h"
75 #include <helper/time_support.h>
77 /* These two below are to be removed when transport is finished... */
78 extern const struct dap_ops jtag_dap_ops;
79 extern struct jtag_interface *jtag_interface;
81 /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
84 uint32_t tar_block_size(uint32_t address)
85 Return the largest block starting at address that does not cross a tar block size alignment boundary
87 static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
89 return (tar_autoincr_block - ((tar_autoincr_block - 1) & address)) >> 2;
92 /***************************************************************************
93 * *
94 * DP and MEM-AP register access through APACC and DPACC *
95 * *
96 ***************************************************************************/
98 /**
99 * Select one of the APs connected to the specified DAP. The
100 * selection is implicitly used with future AP transactions.
101 * This is a NOP if the specified AP is already selected.
103 * @param dap The DAP
104 * @param apsel Number of the AP to (implicitly) use with further
105 * transactions. This normally identifies a MEM-AP.
107 void dap_ap_select(struct adiv5_dap *dap, uint8_t ap)
109 uint32_t new_ap = (ap << 24) & 0xFF000000;
111 if (new_ap != dap->ap_current) {
112 dap->ap_current = new_ap;
113 /* Switching AP invalidates cached values.
114 * Values MUST BE UPDATED BEFORE AP ACCESS.
116 dap->ap_bank_value = -1;
117 dap->ap_csw_value = -1;
118 dap->ap_tar_value = -1;
123 * Queue transactions setting up transfer parameters for the
124 * currently selected MEM-AP.
126 * Subsequent transfers using registers like AP_REG_DRW or AP_REG_BD2
127 * initiate data reads or writes using memory or peripheral addresses.
128 * If the CSW is configured for it, the TAR may be automatically
129 * incremented after each transfer.
131 * @todo Rename to reflect it being specifically a MEM-AP function.
133 * @param dap The DAP connected to the MEM-AP.
134 * @param csw MEM-AP Control/Status Word (CSW) register to assign. If this
135 * matches the cached value, the register is not changed.
136 * @param tar MEM-AP Transfer Address Register (TAR) to assign. If this
137 * matches the cached address, the register is not changed.
139 * @return ERROR_OK if the transaction was properly queued, else a fault code.
141 int dap_setup_accessport(struct adiv5_dap *dap, uint32_t csw, uint32_t tar)
143 int retval;
145 csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
146 if (csw != dap->ap_csw_value) {
147 /* LOG_DEBUG("DAP: Set CSW %x",csw); */
148 retval = dap_queue_ap_write(dap, AP_REG_CSW, csw);
149 if (retval != ERROR_OK)
150 return retval;
151 dap->ap_csw_value = csw;
153 if (tar != dap->ap_tar_value) {
154 /* LOG_DEBUG("DAP: Set TAR %x",tar); */
155 retval = dap_queue_ap_write(dap, AP_REG_TAR, tar);
156 if (retval != ERROR_OK)
157 return retval;
158 dap->ap_tar_value = tar;
160 /* Disable TAR cache when autoincrementing */
161 if (csw & CSW_ADDRINC_MASK)
162 dap->ap_tar_value = -1;
163 return ERROR_OK;
167 * Asynchronous (queued) read of a word from memory or a system register.
169 * @param dap The DAP connected to the MEM-AP performing the read.
170 * @param address Address of the 32-bit word to read; it must be
171 * readable by the currently selected MEM-AP.
172 * @param value points to where the word will be stored when the
173 * transaction queue is flushed (assuming no errors).
175 * @return ERROR_OK for success. Otherwise a fault code.
177 int mem_ap_read_u32(struct adiv5_dap *dap, uint32_t address,
178 uint32_t *value)
180 int retval;
182 /* Use banked addressing (REG_BDx) to avoid some link traffic
183 * (updating TAR) when reading several consecutive addresses.
185 retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_OFF,
186 address & 0xFFFFFFF0);
187 if (retval != ERROR_OK)
188 return retval;
190 return dap_queue_ap_read(dap, AP_REG_BD0 | (address & 0xC), value);
194 * Synchronous read of a word from memory or a system register.
195 * As a side effect, this flushes any queued transactions.
197 * @param dap The DAP connected to the MEM-AP performing the read.
198 * @param address Address of the 32-bit word to read; it must be
199 * readable by the currently selected MEM-AP.
200 * @param value points to where the result will be stored.
202 * @return ERROR_OK for success; *value holds the result.
203 * Otherwise a fault code.
205 int mem_ap_read_atomic_u32(struct adiv5_dap *dap, uint32_t address,
206 uint32_t *value)
208 int retval;
210 retval = mem_ap_read_u32(dap, address, value);
211 if (retval != ERROR_OK)
212 return retval;
214 return dap_run(dap);
218 * Asynchronous (queued) write of a word to memory or a system register.
220 * @param dap The DAP connected to the MEM-AP.
221 * @param address Address to be written; it must be writable by
222 * the currently selected MEM-AP.
223 * @param value Word that will be written to the address when transaction
224 * queue is flushed (assuming no errors).
226 * @return ERROR_OK for success. Otherwise a fault code.
228 int mem_ap_write_u32(struct adiv5_dap *dap, uint32_t address,
229 uint32_t value)
231 int retval;
233 /* Use banked addressing (REG_BDx) to avoid some link traffic
234 * (updating TAR) when writing several consecutive addresses.
236 retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_OFF,
237 address & 0xFFFFFFF0);
238 if (retval != ERROR_OK)
239 return retval;
241 return dap_queue_ap_write(dap, AP_REG_BD0 | (address & 0xC),
242 value);
246 * Synchronous write of a word to memory or a system register.
247 * As a side effect, this flushes any queued transactions.
249 * @param dap The DAP connected to the MEM-AP.
250 * @param address Address to be written; it must be writable by
251 * the currently selected MEM-AP.
252 * @param value Word that will be written.
254 * @return ERROR_OK for success; the data was written. Otherwise a fault code.
256 int mem_ap_write_atomic_u32(struct adiv5_dap *dap, uint32_t address,
257 uint32_t value)
259 int retval = mem_ap_write_u32(dap, address, value);
261 if (retval != ERROR_OK)
262 return retval;
264 return dap_run(dap);
267 /*****************************************************************************
269 * mem_ap_write_buf(struct adiv5_dap *dap, uint8_t *buffer, int count, uint32_t address) *
271 * Write a buffer in target order (little endian) *
273 *****************************************************************************/
274 int mem_ap_write_buf_u32(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address)
276 int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
277 uint32_t adr = address;
278 const uint8_t *pBuffer = buffer;
280 count >>= 2;
281 wcount = count;
283 /* if we have an unaligned access - reorder data */
284 if (adr & 0x3u) {
285 for (writecount = 0; writecount < count; writecount++) {
286 int i;
287 uint32_t outvalue;
288 memcpy(&outvalue, pBuffer, sizeof(uint32_t));
290 for (i = 0; i < 4; i++) {
291 *((uint8_t *)pBuffer + (adr & 0x3)) = outvalue;
292 outvalue >>= 8;
293 adr++;
295 pBuffer += sizeof(uint32_t);
299 while (wcount > 0) {
300 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
301 blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
302 if (wcount < blocksize)
303 blocksize = wcount;
305 /* handle unaligned data at 4k boundary */
306 if (blocksize == 0)
307 blocksize = 1;
309 retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
310 if (retval != ERROR_OK)
311 return retval;
313 for (writecount = 0; writecount < blocksize; writecount++) {
314 uint32_t tmp;
315 tmp = buf_get_u32(buffer + 4 * writecount, 0, 32);
316 retval = dap_queue_ap_write(dap, AP_REG_DRW, tmp);
317 if (retval != ERROR_OK)
318 break;
321 retval = dap_run(dap);
322 if (retval == ERROR_OK) {
323 wcount = wcount - blocksize;
324 address = address + 4 * blocksize;
325 buffer = buffer + 4 * blocksize;
326 } else
327 errorcount++;
329 if (errorcount > 1) {
330 LOG_WARNING("Block write error address 0x%" PRIx32 ", wcount 0x%x", address, wcount);
331 return retval;
335 return retval;
338 static int mem_ap_write_buf_packed_u16(struct adiv5_dap *dap,
339 const uint8_t *buffer, int count, uint32_t address)
341 int retval = ERROR_OK;
342 int wcount, blocksize, writecount, i;
344 wcount = count >> 1;
346 while (wcount > 0) {
347 int nbytes;
349 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
350 blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
352 if (wcount < blocksize)
353 blocksize = wcount;
355 /* handle unaligned data at 4k boundary */
356 if (blocksize == 0)
357 blocksize = 1;
359 retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_PACKED, address);
360 if (retval != ERROR_OK)
361 return retval;
362 writecount = blocksize;
364 do {
365 nbytes = MIN((writecount << 1), 4);
367 if (nbytes < 4) {
368 retval = mem_ap_write_buf_u16(dap, buffer,
369 nbytes, address);
370 if (retval != ERROR_OK) {
371 LOG_WARNING("Block write error address "
372 "0x%" PRIx32 ", count 0x%x",
373 address, count);
374 return retval;
377 address += nbytes >> 1;
378 } else {
379 uint32_t outvalue;
380 memcpy(&outvalue, buffer, sizeof(uint32_t));
382 for (i = 0; i < nbytes; i++) {
383 *((uint8_t *)buffer + (address & 0x3)) = outvalue;
384 outvalue >>= 8;
385 address++;
388 memcpy(&outvalue, buffer, sizeof(uint32_t));
389 retval = dap_queue_ap_write(dap,
390 AP_REG_DRW, outvalue);
391 if (retval != ERROR_OK)
392 break;
394 retval = dap_run(dap);
395 if (retval != ERROR_OK) {
396 LOG_WARNING("Block write error address "
397 "0x%" PRIx32 ", count 0x%x",
398 address, count);
399 return retval;
403 buffer += nbytes >> 1;
404 writecount -= nbytes >> 1;
406 } while (writecount);
407 wcount -= blocksize;
410 return retval;
413 int mem_ap_write_buf_u16(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address)
415 int retval = ERROR_OK;
417 if (count >= 4)
418 return mem_ap_write_buf_packed_u16(dap, buffer, count, address);
420 while (count > 0) {
421 retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
422 if (retval != ERROR_OK)
423 return retval;
424 uint16_t svalue;
425 memcpy(&svalue, buffer, sizeof(uint16_t));
426 uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
427 retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
428 if (retval != ERROR_OK)
429 break;
431 retval = dap_run(dap);
432 if (retval != ERROR_OK)
433 break;
435 count -= 2;
436 address += 2;
437 buffer += 2;
440 return retval;
443 static int mem_ap_write_buf_packed_u8(struct adiv5_dap *dap,
444 const uint8_t *buffer, int count, uint32_t address)
446 int retval = ERROR_OK;
447 int wcount, blocksize, writecount, i;
449 wcount = count;
451 while (wcount > 0) {
452 int nbytes;
454 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
455 blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
457 if (wcount < blocksize)
458 blocksize = wcount;
460 retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_PACKED, address);
461 if (retval != ERROR_OK)
462 return retval;
463 writecount = blocksize;
465 do {
466 nbytes = MIN(writecount, 4);
468 if (nbytes < 4) {
469 retval = mem_ap_write_buf_u8(dap, buffer, nbytes, address);
470 if (retval != ERROR_OK) {
471 LOG_WARNING("Block write error address "
472 "0x%" PRIx32 ", count 0x%x",
473 address, count);
474 return retval;
477 address += nbytes;
478 } else {
479 uint32_t outvalue;
480 memcpy(&outvalue, buffer, sizeof(uint32_t));
482 for (i = 0; i < nbytes; i++) {
483 *((uint8_t *)buffer + (address & 0x3)) = outvalue;
484 outvalue >>= 8;
485 address++;
488 memcpy(&outvalue, buffer, sizeof(uint32_t));
489 retval = dap_queue_ap_write(dap,
490 AP_REG_DRW, outvalue);
491 if (retval != ERROR_OK)
492 break;
494 retval = dap_run(dap);
495 if (retval != ERROR_OK) {
496 LOG_WARNING("Block write error address "
497 "0x%" PRIx32 ", count 0x%x",
498 address, count);
499 return retval;
503 buffer += nbytes;
504 writecount -= nbytes;
506 } while (writecount);
507 wcount -= blocksize;
510 return retval;
513 int mem_ap_write_buf_u8(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address)
515 int retval = ERROR_OK;
517 if (count >= 4)
518 return mem_ap_write_buf_packed_u8(dap, buffer, count, address);
520 while (count > 0) {
521 retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
522 if (retval != ERROR_OK)
523 return retval;
524 uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
525 retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
526 if (retval != ERROR_OK)
527 break;
529 retval = dap_run(dap);
530 if (retval != ERROR_OK)
531 break;
533 count--;
534 address++;
535 buffer++;
538 return retval;
541 /* FIXME don't import ... this is a temporary workaround for the
542 * mem_ap_read_buf_u32() mess, until it's no longer JTAG-specific.
544 extern int adi_jtag_dp_scan(struct adiv5_dap *dap,
545 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
546 uint8_t *outvalue, uint8_t *invalue, uint8_t *ack);
549 * Synchronously read a block of 32-bit words into a buffer
550 * @param dap The DAP connected to the MEM-AP.
551 * @param buffer where the words will be stored (in host byte order).
552 * @param count How many words to read.
553 * @param address Memory address from which to read words; all the
554 * words must be readable by the currently selected MEM-AP.
555 * @Warning! THIS FUNCTION HAS JTAG CALLS HARDCODED!!!
557 int mem_ap_read_buf_u32_jtag(struct adiv5_dap *dap, uint8_t *buffer,
558 int count, uint32_t address)
560 int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
561 uint32_t adr = address;
562 uint8_t *pBuffer = buffer;
564 count >>= 2;
565 wcount = count;
567 while (wcount > 0) {
568 /* Adjust to read blocks within boundaries aligned to the
569 * TAR autoincrement size (at least 2^10). Autoincrement
570 * mode avoids an extra per-word roundtrip to update TAR.
572 blocksize = max_tar_block_size(dap->tar_autoincr_block,
573 address);
574 if (wcount < blocksize)
575 blocksize = wcount;
577 /* handle unaligned data at 4k boundary */
578 if (blocksize == 0)
579 blocksize = 1;
581 retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_SINGLE,
582 address);
583 if (retval != ERROR_OK)
584 return retval;
586 /* FIXME remove these three calls to adi_jtag_dp_scan(),
587 * so this routine becomes transport-neutral. Be careful
588 * not to cause performance problems with JTAG; would it
589 * suffice to loop over dap_queue_ap_read(), or would that
590 * be slower when JTAG is the chosen transport?
593 /* Scan out first read */
594 retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW,
595 DPAP_READ, 0, NULL, NULL);
596 if (retval != ERROR_OK)
597 return retval;
598 for (readcount = 0; readcount < blocksize - 1; readcount++) {
599 /* Scan out next read; scan in posted value for the
600 * previous one. Assumes read is acked "OK/FAULT",
601 * and CTRL_STAT says that meant "OK".
603 retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW,
604 DPAP_READ, 0, buffer + 4 * readcount,
605 &dap->ack);
606 if (retval != ERROR_OK)
607 return retval;
610 /* Scan in last posted value; RDBUFF has no other effect,
611 * assuming ack is OK/FAULT and CTRL_STAT says "OK".
613 retval = adi_jtag_dp_scan(dap, JTAG_DP_DPACC, DP_RDBUFF,
614 DPAP_READ, 0, buffer + 4 * readcount,
615 &dap->ack);
616 if (retval != ERROR_OK)
617 return retval;
619 retval = dap_run(dap);
620 if (retval != ERROR_OK) {
621 errorcount++;
622 if (errorcount <= 1) {
623 /* try again */
624 continue;
626 LOG_WARNING("Block read error address 0x%" PRIx32, address);
627 return retval;
629 wcount = wcount - blocksize;
630 address += 4 * blocksize;
631 buffer += 4 * blocksize;
634 /* if we have an unaligned access - reorder data */
635 if (adr & 0x3u) {
636 for (readcount = 0; readcount < count; readcount++) {
637 int i;
638 uint32_t data;
639 memcpy(&data, pBuffer, sizeof(uint32_t));
641 for (i = 0; i < 4; i++) {
642 *((uint8_t *)pBuffer) =
643 (data >> 8 * (adr & 0x3));
644 pBuffer++;
645 adr++;
650 return retval;
654 * This is a SWD handler, maybe future transport independent implementation
655 * of mem_ap_read_buf_u32() that is responsible to read data from mem-ap.
657 int mem_ap_read_buf_u32_swd(struct adiv5_dap *dap, uint8_t *buffer,
658 int count, uint32_t address)
660 int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
661 uint32_t adr = address;
662 uint8_t *pBuffer = buffer;
664 count >>= 2;
665 wcount = count;
667 while (wcount > 0) {
668 /* Adjust to read blocks within boundaries aligned to the
669 * TAR autoincrement size (at least 2^10). Autoincrement
670 * mode avoids an extra per-word roundtrip to update TAR.
672 blocksize = max_tar_block_size(dap->tar_autoincr_block,
673 address);
674 if (wcount < blocksize)
675 blocksize = wcount;
677 /* handle unaligned data at 4k boundary */
678 if (blocksize == 0)
679 blocksize = 1;
681 retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_SINGLE,
682 address);
683 if (retval != ERROR_OK)
684 return retval;
686 /* FIXME remove these three calls to adi_jtag_dp_scan(),
687 * so this routine becomes transport-neutral. Be careful
688 * not to cause performance problems with JTAG; would it
689 * suffice to loop over dap_queue_ap_read(), or would that
690 * be slower when JTAG is the chosen transport?
693 /* Stage 1: Scan out first read to initiate memory operation. */
694 /*retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW, */
695 /* DPAP_READ, 0, NULL, NULL); */
696 retval = dap_queue_ap_read(dap, AP_REG_DRW, NULL);
697 if (retval != ERROR_OK)
698 /*return retval;*/
699 goto mem_ap_read_buf_u32_swd_handle_errors;
701 /* Stage 2: Loop memory reads with TAR autoincrement. */
702 /* TODO: For SWD it might be possible to read data without
703 * additional request-ack-data phase. Try it out :-) */
704 for (readcount = 0; readcount < blocksize - 1; readcount++) {
705 /* Scan out next read; scan in posted value for the
706 * previous one. Assumes read is acked "OK/FAULT",
707 * and CTRL_STAT says that meant "OK".
710 * retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW,
711 * DPAP_READ, 0, buffer + 4 * readcount,
712 * &dap->ack);
713 * TODO: IS THIS POINTER/MEMORY OPERATION VALID??
715 retval = dap_queue_ap_read(dap, AP_REG_DRW, (uint32_t *)buffer+readcount);
716 /* TODO: For SWD we will get response after call. */
717 /* We need to react / retry operation in here. */
718 if (retval != ERROR_OK)
719 /*return retval;*/
720 goto mem_ap_read_buf_u32_swd_handle_errors;
723 /* Stage 3: Scan in last posted value; RDBUFF has no other effect,
724 * assuming ack is OK/FAULT and CTRL_STAT says "OK".
727 * retval = adi_jtag_dp_scan(dap, JTAG_DP_DPACC, DP_RDBUFF,
728 * DPAP_READ, 0, buffer + 4 * readcount,
729 * &dap->ack);
731 retval = dap_queue_ap_read(dap, DP_RDBUFF, (uint32_t *)buffer+readcount);
732 if (retval != ERROR_OK)
733 goto mem_ap_read_buf_u32_swd_handle_errors;
735 retval = dap_run(dap);
737 mem_ap_read_buf_u32_swd_handle_errors:
738 if (retval != ERROR_OK) {
739 errorcount++;
740 if (errorcount <= 1) {
741 /* try again */
742 continue;
744 LOG_WARNING("Target - ARM DAP MEM-AP block read error address 0x%" PRIx32, address);
745 return retval;
747 wcount = wcount - blocksize;
748 address += 4 * blocksize;
749 buffer += 4 * blocksize;
752 /* if we have an unaligned access - reorder data */
753 if (adr & 0x3u) {
754 for (readcount = 0; readcount < count; readcount++) {
755 int i;
756 uint32_t data;
757 memcpy(&data, pBuffer, sizeof(uint32_t));
759 for (i = 0; i < 4; i++) {
760 *((uint8_t *)pBuffer) =
761 (data >> 8 * (adr & 0x3));
762 pBuffer++;
763 adr++;
768 return retval;
773 * TEMPORARY WRAPPER WORKAROUND FOR MEM_AP_READ_BUF_U32
774 * UNTIL IT BECOMES TRANSPORT INDEPENDENT (ORIGINAL HAD JTAG CODE HARDCODED).
776 int mem_ap_read_buf_u32(struct adiv5_dap *dap, uint8_t *buffer,
777 int count, uint32_t address)
779 if (strncmp(jtag_interface->transport->name, "swd", 3) == 0) {
780 return mem_ap_read_buf_u32_swd(dap, buffer, count, address);
781 } else if (strncmp(jtag_interface->transport->name, "jtag", 4) == 0) {
782 return mem_ap_read_buf_u32_jtag(dap, buffer, count, address);
783 } else {
784 LOG_ERROR("Target - ARM ADI - unsupported transport selected!");
785 return ERROR_FAIL;
790 static int mem_ap_read_buf_packed_u16(struct adiv5_dap *dap,
791 uint8_t *buffer, int count, uint32_t address)
793 uint32_t invalue;
794 int retval = ERROR_OK;
795 int wcount, blocksize, readcount, i;
797 wcount = count >> 1;
799 while (wcount > 0) {
800 int nbytes;
802 /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
803 blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
804 if (wcount < blocksize)
805 blocksize = wcount;
807 retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_PACKED, address);
808 if (retval != ERROR_OK)
809 return retval;
811 /* handle unaligned data at 4k boundary */
812 if (blocksize == 0)
813 blocksize = 1;
814 readcount = blocksize;
816 do {
817 retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
818 if (retval != ERROR_OK)
819 return retval;
820 retval = dap_run(dap);
821 if (retval != ERROR_OK) {
822 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
823 return retval;
826 nbytes = MIN((readcount << 1), 4);
828 for (i = 0; i < nbytes; i++) {
829 *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
830 buffer++;
831 address++;
834 readcount -= (nbytes >> 1);
835 } while (readcount);
836 wcount -= blocksize;
839 return retval;
843 * Synchronously read a block of 16-bit halfwords into a buffer
844 * @param dap The DAP connected to the MEM-AP.
845 * @param buffer where the halfwords will be stored (in host byte order).
846 * @param count How many halfwords to read.
847 * @param address Memory address from which to read words; all the
848 * words must be readable by the currently selected MEM-AP.
850 int mem_ap_read_buf_u16(struct adiv5_dap *dap, uint8_t *buffer,
851 int count, uint32_t address)
853 uint32_t invalue, i;
854 int retval = ERROR_OK;
856 if (count >= 4)
857 return mem_ap_read_buf_packed_u16(dap, buffer, count, address);
859 while (count > 0) {
860 retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
861 if (retval != ERROR_OK)
862 return retval;
863 retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
864 if (retval != ERROR_OK)
865 break;
867 retval = dap_run(dap);
868 if (retval != ERROR_OK)
869 break;
871 if (address & 0x1) {
872 for (i = 0; i < 2; i++) {
873 *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
874 buffer++;
875 address++;
877 } else {
878 uint16_t svalue = (invalue >> 8 * (address & 0x3));
879 memcpy(buffer, &svalue, sizeof(uint16_t));
880 address += 2;
881 buffer += 2;
883 count -= 2;
886 return retval;
889 /* FIX!!! is this a potential performance bottleneck w.r.t. requiring too many
890 * roundtrips when jtag_execute_queue() has a large overhead(e.g. for USB)s?
892 * The solution is to arrange for a large out/in scan in this loop and
893 * and convert data afterwards.
895 static int mem_ap_read_buf_packed_u8(struct adiv5_dap *dap,
896 uint8_t *buffer, int count, uint32_t address)
898 uint32_t invalue;
899 int retval = ERROR_OK;
900 int wcount, blocksize, readcount, i;
902 wcount = count;
904 while (wcount > 0) {
905 int nbytes;
907 /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
908 blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
910 if (wcount < blocksize)
911 blocksize = wcount;
913 retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_PACKED, address);
914 if (retval != ERROR_OK)
915 return retval;
916 readcount = blocksize;
918 do {
919 retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
920 if (retval != ERROR_OK)
921 return retval;
922 retval = dap_run(dap);
923 if (retval != ERROR_OK) {
924 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
925 return retval;
928 nbytes = MIN(readcount, 4);
930 for (i = 0; i < nbytes; i++) {
931 *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
932 buffer++;
933 address++;
936 readcount -= nbytes;
937 } while (readcount);
938 wcount -= blocksize;
941 return retval;
945 * Synchronously read a block of bytes into a buffer
946 * @param dap The DAP connected to the MEM-AP.
947 * @param buffer where the bytes will be stored.
948 * @param count How many bytes to read.
949 * @param address Memory address from which to read data; all the
950 * data must be readable by the currently selected MEM-AP.
952 int mem_ap_read_buf_u8(struct adiv5_dap *dap, uint8_t *buffer,
953 int count, uint32_t address)
955 uint32_t invalue;
956 int retval = ERROR_OK;
958 if (count >= 4)
959 return mem_ap_read_buf_packed_u8(dap, buffer, count, address);
961 while (count > 0) {
962 retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
963 if (retval != ERROR_OK)
964 return retval;
965 retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
966 if (retval != ERROR_OK)
967 return retval;
968 retval = dap_run(dap);
969 if (retval != ERROR_OK)
970 break;
972 *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
973 count--;
974 address++;
975 buffer++;
978 return retval;
981 /*--------------------------------------------------------------------*/
982 /* Wrapping function with selection of AP */
983 /*--------------------------------------------------------------------*/
984 int mem_ap_sel_read_u32(struct adiv5_dap *swjdp, uint8_t ap,
985 uint32_t address, uint32_t *value)
987 dap_ap_select(swjdp, ap);
988 return mem_ap_read_u32(swjdp, address, value);
991 int mem_ap_sel_write_u32(struct adiv5_dap *swjdp, uint8_t ap,
992 uint32_t address, uint32_t value)
994 dap_ap_select(swjdp, ap);
995 return mem_ap_write_u32(swjdp, address, value);
998 int mem_ap_sel_read_atomic_u32(struct adiv5_dap *swjdp, uint8_t ap,
999 uint32_t address, uint32_t *value)
1001 dap_ap_select(swjdp, ap);
1002 return mem_ap_read_atomic_u32(swjdp, address, value);
1005 int mem_ap_sel_write_atomic_u32(struct adiv5_dap *swjdp, uint8_t ap,
1006 uint32_t address, uint32_t value)
1008 dap_ap_select(swjdp, ap);
1009 return mem_ap_write_atomic_u32(swjdp, address, value);
1012 int mem_ap_sel_read_buf_u8(struct adiv5_dap *swjdp, uint8_t ap,
1013 uint8_t *buffer, int count, uint32_t address)
1015 dap_ap_select(swjdp, ap);
1016 return mem_ap_read_buf_u8(swjdp, buffer, count, address);
1019 int mem_ap_sel_read_buf_u16(struct adiv5_dap *swjdp, uint8_t ap,
1020 uint8_t *buffer, int count, uint32_t address)
1022 dap_ap_select(swjdp, ap);
1023 return mem_ap_read_buf_u16(swjdp, buffer, count, address);
1026 int mem_ap_sel_read_buf_u32(struct adiv5_dap *swjdp, uint8_t ap,
1027 uint8_t *buffer, int count, uint32_t address)
1029 dap_ap_select(swjdp, ap);
1030 return mem_ap_read_buf_u32(swjdp, buffer, count, address);
1033 int mem_ap_sel_write_buf_u8(struct adiv5_dap *swjdp, uint8_t ap,
1034 const uint8_t *buffer, int count, uint32_t address)
1036 dap_ap_select(swjdp, ap);
1037 return mem_ap_write_buf_u8(swjdp, buffer, count, address);
1040 int mem_ap_sel_write_buf_u16(struct adiv5_dap *swjdp, uint8_t ap,
1041 const uint8_t *buffer, int count, uint32_t address)
1043 dap_ap_select(swjdp, ap);
1044 return mem_ap_write_buf_u16(swjdp, buffer, count, address);
1047 int mem_ap_sel_write_buf_u32(struct adiv5_dap *swjdp, uint8_t ap,
1048 const uint8_t *buffer, int count, uint32_t address)
1050 dap_ap_select(swjdp, ap);
1051 return mem_ap_write_buf_u32(swjdp, buffer, count, address);
1054 #define MDM_REG_STAT 0x00
1055 #define MDM_REG_CTRL 0x04
1056 #define MDM_REG_ID 0xfc
1058 #define MDM_STAT_FMEACK (1<<0)
1059 #define MDM_STAT_FREADY (1<<1)
1060 #define MDM_STAT_SYSSEC (1<<2)
1061 #define MDM_STAT_SYSRES (1<<3)
1062 #define MDM_STAT_FMEEN (1<<5)
1063 #define MDM_STAT_BACKDOOREN (1<<6)
1064 #define MDM_STAT_LPEN (1<<7)
1065 #define MDM_STAT_VLPEN (1<<8)
1066 #define MDM_STAT_LLSMODEXIT (1<<9)
1067 #define MDM_STAT_VLLSXMODEXIT (1<<10)
1068 #define MDM_STAT_CORE_HALTED (1<<16)
1069 #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
1070 #define MDM_STAT_CORESLEEPING (1<<18)
1072 #define MEM_CTRL_FMEIP (1<<0)
1073 #define MEM_CTRL_DBG_DIS (1<<1)
1074 #define MEM_CTRL_DBG_REQ (1<<2)
1075 #define MEM_CTRL_SYS_RES_REQ (1<<3)
1076 #define MEM_CTRL_CORE_HOLD_RES (1<<4)
1077 #define MEM_CTRL_VLLSX_DBG_REQ (1<<5)
1078 #define MEM_CTRL_VLLSX_DBG_ACK (1<<6)
1079 #define MEM_CTRL_VLLSX_STAT_ACK (1<<7)
1084 int dap_syssec_kinetis_mdmap(struct adiv5_dap *dap)
1086 uint32_t val;
1087 int retval;
1088 enum reset_types jtag_reset_config = jtag_get_reset_config();
1090 dap_ap_select(dap, 1);
1092 /* first check mdm-ap id register */
1093 retval = dap_queue_ap_read(dap, MDM_REG_ID, &val);
1094 if (retval != ERROR_OK)
1095 return retval;
1096 dap_run(dap);
1098 if (val != 0x001C0000) {
1099 LOG_DEBUG("id doesn't match %08X != 0x001C0000", val);
1100 dap_ap_select(dap, 0);
1101 return ERROR_FAIL;
1104 /* read and parse status register
1105 * it's important that the device is out of
1106 * reset here
1108 retval = dap_queue_ap_read(dap, MDM_REG_STAT, &val);
1109 if (retval != ERROR_OK)
1110 return retval;
1111 dap_run(dap);
1113 LOG_DEBUG("MDM_REG_STAT %08X", val);
1115 if ((val & (MDM_STAT_SYSSEC|MDM_STAT_FREADY)) != (MDM_STAT_FREADY)) {
1116 LOG_DEBUG("MDMAP: system is secured, masserase needed");
1118 if (!(val & MDM_STAT_FMEEN))
1119 LOG_DEBUG("MDMAP: masserase is disabled");
1120 else {
1121 /* we need to assert reset */
1122 if (jtag_reset_config & RESET_HAS_SRST) {
1123 /* default to asserting srst */
1124 adapter_assert_reset();
1125 } else {
1126 LOG_DEBUG("SRST not configured");
1127 dap_ap_select(dap, 0);
1128 return ERROR_FAIL;
1131 while (1) {
1132 retval = dap_queue_ap_write(dap, MDM_REG_CTRL, MEM_CTRL_FMEIP);
1133 if (retval != ERROR_OK)
1134 return retval;
1135 dap_run(dap);
1136 /* read status register and wait for ready */
1137 retval = dap_queue_ap_read(dap, MDM_REG_STAT, &val);
1138 if (retval != ERROR_OK)
1139 return retval;
1140 dap_run(dap);
1141 LOG_DEBUG("MDM_REG_STAT %08X", val);
1143 if ((val & 1))
1144 break;
1147 while (1) {
1148 retval = dap_queue_ap_write(dap, MDM_REG_CTRL, 0);
1149 if (retval != ERROR_OK)
1150 return retval;
1151 dap_run(dap);
1152 /* read status register */
1153 retval = dap_queue_ap_read(dap, MDM_REG_STAT, &val);
1154 if (retval != ERROR_OK)
1155 return retval;
1156 dap_run(dap);
1157 LOG_DEBUG("MDM_REG_STAT %08X", val);
1158 /* read control register and wait for ready */
1159 retval = dap_queue_ap_read(dap, MDM_REG_CTRL, &val);
1160 if (retval != ERROR_OK)
1161 return retval;
1162 dap_run(dap);
1163 LOG_DEBUG("MDM_REG_CTRL %08X", val);
1165 if (val == 0x00)
1166 break;
1171 dap_ap_select(dap, 0);
1173 return ERROR_OK;
1176 /** */
1177 struct dap_syssec_filter {
1178 /** */
1179 uint32_t idcode;
1180 /** */
1181 int (*dap_init)(struct adiv5_dap *dap);
1184 /** */
1185 static struct dap_syssec_filter dap_syssec_filter_data[] = {
1186 { 0x4BA00477, dap_syssec_kinetis_mdmap }
1192 int dap_syssec(struct adiv5_dap *dap)
1194 unsigned int i;
1195 struct jtag_tap *tap;
1197 for (i = 0; i < sizeof(dap_syssec_filter_data); i++) {
1198 tap = dap->jtag_info->tap;
1200 while (tap != NULL) {
1201 if (tap->hasidcode && (dap_syssec_filter_data[i].idcode == tap->idcode)) {
1202 LOG_DEBUG("DAP: mdmap_init for idcode: %08x", tap->idcode);
1203 dap_syssec_filter_data[i].dap_init(dap);
1205 tap = tap->next_tap;
1209 return ERROR_OK;
1212 /*--------------------------------------------------------------------------*/
1215 /* FIXME don't import ... just initialize as
1216 * part of DAP transport setup
1218 extern const struct dap_ops jtag_dp_ops;
1220 /*--------------------------------------------------------------------------*/
1223 * Initialize a DAP. This sets up the power domains, prepares the DP
1224 * for further use, and arranges to use AP #0 for all AP operations
1225 * until dap_ap-select() changes that policy.
1227 * @param dap The DAP being initialized.
1229 * @todo Rename this. We also need an initialization scheme which account
1230 * for SWD transports not just JTAG; that will need to address differences
1231 * in layering. (JTAG is useful without any debug target; but not SWD.)
1232 * And this may not even use an AHB-AP ... e.g. DAP-Lite uses an APB-AP.
1234 int ahbap_debugport_init(struct adiv5_dap *dap)
1236 uint32_t ctrlstat;
1237 int cnt = 0;
1238 int retval;
1240 LOG_DEBUG(" ");
1242 /* JTAG-DP or SWJ-DP, in JTAG mode
1243 * ... for SWD mode this is patched as part
1244 * of link switchover
1246 if (!dap->ops)
1247 dap->ops = &jtag_dap_ops;
1249 /* Default MEM-AP setup.
1251 * REVISIT AP #0 may be an inappropriate default for this.
1252 * Should we probe, or take a hint from the caller?
1253 * Presumably we can ignore the possibility of multiple APs.
1255 dap->ap_current = !0;
1256 dap_ap_select(dap, 0);
1258 /* DP initialization */
1260 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
1261 if (retval != ERROR_OK)
1262 return retval;
1264 retval = dap_queue_dp_write(dap, DP_CTRL_STAT, SSTICKYERR);
1265 if (retval != ERROR_OK)
1266 return retval;
1268 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
1269 if (retval != ERROR_OK)
1270 return retval;
1272 dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
1273 retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
1274 if (retval != ERROR_OK)
1275 return retval;
1277 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
1278 if (retval != ERROR_OK)
1279 return retval;
1280 retval = dap_run(dap);
1281 if (retval != ERROR_OK)
1282 return retval;
1284 /* Check that we have debug power domains activated */
1285 while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10)) {
1286 LOG_DEBUG("DAP: wait CDBGPWRUPACK");
1287 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
1288 if (retval != ERROR_OK)
1289 return retval;
1290 retval = dap_run(dap);
1291 if (retval != ERROR_OK)
1292 return retval;
1293 alive_sleep(10);
1296 while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10)) {
1297 LOG_DEBUG("DAP: wait CSYSPWRUPACK");
1298 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
1299 if (retval != ERROR_OK)
1300 return retval;
1301 retval = dap_run(dap);
1302 if (retval != ERROR_OK)
1303 return retval;
1304 alive_sleep(10);
1307 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
1308 if (retval != ERROR_OK)
1309 return retval;
1310 /* With debug power on we can activate OVERRUN checking */
1311 dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
1312 retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
1313 if (retval != ERROR_OK)
1314 return retval;
1315 retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
1316 if (retval != ERROR_OK)
1317 return retval;
1319 dap_syssec(dap);
1321 return ERROR_OK;
1324 /* CID interpretation -- see ARM IHI 0029B section 3
1325 * and ARM IHI 0031A table 13-3.
1327 static const char *class_description[16] = {
1328 "Reserved", "ROM table", "Reserved", "Reserved",
1329 "Reserved", "Reserved", "Reserved", "Reserved",
1330 "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
1331 "Reserved", "OptimoDE DESS",
1332 "Generic IP component", "PrimeCell or System component"
1335 static bool is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
1337 return cid3 == 0xb1 && cid2 == 0x05
1338 && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
1341 int dap_get_debugbase(struct adiv5_dap *dap, int ap,
1342 uint32_t *out_dbgbase, uint32_t *out_apid)
1344 uint32_t ap_old;
1345 int retval;
1346 uint32_t dbgbase, apid;
1348 /* AP address is in bits 31:24 of DP_SELECT */
1349 if (ap >= 256)
1350 return ERROR_COMMAND_SYNTAX_ERROR;
1352 ap_old = dap->ap_current;
1353 dap_ap_select(dap, ap);
1355 retval = dap_queue_ap_read(dap, AP_REG_BASE, &dbgbase);
1356 if (retval != ERROR_OK)
1357 return retval;
1358 retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
1359 if (retval != ERROR_OK)
1360 return retval;
1361 retval = dap_run(dap);
1362 if (retval != ERROR_OK)
1363 return retval;
1365 /* Excavate the device ID code */
1366 struct jtag_tap *tap = dap->jtag_info->tap;
1367 while (tap != NULL) {
1368 if (tap->hasidcode)
1369 break;
1370 tap = tap->next_tap;
1372 if (tap == NULL || !tap->hasidcode)
1373 return ERROR_OK;
1375 dap_ap_select(dap, ap_old);
1377 /* The asignment happens only here to prevent modification of these
1378 * values before they are certain. */
1379 *out_dbgbase = dbgbase;
1380 *out_apid = apid;
1382 return ERROR_OK;
1385 int dap_lookup_cs_component(struct adiv5_dap *dap, int ap,
1386 uint32_t dbgbase, uint8_t type, uint32_t *addr)
1388 uint32_t ap_old;
1389 uint32_t romentry, entry_offset = 0, component_base, devtype;
1390 int retval = ERROR_FAIL;
1392 if (ap >= 256)
1393 return ERROR_COMMAND_SYNTAX_ERROR;
1395 ap_old = dap->ap_current;
1396 dap_ap_select(dap, ap);
1398 do {
1399 retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) |
1400 entry_offset, &romentry);
1401 if (retval != ERROR_OK)
1402 return retval;
1404 component_base = (dbgbase & 0xFFFFF000)
1405 + (romentry & 0xFFFFF000);
1407 if (romentry & 0x1) {
1408 retval = mem_ap_read_atomic_u32(dap,
1409 (component_base & 0xfffff000) | 0xfcc,
1410 &devtype);
1411 if (retval != ERROR_OK)
1412 return retval;
1413 if ((devtype & 0xff) == type) {
1414 *addr = component_base;
1415 retval = ERROR_OK;
1416 break;
1419 entry_offset += 4;
1420 } while (romentry > 0);
1422 dap_ap_select(dap, ap_old);
1424 return retval;
1427 static int dap_info_command(struct command_context *cmd_ctx,
1428 struct adiv5_dap *dap, int ap)
1430 int retval;
1431 uint32_t dbgbase = 0, apid = 0; /* Silence gcc by initializing */
1432 int romtable_present = 0;
1433 uint8_t mem_ap;
1434 uint32_t ap_old;
1436 retval = dap_get_debugbase(dap, ap, &dbgbase, &apid);
1437 if (retval != ERROR_OK)
1438 return retval;
1440 ap_old = dap->ap_current;
1441 dap_ap_select(dap, ap);
1443 /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
1444 mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
1445 command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
1446 if (apid) {
1447 switch (apid&0x0F) {
1448 case 0:
1449 command_print(cmd_ctx, "\tType is JTAG-AP");
1450 break;
1451 case 1:
1452 command_print(cmd_ctx, "\tType is MEM-AP AHB");
1453 break;
1454 case 2:
1455 command_print(cmd_ctx, "\tType is MEM-AP APB");
1456 break;
1457 default:
1458 command_print(cmd_ctx, "\tUnknown AP type");
1459 break;
1462 /* NOTE: a MEM-AP may have a single CoreSight component that's
1463 * not a ROM table ... or have no such components at all.
1465 if (mem_ap)
1466 command_print(cmd_ctx, "AP BASE 0x%8.8" PRIx32, dbgbase);
1467 } else
1468 command_print(cmd_ctx, "No AP found at this ap 0x%x", ap);
1470 romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
1471 if (romtable_present) {
1472 uint32_t cid0, cid1, cid2, cid3, memtype, romentry;
1473 uint16_t entry_offset;
1475 /* bit 16 of apid indicates a memory access port */
1476 if (dbgbase & 0x02)
1477 command_print(cmd_ctx, "\tValid ROM table present");
1478 else
1479 command_print(cmd_ctx, "\tROM table in legacy format");
1481 /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
1482 retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
1483 if (retval != ERROR_OK)
1484 return retval;
1485 retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
1486 if (retval != ERROR_OK)
1487 return retval;
1488 retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
1489 if (retval != ERROR_OK)
1490 return retval;
1491 retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
1492 if (retval != ERROR_OK)
1493 return retval;
1494 retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
1495 if (retval != ERROR_OK)
1496 return retval;
1497 retval = dap_run(dap);
1498 if (retval != ERROR_OK)
1499 return retval;
1501 if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1502 command_print(cmd_ctx, "\tCID3 0x%2.2x"
1503 ", CID2 0x%2.2x"
1504 ", CID1 0x%2.2x"
1505 ", CID0 0x%2.2x",
1506 (unsigned) cid3, (unsigned)cid2,
1507 (unsigned) cid1, (unsigned) cid0);
1508 if (memtype & 0x01)
1509 command_print(cmd_ctx, "\tMEMTYPE system memory present on bus");
1510 else
1511 command_print(cmd_ctx, "\tMEMTYPE System memory not present. "
1512 "Dedicated debug bus.");
1514 /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
1515 entry_offset = 0;
1516 do {
1517 retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
1518 if (retval != ERROR_OK)
1519 return retval;
1520 command_print(cmd_ctx, "\tROMTABLE[0x%x] = 0x%" PRIx32 "", entry_offset, romentry);
1521 if (romentry & 0x01) {
1522 uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
1523 uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
1524 uint32_t component_base;
1525 unsigned part_num;
1526 char *type, *full;
1528 component_base = (dbgbase & 0xFFFFF000) + (romentry & 0xFFFFF000);
1530 /* IDs are in last 4K section */
1531 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE0, &c_pid0);
1532 if (retval != ERROR_OK)
1533 return retval;
1534 c_pid0 &= 0xff;
1535 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE4, &c_pid1);
1536 if (retval != ERROR_OK)
1537 return retval;
1538 c_pid1 &= 0xff;
1539 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE8, &c_pid2);
1540 if (retval != ERROR_OK)
1541 return retval;
1542 c_pid2 &= 0xff;
1543 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFEC, &c_pid3);
1544 if (retval != ERROR_OK)
1545 return retval;
1546 c_pid3 &= 0xff;
1547 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFD0, &c_pid4);
1548 if (retval != ERROR_OK)
1549 return retval;
1550 c_pid4 &= 0xff;
1552 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF0, &c_cid0);
1553 if (retval != ERROR_OK)
1554 return retval;
1555 c_cid0 &= 0xff;
1556 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF4, &c_cid1);
1557 if (retval != ERROR_OK)
1558 return retval;
1559 c_cid1 &= 0xff;
1560 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF8, &c_cid2);
1561 if (retval != ERROR_OK)
1562 return retval;
1563 c_cid2 &= 0xff;
1564 retval = mem_ap_read_atomic_u32(dap, component_base + 0xFFC, &c_cid3);
1565 if (retval != ERROR_OK)
1566 return retval;
1567 c_cid3 &= 0xff;
1569 command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32 ","
1570 "start address 0x%" PRIx32, component_base,
1571 /* component may take multiple 4K pages */
1572 component_base - 0x1000*(c_pid4 >> 4));
1573 command_print(cmd_ctx, "\t\tComponent class is 0x%x, %s",
1574 (int) (c_cid1 >> 4) & 0xf,
1575 /* See ARM IHI 0029B Table 3-3 */
1576 class_description[(c_cid1 >> 4) & 0xf]);
1578 /* CoreSight component? */
1579 if (((c_cid1 >> 4) & 0x0f) == 9) {
1580 uint32_t devtype;
1581 unsigned minor;
1582 char *major = "Reserved", *subtype = "Reserved";
1584 retval = mem_ap_read_atomic_u32(dap,
1585 (component_base & 0xfffff000) | 0xfcc,
1586 &devtype);
1587 if (retval != ERROR_OK)
1588 return retval;
1589 minor = (devtype >> 4) & 0x0f;
1590 switch (devtype & 0x0f) {
1591 case 0:
1592 major = "Miscellaneous";
1593 switch (minor) {
1594 case 0:
1595 subtype = "other";
1596 break;
1597 case 4:
1598 subtype = "Validation component";
1599 break;
1601 break;
1602 case 1:
1603 major = "Trace Sink";
1604 switch (minor) {
1605 case 0:
1606 subtype = "other";
1607 break;
1608 case 1:
1609 subtype = "Port";
1610 break;
1611 case 2:
1612 subtype = "Buffer";
1613 break;
1615 break;
1616 case 2:
1617 major = "Trace Link";
1618 switch (minor) {
1619 case 0:
1620 subtype = "other";
1621 break;
1622 case 1:
1623 subtype = "Funnel, router";
1624 break;
1625 case 2:
1626 subtype = "Filter";
1627 break;
1628 case 3:
1629 subtype = "FIFO, buffer";
1630 break;
1632 break;
1633 case 3:
1634 major = "Trace Source";
1635 switch (minor) {
1636 case 0:
1637 subtype = "other";
1638 break;
1639 case 1:
1640 subtype = "Processor";
1641 break;
1642 case 2:
1643 subtype = "DSP";
1644 break;
1645 case 3:
1646 subtype = "Engine/Coprocessor";
1647 break;
1648 case 4:
1649 subtype = "Bus";
1650 break;
1652 break;
1653 case 4:
1654 major = "Debug Control";
1655 switch (minor) {
1656 case 0:
1657 subtype = "other";
1658 break;
1659 case 1:
1660 subtype = "Trigger Matrix";
1661 break;
1662 case 2:
1663 subtype = "Debug Auth";
1664 break;
1666 break;
1667 case 5:
1668 major = "Debug Logic";
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;
1683 break;
1685 command_print(cmd_ctx, "\t\tType is 0x%2.2x, %s, %s",
1686 (unsigned) (devtype & 0xff),
1687 major, subtype);
1688 /* REVISIT also show 0xfc8 DevId */
1691 if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1692 command_print(cmd_ctx,
1693 "\t\tCID3 0%2.2x"
1694 ", CID2 0%2.2x"
1695 ", CID1 0%2.2x"
1696 ", CID0 0%2.2x",
1697 (int) c_cid3,
1698 (int) c_cid2,
1699 (int)c_cid1,
1700 (int)c_cid0);
1701 command_print(cmd_ctx,
1702 "\t\tPeripheral ID[4..0] = hex "
1703 "%2.2x %2.2x %2.2x %2.2x %2.2x",
1704 (int) c_pid4, (int) c_pid3, (int) c_pid2,
1705 (int) c_pid1, (int) c_pid0);
1707 /* Part number interpretations are from Cortex
1708 * core specs, the CoreSight components TRM
1709 * (ARM DDI 0314H), CoreSight System Design
1710 * Guide (ARM DGI 0012D) and ETM specs; also
1711 * from chip observation (e.g. TI SDTI).
1713 part_num = (c_pid0 & 0xff);
1714 part_num |= (c_pid1 & 0x0f) << 8;
1715 switch (part_num) {
1716 case 0x000:
1717 type = "Cortex-M3 NVIC";
1718 full = "(Interrupt Controller)";
1719 break;
1720 case 0x001:
1721 type = "Cortex-M3 ITM";
1722 full = "(Instrumentation Trace Module)";
1723 break;
1724 case 0x002:
1725 type = "Cortex-M3 DWT";
1726 full = "(Data Watchpoint and Trace)";
1727 break;
1728 case 0x003:
1729 type = "Cortex-M3 FBP";
1730 full = "(Flash Patch and Breakpoint)";
1731 break;
1732 case 0x00c:
1733 type = "Cortex-M4 SCS";
1734 full = "(System Control Space)";
1735 break;
1736 case 0x00d:
1737 type = "CoreSight ETM11";
1738 full = "(Embedded Trace)";
1739 break;
1740 /* case 0x113: what? */
1741 case 0x120: /* from OMAP3 memmap */
1742 type = "TI SDTI";
1743 full = "(System Debug Trace Interface)";
1744 break;
1745 case 0x343: /* from OMAP3 memmap */
1746 type = "TI DAPCTL";
1747 full = "";
1748 break;
1749 case 0x906:
1750 type = "Coresight CTI";
1751 full = "(Cross Trigger)";
1752 break;
1753 case 0x907:
1754 type = "Coresight ETB";
1755 full = "(Trace Buffer)";
1756 break;
1757 case 0x908:
1758 type = "Coresight CSTF";
1759 full = "(Trace Funnel)";
1760 break;
1761 case 0x910:
1762 type = "CoreSight ETM9";
1763 full = "(Embedded Trace)";
1764 break;
1765 case 0x912:
1766 type = "Coresight TPIU";
1767 full = "(Trace Port Interface Unit)";
1768 break;
1769 case 0x921:
1770 type = "Cortex-A8 ETM";
1771 full = "(Embedded Trace)";
1772 break;
1773 case 0x922:
1774 type = "Cortex-A8 CTI";
1775 full = "(Cross Trigger)";
1776 break;
1777 case 0x923:
1778 type = "Cortex-M3 TPIU";
1779 full = "(Trace Port Interface Unit)";
1780 break;
1781 case 0x924:
1782 type = "Cortex-M3 ETM";
1783 full = "(Embedded Trace)";
1784 break;
1785 case 0x925:
1786 type = "Cortex-M4 ETM";
1787 full = "(Embedded Trace)";
1788 break;
1789 case 0x930:
1790 type = "Cortex-R4 ETM";
1791 full = "(Embedded Trace)";
1792 break;
1793 case 0x9a1:
1794 type = "Cortex-M4 TPUI";
1795 full = "(Trace Port Interface Unit)";
1796 break;
1797 case 0xc08:
1798 type = "Cortex-A8 Debug";
1799 full = "(Debug Unit)";
1800 break;
1801 default:
1802 type = "-*- unrecognized -*-";
1803 full = "";
1804 break;
1806 command_print(cmd_ctx, "\t\tPart is %s %s",
1807 type, full);
1808 } else {
1809 if (romentry)
1810 command_print(cmd_ctx, "\t\tComponent not present");
1811 else
1812 command_print(cmd_ctx, "\t\tEnd of ROM table");
1814 entry_offset += 4;
1815 } while (romentry > 0);
1816 } else
1817 command_print(cmd_ctx, "\tNo ROM table present");
1818 dap_ap_select(dap, ap_old);
1820 return ERROR_OK;
1823 COMMAND_HANDLER(handle_dap_info_command)
1825 struct target *target = get_current_target(CMD_CTX);
1826 struct arm *arm = target_to_arm(target);
1827 struct adiv5_dap *dap = arm->dap;
1828 uint32_t apsel;
1830 switch (CMD_ARGC) {
1831 case 0:
1832 apsel = dap->apsel;
1833 break;
1834 case 1:
1835 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1836 break;
1837 default:
1838 return ERROR_COMMAND_SYNTAX_ERROR;
1841 return dap_info_command(CMD_CTX, dap, apsel);
1844 COMMAND_HANDLER(dap_baseaddr_command)
1846 struct target *target = get_current_target(CMD_CTX);
1847 struct arm *arm = target_to_arm(target);
1848 struct adiv5_dap *dap = arm->dap;
1850 uint32_t apsel, baseaddr;
1851 int retval;
1853 switch (CMD_ARGC) {
1854 case 0:
1855 apsel = dap->apsel;
1856 break;
1857 case 1:
1858 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1859 /* AP address is in bits 31:24 of DP_SELECT */
1860 if (apsel >= 256)
1861 return ERROR_COMMAND_SYNTAX_ERROR;
1862 break;
1863 default:
1864 return ERROR_COMMAND_SYNTAX_ERROR;
1867 dap_ap_select(dap, apsel);
1869 /* NOTE: assumes we're talking to a MEM-AP, which
1870 * has a base address. There are other kinds of AP,
1871 * though they're not common for now. This should
1872 * use the ID register to verify it's a MEM-AP.
1874 retval = dap_queue_ap_read(dap, AP_REG_BASE, &baseaddr);
1875 if (retval != ERROR_OK)
1876 return retval;
1877 retval = dap_run(dap);
1878 if (retval != ERROR_OK)
1879 return retval;
1881 command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
1883 return retval;
1886 COMMAND_HANDLER(dap_memaccess_command)
1888 struct target *target = get_current_target(CMD_CTX);
1889 struct arm *arm = target_to_arm(target);
1890 struct adiv5_dap *dap = arm->dap;
1892 uint32_t memaccess_tck;
1894 switch (CMD_ARGC) {
1895 case 0:
1896 memaccess_tck = dap->memaccess_tck;
1897 break;
1898 case 1:
1899 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
1900 break;
1901 default:
1902 return ERROR_COMMAND_SYNTAX_ERROR;
1904 dap->memaccess_tck = memaccess_tck;
1906 command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
1907 dap->memaccess_tck);
1909 return ERROR_OK;
1912 COMMAND_HANDLER(dap_apsel_command)
1914 struct target *target = get_current_target(CMD_CTX);
1915 struct arm *arm = target_to_arm(target);
1916 struct adiv5_dap *dap = arm->dap;
1918 uint32_t apsel, apid;
1919 int retval;
1921 switch (CMD_ARGC) {
1922 case 0:
1923 apsel = 0;
1924 break;
1925 case 1:
1926 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1927 /* AP address is in bits 31:24 of DP_SELECT */
1928 if (apsel >= 256)
1929 return ERROR_COMMAND_SYNTAX_ERROR;
1930 break;
1931 default:
1932 return ERROR_COMMAND_SYNTAX_ERROR;
1935 dap->apsel = apsel;
1936 dap_ap_select(dap, apsel);
1938 retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
1939 if (retval != ERROR_OK)
1940 return retval;
1941 retval = dap_run(dap);
1942 if (retval != ERROR_OK)
1943 return retval;
1945 command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
1946 apsel, apid);
1948 return retval;
1951 COMMAND_HANDLER(dap_apid_command)
1953 struct target *target = get_current_target(CMD_CTX);
1954 struct arm *arm = target_to_arm(target);
1955 struct adiv5_dap *dap = arm->dap;
1957 uint32_t apsel, apid;
1958 int retval;
1960 switch (CMD_ARGC) {
1961 case 0:
1962 apsel = dap->apsel;
1963 break;
1964 case 1:
1965 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1966 /* AP address is in bits 31:24 of DP_SELECT */
1967 if (apsel >= 256)
1968 return ERROR_COMMAND_SYNTAX_ERROR;
1969 break;
1970 default:
1971 return ERROR_COMMAND_SYNTAX_ERROR;
1974 dap_ap_select(dap, apsel);
1976 retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
1977 if (retval != ERROR_OK)
1978 return retval;
1979 retval = dap_run(dap);
1980 if (retval != ERROR_OK)
1981 return retval;
1983 command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
1985 return retval;
1988 static const struct command_registration dap_commands[] = {
1990 .name = "info",
1991 .handler = handle_dap_info_command,
1992 .mode = COMMAND_EXEC,
1993 .help = "display ROM table for MEM-AP "
1994 "(default currently selected AP)",
1995 .usage = "[ap_num]",
1998 .name = "apsel",
1999 .handler = dap_apsel_command,
2000 .mode = COMMAND_EXEC,
2001 .help = "Set the currently selected AP (default 0) "
2002 "and display the result",
2003 .usage = "[ap_num]",
2006 .name = "apid",
2007 .handler = dap_apid_command,
2008 .mode = COMMAND_EXEC,
2009 .help = "return ID register from AP "
2010 "(default currently selected AP)",
2011 .usage = "[ap_num]",
2014 .name = "baseaddr",
2015 .handler = dap_baseaddr_command,
2016 .mode = COMMAND_EXEC,
2017 .help = "return debug base address from MEM-AP "
2018 "(default currently selected AP)",
2019 .usage = "[ap_num]",
2022 .name = "memaccess",
2023 .handler = dap_memaccess_command,
2024 .mode = COMMAND_EXEC,
2025 .help = "set/get number of extra tck for MEM-AP memory "
2026 "bus access [0-255]",
2027 .usage = "[cycles]",
2029 COMMAND_REGISTRATION_DONE
2032 const struct command_registration dap_command_handlers[] = {
2034 .name = "dap",
2035 .mode = COMMAND_EXEC,
2036 .help = "DAP command group",
2037 .usage = "",
2038 .chain = dap_commands,
2040 COMMAND_REGISTRATION_DONE