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.
 
 
 
 
 
 

1887 lines
50 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. * This program is free software; you can redistribute it and/or modify *
  14. * it under the terms of the GNU General Public License as published by *
  15. * the Free Software Foundation; either version 2 of the License, or *
  16. * (at your option) any later version. *
  17. * *
  18. * This program is distributed in the hope that it will be useful, *
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  21. * GNU General Public License for more details. *
  22. * *
  23. * You should have received a copy of the GNU General Public License *
  24. * along with this program; if not, write to the *
  25. * Free Software Foundation, Inc., *
  26. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  27. ***************************************************************************/
  28. /**
  29. * @file
  30. * This file implements support for the ARM Debug Interface version 5 (ADIv5)
  31. * debugging architecture. Compared with previous versions, this includes
  32. * a low pin-count Serial Wire Debug (SWD) alternative to JTAG for message
  33. * transport, and focusses on memory mapped resources as defined by the
  34. * CoreSight architecture.
  35. *
  36. * A key concept in ADIv5 is the Debug Access Port, or DAP. A DAP has two
  37. * basic components: a Debug Port (DP) transporting messages to and from a
  38. * debugger, and an Access Port (AP) accessing resources. Three types of DP
  39. * are defined. One uses only JTAG for communication, and is called JTAG-DP.
  40. * One uses only SWD for communication, and is called SW-DP. The third can
  41. * use either SWD or JTAG, and is called SWJ-DP. The most common type of AP
  42. * is used to access memory mapped resources and is called a MEM-AP. Also a
  43. * JTAG-AP is also defined, bridging to JTAG resources; those are uncommon.
  44. *
  45. * This programming interface allows DAP pipelined operations through a
  46. * transaction queue. This primarily affects AP operations (such as using
  47. * a MEM-AP to access memory or registers). If the current transaction has
  48. * not finished by the time the next one must begin, and the ORUNDETECT bit
  49. * is set in the DP_CTRL_STAT register, the SSTICKYORUN status is set and
  50. * further AP operations will fail. There are two basic methods to avoid
  51. * such overrun errors. One involves polling for status instead of using
  52. * transaction piplining. The other involves adding delays to ensure the
  53. * AP has enough time to complete one operation before starting the next
  54. * one. (For JTAG these delays are controlled by memaccess_tck.)
  55. */
  56. /*
  57. * Relevant specifications from ARM include:
  58. *
  59. * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031A
  60. * CoreSight(tm) v1.0 Architecture Specification ARM IHI 0029B
  61. *
  62. * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316D
  63. * Cortex-M3(tm) TRM, ARM DDI 0337G
  64. */
  65. #ifdef HAVE_CONFIG_H
  66. #include "config.h"
  67. #endif
  68. #include "jtag/interface.h"
  69. #include "arm.h"
  70. #include "arm_adi_v5.h"
  71. #include <helper/time_support.h>
  72. /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
  73. /*
  74. uint32_t tar_block_size(uint32_t address)
  75. Return the largest block starting at address that does not cross a tar block size alignment boundary
  76. */
  77. static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
  78. {
  79. return (tar_autoincr_block - ((tar_autoincr_block - 1) & address)) >> 2;
  80. }
  81. /***************************************************************************
  82. * *
  83. * DP and MEM-AP register access through APACC and DPACC *
  84. * *
  85. ***************************************************************************/
  86. /**
  87. * Select one of the APs connected to the specified DAP. The
  88. * selection is implicitly used with future AP transactions.
  89. * This is a NOP if the specified AP is already selected.
  90. *
  91. * @param dap The DAP
  92. * @param apsel Number of the AP to (implicitly) use with further
  93. * transactions. This normally identifies a MEM-AP.
  94. */
  95. void dap_ap_select(struct adiv5_dap *dap, uint8_t ap)
  96. {
  97. uint32_t new_ap = (ap << 24) & 0xFF000000;
  98. if (new_ap != dap->ap_current) {
  99. dap->ap_current = new_ap;
  100. /* Switching AP invalidates cached values.
  101. * Values MUST BE UPDATED BEFORE AP ACCESS.
  102. */
  103. dap->ap_bank_value = -1;
  104. dap->ap_csw_value = -1;
  105. dap->ap_tar_value = -1;
  106. }
  107. }
  108. /**
  109. * Queue transactions setting up transfer parameters for the
  110. * currently selected MEM-AP.
  111. *
  112. * Subsequent transfers using registers like AP_REG_DRW or AP_REG_BD2
  113. * initiate data reads or writes using memory or peripheral addresses.
  114. * If the CSW is configured for it, the TAR may be automatically
  115. * incremented after each transfer.
  116. *
  117. * @todo Rename to reflect it being specifically a MEM-AP function.
  118. *
  119. * @param dap The DAP connected to the MEM-AP.
  120. * @param csw MEM-AP Control/Status Word (CSW) register to assign. If this
  121. * matches the cached value, the register is not changed.
  122. * @param tar MEM-AP Transfer Address Register (TAR) to assign. If this
  123. * matches the cached address, the register is not changed.
  124. *
  125. * @return ERROR_OK if the transaction was properly queued, else a fault code.
  126. */
  127. int dap_setup_accessport(struct adiv5_dap *dap, uint32_t csw, uint32_t tar)
  128. {
  129. int retval;
  130. csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
  131. if (csw != dap->ap_csw_value) {
  132. /* LOG_DEBUG("DAP: Set CSW %x",csw); */
  133. retval = dap_queue_ap_write(dap, AP_REG_CSW, csw);
  134. if (retval != ERROR_OK)
  135. return retval;
  136. dap->ap_csw_value = csw;
  137. }
  138. if (tar != dap->ap_tar_value) {
  139. /* LOG_DEBUG("DAP: Set TAR %x",tar); */
  140. retval = dap_queue_ap_write(dap, AP_REG_TAR, tar);
  141. if (retval != ERROR_OK)
  142. return retval;
  143. dap->ap_tar_value = tar;
  144. }
  145. /* Disable TAR cache when autoincrementing */
  146. if (csw & CSW_ADDRINC_MASK)
  147. dap->ap_tar_value = -1;
  148. return ERROR_OK;
  149. }
  150. /**
  151. * Asynchronous (queued) read of a word from memory or a system register.
  152. *
  153. * @param dap The DAP connected to the MEM-AP performing the read.
  154. * @param address Address of the 32-bit word to read; it must be
  155. * readable by the currently selected MEM-AP.
  156. * @param value points to where the word will be stored when the
  157. * transaction queue is flushed (assuming no errors).
  158. *
  159. * @return ERROR_OK for success. Otherwise a fault code.
  160. */
  161. int mem_ap_read_u32(struct adiv5_dap *dap, uint32_t address,
  162. uint32_t *value)
  163. {
  164. int retval;
  165. /* Use banked addressing (REG_BDx) to avoid some link traffic
  166. * (updating TAR) when reading several consecutive addresses.
  167. */
  168. retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_OFF,
  169. address & 0xFFFFFFF0);
  170. if (retval != ERROR_OK)
  171. return retval;
  172. return dap_queue_ap_read(dap, AP_REG_BD0 | (address & 0xC), value);
  173. }
  174. /**
  175. * Synchronous read of a word from memory or a system register.
  176. * As a side effect, this flushes any queued transactions.
  177. *
  178. * @param dap The DAP connected to the MEM-AP performing the read.
  179. * @param address Address of the 32-bit word to read; it must be
  180. * readable by the currently selected MEM-AP.
  181. * @param value points to where the result will be stored.
  182. *
  183. * @return ERROR_OK for success; *value holds the result.
  184. * Otherwise a fault code.
  185. */
  186. int mem_ap_read_atomic_u32(struct adiv5_dap *dap, uint32_t address,
  187. uint32_t *value)
  188. {
  189. int retval;
  190. retval = mem_ap_read_u32(dap, address, value);
  191. if (retval != ERROR_OK)
  192. return retval;
  193. return dap_run(dap);
  194. }
  195. /**
  196. * Asynchronous (queued) write of a word to memory or a system register.
  197. *
  198. * @param dap The DAP connected to the MEM-AP.
  199. * @param address Address to be written; it must be writable by
  200. * the currently selected MEM-AP.
  201. * @param value Word that will be written to the address when transaction
  202. * queue is flushed (assuming no errors).
  203. *
  204. * @return ERROR_OK for success. Otherwise a fault code.
  205. */
  206. int mem_ap_write_u32(struct adiv5_dap *dap, uint32_t address,
  207. uint32_t value)
  208. {
  209. int retval;
  210. /* Use banked addressing (REG_BDx) to avoid some link traffic
  211. * (updating TAR) when writing several consecutive addresses.
  212. */
  213. retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_OFF,
  214. address & 0xFFFFFFF0);
  215. if (retval != ERROR_OK)
  216. return retval;
  217. return dap_queue_ap_write(dap, AP_REG_BD0 | (address & 0xC),
  218. value);
  219. }
  220. /**
  221. * Synchronous write of a word to memory or a system register.
  222. * As a side effect, this flushes any queued transactions.
  223. *
  224. * @param dap The DAP connected to the MEM-AP.
  225. * @param address Address to be written; it must be writable by
  226. * the currently selected MEM-AP.
  227. * @param value Word that will be written.
  228. *
  229. * @return ERROR_OK for success; the data was written. Otherwise a fault code.
  230. */
  231. int mem_ap_write_atomic_u32(struct adiv5_dap *dap, uint32_t address,
  232. uint32_t value)
  233. {
  234. int retval = mem_ap_write_u32(dap, address, value);
  235. if (retval != ERROR_OK)
  236. return retval;
  237. return dap_run(dap);
  238. }
  239. /*****************************************************************************
  240. * *
  241. * mem_ap_write_buf(struct adiv5_dap *dap, uint8_t *buffer, int count, uint32_t address) *
  242. * *
  243. * Write a buffer in target order (little endian) *
  244. * *
  245. *****************************************************************************/
  246. int mem_ap_write_buf_u32(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address)
  247. {
  248. int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
  249. uint32_t adr = address;
  250. const uint8_t *pBuffer = buffer;
  251. count >>= 2;
  252. wcount = count;
  253. /* if we have an unaligned access - reorder data */
  254. if (adr & 0x3u) {
  255. for (writecount = 0; writecount < count; writecount++) {
  256. int i;
  257. uint32_t outvalue;
  258. memcpy(&outvalue, pBuffer, sizeof(uint32_t));
  259. for (i = 0; i < 4; i++) {
  260. *((uint8_t *)pBuffer + (adr & 0x3)) = outvalue;
  261. outvalue >>= 8;
  262. adr++;
  263. }
  264. pBuffer += sizeof(uint32_t);
  265. }
  266. }
  267. while (wcount > 0) {
  268. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  269. blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
  270. if (wcount < blocksize)
  271. blocksize = wcount;
  272. /* handle unaligned data at 4k boundary */
  273. if (blocksize == 0)
  274. blocksize = 1;
  275. retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
  276. if (retval != ERROR_OK)
  277. return retval;
  278. for (writecount = 0; writecount < blocksize; writecount++) {
  279. retval = dap_queue_ap_write(dap, AP_REG_DRW,
  280. *(uint32_t *) ((void *) (buffer + 4 * writecount)));
  281. if (retval != ERROR_OK)
  282. break;
  283. }
  284. retval = dap_run(dap);
  285. if (retval == ERROR_OK) {
  286. wcount = wcount - blocksize;
  287. address = address + 4 * blocksize;
  288. buffer = buffer + 4 * blocksize;
  289. } else
  290. errorcount++;
  291. if (errorcount > 1) {
  292. LOG_WARNING("Block write error address 0x%" PRIx32 ", wcount 0x%x", address, wcount);
  293. return retval;
  294. }
  295. }
  296. return retval;
  297. }
  298. static int mem_ap_write_buf_packed_u16(struct adiv5_dap *dap,
  299. const uint8_t *buffer, int count, uint32_t address)
  300. {
  301. int retval = ERROR_OK;
  302. int wcount, blocksize, writecount, i;
  303. wcount = count >> 1;
  304. while (wcount > 0) {
  305. int nbytes;
  306. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  307. blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
  308. if (wcount < blocksize)
  309. blocksize = wcount;
  310. /* handle unaligned data at 4k boundary */
  311. if (blocksize == 0)
  312. blocksize = 1;
  313. retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  314. if (retval != ERROR_OK)
  315. return retval;
  316. writecount = blocksize;
  317. do {
  318. nbytes = MIN((writecount << 1), 4);
  319. if (nbytes < 4) {
  320. retval = mem_ap_write_buf_u16(dap, buffer,
  321. nbytes, address);
  322. if (retval != ERROR_OK) {
  323. LOG_WARNING("Block write error address "
  324. "0x%" PRIx32 ", count 0x%x",
  325. address, count);
  326. return retval;
  327. }
  328. address += nbytes >> 1;
  329. } else {
  330. uint32_t outvalue;
  331. memcpy(&outvalue, buffer, sizeof(uint32_t));
  332. for (i = 0; i < nbytes; i++) {
  333. *((uint8_t *)buffer + (address & 0x3)) = outvalue;
  334. outvalue >>= 8;
  335. address++;
  336. }
  337. memcpy(&outvalue, buffer, sizeof(uint32_t));
  338. retval = dap_queue_ap_write(dap,
  339. AP_REG_DRW, outvalue);
  340. if (retval != ERROR_OK)
  341. break;
  342. retval = dap_run(dap);
  343. if (retval != ERROR_OK) {
  344. LOG_WARNING("Block write error address "
  345. "0x%" PRIx32 ", count 0x%x",
  346. address, count);
  347. return retval;
  348. }
  349. }
  350. buffer += nbytes >> 1;
  351. writecount -= nbytes >> 1;
  352. } while (writecount);
  353. wcount -= blocksize;
  354. }
  355. return retval;
  356. }
  357. int mem_ap_write_buf_u16(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address)
  358. {
  359. int retval = ERROR_OK;
  360. if (count >= 4)
  361. return mem_ap_write_buf_packed_u16(dap, buffer, count, address);
  362. while (count > 0) {
  363. retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  364. if (retval != ERROR_OK)
  365. return retval;
  366. uint16_t svalue;
  367. memcpy(&svalue, buffer, sizeof(uint16_t));
  368. uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
  369. retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
  370. if (retval != ERROR_OK)
  371. break;
  372. retval = dap_run(dap);
  373. if (retval != ERROR_OK)
  374. break;
  375. count -= 2;
  376. address += 2;
  377. buffer += 2;
  378. }
  379. return retval;
  380. }
  381. static int mem_ap_write_buf_packed_u8(struct adiv5_dap *dap,
  382. const uint8_t *buffer, int count, uint32_t address)
  383. {
  384. int retval = ERROR_OK;
  385. int wcount, blocksize, writecount, i;
  386. wcount = count;
  387. while (wcount > 0) {
  388. int nbytes;
  389. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  390. blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
  391. if (wcount < blocksize)
  392. blocksize = wcount;
  393. retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  394. if (retval != ERROR_OK)
  395. return retval;
  396. writecount = blocksize;
  397. do {
  398. nbytes = MIN(writecount, 4);
  399. if (nbytes < 4) {
  400. retval = mem_ap_write_buf_u8(dap, buffer, nbytes, address);
  401. if (retval != ERROR_OK) {
  402. LOG_WARNING("Block write error address "
  403. "0x%" PRIx32 ", count 0x%x",
  404. address, count);
  405. return retval;
  406. }
  407. address += nbytes;
  408. } else {
  409. uint32_t outvalue;
  410. memcpy(&outvalue, buffer, sizeof(uint32_t));
  411. for (i = 0; i < nbytes; i++) {
  412. *((uint8_t *)buffer + (address & 0x3)) = outvalue;
  413. outvalue >>= 8;
  414. address++;
  415. }
  416. memcpy(&outvalue, buffer, sizeof(uint32_t));
  417. retval = dap_queue_ap_write(dap,
  418. AP_REG_DRW, outvalue);
  419. if (retval != ERROR_OK)
  420. break;
  421. retval = dap_run(dap);
  422. if (retval != ERROR_OK) {
  423. LOG_WARNING("Block write error address "
  424. "0x%" PRIx32 ", count 0x%x",
  425. address, count);
  426. return retval;
  427. }
  428. }
  429. buffer += nbytes;
  430. writecount -= nbytes;
  431. } while (writecount);
  432. wcount -= blocksize;
  433. }
  434. return retval;
  435. }
  436. int mem_ap_write_buf_u8(struct adiv5_dap *dap, const uint8_t *buffer, int count, uint32_t address)
  437. {
  438. int retval = ERROR_OK;
  439. if (count >= 4)
  440. return mem_ap_write_buf_packed_u8(dap, buffer, count, address);
  441. while (count > 0) {
  442. retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  443. if (retval != ERROR_OK)
  444. return retval;
  445. uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
  446. retval = dap_queue_ap_write(dap, AP_REG_DRW, outvalue);
  447. if (retval != ERROR_OK)
  448. break;
  449. retval = dap_run(dap);
  450. if (retval != ERROR_OK)
  451. break;
  452. count--;
  453. address++;
  454. buffer++;
  455. }
  456. return retval;
  457. }
  458. /* FIXME don't import ... this is a temporary workaround for the
  459. * mem_ap_read_buf_u32() mess, until it's no longer JTAG-specific.
  460. */
  461. extern int adi_jtag_dp_scan(struct adiv5_dap *dap,
  462. uint8_t instr, uint8_t reg_addr, uint8_t RnW,
  463. uint8_t *outvalue, uint8_t *invalue, uint8_t *ack);
  464. /**
  465. * Synchronously read a block of 32-bit words into a buffer
  466. * @param dap The DAP connected to the MEM-AP.
  467. * @param buffer where the words will be stored (in host byte order).
  468. * @param count How many words to read.
  469. * @param address Memory address from which to read words; all the
  470. * words must be readable by the currently selected MEM-AP.
  471. */
  472. int mem_ap_read_buf_u32(struct adiv5_dap *dap, uint8_t *buffer,
  473. int count, uint32_t address)
  474. {
  475. int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
  476. uint32_t adr = address;
  477. uint8_t *pBuffer = buffer;
  478. count >>= 2;
  479. wcount = count;
  480. while (wcount > 0) {
  481. /* Adjust to read blocks within boundaries aligned to the
  482. * TAR autoincrement size (at least 2^10). Autoincrement
  483. * mode avoids an extra per-word roundtrip to update TAR.
  484. */
  485. blocksize = max_tar_block_size(dap->tar_autoincr_block,
  486. address);
  487. if (wcount < blocksize)
  488. blocksize = wcount;
  489. /* handle unaligned data at 4k boundary */
  490. if (blocksize == 0)
  491. blocksize = 1;
  492. retval = dap_setup_accessport(dap, CSW_32BIT | CSW_ADDRINC_SINGLE,
  493. address);
  494. if (retval != ERROR_OK)
  495. return retval;
  496. /* FIXME remove these three calls to adi_jtag_dp_scan(),
  497. * so this routine becomes transport-neutral. Be careful
  498. * not to cause performance problems with JTAG; would it
  499. * suffice to loop over dap_queue_ap_read(), or would that
  500. * be slower when JTAG is the chosen transport?
  501. */
  502. /* Scan out first read */
  503. retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW,
  504. DPAP_READ, 0, NULL, NULL);
  505. if (retval != ERROR_OK)
  506. return retval;
  507. for (readcount = 0; readcount < blocksize - 1; readcount++) {
  508. /* Scan out next read; scan in posted value for the
  509. * previous one. Assumes read is acked "OK/FAULT",
  510. * and CTRL_STAT says that meant "OK".
  511. */
  512. retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, AP_REG_DRW,
  513. DPAP_READ, 0, buffer + 4 * readcount,
  514. &dap->ack);
  515. if (retval != ERROR_OK)
  516. return retval;
  517. }
  518. /* Scan in last posted value; RDBUFF has no other effect,
  519. * assuming ack is OK/FAULT and CTRL_STAT says "OK".
  520. */
  521. retval = adi_jtag_dp_scan(dap, JTAG_DP_DPACC, DP_RDBUFF,
  522. DPAP_READ, 0, buffer + 4 * readcount,
  523. &dap->ack);
  524. if (retval != ERROR_OK)
  525. return retval;
  526. retval = dap_run(dap);
  527. if (retval != ERROR_OK) {
  528. errorcount++;
  529. if (errorcount <= 1) {
  530. /* try again */
  531. continue;
  532. }
  533. LOG_WARNING("Block read error address 0x%" PRIx32, address);
  534. return retval;
  535. }
  536. wcount = wcount - blocksize;
  537. address += 4 * blocksize;
  538. buffer += 4 * blocksize;
  539. }
  540. /* if we have an unaligned access - reorder data */
  541. if (adr & 0x3u) {
  542. for (readcount = 0; readcount < count; readcount++) {
  543. int i;
  544. uint32_t data;
  545. memcpy(&data, pBuffer, sizeof(uint32_t));
  546. for (i = 0; i < 4; i++) {
  547. *((uint8_t *)pBuffer) =
  548. (data >> 8 * (adr & 0x3));
  549. pBuffer++;
  550. adr++;
  551. }
  552. }
  553. }
  554. return retval;
  555. }
  556. static int mem_ap_read_buf_packed_u16(struct adiv5_dap *dap,
  557. uint8_t *buffer, int count, uint32_t address)
  558. {
  559. uint32_t invalue;
  560. int retval = ERROR_OK;
  561. int wcount, blocksize, readcount, i;
  562. wcount = count >> 1;
  563. while (wcount > 0) {
  564. int nbytes;
  565. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  566. blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
  567. if (wcount < blocksize)
  568. blocksize = wcount;
  569. retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  570. if (retval != ERROR_OK)
  571. return retval;
  572. /* handle unaligned data at 4k boundary */
  573. if (blocksize == 0)
  574. blocksize = 1;
  575. readcount = blocksize;
  576. do {
  577. retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
  578. if (retval != ERROR_OK)
  579. return retval;
  580. retval = dap_run(dap);
  581. if (retval != ERROR_OK) {
  582. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  583. return retval;
  584. }
  585. nbytes = MIN((readcount << 1), 4);
  586. for (i = 0; i < nbytes; i++) {
  587. *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
  588. buffer++;
  589. address++;
  590. }
  591. readcount -= (nbytes >> 1);
  592. } while (readcount);
  593. wcount -= blocksize;
  594. }
  595. return retval;
  596. }
  597. /**
  598. * Synchronously read a block of 16-bit halfwords into a buffer
  599. * @param dap The DAP connected to the MEM-AP.
  600. * @param buffer where the halfwords will be stored (in host byte order).
  601. * @param count How many halfwords to read.
  602. * @param address Memory address from which to read words; all the
  603. * words must be readable by the currently selected MEM-AP.
  604. */
  605. int mem_ap_read_buf_u16(struct adiv5_dap *dap, uint8_t *buffer,
  606. int count, uint32_t address)
  607. {
  608. uint32_t invalue, i;
  609. int retval = ERROR_OK;
  610. if (count >= 4)
  611. return mem_ap_read_buf_packed_u16(dap, buffer, count, address);
  612. while (count > 0) {
  613. retval = dap_setup_accessport(dap, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  614. if (retval != ERROR_OK)
  615. return retval;
  616. retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
  617. if (retval != ERROR_OK)
  618. break;
  619. retval = dap_run(dap);
  620. if (retval != ERROR_OK)
  621. break;
  622. if (address & 0x1) {
  623. for (i = 0; i < 2; i++) {
  624. *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
  625. buffer++;
  626. address++;
  627. }
  628. } else {
  629. uint16_t svalue = (invalue >> 8 * (address & 0x3));
  630. memcpy(buffer, &svalue, sizeof(uint16_t));
  631. address += 2;
  632. buffer += 2;
  633. }
  634. count -= 2;
  635. }
  636. return retval;
  637. }
  638. /* FIX!!! is this a potential performance bottleneck w.r.t. requiring too many
  639. * roundtrips when jtag_execute_queue() has a large overhead(e.g. for USB)s?
  640. *
  641. * The solution is to arrange for a large out/in scan in this loop and
  642. * and convert data afterwards.
  643. */
  644. static int mem_ap_read_buf_packed_u8(struct adiv5_dap *dap,
  645. uint8_t *buffer, int count, uint32_t address)
  646. {
  647. uint32_t invalue;
  648. int retval = ERROR_OK;
  649. int wcount, blocksize, readcount, i;
  650. wcount = count;
  651. while (wcount > 0) {
  652. int nbytes;
  653. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  654. blocksize = max_tar_block_size(dap->tar_autoincr_block, address);
  655. if (wcount < blocksize)
  656. blocksize = wcount;
  657. retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  658. if (retval != ERROR_OK)
  659. return retval;
  660. readcount = blocksize;
  661. do {
  662. retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
  663. if (retval != ERROR_OK)
  664. return retval;
  665. retval = dap_run(dap);
  666. if (retval != ERROR_OK) {
  667. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  668. return retval;
  669. }
  670. nbytes = MIN(readcount, 4);
  671. for (i = 0; i < nbytes; i++) {
  672. *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
  673. buffer++;
  674. address++;
  675. }
  676. readcount -= nbytes;
  677. } while (readcount);
  678. wcount -= blocksize;
  679. }
  680. return retval;
  681. }
  682. /**
  683. * Synchronously read a block of bytes into a buffer
  684. * @param dap The DAP connected to the MEM-AP.
  685. * @param buffer where the bytes will be stored.
  686. * @param count How many bytes to read.
  687. * @param address Memory address from which to read data; all the
  688. * data must be readable by the currently selected MEM-AP.
  689. */
  690. int mem_ap_read_buf_u8(struct adiv5_dap *dap, uint8_t *buffer,
  691. int count, uint32_t address)
  692. {
  693. uint32_t invalue;
  694. int retval = ERROR_OK;
  695. if (count >= 4)
  696. return mem_ap_read_buf_packed_u8(dap, buffer, count, address);
  697. while (count > 0) {
  698. retval = dap_setup_accessport(dap, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  699. if (retval != ERROR_OK)
  700. return retval;
  701. retval = dap_queue_ap_read(dap, AP_REG_DRW, &invalue);
  702. if (retval != ERROR_OK)
  703. return retval;
  704. retval = dap_run(dap);
  705. if (retval != ERROR_OK)
  706. break;
  707. *((uint8_t *)buffer) = (invalue >> 8 * (address & 0x3));
  708. count--;
  709. address++;
  710. buffer++;
  711. }
  712. return retval;
  713. }
  714. /*--------------------------------------------------------------------*/
  715. /* Wrapping function with selection of AP */
  716. /*--------------------------------------------------------------------*/
  717. int mem_ap_sel_read_u32(struct adiv5_dap *swjdp, uint8_t ap,
  718. uint32_t address, uint32_t *value)
  719. {
  720. dap_ap_select(swjdp, ap);
  721. return mem_ap_read_u32(swjdp, address, value);
  722. }
  723. int mem_ap_sel_write_u32(struct adiv5_dap *swjdp, uint8_t ap,
  724. uint32_t address, uint32_t value)
  725. {
  726. dap_ap_select(swjdp, ap);
  727. return mem_ap_write_u32(swjdp, address, value);
  728. }
  729. int mem_ap_sel_read_atomic_u32(struct adiv5_dap *swjdp, uint8_t ap,
  730. uint32_t address, uint32_t *value)
  731. {
  732. dap_ap_select(swjdp, ap);
  733. return mem_ap_read_atomic_u32(swjdp, address, value);
  734. }
  735. int mem_ap_sel_write_atomic_u32(struct adiv5_dap *swjdp, uint8_t ap,
  736. uint32_t address, uint32_t value)
  737. {
  738. dap_ap_select(swjdp, ap);
  739. return mem_ap_write_atomic_u32(swjdp, address, value);
  740. }
  741. int mem_ap_sel_read_buf_u8(struct adiv5_dap *swjdp, uint8_t ap,
  742. uint8_t *buffer, int count, uint32_t address)
  743. {
  744. dap_ap_select(swjdp, ap);
  745. return mem_ap_read_buf_u8(swjdp, buffer, count, address);
  746. }
  747. int mem_ap_sel_read_buf_u16(struct adiv5_dap *swjdp, uint8_t ap,
  748. uint8_t *buffer, int count, uint32_t address)
  749. {
  750. dap_ap_select(swjdp, ap);
  751. return mem_ap_read_buf_u16(swjdp, buffer, count, address);
  752. }
  753. int mem_ap_sel_read_buf_u32(struct adiv5_dap *swjdp, uint8_t ap,
  754. uint8_t *buffer, int count, uint32_t address)
  755. {
  756. dap_ap_select(swjdp, ap);
  757. return mem_ap_read_buf_u32(swjdp, buffer, count, address);
  758. }
  759. int mem_ap_sel_write_buf_u8(struct adiv5_dap *swjdp, uint8_t ap,
  760. const uint8_t *buffer, int count, uint32_t address)
  761. {
  762. dap_ap_select(swjdp, ap);
  763. return mem_ap_write_buf_u8(swjdp, buffer, count, address);
  764. }
  765. int mem_ap_sel_write_buf_u16(struct adiv5_dap *swjdp, uint8_t ap,
  766. const uint8_t *buffer, int count, uint32_t address)
  767. {
  768. dap_ap_select(swjdp, ap);
  769. return mem_ap_write_buf_u16(swjdp, buffer, count, address);
  770. }
  771. int mem_ap_sel_write_buf_u32(struct adiv5_dap *swjdp, uint8_t ap,
  772. const uint8_t *buffer, int count, uint32_t address)
  773. {
  774. dap_ap_select(swjdp, ap);
  775. return mem_ap_write_buf_u32(swjdp, buffer, count, address);
  776. }
  777. #define MDM_REG_STAT 0x00
  778. #define MDM_REG_CTRL 0x04
  779. #define MDM_REG_ID 0xfc
  780. #define MDM_STAT_FMEACK (1<<0)
  781. #define MDM_STAT_FREADY (1<<1)
  782. #define MDM_STAT_SYSSEC (1<<2)
  783. #define MDM_STAT_SYSRES (1<<3)
  784. #define MDM_STAT_FMEEN (1<<5)
  785. #define MDM_STAT_BACKDOOREN (1<<6)
  786. #define MDM_STAT_LPEN (1<<7)
  787. #define MDM_STAT_VLPEN (1<<8)
  788. #define MDM_STAT_LLSMODEXIT (1<<9)
  789. #define MDM_STAT_VLLSXMODEXIT (1<<10)
  790. #define MDM_STAT_CORE_HALTED (1<<16)
  791. #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
  792. #define MDM_STAT_CORESLEEPING (1<<18)
  793. #define MEM_CTRL_FMEIP (1<<0)
  794. #define MEM_CTRL_DBG_DIS (1<<1)
  795. #define MEM_CTRL_DBG_REQ (1<<2)
  796. #define MEM_CTRL_SYS_RES_REQ (1<<3)
  797. #define MEM_CTRL_CORE_HOLD_RES (1<<4)
  798. #define MEM_CTRL_VLLSX_DBG_REQ (1<<5)
  799. #define MEM_CTRL_VLLSX_DBG_ACK (1<<6)
  800. #define MEM_CTRL_VLLSX_STAT_ACK (1<<7)
  801. /**
  802. *
  803. */
  804. int dap_syssec_kinetis_mdmap(struct adiv5_dap *dap)
  805. {
  806. uint32_t val;
  807. int retval;
  808. enum reset_types jtag_reset_config = jtag_get_reset_config();
  809. dap_ap_select(dap, 1);
  810. /* first check mdm-ap id register */
  811. retval = dap_queue_ap_read(dap, MDM_REG_ID, &val);
  812. if (retval != ERROR_OK)
  813. return retval;
  814. dap_run(dap);
  815. if (val != 0x001C0000) {
  816. LOG_DEBUG("id doesn't match %08X != 0x001C0000", val);
  817. dap_ap_select(dap, 0);
  818. return ERROR_FAIL;
  819. }
  820. /* read and parse status register
  821. * it's important that the device is out of
  822. * reset here
  823. */
  824. retval = dap_queue_ap_read(dap, MDM_REG_STAT, &val);
  825. if (retval != ERROR_OK)
  826. return retval;
  827. dap_run(dap);
  828. LOG_DEBUG("MDM_REG_STAT %08X", val);
  829. if ((val & (MDM_STAT_SYSSEC|MDM_STAT_FREADY)) != (MDM_STAT_FREADY)) {
  830. LOG_DEBUG("MDMAP: system is secured, masserase needed");
  831. if (!(val & MDM_STAT_FMEEN))
  832. LOG_DEBUG("MDMAP: masserase is disabled");
  833. else {
  834. /* we need to assert reset */
  835. if (jtag_reset_config & RESET_HAS_SRST) {
  836. /* default to asserting srst */
  837. adapter_assert_reset();
  838. } else {
  839. LOG_DEBUG("SRST not configured");
  840. dap_ap_select(dap, 0);
  841. return ERROR_FAIL;
  842. }
  843. while (1) {
  844. retval = dap_queue_ap_write(dap, MDM_REG_CTRL, MEM_CTRL_FMEIP);
  845. if (retval != ERROR_OK)
  846. return retval;
  847. dap_run(dap);
  848. /* read status register and wait for ready */
  849. retval = dap_queue_ap_read(dap, MDM_REG_STAT, &val);
  850. if (retval != ERROR_OK)
  851. return retval;
  852. dap_run(dap);
  853. LOG_DEBUG("MDM_REG_STAT %08X", val);
  854. if ((val & 1))
  855. break;
  856. }
  857. while (1) {
  858. retval = dap_queue_ap_write(dap, MDM_REG_CTRL, 0);
  859. if (retval != ERROR_OK)
  860. return retval;
  861. dap_run(dap);
  862. /* read status register */
  863. retval = dap_queue_ap_read(dap, MDM_REG_STAT, &val);
  864. if (retval != ERROR_OK)
  865. return retval;
  866. dap_run(dap);
  867. LOG_DEBUG("MDM_REG_STAT %08X", val);
  868. /* read control register and wait for ready */
  869. retval = dap_queue_ap_read(dap, MDM_REG_CTRL, &val);
  870. if (retval != ERROR_OK)
  871. return retval;
  872. dap_run(dap);
  873. LOG_DEBUG("MDM_REG_CTRL %08X", val);
  874. if (val == 0x00)
  875. break;
  876. }
  877. }
  878. }
  879. dap_ap_select(dap, 0);
  880. return ERROR_OK;
  881. }
  882. /** */
  883. struct dap_syssec_filter {
  884. /** */
  885. uint32_t idcode;
  886. /** */
  887. int (*dap_init)(struct adiv5_dap *dap);
  888. };
  889. /** */
  890. static struct dap_syssec_filter dap_syssec_filter_data[] = {
  891. { 0x4BA00477, dap_syssec_kinetis_mdmap }
  892. };
  893. /**
  894. *
  895. */
  896. int dap_syssec(struct adiv5_dap *dap)
  897. {
  898. unsigned int i;
  899. struct jtag_tap *tap;
  900. for (i = 0; i < sizeof(dap_syssec_filter_data); i++) {
  901. tap = dap->jtag_info->tap;
  902. while (tap != NULL) {
  903. if (tap->hasidcode && (dap_syssec_filter_data[i].idcode == tap->idcode)) {
  904. LOG_DEBUG("DAP: mdmap_init for idcode: %08x", tap->idcode);
  905. dap_syssec_filter_data[i].dap_init(dap);
  906. }
  907. tap = tap->next_tap;
  908. }
  909. }
  910. return ERROR_OK;
  911. }
  912. /*--------------------------------------------------------------------------*/
  913. /* FIXME don't import ... just initialize as
  914. * part of DAP transport setup
  915. */
  916. extern const struct dap_ops jtag_dp_ops;
  917. /*--------------------------------------------------------------------------*/
  918. /**
  919. * Initialize a DAP. This sets up the power domains, prepares the DP
  920. * for further use, and arranges to use AP #0 for all AP operations
  921. * until dap_ap-select() changes that policy.
  922. *
  923. * @param dap The DAP being initialized.
  924. *
  925. * @todo Rename this. We also need an initialization scheme which account
  926. * for SWD transports not just JTAG; that will need to address differences
  927. * in layering. (JTAG is useful without any debug target; but not SWD.)
  928. * And this may not even use an AHB-AP ... e.g. DAP-Lite uses an APB-AP.
  929. */
  930. int ahbap_debugport_init(struct adiv5_dap *dap)
  931. {
  932. uint32_t ctrlstat;
  933. int cnt = 0;
  934. int retval;
  935. LOG_DEBUG(" ");
  936. /* JTAG-DP or SWJ-DP, in JTAG mode
  937. * ... for SWD mode this is patched as part
  938. * of link switchover
  939. */
  940. if (!dap->ops)
  941. dap->ops = &jtag_dp_ops;
  942. /* Default MEM-AP setup.
  943. *
  944. * REVISIT AP #0 may be an inappropriate default for this.
  945. * Should we probe, or take a hint from the caller?
  946. * Presumably we can ignore the possibility of multiple APs.
  947. */
  948. dap->ap_current = !0;
  949. dap_ap_select(dap, 0);
  950. /* DP initialization */
  951. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  952. if (retval != ERROR_OK)
  953. return retval;
  954. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, SSTICKYERR);
  955. if (retval != ERROR_OK)
  956. return retval;
  957. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  958. if (retval != ERROR_OK)
  959. return retval;
  960. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  961. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  962. if (retval != ERROR_OK)
  963. return retval;
  964. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
  965. if (retval != ERROR_OK)
  966. return retval;
  967. retval = dap_run(dap);
  968. if (retval != ERROR_OK)
  969. return retval;
  970. /* Check that we have debug power domains activated */
  971. while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10)) {
  972. LOG_DEBUG("DAP: wait CDBGPWRUPACK");
  973. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
  974. if (retval != ERROR_OK)
  975. return retval;
  976. retval = dap_run(dap);
  977. if (retval != ERROR_OK)
  978. return retval;
  979. alive_sleep(10);
  980. }
  981. while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10)) {
  982. LOG_DEBUG("DAP: wait CSYSPWRUPACK");
  983. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
  984. if (retval != ERROR_OK)
  985. return retval;
  986. retval = dap_run(dap);
  987. if (retval != ERROR_OK)
  988. return retval;
  989. alive_sleep(10);
  990. }
  991. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  992. if (retval != ERROR_OK)
  993. return retval;
  994. /* With debug power on we can activate OVERRUN checking */
  995. dap->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
  996. retval = dap_queue_dp_write(dap, DP_CTRL_STAT, dap->dp_ctrl_stat);
  997. if (retval != ERROR_OK)
  998. return retval;
  999. retval = dap_queue_dp_read(dap, DP_CTRL_STAT, NULL);
  1000. if (retval != ERROR_OK)
  1001. return retval;
  1002. dap_syssec(dap);
  1003. return ERROR_OK;
  1004. }
  1005. /* CID interpretation -- see ARM IHI 0029B section 3
  1006. * and ARM IHI 0031A table 13-3.
  1007. */
  1008. static const char *class_description[16] = {
  1009. "Reserved", "ROM table", "Reserved", "Reserved",
  1010. "Reserved", "Reserved", "Reserved", "Reserved",
  1011. "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
  1012. "Reserved", "OptimoDE DESS",
  1013. "Generic IP component", "PrimeCell or System component"
  1014. };
  1015. static bool is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
  1016. {
  1017. return cid3 == 0xb1 && cid2 == 0x05
  1018. && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
  1019. }
  1020. int dap_get_debugbase(struct adiv5_dap *dap, int ap,
  1021. uint32_t *out_dbgbase, uint32_t *out_apid)
  1022. {
  1023. uint32_t ap_old;
  1024. int retval;
  1025. uint32_t dbgbase, apid;
  1026. /* AP address is in bits 31:24 of DP_SELECT */
  1027. if (ap >= 256)
  1028. return ERROR_COMMAND_SYNTAX_ERROR;
  1029. ap_old = dap->ap_current;
  1030. dap_ap_select(dap, ap);
  1031. retval = dap_queue_ap_read(dap, AP_REG_BASE, &dbgbase);
  1032. if (retval != ERROR_OK)
  1033. return retval;
  1034. retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
  1035. if (retval != ERROR_OK)
  1036. return retval;
  1037. retval = dap_run(dap);
  1038. if (retval != ERROR_OK)
  1039. return retval;
  1040. /* Excavate the device ID code */
  1041. struct jtag_tap *tap = dap->jtag_info->tap;
  1042. while (tap != NULL) {
  1043. if (tap->hasidcode)
  1044. break;
  1045. tap = tap->next_tap;
  1046. }
  1047. if (tap == NULL || !tap->hasidcode)
  1048. return ERROR_OK;
  1049. dap_ap_select(dap, ap_old);
  1050. /* The asignment happens only here to prevent modification of these
  1051. * values before they are certain. */
  1052. *out_dbgbase = dbgbase;
  1053. *out_apid = apid;
  1054. return ERROR_OK;
  1055. }
  1056. int dap_lookup_cs_component(struct adiv5_dap *dap, int ap,
  1057. uint32_t dbgbase, uint8_t type, uint32_t *addr)
  1058. {
  1059. uint32_t ap_old;
  1060. uint32_t romentry, entry_offset = 0, component_base, devtype;
  1061. int retval = ERROR_FAIL;
  1062. if (ap >= 256)
  1063. return ERROR_COMMAND_SYNTAX_ERROR;
  1064. ap_old = dap->ap_current;
  1065. dap_ap_select(dap, ap);
  1066. do {
  1067. retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) |
  1068. entry_offset, &romentry);
  1069. if (retval != ERROR_OK)
  1070. return retval;
  1071. component_base = (dbgbase & 0xFFFFF000)
  1072. + (romentry & 0xFFFFF000);
  1073. if (romentry & 0x1) {
  1074. retval = mem_ap_read_atomic_u32(dap,
  1075. (component_base & 0xfffff000) | 0xfcc,
  1076. &devtype);
  1077. if (retval != ERROR_OK)
  1078. return retval;
  1079. if ((devtype & 0xff) == type) {
  1080. *addr = component_base;
  1081. retval = ERROR_OK;
  1082. break;
  1083. }
  1084. }
  1085. entry_offset += 4;
  1086. } while (romentry > 0);
  1087. dap_ap_select(dap, ap_old);
  1088. return retval;
  1089. }
  1090. static int dap_info_command(struct command_context *cmd_ctx,
  1091. struct adiv5_dap *dap, int ap)
  1092. {
  1093. int retval;
  1094. uint32_t dbgbase = 0, apid = 0; /* Silence gcc by initializing */
  1095. int romtable_present = 0;
  1096. uint8_t mem_ap;
  1097. uint32_t ap_old;
  1098. retval = dap_get_debugbase(dap, ap, &dbgbase, &apid);
  1099. if (retval != ERROR_OK)
  1100. return retval;
  1101. ap_old = dap->ap_current;
  1102. dap_ap_select(dap, ap);
  1103. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  1104. mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
  1105. command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
  1106. if (apid) {
  1107. switch (apid&0x0F) {
  1108. case 0:
  1109. command_print(cmd_ctx, "\tType is JTAG-AP");
  1110. break;
  1111. case 1:
  1112. command_print(cmd_ctx, "\tType is MEM-AP AHB");
  1113. break;
  1114. case 2:
  1115. command_print(cmd_ctx, "\tType is MEM-AP APB");
  1116. break;
  1117. default:
  1118. command_print(cmd_ctx, "\tUnknown AP type");
  1119. break;
  1120. }
  1121. /* NOTE: a MEM-AP may have a single CoreSight component that's
  1122. * not a ROM table ... or have no such components at all.
  1123. */
  1124. if (mem_ap)
  1125. command_print(cmd_ctx, "AP BASE 0x%8.8" PRIx32, dbgbase);
  1126. } else
  1127. command_print(cmd_ctx, "No AP found at this ap 0x%x", ap);
  1128. romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
  1129. if (romtable_present) {
  1130. uint32_t cid0, cid1, cid2, cid3, memtype, romentry;
  1131. uint16_t entry_offset;
  1132. /* bit 16 of apid indicates a memory access port */
  1133. if (dbgbase & 0x02)
  1134. command_print(cmd_ctx, "\tValid ROM table present");
  1135. else
  1136. command_print(cmd_ctx, "\tROM table in legacy format");
  1137. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  1138. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
  1139. if (retval != ERROR_OK)
  1140. return retval;
  1141. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
  1142. if (retval != ERROR_OK)
  1143. return retval;
  1144. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
  1145. if (retval != ERROR_OK)
  1146. return retval;
  1147. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
  1148. if (retval != ERROR_OK)
  1149. return retval;
  1150. retval = mem_ap_read_u32(dap, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
  1151. if (retval != ERROR_OK)
  1152. return retval;
  1153. retval = dap_run(dap);
  1154. if (retval != ERROR_OK)
  1155. return retval;
  1156. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  1157. command_print(cmd_ctx, "\tCID3 0x%2.2x"
  1158. ", CID2 0x%2.2x"
  1159. ", CID1 0x%2.2x"
  1160. ", CID0 0x%2.2x",
  1161. (unsigned) cid3, (unsigned)cid2,
  1162. (unsigned) cid1, (unsigned) cid0);
  1163. if (memtype & 0x01)
  1164. command_print(cmd_ctx, "\tMEMTYPE system memory present on bus");
  1165. else
  1166. command_print(cmd_ctx, "\tMEMTYPE System memory not present. "
  1167. "Dedicated debug bus.");
  1168. /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
  1169. entry_offset = 0;
  1170. do {
  1171. retval = mem_ap_read_atomic_u32(dap, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
  1172. if (retval != ERROR_OK)
  1173. return retval;
  1174. command_print(cmd_ctx, "\tROMTABLE[0x%x] = 0x%" PRIx32 "", entry_offset, romentry);
  1175. if (romentry & 0x01) {
  1176. uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
  1177. uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
  1178. uint32_t component_base;
  1179. unsigned part_num;
  1180. char *type, *full;
  1181. component_base = (dbgbase & 0xFFFFF000) + (romentry & 0xFFFFF000);
  1182. /* IDs are in last 4K section */
  1183. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE0, &c_pid0);
  1184. if (retval != ERROR_OK)
  1185. return retval;
  1186. c_pid0 &= 0xff;
  1187. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE4, &c_pid1);
  1188. if (retval != ERROR_OK)
  1189. return retval;
  1190. c_pid1 &= 0xff;
  1191. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFE8, &c_pid2);
  1192. if (retval != ERROR_OK)
  1193. return retval;
  1194. c_pid2 &= 0xff;
  1195. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFEC, &c_pid3);
  1196. if (retval != ERROR_OK)
  1197. return retval;
  1198. c_pid3 &= 0xff;
  1199. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFD0, &c_pid4);
  1200. if (retval != ERROR_OK)
  1201. return retval;
  1202. c_pid4 &= 0xff;
  1203. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF0, &c_cid0);
  1204. if (retval != ERROR_OK)
  1205. return retval;
  1206. c_cid0 &= 0xff;
  1207. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF4, &c_cid1);
  1208. if (retval != ERROR_OK)
  1209. return retval;
  1210. c_cid1 &= 0xff;
  1211. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFF8, &c_cid2);
  1212. if (retval != ERROR_OK)
  1213. return retval;
  1214. c_cid2 &= 0xff;
  1215. retval = mem_ap_read_atomic_u32(dap, component_base + 0xFFC, &c_cid3);
  1216. if (retval != ERROR_OK)
  1217. return retval;
  1218. c_cid3 &= 0xff;
  1219. command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32 ","
  1220. "start address 0x%" PRIx32, component_base,
  1221. /* component may take multiple 4K pages */
  1222. component_base - 0x1000*(c_pid4 >> 4));
  1223. command_print(cmd_ctx, "\t\tComponent class is 0x%x, %s",
  1224. (int) (c_cid1 >> 4) & 0xf,
  1225. /* See ARM IHI 0029B Table 3-3 */
  1226. class_description[(c_cid1 >> 4) & 0xf]);
  1227. /* CoreSight component? */
  1228. if (((c_cid1 >> 4) & 0x0f) == 9) {
  1229. uint32_t devtype;
  1230. unsigned minor;
  1231. char *major = "Reserved", *subtype = "Reserved";
  1232. retval = mem_ap_read_atomic_u32(dap,
  1233. (component_base & 0xfffff000) | 0xfcc,
  1234. &devtype);
  1235. if (retval != ERROR_OK)
  1236. return retval;
  1237. minor = (devtype >> 4) & 0x0f;
  1238. switch (devtype & 0x0f) {
  1239. case 0:
  1240. major = "Miscellaneous";
  1241. switch (minor) {
  1242. case 0:
  1243. subtype = "other";
  1244. break;
  1245. case 4:
  1246. subtype = "Validation component";
  1247. break;
  1248. }
  1249. break;
  1250. case 1:
  1251. major = "Trace Sink";
  1252. switch (minor) {
  1253. case 0:
  1254. subtype = "other";
  1255. break;
  1256. case 1:
  1257. subtype = "Port";
  1258. break;
  1259. case 2:
  1260. subtype = "Buffer";
  1261. break;
  1262. }
  1263. break;
  1264. case 2:
  1265. major = "Trace Link";
  1266. switch (minor) {
  1267. case 0:
  1268. subtype = "other";
  1269. break;
  1270. case 1:
  1271. subtype = "Funnel, router";
  1272. break;
  1273. case 2:
  1274. subtype = "Filter";
  1275. break;
  1276. case 3:
  1277. subtype = "FIFO, buffer";
  1278. break;
  1279. }
  1280. break;
  1281. case 3:
  1282. major = "Trace Source";
  1283. switch (minor) {
  1284. case 0:
  1285. subtype = "other";
  1286. break;
  1287. case 1:
  1288. subtype = "Processor";
  1289. break;
  1290. case 2:
  1291. subtype = "DSP";
  1292. break;
  1293. case 3:
  1294. subtype = "Engine/Coprocessor";
  1295. break;
  1296. case 4:
  1297. subtype = "Bus";
  1298. break;
  1299. }
  1300. break;
  1301. case 4:
  1302. major = "Debug Control";
  1303. switch (minor) {
  1304. case 0:
  1305. subtype = "other";
  1306. break;
  1307. case 1:
  1308. subtype = "Trigger Matrix";
  1309. break;
  1310. case 2:
  1311. subtype = "Debug Auth";
  1312. break;
  1313. }
  1314. break;
  1315. case 5:
  1316. major = "Debug Logic";
  1317. switch (minor) {
  1318. case 0:
  1319. subtype = "other";
  1320. break;
  1321. case 1:
  1322. subtype = "Processor";
  1323. break;
  1324. case 2:
  1325. subtype = "DSP";
  1326. break;
  1327. case 3:
  1328. subtype = "Engine/Coprocessor";
  1329. break;
  1330. }
  1331. break;
  1332. }
  1333. command_print(cmd_ctx, "\t\tType is 0x%2.2x, %s, %s",
  1334. (unsigned) (devtype & 0xff),
  1335. major, subtype);
  1336. /* REVISIT also show 0xfc8 DevId */
  1337. }
  1338. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  1339. command_print(cmd_ctx,
  1340. "\t\tCID3 0%2.2x"
  1341. ", CID2 0%2.2x"
  1342. ", CID1 0%2.2x"
  1343. ", CID0 0%2.2x",
  1344. (int) c_cid3,
  1345. (int) c_cid2,
  1346. (int)c_cid1,
  1347. (int)c_cid0);
  1348. command_print(cmd_ctx,
  1349. "\t\tPeripheral ID[4..0] = hex "
  1350. "%2.2x %2.2x %2.2x %2.2x %2.2x",
  1351. (int) c_pid4, (int) c_pid3, (int) c_pid2,
  1352. (int) c_pid1, (int) c_pid0);
  1353. /* Part number interpretations are from Cortex
  1354. * core specs, the CoreSight components TRM
  1355. * (ARM DDI 0314H), CoreSight System Design
  1356. * Guide (ARM DGI 0012D) and ETM specs; also
  1357. * from chip observation (e.g. TI SDTI).
  1358. */
  1359. part_num = (c_pid0 & 0xff);
  1360. part_num |= (c_pid1 & 0x0f) << 8;
  1361. switch (part_num) {
  1362. case 0x000:
  1363. type = "Cortex-M3 NVIC";
  1364. full = "(Interrupt Controller)";
  1365. break;
  1366. case 0x001:
  1367. type = "Cortex-M3 ITM";
  1368. full = "(Instrumentation Trace Module)";
  1369. break;
  1370. case 0x002:
  1371. type = "Cortex-M3 DWT";
  1372. full = "(Data Watchpoint and Trace)";
  1373. break;
  1374. case 0x003:
  1375. type = "Cortex-M3 FBP";
  1376. full = "(Flash Patch and Breakpoint)";
  1377. break;
  1378. case 0x00d:
  1379. type = "CoreSight ETM11";
  1380. full = "(Embedded Trace)";
  1381. break;
  1382. /* case 0x113: what? */
  1383. case 0x120: /* from OMAP3 memmap */
  1384. type = "TI SDTI";
  1385. full = "(System Debug Trace Interface)";
  1386. break;
  1387. case 0x343: /* from OMAP3 memmap */
  1388. type = "TI DAPCTL";
  1389. full = "";
  1390. break;
  1391. case 0x906:
  1392. type = "Coresight CTI";
  1393. full = "(Cross Trigger)";
  1394. break;
  1395. case 0x907:
  1396. type = "Coresight ETB";
  1397. full = "(Trace Buffer)";
  1398. break;
  1399. case 0x908:
  1400. type = "Coresight CSTF";
  1401. full = "(Trace Funnel)";
  1402. break;
  1403. case 0x910:
  1404. type = "CoreSight ETM9";
  1405. full = "(Embedded Trace)";
  1406. break;
  1407. case 0x912:
  1408. type = "Coresight TPIU";
  1409. full = "(Trace Port Interface Unit)";
  1410. break;
  1411. case 0x921:
  1412. type = "Cortex-A8 ETM";
  1413. full = "(Embedded Trace)";
  1414. break;
  1415. case 0x922:
  1416. type = "Cortex-A8 CTI";
  1417. full = "(Cross Trigger)";
  1418. break;
  1419. case 0x923:
  1420. type = "Cortex-M3 TPIU";
  1421. full = "(Trace Port Interface Unit)";
  1422. break;
  1423. case 0x924:
  1424. type = "Cortex-M3 ETM";
  1425. full = "(Embedded Trace)";
  1426. break;
  1427. case 0x930:
  1428. type = "Cortex-R4 ETM";
  1429. full = "(Embedded Trace)";
  1430. break;
  1431. case 0xc08:
  1432. type = "Cortex-A8 Debug";
  1433. full = "(Debug Unit)";
  1434. break;
  1435. default:
  1436. type = "-*- unrecognized -*-";
  1437. full = "";
  1438. break;
  1439. }
  1440. command_print(cmd_ctx, "\t\tPart is %s %s",
  1441. type, full);
  1442. } else {
  1443. if (romentry)
  1444. command_print(cmd_ctx, "\t\tComponent not present");
  1445. else
  1446. command_print(cmd_ctx, "\t\tEnd of ROM table");
  1447. }
  1448. entry_offset += 4;
  1449. } while (romentry > 0);
  1450. } else
  1451. command_print(cmd_ctx, "\tNo ROM table present");
  1452. dap_ap_select(dap, ap_old);
  1453. return ERROR_OK;
  1454. }
  1455. COMMAND_HANDLER(handle_dap_info_command)
  1456. {
  1457. struct target *target = get_current_target(CMD_CTX);
  1458. struct arm *arm = target_to_arm(target);
  1459. struct adiv5_dap *dap = arm->dap;
  1460. uint32_t apsel;
  1461. switch (CMD_ARGC) {
  1462. case 0:
  1463. apsel = dap->apsel;
  1464. break;
  1465. case 1:
  1466. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1467. break;
  1468. default:
  1469. return ERROR_COMMAND_SYNTAX_ERROR;
  1470. }
  1471. return dap_info_command(CMD_CTX, dap, apsel);
  1472. }
  1473. COMMAND_HANDLER(dap_baseaddr_command)
  1474. {
  1475. struct target *target = get_current_target(CMD_CTX);
  1476. struct arm *arm = target_to_arm(target);
  1477. struct adiv5_dap *dap = arm->dap;
  1478. uint32_t apsel, baseaddr;
  1479. int retval;
  1480. switch (CMD_ARGC) {
  1481. case 0:
  1482. apsel = dap->apsel;
  1483. break;
  1484. case 1:
  1485. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1486. /* AP address is in bits 31:24 of DP_SELECT */
  1487. if (apsel >= 256)
  1488. return ERROR_COMMAND_SYNTAX_ERROR;
  1489. break;
  1490. default:
  1491. return ERROR_COMMAND_SYNTAX_ERROR;
  1492. }
  1493. dap_ap_select(dap, apsel);
  1494. /* NOTE: assumes we're talking to a MEM-AP, which
  1495. * has a base address. There are other kinds of AP,
  1496. * though they're not common for now. This should
  1497. * use the ID register to verify it's a MEM-AP.
  1498. */
  1499. retval = dap_queue_ap_read(dap, AP_REG_BASE, &baseaddr);
  1500. if (retval != ERROR_OK)
  1501. return retval;
  1502. retval = dap_run(dap);
  1503. if (retval != ERROR_OK)
  1504. return retval;
  1505. command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
  1506. return retval;
  1507. }
  1508. COMMAND_HANDLER(dap_memaccess_command)
  1509. {
  1510. struct target *target = get_current_target(CMD_CTX);
  1511. struct arm *arm = target_to_arm(target);
  1512. struct adiv5_dap *dap = arm->dap;
  1513. uint32_t memaccess_tck;
  1514. switch (CMD_ARGC) {
  1515. case 0:
  1516. memaccess_tck = dap->memaccess_tck;
  1517. break;
  1518. case 1:
  1519. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
  1520. break;
  1521. default:
  1522. return ERROR_COMMAND_SYNTAX_ERROR;
  1523. }
  1524. dap->memaccess_tck = memaccess_tck;
  1525. command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
  1526. dap->memaccess_tck);
  1527. return ERROR_OK;
  1528. }
  1529. COMMAND_HANDLER(dap_apsel_command)
  1530. {
  1531. struct target *target = get_current_target(CMD_CTX);
  1532. struct arm *arm = target_to_arm(target);
  1533. struct adiv5_dap *dap = arm->dap;
  1534. uint32_t apsel, apid;
  1535. int retval;
  1536. switch (CMD_ARGC) {
  1537. case 0:
  1538. apsel = 0;
  1539. break;
  1540. case 1:
  1541. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1542. /* AP address is in bits 31:24 of DP_SELECT */
  1543. if (apsel >= 256)
  1544. return ERROR_COMMAND_SYNTAX_ERROR;
  1545. break;
  1546. default:
  1547. return ERROR_COMMAND_SYNTAX_ERROR;
  1548. }
  1549. dap->apsel = apsel;
  1550. dap_ap_select(dap, apsel);
  1551. retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
  1552. if (retval != ERROR_OK)
  1553. return retval;
  1554. retval = dap_run(dap);
  1555. if (retval != ERROR_OK)
  1556. return retval;
  1557. command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
  1558. apsel, apid);
  1559. return retval;
  1560. }
  1561. COMMAND_HANDLER(dap_apid_command)
  1562. {
  1563. struct target *target = get_current_target(CMD_CTX);
  1564. struct arm *arm = target_to_arm(target);
  1565. struct adiv5_dap *dap = arm->dap;
  1566. uint32_t apsel, apid;
  1567. int retval;
  1568. switch (CMD_ARGC) {
  1569. case 0:
  1570. apsel = dap->apsel;
  1571. break;
  1572. case 1:
  1573. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1574. /* AP address is in bits 31:24 of DP_SELECT */
  1575. if (apsel >= 256)
  1576. return ERROR_COMMAND_SYNTAX_ERROR;
  1577. break;
  1578. default:
  1579. return ERROR_COMMAND_SYNTAX_ERROR;
  1580. }
  1581. dap_ap_select(dap, apsel);
  1582. retval = dap_queue_ap_read(dap, AP_REG_IDR, &apid);
  1583. if (retval != ERROR_OK)
  1584. return retval;
  1585. retval = dap_run(dap);
  1586. if (retval != ERROR_OK)
  1587. return retval;
  1588. command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
  1589. return retval;
  1590. }
  1591. static const struct command_registration dap_commands[] = {
  1592. {
  1593. .name = "info",
  1594. .handler = handle_dap_info_command,
  1595. .mode = COMMAND_EXEC,
  1596. .help = "display ROM table for MEM-AP "
  1597. "(default currently selected AP)",
  1598. .usage = "[ap_num]",
  1599. },
  1600. {
  1601. .name = "apsel",
  1602. .handler = dap_apsel_command,
  1603. .mode = COMMAND_EXEC,
  1604. .help = "Set the currently selected AP (default 0) "
  1605. "and display the result",
  1606. .usage = "[ap_num]",
  1607. },
  1608. {
  1609. .name = "apid",
  1610. .handler = dap_apid_command,
  1611. .mode = COMMAND_EXEC,
  1612. .help = "return ID register from AP "
  1613. "(default currently selected AP)",
  1614. .usage = "[ap_num]",
  1615. },
  1616. {
  1617. .name = "baseaddr",
  1618. .handler = dap_baseaddr_command,
  1619. .mode = COMMAND_EXEC,
  1620. .help = "return debug base address from MEM-AP "
  1621. "(default currently selected AP)",
  1622. .usage = "[ap_num]",
  1623. },
  1624. {
  1625. .name = "memaccess",
  1626. .handler = dap_memaccess_command,
  1627. .mode = COMMAND_EXEC,
  1628. .help = "set/get number of extra tck for MEM-AP memory "
  1629. "bus access [0-255]",
  1630. .usage = "[cycles]",
  1631. },
  1632. COMMAND_REGISTRATION_DONE
  1633. };
  1634. const struct command_registration dap_command_handlers[] = {
  1635. {
  1636. .name = "dap",
  1637. .mode = COMMAND_EXEC,
  1638. .help = "DAP command group",
  1639. .usage = "",
  1640. .chain = dap_commands,
  1641. },
  1642. COMMAND_REGISTRATION_DONE
  1643. };