lattice.c 8.2 KB

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