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.
 
 
 
 
 
 

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