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.
 
 
 
 
 
 

1744 lines
49 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. /* check that we support packed transfers */
  573. uint32_t csw, cfg;
  574. int retval;
  575. LOG_DEBUG(" ");
  576. /* JTAG-DP or SWJ-DP, in JTAG mode
  577. * ... for SWD mode this is patched as part
  578. * of link switchover
  579. */
  580. if (!dap->ops)
  581. dap->ops = &jtag_dp_ops;
  582. /* Default MEM-AP setup.
  583. *
  584. * REVISIT AP #0 may be an inappropriate default for this.
  585. * Should we probe, or take a hint from the caller?
  586. * Presumably we can ignore the possibility of multiple APs.
  587. */
  588. dap->ap_current = !0;
  589. dap_ap_select(dap, 0);
  590. dap->last_read = NULL;
  591. for (size_t i = 0; i < 10; i++) {
  592. /* DP initialization */
  593. dap->dp_bank_value = 0;
  594. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  595. if (retval != ERROR_OK)
  596. continue;
  597. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, SSTICKYERR);
  598. if (retval != ERROR_OK)
  599. continue;
  600. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  601. if (retval != ERROR_OK)
  602. continue;
  603. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  604. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  605. if (retval != ERROR_OK)
  606. continue;
  607. /* Check that we have debug power domains activated */
  608. LOG_DEBUG("DAP: wait CDBGPWRUPACK");
  609. retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
  610. CDBGPWRUPACK, CDBGPWRUPACK,
  611. DAP_POWER_DOMAIN_TIMEOUT);
  612. if (retval != ERROR_OK)
  613. continue;
  614. LOG_DEBUG("DAP: wait CSYSPWRUPACK");
  615. retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
  616. CSYSPWRUPACK, CSYSPWRUPACK,
  617. DAP_POWER_DOMAIN_TIMEOUT);
  618. if (retval != ERROR_OK)
  619. continue;
  620. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  621. if (retval != ERROR_OK)
  622. continue;
  623. /* With debug power on we can activate OVERRUN checking */
  624. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
  625. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  626. if (retval != ERROR_OK)
  627. continue;
  628. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  629. if (retval != ERROR_OK)
  630. continue;
  631. retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_PACKED, 0);
  632. if (retval != ERROR_OK)
  633. continue;
  634. retval = dap_queue_ap_read(dap, AP_REG_CSW, &csw);
  635. if (retval != ERROR_OK)
  636. continue;
  637. retval = dap_queue_ap_read(dap, AP_REG_CFG, &cfg);
  638. if (retval != ERROR_OK)
  639. continue;
  640. retval = dap_run(dap);
  641. if (retval != ERROR_OK)
  642. continue;
  643. break;
  644. }
  645. if (retval != ERROR_OK)
  646. return retval;
  647. if (csw & CSW_ADDRINC_PACKED)
  648. dap->packed_transfers = true;
  649. else
  650. dap->packed_transfers = false;
  651. /* Packed transfers on TI BE-32 processors do not work correctly in
  652. * many cases. */
  653. if (dap->ti_be_32_quirks)
  654. dap->packed_transfers = false;
  655. LOG_DEBUG("MEM_AP Packed Transfers: %s",
  656. dap->packed_transfers ? "enabled" : "disabled");
  657. /* The ARM ADI spec leaves implementation-defined whether unaligned
  658. * memory accesses work, only work partially, or cause a sticky error.
  659. * On TI BE-32 processors, reads seem to return garbage in some bytes
  660. * and unaligned writes seem to cause a sticky error.
  661. * TODO: it would be nice to have a way to detect whether unaligned
  662. * operations are supported on other processors. */
  663. dap->unaligned_access_bad = dap->ti_be_32_quirks;
  664. LOG_DEBUG("MEM_AP CFG: large data %d, long address %d, big-endian %d",
  665. !!(cfg & 0x04), !!(cfg & 0x02), !!(cfg & 0x01));
  666. return ERROR_OK;
  667. }
  668. /* CID interpretation -- see ARM IHI 0029B section 3
  669. * and ARM IHI 0031A table 13-3.
  670. */
  671. static const char *class_description[16] = {
  672. "Reserved", "ROM table", "Reserved", "Reserved",
  673. "Reserved", "Reserved", "Reserved", "Reserved",
  674. "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
  675. "Reserved", "OptimoDE DESS",
  676. "Generic IP component", "PrimeCell or System component"
  677. };
  678. static bool is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
  679. {
  680. return cid3 == 0xb1 && cid2 == 0x05
  681. && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
  682. }
  683. /*
  684. * This function checks the ID for each access port to find the requested Access Port type
  685. */
  686. int dap_find_ap(struct adiv5_dap *dap, enum ap_type type_to_find, uint8_t *ap_num_out)
  687. {
  688. int ap;
  689. /* Maximum AP number is 255 since the SELECT register is 8 bits */
  690. for (ap = 0; ap <= 255; ap++) {
  691. /* read the IDR register of the Access Port */
  692. uint32_t id_val = 0;
  693. dap_ap_select(dap, ap);
  694. int retval = dap_queue_ap_read(dap, AP_REG_IDR, &id_val);
  695. if (retval != ERROR_OK)
  696. return retval;
  697. retval = dap_run(dap);
  698. /* IDR bits:
  699. * 31-28 : Revision
  700. * 27-24 : JEDEC bank (0x4 for ARM)
  701. * 23-17 : JEDEC code (0x3B for ARM)
  702. * 16 : Mem-AP
  703. * 15-8 : Reserved
  704. * 7-0 : AP Identity (1=AHB-AP 2=APB-AP 0x10=JTAG-AP)
  705. */
  706. /* Reading register for a non-existant AP should not cause an error,
  707. * but just to be sure, try to continue searching if an error does happen.
  708. */
  709. if ((retval == ERROR_OK) && /* Register read success */
  710. ((id_val & 0x0FFF0000) == 0x04770000) && /* Jedec codes match */
  711. ((id_val & 0xFF) == type_to_find)) { /* type matches*/
  712. LOG_DEBUG("Found %s at AP index: %d (IDR=0x%08" PRIX32 ")",
  713. (type_to_find == AP_TYPE_AHB_AP) ? "AHB-AP" :
  714. (type_to_find == AP_TYPE_APB_AP) ? "APB-AP" :
  715. (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown",
  716. ap, id_val);
  717. *ap_num_out = ap;
  718. return ERROR_OK;
  719. }
  720. }
  721. LOG_DEBUG("No %s found",
  722. (type_to_find == AP_TYPE_AHB_AP) ? "AHB-AP" :
  723. (type_to_find == AP_TYPE_APB_AP) ? "APB-AP" :
  724. (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown");
  725. return ERROR_FAIL;
  726. }
  727. int dap_get_debugbase(struct adiv5_dap *dap, int ap,
  728. uint32_t *dbgbase, uint32_t *apid)
  729. {
  730. uint32_t ap_old;
  731. int retval;
  732. /* AP address is in bits 31:24 of DP_SELECT */
  733. if (ap >= 256)
  734. return ERROR_COMMAND_SYNTAX_ERROR;
  735. ap_old = dap->ap_current;
  736. dap_ap_select(dap, ap);
  737. retval = dap_queue_ap_read(dap, AP_REG_BASE, dbgbase);
  738. if (retval != ERROR_OK)
  739. return retval;
  740. retval = dap_queue_ap_read(dap, AP_REG_IDR, apid);
  741. if (retval != ERROR_OK)
  742. return retval;
  743. retval = dap_run(dap);
  744. if (retval != ERROR_OK)
  745. return retval;
  746. dap_ap_select(dap, ap_old);
  747. return ERROR_OK;
  748. }
  749. int dap_lookup_cs_component(struct adiv5_dap *dap, int ap,
  750. uint32_t dbgbase, uint8_t type, uint32_t *addr, int32_t *idx)
  751. {
  752. uint32_t ap_old;
  753. uint32_t romentry, entry_offset = 0, component_base, devtype;
  754. int retval;
  755. if (ap >= 256)
  756. return ERROR_COMMAND_SYNTAX_ERROR;
  757. *addr = 0;
  758. ap_old = dap->ap_current;
  759. dap_ap_select(dap, ap);
  760. do {
  761. retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) |
  762. entry_offset, &romentry);
  763. if (retval != ERROR_OK)
  764. return retval;
  765. component_base = (dbgbase & 0xFFFFF000)
  766. + (romentry & 0xFFFFF000);
  767. if (romentry & 0x1) {
  768. uint32_t c_cid1;
  769. retval = mem_ap_read_atomic_u32(dap, component_base | 0xff4, &c_cid1);
  770. if (retval != ERROR_OK) {
  771. LOG_ERROR("Can't read component with base address 0x%" PRIx32
  772. ", the corresponding core might be turned off", component_base);
  773. return retval;
  774. }
  775. if (((c_cid1 >> 4) & 0x0f) == 1) {
  776. retval = dap_lookup_cs_component(dap, ap, component_base,
  777. type, addr, idx);
  778. if (retval == ERROR_OK)
  779. break;
  780. if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  781. return retval;
  782. }
  783. retval = mem_ap_read_atomic_u32(dap,
  784. (component_base & 0xfffff000) | 0xfcc,
  785. &devtype);
  786. if (retval != ERROR_OK)
  787. return retval;
  788. if ((devtype & 0xff) == type) {
  789. if (!*idx) {
  790. *addr = component_base;
  791. break;
  792. } else
  793. (*idx)--;
  794. }
  795. }
  796. entry_offset += 4;
  797. } while (romentry > 0);
  798. dap_ap_select(dap, ap_old);
  799. if (!*addr)
  800. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  801. return ERROR_OK;
  802. }
  803. static int dap_rom_display(struct command_context *cmd_ctx,
  804. struct adiv5_dap *dap, int ap, uint32_t dbgbase, int depth)
  805. {
  806. int retval;
  807. uint32_t cid0, cid1, cid2, cid3, memtype, romentry;
  808. uint16_t entry_offset;
  809. char tabs[7] = "";
  810. if (depth > 16) {
  811. command_print(cmd_ctx, "\tTables too deep");
  812. return ERROR_FAIL;
  813. }
  814. if (depth)
  815. snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
  816. /* bit 16 of apid indicates a memory access port */
  817. if (dbgbase & 0x02)
  818. command_print(cmd_ctx, "\t%sValid ROM table present", tabs);
  819. else
  820. command_print(cmd_ctx, "\t%sROM table in legacy format", tabs);
  821. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  822. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
  823. if (retval != ERROR_OK)
  824. return retval;
  825. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
  826. if (retval != ERROR_OK)
  827. return retval;
  828. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
  829. if (retval != ERROR_OK)
  830. return retval;
  831. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
  832. if (retval != ERROR_OK)
  833. return retval;
  834. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
  835. if (retval != ERROR_OK)
  836. return retval;
  837. retval = dap_run(dap);
  838. if (retval != ERROR_OK)
  839. return retval;
  840. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  841. command_print(cmd_ctx, "\t%sCID3 0x%02x"
  842. ", CID2 0x%02x"
  843. ", CID1 0x%02x"
  844. ", CID0 0x%02x",
  845. tabs,
  846. (unsigned)cid3, (unsigned)cid2,
  847. (unsigned)cid1, (unsigned)cid0);
  848. if (memtype & 0x01)
  849. command_print(cmd_ctx, "\t%sMEMTYPE system memory present on bus", tabs);
  850. else
  851. command_print(cmd_ctx, "\t%sMEMTYPE system memory not present: dedicated debug bus", tabs);
  852. /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
  853. for (entry_offset = 0; ; entry_offset += 4) {
  854. retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
  855. if (retval != ERROR_OK)
  856. return retval;
  857. command_print(cmd_ctx, "\t%sROMTABLE[0x%x] = 0x%" PRIx32 "",
  858. tabs, entry_offset, romentry);
  859. if (romentry & 0x01) {
  860. uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
  861. uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
  862. uint32_t component_base;
  863. uint32_t part_num;
  864. const char *type, *full;
  865. component_base = (dbgbase & 0xFFFFF000) + (romentry & 0xFFFFF000);
  866. /* IDs are in last 4K section */
  867. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE0, &c_pid0);
  868. if (retval != ERROR_OK) {
  869. command_print(cmd_ctx, "\t%s\tCan't read component with base address 0x%" PRIx32
  870. ", the corresponding core might be turned off", tabs, component_base);
  871. continue;
  872. }
  873. c_pid0 &= 0xff;
  874. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE4, &c_pid1);
  875. if (retval != ERROR_OK)
  876. return retval;
  877. c_pid1 &= 0xff;
  878. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE8, &c_pid2);
  879. if (retval != ERROR_OK)
  880. return retval;
  881. c_pid2 &= 0xff;
  882. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFEC, &c_pid3);
  883. if (retval != ERROR_OK)
  884. return retval;
  885. c_pid3 &= 0xff;
  886. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFD0, &c_pid4);
  887. if (retval != ERROR_OK)
  888. return retval;
  889. c_pid4 &= 0xff;
  890. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF0, &c_cid0);
  891. if (retval != ERROR_OK)
  892. return retval;
  893. c_cid0 &= 0xff;
  894. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF4, &c_cid1);
  895. if (retval != ERROR_OK)
  896. return retval;
  897. c_cid1 &= 0xff;
  898. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF8, &c_cid2);
  899. if (retval != ERROR_OK)
  900. return retval;
  901. c_cid2 &= 0xff;
  902. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFFC, &c_cid3);
  903. if (retval != ERROR_OK)
  904. return retval;
  905. c_cid3 &= 0xff;
  906. command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32 ", "
  907. "start address 0x%" PRIx32, component_base,
  908. /* component may take multiple 4K pages */
  909. (uint32_t)(component_base - 0x1000*(c_pid4 >> 4)));
  910. command_print(cmd_ctx, "\t\tComponent class is 0x%" PRIx8 ", %s",
  911. (uint8_t)((c_cid1 >> 4) & 0xf),
  912. /* See ARM IHI 0029B Table 3-3 */
  913. class_description[(c_cid1 >> 4) & 0xf]);
  914. /* CoreSight component? */
  915. if (((c_cid1 >> 4) & 0x0f) == 9) {
  916. uint32_t devtype;
  917. unsigned minor;
  918. const char *major = "Reserved", *subtype = "Reserved";
  919. retval = mem_ap_read_atomic_u32(dap,
  920. (component_base & 0xfffff000) | 0xfcc,
  921. &devtype);
  922. if (retval != ERROR_OK)
  923. return retval;
  924. minor = (devtype >> 4) & 0x0f;
  925. switch (devtype & 0x0f) {
  926. case 0:
  927. major = "Miscellaneous";
  928. switch (minor) {
  929. case 0:
  930. subtype = "other";
  931. break;
  932. case 4:
  933. subtype = "Validation component";
  934. break;
  935. }
  936. break;
  937. case 1:
  938. major = "Trace Sink";
  939. switch (minor) {
  940. case 0:
  941. subtype = "other";
  942. break;
  943. case 1:
  944. subtype = "Port";
  945. break;
  946. case 2:
  947. subtype = "Buffer";
  948. break;
  949. case 3:
  950. subtype = "Router";
  951. break;
  952. }
  953. break;
  954. case 2:
  955. major = "Trace Link";
  956. switch (minor) {
  957. case 0:
  958. subtype = "other";
  959. break;
  960. case 1:
  961. subtype = "Funnel, router";
  962. break;
  963. case 2:
  964. subtype = "Filter";
  965. break;
  966. case 3:
  967. subtype = "FIFO, buffer";
  968. break;
  969. }
  970. break;
  971. case 3:
  972. major = "Trace Source";
  973. switch (minor) {
  974. case 0:
  975. subtype = "other";
  976. break;
  977. case 1:
  978. subtype = "Processor";
  979. break;
  980. case 2:
  981. subtype = "DSP";
  982. break;
  983. case 3:
  984. subtype = "Engine/Coprocessor";
  985. break;
  986. case 4:
  987. subtype = "Bus";
  988. break;
  989. case 6:
  990. subtype = "Software";
  991. break;
  992. }
  993. break;
  994. case 4:
  995. major = "Debug Control";
  996. switch (minor) {
  997. case 0:
  998. subtype = "other";
  999. break;
  1000. case 1:
  1001. subtype = "Trigger Matrix";
  1002. break;
  1003. case 2:
  1004. subtype = "Debug Auth";
  1005. break;
  1006. case 3:
  1007. subtype = "Power Requestor";
  1008. break;
  1009. }
  1010. break;
  1011. case 5:
  1012. major = "Debug Logic";
  1013. switch (minor) {
  1014. case 0:
  1015. subtype = "other";
  1016. break;
  1017. case 1:
  1018. subtype = "Processor";
  1019. break;
  1020. case 2:
  1021. subtype = "DSP";
  1022. break;
  1023. case 3:
  1024. subtype = "Engine/Coprocessor";
  1025. break;
  1026. case 4:
  1027. subtype = "Bus";
  1028. break;
  1029. case 5:
  1030. subtype = "Memory";
  1031. break;
  1032. }
  1033. break;
  1034. case 6:
  1035. major = "Perfomance Monitor";
  1036. switch (minor) {
  1037. case 0:
  1038. subtype = "other";
  1039. break;
  1040. case 1:
  1041. subtype = "Processor";
  1042. break;
  1043. case 2:
  1044. subtype = "DSP";
  1045. break;
  1046. case 3:
  1047. subtype = "Engine/Coprocessor";
  1048. break;
  1049. case 4:
  1050. subtype = "Bus";
  1051. break;
  1052. case 5:
  1053. subtype = "Memory";
  1054. break;
  1055. }
  1056. break;
  1057. }
  1058. command_print(cmd_ctx, "\t\tType is 0x%02" PRIx8 ", %s, %s",
  1059. (uint8_t)(devtype & 0xff),
  1060. major, subtype);
  1061. /* REVISIT also show 0xfc8 DevId */
  1062. }
  1063. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  1064. command_print(cmd_ctx,
  1065. "\t\tCID3 0%02x"
  1066. ", CID2 0%02x"
  1067. ", CID1 0%02x"
  1068. ", CID0 0%02x",
  1069. (int)c_cid3,
  1070. (int)c_cid2,
  1071. (int)c_cid1,
  1072. (int)c_cid0);
  1073. command_print(cmd_ctx,
  1074. "\t\tPeripheral ID[4..0] = hex "
  1075. "%02x %02x %02x %02x %02x",
  1076. (int)c_pid4, (int)c_pid3, (int)c_pid2,
  1077. (int)c_pid1, (int)c_pid0);
  1078. /* Part number interpretations are from Cortex
  1079. * core specs, the CoreSight components TRM
  1080. * (ARM DDI 0314H), CoreSight System Design
  1081. * Guide (ARM DGI 0012D) and ETM specs; also
  1082. * from chip observation (e.g. TI SDTI).
  1083. */
  1084. part_num = (c_pid0 & 0xff);
  1085. part_num |= (c_pid1 & 0x0f) << 8;
  1086. switch (part_num) {
  1087. case 0x000:
  1088. type = "Cortex-M3 NVIC";
  1089. full = "(Interrupt Controller)";
  1090. break;
  1091. case 0x001:
  1092. type = "Cortex-M3 ITM";
  1093. full = "(Instrumentation Trace Module)";
  1094. break;
  1095. case 0x002:
  1096. type = "Cortex-M3 DWT";
  1097. full = "(Data Watchpoint and Trace)";
  1098. break;
  1099. case 0x003:
  1100. type = "Cortex-M3 FBP";
  1101. full = "(Flash Patch and Breakpoint)";
  1102. break;
  1103. case 0x008:
  1104. type = "Cortex-M0 SCS";
  1105. full = "(System Control Space)";
  1106. break;
  1107. case 0x00a:
  1108. type = "Cortex-M0 DWT";
  1109. full = "(Data Watchpoint and Trace)";
  1110. break;
  1111. case 0x00b:
  1112. type = "Cortex-M0 BPU";
  1113. full = "(Breakpoint Unit)";
  1114. break;
  1115. case 0x00c:
  1116. type = "Cortex-M4 SCS";
  1117. full = "(System Control Space)";
  1118. break;
  1119. case 0x00d:
  1120. type = "CoreSight ETM11";
  1121. full = "(Embedded Trace)";
  1122. break;
  1123. /* case 0x113: what? */
  1124. case 0x120: /* from OMAP3 memmap */
  1125. type = "TI SDTI";
  1126. full = "(System Debug Trace Interface)";
  1127. break;
  1128. case 0x343: /* from OMAP3 memmap */
  1129. type = "TI DAPCTL";
  1130. full = "";
  1131. break;
  1132. case 0x906:
  1133. type = "Coresight CTI";
  1134. full = "(Cross Trigger)";
  1135. break;
  1136. case 0x907:
  1137. type = "Coresight ETB";
  1138. full = "(Trace Buffer)";
  1139. break;
  1140. case 0x908:
  1141. type = "Coresight CSTF";
  1142. full = "(Trace Funnel)";
  1143. break;
  1144. case 0x910:
  1145. type = "CoreSight ETM9";
  1146. full = "(Embedded Trace)";
  1147. break;
  1148. case 0x912:
  1149. type = "Coresight TPIU";
  1150. full = "(Trace Port Interface Unit)";
  1151. break;
  1152. case 0x913:
  1153. type = "Coresight ITM";
  1154. full = "(Instrumentation Trace Macrocell)";
  1155. break;
  1156. case 0x914:
  1157. type = "Coresight SWO";
  1158. full = "(Single Wire Output)";
  1159. break;
  1160. case 0x917:
  1161. type = "Coresight HTM";
  1162. full = "(AHB Trace Macrocell)";
  1163. break;
  1164. case 0x920:
  1165. type = "CoreSight ETM11";
  1166. full = "(Embedded Trace)";
  1167. break;
  1168. case 0x921:
  1169. type = "Cortex-A8 ETM";
  1170. full = "(Embedded Trace)";
  1171. break;
  1172. case 0x922:
  1173. type = "Cortex-A8 CTI";
  1174. full = "(Cross Trigger)";
  1175. break;
  1176. case 0x923:
  1177. type = "Cortex-M3 TPIU";
  1178. full = "(Trace Port Interface Unit)";
  1179. break;
  1180. case 0x924:
  1181. type = "Cortex-M3 ETM";
  1182. full = "(Embedded Trace)";
  1183. break;
  1184. case 0x925:
  1185. type = "Cortex-M4 ETM";
  1186. full = "(Embedded Trace)";
  1187. break;
  1188. case 0x930:
  1189. type = "Cortex-R4 ETM";
  1190. full = "(Embedded Trace)";
  1191. break;
  1192. case 0x950:
  1193. type = "CoreSight Component";
  1194. full = "(unidentified Cortex-A9 component)";
  1195. break;
  1196. case 0x961:
  1197. type = "CoreSight TMC";
  1198. full = "(Trace Memory Controller)";
  1199. break;
  1200. case 0x962:
  1201. type = "CoreSight STM";
  1202. full = "(System Trace Macrocell)";
  1203. break;
  1204. case 0x9a0:
  1205. type = "CoreSight PMU";
  1206. full = "(Performance Monitoring Unit)";
  1207. break;
  1208. case 0x9a1:
  1209. type = "Cortex-M4 TPUI";
  1210. full = "(Trace Port Interface Unit)";
  1211. break;
  1212. case 0x9a5:
  1213. type = "Cortex-A5 ETM";
  1214. full = "(Embedded Trace)";
  1215. break;
  1216. case 0xc05:
  1217. type = "Cortex-A5 Debug";
  1218. full = "(Debug Unit)";
  1219. break;
  1220. case 0xc08:
  1221. type = "Cortex-A8 Debug";
  1222. full = "(Debug Unit)";
  1223. break;
  1224. case 0xc09:
  1225. type = "Cortex-A9 Debug";
  1226. full = "(Debug Unit)";
  1227. break;
  1228. case 0x4af:
  1229. type = "Cortex-A15 Debug";
  1230. full = "(Debug Unit)";
  1231. break;
  1232. default:
  1233. LOG_DEBUG("Unrecognized Part number 0x%" PRIx32, part_num);
  1234. type = "-*- unrecognized -*-";
  1235. full = "";
  1236. break;
  1237. }
  1238. command_print(cmd_ctx, "\t\tPart is %s %s",
  1239. type, full);
  1240. /* ROM Table? */
  1241. if (((c_cid1 >> 4) & 0x0f) == 1) {
  1242. retval = dap_rom_display(cmd_ctx, dap, ap, component_base, depth + 1);
  1243. if (retval != ERROR_OK)
  1244. return retval;
  1245. }
  1246. } else {
  1247. if (romentry)
  1248. command_print(cmd_ctx, "\t\tComponent not present");
  1249. else
  1250. break;
  1251. }
  1252. }
  1253. command_print(cmd_ctx, "\t%s\tEnd of ROM table", tabs);
  1254. return ERROR_OK;
  1255. }
  1256. static int dap_info_command(struct command_context *cmd_ctx,
  1257. struct adiv5_dap *dap, int ap)
  1258. {
  1259. int retval;
  1260. uint32_t dbgbase, apid;
  1261. int romtable_present = 0;
  1262. uint8_t mem_ap;
  1263. uint32_t ap_old;
  1264. retval = dap_get_debugbase(dap, ap, &dbgbase, &apid);
  1265. if (retval != ERROR_OK)
  1266. return retval;
  1267. ap_old = dap->ap_current;
  1268. dap_ap_select(dap, ap);
  1269. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  1270. mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
  1271. command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
  1272. if (apid) {
  1273. switch (apid&0x0F) {
  1274. case 0:
  1275. command_print(cmd_ctx, "\tType is JTAG-AP");
  1276. break;
  1277. case 1:
  1278. command_print(cmd_ctx, "\tType is MEM-AP AHB");
  1279. break;
  1280. case 2:
  1281. command_print(cmd_ctx, "\tType is MEM-AP APB");
  1282. break;
  1283. default:
  1284. command_print(cmd_ctx, "\tUnknown AP type");
  1285. break;
  1286. }
  1287. /* NOTE: a MEM-AP may have a single CoreSight component that's
  1288. * not a ROM table ... or have no such components at all.
  1289. */
  1290. if (mem_ap)
  1291. command_print(cmd_ctx, "AP BASE 0x%8.8" PRIx32, dbgbase);
  1292. } else
  1293. command_print(cmd_ctx, "No AP found at this ap 0x%x", ap);
  1294. romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
  1295. if (romtable_present)
  1296. dap_rom_display(cmd_ctx, dap, ap, dbgbase, 0);
  1297. else
  1298. command_print(cmd_ctx, "\tNo ROM table present");
  1299. dap_ap_select(dap, ap_old);
  1300. return ERROR_OK;
  1301. }
  1302. COMMAND_HANDLER(handle_dap_info_command)
  1303. {
  1304. struct target *target = get_current_target(CMD_CTX);
  1305. struct arm *arm = target_to_arm(target);
  1306. struct adiv5_dap *dap = arm->dap;
  1307. uint32_t apsel;
  1308. switch (CMD_ARGC) {
  1309. case 0:
  1310. apsel = dap->apsel;
  1311. break;
  1312. case 1:
  1313. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1314. break;
  1315. default:
  1316. return ERROR_COMMAND_SYNTAX_ERROR;
  1317. }
  1318. return dap_info_command(CMD_CTX, dap, apsel);
  1319. }
  1320. COMMAND_HANDLER(dap_baseaddr_command)
  1321. {
  1322. struct target *target = get_current_target(CMD_CTX);
  1323. struct arm *arm = target_to_arm(target);
  1324. struct adiv5_dap *dap = arm->dap;
  1325. uint32_t apsel, baseaddr;
  1326. int retval;
  1327. switch (CMD_ARGC) {
  1328. case 0:
  1329. apsel = dap->apsel;
  1330. break;
  1331. case 1:
  1332. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1333. /* AP address is in bits 31:24 of DP_SELECT */
  1334. if (apsel >= 256)
  1335. return ERROR_COMMAND_SYNTAX_ERROR;
  1336. break;
  1337. default:
  1338. return ERROR_COMMAND_SYNTAX_ERROR;
  1339. }
  1340. dap_ap_select(dap, apsel);
  1341. /* NOTE: assumes we're talking to a MEM-AP, which
  1342. * has a base address. There are other kinds of AP,
  1343. * though they're not common for now. This should
  1344. * use the ID register to verify it's a MEM-AP.
  1345. */
  1346. retval = dap_queue_ap_read(dap, AP_REG_BASE, &baseaddr);
  1347. if (retval != ERROR_OK)
  1348. return retval;
  1349. retval = dap_run(dap);
  1350. if (retval != ERROR_OK)
  1351. return retval;
  1352. command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
  1353. return retval;
  1354. }
  1355. COMMAND_HANDLER(dap_memaccess_command)
  1356. {
  1357. struct target *target = get_current_target(CMD_CTX);
  1358. struct arm *arm = target_to_arm(target);
  1359. struct adiv5_dap *dap = arm->dap;
  1360. uint32_t memaccess_tck;
  1361. switch (CMD_ARGC) {
  1362. case 0:
  1363. memaccess_tck = dap->memaccess_tck;
  1364. break;
  1365. case 1:
  1366. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
  1367. break;
  1368. default:
  1369. return ERROR_COMMAND_SYNTAX_ERROR;
  1370. }
  1371. dap->memaccess_tck = memaccess_tck;
  1372. command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
  1373. dap->memaccess_tck);
  1374. return ERROR_OK;
  1375. }
  1376. COMMAND_HANDLER(dap_apsel_command)
  1377. {
  1378. struct target *target = get_current_target(CMD_CTX);
  1379. struct arm *arm = target_to_arm(target);
  1380. struct adiv5_dap *dap = arm->dap;
  1381. uint32_t apsel, apid;
  1382. int retval;
  1383. switch (CMD_ARGC) {
  1384. case 0:
  1385. apsel = 0;
  1386. break;
  1387. case 1:
  1388. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1389. /* AP address is in bits 31:24 of DP_SELECT */
  1390. if (apsel >= 256)
  1391. return ERROR_COMMAND_SYNTAX_ERROR;
  1392. break;
  1393. default:
  1394. return ERROR_COMMAND_SYNTAX_ERROR;
  1395. }
  1396. dap->apsel = apsel;
  1397. dap_ap_select(dap, apsel);
  1398. retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
  1399. if (retval != ERROR_OK)
  1400. return retval;
  1401. retval = dap_run(dap);
  1402. if (retval != ERROR_OK)
  1403. return retval;
  1404. command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
  1405. apsel, apid);
  1406. return retval;
  1407. }
  1408. COMMAND_HANDLER(dap_apcsw_command)
  1409. {
  1410. struct target *target = get_current_target(CMD_CTX);
  1411. struct arm *arm = target_to_arm(target);
  1412. struct adiv5_dap *dap = arm->dap;
  1413. uint32_t apcsw = dap->apcsw[dap->apsel], sprot = 0;
  1414. switch (CMD_ARGC) {
  1415. case 0:
  1416. command_print(CMD_CTX, "apsel %" PRIi32 " selected, csw 0x%8.8" PRIx32,
  1417. (dap->apsel), apcsw);
  1418. break;
  1419. case 1:
  1420. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], sprot);
  1421. /* AP address is in bits 31:24 of DP_SELECT */
  1422. if (sprot > 1)
  1423. return ERROR_COMMAND_SYNTAX_ERROR;
  1424. if (sprot)
  1425. apcsw |= CSW_SPROT;
  1426. else
  1427. apcsw &= ~CSW_SPROT;
  1428. break;
  1429. default:
  1430. return ERROR_COMMAND_SYNTAX_ERROR;
  1431. }
  1432. dap->apcsw[dap->apsel] = apcsw;
  1433. return 0;
  1434. }
  1435. COMMAND_HANDLER(dap_apid_command)
  1436. {
  1437. struct target *target = get_current_target(CMD_CTX);
  1438. struct arm *arm = target_to_arm(target);
  1439. struct adiv5_dap *dap = arm->dap;
  1440. uint32_t apsel, apid;
  1441. int retval;
  1442. switch (CMD_ARGC) {
  1443. case 0:
  1444. apsel = dap->apsel;
  1445. break;
  1446. case 1:
  1447. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1448. /* AP address is in bits 31:24 of DP_SELECT */
  1449. if (apsel >= 256)
  1450. return ERROR_COMMAND_SYNTAX_ERROR;
  1451. break;
  1452. default:
  1453. return ERROR_COMMAND_SYNTAX_ERROR;
  1454. }
  1455. dap_ap_select(dap, apsel);
  1456. retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
  1457. if (retval != ERROR_OK)
  1458. return retval;
  1459. retval = dap_run(dap);
  1460. if (retval != ERROR_OK)
  1461. return retval;
  1462. command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
  1463. return retval;
  1464. }
  1465. COMMAND_HANDLER(dap_ti_be_32_quirks_command)
  1466. {
  1467. struct target *target = get_current_target(CMD_CTX);
  1468. struct arm *arm = target_to_arm(target);
  1469. struct adiv5_dap *dap = arm->dap;
  1470. uint32_t enable = dap->ti_be_32_quirks;
  1471. switch (CMD_ARGC) {
  1472. case 0:
  1473. break;
  1474. case 1:
  1475. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], enable);
  1476. if (enable > 1)
  1477. return ERROR_COMMAND_SYNTAX_ERROR;
  1478. break;
  1479. default:
  1480. return ERROR_COMMAND_SYNTAX_ERROR;
  1481. }
  1482. dap->ti_be_32_quirks = enable;
  1483. command_print(CMD_CTX, "TI BE-32 quirks mode %s",
  1484. enable ? "enabled" : "disabled");
  1485. return 0;
  1486. }
  1487. static const struct command_registration dap_commands[] = {
  1488. {
  1489. .name = "info",
  1490. .handler = handle_dap_info_command,
  1491. .mode = COMMAND_EXEC,
  1492. .help = "display ROM table for MEM-AP "
  1493. "(default currently selected AP)",
  1494. .usage = "[ap_num]",
  1495. },
  1496. {
  1497. .name = "apsel",
  1498. .handler = dap_apsel_command,
  1499. .mode = COMMAND_EXEC,
  1500. .help = "Set the currently selected AP (default 0) "
  1501. "and display the result",
  1502. .usage = "[ap_num]",
  1503. },
  1504. {
  1505. .name = "apcsw",
  1506. .handler = dap_apcsw_command,
  1507. .mode = COMMAND_EXEC,
  1508. .help = "Set csw access bit ",
  1509. .usage = "[sprot]",
  1510. },
  1511. {
  1512. .name = "apid",
  1513. .handler = dap_apid_command,
  1514. .mode = COMMAND_EXEC,
  1515. .help = "return ID register from AP "
  1516. "(default currently selected AP)",
  1517. .usage = "[ap_num]",
  1518. },
  1519. {
  1520. .name = "baseaddr",
  1521. .handler = dap_baseaddr_command,
  1522. .mode = COMMAND_EXEC,
  1523. .help = "return debug base address from MEM-AP "
  1524. "(default currently selected AP)",
  1525. .usage = "[ap_num]",
  1526. },
  1527. {
  1528. .name = "memaccess",
  1529. .handler = dap_memaccess_command,
  1530. .mode = COMMAND_EXEC,
  1531. .help = "set/get number of extra tck for MEM-AP memory "
  1532. "bus access [0-255]",
  1533. .usage = "[cycles]",
  1534. },
  1535. {
  1536. .name = "ti_be_32_quirks",
  1537. .handler = dap_ti_be_32_quirks_command,
  1538. .mode = COMMAND_CONFIG,
  1539. .help = "set/get quirks mode for TI TMS450/TMS570 processors",
  1540. .usage = "[enable]",
  1541. },
  1542. COMMAND_REGISTRATION_DONE
  1543. };
  1544. const struct command_registration dap_command_handlers[] = {
  1545. {
  1546. .name = "dap",
  1547. .mode = COMMAND_EXEC,
  1548. .help = "DAP command group",
  1549. .usage = "",
  1550. .chain = dap_commands,
  1551. },
  1552. COMMAND_REGISTRATION_DONE
  1553. };