fpga.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <asm/io.h>
  27. #include <command.h>
  28. /* ------------------------------------------------------------------------- */
  29. #ifdef FPGA_DEBUG
  30. #define DBG(x...) printf(x)
  31. #else
  32. #define DBG(x...)
  33. #endif /* DEBUG */
  34. #define MAX_ONES 226
  35. #ifdef CONFIG_SYS_FPGA_PRG
  36. # define FPGA_PRG CONFIG_SYS_FPGA_PRG /* FPGA program pin (ppc output) */
  37. # define FPGA_CLK CONFIG_SYS_FPGA_CLK /* FPGA clk pin (ppc output) */
  38. # define FPGA_DATA CONFIG_SYS_FPGA_DATA /* FPGA data pin (ppc output) */
  39. # define FPGA_DONE CONFIG_SYS_FPGA_DONE /* FPGA done pin (ppc input) */
  40. # define FPGA_INIT CONFIG_SYS_FPGA_INIT /* FPGA init pin (ppc input) */
  41. #else
  42. # define FPGA_PRG 0x04000000 /* FPGA program pin (ppc output) */
  43. # define FPGA_CLK 0x02000000 /* FPGA clk pin (ppc output) */
  44. # define FPGA_DATA 0x01000000 /* FPGA data pin (ppc output) */
  45. # define FPGA_DONE 0x00800000 /* FPGA done pin (ppc input) */
  46. # define FPGA_INIT 0x00400000 /* FPGA init pin (ppc input) */
  47. #endif
  48. #define ERROR_FPGA_PRG_INIT_LOW -1 /* Timeout after PRG* asserted */
  49. #define ERROR_FPGA_PRG_INIT_HIGH -2 /* Timeout after PRG* deasserted */
  50. #define ERROR_FPGA_PRG_DONE -3 /* Timeout after programming */
  51. #ifndef SET_FPGA
  52. # define SET_FPGA(data) out_be32((void *)GPIO0_OR, data)
  53. #endif
  54. #ifdef FPGA_PROG_ACTIVE_HIGH
  55. # define FPGA_PRG_LOW FPGA_PRG
  56. # define FPGA_PRG_HIGH 0
  57. #else
  58. # define FPGA_PRG_LOW 0
  59. # define FPGA_PRG_HIGH FPGA_PRG
  60. #endif
  61. #define FPGA_CLK_LOW 0
  62. #define FPGA_CLK_HIGH FPGA_CLK
  63. #define FPGA_DATA_LOW 0
  64. #define FPGA_DATA_HIGH FPGA_DATA
  65. #define FPGA_WRITE_1 { \
  66. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set clock to 0 */ \
  67. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set data to 1 */ \
  68. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set clock to 1 */ \
  69. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH);} /* set data to 1 */
  70. #define FPGA_WRITE_0 { \
  71. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set clock to 0 */ \
  72. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_LOW); /* set data to 0 */ \
  73. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_LOW); /* set clock to 1 */ \
  74. SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH);} /* set data to 1 */
  75. #ifndef FPGA_DONE_STATE
  76. # define FPGA_DONE_STATE (in_be32((void *)GPIO0_IR) & FPGA_DONE)
  77. #endif
  78. #ifndef FPGA_INIT_STATE
  79. # define FPGA_INIT_STATE (in_be32((void *)GPIO0_IR) & FPGA_INIT)
  80. #endif
  81. static int fpga_boot (const unsigned char *fpgadata, int size)
  82. {
  83. int i, index, len;
  84. int count;
  85. unsigned char b;
  86. #ifdef CONFIG_SYS_FPGA_SPARTAN2
  87. int j;
  88. #else
  89. int bit;
  90. #endif
  91. /* display infos on fpgaimage */
  92. index = 15;
  93. for (i = 0; i < 4; i++) {
  94. len = fpgadata[index];
  95. DBG ("FPGA: %s\n", &(fpgadata[index + 1]));
  96. index += len + 3;
  97. }
  98. #ifdef CONFIG_SYS_FPGA_SPARTAN2
  99. /* search for preamble 0xFFFFFFFF */
  100. while (1) {
  101. if ((fpgadata[index] == 0xff) && (fpgadata[index + 1] == 0xff)
  102. && (fpgadata[index + 2] == 0xff)
  103. && (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. if ((fpgadata[index] == 0xff)
  112. && ((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. out_be32 ((void *)GPIO0_ODR, 0x00000000); /* no open drain pins */
  124. /* setup for output */
  125. out_be32 ((void *)GPIO0_TCR,
  126. in_be32 ((void *)GPIO0_TCR) |
  127. FPGA_PRG | FPGA_CLK | FPGA_DATA);
  128. #endif
  129. SET_FPGA (FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set pins to high */
  130. DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
  131. DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
  132. /*
  133. * Init fpga by asserting and deasserting PROGRAM*
  134. */
  135. SET_FPGA (FPGA_PRG_LOW | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set prog active */
  136. /* Wait for FPGA init line low */
  137. count = 0;
  138. while (FPGA_INIT_STATE) {
  139. udelay (1000); /* wait 1ms */
  140. /* Check for timeout - 100us max, so use 3ms */
  141. if (count++ > 3) {
  142. DBG ("FPGA: Booting failed!\n");
  143. return ERROR_FPGA_PRG_INIT_LOW;
  144. }
  145. }
  146. DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
  147. DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
  148. /* deassert PROGRAM* */
  149. SET_FPGA (FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set prog inactive */
  150. /* Wait for FPGA end of init period . */
  151. count = 0;
  152. while (!(FPGA_INIT_STATE)) {
  153. udelay (1000); /* wait 1ms */
  154. /* Check for timeout */
  155. if (count++ > 3) {
  156. DBG ("FPGA: Booting failed!\n");
  157. return ERROR_FPGA_PRG_INIT_HIGH;
  158. }
  159. }
  160. DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
  161. DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
  162. DBG ("write configuration data into fpga\n");
  163. /* write configuration-data into fpga... */
  164. #ifdef CONFIG_SYS_FPGA_SPARTAN2
  165. /*
  166. * Load uncompressed image into fpga
  167. */
  168. for (i = index; i < size; i++) {
  169. b = fpgadata[i];
  170. for (j = 0; j < 8; j++) {
  171. if ((b & 0x80) == 0x80) {
  172. FPGA_WRITE_1;
  173. } else {
  174. FPGA_WRITE_0;
  175. }
  176. b <<= 1;
  177. }
  178. }
  179. #else
  180. /* send 0xff 0x20 */
  181. FPGA_WRITE_1;
  182. FPGA_WRITE_1;
  183. FPGA_WRITE_1;
  184. FPGA_WRITE_1;
  185. FPGA_WRITE_1;
  186. FPGA_WRITE_1;
  187. FPGA_WRITE_1;
  188. FPGA_WRITE_1;
  189. FPGA_WRITE_0;
  190. FPGA_WRITE_0;
  191. FPGA_WRITE_1;
  192. FPGA_WRITE_0;
  193. FPGA_WRITE_0;
  194. FPGA_WRITE_0;
  195. FPGA_WRITE_0;
  196. FPGA_WRITE_0;
  197. /*
  198. ** Bit_DeCompression
  199. ** Code 1 .. maxOnes : n '1's followed by '0'
  200. ** maxOnes + 1 .. maxOnes + 1 : n - 1 '1's no '0'
  201. ** maxOnes + 2 .. 254 : n - (maxOnes + 2) '0's followed by '1'
  202. ** 255 : '1'
  203. */
  204. for (i = index; i < size; i++) {
  205. b = fpgadata[i];
  206. if ((b >= 1) && (b <= MAX_ONES)) {
  207. for (bit = 0; bit < b; bit++) {
  208. FPGA_WRITE_1;
  209. }
  210. FPGA_WRITE_0;
  211. } else if (b == (MAX_ONES + 1)) {
  212. for (bit = 1; bit < b; bit++) {
  213. FPGA_WRITE_1;
  214. }
  215. } else if ((b >= (MAX_ONES + 2)) && (b <= 254)) {
  216. for (bit = 0; bit < (b - (MAX_ONES + 2)); bit++) {
  217. FPGA_WRITE_0;
  218. }
  219. FPGA_WRITE_1;
  220. } else if (b == 255) {
  221. FPGA_WRITE_1;
  222. }
  223. }
  224. #endif
  225. DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
  226. DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
  227. /*
  228. * Check if fpga's DONE signal - correctly booted ?
  229. */
  230. /* Wait for FPGA end of programming period . */
  231. count = 0;
  232. while (!(FPGA_DONE_STATE)) {
  233. udelay (1000); /* wait 1ms */
  234. /* Check for timeout */
  235. if (count++ > 3) {
  236. DBG ("FPGA: Booting failed!\n");
  237. return ERROR_FPGA_PRG_DONE;
  238. }
  239. }
  240. DBG ("FPGA: Booting successful!\n");
  241. return 0;
  242. }