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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 104 markom
/* Remote debugging interface for JTAG debugging protocol.
2
   Copyright 1993-1995, 2000 Free Software Foundation, Inc.
3
   Contributed by Cygnus Support.  Written by Marko Mlinar
4
   <markom@opencores.org>
5
 
6
   This file is part of GDB.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 2 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program; if not, write to the Free Software
20
   Foundation, Inc., 59 Temple Place - Suite 330,
21
   Boston, MA 02111-1307, USA.  */
22
 
23
#include "defs.h"
24
#include "inferior.h"
25
#include "bfd.h"
26
#include "symfile.h"
27
#include "gdb_wait.h"
28
#include "gdbcmd.h"
29
#include "gdbcore.h"
30
#include "serial.h"
31
#include "target.h"
32
#include "remote-utils.h"
33
#include "gdb_string.h"
34
#include "tm.h"
35
 
36
#include <signal.h>
37
#include <sys/types.h>
38
#include <sys/stat.h>
39
#include <sys/ioctl.h>
40
#include <fcntl.h>
41
 
42
#define TCLK 0x01
43
#define TRST 0x02
44
#define JTAG_WAIT()
45
#define NUM_RETRIES 16
46
#define JTAG_RETRY_WAIT() usleep (100);
47
 
48
/* Scan chain for risc debug interface.  */
49
#define SC_RISC_DEBUG  0x1
50
#define SC_SIZE 4
51
 
52
/* Designates whether we are in SELECT_DR state, otherwise in
53
   RUN TEST/IDLE */
54
static int select_dr = 0;
55
 
56
/* Printer compatible device we have open.  */
57
static int lp;
58
 
59
/* Crc of current read or written data.  */
60
static int crc_r, crc_w = 0;
61
 
62
/* Generates new crc, sending in new bit input_bit */
63
static int
64
crc_calc (int crc, int input_bit)
65
{
66
  int c;
67
  int new_crc;
68
  int d;
69
 
70
  d = input_bit&1;
71
  c = crc;
72
 
73
  /* Move queue left.  */
74
  new_crc = crc << 1;
75
  /* Mask upper five bits.  */
76
  new_crc &= 0xF8;
77
 
78
  /* Set lower three bits */
79
  new_crc |= (d ^ ((c >> 7)&1));
80
  new_crc |= (d ^ ((c >> 0)&1) ^ ((c >> 7)&1)) << 1;
81
  new_crc |= (d ^ ((c >> 1)&1) ^ ((c >> 7)&1)) << 2;
82
  return new_crc;
83
}
84
 
85
/* Resets JTAG.
86
   Writes TRST=0
87
   and    TRST=1 */
88
 
89
static void
90
jp1_reset_JTAG ()
91
{
92
  unsigned char data;
93
  data = 0;
94
  write (lp, &data, sizeof (data));
95
  JTAG_WAIT();
96
  data = TRST;
97
  write (lp, &data, sizeof (data));
98
  JTAG_WAIT();
99
}
100
 
101
/* Writes TCLK=0, TRST=1, TMS=bit0, TDI=bit1
102
   and    TCLK=1, TRST=1, TMS=bit0, TDI=bit1 */
103
 
104
static void
105
jp1_write_JTAG (packet)
106
     unsigned char packet;
107
{
108
  unsigned char data;
109
  data = ((packet & 3) << 2) | TRST;
110
  write (lp, &data, sizeof (data));
111
  JTAG_WAIT();
112
  crc_w = crc_calc (crc_w, (packet >> 1)&1);
113
 
114
  /* rise clock */
115
  data |= TCLK;
116
  write (lp, &data, sizeof (data));
117
  JTAG_WAIT();
118
}
119
 
120
/* Reads TDO, using IOCTL.  */
121
 
122
static int
123
jp1_read_JTAG ()
124
{
125
  int data;
126
  ioctl(data, 0x60b, &data);
127
  data = ((data & 0x80) != 0);
128
  crc_r = crc_calc (crc_r, data);
129
  return data;
130
}
131
 
132
/* Writes bitstream.  MS bit first.  */
133
 
134
static void
135
jp1_write_stream (stream, len, set_last_bit)
136
     unsigned long stream;
137
     int len;
138
     int set_last_bit;
139
{
140
  int i;
141
  if (len <= 0) return;
142
  for (i = len - 1; i > 0; i--)
143
    jp1_write_JTAG (((stream >> i) & 1) << 1);
144
 
145
  if (set_last_bit)
146
    jp1_write_JTAG (((stream & 1) << 1) | 1);
147
  else
148
    jp1_write_JTAG ((stream & 1) << 1);
149
}
150
 
151
/* Gets bitstream.  MS bit first.  */
152
 
153
static unsigned long
154
jp1_read_stream (len, stream, set_last_bit)
155
     int len;
156
     unsigned long stream;
157
     int set_last_bit;
158
{
159
  int i;
160
  unsigned long data;
161
 
162
  if (len <= 0) return;
163
  data = 0;
164
  for (i = 0; i < len-1; i++)
165
    {
166
      jp1_write_JTAG (0 + ((stream & 1) << 1));
167
      stream >>= 1;
168
      data <<= 1;
169
      data |= jp1_read_JTAG ();
170
    }
171
 
172
  if (set_last_bit)
173
    jp1_write_JTAG (1 + (stream & 1) << 1);
174
  else
175
    jp1_write_JTAG (0 + (stream & 1) << 1);
176
  data <<= 1;
177
  data |= jp1_read_JTAG ();
178
  return data;
179
}
180
 
181
/* Goes into SELECT_IR state. Should be called before every control write.  */
182
 
183
static void
184
jp1_prepare_control ()
185
{
186
  if (!select_dr)
187
    jp1_write_JTAG (1); /* SELECT_DR SCAN */
188
  jp1_write_JTAG (1); /* SELECT_IR SCAN */
189
  select_dr = 0;
190
}
191
 
192
/* Sets register/memory regno to data.  */
193
 
194
void
195
jtag_write_reg (regno, data)
196
     int regno;
197
     unsigned int data;
198
{
199
  int crc_read, crc_write, crc_ok, retry;
200
 
201
  if (!select_dr)
202
    jp1_write_JTAG (1); /* SELECT_DR SCAN */
203
  select_dr = 1;
204
 
205
  for (retry = 0; retry < NUM_RETRIES; retry++)
206
    {
207
      jp1_write_JTAG (0); /* CAPTURE_DR */
208
      jp1_write_JTAG (0); /* SHIFT_DR */
209
      crc_w = 0;
210
      jp1_write_stream (regno, 32, 0); /* addr32 */
211
      jp1_write_JTAG (2); /* write (R/W=1) */
212
      jp1_write_stream (data, 32, 0);  /* data32 */
213
      crc_write = crc_w;
214
      crc_read = jp1_read_stream (crc_write, 8, 1); /* write CRC, EXIT1_DR */
215
      jp1_write_JTAG (1); /* UPDATE_DR */
216
      crc_ok = jp1_read_JTAG (); /* Did JTAG receive packet correctly? */
217
      jp1_write_JTAG (1); /* SELECT_DR */
218
      if ((crc_read == crc_write) && (crc_ok))
219
        return;
220
      JTAG_RETRY_WAIT();
221
    }
222
  err = ERR_CRC;
223
}
224
 
225
/* Reads register/memory from regno.  */
226
 
227
unsigned int
228
jtag_read_reg (regno)
229
     unsigned int regno;
230
{
231
  unsigned int data;
232
  int crc_read, crc_write, crc_actual_read,  retry, crc_ok;
233
 
234
  if (!select_dr)
235
    jp1_write_JTAG (1); /* SELECT_DR SCAN */
236
  select_dr = 1;
237
 
238
  for (retry = 0; retry < NUM_RETRIES; retry++)
239
    {
240
      jp1_write_JTAG (0); /* CAPTURE_DR */
241
      jp1_write_JTAG (0); /* SHIFT_DR */
242
      crc_w = 0;
243
      jp1_write_stream (regno, 32, 0); /* addr32 */
244
      jp1_write_JTAG (0); /* read (R/W=0) */
245
      crc_r = 0;
246
      data = jp1_read_stream (0, 32, 0);  /* data32=0 */
247
      crc_write = crc_w;
248
      crc_actual_read = crc_read;
249
      crc_read = jp1_read_stream (crc_write, 8, 1); /* Send my crc, EXIT1_DR */
250
 
251
      jp1_write_JTAG (1); /* UPDATE_DR */
252
      crc_ok = jp1_read_JTAG (); /* Did JTAG receive packet correctly? */
253
      jp1_write_JTAG (1); /* SELECT_DR */
254
      if ((crc_read == crc_actual_read) && (crc_ok))
255
        return;
256
      JTAG_RETRY_WAIT();
257
    }
258
  err = ERR_CRC;
259
}
260
 
261
/* Initialize a new connection to the or1k board, and make sure we are
262
   really connected.  */
263
 
264
void
265
jtag_init (args)
266
     char * args;
267
{
268
  char *ptype;
269
  char *port_name;
270
  char **argv;
271
 
272
  if (args == 0)
273
    error ( "To open a or1k remote debugging connection, you need to specify what parallel\n"
274
            "port device is attached to the target board (e.g., /dev/lp0).\n");
275
 
276
  /* Parse the serial port name.  */
277
  if ((argv = buildargv (args)) == NULL)
278
    nomem (0);
279
  port_name = strsave (argv[0]);
280
  make_cleanup_freeargv (argv);
281
 
282
  /* Open and initialize the parallel port.  */
283
  lp = open (port_name, O_WRONLY);
284
  if (lp < 0)
285
    error ("Cannot open device.");
286
 
287
  printf_unfiltered ("Remote or1k debugging using %s\n", port_name);
288
 
289
  jp1_reset_JTAG ();
290
  jp1_prepare_control ();
291
  jp1_write_stream (SC_RISC_DEBUG, SC_SIZE, 1);
292
  free (port_name);
293
}
294
 
295
void
296
jtag_done ()
297
{
298
  close (lp);
299
}

powered by: WebSVN 2.1.0

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