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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [tags/] [ADS_RELEASE_1_1_0/] [Software/] [adv_jtag_bridge/] [chain_commands.c] - Blame information for rev 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 nyawn
/* jtag_bridge.c -- JTAG protocol bridge between GDB and Advanced debug module.
2
   Copyright(C) Nathan Yawn, nyawn@opencores.net
3
   based on code from jp2 by Marko Mlinar, markom@opencores.org
4
 
5
   This file contains functions which perform high-level transactions
6
   on a JTAG chain and debug unit, such as setting a value in the TAP IR
7
   or doing a burst write through the wishbone module of the debug unit.
8
   It uses the protocol for the Advanced Debug Interface (adv_dbg_if).
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
 
26
 
27
#include <stdio.h>
28
#include <stdlib.h>  // for malloc()
29
#include <unistd.h>  // for usleep()
30
#include <pthread.h>  // for mutexes
31
 
32
#include "chain_commands.h"  // For the return error codes
33
#include "adv_debug_module.h"     // hardware-specific defines for the debug module
34
#include "altera_virtual_jtag.h"  // hardware-specifg defines for the Altera Virtual JTAG interface
35
#include "cable_common.h"         // low-level JTAG IO routines
36
#include "errcodes.h"
37
 
38
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
39
 
40
// Polynomial for the CRC calculation
41
// Yes, it's backwards.  Yes, this is on purpose.
42
// The hardware is designed this way to save on logic and routing,
43
// and it's really all the same to us here.
44
#define DBG_CRC_POLY 0xedb88320
45
 
46
// How many tries before an abort
47
#define NUM_SOFT_RETRIES 5
48
 
49
// How many '0' status bits to get during a burst read
50
// before giving up
51
#define MAX_READ_STATUS_WAIT 100
52
 
53
// wait for 100ms
54
#define JTAG_RETRY_WAIT() usleep(100000);
55
 
56
// Mutex for access to the JTAG cable.  Makes API calls threadsafe.
57
pthread_mutex_t dbg_access_mutex = PTHREAD_MUTEX_INITIALIZER;
58
 
59
/* Currently selected scan chain in the debug unit - just to prevent unnecessary
60
   transfers. */
61
int current_chain = -1;
62
int desired_chain = -1;
63
 
64
// Currently selected internal register in each module
65
// - cuts down on unnecessary transfers
66
int current_reg_idx[DBG_MAX_MODULES];
67
 
68
/* The chain that should be currently selected. */
69
//static int dbg_chain = -1;
70
 
71
// Retry data
72
int soft_retry_no = 0;
73
//static int hard_retry_no = 0;
74
 
75
// Configuration data
76
int global_IR_size = 0;
77
int global_IR_prefix_bits = 0;
78
int global_IR_postfix_bits = 0;
79
int global_DR_prefix_bits = 0;
80
int global_DR_postfix_bits = 0;
81
unsigned int global_jtag_cmd_debug = 0;        // Value to be shifted into the TAP IR to select the debug unit (unused for virtual jtag)
82
unsigned char global_altera_virtual_jtag = 0;  // Set true to use virtual jtag mode
83
unsigned int vjtag_cmd_vir = ALTERA_CYCLONE_CMD_VIR;  // virtual IR-shift command for altera devices, may be configured on command line
84
unsigned int vjtag_cmd_vdr = ALTERA_CYCLONE_CMD_VDR; // virtual DR-shift, ditto
85
unsigned char global_xilinx_bscan = 0;  // Set true if the hardware uses a Xilinx BSCAN_* device.
86
 
87
// Prototypes for local functions
88
uint32_t compute_crc(uint32_t crc_in, uint32_t data_in, int length_bits);
89
int retry_do(void);
90
void retry_ok(void);
91
 
92
int jtag_write_bit(uint8_t packet);
93
int jtag_read_write_bit(uint8_t packet, uint8_t *in_bit);
94
int jtag_write_stream(uint32_t *out_data, int length_bits, unsigned char set_TMS);
95
int jtag_read_write_stream(uint32_t *out_data, uint32_t *in_data, int length_bits,
96
                           unsigned char adjust, unsigned char set_TMS);
97
 
98
 
99
int dbg_select_module(int chain);
100
int dbg_select_ctrl_reg(unsigned long regidx);
101
int dbg_ctrl_write(unsigned long regidx, uint32_t *cmd_data, int length_bits);
102
int dbg_ctrl_read(unsigned long regidx, uint32_t *data, int databits);
103
int dbg_burst_command(unsigned int opcode, unsigned long address, int length_words);
104
int dbg_wb_burst_read(int word_size_bytes, int word_count, unsigned long start_address, void *data);
105
int dbg_wb_burst_write(void *data, int word_size_bytes, int word_count, unsigned long start_address);
106
 
107
 
108
 
109
///////////////////////////////////////////////////////////////////////
110
// Configuration
111
 
112
void config_set_IR_size(int size) {
113
  global_IR_size = size;
114
}
115
 
116
void config_set_IR_prefix_bits(int bits) {
117
  global_IR_prefix_bits = bits;
118
}
119
 
120
void config_set_IR_postfix_bits(int bits) {
121
  global_IR_postfix_bits = bits;
122
}
123
 
124
void config_set_DR_prefix_bits(int bits) {
125
  global_DR_prefix_bits = bits;
126
}
127
 
128
void config_set_DR_postfix_bits(int bits) {
129
  global_DR_postfix_bits = bits;
130
}
131
 
132
void config_set_debug_cmd(unsigned int cmd) {
133
  global_jtag_cmd_debug = cmd;
134
}
135
 
136
void config_set_alt_vjtag(unsigned char enable) {
137
  global_altera_virtual_jtag = (enable) ? 1:0;
138
}
139
 
140
// At present, all devices which support virtual JTAG use the same VIR/VDR
141
// commands.  But, if they ever change, these can be changed on the command line.
142
void config_set_vjtag_cmd_vir(unsigned int cmd) {
143
  vjtag_cmd_vir = cmd;
144
}
145
 
146
void config_set_vjtag_cmd_vdr(unsigned int cmd) {
147
  vjtag_cmd_vdr = cmd;
148
}
149
 
150
void config_set_xilinx_bscan(unsigned char enable) {
151
  global_xilinx_bscan = (enable) ? 1:0;
152
}
153
 
154
//////////////////////////////////////////////////////////////////////
155
// Functions which operate on the JTAG TAP
156
 
157
 
158
/* Resets JTAG - Writes TRST=1, and TRST=0.  Sends 8 TMS to put the TAP
159
 * in test_logic_reset mode, for good measure.
160
 */
161
int tap_reset(void) {
162
  int i;
163
  int err = APP_ERR_NONE;
164
 
165
  debug("\nreset(");
166
  err |= jtag_write_bit(0);
167
  JTAG_RETRY_WAIT();
168
  /* In case we don't have TRST reset it manually */
169
  for(i = 0; i < 8; i++) err |= jtag_write_bit(TMS);
170
  err |= jtag_write_bit(TRST);  // if TRST not supported, this puts us in run test / idle
171
  JTAG_RETRY_WAIT();
172
  err |= jtag_write_bit(0);  // run test / idle
173
  debug(")\n");
174
 
175
  // Reset data on current module/register selections
176
  current_chain = -1;
177
  for(i = 0; i < DBG_MAX_MODULES; i++)
178
    current_reg_idx[i] = -1;
179
 
180
  return err;
181
}
182
 
183
  // Set the IR with the DEBUG command, one way or the other
184
int tap_enable_debug_module(void)
185
{
186
  uint32_t data;
187
 int err = APP_ERR_NONE;
188
 
189
  if(global_altera_virtual_jtag) {
190
    /* Set for virtual IR shift */
191
    err |= tap_set_ir(vjtag_cmd_vir);  // This is the altera virtual IR scan command
192
    err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
193
    err |= jtag_write_bit(0); /* CAPTURE_DR */
194
    err |= jtag_write_bit(0); /* SHIFT_DR */
195
 
196
    /* Select debug scan chain in  virtual IR */
197
    data = (0x1<<ALT_VJTAG_IR_SIZE)|ALT_VJTAG_CMD_DEBUG;
198
    err |= jtag_write_stream(&data, (ALT_VJTAG_IR_SIZE+1), 1);  // EXIT1_DR
199
    err |= jtag_write_bit(TMS); /* UPDATE_DR */
200
    err |= jtag_write_bit(0); /* IDLE */
201
 
202
    // This is a command to set an altera device to the "virtual DR shift" command
203
    err |= tap_set_ir(vjtag_cmd_vdr);
204
  }
205
  else {
206
    /* select debug scan chain and stay in it forever */
207
    err |= tap_set_ir(global_jtag_cmd_debug);
208
  }
209
 
210
  return err;
211
}
212
 
213
/* Moves a value into the TAP instruction register (IR)
214
 * Includes adjustment for scan chain IR length.
215
 */
216
uint32_t *ir_chain = NULL;
217
 
218
int tap_set_ir(int ir) {
219
  int chain_size;
220
  int chain_size_words;
221
  int i;
222
  int startoffset, startshift;
223
  int err = APP_ERR_NONE;
224
 
225
  // Adjust desired IR with prefix, postfix bits to set other devices in the chain to BYPASS
226
  chain_size = global_IR_size + global_IR_prefix_bits + global_IR_postfix_bits;
227
  chain_size_words = (chain_size/32)+1;
228
 
229
  if(ir_chain == NULL)  { // We have no way to know in advance how many bits there are in the combined IR register
230
    ir_chain = (uint32_t *) malloc(chain_size_words * sizeof(uint32_t));
231
    if(ir_chain == NULL)
232
      return APP_ERR_MALLOC;
233
  }
234
 
235
  for(i = 0; i < chain_size_words; i++)
236
    ir_chain[i] = 0xFFFFFFFF;  // Set all other devices to BYPASS
237
 
238
  // Copy the IR value into the output stream
239
  startoffset = global_IR_postfix_bits/32;
240
  startshift = (global_IR_postfix_bits - (startoffset*32));
241
  ir_chain[startoffset] &= (ir << startshift);
242
  ir_chain[startoffset] |= ~(0xFFFFFFFF << startshift);  // Put the 1's back in the LSB positions
243
  ir_chain[startoffset] |= (0xFFFFFFFF << (startshift + global_IR_size));  // Put 1's back in MSB positions, if any 
244
  if((startshift + global_IR_size) > 32) { // Deal with spill into the next word
245
    ir_chain[startoffset+1] &= ir >> (32-startshift);
246
    ir_chain[startoffset+1] |= (0xFFFFFFFF << (global_IR_size - (32-startshift)));  // Put the 1's back in the MSB positions
247
  }
248
 
249
  // Do the actual JTAG transaction
250
  debug("Set IR 0x%X\n", ir);
251
  err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
252
  err |= jtag_write_bit(TMS); /* SELECT_IR SCAN */
253
 
254
  err |= jtag_write_bit(0); /* CAPTURE_IR */
255
  err |= jtag_write_bit(0); /* SHIFT_IR */
256
 
257
  /* write data, EXIT1_IR */
258
  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);
259
  err |= cable_write_stream(ir_chain, chain_size, 1);  // Use cable_ call directly (not jtag_), so we don't add DR prefix bits
260
  debug("Done setting IR\n");
261
 
262
  err |= jtag_write_bit(TMS); /* UPDATE_IR */
263
  err |= jtag_write_bit(0); /* IDLE */
264
  current_chain = -1;
265
  return err;
266
}
267
 
268
 
269
// This assumes we are in the IDLE state, and we want to be in the SHIFT_DR state.
270
// We may need to do a little extra work, for Altera virtual JTAG...
271
int tap_set_shift_dr(void)
272
{
273
  int err = APP_ERR_NONE;
274
 
275
  // This always needs to be done
276
  err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
277
  err |= jtag_write_bit(0); /* CAPTURE_DR */
278
  err |= jtag_write_bit(0); /* SHIFT_DR */
279
  return err;
280
}
281
 
282
 
283
////////////////////////////////////////////////////////////////////
284
// Operations to read / write data over JTAG
285
 
286
 
287
/* Writes TCLK=0, TRST=1, TMS=bit1, TDI=bit0
288
   and    TCLK=1, TRST=1, TMS=bit1, TDI=bit0
289
*/
290
int jtag_write_bit(uint8_t packet) {
291
  debug("Wbit(%i)\n", packet);
292
  return cable_write_bit(packet);
293
}
294
 
295
int jtag_read_write_bit(uint8_t packet, uint8_t *in_bit) {
296
  int retval = cable_read_write_bit(packet, in_bit);
297
  debug("RWbit(%i,%i)", packet, *in_bit);
298
  return retval;
299
}
300
 
301
// This automatically adjusts for the DR length (other devices on scan chain)
302
// when the set_TMS flag is true.
303
int jtag_write_stream(uint32_t *out_data, int length_bits, unsigned char set_TMS)
304
{
305
  int i;
306
  int err = APP_ERR_NONE;
307
 
308
  if(!set_TMS)
309
    err |= cable_write_stream(out_data, length_bits, 0);
310
  else if(global_DR_prefix_bits == 0)
311
    err |= cable_write_stream(out_data, length_bits, 1);
312
  else {
313
    err |= cable_write_stream(out_data, length_bits, 0);
314
    // It could be faster to do a cable_write_stream for all the prefix bits (if >= 8 bits),
315
    // but we'd need a data array of unknown (and theoretically unlimited)
316
    // size to hold the 0 bits to write.
317
    for(i = 0; i < (global_DR_prefix_bits-1); i++)
318
      err |= jtag_write_bit(0);
319
    err |= jtag_write_bit(TMS);
320
  }
321
  return err;
322
}
323
 
324
// When set_TMS is true, this function insures the written data is in the desired position (past prefix bits)
325
// before sending TMS.  When 'adjust' is true, this function insures that the data read in accounts for postfix
326
// bits (they are shifted through before the read starts).
327
int jtag_read_write_stream(uint32_t *out_data, uint32_t *in_data, int length_bits, unsigned char adjust, unsigned char set_TMS)
328
{
329
  int i;
330
  int err = APP_ERR_NONE;
331
 
332
  if(adjust && (global_DR_postfix_bits > 0)) {
333
    // It would be faster to do a cable_write_stream for all the postfix bits,
334
    // but we'd need a data array of unknown (and theoretically unlimited)
335
    // size to hold the '0' bits to write.
336
    for(i = 0; i < global_DR_postfix_bits; i++)
337
      err |= cable_write_bit(0);
338
  }
339
 
340
  // If there are both prefix and postfix bits, we may shift more bits than strictly necessary.
341
  // If we shifted out the data while burning through the postfix bits, these shifts could be subtracted
342
  // from the number of prefix shifts.  However, that way leads to madness.
343
  if(!set_TMS)
344
    err |= cable_read_write_stream(out_data, in_data, length_bits, 0);
345
  else if(global_DR_prefix_bits == 0)
346
    err |= cable_read_write_stream(out_data, in_data, length_bits, 1);
347
  else {
348
    err |= cable_read_write_stream(out_data, in_data, length_bits, 0);
349
    // It would be faster to do a cable_write_stream for all the prefix bits,
350
    // but we'd need a data array of unknown (and theoretically unlimited)
351
    // size to hold the '0' bits to write.
352
    for(i = 0; i < (global_DR_prefix_bits-1); i++)
353
      err |= jtag_write_bit(0);
354
    err |= jtag_write_bit(TMS);
355
  }
356
  return err;
357
}
358
 
359
 
360
 
361
// This function attempts to determine the structure of the JTAG chain
362
// It can determine how many devices are present.
363
// If the devices support the IDCODE command, it will be read and stored.
364
// There is no way to automatically determine the length of the IR registers - 
365
// this must be read from a BSDL file, if IDCODE is supported.
366
// When IDCODE is not supported, IR length of the target device must be entered on the command line.
367
 
368
#define ALLOC_SIZE 64
369
#define MAX_DEVICES 1024
370
int jtag_enumerate_chain(uint32_t **id_array, int *num_devices)
371
{
372
  uint32_t invalid_code = 0x7f;  // Shift this out, we know we're done when we get it back
373
  const unsigned int done_code = 0x3f;  // invalid_code is altered, we keep this for comparison (minus the start bit)
374
  int devindex = 0;  // which device we are currently trying to detect
375
  unsigned int tempID;
376
  uint32_t temp_manuf_code;
377
  uint32_t temp_rest_code;
378
  uint8_t start_bit = 0;
379
  unsigned long *idcodes;
380
  int reallocs = 0;
381
  int err = APP_ERR_NONE;
382
 
383
  // Malloc a reasonable number of entries, we'll expand if we must.  Linked lists are overrated.
384
  idcodes = (unsigned long *) malloc(ALLOC_SIZE*sizeof(unsigned long));
385
  if(idcodes == NULL) {
386
    printf("Failed to allocate memory for device ID codes!\n");
387
    return APP_ERR_MALLOC;
388
  }
389
 
390
  // Put in SHIFT-DR mode
391
  err |= jtag_write_bit(TMS); /* SELECT_DR SCAN */
392
  err |= jtag_write_bit(0); /* CAPTURE_DR */
393
  err |= jtag_write_bit(0); /* SHIFT_DR */
394
 
395
  printf("Enumerating JTAG chain...\n");
396
 
397
  // Putting a limit on the # of devices supported has the useful side effect
398
  // of insuring we still exit in error cases (we never get the 0x7f manuf. id)
399
  while(devindex < MAX_DEVICES) {
400
    // get 1 bit. 0 = BYPASS, 1 = start of IDCODE
401
    err |= jtag_read_write_bit(invalid_code&0x01, &start_bit);
402
    invalid_code >>= 1;
403
 
404
    if(start_bit == 0) {
405
      if(devindex >= (ALLOC_SIZE << reallocs)) {  // Enlarge the memory array if necessary, double the size each time
406
        idcodes = (unsigned long *) realloc(idcodes, (ALLOC_SIZE << ++reallocs)*sizeof(unsigned long));
407
        if(idcodes == NULL) {
408
          printf("Failed to allocate memory for device ID codes during enumeration!\n");
409
          return APP_ERR_MALLOC;
410
        }
411
      }
412
      idcodes[devindex] = -1;
413
      devindex++;
414
    }
415
    else {
416
      // get 11 bit manufacturer code
417
      err |= jtag_read_write_stream(&invalid_code, &temp_manuf_code, 11, 0, 0);
418
      invalid_code >>= 11;
419
 
420
      if(temp_manuf_code != done_code) {
421
        // get 20 more bits, rest of ID
422
        err |= jtag_read_write_stream(&invalid_code, &temp_rest_code, 20, 0, 0);
423
        invalid_code >>= 20;
424
        tempID = (temp_rest_code << 12) | (temp_manuf_code << 1) | 0x01;
425
        if(devindex >= (ALLOC_SIZE << reallocs)) {  // Enlarge the memory array if necessary, double the size each time
426
          idcodes = (unsigned long *) realloc(idcodes, (ALLOC_SIZE << ++reallocs)*sizeof(unsigned long));
427
          if(idcodes == NULL) {
428
            printf("Failed to allocate memory for device ID codes during enumeration!\n");
429
            return APP_ERR_MALLOC;
430
          }
431
        }
432
        idcodes[devindex] = tempID;
433
        devindex++;
434
      } else {
435
        break;
436
      }
437
    }
438
 
439
    if(err)  // Don't try to keep probing if we get a comm. error
440
      return err;
441
  }
442
 
443
  if(devindex >= MAX_DEVICES)
444
    printf("WARNING: maximum supported devices on JTAG chain (%i) exceeded.\n", MAX_DEVICES);
445
 
446
  // Put in IDLE mode
447
  err |= jtag_write_bit(TMS); /* EXIT1_DR */
448
  err |= jtag_write_bit(TMS); /* UPDATE_DR */
449
  err |= jtag_write_bit(0); /* IDLE */
450
 
451
  *id_array = idcodes;
452
  *num_devices = devindex;
453
  return err;
454
}
455
 
456
 
457
 
458
int jtag_get_idcode(uint32_t cmd, uint32_t *idcode)
459
{
460
  uint32_t data_out = 0;
461
  int err = APP_ERR_NONE;
462
  unsigned char saveconfig = global_altera_virtual_jtag;
463
  global_altera_virtual_jtag = 0; // We want the actual IDCODE, not the virtual device IDCODE
464
 
465
  err |= tap_set_ir(cmd);
466
  err |= tap_set_shift_dr();
467
  err |= jtag_read_write_stream(&data_out, idcode, 32, 1, 1);       /* EXIT1_DR */
468
 
469
  if(err)
470
    printf("Error getting ID code!\n");
471
 
472
  // Put in IDLE mode
473
  err |= jtag_write_bit(TMS); /* UPDATE_DR */
474
  err |= jtag_write_bit(0); /* IDLE */
475
 
476
  global_altera_virtual_jtag = saveconfig;
477
  return err;
478
}
479
 
480
 
481
/////////////////////////////////////////////////////////////////
482
// Helper functions
483
 
484
/* counts retries and returns zero if we should abort */
485
/* TODO: dynamically adjust timings for jp2 */
486
int retry_do() {
487
  int err = APP_ERR_NONE;
488
 
489
  if (soft_retry_no >= NUM_SOFT_RETRIES) {
490
      return 0;
491
 
492
      // *** TODO:  Add a 'hard retry', which re-initializes the cable, re-enumerates the bus, etc.
493
 
494
  } else { /* quick reset */
495
    if(err |= tap_reset()) {
496
      printf("Error %s while resetting for retry.\n", get_err_string(err));
497
      return 0;
498
    }
499
 
500
    // Put us back into DEBUG mode
501
    if(err |= tap_enable_debug_module()) {
502
      printf("Error %s enabling debug module during retry.\n", get_err_string(err));
503
      return 0;
504
    }
505
 
506
    soft_retry_no++;
507
    printf("Retry...\n");
508
  }
509
 
510
  return 1;
511
}
512
 
513
/* resets retry counter */
514
void retry_ok() {
515
  soft_retry_no = 0;
516
}
517
 
518
uint32_t compute_crc(uint32_t crc_in, uint32_t data_in, int length_bits)
519
{
520
  int i;
521
  unsigned int d, c;
522
  uint32_t crc_out = crc_in;
523
 
524
  for(i = 0; i < length_bits; i = i+1)
525
    {
526
      d = ((data_in >> i) & 0x1) ? 0xffffffff : 0;
527
      c = (crc_out & 0x1) ? 0xffffffff : 0;
528
      crc_out = crc_out >> 1;
529
      crc_out = crc_out ^ ((d ^ c) & DBG_CRC_POLY);
530
    }
531
  return crc_out;
532
}
533
 
534
//////////////////////////////////////////////////////////////////
535
// Functions which operate on the debug unit
536
 
537
/* Selects one of the modules in the debug unit (e.g. wishbone unit, CPU0, etc.)
538
 */
539
int dbg_select_module(int chain)
540
{
541
  uint32_t data;
542
  int err = APP_ERR_NONE;
543
 
544
  if (current_chain == chain)
545
    return err;
546
 
547
  current_chain = -1;
548
  desired_chain = chain;
549
 
550
  // MSB of the data out must be set to 1, indicating a module select command
551
  data = chain | (1<<DBG_MODULE_SELECT_REG_SIZE);
552
 
553
  debug("select module %i\n", chain);
554
  err |= tap_set_shift_dr();    /* SHIFT_DR */
555
 
556
  /* write data, EXIT1_DR */
557
  err |= jtag_write_stream(&data, 3, 1);  // When TMS is set (last parameter), DR length is also adjusted; EXIT1_DR
558
 
559
  // *** If 'valid module selected' feedback is ever added, test it here
560
 
561
  err |= jtag_write_bit(TMS); /* UPDATE_DR */
562
  err |= jtag_write_bit(0); /* IDLE */
563
  current_chain = chain;
564
 
565
  if(err)
566
    printf("Error %s selecting active debug module\n", get_err_string(err));
567
 
568
  return err;
569
}
570
 
571
// Set the index of the desired register in the currently selected module
572
// 1 bit module select command
573
// 4 bits opcode
574
// n bits index
575
// Make sure the corrent module/chain is selected before calling this
576
int dbg_select_ctrl_reg(unsigned long regidx)
577
{
578
  uint32_t data;
579
  int index_len = 0;
580
  uint32_t opcode;
581
  int err = APP_ERR_NONE;
582
 
583
  if(err |= dbg_select_module(desired_chain))
584
    return err;
585
 
586
  debug("selreg %ld\n", regidx);
587
 
588
  // If this reg is already selected, don't do a JTAG transaction
589
  if(current_reg_idx[current_chain] == regidx)
590
    return APP_ERR_NONE;
591
 
592
  switch(current_chain) {
593
  case DC_WISHBONE:
594
    index_len = DBG_WB_REG_SEL_LEN;
595
    opcode = DBG_WB_CMD_IREG_SEL;
596
    break;
597
  case DC_CPU0:
598
    index_len = DBG_CPU0_REG_SEL_LEN;
599
    opcode = DBG_CPU0_CMD_IREG_SEL;
600
    break;
601
  case DC_CPU1:
602
    index_len = DBG_CPU1_REG_SEL_LEN;
603
    opcode = DBG_CPU1_CMD_IREG_SEL;
604
    break;
605
  default:
606
    printf("ERROR! Illegal debug chain selected while selecting control register!\n");
607
    return 1;
608
  }
609
 
610
 
611
  // Set up the data.
612
  data = (opcode & ~(1<<DBG_WB_OPCODE_LEN)) << index_len;  // MSB must be 0 to access modules
613
  data |= regidx;
614
 
615
  debug("Selreg: data is 0x%lX (opcode = 0x%lX)\n", data,opcode);
616
 
617
  err |= tap_set_shift_dr();  /* SHIFT_DR */
618
 
619
  /* write data, EXIT1_DR */
620
  err |= jtag_write_stream(&data, 5+index_len, 1);
621
 
622
  err |= jtag_write_bit(TMS); /* UPDATE_DR */
623
  err |= jtag_write_bit(0); /* IDLE */
624
 
625
  /* reset retry counter */
626
  retry_ok();
627
  current_reg_idx[current_chain] = regidx;
628
 
629
  if(err)
630
    printf("Error %s selecting control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
631
 
632
  return err;
633
}
634
 
635
 
636
/* Sends out a generic command to the selected debug unit module, LSB first.  Fields are:
637
 * MSB: 1-bit module command
638
 * 4-bit opcode
639
 * m-bit register index
640
 * n-bit data (LSB)
641
 * Note that in the data array, the LSB of data[0] will be sent first,
642
 * (and become the LSB of the command)
643
 * up through the MSB of data[0], then the LSB of data[1], etc.
644
 */
645
int dbg_ctrl_write(unsigned long regidx, uint32_t *cmd_data, int length_bits) {
646
  uint32_t data;
647
  int index_len = 0;
648
  uint32_t opcode;
649
  int err = APP_ERR_NONE;
650
 
651
  if(err |= dbg_select_module(desired_chain))
652
    return err;
653
 
654
  debug("ctrl wr idx %ld dat 0x%lX\n", regidx, cmd_data[0]);
655
 
656
  switch(current_chain) {
657
  case DC_WISHBONE:
658
    index_len = DBG_WB_REG_SEL_LEN;
659
    opcode = DBG_WB_CMD_IREG_WR;
660
    break;
661
  case DC_CPU0:
662
    index_len = DBG_CPU0_REG_SEL_LEN;
663
    opcode = DBG_CPU0_CMD_IREG_WR;
664
    break;
665
  case DC_CPU1:
666
    index_len = DBG_CPU1_REG_SEL_LEN;
667
    opcode = DBG_CPU1_CMD_IREG_WR;
668
    break;
669
  default:
670
    printf("ERROR! Illegal debug chain selected (%i) while doing control write!\n", current_chain);
671
    return 1;
672
  }
673
 
674
 
675
  // Set up the data.  We cheat a bit here, by using 2 stream writes.
676
  data = (opcode & ~(1<<DBG_WB_OPCODE_LEN)) << index_len;  // MSB must be 0 to access modules
677
  data |= regidx;
678
 
679
  err |= tap_set_shift_dr();  /* SHIFT_DR */
680
 
681
  /* write data, EXIT1_DR */
682
  err |= jtag_write_stream(cmd_data, length_bits, 0);
683
  err |= jtag_write_stream(&data, 5+index_len, 1);
684
 
685
  err |= jtag_write_bit(TMS); /* UPDATE_DR */
686
  err |= jtag_write_bit(0); /* IDLE */
687
 
688
  /* reset retry counter */
689
  retry_ok();
690
  current_reg_idx[current_chain] = regidx;
691
 
692
 if(err)
693
    printf("Error %s writing control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
694
 
695
  return err;
696
}
697
 
698
 
699
/* reads control register (internal to the debug unit)
700
 * Currently only 1 register in the CPU module, so no register select
701
 */
702
int dbg_ctrl_read(unsigned long regidx, uint32_t *data, int databits) {
703
  uint32_t outdata[4] = {0,0,0,0};  // *** We assume no more than 128 databits
704
  int opcode;
705
  int opcode_len;
706
  int err = APP_ERR_NONE;
707
 
708
  if(err |= dbg_select_module(desired_chain))
709
    return err;
710
 
711
  if(err |= dbg_select_ctrl_reg(regidx))
712
    return err;
713
 
714
  debug("ctrl rd idx %ld\n", regidx);
715
 
716
  // There is no 'read' command, We write a NOP to read
717
  switch(current_chain) {
718
  case DC_WISHBONE:
719
    opcode = DBG_WB_CMD_NOP;
720
    opcode_len = DBG_WB_OPCODE_LEN;
721
    break;
722
  case DC_CPU0:
723
    opcode = DBG_CPU0_CMD_NOP;
724
    opcode_len = DBG_CPU0_OPCODE_LEN;
725
    break;
726
  case DC_CPU1:
727
    opcode = DBG_CPU1_CMD_NOP;
728
    opcode_len = DBG_CPU1_OPCODE_LEN;
729
    break;
730
  default:
731
    printf("ERROR! Illegal debug chain selected while doing control read!\n");
732
    return 1;
733
  }
734
 
735
  outdata[0] = opcode & ~(0x1 << opcode_len);  // Zero MSB = op for module, not top-level debug unit
736
 
737
  err |= tap_set_shift_dr();  /* SHIFT_DR */
738
 
739
  // We cheat a bit here by using two stream operations.
740
  // First we burn the postfix bits and read the desired data, then we push a NOP
741
  // into position through the prefix bits.  We may be able to combine the two and save
742
  // some cycles, but that way leads to madness.
743
  err |= jtag_read_write_stream(outdata, data, databits, 1, 0);  // adjust for prefix bits
744
  err |= jtag_write_stream(outdata, opcode_len+1, 1);  // adjust for postfix bits, Set TMS: EXIT1_DR
745
 
746
  err |= jtag_write_bit(TMS); /* UPDATE_DR */
747
  err |= jtag_write_bit(0); /* IDLE */
748
 
749
 
750
  /* reset retry counter */
751
  retry_ok();
752
 
753
  if(err)
754
    printf("Error %s reading control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
755
 
756
  return err;
757
}
758
 
759
 
760
/* sends out a burst command to the selected module in the debug unit (MSB to LSB):
761
 * 1-bit module command
762
 * 4-bit opcode
763
 * 32-bit address
764
 * 16-bit length (of the burst, in words)
765
 */
766
int dbg_burst_command(unsigned int opcode, unsigned long address, int length_words) {
767
  uint32_t data[2];
768
  int err = APP_ERR_NONE;
769
 
770
  if(err |= dbg_select_module(desired_chain))
771
    return err;
772
 
773
  debug("burst op %i adr 0x%lX len %i\n", opcode, address, length_words);
774
 
775
  // Set up the data
776
  data[0] = length_words | (address << 16);
777
  data[1] = ((address >> 16) | ((opcode & 0xf) << 16)) & ~(0x1<<20); // MSB must be 0 to access modules
778
 
779
  err |= tap_set_shift_dr();  /* SHIFT_DR */
780
 
781
  /* write data, EXIT1_DR */
782
  err |= jtag_write_stream(data, 53, 1);  // When TMS is set (last parameter), DR length is also adjusted; EXIT1_DR
783
 
784
  err |= jtag_write_bit(TMS); /* UPDATE_DR */
785
  err |= jtag_write_bit(0); /* IDLE */
786
 
787
  /* reset retry counter */
788
  retry_ok();
789
 
790
  if(err)
791
    printf("Error %s sending burst command to module %i\n", get_err_string(err), desired_chain);
792
 
793
  return err;
794
}
795
 
796
// Set up and execute a burst read from a contiguous block of addresses.
797
// Note that there is a minor weakness in the CRC algorithm in case of retries:
798
// the CRC is only checked for the final burst read.  Thus, if errors/partial retries
799
// break up a transfer into multiple bursts, only the last burst will be CRC protected.
800
#define MAX_BUS_ERRORS 10
801
int dbg_wb_burst_read(int word_size_bytes, int word_count, unsigned long start_address, void *data)
802
{
803
  unsigned char opcode;
804
  uint8_t status;
805
  unsigned long instream;
806
  int i, j;
807
  unsigned long crc_calc;
808
  uint32_t crc_read;
809
  unsigned char word_size_bits;
810
  uint32_t out_data = 0;
811
  uint32_t in_data;
812
  unsigned long addr;
813
  uint32_t err_data[2];
814
  int bus_error_retries = 0;
815
  int err = APP_ERR_NONE;
816
 
817
    debug("Doing burst read, word size %d, word count %d, start address 0x%lX", word_size_bytes, word_count, start_address);
818
 
819
    if(word_count <= 0) {
820
      debug("Ignoring illegal read burst length (%d)\n", word_count);
821
      return 0;
822
    }
823
 
824
    instream = 0;
825
    word_size_bits = word_size_bytes << 3;
826
 
827
    // Select the appropriate opcode
828
    switch(current_chain) {
829
    case DC_WISHBONE:
830
      if (word_size_bytes == 1) opcode = DBG_WB_CMD_BREAD8;
831
      else if(word_size_bytes == 2) opcode = DBG_WB_CMD_BREAD16;
832
      else if(word_size_bytes == 4) opcode = DBG_WB_CMD_BREAD32;
833
      else {
834
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
835
        opcode = DBG_WB_CMD_BREAD32;
836
      }
837
      break;
838
    case DC_CPU0:
839
      if(word_size_bytes == 4) opcode = DBG_CPU0_CMD_BREAD32;
840
      else {
841
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
842
        opcode = DBG_CPU0_CMD_BREAD32;
843
      }
844
      break;
845
    case DC_CPU1:
846
      if(word_size_bytes == 4) opcode = DBG_CPU1_CMD_BREAD32;
847
      else {
848
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
849
        opcode = DBG_CPU0_CMD_BREAD32;
850
      }
851
      break;
852
    default:
853
      printf("ERROR! Illegal debug chain selected while doing burst read!\n");
854
      return 1;
855
    }
856
 
857
 wb_burst_read_retry_full:
858
    i = 0;
859
    addr = start_address;
860
 wb_burst_read_retry_partial:
861
    crc_calc = 0xffffffff;
862
 
863
 
864
    // Send the BURST READ command, returns TAP to idle state
865
    if(err |= dbg_burst_command(opcode, addr, (word_count-i)))  // word_count-i in case of partial retry 
866
      return err;
867
 
868
    // This is a kludge to word around oddities in the Xilinx BSCAN_* devices, and the
869
    // adv_dbg_if state machine.  The debug FSM needs 1 TCK between UPDATE_DR above, and
870
    // the CAPTURE_DR below, and the BSCAN_* won't provide it.  So, we force it, by putting the TAP
871
    // in BYPASS, which makes the debug_select line inactive, which is AND'ed with the TCK line (in the xilinx_internal_jtag module),
872
    // which forces it low.  Then we re-enable USER1/debug_select to make TCK high.  One TCK
873
    // event, the hard way. 
874
    if(global_xilinx_bscan) {
875
      err |= tap_set_ir(0xFFFFFFFF);
876
      err |= tap_enable_debug_module();
877
    }
878
 
879
    // Get us back to shift_dr mode to read a burst
880
    err |=  tap_set_shift_dr();
881
 
882
    // We do not adjust for the DR length here.  BYPASS regs are loaded with 0,
883
    // and the debug unit waits for a '1' status bit before beginning to read data.
884
 
885
   // Repeat for each word: wait until ready = 1, then read word_size_bits bits.
886
   for(; i < word_count; i++)
887
     {
888
       // Get 1 status bit, then word_size_bytes*8 bits
889
       status = 0;
890
       j = 0;
891
       while(!status) {  // Status indicates whether there is a word available to read.  Wait until it returns true.
892
         err |= jtag_read_write_bit(0, &status);
893
         j++;
894
         // If max count exceeded, retry starting with the failure address
895
         if(j > MAX_READ_STATUS_WAIT) {
896
           printf("Burst read timed out.\n");
897
           if(!retry_do()) {
898
             printf("Retry count exceeded in burst read!\n");
899
             return err|APP_ERR_MAX_RETRY;
900
           }
901
           err = APP_ERR_NONE;  // on retry, errors cleared
902
           addr = start_address + (i*word_size_bytes);
903
           goto wb_burst_read_retry_partial;
904
         }
905
       }
906
 
907
       if(j > 1) {  // It's actually normal for the first read of a burst to take 2 tries, even with a fast WB clock - 3 with a Xilinx BSCAN
908
         debug("Took %0d tries before good status bit during burst read", j);
909
       }
910
 
911
       // Get one word of data
912
       err |= jtag_read_write_stream(&out_data, &in_data, word_size_bits, 0, 0);
913
       debug("Read 0x%0lx", in_data);
914
 
915
       if(err) {  // Break and retry as soon as possible on error
916
         printf("Error %s during burst read.\n", get_err_string(err));
917
           if(!retry_do()) {
918
             printf("Retry count exceeded in burst read!\n");
919
             return err|APP_ERR_MAX_RETRY;
920
           }
921
           err = APP_ERR_NONE;  // on retry, errors cleared
922
           addr = start_address + (i*word_size_bytes);
923
           goto wb_burst_read_retry_partial;
924
       }
925
 
926
       crc_calc = compute_crc(crc_calc, in_data, word_size_bits);
927
 
928
       if(word_size_bytes == 1) ((unsigned char *)data)[i] = in_data & 0xFF;
929
       else if(word_size_bytes == 2) ((unsigned short *)data)[i] = in_data & 0xFFFF;
930
       else ((unsigned long *)data)[i] = in_data;
931
     }
932
 
933
   // All bus data was read.  Read the data CRC from the debug module.
934
   err |= jtag_read_write_stream(&out_data, &crc_read, 32, 0, 1);
935
   err |= jtag_write_bit(TMS);  // update_ir
936
   err |= jtag_write_bit(0);    // idle
937
 
938
   if(crc_calc != crc_read) {
939
     printf("CRC ERROR! Computed 0x%lx, read CRC 0x%lx\n", crc_calc, crc_read);
940
     if(!retry_do()) {
941
       printf("Retry count exceeded!  Abort!\n\n");
942
       return err|APP_ERR_CRC;
943
     }
944
     goto  wb_burst_read_retry_full;
945
   }
946
   else debug("CRC OK!");
947
 
948
 
949
 
950
   // Now, read the error register, and retry/recompute as necessary.
951
   if(current_chain == DC_WISHBONE)
952
     {
953
       err |= dbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 1);  // First, just get 1 bit...read address only if necessary,
954
       if(err_data[0] & 0x1) {  // Then we have a problem.
955
         err |= dbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 33);
956
         addr = (err_data[0] >> 1) | (err_data[1] << 31);
957
         i = (addr - start_address) / word_size_bytes;
958
         printf("ERROR!  WB bus error during burst read, address 0x%lX (index 0x%X), retrying!\n", addr, i);
959
         bus_error_retries++;
960
         if(bus_error_retries > MAX_BUS_ERRORS) {
961
           printf("Max WB bus errors reached during burst read\n");
962
           return err|APP_ERR_MAX_BUS_ERR;
963
         }
964
         // Don't call retry_do(), a JTAG reset won't help a WB bus error
965
         err_data[0] = 1;
966
         err |= dbg_ctrl_write(DBG_WB_REG_ERROR, err_data, 1);  // Write 1 bit, to reset the error register,
967
         goto wb_burst_read_retry_partial;
968
       }
969
     }
970
 
971
   retry_ok();
972
   return err;
973
}
974
 
975
// Set up and execute a burst write to a contiguous set of addresses
976
int dbg_wb_burst_write(void *data, int word_size_bytes, int word_count, unsigned long start_address)
977
{
978
  unsigned char opcode;
979
  uint8_t status;
980
  uint32_t datawords[2] = {0,0};
981
  uint32_t statuswords[2] = {0,0};
982
  int i;
983
  unsigned long crc_calc;
984
  uint32_t crc_match;
985
  unsigned int word_size_bits;
986
  unsigned long addr;
987
  int bus_error_retries = 0;
988
  uint32_t err_data[2];
989
  int loopct, successes;
990
  int first_status_loop;
991
  int err = APP_ERR_NONE;
992
 
993
    debug("Doing burst write, word size %d, word count %d, start address 0x%lx", word_size_bytes, word_count, start_address);
994
    word_size_bits = word_size_bytes << 3;
995
 
996
    if(word_count <= 0) {
997
      printf("Ignoring illegal burst write size (%d)\n", word_count);
998
      return 0;
999
    }
1000
 
1001
    // Select the appropriate opcode
1002
    switch(current_chain) {
1003
    case DC_WISHBONE:
1004
      if (word_size_bytes == 1) opcode = DBG_WB_CMD_BWRITE8;
1005
      else if(word_size_bytes == 2) opcode = DBG_WB_CMD_BWRITE16;
1006
      else if(word_size_bytes == 4) opcode = DBG_WB_CMD_BWRITE32;
1007
      else {
1008
        printf("Tried WB burst write with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
1009
        opcode = DBG_WB_CMD_BWRITE32;
1010
      }
1011
      break;
1012
    case DC_CPU0:
1013
      if(word_size_bytes == 4) opcode = DBG_CPU0_CMD_BWRITE32;
1014
      else {
1015
        printf("Tried CPU0 burst write with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
1016
        opcode = DBG_CPU0_CMD_BWRITE32;
1017
      }
1018
      break;
1019
    case DC_CPU1:
1020
      if(word_size_bytes == 4) opcode = DBG_CPU1_CMD_BWRITE32;
1021
      else {
1022
        printf("Tried CPU1 burst write with invalid word size (%0X), defaulting to 4-byte words", word_size_bytes);
1023
        opcode = DBG_CPU0_CMD_BWRITE32;
1024
      }
1025
      break;
1026
    default:
1027
      printf("ERROR! Illegal debug chain selected while doing burst WRITE!\n");
1028
      return 1;
1029
    }
1030
 
1031
    // Compute which loop iteration in which to expect the first status bit
1032
    first_status_loop = 1 + ((global_DR_prefix_bits + global_DR_postfix_bits)/(word_size_bits+1));
1033
 
1034
 wb_burst_write_retry_full:
1035
    i = 0;
1036
    addr = start_address;
1037
 wb_burst_write_retry_partial:
1038
    crc_calc = 0xffffffff;
1039
    successes = 0;
1040
 
1041
 
1042
    // Send burst command, return to idle state
1043
    if(err |= dbg_burst_command(opcode, addr, (word_count-i)))  // word_count-i in case of partial retry
1044
      return err;
1045
 
1046
   // Get us back to shift_dr mode to write a burst
1047
   err |= jtag_write_bit(TMS);  // select_dr_scan
1048
   err |= jtag_write_bit(0);           // capture_ir
1049
   err |= jtag_write_bit(0);           // shift_ir
1050
 
1051
   // Write a start bit (a 1) so it knows when to start counting
1052
   err |= jtag_write_bit(TDO);
1053
 
1054
   // Now, repeat...
1055
   for(loopct = 0; i < word_count; i++,loopct++)  // loopct only used to check status... 
1056
     {
1057
       // Write word_size_bytes*8 bits, then get 1 status bit
1058
       if(word_size_bytes == 4)       datawords[0] = ((unsigned long *)data)[i];
1059
       else if(word_size_bytes == 2) datawords[0] = ((unsigned short *)data)[i];
1060
       else                          datawords[0] = ((unsigned char *)data)[i];
1061
 
1062
       crc_calc = compute_crc(crc_calc, datawords[0], word_size_bits);
1063
 
1064
       // This is an optimization
1065
       if((global_DR_prefix_bits + global_DR_postfix_bits) == 0) {
1066
         err |= jtag_write_stream(datawords, word_size_bits, 0);  // Write data
1067
         err |= jtag_read_write_bit(0, &status);  // Read status bit
1068
         if(!status) {
1069
           addr = start_address + (i*word_size_bytes);
1070
           printf("Write before bus ready, retrying (idx %i, addr 0x%08lX).\n", i, addr);
1071
           if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
1072
           // Don't bother going to TAP idle state, we're about to reset the TAP
1073
           goto wb_burst_write_retry_partial;
1074
         }
1075
       }
1076
       else {  // This is slower (for a USB cable anyway), because a read takes 1 more USB transaction than a write.
1077
         err |= jtag_read_write_stream(datawords, statuswords, word_size_bits+1, 0, 0);
1078
         debug("St. 0x%08lX 0x%08lX\n", statuswords[0], statuswords[1]);
1079
         status = (statuswords[0] || statuswords[1]);
1080
         if(loopct > first_status_loop) {
1081
           if(status) successes++;
1082
           else {
1083
             i = successes;
1084
             addr = start_address + (i*word_size_bytes);
1085
             printf("Write before bus ready, retrying (idx %i, addr 0x%08lX).\n", i, addr);
1086
             if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
1087
             // Don't bother going to TAP idle state, we're about to reset the TAP
1088
             goto wb_burst_write_retry_partial;
1089
           }
1090
         }
1091
       }
1092
 
1093
       if(err) {
1094
         printf("Error %s getting status bit, retrying.\n", get_err_string(err));
1095
         if(!retry_do()) {
1096
           printf("Retry count exceeded!\n");
1097
           return err|APP_ERR_MAX_RETRY;
1098
         }
1099
         err = APP_ERR_NONE;
1100
         addr = start_address + (i*word_size_bytes);
1101
         // Don't bother going to TAP idle state, we're about to reset the TAP
1102
         goto wb_burst_write_retry_partial;
1103
         }
1104
 
1105
      debug("Wrote 0x%0lx", datawords[0]);
1106
     }
1107
 
1108
   // *** If this is a multi-device chain, at least one status bit will be lost.
1109
   // *** If we want to check for it, we'd have to look while sending the CRC, and
1110
   // *** maybe while burning bits to get the match bit.  So, for now, there is a
1111
   // *** hole here.
1112
 
1113
   // Done sending data, Send the CRC we computed
1114
   err |= jtag_write_stream(&crc_calc, 32, 0);
1115
   for(i = 0; i < global_DR_prefix_bits; i++)  // Push the CRC data all the way to the debug unit
1116
     err |= jtag_write_bit(0);                 // Can't do this with a stream command without setting TMS on the last bit
1117
 
1118
   // Read the 'CRC match' bit, and go to exit1_dr
1119
   // May need to adjust for other devices in chain!
1120
   datawords[0] = 0;
1121
   err |= jtag_read_write_stream(datawords, &crc_match, 1, 1, 0);  // set 'adjust' to pull match bit all the way in
1122
   // But don't set TMS above, that would shift prefix bits (again), wasting time.
1123
   err |= jtag_write_bit(TMS);  // exit1_dr
1124
   err |= jtag_write_bit(TMS);  // update_dr
1125
   err |= jtag_write_bit(0);           // idle
1126
 
1127
   if(!crc_match) {
1128
     printf("CRC ERROR! match bit after write is %ld (computed CRC 0x%lx)", crc_match, crc_calc);
1129
     if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
1130
     goto  wb_burst_write_retry_full;
1131
   }
1132
   else debug("CRC OK!");
1133
 
1134
 
1135
   // Now, read the error register and retry/recompute as needed
1136
   if (current_chain == DC_WISHBONE)
1137
     {
1138
       err |= dbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 1);  // First, just get 1 bit...read address only if necessary
1139
       if(err_data[0] & 0x1) {  // Then we have a problem.
1140
         err |= dbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 33);
1141
         addr = (err_data[0] >> 1) | (err_data[1] << 31);
1142
         i = (addr - start_address) / word_size_bytes;
1143
         printf("ERROR!  WB bus error during burst write, address 0x%lX (index 0x%X), retrying!\n", addr, i);
1144
         bus_error_retries++;
1145
         if(bus_error_retries > MAX_BUS_ERRORS) {
1146
           printf("Max WB bus errors reached!\n");
1147
           return err|APP_ERR_MAX_BUS_ERR;
1148
         }
1149
         // Don't call retry_do(), a JTAG reset won't help a WB bus error
1150
         err |= dbg_ctrl_write(DBG_WB_REG_ERROR, err_data, 1);  // Write 1 bit, to reset the error register.
1151
         goto wb_burst_write_retry_partial;
1152
       }
1153
     }
1154
 
1155
   retry_ok();
1156
   return err;
1157
}
1158
 
1159
 
1160
//////////////////////////////////////////////////////////////////////
1161
// API used by the GDB server
1162
 
1163
/* read a word from wishbone */
1164
int dbg_wb_read32(unsigned long adr, unsigned long *data) {
1165
  int err;
1166
  pthread_mutex_lock(&dbg_access_mutex);
1167
  if ((err = dbg_select_module(DC_WISHBONE)))
1168
    {
1169
      pthread_mutex_unlock(&dbg_access_mutex);
1170
      return err;
1171
    }
1172
  err = dbg_wb_burst_read(4, 1, adr, (void *)data); // All WB reads / writes are bursts
1173
  pthread_mutex_unlock(&dbg_access_mutex);
1174
  return err;
1175
}
1176
 
1177
/* write a word to wishbone */
1178
int dbg_wb_write32(unsigned long adr, unsigned long data) {
1179
  int err;
1180
  pthread_mutex_lock(&dbg_access_mutex);
1181
  if ((err = dbg_select_module(DC_WISHBONE)))
1182
    {
1183
      pthread_mutex_unlock(&dbg_access_mutex);
1184
      return err;
1185
    }
1186
  err = dbg_wb_burst_write((void *)&data, 4, 1, adr);
1187
  pthread_mutex_unlock(&dbg_access_mutex);
1188
  return err;
1189
}
1190
 
1191
// write a word to wishbone
1192
// Never actually called from the GDB interface
1193
int dbg_wb_write16(unsigned long adr, uint16_t data) {
1194
  int err;
1195
  pthread_mutex_lock(&dbg_access_mutex);
1196
  if ((err = dbg_select_module(DC_WISHBONE)))
1197
    {
1198
      pthread_mutex_unlock(&dbg_access_mutex);
1199
      return err;
1200
    }
1201
  err = dbg_wb_burst_write((void *)&data, 2, 1, adr);
1202
  pthread_mutex_unlock(&dbg_access_mutex);
1203
  return err;
1204
}
1205
 
1206
// write a word to wishbone
1207
// Never actually called from the GDB interface
1208
int dbg_wb_write8(unsigned long adr, uint8_t data) {
1209
  int err;
1210
  pthread_mutex_lock(&dbg_access_mutex);
1211
  if ((err = dbg_select_module(DC_WISHBONE)))
1212
    {
1213
      pthread_mutex_unlock(&dbg_access_mutex);
1214
      return err;
1215
    }
1216
  err = dbg_wb_burst_write((void *)&data, 1, 1, adr);
1217
  pthread_mutex_unlock(&dbg_access_mutex);
1218
  return err;
1219
}
1220
 
1221
 
1222
int dbg_wb_read_block32(unsigned long adr, unsigned long *data, int len) {
1223
  int err;
1224
  pthread_mutex_lock(&dbg_access_mutex);
1225
  if ((err = dbg_select_module(DC_WISHBONE)))
1226
    {
1227
      pthread_mutex_unlock(&dbg_access_mutex);
1228
      return err;
1229
    }
1230
  err = dbg_wb_burst_read(4, len, adr, (void *)data);  // 'len' is words.
1231
  pthread_mutex_unlock(&dbg_access_mutex);
1232
  return err;
1233
}
1234
 
1235
 
1236
// Never actually called from the GDB interface
1237
int dbg_wb_read_block16(unsigned long adr, uint16_t *data, int len) {
1238
  int err;
1239
  pthread_mutex_lock(&dbg_access_mutex);
1240
  if ((err = dbg_select_module(DC_WISHBONE)))
1241
    {
1242
      pthread_mutex_unlock(&dbg_access_mutex);
1243
      return err;
1244
    }
1245
  err = dbg_wb_burst_read(2, len, adr, (void *)data);  // *** is 'len' bits or words?? Call wants words...
1246
  pthread_mutex_unlock(&dbg_access_mutex);
1247
  return err;
1248
}
1249
 
1250
// Never actually called from the GDB interface
1251
int dbg_wb_read_block8(unsigned long adr, uint8_t *data, int len) {
1252
  int err;
1253
  pthread_mutex_lock(&dbg_access_mutex);
1254
  if ((err = dbg_select_module(DC_WISHBONE)))
1255
    {
1256
      pthread_mutex_unlock(&dbg_access_mutex);
1257
      return err;
1258
    }
1259
  err = dbg_wb_burst_read(1, len, adr, (void *)data);  // *** is 'len' bits or words?? Call wants words...
1260
  pthread_mutex_unlock(&dbg_access_mutex);
1261
  return err;
1262
}
1263
 
1264
 
1265
 
1266
// write a block to wishbone 
1267
int dbg_wb_write_block32(unsigned long adr, unsigned long *data, int len) {
1268
  int err;
1269
  pthread_mutex_lock(&dbg_access_mutex);
1270
  if ((err = dbg_select_module(DC_WISHBONE)))
1271
    {
1272
      pthread_mutex_unlock(&dbg_access_mutex);
1273
      return err;
1274
    }
1275
  err = dbg_wb_burst_write((void *)data, 4, len, adr);  // 'len' is words.
1276
  pthread_mutex_unlock(&dbg_access_mutex);
1277
  return err;
1278
}
1279
 
1280
 
1281
// write a block to wishbone
1282
// Never actually called from the GDB interface
1283
int dbg_wb_write_block16(unsigned long adr, uint16_t *data, int len) {
1284
  int err;
1285
  pthread_mutex_lock(&dbg_access_mutex);
1286
  if ((err = dbg_select_module(DC_WISHBONE)))
1287
    {
1288
      pthread_mutex_unlock(&dbg_access_mutex);
1289
      return err;
1290
    }
1291
  err = dbg_wb_burst_write((void *)data, 2, len, adr);  // *** is 'len' bits or words?? Call wants words...
1292
  pthread_mutex_unlock(&dbg_access_mutex);
1293
  return err;
1294
}
1295
 
1296
// write a block to wishbone
1297
int dbg_wb_write_block8(unsigned long adr, uint8_t *data, int len) {
1298
  int err;
1299
  pthread_mutex_lock(&dbg_access_mutex);
1300
  if ((err = dbg_select_module(DC_WISHBONE)))
1301
    {
1302
      pthread_mutex_unlock(&dbg_access_mutex);
1303
      return err;
1304
    }
1305
  err = dbg_wb_burst_write((void *)data, 1, len, adr);  // 'len' is in words...
1306
  pthread_mutex_unlock(&dbg_access_mutex);
1307
  return err;
1308
}
1309
 
1310
 
1311
/* read a register from cpu0.  This is assumed to be an OR32 CPU, with 32-bit regs. */
1312
int dbg_cpu0_read(unsigned long adr, unsigned long *data) {
1313
  int err;
1314
  pthread_mutex_lock(&dbg_access_mutex);
1315
  if ((err = dbg_select_module(DC_CPU0)))
1316
    {
1317
      pthread_mutex_unlock(&dbg_access_mutex);
1318
      return err;
1319
    }
1320
  err = dbg_wb_burst_read(4, 1, adr, (void *) data); // All CPU register reads / writes are bursts
1321
  pthread_mutex_unlock(&dbg_access_mutex);
1322
  debug("dbg_cpu_read(), addr 0x%X, data[0] = 0x%X\n", adr, data[0]);
1323
  return err;
1324
}
1325
 
1326
/* read multiple registers from cpu0.  This is assumed to be an OR32 CPU, with 32-bit regs. */
1327
int dbg_cpu0_read_block(unsigned long adr, unsigned long *data, int count) {
1328
  int err;
1329
  pthread_mutex_lock(&dbg_access_mutex);
1330
  if ((err = dbg_select_module(DC_CPU0)))
1331
    {
1332
      pthread_mutex_unlock(&dbg_access_mutex);
1333
      return err;
1334
    }
1335
  err = dbg_wb_burst_read(4, count, adr, (void *) data); // All CPU register reads / writes are bursts
1336
  pthread_mutex_unlock(&dbg_access_mutex);
1337
  debug("dbg_cpu_read_block(), addr 0x%X, count %i, data[0] = 0x%X\n", adr, count, data[0]);
1338
  return err;
1339
}
1340
 
1341
/* write a cpu register to cpu0.  This is assumed to be an OR32 CPU, with 32-bit regs. */
1342
int dbg_cpu0_write(unsigned long adr, unsigned long data) {
1343
  int err;
1344
  pthread_mutex_lock(&dbg_access_mutex);
1345
  if ((err = dbg_select_module(DC_CPU0)))
1346
    {
1347
      pthread_mutex_unlock(&dbg_access_mutex);
1348
      return err;
1349
    }
1350
  err = dbg_wb_burst_write((void *)&data, 4, 1, adr);
1351
  debug("cpu0_write, adr 0x%X, data 0x%X, ret %i\n", adr, data, err);
1352
  pthread_mutex_unlock(&dbg_access_mutex);
1353
  return err;
1354
}
1355
 
1356
/* write multiple cpu registers to cpu0.  This is assumed to be an OR32 CPU, with 32-bit regs. */
1357
int dbg_cpu0_write_block(unsigned long adr, unsigned long *data, int count) {
1358
  int err;
1359
  pthread_mutex_lock(&dbg_access_mutex);
1360
  if ((err = dbg_select_module(DC_CPU0)))
1361
    {
1362
      pthread_mutex_unlock(&dbg_access_mutex);
1363
      return err;
1364
    }
1365
  err = dbg_wb_burst_write((void *)data, 4, count, adr);
1366
  debug("cpu0_write_block, adr 0x%X, data[0] 0x%X, count %i, ret %i\n", adr, data[0], count, err);
1367
  pthread_mutex_unlock(&dbg_access_mutex);
1368
  return err;
1369
}
1370
 
1371
/* write a debug unit cpu module register
1372
 * Since OR32 debug module has only 1 register,
1373
 * adr is ignored (for now) */
1374
int dbg_cpu0_write_ctrl(unsigned long adr, unsigned char data) {
1375
  int err;
1376
  uint32_t dataword = data;
1377
  pthread_mutex_lock(&dbg_access_mutex);
1378
  if ((err = dbg_select_module(DC_CPU0))) {
1379
    printf("Failed to set chain to 0x%X\n", DC_CPU0);
1380
    pthread_mutex_unlock(&dbg_access_mutex);
1381
    return err;
1382
  }
1383
  if((err = dbg_ctrl_write(DBG_CPU0_REG_STATUS, &dataword, 2))) {
1384
    printf("Failed to write chain to 0x%X control reg 0x%X\n", DC_CPU0,DBG_CPU0_REG_STATUS );  // Only 2 bits: Reset, Stall
1385
    pthread_mutex_unlock(&dbg_access_mutex);
1386
    return err;
1387
  }
1388
  debug("cpu0_write_ctrl(): set reg to 0x%X\n", data);
1389
  pthread_mutex_unlock(&dbg_access_mutex);
1390
  return APP_ERR_NONE;
1391
}
1392
 
1393
 
1394
/* read a register from cpu module of the debug unit.
1395
 * Currently, there is only 1 register, so we do not need to select it, adr is ignored
1396
 */
1397
int dbg_cpu0_read_ctrl(unsigned long adr, unsigned char *data) {
1398
  int err;
1399
  uint32_t dataword;
1400
  pthread_mutex_lock(&dbg_access_mutex);
1401
  if ((err = dbg_select_module(DC_CPU0))) {
1402
    printf("Failed to set chain to 0x%X\n", DC_CPU0);
1403
    pthread_mutex_unlock(&dbg_access_mutex);
1404
    return err;
1405
  }
1406
  if ((err = dbg_ctrl_read(DBG_CPU0_REG_STATUS, &dataword, 2))) {
1407
    printf("Failed to read chain 0x%X control reg 0x%X\n", DC_CPU0, DBG_CPU0_REG_STATUS);
1408
    pthread_mutex_unlock(&dbg_access_mutex);
1409
    return err;
1410
  }
1411
  // reset is bit 1, stall is bit 0 in *data
1412
  pthread_mutex_unlock(&dbg_access_mutex);
1413
  *data = dataword;
1414
  return APP_ERR_NONE;
1415
}
1416
 
1417
// CPU1 Functions.  Note that 2 CPUs are not currently supported by GDB, so these are never actually
1418
// called from the GDB interface.  They are included for completeness and future use.
1419
// read a register from cpu1
1420
int dbg_cpu1_read(unsigned long adr, unsigned long *data)
1421
 {
1422
  int err;
1423
  pthread_mutex_lock(&dbg_access_mutex);
1424
  if ((err = dbg_select_module(DC_CPU1)))
1425
    {
1426
      pthread_mutex_unlock(&dbg_access_mutex);
1427
      return err;
1428
    }
1429
  err = dbg_wb_burst_read(4, 1, adr, (void *) data); // All CPU register reads / writes are bursts
1430
  pthread_mutex_unlock(&dbg_access_mutex);
1431
  return err;
1432
}
1433
 
1434
 
1435
// write a cpu register
1436
int dbg_cpu1_write(unsigned long adr, unsigned long data)
1437
{
1438
  int err;
1439
  pthread_mutex_lock(&dbg_access_mutex);
1440
  if ((err = dbg_select_module(DC_CPU0)))
1441
    {
1442
      pthread_mutex_unlock(&dbg_access_mutex);
1443
      return err;
1444
    }
1445
  err = dbg_wb_burst_write((void *)&data, 4, 1, adr);
1446
  pthread_mutex_unlock(&dbg_access_mutex);
1447
  return err;
1448
}
1449
 
1450
 
1451
// write a debug unit cpu module register
1452
int dbg_cpu1_write_ctrl(unsigned long adr, unsigned char data) {
1453
   int err;
1454
  uint32_t dataword = data;
1455
  pthread_mutex_lock(&dbg_access_mutex);
1456
  if ((err = dbg_select_module(DC_CPU1))) {
1457
    printf("Failed to set chain to 0x%X\n", DC_CPU1);
1458
    pthread_mutex_unlock(&dbg_access_mutex);
1459
    return err;
1460
  }
1461
  if((err = dbg_ctrl_write(DBG_CPU1_REG_STATUS, &dataword, 2))) {
1462
    printf("Failed to write chain to 0x%X control reg 0x%X\n", DC_CPU1,DBG_CPU0_REG_STATUS );  // Only 2 bits: Reset, Stall
1463
    pthread_mutex_unlock(&dbg_access_mutex);
1464
    return err;
1465
  }
1466
  pthread_mutex_unlock(&dbg_access_mutex);
1467
  return APP_ERR_NONE;
1468
}
1469
 
1470
 
1471
// read a debug unit cpu module register
1472
int dbg_cpu1_read_ctrl(unsigned long adr, unsigned char *data) {
1473
  int err;
1474
  uint32_t dataword;
1475
  pthread_mutex_lock(&dbg_access_mutex);
1476
  if ((err = dbg_select_module(DC_CPU1))) {
1477
    printf("Failed to set chain to 0x%X\n", DC_CPU1);
1478
    pthread_mutex_unlock(&dbg_access_mutex);
1479
    return err;
1480
  }
1481
  if ((err = dbg_ctrl_read(DBG_CPU1_REG_STATUS, &dataword, 2))) {
1482
    printf("Failed to read chain 0x%X control reg 0x%X\n", DC_CPU0, DBG_CPU1_REG_STATUS);
1483
    pthread_mutex_unlock(&dbg_access_mutex);
1484
    return err;
1485
  }
1486
  // reset is bit 1, stall is bit 0 in *data
1487
  pthread_mutex_unlock(&dbg_access_mutex);
1488
  *data = dataword;
1489
  return APP_ERR_NONE;
1490
}
1491
 
1492
 
1493
 

powered by: WebSVN 2.1.0

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