fpga.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * (C) Copyright 2007
  3. * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/io.h>
  25. #include <spartan2.h>
  26. #include <spartan3.h>
  27. #include <command.h>
  28. #include "fpga.h"
  29. #include "pmc440.h"
  30. DECLARE_GLOBAL_DATA_PTR;
  31. #if defined(CONFIG_FPGA)
  32. #define USE_SP_CODE
  33. #ifdef USE_SP_CODE
  34. Xilinx_Spartan3_Slave_Parallel_fns pmc440_fpga_fns = {
  35. fpga_pre_config_fn,
  36. fpga_pgm_fn,
  37. fpga_init_fn,
  38. NULL, /* err */
  39. fpga_done_fn,
  40. fpga_clk_fn,
  41. fpga_cs_fn,
  42. fpga_wr_fn,
  43. NULL, /* rdata */
  44. fpga_wdata_fn,
  45. fpga_busy_fn,
  46. fpga_abort_fn,
  47. fpga_post_config_fn,
  48. };
  49. #else
  50. Xilinx_Spartan3_Slave_Serial_fns pmc440_fpga_fns = {
  51. fpga_pre_config_fn,
  52. fpga_pgm_fn,
  53. fpga_clk_fn,
  54. fpga_init_fn,
  55. fpga_done_fn,
  56. fpga_wr_fn,
  57. fpga_post_config_fn,
  58. };
  59. #endif
  60. Xilinx_Spartan2_Slave_Serial_fns ngcc_fpga_fns = {
  61. ngcc_fpga_pre_config_fn,
  62. ngcc_fpga_pgm_fn,
  63. ngcc_fpga_clk_fn,
  64. ngcc_fpga_init_fn,
  65. ngcc_fpga_done_fn,
  66. ngcc_fpga_wr_fn,
  67. ngcc_fpga_post_config_fn
  68. };
  69. Xilinx_desc fpga[CONFIG_FPGA_COUNT] = {
  70. XILINX_XC3S1200E_DESC(
  71. #ifdef USE_SP_CODE
  72. slave_parallel,
  73. #else
  74. slave_serial,
  75. #endif
  76. (void *)&pmc440_fpga_fns,
  77. 0),
  78. XILINX_XC2S200_DESC(
  79. slave_serial,
  80. (void *)&ngcc_fpga_fns,
  81. 0)
  82. };
  83. /*
  84. * Set the active-low FPGA reset signal.
  85. */
  86. void fpga_reset(int assert)
  87. {
  88. debug("%s:%d: RESET ", __FUNCTION__, __LINE__);
  89. if (assert) {
  90. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) & ~GPIO1_FPGA_DATA);
  91. debug("asserted\n");
  92. } else {
  93. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) | GPIO1_FPGA_DATA);
  94. debug("deasserted\n");
  95. }
  96. }
  97. /*
  98. * Initialize the SelectMap interface. We assume that the mode and the
  99. * initial state of all of the port pins have already been set!
  100. */
  101. void fpga_serialslave_init(void)
  102. {
  103. debug("%s:%d: Initialize serial slave interface\n", __FUNCTION__,
  104. __LINE__);
  105. fpga_pgm_fn(FALSE, FALSE, 0); /* make sure program pin is inactive */
  106. }
  107. /*
  108. * Set the FPGA's active-low SelectMap program line to the specified level
  109. */
  110. int fpga_pgm_fn(int assert, int flush, int cookie)
  111. {
  112. debug("%s:%d: FPGA PROGRAM ",
  113. __FUNCTION__, __LINE__);
  114. if (assert) {
  115. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) & ~GPIO1_FPGA_PRG);
  116. debug("asserted\n");
  117. } else {
  118. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) | GPIO1_FPGA_PRG);
  119. debug("deasserted\n");
  120. }
  121. return assert;
  122. }
  123. /*
  124. * Test the state of the active-low FPGA INIT line. Return 1 on INIT
  125. * asserted (low).
  126. */
  127. int fpga_init_fn(int cookie)
  128. {
  129. if (in_be32((void*)GPIO1_IR) & GPIO1_FPGA_INIT)
  130. return 0;
  131. else
  132. return 1;
  133. }
  134. #ifdef USE_SP_CODE
  135. int fpga_abort_fn(int cookie)
  136. {
  137. return 0;
  138. }
  139. int fpga_cs_fn(int assert_cs, int flush, int cookie)
  140. {
  141. return assert_cs;
  142. }
  143. int fpga_busy_fn(int cookie)
  144. {
  145. return 1;
  146. }
  147. #endif
  148. /*
  149. * Test the state of the active-high FPGA DONE pin
  150. */
  151. int fpga_done_fn(int cookie)
  152. {
  153. if (in_be32((void*)GPIO1_IR) & GPIO1_FPGA_DONE)
  154. return 1;
  155. else
  156. return 0;
  157. }
  158. /*
  159. * FPGA pre-configuration function. Just make sure that
  160. * FPGA reset is asserted to keep the FPGA from starting up after
  161. * configuration.
  162. */
  163. int fpga_pre_config_fn(int cookie)
  164. {
  165. debug("%s:%d: FPGA pre-configuration\n", __FUNCTION__, __LINE__);
  166. fpga_reset(TRUE);
  167. /* release init# */
  168. out_be32((void*)GPIO0_OR, in_be32((void*)GPIO0_OR) | GPIO0_FPGA_FORCEINIT);
  169. /* disable PLD IOs */
  170. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) | GPIO1_IOEN_N);
  171. return 0;
  172. }
  173. /*
  174. * FPGA post configuration function. Blip the FPGA reset line and then see if
  175. * the FPGA appears to be running.
  176. */
  177. int fpga_post_config_fn(int cookie)
  178. {
  179. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  180. int rc=0;
  181. char *s;
  182. debug("%s:%d: FPGA post configuration\n", __FUNCTION__, __LINE__);
  183. /* enable PLD0..7 pins */
  184. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) & ~GPIO1_IOEN_N);
  185. fpga_reset(TRUE);
  186. udelay (100);
  187. fpga_reset(FALSE);
  188. udelay (100);
  189. FPGA_OUT32(&fpga->status, (gd->board_type << STATUS_HWREV_SHIFT) & STATUS_HWREV_MASK);
  190. /* NGCC only: enable ledlink */
  191. if ((s = getenv("bd_type")) && !strcmp(s, "ngcc"))
  192. FPGA_SETBITS(&fpga->ctrla, 0x29f8c000);
  193. return rc;
  194. }
  195. int fpga_clk_fn(int assert_clk, int flush, int cookie)
  196. {
  197. if (assert_clk)
  198. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) | GPIO1_FPGA_CLK);
  199. else
  200. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) & ~GPIO1_FPGA_CLK);
  201. return assert_clk;
  202. }
  203. int fpga_wr_fn(int assert_write, int flush, int cookie)
  204. {
  205. if (assert_write)
  206. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) | GPIO1_FPGA_DATA);
  207. else
  208. out_be32((void*)GPIO1_OR, in_be32((void*)GPIO1_OR) & ~GPIO1_FPGA_DATA);
  209. return assert_write;
  210. }
  211. #ifdef USE_SP_CODE
  212. int fpga_wdata_fn(uchar data, int flush, int cookie)
  213. {
  214. uchar val = data;
  215. ulong or = in_be32((void*)GPIO1_OR);
  216. int i = 7;
  217. do {
  218. /* Write data */
  219. if (val & 0x80)
  220. or = (or & ~GPIO1_FPGA_CLK) | GPIO1_FPGA_DATA;
  221. else
  222. or = or & ~(GPIO1_FPGA_CLK | GPIO1_FPGA_DATA);
  223. out_be32((void*)GPIO1_OR, or);
  224. /* Assert the clock */
  225. or |= GPIO1_FPGA_CLK;
  226. out_be32((void*)GPIO1_OR, or);
  227. val <<= 1;
  228. i --;
  229. } while (i > 0);
  230. /* Write last data bit (the 8th clock comes from the sp_load() code */
  231. if (val & 0x80)
  232. or = (or & ~GPIO1_FPGA_CLK) | GPIO1_FPGA_DATA;
  233. else
  234. or = or & ~(GPIO1_FPGA_CLK | GPIO1_FPGA_DATA);
  235. out_be32((void*)GPIO1_OR, or);
  236. return 0;
  237. }
  238. #endif
  239. #define NGCC_FPGA_PRG CLOCK_EN
  240. #define NGCC_FPGA_DATA RESET_OUT
  241. #define NGCC_FPGA_DONE CLOCK_IN
  242. #define NGCC_FPGA_INIT IRIGB_R_IN
  243. #define NGCC_FPGA_CLK CLOCK_OUT
  244. void ngcc_fpga_serialslave_init(void)
  245. {
  246. debug("%s:%d: Initialize serial slave interface\n",
  247. __FUNCTION__, __LINE__);
  248. /* make sure program pin is inactive */
  249. ngcc_fpga_pgm_fn (FALSE, FALSE, 0);
  250. }
  251. /*
  252. * Set the active-low FPGA reset signal.
  253. */
  254. void ngcc_fpga_reset(int assert)
  255. {
  256. debug("%s:%d: RESET ", __FUNCTION__, __LINE__);
  257. if (assert) {
  258. FPGA_CLRBITS(NGCC_CTRL_BASE, NGCC_CTRL_FPGARST_N);
  259. debug("asserted\n");
  260. } else {
  261. FPGA_SETBITS(NGCC_CTRL_BASE, NGCC_CTRL_FPGARST_N);
  262. debug("deasserted\n");
  263. }
  264. }
  265. /*
  266. * Set the FPGA's active-low SelectMap program line to the specified level
  267. */
  268. int ngcc_fpga_pgm_fn(int assert, int flush, int cookie)
  269. {
  270. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  271. debug("%s:%d: FPGA PROGRAM ", __FUNCTION__, __LINE__);
  272. if (assert) {
  273. FPGA_CLRBITS(&fpga->ctrla, NGCC_FPGA_PRG);
  274. debug("asserted\n");
  275. } else {
  276. FPGA_SETBITS(&fpga->ctrla, NGCC_FPGA_PRG);
  277. debug("deasserted\n");
  278. }
  279. return assert;
  280. }
  281. /*
  282. * Test the state of the active-low FPGA INIT line. Return 1 on INIT
  283. * asserted (low).
  284. */
  285. int ngcc_fpga_init_fn(int cookie)
  286. {
  287. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  288. debug("%s:%d: INIT check... ", __FUNCTION__, __LINE__);
  289. if (FPGA_IN32(&fpga->status) & NGCC_FPGA_INIT) {
  290. debug("high\n");
  291. return 0;
  292. } else {
  293. debug("low\n");
  294. return 1;
  295. }
  296. }
  297. /*
  298. * Test the state of the active-high FPGA DONE pin
  299. */
  300. int ngcc_fpga_done_fn(int cookie)
  301. {
  302. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  303. debug("%s:%d: DONE check... ", __FUNCTION__, __LINE__);
  304. if (FPGA_IN32(&fpga->status) & NGCC_FPGA_DONE) {
  305. debug("DONE high\n");
  306. return 1;
  307. } else {
  308. debug("low\n");
  309. return 0;
  310. }
  311. }
  312. /*
  313. * FPGA pre-configuration function.
  314. */
  315. int ngcc_fpga_pre_config_fn(int cookie)
  316. {
  317. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  318. debug("%s:%d: FPGA pre-configuration\n", __FUNCTION__, __LINE__);
  319. ngcc_fpga_reset(TRUE);
  320. FPGA_CLRBITS(&fpga->ctrla, 0xfffffe00);
  321. ngcc_fpga_reset(TRUE);
  322. return 0;
  323. }
  324. /*
  325. * FPGA post configuration function. Blip the FPGA reset line and then see if
  326. * the FPGA appears to be running.
  327. */
  328. int ngcc_fpga_post_config_fn(int cookie)
  329. {
  330. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  331. debug("%s:%d: NGCC FPGA post configuration\n", __FUNCTION__, __LINE__);
  332. udelay (100);
  333. ngcc_fpga_reset(FALSE);
  334. FPGA_SETBITS(&fpga->ctrla, 0x29f8c000);
  335. return 0;
  336. }
  337. int ngcc_fpga_clk_fn(int assert_clk, int flush, int cookie)
  338. {
  339. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  340. if (assert_clk)
  341. FPGA_SETBITS(&fpga->ctrla, NGCC_FPGA_CLK);
  342. else
  343. FPGA_CLRBITS(&fpga->ctrla, NGCC_FPGA_CLK);
  344. return assert_clk;
  345. }
  346. int ngcc_fpga_wr_fn(int assert_write, int flush, int cookie)
  347. {
  348. pmc440_fpga_t *fpga = (pmc440_fpga_t *)FPGA_BA;
  349. if (assert_write)
  350. FPGA_SETBITS(&fpga->ctrla, NGCC_FPGA_DATA);
  351. else
  352. FPGA_CLRBITS(&fpga->ctrla, NGCC_FPGA_DATA);
  353. return assert_write;
  354. }
  355. /*
  356. * Initialize the fpga. Return 1 on success, 0 on failure.
  357. */
  358. int pmc440_init_fpga(void)
  359. {
  360. char *s;
  361. debug("%s:%d: Initialize FPGA interface (relocation offset = 0x%.8lx)\n",
  362. __FUNCTION__, __LINE__, gd->reloc_off);
  363. fpga_init(gd->reloc_off);
  364. fpga_serialslave_init ();
  365. debug("%s:%d: Adding fpga 0\n", __FUNCTION__, __LINE__);
  366. fpga_add (fpga_xilinx, &fpga[0]);
  367. /* NGCC only */
  368. if ((s = getenv("bd_type")) && !strcmp(s, "ngcc")) {
  369. ngcc_fpga_serialslave_init ();
  370. debug("%s:%d: Adding fpga 1\n", __FUNCTION__, __LINE__);
  371. fpga_add (fpga_xilinx, &fpga[1]);
  372. }
  373. return 0;
  374. }
  375. #endif /* CONFIG_FPGA */