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.
 
 
 
 
 
 

542 lines
17 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2013-2014,2019-2020 Synopsys, Inc. *
  3. * Frank Dols <frank.dols@synopsys.com> *
  4. * Mischa Jonker <mischa.jonker@synopsys.com> *
  5. * Anton Kolesov <anton.kolesov@synopsys.com> *
  6. * Evgeniy Didin <didin@synopsys.com> *
  7. * *
  8. * SPDX-License-Identifier: GPL-2.0-or-later *
  9. ***************************************************************************/
  10. #ifdef HAVE_CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include "arc.h"
  14. /*
  15. * This functions sets instruction register in TAP. TAP end state is always
  16. * IRPAUSE.
  17. *
  18. * @param jtag_info
  19. * @param new_instr Instruction to write to instruction register.
  20. */
  21. static void arc_jtag_enque_write_ir(struct arc_jtag *jtag_info, uint32_t
  22. new_instr)
  23. {
  24. uint32_t current_instr;
  25. struct jtag_tap *tap;
  26. uint8_t instr_buffer[sizeof(uint32_t)] = {0};
  27. assert(jtag_info);
  28. assert(jtag_info->tap);
  29. tap = jtag_info->tap;
  30. /* Do not set instruction if it is the same as current. */
  31. current_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
  32. if (current_instr == new_instr)
  33. return;
  34. struct scan_field field = {
  35. .num_bits = tap->ir_length,
  36. .out_value = instr_buffer
  37. };
  38. buf_set_u32(instr_buffer, 0, field.num_bits, new_instr);
  39. /* From code in src/jtag/drivers/driver.c it look like that fields are
  40. * copied so it is OK that field in this function is allocated in stack and
  41. * thus this memory will be repurposed before jtag_execute_queue() will be
  42. * invoked. */
  43. jtag_add_ir_scan(tap, &field, TAP_IRPAUSE);
  44. }
  45. /**
  46. * Read 4-byte word from data register.
  47. *
  48. * Unlike arc_jtag_write_data, this function returns byte-buffer, caller must
  49. * convert this data to required format himself. This is done, because it is
  50. * impossible to convert data before jtag_execute_queue() is invoked, so it
  51. * cannot be done inside this function, so it has to operate with
  52. * byte-buffers. Write function on the other hand can "write-and-forget", data
  53. * is converted to byte-buffer before jtag_execute_queue().
  54. *
  55. * @param jtag_info
  56. * @param data Array of bytes to read into.
  57. * @param end_state End state after reading.
  58. */
  59. static void arc_jtag_enque_read_dr(struct arc_jtag *jtag_info, uint8_t *data,
  60. tap_state_t end_state)
  61. {
  62. assert(jtag_info);
  63. assert(jtag_info->tap);
  64. struct scan_field field = {
  65. .num_bits = 32,
  66. .in_value = data
  67. };
  68. jtag_add_dr_scan(jtag_info->tap, 1, &field, end_state);
  69. }
  70. /**
  71. * Write 4-byte word to data register.
  72. *
  73. * @param jtag_info
  74. * @param data 4-byte word to write into data register.
  75. * @param end_state End state after writing.
  76. */
  77. static void arc_jtag_enque_write_dr(struct arc_jtag *jtag_info, uint32_t data,
  78. tap_state_t end_state)
  79. {
  80. uint8_t out_value[sizeof(uint32_t)] = {0};
  81. assert(jtag_info);
  82. assert(jtag_info->tap);
  83. buf_set_u32(out_value, 0, 32, data);
  84. struct scan_field field = {
  85. .num_bits = 32,
  86. .out_value = out_value
  87. };
  88. jtag_add_dr_scan(jtag_info->tap, 1, &field, end_state);
  89. }
  90. /**
  91. * Set transaction in command register. This function sets instruction register
  92. * and then transaction register, there is no need to invoke write_ir before
  93. * invoking this function.
  94. *
  95. * @param jtag_info
  96. * @param new_trans Transaction to write to transaction command register.
  97. * @param end_state End state after writing.
  98. */
  99. static void arc_jtag_enque_set_transaction(struct arc_jtag *jtag_info,
  100. uint32_t new_trans, tap_state_t end_state)
  101. {
  102. uint8_t out_value[sizeof(uint32_t)] = {0};
  103. assert(jtag_info);
  104. assert(jtag_info->tap);
  105. /* No need to do anything. */
  106. if (jtag_info->cur_trans == new_trans)
  107. return;
  108. /* Set instruction. We used to call write_ir at upper levels, however
  109. * write_ir-write_transaction were constantly in pair, so to avoid code
  110. * duplication this function does it self. For this reasons it is "set"
  111. * instead of "write". */
  112. arc_jtag_enque_write_ir(jtag_info, ARC_TRANSACTION_CMD_REG);
  113. buf_set_u32(out_value, 0, ARC_TRANSACTION_CMD_REG_LENGTH, new_trans);
  114. struct scan_field field = {
  115. .num_bits = ARC_TRANSACTION_CMD_REG_LENGTH,
  116. .out_value = out_value
  117. };
  118. jtag_add_dr_scan(jtag_info->tap, 1, &field, end_state);
  119. jtag_info->cur_trans = new_trans;
  120. }
  121. /**
  122. * Run reset through transaction set. None of the previous
  123. * settings/commands/etc. are used anymore (or no influence).
  124. */
  125. static void arc_jtag_enque_reset_transaction(struct arc_jtag *jtag_info)
  126. {
  127. arc_jtag_enque_set_transaction(jtag_info, ARC_JTAG_CMD_NOP, TAP_IDLE);
  128. }
  129. static void arc_jtag_enque_status_read(struct arc_jtag * const jtag_info,
  130. uint8_t * const buffer)
  131. {
  132. assert(jtag_info);
  133. assert(jtag_info->tap);
  134. assert(buffer);
  135. /* first writing code(0x8) of jtag status register in IR */
  136. arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_STATUS_REG);
  137. /* Now reading dr performs jtag status register read */
  138. arc_jtag_enque_read_dr(jtag_info, buffer, TAP_IDLE);
  139. }
  140. /* ----- Exported JTAG functions ------------------------------------------- */
  141. int arc_jtag_startup(struct arc_jtag *jtag_info)
  142. {
  143. assert(jtag_info);
  144. arc_jtag_enque_reset_transaction(jtag_info);
  145. return jtag_execute_queue();
  146. }
  147. /** Read STATUS register. */
  148. int arc_jtag_status(struct arc_jtag * const jtag_info, uint32_t * const value)
  149. {
  150. uint8_t buffer[sizeof(uint32_t)];
  151. assert(jtag_info);
  152. assert(jtag_info->tap);
  153. /* Fill command queue. */
  154. arc_jtag_enque_reset_transaction(jtag_info);
  155. arc_jtag_enque_status_read(jtag_info, buffer);
  156. arc_jtag_enque_reset_transaction(jtag_info);
  157. /* Execute queue. */
  158. CHECK_RETVAL(jtag_execute_queue());
  159. /* Parse output. */
  160. *value = buf_get_u32(buffer, 0, 32);
  161. return ERROR_OK;
  162. }
  163. /* Helper function: Adding read/write register operation to queue */
  164. static void arc_jtag_enque_register_rw(struct arc_jtag *jtag_info, uint32_t *addr,
  165. uint8_t *read_buffer, const uint32_t *write_buffer, uint32_t count)
  166. {
  167. uint32_t i;
  168. for (i = 0; i < count; i++) {
  169. /* ARC jtag has optimization which is to increment ADDRESS_REG performing
  170. * each transaction. Making sequential reads/writes we can set address for
  171. * only first register in sequence, and than do read/write in cycle. */
  172. if (i == 0 || (addr[i] != addr[i-1] + 1)) {
  173. arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_ADDRESS_REG);
  174. /* Going to TAP_IDLE state we initiate jtag transaction.
  175. * Reading data we must go to TAP_IDLE, because further
  176. * the data would be read. In case of write we go to TAP_DRPAUSE,
  177. * because we need to write data to Data register first. */
  178. if (write_buffer)
  179. arc_jtag_enque_write_dr(jtag_info, addr[i], TAP_DRPAUSE);
  180. else
  181. arc_jtag_enque_write_dr(jtag_info, addr[i], TAP_IDLE);
  182. arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_DATA_REG);
  183. }
  184. if (write_buffer)
  185. arc_jtag_enque_write_dr(jtag_info, *(write_buffer + i), TAP_IDLE);
  186. else
  187. arc_jtag_enque_read_dr(jtag_info, read_buffer + i * 4, TAP_IDLE);
  188. }
  189. /* To prevent pollution of next register due to optimization it is necessary *
  190. * to reset transaction */
  191. arc_jtag_enque_reset_transaction(jtag_info);
  192. }
  193. /**
  194. * Write registers. addr is an array of addresses, and those addresses can be
  195. * in any order, though it is recommended that they are in sequential order
  196. * where possible, as this reduces number of JTAG commands to transfer.
  197. *
  198. * @param jtag_info
  199. * @param type Type of registers to write: core or aux.
  200. * @param addr Array of registers numbers.
  201. * @param count Amount of registers in arrays.
  202. * @param buffer Array of register values.
  203. */
  204. static int arc_jtag_write_registers(struct arc_jtag *jtag_info, uint32_t type,
  205. uint32_t *addr, uint32_t count, const uint32_t *buffer)
  206. {
  207. LOG_DEBUG("Writing to %s registers: addr[0]=0x%" PRIx32 ";count=%" PRIu32
  208. ";buffer[0]=0x%08" PRIx32,
  209. (type == ARC_JTAG_CORE_REG ? "core" : "aux"), *addr, count, *buffer);
  210. if (!count) {
  211. LOG_ERROR("Trying to write 0 registers");
  212. return ERROR_FAIL;
  213. }
  214. arc_jtag_enque_reset_transaction(jtag_info);
  215. /* What registers are we writing to? */
  216. const uint32_t transaction = (type == ARC_JTAG_CORE_REG ?
  217. ARC_JTAG_WRITE_TO_CORE_REG : ARC_JTAG_WRITE_TO_AUX_REG);
  218. arc_jtag_enque_set_transaction(jtag_info, transaction, TAP_DRPAUSE);
  219. arc_jtag_enque_register_rw(jtag_info, addr, NULL, buffer, count);
  220. return jtag_execute_queue();
  221. }
  222. /**
  223. * Read registers. addr is an array of addresses, and those addresses can be in
  224. * any order, though it is recommended that they are in sequential order where
  225. * possible, as this reduces number of JTAG commands to transfer.
  226. *
  227. * @param jtag_info
  228. * @param type Type of registers to read: core or aux.
  229. * @param addr Array of registers numbers.
  230. * @param count Amount of registers in arrays.
  231. * @param buffer Array of register values.
  232. */
  233. static int arc_jtag_read_registers(struct arc_jtag *jtag_info, uint32_t type,
  234. uint32_t *addr, uint32_t count, uint32_t *buffer)
  235. {
  236. int retval;
  237. uint32_t i;
  238. assert(jtag_info);
  239. assert(jtag_info->tap);
  240. LOG_DEBUG("Reading %s registers: addr[0]=0x%" PRIx32 ";count=%" PRIu32,
  241. (type == ARC_JTAG_CORE_REG ? "core" : "aux"), *addr, count);
  242. if (!count) {
  243. LOG_ERROR("Trying to read 0 registers");
  244. return ERROR_FAIL;
  245. }
  246. arc_jtag_enque_reset_transaction(jtag_info);
  247. /* What type of registers we are reading? */
  248. const uint32_t transaction = (type == ARC_JTAG_CORE_REG ?
  249. ARC_JTAG_READ_FROM_CORE_REG : ARC_JTAG_READ_FROM_AUX_REG);
  250. arc_jtag_enque_set_transaction(jtag_info, transaction, TAP_DRPAUSE);
  251. uint8_t *data_buf = calloc(sizeof(uint8_t), count * 4);
  252. arc_jtag_enque_register_rw(jtag_info, addr, data_buf, NULL, count);
  253. retval = jtag_execute_queue();
  254. if (retval != ERROR_OK) {
  255. LOG_ERROR("Failed to execute jtag queue: %d", retval);
  256. retval = ERROR_FAIL;
  257. goto exit;
  258. }
  259. /* Convert byte-buffers to host /presentation. */
  260. for (i = 0; i < count; i++)
  261. buffer[i] = buf_get_u32(data_buf + 4 * i, 0, 32);
  262. LOG_DEBUG("Read from register: buf[0]=0x%" PRIx32, buffer[0]);
  263. exit:
  264. free(data_buf);
  265. return retval;
  266. }
  267. /** Wrapper function to ease writing of one core register. */
  268. int arc_jtag_write_core_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
  269. uint32_t value)
  270. {
  271. return arc_jtag_write_core_reg(jtag_info, &addr, 1, &value);
  272. }
  273. /**
  274. * Write core registers. addr is an array of addresses, and those addresses can
  275. * be in any order, though it is recommended that they are in sequential order
  276. * where possible, as this reduces number of JTAG commands to transfer.
  277. *
  278. * @param jtag_info
  279. * @param addr Array of registers numbers.
  280. * @param count Amount of registers in arrays.
  281. * @param buffer Array of register values.
  282. */
  283. int arc_jtag_write_core_reg(struct arc_jtag *jtag_info, uint32_t *addr,
  284. uint32_t count, const uint32_t *buffer)
  285. {
  286. return arc_jtag_write_registers(jtag_info, ARC_JTAG_CORE_REG, addr, count,
  287. buffer);
  288. }
  289. /** Wrapper function to ease reading of one core register. */
  290. int arc_jtag_read_core_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
  291. uint32_t *value)
  292. {
  293. return arc_jtag_read_core_reg(jtag_info, &addr, 1, value);
  294. }
  295. /**
  296. * Read core registers. addr is an array of addresses, and those addresses can
  297. * be in any order, though it is recommended that they are in sequential order
  298. * where possible, as this reduces number of JTAG commands to transfer.
  299. *
  300. * @param jtag_info
  301. * @param addr Array of core register numbers.
  302. * @param count Amount of registers in arrays.
  303. * @param buffer Array of register values.
  304. */
  305. int arc_jtag_read_core_reg(struct arc_jtag *jtag_info, uint32_t *addr,
  306. uint32_t count, uint32_t *buffer)
  307. {
  308. return arc_jtag_read_registers(jtag_info, ARC_JTAG_CORE_REG, addr, count,
  309. buffer);
  310. }
  311. /** Wrapper function to ease writing of one AUX register. */
  312. int arc_jtag_write_aux_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
  313. uint32_t value)
  314. {
  315. return arc_jtag_write_aux_reg(jtag_info, &addr, 1, &value);
  316. }
  317. /**
  318. * Write AUX registers. addr is an array of addresses, and those addresses can
  319. * be in any order, though it is recommended that they are in sequential order
  320. * where possible, as this reduces number of JTAG commands to transfer.
  321. *
  322. * @param jtag_info
  323. * @param addr Array of registers numbers.
  324. * @param count Amount of registers in arrays.
  325. * @param buffer Array of register values.
  326. */
  327. int arc_jtag_write_aux_reg(struct arc_jtag *jtag_info, uint32_t *addr,
  328. uint32_t count, const uint32_t *buffer)
  329. {
  330. return arc_jtag_write_registers(jtag_info, ARC_JTAG_AUX_REG, addr, count,
  331. buffer);
  332. }
  333. /** Wrapper function to ease reading of one AUX register. */
  334. int arc_jtag_read_aux_reg_one(struct arc_jtag *jtag_info, uint32_t addr,
  335. uint32_t *value)
  336. {
  337. return arc_jtag_read_aux_reg(jtag_info, &addr, 1, value);
  338. }
  339. /**
  340. * Read AUX registers. addr is an array of addresses, and those addresses can
  341. * be in any order, though it is recommended that they are in sequential order
  342. * where possible, as this reduces number of JTAG commands to transfer.
  343. *
  344. * @param jtag_info
  345. * @param addr Array of AUX register numbers.
  346. * @param count Amount of registers in arrays.
  347. * @param buffer Array of register values.
  348. */
  349. int arc_jtag_read_aux_reg(struct arc_jtag *jtag_info, uint32_t *addr,
  350. uint32_t count, uint32_t *buffer)
  351. {
  352. return arc_jtag_read_registers(jtag_info, ARC_JTAG_AUX_REG, addr, count,
  353. buffer);
  354. }
  355. /**
  356. * Write a sequence of 4-byte words into target memory.
  357. *
  358. * We can write only 4byte words via JTAG, so any non-word writes should be
  359. * handled at higher levels by read-modify-write.
  360. *
  361. * This function writes directly to the memory, leaving any caches (if there
  362. * are any) in inconsistent state. It is responsibility of upper level to
  363. * resolve this.
  364. *
  365. * @param jtag_info
  366. * @param addr Address of first word to write into.
  367. * @param count Amount of word to write.
  368. * @param buffer Array to write into memory.
  369. */
  370. int arc_jtag_write_memory(struct arc_jtag *jtag_info, uint32_t addr,
  371. uint32_t count, const uint32_t *buffer)
  372. {
  373. assert(jtag_info);
  374. assert(buffer);
  375. LOG_DEBUG("Writing to memory: addr=0x%08" PRIx32 ";count=%" PRIu32 ";buffer[0]=0x%08" PRIx32,
  376. addr, count, *buffer);
  377. /* No need to waste time on useless operations. */
  378. if (!count)
  379. return ERROR_OK;
  380. /* We do not know where we come from. */
  381. arc_jtag_enque_reset_transaction(jtag_info);
  382. /* We want to write to memory. */
  383. arc_jtag_enque_set_transaction(jtag_info, ARC_JTAG_WRITE_TO_MEMORY, TAP_DRPAUSE);
  384. /* Set target memory address of the first word. */
  385. arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_ADDRESS_REG);
  386. arc_jtag_enque_write_dr(jtag_info, addr, TAP_DRPAUSE);
  387. /* Start sending words. Address is auto-incremented on 4bytes by HW. */
  388. arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_DATA_REG);
  389. uint32_t i;
  390. for (i = 0; i < count; i++)
  391. arc_jtag_enque_write_dr(jtag_info, *(buffer + i), TAP_IDLE);
  392. return jtag_execute_queue();
  393. }
  394. /**
  395. * Read a sequence of 4-byte words from target memory.
  396. *
  397. * We can read only 4byte words via JTAG.
  398. *
  399. * This function read directly from the memory, so it can read invalid data if
  400. * data cache hasn't been flushed before hand. It is responsibility of upper
  401. * level to resolve this.
  402. *
  403. * @param jtag_info
  404. * @param addr Address of first word to read from.
  405. * @param count Amount of words to read.
  406. * @param buffer Array of words to read into.
  407. * @param slow_memory Whether this is a slow memory (DDR) or fast (CCM).
  408. */
  409. int arc_jtag_read_memory(struct arc_jtag *jtag_info, uint32_t addr,
  410. uint32_t count, uint32_t *buffer, bool slow_memory)
  411. {
  412. uint8_t *data_buf;
  413. uint32_t i;
  414. int retval = ERROR_OK;
  415. assert(jtag_info);
  416. assert(jtag_info->tap);
  417. LOG_DEBUG("Reading memory: addr=0x%" PRIx32 ";count=%" PRIu32 ";slow=%c",
  418. addr, count, slow_memory ? 'Y' : 'N');
  419. if (!count)
  420. return ERROR_OK;
  421. data_buf = calloc(sizeof(uint8_t), count * 4);
  422. arc_jtag_enque_reset_transaction(jtag_info);
  423. /* We are reading from memory. */
  424. arc_jtag_enque_set_transaction(jtag_info, ARC_JTAG_READ_FROM_MEMORY, TAP_DRPAUSE);
  425. /* Read data */
  426. for (i = 0; i < count; i++) {
  427. /* When several words are read at consequent addresses we can
  428. * rely on ARC JTAG auto-incrementing address. That means that
  429. * address can be set only once, for a first word. However it
  430. * has been noted that at least in some cases when reading from
  431. * DDR, JTAG returns 0 instead of a real value. To workaround
  432. * this issue we need to do totally non-required address
  433. * writes, which however resolve a problem by introducing
  434. * delay. See STAR 9000832538... */
  435. if (slow_memory || i == 0) {
  436. /* Set address */
  437. arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_ADDRESS_REG);
  438. arc_jtag_enque_write_dr(jtag_info, addr + i * 4, TAP_IDLE);
  439. arc_jtag_enque_write_ir(jtag_info, ARC_JTAG_DATA_REG);
  440. }
  441. arc_jtag_enque_read_dr(jtag_info, data_buf + i * 4, TAP_IDLE);
  442. }
  443. retval = jtag_execute_queue();
  444. if (retval != ERROR_OK) {
  445. LOG_ERROR("Failed to execute jtag queue: %d", retval);
  446. retval = ERROR_FAIL;
  447. goto exit;
  448. }
  449. /* Convert byte-buffers to host presentation. */
  450. for (i = 0; i < count; i++)
  451. buffer[i] = buf_get_u32(data_buf + 4*i, 0, 32);
  452. exit:
  453. free(data_buf);
  454. return retval;
  455. }