fpga.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * (C) Copyright 2006
  3. * Wolfgang Wegner, ASTRO Strobel Kommunikationssysteme GmbH,
  4. * w.wegner@astro-kom.de
  5. *
  6. * based on the files by
  7. * Heiko Schocher, DENX Software Engineering, hs@denx.de
  8. * and
  9. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  10. * Keith Outwater, keith_outwater@mvis.com.
  11. *
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. *
  30. */
  31. /* Altera/Xilinx FPGA configuration support for the ASTRO "URMEL" board */
  32. #include <common.h>
  33. #include <watchdog.h>
  34. #include <altera.h>
  35. #include <ACEX1K.h>
  36. #include <spartan3.h>
  37. #include <command.h>
  38. #include <asm/immap_5329.h>
  39. #include <asm/io.h>
  40. #include "fpga.h"
  41. DECLARE_GLOBAL_DATA_PTR;
  42. int altera_pre_fn(int cookie)
  43. {
  44. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  45. unsigned char tmp_char;
  46. unsigned short tmp_short;
  47. /* first, set the required pins to GPIO function */
  48. /* PAR_T0IN -> GPIO */
  49. tmp_char = readb(&gpiop->par_timer);
  50. tmp_char &= 0xfc;
  51. writeb(tmp_char, &gpiop->par_timer);
  52. /* all QSPI pins -> GPIO */
  53. writew(0x0000, &gpiop->par_qspi);
  54. /* U0RTS, U0CTS -> GPIO */
  55. tmp_short = __raw_readw(&gpiop->par_uart);
  56. tmp_short &= 0xfff3;
  57. __raw_writew(tmp_short, &gpiop->par_uart);
  58. /* all PWM pins -> GPIO */
  59. writeb(0x00, &gpiop->par_pwm);
  60. /* next, set data direction registers */
  61. writeb(0x01, &gpiop->pddr_timer);
  62. writeb(0x25, &gpiop->pddr_qspi);
  63. writeb(0x0c, &gpiop->pddr_uart);
  64. writeb(0x04, &gpiop->pddr_pwm);
  65. /* ensure other SPI peripherals are deselected */
  66. writeb(0x08, &gpiop->ppd_uart);
  67. writeb(0x38, &gpiop->ppd_qspi);
  68. /* CONFIG = 0 STATUS = 0 -> FPGA in reset state */
  69. writeb(0xFB, &gpiop->pclrr_uart);
  70. /* enable Altera configuration by clearing QSPI_CS2 and DT0IN */
  71. writeb(0xFE, &gpiop->pclrr_timer);
  72. writeb(0xDF, &gpiop->pclrr_qspi);
  73. return FPGA_SUCCESS;
  74. }
  75. /* Set the state of CONFIG Pin */
  76. int altera_config_fn(int assert_config, int flush, int cookie)
  77. {
  78. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  79. if (assert_config)
  80. writeb(0x04, &gpiop->ppd_uart);
  81. else
  82. writeb(0xFB, &gpiop->pclrr_uart);
  83. return FPGA_SUCCESS;
  84. }
  85. /* Returns the state of STATUS Pin */
  86. int altera_status_fn(int cookie)
  87. {
  88. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  89. if (readb(&gpiop->ppd_pwm) & 0x08)
  90. return FPGA_FAIL;
  91. return FPGA_SUCCESS;
  92. }
  93. /* Returns the state of CONF_DONE Pin */
  94. int altera_done_fn(int cookie)
  95. {
  96. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  97. if (readb(&gpiop->ppd_pwm) & 0x20)
  98. return FPGA_FAIL;
  99. return FPGA_SUCCESS;
  100. }
  101. /*
  102. * writes the complete buffer to the FPGA
  103. * writing the complete buffer in one function is much faster,
  104. * then calling it for every bit
  105. */
  106. int altera_write_fn(void *buf, size_t len, int flush, int cookie)
  107. {
  108. size_t bytecount = 0;
  109. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  110. unsigned char *data = (unsigned char *)buf;
  111. unsigned char val = 0;
  112. int i;
  113. int len_40 = len / 40;
  114. while (bytecount < len) {
  115. val = data[bytecount++];
  116. i = 8;
  117. do {
  118. writeb(0xFB, &gpiop->pclrr_qspi);
  119. if (val & 0x01)
  120. writeb(0x01, &gpiop->ppd_qspi);
  121. else
  122. writeb(0xFE, &gpiop->pclrr_qspi);
  123. writeb(0x04, &gpiop->ppd_qspi);
  124. val >>= 1;
  125. i--;
  126. } while (i > 0);
  127. if (bytecount % len_40 == 0) {
  128. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  129. WATCHDOG_RESET();
  130. #endif
  131. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  132. putc('.'); /* let them know we are alive */
  133. #endif
  134. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  135. if (ctrlc())
  136. return FPGA_FAIL;
  137. #endif
  138. }
  139. }
  140. return FPGA_SUCCESS;
  141. }
  142. /* called, when programming is aborted */
  143. int altera_abort_fn(int cookie)
  144. {
  145. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  146. writeb(0x20, &gpiop->ppd_qspi);
  147. writeb(0x08, &gpiop->ppd_uart);
  148. return FPGA_SUCCESS;
  149. }
  150. /* called, when programming was succesful */
  151. int altera_post_fn(int cookie)
  152. {
  153. return altera_abort_fn(cookie);
  154. }
  155. /*
  156. * Note that these are pointers to code that is in Flash. They will be
  157. * relocated at runtime.
  158. * FIXME: relocation not yet working for coldfire, see below!
  159. */
  160. Altera_CYC2_Passive_Serial_fns altera_fns = {
  161. altera_pre_fn,
  162. altera_config_fn,
  163. altera_status_fn,
  164. altera_done_fn,
  165. altera_write_fn,
  166. altera_abort_fn,
  167. altera_post_fn
  168. };
  169. Altera_desc altera_fpga[CONFIG_FPGA_COUNT] = {
  170. {Altera_CYC2,
  171. passive_serial,
  172. 85903,
  173. (void *)&altera_fns,
  174. NULL,
  175. 0}
  176. };
  177. /* Initialize the fpga. Return 1 on success, 0 on failure. */
  178. int astro5373l_altera_load(void)
  179. {
  180. int i;
  181. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  182. /*
  183. * I did not yet manage to get relocation work properly,
  184. * so set stuff here instead of static initialisation:
  185. */
  186. altera_fns.pre = altera_pre_fn;
  187. altera_fns.config = altera_config_fn;
  188. altera_fns.status = altera_status_fn;
  189. altera_fns.done = altera_done_fn;
  190. altera_fns.write = altera_write_fn;
  191. altera_fns.abort = altera_abort_fn;
  192. altera_fns.post = altera_post_fn;
  193. altera_fpga[i].iface_fns = (void *)&altera_fns;
  194. fpga_add(fpga_altera, &altera_fpga[i]);
  195. }
  196. return 1;
  197. }
  198. /* Set the FPGA's PROG_B line to the specified level */
  199. int xilinx_pgm_fn(int assert, int flush, int cookie)
  200. {
  201. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  202. if (assert)
  203. writeb(0xFB, &gpiop->pclrr_uart);
  204. else
  205. writeb(0x04, &gpiop->ppd_uart);
  206. return assert;
  207. }
  208. /*
  209. * Test the state of the active-low FPGA INIT line. Return 1 on INIT
  210. * asserted (low).
  211. */
  212. int xilinx_init_fn(int cookie)
  213. {
  214. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  215. return (readb(&gpiop->ppd_pwm) & 0x08) == 0;
  216. }
  217. /* Test the state of the active-high FPGA DONE pin */
  218. int xilinx_done_fn(int cookie)
  219. {
  220. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  221. return (readb(&gpiop->ppd_pwm) & 0x20) >> 5;
  222. }
  223. /* Abort an FPGA operation */
  224. int xilinx_abort_fn(int cookie)
  225. {
  226. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  227. /* ensure all SPI peripherals and FPGAs are deselected */
  228. writeb(0x08, &gpiop->ppd_uart);
  229. writeb(0x01, &gpiop->ppd_timer);
  230. writeb(0x38, &gpiop->ppd_qspi);
  231. return FPGA_FAIL;
  232. }
  233. /*
  234. * FPGA pre-configuration function. Just make sure that
  235. * FPGA reset is asserted to keep the FPGA from starting up after
  236. * configuration.
  237. */
  238. int xilinx_pre_config_fn(int cookie)
  239. {
  240. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  241. unsigned char tmp_char;
  242. unsigned short tmp_short;
  243. /* first, set the required pins to GPIO function */
  244. /* PAR_T0IN -> GPIO */
  245. tmp_char = readb(&gpiop->par_timer);
  246. tmp_char &= 0xfc;
  247. writeb(tmp_char, &gpiop->par_timer);
  248. /* all QSPI pins -> GPIO */
  249. writew(0x0000, &gpiop->par_qspi);
  250. /* U0RTS, U0CTS -> GPIO */
  251. tmp_short = __raw_readw(&gpiop->par_uart);
  252. tmp_short &= 0xfff3;
  253. __raw_writew(tmp_short, &gpiop->par_uart);
  254. /* all PWM pins -> GPIO */
  255. writeb(0x00, &gpiop->par_pwm);
  256. /* next, set data direction registers */
  257. writeb(0x01, &gpiop->pddr_timer);
  258. writeb(0x25, &gpiop->pddr_qspi);
  259. writeb(0x0c, &gpiop->pddr_uart);
  260. writeb(0x04, &gpiop->pddr_pwm);
  261. /* ensure other SPI peripherals are deselected */
  262. writeb(0x08, &gpiop->ppd_uart);
  263. writeb(0x38, &gpiop->ppd_qspi);
  264. writeb(0x01, &gpiop->ppd_timer);
  265. /* CONFIG = 0, STATUS = 0 -> FPGA in reset state */
  266. writeb(0xFB, &gpiop->pclrr_uart);
  267. /* enable Xilinx configuration by clearing QSPI_CS2 and U0CTS */
  268. writeb(0xF7, &gpiop->pclrr_uart);
  269. writeb(0xDF, &gpiop->pclrr_qspi);
  270. return 0;
  271. }
  272. /*
  273. * FPGA post configuration function. Should perform a test if FPGA is running.
  274. */
  275. int xilinx_post_config_fn(int cookie)
  276. {
  277. int rc = 0;
  278. /*
  279. * no test yet
  280. */
  281. return rc;
  282. }
  283. int xilinx_clk_fn(int assert_clk, int flush, int cookie)
  284. {
  285. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  286. if (assert_clk)
  287. writeb(0x04, &gpiop->ppd_qspi);
  288. else
  289. writeb(0xFB, &gpiop->pclrr_qspi);
  290. return assert_clk;
  291. }
  292. int xilinx_wr_fn(int assert_write, int flush, int cookie)
  293. {
  294. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  295. if (assert_write)
  296. writeb(0x01, &gpiop->ppd_qspi);
  297. else
  298. writeb(0xFE, &gpiop->pclrr_qspi);
  299. return assert_write;
  300. }
  301. int xilinx_fastwr_fn(void *buf, size_t len, int flush, int cookie)
  302. {
  303. size_t bytecount = 0;
  304. gpio_t *gpiop = (gpio_t *)MMAP_GPIO;
  305. unsigned char *data = (unsigned char *)buf;
  306. unsigned char val = 0;
  307. int i;
  308. int len_40 = len / 40;
  309. for (bytecount = 0; bytecount < len; bytecount++) {
  310. val = *(data++);
  311. for (i = 8; i > 0; i--) {
  312. writeb(0xFB, &gpiop->pclrr_qspi);
  313. if (val & 0x80)
  314. writeb(0x01, &gpiop->ppd_qspi);
  315. else
  316. writeb(0xFE, &gpiop->pclrr_qspi);
  317. writeb(0x04, &gpiop->ppd_qspi);
  318. val <<= 1;
  319. }
  320. if (bytecount % len_40 == 0) {
  321. #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
  322. WATCHDOG_RESET();
  323. #endif
  324. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  325. putc('.'); /* let them know we are alive */
  326. #endif
  327. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  328. if (ctrlc())
  329. return FPGA_FAIL;
  330. #endif
  331. }
  332. }
  333. return FPGA_SUCCESS;
  334. }
  335. /*
  336. * Note that these are pointers to code that is in Flash. They will be
  337. * relocated at runtime.
  338. * FIXME: relocation not yet working for coldfire, see below!
  339. */
  340. Xilinx_Spartan3_Slave_Serial_fns xilinx_fns = {
  341. xilinx_pre_config_fn,
  342. xilinx_pgm_fn,
  343. xilinx_clk_fn,
  344. xilinx_init_fn,
  345. xilinx_done_fn,
  346. xilinx_wr_fn,
  347. 0,
  348. xilinx_fastwr_fn
  349. };
  350. Xilinx_desc xilinx_fpga[CONFIG_FPGA_COUNT] = {
  351. {Xilinx_Spartan3,
  352. slave_serial,
  353. XILINX_XC3S4000_SIZE,
  354. (void *)&xilinx_fns,
  355. 0}
  356. };
  357. /* Initialize the fpga. Return 1 on success, 0 on failure. */
  358. int astro5373l_xilinx_load(void)
  359. {
  360. int i;
  361. fpga_init();
  362. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  363. /*
  364. * I did not yet manage to get relocation work properly,
  365. * so set stuff here instead of static initialisation:
  366. */
  367. xilinx_fns.pre = xilinx_pre_config_fn;
  368. xilinx_fns.pgm = xilinx_pgm_fn;
  369. xilinx_fns.clk = xilinx_clk_fn;
  370. xilinx_fns.init = xilinx_init_fn;
  371. xilinx_fns.done = xilinx_done_fn;
  372. xilinx_fns.wr = xilinx_wr_fn;
  373. xilinx_fns.bwr = xilinx_fastwr_fn;
  374. xilinx_fpga[i].iface_fns = (void *)&xilinx_fns;
  375. fpga_add(fpga_xilinx, &xilinx_fpga[i]);
  376. }
  377. return 1;
  378. }