fpga.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #if 0
  62. static int fpga_boot (unsigned char *fpgadata, int size)
  63. {
  64. int i, index, len;
  65. int count;
  66. #ifdef CFG_FPGA_SPARTAN2
  67. int j;
  68. #else
  69. unsigned char b;
  70. int bit;
  71. #endif
  72. /* display infos on fpgaimage */
  73. index = 15;
  74. for (i = 0; i < 4; i++) {
  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. if ((fpgadata[index] == 0xff) && (fpgadata[index + 1] == 0xff)
  83. && (fpgadata[index + 2] == 0xff)
  84. && (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. if ((fpgadata[index] == 0xff)
  93. && ((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, ",
  107. ((in32 (GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  108. DBG ("%s\n",
  109. ((in32 (GPIO0_IR) & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
  110. /*
  111. * Init fpga by asserting and deasserting PROGRAM*
  112. */
  113. SET_FPGA (FPGA_CLK | FPGA_DATA);
  114. /* Wait for FPGA init line low */
  115. count = 0;
  116. while (in32 (GPIO0_IR) & FPGA_INIT) {
  117. udelay (1000); /* wait 1ms */
  118. /* Check for timeout - 100us max, so use 3ms */
  119. if (count++ > 3) {
  120. DBG ("FPGA: Booting failed!\n");
  121. return ERROR_FPGA_PRG_INIT_LOW;
  122. }
  123. }
  124. DBG ("%s, ",
  125. ((in32 (GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  126. DBG ("%s\n",
  127. ((in32 (GPIO0_IR) & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
  128. /* deassert PROGRAM* */
  129. SET_FPGA (FPGA_PRG | FPGA_CLK | FPGA_DATA);
  130. /* Wait for FPGA end of init period . */
  131. count = 0;
  132. while (!(in32 (GPIO0_IR) & FPGA_INIT)) {
  133. udelay (1000); /* wait 1ms */
  134. /* Check for timeout */
  135. if (count++ > 3) {
  136. DBG ("FPGA: Booting failed!\n");
  137. return ERROR_FPGA_PRG_INIT_HIGH;
  138. }
  139. }
  140. DBG ("%s, ",
  141. ((in32 (GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  142. DBG ("%s\n",
  143. ((in32 (GPIO0_IR) & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
  144. DBG ("write configuration data into fpga\n");
  145. /* write configuration-data into fpga... */
  146. #ifdef CFG_FPGA_SPARTAN2
  147. /*
  148. * Load uncompressed image into fpga
  149. */
  150. for (i = index; i < size; i++) {
  151. for (j = 0; j < 8; j++) {
  152. if ((fpgadata[i] & 0x80) == 0x80) {
  153. FPGA_WRITE_1;
  154. } else {
  155. FPGA_WRITE_0;
  156. }
  157. fpgadata[i] <<= 1;
  158. }
  159. }
  160. #else /* ! CFG_FPGA_SPARTAN2 */
  161. /* send 0xff 0x20 */
  162. FPGA_WRITE_1;
  163. FPGA_WRITE_1;
  164. FPGA_WRITE_1;
  165. FPGA_WRITE_1;
  166. FPGA_WRITE_1;
  167. FPGA_WRITE_1;
  168. FPGA_WRITE_1;
  169. FPGA_WRITE_1;
  170. FPGA_WRITE_0;
  171. FPGA_WRITE_0;
  172. FPGA_WRITE_1;
  173. FPGA_WRITE_0;
  174. FPGA_WRITE_0;
  175. FPGA_WRITE_0;
  176. FPGA_WRITE_0;
  177. FPGA_WRITE_0;
  178. /*
  179. ** Bit_DeCompression
  180. ** Code 1 .. maxOnes : n '1's followed by '0'
  181. ** maxOnes + 1 .. maxOnes + 1 : n - 1 '1's no '0'
  182. ** maxOnes + 2 .. 254 : n - (maxOnes + 2) '0's followed by '1'
  183. ** 255 : '1'
  184. */
  185. for (i = index; i < size; i++) {
  186. b = fpgadata[i];
  187. if ((b >= 1) && (b <= MAX_ONES)) {
  188. for (bit = 0; bit < b; bit++) {
  189. FPGA_WRITE_1;
  190. }
  191. FPGA_WRITE_0;
  192. } else if (b == (MAX_ONES + 1)) {
  193. for (bit = 1; bit < b; bit++) {
  194. FPGA_WRITE_1;
  195. }
  196. } else if ((b >= (MAX_ONES + 2)) && (b <= 254)) {
  197. for (bit = 0; bit < (b - (MAX_ONES + 2)); bit++) {
  198. FPGA_WRITE_0;
  199. }
  200. FPGA_WRITE_1;
  201. } else if (b == 255) {
  202. FPGA_WRITE_1;
  203. }
  204. }
  205. #endif /* CFG_FPGA_SPARTAN2 */
  206. DBG ("%s, ",
  207. ((in32 (GPIO0_IR) & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  208. DBG ("%s\n",
  209. ((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. udelay (1000); /* wait 1ms */
  217. /* Check for timeout */
  218. if (count++ > 3) {
  219. DBG ("FPGA: Booting failed!\n");
  220. return ERROR_FPGA_PRG_DONE;
  221. }
  222. }
  223. DBG ("FPGA: Booting successful!\n");
  224. return 0;
  225. }
  226. #endif /* 0 */