You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1705 lines
48 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Magnus Lundin *
  3. * lundin@mlu.mine.nu *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * Copyright (C) 2009-2010 by Oyvind Harboe *
  9. * oyvind.harboe@zylin.com *
  10. * *
  11. * Copyright (C) 2009-2010 by David Brownell *
  12. * *
  13. * Copyright (C) 2013 by Andreas Fritiofson *
  14. * andreas.fritiofson@gmail.com *
  15. * *
  16. * This program is free software; you can redistribute it and/or modify *
  17. * it under the terms of the GNU General Public License as published by *
  18. * the Free Software Foundation; either version 2 of the License, or *
  19. * (at your option) any later version. *
  20. * *
  21. * This program is distributed in the hope that it will be useful, *
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  24. * GNU General Public License for more details. *
  25. * *
  26. * You should have received a copy of the GNU General Public License *
  27. * along with this program; if not, write to the *
  28. * Free Software Foundation, Inc., *
  29. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  30. ***************************************************************************/
  31. /**
  32. * @file
  33. * This file implements support for the ARM Debug Interface version 5 (ADIv5)
  34. * debugging architecture. Compared with previous versions, this includes
  35. * a low pin-count Serial Wire Debug (SWD) alternative to JTAG for message
  36. * transport, and focusses on memory mapped resources as defined by the
  37. * CoreSight architecture.
  38. *
  39. * A key concept in ADIv5 is the Debug Access Port, or DAP. A DAP has two
  40. * basic components: a Debug Port (DP) transporting messages to and from a
  41. * debugger, and an Access Port (AP) accessing resources. Three types of DP
  42. * are defined. One uses only JTAG for communication, and is called JTAG-DP.
  43. * One uses only SWD for communication, and is called SW-DP. The third can
  44. * use either SWD or JTAG, and is called SWJ-DP. The most common type of AP
  45. * is used to access memory mapped resources and is called a MEM-AP. Also a
  46. * JTAG-AP is also defined, bridging to JTAG resources; those are uncommon.
  47. *
  48. * This programming interface allows DAP pipelined operations through a
  49. * transaction queue. This primarily affects AP operations (such as using
  50. * a MEM-AP to access memory or registers). If the current transaction has
  51. * not finished by the time the next one must begin, and the ORUNDETECT bit
  52. * is set in the DP_CTRL_STAT register, the SSTICKYORUN status is set and
  53. * further AP operations will fail. There are two basic methods to avoid
  54. * such overrun errors. One involves polling for status instead of using
  55. * transaction piplining. The other involves adding delays to ensure the
  56. * AP has enough time to complete one operation before starting the next
  57. * one. (For JTAG these delays are controlled by memaccess_tck.)
  58. */
  59. /*
  60. * Relevant specifications from ARM include:
  61. *
  62. * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031A
  63. * CoreSight(tm) v1.0 Architecture Specification ARM IHI 0029B
  64. *
  65. * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316D
  66. * Cortex-M3(tm) TRM, ARM DDI 0337G
  67. */
  68. #ifdef HAVE_CONFIG_H
  69. #include "config.h"
  70. #endif
  71. #include "jtag/interface.h"
  72. #include "arm.h"
  73. #include "arm_adi_v5.h"
  74. #include <helper/time_support.h>
  75. /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
  76. /*
  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
  79. */
  80. static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
  81. {
  82. return tar_autoincr_block - ((tar_autoincr_block - 1) & address);
  83. }
  84. /***************************************************************************
  85. * *
  86. * DP and MEM-AP register access through APACC and DPACC *
  87. * *
  88. ***************************************************************************/
  89. /**
  90. * Select one of the APs connected to the specified DAP. The
  91. * selection is implicitly used with future AP transactions.
  92. * This is a NOP if the specified AP is already selected.
  93. *
  94. * @param dap The DAP
  95. * @param apsel Number of the AP to (implicitly) use with further
  96. * transactions. This normally identifies a MEM-AP.
  97. */
  98. void dap_ap_select(struct adiv5_dap *dap, uint8_t ap)
  99. {
  100. uint32_t new_ap = (ap << 24) & 0xFF000000;
  101. if (new_ap != dap->ap_current) {
  102. dap->ap_current = new_ap;
  103. /* Switching AP invalidates cached values.
  104. * Values MUST BE UPDATED BEFORE AP ACCESS.
  105. */
  106. dap->ap_bank_value = -1;
  107. dap->ap_csw_value = -1;
  108. dap->ap_tar_value = -1;
  109. }
  110. }
  111. static int dap_setup_accessport_csw(struct adiv5_dap *dap, uint32_t csw)
  112. {
  113. csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT |
  114. dap->apcsw[dap->ap_current >> 24];
  115. if (csw != dap->ap_csw_value) {
  116. /* LOG_DEBUG("DAP: Set CSW %x",csw); */
  117. int retval = dap_queue_ap_write(dap, AP_REG_CSW, csw);
  118. if (retval != ERROR_OK)
  119. return retval;
  120. dap->ap_csw_value = csw;
  121. }
  122. return ERROR_OK;
  123. }
  124. static int dap_setup_accessport_tar(struct adiv5_dap *dap, uint32_t tar)
  125. {
  126. if (tar != dap->ap_tar_value || dap->ap_csw_value & CSW_ADDRINC_MASK) {
  127. /* LOG_DEBUG("DAP: Set TAR %x",tar); */
  128. int retval = dap_queue_ap_write(dap, AP_REG_TAR, tar);
  129. if (retval != ERROR_OK)
  130. return retval;
  131. dap->ap_tar_value = tar;
  132. }
  133. return ERROR_OK;
  134. }
  135. /**
  136. * Queue transactions setting up transfer parameters for the
  137. * currently selected MEM-AP.
  138. *
  139. * Subsequent transfers using registers like AP_REG_DRW or AP_REG_BD2
  140. * initiate data reads or writes using memory or peripheral addresses.
  141. * If the CSW is configured for it, the TAR may be automatically
  142. * incremented after each transfer.
  143. *
  144. * @todo Rename to reflect it being specifically a MEM-AP function.
  145. *
  146. * @param dap The DAP connected to the MEM-AP.
  147. * @param csw MEM-AP Control/Status Word (CSW) register to assign. If this
  148. * matches the cached value, the register is not changed.
  149. * @param tar MEM-AP Transfer Address Register (TAR) to assign. If this
  150. * matches the cached address, the register is not changed.
  151. *
  152. * @return ERROR_OK if the transaction was properly queued, else a fault code.
  153. */
  154. int dap_setup_accessport(struct adiv5_dap *dap, uint32_t csw, uint32_t tar)
  155. {
  156. int retval;
  157. retval = dap_setup_accessport_csw(dap, csw);
  158. if (retval != ERROR_OK)
  159. return retval;
  160. retval = dap_setup_accessport_tar(dap, tar);
  161. if (retval != ERROR_OK)
  162. return retval;
  163. return ERROR_OK;
  164. }
  165. /**
  166. * Asynchronous (queued) read of a word from memory or a system register.
  167. *
  168. * @param dap The DAP connected to the MEM-AP performing the read.
  169. * @param address Address of the 32-bit word to read; it must be
  170. * readable by the currently selected MEM-AP.
  171. * @param value points to where the word will be stored when the
  172. * transaction queue is flushed (assuming no errors).
  173. *
  174. * @return ERROR_OK for success. Otherwise a fault code.
  175. */
  176. int mem_ap_read_u32(struct adiv5_dap *dap, uint32_t address,
  177. uint32_t *value)
  178. {
  179. int retval;
  180. /* Use banked addressing (REG_BDx) to avoid some link traffic
  181. * (updating TAR) when reading several consecutive addresses.
  182. */
  183. retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_OFF,
  184. address & 0xFFFFFFF0);
  185. if (retval != ERROR_OK)
  186. return retval;
  187. return dap_queue_ap_read(dap, AP_REG_BD0 | (address & 0xC), value);
  188. }
  189. /**
  190. * Synchronous read of a word from memory or a system register.
  191. * As a side effect, this flushes any queued transactions.
  192. *
  193. * @param dap The DAP connected to the MEM-AP performing the read.
  194. * @param address Address of the 32-bit word to read; it must be
  195. * readable by the currently selected MEM-AP.
  196. * @param value points to where the result will be stored.
  197. *
  198. * @return ERROR_OK for success; *value holds the result.
  199. * Otherwise a fault code.
  200. */
  201. int mem_ap_read_atomic_u32(struct adiv5_dap *dap, uint32_t address,
  202. uint32_t *value)
  203. {
  204. int retval;
  205. retval = mem_ap_read_u32(dap, address, value);
  206. if (retval != ERROR_OK)
  207. return retval;
  208. return dap_run(dap);
  209. }
  210. /**
  211. * Asynchronous (queued) write of a word to memory or a system register.
  212. *
  213. * @param dap The DAP connected to the MEM-AP.
  214. * @param address Address to be written; it must be writable by
  215. * the currently selected MEM-AP.
  216. * @param value Word that will be written to the address when transaction
  217. * queue is flushed (assuming no errors).
  218. *
  219. * @return ERROR_OK for success. Otherwise a fault code.
  220. */
  221. int mem_ap_write_u32(struct adiv5_dap *dap, uint32_t address,
  222. uint32_t value)
  223. {
  224. int retval;
  225. /* Use banked addressing (REG_BDx) to avoid some link traffic
  226. * (updating TAR) when writing several consecutive addresses.
  227. */
  228. retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_OFF,
  229. address & 0xFFFFFFF0);
  230. if (retval != ERROR_OK)
  231. return retval;
  232. return dap_queue_ap_write(dap, AP_REG_BD0 | (address & 0xC),
  233. value);
  234. }
  235. /**
  236. * Synchronous write of a word to memory or a system register.
  237. * As a side effect, this flushes any queued transactions.
  238. *
  239. * @param dap The DAP connected to the MEM-AP.
  240. * @param address Address to be written; it must be writable by
  241. * the currently selected MEM-AP.
  242. * @param value Word that will be written.
  243. *
  244. * @return ERROR_OK for success; the data was written. Otherwise a fault code.
  245. */
  246. int mem_ap_write_atomic_u32(struct adiv5_dap *dap, uint32_t address,
  247. uint32_t value)
  248. {
  249. int retval = mem_ap_write_u32(dap, address, value);
  250. if (retval != ERROR_OK)
  251. return retval;
  252. return dap_run(dap);
  253. }
  254. /**
  255. * Synchronous write of a block of memory, using a specific access size.
  256. *
  257. * @param dap The DAP connected to the MEM-AP.
  258. * @param buffer The data buffer to write. No particular alignment is assumed.
  259. * @param size Which access size to use, in bytes. 1, 2 or 4.
  260. * @param count The number of writes to do (in size units, not bytes).
  261. * @param address Address to be written; it must be writable by the currently selected MEM-AP.
  262. * @param addrinc Whether the target address should be increased for each write or not. This
  263. * should normally be true, except when writing to e.g. a FIFO.
  264. * @return ERROR_OK on success, otherwise an error code.
  265. */
  266. int mem_ap_write(struct adiv5_dap *dap, const uint8_t *buffer, uint32_t size, uint32_t count,
  267. uint32_t address, bool addrinc)
  268. {
  269. size_t nbytes = size * count;
  270. const uint32_t csw_addrincr = addrinc ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
  271. uint32_t csw_size;
  272. uint32_t addr_xor;
  273. int retval;
  274. /* TI BE-32 Quirks mode:
  275. * Writes on big-endian TMS570 behave very strangely. Observed behavior:
  276. * size write address bytes written in order
  277. * 4 TAR ^ 0 (val >> 24), (val >> 16), (val >> 8), (val)
  278. * 2 TAR ^ 2 (val >> 8), (val)
  279. * 1 TAR ^ 3 (val)
  280. * For example, if you attempt to write a single byte to address 0, the processor
  281. * will actually write a byte to address 3.
  282. *
  283. * To make writes of size < 4 work as expected, we xor a value with the address before
  284. * setting the TAP, and we set the TAP after every transfer rather then relying on
  285. * address increment. */
  286. if (size == 4) {
  287. csw_size = CSW_32BIT;
  288. addr_xor = 0;
  289. } else if (size == 2) {
  290. csw_size = CSW_16BIT;
  291. addr_xor = dap->ti_be_32_quirks ? 2 : 0;
  292. } else if (size == 1) {
  293. csw_size = CSW_8BIT;
  294. addr_xor = dap->ti_be_32_quirks ? 3 : 0;
  295. } else {
  296. return ERROR_TARGET_UNALIGNED_ACCESS;
  297. }
  298. if (dap->unaligned_access_bad && (address % size != 0))
  299. return ERROR_TARGET_UNALIGNED_ACCESS;
  300. retval = dap_setup_accessport_tar(dap, address ^ addr_xor);
  301. if (retval != ERROR_OK)
  302. return retval;
  303. while (nbytes > 0) {
  304. uint32_t this_size = size;
  305. /* Select packed transfer if possible */
  306. if (addrinc && dap->packed_transfers && nbytes >= 4
  307. && max_tar_block_size(dap->tar_autoincr_block, address) >= 4) {
  308. this_size = 4;
  309. retval = dap_setup_accessport_csw(dap, csw_size | CSW_ADDRINC_PACKED);
  310. } else {
  311. retval = dap_setup_accessport_csw(dap, csw_size | csw_addrincr);
  312. }
  313. if (retval != ERROR_OK)
  314. break;
  315. /* How many source bytes each transfer will consume, and their location in the DRW,
  316. * depends on the type of transfer and alignment. See ARM document IHI0031C. */
  317. uint32_t outvalue = 0;
  318. if (dap->ti_be_32_quirks) {
  319. switch (this_size) {
  320. case 4:
  321. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
  322. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
  323. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
  324. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (address++ & 3) ^ addr_xor);
  325. break;
  326. case 2:
  327. outvalue |= (uint32_t)*buffer++ << 8 * (1 ^ (address++ & 3) ^ addr_xor);
  328. outvalue |= (uint32_t)*buffer++ << 8 * (1 ^ (address++ & 3) ^ addr_xor);
  329. break;
  330. case 1:
  331. outvalue |= (uint32_t)*buffer++ << 8 * (0 ^ (address++ & 3) ^ addr_xor);
  332. break;
  333. }
  334. } else {
  335. switch (this_size) {
  336. case 4:
  337. outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
  338. outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
  339. case 2:
  340. outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
  341. case 1:
  342. outvalue |= (uint32_t)*buffer++ << 8 * (address++ & 3);
  343. }
  344. }
  345. nbytes -= this_size;
  346. retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
  347. if (retval != ERROR_OK)
  348. break;
  349. /* Rewrite TAR if it wrapped or we're xoring addresses */
  350. if (addrinc && (addr_xor || (address % dap->tar_autoincr_block < size && nbytes > 0))) {
  351. retval = dap_setup_accessport_tar(dap, address ^ addr_xor);
  352. if (retval != ERROR_OK)
  353. break;
  354. }
  355. }
  356. /* REVISIT: Might want to have a queued version of this function that does not run. */
  357. if (retval == ERROR_OK)
  358. retval = dap_run(dap);
  359. if (retval != ERROR_OK) {
  360. uint32_t tar;
  361. if (dap_queue_ap_read(dap, AP_REG_TAR, &tar) == ERROR_OK
  362. && dap_run(dap) == ERROR_OK)
  363. LOG_ERROR("Failed to write memory at 0x%08"PRIx32, tar);
  364. else
  365. LOG_ERROR("Failed to write memory and, additionally, failed to find out where");
  366. }
  367. return retval;
  368. }
  369. /**
  370. * Synchronous read of a block of memory, using a specific access size.
  371. *
  372. * @param dap The DAP connected to the MEM-AP.
  373. * @param buffer The data buffer to receive the data. No particular alignment is assumed.
  374. * @param size Which access size to use, in bytes. 1, 2 or 4.
  375. * @param count The number of reads to do (in size units, not bytes).
  376. * @param address Address to be read; it must be readable by the currently selected MEM-AP.
  377. * @param addrinc Whether the target address should be increased after each read or not. This
  378. * should normally be true, except when reading from e.g. a FIFO.
  379. * @return ERROR_OK on success, otherwise an error code.
  380. */
  381. int mem_ap_read(struct adiv5_dap *dap, uint8_t *buffer, uint32_t size, uint32_t count,
  382. uint32_t adr, bool addrinc)
  383. {
  384. size_t nbytes = size * count;
  385. const uint32_t csw_addrincr = addrinc ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
  386. uint32_t csw_size;
  387. uint32_t address = adr;
  388. int retval;
  389. /* TI BE-32 Quirks mode:
  390. * Reads on big-endian TMS570 behave strangely differently than writes.
  391. * They read from the physical address requested, but with DRW byte-reversed.
  392. * For example, a byte read from address 0 will place the result in the high bytes of DRW.
  393. * Also, packed 8-bit and 16-bit transfers seem to sometimes return garbage in some bytes,
  394. * so avoid them. */
  395. if (size == 4)
  396. csw_size = CSW_32BIT;
  397. else if (size == 2)
  398. csw_size = CSW_16BIT;
  399. else if (size == 1)
  400. csw_size = CSW_8BIT;
  401. else
  402. return ERROR_TARGET_UNALIGNED_ACCESS;
  403. if (dap->unaligned_access_bad && (adr % size != 0))
  404. return ERROR_TARGET_UNALIGNED_ACCESS;
  405. /* Allocate buffer to hold the sequence of DRW reads that will be made. This is a significant
  406. * over-allocation if packed transfers are going to be used, but determining the real need at
  407. * this point would be messy. */
  408. uint32_t *read_buf = malloc(count * sizeof(uint32_t));
  409. uint32_t *read_ptr = read_buf;
  410. if (read_buf == NULL) {
  411. LOG_ERROR("Failed to allocate read buffer");
  412. return ERROR_FAIL;
  413. }
  414. retval = dap_setup_accessport_tar(dap, address);
  415. if (retval != ERROR_OK) {
  416. free(read_buf);
  417. return retval;
  418. }
  419. /* Queue up all reads. Each read will store the entire DRW word in the read buffer. How many
  420. * useful bytes it contains, and their location in the word, depends on the type of transfer
  421. * and alignment. */
  422. while (nbytes > 0) {
  423. uint32_t this_size = size;
  424. /* Select packed transfer if possible */
  425. if (addrinc && dap->packed_transfers && nbytes >= 4
  426. && max_tar_block_size(dap->tar_autoincr_block, address) >= 4) {
  427. this_size = 4;
  428. retval = dap_setup_accessport_csw(dap, csw_size | CSW_ADDRINC_PACKED);
  429. } else {
  430. retval = dap_setup_accessport_csw(dap, csw_size | csw_addrincr);
  431. }
  432. if (retval != ERROR_OK)
  433. break;
  434. retval = dap_queue_ap_read(dap, AP_REG_DRW, read_ptr++);
  435. if (retval != ERROR_OK)
  436. break;
  437. nbytes -= this_size;
  438. address += this_size;
  439. /* Rewrite TAR if it wrapped */
  440. if (addrinc && address % dap->tar_autoincr_block < size && nbytes > 0) {
  441. retval = dap_setup_accessport_tar(dap, address);
  442. if (retval != ERROR_OK)
  443. break;
  444. }
  445. }
  446. if (retval == ERROR_OK)
  447. retval = dap_run(dap);
  448. /* Restore state */
  449. address = adr;
  450. nbytes = size * count;
  451. read_ptr = read_buf;
  452. /* If something failed, read TAR to find out how much data was successfully read, so we can
  453. * at least give the caller what we have. */
  454. if (retval != ERROR_OK) {
  455. uint32_t tar;
  456. if (dap_queue_ap_read(dap, AP_REG_TAR, &tar) == ERROR_OK
  457. && dap_run(dap) == ERROR_OK) {
  458. LOG_ERROR("Failed to read memory at 0x%08"PRIx32, tar);
  459. if (nbytes > tar - address)
  460. nbytes = tar - address;
  461. } else {
  462. LOG_ERROR("Failed to read memory and, additionally, failed to find out where");
  463. nbytes = 0;
  464. }
  465. }
  466. /* Replay loop to populate caller's buffer from the correct word and byte lane */
  467. while (nbytes > 0) {
  468. uint32_t this_size = size;
  469. if (addrinc && dap->packed_transfers && nbytes >= 4
  470. && max_tar_block_size(dap->tar_autoincr_block, address) >= 4) {
  471. this_size = 4;
  472. }
  473. if (dap->ti_be_32_quirks) {
  474. switch (this_size) {
  475. case 4:
  476. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  477. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  478. case 2:
  479. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  480. case 1:
  481. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  482. }
  483. } else {
  484. switch (this_size) {
  485. case 4:
  486. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  487. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  488. case 2:
  489. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  490. case 1:
  491. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  492. }
  493. }
  494. read_ptr++;
  495. nbytes -= this_size;
  496. }
  497. free(read_buf);
  498. return retval;
  499. }
  500. /*--------------------------------------------------------------------*/
  501. /* Wrapping function with selection of AP */
  502. /*--------------------------------------------------------------------*/
  503. int mem_ap_sel_read_u32(struct adiv5_dap *swjdp, uint8_t ap,
  504. uint32_t address, uint32_t *value)
  505. {
  506. dap_ap_select(swjdp, ap);
  507. return mem_ap_read_u32(swjdp, address, value);
  508. }
  509. int mem_ap_sel_write_u32(struct adiv5_dap *swjdp, uint8_t ap,
  510. uint32_t address, uint32_t value)
  511. {
  512. dap_ap_select(swjdp, ap);
  513. return mem_ap_write_u32(swjdp, address, value);
  514. }
  515. int mem_ap_sel_read_atomic_u32(struct adiv5_dap *swjdp, uint8_t ap,
  516. uint32_t address, uint32_t *value)
  517. {
  518. dap_ap_select(swjdp, ap);
  519. return mem_ap_read_atomic_u32(swjdp, address, value);
  520. }
  521. int mem_ap_sel_write_atomic_u32(struct adiv5_dap *swjdp, uint8_t ap,
  522. uint32_t address, uint32_t value)
  523. {
  524. dap_ap_select(swjdp, ap);
  525. return mem_ap_write_atomic_u32(swjdp, address, value);
  526. }
  527. int mem_ap_sel_read_buf(struct adiv5_dap *swjdp, uint8_t ap,
  528. uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
  529. {
  530. dap_ap_select(swjdp, ap);
  531. return mem_ap_read(swjdp, buffer, size, count, address, true);
  532. }
  533. int mem_ap_sel_write_buf(struct adiv5_dap *swjdp, uint8_t ap,
  534. const uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
  535. {
  536. dap_ap_select(swjdp, ap);
  537. return mem_ap_write(swjdp, buffer, size, count, address, true);
  538. }
  539. int mem_ap_sel_read_buf_noincr(struct adiv5_dap *swjdp, uint8_t ap,
  540. uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
  541. {
  542. dap_ap_select(swjdp, ap);
  543. return mem_ap_read(swjdp, buffer, size, count, address, false);
  544. }
  545. int mem_ap_sel_write_buf_noincr(struct adiv5_dap *swjdp, uint8_t ap,
  546. const uint8_t *buffer, uint32_t size, uint32_t count, uint32_t address)
  547. {
  548. dap_ap_select(swjdp, ap);
  549. return mem_ap_write(swjdp, buffer, size, count, address, false);
  550. }
  551. /*--------------------------------------------------------------------------*/
  552. #define DAP_POWER_DOMAIN_TIMEOUT (10)
  553. /* FIXME don't import ... just initialize as
  554. * part of DAP transport setup
  555. */
  556. extern const struct dap_ops jtag_dp_ops;
  557. /*--------------------------------------------------------------------------*/
  558. /**
  559. * Initialize a DAP. This sets up the power domains, prepares the DP
  560. * for further use, and arranges to use AP #0 for all AP operations
  561. * until dap_ap-select() changes that policy.
  562. *
  563. * @param dap The DAP being initialized.
  564. *
  565. * @todo Rename this. We also need an initialization scheme which account
  566. * for SWD transports not just JTAG; that will need to address differences
  567. * in layering. (JTAG is useful without any debug target; but not SWD.)
  568. * And this may not even use an AHB-AP ... e.g. DAP-Lite uses an APB-AP.
  569. */
  570. int ahbap_debugport_init(struct adiv5_dap *dap)
  571. {
  572. int retval;
  573. LOG_DEBUG(" ");
  574. /* JTAG-DP or SWJ-DP, in JTAG mode
  575. * ... for SWD mode this is patched as part
  576. * of link switchover
  577. */
  578. if (!dap->ops)
  579. dap->ops = &jtag_dp_ops;
  580. /* Default MEM-AP setup.
  581. *
  582. * REVISIT AP #0 may be an inappropriate default for this.
  583. * Should we probe, or take a hint from the caller?
  584. * Presumably we can ignore the possibility of multiple APs.
  585. */
  586. dap->ap_current = !0;
  587. dap_ap_select(dap, 0);
  588. dap->last_read = NULL;
  589. /* DP initialization */
  590. dap->dp_bank_value = 0;
  591. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  592. if (retval != ERROR_OK)
  593. return retval;
  594. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, SSTICKYERR);
  595. if (retval != ERROR_OK)
  596. return retval;
  597. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  598. if (retval != ERROR_OK)
  599. return retval;
  600. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  601. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  602. if (retval != ERROR_OK)
  603. return retval;
  604. /* Check that we have debug power domains activated */
  605. LOG_DEBUG("DAP: wait CDBGPWRUPACK");
  606. retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
  607. CDBGPWRUPACK, CDBGPWRUPACK,
  608. DAP_POWER_DOMAIN_TIMEOUT);
  609. if (retval != ERROR_OK)
  610. return retval;
  611. LOG_DEBUG("DAP: wait CSYSPWRUPACK");
  612. retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
  613. CSYSPWRUPACK, CSYSPWRUPACK,
  614. DAP_POWER_DOMAIN_TIMEOUT);
  615. if (retval != ERROR_OK)
  616. return retval;
  617. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  618. if (retval != ERROR_OK)
  619. return retval;
  620. /* With debug power on we can activate OVERRUN checking */
  621. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
  622. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  623. if (retval != ERROR_OK)
  624. return retval;
  625. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  626. if (retval != ERROR_OK)
  627. return retval;
  628. /* check that we support packed transfers */
  629. uint32_t csw, cfg;
  630. retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_PACKED, 0);
  631. if (retval != ERROR_OK)
  632. return retval;
  633. retval = dap_queue_ap_read(dap, AP_REG_CSW, &csw);
  634. if (retval != ERROR_OK)
  635. return retval;
  636. retval = dap_queue_ap_read(dap, AP_REG_CFG, &cfg);
  637. if (retval != ERROR_OK)
  638. return retval;
  639. retval = dap_run(dap);
  640. if (retval != ERROR_OK)
  641. return retval;
  642. if (csw & CSW_ADDRINC_PACKED)
  643. dap->packed_transfers = true;
  644. else
  645. dap->packed_transfers = false;
  646. /* Packed transfers on TI BE-32 processors do not work correctly in
  647. * many cases. */
  648. if (dap->ti_be_32_quirks)
  649. dap->packed_transfers = false;
  650. LOG_DEBUG("MEM_AP Packed Transfers: %s",
  651. dap->packed_transfers ? "enabled" : "disabled");
  652. /* The ARM ADI spec leaves implementation-defined whether unaligned
  653. * memory accesses work, only work partially, or cause a sticky error.
  654. * On TI BE-32 processors, reads seem to return garbage in some bytes
  655. * and unaligned writes seem to cause a sticky error.
  656. * TODO: it would be nice to have a way to detect whether unaligned
  657. * operations are supported on other processors. */
  658. dap->unaligned_access_bad = dap->ti_be_32_quirks;
  659. LOG_DEBUG("MEM_AP CFG: large data %d, long address %d, big-endian %d",
  660. !!(cfg & 0x04), !!(cfg & 0x02), !!(cfg & 0x01));
  661. return ERROR_OK;
  662. }
  663. /* CID interpretation -- see ARM IHI 0029B section 3
  664. * and ARM IHI 0031A table 13-3.
  665. */
  666. static const char *class_description[16] = {
  667. "Reserved", "ROM table", "Reserved", "Reserved",
  668. "Reserved", "Reserved", "Reserved", "Reserved",
  669. "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
  670. "Reserved", "OptimoDE DESS",
  671. "Generic IP component", "PrimeCell or System component"
  672. };
  673. static bool is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
  674. {
  675. return cid3 == 0xb1 && cid2 == 0x05
  676. && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
  677. }
  678. /*
  679. * This function checks the ID for each access port to find the requested Access Port type
  680. */
  681. int dap_find_ap(struct adiv5_dap *dap, enum ap_type type_to_find, uint8_t *ap_num_out)
  682. {
  683. int ap;
  684. /* Maximum AP number is 255 since the SELECT register is 8 bits */
  685. for (ap = 0; ap <= 255; ap++) {
  686. /* read the IDR register of the Access Port */
  687. uint32_t id_val = 0;
  688. dap_ap_select(dap, ap);
  689. int retval = dap_queue_ap_read(dap, AP_REG_IDR, &id_val);
  690. if (retval != ERROR_OK)
  691. return retval;
  692. retval = dap_run(dap);
  693. /* IDR bits:
  694. * 31-28 : Revision
  695. * 27-24 : JEDEC bank (0x4 for ARM)
  696. * 23-17 : JEDEC code (0x3B for ARM)
  697. * 16 : Mem-AP
  698. * 15-8 : Reserved
  699. * 7-0 : AP Identity (1=AHB-AP 2=APB-AP 0x10=JTAG-AP)
  700. */
  701. /* Reading register for a non-existant AP should not cause an error,
  702. * but just to be sure, try to continue searching if an error does happen.
  703. */
  704. if ((retval == ERROR_OK) && /* Register read success */
  705. ((id_val & 0x0FFF0000) == 0x04770000) && /* Jedec codes match */
  706. ((id_val & 0xFF) == type_to_find)) { /* type matches*/
  707. LOG_DEBUG("Found %s at AP index: %d (IDR=0x%08" PRIX32 ")",
  708. (type_to_find == AP_TYPE_AHB_AP) ? "AHB-AP" :
  709. (type_to_find == AP_TYPE_APB_AP) ? "APB-AP" :
  710. (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown",
  711. ap, id_val);
  712. *ap_num_out = ap;
  713. return ERROR_OK;
  714. }
  715. }
  716. LOG_DEBUG("No %s found",
  717. (type_to_find == AP_TYPE_AHB_AP) ? "AHB-AP" :
  718. (type_to_find == AP_TYPE_APB_AP) ? "APB-AP" :
  719. (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown");
  720. return ERROR_FAIL;
  721. }
  722. int dap_get_debugbase(struct adiv5_dap *dap, int ap,
  723. uint32_t *dbgbase, uint32_t *apid)
  724. {
  725. uint32_t ap_old;
  726. int retval;
  727. /* AP address is in bits 31:24 of DP_SELECT */
  728. if (ap >= 256)
  729. return ERROR_COMMAND_SYNTAX_ERROR;
  730. ap_old = dap->ap_current;
  731. dap_ap_select(dap, ap);
  732. retval = dap_queue_ap_read(dap, AP_REG_BASE, dbgbase);
  733. if (retval != ERROR_OK)
  734. return retval;
  735. retval = dap_queue_ap_read(dap, AP_REG_IDR, apid);
  736. if (retval != ERROR_OK)
  737. return retval;
  738. retval = dap_run(dap);
  739. if (retval != ERROR_OK)
  740. return retval;
  741. dap_ap_select(dap, ap_old);
  742. return ERROR_OK;
  743. }
  744. int dap_lookup_cs_component(struct adiv5_dap *dap, int ap,
  745. uint32_t dbgbase, uint8_t type, uint32_t *addr, int32_t *idx)
  746. {
  747. uint32_t ap_old;
  748. uint32_t romentry, entry_offset = 0, component_base, devtype;
  749. int retval;
  750. if (ap >= 256)
  751. return ERROR_COMMAND_SYNTAX_ERROR;
  752. *addr = 0;
  753. ap_old = dap->ap_current;
  754. dap_ap_select(dap, ap);
  755. do {
  756. retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) |
  757. entry_offset, &romentry);
  758. if (retval != ERROR_OK)
  759. return retval;
  760. component_base = (dbgbase & 0xFFFFF000)
  761. + (romentry & 0xFFFFF000);
  762. if (romentry & 0x1) {
  763. uint32_t c_cid1;
  764. retval = mem_ap_read_atomic_u32(dap, component_base | 0xff4, &c_cid1);
  765. if (retval != ERROR_OK) {
  766. LOG_ERROR("Can't read component with base address 0x%" PRIx32
  767. ", the corresponding core might be turned off", component_base);
  768. return retval;
  769. }
  770. if (((c_cid1 >> 4) & 0x0f) == 1) {
  771. retval = dap_lookup_cs_component(dap, ap, component_base,
  772. type, addr, idx);
  773. if (retval == ERROR_OK)
  774. break;
  775. if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  776. return retval;
  777. }
  778. retval = mem_ap_read_atomic_u32(dap,
  779. (component_base & 0xfffff000) | 0xfcc,
  780. &devtype);
  781. if (retval != ERROR_OK)
  782. return retval;
  783. if ((devtype & 0xff) == type) {
  784. if (!*idx) {
  785. *addr = component_base;
  786. break;
  787. } else
  788. (*idx)--;
  789. }
  790. }
  791. entry_offset += 4;
  792. } while (romentry > 0);
  793. dap_ap_select(dap, ap_old);
  794. if (!*addr)
  795. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  796. return ERROR_OK;
  797. }
  798. static int dap_rom_display(struct command_context *cmd_ctx,
  799. struct adiv5_dap *dap, int ap, uint32_t dbgbase, int depth)
  800. {
  801. int retval;
  802. uint32_t cid0, cid1, cid2, cid3, memtype, romentry;
  803. uint16_t entry_offset;
  804. char tabs[7] = "";
  805. if (depth > 16) {
  806. command_print(cmd_ctx, "\tTables too deep");
  807. return ERROR_FAIL;
  808. }
  809. if (depth)
  810. snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
  811. /* bit 16 of apid indicates a memory access port */
  812. if (dbgbase & 0x02)
  813. command_print(cmd_ctx, "\t%sValid ROM table present", tabs);
  814. else
  815. command_print(cmd_ctx, "\t%sROM table in legacy format", tabs);
  816. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  817. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
  818. if (retval != ERROR_OK)
  819. return retval;
  820. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
  821. if (retval != ERROR_OK)
  822. return retval;
  823. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
  824. if (retval != ERROR_OK)
  825. return retval;
  826. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
  827. if (retval != ERROR_OK)
  828. return retval;
  829. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
  830. if (retval != ERROR_OK)
  831. return retval;
  832. retval = dap_run(dap);
  833. if (retval != ERROR_OK)
  834. return retval;
  835. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  836. command_print(cmd_ctx, "\t%sCID3 0x%02x"
  837. ", CID2 0x%02x"
  838. ", CID1 0x%02x"
  839. ", CID0 0x%02x",
  840. tabs,
  841. (unsigned)cid3, (unsigned)cid2,
  842. (unsigned)cid1, (unsigned)cid0);
  843. if (memtype & 0x01)
  844. command_print(cmd_ctx, "\t%sMEMTYPE system memory present on bus", tabs);
  845. else
  846. command_print(cmd_ctx, "\t%sMEMTYPE system memory not present: dedicated debug bus", tabs);
  847. /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
  848. for (entry_offset = 0; ; entry_offset += 4) {
  849. retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
  850. if (retval != ERROR_OK)
  851. return retval;
  852. command_print(cmd_ctx, "\t%sROMTABLE[0x%x] = 0x%" PRIx32 "",
  853. tabs, entry_offset, romentry);
  854. if (romentry & 0x01) {
  855. uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
  856. uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
  857. uint32_t component_base;
  858. unsigned part_num;
  859. char *type, *full;
  860. component_base = (dbgbase & 0xFFFFF000) + (romentry & 0xFFFFF000);
  861. /* IDs are in last 4K section */
  862. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE0, &c_pid0);
  863. if (retval != ERROR_OK) {
  864. command_print(cmd_ctx, "\t%s\tCan't read component with base address 0x%" PRIx32
  865. ", the corresponding core might be turned off", tabs, component_base);
  866. continue;
  867. }
  868. c_pid0 &= 0xff;
  869. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE4, &c_pid1);
  870. if (retval != ERROR_OK)
  871. return retval;
  872. c_pid1 &= 0xff;
  873. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE8, &c_pid2);
  874. if (retval != ERROR_OK)
  875. return retval;
  876. c_pid2 &= 0xff;
  877. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFEC, &c_pid3);
  878. if (retval != ERROR_OK)
  879. return retval;
  880. c_pid3 &= 0xff;
  881. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFD0, &c_pid4);
  882. if (retval != ERROR_OK)
  883. return retval;
  884. c_pid4 &= 0xff;
  885. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF0, &c_cid0);
  886. if (retval != ERROR_OK)
  887. return retval;
  888. c_cid0 &= 0xff;
  889. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF4, &c_cid1);
  890. if (retval != ERROR_OK)
  891. return retval;
  892. c_cid1 &= 0xff;
  893. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF8, &c_cid2);
  894. if (retval != ERROR_OK)
  895. return retval;
  896. c_cid2 &= 0xff;
  897. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFFC, &c_cid3);
  898. if (retval != ERROR_OK)
  899. return retval;
  900. c_cid3 &= 0xff;
  901. command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32 ", "
  902. "start address 0x%" PRIx32, component_base,
  903. /* component may take multiple 4K pages */
  904. (uint32_t)(component_base - 0x1000*(c_pid4 >> 4)));
  905. command_print(cmd_ctx, "\t\tComponent class is 0x%" PRIx8 ", %s",
  906. (uint8_t)((c_cid1 >> 4) & 0xf),
  907. /* See ARM IHI 0029B Table 3-3 */
  908. class_description[(c_cid1 >> 4) & 0xf]);
  909. /* CoreSight component? */
  910. if (((c_cid1 >> 4) & 0x0f) == 9) {
  911. uint32_t devtype;
  912. unsigned minor;
  913. char *major = "Reserved", *subtype = "Reserved";
  914. retval = mem_ap_read_atomic_u32(dap,
  915. (component_base & 0xfffff000) | 0xfcc,
  916. &devtype);
  917. if (retval != ERROR_OK)
  918. return retval;
  919. minor = (devtype >> 4) & 0x0f;
  920. switch (devtype & 0x0f) {
  921. case 0:
  922. major = "Miscellaneous";
  923. switch (minor) {
  924. case 0:
  925. subtype = "other";
  926. break;
  927. case 4:
  928. subtype = "Validation component";
  929. break;
  930. }
  931. break;
  932. case 1:
  933. major = "Trace Sink";
  934. switch (minor) {
  935. case 0:
  936. subtype = "other";
  937. break;
  938. case 1:
  939. subtype = "Port";
  940. break;
  941. case 2:
  942. subtype = "Buffer";
  943. break;
  944. case 3:
  945. subtype = "Router";
  946. break;
  947. }
  948. break;
  949. case 2:
  950. major = "Trace Link";
  951. switch (minor) {
  952. case 0:
  953. subtype = "other";
  954. break;
  955. case 1:
  956. subtype = "Funnel, router";
  957. break;
  958. case 2:
  959. subtype = "Filter";
  960. break;
  961. case 3:
  962. subtype = "FIFO, buffer";
  963. break;
  964. }
  965. break;
  966. case 3:
  967. major = "Trace Source";
  968. switch (minor) {
  969. case 0:
  970. subtype = "other";
  971. break;
  972. case 1:
  973. subtype = "Processor";
  974. break;
  975. case 2:
  976. subtype = "DSP";
  977. break;
  978. case 3:
  979. subtype = "Engine/Coprocessor";
  980. break;
  981. case 4:
  982. subtype = "Bus";
  983. break;
  984. case 6:
  985. subtype = "Software";
  986. break;
  987. }
  988. break;
  989. case 4:
  990. major = "Debug Control";
  991. switch (minor) {
  992. case 0:
  993. subtype = "other";
  994. break;
  995. case 1:
  996. subtype = "Trigger Matrix";
  997. break;
  998. case 2:
  999. subtype = "Debug Auth";
  1000. break;
  1001. case 3:
  1002. subtype = "Power Requestor";
  1003. break;
  1004. }
  1005. break;
  1006. case 5:
  1007. major = "Debug Logic";
  1008. switch (minor) {
  1009. case 0:
  1010. subtype = "other";
  1011. break;
  1012. case 1:
  1013. subtype = "Processor";
  1014. break;
  1015. case 2:
  1016. subtype = "DSP";
  1017. break;
  1018. case 3:
  1019. subtype = "Engine/Coprocessor";
  1020. break;
  1021. case 4:
  1022. subtype = "Bus";
  1023. break;
  1024. case 5:
  1025. subtype = "Memory";
  1026. break;
  1027. }
  1028. break;
  1029. case 6:
  1030. major = "Perfomance Monitor";
  1031. switch (minor) {
  1032. case 0:
  1033. subtype = "other";
  1034. break;
  1035. case 1:
  1036. subtype = "Processor";
  1037. break;
  1038. case 2:
  1039. subtype = "DSP";
  1040. break;
  1041. case 3:
  1042. subtype = "Engine/Coprocessor";
  1043. break;
  1044. case 4:
  1045. subtype = "Bus";
  1046. break;
  1047. case 5:
  1048. subtype = "Memory";
  1049. break;
  1050. }
  1051. break;
  1052. }
  1053. command_print(cmd_ctx, "\t\tType is 0x%02" PRIx8 ", %s, %s",
  1054. (uint8_t)(devtype & 0xff),
  1055. major, subtype);
  1056. /* REVISIT also show 0xfc8 DevId */
  1057. }
  1058. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  1059. command_print(cmd_ctx,
  1060. "\t\tCID3 0%02x"
  1061. ", CID2 0%02x"
  1062. ", CID1 0%02x"
  1063. ", CID0 0%02x",
  1064. (int)c_cid3,
  1065. (int)c_cid2,
  1066. (int)c_cid1,
  1067. (int)c_cid0);
  1068. command_print(cmd_ctx,
  1069. "\t\tPeripheral ID[4..0] = hex "
  1070. "%02x %02x %02x %02x %02x",
  1071. (int)c_pid4, (int)c_pid3, (int)c_pid2,
  1072. (int)c_pid1, (int)c_pid0);
  1073. /* Part number interpretations are from Cortex
  1074. * core specs, the CoreSight components TRM
  1075. * (ARM DDI 0314H), CoreSight System Design
  1076. * Guide (ARM DGI 0012D) and ETM specs; also
  1077. * from chip observation (e.g. TI SDTI).
  1078. */
  1079. part_num = (c_pid0 & 0xff);
  1080. part_num |= (c_pid1 & 0x0f) << 8;
  1081. switch (part_num) {
  1082. case 0x000:
  1083. type = "Cortex-M3 NVIC";
  1084. full = "(Interrupt Controller)";
  1085. break;
  1086. case 0x001:
  1087. type = "Cortex-M3 ITM";
  1088. full = "(Instrumentation Trace Module)";
  1089. break;
  1090. case 0x002:
  1091. type = "Cortex-M3 DWT";
  1092. full = "(Data Watchpoint and Trace)";
  1093. break;
  1094. case 0x003:
  1095. type = "Cortex-M3 FBP";
  1096. full = "(Flash Patch and Breakpoint)";
  1097. break;
  1098. case 0x00c:
  1099. type = "Cortex-M4 SCS";
  1100. full = "(System Control Space)";
  1101. break;
  1102. case 0x00d:
  1103. type = "CoreSight ETM11";
  1104. full = "(Embedded Trace)";
  1105. break;
  1106. /* case 0x113: what? */
  1107. case 0x120: /* from OMAP3 memmap */
  1108. type = "TI SDTI";
  1109. full = "(System Debug Trace Interface)";
  1110. break;
  1111. case 0x343: /* from OMAP3 memmap */
  1112. type = "TI DAPCTL";
  1113. full = "";
  1114. break;
  1115. case 0x906:
  1116. type = "Coresight CTI";
  1117. full = "(Cross Trigger)";
  1118. break;
  1119. case 0x907:
  1120. type = "Coresight ETB";
  1121. full = "(Trace Buffer)";
  1122. break;
  1123. case 0x908:
  1124. type = "Coresight CSTF";
  1125. full = "(Trace Funnel)";
  1126. break;
  1127. case 0x910:
  1128. type = "CoreSight ETM9";
  1129. full = "(Embedded Trace)";
  1130. break;
  1131. case 0x912:
  1132. type = "Coresight TPIU";
  1133. full = "(Trace Port Interface Unit)";
  1134. break;
  1135. case 0x913:
  1136. type = "Coresight ITM";
  1137. full = "(Instrumentation Trace Macrocell)";
  1138. break;
  1139. case 0x917:
  1140. type = "Coresight HTM";
  1141. full = "(AHB Trace Macrocell)";
  1142. break;
  1143. case 0x920:
  1144. type = "CoreSight ETM11";
  1145. full = "(Embedded Trace)";
  1146. break;
  1147. case 0x921:
  1148. type = "Cortex-A8 ETM";
  1149. full = "(Embedded Trace)";
  1150. break;
  1151. case 0x922:
  1152. type = "Cortex-A8 CTI";
  1153. full = "(Cross Trigger)";
  1154. break;
  1155. case 0x923:
  1156. type = "Cortex-M3 TPIU";
  1157. full = "(Trace Port Interface Unit)";
  1158. break;
  1159. case 0x924:
  1160. type = "Cortex-M3 ETM";
  1161. full = "(Embedded Trace)";
  1162. break;
  1163. case 0x925:
  1164. type = "Cortex-M4 ETM";
  1165. full = "(Embedded Trace)";
  1166. break;
  1167. case 0x930:
  1168. type = "Cortex-R4 ETM";
  1169. full = "(Embedded Trace)";
  1170. break;
  1171. case 0x950:
  1172. type = "CoreSight Component";
  1173. full = "(unidentified Cortex-A9 component)";
  1174. break;
  1175. case 0x962:
  1176. type = "CoreSight STM";
  1177. full = "(System Trace Macrocell)";
  1178. break;
  1179. case 0x9a0:
  1180. type = "CoreSight PMU";
  1181. full = "(Performance Monitoring Unit)";
  1182. break;
  1183. case 0x9a1:
  1184. type = "Cortex-M4 TPUI";
  1185. full = "(Trace Port Interface Unit)";
  1186. break;
  1187. case 0xc08:
  1188. type = "Cortex-A8 Debug";
  1189. full = "(Debug Unit)";
  1190. break;
  1191. case 0xc09:
  1192. type = "Cortex-A9 Debug";
  1193. full = "(Debug Unit)";
  1194. break;
  1195. default:
  1196. type = "-*- unrecognized -*-";
  1197. full = "";
  1198. break;
  1199. }
  1200. command_print(cmd_ctx, "\t\tPart is %s %s",
  1201. type, full);
  1202. /* ROM Table? */
  1203. if (((c_cid1 >> 4) & 0x0f) == 1) {
  1204. retval = dap_rom_display(cmd_ctx, dap, ap, component_base, depth + 1);
  1205. if (retval != ERROR_OK)
  1206. return retval;
  1207. }
  1208. } else {
  1209. if (romentry)
  1210. command_print(cmd_ctx, "\t\tComponent not present");
  1211. else
  1212. break;
  1213. }
  1214. }
  1215. command_print(cmd_ctx, "\t%s\tEnd of ROM table", tabs);
  1216. return ERROR_OK;
  1217. }
  1218. static int dap_info_command(struct command_context *cmd_ctx,
  1219. struct adiv5_dap *dap, int ap)
  1220. {
  1221. int retval;
  1222. uint32_t dbgbase, apid;
  1223. int romtable_present = 0;
  1224. uint8_t mem_ap;
  1225. uint32_t ap_old;
  1226. retval = dap_get_debugbase(dap, ap, &dbgbase, &apid);
  1227. if (retval != ERROR_OK)
  1228. return retval;
  1229. ap_old = dap->ap_current;
  1230. dap_ap_select(dap, ap);
  1231. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  1232. mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
  1233. command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
  1234. if (apid) {
  1235. switch (apid&0x0F) {
  1236. case 0:
  1237. command_print(cmd_ctx, "\tType is JTAG-AP");
  1238. break;
  1239. case 1:
  1240. command_print(cmd_ctx, "\tType is MEM-AP AHB");
  1241. break;
  1242. case 2:
  1243. command_print(cmd_ctx, "\tType is MEM-AP APB");
  1244. break;
  1245. default:
  1246. command_print(cmd_ctx, "\tUnknown AP type");
  1247. break;
  1248. }
  1249. /* NOTE: a MEM-AP may have a single CoreSight component that's
  1250. * not a ROM table ... or have no such components at all.
  1251. */
  1252. if (mem_ap)
  1253. command_print(cmd_ctx, "AP BASE 0x%8.8" PRIx32, dbgbase);
  1254. } else
  1255. command_print(cmd_ctx, "No AP found at this ap 0x%x", ap);
  1256. romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
  1257. if (romtable_present) {
  1258. dap_rom_display(cmd_ctx, dap, ap, dbgbase, 0);
  1259. } else
  1260. command_print(cmd_ctx, "\tNo ROM table present");
  1261. dap_ap_select(dap, ap_old);
  1262. return ERROR_OK;
  1263. }
  1264. COMMAND_HANDLER(handle_dap_info_command)
  1265. {
  1266. struct target *target = get_current_target(CMD_CTX);
  1267. struct arm *arm = target_to_arm(target);
  1268. struct adiv5_dap *dap = arm->dap;
  1269. uint32_t apsel;
  1270. switch (CMD_ARGC) {
  1271. case 0:
  1272. apsel = dap->apsel;
  1273. break;
  1274. case 1:
  1275. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1276. break;
  1277. default:
  1278. return ERROR_COMMAND_SYNTAX_ERROR;
  1279. }
  1280. return dap_info_command(CMD_CTX, dap, apsel);
  1281. }
  1282. COMMAND_HANDLER(dap_baseaddr_command)
  1283. {
  1284. struct target *target = get_current_target(CMD_CTX);
  1285. struct arm *arm = target_to_arm(target);
  1286. struct adiv5_dap *dap = arm->dap;
  1287. uint32_t apsel, baseaddr;
  1288. int retval;
  1289. switch (CMD_ARGC) {
  1290. case 0:
  1291. apsel = dap->apsel;
  1292. break;
  1293. case 1:
  1294. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1295. /* AP address is in bits 31:24 of DP_SELECT */
  1296. if (apsel >= 256)
  1297. return ERROR_COMMAND_SYNTAX_ERROR;
  1298. break;
  1299. default:
  1300. return ERROR_COMMAND_SYNTAX_ERROR;
  1301. }
  1302. dap_ap_select(dap, apsel);
  1303. /* NOTE: assumes we're talking to a MEM-AP, which
  1304. * has a base address. There are other kinds of AP,
  1305. * though they're not common for now. This should
  1306. * use the ID register to verify it's a MEM-AP.
  1307. */
  1308. retval = dap_queue_ap_read(dap, AP_REG_BASE, &baseaddr);
  1309. if (retval != ERROR_OK)
  1310. return retval;
  1311. retval = dap_run(dap);
  1312. if (retval != ERROR_OK)
  1313. return retval;
  1314. command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
  1315. return retval;
  1316. }
  1317. COMMAND_HANDLER(dap_memaccess_command)
  1318. {
  1319. struct target *target = get_current_target(CMD_CTX);
  1320. struct arm *arm = target_to_arm(target);
  1321. struct adiv5_dap *dap = arm->dap;
  1322. uint32_t memaccess_tck;
  1323. switch (CMD_ARGC) {
  1324. case 0:
  1325. memaccess_tck = dap->memaccess_tck;
  1326. break;
  1327. case 1:
  1328. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
  1329. break;
  1330. default:
  1331. return ERROR_COMMAND_SYNTAX_ERROR;
  1332. }
  1333. dap->memaccess_tck = memaccess_tck;
  1334. command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
  1335. dap->memaccess_tck);
  1336. return ERROR_OK;
  1337. }
  1338. COMMAND_HANDLER(dap_apsel_command)
  1339. {
  1340. struct target *target = get_current_target(CMD_CTX);
  1341. struct arm *arm = target_to_arm(target);
  1342. struct adiv5_dap *dap = arm->dap;
  1343. uint32_t apsel, apid;
  1344. int retval;
  1345. switch (CMD_ARGC) {
  1346. case 0:
  1347. apsel = 0;
  1348. break;
  1349. case 1:
  1350. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1351. /* AP address is in bits 31:24 of DP_SELECT */
  1352. if (apsel >= 256)
  1353. return ERROR_COMMAND_SYNTAX_ERROR;
  1354. break;
  1355. default:
  1356. return ERROR_COMMAND_SYNTAX_ERROR;
  1357. }
  1358. dap->apsel = apsel;
  1359. dap_ap_select(dap, apsel);
  1360. retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
  1361. if (retval != ERROR_OK)
  1362. return retval;
  1363. retval = dap_run(dap);
  1364. if (retval != ERROR_OK)
  1365. return retval;
  1366. command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
  1367. apsel, apid);
  1368. return retval;
  1369. }
  1370. COMMAND_HANDLER(dap_apcsw_command)
  1371. {
  1372. struct target *target = get_current_target(CMD_CTX);
  1373. struct arm *arm = target_to_arm(target);
  1374. struct adiv5_dap *dap = arm->dap;
  1375. uint32_t apcsw = dap->apcsw[dap->apsel], sprot = 0;
  1376. switch (CMD_ARGC) {
  1377. case 0:
  1378. command_print(CMD_CTX, "apsel %" PRIi32 " selected, csw 0x%8.8" PRIx32,
  1379. (dap->apsel), apcsw);
  1380. break;
  1381. case 1:
  1382. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], sprot);
  1383. /* AP address is in bits 31:24 of DP_SELECT */
  1384. if (sprot > 1)
  1385. return ERROR_COMMAND_SYNTAX_ERROR;
  1386. if (sprot)
  1387. apcsw |= CSW_SPROT;
  1388. else
  1389. apcsw &= ~CSW_SPROT;
  1390. break;
  1391. default:
  1392. return ERROR_COMMAND_SYNTAX_ERROR;
  1393. }
  1394. dap->apcsw[dap->apsel] = apcsw;
  1395. return 0;
  1396. }
  1397. COMMAND_HANDLER(dap_apid_command)
  1398. {
  1399. struct target *target = get_current_target(CMD_CTX);
  1400. struct arm *arm = target_to_arm(target);
  1401. struct adiv5_dap *dap = arm->dap;
  1402. uint32_t apsel, apid;
  1403. int retval;
  1404. switch (CMD_ARGC) {
  1405. case 0:
  1406. apsel = dap->apsel;
  1407. break;
  1408. case 1:
  1409. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1410. /* AP address is in bits 31:24 of DP_SELECT */
  1411. if (apsel >= 256)
  1412. return ERROR_COMMAND_SYNTAX_ERROR;
  1413. break;
  1414. default:
  1415. return ERROR_COMMAND_SYNTAX_ERROR;
  1416. }
  1417. dap_ap_select(dap, apsel);
  1418. retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
  1419. if (retval != ERROR_OK)
  1420. return retval;
  1421. retval = dap_run(dap);
  1422. if (retval != ERROR_OK)
  1423. return retval;
  1424. command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
  1425. return retval;
  1426. }
  1427. COMMAND_HANDLER(dap_ti_be_32_quirks_command)
  1428. {
  1429. struct target *target = get_current_target(CMD_CTX);
  1430. struct arm *arm = target_to_arm(target);
  1431. struct adiv5_dap *dap = arm->dap;
  1432. uint32_t enable = dap->ti_be_32_quirks;
  1433. switch (CMD_ARGC) {
  1434. case 0:
  1435. break;
  1436. case 1:
  1437. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], enable);
  1438. if (enable > 1)
  1439. return ERROR_COMMAND_SYNTAX_ERROR;
  1440. break;
  1441. default:
  1442. return ERROR_COMMAND_SYNTAX_ERROR;
  1443. }
  1444. dap->ti_be_32_quirks = enable;
  1445. command_print(CMD_CTX, "TI BE-32 quirks mode %s",
  1446. enable ? "enabled" : "disabled");
  1447. return 0;
  1448. }
  1449. static const struct command_registration dap_commands[] = {
  1450. {
  1451. .name = "info",
  1452. .handler = handle_dap_info_command,
  1453. .mode = COMMAND_EXEC,
  1454. .help = "display ROM table for MEM-AP "
  1455. "(default currently selected AP)",
  1456. .usage = "[ap_num]",
  1457. },
  1458. {
  1459. .name = "apsel",
  1460. .handler = dap_apsel_command,
  1461. .mode = COMMAND_EXEC,
  1462. .help = "Set the currently selected AP (default 0) "
  1463. "and display the result",
  1464. .usage = "[ap_num]",
  1465. },
  1466. {
  1467. .name = "apcsw",
  1468. .handler = dap_apcsw_command,
  1469. .mode = COMMAND_EXEC,
  1470. .help = "Set csw access bit ",
  1471. .usage = "[sprot]",
  1472. },
  1473. {
  1474. .name = "apid",
  1475. .handler = dap_apid_command,
  1476. .mode = COMMAND_EXEC,
  1477. .help = "return ID register from AP "
  1478. "(default currently selected AP)",
  1479. .usage = "[ap_num]",
  1480. },
  1481. {
  1482. .name = "baseaddr",
  1483. .handler = dap_baseaddr_command,
  1484. .mode = COMMAND_EXEC,
  1485. .help = "return debug base address from MEM-AP "
  1486. "(default currently selected AP)",
  1487. .usage = "[ap_num]",
  1488. },
  1489. {
  1490. .name = "memaccess",
  1491. .handler = dap_memaccess_command,
  1492. .mode = COMMAND_EXEC,
  1493. .help = "set/get number of extra tck for MEM-AP memory "
  1494. "bus access [0-255]",
  1495. .usage = "[cycles]",
  1496. },
  1497. {
  1498. .name = "ti_be_32_quirks",
  1499. .handler = dap_ti_be_32_quirks_command,
  1500. .mode = COMMAND_CONFIG,
  1501. .help = "set/get quirks mode for TI TMS450/TMS570 processors",
  1502. .usage = "[enable]",
  1503. },
  1504. COMMAND_REGISTRATION_DONE
  1505. };
  1506. const struct command_registration dap_command_handlers[] = {
  1507. {
  1508. .name = "dap",
  1509. .mode = COMMAND_EXEC,
  1510. .help = "DAP command group",
  1511. .usage = "",
  1512. .chain = dap_commands,
  1513. },
  1514. COMMAND_REGISTRATION_DONE
  1515. };