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

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Software/] [adv_jtag_bridge/] [adv_dbg_commands.c] - Blame information for rev 14

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

Line No. Rev Author Line
1 14 nyawn
/* adv_dbg_commands.c -- JTAG protocol bridge between GDB and Advanced debug module.
2
   Copyright(C) Nathan Yawn, nyawn@opencores.net
3
 
4
   This file contains functions which perform high-level transactions
5
   on a JTAG chain and debug unit, such as setting a value in the TAP IR
6
   or doing a burst write through the wishbone module of the debug unit.
7
   It uses the protocol for the Advanced Debug Interface (adv_dbg_if).
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 exit()
29
//#include <pthread.h>  // for mutexes
30
 
31
#include "chain_commands.h"
32
#include "adv_dbg_commands.h"     // hardware-specific defines for the debug module
33
//#include "altera_virtual_jtag.h"  // hardware-specifg defines for the Altera Virtual JTAG interface
34
#include "cable_common.h"         // low-level JTAG IO routines
35
#include "errcodes.h"
36
 
37
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
38
 
39
// How many '0' status bits to get during a burst read
40
// before giving up
41
#define MAX_READ_STATUS_WAIT 100
42
 
43
// Currently selected internal register in each module
44
// - cuts down on unnecessary transfers
45
int current_reg_idx[DBG_MAX_MODULES];
46
 
47
// Prototypes for local functions
48
uint32_t adbg_compute_crc(uint32_t crc_in, uint32_t data_in, int length_bits);
49
 
50
 
51
 
52
////////////////////////////////////////////////////////////////////////
53
// Helper functions
54
 
55
uint32_t adbg_compute_crc(uint32_t crc_in, uint32_t data_in, int length_bits)
56
{
57
  int i;
58
  unsigned int d, c;
59
  uint32_t crc_out = crc_in;
60
 
61
  for(i = 0; i < length_bits; i = i+1)
62
    {
63
      d = ((data_in >> i) & 0x1) ? 0xffffffff : 0;
64
      c = (crc_out & 0x1) ? 0xffffffff : 0;
65
      crc_out = crc_out >> 1;
66
      crc_out = crc_out ^ ((d ^ c) & ADBG_CRC_POLY);
67
    }
68
  return crc_out;
69
}
70
 
71
//////////////////////////////////////////////////////////////////
72
// Functions which operate on the advanced debug unit
73
 
74
/* Selects one of the modules in the debug unit (e.g. wishbone unit, CPU0, etc.)
75
 */
76
int adbg_select_module(int chain)
77
{
78
  uint32_t data;
79
  int err = APP_ERR_NONE;
80
 
81
  if (current_chain == chain)
82
    return err;
83
 
84
  current_chain = -1;
85
  desired_chain = chain;
86
 
87
  // MSB of the data out must be set to 1, indicating a module select command
88
  data = chain | (1<<DBG_MODULE_SELECT_REG_SIZE);
89
 
90
  debug("select module %i\n", chain);
91
  err |= tap_set_shift_dr();    /* SHIFT_DR */
92
 
93
  /* write data, EXIT1_DR */
94
  err |= jtag_write_stream(&data, 3, 1);  // When TMS is set (last parameter), DR length is also adjusted; EXIT1_DR
95
 
96
  // *** If 'valid module selected' feedback is ever added, test it here
97
 
98
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
99
 
100
  current_chain = chain;
101
 
102
  if(err)
103
    printf("Error %s selecting active debug module\n", get_err_string(err));
104
 
105
  return err;
106
}
107
 
108
// Set the index of the desired register in the currently selected module
109
// 1 bit module select command
110
// 4 bits opcode
111
// n bits index
112
// Make sure the corrent module/chain is selected before calling this
113
int adbg_select_ctrl_reg(unsigned long regidx)
114
{
115
  uint32_t data;
116
  int index_len = 0;
117
  uint32_t opcode;
118
  int err = APP_ERR_NONE;
119
 
120
  if(err |= adbg_select_module(desired_chain))
121
    return err;
122
 
123
  debug("selreg %ld\n", regidx);
124
 
125
  // If this reg is already selected, don't do a JTAG transaction
126
  if(current_reg_idx[current_chain] == regidx)
127
    return APP_ERR_NONE;
128
 
129
  switch(current_chain) {
130
  case DC_WISHBONE:
131
    index_len = DBG_WB_REG_SEL_LEN;
132
    opcode = DBG_WB_CMD_IREG_SEL;
133
    break;
134
  case DC_CPU0:
135
    index_len = DBG_CPU0_REG_SEL_LEN;
136
    opcode = DBG_CPU0_CMD_IREG_SEL;
137
    break;
138
  case DC_CPU1:
139
    index_len = DBG_CPU1_REG_SEL_LEN;
140
    opcode = DBG_CPU1_CMD_IREG_SEL;
141
    break;
142
  default:
143
    printf("ERROR! Illegal debug chain selected while selecting control register!\n");
144
    return 1;
145
  }
146
 
147
 
148
  // Set up the data.
149
  data = (opcode & ~(1<<DBG_WB_OPCODE_LEN)) << index_len;  // MSB must be 0 to access modules
150
  data |= regidx;
151
 
152
  debug("Selreg: data is 0x%lX (opcode = 0x%lX)\n", data,opcode);
153
 
154
  err |= tap_set_shift_dr();  /* SHIFT_DR */
155
 
156
  /* write data, EXIT1_DR */
157
  err |= jtag_write_stream(&data, 5+index_len, 1);
158
 
159
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
160
 
161
  /* reset retry counter */
162
  retry_ok();
163
  current_reg_idx[current_chain] = regidx;
164
 
165
  if(err)
166
    printf("Error %s selecting control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
167
 
168
  return err;
169
}
170
 
171
 
172
/* Sends out a generic command to the selected debug unit module, LSB first.  Fields are:
173
 * MSB: 1-bit module command
174
 * 4-bit opcode
175
 * m-bit register index
176
 * n-bit data (LSB)
177
 * Note that in the data array, the LSB of data[0] will be sent first,
178
 * (and become the LSB of the command)
179
 * up through the MSB of data[0], then the LSB of data[1], etc.
180
 */
181
int adbg_ctrl_write(unsigned long regidx, uint32_t *cmd_data, int length_bits) {
182
  uint32_t data;
183
  int index_len = 0;
184
  uint32_t opcode;
185
  int err = APP_ERR_NONE;
186
 
187
  if(err |= adbg_select_module(desired_chain))
188
    return err;
189
 
190
  debug("ctrl wr idx %ld dat 0x%lX\n", regidx, cmd_data[0]);
191
 
192
  switch(current_chain) {
193
  case DC_WISHBONE:
194
    index_len = DBG_WB_REG_SEL_LEN;
195
    opcode = DBG_WB_CMD_IREG_WR;
196
    break;
197
  case DC_CPU0:
198
    index_len = DBG_CPU0_REG_SEL_LEN;
199
    opcode = DBG_CPU0_CMD_IREG_WR;
200
    break;
201
  case DC_CPU1:
202
    index_len = DBG_CPU1_REG_SEL_LEN;
203
    opcode = DBG_CPU1_CMD_IREG_WR;
204
    break;
205
  default:
206
    printf("ERROR! Illegal debug chain selected (%i) while doing control write!\n", current_chain);
207
    return 1;
208
  }
209
 
210
 
211
  // Set up the data.  We cheat a bit here, by using 2 stream writes.
212
  data = (opcode & ~(1<<DBG_WB_OPCODE_LEN)) << index_len;  // MSB must be 0 to access modules
213
  data |= regidx;
214
 
215
  err |= tap_set_shift_dr();  /* SHIFT_DR */
216
 
217
  /* write data, EXIT1_DR */
218
  err |= jtag_write_stream(cmd_data, length_bits, 0);
219
  err |= jtag_write_stream(&data, 5+index_len, 1);
220
 
221
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
222
 
223
  /* reset retry counter */
224
  retry_ok();
225
  current_reg_idx[current_chain] = regidx;
226
 
227
 if(err)
228
    printf("Error %s writing control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
229
 
230
  return err;
231
}
232
 
233
 
234
/* reads control register (internal to the debug unit)
235
 * Currently only 1 register in the CPU module, so no register select
236
 */
237
int adbg_ctrl_read(unsigned long regidx, uint32_t *data, int databits) {
238
  uint32_t outdata[4] = {0,0,0,0};  // *** We assume no more than 128 databits
239
  int opcode;
240
  int opcode_len;
241
  int err = APP_ERR_NONE;
242
 
243
  if(err |= adbg_select_module(desired_chain))
244
    return err;
245
 
246
  if(err |= adbg_select_ctrl_reg(regidx))
247
    return err;
248
 
249
  debug("ctrl rd idx %ld\n", regidx);
250
 
251
  // There is no 'read' command, We write a NOP to read
252
  switch(current_chain) {
253
  case DC_WISHBONE:
254
    opcode = DBG_WB_CMD_NOP;
255
    opcode_len = DBG_WB_OPCODE_LEN;
256
    break;
257
  case DC_CPU0:
258
    opcode = DBG_CPU0_CMD_NOP;
259
    opcode_len = DBG_CPU0_OPCODE_LEN;
260
    break;
261
  case DC_CPU1:
262
    opcode = DBG_CPU1_CMD_NOP;
263
    opcode_len = DBG_CPU1_OPCODE_LEN;
264
    break;
265
  default:
266
    printf("ERROR! Illegal debug chain selected while doing control read!\n");
267
    return 1;
268
  }
269
 
270
  outdata[0] = opcode & ~(0x1 << opcode_len);  // Zero MSB = op for module, not top-level debug unit
271
 
272
  err |= tap_set_shift_dr();  /* SHIFT_DR */
273
 
274
  // We cheat a bit here by using two stream operations.
275
  // First we burn the postfix bits and read the desired data, then we push a NOP
276
  // into position through the prefix bits.  We may be able to combine the two and save
277
  // some cycles, but that way leads to madness.
278
  err |= jtag_read_write_stream(outdata, data, databits, 1, 0);  // adjust for prefix bits
279
  err |= jtag_write_stream(outdata, opcode_len+1, 1);  // adjust for postfix bits, Set TMS: EXIT1_DR
280
 
281
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
282
 
283
  /* reset retry counter */
284
  retry_ok();
285
 
286
  if(err)
287
    printf("Error %s reading control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
288
 
289
  return err;
290
}
291
 
292
 
293
/* sends out a burst command to the selected module in the debug unit (MSB to LSB):
294
 * 1-bit module command
295
 * 4-bit opcode
296
 * 32-bit address
297
 * 16-bit length (of the burst, in words)
298
 */
299
int adbg_burst_command(unsigned int opcode, unsigned long address, int length_words) {
300
  uint32_t data[2];
301
  int err = APP_ERR_NONE;
302
 
303
  if(err |= adbg_select_module(desired_chain))
304
    return err;
305
 
306
  debug("burst op %i adr 0x%lX len %i\n", opcode, address, length_words);
307
 
308
  // Set up the data
309
  data[0] = length_words | (address << 16);
310
  data[1] = ((address >> 16) | ((opcode & 0xf) << 16)) & ~(0x1<<20); // MSB must be 0 to access modules
311
 
312
  err |= tap_set_shift_dr();  /* SHIFT_DR */
313
 
314
  /* write data, EXIT1_DR */
315
  err |= jtag_write_stream(data, 53, 1);  // When TMS is set (last parameter), DR length is also adjusted; EXIT1_DR
316
 
317
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
318
 
319
  /* reset retry counter */
320
  retry_ok();
321
 
322
  if(err)
323
    printf("Error %s sending burst command to module %i\n", get_err_string(err), desired_chain);
324
 
325
  return err;
326
}
327
 
328
// Set up and execute a burst read from a contiguous block of addresses.
329
// Note that there is a minor weakness in the CRC algorithm in case of retries:
330
// the CRC is only checked for the final burst read.  Thus, if errors/partial retries
331
// break up a transfer into multiple bursts, only the last burst will be CRC protected.
332
#define MAX_BUS_ERRORS 10
333
int adbg_wb_burst_read(int word_size_bytes, int word_count, unsigned long start_address, void *data)
334
{
335
  unsigned char opcode;
336
  uint8_t status;
337
  unsigned long instream;
338
  int i, j;
339
  uint32_t crc_calc;
340
  uint32_t crc_read;
341
  unsigned char word_size_bits;
342
  uint32_t out_data = 0;
343
  uint32_t in_data;
344
  unsigned long addr;
345
  uint32_t err_data[2];
346
  int bus_error_retries = 0;
347
  int err = APP_ERR_NONE;
348
 
349
    debug("Doing burst read, word size %d, word count %d, start address 0x%lX", word_size_bytes, word_count, start_address);
350
 
351
    if(word_count <= 0) {
352
      debug("Ignoring illegal read burst length (%d)\n", word_count);
353
      return 0;
354
    }
355
 
356
    instream = 0;
357
    word_size_bits = word_size_bytes << 3;
358
 
359
    // Select the appropriate opcode
360
    switch(current_chain) {
361
    case DC_WISHBONE:
362
      if (word_size_bytes == 1) opcode = DBG_WB_CMD_BREAD8;
363
      else if(word_size_bytes == 2) opcode = DBG_WB_CMD_BREAD16;
364
      else if(word_size_bytes == 4) opcode = DBG_WB_CMD_BREAD32;
365
      else {
366
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
367
        opcode = DBG_WB_CMD_BREAD32;
368
      }
369
      break;
370
    case DC_CPU0:
371
      if(word_size_bytes == 4) opcode = DBG_CPU0_CMD_BREAD32;
372
      else {
373
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
374
        opcode = DBG_CPU0_CMD_BREAD32;
375
      }
376
      break;
377
    case DC_CPU1:
378
      if(word_size_bytes == 4) opcode = DBG_CPU1_CMD_BREAD32;
379
      else {
380
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
381
        opcode = DBG_CPU0_CMD_BREAD32;
382
      }
383
      break;
384
    default:
385
      printf("ERROR! Illegal debug chain selected while doing burst read!\n");
386
      return 1;
387
    }
388
 
389
 wb_burst_read_retry_full:
390
    i = 0;
391
    addr = start_address;
392
 wb_burst_read_retry_partial:
393
    crc_calc = 0xffffffff;
394
 
395
 
396
    // Send the BURST READ command, returns TAP to idle state
397
    if(err |= adbg_burst_command(opcode, addr, (word_count-i)))  // word_count-i in case of partial retry 
398
      return err;
399
 
400
    // This is a kludge to word around oddities in the Xilinx BSCAN_* devices, and the
401
    // adv_dbg_if state machine.  The debug FSM needs 1 TCK between UPDATE_DR above, and
402
    // the CAPTURE_DR below, and the BSCAN_* won't provide it.  So, we force it, by putting the TAP
403
    // in BYPASS, which makes the debug_select line inactive, which is AND'ed with the TCK line (in the xilinx_internal_jtag module),
404
    // which forces it low.  Then we re-enable USER1/debug_select to make TCK high.  One TCK
405
    // event, the hard way. 
406
    if(global_xilinx_bscan) {
407
      err |= tap_set_ir(0xFFFFFFFF);
408
      err |= tap_enable_debug_module();
409
    }
410
 
411
    // Get us back to shift_dr mode to read a burst
412
    err |=  tap_set_shift_dr();
413
 
414
    // We do not adjust for the DR length here.  BYPASS regs are loaded with 0,
415
    // and the debug unit waits for a '1' status bit before beginning to read data.
416
 
417
   // Repeat for each word: wait until ready = 1, then read word_size_bits bits.
418
   for(; i < word_count; i++)
419
     {
420
       // Get 1 status bit, then word_size_bytes*8 bits
421
       status = 0;
422
       j = 0;
423
       while(!status) {  // Status indicates whether there is a word available to read.  Wait until it returns true.
424
         err |= jtag_read_write_bit(0, &status);
425
         j++;
426
         // If max count exceeded, retry starting with the failure address
427
         if(j > MAX_READ_STATUS_WAIT) {
428
           printf("Burst read timed out.\n");
429
           if(!retry_do()) {
430
             printf("Retry count exceeded in burst read!\n");
431
             return err|APP_ERR_MAX_RETRY;
432
           }
433
           err = APP_ERR_NONE;  // on retry, errors cleared
434
           addr = start_address + (i*word_size_bytes);
435
           goto wb_burst_read_retry_partial;
436
         }
437
       }
438
 
439
       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
440
         debug("Took %0d tries before good status bit during burst read", j);
441
       }
442
 
443
       // Get one word of data
444
       err |= jtag_read_write_stream(&out_data, &in_data, word_size_bits, 0, 0);
445
       debug("Read 0x%0lx", in_data);
446
 
447
       if(err) {  // Break and retry as soon as possible on error
448
         printf("Error %s during burst read.\n", get_err_string(err));
449
           if(!retry_do()) {
450
             printf("Retry count exceeded in burst read!\n");
451
             return err|APP_ERR_MAX_RETRY;
452
           }
453
           err = APP_ERR_NONE;  // on retry, errors cleared
454
           addr = start_address + (i*word_size_bytes);
455
           goto wb_burst_read_retry_partial;
456
       }
457
 
458
       crc_calc = adbg_compute_crc(crc_calc, in_data, word_size_bits);
459
 
460
       if(word_size_bytes == 1) ((unsigned char *)data)[i] = in_data & 0xFF;
461
       else if(word_size_bytes == 2) ((unsigned short *)data)[i] = in_data & 0xFFFF;
462
       else ((unsigned long *)data)[i] = in_data;
463
     }
464
 
465
   // All bus data was read.  Read the data CRC from the debug module.
466
   err |= jtag_read_write_stream(&out_data, &crc_read, 32, 0, 1);
467
 
468
   err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
469
 
470
   if(crc_calc != crc_read) {
471
     printf("CRC ERROR! Computed 0x%x, read CRC 0x%x\n", crc_calc, crc_read);
472
     if(!retry_do()) {
473
       printf("Retry count exceeded!  Abort!\n\n");
474
       return err|APP_ERR_CRC;
475
     }
476
     goto  wb_burst_read_retry_full;
477
   }
478
   else debug("CRC OK!");
479
 
480
 
481
 
482
   // Now, read the error register, and retry/recompute as necessary.
483
   if(current_chain == DC_WISHBONE)
484
     {
485
       err |= adbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 1);  // First, just get 1 bit...read address only if necessary,
486
       if(err_data[0] & 0x1) {  // Then we have a problem.
487
         err |= adbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 33);
488
         addr = (err_data[0] >> 1) | (err_data[1] << 31);
489
         i = (addr - start_address) / word_size_bytes;
490
         printf("ERROR!  WB bus error during burst read, address 0x%lX (index 0x%X), retrying!\n", addr, i);
491
         bus_error_retries++;
492
         if(bus_error_retries > MAX_BUS_ERRORS) {
493
           printf("Max WB bus errors reached during burst read\n");
494
           return err|APP_ERR_MAX_BUS_ERR;
495
         }
496
         // Don't call retry_do(), a JTAG reset won't help a WB bus error
497
         err_data[0] = 1;
498
         err |= adbg_ctrl_write(DBG_WB_REG_ERROR, err_data, 1);  // Write 1 bit, to reset the error register,
499
         goto wb_burst_read_retry_partial;
500
       }
501
     }
502
 
503
   retry_ok();
504
   return err;
505
}
506
 
507
// Set up and execute a burst write to a contiguous set of addresses
508
int adbg_wb_burst_write(void *data, int word_size_bytes, int word_count, unsigned long start_address)
509
{
510
  unsigned char opcode;
511
  uint8_t status;
512
  uint32_t datawords[2] = {0,0};
513
  uint32_t statuswords[2] = {0,0};
514
  int i;
515
  uint32_t crc_calc;
516
  uint32_t crc_match;
517
  unsigned int word_size_bits;
518
  unsigned long addr;
519
  int bus_error_retries = 0;
520
  uint32_t err_data[2];
521
  int loopct, successes;
522
  int first_status_loop;
523
  int err = APP_ERR_NONE;
524
 
525
    debug("Doing burst write, word size %d, word count %d, start address 0x%lx", word_size_bytes, word_count, start_address);
526
    word_size_bits = word_size_bytes << 3;
527
 
528
    if(word_count <= 0) {
529
      printf("Ignoring illegal burst write size (%d)\n", word_count);
530
      return 0;
531
    }
532
 
533
    // Select the appropriate opcode
534
    switch(current_chain) {
535
    case DC_WISHBONE:
536
      if (word_size_bytes == 1) opcode = DBG_WB_CMD_BWRITE8;
537
      else if(word_size_bytes == 2) opcode = DBG_WB_CMD_BWRITE16;
538
      else if(word_size_bytes == 4) opcode = DBG_WB_CMD_BWRITE32;
539
      else {
540
        printf("Tried WB burst write with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
541
        opcode = DBG_WB_CMD_BWRITE32;
542
      }
543
      break;
544
    case DC_CPU0:
545
      if(word_size_bytes == 4) opcode = DBG_CPU0_CMD_BWRITE32;
546
      else {
547
        printf("Tried CPU0 burst write with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
548
        opcode = DBG_CPU0_CMD_BWRITE32;
549
      }
550
      break;
551
    case DC_CPU1:
552
      if(word_size_bytes == 4) opcode = DBG_CPU1_CMD_BWRITE32;
553
      else {
554
        printf("Tried CPU1 burst write with invalid word size (%0X), defaulting to 4-byte words", word_size_bytes);
555
        opcode = DBG_CPU0_CMD_BWRITE32;
556
      }
557
      break;
558
    default:
559
      printf("ERROR! Illegal debug chain selected while doing burst WRITE!\n");
560
      return 1;
561
    }
562
 
563
    // Compute which loop iteration in which to expect the first status bit
564
    first_status_loop = 1 + ((global_DR_prefix_bits + global_DR_postfix_bits)/(word_size_bits+1));
565
 
566
 wb_burst_write_retry_full:
567
    i = 0;
568
    addr = start_address;
569
 wb_burst_write_retry_partial:
570
    crc_calc = 0xffffffff;
571
    successes = 0;
572
 
573
 
574
    // Send burst command, return to idle state
575
    if(err |= adbg_burst_command(opcode, addr, (word_count-i)))  // word_count-i in case of partial retry
576
      return err;
577
 
578
   // Get us back to shift_dr mode to write a burst
579
   //err |= jtag_write_bit(TMS);  // select_dr_scan
580
   //err |= jtag_write_bit(0);           // capture_ir
581
   //err |= jtag_write_bit(0);           // shift_ir
582
   err |= tap_set_shift_dr();
583
 
584
   // Write a start bit (a 1) so it knows when to start counting
585
   err |= jtag_write_bit(TDO);
586
 
587
   // Now, repeat...
588
   for(loopct = 0; i < word_count; i++,loopct++)  // loopct only used to check status... 
589
     {
590
       // Write word_size_bytes*8 bits, then get 1 status bit
591
       if(word_size_bytes == 4)       datawords[0] = ((unsigned long *)data)[i];
592
       else if(word_size_bytes == 2) datawords[0] = ((unsigned short *)data)[i];
593
       else                          datawords[0] = ((unsigned char *)data)[i];
594
 
595
       crc_calc = adbg_compute_crc(crc_calc, datawords[0], word_size_bits);
596
 
597
       // This is an optimization
598
       if((global_DR_prefix_bits + global_DR_postfix_bits) == 0) {
599
         err |= jtag_write_stream(datawords, word_size_bits, 0);  // Write data
600
         err |= jtag_read_write_bit(0, &status);  // Read status bit
601
         if(!status) {
602
           addr = start_address + (i*word_size_bytes);
603
           printf("Write before bus ready, retrying (idx %i, addr 0x%08lX).\n", i, addr);
604
           if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
605
           // Don't bother going to TAP idle state, we're about to reset the TAP
606
           goto wb_burst_write_retry_partial;
607
         }
608
       }
609
       else {  // This is slower (for a USB cable anyway), because a read takes 1 more USB transaction than a write.
610
         err |= jtag_read_write_stream(datawords, statuswords, word_size_bits+1, 0, 0);
611
         debug("St. 0x%08lX 0x%08lX\n", statuswords[0], statuswords[1]);
612
         status = (statuswords[0] || statuswords[1]);
613
         if(loopct > first_status_loop) {
614
           if(status) successes++;
615
           else {
616
             i = successes;
617
             addr = start_address + (i*word_size_bytes);
618
             printf("Write before bus ready, retrying (idx %i, addr 0x%08lX).\n", i, addr);
619
             if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
620
             // Don't bother going to TAP idle state, we're about to reset the TAP
621
             goto wb_burst_write_retry_partial;
622
           }
623
         }
624
       }
625
 
626
       if(err) {
627
         printf("Error %s getting status bit, retrying.\n", get_err_string(err));
628
         if(!retry_do()) {
629
           printf("Retry count exceeded!\n");
630
           return err|APP_ERR_MAX_RETRY;
631
         }
632
         err = APP_ERR_NONE;
633
         addr = start_address + (i*word_size_bytes);
634
         // Don't bother going to TAP idle state, we're about to reset the TAP
635
         goto wb_burst_write_retry_partial;
636
         }
637
 
638
      debug("Wrote 0x%0lx", datawords[0]);
639
     }
640
 
641
   // *** If this is a multi-device chain, at least one status bit will be lost.
642
   // *** If we want to check for it, we'd have to look while sending the CRC, and
643
   // *** maybe while burning bits to get the match bit.  So, for now, there is a
644
   // *** hole here.
645
 
646
   // Done sending data, Send the CRC we computed
647
   err |= jtag_write_stream(&crc_calc, 32, 0);
648
   for(i = 0; i < global_DR_prefix_bits; i++)  // Push the CRC data all the way to the debug unit
649
     err |= jtag_write_bit(0);                 // Can't do this with a stream command without setting TMS on the last bit
650
 
651
   // Read the 'CRC match' bit, and go to exit1_dr
652
   // May need to adjust for other devices in chain!
653
   datawords[0] = 0;
654
   err |= jtag_read_write_stream(datawords, &crc_match, 1, 1, 0);  // set 'adjust' to pull match bit all the way in
655
   // But don't set TMS above, that would shift prefix bits (again), wasting time.
656
   err |= jtag_write_bit(TMS);  // exit1_dr
657
   err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
658
 
659
   if(!crc_match) {
660
     printf("CRC ERROR! match bit after write is %i (computed CRC 0x%x)", crc_match, crc_calc);
661
     if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
662
     goto  wb_burst_write_retry_full;
663
   }
664
   else debug("CRC OK!");
665
 
666
 
667
   // Now, read the error register and retry/recompute as needed
668
   if (current_chain == DC_WISHBONE)
669
     {
670
       err |= adbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 1);  // First, just get 1 bit...read address only if necessary
671
       if(err_data[0] & 0x1) {  // Then we have a problem.
672
         err |= adbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 33);
673
         addr = (err_data[0] >> 1) | (err_data[1] << 31);
674
         i = (addr - start_address) / word_size_bytes;
675
         printf("ERROR!  WB bus error during burst write, address 0x%lX (index 0x%X), retrying!\n", addr, i);
676
         bus_error_retries++;
677
         if(bus_error_retries > MAX_BUS_ERRORS) {
678
           printf("Max WB bus errors reached!\n");
679
           return err|APP_ERR_MAX_BUS_ERR;
680
         }
681
         // Don't call retry_do(), a JTAG reset won't help a WB bus error
682
         err |= adbg_ctrl_write(DBG_WB_REG_ERROR, err_data, 1);  // Write 1 bit, to reset the error register.
683
         goto wb_burst_write_retry_partial;
684
       }
685
     }
686
 
687
   retry_ok();
688
   return err;
689
}

powered by: WebSVN 2.1.0

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