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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [jtag/] [jp1.c] - Blame information for rev 588

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

Line No. Rev Author Line
1 435 simons
/* jp1-linux.c -- JTAG protocol via parallel port for linux
2
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
3
   Code for TCP/IP copied from gdb, by Chris Ziomkowski
4
 
5
This file is part of OpenRISC 1000 Architectural Simulator.
6
 
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
 
21
/* Establishes jtag proxy server and communicates with parallel
22
 port directly.  Requires root access. */
23
 
24
#include <stdio.h>
25
#include <ctype.h>
26
#include <string.h>
27
#include <stdlib.h>
28
#include <unistd.h>
29
#include <stdarg.h>
30
 
31
/* Dirty way to include inb and outb from, but they say it is
32
   a standard one.  */
33
#include <asm/io.h>
34
#include <asm/system.h>
35 588 simons
#include "mc.h"
36 435 simons
 
37
#define GDB_IN "../sim/rtl_sim/run/gdb_in.dat"
38
#define GDB_OUT "../sim/rtl_sim/run/gdb_out.dat"
39
 
40
/* Libraries for JTAG proxy server.  */
41
#include <sys/stat.h>
42
#include <sys/types.h>
43
#include <sys/socket.h>
44
#include <netinet/in.h>
45
#include <sys/select.h>
46
#include <sys/poll.h>
47
#include <fcntl.h>
48
#include <netdb.h>
49
#include <netinet/tcp.h>
50
#include <inttypes.h>
51
#include <errno.h>
52
 
53
#include "gdb.h" /* partially copied from gdb/config/or1k */
54
 
55
#ifdef DEBUG
56
#define debug printf
57
#define debug2 printf
58
#define flush_debug() fflush(stdout)
59
#else
60
#define debug
61
#define debug2 printf
62
#define flush_debug()
63
#endif
64
 
65
#define LPT_BASE (base)
66
#define LPT_READ (LPT_BASE+1)
67
#define LPT_WRITE LPT_BASE
68
#if RTL_SIM
69
#define TCLK_BIT (0x01) /* D0, pin #2 */
70
#define TRST_BIT (0x02) /* D1, pin #3 */
71
#define TDI_BIT  (0x04) /* D2, pin #4 */
72
#define TMS_BIT  (0x08) /* D0, pin #5 */
73
#define TDO_BIT  (0x20) /* PE, pin #12 */
74
#define TMS      (0x02)
75
#define TDI      (0x01)
76
#else
77 588 simons
#ifdef XILINX
78
#define TCLK_BIT (0x02) /* D1 pin 3 */
79
#define TRST_BIT (0x10) /* Not used */
80
#define TDI_BIT  (0x01) /* D0 pin 2 */
81
#define TMS_BIT  (0x04) /* D2 pin 4 */
82
#define TDO_BIT  (0x10) /* S6 pin 13*/
83
#define TMS      (0x02)
84
#define TDI      (0x01)
85
//#define TDO_INV
86
#else
87 435 simons
#define TCLK_BIT (0x04) /* D2 pin 4 */
88
#define TRST_BIT (0x08) /* D3 pin 5 */
89
#define TDI_BIT  (0x10) /* D4 pin 6 */
90
#define TMS_BIT  (0x20) /* D5 pin 7 */
91
#define TDO_BIT  (0x20) /* S5 pin 12*/
92
#define TMS      (0x02)
93
#define TDI      (0x01)
94
#endif
95 588 simons
#endif
96 435 simons
#ifdef RTL_SIM
97
# define JTAG_WAIT() usleep(1000)
98
# define NUM_RETRIES (16)
99
# define JTAG_RETRY_WAIT() usleep (1000)
100
#else
101
# define JTAG_WAIT() {      \
102
  int i;        \
103
  volatile int j;   \
104
  for(i = 0; i < 1000; i++) \
105
    j = i;      \
106
  }
107
# define NUM_RETRIES (16)
108
# define JTAG_RETRY_WAIT() usleep (1000)
109
#endif
110
 
111
/* Selects crc trailer size in bits. Currently supported: 8 */
112
#define CRC_SIZE (8)
113
 
114
/* Scan chain size in bits.  */
115
#define SC_SIZE (4)
116
 
117
#ifndef ULONGEST
118
#define ULONGEST unsigned long
119
#endif
120
 
121
typedef enum {
122
  false = 0,
123
  true = 1,
124
} Boolean;
125
 
126
static int base = 0x378; /* FIXME: We should detect the address. */
127
int err = 0;
128
int set_pc = 0;
129
int set_step = 0;
130
 
131
unsigned int serverIP = 0;
132
unsigned int serverPort = 0;
133
unsigned int server_fd = 0;
134
unsigned int gdb_fd = 0;
135
void HandleServerSocket(Boolean);
136
void JTAGRequest(void);
137
void GDBRequest(void);
138
void ProtocolClean(int,int32_t);
139
static int gdb_read(void*,int);
140
static int gdb_write(void*,int);
141
static void jtag_set_chain (int);
142
 
143
/* Scan chain info. */
144
/* *INDENT-OFF* */
145
static int chain_addr_size[] = { 0,  32, 0,  0,  5,  32, 32};
146
static int chain_data_size[] = { 0,  32, 0,  32, 32, 32, 32};
147
static int chain_is_valid[]  = { 0,  1,  0,  1,  1,  1,   1};
148
static int chain_has_crc[]   = { 0,  1,  0,  1,  1,  1,   1};
149
static int chain_has_rw[]    = { 0,  1,  0,  0,  1,  1,   1};
150
/* *INDENT-OFF* */
151
 
152
/* Currently selected scan chain - just to prevent unnecessary
153
   transfers. */
154
static int current_chain;
155
 
156
/* Designates whether we are in SELECT_DR state, otherwise in
157
   RUN TEST/IDLE */
158
static int select_dr = 0;
159
 
160
/* Crc of current read or written data.  */
161
static int crc_r, crc_w = 0;
162
 
163
/* Address of previous read */
164
static unsigned long prev_regno = 0;
165
 
166
/* Generates new crc, sending in new bit input_bit */
167
 
168
static int
169
crc_calc (int crc, int input_bit)
170
{
171
  int c;
172
  int new_crc;
173
  int d;
174
 
175
#if (CRC_SIZE == 8)
176
  d = input_bit&1;
177
  c = crc;
178
 
179
  /* Move queue left.  */
180
  new_crc = crc << 1;
181
 
182
  /* Mask upper five bits.  */
183
  new_crc &= 0xF8;
184
 
185
  /* Set lower three bits */
186
  new_crc |= (d ^ ((c >> 7)&1));
187
  new_crc |= (d ^ ((c >> 0)&1) ^ ((c >> 7)&1)) << 1;
188
  new_crc |= (d ^ ((c >> 1)&1) ^ ((c >> 7)&1)) << 2;
189
  return new_crc;
190
#else
191
  return 0;
192
#endif
193
}
194
 
195
/* Send a byte to parallel port. */
196
 
197
inline static void
198
jp1_out (unsigned int value) {
199
 
200
#ifdef RTL_SIM
201
  time_t time;
202
  struct stat s;
203
  char buf[1000];
204
  FILE *fout;
205
  unsigned num_read;
206
  int r;
207
  fout = fopen (GDB_IN, "wt+");
208
  fprintf (fout, "F\n");
209
  fclose (fout);
210
  fout = fopen (GDB_OUT, "wt+");
211
  fprintf (fout, "%02X\n", value);
212
  fclose (fout);
213
error:
214
  fout = fopen (GDB_OUT, "rt");
215
  r = fscanf(fout,"%x", &num_read);
216
  fclose (fout);
217
  if (r == 0 || num_read != (0x10 | value))
218
    goto error;
219
#else
220
  outb(value, LPT_WRITE);
221
#endif /* RTL_SIM */
222
  if  (!(value & 1))
223
    debug("[%x%c]", (value & TDI_BIT) != 0, (value & TMS_BIT)?'^':'_');
224
  flush_debug();
225
}
226
 
227
/* Receive a byte from parallel port.  */
228
 
229
inline static unsigned char
230
jp1_in () {
231
  int data;
232
#ifndef RTL_SIM
233
  data = inb (LPT_READ);
234 588 simons
#ifdef TDO_INV
235
  data = (data & TDO_BIT) != TDO_BIT;
236
#else
237 435 simons
  data = (data & TDO_BIT) == TDO_BIT;
238 588 simons
#endif
239 435 simons
#else
240
  FILE *fin = 0;
241
  char ch;
242
  time_t time;
243
  struct stat s;
244
  while (1) {
245
    fin = fopen (GDB_IN, "rt");
246
    if (fin == 0)
247
      continue;
248
    ch = fgetc (fin);
249
    if (ch != '0' && ch != '1') {
250
      fclose (fin);
251
      continue;
252
    } else break;
253
  }
254
  fclose (fin);
255
  data = ch == '1';
256
#endif /* !RTL_SIM */
257
  debug(" R%01X ", data);
258
  flush_debug();
259
  return data;
260
}
261
 
262
/* Writes TCLK=0, TRST=1, TMS=bit1, TDI=bit0
263
   and    TCLK=1, TRST=1, TMS=bit1, TDI=bit0 */
264
 
265
static inline void
266
jp1_write_JTAG (packet)
267
  unsigned char packet;
268
{
269
  unsigned char data = TRST_BIT;
270
  if (packet & 1)
271
    data |= TDI_BIT;
272
  if (packet & 2)
273
    data |= TMS_BIT;
274
 
275
  jp1_out (data);
276
  JTAG_WAIT();
277
  crc_w = crc_calc (crc_w, packet&1);
278
 
279
  /* rise clock */
280
  jp1_out (data | TCLK_BIT);
281
  JTAG_WAIT();
282
}
283
 
284
/* Reads TDI.  */
285
 
286
static inline int
287
jp1_read_JTAG ()
288
{
289
  int data;
290
  data = jp1_in ();
291
  crc_r = crc_calc (crc_r, data);
292
  return data;
293
}
294
 
295
/* Writes bitstream.  LS bit first.  */
296
 
297
static inline void
298
jp1_write_stream (stream, len, set_last_bit)
299
  ULONGEST stream;
300
  int len;
301
  int set_last_bit;
302
{
303
  int i;
304
  if (len <= 0) return;
305
  debug("\nwrite(");
306
  for (i = 0; i < len - 1; i++)
307
    jp1_write_JTAG ((stream >> i) & 1);
308
 
309
  if (set_last_bit)
310
    jp1_write_JTAG ((stream >> (len - 1))& 1 | TMS);
311
  else
312
    jp1_write_JTAG ((stream >> (len - 1))& 1);
313
  debug(")\n");
314
}
315
 
316
/* Gets bitstream.  LS bit first.  */
317
 
318
inline static ULONGEST
319
jp1_read_stream (stream, len, set_last_bit)
320
  unsigned long stream;
321
  int len;
322
  int set_last_bit;
323
{
324
  int i;
325
  ULONGEST data;
326
  debug("\nread(");
327
  if (len <= 0) return;
328
  data = 0;
329
  for (i = 0; i < len - 1; i++)
330
    {
331
      jp1_write_JTAG (stream & 1);   /* LSB first */
332
      stream >>= 1;
333
      data |= jp1_read_JTAG () << i; /* LSB first */
334
    }
335
 
336
  if (set_last_bit)
337
    jp1_write_JTAG (stream & 1 | TMS);
338
  else
339
    jp1_write_JTAG (stream & 1);
340
  data |= jp1_read_JTAG () << (len - 1);
341
  debug(")\n");
342
  return data;
343
}
344
 
345
/* Goes into SELECT_IR state. Should be called before every control write.  */
346
 
347
inline static void
348
jp1_prepare_control ()
349
{
350
  if (!select_dr)
351
    jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
352
  jp1_write_JTAG (TMS);   /* SELECT_IR SCAN */
353
  select_dr = 0;
354
}
355
 
356
/* Resets JTAG.
357
   Writes TRST=0
358
   and    TRST=1 */
359
 
360
static void
361
jp1_reset_JTAG ()
362
{
363
  int i;
364
  debug2 ("\nreset(");
365
  jp1_out (0);
366
  JTAG_RETRY_WAIT();
367 588 simons
  /* In case we don't have TRST reset it manually */
368
  for (i = 0; i < 8; i++)
369
    jp1_write_JTAG (TMS);
370 435 simons
  jp1_out (TRST_BIT);
371
  JTAG_RETRY_WAIT();
372
  jp1_write_JTAG (0);
373
  debug2(")\n");
374
  select_dr = 0;
375
}
376
 
377
/* Sets register/memory regno to data.  */
378
 
379
/* CZ 08/06/01: I am not sure how error checking is intended to
380
   be implemented here. It appears that no indication is returned
381
   to the caller as you have in standard unix system calls. Therefore,
382
   I guess the only way to use these functions when you want to know
383
   the exact position of the error is to manually clear err, call the
384
   function, and then manually check err. I have also made some changes
385
   where necessary because no value was returned at all int jtag_read_reg.
386
*/
387
 
388
static void
389
jtag_write_reg_support (regno, data)
390
  int regno;
391
  ULONGEST data;
392
{
393
  int crc_read, crc_write, crc_ok, retry;
394
  int result;
395
  int tmp;
396
 
397
  debug("\n");
398
  debug2("write_reg %i(%08x) <- %08x \n", regno, regno, data);
399
  if (!select_dr)
400
    jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
401
  select_dr = 1;
402
 
403
      /* If we don't have rw bit, we assume chain
404
   is read only. */
405
  if (!chain_has_rw[current_chain])
406
    error ("Internal: Chain not writable.");
407
 
408
  for (retry = 0; retry < NUM_RETRIES; retry++) {
409
    jp1_write_JTAG (0); /* CAPTURE_DR */
410
    jp1_write_JTAG (0); /* SHIFT_DR */
411
    crc_w = 0;
412
 
413
    /* write addr */
414
    jp1_write_stream (regno, chain_addr_size[current_chain], 0);
415
 
416
    /* write (R/W=1) - we tested that previously. */
417
    jp1_write_JTAG (TDI);
418
 
419
    if (chain_has_crc[current_chain])
420
      {
421
        /* write data */
422
        jp1_write_stream (data, chain_data_size[current_chain], 0);
423
        crc_write = crc_w;
424
 
425
        /* write CRC, EXIT1_DR */
426
        crc_read = jp1_read_stream (crc_write, CRC_SIZE + 1, 1) >> 1;
427
      }
428
    else
429
      {
430
        /* write data */
431
        jp1_write_stream (data, chain_data_size[current_chain], 1);
432
      }
433
    jp1_write_JTAG (TMS); /* UPDATE_DR */
434
    jp1_write_JTAG (TMS); /* SELECT_DR */
435
 
436
    /* Did JTAG receive packet correctly? */
437
    if (chain_has_crc[current_chain])
438
      crc_ok = crc_read == crc_write;
439
 
440
    if (chain_has_crc[current_chain])
441
      {
442
        if (crc_ok)
443
          return;
444
 
445
        debug2(", crc failed. read %08x, generated %08x\n", crc_read, crc_write);
446
        jp1_reset_JTAG();
447
        jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
448
        select_dr = 1;
449
        tmp = current_chain;
450
        current_chain = -1;
451
        jtag_set_chain(tmp);
452
      }
453
    else
454
      return;
455
  }
456
  printf ("Invalid CRC\n");
457
  err = ERR_CRC;
458
}
459
 
460
/* Reads register/memory from regno.
461
   Reading is a bit strange. Data is not available
462
   at the time we pass an address, but in successive
463
   read instead. Call jtag_read_reg twice to get correct
464
   data. */
465
 
466
static ULONGEST
467
jtag_read_reg (regno)
468
  unsigned int regno;
469
{
470
  ULONGEST data;
471
  int crc_read, crc_write, crc_actual_read,  retry, crc_ok;
472
  int result;
473
  int tmp;
474
 
475
  debug("\n");
476
  debug2("read_reg %i(%08x)", regno, regno);
477
  debug (" \n ");
478
  if (!select_dr)
479
    jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
480
  select_dr = 1;
481
 
482
  for (retry = 0; retry < NUM_RETRIES; retry++) {
483
    jp1_write_JTAG (0); /* CAPTURE_DR */
484
    jp1_write_JTAG (0); /* SHIFT_DR */
485
    crc_w = 0;
486
 
487
    /* write addr */
488
    jp1_write_stream (regno, chain_addr_size[current_chain], 0);
489
 
490
    /* read (R/W=0) */
491
    if (chain_has_rw[current_chain])
492
      jp1_write_JTAG (0);
493
    if (chain_has_crc[current_chain])
494
      {
495
        crc_r = 0;
496
 
497
        /* data = 0 */
498
        data = jp1_read_stream (0, chain_data_size[current_chain], 0);
499
        crc_write = crc_w;
500
        crc_actual_read = crc_r;
501
 
502
        /* Send my crc, EXIT1_DR */
503
        crc_read = jp1_read_stream (crc_write, CRC_SIZE, 1);
504
      } else {
505
        /* data = 0 */
506
        data = jp1_read_stream (0, chain_data_size[current_chain], 1);
507
      }
508
    jp1_write_JTAG (TMS); /* UPDATE_DR */
509
    jp1_write_JTAG (TMS); /* SELECT_DR */
510
 
511
    /* Did JTAG receive packet correctly? */
512
    if (chain_has_crc[current_chain])
513
      crc_ok = jp1_read_JTAG ();
514
 
515
    if (chain_has_crc[current_chain])
516
      {
517
        if ((crc_read == crc_actual_read) && (crc_ok)) {
518
          debug2(" , read_reg %i(%08x) = %08x\n", regno, regno, data);
519
          prev_regno = regno;
520
          return data;
521
        }
522
        debug2(", crc failed. read %08x, generated %08x\n", crc_read, crc_actual_read);
523
        jp1_reset_JTAG();
524
        jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
525
        select_dr = 1;
526
        tmp = current_chain;
527
        current_chain = -1;
528
        jtag_set_chain(tmp);
529
        jtag_read_reg (prev_regno);
530
        if (err) return -1;
531
      }
532
    else {
533
      debug2(" , read_reg %i(%08x) = %08x\n", regno, regno, data);
534
      prev_regno = regno;
535
      return data;
536
    }
537
  }
538
  printf ("Invalid CRC\n");
539
  err = ERR_CRC;
540
  return -1;
541
}
542
 
543
/* Sets scan chain.  */
544
 
545
static void
546
jtag_set_chain (chain)
547
  int chain;
548
{
549
  int crc_read, crc_write, crc_ok, retry;
550
  int result;
551
 
552
  debug("\n");
553
  debug2("set_chain %i\n", chain);
554
  if (current_chain != chain) {
555
    if (!chain_is_valid[chain])
556
      error ("Chain not valid.");
557
 
558
    current_chain = chain;
559
    jp1_prepare_control ();
560
 
561
    while (1) {
562
      jp1_write_JTAG (0); /* CAPTURE_IR */
563
      jp1_write_JTAG (0); /* SHIFT_IR */
564
 
565
      /* write data, EXIT1_IR */
566
      jp1_write_stream (JI_CHAIN_SELECT, JI_SIZE, 1);
567
 
568
      jp1_write_JTAG (TMS); /* UPDATE_IR */
569
      jp1_write_JTAG (TMS); /* SELECT_DR */
570
 
571
      jp1_write_JTAG (0); /* CAPTURE_DR */
572
      jp1_write_JTAG (0); /* SHIFT_DR */
573
 
574
      if (chain_has_crc[current_chain])
575
        {
576
          crc_w = 0;
577
          /* write data */
578
          jp1_write_stream (chain, SC_SIZE, 0);
579
 
580
          crc_write = crc_w;
581
          /* write CRC, EXIT1_DR */
582
          crc_read = jp1_read_stream (crc_write, CRC_SIZE + 1, 1) >> 1;
583
        } else {
584
          /* write data, EXIT1_DR */
585
          jp1_write_stream (chain, SC_SIZE, 1);
586
        }
587
      jp1_write_JTAG (TMS); /* UPDATE_DR */
588
      jp1_write_JTAG (TMS); /* SELECT_DR */
589
 
590
      /* Did JTAG receive packet correctly? */
591
      if (chain_has_crc[current_chain])
592
        crc_ok = crc_read == crc_write;
593
 
594
      if (chain_has_crc[current_chain])
595
        {
596
          if (!crc_ok)
597
            {
598
              debug2(", crc failed.\n");
599
              jp1_reset_JTAG();
600
              jp1_prepare_control ();
601
              continue;
602
            }
603
        }
604
 
605
      jp1_write_JTAG (TMS); /* SELECT_IR */
606
      jp1_write_JTAG (0); /* CAPTURE_IR */
607
      jp1_write_JTAG (0); /* SHIFT_IR */
608
      crc_w = 0;
609
 
610
      /* write data, EXIT1_IR */
611
      jp1_write_stream (JI_DEBUG, JI_SIZE, 1);
612
 
613
      jp1_write_JTAG (TMS); /* UPDATE_IR */
614
      jp1_write_JTAG (TMS); /* SELECT_DR */
615
      select_dr = 1;
616
 
617
      return;
618
    }
619
    printf ("Invalid CRC\n");
620
    err = ERR_CRC;
621
  } else
622
    debug2 ("Already set.\n");
623
}
624
 
625
/* Sets register/memory regno to data.  */
626
 
627
static void
628
jtag_write_reg (regno, data)
629
  int regno;
630
  ULONGEST data;
631
{
632
  /* Set PC */
633 588 simons
//  if (current_chain == SC_RISC_DEBUG && regno == 0x10)
634
//    data = data - 4;
635 435 simons
 
636
  jtag_write_reg_support (regno, data);
637
}
638
 
639
/* Stalls the CPU.  */
640
 
641
static void
642
or1k_stall ()
643
{
644
  int val;
645
  jtag_set_chain (SC_REGISTER);
646
  val = jtag_read_reg (JTAG_RISCOP);
647
  jtag_write_reg (JTAG_RISCOP, val | 1);
648
}
649
 
650
/* Unstalls the CPU.  */
651
 
652
static void
653
or1k_unstall ()
654
{
655
  unsigned int val;
656
 
657
  jtag_set_chain (SC_REGISTER);
658
  val = jtag_read_reg (JTAG_RISCOP);
659
  jtag_write_reg (JTAG_RISCOP, val & ~1);
660
}
661
 
662
/* Initialize a new connection to the or1k board, and make sure we are
663
   really connected.  */
664
 
665
static int
666
jtag_init () {
667
  int tmp, i;
668
  unsigned int npc, ppc, r1, insn, result;
669
  current_chain = -1;
670
  jp1_reset_JTAG ();
671 588 simons
#if 0
672
#define MC_BASE_ADD     0x60000000
673
#define MC_CSR_VAL      0x04300300
674
#define MC_MASK_VAL     0x000000ff
675
#define FLASH_BASE_ADD  0x04000000
676
#define FLASH_TMS_VAL   0x0010a10a
677
#define SDRAM_BASE_ADD  0x00000000
678
#define SDRAM_TMS_VAL   0x07248230
679 435 simons
 
680 588 simons
  jtag_set_chain (SC_REGISTER);
681
  jtag_write_reg (4, 0x00000001);
682
 
683
  jtag_set_chain (SC_WISHBONE);
684
  jtag_write_reg (MC_BASE_ADD + MC_CSC(0), (((FLASH_BASE_ADD & 0xffff0000) >> 5) | 0x25));
685
  jtag_write_reg (MC_BASE_ADD + MC_TMS(0), FLASH_TMS_VAL);
686
 
687
  jtag_write_reg (MC_BASE_ADD + MC_BA_MASK, MC_MASK_VAL);
688
  jtag_write_reg (MC_BASE_ADD + MC_CSR, MC_CSR_VAL);
689
 
690
  jtag_write_reg (MC_BASE_ADD + MC_TMS(1), SDRAM_TMS_VAL);
691
  jtag_write_reg (MC_BASE_ADD + MC_CSC(1), (((SDRAM_BASE_ADD & 0xffff0000) >> 5) | 0x0411));
692
 
693
  sleep(1);
694
#endif
695
 
696 435 simons
#if 1
697
 
698 588 simons
#define RAM_BASE 0x00000000
699 435 simons
  /* Stall risc */
700
  jtag_set_chain (SC_REGISTER);
701
  jtag_write_reg (4, 0x00000001);
702
 
703
  jtag_set_chain (SC_WISHBONE);
704
  jtag_write_reg (RAM_BASE + 0x00, 0x9c200000);                                /* l.addi  r1,r0,0x0       */
705
  jtag_write_reg (RAM_BASE + 0x04, 0x18400000 + (RAM_BASE >> 16));             /* l.movhi r2,0x4000       */
706
  jtag_write_reg (RAM_BASE + 0x08, 0xa8420000 + ((RAM_BASE + 0x30) & 0xffff)); /* l.ori   r2,r2,0x0000    */
707
  jtag_write_reg (RAM_BASE + 0x0c, 0x9c210001);                                /* l.addi  r1,r1,1         */
708
  jtag_write_reg (RAM_BASE + 0x10, 0x9c210001);                                /* l.addi  r1,r1,1         */
709
  jtag_write_reg (RAM_BASE + 0x14, 0xd4020800);                                /* l.sw    0(r2),r1        */
710
  jtag_write_reg (RAM_BASE + 0x18, 0x9c210001);                                /* l.addi  r1,r1,1         */
711
  jtag_write_reg (RAM_BASE + 0x1c, 0x84620000);                                /* l.lwz   r3,0(r2)        */
712
  jtag_write_reg (RAM_BASE + 0x20, 0x03fffffb);                                /* l.j     loop2           */
713
  jtag_write_reg (RAM_BASE + 0x24, 0xe0211800);                                /* l.add   r1,r1,r3        */
714
  jtag_write_reg (RAM_BASE + 0x24, 0xe0211800);                                /* l.add   r1,r1,r3        */
715
 
716
  /* Enable exceptions */
717
  jtag_set_chain (SC_RISC_DEBUG);
718
  jtag_write_reg ((0 << 11) + 17, 0x03);
719
 
720
  /* Trap causes stall */
721
  jtag_set_chain (SC_RISC_DEBUG);
722
  jtag_write_reg ((6 << 11) + 20, 0x2000);
723
 
724
  /* Set PC */
725
  jtag_set_chain (SC_RISC_DEBUG);
726
  jtag_write_reg ((0 << 11) + 16, RAM_BASE);
727
 
728
  /* Set step bit */
729
  jtag_set_chain (SC_RISC_DEBUG);
730
  jtag_write_reg ((6 << 11) + 16, 1 << 22);
731
 
732
 
733
  for (i = 0; i < 10; i++)
734
    {
735
      /* Unstall */
736
      jtag_set_chain (SC_REGISTER);
737
      jtag_write_reg (4, 0x00000000);
738
      jtag_set_chain (SC_RISC_DEBUG);
739
    }
740
 
741
  /* Read NPC */
742
  jtag_set_chain (SC_RISC_DEBUG);
743
  npc = jtag_read_reg ((0 << 11) + 16);
744
  npc = jtag_read_reg ((0 << 11) + 16);
745
 
746
  /* Read PPC */
747
  jtag_set_chain (SC_RISC_DEBUG);
748
  ppc = jtag_read_reg ((0 << 11) + 18);
749
  ppc = jtag_read_reg ((0 << 11) + 18);
750
 
751
  /* Read R1 */
752
  jtag_set_chain (SC_RISC_DEBUG);
753
  r1 = jtag_read_reg (0x401);
754
  r1 = jtag_read_reg (0x401);
755
 
756 588 simons
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
757
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000000c, 0x40000024, 5);
758 435 simons
  result = npc + ppc + r1;
759
 
760
 
761
  /* Reset step bit */
762
  jtag_set_chain (SC_RISC_DEBUG);
763
  jtag_write_reg ((6 << 11) + 16, 0);
764
 
765
 
766
 
767
  /* Set trap insn in delay slot */
768
  jtag_set_chain (SC_WISHBONE);
769
  insn = jtag_read_reg (RAM_BASE + 0x24);
770
  insn = jtag_read_reg (RAM_BASE + 0x24);
771
  jtag_write_reg (RAM_BASE + 0x24, 0x21000001);
772
 
773
  /* Unstall */
774
  jtag_set_chain (SC_REGISTER);
775
  jtag_write_reg (4, 0x00000000);
776
  jtag_set_chain (SC_RISC_DEBUG);
777
 
778
  /* Read NPC */
779
  jtag_set_chain (SC_RISC_DEBUG);
780
  npc = jtag_read_reg ((0 << 11) + 16);
781
  npc = jtag_read_reg ((0 << 11) + 16);
782
 
783
  /* Read PPC */
784
  jtag_set_chain (SC_RISC_DEBUG);
785
  ppc = jtag_read_reg ((0 << 11) + 18);
786
  ppc = jtag_read_reg ((0 << 11) + 18);
787
 
788
  /* Read R1 */
789
  jtag_set_chain (SC_RISC_DEBUG);
790
  r1 = jtag_read_reg (0x401);
791
  r1 = jtag_read_reg (0x401);
792
 
793
  /* Set back original insn */
794
  jtag_set_chain (SC_WISHBONE);
795
  jtag_write_reg (RAM_BASE + 0x24, insn);
796
 
797 588 simons
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
798
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000000c, 0x40000024, 8);
799 435 simons
  result = npc + ppc + r1 + result;
800
 
801
 
802
  /* Set trap insn in place of branch insn */
803
  jtag_set_chain (SC_WISHBONE);
804
  insn = jtag_read_reg (RAM_BASE + 0x20);
805
  insn = jtag_read_reg (RAM_BASE + 0x20);
806
  jtag_write_reg (RAM_BASE + 0x20, 0x21000001);
807
 
808
  /* Set PC */
809
  jtag_set_chain (SC_RISC_DEBUG);
810
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x0c);
811
 
812
  /* Unstall */
813
  jtag_set_chain (SC_REGISTER);
814
  jtag_write_reg (4, 0x00000000);
815
  jtag_set_chain (SC_RISC_DEBUG);
816
 
817
  /* Read NPC */
818
  jtag_set_chain (SC_RISC_DEBUG);
819
  npc = jtag_read_reg ((0 << 11) + 16);
820
  npc = jtag_read_reg ((0 << 11) + 16);
821
 
822
  /* Read PPC */
823
  jtag_set_chain (SC_RISC_DEBUG);
824
  ppc = jtag_read_reg ((0 << 11) + 18);
825
  ppc = jtag_read_reg ((0 << 11) + 18);
826
 
827
  /* Read R1 */
828
  jtag_set_chain (SC_RISC_DEBUG);
829
  r1 = jtag_read_reg (0x401);
830
  r1 = jtag_read_reg (0x401);
831
 
832
  /* Set back original insn */
833
  jtag_set_chain (SC_WISHBONE);
834
  jtag_write_reg (RAM_BASE + 0x20, insn);
835
 
836 588 simons
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
837
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000024, 0x40000020, 11);
838 435 simons
  result = npc + ppc + r1 + result;
839
 
840
 
841
  /* Set trap insn before branch insn */
842
  jtag_set_chain (SC_WISHBONE);
843
  insn = jtag_read_reg (RAM_BASE + 0x1c);
844
  insn = jtag_read_reg (RAM_BASE + 0x1c);
845
  jtag_write_reg (RAM_BASE + 0x1c, 0x21000001);
846
 
847
  /* Set PC */
848
  jtag_set_chain (SC_RISC_DEBUG);
849
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x20);
850
 
851
  /* Unstall */
852
  jtag_set_chain (SC_REGISTER);
853
  jtag_write_reg (4, 0x00000000);
854
  jtag_set_chain (SC_RISC_DEBUG);
855
 
856
  /* Read NPC */
857
  jtag_set_chain (SC_RISC_DEBUG);
858
  npc = jtag_read_reg ((0 << 11) + 16);
859
  npc = jtag_read_reg ((0 << 11) + 16);
860
 
861
  /* Read PPC */
862
  jtag_set_chain (SC_RISC_DEBUG);
863
  ppc = jtag_read_reg ((0 << 11) + 18);
864
  ppc = jtag_read_reg ((0 << 11) + 18);
865
 
866
  /* Read R1 */
867
  jtag_set_chain (SC_RISC_DEBUG);
868
  r1 = jtag_read_reg (0x401);
869
  r1 = jtag_read_reg (0x401);
870
 
871
  /* Set back original insn */
872
  jtag_set_chain (SC_WISHBONE);
873
  jtag_write_reg (RAM_BASE + 0x1c, insn);
874
 
875 588 simons
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
876
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000020, 0x4000001c, 24);
877 435 simons
  result = npc + ppc + r1 + result;
878
 
879
 
880
  /* Set trap insn behind lsu insn */
881
  jtag_set_chain (SC_WISHBONE);
882
  insn = jtag_read_reg (RAM_BASE + 0x18);
883
  insn = jtag_read_reg (RAM_BASE + 0x18);
884
  jtag_write_reg (RAM_BASE + 0x18, 0x21000001);
885
 
886
  /* Set PC */
887
  jtag_set_chain (SC_RISC_DEBUG);
888
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x1c);
889
 
890
  /* Unstall */
891
  jtag_set_chain (SC_REGISTER);
892
  jtag_write_reg (4, 0x00000000);
893
  jtag_set_chain (SC_RISC_DEBUG);
894
 
895
  /* Read NPC */
896
  jtag_set_chain (SC_RISC_DEBUG);
897
  npc = jtag_read_reg ((0 << 11) + 16);
898
  npc = jtag_read_reg ((0 << 11) + 16);
899
 
900
  /* Read PPC */
901
  jtag_set_chain (SC_RISC_DEBUG);
902
  ppc = jtag_read_reg ((0 << 11) + 18);
903
  ppc = jtag_read_reg ((0 << 11) + 18);
904
 
905
  /* Read R1 */
906
  jtag_set_chain (SC_RISC_DEBUG);
907
  r1 = jtag_read_reg (0x401);
908
  r1 = jtag_read_reg (0x401);
909
 
910
  /* Set back original insn */
911
  jtag_set_chain (SC_WISHBONE);
912
  jtag_write_reg (RAM_BASE + 0x18, insn);
913
 
914 588 simons
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
915
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000001c, 0x40000018, 49);
916 435 simons
  result = npc + ppc + r1 + result;
917
 
918
  /* Set trap insn very near previous one */
919
  jtag_set_chain (SC_WISHBONE);
920
  insn = jtag_read_reg (RAM_BASE + 0x1c);
921
  insn = jtag_read_reg (RAM_BASE + 0x1c);
922
  jtag_write_reg (RAM_BASE + 0x1c, 0x21000001);
923
 
924
  /* Set PC */
925
  jtag_set_chain (SC_RISC_DEBUG);
926
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x18);
927
 
928
  /* Unstall */
929
  jtag_set_chain (SC_REGISTER);
930
  jtag_write_reg (4, 0x00000000);
931
  jtag_set_chain (SC_RISC_DEBUG);
932
 
933
  /* Read NPC */
934
  jtag_set_chain (SC_RISC_DEBUG);
935
  npc = jtag_read_reg ((0 << 11) + 16);
936
  npc = jtag_read_reg ((0 << 11) + 16);
937
 
938
  /* Read PPC */
939
  jtag_set_chain (SC_RISC_DEBUG);
940
  ppc = jtag_read_reg ((0 << 11) + 18);
941
  ppc = jtag_read_reg ((0 << 11) + 18);
942
 
943
  /* Read R1 */
944
  jtag_set_chain (SC_RISC_DEBUG);
945
  r1 = jtag_read_reg (0x401);
946
  r1 = jtag_read_reg (0x401);
947
 
948
  /* Set back original insn */
949
  jtag_set_chain (SC_WISHBONE);
950
  jtag_write_reg (RAM_BASE + 0x1c, insn);
951
 
952 588 simons
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
953
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000020, 0x4000001c, 50);
954 435 simons
  result = npc + ppc + r1 + result;
955
 
956
 
957
  /* Set trap insn to the start */
958
  jtag_set_chain (SC_WISHBONE);
959
  insn = jtag_read_reg (RAM_BASE + 0x0c);
960
  insn = jtag_read_reg (RAM_BASE + 0x0c);
961
  jtag_write_reg (RAM_BASE + 0x0c, 0x21000001);
962
 
963
  /* Set PC */
964
  jtag_set_chain (SC_RISC_DEBUG);
965
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x1c);
966
 
967
  /* Unstall */
968
  jtag_set_chain (SC_REGISTER);
969
  jtag_write_reg (4, 0x00000000);
970
  jtag_set_chain (SC_RISC_DEBUG);
971
 
972
  /* Read NPC */
973
  jtag_set_chain (SC_RISC_DEBUG);
974
  npc = jtag_read_reg ((0 << 11) + 16);
975
  npc = jtag_read_reg ((0 << 11) + 16);
976
 
977
  /* Read PPC */
978
  jtag_set_chain (SC_RISC_DEBUG);
979
  ppc = jtag_read_reg ((0 << 11) + 18);
980
  ppc = jtag_read_reg ((0 << 11) + 18);
981
 
982
  /* Read R1 */
983
  jtag_set_chain (SC_RISC_DEBUG);
984
  r1 = jtag_read_reg (0x401);
985
  r1 = jtag_read_reg (0x401);
986
 
987
  /* Set back original insn */
988
  jtag_set_chain (SC_WISHBONE);
989
  jtag_write_reg (RAM_BASE + 0x0c, insn);
990
 
991 588 simons
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
992
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000010, 0x4000000c, 99);
993 435 simons
  result = npc + ppc + r1 + result;
994
 
995
 
996 588 simons
  /* Set step bit */
997
  jtag_set_chain (SC_RISC_DEBUG);
998
  jtag_write_reg ((6 << 11) + 16, 1 << 22);
999
 
1000
  for (i = 0; i < 5; i++)
1001
    {
1002
      /* Unstall */
1003
      jtag_set_chain (SC_REGISTER);
1004
      jtag_write_reg (4, 0x00000000);
1005
      jtag_set_chain (SC_RISC_DEBUG);
1006
    }
1007
 
1008
  /* Read NPC */
1009
  jtag_set_chain (SC_RISC_DEBUG);
1010
  npc = jtag_read_reg ((0 << 11) + 16);
1011
  npc = jtag_read_reg ((0 << 11) + 16);
1012
 
1013
  /* Read PPC */
1014
  jtag_set_chain (SC_RISC_DEBUG);
1015
  ppc = jtag_read_reg ((0 << 11) + 18);
1016
  ppc = jtag_read_reg ((0 << 11) + 18);
1017
 
1018
  /* Read R1 */
1019
  jtag_set_chain (SC_RISC_DEBUG);
1020
  r1 = jtag_read_reg (0x401);
1021
  r1 = jtag_read_reg (0x401);
1022
 
1023
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
1024
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x40000024, 0x40000020, 101);
1025
  result = npc + ppc + r1 + result;
1026
 
1027
  /* Set PC */
1028
  jtag_set_chain (SC_RISC_DEBUG);
1029
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x20);
1030
 
1031
  for (i = 0; i < 2; i++)
1032
    {
1033
      /* Unstall */
1034
      jtag_set_chain (SC_REGISTER);
1035
      jtag_write_reg (4, 0x00000000);
1036
      jtag_set_chain (SC_RISC_DEBUG);
1037
    }
1038
 
1039
  /* Read NPC */
1040
  jtag_set_chain (SC_RISC_DEBUG);
1041
  npc = jtag_read_reg ((0 << 11) + 16);
1042
  npc = jtag_read_reg ((0 << 11) + 16);
1043
 
1044
  /* Read PPC */
1045
  jtag_set_chain (SC_RISC_DEBUG);
1046
  ppc = jtag_read_reg ((0 << 11) + 18);
1047
  ppc = jtag_read_reg ((0 << 11) + 18);
1048
 
1049
  /* Read R1 */
1050
  jtag_set_chain (SC_RISC_DEBUG);
1051
  r1 = jtag_read_reg (0x401);
1052
  r1 = jtag_read_reg (0x401);
1053
 
1054
  printf("Read      npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
1055
  printf("Expected  npc = %.8lx ppc = %.8lx r1 = %.8lx\n", 0x4000000c, 0x40000024, 201);
1056
  result = npc + ppc + r1 + result;
1057
 
1058
  printf("result = %.8lx\n", result + 0x5eaddaa9);
1059
 
1060 435 simons
#endif
1061
 
1062
  return err;
1063
}
1064
 
1065
main(argc, argv)
1066
  int argc;
1067
  char *argv[];
1068
{
1069
  char *redirstr;
1070
  int trace_fd = 0;
1071
  char *s;
1072
 
1073
  srand(getpid());
1074
  if (argc != 2) {
1075
    printf("JTAG protocol via parallel port for linux.\n");
1076
    printf("Copyright (C) 2001 Marko Mlinar, markom@opencores.org\n\n");
1077
    printf("Usage: %s JTAG port_number\n", argv[0]);
1078
    return -1;
1079
  }
1080
 
1081
#ifndef RTL_SIM
1082
  if (ioperm(LPT_BASE, 3, 1)) {
1083
    fprintf(stderr, "Couldn't get the port at %x\n", LPT_BASE);
1084
    perror("Root privileges are required.\n");
1085
    return -1;
1086
  }
1087
  printf("Connected to parallel port at %x\n", LPT_BASE);
1088
#else
1089
  {
1090
    FILE *fin = fopen (GDB_IN, "wt+");
1091
    if(fin == 0) {
1092
      fprintf(stderr, "Can not open %s\n", GDB_IN);
1093
      exit(1);
1094
    }
1095
    fclose(fin);
1096
 
1097
  }
1098
#endif
1099
#ifndef RTL_SIM
1100
  /* Get rid of root privileges.  */
1101
  setreuid(getuid(), getuid());
1102
#endif
1103
 
1104
  /* Test the connection.  */
1105
  if (jtag_init()) {
1106
    fprintf(stderr,"Connection with jtag via parallel port failed.\n");
1107
    exit(-1);
1108
  }
1109
 
1110
  /* We have a connection.  Establish server.  */
1111
  argv++; argc--;
1112
  printf ("Dropping root privileges.\n");
1113
  serverPort = strtol(*(argv),&s,10);
1114
  if(*s)
1115
    return -1;
1116
  argv++; argc--;
1117
 
1118
  if(server_fd = GetServerSocket("or1ksim","tcp", serverPort)) {
1119
    printf("JTAG Proxy server started on port %d\n", serverPort);
1120
    printf("Press CTRL+c to exit.\n");
1121
  } else {
1122
    fprintf(stderr,"Cannot start JTAG Proxy server on port %d\n", serverPort);
1123
    exit(-1);
1124
  }
1125
 
1126
  /* Do endless loop of checking.  */
1127
  while(1) {
1128
    /* Handle GDB requests.  Ctrl-c exits.  */
1129
    HandleServerSocket(true);
1130
    /* Do some waiting to reduce load on CPU and should not be too long reply time also.  */
1131
    usleep(10);
1132
  }
1133
}
1134
 
1135
/************************
1136
   JTAG Server Routines
1137
 ************************/
1138
 
1139
static int tcp_level = 0;
1140
 
1141
/* Added by CZ 24/05/01 */
1142
int GetServerSocket(const char* name,const char* proto,int port)
1143
{
1144
  struct servent *service;
1145
  struct protoent *protocol;
1146
  struct sockaddr_in sa;
1147
  struct hostent *hp;
1148
  int sockfd;
1149
  char myname[256];
1150
  int flags;
1151
  char sTemp[256];
1152
 
1153
  /* First, get the protocol number of TCP */
1154
  if(!(protocol = getprotobyname(proto)))
1155
    {
1156
      sprintf(sTemp,"Unable to load protocol \"%s\"",proto);
1157
      perror(sTemp);
1158
      return 0;
1159
    }
1160
  tcp_level = protocol->p_proto; /* Save for later */
1161
 
1162
  /* If we weren't passed a non standard port, get the port
1163
     from the services directory. */
1164
  if(!port)
1165
    {
1166
      if(service = getservbyname(name,protocol->p_name))
1167
  port = ntohs(service->s_port);
1168
    }
1169
 
1170
  /* Create the socket using the TCP protocol */
1171
  if((sockfd = socket(PF_INET,SOCK_STREAM,protocol->p_proto)) < 0)
1172
    {
1173
      perror("Unable to create socket");
1174
      return 0;
1175
    }
1176
 
1177
  flags = 1;
1178
  if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&flags,sizeof(int))
1179
 < 0)
1180
    {
1181
      sprintf(sTemp,"Can not set SO_REUSEADDR option on socket %d",sockfd);
1182
      perror(sTemp);
1183
      close(sockfd);
1184
      return 0;
1185
    }
1186
 
1187
  /* The server should also be non blocking. Get the current flags. */
1188
  if(fcntl(sockfd,F_GETFL,&flags) < 0)
1189
    {
1190
      sprintf(sTemp,"Unable to get flags for socket %d",sockfd);
1191
      perror(sTemp);
1192
      close(sockfd);
1193
      return 0;
1194
    }
1195
 
1196
  /* Set the nonblocking flag */
1197
  if(fcntl(sockfd,F_SETFL, flags | O_NONBLOCK) < 0)
1198
    {
1199
      sprintf(sTemp,"Unable to set flags for socket %d to value 0x%08x",
1200
        sockfd,flags | O_NONBLOCK);
1201
      perror(sTemp);
1202
      close(sockfd);
1203
      return 0;
1204
    }
1205
 
1206
  /* Find out what our address is */
1207
  memset(&sa,0,sizeof(struct sockaddr_in));
1208
  gethostname(myname,sizeof(myname));
1209
  if(!(hp = gethostbyname(myname)))
1210
    {
1211
      perror("Unable to read hostname");
1212
      close(sockfd);
1213
      return 0;
1214
    }
1215
 
1216
  /* Bind our socket to the appropriate address */
1217
  sa.sin_family = hp->h_addrtype;
1218
  sa.sin_port = htons(port);
1219
  if(bind(sockfd,(struct sockaddr*)&sa,sizeof(struct sockaddr_in)) < 0)
1220
    {
1221
      sprintf(sTemp,"Unable to bind socket %d to port %d",sockfd,port);
1222
      perror(sTemp);
1223
      close(sockfd);
1224
      return 0;
1225
    }
1226
  serverIP = sa.sin_addr.s_addr;
1227
  flags = sizeof(struct sockaddr_in);
1228
  if(getsockname(sockfd,(struct sockaddr*)&sa,&flags) < 0)
1229
    {
1230
      sprintf(sTemp,"Unable to get socket information for socket %d",sockfd);
1231
      perror(sTemp);
1232
      close(sockfd);
1233
      return 0;
1234
    }
1235
  serverPort = ntohs(sa.sin_port);
1236
 
1237
  /* Set the backlog to 1 connections */
1238
  if(listen(sockfd,1) < 0)
1239
    {
1240
      sprintf(sTemp,"Unable to set backlog on socket %d to %d",sockfd,1);
1241
      perror(sTemp);
1242
      close(sockfd);
1243
      return 0;
1244
    }
1245
 
1246
  return sockfd;
1247
}
1248
 
1249
void HandleServerSocket(Boolean block)
1250
{
1251
  struct pollfd fds[3];
1252
  int n = 0;
1253
  int timeout = block ? -1 : 0;
1254
  int server_index = -1;
1255
  int gdb_index = -1;
1256
  Boolean data_on_stdin = false;
1257
  int o_serv_fd = server_fd;
1258
 
1259
  if(!o_serv_fd && !gdb_fd)
1260
    return;
1261
 
1262
  if(o_serv_fd)
1263
    {
1264
      fds[n].fd = o_serv_fd;
1265
      fds[n].events = POLLIN;
1266
      fds[n++].revents = 0;
1267
    }
1268
  if(gdb_fd)
1269
    {
1270
      fds[n].fd = gdb_fd;
1271
      fds[n].events = POLLIN;
1272
      fds[n++].revents = 0;
1273
    }
1274
  if(block)
1275
    {
1276
      fds[n].fd = 0;
1277
      fds[n].events = POLLIN;
1278
      fds[n++].revents = 0;
1279
    }
1280
 
1281
  while(!data_on_stdin)
1282
    {
1283
      switch(poll(fds,n,timeout))
1284
        {
1285
          case -1:
1286
            if(errno == EINTR)
1287
              continue;
1288
            perror("poll");
1289
            server_fd = 0;
1290
            break;
1291
          case 0: /* Nothing interesting going on */
1292
            data_on_stdin = true; /* Can only get here if nonblocking */
1293
            break;
1294
          default:
1295
            /* Make sure to handle the gdb port first! */
1296
            if((fds[0].revents && (gdb_fd && !o_serv_fd) ||
1297
                fds[1].revents && (server_fd && gdb_fd)))
1298
              {
1299
                int revents = o_serv_fd ? fds[1].revents : fds[0].revents;
1300
 
1301
                if(revents & POLLIN)
1302
                   GDBRequest();
1303
                else /* Error Occurred */
1304
                  {
1305
                    fprintf(stderr,"Received flags 0x%08x on gdb socket. Shutting down.\n",revents);
1306
                    close(gdb_fd);
1307
                    gdb_fd = 0;
1308
                  }
1309
               }
1310
            if(fds[0].revents && o_serv_fd)
1311
              {
1312
                if(fds[0].revents & POLLIN)
1313
                  JTAGRequest();
1314
                else /* Error Occurred */
1315
                  {
1316
                    fprintf(stderr,"Received flags 0x%08x on server. Shutting down.\n",fds[0].revents);
1317
                    close(o_serv_fd);
1318
                    server_fd = 0;
1319
                    serverPort = 0;
1320
                    serverIP = 0;
1321
                  }
1322
              }
1323
            if(fds[2].revents || (fds[1].revents && !gdb_fd))
1324
              data_on_stdin = true;
1325
            break;
1326
        } /* End of switch statement */
1327
    } /* End of while statement */
1328
}
1329
 
1330
void JTAGRequest()
1331
{
1332
  struct sockaddr_in sa;
1333
  struct sockaddr* addr = (struct sockaddr*)&sa;
1334
  int n = sizeof(struct sockaddr_in);
1335
  int fd = accept(server_fd,addr,&n);
1336
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
1337
  int flags;
1338
  char sTemp[256];
1339
 
1340
  if(fd < 0)
1341
    {
1342
      /* This is valid, because a connection could have started,
1343
         and then terminated due to a protocol error or user
1344
         initiation before the accept could take place. */
1345
      if(errno != EWOULDBLOCK && errno != EAGAIN)
1346
        {
1347
          perror("accept");
1348
          close(server_fd);
1349
          server_fd = 0;
1350
          serverPort = 0;
1351
          serverIP = 0;
1352
        }
1353
      return;
1354
    }
1355
 
1356
  if(gdb_fd)
1357
    {
1358
      close(fd);
1359
      return;
1360
    }
1361
 
1362
  if(fcntl(fd,F_GETFL,&flags) < 0)
1363
    {
1364
      sprintf(sTemp,"Unable to get flags for gdb socket %d",fd);
1365
      perror(sTemp);
1366
      close(fd);
1367
      return;
1368
    }
1369
 
1370
  if(fcntl(fd,F_SETFL, flags | O_NONBLOCK) < 0)
1371
    {
1372
      sprintf(sTemp,"Unable to set flags for gdb socket %d to value 0x%08x",
1373
        fd,flags | O_NONBLOCK);
1374
      perror(sTemp);
1375
      close(fd);
1376
      return;
1377
    }
1378
 
1379
  if(setsockopt(fd,tcp_level,TCP_NODELAY,&on_off,sizeof(int)) < 0)
1380
    {
1381
      sprintf(sTemp,"Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",fd);
1382
      perror(sTemp);
1383
      close(fd);
1384
      return;
1385
    }
1386
 
1387
  gdb_fd = fd;
1388
}
1389
 
1390
void GDBRequest()
1391
{
1392
  JTAGProxyWriteMessage msg_write;
1393
  JTAGProxyReadMessage msg_read;
1394
  JTAGProxyChainMessage msg_chain;
1395
  JTAGProxyWriteResponse resp_write;
1396
  JTAGProxyReadResponse resp_read;
1397
  JTAGProxyChainResponse resp_chain;
1398
  JTAGProxyBlockWriteMessage *msg_bwrite;
1399
  JTAGProxyBlockReadMessage msg_bread;
1400
  JTAGProxyBlockWriteResponse resp_bwrite;
1401
  JTAGProxyBlockReadResponse *resp_bread;
1402
  char *buf;
1403
  unsigned long long data;
1404
  uint32_t command,length;
1405
  int len,i;
1406
  err = 0;
1407
 
1408
  /* First, we must read the incomming command */
1409
  if(gdb_read(&command,sizeof(uint32_t)) < 0)
1410
    {
1411
      if(gdb_fd)
1412
        {
1413
          perror("gdb socket - 1");
1414
          close(gdb_fd);
1415
          gdb_fd = 0;
1416
        }
1417
      return;
1418
    }
1419
  if(gdb_read(&length,sizeof(uint32_t)) < 0)
1420
    {
1421
      if(gdb_fd)
1422
        {
1423
          perror("gdb socket - 2");
1424
          close(gdb_fd);
1425
          gdb_fd = 0;
1426
        }
1427
      return;
1428
    }
1429
  length = ntohl(length);
1430
 
1431
  /* Now, verify the protocol and implement the command */
1432
  switch(ntohl(command))
1433
    {
1434
    case JTAG_COMMAND_WRITE:
1435
      if(length != sizeof(msg_write) - 8)
1436
        {
1437
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1438
          return;
1439
        }
1440
      buf = (char*)&msg_write;
1441
      if(gdb_read(&buf[8],length) < 0)
1442
        {
1443
          if(gdb_fd)
1444
            {
1445
              perror("gdb socket - 3");
1446
              close(gdb_fd);
1447
              gdb_fd = 0;
1448
            }
1449
          return;
1450
        }
1451
      msg_write.address = ntohl(msg_write.address);
1452
      msg_write.data_H = ntohl(msg_write.data_H);
1453
      msg_write.data_L = ntohl(msg_write.data_L);
1454
      jtag_write_reg(msg_write.address,msg_write.data_L);
1455
      resp_write.status = htonl(err);
1456
      if(gdb_write(&resp_write,sizeof(resp_write)) < 0)
1457
        {
1458
          if(gdb_fd)
1459
            {
1460
              perror("gdb socket - 4");
1461
              close(gdb_fd);
1462
              gdb_fd = 0;
1463
            }
1464
          return;
1465
        }
1466
      break;
1467
    case JTAG_COMMAND_READ:
1468
      if(length != sizeof(msg_read) - 8)
1469
        {
1470
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1471
          return;
1472
        }
1473
      buf = (char*)&msg_read;
1474
      if(gdb_read(&buf[8],length) < 0)
1475
        {
1476
          if(gdb_fd)
1477
            {
1478
              perror("gdb socket - 5");
1479
              close(gdb_fd);
1480
              gdb_fd = 0;
1481
            }
1482
          return;
1483
        }
1484
      msg_read.address = ntohl(msg_read.address);
1485
      jtag_read_reg(msg_read.address); /* Data not ready at this time, repeat. */
1486
      resp_read.data_L = jtag_read_reg(msg_read.address);
1487
      resp_read.status = htonl(err);
1488
      resp_read.data_H = 0;
1489
      resp_read.data_L = htonl(resp_read.data_L);
1490
      if(gdb_write(&resp_read,sizeof(resp_read)) < 0)
1491
        {
1492
          if(gdb_fd)
1493
            {
1494
              perror("gdb socket - 6");
1495
              close(gdb_fd);
1496
              gdb_fd = 0;
1497
            }
1498
          return;
1499
        }
1500
      break;
1501
    case JTAG_COMMAND_BLOCK_WRITE:
1502
      if(length < sizeof(JTAGProxyBlockWriteMessage)-8)
1503
        {
1504
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1505
          return;
1506
        }
1507
      if(!(buf = (char*)malloc(8+length)))
1508
        {
1509
          ProtocolClean(length,JTAG_PROXY_OUT_OF_MEMORY);
1510
          return;
1511
        }
1512
      msg_bwrite = (JTAGProxyBlockWriteMessage*)buf;
1513
      if(gdb_read(&buf[8],length) < 0)
1514
        {
1515
          if(gdb_fd)
1516
            {
1517
              perror("gdb socket - 5");
1518
              close(gdb_fd);
1519
              gdb_fd = 0;
1520
            }
1521
          free(buf);
1522
          return;
1523
        }
1524
      msg_bwrite->address = ntohl(msg_bwrite->address);
1525
      msg_bwrite->nRegisters = ntohl(msg_bwrite->nRegisters);
1526
      for(i=0;i<msg_bwrite->nRegisters;i++)
1527
        {
1528
          msg_bwrite->data[i] = ntohl(msg_bwrite->data[i]);
1529
          jtag_write_reg(msg_bwrite->address + i * 4,msg_bwrite->data[i]);
1530
        }
1531
      resp_bwrite.status = htonl(err);
1532
      free(buf);
1533
      buf = msg_bwrite = NULL;
1534
      if(gdb_write(&resp_bwrite,sizeof(resp_bwrite)) < 0)
1535
        {
1536
          if(gdb_fd)
1537
            {
1538
              perror("gdb socket - 4");
1539
              close(gdb_fd);
1540
              gdb_fd = 0;
1541
            }
1542
          return;
1543
        }
1544
      break;
1545
    case JTAG_COMMAND_BLOCK_READ:
1546
      if(length != sizeof(msg_bread) - 8)
1547
        {
1548
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1549
          return;
1550
        }
1551
      buf = (char*)&msg_bread;
1552
      if(gdb_read(&buf[8],length) < 0)
1553
        {
1554
          if(gdb_fd)
1555
            {
1556
              perror("gdb socket - 5");
1557
              close(gdb_fd);
1558
              gdb_fd = 0;
1559
            }
1560
          return;
1561
        }
1562
      msg_bread.address = ntohl(msg_bread.address);
1563
      msg_bread.nRegisters = ntohl(msg_bread.nRegisters);
1564
      len = sizeof(JTAGProxyBlockReadResponse) + 4*(msg_bread.nRegisters-1);
1565
      if(!(buf = (char*)malloc(len)))
1566
        {
1567
          ProtocolClean(0,JTAG_PROXY_OUT_OF_MEMORY);
1568
          return;
1569
        }
1570
      resp_bread = (JTAGProxyBlockReadResponse*)buf;
1571
      jtag_read_reg(msg_bread.address); /* Prepare for reading. */
1572
      for(i=0;i<msg_bread.nRegisters;i++)
1573
        {
1574
          /* Read previous, address next one. */
1575
          resp_bread->data[i] = jtag_read_reg(msg_bread.address + (i + 1) * 4);
1576
          resp_bread->data[i] = htonl(resp_bread->data[i]);
1577
        }
1578
      resp_bread->status = htonl(err);
1579
      resp_bread->nRegisters = htonl(msg_bread.nRegisters);
1580
      if(gdb_write(resp_bread,len) < 0)
1581
        {
1582
          if(gdb_fd)
1583
            {
1584
              perror("gdb socket - 6");
1585
              close(gdb_fd);
1586
              gdb_fd = 0;
1587
            }
1588
          free(buf);
1589
          return;
1590
        }
1591
      free(buf);
1592
      buf = resp_bread = NULL;
1593
      break;
1594
    case JTAG_COMMAND_CHAIN:
1595
      if(length != sizeof(msg_chain) - 8)
1596
        {
1597
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1598
          return;
1599
        }
1600
      buf = (char*)&msg_chain;
1601
      if(gdb_read(&buf[8],sizeof(msg_chain)-8) < 0)
1602
        {
1603
          if(gdb_fd)
1604
            {
1605
              perror("gdb socket - 7");
1606
              close(gdb_fd);
1607
              gdb_fd = 0;
1608
            }
1609
          return;
1610
        }
1611
      msg_chain.chain = htonl(msg_chain.chain);
1612
      jtag_set_chain(msg_chain.chain);
1613
      resp_chain.status = htonl(err);
1614
      if(gdb_write(&resp_chain,sizeof(resp_chain)) < 0)
1615
        {
1616
          if(gdb_fd)
1617
            {
1618
              perror("gdb socket - 8");
1619
              close(gdb_fd);
1620
              gdb_fd = 0;
1621
            }
1622
          return;
1623
        }
1624
      break;
1625
    default:
1626
      perror("Unknown JTAG command.");
1627
      ProtocolClean(length,JTAG_PROXY_COMMAND_NOT_IMPLEMENTED);
1628
      break;
1629
    }
1630
}
1631
 
1632
void ProtocolClean(int length,int32_t err)
1633
{
1634
  char buf[4096];
1635
 
1636
  err = htonl(err);
1637
  if((gdb_read(buf,length) < 0) ||
1638
      (gdb_write(&err,sizeof(err)) < 0) && gdb_fd)
1639
    {
1640
      perror("gdb socket - 9");
1641
      close(gdb_fd);
1642
      gdb_fd = 0;
1643
    }
1644
}
1645
 
1646
static int gdb_write(void* buf,int len)
1647
{
1648
  int n;
1649
  char* w_buf = (char*)buf;
1650
  struct pollfd block;
1651
 
1652
  while(len)
1653
    {
1654
      if((n = write(gdb_fd,w_buf,len)) < 0)
1655
        {
1656
          switch(errno)
1657
            {
1658
            case EWOULDBLOCK: /* or EAGAIN */
1659
              /* We've been called on a descriptor marked
1660
           for nonblocking I/O. We better simulate
1661
           blocking behavior. */
1662
              block.fd = gdb_fd;
1663
              block.events = POLLOUT;
1664
              block.revents = 0;
1665
              poll(&block,1,-1);
1666
              continue;
1667
            case EINTR:
1668
              continue;
1669
            case EPIPE:
1670
              close(gdb_fd);
1671
              gdb_fd = 0;
1672
              return -1;
1673
            default:
1674
              return -1;
1675
            }
1676
        }
1677
      else
1678
        {
1679
          len -= n;
1680
          w_buf += n;
1681
        }
1682
    }
1683
  return 0;
1684
}
1685
 
1686
static int gdb_read(void* buf,int len)
1687
{
1688
  int n;
1689
  char* r_buf = (char*)buf;
1690
  struct pollfd block;
1691
 
1692
  while(len)
1693
    {
1694
      if((n = read(gdb_fd,r_buf,len)) < 0)
1695
        {
1696
          switch(errno)
1697
            {
1698
            case EWOULDBLOCK: /* or EAGAIN */
1699
              /* We've been called on a descriptor marked
1700
           for nonblocking I/O. We better simulate
1701
           blocking behavior. */
1702
              block.fd = gdb_fd;
1703
              block.events = POLLIN;
1704
              block.revents = 0;
1705
              poll(&block,1,-1);
1706
              continue;
1707
            case EINTR:
1708
              continue;
1709
            default:
1710
              return -1;
1711
            }
1712
        }
1713
      else if(n == 0)
1714
        {
1715
          close(gdb_fd);
1716
          gdb_fd = 0;
1717
          return -1;
1718
        }
1719
      else
1720
        {
1721
          len -= n;
1722
          r_buf += n;
1723
        }
1724
    }
1725
  return 0;
1726
}

powered by: WebSVN 2.1.0

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