3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
6 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
8 * ispVM functions adapted from Lattice's ispmVMEmbedded code:
9 * Copyright 2009 Lattice Semiconductor Corp.
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 static lattice_board_specific_func
*pfns
;
37 static const char *fpga_image
;
38 static unsigned long read_bytes
;
39 static unsigned long bufsize
;
40 static unsigned short expectedCRC
;
43 * External variables and functions declared in ivm_core.c module.
45 extern unsigned short g_usCalculatedCRC
;
46 extern unsigned short g_usDataType
;
47 extern unsigned char *g_pucIntelBuffer
;
48 extern unsigned char *g_pucHeapMemory
;
49 extern unsigned short g_iHeapCounter
;
50 extern unsigned short g_iHEAPSize
;
51 extern unsigned short g_usIntelDataIndex
;
52 extern unsigned short g_usIntelBufferSize
;
53 extern char *const g_szSupportedVersions
[];
59 * Users must implement a delay to observe a_usTimeDelay, where
60 * bit 15 of the a_usTimeDelay defines the unit.
64 * a_usTimeDelay = 0x0001 = 1 microsecond delay.
65 * a_usTimeDelay = 0x8001 = 1 millisecond delay.
67 * This subroutine is called upon to provide a delay from 1 millisecond to a few
68 * hundreds milliseconds each time.
69 * It is understood that due to a_usTimeDelay is defined as unsigned short, a 16
70 * bits integer, this function is restricted to produce a delay to 64000
71 * micro-seconds or 32000 milli-second maximum. The VME file will never pass on
72 * to this function a delay time > those maximum number. If it needs more than
73 * those maximum, the VME file will launch the delay function several times to
74 * realize a larger delay time cummulatively.
75 * It is perfectly alright to provide a longer delay than required. It is not
76 * acceptable if the delay is shorter.
78 void ispVMDelay(unsigned short delay
)
81 delay
= (delay
& ~0x8000) * 1000;
85 void writePort(unsigned char a_ucPins
, unsigned char a_ucValue
)
87 a_ucValue
= a_ucValue
? 1 : 0;
91 pfns
->jtag_set_tdi(a_ucValue
);
94 pfns
->jtag_set_tck(a_ucValue
);
97 pfns
->jtag_set_tms(a_ucValue
);
100 printf("%s: requested unknown pin\n", __func__
);
104 unsigned char readPort(void)
106 return pfns
->jtag_get_tdo();
111 writePort(g_ucPinTCK
, 0x01);
112 writePort(g_ucPinTCK
, 0x00);
115 void calibration(void)
117 /* Apply 2 pulses to TCK. */
118 writePort(g_ucPinTCK
, 0x00);
119 writePort(g_ucPinTCK
, 0x01);
120 writePort(g_ucPinTCK
, 0x00);
121 writePort(g_ucPinTCK
, 0x01);
122 writePort(g_ucPinTCK
, 0x00);
126 /* Apply 2 pulses to TCK. */
127 writePort(g_ucPinTCK
, 0x01);
128 writePort(g_ucPinTCK
, 0x00);
129 writePort(g_ucPinTCK
, 0x01);
130 writePort(g_ucPinTCK
, 0x00);
136 * Returns a byte to the caller. The returned byte depends on the
137 * g_usDataType register. If the HEAP_IN bit is set, then the byte
138 * is returned from the HEAP. If the LHEAP_IN bit is set, then
139 * the byte is returned from the intelligent buffer. Otherwise,
140 * the byte is returned directly from the VME file.
142 unsigned char GetByte(void)
144 unsigned char ucData
;
145 unsigned int block_size
= 4 * 1024;
147 if (g_usDataType
& HEAP_IN
) {
150 * Get data from repeat buffer.
153 if (g_iHeapCounter
> g_iHEAPSize
) {
162 ucData
= g_pucHeapMemory
[g_iHeapCounter
++];
163 } else if (g_usDataType
& LHEAP_IN
) {
166 * Get data from intel buffer.
169 if (g_usIntelDataIndex
>= g_usIntelBufferSize
) {
173 ucData
= g_pucIntelBuffer
[g_usIntelDataIndex
++];
175 if (read_bytes
== bufsize
) {
178 ucData
= *fpga_image
++;
181 if (!(read_bytes
% block_size
)) {
182 printf("Downloading FPGA %ld/%ld completed\r",
187 if (expectedCRC
!= 0) {
188 ispVMCalculateCRC32(ucData
);
195 signed char ispVM(void)
197 char szFileVersion
[9] = { 0 };
198 signed char cRetCode
= 0;
199 signed char cIndex
= 0;
200 signed char cVersionIndex
= 0;
201 unsigned char ucReadByte
= 0;
204 g_pucHeapMemory
= NULL
;
207 g_usIntelDataIndex
= 0;
208 g_usIntelBufferSize
= 0;
209 g_usCalculatedCRC
= 0;
211 ucReadByte
= GetByte();
212 switch (ucReadByte
) {
214 crc
= (unsigned char)GetByte();
219 for (cIndex
= 0; cIndex
< 8; cIndex
++)
220 szFileVersion
[cIndex
] = GetByte();
224 szFileVersion
[0] = (signed char) ucReadByte
;
225 for (cIndex
= 1; cIndex
< 8; cIndex
++)
226 szFileVersion
[cIndex
] = GetByte();
233 * Compare the VME file version against the supported version.
237 for (cVersionIndex
= 0; g_szSupportedVersions
[cVersionIndex
] != 0;
239 for (cIndex
= 0; cIndex
< 8; cIndex
++) {
240 if (szFileVersion
[cIndex
] !=
241 g_szSupportedVersions
[cVersionIndex
][cIndex
]) {
242 cRetCode
= VME_VERSION_FAILURE
;
254 return VME_VERSION_FAILURE
;
257 printf("VME file checked: starting downloading to FPGA\n");
261 cRetCode
= ispVMCode();
267 if (cRetCode
== 0 && expectedCRC
!= 0 &&
268 (expectedCRC
!= g_usCalculatedCRC
)) {
269 printf("Expected CRC: 0x%.4X\n", expectedCRC
);
270 printf("Calculated CRC: 0x%.4X\n", g_usCalculatedCRC
);
271 return VME_CRC_FAILURE
;
276 static int lattice_validate(Lattice_desc
*desc
, const char *fn
)
281 if ((desc
->family
> min_lattice_type
) &&
282 (desc
->family
< max_lattice_type
)) {
283 if ((desc
->iface
> min_lattice_iface_type
) &&
284 (desc
->iface
< max_lattice_iface_type
)) {
288 printf("%s: NULL part size\n", fn
);
291 printf("%s: Invalid Interface type, %d\n",
295 printf("%s: Invalid family type, %d\n",
299 printf("%s: NULL descriptor!\n", fn
);
305 int lattice_load(Lattice_desc
*desc
, const void *buf
, size_t bsize
)
307 int ret_val
= FPGA_FAIL
;
309 if (!lattice_validate(desc
, (char *)__func__
)) {
310 printf("%s: Invalid device descriptor\n", __func__
);
312 pfns
= desc
->iface_fns
;
314 switch (desc
->family
) {
319 debug("%s: Launching the Lattice ISPVME Loader:"
320 " addr %p size 0x%lx...\n",
321 __func__
, fpga_image
, bufsize
);
324 printf("%s: error %d downloading FPGA image\n",
327 puts("FPGA downloaded successfully\n");
330 printf("%s: Unsupported family type, %d\n",
331 __func__
, desc
->family
);
338 int lattice_dump(Lattice_desc
*desc
, const void *buf
, size_t bsize
)
340 puts("Dump not supported for Lattice FPGA\n");
346 int lattice_info(Lattice_desc
*desc
)
348 int ret_val
= FPGA_FAIL
;
350 if (lattice_validate(desc
, (char *)__func__
)) {
351 printf("Family: \t");
352 switch (desc
->family
) {
356 /* Add new family types here */
358 printf("Unknown family type, %d\n", desc
->family
);
361 puts("Interface type:\t");
362 switch (desc
->iface
) {
363 case lattice_jtag_mode
:
366 /* Add new interface types here */
368 printf("Unsupported interface type, %d\n", desc
->iface
);
371 printf("Device Size: \t%d bytes\n",
374 if (desc
->iface_fns
) {
375 printf("Device Function Table @ 0x%p\n",
377 switch (desc
->family
) {
380 /* Add new family types here */
385 puts("No Device Function Table.\n");
389 printf("Model: \t%s\n", desc
->desc
);
391 ret_val
= FPGA_SUCCESS
;
393 printf("%s: Invalid device descriptor\n", __func__
);