OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [jtag/] [jtag_libusb/] [jtag-virtual.c] - Blame information for rev 38

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 alirezamon
/* Copyright 2012 Brian Swetland <swetland@frotz.net>
2
 *
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 *     http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15
 
16
#include <stdio.h>
17
#include <stdlib.h>
18
 
19
#include "jtag.h"
20
 
21
int jtag_dr_8x4(unsigned *out) {
22
        unsigned bits = 0;
23
        unsigned tmp;
24
        int n, r;
25
 
26
        for (n = 0; n < 8; n++) {
27
                if ((r = jtag_dr(4, 0, &tmp)) < 0) return r;
28
                bits |= (tmp <<= (n * 4));
29
        }
30
        *out = bits;
31
        return 0;
32
}
33
 
34
/* number of bits needed given a max value 1-255 */
35
unsigned needbits(unsigned max) {
36
        if (max > 127) return 8;
37
        if (max > 63) return 7;
38
        if (max > 31) return 6;
39
        if (max > 15) return 5;
40
        if (max > 7) return 4;
41
        if (max > 3) return 3;
42
        if (max > 1) return 2;
43
        return 1;
44
}
45
 
46
static unsigned ir_width = 10;
47
 
48
static unsigned hub_version = 0;
49
static unsigned hub_nodecount = 0;
50
static unsigned hub_mfg = 0;
51
 
52
static unsigned vir_width = 0;
53
static unsigned vir_width_addr = 0;
54
static unsigned vir_width_ir = 0;
55
static unsigned vir_addr = 0;
56
 
57
 
58
int jtag_vir(unsigned vir) {
59
        int r;
60
        if ((r = jtag_ir(ir_width, 14)) < 0) return r;
61
        if ((r = jtag_dr(vir_width, vir_addr | vir, 0)) < 0) return r;
62
        return 0;
63
}
64
 
65
int jtag_vdr(unsigned sz, unsigned bits, unsigned *out) {
66
        int r;
67
        if ((r = jtag_ir(ir_width, 12)) < 0) return r;
68
        if ((r = jtag_dr(sz, bits, out)) < 0) return r;
69
        return 0;
70
}
71
 
72
int jtag_vdr_long(unsigned sz, unsigned * bits, unsigned *out, int words) {
73
        int r;
74
        if ((r = jtag_ir(ir_width, 12)) < 0) return r;
75
        if ((r = jtag_dr_long(sz, bits, out, words)) < 0) return r;
76
        return 0;
77
}
78
 
79
int jtag_open_virtual_device(unsigned iid,unsigned vid, unsigned pid) {
80
        unsigned n, bits;
81
        int r;
82
 
83
        if ((r = jtag_open(vid,pid)) < 0) return r;
84
 
85
        if ((r = jtag_reset()) < 0) return r;
86
 
87
        /* select empty node_addr + node_vir -- all zeros */
88
        if ((r = jtag_ir(ir_width, 14)) < 0) return r;
89
        if ((r = jtag_dr(32, 0, 0)) < 0) return r;
90
 
91
        /* select vdr - this will be the hub info (addr=0,vir=0) */
92
        if ((r = jtag_ir(ir_width, 12)) < 0) return r;
93
 
94
        /* read hub info */
95
        if ((r = jtag_dr_8x4(&bits)) < 0) return r;
96
        hub_version = (bits >> 27) & 0x1F;
97
        hub_nodecount = (bits >> 19) & 0xFF;
98
        hub_mfg = (bits >> 8) & 0x7FF;
99
 
100
        if (hub_mfg != 0x06e) {
101
                fprintf(stderr,"hub_version=%x, hub_nodecount=%x,       hub_mfg=%x \n",hub_version,     hub_nodecount,  hub_mfg);
102
 
103
                fprintf(stderr,"HUB:    Cannot Find Virtual JTAG HUB\n");
104
                return -1;
105
        }
106
 
107
        /* altera docs claim this field is the sum of M bits (VIR field) and
108
         * N bits (ADDR field), but empirical evidence suggests it is actually
109
         * just the width of the ADDR field and the docs are wrong...
110
         */
111
        vir_width_ir = bits & 0xFF;
112
        vir_width_addr = needbits(hub_nodecount);
113
        vir_width = vir_width_ir + vir_width_addr;
114
 
115
        fprintf(stderr,"HUB:    Mfg=0x%03x, Ver=0x%02x, Nodes=%d, VIR=%d+%d bits\n",
116
                hub_mfg, hub_version, hub_nodecount, vir_width_addr, vir_width_ir);
117
 
118
        for (n = 0; n < hub_nodecount; n++) {
119
                unsigned node_ver, node_id, node_mfg, node_iid;
120
                if ((r = jtag_dr_8x4(&bits)) < 0) return r;
121
                node_ver = (bits >> 27) & 0x1F;
122
                node_id = (bits >> 19) & 0xFF;
123
                node_mfg = (bits >> 8) & 0x7FF;
124
                node_iid = bits & 0xFF;
125
 
126
                fprintf(stderr,"NODE:   Mfg=0x%03x, Ver=0x%02x, ID=0x%02x, IID=0x%02x\n",
127
                        node_mfg, node_ver, node_id, node_iid);
128
 
129
                if ((node_id == 0x08) && (node_iid) == iid) {
130
                        vir_addr = (n + 1) << vir_width_ir;
131
                }
132
        }
133
 
134
        if ((vir_addr == 0) && (iid < 256)) {
135
                fprintf(stderr,"ERROR: IID 0x%02x not found\n", iid);
136
                return -1;
137
        }
138
        return 0;
139
}
140
 
141
 

powered by: WebSVN 2.1.0

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