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.
 
 
 
 
 
 

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