fpga.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <linux/byteorder/generic.h>
  34. #include "fpga.h"
  35. #ifdef FPGA_DEBUG
  36. #define fpga_debug(fmt, args...) printf("%s: "fmt, __func__, ##args)
  37. #else
  38. #define fpga_debug(fmt, args...)
  39. #endif
  40. Altera_CYC2_Passive_Serial_fns altera_fns = {
  41. fpga_null_fn, /* Altera_pre_fn */
  42. fpga_config_fn,
  43. fpga_status_fn,
  44. fpga_done_fn,
  45. fpga_wr_fn,
  46. fpga_null_fn,
  47. fpga_null_fn,
  48. };
  49. Altera_desc cyclone2 = {
  50. Altera_CYC2,
  51. fast_passive_parallel,
  52. Altera_EP3C5_SIZE,
  53. (void *) &altera_fns,
  54. NULL,
  55. 0
  56. };
  57. #define GPIO_RESET 43
  58. #define GPIO_DCLK 65
  59. #define GPIO_nSTATUS 157
  60. #define GPIO_CONF_DONE 158
  61. #define GPIO_nCONFIG 159
  62. #define GPIO_DATA0 54
  63. #define GPIO_DATA1 55
  64. #define GPIO_DATA2 56
  65. #define GPIO_DATA3 57
  66. #define GPIO_DATA4 58
  67. #define GPIO_DATA5 60
  68. #define GPIO_DATA6 61
  69. #define GPIO_DATA7 62
  70. DECLARE_GLOBAL_DATA_PTR;
  71. /* return FPGA_SUCCESS on success, else FPGA_FAIL
  72. */
  73. int mvblx_init_fpga(void)
  74. {
  75. fpga_debug("Initializing FPGA interface\n");
  76. fpga_init();
  77. fpga_add(fpga_altera, &cyclone2);
  78. if (gpio_request(GPIO_DCLK, "dclk") ||
  79. gpio_request(GPIO_nSTATUS, "nStatus") ||
  80. #ifndef CONFIG_SYS_FPGA_DONT_USE_CONF_DONE
  81. gpio_request(GPIO_CONF_DONE, "conf_done") ||
  82. #endif
  83. gpio_request(GPIO_nCONFIG, "nConfig") ||
  84. gpio_request(GPIO_DATA0, "data0") ||
  85. gpio_request(GPIO_DATA1, "data1") ||
  86. gpio_request(GPIO_DATA2, "data2") ||
  87. gpio_request(GPIO_DATA3, "data3") ||
  88. gpio_request(GPIO_DATA4, "data4") ||
  89. gpio_request(GPIO_DATA5, "data5") ||
  90. gpio_request(GPIO_DATA6, "data6") ||
  91. gpio_request(GPIO_DATA7, "data7")) {
  92. printf("%s: error requesting GPIOs.", __func__);
  93. return FPGA_FAIL;
  94. }
  95. /* set up outputs */
  96. gpio_direction_output(GPIO_DCLK, 0);
  97. gpio_direction_output(GPIO_nCONFIG, 0);
  98. gpio_direction_output(GPIO_DATA0, 0);
  99. gpio_direction_output(GPIO_DATA1, 0);
  100. gpio_direction_output(GPIO_DATA2, 0);
  101. gpio_direction_output(GPIO_DATA3, 0);
  102. gpio_direction_output(GPIO_DATA4, 0);
  103. gpio_direction_output(GPIO_DATA5, 0);
  104. gpio_direction_output(GPIO_DATA6, 0);
  105. gpio_direction_output(GPIO_DATA7, 0);
  106. /* NB omap_free_gpio() resets to an input, so we can't
  107. * free ie. nCONFIG, or else the FPGA would reset
  108. * Q: presumably gpio_free() has the same effect?
  109. */
  110. /* set up inputs */
  111. gpio_direction_input(GPIO_nSTATUS);
  112. #ifndef CONFIG_SYS_FPGA_DONT_USE_CONF_DONE
  113. gpio_direction_input(GPIO_CONF_DONE);
  114. #endif
  115. fpga_config_fn(0, 1, 0);
  116. udelay(60);
  117. return FPGA_SUCCESS;
  118. }
  119. int fpga_null_fn(int cookie)
  120. {
  121. return 0;
  122. }
  123. int fpga_config_fn(int assert, int flush, int cookie)
  124. {
  125. fpga_debug("SET config : %s=%d\n", assert ? "low" : "high", assert);
  126. if (flush) {
  127. gpio_set_value(GPIO_nCONFIG, !assert);
  128. udelay(1);
  129. gpio_set_value(GPIO_nCONFIG, assert);
  130. }
  131. return assert;
  132. }
  133. int fpga_done_fn(int cookie)
  134. {
  135. int result = 0;
  136. /* since revA of BLX, we will not get this signal. */
  137. udelay(10);
  138. #ifdef CONFIG_SYS_FPGA_DONT_USE_CONF_DONE
  139. fpga_debug("not waiting for CONF_DONE.");
  140. result = 1;
  141. #else
  142. fpga_debug("CONF_DONE check ... ");
  143. if (gpio_get_value(GPIO_CONF_DONE)) {
  144. fpga_debug("high\n");
  145. result = 1;
  146. } else
  147. fpga_debug("low\n");
  148. gpio_free(GPIO_CONF_DONE);
  149. #endif
  150. return result;
  151. }
  152. int fpga_status_fn(int cookie)
  153. {
  154. int result = 0;
  155. fpga_debug("STATUS check ... ");
  156. result = gpio_get_value(GPIO_nSTATUS);
  157. if (result < 0)
  158. fpga_debug("error\n");
  159. else if (result > 0)
  160. fpga_debug("high\n");
  161. else
  162. fpga_debug("low\n");
  163. return result;
  164. }
  165. static inline int _write_fpga(u8 byte)
  166. {
  167. gpio_set_value(GPIO_DATA0, byte & 0x01);
  168. gpio_set_value(GPIO_DATA1, (byte >> 1) & 0x01);
  169. gpio_set_value(GPIO_DATA2, (byte >> 2) & 0x01);
  170. gpio_set_value(GPIO_DATA3, (byte >> 3) & 0x01);
  171. gpio_set_value(GPIO_DATA4, (byte >> 4) & 0x01);
  172. gpio_set_value(GPIO_DATA5, (byte >> 5) & 0x01);
  173. gpio_set_value(GPIO_DATA6, (byte >> 6) & 0x01);
  174. gpio_set_value(GPIO_DATA7, (byte >> 7) & 0x01);
  175. /* clock */
  176. gpio_set_value(GPIO_DCLK, 1);
  177. udelay(1);
  178. gpio_set_value(GPIO_DCLK, 0);
  179. udelay(1);
  180. return 0;
  181. }
  182. int fpga_wr_fn(const void *buf, size_t len, int flush, int cookie)
  183. {
  184. unsigned char *data = (unsigned char *) buf;
  185. int i;
  186. int headerlen = len - cyclone2.size;
  187. if (headerlen < 0)
  188. return FPGA_FAIL;
  189. else if (headerlen == sizeof(uint32_t)) {
  190. const unsigned int fpgavers_len = 11; /* '0x' + 8 hex digits + \0 */
  191. char fpgavers_str[fpgavers_len];
  192. snprintf(fpgavers_str, fpgavers_len, "0x%08x",
  193. be32_to_cpup((uint32_t*)data));
  194. setenv("fpgavers", fpgavers_str);
  195. }
  196. fpga_debug("fpga_wr: buf %p / size %d\n", buf, len);
  197. for (i = headerlen; i < len; i++)
  198. _write_fpga(data[i]);
  199. fpga_debug("-%s\n", __func__);
  200. return FPGA_SUCCESS;
  201. }