scan_manager.c 6.9 KB

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