socfpga.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2012 Altera Corporation <www.altera.com>
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <linux/errno.h>
  10. #include <asm/arch/fpga_manager.h>
  11. #include <asm/arch/reset_manager.h>
  12. #include <asm/arch/system_manager.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /* Timeout count */
  15. #define FPGA_TIMEOUT_CNT 0x1000000
  16. static struct socfpga_fpga_manager *fpgamgr_regs =
  17. (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS;
  18. static struct socfpga_system_manager *sysmgr_regs =
  19. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  20. /* Set CD ratio */
  21. static void fpgamgr_set_cd_ratio(unsigned long ratio)
  22. {
  23. clrsetbits_le32(&fpgamgr_regs->ctrl,
  24. 0x3 << FPGAMGRREGS_CTRL_CDRATIO_LSB,
  25. (ratio & 0x3) << FPGAMGRREGS_CTRL_CDRATIO_LSB);
  26. }
  27. static int fpgamgr_dclkcnt_set(unsigned long cnt)
  28. {
  29. unsigned long i;
  30. /* Clear any existing done status */
  31. if (readl(&fpgamgr_regs->dclkstat))
  32. writel(0x1, &fpgamgr_regs->dclkstat);
  33. /* Write the dclkcnt */
  34. writel(cnt, &fpgamgr_regs->dclkcnt);
  35. /* Wait till the dclkcnt done */
  36. for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
  37. if (!readl(&fpgamgr_regs->dclkstat))
  38. continue;
  39. writel(0x1, &fpgamgr_regs->dclkstat);
  40. return 0;
  41. }
  42. return -ETIMEDOUT;
  43. }
  44. /* Start the FPGA programming by initialize the FPGA Manager */
  45. static int fpgamgr_program_init(void)
  46. {
  47. unsigned long msel, i;
  48. /* Get the MSEL value */
  49. msel = readl(&fpgamgr_regs->stat);
  50. msel &= FPGAMGRREGS_STAT_MSEL_MASK;
  51. msel >>= FPGAMGRREGS_STAT_MSEL_LSB;
  52. /*
  53. * Set the cfg width
  54. * If MSEL[3] = 1, cfg width = 32 bit
  55. */
  56. if (msel & 0x8) {
  57. setbits_le32(&fpgamgr_regs->ctrl,
  58. FPGAMGRREGS_CTRL_CFGWDTH_MASK);
  59. /* To determine the CD ratio */
  60. /* MSEL[1:0] = 0, CD Ratio = 1 */
  61. if ((msel & 0x3) == 0x0)
  62. fpgamgr_set_cd_ratio(CDRATIO_x1);
  63. /* MSEL[1:0] = 1, CD Ratio = 4 */
  64. else if ((msel & 0x3) == 0x1)
  65. fpgamgr_set_cd_ratio(CDRATIO_x4);
  66. /* MSEL[1:0] = 2, CD Ratio = 8 */
  67. else if ((msel & 0x3) == 0x2)
  68. fpgamgr_set_cd_ratio(CDRATIO_x8);
  69. } else { /* MSEL[3] = 0 */
  70. clrbits_le32(&fpgamgr_regs->ctrl,
  71. FPGAMGRREGS_CTRL_CFGWDTH_MASK);
  72. /* To determine the CD ratio */
  73. /* MSEL[1:0] = 0, CD Ratio = 1 */
  74. if ((msel & 0x3) == 0x0)
  75. fpgamgr_set_cd_ratio(CDRATIO_x1);
  76. /* MSEL[1:0] = 1, CD Ratio = 2 */
  77. else if ((msel & 0x3) == 0x1)
  78. fpgamgr_set_cd_ratio(CDRATIO_x2);
  79. /* MSEL[1:0] = 2, CD Ratio = 4 */
  80. else if ((msel & 0x3) == 0x2)
  81. fpgamgr_set_cd_ratio(CDRATIO_x4);
  82. }
  83. /* To enable FPGA Manager configuration */
  84. clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCE_MASK);
  85. /* To enable FPGA Manager drive over configuration line */
  86. setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
  87. /* Put FPGA into reset phase */
  88. setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
  89. /* (1) wait until FPGA enter reset phase */
  90. for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
  91. if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_RESETPHASE)
  92. break;
  93. }
  94. /* If not in reset state, return error */
  95. if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_RESETPHASE) {
  96. puts("FPGA: Could not reset\n");
  97. return -1;
  98. }
  99. /* Release FPGA from reset phase */
  100. clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
  101. /* (2) wait until FPGA enter configuration phase */
  102. for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
  103. if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_CFGPHASE)
  104. break;
  105. }
  106. /* If not in configuration state, return error */
  107. if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_CFGPHASE) {
  108. puts("FPGA: Could not configure\n");
  109. return -2;
  110. }
  111. /* Clear all interrupts in CB Monitor */
  112. writel(0xFFF, &fpgamgr_regs->gpio_porta_eoi);
  113. /* Enable AXI configuration */
  114. setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
  115. return 0;
  116. }
  117. /* Write the RBF data to FPGA Manager */
  118. static void fpgamgr_program_write(const void *rbf_data, unsigned long rbf_size)
  119. {
  120. uint32_t src = (uint32_t)rbf_data;
  121. uint32_t dst = SOCFPGA_FPGAMGRDATA_ADDRESS;
  122. /* Number of loops for 32-byte long copying. */
  123. uint32_t loops32 = rbf_size / 32;
  124. /* Number of loops for 4-byte long copying + trailing bytes */
  125. uint32_t loops4 = DIV_ROUND_UP(rbf_size % 32, 4);
  126. asm volatile(
  127. "1: ldmia %0!, {r0-r7}\n"
  128. " stmia %1!, {r0-r7}\n"
  129. " sub %1, #32\n"
  130. " subs %2, #1\n"
  131. " bne 1b\n"
  132. " cmp %3, #0\n"
  133. " beq 3f\n"
  134. "2: ldr %2, [%0], #4\n"
  135. " str %2, [%1]\n"
  136. " subs %3, #1\n"
  137. " bne 2b\n"
  138. "3: nop\n"
  139. : "+r"(src), "+r"(dst), "+r"(loops32), "+r"(loops4) :
  140. : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "cc");
  141. }
  142. /* Ensure the FPGA entering config done */
  143. static int fpgamgr_program_poll_cd(void)
  144. {
  145. const uint32_t mask = FPGAMGRREGS_MON_GPIO_EXT_PORTA_NS_MASK |
  146. FPGAMGRREGS_MON_GPIO_EXT_PORTA_CD_MASK;
  147. unsigned long reg, i;
  148. /* (3) wait until full config done */
  149. for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
  150. reg = readl(&fpgamgr_regs->gpio_ext_porta);
  151. /* Config error */
  152. if (!(reg & mask)) {
  153. printf("FPGA: Configuration error.\n");
  154. return -3;
  155. }
  156. /* Config done without error */
  157. if (reg & mask)
  158. break;
  159. }
  160. /* Timeout happened, return error */
  161. if (i == FPGA_TIMEOUT_CNT) {
  162. printf("FPGA: Timeout waiting for program.\n");
  163. return -4;
  164. }
  165. /* Disable AXI configuration */
  166. clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
  167. return 0;
  168. }
  169. /* Ensure the FPGA entering init phase */
  170. static int fpgamgr_program_poll_initphase(void)
  171. {
  172. unsigned long i;
  173. /* Additional clocks for the CB to enter initialization phase */
  174. if (fpgamgr_dclkcnt_set(0x4))
  175. return -5;
  176. /* (4) wait until FPGA enter init phase or user mode */
  177. for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
  178. if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_INITPHASE)
  179. break;
  180. if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
  181. break;
  182. }
  183. /* If not in configuration state, return error */
  184. if (i == FPGA_TIMEOUT_CNT)
  185. return -6;
  186. return 0;
  187. }
  188. /* Ensure the FPGA entering user mode */
  189. static int fpgamgr_program_poll_usermode(void)
  190. {
  191. unsigned long i;
  192. /* Additional clocks for the CB to exit initialization phase */
  193. if (fpgamgr_dclkcnt_set(0x5000))
  194. return -7;
  195. /* (5) wait until FPGA enter user mode */
  196. for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
  197. if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
  198. break;
  199. }
  200. /* If not in configuration state, return error */
  201. if (i == FPGA_TIMEOUT_CNT)
  202. return -8;
  203. /* To release FPGA Manager drive over configuration line */
  204. clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
  205. return 0;
  206. }
  207. /*
  208. * FPGA Manager to program the FPGA. This is the interface used by FPGA driver.
  209. * Return 0 for sucess, non-zero for error.
  210. */
  211. int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
  212. {
  213. unsigned long status;
  214. if ((uint32_t)rbf_data & 0x3) {
  215. puts("FPGA: Unaligned data, realign to 32bit boundary.\n");
  216. return -EINVAL;
  217. }
  218. /* Prior programming the FPGA, all bridges need to be shut off */
  219. /* Disable all signals from hps peripheral controller to fpga */
  220. writel(0, &sysmgr_regs->fpgaintfgrp_module);
  221. /* Disable all signals from FPGA to HPS SDRAM */
  222. #define SDR_CTRLGRP_FPGAPORTRST_ADDRESS 0x5080
  223. writel(0, SOCFPGA_SDR_ADDRESS + SDR_CTRLGRP_FPGAPORTRST_ADDRESS);
  224. /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
  225. socfpga_bridges_reset(1);
  226. /* Unmap the bridges from NIC-301 */
  227. writel(0x1, SOCFPGA_L3REGS_ADDRESS);
  228. /* Initialize the FPGA Manager */
  229. status = fpgamgr_program_init();
  230. if (status)
  231. return status;
  232. /* Write the RBF data to FPGA Manager */
  233. fpgamgr_program_write(rbf_data, rbf_size);
  234. /* Ensure the FPGA entering config done */
  235. status = fpgamgr_program_poll_cd();
  236. if (status)
  237. return status;
  238. /* Ensure the FPGA entering init phase */
  239. status = fpgamgr_program_poll_initphase();
  240. if (status)
  241. return status;
  242. /* Ensure the FPGA entering user mode */
  243. return fpgamgr_program_poll_usermode();
  244. }