forked from ckolivas/cgminer
-
Notifications
You must be signed in to change notification settings - Fork 38
/
driver-bitfury.c
1656 lines (1419 loc) · 42.5 KB
/
driver-bitfury.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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013-2014 Con Kolivas
*
* 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"
#include "miner.h"
#include "driver-bitfury.h"
#include "sha2.h"
#include "mcp2210.h"
#include "libbitfury.h"
int opt_bxf_temp_target = BXF_TEMP_TARGET / 10;
int opt_nfu_bits = 50;
int opt_bxm_bits = 54;
int opt_bxf_bits = 54;
int opt_bxf_debug;
int opt_osm_led_mode = 4;
/* Wait longer 1/3 longer than it would take for a full nonce range */
#define BF1WAIT 1600
#define BF1MSGSIZE 7
#define BF1INFOSIZE 14
#define TWELVE_MHZ 12000000
//Low port pins
#define SK 1
#define DO 2
#define DI 4
#define CS 8
#define GPIO0 16
#define GPIO1 32
#define GPIO2 64
#define GPIO3 128
//GPIO pins
#define GPIOL0 0
#define GPIOL1 1
#define GPIOL2 2
#define GPIOL3 3
#define GPIOH 4
#define GPIOH1 5
#define GPIOH2 6
#define GPIOH3 7
#define GPIOH4 8
#define GPIOH5 9
#define GPIOH6 10
#define GPIOH7 11
#define DEFAULT_DIR (SK | DO | CS | GPIO0 | GPIO1 | GPIO2 | GPIO3) /* Setup default input or output state per FTDI for SPI */
#define DEFAULT_STATE (CS) /* CS idles high, CLK idles LOW for SPI0 */
//MPSSE commands from FTDI AN_108
#define INVALID_COMMAND 0xAB
#define ENABLE_ADAPTIVE_CLOCK 0x96
#define DISABLE_ADAPTIVE_CLOCK 0x97
#define ENABLE_3_PHASE_CLOCK 0x8C
#define DISABLE_3_PHASE_CLOCK 0x8D
#define TCK_X5 0x8A
#define TCK_D5 0x8B
#define CLOCK_N_CYCLES 0x8E
#define CLOCK_N8_CYCLES 0x8F
#define PULSE_CLOCK_IO_HIGH 0x94
#define PULSE_CLOCK_IO_LOW 0x95
#define CLOCK_N8_CYCLES_IO_HIGH 0x9C
#define CLOCK_N8_CYCLES_IO_LOW 0x9D
#define TRISTATE_IO 0x9E
#define TCK_DIVISOR 0x86
#define LOOPBACK_END 0x85
#define SET_OUT_ADBUS 0x80
#define SET_OUT_ACBUS 0x82
#define WRITE_BYTES_SPI0 0x11
#define READ_WRITE_BYTES_SPI0 0x31
static void bf1_empty_buffer(struct cgpu_info *bitfury)
{
char buf[512];
int amount;
do {
usb_read_once(bitfury, buf, 512, &amount, C_BF1_FLUSH);
} while (amount);
}
static bool bf1_open(struct cgpu_info *bitfury)
{
uint32_t buf[2];
int err;
bf1_empty_buffer(bitfury);
/* Magic sequence to reset device only really needed for windows but
* harmless on linux. */
buf[0] = 0x80250000;
buf[1] = 0x00000800;
err = usb_transfer(bitfury, 0, 9, 1, 0, C_ATMEL_RESET);
if (!err)
err = usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_ATMEL_OPEN);
if (!err) {
err = usb_transfer_data(bitfury, 0x21, 0x20, 0x0000, 0, buf,
BF1MSGSIZE, C_ATMEL_INIT);
}
if (err < 0) {
applog(LOG_INFO, "%s %d: Failed to open with error %s", bitfury->drv->name,
bitfury->device_id, libusb_error_name(err));
}
return (err == BF1MSGSIZE);
}
static void bf1_close(struct cgpu_info *bitfury)
{
bf1_empty_buffer(bitfury);
}
static void bf1_identify(struct cgpu_info *bitfury)
{
int amount;
usb_write(bitfury, "L", 1, &amount, C_BF1_IDENTIFY);
}
static void bitfury_identify(struct cgpu_info *bitfury)
{
struct bitfury_info *info = bitfury->device_data;
switch(info->ident) {
case IDENT_BF1:
bf1_identify(bitfury);
break;
case IDENT_BXF:
case IDENT_OSM:
default:
break;
}
}
static bool bf1_getinfo(struct cgpu_info *bitfury, struct bitfury_info *info)
{
int amount, err;
char buf[16];
err = usb_write(bitfury, "I", 1, &amount, C_BF1_REQINFO);
if (err) {
applog(LOG_INFO, "%s %d: Failed to write REQINFO",
bitfury->drv->name, bitfury->device_id);
return false;
}
err = usb_read(bitfury, buf, BF1INFOSIZE, &amount, C_BF1_GETINFO);
if (err) {
applog(LOG_INFO, "%s %d: Failed to read GETINFO",
bitfury->drv->name, bitfury->device_id);
return false;
}
if (amount != BF1INFOSIZE) {
applog(LOG_INFO, "%s %d: Getinfo received %d bytes instead of %d",
bitfury->drv->name, bitfury->device_id, amount, BF1INFOSIZE);
return false;
}
info->version = buf[1];
cg_memcpy(&info->product, buf + 2, 8);
cg_memcpy(&info->serial, buf + 10, 4);
bitfury->unique_id = bin2hex((unsigned char *)buf + 10, 4);
applog(LOG_INFO, "%s %d: Getinfo returned version %d, product %s serial %s", bitfury->drv->name,
bitfury->device_id, info->version, info->product, bitfury->unique_id);
bf1_empty_buffer(bitfury);
return true;
}
static bool bf1_reset(struct cgpu_info *bitfury)
{
int amount, err;
char buf[16];
err = usb_write(bitfury, "R", 1, &amount, C_BF1_REQRESET);
if (err) {
applog(LOG_INFO, "%s %d: Failed to write REQRESET",
bitfury->drv->name, bitfury->device_id);
return false;
}
err = usb_read_timeout(bitfury, buf, BF1MSGSIZE, &amount, BF1WAIT,
C_BF1_GETRESET);
if (err) {
applog(LOG_INFO, "%s %d: Failed to read GETRESET",
bitfury->drv->name, bitfury->device_id);
return false;
}
if (amount != BF1MSGSIZE) {
applog(LOG_INFO, "%s %d: Getreset received %d bytes instead of %d",
bitfury->drv->name, bitfury->device_id, amount, BF1MSGSIZE);
return false;
}
applog(LOG_DEBUG, "%s %d: Getreset returned %s", bitfury->drv->name,
bitfury->device_id, buf);
bf1_empty_buffer(bitfury);
return true;
}
static bool bxf_send_msg(struct cgpu_info *bitfury, char *buf, enum usb_cmds cmd)
{
int err, amount, len;
if (unlikely(bitfury->usbinfo.nodev))
return false;
if (opt_bxf_debug) {
char *strbuf = str_text(buf);
applog(LOG_ERR, "%s %d: >BXF [%s]", bitfury->drv->name, bitfury->device_id, strbuf);
free(strbuf);
}
len = strlen(buf);
applog(LOG_DEBUG, "%s %d: Sending %s", bitfury->drv->name, bitfury->device_id, buf);
err = usb_write(bitfury, buf, len, &amount, cmd);
if (err || amount != len) {
applog(LOG_WARNING, "%s %d: Error %d sending %s sent %d of %d", bitfury->drv->name,
bitfury->device_id, err, usb_cmdname(cmd), amount, len);
return false;
}
return true;
}
static bool bxf_send_debugmode(struct cgpu_info *bitfury)
{
char buf[16];
sprintf(buf, "debug-mode %d\n", opt_bxf_debug);
return bxf_send_msg(bitfury, buf, C_BXF_DEBUGMODE);
}
static bool bxf_send_ledmode(struct cgpu_info *bitfury)
{
char buf[16];
sprintf(buf, "led-mode %d\n", opt_osm_led_mode);
return bxf_send_msg(bitfury, buf, C_BXF_LEDMODE);
}
/* Returns the amount received only if we receive a full message, otherwise
* it returns the err value. */
static int bxf_recv_msg(struct cgpu_info *bitfury, char *buf)
{
int err, amount;
err = usb_read_nl(bitfury, buf, 512, &amount, C_BXF_READ);
if (amount)
applog(LOG_DEBUG, "%s %d: Received %s", bitfury->drv->name, bitfury->device_id, buf);
if (!err)
return amount;
return err;
}
/* Keep reading till the first timeout or error */
static void bxf_clear_buffer(struct cgpu_info *bitfury)
{
int err, retries = 0;
char buf[512];
do {
err = bxf_recv_msg(bitfury, buf);
usb_buffer_clear(bitfury);
if (err < 0)
break;
} while (retries++ < 10);
}
static bool bxf_send_flush(struct cgpu_info *bitfury)
{
char buf[8];
sprintf(buf, "flush\n");
return bxf_send_msg(bitfury, buf, C_BXF_FLUSH);
}
static bool bxf_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
{
int err, retries = 0;
char buf[512];
if (!bxf_send_flush(bitfury))
return false;
bxf_clear_buffer(bitfury);
sprintf(buf, "version\n");
if (!bxf_send_msg(bitfury, buf, C_BXF_VERSION))
return false;
do {
err = bxf_recv_msg(bitfury, buf);
if (err < 0 && err != LIBUSB_ERROR_TIMEOUT)
return false;
if (err > 0 && !strncmp(buf, "version", 7)) {
sscanf(&buf[8], "%d.%d rev %d chips %d", &info->ver_major,
&info->ver_minor, &info->hw_rev, &info->chips);
applog(LOG_INFO, "%s %d: Version %d.%d rev %d chips %d",
bitfury->drv->name, bitfury->device_id, info->ver_major,
info->ver_minor, info->hw_rev, info->chips);
break;
}
/* Keep parsing if the buffer is full without counting it as
* a retry. */
if (usb_buffer_size(bitfury))
continue;
} while (retries++ < 10);
if (!add_cgpu(bitfury))
quit(1, "Failed to add_cgpu in bxf_detect_one");
update_usb_stats(bitfury);
applog(LOG_INFO, "%s %d: Successfully initialised %s",
bitfury->drv->name, bitfury->device_id, bitfury->device_path);
/* Sanity check and recognise variations */
if (info->chips <= 2 || info->chips > 999)
info->chips = 2;
else if (info->chips <= 6 && info->ident == IDENT_BXF)
bitfury->drv->name = "HXF";
else if (info->chips > 6 && info->ident == IDENT_BXF)
bitfury->drv->name = "MXF";
info->filtered_hw = cgcalloc(sizeof(int), info->chips);
info->job = cgcalloc(sizeof(int), info->chips);
info->submits = cgcalloc(sizeof(int), info->chips);
info->total_nonces = 1;
info->temp_target = opt_bxf_temp_target * 10;
/* This unsets it to make sure it gets set on the first pass */
info->maxroll = -1;
return true;
}
static bool bf1_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
{
if (!bf1_open(bitfury))
goto out_close;
/* Send getinfo request */
if (!bf1_getinfo(bitfury, info))
goto out_close;
/* Send reset request */
if (!bf1_reset(bitfury))
goto out_close;
bf1_identify(bitfury);
bf1_empty_buffer(bitfury);
if (!add_cgpu(bitfury))
quit(1, "Failed to add_cgpu in bf1_detect_one");
update_usb_stats(bitfury);
applog(LOG_INFO, "%s %d: Successfully initialised %s",
bitfury->drv->name, bitfury->device_id, bitfury->device_path);
/* This does not artificially raise hashrate, it simply allows the
* hashrate to adapt quickly on starting. */
info->total_nonces = 1;
return true;
out_close:
bf1_close(bitfury);
return false;
}
static void nfu_close(struct cgpu_info *bitfury)
{
struct bitfury_info *info = bitfury->device_data;
struct mcp_settings *mcp = &info->mcp;
int i;
mcp2210_spi_cancel(bitfury);
/* Set all pins to input mode, ignoring return code */
for (i = 0; i < 9; i++) {
mcp->direction.pin[i] = MCP2210_GPIO_INPUT;
mcp->value.pin[i] = MCP2210_GPIO_PIN_LOW;
}
mcp2210_set_gpio_settings(bitfury, mcp);
}
static bool nfu_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
{
bool ret = true;
int i;
for (i = 0; i < info->chips; i++) {
spi_clear_buf(info);
spi_add_break(info);
spi_add_fasync(info, i);
spi_set_freq(info);
spi_send_conf(info);
spi_send_init(info);
spi_reset(bitfury, info);
ret = info->spi_txrx(bitfury, info);
if (!ret)
break;
}
return ret;
}
static bool nfu_set_spi_settings(struct cgpu_info *bitfury, struct bitfury_info *info)
{
struct mcp_settings *mcp = &info->mcp;
return mcp2210_set_spi_transfer_settings(bitfury, mcp->bitrate, mcp->icsv,
mcp->acsv, mcp->cstdd, mcp->ldbtcsd, mcp->sdbd, mcp->bpst, mcp->spimode);
}
static void nfu_alloc_arrays(struct bitfury_info *info)
{
info->payload = cgcalloc(sizeof(struct bitfury_payload), info->chips);
info->oldbuf = cgcalloc(sizeof(unsigned int) * 17, info->chips);
info->job_switched = cgcalloc(sizeof(bool), info->chips);
info->second_run = cgcalloc(sizeof(bool), info->chips);
info->work = cgcalloc(sizeof(struct work *), info->chips);
info->owork = cgcalloc(sizeof(struct work *), info->chips);
info->submits = cgcalloc(sizeof(int *), info->chips);
}
static bool nfu_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
{
struct mcp_settings *mcp = &info->mcp;
char buf[MCP2210_BUFFER_LENGTH];
unsigned int length;
bool ret = false;
int i, val;
/* Identify number of chips, and use it in device name if it can fit
* into 3 chars, otherwise use generic NFU name. */
val = sscanf(bitfury->usbdev->prod_string, "NanoFury NF%u ", &info->chips);
if (val < 1)
info->chips = 1;
else if (info->chips < 10) {
sprintf(info->product, "NF%u", info->chips);
bitfury->drv->name = info->product;
}
nfu_alloc_arrays(info);
info->spi_txrx = &mcp_spi_txrx;
mcp2210_get_gpio_settings(bitfury, mcp);
for (i = 0; i < 9; i++) {
/* Set all pins to GPIO mode */
mcp->designation.pin[i] = MCP2210_PIN_GPIO;
/* Set all pins to input mode */
mcp->direction.pin[i] = MCP2210_GPIO_INPUT;
mcp->value.pin[i] = MCP2210_GPIO_PIN_LOW;
}
/* Set LED and PWR pins to output and high */
mcp->direction.pin[NFU_PIN_LED] = mcp->direction.pin[NFU_PIN_PWR_EN] = MCP2210_GPIO_OUTPUT;
mcp->value.pin[NFU_PIN_LED] = mcp->value.pin[NFU_PIN_PWR_EN] = MCP2210_GPIO_PIN_HIGH;
mcp->direction.pin[NFU_PIN_PWR_EN0] = MCP2210_GPIO_OUTPUT;
mcp->value.pin[NFU_PIN_PWR_EN0] = MCP2210_GPIO_PIN_LOW;
mcp->direction.pin[4] = MCP2210_GPIO_OUTPUT;
mcp->designation.pin[4] = MCP2210_PIN_CS;
if (!mcp2210_set_gpio_settings(bitfury, mcp))
goto out;
if (opt_debug) {
struct gpio_pin gp;
mcp2210_get_gpio_pindirs(bitfury, &gp);
for (i = 0; i < 9; i++) {
applog(LOG_DEBUG, "%s %d: Pin dir %d %d", bitfury->drv->name,
bitfury->device_id, i, gp.pin[i]);
}
mcp2210_get_gpio_pinvals(bitfury, &gp);
for (i = 0; i < 9; i++) {
applog(LOG_DEBUG, "%s %d: Pin val %d %d", bitfury->drv->name,
bitfury->device_id, i, gp.pin[i]);
}
mcp2210_get_gpio_pindes(bitfury, &gp);
for (i = 0; i < 9; i++) {
applog(LOG_DEBUG, "%s %d: Pin des %d %d", bitfury->drv->name,
bitfury->device_id, i, gp.pin[i]);
}
}
/* Cancel any transfers in progress */
if (!mcp2210_spi_cancel(bitfury))
goto out;
if (!mcp2210_get_spi_transfer_settings(bitfury, &mcp->bitrate, &mcp->icsv,
&mcp->acsv, &mcp->cstdd, &mcp->ldbtcsd, &mcp->sdbd, &mcp->bpst, &mcp->spimode))
goto out;
mcp->bitrate = 200000; // default to 200kHz
mcp->icsv = 0xffff;
mcp->acsv = 0xffef;
mcp->cstdd = mcp->ldbtcsd = mcp->sdbd = mcp->spimode = 0;
mcp->bpst = 1;
if (!nfu_set_spi_settings(bitfury, info))
goto out;
buf[0] = 0;
length = 1;
if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
goto out;
/* after this command SCK_OVRRIDE should read the same as current SCK
* value (which for mode 0 should be 0) */
if (!mcp2210_get_gpio_pinval(bitfury, NFU_PIN_SCK_OVR, &val))
goto out;
if (val != MCP2210_GPIO_PIN_LOW)
goto out;
/* switch SCK to polarity (default SCK=1 in mode 2) */
mcp->spimode = 2;
if (!nfu_set_spi_settings(bitfury, info))
goto out;
buf[0] = 0;
length = 1;
if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
goto out;
/* after this command SCK_OVRRIDE should read the same as current SCK
* value (which for mode 2 should be 1) */
if (!mcp2210_get_gpio_pinval(bitfury, NFU_PIN_SCK_OVR, &val))
goto out;
if (val != MCP2210_GPIO_PIN_HIGH)
goto out;
/* switch SCK to polarity (default SCK=0 in mode 0) */
mcp->spimode = 0;
if (!nfu_set_spi_settings(bitfury, info))
goto out;
buf[0] = 0;
length = 1;
if (!mcp2210_spi_transfer(bitfury, mcp, buf, &length))
goto out;
if (!mcp2210_get_gpio_pinval(bitfury, NFU_PIN_SCK_OVR, &val))
goto out;
if (val != MCP2210_GPIO_PIN_LOW)
goto out;
info->osc6_bits = opt_nfu_bits;
if (!nfu_reinit(bitfury, info))
goto out;
ret = true;
if (!add_cgpu(bitfury))
quit(1, "Failed to add_cgpu in nfu_detect_one");
update_usb_stats(bitfury);
applog(LOG_INFO, "%s %d: Successfully initialised %s",
bitfury->drv->name, bitfury->device_id, bitfury->device_path);
spi_clear_buf(info);
info->total_nonces = info->chips;
out:
if (!ret)
nfu_close(bitfury);
return ret;
}
static bool bxm_purge_buffers(struct cgpu_info *bitfury)
{
int err;
err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_RESET_REQUEST, SIO_RESET_PURGE_RX, 1, C_BXM_PURGERX);
if (err)
return false;
err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_RESET_REQUEST, SIO_RESET_PURGE_TX, 1, C_BXM_PURGETX);
if (err)
return false;
return true;
}
/* Calculate required divisor for desired frequency see FTDI AN_108 page 19*/
static uint16_t calc_divisor(uint32_t system_clock, uint32_t freq)
{
uint16_t divisor = system_clock / freq;
divisor /= 2;
divisor -= 1;
return divisor;
}
static void bxm_shutdown(struct cgpu_info *bitfury, struct bitfury_info *info)
{
int chip_n;
for (chip_n = 0; chip_n < 2; chip_n++) {
spi_clear_buf(info);
spi_add_break(info);
spi_add_fasync(info, chip_n);
spi_config_reg(info, 4, 0);
info->spi_txrx(bitfury, info);
}
}
static void bxm_close(struct cgpu_info *bitfury, struct bitfury_info *info)
{
unsigned char bitmask = 0;
unsigned char mode = BITMODE_RESET;
unsigned short usb_val = bitmask;
bxm_shutdown(bitfury, info);
//Need to do BITMODE_RESET before usb close per FTDI
usb_val |= (mode << 8);
usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_BITMODE_REQUEST, usb_val, 1, C_BXM_SETBITMODE);
}
static bool bxm_open(struct cgpu_info *bitfury)
{
unsigned char mode = BITMODE_RESET;
unsigned char bitmask = 0;
unsigned short usb_val = bitmask;
uint32_t system_clock = TWELVE_MHZ;
uint32_t freq = 200000;
uint16_t divisor = calc_divisor(system_clock,freq);
int amount, err;
char buf[4];
/* Enable the transaction translator emulator for these devices
* otherwise we may write to them too quickly. */
bitfury->usbdev->tt = true;
err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_RESET_REQUEST, SIO_RESET_SIO, 1, C_BXM_SRESET);
if (err)
return false;
err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_LATENCY_TIMER_REQUEST, BXM_LATENCY_MS, 1, C_BXM_SETLATENCY);
if (err)
return false;
err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_EVENT_CHAR_REQUEST, 0x00, 1, C_BXM_SECR);
if (err)
return false;
//Do a BITMODE_RESET
usb_val |= (mode << 8);
err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_BITMODE_REQUEST, usb_val, 1, C_BXM_SETBITMODE);
if (err)
return false;
//Now set to MPSSE mode
bitmask = 0;
mode = BITMODE_MPSSE;
usb_val = bitmask;
usb_val |= (mode << 8);
err = usb_transfer(bitfury, FTDI_TYPE_OUT, SIO_SET_BITMODE_REQUEST, usb_val, 1, C_BXM_SETBITMODE);
if (err)
return false;
//Now set the clock divisor
//First send just the 0x8B command to set the system clock to 12MHz
memset(buf, 0, 4);
buf[0] = TCK_D5;
err = usb_write(bitfury, buf, 1, &amount, C_BXM_CLOCK);
if (err || amount != 1)
return false;
buf[0] = TCK_DIVISOR;
buf[1] = (divisor & 0xFF);
buf[2] = ((divisor >> 8) & 0xFF);
err = usb_write(bitfury, buf, 3, &amount, C_BXM_CLOCKDIV);
if (err || amount != 3)
return false;
//Disable internal loopback
buf[0] = LOOPBACK_END;
err = usb_write(bitfury, buf, 1, &amount, C_BXM_LOOP);
if (err || amount != 1)
return false;
//Now set direction and idle (initial) states for the pins
buf[0] = SET_OUT_ADBUS;
buf[1] = DEFAULT_STATE; //Bitmask for LOW_PORT
buf[2] = DEFAULT_DIR;
err = usb_write(bitfury, buf, 3, &amount, C_BXM_ADBUS);
if (err || amount != 3)
return false;
//Set the pin states for the HIGH_BITS port as all outputs, all low
buf[0] = SET_OUT_ACBUS;
buf[1] = 0x00; //Bitmask for HIGH_PORT
buf[2] = 0xFF;
err = usb_write(bitfury, buf, 3, &amount, C_BXM_ACBUS);
if (err || amount != 3)
return false;
return true;
}
static bool bxm_set_CS_low(struct cgpu_info *bitfury)
{
char buf[4] = { 0 };
int err, amount;
buf[0] = SET_OUT_ADBUS;
buf[1] &= ~DEFAULT_STATE; //Bitmask for LOW_PORT
buf[2] = DEFAULT_DIR;
err = usb_write(bitfury, buf, 3, &amount, C_BXM_CSLOW);
if (err || amount != 3)
return false;
return true;
}
static bool bxm_set_CS_high(struct cgpu_info *bitfury)
{
char buf[4] = { 0 };
int err, amount;
buf[0] = SET_OUT_ADBUS;
buf[1] = DEFAULT_STATE; //Bitmask for LOW_PORT
buf[2] = DEFAULT_DIR;
err = usb_write(bitfury, buf, 3, &amount, C_BXM_CSHIGH);
if (err || amount != 3)
return false;
return true;
}
static bool bxm_reset_bitfury(struct cgpu_info *bitfury)
{
char buf[20] = { 0 };
char rst_buf[8] = {0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00};
int err, amount;
//Set the FTDI CS pin HIGH. This will gate the clock to the Bitfury chips so we can send the reset sequence.
if (!bxm_set_CS_high(bitfury))
return false;
buf[0] = WRITE_BYTES_SPI0;
buf[1] = (uint8_t)16 - (uint8_t)1;
buf[2] = 0;
cg_memcpy(&buf[3], rst_buf, 8);
cg_memcpy(&buf[11], rst_buf, 8);
err = usb_write(bitfury, buf, 19, &amount, C_BXM_RESET);
if (err || amount != 19)
return false;
if (!bxm_set_CS_low(bitfury))
return false;
return true;
}
static bool bxm_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
{
bool ret;
int i;
for (i = 0; i < 2; i++) {
spi_clear_buf(info);
spi_add_break(info);
spi_add_fasync(info, i);
spi_set_freq(info);
spi_send_conf(info);
spi_send_init(info);
ret = info->spi_txrx(bitfury, info);
if (!ret)
break;
}
return ret;
}
static bool bxm_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
{
bool ret;
info->spi_txrx = &ftdi_spi_txrx;
ret = bxm_open(bitfury);
if (!ret)
goto out;
ret = bxm_purge_buffers(bitfury);
if (!ret)
goto out;
ret = bxm_reset_bitfury(bitfury);
if (!ret)
goto out;
ret = bxm_purge_buffers(bitfury);
if (!ret)
goto out;
/* Do a dummy read */
memset(info->spibuf, 0, 80);
info->spibufsz = 80;
ret = info->spi_txrx(bitfury, info);
if (!ret)
goto out;
info->osc6_bits = opt_bxm_bits;
/* Only have 2 chip devices for now */
info->chips = 2;
nfu_alloc_arrays(info);
ret = bxm_reinit(bitfury, info);
if (!ret)
goto out;
if (!add_cgpu(bitfury))
quit(1, "Failed to add_cgpu in bxm_detect_one");
update_usb_stats(bitfury);
applog(LOG_INFO, "%s %d: Successfully initialised %s",
bitfury->drv->name, bitfury->device_id, bitfury->device_path);
spi_clear_buf(info);
info->total_nonces = 1;
out:
if (!ret)
bxm_close(bitfury, info);
return ret;
}
static struct cgpu_info *bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
{
struct cgpu_info *bitfury;
struct bitfury_info *info;
enum sub_ident ident;
bool ret = false;
bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
if (!usb_init(bitfury, dev, found))
goto out;
applog(LOG_INFO, "%s %d: Found at %s", bitfury->drv->name,
bitfury->device_id, bitfury->device_path);
info = cgcalloc(sizeof(struct bitfury_info), 1);
bitfury->device_data = info;
info->ident = ident = usb_ident(bitfury);
switch (ident) {
case IDENT_BF1:
ret = bf1_detect_one(bitfury, info);
break;
case IDENT_BXF:
case IDENT_OSM:
ret = bxf_detect_one(bitfury, info);
break;
case IDENT_NFU:
ret = nfu_detect_one(bitfury, info);
break;
case IDENT_BXM:
ret = bxm_detect_one(bitfury, info);
break;
default:
applog(LOG_INFO, "%s %d: Unrecognised bitfury device",
bitfury->drv->name, bitfury->device_id);
break;
}
if (!ret) {
free(info);
usb_uninit(bitfury);
out:
bitfury = usb_free_cgpu(bitfury);
}
return bitfury;
}
static void bitfury_detect(bool __maybe_unused hotplug)
{
usb_detect(&bitfury_drv, bitfury_detect_one);
}
static void adjust_bxf_chips(struct cgpu_info *bitfury, struct bitfury_info *info, int chip)
{
int chips = chip + 1;
size_t old, new;
if (likely(chips <= info->chips))
return;
if (chips > 999)
return;
old = sizeof(int) * info->chips;
new = sizeof(int) * chips;
applog(LOG_INFO, "%s %d: Adjust chip size to %d", bitfury->drv->name, bitfury->device_id,
chips);
recalloc(info->filtered_hw, old, new);
recalloc(info->job, old, new);
recalloc(info->submits, old, new);
if (info->chips == 2 && chips <= 6 && info->ident == IDENT_BXF)
bitfury->drv->name = "HXF";
else if (info->chips <= 6 && chips > 6 && info->ident == IDENT_BXF)
bitfury->drv->name = "MXF";
info->chips = chips;
}
static void parse_bxf_submit(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
{
struct work *match_work, *tmp, *work = NULL;
struct thr_info *thr = info->thr;
uint32_t nonce, timestamp;
int workid, chip = -1;
if (!sscanf(&buf[7], "%x %x %x %d", &nonce, &workid, ×tamp, &chip)) {
applog(LOG_WARNING, "%s %d: Failed to parse submit response",
bitfury->drv->name, bitfury->device_id);
return;
}
adjust_bxf_chips(bitfury, info, chip);
if (unlikely(chip >= info->chips || chip < 0)) {
applog(LOG_INFO, "%s %d: Invalid submit chip number %d",
bitfury->drv->name, bitfury->device_id, chip);
} else
info->submits[chip]++;
applog(LOG_DEBUG, "%s %d: Parsed nonce %u workid %d timestamp %u",
bitfury->drv->name, bitfury->device_id, nonce, workid, timestamp);
rd_lock(&bitfury->qlock);
HASH_ITER(hh, bitfury->queued_work, match_work, tmp) {
if (match_work->subid == workid) {
work = copy_work(match_work);
break;
}
}
rd_unlock(&bitfury->qlock);
if (!work) {
/* Discard first results from any previous run */
if (unlikely(!info->valid))
return;
applog(LOG_INFO, "%s %d: No matching work", bitfury->drv->name, bitfury->device_id);
mutex_lock(&info->lock);
info->no_matching_work++;
mutex_unlock(&info->lock);
inc_hw_errors(thr);
return;
}
/* Set the device start time from when we first get valid results */
if (unlikely(!info->valid)) {
info->valid = true;
cgtime(&bitfury->dev_start_tv);
}
set_work_ntime(work, timestamp);
if (submit_nonce(thr, work, nonce)) {
mutex_lock(&info->lock);
info->nonces++;
mutex_unlock(&info->lock);
}
free_work(work);
}
static bool bxf_send_clock(struct cgpu_info *bitfury, struct bitfury_info *info,
uint8_t clockspeed)
{
char buf[64];
info->clocks = clockspeed;
sprintf(buf, "clock %d %d\n", clockspeed, clockspeed);
return bxf_send_msg(bitfury, buf, C_BXF_CLOCK);
}
static void parse_bxf_temp(struct cgpu_info *bitfury, struct bitfury_info *info, char *buf)
{
uint8_t clockspeed = info->clocks;
int decitemp;
if (!sscanf(&buf[5], "%d", &decitemp)) {
applog(LOG_INFO, "%s %d: Failed to parse temperature",
bitfury->drv->name, bitfury->device_id);
return;
}
mutex_lock(&info->lock);
bitfury->temp = (double)decitemp / 10;
if (decitemp > info->max_decitemp) {
info->max_decitemp = decitemp;
applog(LOG_DEBUG, "%s %d: New max decitemp %d", bitfury->drv->name,
bitfury->device_id, decitemp);
}
mutex_unlock(&info->lock);
if (decitemp > info->temp_target + BXF_TEMP_HYSTERESIS) {
if (info->clocks <= BXF_CLOCK_MIN)
goto out;
applog(LOG_WARNING, "%s %d: Hit overheat temperature of %d, throttling!",
bitfury->drv->name, bitfury->device_id, decitemp);
clockspeed = BXF_CLOCK_MIN;
goto out;
}
if (decitemp > info->temp_target) {
if (info->clocks <= BXF_CLOCK_MIN)
goto out;
if (decitemp < info->last_decitemp)
goto out;
applog(LOG_INFO, "%s %d: Temp %d over target and not falling, decreasing clock",
bitfury->drv->name, bitfury->device_id, decitemp);
clockspeed = info->clocks - 1;
goto out;
}
if (decitemp <= info->temp_target && decitemp >= info->temp_target - BXF_TEMP_HYSTERESIS) {
if (decitemp == info->last_decitemp)
goto out;
if (decitemp > info->last_decitemp) {
if (info->clocks <= BXF_CLOCK_MIN)
goto out;