fpga.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. * Keith Outwater, keith_outwater@mvis.com.
  5. *
  6. * (C) Copyright 2011
  7. * Andre Schwarz, Matrix Vision GmbH, andre.schwarz@matrix-vision.de
  8. * Michael Jones, Matrix Vision GmbH, michael.jones@matrix-vision.de
  9. *
  10. * See file CREDITS for list of people who contributed to this
  11. * project.
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation; either version 2 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  26. * MA 02111-1307 USA
  27. *
  28. */
  29. #include <common.h>
  30. #include <ACEX1K.h>
  31. #include <command.h>
  32. #include <asm/gpio.h>
  33. #include "fpga.h"
  34. #ifdef FPGA_DEBUG
  35. #define fpga_debug(fmt, args...) printf("%s: "fmt, __func__, ##args)
  36. #else
  37. #define fpga_debug(fmt, args...)
  38. #endif
  39. Altera_CYC2_Passive_Serial_fns altera_fns = {
  40. fpga_null_fn, /* Altera_pre_fn */
  41. fpga_config_fn,
  42. fpga_status_fn,
  43. fpga_done_fn,
  44. fpga_wr_fn,
  45. fpga_null_fn,
  46. fpga_null_fn,
  47. };
  48. Altera_desc cyclone2 = {
  49. Altera_CYC2,
  50. fast_passive_parallel,
  51. Altera_EP3C5_SIZE,
  52. (void *) &altera_fns,
  53. NULL,
  54. 0
  55. };
  56. #define GPIO_RESET 43
  57. #define GPIO_DCLK 65
  58. #define GPIO_nSTATUS 157
  59. #define GPIO_CONF_DONE 158
  60. #define GPIO_nCONFIG 159
  61. #define GPIO_DATA0 54
  62. #define GPIO_DATA1 55
  63. #define GPIO_DATA2 56
  64. #define GPIO_DATA3 57
  65. #define GPIO_DATA4 58
  66. #define GPIO_DATA5 60
  67. #define GPIO_DATA6 61
  68. #define GPIO_DATA7 62
  69. DECLARE_GLOBAL_DATA_PTR;
  70. /* return FPGA_SUCCESS on success, else FPGA_FAIL
  71. */
  72. int mvblx_init_fpga(void)
  73. {
  74. fpga_debug("Initializing FPGA interface\n");
  75. fpga_init();
  76. fpga_add(fpga_altera, &cyclone2);
  77. if (gpio_request(GPIO_DCLK, "dclk") ||
  78. gpio_request(GPIO_nSTATUS, "nStatus") ||
  79. #ifndef CONFIG_SYS_FPGA_DONT_USE_CONF_DONE
  80. gpio_request(GPIO_CONF_DONE, "conf_done") ||
  81. #endif
  82. gpio_request(GPIO_nCONFIG, "nConfig") ||
  83. gpio_request(GPIO_DATA0, "data0") ||
  84. gpio_request(GPIO_DATA1, "data1") ||
  85. gpio_request(GPIO_DATA2, "data2") ||
  86. gpio_request(GPIO_DATA3, "data3") ||
  87. gpio_request(GPIO_DATA4, "data4") ||
  88. gpio_request(GPIO_DATA5, "data5") ||
  89. gpio_request(GPIO_DATA6, "data6") ||
  90. gpio_request(GPIO_DATA7, "data7")) {
  91. printf("%s: error requesting GPIOs.", __func__);
  92. return FPGA_FAIL;
  93. }
  94. /* set up outputs */
  95. gpio_direction_output(GPIO_DCLK, 0);
  96. gpio_direction_output(GPIO_nCONFIG, 0);
  97. gpio_direction_output(GPIO_DATA0, 0);
  98. gpio_direction_output(GPIO_DATA1, 0);
  99. gpio_direction_output(GPIO_DATA2, 0);
  100. gpio_direction_output(GPIO_DATA3, 0);
  101. gpio_direction_output(GPIO_DATA4, 0);
  102. gpio_direction_output(GPIO_DATA5, 0);
  103. gpio_direction_output(GPIO_DATA6, 0);
  104. gpio_direction_output(GPIO_DATA7, 0);
  105. /* NB omap_free_gpio() resets to an input, so we can't
  106. * free ie. nCONFIG, or else the FPGA would reset
  107. * Q: presumably gpio_free() has the same effect?
  108. */
  109. /* set up inputs */
  110. gpio_direction_input(GPIO_nSTATUS);
  111. #ifndef CONFIG_SYS_FPGA_DONT_USE_CONF_DONE
  112. gpio_direction_input(GPIO_CONF_DONE);
  113. #endif
  114. fpga_config_fn(0, 1, 0);
  115. udelay(60);
  116. return FPGA_SUCCESS;
  117. }
  118. int fpga_null_fn(int cookie)
  119. {
  120. return 0;
  121. }
  122. int fpga_config_fn(int assert, int flush, int cookie)
  123. {
  124. fpga_debug("SET config : %s=%d\n", assert ? "low" : "high", assert);
  125. if (flush) {
  126. gpio_set_value(GPIO_nCONFIG, !assert);
  127. udelay(1);
  128. gpio_set_value(GPIO_nCONFIG, assert);
  129. }
  130. return assert;
  131. }
  132. int fpga_done_fn(int cookie)
  133. {
  134. int result = 0;
  135. /* since revA of BLX, we will not get this signal. */
  136. udelay(10);
  137. #ifdef CONFIG_SYS_FPGA_DONT_USE_CONF_DONE
  138. fpga_debug("not waiting for CONF_DONE.");
  139. result = 1;
  140. #else
  141. fpga_debug("CONF_DONE check ... ");
  142. if (gpio_get_value(GPIO_CONF_DONE)) {
  143. fpga_debug("high\n");
  144. result = 1;
  145. } else
  146. fpga_debug("low\n");
  147. gpio_free(GPIO_CONF_DONE);
  148. #endif
  149. return result;
  150. }
  151. int fpga_status_fn(int cookie)
  152. {
  153. int result = 0;
  154. fpga_debug("STATUS check ... ");
  155. result = gpio_get_value(GPIO_nSTATUS);
  156. if (result < 0)
  157. fpga_debug("error\n");
  158. else if (result > 0)
  159. fpga_debug("high\n");
  160. else
  161. fpga_debug("low\n");
  162. return result;
  163. }
  164. static inline int _write_fpga(u8 byte)
  165. {
  166. gpio_set_value(GPIO_DATA0, byte & 0x01);
  167. gpio_set_value(GPIO_DATA1, (byte >> 1) & 0x01);
  168. gpio_set_value(GPIO_DATA2, (byte >> 2) & 0x01);
  169. gpio_set_value(GPIO_DATA3, (byte >> 3) & 0x01);
  170. gpio_set_value(GPIO_DATA4, (byte >> 4) & 0x01);
  171. gpio_set_value(GPIO_DATA5, (byte >> 5) & 0x01);
  172. gpio_set_value(GPIO_DATA6, (byte >> 6) & 0x01);
  173. gpio_set_value(GPIO_DATA7, (byte >> 7) & 0x01);
  174. /* clock */
  175. gpio_set_value(GPIO_DCLK, 1);
  176. udelay(1);
  177. gpio_set_value(GPIO_DCLK, 0);
  178. udelay(1);
  179. return 0;
  180. }
  181. int fpga_wr_fn(const void *buf, size_t len, int flush, int cookie)
  182. {
  183. unsigned char *data = (unsigned char *) buf;
  184. int i;
  185. fpga_debug("fpga_wr: buf %p / size %d\n", buf, len);
  186. for (i = 0; i < len; i++)
  187. _write_fpga(data[i]);
  188. fpga_debug("-%s\n", __func__);
  189. return FPGA_SUCCESS;
  190. }