gc3355.c 12 KB

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