forked from ubiGG/bmminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-btm-c5.c
8569 lines (7366 loc) · 256 KB
/
driver-btm-c5.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
#include "config.h"
#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 "elist.h"
#include "miner.h"
// #include "usbutils.h"
extern bool re_calc_ghs;
bool someBoardUpVoltage=false;
bool isUseDefaultFreq=false;
bool doTestPatten=false;
bool startCheckNetworkJob=false;
extern bool clement_preInit(bool showlog);
extern int clement_ReInit(bool showlog);
extern bool clement_doTestBoard(bool showlog);
bool clement_doTestBoardOnce(bool showlog);
static int get_macBytes(char * device, unsigned char *mac);
#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);
}
}
#include "util.h"
#include "driver-btm-c5.h"
#include "sha2_c5.h"
#ifdef DEBUG_218_FAN_FULLSPEED
bool is218_Temp=false;
#endif
int global_ASIC_NUM=CHAIN_ASIC_NUM;
//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;
struct thr_info *send_mac_thr;
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 opencore_readtemp_mutex = PTHREAD_MUTEX_INITIALIZER;
extern pthread_mutex_t temp_work_mutex[BITMAIN_MAX_CHAIN_NUM];
uint64_t h = 0;
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_c5_freq = 600;
int opt_bitmain_c5_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 status_error = false;
bool once_error = false;
bool iic_ok = false;
int check_iic = 0;
bool update_temp =false;
bool check_temp_offside = false;
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[BITMAIN_MAX_CHAIN_NUM] = {0};
int chain_voltage_pic[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;
unsigned char last_freq[BITMAIN_MAX_CHAIN_NUM][256];
unsigned char badcore_num_buf[BITMAIN_MAX_CHAIN_NUM][64];
int chain_badcore_num[BITMAIN_MAX_CHAIN_NUM][256];
unsigned char show_last_freq[BITMAIN_MAX_CHAIN_NUM][256]; // only used to showed to users
unsigned char chip_last_freq[BITMAIN_MAX_CHAIN_NUM][256]; // this is the real value , which set freq into chips
unsigned char pic_temp_offset[BITMAIN_MAX_CHAIN_NUM];
unsigned char base_freq_index[BITMAIN_MAX_CHAIN_NUM];
int x_time[BITMAIN_MAX_CHAIN_NUM][256] = {0};
int temp_offside[BITMAIN_MAX_CHAIN_NUM] = {0};
static bool global_stop = false;
#define id_string_len 34
#define AUTH_URL "auth.minerlink.com"
#define PORT "7000"
static bool need_send = true;
char * mac;
bool stop_mining = false;
char hash_board_id_string[BITMAIN_MAX_CHAIN_NUM*id_string_len];
//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;
void set_led(bool stop);
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];
}
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 writeInitLogFile(char *logstr);
void clearInitLogFile();
void re_send_last_job();
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
unsigned char temp_voltage=824.784-73.1705*((vol_value*1.0)/100.0);
#else
unsigned char temp_voltage = 1608.420446 - 170.423497*(vol_value*1.0)/100.0;
#endif
return temp_voltage;
}
int getVolValueFromPICvoltage(unsigned char vol_pic)
{
#ifdef S9_PLUS
int vol_value = ((824.784 - vol_pic)/73.1705)*100.0;
#else
int vol_value = ((1608.420446 - vol_pic) *100.0)/170.423497;
#endif
return vol_value;
}
int getVoltageLimitedFromHashrate(int hashrate_GHz)
{
int vol_value;
#ifdef R4
vol_value=R4_MAX_VOLTAGE;
#else
#ifdef S9_PLUS
#ifdef ENABLE_HIGH_VOLTAGE_BUG_FIX
if(hashrate_GHz>=12500)
vol_value=S9_PLUS_12500_VOL_LIMITED;
else if(hashrate_GHz>=12000)
vol_value=S9_PLUS_12000_VOL_LIMITED;
else if(hashrate_GHz>=11500)
vol_value=S9_PLUS_11500_VOL_LIMITED;
else if(hashrate_GHz>=11000)
vol_value=S9_PLUS_11000_VOL_LIMITED;
else
vol_value=S9_PLUS_LOWER_11000_VOL_LIMITED;
#else
if(hashrate_GHz>=12500)
vol_value=S9_PLUS_12500_VOL_LIMITED;
else if(hashrate_GHz>=12000)
vol_value=S9_PLUS_12000_VOL_LIMITED;
else if(hashrate_GHz>=11500)
vol_value=S9_PLUS_11500_VOL_LIMITED;
else if(hashrate_GHz>=11000)
vol_value=S9_PLUS_11000_VOL_LIMITED;
else if(hashrate_GHz>=10500)
vol_value=S9_PLUS_10500_VOL_LIMITED;
else if(hashrate_GHz>=10000)
vol_value=S9_PLUS_10000_VOL_LIMITED;
else if(hashrate_GHz>=9500)
vol_value=S9_PLUS_9500_VOL_LIMITED;
else if(hashrate_GHz>=9000)
vol_value=S9_PLUS_9000_VOL_LIMITED;
else
vol_value=S9_PLUS_LOWER_9000_VOL_LIMITED;
#endif
#else
if(hashrate_GHz>=14000)
vol_value=S9_14000_VOL_LIMITED;
else if(hashrate_GHz>=13500)
vol_value=S9_13500_VOL_LIMITED;
else if(hashrate_GHz>=13000)
vol_value=S9_13000_VOL_LIMITED;
else if(hashrate_GHz>=12500)
vol_value=S9_12500_VOL_LIMITED;
else
vol_value=S9_LOWER_12000_VOL_LIMITED;
#endif
#endif
return vol_value;
}
// pic
unsigned int get_pic_iic()
{
int ret = -1;
ret = *(axi_fpga_addr + IIC_COMMAND);
applog(LOG_DEBUG,"%s: IIC_COMMAND is 0x%x\n", __FUNCTION__, ret);
return ret;
}
unsigned char set_pic_iic(unsigned int data)
{
unsigned int ret=0;
unsigned char ret_data = 0;
*((unsigned int *)(axi_fpga_addr + IIC_COMMAND)) = data & 0x7fffffff;
applog(LOG_DEBUG,"%s: set IIC_COMMAND is 0x%x\n", __FUNCTION__, data & 0x7fffffff);
while(1)
{
ret = get_pic_iic();
if(ret & 0x80000000)
{
ret_data = (unsigned char)(ret & 0x000000ff);
return ret_data;
}
else
{
applog(LOG_DEBUG,"%s: waiting write pic iic\n", __FUNCTION__);
cgsleep_us(1000);
}
}
}
unsigned char write_pic_iic(bool read, bool reg_addr_valid, unsigned char reg_addr, unsigned char chain, unsigned char data)
{
unsigned int value = 0x00000000;
unsigned char ret = 0;
if(read)
{
value |= IIC_READ;
}
if(reg_addr_valid)
{
value |= IIC_REG_ADDR_VALID;
value |= IIC_REG_ADDR(reg_addr);
}
value |= IIC_ADDR_HIGH_4_BIT;
value |= IIC_CHAIN_NUMBER(chain);
value |= data;
ret = set_pic_iic(value);
return ret;
}
void send_pic_command(unsigned char chain)
{
write_pic_iic(false, false, 0x0, chain, PIC_COMMAND_1);
write_pic_iic(false, false, 0x0, chain, PIC_COMMAND_2);
}
void set_pic_iic_flash_addr_pointer(unsigned char chain, unsigned char addr_H, unsigned char addr_L)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, SET_PIC_FLASH_POINTER);
write_pic_iic(false, false, 0x0, chain, addr_H);
write_pic_iic(false, false, 0x0, chain, addr_L);
}
void send_data_to_pic_iic(unsigned char chain, unsigned char command, unsigned char *buf, unsigned char length)
{
int i=0;
write_pic_iic(false, false, 0x0, chain, command);
for(i=0; i<length; i++)
{
write_pic_iic(false, false, 0x0, chain, *(buf + i));
}
}
void get_data_from_pic_iic(unsigned char chain, unsigned char command, unsigned char *buf, unsigned char length)
{
int i=0;
write_pic_iic(false, false, 0x0, chain, command);
for(i=0; i<length; i++)
{
*(buf + i) = write_pic_iic(true, false, 0x0, chain, 0);
}
}
void send_data_to_pic_flash(unsigned char chain, unsigned char *buf)
{
send_pic_command(chain);
send_data_to_pic_iic(chain, SEND_DATA_TO_IIC, buf, 16);
}
void get_data_from_pic_flash(unsigned char chain, unsigned char *buf)
{
send_pic_command(chain);
get_data_from_pic_iic(chain, READ_DATA_FROM_IIC, buf, 16);
}
unsigned char erase_pic_flash(unsigned char chain)
{
unsigned char ret=0xff;
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, ERASE_IIC_FLASH);
while(1)
{
cgsleep_us(3000);
ret = write_pic_iic(true, false, 0x0, chain, 0);
if(ret == 0x0)
{
return ret;
}
}
}
unsigned char erase_pic_flash_all(unsigned char chain)
{
unsigned char ret=0xff;
unsigned int i=0, erase_loop = 0;
unsigned char start_addr_h = PIC_FLASH_POINTER_START_ADDRESS_H, start_addr_l = PIC_FLASH_POINTER_START_ADDRESS_L;
unsigned char end_addr_h = PIC_FLASH_POINTER_END_ADDRESS_H, end_addr_l = PIC_FLASH_POINTER_END_ADDRESS_L;
unsigned int pic_flash_length=0;
set_pic_iic_flash_addr_pointer(chain, PIC_FLASH_POINTER_START_ADDRESS_H, PIC_FLASH_POINTER_START_ADDRESS_L);
pic_flash_length = (((unsigned int)end_addr_h << 8) + end_addr_l) - (((unsigned int)start_addr_h << 8) + start_addr_l) + 1;
erase_loop = pic_flash_length/PIC_FLASH_SECTOR_LENGTH;
for(i=0; i<erase_loop; i++)
{
erase_pic_flash(chain);
}
}
void set_temperature_offset_value(unsigned char chain, unsigned char *value)
{
send_pic_command(chain);
send_data_to_pic_iic(chain, WR_TEMP_OFFSET_VALUE, value, 8);
cgsleep_ms(100000);
}
void get_temperature_offset_value(unsigned char chain, unsigned char *value)
{
send_pic_command(chain);
get_data_from_pic_iic(chain, RD_TEMP_OFFSET_VALUE, value, 8);
}
unsigned char write_data_into_pic_flash(unsigned char chain)
{
unsigned char ret=0xff;
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, WRITE_DATA_INTO_PIC);
while(1)
{
cgsleep_us(10000);
ret = write_pic_iic(true, false, 0x0, chain, 0);
if(ret == 0x0)
{
return ret;
}
}
}
unsigned char jump_to_app_from_loader(unsigned char chain)
{
unsigned char ret=0xff;
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, JUMP_FROM_LOADER_TO_APP);
cgsleep_us(100000);
}
unsigned char reset_iic_pic(unsigned char chain)
{
unsigned char ret=0xff;
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, RESET_PIC);
cgsleep_us(100000);
}
void get_pic_iic_flash_addr_pointer(unsigned char chain, unsigned char *addr_H, unsigned char *addr_L)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, GET_PIC_FLASH_POINTER);
*addr_H = write_pic_iic(true, false, 0x0, chain, 0);
*addr_L = write_pic_iic(true, false, 0x0, chain, 0);
}
void set_pic_voltage(unsigned char chain, unsigned char voltage)
{
send_pic_command(chain);
applog(LOG_NOTICE,"%s voltage %u",__FUNCTION__,voltage);
write_pic_iic(false, false, 0x0, chain, SET_VOLTAGE);
write_pic_iic(false, false, 0x0, chain, voltage);
cgsleep_us(100000);
}
unsigned char get_pic_voltage(unsigned char chain)
{
unsigned char ret=0;
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, GET_VOLTAGE);
ret = write_pic_iic(true, false, 0x0, chain, 0);
applog(LOG_NOTICE,"%s: voltage = %d\n", __FUNCTION__, ret);
return ret;
}
void set_voltage_setting_time(unsigned char chain, unsigned char *time)
{
send_pic_command(chain);
send_data_to_pic_iic(chain, SET_VOLTAGE_TIME, time, 6);
cgsleep_us(100000);
}
void set_hash_board_id_number(unsigned char chain, unsigned char *id)
{
send_pic_command(chain);
send_data_to_pic_iic(chain, SET_HASH_BOARD_ID, id, 12);
cgsleep_us(100000);
}
void get_hash_board_id_number(unsigned char chain, unsigned char *id)
{
send_pic_command(chain);
get_data_from_pic_iic(chain, GET_HASH_BOARD_ID, id, 12);
}
void write_host_MAC_and_time(unsigned char chain, unsigned char *buf)
{
send_pic_command(chain);
send_data_to_pic_iic(chain, SET_HOST_MAC_ADDRESS, buf, 12);
cgsleep_us(100000);
}
void enable_pic_dc_dc(unsigned char chain)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, ENABLE_VOLTAGE);
write_pic_iic(false, false, 0x0, chain, 1);
}
void enable_pic_dc_dc_all()
{
int i;
for(i=0; i < BITMAIN_MAX_CHAIN_NUM; i++)
{
if(dev->chain_exist[i] == 1)
{
enable_pic_dc_dc(i);
cgsleep_ms(1);
}
}
}
void enable_pic_dac(unsigned char chain)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, ENABLE_VOLTAGE);
write_pic_iic(false, false, 0x0, chain, 1);
}
void disable_pic_dac(unsigned char chain)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, ENABLE_VOLTAGE);
write_pic_iic(false, false, 0x0, chain, 0);
}
void pic_heart_beat_each_chain(unsigned char chain)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, SEND_HEART_BEAT);
}
//FPGA related
int get_nonce2_and_job_id_store_address(void)
{
int ret = -1;
ret = *((unsigned int *)(axi_fpga_addr + NONCE2_AND_JOBID_STORE_ADDRESS));
applog(LOG_DEBUG,"%s: NONCE2_AND_JOBID_STORE_ADDRESS is 0x%x\n", __FUNCTION__, ret);
return ret;
}
void set_nonce2_and_job_id_store_address(unsigned int value)
{
*((unsigned int *)(axi_fpga_addr + NONCE2_AND_JOBID_STORE_ADDRESS)) = value;
applog(LOG_DEBUG,"%s: set NONCE2_AND_JOBID_STORE_ADDRESS is 0x%x\n", __FUNCTION__, value);
get_nonce2_and_job_id_store_address();
}
int get_job_start_address(void)
{
int ret = -1;
ret = *((unsigned int *)(axi_fpga_addr + JOB_START_ADDRESS));
applog(LOG_DEBUG,"%s: JOB_START_ADDRESS is 0x%x\n", __FUNCTION__, ret);
return ret;
}
void set_job_start_address(unsigned int value)
{
*((unsigned int *)(axi_fpga_addr + JOB_START_ADDRESS)) = value;
applog(LOG_DEBUG,"%s: set JOB_START_ADDRESS is 0x%x\n", __FUNCTION__, value);
get_job_start_address();
}
int get_QN_write_data_command(void)
{
int ret = -1;
ret = *((axi_fpga_addr + QN_WRITE_DATA_COMMAND));
applog(LOG_DEBUG,"%s: QN_WRITE_DATA_COMMAND is 0x%x\n", __FUNCTION__, ret);
return ret;
}
void set_QN_write_data_command(unsigned int value)
{
*(axi_fpga_addr + QN_WRITE_DATA_COMMAND) = value;
applog(LOG_DEBUG,"%s: set QN_WRITE_DATA_COMMAND is 0x%x\n", __FUNCTION__, value);
get_QN_write_data_command();
}
int bitmain_axi_init()
{
unsigned int data;
int ret=0;
fd = open("/dev/axi_fpga_dev", O_RDWR);
if(fd < 0)
{
applog(LOG_DEBUG,"/dev/axi_fpga_dev open failed. fd = %d\n", fd);
perror("open");
return -1;
}
axi_fpga_addr = mmap(NULL, TOTAL_LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if(!axi_fpga_addr)
{
applog(LOG_DEBUG,"mmap axi_fpga_addr failed. axi_fpga_addr = 0x%x\n", axi_fpga_addr);
return -1;
}
applog(LOG_DEBUG,"mmap axi_fpga_addr = 0x%x\n", axi_fpga_addr);
//check the value in address 0xff200000
data = *axi_fpga_addr;
if((data & 0x0000FFFF) != HARDWARE_VERSION_VALUE)
{
applog(LOG_DEBUG,"data = 0x%x, and it's not equal to HARDWARE_VERSION_VALUE : 0x%x\n", data, HARDWARE_VERSION_VALUE);
//return -1;
}
applog(LOG_DEBUG,"axi_fpga_addr data = 0x%x\n", data);
fd_fpga_mem = open("/dev/fpga_mem", O_RDWR);
if(fd_fpga_mem < 0)
{
applog(LOG_DEBUG,"/dev/fpga_mem open failed. fd_fpga_mem = %d\n", fd_fpga_mem);
perror("open");
return -1;
}
fpga_mem_addr = mmap(NULL, FPGA_MEM_TOTAL_LEN, PROT_READ|PROT_WRITE, MAP_SHARED, fd_fpga_mem, 0);
if(!fpga_mem_addr)
{
applog(LOG_DEBUG,"mmap fpga_mem_addr failed. fpga_mem_addr = 0x%x\n", fpga_mem_addr);
return -1;
}
applog(LOG_DEBUG,"mmap fpga_mem_addr = 0x%x\n", fpga_mem_addr);
nonce2_jobid_address = fpga_mem_addr;
job_start_address_1 = fpga_mem_addr + NONCE2_AND_JOBID_STORE_SPACE/sizeof(int);
job_start_address_2 = fpga_mem_addr + (NONCE2_AND_JOBID_STORE_SPACE + JOB_STORE_SPACE)/sizeof(int);
applog(LOG_DEBUG,"job_start_address_1 = 0x%x\n", job_start_address_1);
applog(LOG_DEBUG,"job_start_address_2 = 0x%x\n", job_start_address_2);
set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
dev = calloc(sizeof(struct all_parameters), sizeof(char));
if(!dev)
{
applog(LOG_DEBUG,"kmalloc for dev failed.\n");
return -1;
}
else
{
dev->current_job_start_address = job_start_address_1;
applog(LOG_DEBUG,"kmalloc for dev success.\n");
}
return ret;
}
int bitmain_axi_Reinit()
{
int ret=0;
unsigned int data;
char logstr[256];
data = *axi_fpga_addr;
if((data & 0x0000FFFF) != HARDWARE_VERSION_VALUE)
{
sprintf(logstr,"data = 0x%x, and it's not equal to HARDWARE_VERSION_VALUE : 0x%x\n", data, HARDWARE_VERSION_VALUE);
writeInitLogFile(logstr);
}
sprintf(logstr,"axi_fpga_addr data = 0x%x\n", data);
writeInitLogFile(logstr);
nonce2_jobid_address = fpga_mem_addr;
job_start_address_1 = fpga_mem_addr + NONCE2_AND_JOBID_STORE_SPACE/sizeof(int);
job_start_address_2 = fpga_mem_addr + (NONCE2_AND_JOBID_STORE_SPACE + JOB_STORE_SPACE)/sizeof(int);
set_nonce2_and_job_id_store_address(PHY_MEM_NONCE2_JOBID_ADDRESS);
set_job_start_address(PHY_MEM_JOB_START_ADDRESS_1);
return ret;
}
int bitmain_axi_close()
{
int ret = 0;
ret = munmap((void *)axi_fpga_addr, TOTAL_LEN);
if(ret<0)
{
applog(LOG_DEBUG,"munmap failed!\n");
}
ret = munmap((void *)fpga_mem_addr, FPGA_MEM_TOTAL_LEN);
if(ret<0)
{
applog(LOG_DEBUG,"munmap failed!\n");
}
//free_pages((unsigned long)nonce2_jobid_address, NONCE2_AND_JOBID_STORE_SPACE_ORDER);
//free(temp_job_start_address_1);
//free(temp_job_start_address_2);
close(fd);
close(fd_fpga_mem);
}
int get_fan_control(void)
{
int ret = -1;
ret = *((unsigned int *)(axi_fpga_addr + FAN_CONTROL));
applog(LOG_DEBUG,"%s: FAN_CONTROL is 0x%x\n", __FUNCTION__, ret);
return ret;
}
void set_fan_control(unsigned int value)
{
*((unsigned int *)(axi_fpga_addr + FAN_CONTROL)) = value;
applog(LOG_DEBUG,"%s: set FAN_CONTROL is 0x%x\n", __FUNCTION__, value);
get_fan_control();
}
int get_hash_on_plug(void)