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/] [adv_dbg_commands.c] - Blame information for rev 21

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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