fpga.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /* The DEBUG define must be before common to enable debugging */
  25. #undef DEBUG
  26. #include <common.h>
  27. #include <asm/processor.h>
  28. #include <command.h>
  29. #include "fpga.h"
  30. /* ------------------------------------------------------------------------- */
  31. #define MAX_ONES 226
  32. /* MPC850 port D */
  33. #define PD(bit) (1 << (15 - (bit)))
  34. # define FPGA_INIT PD(11) /* FPGA init pin (ppc input) */
  35. # define FPGA_PRG PD(12) /* FPGA program pin (ppc output) */
  36. # define FPGA_CLK PD(13) /* FPGA clk pin (ppc output) */
  37. # define FPGA_DATA PD(14) /* FPGA data pin (ppc output) */
  38. # define FPGA_DONE PD(15) /* FPGA done pin (ppc input) */
  39. /* DDR 0 - input, 1 - output */
  40. #define FPGA_INIT_PDDIR FPGA_PRG | FPGA_CLK | FPGA_DATA /* just set outputs */
  41. #define SET_FPGA(data) immr->im_ioport.iop_pddat = (data)
  42. #define GET_FPGA immr->im_ioport.iop_pddat
  43. #define FPGA_WRITE_1 { \
  44. SET_FPGA(FPGA_PRG | FPGA_DATA); /* set clock to 0 */ \
  45. SET_FPGA(FPGA_PRG | FPGA_DATA); /* set data to 1 */ \
  46. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA); /* set clock to 1 */ \
  47. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  48. #define FPGA_WRITE_0 { \
  49. SET_FPGA(FPGA_PRG | FPGA_DATA); /* set clock to 0 */ \
  50. SET_FPGA(FPGA_PRG); /* set data to 0 */ \
  51. SET_FPGA(FPGA_PRG | FPGA_CLK); /* set clock to 1 */ \
  52. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  53. int fpga_boot (unsigned char *fpgadata, int size)
  54. {
  55. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  56. int i, index, len;
  57. int count;
  58. #ifdef CFG_FPGA_SPARTAN2
  59. int j;
  60. unsigned char data;
  61. #else
  62. unsigned char b;
  63. int bit;
  64. #endif
  65. debug ("fpga_boot: fpgadata = %p, size = %d\n", fpgadata, size);
  66. /* display infos on fpgaimage */
  67. printf ("FPGA:");
  68. index = 15;
  69. for (i = 0; i < 4; i++) {
  70. len = fpgadata[index];
  71. printf (" %s", &(fpgadata[index + 1]));
  72. index += len + 3;
  73. }
  74. printf ("\n");
  75. index = 0;
  76. #ifdef CFG_FPGA_SPARTAN2
  77. /* search for preamble 0xFFFFFFFF */
  78. while (1) {
  79. if ((fpgadata[index] == 0xff) && (fpgadata[index + 1] == 0xff)
  80. && (fpgadata[index + 2] == 0xff)
  81. && (fpgadata[index + 3] == 0xff))
  82. break; /* preamble found */
  83. else
  84. index++;
  85. }
  86. #else
  87. /* search for preamble 0xFF2X */
  88. for (index = 0; index < size - 1; index++) {
  89. if ((fpgadata[index] == 0xff)
  90. && ((fpgadata[index + 1] & 0xf0) == 0x30))
  91. break;
  92. }
  93. index += 2;
  94. #endif
  95. debug ("FPGA: configdata starts at position 0x%x\n", index);
  96. debug ("FPGA: length of fpga-data %d\n", size - index);
  97. /*
  98. * Setup port pins for fpga programming
  99. */
  100. immr->im_ioport.iop_pddir = FPGA_INIT_PDDIR;
  101. debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  102. debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
  103. /*
  104. * Init fpga by asserting and deasserting PROGRAM*
  105. */
  106. SET_FPGA (FPGA_CLK | FPGA_DATA);
  107. /* Wait for FPGA init line low */
  108. count = 0;
  109. while (GET_FPGA & FPGA_INIT) {
  110. udelay (1000); /* wait 1ms */
  111. /* Check for timeout - 100us max, so use 3ms */
  112. if (count++ > 3) {
  113. debug ("FPGA: Booting failed!\n");
  114. return ERROR_FPGA_PRG_INIT_LOW;
  115. }
  116. }
  117. debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  118. debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
  119. /* deassert PROGRAM* */
  120. SET_FPGA (FPGA_PRG | FPGA_CLK | FPGA_DATA);
  121. /* Wait for FPGA end of init period . */
  122. count = 0;
  123. while (!(GET_FPGA & FPGA_INIT)) {
  124. udelay (1000); /* wait 1ms */
  125. /* Check for timeout */
  126. if (count++ > 3) {
  127. debug ("FPGA: Booting failed!\n");
  128. return ERROR_FPGA_PRG_INIT_HIGH;
  129. }
  130. }
  131. debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  132. debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
  133. debug ("write configuration data into fpga\n");
  134. /* write configuration-data into fpga... */
  135. #ifdef CFG_FPGA_SPARTAN2
  136. /*
  137. * Load uncompressed image into fpga
  138. */
  139. for (i = index; i < size; i++) {
  140. #ifdef CFG_FPGA_PROG_FEEDBACK
  141. if ((i % 1024) == 0)
  142. printf ("%6d out of %6d\r", i, size); /* let them know we are alive */
  143. #endif
  144. data = fpgadata[i];
  145. for (j = 0; j < 8; j++) {
  146. if ((data & 0x80) == 0x80) {
  147. FPGA_WRITE_1;
  148. } else {
  149. FPGA_WRITE_0;
  150. }
  151. data <<= 1;
  152. }
  153. }
  154. /* add some 0xff to the end of the file */
  155. for (i = 0; i < 8; i++) {
  156. data = 0xff;
  157. for (j = 0; j < 8; j++) {
  158. if ((data & 0x80) == 0x80) {
  159. FPGA_WRITE_1;
  160. } else {
  161. FPGA_WRITE_0;
  162. }
  163. data <<= 1;
  164. }
  165. }
  166. #else
  167. /* send 0xff 0x20 */
  168. FPGA_WRITE_1;
  169. FPGA_WRITE_1;
  170. FPGA_WRITE_1;
  171. FPGA_WRITE_1;
  172. FPGA_WRITE_1;
  173. FPGA_WRITE_1;
  174. FPGA_WRITE_1;
  175. FPGA_WRITE_1;
  176. FPGA_WRITE_0;
  177. FPGA_WRITE_0;
  178. FPGA_WRITE_1;
  179. FPGA_WRITE_0;
  180. FPGA_WRITE_0;
  181. FPGA_WRITE_0;
  182. FPGA_WRITE_0;
  183. FPGA_WRITE_0;
  184. /*
  185. ** Bit_DeCompression
  186. ** Code 1 .. maxOnes : n '1's followed by '0'
  187. ** maxOnes + 1 .. maxOnes + 1 : n - 1 '1's no '0'
  188. ** maxOnes + 2 .. 254 : n - (maxOnes + 2) '0's followed by '1'
  189. ** 255 : '1'
  190. */
  191. for (i = index; i < size; i++) {
  192. b = fpgadata[i];
  193. if ((b >= 1) && (b <= MAX_ONES)) {
  194. for (bit = 0; bit < b; bit++) {
  195. FPGA_WRITE_1;
  196. }
  197. FPGA_WRITE_0;
  198. } else if (b == (MAX_ONES + 1)) {
  199. for (bit = 1; bit < b; bit++) {
  200. FPGA_WRITE_1;
  201. }
  202. } else if ((b >= (MAX_ONES + 2)) && (b <= 254)) {
  203. for (bit = 0; bit < (b - (MAX_ONES + 2)); bit++) {
  204. FPGA_WRITE_0;
  205. }
  206. FPGA_WRITE_1;
  207. } else if (b == 255) {
  208. FPGA_WRITE_1;
  209. }
  210. }
  211. #endif
  212. debug ("\n\n");
  213. debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
  214. debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
  215. /*
  216. * Check if fpga's DONE signal - correctly booted ?
  217. */
  218. /* Wait for FPGA end of programming period . */
  219. count = 0;
  220. while (!(GET_FPGA & FPGA_DONE)) {
  221. udelay (1000); /* wait 1ms */
  222. /* Check for timeout */
  223. if (count++ > 3) {
  224. debug ("FPGA: Booting failed!\n");
  225. return ERROR_FPGA_PRG_DONE;
  226. }
  227. }
  228. debug ("FPGA: Booting successful!\n");
  229. return 0;
  230. }