stratixII.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * (C) Copyright 2007
  3. * Eran Liberty, Extricom , eran.liberty@gmail.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. */
  24. #include <common.h> /* core U-Boot definitions */
  25. #include <altera.h>
  26. int StratixII_ps_fpp_load (Altera_desc * desc, void *buf, size_t bsize,
  27. int isSerial, int isSecure);
  28. int StratixII_ps_fpp_dump (Altera_desc * desc, void *buf, size_t bsize);
  29. /****************************************************************/
  30. /* Stratix II Generic Implementation */
  31. int StratixII_load (Altera_desc * desc, void *buf, size_t bsize)
  32. {
  33. int ret_val = FPGA_FAIL;
  34. switch (desc->iface) {
  35. case passive_serial:
  36. ret_val = StratixII_ps_fpp_load (desc, buf, bsize, 1, 0);
  37. break;
  38. case fast_passive_parallel:
  39. ret_val = StratixII_ps_fpp_load (desc, buf, bsize, 0, 0);
  40. break;
  41. case fast_passive_parallel_security:
  42. ret_val = StratixII_ps_fpp_load (desc, buf, bsize, 0, 1);
  43. break;
  44. /* Add new interface types here */
  45. default:
  46. printf ("%s: Unsupported interface type, %d\n", __FUNCTION__,
  47. desc->iface);
  48. }
  49. return ret_val;
  50. }
  51. int StratixII_dump (Altera_desc * desc, void *buf, size_t bsize)
  52. {
  53. int ret_val = FPGA_FAIL;
  54. switch (desc->iface) {
  55. case passive_serial:
  56. case fast_passive_parallel:
  57. case fast_passive_parallel_security:
  58. ret_val = StratixII_ps_fpp_dump (desc, buf, bsize);
  59. break;
  60. /* Add new interface types here */
  61. default:
  62. printf ("%s: Unsupported interface type, %d\n", __FUNCTION__,
  63. desc->iface);
  64. }
  65. return ret_val;
  66. }
  67. int StratixII_info (Altera_desc * desc)
  68. {
  69. return FPGA_SUCCESS;
  70. }
  71. int StratixII_reloc (Altera_desc * desc, ulong reloc_offset)
  72. {
  73. int i;
  74. uint32_t dest = (uint32_t) desc & 0xff000000;
  75. /* we assume a relocated code and non relocated code has different upper 8 bits */
  76. if (dest != ((uint32_t) desc->iface_fns & 0xff000000)) {
  77. desc->iface_fns =
  78. (void *)((uint32_t) (desc->iface_fns) + reloc_offset);
  79. }
  80. for (i = 0; i < sizeof (altera_board_specific_func) / sizeof (void *);
  81. i++) {
  82. if (dest !=
  83. ((uint32_t) (((void **)(desc->iface_fns))[i]) & 0xff000000))
  84. {
  85. ((void **)(desc->iface_fns))[i] =
  86. (void
  87. *)(((uint32_t) (((void **)(desc->iface_fns))[i])) +
  88. reloc_offset);
  89. }
  90. }
  91. return FPGA_SUCCESS;
  92. }
  93. int StratixII_ps_fpp_dump (Altera_desc * desc, void *buf, size_t bsize)
  94. {
  95. printf ("Stratix II Fast Passive Parallel dump is not implemented\n");
  96. return FPGA_FAIL;
  97. }
  98. int StratixII_ps_fpp_load (Altera_desc * desc, void *buf, size_t bsize,
  99. int isSerial, int isSecure)
  100. {
  101. altera_board_specific_func *fns;
  102. int cookie;
  103. int ret_val = FPGA_FAIL;
  104. int bytecount;
  105. char *buff = buf;
  106. int i;
  107. if (!desc) {
  108. printf ("%s(%d) Altera_desc missing\n", __FUNCTION__, __LINE__);
  109. return FPGA_FAIL;
  110. }
  111. if (!buff) {
  112. printf ("%s(%d) buffer is missing\n", __FUNCTION__, __LINE__);
  113. return FPGA_FAIL;
  114. }
  115. if (!bsize) {
  116. printf ("%s(%d) size is zero\n", __FUNCTION__, __LINE__);
  117. return FPGA_FAIL;
  118. }
  119. if (!desc->iface_fns) {
  120. printf
  121. ("%s(%d) Altera_desc function interface table is missing\n",
  122. __FUNCTION__, __LINE__);
  123. return FPGA_FAIL;
  124. }
  125. fns = (altera_board_specific_func *) (desc->iface_fns);
  126. cookie = desc->cookie;
  127. if (!
  128. (fns->config && fns->status && fns->done && fns->data
  129. && fns->abort)) {
  130. printf
  131. ("%s(%d) Missing some function in the function interface table\n",
  132. __FUNCTION__, __LINE__);
  133. return FPGA_FAIL;
  134. }
  135. /* 1. give board specific a chance to do anything before we start */
  136. if (fns->pre) {
  137. if ((ret_val = fns->pre (cookie)) < 0) {
  138. return ret_val;
  139. }
  140. }
  141. /* from this point on we must fail gracfully by calling lower layer abort */
  142. /* 2. Strat burn cycle by deasserting config for t_CFG and waiting t_CF2CK after reaserted */
  143. fns->config (0, 1, cookie);
  144. udelay (5); /* nCONFIG low pulse width 2usec */
  145. fns->config (1, 1, cookie);
  146. udelay (100); /* nCONFIG high to first rising edge on DCLK */
  147. /* 3. Start the Data cycle with clk deasserted */
  148. bytecount = 0;
  149. fns->clk (0, 1, cookie);
  150. printf ("loading to fpga ");
  151. while (bytecount < bsize) {
  152. /* 3.1 check stratix has not signaled us an error */
  153. if (fns->status (cookie) != 1) {
  154. printf
  155. ("\n%s(%d) Stratix failed (byte transfered till failure 0x%x)\n",
  156. __FUNCTION__, __LINE__, bytecount);
  157. fns->abort (cookie);
  158. return FPGA_FAIL;
  159. }
  160. if (isSerial) {
  161. int i;
  162. uint8_t data = buff[bytecount++];
  163. for (i = 0; i < 8; i++) {
  164. /* 3.2(ps) put data on the bus */
  165. fns->data ((data >> i) & 1, 1, cookie);
  166. /* 3.3(ps) clock once */
  167. fns->clk (1, 1, cookie);
  168. fns->clk (0, 1, cookie);
  169. }
  170. } else {
  171. /* 3.2(fpp) put data on the bus */
  172. fns->data (buff[bytecount++], 1, cookie);
  173. /* 3.3(fpp) clock once */
  174. fns->clk (1, 1, cookie);
  175. fns->clk (0, 1, cookie);
  176. /* 3.4(fpp) for secure cycle push 3 more clocks */
  177. for (i = 0; isSecure && i < 3; i++) {
  178. fns->clk (1, 1, cookie);
  179. fns->clk (0, 1, cookie);
  180. }
  181. }
  182. /* 3.5 while clk is deasserted it is safe to print some progress indication */
  183. if ((bytecount % (bsize / 100)) == 0) {
  184. printf ("\b\b\b%02d\%", bytecount * 100 / bsize);
  185. }
  186. }
  187. /* 4. Set one last clock and check conf done signal */
  188. fns->clk (1, 1, cookie);
  189. udelay (100);
  190. if (!fns->done (cookie)) {
  191. printf (" error!.\n");
  192. fns->abort (cookie);
  193. return FPGA_FAIL;
  194. } else {
  195. printf ("\b\b\b done.\n");
  196. }
  197. /* 5. call lower layer post configuration */
  198. if (fns->post) {
  199. if ((ret_val = fns->post (cookie)) < 0) {
  200. fns->abort (cookie);
  201. return ret_val;
  202. }
  203. }
  204. return FPGA_SUCCESS;
  205. }