OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or_debug_proxy/] [src/] [usb_functions.c] - Blame information for rev 59

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 47 julius
// Constants that are used a lot, and were 5 bits, so might as well 
150
// precalculate them to save reversing them each time.
151 39 julius
// These are from or_debug_proxy.h, so if the original values change these
152
// should be recalculated!!
153
const uint8_t DI_GO_5BITREVERSED = 0x00;
154
const uint8_t DI_READ_CMD_5BITREVERSED = 0x10;
155
const uint8_t DI_WRITE_CMD_5BITREVERSED = 0x08;
156
const uint8_t DI_READ_CTRL_5BITREVERSED = 0x18;
157
const uint8_t DI_WRITE_CTRL_5BITREVERSED = 0x04;
158
 
159
 
160
void usb_dbg_test() {
161
 
162
  uint32_t npc, ppc, r1;
163
  unsigned char stalled;
164
 
165
  printf("Stalling or1k\n");
166
  err = dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
167
 
168
  err = dbg_cpu0_read_ctrl(0, &stalled);
169
  if (!(stalled & 0x1)) {
170
    printf("or1k should be stalled\n");   // check stall or1k
171
    exit(1);
172
  }
173 47 julius
  uint32_t zero = 0;
174 39 julius
  /* Clear Debug Reason Register (DRR) 0x3015 */
175 47 julius
  err = dbg_cpu0_write((6 << 11) + 21, &zero, 4);
176
  err = dbg_cpu0_read((0 << 11) + 16, &npc, 4);  /* Read NPC */
177
  err = dbg_cpu0_read((0 << 11) + 18, &ppc, 4);  /* Read PPC */
178
  err = dbg_cpu0_read(0x401, &r1, 4);  /* Read R1 */
179 39 julius
 
180
  if (err)
181
    {
182
      printf("Jtag error %d occured; exiting.", err);
183
      FT2232_USB_JTAG_CloseDevice();
184
      exit(1);
185
    }
186
  printf("Read      npc = %.8x ppc = %.8x r1 = %.8x\n", npc, ppc, r1);
187 47 julius
 
188
  /*
189
  // Memory test - attempt to read and write massive arrays
190
  char biggest_array[65000];
191
  int i;
192
  printf("Testing 65000 byte array write\n");
193
  printf("Filling array...\n");
194
  for(i=0;i<65000;i++) biggest_array[i] = i;
195
  printf("Writing array\n");
196
  err = usb_dbg_wb_write_block32(0, (uint32_t *)biggest_array, 65000);
197
  printf("err = %d\n",err);
198
  */
199 39 julius
  return;
200
}
201
 
202
 
203
 
204
/*---------------------------------------------------------------------------*/
205
/*!Write up to 32-bits to the JTAG bus via the USB device
206
 
207
Write up to 32-bits to the JTAG bus.
208
 
209
@param[in] stream  This should store the data to be written to the bus
210
@param[in] num_bits  Number of bits to write on the JTAG bus
211
@param[in] dwTapControllerState  State to leave the JTAG TAP in when done    */
212
/*---------------------------------------------------------------------------*/
213
void usb_write_stream (uint32_t stream, uint32_t num_bits, DWORD dwTapControllerState){
214
  FTC_STATUS Status = FTC_SUCCESS;
215
  uint32_t i,num_bytes;
216
  WriteDataByteBuffer WriteDataBuffer;
217
 
218
  if ((num_bits/8)>0)num_bytes=(num_bits/8);
219
  else num_bytes=1;
220
  // First clear the buffer for the amount of data we're shifting in
221
  // Bytewise copy the stream into WriteDataBuffer, LSB first
222
 
223
  // This means if we want to send things MSb first, we need to pass them
224
  // to this function wrapped with a call to bit_reverse_data(data,length)
225
 
226
  for (i=0; i<(num_bytes)+1;i++)
227
    WriteDataBuffer[i] = ((stream >>(8*i)) & (0xff));
228
 
229
  // Now generate the CRC for what we're sending
230
  // data should be presented MSb first (at bit0)
231
  for (i=0;i<num_bits;i++)
232
    crc_w = crc_calc(crc_w,((stream>>i)&1));
233
 
234
 
235
  if (DEBUG_USB_DRVR_FUNCS)
236
    {
237
      printf("\nusb_write_stream: num_bytes=%d, WriteDataBuffer contents for %d bits:",num_bytes,num_bits);
238
      for (i=0;i<num_bytes+1;i++)
239
        printf("%x",WriteDataBuffer[num_bytes-i]);
240
      printf("\n");
241
    }
242
 
243
  //Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, false, num_bits, 
244
  //                   &WriteDataBuffer, num_bytes, dwTapControllerState);
245
  // Platform independant function call
246
  Status = FT2232_USB_JTAG_WriteDataToExternalDevice(false, num_bits,
247
                       &WriteDataBuffer, num_bytes, dwTapControllerState);
248
 
249
  if (Status != FTC_SUCCESS)
250
    printf("Write to USB device failed: status code: %ld\n",Status);
251
 
252
}
253
 
254
/*---------------------------------------------------------------------------*/
255
/*!Read up to 32-bits off the JTAG bus via the USB device
256
 
257
The return value of this should remain uint32_t as we're returning all the
258
data in a signal variable. We never need more than 32-bits in a single read
259
when using this function.
260
 
261
@param[in] num_bits  Number of bits to read off from the USB
262
@param[in] dwTapControllerState  State to leave the JTAG TAP in when done
263
@return: The data read from the USB device                                   */
264
/*---------------------------------------------------------------------------*/
265
uint32_t usb_read_stream(uint32_t num_bits, DWORD dwTapControllerState){
266
  ReadDataByteBuffer ReadDataBuffer;
267
  FTC_STATUS Status = FTC_SUCCESS;
268
  uint32_t returnVal =0;
269
  uint32_t i;
270
 
271
  // Platform independant driver call
272
  Status = FT2232_USB_JTAG_ReadDataFromExternalDevice(false, num_bits, &ReadDataBuffer, &dwNumBytesReturned, dwTapControllerState);
273
 
274
  if (DEBUG_USB_DRVR_FUNCS)
275
    printf("usb_read_stream: returned Status: 0x%lx from reading %u bits, \n",
276
           Status,num_bits);
277
 
278
  if (Status == FTC_SUCCESS){
279
 
280
    for(i=0;i<num_bits;i++){
281
      returnVal |= ((ReadDataBuffer[i/8]>>(i%8))&0x1)<<(num_bits-1-i);
282
    }
283
  }
284
  // Generate the CRC for read
285
  for (i=0;i<num_bits;i++)
286
      crc_r = crc_calc(crc_r, ((returnVal>>(num_bits-1-i))&1));
287
 
288
  return returnVal;
289
}
290
 
291
/* Sets TAP instruction register */
292
void usb_set_tap_ir(uint32_t ir) {
293
 
294
  WriteDataByteBuffer WriteDataBuffer;
295
  FTC_STATUS Status = FTC_SUCCESS;
296
 
297
  WriteDataBuffer[0] = ir;
298
 
299
  // Using global ftHandle, writing to TAP instruction register 4 bits, 
300
  //Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, true, 
301
  //JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
302
  // Platform independant driver call
303
 
304
  Status = FT2232_USB_JTAG_WriteDataToExternalDevice(true, JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
305
 
306
  if (DEBUG_USB_DRVR_FUNCS)
307
    printf("USB JTAG write of %x to instruction register returned Status: 0x%lx\n",
308
           ir, Status);
309
  current_chain = -1;
310
}
311
 
312
static uint32_t id_read_at_reset = 0;
313
 
314
/* Resets JTAG, and sets DEBUG scan chain */
315
int usb_dbg_reset() {
316
 
317
  // uint32_t err;
318
  uint32_t id;
319
  uint32_t reinit_count=0;
320
 retry_jtag_init:
321
  if (init_usb_jtag() > 0)
322
    return DBG_ERR_CRC;
323
 
324
  // Set ID code instruction in IR
325
  usb_set_tap_ir(JI_IDCODE);
326
 
327
  // Now read out the IDCODE for the device
328
  id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
329
 
330
  // if read ID was rubbish retry init - this is probably NOT the best way to do this...
331
  if ((id == 0xffffffff) | (id == 0x00000002) | (id == 0x00000000)) {
332
    // Platform independant driver call
333
    FT2232_USB_JTAG_CloseDevice();
334
    if (reinit_count++ > 4){
335
      printf("JTAG TAP ID read error. Unable to detect TAP controller. \nPlease ensure debugger is connected to target correctly.\n");
336
      exit(1);
337
    }
338
    goto retry_jtag_init;
339
  }
340
 
341
  printf("JTAG ID = %08x\n", id & 0xffffffff);
342
 
343
  /* select debug scan chain and stay in it forever */
344
  usb_set_tap_ir(JI_DEBUG);
345
 
346
  id_read_at_reset = id; // Store this to check later if there's errors
347
 
348
  current_chain = -1;
349
  return DBG_ERR_OK;
350
}
351
 
352 47 julius
static void reset_tap(void)
353
{
354
  uint32_t id = 0;
355
  reinit_usb_jtag();
356
 
357
 
358
  while (id != id_read_at_reset)
359
    {
360
      usb_set_tap_ir(JI_IDCODE);
361
      id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
362
      //printf("reset_tap: read ID %.8x\n",id);
363
    }
364
  //Return the chain to DEBUG mode
365
  usb_set_tap_ir(JI_DEBUG);
366
 
367
}
368
 
369
 
370 39 julius
/* counts retries and returns zero if we should abort */
371
static int retry_no = 0;
372
int retry_do() {
373
 
374
  unsigned char stalled;
375 45 julius
  int tap_id_reads = 0;
376 39 julius
 
377
  //printf("RETRY\n");
378
  if (retry_no == 0)
379
    printf("Communication error. Retrying\n");
380
 
381
  retry_no++;
382
 
383
  // Odds are if we're having a communication problem, we should 
384
  // just reconnect to the processor. It's probably just been reset.
385
  /*
386
    FT2232_USB_JTAG_CloseDevice();// Close the device
387
 
388
    if (init_usb_jtag() > 0)
389
    {
390
    if (retry_no >= NUM_HARD_RETRIES)
391
    return 1;
392
    else
393
    return 0;
394
    }
395
  */
396
 
397
  // Try a readback of the TAP ID
398 45 julius
 read_tap:
399 39 julius
  // Set ID code instruction in IR
400
  usb_set_tap_ir(JI_IDCODE);
401
 
402
  // Now read out the IDCODE for the device
403
  uint32_t id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
404
 
405
  //Return the chain to DEBUG mode
406
  usb_set_tap_ir(JI_DEBUG);
407
 
408
  if((id&0xffffffff) != (id_read_at_reset&0xffffffff))
409
    {
410 45 julius
      if (tap_id_reads == 10)
411
        {
412
          // Pretty big problem - can't even read the ID of the TAP anymore
413
          // So return error
414
          printf("Unable to read JTAG TAP ID - read %08x, expected %08x\n",
415
                 id, id_read_at_reset);
416
 
417
          return 1;
418
        }
419
 
420
      tap_id_reads++;
421
 
422
      goto read_tap;
423 39 julius
    }
424
 
425
  current_chain = -1;
426
 
427
  if (retry_no == 1)
428
    sleep(1);
429
 
430
  else if ( retry_no < NUM_SOFT_RETRIES)
431
    return 0; // Just attempt again    
432
 
433
  if ((retry_no >= NUM_SOFT_RETRIES) && (retry_no < NUM_SOFT_RETRIES + NUM_HARD_RETRIES) )
434
      {
435
        // Attempt a stall of the processor and then we'll try again
436
        //usb_dbg_test();
437
        printf("Resetting or1k\n");
438
        err = dbg_cpu0_write_ctrl(0, 0x02);      // reset or1k
439
 
440
        printf("Stalling or1k\n");
441
        err = dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
442
 
443
        err = dbg_cpu0_read_ctrl(0, &stalled);
444
        if (!(stalled & 0x1)) {
445
          printf("or1k should be stalled\n");   // check stall or1k
446
          FT2232_USB_JTAG_CloseDevice();// Close the device
447
          exit(1);
448
        }
449
 
450
        //retry_no++;
451
        return 0;
452
      }
453
    else // Unable to get the processor going again, return 1
454
      return 1;
455
 
456
}
457
 
458
/* resets retry counter */
459
void retry_ok() {
460
        retry_no = 0;
461
}
462
 
463
/* Sets scan chain.  */
464
int usb_dbg_set_chain(int chain) {
465
  uint32_t status, crc_generated, crc_read;
466
  dbg_chain = chain;
467
 
468
 try_again:
469
  if (current_chain == chain) return DBG_ERR_OK;
470
  current_chain = -1;
471
  if (DEBUG_CMDS) printf("\n");
472
  if (DEBUG_CMDS) printf("set_chain %i\n", chain);
473
 
474
  crc_w = 0xffffffff; // reset crc write variable
475
 
476
  // CRC generated in usb_read/write_stream functions
477
 
478
  usb_write_stream((((bit_reverse_data(chain,DBGCHAIN_SIZE) & 0xf) << 1) | 1),
479
                   DBGCHAIN_SIZE+1 , PAUSE_TEST_DATA_REGISTER_STATE);
480
 
481
  usb_write_stream(bit_reverse_data(crc_w, DBG_CRC_SIZE),DBG_CRC_SIZE,
482
                   PAUSE_TEST_DATA_REGISTER_STATE);
483
 
484
  crc_r = 0xffffffff; // reset the crc_r variable
485
 
486
  status = (usb_read_stream(DC_STATUS_SIZE,
487
                            PAUSE_TEST_DATA_REGISTER_STATE) & 0xf);
488
 
489
  crc_generated = crc_r;
490
 
491
  crc_read = usb_read_stream(DBG_CRC_SIZE, RUN_TEST_IDLE_STATE);
492
  //printf("%x %x %x\n",status, crc_generated, crc_read);
493
 
494
  /* CRCs must match, otherwise retry */
495
  if (crc_read != crc_generated) {
496
     if (!retry_do()) goto try_again;
497
    else return DBG_ERR_CRC;
498
  }
499
  /* we should read expected status value, otherwise retry */
500
  if (status != 0) {
501
    if (!retry_do()) goto try_again;
502
    else return status;
503
  }
504
 
505
  /* reset retry counter */
506
  retry_ok();
507
 
508
  current_chain = chain;
509
  return DBG_ERR_OK;
510
}
511
 
512
 
513
 
514
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
515
int usb_dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
516
  uint32_t i,status, crc_generated, crc_read;
517
 
518
   // JTAG driver things
519
  FTC_STATUS Status = FTC_SUCCESS;
520
  WriteDataByteBuffer WriteDataBuffer;
521
  ReadDataByteBuffer ReadDataBuffer;
522
 
523
 try_again:
524
  usb_dbg_set_chain(dbg_chain);
525
  if (DEBUG_CMDS) printf("\n");
526
  if (DEBUG_CMDS) printf("comm %d %d %8x \n", type,len,adr);
527
 
528
  //Calculate CRCs first
529
  crc_w = 0xffffffff;
530
 
531
  for (i=0;i<DBGCHAIN_SIZE+1;i++)
532
    crc_w = crc_calc(crc_w, (DI_WRITE_CMD_5BITREVERSED >> i)  & 1);
533
    //crc_w = crc_calc(crc_w,
534
    //       ((bit_reverse_data((DI_WRITE_CMD & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
535
 
536
  for (i=0;i<4;i++)
537
    crc_w = crc_calc(crc_w,((bit_reverse_data(type,4))>>i)&1);
538
 
539
  for (i=0;i<32;i++)
540
    crc_w = crc_calc(crc_w,((bit_reverse_data(adr,32))>>i)&1);
541
 
542
  assert(len > 0);
543
  for (i=0;i<16;i++)
544
    crc_w = crc_calc(crc_w,((bit_reverse_data(len-1,16))>>i)&1);
545
 
546
 
547
 
548
  // Now pack the write data buffer
549
  // 1-bit 0, 4-bits cmd, 4-bit type, 32-bit adr, 32-bit CRC
550
  // [0]
551
  //bits 0-4
552
  WriteDataBuffer[0]=(DI_WRITE_CMD_5BITREVERSED);
553
  //bits 5-7 
554
  WriteDataBuffer[0] |= ((bit_reverse_data(type,4)&0x7)<<5);
555
  // [1]
556
  // bit 0 - last bit of type
557
  WriteDataBuffer[1] = ((bit_reverse_data(type,4)&0x08)>>3);
558
  //bits 1-7 - first 7 bits of adr
559
  WriteDataBuffer[1] |= ((bit_reverse_data(adr,32)&0x07f)<<1);
560
  //[2-4] 24 bits of adr
561
  for(i=0;i<3;i++)
562
    WriteDataBuffer[2+i] = (bit_reverse_data(adr,32)>>(7+(i*8)))&0xff;
563
  // [5] last bit of adr in bit 0, first 7 bits of len-1 follow
564
  WriteDataBuffer[5] = (bit_reverse_data(adr,32)>>31)&1;
565
  WriteDataBuffer[5] |= (bit_reverse_data(len-1,16)&0x7f)<<1;
566
  // [6] bits 7-14 of (len-1)
567
  WriteDataBuffer[6] = (bit_reverse_data(len-1,16)>>7)&0xff;
568
  // [7] - last bit of len-1 and first 7 bits of the CRC
569
  WriteDataBuffer[7] = (bit_reverse_data(len-1,16)>>15)&1;
570
  //Reverse the CRC first
571
  crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
572
  WriteDataBuffer[7] |= (crc_w&0x7f)<<1;
573
  //[8-10] next 24 bits (7-30) of crc_w
574
  WriteDataBuffer[8] = (crc_w>>7)&0xff;
575
  WriteDataBuffer[9] = (crc_w>>15)&0xff;
576
  WriteDataBuffer[10] = (crc_w>>23)&0xff;
577
  //[11] final bit of crc_w
578
  WriteDataBuffer[11] = (crc_w>>31)&1;
579
 
580
  //  Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
581
  // Platform independant driver call
582
  Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
583
 
584
  if (Status != FTC_SUCCESS)
585
    printf("USB write fail - code %ld\b",Status);
586
 
587
  // Now look through the read data
588
 
589
  // From bit1 of ReadDataBuffer[11] we should have our 4-bit status
590
  status = (ReadDataBuffer[11] >> 1) & 0xf;
591
 
592
  // Now extract the received CRC
593
  crc_read = 0;
594
  //first 3 bits (0-2)
595
  crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
596
  // middle 3 bytes (bits 3 to 26)
597
  for (i=0;i<3;i++)
598
    crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
599
  // last 5 bits from ReadDataBuffer[15]
600
  crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
601
 
602
  // Now calculate CRC on status
603
  crc_r = 0xffffffff;
604
  for(i=0;i<DC_STATUS_SIZE;i++)
605
    crc_r = crc_calc(crc_r, (status>>i)&1);
606
 
607
  crc_generated = crc_r;
608
  // Now bit reverse status and crc_read as we unpacked them
609
  // with the MSb going to the LSb
610
  status = bit_reverse_data(status, DC_STATUS_SIZE);
611
  crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
612
 
613
  //printf("%x %x %x\n", status, crc_read, crc_generated);
614
  /* CRCs must match, otherwise retry */
615 47 julius
  uint32_t tries = 0;
616 39 julius
  if (crc_read != crc_generated) {
617 47 julius
    tries++;
618
    if (tries < 2)
619
      {
620
        if (DEBUG_USB_DRVR_FUNCS)
621
          printf("usb_functions - usb_dbg_command - CRC fail. Going again\n");
622
        goto try_again;
623
      }
624
    else if (tries < 8)
625
      {
626
        reset_tap();
627
        goto try_again;
628
      }
629 39 julius
    else return DBG_ERR_CRC;
630
  }
631
  /* we should read expected status value, otherwise retry */
632 47 julius
  tries = 0;
633 39 julius
  if (status != 0) {
634 47 julius
    if (tries < 2)
635
      {
636
        if (DEBUG_USB_DRVR_FUNCS)
637
          printf("usb_functions - usb_dbg_command - bad status (%d). Going again\n",status);
638
        goto try_again;
639
      }
640
    else if (tries < 8)
641
      {
642
        reset_tap();
643
        goto try_again;
644
      }
645 39 julius
    else return status;
646 47 julius
 
647 39 julius
  }
648
  /* reset retry counter */
649
  retry_ok();
650
 
651
  return DBG_ERR_OK;
652
}
653
 
654
 
655
/* writes a ctrl reg */
656
int usb_dbg_ctrl(uint32_t reset, uint32_t stall) {
657
  uint32_t i,status, crc_generated, crc_read;
658
  // JTAG driver things
659
  FTC_STATUS Status = FTC_SUCCESS;
660
  WriteDataByteBuffer WriteDataBuffer;
661
  ReadDataByteBuffer ReadDataBuffer;
662
 
663
try_again:
664
  usb_dbg_set_chain(dbg_chain);
665
        if (DEBUG_CMDS) printf("\n");
666
        if (DEBUG_CMDS) printf("ctrl\n");
667
        if (DEBUG_CMDS) printf("reset %x stall %x\n", reset, stall);
668
 
669
        crc_w = 0xffffffff;
670
        // Try packing everyhing we want to send into one write buffer
671
        //Calculate CRCs first
672
        for (i=0;i<DBGCHAIN_SIZE+1;i++)
673
          crc_w = crc_calc(crc_w, (DI_WRITE_CTRL_5BITREVERSED>>i)&1);
674
          //crc_w = crc_calc(crc_w, 
675
          //       ((bit_reverse_data((DI_WRITE_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
676
        crc_w = crc_calc(crc_w,(reset&1));
677
        crc_w = crc_calc(crc_w,(stall&1));
678
        for (i=0;i<50;i++)
679
          crc_w = crc_calc(crc_w,0);
680
 
681
 
682
 
683
        // Now pack the write data buffer
684
        // 1-bit 0, 4-bits cmd, 52-bits CPU control register data (only first 2 matter)
685
        //bits 0-4
686
        WriteDataBuffer[0]=(DI_WRITE_CTRL_5BITREVERSED);
687
        // bit 5
688
        WriteDataBuffer[0] |= (reset)<<(DBGCHAIN_SIZE+1);
689
        // bit 6
690
        WriteDataBuffer[0] |= (stall)<<(DBGCHAIN_SIZE+2);
691
        // Clear the next 48 bits
692
        for (i=1; i<16;i++)   //actually clear more than just the next 50 bits
693
          WriteDataBuffer[i] = 0;
694
 
695
        //Reverse the CRC first
696
        crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
697
        //Now load in the CRC, but take note of fact 
698
        // that bit 0 of buffer[7] is last 0 from cmd register data
699
        // fill up from WriteDataBuffer[7-11]
700
        for (i=0;i<4;i++)
701
          WriteDataBuffer[7+i] = ((crc_w<<1)>>(i*8))&0xff;
702
        //Final bit, shift in and make sure is the only thing int he buffer
703
        WriteDataBuffer[11]=0|((crc_w>>31)&1);
704
 
705
 
706
        // Platform independant driver call
707
        Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
708
 
709
 
710
        if (Status != FTC_SUCCESS)
711
          printf("USB write fail - code %ld\b",Status);
712
 
713
        // Now look through the read data
714
 
715
        // From bit1 of ReadDataBuffer[11] we should have our 4-bit status
716
        status = (ReadDataBuffer[11] >> 1) & 0xf;
717
 
718
        // Now extract the received CRC
719
        crc_read = 0;
720
        //first 3 bits (0-2)
721
        crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
722
        // middle 3 bytes (bits 3 to 26)
723
        for (i=0;i<3;i++)
724
          crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
725
        // last 5 bits from ReadDataBuffer[15]
726
        crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
727
 
728
 
729
        // Now calculate CRC on status and crc_read
730
        crc_r = 0xffffffff;
731
        for(i=0;i<DC_STATUS_SIZE;i++)
732
          crc_r = crc_calc(crc_r, (status>>i)&1);
733
 
734
        crc_generated = crc_r;
735
        // Now bit reverse status and crc_read as we unpacked them
736
        // with the MSb going to the LSb
737
        status = bit_reverse_data(status, DC_STATUS_SIZE);
738
        crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
739
 
740
        /* CRCs must match, otherwise retry */
741
        //printf("%x %x %x\n", status, crc_read, crc_generated);
742 47 julius
        uint32_t tries = 0;
743
          if (crc_read != crc_generated) {
744
          tries++;
745
          if (tries < 2)
746
            {
747
              if (DEBUG_USB_DRVR_FUNCS)
748
                printf("usb_functions - usb_dbg_ctrl - CRC fail. Going again\n");
749
              goto try_again;
750
            }
751
          else if (tries < 8)
752
            {
753
              reset_tap();
754
              goto try_again;
755
            }
756 39 julius
          else return DBG_ERR_CRC;
757
        }
758
        /* we should read expected status value, otherwise retry */
759 47 julius
        tries = 0;
760 39 julius
        if (status != 0) {
761 47 julius
          if (tries < 2)
762
            {
763
              if (DEBUG_USB_DRVR_FUNCS)
764
                printf("usb_functions - usb_dbg_ctrl - bad status (%d). Going again\n",status);
765
              goto try_again;
766
            }
767
          else if (tries < 8)
768
            {
769
              reset_tap();
770
              goto try_again;
771
            }
772 39 julius
          else return status;
773 47 julius
 
774 39 julius
        }
775
 
776
        /* reset retry counter */
777
        retry_ok();
778
        return DBG_ERR_OK;
779
}
780
 
781
 
782
/* reads control register */
783
int usb_dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
784
  uint32_t i, status, crc_generated, crc_read;
785
 
786
  // JTAG driver things
787
  FTC_STATUS Status = FTC_SUCCESS;
788
  WriteDataByteBuffer WriteDataBuffer;
789
  ReadDataByteBuffer ReadDataBuffer;
790
 
791
 
792
 try_again:
793
  usb_dbg_set_chain(dbg_chain);
794
  if (DEBUG_CMDS) printf("\n");
795
  if (DEBUG_CMDS) printf("ctrl_read\n");
796
 
797
  crc_w = 0xffffffff;
798
  // Try packing everyhing we want to send into one write buffer
799
  //Calculate CRCs first
800
  for (i=0;i<DBGCHAIN_SIZE+1;i++)
801
    crc_w = crc_calc(crc_w, (DI_READ_CTRL_5BITREVERSED>>i)&1);
802
    //crc_w = crc_calc(crc_w, 
803
    //((bit_reverse_data((DI_READ_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
804
 
805
 
806
  // Now pack the write data buffer
807
  // 1-bit 0, 4-bits cmd, 32-bit CRC
808
  //bits 0-4
809
  WriteDataBuffer[0]=(DI_READ_CTRL_5BITREVERSED);
810
  // Clear the next 48 bits
811
  for (i=1; i<16;i++)   //actually clear more than just the next 50 bits
812
    WriteDataBuffer[i] = 0;
813
 
814
  //Reverse the CRC first
815
  crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
816
  //Now load in the CRC
817
  //First 3 bits go in last 3 bits of buffer[0]
818
  WriteDataBuffer[0] |= (crc_w & 0x7)<<5;
819
  // The rest of crc_w goes in buffer[1-4]
820
  for (i=0;i<3;i++)
821
    WriteDataBuffer[1+i] = ((crc_w>>3)>>(i*8))&0xff;
822
  //Final bit of write buffer with CRC
823
  WriteDataBuffer[4] = ((crc_w>>3)>>(24))&0x1f;
824
 
825
 
826
 
827
  //Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
828
  // Platform independant driver call
829
  Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
830
  if (Status != FTC_SUCCESS)
831
    printf("USB read fail - code %ld\b",Status);
832
 
833
  // Now look through the read data
834
  //0 - 1+4-bit status, 3-bits CRC
835
  //1,2,3 - CRC
836
  //4 - first 5 bits CRC, last 3, control reg bits (first 3)
837
  //5,6,7,8,9,10 - control reg data (48 bits)
838
  //11 - bit0 - control reg data, bit 1-4 status bits, bits 5-7 CRC
839
  //12, 13 14 - CRC
840
  // 15 bits 0-4 CRC
841
  // Starting from bit1 of ReadDataBuffer[11] we should have our 4-bit status
842
  status = (ReadDataBuffer[11] >> 1) & 0xf;
843
 
844
  //reset bit should be in ReadDataBuffer[4] as bit 5
845
  *reset = (ReadDataBuffer[4] >> 5) & 1;
846
  //stalled bit should be in ReadDataBuffer[4] as bit 6
847
  *stall = (ReadDataBuffer[4] >> 6) & 1;
848
  // Now extract the received CRC
849
  crc_read = 0;
850
  //first 3 bits (0-2) of CRC are in bits 5-7 of ReadDataBuffer[11]
851
  crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
852
  // middle 3 bytes (bits 3 to 26)
853
  for (i=0;i<3;i++)
854
    crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
855
  // last 5 bits from ReadDataBuffer[15]
856
  crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
857
 
858
  if (DEBUG_CMDS) printf("reset bit %x stalled bit %x:\n",
859
         ((ReadDataBuffer[4] >> 5) & 1), ((ReadDataBuffer[4] >> 6) & 1));
860
 
861
  // Now calculate CRC on status and crc_read
862
  crc_r = 0xffffffff;
863
 
864
  crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 5) & 1));
865
  crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 6) & 1));
866
  for(i=0;i<50;i++)
867
    crc_r = crc_calc(crc_r,0);
868
  for(i=0;i<DC_STATUS_SIZE;i++)
869
    crc_r = crc_calc(crc_r, (status>>i)&1);
870
 
871
  crc_generated = crc_r;
872
  // Now bit reverse status and crc_read as we unpacked them
873
  // with the MSb going to the LSb
874
  status = bit_reverse_data(status, DC_STATUS_SIZE);
875
  crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
876 47 julius
 
877 39 julius
 
878
  /* CRCs must match, otherwise retry */
879
  //printf("%x %x %x\n", status, crc_generated, crc_read);
880 47 julius
  uint32_t tries = 0;
881 39 julius
  if (crc_read != crc_generated) {
882 47 julius
    tries++;
883
    if (tries < 2)
884
      {
885
        if (DEBUG_USB_DRVR_FUNCS)
886
          printf("usb_functions - usb_dbg_ctrl_read - CRC fail. Going again\n");
887
        goto try_again;
888
      }
889
    else if (tries < 8)
890
      {
891
        reset_tap();
892
        goto try_again;
893
      }
894 39 julius
    else return DBG_ERR_CRC;
895 47 julius
        }
896 39 julius
  /* we should read expected status value, otherwise retry */
897 47 julius
  tries = 0;
898 39 julius
  if (status != 0) {
899 47 julius
    if (tries < 2)
900
      {
901
        if (DEBUG_USB_DRVR_FUNCS)
902
          printf("usb_functions - usb_dbg_ctrl_read - bad status (%d). Going again\n",status);
903
        goto try_again;
904
      }
905
    else if (tries < 8)
906
      {
907
        reset_tap();
908
        goto try_again;
909
      }
910 39 julius
    else return status;
911 47 julius
 
912 39 julius
  }
913 47 julius
 
914 39 julius
  /* reset retry counter */
915
  retry_ok();
916
  return DBG_ERR_OK;
917
}
918
 
919
 
920
/* issues a burst read/write */
921
int usb_dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
922
        uint32_t status, crc_generated, crc_read;
923
        int i,j;
924 47 julius
        int tries = 0;
925 39 julius
        uint8_t data_byte;
926
 
927
        // JTAG driver things
928
        FTC_STATUS Status = FTC_SUCCESS;
929
        WriteDataByteBuffer WriteDataBuffer;
930
        ReadDataByteBuffer ReadDataBuffer;
931
 
932
 try_again:
933
        usb_dbg_set_chain(dbg_chain);
934
        if (DEBUG_CMDS) printf("\n");
935 47 julius
        if (DEBUG_CMDS) printf("go len is %dbytes, read=%d (if 0, writing)\n", len, read);
936 39 julius
 
937
        crc_w = 0xffffffff;
938
        // Try packing everyhing we want to send into one write buffer
939
        //Calculate CRCs first
940
        for (i=0;i<DBGCHAIN_SIZE+1;i++)
941
          crc_w = crc_calc(crc_w, (DI_GO_5BITREVERSED>>i)&1);
942
          //crc_w = crc_calc(crc_w, 
943
          //       ((bit_reverse_data((DI_GO & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
944
        // Set first 5 bits of WriteDataBuffer up to 
945
        // be the GO command and 0 first bit
946
        WriteDataBuffer[0]=DI_GO_5BITREVERSED;
947
 
948
        if (read)
949
          {
950
            // Do GO command for a read
951
            // 0 then 4-bit go command, then 32-bit CRC for the last 5 bits
952
            // Then read (len+1)*8 + 4-bit status + 32-bit CRC
953
 
954
 
955
            // Reverse crc_w
956
            crc_w = bit_reverse_data(crc_w,DBG_CRC_SIZE);
957
 
958
            // Now pack in the 32-bit CRC as we're not 
959
            // writing anything else after it
960
             //First 3 bits of each byte go in last 3 bits of buffer[i], 
961
            // next 5 go in buffer[i+1]
962
            for(i=0;i<4;i++){
963
              WriteDataBuffer[i] |= ((crc_w>>(i*8))&0x07)<<5;
964
              WriteDataBuffer[i+1] = ((crc_w>>(i*8))>>3)&0x1f;
965
            }
966
 
967
            // Should have data up to WriteDataBuffer[4] bit 4
968
            // Read data should start at ReadDataBuffer[4] bit 5
969
 
970
            //Clear the rest of the write buffer
971
            for(i=5;i<(10+len);i++)
972
              WriteDataBuffer[i]=0;
973
          }
974
        if (!read){
975
          // If we're writing we put in the 5 command bits, (len+1)*8 data bits,
976
          // and then the 32-bit CRC do first 3 bits, then next 5 bits in 
977
          // each of the for loops iterations
978
          for(i=0;i<len;i++){
979
 
980
            data_byte = bit_reverse_swar_8(data[i]);
981
 
982
            WriteDataBuffer[i] |= ((data_byte&0x07)<<5);
983
            WriteDataBuffer[i+1] = ((data_byte>>3)&0x1f);
984
 
985
            // Now update the CRC
986
            for(j=0;j<8;j++)
987
              crc_w = crc_calc(crc_w, (data_byte>>j)&1);
988
 
989
          }
990
 
991
          // Reverse crc_w
992
          crc_w = bit_reverse_data(crc_w,DBG_CRC_SIZE);
993
 
994
          // If we have len=4 for example, we will write to 
995
          // WriteDataBuffer[4]'s first 5 bits
996
 
997
          // So now load in the 32-bit CRC from there
998
          for(i=0;i<4;i++){
999
            WriteDataBuffer[len+i] |= ((crc_w>>(i*8))&0x07)<<5;
1000
            WriteDataBuffer[len+i+1] = ((crc_w>>(i*8))>>3)&0x1f;
1001
          }
1002
          // Should have data up to WriteDataBuffer[4+len] bit 4
1003
          // Read data should start at ReadDataBuffer[4+len] bit 5
1004
 
1005
        }
1006
 
1007
        //Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,(5+(len*8)+32+36), &WriteDataBuffer, (6+len+4), &ReadDataBuffer,&dwNumBytesReturned,RUN_TEST_IDLE_STATE);
1008
        Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,(5+(len*8)+32+36), &WriteDataBuffer, (6+len+4), &ReadDataBuffer,&dwNumBytesReturned,RUN_TEST_IDLE_STATE);
1009
 
1010
        if (Status != FTC_SUCCESS)
1011
          printf("USB write fail - code %ld\n",Status);
1012
 
1013
        crc_r = 0xffffffff;
1014
 
1015
        if (read){
1016
          // Look through our data, starting from ReadDataBuffer[4] bit 5
1017
          // We receive len bytes, starting at ReadDataBuffer[4] bit 5, so
1018
          // unpack like so
1019
          if (DEBUG_USB_DRVR_FUNCS) printf("USB read data buffer: ");
1020
          for(i=0;i<len;i++){
1021
            // get first 3 bits
1022
            data[i] = (ReadDataBuffer[4+i]>>5)&0x07;
1023
            // then next 5 from next ReadDataBuffer byte
1024
            data[i] |= (ReadDataBuffer[4+i+1]&0x1f)<<3;
1025
 
1026
            // Now calculate the CRC for this byte
1027
            for(j=0;j<8;j++)
1028
              crc_r = crc_calc(crc_r, (data[i]>>j)&1);
1029
 
1030
            // Now bit reverse the byte as it was read in MSb first but 
1031
            // written to LSb first
1032
            data[i] = bit_reverse_data(data[i],8);
1033
 
1034 47 julius
            if (DEBUG_USB_DRVR_FUNCS) printf("%.2x",data[i]);
1035 39 julius
          }
1036
          if (DEBUG_USB_DRVR_FUNCS) printf("\n");
1037
 
1038
          // Should be up to ReadDataBuffer[4+len] bit 5 for data
1039
          status = (ReadDataBuffer[4+len]>>5)&0x07;
1040
          status |= (ReadDataBuffer[4+len+1]&1)<<3;
1041
          // Now get out crc_read
1042
          crc_read = 0;
1043
          for(i=0;i<4;i++){
1044
            crc_read |= ((ReadDataBuffer[4+len+1+i]>>1)&0x7f)<<(i*8);
1045
            crc_read |= ((ReadDataBuffer[4+len+1+i+1]&0x1)<<7)<<(i*8);
1046
          }
1047
 
1048
          for(i=0;i<4;i++)
1049
            crc_r = crc_calc(crc_r, (status>>i)&1);
1050
        }
1051
        if (!read){
1052
          // Just extract our 4-bits of status and CRC
1053
          status = (ReadDataBuffer[4+len]>>5)&0x07;
1054
          status |= (ReadDataBuffer[4+len+1]&1)<<3;
1055
 
1056
          // extract crc_read from the ReadDataBuffer
1057
          crc_read = 0;
1058
          for(i=0;i<4;i++){
1059
            crc_read |= ((ReadDataBuffer[4+len+1+i]>>1)&0x7f)<<(i*8);
1060
            crc_read |= ((ReadDataBuffer[4+len+1+i+1]&1)<<7)<<(i*8);
1061
          }
1062
          // Calculate our own CRC from the status value
1063
          for(i=0;i<4;i++)
1064
            crc_r = crc_calc(crc_r, (status>>i)&1);
1065
 
1066
        }
1067
 
1068
        crc_generated = crc_r;
1069 47 julius
 
1070 39 julius
        // Now bit reverse status and crc_read as we unpacked them
1071
        // with the MSb going to the LSb
1072
        status = bit_reverse_data(status, DC_STATUS_SIZE);
1073
        crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
1074
 
1075
        /* CRCs must match, otherwise retry */
1076 47 julius
 
1077
        //printf("%x %x %x\n", status, crc_read, crc_generated);
1078
 
1079 39 julius
        if (crc_read != crc_generated) {
1080 47 julius
          tries++;
1081
          if (tries < 8)
1082
            {
1083
              if (DEBUG_USB_DRVR_FUNCS) printf("usb_functions - usb_dbg_go - CRC fail (%d) try %d. Going again\n",status, tries);
1084
              reset_tap() ;
1085
              goto try_again;
1086
            }
1087 39 julius
          else return DBG_ERR_CRC;
1088
        }
1089 47 julius
        //if (crc_read == crc_generated)
1090
        //tries = 0;
1091 39 julius
        /* we should read expected status value, otherwise retry */
1092
        if (status != 0) {
1093 47 julius
          tries++;
1094
          if (tries < 8)
1095
            {
1096
              if (DEBUG_USB_DRVR_FUNCS) printf("usb_functions - usb_dbg_go - bad status (%d) try %d. Going again\n",status, tries);
1097
              reset_tap();
1098
              goto try_again;
1099
 
1100
            }
1101 39 julius
          else return status;
1102 47 julius
 
1103 39 julius
        }
1104
 
1105
        retry_ok();
1106
        return DBG_ERR_OK;
1107
}
1108
 
1109
/* read a word from wishbone */
1110
int usb_dbg_wb_read32(uint32_t adr, uint32_t *data) {
1111
  // uint32_t err;
1112
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1113 46 julius
  if ((err = usb_dbg_command(DBG_WB_READ32, adr, 4))) return err;
1114 39 julius
  if ((err = usb_dbg_go((unsigned char*)data, 4, 1))) return err;
1115 46 julius
  //*data = ntohl(*data);
1116 39 julius
  return err;
1117
}
1118
 
1119
/* write a word to wishbone */
1120 46 julius
int usb_dbg_wb_write8(uint32_t adr, uint8_t data) {
1121
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1122
  if ((err = usb_dbg_command(DBG_WB_WRITE8, adr, 1))) return err;
1123
  if ((err = usb_dbg_go((unsigned char*)&data, 1, 0))) return err;
1124
  return DBG_ERR_OK;
1125
}
1126
 
1127
 
1128
/* write a word to wishbone */
1129 39 julius
int usb_dbg_wb_write32(uint32_t adr, uint32_t data) {
1130
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1131 46 julius
  if ((err = usb_dbg_command(DBG_WB_WRITE32, adr, 4))) return err;
1132 39 julius
  if ((err = usb_dbg_go((unsigned char*)&data, 4, 0))) return err;
1133
  return DBG_ERR_OK;
1134
}
1135
 
1136
/* read a block from wishbone */
1137
int usb_dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
1138
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1139 46 julius
  if ((err = usb_dbg_command(DBG_WB_READ32, adr, len))) return err;
1140 39 julius
  if ((err = usb_dbg_go((unsigned char*)data, len, 1))) return err;
1141
  return DBG_ERR_OK;
1142
}
1143
 
1144
 
1145
/* write a block to wishbone */
1146
int usb_dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
1147 47 julius
  if (DEBUG_CMDS) printf("usb_functions: wb_write_block %.8x %d bytes\n",adr, len);
1148 39 julius
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1149 46 julius
  if ((err = usb_dbg_command(DBG_WB_WRITE32, adr, len))) return err;
1150 39 julius
  if ((err = usb_dbg_go((unsigned char*)data, len, 0))) return err;
1151
  return DBG_ERR_OK;
1152
}
1153
 
1154
 
1155
/* read a register from cpu */
1156 47 julius
int usb_dbg_cpu0_read(uint32_t adr, uint32_t *data, uint32_t length) {
1157 39 julius
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1158 47 julius
  if ((err = usb_dbg_command(DBG_CPU_READ, adr, length))) return err;
1159
  if ((err = usb_dbg_go((unsigned char*)data, length, 1))) return err;
1160
  int i;for(i=0;i<(length/4);i++)data[i]=ntohl(data[i]);
1161 39 julius
  return DBG_ERR_OK;
1162
}
1163
 
1164
/* write a cpu register */
1165 47 julius
int usb_dbg_cpu0_write(uint32_t adr, uint32_t *data, uint32_t length) {
1166
  int i;for(i=0;i<(length/4);i++){data[i]=ntohl(data[i]);}
1167 39 julius
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1168 47 julius
  if ((err = usb_dbg_command(DBG_CPU_WRITE, adr, length))) return err;
1169
  if ((err = usb_dbg_go((unsigned char*)data, length, 0))) return err;
1170 39 julius
  return DBG_ERR_OK;
1171
}
1172
 
1173
/* write a cpu module register */
1174
int usb_dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
1175
  // no ensuring that or1k is stalled here, becuase we're call this from that function
1176
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1177
  if ((err = usb_dbg_ctrl(data >> 1, data & 0x1))) return err;
1178
  return DBG_ERR_OK;
1179
}
1180
 
1181
/* read a register from cpu module */
1182
int usb_dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
1183
  // no ensuring that or1k is stalled here, becuase we're call this from that function
1184
  uint32_t r, s;
1185
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1186
  if ((err = usb_dbg_ctrl_read(&r, &s))) return err;
1187
  *data = (r << 1) | s;
1188
  return DBG_ERR_OK;
1189
}
1190
 
1191
/* Function to close the device handle. Is called when closing the app */
1192
void usb_close_device_handle()
1193
{
1194
  // try unstalling the processor before quitting
1195
  dbg_cpu0_write_ctrl(0, 0x00);      // unstall or1k
1196
  FT2232_USB_JTAG_CloseDevice();
1197
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.