fpga.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2012 Stefan Roese <sr@denx.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <spartan3.h>
  8. #include <command.h>
  9. #include <asm/gpio.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/hardware.h>
  12. #include <asm/arch/spr_misc.h>
  13. #include <asm/arch/spr_ssp.h>
  14. /*
  15. * FPGA program pin configuration on X600:
  16. *
  17. * Only PROG and DONE are connected to GPIOs. INIT is not connected to the
  18. * SoC at all. And CLOCK and DATA are connected to the SSP2 port. We use
  19. * 16bit serial writes via this SSP port to write the data bits into the
  20. * FPGA.
  21. */
  22. #define CONFIG_SYS_FPGA_PROG 2
  23. #define CONFIG_SYS_FPGA_DONE 3
  24. /*
  25. * Set the active-low FPGA reset signal.
  26. */
  27. static void fpga_reset(int assert)
  28. {
  29. /*
  30. * On x600 we have no means to toggle the FPGA reset signal
  31. */
  32. debug("%s:%d: RESET (%d)\n", __func__, __LINE__, assert);
  33. }
  34. /*
  35. * Set the FPGA's active-low SelectMap program line to the specified level
  36. */
  37. static int fpga_pgm_fn(int assert, int flush, int cookie)
  38. {
  39. debug("%s:%d: FPGA PROG (%d)\n", __func__, __LINE__, assert);
  40. gpio_set_value(CONFIG_SYS_FPGA_PROG, assert);
  41. return assert;
  42. }
  43. /*
  44. * Test the state of the active-low FPGA INIT line. Return 1 on INIT
  45. * asserted (low).
  46. */
  47. static int fpga_init_fn(int cookie)
  48. {
  49. static int state;
  50. debug("%s:%d: init (state=%d)\n", __func__, __LINE__, state);
  51. /*
  52. * On x600, the FPGA INIT signal is not connected to the SoC.
  53. * We can't read the INIT status. Let's return the "correct"
  54. * INIT signal state generated via a local state-machine.
  55. */
  56. if (++state == 1) {
  57. return 1;
  58. } else {
  59. state = 0;
  60. return 0;
  61. }
  62. }
  63. /*
  64. * Test the state of the active-high FPGA DONE pin
  65. */
  66. static int fpga_done_fn(int cookie)
  67. {
  68. struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
  69. /*
  70. * Wait for Tx-FIFO to become empty before looking for DONE
  71. */
  72. while (!(readl(&ssp->sspsr) & SSPSR_TFE))
  73. ;
  74. if (gpio_get_value(CONFIG_SYS_FPGA_DONE))
  75. return 1;
  76. else
  77. return 0;
  78. }
  79. /*
  80. * FPGA pre-configuration function. Just make sure that
  81. * FPGA reset is asserted to keep the FPGA from starting up after
  82. * configuration.
  83. */
  84. static int fpga_pre_config_fn(int cookie)
  85. {
  86. debug("%s:%d: FPGA pre-configuration\n", __func__, __LINE__);
  87. fpga_reset(true);
  88. return 0;
  89. }
  90. /*
  91. * FPGA post configuration function. Blip the FPGA reset line and then see if
  92. * the FPGA appears to be running.
  93. */
  94. static int fpga_post_config_fn(int cookie)
  95. {
  96. int rc = 0;
  97. debug("%s:%d: FPGA post configuration\n", __func__, __LINE__);
  98. fpga_reset(true);
  99. udelay(100);
  100. fpga_reset(false);
  101. udelay(100);
  102. return rc;
  103. }
  104. static int fpga_clk_fn(int assert_clk, int flush, int cookie)
  105. {
  106. /*
  107. * No dedicated clock signal on x600 (data & clock generated)
  108. * in SSP interface. So we don't have to do anything here.
  109. */
  110. return assert_clk;
  111. }
  112. static int fpga_wr_fn(int assert_write, int flush, int cookie)
  113. {
  114. struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
  115. static int count;
  116. static u16 data;
  117. /*
  118. * First collect 16 bits of data
  119. */
  120. data = data << 1;
  121. if (assert_write)
  122. data |= 1;
  123. /*
  124. * If 16 bits are not available, return for more bits
  125. */
  126. count++;
  127. if (count != 16)
  128. return assert_write;
  129. count = 0;
  130. /*
  131. * Wait for Tx-FIFO to become ready
  132. */
  133. while (!(readl(&ssp->sspsr) & SSPSR_TNF))
  134. ;
  135. /* Send 16 bits to FPGA via SSP bus */
  136. writel(data, &ssp->sspdr);
  137. return assert_write;
  138. }
  139. static xilinx_spartan3_slave_serial_fns x600_fpga_fns = {
  140. fpga_pre_config_fn,
  141. fpga_pgm_fn,
  142. fpga_clk_fn,
  143. fpga_init_fn,
  144. fpga_done_fn,
  145. fpga_wr_fn,
  146. fpga_post_config_fn,
  147. };
  148. static xilinx_desc fpga[CONFIG_FPGA_COUNT] = {
  149. XILINX_XC3S1200E_DESC(slave_serial, &x600_fpga_fns, 0)
  150. };
  151. /*
  152. * Initialize the SelectMap interface. We assume that the mode and the
  153. * initial state of all of the port pins have already been set!
  154. */
  155. static void fpga_serialslave_init(void)
  156. {
  157. debug("%s:%d: Initialize serial slave interface\n", __func__, __LINE__);
  158. fpga_pgm_fn(false, false, 0); /* make sure program pin is inactive */
  159. }
  160. static int expi_setup(int freq)
  161. {
  162. struct misc_regs *misc = (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
  163. int pll2_m, pll2_n, pll2_p, expi_x, expi_y;
  164. pll2_m = (freq * 2) / 1000;
  165. pll2_n = 15;
  166. pll2_p = 1;
  167. expi_x = 1;
  168. expi_y = 2;
  169. /*
  170. * Disable reset, Low compression, Disable retiming, Enable Expi,
  171. * Enable soft reset, DMA, PLL2, Internal
  172. */
  173. writel(EXPI_CLK_CFG_LOW_COMPR | EXPI_CLK_CFG_CLK_EN | EXPI_CLK_CFG_RST |
  174. EXPI_CLK_SYNT_EN | EXPI_CLK_CFG_SEL_PLL2 |
  175. EXPI_CLK_CFG_INT_CLK_EN | (expi_y << 16) | (expi_x << 24),
  176. &misc->expi_clk_cfg);
  177. /*
  178. * 6 uA, Internal feedback, 1st order, Non-dithered, Sample Parameters,
  179. * Enable PLL2, Disable reset
  180. */
  181. writel((pll2_m << 24) | (pll2_p << 8) | (pll2_n), &misc->pll2_frq);
  182. writel(PLL2_CNTL_6UA | PLL2_CNTL_SAMPLE | PLL2_CNTL_ENABLE |
  183. PLL2_CNTL_RESETN | PLL2_CNTL_LOCK, &misc->pll2_cntl);
  184. /*
  185. * Disable soft reset
  186. */
  187. clrbits_le32(&misc->expi_clk_cfg, EXPI_CLK_CFG_RST);
  188. return 0;
  189. }
  190. /*
  191. * Initialize the fpga
  192. */
  193. int x600_init_fpga(void)
  194. {
  195. struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
  196. struct misc_regs *misc = (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
  197. /* Enable SSP2 clock */
  198. writel(readl(&misc->periph1_clken) | MISC_SSP2ENB | MISC_GPIO4ENB,
  199. &misc->periph1_clken);
  200. /* Set EXPI clock to 45 MHz */
  201. expi_setup(45000);
  202. /* Configure GPIO directions */
  203. gpio_direction_output(CONFIG_SYS_FPGA_PROG, 0);
  204. gpio_direction_input(CONFIG_SYS_FPGA_DONE);
  205. writel(SSPCR0_DSS_16BITS, &ssp->sspcr0);
  206. writel(SSPCR1_SSE, &ssp->sspcr1);
  207. /*
  208. * Set lowest prescale divisor value (CPSDVSR) of 2 for max download
  209. * speed.
  210. *
  211. * Actual data clock rate is: 80MHz / (CPSDVSR * (SCR + 1))
  212. * With CPSDVSR at 2 and SCR at 0, the maximume clock rate is 40MHz.
  213. */
  214. writel(2, &ssp->sspcpsr);
  215. fpga_init();
  216. fpga_serialslave_init();
  217. debug("%s:%d: Adding fpga 0\n", __func__, __LINE__);
  218. fpga_add(fpga_xilinx, &fpga[0]);
  219. return 0;
  220. }