fpga.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * (C) Copyright 2002-2013
  3. * Eric Jarrige <eric.jarrige@armadeus.org>
  4. *
  5. * based on the files by
  6. * Rich Ireland, Enterasys Networks, rireland@enterasys.com
  7. * and
  8. * Keith Outwater, keith_outwater@mvis.com
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <asm/arch/imx-regs.h>
  14. #include <asm/gpio.h>
  15. #include <asm/io.h>
  16. #include <command.h>
  17. #include <config.h>
  18. #include "fpga.h"
  19. #include <spartan3.h>
  20. #include "apf27.h"
  21. /*
  22. * Note that these are pointers to code that is in Flash. They will be
  23. * relocated at runtime.
  24. * Spartan2 code is used to download our Spartan 3 :) code is compatible.
  25. * Just take care about the file size
  26. */
  27. xilinx_spartan3_slave_parallel_fns fpga_fns = {
  28. fpga_pre_fn,
  29. fpga_pgm_fn,
  30. fpga_init_fn,
  31. NULL,
  32. fpga_done_fn,
  33. fpga_clk_fn,
  34. fpga_cs_fn,
  35. fpga_wr_fn,
  36. fpga_rdata_fn,
  37. fpga_wdata_fn,
  38. fpga_busy_fn,
  39. fpga_abort_fn,
  40. fpga_post_fn,
  41. };
  42. xilinx_desc fpga[CONFIG_FPGA_COUNT] = {
  43. {xilinx_spartan3,
  44. slave_parallel,
  45. 1196128l/8,
  46. (void *)&fpga_fns,
  47. 0,
  48. &spartan3_op,
  49. "3s200aft256"}
  50. };
  51. /*
  52. * Initialize GPIO port B before download
  53. */
  54. int fpga_pre_fn(int cookie)
  55. {
  56. /* Initialize GPIO pins */
  57. gpio_set_value(ACFG_FPGA_PWR, 1);
  58. imx_gpio_mode(ACFG_FPGA_INIT | GPIO_IN | GPIO_PUEN | GPIO_GPIO);
  59. imx_gpio_mode(ACFG_FPGA_DONE | GPIO_IN | GPIO_PUEN | GPIO_GPIO);
  60. imx_gpio_mode(ACFG_FPGA_PRG | GPIO_OUT | GPIO_PUEN | GPIO_GPIO);
  61. imx_gpio_mode(ACFG_FPGA_CLK | GPIO_OUT | GPIO_PUEN | GPIO_GPIO);
  62. imx_gpio_mode(ACFG_FPGA_RW | GPIO_OUT | GPIO_PUEN | GPIO_GPIO);
  63. imx_gpio_mode(ACFG_FPGA_CS | GPIO_OUT | GPIO_PUEN | GPIO_GPIO);
  64. imx_gpio_mode(ACFG_FPGA_SUSPEND|GPIO_OUT|GPIO_PUEN|GPIO_GPIO);
  65. gpio_set_value(ACFG_FPGA_RESET, 1);
  66. imx_gpio_mode(ACFG_FPGA_RESET | GPIO_OUT | GPIO_PUEN | GPIO_GPIO);
  67. imx_gpio_mode(ACFG_FPGA_PWR | GPIO_OUT | GPIO_PUEN | GPIO_GPIO);
  68. gpio_set_value(ACFG_FPGA_PRG, 1);
  69. gpio_set_value(ACFG_FPGA_CLK, 1);
  70. gpio_set_value(ACFG_FPGA_RW, 1);
  71. gpio_set_value(ACFG_FPGA_CS, 1);
  72. gpio_set_value(ACFG_FPGA_SUSPEND, 0);
  73. gpio_set_value(ACFG_FPGA_PWR, 0);
  74. udelay(30000); /*wait until supply started*/
  75. return cookie;
  76. }
  77. /*
  78. * Set the FPGA's active-low program line to the specified level
  79. */
  80. int fpga_pgm_fn(int assert, int flush, int cookie)
  81. {
  82. debug("%s:%d: FPGA PROGRAM %s", __func__, __LINE__,
  83. assert ? "high" : "low");
  84. gpio_set_value(ACFG_FPGA_PRG, !assert);
  85. return assert;
  86. }
  87. /*
  88. * Set the FPGA's active-high clock line to the specified level
  89. */
  90. int fpga_clk_fn(int assert_clk, int flush, int cookie)
  91. {
  92. debug("%s:%d: FPGA CLOCK %s", __func__, __LINE__,
  93. assert_clk ? "high" : "low");
  94. gpio_set_value(ACFG_FPGA_CLK, !assert_clk);
  95. return assert_clk;
  96. }
  97. /*
  98. * Test the state of the active-low FPGA INIT line. Return 1 on INIT
  99. * asserted (low).
  100. */
  101. int fpga_init_fn(int cookie)
  102. {
  103. int value;
  104. debug("%s:%d: INIT check... ", __func__, __LINE__);
  105. value = gpio_get_value(ACFG_FPGA_INIT);
  106. /* printf("init value read %x",value); */
  107. #ifdef CONFIG_SYS_FPGA_IS_PROTO
  108. return value;
  109. #else
  110. return !value;
  111. #endif
  112. }
  113. /*
  114. * Test the state of the active-high FPGA DONE pin
  115. */
  116. int fpga_done_fn(int cookie)
  117. {
  118. debug("%s:%d: DONE check... %s", __func__, __LINE__,
  119. gpio_get_value(ACFG_FPGA_DONE) ? "high" : "low");
  120. return gpio_get_value(ACFG_FPGA_DONE) ? FPGA_SUCCESS : FPGA_FAIL;
  121. }
  122. /*
  123. * Set the FPGA's wr line to the specified level
  124. */
  125. int fpga_wr_fn(int assert_write, int flush, int cookie)
  126. {
  127. debug("%s:%d: FPGA RW... %s ", __func__, __LINE__,
  128. assert_write ? "high" : "low");
  129. gpio_set_value(ACFG_FPGA_RW, !assert_write);
  130. return assert_write;
  131. }
  132. int fpga_cs_fn(int assert_cs, int flush, int cookie)
  133. {
  134. debug("%s:%d: FPGA CS %s ", __func__, __LINE__,
  135. assert_cs ? "high" : "low");
  136. gpio_set_value(ACFG_FPGA_CS, !assert_cs);
  137. return assert_cs;
  138. }
  139. int fpga_rdata_fn(unsigned char *data, int cookie)
  140. {
  141. debug("%s:%d: FPGA READ DATA %02X ", __func__, __LINE__,
  142. *((char *)ACFG_FPGA_RDATA));
  143. *data = (unsigned char)
  144. ((*((unsigned short *)ACFG_FPGA_RDATA))&0x00FF);
  145. return *data;
  146. }
  147. int fpga_wdata_fn(unsigned char data, int flush, int cookie)
  148. {
  149. debug("%s:%d: FPGA WRITE DATA %02X ", __func__, __LINE__,
  150. data);
  151. *((unsigned short *)ACFG_FPGA_WDATA) = data;
  152. return data;
  153. }
  154. int fpga_abort_fn(int cookie)
  155. {
  156. return fpga_post_fn(cookie);
  157. }
  158. int fpga_busy_fn(int cookie)
  159. {
  160. return 1;
  161. }
  162. int fpga_post_fn(int cookie)
  163. {
  164. debug("%s:%d: FPGA POST ", __func__, __LINE__);
  165. imx_gpio_mode(ACFG_FPGA_RW | GPIO_PF | GPIO_PUEN);
  166. imx_gpio_mode(ACFG_FPGA_CS | GPIO_PF | GPIO_PUEN);
  167. imx_gpio_mode(ACFG_FPGA_CLK | GPIO_PF | GPIO_PUEN);
  168. gpio_set_value(ACFG_FPGA_PRG, 1);
  169. gpio_set_value(ACFG_FPGA_RESET, 0);
  170. imx_gpio_mode(ACFG_FPGA_RESET | GPIO_OUT | GPIO_PUEN | GPIO_GPIO);
  171. return cookie;
  172. }
  173. void apf27_fpga_setup(void)
  174. {
  175. struct pll_regs *pll = (struct pll_regs *)IMX_PLL_BASE;
  176. struct system_control_regs *system =
  177. (struct system_control_regs *)IMX_SYSTEM_CTL_BASE;
  178. /* Configure FPGA CLKO */
  179. writel(ACFG_CCSR_VAL, &pll->ccsr);
  180. /* Configure strentgh for FPGA */
  181. writel(ACFG_DSCR10_VAL, &system->dscr10);
  182. writel(ACFG_DSCR3_VAL, &system->dscr3);
  183. writel(ACFG_DSCR7_VAL, &system->dscr7);
  184. writel(ACFG_DSCR2_VAL, &system->dscr2);
  185. }
  186. /*
  187. * Initialize the fpga. Return 1 on success, 0 on failure.
  188. */
  189. void APF27_init_fpga(void)
  190. {
  191. int i;
  192. apf27_fpga_setup();
  193. fpga_init();
  194. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  195. debug("%s:%d: Adding fpga %d\n", __func__, __LINE__, i);
  196. fpga_add(fpga_xilinx, &fpga[i]);
  197. }
  198. return;
  199. }