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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or_debug_proxy/] [src/] [or_debug_proxy.c] - Blame information for rev 662

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

Line No. Rev Author Line
1 39 julius
/*$$HEADER*/
2
/******************************************************************************/
3
/*                                                                            */
4
/*                    H E A D E R   I N F O R M A T I O N                     */
5
/*                                                                            */
6
/******************************************************************************/
7
 
8
// Project Name                   : OpenRISC Debug Proxy
9
// File Name                      : or_debug_proxy.c
10
// Prepared By                    : jb
11
// Project Start                  : 2008-10-01
12
 
13
/*$$COPYRIGHT NOTICE*/
14
/******************************************************************************/
15
/*                                                                            */
16
/*                      C O P Y R I G H T   N O T I C E                       */
17
/*                                                                            */
18
/******************************************************************************/
19
/*
20
  This library is free software; you can redistribute it and/or
21
  modify it under the terms of the GNU Lesser General Public
22
  License as published by the Free Software Foundation;
23
  version 2.1 of the License, a copy of which is available from
24
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
25
 
26
  This library is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29
  Lesser General Public License for more details.
30
 
31
  You should have received a copy of the GNU Lesser General Public
32
  License along with this library; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34
*/
35
 
36
/*$$DESCRIPTION*/
37
/******************************************************************************/
38
/*                                                                            */
39
/*                           D E S C R I P T I O N                            */
40
/*                                                                            */
41
/******************************************************************************/
42
//
43
// The entry point for the OpenRISC debug proxy console application. Is 
44
// compilable under both Linux/Unix systems and Cygwin Windows.
45
//
46
 
47
#include <assert.h>
48
#include <stdio.h>
49
#include <ctype.h>
50
#include <string.h>
51
#include <stdlib.h>
52
#include <unistd.h>
53
#include <stdarg.h>
54
#include <sys/stat.h>
55
#include <sys/types.h>
56
 
57
// Windows includes
58
#ifdef CYGWIN_COMPILE
59
#include <windows.h>
60
#include "win_FTCJTAG.h"
61
#include "win_FTCJTAG_ptrs.h"
62
#else
63
#include <signal.h>
64 497 julius
void catch_sigint(int sig_num); // First param must be "int"
65 39 julius
#endif
66
 
67
#include "gdb.h"
68
 
69
#ifdef USB_ENDPOINT_ENABLED
70
#include "usb_functions.h"
71
#endif
72
 
73
#include "or_debug_proxy.h"
74
 
75
// Defines of endpoint numbers
76
#define ENDPOINT_TARGET_NONE 0
77
#define ENDPOINT_TARGET_USB 1
78 376 julius
#define ENDPOINT_TARGET_OTHER 2
79 39 julius
 
80 497 julius
static int endpoint_target;     // Value to hold targeted endpoint
81 39 julius
 
82
#define GDB_PROTOCOL_JTAG  1
83
#define GDB_PROTOCOL_RSP   2
84
#define GDB_PROTOCOL_NONE  3
85
 
86 497 julius
int err;                        // Global error value
87 39 julius
 
88
/* Currently selected scan chain - just to prevent unnecessary transfers. */
89
int current_chain = -1;
90
 
91
/* The chain that should be currently selected. */
92
int dbg_chain = -1;
93
 
94 529 julius
/* By default, provide access to CPU */
95
int no_cpu = 0;
96
 
97 497 julius
int main(int argc, char *argv[])
98
{
99 39 julius
 
100 497 julius
        char *s;
101
        int gdb_protocol = GDB_PROTOCOL_NONE;
102
        endpoint_target = ENDPOINT_TARGET_NONE;
103
        int inp_arg = 1;
104 529 julius
 
105 39 julius
 
106 497 julius
        // Check we were compiled with at least one endpoint enabled
107 39 julius
#ifndef USB_ENDPOINT_ENABLED
108 497 julius
        printf
109
            ("No endpoints enabled.\nRecompile the proxy with at least one endpoint enabled\n");
110
        exit(0);
111 39 julius
#endif
112
 
113 497 julius
        // init our global error number
114
        err = DBG_ERR_OK;
115 39 julius
 
116 497 julius
        // Parse input options
117
        if (argc < 3) {
118
                print_usage();
119
                exit(1);
120
        }
121 39 julius
 
122 497 julius
        err = DBG_ERR_OK;
123
        srand(getpid());
124 39 julius
 
125 497 julius
        // Parse through the input, check what we've been given
126 39 julius
 
127 497 julius
        while (argv[inp_arg] != NULL) {
128
                if (strcmp(argv[inp_arg], "-r") == 0) {
129
                        gdb_protocol = GDB_PROTOCOL_RSP;
130
                        endpoint_target = ENDPOINT_TARGET_USB;
131
                } else if (strcmp(argv[inp_arg], "-o") == 0) {
132
                        gdb_protocol = GDB_PROTOCOL_RSP;
133
                        endpoint_target = ENDPOINT_TARGET_OTHER;
134
                } else if (strcmp(argv[inp_arg], "-k") == 0) {
135
                        kernel_debug = 1;
136 529 julius
                } else if (strcmp(argv[inp_arg], "-b") == 0) {
137
                        no_cpu = 1;
138 497 julius
                } else {
139
                        serverPort = strtol(argv[2], &s, 10);
140
                }
141
 
142
                inp_arg++;
143 39 julius
        }
144 497 julius
 
145
        if (endpoint_target == ENDPOINT_TARGET_NONE ||
146
            gdb_protocol == GDB_PROTOCOL_NONE ||
147
            serverPort > 65535 || *s != '\0') {
148
                print_usage();
149
                exit(1);
150 39 julius
        }
151 497 julius
#ifdef CYGWIN_COMPILE
152
        // Load the FTCJTAG DLL function pointers
153
        if (getFTDIJTAGFunctions() < 0) {
154
                exit(-1);
155 39 julius
        }
156
#endif
157
 
158
#ifndef CYGWIN_COMPILE
159 497 julius
        // Install a signal handler to exit gracefully
160
        // when we receive a sigint
161
        signal(SIGINT, catch_sigint);
162 39 julius
#endif
163 497 julius
 
164
        /* Initialise connection to our OR1k system */
165
        current_chain = -1;
166 39 julius
#ifdef USB_ENDPOINT_ENABLED
167 497 julius
        /* USB Endpoint */
168
        if (endpoint_target == ENDPOINT_TARGET_USB) {
169
                printf("\nConnecting to OR1k via USB debug cable\n\n");
170
                if ((err = usb_dbg_reset()))
171
                        goto JtagIfError;
172 529 julius
                if (!no_cpu)
173
                        dbg_test();     // Perform some tests
174 497 julius
        }
175 39 julius
#endif
176
 
177 497 julius
        /* We have a connection to the target system.  Now establish server
178
           connection. */
179
        if (gdb_protocol == GDB_PROTOCOL_RSP) { // Connect to RSP server
180
                /* RSP always starts stalled as though we have just reset the
181
                   processor. */
182
                // rsp_exception (EXCEPT_TRAP);
183
                handle_rsp();
184
        } else {
185
                fprintf(stderr, "Cannot start RSP Proxy server on port %d\n",
186
                        serverPort);
187
                exit(-1);
188
        }
189 39 julius
 
190
JtagIfError:
191 497 julius
        fprintf(stderr,
192
                "Connection via USB debug cable failed (err = %d).\nPlease ensure the device is attached and correctly installed\n\n",
193
                err);
194
        exit(-1);
195
        return 0;
196 39 julius
}
197
 
198
int dbg_reset()
199
{
200
#ifdef USB_ENDPOINT_ENABLED
201 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
202
                return usb_dbg_reset();
203 39 julius
#endif
204 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
205 39 julius
}
206
 
207 497 julius
void dbg_test()
208
{
209 39 julius
#ifdef USB_ENDPOINT_ENABLED
210 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
211
                usb_dbg_test();
212 39 julius
#endif
213
}
214
 
215
/* Set TAP instruction register */
216 497 julius
int dbg_set_tap_ir(uint32_t ir)
217
{
218 39 julius
#ifdef USB_ENDPOINT_ENABLED
219 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
220
                usb_set_tap_ir(ir);
221 39 julius
#endif
222 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
223 39 julius
}
224
 
225
/* Sets scan chain.  */
226 497 julius
int dbg_set_chain(uint32_t chain)
227
{
228 39 julius
#ifdef USB_ENDPOINT_ENABLED
229 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
230
                return usb_dbg_set_chain(chain);
231 39 julius
#endif
232 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
233
}
234 39 julius
 
235
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
236 497 julius
int dbg_command(uint32_t type, uint32_t adr, uint32_t len)
237
{
238
        // This is never called by any of the VPI functions, so only USB 
239
        // endpoint
240 39 julius
#ifdef USB_ENDPOINT_ENABLED
241 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
242
                return usb_dbg_command(type, adr, len);
243 39 julius
#endif
244 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
245 39 julius
}
246
 
247
/* writes a ctrl reg */
248 497 julius
int dbg_ctrl(uint32_t reset, uint32_t stall)
249
{
250 39 julius
#ifdef USB_ENDPOINT_ENABLED
251 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
252
                return usb_dbg_ctrl(reset, stall);
253 39 julius
#endif
254 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
255 39 julius
}
256
 
257
/* reads control register */
258 497 julius
int dbg_ctrl_read(uint32_t * reset, uint32_t * stall)
259
{
260 39 julius
#ifdef USB_ENDPOINT_ENABLED
261 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
262
                return usb_dbg_ctrl_read(reset, stall);
263 39 julius
#endif
264 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
265 39 julius
}
266
 
267
/* issues a burst read/write */
268 497 julius
int dbg_go(unsigned char *data, uint16_t len, uint32_t read)
269
{
270
        // Only USB endpouint32_t option here
271 39 julius
#ifdef USB_ENDPOINT_ENABLED
272 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
273
                return usb_dbg_go(data, len, read);
274 39 julius
#endif
275 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
276 39 julius
}
277
 
278 94 julius
/* read a byte from wishbone */
279 497 julius
int dbg_wb_read8(uint32_t adr, uint8_t * data)
280
{
281 94 julius
#ifdef USB_ENDPOINT_ENABLED
282 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
283
                return usb_dbg_wb_read8(adr, data);
284 94 julius
#endif
285 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
286 94 julius
}
287
 
288 39 julius
/* read a word from wishbone */
289 497 julius
int dbg_wb_read32(uint32_t adr, uint32_t * data)
290
{
291 39 julius
#ifdef USB_ENDPOINT_ENABLED
292 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
293
                return usb_dbg_wb_read32(adr, data);
294 39 julius
#endif
295 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
296 39 julius
}
297
 
298 46 julius
/* write a word to wishbone */
299 497 julius
int dbg_wb_write8(uint32_t adr, uint8_t data)
300
{
301 46 julius
#ifdef USB_ENDPOINT_ENABLED
302 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
303
                return usb_dbg_wb_write8(adr, data);
304 46 julius
#endif
305 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
306 46 julius
}
307 39 julius
 
308
/* write a word to wishbone */
309 497 julius
int dbg_wb_write32(uint32_t adr, uint32_t data)
310
{
311 39 julius
#ifdef USB_ENDPOINT_ENABLED
312 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
313
                return usb_dbg_wb_write32(adr, data);
314 39 julius
#endif
315 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
316 39 julius
}
317
 
318
/* read a block from wishbone */
319 497 julius
int dbg_wb_read_block32(uint32_t adr, uint32_t * data, uint32_t len)
320
{
321 39 julius
#ifdef USB_ENDPOINT_ENABLED
322 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
323
                return usb_dbg_wb_read_block32(adr, data, len);
324 39 julius
#endif
325 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
326 39 julius
}
327
 
328
/* write a block to wishbone */
329 497 julius
int dbg_wb_write_block32(uint32_t adr, uint32_t * data, uint32_t len)
330
{
331 39 julius
#ifdef USB_ENDPOINT_ENABLED
332 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
333
                return usb_dbg_wb_write_block32(adr, data, len);
334 39 julius
#endif
335 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
336 39 julius
}
337
 
338
/* read a register from cpu */
339 497 julius
int dbg_cpu0_read(uint32_t adr, uint32_t * data, uint32_t length)
340
{
341 39 julius
#ifdef USB_ENDPOINT_ENABLED
342 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
343
                return usb_dbg_cpu0_read(adr, data, length);
344 39 julius
#endif
345 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
346 39 julius
}
347
 
348
/* write a cpu register */
349 497 julius
int dbg_cpu0_write(uint32_t adr, uint32_t * data, uint32_t length)
350
{
351 39 julius
#ifdef USB_ENDPOINT_ENABLED
352 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
353
                return usb_dbg_cpu0_write(adr, data, length);
354 39 julius
#endif
355 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
356 39 julius
}
357
 
358
/* write a cpu module register */
359 497 julius
int dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data)
360
{
361 39 julius
#ifdef USB_ENDPOINT_ENABLED
362 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
363
                return usb_dbg_cpu0_write_ctrl(adr, data);
364 39 julius
#endif
365 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
366 39 julius
}
367
 
368
/* read a register from cpu module */
369 497 julius
int dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data)
370
{
371 39 julius
#ifdef USB_ENDPOINT_ENABLED
372 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
373
                return usb_dbg_cpu0_read_ctrl(adr, data);
374 39 julius
#endif
375 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
376 39 julius
}
377
 
378 497 julius
void test_sdram(void)
379
{
380 39 julius
        return;
381
}
382
 
383
// Close down gracefully when we receive any kill signals
384
void catch_sigint(int sig_num)
385
{
386 497 julius
        // Close down any potentially open sockets and USB handles
387
        if (server_fd)
388
                close(server_fd);
389
        gdb_close();
390 39 julius
#ifdef USB_ENDPOINT_ENABLED
391 497 julius
        usb_close_device_handle();
392 39 julius
#endif
393 497 julius
        printf
394
            ("\nInterrupt signal received. Closing down connections and exiting\n\n");
395
        exit(0);
396 39 julius
}
397
 
398
void print_usage()
399
{
400 497 julius
        printf("\n");
401
        printf("Usage: or_debug_proxy <SERVERTYPE> <PORT> <OPTIONS>\n");
402
        printf("OpenRISC GDB Proxy Server\n");
403
        printf("\n");
404
        printf("\tServer Type:\n");
405 39 julius
#ifdef USB_ENDPOINT_ENABLED
406 497 julius
        printf
407
            ("\t-r\tStart a server using RSP, connection to hadware target via\n\t\tUSB\n");
408
        printf
409
            ("\t-j\tStart a server using legacy OR remote JTAG protocol, to\n\t\thardware target via USB (DEPRECATED)\n");
410
        printf("\n");
411 39 julius
#endif
412 497 julius
        printf("\tPort:\n");
413
        printf("\t\tAny free port within the usable range of 0 - 65535\n");
414
        printf("\n");
415
        printf("\tOptions:\n");
416
        printf
417
            ("\t-k\tAccesses to 0xC0000000 map to 0x0. Useful for kernel debugging.\n");
418 529 julius
        printf
419
            ("\t-b\tBus access only - do not attempt to talk to the CPU\n");
420 497 julius
        printf("\n");
421
        printf("\tExample:\n");
422 39 julius
#ifdef USB_ENDPOINT_ENABLED
423 497 julius
        printf
424
            ("\t\tStart a GDB server on port 50001, using RSP, connecting to\n\t\thardware target via USB:\n\n");
425
        printf("\t\t./or_debug_proxy -r 50001\n");
426
        printf("\n");
427 39 julius
#endif
428 497 julius
        fflush(stdout);
429 39 julius
}

powered by: WebSVN 2.1.0

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