fpga.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * (C) Copyright 2001-2003
  3. * Matthias Fuchs, esd gmbh germany, matthias.fuchs@esd-electronics.com
  4. * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.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. #include <common.h>
  25. #include <asm/processor.h>
  26. #include <command.h>
  27. /* ------------------------------------------------------------------------- */
  28. #ifdef FPGA_DEBUG
  29. #define DBG(x...) printf(x)
  30. #else
  31. #define DBG(x...)
  32. #endif /* DEBUG */
  33. #define MAX_ONES 226
  34. #ifdef CFG_FPGA_PRG
  35. # define FPGA_PRG CFG_FPGA_PRG /* FPGA program pin (ppc output)*/
  36. # define FPGA_CLK CFG_FPGA_CLK /* FPGA clk pin (ppc output) */
  37. # define FPGA_DATA CFG_FPGA_DATA /* FPGA data pin (ppc output) */
  38. # define FPGA_DONE CFG_FPGA_DONE /* FPGA done pin (ppc input) */
  39. # define FPGA_INIT CFG_FPGA_INIT /* FPGA init pin (ppc input) */
  40. #else
  41. # define FPGA_PRG 0x04000000 /* FPGA program pin (ppc output) */
  42. # define FPGA_CLK 0x02000000 /* FPGA clk pin (ppc output) */
  43. # define FPGA_DATA 0x01000000 /* FPGA data pin (ppc output) */
  44. # define FPGA_DONE 0x00800000 /* FPGA done pin (ppc input) */
  45. # define FPGA_INIT 0x00400000 /* FPGA init pin (ppc input) */
  46. #endif
  47. #define ERROR_FPGA_PRG_INIT_LOW -1 /* Timeout after PRG* asserted */
  48. #define ERROR_FPGA_PRG_INIT_HIGH -2 /* Timeout after PRG* deasserted */
  49. #define ERROR_FPGA_PRG_DONE -3 /* Timeout after programming */
  50. #define SET_FPGA(data) out32(GPIO0_OR, data)
  51. #define FPGA_WRITE_1 { \
  52. SET_FPGA(FPGA_PRG | FPGA_DATA); /* set clock to 0 */ \
  53. SET_FPGA(FPGA_PRG | FPGA_DATA); /* set data to 1 */ \
  54. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA); /* set clock to 1 */ \
  55. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  56. #define FPGA_WRITE_0 { \
  57. SET_FPGA(FPGA_PRG | FPGA_DATA); /* set clock to 0 */ \
  58. SET_FPGA(FPGA_PRG); /* set data to 0 */ \
  59. SET_FPGA(FPGA_PRG | FPGA_CLK); /* set clock to 1 */ \
  60. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  61. static int fpga_boot(unsigned char *fpgadata, int size)
  62. {
  63. int i,index,len;
  64. int count;
  65. #ifdef CFG_FPGA_SPARTAN2
  66. int j;
  67. #else
  68. unsigned char b;
  69. int bit;
  70. #endif
  71. /* display infos on fpgaimage */
  72. index = 15;
  73. for (i=0; i<4; i++)
  74. {
  75. len = fpgadata[index];
  76. DBG("FPGA: %s\n", &(fpgadata[index+1]));
  77. index += len+3;
  78. }
  79. #ifdef CFG_FPGA_SPARTAN2
  80. /* search for preamble 0xFFFFFFFF */
  81. while (1)
  82. {
  83. if ((fpgadata[index] == 0xff) && (fpgadata[index+1] == 0xff) &&
  84. (fpgadata[index+2] == 0xff) && (fpgadata[index+3] == 0xff))
  85. break; /* preamble found */
  86. else
  87. index++;
  88. }
  89. #else
  90. /* search for preamble 0xFF2X */
  91. for (index = 0; index < size-1 ; index++)
  92. {
  93. if ((fpgadata[index] == 0xff) && ((fpgadata[index+1] & 0xf0) == 0x30))
  94. break;
  95. }
  96. index += 2;
  97. #endif
  98. DBG("FPGA: configdata starts at position 0x%x\n",index);
  99. DBG("FPGA: length of fpga-data %d\n", size-index);
  100. /*
  101. * Setup port pins for fpga programming
  102. */
  103. out32(GPIO0_ODR, 0x00000000); /* no open drain pins */
  104. out32(GPIO0_TCR, in32(GPIO0_TCR) | FPGA_PRG | FPGA_CLK | FPGA_DATA); /* setup for output */
  105. out32(GPIO0_OR, in32(GPIO0_OR) | FPGA_PRG | FPGA_CLK | FPGA_DATA); /* set pins to high */
  106. DBG("%s, ",((in32(GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE" );
  107. DBG("%s\n",((in32(GPIO0_IR) & FPGA_INIT) == 0) ? "NOT INIT" : "INIT" );
  108. /*
  109. * Init fpga by asserting and deasserting PROGRAM*
  110. */
  111. SET_FPGA(FPGA_CLK | FPGA_DATA);
  112. /* Wait for FPGA init line low */
  113. count = 0;
  114. while (in32(GPIO0_IR) & FPGA_INIT)
  115. {
  116. udelay(1000); /* wait 1ms */
  117. /* Check for timeout - 100us max, so use 3ms */
  118. if (count++ > 3)
  119. {
  120. DBG("FPGA: Booting failed!\n");
  121. return ERROR_FPGA_PRG_INIT_LOW;
  122. }
  123. }
  124. DBG("%s, ",((in32(GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE" );
  125. DBG("%s\n",((in32(GPIO0_IR) & FPGA_INIT) == 0) ? "NOT INIT" : "INIT" );
  126. /* deassert PROGRAM* */
  127. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
  128. /* Wait for FPGA end of init period . */
  129. count = 0;
  130. while (!(in32(GPIO0_IR) & FPGA_INIT))
  131. {
  132. udelay(1000); /* wait 1ms */
  133. /* Check for timeout */
  134. if (count++ > 3)
  135. {
  136. DBG("FPGA: Booting failed!\n");
  137. return ERROR_FPGA_PRG_INIT_HIGH;
  138. }
  139. }
  140. DBG("%s, ",((in32(GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE" );
  141. DBG("%s\n",((in32(GPIO0_IR) & FPGA_INIT) == 0) ? "NOT INIT" : "INIT" );
  142. DBG("write configuration data into fpga\n");
  143. /* write configuration-data into fpga... */
  144. #ifdef CFG_FPGA_SPARTAN2
  145. /*
  146. * Load uncompressed image into fpga
  147. */
  148. for (i=index; i<size; i++)
  149. {
  150. for (j=0; j<8; j++)
  151. {
  152. if ((fpgadata[i] & 0x80) == 0x80)
  153. {
  154. FPGA_WRITE_1;
  155. }
  156. else
  157. {
  158. FPGA_WRITE_0;
  159. }
  160. fpgadata[i] <<= 1;
  161. }
  162. }
  163. #else
  164. /* send 0xff 0x20 */
  165. FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1;
  166. FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1;
  167. FPGA_WRITE_0; FPGA_WRITE_0; FPGA_WRITE_1; FPGA_WRITE_0;
  168. FPGA_WRITE_0; FPGA_WRITE_0; FPGA_WRITE_0; FPGA_WRITE_0;
  169. /*
  170. ** Bit_DeCompression
  171. ** Code 1 .. maxOnes : n '1's followed by '0'
  172. ** maxOnes + 1 .. maxOnes + 1 : n - 1 '1's no '0'
  173. ** maxOnes + 2 .. 254 : n - (maxOnes + 2) '0's followed by '1'
  174. ** 255 : '1'
  175. */
  176. for (i=index; i<size; i++)
  177. {
  178. b = fpgadata[i];
  179. if ((b >= 1) && (b <= MAX_ONES))
  180. {
  181. for(bit=0; bit<b; bit++)
  182. {
  183. FPGA_WRITE_1;
  184. }
  185. FPGA_WRITE_0;
  186. }
  187. else if (b == (MAX_ONES+1))
  188. {
  189. for(bit=1; bit<b; bit++)
  190. {
  191. FPGA_WRITE_1;
  192. }
  193. }
  194. else if ((b >= (MAX_ONES+2)) && (b <= 254))
  195. {
  196. for(bit=0; bit<(b-(MAX_ONES+2)); bit++)
  197. {
  198. FPGA_WRITE_0;
  199. }
  200. FPGA_WRITE_1;
  201. }
  202. else if (b == 255)
  203. {
  204. FPGA_WRITE_1;
  205. }
  206. }
  207. #endif
  208. DBG("%s, ",((in32(GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE" );
  209. DBG("%s\n",((in32(GPIO0_IR) & FPGA_INIT) == 0) ? "NOT INIT" : "INIT" );
  210. /*
  211. * Check if fpga's DONE signal - correctly booted ?
  212. */
  213. /* Wait for FPGA end of programming period . */
  214. count = 0;
  215. while (!(in32(GPIO0_IR) & FPGA_DONE))
  216. {
  217. udelay(1000); /* wait 1ms */
  218. /* Check for timeout */
  219. if (count++ > 3)
  220. {
  221. DBG("FPGA: Booting failed!\n");
  222. return ERROR_FPGA_PRG_DONE;
  223. }
  224. }
  225. DBG("FPGA: Booting successful!\n");
  226. return 0;
  227. }