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

Subversion Repositories openrisc

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

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

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

powered by: WebSVN 2.1.0

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