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.
 
 
 
 
 
 

1198 lines
32 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2008 digenius technology GmbH. *
  3. * Michael Bruck *
  4. * *
  5. * Copyright (C) 2008,2009 Oyvind Harboe oyvind.harboe@zylin.com *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  21. ***************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. #include "arm_jtag.h"
  26. #include "arm11_dbgtap.h"
  27. #include <helper/time_support.h>
  28. #if 0
  29. #define JTAG_DEBUG(expr ...) do { if (1) \
  30. LOG_DEBUG(expr); } while (0)
  31. #else
  32. #define JTAG_DEBUG(expr ...) do { if (0) \
  33. LOG_DEBUG(expr); } while (0)
  34. #endif
  35. /*
  36. This pathmove goes from Pause-IR to Shift-IR while avoiding RTI. The
  37. behavior of the FTDI driver IIRC was to go via RTI.
  38. Conversely there may be other places in this code where the ARM11 code relies
  39. on the driver to hit through RTI when coming from Update-?R.
  40. */
  41. static const tap_state_t arm11_move_pi_to_si_via_ci[] = {
  42. TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IRSHIFT
  43. };
  44. /* REVISIT no error handling here! */
  45. static void arm11_add_ir_scan_vc(struct jtag_tap *tap, struct scan_field *fields,
  46. tap_state_t state)
  47. {
  48. if (cmd_queue_cur_state == TAP_IRPAUSE)
  49. jtag_add_pathmove(ARRAY_SIZE(arm11_move_pi_to_si_via_ci),
  50. arm11_move_pi_to_si_via_ci);
  51. jtag_add_ir_scan(tap, fields, state);
  52. }
  53. static const tap_state_t arm11_move_pd_to_sd_via_cd[] = {
  54. TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DRSHIFT
  55. };
  56. /* REVISIT no error handling here! */
  57. void arm11_add_dr_scan_vc(struct jtag_tap *tap, int num_fields, struct scan_field *fields,
  58. tap_state_t state)
  59. {
  60. if (cmd_queue_cur_state == TAP_DRPAUSE)
  61. jtag_add_pathmove(ARRAY_SIZE(arm11_move_pd_to_sd_via_cd),
  62. arm11_move_pd_to_sd_via_cd);
  63. jtag_add_dr_scan(tap, num_fields, fields, state);
  64. }
  65. /** Code de-clutter: Construct struct scan_field to write out a value
  66. *
  67. * \param arm11 Target state variable.
  68. * \param num_bits Length of the data field
  69. * \param out_data pointer to the data that will be sent out
  70. * <em > (data is read when it is added to the JTAG queue)</em>
  71. * \param in_data pointer to the memory that will receive data that was clocked in
  72. * <em > (data is written when the JTAG queue is executed)</em>
  73. * \param field target data structure that will be initialized
  74. */
  75. void arm11_setup_field(struct arm11_common *arm11, int num_bits,
  76. void *out_data, void *in_data, struct scan_field *field)
  77. {
  78. field->num_bits = num_bits;
  79. field->out_value = out_data;
  80. field->in_value = in_data;
  81. }
  82. static const char *arm11_ir_to_string(uint8_t ir)
  83. {
  84. const char *s = "unknown";
  85. switch (ir) {
  86. case ARM11_EXTEST:
  87. s = "EXTEST";
  88. break;
  89. case ARM11_SCAN_N:
  90. s = "SCAN_N";
  91. break;
  92. case ARM11_RESTART:
  93. s = "RESTART";
  94. break;
  95. case ARM11_HALT:
  96. s = "HALT";
  97. break;
  98. case ARM11_INTEST:
  99. s = "INTEST";
  100. break;
  101. case ARM11_ITRSEL:
  102. s = "ITRSEL";
  103. break;
  104. case ARM11_IDCODE:
  105. s = "IDCODE";
  106. break;
  107. case ARM11_BYPASS:
  108. s = "BYPASS";
  109. break;
  110. }
  111. return s;
  112. }
  113. /** Write JTAG instruction register
  114. *
  115. * \param arm11 Target state variable.
  116. * \param instr An ARM11 DBGTAP instruction. Use enum #arm11_instructions.
  117. * \param state Pass the final TAP state or ARM11_TAP_DEFAULT for the default value (Pause-IR).
  118. *
  119. * \remarks This adds to the JTAG command queue but does \em not execute it.
  120. */
  121. void arm11_add_IR(struct arm11_common *arm11, uint8_t instr, tap_state_t state)
  122. {
  123. struct jtag_tap *tap = arm11->arm.target->tap;
  124. if (buf_get_u32(tap->cur_instr, 0, 5) == instr) {
  125. JTAG_DEBUG("IR <= 0x%02x SKIPPED", instr);
  126. return;
  127. }
  128. JTAG_DEBUG("IR <= %s (0x%02x)", arm11_ir_to_string(instr), instr);
  129. struct scan_field field;
  130. arm11_setup_field(arm11, 5, &instr, NULL, &field);
  131. arm11_add_ir_scan_vc(arm11->arm.target->tap,
  132. &field,
  133. state == ARM11_TAP_DEFAULT ? TAP_IRPAUSE : state);
  134. }
  135. /** Verify data shifted out from Scan Chain Register (SCREG). */
  136. static void arm11_in_handler_SCAN_N(uint8_t *in_value)
  137. {
  138. /* Don't expect JTAG layer to modify bits we didn't ask it to read */
  139. uint8_t v = *in_value & 0x1F;
  140. if (v != 0x10) {
  141. LOG_ERROR("'arm11 target' JTAG error SCREG OUT 0x%02x", v);
  142. jtag_set_error(ERROR_FAIL);
  143. }
  144. }
  145. /** Select and write to Scan Chain Register (SCREG)
  146. *
  147. * This function sets the instruction register to SCAN_N and writes
  148. * the data register with the selected chain number.
  149. *
  150. * http://infocenter.arm.com/help/topic/com.arm.doc.ddi0301f/Cacbjhfg.html
  151. *
  152. * \param arm11 Target state variable.
  153. * \param chain Scan chain that will be selected.
  154. * \param state Pass the final TAP state or ARM11_TAP_DEFAULT for the default
  155. * value (Pause-DR).
  156. *
  157. * Changes the current scan chain if needed, transitions to the specified
  158. * TAP state, and leaves the IR undefined.
  159. *
  160. * The chain takes effect when Update-DR is passed (usually when subsequently
  161. * the INTEXT/EXTEST instructions are written).
  162. *
  163. * \warning (Obsolete) Using this twice in a row will \em fail. The first
  164. * call will end in Pause-DR. The second call, due to the IR
  165. * caching, will not go through Capture-DR when shifting in the
  166. * new scan chain number. As a result the verification in
  167. * arm11_in_handler_SCAN_N() must fail.
  168. *
  169. * \remarks This adds to the JTAG command queue but does \em not execute it.
  170. */
  171. int arm11_add_debug_SCAN_N(struct arm11_common *arm11,
  172. uint8_t chain, tap_state_t state)
  173. {
  174. /* Don't needlessly switch the scan chain.
  175. * NOTE: the ITRSEL instruction fakes SCREG changing;
  176. * but leaves its actual value unchanged.
  177. */
  178. #if 0
  179. /* FIX!!! the optimization below is broken because we do not */
  180. /* invalidate the cur_scan_chain upon a TRST/TMS. See arm_jtag.c */
  181. /* for example on how to invalidate cur_scan_chain. Tested patches gladly */
  182. /* accepted! */
  183. if (arm11->jtag_info.cur_scan_chain == chain) {
  184. JTAG_DEBUG("SCREG <= %d SKIPPED", chain);
  185. return jtag_add_statemove((state == ARM11_TAP_DEFAULT)
  186. ? TAP_DRPAUSE : state);
  187. }
  188. #endif
  189. JTAG_DEBUG("SCREG <= %d", chain);
  190. arm11_add_IR(arm11, ARM11_SCAN_N, ARM11_TAP_DEFAULT);
  191. struct scan_field field;
  192. uint8_t tmp[1];
  193. arm11_setup_field(arm11, 5, &chain, &tmp, &field);
  194. arm11_add_dr_scan_vc(arm11->arm.target->tap,
  195. 1,
  196. &field,
  197. state == ARM11_TAP_DEFAULT ? TAP_DRPAUSE : state);
  198. jtag_execute_queue_noclear();
  199. arm11_in_handler_SCAN_N(tmp);
  200. arm11->jtag_info.cur_scan_chain = chain;
  201. return jtag_execute_queue();
  202. }
  203. /**
  204. * Queue a DR scan of the ITR register. Caller must have selected
  205. * scan chain 4 (ITR), possibly using ITRSEL.
  206. *
  207. * \param arm11 Target state variable.
  208. * \param inst An ARM11 processor instruction/opcode.
  209. * \param flag Optional parameter to retrieve the Ready flag;
  210. * this address will be written when the JTAG chain is scanned.
  211. * \param state The TAP state to enter after the DR scan.
  212. *
  213. * Going through the TAP_DRUPDATE state writes ITR only if Ready was
  214. * previously set. Only the Ready flag is readable by the scan.
  215. *
  216. * An instruction loaded into ITR is executed when going through the
  217. * TAP_IDLE state only if Ready was previously set and the debug state
  218. * is properly set up. Depending on the instruction, you may also need
  219. * to ensure that the rDTR is ready before that Run-Test/Idle state.
  220. */
  221. static void arm11_add_debug_INST(struct arm11_common *arm11,
  222. uint32_t inst, uint8_t *flag, tap_state_t state)
  223. {
  224. JTAG_DEBUG("INST <= 0x%08x", (unsigned) inst);
  225. struct scan_field itr[2];
  226. arm11_setup_field(arm11, 32, &inst, NULL, itr + 0);
  227. arm11_setup_field(arm11, 1, NULL, flag, itr + 1);
  228. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(itr), itr, state);
  229. }
  230. /**
  231. * Read and save the Debug Status and Control Register (DSCR).
  232. *
  233. * \param arm11 Target state variable.
  234. * \return Error status; arm11->dscr is updated on success.
  235. *
  236. * \remarks This is a stand-alone function that executes the JTAG
  237. * command queue. It does not require the ARM11 debug TAP to be
  238. * in any particular state.
  239. */
  240. int arm11_read_DSCR(struct arm11_common *arm11)
  241. {
  242. int retval;
  243. retval = arm11_add_debug_SCAN_N(arm11, 0x01, ARM11_TAP_DEFAULT);
  244. if (retval != ERROR_OK)
  245. return retval;
  246. arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
  247. uint32_t dscr;
  248. struct scan_field chain1_field;
  249. arm11_setup_field(arm11, 32, NULL, &dscr, &chain1_field);
  250. arm11_add_dr_scan_vc(arm11->arm.target->tap, 1, &chain1_field, TAP_DRPAUSE);
  251. CHECK_RETVAL(jtag_execute_queue());
  252. if (arm11->dscr != dscr)
  253. JTAG_DEBUG("DSCR = %08x (OLD %08x)",
  254. (unsigned) dscr,
  255. (unsigned) arm11->dscr);
  256. arm11->dscr = dscr;
  257. return ERROR_OK;
  258. }
  259. /** Write the Debug Status and Control Register (DSCR)
  260. *
  261. * same as CP14 c1
  262. *
  263. * \param arm11 Target state variable.
  264. * \param dscr DSCR content
  265. *
  266. * \remarks This is a stand-alone function that executes the JTAG command queue.
  267. */
  268. int arm11_write_DSCR(struct arm11_common *arm11, uint32_t dscr)
  269. {
  270. int retval;
  271. retval = arm11_add_debug_SCAN_N(arm11, 0x01, ARM11_TAP_DEFAULT);
  272. if (retval != ERROR_OK)
  273. return retval;
  274. arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
  275. struct scan_field chain1_field;
  276. arm11_setup_field(arm11, 32, &dscr, NULL, &chain1_field);
  277. arm11_add_dr_scan_vc(arm11->arm.target->tap, 1, &chain1_field, TAP_DRPAUSE);
  278. CHECK_RETVAL(jtag_execute_queue());
  279. JTAG_DEBUG("DSCR <= %08x (OLD %08x)",
  280. (unsigned) dscr,
  281. (unsigned) arm11->dscr);
  282. arm11->dscr = dscr;
  283. return ERROR_OK;
  284. }
  285. /** Prepare the stage for ITR/DTR operations
  286. * from the arm11_run_instr... group of functions.
  287. *
  288. * Put arm11_run_instr_data_prepare() and arm11_run_instr_data_finish()
  289. * around a block of arm11_run_instr_... calls.
  290. *
  291. * Select scan chain 5 to allow quick access to DTR. When scan
  292. * chain 4 is needed to put in a register the ITRSel instruction
  293. * shortcut is used instead of actually changing the Scan_N
  294. * register.
  295. *
  296. * \param arm11 Target state variable.
  297. *
  298. */
  299. int arm11_run_instr_data_prepare(struct arm11_common *arm11)
  300. {
  301. return arm11_add_debug_SCAN_N(arm11, 0x05, ARM11_TAP_DEFAULT);
  302. }
  303. /** Cleanup after ITR/DTR operations
  304. * from the arm11_run_instr... group of functions
  305. *
  306. * Put arm11_run_instr_data_prepare() and arm11_run_instr_data_finish()
  307. * around a block of arm11_run_instr_... calls.
  308. *
  309. * Any IDLE can lead to an instruction execution when
  310. * scan chains 4 or 5 are selected and the IR holds
  311. * INTEST or EXTEST. So we must disable that before
  312. * any following activities lead to an IDLE.
  313. *
  314. * \param arm11 Target state variable.
  315. *
  316. */
  317. int arm11_run_instr_data_finish(struct arm11_common *arm11)
  318. {
  319. return arm11_add_debug_SCAN_N(arm11, 0x00, ARM11_TAP_DEFAULT);
  320. }
  321. /**
  322. * Execute one or more instructions via ITR.
  323. * Caller guarantees that processor is in debug state, that DSCR_ITR_EN
  324. * is set, the ITR Ready flag is set (as seen on the previous entry to
  325. * TAP_DRCAPTURE), and the DSCR sticky abort flag is clear.
  326. *
  327. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  328. *
  329. * \param arm11 Target state variable.
  330. * \param opcode Pointer to sequence of ARM opcodes
  331. * \param count Number of opcodes to execute
  332. *
  333. */
  334. static
  335. int arm11_run_instr_no_data(struct arm11_common *arm11,
  336. uint32_t *opcode, size_t count)
  337. {
  338. arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
  339. while (count--) {
  340. arm11_add_debug_INST(arm11, *opcode++, NULL, TAP_IDLE);
  341. int i = 0;
  342. while (1) {
  343. uint8_t flag;
  344. arm11_add_debug_INST(arm11, 0, &flag, count ? TAP_IDLE : TAP_DRPAUSE);
  345. CHECK_RETVAL(jtag_execute_queue());
  346. if (flag)
  347. break;
  348. long long then = 0;
  349. if (i == 1000)
  350. then = timeval_ms();
  351. if (i >= 1000) {
  352. if ((timeval_ms()-then) > 1000) {
  353. LOG_WARNING(
  354. "Timeout (1000ms) waiting for instructions to complete");
  355. return ERROR_FAIL;
  356. }
  357. }
  358. i++;
  359. }
  360. }
  361. return ERROR_OK;
  362. }
  363. /** Execute one instruction via ITR
  364. *
  365. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  366. *
  367. * \param arm11 Target state variable.
  368. * \param opcode ARM opcode
  369. *
  370. */
  371. int arm11_run_instr_no_data1(struct arm11_common *arm11, uint32_t opcode)
  372. {
  373. return arm11_run_instr_no_data(arm11, &opcode, 1);
  374. }
  375. /** Execute one instruction via ITR repeatedly while
  376. * passing data to the core via DTR on each execution.
  377. *
  378. * Caller guarantees that processor is in debug state, that DSCR_ITR_EN
  379. * is set, the ITR Ready flag is set (as seen on the previous entry to
  380. * TAP_DRCAPTURE), and the DSCR sticky abort flag is clear.
  381. *
  382. * The executed instruction \em must read data from DTR.
  383. *
  384. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  385. *
  386. * \param arm11 Target state variable.
  387. * \param opcode ARM opcode
  388. * \param data Pointer to the data words to be passed to the core
  389. * \param count Number of data words and instruction repetitions
  390. *
  391. */
  392. int arm11_run_instr_data_to_core(struct arm11_common *arm11,
  393. uint32_t opcode,
  394. uint32_t *data,
  395. size_t count)
  396. {
  397. arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
  398. arm11_add_debug_INST(arm11, opcode, NULL, TAP_DRPAUSE);
  399. arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
  400. struct scan_field chain5_fields[3];
  401. uint32_t Data;
  402. uint8_t Ready;
  403. uint8_t nRetry;
  404. arm11_setup_field(arm11, 32, &Data, NULL, chain5_fields + 0);
  405. arm11_setup_field(arm11, 1, NULL, &Ready, chain5_fields + 1);
  406. arm11_setup_field(arm11, 1, NULL, &nRetry, chain5_fields + 2);
  407. while (count--) {
  408. int i = 0;
  409. do {
  410. Data = *data;
  411. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(
  412. chain5_fields), chain5_fields, TAP_IDLE);
  413. CHECK_RETVAL(jtag_execute_queue());
  414. JTAG_DEBUG("DTR Ready %d nRetry %d", Ready, nRetry);
  415. long long then = 0;
  416. if (i == 1000)
  417. then = timeval_ms();
  418. if (i >= 1000) {
  419. if ((timeval_ms()-then) > 1000) {
  420. LOG_WARNING(
  421. "Timeout (1000ms) waiting for instructions to complete");
  422. return ERROR_FAIL;
  423. }
  424. }
  425. i++;
  426. } while (!Ready);
  427. data++;
  428. }
  429. arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
  430. int i = 0;
  431. do {
  432. Data = 0;
  433. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(
  434. chain5_fields), chain5_fields, TAP_DRPAUSE);
  435. CHECK_RETVAL(jtag_execute_queue());
  436. JTAG_DEBUG("DTR Data %08x Ready %d nRetry %d",
  437. (unsigned) Data, Ready, nRetry);
  438. long long then = 0;
  439. if (i == 1000)
  440. then = timeval_ms();
  441. if (i >= 1000) {
  442. if ((timeval_ms()-then) > 1000) {
  443. LOG_WARNING("Timeout (1000ms) waiting for instructions to complete");
  444. return ERROR_FAIL;
  445. }
  446. }
  447. i++;
  448. } while (!Ready);
  449. return ERROR_OK;
  450. }
  451. /** JTAG path for arm11_run_instr_data_to_core_noack
  452. *
  453. * The repeated TAP_IDLE's do not cause a repeated execution
  454. * if passed without leaving the state.
  455. *
  456. * Since this is more than 7 bits (adjustable via adding more
  457. * TAP_IDLE's) it produces an artificial delay in the lower
  458. * layer (FT2232) that is long enough to finish execution on
  459. * the core but still shorter than any manually inducible delays.
  460. *
  461. * To disable this code, try "memwrite burst false"
  462. *
  463. * FIX!!! should we use multiple TAP_IDLE here or not???
  464. *
  465. * https://lists.berlios.de/pipermail/openocd-development/2009-July/009698.html
  466. * https://lists.berlios.de/pipermail/openocd-development/2009-August/009865.html
  467. */
  468. static const tap_state_t arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay[] = {
  469. TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE, TAP_IDLE, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE,
  470. TAP_DRSHIFT
  471. };
  472. /* This inner loop can be implemented by the minidriver, oftentimes in hardware... The
  473. * minidriver can call the default implementation as a fallback or implement it
  474. * from scratch.
  475. */
  476. int arm11_run_instr_data_to_core_noack_inner_default(struct jtag_tap *tap,
  477. uint32_t opcode,
  478. uint32_t *data,
  479. size_t count)
  480. {
  481. struct scan_field chain5_fields[3];
  482. chain5_fields[0].num_bits = 32;
  483. chain5_fields[0].out_value = NULL; /*&Data*/
  484. chain5_fields[0].in_value = NULL;
  485. chain5_fields[1].num_bits = 1;
  486. chain5_fields[1].out_value = NULL;
  487. chain5_fields[1].in_value = NULL; /*&Ready*/
  488. chain5_fields[2].num_bits = 1;
  489. chain5_fields[2].out_value = NULL;
  490. chain5_fields[2].in_value = NULL;
  491. uint8_t *Readies;
  492. unsigned readiesNum = count;
  493. unsigned bytes = sizeof(*Readies)*readiesNum;
  494. Readies = (uint8_t *) malloc(bytes);
  495. if (Readies == NULL) {
  496. LOG_ERROR("Out of memory allocating %u bytes", bytes);
  497. return ERROR_FAIL;
  498. }
  499. uint8_t *ReadyPos = Readies;
  500. while (count--) {
  501. chain5_fields[0].out_value = (void *)(data++);
  502. chain5_fields[1].in_value = ReadyPos++;
  503. if (count > 0) {
  504. jtag_add_dr_scan(tap, ARRAY_SIZE(chain5_fields), chain5_fields,
  505. TAP_DRPAUSE);
  506. jtag_add_pathmove(ARRAY_SIZE(arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay),
  507. arm11_MOVE_DRPAUSE_IDLE_DRPAUSE_with_delay);
  508. } else
  509. jtag_add_dr_scan(tap, ARRAY_SIZE(chain5_fields), chain5_fields, TAP_IDLE);
  510. }
  511. int retval = jtag_execute_queue();
  512. if (retval == ERROR_OK) {
  513. unsigned error_count = 0;
  514. for (size_t i = 0; i < readiesNum; i++) {
  515. if (Readies[i] != 1)
  516. error_count++;
  517. }
  518. if (error_count > 0) {
  519. LOG_ERROR("%u words out of %u not transferred",
  520. error_count, readiesNum);
  521. retval = ERROR_FAIL;
  522. }
  523. }
  524. free(Readies);
  525. return retval;
  526. }
  527. int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap *tap,
  528. uint32_t opcode,
  529. uint32_t *data,
  530. size_t count);
  531. #ifndef HAVE_JTAG_MINIDRIVER_H
  532. int arm11_run_instr_data_to_core_noack_inner(struct jtag_tap *tap,
  533. uint32_t opcode,
  534. uint32_t *data,
  535. size_t count)
  536. {
  537. return arm11_run_instr_data_to_core_noack_inner_default(tap, opcode, data, count);
  538. }
  539. #endif
  540. /** Execute one instruction via ITR repeatedly while
  541. * passing data to the core via DTR on each execution.
  542. *
  543. * Caller guarantees that processor is in debug state, that DSCR_ITR_EN
  544. * is set, the ITR Ready flag is set (as seen on the previous entry to
  545. * TAP_DRCAPTURE), and the DSCR sticky abort flag is clear.
  546. *
  547. * No Ready check during transmission.
  548. *
  549. * The executed instruction \em must read data from DTR.
  550. *
  551. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  552. *
  553. * \param arm11 Target state variable.
  554. * \param opcode ARM opcode
  555. * \param data Pointer to the data words to be passed to the core
  556. * \param count Number of data words and instruction repetitions
  557. *
  558. */
  559. int arm11_run_instr_data_to_core_noack(struct arm11_common *arm11,
  560. uint32_t opcode,
  561. uint32_t *data,
  562. size_t count)
  563. {
  564. arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
  565. arm11_add_debug_INST(arm11, opcode, NULL, TAP_DRPAUSE);
  566. arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
  567. int retval = arm11_run_instr_data_to_core_noack_inner(arm11->arm.target->tap,
  568. opcode,
  569. data,
  570. count);
  571. if (retval != ERROR_OK)
  572. return retval;
  573. arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
  574. struct scan_field chain5_fields[3];
  575. arm11_setup_field(arm11,
  576. 32,
  577. NULL /*&Data*/,
  578. NULL,
  579. chain5_fields + 0);
  580. arm11_setup_field(arm11,
  581. 1,
  582. NULL,
  583. NULL /*&Ready*/,
  584. chain5_fields + 1);
  585. arm11_setup_field(arm11,
  586. 1,
  587. NULL,
  588. NULL,
  589. chain5_fields + 2);
  590. uint8_t ready_flag;
  591. chain5_fields[1].in_value = &ready_flag;
  592. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(
  593. chain5_fields), chain5_fields, TAP_DRPAUSE);
  594. retval = jtag_execute_queue();
  595. if (retval == ERROR_OK) {
  596. if (ready_flag != 1) {
  597. LOG_ERROR("last word not transferred");
  598. retval = ERROR_FAIL;
  599. }
  600. }
  601. return retval;
  602. }
  603. /** Execute an instruction via ITR while handing data into the core via DTR.
  604. *
  605. * The executed instruction \em must read data from DTR.
  606. *
  607. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  608. *
  609. * \param arm11 Target state variable.
  610. * \param opcode ARM opcode
  611. * \param data Data word to be passed to the core via DTR
  612. *
  613. */
  614. int arm11_run_instr_data_to_core1(struct arm11_common *arm11, uint32_t opcode, uint32_t data)
  615. {
  616. return arm11_run_instr_data_to_core(arm11, opcode, &data, 1);
  617. }
  618. /** Execute one instruction via ITR repeatedly while
  619. * reading data from the core via DTR on each execution.
  620. *
  621. * Caller guarantees that processor is in debug state, that DSCR_ITR_EN
  622. * is set, the ITR Ready flag is set (as seen on the previous entry to
  623. * TAP_DRCAPTURE), and the DSCR sticky abort flag is clear.
  624. *
  625. * The executed instruction \em must write data to DTR.
  626. *
  627. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  628. *
  629. * \param arm11 Target state variable.
  630. * \param opcode ARM opcode
  631. * \param data Pointer to an array that receives the data words from the core
  632. * \param count Number of data words and instruction repetitions
  633. *
  634. */
  635. int arm11_run_instr_data_from_core(struct arm11_common *arm11,
  636. uint32_t opcode,
  637. uint32_t *data,
  638. size_t count)
  639. {
  640. arm11_add_IR(arm11, ARM11_ITRSEL, ARM11_TAP_DEFAULT);
  641. arm11_add_debug_INST(arm11, opcode, NULL, TAP_IDLE);
  642. arm11_add_IR(arm11, ARM11_INTEST, ARM11_TAP_DEFAULT);
  643. struct scan_field chain5_fields[3];
  644. uint32_t Data;
  645. uint8_t Ready;
  646. uint8_t nRetry;
  647. arm11_setup_field(arm11, 32, NULL, &Data, chain5_fields + 0);
  648. arm11_setup_field(arm11, 1, NULL, &Ready, chain5_fields + 1);
  649. arm11_setup_field(arm11, 1, NULL, &nRetry, chain5_fields + 2);
  650. while (count--) {
  651. int i = 0;
  652. do {
  653. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(
  654. chain5_fields), chain5_fields,
  655. count ? TAP_IDLE : TAP_DRPAUSE);
  656. CHECK_RETVAL(jtag_execute_queue());
  657. JTAG_DEBUG("DTR Data %08x Ready %d nRetry %d",
  658. (unsigned) Data, Ready, nRetry);
  659. long long then = 0;
  660. if (i == 1000)
  661. then = timeval_ms();
  662. if (i >= 1000) {
  663. if ((timeval_ms()-then) > 1000) {
  664. LOG_WARNING(
  665. "Timeout (1000ms) waiting for instructions to complete");
  666. return ERROR_FAIL;
  667. }
  668. }
  669. i++;
  670. } while (!Ready);
  671. *data++ = Data;
  672. }
  673. return ERROR_OK;
  674. }
  675. /** Execute one instruction via ITR
  676. * then load r0 into DTR and read DTR from core.
  677. *
  678. * The first executed instruction (\p opcode) should write data to r0.
  679. *
  680. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  681. *
  682. * \param arm11 Target state variable.
  683. * \param opcode ARM opcode to write r0 with the value of interest
  684. * \param data Pointer to a data word that receives the value from r0 after \p opcode was executed.
  685. *
  686. */
  687. int arm11_run_instr_data_from_core_via_r0(struct arm11_common *arm11,
  688. uint32_t opcode,
  689. uint32_t *data)
  690. {
  691. int retval;
  692. retval = arm11_run_instr_no_data1(arm11, opcode);
  693. if (retval != ERROR_OK)
  694. return retval;
  695. /* MCR p14,0,R0,c0,c5,0 (move r0 -> wDTR -> local var) */
  696. arm11_run_instr_data_from_core(arm11, 0xEE000E15, data, 1);
  697. return ERROR_OK;
  698. }
  699. /** Load data into core via DTR then move it to r0 then
  700. * execute one instruction via ITR
  701. *
  702. * The final executed instruction (\p opcode) should read data from r0.
  703. *
  704. * \pre arm11_run_instr_data_prepare() / arm11_run_instr_data_finish() block
  705. *
  706. * \param arm11 Target state variable.
  707. * \param opcode ARM opcode to read r0 act upon it
  708. * \param data Data word that will be written to r0 before \p opcode is executed
  709. *
  710. */
  711. int arm11_run_instr_data_to_core_via_r0(struct arm11_common *arm11, uint32_t opcode, uint32_t data)
  712. {
  713. int retval;
  714. /* MRC p14,0,r0,c0,c5,0 */
  715. retval = arm11_run_instr_data_to_core1(arm11, 0xEE100E15, data);
  716. if (retval != ERROR_OK)
  717. return retval;
  718. retval = arm11_run_instr_no_data1(arm11, opcode);
  719. if (retval != ERROR_OK)
  720. return retval;
  721. return ERROR_OK;
  722. }
  723. /** Apply reads and writes to scan chain 7
  724. *
  725. * \see struct arm11_sc7_action
  726. *
  727. * \param arm11 Target state variable.
  728. * \param actions A list of read and/or write instructions
  729. * \param count Number of instructions in the list.
  730. *
  731. */
  732. int arm11_sc7_run(struct arm11_common *arm11, struct arm11_sc7_action *actions, size_t count)
  733. {
  734. int retval;
  735. retval = arm11_add_debug_SCAN_N(arm11, 0x07, ARM11_TAP_DEFAULT);
  736. if (retval != ERROR_OK)
  737. return retval;
  738. arm11_add_IR(arm11, ARM11_EXTEST, ARM11_TAP_DEFAULT);
  739. struct scan_field chain7_fields[3];
  740. uint8_t nRW;
  741. uint32_t DataOut;
  742. uint8_t AddressOut;
  743. uint8_t Ready;
  744. uint32_t DataIn;
  745. uint8_t AddressIn;
  746. arm11_setup_field(arm11, 1, &nRW, &Ready, chain7_fields + 0);
  747. arm11_setup_field(arm11, 32, &DataOut, &DataIn, chain7_fields + 1);
  748. arm11_setup_field(arm11, 7, &AddressOut, &AddressIn, chain7_fields + 2);
  749. for (size_t i = 0; i < count + 1; i++) {
  750. if (i < count) {
  751. nRW = actions[i].write ? 1 : 0;
  752. DataOut = actions[i].value;
  753. AddressOut = actions[i].address;
  754. } else {
  755. nRW = 1;
  756. DataOut = 0;
  757. AddressOut = 0;
  758. }
  759. /* Timeout here so we don't get stuck. */
  760. int i_n = 0;
  761. while (1) {
  762. JTAG_DEBUG("SC7 <= c%-3d Data %08x %s",
  763. (unsigned) AddressOut,
  764. (unsigned) DataOut,
  765. nRW ? "write" : "read");
  766. arm11_add_dr_scan_vc(arm11->arm.target->tap, ARRAY_SIZE(chain7_fields),
  767. chain7_fields, TAP_DRPAUSE);
  768. CHECK_RETVAL(jtag_execute_queue());
  769. /* 'nRW' is 'Ready' on read out */
  770. if (Ready)
  771. break;
  772. long long then = 0;
  773. if (i_n == 1000)
  774. then = timeval_ms();
  775. if (i_n >= 1000) {
  776. if ((timeval_ms()-then) > 1000) {
  777. LOG_WARNING(
  778. "Timeout (1000ms) waiting for instructions to complete");
  779. return ERROR_FAIL;
  780. }
  781. }
  782. i_n++;
  783. }
  784. if (!nRW)
  785. JTAG_DEBUG("SC7 => Data %08x", (unsigned) DataIn);
  786. if (i > 0) {
  787. if (actions[i - 1].address != AddressIn)
  788. LOG_WARNING("Scan chain 7 shifted out unexpected address");
  789. if (!actions[i - 1].write)
  790. actions[i - 1].value = DataIn;
  791. else {
  792. if (actions[i - 1].value != DataIn)
  793. LOG_WARNING("Scan chain 7 shifted out unexpected data");
  794. }
  795. }
  796. }
  797. return ERROR_OK;
  798. }
  799. /** Clear VCR and all breakpoints and watchpoints via scan chain 7
  800. *
  801. * \param arm11 Target state variable.
  802. *
  803. */
  804. int arm11_sc7_clear_vbw(struct arm11_common *arm11)
  805. {
  806. size_t clear_bw_size = arm11->brp + 1;
  807. struct arm11_sc7_action *clear_bw = malloc(sizeof(struct arm11_sc7_action) * clear_bw_size);
  808. struct arm11_sc7_action *pos = clear_bw;
  809. for (size_t i = 0; i < clear_bw_size; i++) {
  810. clear_bw[i].write = true;
  811. clear_bw[i].value = 0;
  812. }
  813. for (size_t i = 0; i < arm11->brp; i++)
  814. (pos++)->address = ARM11_SC7_BCR0 + i;
  815. (pos++)->address = ARM11_SC7_VCR;
  816. int retval;
  817. retval = arm11_sc7_run(arm11, clear_bw, clear_bw_size);
  818. free(clear_bw);
  819. return retval;
  820. }
  821. /** Write VCR register
  822. *
  823. * \param arm11 Target state variable.
  824. * \param value Value to be written
  825. */
  826. int arm11_sc7_set_vcr(struct arm11_common *arm11, uint32_t value)
  827. {
  828. struct arm11_sc7_action set_vcr;
  829. set_vcr.write = true;
  830. set_vcr.address = ARM11_SC7_VCR;
  831. set_vcr.value = value;
  832. return arm11_sc7_run(arm11, &set_vcr, 1);
  833. }
  834. /** Read word from address
  835. *
  836. * \param arm11 Target state variable.
  837. * \param address Memory address to be read
  838. * \param result Pointer where to store result
  839. *
  840. */
  841. int arm11_read_memory_word(struct arm11_common *arm11, uint32_t address, uint32_t *result)
  842. {
  843. int retval;
  844. retval = arm11_run_instr_data_prepare(arm11);
  845. if (retval != ERROR_OK)
  846. return retval;
  847. /* MRC p14,0,r0,c0,c5,0 (r0 = address) */
  848. CHECK_RETVAL(arm11_run_instr_data_to_core1(arm11, 0xee100e15, address));
  849. /* LDC p14,c5,[R0],#4 (DTR = [r0]) */
  850. CHECK_RETVAL(arm11_run_instr_data_from_core(arm11, 0xecb05e01, result, 1));
  851. return arm11_run_instr_data_finish(arm11);
  852. }
  853. /************************************************************************/
  854. /*
  855. * ARM11 provider for the OpenOCD implementation of the standard
  856. * architectural ARM v6/v7 "Debug Programmer's Model" (DPM).
  857. */
  858. static inline struct arm11_common *dpm_to_arm11(struct arm_dpm *dpm)
  859. {
  860. return container_of(dpm, struct arm11_common, dpm);
  861. }
  862. static int arm11_dpm_prepare(struct arm_dpm *dpm)
  863. {
  864. return arm11_run_instr_data_prepare(dpm_to_arm11(dpm));
  865. }
  866. static int arm11_dpm_finish(struct arm_dpm *dpm)
  867. {
  868. return arm11_run_instr_data_finish(dpm_to_arm11(dpm));
  869. }
  870. static int arm11_dpm_instr_write_data_dcc(struct arm_dpm *dpm,
  871. uint32_t opcode, uint32_t data)
  872. {
  873. return arm11_run_instr_data_to_core(dpm_to_arm11(dpm),
  874. opcode, &data, 1);
  875. }
  876. static int arm11_dpm_instr_write_data_r0(struct arm_dpm *dpm,
  877. uint32_t opcode, uint32_t data)
  878. {
  879. return arm11_run_instr_data_to_core_via_r0(dpm_to_arm11(dpm),
  880. opcode, data);
  881. }
  882. static int arm11_dpm_instr_read_data_dcc(struct arm_dpm *dpm,
  883. uint32_t opcode, uint32_t *data)
  884. {
  885. return arm11_run_instr_data_from_core(dpm_to_arm11(dpm),
  886. opcode, data, 1);
  887. }
  888. static int arm11_dpm_instr_read_data_r0(struct arm_dpm *dpm,
  889. uint32_t opcode, uint32_t *data)
  890. {
  891. return arm11_run_instr_data_from_core_via_r0(dpm_to_arm11(dpm),
  892. opcode, data);
  893. }
  894. /* Because arm11_sc7_run() takes a vector of actions, we batch breakpoint
  895. * and watchpoint operations instead of running them right away. Since we
  896. * pre-allocated our vector, we don't need to worry about space.
  897. */
  898. static int arm11_bpwp_enable(struct arm_dpm *dpm, unsigned index_t,
  899. uint32_t addr, uint32_t control)
  900. {
  901. struct arm11_common *arm11 = dpm_to_arm11(dpm);
  902. struct arm11_sc7_action *action;
  903. action = arm11->bpwp_actions + arm11->bpwp_n;
  904. /* Invariant: this bp/wp is disabled.
  905. * It also happens that the core is halted here, but for
  906. * DPM-based cores we don't actually care about that.
  907. */
  908. action[0].write = action[1].write = true;
  909. action[0].value = addr;
  910. action[1].value = control;
  911. switch (index_t) {
  912. case 0 ... 15:
  913. action[0].address = ARM11_SC7_BVR0 + index_t;
  914. action[1].address = ARM11_SC7_BCR0 + index_t;
  915. break;
  916. case 16 ... 32:
  917. index_t -= 16;
  918. action[0].address = ARM11_SC7_WVR0 + index_t;
  919. action[1].address = ARM11_SC7_WCR0 + index_t;
  920. break;
  921. default:
  922. return ERROR_FAIL;
  923. }
  924. arm11->bpwp_n += 2;
  925. return ERROR_OK;
  926. }
  927. static int arm11_bpwp_disable(struct arm_dpm *dpm, unsigned index_t)
  928. {
  929. struct arm11_common *arm11 = dpm_to_arm11(dpm);
  930. struct arm11_sc7_action *action;
  931. action = arm11->bpwp_actions + arm11->bpwp_n;
  932. action[0].write = true;
  933. action[0].value = 0;
  934. switch (index_t) {
  935. case 0 ... 15:
  936. action[0].address = ARM11_SC7_BCR0 + index_t;
  937. break;
  938. case 16 ... 32:
  939. index_t -= 16;
  940. action[0].address = ARM11_SC7_WCR0 + index_t;
  941. break;
  942. default:
  943. return ERROR_FAIL;
  944. }
  945. arm11->bpwp_n += 1;
  946. return ERROR_OK;
  947. }
  948. /** Flush any pending breakpoint and watchpoint updates. */
  949. int arm11_bpwp_flush(struct arm11_common *arm11)
  950. {
  951. int retval;
  952. if (!arm11->bpwp_n)
  953. return ERROR_OK;
  954. retval = arm11_sc7_run(arm11, arm11->bpwp_actions, arm11->bpwp_n);
  955. arm11->bpwp_n = 0;
  956. return retval;
  957. }
  958. /** Set up high-level debug module utilities */
  959. int arm11_dpm_init(struct arm11_common *arm11, uint32_t didr)
  960. {
  961. struct arm_dpm *dpm = &arm11->dpm;
  962. int retval;
  963. dpm->arm = &arm11->arm;
  964. dpm->didr = didr;
  965. dpm->prepare = arm11_dpm_prepare;
  966. dpm->finish = arm11_dpm_finish;
  967. dpm->instr_write_data_dcc = arm11_dpm_instr_write_data_dcc;
  968. dpm->instr_write_data_r0 = arm11_dpm_instr_write_data_r0;
  969. dpm->instr_read_data_dcc = arm11_dpm_instr_read_data_dcc;
  970. dpm->instr_read_data_r0 = arm11_dpm_instr_read_data_r0;
  971. dpm->bpwp_enable = arm11_bpwp_enable;
  972. dpm->bpwp_disable = arm11_bpwp_disable;
  973. retval = arm_dpm_setup(dpm);
  974. if (retval != ERROR_OK)
  975. return retval;
  976. /* alloc enough to enable all breakpoints and watchpoints at once */
  977. arm11->bpwp_actions = calloc(2 * (dpm->nbp + dpm->nwp),
  978. sizeof *arm11->bpwp_actions);
  979. if (!arm11->bpwp_actions)
  980. return ERROR_FAIL;
  981. retval = arm_dpm_initialize(dpm);
  982. if (retval != ERROR_OK)
  983. return retval;
  984. return arm11_bpwp_flush(arm11);
  985. }