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.
 
 
 
 
 
 

1015 lines
29 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. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. /***************************************************************************
  24. * *
  25. * CoreSight (Light?) SerialWireJtagDebugPort *
  26. * *
  27. * CoreSightâ„¢ DAP-Lite TRM, ARM DDI 0316A *
  28. * Cortex-M3â„¢ TRM, ARM DDI 0337C *
  29. * *
  30. ***************************************************************************/
  31. #ifdef HAVE_CONFIG_H
  32. #include "config.h"
  33. #endif
  34. #include "replacements.h"
  35. #include "cortex_m3.h"
  36. #include "cortex_swjdp.h"
  37. #include "jtag.h"
  38. #include "log.h"
  39. #include <stdlib.h>
  40. /*
  41. * Transaction Mode:
  42. * swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  43. * Uses Overrun checking mode and does not do actual JTAG send/receive or transaction
  44. * result checking until swjdp_end_transaction()
  45. * This must be done before using or deallocating any return variables.
  46. * swjdp->trans_mode == TRANS_MODE_ATOMIC
  47. * All reads and writes to the AHB bus are checked for valid completion, and return values
  48. * are immediatley available.
  49. */
  50. /***************************************************************************
  51. * *
  52. * DPACC and APACC scanchain access through JTAG-DR *
  53. * *
  54. ***************************************************************************/
  55. /* Scan out and in from target ordered u8 buffers */
  56. int swjdp_scan(arm_jtag_t *jtag_info, u8 instr, u8 reg_addr, u8 RnW, u8 *outvalue, u8 *invalue, u8 *ack)
  57. {
  58. scan_field_t fields[2];
  59. u8 out_addr_buf;
  60. jtag_add_end_state(TAP_RTI);
  61. arm_jtag_set_instr(jtag_info, instr, NULL);
  62. fields[0].device = jtag_info->chain_pos;
  63. fields[0].num_bits = 3;
  64. buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
  65. fields[0].out_value = &out_addr_buf;
  66. fields[0].out_mask = NULL;
  67. fields[0].in_value = ack;
  68. fields[0].in_check_value = NULL;
  69. fields[0].in_check_mask = NULL;
  70. fields[0].in_handler = NULL;
  71. fields[0].in_handler_priv = NULL;
  72. fields[1].device = jtag_info->chain_pos;
  73. fields[1].num_bits = 32;
  74. fields[1].out_value = outvalue;
  75. fields[1].out_mask = NULL;
  76. fields[1].in_value = invalue;
  77. fields[1].in_handler = NULL;
  78. fields[1].in_handler_priv = NULL;
  79. fields[1].in_check_value = NULL;
  80. fields[1].in_check_mask = NULL;
  81. jtag_add_dr_scan(2, fields, -1);
  82. return ERROR_OK;
  83. }
  84. /* Scan out and in from host ordered u32 variables */
  85. int swjdp_scan_u32(arm_jtag_t *jtag_info, u8 instr, u8 reg_addr, u8 RnW, u32 outvalue, u32 *invalue, u8 *ack)
  86. {
  87. scan_field_t fields[2];
  88. u8 out_value_buf[4];
  89. u8 out_addr_buf;
  90. jtag_add_end_state(TAP_RTI);
  91. arm_jtag_set_instr(jtag_info, instr, NULL);
  92. fields[0].device = jtag_info->chain_pos;
  93. fields[0].num_bits = 3;
  94. buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
  95. fields[0].out_value = &out_addr_buf;
  96. fields[0].out_mask = NULL;
  97. fields[0].in_value = ack;
  98. fields[0].in_check_value = NULL;
  99. fields[0].in_check_mask = NULL;
  100. fields[0].in_handler = NULL;
  101. fields[0].in_handler_priv = NULL;
  102. fields[1].device = jtag_info->chain_pos;
  103. fields[1].num_bits = 32;
  104. buf_set_u32(out_value_buf, 0, 32, outvalue);
  105. fields[1].out_value = out_value_buf;
  106. fields[1].out_mask = NULL;
  107. fields[1].in_value = NULL;
  108. if (invalue)
  109. {
  110. fields[1].in_handler = arm_jtag_buf_to_u32;
  111. fields[1].in_handler_priv = invalue;
  112. }
  113. else
  114. {
  115. fields[1].in_handler = NULL;
  116. fields[1].in_handler_priv = NULL;
  117. }
  118. fields[1].in_check_value = NULL;
  119. fields[1].in_check_mask = NULL;
  120. jtag_add_dr_scan(2, fields, -1);
  121. return ERROR_OK;
  122. }
  123. /* scan_inout_check adds one extra inscan for DPAP_READ commands to read variables */
  124. int scan_inout_check(swjdp_common_t *swjdp, u8 instr, u8 reg_addr, u8 RnW, u8 *outvalue, u8 *invalue)
  125. {
  126. swjdp_scan(swjdp->jtag_info, instr, reg_addr, RnW, outvalue, NULL, NULL);
  127. if ((RnW == DPAP_READ) && (invalue != NULL))
  128. {
  129. swjdp_scan(swjdp->jtag_info, SWJDP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
  130. }
  131. /* In TRANS_MODE_ATOMIC all SWJDP_IR_APACC transactions wait for ack=OK/FAULT and the check CTRL_STAT */
  132. if ((instr == SWJDP_IR_APACC) && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
  133. {
  134. return swjdp_transaction_endcheck(swjdp);
  135. }
  136. return ERROR_OK;
  137. }
  138. int scan_inout_check_u32(swjdp_common_t *swjdp, u8 instr, u8 reg_addr, u8 RnW, u32 outvalue, u32 *invalue)
  139. {
  140. swjdp_scan_u32(swjdp->jtag_info, instr, reg_addr, RnW, outvalue, NULL, NULL);
  141. if ((RnW==DPAP_READ) && (invalue != NULL))
  142. {
  143. swjdp_scan_u32(swjdp->jtag_info, SWJDP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, invalue, &swjdp->ack);
  144. }
  145. /* In TRANS_MODE_ATOMIC all SWJDP_IR_APACC transactions wait for ack=OK/FAULT and then check CTRL_STAT */
  146. if ((instr == SWJDP_IR_APACC) && (swjdp->trans_mode == TRANS_MODE_ATOMIC))
  147. {
  148. return swjdp_transaction_endcheck(swjdp);
  149. }
  150. return ERROR_OK;
  151. }
  152. int swjdp_transaction_endcheck(swjdp_common_t *swjdp)
  153. {
  154. int retval;
  155. u32 ctrlstat;
  156. keep_alive();
  157. /* Danger!!!! BROKEN!!!! */
  158. scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  159. /* Danger!!!! BROKEN!!!! Why will jtag_execute_queue() fail here????
  160. R956 introduced the check on return value here and now Michael Schwingen reports
  161. that this code no longer works....
  162. https://lists.berlios.de/pipermail/openocd-development/2008-September/003107.html
  163. */
  164. if ((retval=jtag_execute_queue())!=ERROR_OK)
  165. {
  166. LOG_ERROR("BUG: Why does this fail the first time????");
  167. }
  168. /* Why??? second time it works??? */
  169. scan_inout_check_u32(swjdp, SWJDP_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. long long then=timeval_ms();
  174. while (swjdp->ack != 2)
  175. {
  176. if (swjdp->ack == 1)
  177. {
  178. if ((timeval_ms()-then) > 1000)
  179. {
  180. LOG_WARNING("Timeout (1000ms) waiting for ACK = OK/FAULT in SWJDP transaction");
  181. return ERROR_JTAG_DEVICE_ERROR;
  182. }
  183. }
  184. else
  185. {
  186. LOG_WARNING("Invalid ACK in SWJDP transaction");
  187. return ERROR_JTAG_DEVICE_ERROR;
  188. }
  189. scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  190. if ((retval=jtag_execute_queue())!=ERROR_OK)
  191. return retval;
  192. swjdp->ack = swjdp->ack & 0x7;
  193. }
  194. /* Check for STICKYERR and STICKYORUN */
  195. if (ctrlstat & (SSTICKYORUN | SSTICKYERR))
  196. {
  197. LOG_DEBUG("swjdp: CTRL/STAT error 0x%x", ctrlstat);
  198. /* Check power to debug regions */
  199. if ((ctrlstat & 0xf0000000) != 0xf0000000)
  200. {
  201. ahbap_debugport_init(swjdp);
  202. }
  203. else
  204. {
  205. u32 dcb_dhcsr,nvic_shcsr, nvic_bfar, nvic_cfsr;
  206. if (ctrlstat & SSTICKYORUN)
  207. LOG_ERROR("SWJ-DP OVERRUN - check clock or reduce jtag speed");
  208. if (ctrlstat & SSTICKYERR)
  209. LOG_ERROR("SWJ-DP STICKY ERROR");
  210. /* Clear Sticky Error Bits */
  211. scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_WRITE, swjdp->dp_ctrl_stat | SSTICKYORUN | SSTICKYERR, NULL);
  212. scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
  213. if ((retval=jtag_execute_queue())!=ERROR_OK)
  214. return retval;
  215. LOG_DEBUG("swjdp: status 0x%x", ctrlstat);
  216. /* Can we find out the reason for the error ?? */
  217. ahbap_read_system_atomic_u32(swjdp, DCB_DHCSR, &dcb_dhcsr);
  218. ahbap_read_system_atomic_u32(swjdp, NVIC_SHCSR, &nvic_shcsr);
  219. ahbap_read_system_atomic_u32(swjdp, NVIC_CFSR, &nvic_cfsr);
  220. ahbap_read_system_atomic_u32(swjdp, NVIC_BFAR, &nvic_bfar);
  221. LOG_ERROR("dcb_dhcsr 0x%x, nvic_shcsr 0x%x, nvic_cfsr 0x%x, nvic_bfar 0x%x", dcb_dhcsr, nvic_shcsr, nvic_cfsr, nvic_bfar);
  222. }
  223. if ((retval=jtag_execute_queue())!=ERROR_OK)
  224. return retval;
  225. return ERROR_JTAG_DEVICE_ERROR;
  226. }
  227. return ERROR_OK;
  228. }
  229. /***************************************************************************
  230. * *
  231. * DP and AHB-AP register access through APACC and DPACC *
  232. * *
  233. ***************************************************************************/
  234. int swjdp_write_dpacc(swjdp_common_t *swjdp, u32 value, u8 reg_addr)
  235. {
  236. return scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, reg_addr, DPAP_WRITE, value, NULL);
  237. }
  238. int swjdp_read_dpacc(swjdp_common_t *swjdp, u32 *value, u8 reg_addr)
  239. {
  240. return scan_inout_check_u32(swjdp, SWJDP_IR_DPACC, reg_addr, DPAP_READ, 0, value);
  241. }
  242. int swjdp_bankselect_apacc(swjdp_common_t *swjdp,u32 reg_addr)
  243. {
  244. u32 select;
  245. select = (reg_addr & 0xFF0000F0);
  246. if (select != swjdp->dp_select_value)
  247. {
  248. swjdp_write_dpacc(swjdp, select, DP_SELECT);
  249. swjdp->dp_select_value = select;
  250. }
  251. return ERROR_OK;
  252. }
  253. int ahbap_write_reg(swjdp_common_t *swjdp, u32 reg_addr, u8* out_value_buf)
  254. {
  255. swjdp_bankselect_apacc(swjdp, reg_addr);
  256. scan_inout_check(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_WRITE, out_value_buf, NULL);
  257. return ERROR_OK;
  258. }
  259. int ahbap_read_reg(swjdp_common_t *swjdp, u32 reg_addr, u8 *in_value_buf)
  260. {
  261. swjdp_bankselect_apacc(swjdp, reg_addr);
  262. scan_inout_check(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_READ, 0, in_value_buf);
  263. return ERROR_OK;
  264. }
  265. int ahbap_write_reg_u32(swjdp_common_t *swjdp, u32 reg_addr, u32 value)
  266. {
  267. u8 out_value_buf[4];
  268. buf_set_u32(out_value_buf, 0, 32, value);
  269. swjdp_bankselect_apacc(swjdp, reg_addr);
  270. scan_inout_check(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_WRITE, out_value_buf, NULL);
  271. return ERROR_OK;
  272. }
  273. int ahbap_read_reg_u32(swjdp_common_t *swjdp, u32 reg_addr, u32 *value)
  274. {
  275. swjdp_bankselect_apacc(swjdp, reg_addr);
  276. scan_inout_check_u32(swjdp, SWJDP_IR_APACC, reg_addr, DPAP_READ, 0, value);
  277. return ERROR_OK;
  278. }
  279. /***************************************************************************
  280. * *
  281. * AHB-AP access to memory and system registers on AHB bus *
  282. * *
  283. ***************************************************************************/
  284. int ahbap_setup_accessport(swjdp_common_t *swjdp, u32 csw, u32 tar)
  285. {
  286. csw = csw | CSW_DBGSWENABLE | CSW_MASTER_DEBUG | CSW_HPROT;
  287. if (csw != swjdp->ap_csw_value)
  288. {
  289. /* LOG_DEBUG("swjdp : Set CSW %x",csw); */
  290. ahbap_write_reg_u32(swjdp, AHBAP_CSW, csw );
  291. swjdp->ap_csw_value = csw;
  292. }
  293. if (tar != swjdp->ap_tar_value)
  294. {
  295. /* LOG_DEBUG("swjdp : Set TAR %x",tar); */
  296. ahbap_write_reg_u32(swjdp, AHBAP_TAR, tar );
  297. swjdp->ap_tar_value = tar;
  298. }
  299. if (csw & CSW_ADDRINC_MASK)
  300. {
  301. /* Do not cache TAR value when autoincrementing */
  302. swjdp->ap_tar_value = -1;
  303. }
  304. return ERROR_OK;
  305. }
  306. /*****************************************************************************
  307. * *
  308. * ahbap_read_system_u32(swjdp_common_t *swjdp, u32 address, u32 *value) *
  309. * *
  310. * Read a u32 value from memory or system register *
  311. * Functionally equivalent to target_read_u32(target, address, u32 *value), *
  312. * but with less overhead *
  313. *****************************************************************************/
  314. int ahbap_read_system_u32(swjdp_common_t *swjdp, u32 address, u32 *value)
  315. {
  316. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  317. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
  318. ahbap_read_reg_u32(swjdp, AHBAP_BD0 | (address & 0xC), value );
  319. return ERROR_OK;
  320. }
  321. int ahbap_read_system_atomic_u32(swjdp_common_t *swjdp, u32 address, u32 *value)
  322. {
  323. ahbap_read_system_u32(swjdp, address, value);
  324. return swjdp_transaction_endcheck(swjdp);
  325. }
  326. /*****************************************************************************
  327. * *
  328. * ahbap_write_system_u32(swjdp_common_t *swjdp, u32 address, u32 value) *
  329. * *
  330. * Write a u32 value to memory or system register *
  331. * *
  332. *****************************************************************************/
  333. int ahbap_write_system_u32(swjdp_common_t *swjdp, u32 address, u32 value)
  334. {
  335. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  336. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, address & 0xFFFFFFF0);
  337. ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (address & 0xC), value );
  338. return ERROR_OK;
  339. }
  340. int ahbap_write_system_atomic_u32(swjdp_common_t *swjdp, u32 address, u32 value)
  341. {
  342. ahbap_write_system_u32(swjdp, address, value);
  343. return swjdp_transaction_endcheck(swjdp);
  344. }
  345. /*****************************************************************************
  346. * *
  347. * ahbap_write_buf(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address) *
  348. * *
  349. * Write a buffer in target order (little endian) *
  350. * *
  351. *****************************************************************************/
  352. int ahbap_write_buf_u32(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  353. {
  354. u32 outvalue;
  355. int wcount, blocksize, writecount, errorcount = 0, retval = ERROR_OK;
  356. u32 adr = address;
  357. u8* pBuffer = buffer;
  358. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  359. count >>= 2;
  360. wcount = count;
  361. /* if we have an unaligned access - reorder data */
  362. if (adr & 0x3u)
  363. {
  364. for (writecount = 0; writecount < count; writecount++)
  365. {
  366. int i;
  367. outvalue = *((u32*)pBuffer);
  368. for (i = 0; i < 4; i++ )
  369. {
  370. *((u8*)pBuffer + (adr & 0x3)) = outvalue;
  371. outvalue >>= 8;
  372. adr++;
  373. }
  374. pBuffer += 4;
  375. }
  376. }
  377. while (wcount > 0)
  378. {
  379. /* Adjust to write blocks within 4K aligned boundaries */
  380. blocksize = (0x1000 - (0xFFF & address)) >> 2;
  381. if (wcount < blocksize)
  382. blocksize = wcount;
  383. /* handle unaligned data at 4k boundary */
  384. if (blocksize == 0)
  385. blocksize = 1;
  386. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
  387. for (writecount = 0; writecount < blocksize; writecount++)
  388. {
  389. ahbap_write_reg(swjdp, AHBAP_DRW, buffer + 4 * writecount );
  390. }
  391. if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
  392. {
  393. wcount = wcount - blocksize;
  394. address = address + 4 * blocksize;
  395. buffer = buffer + 4 * blocksize;
  396. }
  397. else
  398. {
  399. errorcount++;
  400. }
  401. if (errorcount > 1)
  402. {
  403. LOG_WARNING("Block write error address 0x%x, wcount 0x%x", address, wcount);
  404. return ERROR_JTAG_DEVICE_ERROR;
  405. }
  406. }
  407. return retval;
  408. }
  409. int ahbap_write_buf_packed_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  410. {
  411. u32 outvalue;
  412. int retval = ERROR_OK;
  413. int wcount, blocksize, writecount, i;
  414. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  415. wcount = count >> 1;
  416. while (wcount > 0)
  417. {
  418. int nbytes;
  419. /* Adjust to read within 4K block boundaries */
  420. blocksize = (0x1000 - (0xFFF & address)) >> 1;
  421. if (wcount < blocksize)
  422. blocksize = wcount;
  423. /* handle unaligned data at 4k boundary */
  424. if (blocksize == 0)
  425. blocksize = 1;
  426. ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  427. writecount = blocksize;
  428. do
  429. {
  430. nbytes = MIN((writecount << 1), 4);
  431. if (nbytes < 4 )
  432. {
  433. if (ahbap_write_buf_u16(swjdp, buffer, nbytes, address) != ERROR_OK)
  434. {
  435. LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
  436. return ERROR_JTAG_DEVICE_ERROR;
  437. }
  438. address += nbytes >> 1;
  439. }
  440. else
  441. {
  442. outvalue = *((u32*)buffer);
  443. for (i = 0; i < nbytes; i++ )
  444. {
  445. *((u8*)buffer + (address & 0x3)) = outvalue;
  446. outvalue >>= 8;
  447. address++;
  448. }
  449. outvalue = *((u32*)buffer);
  450. ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue);
  451. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  452. {
  453. LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
  454. return ERROR_JTAG_DEVICE_ERROR;
  455. }
  456. }
  457. buffer += nbytes >> 1;
  458. writecount -= nbytes >> 1;
  459. } while (writecount);
  460. wcount -= blocksize;
  461. }
  462. return retval;
  463. }
  464. int ahbap_write_buf_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  465. {
  466. u32 outvalue;
  467. int retval = ERROR_OK;
  468. if (count >= 4)
  469. return ahbap_write_buf_packed_u16(swjdp, buffer, count, address);
  470. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  471. while (count > 0)
  472. {
  473. ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  474. outvalue = *((u16*)buffer) << 8 * (address & 0x3);
  475. ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue );
  476. retval = swjdp_transaction_endcheck(swjdp);
  477. count -= 2;
  478. address += 2;
  479. buffer += 2;
  480. }
  481. return retval;
  482. }
  483. int ahbap_write_buf_packed_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  484. {
  485. u32 outvalue;
  486. int retval = ERROR_OK;
  487. int wcount, blocksize, writecount, i;
  488. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  489. wcount = count;
  490. while (wcount > 0)
  491. {
  492. int nbytes;
  493. /* Adjust to read within 4K block boundaries */
  494. blocksize = (0x1000 - (0xFFF & address));
  495. if (wcount < blocksize)
  496. blocksize = wcount;
  497. ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  498. writecount = blocksize;
  499. do
  500. {
  501. nbytes = MIN(writecount, 4);
  502. if (nbytes < 4 )
  503. {
  504. if (ahbap_write_buf_u8(swjdp, buffer, nbytes, address) != ERROR_OK)
  505. {
  506. LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
  507. return ERROR_JTAG_DEVICE_ERROR;
  508. }
  509. address += nbytes;
  510. }
  511. else
  512. {
  513. outvalue = *((u32*)buffer);
  514. for (i = 0; i < nbytes; i++ )
  515. {
  516. *((u8*)buffer + (address & 0x3)) = outvalue;
  517. outvalue >>= 8;
  518. address++;
  519. }
  520. outvalue = *((u32*)buffer);
  521. ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue);
  522. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  523. {
  524. LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
  525. return ERROR_JTAG_DEVICE_ERROR;
  526. }
  527. }
  528. buffer += nbytes;
  529. writecount -= nbytes;
  530. } while (writecount);
  531. wcount -= blocksize;
  532. }
  533. return retval;
  534. }
  535. int ahbap_write_buf_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  536. {
  537. u32 outvalue;
  538. int retval = ERROR_OK;
  539. if (count >= 4)
  540. return ahbap_write_buf_packed_u8(swjdp, buffer, count, address);
  541. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  542. while (count > 0)
  543. {
  544. ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  545. outvalue = *((u8*)buffer) << 8 * (address & 0x3);
  546. ahbap_write_reg_u32(swjdp, AHBAP_DRW, outvalue );
  547. retval = swjdp_transaction_endcheck(swjdp);
  548. count--;
  549. address++;
  550. buffer++;
  551. }
  552. return retval;
  553. }
  554. /*********************************************************************************
  555. * *
  556. * ahbap_read_buf_u32(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address) *
  557. * *
  558. * Read block fast in target order (little endian) into a buffer *
  559. * *
  560. **********************************************************************************/
  561. int ahbap_read_buf_u32(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  562. {
  563. int wcount, blocksize, readcount, errorcount = 0, retval = ERROR_OK;
  564. u32 adr = address;
  565. u8* pBuffer = buffer;
  566. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  567. count >>= 2;
  568. wcount = count;
  569. while (wcount > 0)
  570. {
  571. /* Adjust to read within 4K block boundaries */
  572. blocksize = (0x1000 - (0xFFF & address)) >> 2;
  573. if (wcount < blocksize)
  574. blocksize = wcount;
  575. /* handle unaligned data at 4k boundary */
  576. if (blocksize == 0)
  577. blocksize = 1;
  578. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_SINGLE, address);
  579. /* Scan out first read */
  580. swjdp_scan(swjdp->jtag_info, SWJDP_IR_APACC, AHBAP_DRW, DPAP_READ, 0, NULL, NULL);
  581. for (readcount = 0; readcount < blocksize - 1; readcount++)
  582. {
  583. /* Scan out read instruction and scan in previous value */
  584. swjdp_scan(swjdp->jtag_info, SWJDP_IR_APACC, AHBAP_DRW, DPAP_READ, 0, buffer + 4 * readcount, &swjdp->ack);
  585. }
  586. /* Scan in last value */
  587. swjdp_scan(swjdp->jtag_info, SWJDP_IR_DPACC, DP_RDBUFF, DPAP_READ, 0, buffer + 4 * readcount, &swjdp->ack);
  588. if (swjdp_transaction_endcheck(swjdp) == ERROR_OK)
  589. {
  590. wcount = wcount - blocksize;
  591. address += 4 * blocksize;
  592. buffer += 4 * blocksize;
  593. }
  594. else
  595. {
  596. errorcount++;
  597. }
  598. if (errorcount > 1)
  599. {
  600. LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
  601. return ERROR_JTAG_DEVICE_ERROR;
  602. }
  603. }
  604. /* if we have an unaligned access - reorder data */
  605. if (adr & 0x3u)
  606. {
  607. for (readcount = 0; readcount < count; readcount++)
  608. {
  609. int i;
  610. u32 data = *((u32*)pBuffer);
  611. for (i = 0; i < 4; i++ )
  612. {
  613. *((u8*)pBuffer) = (data >> 8 * (adr & 0x3));
  614. pBuffer++;
  615. adr++;
  616. }
  617. }
  618. }
  619. return retval;
  620. }
  621. int ahbap_read_buf_packed_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  622. {
  623. u32 invalue;
  624. int retval = ERROR_OK;
  625. int wcount, blocksize, readcount, i;
  626. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  627. wcount = count >> 1;
  628. while (wcount > 0)
  629. {
  630. int nbytes;
  631. /* Adjust to read within 4K block boundaries */
  632. blocksize = (0x1000 - (0xFFF & address)) >> 1;
  633. if (wcount < blocksize)
  634. blocksize = wcount;
  635. ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_PACKED, address);
  636. /* handle unaligned data at 4k boundary */
  637. if (blocksize == 0)
  638. blocksize = 1;
  639. readcount = blocksize;
  640. do
  641. {
  642. ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
  643. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  644. {
  645. LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
  646. return ERROR_JTAG_DEVICE_ERROR;
  647. }
  648. nbytes = MIN((readcount << 1), 4);
  649. for (i = 0; i < nbytes; i++ )
  650. {
  651. *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
  652. buffer++;
  653. address++;
  654. }
  655. readcount -= (nbytes >> 1);
  656. } while (readcount);
  657. wcount -= blocksize;
  658. }
  659. return retval;
  660. }
  661. int ahbap_read_buf_u16(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  662. {
  663. u32 invalue, i;
  664. int retval = ERROR_OK;
  665. if (count >= 4)
  666. return ahbap_read_buf_packed_u16(swjdp, buffer, count, address);
  667. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  668. while (count > 0)
  669. {
  670. ahbap_setup_accessport(swjdp, CSW_16BIT | CSW_ADDRINC_SINGLE, address);
  671. ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
  672. retval = swjdp_transaction_endcheck(swjdp);
  673. if (address & 0x1)
  674. {
  675. for (i = 0; i < 2; i++ )
  676. {
  677. *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
  678. buffer++;
  679. address++;
  680. }
  681. }
  682. else
  683. {
  684. *((u16*)buffer) = (invalue >> 8 * (address & 0x3));
  685. address += 2;
  686. buffer += 2;
  687. }
  688. count -= 2;
  689. }
  690. return retval;
  691. }
  692. int ahbap_read_buf_packed_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  693. {
  694. u32 invalue;
  695. int retval = ERROR_OK;
  696. int wcount, blocksize, readcount, i;
  697. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  698. wcount = count;
  699. while (wcount > 0)
  700. {
  701. int nbytes;
  702. /* Adjust to read within 4K block boundaries */
  703. blocksize = (0x1000 - (0xFFF & address));
  704. if (wcount < blocksize)
  705. blocksize = wcount;
  706. ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_PACKED, address);
  707. readcount = blocksize;
  708. do
  709. {
  710. ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
  711. if (swjdp_transaction_endcheck(swjdp) != ERROR_OK)
  712. {
  713. LOG_WARNING("Block read error address 0x%x, count 0x%x", address, count);
  714. return ERROR_JTAG_DEVICE_ERROR;
  715. }
  716. nbytes = MIN(readcount, 4);
  717. for (i = 0; i < nbytes; i++ )
  718. {
  719. *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
  720. buffer++;
  721. address++;
  722. }
  723. readcount -= nbytes;
  724. } while (readcount);
  725. wcount -= blocksize;
  726. }
  727. return retval;
  728. }
  729. int ahbap_read_buf_u8(swjdp_common_t *swjdp, u8 *buffer, int count, u32 address)
  730. {
  731. u32 invalue;
  732. int retval = ERROR_OK;
  733. if (count >= 4)
  734. return ahbap_read_buf_packed_u8(swjdp, buffer, count, address);
  735. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  736. while (count > 0)
  737. {
  738. ahbap_setup_accessport(swjdp, CSW_8BIT | CSW_ADDRINC_SINGLE, address);
  739. ahbap_read_reg_u32(swjdp, AHBAP_DRW, &invalue );
  740. retval = swjdp_transaction_endcheck(swjdp);
  741. *((u8*)buffer) = (invalue >> 8 * (address & 0x3));
  742. count--;
  743. address++;
  744. buffer++;
  745. }
  746. return retval;
  747. }
  748. int ahbap_read_coreregister_u32(swjdp_common_t *swjdp, u32 *value, int regnum)
  749. {
  750. int retval;
  751. u32 dcrdr;
  752. /* because the DCB_DCRDR is used for the emulated dcc channel
  753. * we gave to save/restore the DCB_DCRDR when used */
  754. ahbap_read_system_atomic_u32(swjdp, DCB_DCRDR, &dcrdr);
  755. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  756. /* ahbap_write_system_u32(swjdp, DCB_DCRSR, regnum); */
  757. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  758. ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRSR & 0xC), regnum );
  759. /* ahbap_read_system_u32(swjdp, DCB_DCRDR, value); */
  760. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  761. ahbap_read_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRDR & 0xC), value );
  762. retval = swjdp_transaction_endcheck(swjdp);
  763. ahbap_write_system_atomic_u32(swjdp, DCB_DCRDR, dcrdr);
  764. return retval;
  765. }
  766. int ahbap_write_coreregister_u32(swjdp_common_t *swjdp, u32 value, int regnum)
  767. {
  768. int retval;
  769. u32 dcrdr;
  770. /* because the DCB_DCRDR is used for the emulated dcc channel
  771. * we gave to save/restore the DCB_DCRDR when used */
  772. ahbap_read_system_atomic_u32(swjdp, DCB_DCRDR, &dcrdr);
  773. swjdp->trans_mode = TRANS_MODE_COMPOSITE;
  774. /* ahbap_write_system_u32(swjdp, DCB_DCRDR, core_regs[i]); */
  775. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRDR & 0xFFFFFFF0);
  776. ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRDR & 0xC), value );
  777. /* ahbap_write_system_u32(swjdp, DCB_DCRSR, i | DCRSR_WnR ); */
  778. ahbap_setup_accessport(swjdp, CSW_32BIT | CSW_ADDRINC_OFF, DCB_DCRSR & 0xFFFFFFF0);
  779. ahbap_write_reg_u32(swjdp, AHBAP_BD0 | (DCB_DCRSR & 0xC), regnum | DCRSR_WnR );
  780. retval = swjdp_transaction_endcheck(swjdp);
  781. ahbap_write_system_atomic_u32(swjdp, DCB_DCRDR, dcrdr);
  782. return retval;
  783. }
  784. int ahbap_debugport_init(swjdp_common_t *swjdp)
  785. {
  786. u32 idreg, romaddr, dummy;
  787. u32 ctrlstat;
  788. int cnt = 0;
  789. int retval;
  790. LOG_DEBUG(" ");
  791. swjdp->ap_csw_value = -1;
  792. swjdp->ap_tar_value = -1;
  793. swjdp->trans_mode = TRANS_MODE_ATOMIC;
  794. swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
  795. swjdp_write_dpacc(swjdp, SSTICKYERR, DP_CTRL_STAT);
  796. swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
  797. swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ;
  798. swjdp_write_dpacc(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
  799. swjdp_read_dpacc(swjdp, &ctrlstat, DP_CTRL_STAT);
  800. if ((retval=jtag_execute_queue())!=ERROR_OK)
  801. return retval;
  802. /* Check that we have debug power domains activated */
  803. while (!(ctrlstat & CDBGPWRUPACK) && (cnt++ < 10))
  804. {
  805. LOG_DEBUG("swjdp: wait CDBGPWRUPACK");
  806. swjdp_read_dpacc(swjdp, &ctrlstat, DP_CTRL_STAT);
  807. if ((retval=jtag_execute_queue())!=ERROR_OK)
  808. return retval;
  809. alive_sleep(10);
  810. }
  811. while (!(ctrlstat & CSYSPWRUPACK) && (cnt++ < 10))
  812. {
  813. LOG_DEBUG("swjdp: wait CSYSPWRUPACK");
  814. swjdp_read_dpacc(swjdp, &ctrlstat, DP_CTRL_STAT);
  815. if ((retval=jtag_execute_queue())!=ERROR_OK)
  816. return retval;
  817. alive_sleep(10);
  818. }
  819. swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
  820. /* With debug power on we can activate OVERRUN checking */
  821. swjdp->dp_ctrl_stat = CDBGPWRUPREQ | CSYSPWRUPREQ | CORUNDETECT;
  822. swjdp_write_dpacc(swjdp, swjdp->dp_ctrl_stat, DP_CTRL_STAT);
  823. swjdp_read_dpacc(swjdp, &dummy, DP_CTRL_STAT);
  824. ahbap_read_reg_u32(swjdp, 0xFC, &idreg);
  825. ahbap_read_reg_u32(swjdp, 0xF8, &romaddr);
  826. LOG_DEBUG("AHB-AP ID Register 0x%x, Debug ROM Address 0x%x", idreg, romaddr);
  827. return ERROR_OK;
  828. }