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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Software/] [adv_jtag_bridge/] [cable_xpc_dlc9.c] - Blame information for rev 59

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 59 nyawn
/* cable_xpc_dlc9.c - Xilinx Platform Cable (DLC9) driver for the Advanced JTAG Bridge
2
   Copyright (C) 2008 - 2010 Nathan Yawn, nathan.yawn@opencores.org
3
   Copyright (C) 2008 Kolja Waschk (UrJTAG project)
4
 
5
   CPLD mode for burst transfers added by:
6
           Copyright (C) 2011 Raul Fajardo, rfajardo@opencores.org
7
   adapted from xc3sprog/ioxpc.cpp:
8
           Copyright (C) 2009-2011 Uwe Bonnes bon@elektron.ikp.physik.tu-darmstadt.de
9
 
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation; either version 2 of the License, or
13
(at your option) any later version.
14
 
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
GNU General Public License for more details.
19
 
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23
 
24
 
25
#include <stdio.h>
26
#include <sys/types.h>
27
#include <stdlib.h>  // for sleep()
28
#include <errno.h>
29
#include <string.h>
30
 
31
#include "usb.h"  // libusb header
32
 
33
#include "cable_xpc_dlc9.h"
34
#include "utilities.h"
35
#include "errcodes.h"
36
 
37
/*
38
 * The dynamic switch between FX2 and CPLD modes works fine. If a switch is required, the functions:
39
 *              static int cable_xpcusb_fx2_init();
40
 *              static int cable_xpcusb_cpld_init();
41
 * can be called. The variable cpld_ctrl can tell if the CPLD is active (1) or FX2 (0).
42
 *
43
 * The functions accessing the cable in this driver always check the cpld_ctrl variable and adapt the
44
 * cable mode accordingly.
45
 *
46
 * Therefore, we can arbitrarily define bit and stream functionality from different modes without
47
 * concern. Out_func and inout_func are not provided under CPLD mode because it always complete
48
 * a bit transfer by toggling the clock twice 0-1-0 providing a write and a read.
49
 *
50
 * When using stream functionality of the CPLD, and bit functionality of FX2, also cable_common_read_write_bit
51
 * works. The cable_xpcusb_read_write_bit is not necessary then. However, it is still necessary when using:
52
 *                      cable_common_write_stream
53
 *                      cable_common_read_stream
54
 */
55
 
56
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
57
 
58
//#define CLASSIC
59
#define CPLDBITFUNC
60
 
61
#ifdef CLASSIC
62
jtag_cable_t dlc9_cable_driver = {
63
    .name ="xpc_usb" ,
64
    .inout_func = cable_xpcusb_inout,
65
    .out_func = cable_xpcusb_out,
66
    .init_func = cable_xpcusb_init,
67
    .opt_func = cable_xpcusb_opt,
68
    .bit_out_func = cable_common_write_bit,
69
    .bit_inout_func = cable_xpcusb_read_write_bit,
70
    .stream_out_func = cable_common_write_stream,
71
    .stream_inout_func = cable_common_read_stream,
72
    .flush_func = NULL,
73
    .opts = "",
74
    .help = "no options\n",
75
   };
76
#else
77
jtag_cable_t dlc9_cable_driver = {
78
    .name ="xpc_usb" ,
79
    .inout_func = cable_xpcusb_inout,
80
    .out_func = cable_xpcusb_out,
81
    .init_func = cable_xpcusb_init,
82
    .opt_func = cable_xpcusb_opt,
83
#ifdef CPLDBITFUNC
84
    .bit_out_func = cable_xpcusb_cpld_write_bit,
85
    .bit_inout_func = cable_xpcusb_cpld_readwrite_bit,
86
#else
87
    .bit_out_func = cable_common_write_bit,
88
    .bit_inout_func = cable_common_read_write_bit,
89
#endif
90
    .stream_out_func = cable_xpcusb_write_stream,
91
    .stream_inout_func = cable_xpcusb_readwrite_stream,
92
    .flush_func = NULL,
93
    .opts = "",
94
    .help = "no options\n",
95
   };
96
#endif
97
 
98
#define USB_TIMEOUT 500
99
 
100
// USB constants for the DLC9
101
#define XPCUSB_VID  0x3fd
102
#define XPCUSB_PID  0x08
103
 
104
// Bit meanings in the command byte sent to the DLC9
105
// DLC9 has no TRST bit
106
#define XPCUSB_CMD_TDI 0x01
107
#define XPCUSB_CMD_TDO 0x01
108
#define XPCUSB_CMD_TMS 0x02
109
#define XPCUSB_CMD_TCK 0x04
110
#define XPCUSB_CMD_PROG 0x08
111
 
112
/*
113
 * send max 4096 bytes to CPLD
114
 * this is equal to 8192 TDI plus 8192 TDO bits
115
 */
116
#define CPLD_MAX_BYTES (1<<12)
117
 
118
/*
119
 * Buffer has to hold 8192 bits for write, each 2 bytes hold 4 bits for write, so this has to be 4096
120
 * Buffer has to hold 8192 bits for read, each byte holds 8 bits for read, so this has to be 1024
121
 * Therefore, buffer size -> CPLD_MAX_BYTES
122
 */
123
typedef struct
124
{
125
  int in_bits;
126
  int out_bits;
127
  uint8_t buf[CPLD_MAX_BYTES];
128
}  xpc_ext_transfer_state_t;
129
 
130
 
131
static struct usb_device *device = NULL;
132
static usb_dev_handle *h_device = NULL;
133
static int cpld_ctrl = 0;
134
 
135
static const uint32_t endianess_test = 1;
136
#define is_bigendian() ( (*(uint8_t*)&endianess_test) == 0 )
137
 
138
static int cable_xpcusb_open_cable(void);
139
static void cable_xpcusb_close_cable(void);
140
static int cable_xpcusb_fx2_init();
141
static int cable_xpcusb_cpld_init();
142
 
143
 
144
 
145
 
146
///////////////////////////////////////////////////////////////////////////////
147
/*----- Functions for the Xilinx Platform Cable USB (Model DLC9)            */
148
/////////////////////////////////////////////////////////////////////////////
149
 
150
static int xpcu_request_28(struct usb_dev_handle *xpcu, int value)
151
{
152
    /* Typical values seen during autodetection of chain configuration: 0x11, 0x12 */
153
 
154
    if(usb_control_msg(xpcu, 0x40, 0xB0, 0x0028, value, NULL, 0, 1000)<0)
155
    {
156
        perror("usb_control_msg(0x28.x)");
157
        return -1;
158
    }
159
 
160
    return 0;
161
}
162
 
163
static int xpcu_select_gpio(struct usb_dev_handle *xpcu, int chain)
164
{
165
  if(usb_control_msg(xpcu, 0x40, 0xB0, 0x0052, chain, NULL, 0, USB_TIMEOUT)<0)
166
    {
167
      fprintf(stderr, "Error sending usb_control_msg(0x52.x) (select gpio)\n");
168
      return APP_ERR_USB;
169
    }
170
 
171
  return APP_ERR_NONE;
172
}
173
 
174
static int xpcu_write_gpio(struct usb_dev_handle *xpcu, uint8_t bits)
175
{
176
    if(usb_control_msg(xpcu, 0x40, 0xB0, 0x0030, bits, NULL, 0, 1000)<0)
177
    {
178
        perror("usb_control_msg(0x30.0x00) (write port E)");
179
        return -1;
180
    }
181
 
182
    return 0;
183
}
184
 
185
 
186
static int xpcu_read_cpld_version(struct usb_dev_handle *xpcu, uint16_t *buf)
187
{
188
    if(usb_control_msg(xpcu, 0xC0, 0xB0, 0x0050, 0x0001, (char*)buf, 2, 1000)<0)
189
    {
190
        perror("usb_control_msg(0x50.1) (read_cpld_version)");
191
        return -1;
192
    }
193
    return 0;
194
}
195
 
196
 
197
static int xpcu_read_firmware_version(struct usb_dev_handle *xpcu, uint16_t *buf)
198
{
199
    if(usb_control_msg(xpcu, 0xC0, 0xB0, 0x0050, 0x0000, (char*)buf, 2, 1000)<0)
200
    {
201
        perror("usb_control_msg(0x50.0) (read_firmware_version)");
202
        return -1;
203
    }
204
 
205
    return 0;
206
}
207
 
208
static int xpcu_output_enable(struct usb_dev_handle *xpcu, int enable)
209
{
210
    if(usb_control_msg(xpcu, 0x40, 0xB0, enable ? 0x18 : 0x10, 0, NULL, 0, 1000)<0)
211
    {
212
        perror("usb_control_msg(0x10/0x18)");
213
        return -1;
214
    }
215
 
216
    return 0;
217
}
218
 
219
/*
220
 *   === A6 transfer (TDI/TMS/TCK/RDO) ===
221
 *
222
 *   Vendor request 0xA6 initiates a quite universal shift operation. The data
223
 *   is passed directly to the CPLD as 16-bit words.
224
 *
225
 *   The argument N in the request specifies the number of state changes/bits.
226
 *
227
 *   State changes are described by the following bulk write. It consists
228
 *   of ceil(N/4) little-endian 16-bit words, each describing up to 4 changes.
229
 *   (see xpcusb_add_bit_for_ext_transfer)
230
 *
231
 *   After the bulk write, if any of the bits 12..15 was set in any word
232
 *   (see xpcusb_add_bit_for_ext_transfer), a bulk_read shall follow to collect
233
 *   the TDO data.
234
 */
235
static int xpcu_shift(struct usb_dev_handle *xpcu, int reqno, int bits, int in_len, uint8_t *in, int out_len, uint8_t *out )
236
{
237
    if(usb_control_msg(xpcu, 0x40, 0xB0, reqno, bits, NULL, 0, 1000)<0)
238
    {
239
        perror("usb_control_msg(x.x) (shift)");
240
        return -1;
241
    }
242
 
243
#if VERBOSE
244
        {
245
        int i;
246
    printf("\n###\n");
247
    printf("reqno = %02X\n", reqno);
248
    printf("bits    = %d\n", bits);
249
    printf("in_len  = %d, in_len*2  = %d\n", in_len, in_len * 2);
250
    printf("out_len = %d, out_len*8 = %d\n", out_len, out_len * 8);
251
 
252
    printf("a6_display(\"%02X\", \"", bits);
253
    for(i=0;i<in_len;i++) printf("%02X%s", in[i], (i+1<in_len)?",":"");
254
    printf("\", ");
255
        }
256
#endif
257
 
258
    if(usb_bulk_write(xpcu, 0x02, (char*)in, in_len, 1000)<0)
259
    {
260
        fprintf(stderr, "\nusb_bulk_write error(shift): %s\n", strerror(errno));
261
        fprintf(stderr, "Burst length: %d\n", in_len);
262
        return -1;
263
    }
264
 
265
    if(out_len > 0 && out != NULL)
266
    {
267
      if(usb_bulk_read(xpcu, 0x86, (char*)out, out_len, 1000)<0)
268
      {
269
        printf("\nusb_bulk_read error(shift): %s\n", strerror(errno));
270
        return -1;
271
      }
272
    }
273
 
274
#if VERBOSE
275
        {
276
        int i;
277
    printf("\"");
278
    for(i=0;i<out_len;i++) printf("%02X%s", out[i], (i+1<out_len)?",":"");
279
    printf("\")\n");
280
        }
281
#endif
282
 
283
    return 0;
284
}
285
 
286
/*
287
 *   Bit 0: Value for first TDI to shift out.
288
 *   Bit 1: Second TDI.
289
 *   Bit 2: Third TDI.
290
 *   Bit 3: Fourth TDI.
291
 *
292
 *   Bit 4: Value for first TMS to shift out.
293
 *   Bit 5: Second TMS.
294
 *   Bit 6: Third TMS.
295
 *   Bit 7: Fourth TMS.
296
 *
297
 *   Bit 8: Whether to raise/lower TCK for first bit.
298
 *   Bit 9: Same for second bit.
299
 *   Bit 10: Third bit.
300
 *   Bit 11: Fourth bit.
301
 *
302
 *   Bit 12: Whether to read TDO for first bit
303
 *   Bit 13: Same for second bit.
304
 *   Bit 14: Third bit.
305
 *   Bit 15: Fourth bit.
306
 *   */
307
static void xpcusb_add_bit_for_ext_transfer(xpc_ext_transfer_state_t *xts, uint8_t toggle_tclk, uint8_t tms, uint8_t tdi, uint8_t sample_tdo)
308
{
309
        int bit_idx = (xts->in_bits & 3);
310
        int buf_idx = (xts->in_bits - bit_idx) >> 1;
311
 
312
        debug("add_bit, in = %i, bit_idx = %i, buf_idx = %i\n", tdi, bit_idx, buf_idx);
313
 
314
        if(bit_idx == 0)
315
        {
316
                xts->buf[buf_idx] = 0;
317
                xts->buf[buf_idx+1] = 0;
318
        }
319
 
320
        xts->in_bits++;
321
 
322
        if(tdi) xts->buf[buf_idx] |= (0x01<<bit_idx);
323
 
324
        if(tms) xts->buf[buf_idx] |= (0x10<<bit_idx);
325
 
326
        if ( toggle_tclk )      xts->buf[buf_idx+1] |= (0x01<<bit_idx);
327
 
328
        if(sample_tdo)
329
        {
330
                xts->buf[buf_idx+1] |= (0x10<<bit_idx);
331
                xts->out_bits++;
332
        }
333
}
334
 
335
/*
336
 *   TDO data is shifted in from MSB to LSB and transferred 32-bit little-endian.
337
 *   In a "full" word with 32 TDO bits, the earliest one reached bit 0.
338
 *   The earliest of 31 bits however would be bit 1. A 17 bit transfer has the LSB
339
 *   as the MSB of uint16_t[0], other bits are in uint16_t[1].
340
 *
341
 *   However, if the last packet is smaller than 16, only 2 bytes are transferred.
342
 *   If there's only one TDO bit, it arrives as the MSB of the 16-bit word, uint16_t[0].
343
 *   uint16_t[1] is then skipped.
344
 *
345
 *   For full 32 bits blocks, the data is aligned. The last non 32-bits block arrives
346
 *   non-aligned and has to be re-aligned. Half-words (16-bits) transfers have to be
347
 *   re-aligned too.
348
 */
349
static int xpcusb_do_ext_transfer(xpc_ext_transfer_state_t *xts, uint32_t * tdostream)
350
{
351
    int i, r;
352
    int in_len, out_len;
353
    int shift, bit_num, bit_val;
354
    uint32_t aligned_32bitwords, aligned_bytes;
355
    uint32_t out_done;
356
 
357
    //cpld expects data (tdi) to be in 16 bit words
358
    in_len = 2 * (xts->in_bits >> 2);
359
    if ((xts->in_bits & 3) != 0) in_len += 2;
360
 
361
    //cpld returns the read data (tdo) in 32 bit words
362
    out_len = 2 * (xts->out_bits >> 4);
363
    if ((xts->out_bits & 15) != 0) out_len += 2;
364
 
365
    r = xpcu_shift (h_device, 0xA6, xts->in_bits, in_len, xts->buf, out_len, xts->buf);
366
 
367
    if(r >= 0 && xts->out_bits > 0 && tdostream != NULL)
368
    {
369
        aligned_32bitwords = xts->out_bits/32;
370
        aligned_bytes = aligned_32bitwords*4;
371
        if ( is_bigendian() )                                                           //these data is aligned as little-endian
372
        {
373
                for (i=0; i<aligned_bytes; i++)
374
                {
375
                        if ( i%4 == 0 )
376
                                tdostream[i/4] = 0;
377
                        tdostream[i/4] |= xts->buf[i] << (i%4)*8;
378
                }
379
        }
380
        else
381
                memcpy(tdostream, xts->buf, aligned_bytes);             //these data is already little-endian
382
 
383
        out_done = aligned_bytes*8;
384
 
385
        //This data is not aligned
386
        if (xts->out_bits % 32)
387
        {
388
            shift =  xts->out_bits % 16;                //we can also receive a 16-bit word in which case
389
            if (shift)                                                  //the MSB starts in the least significant 16 bit word
390
                shift = 16 - shift;                             //and it shifts the same way for 32 bit if
391
                                                                                                //out_bits > 16 and ( shift = 32 - out_bits % 32 )
392
 
393
            debug("out_done %d shift %d\n", out_done, shift);
394
            for (i= aligned_bytes*8; i <xts->out_bits; i++)
395
            {
396
                bit_num = i + shift;
397
                bit_val = xts->buf[bit_num/8] & (1<<(bit_num%8));
398
                if(!(out_done % 32))
399
                        tdostream[out_done/32] = 0;
400
                if (bit_val)
401
                        tdostream[out_done/32] |= (1<<(out_done%32));
402
                out_done++;
403
            }
404
        }
405
    }
406
 
407
  xts->in_bits = 0;
408
  xts->out_bits = 0;
409
 
410
  return r;
411
}
412
 
413
 
414
 
415
int cable_xpcusb_out(uint8_t value)
416
{
417
        int             rv;                  // to catch return values of functions
418
        //usb_dev_handle *h_device;            // handle on the ubs device
419
        uint8_t out;
420
 
421
        // open the device, if necessary
422
        if(h_device == NULL) {
423
                rv = cable_xpcusb_open_cable();
424
                if(rv != APP_ERR_NONE) return rv;
425
        }
426
 
427
        if ( cpld_ctrl )
428
        {
429
                rv = cable_xpcusb_fx2_init();
430
                if ( rv != APP_ERR_NONE) return rv;
431
        }
432
 
433
        // send the buffer
434
        // Translate to USB blaster protocol
435
        out = 0;
436
        if(value & TCLK_BIT)
437
                out |= XPCUSB_CMD_TCK;
438
        if(value & TDI_BIT)
439
                out |= XPCUSB_CMD_TDI;
440
        if(value & TMS_BIT)
441
                out |= XPCUSB_CMD_TMS;
442
 
443
        out |= XPCUSB_CMD_PROG;  // Set output PROG (always necessary)
444
 
445
        rv = usb_control_msg(h_device, 0x40, 0xB0, 0x0030, out, NULL, 0, USB_TIMEOUT);
446
        if (rv < 0){
447
                fprintf(stderr, "\nFailed to send a write control message (rv = %d):\n%s\n", rv, usb_strerror());
448
                cable_xpcusb_close_cable();
449
                return APP_ERR_USB;
450
        }
451
 
452
        return APP_ERR_NONE;
453
}
454
 
455
int cable_xpcusb_inout(uint8_t value, uint8_t *inval)
456
{
457
        int rv;                  // to catch return values of functions
458
        //usb_dev_handle *h_device;            // handle on the usb device
459
        char ret = 0;
460
        uint8_t out;
461
 
462
        // open the device, if necessary
463
        if(h_device == NULL) {
464
                rv = cable_xpcusb_open_cable();
465
                if(rv != APP_ERR_NONE) return rv;
466
        }
467
 
468
        if ( cpld_ctrl )
469
        {
470
                rv = cable_xpcusb_fx2_init();
471
                if ( rv != APP_ERR_NONE) return rv;
472
        }
473
 
474
        // Translate to USB blaster protocol
475
        out = 0;
476
        if(value & TCLK_BIT)
477
                out |= XPCUSB_CMD_TCK;
478
        if(value & TDI_BIT)
479
                out |= XPCUSB_CMD_TDI;
480
        if(value & TMS_BIT)
481
                out |= XPCUSB_CMD_TMS;
482
 
483
        out |= XPCUSB_CMD_PROG;  // Set output PROG (always necessary)
484
 
485
        // Send the output
486
        rv = usb_control_msg(h_device, 0x40, 0xB0, 0x0030, out, NULL, 0, USB_TIMEOUT);
487
        if (rv < 0){
488
                fprintf(stderr, "\nFailed to send a write control message (rv = %x):\n%s\n", rv, usb_strerror());
489
                cable_xpcusb_close_cable();
490
                return APP_ERR_USB;
491
        }
492
 
493
        // receive the response
494
        rv = usb_control_msg(h_device, 0xC0, 0xB0, 0x0038, 0, (char*)&ret, 1, USB_TIMEOUT);
495
        if (rv < 0){
496
                fprintf(stderr, "\nFailed to execute a read control message:\n%s\n", usb_strerror());
497
                cable_xpcusb_close_cable();
498
                return APP_ERR_USB;
499
        }
500
 
501
        if(ret & XPCUSB_CMD_TDO)
502
                *inval = 1;
503
        else
504
                *inval = 0;
505
 
506
        return APP_ERR_NONE;
507
}
508
 
509
// Xilinx couldn't be like everyone else.  Oh, no.
510
// For some reason, "set data/drop TCK" then "read data/raise TCK" won't work.
511
// So we have our very own bit read/write function.  @whee.
512
int cable_xpcusb_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
513
        uint8_t data = TRST_BIT;  //  TRST is active low, don't clear unless /set/ in 'packet'
514
        int err = APP_ERR_NONE;
515
 
516
        /* Write data, drop clock */
517
        if(packet_out & TDO) data |= TDI_BIT;
518
        if(packet_out & TMS) data |= TMS_BIT;
519
        if(packet_out & TRST) data &= ~TRST_BIT;
520
 
521
        err |= cable_xpcusb_inout(data, bit_in);  // read in bit, set data, drop clock
522
        err |= cable_xpcusb_out(data|TCLK_BIT);  // clk hi
523
 
524
        return err;
525
}
526
 
527
 
528
int cable_xpcusb_cpld_write_bit(uint8_t value)
529
{
530
        uint32_t out;
531
        out = (value & TDO) ? 1:0;
532
        return cable_xpcusb_write_stream(&out, 1, value & TMS);
533
}
534
 
535
int cable_xpcusb_cpld_readwrite_bit(uint8_t value, uint8_t *inval)
536
{
537
        int r;
538
        uint32_t out;
539
        uint32_t in;
540
        out = (value & TDO) ? 1:0;
541
        r = cable_xpcusb_readwrite_stream(&out, &in, 1, value & TMS);
542
        if ( r < 0 )
543
                return r;
544
        *inval = in & 0x1;
545
        return APP_ERR_NONE;
546
}
547
 
548
int cable_xpcusb_write_stream(uint32_t *outstream, int len_bits, int set_last_bit)
549
{
550
    return cable_xpcusb_readwrite_stream(outstream, NULL, len_bits, set_last_bit);
551
}
552
 
553
/*
554
 *   Care has to be taken that the number of bits to be transferred
555
 *   is NOT a multiple of 4. The CPLD doesn't seem to handle that well.
556
 */
557
int cable_xpcusb_readwrite_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit)
558
{
559
        int i;
560
        int ret = APP_ERR_NONE;
561
        uint32_t bitval;
562
        xpc_ext_transfer_state_t xts;
563
        uint8_t tms, tdi, sample_tdo, toggle_clock;
564
 
565
        // open the device, if necessary
566
        if(h_device == NULL) {
567
                ret = cable_xpcusb_open_cable();
568
                if(ret != APP_ERR_NONE) return ret;
569
        }
570
 
571
        if ( !cpld_ctrl )
572
        {
573
                ret = cable_xpcusb_cpld_init();
574
                if ( ret != APP_ERR_NONE) return ret;
575
        }
576
 
577
        debug("cable_xpcusb_write_stream(), len_bits = 0x%X, set_last_bit = %i\n", len_bits, set_last_bit);
578
 
579
        xts.in_bits = 0;
580
        xts.out_bits = 0;
581
 
582
        tms = 0;
583
        tdi = 0;
584
        toggle_clock = 1;
585
        sample_tdo = 1;         //automatically ignored if xts.out == NULL
586
 
587
        for (i = 0; i < len_bits && ret == APP_ERR_NONE; i++)
588
        {
589
                if ( outstream )
590
                        bitval = outstream[i/32] & (1<<(i%32));
591
                else
592
                        bitval = 0;
593
 
594
                tms = ( i == len_bits - 1 ) ? set_last_bit:0;
595
                tdi = bitval ? 1:0;
596
 
597
                debug("Adding bit for transfer, bitval = %i, set_tms = %i\n", tdi, tms);
598
                xpcusb_add_bit_for_ext_transfer(&xts, toggle_clock, tms, tdi, sample_tdo);
599
 
600
                if ( xts.in_bits == (2*CPLD_MAX_BYTES - 1) )
601
                {
602
                        debug("Reached %i bits, doing transfer\n", (2*CPLD_MAX_BYTES - 1));
603
                        ret = xpcusb_do_ext_transfer(&xts, instream);
604
                }
605
        }
606
 
607
        if((xts.in_bits > 0) && (ret == APP_ERR_NONE))
608
        {
609
                /* CPLD doesn't like multiples of 4; add one dummy bit */
610
                if((xts.in_bits & 3) == 0)
611
                {
612
                        debug("Adding dummy bit\n");
613
                        xpcusb_add_bit_for_ext_transfer(&xts, 0, 0, 0, 0);
614
                }
615
                debug("Doing final transfer of sequence\n");
616
                ret = xpcusb_do_ext_transfer(&xts, instream);
617
        }
618
 
619
        if(ret != APP_ERR_NONE)
620
        {
621
                fprintf(stderr, "Cable will block until next power reset\n");
622
                fprintf(stderr, "Closing connection to cable.\n");
623
                cable_xpcusb_close_cable();
624
                fprintf(stderr, "Aborting adv_jtag_bridge.\n");
625
                exit(1);
626
        }
627
 
628
        return ret;
629
}
630
 
631
 
632
static int cable_xpcusb_open_cable(void)
633
{
634
        int if_not_claimed = 1;
635
        timeout_timer timer;
636
 
637
        fprintf(stderr, "XPC USB driver opening cable\n");
638
        // open the device (assumes 'device' has already been set/populated)
639
        h_device = usb_open(device);
640
        if (h_device == NULL){
641
                fprintf(stderr, "XPC USB driver failed to open device\n");
642
                return APP_ERR_USB;
643
        }
644
 
645
        // set the configuration
646
        if (usb_set_configuration(h_device, device->config->bConfigurationValue))
647
        {
648
                usb_close(h_device);
649
                h_device = NULL;
650
                fprintf(stderr, "XPC USB driver failed to set configuration\n");
651
                return APP_ERR_USB;
652
        }
653
 
654
        if ( create_timer(&timer) )
655
        {
656
              fprintf(stderr, "Failed to create timer\n");
657
              // fall back to infinite wait
658
              while (usb_claim_interface(h_device, device->config->interface->altsetting->bInterfaceNumber));
659
        }
660
        else
661
        {
662
 
663
              while (if_not_claimed && !timedout(&timer) )
664
                  if_not_claimed = usb_claim_interface(h_device, device->config->interface->altsetting->bInterfaceNumber);
665
 
666
            if ( timedout(&timer) )
667
            {
668
                fprintf(stderr, "Claiming interface timed out...\n");
669
                return APP_ERR_USB;
670
            }
671
        }
672
 
673
        return APP_ERR_NONE;
674
}
675
 
676
 
677
static void cable_xpcusb_close_cable(void)
678
{
679
  fprintf(stderr, "XPC USB driver closing cable\n");
680
  if(h_device != NULL) {
681
    // release the interface cleanly
682
    if (usb_release_interface(h_device, device->config->interface->altsetting->bInterfaceNumber)){
683
      fprintf(stderr, "Warning: failed to release usb interface\n");
684
    }
685
 
686
    // close the device
687
    usb_close(h_device);
688
    h_device = NULL;
689
  }
690
 
691
  return;
692
}
693
 
694
int cable_xpcusb_opt(int c, char *str)
695
{
696
    fprintf(stderr, "Unknown parameter '%c'\n", c);
697
    return APP_ERR_BAD_PARAM;
698
}
699
 
700
jtag_cable_t *cable_xpcusb_get_driver(void)
701
{
702
  return &dlc9_cable_driver;
703
}
704
 
705
static int xpcusb_enumerate_bus(void)
706
{
707
  int             flag;  // for USB bus scanning stop condition
708
  struct usb_bus *bus;   // pointer on the USB bus
709
 
710
  // board detection
711
  usb_init();
712
  usb_find_busses();
713
  usb_find_devices();
714
 
715
  flag = 0;
716
 
717
  for (bus = usb_get_busses(); bus; bus = bus->next)
718
  {
719
    for (device = bus->devices; device; device = device->next)
720
    {
721
      if (device->descriptor.idVendor  == XPCUSB_VID &&
722
          device->descriptor.idProduct == XPCUSB_PID)
723
      {
724
              flag = 1;
725
              fprintf(stderr, "Found Xilinx Platform Cable USB (DLC9)\n");
726
              return APP_ERR_NONE;
727
      }
728
    }
729
    if (flag)
730
      break;
731
  }
732
 
733
  fprintf(stderr, "Failed to find Xilinx Platform Cable USB\n");
734
  return APP_ERR_CABLENOTFOUND;
735
}
736
 
737
 
738
 
739
static int xpcu_common_init( struct usb_dev_handle *xpcu )
740
{
741
    int r;
742
 
743
    r = xpcu_request_28(xpcu, 0x11);
744
    if (r>=0)
745
        r = xpcu_write_gpio(xpcu, 8);
746
 
747
    if (r<0)
748
        cable_xpcusb_close_cable();
749
 
750
    return r;
751
}
752
 
753
 
754
static int cable_xpcusb_fx2_init()
755
{
756
        int r;
757
 
758
        r = xpcu_select_gpio(h_device, 0);
759
        if ( r < 0 ) fprintf(stderr, "Error setting FX2 mode\n");
760
        cpld_ctrl = 0;
761
 
762
        return APP_ERR_NONE;
763
}
764
 
765
static int cable_xpcusb_cpld_init()
766
{
767
        int r;
768
        uint8_t zero[2] = {0,0};
769
 
770
        r = xpcu_request_28(h_device, 0x11);
771
        if (r >= 0) r = xpcu_output_enable(h_device, 1);
772
        else fprintf(stderr, "First xpcu_request_28 failed!\n");
773
        if (r >= 0) r = xpcu_shift(h_device, 0xA6, 2, 2, zero, 0, NULL);
774
        else fprintf(stderr, "xpcu_output_enable failed!\n");
775
        if (r >= 0) r = xpcu_request_28(h_device, 0x12);
776
        else fprintf(stderr, "xpcu_shift for init failed!\n");
777
        if(r < 0) fprintf(stderr, "second xpcu_request_28 failed!\n");
778
 
779
        cpld_ctrl = 1;
780
 
781
        return APP_ERR_NONE;
782
}
783
 
784
 
785
int cable_xpcusb_init()
786
{
787
        int r = APP_ERR_NONE;
788
    uint16_t buf;
789
        // Process to reset the XPC USB (DLC9)
790
        if(r |= xpcusb_enumerate_bus()) {
791
                return r;
792
        }
793
 
794
        //usb_dev_handle *
795
        h_device = usb_open(device);
796
 
797
        if(h_device == NULL)
798
        {
799
                fprintf(stderr, "Init failed to open USB device for reset\n");
800
                return APP_ERR_USB;
801
        }
802
 
803
        if(usb_reset(h_device) != APP_ERR_NONE)
804
                fprintf(stderr, "Failed to reset XPC-USB\n");
805
 
806
        usb_close(h_device);
807
        h_device = NULL;
808
 
809
        // Wait for reset!!!
810
        sleep(1);
811
 
812
        // Do device initialization
813
        if(r |= xpcusb_enumerate_bus())
814
                return r;
815
 
816
        r = cable_xpcusb_open_cable();
817
        if ( r )
818
        {
819
                fprintf(stderr, "Open cable failed\n");
820
                return APP_ERR_USB;
821
        }
822
 
823
        r = xpcu_common_init(h_device);
824
 
825
    /* Read firmware version (constant embedded in firmware) */
826
 
827
    if (r>=0) r = xpcu_read_firmware_version(h_device, &buf);
828
    if (r>=0)
829
    {
830
        printf("firmware version = 0x%04X (%u)\n", buf, buf);
831
    }
832
 
833
    /* Read CPLD version (via GPIF) */
834
 
835
    if (r>=0) r = xpcu_read_cpld_version(h_device, &buf);
836
    if (r>=0)
837
    {
838
        printf("cable CPLD version = 0x%04X (%u)\n", buf, buf);
839
        if(buf == 0)
840
        {
841
            printf("Warning: version '0' can't be correct. Please try resetting the cable\n");
842
            r = -1;
843
        }
844
    }
845
 
846
    if (r<0)
847
        cable_xpcusb_close_cable();
848
 
849
    r = cable_xpcusb_cpld_init();
850
    if (r<0)
851
        cable_xpcusb_close_cable();
852
 
853
        return r;
854
}
855
 
856
 

powered by: WebSVN 2.1.0

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