fpga.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * (C) Copyright 2001
  3. * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com
  4. * and
  5. * Bill Hunter, Wave 7 Optics, william.hunter@mediaone.net
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <config.h>
  26. #include <common.h>
  27. #include "w7o.h"
  28. #include <asm/processor.h>
  29. #include "errors.h"
  30. static void
  31. fpga_img_write(unsigned long *src, unsigned long len, unsigned short *daddr)
  32. {
  33. unsigned long i;
  34. volatile unsigned long val;
  35. volatile unsigned short *dest = daddr; /* volatile-bypass optimizer */
  36. for (i = 0; i < len; i++, src++) {
  37. val = *src;
  38. *dest = (unsigned short)((val & 0xff000000L) >> 16);
  39. *dest = (unsigned short)((val & 0x00ff0000L) >> 8);
  40. *dest = (unsigned short)(val & 0x0000ff00L);
  41. *dest = (unsigned short)((val & 0x000000ffL) << 8);
  42. }
  43. /* Terminate programming with 4 C clocks */
  44. dest = daddr;
  45. val = *(unsigned short *)dest;
  46. val = *(unsigned short *)dest;
  47. val = *(unsigned short *)dest;
  48. val = *(unsigned short *)dest;
  49. }
  50. int
  51. fpgaDownload(unsigned char *saddr,
  52. unsigned long size,
  53. unsigned short *daddr)
  54. {
  55. int i; /* index, intr disable flag */
  56. int start; /* timer */
  57. unsigned long greg, grego; /* GPIO & output register */
  58. unsigned long length; /* image size in words */
  59. unsigned long *source; /* image source addr */
  60. unsigned short *dest; /* destination FPGA addr */
  61. volatile unsigned short *ndest; /* temp dest FPGA addr */
  62. volatile unsigned short val; /* temp val */
  63. unsigned long cnfg = GPIO_XCV_CNFG; /* FPGA CNFG */
  64. unsigned long eirq = GPIO_XCV_IRQ;
  65. int retval = -1; /* Function return value */
  66. /* Setup some basic values */
  67. length = (size / 4) + 1; /* size in words, rounding UP
  68. is OK */
  69. source = (unsigned long *)saddr;
  70. dest = (unsigned short *)daddr;
  71. /* Get DCR output register */
  72. grego = in32(PPC405GP_GPIO0_OR);
  73. /* Reset FPGA */
  74. grego &= ~GPIO_XCV_PROG; /* PROG line low */
  75. out32(PPC405GP_GPIO0_OR, grego);
  76. /* Setup timeout timer */
  77. start = get_timer(0);
  78. /* Wait for FPGA init line */
  79. while(in32(PPC405GP_GPIO0_IR) & GPIO_XCV_INIT) { /* Wait INIT line low */
  80. /* Check for timeout - 100us max, so use 3ms */
  81. if (get_timer(start) > 3) {
  82. printf(" failed to start init.\n");
  83. log_warn(ERR_XINIT0); /* Don't halt */
  84. /* Reset line stays low */
  85. goto done; /* I like gotos... */
  86. }
  87. }
  88. /* Unreset FPGA */
  89. grego |= GPIO_XCV_PROG; /* PROG line high */
  90. out32(PPC405GP_GPIO0_OR, grego);
  91. /* Wait for FPGA end of init period . */
  92. while(!(in32(PPC405GP_GPIO0_IR) & GPIO_XCV_INIT)) { /* Wait for INIT hi */
  93. /* Check for timeout */
  94. if (get_timer(start) > 3) {
  95. printf(" failed to exit init.\n");
  96. log_warn(ERR_XINIT1);
  97. /* Reset FPGA */
  98. grego &= ~GPIO_XCV_PROG; /* PROG line low */
  99. out32(PPC405GP_GPIO0_OR, grego);
  100. goto done;
  101. }
  102. }
  103. /* Now program FPGA ... */
  104. ndest = dest;
  105. for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
  106. /* Toggle IRQ/GPIO */
  107. greg = mfdcr(CPC0_CR0); /* get chip ctrl register */
  108. greg |= eirq; /* toggle irq/gpio */
  109. mtdcr(CPC0_CR0, greg); /* ... just do it */
  110. /* turn on open drain for CNFG */
  111. greg = in32(PPC405GP_GPIO0_ODR); /* get open drain register */
  112. greg |= cnfg; /* CNFG open drain */
  113. out32(PPC405GP_GPIO0_ODR, greg); /* .. just do it */
  114. /* Turn output enable on for CNFG */
  115. greg = in32(PPC405GP_GPIO0_TCR); /* get tristate register */
  116. greg |= cnfg; /* CNFG tristate inactive */
  117. out32(PPC405GP_GPIO0_TCR, greg); /* ... just do it */
  118. /* Setup FPGA for programming */
  119. grego &= ~cnfg; /* CONFIG line low */
  120. out32(PPC405GP_GPIO0_OR, grego);
  121. /*
  122. * Program the FPGA
  123. */
  124. printf("\n destination: 0x%lx ", (unsigned long)ndest);
  125. fpga_img_write(source, length, (unsigned short *)ndest);
  126. /* Done programming */
  127. grego |= cnfg; /* CONFIG line high */
  128. out32(PPC405GP_GPIO0_OR, grego);
  129. /* Turn output enable OFF for CNFG */
  130. greg = in32(PPC405GP_GPIO0_TCR); /* get tristate register */
  131. greg &= ~cnfg; /* CNFG tristate inactive */
  132. out32(PPC405GP_GPIO0_TCR, greg); /* ... just do it */
  133. /* Toggle IRQ/GPIO */
  134. greg = mfdcr(CPC0_CR0); /* get chip ctrl register */
  135. greg &= ~eirq; /* toggle irq/gpio */
  136. mtdcr(CPC0_CR0, greg); /* ... just do it */
  137. ndest = (unsigned short *)((char *)ndest + 0x00100000L); /* XXX - Next FPGA addr */
  138. cnfg >>= 1; /* XXX - Next */
  139. eirq >>= 1;
  140. }
  141. /* Terminate programming with 4 C clocks */
  142. ndest = dest;
  143. for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
  144. val = *ndest;
  145. val = *ndest;
  146. val = *ndest;
  147. val = *ndest;
  148. ndest = (unsigned short *)((char *)ndest + 0x00100000L);
  149. }
  150. /* Setup timer */
  151. start = get_timer(0);
  152. /* Wait for FPGA end of programming period . */
  153. while(!(in32(PPC405GP_GPIO0_IR) & GPIO_XCV_DONE)) { /* Test DONE low */
  154. /* Check for timeout */
  155. if (get_timer(start) > 3) {
  156. printf(" done failed to come high.\n");
  157. log_warn(ERR_XDONE1);
  158. /* Reset FPGA */
  159. grego &= ~GPIO_XCV_PROG; /* PROG line low */
  160. out32(PPC405GP_GPIO0_OR, grego);
  161. goto done;
  162. }
  163. }
  164. printf("\n FPGA load succeeded\n");
  165. retval = 0; /* Program OK */
  166. done:
  167. return retval;
  168. }
  169. /* FPGA image is stored in flash */
  170. extern flash_info_t flash_info[];
  171. int init_fpga(void)
  172. {
  173. unsigned int i,j,ptr; /* General purpose */
  174. unsigned char bufchar; /* General purpose character */
  175. unsigned char *buf; /* Start of image pointer */
  176. unsigned long len; /* Length of image */
  177. unsigned char *fn_buf; /* Start of filename string */
  178. unsigned int fn_len; /* Length of filename string */
  179. unsigned char *xcv_buf; /* Pointer to start of image */
  180. unsigned long xcv_len; /* Length of image */
  181. unsigned long crc; /* 30bit crc in image */
  182. unsigned long calc_crc; /* Calc'd 30bit crc */
  183. int retval = -1;
  184. /* Tell the world what we are doing */
  185. printf("FPGA: ");
  186. /*
  187. * Get address of first sector where the FPGA
  188. * image is stored.
  189. */
  190. buf = (unsigned char *)flash_info[1].start[0];
  191. /*
  192. * Get the stored image's CRC & length.
  193. */
  194. crc = *(unsigned long *)(buf+4); /* CRC is first long word */
  195. len = *(unsigned long *)(buf+8); /* Image len is next long */
  196. /* Pedantic */
  197. if ((len < 0x133A4) || (len > 0x80000))
  198. goto bad_image;
  199. /*
  200. * Get the file name pointer and length.
  201. */
  202. fn_len = (*(unsigned short *)(buf+12) & 0xff); /* filename length
  203. is next short */
  204. fn_buf = buf + 14;
  205. /*
  206. * Get the FPGA image pointer and length length.
  207. */
  208. xcv_buf = fn_buf + fn_len; /* pointer to fpga image */
  209. xcv_len = len - 14 - fn_len; /* fpga image length */
  210. /* Check for uninitialized FLASH */
  211. if ((strncmp(buf, "w7o", 3)!=0) || (len > 0x0007ffffL) || (len == 0))
  212. goto bad_image;
  213. /*
  214. * Calculate and Check the image's CRC.
  215. */
  216. calc_crc = crc32(0, xcv_buf, xcv_len);
  217. if (crc != calc_crc) {
  218. printf("\nfailed - bad CRC\n");
  219. goto done;
  220. }
  221. /* Output the file name */
  222. printf("file name : ");
  223. for (i=0;i<fn_len;i++) {
  224. bufchar = fn_buf[+i];
  225. if (bufchar<' ' || bufchar>'~') bufchar = '.';
  226. putc(bufchar);
  227. }
  228. /*
  229. * find rest of display data
  230. */
  231. ptr = 15; /* Offset to ncd filename
  232. length in fpga image */
  233. j = xcv_buf[ptr]; /* Get len of ncd filename */
  234. if (j > 32) goto bad_image;
  235. ptr = ptr + j + 3; /* skip ncd filename string +
  236. 3 bytes more bytes */
  237. /*
  238. * output target device string
  239. */
  240. j = xcv_buf[ptr++] - 1; /* len of targ str less term */
  241. if (j > 32) goto bad_image;
  242. printf("\n target : ");
  243. for (i = 0; i < j; i++) {
  244. bufchar = (xcv_buf[ptr++]);
  245. if (bufchar<' ' || bufchar>'~') bufchar = '.';
  246. putc(bufchar);
  247. }
  248. /*
  249. * output compilation date string and time string
  250. */
  251. ptr += 3; /* skip 2 bytes */
  252. printf("\n synth time : ");
  253. j = (xcv_buf[ptr++] - 1); /* len of date str less term */
  254. if (j > 32) goto bad_image;
  255. for (i = 0; i < j; i++) {
  256. bufchar = (xcv_buf[ptr++]);
  257. if (bufchar<' ' || bufchar>'~') bufchar = '.';
  258. putc(bufchar);
  259. }
  260. ptr += 3; /* Skip 2 bytes */
  261. printf(" - ");
  262. j = (xcv_buf[ptr++] - 1); /* slen = targ dev str len */
  263. if (j > 32) goto bad_image;
  264. for (i = 0; i < j; i++) {
  265. bufchar = (xcv_buf[ptr++]);
  266. if (bufchar<' ' || bufchar>'~') bufchar = '.';
  267. putc(bufchar);
  268. }
  269. /*
  270. * output crc and length strings
  271. */
  272. printf("\n len & crc : 0x%lx 0x%lx", len, crc);
  273. /*
  274. * Program the FPGA.
  275. */
  276. retval = fpgaDownload((unsigned char*)xcv_buf, xcv_len,
  277. (unsigned short *)0xfd000000L);
  278. return retval;
  279. bad_image:
  280. printf("\n BAD FPGA image format @ %lx\n", flash_info[1].start[0]);
  281. log_warn(ERR_XIMAGE);
  282. done:
  283. return retval;
  284. }
  285. void test_fpga(unsigned short *daddr)
  286. {
  287. int i;
  288. volatile unsigned short *ndest = daddr;
  289. for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
  290. #if defined(CONFIG_W7OLMG)
  291. ndest[0x7e] = 0x55aa;
  292. if (ndest[0x7e] != 0x55aa)
  293. log_warn(ERR_XRW1 + i);
  294. ndest[0x7e] = 0xaa55;
  295. if (ndest[0x7e] != 0xaa55)
  296. log_warn(ERR_XRW1 + i);
  297. ndest[0x7e] = 0xc318;
  298. if (ndest[0x7e] != 0xc318)
  299. log_warn(ERR_XRW1 + i);
  300. #elif defined(CONFIG_W7OLMC)
  301. ndest[0x800] = 0x55aa;
  302. ndest[0x801] = 0xaa55;
  303. ndest[0x802] = 0xc318;
  304. ndest[0x4800] = 0x55aa;
  305. ndest[0x4801] = 0xaa55;
  306. ndest[0x4802] = 0xc318;
  307. if ((ndest[0x800] != 0x55aa) ||
  308. (ndest[0x801] != 0xaa55) ||
  309. (ndest[0x802] != 0xc318))
  310. log_warn(ERR_XRW1 + (2 * i)); /* Auto gen error code */
  311. if ((ndest[0x4800] != 0x55aa) ||
  312. (ndest[0x4801] != 0xaa55) ||
  313. (ndest[0x4802] != 0xc318))
  314. log_warn(ERR_XRW2 + (2 * i)); /* Auto gen error code */
  315. #else
  316. # error "Unknown W7O board configuration"
  317. #endif
  318. }
  319. printf(" FPGA ready\n");
  320. return;
  321. }