1 /***************************************************************************
2 * Copyright (C) 2013 by Franck Jullien *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 ***************************************************************************/
23 #include <jtag/jtag.h>
25 /* Contains constants relevant to the Altera Virtual JTAG
26 * device, which are not included in the BSDL.
27 * As of this writing, these are constant across every
28 * device which supports virtual JTAG.
31 /* These are commands for the FPGA's IR. */
32 #define ALTERA_CYCLONE_CMD_USER1 0x0E
33 #define ALTERA_CYCLONE_CMD_USER0 0x0C
35 /* These defines are for the virtual IR (not the FPGA's)
36 * The virtual TAP was defined in hardware to match the OpenCores native
37 * TAP in both IR size and DEBUG command.
39 #define ALT_VJTAG_IR_SIZE 4
40 #define ALT_VJTAG_CMD_DEBUG 0x8
43 #define JTAG_TO_AVALON_NODE_ID 0x84
44 #define VJTAG_NODE_ID 0x08
45 #define SIGNAL_TAP_NODE_ID 0x00
46 #define SERIAL_FLASH_LOADER_NODE_ID 0x04
48 #define VER(x) ((x >> 27) & 0x1f)
49 #define NB_NODES(x) ((x >> 19) & 0xff)
50 #define ID(x) ((x >> 19) & 0xff)
51 #define MANUF(x) ((x >> 8) & 0x7ff)
52 #define M_WIDTH(x) ((x >> 0) & 0xff)
53 #define INST_ID(x) ((x >> 0) & 0xff)
55 /* tap instructions - Mohor JTAG TAP */
56 #define OR1K_TAP_INST_IDCODE 0x2
57 #define OR1K_TAP_INST_DEBUG 0x8
59 static const char *id_to_string(unsigned char id)
63 return "Virtual JTAG";
64 case JTAG_TO_AVALON_NODE_ID:
65 return "JTAG to avalon bridge";
66 case SIGNAL_TAP_NODE_ID:
68 case SERIAL_FLASH_LOADER_NODE_ID:
69 return "Serial Flash Loader";
74 static unsigned char guess_addr_width(unsigned char number_of_nodes)
76 unsigned char width = 0;
78 while (number_of_nodes) {
79 number_of_nodes >>= 1;
86 static int or1k_tap_vjtag_init(struct or1k_jtag *jtag_info)
88 LOG_DEBUG("Initialising Altera Virtual JTAG TAP");
90 /* Put TAP into state where it can talk to the debug interface
91 * by shifting in correct value to IR.
94 /* Ensure TAP is reset - maybe not necessary*/
97 /* You can use a custom JTAG controller to discover transactions
98 * necessary to enumerate all Virtual JTAG megafunction instances
99 * from your design atruntime. All SLD nodes and the virtual JTAG
100 * registers that they contain are targeted by two Instruction Register
101 * values, USER0 and USER1.
103 * The USER1 instruction targets the virtual IR of either the sld_hub
104 * or a SLD node. That is,when the USER1 instruction is issued to
105 * the device, the subsequent DR scans target a specific virtual
106 * IR chain based on an address field contained within the DR scan.
107 * The table below shows how the virtual IR, the DR target of the
108 * USER1 instruction is interpreted.
110 * The VIR_VALUE in the table below is the virtual IR value for the
111 * target SLD node. The width of this field is m bits in length,
112 * where m is the length of the largest VIR for all of the SLD nodes
113 * in the design. All SLD nodes with VIR lengths of fewer than m
114 * bits must pad VIR_VALUE with zeros up to a length of m.
116 * -------------------------------+-------------------------------
117 * m + n - 1 m | m -1 0
118 * -------------------------------+-------------------------------
119 * ADDR [(n – 1)..0] | VIR_VALUE [(m – 1)..0]
120 * -------------------------------+-------------------------------
122 * The ADDR bits act as address values to signal the active SLD node
123 * that the virtual IR shift targets. ADDR is n bits in length, where
124 * n bits must be long enough to encode all SLD nodes within the design,
127 * n = CEIL(log2(Number of SLD_nodes +1))
129 * The SLD hub is always 0 in the address map.
131 * Discovery and enumeration of the SLD instances within a design
132 * requires interrogation of the sld_hub to determine the dimensions
133 * of the USER1 DR (m and n) and associating each SLD instance, specifically
134 * the Virtual JTAG megafunction instances, with an address value
135 * contained within the ADDR bits of the USER1 DR.
137 * The SLD hub contains the HUB IP Configuration Register and SLD_NODE_INFO
138 * register for each SLD node in the design. The HUB IP configuration register provides
139 * information needed to determine the dimensions of the USER1 DR chain. The
140 * SLD_NODE_INFO register is used to determine the address mapping for Virtual
141 * JTAG instance in your design. This register set is shifted out by issuing the
142 * HUB_INFO instruction. Both the ADDR bits for the SLD hub and the HUB_INFO
143 * instruction is 0 × 0.
144 * Because m and n are unknown at this point, the DR register
145 * (ADDR bits + VIR_VALUE) must be filled with zeros. Shifting a sequence of 64 zeroes
146 * into the USER1 DR is sufficient to cover the most conservative case for m and n.
150 struct scan_field field;
151 struct jtag_tap *tap = jtag_info->tap;
154 buf_set_u32(t, 0, tap->ir_length, ALTERA_CYCLONE_CMD_USER1);
155 field.num_bits = tap->ir_length;
157 field.in_value = NULL;
158 jtag_add_ir_scan(tap, &field, TAP_IDLE);
160 /* Select the SLD Hub */
162 field.out_value = NULL;
163 field.in_value = NULL;
164 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
166 /* HUB IP Configuration Register
168 * When the USER1 and HUB_INFO instruction sequence is issued, the
169 * USER0 instruction must be applied to enable the target register
170 * of the HUB_INFO instruction. The HUB IP configuration register
171 * is shifted out using eight four-bit nibble scans of the DR register.
172 * Each four-bit scan must pass through the UPDATE_DR state before
173 * the next four-bit scan. The 8 scans are assembled into a 32-bit
174 * value with the definitions shown in the table below.
176 * --------------------------------------------------------------------------------
177 * NIBBLE7 | NIBBLE6 | NIBBLE5 | NIBBLE4 | NIBBLE3 | NIBBLE2 | NIBBLE1 | NIBBLE0
178 * ----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-----
179 * | | | | | | | | | | | | | | |
180 * ----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+-----
181 * HUB IP version| N | ALTERA_MFG_ID (0x06E) | SUM (m, n)
182 * --------------+-------------------+------------------------+--------------------
186 buf_set_u32(t, 0, tap->ir_length, ALTERA_CYCLONE_CMD_USER0);
187 field.num_bits = tap->ir_length;
189 field.in_value = NULL;
190 jtag_add_ir_scan(tap, &field, TAP_IDLE);
192 int retval = jtag_execute_queue();
193 if (retval != ERROR_OK)
197 uint32_t hub_info = 0;
199 for (int i = 0; i < 8; i++) {
201 field.out_value = NULL;
202 field.in_value = &nibble;
203 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
204 retval = jtag_execute_queue();
205 if (retval != ERROR_OK)
207 hub_info = ((hub_info >> 4) | ((nibble & 0xf) << 28));
210 int nb_nodes = NB_NODES(hub_info);
211 int m_width = M_WIDTH(hub_info);
213 LOG_DEBUG("SLD HUB Configuration register");
214 LOG_DEBUG("------------------------------");
215 LOG_DEBUG("m_width = %d", m_width);
216 LOG_DEBUG("manufacturer_id = 0x%02" PRIx32, MANUF(hub_info));
217 LOG_DEBUG("nb_of_node = %d", nb_nodes);
218 LOG_DEBUG("version = %" PRId32, VER(hub_info));
219 LOG_DEBUG("VIR length = %d", guess_addr_width(nb_nodes) + m_width);
221 /* Because the number of SLD nodes is now known, the Nodes on the hub can be
222 * enumerated by repeating the 8 four-bit nibble scans, once for each Node,
223 * to yield the SLD_NODE_INFO register of each Node. The DR nibble shifts
224 * are a continuation of the HUB_INFO DR shift used to shift out the Hub IP
225 * Configuration register.
227 * The order of the Nodes as they are shifted out determines the ADDR
228 * values for the Nodes, beginning with, for the first Node SLD_NODE_INFO
229 * shifted out, up to and including, for the last node on the hub. The
230 * tables below show the SLD_NODE_INFO register and a their functional descriptions.
232 * --------------+-----------+---------------+----------------
233 * 31 27 | 26 19 | 18 8 | 7 0
234 * --------------+-----------+---------------+----------------
235 * Node Version | NODE ID | NODE MFG_ID | NODE INST ID
239 int vjtag_node_address = -1;
241 uint32_t node_info = 0;
242 for (node_index = 0; node_index < nb_nodes; node_index++) {
244 for (int i = 0; i < 8; i++) {
246 field.out_value = NULL;
247 field.in_value = &nibble;
248 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
249 retval = jtag_execute_queue();
250 if (retval != ERROR_OK)
252 node_info = ((node_info >> 4) | ((nibble & 0xf) << 28));
255 LOG_DEBUG("Node info register");
256 LOG_DEBUG("--------------------");
257 LOG_DEBUG("instance_id = %" PRId32, ID(node_info));
258 LOG_DEBUG("manufacturer_id = 0x%02" PRIx32, MANUF(node_info));
259 LOG_DEBUG("node_id = %" PRId32 " (%s)", ID(node_info),
260 id_to_string(ID(node_info)));
261 LOG_DEBUG("version = %" PRId32, VER(node_info));
263 if (ID(node_info) == VJTAG_NODE_ID)
264 vjtag_node_address = node_index + 1;
267 if (vjtag_node_address < 0) {
268 LOG_ERROR("No VJTAG TAP instance found !");
273 buf_set_u32(t, 0, tap->ir_length, ALTERA_CYCLONE_CMD_USER1);
274 field.num_bits = tap->ir_length;
276 field.in_value = NULL;
277 jtag_add_ir_scan(tap, &field, TAP_IDLE);
279 /* Send the DEBUG command to the VJTAG IR */
280 int dr_length = guess_addr_width(nb_nodes) + m_width;
281 buf_set_u32(t, 0, dr_length, (vjtag_node_address << m_width) | ALT_VJTAG_CMD_DEBUG);
282 field.num_bits = dr_length;
284 field.in_value = NULL;
285 jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
287 /* Select the VJTAG DR */
288 buf_set_u32(t, 0, tap->ir_length, ALTERA_CYCLONE_CMD_USER0);
289 field.num_bits = tap->ir_length;
291 field.in_value = NULL;
292 jtag_add_ir_scan(tap, &field, TAP_IDLE);
294 return jtag_execute_queue();
297 static struct or1k_tap_ip vjtag_tap = {
299 .init = or1k_tap_vjtag_init,
302 int or1k_tap_vjtag_register(void)
304 list_add_tail(&vjtag_tap.list, &tap_list);