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.
 
 
 
 
 
 

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