fpga.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * (C) Copyright 2006
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * Altera FPGA configuration support for the ALPR computer from prodrive
  26. */
  27. #include <common.h>
  28. #include <altera.h>
  29. #include <ACEX1K.h>
  30. #include <command.h>
  31. #include <asm-ppc/processor.h>
  32. #include <ppc440.h>
  33. #include "fpga.h"
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #if defined(CONFIG_FPGA)
  36. #ifdef FPGA_DEBUG
  37. #define PRINTF(fmt,args...) printf (fmt ,##args)
  38. #else
  39. #define PRINTF(fmt,args...)
  40. #endif
  41. static unsigned long regval;
  42. #define SET_GPIO_REG_0(reg, bit) { \
  43. regval = in32(reg); \
  44. regval &= ~(0x80000000 >> bit); \
  45. out32(reg, regval); \
  46. }
  47. #define SET_GPIO_REG_1(reg, bit) { \
  48. regval = in32(reg); \
  49. regval |= (0x80000000 >> bit); \
  50. out32(reg, regval); \
  51. }
  52. #define SET_GPIO_0(bit) SET_GPIO_REG_0(GPIO0_OR, bit)
  53. #define SET_GPIO_1(bit) SET_GPIO_REG_1(GPIO0_OR, bit)
  54. #define FPGA_PRG (0x80000000 >> CFG_GPIO_PROG_EN)
  55. #define FPGA_CONFIG (0x80000000 >> CFG_GPIO_CONFIG)
  56. #define FPGA_DATA (0x80000000 >> CFG_GPIO_DATA)
  57. #define FPGA_CLK (0x80000000 >> CFG_GPIO_CLK)
  58. #define OLD_VAL (FPGA_PRG | FPGA_CONFIG)
  59. #define SET_FPGA(data) out32(GPIO0_OR, data)
  60. #define FPGA_WRITE_1 { \
  61. SET_FPGA(OLD_VAL | 0 | FPGA_DATA); /* set data to 1 */ \
  62. SET_FPGA(OLD_VAL | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
  63. #define FPGA_WRITE_0 { \
  64. SET_FPGA(OLD_VAL | 0 | 0 ); /* set data to 0 */ \
  65. SET_FPGA(OLD_VAL | FPGA_CLK | 0 );} /* set data to 1 */
  66. /* Plattforminitializations */
  67. /* Here we have to set the FPGA Chain */
  68. /* PROGRAM_PROG_EN = HIGH */
  69. /* PROGRAM_SEL_DPR = LOW */
  70. int fpga_pre_fn (int cookie)
  71. {
  72. unsigned long reg;
  73. reg = in32(GPIO0_IR);
  74. /* Enable the FPGA Chain */
  75. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_PROG_EN);
  76. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_PROG_EN);
  77. SET_GPIO_1(CFG_GPIO_PROG_EN);
  78. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_SEL_DPR);
  79. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_SEL_DPR);
  80. SET_GPIO_0((CFG_GPIO_SEL_DPR));
  81. /* initialize the GPIO Pins */
  82. /* output */
  83. SET_GPIO_0(CFG_GPIO_CLK);
  84. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_CLK);
  85. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_CLK);
  86. /* output */
  87. SET_GPIO_0(CFG_GPIO_DATA);
  88. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_DATA);
  89. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_DATA);
  90. /* First we set STATUS to 0 then as an input */
  91. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_STATUS);
  92. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_STATUS);
  93. SET_GPIO_0(CFG_GPIO_STATUS);
  94. SET_GPIO_REG_0(GPIO0_TCR, CFG_GPIO_STATUS);
  95. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_STATUS);
  96. /* output */
  97. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_CONFIG);
  98. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_CONFIG);
  99. SET_GPIO_0(CFG_GPIO_CONFIG);
  100. /* input */
  101. SET_GPIO_0(CFG_GPIO_CON_DON);
  102. SET_GPIO_REG_0(GPIO0_TCR, CFG_GPIO_CON_DON);
  103. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_CON_DON);
  104. /* CONFIG = 0 STATUS = 0 -> FPGA in reset state */
  105. SET_GPIO_0(CFG_GPIO_CONFIG);
  106. return FPGA_SUCCESS;
  107. }
  108. /* Set the state of CONFIG Pin */
  109. int fpga_config_fn (int assert_config, int flush, int cookie)
  110. {
  111. if (assert_config) {
  112. SET_GPIO_1(CFG_GPIO_CONFIG);
  113. } else {
  114. SET_GPIO_0(CFG_GPIO_CONFIG);
  115. }
  116. return FPGA_SUCCESS;
  117. }
  118. /* Returns the state of STATUS Pin */
  119. int fpga_status_fn (int cookie)
  120. {
  121. unsigned long reg;
  122. reg = in32(GPIO0_IR);
  123. if (reg &= (0x80000000 >> CFG_GPIO_STATUS)) {
  124. PRINTF("STATUS = HIGH\n");
  125. return FPGA_FAIL;
  126. }
  127. PRINTF("STATUS = LOW\n");
  128. return FPGA_SUCCESS;
  129. }
  130. /* Returns the state of CONF_DONE Pin */
  131. int fpga_done_fn (int cookie)
  132. {
  133. unsigned long reg;
  134. reg = in32(GPIO0_IR);
  135. if (reg &= (0x80000000 >> CFG_GPIO_CON_DON)) {
  136. PRINTF("CONF_DON = HIGH\n");
  137. return FPGA_FAIL;
  138. }
  139. PRINTF("CONF_DON = LOW\n");
  140. return FPGA_SUCCESS;
  141. }
  142. /* writes the complete buffer to the FPGA
  143. writing the complete buffer in one function is much faster,
  144. then calling it for every bit */
  145. int fpga_write_fn (void *buf, size_t len, int flush, int cookie)
  146. {
  147. size_t bytecount = 0;
  148. unsigned char *data = (unsigned char *) buf;
  149. unsigned char val=0;
  150. int i;
  151. int len_40 = len / 40;
  152. while (bytecount < len) {
  153. val = data[bytecount++];
  154. i = 8;
  155. do {
  156. if (val & 0x01) {
  157. FPGA_WRITE_1;
  158. } else {
  159. FPGA_WRITE_0;
  160. }
  161. val >>= 1;
  162. i --;
  163. } while (i > 0);
  164. #ifdef CFG_FPGA_PROG_FEEDBACK
  165. if (bytecount % len_40 == 0) {
  166. putc ('.'); /* let them know we are alive */
  167. #ifdef CFG_FPGA_CHECK_CTRLC
  168. if (ctrlc ())
  169. return FPGA_FAIL;
  170. #endif
  171. }
  172. #endif
  173. }
  174. return FPGA_SUCCESS;
  175. }
  176. /* called, when programming is aborted */
  177. int fpga_abort_fn (int cookie)
  178. {
  179. SET_GPIO_1((CFG_GPIO_SEL_DPR));
  180. return FPGA_SUCCESS;
  181. }
  182. /* called, when programming was succesful */
  183. int fpga_post_fn (int cookie)
  184. {
  185. return fpga_abort_fn (cookie);
  186. }
  187. /* Note that these are pointers to code that is in Flash. They will be
  188. * relocated at runtime.
  189. */
  190. Altera_CYC2_Passive_Serial_fns fpga_fns = {
  191. fpga_pre_fn,
  192. fpga_config_fn,
  193. fpga_status_fn,
  194. fpga_done_fn,
  195. fpga_write_fn,
  196. fpga_abort_fn,
  197. fpga_post_fn
  198. };
  199. Altera_desc fpga[CONFIG_FPGA_COUNT] = {
  200. {Altera_CYC2,
  201. passive_serial,
  202. Altera_EP2C35_SIZE,
  203. (void *) &fpga_fns,
  204. NULL,
  205. 0}
  206. };
  207. /*
  208. * Initialize the fpga. Return 1 on success, 0 on failure.
  209. */
  210. int alpr_fpga_init (void)
  211. {
  212. int i;
  213. PRINTF ("%s:%d: Initialize FPGA interface (relocation offset = 0x%.8lx)\n", __FUNCTION__, __LINE__, gd->reloc_off);
  214. fpga_init (gd->reloc_off);
  215. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  216. PRINTF ("%s:%d: Adding fpga %d\n", __FUNCTION__, __LINE__, i);
  217. fpga_add (fpga_altera, &fpga[i]);
  218. }
  219. return 1;
  220. }
  221. #endif