OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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

powered by: WebSVN 2.1.0

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