fpga.c 6.4 KB

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