fpga.c 8.0 KB

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