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

Subversion Repositories openrisc_me

[/] [openrisc/] [tags/] [gdb/] [gdb-6.8/] [gdb-6.8.openrisc-2.1/] [gdb/] [or1k-jtag.c] - Blame information for rev 33

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
/* Remote debugging interface for JTAG debugging protocol.
2
   JTAG connects to or1k target ops. See or1k-tdep.c
3
 
4
   Copyright 1993-1995, 2000 Free Software Foundation, Inc.
5
   Copyright 2008 Embecosm Limited
6
 
7
   Contributed by Cygnus Support.  Written by Marko Mlinar
8
   <markom@opencores.org>
9
   Areas noted by (CZ) were modified by Chris Ziomkowski <chris@asics.ws>
10
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
11
 
12
   This file is part of GDB.
13
 
14
   This program is free software; you can redistribute it and/or modify it
15
   under the terms of the GNU General Public License as published by the Free
16
   Software Foundation; either version 3 of the License, or (at your option)
17
   any later version.
18
 
19
   This program is distributed in the hope that it will be useful, but WITHOUT
20
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
22
   more details.
23
 
24
   You should have received a copy of the GNU General Public License along
25
   with this program.  If not, see <http://www.gnu.org/licenses/>.
26
*/
27
 
28
/*---------------------------------------------------------------------------*/
29
/*!Updated for GDB 6.8 by Jeremy Bennett. All code converted to ANSI C style
30
   and in general to GDB format. All global OpenRISC specific functions and
31
   variables should now have a prefix of or1k_ or OR1K_.
32
 
33
   The coding for the local JTAG connections assumes a Xilinx JP1 port
34
   connecting via the local parallel port. Constants and routines processing
35
   these have the prefix jp1_ or JP1_. Remote connections use the OpenRISC
36
   remote JTAG protocol.
37
 
38
   The interface is layered
39
 
40
   The highest level is the public functions, which operate in terms of
41
   entities that are visible in GDB: open & close the connection, read and
42
   write SPRs, read and write memory, stall and unstall the
43
   processor. These functions always succeed Function prefixes: or1k_jtag_
44
 
45
   The next level is the abstraction provided by the OR1K Remote JTAG
46
   protocol: read/write a JTAG register, read/write a block of JTAG registers
47
   and select a scan chain. These functions may encounter errors and will deal
48
   with them, but otherwise return no error result. Static function prefixes:
49
   or1k_jtag_
50
 
51
   The next level is the two sets corresponding to the remote JTAG protocol,
52
   one for use with a locally connected (JP1) JTAG and one for a remote
53
   connection. These functions deal with errors and return an error code to
54
   indicate an error has occurred. Static function prefixes: jp1_ and jtr_
55
   respectively.
56
 
57
   The final level comes in separate flavours for local use (low level
58
   routines to drive JTAG) and remote use (to build and send/receive
59
   packets). These functions deal with errors and return an error code to
60
   indicate an error has occurred. Static function prefixes: jp1_ll_ and
61
   jtr_ll_ respectively.
62
 
63
   Errors are either dealt with silently or (if fatal) via the GDB error()
64
   function.
65
 
66
   @note Few people use the JP1 direct connection, and there is no
67
         confidence that this code works at all!
68
 
69
         Commenting compatible with Doxygen added throughout. */
70
/*---------------------------------------------------------------------------*/
71
 
72
 
73
#include "defs.h"
74
#include "inferior.h"
75
#include "bfd.h"
76
#include "symfile.h"
77
#include "gdb_wait.h"
78
#include "gdbcmd.h"
79
#include "gdbcore.h"
80
#include "serial.h"
81
#include "target.h"
82
#include "gdb_string.h"
83
#include "or1k-tdep.h"
84
#include "or1k-jtag.h"
85
 
86
/* Added by CZ 24/05/01 */
87
#include <sys/poll.h>
88
#include <sys/socket.h>
89
#include <netdb.h>
90
#include <netinet/in.h>
91
#include <netinet/tcp.h>
92
#include <sys/select.h>
93
#include <sys/time.h>
94
#include <unistd.h>
95
#include <signal.h>
96
#include <sys/types.h>
97
#include <sys/stat.h>
98
#include <sys/ioctl.h>
99
#include <fcntl.h>
100
 
101
#include <inttypes.h>
102
 
103
/* Wait times (us) */
104
#define OR1K_JTAG_FAST_WAIT     1       /*!< Wait us when single stepping */
105
#define OR1K_JTAG_SLOW_WAIT  1000       /*!< Wait us when not single stepping */
106
 
107
/* Bit numbers in packets for the JP1 JTAG pins */
108
#define JP1_TCK   0x01          /*!< JTAG TCK pin */
109
#define JP1_TRST  0x02          /*!< JTAG TRST pin */
110
#define JP1_TMS   0x04          /*!< JTAG TMS pin */
111
#define JP1_TDI   0x08          /*!< JTAG TDI pin */
112
#define JP1_TDO   0x80          /*!< JTAG TDO pin */
113
 
114
#define JP1_WAIT()
115
#define JP1_RETRY_WAIT()  (usleep (100))
116
 
117
#define JP1_NUM_RETRIES   16
118
 
119
/*! Selects crc trailer size in bits. Currently supported: 8 */
120
#define JP1_CRC_SIZE       8
121
 
122
static int  jp1_crc_r;                  /*!< CRC of current read data */
123
static int  jp1_crc_w = 0;               /*!< CRC of current written data */
124
 
125
/*! Designates whether we are in SELECT_DR state */
126
static int or1k_select_dr = 0;
127
 
128
/*! Scan chain info. */
129
static int  jp1_chain_addr_size[] = { 0,  32, 0,   0,  5 };
130
static int  jp1_chain_data_size[] = { 0,  32, 0,  32, 32 };
131
static int  jp1_chain_is_valid[]  = { 0,   1, 0,   1,  1 };
132
static int  jp1_chain_has_crc[]   = { 0,   1, 0,   1,  1 };
133
static int  jp1_chain_has_rw[]    = { 0,   1, 0,   0,  1 };
134
 
135
/*! Currently selected scan chain - just to prevent unnecessary transfers. */
136
static int  or1k_jtag_current_chain;
137
 
138
/*! Information about the JTAG connection, if any */
139
struct {
140
  union {
141
    int lp;             /* Printer compatible device we have open.  */
142
    int fd;             /* Socket for remote or1k jtag interface */
143
  } device;
144
  enum {
145
    OR1K_JTAG_NOT_CONNECTED,
146
    OR1K_JTAG_LOCAL,
147
    OR1K_JTAG_REMOTE
148
  } location;
149
} or1k_jtag_connection;
150
 
151
 
152
/*! Global variable identifying the debug interface version. Assume
153
    consistency with GDB 5.3, unless otherwise corrected. */
154
enum or1k_dbg_if_version_enum  or1k_dbg_if_version = OR1K_DBG_IF_ORPSOC;
155
 
156
/* Forward declarations of the public interface functions */
157
 
158
void      or1k_jtag_init (char *args);
159
void      or1k_jtag_close ();
160
ULONGEST  or1k_jtag_read_spr (unsigned int  sprnum);
161
void      or1k_jtag_write_spr (unsigned int  sprnum,
162
                               ULONGEST      data);
163
int       or1k_jtag_read_mem (CORE_ADDR  addr,
164
                              gdb_byte  *bdata,
165
                              int        len);
166
int       or1k_jtag_write_mem (CORE_ADDR       addr,
167
                               const gdb_byte *bdata,
168
                               int             len);
169
void      or1k_jtag_stall ();
170
void      or1k_jtag_unstall ();
171
void      or1k_jtag_wait (int  fast);
172
 
173
/* Forward declarations for the low level JP1 routines. */
174
 
175
static int            jp1_ll_crc_calc (int  crc,
176
                                       int  input_bit);
177
static void           jp1_ll_reset_jp1 ();
178
static unsigned char  jp1_ll_read_jp1 ();
179
static void           jp1_ll_write_jp1 (unsigned char  tms,
180
                                        unsigned char  tdi);
181
static ULONGEST       jp1_ll_read_stream (ULONGEST  stream,
182
                                          int       len,
183
                                          int       set_last_bit);
184
static void           jp1_ll_write_stream (ULONGEST  stream,
185
                                           int       len,
186
                                           int       set_last_bit);
187
static void           jp1_ll_prepare_control ();
188
static void           jp1_ll_prepare_data ();
189
 
190
/* Forward declarations of low level support functions for remote OR1K remote
191
   JTAG server. */
192
 
193
static int                    jtr_ll_connect (char *hostname,
194
                                              char *port_or_service);
195
static void                   jtr_ll_close ();
196
static enum or1k_jtag_errors  jtr_ll_check (enum or1k_jtag_errors  result);
197
static enum or1k_jtag_errors  jtr_ll_read (int   fd,
198
                                           void *buf,
199
                                           int   len);
200
static enum or1k_jtag_errors  jtr_ll_write (int   fd,
201
                                            void *buf,
202
                                            int   len);
203
static enum or1k_jtag_errors  jtr_ll_response (int   fd,
204
                                               void *resp_buf,
205
                                               int   len);
206
 
207
/* Forward declarations for OR1K JTAG protocol functions for use with a local
208
   JP1 connection. */
209
 
210
static enum or1k_jtag_errors  jp1_read_jtag_reg (unsigned int  regnum,
211
                                                 ULONGEST     *data);
212
static enum or1k_jtag_errors  jp1_write_jtag_reg (unsigned int  regnum,
213
                                                  ULONGEST      data);
214
static enum or1k_jtag_errors  jp1_read_jtag_block (unsigned int  regnum,
215
                                                   uint32_t     *bdata,
216
                                                   int           count);
217
static enum or1k_jtag_errors  jp1_write_jtag_block (unsigned int    regnum,
218
                                                    const uint32_t *bdata,
219
                                                    int             count);
220
static enum or1k_jtag_errors  jp1_select_chain (int  chain);
221
 
222
/* Forward declarations for OR1K JTAG protocol functions for use with a remote
223
   JTAG server. */
224
 
225
static enum or1k_jtag_errors  jtr_read_jtag_reg (unsigned int  regnum,
226
                                                 ULONGEST     *data);
227
static enum or1k_jtag_errors  jtr_write_jtag_reg (unsigned long int  regnum,
228
                                                  ULONGEST           data);
229
static enum or1k_jtag_errors  jtr_read_jtag_block (CORE_ADDR  regnum,
230
                                                   uint32_t  *bdata,
231
                                                   int        count);
232
static enum or1k_jtag_errors  jtr_write_jtag_block (CORE_ADDR     regnum,
233
                                                    const uint32_t *bdata,
234
                                                    int             count);
235
static enum or1k_jtag_errors  jtr_select_chain (int  chain);
236
 
237
/* Forward declarations for OR1K JTAG protocol functions for use with either a
238
   local connection or remote server */
239
 
240
static void        or1k_jtag_read_jtag_reg (unsigned int  regnum,
241
                                            ULONGEST     *data);
242
static void        or1k_jtag_write_jtag_reg (unsigned int  regnum,
243
                                             ULONGEST      data);
244
static void        or1k_jtag_read_jtag_block (unsigned int  regnum,
245
                                              uint32_t     *bdata,
246
                                              int           count);
247
static void        or1k_jtag_write_jtag_block (unsigned int    regnum,
248
                                               const uint32_t *bdata,
249
                                               int             count);
250
static void        or1k_jtag_select_chain (int  chain);
251
static void        or1k_jtag_reset ();
252
static const char *or1k_jtag_err_name (enum or1k_jtag_errors  e);
253
 
254
 
255
 
256
/* These are the low level OpenRISC 1000 JTAG Protocol functions for use with
257
   a local JTAG connection. All these functions are static and have the prefix
258
   jp1_ll_ */
259
 
260
/*----------------------------------------------------------------------------*/
261
/*!Generates new crc, sending in new bit input_bit
262
 
263
   @note  Only actually calculates anything if the CRC size is 8.
264
 
265
   @param[in] crc        Current CRC
266
   @param[in] input_bit  New bit to incorporate
267
 
268
   @return  New CRC */
269
/*---------------------------------------------------------------------------*/
270
 
271
static int
272
jp1_ll_crc_calc (int  crc,
273
                 int  input_bit)
274
{
275
  int c;
276
  int new_crc;
277
  int d;
278
 
279
#if (JP1_CRC_SIZE == 8)
280
  d = input_bit&1;
281
  c = crc;
282
 
283
  /* Move queue left.  */
284
  new_crc = crc << 1;
285
 
286
  /* Mask upper five bits.  */
287
  new_crc &= 0xF8;
288
 
289
  /* Set lower three bits */
290
  new_crc |= (d ^ ((c >> 7) & 1));
291
  new_crc |= (d ^ ((c >> 0) & 1) ^ ((c >> 7) & 1)) << 1;
292
  new_crc |= (d ^ ((c >> 1) & 1) ^ ((c >> 7) & 1)) << 2;
293
 
294
  return new_crc;
295
#else
296
  return 0;
297
#endif
298
}
299
 
300
 
301
/*----------------------------------------------------------------------------*/
302
/*!Resets the local JTAG via JP1
303
 
304
   Only works if this is a local connection
305
 
306
   Writes:
307
   @verbatim
308
     TCK=0 TRST=0 TMS=0 TDI=0
309
     TCK=0 TRST=1 TMS=0 TDI=0
310
   @endverbatim
311
 
312
   JTAG reset is active low */
313
/*---------------------------------------------------------------------------*/
314
 
315
static void
316
jp1_ll_reset_jp1()
317
{
318
  unsigned char  data;
319
  int            lp = or1k_jtag_connection.device.lp;         /* CZ */
320
 
321
  if (OR1K_JTAG_LOCAL != or1k_jtag_connection.location)
322
    {
323
      error ("jp1_ll_reset_jp1 called without a local connection!");
324
    }
325
 
326
  data = 0;
327
  write (lp, &data, sizeof (data));
328
  JP1_WAIT ();
329
 
330
  data = JP1_TRST;
331
  write (lp, &data, sizeof (data) );
332
  JP1_WAIT ();
333
 
334
}       /* jp1_ll_reset_jp1() */
335
 
336
 
337
/*----------------------------------------------------------------------------*/
338
/*!Gets TD0 from JP1
339
 
340
   This is done using ioctl().
341
 
342
   Rewritten by Jeremy Bennett to work whatever the TMS and TDI bit positions
343
   in JP1.
344
 
345
   @todo I really do not believe this can work. ioctl() expects a FD
346
         (presumably or1k_jtag_connection.device.lp). A read() call would be the
347
         more usual way to actually obtain data, given the data is set using
348
         a write() call.
349
 
350
   Writes:
351
   @verbatim
352
     TCK=0 TRST=1 TMS=bit0 TDI=bit1
353
     TCK=1 TRST=1 TMS=bit0 TDI=bit1
354
   @endverbatim
355
 
356
   @return  Bit 0 is the value of TD0. */
357
/*---------------------------------------------------------------------------*/
358
 
359
static unsigned char
360
jp1_ll_read_jp1()
361
{
362
  int data;
363
 
364
  if (OR1K_JTAG_LOCAL != or1k_jtag_connection.location)
365
    {   /* CZ */
366
      error ("jp1_ll_read_jp1 called without a local connection!");
367
    }
368
 
369
  ioctl (data, 0x60b, &data);           /* Really!!!!! */
370
  data = ((data & JP1_TDO) != 0);
371
 
372
  /* Update the CRC for read */
373
  jp1_crc_r = jp1_ll_crc_calc (jp1_crc_r, data);
374
  return data;
375
 
376
}       /* jp1_ll_read_jp1() */
377
 
378
 
379
/*----------------------------------------------------------------------------*/
380
/*!Clocks a bit into the local JTAG via JP1
381
 
382
   Takes TMS and TDI values from the bottom two bits of the argument
383
 
384
   Rewritten by Jeremy Bennett to work whatever the TMS and TDI bit positions
385
   in JP1.
386
 
387
   Writes:
388
   @verbatim
389
     TCK=0 TRST=1 TMS=tms TDI=tdi
390
     TCK=1 TRST=1 TMS=tms TDI=tdi
391
   @endverbatim
392
 
393
   @param[in] tms  The JTAG TMS pin to be clocked in
394
   @param[in] tdi  The JTAG TDI pin to be clocked in */
395
/*---------------------------------------------------------------------------*/
396
 
397
static void
398
jp1_ll_write_jp1 (unsigned char  tms,
399
                  unsigned char  tdi)
400
{
401
  unsigned char  data;
402
  int            lp = or1k_jtag_connection.device.lp;    /* CZ */
403
 
404
  if (OR1K_JTAG_LOCAL != or1k_jtag_connection.location)
405
    {
406
      error ("jp1_ll_write_jp1 called without a local connection!");
407
    }
408
 
409
  data = (tms ? JP1_TDI : 0) |
410
         (tdi ? JP1_TMS : 0) |
411
          JP1_TRST;
412
  write (lp, &data, sizeof (data) );
413
  JP1_WAIT ();
414
 
415
  /* rise clock */
416
  data |= JP1_TCK;
417
  write (lp, &data, sizeof (data) );
418
  JP1_WAIT ();
419
 
420
  /* Update the CRC for this TDI bit */
421
  jp1_crc_w = jp1_ll_crc_calc (jp1_crc_w, tdi ? 1 : 0);
422
 
423
}       /* jp1_write_jtag() */
424
 
425
 
426
/*----------------------------------------------------------------------------*/
427
/*!Read a stream of bits from TDO whilst also writing TDI and optionally TMS
428
 
429
   Reads a bitstream of 1 or more bits from TDO, MS bit first whilst writing
430
   a bitstream of the same length LS bit first. Optionally (if
431
   set_last_bit is 1) writes the MS bit with TMS also set.
432
 
433
   @todo  The expression precedence in the original setting of TMS bit seemed
434
          in error (due to a misunderstanding of the relative precedence of +
435
          and <<. The changed expression should be correct, but needs testing.
436
 
437
   @note If len is > sizeof (ULONGEST), then then only 0's are written for
438
         all except the MS sizeof (ULONGEST)  bits of stream.
439
 
440
   @param[in] stream        string of bits to write to TDI
441
   @param[in] len           number of bits to write
442
   @param[in] set_last_bit  if true also set TMS bit with last TDI bit.
443
 
444
   @return  Bitstream read MS bit first from TDO. */
445
/*---------------------------------------------------------------------------*/
446
 
447
static ULONGEST
448
jp1_ll_read_stream (ULONGEST  stream,
449
                    int       len,
450
                    int       set_last_bit)
451
{
452
  int i;
453
  ULONGEST data = 0;             /* The resulting stream */
454
 
455
  /* Do nothing unless len is positive */
456
  if (len <= 0)
457
    {
458
      return  (ULONGEST)0;
459
    }
460
 
461
  /* Read all but last bit MS bit first while writing stream LS bit first */
462
  for (i = 0; i < len-1; i++)
463
    {
464
      jp1_ll_write_jp1 (0, stream & 1);
465
      stream >>= 1;
466
      data   <<= 1;
467
      data    |= jp1_ll_read_jp1 ();
468
    }
469
 
470
  /* Last bit optionally sets TMS as well */
471
  jp1_ll_write_jp1 (set_last_bit, stream & 1);
472
  data <<= 1;
473
  data  |= jp1_ll_read_jp1 ();
474
 
475
  return data;
476
 
477
}       /* jp1_ll_read_stream() */
478
 
479
 
480
/*----------------------------------------------------------------------------*/
481
/*!Write a stream of bits to TDI and optionally TMS
482
 
483
   Writes bitstream of 1 or more bits to TDI, MS bit first. Optionally (if
484
   set_last_bit is 1) writes the last bit with TMS also set.
485
 
486
   @note If len is > sizeof (ULONGEST), then then only 0's are written for
487
         all the bits > sizeof (ULONGEST)
488
 
489
   @param[in] stream        string of bits to write to TDI
490
   @param[in] len           number of bits to write
491
   @param[in] set_last_bit  if true also set TMS bit with last TDI bit. */
492
/*---------------------------------------------------------------------------*/
493
 
494
static void
495
jp1_ll_write_stream (ULONGEST  stream,
496
                     int       len,
497
                     int       set_last_bit)
498
{
499
  int i;
500
 
501
  /* Do nothing if len is not positive */
502
  if (len <= 0)
503
    {
504
      return;
505
    }
506
 
507
  /* All but last bit to TDI only */
508
  for (i = len - 1; i > 0; i--)
509
    {
510
      jp1_ll_write_jp1 (0, (stream >> i) & 1);
511
    }
512
 
513
  /* Last bit optionally sets TMS as well */
514
  jp1_ll_write_jp1 (set_last_bit, stream & 1);
515
 
516
}       /* jp1_ll_write_stream() */
517
 
518
 
519
/*----------------------------------------------------------------------------*/
520
/*!Force JTAG SELECT_IR state
521
 
522
   Should be called before every control write.
523
 
524
   If we are not in SELECT_DR, we must be in RUN TEST/IDLE, so step the state
525
   to SELECT_DR. One more step takes us to SELECT_IR. Set the or1k_select_dr
526
   flag to false. */
527
/*---------------------------------------------------------------------------*/
528
 
529
static void
530
jp1_ll_prepare_control()
531
{
532
  if (!or1k_select_dr)
533
    {
534
      jp1_ll_write_jp1 (1, 0);   /* SELECT_DR SCAN */
535
    }
536
 
537
  jp1_ll_write_jp1 (1, 0);       /* SELECT_IR SCAN */
538
  or1k_select_dr = 0;
539
 
540
}       /* jp1_ll_prepare_control() */
541
 
542
 
543
/*----------------------------------------------------------------------------*/
544
/*!Force JTAG SELECT_DR state
545
 
546
   If we are not in SELECT_DR, we must be in RUN TEST/IDLE, so step the state
547
   to SELECT_DR. */
548
/*---------------------------------------------------------------------------*/
549
 
550
static void
551
jp1_ll_prepare_data()
552
{
553
  if (!or1k_select_dr)
554
    {
555
      jp1_ll_write_jp1 (0, 1);   /* SELECT_DR SCAN */
556
    }
557
 
558
  or1k_select_dr = 1;
559
 
560
}       /* jp1_ll_prepare_data() */
561
 
562
 
563
 
564
/* These are the low level OpenRISC 1000 JTAG Protocol functions for use with
565
   a remote JTAG server. All these functions are static and have the prefix
566
   jtr_ll_ */
567
 
568
/*----------------------------------------------------------------------------*/
569
/*!Connect to a remote OpenRISC 1000 JTAG server.
570
 
571
   Added by CZ 24/05/01
572
 
573
   Makes the connection as a socket and returns the file descriptor for the
574
   socket.
575
 
576
   @param[in]  hostname         The host to connect to
577
   @param[in]  port_or_service  Port or service to connect to
578
 
579
   @return  A file descriptor for the new socket or 0 on error. */
580
/*---------------------------------------------------------------------------*/
581
 
582
static int
583
jtr_ll_connect (char *hostname,
584
                char *port_or_service)
585
{
586
  struct hostent     *host;
587
  struct sockaddr_in  sin;
588
  struct servent     *service;
589
  struct protoent    *protocol;
590
 
591
  int       sock;
592
  long int  flags;
593
 
594
  char *endptr;
595
 
596
  const char *proto_name = "tcp";
597
 
598
  int   port   = 0;
599
  int   on_off = 0; /* Turn off Nagel's algorithm on the socket */
600
 
601
  protocol = getprotobyname (proto_name);
602
  if (NULL == protocol)
603
    {
604
      error ("jtr_ll_connect: Protocol \"%s\" not available.\n",
605
             proto_name);
606
      return  0;
607
    }
608
 
609
  /* Convert port_or_service to an integer only if it is a well formatted
610
     decimal number (i.e. the end pointer is the null char at the end of the
611
     string.  Otherwise, assume that it is a service name. */
612
 
613
  port = strtol (port_or_service, &endptr, 10);
614
  if ('\0' != endptr[0])
615
    {
616
      port = 0;
617
 
618
      /* Not a port, see if its a service */
619
      service  = getservbyname (port_or_service, protocol->p_name);
620
      if (NULL == service)
621
        {
622
          error ("jtr_ll_connect: Unknown service \"%s\".\n",
623
                 port_or_service);
624
          return 0;
625
        }
626
      else
627
        {
628
          port = ntohs (service->s_port);
629
        }
630
    }
631
 
632
  /* Try to find the host */
633
  host = gethostbyname (hostname);
634
  if (NULL == host)
635
    {
636
      error ("jtr_ll_connect: Unknown host \"%s\"\n", hostname);
637
      return 0;
638
    }
639
 
640
  /* Open a socket using IP4 stream (i.e. TCP/IP) connection and make it
641
     blocking. */
642
  sock = socket (PF_INET, SOCK_STREAM, 0);
643
  if (sock < 0)
644
    {
645
      error ("jtr_ll_connect: can't create socket, errno = %d (%s)\n",
646
             errno ,strerror (errno) );
647
      return 0;
648
    }
649
 
650
  flags = fcntl (sock, F_GETFL, 0);
651
  if (flags < 0)
652
    {
653
      error ("jtr_ll_connect: can't get flags, errno = %d (%s)\n",
654
             errno, strerror (errno) );
655
      close (sock);
656
      return 0;
657
    }
658
 
659
  flags &= ~O_NONBLOCK;
660
  if (fcntl (sock, F_SETFL, flags)  < 0)
661
    {
662
      error ("jtr_ll_connect: can't set flags %lx, errno = %d (%s)\n",
663
             flags, errno, strerror (errno) );
664
      close (sock);
665
      return 0;
666
    }
667
 
668
  /* Try to open a connection to the remote end */
669
  memset (&sin, 0, sizeof (sin) );
670
  sin.sin_family = host->h_addrtype;
671
  sin.sin_port   = htons (port);
672
  memcpy (&sin.sin_addr, host->h_addr_list[0], host->h_length);
673
 
674
  /* Old version of this allowed EINPROGRESS, but did nothing about following
675
     it up and in any case, that should not be possible with a blocking
676
     connection. */
677
  if (connect (sock, (struct sockaddr *)&sin, sizeof (sin) ) < 0)
678
    {
679
      error ("jtr_ll_connect: connect failed  errno = %d (%s)\n",
680
             errno, strerror (errno) );
681
      close (sock);
682
      return 0;
683
    }
684
 
685
  /* Turn the socket back to non-blocking */
686
  flags |= O_NONBLOCK;
687
  if (fcntl (sock, F_SETFL, flags)  < 0)
688
    {
689
      error ("jtr_ll_connect: can't set flags %lx, errno = %d (%s)\n",
690
             flags, errno, strerror (errno) );
691
      close (sock);
692
      return 0;
693
    }
694
 
695
  /* Turn off Nagle's algorithm, i.e. send data immediately, don't bottle it
696
     up into useful sized chunks. */
697
  if (setsockopt (sock, protocol->p_proto, TCP_NODELAY, &on_off,
698
                  sizeof(int)) < 0)
699
    {
700
      error ("jtr_ll_connect: "
701
             "can't disable Nagel's algorithm, errno %d (%s)\n", errno,
702
             strerror (errno) );
703
      close(sock);
704
      return 0;
705
    }
706
 
707
  return sock;
708
 
709
}       /* jtr_ll_connect() */
710
 
711
 
712
/*----------------------------------------------------------------------------*/
713
/*!Close down an OpenRISC 1000 JTAG remote connection
714
 
715
   Closes the connection without waiting for any queued messages on the
716
   socket.
717
 
718
   In this version the remote socket FD is always used and the result is
719
   discarded, since it is never used by the calling function. */
720
/*---------------------------------------------------------------------------*/
721
 
722
static void
723
jtr_ll_close()
724
{
725
  int            flags;
726
  struct linger  linger;
727
 
728
  /* First, make sure we're non blocking */
729
  flags = fcntl (or1k_jtag_connection.device.fd, F_GETFL, 0);
730
  if (flags < 0)
731
    {
732
      error ("jtr_ll_close: can't get flags errno %d (%s)\n",
733
             errno, strerror (errno) );
734
    }
735
 
736
  flags &= ~O_NONBLOCK;
737
  if (fcntl (or1k_jtag_connection.device.fd, F_SETFL, flags)  < 0)
738
    {
739
      error ("jtr_ll_close: can't set flags to 0x%x, errno %d (%s)\n",
740
             flags, errno, strerror (errno) );
741
    }
742
 
743
  /* Now, make sure we don't linger around */
744
  linger.l_onoff  = 0;
745
  linger.l_linger = 0;
746
 
747
  if (setsockopt (or1k_jtag_connection.device.fd, SOL_SOCKET, SO_LINGER,
748
                  &linger, sizeof (linger) ) < 0)
749
    {
750
      error ("jtr_ll_close: can't disable SO_LINGER, errno %d (%s)",
751
             errno, strerror (errno) );
752
    }
753
 
754
  (void)close (or1k_jtag_connection.device.fd);
755
 
756
  or1k_jtag_connection.device.fd = 0;
757
  or1k_jtag_connection.location  = OR1K_JTAG_NOT_CONNECTED;
758
 
759
}       /* jtr_ll_close() */
760
 
761
 
762
/*----------------------------------------------------------------------------*/
763
/*!Close down a remote connection if the server has terminated
764
 
765
   Utility function which closes the remote connection if the error result
766
   OR1K_JTAG_ERR_SERVER_TERMINATED has been received.
767
 
768
   @param[in]  result  Result code to check
769
 
770
   @return  The result passed as argument (for convenience) */
771
/*---------------------------------------------------------------------------*/
772
 
773
static enum or1k_jtag_errors
774
jtr_ll_check (enum or1k_jtag_errors  result)
775
{
776
  if (OR1K_JTAG_ERR_SERVER_TERMINATED == result)
777
    {
778
      jtr_ll_close ();
779
    }
780
 
781
  return result;
782
}       /* jtr_ll_check() */
783
 
784
 
785
/*----------------------------------------------------------------------------*/
786
/*!Read from a remote OpenRISC 1000 JTAG connection
787
 
788
   Added by CZ 24/05/01. Modified by Jeremy Bennett.
789
 
790
   The read should always be blocking, so if we get a result indicating a
791
   non-blocking FD, we simulate the blocking behavior using poll(). May take
792
   several reads to get the whole buffer in.
793
 
794
   @param[in]  fd   File descriptor for remote connection
795
   @param[out] buf  Buffer for data that is read
796
   @param[in]  len  Number of bytes to read
797
 
798
   @return  system error (positive) or OR1K JTAG error code */
799
/*---------------------------------------------------------------------------*/
800
 
801
static enum or1k_jtag_errors
802
jtr_ll_read (int   fd,
803
             void *buf,
804
             int   len)
805
{
806
  char* r_buf = (char*)buf;
807
 
808
  while (len > 0)
809
    {
810
      int  n = read (fd, r_buf, len);
811
 
812
      if (n < 0)
813
        {
814
          struct pollfd block;
815
 
816
          switch(errno)
817
            {
818
            case EWOULDBLOCK: /* or EAGAIN */
819
              /* We've been called on a descriptor marked for nonblocking
820
                 I/O. We'd better simulate blocking behavior. */
821
              block.fd = fd;
822
              block.events = POLLIN;
823
              block.revents = 0;
824
 
825
              poll(&block,1,-1);
826
              continue;
827
 
828
            case EINTR: continue;
829
            default:    return  errno;
830
            }
831
        }
832
      else if (0 == n)
833
        {
834
          /* Zero indicates EOF. */
835
          return OR1K_JTAG_ERR_SERVER_TERMINATED;
836
        }
837
      else
838
        {
839
          len   -= n;
840
          r_buf += n;
841
        }
842
    }
843
 
844
  return  OR1K_JTAG_ERR_NONE;
845
 
846
}       /* jtr_ll_read() */
847
 
848
 
849
/*----------------------------------------------------------------------------*/
850
/*!Write to a remote OpenRISC 1000 JTAG connection
851
 
852
   Added by CZ 24/05/01. Modified by Jeremy Bennett.
853
 
854
   The write should always be blocking, so if we get a result indicating a
855
   non-blocking FD, we simulate the blocking behavior using poll(). May take
856
   several writes to get the whole buffer out.
857
 
858
   @param[in] fd   File descriptor for remote connection
859
   @param[in] buf  Buffer of data to be written
860
   @param[in] len  Number of bytes to write
861
 
862
   @return  system error (positive) or OR1K JTAG error code */
863
/*---------------------------------------------------------------------------*/
864
 
865
static enum or1k_jtag_errors
866
jtr_ll_write (int   fd,
867
           void *buf,
868
           int   len)
869
{
870
  char *w_buf = (char*)buf;     /* Pointer to buffer left to write */
871
 
872
  while (len > 0)
873
    {
874
      int            n = write (fd, w_buf, len);
875
 
876
      if (n < 0)
877
        {
878
          struct pollfd  block;
879
 
880
          switch (errno)
881
            {
882
            case EWOULDBLOCK: /* or EAGAIN */
883
              /* We've been called on a descriptor marked for nonblocking
884
                 I/O. We'd better simulate blocking behavior. */
885
              block.fd = fd;
886
              block.events = POLLOUT;
887
              block.revents = 0;
888
 
889
              poll (&block, 1, -1);             /* Wait forever for output */
890
              continue;
891
 
892
            case EINTR: continue;
893
            case EPIPE: return  OR1K_JTAG_ERR_SERVER_TERMINATED;
894
            default:    return  errno;
895
            }
896
        }
897
      else
898
        {
899
          len   -= n;
900
          w_buf += n;
901
        }
902
    }
903
 
904
  return  OR1K_JTAG_ERR_NONE;
905
 
906
}       /* jtr_ll_write() */
907
 
908
 
909
/*----------------------------------------------------------------------------*/
910
/*!Fetch a remote OpenRISC 1000 JTAG response
911
 
912
   All OR1K reponse structs start with a uint32_t status field. If a response
913
   is an error response this is all that is set.
914
 
915
   If the response is not an error, then there may be further bytes to
916
   get. The total number to get is gives by len.
917
 
918
   All data in the response is in raw network format. A bug has been fixed
919
   which converted the status to host format in the buffer (that should be
920
   done by the caller).
921
 
922
   All errors are not logged using the GDB logging function, not just plain
923
   printf.
924
 
925
   @param[in]  fd   File descriptor for remote connection
926
   @param[out] resp_buf  Buffer for the response
927
   @param[in]  len       Size of the response buffer in bytes if this is a
928
                         successful response.
929
 
930
                         @return  system error (positive) or OR1K JTAG error code */
931
/*---------------------------------------------------------------------------*/
932
 
933
static enum or1k_jtag_errors
934
jtr_ll_response (int   fd,
935
              void *resp_buf,
936
              int   len)
937
{
938
  long int         status;
939
  enum or1k_jtag_errors  result;
940
 
941
  /* Get the status and blow out for any failure */
942
  result = jtr_ll_read (fd, resp_buf, sizeof (status) );
943
  if (OR1K_JTAG_ERR_NONE != result)
944
    {
945
      return  jtr_ll_check (result);
946
    }
947
 
948
  status = ((struct jtr_failure_response *)resp_buf)->status;
949
  status = ntohl (status);
950
 
951
  if (OR1K_JTAG_ERR_NONE != status)
952
    {
953
      return  jtr_ll_check (status);
954
    }
955
 
956
  /* Get the rest of a good result. Note this works even if there is no more
957
     to get (i.e. len=4) */
958
  result = jtr_ll_read (fd, &(((char *)resp_buf)[sizeof (status) ]),
959
                                 len - sizeof (status) );
960
  return  jtr_ll_check (result);
961
 
962
}       /* jtr_ll_response() */
963
 
964
 
965
 
966
/* These are the OpenRISC 1000 JTAG Protocol routines for use with a local JP1
967
   connection. They are built on tol of the remote protocol low level helper
968
   functions. All these functions are static and have the prefix jp1_ */
969
 
970
 
971
/*----------------------------------------------------------------------------*/
972
/*!Read a JTAG register from a local JTAG connection.
973
 
974
   What this does depends on which chain is selected. Drive the JTAG state
975
   machine to read a value from a register
976
 
977
   @param[in]  regnum  The JTAG register from which to read
978
   @param[out] data    Pointer for the returned value.
979
 
980
   @return  system error (positive) or OR1K JTAG error code */
981
/*---------------------------------------------------------------------------*/
982
 
983
static enum or1k_jtag_errors
984
jp1_read_jtag_reg (unsigned int  regnum,
985
                   ULONGEST     *data)
986
{
987
  int  retry;
988
 
989
  jp1_ll_prepare_data ();               /* SELECT_DR SCAN */
990
 
991
  for (retry = 0; retry < JP1_NUM_RETRIES; retry++)
992
    {
993
      int  crc_read        = 0;
994
      int  crc_write       = 0;
995
      int  crc_actual_read = 0;
996
      int  crc_ok          = 0;
997
 
998
      jp1_ll_write_jp1 (0, 0);    /* CAPTURE_DR */
999
      jp1_ll_write_jp1 (0, 0);    /* SHIFT_DR */
1000
      jp1_crc_w = 0;
1001
 
1002
      /* write regnum */
1003
      jp1_ll_write_stream((ULONGEST)regnum,
1004
                          jp1_chain_addr_size[or1k_jtag_current_chain], 0);
1005
 
1006
      /* read (R/W=0) */
1007
      if (jp1_chain_has_rw[or1k_jtag_current_chain])
1008
        {
1009
          jp1_ll_write_jp1 (0, 0);
1010
        }
1011
 
1012
      if (jp1_chain_has_crc[or1k_jtag_current_chain])
1013
        {
1014
          jp1_crc_r = 0;
1015
 
1016
          /* *data = 0 */
1017
          *data = jp1_ll_read_stream (0,
1018
                                      jp1_chain_data_size[or1k_jtag_current_chain],
1019
                                      0);
1020
          crc_write       = jp1_crc_w;
1021
          crc_actual_read = crc_read;
1022
 
1023
          /* Send my crc, EXIT1_DR */
1024
          crc_read = (int)jp1_ll_read_stream (crc_write, JP1_CRC_SIZE, 1);
1025
        }
1026
 
1027
      jp1_ll_write_jp1 (1, 0);   /* UPDATE_DR */
1028
 
1029
      /* Did JTAG receive packet correctly? */
1030
      if (jp1_chain_has_crc[or1k_jtag_current_chain])
1031
        {
1032
          crc_ok = jp1_ll_read_jp1 ();
1033
        }
1034
 
1035
      jp1_ll_write_jp1 (1, 0);   /* SELECT_DR */
1036
      if (jp1_chain_has_crc[or1k_jtag_current_chain])
1037
        {
1038
          if ((crc_read == crc_actual_read) && crc_ok)
1039
            {
1040
              return  OR1K_JTAG_ERR_CRC;  /* CZ */
1041
            }
1042
          JP1_RETRY_WAIT ();
1043
        }
1044
      else
1045
        {
1046
          return  OR1K_JTAG_ERR_CRC;  /* CZ */
1047
        }
1048
    }
1049
 
1050
  return  OR1K_JTAG_ERR_NONE;
1051
 
1052
}       /* jp1_read_jtag_reg() */
1053
 
1054
 
1055
/*----------------------------------------------------------------------------*/
1056
/*!Write a JTAG register on a local JTAG connection.
1057
 
1058
   What this does depends on which chain is selected. Drive the JTAG state
1059
   machine to read a value from a register
1060
 
1061
   @param[in]  regnum  The register to write to
1062
   @param[in]  data    Data to write
1063
 
1064
   @return  system error (positive) or OR1K JTAG error code */
1065
/*---------------------------------------------------------------------------*/
1066
 
1067
static enum or1k_jtag_errors
1068
jp1_write_jtag_reg (unsigned int  regnum,
1069
                    ULONGEST      data)
1070
{
1071
  int       retry;
1072
 
1073
  jp1_ll_prepare_data ();               /* SELECT_DR SCAN */
1074
 
1075
  /* If we don't have rw bit, we assume chain is read only. */
1076
  if (!jp1_chain_has_rw[or1k_jtag_current_chain])
1077
    {
1078
      error ("or1k_jtag_write_reg: Chain not writable");
1079
    }
1080
 
1081
  for (retry = 0; retry < JP1_NUM_RETRIES; retry++)
1082
    {
1083
      int  crc_read  = 0;
1084
      int  crc_write = 0;
1085
      int  crc_ok    = 0;
1086
 
1087
      jp1_ll_write_jp1 (0, 0);    /* CAPTURE_DR */
1088
      jp1_ll_write_jp1 (0, 0);    /* SHIFT_DR */
1089
      jp1_crc_w = 0;
1090
 
1091
      /* write regnum */
1092
      jp1_ll_write_stream((ULONGEST)regnum,
1093
                          jp1_chain_addr_size[or1k_jtag_current_chain], 0);
1094
 
1095
      /* write (R/W=1) - we tested that previously. */
1096
      jp1_ll_write_jp1 (0, 1);   /* TDI=1 */
1097
 
1098
      /* write data */
1099
      jp1_ll_write_stream (data, jp1_chain_data_size[or1k_jtag_current_chain],
1100
                         0);
1101
      if (jp1_chain_has_crc[or1k_jtag_current_chain])
1102
        {
1103
          crc_write = jp1_crc_w;
1104
 
1105
          /* write CRC, EXIT1_DR */
1106
          crc_read = (int)jp1_ll_read_stream  (crc_write, JP1_CRC_SIZE, 1);
1107
        }
1108
 
1109
      jp1_ll_write_jp1 (1, 0);   /* UPDATE_DR */
1110
 
1111
      /* Did JTAG receive packet correctly? */
1112
      if (jp1_chain_has_crc[or1k_jtag_current_chain])
1113
        {
1114
          crc_ok = jp1_ll_read_jp1 ();
1115
        }
1116
 
1117
      jp1_ll_write_jp1 (1, 0);   /* SELECT_DR */
1118
      if (jp1_chain_has_crc[or1k_jtag_current_chain])
1119
        {
1120
          if ((crc_read == crc_write) && crc_ok)
1121
            {
1122
              return  OR1K_JTAG_ERR_NONE;
1123
            }
1124
          JP1_RETRY_WAIT ();
1125
        }
1126
      else
1127
        {
1128
          return  OR1K_JTAG_ERR_NONE;
1129
        }
1130
    }
1131
  return  OR1K_JTAG_ERR_CRC;
1132
 
1133
}       /* jp1_write_jtag_reg() */
1134
 
1135
 
1136
 
1137
/*----------------------------------------------------------------------------*/
1138
/*!Process a local OpenRISC 1000 JTAG block read command
1139
 
1140
   Process the JTAG state machine to read values from a block of
1141
   registers/memory. Built on top of the local routine to read a single
1142
   register.
1143
 
1144
   If any of the individual reads fail, give up and return the error code of
1145
   that failure. Otherwise return success.
1146
 
1147
   @param[in]  regnum  The first register for the block from which to read
1148
   @param[out] bdata   Pointer to block for the returned values
1149
   @param[out] count   How many registers to read.
1150
 
1151
   @return  system error (positive) or OR1K JTAG error code */
1152
/*---------------------------------------------------------------------------*/
1153
 
1154
static enum or1k_jtag_errors
1155
jp1_read_jtag_block (unsigned int  regnum,
1156
                     uint32_t     *bdata,
1157
                     int           count)
1158
{
1159
  ULONGEST               data;
1160
  enum or1k_jtag_errors  result;
1161
  int                    i;
1162
 
1163
  for (i=0; i < count; i++)
1164
    {
1165
      result = jp1_read_jtag_reg (regnum + i, &data);
1166
      if (OR1K_JTAG_ERR_NONE != result)
1167
        {
1168
          return result;
1169
        }
1170
 
1171
      bdata[i] = (uint32_t)data;
1172
    }
1173
 
1174
  return  OR1K_JTAG_ERR_NONE;
1175
 
1176
}       /* jp1_read_jtag_block() */
1177
 
1178
 
1179
/*----------------------------------------------------------------------------*/
1180
/*!Process a local OpenRISC 1000 JTAG block write command
1181
 
1182
   Process the JTAG state machine to write values to a block of
1183
   registers/memory. Built on top of the local routine to write a single
1184
   register.
1185
 
1186
   If any of the individual writes fail, give up and return the error code of
1187
   that failure. Otherwise return success.
1188
 
1189
   @param[in]  regnum  The first register for the block to which to write
1190
   @param[out] bdata   Pointer to block for the values to write
1191
   @param[out] count   How many registers to write.
1192
 
1193
   @return  system error (positive) or OR1K JTAG error code */
1194
/*---------------------------------------------------------------------------*/
1195
 
1196
static enum or1k_jtag_errors
1197
jp1_write_jtag_block (unsigned int    regnum,
1198
                      const uint32_t *bdata,
1199
                      int             count)
1200
{
1201
  enum or1k_jtag_errors  result;
1202
  int                    i;
1203
 
1204
  for (i=0; i < count; i++)
1205
    {
1206
      result = jp1_write_jtag_reg (regnum + i, (ULONGEST)(bdata[i]));
1207
 
1208
      if (OR1K_JTAG_ERR_NONE != result)
1209
        {
1210
          return result;
1211
        }
1212
    }
1213
 
1214
  return  OR1K_JTAG_ERR_NONE;
1215
 
1216
}       /* jp1_write_jtag_block() */
1217
 
1218
 
1219
/*----------------------------------------------------------------------------*/
1220
/*!Process a local OpenRISC 1000 JTAG set chain command
1221
 
1222
   Process the JTAG state machine to select the specified chain, unless it is
1223
   already selected.
1224
 
1225
   @param[in] chain  The scan chain to use
1226
 
1227
   @return  system error (positive) or OR1K JTAG error code */
1228
/*---------------------------------------------------------------------------*/
1229
 
1230
static enum or1k_jtag_errors
1231
jp1_select_chain (int  chain)
1232
{
1233
  enum or1k_jtag_errors  result;
1234
 
1235
  /* Nothing to do if we already have the chain we want */
1236
  if (or1k_jtag_current_chain == chain)
1237
    {
1238
      return  OR1K_JTAG_ERR_NONE;
1239
    }
1240
 
1241
  /* Is it a chain we are allowed to set */
1242
  if (!jp1_chain_is_valid[chain])
1243
    {
1244
      return OR1K_JTAG_ERR_INVALID_CHAIN;
1245
    }
1246
 
1247
  jp1_ll_prepare_control ();    /* Select IR scan*/
1248
 
1249
  jp1_ll_write_jp1 (0, 0);                /* CAPTURE_IR */
1250
  jp1_ll_write_jp1 (0, 0);                /* SHIFT_IR */
1251
 
1252
  /* write data, EXIT1_IR */
1253
  jp1_ll_write_stream((ULONGEST)OR1K_JI_CHAIN_SELECT, OR1K_JI_SIZE, 1);
1254
 
1255
  jp1_ll_write_jp1 (1, 0);               /* UPDATE_IR */
1256
  jp1_ll_write_jp1 (1, 0);               /* SELECT_DR */
1257
 
1258
  jp1_ll_write_jp1 (0, 0);                /* CAPTURE_DR */
1259
  jp1_ll_write_jp1 (0, 0);                /* SHIFT_DR */
1260
 
1261
  /* write data, EXIT1_DR */
1262
  jp1_ll_write_stream ((ULONGEST)chain, OR1K_SC_SIZE, 1);
1263
 
1264
  jp1_ll_write_jp1 (1, 0);               /* UPDATE_DR */
1265
  jp1_ll_write_jp1 (1, 0);               /* SELECT_DR */
1266
 
1267
  /* Now we have to go out of SELECT_CHAIN mode.  */
1268
  jp1_ll_write_jp1 (1, 0);               /* SELECT_IR */
1269
  jp1_ll_write_jp1 (0, 0);                /* CAPTURE_IR */
1270
  jp1_ll_write_jp1 (0, 0);                /* SHIFT_IR */
1271
 
1272
  /* write data, EXIT1_IR */
1273
  jp1_ll_write_stream ((ULONGEST)OR1K_JI_DEBUG, OR1K_JI_SIZE, 1);
1274
 
1275
  jp1_ll_write_jp1 (1, 0);               /* UPDATE_IR */
1276
  jp1_ll_write_jp1 (1, 0);               /* SELECT_DR */
1277
 
1278
  or1k_select_dr     = 1;
1279
  or1k_jtag_current_chain = chain;
1280
 
1281
  return OR1K_JTAG_ERR_NONE;
1282
 
1283
}       /* jp1_select_chain() */
1284
 
1285
 
1286
 
1287
/* These are the OpenRISC 1000 JTAG Protocol routines for use with a remote
1288
   server. They are built on tol of the remote protocol low level helper
1289
   functions. All these functions are static and have the prefix jtr_ */
1290
 
1291
/*----------------------------------------------------------------------------*/
1292
/*!Read a JTAG register from a remote OpenRISC 1000 JTAG server
1293
 
1294
   What this does depends on which chain is selected.
1295
 
1296
   Build the command from the args and send it. If successful get the
1297
   response.
1298
 
1299
   All args and the results are in host format. All transmitted data is in
1300
   network format. This routine converts between the two
1301
 
1302
   @param[in]  regnum  The JTAG register to read
1303
   @param[out] data    Pointer for the returned value.
1304
 
1305
   @return  system error (positive) or OR1K JTAG error code */
1306
/*---------------------------------------------------------------------------*/
1307
 
1308
static enum or1k_jtag_errors
1309
jtr_read_jtag_reg (unsigned int  regnum,
1310
                   ULONGEST     *data)
1311
{
1312
  enum or1k_jtag_errors  result;
1313
  int                    fd = or1k_jtag_connection.device.fd;
1314
  unsigned long int      len;
1315
  unsigned long int      data_h;
1316
  unsigned long int      data_l;
1317
 
1318
  struct jtr_read_message   tx_buf;
1319
  struct jtr_read_response  rx_buf;
1320
 
1321
  /* Build the command in network format */
1322
  len = sizeof (tx_buf)  - sizeof (tx_buf.command)  - sizeof (tx_buf.length);
1323
 
1324
  tx_buf.command = htonl (OR1K_JTAG_COMMAND_READ);
1325
  tx_buf.length  = htonl (len);
1326
  tx_buf.address = htonl (regnum);
1327
 
1328
  /* Try to send the command */
1329
  result = jtr_ll_write (fd, &tx_buf, sizeof (tx_buf) );
1330
  if (OR1K_JTAG_ERR_NONE != result)
1331
    {
1332
      return  jtr_ll_check (result);
1333
    }
1334
 
1335
  /* Try to get the result */
1336
  result = jtr_ll_response (fd, &rx_buf, sizeof (rx_buf) );
1337
  if (OR1K_JTAG_ERR_NONE != result)
1338
    {
1339
      return  jtr_ll_check (result);
1340
    }
1341
 
1342
  /* Break out the data */
1343
  data_h  = ntohl (rx_buf.data_h);
1344
  data_l  = ntohl (rx_buf.data_l);
1345
 
1346
  *data = (((ULONGEST)data_h) << 32) | (ULONGEST)data_l;
1347
  return jtr_ll_check (ntohl (rx_buf.status) );
1348
 
1349
}       /* jtr_read_jtag_reg() */
1350
 
1351
 
1352
/*----------------------------------------------------------------------------*/
1353
/*!Write a JTAG register on a remote OpenRISC 1000 JTAG server
1354
 
1355
   What this does depends on which chain is selected.
1356
 
1357
   Build the command from the args and send it. If successful get the
1358
   response.
1359
 
1360
   All args and the results are in host format. All transmitted data is in
1361
   network format. This routine converts between the two
1362
 
1363
   @param[in]  regnum  The JTAG register to write
1364
   @param[out] data    The data to write
1365
 
1366
   @return  system error (positive) or OR1K JTAG error code */
1367
/*---------------------------------------------------------------------------*/
1368
 
1369
static enum or1k_jtag_errors
1370
jtr_write_jtag_reg (unsigned long int  regnum,
1371
                    ULONGEST           data)
1372
{
1373
  enum or1k_jtag_errors  result;
1374
  int                    fd = or1k_jtag_connection.device.fd;
1375
  unsigned long int      len;
1376
 
1377
  struct jtr_write_message   tx_buf;
1378
  struct jtr_write_response  rx_buf;
1379
 
1380
  /* Build the command in network format */
1381
  len = sizeof (tx_buf)  - sizeof (tx_buf.command)  - sizeof (tx_buf.length);
1382
 
1383
  tx_buf.command = htonl (OR1K_JTAG_COMMAND_WRITE);
1384
  tx_buf.length  = htonl (len);
1385
  tx_buf.address = htonl (regnum);
1386
  tx_buf.data_h  = htonl((uint32_t)(data >> 32));
1387
  tx_buf.data_l  = htonl((uint32_t)(data & 0xffffffff));
1388
 
1389
  /* Try to send the command */
1390
  result = jtr_ll_write (fd, &tx_buf, sizeof (tx_buf) );
1391
  if (OR1K_JTAG_ERR_NONE != result)
1392
    {
1393
      return  jtr_ll_check (result);
1394
    }
1395
 
1396
  /* Try to get the result */
1397
  result = jtr_ll_response (fd, &rx_buf, sizeof (rx_buf) );
1398
  if (OR1K_JTAG_ERR_NONE != result)
1399
    {
1400
      return  jtr_ll_check (result);
1401
    }
1402
 
1403
  /* Break out the response */
1404
  return  jtr_ll_check (ntohl (rx_buf.status) );
1405
 
1406
}       /* jtr_write_jtag_reg() */
1407
 
1408
 
1409
/*----------------------------------------------------------------------------*/
1410
/*!Read a block of JTAG registers from a remote OpenRISC 1000 JTAG server
1411
 
1412
   What this does depends on which chain is selected.
1413
 
1414
   Construct the command from the args and send it. If successful get the
1415
   response.
1416
 
1417
   All args and the results are in host format. All transmitted data is in
1418
   network format. This routine converts between the two
1419
 
1420
   @param[in]  regnum  The first register to read
1421
   @param[out] bdata   Pointer to block of memory for the returned values.
1422
   @param[out] count   Number of registers to read.
1423
 
1424
   @return  system error (positive) or OR1K JTAG error code */
1425
/*---------------------------------------------------------------------------*/
1426
 
1427
static enum or1k_jtag_errors
1428
jtr_read_jtag_block (CORE_ADDR  regnum,
1429
                     uint32_t  *bdata,
1430
                     int        count)
1431
{
1432
  enum or1k_jtag_errors  result;
1433
  int                    fd = or1k_jtag_connection.device.fd;
1434
  unsigned long int      count_rv;
1435
  unsigned long int      len;
1436
  int                    r;
1437
 
1438
  struct jtr_read_block_message   tx_buf;
1439
  struct jtr_read_block_response  rx_buf;
1440
 
1441
  /* Build the command in network format */
1442
  len = sizeof (tx_buf)  - sizeof (tx_buf.command)  - sizeof (tx_buf.length);
1443
 
1444
  tx_buf.command  = htonl (OR1K_JTAG_COMMAND_READ_BLOCK);
1445
  tx_buf.length   = htonl (len);
1446
  tx_buf.address  = htonl (regnum);
1447
  tx_buf.num_regs = htonl (count);
1448
 
1449
  /* Try to send the command */
1450
  result = jtr_ll_write (fd, &tx_buf, sizeof (tx_buf) );
1451
  if (OR1K_JTAG_ERR_NONE != result)
1452
    {
1453
      return  jtr_ll_check (result);
1454
    }
1455
 
1456
  /* Try to get the result. This will have just the status and number of regs */
1457
  result = jtr_ll_response (fd, &rx_buf, sizeof (rx_buf) );
1458
  if (OR1K_JTAG_ERR_NONE != result)
1459
    {
1460
      return  jtr_ll_check (result);
1461
    }
1462
 
1463
  /* Break out the num regs received */
1464
  count_rv = ntohl (rx_buf.num_regs);
1465
 
1466
  /* Must get the same number of regs back */
1467
  if (count_rv != count)
1468
    {
1469
      /* Read the offered registers back to clear the protocol. We don't want
1470
         them going in to the result buffer and their could be an arbitary
1471
         number, so get them one at a time. */
1472
      for (r =  0; r < count_rv; r++)
1473
        {
1474
          uint32_t  dummy_data;
1475
 
1476
          result = jtr_ll_read (fd, &dummy_data, sizeof (dummy_data) );
1477
          if (OR1K_JTAG_ERR_NONE != result)
1478
            {
1479
              return  jtr_ll_check (result);
1480
            }
1481
        }
1482
 
1483
      return OR1K_JTAG_ERR_PROTOCOL_ERROR;
1484
    }
1485
 
1486
  /* Get the regs back and then convert them to host representation */
1487
  result = jtr_ll_read (fd, bdata, count * sizeof (uint32_t) );
1488
  if (OR1K_JTAG_ERR_NONE != result)
1489
    {
1490
      return  jtr_ll_check (result);
1491
    }
1492
 
1493
  for (r = 0; r < count; r++)
1494
    {
1495
      bdata[r] = ntohl (bdata[r]);
1496
    }
1497
 
1498
  /* Return the result */
1499
  return  jtr_ll_check (ntohl (rx_buf.status) );
1500
 
1501
}       /* jtr_read_jtag_block() */
1502
 
1503
 
1504
/*----------------------------------------------------------------------------*/
1505
/*!Write a block of JTAG registers on a remote OpenRISC 1000 JTAG server
1506
 
1507
   What this does depends on which chain is selected.
1508
 
1509
   Construct the command from the args and send it. If successful get the
1510
   response.
1511
 
1512
   All args and the results are in host format. All transmitted data is in
1513
   network format. This routine converts between the two
1514
 
1515
   @param[in]  regnum  The start address address for the block to be read
1516
   @param[out] bdata   Pointer to values to write to the block of memory.
1517
   @param[out] count   Number of values to read.
1518
 
1519
   @return  system error (positive) or OR1K JTAG error code */
1520
/*---------------------------------------------------------------------------*/
1521
 
1522
static enum or1k_jtag_errors
1523
jtr_write_jtag_block (CORE_ADDR       regnum,
1524
                      const uint32_t *bdata,
1525
                      int             count)
1526
{
1527
  enum or1k_jtag_errors  result;
1528
  int                    fd = or1k_jtag_connection.device.fd;
1529
  unsigned long int      len;
1530
  unsigned long int      full_len;
1531
  int                    r;
1532
 
1533
  struct jtr_write_block_message  *tx_buf;      /* Allocate dynamically */
1534
  struct jtr_write_block_response  rx_buf;
1535
 
1536
  /* Allocate a buffer large enough to send, then build the command */
1537
  full_len = sizeof (*tx_buf)  +  (count - 1)  * sizeof (uint32_t);
1538
  len      = full_len - sizeof (tx_buf->command)  - sizeof (tx_buf->length);
1539
  tx_buf   = (struct jtr_write_block_message *)(xmalloc (full_len) );
1540
 
1541
  tx_buf->command  = htonl (OR1K_JTAG_COMMAND_WRITE_BLOCK);
1542
  tx_buf->length   = htonl (len);
1543
  tx_buf->address  = htonl (regnum);
1544
  tx_buf->num_regs = htonl (count);
1545
 
1546
  for (r = 0; r < count; r++)
1547
    {
1548
      tx_buf->data[r] = htonl (bdata[r]);
1549
    }
1550
 
1551
  /* Try to send the command, after which we can free the buffer */
1552
  result = jtr_ll_write (fd, tx_buf, full_len);
1553
  xfree (tx_buf);
1554
 
1555
  if (OR1K_JTAG_ERR_NONE != result)
1556
    {
1557
      return  jtr_ll_check (result);
1558
    }
1559
 
1560
  /* Try to get the result */
1561
  result = jtr_ll_response (fd, &rx_buf, sizeof (rx_buf) );
1562
  if (OR1K_JTAG_ERR_NONE != result)
1563
    {
1564
      return  jtr_ll_check (result);
1565
    }
1566
 
1567
  return jtr_ll_check (ntohl (rx_buf.status) );
1568
 
1569
}       /* jtr_write_jtag_block() */
1570
 
1571
 
1572
/*----------------------------------------------------------------------------*/
1573
/*!Select a remote OpenRISC 1000 JTAG chain command
1574
 
1575
   Construct the command from the args and send it. If successful get the
1576
   response.
1577
 
1578
   All args and the results are in host format. All transmitted data is in
1579
   network format. This routine converts between the two
1580
 
1581
   @param[in] chain  The scan chain to use
1582
 
1583
   @return  system error (positive) or OR1K JTAG error code */
1584
/*---------------------------------------------------------------------------*/
1585
 
1586
static enum or1k_jtag_errors
1587
jtr_select_chain (int  chain)
1588
{
1589
  enum or1k_jtag_errors  result;
1590
  int                    fd = or1k_jtag_connection.device.fd;
1591
  unsigned long int      len;
1592
 
1593
  struct jtr_chain_message   tx_buf;
1594
  struct jtr_chain_response  rx_buf;
1595
 
1596
  /* Nothing to do if we already have the chain we want */
1597
  if (or1k_jtag_current_chain == chain)
1598
    {
1599
      return  OR1K_JTAG_ERR_NONE;
1600
    }
1601
 
1602
  /* Build the command in network format */
1603
  len = sizeof (tx_buf)  - sizeof (tx_buf.command)  - sizeof (tx_buf.length);
1604
 
1605
  tx_buf.command = htonl (OR1K_JTAG_COMMAND_CHAIN);
1606
  tx_buf.length  = htonl (len);
1607
  tx_buf.chain   = htonl (chain);
1608
 
1609
  /* Try to send the command */
1610
  result = jtr_ll_write (fd, &tx_buf, sizeof (tx_buf) );
1611
  if (OR1K_JTAG_ERR_NONE != result)
1612
    {
1613
      or1k_jtag_current_chain = OR1K_SC_UNDEF;
1614
      return  jtr_ll_check (result);
1615
    }
1616
 
1617
  /* Try to get the result */
1618
  result = jtr_ll_response (fd, &rx_buf, sizeof (rx_buf) );
1619
  if (OR1K_JTAG_ERR_NONE != result)
1620
    {
1621
      or1k_jtag_current_chain = OR1K_SC_UNDEF;
1622
      return  jtr_ll_check (result);
1623
    }
1624
 
1625
  result = jtr_ll_check (ntohl (rx_buf.status) );
1626
 
1627
  if (OR1K_JTAG_ERR_NONE != result)
1628
    {
1629
      or1k_jtag_current_chain = OR1K_SC_UNDEF;
1630
    }
1631
  else
1632
    {
1633
      or1k_jtag_current_chain = chain;
1634
    }
1635
 
1636
  return  result;
1637
 
1638
}       /* jtr_select_chain() */
1639
 
1640
 
1641
 
1642
/* These are the high level functions based on the JTAG Remote protocol,
1643
   independent of whether they are local or remote. They just split the calls
1644
   down to their local or remote variants. All errors are dealt with by this
1645
   stage, so all functions have void returns. All functions are static and
1646
   begin or1k_jtag_ */
1647
 
1648
/*----------------------------------------------------------------------------*/
1649
/*!Function to read a JTAG register
1650
 
1651
   Call either the local or remote version of the corresponding JTAG function
1652
   as appropriate.
1653
 
1654
   @param[in]  regnum  The JTAG register to read
1655
   @param[out] data    Where to put the written value
1656
 
1657
   @return  system error (positive) or OR1K JTAG error code */
1658
/*---------------------------------------------------------------------------*/
1659
 
1660
static void
1661
or1k_jtag_read_jtag_reg (unsigned int  regnum,
1662
                         ULONGEST     *data)
1663
{
1664
  enum or1k_jtag_errors  result;
1665
 
1666
  switch (or1k_jtag_connection.location)
1667
    {
1668
    case OR1K_JTAG_LOCAL:
1669
      result = jp1_read_jtag_reg (regnum, data);
1670
      break;
1671
 
1672
    case OR1K_JTAG_REMOTE:
1673
      result = jtr_read_jtag_reg (regnum, data);
1674
      break;
1675
 
1676
    default:
1677
      result = OR1K_JTAG_ERR_NO_CONNECTION;
1678
      break;
1679
    }
1680
 
1681
  if (OR1K_JTAG_ERR_NONE != result)
1682
    {
1683
      error ("or1k_jtag_read_jtag_reg: regnum %u, data 0x%p, result %s\n",
1684
             regnum, data, or1k_jtag_err_name (result) );
1685
    }
1686
}       /* or1k_jtag_read_jtag_reg() */
1687
 
1688
 
1689
/*----------------------------------------------------------------------------*/
1690
/*!Function to write a JTAG register
1691
 
1692
   Call either the local or remote version of the corresponding JTAG function
1693
   as appropriate.
1694
 
1695
   @param[in] regnum  The JTAG register to write
1696
   @param[in] data    The value to write
1697
 
1698
   @return  system error (positive) or OR1K JTAG error code */
1699
/*---------------------------------------------------------------------------*/
1700
 
1701
static void
1702
or1k_jtag_write_jtag_reg (unsigned int  regnum,
1703
                          ULONGEST      data)
1704
{
1705
  enum or1k_jtag_errors  result;
1706
 
1707
  switch (or1k_jtag_connection.location)
1708
    {
1709
    case OR1K_JTAG_LOCAL:
1710
      result = jp1_write_jtag_reg((unsigned long int)regnum, data);
1711
      break;
1712
 
1713
    case OR1K_JTAG_REMOTE:
1714
      result = jtr_write_jtag_reg((unsigned long int)regnum, data);
1715
      break;
1716
 
1717
    default:
1718
      result = OR1K_JTAG_ERR_NO_CONNECTION;
1719
      break;
1720
    }
1721
 
1722
  if (OR1K_JTAG_ERR_NONE != result)
1723
    {
1724
      error ("or1k_jtag_write_jtag_reg: regnum %u, data 0x%16llx, result %s\n",
1725
             regnum, (long long unsigned int)data, or1k_jtag_err_name (result) );
1726
    }
1727
}       /* or1k_jtag_write_jtag_reg() */
1728
 
1729
 
1730
/*----------------------------------------------------------------------------*/
1731
/*!Function to read a block of JTAG registers via OpenRISC 1000 JTAG
1732
 
1733
   Call either the local or remote version of the corresponding JTAG function
1734
   as appropriate.
1735
 
1736
   @param[in]   regnum The starting register
1737
   @param[out]  bdata  Pointer to a buffer for the results
1738
   @param[in]   count  Number of registers to be read
1739
 
1740
   @return  system error (positive) or OR1K JTAG error code */
1741
/*---------------------------------------------------------------------------*/
1742
 
1743
static void
1744
or1k_jtag_read_jtag_block (unsigned int  regnum,
1745
                           uint32_t     *bdata,
1746
                           int           count)
1747
{
1748
  enum or1k_jtag_errors  result;
1749
 
1750
  switch (or1k_jtag_connection.location)
1751
    {
1752
    case OR1K_JTAG_LOCAL:
1753
      result = jp1_read_jtag_block (regnum, bdata, count);
1754
      break;
1755
 
1756
    case OR1K_JTAG_REMOTE:
1757
      result = jtr_read_jtag_block (regnum, bdata, count);
1758
      break;
1759
 
1760
    default:
1761
      result = OR1K_JTAG_ERR_NO_CONNECTION;
1762
      break;
1763
    }
1764
 
1765
  if (OR1K_JTAG_ERR_NONE != result)
1766
    {
1767
      error ("or1k_jtag_read_jtag_block: "
1768
             "regnum %u, bdata 0x%p, count %d, result %s\n",
1769
             regnum, bdata, count, or1k_jtag_err_name (result) );
1770
    }
1771
}       /* or1k_jtag_read_jtag_block() */
1772
 
1773
 
1774
/*----------------------------------------------------------------------------*/
1775
/*!Function to write a block of JTAG registers via OpenRISC 1000 JTAG
1776
 
1777
   Call either the local or remote version of the corresponding JTAG function
1778
   as appropriate.
1779
 
1780
   @param[in]  regnum  The starting register to write
1781
   @param[out] bdata   Pointer to a buffer with the values to write
1782
   @param[in]  count  Number of values to write
1783
 
1784
   @return  system error (positive) or OR1K JTAG error code */
1785
/*---------------------------------------------------------------------------*/
1786
 
1787
static void
1788
or1k_jtag_write_jtag_block (unsigned int    regnum,
1789
                            const uint32_t *bdata,
1790
                            int             count)
1791
{
1792
  enum or1k_jtag_errors  result;
1793
 
1794
  switch (or1k_jtag_connection.location)
1795
    {
1796
    case OR1K_JTAG_LOCAL:
1797
      result = jp1_write_jtag_block (regnum, bdata, count);
1798
      break;
1799
 
1800
    case OR1K_JTAG_REMOTE:
1801
      result = jtr_write_jtag_block (regnum, bdata, count);
1802
      break;
1803
 
1804
    default:
1805
      result = OR1K_JTAG_ERR_NO_CONNECTION;
1806
      break;
1807
  }
1808
 
1809
  if (OR1K_JTAG_ERR_NONE != result)
1810
    {
1811
      error ("or1k_jtag_write_jtag_block: "
1812
             "regnum %u, bdata 0x%p, count %d, result %s\n",
1813
             regnum, bdata, count, or1k_jtag_err_name (result) );
1814
    }
1815
}       /* or1k_jtag_write_jtag_block() */
1816
 
1817
 
1818
/*----------------------------------------------------------------------------*/
1819
/*!Function to select a scan chain via OpenRISC 1000 JTAG
1820
 
1821
   Call either the local or remote version of the corresponding JTAG function
1822
   as appropriate.
1823
 
1824
   @param[in] chain  The scan chain to use
1825
 
1826
   @return  system error (positive) or OR1K JTAG error code */
1827
/*---------------------------------------------------------------------------*/
1828
 
1829
static void
1830
or1k_jtag_select_chain (int  chain)
1831
{
1832
  enum or1k_jtag_errors  result;
1833
 
1834
  switch (or1k_jtag_connection.location)
1835
    {
1836
    case OR1K_JTAG_LOCAL:
1837
      result = jp1_select_chain (chain);
1838
      break;
1839
 
1840
    case OR1K_JTAG_REMOTE:
1841
      result = jtr_select_chain (chain);
1842
      break;
1843
 
1844
    default:
1845
      result = OR1K_JTAG_ERR_NO_CONNECTION;
1846
      break;
1847
    }
1848
 
1849
  if (OR1K_JTAG_ERR_NONE != result)
1850
    {
1851
      error ("or1k_jtag_select_chain: chain %d, result %s\n", chain,
1852
             or1k_jtag_err_name (result) );
1853
    }
1854
}       /* or1k_jtag_select_chain() */
1855
 
1856
 
1857
/*----------------------------------------------------------------------------*/
1858
/*!Function to reset the CPU.
1859
 
1860
   This takes the processor into reset and then releases the reset.
1861
 
1862
   The code is drawn from the GDB 5.3 code, pending clarification of how the
1863
   reset works on real hardware. However there is no attempt to clear HW trace
1864
   (not available with Igor Moder's version of the JTAG hardware), nor to set
1865
   the stall flag (reset is defined to clear stall).
1866
 
1867
   Use the underlying JTAG protocol calls to select the relevant scan chain
1868
   and change the relevant registers. Always succeeds.
1869
*/
1870
/*---------------------------------------------------------------------------*/
1871
 
1872
static void
1873
or1k_jtag_reset()
1874
{
1875
  ULONGEST               data;
1876
 
1877
  /* Select the correct chain for controlling the processor */
1878
  or1k_jtag_select_chain (OR1K_SC_REGISTER);
1879
 
1880
  /* Read the JTAG register, set the and reset bit and write it back */
1881
  or1k_jtag_read_jtag_reg (OR1K_JTAG_RISCOP, &data);
1882
  data |= OR1K_JTAG_RISCOP_RESET;
1883
  or1k_jtag_write_jtag_reg (OR1K_JTAG_RISCOP, data);
1884
 
1885
  /* Wait 1000 microseconds. The old code used to loop reading the register
1886
     100 times before this, but we've left that out. */
1887
  usleep (1000);
1888
 
1889
  /* Clear the reset bit and write it back. The old code used to read the
1890
     register back (and ignore the result), but we don't bother. */
1891
  data &= ~OR1K_JTAG_RISCOP_RESET;
1892
  or1k_jtag_write_jtag_reg (OR1K_JTAG_RISCOP, data);
1893
 
1894
}       /* or1k_jtag_reset() */
1895
 
1896
 
1897
/*----------------------------------------------------------------------------*/
1898
/*!Utility method to turn an OpenRISC 1000 JTAG error into a string
1899
 
1900
   Positive error values are system errors, negative values OpenRISC 100 JTAG
1901
   errors.
1902
 
1903
   @param[in] e  The error number */
1904
/*---------------------------------------------------------------------------*/
1905
 
1906
static const char *
1907
or1k_jtag_err_name (enum or1k_jtag_errors  e)
1908
{
1909
  const char *jtag_err[] =
1910
    {
1911
      "None",
1912
      "JTAG CRC error",
1913
      "JTAG memory error",
1914
      "Invalid JTAG command"
1915
      "Remote JTAG server terminated",
1916
      "No JTAG server connection",
1917
      "JTAG protocol error",
1918
      "JTAG command not implemented",
1919
      "Invalid JTAG chain",
1920
      "Invalid JTAG address",
1921
      "JTAG access exception",
1922
      "Invalid JTAG length",
1923
      "JTAG out of memory"
1924
    };
1925
 
1926
  if (e > 0)
1927
    {
1928
      return strerror (e);
1929
    }
1930
  else
1931
    {
1932
      return jtag_err[-e];
1933
    }
1934
}       /* or1k_jtag_err_name() */
1935
 
1936
 
1937
 
1938
/* These are the high level public functions. They call the underlying
1939
   functions based on the OR1K JTAG protocol. */
1940
 
1941
/*----------------------------------------------------------------------------*/
1942
/*!Initialize a new OpenRISC 1000 JTAG connection.
1943
 
1944
   Initialize a new connection to the or1k board, and make sure we are really
1945
   connected.
1946
 
1947
   @param[in] args  The entity to connect to followed by an optional NO_RESET
1948
   flag */
1949
/*--------------------------------------------------------------------------*/
1950
 
1951
void
1952
or1k_jtag_init (char *args)
1953
{
1954
  char  *port_name;
1955
  char  *reset_arg;
1956
  char **argv;
1957
  int    remote_offset;
1958
 
1959
  if (args == 0)
1960
    { /* CZ */
1961
      error ("or1k_jtag_init: To open a or1k remote debugging connection,\n"
1962
             "you need to specify a parallel port connected to the target\n"
1963
             "board, or else a remote server which will proxy these services\n"
1964
             "for you.\n"
1965
             "Example: /dev/lp0 or jtag://debughost.mydomain.com:8100.\n");
1966
      return;
1967
    }
1968
 
1969
  /* If we currently have an open connection, shut it down. This is due to a
1970
     temporary bug in gdb. */
1971
  switch(or1k_jtag_connection.location)
1972
    {
1973
    case OR1K_JTAG_REMOTE:
1974
      if (or1k_jtag_connection.device.fd > 0)
1975
        {
1976
          jtr_ll_close ();
1977
        }
1978
      break;
1979
    case OR1K_JTAG_LOCAL:
1980
      if (or1k_jtag_connection.device.lp > 0)
1981
        {
1982
          close (or1k_jtag_connection.device.lp);
1983
        }
1984
      break;
1985
    }
1986
 
1987
  /* Parse the port name. Use libiberty to get rid of quote marks etc. */
1988
  argv = buildargv (args);
1989
  if (NULL == argv)
1990
    {
1991
      nomem (0);         /* Memory exhaustion is only cause of failure */
1992
      return;
1993
    }
1994
  port_name = xstrdup (argv[0]);
1995
 
1996
  if (NULL != argv[1])
1997
    {
1998
      reset_arg = xstrdup (argv[1]);
1999
    }
2000
  else
2001
    {
2002
      reset_arg = NULL;
2003
    }
2004
 
2005
  make_cleanup_freeargv (argv);
2006
 
2007
  /* CZ 24/05/01 - Check to see if we have specified a remote jtag interface
2008
     or a local one. It is remote if it follows one of the URL naming
2009
     conventions:
2010
 
2011
     jtag://<hostname>:<port_or_service> (default debug interface)
2012
     jtag_orpsoc://<hostname>:<port_or_service> (orpsoc debug interface)
2013
     jtag_mohor://<hostname>:<port_or_service> (Igor Mohor's debug interface)
2014
 
2015
     Work out the offset to the end of the :// in remote_offset, then use this
2016
     as an indicator of whether a remote or local interface was requested.
2017
  */
2018
  if (0 == strncmp (port_name, "jtag://", 7))
2019
    {
2020
      remote_offset       = 7;
2021
      or1k_dbg_if_version = OR1K_DBG_IF_ORPSOC;
2022
    }
2023
  else if (0 == strncmp (port_name, "jtag_orpsoc://", 14))
2024
    {
2025
      remote_offset       = 14;
2026
      or1k_dbg_if_version = OR1K_DBG_IF_ORPSOC;
2027
    }
2028
  else if (0 == strncmp (port_name, "jtag_mohor://", 13))
2029
    {
2030
      remote_offset       = 13;
2031
      or1k_dbg_if_version = OR1K_DBG_IF_MOHOR;
2032
    }
2033
  else
2034
    {
2035
      remote_offset = 0;
2036
    }
2037
 
2038
  if (remote_offset > 0)
2039
    {
2040
      /* Interface is remote */
2041
      /* Connect to the remote proxy server */
2042
      char *port_or_service = strchr (&(port_name[remote_offset]), ':');
2043
      char  hostname[256];
2044
      int   len;
2045
 
2046
      if (NULL == port_or_service)
2047
        {
2048
          error ("or1k_jtag_init: must specify remote port or service\n");
2049
          free (port_name);
2050
          return;
2051
        }
2052
 
2053
      len = port_or_service - port_name - remote_offset;
2054
      strncpy (hostname, &port_name[remote_offset], len);
2055
      hostname[len] = '\0';
2056
      port_or_service++;
2057
 
2058
      or1k_jtag_connection.device.fd = jtr_ll_connect (hostname,
2059
                                                     port_or_service);
2060
      free (port_name);
2061
      if (0 == or1k_jtag_connection.device.fd)
2062
        {
2063
          error ("or1k_jtag_init: can't access JTAG Proxy Server at \"%s\"",
2064
                 port_name);
2065
          return;
2066
        }
2067
 
2068
      or1k_jtag_connection.location = OR1K_JTAG_REMOTE;
2069
    }
2070
  else
2071
    {
2072
      /* Interface is local */
2073
      /* Open and initialize the parallel port.  */
2074
      or1k_jtag_connection.device.lp = open (port_name, O_WRONLY);
2075
      free (port_name);
2076
 
2077
      if (or1k_jtag_connection.device.lp < 0)
2078
        {
2079
          error ("Cannot open device.");
2080
          return;
2081
        }
2082
 
2083
      jp1_ll_reset_jp1 ();
2084
      or1k_jtag_connection.location = OR1K_JTAG_LOCAL; /* CZ */
2085
    }
2086
 
2087
  or1k_jtag_current_chain = OR1K_SC_UNDEF;
2088
 
2089
  /* See if the user requested a reset */
2090
 
2091
  if (NULL != reset_arg)
2092
    {
2093
      if (0 == strncasecmp( reset_arg, "RESET", strlen ("RESET")))
2094
        {
2095
          or1k_jtag_reset ();
2096
        }
2097
      else
2098
        {
2099
          warning ("or1k_jtag_init: unrecognized argument, assume no reset\n");
2100
        }
2101
    }
2102
 
2103
}       /* or1k_jtag_init() */
2104
 
2105
 
2106
/*----------------------------------------------------------------------------*/
2107
/*!Close down an OpenRISC 1000 JTAG connection.
2108
 
2109
   Fixed an apparent problem - jtr_ll_close() should not be used with the local
2110
   connection, since it isn't a socket.
2111
*/
2112
/*---------------------------------------------------------------------------*/
2113
 
2114
void
2115
or1k_jtag_close()  /* CZ */
2116
{
2117
  switch (or1k_jtag_connection.location)
2118
    {
2119
    case OR1K_JTAG_LOCAL:
2120
      (void)close (or1k_jtag_connection.device.lp);
2121
      or1k_jtag_connection.device.lp = 0;
2122
      break;
2123
 
2124
    case OR1K_JTAG_REMOTE:
2125
      jtr_ll_close ();
2126
      or1k_jtag_connection.device.fd = 0;
2127
      break;
2128
    }
2129
 
2130
  or1k_jtag_connection.location = OR1K_JTAG_NOT_CONNECTED;
2131
 
2132
}       /* or1k_jtag_close() */
2133
 
2134
 
2135
/*----------------------------------------------------------------------------*/
2136
/*!Global function to read a special purpose register via OpenRISC 1000 JTAG
2137
 
2138
   This can be used to read GPRs, since GPRs are mapped onto SPRs.
2139
 
2140
   Use the underlying JTAG protocol calls to select the relevant scan chain
2141
   and get the SPR value. Always succeeds.
2142
 
2143
   @param[in]  sprnum  The address (register) to read
2144
 
2145
   @return  The value read */
2146
/*---------------------------------------------------------------------------*/
2147
 
2148
ULONGEST
2149
or1k_jtag_read_spr (unsigned int  sprnum)
2150
{
2151
  ULONGEST  data;
2152
 
2153
  /* Select the correct chain for SPR read/write, then read */
2154
  or1k_jtag_select_chain (OR1K_SC_RISC_DEBUG);
2155
  or1k_jtag_read_jtag_reg (sprnum, &data);
2156
 
2157
  return  data;
2158
 
2159
}       /* or1k_jtag_read_spr() */
2160
 
2161
 
2162
/*----------------------------------------------------------------------------*/
2163
/*!Global function to write a special purpose register via OpenRISC 1000 JTAG
2164
 
2165
   This can be used to write GPRs, since GPRs are mapped onto SPRs.
2166
 
2167
   Use the underlying JTAG protocol calls to select the relevant scan chain
2168
   and write the SPR value. Always succeeds.
2169
 
2170
   @param[in]  sprnum  The SPR to write
2171
   @param[in]  data    The value to write */
2172
/*---------------------------------------------------------------------------*/
2173
 
2174
void
2175
or1k_jtag_write_spr (unsigned int  sprnum,
2176
                     ULONGEST      data)
2177
{
2178
  /* Select the correct chain for SPR read/write, then write */
2179
  or1k_jtag_select_chain (OR1K_SC_RISC_DEBUG);
2180
  or1k_jtag_write_jtag_reg (sprnum, data);
2181
 
2182
}       /* or1k_jtag_write_spr() */
2183
 
2184
 
2185
/*----------------------------------------------------------------------------*/
2186
/*!Global function to read a block of memory via OpenRISC 1000 JTAG
2187
 
2188
   Use the underlying JTAG protocol calls to select the relevant scan chain
2189
   and read the memory. Always succeeds.
2190
 
2191
   The underlying protocol only reads whole uint32_t words, so we must fix
2192
   this if the address and/or len are not on a word boundary with partial
2193
   reads.
2194
 
2195
   This needs to work even for very small blocks of data (where the start and
2196
   end may be in the same word).
2197
 
2198
   This also runs into endianism issues, since this is a read of a stream of
2199
   bytes, but the underlying JTAG call is a stream of uint32_t. We need to
2200
   handle this appropriately.
2201
 
2202
   @param[in]   addr   The starting address for the read
2203
   @param[out]  bdata  Pointer to a buffer for the results
2204
   @param[in]   len    Number of values to be read
2205
 
2206
   @return  The number of bytes read, or zero if there is an error */
2207
/*---------------------------------------------------------------------------*/
2208
 
2209
int
2210
or1k_jtag_read_mem (CORE_ADDR  addr,
2211
                    gdb_byte  *bdata,
2212
                    int        len)
2213
{
2214
  const int  bpw         = sizeof (uint32_t);
2215
  CORE_ADDR  offset_mask = (CORE_ADDR)(bpw - 1);
2216
  CORE_ADDR  word_mask   = ~offset_mask;
2217
 
2218
  int        start_off   =  addr        & offset_mask;
2219
  int        end_off     = (addr + len) & offset_mask;
2220
  int        start_bytes = (bpw - start_off) % bpw;
2221
  int        end_bytes   = end_off;
2222
  CORE_ADDR  aligned     = (CORE_ADDR)(addr + bpw - 1) & word_mask;
2223
  int        word_count;
2224
 
2225
  gdb_byte   buf[bpw];
2226
  int        w;
2227
 
2228
  /* Scan chain for memory access */
2229
  or1k_jtag_select_chain (OR1K_SC_WISHBONE);
2230
 
2231
  /* Get any odd start bytes */
2232
  if (0 != start_bytes)
2233
    {
2234
      ULONGEST  data;
2235
      int       i;
2236
 
2237
      start_bytes = start_bytes < len ? start_bytes : len;
2238
 
2239
      /* Read word (target endianism) and unpack allowing for the
2240
         endianism. Buffer will now have the character that was at the lowest
2241
         address at the lowest address in the buffer, independent of the
2242
         endianism. */
2243
      or1k_jtag_read_jtag_reg (aligned - bpw, &data);
2244
      store_unsigned_integer (buf, bpw, data);
2245
 
2246
      /* Copy to the main buffer */
2247
      for (i = 0; i < start_bytes; i++)
2248
        {
2249
          bdata[i] = buf[start_off + i];
2250
        }
2251
 
2252
      /* If this was the lot, stop here */
2253
      if( len == start_bytes )
2254
        {
2255
          return  len;
2256
        }
2257
    }
2258
 
2259
  /* Get the main block (if any). This will be a string of words (target
2260
     endianism), which we have to unpack to characters, so that the character
2261
     at the lowest address in the word ends up in the lowest address in the
2262
     buffer, independent of the endianism. */
2263
 
2264
  word_count  = (len - start_bytes - end_bytes) / bpw;
2265
 
2266
  if (word_count > 0)
2267
    {
2268
      or1k_jtag_read_jtag_block (aligned, (uint32_t *)(bdata + start_bytes),
2269
                                 word_count);
2270
 
2271
      for (w = 0; w < word_count; w++)
2272
        {
2273
          gdb_byte *offset = bdata + start_bytes + w * bpw;
2274
 
2275
          store_unsigned_integer (offset, bpw, *((uint32_t *)offset));
2276
        }
2277
    }
2278
 
2279
  /* Get any odd end bytes */
2280
  if (0 != end_bytes)
2281
    {
2282
      ULONGEST  data;
2283
      int       i;
2284
 
2285
      /* Read word and convert to endianism independent buffer */
2286
      or1k_jtag_read_jtag_reg (aligned + word_count * bpw, &data);
2287
      store_unsigned_integer (buf, bpw, data);
2288
 
2289
      /* Copy the relevant bytes to the main buffer */
2290
      for (i = 0; i < end_bytes; i++)
2291
        {
2292
          bdata[start_bytes + word_count * bpw + i] = buf[i];
2293
        }
2294
    }
2295
 
2296
  return  len;
2297
 
2298
}       /* or1k_jtag_read_mem() */
2299
 
2300
 
2301
/*----------------------------------------------------------------------------*/
2302
/*!Global function to write a block of memory via OpenRISC 1000 JTAG
2303
 
2304
   Use the underlying JTAG protocol calls to select the relevant scan chain
2305
   and write the memory. Always succeeds.
2306
 
2307
   The underlying protocol only writes whole words, so we must fix this if the
2308
   address and/or len are not on a word boundary with partial writes.
2309
 
2310
   This also runs into endianism issues, since this is a read of a stream of
2311
   bytes, but the underlying JTAG call is a stream of uint32_t. We need to
2312
   handle this appropriately.
2313
 
2314
   @param[in]   addr   The starting address for the read
2315
   @param[out]  bdata  Pointer to a buffer for the results
2316
   @param[in]   len    Number of values to be read
2317
 
2318
   @return  The number of bytes read, or zero if there is an error */
2319
/*---------------------------------------------------------------------------*/
2320
 
2321
int
2322
or1k_jtag_write_mem (CORE_ADDR       addr,
2323
                     const gdb_byte *bdata,
2324
                     int             len)
2325
{
2326
  const int  bpw         = sizeof (uint32_t);
2327
  CORE_ADDR  offset_mask = (CORE_ADDR)(bpw - 1);
2328
  CORE_ADDR  word_mask   = ~offset_mask;
2329
 
2330
  int        start_off   = addr         & offset_mask;
2331
  int        end_off     = (addr + len) & offset_mask;
2332
  int        start_bytes = (bpw - start_off) % bpw;
2333
  int        end_bytes   = end_off;
2334
  CORE_ADDR  aligned     = (CORE_ADDR)(addr + bpw - 1) & word_mask;
2335
  int        word_count;
2336
 
2337
  gdb_byte   buf[bpw];
2338
  int        w;
2339
  CORE_ADDR  c;
2340
 
2341
  /* Scan chain for memory access */
2342
  or1k_jtag_select_chain (OR1K_SC_WISHBONE);
2343
 
2344
  /* Write any odd start bytes */
2345
  if (0 != start_bytes)
2346
    {
2347
      ULONGEST  data;
2348
      int       i;
2349
 
2350
      start_bytes = start_bytes < len ? start_bytes : len;
2351
 
2352
      /* Read word (target endianism) and unpack allowing for
2353
         endianism. Buffer will now have the character that was at the lowest
2354
         address at the lowest address in the buffer, independent of the
2355
         endianism. */
2356
      or1k_jtag_read_jtag_reg (aligned - bpw, &data);
2357
      store_unsigned_integer (buf, bpw, data);
2358
 
2359
      /* Patch bytes, which is now endianism independent. */
2360
      for (i = 0; i < start_bytes; i++)
2361
        {
2362
          buf[start_off + i] = bdata[i];
2363
        }
2364
 
2365
      /* Convert back to a value represented with target endianism and write
2366
         back. */
2367
      data = extract_unsigned_integer (buf, bpw);
2368
      or1k_jtag_write_jtag_reg (aligned - bpw, data);
2369
 
2370
      /* If this was the lot, stop here */
2371
      if( len == start_bytes )
2372
        {
2373
          return  len;
2374
        }
2375
    }
2376
 
2377
  /* We need to patch the main data (if any) before sending, but this is
2378
     immutable, so we need a copy. Since the originating buffer could be
2379
     arbitrarily large we have to do this in chunks of up to
2380
     OR1K_MAX_JTAG_WRITE words. */
2381
 
2382
  word_count  = (len - start_bytes - end_bytes) / bpw;
2383
 
2384
  for (c  = 0; c * OR1K_MAX_JTAG_WRITE < word_count; c++)
2385
    {
2386
      uint32_t      new_bdata[OR1K_MAX_JTAG_WRITE * bpw];
2387
      unsigned int  c_offset = c * OR1K_MAX_JTAG_WRITE;
2388
      unsigned int  todo     = word_count - c_offset;
2389
 
2390
      todo = todo > OR1K_MAX_JTAG_WRITE ? OR1K_MAX_JTAG_WRITE : todo;
2391
 
2392
      for (w = 0; w < todo; w++)
2393
        {
2394
          const gdb_byte *byte_off = bdata + start_bytes + (c_offset + w) * bpw;
2395
          new_bdata[w] = extract_unsigned_integer (byte_off, bpw);
2396
        }
2397
 
2398
      /* Write the patched block */
2399
      or1k_jtag_write_jtag_block (aligned + c_offset * bpw,
2400
                                  (const uint32_t *)new_bdata,  todo);
2401
    }
2402
 
2403
  /* Get any odd end bytes */
2404
  if (0 != end_bytes)
2405
    {
2406
      ULONGEST  data;
2407
      int       i;
2408
 
2409
      /* Read word and convert to endianism independent buffer */
2410
      or1k_jtag_read_jtag_reg (aligned + word_count * bpw, &data);
2411
      store_unsigned_integer (buf, bpw, data);
2412
 
2413
      /* Patch bytes */
2414
      for (i = 0; i < end_bytes; i++)
2415
        {
2416
          buf[i] = bdata[start_bytes + word_count * bpw + i];
2417
        }
2418
 
2419
      /* Write back */
2420
      data = extract_unsigned_integer (buf, bpw);
2421
      or1k_jtag_write_jtag_reg (aligned + word_count * bpw, data);
2422
    }
2423
 
2424
  return  len;
2425
 
2426
}       /* or1k_jtag_write_mem() */
2427
 
2428
 
2429
/*----------------------------------------------------------------------------*/
2430
/*!Global function to stall the CPU
2431
 
2432
   Use the underlying JTAG protocol calls to select the relevant scan chain
2433
   and change the relevant registers. Always succeeds.
2434
 
2435
   @todo The old code always read back at least twice after setting the stall
2436
         bit. We've assumed that wasn't necessary (is any read back
2437
         necessary?).
2438
 
2439
         @todo The old code also disabled HW trace. Was that really necessary? */
2440
/*---------------------------------------------------------------------------*/
2441
 
2442
void
2443
or1k_jtag_stall()
2444
{
2445
  ULONGEST               data;
2446
 
2447
  /* Select the correct chain for controlling the processor */
2448
  or1k_jtag_select_chain (OR1K_SC_REGISTER);
2449
 
2450
  /* Read the JTAG register, set the stall bit and write it back. */
2451
  or1k_jtag_read_jtag_reg (OR1K_JTAG_RISCOP, &data);
2452
  data  |= OR1K_JTAG_RISCOP_STALL;
2453
  or1k_jtag_write_jtag_reg (OR1K_JTAG_RISCOP, data);
2454
 
2455
  /* Loop until we read back that the register is set. */
2456
  do
2457
    {
2458
      or1k_jtag_read_jtag_reg (OR1K_JTAG_RISCOP, &data);
2459
    }
2460
  while (OR1K_JTAG_RISCOP_STALL != (data & OR1K_JTAG_RISCOP_STALL));
2461
 
2462
}       /* or1k_jtag_stall() */
2463
 
2464
 
2465
/*----------------------------------------------------------------------------*/
2466
/*!Global function to unstall the CPU
2467
 
2468
   Use the underlying JTAG protocol calls to select the relevant scan chain
2469
   and change the relevant registers. Always succeeds.
2470
 
2471
   @note At one stage the code used to loop to check the stall flag had been
2472
         cleared. However that could allow time for the target to execute and
2473
         then stall again, so this function would hang. */
2474
/*---------------------------------------------------------------------------*/
2475
 
2476
void
2477
or1k_jtag_unstall()
2478
{
2479
  ULONGEST               data;
2480
 
2481
  /* Select the correct chain for controlling the processor */
2482
  or1k_jtag_select_chain (OR1K_SC_REGISTER);
2483
 
2484
  /* Read the JTAG register, clear the stall bit and write it back */
2485
  or1k_jtag_read_jtag_reg (OR1K_JTAG_RISCOP, &data);
2486
  data &= ~OR1K_JTAG_RISCOP_STALL;
2487
  or1k_jtag_write_jtag_reg (OR1K_JTAG_RISCOP, data);
2488
 
2489
}       /* or1k_jtag_unstall() */
2490
 
2491
 
2492
/*----------------------------------------------------------------------------*/
2493
/*!Global function to wait for the CPU to stall
2494
 
2495
   Use the underlying JTAG protocol calls to wait for the processor to
2496
   stall. Always succeeds.
2497
 
2498
   The wait time should be small if we are single stepping, but can be longer
2499
   otherwise. This is controlled by the argument.
2500
 
2501
   @param[in] fast  1 (true) if the wait should be quick. */
2502
/*---------------------------------------------------------------------------*/
2503
 
2504
void
2505
or1k_jtag_wait (int  fast)
2506
{
2507
  ULONGEST  data;
2508
 
2509
  /* Select the correct chain for controlling the processor */
2510
  or1k_jtag_select_chain (OR1K_SC_REGISTER);
2511
 
2512
  /* Read the JTAG register every 10us until the stall flag is set. */
2513
  do
2514
    {
2515
      if (fast)
2516
        {
2517
          usleep (OR1K_JTAG_FAST_WAIT);
2518
        }
2519
      else
2520
        {
2521
          usleep (OR1K_JTAG_SLOW_WAIT);
2522
        }
2523
 
2524
      or1k_jtag_read_jtag_reg (OR1K_JTAG_RISCOP, &data);
2525
    }
2526
  while (OR1K_JTAG_RISCOP_STALL != (data & OR1K_JTAG_RISCOP_STALL));
2527
 
2528
}       /* or1k_jtag_wait() */
2529
 
2530
 
2531
/* EOF */

powered by: WebSVN 2.1.0

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