lattice.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * (C) Copyright 2010
  3. * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
  4. *
  5. * (C) Copyright 2002
  6. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  7. *
  8. * ispVM functions adapted from Lattice's ispmVMEmbedded code:
  9. * Copyright 2009 Lattice Semiconductor Corp.
  10. *
  11. * SPDX-License-Identifier: GPL-2.0+
  12. */
  13. #include <common.h>
  14. #include <malloc.h>
  15. #include <fpga.h>
  16. #include <lattice.h>
  17. static lattice_board_specific_func *pfns;
  18. static const char *fpga_image;
  19. static unsigned long read_bytes;
  20. static unsigned long bufsize;
  21. static unsigned short expectedCRC;
  22. /*
  23. * External variables and functions declared in ivm_core.c module.
  24. */
  25. extern unsigned short g_usCalculatedCRC;
  26. extern unsigned short g_usDataType;
  27. extern unsigned char *g_pucIntelBuffer;
  28. extern unsigned char *g_pucHeapMemory;
  29. extern unsigned short g_iHeapCounter;
  30. extern unsigned short g_iHEAPSize;
  31. extern unsigned short g_usIntelDataIndex;
  32. extern unsigned short g_usIntelBufferSize;
  33. extern char *const g_szSupportedVersions[];
  34. /*
  35. * ispVMDelay
  36. *
  37. * Users must implement a delay to observe a_usTimeDelay, where
  38. * bit 15 of the a_usTimeDelay defines the unit.
  39. * 1 = milliseconds
  40. * 0 = microseconds
  41. * Example:
  42. * a_usTimeDelay = 0x0001 = 1 microsecond delay.
  43. * a_usTimeDelay = 0x8001 = 1 millisecond delay.
  44. *
  45. * This subroutine is called upon to provide a delay from 1 millisecond to a few
  46. * hundreds milliseconds each time.
  47. * It is understood that due to a_usTimeDelay is defined as unsigned short, a 16
  48. * bits integer, this function is restricted to produce a delay to 64000
  49. * micro-seconds or 32000 milli-second maximum. The VME file will never pass on
  50. * to this function a delay time > those maximum number. If it needs more than
  51. * those maximum, the VME file will launch the delay function several times to
  52. * realize a larger delay time cummulatively.
  53. * It is perfectly alright to provide a longer delay than required. It is not
  54. * acceptable if the delay is shorter.
  55. */
  56. void ispVMDelay(unsigned short delay)
  57. {
  58. if (delay & 0x8000)
  59. delay = (delay & ~0x8000) * 1000;
  60. udelay(delay);
  61. }
  62. void writePort(unsigned char a_ucPins, unsigned char a_ucValue)
  63. {
  64. a_ucValue = a_ucValue ? 1 : 0;
  65. switch (a_ucPins) {
  66. case g_ucPinTDI:
  67. pfns->jtag_set_tdi(a_ucValue);
  68. break;
  69. case g_ucPinTCK:
  70. pfns->jtag_set_tck(a_ucValue);
  71. break;
  72. case g_ucPinTMS:
  73. pfns->jtag_set_tms(a_ucValue);
  74. break;
  75. default:
  76. printf("%s: requested unknown pin\n", __func__);
  77. }
  78. }
  79. unsigned char readPort(void)
  80. {
  81. return pfns->jtag_get_tdo();
  82. }
  83. void sclock(void)
  84. {
  85. writePort(g_ucPinTCK, 0x01);
  86. writePort(g_ucPinTCK, 0x00);
  87. }
  88. void calibration(void)
  89. {
  90. /* Apply 2 pulses to TCK. */
  91. writePort(g_ucPinTCK, 0x00);
  92. writePort(g_ucPinTCK, 0x01);
  93. writePort(g_ucPinTCK, 0x00);
  94. writePort(g_ucPinTCK, 0x01);
  95. writePort(g_ucPinTCK, 0x00);
  96. ispVMDelay(0x8001);
  97. /* Apply 2 pulses to TCK. */
  98. writePort(g_ucPinTCK, 0x01);
  99. writePort(g_ucPinTCK, 0x00);
  100. writePort(g_ucPinTCK, 0x01);
  101. writePort(g_ucPinTCK, 0x00);
  102. }
  103. /*
  104. * GetByte
  105. *
  106. * Returns a byte to the caller. The returned byte depends on the
  107. * g_usDataType register. If the HEAP_IN bit is set, then the byte
  108. * is returned from the HEAP. If the LHEAP_IN bit is set, then
  109. * the byte is returned from the intelligent buffer. Otherwise,
  110. * the byte is returned directly from the VME file.
  111. */
  112. unsigned char GetByte(void)
  113. {
  114. unsigned char ucData;
  115. unsigned int block_size = 4 * 1024;
  116. if (g_usDataType & HEAP_IN) {
  117. /*
  118. * Get data from repeat buffer.
  119. */
  120. if (g_iHeapCounter > g_iHEAPSize) {
  121. /*
  122. * Data over-run.
  123. */
  124. return 0xFF;
  125. }
  126. ucData = g_pucHeapMemory[g_iHeapCounter++];
  127. } else if (g_usDataType & LHEAP_IN) {
  128. /*
  129. * Get data from intel buffer.
  130. */
  131. if (g_usIntelDataIndex >= g_usIntelBufferSize) {
  132. return 0xFF;
  133. }
  134. ucData = g_pucIntelBuffer[g_usIntelDataIndex++];
  135. } else {
  136. if (read_bytes == bufsize) {
  137. return 0xFF;
  138. }
  139. ucData = *fpga_image++;
  140. read_bytes++;
  141. if (!(read_bytes % block_size)) {
  142. printf("Downloading FPGA %ld/%ld completed\r",
  143. read_bytes,
  144. bufsize);
  145. }
  146. if (expectedCRC != 0) {
  147. ispVMCalculateCRC32(ucData);
  148. }
  149. }
  150. return ucData;
  151. }
  152. signed char ispVM(void)
  153. {
  154. char szFileVersion[9] = { 0 };
  155. signed char cRetCode = 0;
  156. signed char cIndex = 0;
  157. signed char cVersionIndex = 0;
  158. unsigned char ucReadByte = 0;
  159. unsigned short crc;
  160. g_pucHeapMemory = NULL;
  161. g_iHeapCounter = 0;
  162. g_iHEAPSize = 0;
  163. g_usIntelDataIndex = 0;
  164. g_usIntelBufferSize = 0;
  165. g_usCalculatedCRC = 0;
  166. expectedCRC = 0;
  167. ucReadByte = GetByte();
  168. switch (ucReadByte) {
  169. case FILE_CRC:
  170. crc = (unsigned char)GetByte();
  171. crc <<= 8;
  172. crc |= GetByte();
  173. expectedCRC = crc;
  174. for (cIndex = 0; cIndex < 8; cIndex++)
  175. szFileVersion[cIndex] = GetByte();
  176. break;
  177. default:
  178. szFileVersion[0] = (signed char) ucReadByte;
  179. for (cIndex = 1; cIndex < 8; cIndex++)
  180. szFileVersion[cIndex] = GetByte();
  181. break;
  182. }
  183. /*
  184. *
  185. * Compare the VME file version against the supported version.
  186. *
  187. */
  188. for (cVersionIndex = 0; g_szSupportedVersions[cVersionIndex] != 0;
  189. cVersionIndex++) {
  190. for (cIndex = 0; cIndex < 8; cIndex++) {
  191. if (szFileVersion[cIndex] !=
  192. g_szSupportedVersions[cVersionIndex][cIndex]) {
  193. cRetCode = VME_VERSION_FAILURE;
  194. break;
  195. }
  196. cRetCode = 0;
  197. }
  198. if (cRetCode == 0) {
  199. break;
  200. }
  201. }
  202. if (cRetCode < 0) {
  203. return VME_VERSION_FAILURE;
  204. }
  205. printf("VME file checked: starting downloading to FPGA\n");
  206. ispVMStart();
  207. cRetCode = ispVMCode();
  208. ispVMEnd();
  209. ispVMFreeMem();
  210. puts("\n");
  211. if (cRetCode == 0 && expectedCRC != 0 &&
  212. (expectedCRC != g_usCalculatedCRC)) {
  213. printf("Expected CRC: 0x%.4X\n", expectedCRC);
  214. printf("Calculated CRC: 0x%.4X\n", g_usCalculatedCRC);
  215. return VME_CRC_FAILURE;
  216. }
  217. return cRetCode;
  218. }
  219. static int lattice_validate(Lattice_desc *desc, const char *fn)
  220. {
  221. int ret_val = false;
  222. if (desc) {
  223. if ((desc->family > min_lattice_type) &&
  224. (desc->family < max_lattice_type)) {
  225. if ((desc->iface > min_lattice_iface_type) &&
  226. (desc->iface < max_lattice_iface_type)) {
  227. if (desc->size) {
  228. ret_val = true;
  229. } else {
  230. printf("%s: NULL part size\n", fn);
  231. }
  232. } else {
  233. printf("%s: Invalid Interface type, %d\n",
  234. fn, desc->iface);
  235. }
  236. } else {
  237. printf("%s: Invalid family type, %d\n",
  238. fn, desc->family);
  239. }
  240. } else {
  241. printf("%s: NULL descriptor!\n", fn);
  242. }
  243. return ret_val;
  244. }
  245. int lattice_load(Lattice_desc *desc, const void *buf, size_t bsize)
  246. {
  247. int ret_val = FPGA_FAIL;
  248. if (!lattice_validate(desc, (char *)__func__)) {
  249. printf("%s: Invalid device descriptor\n", __func__);
  250. } else {
  251. pfns = desc->iface_fns;
  252. switch (desc->family) {
  253. case Lattice_XP2:
  254. fpga_image = buf;
  255. read_bytes = 0;
  256. bufsize = bsize;
  257. debug("%s: Launching the Lattice ISPVME Loader:"
  258. " addr %p size 0x%lx...\n",
  259. __func__, fpga_image, bufsize);
  260. ret_val = ispVM();
  261. if (ret_val)
  262. printf("%s: error %d downloading FPGA image\n",
  263. __func__, ret_val);
  264. else
  265. puts("FPGA downloaded successfully\n");
  266. break;
  267. default:
  268. printf("%s: Unsupported family type, %d\n",
  269. __func__, desc->family);
  270. }
  271. }
  272. return ret_val;
  273. }
  274. int lattice_dump(Lattice_desc *desc, const void *buf, size_t bsize)
  275. {
  276. puts("Dump not supported for Lattice FPGA\n");
  277. return FPGA_FAIL;
  278. }
  279. int lattice_info(Lattice_desc *desc)
  280. {
  281. int ret_val = FPGA_FAIL;
  282. if (lattice_validate(desc, (char *)__func__)) {
  283. printf("Family: \t");
  284. switch (desc->family) {
  285. case Lattice_XP2:
  286. puts("XP2\n");
  287. break;
  288. /* Add new family types here */
  289. default:
  290. printf("Unknown family type, %d\n", desc->family);
  291. }
  292. puts("Interface type:\t");
  293. switch (desc->iface) {
  294. case lattice_jtag_mode:
  295. puts("JTAG Mode\n");
  296. break;
  297. /* Add new interface types here */
  298. default:
  299. printf("Unsupported interface type, %d\n", desc->iface);
  300. }
  301. printf("Device Size: \t%d bytes\n",
  302. desc->size);
  303. if (desc->iface_fns) {
  304. printf("Device Function Table @ 0x%p\n",
  305. desc->iface_fns);
  306. switch (desc->family) {
  307. case Lattice_XP2:
  308. break;
  309. /* Add new family types here */
  310. default:
  311. break;
  312. }
  313. } else {
  314. puts("No Device Function Table.\n");
  315. }
  316. if (desc->desc)
  317. printf("Model: \t%s\n", desc->desc);
  318. ret_val = FPGA_SUCCESS;
  319. } else {
  320. printf("%s: Invalid device descriptor\n", __func__);
  321. }
  322. return ret_val;
  323. }