fpga.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <linux/compiler.h>
  30. #include "errors.h"
  31. static void
  32. fpga_img_write(unsigned long *src, unsigned long len, unsigned short *daddr)
  33. {
  34. unsigned long i;
  35. volatile unsigned long val;
  36. volatile unsigned short *dest = daddr; /* volatile-bypass optimizer */
  37. for (i = 0; i < len; i++, src++) {
  38. val = *src;
  39. *dest = (unsigned short) ((val & 0xff000000L) >> 16);
  40. *dest = (unsigned short) ((val & 0x00ff0000L) >> 8);
  41. *dest = (unsigned short) (val & 0x0000ff00L);
  42. *dest = (unsigned short) ((val & 0x000000ffL) << 8);
  43. }
  44. /* Terminate programming with 4 C clocks */
  45. dest = daddr;
  46. val = *(unsigned short *) dest;
  47. val = *(unsigned short *) dest;
  48. val = *(unsigned short *) dest;
  49. val = *(unsigned short *) dest;
  50. }
  51. int
  52. fpgaDownload(unsigned char *saddr, unsigned long size, unsigned short *daddr)
  53. {
  54. int i; /* index, intr disable flag */
  55. int start; /* timer */
  56. unsigned long greg, grego; /* GPIO & output register */
  57. unsigned long length; /* image size in words */
  58. unsigned long *source; /* image source addr */
  59. unsigned short *dest; /* destination FPGA addr */
  60. volatile unsigned short *ndest; /* temp dest FPGA addr */
  61. unsigned long cnfg = GPIO_XCV_CNFG; /* FPGA CNFG */
  62. unsigned long eirq = GPIO_XCV_IRQ;
  63. int retval = -1; /* Function return value */
  64. __maybe_unused volatile unsigned short val; /* temp val */
  65. /* Setup some basic values */
  66. length = (size / 4) + 1; /* size in words, rounding UP
  67. is OK */
  68. source = (unsigned long *) saddr;
  69. dest = (unsigned short *) daddr;
  70. /* Get DCR output register */
  71. grego = in32(PPC405GP_GPIO0_OR);
  72. /* Reset FPGA */
  73. grego &= ~GPIO_XCV_PROG; /* PROG line low */
  74. out32(PPC405GP_GPIO0_OR, grego);
  75. /* Setup timeout timer */
  76. start = get_timer(0);
  77. /* Wait for FPGA init line to go low */
  78. while (in32(PPC405GP_GPIO0_IR) & GPIO_XCV_INIT) {
  79. /* Check for timeout - 100us max, so use 3ms */
  80. if (get_timer(start) > 3) {
  81. printf(" failed to start init.\n");
  82. log_warn(ERR_XINIT0); /* Don't halt */
  83. /* Reset line stays low */
  84. goto done; /* I like gotos... */
  85. }
  86. }
  87. /* Unreset FPGA */
  88. grego |= GPIO_XCV_PROG; /* PROG line high */
  89. out32(PPC405GP_GPIO0_OR, grego);
  90. /* Wait for FPGA end of init period = init line go hi */
  91. while (!(in32(PPC405GP_GPIO0_IR) & GPIO_XCV_INIT)) {
  92. /* Check for timeout */
  93. if (get_timer(start) > 3) {
  94. printf(" failed to exit init.\n");
  95. log_warn(ERR_XINIT1);
  96. /* Reset FPGA */
  97. grego &= ~GPIO_XCV_PROG; /* PROG line low */
  98. out32(PPC405GP_GPIO0_OR, grego);
  99. goto done;
  100. }
  101. }
  102. /* Now program FPGA ... */
  103. ndest = dest;
  104. for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
  105. /* Toggle IRQ/GPIO */
  106. greg = mfdcr(CPC0_CR0); /* get chip ctrl register */
  107. greg |= eirq; /* toggle irq/gpio */
  108. mtdcr(CPC0_CR0, greg); /* ... just do it */
  109. /* turn on open drain for CNFG */
  110. greg = in32(PPC405GP_GPIO0_ODR); /* get open drain register */
  111. greg |= cnfg; /* CNFG open drain */
  112. out32(PPC405GP_GPIO0_ODR, greg); /* .. just do it */
  113. /* Turn output enable on for CNFG */
  114. greg = in32(PPC405GP_GPIO0_TCR); /* get tristate register */
  115. greg |= cnfg; /* CNFG tristate inactive */
  116. out32(PPC405GP_GPIO0_TCR, greg); /* ... just do it */
  117. /* Setup FPGA for programming */
  118. grego &= ~cnfg; /* CONFIG line low */
  119. out32(PPC405GP_GPIO0_OR, grego);
  120. /*
  121. * Program the FPGA
  122. */
  123. printf("\n destination: 0x%lx ", (unsigned long) ndest);
  124. fpga_img_write(source, length, (unsigned short *) ndest);
  125. /* Done programming */
  126. grego |= cnfg; /* CONFIG line high */
  127. out32(PPC405GP_GPIO0_OR, grego);
  128. /* Turn output enable OFF for CNFG */
  129. greg = in32(PPC405GP_GPIO0_TCR); /* get tristate register */
  130. greg &= ~cnfg; /* CNFG tristate inactive */
  131. out32(PPC405GP_GPIO0_TCR, greg); /* ... just do it */
  132. /* Toggle IRQ/GPIO */
  133. greg = mfdcr(CPC0_CR0); /* get chip ctrl register */
  134. greg &= ~eirq; /* toggle irq/gpio */
  135. mtdcr(CPC0_CR0, greg); /* ... just do it */
  136. /* XXX - Next FPGA addr */
  137. ndest = (unsigned short *) ((char *) ndest + 0x00100000L);
  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 = Test DONE low */
  153. while (!(in32(PPC405GP_GPIO0_IR) & GPIO_XCV_DONE)) {
  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. * filename length is next short
  202. */
  203. fn_len = (*(unsigned short *) (buf + 12) & 0xff);
  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((char *) buf, "w7o", 3) != 0) || (len > 0x0007ffffL)
  212. || (len == 0))
  213. goto bad_image;
  214. /*
  215. * Calculate and Check the image's CRC.
  216. */
  217. calc_crc = crc32(0, xcv_buf, xcv_len);
  218. if (crc != calc_crc) {
  219. printf("\nfailed - bad CRC\n");
  220. goto done;
  221. }
  222. /* Output the file name */
  223. printf("file name : ");
  224. for (i = 0; i < fn_len; i++) {
  225. bufchar = fn_buf[+i];
  226. if (bufchar < ' ' || bufchar > '~')
  227. bufchar = '.';
  228. putc(bufchar);
  229. }
  230. /*
  231. * find rest of display data
  232. */
  233. ptr = 15; /* Offset to ncd filename
  234. length in fpga image */
  235. j = xcv_buf[ptr]; /* Get len of ncd filename */
  236. if (j > 32)
  237. goto bad_image;
  238. ptr = ptr + j + 3; /* skip ncd filename string +
  239. 3 bytes more bytes */
  240. /*
  241. * output target device string
  242. */
  243. j = xcv_buf[ptr++] - 1; /* len of targ str less term */
  244. if (j > 32)
  245. goto bad_image;
  246. printf("\n target : ");
  247. for (i = 0; i < j; i++) {
  248. bufchar = (xcv_buf[ptr++]);
  249. if (bufchar < ' ' || bufchar > '~')
  250. bufchar = '.';
  251. putc(bufchar);
  252. }
  253. /*
  254. * output compilation date string and time string
  255. */
  256. ptr += 3; /* skip 2 bytes */
  257. printf("\n synth time : ");
  258. j = (xcv_buf[ptr++] - 1); /* len of date str less term */
  259. if (j > 32)
  260. goto bad_image;
  261. for (i = 0; i < j; i++) {
  262. bufchar = (xcv_buf[ptr++]);
  263. if (bufchar < ' ' || bufchar > '~')
  264. bufchar = '.';
  265. putc(bufchar);
  266. }
  267. ptr += 3; /* Skip 2 bytes */
  268. printf(" - ");
  269. j = (xcv_buf[ptr++] - 1); /* slen = targ dev str len */
  270. if (j > 32)
  271. goto bad_image;
  272. for (i = 0; i < j; i++) {
  273. bufchar = (xcv_buf[ptr++]);
  274. if (bufchar < ' ' || bufchar > '~')
  275. bufchar = '.';
  276. putc(bufchar);
  277. }
  278. /*
  279. * output crc and length strings
  280. */
  281. printf("\n len & crc : 0x%lx 0x%lx", len, crc);
  282. /*
  283. * Program the FPGA.
  284. */
  285. retval = fpgaDownload((unsigned char *) xcv_buf, xcv_len,
  286. (unsigned short *) 0xfd000000L);
  287. return retval;
  288. bad_image:
  289. printf("\n BAD FPGA image format @ %lx\n",
  290. flash_info[1].start[0]);
  291. log_warn(ERR_XIMAGE);
  292. done:
  293. return retval;
  294. }
  295. void test_fpga(unsigned short *daddr)
  296. {
  297. int i;
  298. volatile unsigned short *ndest = daddr;
  299. for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
  300. #if defined(CONFIG_W7OLMG)
  301. ndest[0x7e] = 0x55aa;
  302. if (ndest[0x7e] != 0x55aa)
  303. log_warn(ERR_XRW1 + i);
  304. ndest[0x7e] = 0xaa55;
  305. if (ndest[0x7e] != 0xaa55)
  306. log_warn(ERR_XRW1 + i);
  307. ndest[0x7e] = 0xc318;
  308. if (ndest[0x7e] != 0xc318)
  309. log_warn(ERR_XRW1 + i);
  310. #elif defined(CONFIG_W7OLMC)
  311. ndest[0x800] = 0x55aa;
  312. ndest[0x801] = 0xaa55;
  313. ndest[0x802] = 0xc318;
  314. ndest[0x4800] = 0x55aa;
  315. ndest[0x4801] = 0xaa55;
  316. ndest[0x4802] = 0xc318;
  317. if ((ndest[0x800] != 0x55aa) ||
  318. (ndest[0x801] != 0xaa55) || (ndest[0x802] != 0xc318))
  319. log_warn(ERR_XRW1 + (2 * i)); /* Auto gen error code */
  320. if ((ndest[0x4800] != 0x55aa) ||
  321. (ndest[0x4801] != 0xaa55) || (ndest[0x4802] != 0xc318))
  322. log_warn(ERR_XRW2 + (2 * i)); /* Auto gen error code */
  323. #else
  324. #error "Unknown W7O board configuration"
  325. #endif
  326. }
  327. printf(" FPGA ready\n");
  328. return;
  329. }