fpga.c 4.9 KB

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