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

Subversion Repositories or1k_soc_on_altera_embedded_dev_kit

[/] [or1k_soc_on_altera_embedded_dev_kit/] [trunk/] [soc/] [sw/] [adv_jtag_bridge/] [chain_commands.c] - Blame information for rev 21

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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