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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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