fpga.c 8.6 KB

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