forked from ckolivas/cgminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-gridseed.c
1989 lines (1675 loc) · 52.5 KB
/
driver-gridseed.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 Faster <[email protected]>
* Copyright 2012-2013 Andrew Smith
* Copyright 2012 Luke Dashjr
* Copyright 2012 Con Kolivas <[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 <limits.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h>
#include <sys/time.h>
#include <unistd.h>
#include <math.h>
#ifdef WIN32
#include "compat.h"
#include <windows.h>
#include <winsock2.h>
#include <io.h>
#else
#include <sys/select.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif /* WIN32 */
#include "elist.h"
#include "miner.h"
#include "usbutils.h"
#include "driver-gridseed.h"
#include "util.h"
static const char *gridseed_version = "v3.8.5.20140210.02";
static const char *str_reset[] = {
"55AAC000808080800000000001000000", // Chip reset
"55AAC000E0E0E0E00000000001000000", // FW reset
NULL
};
static const char *str_init[] = {
"55AAC000C0C0C0C00500000001000000",
"55AAEF020000000000000000000000000000000000000000",
"55AAEF3020000000",
NULL
};
static const char *str_ltc_reset[] = {
"55AA1F2817000000",
"55AA1F2814000000",
"55AA1F2817000000",
NULL
};
static const char *str_nofifo[] = {
"55AAC000D0D0D0D00000000001000000",
NULL
};
#ifdef WIN32
static void set_text_color(WORD color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
#endif
/*---------------------------------------------------------------------------------------*/
static int gridseed_send_ping_packet(GRIDSEED_INFO *, struct sockaddr_in);
static void *gridseed_recv_packet(void *);
static void gc3355_send_cmds(struct cgpu_info *, const char **);
static bool gridseed_send_work_usb(struct cgpu_info *, unsigned char *, unsigned char *,
unsigned char *, int, enum gsd_mode);
static void __gridseed_test_ltc_nonce(struct cgpu_info *, GRIDSEED_INFO *,
struct thr_info *, uint32_t, unsigned int);
static int check_udp_port_in_use(short port)
{
int sock;
struct sockaddr_in local;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
return -1;
local.sin_family = AF_INET;
local.sin_port = htons(port);
local.sin_addr.s_addr = inet_addr("127.0.0.1");
if (bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0) {
close(sock);
return -2;
}
return sock;
}
static int gridseed_create_proxy(struct cgpu_info *gridseed, GRIDSEED_INFO *info)
{
int sock = info->sockltc;
short port = info->ltc_port;
if (sock > 0)
close(sock);
info->sockltc = -1;
while (true) {
sock = check_udp_port_in_use(port);
if (sock > 0)
break;
port++;
}
info->sockltc = sock;
info->ltc_port = port;
applog(LOG_NOTICE, "Create scrypt proxy on %d/UDP for %s%d", info->ltc_port, gridseed->drv->name, gridseed->device_id);
return 0;
}
static int gridseed_find_proxy(GRIDSEED_INFO *info)
{
struct sockaddr_in remote;
struct timeval tv_timeout;
fd_set rdfs;
int sock = info->sockltc;
short port = info->ltc_port + 1000;
if (sock > 0)
close(sock);
info->sockltc = -1;
while (true) {
sock = check_udp_port_in_use(port);
if (sock > 0)
break;
port++;
}
info->sockltc = sock;
info->ltc_port = port - 1000;
remote.sin_family = AF_INET;
remote.sin_port = htons(info->ltc_port);
remote.sin_addr.s_addr = inet_addr("127.0.0.1");
if (gridseed_send_ping_packet(info, remote) != 0)
return -1;
tv_timeout.tv_sec = 0;
tv_timeout.tv_usec = 500000;
FD_ZERO(&rdfs);
FD_SET(sock, &rdfs);
if (select(sock+1, &rdfs, NULL, NULL, &tv_timeout) != 1)
return -1;
//if (cgsem_mswait(&info->psem, 500) != 0)
// return -1;
applog(LOG_NOTICE, "Found scrypt proxy on %d/UDP", info->ltc_port);
return 0;
}
static int gridseed_send_ping_packet(GRIDSEED_INFO *info, struct sockaddr_in to)
{
GRIDSEED_PACKET packet;
if (info->sockltc <= 0)
return -1;
packet.type = PACKET_PING;
if (sendto(info->sockltc, &packet, sizeof(packet), 0, (struct sockaddr *)&to,
sizeof(to)) != sizeof(packet)) {
applog(LOG_WARNING, "Couldn't send ping packet: %s", strerror(errno));
return -1;
}
return 0;
}
static int gridseed_send_info_packet(GRIDSEED_INFO *info, struct sockaddr_in to)
{
GRIDSEED_PACKET packet;
if (info->sockltc <= 0)
return -1;
packet.type = PACKET_INFO;
packet.info.freq = info->freq;
packet.info.chips = info->chips;
packet.info.modules = info->modules;
strncpy(packet.info.serial, info->serial, sizeof(packet.info.serial));
if (sendto(info->sockltc, &packet, sizeof(packet), 0, (struct sockaddr *)&to,
sizeof(to)) != sizeof(packet)) {
applog(LOG_WARNING, "Couldn't send info packet: %s", strerror(errno));
return -1;
}
return 0;
}
static void gridseed_recv_info_packet(struct cgpu_info *gridseed, GRIDSEED_INFO *info,
GRIDSEED_PACKET packet, struct sockaddr_in from)
{
mutex_lock(&info->qlock);
info->freq = packet.info.freq;
info->chips = packet.info.chips;
info->modules = packet.info.modules;
strncpy(info->serial, packet.info.serial, sizeof(info->serial));
gridseed->unique_id = info->serial;
info->toaddr = from;
mutex_unlock(&info->qlock);
cgsem_post(&info->psem);
}
static bool gridseed_send_work_packet(GRIDSEED_INFO *info, struct work *work)
{
GRIDSEED_PACKET packet;
if (info->sockltc <= 0)
return false;
packet.type = PACKET_WORK;
memcpy(packet.work.target, work->target, sizeof(packet.work.target));
memcpy(packet.work.midstate, work->midstate, sizeof(packet.work.midstate));
memcpy(packet.work.data, work->data, sizeof(packet.work.data));
packet.work.id = work->id;
if (sendto(info->sockltc, &packet, sizeof(packet), 0, (struct sockaddr *)&(info->toaddr),
sizeof(info->toaddr)) != sizeof(packet)) {
applog(LOG_WARNING, "Couldn't send work packet: %s", strerror(errno));
return false;
}
return true;
}
static void gridseed_recv_work_packet(struct cgpu_info *gridseed, GRIDSEED_INFO *info,
GRIDSEED_PACKET packet, struct sockaddr_in from)
{
mutex_lock(&info->qlock);
info->toaddr = from;
gridseed_send_work_usb(gridseed, packet.work.target, packet.work.midstate,
packet.work.data, packet.work.id, MODE_SCRYPT_DUAL);
mutex_unlock(&info->qlock);
}
static int gridseed_send_nonce_packet(GRIDSEED_INFO *info, unsigned char *data)
{
GRIDSEED_PACKET packet;
uint32_t nonce;
int workid;
if (info->sockltc <= 0)
return -1;
memcpy(&workid, data+8, 4);
memcpy(&nonce, data+4, 4);
packet.type = PACKET_NONCE;
packet.nonce.nonce = nonce;
packet.nonce.workid = workid;
if (sendto(info->sockltc, &packet, sizeof(packet), 0, (struct sockaddr *)&(info->toaddr),
sizeof(info->toaddr)) != sizeof(packet)) {
applog(LOG_WARNING, "Couldn't send nonce packet: %s", strerror(errno));
return -1;
}
return 0;
}
static void gridseed_recv_nonce_packet(struct cgpu_info *gridseed, GRIDSEED_INFO *info,
GRIDSEED_PACKET packet)
{
__gridseed_test_ltc_nonce(gridseed, info, info->thr, packet.nonce.nonce, packet.nonce.workid);
}
static void *gridseed_recv_packet(void *userdata)
{
struct cgpu_info *gridseed = (struct cgpu_info *)userdata;
GRIDSEED_INFO *info = gridseed->device_data;
GRIDSEED_PACKET packet;
struct timeval ts_packet, ts_now, tv_timeout;
struct sockaddr_in fromaddr;
fd_set rdfs;
char threadname[24];
int addrlen, n, no_packets = 0, sock = info->sockltc;
snprintf(threadname, sizeof(threadname), "GridSeed_Packet/%d", gridseed->device_id);
RenameThread(threadname);
applog(LOG_INFO, "GridSeed: packet thread running, %s", threadname);
cgtime(&ts_packet); // initialize
while(likely(!gridseed->shutdown)) {
cgtime(&ts_now);
if (info->mode == MODE_SCRYPT_DUAL &&
tdiff(&ts_now, &ts_packet) > (120 + 60 * no_packets)) {
if (no_packets > 5) {
applog(LOG_ERR, "%s%d: Proxy not responding, shutting down",
gridseed->drv->name, gridseed->device_id);
gridseed->shutdown = true;
break;
} else {
applog(LOG_NOTICE, "%s%d: No data from proxy, sending ping",
gridseed->drv->name, gridseed->device_id);
gridseed_send_ping_packet(info, info->toaddr);
}
no_packets++;
} else if (info->mode == MODE_SHA256_DUAL &&
tdiff(&ts_now, &ts_packet) > 130) {
info->mode = MODE_SHA256;
mutex_lock(&info->qlock);
gc3355_send_cmds(gridseed, str_ltc_reset);
cgsleep_ms(50);
mutex_unlock(&info->qlock);
}
tv_timeout.tv_sec = 2;
tv_timeout.tv_usec = 0;
FD_ZERO(&rdfs);
FD_SET(sock, &rdfs);
n = select(sock+1, &rdfs, NULL, NULL, &tv_timeout);
if (n == 0)
continue;
if (n < 0) {
if (errno == EINTR)
continue;
applog(LOG_ERR, "Error calling select: %s", strerror(errno));
gridseed->shutdown = true;
break;
}
addrlen = sizeof(fromaddr);
n = recvfrom(sock, &packet, sizeof(packet), 0, (struct sockaddr *)&fromaddr, (socklen_t *)&addrlen);
if (n != sizeof(packet))
continue;
if (n < 0) {
if (errno == EINTR)
continue;
applog(LOG_ERR, "Error calling recvfrom: %s", strerror(errno));
gridseed->shutdown = true;
break;
}
cgtime(&ts_packet);
switch (packet.type) {
case PACKET_PING:
if (!SHA256_MODE(info->mode))
break;
gridseed_send_info_packet(info, fromaddr);
applog(LOG_INFO, "Received ping packet");
break;
case PACKET_INFO:
if (!SCRYPT_MODE(info->mode))
break;
gridseed_recv_info_packet(gridseed, info, packet, fromaddr);
applog(LOG_INFO, "Received info packet");
break;
case PACKET_WORK:
if (!SHA256_MODE(info->mode))
break;
info->mode = MODE_SHA256_DUAL;
gridseed_recv_work_packet(gridseed, info, packet, fromaddr);
applog(LOG_INFO, "Received work packet");
break;
case PACKET_NONCE:
if (!SCRYPT_MODE(info->mode))
break;
gridseed_recv_nonce_packet(gridseed, info, packet);
applog(LOG_INFO, "Received nonce packet");
break;
default:
applog(LOG_ERR, "Received unknown packet");
break;
}
}
return NULL;
}
/*---------------------------------------------------------------------------------------*/
static void _transfer(struct cgpu_info *gridseed, uint8_t request_type, uint8_t bRequest,
uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, enum usb_cmds cmd)
{
int err;
err = usb_transfer_data(gridseed, request_type, bRequest, wValue, wIndex, data, siz, cmd);
cgsleep_ms(GRIDSEED_COMMAND_DELAY);
applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
gridseed->drv->name, gridseed->cgminer_id,
usb_cmdname(cmd), err);
}
static int gc3355_write(struct cgpu_info *gridseed, unsigned char *data, int size)
{
int err, wrote;
#if 1
if (!opt_quiet && opt_debug) {
int i;
#ifndef WIN32
fprintf(stderr, "[1;33m >>> %d : [0m", size);
#else
set_text_color(FOREGROUND_RED|FOREGROUND_GREEN);
fprintf(stderr, " >>> %d : ", size);
set_text_color(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
#endif
for(i=0; i<size; i++) {
fprintf(stderr, "%02x", data[i]);
if (i==3)
fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
#endif
err = usb_write(gridseed, (char *)data, size, &wrote, C_SENDWORK);
if (err != LIBUSB_SUCCESS || wrote != size)
return -1;
cgsleep_ms(GRIDSEED_COMMAND_DELAY);
return 0;
}
#define gc3355_get_data(gridseed, buf, size) gc3355_read(gridseed, buf, size, NULL)
static int gc3355_read(struct cgpu_info *gridseed, unsigned char *buf, int size, int *amount)
{
int err = 0, offset = 0, end = 0;
while (offset == 0 && likely(!gridseed->shutdown)) {
err = usb_read_once_timeout(gridseed, (char *)buf + offset, size - offset, &end, 2000, C_GETRESULTS);
if (err && err != LIBUSB_ERROR_TIMEOUT)
break;
offset += end;
}
if (amount != NULL)
*amount = offset;
if (!opt_quiet && opt_debug) {
int i;
#ifndef WIN32
fprintf(stderr, "[1;31m <<< %d : [0m", offset);
#else
set_text_color(FOREGROUND_RED);
fprintf(stderr, " <<< %d : ", offset);
set_text_color(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
#endif
for(i = 0; i < offset; i++) {
fprintf(stderr, "%02x", buf[i]);
if ((i+1) % 4 == 0)
fprintf(stderr, " ");
}
fprintf(stderr, "\n");
}
if (amount == NULL)
return !(size == offset);
else
return err;
}
static void gc3355_send_cmds(struct cgpu_info *gridseed, const char *cmds[])
{
unsigned char ob[512];
int i;
for(i=0; cmds[i] != NULL; i++) {
hex2bin(ob, cmds[i], sizeof(ob));
gc3355_write(gridseed, ob, strlen(cmds[i])/2);
}
}
static void gc3355_send_cmds_bin(struct cgpu_info *gridseed, const char *cmds[], int size)
{
int i;
for(i=0; cmds[i] != NULL; i++) {
gc3355_write(gridseed, (unsigned char *)cmds[i], size);
}
}
static bool gc3355_read_register(struct cgpu_info *gridseed, uint32_t reg_addr,
uint32_t *reg_value) {
GRIDSEED_INFO *info = (GRIDSEED_INFO*)(gridseed->device_data);
unsigned char cmd[16] = "\x55\xaa\xc0\x01";
uint32_t reg_len = 4;
unsigned char buf[4];
if (info->fw_version != 0x01140113) {
applog(LOG_ERR, "Can't read registers; incompatible firmware %08X on %s%d",
info->fw_version, gridseed->drv->name, gridseed->device_id);
return false;
}
*(uint32_t *)(cmd + 4) = htole32(reg_addr);
*(uint32_t *)(cmd + 8) = htole32(reg_len);
*(uint32_t *)(cmd + 12) = htole32(reg_len);
if (gc3355_write(gridseed, cmd, sizeof(cmd))) {
applog(LOG_DEBUG, "Failed to write data to %s%d", gridseed->drv->name, gridseed->device_id);
return false;
}
if (gc3355_get_data(gridseed, buf, 4)) {
applog(LOG_DEBUG, "No response from %s%d", gridseed->drv->name, gridseed->device_id);
return false;
}
*reg_value = le32toh(*(uint32_t *)buf);
return true;
}
static bool gc3355_write_register(struct cgpu_info *gridseed, uint32_t reg_addr,
uint32_t reg_value) {
GRIDSEED_INFO *info = (GRIDSEED_INFO*)(gridseed->device_data);
unsigned char cmd[16] = "\x55\xaa\xc0\x02";
uint32_t reg_len = 4;
unsigned char buf[4];
if (info->fw_version != 0x01140113) {
applog(LOG_ERR, "Can't write registers; incompatible firmware %08X on %s%d",
info->fw_version, gridseed->drv->name, gridseed->device_id);
return false;
}
*(uint32_t *)(cmd + 4) = htole32(reg_addr);
*(uint32_t *)(cmd + 8) = htole32(reg_value);
*(uint32_t *)(cmd + 12) = htole32(reg_len);
if (gc3355_write(gridseed, cmd, sizeof(cmd)) != 0) {
applog(LOG_DEBUG, "Failed to write data to %s%d", gridseed->drv->name, gridseed->device_id);
return false;
}
if (gc3355_get_data(gridseed, buf, 4)) {
applog(LOG_DEBUG, "No response from %s%d", gridseed->drv->name, gridseed->device_id);
return false;
}
return true;
}
static void gc3355_switch_leds(struct cgpu_info *gridseed) {
uint32_t reg_value;
// Set GPIOB pins 0 and 1 as general purpose output, open-drain, 50 MHz max
if (!gc3355_read_register(gridseed, GRIDSEED_GPIOB_BASE + GRIDSEED_CRL_OFFSET, ®_value)) {
applog(LOG_DEBUG, "Failed to read GPIOA CRL register from %i", gridseed->device_id);
return;
}
reg_value = (reg_value & 0xffffff00) | 0x00000077;
if (!gc3355_write_register(gridseed, GRIDSEED_GPIOB_BASE + GRIDSEED_CRL_OFFSET, reg_value)) {
applog(LOG_DEBUG, "Failed to write GPIOA CRL register from %i", gridseed->device_id);
return;
}
applog(LOG_NOTICE, "%s%d: Turned off GC3355 LEDs",
gridseed->drv->name, gridseed->device_id);
}
static void gc3355_switch_voltage(struct cgpu_info *gridseed) {
uint32_t reg_value;
// Put GPIOA pin 5 into general function, 50 MHz output.
if (!gc3355_read_register(gridseed, GRIDSEED_GPIOA_BASE + GRIDSEED_CRL_OFFSET, ®_value)) {
applog(LOG_DEBUG, "Failed to read GPIOA CRL register from %s%d", gridseed->drv->name, gridseed->device_id);
return;
}
reg_value = (reg_value & 0xff0fffff) | 0x00300000;
if (!gc3355_write_register(gridseed, GRIDSEED_GPIOA_BASE + GRIDSEED_CRL_OFFSET, reg_value)) {
applog(LOG_DEBUG, "Failed to write GPIOA CRL register from %s%d", gridseed->drv->name, gridseed->device_id);
return;
}
// Set GPIOA pin 5 high.
if (!gc3355_read_register(gridseed, GRIDSEED_GPIOA_BASE + GRIDSEED_ODR_OFFSET, ®_value)) {
applog(LOG_DEBUG, "Failed to read GPIOA ODR register from %s%d", gridseed->drv->name, gridseed->device_id);
return;
}
reg_value |= 0x00000020;
if (!gc3355_write_register(gridseed, GRIDSEED_GPIOA_BASE + GRIDSEED_ODR_OFFSET, reg_value)) {
applog(LOG_DEBUG, "Failed to write GPIOA ODR register from %s%d", gridseed->drv->name, gridseed->device_id);
return;
}
applog(LOG_NOTICE, "%s%d: Switched GC3355 voltage to alternate voltage",
gridseed->drv->name, gridseed->device_id);
}
static void gc3355_set_init_nonce(struct cgpu_info *gridseed)
{
GRIDSEED_INFO *info;
int i;
char **cmds, *p;
uint32_t nonce, step;
info = (GRIDSEED_INFO*)(gridseed->device_data);
cmds = calloc(sizeof(char*)*(info->chips+1), 1);
if (unlikely(!cmds))
quit(1, "Failed to calloc init nonce commands data array");
step = 0xffffffff / info->chips;
for(i=0; i<info->chips; i++) {
p = calloc(8, 1);
if (unlikely(!p))
quit(1, "Failed to calloc init nonce commands data");
memcpy(p, "\x55\xaa\x00\x00", 4);
p[2] = i;
nonce = htole32(step*i);
memcpy(p+4, &nonce, sizeof(nonce));
cmds[i] = p;
}
cmds[i] = NULL;
gc3355_send_cmds_bin(gridseed, (const char **)cmds, 8);
for(i=0; i<info->chips; i++)
free(cmds[i]);
free(cmds);
}
static void gc3355_enable_btc_cores(struct cgpu_info *gridseed, GRIDSEED_INFO *info)
{
unsigned char cmd[24], c1, c2;
uint16_t mask;
int i;
mask = 0x00;
for(i=0; i<info->btcore; i++)
mask = mask << 1 | 0x01;
if (mask == 0)
return;
c1 = mask & 0x00ff;
c2 = mask >> 8;
memset(cmd, 0, sizeof(cmd));
memcpy(cmd, "\x55\xAA\xEF\x02", 4);
for(i=4; i<24; i++) {
cmd[i] = ((i%2)==0) ? c1 : c2;
gc3355_write(gridseed, cmd, sizeof(cmd));
}
}
static void gc3355_set_core_freq(struct cgpu_info *gridseed)
{
GRIDSEED_INFO *info = (GRIDSEED_INFO*)(gridseed->device_data);
gc3355_write(gridseed, info->cmd_freq, sizeof(info->cmd_freq));
if (SHA256_MODE(info->mode)) {
gc3355_write(gridseed, info->cmd_btc_baud, sizeof(info->cmd_btc_baud));
}
applog(LOG_NOTICE, "%s%d: Set GC3355 core frequency to %d MHz",
gridseed->drv->name, gridseed->device_id, info->freq);
}
static void gc3355_init(struct cgpu_info *gridseed, GRIDSEED_INFO *info)
{
char buf[512];
int amount;
applog(LOG_NOTICE, "%s%d: System reseting", gridseed->drv->name, gridseed->device_id);
gc3355_send_cmds(gridseed, str_reset);
cgsleep_ms(200);
usb_buffer_clear(gridseed);
usb_read_timeout(gridseed, buf, sizeof(buf), &amount, 10, C_GETRESULTS);
gc3355_send_cmds(gridseed, str_init);
gc3355_send_cmds(gridseed, str_ltc_reset);
gc3355_set_core_freq(gridseed);
if (SHA256_MODE(info->mode)) {
gc3355_set_init_nonce(gridseed);
gc3355_enable_btc_cores(gridseed, info);
if (info->usefifo == 0)
gc3355_send_cmds(gridseed, str_nofifo);
}
if (info->voltage)
gc3355_switch_voltage(gridseed);
if (info->led)
gc3355_switch_leds(gridseed);
}
static void set_freq_cmd(GRIDSEED_INFO *info, int pll_r, int pll_f, int pll_od)
{
if (pll_r == 0 && pll_f == 0 && pll_od == 0) {
// Support frequency increments of 12.5 MHz
// Non-integer frequencies must be specified rounded up
// With these values the minimum we can set is 12.5 and the max 1600
pll_r = 1;
pll_f = 2 * info->freq / GRIDSEED_F_IN - 1;
pll_f = MAX(0, MIN(127, pll_f));
}
double f_ref = GRIDSEED_F_IN / (pll_r + 1.);
double f_vco = f_ref * (pll_f + 1.);
double f_out = f_vco / (1 << pll_od);
int pll_bs = (f_out >= 500.) ? 1 : 0;
int cfg_pm = 1, pll_clk_gate = 1;
uint32_t cmdf = (cfg_pm << 0) | (pll_clk_gate << 2) | (pll_r << 16) |
(pll_f << 21) | (pll_od << 28) | (pll_bs << 31);
info->freq = (int)ceil(f_out);
memcpy(info->cmd_freq, "\x55\xaa\xef\x00", 4);
//*(uint32_t *)(info->cmd_freq + 4) = htole32(cmdf);
cmdf = htole32(cmdf);
memcpy(info->cmd_freq + 4, &cmdf, 4);
uint32_t cmdb = 0x40000000 | ((uint32_t)round(f_out * 1000000. / info->baud) & 0xffff);
cmdb = htole32(cmdb);
memcpy(info->cmd_btc_baud, "\x55\xaa\x0f\xff", 4);
memcpy(info->cmd_btc_baud + 4, &cmdb, 4);
}
static bool get_options(GRIDSEED_INFO *info, char *options)
{
char *ss, *p, *end, *comma, *eq;
int tmp, pll_r = 0, pll_f = 0, pll_od = 0;
if (options == NULL)
return false;
applog(LOG_NOTICE, "GridSeed options: '%s'", options);
ss = strdup(options);
p = ss;
end = p + strlen(p);
another:
comma = strchr(p, ',');
if (comma != NULL)
*comma = '\0';
eq = strchr(p, '=');
if (eq == NULL)
goto next;
*eq = '\0';
tmp = atoi(eq+1);
if (strcasecmp(p, "baud")==0) {
info->baud = (tmp != 0) ? tmp : info->baud;
}
else if (strcasecmp(p, "freq")==0) {
info->freq = tmp;
}
else if (strcasecmp(p, "pll_r")==0) {
pll_r = (tmp != 0) ? tmp : pll_r;
pll_r = MAX(0, MIN(31, pll_r));
}
else if (strcasecmp(p, "pll_f")==0) {
pll_f = (tmp != 0) ? tmp : pll_f;
pll_f = MAX(0, MIN(127, pll_f));
}
else if (strcasecmp(p, "pll_od")==0) {
pll_od = (tmp != 0) ? tmp : pll_od;
pll_od = MAX(0, MIN(4, pll_od));
}
else if (strcasecmp(p, "chips")==0) {
info->chips = (tmp != 0) ? tmp : info->chips;
info->chips = MAX(0, MIN(GRIDSEED_MAX_CHIPS, info->chips));
}
else if (strcasecmp(p, "modules")==0) {
info->modules = (tmp != 0) ? tmp : info->modules;
}
else if (strcasecmp(p, "usefifo")==0) {
info->usefifo = tmp;
}
else if (strcasecmp(p, "btc")==0) {
info->btcore = tmp;
}
else if (strcasecmp(p, "voltage")==0) {
info->voltage = (tmp != 0) ? tmp : info->voltage;
}
else if (strcasecmp(p, "led_off")==0) {
info->led = (tmp != 0) ? tmp : info->led;
}
else if (strcasecmp(p, "per_chip_stats")==0) {
info->per_chip_stats = (tmp != 0) ? tmp : info->per_chip_stats;
}
else if (strcasecmp(p, "start_port")==0) {
info->ltc_port = tmp;
}
next:
if (comma != NULL) {
p = comma + 1;
if (p < end)
goto another;
}
free(ss);
set_freq_cmd(info, pll_r, pll_f, pll_od);
return true;
}
static bool get_freq(GRIDSEED_INFO *info, char *options)
{
char *ss, *p, *end, *comma, *eq;
int tmp;
if (options == NULL)
return false;
applog(LOG_NOTICE, "GridSeed freq options: '%s'", options);
ss = strdup(options);
p = ss;
end = p + strlen(p);
another:
comma = strchr(p, ',');
if (comma != NULL)
*comma = '\0';
eq = strchr(p, '=');
if (eq == NULL)
goto next;
*eq = '\0';
tmp = atoi(eq+1);
if (strcasecmp(p, info->serial)==0) {
info->freq = tmp;
set_freq_cmd(info, 0, 0, 0);
if (info->freq == tmp)
applog(LOG_INFO, "%s unique frequency: %i MHz", p, info->freq);
else
applog(LOG_NOTICE, "%s unique frequency: requested %i MHz, using instead %i MHz", p, tmp, info->freq);
}
next:
if (comma != NULL) {
p = comma + 1;
if (p < end)
goto another;
}
free(ss);
return true;
}
static bool get_override(GRIDSEED_INFO *info, char *options)
{
char *ss, *p, *colon, *semi;
bool ret = false;
if (options == NULL)
return false;
ss = strdup(options);
p = ss;
do {
semi = strchr(p, ';');
if (semi != NULL)
*semi = '\0';
colon = strchr(p, ':');
if (colon == NULL)
continue;
*colon = '\0';
if (strcasecmp(p, info->serial) == 0) {
ret = get_options(info, colon + 1);
break;
}
} while (semi != NULL && (p = semi + 1));
free(ss);
return ret;
}
static int gridseed_cp210x_init(struct cgpu_info *gridseed, int interface)
{
// Enable the UART
transfer(gridseed, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE, CP210X_VALUE_UART_ENABLE,
interface, C_ENABLE_UART);
if (gridseed->usbinfo.nodev)
return -1;
// Set data control
transfer(gridseed, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
interface, C_SETDATA);
if (gridseed->usbinfo.nodev)
return -1;
// Set the baud
uint32_t data = CP210X_DATA_BAUD;
_transfer(gridseed, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
interface, &data, sizeof(data), C_SETBAUD);
return 0;
}
static int gridseed_ftdi_init(struct cgpu_info *gridseed, int interface)
{
int err;
// Reset
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_RESET, interface, C_RESET);
applog(LOG_DEBUG, "%s%i: reset got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
// Set latency
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY,
GRIDSEED_LATENCY, interface, C_LATENCY);
applog(LOG_DEBUG, "%s%i: latency got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
// Set data
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
FTDI_VALUE_DATA_AVA, interface, C_SETDATA);
applog(LOG_DEBUG, "%s%i: data got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
// Set the baud
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_AVA,
(FTDI_INDEX_BAUD_AVA & 0xff00) | interface,
C_SETBAUD);
applog(LOG_DEBUG, "%s%i: setbaud got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
// Set Modem Control
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
FTDI_VALUE_MODEM, interface, C_SETMODEM);
applog(LOG_DEBUG, "%s%i: setmodemctrl got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
// Set Flow Control
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
FTDI_VALUE_FLOW, interface, C_SETFLOW);
applog(LOG_DEBUG, "%s%i: setflowctrl got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
/* Avalon repeats the following */
// Set Modem Control
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
FTDI_VALUE_MODEM, interface, C_SETMODEM);
applog(LOG_DEBUG, "%s%i: setmodemctrl 2 got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
// Set Flow Control
err = usb_transfer(gridseed, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
FTDI_VALUE_FLOW, interface, C_SETFLOW);
applog(LOG_DEBUG, "%s%i: setflowctrl 2 got err %d",
gridseed->drv->name, gridseed->device_id, err);
if (gridseed->usbinfo.nodev)
return -1;
return 0;
}
static int gridseed_pl2303_init(struct cgpu_info *gridseed, int interface)
{
// Set Data Control
transfer(gridseed, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
interface, C_SETDATA);
if (gridseed->usbinfo.nodev)
return -1;