1 |
39 |
julius |
/*$$HEADER*/
|
2 |
|
|
/******************************************************************************/
|
3 |
|
|
/* */
|
4 |
|
|
/* H E A D E R I N F O R M A T I O N */
|
5 |
|
|
/* */
|
6 |
|
|
/******************************************************************************/
|
7 |
|
|
|
8 |
|
|
// Project Name : OpenRISC Debug Proxy
|
9 |
|
|
// File Name : usb_functions.c
|
10 |
|
|
// Prepared By : jb
|
11 |
|
|
// Project Start : 2008-10-01
|
12 |
|
|
|
13 |
|
|
/*$$COPYRIGHT NOTICE*/
|
14 |
|
|
/******************************************************************************/
|
15 |
|
|
/* */
|
16 |
|
|
/* C O P Y R I G H T N O T I C E */
|
17 |
|
|
/* */
|
18 |
|
|
/******************************************************************************/
|
19 |
|
|
/*
|
20 |
|
|
This library is free software; you can redistribute it and/or
|
21 |
|
|
modify it under the terms of the GNU Lesser General Public
|
22 |
|
|
License as published by the Free Software Foundation;
|
23 |
|
|
version 2.1 of the License, a copy of which is available from
|
24 |
|
|
http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
|
25 |
|
|
|
26 |
|
|
This library is distributed in the hope that it will be useful,
|
27 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
28 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
29 |
|
|
Lesser General Public License for more details.
|
30 |
|
|
|
31 |
|
|
You should have received a copy of the GNU Lesser General Public
|
32 |
|
|
License along with this library; if not, write to the Free Software
|
33 |
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
34 |
|
|
*/
|
35 |
|
|
|
36 |
|
|
/*$$DESCRIPTION*/
|
37 |
|
|
/******************************************************************************/
|
38 |
|
|
/* */
|
39 |
|
|
/* D E S C R I P T I O N */
|
40 |
|
|
/* */
|
41 |
|
|
/******************************************************************************/
|
42 |
|
|
//
|
43 |
|
|
// Code calling the FT2232 JTAG MPSSE driver functions. Does a majority of the
|
44 |
|
|
// fiddly work when interfacing to the system via its debug module.
|
45 |
|
|
//
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/*$$CHANGE HISTORY*/
|
49 |
|
|
/******************************************************************************/
|
50 |
|
|
/* */
|
51 |
|
|
/* C H A N G E H I S T O R Y */
|
52 |
|
|
/* */
|
53 |
|
|
/******************************************************************************/
|
54 |
|
|
// Date Version Description
|
55 |
|
|
//------------------------------------------------------------------------
|
56 |
|
|
// 081101 First revision jb
|
57 |
|
|
|
58 |
|
|
#include <assert.h>
|
59 |
|
|
#include <stdio.h>
|
60 |
|
|
#include <ctype.h>
|
61 |
|
|
#include <string.h>
|
62 |
|
|
#include <stdlib.h>
|
63 |
|
|
#include <unistd.h>
|
64 |
|
|
#include <stdarg.h>
|
65 |
|
|
#include <sys/stat.h>
|
66 |
|
|
#include <sys/types.h>
|
67 |
|
|
|
68 |
|
|
#include <netinet/in.h>
|
69 |
|
|
|
70 |
|
|
#ifdef CYGWIN_COMPILE
|
71 |
|
|
#include <windows.h>
|
72 |
|
|
#include "win_FTCJTAG.h"
|
73 |
|
|
#else // We're on Linux or Mac OS X
|
74 |
|
|
#include "WinTypes.h" // We still use things from here in this file - TODO: remove this dependency
|
75 |
|
|
#include "FT2232cMpsseJtag.h"
|
76 |
|
|
#endif
|
77 |
|
|
|
78 |
|
|
#include "gdb.h"
|
79 |
|
|
|
80 |
|
|
#include "usb_functions.h"
|
81 |
|
|
|
82 |
|
|
#include "or_debug_proxy.h"
|
83 |
|
|
|
84 |
|
|
#include "usb_driver_calls.h"
|
85 |
|
|
|
86 |
|
|
// Global JTAG signals for reading/writing
|
87 |
|
|
DWORD dwNumBytesReturned = 0;
|
88 |
|
|
|
89 |
|
|
/* Crc of current read or written data. */
|
90 |
|
|
uint32_t crc_r, crc_w = 0;
|
91 |
|
|
|
92 |
|
|
/* Generates new crc, sending in new bit input_bit */
|
93 |
|
|
uint32_t crc_calc(uint32_t crc, uint32_t input_bit) {
|
94 |
|
|
uint32_t d = (input_bit) ? 0xfffffff : 0x0000000;
|
95 |
|
|
uint32_t crc_32 = ((crc >> 31)&1) ? 0xfffffff : 0x0000000;
|
96 |
|
|
crc <<= 1;
|
97 |
|
|
return crc ^ ((d ^ crc_32) & DBG_CRC_POLY);
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
// Speedy bit reversing algorithms for base2 lengths
|
101 |
|
|
// Thanks to: http://aggregate.org/MAGIC/#Bit%20Reversal
|
102 |
|
|
uint32_t bit_reverse_swar_2(uint32_t x)
|
103 |
|
|
{
|
104 |
|
|
x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
|
105 |
|
|
return x;
|
106 |
|
|
}
|
107 |
|
|
uint32_t bit_reverse_swar_4(uint32_t x)
|
108 |
|
|
{
|
109 |
|
|
x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
|
110 |
|
|
x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
|
111 |
|
|
return x;
|
112 |
|
|
}
|
113 |
|
|
static inline uint32_t bit_reverse_swar_8(uint32_t x)
|
114 |
|
|
{
|
115 |
|
|
x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
|
116 |
|
|
x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
|
117 |
|
|
x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
|
118 |
|
|
return x;
|
119 |
|
|
}
|
120 |
|
|
uint32_t bit_reverse_swar_16(uint32_t x)
|
121 |
|
|
{
|
122 |
|
|
x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
|
123 |
|
|
x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
|
124 |
|
|
x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
|
125 |
|
|
x=(((x&0xff00ff00)>>8)|((x&0x00ff00ff)<<8));
|
126 |
|
|
return x;
|
127 |
|
|
}
|
128 |
|
|
uint32_t bit_reverse_swar_32(uint32_t x)
|
129 |
|
|
{
|
130 |
|
|
x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
|
131 |
|
|
x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
|
132 |
|
|
x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
|
133 |
|
|
x=(((x&0xff00ff00)>>8)|((x&0x00ff00ff)<<8));
|
134 |
|
|
x=(((x&0xffff0000)>>16)|((x&0x0000ffff)<<16)); // We could be on 64-bit arch!
|
135 |
|
|
return x;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
uint32_t bit_reverse_data(uint32_t data, uint32_t length){
|
139 |
|
|
if (length == 2) return bit_reverse_swar_2(data);
|
140 |
|
|
if (length == 4) return bit_reverse_swar_4(data);
|
141 |
|
|
if (length == 8) return bit_reverse_swar_8(data);
|
142 |
|
|
if (length == 16) return bit_reverse_swar_16(data);
|
143 |
|
|
if (length == 32) return bit_reverse_swar_32(data);
|
144 |
|
|
// Long and laborious way - hopefully never gets called anymore!
|
145 |
|
|
uint32_t i,reverse=0;
|
146 |
|
|
for (i=0;i<length;i++) reverse |= (((data>>i)&1)<<(length-1-i));
|
147 |
|
|
return reverse;
|
148 |
|
|
}
|
149 |
|
|
// Constants that are used a lot, and were 5 bits, so might as well precalculate them
|
150 |
|
|
// These are from or_debug_proxy.h, so if the original values change these
|
151 |
|
|
// should be recalculated!!
|
152 |
|
|
const uint8_t DI_GO_5BITREVERSED = 0x00;
|
153 |
|
|
const uint8_t DI_READ_CMD_5BITREVERSED = 0x10;
|
154 |
|
|
const uint8_t DI_WRITE_CMD_5BITREVERSED = 0x08;
|
155 |
|
|
const uint8_t DI_READ_CTRL_5BITREVERSED = 0x18;
|
156 |
|
|
const uint8_t DI_WRITE_CTRL_5BITREVERSED = 0x04;
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
void usb_dbg_test() {
|
160 |
|
|
|
161 |
|
|
uint32_t npc, ppc, r1;
|
162 |
|
|
unsigned char stalled;
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
printf("Stalling or1k\n");
|
167 |
|
|
err = dbg_cpu0_write_ctrl(0, 0x01); // stall or1k
|
168 |
|
|
|
169 |
|
|
err = dbg_cpu0_read_ctrl(0, &stalled);
|
170 |
|
|
if (!(stalled & 0x1)) {
|
171 |
|
|
printf("or1k should be stalled\n"); // check stall or1k
|
172 |
|
|
exit(1);
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
/* Clear Debug Reason Register (DRR) 0x3015 */
|
176 |
|
|
err = dbg_cpu0_write((6 << 11) + 21, 0);
|
177 |
|
|
err = dbg_cpu0_read((0 << 11) + 16, &npc); /* Read NPC */
|
178 |
|
|
err = dbg_cpu0_read((0 << 11) + 18, &ppc); /* Read PPC */
|
179 |
|
|
err = dbg_cpu0_read(0x401, &r1); /* Read R1 */
|
180 |
|
|
|
181 |
|
|
if (err)
|
182 |
|
|
{
|
183 |
|
|
printf("Jtag error %d occured; exiting.", err);
|
184 |
|
|
FT2232_USB_JTAG_CloseDevice();
|
185 |
|
|
exit(1);
|
186 |
|
|
}
|
187 |
|
|
printf("Read npc = %.8x ppc = %.8x r1 = %.8x\n", npc, ppc, r1);
|
188 |
|
|
|
189 |
|
|
return;
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
/*
|
193 |
|
|
void ensure_or1k_stalled();
|
194 |
|
|
|
195 |
|
|
// Function to check if the processor is stalled, if not, stall it.
|
196 |
|
|
// this is useful in the event that GDB thinks the processor is stalled, but has, in fact
|
197 |
|
|
// been hard reset on the board and is running.
|
198 |
|
|
void ensure_or1k_stalled()
|
199 |
|
|
{
|
200 |
|
|
unsigned char stalled;
|
201 |
|
|
dbg_cpu0_read_ctrl(0, &stalled);
|
202 |
|
|
if ((stalled & 0x1) != 0x1)
|
203 |
|
|
{
|
204 |
|
|
if (DEBUG_CMDS)
|
205 |
|
|
printf("Processor not stalled, like we thought\n");
|
206 |
|
|
|
207 |
|
|
// Set the TAP controller to its OR1k chain
|
208 |
|
|
usb_set_tap_ir(JI_DEBUG);
|
209 |
|
|
current_chain = -1;
|
210 |
|
|
|
211 |
|
|
// Processor isn't stalled, contrary to what we though, so stall it
|
212 |
|
|
printf("Stalling or1k\n");
|
213 |
|
|
dbg_cpu0_write_ctrl(0, 0x01); // stall or1k
|
214 |
|
|
|
215 |
|
|
}
|
216 |
|
|
}
|
217 |
|
|
*/
|
218 |
|
|
/*---------------------------------------------------------------------------*/
|
219 |
|
|
/*!Write up to 32-bits to the JTAG bus via the USB device
|
220 |
|
|
|
221 |
|
|
Write up to 32-bits to the JTAG bus.
|
222 |
|
|
|
223 |
|
|
@param[in] stream This should store the data to be written to the bus
|
224 |
|
|
@param[in] num_bits Number of bits to write on the JTAG bus
|
225 |
|
|
@param[in] dwTapControllerState State to leave the JTAG TAP in when done */
|
226 |
|
|
/*---------------------------------------------------------------------------*/
|
227 |
|
|
void usb_write_stream (uint32_t stream, uint32_t num_bits, DWORD dwTapControllerState){
|
228 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
229 |
|
|
uint32_t i,num_bytes;
|
230 |
|
|
WriteDataByteBuffer WriteDataBuffer;
|
231 |
|
|
|
232 |
|
|
if ((num_bits/8)>0)num_bytes=(num_bits/8);
|
233 |
|
|
else num_bytes=1;
|
234 |
|
|
// First clear the buffer for the amount of data we're shifting in
|
235 |
|
|
// Bytewise copy the stream into WriteDataBuffer, LSB first
|
236 |
|
|
|
237 |
|
|
// This means if we want to send things MSb first, we need to pass them
|
238 |
|
|
// to this function wrapped with a call to bit_reverse_data(data,length)
|
239 |
|
|
|
240 |
|
|
for (i=0; i<(num_bytes)+1;i++)
|
241 |
|
|
WriteDataBuffer[i] = ((stream >>(8*i)) & (0xff));
|
242 |
|
|
|
243 |
|
|
// Now generate the CRC for what we're sending
|
244 |
|
|
// data should be presented MSb first (at bit0)
|
245 |
|
|
for (i=0;i<num_bits;i++)
|
246 |
|
|
crc_w = crc_calc(crc_w,((stream>>i)&1));
|
247 |
|
|
|
248 |
|
|
|
249 |
|
|
if (DEBUG_USB_DRVR_FUNCS)
|
250 |
|
|
{
|
251 |
|
|
printf("\nusb_write_stream: num_bytes=%d, WriteDataBuffer contents for %d bits:",num_bytes,num_bits);
|
252 |
|
|
for (i=0;i<num_bytes+1;i++)
|
253 |
|
|
printf("%x",WriteDataBuffer[num_bytes-i]);
|
254 |
|
|
printf("\n");
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
//Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, false, num_bits,
|
258 |
|
|
// &WriteDataBuffer, num_bytes, dwTapControllerState);
|
259 |
|
|
// Platform independant function call
|
260 |
|
|
Status = FT2232_USB_JTAG_WriteDataToExternalDevice(false, num_bits,
|
261 |
|
|
&WriteDataBuffer, num_bytes, dwTapControllerState);
|
262 |
|
|
|
263 |
|
|
if (Status != FTC_SUCCESS)
|
264 |
|
|
printf("Write to USB device failed: status code: %ld\n",Status);
|
265 |
|
|
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
/*---------------------------------------------------------------------------*/
|
269 |
|
|
/*!Read up to 32-bits off the JTAG bus via the USB device
|
270 |
|
|
|
271 |
|
|
The return value of this should remain uint32_t as we're returning all the
|
272 |
|
|
data in a signal variable. We never need more than 32-bits in a single read
|
273 |
|
|
when using this function.
|
274 |
|
|
|
275 |
|
|
@param[in] num_bits Number of bits to read off from the USB
|
276 |
|
|
@param[in] dwTapControllerState State to leave the JTAG TAP in when done
|
277 |
|
|
@return: The data read from the USB device */
|
278 |
|
|
/*---------------------------------------------------------------------------*/
|
279 |
|
|
uint32_t usb_read_stream(uint32_t num_bits, DWORD dwTapControllerState){
|
280 |
|
|
ReadDataByteBuffer ReadDataBuffer;
|
281 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
282 |
|
|
uint32_t returnVal =0;
|
283 |
|
|
uint32_t i;
|
284 |
|
|
|
285 |
|
|
// Platform independant driver call
|
286 |
|
|
Status = FT2232_USB_JTAG_ReadDataFromExternalDevice(false, num_bits, &ReadDataBuffer, &dwNumBytesReturned, dwTapControllerState);
|
287 |
|
|
|
288 |
|
|
if (DEBUG_USB_DRVR_FUNCS)
|
289 |
|
|
printf("usb_read_stream: returned Status: 0x%lx from reading %u bits, \n",
|
290 |
|
|
Status,num_bits);
|
291 |
|
|
|
292 |
|
|
if (Status == FTC_SUCCESS){
|
293 |
|
|
|
294 |
|
|
for(i=0;i<num_bits;i++){
|
295 |
|
|
returnVal |= ((ReadDataBuffer[i/8]>>(i%8))&0x1)<<(num_bits-1-i);
|
296 |
|
|
}
|
297 |
|
|
}
|
298 |
|
|
// Generate the CRC for read
|
299 |
|
|
for (i=0;i<num_bits;i++)
|
300 |
|
|
crc_r = crc_calc(crc_r, ((returnVal>>(num_bits-1-i))&1));
|
301 |
|
|
|
302 |
|
|
return returnVal;
|
303 |
|
|
}
|
304 |
|
|
|
305 |
|
|
/* Sets TAP instruction register */
|
306 |
|
|
void usb_set_tap_ir(uint32_t ir) {
|
307 |
|
|
|
308 |
|
|
WriteDataByteBuffer WriteDataBuffer;
|
309 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
310 |
|
|
|
311 |
|
|
WriteDataBuffer[0] = ir;
|
312 |
|
|
|
313 |
|
|
// Using global ftHandle, writing to TAP instruction register 4 bits,
|
314 |
|
|
//Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, true,
|
315 |
|
|
//JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
|
316 |
|
|
// Platform independant driver call
|
317 |
|
|
|
318 |
|
|
Status = FT2232_USB_JTAG_WriteDataToExternalDevice(true, JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
|
319 |
|
|
|
320 |
|
|
if (DEBUG_USB_DRVR_FUNCS)
|
321 |
|
|
printf("USB JTAG write of %x to instruction register returned Status: 0x%lx\n",
|
322 |
|
|
ir, Status);
|
323 |
|
|
current_chain = -1;
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
static uint32_t id_read_at_reset = 0;
|
327 |
|
|
|
328 |
|
|
/* Resets JTAG, and sets DEBUG scan chain */
|
329 |
|
|
int usb_dbg_reset() {
|
330 |
|
|
|
331 |
|
|
// uint32_t err;
|
332 |
|
|
uint32_t id;
|
333 |
|
|
uint32_t reinit_count=0;
|
334 |
|
|
retry_jtag_init:
|
335 |
|
|
if (init_usb_jtag() > 0)
|
336 |
|
|
return DBG_ERR_CRC;
|
337 |
|
|
|
338 |
|
|
// Set ID code instruction in IR
|
339 |
|
|
usb_set_tap_ir(JI_IDCODE);
|
340 |
|
|
|
341 |
|
|
// Now read out the IDCODE for the device
|
342 |
|
|
id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
|
343 |
|
|
|
344 |
|
|
// if read ID was rubbish retry init - this is probably NOT the best way to do this...
|
345 |
|
|
if ((id == 0xffffffff) | (id == 0x00000002) | (id == 0x00000000)) {
|
346 |
|
|
//pFT2232cMpsseJtag->JTAG_CloseDevice(gFtHandle);
|
347 |
|
|
// Platform independant driver call
|
348 |
|
|
FT2232_USB_JTAG_CloseDevice();
|
349 |
|
|
if (reinit_count++ > 4){
|
350 |
|
|
printf("JTAG TAP ID read error. Unable to detect TAP controller. \nPlease ensure debugger is connected to target correctly.\n");
|
351 |
|
|
exit(1);
|
352 |
|
|
}
|
353 |
|
|
goto retry_jtag_init;
|
354 |
|
|
}
|
355 |
|
|
|
356 |
|
|
printf("JTAG ID = %08x\n", id & 0xffffffff);
|
357 |
|
|
|
358 |
|
|
/* select debug scan chain and stay in it forever */
|
359 |
|
|
usb_set_tap_ir(JI_DEBUG);
|
360 |
|
|
|
361 |
|
|
id_read_at_reset = id; // Store this to check later if there's errors
|
362 |
|
|
|
363 |
|
|
current_chain = -1;
|
364 |
|
|
return DBG_ERR_OK;
|
365 |
|
|
}
|
366 |
|
|
|
367 |
|
|
/* counts retries and returns zero if we should abort */
|
368 |
|
|
static int retry_no = 0;
|
369 |
|
|
int retry_do() {
|
370 |
|
|
|
371 |
|
|
unsigned char stalled;
|
372 |
45 |
julius |
int tap_id_reads = 0;
|
373 |
39 |
julius |
|
374 |
|
|
//printf("RETRY\n");
|
375 |
|
|
if (retry_no == 0)
|
376 |
|
|
printf("Communication error. Retrying\n");
|
377 |
|
|
|
378 |
|
|
retry_no++;
|
379 |
|
|
|
380 |
|
|
// Odds are if we're having a communication problem, we should
|
381 |
|
|
// just reconnect to the processor. It's probably just been reset.
|
382 |
|
|
/*
|
383 |
|
|
FT2232_USB_JTAG_CloseDevice();// Close the device
|
384 |
|
|
|
385 |
|
|
if (init_usb_jtag() > 0)
|
386 |
|
|
{
|
387 |
|
|
if (retry_no >= NUM_HARD_RETRIES)
|
388 |
|
|
return 1;
|
389 |
|
|
else
|
390 |
|
|
return 0;
|
391 |
|
|
}
|
392 |
|
|
*/
|
393 |
|
|
|
394 |
|
|
// Try a readback of the TAP ID
|
395 |
45 |
julius |
read_tap:
|
396 |
39 |
julius |
// Set ID code instruction in IR
|
397 |
|
|
usb_set_tap_ir(JI_IDCODE);
|
398 |
|
|
|
399 |
|
|
// Now read out the IDCODE for the device
|
400 |
|
|
uint32_t id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
|
401 |
|
|
|
402 |
|
|
//Return the chain to DEBUG mode
|
403 |
|
|
usb_set_tap_ir(JI_DEBUG);
|
404 |
|
|
|
405 |
|
|
if((id&0xffffffff) != (id_read_at_reset&0xffffffff))
|
406 |
|
|
{
|
407 |
45 |
julius |
if (tap_id_reads == 10)
|
408 |
|
|
{
|
409 |
|
|
// Pretty big problem - can't even read the ID of the TAP anymore
|
410 |
|
|
// So return error
|
411 |
|
|
printf("Unable to read JTAG TAP ID - read %08x, expected %08x\n",
|
412 |
|
|
id, id_read_at_reset);
|
413 |
|
|
|
414 |
|
|
return 1;
|
415 |
|
|
}
|
416 |
|
|
|
417 |
|
|
tap_id_reads++;
|
418 |
|
|
|
419 |
|
|
goto read_tap;
|
420 |
39 |
julius |
}
|
421 |
|
|
|
422 |
|
|
current_chain = -1;
|
423 |
|
|
|
424 |
|
|
if (retry_no == 1)
|
425 |
|
|
sleep(1);
|
426 |
|
|
|
427 |
|
|
else if ( retry_no < NUM_SOFT_RETRIES)
|
428 |
|
|
return 0; // Just attempt again
|
429 |
|
|
|
430 |
|
|
if ((retry_no >= NUM_SOFT_RETRIES) && (retry_no < NUM_SOFT_RETRIES + NUM_HARD_RETRIES) )
|
431 |
|
|
{
|
432 |
|
|
// Attempt a stall of the processor and then we'll try again
|
433 |
|
|
//usb_dbg_test();
|
434 |
|
|
printf("Resetting or1k\n");
|
435 |
|
|
err = dbg_cpu0_write_ctrl(0, 0x02); // reset or1k
|
436 |
|
|
|
437 |
|
|
printf("Stalling or1k\n");
|
438 |
|
|
err = dbg_cpu0_write_ctrl(0, 0x01); // stall or1k
|
439 |
|
|
|
440 |
|
|
err = dbg_cpu0_read_ctrl(0, &stalled);
|
441 |
|
|
if (!(stalled & 0x1)) {
|
442 |
|
|
printf("or1k should be stalled\n"); // check stall or1k
|
443 |
|
|
FT2232_USB_JTAG_CloseDevice();// Close the device
|
444 |
|
|
exit(1);
|
445 |
|
|
}
|
446 |
|
|
|
447 |
|
|
//retry_no++;
|
448 |
|
|
return 0;
|
449 |
|
|
}
|
450 |
|
|
else // Unable to get the processor going again, return 1
|
451 |
|
|
return 1;
|
452 |
|
|
|
453 |
|
|
}
|
454 |
|
|
|
455 |
|
|
/* resets retry counter */
|
456 |
|
|
void retry_ok() {
|
457 |
|
|
retry_no = 0;
|
458 |
|
|
}
|
459 |
|
|
|
460 |
|
|
/* Sets scan chain. */
|
461 |
|
|
int usb_dbg_set_chain(int chain) {
|
462 |
|
|
uint32_t status, crc_generated, crc_read;
|
463 |
|
|
dbg_chain = chain;
|
464 |
|
|
|
465 |
|
|
try_again:
|
466 |
|
|
if (current_chain == chain) return DBG_ERR_OK;
|
467 |
|
|
current_chain = -1;
|
468 |
|
|
if (DEBUG_CMDS) printf("\n");
|
469 |
|
|
if (DEBUG_CMDS) printf("set_chain %i\n", chain);
|
470 |
|
|
|
471 |
|
|
crc_w = 0xffffffff; // reset crc write variable
|
472 |
|
|
|
473 |
|
|
// CRC generated in usb_read/write_stream functions
|
474 |
|
|
|
475 |
|
|
usb_write_stream((((bit_reverse_data(chain,DBGCHAIN_SIZE) & 0xf) << 1) | 1),
|
476 |
|
|
DBGCHAIN_SIZE+1 , PAUSE_TEST_DATA_REGISTER_STATE);
|
477 |
|
|
|
478 |
|
|
usb_write_stream(bit_reverse_data(crc_w, DBG_CRC_SIZE),DBG_CRC_SIZE,
|
479 |
|
|
PAUSE_TEST_DATA_REGISTER_STATE);
|
480 |
|
|
|
481 |
|
|
crc_r = 0xffffffff; // reset the crc_r variable
|
482 |
|
|
|
483 |
|
|
status = (usb_read_stream(DC_STATUS_SIZE,
|
484 |
|
|
PAUSE_TEST_DATA_REGISTER_STATE) & 0xf);
|
485 |
|
|
|
486 |
|
|
crc_generated = crc_r;
|
487 |
|
|
|
488 |
|
|
crc_read = usb_read_stream(DBG_CRC_SIZE, RUN_TEST_IDLE_STATE);
|
489 |
|
|
//printf("%x %x %x\n",status, crc_generated, crc_read);
|
490 |
|
|
|
491 |
|
|
/* CRCs must match, otherwise retry */
|
492 |
|
|
if (crc_read != crc_generated) {
|
493 |
|
|
if (!retry_do()) goto try_again;
|
494 |
|
|
else return DBG_ERR_CRC;
|
495 |
|
|
}
|
496 |
|
|
/* we should read expected status value, otherwise retry */
|
497 |
|
|
if (status != 0) {
|
498 |
|
|
if (!retry_do()) goto try_again;
|
499 |
|
|
else return status;
|
500 |
|
|
}
|
501 |
|
|
|
502 |
|
|
/* reset retry counter */
|
503 |
|
|
retry_ok();
|
504 |
|
|
|
505 |
|
|
current_chain = chain;
|
506 |
|
|
return DBG_ERR_OK;
|
507 |
|
|
}
|
508 |
|
|
|
509 |
|
|
|
510 |
|
|
|
511 |
|
|
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
|
512 |
|
|
int usb_dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
|
513 |
|
|
uint32_t i,status, crc_generated, crc_read;
|
514 |
|
|
|
515 |
|
|
// JTAG driver things
|
516 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
517 |
|
|
WriteDataByteBuffer WriteDataBuffer;
|
518 |
|
|
ReadDataByteBuffer ReadDataBuffer;
|
519 |
|
|
|
520 |
|
|
try_again:
|
521 |
|
|
usb_dbg_set_chain(dbg_chain);
|
522 |
|
|
if (DEBUG_CMDS) printf("\n");
|
523 |
|
|
if (DEBUG_CMDS) printf("comm %d %d %8x \n", type,len,adr);
|
524 |
|
|
|
525 |
|
|
//Calculate CRCs first
|
526 |
|
|
crc_w = 0xffffffff;
|
527 |
|
|
|
528 |
|
|
for (i=0;i<DBGCHAIN_SIZE+1;i++)
|
529 |
|
|
crc_w = crc_calc(crc_w, (DI_WRITE_CMD_5BITREVERSED >> i) & 1);
|
530 |
|
|
//crc_w = crc_calc(crc_w,
|
531 |
|
|
// ((bit_reverse_data((DI_WRITE_CMD & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
|
532 |
|
|
|
533 |
|
|
for (i=0;i<4;i++)
|
534 |
|
|
crc_w = crc_calc(crc_w,((bit_reverse_data(type,4))>>i)&1);
|
535 |
|
|
|
536 |
|
|
for (i=0;i<32;i++)
|
537 |
|
|
crc_w = crc_calc(crc_w,((bit_reverse_data(adr,32))>>i)&1);
|
538 |
|
|
|
539 |
|
|
assert(len > 0);
|
540 |
|
|
for (i=0;i<16;i++)
|
541 |
|
|
crc_w = crc_calc(crc_w,((bit_reverse_data(len-1,16))>>i)&1);
|
542 |
|
|
|
543 |
|
|
|
544 |
|
|
|
545 |
|
|
// Now pack the write data buffer
|
546 |
|
|
// 1-bit 0, 4-bits cmd, 4-bit type, 32-bit adr, 32-bit CRC
|
547 |
|
|
// [0]
|
548 |
|
|
//bits 0-4
|
549 |
|
|
WriteDataBuffer[0]=(DI_WRITE_CMD_5BITREVERSED);
|
550 |
|
|
//bits 5-7
|
551 |
|
|
WriteDataBuffer[0] |= ((bit_reverse_data(type,4)&0x7)<<5);
|
552 |
|
|
// [1]
|
553 |
|
|
// bit 0 - last bit of type
|
554 |
|
|
WriteDataBuffer[1] = ((bit_reverse_data(type,4)&0x08)>>3);
|
555 |
|
|
//bits 1-7 - first 7 bits of adr
|
556 |
|
|
WriteDataBuffer[1] |= ((bit_reverse_data(adr,32)&0x07f)<<1);
|
557 |
|
|
//[2-4] 24 bits of adr
|
558 |
|
|
for(i=0;i<3;i++)
|
559 |
|
|
WriteDataBuffer[2+i] = (bit_reverse_data(adr,32)>>(7+(i*8)))&0xff;
|
560 |
|
|
// [5] last bit of adr in bit 0, first 7 bits of len-1 follow
|
561 |
|
|
WriteDataBuffer[5] = (bit_reverse_data(adr,32)>>31)&1;
|
562 |
|
|
WriteDataBuffer[5] |= (bit_reverse_data(len-1,16)&0x7f)<<1;
|
563 |
|
|
// [6] bits 7-14 of (len-1)
|
564 |
|
|
WriteDataBuffer[6] = (bit_reverse_data(len-1,16)>>7)&0xff;
|
565 |
|
|
// [7] - last bit of len-1 and first 7 bits of the CRC
|
566 |
|
|
WriteDataBuffer[7] = (bit_reverse_data(len-1,16)>>15)&1;
|
567 |
|
|
//Reverse the CRC first
|
568 |
|
|
crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
|
569 |
|
|
WriteDataBuffer[7] |= (crc_w&0x7f)<<1;
|
570 |
|
|
//[8-10] next 24 bits (7-30) of crc_w
|
571 |
|
|
WriteDataBuffer[8] = (crc_w>>7)&0xff;
|
572 |
|
|
WriteDataBuffer[9] = (crc_w>>15)&0xff;
|
573 |
|
|
WriteDataBuffer[10] = (crc_w>>23)&0xff;
|
574 |
|
|
//[11] final bit of crc_w
|
575 |
|
|
WriteDataBuffer[11] = (crc_w>>31)&1;
|
576 |
|
|
|
577 |
|
|
// Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
|
578 |
|
|
// Platform independant driver call
|
579 |
|
|
Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
|
580 |
|
|
|
581 |
|
|
if (Status != FTC_SUCCESS)
|
582 |
|
|
printf("USB write fail - code %ld\b",Status);
|
583 |
|
|
|
584 |
|
|
// Now look through the read data
|
585 |
|
|
|
586 |
|
|
// From bit1 of ReadDataBuffer[11] we should have our 4-bit status
|
587 |
|
|
status = (ReadDataBuffer[11] >> 1) & 0xf;
|
588 |
|
|
|
589 |
|
|
// Now extract the received CRC
|
590 |
|
|
crc_read = 0;
|
591 |
|
|
//first 3 bits (0-2)
|
592 |
|
|
crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
|
593 |
|
|
// middle 3 bytes (bits 3 to 26)
|
594 |
|
|
for (i=0;i<3;i++)
|
595 |
|
|
crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
|
596 |
|
|
// last 5 bits from ReadDataBuffer[15]
|
597 |
|
|
crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
|
598 |
|
|
|
599 |
|
|
// Now calculate CRC on status
|
600 |
|
|
crc_r = 0xffffffff;
|
601 |
|
|
for(i=0;i<DC_STATUS_SIZE;i++)
|
602 |
|
|
crc_r = crc_calc(crc_r, (status>>i)&1);
|
603 |
|
|
|
604 |
|
|
crc_generated = crc_r;
|
605 |
|
|
// Now bit reverse status and crc_read as we unpacked them
|
606 |
|
|
// with the MSb going to the LSb
|
607 |
|
|
status = bit_reverse_data(status, DC_STATUS_SIZE);
|
608 |
|
|
crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
|
609 |
|
|
|
610 |
|
|
//printf("%x %x %x\n", status, crc_read, crc_generated);
|
611 |
|
|
/* CRCs must match, otherwise retry */
|
612 |
|
|
if (crc_read != crc_generated) {
|
613 |
|
|
//exit(1);//remove later
|
614 |
|
|
if (!retry_do()) goto try_again;
|
615 |
|
|
else return DBG_ERR_CRC;
|
616 |
|
|
}
|
617 |
|
|
/* we should read expected status value, otherwise retry */
|
618 |
|
|
if (status != 0) {
|
619 |
|
|
//exit(1);//remove later
|
620 |
|
|
if (!retry_do()) goto try_again;
|
621 |
|
|
else return status;
|
622 |
|
|
}
|
623 |
|
|
/* reset retry counter */
|
624 |
|
|
retry_ok();
|
625 |
|
|
|
626 |
|
|
return DBG_ERR_OK;
|
627 |
|
|
}
|
628 |
|
|
|
629 |
|
|
|
630 |
|
|
/* writes a ctrl reg */
|
631 |
|
|
int usb_dbg_ctrl(uint32_t reset, uint32_t stall) {
|
632 |
|
|
uint32_t i,status, crc_generated, crc_read;
|
633 |
|
|
|
634 |
|
|
// JTAG driver things
|
635 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
636 |
|
|
WriteDataByteBuffer WriteDataBuffer;
|
637 |
|
|
ReadDataByteBuffer ReadDataBuffer;
|
638 |
|
|
|
639 |
|
|
try_again:
|
640 |
|
|
usb_dbg_set_chain(dbg_chain);
|
641 |
|
|
if (DEBUG_CMDS) printf("\n");
|
642 |
|
|
if (DEBUG_CMDS) printf("ctrl\n");
|
643 |
|
|
if (DEBUG_CMDS) printf("reset %x stall %x\n", reset, stall);
|
644 |
|
|
|
645 |
|
|
crc_w = 0xffffffff;
|
646 |
|
|
// Try packing everyhing we want to send into one write buffer
|
647 |
|
|
//Calculate CRCs first
|
648 |
|
|
for (i=0;i<DBGCHAIN_SIZE+1;i++)
|
649 |
|
|
crc_w = crc_calc(crc_w, (DI_WRITE_CTRL_5BITREVERSED>>i)&1);
|
650 |
|
|
//crc_w = crc_calc(crc_w,
|
651 |
|
|
// ((bit_reverse_data((DI_WRITE_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
|
652 |
|
|
crc_w = crc_calc(crc_w,(reset&1));
|
653 |
|
|
crc_w = crc_calc(crc_w,(stall&1));
|
654 |
|
|
for (i=0;i<50;i++)
|
655 |
|
|
crc_w = crc_calc(crc_w,0);
|
656 |
|
|
|
657 |
|
|
|
658 |
|
|
|
659 |
|
|
// Now pack the write data buffer
|
660 |
|
|
// 1-bit 0, 4-bits cmd, 52-bits CPU control register data (only first 2 matter)
|
661 |
|
|
//bits 0-4
|
662 |
|
|
WriteDataBuffer[0]=(DI_WRITE_CTRL_5BITREVERSED);
|
663 |
|
|
// bit 5
|
664 |
|
|
WriteDataBuffer[0] |= (reset)<<(DBGCHAIN_SIZE+1);
|
665 |
|
|
// bit 6
|
666 |
|
|
WriteDataBuffer[0] |= (stall)<<(DBGCHAIN_SIZE+2);
|
667 |
|
|
// Clear the next 48 bits
|
668 |
|
|
for (i=1; i<16;i++) //actually clear more than just the next 50 bits
|
669 |
|
|
WriteDataBuffer[i] = 0;
|
670 |
|
|
|
671 |
|
|
//Reverse the CRC first
|
672 |
|
|
crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
|
673 |
|
|
//Now load in the CRC, but take note of fact
|
674 |
|
|
// that bit 0 of buffer[7] is last 0 from cmd register data
|
675 |
|
|
// fill up from WriteDataBuffer[7-11]
|
676 |
|
|
for (i=0;i<4;i++)
|
677 |
|
|
WriteDataBuffer[7+i] = ((crc_w<<1)>>(i*8))&0xff;
|
678 |
|
|
//Final bit, shift in and make sure is the only thing int he buffer
|
679 |
|
|
WriteDataBuffer[11]=0|((crc_w>>31)&1);
|
680 |
|
|
|
681 |
|
|
|
682 |
|
|
//Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
|
683 |
|
|
// Platform independant driver call
|
684 |
|
|
Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
|
685 |
|
|
|
686 |
|
|
|
687 |
|
|
if (Status != FTC_SUCCESS)
|
688 |
|
|
printf("USB write fail - code %ld\b",Status);
|
689 |
|
|
|
690 |
|
|
// Now look through the read data
|
691 |
|
|
|
692 |
|
|
// From bit1 of ReadDataBuffer[11] we should have our 4-bit status
|
693 |
|
|
status = (ReadDataBuffer[11] >> 1) & 0xf;
|
694 |
|
|
|
695 |
|
|
// Now extract the received CRC
|
696 |
|
|
crc_read = 0;
|
697 |
|
|
//first 3 bits (0-2)
|
698 |
|
|
crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
|
699 |
|
|
// middle 3 bytes (bits 3 to 26)
|
700 |
|
|
for (i=0;i<3;i++)
|
701 |
|
|
crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
|
702 |
|
|
// last 5 bits from ReadDataBuffer[15]
|
703 |
|
|
crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
|
704 |
|
|
|
705 |
|
|
|
706 |
|
|
// Now calculate CRC on status and crc_read
|
707 |
|
|
crc_r = 0xffffffff;
|
708 |
|
|
for(i=0;i<DC_STATUS_SIZE;i++)
|
709 |
|
|
crc_r = crc_calc(crc_r, (status>>i)&1);
|
710 |
|
|
|
711 |
|
|
crc_generated = crc_r;
|
712 |
|
|
// Now bit reverse status and crc_read as we unpacked them
|
713 |
|
|
// with the MSb going to the LSb
|
714 |
|
|
status = bit_reverse_data(status, DC_STATUS_SIZE);
|
715 |
|
|
crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
|
716 |
|
|
|
717 |
|
|
/* CRCs must match, otherwise retry */
|
718 |
|
|
//printf("%x %x %x\n", status, crc_read, crc_generated);
|
719 |
|
|
if (crc_read != crc_generated) {
|
720 |
|
|
//exit(1);//Remove later!!
|
721 |
|
|
if (!retry_do()) goto try_again;
|
722 |
|
|
else return DBG_ERR_CRC;
|
723 |
|
|
}
|
724 |
|
|
/* we should read expected status value, otherwise retry */
|
725 |
|
|
if (status != 0) {
|
726 |
|
|
//exit(1);//Remove later!!
|
727 |
|
|
if (!retry_do()) goto try_again;
|
728 |
|
|
else return status;
|
729 |
|
|
}
|
730 |
|
|
|
731 |
|
|
/* reset retry counter */
|
732 |
|
|
retry_ok();
|
733 |
|
|
return DBG_ERR_OK;
|
734 |
|
|
}
|
735 |
|
|
|
736 |
|
|
|
737 |
|
|
/* reads control register */
|
738 |
|
|
int usb_dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
|
739 |
|
|
uint32_t i, status, crc_generated, crc_read;
|
740 |
|
|
|
741 |
|
|
// JTAG driver things
|
742 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
743 |
|
|
WriteDataByteBuffer WriteDataBuffer;
|
744 |
|
|
ReadDataByteBuffer ReadDataBuffer;
|
745 |
|
|
|
746 |
|
|
|
747 |
|
|
try_again:
|
748 |
|
|
usb_dbg_set_chain(dbg_chain);
|
749 |
|
|
if (DEBUG_CMDS) printf("\n");
|
750 |
|
|
if (DEBUG_CMDS) printf("ctrl_read\n");
|
751 |
|
|
|
752 |
|
|
crc_w = 0xffffffff;
|
753 |
|
|
// Try packing everyhing we want to send into one write buffer
|
754 |
|
|
//Calculate CRCs first
|
755 |
|
|
for (i=0;i<DBGCHAIN_SIZE+1;i++)
|
756 |
|
|
crc_w = crc_calc(crc_w, (DI_READ_CTRL_5BITREVERSED>>i)&1);
|
757 |
|
|
//crc_w = crc_calc(crc_w,
|
758 |
|
|
//((bit_reverse_data((DI_READ_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
|
759 |
|
|
|
760 |
|
|
|
761 |
|
|
// Now pack the write data buffer
|
762 |
|
|
// 1-bit 0, 4-bits cmd, 32-bit CRC
|
763 |
|
|
//bits 0-4
|
764 |
|
|
WriteDataBuffer[0]=(DI_READ_CTRL_5BITREVERSED);
|
765 |
|
|
// Clear the next 48 bits
|
766 |
|
|
for (i=1; i<16;i++) //actually clear more than just the next 50 bits
|
767 |
|
|
WriteDataBuffer[i] = 0;
|
768 |
|
|
|
769 |
|
|
//Reverse the CRC first
|
770 |
|
|
crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
|
771 |
|
|
//Now load in the CRC
|
772 |
|
|
//First 3 bits go in last 3 bits of buffer[0]
|
773 |
|
|
WriteDataBuffer[0] |= (crc_w & 0x7)<<5;
|
774 |
|
|
// The rest of crc_w goes in buffer[1-4]
|
775 |
|
|
for (i=0;i<3;i++)
|
776 |
|
|
WriteDataBuffer[1+i] = ((crc_w>>3)>>(i*8))&0xff;
|
777 |
|
|
//Final bit of write buffer with CRC
|
778 |
|
|
WriteDataBuffer[4] = ((crc_w>>3)>>(24))&0x1f;
|
779 |
|
|
|
780 |
|
|
|
781 |
|
|
|
782 |
|
|
//Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
|
783 |
|
|
// Platform independant driver call
|
784 |
|
|
Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
|
785 |
|
|
if (Status != FTC_SUCCESS)
|
786 |
|
|
printf("USB read fail - code %ld\b",Status);
|
787 |
|
|
|
788 |
|
|
// Now look through the read data
|
789 |
|
|
//0 - 1+4-bit status, 3-bits CRC
|
790 |
|
|
//1,2,3 - CRC
|
791 |
|
|
//4 - first 5 bits CRC, last 3, control reg bits (first 3)
|
792 |
|
|
//5,6,7,8,9,10 - control reg data (48 bits)
|
793 |
|
|
//11 - bit0 - control reg data, bit 1-4 status bits, bits 5-7 CRC
|
794 |
|
|
//12, 13 14 - CRC
|
795 |
|
|
// 15 bits 0-4 CRC
|
796 |
|
|
// Starting from bit1 of ReadDataBuffer[11] we should have our 4-bit status
|
797 |
|
|
status = (ReadDataBuffer[11] >> 1) & 0xf;
|
798 |
|
|
|
799 |
|
|
//reset bit should be in ReadDataBuffer[4] as bit 5
|
800 |
|
|
*reset = (ReadDataBuffer[4] >> 5) & 1;
|
801 |
|
|
//stalled bit should be in ReadDataBuffer[4] as bit 6
|
802 |
|
|
*stall = (ReadDataBuffer[4] >> 6) & 1;
|
803 |
|
|
// Now extract the received CRC
|
804 |
|
|
crc_read = 0;
|
805 |
|
|
//first 3 bits (0-2) of CRC are in bits 5-7 of ReadDataBuffer[11]
|
806 |
|
|
crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
|
807 |
|
|
// middle 3 bytes (bits 3 to 26)
|
808 |
|
|
for (i=0;i<3;i++)
|
809 |
|
|
crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
|
810 |
|
|
// last 5 bits from ReadDataBuffer[15]
|
811 |
|
|
crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
|
812 |
|
|
|
813 |
|
|
if (DEBUG_CMDS) printf("reset bit %x stalled bit %x:\n",
|
814 |
|
|
((ReadDataBuffer[4] >> 5) & 1), ((ReadDataBuffer[4] >> 6) & 1));
|
815 |
|
|
|
816 |
|
|
// Now calculate CRC on status and crc_read
|
817 |
|
|
crc_r = 0xffffffff;
|
818 |
|
|
|
819 |
|
|
crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 5) & 1));
|
820 |
|
|
crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 6) & 1));
|
821 |
|
|
for(i=0;i<50;i++)
|
822 |
|
|
crc_r = crc_calc(crc_r,0);
|
823 |
|
|
for(i=0;i<DC_STATUS_SIZE;i++)
|
824 |
|
|
crc_r = crc_calc(crc_r, (status>>i)&1);
|
825 |
|
|
|
826 |
|
|
crc_generated = crc_r;
|
827 |
|
|
// Now bit reverse status and crc_read as we unpacked them
|
828 |
|
|
// with the MSb going to the LSb
|
829 |
|
|
status = bit_reverse_data(status, DC_STATUS_SIZE);
|
830 |
|
|
crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
|
831 |
|
|
|
832 |
|
|
/* CRCs must match, otherwise retry */
|
833 |
|
|
//printf("%x %x %x\n", status, crc_generated, crc_read);
|
834 |
|
|
if (crc_read != crc_generated) {
|
835 |
|
|
if (!retry_do()) goto try_again;
|
836 |
|
|
else return DBG_ERR_CRC;
|
837 |
|
|
}
|
838 |
|
|
/* we should read expected status value, otherwise retry */
|
839 |
|
|
if (status != 0) {
|
840 |
|
|
if (!retry_do()) goto try_again;
|
841 |
|
|
else return status;
|
842 |
|
|
}
|
843 |
|
|
/* reset retry counter */
|
844 |
|
|
retry_ok();
|
845 |
|
|
return DBG_ERR_OK;
|
846 |
|
|
}
|
847 |
|
|
|
848 |
|
|
|
849 |
|
|
/* issues a burst read/write */
|
850 |
|
|
int usb_dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
|
851 |
|
|
uint32_t status, crc_generated, crc_read;
|
852 |
|
|
int i,j;
|
853 |
|
|
uint8_t data_byte;
|
854 |
|
|
|
855 |
|
|
// JTAG driver things
|
856 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
857 |
|
|
WriteDataByteBuffer WriteDataBuffer;
|
858 |
|
|
ReadDataByteBuffer ReadDataBuffer;
|
859 |
|
|
|
860 |
|
|
try_again:
|
861 |
|
|
usb_dbg_set_chain(dbg_chain);
|
862 |
|
|
if (DEBUG_CMDS) printf("\n");
|
863 |
|
|
if (DEBUG_CMDS) printf("go len is %d, read is %d\n", len, read);
|
864 |
|
|
|
865 |
|
|
crc_w = 0xffffffff;
|
866 |
|
|
// Try packing everyhing we want to send into one write buffer
|
867 |
|
|
//Calculate CRCs first
|
868 |
|
|
for (i=0;i<DBGCHAIN_SIZE+1;i++)
|
869 |
|
|
crc_w = crc_calc(crc_w, (DI_GO_5BITREVERSED>>i)&1);
|
870 |
|
|
//crc_w = crc_calc(crc_w,
|
871 |
|
|
// ((bit_reverse_data((DI_GO & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
|
872 |
|
|
// Set first 5 bits of WriteDataBuffer up to
|
873 |
|
|
// be the GO command and 0 first bit
|
874 |
|
|
WriteDataBuffer[0]=DI_GO_5BITREVERSED;
|
875 |
|
|
|
876 |
|
|
if (read)
|
877 |
|
|
{
|
878 |
|
|
// Do GO command for a read
|
879 |
|
|
// 0 then 4-bit go command, then 32-bit CRC for the last 5 bits
|
880 |
|
|
// Then read (len+1)*8 + 4-bit status + 32-bit CRC
|
881 |
|
|
|
882 |
|
|
|
883 |
|
|
// Reverse crc_w
|
884 |
|
|
crc_w = bit_reverse_data(crc_w,DBG_CRC_SIZE);
|
885 |
|
|
|
886 |
|
|
// Now pack in the 32-bit CRC as we're not
|
887 |
|
|
// writing anything else after it
|
888 |
|
|
//First 3 bits of each byte go in last 3 bits of buffer[i],
|
889 |
|
|
// next 5 go in buffer[i+1]
|
890 |
|
|
for(i=0;i<4;i++){
|
891 |
|
|
WriteDataBuffer[i] |= ((crc_w>>(i*8))&0x07)<<5;
|
892 |
|
|
WriteDataBuffer[i+1] = ((crc_w>>(i*8))>>3)&0x1f;
|
893 |
|
|
}
|
894 |
|
|
|
895 |
|
|
// Should have data up to WriteDataBuffer[4] bit 4
|
896 |
|
|
// Read data should start at ReadDataBuffer[4] bit 5
|
897 |
|
|
|
898 |
|
|
//Clear the rest of the write buffer
|
899 |
|
|
for(i=5;i<(10+len);i++)
|
900 |
|
|
WriteDataBuffer[i]=0;
|
901 |
|
|
}
|
902 |
|
|
if (!read){
|
903 |
|
|
// If we're writing we put in the 5 command bits, (len+1)*8 data bits,
|
904 |
|
|
// and then the 32-bit CRC do first 3 bits, then next 5 bits in
|
905 |
|
|
// each of the for loops iterations
|
906 |
|
|
for(i=0;i<len;i++){
|
907 |
|
|
|
908 |
|
|
data_byte = bit_reverse_swar_8(data[i]);
|
909 |
|
|
|
910 |
|
|
WriteDataBuffer[i] |= ((data_byte&0x07)<<5);
|
911 |
|
|
WriteDataBuffer[i+1] = ((data_byte>>3)&0x1f);
|
912 |
|
|
|
913 |
|
|
// Now update the CRC
|
914 |
|
|
for(j=0;j<8;j++)
|
915 |
|
|
crc_w = crc_calc(crc_w, (data_byte>>j)&1);
|
916 |
|
|
|
917 |
|
|
}
|
918 |
|
|
|
919 |
|
|
// Reverse crc_w
|
920 |
|
|
crc_w = bit_reverse_data(crc_w,DBG_CRC_SIZE);
|
921 |
|
|
|
922 |
|
|
// If we have len=4 for example, we will write to
|
923 |
|
|
// WriteDataBuffer[4]'s first 5 bits
|
924 |
|
|
|
925 |
|
|
// So now load in the 32-bit CRC from there
|
926 |
|
|
for(i=0;i<4;i++){
|
927 |
|
|
WriteDataBuffer[len+i] |= ((crc_w>>(i*8))&0x07)<<5;
|
928 |
|
|
WriteDataBuffer[len+i+1] = ((crc_w>>(i*8))>>3)&0x1f;
|
929 |
|
|
}
|
930 |
|
|
// Should have data up to WriteDataBuffer[4+len] bit 4
|
931 |
|
|
// Read data should start at ReadDataBuffer[4+len] bit 5
|
932 |
|
|
|
933 |
|
|
}
|
934 |
|
|
|
935 |
|
|
//Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,(5+(len*8)+32+36), &WriteDataBuffer, (6+len+4), &ReadDataBuffer,&dwNumBytesReturned,RUN_TEST_IDLE_STATE);
|
936 |
|
|
Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,(5+(len*8)+32+36), &WriteDataBuffer, (6+len+4), &ReadDataBuffer,&dwNumBytesReturned,RUN_TEST_IDLE_STATE);
|
937 |
|
|
|
938 |
|
|
if (Status != FTC_SUCCESS)
|
939 |
|
|
printf("USB write fail - code %ld\n",Status);
|
940 |
|
|
|
941 |
|
|
crc_r = 0xffffffff;
|
942 |
|
|
|
943 |
|
|
if (read){
|
944 |
|
|
// Look through our data, starting from ReadDataBuffer[4] bit 5
|
945 |
|
|
// We receive len bytes, starting at ReadDataBuffer[4] bit 5, so
|
946 |
|
|
// unpack like so
|
947 |
|
|
if (DEBUG_USB_DRVR_FUNCS) printf("USB read data buffer: ");
|
948 |
|
|
for(i=0;i<len;i++){
|
949 |
|
|
// get first 3 bits
|
950 |
|
|
data[i] = (ReadDataBuffer[4+i]>>5)&0x07;
|
951 |
|
|
// then next 5 from next ReadDataBuffer byte
|
952 |
|
|
data[i] |= (ReadDataBuffer[4+i+1]&0x1f)<<3;
|
953 |
|
|
|
954 |
|
|
// Now calculate the CRC for this byte
|
955 |
|
|
for(j=0;j<8;j++)
|
956 |
|
|
crc_r = crc_calc(crc_r, (data[i]>>j)&1);
|
957 |
|
|
|
958 |
|
|
// Now bit reverse the byte as it was read in MSb first but
|
959 |
|
|
// written to LSb first
|
960 |
|
|
data[i] = bit_reverse_data(data[i],8);
|
961 |
|
|
|
962 |
|
|
if (DEBUG_USB_DRVR_FUNCS) printf("%2x",data[i]);
|
963 |
|
|
}
|
964 |
|
|
if (DEBUG_USB_DRVR_FUNCS) printf("\n");
|
965 |
|
|
|
966 |
|
|
// Should be up to ReadDataBuffer[4+len] bit 5 for data
|
967 |
|
|
status = (ReadDataBuffer[4+len]>>5)&0x07;
|
968 |
|
|
status |= (ReadDataBuffer[4+len+1]&1)<<3;
|
969 |
|
|
// Now get out crc_read
|
970 |
|
|
crc_read = 0;
|
971 |
|
|
for(i=0;i<4;i++){
|
972 |
|
|
crc_read |= ((ReadDataBuffer[4+len+1+i]>>1)&0x7f)<<(i*8);
|
973 |
|
|
crc_read |= ((ReadDataBuffer[4+len+1+i+1]&0x1)<<7)<<(i*8);
|
974 |
|
|
}
|
975 |
|
|
|
976 |
|
|
for(i=0;i<4;i++)
|
977 |
|
|
crc_r = crc_calc(crc_r, (status>>i)&1);
|
978 |
|
|
}
|
979 |
|
|
if (!read){
|
980 |
|
|
// Just extract our 4-bits of status and CRC
|
981 |
|
|
status = (ReadDataBuffer[4+len]>>5)&0x07;
|
982 |
|
|
status |= (ReadDataBuffer[4+len+1]&1)<<3;
|
983 |
|
|
|
984 |
|
|
// extract crc_read from the ReadDataBuffer
|
985 |
|
|
crc_read = 0;
|
986 |
|
|
for(i=0;i<4;i++){
|
987 |
|
|
crc_read |= ((ReadDataBuffer[4+len+1+i]>>1)&0x7f)<<(i*8);
|
988 |
|
|
crc_read |= ((ReadDataBuffer[4+len+1+i+1]&1)<<7)<<(i*8);
|
989 |
|
|
}
|
990 |
|
|
// Calculate our own CRC from the status value
|
991 |
|
|
for(i=0;i<4;i++)
|
992 |
|
|
crc_r = crc_calc(crc_r, (status>>i)&1);
|
993 |
|
|
|
994 |
|
|
}
|
995 |
|
|
|
996 |
|
|
crc_generated = crc_r;
|
997 |
|
|
// Now bit reverse status and crc_read as we unpacked them
|
998 |
|
|
// with the MSb going to the LSb
|
999 |
|
|
status = bit_reverse_data(status, DC_STATUS_SIZE);
|
1000 |
|
|
crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
|
1001 |
|
|
|
1002 |
|
|
|
1003 |
|
|
/* CRCs must match, otherwise retry */
|
1004 |
|
|
if (crc_read != crc_generated) {
|
1005 |
|
|
if (DEBUG_CMDS || DEBUG_USB_DRVR_FUNCS)printf("CRC mismatch: %x %x %x\n", status, crc_read, crc_generated);
|
1006 |
|
|
if (!retry_do()) goto try_again;
|
1007 |
|
|
else return DBG_ERR_CRC;
|
1008 |
|
|
}
|
1009 |
|
|
/* we should read expected status value, otherwise retry */
|
1010 |
|
|
if (status != 0) {
|
1011 |
|
|
if (!retry_do()) goto try_again;
|
1012 |
|
|
else return status;
|
1013 |
|
|
}
|
1014 |
|
|
|
1015 |
|
|
retry_ok();
|
1016 |
|
|
return DBG_ERR_OK;
|
1017 |
|
|
}
|
1018 |
|
|
|
1019 |
|
|
/* read a word from wishbone */
|
1020 |
|
|
int usb_dbg_wb_read32(uint32_t adr, uint32_t *data) {
|
1021 |
|
|
// uint32_t err;
|
1022 |
|
|
if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
|
1023 |
46 |
julius |
if ((err = usb_dbg_command(DBG_WB_READ32, adr, 4))) return err;
|
1024 |
39 |
julius |
if ((err = usb_dbg_go((unsigned char*)data, 4, 1))) return err;
|
1025 |
46 |
julius |
//*data = ntohl(*data);
|
1026 |
39 |
julius |
return err;
|
1027 |
|
|
}
|
1028 |
|
|
|
1029 |
|
|
/* write a word to wishbone */
|
1030 |
46 |
julius |
int usb_dbg_wb_write8(uint32_t adr, uint8_t data) {
|
1031 |
|
|
if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
|
1032 |
|
|
if ((err = usb_dbg_command(DBG_WB_WRITE8, adr, 1))) return err;
|
1033 |
|
|
if ((err = usb_dbg_go((unsigned char*)&data, 1, 0))) return err;
|
1034 |
|
|
return DBG_ERR_OK;
|
1035 |
|
|
}
|
1036 |
|
|
|
1037 |
|
|
|
1038 |
|
|
/* write a word to wishbone */
|
1039 |
39 |
julius |
int usb_dbg_wb_write32(uint32_t adr, uint32_t data) {
|
1040 |
|
|
if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
|
1041 |
46 |
julius |
if ((err = usb_dbg_command(DBG_WB_WRITE32, adr, 4))) return err;
|
1042 |
39 |
julius |
if ((err = usb_dbg_go((unsigned char*)&data, 4, 0))) return err;
|
1043 |
|
|
return DBG_ERR_OK;
|
1044 |
|
|
}
|
1045 |
|
|
|
1046 |
|
|
/* read a block from wishbone */
|
1047 |
|
|
int usb_dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
|
1048 |
|
|
if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
|
1049 |
46 |
julius |
if ((err = usb_dbg_command(DBG_WB_READ32, adr, len))) return err;
|
1050 |
39 |
julius |
if ((err = usb_dbg_go((unsigned char*)data, len, 1))) return err;
|
1051 |
|
|
return DBG_ERR_OK;
|
1052 |
|
|
}
|
1053 |
|
|
|
1054 |
|
|
|
1055 |
|
|
/* write a block to wishbone */
|
1056 |
|
|
int usb_dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
|
1057 |
|
|
if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
|
1058 |
46 |
julius |
if ((err = usb_dbg_command(DBG_WB_WRITE32, adr, len))) return err;
|
1059 |
39 |
julius |
if ((err = usb_dbg_go((unsigned char*)data, len, 0))) return err;
|
1060 |
|
|
return DBG_ERR_OK;
|
1061 |
|
|
}
|
1062 |
|
|
|
1063 |
|
|
|
1064 |
|
|
/* read a register from cpu */
|
1065 |
|
|
int usb_dbg_cpu0_read(uint32_t adr, uint32_t *data) {
|
1066 |
|
|
// uint32_t err;
|
1067 |
|
|
if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
|
1068 |
46 |
julius |
if ((err = usb_dbg_command(DBG_CPU_READ, adr, 4))) return err;
|
1069 |
39 |
julius |
if ((err = usb_dbg_go((unsigned char*)data, 4, 1))) return err;
|
1070 |
|
|
*data = ntohl(*data);
|
1071 |
|
|
return DBG_ERR_OK;
|
1072 |
|
|
}
|
1073 |
|
|
|
1074 |
|
|
/* write a cpu register */
|
1075 |
|
|
int usb_dbg_cpu0_write(uint32_t adr, uint32_t data) {
|
1076 |
|
|
// uint32_t err;
|
1077 |
|
|
data = ntohl(data);
|
1078 |
|
|
if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
|
1079 |
46 |
julius |
if ((err = usb_dbg_command(DBG_CPU_WRITE, adr, 4))) return err;
|
1080 |
39 |
julius |
if ((err = usb_dbg_go((unsigned char*)&data, 4, 0))) return err;
|
1081 |
|
|
return DBG_ERR_OK;
|
1082 |
|
|
}
|
1083 |
|
|
|
1084 |
|
|
/* write a cpu module register */
|
1085 |
|
|
int usb_dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
|
1086 |
|
|
// no ensuring that or1k is stalled here, becuase we're call this from that function
|
1087 |
|
|
if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
|
1088 |
|
|
if ((err = usb_dbg_ctrl(data >> 1, data & 0x1))) return err;
|
1089 |
|
|
return DBG_ERR_OK;
|
1090 |
|
|
}
|
1091 |
|
|
|
1092 |
|
|
/* read a register from cpu module */
|
1093 |
|
|
int usb_dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
|
1094 |
|
|
// no ensuring that or1k is stalled here, becuase we're call this from that function
|
1095 |
|
|
uint32_t r, s;
|
1096 |
|
|
if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
|
1097 |
|
|
if ((err = usb_dbg_ctrl_read(&r, &s))) return err;
|
1098 |
|
|
*data = (r << 1) | s;
|
1099 |
|
|
return DBG_ERR_OK;
|
1100 |
|
|
}
|
1101 |
|
|
|
1102 |
|
|
/* Function to close the device handle. Is called when closing the app */
|
1103 |
|
|
void usb_close_device_handle()
|
1104 |
|
|
{
|
1105 |
|
|
// try unstalling the processor before quitting
|
1106 |
|
|
dbg_cpu0_write_ctrl(0, 0x00); // unstall or1k
|
1107 |
|
|
FT2232_USB_JTAG_CloseDevice();
|
1108 |
|
|
}
|