gc3355.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright 2014 Nate Woolls
  3. * Copyright 2014 Dualminer Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "gc3355.h"
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include "miner.h"
  14. #include "driver-icarus.h"
  15. #include "logging.h"
  16. #include "lowl-vcom.h"
  17. #ifndef WIN32
  18. #include <sys/ioctl.h>
  19. #else
  20. #include <io.h>
  21. #endif
  22. #define DEFAULT_DELAY_TIME 2000
  23. #define HUBFANS_0_9V_sha2 "60"
  24. #define HUBFANS_1_2V_sha2 "0"
  25. #define DEFAULT_0_9V_sha2 "60"
  26. #define DEFAULT_1_2V_sha2 "0"
  27. static
  28. const char *sha2_gating[] =
  29. {
  30. "55AAEF0200000000",
  31. "55AAEF0300000000",
  32. "55AAEF0400000000",
  33. "55AAEF0500000000",
  34. "55AAEF0600000000",
  35. "",
  36. };
  37. static
  38. const char *sha2_single_open[] =
  39. {
  40. "55AAEF0200000001",
  41. "55AAEF0200000003",
  42. "55AAEF0200000007",
  43. "55AAEF020000000F",
  44. "55AAEF020000001F",
  45. "55AAEF020000003F",
  46. "55AAEF020000007F",
  47. "55AAEF02000000FF",
  48. "55AAEF02000001FF",
  49. "55AAEF02000003FF",
  50. "55AAEF02000007FF",
  51. "55AAEF0200000FFF",
  52. "55AAEF0200001FFF",
  53. "55AAEF0200003FFF",
  54. "55AAEF0200007FFF",
  55. "55AAEF020000FFFF",
  56. "55AAEF020001FFFF",
  57. "55AAEF020003FFFF",
  58. "55AAEF020007FFFF",
  59. "55AAEF02000FFFFF",
  60. "55AAEF02001FFFFF",
  61. "55AAEF02003FFFFF",
  62. "55AAEF02007FFFFF",
  63. "55AAEF0200FFFFFF",
  64. "55AAEF0201FFFFFF",
  65. "55AAEF0203FFFFFF",
  66. "55AAEF0207FFFFFF",
  67. "55AAEF020FFFFFFF",
  68. "55AAEF021FFFFFFF",
  69. "55AAEF023FFFFFFF",
  70. "55AAEF027FFFFFFF",
  71. "55AAEF02FFFFFFFF",
  72. "55AAEF0300000001",
  73. "55AAEF0300000003",
  74. "55AAEF0300000007",
  75. "55AAEF030000000F",
  76. "55AAEF030000001F",
  77. "55AAEF030000003F",
  78. "55AAEF030000007F",
  79. "55AAEF03000000FF",
  80. "55AAEF03000001FF",
  81. "55AAEF03000003FF",
  82. "55AAEF03000007FF",
  83. "55AAEF0300000FFF",
  84. "55AAEF0300001FFF",
  85. "55AAEF0300003FFF",
  86. "55AAEF0300007FFF",
  87. "55AAEF030000FFFF",
  88. "55AAEF030001FFFF",
  89. "55AAEF030003FFFF",
  90. "55AAEF030007FFFF",
  91. "55AAEF03000FFFFF",
  92. "55AAEF03001FFFFF",
  93. "55AAEF03003FFFFF",
  94. "55AAEF03007FFFFF",
  95. "55AAEF0300FFFFFF",
  96. "55AAEF0301FFFFFF",
  97. "55AAEF0303FFFFFF",
  98. "55AAEF0307FFFFFF",
  99. "55AAEF030FFFFFFF",
  100. "55AAEF031FFFFFFF",
  101. "55AAEF033FFFFFFF",
  102. "55AAEF037FFFFFFF",
  103. "55AAEF03FFFFFFFF",
  104. "55AAEF0400000001",
  105. "55AAEF0400000003",
  106. "55AAEF0400000007",
  107. "55AAEF040000000F",
  108. "55AAEF040000001F",
  109. "55AAEF040000003F",
  110. "55AAEF040000007F",
  111. "55AAEF04000000FF",
  112. "55AAEF04000001FF",
  113. "55AAEF04000003FF",
  114. "55AAEF04000007FF",
  115. "55AAEF0400000FFF",
  116. "55AAEF0400001FFF",
  117. "55AAEF0400003FFF",
  118. "55AAEF0400007FFF",
  119. "55AAEF040000FFFF",
  120. "55AAEF040001FFFF",
  121. "55AAEF040003FFFF",
  122. "55AAEF040007FFFF",
  123. "55AAEF04000FFFFF",
  124. "55AAEF04001FFFFF",
  125. "55AAEF04003FFFFF",
  126. "55AAEF04007FFFFF",
  127. "55AAEF0400FFFFFF",
  128. "55AAEF0401FFFFFF",
  129. "55AAEF0403FFFFFF",
  130. "55AAEF0407FFFFFF",
  131. "55AAEF040FFFFFFF",
  132. "55AAEF041FFFFFFF",
  133. "55AAEF043FFFFFFF",
  134. "55AAEF047FFFFFFF",
  135. "55AAEF04FFFFFFFF",
  136. "55AAEF0500000001",
  137. "55AAEF0500000003",
  138. "55AAEF0500000007",
  139. "55AAEF050000000F",
  140. "55AAEF050000001F",
  141. "55AAEF050000003F",
  142. "55AAEF050000007F",
  143. "55AAEF05000000FF",
  144. "55AAEF05000001FF",
  145. "55AAEF05000003FF",
  146. "55AAEF05000007FF",
  147. "55AAEF0500000FFF",
  148. "55AAEF0500001FFF",
  149. "55AAEF0500003FFF",
  150. "55AAEF0500007FFF",
  151. "55AAEF050000FFFF",
  152. "55AAEF050001FFFF",
  153. "55AAEF050003FFFF",
  154. "55AAEF050007FFFF",
  155. "55AAEF05000FFFFF",
  156. "55AAEF05001FFFFF",
  157. "55AAEF05003FFFFF",
  158. "55AAEF05007FFFFF",
  159. "55AAEF0500FFFFFF",
  160. "55AAEF0501FFFFFF",
  161. "55AAEF0503FFFFFF",
  162. "55AAEF0507FFFFFF",
  163. "55AAEF050FFFFFFF",
  164. "55AAEF051FFFFFFF",
  165. "55AAEF053FFFFFFF",
  166. "55AAEF057FFFFFFF",
  167. "55AAEF05FFFFFFFF",
  168. "55AAEF0600000001",
  169. "55AAEF0600000003",
  170. "55AAEF0600000007",
  171. "55AAEF060000000F",
  172. "55AAEF060000001F",
  173. "55AAEF060000003F",
  174. "55AAEF060000007F",
  175. "55AAEF06000000FF",
  176. "55AAEF06000001FF",
  177. "55AAEF06000003FF",
  178. "55AAEF06000007FF",
  179. "55AAEF0600000FFF",
  180. "55AAEF0600001FFF",
  181. "55AAEF0600003FFF",
  182. "55AAEF0600007FFF",
  183. "55AAEF060000FFFF",
  184. "55AAEF060001FFFF",
  185. "55AAEF060003FFFF",
  186. "55AAEF060007FFFF",
  187. "55AAEF06000FFFFF",
  188. "55AAEF06001FFFFF",
  189. "55AAEF06003FFFFF",
  190. "55AAEF06007FFFFF",
  191. "55AAEF0600FFFFFF",
  192. "55AAEF0601FFFFFF",
  193. "55AAEF0603FFFFFF",
  194. "55AAEF0607FFFFFF",
  195. "55AAEF060FFFFFFF",
  196. "55AAEF061FFFFFFF",
  197. "55AAEF063FFFFFFF",
  198. "55AAEF067FFFFFFF",
  199. "55AAEF06FFFFFFFF",
  200. "",
  201. };
  202. static
  203. const char *scrypt_only_init[] =
  204. {
  205. "55AAEF0200000000",
  206. "55AAEF0300000000",
  207. "55AAEF0400000000",
  208. "55AAEF0500000000",
  209. "55AAEF0600000000",
  210. "55AAEF3040000000",
  211. "55AA1F2810000000",
  212. "55AA1F2813000000",
  213. "",
  214. };
  215. char *opt_dualminer_sha2_gating = NULL;
  216. int opt_pll_freq = 0; // default is set in gc3355_pll_freq_init2
  217. int opt_sha2_number = 160;
  218. bool opt_hubfans = false;
  219. bool opt_dual_mode = false;
  220. void gc3355_dual_reset(int fd)
  221. {
  222. set_serial_dtr(fd, BGV_HIGH);
  223. cgsleep_ms(1000);
  224. set_serial_dtr(fd, BGV_LOW);
  225. }
  226. static
  227. void gc3355_set_register(uint8_t * const buf, const uint8_t clusaddr, const uint8_t chipaddr, const uint8_t regaddr, const uint32_t val)
  228. {
  229. buf[0] = 0x55;
  230. buf[1] = 0xaa;
  231. buf[2] = (clusaddr << 4) | chipaddr;
  232. buf[3] = regaddr;
  233. buf[4] = (val >> 0) & 0xff;
  234. buf[5] = (val >> 8) & 0xff;
  235. buf[6] = (val >> 0x10) & 0xff;
  236. buf[7] = (val >> 0x18) & 0xff;
  237. }
  238. static
  239. void gc3355_config_cpm(uint8_t * const buf, const uint8_t chipaddr, const float mhz)
  240. {
  241. // See https://github.com/gridseed/gc3355-doc/blob/master/GC3355_Register_Spec.pdf
  242. const uint8_t pll_bypass = 1;
  243. const uint8_t pll_bandselect = 0;
  244. const uint8_t pll_outdiv = 0;
  245. uint8_t freq_div, freq_mult, last_freq_mult; // mhz = (25 / freq_div * freq_mult)
  246. float actual_mhz, last_actual_mhz = -1;
  247. for (freq_div = 1; freq_div <= 32; ++freq_div)
  248. {
  249. freq_mult = mhz * freq_div / 25;
  250. if (freq_mult > 0x80)
  251. freq_mult = 0x80;
  252. actual_mhz = 25. / freq_div * freq_mult;
  253. if (last_actual_mhz > actual_mhz)
  254. {
  255. --freq_div;
  256. freq_mult = last_freq_mult;
  257. if (opt_debug)
  258. actual_mhz = 25. / freq_div * freq_mult;
  259. break;
  260. }
  261. if (actual_mhz > mhz - .5)
  262. break;
  263. last_actual_mhz = actual_mhz;
  264. last_freq_mult = freq_mult;
  265. }
  266. const uint8_t pll_F = freq_mult - 1;
  267. const uint8_t pll_R = freq_div - 1;
  268. const uint8_t core_clk_out1_diven = 0;
  269. const uint8_t core_clk_sel1 = 0;
  270. const uint8_t core_clk_sel0 = 0;
  271. const uint8_t pll_clk_gate = 0;
  272. const uint8_t pll_recfg = 1;
  273. const uint8_t cfg_cpm = 1;
  274. const uint32_t cfg = (pll_bypass << 31) | (pll_bandselect << 30) | (pll_outdiv << 28) | (pll_F << 21) | (pll_R << 16) | (core_clk_out1_diven << 6) | (core_clk_sel1 << 5) | (core_clk_sel0 << 4) | (pll_clk_gate << 3) | (pll_recfg << 2) | (cfg_cpm << 0);
  275. gc3355_set_register(buf, 0xe, chipaddr, 0, cfg);
  276. }
  277. // NOTE: MHz must match CPM config
  278. static
  279. void gc3355_config_sha256d(uint8_t * const buf, const uint8_t chipaddr, const float mhz, const uint32_t baud)
  280. {
  281. // See https://github.com/gridseed/gc3355-doc/blob/master/GC3355_Register_Spec.pdf
  282. const uint8_t force_start = 1;
  283. const uint8_t uart_enable = 1;
  284. const uint8_t uart_debug = 0;
  285. const uint8_t byte_order = 0;
  286. const uint16_t rpt_cycle = (mhz * 1000000 / baud);
  287. const uint32_t cfg = (force_start << 31) | (uart_enable << 30) | (uart_debug << 29) | (byte_order << 28) | rpt_cycle;
  288. gc3355_set_register(buf, 0, chipaddr, 0xff, cfg);
  289. }
  290. static
  291. int gc3355_write(const int fd, const void * const buf, const size_t bufsz)
  292. {
  293. const int rv = icarus_write(fd, buf, bufsz);
  294. usleep(DEFAULT_DELAY_TIME);
  295. return rv;
  296. }
  297. static
  298. void gc3355_send_cmds(int fd, const char *cmds[])
  299. {
  300. int i = 0;
  301. unsigned char ob_bin[32];
  302. for(i = 0 ;; i++)
  303. {
  304. memset(ob_bin, 0, sizeof(ob_bin));
  305. if (cmds[i][0] == 0)
  306. break;
  307. hex2bin(ob_bin, cmds[i], strlen(cmds[i]) / 2);
  308. icarus_write(fd, ob_bin, 8);
  309. usleep(DEFAULT_DELAY_TIME);
  310. }
  311. }
  312. void gc3355_opt_scrypt_init(int fd)
  313. {
  314. const char *initscrypt_ob[] =
  315. {
  316. "55AA1F2810000000",
  317. "55AA1F2813000000",
  318. ""
  319. };
  320. gc3355_send_cmds(fd, initscrypt_ob);
  321. }
  322. static
  323. void gc3355_pll_freq_init2(int fd, int pll_freq)
  324. {
  325. const uint8_t chipaddr = 0xf;
  326. const uint32_t baud = 115200; // FIXME: Make this configurable
  327. uint8_t buf[8];
  328. if (!pll_freq)
  329. {
  330. if (gc3355_get_cts_status(fd) == 1)
  331. //1.2v - Scrypt mode
  332. pll_freq = 850;
  333. else
  334. //0.9v - Scrypt + SHA mode
  335. pll_freq = 550;
  336. }
  337. gc3355_config_cpm(buf, chipaddr, pll_freq);
  338. gc3355_write(fd, buf, sizeof(buf));
  339. gc3355_config_sha256d(buf, chipaddr, pll_freq, baud);
  340. gc3355_write(fd, buf, sizeof(buf));
  341. }
  342. void gc3355_open_sha2_unit(int fd, char *opt_sha2_gating)
  343. {
  344. unsigned char ob_bin[8];
  345. int i;
  346. //---sha2 unit---
  347. char sha2_gating[5][17] =
  348. {
  349. "55AAEF0200000000",
  350. "55AAEF0300000000",
  351. "55AAEF0400000000",
  352. "55AAEF0500000000",
  353. "55AAEF0600000000",
  354. };
  355. union
  356. {
  357. unsigned int i32[5];
  358. unsigned char c8[20] ;
  359. }sha2_group;
  360. int sha2_number=0;
  361. if (opt_sha2_gating== NULL)
  362. sha2_number = 70;
  363. else
  364. {
  365. if (atoi(opt_sha2_gating) <= 160 && atoi(opt_sha2_gating) >= 0)
  366. sha2_number = atoi(opt_sha2_gating);
  367. else
  368. sha2_number = 70;
  369. }
  370. for(i = 0; i < 5; i++)
  371. sha2_group.i32[i] = 0;
  372. for(i = 0; i < sha2_number; i++)
  373. sha2_group.i32[i / 32] += 1 << ( i % 32);
  374. for(i = 0; i < 20; i++)
  375. sprintf(&sha2_gating[i / 4][8 + (i % 4) * 2], "%02x", sha2_group.c8[i]);
  376. //---sha2 unit end---
  377. for(i = 0; i < 5; i++)
  378. {
  379. if (sha2_gating[i][0] == '\0')
  380. break;
  381. hex2bin(ob_bin, sha2_gating[i], sizeof(ob_bin));
  382. icarus_write(fd, ob_bin, 8);
  383. usleep(DEFAULT_DELAY_TIME);
  384. }
  385. opt_sha2_number = sha2_number;
  386. }
  387. static
  388. void gc3355_open_sha2_unit_one_by_one(int fd, char *opt_sha2_gating)
  389. {
  390. int unit_count = 0;
  391. unsigned char ob_bin[8];
  392. int i;
  393. unit_count = atoi(opt_sha2_gating);
  394. if (unit_count < 0)
  395. unit_count = 0;
  396. if (unit_count > 160)
  397. unit_count = 160;
  398. if (unit_count > 0 && unit_count <= 160)
  399. {
  400. for(i = 0; i <= unit_count; i++)
  401. {
  402. hex2bin(ob_bin, sha2_single_open[i], sizeof(ob_bin));
  403. icarus_write(fd, ob_bin, 8);
  404. usleep(DEFAULT_DELAY_TIME * 2);
  405. }
  406. opt_sha2_number = unit_count;
  407. }
  408. else if (unit_count == 0)
  409. gc3355_send_cmds(fd, sha2_gating);
  410. }
  411. void gc3355_opt_scrypt_only_init(int fd)
  412. {
  413. gc3355_send_cmds(fd, scrypt_only_init);
  414. gc3355_pll_freq_init2(fd, opt_pll_freq);
  415. }
  416. void gc3355_open_scrypt_unit(int fd, int status)
  417. {
  418. const char *scrypt_only_ob[] =
  419. {
  420. "55AA1F2810000000",
  421. "",
  422. };
  423. const char *scrypt_ob[] =
  424. {
  425. "55AA1F2814000000",
  426. "",
  427. };
  428. if (status == SCRYPT_UNIT_OPEN)
  429. {
  430. if (opt_dual_mode)
  431. gc3355_opt_scrypt_init(fd);
  432. else
  433. gc3355_opt_scrypt_only_init(fd);
  434. }
  435. else
  436. {
  437. if (opt_dual_mode)
  438. gc3355_send_cmds(fd, scrypt_ob);
  439. else
  440. gc3355_send_cmds(fd, scrypt_only_ob);
  441. }
  442. }
  443. void gc3355_dualminer_init(int fd)
  444. {
  445. const char *init_ob[] =
  446. {
  447. #if 1
  448. "55AAEF0200000000",
  449. "55AAEF0300000000",
  450. "55AAEF0400000000",
  451. "55AAEF0500000000",
  452. "55AAEF0600000000",
  453. #endif
  454. "55AAEF3020000000",
  455. "55AA1F2817000000",
  456. ""
  457. };
  458. const char *initscrypt_ob[] =
  459. {
  460. "55AA1F2814000000",
  461. "55AA1F2817000000",
  462. ""
  463. };
  464. if (opt_scrypt)
  465. gc3355_send_cmds(fd, initscrypt_ob);
  466. else
  467. gc3355_send_cmds(fd, init_ob);
  468. if (!opt_scrypt)
  469. gc3355_pll_freq_init2(fd, opt_pll_freq);
  470. }
  471. void gc3355_init(int fd, char *sha2_unit, bool is_scrypt_only)
  472. {
  473. if (gc3355_get_cts_status(fd) == 1)
  474. {
  475. //1.2v - Scrypt mode
  476. if (opt_scrypt)
  477. {
  478. if (is_scrypt_only)
  479. gc3355_opt_scrypt_only_init(fd);
  480. }
  481. else
  482. {
  483. if (opt_hubfans)
  484. ((sha2_unit == NULL) ? gc3355_open_sha2_unit_one_by_one(fd, HUBFANS_1_2V_sha2) : gc3355_open_sha2_unit_one_by_one(fd, sha2_unit));
  485. else
  486. ((sha2_unit == NULL) ? gc3355_open_sha2_unit_one_by_one(fd, DEFAULT_1_2V_sha2) : gc3355_open_sha2_unit_one_by_one(fd, sha2_unit));
  487. }
  488. }
  489. else
  490. {
  491. //0.9v - Scrypt + SHA mode
  492. if (opt_scrypt)
  493. {
  494. if (is_scrypt_only)
  495. gc3355_opt_scrypt_only_init(fd);
  496. }
  497. else
  498. {
  499. if (opt_hubfans)
  500. ((sha2_unit == NULL) ? gc3355_open_sha2_unit_one_by_one(fd, HUBFANS_0_9V_sha2) : gc3355_open_sha2_unit_one_by_one(fd, sha2_unit));
  501. else
  502. ((sha2_unit == NULL) ? gc3355_open_sha2_unit_one_by_one(fd, DEFAULT_0_9V_sha2) : gc3355_open_sha2_unit_one_by_one(fd, sha2_unit));
  503. }
  504. }
  505. }