fpga.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. * Keith Outwater, keith_outwater@mvis.com.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*
  9. * Virtex2 FPGA configuration support for the GEN860T computer
  10. */
  11. #include <common.h>
  12. #include <virtex2.h>
  13. #include <command.h>
  14. #include "fpga.h"
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #if defined(CONFIG_FPGA)
  17. #if 0
  18. #define GEN860T_FPGA_DEBUG
  19. #endif
  20. #ifdef GEN860T_FPGA_DEBUG
  21. #define PRINTF(fmt,args...) printf (fmt ,##args)
  22. #else
  23. #define PRINTF(fmt,args...)
  24. #endif
  25. /*
  26. * Port bit numbers for the Selectmap controls
  27. */
  28. #define FPGA_INIT_BIT_NUM 22 /* PB22 */
  29. #define FPGA_RESET_BIT_NUM 11 /* PC11 */
  30. #define FPGA_DONE_BIT_NUM 16 /* PB16 */
  31. #define FPGA_PROGRAM_BIT_NUM 7 /* PA7 */
  32. /* Note that these are pointers to code that is in Flash. They will be
  33. * relocated at runtime.
  34. */
  35. xilinx_virtex2_slave_selectmap_fns fpga_fns = {
  36. fpga_pre_config_fn,
  37. fpga_pgm_fn,
  38. fpga_init_fn,
  39. fpga_err_fn,
  40. fpga_done_fn,
  41. fpga_clk_fn,
  42. fpga_cs_fn,
  43. fpga_wr_fn,
  44. fpga_read_data_fn,
  45. fpga_write_data_fn,
  46. fpga_busy_fn,
  47. fpga_abort_fn,
  48. fpga_post_config_fn
  49. };
  50. xilinx_desc fpga[CONFIG_FPGA_COUNT] = {
  51. {xilinx_virtex2,
  52. slave_selectmap,
  53. XILINX_XC2V3000_SIZE,
  54. (void *) &fpga_fns,
  55. 0}
  56. };
  57. /*
  58. * Display FPGA revision information
  59. */
  60. void print_fpga_revision (void)
  61. {
  62. vu_long *rev_p = (vu_long *) 0x60000008;
  63. printf ("FPGA Revision 0x%.8lx"
  64. " (Date %.2lx/%.2lx/%.2lx, Status \"%.1lx\", Version %.3lu)\n",
  65. *rev_p,
  66. ((*rev_p >> 28) & 0xf),
  67. ((*rev_p >> 20) & 0xff),
  68. ((*rev_p >> 12) & 0xff),
  69. ((*rev_p >> 8) & 0xf), (*rev_p & 0xff));
  70. }
  71. /*
  72. * Perform a simple test of the FPGA to processor interface using the FPGA's
  73. * inverting bus test register. The great thing about doing a read/write
  74. * test on a register that inverts it's contents is that you avoid any
  75. * problems with bus charging.
  76. * Return 0 on failure, 1 on success.
  77. */
  78. int test_fpga_ibtr (void)
  79. {
  80. vu_long *ibtr_p = (vu_long *) 0x60000010;
  81. vu_long readback;
  82. vu_long compare;
  83. int i;
  84. int j;
  85. int k;
  86. int pass = 1;
  87. static const ulong bitpattern[] = {
  88. 0xdeadbeef, /* magic ID pattern for debug */
  89. 0x00000001, /* single bit */
  90. 0x00000003, /* two adjacent bits */
  91. 0x00000007, /* three adjacent bits */
  92. 0x0000000F, /* four adjacent bits */
  93. 0x00000005, /* two non-adjacent bits */
  94. 0x00000015, /* three non-adjacent bits */
  95. 0x00000055, /* four non-adjacent bits */
  96. 0xaaaaaaaa, /* alternating 1/0 */
  97. };
  98. for (i = 0; i < 1024; i++) {
  99. for (j = 0; j < 31; j++) {
  100. for (k = 0;
  101. k < sizeof (bitpattern) / sizeof (bitpattern[0]);
  102. k++) {
  103. *ibtr_p = compare = (bitpattern[k] << j);
  104. readback = *ibtr_p;
  105. if (readback != ~compare) {
  106. printf ("%s:%d: FPGA test fail: expected 0x%.8lx" " actual 0x%.8lx\n", __FUNCTION__, __LINE__, ~compare, readback);
  107. pass = 0;
  108. break;
  109. }
  110. }
  111. if (!pass)
  112. break;
  113. }
  114. if (!pass)
  115. break;
  116. }
  117. if (pass) {
  118. printf ("FPGA inverting bus test passed\n");
  119. print_fpga_revision ();
  120. } else {
  121. printf ("** FPGA inverting bus test failed\n");
  122. }
  123. return pass;
  124. }
  125. /*
  126. * Set the active-low FPGA reset signal.
  127. */
  128. void fpga_reset (int assert)
  129. {
  130. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  131. PRINTF ("%s:%d: RESET ", __FUNCTION__, __LINE__);
  132. if (assert) {
  133. immap->im_ioport.iop_pcdat &= ~(0x8000 >> FPGA_RESET_BIT_NUM);
  134. PRINTF ("asserted\n");
  135. } else {
  136. immap->im_ioport.iop_pcdat |= (0x8000 >> FPGA_RESET_BIT_NUM);
  137. PRINTF ("deasserted\n");
  138. }
  139. }
  140. /*
  141. * Initialize the SelectMap interface. We assume that the mode and the
  142. * initial state of all of the port pins have already been set!
  143. */
  144. void fpga_selectmap_init (void)
  145. {
  146. PRINTF ("%s:%d: Initialize SelectMap interface\n", __FUNCTION__,
  147. __LINE__);
  148. fpga_pgm_fn(false, false, 0); /* make sure program pin is inactive */
  149. }
  150. /*
  151. * Initialize the fpga. Return 1 on success, 0 on failure.
  152. */
  153. int gen860t_init_fpga (void)
  154. {
  155. int i;
  156. PRINTF ("%s:%d: Initialize FPGA interface\n",
  157. __FUNCTION__, __LINE__);
  158. fpga_init ();
  159. fpga_selectmap_init ();
  160. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  161. PRINTF ("%s:%d: Adding fpga %d\n", __FUNCTION__, __LINE__, i);
  162. fpga_add (fpga_xilinx, &fpga[i]);
  163. }
  164. return 1;
  165. }
  166. /*
  167. * Set the FPGA's active-low SelectMap program line to the specified level
  168. */
  169. int fpga_pgm_fn (int assert, int flush, int cookie)
  170. {
  171. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  172. PRINTF ("%s:%d: FPGA PROGRAM ", __FUNCTION__, __LINE__);
  173. if (assert) {
  174. immap->im_ioport.iop_padat &=
  175. ~(0x8000 >> FPGA_PROGRAM_BIT_NUM);
  176. PRINTF ("asserted\n");
  177. } else {
  178. immap->im_ioport.iop_padat |=
  179. (0x8000 >> FPGA_PROGRAM_BIT_NUM);
  180. PRINTF ("deasserted\n");
  181. }
  182. return assert;
  183. }
  184. /*
  185. * Test the state of the active-low FPGA INIT line. Return 1 on INIT
  186. * asserted (low).
  187. */
  188. int fpga_init_fn (int cookie)
  189. {
  190. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  191. PRINTF ("%s:%d: INIT check... ", __FUNCTION__, __LINE__);
  192. if (immap->im_cpm.cp_pbdat & (0x80000000 >> FPGA_INIT_BIT_NUM)) {
  193. PRINTF ("high\n");
  194. return 0;
  195. } else {
  196. PRINTF ("low\n");
  197. return 1;
  198. }
  199. }
  200. /*
  201. * Test the state of the active-high FPGA DONE pin
  202. */
  203. int fpga_done_fn (int cookie)
  204. {
  205. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  206. PRINTF ("%s:%d: DONE check... ", __FUNCTION__, __LINE__);
  207. if (immap->im_cpm.cp_pbdat & (0x80000000 >> FPGA_DONE_BIT_NUM)) {
  208. PRINTF ("high\n");
  209. return FPGA_SUCCESS;
  210. } else {
  211. PRINTF ("low\n");
  212. return FPGA_FAIL;
  213. }
  214. }
  215. /*
  216. * Read FPGA SelectMap data.
  217. */
  218. int fpga_read_data_fn (unsigned char *data, int cookie)
  219. {
  220. vu_char *p = (vu_char *) SELECTMAP_BASE;
  221. *data = *p;
  222. #if 0
  223. PRINTF ("%s: Read 0x%x into 0x%p\n", __FUNCTION__, (int) data, data);
  224. #endif
  225. return (int) data;
  226. }
  227. /*
  228. * Write data to the FPGA SelectMap port
  229. */
  230. int fpga_write_data_fn (unsigned char data, int flush, int cookie)
  231. {
  232. vu_char *p = (vu_char *) SELECTMAP_BASE;
  233. #if 0
  234. PRINTF ("%s: Write Data 0x%x\n", __FUNCTION__, (int) data);
  235. #endif
  236. *p = data;
  237. return (int) data;
  238. }
  239. /*
  240. * Abort and FPGA operation
  241. */
  242. int fpga_abort_fn (int cookie)
  243. {
  244. PRINTF ("%s:%d: FPGA program sequence aborted\n",
  245. __FUNCTION__, __LINE__);
  246. return FPGA_FAIL;
  247. }
  248. /*
  249. * FPGA pre-configuration function. Just make sure that
  250. * FPGA reset is asserted to keep the FPGA from starting up after
  251. * configuration.
  252. */
  253. int fpga_pre_config_fn (int cookie)
  254. {
  255. PRINTF ("%s:%d: FPGA pre-configuration\n", __FUNCTION__, __LINE__);
  256. fpga_reset(true);
  257. return 0;
  258. }
  259. /*
  260. * FPGA post configuration function. Blip the FPGA reset line and then see if
  261. * the FPGA appears to be running.
  262. */
  263. int fpga_post_config_fn (int cookie)
  264. {
  265. int rc;
  266. PRINTF ("%s:%d: FPGA post configuration\n", __FUNCTION__, __LINE__);
  267. fpga_reset(true);
  268. udelay (1000);
  269. fpga_reset(false);
  270. udelay (1000);
  271. /*
  272. * Use the FPGA,s inverting bus test register to do a simple test of the
  273. * processor interface.
  274. */
  275. rc = test_fpga_ibtr ();
  276. return rc;
  277. }
  278. /*
  279. * Clock, chip select and write signal assert functions and error check
  280. * and busy functions. These are only stubs because the GEN860T selectmap
  281. * interface handles sequencing of control signals automatically (it uses
  282. * a memory-mapped interface to the FPGA SelectMap port). The design of
  283. * the interface guarantees that the SelectMap port cannot be overrun so
  284. * no busy check is needed. A configuration error is signalled by INIT
  285. * going low during configuration, so there is no need for a separate error
  286. * function.
  287. */
  288. int fpga_clk_fn (int assert_clk, int flush, int cookie)
  289. {
  290. return assert_clk;
  291. }
  292. int fpga_cs_fn (int assert_cs, int flush, int cookie)
  293. {
  294. return assert_cs;
  295. }
  296. int fpga_wr_fn (int assert_write, int flush, int cookie)
  297. {
  298. return assert_write;
  299. }
  300. int fpga_err_fn (int cookie)
  301. {
  302. return 0;
  303. }
  304. int fpga_busy_fn (int cookie)
  305. {
  306. return 0;
  307. }
  308. #endif