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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [jtag.c] - Blame information for rev 124

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

Line No. Rev Author Line
1 104 markom
/* Remote debugging interface for JTAG debugging protocol.
2 118 markom
   JTAG connects to or1k target ops. See or1k-tdep.c
3
 
4 104 markom
   Copyright 1993-1995, 2000 Free Software Foundation, Inc.
5
   Contributed by Cygnus Support.  Written by Marko Mlinar
6
   <markom@opencores.org>
7 124 chris
   Areas noted by (CZ) were modified by Chris Ziomkowski
8
   <chris@asics.ws>
9 104 markom
 
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 124 chris
/* 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 104 markom
 
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 113 markom
#define TCLK (0x01)
56
#define TRST (0x02)
57 104 markom
#define JTAG_WAIT()
58 113 markom
#define NUM_RETRIES (16)
59
#define JTAG_RETRY_WAIT() usleep (100)
60 104 markom
 
61 113 markom
/* Selects crc trailer size in bits. Currently supported: 8 */
62
#define CRC_SIZE (8)
63 104 markom
 
64 113 markom
/* 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 104 markom
/* Designates whether we are in SELECT_DR state, otherwise in
81
   RUN TEST/IDLE */
82
static int select_dr = 0;
83
 
84 124 chris
/* 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 104 markom
 
91 124 chris
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 104 markom
/* 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 118 markom
 
107 104 markom
static int
108
crc_calc (int crc, int input_bit)
109
{
110
  int c;
111
  int new_crc;
112
  int d;
113
 
114 113 markom
#if (CRC_SIZE == 8)
115 104 markom
  d = input_bit&1;
116
  c = crc;
117
 
118
  /* Move queue left.  */
119
  new_crc = crc << 1;
120 118 markom
 
121 104 markom
  /* 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 113 markom
#else
130
  return 0;
131
#endif
132 104 markom
}
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 124 chris
  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 104 markom
  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 124 chris
  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 104 markom
  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 124 chris
 
186
  if(connection.location != JTAG_LOCAL)    /* CZ */
187
    error("jp1_read_JTAG called without a local connection!");
188
 
189 113 markom
  ioctl (data, 0x60b, &data);
190 104 markom
  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 122 markom
     ULONGEST stream;
200 104 markom
     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 122 markom
static ULONGEST
217 104 markom
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 122 markom
  ULONGEST data;
224 104 markom
 
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 118 markom
  jp1_write_JTAG (1);   /* SELECT_IR SCAN */
252 104 markom
  select_dr = 0;
253
}
254
 
255 124 chris
/* 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 = jtag_proxy_read(fd,&status,4);
337
  char* buf = (char*)buffer;
338
 
339
  status = ntohl(status);
340
  *((int32_t*)buffer) = status;
341
 
342
  if(result)    return result;
343
  if(status)   return status;
344
 
345
  result = jtag_proxy_read(fd,&buf[4],len-4);
346
  return result;
347
}
348
 
349
/* Added by CZ 24/05/01 */
350
#ifndef ANSI_PROTOTYPES
351
static int jtag_send_proxy(va_alist)
352
va_dcl
353
#else
354
static int jtag_send_proxy(int command,...)
355
#endif
356
{
357
  va_list ap;
358
  int result = 0;
359
  int fd = connection.device.fd;
360
  JTAGProxyWriteMessage xmit_write;
361
  JTAGProxyReadMessage xmit_read;
362
  JTAGProxyChainMessage xmit_chain;
363
  JTAGProxyWriteResponse recv_write;
364
  JTAGProxyReadResponse recv_read;
365
  JTAGProxyChainResponse recv_chain;
366
  unsigned long long data,* ret_val;
367
  uint32_t word1; /* Use word1 and word2 to ease portability to platforms */
368
  uint32_t word2; /* without long long and for alignment reasons */
369
 
370
#ifndef ANSI_PROTOTYPES
371
  int command;
372
 
373
  va_start(ap);
374
  command = va_arg(ap,int);
375
#else
376
  va_start(ap, command);
377
#endif
378
 
379
 
380
 
381
  if(connection.location == JTAG_REMOTE)
382
    {
383
      switch(command)
384
        {
385
        case JTAG_COMMAND_READ:
386
          xmit_read.command = htonl(command);
387
          xmit_read.length = htonl(sizeof(xmit_read)-8);
388
          xmit_read.address = htonl(va_arg(ap,int));
389
          ret_val = va_arg(ap,unsigned long long *);
390
          /* intentional single equals */
391
          if(result = jtag_proxy_write(fd,&xmit_read,sizeof(xmit_read)))
392
            break;
393
          if(result = ReadResponse(fd,&recv_read,sizeof(recv_read)))
394
            break;
395
          result = ntohl(recv_read.status);
396
          word1 = ntohl(recv_read.data_H);
397
          word2 = ntohl(recv_read.data_L);
398
          *ret_val = (((unsigned long long)word1) << 32) | word2;
399
          break;
400
        case JTAG_COMMAND_WRITE:
401
          xmit_write.command = htonl(command);
402
          xmit_write.length = htonl(sizeof(xmit_write)-8);
403
          xmit_write.address = htonl(va_arg(ap,int));
404
          data = va_arg(ap,unsigned long long);
405
          word1 = htonl(data >> 32);
406
          word2 = htonl(data & 0xFFFFFFFF);
407
          xmit_write.data_H = word1;
408
          xmit_write.data_L = word2;
409
          if(result = jtag_proxy_write(fd,&xmit_write,sizeof(xmit_write)))
410
            break;
411
          if(result = ReadResponse(fd,&recv_write,sizeof(recv_write)))
412
            break;
413
          result = recv_write.status;
414
          break;
415
        case JTAG_COMMAND_CHAIN:
416
          xmit_chain.command = htonl(command);
417
          xmit_chain.length = htonl(sizeof(xmit_chain)-8);
418
          xmit_chain.chain = htonl(va_arg(ap,unsigned int));
419
          if(result = jtag_proxy_write(fd,&xmit_chain,sizeof(xmit_chain)))
420
            break;
421
          if(result = ReadResponse(fd,&recv_chain,sizeof(recv_chain)))
422
            break;
423
          result = recv_chain.status;
424
          break;
425
        default:
426
          result = JTAG_PROXY_INVALID_COMMAND;
427
          break;
428
        }
429
      va_end(ap);
430
    }
431
  else
432
    {
433
      va_end(ap);
434
      error("jtag_send_proxy called without a remote proxy connection!");
435
      result = JTAG_PROXY_NO_CONNECTION;
436
    }
437
 
438
  if(result == JTAG_PROXY_SERVER_TERMINATED)
439
    {
440
      close(connection.device.fd);
441
      connection.device.fd = 0;
442
      connection.location = JTAG_NOT_CONNECTED;
443
    }
444
 
445
  return result;
446
}
447
 
448
/* Added by CZ 24/05/01 */
449
#ifndef ANSI_PROTOTYPES
450
static void jtag_proxy_error(va_alist)
451
va_dcl
452
#else
453
static void jtag_proxy_error(int result,int command,...)
454
#endif
455
{
456
  va_list ap;
457
  char sTemp[256];
458
 
459
#ifndef ANSI_PROTOTYPES
460
  int result;
461
  int command;
462
 
463
  va_start(ap);
464
  result = va_arg(ap,int);
465
  command = va_arg(ap,int);
466
#else
467
  va_start(ap, command);
468
#endif
469
 
470
  switch(command)
471
    {
472
    case JTAG_COMMAND_READ:
473
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
474
              "\"JTAG_COMMAND_READ\",%u,0x%08x\nThe command returned %d.\n",
475
              va_arg(ap,unsigned int),va_arg(ap,unsigned long long*),result);
476
      error(sTemp);
477
      break;
478
    case JTAG_COMMAND_WRITE:
479
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
480
              "\"JTAG_COMMAND_WRITE\",%u,0x%016llx\nThe command returned %d.\n",
481
              va_arg(ap,unsigned int),va_arg(ap,unsigned long long),result);
482
      error(sTemp);
483
      break;
484
    case JTAG_COMMAND_CHAIN:
485
      sprintf(sTemp,"An error was reported by the proxy server. The command was:\n"
486
              "\"JTAG_COMMAND_CHAIN\",%u. The command returned %d.\n",
487
              va_arg(ap,unsigned int));
488
      error(sTemp);
489
      break;
490
    default:
491
      sprintf(sTemp,"An error was reported by the proxy server. The command is "
492
              "currently unimplemented.\nThe command code was %d, and the result "
493
              "code was %d.\n",command,result);
494
      error(sTemp);
495
      break;
496
    }
497
  va_end(ap);
498
}
499
 
500 104 markom
/* Sets register/memory regno to data.  */
501
 
502 124 chris
/* CZ 08/06/01: I am not sure how error checking is intended to
503
   be implemented here. It appears that no indication is returned
504
   to the caller as you have in standard unix system calls. Therefore,
505
   I guess the only way to use these functions when you want to know
506
   the exact position of the error is to manually clear err, call the
507
   function, and then manually check err. I have also made some changes
508
   where necessary because no value was returned at all int jtag_read_reg.
509
*/
510
 
511 104 markom
void
512
jtag_write_reg (regno, data)
513
     int regno;
514 122 markom
     ULONGEST data;
515 104 markom
{
516
  int crc_read, crc_write, crc_ok, retry;
517 124 chris
  int result;
518 104 markom
 
519 124 chris
  switch(connection.location) /* CZ */
520
    {
521
    case JTAG_LOCAL:
522
      if (!select_dr)
523
        jp1_write_JTAG (1); /* SELECT_DR SCAN */
524
      select_dr = 1;
525 104 markom
 
526 124 chris
      /* If we don't have rw bit, we assume chain
527
         is read only. */
528
      if (!chain_has_rw[current_chain])
529
        error ("Internal: Chain not writable.");
530 113 markom
 
531 124 chris
      for (retry = 0; retry < NUM_RETRIES; retry++)
532
        {
533
          jp1_write_JTAG (0); /* CAPTURE_DR */
534
          jp1_write_JTAG (0); /* SHIFT_DR */
535
          crc_w = 0;
536 118 markom
 
537 124 chris
          /* write addr */
538
          jp1_write_stream (regno, chain_addr_size[current_chain], 0);
539 118 markom
 
540 124 chris
          /* write (R/W=1) - we tested that previously. */
541
          jp1_write_JTAG (2);
542 118 markom
 
543 124 chris
          /* write data */
544
          jp1_write_stream (data, chain_data_size[current_chain], 0);
545
          if (chain_has_crc[current_chain])
546
            {
547
              crc_write = crc_w;
548 118 markom
 
549 124 chris
              /* write CRC, EXIT1_DR */
550
              crc_read = jp1_read_stream (crc_write, CRC_SIZE, 1);
551
            }
552
          jp1_write_JTAG (1); /* UPDATE_DR */
553
 
554
          /* Did JTAG receive packet correctly? */
555
          if (chain_has_crc[current_chain])
556
            crc_ok = jp1_read_JTAG ();
557
          jp1_write_JTAG (1); /* SELECT_DR */
558
          if (chain_has_crc[current_chain])
559
            {
560
              if ((crc_read == crc_write) && (crc_ok))
561
                return;
562
              JTAG_RETRY_WAIT();
563
            }
564
          else return;
565 113 markom
        }
566 124 chris
      err = ERR_CRC;
567
      break;
568
    case JTAG_REMOTE:
569
      if(result = jtag_send_proxy(JTAG_COMMAND_WRITE,regno,data))
570 113 markom
        {
571 124 chris
          jtag_proxy_error(result,JTAG_COMMAND_WRITE,regno,data);
572
          err = result;
573 113 markom
        }
574 124 chris
      break;
575
    default:
576
      error("jtag_write_reg called with no connection!");
577
      break;
578 104 markom
    }
579
}
580
 
581
/* Reads register/memory from regno.  */
582
 
583 122 markom
ULONGEST
584 104 markom
jtag_read_reg (regno)
585
     unsigned int regno;
586
{
587 122 markom
  ULONGEST data;
588 104 markom
  int crc_read, crc_write, crc_actual_read,  retry, crc_ok;
589 124 chris
  int result;
590 104 markom
 
591 124 chris
  switch(connection.location)
592
    {
593
    case JTAG_LOCAL:
594
      if (!select_dr)
595
        jp1_write_JTAG (1); /* SELECT_DR SCAN */
596
      select_dr = 1;
597 104 markom
 
598 124 chris
      for (retry = 0; retry < NUM_RETRIES; retry++)
599
        {
600
          jp1_write_JTAG (0); /* CAPTURE_DR */
601
          jp1_write_JTAG (0); /* SHIFT_DR */
602
          crc_w = 0;
603 118 markom
 
604 124 chris
          /* write addr */
605
          jp1_write_stream (regno, chain_addr_size[current_chain], 0);
606 118 markom
 
607 124 chris
          /* read (R/W=0) */
608
          if (chain_has_rw[current_chain])
609
            jp1_write_JTAG (0);
610
          if (chain_has_crc[current_chain])
611
            {
612
              crc_r = 0;
613 118 markom
 
614 124 chris
              /* data = 0 */
615
              data = jp1_read_stream (0, chain_data_size[current_chain], 0);
616
              crc_write = crc_w;
617
              crc_actual_read = crc_read;
618 118 markom
 
619 124 chris
              /* Send my crc, EXIT1_DR */
620
              crc_read = jp1_read_stream (crc_write, CRC_SIZE, 1);
621
            }
622
          jp1_write_JTAG (1); /* UPDATE_DR */
623
 
624
          /* Did JTAG receive packet correctly? */
625
          if (chain_has_crc[current_chain])
626
            crc_ok = jp1_read_JTAG ();
627
          jp1_write_JTAG (1); /* SELECT_DR */
628
          if (chain_has_crc[current_chain])
629
            {
630
              if ((crc_read == crc_actual_read) && (crc_ok))
631
                return -1;  /* CZ */
632
              JTAG_RETRY_WAIT();
633
            }
634
          else
635
            return -1;  /* CZ */
636 113 markom
        }
637 124 chris
      err = ERR_CRC;
638
      break;
639
    case JTAG_REMOTE:
640
      if(result = jtag_send_proxy(JTAG_COMMAND_READ,regno,&data))
641 113 markom
        {
642 124 chris
          jtag_proxy_error(result,JTAG_COMMAND_READ,regno,&data);
643
          err = result;
644 113 markom
        }
645 124 chris
      break;
646
    default:
647
      error("jtag_write_reg called with no connection!");
648
      break;
649 104 markom
    }
650 124 chris
 
651
  return data;  /* CZ */
652 104 markom
}
653
 
654 113 markom
/* Sets scan chain.  */
655
 
656
void
657
jtag_set_chain (chain)
658
     int chain;
659
{
660 124 chris
  int result;
661
 
662
  switch(connection.location)
663 113 markom
    {
664 124 chris
    case JTAG_LOCAL:
665
      if (current_chain != chain)
666
        {
667
          if (!chain_is_valid[chain])
668
            error ("Chain not valid.");
669 113 markom
 
670 124 chris
          current_chain = chain;
671
          jp1_prepare_control ();
672 118 markom
 
673 124 chris
          jp1_write_JTAG (0); /* CAPTURE_IR */
674
          jp1_write_JTAG (0); /* SHIFT_IR */
675 118 markom
 
676 124 chris
          /* write data, EXIT1_IR */
677
          jp1_write_stream (JI_CHAIN_SELECT, JI_SIZE, 4);
678 118 markom
 
679 124 chris
          jp1_write_JTAG (1); /* UPDATE_IR */
680
          jp1_write_JTAG (1); /* SELECT_DR */
681 118 markom
 
682 124 chris
          jp1_write_JTAG (0); /* CAPTURE_DR */
683
          jp1_write_JTAG (0); /* SHIFT_DR */
684 118 markom
 
685 124 chris
          /* write data, EXIT1_DR */
686
          jp1_write_stream (chain, SC_SIZE, 1);
687 118 markom
 
688 124 chris
          jp1_write_JTAG (1); /* UPDATE_DR */
689
          jp1_write_JTAG (1); /* SELECT_DR */
690 118 markom
 
691 124 chris
          /* Now we have to go out of SELECT_CHAIN mode.  */
692
          jp1_write_JTAG (1); /* SELECT_IR */
693
          jp1_write_JTAG (0); /* CAPTURE_IR */
694
          jp1_write_JTAG (0); /* SHIFT_IR */
695 118 markom
 
696 124 chris
          /* write data, EXIT1_IR */
697
          jp1_write_stream (JI_DEBUG, JI_SIZE,1 );
698 118 markom
 
699 124 chris
          jp1_write_JTAG (1); /* UPDATE_IR */
700
          jp1_write_JTAG (1); /* SELECT_DR */
701
          select_dr = 1;
702
        }
703
      break;
704
    case JTAG_REMOTE:
705
      if(current_chain != chain)
706
        {
707
          if(result = jtag_send_proxy(JTAG_COMMAND_CHAIN,chain))
708
            {
709
              jtag_proxy_error(result,JTAG_COMMAND_CHAIN,chain);
710
              err = result;
711
            }
712
        }
713
      break;
714
    default:
715
      error("jtag_set_chain called with no connection!");
716
      break;
717 113 markom
    }
718
}
719
 
720 124 chris
/* Added by CZ 24/05/01 */
721
static int jtag_connect_to_server(char* hostname,char* name)
722
{
723
  struct hostent *host;
724
  struct sockaddr_in sin;
725
  struct servent *service;
726
  struct protoent *protocol;
727
  int sock,flags;
728
  int fd;
729
  char sTemp[256],sTemp2[256];
730
  char* proto_name = "tcp";
731
  int port = 0;
732
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
733
  char *s;
734
 
735
  if(!(protocol = getprotobyname(proto_name)))
736
    {
737
      sprintf(sTemp,"jtag_connect_to_server: Protocol \"%s\" not available.\n",
738
              proto_name);
739
      error(sTemp);
740
      return 0;
741
    }
742
 
743
  /* Convert name to an integer only if it is well formatted.
744
     Otherwise, assume that it is a service name. */
745
 
746
  port = strtol(name,&s,10);
747
  if(*s)
748
    port = 0;
749
 
750
  if(!port)
751
    {
752
      if(!(service = getservbyname(name,protocol->p_name)))
753
        {
754
          sprintf(sTemp,"jtag_connect_to_server: Unknown service \"%s\".\n",name);
755
          error(sTemp);
756
          return 0;
757
        }
758
 
759
      port = ntohs(service->s_port);
760
    }
761
 
762
  if(!(host = gethostbyname(hostname)))
763
    {
764
      sprintf(sTemp,"jtag_connect_to_server: Unknown host \"%s\"\n",hostname);
765
      error(sTemp);
766
      return 0;
767
    }
768
 
769
  if((sock = socket(PF_INET,SOCK_STREAM,0)) < 0)
770
    {
771
      sprintf(sTemp,"jtag_connect_to_server: can't create socket errno = %d\n",
772
              errno);
773
      sprintf(sTemp2,"%s\n",strerror(errno));
774
      strcat(sTemp,sTemp2);
775
      error(sTemp);
776
      return 0;
777
    }
778
 
779
  if(fcntl(sock,F_GETFL,&flags) < 0)
780
    {
781
      sprintf(sTemp,"Unable to get flags for JTAG proxy socket %d",sock);
782
      error(sTemp);
783
      close(sock);
784
      return 0;
785
    }
786
 
787
  if(fcntl(sock,F_SETFL, flags & ~O_NONBLOCK) < 0)
788
    {
789
      sprintf(sTemp,"Unable to set flags for JTAG proxy socket %d to value 0x%08x",
790
              sock,flags | O_NONBLOCK);
791
      error(sTemp);
792
      close(sock);
793
      return 0;
794
    }
795
 
796
  memset(&sin,0,sizeof(sin));
797
  sin.sin_family = host->h_addrtype;
798
  memcpy(&sin.sin_addr,host->h_addr_list[0],host->h_length);
799
  sin.sin_port = htons(port);
800
 
801
  if((connect(sock,(struct sockaddr*)&sin, sizeof(sin)) < 0)
802
     && errno != EINPROGRESS)
803
    {
804
 
805
      sprintf(sTemp,"jtag_connect_to_server: connect failed  errno = %d\n",errno);
806
      sprintf(sTemp2,"%s\n",strerror(errno));
807
      close(sock);
808
      strcat(sTemp,sTemp2);
809
      error(sTemp);
810
      return 0;
811
    }
812
 
813
  if(fcntl(sock,F_SETFL, flags | O_NONBLOCK) < 0)
814
    {
815
      sprintf(sTemp,"Unable to set flags for JTAG proxy socket %d to value 0x%08x",
816
              sock,flags | O_NONBLOCK);
817
      error(sTemp);
818
      close(sock);
819
      return 0;
820
    }
821
 
822
  if(setsockopt(sock,protocol->p_proto,TCP_NODELAY,&on_off,sizeof(int)) < 0)
823
    {
824
      sprintf(sTemp,"Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",sock);
825
      error(sTemp);
826
      close(sock);
827
      return 0;
828
    }
829
 
830
  return sock;
831
}
832
 
833 104 markom
/* Initialize a new connection to the or1k board, and make sure we are
834
   really connected.  */
835
 
836
void
837
jtag_init (args)
838
     char * args;
839
{
840
  char *ptype;
841
  char *port_name;
842
  char **argv;
843
 
844 124 chris
  if (args == 0) /* CZ */
845
    error ( "To open a or1k remote debugging connection, you need to specify a "
846
            "parallel port\nconnected to the target board, or else a remote "
847
            "server which will proxy these\nservices for you.\nExample: "
848
            "/dev/lp0 or jtag://debughost.mydomain.com:8100.\n");
849 104 markom
 
850 124 chris
  /* If we currently have an open connection, shut it
851
     down.  This is due to a temporary bug in gdb. */
852
  switch(connection.location)
853
    {
854
    case JTAG_REMOTE:
855
      if(connection.device.fd > 0)
856
        {
857
          close(connection.device.fd);
858
        }
859
      break;
860
    case JTAG_LOCAL:
861
      if(connection.device.lp > 0)
862
        {
863
          close(connection.device.lp);
864
        }
865
      break;
866
    default:
867
      break;
868
    }
869
 
870 118 markom
  /* Parse the port name.  */
871 104 markom
  if ((argv = buildargv (args)) == NULL)
872
    nomem (0);
873
  port_name = strsave (argv[0]);
874
  make_cleanup_freeargv (argv);
875
 
876 124 chris
  /* CZ 24/05/01 - Check to see if we have specified a remote
877
     jtag interface or a local one. It is remote if it follows
878
     the URL naming convention jtag://<hostname>:<port> */
879
  if(!strncmp(port_name,"jtag://",7))
880
    {
881
      char *port = strchr(&port_name[7],':');
882
      char hostname[256];
883 104 markom
 
884 124 chris
      if(port)
885
        {
886
          int len = port - port_name - 7;
887
          strncpy(hostname,&port_name[7],len);
888
          hostname[len] = '\0';
889
          port++;
890
        }
891
      else
892
        strcpy(hostname,&port_name[7]);
893 104 markom
 
894 124 chris
      /* Interface is remote */
895
      if(!(connection.device.fd = jtag_connect_to_server(hostname,port)))
896
        {
897
          char sTemp[256];
898
          sprintf(sTemp,"Can not access JTAG Proxy Server at \"%s\"",
899
                  &port_name[5]);
900
          error(sTemp);
901
        }
902
      connection.location = JTAG_REMOTE;
903
      printf_unfiltered ("Remote or1k debugging using %s\n",port_name);
904
    }
905
  else
906
    {
907
      /* Interface is local */
908
      /* Open and initialize the parallel port.  */
909
      connection.device.lp = open (port_name, O_WRONLY);
910
      if (connection.device.lp < 0)
911
        error ("Cannot open device.");
912
      connection.location = JTAG_LOCAL; /* CZ */
913
      printf_unfiltered ("Local or1k debugging using %s\n", port_name);
914
 
915
    }
916
 
917 113 markom
  current_chain = -1;
918 124 chris
  if(connection.location == JTAG_LOCAL)
919
    jp1_reset_JTAG ();
920 113 markom
  jtag_set_chain (SC_RISC_DEBUG);
921 124 chris
 
922 104 markom
  free (port_name);
923
}
924
 
925
void
926 124 chris
jtag_done ()  /* CZ */
927 104 markom
{
928 124 chris
  switch(connection.location)
929
    {
930
    case JTAG_LOCAL:
931
      sync_close (connection.device.lp);
932
      connection.device.lp = 0;
933
      break;
934
    case JTAG_REMOTE:
935
      sync_close(connection.device.fd);
936
      connection.device.fd = 0;
937
      break;
938
    default:
939
      error("No jtag connection specified!");
940
      break;
941
    }
942
  connection.location = JTAG_NOT_CONNECTED;
943 104 markom
}
944 124 chris
 
945
int sync_close(int fd)
946
{
947
  int flags = 0;
948
  struct linger linger;
949
  char sTemp[256];
950
 
951
  linger.l_onoff = 0;
952
  linger.l_linger = 0;
953
 
954
  /* First, make sure we're non blocking */
955
  if(fcntl(fd,F_GETFL,&flags) < 0)
956
    {
957
      sprintf(sTemp,"Unable to get flags for JTAG proxy socket %d",fd);
958
      error(sTemp);
959
    }
960
  if(fcntl(fd,F_SETFL, flags & ~O_NONBLOCK) < 0)
961
    {
962
      sprintf(sTemp,"Unable to set flags for JTAG proxy socket %d to value 0x%08x",
963
              fd,flags | O_NONBLOCK);
964
      error(sTemp);
965
    }
966
 
967
  /* Now, make sure we don't linger around */
968
  if(setsockopt(fd,SOL_SOCKET,SO_LINGER,&linger,sizeof(linger)) < 0)
969
    {
970
      sprintf(sTemp,"Unable to disable SO_LINGER for JTAG proxy socket %d.",fd);
971
      error(sTemp);
972
    }
973
 
974
  return close(fd);
975
}

powered by: WebSVN 2.1.0

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