-
Notifications
You must be signed in to change notification settings - Fork 68
/
driver-x6500.c
723 lines (614 loc) · 18.6 KB
/
driver-x6500.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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/*
* Copyright 2012-2013 Luke Dashjr
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "config.h"
#ifdef WIN32
#include <winsock2.h>
#endif
#include <math.h>
#include <sys/time.h>
#include <libusb.h>
#include "compat.h"
#include "dynclock.h"
#include "jtag.h"
#include "logging.h"
#include "miner.h"
#include "fpgautils.h"
#include "ft232r.h"
extern pthread_mutex_t stats_lock;
#define X6500_USB_PRODUCT "X6500 FPGA Miner"
#define X6500_BITSTREAM_FILENAME "fpgaminer_x6500-overclocker-0402.bit"
// NOTE: X6500_BITSTREAM_USERID is bitflipped
#define X6500_BITSTREAM_USERID "\x40\x20\x24\x42"
#define X6500_MINIMUM_CLOCK 2
#define X6500_DEFAULT_CLOCK 200
#define X6500_MAXIMUM_CLOCK 250
struct device_api x6500_api;
#define fromlebytes(ca, j) (ca[j] | (((uint16_t)ca[j+1])<<8) | (((uint32_t)ca[j+2])<<16) | (((uint32_t)ca[j+3])<<24))
static
void int2bits(uint32_t n, uint8_t *b, uint8_t bits)
{
uint8_t i;
for (i = (bits + 7) / 8; i > 0; )
b[--i] = 0;
for (i = 0; i < bits; ++i) {
if (n & 1)
b[i/8] |= 0x80 >> (i % 8);
n >>= 1;
}
}
static
uint32_t bits2int(uint8_t *b, uint8_t bits)
{
uint32_t n, i;
n = 0;
for (i = 0; i < bits; ++i)
if (b[i/8] & (0x80 >> (i % 8)))
n |= 1<<i;
return n;
}
static
void checksum(uint8_t *b, uint8_t bits)
{
uint8_t i;
uint8_t checksum = 1;
for(i = 0; i < bits; ++i)
checksum ^= (b[i/8] & (0x80 >> (i % 8))) ? 1 : 0;
if (checksum)
b[i/8] |= 0x80 >> (i % 8);
}
static
void x6500_jtag_set(struct jtag_port *jp, uint8_t pinoffset)
{
jp->tck = pinoffset << 3;
jp->tms = pinoffset << 2;
jp->tdi = pinoffset << 1;
jp->tdo = pinoffset << 0;
jp->ignored = ~(jp->tdo | jp->tdi | jp->tms | jp->tck);
}
static uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr);
static
void x6500_set_register(struct jtag_port *jp, uint8_t addr, uint32_t nv)
{
uint8_t buf[38];
retry:
jtag_write(jp, JTAG_REG_IR, "\x40", 6);
int2bits(nv, &buf[0], 32);
int2bits(addr, &buf[4], 4);
buf[4] |= 8;
checksum(buf, 37);
jtag_write(jp, JTAG_REG_DR, buf, 38);
jtag_run(jp);
#ifdef DEBUG_X6500_SET_REGISTER
if (x6500_get_register(jp, addr) != nv)
#else
if (0)
#endif
{
applog(LOG_WARNING, "x6500_set_register failed %x=%08x", addr, nv);
goto retry;
}
}
static
uint32_t x6500_get_register(struct jtag_port *jp, uint8_t addr)
{
uint8_t buf[4] = {0};
jtag_write(jp, JTAG_REG_IR, "\x40", 6);
int2bits(addr, &buf[0], 4);
checksum(buf, 5);
jtag_write(jp, JTAG_REG_DR, buf, 6);
jtag_read (jp, JTAG_REG_DR, buf, 32);
jtag_reset(jp);
return bits2int(buf, 32);
}
static bool x6500_foundusb(libusb_device *dev, const char *product, const char *serial)
{
struct cgpu_info *x6500;
x6500 = calloc(1, sizeof(*x6500));
x6500->api = &x6500_api;
mutex_init(&x6500->device_mutex);
x6500->device_path = strdup(serial);
x6500->deven = DEV_ENABLED;
x6500->threads = 2;
x6500->name = strdup(product);
x6500->cutofftemp = 85;
x6500->cgpu_data = dev;
return add_cgpu(x6500);
}
static bool x6500_detect_one(const char *serial)
{
return ft232r_detect(X6500_USB_PRODUCT, serial, x6500_foundusb);
}
static int x6500_detect_auto()
{
return ft232r_detect(X6500_USB_PRODUCT, NULL, x6500_foundusb);
}
static void x6500_detect()
{
serial_detect_auto(&x6500_api, x6500_detect_one, x6500_detect_auto);
}
static bool x6500_prepare(struct thr_info *thr)
{
if (thr->device_thread)
return true;
struct cgpu_info *x6500 = thr->cgpu;
mutex_init(&x6500->device_mutex);
struct ft232r_device_handle *ftdi = ft232r_open(x6500->cgpu_data);
x6500->device_ft232r = NULL;
if (!ftdi)
return false;
if (!ft232r_set_bitmode(ftdi, 0xee, 4))
return false;
if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
return false;
x6500->device_ft232r = ftdi;
struct jtag_port_a *jtag_a;
unsigned char *pdone = calloc(1, sizeof(*jtag_a) + 1);
*pdone = 101;
jtag_a = (void*)(pdone + 1);
jtag_a->ftdi = ftdi;
x6500->cgpu_data = jtag_a;
return true;
}
struct x6500_fpga_data {
struct jtag_port jtag;
struct work prevwork;
struct timeval tv_workstart;
struct dclk_data dclk;
uint8_t freqMaxMaxM;
// Time the clock was last reduced due to temperature
time_t last_cutoff_reduced;
float temp;
};
#define bailout2(...) do { \
applog(__VA_ARGS__); \
return false; \
} while(0)
static bool
x6500_fpga_upload_bitstream(struct cgpu_info *x6500, struct jtag_port *jp1)
{
char buf[0x100];
unsigned long len, flen;
unsigned char *pdone = (unsigned char*)x6500->cgpu_data - 1;
struct ft232r_device_handle *ftdi = jp1->a->ftdi;
FILE *f = open_xilinx_bitstream(x6500, X6500_BITSTREAM_FILENAME, &len);
if (!f)
return false;
flen = len;
applog(LOG_WARNING, "%s %u: Programming %s...",
x6500->api->name, x6500->device_id, x6500->device_path);
x6500->status = LIFE_INIT2;
// "Magic" jtag_port configured to access both FPGAs concurrently
struct jtag_port jpt = {
.a = jp1->a,
};
struct jtag_port *jp = &jpt;
uint8_t i, j;
x6500_jtag_set(jp, 0x11);
// Need to reset here despite previous FPGA state, since we are programming all at once
jtag_reset(jp);
jtag_write(jp, JTAG_REG_IR, "\xd0", 6); // JPROGRAM
// Poll each FPGA status individually since they might not be ready at the same time
for (j = 0; j < 2; ++j) {
x6500_jtag_set(jp, j ? 0x10 : 1);
do {
i = 0xd0; // Re-set JPROGRAM while reading status
jtag_read(jp, JTAG_REG_IR, &i, 6);
} while (i & 8);
applog(LOG_DEBUG, "%s %u.%u: JPROGRAM ready",
x6500->api->name, x6500->device_id, j);
}
x6500_jtag_set(jp, 0x11);
jtag_write(jp, JTAG_REG_IR, "\xa0", 6); // CFG_IN
sleep(1);
if (fread(buf, 32, 1, f) != 1)
bailout2(LOG_ERR, "%s %u: File underrun programming %s (%lu bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
jtag_swrite(jp, JTAG_REG_DR, buf, 256);
len -= 32;
// Put ft232r chip in asynchronous bitbang mode so we don't need to read back tdo
// This takes upload time down from about an hour to about 3 minutes
if (!ft232r_set_bitmode(ftdi, 0xee, 1))
return false;
if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
return false;
jp->a->bufread = 0;
jp->a->async = true;
ssize_t buflen;
char nextstatus = 25;
while (len) {
buflen = len < 32 ? len : 32;
if (fread(buf, buflen, 1, f) != 1)
bailout2(LOG_ERR, "%s %u: File underrun programming %s (%lu bytes left)", x6500->api->name, x6500->device_id, x6500->device_path, len);
jtag_swrite_more(jp, buf, buflen * 8, len == (unsigned long)buflen);
*pdone = 100 - ((len * 100) / flen);
if (*pdone >= nextstatus)
{
nextstatus += 25;
applog(LOG_WARNING, "%s %u: Programming %s... %d%% complete...", x6500->api->name, x6500->device_id, x6500->device_path, *pdone);
}
len -= buflen;
}
// Switch back to synchronous bitbang mode
if (!ft232r_set_bitmode(ftdi, 0xee, 4))
return false;
if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
return false;
jp->a->bufread = 0;
jp->a->async = false;
jp->a->bufread = 0;
jtag_write(jp, JTAG_REG_IR, "\x30", 6); // JSTART
for (i=0; i<16; ++i)
jtag_run(jp);
i = 0xff; // BYPASS
jtag_read(jp, JTAG_REG_IR, &i, 6);
if (!(i & 4))
return false;
applog(LOG_WARNING, "%s %u: Done programming %s", x6500->api->name, x6500->device_id, x6500->device_path);
*pdone = 101;
return true;
}
static bool x6500_change_clock(struct thr_info *thr, int multiplier)
{
struct x6500_fpga_data *fpga = thr->cgpu_data;
struct jtag_port *jp = &fpga->jtag;
x6500_set_register(jp, 0xD, multiplier * 2);
ft232r_flush(jp->a->ftdi);
fpga->dclk.freqM = multiplier;
return true;
}
static bool x6500_dclk_change_clock(struct thr_info *thr, int multiplier)
{
struct cgpu_info *x6500 = thr->cgpu;
char fpgaid = thr->device_thread;
struct x6500_fpga_data *fpga = thr->cgpu_data;
uint8_t oldFreq = fpga->dclk.freqM;
mutex_lock(&x6500->device_mutex);
if (!x6500_change_clock(thr, multiplier)) {
mutex_unlock(&x6500->device_mutex);
return false;
}
mutex_unlock(&x6500->device_mutex);
char repr[0x10];
sprintf(repr, "%s %u.%u", x6500->api->name, x6500->device_id, fpgaid);
dclk_msg_freqchange(repr, oldFreq * 2, fpga->dclk.freqM * 2, NULL);
return true;
}
static bool x6500_fpga_init(struct thr_info *thr)
{
struct cgpu_info *x6500 = thr->cgpu;
struct ft232r_device_handle *ftdi = x6500->device_ft232r;
struct x6500_fpga_data *fpga;
struct jtag_port *jp;
int fpgaid = thr->device_thread;
uint8_t pinoffset = fpgaid ? 0x10 : 1;
unsigned char buf[4] = {0};
int i;
if (!ftdi)
return false;
fpga = calloc(1, sizeof(*fpga));
jp = &fpga->jtag;
jp->a = x6500->cgpu_data;
x6500_jtag_set(jp, pinoffset);
thr->cgpu_data = fpga;
x6500->status = LIFE_INIT2;
mutex_lock(&x6500->device_mutex);
if (!jtag_reset(jp)) {
mutex_unlock(&x6500->device_mutex);
applog(LOG_ERR, "%s %u: JTAG reset failed",
x6500->api->name, x6500->device_id);
return false;
}
i = jtag_detect(jp);
if (i != 1) {
mutex_unlock(&x6500->device_mutex);
applog(LOG_ERR, "%s %u: JTAG detect returned %d",
x6500->api->name, x6500->device_id, i);
return false;
}
if (!(1
&& jtag_write(jp, JTAG_REG_IR, "\x10", 6)
&& jtag_read (jp, JTAG_REG_DR, buf, 32)
&& jtag_reset(jp)
)) {
mutex_unlock(&x6500->device_mutex);
applog(LOG_ERR, "%s %u: JTAG error reading user code",
x6500->api->name, x6500->device_id);
return false;
}
if (memcmp(buf, X6500_BITSTREAM_USERID, 4)) {
applog(LOG_ERR, "%s %u.%u: FPGA not programmed",
x6500->api->name, x6500->device_id, fpgaid);
if (!x6500_fpga_upload_bitstream(x6500, jp))
return false;
} else if (opt_force_dev_init && x6500->status == LIFE_INIT2) {
applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed, but --force-dev-init is set",
x6500->api->name, x6500->device_id, fpgaid);
if (!x6500_fpga_upload_bitstream(x6500, jp))
return false;
} else
applog(LOG_DEBUG, "%s %u.%u: FPGA is already programmed :)",
x6500->api->name, x6500->device_id, fpgaid);
dclk_prepare(&fpga->dclk);
fpga->dclk.freqMinM = X6500_MINIMUM_CLOCK / 2;
x6500_change_clock(thr, X6500_DEFAULT_CLOCK / 2);
for (i = 0; 0xffffffff != x6500_get_register(jp, 0xE); ++i)
{}
mutex_unlock(&x6500->device_mutex);
if (i)
applog(LOG_WARNING, "%s %u.%u: Flushed %d nonces from buffer at init",
x6500->api->name, x6500->device_id, fpgaid, i);
fpga->dclk.minGoodSamples = 3;
fpga->freqMaxMaxM =
fpga->dclk.freqMaxM = X6500_MAXIMUM_CLOCK / 2;
fpga->dclk.freqMDefault = fpga->dclk.freqM;
applog(LOG_WARNING, "%s %u.%u: Frequency set to %u MHz (range: %u-%u)",
x6500->api->name, x6500->device_id, fpgaid,
fpga->dclk.freqM * 2,
X6500_MINIMUM_CLOCK,
fpga->dclk.freqMaxM * 2);
return true;
}
static
void x6500_get_temperature(struct cgpu_info *x6500)
{
struct x6500_fpga_data *fpga = x6500->thr[0]->cgpu_data;
struct jtag_port *jp = &fpga->jtag;
struct ft232r_device_handle *ftdi = jp->a->ftdi;
int i, code[2];
bool sio[2];
code[0] = 0;
code[1] = 0;
ft232r_flush(ftdi);
if (!(ft232r_set_cbus_bits(ftdi, false, true))) return;
if (!(ft232r_set_cbus_bits(ftdi, true, true))) return;
if (!(ft232r_set_cbus_bits(ftdi, false, true))) return;
if (!(ft232r_set_cbus_bits(ftdi, true, true))) return;
if (!(ft232r_set_cbus_bits(ftdi, false, false))) return;
for (i = 16; i--; ) {
if (ft232r_set_cbus_bits(ftdi, true, false)) {
if (!(ft232r_get_cbus_bits(ftdi, &sio[0], &sio[1]))) {
return;
}
} else {
return;
}
code[0] |= sio[0] << i;
code[1] |= sio[1] << i;
if (!ft232r_set_cbus_bits(ftdi, false, false)) {
return;
}
}
if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
return;
}
if (!(ft232r_set_cbus_bits(ftdi, true, true))) {
return;
}
if (!(ft232r_set_cbus_bits(ftdi, false, true))) {
return;
}
if (!ft232r_set_bitmode(ftdi, 0xee, 4)) {
return;
}
ft232r_purge_buffers(jp->a->ftdi, FTDI_PURGE_BOTH);
jp->a->bufread = 0;
for (i = 0; i < 2; ++i) {
struct thr_info *thr = x6500->thr[i];
fpga = thr->cgpu_data;
if (!fpga) continue;
if (code[i] == 0xffff || !code[i]) {
fpga->temp = 0;
continue;
}
if ((code[i] >> 15) & 1)
code[i] -= 0x10000;
fpga->temp = (float)(code[i] >> 2) * 0.03125f;
applog(LOG_DEBUG,"x6500_get_temperature: fpga[%d]->temp=%.1fC",i,fpga->temp);
int temperature = round(fpga->temp);
if (temperature > x6500->targettemp + opt_hysteresis) {
time_t now = time(NULL);
if (fpga->last_cutoff_reduced != now) {
fpga->last_cutoff_reduced = now;
int oldFreq = fpga->dclk.freqM;
if (x6500_change_clock(thr, oldFreq - 1))
applog(LOG_NOTICE, "%s %u.%u: Frequency dropped from %u to %u MHz (temp: %.1fC)",
x6500->api->name, x6500->device_id, i,
oldFreq * 2, fpga->dclk.freqM * 2,
fpga->temp
);
fpga->dclk.freqMaxM = fpga->dclk.freqM;
}
}
else
if (fpga->dclk.freqMaxM < fpga->freqMaxMaxM && temperature < x6500->targettemp) {
if (temperature < x6500->targettemp - opt_hysteresis) {
fpga->dclk.freqMaxM = fpga->freqMaxMaxM;
} else if (fpga->dclk.freqM == fpga->dclk.freqMaxM) {
++fpga->dclk.freqMaxM;
}
}
}
}
static bool x6500_get_stats(struct cgpu_info *x6500)
{
float hottest = 0;
if (x6500->deven != DEV_ENABLED) {
// Getting temperature more efficiently while enabled
// NOTE: Don't need to mess with mutex here, since the device is disabled
x6500_get_temperature(x6500);
}
for (int i = x6500->threads; i--; ) {
struct thr_info *thr = x6500->thr[i];
struct x6500_fpga_data *fpga = thr->cgpu_data;
if (!fpga)
continue;
float temp = fpga->temp;
if (temp > hottest)
hottest = temp;
}
x6500->temp = hottest;
return true;
}
static void
get_x6500_statline_before(char *buf, struct cgpu_info *x6500)
{
char info[18] = " | ";
struct x6500_fpga_data *fpga0 = x6500->thr[0]->cgpu_data;
struct x6500_fpga_data *fpga1 = x6500->thr[1]->cgpu_data;
unsigned char pdone = *((unsigned char*)x6500->cgpu_data - 1);
if (pdone != 101) {
sprintf(&info[1], "%3d%%", pdone);
info[5] = ' ';
strcat(buf, info);
return;
}
if (x6500->temp) {
sprintf(&info[1], "%.1fC/%.1fC", fpga0->temp, fpga1->temp);
info[strlen(info)] = ' ';
strcat(buf, info);
return;
}
strcat(buf, " | ");
}
static struct api_data*
get_x6500_api_extra_device_status(struct cgpu_info *x6500)
{
struct api_data *root = NULL;
static char *k[2] = {"FPGA0", "FPGA1"};
int i;
for (i = 0; i < 2; ++i) {
struct thr_info *thr = x6500->thr[i];
struct x6500_fpga_data *fpga = thr->cgpu_data;
json_t *o = json_object();
if (fpga->temp)
json_object_set_new(o, "Temperature", json_real(fpga->temp));
json_object_set_new(o, "Frequency", json_real((double)fpga->dclk.freqM * 2 * 1000000.));
json_object_set_new(o, "Cool Max Frequency", json_real((double)fpga->dclk.freqMaxM * 2 * 1000000.));
json_object_set_new(o, "Max Frequency", json_real((double)fpga->freqMaxMaxM * 2 * 1000000.));
root = api_add_json(root, k[i], o, false);
json_decref(o);
}
return root;
}
static
bool x6500_start_work(struct thr_info *thr, struct work *work)
{
struct cgpu_info *x6500 = thr->cgpu;
struct x6500_fpga_data *fpga = thr->cgpu_data;
struct jtag_port *jp = &fpga->jtag;
char fpgaid = thr->device_thread;
mutex_lock(&x6500->device_mutex);
for (int i = 1, j = 0; i < 9; ++i, j += 4)
x6500_set_register(jp, i, fromlebytes(work->midstate, j));
for (int i = 9, j = 64; i < 12; ++i, j += 4)
x6500_set_register(jp, i, fromlebytes(work->data, j));
ft232r_flush(jp->a->ftdi);
gettimeofday(&fpga->tv_workstart, NULL);
x6500_get_temperature(x6500);
mutex_unlock(&x6500->device_mutex);
if (opt_debug) {
char *xdata = bin2hex(work->data, 80);
applog(LOG_DEBUG, "%s %u.%u: Started work: %s",
x6500->api->name, x6500->device_id, fpgaid, xdata);
free(xdata);
}
return true;
}
static
int64_t calc_hashes(struct x6500_fpga_data *fpga, struct timeval *tv_now)
{
struct timeval tv_delta;
int64_t hashes;
timersub(tv_now, &fpga->tv_workstart, &tv_delta);
hashes = (((int64_t)tv_delta.tv_sec * 1000000) + tv_delta.tv_usec) * fpga->dclk.freqM * 2;
if (unlikely(hashes > 0x100000000))
hashes = 0x100000000;
return hashes;
}
static
int64_t x6500_process_results(struct thr_info *thr, struct work *work)
{
struct cgpu_info *x6500 = thr->cgpu;
struct x6500_fpga_data *fpga = thr->cgpu_data;
struct jtag_port *jtag = &fpga->jtag;
char fpgaid = thr->device_thread;
struct timeval tv_now;
int64_t hashes;
uint32_t nonce;
bool bad;
while (1) {
mutex_lock(&x6500->device_mutex);
gettimeofday(&tv_now, NULL);
nonce = x6500_get_register(jtag, 0xE);
mutex_unlock(&x6500->device_mutex);
if (nonce != 0xffffffff) {
bad = !test_nonce(work, nonce, false);
if (!bad) {
submit_nonce(thr, work, nonce);
applog(LOG_DEBUG, "%s %u.%u: Nonce for current work: %08lx",
x6500->api->name, x6500->device_id, fpgaid,
(unsigned long)nonce);
dclk_gotNonces(&fpga->dclk);
} else if (test_nonce(&fpga->prevwork, nonce, false)) {
submit_nonce(thr, &fpga->prevwork, nonce);
applog(LOG_DEBUG, "%s %u.%u: Nonce for PREVIOUS work: %08lx",
x6500->api->name, x6500->device_id, fpgaid,
(unsigned long)nonce);
} else {
applog(LOG_DEBUG, "%s %u.%u: Nonce with H not zero : %08lx",
x6500->api->name, x6500->device_id, fpgaid,
(unsigned long)nonce);
mutex_lock(&stats_lock);
++total_diff1;
++x6500->diff1;
++work->pool->diff1;
++hw_errors;
++x6500->hw_errors;
mutex_unlock(&stats_lock);
dclk_gotNonces(&fpga->dclk);
dclk_errorCount(&fpga->dclk, 1.);
}
// Keep reading nonce buffer until it's empty
// This is necessary to avoid getting hw errors from Freq B after we've moved on to Freq A
continue;
}
hashes = calc_hashes(fpga, &tv_now);
if (thr->work_restart || hashes >= 0xf0000000)
break;
usleep(10000);
hashes = calc_hashes(fpga, &tv_now);
if (thr->work_restart || hashes >= 0xf0000000)
break;
}
dclk_preUpdate(&fpga->dclk);
dclk_updateFreq(&fpga->dclk, x6500_dclk_change_clock, thr);
__copy_work(&fpga->prevwork, work);
return hashes;
}
static int64_t
x6500_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
{
if (!x6500_start_work(thr, work))
return -1;
int64_t hashes = x6500_process_results(thr, work);
if (hashes > 0)
work->blk.nonce += hashes;
return hashes;
}
struct device_api x6500_api = {
.dname = "x6500",
.name = "XBS",
.api_detect = x6500_detect,
.thread_prepare = x6500_prepare,
.thread_init = x6500_fpga_init,
.get_stats = x6500_get_stats,
.get_statline_before = get_x6500_statline_before,
.get_api_extra_device_status = get_x6500_api_extra_device_status,
.scanhash = x6500_scanhash,
// .thread_shutdown = x6500_fpga_shutdown,
};