fpga.c 7.8 KB

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