fpga.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/processor.h>
  32. #include <asm/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) do { \
  43. regval = in32(reg); \
  44. regval &= ~(0x80000000 >> bit); \
  45. out32(reg, regval); \
  46. } while (0)
  47. #define SET_GPIO_REG_1(reg, bit) do { \
  48. regval = in32(reg); \
  49. regval |= (0x80000000 >> bit); \
  50. out32(reg, regval); \
  51. } while (0)
  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 >> CONFIG_SYS_GPIO_PROG_EN)
  55. #define FPGA_CONFIG (0x80000000 >> CONFIG_SYS_GPIO_CONFIG)
  56. #define FPGA_DATA (0x80000000 >> CONFIG_SYS_GPIO_DATA)
  57. #define FPGA_CLK (0x80000000 >> CONFIG_SYS_GPIO_CLK)
  58. #define OLD_VAL (FPGA_PRG | FPGA_CONFIG)
  59. #define SET_FPGA(data) out32(GPIO0_OR, data)
  60. #define FPGA_WRITE_1 do { \
  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. } while (0)
  64. #define FPGA_WRITE_0 do { \
  65. SET_FPGA(OLD_VAL | 0 | 0); /* set data to 0 */ \
  66. SET_FPGA(OLD_VAL | FPGA_CLK | 0); /* set data to 1 */ \
  67. } while (0)
  68. /* Plattforminitializations */
  69. /* Here we have to set the FPGA Chain */
  70. /* PROGRAM_PROG_EN = HIGH */
  71. /* PROGRAM_SEL_DPR = LOW */
  72. int fpga_pre_fn(int cookie)
  73. {
  74. unsigned long reg;
  75. reg = in32(GPIO0_IR);
  76. /* Enable the FPGA Chain */
  77. SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_PROG_EN);
  78. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_PROG_EN);
  79. SET_GPIO_1(CONFIG_SYS_GPIO_PROG_EN);
  80. SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_SEL_DPR);
  81. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_SEL_DPR);
  82. SET_GPIO_0((CONFIG_SYS_GPIO_SEL_DPR));
  83. /* initialize the GPIO Pins */
  84. /* output */
  85. SET_GPIO_0(CONFIG_SYS_GPIO_CLK);
  86. SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CLK);
  87. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CLK);
  88. /* output */
  89. SET_GPIO_0(CONFIG_SYS_GPIO_DATA);
  90. SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_DATA);
  91. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_DATA);
  92. /* First we set STATUS to 0 then as an input */
  93. SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS);
  94. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS);
  95. SET_GPIO_0(CONFIG_SYS_GPIO_STATUS);
  96. SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS);
  97. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS);
  98. /* output */
  99. SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CONFIG);
  100. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CONFIG);
  101. SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
  102. /* input */
  103. SET_GPIO_0(CONFIG_SYS_GPIO_CON_DON);
  104. SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_CON_DON);
  105. SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CON_DON);
  106. /* CONFIG = 0 STATUS = 0 -> FPGA in reset state */
  107. SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
  108. return FPGA_SUCCESS;
  109. }
  110. /* Set the state of CONFIG Pin */
  111. int fpga_config_fn(int assert_config, int flush, int cookie)
  112. {
  113. if (assert_config)
  114. SET_GPIO_1(CONFIG_SYS_GPIO_CONFIG);
  115. else
  116. SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
  117. return FPGA_SUCCESS;
  118. }
  119. /* Returns the state of STATUS Pin */
  120. int fpga_status_fn(int cookie)
  121. {
  122. unsigned long reg;
  123. reg = in32(GPIO0_IR);
  124. if (reg & (0x80000000 >> CONFIG_SYS_GPIO_STATUS)) {
  125. PRINTF("STATUS = HIGH\n");
  126. return FPGA_FAIL;
  127. }
  128. PRINTF("STATUS = LOW\n");
  129. return FPGA_SUCCESS;
  130. }
  131. /* Returns the state of CONF_DONE Pin */
  132. int fpga_done_fn(int cookie)
  133. {
  134. unsigned long reg;
  135. reg = in32(GPIO0_IR);
  136. if (reg & (0x80000000 >> CONFIG_SYS_GPIO_CON_DON)) {
  137. PRINTF("CONF_DON = HIGH\n");
  138. return FPGA_FAIL;
  139. }
  140. PRINTF("CONF_DON = LOW\n");
  141. return FPGA_SUCCESS;
  142. }
  143. /* writes the complete buffer to the FPGA
  144. writing the complete buffer in one function is much faster,
  145. then calling it for every bit */
  146. int fpga_write_fn(const void *buf, size_t len, int flush, int cookie)
  147. {
  148. size_t bytecount = 0;
  149. unsigned char *data = (unsigned char *) buf;
  150. unsigned char val = 0;
  151. int i;
  152. int len_40 = len / 40;
  153. while (bytecount < len) {
  154. val = data[bytecount++];
  155. i = 8;
  156. do {
  157. if (val & 0x01)
  158. FPGA_WRITE_1;
  159. else
  160. FPGA_WRITE_0;
  161. val >>= 1;
  162. i--;
  163. } while (i > 0);
  164. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  165. if (bytecount % len_40 == 0) {
  166. putc('.'); /* let them know we are alive */
  167. #ifdef CONFIG_SYS_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((CONFIG_SYS_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\n", __func__, __LINE__);
  214. fpga_init();
  215. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  216. PRINTF("%s:%d: Adding fpga %d\n", __func__, __LINE__, i);
  217. fpga_add(fpga_altera, &fpga[i]);
  218. }
  219. return 1;
  220. }
  221. #endif