1 /***************************************************************************
2 * Copyright (C) 2006 by Magnus Lundin *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2009 by Oyvind Harboe *
9 * oyvind.harboe@zylin.com *
11 * Copyright (C) 2009-2010 by David Brownell *
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. *
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. *
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 ***************************************************************************/
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
73 #include "arm_adi_v5.h"
74 #include <helper/time_support.h>
77 /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
80 uint32_t tar_block_size(uint32_t address)
81 Return the largest block starting at address that does not cross a tar block size alignment boundary
83 static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
85 return (tar_autoincr_block - ((tar_autoincr_block - 1) & address)) >> 2;
88 /***************************************************************************
90 <<<<<<< HEAD:src/target/arm_adi_v5.c
92 * DPACC and APACC scanchain access through JTAG-DP *
94 ***************************************************************************/
97 * Scan DPACC or APACC using target ordered uint8_t buffers. No endianness
98 * conversions are performed. See section 4.4.3 of the ADIv5 spec, which
99 * discusses operations which access these registers.
101 * Note that only one scan is performed. If RnW is set, a separate scan
102 * will be needed to collect the data which was read; the "invalue" collects
103 * the posted result of a preceding operation, not the current one.
105 * @param swjdp the DAP
106 * @param instr JTAG_DP_APACC (AP access) or JTAG_DP_DPACC (DP access)
107 * @param reg_addr two significant bits; A[3:2]; for APACC access, the
108 * SELECT register has more addressing bits.
109 * @param RnW false iff outvalue will be written to the DP or AP
110 * @param outvalue points to a 32-bit (little-endian) integer
111 * @param invalue NULL, or points to a 32-bit (little-endian) integer
112 * @param ack points to where the three bit JTAG_ACK_* code will be stored
114 static int adi_jtag_dp_scan(struct adiv5_dap *swjdp,
115 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
116 uint8_t *outvalue, uint8_t *invalue, uint8_t *ack)
118 struct arm_jtag *jtag_info = swjdp->jtag_info;
119 struct scan_field fields[2];
120 uint8_t out_addr_buf;
122 jtag_set_end_state(TAP_IDLE);
123 arm_jtag_set_instr(jtag_info, instr, NULL);
125 /* Scan out a read or write operation using some DP or AP register.
126 * For APACC access with any sticky error flag set, this is discarded.
128 fields[0].num_bits = 3;
129 buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
130 fields[0].out_value = &out_addr_buf;
131 fields[0].in_value = ack;
133 /* NOTE: if we receive JTAG_ACK_WAIT, the previous operation did not
134 * complete; data we write is discarded, data we read is unpredictable.
135 * When overrun detect is active, STICKYORUN is set.
138 fields[1].num_bits = 32;
139 fields[1].out_value = outvalue;
140 fields[1].in_value = invalue;
142 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
144 /* Add specified number of tck clocks after starting memory bus
145 * access, giving the hardware time to complete the access.
146 * They provide more time for the (MEM) AP to complete the read ...
147 * See "Minimum Response Time" for JTAG-DP, in the ADIv5 spec.
149 if ((instr == JTAG_DP_APACC)
150 && ((reg_addr == AP_REG_DRW)
151 || ((reg_addr & 0xF0) == AP_REG_BD0))
152 && (swjdp->memaccess_tck != 0))
153 jtag_add_runtest(swjdp->memaccess_tck,
156 return jtag_get_error();
160 * Scan DPACC or APACC out and in from host ordered uint32_t buffers.
161 * This is exactly like adi_jtag_dp_scan(), except that endianness
162 * conversions are performed (so the types of invalue and outvalue
163 * must be different).
165 static int adi_jtag_dp_scan_u32(struct adiv5_dap *swjdp,
166 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
167 uint32_t outvalue, uint32_t *invalue, uint8_t *ack)
169 uint8_t out_value_buf[4];
172 buf_set_u32(out_value_buf, 0, 32, outvalue);
174 retval = adi_jtag_dp_scan(swjdp, instr, reg_addr, RnW,
175 out_value_buf, (uint8_t *)invalue, ack);
176 if (retval != ERROR_OK)
180 jtag_add_callback(arm_le_to_h_u32,
181 (jtag_callback_data_t) invalue);
187 * Utility to write AP registers.
189 static inline int adi_jtag_ap_write_check(struct adiv5_dap *dap,
190 uint8_t reg_addr, uint8_t *outvalue)
192 return adi_jtag_dp_scan(dap, JTAG_DP_APACC, reg_addr, DPAP_WRITE,
193 outvalue, NULL, NULL);
196 static int adi_jtag_scan_inout_check_u32(struct adiv5_dap *swjdp,
197 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
198 uint32_t outvalue, uint32_t *invalue)
202 /* Issue the read or write */
203 retval = adi_jtag_dp_scan_u32(swjdp, instr, reg_addr,
204 RnW, outvalue, NULL, NULL);
205 if (retval != ERROR_OK)
208 /* For reads, collect posted value; RDBUFF has no other effect.
209 * Assumes read gets acked with OK/FAULT, and CTRL_STAT says "OK".
211 if ((RnW == DPAP_READ) && (invalue != NULL))
212 retval = adi_jtag_dp_scan_u32(swjdp, JTAG_DP_DPACC,
213 DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
217 static int jtagdp_transaction_endcheck(struct adiv5_dap *swjdp)
222 /* too expensive to call keep_alive() here */
225 /* Danger!!!! BROKEN!!!! */
226 adi_jtag_scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
227 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
228 /* Danger!!!! BROKEN!!!! Why will jtag_execute_queue() fail here????
229 R956 introduced the check on return value here and now Michael Schwingen reports
230 that this code no longer works....
232 https://lists.berlios.de/pipermail/openocd-development/2008-September/003107.html
234 if ((retval = jtag_execute_queue()) != ERROR_OK)
236 LOG_ERROR("BUG: Why does this fail the first time????");
238 /* Why??? second time it works??? */
241 /* Post CTRL/STAT read; discard any previous posted read value
242 * but collect its ACK status.
244 adi_jtag_scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
245 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
246 if ((retval = jtag_execute_queue()) != ERROR_OK)
249 swjdp->ack = swjdp->ack & 0x7;
251 /* common code path avoids calling timeval_ms() */
252 if (swjdp->ack != JTAG_ACK_OK_FAULT)
254 long long then = timeval_ms();
256 while (swjdp->ack != JTAG_ACK_OK_FAULT)
258 if (swjdp->ack == JTAG_ACK_WAIT)
260 if ((timeval_ms()-then) > 1000)
262 /* NOTE: this would be a good spot
263 * to use JTAG_DP_ABORT.
265 LOG_WARNING("Timeout (1000ms) waiting "
267 "in JTAG-DP transaction");
268 return ERROR_JTAG_DEVICE_ERROR;
273 LOG_WARNING("Invalid ACK %#x "
274 "in JTAG-DP transaction",
276 return ERROR_JTAG_DEVICE_ERROR;
279 adi_jtag_scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
280 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
281 if ((retval = dap_run(swjdp)) != ERROR_OK)
283 swjdp->ack = swjdp->ack & 0x7;
287 /* REVISIT also STICKYCMP, for pushed comparisons (nyet used) */
289 /* Check for STICKYERR and STICKYORUN */
290 if (ctrlstat & (SSTICKYORUN | SSTICKYERR))
292 LOG_DEBUG("jtag-dp: CTRL/STAT error, 0x%" PRIx32, ctrlstat);
293 /* Check power to debug regions */
294 if ((ctrlstat & 0xf0000000) != 0xf0000000)
295 ahbap_debugport_init(swjdp);
298 uint32_t mem_ap_csw, mem_ap_tar;
300 /* Maybe print information about last intended
301 * MEM-AP access; but not if autoincrementing.
302 * *Real* CSW and TAR values are always shown.
304 if (swjdp->ap_tar_value != (uint32_t) -1)
305 LOG_DEBUG("MEM-AP Cached values: "
307 ", ap_csw 0x%" PRIx32
308 ", ap_tar 0x%" PRIx32,
309 swjdp->ap_bank_value,
311 swjdp->ap_tar_value);
313 if (ctrlstat & SSTICKYORUN)
314 LOG_ERROR("JTAG-DP OVERRUN - check clock, "
315 "memaccess, or reduce jtag speed");
317 if (ctrlstat & SSTICKYERR)
318 LOG_ERROR("JTAG-DP STICKY ERROR");
320 /* Clear Sticky Error Bits */
321 adi_jtag_scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
322 DP_CTRL_STAT, DPAP_WRITE,
323 swjdp->dp_ctrl_stat | SSTICKYORUN
325 adi_jtag_scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
326 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
327 if ((retval = dap_run(swjdp)) != ERROR_OK)
330 LOG_DEBUG("jtag-dp: CTRL/STAT 0x%" PRIx32, ctrlstat);
332 retval = dap_queue_ap_read(swjdp,
333 AP_REG_CSW, &mem_ap_csw);
334 if (retval != ERROR_OK)
337 retval = dap_queue_ap_read(swjdp,
338 AP_REG_TAR, &mem_ap_tar);
339 if (retval != ERROR_OK)
342 if ((retval = dap_run(swjdp)) != ERROR_OK)
344 LOG_ERROR("MEM_AP_CSW 0x%" PRIx32 ", MEM_AP_TAR 0x%"
345 PRIx32, mem_ap_csw, mem_ap_tar);
348 if ((retval = dap_run(swjdp)) != ERROR_OK)
350 return ERROR_JTAG_DEVICE_ERROR;
356 /***************************************************************************
358 >>>>>>> jtag: cut down on usage of unintended modification of global end state:src/target/arm_adi_v5.c
359 * DP and MEM-AP register access through APACC and DPACC *
361 ***************************************************************************/
364 * Select one of the APs connected to the specified DAP. The
365 * selection is implicitly used with future AP transactions.
366 * This is a NOP if the specified AP is already selected.
368 * @param swjdp The DAP
369 * @param apsel Number of the AP to (implicitly) use with further
370 * transactions. This normally identifies a MEM-AP.
372 void dap_ap_select(struct adiv5_dap *swjdp,uint8_t apsel)
374 uint32_t select = (apsel << 24) & 0xFF000000;
376 if (select != swjdp->apsel)
378 swjdp->apsel = select;
379 /* Switching AP invalidates cached values.
380 * Values MUST BE UPDATED BEFORE AP ACCESS.
382 swjdp->ap_bank_value = -1;
383 swjdp->ap_csw_value = -1;
384 swjdp->ap_tar_value = -1;
389 * Queue transactions setting up transfer parameters for the
390 * currently selected MEM-AP.
392 * Subsequent transfers using registers like AP_REG_DRW or AP_REG_BD2
393 * initiate data reads or writes using memory or peripheral addresses.
394 * If the CSW is configured for it, the TAR may be automatically
395 * incremented after each transfer.
397 * @todo Rename to reflect it being specifically a MEM-AP function.
399 * @param swjdp The DAP connected to the MEM-AP.
400 * @param csw MEM-AP Control/Status Word (CSW) register to assign. If this
401 * matches the cached value, the register is not changed.
402 * @param tar MEM-AP Transfer Address Register (TAR) to assign. If this
403 * matches the cached address, the register is not changed.
405 * @return ERROR_OK if the transaction was properly queued, else a fault code.
407 int dap_setup_accessport(struct adiv5_dap *swjdp, uint32_t csw, uint32_t tar)
411 csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
412 if (csw != swjdp->ap_csw_value)
414 /* LOG_DEBUG("DAP: Set CSW %x",csw); */
415 retval = dap_queue_ap_write(swjdp, AP_REG_CSW, csw);
416 if (retval != ERROR_OK)
418 swjdp->ap_csw_value = csw;
420 if (tar != swjdp->ap_tar_value)
422 /* LOG_DEBUG("DAP: Set TAR %x",tar); */
423 retval = dap_queue_ap_write(swjdp, AP_REG_TAR, tar);
424 if (retval != ERROR_OK)
426 swjdp->ap_tar_value = tar;
428 /* Disable TAR cache when autoincrementing */
429 if (csw & CSW_ADDRINC_MASK)
430 swjdp->ap_tar_value = -1;
435 * Asynchronous (queued) read of a word from memory or a system register.
437 * @param swjdp The DAP connected to the MEM-AP performing the read.
438 * @param address Address of the 32-bit word to read; it must be
439 * readable by the currently selected MEM-AP.
440 * @param value points to where the word will be stored when the
441 * transaction queue is flushed (assuming no errors).
443 * @return ERROR_OK for success. Otherwise a fault code.
445 int mem_ap_read_u32(struct adiv5_dap *swjdp, uint32_t address,
450 /* Use banked addressing (REG_BDx) to avoid some link traffic
451 * (updating TAR) when reading several consecutive addresses.
453 retval = dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF,
454 address & 0xFFFFFFF0);
455 if (retval != ERROR_OK)
458 return dap_queue_ap_read(swjdp, AP_REG_BD0 | (address & 0xC), value);
462 * Synchronous read of a word from memory or a system register.
463 * As a side effect, this flushes any queued transactions.
465 * @param swjdp The DAP connected to the MEM-AP performing the read.
466 * @param address Address of the 32-bit word to read; it must be
467 * readable by the currently selected MEM-AP.
468 * @param value points to where the result will be stored.
470 * @return ERROR_OK for success; *value holds the result.
471 * Otherwise a fault code.
473 int mem_ap_read_atomic_u32(struct adiv5_dap *swjdp, uint32_t address,
478 retval = mem_ap_read_u32(swjdp, address, value);
479 if (retval != ERROR_OK)
482 return dap_run(swjdp);
486 * Asynchronous (queued) write of a word to memory or a system register.
488 * @param swjdp The DAP connected to the MEM-AP.
489 * @param address Address to be written; it must be writable by
490 * the currently selected MEM-AP.
491 * @param value Word that will be written to the address when transaction
492 * queue is flushed (assuming no errors).
494 * @return ERROR_OK for success. Otherwise a fault code.
496 int mem_ap_write_u32(struct adiv5_dap *swjdp, uint32_t address,
501 /* Use banked addressing (REG_BDx) to avoid some link traffic
502 * (updating TAR) when writing several consecutive addresses.
504 retval = dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF,
505 address & 0xFFFFFFF0);
506 if (retval != ERROR_OK)
509 return dap_queue_ap_write(swjdp, AP_REG_BD0 | (address & 0xC),
514 * Synchronous write of a word to memory or a system register.
515 * As a side effect, this flushes any queued transactions.
517 * @param swjdp The DAP connected to the MEM-AP.
518 * @param address Address to be written; it must be writable by
519 * the currently selected MEM-AP.
520 * @param value Word that will be written.
522 * @return ERROR_OK for success; the data was written. Otherwise a fault code.
524 int mem_ap_write_atomic_u32(struct adiv5_dap *swjdp, uint32_t address,
527 int retval = mem_ap_write_u32(swjdp, address, value);
529 if (retval != ERROR_OK)
532 return dap_run(swjdp);
535 /*****************************************************************************
537 * mem_ap_write_buf(struct adiv5_dap *swjdp, uint8_t *buffer, int count, uint32_t address) *
539 * Write a buffer in target order (little endian) *
541 *****************************************************************************/
542 int mem_ap_write_buf_u32(struct adiv5_dap *swjdp, uint8_t *buffer, int count, uint32_t address)
544 int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
545 uint32_t adr = address;
546 uint8_t* pBuffer = buffer;
551 /* if we have an unaligned access - reorder data */
554 for (writecount = 0; writecount < count; writecount++)
558 memcpy(&outvalue, pBuffer, sizeof(uint32_t));
560 for (i = 0; i < 4; i++)
562 *((uint8_t*)pBuffer + (adr & 0x3)) = outvalue;
566 pBuffer += sizeof(uint32_t);
572 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
573 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
574 if (wcount < blocksize)
577 /* handle unaligned data at 4k boundary */
581 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
583 for (writecount = 0; writecount < blocksize; writecount++)
585 retval = dap_queue_ap_write(swjdp, AP_REG_DRW,
586 *(uint32_t *) (buffer + 4 * writecount));
587 if (retval != ERROR_OK)
591 if (dap_run(swjdp) == ERROR_OK)
593 wcount = wcount - blocksize;
594 address = address + 4 * blocksize;
595 buffer = buffer + 4 * blocksize;
604 LOG_WARNING("Block write error address 0x%" PRIx32 ", wcount 0x%x", address, wcount);
605 /* REVISIT return the *actual* fault code */
606 return ERROR_JTAG_DEVICE_ERROR;
613 static int mem_ap_write_buf_packed_u16(struct adiv5_dap *swjdp,
614 uint8_t *buffer, int count, uint32_t address)
616 int retval = ERROR_OK;
617 int wcount, blocksize, writecount, i;
625 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
626 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
628 if (wcount < blocksize)
631 /* handle unaligned data at 4k boundary */
635 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
636 writecount = blocksize;
640 nbytes = MIN((writecount << 1), 4);
644 if (mem_ap_write_buf_u16(swjdp, buffer,
645 nbytes, address) != ERROR_OK)
647 LOG_WARNING("Block write error address "
648 "0x%" PRIx32 ", count 0x%x",
650 return ERROR_JTAG_DEVICE_ERROR;
653 address += nbytes >> 1;
658 memcpy(&outvalue, buffer, sizeof(uint32_t));
660 for (i = 0; i < nbytes; i++)
662 *((uint8_t*)buffer + (address & 0x3)) = outvalue;
667 memcpy(&outvalue, buffer, sizeof(uint32_t));
668 retval = dap_queue_ap_write(swjdp,
669 AP_REG_DRW, outvalue);
670 if (retval != ERROR_OK)
673 if (dap_run(swjdp) != ERROR_OK)
675 LOG_WARNING("Block write error address "
676 "0x%" PRIx32 ", count 0x%x",
678 /* REVISIT return *actual* fault code */
679 return ERROR_JTAG_DEVICE_ERROR;
683 buffer += nbytes >> 1;
684 writecount -= nbytes >> 1;
686 } while (writecount);
693 int mem_ap_write_buf_u16(struct adiv5_dap *swjdp, uint8_t *buffer, int count, uint32_t address)
695 int retval = ERROR_OK;
698 return mem_ap_write_buf_packed_u16(swjdp, buffer, count, address);
702 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
704 memcpy(&svalue, buffer, sizeof(uint16_t));
705 uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
706 retval = dap_queue_ap_write(swjdp, AP_REG_DRW, outvalue);
707 if (retval != ERROR_OK)
710 retval = dap_run(swjdp);
711 if (retval != ERROR_OK)
722 static int mem_ap_write_buf_packed_u8(struct adiv5_dap *swjdp,
723 uint8_t *buffer, int count, uint32_t address)
725 int retval = ERROR_OK;
726 int wcount, blocksize, writecount, i;
734 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
735 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
737 if (wcount < blocksize)
740 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
741 writecount = blocksize;
745 nbytes = MIN(writecount, 4);
749 if (mem_ap_write_buf_u8(swjdp, buffer, nbytes, address) != ERROR_OK)
751 LOG_WARNING("Block write error address "
752 "0x%" PRIx32 ", count 0x%x",
754 return ERROR_JTAG_DEVICE_ERROR;
762 memcpy(&outvalue, buffer, sizeof(uint32_t));
764 for (i = 0; i < nbytes; i++)
766 *((uint8_t*)buffer + (address & 0x3)) = outvalue;
771 memcpy(&outvalue, buffer, sizeof(uint32_t));
772 retval = dap_queue_ap_write(swjdp,
773 AP_REG_DRW, outvalue);
774 if (retval != ERROR_OK)
777 if (dap_run(swjdp) != ERROR_OK)
779 LOG_WARNING("Block write error address "
780 "0x%" PRIx32 ", count 0x%x",
782 /* REVISIT return *actual* fault code */
783 return ERROR_JTAG_DEVICE_ERROR;
788 writecount -= nbytes;
790 } while (writecount);
797 int mem_ap_write_buf_u8(struct adiv5_dap *swjdp, uint8_t *buffer, int count, uint32_t address)
799 int retval = ERROR_OK;
802 return mem_ap_write_buf_packed_u8(swjdp, buffer, count, address);
806 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
807 uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
808 retval = dap_queue_ap_write(swjdp, AP_REG_DRW, outvalue);
809 if (retval != ERROR_OK)
812 retval = dap_run(swjdp);
813 if (retval != ERROR_OK)
824 /* FIXME don't import ... this is a temporary workaround for the
825 * mem_ap_read_buf_u32() mess, until it's no longer JTAG-specific.
827 extern int adi_jtag_dp_scan(struct adiv5_dap *swjdp,
828 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
829 uint8_t *outvalue, uint8_t *invalue, uint8_t *ack);
832 * Synchronously read a block of 32-bit words into a buffer
833 * @param swjdp The DAP connected to the MEM-AP.
834 * @param buffer where the words will be stored (in host byte order).
835 * @param count How many words to read.
836 * @param address Memory address from which to read words; all the
837 * words must be readable by the currently selected MEM-AP.
839 int mem_ap_read_buf_u32(struct adiv5_dap *swjdp, uint8_t *buffer,
840 int count, uint32_t address)
842 int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
843 uint32_t adr = address;
844 uint8_t* pBuffer = buffer;
851 /* Adjust to read blocks within boundaries aligned to the
852 * TAR autoincrement size (at least 2^10). Autoincrement
853 * mode avoids an extra per-word roundtrip to update TAR.
855 blocksize = max_tar_block_size(swjdp->tar_autoincr_block,
857 if (wcount < blocksize)
860 /* handle unaligned data at 4k boundary */
864 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE,
867 /* FIXME remove these three calls to adi_jtag_dp_scan(),
868 * so this routine becomes transport-neutral. Be careful
869 * not to cause performance problems with JTAG; would it
870 * suffice to loop over dap_queue_ap_read(), or would that
871 * be slower when JTAG is the chosen transport?
874 /* Scan out first read */
875 adi_jtag_dp_scan(swjdp, JTAG_DP_APACC, AP_REG_DRW,
876 DPAP_READ, 0, NULL, NULL);
877 for (readcount = 0; readcount < blocksize - 1; readcount++)
879 /* Scan out next read; scan in posted value for the
880 * previous one. Assumes read is acked "OK/FAULT",
881 * and CTRL_STAT says that meant "OK".
883 adi_jtag_dp_scan(swjdp, JTAG_DP_APACC, AP_REG_DRW,
884 DPAP_READ, 0, buffer + 4 * readcount,
888 /* Scan in last posted value; RDBUFF has no other effect,
889 * assuming ack is OK/FAULT and CTRL_STAT says "OK".
891 adi_jtag_dp_scan(swjdp, JTAG_DP_DPACC, DP_RDBUFF,
892 DPAP_READ, 0, buffer + 4 * readcount,
894 if (dap_run(swjdp) == ERROR_OK)
896 wcount = wcount - blocksize;
897 address += 4 * blocksize;
898 buffer += 4 * blocksize;
907 LOG_WARNING("Block read error address 0x%" PRIx32
908 ", count 0x%x", address, count);
909 /* REVISIT return the *actual* fault code */
910 return ERROR_JTAG_DEVICE_ERROR;
914 /* if we have an unaligned access - reorder data */
917 for (readcount = 0; readcount < count; readcount++)
921 memcpy(&data, pBuffer, sizeof(uint32_t));
923 for (i = 0; i < 4; i++)
925 *((uint8_t*)pBuffer) =
926 (data >> 8 * (adr & 0x3));
936 static int mem_ap_read_buf_packed_u16(struct adiv5_dap *swjdp,
937 uint8_t *buffer, int count, uint32_t address)
940 int retval = ERROR_OK;
941 int wcount, blocksize, readcount, i;
949 /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
950 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
951 if (wcount < blocksize)
954 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
956 /* handle unaligned data at 4k boundary */
959 readcount = blocksize;
963 retval = dap_queue_ap_read(swjdp, AP_REG_DRW, &invalue);
964 if (dap_run(swjdp) != ERROR_OK)
966 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
967 /* REVISIT return the *actual* fault code */
968 return ERROR_JTAG_DEVICE_ERROR;
971 nbytes = MIN((readcount << 1), 4);
973 for (i = 0; i < nbytes; i++)
975 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
980 readcount -= (nbytes >> 1);
989 * Synchronously read a block of 16-bit halfwords into a buffer
990 * @param swjdp The DAP connected to the MEM-AP.
991 * @param buffer where the halfwords will be stored (in host byte order).
992 * @param count How many halfwords to read.
993 * @param address Memory address from which to read words; all the
994 * words must be readable by the currently selected MEM-AP.
996 int mem_ap_read_buf_u16(struct adiv5_dap *swjdp, uint8_t *buffer,
997 int count, uint32_t address)
1000 int retval = ERROR_OK;
1003 return mem_ap_read_buf_packed_u16(swjdp, buffer, count, address);
1007 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
1008 retval = dap_queue_ap_read(swjdp, AP_REG_DRW, &invalue);
1009 if (retval != ERROR_OK)
1012 retval = dap_run(swjdp);
1013 if (retval != ERROR_OK)
1018 for (i = 0; i < 2; i++)
1020 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
1027 uint16_t svalue = (invalue >> 8 * (address & 0x3));
1028 memcpy(buffer, &svalue, sizeof(uint16_t));
1038 /* FIX!!! is this a potential performance bottleneck w.r.t. requiring too many
1039 * roundtrips when jtag_execute_queue() has a large overhead(e.g. for USB)s?
1041 * The solution is to arrange for a large out/in scan in this loop and
1042 * and convert data afterwards.
1044 static int mem_ap_read_buf_packed_u8(struct adiv5_dap *swjdp,
1045 uint8_t *buffer, int count, uint32_t address)
1048 int retval = ERROR_OK;
1049 int wcount, blocksize, readcount, i;
1057 /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
1058 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
1060 if (wcount < blocksize)
1063 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
1064 readcount = blocksize;
1068 retval = dap_queue_ap_read(swjdp, AP_REG_DRW, &invalue);
1069 if (dap_run(swjdp) != ERROR_OK)
1071 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
1072 /* REVISIT return the *actual* fault code */
1073 return ERROR_JTAG_DEVICE_ERROR;
1076 nbytes = MIN(readcount, 4);
1078 for (i = 0; i < nbytes; i++)
1080 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
1085 readcount -= nbytes;
1086 } while (readcount);
1087 wcount -= blocksize;
1094 * Synchronously read a block of bytes into a buffer
1095 * @param swjdp The DAP connected to the MEM-AP.
1096 * @param buffer where the bytes will be stored.
1097 * @param count How many bytes to read.
1098 * @param address Memory address from which to read data; all the
1099 * data must be readable by the currently selected MEM-AP.
1101 int mem_ap_read_buf_u8(struct adiv5_dap *swjdp, uint8_t *buffer,
1102 int count, uint32_t address)
1105 int retval = ERROR_OK;
1108 return mem_ap_read_buf_packed_u8(swjdp, buffer, count, address);
1112 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
1113 retval = dap_queue_ap_read(swjdp, AP_REG_DRW, &invalue);
1114 retval = dap_run(swjdp);
1115 if (retval != ERROR_OK)
1118 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
1127 /*--------------------------------------------------------------------------*/
1129 <<<<<<< HEAD:src/target/arm_adi_v5.c
1131 static int jtag_idcode_q_read(struct adiv5_dap *dap,
1132 uint8_t *ack, uint32_t *data)
1134 struct arm_jtag *jtag_info = dap->jtag_info;
1136 struct scan_field fields[1];
1138 jtag_set_end_state(TAP_IDLE);
1140 /* This is a standard JTAG operation -- no DAP tweakage */
1141 retval = arm_jtag_set_instr(jtag_info, JTAG_DP_IDCODE, NULL);
1142 if (retval != ERROR_OK)
1145 fields[0].num_bits = 32;
1146 fields[0].out_value = NULL;
1147 fields[0].in_value = (void *) data;
1149 jtag_add_dr_scan(jtag_info->tap, 1, fields, TAP_IDLE);
1150 retval = jtag_get_error();
1151 if (retval != ERROR_OK)
1154 jtag_add_callback(arm_le_to_h_u32,
1155 (jtag_callback_data_t) data);
1160 static int jtag_dp_q_read(struct adiv5_dap *dap, unsigned reg,
1163 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
1164 reg, DPAP_READ, 0, data);
1167 static int jtag_dp_q_write(struct adiv5_dap *dap, unsigned reg,
1170 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
1171 reg, DPAP_WRITE, data, NULL);
1174 /** Select the AP register bank matching bits 7:4 of reg. */
1175 static int jtag_ap_q_bankselect(struct adiv5_dap *dap, unsigned reg)
1177 uint32_t select = reg & 0x000000F0;
1179 if (select == dap->ap_bank_value)
1181 dap->ap_bank_value = select;
1183 select |= dap->apsel;
1185 return jtag_dp_q_write(dap, DP_SELECT, select);
1188 static int jtag_ap_q_read(struct adiv5_dap *dap, unsigned reg,
1191 int retval = jtag_ap_q_bankselect(dap, reg);
1193 if (retval != ERROR_OK)
1196 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_APACC, reg,
1197 DPAP_READ, 0, data);
1200 static int jtag_ap_q_write(struct adiv5_dap *dap, unsigned reg,
1203 uint8_t out_value_buf[4];
1205 int retval = jtag_ap_q_bankselect(dap, reg);
1206 if (retval != ERROR_OK)
1208 >>>>>>> jtag: cut down on usage of unintended modification of global end state:src/target/arm_adi_v5.c
1210 /* FIXME don't import ... just initialize as
1211 * part of DAP transport setup
1213 extern const struct dap_ops jtag_dp_ops;
1215 /*--------------------------------------------------------------------------*/
1218 * Initialize a DAP. This sets up the power domains, prepares the DP
1219 * for further use, and arranges to use AP #0 for all AP operations
1220 * until dap_ap-select() changes that policy.
1222 * @param swjdp The DAP being initialized.
1224 * @todo Rename this. We also need an initialization scheme which account
1225 * for SWD transports not just JTAG; that will need to address differences
1226 * in layering. (JTAG is useful without any debug target; but not SWD.)
1227 * And this may not even use an AHB-AP ... e.g. DAP-Lite uses an APB-AP.
1229 int ahbap_debugport_init(struct adiv5_dap *swjdp)
1231 uint32_t idreg, romaddr, dummy;
1238 /* JTAG-DP or SWJ-DP, in JTAG mode */
1239 swjdp->ops = &jtag_dp_ops;
1241 /* Default MEM-AP setup.
1243 * REVISIT AP #0 may be an inappropriate default for this.
1244 * Should we probe, or take a hint from the caller?
1245 * Presumably we can ignore the possibility of multiple APs.
1248 dap_ap_select(swjdp, 0);
1250 /* DP initialization */
1252 retval = dap_queue_dp_read(swjdp, DP_CTRL_STAT, &dummy);
1253 if (retval != ERROR_OK)
1256 retval = dap_queue_dp_write(swjdp, DP_CTRL_STAT, SSTICKYERR);
1257 if (retval != ERROR_OK)
1260 retval = dap_queue_dp_read(swjdp, DP_CTRL_STAT, &dummy);
1261 if (retval != ERROR_OK)
1264 swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
1265 retval = dap_queue_dp_write(swjdp, DP_CTRL_STAT, swjdp->dp_ctrl_stat);
1266 if (retval != ERROR_OK)
1269 retval = dap_queue_dp_read(swjdp, DP_CTRL_STAT, &ctrlstat);
1270 if (retval != ERROR_OK)
1272 if ((retval = dap_run(swjdp)) != ERROR_OK)
1275 /* Check that we have debug power domains activated */
1276 while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10))
1278 LOG_DEBUG("DAP: wait CDBGPWRUPACK");
1279 retval = dap_queue_dp_read(swjdp, DP_CTRL_STAT, &ctrlstat);
1280 if (retval != ERROR_OK)
1282 if ((retval = dap_run(swjdp)) != ERROR_OK)
1287 while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10))
1289 LOG_DEBUG("DAP: wait CSYSPWRUPACK");
1290 retval = dap_queue_dp_read(swjdp, DP_CTRL_STAT, &ctrlstat);
1291 if (retval != ERROR_OK)
1293 if ((retval = dap_run(swjdp)) != ERROR_OK)
1298 retval = dap_queue_dp_read(swjdp, DP_CTRL_STAT, &dummy);
1299 if (retval != ERROR_OK)
1301 /* With debug power on we can activate OVERRUN checking */
1302 swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
1303 retval = dap_queue_dp_write(swjdp, DP_CTRL_STAT, swjdp->dp_ctrl_stat);
1304 if (retval != ERROR_OK)
1306 retval = dap_queue_dp_read(swjdp, DP_CTRL_STAT, &dummy);
1307 if (retval != ERROR_OK)
1311 * REVISIT this isn't actually *initializing* anything in an AP,
1312 * and doesn't care if it's a MEM-AP at all (much less AHB-AP).
1313 * Should it? If the ROM address is valid, is this the right
1314 * place to scan the table and do any topology detection?
1316 retval = dap_queue_ap_read(swjdp, AP_REG_IDR, &idreg);
1317 retval = dap_queue_ap_read(swjdp, AP_REG_BASE, &romaddr);
1319 LOG_DEBUG("MEM-AP #%d ID Register 0x%" PRIx32
1320 ", Debug ROM Address 0x%" PRIx32,
1321 swjdp->apsel, idreg, romaddr);
1326 /* CID interpretation -- see ARM IHI 0029B section 3
1327 * and ARM IHI 0031A table 13-3.
1329 static const char *class_description[16] ={
1330 "Reserved", "ROM table", "Reserved", "Reserved",
1331 "Reserved", "Reserved", "Reserved", "Reserved",
1332 "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
1333 "Reserved", "OptimoDE DESS",
1334 "Generic IP component", "PrimeCell or System component"
1338 is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
1340 return cid3 == 0xb1 && cid2 == 0x05
1341 && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
1344 static int dap_info_command(struct command_context *cmd_ctx,
1345 struct adiv5_dap *swjdp, int apsel)
1348 uint32_t dbgbase, apid;
1349 int romtable_present = 0;
1353 /* AP address is in bits 31:24 of DP_SELECT */
1355 return ERROR_INVALID_ARGUMENTS;
1357 apselold = swjdp->apsel;
1358 dap_ap_select(swjdp, apsel);
1359 retval = dap_queue_ap_read(swjdp, AP_REG_BASE, &dbgbase);
1360 retval = dap_queue_ap_read(swjdp, AP_REG_IDR, &apid);
1361 retval = dap_run(swjdp);
1362 if (retval != ERROR_OK)
1365 /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
1366 mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
1367 command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
1373 command_print(cmd_ctx, "\tType is JTAG-AP");
1376 command_print(cmd_ctx, "\tType is MEM-AP AHB");
1379 command_print(cmd_ctx, "\tType is MEM-AP APB");
1382 command_print(cmd_ctx, "\tUnknown AP type");
1386 /* NOTE: a MEM-AP may have a single CoreSight component that's
1387 * not a ROM table ... or have no such components at all.
1390 command_print(cmd_ctx, "AP BASE 0x%8.8" PRIx32,
1395 command_print(cmd_ctx, "No AP found at this apsel 0x%x", apsel);
1398 romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
1399 if (romtable_present)
1401 uint32_t cid0,cid1,cid2,cid3,memtype,romentry;
1402 uint16_t entry_offset;
1404 /* bit 16 of apid indicates a memory access port */
1406 command_print(cmd_ctx, "\tValid ROM table present");
1408 command_print(cmd_ctx, "\tROM table in legacy format");
1410 /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
1411 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
1412 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
1413 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
1414 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
1415 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
1416 retval = dap_run(swjdp);
1417 if (retval != ERROR_OK)
1420 if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1421 command_print(cmd_ctx, "\tCID3 0x%2.2" PRIx32
1422 ", CID2 0x%2.2" PRIx32
1423 ", CID1 0x%2.2" PRIx32
1424 ", CID0 0x%2.2" PRIx32,
1425 cid3, cid2, cid1, cid0);
1427 command_print(cmd_ctx, "\tMEMTYPE system memory present on bus");
1429 command_print(cmd_ctx, "\tMEMTYPE System memory not present. "
1430 "Dedicated debug bus.");
1432 /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
1436 mem_ap_read_atomic_u32(swjdp, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
1437 command_print(cmd_ctx, "\tROMTABLE[0x%x] = 0x%" PRIx32 "",entry_offset,romentry);
1440 uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
1441 uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
1442 uint32_t component_start, component_base;
1446 component_base = (uint32_t)((dbgbase & 0xFFFFF000)
1447 + (int)(romentry & 0xFFFFF000));
1448 mem_ap_read_atomic_u32(swjdp,
1449 (component_base & 0xFFFFF000) | 0xFE0, &c_pid0);
1450 mem_ap_read_atomic_u32(swjdp,
1451 (component_base & 0xFFFFF000) | 0xFE4, &c_pid1);
1452 mem_ap_read_atomic_u32(swjdp,
1453 (component_base & 0xFFFFF000) | 0xFE8, &c_pid2);
1454 mem_ap_read_atomic_u32(swjdp,
1455 (component_base & 0xFFFFF000) | 0xFEC, &c_pid3);
1456 mem_ap_read_atomic_u32(swjdp,
1457 (component_base & 0xFFFFF000) | 0xFD0, &c_pid4);
1458 mem_ap_read_atomic_u32(swjdp,
1459 (component_base & 0xFFFFF000) | 0xFF0, &c_cid0);
1460 mem_ap_read_atomic_u32(swjdp,
1461 (component_base & 0xFFFFF000) | 0xFF4, &c_cid1);
1462 mem_ap_read_atomic_u32(swjdp,
1463 (component_base & 0xFFFFF000) | 0xFF8, &c_cid2);
1464 mem_ap_read_atomic_u32(swjdp,
1465 (component_base & 0xFFFFF000) | 0xFFC, &c_cid3);
1466 component_start = component_base - 0x1000*(c_pid4 >> 4);
1468 command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32
1469 ", start address 0x%" PRIx32,
1470 component_base, component_start);
1471 command_print(cmd_ctx, "\t\tComponent class is 0x%x, %s",
1472 (int) (c_cid1 >> 4) & 0xf,
1473 /* See ARM IHI 0029B Table 3-3 */
1474 class_description[(c_cid1 >> 4) & 0xf]);
1476 /* CoreSight component? */
1477 if (((c_cid1 >> 4) & 0x0f) == 9) {
1480 char *major = "Reserved", *subtype = "Reserved";
1482 mem_ap_read_atomic_u32(swjdp,
1483 (component_base & 0xfffff000) | 0xfcc,
1485 minor = (devtype >> 4) & 0x0f;
1486 switch (devtype & 0x0f) {
1488 major = "Miscellaneous";
1494 subtype = "Validation component";
1499 major = "Trace Sink";
1513 major = "Trace Link";
1519 subtype = "Funnel, router";
1525 subtype = "FIFO, buffer";
1530 major = "Trace Source";
1536 subtype = "Processor";
1542 subtype = "Engine/Coprocessor";
1550 major = "Debug Control";
1556 subtype = "Trigger Matrix";
1559 subtype = "Debug Auth";
1564 major = "Debug Logic";
1570 subtype = "Processor";
1576 subtype = "Engine/Coprocessor";
1581 command_print(cmd_ctx, "\t\tType is 0x%2.2x, %s, %s",
1582 (unsigned) (devtype & 0xff),
1584 /* REVISIT also show 0xfc8 DevId */
1587 if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1588 command_print(cmd_ctx, "\t\tCID3 0x%2.2" PRIx32
1589 ", CID2 0x%2.2" PRIx32
1590 ", CID1 0x%2.2" PRIx32
1591 ", CID0 0x%2.2" PRIx32,
1592 c_cid3, c_cid2, c_cid1, c_cid0);
1593 command_print(cmd_ctx, "\t\tPeripheral ID[4..0] = hex "
1594 "%2.2x %2.2x %2.2x %2.2x %2.2x",
1596 (int) c_pid3, (int) c_pid2,
1597 (int) c_pid1, (int) c_pid0);
1599 /* Part number interpretations are from Cortex
1600 * core specs, the CoreSight components TRM
1601 * (ARM DDI 0314H), and ETM specs; also from
1602 * chip observation (e.g. TI SDTI).
1604 part_num = c_pid0 & 0xff;
1605 part_num |= (c_pid1 & 0x0f) << 8;
1608 type = "Cortex-M3 NVIC";
1609 full = "(Interrupt Controller)";
1612 type = "Cortex-M3 ITM";
1613 full = "(Instrumentation Trace Module)";
1616 type = "Cortex-M3 DWT";
1617 full = "(Data Watchpoint and Trace)";
1620 type = "Cortex-M3 FBP";
1621 full = "(Flash Patch and Breakpoint)";
1624 type = "CoreSight ETM11";
1625 full = "(Embedded Trace)";
1627 // case 0x113: what?
1628 case 0x120: /* from OMAP3 memmap */
1630 full = "(System Debug Trace Interface)";
1632 case 0x343: /* from OMAP3 memmap */
1637 type = "Coresight CTI";
1638 full = "(Cross Trigger)";
1641 type = "Coresight ETB";
1642 full = "(Trace Buffer)";
1645 type = "Coresight CSTF";
1646 full = "(Trace Funnel)";
1649 type = "CoreSight ETM9";
1650 full = "(Embedded Trace)";
1653 type = "Coresight TPIU";
1654 full = "(Trace Port Interface Unit)";
1657 type = "Cortex-A8 ETM";
1658 full = "(Embedded Trace)";
1661 type = "Cortex-A8 CTI";
1662 full = "(Cross Trigger)";
1665 type = "Cortex-M3 TPIU";
1666 full = "(Trace Port Interface Unit)";
1669 type = "Cortex-M3 ETM";
1670 full = "(Embedded Trace)";
1673 type = "Cortex-A8 Debug";
1674 full = "(Debug Unit)";
1677 type = "-*- unrecognized -*-";
1681 command_print(cmd_ctx, "\t\tPart is %s %s",
1687 command_print(cmd_ctx, "\t\tComponent not present");
1689 command_print(cmd_ctx, "\t\tEnd of ROM table");
1692 } while (romentry > 0);
1696 command_print(cmd_ctx, "\tNo ROM table present");
1698 dap_ap_select(swjdp, apselold);
1703 COMMAND_HANDLER(handle_dap_info_command)
1705 struct target *target = get_current_target(CMD_CTX);
1706 struct arm *arm = target_to_arm(target);
1707 struct adiv5_dap *dap = arm->dap;
1715 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1718 return ERROR_COMMAND_SYNTAX_ERROR;
1721 return dap_info_command(CMD_CTX, dap, apsel);
1724 COMMAND_HANDLER(dap_baseaddr_command)
1726 struct target *target = get_current_target(CMD_CTX);
1727 struct arm *arm = target_to_arm(target);
1728 struct adiv5_dap *dap = arm->dap;
1730 uint32_t apsel, apselsave, baseaddr;
1733 apselsave = dap->apsel;
1739 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1740 /* AP address is in bits 31:24 of DP_SELECT */
1742 return ERROR_INVALID_ARGUMENTS;
1745 return ERROR_COMMAND_SYNTAX_ERROR;
1748 if (apselsave != apsel)
1749 dap_ap_select(dap, apsel);
1751 /* NOTE: assumes we're talking to a MEM-AP, which
1752 * has a base address. There are other kinds of AP,
1753 * though they're not common for now. This should
1754 * use the ID register to verify it's a MEM-AP.
1756 retval = dap_queue_ap_read(dap, AP_REG_BASE, &baseaddr);
1757 retval = dap_run(dap);
1758 if (retval != ERROR_OK)
1761 command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
1763 if (apselsave != apsel)
1764 dap_ap_select(dap, apselsave);
1769 COMMAND_HANDLER(dap_memaccess_command)
1771 struct target *target = get_current_target(CMD_CTX);
1772 struct arm *arm = target_to_arm(target);
1773 struct adiv5_dap *dap = arm->dap;
1775 uint32_t memaccess_tck;
1779 memaccess_tck = dap->memaccess_tck;
1782 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
1785 return ERROR_COMMAND_SYNTAX_ERROR;
1787 dap->memaccess_tck = memaccess_tck;
1789 command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
1790 dap->memaccess_tck);
1795 COMMAND_HANDLER(dap_apsel_command)
1797 struct target *target = get_current_target(CMD_CTX);
1798 struct arm *arm = target_to_arm(target);
1799 struct adiv5_dap *dap = arm->dap;
1801 uint32_t apsel, apid;
1809 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1810 /* AP address is in bits 31:24 of DP_SELECT */
1812 return ERROR_INVALID_ARGUMENTS;
1815 return ERROR_COMMAND_SYNTAX_ERROR;
1818 dap_ap_select(dap, apsel);
1819 retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
1820 retval = dap_run(dap);
1821 if (retval != ERROR_OK)
1824 command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
1830 COMMAND_HANDLER(dap_apid_command)
1832 struct target *target = get_current_target(CMD_CTX);
1833 struct arm *arm = target_to_arm(target);
1834 struct adiv5_dap *dap = arm->dap;
1836 uint32_t apsel, apselsave, apid;
1839 apselsave = dap->apsel;
1845 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1846 /* AP address is in bits 31:24 of DP_SELECT */
1848 return ERROR_INVALID_ARGUMENTS;
1851 return ERROR_COMMAND_SYNTAX_ERROR;
1854 if (apselsave != apsel)
1855 dap_ap_select(dap, apsel);
1857 retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
1858 retval = dap_run(dap);
1859 if (retval != ERROR_OK)
1862 command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
1863 if (apselsave != apsel)
1864 dap_ap_select(dap, apselsave);
1869 static const struct command_registration dap_commands[] = {
1872 .handler = handle_dap_info_command,
1873 .mode = COMMAND_EXEC,
1874 .help = "display ROM table for MEM-AP "
1875 "(default currently selected AP)",
1876 .usage = "[ap_num]",
1880 .handler = dap_apsel_command,
1881 .mode = COMMAND_EXEC,
1882 .help = "Set the currently selected AP (default 0) "
1883 "and display the result",
1884 .usage = "[ap_num]",
1888 .handler = dap_apid_command,
1889 .mode = COMMAND_EXEC,
1890 .help = "return ID register from AP "
1891 "(default currently selected AP)",
1892 .usage = "[ap_num]",
1896 .handler = dap_baseaddr_command,
1897 .mode = COMMAND_EXEC,
1898 .help = "return debug base address from MEM-AP "
1899 "(default currently selected AP)",
1900 .usage = "[ap_num]",
1903 .name = "memaccess",
1904 .handler = dap_memaccess_command,
1905 .mode = COMMAND_EXEC,
1906 .help = "set/get number of extra tck for MEM-AP memory "
1907 "bus access [0-255]",
1908 .usage = "[cycles]",
1910 COMMAND_REGISTRATION_DONE
1913 const struct command_registration dap_command_handlers[] = {
1916 .mode = COMMAND_EXEC,
1917 .help = "DAP command group",
1918 .chain = dap_commands,
1920 COMMAND_REGISTRATION_DONE
1925 * This represents the bits which must be sent out on TMS/SWDIO to
1926 * switch a DAP implemented using an SWJ-DP module into SWD mode.
1927 * These bits are stored (and transmitted) LSB-first.
1929 * See the DAP-Lite specification, section 2.2.5 for information
1930 * about making the debug link select SWD or JTAG. (Similar info
1931 * is in a few other ARM documents.)
1933 static const uint8_t jtag2swd_bitseq[] = {
1934 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
1935 * putting both JTAG and SWD logic into reset state.
1937 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1938 /* Switching sequence enables SWD and disables JTAG
1939 * NOTE: bits in the DP's IDCODE may expose the need for
1940 * an old/deprecated sequence (0xb6 0xed).
1943 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
1944 * putting both JTAG and SWD logic into reset state.
1946 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1950 * Put the debug link into SWD mode, if the target supports it.
1951 * The link's initial mode may be either JTAG (for example,
1952 * with SWJ-DP after reset) or SWD.
1954 * @param target Enters SWD mode (if possible).
1956 * Note that targets using the JTAG-DP do not support SWD, and that
1957 * some targets which could otherwise support it may have have been
1958 * configured to disable SWD signaling
1960 * @return ERROR_OK or else a fault code.
1962 int dap_to_swd(struct target *target)
1966 LOG_DEBUG("Enter SWD mode");
1968 /* REVISIT it's nasty to need to make calls to a "jtag"
1969 * subsystem if the link isn't in JTAG mode...
1972 retval = jtag_add_tms_seq(8 * sizeof(jtag2swd_bitseq),
1973 jtag2swd_bitseq, TAP_INVALID);
1974 if (retval == ERROR_OK)
1975 retval = jtag_execute_queue();
1977 /* REVISIT set up the DAP's ops vector for SWD mode. */