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

Subversion Repositories openrisc_me

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

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 79 julius
// 100408               Fixed up retries                                jb
58 39 julius
 
59
#include <assert.h>
60
#include <stdio.h>
61
#include <ctype.h>
62
#include <string.h>
63
#include <stdlib.h>
64
#include <unistd.h>
65
#include <stdarg.h>
66
#include <sys/stat.h>
67
#include <sys/types.h>
68
 
69
#include <netinet/in.h>
70
 
71
#ifdef CYGWIN_COMPILE
72
#include <windows.h>
73
#include "win_FTCJTAG.h"
74
#else // We're on Linux or Mac OS X
75
#include "WinTypes.h" // We still use things from here in this file - TODO: remove this dependency
76
#include "FT2232cMpsseJtag.h"
77
#endif
78
 
79
#include "gdb.h"
80
 
81
#include "usb_functions.h"
82
 
83
#include "or_debug_proxy.h"
84
 
85
#include "usb_driver_calls.h"
86
 
87
// Global JTAG signals for reading/writing
88
DWORD dwNumBytesReturned = 0;
89
 
90
/* Crc of current read or written data.  */
91
uint32_t crc_r, crc_w = 0;
92
 
93 79 julius
/* Number of retries for a command */
94
uint32_t retries;
95
 
96 39 julius
/* Generates new crc, sending in new bit input_bit */
97
uint32_t crc_calc(uint32_t crc, uint32_t input_bit) {
98
        uint32_t d = (input_bit) ? 0xfffffff : 0x0000000;
99
        uint32_t crc_32 = ((crc >> 31)&1) ? 0xfffffff : 0x0000000;
100
        crc <<= 1;
101
        return crc ^ ((d ^ crc_32) & DBG_CRC_POLY);
102
}
103
 
104
// Speedy bit reversing algorithms for base2 lengths
105
// Thanks to: http://aggregate.org/MAGIC/#Bit%20Reversal
106
uint32_t bit_reverse_swar_2(uint32_t x)
107
{
108
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
109
  return x;
110
}
111
uint32_t bit_reverse_swar_4(uint32_t x)
112
{
113
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
114
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
115
  return x;
116
}
117
static inline uint32_t bit_reverse_swar_8(uint32_t x)
118
{
119
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
120
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
121
  x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
122
  return x;
123
}
124
uint32_t bit_reverse_swar_16(uint32_t x)
125
{
126
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
127
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
128
  x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
129
  x=(((x&0xff00ff00)>>8)|((x&0x00ff00ff)<<8));
130
  return x;
131
}
132
uint32_t bit_reverse_swar_32(uint32_t x)
133
{
134
  x=(((x&0xaaaaaaaa)>>1)|((x&0x55555555)<<1));
135
  x=(((x&0xcccccccc)>>2)|((x&0x33333333)<<2));
136
  x=(((x&0xf0f0f0f0)>>4)|((x&0x0f0f0f0f)<<4));
137
  x=(((x&0xff00ff00)>>8)|((x&0x00ff00ff)<<8));
138
  x=(((x&0xffff0000)>>16)|((x&0x0000ffff)<<16)); // We could be on 64-bit arch!
139
  return x;
140
}
141
 
142
uint32_t bit_reverse_data(uint32_t data, uint32_t length){
143
  if (length == 2) return bit_reverse_swar_2(data);
144
  if (length == 4) return bit_reverse_swar_4(data);
145
  if (length == 8) return bit_reverse_swar_8(data);
146
  if (length == 16) return bit_reverse_swar_16(data);
147
  if (length == 32) return bit_reverse_swar_32(data);
148
  // Long and laborious way - hopefully never gets called anymore!
149
  uint32_t i,reverse=0;
150
  for (i=0;i<length;i++) reverse |= (((data>>i)&1)<<(length-1-i));
151
  return reverse;
152
}
153 47 julius
// Constants that are used a lot, and were 5 bits, so might as well 
154
// precalculate them to save reversing them each time.
155 39 julius
// These are from or_debug_proxy.h, so if the original values change these
156
// should be recalculated!!
157
const uint8_t DI_GO_5BITREVERSED = 0x00;
158
const uint8_t DI_READ_CMD_5BITREVERSED = 0x10;
159
const uint8_t DI_WRITE_CMD_5BITREVERSED = 0x08;
160
const uint8_t DI_READ_CTRL_5BITREVERSED = 0x18;
161
const uint8_t DI_WRITE_CTRL_5BITREVERSED = 0x04;
162
 
163
 
164
void usb_dbg_test() {
165
 
166
  uint32_t npc, ppc, r1;
167
  unsigned char stalled;
168
 
169
  printf("Stalling or1k\n");
170
  err = dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
171
 
172
  err = dbg_cpu0_read_ctrl(0, &stalled);
173
  if (!(stalled & 0x1)) {
174
    printf("or1k should be stalled\n");   // check stall or1k
175
    exit(1);
176
  }
177 47 julius
  uint32_t zero = 0;
178 39 julius
  /* Clear Debug Reason Register (DRR) 0x3015 */
179 47 julius
  err = dbg_cpu0_write((6 << 11) + 21, &zero, 4);
180
  err = dbg_cpu0_read((0 << 11) + 16, &npc, 4);  /* Read NPC */
181
  err = dbg_cpu0_read((0 << 11) + 18, &ppc, 4);  /* Read PPC */
182
  err = dbg_cpu0_read(0x401, &r1, 4);  /* Read R1 */
183 39 julius
 
184
  if (err)
185
    {
186
      printf("Jtag error %d occured; exiting.", err);
187
      FT2232_USB_JTAG_CloseDevice();
188
      exit(1);
189
    }
190
  printf("Read      npc = %.8x ppc = %.8x r1 = %.8x\n", npc, ppc, r1);
191 47 julius
 
192
  /*
193
  // Memory test - attempt to read and write massive arrays
194
  char biggest_array[65000];
195
  int i;
196
  printf("Testing 65000 byte array write\n");
197
  printf("Filling array...\n");
198
  for(i=0;i<65000;i++) biggest_array[i] = i;
199
  printf("Writing array\n");
200
  err = usb_dbg_wb_write_block32(0, (uint32_t *)biggest_array, 65000);
201
  printf("err = %d\n",err);
202
  */
203 39 julius
  return;
204
}
205
 
206
 
207
 
208
/*---------------------------------------------------------------------------*/
209
/*!Write up to 32-bits to the JTAG bus via the USB device
210
 
211
Write up to 32-bits to the JTAG bus.
212
 
213
@param[in] stream  This should store the data to be written to the bus
214
@param[in] num_bits  Number of bits to write on the JTAG bus
215
@param[in] dwTapControllerState  State to leave the JTAG TAP in when done    */
216
/*---------------------------------------------------------------------------*/
217
void usb_write_stream (uint32_t stream, uint32_t num_bits, DWORD dwTapControllerState){
218
  FTC_STATUS Status = FTC_SUCCESS;
219
  uint32_t i,num_bytes;
220
  WriteDataByteBuffer WriteDataBuffer;
221
 
222
  if ((num_bits/8)>0)num_bytes=(num_bits/8);
223
  else num_bytes=1;
224
  // First clear the buffer for the amount of data we're shifting in
225
  // Bytewise copy the stream into WriteDataBuffer, LSB first
226
 
227
  // This means if we want to send things MSb first, we need to pass them
228
  // to this function wrapped with a call to bit_reverse_data(data,length)
229
 
230
  for (i=0; i<(num_bytes)+1;i++)
231
    WriteDataBuffer[i] = ((stream >>(8*i)) & (0xff));
232
 
233
  // Now generate the CRC for what we're sending
234
  // data should be presented MSb first (at bit0)
235
  for (i=0;i<num_bits;i++)
236
    crc_w = crc_calc(crc_w,((stream>>i)&1));
237
 
238
 
239
  if (DEBUG_USB_DRVR_FUNCS)
240
    {
241
      printf("\nusb_write_stream: num_bytes=%d, WriteDataBuffer contents for %d bits:",num_bytes,num_bits);
242
      for (i=0;i<num_bytes+1;i++)
243
        printf("%x",WriteDataBuffer[num_bytes-i]);
244
      printf("\n");
245
    }
246
 
247
  //Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, false, num_bits, 
248
  //                   &WriteDataBuffer, num_bytes, dwTapControllerState);
249
  // Platform independant function call
250
  Status = FT2232_USB_JTAG_WriteDataToExternalDevice(false, num_bits,
251
                       &WriteDataBuffer, num_bytes, dwTapControllerState);
252
 
253
  if (Status != FTC_SUCCESS)
254
    printf("Write to USB device failed: status code: %ld\n",Status);
255
 
256
}
257
 
258
/*---------------------------------------------------------------------------*/
259
/*!Read up to 32-bits off the JTAG bus via the USB device
260
 
261
The return value of this should remain uint32_t as we're returning all the
262
data in a signal variable. We never need more than 32-bits in a single read
263
when using this function.
264
 
265
@param[in] num_bits  Number of bits to read off from the USB
266
@param[in] dwTapControllerState  State to leave the JTAG TAP in when done
267
@return: The data read from the USB device                                   */
268
/*---------------------------------------------------------------------------*/
269
uint32_t usb_read_stream(uint32_t num_bits, DWORD dwTapControllerState){
270
  ReadDataByteBuffer ReadDataBuffer;
271
  FTC_STATUS Status = FTC_SUCCESS;
272
  uint32_t returnVal =0;
273
  uint32_t i;
274
 
275
  // Platform independant driver call
276
  Status = FT2232_USB_JTAG_ReadDataFromExternalDevice(false, num_bits, &ReadDataBuffer, &dwNumBytesReturned, dwTapControllerState);
277
 
278
  if (DEBUG_USB_DRVR_FUNCS)
279
    printf("usb_read_stream: returned Status: 0x%lx from reading %u bits, \n",
280
           Status,num_bits);
281
 
282
  if (Status == FTC_SUCCESS){
283
 
284
    for(i=0;i<num_bits;i++){
285
      returnVal |= ((ReadDataBuffer[i/8]>>(i%8))&0x1)<<(num_bits-1-i);
286
    }
287
  }
288
  // Generate the CRC for read
289
  for (i=0;i<num_bits;i++)
290
      crc_r = crc_calc(crc_r, ((returnVal>>(num_bits-1-i))&1));
291
 
292
  return returnVal;
293
}
294
 
295
/* Sets TAP instruction register */
296
void usb_set_tap_ir(uint32_t ir) {
297
 
298
  WriteDataByteBuffer WriteDataBuffer;
299
  FTC_STATUS Status = FTC_SUCCESS;
300
 
301
  WriteDataBuffer[0] = ir;
302
 
303
  // Using global ftHandle, writing to TAP instruction register 4 bits, 
304
  //Status = pFT2232cMpsseJtag->JTAG_WriteDataToExternalDevice(ftHandle, true, 
305
  //JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
306
  // Platform independant driver call
307
 
308
  Status = FT2232_USB_JTAG_WriteDataToExternalDevice(true, JI_SIZE, &WriteDataBuffer, 1, RUN_TEST_IDLE_STATE);
309
 
310
  if (DEBUG_USB_DRVR_FUNCS)
311
    printf("USB JTAG write of %x to instruction register returned Status: 0x%lx\n",
312
           ir, Status);
313
  current_chain = -1;
314
}
315
 
316
static uint32_t id_read_at_reset = 0;
317
 
318
/* Resets JTAG, and sets DEBUG scan chain */
319
int usb_dbg_reset() {
320
 
321
  // uint32_t err;
322
  uint32_t id;
323
  uint32_t reinit_count=0;
324
 retry_jtag_init:
325
  if (init_usb_jtag() > 0)
326
    return DBG_ERR_CRC;
327
 
328
  // Set ID code instruction in IR
329
  usb_set_tap_ir(JI_IDCODE);
330
 
331
  // Now read out the IDCODE for the device
332
  id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
333
 
334
  // if read ID was rubbish retry init - this is probably NOT the best way to do this...
335
  if ((id == 0xffffffff) | (id == 0x00000002) | (id == 0x00000000)) {
336
    // Platform independant driver call
337
    FT2232_USB_JTAG_CloseDevice();
338
    if (reinit_count++ > 4){
339
      printf("JTAG TAP ID read error. Unable to detect TAP controller. \nPlease ensure debugger is connected to target correctly.\n");
340
      exit(1);
341
    }
342
    goto retry_jtag_init;
343
  }
344
 
345
  printf("JTAG ID = %08x\n", id & 0xffffffff);
346
 
347
  /* select debug scan chain and stay in it forever */
348
  usb_set_tap_ir(JI_DEBUG);
349
 
350
  id_read_at_reset = id; // Store this to check later if there's errors
351
 
352
  current_chain = -1;
353
  return DBG_ERR_OK;
354
}
355
 
356 47 julius
static void reset_tap(void)
357
{
358
  uint32_t id = 0;
359
  reinit_usb_jtag();
360
 
361
 
362
  while (id != id_read_at_reset)
363
    {
364
      usb_set_tap_ir(JI_IDCODE);
365
      id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
366
      //printf("reset_tap: read ID %.8x\n",id);
367
    }
368
  //Return the chain to DEBUG mode
369
  usb_set_tap_ir(JI_DEBUG);
370
 
371
}
372
 
373
 
374 39 julius
/* counts retries and returns zero if we should abort */
375
static int retry_no = 0;
376
int retry_do() {
377
 
378
  unsigned char stalled;
379 45 julius
  int tap_id_reads = 0;
380 39 julius
 
381
  //printf("RETRY\n");
382
  if (retry_no == 0)
383
    printf("Communication error. Retrying\n");
384
 
385
  retry_no++;
386
 
387
  // Odds are if we're having a communication problem, we should 
388
  // just reconnect to the processor. It's probably just been reset.
389
  /*
390
    FT2232_USB_JTAG_CloseDevice();// Close the device
391
 
392
    if (init_usb_jtag() > 0)
393
    {
394
    if (retry_no >= NUM_HARD_RETRIES)
395
    return 1;
396
    else
397
    return 0;
398
    }
399
  */
400
 
401
  // Try a readback of the TAP ID
402 45 julius
 read_tap:
403 39 julius
  // Set ID code instruction in IR
404
  usb_set_tap_ir(JI_IDCODE);
405
 
406
  // Now read out the IDCODE for the device
407
  uint32_t id = usb_read_stream(32, RUN_TEST_IDLE_STATE);
408
 
409
  //Return the chain to DEBUG mode
410
  usb_set_tap_ir(JI_DEBUG);
411
 
412
  if((id&0xffffffff) != (id_read_at_reset&0xffffffff))
413
    {
414 45 julius
      if (tap_id_reads == 10)
415
        {
416
          // Pretty big problem - can't even read the ID of the TAP anymore
417
          // So return error
418
          printf("Unable to read JTAG TAP ID - read %08x, expected %08x\n",
419
                 id, id_read_at_reset);
420
 
421
          return 1;
422
        }
423
 
424
      tap_id_reads++;
425
 
426
      goto read_tap;
427 39 julius
    }
428
 
429
  current_chain = -1;
430
 
431
  if (retry_no == 1)
432
    sleep(1);
433
 
434
  else if ( retry_no < NUM_SOFT_RETRIES)
435
    return 0; // Just attempt again    
436
 
437
  if ((retry_no >= NUM_SOFT_RETRIES) && (retry_no < NUM_SOFT_RETRIES + NUM_HARD_RETRIES) )
438
      {
439
        // Attempt a stall of the processor and then we'll try again
440
        //usb_dbg_test();
441
        printf("Resetting or1k\n");
442
        err = dbg_cpu0_write_ctrl(0, 0x02);      // reset or1k
443
 
444
        printf("Stalling or1k\n");
445
        err = dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
446
 
447
        err = dbg_cpu0_read_ctrl(0, &stalled);
448
        if (!(stalled & 0x1)) {
449
          printf("or1k should be stalled\n");   // check stall or1k
450
          FT2232_USB_JTAG_CloseDevice();// Close the device
451
          exit(1);
452
        }
453
 
454
        //retry_no++;
455
        return 0;
456
      }
457
    else // Unable to get the processor going again, return 1
458
      return 1;
459
 
460
}
461
 
462
/* resets retry counter */
463
void retry_ok() {
464
        retry_no = 0;
465
}
466
 
467
/* Sets scan chain.  */
468
int usb_dbg_set_chain(int chain) {
469
  uint32_t status, crc_generated, crc_read;
470
  dbg_chain = chain;
471
 
472
 try_again:
473
  if (current_chain == chain) return DBG_ERR_OK;
474
  current_chain = -1;
475
  if (DEBUG_CMDS) printf("\n");
476
  if (DEBUG_CMDS) printf("set_chain %i\n", chain);
477
 
478
  crc_w = 0xffffffff; // reset crc write variable
479
 
480
  // CRC generated in usb_read/write_stream functions
481
 
482
  usb_write_stream((((bit_reverse_data(chain,DBGCHAIN_SIZE) & 0xf) << 1) | 1),
483
                   DBGCHAIN_SIZE+1 , PAUSE_TEST_DATA_REGISTER_STATE);
484
 
485
  usb_write_stream(bit_reverse_data(crc_w, DBG_CRC_SIZE),DBG_CRC_SIZE,
486
                   PAUSE_TEST_DATA_REGISTER_STATE);
487
 
488
  crc_r = 0xffffffff; // reset the crc_r variable
489
 
490
  status = (usb_read_stream(DC_STATUS_SIZE,
491
                            PAUSE_TEST_DATA_REGISTER_STATE) & 0xf);
492
 
493
  crc_generated = crc_r;
494
 
495
  crc_read = usb_read_stream(DBG_CRC_SIZE, RUN_TEST_IDLE_STATE);
496
  //printf("%x %x %x\n",status, crc_generated, crc_read);
497
 
498
  /* CRCs must match, otherwise retry */
499
  if (crc_read != crc_generated) {
500
     if (!retry_do()) goto try_again;
501
    else return DBG_ERR_CRC;
502
  }
503
  /* we should read expected status value, otherwise retry */
504
  if (status != 0) {
505
    if (!retry_do()) goto try_again;
506
    else return status;
507
  }
508
 
509
  /* reset retry counter */
510
  retry_ok();
511
 
512
  current_chain = chain;
513
  return DBG_ERR_OK;
514
}
515
 
516
 
517
 
518
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
519
int usb_dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
520
  uint32_t i,status, crc_generated, crc_read;
521
 
522
   // JTAG driver things
523
  FTC_STATUS Status = FTC_SUCCESS;
524
  WriteDataByteBuffer WriteDataBuffer;
525
  ReadDataByteBuffer ReadDataBuffer;
526 79 julius
  retries = 0;
527 39 julius
 try_again:
528
  usb_dbg_set_chain(dbg_chain);
529
  if (DEBUG_CMDS) printf("\n");
530
  if (DEBUG_CMDS) printf("comm %d %d %8x \n", type,len,adr);
531
 
532
  //Calculate CRCs first
533
  crc_w = 0xffffffff;
534
 
535
  for (i=0;i<DBGCHAIN_SIZE+1;i++)
536
    crc_w = crc_calc(crc_w, (DI_WRITE_CMD_5BITREVERSED >> i)  & 1);
537
    //crc_w = crc_calc(crc_w,
538
    //       ((bit_reverse_data((DI_WRITE_CMD & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
539
 
540
  for (i=0;i<4;i++)
541
    crc_w = crc_calc(crc_w,((bit_reverse_data(type,4))>>i)&1);
542
 
543
  for (i=0;i<32;i++)
544
    crc_w = crc_calc(crc_w,((bit_reverse_data(adr,32))>>i)&1);
545
 
546
  assert(len > 0);
547
  for (i=0;i<16;i++)
548
    crc_w = crc_calc(crc_w,((bit_reverse_data(len-1,16))>>i)&1);
549
 
550
 
551
 
552
  // Now pack the write data buffer
553
  // 1-bit 0, 4-bits cmd, 4-bit type, 32-bit adr, 32-bit CRC
554
  // [0]
555
  //bits 0-4
556
  WriteDataBuffer[0]=(DI_WRITE_CMD_5BITREVERSED);
557
  //bits 5-7 
558
  WriteDataBuffer[0] |= ((bit_reverse_data(type,4)&0x7)<<5);
559
  // [1]
560
  // bit 0 - last bit of type
561
  WriteDataBuffer[1] = ((bit_reverse_data(type,4)&0x08)>>3);
562
  //bits 1-7 - first 7 bits of adr
563
  WriteDataBuffer[1] |= ((bit_reverse_data(adr,32)&0x07f)<<1);
564
  //[2-4] 24 bits of adr
565
  for(i=0;i<3;i++)
566
    WriteDataBuffer[2+i] = (bit_reverse_data(adr,32)>>(7+(i*8)))&0xff;
567
  // [5] last bit of adr in bit 0, first 7 bits of len-1 follow
568
  WriteDataBuffer[5] = (bit_reverse_data(adr,32)>>31)&1;
569
  WriteDataBuffer[5] |= (bit_reverse_data(len-1,16)&0x7f)<<1;
570
  // [6] bits 7-14 of (len-1)
571
  WriteDataBuffer[6] = (bit_reverse_data(len-1,16)>>7)&0xff;
572
  // [7] - last bit of len-1 and first 7 bits of the CRC
573
  WriteDataBuffer[7] = (bit_reverse_data(len-1,16)>>15)&1;
574
  //Reverse the CRC first
575
  crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
576
  WriteDataBuffer[7] |= (crc_w&0x7f)<<1;
577
  //[8-10] next 24 bits (7-30) of crc_w
578
  WriteDataBuffer[8] = (crc_w>>7)&0xff;
579
  WriteDataBuffer[9] = (crc_w>>15)&0xff;
580
  WriteDataBuffer[10] = (crc_w>>23)&0xff;
581
  //[11] final bit of crc_w
582
  WriteDataBuffer[11] = (crc_w>>31)&1;
583
 
584
  //  Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
585
  // Platform independant driver call
586
  Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
587
 
588
  if (Status != FTC_SUCCESS)
589
    printf("USB write fail - code %ld\b",Status);
590
 
591
  // Now look through the read data
592
 
593
  // From bit1 of ReadDataBuffer[11] we should have our 4-bit status
594
  status = (ReadDataBuffer[11] >> 1) & 0xf;
595
 
596
  // Now extract the received CRC
597
  crc_read = 0;
598
  //first 3 bits (0-2)
599
  crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
600
  // middle 3 bytes (bits 3 to 26)
601
  for (i=0;i<3;i++)
602
    crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
603
  // last 5 bits from ReadDataBuffer[15]
604
  crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
605
 
606
  // Now calculate CRC on status
607
  crc_r = 0xffffffff;
608
  for(i=0;i<DC_STATUS_SIZE;i++)
609
    crc_r = crc_calc(crc_r, (status>>i)&1);
610
 
611
  crc_generated = crc_r;
612
  // Now bit reverse status and crc_read as we unpacked them
613
  // with the MSb going to the LSb
614
  status = bit_reverse_data(status, DC_STATUS_SIZE);
615
  crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
616
 
617
  //printf("%x %x %x\n", status, crc_read, crc_generated);
618
  /* CRCs must match, otherwise retry */
619 79 julius
  //uint32_t tries = 0;
620 39 julius
  if (crc_read != crc_generated) {
621 79 julius
    retries++;
622
    if (retries < 2)
623 47 julius
      {
624
        if (DEBUG_USB_DRVR_FUNCS)
625
          printf("usb_functions - usb_dbg_command - CRC fail. Going again\n");
626
        goto try_again;
627
      }
628 79 julius
    else if (retries < 8)
629 47 julius
      {
630
        reset_tap();
631
        goto try_again;
632
      }
633 39 julius
    else return DBG_ERR_CRC;
634
  }
635
  /* we should read expected status value, otherwise retry */
636
  if (status != 0) {
637 79 julius
    if (retries < 2)
638 47 julius
      {
639
        if (DEBUG_USB_DRVR_FUNCS)
640
          printf("usb_functions - usb_dbg_command - bad status (%d). Going again\n",status);
641
        goto try_again;
642
      }
643 79 julius
    else if (retries < 8)
644 47 julius
      {
645
        reset_tap();
646
        goto try_again;
647
      }
648 39 julius
    else return status;
649 47 julius
 
650 39 julius
  }
651
  /* reset retry counter */
652
  retry_ok();
653
 
654
  return DBG_ERR_OK;
655
}
656
 
657
 
658
/* writes a ctrl reg */
659
int usb_dbg_ctrl(uint32_t reset, uint32_t stall) {
660
  uint32_t i,status, crc_generated, crc_read;
661
  // JTAG driver things
662
  FTC_STATUS Status = FTC_SUCCESS;
663
  WriteDataByteBuffer WriteDataBuffer;
664
  ReadDataByteBuffer ReadDataBuffer;
665 79 julius
  retries = 0;
666 39 julius
try_again:
667
  usb_dbg_set_chain(dbg_chain);
668
        if (DEBUG_CMDS) printf("\n");
669
        if (DEBUG_CMDS) printf("ctrl\n");
670
        if (DEBUG_CMDS) printf("reset %x stall %x\n", reset, stall);
671
 
672
        crc_w = 0xffffffff;
673
        // Try packing everyhing we want to send into one write buffer
674
        //Calculate CRCs first
675
        for (i=0;i<DBGCHAIN_SIZE+1;i++)
676
          crc_w = crc_calc(crc_w, (DI_WRITE_CTRL_5BITREVERSED>>i)&1);
677
          //crc_w = crc_calc(crc_w, 
678
          //       ((bit_reverse_data((DI_WRITE_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
679
        crc_w = crc_calc(crc_w,(reset&1));
680
        crc_w = crc_calc(crc_w,(stall&1));
681
        for (i=0;i<50;i++)
682
          crc_w = crc_calc(crc_w,0);
683
 
684
 
685
 
686
        // Now pack the write data buffer
687
        // 1-bit 0, 4-bits cmd, 52-bits CPU control register data (only first 2 matter)
688
        //bits 0-4
689
        WriteDataBuffer[0]=(DI_WRITE_CTRL_5BITREVERSED);
690
        // bit 5
691
        WriteDataBuffer[0] |= (reset)<<(DBGCHAIN_SIZE+1);
692
        // bit 6
693
        WriteDataBuffer[0] |= (stall)<<(DBGCHAIN_SIZE+2);
694
        // Clear the next 48 bits
695
        for (i=1; i<16;i++)   //actually clear more than just the next 50 bits
696
          WriteDataBuffer[i] = 0;
697
 
698
        //Reverse the CRC first
699
        crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
700
        //Now load in the CRC, but take note of fact 
701
        // that bit 0 of buffer[7] is last 0 from cmd register data
702
        // fill up from WriteDataBuffer[7-11]
703
        for (i=0;i<4;i++)
704
          WriteDataBuffer[7+i] = ((crc_w<<1)>>(i*8))&0xff;
705
        //Final bit, shift in and make sure is the only thing int he buffer
706
        WriteDataBuffer[11]=0|((crc_w>>31)&1);
707
 
708
 
709
        // Platform independant driver call
710
        Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,89+4+32 , &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
711
 
712
 
713
        if (Status != FTC_SUCCESS)
714
          printf("USB write fail - code %ld\b",Status);
715
 
716
        // Now look through the read data
717
 
718
        // From bit1 of ReadDataBuffer[11] we should have our 4-bit status
719
        status = (ReadDataBuffer[11] >> 1) & 0xf;
720
 
721
        // Now extract the received CRC
722
        crc_read = 0;
723
        //first 3 bits (0-2)
724
        crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
725
        // middle 3 bytes (bits 3 to 26)
726
        for (i=0;i<3;i++)
727
          crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
728
        // last 5 bits from ReadDataBuffer[15]
729
        crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
730
 
731
 
732
        // Now calculate CRC on status and crc_read
733
        crc_r = 0xffffffff;
734
        for(i=0;i<DC_STATUS_SIZE;i++)
735
          crc_r = crc_calc(crc_r, (status>>i)&1);
736
 
737
        crc_generated = crc_r;
738
        // Now bit reverse status and crc_read as we unpacked them
739
        // with the MSb going to the LSb
740
        status = bit_reverse_data(status, DC_STATUS_SIZE);
741
        crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
742
 
743
        /* CRCs must match, otherwise retry */
744
        //printf("%x %x %x\n", status, crc_read, crc_generated);
745 47 julius
          if (crc_read != crc_generated) {
746 79 julius
          retries++;
747
          if (retries < 2)
748 47 julius
            {
749
              if (DEBUG_USB_DRVR_FUNCS)
750
                printf("usb_functions - usb_dbg_ctrl - CRC fail. Going again\n");
751
              goto try_again;
752
            }
753 79 julius
          else if (retries < 8)
754 47 julius
            {
755
              reset_tap();
756
              goto try_again;
757
            }
758 39 julius
          else return DBG_ERR_CRC;
759
        }
760
        /* we should read expected status value, otherwise retry */
761 79 julius
        retries = 0;
762 39 julius
        if (status != 0) {
763 79 julius
          if (retries < 2)
764 47 julius
            {
765
              if (DEBUG_USB_DRVR_FUNCS)
766
                printf("usb_functions - usb_dbg_ctrl - bad status (%d). Going again\n",status);
767
              goto try_again;
768
            }
769 79 julius
          else if (retries < 8)
770 47 julius
            {
771
              reset_tap();
772
              goto try_again;
773
            }
774 39 julius
          else return status;
775 47 julius
 
776 39 julius
        }
777
 
778
        /* reset retry counter */
779
        retry_ok();
780
        return DBG_ERR_OK;
781
}
782
 
783
 
784
/* reads control register */
785
int usb_dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
786
  uint32_t i, status, crc_generated, crc_read;
787
 
788
  // JTAG driver things
789
  FTC_STATUS Status = FTC_SUCCESS;
790
  WriteDataByteBuffer WriteDataBuffer;
791
  ReadDataByteBuffer ReadDataBuffer;
792 79 julius
  retries = 0;
793
 
794 39 julius
 try_again:
795
  usb_dbg_set_chain(dbg_chain);
796
  if (DEBUG_CMDS) printf("\n");
797
  if (DEBUG_CMDS) printf("ctrl_read\n");
798
 
799
  crc_w = 0xffffffff;
800
  // Try packing everyhing we want to send into one write buffer
801
  //Calculate CRCs first
802
  for (i=0;i<DBGCHAIN_SIZE+1;i++)
803
    crc_w = crc_calc(crc_w, (DI_READ_CTRL_5BITREVERSED>>i)&1);
804
    //crc_w = crc_calc(crc_w, 
805
    //((bit_reverse_data((DI_READ_CTRL & 0xf),DBGCHAIN_SIZE+1))>>i)&1);
806
 
807
 
808
  // Now pack the write data buffer
809
  // 1-bit 0, 4-bits cmd, 32-bit CRC
810
  //bits 0-4
811
  WriteDataBuffer[0]=(DI_READ_CTRL_5BITREVERSED);
812
  // Clear the next 48 bits
813
  for (i=1; i<16;i++)   //actually clear more than just the next 50 bits
814
    WriteDataBuffer[i] = 0;
815
 
816
  //Reverse the CRC first
817
  crc_w = bit_reverse_data(crc_w, DBG_CRC_SIZE);
818
  //Now load in the CRC
819
  //First 3 bits go in last 3 bits of buffer[0]
820
  WriteDataBuffer[0] |= (crc_w & 0x7)<<5;
821
  // The rest of crc_w goes in buffer[1-4]
822
  for (i=0;i<3;i++)
823
    WriteDataBuffer[1+i] = ((crc_w>>3)>>(i*8))&0xff;
824
  //Final bit of write buffer with CRC
825
  WriteDataBuffer[4] = ((crc_w>>3)>>(24))&0x1f;
826
 
827
 
828
 
829
  //Status = pFT2232cMpsseJtag->JTAG_WriteReadDataToFromExternalDevice(gFtHandle,false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
830
  // Platform independant driver call
831
  Status = FT2232_USB_JTAG_WriteReadDataToFromExternalDevice(false,5+32+52+4+32, &WriteDataBuffer, 16, &ReadDataBuffer, &dwNumBytesReturned, RUN_TEST_IDLE_STATE);
832
  if (Status != FTC_SUCCESS)
833
    printf("USB read fail - code %ld\b",Status);
834
 
835
  // Now look through the read data
836
  //0 - 1+4-bit status, 3-bits CRC
837
  //1,2,3 - CRC
838
  //4 - first 5 bits CRC, last 3, control reg bits (first 3)
839
  //5,6,7,8,9,10 - control reg data (48 bits)
840
  //11 - bit0 - control reg data, bit 1-4 status bits, bits 5-7 CRC
841
  //12, 13 14 - CRC
842
  // 15 bits 0-4 CRC
843
  // Starting from bit1 of ReadDataBuffer[11] we should have our 4-bit status
844
  status = (ReadDataBuffer[11] >> 1) & 0xf;
845
 
846
  //reset bit should be in ReadDataBuffer[4] as bit 5
847
  *reset = (ReadDataBuffer[4] >> 5) & 1;
848
  //stalled bit should be in ReadDataBuffer[4] as bit 6
849
  *stall = (ReadDataBuffer[4] >> 6) & 1;
850
  // Now extract the received CRC
851
  crc_read = 0;
852
  //first 3 bits (0-2) of CRC are in bits 5-7 of ReadDataBuffer[11]
853
  crc_read |= (ReadDataBuffer[11] >> 5) & 0x7;
854
  // middle 3 bytes (bits 3 to 26)
855
  for (i=0;i<3;i++)
856
    crc_read |= ((ReadDataBuffer[12+i]&0xff) << ((i*8)+3));
857
  // last 5 bits from ReadDataBuffer[15]
858
  crc_read |= (ReadDataBuffer[15]&0x1f)<<27;
859
 
860
  if (DEBUG_CMDS) printf("reset bit %x stalled bit %x:\n",
861
         ((ReadDataBuffer[4] >> 5) & 1), ((ReadDataBuffer[4] >> 6) & 1));
862
 
863
  // Now calculate CRC on status and crc_read
864
  crc_r = 0xffffffff;
865
 
866
  crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 5) & 1));
867
  crc_r = crc_calc(crc_r, ((ReadDataBuffer[4] >> 6) & 1));
868
  for(i=0;i<50;i++)
869
    crc_r = crc_calc(crc_r,0);
870
  for(i=0;i<DC_STATUS_SIZE;i++)
871
    crc_r = crc_calc(crc_r, (status>>i)&1);
872
 
873
  crc_generated = crc_r;
874
  // Now bit reverse status and crc_read as we unpacked them
875
  // with the MSb going to the LSb
876
  status = bit_reverse_data(status, DC_STATUS_SIZE);
877
  crc_read = bit_reverse_data(crc_read, DBG_CRC_SIZE);
878 47 julius
 
879 39 julius
 
880
  /* CRCs must match, otherwise retry */
881
  //printf("%x %x %x\n", status, crc_generated, crc_read);
882
  if (crc_read != crc_generated) {
883 79 julius
    retries++;
884
    if (retries < 2)
885 47 julius
      {
886
        if (DEBUG_USB_DRVR_FUNCS)
887
          printf("usb_functions - usb_dbg_ctrl_read - CRC fail. Going again\n");
888
        goto try_again;
889
      }
890 79 julius
    else if (retries < 8)
891 47 julius
      {
892
        reset_tap();
893
        goto try_again;
894
      }
895 39 julius
    else return DBG_ERR_CRC;
896 79 julius
  }
897 39 julius
  /* we should read expected status value, otherwise retry */
898 79 julius
  retries = 0;
899 39 julius
  if (status != 0) {
900 79 julius
    if (retries < 2)
901 47 julius
      {
902
        if (DEBUG_USB_DRVR_FUNCS)
903
          printf("usb_functions - usb_dbg_ctrl_read - bad status (%d). Going again\n",status);
904
        goto try_again;
905
      }
906 79 julius
    else if (retries < 8)
907 47 julius
      {
908
        reset_tap();
909
        goto try_again;
910
      }
911 39 julius
    else return status;
912 47 julius
 
913 39 julius
  }
914 47 julius
 
915 39 julius
  /* reset retry counter */
916
  retry_ok();
917
  return DBG_ERR_OK;
918
}
919
 
920
 
921
/* issues a burst read/write */
922
int usb_dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
923
        uint32_t status, crc_generated, crc_read;
924
        int i,j;
925
        uint8_t data_byte;
926 79 julius
        retries = 0;
927 39 julius
        // 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 79 julius
          retries++;
1081
          if (retries < 8)
1082 47 julius
            {
1083 79 julius
              if (DEBUG_USB_DRVR_FUNCS) printf("usb_functions - usb_dbg_go - CRC fail (%d) try %d. Going again\n",status, retries);
1084 47 julius
              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 79 julius
          retries++;
1094
          if (retries < 8)
1095 47 julius
            {
1096 79 julius
              if (DEBUG_USB_DRVR_FUNCS) printf("usb_functions - usb_dbg_go - bad status (%d) try %d. Going again\n",status, retries);
1097 47 julius
              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 94 julius
 
1110 39 julius
/* read a word from wishbone */
1111 94 julius
int usb_dbg_wb_read8(uint32_t adr, uint8_t *data) {
1112
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1113
  if ((err = usb_dbg_command(DBG_WB_READ8, adr, 1))) return err;
1114
  if ((err = usb_dbg_go((unsigned char*)data, 1, 1))) return err;
1115
  //*data = ntohl(*data);
1116
  return err;
1117
}
1118
 
1119
/* read a word from wishbone */
1120 39 julius
int usb_dbg_wb_read32(uint32_t adr, uint32_t *data) {
1121
  // uint32_t err;
1122
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1123 46 julius
  if ((err = usb_dbg_command(DBG_WB_READ32, adr, 4))) return err;
1124 39 julius
  if ((err = usb_dbg_go((unsigned char*)data, 4, 1))) return err;
1125 46 julius
  //*data = ntohl(*data);
1126 39 julius
  return err;
1127
}
1128
 
1129 94 julius
/* write a byte to wishbone */
1130 46 julius
int usb_dbg_wb_write8(uint32_t adr, uint8_t data) {
1131
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1132
  if ((err = usb_dbg_command(DBG_WB_WRITE8, adr, 1))) return err;
1133
  if ((err = usb_dbg_go((unsigned char*)&data, 1, 0))) return err;
1134
  return DBG_ERR_OK;
1135
}
1136
 
1137
 
1138
/* write a word to wishbone */
1139 39 julius
int usb_dbg_wb_write32(uint32_t adr, uint32_t data) {
1140
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1141 46 julius
  if ((err = usb_dbg_command(DBG_WB_WRITE32, adr, 4))) return err;
1142 39 julius
  if ((err = usb_dbg_go((unsigned char*)&data, 4, 0))) return err;
1143
  return DBG_ERR_OK;
1144
}
1145
 
1146
/* read a block from wishbone */
1147
int usb_dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
1148
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1149 46 julius
  if ((err = usb_dbg_command(DBG_WB_READ32, adr, len))) return err;
1150 39 julius
  if ((err = usb_dbg_go((unsigned char*)data, len, 1))) return err;
1151
  return DBG_ERR_OK;
1152
}
1153
 
1154
 
1155
/* write a block to wishbone */
1156
int usb_dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
1157 47 julius
  if (DEBUG_CMDS) printf("usb_functions: wb_write_block %.8x %d bytes\n",adr, len);
1158 39 julius
  if ((err = usb_dbg_set_chain(DC_WISHBONE))) return err;
1159 46 julius
  if ((err = usb_dbg_command(DBG_WB_WRITE32, adr, len))) return err;
1160 39 julius
  if ((err = usb_dbg_go((unsigned char*)data, len, 0))) return err;
1161
  return DBG_ERR_OK;
1162
}
1163
 
1164
 
1165
/* read a register from cpu */
1166 47 julius
int usb_dbg_cpu0_read(uint32_t adr, uint32_t *data, uint32_t length) {
1167 39 julius
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1168 47 julius
  if ((err = usb_dbg_command(DBG_CPU_READ, adr, length))) return err;
1169
  if ((err = usb_dbg_go((unsigned char*)data, length, 1))) return err;
1170
  int i;for(i=0;i<(length/4);i++)data[i]=ntohl(data[i]);
1171 39 julius
  return DBG_ERR_OK;
1172
}
1173
 
1174
/* write a cpu register */
1175 47 julius
int usb_dbg_cpu0_write(uint32_t adr, uint32_t *data, uint32_t length) {
1176
  int i;for(i=0;i<(length/4);i++){data[i]=ntohl(data[i]);}
1177 39 julius
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1178 47 julius
  if ((err = usb_dbg_command(DBG_CPU_WRITE, adr, length))) return err;
1179
  if ((err = usb_dbg_go((unsigned char*)data, length, 0))) return err;
1180 39 julius
  return DBG_ERR_OK;
1181
}
1182
 
1183
/* write a cpu module register */
1184
int usb_dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
1185
  // no ensuring that or1k is stalled here, becuase we're call this from that function
1186
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1187
  if ((err = usb_dbg_ctrl(data >> 1, data & 0x1))) return err;
1188
  return DBG_ERR_OK;
1189
}
1190
 
1191
/* read a register from cpu module */
1192
int usb_dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
1193
  // no ensuring that or1k is stalled here, becuase we're call this from that function
1194
  uint32_t r, s;
1195
  if ((err = usb_dbg_set_chain(DC_CPU0))) return err;
1196
  if ((err = usb_dbg_ctrl_read(&r, &s))) return err;
1197
  *data = (r << 1) | s;
1198
  return DBG_ERR_OK;
1199
}
1200
 
1201
/* Function to close the device handle. Is called when closing the app */
1202
void usb_close_device_handle()
1203
{
1204
  // try unstalling the processor before quitting
1205
  dbg_cpu0_write_ctrl(0, 0x00);      // unstall or1k
1206
  FT2232_USB_JTAG_CloseDevice();
1207
}

powered by: WebSVN 2.1.0

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