ADIv5 improved diagnostic
[openocd.git] / src / target / arm_adi_v5.c
blobba5db3b13c23bd1faca14cfd2c350524c51a6737
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 by Oyvind Harboe *
9 * oyvind.harboe@zylin.com *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
27 /**
28 * @file
29 * This file implements support for the ARM Debug Interface version 5 (ADIv5)
30 * debugging architecture. Compared with previous versions, this includes
31 * a low pin-count Serial Wire Debug (SWD) alternative to JTAG for message
32 * transport, and focusses on memory mapped resources as defined by the
33 * CoreSight architecture.
35 * A key concept in ADIv5 is the Debug Access Port, or DAP. A DAP has two
36 * basic components: a Debug Port (DP) transporting messages to and from a
37 * debugger, and an Access Port (AP) accessing resources. Three types of DP
38 * are defined. One uses only JTAG for communication, and is called JTAG-DP.
39 * One uses only SWD for communication, and is called SW-DP. The third can
40 * use either SWD or JTAG, and is called SWJ-DP. The most common type of AP
41 * is used to access memory mapped resources and is called a MEM-AP. Also a
42 * JTAG-AP is also defined, bridging to JTAG resources; those are uncommon.
46 * Relevant specifications from ARM include:
48 * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031A
49 * CoreSight(tm) v1.0 Architecture Specification ARM IHI 0029B
51 * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316D
52 * Cortex-M3(tm) TRM, ARM DDI 0337G
55 #ifdef HAVE_CONFIG_H
56 #include "config.h"
57 #endif
59 #include "arm_adi_v5.h"
60 #include <helper/time_support.h>
63 * Transaction Mode:
64 * swjdp->trans_mode = TRANS_MODE_COMPOSITE;
65 * Uses Overrun checking mode and does not do actual JTAG send/receive or transaction
66 * result checking until swjdp_end_transaction()
67 * This must be done before using or deallocating any return variables.
68 * swjdp->trans_mode == TRANS_MODE_ATOMIC
69 * All reads and writes to the AHB bus are checked for valid completion, and return values
70 * are immediatley available.
74 /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
77 uint32_t tar_block_size(uint32_t address)
78 Return the largest block starting at address that does not cross a tar block size alignment boundary
80 static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
82 return (tar_autoincr_block - ((tar_autoincr_block - 1) & address)) >> 2;
85 /***************************************************************************
86 * *
87 * DPACC and APACC scanchain access through JTAG-DP *
88 * *
89 ***************************************************************************/
91 /**
92 * Scan DPACC or APACC using target ordered uint8_t buffers. No endianness
93 * conversions are performed. See section 4.4.3 of the ADIv5 spec, which
94 * discusses operations which access these registers.
96 * Note that only one scan is performed. If RnW is set, a separate scan
97 * will be needed to collect the data which was read; the "invalue" collects
98 * the posted result of a preceding operation, not the current one.
100 * @param swjdp the DAP
101 * @param instr JTAG_DP_APACC (AP access) or JTAG_DP_DPACC (DP access)
102 * @param reg_addr two significant bits; A[3:2]; for APACC access, the
103 * SELECT register has more addressing bits.
104 * @param RnW false iff outvalue will be written to the DP or AP
105 * @param outvalue points to a 32-bit (little-endian) integer
106 * @param invalue NULL, or points to a 32-bit (little-endian) integer
107 * @param ack points to where the three bit JTAG_ACK_* code will be stored
109 static int adi_jtag_dp_scan(struct swjdp_common *swjdp,
110 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
111 uint8_t *outvalue, uint8_t *invalue, uint8_t *ack)
113 struct arm_jtag *jtag_info = swjdp->jtag_info;
114 struct scan_field fields[2];
115 uint8_t out_addr_buf;
117 jtag_set_end_state(TAP_IDLE);
118 arm_jtag_set_instr(jtag_info, instr, NULL);
120 /* Add specified number of tck clocks before accessing memory bus */
122 /* REVISIT these TCK cycles should be *AFTER* updating APACC, since
123 * they provide more time for the (MEM) AP to complete the read ...
124 * See "Minimum Response Time" for JTAG-DP, in the ADIv5 spec.
126 if ((instr == JTAG_DP_APACC)
127 && ((reg_addr == AP_REG_DRW)
128 || ((reg_addr & 0xF0) == AP_REG_BD0))
129 && (swjdp->memaccess_tck != 0))
130 jtag_add_runtest(swjdp->memaccess_tck, jtag_set_end_state(TAP_IDLE));
132 /* Scan out a read or write operation using some DP or AP register.
133 * For APACC access with any sticky error flag set, this is discarded.
135 fields[0].tap = jtag_info->tap;
136 fields[0].num_bits = 3;
137 buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
138 fields[0].out_value = &out_addr_buf;
139 fields[0].in_value = ack;
141 /* NOTE: if we receive JTAG_ACK_WAIT, the previous operation did not
142 * complete; data we write is discarded, data we read is unpredictable.
143 * When overrun detect is active, STICKYORUN is set.
146 fields[1].tap = jtag_info->tap;
147 fields[1].num_bits = 32;
148 fields[1].out_value = outvalue;
149 fields[1].in_value = invalue;
151 jtag_add_dr_scan(2, fields, jtag_get_end_state());
153 return ERROR_OK;
156 /* Scan out and in from host ordered uint32_t variables */
157 static int adi_jtag_dp_scan_u32(struct swjdp_common *swjdp,
158 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
159 uint32_t outvalue, uint32_t *invalue, uint8_t *ack)
161 struct arm_jtag *jtag_info = swjdp->jtag_info;
162 struct scan_field fields[2];
163 uint8_t out_value_buf[4];
164 uint8_t out_addr_buf;
166 jtag_set_end_state(TAP_IDLE);
167 arm_jtag_set_instr(jtag_info, instr, NULL);
169 /* Add specified number of tck clocks before accessing memory bus */
171 /* REVISIT these TCK cycles should be *AFTER* updating APACC, since
172 * they provide more time for the (MEM) AP to complete the read ...
174 if ((instr == JTAG_DP_APACC)
175 && ((reg_addr == AP_REG_DRW)
176 || ((reg_addr & 0xF0) == AP_REG_BD0))
177 && (swjdp->memaccess_tck != 0))
178 jtag_add_runtest(swjdp->memaccess_tck, jtag_set_end_state(TAP_IDLE));
180 fields[0].tap = jtag_info->tap;
181 fields[0].num_bits = 3;
182 buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
183 fields[0].out_value = &out_addr_buf;
184 fields[0].in_value = ack;
186 fields[1].tap = jtag_info->tap;
187 fields[1].num_bits = 32;
188 buf_set_u32(out_value_buf, 0, 32, outvalue);
189 fields[1].out_value = out_value_buf;
190 fields[1].in_value = NULL;
192 if (invalue)
194 fields[1].in_value = (uint8_t *)invalue;
195 jtag_add_dr_scan(2, fields, jtag_get_end_state());
197 jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t) invalue);
198 } else
201 jtag_add_dr_scan(2, fields, jtag_get_end_state());
204 return ERROR_OK;
207 /* scan_inout_check adds one extra inscan for DPAP_READ commands to read variables */
208 static int scan_inout_check(struct swjdp_common *swjdp,
209 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
210 uint8_t *outvalue, uint8_t *invalue)
212 adi_jtag_dp_scan(swjdp, instr, reg_addr, RnW, outvalue, NULL, NULL);
214 if ((RnW == DPAP_READ) && (invalue != NULL))
215 adi_jtag_dp_scan(swjdp, JTAG_DP_DPACC,
216 DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
218 /* In TRANS_MODE_ATOMIC all JTAG_DP_APACC transactions wait for
219 * ack = OK/FAULT and the check CTRL_STAT
221 if ((instr == JTAG_DP_APACC)
222 && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
223 return swjdp_transaction_endcheck(swjdp);
225 return ERROR_OK;
228 static int scan_inout_check_u32(struct swjdp_common *swjdp,
229 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
230 uint32_t outvalue, uint32_t *invalue)
232 /* Issue the read or write */
233 adi_jtag_dp_scan_u32(swjdp, instr, reg_addr, RnW, outvalue, NULL, NULL);
235 /* For reads, collect posted value; RDBUFF has no other effect.
236 * Assumes read gets acked with OK/FAULT, and CTRL_STAT says "OK".
238 if ((RnW == DPAP_READ) && (invalue != NULL))
239 adi_jtag_dp_scan_u32(swjdp, JTAG_DP_DPACC,
240 DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
242 /* In TRANS_MODE_ATOMIC all JTAG_DP_APACC transactions wait for
243 * ack = OK/FAULT and then check CTRL_STAT
245 if ((instr == JTAG_DP_APACC)
246 && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
247 return swjdp_transaction_endcheck(swjdp);
249 return ERROR_OK;
252 int swjdp_transaction_endcheck(struct swjdp_common *swjdp)
254 int retval;
255 uint32_t ctrlstat;
257 /* too expensive to call keep_alive() here */
259 #if 0
260 /* Danger!!!! BROKEN!!!! */
261 scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
262 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
263 /* Danger!!!! BROKEN!!!! Why will jtag_execute_queue() fail here????
264 R956 introduced the check on return value here and now Michael Schwingen reports
265 that this code no longer works....
267 https://lists.berlios.de/pipermail/openocd-development/2008-September/003107.html
269 if ((retval = jtag_execute_queue()) != ERROR_OK)
271 LOG_ERROR("BUG: Why does this fail the first time????");
273 /* Why??? second time it works??? */
274 #endif
276 /* Post CTRL/STAT read; discard any previous posted read value
277 * but collect its ACK status.
279 scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
280 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
281 if ((retval = jtag_execute_queue()) != ERROR_OK)
282 return retval;
284 swjdp->ack = swjdp->ack & 0x7;
286 /* common code path avoids calling timeval_ms() */
287 if (swjdp->ack != JTAG_ACK_OK_FAULT)
289 long long then = timeval_ms();
291 while (swjdp->ack != JTAG_ACK_OK_FAULT)
293 if (swjdp->ack == JTAG_ACK_WAIT)
295 if ((timeval_ms()-then) > 1000)
297 /* NOTE: this would be a good spot
298 * to use JTAG_DP_ABORT.
300 LOG_WARNING("Timeout (1000ms) waiting "
301 "for ACK=OK/FAULT "
302 "in JTAG-DP transaction");
303 return ERROR_JTAG_DEVICE_ERROR;
306 else
308 LOG_WARNING("Invalid ACK %#x"
309 "in JTAG-DP transaction",
310 swjdp->ack);
311 return ERROR_JTAG_DEVICE_ERROR;
314 scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
315 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
316 if ((retval = jtag_execute_queue()) != ERROR_OK)
317 return retval;
318 swjdp->ack = swjdp->ack & 0x7;
322 /* Check for STICKYERR and STICKYORUN */
323 if (ctrlstat & (SSTICKYORUN | SSTICKYERR))
325 LOG_DEBUG("swjdp: CTRL/STAT error 0x%" PRIx32 "", ctrlstat);
326 /* Check power to debug regions */
327 if ((ctrlstat & 0xf0000000) != 0xf0000000)
329 ahbap_debugport_init(swjdp);
331 else
333 uint32_t mem_ap_csw, mem_ap_tar;
335 /* Print information about last AHBAP access */
336 LOG_ERROR("AHBAP Cached values: dp_select 0x%" PRIx32 ", ap_csw 0x%" PRIx32 ", ap_tar 0x%" PRIx32 "", swjdp->dp_select_value, swjdp->ap_csw_value, swjdp->ap_tar_value);
337 if (ctrlstat & SSTICKYORUN)
338 LOG_ERROR("JTAG-DP OVERRUN - "
339 "check clock or reduce jtag speed");
341 if (ctrlstat & SSTICKYERR)
342 LOG_ERROR("JTAG-DP STICKY ERROR");
344 /* Clear Sticky Error Bits */
345 scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
346 DP_CTRL_STAT, DPAP_WRITE,
347 swjdp->dp_ctrl_stat | SSTICKYORUN
348 | SSTICKYERR, NULL);
349 scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
350 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
351 if ((retval = jtag_execute_queue()) != ERROR_OK)
352 return retval;
354 LOG_DEBUG("swjdp: status 0x%" PRIx32 "", ctrlstat);
356 dap_ap_read_reg_u32(swjdp, AP_REG_CSW, &mem_ap_csw);
357 dap_ap_read_reg_u32(swjdp, AP_REG_TAR, &mem_ap_tar);
358 if ((retval = jtag_execute_queue()) != ERROR_OK)
359 return retval;
360 LOG_ERROR("Read MEM_AP_CSW 0x%" PRIx32 ", MEM_AP_TAR 0x%" PRIx32 "", mem_ap_csw, mem_ap_tar);
363 if ((retval = jtag_execute_queue()) != ERROR_OK)
364 return retval;
365 return ERROR_JTAG_DEVICE_ERROR;
368 return ERROR_OK;
371 /***************************************************************************
373 * DP and MEM-AP register access through APACC and DPACC *
375 ***************************************************************************/
377 static int dap_dp_write_reg(struct swjdp_common *swjdp,
378 uint32_t value, uint8_t reg_addr)
380 return scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
381 reg_addr, DPAP_WRITE, value, NULL);
384 static int dap_dp_read_reg(struct swjdp_common *swjdp,
385 uint32_t *value, uint8_t reg_addr)
387 return scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
388 reg_addr, DPAP_READ, 0, value);
391 int dap_ap_select(struct swjdp_common *swjdp,uint8_t apsel)
393 uint32_t select;
394 select = (apsel << 24) & 0xFF000000;
396 if (select != swjdp->apsel)
398 swjdp->apsel = select;
399 /* Switching AP invalidates cached values */
400 swjdp->dp_select_value = -1;
401 swjdp->ap_csw_value = -1;
402 swjdp->ap_tar_value = -1;
405 return ERROR_OK;
408 static int dap_dp_bankselect(struct swjdp_common *swjdp, uint32_t ap_reg)
410 uint32_t select;
411 select = (ap_reg & 0x000000F0);
413 if (select != swjdp->dp_select_value)
415 dap_dp_write_reg(swjdp, select | swjdp->apsel, DP_SELECT);
416 swjdp->dp_select_value = select;
419 return ERROR_OK;
422 static int dap_ap_write_reg(struct swjdp_common *swjdp,
423 uint32_t reg_addr, uint8_t *out_value_buf)
425 dap_dp_bankselect(swjdp, reg_addr);
426 scan_inout_check(swjdp, JTAG_DP_APACC, reg_addr,
427 DPAP_WRITE, out_value_buf, NULL);
429 return ERROR_OK;
432 int dap_ap_write_reg_u32(struct swjdp_common *swjdp, uint32_t reg_addr, uint32_t value)
434 uint8_t out_value_buf[4];
436 buf_set_u32(out_value_buf, 0, 32, value);
437 dap_dp_bankselect(swjdp, reg_addr);
438 scan_inout_check(swjdp, JTAG_DP_APACC, reg_addr,
439 DPAP_WRITE, out_value_buf, NULL);
441 return ERROR_OK;
444 int dap_ap_read_reg_u32(struct swjdp_common *swjdp, uint32_t reg_addr, uint32_t *value)
446 dap_dp_bankselect(swjdp, reg_addr);
447 scan_inout_check_u32(swjdp, JTAG_DP_APACC, reg_addr,
448 DPAP_READ, 0, value);
450 return ERROR_OK;
453 /***************************************************************************
455 * AHB-AP access to memory and system registers on AHB bus *
457 ***************************************************************************/
459 int dap_setup_accessport(struct swjdp_common *swjdp, uint32_t csw, uint32_t tar)
461 csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
462 if (csw != swjdp->ap_csw_value)
464 /* LOG_DEBUG("swjdp : Set CSW %x",csw); */
465 dap_ap_write_reg_u32(swjdp, AP_REG_CSW, csw);
466 swjdp->ap_csw_value = csw;
468 if (tar != swjdp->ap_tar_value)
470 /* LOG_DEBUG("swjdp : Set TAR %x",tar); */
471 dap_ap_write_reg_u32(swjdp, AP_REG_TAR, tar);
472 swjdp->ap_tar_value = tar;
474 if (csw & CSW_ADDRINC_MASK)
476 /* Do not cache TAR value when autoincrementing */
477 swjdp->ap_tar_value = -1;
479 return ERROR_OK;
482 /*****************************************************************************
484 * mem_ap_read_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t *value) *
486 * Read a uint32_t value from memory or system register *
487 * Functionally equivalent to target_read_u32(target, address, uint32_t *value), *
488 * but with less overhead *
489 *****************************************************************************/
490 int mem_ap_read_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t *value)
492 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
494 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
495 dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (address & 0xC), value);
497 return ERROR_OK;
500 int mem_ap_read_atomic_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t *value)
502 mem_ap_read_u32(swjdp, address, value);
504 return swjdp_transaction_endcheck(swjdp);
507 /*****************************************************************************
509 * mem_ap_write_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t value) *
511 * Write a uint32_t value to memory or memory mapped register *
513 *****************************************************************************/
514 int mem_ap_write_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t value)
516 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
518 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
519 dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (address & 0xC), value);
521 return ERROR_OK;
524 int mem_ap_write_atomic_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t value)
526 mem_ap_write_u32(swjdp, address, value);
528 return swjdp_transaction_endcheck(swjdp);
531 /*****************************************************************************
533 * mem_ap_write_buf(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address) *
535 * Write a buffer in target order (little endian) *
537 *****************************************************************************/
538 int mem_ap_write_buf_u32(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
540 int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
541 uint32_t adr = address;
542 uint8_t* pBuffer = buffer;
544 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
546 count >>= 2;
547 wcount = count;
549 /* if we have an unaligned access - reorder data */
550 if (adr & 0x3u)
552 for (writecount = 0; writecount < count; writecount++)
554 int i;
555 uint32_t outvalue;
556 memcpy(&outvalue, pBuffer, sizeof(uint32_t));
558 for (i = 0; i < 4; i++)
560 *((uint8_t*)pBuffer + (adr & 0x3)) = outvalue;
561 outvalue >>= 8;
562 adr++;
564 pBuffer += sizeof(uint32_t);
568 while (wcount > 0)
570 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
571 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
572 if (wcount < blocksize)
573 blocksize = wcount;
575 /* handle unaligned data at 4k boundary */
576 if (blocksize == 0)
577 blocksize = 1;
579 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
581 for (writecount = 0; writecount < blocksize; writecount++)
583 dap_ap_write_reg(swjdp, AP_REG_DRW, buffer + 4 * writecount);
586 if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
588 wcount = wcount - blocksize;
589 address = address + 4 * blocksize;
590 buffer = buffer + 4 * blocksize;
592 else
594 errorcount++;
597 if (errorcount > 1)
599 LOG_WARNING("Block write error address 0x%" PRIx32 ", wcount 0x%x", address, wcount);
600 return ERROR_JTAG_DEVICE_ERROR;
604 return retval;
607 static int mem_ap_write_buf_packed_u16(struct swjdp_common *swjdp,
608 uint8_t *buffer, int count, uint32_t address)
610 int retval = ERROR_OK;
611 int wcount, blocksize, writecount, i;
613 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
615 wcount = count >> 1;
617 while (wcount > 0)
619 int nbytes;
621 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
622 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
624 if (wcount < blocksize)
625 blocksize = wcount;
627 /* handle unaligned data at 4k boundary */
628 if (blocksize == 0)
629 blocksize = 1;
631 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
632 writecount = blocksize;
636 nbytes = MIN((writecount << 1), 4);
638 if (nbytes < 4)
640 if (mem_ap_write_buf_u16(swjdp, buffer, nbytes, address) != ERROR_OK)
642 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
643 return ERROR_JTAG_DEVICE_ERROR;
646 address += nbytes >> 1;
648 else
650 uint32_t outvalue;
651 memcpy(&outvalue, buffer, sizeof(uint32_t));
653 for (i = 0; i < nbytes; i++)
655 *((uint8_t*)buffer + (address & 0x3)) = outvalue;
656 outvalue >>= 8;
657 address++;
660 memcpy(&outvalue, buffer, sizeof(uint32_t));
661 dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
662 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
664 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
665 return ERROR_JTAG_DEVICE_ERROR;
669 buffer += nbytes >> 1;
670 writecount -= nbytes >> 1;
672 } while (writecount);
673 wcount -= blocksize;
676 return retval;
679 int mem_ap_write_buf_u16(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
681 int retval = ERROR_OK;
683 if (count >= 4)
684 return mem_ap_write_buf_packed_u16(swjdp, buffer, count, address);
686 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
688 while (count > 0)
690 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
691 uint16_t svalue;
692 memcpy(&svalue, buffer, sizeof(uint16_t));
693 uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
694 dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
695 retval = swjdp_transaction_endcheck(swjdp);
696 count -= 2;
697 address += 2;
698 buffer += 2;
701 return retval;
704 static int mem_ap_write_buf_packed_u8(struct swjdp_common *swjdp,
705 uint8_t *buffer, int count, uint32_t address)
707 int retval = ERROR_OK;
708 int wcount, blocksize, writecount, i;
710 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
712 wcount = count;
714 while (wcount > 0)
716 int nbytes;
718 /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
719 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
721 if (wcount < blocksize)
722 blocksize = wcount;
724 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
725 writecount = blocksize;
729 nbytes = MIN(writecount, 4);
731 if (nbytes < 4)
733 if (mem_ap_write_buf_u8(swjdp, buffer, nbytes, address) != ERROR_OK)
735 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
736 return ERROR_JTAG_DEVICE_ERROR;
739 address += nbytes;
741 else
743 uint32_t outvalue;
744 memcpy(&outvalue, buffer, sizeof(uint32_t));
746 for (i = 0; i < nbytes; i++)
748 *((uint8_t*)buffer + (address & 0x3)) = outvalue;
749 outvalue >>= 8;
750 address++;
753 memcpy(&outvalue, buffer, sizeof(uint32_t));
754 dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
755 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
757 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
758 return ERROR_JTAG_DEVICE_ERROR;
762 buffer += nbytes;
763 writecount -= nbytes;
765 } while (writecount);
766 wcount -= blocksize;
769 return retval;
772 int mem_ap_write_buf_u8(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
774 int retval = ERROR_OK;
776 if (count >= 4)
777 return mem_ap_write_buf_packed_u8(swjdp, buffer, count, address);
779 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
781 while (count > 0)
783 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
784 uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
785 dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
786 retval = swjdp_transaction_endcheck(swjdp);
787 count--;
788 address++;
789 buffer++;
792 return retval;
795 /*********************************************************************************
797 * mem_ap_read_buf_u32(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address) *
799 * Read block fast in target order (little endian) into a buffer *
801 **********************************************************************************/
802 int mem_ap_read_buf_u32(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
804 int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
805 uint32_t adr = address;
806 uint8_t* pBuffer = buffer;
808 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
810 count >>= 2;
811 wcount = count;
813 while (wcount > 0)
815 /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
816 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
817 if (wcount < blocksize)
818 blocksize = wcount;
820 /* handle unaligned data at 4k boundary */
821 if (blocksize == 0)
822 blocksize = 1;
824 dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
826 /* Scan out first read */
827 adi_jtag_dp_scan(swjdp, JTAG_DP_APACC, AP_REG_DRW,
828 DPAP_READ, 0, NULL, NULL);
829 for (readcount = 0; readcount < blocksize - 1; readcount++)
831 /* Scan out next read; scan in posted value for the
832 * previous one. Assumes read is acked "OK/FAULT",
833 * and CTRL_STAT says that meant "OK".
835 adi_jtag_dp_scan(swjdp, JTAG_DP_APACC, AP_REG_DRW,
836 DPAP_READ, 0, buffer + 4 * readcount,
837 &swjdp->ack);
840 /* Scan in last posted value; RDBUFF has no other effect,
841 * assuming ack is OK/FAULT and CTRL_STAT says "OK".
843 adi_jtag_dp_scan(swjdp, JTAG_DP_DPACC, DP_RDBUFF,
844 DPAP_READ, 0, buffer + 4 * readcount,
845 &swjdp->ack);
846 if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
848 wcount = wcount - blocksize;
849 address += 4 * blocksize;
850 buffer += 4 * blocksize;
852 else
854 errorcount++;
857 if (errorcount > 1)
859 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
860 return ERROR_JTAG_DEVICE_ERROR;
864 /* if we have an unaligned access - reorder data */
865 if (adr & 0x3u)
867 for (readcount = 0; readcount < count; readcount++)
869 int i;
870 uint32_t data;
871 memcpy(&data, pBuffer, sizeof(uint32_t));
873 for (i = 0; i < 4; i++)
875 *((uint8_t*)pBuffer) = (data >> 8 * (adr & 0x3));
876 pBuffer++;
877 adr++;
882 return retval;
885 static int mem_ap_read_buf_packed_u16(struct swjdp_common *swjdp,
886 uint8_t *buffer, int count, uint32_t address)
888 uint32_t invalue;
889 int retval = ERROR_OK;
890 int wcount, blocksize, readcount, i;
892 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
894 wcount = count >> 1;
896 while (wcount > 0)
898 int nbytes;
900 /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
901 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
902 if (wcount < blocksize)
903 blocksize = wcount;
905 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
907 /* handle unaligned data at 4k boundary */
908 if (blocksize == 0)
909 blocksize = 1;
910 readcount = blocksize;
914 dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
915 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
917 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
918 return ERROR_JTAG_DEVICE_ERROR;
921 nbytes = MIN((readcount << 1), 4);
923 for (i = 0; i < nbytes; i++)
925 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
926 buffer++;
927 address++;
930 readcount -= (nbytes >> 1);
931 } while (readcount);
932 wcount -= blocksize;
935 return retval;
938 int mem_ap_read_buf_u16(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
940 uint32_t invalue, i;
941 int retval = ERROR_OK;
943 if (count >= 4)
944 return mem_ap_read_buf_packed_u16(swjdp, buffer, count, address);
946 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
948 while (count > 0)
950 dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
951 dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
952 retval = swjdp_transaction_endcheck(swjdp);
953 if (address & 0x1)
955 for (i = 0; i < 2; i++)
957 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
958 buffer++;
959 address++;
962 else
964 uint16_t svalue = (invalue >> 8 * (address & 0x3));
965 memcpy(buffer, &svalue, sizeof(uint16_t));
966 address += 2;
967 buffer += 2;
969 count -= 2;
972 return retval;
975 /* FIX!!! is this a potential performance bottleneck w.r.t. requiring too many
976 * roundtrips when jtag_execute_queue() has a large overhead(e.g. for USB)s?
978 * The solution is to arrange for a large out/in scan in this loop and
979 * and convert data afterwards.
981 static int mem_ap_read_buf_packed_u8(struct swjdp_common *swjdp,
982 uint8_t *buffer, int count, uint32_t address)
984 uint32_t invalue;
985 int retval = ERROR_OK;
986 int wcount, blocksize, readcount, i;
988 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
990 wcount = count;
992 while (wcount > 0)
994 int nbytes;
996 /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
997 blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
999 if (wcount < blocksize)
1000 blocksize = wcount;
1002 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
1003 readcount = blocksize;
1007 dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
1008 if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
1010 LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
1011 return ERROR_JTAG_DEVICE_ERROR;
1014 nbytes = MIN(readcount, 4);
1016 for (i = 0; i < nbytes; i++)
1018 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
1019 buffer++;
1020 address++;
1023 readcount -= nbytes;
1024 } while (readcount);
1025 wcount -= blocksize;
1028 return retval;
1031 int mem_ap_read_buf_u8(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
1033 uint32_t invalue;
1034 int retval = ERROR_OK;
1036 if (count >= 4)
1037 return mem_ap_read_buf_packed_u8(swjdp, buffer, count, address);
1039 swjdp->trans_mode = TRANS_MODE_COMPOSITE;
1041 while (count > 0)
1043 dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
1044 dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
1045 retval = swjdp_transaction_endcheck(swjdp);
1046 *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
1047 count--;
1048 address++;
1049 buffer++;
1052 return retval;
1056 * Initialize a DAP.
1058 * @todo Rename this. We also need an initialization scheme which account
1059 * for SWD transports not just JTAG; that will need to address differences
1060 * in layering. (JTAG is useful without any debug target; but not SWD.)
1062 int ahbap_debugport_init(struct swjdp_common *swjdp)
1064 uint32_t idreg, romaddr, dummy;
1065 uint32_t ctrlstat;
1066 int cnt = 0;
1067 int retval;
1069 LOG_DEBUG(" ");
1071 /* Default MEM-AP setup.
1073 * REVISIT AP #0 may be an inappropriate default for this.
1074 * Should we probe, or receve a hint from the caller?
1075 * Presumably we can ignore the possibility of multiple APs.
1077 swjdp->apsel = 0;
1078 swjdp->ap_csw_value = -1;
1079 swjdp->ap_tar_value = -1;
1081 /* DP initialization */
1082 swjdp->trans_mode = TRANS_MODE_ATOMIC;
1083 dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
1084 dap_dp_write_reg(swjdp, SSTICKYERR, DP_CTRL_STAT);
1085 dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
1087 swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
1089 dap_dp_write_reg(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
1090 dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
1091 if ((retval = jtag_execute_queue()) != ERROR_OK)
1092 return retval;
1094 /* Check that we have debug power domains activated */
1095 while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10))
1097 LOG_DEBUG("swjdp: wait CDBGPWRUPACK");
1098 dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
1099 if ((retval = jtag_execute_queue()) != ERROR_OK)
1100 return retval;
1101 alive_sleep(10);
1104 while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10))
1106 LOG_DEBUG("swjdp: wait CSYSPWRUPACK");
1107 dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
1108 if ((retval = jtag_execute_queue()) != ERROR_OK)
1109 return retval;
1110 alive_sleep(10);
1113 dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
1114 /* With debug power on we can activate OVERRUN checking */
1115 swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
1116 dap_dp_write_reg(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
1117 dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
1120 * REVISIT this isn't actually *initializing* anything in an AP,
1121 * and doesn't care if it's a MEM-AP at all (much less AHB-AP).
1122 * Should it? If the ROM address is valid, is this the right
1123 * place to scan the table and do any topology detection?
1125 dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &idreg);
1126 dap_ap_read_reg_u32(swjdp, AP_REG_BASE, &romaddr);
1128 LOG_DEBUG("AHB-AP ID Register 0x%" PRIx32 ", Debug ROM Address 0x%" PRIx32 "", idreg, romaddr);
1130 return ERROR_OK;
1133 /* CID interpretation -- see ARM IHI 0029B section 3
1134 * and ARM IHI 0031A table 13-3.
1136 static const char *class_description[16] ={
1137 "Reserved", "ROM table", "Reserved", "Reserved",
1138 "Reserved", "Reserved", "Reserved", "Reserved",
1139 "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
1140 "Reserved", "OptimoDE DESS",
1141 "Generic IP component", "PrimeCell or System component"
1144 static bool
1145 is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
1147 return cid3 == 0xb1 && cid2 == 0x05
1148 && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
1151 int dap_info_command(struct command_context *cmd_ctx, struct swjdp_common *swjdp, int apsel)
1154 uint32_t dbgbase, apid;
1155 int romtable_present = 0;
1156 uint8_t mem_ap;
1157 uint32_t apselold;
1159 apselold = swjdp->apsel;
1160 dap_ap_select(swjdp, apsel);
1161 dap_ap_read_reg_u32(swjdp, AP_REG_BASE, &dbgbase);
1162 dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &apid);
1163 swjdp_transaction_endcheck(swjdp);
1164 /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
1165 mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
1166 command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
1167 if (apid)
1169 switch (apid&0x0F)
1171 case 0:
1172 command_print(cmd_ctx, "\tType is JTAG-AP");
1173 break;
1174 case 1:
1175 command_print(cmd_ctx, "\tType is MEM-AP AHB");
1176 break;
1177 case 2:
1178 command_print(cmd_ctx, "\tType is MEM-AP APB");
1179 break;
1180 default:
1181 command_print(cmd_ctx, "\tUnknown AP type");
1182 break;
1185 /* NOTE: a MEM-AP may have a single CoreSight component that's
1186 * not a ROM table ... or have no such components at all.
1188 if (mem_ap)
1189 command_print(cmd_ctx, "AP BASE 0x%8.8" PRIx32,
1190 dbgbase);
1192 else
1194 command_print(cmd_ctx, "No AP found at this apsel 0x%x", apsel);
1197 romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
1198 if (romtable_present)
1200 uint32_t cid0,cid1,cid2,cid3,memtype,romentry;
1201 uint16_t entry_offset;
1203 /* bit 16 of apid indicates a memory access port */
1204 if (dbgbase & 0x02)
1205 command_print(cmd_ctx, "\tValid ROM table present");
1206 else
1207 command_print(cmd_ctx, "\tROM table in legacy format");
1209 /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
1210 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
1211 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
1212 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
1213 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
1214 mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
1215 swjdp_transaction_endcheck(swjdp);
1216 if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1217 command_print(cmd_ctx, "\tCID3 0x%2.2" PRIx32
1218 ", CID2 0x%2.2" PRIx32
1219 ", CID1 0x%2.2" PRIx32
1220 ", CID0 0x%2.2" PRIx32,
1221 cid3, cid2, cid1, cid0);
1222 if (memtype & 0x01)
1223 command_print(cmd_ctx, "\tMEMTYPE system memory present on bus");
1224 else
1225 command_print(cmd_ctx, "\tMEMTYPE System memory not present. "
1226 "Dedicated debug bus.");
1228 /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
1229 entry_offset = 0;
1232 mem_ap_read_atomic_u32(swjdp, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
1233 command_print(cmd_ctx, "\tROMTABLE[0x%x] = 0x%" PRIx32 "",entry_offset,romentry);
1234 if (romentry&0x01)
1236 uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
1237 uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
1238 uint32_t component_start, component_base;
1239 unsigned part_num;
1240 char *type, *full;
1242 component_base = (uint32_t)((dbgbase & 0xFFFFF000)
1243 + (int)(romentry & 0xFFFFF000));
1244 mem_ap_read_atomic_u32(swjdp,
1245 (component_base & 0xFFFFF000) | 0xFE0, &c_pid0);
1246 mem_ap_read_atomic_u32(swjdp,
1247 (component_base & 0xFFFFF000) | 0xFE4, &c_pid1);
1248 mem_ap_read_atomic_u32(swjdp,
1249 (component_base & 0xFFFFF000) | 0xFE8, &c_pid2);
1250 mem_ap_read_atomic_u32(swjdp,
1251 (component_base & 0xFFFFF000) | 0xFEC, &c_pid3);
1252 mem_ap_read_atomic_u32(swjdp,
1253 (component_base & 0xFFFFF000) | 0xFD0, &c_pid4);
1254 mem_ap_read_atomic_u32(swjdp,
1255 (component_base & 0xFFFFF000) | 0xFF0, &c_cid0);
1256 mem_ap_read_atomic_u32(swjdp,
1257 (component_base & 0xFFFFF000) | 0xFF4, &c_cid1);
1258 mem_ap_read_atomic_u32(swjdp,
1259 (component_base & 0xFFFFF000) | 0xFF8, &c_cid2);
1260 mem_ap_read_atomic_u32(swjdp,
1261 (component_base & 0xFFFFF000) | 0xFFC, &c_cid3);
1262 component_start = component_base - 0x1000*(c_pid4 >> 4);
1264 command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32
1265 ", start address 0x%" PRIx32,
1266 component_base, component_start);
1267 command_print(cmd_ctx, "\t\tComponent class is 0x%x, %s",
1268 (int) (c_cid1 >> 4) & 0xf,
1269 /* See ARM IHI 0029B Table 3-3 */
1270 class_description[(c_cid1 >> 4) & 0xf]);
1272 /* CoreSight component? */
1273 if (((c_cid1 >> 4) & 0x0f) == 9) {
1274 uint32_t devtype;
1275 unsigned minor;
1276 char *major = "Reserved", *subtype = "Reserved";
1278 mem_ap_read_atomic_u32(swjdp,
1279 (component_base & 0xfffff000) | 0xfcc,
1280 &devtype);
1281 minor = (devtype >> 4) & 0x0f;
1282 switch (devtype & 0x0f) {
1283 case 0:
1284 major = "Miscellaneous";
1285 switch (minor) {
1286 case 0:
1287 subtype = "other";
1288 break;
1289 case 4:
1290 subtype = "Validation component";
1291 break;
1293 break;
1294 case 1:
1295 major = "Trace Sink";
1296 switch (minor) {
1297 case 0:
1298 subtype = "other";
1299 break;
1300 case 1:
1301 subtype = "Port";
1302 break;
1303 case 2:
1304 subtype = "Buffer";
1305 break;
1307 break;
1308 case 2:
1309 major = "Trace Link";
1310 switch (minor) {
1311 case 0:
1312 subtype = "other";
1313 break;
1314 case 1:
1315 subtype = "Funnel, router";
1316 break;
1317 case 2:
1318 subtype = "Filter";
1319 break;
1320 case 3:
1321 subtype = "FIFO, buffer";
1322 break;
1324 break;
1325 case 3:
1326 major = "Trace Source";
1327 switch (minor) {
1328 case 0:
1329 subtype = "other";
1330 break;
1331 case 1:
1332 subtype = "Processor";
1333 break;
1334 case 2:
1335 subtype = "DSP";
1336 break;
1337 case 3:
1338 subtype = "Engine/Coprocessor";
1339 break;
1340 case 4:
1341 subtype = "Bus";
1342 break;
1344 break;
1345 case 4:
1346 major = "Debug Control";
1347 switch (minor) {
1348 case 0:
1349 subtype = "other";
1350 break;
1351 case 1:
1352 subtype = "Trigger Matrix";
1353 break;
1354 case 2:
1355 subtype = "Debug Auth";
1356 break;
1358 break;
1359 case 5:
1360 major = "Debug Logic";
1361 switch (minor) {
1362 case 0:
1363 subtype = "other";
1364 break;
1365 case 1:
1366 subtype = "Processor";
1367 break;
1368 case 2:
1369 subtype = "DSP";
1370 break;
1371 case 3:
1372 subtype = "Engine/Coprocessor";
1373 break;
1375 break;
1377 command_print(cmd_ctx, "\t\tType is 0x%2.2x, %s, %s",
1378 (unsigned) (devtype & 0xff),
1379 major, subtype);
1380 /* REVISIT also show 0xfc8 DevId */
1383 if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
1384 command_print(cmd_ctx, "\t\tCID3 0x%2.2" PRIx32
1385 ", CID2 0x%2.2" PRIx32
1386 ", CID1 0x%2.2" PRIx32
1387 ", CID0 0x%2.2" PRIx32,
1388 c_cid3, c_cid2, c_cid1, c_cid0);
1389 command_print(cmd_ctx, "\t\tPeripheral ID[4..0] = hex "
1390 "%2.2x %2.2x %2.2x %2.2x %2.2x",
1391 (int) c_pid4,
1392 (int) c_pid3, (int) c_pid2,
1393 (int) c_pid1, (int) c_pid0);
1395 /* Part number interpretations are from Cortex
1396 * core specs, the CoreSight components TRM
1397 * (ARM DDI 0314H), and ETM specs; also from
1398 * chip observation (e.g. TI SDTI).
1400 part_num = c_pid0 & 0xff;
1401 part_num |= (c_pid1 & 0x0f) << 8;
1402 switch (part_num) {
1403 case 0x000:
1404 type = "Cortex-M3 NVIC";
1405 full = "(Interrupt Controller)";
1406 break;
1407 case 0x001:
1408 type = "Cortex-M3 ITM";
1409 full = "(Instrumentation Trace Module)";
1410 break;
1411 case 0x002:
1412 type = "Cortex-M3 DWT";
1413 full = "(Data Watchpoint and Trace)";
1414 break;
1415 case 0x003:
1416 type = "Cortex-M3 FBP";
1417 full = "(Flash Patch and Breakpoint)";
1418 break;
1419 case 0x00d:
1420 type = "CoreSight ETM11";
1421 full = "(Embedded Trace)";
1422 break;
1423 // case 0x113: what?
1424 case 0x120: /* from OMAP3 memmap */
1425 type = "TI SDTI";
1426 full = "(System Debug Trace Interface)";
1427 break;
1428 case 0x343: /* from OMAP3 memmap */
1429 type = "TI DAPCTL";
1430 full = "";
1431 break;
1432 case 0x4e0:
1433 type = "Cortex-M3 ETM";
1434 full = "(Embedded Trace)";
1435 break;
1436 case 0x906:
1437 type = "Coresight CTI";
1438 full = "(Cross Trigger)";
1439 break;
1440 case 0x907:
1441 type = "Coresight ETB";
1442 full = "(Trace Buffer)";
1443 break;
1444 case 0x908:
1445 type = "Coresight CSTF";
1446 full = "(Trace Funnel)";
1447 break;
1448 case 0x910:
1449 type = "CoreSight ETM9";
1450 full = "(Embedded Trace)";
1451 break;
1452 case 0x912:
1453 type = "Coresight TPIU";
1454 full = "(Trace Port Interface Unit)";
1455 break;
1456 case 0x921:
1457 type = "Cortex-A8 ETM";
1458 full = "(Embedded Trace)";
1459 break;
1460 case 0x922:
1461 type = "Cortex-A8 CTI";
1462 full = "(Cross Trigger)";
1463 break;
1464 case 0x923:
1465 type = "Cortex-M3 TPIU";
1466 full = "(Trace Port Interface Unit)";
1467 break;
1468 case 0xc08:
1469 type = "Cortex-A8 Debug";
1470 full = "(Debug Unit)";
1471 break;
1472 default:
1473 type = "-*- unrecognized -*-";
1474 full = "";
1475 break;
1477 command_print(cmd_ctx, "\t\tPart is %s %s",
1478 type, full);
1480 else
1482 if (romentry)
1483 command_print(cmd_ctx, "\t\tComponent not present");
1484 else
1485 command_print(cmd_ctx, "\t\tEnd of ROM table");
1487 entry_offset += 4;
1488 } while (romentry > 0);
1490 else
1492 command_print(cmd_ctx, "\tNo ROM table present");
1494 dap_ap_select(swjdp, apselold);
1496 return ERROR_OK;
1499 DAP_COMMAND_HANDLER(dap_baseaddr_command)
1501 uint32_t apsel, apselsave, baseaddr;
1502 int retval;
1504 apselsave = swjdp->apsel;
1505 switch (CMD_ARGC) {
1506 case 0:
1507 apsel = swjdp->apsel;
1508 break;
1509 case 1:
1510 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1511 break;
1512 default:
1513 return ERROR_COMMAND_SYNTAX_ERROR;
1516 if (apselsave != apsel)
1517 dap_ap_select(swjdp, apsel);
1519 dap_ap_read_reg_u32(swjdp, AP_REG_BASE, &baseaddr);
1520 retval = swjdp_transaction_endcheck(swjdp);
1521 command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
1523 if (apselsave != apsel)
1524 dap_ap_select(swjdp, apselsave);
1526 return retval;
1529 DAP_COMMAND_HANDLER(dap_memaccess_command)
1531 uint32_t memaccess_tck;
1533 switch (CMD_ARGC) {
1534 case 0:
1535 memaccess_tck = swjdp->memaccess_tck;
1536 break;
1537 case 1:
1538 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
1539 break;
1540 default:
1541 return ERROR_COMMAND_SYNTAX_ERROR;
1543 swjdp->memaccess_tck = memaccess_tck;
1545 command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
1546 swjdp->memaccess_tck);
1548 return ERROR_OK;
1551 DAP_COMMAND_HANDLER(dap_apsel_command)
1553 uint32_t apsel, apid;
1554 int retval;
1556 switch (CMD_ARGC) {
1557 case 0:
1558 apsel = 0;
1559 break;
1560 case 1:
1561 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1562 break;
1563 default:
1564 return ERROR_COMMAND_SYNTAX_ERROR;
1567 dap_ap_select(swjdp, apsel);
1568 dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &apid);
1569 retval = swjdp_transaction_endcheck(swjdp);
1570 command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
1571 apsel, apid);
1573 return retval;
1576 DAP_COMMAND_HANDLER(dap_apid_command)
1578 uint32_t apsel, apselsave, apid;
1579 int retval;
1581 apselsave = swjdp->apsel;
1582 switch (CMD_ARGC) {
1583 case 0:
1584 apsel = swjdp->apsel;
1585 break;
1586 case 1:
1587 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
1588 break;
1589 default:
1590 return ERROR_COMMAND_SYNTAX_ERROR;
1593 if (apselsave != apsel)
1594 dap_ap_select(swjdp, apsel);
1596 dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &apid);
1597 retval = swjdp_transaction_endcheck(swjdp);
1598 command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
1599 if (apselsave != apsel)
1600 dap_ap_select(swjdp, apselsave);
1602 return retval;