scan_manager.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2013 Altera Corporation <www.altera.com>
  4. */
  5. #include <common.h>
  6. #include <errno.h>
  7. #include <asm/io.h>
  8. #include <asm/arch/freeze_controller.h>
  9. #include <asm/arch/scan_manager.h>
  10. #include <asm/arch/system_manager.h>
  11. /*
  12. * Maximum polling loop to wait for IO scan chain engine becomes idle
  13. * to prevent infinite loop. It is important that this is NOT changed
  14. * to delay using timer functions, since at the time this function is
  15. * called, timer might not yet be inited.
  16. */
  17. #define SCANMGR_MAX_DELAY 100
  18. /*
  19. * Maximum length of TDI_TDO packet payload is 128 bits,
  20. * represented by (length - 1) in TDI_TDO header.
  21. */
  22. #define TDI_TDO_MAX_PAYLOAD 127
  23. #define SCANMGR_STAT_ACTIVE (1 << 31)
  24. #define SCANMGR_STAT_WFIFOCNT_MASK 0x70000000
  25. static const struct socfpga_scan_manager *scan_manager_base =
  26. (void *)(SOCFPGA_SCANMGR_ADDRESS);
  27. static const struct socfpga_freeze_controller *freeze_controller_base =
  28. (void *)(SOCFPGA_SYSMGR_ADDRESS + SYSMGR_FRZCTRL_ADDRESS);
  29. static struct socfpga_system_manager *sys_mgr_base =
  30. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  31. /**
  32. * scan_chain_engine_is_idle() - Check if the JTAG scan chain is idle
  33. * @max_iter: Maximum number of iterations to wait for idle
  34. *
  35. * Function to check IO scan chain engine status and wait if the engine is
  36. * is active. Poll the IO scan chain engine till maximum iteration reached.
  37. */
  38. static u32 scan_chain_engine_is_idle(u32 max_iter)
  39. {
  40. const u32 mask = SCANMGR_STAT_ACTIVE | SCANMGR_STAT_WFIFOCNT_MASK;
  41. u32 status;
  42. /* Poll the engine until the scan engine is inactive. */
  43. do {
  44. status = readl(&scan_manager_base->stat);
  45. if (!(status & mask))
  46. return 0;
  47. } while (max_iter--);
  48. return -ETIMEDOUT;
  49. }
  50. #define JTAG_BP_INSN (1 << 0)
  51. #define JTAG_BP_TMS (1 << 1)
  52. #define JTAG_BP_PAYLOAD (1 << 2)
  53. #define JTAG_BP_2BYTE (1 << 3)
  54. #define JTAG_BP_4BYTE (1 << 4)
  55. /**
  56. * scan_mgr_jtag_io() - Access the JTAG chain
  57. * @flags: Control flags, used to configure the action on the JTAG
  58. * @iarg: Instruction argument
  59. * @parg: Payload argument or data
  60. *
  61. * Perform I/O on the JTAG chain
  62. */
  63. static void scan_mgr_jtag_io(const u32 flags, const u8 iarg, const u32 parg)
  64. {
  65. u32 data = parg;
  66. if (flags & JTAG_BP_INSN) { /* JTAG instruction */
  67. /*
  68. * The SCC JTAG register is LSB first, so make
  69. * space for the instruction at the LSB.
  70. */
  71. data <<= 8;
  72. if (flags & JTAG_BP_TMS) {
  73. data |= (0 << 7); /* TMS instruction. */
  74. data |= iarg & 0x3f; /* TMS arg is 6 bits. */
  75. if (flags & JTAG_BP_PAYLOAD)
  76. data |= (1 << 6);
  77. } else {
  78. data |= (1 << 7); /* TDI/TDO instruction. */
  79. data |= iarg & 0xf; /* TDI/TDO arg is 4 bits. */
  80. if (flags & JTAG_BP_PAYLOAD)
  81. data |= (1 << 4);
  82. }
  83. }
  84. if (flags & JTAG_BP_4BYTE)
  85. writel(data, &scan_manager_base->fifo_quad_byte);
  86. else if (flags & JTAG_BP_2BYTE)
  87. writel(data & 0xffff, &scan_manager_base->fifo_double_byte);
  88. else
  89. writel(data & 0xff, &scan_manager_base->fifo_single_byte);
  90. }
  91. /**
  92. * scan_mgr_jtag_insn_data() - Send JTAG instruction and data
  93. * @iarg: Instruction argument
  94. * @data: Associated data
  95. * @dlen: Length of data in bits
  96. *
  97. * This function is used when programming the IO chains to submit the
  98. * instruction followed by variable length payload.
  99. */
  100. static int
  101. scan_mgr_jtag_insn_data(const u8 iarg, const unsigned long *data,
  102. const unsigned int dlen)
  103. {
  104. int i, j;
  105. scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, iarg, dlen - 1);
  106. /* 32 bits or more remain */
  107. for (i = 0; i < dlen / 32; i++)
  108. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, data[i]);
  109. if ((dlen % 32) > 24) { /* 31...24 bits remain */
  110. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, data[i]);
  111. } else if (dlen % 32) { /* 24...1 bit remain */
  112. for (j = 0; j < dlen % 32; j += 8)
  113. scan_mgr_jtag_io(0, 0x0, data[i] >> j);
  114. }
  115. return scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  116. }
  117. /**
  118. * scan_mgr_io_scan_chain_prg() - Program HPS IO Scan Chain
  119. * @io_scan_chain_id: IO scan chain ID
  120. */
  121. static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id)
  122. {
  123. u32 io_scan_chain_len_in_bits;
  124. const unsigned long *iocsr_scan_chain;
  125. unsigned int rem, idx = 0;
  126. int ret;
  127. ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain,
  128. &io_scan_chain_len_in_bits);
  129. if (ret)
  130. return 1;
  131. /*
  132. * De-assert reinit if the IO scan chain is intended for HIO. In
  133. * this, its the chain 3.
  134. */
  135. if (io_scan_chain_id == 3)
  136. clrbits_le32(&freeze_controller_base->hioctrl,
  137. SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK);
  138. /*
  139. * Check if the scan chain engine is inactive and the
  140. * WFIFO is empty before enabling the IO scan chain
  141. */
  142. ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  143. if (ret)
  144. return ret;
  145. /*
  146. * Enable IO Scan chain based on scan chain id
  147. * Note: only one chain can be enabled at a time
  148. */
  149. setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  150. /* Program IO scan chain. */
  151. while (io_scan_chain_len_in_bits) {
  152. if (io_scan_chain_len_in_bits > 128)
  153. rem = 128;
  154. else
  155. rem = io_scan_chain_len_in_bits;
  156. ret = scan_mgr_jtag_insn_data(0x0, &iocsr_scan_chain[idx], rem);
  157. if (ret)
  158. goto error;
  159. io_scan_chain_len_in_bits -= rem;
  160. idx += 4;
  161. }
  162. /* Disable IO Scan chain when configuration done*/
  163. clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  164. return 0;
  165. error:
  166. /* Disable IO Scan chain when error detected */
  167. clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  168. return ret;
  169. }
  170. int scan_mgr_configure_iocsr(void)
  171. {
  172. int status = 0;
  173. /* configure the IOCSR through scan chain */
  174. status |= scan_mgr_io_scan_chain_prg(0);
  175. status |= scan_mgr_io_scan_chain_prg(1);
  176. status |= scan_mgr_io_scan_chain_prg(2);
  177. status |= scan_mgr_io_scan_chain_prg(3);
  178. return status;
  179. }
  180. /**
  181. * scan_mgr_get_fpga_id() - Obtain FPGA JTAG ID
  182. *
  183. * This function obtains JTAG ID from the FPGA TAP controller.
  184. */
  185. u32 scan_mgr_get_fpga_id(void)
  186. {
  187. const unsigned long data = 0;
  188. u32 id = 0xffffffff;
  189. int ret;
  190. /* Enable HPS to talk to JTAG in the FPGA through the System Manager */
  191. writel(0x1, &sys_mgr_base->scanmgrgrp_ctrl);
  192. /* Enable port 7 */
  193. writel(0x80, &scan_manager_base->en);
  194. /* write to CSW to make s2f_ntrst reset */
  195. writel(0x02, &scan_manager_base->stat);
  196. /* Add a pause */
  197. mdelay(1);
  198. /* write 0x00 to CSW to clear the s2f_ntrst */
  199. writel(0, &scan_manager_base->stat);
  200. /*
  201. * Go to Test-Logic-Reset state.
  202. * This sets TAP controller into IDCODE mode.
  203. */
  204. scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_TMS, 0x1f | (1 << 5), 0x0);
  205. /* Go to Run-Test/Idle -> DR-Scan -> Capture-DR -> Shift-DR state. */
  206. scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_TMS, 0x02 | (1 << 4), 0x0);
  207. /*
  208. * Push 4 bytes of data through TDI->DR->TDO.
  209. *
  210. * Length of TDI data is 32bits (length - 1) and they are only
  211. * zeroes as we care only for TDO data.
  212. */
  213. ret = scan_mgr_jtag_insn_data(0x4, &data, 32);
  214. /* Read 32 bit from captured JTAG data. */
  215. if (!ret)
  216. id = readl(&scan_manager_base->fifo_quad_byte);
  217. /* Disable all port */
  218. writel(0, &scan_manager_base->en);
  219. writel(0, &sys_mgr_base->scanmgrgrp_ctrl);
  220. return id;
  221. }