fpga.c 9.6 KB

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