This repository has been archived by the owner on May 27, 2020. It is now read-only.
forked from jgarzik/cpuminer
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
driver-btm-soc.c
12083 lines (10458 loc) · 431 KB
/
driver-btm-soc.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 2016-2017 Fazio Bai <[email protected]>
* Copyright 2016-2017 Clement Duan <[email protected]>
*
* 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 <assert.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/mman.h>
#include <math.h>
#ifndef WIN32
#include <sys/select.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#else
#include "compat.h"
#include <windows.h>
#include <io.h>
#endif
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <sys/sysinfo.h>
#include "elist.h"
#include "miner.h"
// #include "usbutils.h"
#include "util.h"
#include "driver-btm-soc.h"
#define MAX_CHAR_NUM 1024
extern void reCalculateAVG();
extern void setStartTimePoint();
bool someBoardUpVoltage=false;
bool isUseDefaultFreq=false;
bool doTestPatten=false;
bool startCheckNetworkJob=false;
unsigned char reset_iic_pic(unsigned char chain);
extern bool clement_doTestBoard(bool showlog);
bool clement_doTestBoardOnce(bool showlog);
int calculate_core_number(unsigned int actual_core_number);
#define hex_print(p) applog(LOG_DEBUG, "%s", p)
static char nibble[] =
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
#define BYTES_PER_LINE 0x10
static void hexdump(const uint8_t *p, unsigned int len)
{
unsigned int i, addr;
unsigned int wordlen = sizeof(unsigned int);
unsigned char v, line[BYTES_PER_LINE * 5];
for (addr = 0; addr < len; addr += BYTES_PER_LINE)
{
/* clear line */
for (i = 0; i < sizeof(line); i++)
{
if (i == wordlen * 2 + 52 ||
i == wordlen * 2 + 69)
{
line[i] = '|';
continue;
}
if (i == wordlen * 2 + 70)
{
line[i] = '\0';
continue;
}
line[i] = ' ';
}
/* print address */
for (i = 0; i < wordlen * 2; i++)
{
v = addr >> ((wordlen * 2 - i - 1) * 4);
line[i] = nibble[v & 0xf];
}
/* dump content */
for (i = 0; i < BYTES_PER_LINE; i++)
{
int pos = (wordlen * 2) + 3 + (i / 8);
if (addr + i >= len)
break;
v = p[addr + i];
line[pos + (i * 3) + 0] = nibble[v >> 4];
line[pos + (i * 3) + 1] = nibble[v & 0xf];
/* character printable? */
line[(wordlen * 2) + 53 + i] =
(v >= ' ' && v <= '~') ? v : '.';
}
hex_print(line);
}
}
/**
* \brief SHA-256 context structure
*/
typedef struct
{
uint32_t total[2]; /*!< number of bytes processed */
uint32_t state[8]; /*!< intermediate digest state */
unsigned char buffer[64]; /*!< data block being processed */
unsigned char ipad[64]; /*!< HMAC: inner padding */
unsigned char opad[64]; /*!< HMAC: outer padding */
}
sha2_context;
/*
* 32-bit integer manipulation macros (big endian)
*/
#ifndef GET_ULONG_BE
#define GET_ULONG_BE(n,b,i) \
{ \
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
| ( (uint32_t) (b)[(i) + 3] ); \
}
#endif
#ifndef PUT_ULONG_BE
#define PUT_ULONG_BE(n,b,i) \
{ \
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 3] = (unsigned char) ( (n) ); \
}
#endif
/*
* SHA-256 context setup
*/
void sha2_starts( sha2_context *ctx )
{
ctx->total[0] = 0;
ctx->total[1] = 0;
ctx->state[0] = 0x6A09E667;
ctx->state[1] = 0xBB67AE85;
ctx->state[2] = 0x3C6EF372;
ctx->state[3] = 0xA54FF53A;
ctx->state[4] = 0x510E527F;
ctx->state[5] = 0x9B05688C;
ctx->state[6] = 0x1F83D9AB;
ctx->state[7] = 0x5BE0CD19;
}
void sha2_process( sha2_context *ctx, const unsigned char data[64] )
{
uint32_t temp1, temp2, W[64];
uint32_t A, B, C, D, E, F, G, H;
GET_ULONG_BE( W[ 0], data, 0 );
GET_ULONG_BE( W[ 1], data, 4 );
GET_ULONG_BE( W[ 2], data, 8 );
GET_ULONG_BE( W[ 3], data, 12 );
GET_ULONG_BE( W[ 4], data, 16 );
GET_ULONG_BE( W[ 5], data, 20 );
GET_ULONG_BE( W[ 6], data, 24 );
GET_ULONG_BE( W[ 7], data, 28 );
GET_ULONG_BE( W[ 8], data, 32 );
GET_ULONG_BE( W[ 9], data, 36 );
GET_ULONG_BE( W[10], data, 40 );
GET_ULONG_BE( W[11], data, 44 );
GET_ULONG_BE( W[12], data, 48 );
GET_ULONG_BE( W[13], data, 52 );
GET_ULONG_BE( W[14], data, 56 );
GET_ULONG_BE( W[15], data, 60 );
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
#define F0(x,y,z) ((x & y) | (z & (x | y)))
#define F1(x,y,z) (z ^ (x & (y ^ z)))
#define R(t) \
( \
W[t] = S1(W[t - 2]) + W[t - 7] + \
S0(W[t - 15]) + W[t - 16] \
)
#define P(a,b,c,d,e,f,g,h,x,K) \
{ \
temp1 = h + S3(e) + F1(e,f,g) + K + x; \
temp2 = S2(a) + F0(a,b,c); \
d += temp1; h = temp1 + temp2; \
}
A = ctx->state[0];
B = ctx->state[1];
C = ctx->state[2];
D = ctx->state[3];
E = ctx->state[4];
F = ctx->state[5];
G = ctx->state[6];
H = ctx->state[7];
P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
ctx->state[0] += A;
ctx->state[1] += B;
ctx->state[2] += C;
ctx->state[3] += D;
ctx->state[4] += E;
ctx->state[5] += F;
ctx->state[6] += G;
ctx->state[7] += H;
}
/*
* SHA-256 process buffer
*/
void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
{
int fill;
uint32_t left;
if( ilen <= 0 )
return;
left = ctx->total[0] & 0x3F;
fill = 64 - left;
ctx->total[0] += ilen;
ctx->total[0] &= 0xFFFFFFFF;
if( ctx->total[0] < (uint32_t) ilen )
ctx->total[1]++;
if( left && ilen >= fill )
{
memcpy( (void *) (ctx->buffer + left),
(void *) input, fill );
sha2_process( ctx, ctx->buffer );
input += fill;
ilen -= fill;
left = 0;
}
while( ilen >= 64 )
{
sha2_process( ctx, input );
input += 64;
ilen -= 64;
}
if( ilen > 0 )
{
memcpy((void *) (ctx->buffer + left),
(void *) input, ilen );
}
/*
printk("ctx sha2_update:");
dump_hex((uint8_t*)ctx,sizeof(*ctx));
*/
}
static const unsigned char sha2_padding[64] =
{
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
/*
* SHA-256 final digest
*/
void sha2_finish( sha2_context *ctx, unsigned char output[32] )
{
uint32_t last, padn;
uint32_t high, low;
unsigned char msglen[8];
high = ( ctx->total[0] >> 29 )
| ( ctx->total[1] << 3 );
low = ( ctx->total[0] << 3 );
PUT_ULONG_BE( high, msglen, 0 );
PUT_ULONG_BE( low, msglen, 4 );
last = ctx->total[0] & 0x3F;
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
sha2_update( ctx, (unsigned char *) sha2_padding, padn );
sha2_update( ctx, msglen, 8 );
PUT_ULONG_BE( ctx->state[0], output, 0 );
PUT_ULONG_BE( ctx->state[1], output, 4 );
PUT_ULONG_BE( ctx->state[2], output, 8 );
PUT_ULONG_BE( ctx->state[3], output, 12 );
PUT_ULONG_BE( ctx->state[4], output, 16 );
PUT_ULONG_BE( ctx->state[5], output, 20 );
PUT_ULONG_BE( ctx->state[6], output, 24 );
PUT_ULONG_BE( ctx->state[7], output, 28 );
}
/*
* output = SHA-256( input buffer )
*/
void sha2( const unsigned char *input, int ilen,
unsigned char output[32] )
{
sha2_context ctx;
sha2_starts( &ctx );
sha2_update( &ctx, input, ilen );
sha2_finish( &ctx, output );
memset(&ctx, 0, sizeof(sha2_context));
}
#ifdef R4
int MIN_PWM_PERCENT;
int MID_PWM_PERCENT;
int MAX_PWM_PERCENT;
int MAX_TEMP;
int MAX_FAN_TEMP;
int MID_FAN_TEMP;
int MIN_FAN_TEMP;
int MAX_PCB_TEMP;
int MAX_FAN_PCB_TEMP;
#endif
bool is218_Temp=false;
//interface between bmminer and axi driver
static struct init_config config_parameter;
//global various
int fd; // axi fpga
int fd_fpga_mem; // fpga memory
int fpga_version;
int pcb_version;
unsigned int *axi_fpga_addr = NULL; // axi address
unsigned int *fpga_mem_addr = NULL; // fpga memory address
unsigned int *nonce2_jobid_address = NULL; // the value should be filled in NONCE2_AND_JOBID_STORE_ADDRESS
unsigned int *job_start_address_1 = NULL; // the value should be filled in JOB_START_ADDRESS
unsigned int *job_start_address_2 = NULL; // the value should be filled in JOB_START_ADDRESS
struct thr_info *read_nonce_reg_id; // thread id for read nonce and register
struct thr_info *check_system_work_id; // thread id for check system
struct thr_info *read_temp_id;
struct thr_info *pic_heart_beat;
struct thr_info *change_voltage_to_old;
extern void writeLogFile(char *logstr);
bool gBegin_get_nonce = false;
struct timeval tv_send_job = {0, 0};
struct timeval tv_send = {0, 0};
pthread_mutex_t reg_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t iic_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t fpga_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t opencore_readtemp_mutex = PTHREAD_MUTEX_INITIALIZER;
uint64_t h = 0;
int opt_multi_version = 1;
uint32_t given_id = 2;
uint32_t c_coinbase_padding = 0;
uint32_t c_merkles_num = 0;
uint32_t l_coinbase_padding = 0;
uint32_t l_merkles_num = 0;
int last_temperature = 0, temp_highest = 0;
bool opt_bitmain_fan_ctrl = false;
int opt_bitmain_fan_pwm = 0;
int opt_bitmain_soc_freq = 600;
int opt_bitmain_soc_voltage = 176;
int ADD_FREQ = 0;
int ADD_FREQ1 = 0;
uint8_t de_voltage = 176;
#define ERROR_OVER_MAXTEMP 1 // temp is too high
#define ERROR_FAN_LOST 2 // fan num is not right, some fan lost
#define ERROR_FAN_SPEED 3 // fan speed error
#define ERROR_UNKOWN_STATUS 4 // unkown error, never get this!
int FatalErrorValue=0;
bool opt_bitmain_new_cmd_type_vil = false;
bool opt_fixed_freq = false;
bool opt_pre_heat = true;
bool status_error = false;
bool once_error = false;
bool iic_ok = false;
int check_iic = 0;
bool update_temp =false;
bool check_temp_offside = false;
double chain_asic_RT[BITMAIN_MAX_CHAIN_NUM][CHAIN_ASIC_NUM]= {0};
uint64_t rate[BITMAIN_MAX_CHAIN_NUM] = {0};
uint64_t nonce_num[BITMAIN_MAX_CHAIN_NUM][BITMAIN_DEFAULT_ASIC_NUM][TIMESLICE] = {0};
int nonce_times = 0;
int rate_error[BITMAIN_MAX_CHAIN_NUM] = {0};
char displayed_rate[BITMAIN_MAX_CHAIN_NUM][32];
uint8_t chain_voltage_pic[BITMAIN_MAX_CHAIN_NUM] = {0xff};
int chain_voltage_value[BITMAIN_MAX_CHAIN_NUM] = {0};
unsigned char hash_board_id[BITMAIN_MAX_CHAIN_NUM][12];
int lowest_testOK_temp[BITMAIN_MAX_CHAIN_NUM]= {0}; // board test patten OK, we record temp in PIC, then we need keep board temp >= this lowest temp
int chain_temp_toolow[BITMAIN_MAX_CHAIN_NUM]= {0};
int LOWEST_TEMP_DOWN_FAN=MIN_TEMP_CONTINUE_DOWN_FAN;
#ifdef T9_18
unsigned char chain_pic_buf[BITMAIN_MAX_CHAIN_NUM][128] = {0};
#else
unsigned char last_freq[BITMAIN_MAX_CHAIN_NUM][256] = {0};
unsigned char badcore_num_buf[BITMAIN_MAX_CHAIN_NUM][64] = {0};
#endif
int chain_badcore_num[BITMAIN_MAX_CHAIN_NUM][256] = {0};
unsigned char show_last_freq[BITMAIN_MAX_CHAIN_NUM][256] = {0}; // only used to showed to users
unsigned char chip_last_freq[BITMAIN_MAX_CHAIN_NUM][256] = {0}; // this is the real value , which set freq into chips
unsigned char pic_temp_offset[BITMAIN_MAX_CHAIN_NUM] = {0};
unsigned char base_freq_index[BITMAIN_MAX_CHAIN_NUM] = {0};
int x_time[BITMAIN_MAX_CHAIN_NUM][256] = {0};
int temp_offside[BITMAIN_MAX_CHAIN_NUM] = {0};
static bool global_stop = false;
//Test Core
static int test_core = 8;
struct nonce_content temp_nonce_buf[MAX_RETURNED_NONCE_NUM];
struct reg_content temp_reg_buf[MAX_RETURNED_NONCE_NUM];
volatile struct nonce_buf nonce_read_out;
volatile struct reg_buf reg_value_buf;
#define USE_IIC 1
#define TEMP_CALI 0
static int8_t bottom_Offset[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
static int8_t middle_Offset[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
static int8_t bottom_Offset_sw[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
static int8_t middle_Offset_sw[BITMAIN_MAX_CHAIN_NUM][MAX_TEMPCHIP_NUM] = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0};
pthread_mutex_t init_log_mutex = PTHREAD_MUTEX_INITIALIZER;
bool isC5_CtrlBoard=false;
bool isChainAllCoresOpened[BITMAIN_MAX_CHAIN_NUM]= {false}; // is all cores opened flag
void set_led(bool stop);
void open_core(bool nullwork_enable);
pthread_mutex_t reinit_mutex = PTHREAD_MUTEX_INITIALIZER;
static int reinit_counter=0;
void bitmain_core_reInit();
signed char getMeddleOffsetForTestPatten(int chainIndex)
{
return middle_Offset[chainIndex][0];
}
unsigned int PHY_MEM_NONCE2_JOBID_ADDRESS=PHY_MEM_NONCE2_JOBID_ADDRESS_XILINX_1GB; // set to XILINX as default
bool isFixedFreqMode()
{
return opt_fixed_freq;
}
bool isC5_Board()
{
FILE *fd;
char board_type[32];
int isC5=0;
memset(board_type,'\0',32);
fd=fopen("/usr/bin/ctrl_bd","rb");
if(fd)
{
fread(board_type,1,32,fd);
fclose(fd);
if(strstr(board_type,"XILINX"))
{
isC5=0;
}
else isC5=1;
}
else
{
isC5=1;
}
if(isC5)
return true;
else return false;
}
void software_set_address();
void set_asic_ticket_mask(unsigned int ticket_mask);
void init_uart_baud();
void open_core_one_chain(int chainIndex, bool nullwork_enable);
void getAsicNum_preOpenCore(int chainIndex);
void writeInitLogFile(char *logstr);
void clearInitLogFile();
void re_send_last_job();
void saveSearchFailedFlagInfo(char *search_failed_info);
extern void jump_to_app_CheckAndRestorePIC(int chainIndex); // defined in Clement-bitmain.c
static unsigned char last_job_buffer[8192]= {23};
///////////// below they must be changed at same time!!!! ///////////////////////
typedef enum
{
TEMP_BOTTOM = 0, // 0 is bottom , 1 is middle
TEMP_MIDDLE
} Temp_Type_E;
////////////////////////////////////////////////////////////////////////////
#define MAX_ERROR_LIMIT_ABS ( 2 )
#define MAX_RETRY_COUNT ( 16 + 1 )
void *gpio0_vaddr=NULL;
struct all_parameters *dev;
unsigned int is_first_job = 0;
//other equipment related
// --------------------------------------------------------------
// CRC16 check table
// --------------------------------------------------------------
const uint8_t chCRCHTalbe[] = // CRC high byte table
{
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40
};
const uint8_t chCRCLTalbe[] = // CRC low byte table
{
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
0x41, 0x81, 0x80, 0x40
};
//crc
uint16_t CRC16(const uint8_t* p_data, uint16_t w_len)
{
uint8_t chCRCHi = 0xFF; // CRC high byte initialize
uint8_t chCRCLo = 0xFF; // CRC low byte initialize
uint16_t wIndex = 0; // CRC cycling index
while (w_len--)
{
wIndex = chCRCLo ^ *p_data++;
chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex];
chCRCHi = chCRCLTalbe[wIndex];
}
return ((chCRCHi << 8) | chCRCLo);
}
unsigned char CRC5(unsigned char *ptr, unsigned char len)
{
unsigned char i, j, k;
unsigned char crc = 0x1f;
unsigned char crcin[5] = {1, 1, 1, 1, 1};
unsigned char crcout[5] = {1, 1, 1, 1, 1};
unsigned char din = 0;
j = 0x80;
k = 0;
for (i = 0; i < len; i++)
{
if (*ptr & j)
{
din = 1;
}
else
{
din = 0;
}
crcout[0] = crcin[4] ^ din;
crcout[1] = crcin[0];
crcout[2] = crcin[1] ^ crcin[4] ^ din;
crcout[3] = crcin[2];
crcout[4] = crcin[3];
j = j >> 1;
k++;
if (k == 8)
{
j = 0x80;
k = 0;
ptr++;
}
memcpy(crcin, crcout, 5);
}
crc = 0;
if(crcin[4])
{
crc |= 0x10;
}
if(crcin[3])
{
crc |= 0x08;
}
if(crcin[2])
{
crc |= 0x04;
}
if(crcin[1])
{
crc |= 0x02;
}
if(crcin[0])
{
crc |= 0x01;
}
return crc;
}
unsigned char getPICvoltageFromValue(int vol_value) // vol_value = 940 means 9.4V
{
#ifdef S9_PLUS
#ifdef S9_PLUS_VOLTAGE2
unsigned char temp_voltage=1250.809516-127.817623*(vol_value*1.0)/100;
#else
unsigned char temp_voltage=824.784-73.1705*((vol_value*1.0)/100.0);
#endif
#endif
#ifdef S9_63
unsigned char temp_voltage = 1608.420446 - 170.423497*(vol_value*1.0)/100.0;
#endif
#ifdef R4
unsigned char temp_voltage = 1608.420446 - 170.423497*(vol_value*1.0)/100.0;
#endif
#ifdef T9_18
unsigned char temp_voltage = 364.0704 / (4.75*(vol_value*1.0)/100 - 32.79) - 30.72;
#endif
return temp_voltage;
}
int getVolValueFromPICvoltage(unsigned char vol_pic)
{
#ifdef S9_PLUS
#ifdef S9_PLUS_VOLTAGE2
int vol_value = ((1250.809516 - vol_pic)/127.817623)*100.0;
#else
int vol_value = ((824.784 - vol_pic)/73.1705)*100.0;
#endif
#endif
#ifdef S9_63
int vol_value = ((1608.420446 - vol_pic) *100.0)/170.423497;
#endif
#ifdef R4
int vol_value = ((1608.420446 - vol_pic) *100.0)/170.423497;
#endif
#ifdef T9_18
int vol_value = ((364.0704/(vol_pic+30.72))+32.79)*100/4.75;
#endif
vol_value=(vol_value/10)*10;
return vol_value;
}
int getVoltageLimitedFromHashrate(int hashrate_GHz)
{
int vol_value;
#ifdef DEBUG_WITHOUT_FREQ_VOLTAGE_LIMIT
return 940; // just fixed to highest voltage
#endif
#ifdef R4
if(isC5_CtrlBoard)
vol_value=R4_MAX_VOLTAGE_C5;
else vol_value=R4_MAX_VOLTAGE_XILINX;
#endif
#ifdef S9_PLUS
if(hashrate_GHz>=12500)
vol_value=840;
else if(hashrate_GHz>=12000)
vol_value=850;
else if(hashrate_GHz>=11500)
vol_value=870;
else if(hashrate_GHz>=11000)
vol_value=890;
else if(hashrate_GHz>=10500)
vol_value=910;
else if(hashrate_GHz>=10000)
vol_value=930;
else if(hashrate_GHz>=9500)
vol_value=960;
else if(hashrate_GHz>=9000)
vol_value=970;
else
vol_value=970;
#endif
#ifdef S9_63
if(hashrate_GHz>=14500)
vol_value=870;
else if(hashrate_GHz>=14000)
vol_value=880;
else if(hashrate_GHz>=13500)
vol_value=900;
else if(hashrate_GHz>=13000)
vol_value=910;
else if(hashrate_GHz>=12500)
vol_value=930;
else
vol_value=940;
#endif
#ifdef T9_18
if(hashrate_GHz>=12000)
vol_value=810;
else if(hashrate_GHz>=11500)
vol_value=830;
else if(hashrate_GHz>=11000)
vol_value=850;
else if(hashrate_GHz>=10500)
vol_value=870;
else if(hashrate_GHz>=10000)
vol_value=890;
else if(hashrate_GHz>=9500)
vol_value=920;
else if(hashrate_GHz>=9000)
vol_value=930;
else
vol_value=930;
#endif
return vol_value;
}
int getFixedFreqVoltageValue(int freq)
{
int vol_value;
#ifdef R4
if(isC5_CtrlBoard)
vol_value=890;
else vol_value=910;
#endif
#ifdef S9_PLUS
if(freq>=643) // hashrate 12500
vol_value=840;
else if(freq>=618) // hashrate 12000
vol_value=850;
else if(freq>=593) // hashrate 11500
vol_value=870;
else if(freq>=568) // hashrate 11000
vol_value=890;
else if(freq>=543) // hashrate 10500
vol_value=910;
else if(freq>=516) // hashrate 10000
vol_value=930;
else if(freq>=491) // hashrate 9500
vol_value=960;
else if(freq>=462) // hashrate 9000
vol_value=970;
else
vol_value=970;
#endif
#ifdef S9_63
if(freq>=675) // hashrate 14500
vol_value=870;
else if(freq>=650) // hashrate 14000
vol_value=880;
else if(freq>=631) // hashrate 13500
vol_value=900;
else if(freq>=606) // hashrate 13000
vol_value=910;
else if(freq>=581) // hashrate 12500
vol_value=930;
else
vol_value=940;
#endif
#ifdef T9_18
if(freq>=650) // hashrate 12000
vol_value=810;
else if(freq>=625) // hashrate 11500
vol_value=830;
else if(freq>=600) // hashrate 11000
vol_value=850;
else if(freq>=575) // hashrate 10500
vol_value=870;
else if(freq>=543) // hashrate 10000
vol_value=890;
else if(freq>=516) // hashrate 9500
vol_value=920;
else if(freq>=491) // hashrate 9000
vol_value=930;
else
vol_value=930;
#endif
return vol_value;
}
#ifdef T9_18
void getPICChainIndexOffset(int chainIndex, int *pChain, int *pOffset)
{
int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
switch(chainIndex)
{
case 1:
new_T9_PLUS_chainIndex=1;
new_T9_PLUS_chainOffset=0;
break;
case 8:
new_T9_PLUS_chainIndex=1;
new_T9_PLUS_chainOffset=1;
break;
case 9:
new_T9_PLUS_chainIndex=1;
new_T9_PLUS_chainOffset=2;
break;
case 2:
new_T9_PLUS_chainIndex=2;
new_T9_PLUS_chainOffset=0;