fpga.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * (C) Copyright 2006
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * (C) Copyright 2001-2004
  6. * Matthias Fuchs, esd gmbh germany, matthias.fuchs@esd-electronics.com
  7. * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <asm/processor.h>
  29. #include <command.h>
  30. /* ------------------------------------------------------------------------- */
  31. #ifdef FPGA_DEBUG
  32. #define DBG(x...) printf(x)
  33. #else
  34. #define DBG(x...)
  35. #endif /* DEBUG */
  36. #define FPGA_PRG CFG_FPGA_PRG /* FPGA program pin (cpu output)*/
  37. #define FPGA_CLK CFG_FPGA_CLK /* FPGA clk pin (cpu output) */
  38. #define FPGA_DATA CFG_FPGA_DATA /* FPGA data pin (cpu output) */
  39. #define FPGA_DONE CFG_FPGA_DONE /* FPGA done pin (cpu input) */
  40. #define FPGA_INIT CFG_FPGA_INIT /* FPGA init pin (cpu input) */
  41. #define ERROR_FPGA_PRG_INIT_LOW -1 /* Timeout after PRG* asserted */
  42. #define ERROR_FPGA_PRG_INIT_HIGH -2 /* Timeout after PRG* deasserted */
  43. #define ERROR_FPGA_PRG_DONE -3 /* Timeout after programming */
  44. #ifndef OLD_VAL
  45. # define OLD_VAL 0
  46. #endif
  47. #if 0 /* test-only */
  48. #define FPGA_WRITE_1 { \
  49. SET_FPGA(OLD_VAL | FPGA_PRG | 0 | FPGA_DATA); /* set clock to 0 */ \
  50. SET_FPGA(OLD_VAL | FPGA_PRG | 0 | FPGA_DATA); /* set data to 1 */ \
  51. SET_FPGA(OLD_VAL | FPGA_PRG | FPGA_CLK | FPGA_DATA); /* set clock to 1 */ \
  52. SET_FPGA(OLD_VAL | FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  53. #define FPGA_WRITE_0 { \
  54. SET_FPGA(OLD_VAL | FPGA_PRG | 0 | FPGA_DATA); /* set clock to 0 */ \
  55. SET_FPGA(OLD_VAL | FPGA_PRG | 0 | 0 ); /* set data to 0 */ \
  56. SET_FPGA(OLD_VAL | FPGA_PRG | FPGA_CLK | 0 ); /* set clock to 1 */ \
  57. SET_FPGA(OLD_VAL | FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  58. #else
  59. #define FPGA_WRITE_1 { \
  60. SET_FPGA(OLD_VAL | FPGA_PRG | 0 | FPGA_DATA); /* set data to 1 */ \
  61. SET_FPGA(OLD_VAL | FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  62. #define FPGA_WRITE_0 { \
  63. SET_FPGA(OLD_VAL | FPGA_PRG | 0 | 0 ); /* set data to 0 */ \
  64. SET_FPGA(OLD_VAL | FPGA_PRG | FPGA_CLK | 0 );} /* set data to 1 */
  65. #endif
  66. static int fpga_boot(unsigned char *fpgadata, int size)
  67. {
  68. int i,index,len;
  69. int count;
  70. int j;
  71. /* display infos on fpgaimage */
  72. index = 15;
  73. for (i=0; i<4; i++) {
  74. len = fpgadata[index];
  75. DBG("FPGA: %s\n", &(fpgadata[index+1]));
  76. index += len+3;
  77. }
  78. /* search for preamble 0xFFFFFFFF */
  79. while (1) {
  80. if ((fpgadata[index] == 0xff) && (fpgadata[index+1] == 0xff) &&
  81. (fpgadata[index+2] == 0xff) && (fpgadata[index+3] == 0xff))
  82. break; /* preamble found */
  83. else
  84. index++;
  85. }
  86. DBG("FPGA: configdata starts at position 0x%x\n",index);
  87. DBG("FPGA: length of fpga-data %d\n", size-index);
  88. /*
  89. * Setup port pins for fpga programming
  90. */
  91. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA); /* set pins to high */
  92. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  93. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  94. /*
  95. * Init fpga by asserting and deasserting PROGRAM*
  96. */
  97. SET_FPGA(0 | FPGA_CLK | FPGA_DATA); /* set prog active */
  98. /* Wait for FPGA init line low */
  99. count = 0;
  100. while (FPGA_INIT_STATE) {
  101. udelay(1000); /* wait 1ms */
  102. /* Check for timeout - 100us max, so use 3ms */
  103. if (count++ > 3) {
  104. DBG("FPGA: Booting failed!\n");
  105. return ERROR_FPGA_PRG_INIT_LOW;
  106. }
  107. }
  108. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  109. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  110. /* deassert PROGRAM* */
  111. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA); /* set prog inactive */
  112. /* Wait for FPGA end of init period . */
  113. count = 0;
  114. while (!(FPGA_INIT_STATE)) {
  115. udelay(1000); /* wait 1ms */
  116. /* Check for timeout */
  117. if (count++ > 3) {
  118. DBG("FPGA: Booting failed!\n");
  119. return ERROR_FPGA_PRG_INIT_HIGH;
  120. }
  121. }
  122. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  123. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  124. DBG("write configuration data into fpga\n");
  125. /* write configuration-data into fpga... */
  126. /*
  127. * Load uncompressed image into fpga
  128. */
  129. for (i=index; i<size; i++) {
  130. for (j=0; j<8; j++) {
  131. if ((fpgadata[i] & 0x80) == 0x80) {
  132. FPGA_WRITE_1;
  133. } else {
  134. FPGA_WRITE_0;
  135. }
  136. fpgadata[i] <<= 1;
  137. }
  138. }
  139. DBG("%s, ",(FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE" );
  140. DBG("%s\n",(FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT" );
  141. /*
  142. * Check if fpga's DONE signal - correctly booted ?
  143. */
  144. /* Wait for FPGA end of programming period . */
  145. count = 0;
  146. while (!(FPGA_DONE_STATE)) {
  147. udelay(1000); /* wait 1ms */
  148. /* Check for timeout */
  149. if (count++ > 3) {
  150. DBG("FPGA: Booting failed!\n");
  151. return ERROR_FPGA_PRG_DONE;
  152. }
  153. }
  154. DBG("FPGA: Booting successful!\n");
  155. return 0;
  156. }