gc3355.c 12 KB

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