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

Subversion Repositories eus100lx

[/] [eus100lx/] [trunk/] [tools/] [cris-bus/] [bus.c] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 freza
/* $Id: bus.c,v 1.1.1.1 2006-02-04 03:35:01 freza Exp $ */
2
 
3
#include <sys/mman.h>
4
 
5
#include <errno.h>
6
#include <fcntl.h>
7
#include <stdarg.h>
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <unistd.h>
12
 
13
#include <asm/page.h>
14
 
15
 
16
typedef int                     bus_space_tag_t;
17
typedef u_char                  *bus_space_handle_t;
18
 
19
#define bus_space_read_4(t, h, addr) \
20
        *((volatile u_int32_t *) ((h) + (addr)))
21
 
22
#define bus_space_write_4(t, h, addr, val) \
23
        (*((volatile u_int32_t *) ((h) + (addr))) = (u_int32_t)(val))
24
 
25
#define bus_space_read_2(t, h, addr) \
26
        *((volatile u_int16_t *) ((h) + (u_int16_t)(addr)))
27
 
28
#define bus_space_write_2(t, h, addr, val) \
29
        (*((volatile u_int16_t *) ((h) + (addr))) = (u_int16_t)(val))
30
 
31
#define bus_space_read_1(t, h, addr) \
32
        *((volatile u_int8_t *) ((h) + (addr)))
33
 
34
#define bus_space_write_1(t, h, addr, val) \
35
        (*((volatile u_int8_t *) ((h) + (addr))) = (u_int8_t)(val))
36
 
37
#define VERBOSE(arg)    if (verbose) warnx arg
38
 
39
static int              verbose = 0;
40
 
41
 
42
void
43
warnx(char *fmt, ...)
44
{
45
        va_list                 ap;
46
 
47
        fprintf(stderr, "bus: ");
48
        va_start(ap, fmt);
49
        vfprintf(stderr, fmt, ap);
50
        va_end(ap);
51
        fprintf(stderr, "\n");
52
}
53
 
54
void
55
errx(int ret, char *fmt, ...)
56
{
57
        va_list                 ap;
58
 
59
        fprintf(stderr, "bus: ");
60
        va_start(ap, fmt);
61
        vfprintf(stderr, fmt, ap);
62
        va_end(ap);
63
        fprintf(stderr, "\n");
64
 
65
        exit(ret);
66
}
67
 
68
void
69
err(int ret, char *fmt, ...)
70
{
71
        va_list                 ap;
72
 
73
        fprintf(stderr, "bus: ");
74
        va_start(ap, fmt);
75
        vfprintf(stderr, fmt, ap);
76
        va_end(ap);
77
        fprintf(stderr, ": %s\n", strerror(errno));
78
 
79
        exit(ret);
80
}
81
 
82
 
83
#include <stdlib.h>
84
#include <errno.h>
85
 
86
int
87
xstrtou(char *str, u_long *val)
88
{
89
        char                    *end;
90
        int                     base = 10;
91
 
92
        if (str[0] == '0')
93
                switch (str[1]) {
94
                case 'x':
95
                        base = 16;
96
                        str += 2;
97
                        break;
98
                case 'd':
99
                        base = 10;
100
                        str += 2;
101
                        break;
102
                case 'o':
103
                        base = 8;
104
                        str += 2;
105
                        break;
106
                case 'b':
107
                        base = 2;
108
                        str += 2;
109
                        break;
110
                default:
111
                        return EINVAL;
112
                }
113
 
114
        *val = (u_long) strtoul(str, &end, base);
115
        if (*end != '\0' || str[0] == '\0')
116
                return EINVAL;
117
 
118
        return 0;
119
}
120
 
121
int
122
bus_space_tag(bus_space_tag_t *t)
123
{
124
        *t = open("/dev/mem", O_RDWR | O_SYNC, 0);
125
        if (*t == -1)
126
                return (errno);
127
        return 0;
128
}
129
 
130
#ifndef PAGE_ALIGN
131
#define PAGE_ALIGN(val)         (((val) + PAGE_SIZE - 1) & PAGE_MASK)
132
#endif
133
 
134
int
135
bus_space_map(bus_space_tag_t t, u_int32_t base, u_int32_t size, int flags,
136
    bus_space_handle_t *h)
137
{
138
        off_t                   real;
139
 
140
        real = base & PAGE_MASK;
141
        size = PAGE_ALIGN(size);
142
 
143
        VERBOSE(("fd %d, mapping %dB at 0x%08x with offs 0x%08x",
144
            t, size, real, base % PAGE_SIZE));
145
 
146
        *h = mmap(0, size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, t, real);
147
        if (*h == MAP_FAILED)
148
                return (errno);
149
        *h += base % PAGE_SIZE;
150
 
151
        return (0);
152
}
153
 
154
/*
155
 * Main and stuff.
156
 */
157
#define ARGUMENTS       "a:b:c:f:hvw:"
158
 
159
void
160
usage()
161
{
162
        printf("Version: $Freza: bus.c,v 1.6 2005/11/22 12:57:25 jh Exp $\n");
163
        printf("Usage: bus [opts]\n");
164
        printf("-a x   Set bus address\n");
165
        printf("-b n   Set bus access width, one of 1, 2, 4 [4]\n");
166
        printf("-c n   How many values to process\n");
167
        printf("-f s   Copy data FROM hexa text file TO memory\n");
168
        printf("-h     Print this help\n");
169
        printf("-v     Verbose operation\n");
170
        printf("-w x   Write value x\n");
171
}
172
 
173
int
174
main(int argc, char *argv[])
175
{
176
        FILE                    *data;
177
        bus_space_handle_t      ioh;
178
        bus_space_tag_t         tag;
179
        u_int32_t               addr;
180
        u_int32_t               value;
181
        int                     do_write, has_addr;
182
        int                     count;
183
        int                     width;
184
        int                     i, c, ret;
185
 
186
        do_write        = 0;
187
        has_addr        = 0;
188
        count           = -1;
189
        width           = 4;
190
        data            = NULL;
191
 
192
        while((c = getopt(argc, argv, ARGUMENTS)) != -1) {
193
                switch (c) {
194
                case 'a':               /* Set address          */
195
                        if (xstrtou(optarg, (u_long *) &addr) != 0)
196
                                errx(1, "not a number: %s", optarg);
197
                        has_addr = 1;
198
                        break;
199
 
200
                case 'b':               /* Set access width     */
201
                        if (xstrtou(optarg, (u_long *) &width) != 0)
202
                                errx(1, "not a number: %s", optarg);
203
                        switch (width) {
204
                                case 1:
205
                                case 2:
206
                                case 4:
207
                                        break;
208
                                default:
209
                                        errx(1, "bad width: %d", width);
210
                        }
211
                        break;
212
 
213
                case 'c':               /* Set value count      */
214
                        if (xstrtou(optarg, (u_long *) &count) != 0)
215
                                errx(1, "not a number: %s", optarg);
216
                        break;
217
 
218
                case 'f':               /* File to read from/write to */
219
                        do_write = 1;
220
                        if ((data = fopen(optarg, "r")) == NULL)
221
                                err(1, "could not open %s", optarg);
222
 
223
                        break;
224
 
225
                case 'h':               /* Show help            */
226
                        usage();
227
                        return 0;
228
 
229
                case 'v':               /* Verbose operation    */
230
                        verbose = 1;
231
                        break;
232
 
233
                case 'w':               /* Value to write       */
234
                        if (xstrtou(optarg, (u_long *) &value) != 0)
235
                                errx(1, "not a number: %s", optarg);
236
                        do_write = 1;
237
                        break;
238
 
239
                default:
240
                        errx(1, "unknown argument -%c", optopt);
241
                }
242
        }
243
 
244
        argc -= optind;
245
        argv += optind;
246
 
247
        /* Validate user isn't insane. */
248
        if (argc > 0)
249
                errx(1, "stray arguments");
250
 
251
        if (! has_addr)
252
                errx(1, "address is mandatory");
253
 
254
        /* Grab bus space. */
255
        if ((ret = bus_space_tag(&tag)) != 0)
256
                err(1, "could not access physical memory, error %d", ret);
257
 
258
        if (count == -1) {
259
                if (data != NULL)
260
                        count = PAGE_SIZE / width;
261
                else
262
                        count = 1;
263
        }
264
 
265
        if ((ret = bus_space_map(tag, addr, count * width, 0, &ioh)) != 0)
266
                err(1, "could not map bus space, error %d", ret);
267
 
268
        if (do_write) {
269
                for (i = 0; i < count; i++) {
270
                        if (data != NULL) {
271
                                if (fscanf(data, "%x", &value) != 1) {
272
                                        if (feof(data))
273
                                                break;
274
                                        else
275
                                        if (ferror(data))
276
                                                err(1, "file read error");
277
                                        else
278
                                                err(1, "invalid input");
279
                                }
280
                        }
281
                        VERBOSE(("[%03d] 0x%08x <- 0x%08x", i,
282
                            addr + i * width, value));
283
 
284
                        switch (width) {
285
                        case 1:
286
                                bus_space_write_1(tag, ioh, i * width, value);
287
                                break;
288
                        case 2:
289
                                bus_space_write_2(tag, ioh, i * width, value);
290
                                break;
291
                        case 4:
292
                                bus_space_write_4(tag, ioh, i * width, value);
293
                                break;
294
                        }
295
                }
296
        } else {
297
                for (i = 0; i < count; i++) {
298
                        switch (width) {
299
                        case 1:
300
                                value = bus_space_read_1(tag, ioh, i * width);
301
                                printf("%02x\n", value);
302
                                break;
303
                        case 2:
304
                                value = bus_space_read_2(tag, ioh, i * width);
305
                                printf("%04x\n", value);
306
                                break;
307
                        case 4:
308
                                value = bus_space_read_4(tag, ioh, i * width);
309
                                printf("%08x\n", value);
310
                                break;
311
                        }
312
                }
313
        }
314
 
315
        return (0);
316
}

powered by: WebSVN 2.1.0

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