fpga.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * (C) Copyright 2001-2004
  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. #ifndef SET_FPGA
  51. # define SET_FPGA(data) out32(GPIO0_OR, data)
  52. #endif
  53. #ifdef FPGA_PROG_ACTIVE_HIGH
  54. # define FPGA_PRG_LOW FPGA_PRG
  55. # define FPGA_PRG_HIGH 0
  56. #else
  57. # define FPGA_PRG_LOW 0
  58. # define FPGA_PRG_HIGH FPGA_PRG
  59. #endif
  60. #define FPGA_CLK_LOW 0
  61. #define FPGA_CLK_HIGH FPGA_CLK
  62. #define FPGA_DATA_LOW 0
  63. #define FPGA_DATA_HIGH FPGA_DATA
  64. #define FPGA_WRITE_1 { \
  65. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set clock to 0 */ \
  66. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set data to 1 */ \
  67. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set clock to 1 */ \
  68. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH);} /* set data to 1 */
  69. #define FPGA_WRITE_0 { \
  70. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set clock to 0 */ \
  71. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_LOW); /* set data to 0 */ \
  72. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_LOW); /* set clock to 1 */ \
  73. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH);} /* set data to 1 */
  74. #ifndef FPGA_DONE_STATE
  75. # define FPGA_DONE_STATE (in32(GPIO0_IR) & FPGA_DONE)
  76. #endif
  77. #ifndef FPGA_INIT_STATE
  78. # define FPGA_INIT_STATE (in32(GPIO0_IR) & FPGA_INIT)
  79. #endif
  80. static int fpga_boot(unsigned char *fpgadata, int size)
  81. {
  82. int i,index,len;
  83. int count;
  84. #ifdef CFG_FPGA_SPARTAN2
  85. int j;
  86. #else
  87. unsigned char b;
  88. int bit;
  89. #endif
  90. /* display infos on fpgaimage */
  91. index = 15;
  92. for (i=0; i<4; i++)
  93. {
  94. len = fpgadata[index];
  95. DBG("FPGA: %s\n", &(fpgadata[index+1]));
  96. index += len+3;
  97. }
  98. #ifdef CFG_FPGA_SPARTAN2
  99. /* search for preamble 0xFFFFFFFF */
  100. while (1)
  101. {
  102. if ((fpgadata[index] == 0xff) && (fpgadata[index+1] == 0xff) &&
  103. (fpgadata[index+2] == 0xff) && (fpgadata[index+3] == 0xff))
  104. break; /* preamble found */
  105. else
  106. index++;
  107. }
  108. #else
  109. /* search for preamble 0xFF2X */
  110. for (index = 0; index < size-1 ; index++)
  111. {
  112. if ((fpgadata[index] == 0xff) && ((fpgadata[index+1] & 0xf0) == 0x30))
  113. break;
  114. }
  115. index += 2;
  116. #endif
  117. DBG("FPGA: configdata starts at position 0x%x\n",index);
  118. DBG("FPGA: length of fpga-data %d\n", size-index);
  119. /*
  120. * Setup port pins for fpga programming
  121. */
  122. #ifndef CONFIG_M5249
  123. out32(GPIO0_ODR, 0x00000000); /* no open drain pins */
  124. out32(GPIO0_TCR, in32(GPIO0_TCR) | FPGA_PRG | FPGA_CLK | FPGA_DATA); /* setup for output */
  125. #endif
  126. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set pins to high */
  127. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  128. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  129. /*
  130. * Init fpga by asserting and deasserting PROGRAM*
  131. */
  132. SET_FPGA(FPGA_PRG_LOW | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set prog active */
  133. /* Wait for FPGA init line low */
  134. count = 0;
  135. while (FPGA_INIT_STATE)
  136. {
  137. udelay(1000); /* wait 1ms */
  138. /* Check for timeout - 100us max, so use 3ms */
  139. if (count++ > 3)
  140. {
  141. DBG("FPGA: Booting failed!\n");
  142. return ERROR_FPGA_PRG_INIT_LOW;
  143. }
  144. }
  145. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  146. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  147. /* deassert PROGRAM* */
  148. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set prog inactive */
  149. /* Wait for FPGA end of init period . */
  150. count = 0;
  151. while (!(FPGA_INIT_STATE))
  152. {
  153. udelay(1000); /* wait 1ms */
  154. /* Check for timeout */
  155. if (count++ > 3)
  156. {
  157. DBG("FPGA: Booting failed!\n");
  158. return ERROR_FPGA_PRG_INIT_HIGH;
  159. }
  160. }
  161. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  162. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  163. DBG("write configuration data into fpga\n");
  164. /* write configuration-data into fpga... */
  165. #ifdef CFG_FPGA_SPARTAN2
  166. /*
  167. * Load uncompressed image into fpga
  168. */
  169. for (i=index; i<size; i++)
  170. {
  171. for (j=0; j<8; j++)
  172. {
  173. if ((fpgadata[i] & 0x80) == 0x80)
  174. {
  175. FPGA_WRITE_1;
  176. }
  177. else
  178. {
  179. FPGA_WRITE_0;
  180. }
  181. fpgadata[i] <<= 1;
  182. }
  183. }
  184. #else
  185. /* send 0xff 0x20 */
  186. FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1;
  187. FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1; FPGA_WRITE_1;
  188. FPGA_WRITE_0; FPGA_WRITE_0; FPGA_WRITE_1; FPGA_WRITE_0;
  189. FPGA_WRITE_0; FPGA_WRITE_0; FPGA_WRITE_0; FPGA_WRITE_0;
  190. /*
  191. ** Bit_DeCompression
  192. ** Code 1 .. maxOnes : n '1's followed by '0'
  193. ** maxOnes + 1 .. maxOnes + 1 : n - 1 '1's no '0'
  194. ** maxOnes + 2 .. 254 : n - (maxOnes + 2) '0's followed by '1'
  195. ** 255 : '1'
  196. */
  197. for (i=index; i<size; i++)
  198. {
  199. b = fpgadata[i];
  200. if ((b >= 1) && (b <= MAX_ONES))
  201. {
  202. for(bit=0; bit<b; bit++)
  203. {
  204. FPGA_WRITE_1;
  205. }
  206. FPGA_WRITE_0;
  207. }
  208. else if (b == (MAX_ONES+1))
  209. {
  210. for(bit=1; bit<b; bit++)
  211. {
  212. FPGA_WRITE_1;
  213. }
  214. }
  215. else if ((b >= (MAX_ONES+2)) && (b <= 254))
  216. {
  217. for(bit=0; bit<(b-(MAX_ONES+2)); bit++)
  218. {
  219. FPGA_WRITE_0;
  220. }
  221. FPGA_WRITE_1;
  222. }
  223. else if (b == 255)
  224. {
  225. FPGA_WRITE_1;
  226. }
  227. }
  228. #endif
  229. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  230. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  231. /*
  232. * Check if fpga's DONE signal - correctly booted ?
  233. */
  234. /* Wait for FPGA end of programming period . */
  235. count = 0;
  236. while (!(FPGA_DONE_STATE))
  237. {
  238. udelay(1000); /* wait 1ms */
  239. /* Check for timeout */
  240. if (count++ > 3)
  241. {
  242. DBG("FPGA: Booting failed!\n");
  243. return ERROR_FPGA_PRG_DONE;
  244. }
  245. }
  246. DBG("FPGA: Booting successful!\n");
  247. return 0;
  248. }