-
Notifications
You must be signed in to change notification settings - Fork 0
/
litepcie_util.c
617 lines (530 loc) · 16.3 KB
/
litepcie_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* SPDX-License-Identifier: BSD-2-Clause
*
* LitePCIe util
*
* This file is part of LitePCIe.
*
* Copyright (C) 2018-2023 / EnjoyDigital / [email protected]
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <inttypes.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include "liblitepcie.h"
/* Parameters */
/*------------*/
#define DMA_CHECK_DATA /* Un-comment to disable data check */
#define DMA_RANDOM_DATA /* Un-comment to disable data random */
/* Variables */
/*-----------*/
static char litepcie_device[1024];
static int litepcie_device_num;
sig_atomic_t keep_running = 1;
void intHandler(int dummy) {
keep_running = 0;
}
/* Info */
/*------*/
static void info(void)
{
int fd;
int i;
unsigned char fpga_identifier[256];
fd = litepcie_open(litepcie_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Could not init driver\n");
exit(1);
}
printf("\e[1m[> FPGA/SoC Information:\e[0m\n");
printf("------------------------\n");
for (i = 0; i < 256; i ++)
fpga_identifier[i] = litepcie_readl(fd, CSR_IDENTIFIER_MEM_BASE + 4 * i);
printf("FPGA Identifier: %s.\n", fpga_identifier);
#ifdef CSR_DNA_BASE
printf("FPGA DNA: 0x%08x%08x\n",
litepcie_readl(fd, CSR_DNA_ID_ADDR + 4 * 0),
litepcie_readl(fd, CSR_DNA_ID_ADDR + 4 * 1)
);
#endif
#ifdef CSR_XADC_BASE
printf("FPGA Temperature: %0.1f °C\n",
(double)litepcie_readl(fd, CSR_XADC_TEMPERATURE_ADDR) * 503.975/4096 - 273.15);
printf("FPGA VCC-INT: %0.2f V\n",
(double)litepcie_readl(fd, CSR_XADC_VCCINT_ADDR) / 4096 * 3);
printf("FPGA VCC-AUX: %0.2f V\n",
(double)litepcie_readl(fd, CSR_XADC_VCCAUX_ADDR) / 4096 * 3);
printf("FPGA VCC-BRAM: %0.2f V\n",
(double)litepcie_readl(fd, CSR_XADC_VCCBRAM_ADDR) / 4096 * 3);
#endif
litepcie_close(fd);
}
/* Scratch */
/*---------*/
void scratch_test(void)
{
int fd;
printf("\e[1m[> Scratch register test:\e[0m\n");
printf("-------------------------\n");
/* Open LitePCIe device. */
fd = litepcie_open(litepcie_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Could not init driver %d\n", fd);
exit(1);
}
/* Write to scratch register. */
printf("Write 0x12345678 to Scratch register:\n");
litepcie_writel(fd, CSR_CTRL_SCRATCH_ADDR, 0x12345678);
printf("Read: 0x%08x\n", litepcie_readl(fd, CSR_CTRL_SCRATCH_ADDR));
/* Read from scratch register. */
printf("Write 0xdeadbeef to Scratch register:\n");
litepcie_writel(fd, CSR_CTRL_SCRATCH_ADDR, 0xdeadbeef);
printf("Read: 0x%08x\n", litepcie_readl(fd, CSR_CTRL_SCRATCH_ADDR));
/* Close LitePCIe device. */
close(fd);
}
/* SPI Flash */
/*-----------*/
#ifdef CSR_FLASH_BASE
static void flash_progress(void *opaque, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
fflush(stdout);
va_end(ap);
}
static void flash_program(uint32_t base, const uint8_t *buf1, int size1)
{
int fd;
uint32_t size;
uint8_t *buf;
int sector_size;
int errors;
/* Open LitePCIe device. */
fd = open(litepcie_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Could not init driver\n");
exit(1);
}
/* Get flash sector size and pad size to it. */
sector_size = litepcie_flash_get_erase_block_size(fd);
size = ((size1 + sector_size - 1) / sector_size) * sector_size;
/* Alloc buffer and copy data to it. */
buf = calloc(1, size);
if (!buf) {
fprintf(stderr, "%d: alloc failed\n", __LINE__);
exit(1);
}
memcpy(buf, buf1, size1);
/* Program flash. */
printf("Programming (%d bytes at 0x%08x)...\n", size, base);
errors = litepcie_flash_write(fd, buf, base, size, flash_progress, NULL);
if (errors) {
printf("Failed %d errors.\n", errors);
exit(1);
} else {
printf("Success.\n");
}
/* Free buffer and close LitePCIe device. */
free(buf);
close(fd);
}
static void flash_write(const char *filename, uint32_t offset)
{
uint8_t *data;
int size;
FILE * f;
/* Open data source file. */
f = fopen(filename, "rb");
if (!f) {
perror(filename);
exit(1);
}
/* Get size, alloc buffer and copy data to it. */
fseek(f, 0L, SEEK_END);
size = ftell(f);
fseek(f, 0L, SEEK_SET);
data = malloc(size);
if (!data) {
fprintf(stderr, "%d: malloc failed\n", __LINE__);
exit(1);
}
ssize_t ret = fread(data, size, 1, f);
fclose(f);
/* Program file to flash */
if (ret != 1)
perror(filename);
else
flash_program(offset, data, size);
/* Free buffer */
free(data);
}
static void flash_read(const char *filename, uint32_t size, uint32_t offset)
{
int fd;
FILE * f;
uint32_t base;
uint32_t sector_size;
uint8_t byte;
int i;
/* Open data destination file. */
f = fopen(filename, "wb");
if (!f) {
perror(filename);
exit(1);
}
/* Open LitePCIe device. */
fd = open(litepcie_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Could not init driver\n");
exit(1);
}
/* Get flash sector size. */
sector_size = litepcie_flash_get_erase_block_size(fd);
/* Read flash and write to destination file. */
base = offset;
for (i = 0; i < size; i++) {
if ((i % sector_size) == 0) {
printf("Reading 0x%08x\r", base + i);
fflush(stdout);
}
byte = litepcie_flash_read(fd, base + i);
fwrite(&byte, 1, 1, f);
}
/* Close destination file and LitePCIe device. */
fclose(f);
close(fd);
}
static void flash_reload(void)
{
int fd;
/* Open LitePCIe device. */
fd = open(litepcie_device, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Could not init driver\n");
exit(1);
}
/* Reload FPGA through ICAP.*/
litepcie_reload(fd);
/* Notice user to reboot the hardware.*/
printf("================================================================\n");
printf("= PLEASE REBOOT YOUR HARDWARE TO START WITH NEW FPGA GATEWARE =\n");
printf("================================================================\n");
/* Close LitePCIe device. */
close(fd);
}
#endif
/* DMA */
/*-----*/
static inline int64_t add_mod_int(int64_t a, int64_t b, int64_t m)
{
a += b;
if (a >= m)
a -= m;
return a;
}
static int get_next_pow2(int data_width)
{
int x = 1;
while (x < data_width)
x <<= 1;
return x;
}
#ifdef DMA_CHECK_DATA
static inline uint32_t seed_to_data(uint32_t seed)
{
#ifdef DMA_RANDOM_DATA
/* Return pseudo random data from seed. */
return seed * 69069 + 1;
#else
/* Return seed. */
return seed;
#endif
}
static uint32_t get_data_mask(int data_width)
{
int i;
uint32_t mask;
mask = 0;
for (i = 0; i < 32/get_next_pow2(data_width); i++) {
mask <<= get_next_pow2(data_width);
mask |= (1 << data_width) - 1;
}
return mask;
}
static void write_pn_data(uint32_t *buf, int count, uint32_t *pseed, int data_width)
{
int i;
uint32_t seed;
uint32_t mask = get_data_mask(data_width);
seed = *pseed;
for(i = 0; i < count; i++) {
buf[i] = (seed_to_data(seed) & mask);
seed = add_mod_int(seed, 1, DMA_BUFFER_SIZE / sizeof(uint32_t));
}
*pseed = seed;
}
static int check_pn_data(const uint32_t *buf, int count, uint32_t *pseed, int data_width)
{
int i, errors;
uint32_t seed;
uint32_t mask = get_data_mask(data_width);
errors = 0;
seed = *pseed;
for (i = 0; i < count; i++) {
if (buf[i] != (seed_to_data(seed) & mask)) {
errors ++;
}
seed = add_mod_int(seed, 1, DMA_BUFFER_SIZE / sizeof(uint32_t));
}
*pseed = seed;
return errors;
}
#endif
static void dma_test(uint8_t zero_copy, uint8_t external_loopback, int data_width, int auto_rx_delay)
{
static struct litepcie_dma_ctrl dma = {.use_reader = 1, .use_writer = 1, .dma_channel = 0};
dma.loopback = external_loopback ? 0 : 1;
if (data_width > 32 || data_width < 1) {
fprintf(stderr, "Invalid data width %d\n", data_width);
exit(1);
}
/* Statistics */
int i = 0;
int64_t reader_sw_count_last = 0;
int64_t last_time;
uint32_t errors = 0;
#ifdef DMA_CHECK_DATA
uint32_t seed_wr = 0;
uint32_t seed_rd = 0;
uint8_t run = (auto_rx_delay == 0);
#else
uint8_t run = 1;
#endif
signal(SIGINT, intHandler);
printf("\e[1m[> DMA loopback test:\e[0m\n");
printf("---------------------\n");
if (litepcie_dma_init(&dma, litepcie_device, zero_copy))
exit(1);
/* Test loop. */
last_time = get_time_ms();
for (;;) {
/* Exit loop on CTRL+C. */
if (!keep_running)
break;
/* Update DMA status. */
litepcie_dma_process(&dma);
// printf("wr av: %d rd av: %d wr swc: %d rd swc: %d wr hwc: %d rd hwc: %d\n",
// dma.buffers_available_write, dma.buffers_available_read,
// dma.writer_sw_count, dma.reader_sw_count,
// dma.hw_counts->hwWriterCountTotal, dma.hw_counts->hwReaderCountTotal);
#ifdef DMA_CHECK_DATA
char *buf_wr;
char *buf_rd;
/* DMA-TX Write. */
while (1) {
/* Get Write buffer. */
buf_wr = litepcie_dma_next_write_buffer(&dma);
/* Break when no buffer available for Write. */
if (!buf_wr)
break;
/* Write data to buffer. */
write_pn_data((uint32_t *) buf_wr, DMA_BUFFER_SIZE / sizeof(uint32_t), &seed_wr, data_width);
}
/* DMA-RX Read/Check */
while (1) {
/* Get Read buffer. */
buf_rd = litepcie_dma_next_read_buffer(&dma);
/* Break when no buffer available for Read. */
if (!buf_rd)
break;
/* Skip the first 128 DMA loops. */
if (dma.writer_hw_count < 128*DMA_BUFFER_COUNT)
break;
/* When running... */
if (run) {
/* Check data in Read buffer. */
errors += check_pn_data((uint32_t *) buf_rd, DMA_BUFFER_SIZE / sizeof(uint32_t), &seed_rd, data_width);
/* Clear Read buffer */
memset(buf_rd, 0, DMA_BUFFER_SIZE);
} else {
/* Find initial Delay/Seed (Useful when loopback is introducing delay). */
uint32_t errors_min = 0xffffffff;
for (int delay = 0; delay < DMA_BUFFER_SIZE / sizeof(uint32_t); delay++) {
seed_rd = delay;
errors = check_pn_data((uint32_t *) buf_rd, DMA_BUFFER_SIZE / sizeof(uint32_t), &seed_rd, data_width);
//printf("delay: %d / errors: %d\n", delay, errors);
if (errors < errors_min)
errors_min = errors;
if (errors < (DMA_BUFFER_SIZE / sizeof(uint32_t)) / 2) {
printf("RX_DELAY: %d (errors: %d)\n", delay, errors);
run = 1;
break;
}
}
if (!run) {
printf("Unable to find DMA RX_DELAY (min errors: %d/%ld), exiting.\n",
errors_min,
DMA_BUFFER_SIZE / sizeof(uint32_t));
goto end;
}
}
}
#endif
/* Statistics every 200ms. */
int64_t duration = get_time_ms() - last_time;
if (run & (duration > 200)) {
/* Print banner every 10 lines. */
if (i % 10 == 0)
printf("\e[1mDMA_SPEED(Gbps)\tTX_BUFFERS\tRX_BUFFERS\tDIFF\tERRORS\e[0m\n");
i++;
/* Print statistics. */
printf("%14.2f\t%10" PRIu64 "\t%10" PRIu64 "\t%4" PRIi64 "\t%6u\n",
(double)(dma.reader_sw_count - reader_sw_count_last) * DMA_BUFFER_SIZE * 8 * data_width / (get_next_pow2(data_width) * (double)duration * 1e6),
dma.reader_sw_count,
dma.writer_sw_count,
dma.reader_sw_count - dma.writer_sw_count,
errors);
/* Update errors/time/count. */
errors = 0;
last_time = get_time_ms();
reader_sw_count_last = dma.reader_sw_count;
}
}
/* Cleanup DMA. */
#ifdef DMA_CHECK_DATA
end:
#endif
litepcie_dma_cleanup(&dma);
}
/* Help */
/*------*/
static void help(void)
{
printf("LitePCIe utilities\n"
"usage: litepcie_util [options] cmd [args...]\n"
"\n"
"options:\n"
"-h Help.\n"
"-c device_num Select the device (default = 0).\n"
"-z Enable zero-copy DMA mode.\n"
"-e Use external loopback (default = internal).\n"
"-w data_width Width of data bus (default = 16).\n"
"-a Automatic DMA RX-Delay calibration.\n"
"\n"
"available commands:\n"
"info Get Board information.\n"
"\n"
"dma_test Test DMA.\n"
"scratch_test Test Scratch register.\n"
"\n"
#ifdef CSR_FLASH_BASE
"flash_write filename [offset] Write file contents to SPI Flash.\n"
"flash_read filename size [offset] Read from SPI Flash and write contents to file.\n"
"flash_reload Reload FPGA Image.\n"
#endif
);
exit(1);
}
/* Main */
/*------*/
int main(int argc, char **argv)
{
const char *cmd;
int c;
static uint8_t litepcie_device_zero_copy;
static uint8_t litepcie_device_external_loopback;
static int litepcie_data_width;
static int litepcie_auto_rx_delay;
litepcie_device_num = 0;
litepcie_data_width = 16;
litepcie_auto_rx_delay = 0;
litepcie_device_zero_copy = 0;
litepcie_device_external_loopback = 0;
/* Parameters. */
for (;;) {
c = getopt(argc, argv, "hc:w:zea");
if (c == -1)
break;
switch(c) {
case 'h':
help();
break;
case 'c':
litepcie_device_num = atoi(optarg);
break;
case 'w':
litepcie_data_width = atoi(optarg);
break;
case 'z':
litepcie_device_zero_copy = 1;
break;
case 'e':
litepcie_device_external_loopback = 1;
break;
case 'a':
litepcie_auto_rx_delay = 1;
break;
default:
exit(1);
}
}
/* Show help when too much args. */
if (optind >= argc)
help();
/* Select device. */
snprintf(litepcie_device, sizeof(litepcie_device), "/dev/litepcie%d", litepcie_device_num);
cmd = argv[optind++];
/* Info cmds. */
if (!strcmp(cmd, "info"))
info();
/* Scratch cmds. */
else if (!strcmp(cmd, "scratch_test"))
scratch_test();
/* SPI Flash cmds. */
#if CSR_FLASH_BASE
else if (!strcmp(cmd, "flash_write")) {
const char *filename;
uint32_t offset = 0;
if (optind + 1 > argc)
goto show_help;
filename = argv[optind++];
if (optind < argc)
offset = strtoul(argv[optind++], NULL, 0);
flash_write(filename, offset);
}
else if (!strcmp(cmd, "flash_read")) {
const char *filename;
uint32_t size = 0;
uint32_t offset = 0;
if (optind + 2 > argc)
goto show_help;
filename = argv[optind++];
size = strtoul(argv[optind++], NULL, 0);
if (optind < argc)
offset = strtoul(argv[optind++], NULL, 0);
flash_read(filename, size, offset);
}
else if (!strcmp(cmd, "flash_reload"))
flash_reload();
#endif
/* DMA cmds. */
else if (!strcmp(cmd, "dma_test"))
dma_test(
litepcie_device_zero_copy,
litepcie_device_external_loopback,
litepcie_data_width,
litepcie_auto_rx_delay);
/* Show help otherwise. */
else
goto show_help;
return 0;
show_help:
help();
return 0;
}