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.
 
 
 
 
 
 

2093 lines
63 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. * Copyright (C) 2019-2021, Ampere Computing LLC *
  17. * *
  18. * This program is free software; you can redistribute it and/or modify *
  19. * it under the terms of the GNU General Public License as published by *
  20. * the Free Software Foundation; either version 2 of the License, or *
  21. * (at your option) any later version. *
  22. * *
  23. * This program is distributed in the hope that it will be useful, *
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  26. * GNU General Public License for more details. *
  27. * *
  28. * You should have received a copy of the GNU General Public License *
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  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 focuses 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 pipelining. 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 0031E
  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 "jtag/swd.h"
  75. #include "transport/transport.h"
  76. #include <helper/jep106.h>
  77. #include <helper/time_support.h>
  78. #include <helper/list.h>
  79. #include <helper/jim-nvp.h>
  80. /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
  81. /*
  82. uint32_t tar_block_size(uint32_t address)
  83. Return the largest block starting at address that does not cross a tar block size alignment boundary
  84. */
  85. static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, target_addr_t address)
  86. {
  87. return tar_autoincr_block - ((tar_autoincr_block - 1) & address);
  88. }
  89. /***************************************************************************
  90. * *
  91. * DP and MEM-AP register access through APACC and DPACC *
  92. * *
  93. ***************************************************************************/
  94. static int mem_ap_setup_csw(struct adiv5_ap *ap, uint32_t csw)
  95. {
  96. csw |= ap->csw_default;
  97. if (csw != ap->csw_value) {
  98. /* LOG_DEBUG("DAP: Set CSW %x",csw); */
  99. int retval = dap_queue_ap_write(ap, MEM_AP_REG_CSW, csw);
  100. if (retval != ERROR_OK) {
  101. ap->csw_value = 0;
  102. return retval;
  103. }
  104. ap->csw_value = csw;
  105. }
  106. return ERROR_OK;
  107. }
  108. static int mem_ap_setup_tar(struct adiv5_ap *ap, target_addr_t tar)
  109. {
  110. if (!ap->tar_valid || tar != ap->tar_value) {
  111. /* LOG_DEBUG("DAP: Set TAR %x",tar); */
  112. int retval = dap_queue_ap_write(ap, MEM_AP_REG_TAR, (uint32_t)(tar & 0xffffffffUL));
  113. if (retval == ERROR_OK && is_64bit_ap(ap)) {
  114. /* See if bits 63:32 of tar is different from last setting */
  115. if ((ap->tar_value >> 32) != (tar >> 32))
  116. retval = dap_queue_ap_write(ap, MEM_AP_REG_TAR64, (uint32_t)(tar >> 32));
  117. }
  118. if (retval != ERROR_OK) {
  119. ap->tar_valid = false;
  120. return retval;
  121. }
  122. ap->tar_value = tar;
  123. ap->tar_valid = true;
  124. }
  125. return ERROR_OK;
  126. }
  127. static int mem_ap_read_tar(struct adiv5_ap *ap, target_addr_t *tar)
  128. {
  129. uint32_t lower;
  130. uint32_t upper = 0;
  131. int retval = dap_queue_ap_read(ap, MEM_AP_REG_TAR, &lower);
  132. if (retval == ERROR_OK && is_64bit_ap(ap))
  133. retval = dap_queue_ap_read(ap, MEM_AP_REG_TAR64, &upper);
  134. if (retval != ERROR_OK) {
  135. ap->tar_valid = false;
  136. return retval;
  137. }
  138. retval = dap_run(ap->dap);
  139. if (retval != ERROR_OK) {
  140. ap->tar_valid = false;
  141. return retval;
  142. }
  143. *tar = (((target_addr_t)upper) << 32) | (target_addr_t)lower;
  144. ap->tar_value = *tar;
  145. ap->tar_valid = true;
  146. return ERROR_OK;
  147. }
  148. static uint32_t mem_ap_get_tar_increment(struct adiv5_ap *ap)
  149. {
  150. switch (ap->csw_value & CSW_ADDRINC_MASK) {
  151. case CSW_ADDRINC_SINGLE:
  152. switch (ap->csw_value & CSW_SIZE_MASK) {
  153. case CSW_8BIT:
  154. return 1;
  155. case CSW_16BIT:
  156. return 2;
  157. case CSW_32BIT:
  158. return 4;
  159. default:
  160. return 0;
  161. }
  162. case CSW_ADDRINC_PACKED:
  163. return 4;
  164. }
  165. return 0;
  166. }
  167. /* mem_ap_update_tar_cache is called after an access to MEM_AP_REG_DRW
  168. */
  169. static void mem_ap_update_tar_cache(struct adiv5_ap *ap)
  170. {
  171. if (!ap->tar_valid)
  172. return;
  173. uint32_t inc = mem_ap_get_tar_increment(ap);
  174. if (inc >= max_tar_block_size(ap->tar_autoincr_block, ap->tar_value))
  175. ap->tar_valid = false;
  176. else
  177. ap->tar_value += inc;
  178. }
  179. /**
  180. * Queue transactions setting up transfer parameters for the
  181. * currently selected MEM-AP.
  182. *
  183. * Subsequent transfers using registers like MEM_AP_REG_DRW or MEM_AP_REG_BD2
  184. * initiate data reads or writes using memory or peripheral addresses.
  185. * If the CSW is configured for it, the TAR may be automatically
  186. * incremented after each transfer.
  187. *
  188. * @param ap The MEM-AP.
  189. * @param csw MEM-AP Control/Status Word (CSW) register to assign. If this
  190. * matches the cached value, the register is not changed.
  191. * @param tar MEM-AP Transfer Address Register (TAR) to assign. If this
  192. * matches the cached address, the register is not changed.
  193. *
  194. * @return ERROR_OK if the transaction was properly queued, else a fault code.
  195. */
  196. static int mem_ap_setup_transfer(struct adiv5_ap *ap, uint32_t csw, target_addr_t tar)
  197. {
  198. int retval;
  199. retval = mem_ap_setup_csw(ap, csw);
  200. if (retval != ERROR_OK)
  201. return retval;
  202. retval = mem_ap_setup_tar(ap, tar);
  203. if (retval != ERROR_OK)
  204. return retval;
  205. return ERROR_OK;
  206. }
  207. /**
  208. * Asynchronous (queued) read of a word from memory or a system register.
  209. *
  210. * @param ap The MEM-AP to access.
  211. * @param address Address of the 32-bit word to read; it must be
  212. * readable by the currently selected MEM-AP.
  213. * @param value points to where the word will be stored when the
  214. * transaction queue is flushed (assuming no errors).
  215. *
  216. * @return ERROR_OK for success. Otherwise a fault code.
  217. */
  218. int mem_ap_read_u32(struct adiv5_ap *ap, target_addr_t address,
  219. uint32_t *value)
  220. {
  221. int retval;
  222. /* Use banked addressing (REG_BDx) to avoid some link traffic
  223. * (updating TAR) when reading several consecutive addresses.
  224. */
  225. retval = mem_ap_setup_transfer(ap,
  226. CSW_32BIT | (ap->csw_value & CSW_ADDRINC_MASK),
  227. address & 0xFFFFFFFFFFFFFFF0ull);
  228. if (retval != ERROR_OK)
  229. return retval;
  230. return dap_queue_ap_read(ap, MEM_AP_REG_BD0 | (address & 0xC), value);
  231. }
  232. /**
  233. * Synchronous read of a word from memory or a system register.
  234. * As a side effect, this flushes any queued transactions.
  235. *
  236. * @param ap The MEM-AP to access.
  237. * @param address Address of the 32-bit word to read; it must be
  238. * readable by the currently selected MEM-AP.
  239. * @param value points to where the result will be stored.
  240. *
  241. * @return ERROR_OK for success; *value holds the result.
  242. * Otherwise a fault code.
  243. */
  244. int mem_ap_read_atomic_u32(struct adiv5_ap *ap, target_addr_t address,
  245. uint32_t *value)
  246. {
  247. int retval;
  248. retval = mem_ap_read_u32(ap, address, value);
  249. if (retval != ERROR_OK)
  250. return retval;
  251. return dap_run(ap->dap);
  252. }
  253. /**
  254. * Asynchronous (queued) write of a word to memory or a system register.
  255. *
  256. * @param ap The MEM-AP to access.
  257. * @param address Address to be written; it must be writable by
  258. * the currently selected MEM-AP.
  259. * @param value Word that will be written to the address when transaction
  260. * queue is flushed (assuming no errors).
  261. *
  262. * @return ERROR_OK for success. Otherwise a fault code.
  263. */
  264. int mem_ap_write_u32(struct adiv5_ap *ap, target_addr_t address,
  265. uint32_t value)
  266. {
  267. int retval;
  268. /* Use banked addressing (REG_BDx) to avoid some link traffic
  269. * (updating TAR) when writing several consecutive addresses.
  270. */
  271. retval = mem_ap_setup_transfer(ap,
  272. CSW_32BIT | (ap->csw_value & CSW_ADDRINC_MASK),
  273. address & 0xFFFFFFFFFFFFFFF0ull);
  274. if (retval != ERROR_OK)
  275. return retval;
  276. return dap_queue_ap_write(ap, MEM_AP_REG_BD0 | (address & 0xC),
  277. value);
  278. }
  279. /**
  280. * Synchronous write of a word to memory or a system register.
  281. * As a side effect, this flushes any queued transactions.
  282. *
  283. * @param ap The MEM-AP to access.
  284. * @param address Address to be written; it must be writable by
  285. * the currently selected MEM-AP.
  286. * @param value Word that will be written.
  287. *
  288. * @return ERROR_OK for success; the data was written. Otherwise a fault code.
  289. */
  290. int mem_ap_write_atomic_u32(struct adiv5_ap *ap, target_addr_t address,
  291. uint32_t value)
  292. {
  293. int retval = mem_ap_write_u32(ap, address, value);
  294. if (retval != ERROR_OK)
  295. return retval;
  296. return dap_run(ap->dap);
  297. }
  298. /**
  299. * Synchronous write of a block of memory, using a specific access size.
  300. *
  301. * @param ap The MEM-AP to access.
  302. * @param buffer The data buffer to write. No particular alignment is assumed.
  303. * @param size Which access size to use, in bytes. 1, 2 or 4.
  304. * @param count The number of writes to do (in size units, not bytes).
  305. * @param address Address to be written; it must be writable by the currently selected MEM-AP.
  306. * @param addrinc Whether the target address should be increased for each write or not. This
  307. * should normally be true, except when writing to e.g. a FIFO.
  308. * @return ERROR_OK on success, otherwise an error code.
  309. */
  310. static int mem_ap_write(struct adiv5_ap *ap, const uint8_t *buffer, uint32_t size, uint32_t count,
  311. target_addr_t address, bool addrinc)
  312. {
  313. struct adiv5_dap *dap = ap->dap;
  314. size_t nbytes = size * count;
  315. const uint32_t csw_addrincr = addrinc ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
  316. uint32_t csw_size;
  317. target_addr_t addr_xor;
  318. int retval = ERROR_OK;
  319. /* TI BE-32 Quirks mode:
  320. * Writes on big-endian TMS570 behave very strangely. Observed behavior:
  321. * size write address bytes written in order
  322. * 4 TAR ^ 0 (val >> 24), (val >> 16), (val >> 8), (val)
  323. * 2 TAR ^ 2 (val >> 8), (val)
  324. * 1 TAR ^ 3 (val)
  325. * For example, if you attempt to write a single byte to address 0, the processor
  326. * will actually write a byte to address 3.
  327. *
  328. * To make writes of size < 4 work as expected, we xor a value with the address before
  329. * setting the TAP, and we set the TAP after every transfer rather then relying on
  330. * address increment. */
  331. if (size == 4) {
  332. csw_size = CSW_32BIT;
  333. addr_xor = 0;
  334. } else if (size == 2) {
  335. csw_size = CSW_16BIT;
  336. addr_xor = dap->ti_be_32_quirks ? 2 : 0;
  337. } else if (size == 1) {
  338. csw_size = CSW_8BIT;
  339. addr_xor = dap->ti_be_32_quirks ? 3 : 0;
  340. } else {
  341. return ERROR_TARGET_UNALIGNED_ACCESS;
  342. }
  343. if (ap->unaligned_access_bad && (address % size != 0))
  344. return ERROR_TARGET_UNALIGNED_ACCESS;
  345. while (nbytes > 0) {
  346. uint32_t this_size = size;
  347. /* Select packed transfer if possible */
  348. if (addrinc && ap->packed_transfers && nbytes >= 4
  349. && max_tar_block_size(ap->tar_autoincr_block, address) >= 4) {
  350. this_size = 4;
  351. retval = mem_ap_setup_csw(ap, csw_size | CSW_ADDRINC_PACKED);
  352. } else {
  353. retval = mem_ap_setup_csw(ap, csw_size | csw_addrincr);
  354. }
  355. if (retval != ERROR_OK)
  356. break;
  357. retval = mem_ap_setup_tar(ap, address ^ addr_xor);
  358. if (retval != ERROR_OK)
  359. return retval;
  360. /* How many source bytes each transfer will consume, and their location in the DRW,
  361. * depends on the type of transfer and alignment. See ARM document IHI0031C. */
  362. uint32_t outvalue = 0;
  363. uint32_t drw_byte_idx = address;
  364. if (dap->ti_be_32_quirks) {
  365. switch (this_size) {
  366. case 4:
  367. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (drw_byte_idx++ & 3) ^ addr_xor);
  368. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (drw_byte_idx++ & 3) ^ addr_xor);
  369. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (drw_byte_idx++ & 3) ^ addr_xor);
  370. outvalue |= (uint32_t)*buffer++ << 8 * (3 ^ (drw_byte_idx & 3) ^ addr_xor);
  371. break;
  372. case 2:
  373. outvalue |= (uint32_t)*buffer++ << 8 * (1 ^ (drw_byte_idx++ & 3) ^ addr_xor);
  374. outvalue |= (uint32_t)*buffer++ << 8 * (1 ^ (drw_byte_idx & 3) ^ addr_xor);
  375. break;
  376. case 1:
  377. outvalue |= (uint32_t)*buffer++ << 8 * (0 ^ (drw_byte_idx & 3) ^ addr_xor);
  378. break;
  379. }
  380. } else {
  381. switch (this_size) {
  382. case 4:
  383. outvalue |= (uint32_t)*buffer++ << 8 * (drw_byte_idx++ & 3);
  384. outvalue |= (uint32_t)*buffer++ << 8 * (drw_byte_idx++ & 3);
  385. /* fallthrough */
  386. case 2:
  387. outvalue |= (uint32_t)*buffer++ << 8 * (drw_byte_idx++ & 3);
  388. /* fallthrough */
  389. case 1:
  390. outvalue |= (uint32_t)*buffer++ << 8 * (drw_byte_idx & 3);
  391. }
  392. }
  393. nbytes -= this_size;
  394. retval = dap_queue_ap_write(ap, MEM_AP_REG_DRW, outvalue);
  395. if (retval != ERROR_OK)
  396. break;
  397. mem_ap_update_tar_cache(ap);
  398. if (addrinc)
  399. address += this_size;
  400. }
  401. /* REVISIT: Might want to have a queued version of this function that does not run. */
  402. if (retval == ERROR_OK)
  403. retval = dap_run(dap);
  404. if (retval != ERROR_OK) {
  405. target_addr_t tar;
  406. if (mem_ap_read_tar(ap, &tar) == ERROR_OK)
  407. LOG_ERROR("Failed to write memory at " TARGET_ADDR_FMT, tar);
  408. else
  409. LOG_ERROR("Failed to write memory and, additionally, failed to find out where");
  410. }
  411. return retval;
  412. }
  413. /**
  414. * Synchronous read of a block of memory, using a specific access size.
  415. *
  416. * @param ap The MEM-AP to access.
  417. * @param buffer The data buffer to receive the data. No particular alignment is assumed.
  418. * @param size Which access size to use, in bytes. 1, 2 or 4.
  419. * @param count The number of reads to do (in size units, not bytes).
  420. * @param adr Address to be read; it must be readable by the currently selected MEM-AP.
  421. * @param addrinc Whether the target address should be increased after each read or not. This
  422. * should normally be true, except when reading from e.g. a FIFO.
  423. * @return ERROR_OK on success, otherwise an error code.
  424. */
  425. static int mem_ap_read(struct adiv5_ap *ap, uint8_t *buffer, uint32_t size, uint32_t count,
  426. target_addr_t adr, bool addrinc)
  427. {
  428. struct adiv5_dap *dap = ap->dap;
  429. size_t nbytes = size * count;
  430. const uint32_t csw_addrincr = addrinc ? CSW_ADDRINC_SINGLE : CSW_ADDRINC_OFF;
  431. uint32_t csw_size;
  432. target_addr_t address = adr;
  433. int retval = ERROR_OK;
  434. /* TI BE-32 Quirks mode:
  435. * Reads on big-endian TMS570 behave strangely differently than writes.
  436. * They read from the physical address requested, but with DRW byte-reversed.
  437. * For example, a byte read from address 0 will place the result in the high bytes of DRW.
  438. * Also, packed 8-bit and 16-bit transfers seem to sometimes return garbage in some bytes,
  439. * so avoid them. */
  440. if (size == 4)
  441. csw_size = CSW_32BIT;
  442. else if (size == 2)
  443. csw_size = CSW_16BIT;
  444. else if (size == 1)
  445. csw_size = CSW_8BIT;
  446. else
  447. return ERROR_TARGET_UNALIGNED_ACCESS;
  448. if (ap->unaligned_access_bad && (adr % size != 0))
  449. return ERROR_TARGET_UNALIGNED_ACCESS;
  450. /* Allocate buffer to hold the sequence of DRW reads that will be made. This is a significant
  451. * over-allocation if packed transfers are going to be used, but determining the real need at
  452. * this point would be messy. */
  453. uint32_t *read_buf = calloc(count, sizeof(uint32_t));
  454. /* Multiplication count * sizeof(uint32_t) may overflow, calloc() is safe */
  455. uint32_t *read_ptr = read_buf;
  456. if (!read_buf) {
  457. LOG_ERROR("Failed to allocate read buffer");
  458. return ERROR_FAIL;
  459. }
  460. /* Queue up all reads. Each read will store the entire DRW word in the read buffer. How many
  461. * useful bytes it contains, and their location in the word, depends on the type of transfer
  462. * and alignment. */
  463. while (nbytes > 0) {
  464. uint32_t this_size = size;
  465. /* Select packed transfer if possible */
  466. if (addrinc && ap->packed_transfers && nbytes >= 4
  467. && max_tar_block_size(ap->tar_autoincr_block, address) >= 4) {
  468. this_size = 4;
  469. retval = mem_ap_setup_csw(ap, csw_size | CSW_ADDRINC_PACKED);
  470. } else {
  471. retval = mem_ap_setup_csw(ap, csw_size | csw_addrincr);
  472. }
  473. if (retval != ERROR_OK)
  474. break;
  475. retval = mem_ap_setup_tar(ap, address);
  476. if (retval != ERROR_OK)
  477. break;
  478. retval = dap_queue_ap_read(ap, MEM_AP_REG_DRW, read_ptr++);
  479. if (retval != ERROR_OK)
  480. break;
  481. nbytes -= this_size;
  482. if (addrinc)
  483. address += this_size;
  484. mem_ap_update_tar_cache(ap);
  485. }
  486. if (retval == ERROR_OK)
  487. retval = dap_run(dap);
  488. /* Restore state */
  489. address = adr;
  490. nbytes = size * count;
  491. read_ptr = read_buf;
  492. /* If something failed, read TAR to find out how much data was successfully read, so we can
  493. * at least give the caller what we have. */
  494. if (retval != ERROR_OK) {
  495. target_addr_t tar;
  496. if (mem_ap_read_tar(ap, &tar) == ERROR_OK) {
  497. /* TAR is incremented after failed transfer on some devices (eg Cortex-M4) */
  498. LOG_ERROR("Failed to read memory at " TARGET_ADDR_FMT, tar);
  499. if (nbytes > tar - address)
  500. nbytes = tar - address;
  501. } else {
  502. LOG_ERROR("Failed to read memory and, additionally, failed to find out where");
  503. nbytes = 0;
  504. }
  505. }
  506. /* Replay loop to populate caller's buffer from the correct word and byte lane */
  507. while (nbytes > 0) {
  508. uint32_t this_size = size;
  509. if (addrinc && ap->packed_transfers && nbytes >= 4
  510. && max_tar_block_size(ap->tar_autoincr_block, address) >= 4) {
  511. this_size = 4;
  512. }
  513. if (dap->ti_be_32_quirks) {
  514. switch (this_size) {
  515. case 4:
  516. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  517. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  518. /* fallthrough */
  519. case 2:
  520. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  521. /* fallthrough */
  522. case 1:
  523. *buffer++ = *read_ptr >> 8 * (3 - (address++ & 3));
  524. }
  525. } else {
  526. switch (this_size) {
  527. case 4:
  528. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  529. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  530. /* fallthrough */
  531. case 2:
  532. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  533. /* fallthrough */
  534. case 1:
  535. *buffer++ = *read_ptr >> 8 * (address++ & 3);
  536. }
  537. }
  538. read_ptr++;
  539. nbytes -= this_size;
  540. }
  541. free(read_buf);
  542. return retval;
  543. }
  544. int mem_ap_read_buf(struct adiv5_ap *ap,
  545. uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
  546. {
  547. return mem_ap_read(ap, buffer, size, count, address, true);
  548. }
  549. int mem_ap_write_buf(struct adiv5_ap *ap,
  550. const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
  551. {
  552. return mem_ap_write(ap, buffer, size, count, address, true);
  553. }
  554. int mem_ap_read_buf_noincr(struct adiv5_ap *ap,
  555. uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
  556. {
  557. return mem_ap_read(ap, buffer, size, count, address, false);
  558. }
  559. int mem_ap_write_buf_noincr(struct adiv5_ap *ap,
  560. const uint8_t *buffer, uint32_t size, uint32_t count, target_addr_t address)
  561. {
  562. return mem_ap_write(ap, buffer, size, count, address, false);
  563. }
  564. /*--------------------------------------------------------------------------*/
  565. #define DAP_POWER_DOMAIN_TIMEOUT (10)
  566. /*--------------------------------------------------------------------------*/
  567. /**
  568. * Invalidate cached DP select and cached TAR and CSW of all APs
  569. */
  570. void dap_invalidate_cache(struct adiv5_dap *dap)
  571. {
  572. dap->select = DP_SELECT_INVALID;
  573. dap->last_read = NULL;
  574. int i;
  575. for (i = 0; i <= 255; i++) {
  576. /* force csw and tar write on the next mem-ap access */
  577. dap->ap[i].tar_valid = false;
  578. dap->ap[i].csw_value = 0;
  579. }
  580. }
  581. /**
  582. * Initialize a DAP. This sets up the power domains, prepares the DP
  583. * for further use and activates overrun checking.
  584. *
  585. * @param dap The DAP being initialized.
  586. */
  587. int dap_dp_init(struct adiv5_dap *dap)
  588. {
  589. int retval;
  590. LOG_DEBUG("%s", adiv5_dap_name(dap));
  591. dap->do_reconnect = false;
  592. dap_invalidate_cache(dap);
  593. /*
  594. * Early initialize dap->dp_ctrl_stat.
  595. * In jtag mode only, if the following queue run (in dap_dp_poll_register)
  596. * fails and sets the sticky error, it will trigger the clearing
  597. * of the sticky. Without this initialization system and debug power
  598. * would be disabled while clearing the sticky error bit.
  599. */
  600. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  601. /*
  602. * This write operation clears the sticky error bit in jtag mode only and
  603. * is ignored in swd mode. It also powers-up system and debug domains in
  604. * both jtag and swd modes, if not done before.
  605. */
  606. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat | SSTICKYERR);
  607. if (retval != ERROR_OK)
  608. return retval;
  609. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  610. if (retval != ERROR_OK)
  611. return retval;
  612. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  613. if (retval != ERROR_OK)
  614. return retval;
  615. /* Check that we have debug power domains activated */
  616. LOG_DEBUG("DAP: wait CDBGPWRUPACK");
  617. retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
  618. CDBGPWRUPACK, CDBGPWRUPACK,
  619. DAP_POWER_DOMAIN_TIMEOUT);
  620. if (retval != ERROR_OK)
  621. return retval;
  622. if (!dap->ignore_syspwrupack) {
  623. LOG_DEBUG("DAP: wait CSYSPWRUPACK");
  624. retval = dap_dp_poll_register(dap, DP_CTRL_STAT,
  625. CSYSPWRUPACK, CSYSPWRUPACK,
  626. DAP_POWER_DOMAIN_TIMEOUT);
  627. if (retval != ERROR_OK)
  628. return retval;
  629. }
  630. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  631. if (retval != ERROR_OK)
  632. return retval;
  633. /* With debug power on we can activate OVERRUN checking */
  634. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
  635. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  636. if (retval != ERROR_OK)
  637. return retval;
  638. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  639. if (retval != ERROR_OK)
  640. return retval;
  641. retval = dap_run(dap);
  642. if (retval != ERROR_OK)
  643. return retval;
  644. return retval;
  645. }
  646. /**
  647. * Initialize a DAP or do reconnect if DAP is not accessible.
  648. *
  649. * @param dap The DAP being initialized.
  650. */
  651. int dap_dp_init_or_reconnect(struct adiv5_dap *dap)
  652. {
  653. LOG_DEBUG("%s", adiv5_dap_name(dap));
  654. /*
  655. * Early initialize dap->dp_ctrl_stat.
  656. * In jtag mode only, if the following atomic reads fail and set the
  657. * sticky error, it will trigger the clearing of the sticky. Without this
  658. * initialization system and debug power would be disabled while clearing
  659. * the sticky error bit.
  660. */
  661. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  662. dap->do_reconnect = false;
  663. dap_dp_read_atomic(dap, DP_CTRL_STAT, NULL);
  664. if (dap->do_reconnect) {
  665. /* dap connect calls dap_dp_init() after transport dependent initialization */
  666. return dap->ops->connect(dap);
  667. } else {
  668. return dap_dp_init(dap);
  669. }
  670. }
  671. /**
  672. * Initialize a DAP. This sets up the power domains, prepares the DP
  673. * for further use, and arranges to use AP #0 for all AP operations
  674. * until dap_ap-select() changes that policy.
  675. *
  676. * @param ap The MEM-AP being initialized.
  677. */
  678. int mem_ap_init(struct adiv5_ap *ap)
  679. {
  680. /* check that we support packed transfers */
  681. uint32_t csw, cfg;
  682. int retval;
  683. struct adiv5_dap *dap = ap->dap;
  684. /* Set ap->cfg_reg before calling mem_ap_setup_transfer(). */
  685. /* mem_ap_setup_transfer() needs to know if the MEM_AP supports LPAE. */
  686. retval = dap_queue_ap_read(ap, MEM_AP_REG_CFG, &cfg);
  687. if (retval != ERROR_OK)
  688. return retval;
  689. retval = dap_run(dap);
  690. if (retval != ERROR_OK)
  691. return retval;
  692. ap->cfg_reg = cfg;
  693. ap->tar_valid = false;
  694. ap->csw_value = 0; /* force csw and tar write */
  695. retval = mem_ap_setup_transfer(ap, CSW_8BIT | CSW_ADDRINC_PACKED, 0);
  696. if (retval != ERROR_OK)
  697. return retval;
  698. retval = dap_queue_ap_read(ap, MEM_AP_REG_CSW, &csw);
  699. if (retval != ERROR_OK)
  700. return retval;
  701. retval = dap_run(dap);
  702. if (retval != ERROR_OK)
  703. return retval;
  704. if (csw & CSW_ADDRINC_PACKED)
  705. ap->packed_transfers = true;
  706. else
  707. ap->packed_transfers = false;
  708. /* Packed transfers on TI BE-32 processors do not work correctly in
  709. * many cases. */
  710. if (dap->ti_be_32_quirks)
  711. ap->packed_transfers = false;
  712. LOG_DEBUG("MEM_AP Packed Transfers: %s",
  713. ap->packed_transfers ? "enabled" : "disabled");
  714. /* The ARM ADI spec leaves implementation-defined whether unaligned
  715. * memory accesses work, only work partially, or cause a sticky error.
  716. * On TI BE-32 processors, reads seem to return garbage in some bytes
  717. * and unaligned writes seem to cause a sticky error.
  718. * TODO: it would be nice to have a way to detect whether unaligned
  719. * operations are supported on other processors. */
  720. ap->unaligned_access_bad = dap->ti_be_32_quirks;
  721. LOG_DEBUG("MEM_AP CFG: large data %d, long address %d, big-endian %d",
  722. !!(cfg & MEM_AP_REG_CFG_LD), !!(cfg & MEM_AP_REG_CFG_LA), !!(cfg & MEM_AP_REG_CFG_BE));
  723. return ERROR_OK;
  724. }
  725. /**
  726. * Put the debug link into SWD mode, if the target supports it.
  727. * The link's initial mode may be either JTAG (for example,
  728. * with SWJ-DP after reset) or SWD.
  729. *
  730. * Note that targets using the JTAG-DP do not support SWD, and that
  731. * some targets which could otherwise support it may have been
  732. * configured to disable SWD signaling
  733. *
  734. * @param dap The DAP used
  735. * @return ERROR_OK or else a fault code.
  736. */
  737. int dap_to_swd(struct adiv5_dap *dap)
  738. {
  739. LOG_DEBUG("Enter SWD mode");
  740. return dap_send_sequence(dap, JTAG_TO_SWD);
  741. }
  742. /**
  743. * Put the debug link into JTAG mode, if the target supports it.
  744. * The link's initial mode may be either SWD or JTAG.
  745. *
  746. * Note that targets implemented with SW-DP do not support JTAG, and
  747. * that some targets which could otherwise support it may have been
  748. * configured to disable JTAG signaling
  749. *
  750. * @param dap The DAP used
  751. * @return ERROR_OK or else a fault code.
  752. */
  753. int dap_to_jtag(struct adiv5_dap *dap)
  754. {
  755. LOG_DEBUG("Enter JTAG mode");
  756. return dap_send_sequence(dap, SWD_TO_JTAG);
  757. }
  758. /* CID interpretation -- see ARM IHI 0029B section 3
  759. * and ARM IHI 0031A table 13-3.
  760. */
  761. static const char *class_description[16] = {
  762. "Reserved", "ROM table", "Reserved", "Reserved",
  763. "Reserved", "Reserved", "Reserved", "Reserved",
  764. "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
  765. "Reserved", "OptimoDE DESS",
  766. "Generic IP component", "PrimeCell or System component"
  767. };
  768. static bool is_dap_cid_ok(uint32_t cid)
  769. {
  770. return (cid & 0xffff0fff) == 0xb105000d;
  771. }
  772. /*
  773. * This function checks the ID for each access port to find the requested Access Port type
  774. */
  775. int dap_find_ap(struct adiv5_dap *dap, enum ap_type type_to_find, struct adiv5_ap **ap_out)
  776. {
  777. int ap_num;
  778. /* Maximum AP number is 255 since the SELECT register is 8 bits */
  779. for (ap_num = 0; ap_num <= DP_APSEL_MAX; ap_num++) {
  780. /* read the IDR register of the Access Port */
  781. uint32_t id_val = 0;
  782. int retval = dap_queue_ap_read(dap_ap(dap, ap_num), AP_REG_IDR, &id_val);
  783. if (retval != ERROR_OK)
  784. return retval;
  785. retval = dap_run(dap);
  786. /* IDR bits:
  787. * 31-28 : Revision
  788. * 27-24 : JEDEC bank (0x4 for ARM)
  789. * 23-17 : JEDEC code (0x3B for ARM)
  790. * 16-13 : Class (0b1000=Mem-AP)
  791. * 12-8 : Reserved
  792. * 7-4 : AP Variant (non-zero for JTAG-AP)
  793. * 3-0 : AP Type (0=JTAG-AP 1=AHB-AP 2=APB-AP 4=AXI-AP)
  794. */
  795. /* Reading register for a non-existent AP should not cause an error,
  796. * but just to be sure, try to continue searching if an error does happen.
  797. */
  798. if ((retval == ERROR_OK) && /* Register read success */
  799. ((id_val & IDR_JEP106) == IDR_JEP106_ARM) && /* Jedec codes match */
  800. ((id_val & IDR_TYPE) == type_to_find)) { /* type matches*/
  801. LOG_DEBUG("Found %s at AP index: %d (IDR=0x%08" PRIX32 ")",
  802. (type_to_find == AP_TYPE_AHB3_AP) ? "AHB3-AP" :
  803. (type_to_find == AP_TYPE_AHB5_AP) ? "AHB5-AP" :
  804. (type_to_find == AP_TYPE_APB_AP) ? "APB-AP" :
  805. (type_to_find == AP_TYPE_AXI_AP) ? "AXI-AP" :
  806. (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown",
  807. ap_num, id_val);
  808. *ap_out = &dap->ap[ap_num];
  809. return ERROR_OK;
  810. }
  811. }
  812. LOG_DEBUG("No %s found",
  813. (type_to_find == AP_TYPE_AHB3_AP) ? "AHB3-AP" :
  814. (type_to_find == AP_TYPE_AHB5_AP) ? "AHB5-AP" :
  815. (type_to_find == AP_TYPE_APB_AP) ? "APB-AP" :
  816. (type_to_find == AP_TYPE_AXI_AP) ? "AXI-AP" :
  817. (type_to_find == AP_TYPE_JTAG_AP) ? "JTAG-AP" : "Unknown");
  818. return ERROR_FAIL;
  819. }
  820. int dap_get_debugbase(struct adiv5_ap *ap,
  821. target_addr_t *dbgbase, uint32_t *apid)
  822. {
  823. struct adiv5_dap *dap = ap->dap;
  824. int retval;
  825. uint32_t baseptr_upper, baseptr_lower;
  826. baseptr_upper = 0;
  827. if (is_64bit_ap(ap)) {
  828. /* Read higher order 32-bits of base address */
  829. retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE64, &baseptr_upper);
  830. if (retval != ERROR_OK)
  831. return retval;
  832. }
  833. retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE, &baseptr_lower);
  834. if (retval != ERROR_OK)
  835. return retval;
  836. retval = dap_queue_ap_read(ap, AP_REG_IDR, apid);
  837. if (retval != ERROR_OK)
  838. return retval;
  839. retval = dap_run(dap);
  840. if (retval != ERROR_OK)
  841. return retval;
  842. *dbgbase = (((target_addr_t)baseptr_upper) << 32) | baseptr_lower;
  843. return ERROR_OK;
  844. }
  845. int dap_lookup_cs_component(struct adiv5_ap *ap,
  846. target_addr_t dbgbase, uint8_t type, target_addr_t *addr, int32_t *idx)
  847. {
  848. uint32_t romentry, entry_offset = 0, devtype;
  849. target_addr_t component_base;
  850. int retval;
  851. dbgbase &= 0xFFFFFFFFFFFFF000ull;
  852. *addr = 0;
  853. do {
  854. retval = mem_ap_read_atomic_u32(ap, dbgbase |
  855. entry_offset, &romentry);
  856. if (retval != ERROR_OK)
  857. return retval;
  858. component_base = dbgbase + (target_addr_t)(romentry & 0xFFFFF000);
  859. if (romentry & 0x1) {
  860. uint32_t c_cid1;
  861. retval = mem_ap_read_atomic_u32(ap, component_base | 0xff4, &c_cid1);
  862. if (retval != ERROR_OK) {
  863. LOG_ERROR("Can't read component with base address " TARGET_ADDR_FMT
  864. ", the corresponding core might be turned off", component_base);
  865. return retval;
  866. }
  867. if (((c_cid1 >> 4) & 0x0f) == 1) {
  868. retval = dap_lookup_cs_component(ap, component_base,
  869. type, addr, idx);
  870. if (retval == ERROR_OK)
  871. break;
  872. if (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
  873. return retval;
  874. }
  875. retval = mem_ap_read_atomic_u32(ap, component_base | 0xfcc, &devtype);
  876. if (retval != ERROR_OK)
  877. return retval;
  878. if ((devtype & 0xff) == type) {
  879. if (!*idx) {
  880. *addr = component_base;
  881. break;
  882. } else
  883. (*idx)--;
  884. }
  885. }
  886. entry_offset += 4;
  887. } while ((romentry > 0) && (entry_offset < 0xf00));
  888. if (!*addr)
  889. return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
  890. return ERROR_OK;
  891. }
  892. static int dap_read_part_id(struct adiv5_ap *ap, target_addr_t component_base, uint32_t *cid, uint64_t *pid)
  893. {
  894. assert((component_base & 0xFFF) == 0);
  895. assert(ap && cid && pid);
  896. uint32_t cid0, cid1, cid2, cid3;
  897. uint32_t pid0, pid1, pid2, pid3, pid4;
  898. int retval;
  899. /* IDs are in last 4K section */
  900. retval = mem_ap_read_u32(ap, component_base + 0xFE0, &pid0);
  901. if (retval != ERROR_OK)
  902. return retval;
  903. retval = mem_ap_read_u32(ap, component_base + 0xFE4, &pid1);
  904. if (retval != ERROR_OK)
  905. return retval;
  906. retval = mem_ap_read_u32(ap, component_base + 0xFE8, &pid2);
  907. if (retval != ERROR_OK)
  908. return retval;
  909. retval = mem_ap_read_u32(ap, component_base + 0xFEC, &pid3);
  910. if (retval != ERROR_OK)
  911. return retval;
  912. retval = mem_ap_read_u32(ap, component_base + 0xFD0, &pid4);
  913. if (retval != ERROR_OK)
  914. return retval;
  915. retval = mem_ap_read_u32(ap, component_base + 0xFF0, &cid0);
  916. if (retval != ERROR_OK)
  917. return retval;
  918. retval = mem_ap_read_u32(ap, component_base + 0xFF4, &cid1);
  919. if (retval != ERROR_OK)
  920. return retval;
  921. retval = mem_ap_read_u32(ap, component_base + 0xFF8, &cid2);
  922. if (retval != ERROR_OK)
  923. return retval;
  924. retval = mem_ap_read_u32(ap, component_base + 0xFFC, &cid3);
  925. if (retval != ERROR_OK)
  926. return retval;
  927. retval = dap_run(ap->dap);
  928. if (retval != ERROR_OK)
  929. return retval;
  930. *cid = (cid3 & 0xff) << 24
  931. | (cid2 & 0xff) << 16
  932. | (cid1 & 0xff) << 8
  933. | (cid0 & 0xff);
  934. *pid = (uint64_t)(pid4 & 0xff) << 32
  935. | (pid3 & 0xff) << 24
  936. | (pid2 & 0xff) << 16
  937. | (pid1 & 0xff) << 8
  938. | (pid0 & 0xff);
  939. return ERROR_OK;
  940. }
  941. /* The designer identity code is encoded as:
  942. * bits 11:8 : JEP106 Bank (number of continuation codes), only valid when bit 7 is 1.
  943. * bit 7 : Set when bits 6:0 represent a JEP106 ID and cleared when bits 6:0 represent
  944. * a legacy ASCII Identity Code.
  945. * bits 6:0 : JEP106 Identity Code (without parity) or legacy ASCII code according to bit 7.
  946. * JEP106 is a standard available from jedec.org
  947. */
  948. /* Part number interpretations are from Cortex
  949. * core specs, the CoreSight components TRM
  950. * (ARM DDI 0314H), CoreSight System Design
  951. * Guide (ARM DGI 0012D) and ETM specs; also
  952. * from chip observation (e.g. TI SDTI).
  953. */
  954. /* The legacy code only used the part number field to identify CoreSight peripherals.
  955. * This meant that the same part number from two different manufacturers looked the same.
  956. * It is desirable for all future additions to identify with both part number and JEP106.
  957. * "ANY_ID" is a wildcard (any JEP106) only to preserve legacy behavior for legacy entries.
  958. */
  959. #define ANY_ID 0x1000
  960. #define ARM_ID 0x4BB
  961. static const struct {
  962. uint16_t designer_id;
  963. uint16_t part_num;
  964. const char *type;
  965. const char *full;
  966. } dap_partnums[] = {
  967. { ARM_ID, 0x000, "Cortex-M3 SCS", "(System Control Space)", },
  968. { ARM_ID, 0x001, "Cortex-M3 ITM", "(Instrumentation Trace Module)", },
  969. { ARM_ID, 0x002, "Cortex-M3 DWT", "(Data Watchpoint and Trace)", },
  970. { ARM_ID, 0x003, "Cortex-M3 FPB", "(Flash Patch and Breakpoint)", },
  971. { ARM_ID, 0x008, "Cortex-M0 SCS", "(System Control Space)", },
  972. { ARM_ID, 0x00a, "Cortex-M0 DWT", "(Data Watchpoint and Trace)", },
  973. { ARM_ID, 0x00b, "Cortex-M0 BPU", "(Breakpoint Unit)", },
  974. { ARM_ID, 0x00c, "Cortex-M4 SCS", "(System Control Space)", },
  975. { ARM_ID, 0x00d, "CoreSight ETM11", "(Embedded Trace)", },
  976. { ARM_ID, 0x00e, "Cortex-M7 FPB", "(Flash Patch and Breakpoint)", },
  977. { ARM_ID, 0x470, "Cortex-M1 ROM", "(ROM Table)", },
  978. { ARM_ID, 0x471, "Cortex-M0 ROM", "(ROM Table)", },
  979. { ARM_ID, 0x490, "Cortex-A15 GIC", "(Generic Interrupt Controller)", },
  980. { ARM_ID, 0x4a1, "Cortex-A53 ROM", "(v8 Memory Map ROM Table)", },
  981. { ARM_ID, 0x4a2, "Cortex-A57 ROM", "(ROM Table)", },
  982. { ARM_ID, 0x4a3, "Cortex-A53 ROM", "(v7 Memory Map ROM Table)", },
  983. { ARM_ID, 0x4a4, "Cortex-A72 ROM", "(ROM Table)", },
  984. { ARM_ID, 0x4a9, "Cortex-A9 ROM", "(ROM Table)", },
  985. { ARM_ID, 0x4aa, "Cortex-A35 ROM", "(v8 Memory Map ROM Table)", },
  986. { ARM_ID, 0x4af, "Cortex-A15 ROM", "(ROM Table)", },
  987. { ARM_ID, 0x4b5, "Cortex-R5 ROM", "(ROM Table)", },
  988. { ARM_ID, 0x4c0, "Cortex-M0+ ROM", "(ROM Table)", },
  989. { ARM_ID, 0x4c3, "Cortex-M3 ROM", "(ROM Table)", },
  990. { ARM_ID, 0x4c4, "Cortex-M4 ROM", "(ROM Table)", },
  991. { ARM_ID, 0x4c7, "Cortex-M7 PPB ROM", "(Private Peripheral Bus ROM Table)", },
  992. { ARM_ID, 0x4c8, "Cortex-M7 ROM", "(ROM Table)", },
  993. { ARM_ID, 0x4e0, "Cortex-A35 ROM", "(v7 Memory Map ROM Table)", },
  994. { ARM_ID, 0x906, "CoreSight CTI", "(Cross Trigger)", },
  995. { ARM_ID, 0x907, "CoreSight ETB", "(Trace Buffer)", },
  996. { ARM_ID, 0x908, "CoreSight CSTF", "(Trace Funnel)", },
  997. { ARM_ID, 0x909, "CoreSight ATBR", "(Advanced Trace Bus Replicator)", },
  998. { ARM_ID, 0x910, "CoreSight ETM9", "(Embedded Trace)", },
  999. { ARM_ID, 0x912, "CoreSight TPIU", "(Trace Port Interface Unit)", },
  1000. { ARM_ID, 0x913, "CoreSight ITM", "(Instrumentation Trace Macrocell)", },
  1001. { ARM_ID, 0x914, "CoreSight SWO", "(Single Wire Output)", },
  1002. { ARM_ID, 0x917, "CoreSight HTM", "(AHB Trace Macrocell)", },
  1003. { ARM_ID, 0x920, "CoreSight ETM11", "(Embedded Trace)", },
  1004. { ARM_ID, 0x921, "Cortex-A8 ETM", "(Embedded Trace)", },
  1005. { ARM_ID, 0x922, "Cortex-A8 CTI", "(Cross Trigger)", },
  1006. { ARM_ID, 0x923, "Cortex-M3 TPIU", "(Trace Port Interface Unit)", },
  1007. { ARM_ID, 0x924, "Cortex-M3 ETM", "(Embedded Trace)", },
  1008. { ARM_ID, 0x925, "Cortex-M4 ETM", "(Embedded Trace)", },
  1009. { ARM_ID, 0x930, "Cortex-R4 ETM", "(Embedded Trace)", },
  1010. { ARM_ID, 0x931, "Cortex-R5 ETM", "(Embedded Trace)", },
  1011. { ARM_ID, 0x932, "CoreSight MTB-M0+", "(Micro Trace Buffer)", },
  1012. { ARM_ID, 0x941, "CoreSight TPIU-Lite", "(Trace Port Interface Unit)", },
  1013. { ARM_ID, 0x950, "Cortex-A9 PTM", "(Program Trace Macrocell)", },
  1014. { ARM_ID, 0x955, "Cortex-A5 ETM", "(Embedded Trace)", },
  1015. { ARM_ID, 0x95a, "Cortex-A72 ETM", "(Embedded Trace)", },
  1016. { ARM_ID, 0x95b, "Cortex-A17 PTM", "(Program Trace Macrocell)", },
  1017. { ARM_ID, 0x95d, "Cortex-A53 ETM", "(Embedded Trace)", },
  1018. { ARM_ID, 0x95e, "Cortex-A57 ETM", "(Embedded Trace)", },
  1019. { ARM_ID, 0x95f, "Cortex-A15 PTM", "(Program Trace Macrocell)", },
  1020. { ARM_ID, 0x961, "CoreSight TMC", "(Trace Memory Controller)", },
  1021. { ARM_ID, 0x962, "CoreSight STM", "(System Trace Macrocell)", },
  1022. { ARM_ID, 0x975, "Cortex-M7 ETM", "(Embedded Trace)", },
  1023. { ARM_ID, 0x9a0, "CoreSight PMU", "(Performance Monitoring Unit)", },
  1024. { ARM_ID, 0x9a1, "Cortex-M4 TPIU", "(Trace Port Interface Unit)", },
  1025. { ARM_ID, 0x9a4, "CoreSight GPR", "(Granular Power Requester)", },
  1026. { ARM_ID, 0x9a5, "Cortex-A5 PMU", "(Performance Monitor Unit)", },
  1027. { ARM_ID, 0x9a7, "Cortex-A7 PMU", "(Performance Monitor Unit)", },
  1028. { ARM_ID, 0x9a8, "Cortex-A53 CTI", "(Cross Trigger)", },
  1029. { ARM_ID, 0x9a9, "Cortex-M7 TPIU", "(Trace Port Interface Unit)", },
  1030. { ARM_ID, 0x9ae, "Cortex-A17 PMU", "(Performance Monitor Unit)", },
  1031. { ARM_ID, 0x9af, "Cortex-A15 PMU", "(Performance Monitor Unit)", },
  1032. { ARM_ID, 0x9b7, "Cortex-R7 PMU", "(Performance Monitor Unit)", },
  1033. { ARM_ID, 0x9d3, "Cortex-A53 PMU", "(Performance Monitor Unit)", },
  1034. { ARM_ID, 0x9d7, "Cortex-A57 PMU", "(Performance Monitor Unit)", },
  1035. { ARM_ID, 0x9d8, "Cortex-A72 PMU", "(Performance Monitor Unit)", },
  1036. { ARM_ID, 0x9da, "Cortex-A35 PMU/CTI/ETM", "(Performance Monitor Unit/Cross Trigger/ETM)", },
  1037. { ARM_ID, 0xc05, "Cortex-A5 Debug", "(Debug Unit)", },
  1038. { ARM_ID, 0xc07, "Cortex-A7 Debug", "(Debug Unit)", },
  1039. { ARM_ID, 0xc08, "Cortex-A8 Debug", "(Debug Unit)", },
  1040. { ARM_ID, 0xc09, "Cortex-A9 Debug", "(Debug Unit)", },
  1041. { ARM_ID, 0xc0e, "Cortex-A17 Debug", "(Debug Unit)", },
  1042. { ARM_ID, 0xc0f, "Cortex-A15 Debug", "(Debug Unit)", },
  1043. { ARM_ID, 0xc14, "Cortex-R4 Debug", "(Debug Unit)", },
  1044. { ARM_ID, 0xc15, "Cortex-R5 Debug", "(Debug Unit)", },
  1045. { ARM_ID, 0xc17, "Cortex-R7 Debug", "(Debug Unit)", },
  1046. { ARM_ID, 0xd03, "Cortex-A53 Debug", "(Debug Unit)", },
  1047. { ARM_ID, 0xd04, "Cortex-A35 Debug", "(Debug Unit)", },
  1048. { ARM_ID, 0xd07, "Cortex-A57 Debug", "(Debug Unit)", },
  1049. { ARM_ID, 0xd08, "Cortex-A72 Debug", "(Debug Unit)", },
  1050. { 0x097, 0x9af, "MSP432 ROM", "(ROM Table)" },
  1051. { 0x09f, 0xcd0, "Atmel CPU with DSU", "(CPU)" },
  1052. { 0x0c1, 0x1db, "XMC4500 ROM", "(ROM Table)" },
  1053. { 0x0c1, 0x1df, "XMC4700/4800 ROM", "(ROM Table)" },
  1054. { 0x0c1, 0x1ed, "XMC1000 ROM", "(ROM Table)" },
  1055. { 0x0E5, 0x000, "SHARC+/Blackfin+", "", },
  1056. { 0x0F0, 0x440, "Qualcomm QDSS Component v1", "(Qualcomm Designed CoreSight Component v1)", },
  1057. { 0x3eb, 0x181, "Tegra 186 ROM", "(ROM Table)", },
  1058. { 0x3eb, 0x202, "Denver ETM", "(Denver Embedded Trace)", },
  1059. { 0x3eb, 0x211, "Tegra 210 ROM", "(ROM Table)", },
  1060. { 0x3eb, 0x302, "Denver Debug", "(Debug Unit)", },
  1061. { 0x3eb, 0x402, "Denver PMU", "(Performance Monitor Unit)", },
  1062. /* legacy comment: 0x113: what? */
  1063. { ANY_ID, 0x120, "TI SDTI", "(System Debug Trace Interface)", }, /* from OMAP3 memmap */
  1064. { ANY_ID, 0x343, "TI DAPCTL", "", }, /* from OMAP3 memmap */
  1065. };
  1066. static int dap_rom_display(struct command_invocation *cmd,
  1067. struct adiv5_ap *ap, target_addr_t dbgbase, int depth)
  1068. {
  1069. int retval;
  1070. uint64_t pid;
  1071. uint32_t cid;
  1072. char tabs[16] = "";
  1073. if (depth > 16) {
  1074. command_print(cmd, "\tTables too deep");
  1075. return ERROR_FAIL;
  1076. }
  1077. if (depth)
  1078. snprintf(tabs, sizeof(tabs), "[L%02d] ", depth);
  1079. target_addr_t base_addr = dbgbase & 0xFFFFFFFFFFFFF000ull;
  1080. command_print(cmd, "\t\tComponent base address " TARGET_ADDR_FMT, base_addr);
  1081. retval = dap_read_part_id(ap, base_addr, &cid, &pid);
  1082. if (retval != ERROR_OK) {
  1083. command_print(cmd, "\t\tCan't read component, the corresponding core might be turned off");
  1084. return ERROR_OK; /* Don't abort recursion */
  1085. }
  1086. if (!is_dap_cid_ok(cid)) {
  1087. command_print(cmd, "\t\tInvalid CID 0x%08" PRIx32, cid);
  1088. return ERROR_OK; /* Don't abort recursion */
  1089. }
  1090. /* component may take multiple 4K pages */
  1091. uint32_t size = (pid >> 36) & 0xf;
  1092. if (size > 0)
  1093. command_print(cmd, "\t\tStart address " TARGET_ADDR_FMT, base_addr - 0x1000 * size);
  1094. command_print(cmd, "\t\tPeripheral ID 0x%010" PRIx64, pid);
  1095. uint8_t class = (cid >> 12) & 0xf;
  1096. uint16_t part_num = pid & 0xfff;
  1097. uint16_t designer_id = ((pid >> 32) & 0xf) << 8 | ((pid >> 12) & 0xff);
  1098. if (designer_id & 0x80) {
  1099. /* JEP106 code */
  1100. command_print(cmd, "\t\tDesigner is 0x%03" PRIx16 ", %s",
  1101. designer_id, jep106_manufacturer(designer_id >> 8, designer_id & 0x7f));
  1102. } else {
  1103. /* Legacy ASCII ID, clear invalid bits */
  1104. designer_id &= 0x7f;
  1105. command_print(cmd, "\t\tDesigner ASCII code 0x%02" PRIx16 ", %s",
  1106. designer_id, designer_id == 0x41 ? "ARM" : "<unknown>");
  1107. }
  1108. /* default values to be overwritten upon finding a match */
  1109. const char *type = "Unrecognized";
  1110. const char *full = "";
  1111. /* search dap_partnums[] array for a match */
  1112. for (unsigned entry = 0; entry < ARRAY_SIZE(dap_partnums); entry++) {
  1113. if ((dap_partnums[entry].designer_id != designer_id) && (dap_partnums[entry].designer_id != ANY_ID))
  1114. continue;
  1115. if (dap_partnums[entry].part_num != part_num)
  1116. continue;
  1117. type = dap_partnums[entry].type;
  1118. full = dap_partnums[entry].full;
  1119. break;
  1120. }
  1121. command_print(cmd, "\t\tPart is 0x%" PRIx16", %s %s", part_num, type, full);
  1122. command_print(cmd, "\t\tComponent class is 0x%" PRIx8 ", %s", class, class_description[class]);
  1123. if (class == 1) { /* ROM Table */
  1124. uint32_t memtype;
  1125. retval = mem_ap_read_atomic_u32(ap, base_addr | 0xFCC, &memtype);
  1126. if (retval != ERROR_OK)
  1127. return retval;
  1128. if (memtype & 0x01)
  1129. command_print(cmd, "\t\tMEMTYPE system memory present on bus");
  1130. else
  1131. command_print(cmd, "\t\tMEMTYPE system memory not present: dedicated debug bus");
  1132. /* Read ROM table entries from base address until we get 0x00000000 or reach the reserved area */
  1133. for (uint16_t entry_offset = 0; entry_offset < 0xF00; entry_offset += 4) {
  1134. uint32_t romentry;
  1135. retval = mem_ap_read_atomic_u32(ap, base_addr | entry_offset, &romentry);
  1136. if (retval != ERROR_OK)
  1137. return retval;
  1138. command_print(cmd, "\t%sROMTABLE[0x%x] = 0x%" PRIx32 "",
  1139. tabs, entry_offset, romentry);
  1140. if (romentry & 0x01) {
  1141. /* Recurse */
  1142. retval = dap_rom_display(cmd, ap, base_addr + (romentry & 0xFFFFF000), depth + 1);
  1143. if (retval != ERROR_OK)
  1144. return retval;
  1145. } else if (romentry != 0) {
  1146. command_print(cmd, "\t\tComponent not present");
  1147. } else {
  1148. command_print(cmd, "\t%s\tEnd of ROM table", tabs);
  1149. break;
  1150. }
  1151. }
  1152. } else if (class == 9) { /* CoreSight component */
  1153. const char *major = "Reserved", *subtype = "Reserved";
  1154. uint32_t devtype;
  1155. retval = mem_ap_read_atomic_u32(ap, base_addr | 0xFCC, &devtype);
  1156. if (retval != ERROR_OK)
  1157. return retval;
  1158. unsigned minor = (devtype >> 4) & 0x0f;
  1159. switch (devtype & 0x0f) {
  1160. case 0:
  1161. major = "Miscellaneous";
  1162. switch (minor) {
  1163. case 0:
  1164. subtype = "other";
  1165. break;
  1166. case 4:
  1167. subtype = "Validation component";
  1168. break;
  1169. }
  1170. break;
  1171. case 1:
  1172. major = "Trace Sink";
  1173. switch (minor) {
  1174. case 0:
  1175. subtype = "other";
  1176. break;
  1177. case 1:
  1178. subtype = "Port";
  1179. break;
  1180. case 2:
  1181. subtype = "Buffer";
  1182. break;
  1183. case 3:
  1184. subtype = "Router";
  1185. break;
  1186. }
  1187. break;
  1188. case 2:
  1189. major = "Trace Link";
  1190. switch (minor) {
  1191. case 0:
  1192. subtype = "other";
  1193. break;
  1194. case 1:
  1195. subtype = "Funnel, router";
  1196. break;
  1197. case 2:
  1198. subtype = "Filter";
  1199. break;
  1200. case 3:
  1201. subtype = "FIFO, buffer";
  1202. break;
  1203. }
  1204. break;
  1205. case 3:
  1206. major = "Trace Source";
  1207. switch (minor) {
  1208. case 0:
  1209. subtype = "other";
  1210. break;
  1211. case 1:
  1212. subtype = "Processor";
  1213. break;
  1214. case 2:
  1215. subtype = "DSP";
  1216. break;
  1217. case 3:
  1218. subtype = "Engine/Coprocessor";
  1219. break;
  1220. case 4:
  1221. subtype = "Bus";
  1222. break;
  1223. case 6:
  1224. subtype = "Software";
  1225. break;
  1226. }
  1227. break;
  1228. case 4:
  1229. major = "Debug Control";
  1230. switch (minor) {
  1231. case 0:
  1232. subtype = "other";
  1233. break;
  1234. case 1:
  1235. subtype = "Trigger Matrix";
  1236. break;
  1237. case 2:
  1238. subtype = "Debug Auth";
  1239. break;
  1240. case 3:
  1241. subtype = "Power Requestor";
  1242. break;
  1243. }
  1244. break;
  1245. case 5:
  1246. major = "Debug Logic";
  1247. switch (minor) {
  1248. case 0:
  1249. subtype = "other";
  1250. break;
  1251. case 1:
  1252. subtype = "Processor";
  1253. break;
  1254. case 2:
  1255. subtype = "DSP";
  1256. break;
  1257. case 3:
  1258. subtype = "Engine/Coprocessor";
  1259. break;
  1260. case 4:
  1261. subtype = "Bus";
  1262. break;
  1263. case 5:
  1264. subtype = "Memory";
  1265. break;
  1266. }
  1267. break;
  1268. case 6:
  1269. major = "Performance Monitor";
  1270. switch (minor) {
  1271. case 0:
  1272. subtype = "other";
  1273. break;
  1274. case 1:
  1275. subtype = "Processor";
  1276. break;
  1277. case 2:
  1278. subtype = "DSP";
  1279. break;
  1280. case 3:
  1281. subtype = "Engine/Coprocessor";
  1282. break;
  1283. case 4:
  1284. subtype = "Bus";
  1285. break;
  1286. case 5:
  1287. subtype = "Memory";
  1288. break;
  1289. }
  1290. break;
  1291. }
  1292. command_print(cmd, "\t\tType is 0x%02" PRIx8 ", %s, %s",
  1293. (uint8_t)(devtype & 0xff),
  1294. major, subtype);
  1295. /* REVISIT also show 0xfc8 DevId */
  1296. }
  1297. return ERROR_OK;
  1298. }
  1299. int dap_info_command(struct command_invocation *cmd,
  1300. struct adiv5_ap *ap)
  1301. {
  1302. int retval;
  1303. uint32_t apid;
  1304. target_addr_t dbgbase;
  1305. target_addr_t dbgaddr;
  1306. uint8_t mem_ap;
  1307. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  1308. retval = dap_get_debugbase(ap, &dbgbase, &apid);
  1309. if (retval != ERROR_OK)
  1310. return retval;
  1311. command_print(cmd, "AP ID register 0x%8.8" PRIx32, apid);
  1312. if (apid == 0) {
  1313. command_print(cmd, "No AP found at this ap 0x%x", ap->ap_num);
  1314. return ERROR_FAIL;
  1315. }
  1316. switch (apid & (IDR_JEP106 | IDR_TYPE)) {
  1317. case IDR_JEP106_ARM | AP_TYPE_JTAG_AP:
  1318. command_print(cmd, "\tType is JTAG-AP");
  1319. break;
  1320. case IDR_JEP106_ARM | AP_TYPE_AHB3_AP:
  1321. command_print(cmd, "\tType is MEM-AP AHB3");
  1322. break;
  1323. case IDR_JEP106_ARM | AP_TYPE_AHB5_AP:
  1324. command_print(cmd, "\tType is MEM-AP AHB5");
  1325. break;
  1326. case IDR_JEP106_ARM | AP_TYPE_APB_AP:
  1327. command_print(cmd, "\tType is MEM-AP APB");
  1328. break;
  1329. case IDR_JEP106_ARM | AP_TYPE_AXI_AP:
  1330. command_print(cmd, "\tType is MEM-AP AXI");
  1331. break;
  1332. default:
  1333. command_print(cmd, "\tUnknown AP type");
  1334. break;
  1335. }
  1336. /* NOTE: a MEM-AP may have a single CoreSight component that's
  1337. * not a ROM table ... or have no such components at all.
  1338. */
  1339. mem_ap = (apid & IDR_CLASS) == AP_CLASS_MEM_AP;
  1340. if (mem_ap) {
  1341. if (is_64bit_ap(ap))
  1342. dbgaddr = 0xFFFFFFFFFFFFFFFFull;
  1343. else
  1344. dbgaddr = 0xFFFFFFFFul;
  1345. command_print(cmd, "MEM-AP BASE " TARGET_ADDR_FMT, dbgbase);
  1346. if (dbgbase == dbgaddr || (dbgbase & 0x3) == 0x2) {
  1347. command_print(cmd, "\tNo ROM table present");
  1348. } else {
  1349. if (dbgbase & 0x01)
  1350. command_print(cmd, "\tValid ROM table present");
  1351. else
  1352. command_print(cmd, "\tROM table in legacy format");
  1353. dap_rom_display(cmd, ap, dbgbase & 0xFFFFFFFFFFFFF000ull, 0);
  1354. }
  1355. }
  1356. return ERROR_OK;
  1357. }
  1358. enum adiv5_cfg_param {
  1359. CFG_DAP,
  1360. CFG_AP_NUM,
  1361. CFG_BASEADDR,
  1362. CFG_CTIBASE, /* DEPRECATED */
  1363. };
  1364. static const struct jim_nvp nvp_config_opts[] = {
  1365. { .name = "-dap", .value = CFG_DAP },
  1366. { .name = "-ap-num", .value = CFG_AP_NUM },
  1367. { .name = "-baseaddr", .value = CFG_BASEADDR },
  1368. { .name = "-ctibase", .value = CFG_CTIBASE }, /* DEPRECATED */
  1369. { .name = NULL, .value = -1 }
  1370. };
  1371. static int adiv5_jim_spot_configure(struct jim_getopt_info *goi,
  1372. struct adiv5_dap **dap_p, int *ap_num_p, uint32_t *base_p)
  1373. {
  1374. if (!goi->argc)
  1375. return JIM_OK;
  1376. Jim_SetEmptyResult(goi->interp);
  1377. struct jim_nvp *n;
  1378. int e = jim_nvp_name2value_obj(goi->interp, nvp_config_opts,
  1379. goi->argv[0], &n);
  1380. if (e != JIM_OK)
  1381. return JIM_CONTINUE;
  1382. /* base_p can be NULL, then '-baseaddr' option is treated as unknown */
  1383. if (!base_p && (n->value == CFG_BASEADDR || n->value == CFG_CTIBASE))
  1384. return JIM_CONTINUE;
  1385. e = jim_getopt_obj(goi, NULL);
  1386. if (e != JIM_OK)
  1387. return e;
  1388. switch (n->value) {
  1389. case CFG_DAP:
  1390. if (goi->isconfigure) {
  1391. Jim_Obj *o_t;
  1392. struct adiv5_dap *dap;
  1393. e = jim_getopt_obj(goi, &o_t);
  1394. if (e != JIM_OK)
  1395. return e;
  1396. dap = dap_instance_by_jim_obj(goi->interp, o_t);
  1397. if (!dap) {
  1398. Jim_SetResultString(goi->interp, "DAP name invalid!", -1);
  1399. return JIM_ERR;
  1400. }
  1401. if (*dap_p && *dap_p != dap) {
  1402. Jim_SetResultString(goi->interp,
  1403. "DAP assignment cannot be changed!", -1);
  1404. return JIM_ERR;
  1405. }
  1406. *dap_p = dap;
  1407. } else {
  1408. if (goi->argc)
  1409. goto err_no_param;
  1410. if (!*dap_p) {
  1411. Jim_SetResultString(goi->interp, "DAP not configured", -1);
  1412. return JIM_ERR;
  1413. }
  1414. Jim_SetResultString(goi->interp, adiv5_dap_name(*dap_p), -1);
  1415. }
  1416. break;
  1417. case CFG_AP_NUM:
  1418. if (goi->isconfigure) {
  1419. jim_wide ap_num;
  1420. e = jim_getopt_wide(goi, &ap_num);
  1421. if (e != JIM_OK)
  1422. return e;
  1423. if (ap_num < 0 || ap_num > DP_APSEL_MAX) {
  1424. Jim_SetResultString(goi->interp, "Invalid AP number!", -1);
  1425. return JIM_ERR;
  1426. }
  1427. *ap_num_p = ap_num;
  1428. } else {
  1429. if (goi->argc)
  1430. goto err_no_param;
  1431. if (*ap_num_p == DP_APSEL_INVALID) {
  1432. Jim_SetResultString(goi->interp, "AP number not configured", -1);
  1433. return JIM_ERR;
  1434. }
  1435. Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, *ap_num_p));
  1436. }
  1437. break;
  1438. case CFG_CTIBASE:
  1439. LOG_WARNING("DEPRECATED! use \'-baseaddr' not \'-ctibase\'");
  1440. /* fall through */
  1441. case CFG_BASEADDR:
  1442. if (goi->isconfigure) {
  1443. jim_wide base;
  1444. e = jim_getopt_wide(goi, &base);
  1445. if (e != JIM_OK)
  1446. return e;
  1447. *base_p = (uint32_t)base;
  1448. } else {
  1449. if (goi->argc)
  1450. goto err_no_param;
  1451. Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, *base_p));
  1452. }
  1453. break;
  1454. };
  1455. return JIM_OK;
  1456. err_no_param:
  1457. Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
  1458. return JIM_ERR;
  1459. }
  1460. int adiv5_jim_configure(struct target *target, struct jim_getopt_info *goi)
  1461. {
  1462. struct adiv5_private_config *pc;
  1463. int e;
  1464. pc = (struct adiv5_private_config *)target->private_config;
  1465. if (!pc) {
  1466. pc = calloc(1, sizeof(struct adiv5_private_config));
  1467. pc->ap_num = DP_APSEL_INVALID;
  1468. target->private_config = pc;
  1469. }
  1470. target->has_dap = true;
  1471. e = adiv5_jim_spot_configure(goi, &pc->dap, &pc->ap_num, NULL);
  1472. if (e != JIM_OK)
  1473. return e;
  1474. if (pc->dap && !target->dap_configured) {
  1475. if (target->tap_configured) {
  1476. pc->dap = NULL;
  1477. Jim_SetResultString(goi->interp,
  1478. "-chain-position and -dap configparams are mutually exclusive!", -1);
  1479. return JIM_ERR;
  1480. }
  1481. target->tap = pc->dap->tap;
  1482. target->dap_configured = true;
  1483. }
  1484. return JIM_OK;
  1485. }
  1486. int adiv5_verify_config(struct adiv5_private_config *pc)
  1487. {
  1488. if (!pc)
  1489. return ERROR_FAIL;
  1490. if (!pc->dap)
  1491. return ERROR_FAIL;
  1492. return ERROR_OK;
  1493. }
  1494. int adiv5_jim_mem_ap_spot_configure(struct adiv5_mem_ap_spot *cfg,
  1495. struct jim_getopt_info *goi)
  1496. {
  1497. return adiv5_jim_spot_configure(goi, &cfg->dap, &cfg->ap_num, &cfg->base);
  1498. }
  1499. int adiv5_mem_ap_spot_init(struct adiv5_mem_ap_spot *p)
  1500. {
  1501. p->dap = NULL;
  1502. p->ap_num = DP_APSEL_INVALID;
  1503. p->base = 0;
  1504. return ERROR_OK;
  1505. }
  1506. COMMAND_HANDLER(handle_dap_info_command)
  1507. {
  1508. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1509. uint32_t apsel;
  1510. switch (CMD_ARGC) {
  1511. case 0:
  1512. apsel = dap->apsel;
  1513. break;
  1514. case 1:
  1515. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1516. if (apsel > DP_APSEL_MAX) {
  1517. command_print(CMD, "Invalid AP number");
  1518. return ERROR_COMMAND_ARGUMENT_INVALID;
  1519. }
  1520. break;
  1521. default:
  1522. return ERROR_COMMAND_SYNTAX_ERROR;
  1523. }
  1524. return dap_info_command(CMD, &dap->ap[apsel]);
  1525. }
  1526. COMMAND_HANDLER(dap_baseaddr_command)
  1527. {
  1528. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1529. uint32_t apsel, baseaddr_lower, baseaddr_upper;
  1530. struct adiv5_ap *ap;
  1531. target_addr_t baseaddr;
  1532. int retval;
  1533. baseaddr_upper = 0;
  1534. switch (CMD_ARGC) {
  1535. case 0:
  1536. apsel = dap->apsel;
  1537. break;
  1538. case 1:
  1539. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1540. /* AP address is in bits 31:24 of DP_SELECT */
  1541. if (apsel > DP_APSEL_MAX) {
  1542. command_print(CMD, "Invalid AP number");
  1543. return ERROR_COMMAND_ARGUMENT_INVALID;
  1544. }
  1545. break;
  1546. default:
  1547. return ERROR_COMMAND_SYNTAX_ERROR;
  1548. }
  1549. /* NOTE: assumes we're talking to a MEM-AP, which
  1550. * has a base address. There are other kinds of AP,
  1551. * though they're not common for now. This should
  1552. * use the ID register to verify it's a MEM-AP.
  1553. */
  1554. ap = dap_ap(dap, apsel);
  1555. retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE, &baseaddr_lower);
  1556. if (is_64bit_ap(ap) && retval == ERROR_OK)
  1557. retval = dap_queue_ap_read(ap, MEM_AP_REG_BASE64, &baseaddr_upper);
  1558. if (retval != ERROR_OK)
  1559. return retval;
  1560. retval = dap_run(dap);
  1561. if (retval != ERROR_OK)
  1562. return retval;
  1563. if (is_64bit_ap(ap)) {
  1564. baseaddr = (((target_addr_t)baseaddr_upper) << 32) | baseaddr_lower;
  1565. command_print(CMD, "0x%016" PRIx64, baseaddr);
  1566. } else
  1567. command_print(CMD, "0x%08" PRIx32, baseaddr_lower);
  1568. return retval;
  1569. }
  1570. COMMAND_HANDLER(dap_memaccess_command)
  1571. {
  1572. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1573. uint32_t memaccess_tck;
  1574. switch (CMD_ARGC) {
  1575. case 0:
  1576. memaccess_tck = dap->ap[dap->apsel].memaccess_tck;
  1577. break;
  1578. case 1:
  1579. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
  1580. break;
  1581. default:
  1582. return ERROR_COMMAND_SYNTAX_ERROR;
  1583. }
  1584. dap->ap[dap->apsel].memaccess_tck = memaccess_tck;
  1585. command_print(CMD, "memory bus access delay set to %" PRIu32 " tck",
  1586. dap->ap[dap->apsel].memaccess_tck);
  1587. return ERROR_OK;
  1588. }
  1589. COMMAND_HANDLER(dap_apsel_command)
  1590. {
  1591. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1592. uint32_t apsel;
  1593. switch (CMD_ARGC) {
  1594. case 0:
  1595. command_print(CMD, "%" PRIu32, dap->apsel);
  1596. return ERROR_OK;
  1597. case 1:
  1598. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1599. /* AP address is in bits 31:24 of DP_SELECT */
  1600. if (apsel > DP_APSEL_MAX) {
  1601. command_print(CMD, "Invalid AP number");
  1602. return ERROR_COMMAND_ARGUMENT_INVALID;
  1603. }
  1604. break;
  1605. default:
  1606. return ERROR_COMMAND_SYNTAX_ERROR;
  1607. }
  1608. dap->apsel = apsel;
  1609. return ERROR_OK;
  1610. }
  1611. COMMAND_HANDLER(dap_apcsw_command)
  1612. {
  1613. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1614. uint32_t apcsw = dap->ap[dap->apsel].csw_default;
  1615. uint32_t csw_val, csw_mask;
  1616. switch (CMD_ARGC) {
  1617. case 0:
  1618. command_print(CMD, "ap %" PRIu32 " selected, csw 0x%8.8" PRIx32,
  1619. dap->apsel, apcsw);
  1620. return ERROR_OK;
  1621. case 1:
  1622. if (strcmp(CMD_ARGV[0], "default") == 0)
  1623. csw_val = CSW_AHB_DEFAULT;
  1624. else
  1625. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], csw_val);
  1626. if (csw_val & (CSW_SIZE_MASK | CSW_ADDRINC_MASK)) {
  1627. LOG_ERROR("CSW value cannot include 'Size' and 'AddrInc' bit-fields");
  1628. return ERROR_COMMAND_ARGUMENT_INVALID;
  1629. }
  1630. apcsw = csw_val;
  1631. break;
  1632. case 2:
  1633. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], csw_val);
  1634. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], csw_mask);
  1635. if (csw_mask & (CSW_SIZE_MASK | CSW_ADDRINC_MASK)) {
  1636. LOG_ERROR("CSW mask cannot include 'Size' and 'AddrInc' bit-fields");
  1637. return ERROR_COMMAND_ARGUMENT_INVALID;
  1638. }
  1639. apcsw = (apcsw & ~csw_mask) | (csw_val & csw_mask);
  1640. break;
  1641. default:
  1642. return ERROR_COMMAND_SYNTAX_ERROR;
  1643. }
  1644. dap->ap[dap->apsel].csw_default = apcsw;
  1645. return 0;
  1646. }
  1647. COMMAND_HANDLER(dap_apid_command)
  1648. {
  1649. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1650. uint32_t apsel, apid;
  1651. int retval;
  1652. switch (CMD_ARGC) {
  1653. case 0:
  1654. apsel = dap->apsel;
  1655. break;
  1656. case 1:
  1657. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1658. /* AP address is in bits 31:24 of DP_SELECT */
  1659. if (apsel > DP_APSEL_MAX) {
  1660. command_print(CMD, "Invalid AP number");
  1661. return ERROR_COMMAND_ARGUMENT_INVALID;
  1662. }
  1663. break;
  1664. default:
  1665. return ERROR_COMMAND_SYNTAX_ERROR;
  1666. }
  1667. retval = dap_queue_ap_read(dap_ap(dap, apsel), AP_REG_IDR, &apid);
  1668. if (retval != ERROR_OK)
  1669. return retval;
  1670. retval = dap_run(dap);
  1671. if (retval != ERROR_OK)
  1672. return retval;
  1673. command_print(CMD, "0x%8.8" PRIx32, apid);
  1674. return retval;
  1675. }
  1676. COMMAND_HANDLER(dap_apreg_command)
  1677. {
  1678. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1679. uint32_t apsel, reg, value;
  1680. struct adiv5_ap *ap;
  1681. int retval;
  1682. if (CMD_ARGC < 2 || CMD_ARGC > 3)
  1683. return ERROR_COMMAND_SYNTAX_ERROR;
  1684. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1685. /* AP address is in bits 31:24 of DP_SELECT */
  1686. if (apsel > DP_APSEL_MAX) {
  1687. command_print(CMD, "Invalid AP number");
  1688. return ERROR_COMMAND_ARGUMENT_INVALID;
  1689. }
  1690. ap = dap_ap(dap, apsel);
  1691. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg);
  1692. if (reg >= 256 || (reg & 3)) {
  1693. command_print(CMD, "Invalid reg value (should be less than 256 and 4 bytes aligned)");
  1694. return ERROR_COMMAND_ARGUMENT_INVALID;
  1695. }
  1696. if (CMD_ARGC == 3) {
  1697. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
  1698. switch (reg) {
  1699. case MEM_AP_REG_CSW:
  1700. ap->csw_value = 0; /* invalid, in case write fails */
  1701. retval = dap_queue_ap_write(ap, reg, value);
  1702. if (retval == ERROR_OK)
  1703. ap->csw_value = value;
  1704. break;
  1705. case MEM_AP_REG_TAR:
  1706. retval = dap_queue_ap_write(ap, reg, value);
  1707. if (retval == ERROR_OK)
  1708. ap->tar_value = (ap->tar_value & ~0xFFFFFFFFull) | value;
  1709. else {
  1710. /* To track independent writes to TAR and TAR64, two tar_valid flags */
  1711. /* should be used. To keep it simple, tar_valid is only invalidated on a */
  1712. /* write fail. This approach causes a later re-write of the TAR and TAR64 */
  1713. /* if tar_valid is false. */
  1714. ap->tar_valid = false;
  1715. }
  1716. break;
  1717. case MEM_AP_REG_TAR64:
  1718. retval = dap_queue_ap_write(ap, reg, value);
  1719. if (retval == ERROR_OK)
  1720. ap->tar_value = (ap->tar_value & 0xFFFFFFFFull) | (((target_addr_t)value) << 32);
  1721. else {
  1722. /* See above comment for the MEM_AP_REG_TAR failed write case */
  1723. ap->tar_valid = false;
  1724. }
  1725. break;
  1726. default:
  1727. retval = dap_queue_ap_write(ap, reg, value);
  1728. break;
  1729. }
  1730. } else {
  1731. retval = dap_queue_ap_read(ap, reg, &value);
  1732. }
  1733. if (retval == ERROR_OK)
  1734. retval = dap_run(dap);
  1735. if (retval != ERROR_OK)
  1736. return retval;
  1737. if (CMD_ARGC == 2)
  1738. command_print(CMD, "0x%08" PRIx32, value);
  1739. return retval;
  1740. }
  1741. COMMAND_HANDLER(dap_dpreg_command)
  1742. {
  1743. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1744. uint32_t reg, value;
  1745. int retval;
  1746. if (CMD_ARGC < 1 || CMD_ARGC > 2)
  1747. return ERROR_COMMAND_SYNTAX_ERROR;
  1748. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], reg);
  1749. if (reg >= 256 || (reg & 3)) {
  1750. command_print(CMD, "Invalid reg value (should be less than 256 and 4 bytes aligned)");
  1751. return ERROR_COMMAND_ARGUMENT_INVALID;
  1752. }
  1753. if (CMD_ARGC == 2) {
  1754. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
  1755. retval = dap_queue_dp_write(dap, reg, value);
  1756. } else {
  1757. retval = dap_queue_dp_read(dap, reg, &value);
  1758. }
  1759. if (retval == ERROR_OK)
  1760. retval = dap_run(dap);
  1761. if (retval != ERROR_OK)
  1762. return retval;
  1763. if (CMD_ARGC == 1)
  1764. command_print(CMD, "0x%08" PRIx32, value);
  1765. return retval;
  1766. }
  1767. COMMAND_HANDLER(dap_ti_be_32_quirks_command)
  1768. {
  1769. struct adiv5_dap *dap = adiv5_get_dap(CMD_DATA);
  1770. return CALL_COMMAND_HANDLER(handle_command_parse_bool, &dap->ti_be_32_quirks,
  1771. "TI BE-32 quirks mode");
  1772. }
  1773. const struct command_registration dap_instance_commands[] = {
  1774. {
  1775. .name = "info",
  1776. .handler = handle_dap_info_command,
  1777. .mode = COMMAND_EXEC,
  1778. .help = "display ROM table for MEM-AP "
  1779. "(default currently selected AP)",
  1780. .usage = "[ap_num]",
  1781. },
  1782. {
  1783. .name = "apsel",
  1784. .handler = dap_apsel_command,
  1785. .mode = COMMAND_ANY,
  1786. .help = "Set the currently selected AP (default 0) "
  1787. "and display the result",
  1788. .usage = "[ap_num]",
  1789. },
  1790. {
  1791. .name = "apcsw",
  1792. .handler = dap_apcsw_command,
  1793. .mode = COMMAND_ANY,
  1794. .help = "Set CSW default bits",
  1795. .usage = "[value [mask]]",
  1796. },
  1797. {
  1798. .name = "apid",
  1799. .handler = dap_apid_command,
  1800. .mode = COMMAND_EXEC,
  1801. .help = "return ID register from AP "
  1802. "(default currently selected AP)",
  1803. .usage = "[ap_num]",
  1804. },
  1805. {
  1806. .name = "apreg",
  1807. .handler = dap_apreg_command,
  1808. .mode = COMMAND_EXEC,
  1809. .help = "read/write a register from AP "
  1810. "(reg is byte address of a word register, like 0 4 8...)",
  1811. .usage = "ap_num reg [value]",
  1812. },
  1813. {
  1814. .name = "dpreg",
  1815. .handler = dap_dpreg_command,
  1816. .mode = COMMAND_EXEC,
  1817. .help = "read/write a register from DP "
  1818. "(reg is byte address (bank << 4 | reg) of a word register, like 0 4 8...)",
  1819. .usage = "reg [value]",
  1820. },
  1821. {
  1822. .name = "baseaddr",
  1823. .handler = dap_baseaddr_command,
  1824. .mode = COMMAND_EXEC,
  1825. .help = "return debug base address from MEM-AP "
  1826. "(default currently selected AP)",
  1827. .usage = "[ap_num]",
  1828. },
  1829. {
  1830. .name = "memaccess",
  1831. .handler = dap_memaccess_command,
  1832. .mode = COMMAND_EXEC,
  1833. .help = "set/get number of extra tck for MEM-AP memory "
  1834. "bus access [0-255]",
  1835. .usage = "[cycles]",
  1836. },
  1837. {
  1838. .name = "ti_be_32_quirks",
  1839. .handler = dap_ti_be_32_quirks_command,
  1840. .mode = COMMAND_CONFIG,
  1841. .help = "set/get quirks mode for TI TMS450/TMS570 processors",
  1842. .usage = "[enable]",
  1843. },
  1844. COMMAND_REGISTRATION_DONE
  1845. };