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.
 
 
 
 
 
 

1570 lines
44 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 by Oyvind Harboe *
  9. * oyvind.harboe@zylin.com *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. /**
  27. * @file
  28. * This file implements support for the ARM Debug Interface version 5 (ADIv5)
  29. * debugging architecture. Compared with previous versions, this includes
  30. * a low pin-count Serial Wire Debug (SWD) alternative to JTAG for message
  31. * transport, and focusses on memory mapped resources as defined by the
  32. * CoreSight architecture.
  33. *
  34. * A key concept in ADIv5 is the Debug Access Port, or DAP. A DAP has two
  35. * basic components: a Debug Port (DP) transporting messages to and from a
  36. * debugger, and an Access Port (AP) accessing resources. Three types of DP
  37. * are defined. One uses only JTAG for communication, and is called JTAG-DP.
  38. * One uses only SWD for communication, and is called SW-DP. The third can
  39. * use either SWD or JTAG, and is called SWJ-DP. The most common type of AP
  40. * is used to access memory mapped resources and is called a MEM-AP. Also a
  41. * JTAG-AP is also defined, bridging to JTAG resources; those are uncommon.
  42. */
  43. /*
  44. * Relevant specifications from ARM include:
  45. *
  46. * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031A
  47. * CoreSight(tm) v1.0 Architecture Specification ARM IHI 0029B
  48. *
  49. * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316D
  50. * Cortex-M3(tm) TRM, ARM DDI 0337G
  51. */
  52. #ifdef HAVE_CONFIG_H
  53. #include "config.h"
  54. #endif
  55. #include "arm_adi_v5.h"
  56. #include <helper/time_support.h>
  57. /*
  58. * Transaction Mode:
  59. * swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  60. * Uses Overrun checking mode and does not do actual JTAG send/receive or transaction
  61. * result checking until swjdp_end_transaction()
  62. * This must be done before using or deallocating any return variables.
  63. * swjdp->trans_mode == TRANS_MODE_ATOMIC
  64. * All reads and writes to the AHB bus are checked for valid completion, and return values
  65. * are immediatley available.
  66. */
  67. /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
  68. /*
  69. uint32_t tar_block_size(uint32_t address)
  70. Return the largest block starting at address that does not cross a tar block size alignment boundary
  71. */
  72. static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
  73. {
  74. return (tar_autoincr_block - ((tar_autoincr_block - 1) & address)) >> 2;
  75. }
  76. /***************************************************************************
  77. * *
  78. * DPACC and APACC scanchain access through JTAG-DP *
  79. * *
  80. ***************************************************************************/
  81. /* Scan out and in from target ordered uint8_t buffers */
  82. static int adi_jtag_dp_scan(struct swjdp_common *swjdp,
  83. uint8_t instr, uint8_t reg_addr, uint8_t RnW,
  84. uint8_t *outvalue, uint8_t *invalue, uint8_t *ack)
  85. {
  86. struct arm_jtag *jtag_info = swjdp->jtag_info;
  87. struct scan_field fields[2];
  88. uint8_t out_addr_buf;
  89. jtag_set_end_state(TAP_IDLE);
  90. arm_jtag_set_instr(jtag_info, instr, NULL);
  91. /* Add specified number of tck clocks before accessing memory bus */
  92. if ((instr == JTAG_DP_APACC)
  93. && ((reg_addr == AP_REG_DRW)
  94. || ((reg_addr & 0xF0) == AP_REG_BD0))
  95. && (swjdp->memaccess_tck != 0))
  96. jtag_add_runtest(swjdp->memaccess_tck, jtag_set_end_state(TAP_IDLE));
  97. fields[0].tap = jtag_info->tap;
  98. fields[0].num_bits = 3;
  99. buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
  100. fields[0].out_value = &out_addr_buf;
  101. fields[0].in_value = ack;
  102. fields[1].tap = jtag_info->tap;
  103. fields[1].num_bits = 32;
  104. fields[1].out_value = outvalue;
  105. fields[1].in_value = invalue;
  106. jtag_add_dr_scan(2, fields, jtag_get_end_state());
  107. return ERROR_OK;
  108. }
  109. /* Scan out and in from host ordered uint32_t variables */
  110. static int adi_jtag_dp_scan_u32(struct swjdp_common *swjdp,
  111. uint8_t instr, uint8_t reg_addr, uint8_t RnW,
  112. uint32_t outvalue, uint32_t *invalue, uint8_t *ack)
  113. {
  114. struct arm_jtag *jtag_info = swjdp->jtag_info;
  115. struct scan_field fields[2];
  116. uint8_t out_value_buf[4];
  117. uint8_t out_addr_buf;
  118. jtag_set_end_state(TAP_IDLE);
  119. arm_jtag_set_instr(jtag_info, instr, NULL);
  120. /* Add specified number of tck clocks before accessing memory bus */
  121. if ((instr == JTAG_DP_APACC)
  122. && ((reg_addr == AP_REG_DRW)
  123. || ((reg_addr & 0xF0) == AP_REG_BD0))
  124. && (swjdp->memaccess_tck != 0))
  125. jtag_add_runtest(swjdp->memaccess_tck, jtag_set_end_state(TAP_IDLE));
  126. fields[0].tap = jtag_info->tap;
  127. fields[0].num_bits = 3;
  128. buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
  129. fields[0].out_value = &out_addr_buf;
  130. fields[0].in_value = ack;
  131. fields[1].tap = jtag_info->tap;
  132. fields[1].num_bits = 32;
  133. buf_set_u32(out_value_buf, 0, 32, outvalue);
  134. fields[1].out_value = out_value_buf;
  135. fields[1].in_value = NULL;
  136. if (invalue)
  137. {
  138. fields[1].in_value = (uint8_t *)invalue;
  139. jtag_add_dr_scan(2, fields, jtag_get_end_state());
  140. jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t) invalue);
  141. } else
  142. {
  143. jtag_add_dr_scan(2, fields, jtag_get_end_state());
  144. }
  145. return ERROR_OK;
  146. }
  147. /* scan_inout_check adds one extra inscan for DPAP_READ commands to read variables */
  148. static int scan_inout_check(struct swjdp_common *swjdp,
  149. uint8_t instr, uint8_t reg_addr, uint8_t RnW,
  150. uint8_t *outvalue, uint8_t *invalue)
  151. {
  152. adi_jtag_dp_scan(swjdp, instr, reg_addr, RnW, outvalue, NULL, NULL);
  153. if ((RnW == DPAP_READ) && (invalue != NULL))
  154. adi_jtag_dp_scan(swjdp, JTAG_DP_DPACC,
  155. DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
  156. /* In TRANS_MODE_ATOMIC all JTAG_DP_APACC transactions wait for
  157. * ack = OK/FAULT and the check CTRL_STAT
  158. */
  159. if ((instr == JTAG_DP_APACC)
  160. && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
  161. return swjdp_transaction_endcheck(swjdp);
  162. return ERROR_OK;
  163. }
  164. static int scan_inout_check_u32(struct swjdp_common *swjdp,
  165. uint8_t instr, uint8_t reg_addr, uint8_t RnW,
  166. uint32_t outvalue, uint32_t *invalue)
  167. {
  168. /* Issue the read or write */
  169. adi_jtag_dp_scan_u32(swjdp, instr, reg_addr, RnW, outvalue, NULL, NULL);
  170. /* For reads, collect posted value; RDBUFF has no other effect.
  171. * Assumes read gets acked with OK/FAULT, and CTRL_STAT says "OK".
  172. */
  173. if ((RnW == DPAP_READ) && (invalue != NULL))
  174. adi_jtag_dp_scan_u32(swjdp, JTAG_DP_DPACC,
  175. DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
  176. /* In TRANS_MODE_ATOMIC all JTAG_DP_APACC transactions wait for
  177. * ack = OK/FAULT and then check CTRL_STAT
  178. */
  179. if ((instr == JTAG_DP_APACC)
  180. && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
  181. return swjdp_transaction_endcheck(swjdp);
  182. return ERROR_OK;
  183. }
  184. int swjdp_transaction_endcheck(struct swjdp_common *swjdp)
  185. {
  186. int retval;
  187. uint32_t ctrlstat;
  188. /* too expensive to call keep_alive() here */
  189. #if 0
  190. /* Danger!!!! BROKEN!!!! */
  191. scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
  192. DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  193. /* Danger!!!! BROKEN!!!! Why will jtag_execute_queue() fail here????
  194. R956 introduced the check on return value here and now Michael Schwingen reports
  195. that this code no longer works....
  196. https://lists.berlios.de/pipermail/openocd-development/2008-September/003107.html
  197. */
  198. if ((retval = jtag_execute_queue()) != ERROR_OK)
  199. {
  200. LOG_ERROR("BUG: Why does this fail the first time????");
  201. }
  202. /* Why??? second time it works??? */
  203. #endif
  204. /* Post CTRL/STAT read; discard any previous posted read value
  205. * but collect its ACK status.
  206. */
  207. scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
  208. DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  209. if ((retval = jtag_execute_queue()) != ERROR_OK)
  210. return retval;
  211. swjdp->ack = swjdp->ack & 0x7;
  212. if (swjdp->ack != 2)
  213. {
  214. long long then = timeval_ms();
  215. while (swjdp->ack != 2)
  216. {
  217. if (swjdp->ack == 1)
  218. {
  219. if ((timeval_ms()-then) > 1000)
  220. {
  221. /* NOTE: this would be a good spot
  222. * to use JTAG_DP_ABORT.
  223. */
  224. LOG_WARNING("Timeout (1000ms) waiting "
  225. "for ACK=OK/FAULT "
  226. "in JTAG-DP transaction");
  227. return ERROR_JTAG_DEVICE_ERROR;
  228. }
  229. }
  230. else
  231. {
  232. LOG_WARNING("Invalid ACK "
  233. "in JTAG-DP transaction");
  234. return ERROR_JTAG_DEVICE_ERROR;
  235. }
  236. scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
  237. DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  238. if ((retval = jtag_execute_queue()) != ERROR_OK)
  239. return retval;
  240. swjdp->ack = swjdp->ack & 0x7;
  241. }
  242. } else
  243. {
  244. /* common code path avoids fn to timeval_ms() */
  245. }
  246. /* Check for STICKYERR and STICKYORUN */
  247. if (ctrlstat & (SSTICKYORUN | SSTICKYERR))
  248. {
  249. LOG_DEBUG("swjdp: CTRL/STAT error 0x%" PRIx32 "", ctrlstat);
  250. /* Check power to debug regions */
  251. if ((ctrlstat & 0xf0000000) != 0xf0000000)
  252. {
  253. ahbap_debugport_init(swjdp);
  254. }
  255. else
  256. {
  257. uint32_t mem_ap_csw, mem_ap_tar;
  258. /* Print information about last AHBAP access */
  259. LOG_ERROR("AHBAP Cached values: dp_select 0x%" PRIx32 ", ap_csw 0x%" PRIx32 ", ap_tar 0x%" PRIx32 "", swjdp->dp_select_value, swjdp->ap_csw_value, swjdp->ap_tar_value);
  260. if (ctrlstat & SSTICKYORUN)
  261. LOG_ERROR("JTAG-DP OVERRUN - "
  262. "check clock or reduce jtag speed");
  263. if (ctrlstat & SSTICKYERR)
  264. LOG_ERROR("JTAG-DP STICKY ERROR");
  265. /* Clear Sticky Error Bits */
  266. scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
  267. DP_CTRL_STAT, DPAP_WRITE,
  268. swjdp->dp_ctrl_stat | SSTICKYORUN
  269. | SSTICKYERR, NULL);
  270. scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
  271. DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  272. if ((retval = jtag_execute_queue()) != ERROR_OK)
  273. return retval;
  274. LOG_DEBUG("swjdp: status 0x%" PRIx32 "", ctrlstat);
  275. dap_ap_read_reg_u32(swjdp, AP_REG_CSW, &mem_ap_csw);
  276. dap_ap_read_reg_u32(swjdp, AP_REG_TAR, &mem_ap_tar);
  277. if ((retval = jtag_execute_queue()) != ERROR_OK)
  278. return retval;
  279. LOG_ERROR("Read MEM_AP_CSW 0x%" PRIx32 ", MEM_AP_TAR 0x%" PRIx32 "", mem_ap_csw, mem_ap_tar);
  280. }
  281. if ((retval = jtag_execute_queue()) != ERROR_OK)
  282. return retval;
  283. return ERROR_JTAG_DEVICE_ERROR;
  284. }
  285. return ERROR_OK;
  286. }
  287. /***************************************************************************
  288. * *
  289. * DP and MEM-AP register access through APACC and DPACC *
  290. * *
  291. ***************************************************************************/
  292. static int dap_dp_write_reg(struct swjdp_common *swjdp,
  293. uint32_t value, uint8_t reg_addr)
  294. {
  295. return scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
  296. reg_addr, DPAP_WRITE, value, NULL);
  297. }
  298. static int dap_dp_read_reg(struct swjdp_common *swjdp,
  299. uint32_t *value, uint8_t reg_addr)
  300. {
  301. return scan_inout_check_u32(swjdp, JTAG_DP_DPACC,
  302. reg_addr, DPAP_READ, 0, value);
  303. }
  304. int dap_ap_select(struct swjdp_common *swjdp,uint8_t apsel)
  305. {
  306. uint32_t select;
  307. select = (apsel << 24) & 0xFF000000;
  308. if (select != swjdp->apsel)
  309. {
  310. swjdp->apsel = select;
  311. /* Switching AP invalidates cached values */
  312. swjdp->dp_select_value = -1;
  313. swjdp->ap_csw_value = -1;
  314. swjdp->ap_tar_value = -1;
  315. }
  316. return ERROR_OK;
  317. }
  318. static int dap_dp_bankselect(struct swjdp_common *swjdp, uint32_t ap_reg)
  319. {
  320. uint32_t select;
  321. select = (ap_reg & 0x000000F0);
  322. if (select != swjdp->dp_select_value)
  323. {
  324. dap_dp_write_reg(swjdp, select | swjdp->apsel, DP_SELECT);
  325. swjdp->dp_select_value = select;
  326. }
  327. return ERROR_OK;
  328. }
  329. static int dap_ap_write_reg(struct swjdp_common *swjdp,
  330. uint32_t reg_addr, uint8_t *out_value_buf)
  331. {
  332. dap_dp_bankselect(swjdp, reg_addr);
  333. scan_inout_check(swjdp, JTAG_DP_APACC, reg_addr,
  334. DPAP_WRITE, out_value_buf, NULL);
  335. return ERROR_OK;
  336. }
  337. int dap_ap_write_reg_u32(struct swjdp_common *swjdp, uint32_t reg_addr, uint32_t value)
  338. {
  339. uint8_t out_value_buf[4];
  340. buf_set_u32(out_value_buf, 0, 32, value);
  341. dap_dp_bankselect(swjdp, reg_addr);
  342. scan_inout_check(swjdp, JTAG_DP_APACC, reg_addr,
  343. DPAP_WRITE, out_value_buf, NULL);
  344. return ERROR_OK;
  345. }
  346. int dap_ap_read_reg_u32(struct swjdp_common *swjdp, uint32_t reg_addr, uint32_t *value)
  347. {
  348. dap_dp_bankselect(swjdp, reg_addr);
  349. scan_inout_check_u32(swjdp, JTAG_DP_APACC, reg_addr,
  350. DPAP_READ, 0, value);
  351. return ERROR_OK;
  352. }
  353. /***************************************************************************
  354. * *
  355. * AHB-AP access to memory and system registers on AHB bus *
  356. * *
  357. ***************************************************************************/
  358. int dap_setup_accessport(struct swjdp_common *swjdp, uint32_t csw, uint32_t tar)
  359. {
  360. csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
  361. if (csw != swjdp->ap_csw_value)
  362. {
  363. /* LOG_DEBUG("swjdp : Set CSW %x",csw); */
  364. dap_ap_write_reg_u32(swjdp, AP_REG_CSW, csw);
  365. swjdp->ap_csw_value = csw;
  366. }
  367. if (tar != swjdp->ap_tar_value)
  368. {
  369. /* LOG_DEBUG("swjdp : Set TAR %x",tar); */
  370. dap_ap_write_reg_u32(swjdp, AP_REG_TAR, tar);
  371. swjdp->ap_tar_value = tar;
  372. }
  373. if (csw & CSW_ADDRINC_MASK)
  374. {
  375. /* Do not cache TAR value when autoincrementing */
  376. swjdp->ap_tar_value = -1;
  377. }
  378. return ERROR_OK;
  379. }
  380. /*****************************************************************************
  381. * *
  382. * mem_ap_read_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t *value) *
  383. * *
  384. * Read a uint32_t value from memory or system register *
  385. * Functionally equivalent to target_read_u32(target, address, uint32_t *value), *
  386. * but with less overhead *
  387. *****************************************************************************/
  388. int mem_ap_read_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t *value)
  389. {
  390. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  391. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
  392. dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (address & 0xC), value);
  393. return ERROR_OK;
  394. }
  395. int mem_ap_read_atomic_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t *value)
  396. {
  397. mem_ap_read_u32(swjdp, address, value);
  398. return swjdp_transaction_endcheck(swjdp);
  399. }
  400. /*****************************************************************************
  401. * *
  402. * mem_ap_write_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t value) *
  403. * *
  404. * Write a uint32_t value to memory or memory mapped register *
  405. * *
  406. *****************************************************************************/
  407. int mem_ap_write_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t value)
  408. {
  409. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  410. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
  411. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (address & 0xC), value);
  412. return ERROR_OK;
  413. }
  414. int mem_ap_write_atomic_u32(struct swjdp_common *swjdp, uint32_t address, uint32_t value)
  415. {
  416. mem_ap_write_u32(swjdp, address, value);
  417. return swjdp_transaction_endcheck(swjdp);
  418. }
  419. /*****************************************************************************
  420. * *
  421. * mem_ap_write_buf(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address) *
  422. * *
  423. * Write a buffer in target order (little endian) *
  424. * *
  425. *****************************************************************************/
  426. int mem_ap_write_buf_u32(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
  427. {
  428. int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
  429. uint32_t adr = address;
  430. uint8_t* pBuffer = buffer;
  431. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  432. count >>= 2;
  433. wcount = count;
  434. /* if we have an unaligned access - reorder data */
  435. if (adr & 0x3u)
  436. {
  437. for (writecount = 0; writecount < count; writecount++)
  438. {
  439. int i;
  440. uint32_t outvalue;
  441. memcpy(&outvalue, pBuffer, sizeof(uint32_t));
  442. for (i = 0; i < 4; i++)
  443. {
  444. *((uint8_t*)pBuffer + (adr & 0x3)) = outvalue;
  445. outvalue >>= 8;
  446. adr++;
  447. }
  448. pBuffer += sizeof(uint32_t);
  449. }
  450. }
  451. while (wcount > 0)
  452. {
  453. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  454. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  455. if (wcount < blocksize)
  456. blocksize = wcount;
  457. /* handle unaligned data at 4k boundary */
  458. if (blocksize == 0)
  459. blocksize = 1;
  460. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
  461. for (writecount = 0; writecount < blocksize; writecount++)
  462. {
  463. dap_ap_write_reg(swjdp, AP_REG_DRW, buffer + 4 * writecount);
  464. }
  465. if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
  466. {
  467. wcount = wcount - blocksize;
  468. address = address + 4 * blocksize;
  469. buffer = buffer + 4 * blocksize;
  470. }
  471. else
  472. {
  473. errorcount++;
  474. }
  475. if (errorcount > 1)
  476. {
  477. LOG_WARNING("Block write error address 0x%" PRIx32 ", wcount 0x%x", address, wcount);
  478. return ERROR_JTAG_DEVICE_ERROR;
  479. }
  480. }
  481. return retval;
  482. }
  483. static int mem_ap_write_buf_packed_u16(struct swjdp_common *swjdp,
  484. uint8_t *buffer, int count, uint32_t address)
  485. {
  486. int retval = ERROR_OK;
  487. int wcount, blocksize, writecount, i;
  488. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  489. wcount = count >> 1;
  490. while (wcount > 0)
  491. {
  492. int nbytes;
  493. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  494. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  495. if (wcount < blocksize)
  496. blocksize = wcount;
  497. /* handle unaligned data at 4k boundary */
  498. if (blocksize == 0)
  499. blocksize = 1;
  500. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  501. writecount = blocksize;
  502. do
  503. {
  504. nbytes = MIN((writecount << 1), 4);
  505. if (nbytes < 4)
  506. {
  507. if (mem_ap_write_buf_u16(swjdp, buffer, nbytes, address) != ERROR_OK)
  508. {
  509. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  510. return ERROR_JTAG_DEVICE_ERROR;
  511. }
  512. address += nbytes >> 1;
  513. }
  514. else
  515. {
  516. uint32_t outvalue;
  517. memcpy(&outvalue, buffer, sizeof(uint32_t));
  518. for (i = 0; i < nbytes; i++)
  519. {
  520. *((uint8_t*)buffer + (address & 0x3)) = outvalue;
  521. outvalue >>= 8;
  522. address++;
  523. }
  524. memcpy(&outvalue, buffer, sizeof(uint32_t));
  525. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
  526. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  527. {
  528. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  529. return ERROR_JTAG_DEVICE_ERROR;
  530. }
  531. }
  532. buffer += nbytes >> 1;
  533. writecount -= nbytes >> 1;
  534. } while (writecount);
  535. wcount -= blocksize;
  536. }
  537. return retval;
  538. }
  539. int mem_ap_write_buf_u16(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
  540. {
  541. int retval = ERROR_OK;
  542. if (count >= 4)
  543. return mem_ap_write_buf_packed_u16(swjdp, buffer, count, address);
  544. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  545. while (count > 0)
  546. {
  547. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  548. uint16_t svalue;
  549. memcpy(&svalue, buffer, sizeof(uint16_t));
  550. uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
  551. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
  552. retval = swjdp_transaction_endcheck(swjdp);
  553. count -= 2;
  554. address += 2;
  555. buffer += 2;
  556. }
  557. return retval;
  558. }
  559. static int mem_ap_write_buf_packed_u8(struct swjdp_common *swjdp,
  560. uint8_t *buffer, int count, uint32_t address)
  561. {
  562. int retval = ERROR_OK;
  563. int wcount, blocksize, writecount, i;
  564. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  565. wcount = count;
  566. while (wcount > 0)
  567. {
  568. int nbytes;
  569. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  570. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  571. if (wcount < blocksize)
  572. blocksize = wcount;
  573. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  574. writecount = blocksize;
  575. do
  576. {
  577. nbytes = MIN(writecount, 4);
  578. if (nbytes < 4)
  579. {
  580. if (mem_ap_write_buf_u8(swjdp, buffer, nbytes, address) != ERROR_OK)
  581. {
  582. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  583. return ERROR_JTAG_DEVICE_ERROR;
  584. }
  585. address += nbytes;
  586. }
  587. else
  588. {
  589. uint32_t outvalue;
  590. memcpy(&outvalue, buffer, sizeof(uint32_t));
  591. for (i = 0; i < nbytes; i++)
  592. {
  593. *((uint8_t*)buffer + (address & 0x3)) = outvalue;
  594. outvalue >>= 8;
  595. address++;
  596. }
  597. memcpy(&outvalue, buffer, sizeof(uint32_t));
  598. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
  599. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  600. {
  601. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  602. return ERROR_JTAG_DEVICE_ERROR;
  603. }
  604. }
  605. buffer += nbytes;
  606. writecount -= nbytes;
  607. } while (writecount);
  608. wcount -= blocksize;
  609. }
  610. return retval;
  611. }
  612. int mem_ap_write_buf_u8(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
  613. {
  614. int retval = ERROR_OK;
  615. if (count >= 4)
  616. return mem_ap_write_buf_packed_u8(swjdp, buffer, count, address);
  617. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  618. while (count > 0)
  619. {
  620. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  621. uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
  622. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
  623. retval = swjdp_transaction_endcheck(swjdp);
  624. count--;
  625. address++;
  626. buffer++;
  627. }
  628. return retval;
  629. }
  630. /*********************************************************************************
  631. * *
  632. * mem_ap_read_buf_u32(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address) *
  633. * *
  634. * Read block fast in target order (little endian) into a buffer *
  635. * *
  636. **********************************************************************************/
  637. int mem_ap_read_buf_u32(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
  638. {
  639. int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
  640. uint32_t adr = address;
  641. uint8_t* pBuffer = buffer;
  642. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  643. count >>= 2;
  644. wcount = count;
  645. while (wcount > 0)
  646. {
  647. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  648. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  649. if (wcount < blocksize)
  650. blocksize = wcount;
  651. /* handle unaligned data at 4k boundary */
  652. if (blocksize == 0)
  653. blocksize = 1;
  654. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
  655. /* Scan out first read */
  656. adi_jtag_dp_scan(swjdp, JTAG_DP_APACC, AP_REG_DRW,
  657. DPAP_READ, 0, NULL, NULL);
  658. for (readcount = 0; readcount < blocksize - 1; readcount++)
  659. {
  660. /* Scan out next read; scan in posted value for the
  661. * previous one. Assumes read is acked "OK/FAULT",
  662. * and CTRL_STAT says that meant "OK".
  663. */
  664. adi_jtag_dp_scan(swjdp, JTAG_DP_APACC, AP_REG_DRW,
  665. DPAP_READ, 0, buffer + 4 * readcount,
  666. &swjdp->ack);
  667. }
  668. /* Scan in last posted value; RDBUFF has no other effect,
  669. * assuming ack is OK/FAULT and CTRL_STAT says "OK".
  670. */
  671. adi_jtag_dp_scan(swjdp, JTAG_DP_DPACC, DP_RDBUFF,
  672. DPAP_READ, 0, buffer + 4 * readcount,
  673. &swjdp->ack);
  674. if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
  675. {
  676. wcount = wcount - blocksize;
  677. address += 4 * blocksize;
  678. buffer += 4 * blocksize;
  679. }
  680. else
  681. {
  682. errorcount++;
  683. }
  684. if (errorcount > 1)
  685. {
  686. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  687. return ERROR_JTAG_DEVICE_ERROR;
  688. }
  689. }
  690. /* if we have an unaligned access - reorder data */
  691. if (adr & 0x3u)
  692. {
  693. for (readcount = 0; readcount < count; readcount++)
  694. {
  695. int i;
  696. uint32_t data;
  697. memcpy(&data, pBuffer, sizeof(uint32_t));
  698. for (i = 0; i < 4; i++)
  699. {
  700. *((uint8_t*)pBuffer) = (data >> 8 * (adr & 0x3));
  701. pBuffer++;
  702. adr++;
  703. }
  704. }
  705. }
  706. return retval;
  707. }
  708. static int mem_ap_read_buf_packed_u16(struct swjdp_common *swjdp,
  709. uint8_t *buffer, int count, uint32_t address)
  710. {
  711. uint32_t invalue;
  712. int retval = ERROR_OK;
  713. int wcount, blocksize, readcount, i;
  714. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  715. wcount = count >> 1;
  716. while (wcount > 0)
  717. {
  718. int nbytes;
  719. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  720. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  721. if (wcount < blocksize)
  722. blocksize = wcount;
  723. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  724. /* handle unaligned data at 4k boundary */
  725. if (blocksize == 0)
  726. blocksize = 1;
  727. readcount = blocksize;
  728. do
  729. {
  730. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
  731. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  732. {
  733. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  734. return ERROR_JTAG_DEVICE_ERROR;
  735. }
  736. nbytes = MIN((readcount << 1), 4);
  737. for (i = 0; i < nbytes; i++)
  738. {
  739. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  740. buffer++;
  741. address++;
  742. }
  743. readcount -= (nbytes >> 1);
  744. } while (readcount);
  745. wcount -= blocksize;
  746. }
  747. return retval;
  748. }
  749. int mem_ap_read_buf_u16(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
  750. {
  751. uint32_t invalue, i;
  752. int retval = ERROR_OK;
  753. if (count >= 4)
  754. return mem_ap_read_buf_packed_u16(swjdp, buffer, count, address);
  755. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  756. while (count > 0)
  757. {
  758. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  759. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
  760. retval = swjdp_transaction_endcheck(swjdp);
  761. if (address & 0x1)
  762. {
  763. for (i = 0; i < 2; i++)
  764. {
  765. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  766. buffer++;
  767. address++;
  768. }
  769. }
  770. else
  771. {
  772. uint16_t svalue = (invalue >> 8 * (address & 0x3));
  773. memcpy(buffer, &svalue, sizeof(uint16_t));
  774. address += 2;
  775. buffer += 2;
  776. }
  777. count -= 2;
  778. }
  779. return retval;
  780. }
  781. /* FIX!!! is this a potential performance bottleneck w.r.t. requiring too many
  782. * roundtrips when jtag_execute_queue() has a large overhead(e.g. for USB)s?
  783. *
  784. * The solution is to arrange for a large out/in scan in this loop and
  785. * and convert data afterwards.
  786. */
  787. static int mem_ap_read_buf_packed_u8(struct swjdp_common *swjdp,
  788. uint8_t *buffer, int count, uint32_t address)
  789. {
  790. uint32_t invalue;
  791. int retval = ERROR_OK;
  792. int wcount, blocksize, readcount, i;
  793. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  794. wcount = count;
  795. while (wcount > 0)
  796. {
  797. int nbytes;
  798. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  799. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  800. if (wcount < blocksize)
  801. blocksize = wcount;
  802. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  803. readcount = blocksize;
  804. do
  805. {
  806. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
  807. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  808. {
  809. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  810. return ERROR_JTAG_DEVICE_ERROR;
  811. }
  812. nbytes = MIN(readcount, 4);
  813. for (i = 0; i < nbytes; i++)
  814. {
  815. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  816. buffer++;
  817. address++;
  818. }
  819. readcount -= nbytes;
  820. } while (readcount);
  821. wcount -= blocksize;
  822. }
  823. return retval;
  824. }
  825. int mem_ap_read_buf_u8(struct swjdp_common *swjdp, uint8_t *buffer, int count, uint32_t address)
  826. {
  827. uint32_t invalue;
  828. int retval = ERROR_OK;
  829. if (count >= 4)
  830. return mem_ap_read_buf_packed_u8(swjdp, buffer, count, address);
  831. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  832. while (count > 0)
  833. {
  834. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  835. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue);
  836. retval = swjdp_transaction_endcheck(swjdp);
  837. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  838. count--;
  839. address++;
  840. buffer++;
  841. }
  842. return retval;
  843. }
  844. /**
  845. * Initialize a DAP.
  846. *
  847. * @todo Rename this. We also need an initialization scheme which account
  848. * for SWD transports not just JTAG; that will need to address differences
  849. * in layering. (JTAG is useful without any debug target; but not SWD.)
  850. */
  851. int ahbap_debugport_init(struct swjdp_common *swjdp)
  852. {
  853. uint32_t idreg, romaddr, dummy;
  854. uint32_t ctrlstat;
  855. int cnt = 0;
  856. int retval;
  857. LOG_DEBUG(" ");
  858. /* Default MEM-AP setup.
  859. *
  860. * REVISIT AP #0 may be an inappropriate default for this.
  861. * Should we probe, or receve a hint from the caller?
  862. * Presumably we can ignore the possibility of multiple APs.
  863. */
  864. swjdp->apsel = 0;
  865. swjdp->ap_csw_value = -1;
  866. swjdp->ap_tar_value = -1;
  867. /* DP initialization */
  868. swjdp->trans_mode = TRANS_MODE_ATOMIC;
  869. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  870. dap_dp_write_reg(swjdp, SSTICKYERR, DP_CTRL_STAT);
  871. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  872. swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  873. dap_dp_write_reg(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
  874. dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
  875. if ((retval = jtag_execute_queue()) != ERROR_OK)
  876. return retval;
  877. /* Check that we have debug power domains activated */
  878. while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10))
  879. {
  880. LOG_DEBUG("swjdp: wait CDBGPWRUPACK");
  881. dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
  882. if ((retval = jtag_execute_queue()) != ERROR_OK)
  883. return retval;
  884. alive_sleep(10);
  885. }
  886. while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10))
  887. {
  888. LOG_DEBUG("swjdp: wait CSYSPWRUPACK");
  889. dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
  890. if ((retval = jtag_execute_queue()) != ERROR_OK)
  891. return retval;
  892. alive_sleep(10);
  893. }
  894. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  895. /* With debug power on we can activate OVERRUN checking */
  896. swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
  897. dap_dp_write_reg(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
  898. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  899. /*
  900. * REVISIT this isn't actually *initializing* anything in an AP,
  901. * and doesn't care if it's a MEM-AP at all (much less AHB-AP).
  902. * Should it? If the ROM address is valid, is this the right
  903. * place to scan the table and do any topology detection?
  904. */
  905. dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &idreg);
  906. dap_ap_read_reg_u32(swjdp, AP_REG_BASE, &romaddr);
  907. LOG_DEBUG("AHB-AP ID Register 0x%" PRIx32 ", Debug ROM Address 0x%" PRIx32 "", idreg, romaddr);
  908. return ERROR_OK;
  909. }
  910. /* CID interpretation -- see ARM IHI 0029B section 3
  911. * and ARM IHI 0031A table 13-3.
  912. */
  913. static const char *class_description[16] ={
  914. "Reserved", "ROM table", "Reserved", "Reserved",
  915. "Reserved", "Reserved", "Reserved", "Reserved",
  916. "Reserved", "CoreSight component", "Reserved", "Peripheral Test Block",
  917. "Reserved", "OptimoDE DESS",
  918. "Generic IP component", "PrimeCell or System component"
  919. };
  920. static bool
  921. is_dap_cid_ok(uint32_t cid3, uint32_t cid2, uint32_t cid1, uint32_t cid0)
  922. {
  923. return cid3 == 0xb1 && cid2 == 0x05
  924. && ((cid1 & 0x0f) == 0) && cid0 == 0x0d;
  925. }
  926. int dap_info_command(struct command_context *cmd_ctx, struct swjdp_common *swjdp, int apsel)
  927. {
  928. uint32_t dbgbase, apid;
  929. int romtable_present = 0;
  930. uint8_t mem_ap;
  931. uint32_t apselold;
  932. apselold = swjdp->apsel;
  933. dap_ap_select(swjdp, apsel);
  934. dap_ap_read_reg_u32(swjdp, AP_REG_BASE, &dbgbase);
  935. dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &apid);
  936. swjdp_transaction_endcheck(swjdp);
  937. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  938. mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
  939. command_print(cmd_ctx, "AP ID register 0x%8.8" PRIx32, apid);
  940. if (apid)
  941. {
  942. switch (apid&0x0F)
  943. {
  944. case 0:
  945. command_print(cmd_ctx, "\tType is JTAG-AP");
  946. break;
  947. case 1:
  948. command_print(cmd_ctx, "\tType is MEM-AP AHB");
  949. break;
  950. case 2:
  951. command_print(cmd_ctx, "\tType is MEM-AP APB");
  952. break;
  953. default:
  954. command_print(cmd_ctx, "\tUnknown AP type");
  955. break;
  956. }
  957. /* NOTE: a MEM-AP may have a single CoreSight component that's
  958. * not a ROM table ... or have no such components at all.
  959. */
  960. if (mem_ap)
  961. command_print(cmd_ctx, "AP BASE 0x%8.8" PRIx32,
  962. dbgbase);
  963. }
  964. else
  965. {
  966. command_print(cmd_ctx, "No AP found at this apsel 0x%x", apsel);
  967. }
  968. romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
  969. if (romtable_present)
  970. {
  971. uint32_t cid0,cid1,cid2,cid3,memtype,romentry;
  972. uint16_t entry_offset;
  973. /* bit 16 of apid indicates a memory access port */
  974. if (dbgbase & 0x02)
  975. command_print(cmd_ctx, "\tValid ROM table present");
  976. else
  977. command_print(cmd_ctx, "\tROM table in legacy format");
  978. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  979. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF0, &cid0);
  980. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF4, &cid1);
  981. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFF8, &cid2);
  982. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFFC, &cid3);
  983. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000) | 0xFCC, &memtype);
  984. swjdp_transaction_endcheck(swjdp);
  985. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  986. command_print(cmd_ctx, "\tCID3 0x%2.2" PRIx32
  987. ", CID2 0x%2.2" PRIx32
  988. ", CID1 0x%2.2" PRIx32
  989. ", CID0 0x%2.2" PRIx32,
  990. cid3, cid2, cid1, cid0);
  991. if (memtype & 0x01)
  992. command_print(cmd_ctx, "\tMEMTYPE system memory present on bus");
  993. else
  994. command_print(cmd_ctx, "\tMEMTYPE System memory not present. "
  995. "Dedicated debug bus.");
  996. /* Now we read ROM table entries from dbgbase&0xFFFFF000) | 0x000 until we get 0x00000000 */
  997. entry_offset = 0;
  998. do
  999. {
  1000. mem_ap_read_atomic_u32(swjdp, (dbgbase&0xFFFFF000) | entry_offset, &romentry);
  1001. command_print(cmd_ctx, "\tROMTABLE[0x%x] = 0x%" PRIx32 "",entry_offset,romentry);
  1002. if (romentry&0x01)
  1003. {
  1004. uint32_t c_cid0, c_cid1, c_cid2, c_cid3;
  1005. uint32_t c_pid0, c_pid1, c_pid2, c_pid3, c_pid4;
  1006. uint32_t component_start, component_base;
  1007. unsigned part_num;
  1008. char *type, *full;
  1009. component_base = (uint32_t)((dbgbase & 0xFFFFF000)
  1010. + (int)(romentry & 0xFFFFF000));
  1011. mem_ap_read_atomic_u32(swjdp,
  1012. (component_base & 0xFFFFF000) | 0xFE0, &c_pid0);
  1013. mem_ap_read_atomic_u32(swjdp,
  1014. (component_base & 0xFFFFF000) | 0xFE4, &c_pid1);
  1015. mem_ap_read_atomic_u32(swjdp,
  1016. (component_base & 0xFFFFF000) | 0xFE8, &c_pid2);
  1017. mem_ap_read_atomic_u32(swjdp,
  1018. (component_base & 0xFFFFF000) | 0xFEC, &c_pid3);
  1019. mem_ap_read_atomic_u32(swjdp,
  1020. (component_base & 0xFFFFF000) | 0xFD0, &c_pid4);
  1021. mem_ap_read_atomic_u32(swjdp,
  1022. (component_base & 0xFFFFF000) | 0xFF0, &c_cid0);
  1023. mem_ap_read_atomic_u32(swjdp,
  1024. (component_base & 0xFFFFF000) | 0xFF4, &c_cid1);
  1025. mem_ap_read_atomic_u32(swjdp,
  1026. (component_base & 0xFFFFF000) | 0xFF8, &c_cid2);
  1027. mem_ap_read_atomic_u32(swjdp,
  1028. (component_base & 0xFFFFF000) | 0xFFC, &c_cid3);
  1029. component_start = component_base - 0x1000*(c_pid4 >> 4);
  1030. command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32
  1031. ", start address 0x%" PRIx32,
  1032. component_base, component_start);
  1033. command_print(cmd_ctx, "\t\tComponent class is 0x%x, %s",
  1034. (int) (c_cid1 >> 4) & 0xf,
  1035. /* See ARM IHI 0029B Table 3-3 */
  1036. class_description[(c_cid1 >> 4) & 0xf]);
  1037. /* CoreSight component? */
  1038. if (((c_cid1 >> 4) & 0x0f) == 9) {
  1039. uint32_t devtype;
  1040. unsigned minor;
  1041. char *major = "Reserved", *subtype = "Reserved";
  1042. mem_ap_read_atomic_u32(swjdp,
  1043. (component_base & 0xfffff000) | 0xfcc,
  1044. &devtype);
  1045. minor = (devtype >> 4) & 0x0f;
  1046. switch (devtype & 0x0f) {
  1047. case 0:
  1048. major = "Miscellaneous";
  1049. switch (minor) {
  1050. case 0:
  1051. subtype = "other";
  1052. break;
  1053. case 4:
  1054. subtype = "Validation component";
  1055. break;
  1056. }
  1057. break;
  1058. case 1:
  1059. major = "Trace Sink";
  1060. switch (minor) {
  1061. case 0:
  1062. subtype = "other";
  1063. break;
  1064. case 1:
  1065. subtype = "Port";
  1066. break;
  1067. case 2:
  1068. subtype = "Buffer";
  1069. break;
  1070. }
  1071. break;
  1072. case 2:
  1073. major = "Trace Link";
  1074. switch (minor) {
  1075. case 0:
  1076. subtype = "other";
  1077. break;
  1078. case 1:
  1079. subtype = "Funnel, router";
  1080. break;
  1081. case 2:
  1082. subtype = "Filter";
  1083. break;
  1084. case 3:
  1085. subtype = "FIFO, buffer";
  1086. break;
  1087. }
  1088. break;
  1089. case 3:
  1090. major = "Trace Source";
  1091. switch (minor) {
  1092. case 0:
  1093. subtype = "other";
  1094. break;
  1095. case 1:
  1096. subtype = "Processor";
  1097. break;
  1098. case 2:
  1099. subtype = "DSP";
  1100. break;
  1101. case 3:
  1102. subtype = "Engine/Coprocessor";
  1103. break;
  1104. case 4:
  1105. subtype = "Bus";
  1106. break;
  1107. }
  1108. break;
  1109. case 4:
  1110. major = "Debug Control";
  1111. switch (minor) {
  1112. case 0:
  1113. subtype = "other";
  1114. break;
  1115. case 1:
  1116. subtype = "Trigger Matrix";
  1117. break;
  1118. case 2:
  1119. subtype = "Debug Auth";
  1120. break;
  1121. }
  1122. break;
  1123. case 5:
  1124. major = "Debug Logic";
  1125. switch (minor) {
  1126. case 0:
  1127. subtype = "other";
  1128. break;
  1129. case 1:
  1130. subtype = "Processor";
  1131. break;
  1132. case 2:
  1133. subtype = "DSP";
  1134. break;
  1135. case 3:
  1136. subtype = "Engine/Coprocessor";
  1137. break;
  1138. }
  1139. break;
  1140. }
  1141. command_print(cmd_ctx, "\t\tType is 0x%2.2x, %s, %s",
  1142. (unsigned) (devtype & 0xff),
  1143. major, subtype);
  1144. /* REVISIT also show 0xfc8 DevId */
  1145. }
  1146. if (!is_dap_cid_ok(cid3, cid2, cid1, cid0))
  1147. command_print(cmd_ctx, "\t\tCID3 0x%2.2" PRIx32
  1148. ", CID2 0x%2.2" PRIx32
  1149. ", CID1 0x%2.2" PRIx32
  1150. ", CID0 0x%2.2" PRIx32,
  1151. c_cid3, c_cid2, c_cid1, c_cid0);
  1152. command_print(cmd_ctx, "\t\tPeripheral ID[4..0] = hex "
  1153. "%2.2x %2.2x %2.2x %2.2x %2.2x",
  1154. (int) c_pid4,
  1155. (int) c_pid3, (int) c_pid2,
  1156. (int) c_pid1, (int) c_pid0);
  1157. /* Part number interpretations are from Cortex
  1158. * core specs, the CoreSight components TRM
  1159. * (ARM DDI 0314H), and ETM specs; also from
  1160. * chip observation (e.g. TI SDTI).
  1161. */
  1162. part_num = c_pid0 & 0xff;
  1163. part_num |= (c_pid1 & 0x0f) << 8;
  1164. switch (part_num) {
  1165. case 0x000:
  1166. type = "Cortex-M3 NVIC";
  1167. full = "(Interrupt Controller)";
  1168. break;
  1169. case 0x001:
  1170. type = "Cortex-M3 ITM";
  1171. full = "(Instrumentation Trace Module)";
  1172. break;
  1173. case 0x002:
  1174. type = "Cortex-M3 DWT";
  1175. full = "(Data Watchpoint and Trace)";
  1176. break;
  1177. case 0x003:
  1178. type = "Cortex-M3 FBP";
  1179. full = "(Flash Patch and Breakpoint)";
  1180. break;
  1181. case 0x00d:
  1182. type = "CoreSight ETM11";
  1183. full = "(Embedded Trace)";
  1184. break;
  1185. // case 0x113: what?
  1186. case 0x120: /* from OMAP3 memmap */
  1187. type = "TI SDTI";
  1188. full = "(System Debug Trace Interface)";
  1189. break;
  1190. case 0x343: /* from OMAP3 memmap */
  1191. type = "TI DAPCTL";
  1192. full = "";
  1193. break;
  1194. case 0x4e0:
  1195. type = "Cortex-M3 ETM";
  1196. full = "(Embedded Trace)";
  1197. break;
  1198. case 0x906:
  1199. type = "Coresight CTI";
  1200. full = "(Cross Trigger)";
  1201. break;
  1202. case 0x907:
  1203. type = "Coresight ETB";
  1204. full = "(Trace Buffer)";
  1205. break;
  1206. case 0x908:
  1207. type = "Coresight CSTF";
  1208. full = "(Trace Funnel)";
  1209. break;
  1210. case 0x910:
  1211. type = "CoreSight ETM9";
  1212. full = "(Embedded Trace)";
  1213. break;
  1214. case 0x912:
  1215. type = "Coresight TPIU";
  1216. full = "(Trace Port Interface Unit)";
  1217. break;
  1218. case 0x921:
  1219. type = "Cortex-A8 ETM";
  1220. full = "(Embedded Trace)";
  1221. break;
  1222. case 0x922:
  1223. type = "Cortex-A8 CTI";
  1224. full = "(Cross Trigger)";
  1225. break;
  1226. case 0x923:
  1227. type = "Cortex-M3 TPIU";
  1228. full = "(Trace Port Interface Unit)";
  1229. break;
  1230. case 0xc08:
  1231. type = "Cortex-A8 Debug";
  1232. full = "(Debug Unit)";
  1233. break;
  1234. default:
  1235. type = "-*- unrecognized -*-";
  1236. full = "";
  1237. break;
  1238. }
  1239. command_print(cmd_ctx, "\t\tPart is %s %s",
  1240. type, full);
  1241. }
  1242. else
  1243. {
  1244. if (romentry)
  1245. command_print(cmd_ctx, "\t\tComponent not present");
  1246. else
  1247. command_print(cmd_ctx, "\t\tEnd of ROM table");
  1248. }
  1249. entry_offset += 4;
  1250. } while (romentry > 0);
  1251. }
  1252. else
  1253. {
  1254. command_print(cmd_ctx, "\tNo ROM table present");
  1255. }
  1256. dap_ap_select(swjdp, apselold);
  1257. return ERROR_OK;
  1258. }
  1259. DAP_COMMAND_HANDLER(dap_baseaddr_command)
  1260. {
  1261. uint32_t apsel, apselsave, baseaddr;
  1262. int retval;
  1263. apselsave = swjdp->apsel;
  1264. switch (CMD_ARGC) {
  1265. case 0:
  1266. apsel = swjdp->apsel;
  1267. break;
  1268. case 1:
  1269. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1270. break;
  1271. default:
  1272. return ERROR_COMMAND_SYNTAX_ERROR;
  1273. }
  1274. if (apselsave != apsel)
  1275. dap_ap_select(swjdp, apsel);
  1276. dap_ap_read_reg_u32(swjdp, AP_REG_BASE, &baseaddr);
  1277. retval = swjdp_transaction_endcheck(swjdp);
  1278. command_print(CMD_CTX, "0x%8.8" PRIx32, baseaddr);
  1279. if (apselsave != apsel)
  1280. dap_ap_select(swjdp, apselsave);
  1281. return retval;
  1282. }
  1283. DAP_COMMAND_HANDLER(dap_memaccess_command)
  1284. {
  1285. uint32_t memaccess_tck;
  1286. switch (CMD_ARGC) {
  1287. case 0:
  1288. memaccess_tck = swjdp->memaccess_tck;
  1289. break;
  1290. case 1:
  1291. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], memaccess_tck);
  1292. break;
  1293. default:
  1294. return ERROR_COMMAND_SYNTAX_ERROR;
  1295. }
  1296. swjdp->memaccess_tck = memaccess_tck;
  1297. command_print(CMD_CTX, "memory bus access delay set to %" PRIi32 " tck",
  1298. swjdp->memaccess_tck);
  1299. return ERROR_OK;
  1300. }
  1301. DAP_COMMAND_HANDLER(dap_apsel_command)
  1302. {
  1303. uint32_t apsel, apid;
  1304. int retval;
  1305. switch (CMD_ARGC) {
  1306. case 0:
  1307. apsel = 0;
  1308. break;
  1309. case 1:
  1310. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1311. break;
  1312. default:
  1313. return ERROR_COMMAND_SYNTAX_ERROR;
  1314. }
  1315. dap_ap_select(swjdp, apsel);
  1316. dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &apid);
  1317. retval = swjdp_transaction_endcheck(swjdp);
  1318. command_print(CMD_CTX, "ap %" PRIi32 " selected, identification register 0x%8.8" PRIx32,
  1319. apsel, apid);
  1320. return retval;
  1321. }
  1322. DAP_COMMAND_HANDLER(dap_apid_command)
  1323. {
  1324. uint32_t apsel, apselsave, apid;
  1325. int retval;
  1326. apselsave = swjdp->apsel;
  1327. switch (CMD_ARGC) {
  1328. case 0:
  1329. apsel = swjdp->apsel;
  1330. break;
  1331. case 1:
  1332. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], apsel);
  1333. break;
  1334. default:
  1335. return ERROR_COMMAND_SYNTAX_ERROR;
  1336. }
  1337. if (apselsave != apsel)
  1338. dap_ap_select(swjdp, apsel);
  1339. dap_ap_read_reg_u32(swjdp, AP_REG_IDR, &apid);
  1340. retval = swjdp_transaction_endcheck(swjdp);
  1341. command_print(CMD_CTX, "0x%8.8" PRIx32, apid);
  1342. if (apselsave != apsel)
  1343. dap_ap_select(swjdp, apselsave);
  1344. return retval;
  1345. }