-
Notifications
You must be signed in to change notification settings - Fork 109
/
driver-btm-c5.c
12362 lines (10708 loc) · 443 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
/* 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 "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 <sys/sysinfo.h>
#include "elist.h"
#include "miner.h"
// #include "usbutils.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);
#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 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;
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 fpga_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t opencore_readtemp_mutex = PTHREAD_MUTEX_INITIALIZER;
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 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;
break;
case 10:
new_T9_PLUS_chainIndex=2;
new_T9_PLUS_chainOffset=1;
break;
case 11:
new_T9_PLUS_chainIndex=2;
new_T9_PLUS_chainOffset=2;
break;
case 3:
new_T9_PLUS_chainIndex=3;
new_T9_PLUS_chainOffset=0;
break;
case 12:
new_T9_PLUS_chainIndex=3;
new_T9_PLUS_chainOffset=1;
break;
case 13:
new_T9_PLUS_chainIndex=3;
new_T9_PLUS_chainOffset=2;
break;
default:
new_T9_PLUS_chainIndex=0;
new_T9_PLUS_chainOffset=0;
break;
}
*pChain=new_T9_PLUS_chainIndex;
*pOffset=new_T9_PLUS_chainOffset;
}
#endif
int getChainAsicFreqIndex(int chainIndex, int asicIndex)
{
#ifdef T9_18
if(fpga_version>=0xE)
{
int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset; // only used by new T9+ FPGA
getPICChainIndexOffset(chainIndex,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
return chain_pic_buf[new_T9_PLUS_chainIndex][7+new_T9_PLUS_chainOffset*31+4+asicIndex];
}
else
{
return chain_pic_buf[((chainIndex/3)*3)][7+(chainIndex%3)*31+4+asicIndex];
}
#else
return last_freq[chainIndex][asicIndex*2+3];
#endif
}
// 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 get_pic_iic_flash_addr_pointer(unsigned char chain, unsigned char *addr_H, unsigned char *addr_L);
void set_pic_iic_flash_addr_pointer(unsigned char chain, unsigned char addr_H, unsigned char addr_L)
{
unsigned char check_addr_H, check_addr_L;
char logstr[256];
int try_count=0;
#ifdef ENABLE_CHECK_PIC_FLASH_ADDR
do
{
#endif
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);
// we need check this address, because some PIC lost data of flash!!!
get_pic_iic_flash_addr_pointer(chain,&check_addr_H,&check_addr_L);
if(check_addr_H!=addr_H || check_addr_L!=addr_L)
{
sprintf(logstr,"Error of set PIC FLASH addr: addr_H=%x(%x) addr_L=%x(%x) on Chain[%d]\n",addr_H,check_addr_H,addr_L,check_addr_L,chain);
writeInitLogFile(logstr);
#ifndef ENABLE_CHECK_PIC_FLASH_ADDR
}
#else
try_count++;
if(try_count>3)
break;
reset_iic_pic(chain);
sleep(5);
}
else break;
}
while(1);
#endif
}
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)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, ERASE_IIC_FLASH);
usleep(100000);
/* while(1)
{
usleep(3000);
ret = write_pic_iic(true, false, 0x0, chain, 0);
if(ret == 0x0)
{
printf("erase done\n");
return ret;
}
}
*/
return 0;
}
unsigned char erase_pic_flash_all(unsigned char chain)
{
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);
}
#ifdef T9_18
void AT24C02_read_bytes(unsigned char address, unsigned char *buf, unsigned char which_iic, unsigned char length);
// only the first chain of hashboard has temp sensor!!!
void get_temperature_offset_value(unsigned char chain, unsigned char *value)
{
if(fpga_version>=0xE)
{
int new_T9_PLUS_chainIndex,new_T9_PLUS_chainOffset;
getPICChainIndexOffset(chain,&new_T9_PLUS_chainIndex,&new_T9_PLUS_chainOffset);
AT24C02_read_bytes(SENSOR_OFFSET_ADDR, value, new_T9_PLUS_chainIndex, 8);
}
else
{
AT24C02_read_bytes(SENSOR_OFFSET_ADDR, value, (chain/3), 8);
}
}
#else
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);
}
#endif
unsigned char write_data_into_pic_flash(unsigned char chain)
{
send_pic_command(chain);
write_pic_iic(false, false, 0x0, chain, WRITE_DATA_INTO_PIC);
usleep(100000);
return 0;
}
#ifdef T9_18
int dsPIC33EP16GS202_get_pic_sw_version(unsigned char which_iic, unsigned char *version)
{
unsigned char length = 0x04, crc_data[2] = {0xff}, read_back_data[5] = {0xff};
unsigned short crc = 0;
int retry_count=0;
char logstr[256];
printf("\n--- %s\n", __FUNCTION__);
*version = 0xff;
crc = length + GET_PIC_SOFTWARE_VERSION;
crc_data[0] = (unsigned char)((crc >> 8) & 0x00ff);
crc_data[1] = (unsigned char)((crc >> 0) & 0x00ff);