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.
 
 
 
 
 
 

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