fpga.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 (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 GPIO_CLK_PIN 0x00002000
  53. #define GPIO_CLK_PIN_I 0xffffdfff
  54. #define GPIO_DAT_PIN 0x00001000
  55. #define GPIO_DAT_PIN_I 0xffffefff
  56. #define GPIO_CLKDAT_PIN_I 0xffffcfff
  57. #define SET_GPIO_CLK_0 out32(GPIO0_OR, in32(GPIO0_OR) & GPIO_CLK_PIN_I);
  58. #define SET_GPIO_CLK_1 out32(GPIO0_OR, in32(GPIO0_OR) | GPIO_CLK_PIN);
  59. #define SET_GPIO_DAT_0 out32(GPIO0_OR, in32(GPIO0_OR) & GPIO_DAT_PIN_I);
  60. #define SET_GPIO_DAT_1 out32(GPIO0_OR, in32(GPIO0_OR) | GPIO_DAT_PIN);
  61. #define SET_GPIO_0(bit) SET_GPIO_REG_0(GPIO0_OR, bit)
  62. #define SET_GPIO_1(bit) SET_GPIO_REG_1(GPIO0_OR, bit)
  63. #define SET_GPIO_CLK_0_Z1 out32(GPIO0_OR, (in32(GPIO0_OR) & GPIO_CLK_PIN_I) | GPIO_DAT_PIN);
  64. #define SET_GPIO_CLK_0_Z0 out32(GPIO0_OR, in32(GPIO0_OR) & GPIO_CLKDAT_PIN_I);
  65. #define FPGA_WRITE_1 { \
  66. SET_GPIO_CLK_0_Z1\
  67. SET_GPIO_CLK_1}
  68. #define FPGA_WRITE_0 { \
  69. SET_GPIO_CLK_0_Z0\
  70. SET_GPIO_CLK_1}
  71. #define P_GP(reg) (reg & 0x00023f00)
  72. /* Plattforminitializations */
  73. /* Here we have to set the FPGA Chain */
  74. /* PROGRAM_PROG_EN = HIGH */
  75. /* PROGRAM_SEL_DPR = LOW */
  76. int fpga_pre_fn (int cookie)
  77. {
  78. unsigned long reg;
  79. reg = in32(GPIO0_IR);
  80. /* Enable the FPGA Chain */
  81. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_PROG_EN);
  82. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_PROG_EN);
  83. SET_GPIO_1(CFG_GPIO_PROG_EN);
  84. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_SEL_DPR);
  85. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_SEL_DPR);
  86. SET_GPIO_0((CFG_GPIO_SEL_DPR));
  87. /* initialize the GPIO Pins */
  88. /* output */
  89. SET_GPIO_0(CFG_GPIO_CLK);
  90. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_CLK);
  91. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_CLK);
  92. /* output */
  93. SET_GPIO_0(CFG_GPIO_DATA);
  94. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_DATA);
  95. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_DATA);
  96. /* First we set STATUS to 0 then as an input */
  97. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_STATUS);
  98. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_STATUS);
  99. SET_GPIO_0(CFG_GPIO_STATUS);
  100. SET_GPIO_REG_0(GPIO0_TCR, CFG_GPIO_STATUS);
  101. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_STATUS);
  102. /* output */
  103. SET_GPIO_REG_1(GPIO0_TCR, CFG_GPIO_CONFIG);
  104. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_CONFIG);
  105. SET_GPIO_0(CFG_GPIO_CONFIG);
  106. /* input */
  107. SET_GPIO_0(CFG_GPIO_CON_DON);
  108. SET_GPIO_REG_0(GPIO0_TCR, CFG_GPIO_CON_DON);
  109. SET_GPIO_REG_0(GPIO0_ODR, CFG_GPIO_CON_DON);
  110. /* CONFIG = 0 STATUS = 0 -> FPGA in reset state */
  111. SET_GPIO_0(CFG_GPIO_CONFIG);
  112. return FPGA_SUCCESS;
  113. }
  114. /* Set the state of CONFIG Pin */
  115. int fpga_config_fn (int assert_config, int flush, int cookie)
  116. {
  117. if (assert_config) {
  118. SET_GPIO_1(CFG_GPIO_CONFIG);
  119. } else {
  120. SET_GPIO_0(CFG_GPIO_CONFIG);
  121. }
  122. return FPGA_SUCCESS;
  123. }
  124. /* Returns the state of STATUS Pin */
  125. int fpga_status_fn (int cookie)
  126. {
  127. unsigned long reg;
  128. reg = in32(GPIO0_IR);
  129. if (reg &= (0x80000000 >> CFG_GPIO_STATUS)) {
  130. PRINTF("STATUS = HIGH\n");
  131. return FPGA_FAIL;
  132. }
  133. PRINTF("STATUS = LOW\n");
  134. return FPGA_SUCCESS;
  135. }
  136. /* Returns the state of CONF_DONE Pin */
  137. int fpga_done_fn (int cookie)
  138. {
  139. unsigned long reg;
  140. reg = in32(GPIO0_IR);
  141. if (reg &= (0x80000000 >> CFG_GPIO_CON_DON)) {
  142. PRINTF("CONF_DON = HIGH\n");
  143. return FPGA_FAIL;
  144. }
  145. PRINTF("CONF_DON = LOW\n");
  146. return FPGA_SUCCESS;
  147. }
  148. /* writes the complete buffer to the FPGA
  149. writing the complete buffer in one function is very faster,
  150. then calling it for every bit */
  151. int fpga_write_fn (void *buf, size_t len, int flush, int cookie)
  152. {
  153. size_t bytecount = 0;
  154. unsigned char *data = (unsigned char *) buf;
  155. unsigned char val=0;
  156. int i;
  157. while (bytecount < len) {
  158. #ifdef CFG_FPGA_CHECK_CTRLC
  159. if (ctrlc ()) {
  160. return FPGA_FAIL;
  161. }
  162. #endif
  163. val = data[bytecount ++ ];
  164. i = 8;
  165. do {
  166. if (val & 0x01) {
  167. FPGA_WRITE_1;
  168. } else {
  169. FPGA_WRITE_0;
  170. }
  171. val >>= 1;
  172. i --;
  173. } while (i > 0);
  174. #ifdef CFG_FPGA_PROG_FEEDBACK
  175. if (bytecount % (len / 40) == 0)
  176. putc ('.'); /* let them know we are alive */
  177. #endif
  178. }
  179. return FPGA_SUCCESS;
  180. }
  181. /* called, when programming is aborted */
  182. int fpga_abort_fn (int cookie)
  183. {
  184. SET_GPIO_1((CFG_GPIO_SEL_DPR));
  185. return FPGA_SUCCESS;
  186. }
  187. /* called, when programming was succesful */
  188. int fpga_post_fn (int cookie)
  189. {
  190. return fpga_abort_fn (cookie);
  191. }
  192. /* Note that these are pointers to code that is in Flash. They will be
  193. * relocated at runtime.
  194. */
  195. Altera_CYC2_Passive_Serial_fns fpga_fns = {
  196. fpga_pre_fn,
  197. fpga_config_fn,
  198. fpga_status_fn,
  199. fpga_done_fn,
  200. fpga_write_fn,
  201. fpga_abort_fn,
  202. fpga_post_fn
  203. };
  204. Altera_desc fpga[CONFIG_FPGA_COUNT] = {
  205. {Altera_CYC2,
  206. passive_serial,
  207. Altera_EP2C35_SIZE,
  208. (void *) &fpga_fns,
  209. NULL,
  210. 0}
  211. };
  212. /*
  213. * Initialize the fpga. Return 1 on success, 0 on failure.
  214. */
  215. int alpr_fpga_init (void)
  216. {
  217. int i;
  218. PRINTF ("%s:%d: Initialize FPGA interface (relocation offset = 0x%.8lx)\n", __FUNCTION__, __LINE__, gd->reloc_off);
  219. fpga_init (gd->reloc_off);
  220. for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
  221. PRINTF ("%s:%d: Adding fpga %d\n", __FUNCTION__, __LINE__, i);
  222. fpga_add (fpga_altera, &fpga[i]);
  223. }
  224. return 1;
  225. }
  226. #endif