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.
 
 
 
 
 
 

1132 lines
34 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. * *
  28. * This file implements support for the ARM Debug Interface v5 (ADI_V5) *
  29. * *
  30. * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031A *
  31. * *
  32. * CoreSight(tm) DAP-Lite TRM, ARM DDI 0316A *
  33. * Cortex-M3(tm) TRM, ARM DDI 0337C *
  34. * *
  35. ***************************************************************************/
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #include "arm_adi_v5.h"
  40. #include "time_support.h"
  41. /*
  42. * Transaction Mode:
  43. * swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  44. * Uses Overrun checking mode and does not do actual JTAG send/receive or transaction
  45. * result checking until swjdp_end_transaction()
  46. * This must be done before using or deallocating any return variables.
  47. * swjdp->trans_mode == TRANS_MODE_ATOMIC
  48. * All reads and writes to the AHB bus are checked for valid completion, and return values
  49. * are immediatley available.
  50. */
  51. /* ARM ADI Specification requires at least 10 bits used for TAR autoincrement */
  52. /*
  53. uint32_t tar_block_size(uint32_t address)
  54. Return the largest block starting at address that does not cross a tar block size alignment boundary
  55. */
  56. static uint32_t max_tar_block_size(uint32_t tar_autoincr_block, uint32_t address)
  57. {
  58. return (tar_autoincr_block - ((tar_autoincr_block - 1) & address)) >> 2;
  59. }
  60. /***************************************************************************
  61. * *
  62. * DPACC and APACC scanchain access through JTAG-DP *
  63. * *
  64. ***************************************************************************/
  65. /* Scan out and in from target ordered uint8_t buffers */
  66. int adi_jtag_dp_scan(swjdp_common_t *swjdp, uint8_t instr, uint8_t reg_addr, uint8_t RnW, uint8_t *outvalue, uint8_t *invalue, uint8_t *ack)
  67. {
  68. arm_jtag_t *jtag_info = swjdp->jtag_info;
  69. scan_field_t fields[2];
  70. uint8_t out_addr_buf;
  71. jtag_set_end_state(TAP_IDLE);
  72. arm_jtag_set_instr(jtag_info, instr, NULL);
  73. /* Add specified number of tck clocks before accessing memory bus */
  74. if ((instr == DAP_IR_APACC) && ((reg_addr == AP_REG_DRW)||((reg_addr&0xF0) == AP_REG_BD0) )&& (swjdp->memaccess_tck != 0))
  75. jtag_add_runtest(swjdp->memaccess_tck, jtag_set_end_state(TAP_IDLE));
  76. fields[0].tap = jtag_info->tap;
  77. fields[0].num_bits = 3;
  78. buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
  79. fields[0].out_value = &out_addr_buf;
  80. fields[0].in_value = ack;
  81. fields[1].tap = jtag_info->tap;
  82. fields[1].num_bits = 32;
  83. fields[1].out_value = outvalue;
  84. fields[1].in_value = invalue;
  85. jtag_add_dr_scan(2, fields, jtag_get_end_state());
  86. return ERROR_OK;
  87. }
  88. /* Scan out and in from host ordered uint32_t variables */
  89. int adi_jtag_dp_scan_u32(swjdp_common_t *swjdp, uint8_t instr, uint8_t reg_addr, uint8_t RnW, uint32_t outvalue, uint32_t *invalue, uint8_t *ack)
  90. {
  91. arm_jtag_t *jtag_info = swjdp->jtag_info;
  92. scan_field_t fields[2];
  93. uint8_t out_value_buf[4];
  94. uint8_t out_addr_buf;
  95. jtag_set_end_state(TAP_IDLE);
  96. arm_jtag_set_instr(jtag_info, instr, NULL);
  97. /* Add specified number of tck clocks before accessing memory bus */
  98. if ((instr == DAP_IR_APACC) && ((reg_addr == AP_REG_DRW)||((reg_addr&0xF0) == AP_REG_BD0) )&& (swjdp->memaccess_tck != 0))
  99. jtag_add_runtest(swjdp->memaccess_tck, jtag_set_end_state(TAP_IDLE));
  100. fields[0].tap = jtag_info->tap;
  101. fields[0].num_bits = 3;
  102. buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
  103. fields[0].out_value = &out_addr_buf;
  104. fields[0].in_value = ack;
  105. fields[1].tap = jtag_info->tap;
  106. fields[1].num_bits = 32;
  107. buf_set_u32(out_value_buf, 0, 32, outvalue);
  108. fields[1].out_value = out_value_buf;
  109. fields[1].in_value = NULL;
  110. if (invalue)
  111. {
  112. fields[1].in_value = (uint8_t *)invalue;
  113. jtag_add_dr_scan(2, fields, jtag_get_end_state());
  114. jtag_add_callback(arm_le_to_h_u32, (jtag_callback_data_t) invalue);
  115. } else
  116. {
  117. jtag_add_dr_scan(2, fields, jtag_get_end_state());
  118. }
  119. return ERROR_OK;
  120. }
  121. /* scan_inout_check adds one extra inscan for DPAP_READ commands to read variables */
  122. int scan_inout_check(swjdp_common_t *swjdp, uint8_t instr, uint8_t reg_addr, uint8_t RnW, uint8_t *outvalue, uint8_t *invalue)
  123. {
  124. adi_jtag_dp_scan(swjdp, instr, reg_addr, RnW, outvalue, NULL, NULL);
  125. if ((RnW == DPAP_READ) && (invalue != NULL))
  126. {
  127. adi_jtag_dp_scan(swjdp, DAP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
  128. }
  129. /* In TRANS_MODE_ATOMIC all DAP_IR_APACC transactions wait for ack=OK/FAULT and the check CTRL_STAT */
  130. if ((instr == DAP_IR_APACC) && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
  131. {
  132. return swjdp_transaction_endcheck(swjdp);
  133. }
  134. return ERROR_OK;
  135. }
  136. int scan_inout_check_u32(swjdp_common_t *swjdp, uint8_t instr, uint8_t reg_addr, uint8_t RnW, uint32_t outvalue, uint32_t *invalue)
  137. {
  138. adi_jtag_dp_scan_u32(swjdp, instr, reg_addr, RnW, outvalue, NULL, NULL);
  139. if ((RnW==DPAP_READ) && (invalue != NULL))
  140. {
  141. adi_jtag_dp_scan_u32(swjdp, DAP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
  142. }
  143. /* In TRANS_MODE_ATOMIC all DAP_IR_APACC transactions wait for ack=OK/FAULT and then check CTRL_STAT */
  144. if ((instr == DAP_IR_APACC) && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
  145. {
  146. return swjdp_transaction_endcheck(swjdp);
  147. }
  148. return ERROR_OK;
  149. }
  150. int swjdp_transaction_endcheck(swjdp_common_t *swjdp)
  151. {
  152. int retval;
  153. uint32_t ctrlstat;
  154. /* too expensive to call keep_alive() here */
  155. #if 0
  156. /* Danger!!!! BROKEN!!!! */
  157. scan_inout_check_u32(swjdp, DAP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  158. /* Danger!!!! BROKEN!!!! Why will jtag_execute_queue() fail here????
  159. R956 introduced the check on return value here and now Michael Schwingen reports
  160. that this code no longer works....
  161. https://lists.berlios.de/pipermail/openocd-development/2008-September/003107.html
  162. */
  163. if ((retval=jtag_execute_queue()) != ERROR_OK)
  164. {
  165. LOG_ERROR("BUG: Why does this fail the first time????");
  166. }
  167. /* Why??? second time it works??? */
  168. #endif
  169. scan_inout_check_u32(swjdp, DAP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  170. if ((retval=jtag_execute_queue()) != ERROR_OK)
  171. return retval;
  172. swjdp->ack = swjdp->ack & 0x7;
  173. if (swjdp->ack != 2)
  174. {
  175. long long then=timeval_ms();
  176. while (swjdp->ack != 2)
  177. {
  178. if (swjdp->ack == 1)
  179. {
  180. if ((timeval_ms()-then) > 1000)
  181. {
  182. LOG_WARNING("Timeout (1000ms) waiting for ACK = OK/FAULT in SWJDP transaction");
  183. return ERROR_JTAG_DEVICE_ERROR;
  184. }
  185. }
  186. else
  187. {
  188. LOG_WARNING("Invalid ACK in SWJDP transaction");
  189. return ERROR_JTAG_DEVICE_ERROR;
  190. }
  191. scan_inout_check_u32(swjdp, DAP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  192. if ((retval=jtag_execute_queue()) != ERROR_OK)
  193. return retval;
  194. swjdp->ack = swjdp->ack & 0x7;
  195. }
  196. } else
  197. {
  198. /* common code path avoids fn to timeval_ms() */
  199. }
  200. /* Check for STICKYERR and STICKYORUN */
  201. if (ctrlstat & (SSTICKYORUN | SSTICKYERR))
  202. {
  203. LOG_DEBUG("swjdp: CTRL/STAT error 0x%" PRIx32 "", ctrlstat);
  204. /* Check power to debug regions */
  205. if ((ctrlstat & 0xf0000000) != 0xf0000000)
  206. {
  207. ahbap_debugport_init(swjdp);
  208. }
  209. else
  210. {
  211. uint32_t mem_ap_csw, mem_ap_tar;
  212. /* Print information about last AHBAP access */
  213. 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);
  214. if (ctrlstat & SSTICKYORUN)
  215. LOG_ERROR("SWJ-DP OVERRUN - check clock or reduce jtag speed");
  216. if (ctrlstat & SSTICKYERR)
  217. LOG_ERROR("SWJ-DP STICKY ERROR");
  218. /* Clear Sticky Error Bits */
  219. scan_inout_check_u32(swjdp, DAP_IR_DPACC, DP_CTRL_STAT, DPAP_WRITE, swjdp->dp_ctrl_stat | SSTICKYORUN | SSTICKYERR, NULL);
  220. scan_inout_check_u32(swjdp, DAP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  221. if ((retval=jtag_execute_queue()) != ERROR_OK)
  222. return retval;
  223. LOG_DEBUG("swjdp: status 0x%" PRIx32 "", ctrlstat);
  224. dap_ap_read_reg_u32(swjdp, AP_REG_CSW, &mem_ap_csw);
  225. dap_ap_read_reg_u32(swjdp, AP_REG_TAR, &mem_ap_tar);
  226. if ((retval=jtag_execute_queue()) != ERROR_OK)
  227. return retval;
  228. LOG_ERROR("Read MEM_AP_CSW 0x%" PRIx32 ", MEM_AP_TAR 0x%" PRIx32 "", mem_ap_csw, mem_ap_tar);
  229. }
  230. if ((retval=jtag_execute_queue()) != ERROR_OK)
  231. return retval;
  232. return ERROR_JTAG_DEVICE_ERROR;
  233. }
  234. return ERROR_OK;
  235. }
  236. /***************************************************************************
  237. * *
  238. * DP and MEM-AP register access through APACC and DPACC *
  239. * *
  240. ***************************************************************************/
  241. int dap_dp_write_reg(swjdp_common_t *swjdp, uint32_t value, uint8_t reg_addr)
  242. {
  243. return scan_inout_check_u32(swjdp, DAP_IR_DPACC, reg_addr, DPAP_WRITE, value, NULL);
  244. }
  245. int dap_dp_read_reg(swjdp_common_t *swjdp, uint32_t *value, uint8_t reg_addr)
  246. {
  247. return scan_inout_check_u32(swjdp, DAP_IR_DPACC, reg_addr, DPAP_READ, 0, value);
  248. }
  249. int dap_ap_select(swjdp_common_t *swjdp,uint8_t apsel)
  250. {
  251. uint32_t select;
  252. select = (apsel<<24) & 0xFF000000;
  253. if (select != swjdp->apsel)
  254. {
  255. swjdp->apsel = select;
  256. /* Switching AP invalidates cached values */
  257. swjdp->dp_select_value = -1;
  258. swjdp->ap_csw_value = -1;
  259. swjdp->ap_tar_value = -1;
  260. }
  261. return ERROR_OK;
  262. }
  263. int dap_dp_bankselect(swjdp_common_t *swjdp,uint32_t ap_reg)
  264. {
  265. uint32_t select;
  266. select = (ap_reg & 0x000000F0);
  267. if (select != swjdp->dp_select_value)
  268. {
  269. dap_dp_write_reg(swjdp, select | swjdp->apsel, DP_SELECT);
  270. swjdp->dp_select_value = select;
  271. }
  272. return ERROR_OK;
  273. }
  274. int dap_ap_write_reg(swjdp_common_t *swjdp, uint32_t reg_addr, uint8_t* out_value_buf)
  275. {
  276. dap_dp_bankselect(swjdp, reg_addr);
  277. scan_inout_check(swjdp, DAP_IR_APACC, reg_addr, DPAP_WRITE, out_value_buf, NULL);
  278. return ERROR_OK;
  279. }
  280. int dap_ap_read_reg(swjdp_common_t *swjdp, uint32_t reg_addr, uint8_t *in_value_buf)
  281. {
  282. dap_dp_bankselect(swjdp, reg_addr);
  283. scan_inout_check(swjdp, DAP_IR_APACC, reg_addr, DPAP_READ, 0, in_value_buf);
  284. return ERROR_OK;
  285. }
  286. int dap_ap_write_reg_u32(swjdp_common_t *swjdp, uint32_t reg_addr, uint32_t value)
  287. {
  288. uint8_t out_value_buf[4];
  289. buf_set_u32(out_value_buf, 0, 32, value);
  290. dap_dp_bankselect(swjdp, reg_addr);
  291. scan_inout_check(swjdp, DAP_IR_APACC, reg_addr, DPAP_WRITE, out_value_buf, NULL);
  292. return ERROR_OK;
  293. }
  294. int dap_ap_read_reg_u32(swjdp_common_t *swjdp, uint32_t reg_addr, uint32_t *value)
  295. {
  296. dap_dp_bankselect(swjdp, reg_addr);
  297. scan_inout_check_u32(swjdp, DAP_IR_APACC, reg_addr, DPAP_READ, 0, value);
  298. return ERROR_OK;
  299. }
  300. /***************************************************************************
  301. * *
  302. * AHB-AP access to memory and system registers on AHB bus *
  303. * *
  304. ***************************************************************************/
  305. int dap_setup_accessport(swjdp_common_t *swjdp, uint32_t csw, uint32_t tar)
  306. {
  307. csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
  308. if (csw != swjdp->ap_csw_value)
  309. {
  310. /* LOG_DEBUG("swjdp : Set CSW %x",csw); */
  311. dap_ap_write_reg_u32(swjdp, AP_REG_CSW, csw );
  312. swjdp->ap_csw_value = csw;
  313. }
  314. if (tar != swjdp->ap_tar_value)
  315. {
  316. /* LOG_DEBUG("swjdp : Set TAR %x",tar); */
  317. dap_ap_write_reg_u32(swjdp, AP_REG_TAR, tar );
  318. swjdp->ap_tar_value = tar;
  319. }
  320. if (csw & CSW_ADDRINC_MASK)
  321. {
  322. /* Do not cache TAR value when autoincrementing */
  323. swjdp->ap_tar_value = -1;
  324. }
  325. return ERROR_OK;
  326. }
  327. /*****************************************************************************
  328. * *
  329. * mem_ap_read_u32(swjdp_common_t *swjdp, uint32_t address, uint32_t *value) *
  330. * *
  331. * Read a uint32_t value from memory or system register *
  332. * Functionally equivalent to target_read_u32(target, address, uint32_t *value), *
  333. * but with less overhead *
  334. *****************************************************************************/
  335. int mem_ap_read_u32(swjdp_common_t *swjdp, uint32_t address, uint32_t *value)
  336. {
  337. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  338. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
  339. dap_ap_read_reg_u32(swjdp, AP_REG_BD0 | (address & 0xC), value );
  340. return ERROR_OK;
  341. }
  342. int mem_ap_read_atomic_u32(swjdp_common_t *swjdp, uint32_t address, uint32_t *value)
  343. {
  344. mem_ap_read_u32(swjdp, address, value);
  345. return swjdp_transaction_endcheck(swjdp);
  346. }
  347. /*****************************************************************************
  348. * *
  349. * mem_ap_write_u32(swjdp_common_t *swjdp, uint32_t address, uint32_t value) *
  350. * *
  351. * Write a uint32_t value to memory or memory mapped register *
  352. * *
  353. *****************************************************************************/
  354. int mem_ap_write_u32(swjdp_common_t *swjdp, uint32_t address, uint32_t value)
  355. {
  356. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  357. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
  358. dap_ap_write_reg_u32(swjdp, AP_REG_BD0 | (address & 0xC), value );
  359. return ERROR_OK;
  360. }
  361. int mem_ap_write_atomic_u32(swjdp_common_t *swjdp, uint32_t address, uint32_t value)
  362. {
  363. mem_ap_write_u32(swjdp, address, value);
  364. return swjdp_transaction_endcheck(swjdp);
  365. }
  366. /*****************************************************************************
  367. * *
  368. * mem_ap_write_buf(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address) *
  369. * *
  370. * Write a buffer in target order (little endian) *
  371. * *
  372. *****************************************************************************/
  373. int mem_ap_write_buf_u32(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  374. {
  375. int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
  376. uint32_t adr = address;
  377. uint8_t* pBuffer = buffer;
  378. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  379. count >>= 2;
  380. wcount = count;
  381. /* if we have an unaligned access - reorder data */
  382. if (adr & 0x3u)
  383. {
  384. for (writecount = 0; writecount < count; writecount++)
  385. {
  386. int i;
  387. uint32_t outvalue;
  388. memcpy(&outvalue, pBuffer, sizeof(uint32_t));
  389. for (i = 0; i < 4; i++ )
  390. {
  391. *((uint8_t*)pBuffer + (adr & 0x3)) = outvalue;
  392. outvalue >>= 8;
  393. adr++;
  394. }
  395. pBuffer += sizeof(uint32_t);
  396. }
  397. }
  398. while (wcount > 0)
  399. {
  400. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  401. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  402. if (wcount < blocksize)
  403. blocksize = wcount;
  404. /* handle unaligned data at 4k boundary */
  405. if (blocksize == 0)
  406. blocksize = 1;
  407. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
  408. for (writecount = 0; writecount < blocksize; writecount++)
  409. {
  410. dap_ap_write_reg(swjdp, AP_REG_DRW, buffer + 4 * writecount );
  411. }
  412. if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
  413. {
  414. wcount = wcount - blocksize;
  415. address = address + 4 * blocksize;
  416. buffer = buffer + 4 * blocksize;
  417. }
  418. else
  419. {
  420. errorcount++;
  421. }
  422. if (errorcount > 1)
  423. {
  424. LOG_WARNING("Block write error address 0x%" PRIx32 ", wcount 0x%x", address, wcount);
  425. return ERROR_JTAG_DEVICE_ERROR;
  426. }
  427. }
  428. return retval;
  429. }
  430. int mem_ap_write_buf_packed_u16(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  431. {
  432. int retval = ERROR_OK;
  433. int wcount, blocksize, writecount, i;
  434. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  435. wcount = count >> 1;
  436. while (wcount > 0)
  437. {
  438. int nbytes;
  439. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  440. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  441. if (wcount < blocksize)
  442. blocksize = wcount;
  443. /* handle unaligned data at 4k boundary */
  444. if (blocksize == 0)
  445. blocksize = 1;
  446. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  447. writecount = blocksize;
  448. do
  449. {
  450. nbytes = MIN((writecount << 1), 4);
  451. if (nbytes < 4 )
  452. {
  453. if (mem_ap_write_buf_u16(swjdp, buffer, nbytes, address) != ERROR_OK)
  454. {
  455. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  456. return ERROR_JTAG_DEVICE_ERROR;
  457. }
  458. address += nbytes >> 1;
  459. }
  460. else
  461. {
  462. uint32_t outvalue;
  463. memcpy(&outvalue, buffer, sizeof(uint32_t));
  464. for (i = 0; i < nbytes; i++ )
  465. {
  466. *((uint8_t*)buffer + (address & 0x3)) = outvalue;
  467. outvalue >>= 8;
  468. address++;
  469. }
  470. memcpy(&outvalue, buffer, sizeof(uint32_t));
  471. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
  472. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  473. {
  474. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  475. return ERROR_JTAG_DEVICE_ERROR;
  476. }
  477. }
  478. buffer += nbytes >> 1;
  479. writecount -= nbytes >> 1;
  480. } while (writecount);
  481. wcount -= blocksize;
  482. }
  483. return retval;
  484. }
  485. int mem_ap_write_buf_u16(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  486. {
  487. int retval = ERROR_OK;
  488. if (count >= 4)
  489. return mem_ap_write_buf_packed_u16(swjdp, buffer, count, address);
  490. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  491. while (count > 0)
  492. {
  493. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  494. uint16_t svalue;
  495. memcpy(&svalue, buffer, sizeof(uint16_t));
  496. uint32_t outvalue = (uint32_t)svalue << 8 * (address & 0x3);
  497. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue );
  498. retval = swjdp_transaction_endcheck(swjdp);
  499. count -= 2;
  500. address += 2;
  501. buffer += 2;
  502. }
  503. return retval;
  504. }
  505. int mem_ap_write_buf_packed_u8(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  506. {
  507. int retval = ERROR_OK;
  508. int wcount, blocksize, writecount, i;
  509. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  510. wcount = count;
  511. while (wcount > 0)
  512. {
  513. int nbytes;
  514. /* Adjust to write blocks within boundaries aligned to the TAR autoincremnent size*/
  515. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  516. if (wcount < blocksize)
  517. blocksize = wcount;
  518. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  519. writecount = blocksize;
  520. do
  521. {
  522. nbytes = MIN(writecount, 4);
  523. if (nbytes < 4 )
  524. {
  525. if (mem_ap_write_buf_u8(swjdp, buffer, nbytes, address) != ERROR_OK)
  526. {
  527. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  528. return ERROR_JTAG_DEVICE_ERROR;
  529. }
  530. address += nbytes;
  531. }
  532. else
  533. {
  534. uint32_t outvalue;
  535. memcpy(&outvalue, buffer, sizeof(uint32_t));
  536. for (i = 0; i < nbytes; i++ )
  537. {
  538. *((uint8_t*)buffer + (address & 0x3)) = outvalue;
  539. outvalue >>= 8;
  540. address++;
  541. }
  542. memcpy(&outvalue, buffer, sizeof(uint32_t));
  543. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue);
  544. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  545. {
  546. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  547. return ERROR_JTAG_DEVICE_ERROR;
  548. }
  549. }
  550. buffer += nbytes;
  551. writecount -= nbytes;
  552. } while (writecount);
  553. wcount -= blocksize;
  554. }
  555. return retval;
  556. }
  557. int mem_ap_write_buf_u8(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  558. {
  559. int retval = ERROR_OK;
  560. if (count >= 4)
  561. return mem_ap_write_buf_packed_u8(swjdp, buffer, count, address);
  562. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  563. while (count > 0)
  564. {
  565. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  566. uint32_t outvalue = (uint32_t)*buffer << 8 * (address & 0x3);
  567. dap_ap_write_reg_u32(swjdp, AP_REG_DRW, outvalue );
  568. retval = swjdp_transaction_endcheck(swjdp);
  569. count--;
  570. address++;
  571. buffer++;
  572. }
  573. return retval;
  574. }
  575. /*********************************************************************************
  576. * *
  577. * mem_ap_read_buf_u32(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address) *
  578. * *
  579. * Read block fast in target order (little endian) into a buffer *
  580. * *
  581. **********************************************************************************/
  582. int mem_ap_read_buf_u32(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  583. {
  584. int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
  585. uint32_t adr = address;
  586. uint8_t* pBuffer = buffer;
  587. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  588. count >>= 2;
  589. wcount = count;
  590. while (wcount > 0)
  591. {
  592. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  593. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  594. if (wcount < blocksize)
  595. blocksize = wcount;
  596. /* handle unaligned data at 4k boundary */
  597. if (blocksize == 0)
  598. blocksize = 1;
  599. dap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
  600. /* Scan out first read */
  601. adi_jtag_dp_scan(swjdp, DAP_IR_APACC, AP_REG_DRW, DPAP_READ, 0, NULL, NULL);
  602. for (readcount = 0; readcount < blocksize - 1; readcount++)
  603. {
  604. /* Scan out read instruction and scan in previous value */
  605. adi_jtag_dp_scan(swjdp, DAP_IR_APACC, AP_REG_DRW, DPAP_READ, 0, buffer + 4 * readcount, &swjdp->ack);
  606. }
  607. /* Scan in last value */
  608. adi_jtag_dp_scan(swjdp, DAP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, buffer + 4 * readcount, &swjdp->ack);
  609. if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
  610. {
  611. wcount = wcount - blocksize;
  612. address += 4 * blocksize;
  613. buffer += 4 * blocksize;
  614. }
  615. else
  616. {
  617. errorcount++;
  618. }
  619. if (errorcount > 1)
  620. {
  621. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  622. return ERROR_JTAG_DEVICE_ERROR;
  623. }
  624. }
  625. /* if we have an unaligned access - reorder data */
  626. if (adr & 0x3u)
  627. {
  628. for (readcount = 0; readcount < count; readcount++)
  629. {
  630. int i;
  631. uint32_t data;
  632. memcpy(&data, pBuffer, sizeof(uint32_t));
  633. for (i = 0; i < 4; i++ )
  634. {
  635. *((uint8_t*)pBuffer) = (data >> 8 * (adr & 0x3));
  636. pBuffer++;
  637. adr++;
  638. }
  639. }
  640. }
  641. return retval;
  642. }
  643. int mem_ap_read_buf_packed_u16(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  644. {
  645. uint32_t invalue;
  646. int retval = ERROR_OK;
  647. int wcount, blocksize, readcount, i;
  648. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  649. wcount = count >> 1;
  650. while (wcount > 0)
  651. {
  652. int nbytes;
  653. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  654. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  655. if (wcount < blocksize)
  656. blocksize = wcount;
  657. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  658. /* handle unaligned data at 4k boundary */
  659. if (blocksize == 0)
  660. blocksize = 1;
  661. readcount = blocksize;
  662. do
  663. {
  664. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue );
  665. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  666. {
  667. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  668. return ERROR_JTAG_DEVICE_ERROR;
  669. }
  670. nbytes = MIN((readcount << 1), 4);
  671. for (i = 0; i < nbytes; i++ )
  672. {
  673. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  674. buffer++;
  675. address++;
  676. }
  677. readcount -= (nbytes >> 1);
  678. } while (readcount);
  679. wcount -= blocksize;
  680. }
  681. return retval;
  682. }
  683. int mem_ap_read_buf_u16(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  684. {
  685. uint32_t invalue, i;
  686. int retval = ERROR_OK;
  687. if (count >= 4)
  688. return mem_ap_read_buf_packed_u16(swjdp, buffer, count, address);
  689. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  690. while (count > 0)
  691. {
  692. dap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  693. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue );
  694. retval = swjdp_transaction_endcheck(swjdp);
  695. if (address & 0x1)
  696. {
  697. for (i = 0; i < 2; i++ )
  698. {
  699. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  700. buffer++;
  701. address++;
  702. }
  703. }
  704. else
  705. {
  706. uint16_t svalue = (invalue >> 8 * (address & 0x3));
  707. memcpy(buffer, &svalue, sizeof(uint16_t));
  708. address += 2;
  709. buffer += 2;
  710. }
  711. count -= 2;
  712. }
  713. return retval;
  714. }
  715. /* FIX!!! is this a potential performance bottleneck w.r.t. requiring too many
  716. * roundtrips when jtag_execute_queue() has a large overhead(e.g. for USB)s?
  717. *
  718. * The solution is to arrange for a large out/in scan in this loop and
  719. * and convert data afterwards.
  720. */
  721. int mem_ap_read_buf_packed_u8(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  722. {
  723. uint32_t invalue;
  724. int retval = ERROR_OK;
  725. int wcount, blocksize, readcount, i;
  726. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  727. wcount = count;
  728. while (wcount > 0)
  729. {
  730. int nbytes;
  731. /* Adjust to read blocks within boundaries aligned to the TAR autoincremnent size*/
  732. blocksize = max_tar_block_size(swjdp->tar_autoincr_block, address);
  733. if (wcount < blocksize)
  734. blocksize = wcount;
  735. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  736. readcount = blocksize;
  737. do
  738. {
  739. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue );
  740. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  741. {
  742. LOG_WARNING("Block read error address 0x%" PRIx32 ", count 0x%x", address, count);
  743. return ERROR_JTAG_DEVICE_ERROR;
  744. }
  745. nbytes = MIN(readcount, 4);
  746. for (i = 0; i < nbytes; i++ )
  747. {
  748. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  749. buffer++;
  750. address++;
  751. }
  752. readcount -= nbytes;
  753. } while (readcount);
  754. wcount -= blocksize;
  755. }
  756. return retval;
  757. }
  758. int mem_ap_read_buf_u8(swjdp_common_t *swjdp, uint8_t *buffer, int count, uint32_t address)
  759. {
  760. uint32_t invalue;
  761. int retval = ERROR_OK;
  762. if (count >= 4)
  763. return mem_ap_read_buf_packed_u8(swjdp, buffer, count, address);
  764. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  765. while (count > 0)
  766. {
  767. dap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  768. dap_ap_read_reg_u32(swjdp, AP_REG_DRW, &invalue );
  769. retval = swjdp_transaction_endcheck(swjdp);
  770. *((uint8_t*)buffer) = (invalue >> 8 * (address & 0x3));
  771. count--;
  772. address++;
  773. buffer++;
  774. }
  775. return retval;
  776. }
  777. int ahbap_debugport_init(swjdp_common_t *swjdp)
  778. {
  779. uint32_t idreg, romaddr, dummy;
  780. uint32_t ctrlstat;
  781. int cnt = 0;
  782. int retval;
  783. LOG_DEBUG(" ");
  784. swjdp->apsel = 0;
  785. swjdp->ap_csw_value = -1;
  786. swjdp->ap_tar_value = -1;
  787. swjdp->trans_mode = TRANS_MODE_ATOMIC;
  788. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  789. dap_dp_write_reg(swjdp, SSTICKYERR, DP_CTRL_STAT);
  790. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  791. swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  792. dap_dp_write_reg(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
  793. dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
  794. if ((retval=jtag_execute_queue()) != ERROR_OK)
  795. return retval;
  796. /* Check that we have debug power domains activated */
  797. while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10))
  798. {
  799. LOG_DEBUG("swjdp: wait CDBGPWRUPACK");
  800. dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
  801. if ((retval=jtag_execute_queue()) != ERROR_OK)
  802. return retval;
  803. alive_sleep(10);
  804. }
  805. while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10))
  806. {
  807. LOG_DEBUG("swjdp: wait CSYSPWRUPACK");
  808. dap_dp_read_reg(swjdp, &ctrlstat, DP_CTRL_STAT);
  809. if ((retval=jtag_execute_queue()) != ERROR_OK)
  810. return retval;
  811. alive_sleep(10);
  812. }
  813. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  814. /* With debug power on we can activate OVERRUN checking */
  815. swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
  816. dap_dp_write_reg(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
  817. dap_dp_read_reg(swjdp, &dummy, DP_CTRL_STAT);
  818. dap_ap_read_reg_u32(swjdp, 0xFC, &idreg);
  819. dap_ap_read_reg_u32(swjdp, 0xF8, &romaddr);
  820. LOG_DEBUG("AHB-AP ID Register 0x%" PRIx32 ", Debug ROM Address 0x%" PRIx32 "", idreg, romaddr);
  821. return ERROR_OK;
  822. }
  823. char * class_description[16] ={
  824. "Reserved",
  825. "ROM table","Reserved","Reserved","Reserved","Reserved","Reserved","Reserved","Reserved",
  826. "CoreSight component","Reserved","Peripheral Test Block","Reserved","DESS","Generic IP component","Non standard layout"};
  827. int dap_info_command(struct command_context_s *cmd_ctx, swjdp_common_t *swjdp, int apsel)
  828. {
  829. uint32_t dbgbase,apid;
  830. int romtable_present = 0;
  831. uint8_t mem_ap;
  832. uint32_t apselold;
  833. apselold = swjdp->apsel;
  834. dap_ap_select(swjdp, apsel);
  835. dap_ap_read_reg_u32(swjdp, 0xF8, &dbgbase);
  836. dap_ap_read_reg_u32(swjdp, 0xFC, &apid);
  837. swjdp_transaction_endcheck(swjdp);
  838. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  839. mem_ap = ((apid&0x10000) && ((apid&0x0F) != 0));
  840. command_print(cmd_ctx, "ap identification register 0x%8.8" PRIx32 "", apid);
  841. if (apid)
  842. {
  843. switch (apid&0x0F)
  844. {
  845. case 0:
  846. command_print(cmd_ctx, "\tType is jtag-ap");
  847. break;
  848. case 1:
  849. command_print(cmd_ctx, "\tType is mem-ap AHB");
  850. break;
  851. case 2:
  852. command_print(cmd_ctx, "\tType is mem-ap APB");
  853. break;
  854. default:
  855. command_print(cmd_ctx, "\tUnknown AP-type");
  856. break;
  857. }
  858. command_print(cmd_ctx, "ap debugbase 0x%8.8" PRIx32 "", dbgbase);
  859. }
  860. else
  861. {
  862. command_print(cmd_ctx, "No AP found at this apsel 0x%x", apsel);
  863. }
  864. romtable_present = ((mem_ap) && (dbgbase != 0xFFFFFFFF));
  865. if (romtable_present)
  866. {
  867. uint32_t cid0,cid1,cid2,cid3,memtype,romentry;
  868. uint16_t entry_offset;
  869. /* bit 16 of apid indicates a memory access port */
  870. if (dbgbase&0x02)
  871. {
  872. command_print(cmd_ctx, "\tValid ROM table present");
  873. }
  874. else
  875. {
  876. command_print(cmd_ctx, "\tROM table in legacy format" );
  877. }
  878. /* Now we read ROM table ID registers, ref. ARM IHI 0029B sec */
  879. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000)|0xFF0, &cid0);
  880. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000)|0xFF4, &cid1);
  881. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000)|0xFF8, &cid2);
  882. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000)|0xFFC, &cid3);
  883. mem_ap_read_u32(swjdp, (dbgbase&0xFFFFF000)|0xFCC, &memtype);
  884. swjdp_transaction_endcheck(swjdp);
  885. command_print(cmd_ctx, "\tCID3 0x%" PRIx32 ", CID2 0x%" PRIx32 ", CID1 0x%" PRIx32 " CID0, 0x%" PRIx32,cid3,cid2,cid1,cid0);
  886. if (memtype&0x01)
  887. {
  888. command_print(cmd_ctx, "\tMEMTYPE system memory present on bus");
  889. }
  890. else
  891. {
  892. command_print(cmd_ctx, "\tMEMTYPE system memory not present. Dedicated debug bus" );
  893. }
  894. /* Now we read ROM table entries from dbgbase&0xFFFFF000)|0x000 until we get 0x00000000 */
  895. entry_offset = 0;
  896. do
  897. {
  898. mem_ap_read_atomic_u32(swjdp, (dbgbase&0xFFFFF000)|entry_offset, &romentry);
  899. command_print(cmd_ctx, "\tROMTABLE[0x%x] = 0x%" PRIx32 "",entry_offset,romentry);
  900. if (romentry&0x01)
  901. {
  902. uint32_t c_cid0,c_cid1,c_cid2,c_cid3,c_pid0,c_pid1,c_pid2,c_pid3,c_pid4,component_start;
  903. uint32_t component_base = (uint32_t)((dbgbase&0xFFFFF000)+(int)(romentry&0xFFFFF000));
  904. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFE0, &c_pid0);
  905. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFE4, &c_pid1);
  906. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFE8, &c_pid2);
  907. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFEC, &c_pid3);
  908. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFD0, &c_pid4);
  909. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFF0, &c_cid0);
  910. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFF4, &c_cid1);
  911. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFF8, &c_cid2);
  912. mem_ap_read_atomic_u32(swjdp, (component_base&0xFFFFF000)|0xFFC, &c_cid3);
  913. component_start = component_base - 0x1000*(c_pid4>>4);
  914. command_print(cmd_ctx, "\t\tComponent base address 0x%" PRIx32 ", pid4 0x%" PRIx32 ", start address 0x%" PRIx32 "",component_base,c_pid4,component_start);
  915. command_print(cmd_ctx, "\t\tComponent cid1 0x%" PRIx32 ", class is %s",c_cid1,class_description[(c_cid1>>4)&0xF]); /* Se ARM DDI 0314 C Table 2.2 */
  916. command_print(cmd_ctx, "\t\tCID3 0x%" PRIx32 ", CID2 0x%" PRIx32 ", CID1 0x%" PRIx32 ", CID0, 0x%" PRIx32 "",c_cid3,c_cid2,c_cid1,c_cid0);
  917. command_print(cmd_ctx, "\t\tPID3 0x%" PRIx32 ", PID2 0x%" PRIx32 ", PID1 0x%" PRIx32 ", PID0, 0x%" PRIx32 "",c_pid3,c_pid2,c_pid1,c_pid0);
  918. /* For CoreSight components, (c_cid1>>4)&0xF==9 , we also read 0xFC8 DevId and 0xFCC DevType */
  919. }
  920. else
  921. {
  922. if (romentry)
  923. command_print(cmd_ctx, "\t\tComponent not present");
  924. else
  925. command_print(cmd_ctx, "\t\tEnd of ROM table");
  926. }
  927. entry_offset += 4;
  928. } while (romentry>0);
  929. }
  930. else
  931. {
  932. command_print(cmd_ctx, "\tNo ROM table present");
  933. }
  934. dap_ap_select(swjdp, apselold);
  935. return ERROR_OK;
  936. }