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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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