fpga.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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\n",
  174. __FUNCTION__, __LINE__);
  175. fpga_init ();
  176. fpga_selectmap_init ();
  177. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  178. PRINTF ("%s:%d: Adding fpga %d\n", __FUNCTION__, __LINE__, i);
  179. fpga_add (fpga_xilinx, &fpga[i]);
  180. }
  181. return 1;
  182. }
  183. /*
  184. * Set the FPGA's active-low SelectMap program line to the specified level
  185. */
  186. int fpga_pgm_fn (int assert, int flush, int cookie)
  187. {
  188. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  189. PRINTF ("%s:%d: FPGA PROGRAM ", __FUNCTION__, __LINE__);
  190. if (assert) {
  191. immap->im_ioport.iop_padat &=
  192. ~(0x8000 >> FPGA_PROGRAM_BIT_NUM);
  193. PRINTF ("asserted\n");
  194. } else {
  195. immap->im_ioport.iop_padat |=
  196. (0x8000 >> FPGA_PROGRAM_BIT_NUM);
  197. PRINTF ("deasserted\n");
  198. }
  199. return assert;
  200. }
  201. /*
  202. * Test the state of the active-low FPGA INIT line. Return 1 on INIT
  203. * asserted (low).
  204. */
  205. int fpga_init_fn (int cookie)
  206. {
  207. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  208. PRINTF ("%s:%d: INIT check... ", __FUNCTION__, __LINE__);
  209. if (immap->im_cpm.cp_pbdat & (0x80000000 >> FPGA_INIT_BIT_NUM)) {
  210. PRINTF ("high\n");
  211. return 0;
  212. } else {
  213. PRINTF ("low\n");
  214. return 1;
  215. }
  216. }
  217. /*
  218. * Test the state of the active-high FPGA DONE pin
  219. */
  220. int fpga_done_fn (int cookie)
  221. {
  222. volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
  223. PRINTF ("%s:%d: DONE check... ", __FUNCTION__, __LINE__);
  224. if (immap->im_cpm.cp_pbdat & (0x80000000 >> FPGA_DONE_BIT_NUM)) {
  225. PRINTF ("high\n");
  226. return FPGA_SUCCESS;
  227. } else {
  228. PRINTF ("low\n");
  229. return FPGA_FAIL;
  230. }
  231. }
  232. /*
  233. * Read FPGA SelectMap data.
  234. */
  235. int fpga_read_data_fn (unsigned char *data, int cookie)
  236. {
  237. vu_char *p = (vu_char *) SELECTMAP_BASE;
  238. *data = *p;
  239. #if 0
  240. PRINTF ("%s: Read 0x%x into 0x%p\n", __FUNCTION__, (int) data, data);
  241. #endif
  242. return (int) data;
  243. }
  244. /*
  245. * Write data to the FPGA SelectMap port
  246. */
  247. int fpga_write_data_fn (unsigned char data, int flush, int cookie)
  248. {
  249. vu_char *p = (vu_char *) SELECTMAP_BASE;
  250. #if 0
  251. PRINTF ("%s: Write Data 0x%x\n", __FUNCTION__, (int) data);
  252. #endif
  253. *p = data;
  254. return (int) data;
  255. }
  256. /*
  257. * Abort and FPGA operation
  258. */
  259. int fpga_abort_fn (int cookie)
  260. {
  261. PRINTF ("%s:%d: FPGA program sequence aborted\n",
  262. __FUNCTION__, __LINE__);
  263. return FPGA_FAIL;
  264. }
  265. /*
  266. * FPGA pre-configuration function. Just make sure that
  267. * FPGA reset is asserted to keep the FPGA from starting up after
  268. * configuration.
  269. */
  270. int fpga_pre_config_fn (int cookie)
  271. {
  272. PRINTF ("%s:%d: FPGA pre-configuration\n", __FUNCTION__, __LINE__);
  273. fpga_reset (TRUE);
  274. return 0;
  275. }
  276. /*
  277. * FPGA post configuration function. Blip the FPGA reset line and then see if
  278. * the FPGA appears to be running.
  279. */
  280. int fpga_post_config_fn (int cookie)
  281. {
  282. int rc;
  283. PRINTF ("%s:%d: FPGA post configuration\n", __FUNCTION__, __LINE__);
  284. fpga_reset (TRUE);
  285. udelay (1000);
  286. fpga_reset (FALSE);
  287. udelay (1000);
  288. /*
  289. * Use the FPGA,s inverting bus test register to do a simple test of the
  290. * processor interface.
  291. */
  292. rc = test_fpga_ibtr ();
  293. return rc;
  294. }
  295. /*
  296. * Clock, chip select and write signal assert functions and error check
  297. * and busy functions. These are only stubs because the GEN860T selectmap
  298. * interface handles sequencing of control signals automatically (it uses
  299. * a memory-mapped interface to the FPGA SelectMap port). The design of
  300. * the interface guarantees that the SelectMap port cannot be overrun so
  301. * no busy check is needed. A configuration error is signalled by INIT
  302. * going low during configuration, so there is no need for a separate error
  303. * function.
  304. */
  305. int fpga_clk_fn (int assert_clk, int flush, int cookie)
  306. {
  307. return assert_clk;
  308. }
  309. int fpga_cs_fn (int assert_cs, int flush, int cookie)
  310. {
  311. return assert_cs;
  312. }
  313. int fpga_wr_fn (int assert_write, int flush, int cookie)
  314. {
  315. return assert_write;
  316. }
  317. int fpga_err_fn (int cookie)
  318. {
  319. return 0;
  320. }
  321. int fpga_busy_fn (int cookie)
  322. {
  323. return 0;
  324. }
  325. #endif