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 42

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 32 nyawn
   Copyright (C) 2008-2010 Nathan Yawn, nyawn@opencores.net
3 14 nyawn
 
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 42 nyawn
#include <string.h>  // for memcpy()
30 14 nyawn
 
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 42 nyawn
#include "utilities.h"
36 14 nyawn
 
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 42 nyawn
// Scratchpad I/O buffers
48
static char *input_scratchpad = NULL;
49
static char *output_scratchpad = NULL;
50
static int input_scratchpad_size = 0;
51
static int output_scratchpad_size = 0;
52
 
53 14 nyawn
// Prototypes for local functions
54
uint32_t adbg_compute_crc(uint32_t crc_in, uint32_t data_in, int length_bits);
55
 
56
 
57
 
58
////////////////////////////////////////////////////////////////////////
59
// Helper functions
60
 
61
uint32_t adbg_compute_crc(uint32_t crc_in, uint32_t data_in, int length_bits)
62
{
63
  int i;
64
  unsigned int d, c;
65
  uint32_t crc_out = crc_in;
66
 
67
  for(i = 0; i < length_bits; i = i+1)
68
    {
69
      d = ((data_in >> i) & 0x1) ? 0xffffffff : 0;
70
      c = (crc_out & 0x1) ? 0xffffffff : 0;
71
      crc_out = crc_out >> 1;
72
      crc_out = crc_out ^ ((d ^ c) & ADBG_CRC_POLY);
73
    }
74
  return crc_out;
75
}
76
 
77
//////////////////////////////////////////////////////////////////
78
// Functions which operate on the advanced debug unit
79
 
80
/* Selects one of the modules in the debug unit (e.g. wishbone unit, CPU0, etc.)
81
 */
82
int adbg_select_module(int chain)
83
{
84
  uint32_t data;
85
  int err = APP_ERR_NONE;
86
 
87
  if (current_chain == chain)
88
    return err;
89
 
90
  current_chain = -1;
91
  desired_chain = chain;
92
 
93
  // MSB of the data out must be set to 1, indicating a module select command
94
  data = chain | (1<<DBG_MODULE_SELECT_REG_SIZE);
95
 
96
  debug("select module %i\n", chain);
97
  err |= tap_set_shift_dr();    /* SHIFT_DR */
98
 
99
  /* write data, EXIT1_DR */
100
  err |= jtag_write_stream(&data, 3, 1);  // When TMS is set (last parameter), DR length is also adjusted; EXIT1_DR
101
 
102
  // *** If 'valid module selected' feedback is ever added, test it here
103
 
104
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
105
 
106
  current_chain = chain;
107
 
108
  if(err)
109
    printf("Error %s selecting active debug module\n", get_err_string(err));
110
 
111
  return err;
112
}
113
 
114
// Set the index of the desired register in the currently selected module
115
// 1 bit module select command
116
// 4 bits opcode
117
// n bits index
118
// Make sure the corrent module/chain is selected before calling this
119
int adbg_select_ctrl_reg(unsigned long regidx)
120
{
121
  uint32_t data;
122
  int index_len = 0;
123
  uint32_t opcode;
124
  int err = APP_ERR_NONE;
125
 
126
  if(err |= adbg_select_module(desired_chain))
127
    return err;
128
 
129
  debug("selreg %ld\n", regidx);
130
 
131
  // If this reg is already selected, don't do a JTAG transaction
132 32 nyawn
  if(current_reg_idx[current_chain] == regidx)
133 14 nyawn
    return APP_ERR_NONE;
134
 
135
  switch(current_chain) {
136
  case DC_WISHBONE:
137
    index_len = DBG_WB_REG_SEL_LEN;
138
    opcode = DBG_WB_CMD_IREG_SEL;
139
    break;
140
  case DC_CPU0:
141
    index_len = DBG_CPU0_REG_SEL_LEN;
142
    opcode = DBG_CPU0_CMD_IREG_SEL;
143
    break;
144
  case DC_CPU1:
145
    index_len = DBG_CPU1_REG_SEL_LEN;
146
    opcode = DBG_CPU1_CMD_IREG_SEL;
147
    break;
148
  default:
149
    printf("ERROR! Illegal debug chain selected while selecting control register!\n");
150
    return 1;
151
  }
152
 
153
 
154
  // Set up the data.
155
  data = (opcode & ~(1<<DBG_WB_OPCODE_LEN)) << index_len;  // MSB must be 0 to access modules
156
  data |= regidx;
157
 
158
  debug("Selreg: data is 0x%lX (opcode = 0x%lX)\n", data,opcode);
159
 
160
  err |= tap_set_shift_dr();  /* SHIFT_DR */
161
 
162
  /* write data, EXIT1_DR */
163
  err |= jtag_write_stream(&data, 5+index_len, 1);
164
 
165
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
166
 
167
  /* reset retry counter */
168
  retry_ok();
169
  current_reg_idx[current_chain] = regidx;
170
 
171
  if(err)
172
    printf("Error %s selecting control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
173
 
174
  return err;
175
}
176
 
177
 
178
/* Sends out a generic command to the selected debug unit module, LSB first.  Fields are:
179
 * MSB: 1-bit module command
180
 * 4-bit opcode
181
 * m-bit register index
182
 * n-bit data (LSB)
183
 * Note that in the data array, the LSB of data[0] will be sent first,
184
 * (and become the LSB of the command)
185
 * up through the MSB of data[0], then the LSB of data[1], etc.
186
 */
187
int adbg_ctrl_write(unsigned long regidx, uint32_t *cmd_data, int length_bits) {
188
  uint32_t data;
189
  int index_len = 0;
190
  uint32_t opcode;
191
  int err = APP_ERR_NONE;
192
 
193
  if(err |= adbg_select_module(desired_chain))
194
    return err;
195
 
196
  debug("ctrl wr idx %ld dat 0x%lX\n", regidx, cmd_data[0]);
197
 
198
  switch(current_chain) {
199
  case DC_WISHBONE:
200
    index_len = DBG_WB_REG_SEL_LEN;
201
    opcode = DBG_WB_CMD_IREG_WR;
202
    break;
203
  case DC_CPU0:
204
    index_len = DBG_CPU0_REG_SEL_LEN;
205
    opcode = DBG_CPU0_CMD_IREG_WR;
206
    break;
207
  case DC_CPU1:
208
    index_len = DBG_CPU1_REG_SEL_LEN;
209
    opcode = DBG_CPU1_CMD_IREG_WR;
210
    break;
211
  default:
212
    printf("ERROR! Illegal debug chain selected (%i) while doing control write!\n", current_chain);
213
    return 1;
214
  }
215
 
216
 
217
  // Set up the data.  We cheat a bit here, by using 2 stream writes.
218
  data = (opcode & ~(1<<DBG_WB_OPCODE_LEN)) << index_len;  // MSB must be 0 to access modules
219
  data |= regidx;
220
 
221
  err |= tap_set_shift_dr();  /* SHIFT_DR */
222
 
223
  /* write data, EXIT1_DR */
224
  err |= jtag_write_stream(cmd_data, length_bits, 0);
225
  err |= jtag_write_stream(&data, 5+index_len, 1);
226
 
227
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
228
 
229
  /* reset retry counter */
230
  retry_ok();
231
  current_reg_idx[current_chain] = regidx;
232
 
233
 if(err)
234
    printf("Error %s writing control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
235
 
236
  return err;
237
}
238
 
239
 
240
/* reads control register (internal to the debug unit)
241
 * Currently only 1 register in the CPU module, so no register select
242
 */
243
int adbg_ctrl_read(unsigned long regidx, uint32_t *data, int databits) {
244
  uint32_t outdata[4] = {0,0,0,0};  // *** We assume no more than 128 databits
245
  int opcode;
246
  int opcode_len;
247
  int err = APP_ERR_NONE;
248
 
249
  if(err |= adbg_select_module(desired_chain))
250
    return err;
251
 
252
  if(err |= adbg_select_ctrl_reg(regidx))
253
    return err;
254
 
255
  debug("ctrl rd idx %ld\n", regidx);
256
 
257
  // There is no 'read' command, We write a NOP to read
258
  switch(current_chain) {
259
  case DC_WISHBONE:
260
    opcode = DBG_WB_CMD_NOP;
261
    opcode_len = DBG_WB_OPCODE_LEN;
262
    break;
263
  case DC_CPU0:
264
    opcode = DBG_CPU0_CMD_NOP;
265
    opcode_len = DBG_CPU0_OPCODE_LEN;
266
    break;
267
  case DC_CPU1:
268
    opcode = DBG_CPU1_CMD_NOP;
269
    opcode_len = DBG_CPU1_OPCODE_LEN;
270
    break;
271
  default:
272
    printf("ERROR! Illegal debug chain selected while doing control read!\n");
273
    return 1;
274
  }
275
 
276
  outdata[0] = opcode & ~(0x1 << opcode_len);  // Zero MSB = op for module, not top-level debug unit
277
 
278
  err |= tap_set_shift_dr();  /* SHIFT_DR */
279
 
280
  // We cheat a bit here by using two stream operations.
281
  // First we burn the postfix bits and read the desired data, then we push a NOP
282
  // into position through the prefix bits.  We may be able to combine the two and save
283
  // some cycles, but that way leads to madness.
284
  err |= jtag_read_write_stream(outdata, data, databits, 1, 0);  // adjust for prefix bits
285
  err |= jtag_write_stream(outdata, opcode_len+1, 1);  // adjust for postfix bits, Set TMS: EXIT1_DR
286
 
287
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
288
 
289
  /* reset retry counter */
290
  retry_ok();
291
 
292
  if(err)
293
    printf("Error %s reading control register %ld in module %i\n", get_err_string(err), regidx, current_chain);
294
 
295
  return err;
296
}
297
 
298
 
299
/* sends out a burst command to the selected module in the debug unit (MSB to LSB):
300
 * 1-bit module command
301
 * 4-bit opcode
302
 * 32-bit address
303
 * 16-bit length (of the burst, in words)
304
 */
305
int adbg_burst_command(unsigned int opcode, unsigned long address, int length_words) {
306
  uint32_t data[2];
307
  int err = APP_ERR_NONE;
308
 
309
  if(err |= adbg_select_module(desired_chain))
310
    return err;
311
 
312
  debug("burst op %i adr 0x%lX len %i\n", opcode, address, length_words);
313
 
314
  // Set up the data
315
  data[0] = length_words | (address << 16);
316
  data[1] = ((address >> 16) | ((opcode & 0xf) << 16)) & ~(0x1<<20); // MSB must be 0 to access modules
317
 
318
  err |= tap_set_shift_dr();  /* SHIFT_DR */
319
 
320
  /* write data, EXIT1_DR */
321
  err |= jtag_write_stream(data, 53, 1);  // When TMS is set (last parameter), DR length is also adjusted; EXIT1_DR
322
 
323
  err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
324
 
325
  /* reset retry counter */
326
  retry_ok();
327
 
328
  if(err)
329
    printf("Error %s sending burst command to module %i\n", get_err_string(err), desired_chain);
330
 
331
  return err;
332
}
333
 
334
// Set up and execute a burst read from a contiguous block of addresses.
335
// Note that there is a minor weakness in the CRC algorithm in case of retries:
336
// the CRC is only checked for the final burst read.  Thus, if errors/partial retries
337
// break up a transfer into multiple bursts, only the last burst will be CRC protected.
338
#define MAX_BUS_ERRORS 10
339
int adbg_wb_burst_read(int word_size_bytes, int word_count, unsigned long start_address, void *data)
340
{
341
  unsigned char opcode;
342
  uint8_t status;
343
  unsigned long instream;
344
  int i, j;
345
  uint32_t crc_calc;
346
  uint32_t crc_read;
347
  unsigned char word_size_bits;
348
  uint32_t out_data = 0;
349
  uint32_t in_data;
350
  unsigned long addr;
351
  uint32_t err_data[2];
352
  int bus_error_retries = 0;
353
  int err = APP_ERR_NONE;
354
 
355
    debug("Doing burst read, word size %d, word count %d, start address 0x%lX", word_size_bytes, word_count, start_address);
356
 
357
    if(word_count <= 0) {
358
      debug("Ignoring illegal read burst length (%d)\n", word_count);
359
      return 0;
360
    }
361
 
362
    instream = 0;
363
    word_size_bits = word_size_bytes << 3;
364
 
365
    // Select the appropriate opcode
366
    switch(current_chain) {
367
    case DC_WISHBONE:
368
      if (word_size_bytes == 1) opcode = DBG_WB_CMD_BREAD8;
369
      else if(word_size_bytes == 2) opcode = DBG_WB_CMD_BREAD16;
370
      else if(word_size_bytes == 4) opcode = DBG_WB_CMD_BREAD32;
371
      else {
372
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
373
        opcode = DBG_WB_CMD_BREAD32;
374
      }
375
      break;
376
    case DC_CPU0:
377
      if(word_size_bytes == 4) opcode = DBG_CPU0_CMD_BREAD32;
378
      else {
379
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
380
        opcode = DBG_CPU0_CMD_BREAD32;
381
      }
382
      break;
383
    case DC_CPU1:
384
      if(word_size_bytes == 4) opcode = DBG_CPU1_CMD_BREAD32;
385
      else {
386
        printf("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
387
        opcode = DBG_CPU0_CMD_BREAD32;
388
      }
389
      break;
390
    default:
391
      printf("ERROR! Illegal debug chain selected while doing burst read!\n");
392
      return 1;
393
    }
394
 
395
 wb_burst_read_retry_full:
396
    i = 0;
397
    addr = start_address;
398
 wb_burst_read_retry_partial:
399
    crc_calc = 0xffffffff;
400
 
401
 
402
    // Send the BURST READ command, returns TAP to idle state
403
    if(err |= adbg_burst_command(opcode, addr, (word_count-i)))  // word_count-i in case of partial retry 
404
      return err;
405
 
406 32 nyawn
    // This is a kludge to work around oddities in the Xilinx BSCAN_* devices, and the
407 14 nyawn
    // adv_dbg_if state machine.  The debug FSM needs 1 TCK between UPDATE_DR above, and
408
    // the CAPTURE_DR below, and the BSCAN_* won't provide it.  So, we force it, by putting the TAP
409
    // in BYPASS, which makes the debug_select line inactive, which is AND'ed with the TCK line (in the xilinx_internal_jtag module),
410
    // which forces it low.  Then we re-enable USER1/debug_select to make TCK high.  One TCK
411
    // event, the hard way. 
412
    if(global_xilinx_bscan) {
413
      err |= tap_set_ir(0xFFFFFFFF);
414
      err |= tap_enable_debug_module();
415
    }
416
 
417
    // Get us back to shift_dr mode to read a burst
418
    err |=  tap_set_shift_dr();
419
 
420
    // We do not adjust for the DR length here.  BYPASS regs are loaded with 0,
421
    // and the debug unit waits for a '1' status bit before beginning to read data.
422 32 nyawn
 
423
#ifdef ADBG_OPT_HISPEED
424
       // Get 1 status bit, then word_size_bytes*8 bits
425
       status = 0;
426
       j = 0;
427
       while(!status) {  // Status indicates whether there is a word available to read.  Wait until it returns true.
428
         err |= jtag_read_write_bit(0, &status);
429
         j++;
430
         // If max count exceeded, retry
431
         if(j > MAX_READ_STATUS_WAIT) {
432
           printf("Burst read timed out.\n");
433
           if(!retry_do()) {
434
             printf("Retry count exceeded in burst read!\n");
435
             return err|APP_ERR_MAX_RETRY;
436
           }
437
           err = APP_ERR_NONE;  // on retry, errors cleared
438
           goto wb_burst_read_retry_full;
439
         }
440
       }
441
 
442
       // Check we have enough space for the (zero) output data
443 42 nyawn
       int total_size_bytes = (word_count*word_size_bytes)+4;
444
       err |= check_buffer_size(&input_scratchpad, &input_scratchpad_size, total_size_bytes);
445
       err |= check_buffer_size(&output_scratchpad, &output_scratchpad_size, total_size_bytes);
446
       if(err != APP_ERR_NONE) return err;
447
       memset(output_scratchpad, 0, total_size_bytes);
448 32 nyawn
 
449 42 nyawn
       // Get the data in one shot, including the CRC.  This requires two memcpy(), which take time,
450
       // but it's still faster than an added USB transaction (assuming a USB cable).
451
       err |= jtag_read_write_stream((uint32_t *)output_scratchpad, (uint32_t *)input_scratchpad, (total_size_bytes*8), 0, 1);
452
       memcpy(data, input_scratchpad, (word_count*word_size_bytes));
453
       memcpy(&crc_read, &input_scratchpad[(word_count*word_size_bytes)], 4);
454 32 nyawn
       for(i = 0; i < (word_count*word_size_bytes); i++)
455
         {
456
           crc_calc = adbg_compute_crc(crc_calc, ((uint8_t *)data)[i], 8);
457
         }
458 42 nyawn
 
459 32 nyawn
#else
460
 
461 14 nyawn
   // Repeat for each word: wait until ready = 1, then read word_size_bits bits.
462
   for(; i < word_count; i++)
463
     {
464
       // Get 1 status bit, then word_size_bytes*8 bits
465
       status = 0;
466
       j = 0;
467
       while(!status) {  // Status indicates whether there is a word available to read.  Wait until it returns true.
468
         err |= jtag_read_write_bit(0, &status);
469
         j++;
470
         // If max count exceeded, retry starting with the failure address
471
         if(j > MAX_READ_STATUS_WAIT) {
472
           printf("Burst read timed out.\n");
473
           if(!retry_do()) {
474
             printf("Retry count exceeded in burst read!\n");
475
             return err|APP_ERR_MAX_RETRY;
476
           }
477
           err = APP_ERR_NONE;  // on retry, errors cleared
478
           addr = start_address + (i*word_size_bytes);
479
           goto wb_burst_read_retry_partial;
480
         }
481
       }
482
 
483
       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
484
         debug("Took %0d tries before good status bit during burst read", j);
485
       }
486
 
487
       // Get one word of data
488
       err |= jtag_read_write_stream(&out_data, &in_data, word_size_bits, 0, 0);
489
       debug("Read 0x%0lx", in_data);
490
 
491
       if(err) {  // Break and retry as soon as possible on error
492
         printf("Error %s during burst read.\n", get_err_string(err));
493
           if(!retry_do()) {
494
             printf("Retry count exceeded in burst read!\n");
495
             return err|APP_ERR_MAX_RETRY;
496
           }
497
           err = APP_ERR_NONE;  // on retry, errors cleared
498
           addr = start_address + (i*word_size_bytes);
499
           goto wb_burst_read_retry_partial;
500
       }
501
 
502
       crc_calc = adbg_compute_crc(crc_calc, in_data, word_size_bits);
503
 
504
       if(word_size_bytes == 1) ((unsigned char *)data)[i] = in_data & 0xFF;
505
       else if(word_size_bytes == 2) ((unsigned short *)data)[i] = in_data & 0xFFFF;
506
       else ((unsigned long *)data)[i] = in_data;
507
     }
508
 
509
   // All bus data was read.  Read the data CRC from the debug module.
510
   err |= jtag_read_write_stream(&out_data, &crc_read, 32, 0, 1);
511
 
512 42 nyawn
#endif
513
 
514 14 nyawn
   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 32 nyawn
#ifndef ADBG_OPT_HISPEED
566
  uint8_t status;
567
  uint32_t statuswords[2] = {0,0};
568
  int first_status_loop = 1;
569
#endif
570 14 nyawn
  int err = APP_ERR_NONE;
571
 
572 32 nyawn
    debug("Doing burst write, word size %d, word count %d, start address 0x%lx\n", word_size_bytes, word_count, start_address);
573 14 nyawn
    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 32 nyawn
#ifndef ADBG_OPT_HISPEED
611 14 nyawn
    // 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 32 nyawn
#endif
614 14 nyawn
 
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 32 nyawn
#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 42 nyawn
   // We send the CRC at the same time, so we have to compute it first.
637
   for(loopct = 0; loopct < word_count; loopct++) {
638
       if(word_size_bytes == 4)       datawords[0] = ((unsigned long *)data)[loopct];
639
       else if(word_size_bytes == 2) datawords[0] = ((unsigned short *)data)[loopct];
640
       else                          datawords[0] = ((unsigned char *)data)[loopct];
641
       crc_calc = adbg_compute_crc(crc_calc, datawords[0], word_size_bits);
642
   }
643 32 nyawn
 
644 42 nyawn
   int total_size_bytes = (word_count * word_size_bytes) + 4;
645
   err |= check_buffer_size(&output_scratchpad, &output_scratchpad_size, total_size_bytes);
646
   if(err != APP_ERR_NONE) return err;
647
 
648
   memcpy(output_scratchpad, data, (word_count * word_size_bytes));
649
   memcpy(&output_scratchpad[(word_count*word_size_bytes)], &crc_calc, 4);
650
 
651
   err |= jtag_write_stream((uint32_t *) output_scratchpad, total_size_bytes*8, 0);  // Write data
652
 
653
#else
654
 
655
   // Or, repeat...
656 14 nyawn
   for(loopct = 0; i < word_count; i++,loopct++)  // loopct only used to check status... 
657
     {
658
       // Write word_size_bytes*8 bits, then get 1 status bit
659
       if(word_size_bytes == 4)       datawords[0] = ((unsigned long *)data)[i];
660
       else if(word_size_bytes == 2) datawords[0] = ((unsigned short *)data)[i];
661
       else                          datawords[0] = ((unsigned char *)data)[i];
662
 
663
       crc_calc = adbg_compute_crc(crc_calc, datawords[0], word_size_bits);
664
 
665
       // This is an optimization
666
       if((global_DR_prefix_bits + global_DR_postfix_bits) == 0) {
667 32 nyawn
         //#endif
668 14 nyawn
         err |= jtag_write_stream(datawords, word_size_bits, 0);  // Write data
669 32 nyawn
         //#ifndef ADBG_OPT_HISPEED
670 14 nyawn
         err |= jtag_read_write_bit(0, &status);  // Read status bit
671
         if(!status) {
672
           addr = start_address + (i*word_size_bytes);
673
           printf("Write before bus ready, retrying (idx %i, addr 0x%08lX).\n", i, addr);
674
           if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
675
           // Don't bother going to TAP idle state, we're about to reset the TAP
676
           goto wb_burst_write_retry_partial;
677
         }
678
       }
679
       else {  // This is slower (for a USB cable anyway), because a read takes 1 more USB transaction than a write.
680
         err |= jtag_read_write_stream(datawords, statuswords, word_size_bits+1, 0, 0);
681
         debug("St. 0x%08lX 0x%08lX\n", statuswords[0], statuswords[1]);
682
         status = (statuswords[0] || statuswords[1]);
683
         if(loopct > first_status_loop) {
684
           if(status) successes++;
685
           else {
686
             i = successes;
687
             addr = start_address + (i*word_size_bytes);
688
             printf("Write before bus ready, retrying (idx %i, addr 0x%08lX).\n", i, addr);
689
             if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
690
             // Don't bother going to TAP idle state, we're about to reset the TAP
691
             goto wb_burst_write_retry_partial;
692
           }
693
         }
694
       }
695
 
696
       if(err) {
697 32 nyawn
         printf("Error %s during burst write, retrying.\n", get_err_string(err));
698 14 nyawn
         if(!retry_do()) {
699
           printf("Retry count exceeded!\n");
700
           return err|APP_ERR_MAX_RETRY;
701
         }
702
         err = APP_ERR_NONE;
703
         addr = start_address + (i*word_size_bytes);
704
         // Don't bother going to TAP idle state, we're about to reset the TAP
705
         goto wb_burst_write_retry_partial;
706
         }
707
 
708
      debug("Wrote 0x%0lx", datawords[0]);
709
     }
710
 
711 32 nyawn
   // *** If this is a multi-device chain (and we're not in hi-speed mode), at least one status bit will be lost.
712 14 nyawn
   // *** If we want to check for it, we'd have to look while sending the CRC, and
713
   // *** maybe while burning bits to get the match bit.  So, for now, there is a
714
   // *** hole here.
715
 
716
   // Done sending data, Send the CRC we computed
717
   err |= jtag_write_stream(&crc_calc, 32, 0);
718 42 nyawn
 
719
#endif
720
 
721 14 nyawn
   for(i = 0; i < global_DR_prefix_bits; i++)  // Push the CRC data all the way to the debug unit
722
     err |= jtag_write_bit(0);                 // Can't do this with a stream command without setting TMS on the last bit
723
 
724
   // Read the 'CRC match' bit, and go to exit1_dr
725
   // May need to adjust for other devices in chain!
726
   datawords[0] = 0;
727
   err |= jtag_read_write_stream(datawords, &crc_match, 1, 1, 0);  // set 'adjust' to pull match bit all the way in
728
   // But don't set TMS above, that would shift prefix bits (again), wasting time.
729
   err |= jtag_write_bit(TMS);  // exit1_dr
730
   err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
731
 
732
   if(!crc_match) {
733
     printf("CRC ERROR! match bit after write is %i (computed CRC 0x%x)", crc_match, crc_calc);
734
     if(!retry_do()) { printf("Retry count exceeded!  Abort!\n\n"); exit(1);}
735
     goto  wb_burst_write_retry_full;
736
   }
737
   else debug("CRC OK!");
738
 
739
 
740
   // Now, read the error register and retry/recompute as needed
741
   if (current_chain == DC_WISHBONE)
742
     {
743
       err |= adbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 1);  // First, just get 1 bit...read address only if necessary
744
       if(err_data[0] & 0x1) {  // Then we have a problem.
745
         err |= adbg_ctrl_read(DBG_WB_REG_ERROR, err_data, 33);
746
         addr = (err_data[0] >> 1) | (err_data[1] << 31);
747
         i = (addr - start_address) / word_size_bytes;
748
         printf("ERROR!  WB bus error during burst write, address 0x%lX (index 0x%X), retrying!\n", addr, i);
749
         bus_error_retries++;
750
         if(bus_error_retries > MAX_BUS_ERRORS) {
751
           printf("Max WB bus errors reached!\n");
752
           return err|APP_ERR_MAX_BUS_ERR;
753
         }
754
         // Don't call retry_do(), a JTAG reset won't help a WB bus error
755
         err |= adbg_ctrl_write(DBG_WB_REG_ERROR, err_data, 1);  // Write 1 bit, to reset the error register.
756
         goto wb_burst_write_retry_partial;
757
       }
758
     }
759
 
760
   retry_ok();
761
   return err;
762
}
763 42 nyawn
 
764
/*--------------------------------------------------------------------------------------------*/
765
 
766
// This does a simultaneous read/write to the JTAG serial port.  It will send/receive at most 8 bytes,
767
// so data_received must be at least 8 bytes long.  It is not guaranteed that all data (or any data)
768
// will be sent.  On return, bytes_to_send will be set to the number of bytes actually senet.
769
 
770
int adbg_jsp_transact(unsigned int *bytes_to_send, const char *data_to_send, unsigned int *bytes_received, char *data_received)
771
{
772
  int err = APP_ERR_NONE;
773
  unsigned int xmitsize;
774
  char outdata[10];  // These must 10 bytes: 1 for counts, 8 for data, and 1 (possible) start  and end bits
775
  char indata[10];
776
  unsigned char stopbit = 0, startbit = 0, wrapbit;
777
  int bytes_free;
778
  int i;
779
 
780
  if(*bytes_to_send > 8)
781
    xmitsize = 8;
782
  else
783
    xmitsize = *bytes_to_send;
784
 
785
  if(err |= adbg_select_module(desired_chain))
786
    return err;
787
 
788
    // Put us in shift_dr mode
789
    err |=  tap_set_shift_dr();
790
 
791
    // There are two independant compile-time options here, making four different ways to do this transaction.
792
    // If OPTIMIZE_FOR_USB is not defined, then one byte will be transacted to get the 'bytes available' and
793
    // 'bytes free' counts, then the minimum number of bytes will be transacted to get all available bytes
794
    // and put as many bytes as possible.  If OPTIMIZE_FOR_USB is defined, then 9 bytes will always be transacted,
795
    // the JSP will ignore extras, and user code will have to check to see  how many bytes were written.
796
    //
797
    // if ENABLE_JSP_MULTI is enabled, then a '1' bit will be pre-pended to the data being sent (before the 'count'
798
    // byte).  This is for compatibility with multi-device JTAG chains.
799
 
800
#ifdef OPTIMIZE_JSP_FOR_USB
801
 
802
    // Simplest case: do everything in 1 burst transaction
803
    memset(outdata, 0, 10);  // Clear to act as 'stopbits'.  [8] may be overwritten in the following memcpy().
804
 
805
 #ifdef ENABLE_JSP_MULTI
806
 
807
    startbit = 1;
808
    wrapbit = (xmitsize >> 3) & 0x1;
809
    outdata[0] = (xmitsize << 5) | 0x1;  // set the start bit
810
 
811
    for(i = 0; i < xmitsize; i++)  // don't copy off the end of the input array
812
      {
813
        outdata[i+1] = (data_to_send[i] << 1) | wrapbit;
814
        wrapbit = (data_to_send[i] >> 7) & 0x1;
815
      }
816
 
817
    if(i < 8)
818
      outdata[i+1] = wrapbit;
819
    else
820
      outdata[9] = wrapbit;
821
 
822
    // If the last data bit is a '1', then we need to append a '0' so the top-level module
823
    // won't treat the burst as a 'module select' command.
824
    if(outdata[9] & 0x01) stopbit = 1;
825
    else                  stopbit = 0;
826
 
827
 #else
828
 
829
    startbit = 0;
830
    outdata[0] = 0x0 | (xmitsize << 4);  // First byte out has write count in upper nibble
831
    if (xmitsize > 0) memcpy(&outdata[1], data_to_send, xmitsize);
832
 
833
    // If the last data bit is a '1', then we need to append a '0' so the top-level module
834
    // won't treat the burst as a 'module select' command.
835
    if(outdata[8] & 0x80) stopbit = 1;
836
    else                  stopbit = 0;
837
 
838
 #endif
839
 
840
    debug("jsp doing 9 bytes, xmitsize %i\n", xmitsize);
841
 
842
    // 72 bits: 9 bytes * 8 bits
843
    err |= jtag_read_write_stream((uint32_t *) outdata, (uint32_t *) indata, 72+startbit+stopbit, 1, 1);
844
 
845
    debug("jsp got remote sizes 0x%X\n", indata[0]);
846
 
847
    *bytes_received = (indata[0] >> 4) & 0xF;  // bytes available is in the upper nibble
848
    memcpy(data_received, &indata[1], *bytes_received);
849
 
850
    bytes_free = indata[0] & 0x0F;
851
    *bytes_to_send = (bytes_free < xmitsize) ? bytes_free : xmitsize;
852
 
853
#else  // !OPTIMIZE_JSP_FOR_USB
854
 
855
 #ifdef ENABLE_JSP_MULTI
856
    indata[0] = indata[1] = 0;
857
    outdata[1] = (xmitsize >> 3) & 0x1;
858
    outdata[0] = (xmitsize << 5) | 0x1;  // set the start bit
859
    startbit = 1;
860
 
861
 #else
862
    outdata[0] = 0x0 | (xmitsize << 4);  // First byte out has write count in upper nibble
863
    startbit = 0;
864
 #endif
865
 
866
    err |= jtag_read_write_stream((uint32_t *) outdata, (uint32_t *) indata, 8+startbit, 1, 0);
867
 
868
    wrapbit = indata[1] & 0x1;  // only used if ENABLE_JSP_MULTI is defined
869
    bytes_free = indata[0] & 0x0F;
870
    *bytes_received = (indata[0] >> 4) & 0xF;  // bytes available is in the upper nibble
871
 
872
    // Number of bytes to transact is max(bytes_available, min(bytes_to_send,bytes_free))
873
    if(bytes_free < xmitsize) xmitsize = bytes_free;
874
    if((*bytes_received) > xmitsize) xmitsize = *bytes_received;
875
 
876
    memset(outdata, 0, 10);
877
    memcpy(outdata, data_to_send, xmitsize);  // use larger array in case we need to send stopbit
878
 
879
    // If the last data bit is a '1', then we need to append a '0' so the top-level module
880
    // won't treat the burst as a 'module select' command.
881
    if(xmitsize && (outdata[xmitsize - 1] & 0x80)) stopbit = 2;
882
    else                             stopbit = 1;
883
 
884
    err |= jtag_read_write_stream((uint32_t *) outdata, (uint32_t *) indata, (xmitsize*8)+stopbit, 0, 1);
885
 
886
 #ifdef ENABLE_JSP_MULTI
887
 
888
    for(i = 0; i < (*bytes_received); i++)
889
      {
890
        data_received[i] = (indata[i] << 1) | wrapbit;
891
        wrapbit = (indata[i] >> 7) & 0x1;
892
      }
893
 #else
894
    memcpy(data_received, indata, xmitsize);
895
 #endif
896
 
897
    if(bytes_free < *bytes_to_send) *bytes_to_send = bytes_free;
898
 
899
#endif  // !OPTIMIZE_JSP_FOR_USB
900
 
901
   err |= tap_exit_to_idle();  // Go from EXIT1 to IDLE
902
 
903
   return err;
904
}

powered by: WebSVN 2.1.0

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