scan_manager.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_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. }