-
Notifications
You must be signed in to change notification settings - Fork 5
/
chain_commands.c
491 lines (393 loc) · 16.6 KB
/
chain_commands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* chain_commands.c -- JTAG protocol bridge between GDB and Advanced debug module.
Copyright(C) 2008 - 2010 Nathan Yawn, [email protected]
based on code from jp2 by Marko Mlinar, [email protected]
This file contains functions which perform mid-level transactions
on a JTAG, such as setting a value in the TAP IR
or doing a burst write on the JTAG chain.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h> // for malloc()
#include <unistd.h> // for usleep()
//#include <pthread.h> // for mutexes
#include "chain_commands.h" // For the return error codes
#include "altera_virtual_jtag.h" // hardware-specifg defines for the Altera Virtual JTAG interface
#include "cable_common.h" // low-level JTAG IO routines
#include "adv_dbg_commands.h" // for the kludge in tap_reset()
#include "errcodes.h"
#define debug(...) if (getenv("ADV_DEBUG1")) fprintf(stderr, __VA_ARGS__ )
// How many tries before an abort
#define NUM_SOFT_RETRIES 0
// for the klugde in tap_reset()
extern int current_reg_idx[DBG_MAX_MODULES];
/* Currently selected scan chain in the debug unit - just to prevent unnecessary
transfers. */
int current_chain = -1;
int desired_chain = -1;
// wait for 100ms
#define JTAG_RETRY_WAIT() usleep(100000);
// Retry data
int soft_retry_no = 0;
//static int hard_retry_no = 0;
// Configuration data
int global_IR_size = 0;
int global_IR_prefix_bits = 0;
int global_IR_postfix_bits = 0;
int global_DR_prefix_bits = 0;
int global_DR_postfix_bits = 0;
unsigned int global_jtag_cmd_debug = 0; // Value to be shifted into the TAP IR to select the debug unit (unused for virtual jtag)
unsigned char global_altera_virtual_jtag = 0; // Set true to use virtual jtag mode
unsigned int vjtag_cmd_vir = ALTERA_CYCLONE_CMD_VIR; // virtual IR-shift command for altera devices, may be configured on command line
unsigned int vjtag_cmd_vdr = ALTERA_CYCLONE_CMD_VDR; // virtual DR-shift, ditto
unsigned char global_xilinx_bscan = 0; // Set true if the hardware uses a Xilinx BSCAN_* device.
///////////////////////////////////////////////////////////////////////
// Configuration
void config_set_IR_size(int size) {
global_IR_size = size;
}
void config_set_IR_prefix_bits(int bits) {
global_IR_prefix_bits = bits;
}
void config_set_IR_postfix_bits(int bits) {
global_IR_postfix_bits = bits;
}
void config_set_DR_prefix_bits(int bits) {
global_DR_prefix_bits = bits;
}
void config_set_DR_postfix_bits(int bits) {
global_DR_postfix_bits = bits;
}
void config_set_debug_cmd(unsigned int cmd) {
global_jtag_cmd_debug = cmd;
}
void config_set_alt_vjtag(unsigned char enable) {
global_altera_virtual_jtag = (enable) ? 1:0;
}
// At present, all devices which support virtual JTAG use the same VIR/VDR
// commands. But, if they ever change, these can be changed on the command line.
void config_set_vjtag_cmd_vir(unsigned int cmd) {
vjtag_cmd_vir = cmd;
}
void config_set_vjtag_cmd_vdr(unsigned int cmd) {
vjtag_cmd_vdr = cmd;
}
void config_set_xilinx_bscan(unsigned char enable) {
global_xilinx_bscan = (enable) ? 1:0;
}
//////////////////////////////////////////////////////////////////////
// Functions which operate on the JTAG TAP
/* Resets JTAG - Writes TRST=1, and TRST=0. Sends 8 TMS to put the TAP
* in test_logic_reset mode, for good measure.
*/
int tap_reset(void) {
int i;
int err = APP_ERR_NONE;
debug("\nreset(");
err |= jtag_write_bit(0);
JTAG_RETRY_WAIT();
/* In case we don't have TRST reset it manually */
for(i = 0; i < 8; i++) err |= jtag_write_bit(TMS);
err |= jtag_write_bit(TRST); // if TRST not supported, this puts us in test logic/reset
JTAG_RETRY_WAIT();
err |= jtag_write_bit(0); // run test / idle
debug(")\n");
// Reset data on current module/register selections
current_chain = -1;
// (this is only for the adv. debug i/f...bit of a kludge)
for(i = 0; i < DBG_MAX_MODULES; i++)
current_reg_idx[i] = -1;
return err;
}
// Set the IR with the DEBUG command, one way or the other
int tap_enable_debug_module(void)
{
uint32_t data;
int err = APP_ERR_NONE;
if(global_altera_virtual_jtag) {
/* Set for virtual IR shift */
err |= tap_set_ir(vjtag_cmd_vir); // This is the altera virtual IR scan command
err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
err |= jtag_write_bit(0); /* CAPTURE_DR */
err |= jtag_write_bit(0); /* SHIFT_DR */
/* Select debug scan chain in virtual IR */
data = (0x1<<ALT_VJTAG_IR_SIZE)|ALT_VJTAG_CMD_DEBUG;
err |= jtag_write_stream(&data, (ALT_VJTAG_IR_SIZE+1), 1); // EXIT1_DR
err |= jtag_write_bit(TMS); /* UPDATE_DR */
err |= jtag_write_bit(0); /* IDLE */
// This is a command to set an altera device to the "virtual DR shift" command
err |= tap_set_ir(vjtag_cmd_vdr);
}
else {
/* select debug scan chain and stay in it forever */
err |= tap_set_ir(global_jtag_cmd_debug);
}
return err;
}
/* Moves a value into the TAP instruction register (IR)
* Includes adjustment for scan chain IR length.
*/
uint32_t *ir_chain = NULL;
int tap_set_ir(int ir) {
int chain_size;
int chain_size_words;
int i;
int startoffset, startshift;
int err = APP_ERR_NONE;
// Adjust desired IR with prefix, postfix bits to set other devices in the chain to BYPASS
chain_size = global_IR_size + global_IR_prefix_bits + global_IR_postfix_bits;
chain_size_words = (chain_size/32)+1;
if(ir_chain == NULL) { // We have no way to know in advance how many bits there are in the combined IR register
ir_chain = (uint32_t *) malloc(chain_size_words * sizeof(uint32_t));
if(ir_chain == NULL)
return APP_ERR_MALLOC;
}
for(i = 0; i < chain_size_words; i++)
ir_chain[i] = 0xFFFFFFFF; // Set all other devices to BYPASS
// Copy the IR value into the output stream
startoffset = global_IR_postfix_bits/32;
startshift = (global_IR_postfix_bits - (startoffset*32));
ir_chain[startoffset] &= (ir << startshift);
ir_chain[startoffset] |= ~(0xFFFFFFFF << startshift); // Put the 1's back in the LSB positions
ir_chain[startoffset] |= (0xFFFFFFFF << (startshift + global_IR_size)); // Put 1's back in MSB positions, if any
if((startshift + global_IR_size) > 32) { // Deal with spill into the next word
ir_chain[startoffset+1] &= ir >> (32-startshift);
ir_chain[startoffset+1] |= (0xFFFFFFFF << (global_IR_size - (32-startshift))); // Put the 1's back in the MSB positions
}
// Do the actual JTAG transaction
debug("Set IR 0x%X\n", ir);
err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
err |= jtag_write_bit(TMS); /* SELECT_IR SCAN */
err |= jtag_write_bit(0); /* CAPTURE_IR */
err |= jtag_write_bit(0); /* SHIFT_IR */
/* write data, EXIT1_IR */
debug("Setting IR, size %i, IR_size = %i, pre_size = %i, post_size = %i, data 0x%X\n", chain_size, global_IR_size, global_IR_prefix_bits, global_IR_postfix_bits, ir);
err |= cable_write_stream(ir_chain, chain_size, 1); // Use cable_ call directly (not jtag_), so we don't add DR prefix bits
debug("Done setting IR\n");
err |= jtag_write_bit(TMS); /* UPDATE_IR */
err |= jtag_write_bit(0); /* IDLE */
current_chain = -1;
return err;
}
// This assumes we are in the IDLE state, and we want to be in the SHIFT_DR state.
int tap_set_shift_dr(void)
{
int err = APP_ERR_NONE;
err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
err |= jtag_write_bit(0); /* CAPTURE_DR */
err |= jtag_write_bit(0); /* SHIFT_DR */
return err;
}
// This transitions from EXIT1 to IDLE. It should be the last thing called
// in any debug unit transaction.
int tap_exit_to_idle(void)
{
int err = APP_ERR_NONE;
err |= jtag_write_bit(TMS); /* UPDATE_DR */
err |= jtag_write_bit(0); /* IDLE */
return err;
}
////////////////////////////////////////////////////////////////////
// Operations to read / write data over JTAG
/* Writes TCLK=0, TRST=1, TMS=bit1, TDI=bit0
and TCLK=1, TRST=1, TMS=bit1, TDI=bit0
*/
int jtag_write_bit(uint8_t packet) {
debug("Wbit(%i)\n", packet);
return cable_write_bit(packet);
}
int jtag_read_write_bit(uint8_t packet, uint8_t *in_bit) {
int retval = cable_read_write_bit(packet, in_bit);
debug("RWbit(%i,%i)", packet, *in_bit);
return retval;
}
// This automatically adjusts for the DR length (other devices on scan chain)
// when the set_TMS flag is true.
int jtag_write_stream(uint32_t *out_data, int length_bits, unsigned char set_TMS)
{
int i;
int err = APP_ERR_NONE;
if(!set_TMS)
err |= cable_write_stream(out_data, length_bits, 0);
else if(global_DR_prefix_bits == 0)
err |= cable_write_stream(out_data, length_bits, 1);
else {
err |= cable_write_stream(out_data, length_bits, 0);
// It could be faster to do a cable_write_stream for all the prefix bits (if >= 8 bits),
// but we'd need a data array of unknown (and theoretically unlimited)
// size to hold the 0 bits to write. TODO: alloc/realloc one.
for(i = 0; i < (global_DR_prefix_bits-1); i++)
err |= jtag_write_bit(0);
err |= jtag_write_bit(TMS);
}
return err;
}
// When set_TMS is true, this function insures the written data is in the desired position (past prefix bits)
// before sending TMS. When 'adjust' is true, this function insures that the data read in accounts for postfix
// bits (they are shifted through before the read starts).
int jtag_read_write_stream(uint32_t *out_data, uint32_t *in_data, int length_bits, unsigned char adjust, unsigned char set_TMS)
{
int i;
int err = APP_ERR_NONE;
if(adjust && (global_DR_postfix_bits > 0)) {
// It would be faster to do a cable_write_stream for all the postfix bits,
// but we'd need a data array of unknown (and theoretically unlimited)
// size to hold the '0' bits to write.
for(i = 0; i < global_DR_postfix_bits; i++)
err |= cable_write_bit(0);
}
// If there are both prefix and postfix bits, we may shift more bits than strictly necessary.
// If we shifted out the data while burning through the postfix bits, these shifts could be subtracted
// from the number of prefix shifts. However, that way leads to madness.
if(!set_TMS)
err |= cable_read_write_stream(out_data, in_data, length_bits, 0);
else if(global_DR_prefix_bits == 0)
err |= cable_read_write_stream(out_data, in_data, length_bits, 1);
else {
err |= cable_read_write_stream(out_data, in_data, length_bits, 0);
// It would be faster to do a cable_write_stream for all the prefix bits,
// but we'd need a data array of unknown (and theoretically unlimited)
// size to hold the '0' bits to write.
for(i = 0; i < (global_DR_prefix_bits-1); i++)
err |= jtag_write_bit(0);
err |= jtag_write_bit(TMS);
}
return err;
}
// This function attempts to determine the structure of the JTAG chain
// It can determine how many devices are present.
// If the devices support the IDCODE command, it will be read and stored.
// There is no way to automatically determine the length of the IR registers -
// this must be read from a BSDL file, if IDCODE is supported.
// When IDCODE is not supported, IR length of the target device must be entered on the command line.
#define ALLOC_SIZE 64
#define MAX_DEVICES 1024
int jtag_enumerate_chain(uint32_t **id_array, int *num_devices)
{
uint32_t invalid_code = 0x7f; // Shift this out, we know we're done when we get it back
const unsigned int done_code = 0x3f; // invalid_code is altered, we keep this for comparison (minus the start bit)
int devindex = 0; // which device we are currently trying to detect
uint32_t tempID;
uint32_t temp_manuf_code;
uint32_t temp_rest_code;
uint8_t start_bit = 0;
uint32_t *idcodes;
int reallocs = 0;
int err = APP_ERR_NONE;
// Malloc a reasonable number of entries, we'll expand if we must. Linked lists are overrated.
idcodes = (uint32_t *) malloc(ALLOC_SIZE*sizeof(uint32_t));
if(idcodes == NULL) {
printf("Failed to allocate memory for device ID codes!\n");
return APP_ERR_MALLOC;
}
// Put in SHIFT-DR mode
err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
err |= jtag_write_bit(0); /* CAPTURE_DR */
err |= jtag_write_bit(0); /* SHIFT_DR */
printf("Enumerating JTAG chain...\n");
// Putting a limit on the # of devices supported has the useful side effect
// of insuring we still exit in error cases (we never get the 0x7f manuf. id)
while(devindex < MAX_DEVICES) {
// get 1 bit. 0 = BYPASS, 1 = start of IDCODE
err |= jtag_read_write_bit(invalid_code&0x01, &start_bit);
invalid_code >>= 1;
if(start_bit == 0) {
if(devindex >= (ALLOC_SIZE << reallocs)) { // Enlarge the memory array if necessary, double the size each time
idcodes = (uint32_t *) realloc(idcodes, (ALLOC_SIZE << ++reallocs)*sizeof(uint32_t));
if(idcodes == NULL) {
printf("Failed to allocate memory for device ID codes during enumeration!\n");
return APP_ERR_MALLOC;
}
}
idcodes[devindex] = -1;
devindex++;
}
else {
// get 11 bit manufacturer code
err |= jtag_read_write_stream(&invalid_code, &temp_manuf_code, 11, 0, 0);
invalid_code >>= 11;
if(temp_manuf_code != done_code) {
// get 20 more bits, rest of ID
err |= jtag_read_write_stream(&invalid_code, &temp_rest_code, 20, 0, 0);
invalid_code >>= 20;
tempID = (temp_rest_code << 12) | (temp_manuf_code << 1) | 0x01;
if(devindex >= (ALLOC_SIZE << reallocs)) { // Enlarge the memory array if necessary, double the size each time
idcodes = (uint32_t *) realloc(idcodes, (ALLOC_SIZE << ++reallocs)*sizeof(unsigned long));
if(idcodes == NULL) {
printf("Failed to allocate memory for device ID codes during enumeration!\n");
return APP_ERR_MALLOC;
}
}
idcodes[devindex] = tempID;
devindex++;
} else {
break;
}
}
if(err) // Don't try to keep probing if we get a comm. error
return err;
}
if(devindex >= MAX_DEVICES)
printf("WARNING: maximum supported devices on JTAG chain (%i) exceeded.\n", MAX_DEVICES);
// Put in IDLE mode
err |= jtag_write_bit(TMS); /* EXIT1_DR */
err |= jtag_write_bit(TMS); /* UPDATE_DR */
err |= jtag_write_bit(0); /* IDLE */
*id_array = idcodes;
*num_devices = devindex;
return err;
}
int jtag_get_idcode(uint32_t cmd, uint32_t *idcode)
{
uint32_t data_out = 0;
int err = APP_ERR_NONE;
unsigned char saveconfig = global_altera_virtual_jtag;
global_altera_virtual_jtag = 0; // We want the actual IDCODE, not the virtual device IDCODE
err |= tap_set_ir(cmd);
err |= tap_set_shift_dr();
err |= jtag_read_write_stream(&data_out, idcode, 32, 1, 1); /* EXIT1_DR */
if(err)
printf("Error getting ID code!\n");
// Put in IDLE mode
err |= jtag_write_bit(TMS); /* UPDATE_DR */
err |= jtag_write_bit(0); /* IDLE */
global_altera_virtual_jtag = saveconfig;
return err;
}
/////////////////////////////////////////////////////////////////
// Helper functions
/* counts retries and returns zero if we should abort */
/* TODO: dynamically adjust timings */
int retry_do() {
int err = APP_ERR_NONE;
if (soft_retry_no >= NUM_SOFT_RETRIES) {
return 0;
// *** TODO: Add a 'hard retry', which re-initializes the cable, re-enumerates the bus, etc.
} else { /* quick reset */
if(err |= tap_reset()) {
printf("Error %s while resetting for retry.\n", get_err_string(err));
return 0;
}
// Put us back into DEBUG mode
if(err |= tap_enable_debug_module()) {
printf("Error %s enabling debug module during retry.\n", get_err_string(err));
return 0;
}
soft_retry_no++;
printf("Retry...\n");
}
return 1;
}
/* resets retry counter */
void retry_ok() {
soft_retry_no = 0;
}