scan_manager.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /*
  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. DECLARE_GLOBAL_DATA_PTR;
  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_io_scan_chain_prg() - Program HPS IO Scan Chain
  92. * @io_scan_chain_id: IO scan chain ID
  93. */
  94. static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id)
  95. {
  96. u32 residual;
  97. u32 io_scan_chain_len_in_bits;
  98. const unsigned long *iocsr_scan_chain;
  99. int i, ret, index = 0;
  100. ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain,
  101. &io_scan_chain_len_in_bits);
  102. if (ret)
  103. return 1;
  104. /*
  105. * De-assert reinit if the IO scan chain is intended for HIO. In
  106. * this, its the chain 3.
  107. */
  108. if (io_scan_chain_id == 3)
  109. clrbits_le32(&freeze_controller_base->hioctrl,
  110. SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK);
  111. /*
  112. * Check if the scan chain engine is inactive and the
  113. * WFIFO is empty before enabling the IO scan chain
  114. */
  115. ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  116. if (ret)
  117. return ret;
  118. /*
  119. * Enable IO Scan chain based on scan chain id
  120. * Note: only one chain can be enabled at a time
  121. */
  122. setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  123. /* Program IO scan chain in 128-bit iteration */
  124. for (i = 0; i < io_scan_chain_len_in_bits / 128; i++) {
  125. /* Write TDI_TDO packet header for 128-bit IO scan chain */
  126. scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0,
  127. TDI_TDO_MAX_PAYLOAD);
  128. /* write 4 successive 32-bit IO scan chain data into WFIFO */
  129. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  130. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  131. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  132. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  133. /*
  134. * Check if the scan chain engine has completed the
  135. * IO scan chain data shifting
  136. */
  137. ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  138. if (ret)
  139. goto error;
  140. }
  141. residual = io_scan_chain_len_in_bits % 128;
  142. /* Final TDI_TDO packet (if chain length is not aligned to 128 bits) */
  143. if (residual) {
  144. /*
  145. * Program the last part of IO scan chain write TDI_TDO
  146. * packet header (2 bytes) to scan manager.
  147. */
  148. scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0,
  149. residual - 1);
  150. for (i = 0; i < residual / 32; i++) {
  151. /*
  152. * write remaining scan chain data into scan
  153. * manager WFIFO with 4 bytes write
  154. */
  155. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0,
  156. iocsr_scan_chain[index++]);
  157. }
  158. residual = io_scan_chain_len_in_bits % 32;
  159. if (residual > 24) {
  160. /*
  161. * write the last 4B scan chain data
  162. * into scan manager WFIFO
  163. */
  164. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0,
  165. iocsr_scan_chain[index]);
  166. } else {
  167. /*
  168. * write the remaining 1 - 3 bytes scan chain
  169. * data into scan manager WFIFO byte by byte
  170. * to prevent JTAG engine shifting unused data
  171. * from the FIFO and mistaken the data as a
  172. * valid command (even though unused bits are
  173. * set to 0, but just to prevent hardware
  174. * glitch)
  175. */
  176. for (i = 0; i < residual; i += 8) {
  177. scan_mgr_jtag_io(0, 0x0,
  178. iocsr_scan_chain[index] >> i);
  179. }
  180. }
  181. /*
  182. * Check if the scan chain engine has completed the
  183. * IO scan chain data shifting
  184. */
  185. ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  186. if (ret)
  187. goto error;
  188. }
  189. /* Disable IO Scan chain when configuration done*/
  190. clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  191. return 0;
  192. error:
  193. /* Disable IO Scan chain when error detected */
  194. clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  195. return ret;
  196. }
  197. int scan_mgr_configure_iocsr(void)
  198. {
  199. int status = 0;
  200. /* configure the IOCSR through scan chain */
  201. status |= scan_mgr_io_scan_chain_prg(0);
  202. status |= scan_mgr_io_scan_chain_prg(1);
  203. status |= scan_mgr_io_scan_chain_prg(2);
  204. status |= scan_mgr_io_scan_chain_prg(3);
  205. return status;
  206. }