scan_manager.c 6.9 KB

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