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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [jtag.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* Remote debugging interface for JTAG debugging protocol.
2
   JTAG connects to or1k target ops. See or1k-tdep.c
3
 
4
   Copyright 1993-1995, 2000 Free Software Foundation, Inc.
5
   Contributed by Cygnus Support.  Written by Marko Mlinar
6
   <markom@opencores.org>
7
   Areas noted by (CZ) were modified by Chris Ziomkowski
8
   <chris@asics.ws>
9
 
10
   This file is part of GDB.
11
 
12
   This program is free software; you can redistribute it and/or modify
13
   it under the terms of the GNU General Public License as published by
14
   the Free Software Foundation; either version 2 of the License, or
15
   (at your option) any later version.
16
 
17
   This program is distributed in the hope that it will be useful,
18
   but WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
   GNU General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, write to the Free Software
24
   Foundation, Inc., 59 Temple Place - Suite 330,
25
   Boston, MA 02111-1307, USA.  */
26
 
27
#include "defs.h"
28
#include "inferior.h"
29
#include "bfd.h"
30
#include "symfile.h"
31
#include "gdb_wait.h"
32
#include "gdbcmd.h"
33
#include "gdbcore.h"
34
#include "serial.h"
35
#include "target.h"
36
#include "remote-utils.h"
37
#include "gdb_string.h"
38
#include "tm.h"
39
/* Added by CZ 24/05/01 */
40
#include <sys/poll.h>
41
#include <sys/socket.h>
42
#include <netdb.h>
43
#include <netinet/in.h>
44
#include <netinet/tcp.h>
45
#include <sys/select.h>
46
#include <sys/time.h>
47
#include <unistd.h>
48
 
49
#include <signal.h>
50
#include <sys/types.h>
51
#include <sys/stat.h>
52
#include <sys/ioctl.h>
53
#include <fcntl.h>
54
 
55
#define TCLK (0x01)
56
#define TRST (0x02)
57
#define JTAG_WAIT()
58
#define NUM_RETRIES (16)
59
#define JTAG_RETRY_WAIT() usleep (100)
60
 
61
/* Selects crc trailer size in bits. Currently supported: 8 */
62
#define CRC_SIZE (8)
63
 
64
/* Scan chain size in bits.  */
65
#define SC_SIZE (4)
66
 
67
/* Scan chain info. */
68
/* *INDENT-OFF* */
69
static int chain_addr_size[] = { 0,  32, 0,  0,  5  };
70
static int chain_data_size[] = { 0,  32, 0,  32, 32 };
71
static int chain_is_valid[]  = { 0,  1,  0,  1,  1  };
72
static int chain_has_crc[]   = { 0,  1,  0,  1,  1  };
73
static int chain_has_rw[]    = { 0,  1,  0,  0,  1  };
74
/* *INDENT-OFF* */
75
 
76
/* Currently selected scan chain - just to prevent unnecessary
77
   transfers. */
78
static int current_chain;
79
 
80
/* Designates whether we are in SELECT_DR state, otherwise in
81
   RUN TEST/IDLE */
82
static int select_dr = 0;
83
 
84
/* CZ - 24/05/01 - Changed to structure for remote/local */
85
typedef enum {
86
  JTAG_NOT_CONNECTED = 0,
87
  JTAG_LOCAL = 1,
88
  JTAG_REMOTE = 2,
89
} jtag_location;
90
 
91
typedef struct {
92
  union {
93
    int lp; /* Printer compatible device we have open.  */
94
    int fd; /* Socket for remote or1k jtag interface */
95
  } device;
96
  jtag_location location;
97
} jtag_connection;
98
 
99
/* Our current connect information */
100
static jtag_connection connection;
101
 
102
/* Crc of current read or written data.  */
103
static int crc_r, crc_w = 0;
104
 
105
/* Generates new crc, sending in new bit input_bit */
106
 
107
static int
108
crc_calc (int crc, int input_bit)
109
{
110
  int c;
111
  int new_crc;
112
  int d;
113
 
114
#if (CRC_SIZE == 8)
115
  d = input_bit&1;
116
  c = crc;
117
 
118
  /* Move queue left.  */
119
  new_crc = crc << 1;
120
 
121
  /* Mask upper five bits.  */
122
  new_crc &= 0xF8;
123
 
124
  /* Set lower three bits */
125
  new_crc |= (d ^ ((c >> 7)&1));
126
  new_crc |= (d ^ ((c >> 0)&1) ^ ((c >> 7)&1)) << 1;
127
  new_crc |= (d ^ ((c >> 1)&1) ^ ((c >> 7)&1)) << 2;
128
  return new_crc;
129
#else
130
  return 0;
131
#endif
132
}
133
 
134
/* Resets JTAG.
135
   Writes TRST=0
136
   and    TRST=1 */
137
 
138
static void
139
jp1_reset_JTAG ()
140
{
141
  unsigned char data;
142
  int lp = connection.device.lp;         /* CZ */
143
 
144
  if(connection.location != JTAG_LOCAL)
145
    error("jp1_reset_JTAG called without a local connection!");
146
 
147
  data = 0;
148
  write (lp, &data, sizeof (data));
149
  JTAG_WAIT();
150
  data = TRST;
151
  write (lp, &data, sizeof (data));
152
  JTAG_WAIT();
153
}
154
 
155
/* Writes TCLK=0, TRST=1, TMS=bit0, TDI=bit1
156
   and    TCLK=1, TRST=1, TMS=bit0, TDI=bit1 */
157
 
158
static void
159
jp1_write_JTAG (packet)
160
     unsigned char packet;
161
{
162
  unsigned char data;
163
  int lp = connection.device.lp;    /* CZ */
164
 
165
  if(connection.location != JTAG_LOCAL)
166
    error("jp1_write_JTAG called without a local connection!");
167
 
168
  data = ((packet & 3) << 2) | TRST;
169
  write (lp, &data, sizeof (data));
170
  JTAG_WAIT();
171
  crc_w = crc_calc (crc_w, (packet >> 1)&1);
172
 
173
  /* rise clock */
174
  data |= TCLK;
175
  write (lp, &data, sizeof (data));
176
  JTAG_WAIT();
177
}
178
 
179
/* Reads TDO, using IOCTL.  */
180
 
181
static int
182
jp1_read_JTAG ()
183
{
184
  int data;
185
 
186
  if(connection.location != JTAG_LOCAL)    /* CZ */
187
    error("jp1_read_JTAG called without a local connection!");
188
 
189
  ioctl (data, 0x60b, &data);
190
  data = ((data & 0x80) != 0);
191
  crc_r = crc_calc (crc_r, data);
192
  return data;
193
}
194
 
195
/* Writes bitstream.  MS bit first.  */
196
 
197
static void
198
jp1_write_stream (stream, len, set_last_bit)
199
     ULONGEST stream;
200
     int len;
201
     int set_last_bit;
202
{
203
  int i;
204
  if (len <= 0) return;
205
  for (i = len - 1; i > 0; i--)
206
    jp1_write_JTAG (((stream >> i) & 1) << 1);
207
 
208
  if (set_last_bit)
209
    jp1_write_JTAG (((stream & 1) << 1) | 1);
210
  else
211
    jp1_write_JTAG ((stream & 1) << 1);
212
}
213
 
214
/* Gets bitstream.  MS bit first.  */
215
 
216
static ULONGEST
217
jp1_read_stream (len, stream, set_last_bit)
218
     int len;
219
     unsigned long stream;
220
     int set_last_bit;
221
{
222
  int i;
223
  ULONGEST data;
224
 
225
  if (len <= 0) return;
226
  data = 0;
227
  for (i = 0; i < len-1; i++)
228
    {
229
      jp1_write_JTAG (0 + ((stream & 1) << 1));
230
      stream >>= 1;
231
      data <<= 1;
232
      data |= jp1_read_JTAG ();
233
    }
234
 
235
  if (set_last_bit)
236
    jp1_write_JTAG (1 + (stream & 1) << 1);
237
  else
238
    jp1_write_JTAG (0 + (stream & 1) << 1);
239
  data <<= 1;
240
  data |= jp1_read_JTAG ();
241
  return data;
242
}
243
 
244
/* Goes into SELECT_IR state. Should be called before every control write.  */
245
 
246
static void
247
jp1_prepare_control ()
248
{
249
  if (!select_dr)
250
    jp1_write_JTAG (1); /* SELECT_DR SCAN */
251
  jp1_write_JTAG (1);   /* SELECT_IR SCAN */
252
  select_dr = 0;
253
}
254
 
255
/* Added by CZ 24/05/01 */
256
static int jtag_proxy_write(int fd,void* buf,int len)
257
{
258
  int n;
259
  char* w_buf = (char*)buf;
260
  struct pollfd block;
261
 
262
  while(len)
263
    {
264
      if((n = write(fd,w_buf,len)) < 0)
265
        {
266
          switch(errno)
267
            {
268
            case EWOULDBLOCK: /* or EAGAIN */
269
              /* We've been called on a descriptor marked
270
                 for nonblocking I/O. We better simulate
271
                 blocking behavior. */
272
              block.fd = fd;
273
              block.events = POLLOUT;
274
              block.revents = 0;
275
              poll(&block,1,-1);
276
              continue;
277
            case EINTR:
278
              continue;
279
            case EPIPE:
280
              return JTAG_PROXY_SERVER_TERMINATED;
281
            default:
282
              return errno;
283
            }
284
        }
285
      else
286
        {
287
          len -= n;
288
          w_buf += n;
289
        }
290
    }
291
  return 0;
292
}
293
 
294
/* Added by CZ 24/05/01 */
295
static int jtag_proxy_read(int fd,void* buf,int len)
296
{
297
  int n;
298
  char* r_buf = (char*)buf;
299
  struct pollfd block;
300
 
301
  while(len)
302
    {
303
      if((n = read(fd,r_buf,len)) < 0)
304
        {
305
          switch(errno)
306
            {
307
            case EWOULDBLOCK: /* or EAGAIN */
308
              /* We've been called on a descriptor marked
309
                 for nonblocking I/O. We better simulate
310
                 blocking behavior. */
311
              block.fd = fd;
312
              block.events = POLLIN;
313
              block.revents = 0;
314
              poll(&block,1,-1);
315
              continue;
316
            case EINTR:
317
              continue;
318
            default:
319
              return errno;
320
            }
321
        }
322
      else if(n == 0)
323
        return JTAG_PROXY_SERVER_TERMINATED;
324
      else
325
        {
326
          len -= n;
327
          r_buf += n;
328
        }
329
    }
330
  return 0;
331
}
332
 
333
static int ReadResponse(int fd,void* buffer,int len)
334
{
335
  int32_t status = 0;
336
  int result;
337
  char* buf = (char*)buffer;
338
 
339
        result = jtag_proxy_read(fd,&status,4);
340
 
341
  status = ntohl(status);
342
  *((int32_t*)buffer) = status;
343
 
344
  if(result)    {printf("result %i %x\n", result, result); return result;}
345
  if(status)    {printf("status %i %x\n", status, status); return status;}
346
 
347
  result = jtag_proxy_read(fd,&buf[4],len-4);
348
  return result;
349
}
350
 
351
/* Added by CZ 24/05/01 */
352
#ifndef ANSI_PROTOTYPES
353
static int jtag_send_proxy(va_alist)
354
va_dcl
355
#else
356
static int jtag_send_proxy(int command,...)
357
#endif
358
{
359
  va_list ap;
360
  int result = 0;
361
  int fd = connection.device.fd;
362
  JTAGProxyWriteMessage xmit_write;
363
  JTAGProxyReadMessage xmit_read;
364
  JTAGProxyChainMessage xmit_chain;
365
  JTAGProxyBlockWriteMessage *xmit_bwrite;
366
  JTAGProxyBlockReadMessage xmit_bread;
367
  JTAGProxyWriteResponse recv_write;
368
  JTAGProxyReadResponse recv_read;
369
  JTAGProxyChainResponse recv_chain;
370
  JTAGProxyBlockWriteResponse recv_bwrite;
371
  JTAGProxyBlockReadResponse recv_bread;
372
  unsigned long long data,* ret_val;
373
  uint32_t word1; /* Use word1 and word2 to ease portability to platforms */
374
  uint32_t word2; /* without long long and for alignment reasons */
375
  unsigned long  address;
376
  unsigned long* b_data;
377
  int nRegisters;
378
  int len,i;
379
 
380
#ifndef ANSI_PROTOTYPES
381
  int command;
382
 
383
  va_start(ap);
384
  command = va_arg(ap,int);
385
#else
386
  va_start(ap, command);
387
#endif
388
 
389
 
390
 
391
  if(connection.location == JTAG_REMOTE)
392
    {
393
      switch(command)
394
        {
395
        case JTAG_COMMAND_READ:
396
          xmit_read.command = htonl(command);
397
          xmit_read.length = htonl(sizeof(xmit_read)-8);
398
          xmit_read.address = htonl(va_arg(ap,int));
399
          ret_val = va_arg(ap,unsigned long long *);
400
          /* intentional single equals */
401
          if(result = jtag_proxy_write(fd,&xmit_read,sizeof(xmit_read)))
402
            break;
403
          if(result = ReadResponse(fd,&recv_read,sizeof(recv_read)))
404
            break;
405
          result = ntohl(recv_read.status);
406
          word1 = ntohl(recv_read.data_H);
407
          word2 = ntohl(recv_read.data_L);
408
          *ret_val = (((unsigned long long)word1) << 32) | word2;
409
          break;
410
        case JTAG_COMMAND_WRITE:
411
          xmit_write.command = htonl(command);
412
          xmit_write.length = htonl(sizeof(xmit_write)-8);
413
          xmit_write.address = htonl(va_arg(ap,int));
414
          data = va_arg(ap,unsigned long long);
415
          word1 = htonl(data >> 32);
416
          word2 = htonl(data & 0xFFFFFFFF);
417
          xmit_write.data_H = word1;
418
          xmit_write.data_L = word2;
419
          if(result = jtag_proxy_write(fd,&xmit_write,sizeof(xmit_write)))
420
            break;
421
          if(result = ReadResponse(fd,&recv_write,sizeof(recv_write)))
422
            break;
423
          result = recv_write.status;
424
          break;
425
        case JTAG_COMMAND_BLOCK_WRITE:
426
          address = va_arg(ap,unsigned long);
427
          b_data = va_arg(ap,unsigned long*);
428
          nRegisters = va_arg(ap,int);
429
          len = sizeof(JTAGProxyBlockWriteMessage) + (nRegisters-1)*4;
430
          xmit_bwrite = (JTAGProxyBlockWriteMessage*)malloc(len);
431
          xmit_bwrite->command = htonl(command);
432
          xmit_bwrite->length = htonl(len-8);
433
          xmit_bwrite->address = htonl(address);
434
          xmit_bwrite->nRegisters = htonl(nRegisters);
435
          for(i=0;i<nRegisters;i++)
436
            xmit_bwrite->data[i] = htonl(b_data[i]);
437
          if(!(result = jtag_proxy_write(fd,xmit_bwrite,len)) &&
438
             !(result = ReadResponse(fd,&recv_bwrite,sizeof(recv_bwrite))))
439
            result = recv_bwrite.status;
440
          free(xmit_bwrite);
441
          xmit_bwrite = NULL;
442
          break;
443
        case JTAG_COMMAND_BLOCK_READ:
444
          address = va_arg(ap,unsigned long);
445
          b_data = va_arg(ap,unsigned long*);
446
          nRegisters = va_arg(ap,int);
447
          xmit_bread.command = htonl(command);
448
          xmit_bread.length = htonl(sizeof(xmit_bread)-8);
449
          xmit_bread.address = htonl(address);
450
          xmit_bread.nRegisters = htonl(nRegisters);
451
          if(result = jtag_proxy_write(fd,&xmit_bread,sizeof(xmit_bread)))
452
            break;
453
          if(result = ReadResponse(fd,&recv_bread,sizeof(recv_bread)))
454
            break;
455
          b_data[0] = ntohl(recv_bread.data[0]);
456
          recv_bread.nRegisters = ntohl(recv_bread.nRegisters);
457
          if(result = jtag_proxy_read(fd,&b_data[1],4*(recv_bread.nRegisters-1)))
458
            break;
459
          if(recv_bread.nRegisters != nRegisters)
460
            result = JTAG_PROXY_PROTOCOL_ERROR;
461
          else
462
            {
463
              for(i=1;i<nRegisters;i++)
464
                b_data[i] = ntohl(b_data[i]);
465
            }
466
          break;
467
        case JTAG_COMMAND_CHAIN:
468
          xmit_chain.command = htonl(command);
469
          xmit_chain.length = htonl(sizeof(xmit_chain)-8);
470
          xmit_chain.chain = htonl(va_arg(ap,unsigned int));
471
          if(result = jtag_proxy_write(fd,&xmit_chain,sizeof(xmit_chain)))
472
            break;
473
          if(result = ReadResponse(fd,&recv_chain,sizeof(recv_chain)))
474
            break;
475
          result = recv_chain.status;
476
          break;
477
        default:
478
          result = JTAG_PROXY_INVALID_COMMAND;
479
          break;
480
        }
481
      va_end(ap);
482
    }
483
  else
484
    {
485
      va_end(ap);
486
      error("jtag_send_proxy called without a remote proxy connection!");
487
      result = JTAG_PROXY_NO_CONNECTION;
488
    }
489
 
490
  if(result == JTAG_PROXY_SERVER_TERMINATED)
491
    {
492
      close(connection.device.fd);
493
      connection.device.fd = 0;
494
      connection.location = JTAG_NOT_CONNECTED;
495
    }
496
 
497
  return result;
498
}
499
 
500
/* Added by CZ 24/05/01 */
501
#ifndef ANSI_PROTOTYPES
502
static void jtag_proxy_error(va_alist)
503
va_dcl
504
#else
505
static void jtag_proxy_error(int result,int command,...)
506
#endif
507
{
508
  va_list ap;
509
  char sTemp[256];
510
 
511
#ifndef ANSI_PROTOTYPES
512
  int result;
513
  int command;
514
 
515
  va_start(ap);
516
  result = va_arg(ap,int);
517
  command = va_arg(ap,int);
518
#else
519
  va_start(ap, command);
520
#endif
521
 
522
  switch(command)
523
    {
524
    case JTAG_COMMAND_READ:
525
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
526
              "\"JTAG_COMMAND_READ\",%u,0x%08x\nThe command returned %d.\n",
527
              va_arg(ap,unsigned int),va_arg(ap,unsigned long long*),result);
528
      error(sTemp);
529
      break;
530
    case JTAG_COMMAND_WRITE:
531
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
532
              "\"JTAG_COMMAND_WRITE\",%u,0x%016llx\nThe command returned %d.\n",
533
              va_arg(ap,unsigned int),va_arg(ap,unsigned long long),result);
534
      error(sTemp);
535
      break;
536
    case JTAG_COMMAND_CHAIN:
537
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
538
              "\"JTAG_COMMAND_CHAIN\",%u. The command returned %d.\n",
539
              va_arg(ap,unsigned int));
540
      error(sTemp);
541
      break;
542
    case JTAG_COMMAND_BLOCK_READ:
543
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
544
              "\"JTAG_COMMAND_BLOCK_READ\",0x%08x,0x%08x,%d\nThe command returned %d.\n",
545
              va_arg(ap,unsigned long),va_arg(ap,unsigned long),va_arg(ap,int),result);
546
      error(sTemp);
547
      break;
548
    case JTAG_COMMAND_BLOCK_WRITE:
549
    default:
550
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
551
              "\"JTAG_COMMAND_BLOCK_WRITE\",0x%08x,0x%08x,%d\nThe command returned %d.\n",
552
              va_arg(ap,unsigned long),va_arg(ap,unsigned long),va_arg(ap,int),result);
553
      error(sTemp);
554
      break;
555
    }
556
  va_end(ap);
557
}
558
 
559
/* Sets register/memory regno to data.  */
560
 
561
/* CZ 08/06/01: I am not sure how error checking is intended to
562
   be implemented here. It appears that no indication is returned
563
   to the caller as you have in standard unix system calls. Therefore,
564
   I guess the only way to use these functions when you want to know
565
   the exact position of the error is to manually clear err, call the
566
   function, and then manually check err. I have also made some changes
567
   where necessary because no value was returned at all int jtag_read_reg.
568
*/
569
 
570
void
571
jtag_write_reg (regno, data)
572
     int regno;
573
     ULONGEST data;
574
{
575
  int crc_read, crc_write, crc_ok, retry;
576
  int result;
577
 
578
#ifdef DEBUG_JTAG
579
  printf_unfiltered ("write reg %x <- %x\n", regno, data);
580
  fflush (stdout);
581
#endif
582
  switch(connection.location) /* CZ */
583
    {
584
    case JTAG_LOCAL:
585
      if (!select_dr)
586
        jp1_write_JTAG (1); /* SELECT_DR SCAN */
587
      select_dr = 1;
588
 
589
      /* If we don't have rw bit, we assume chain
590
         is read only. */
591
      if (!chain_has_rw[current_chain])
592
        error ("Internal: Chain not writable.");
593
 
594
      for (retry = 0; retry < NUM_RETRIES; retry++)
595
        {
596
          jp1_write_JTAG (0); /* CAPTURE_DR */
597
          jp1_write_JTAG (0); /* SHIFT_DR */
598
          crc_w = 0;
599
 
600
          /* write addr */
601
          jp1_write_stream (regno, chain_addr_size[current_chain], 0);
602
 
603
          /* write (R/W=1) - we tested that previously. */
604
          jp1_write_JTAG (2);
605
 
606
          /* write data */
607
          jp1_write_stream (data, chain_data_size[current_chain], 0);
608
          if (chain_has_crc[current_chain])
609
            {
610
              crc_write = crc_w;
611
 
612
              /* write CRC, EXIT1_DR */
613
              crc_read = jp1_read_stream (crc_write, CRC_SIZE, 1);
614
            }
615
          jp1_write_JTAG (1); /* UPDATE_DR */
616
 
617
          /* Did JTAG receive packet correctly? */
618
          if (chain_has_crc[current_chain])
619
            crc_ok = jp1_read_JTAG ();
620
          jp1_write_JTAG (1); /* SELECT_DR */
621
          if (chain_has_crc[current_chain])
622
            {
623
              if ((crc_read == crc_write) && (crc_ok))
624
                return;
625
              JTAG_RETRY_WAIT();
626
            }
627
          else return;
628
        }
629
      err = ERR_CRC;
630
      break;
631
    case JTAG_REMOTE:
632
      if(result = jtag_send_proxy(JTAG_COMMAND_WRITE,regno,data))
633
        {
634
          jtag_proxy_error(result,JTAG_COMMAND_WRITE,regno,data);
635
          err = result;
636
        }
637
      break;
638
    default:
639
      error("jtag_write_reg called with no connection!");
640
      break;
641
    }
642
#ifdef DEBUG_JTAG
643
  printf_unfiltered ("!write reg\n");
644
  fflush (stdout);
645
#endif
646
}
647
 
648
/* Reads register/memory from regno.  */
649
 
650
ULONGEST
651
jtag_read_reg (regno)
652
     unsigned int regno;
653
{
654
  ULONGEST data;
655
  int crc_read, crc_write, crc_actual_read,  retry, crc_ok;
656
  int result;
657
 
658
#ifdef DEBUG_JTAG
659
  printf_unfiltered ("read reg %x\n", regno);
660
  fflush (stdout);
661
#endif
662
  switch(connection.location)
663
    {
664
    case JTAG_LOCAL:
665
      if (!select_dr)
666
        jp1_write_JTAG (1); /* SELECT_DR SCAN */
667
      select_dr = 1;
668
 
669
      for (retry = 0; retry < NUM_RETRIES; retry++)
670
        {
671
          jp1_write_JTAG (0); /* CAPTURE_DR */
672
          jp1_write_JTAG (0); /* SHIFT_DR */
673
          crc_w = 0;
674
 
675
          /* write addr */
676
          jp1_write_stream (regno, chain_addr_size[current_chain], 0);
677
 
678
          /* read (R/W=0) */
679
          if (chain_has_rw[current_chain])
680
            jp1_write_JTAG (0);
681
          if (chain_has_crc[current_chain])
682
            {
683
              crc_r = 0;
684
 
685
              /* data = 0 */
686
              data = jp1_read_stream (0, chain_data_size[current_chain], 0);
687
              crc_write = crc_w;
688
              crc_actual_read = crc_read;
689
 
690
              /* Send my crc, EXIT1_DR */
691
              crc_read = jp1_read_stream (crc_write, CRC_SIZE, 1);
692
            }
693
          jp1_write_JTAG (1); /* UPDATE_DR */
694
 
695
          /* Did JTAG receive packet correctly? */
696
          if (chain_has_crc[current_chain])
697
            crc_ok = jp1_read_JTAG ();
698
          jp1_write_JTAG (1); /* SELECT_DR */
699
          if (chain_has_crc[current_chain])
700
            {
701
              if ((crc_read == crc_actual_read) && (crc_ok))
702
                return -1;  /* CZ */
703
              JTAG_RETRY_WAIT();
704
            }
705
          else
706
            return -1;  /* CZ */
707
        }
708
      err = ERR_CRC;
709
      break;
710
    case JTAG_REMOTE:
711
      if(result = jtag_send_proxy(JTAG_COMMAND_READ,regno,&data))
712
        {
713
          jtag_proxy_error(result,JTAG_COMMAND_READ,regno,&data);
714
          err = result;
715
        }
716
      break;
717
    default:
718
      error("jtag_write_reg called with no connection!");
719
      break;
720
    }
721
 
722
#ifdef DEBUG_JTAG
723
  printf_unfiltered ("read reg %x\n", data);
724
  fflush (stdout);
725
#endif
726
  return data;  /* CZ */
727
}
728
 
729
int jtag_write_block(unsigned int regno,void* data,int nRegisters)
730
{
731
  unsigned long* buffer = (unsigned long*)data;
732
  int i,result;
733
 
734
  if(nRegisters == 1) {
735
    jtag_write_reg(regno, *buffer);
736
    return err;
737
  }
738
 
739
#ifdef DEBUG_JTAG
740
  printf_unfiltered ("write block %x,%i\n", regno, nRegisters);
741
  fflush (stdout);
742
#endif
743
 
744
  err = 0;
745
  switch(connection.location)
746
    {
747
    case JTAG_LOCAL:         /* We can't do any better than this locally */
748
      for(i=0;i<nRegisters;i++)
749
        jtag_write_reg(regno+i,(ULONGEST)buffer[i]);
750
      break;
751
    case JTAG_REMOTE:
752
      if(result = jtag_send_proxy(JTAG_COMMAND_BLOCK_WRITE,regno,data,nRegisters))
753
        {
754
          jtag_proxy_error(result,JTAG_COMMAND_BLOCK_WRITE,regno,data,nRegisters);
755
          err = result;
756
        }
757
      break;
758
    default:
759
      error("jtag_write_reg called with no connection!");
760
      break;
761
    }
762
 
763
#ifdef DEBUG_JTAG
764
  printf_unfiltered ("!write block\n");
765
  fflush (stdout);
766
#endif
767
  return err;
768
}
769
 
770
int jtag_read_block(unsigned int regno,void* data,int nRegisters)
771
{
772
  unsigned long* buffer = (unsigned long*)data;
773
  int i,result;
774
 
775
#ifdef DEBUG_JTAG
776
  printf_unfiltered ("read block %x,%i\n", regno, nRegisters);
777
  fflush (stdout);
778
#endif
779
 
780
  /*  if(nRegisters == 1) {
781
    *buffer = jtag_read_reg(regno);
782
    return err;
783
    }*/
784
  err = 0;
785
  switch(connection.location)
786
    {
787
    case JTAG_LOCAL:         /* We can't do any better than this locally */
788
      for(i=0;i<nRegisters;i++)
789
        buffer[i] = (unsigned long)jtag_read_reg(regno+i);
790
      break;
791
    case JTAG_REMOTE:
792
      if(result = jtag_send_proxy(JTAG_COMMAND_BLOCK_READ,regno,data,nRegisters))
793
        {
794
          jtag_proxy_error(result,JTAG_COMMAND_BLOCK_READ,regno,data,nRegisters);
795
          err = result;
796
        }
797
      break;
798
    default:
799
      error("jtag_write_reg called with no connection!");
800
      break;
801
    }
802
 
803
#ifdef DEBUG_JTAG
804
  printf_unfiltered ("read block\n");
805
  fflush (stdout);
806
#endif  
807
  return err;
808
}
809
 
810
/* Sets scan chain.  */
811
 
812
void
813
jtag_set_chain (chain)
814
     int chain;
815
{
816
  int result;
817
 
818
#ifdef DEBUG_JTAG
819
  printf_unfiltered ("set chain %x\n", chain);
820
  fflush (stdout);
821
#endif
822
  switch(connection.location)
823
    {
824
    case JTAG_LOCAL:
825
      if (current_chain != chain)
826
        {
827
          if (!chain_is_valid[chain])
828
            error ("Chain not valid.");
829
 
830
          current_chain = chain;
831
          jp1_prepare_control ();
832
 
833
          jp1_write_JTAG (0); /* CAPTURE_IR */
834
          jp1_write_JTAG (0); /* SHIFT_IR */
835
 
836
          /* write data, EXIT1_IR */
837
          jp1_write_stream (JI_CHAIN_SELECT, JI_SIZE, 4);
838
 
839
          jp1_write_JTAG (1); /* UPDATE_IR */
840
          jp1_write_JTAG (1); /* SELECT_DR */
841
 
842
          jp1_write_JTAG (0); /* CAPTURE_DR */
843
          jp1_write_JTAG (0); /* SHIFT_DR */
844
 
845
          /* write data, EXIT1_DR */
846
          jp1_write_stream (chain, SC_SIZE, 1);
847
 
848
          jp1_write_JTAG (1); /* UPDATE_DR */
849
          jp1_write_JTAG (1); /* SELECT_DR */
850
 
851
          /* Now we have to go out of SELECT_CHAIN mode.  */
852
          jp1_write_JTAG (1); /* SELECT_IR */
853
          jp1_write_JTAG (0); /* CAPTURE_IR */
854
          jp1_write_JTAG (0); /* SHIFT_IR */
855
 
856
          /* write data, EXIT1_IR */
857
          jp1_write_stream (JI_DEBUG, JI_SIZE,1 );
858
 
859
          jp1_write_JTAG (1); /* UPDATE_IR */
860
          jp1_write_JTAG (1); /* SELECT_DR */
861
          select_dr = 1;
862
        }
863
      break;
864
    case JTAG_REMOTE:
865
      if(current_chain != chain)
866
        {
867
          if(result = jtag_send_proxy(JTAG_COMMAND_CHAIN,chain))
868
            {
869
              jtag_proxy_error(result,JTAG_COMMAND_CHAIN,chain);
870
              err = result;
871
            }
872
        }
873
      break;
874
    default:
875
      error("jtag_set_chain called with no connection!");
876
      break;
877
    }
878
#ifdef DEBUG_JTAG
879
  printf_unfiltered ("!set chain\n");
880
  fflush (stdout);
881
#endif
882
}
883
 
884
/* Added by CZ 24/05/01 */
885
static int jtag_connect_to_server(char* hostname,char* name)
886
{
887
  struct hostent *host;
888
  struct sockaddr_in sin;
889
  struct servent *service;
890
  struct protoent *protocol;
891
  int sock,flags;
892
  int fd;
893
  char sTemp[256],sTemp2[256];
894
  char* proto_name = "tcp";
895
  int port = 0;
896
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
897
  char *s;
898
 
899
  if(!(protocol = getprotobyname(proto_name)))
900
    {
901
      sprintf(sTemp,"jtag_connect_to_server: Protocol \"%s\" not available.\n",
902
              proto_name);
903
      error(sTemp);
904
      return 0;
905
    }
906
 
907
  /* Convert name to an integer only if it is well formatted.
908
     Otherwise, assume that it is a service name. */
909
 
910
  port = strtol(name,&s,10);
911
  if(*s)
912
    port = 0;
913
 
914
  if(!port)
915
    {
916
      if(!(service = getservbyname(name,protocol->p_name)))
917
        {
918
          sprintf(sTemp,"jtag_connect_to_server: Unknown service \"%s\".\n",name);
919
          error(sTemp);
920
          return 0;
921
        }
922
 
923
      port = ntohs(service->s_port);
924
    }
925
 
926
  if(!(host = gethostbyname(hostname)))
927
    {
928
      sprintf(sTemp,"jtag_connect_to_server: Unknown host \"%s\"\n",hostname);
929
      error(sTemp);
930
      return 0;
931
    }
932
 
933
  if((sock = socket(PF_INET,SOCK_STREAM,0)) < 0)
934
    {
935
      sprintf(sTemp,"jtag_connect_to_server: can't create socket errno = %d\n",
936
              errno);
937
      sprintf(sTemp2,"%s\n",strerror(errno));
938
      strcat(sTemp,sTemp2);
939
      error(sTemp);
940
      return 0;
941
    }
942
 
943
  if(fcntl(sock,F_GETFL,&flags) < 0)
944
    {
945
      sprintf(sTemp,"Unable to get flags for JTAG proxy socket %d",sock);
946
      error(sTemp);
947
      close(sock);
948
      return 0;
949
    }
950
 
951
  if(fcntl(sock,F_SETFL, flags & ~O_NONBLOCK) < 0)
952
    {
953
      sprintf(sTemp,"Unable to set flags for JTAG proxy socket %d to value 0x%08x",
954
              sock,flags | O_NONBLOCK);
955
      error(sTemp);
956
      close(sock);
957
      return 0;
958
    }
959
 
960
  memset(&sin,0,sizeof(sin));
961
  sin.sin_family = host->h_addrtype;
962
  memcpy(&sin.sin_addr,host->h_addr_list[0],host->h_length);
963
  sin.sin_port = htons(port);
964
 
965
  if((connect(sock,(struct sockaddr*)&sin, sizeof(sin)) < 0)
966
     && errno != EINPROGRESS)
967
    {
968
 
969
      sprintf(sTemp,"jtag_connect_to_server: connect failed  errno = %d\n",errno);
970
      sprintf(sTemp2,"%s\n",strerror(errno));
971
      close(sock);
972
      strcat(sTemp,sTemp2);
973
      error(sTemp);
974
      return 0;
975
    }
976
 
977
  if(fcntl(sock,F_SETFL, flags | O_NONBLOCK) < 0)
978
    {
979
      sprintf(sTemp,"Unable to set flags for JTAG proxy socket %d to value 0x%08x",
980
              sock,flags | O_NONBLOCK);
981
      error(sTemp);
982
      close(sock);
983
      return 0;
984
    }
985
 
986
  if(setsockopt(sock,protocol->p_proto,TCP_NODELAY,&on_off,sizeof(int)) < 0)
987
    {
988
      sprintf(sTemp,"Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",sock);
989
      error(sTemp);
990
      close(sock);
991
      return 0;
992
    }
993
 
994
  return sock;
995
}
996
 
997
/* Initialize a new connection to the or1k board, and make sure we are
998
   really connected.  */
999
 
1000
void
1001
jtag_init (args)
1002
     char * args;
1003
{
1004
  char *ptype;
1005
  char *port_name;
1006
  char **argv;
1007
 
1008
#ifdef DEBUG_JTAG
1009
  printf_unfiltered ("INIT\n");
1010
  fflush(stdout);
1011
#endif
1012
  if (args == 0) /* CZ */
1013
    error ( "To open a or1k remote debugging connection, you need to specify a "
1014
            "parallel port\nconnected to the target board, or else a remote "
1015
            "server which will proxy these\nservices for you.\nExample: "
1016
            "/dev/lp0 or jtag://debughost.mydomain.com:8100.\n");
1017
 
1018
  /* If we currently have an open connection, shut it
1019
     down.  This is due to a temporary bug in gdb. */
1020
  switch(connection.location)
1021
    {
1022
    case JTAG_REMOTE:
1023
      if(connection.device.fd > 0)
1024
        {
1025
          close(connection.device.fd);
1026
        }
1027
      break;
1028
    case JTAG_LOCAL:
1029
      if(connection.device.lp > 0)
1030
        {
1031
          close(connection.device.lp);
1032
        }
1033
      break;
1034
    default:
1035
      break;
1036
    }
1037
 
1038
  /* Parse the port name.  */
1039
  if ((argv = buildargv (args)) == NULL)
1040
    nomem (0);
1041
  port_name = strsave (argv[0]);
1042
  make_cleanup_freeargv (argv);
1043
 
1044
  /* CZ 24/05/01 - Check to see if we have specified a remote
1045
     jtag interface or a local one. It is remote if it follows
1046
     the URL naming convention jtag://<hostname>:<port> */
1047
  if(!strncmp(port_name,"jtag://",7))
1048
    {
1049
      char *port = strchr(&port_name[7],':');
1050
      char hostname[256];
1051
 
1052
      if(port)
1053
        {
1054
          int len = port - port_name - 7;
1055
          strncpy(hostname,&port_name[7],len);
1056
          hostname[len] = '\0';
1057
          port++;
1058
        }
1059
      else
1060
        strcpy(hostname,&port_name[7]);
1061
 
1062
      /* Interface is remote */
1063
      if(!(connection.device.fd = jtag_connect_to_server(hostname,port)))
1064
        {
1065
          char sTemp[256];
1066
          sprintf(sTemp,"Can not access JTAG Proxy Server at \"%s\"",
1067
                  &port_name[5]);
1068
          error(sTemp);
1069
        }
1070
      connection.location = JTAG_REMOTE;
1071
      printf_unfiltered ("Remote or1k debugging using %s\n",port_name);
1072
    }
1073
  else
1074
    {
1075
      /* Interface is local */
1076
      /* Open and initialize the parallel port.  */
1077
      connection.device.lp = open (port_name, O_WRONLY);
1078
      if (connection.device.lp < 0)
1079
        error ("Cannot open device.");
1080
      connection.location = JTAG_LOCAL; /* CZ */
1081
      printf_unfiltered ("Local or1k debugging using %s\n", port_name);
1082
 
1083
    }
1084
 
1085
  current_chain = -1;
1086
  if(connection.location == JTAG_LOCAL)
1087
    jp1_reset_JTAG ();
1088
  jtag_set_chain (SC_RISC_DEBUG);
1089
 
1090
  free (port_name);
1091
}
1092
 
1093
void
1094
jtag_done ()  /* CZ */
1095
{
1096
  switch(connection.location)
1097
    {
1098
    case JTAG_LOCAL:
1099
      sync_close (connection.device.lp);
1100
      connection.device.lp = 0;
1101
      break;
1102
    case JTAG_REMOTE:
1103
      sync_close(connection.device.fd);
1104
      connection.device.fd = 0;
1105
      break;
1106
    default:
1107
      break;
1108
    }
1109
  connection.location = JTAG_NOT_CONNECTED;
1110
}
1111
 
1112
int sync_close(int fd)
1113
{
1114
  int flags = 0;
1115
  struct linger linger;
1116
  char sTemp[256];
1117
 
1118
  linger.l_onoff = 0;
1119
  linger.l_linger = 0;
1120
 
1121
  /* First, make sure we're non blocking */
1122
  if(fcntl(fd,F_GETFL,&flags) < 0)
1123
    {
1124
      sprintf(sTemp,"Unable to get flags for JTAG proxy socket %d",fd);
1125
      error(sTemp);
1126
    }
1127
  if(fcntl(fd,F_SETFL, flags & ~O_NONBLOCK) < 0)
1128
    {
1129
      sprintf(sTemp,"Unable to set flags for JTAG proxy socket %d to value 0x%08x",
1130
              fd,flags | O_NONBLOCK);
1131
      error(sTemp);
1132
    }
1133
 
1134
  /* Now, make sure we don't linger around */
1135
  if(setsockopt(fd,SOL_SOCKET,SO_LINGER,&linger,sizeof(linger)) < 0)
1136
    {
1137
      sprintf(sTemp,"Unable to disable SO_LINGER for JTAG proxy socket %d.",fd);
1138
      error(sTemp);
1139
    }
1140
 
1141
  return close(fd);
1142
}

powered by: WebSVN 2.1.0

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