scan_manager.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. #define SCANMGR_STAT_ACTIVE (1 << 31)
  19. #define SCANMGR_STAT_WFIFOCNT_MASK 0x70000000
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static const struct socfpga_scan_manager *scan_manager_base =
  22. (void *)(SOCFPGA_SCANMGR_ADDRESS);
  23. static const struct socfpga_freeze_controller *freeze_controller_base =
  24. (void *)(SOCFPGA_SYSMGR_ADDRESS + SYSMGR_FRZCTRL_ADDRESS);
  25. /**
  26. * scan_chain_engine_is_idle() - Check if the JTAG scan chain is idle
  27. * @max_iter: Maximum number of iterations to wait for idle
  28. *
  29. * Function to check IO scan chain engine status and wait if the engine is
  30. * is active. Poll the IO scan chain engine till maximum iteration reached.
  31. */
  32. static u32 scan_chain_engine_is_idle(u32 max_iter)
  33. {
  34. const u32 mask = SCANMGR_STAT_ACTIVE | SCANMGR_STAT_WFIFOCNT_MASK;
  35. u32 status;
  36. /* Poll the engine until the scan engine is inactive. */
  37. do {
  38. status = readl(&scan_manager_base->stat);
  39. if (!(status & mask))
  40. return 0;
  41. } while (max_iter--);
  42. return -ETIMEDOUT;
  43. }
  44. #define JTAG_BP_INSN (1 << 0)
  45. #define JTAG_BP_TMS (1 << 1)
  46. #define JTAG_BP_PAYLOAD (1 << 2)
  47. #define JTAG_BP_2BYTE (1 << 3)
  48. #define JTAG_BP_4BYTE (1 << 4)
  49. /**
  50. * scan_mgr_jtag_io() - Access the JTAG chain
  51. * @flags: Control flags, used to configure the action on the JTAG
  52. * @iarg: Instruction argument
  53. * @parg: Payload argument or data
  54. *
  55. * Perform I/O on the JTAG chain
  56. */
  57. static void scan_mgr_jtag_io(const u32 flags, const u8 iarg, const u32 parg)
  58. {
  59. u32 data = parg;
  60. if (flags & JTAG_BP_INSN) { /* JTAG instruction */
  61. /*
  62. * The SCC JTAG register is LSB first, so make
  63. * space for the instruction at the LSB.
  64. */
  65. data <<= 8;
  66. if (flags & JTAG_BP_TMS) {
  67. data |= (0 << 7); /* TMS instruction. */
  68. data |= iarg & 0x3f; /* TMS arg is 6 bits. */
  69. if (flags & JTAG_BP_PAYLOAD)
  70. data |= (1 << 6);
  71. } else {
  72. data |= (1 << 7); /* TDI/TDO instruction. */
  73. data |= iarg & 0xf; /* TDI/TDO arg is 4 bits. */
  74. if (flags & JTAG_BP_PAYLOAD)
  75. data |= (1 << 4);
  76. }
  77. }
  78. if (flags & JTAG_BP_4BYTE)
  79. writel(data, &scan_manager_base->fifo_quad_byte);
  80. else if (flags & JTAG_BP_2BYTE)
  81. writel(data & 0xffff, &scan_manager_base->fifo_double_byte);
  82. else
  83. writel(data & 0xff, &scan_manager_base->fifo_single_byte);
  84. }
  85. /**
  86. * scan_mgr_io_scan_chain_prg() - Program HPS IO Scan Chain
  87. * @io_scan_chain_id: IO scan chain ID
  88. */
  89. static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id)
  90. {
  91. uint32_t io_program_iter;
  92. uint32_t io_scan_chain_data_residual;
  93. uint32_t residual;
  94. uint32_t i, ret;
  95. uint32_t index = 0;
  96. uint32_t io_scan_chain_len_in_bits;
  97. const unsigned long *iocsr_scan_chain;
  98. ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain,
  99. &io_scan_chain_len_in_bits);
  100. if (ret)
  101. return 1;
  102. /*
  103. * De-assert reinit if the IO scan chain is intended for HIO. In
  104. * this, its the chain 3.
  105. */
  106. if (io_scan_chain_id == 3)
  107. clrbits_le32(&freeze_controller_base->hioctrl,
  108. SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK);
  109. /*
  110. * Check if the scan chain engine is inactive and the
  111. * WFIFO is empty before enabling the IO scan chain
  112. */
  113. ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  114. if (ret)
  115. return ret;
  116. /*
  117. * Enable IO Scan chain based on scan chain id
  118. * Note: only one chain can be enabled at a time
  119. */
  120. setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  121. /*
  122. * Calculate number of iteration needed for full 128-bit (4 x32-bits)
  123. * bits shifting. Each TDI_TDO packet can shift in maximum 128-bits
  124. */
  125. io_program_iter = io_scan_chain_len_in_bits >>
  126. IO_SCAN_CHAIN_128BIT_SHIFT;
  127. io_scan_chain_data_residual = io_scan_chain_len_in_bits &
  128. IO_SCAN_CHAIN_128BIT_MASK;
  129. /* Program IO scan chain in 128-bit iteration */
  130. for (i = 0; i < io_program_iter; i++) {
  131. /* Write TDI_TDO packet header for 128-bit IO scan chain */
  132. scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0,
  133. TDI_TDO_MAX_PAYLOAD);
  134. /* write 4 successive 32-bit IO scan chain data into WFIFO */
  135. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  136. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  137. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  138. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
  139. /*
  140. * Check if the scan chain engine has completed the
  141. * IO scan chain data shifting
  142. */
  143. ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  144. if (ret)
  145. goto error;
  146. }
  147. /* Final TDI_TDO packet if any */
  148. if (io_scan_chain_data_residual) {
  149. /*
  150. * Calculate number of quad bytes FIFO write
  151. * needed for the final TDI_TDO packet
  152. */
  153. io_program_iter = io_scan_chain_data_residual >>
  154. IO_SCAN_CHAIN_32BIT_SHIFT;
  155. /*
  156. * Program the last part of IO scan chain write TDI_TDO
  157. * packet header (2 bytes) to scan manager.
  158. */
  159. scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0,
  160. io_scan_chain_data_residual - 1);
  161. for (i = 0; i < io_program_iter; i++) {
  162. /*
  163. * write remaining scan chain data into scan
  164. * manager WFIFO with 4 bytes write
  165. */
  166. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0,
  167. iocsr_scan_chain[index++]);
  168. }
  169. residual = io_scan_chain_data_residual &
  170. IO_SCAN_CHAIN_32BIT_MASK;
  171. if (IO_SCAN_CHAIN_PAYLOAD_24BIT < residual) {
  172. /*
  173. * write the last 4B scan chain data
  174. * into scan manager WFIFO
  175. */
  176. scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0,
  177. iocsr_scan_chain[index]);
  178. } else {
  179. /*
  180. * write the remaining 1 - 3 bytes scan chain
  181. * data into scan manager WFIFO byte by byte
  182. * to prevent JTAG engine shifting unused data
  183. * from the FIFO and mistaken the data as a
  184. * valid command (even though unused bits are
  185. * set to 0, but just to prevent hardware
  186. * glitch)
  187. */
  188. for (i = 0; i < residual; i += 8) {
  189. scan_mgr_jtag_io(0, 0x0,
  190. iocsr_scan_chain[index] >> i);
  191. }
  192. }
  193. /*
  194. * Check if the scan chain engine has completed the
  195. * IO scan chain data shifting
  196. */
  197. ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
  198. if (ret)
  199. goto error;
  200. }
  201. /* Disable IO Scan chain when configuration done*/
  202. clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  203. return 0;
  204. error:
  205. /* Disable IO Scan chain when error detected */
  206. clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
  207. return ret;
  208. }
  209. int scan_mgr_configure_iocsr(void)
  210. {
  211. int status = 0;
  212. /* configure the IOCSR through scan chain */
  213. status |= scan_mgr_io_scan_chain_prg(0);
  214. status |= scan_mgr_io_scan_chain_prg(1);
  215. status |= scan_mgr_io_scan_chain_prg(2);
  216. status |= scan_mgr_io_scan_chain_prg(3);
  217. return status;
  218. }