gc3355.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. int opt_pll_freq = 0; // default is set in gc3355_set_pll_freq
  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. static
  205. const char *sha2_init_cmd[] =
  206. {
  207. "55AAEF3020000000", // Enable SHA-2
  208. "55AA1F2817000000", // Enable GCP
  209. NULL
  210. };
  211. // called when initializing GridSeed device
  212. // called while initializing DualMiner when mining in scrypt+sha (dual-mode)
  213. static
  214. const char *scrypt_init_cmd[] =
  215. {
  216. "55AA1F2814000000", // Enable Scrypt
  217. "55AA1F2817000000", // Enable GCP
  218. NULL
  219. };
  220. // called before job start by GridSeed when mining scrypt
  221. // called before job start by DualMiner when mining scrypt in scrypt+sha (dual-mode)
  222. static
  223. const char *scrypt_reset_cmd[] =
  224. {
  225. // faster, for start of each job:
  226. "55AA1F2816000000", // Reset Scrypt(?)
  227. "55AA1F2817000000", // Enable GCP(?)
  228. NULL
  229. };
  230. // called while initializing DualMiner when mining scrypt in scrypt-only (not dual-mode)
  231. static
  232. const char *scrypt_only_init_cmd[] =
  233. {
  234. "55AAEF0200000000",
  235. "55AAEF0300000000",
  236. "55AAEF0400000000",
  237. "55AAEF0500000000",
  238. "55AAEF0600000000",
  239. "55AAEF3040000000",
  240. "55AA1F2810000000",
  241. "55AA1F2813000000",
  242. NULL
  243. };
  244. static
  245. const char *gcp_chip_reset_cmd[] =
  246. {
  247. "55AAC000808080800000000001000000", // GCP (GridChip) reset
  248. NULL
  249. };
  250. static
  251. const char *sha2_chip_reset_cmd[] =
  252. {
  253. "55AAC000E0E0E0E00000000001000000", // SHA2 reset
  254. NULL
  255. };
  256. bool opt_dual_mode = false;
  257. void gc3355_reset_dtr(int fd)
  258. {
  259. // set data terminal ready (DTR) status
  260. set_serial_dtr(fd, BGV_HIGH);
  261. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  262. set_serial_dtr(fd, BGV_LOW);
  263. }
  264. static
  265. void gc3355_set_register(uint8_t * const buf, const uint8_t clusaddr, const uint8_t chipaddr, const uint8_t regaddr, const uint32_t val)
  266. {
  267. buf[0] = 0x55;
  268. buf[1] = 0xaa;
  269. buf[2] = (clusaddr << 4) | chipaddr;
  270. buf[3] = regaddr;
  271. buf[4] = (val >> 0) & 0xff;
  272. buf[5] = (val >> 8) & 0xff;
  273. buf[6] = (val >> 0x10) & 0xff;
  274. buf[7] = (val >> 0x18) & 0xff;
  275. }
  276. static
  277. void gc3355_config_cpm(uint8_t * const buf, const uint8_t chipaddr, const float mhz)
  278. {
  279. // See https://github.com/gridseed/gc3355-doc/blob/master/GC3355_Register_Spec.pdf
  280. const uint8_t pll_bypass = 1;
  281. const uint8_t pll_bandselect = 0;
  282. const uint8_t pll_outdiv = 0;
  283. uint8_t freq_div, freq_mult, last_freq_mult; // mhz = (25 / freq_div * freq_mult)
  284. float actual_mhz, last_actual_mhz = -1;
  285. for (freq_div = 1; freq_div <= 32; ++freq_div)
  286. {
  287. freq_mult = mhz * freq_div / 25;
  288. if (freq_mult > 0x80)
  289. freq_mult = 0x80;
  290. actual_mhz = 25. / freq_div * freq_mult;
  291. if (last_actual_mhz > actual_mhz)
  292. {
  293. --freq_div;
  294. freq_mult = last_freq_mult;
  295. if (opt_debug)
  296. actual_mhz = 25. / freq_div * freq_mult;
  297. break;
  298. }
  299. if (actual_mhz > mhz - .5)
  300. break;
  301. last_actual_mhz = actual_mhz;
  302. last_freq_mult = freq_mult;
  303. }
  304. const uint8_t pll_F = freq_mult - 1;
  305. const uint8_t pll_R = freq_div - 1;
  306. const uint8_t core_clk_out1_diven = 0;
  307. const uint8_t core_clk_sel1 = 0;
  308. const uint8_t core_clk_sel0 = 0;
  309. const uint8_t pll_clk_gate = 0;
  310. const uint8_t pll_recfg = 1;
  311. const uint8_t cfg_cpm = 1;
  312. 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);
  313. gc3355_set_register(buf, 0xe, chipaddr, 0, cfg);
  314. }
  315. // NOTE: MHz must match CPM config
  316. static
  317. void gc3355_config_sha256d(uint8_t * const buf, const uint8_t chipaddr, const float mhz, const uint32_t baud)
  318. {
  319. // See https://github.com/gridseed/gc3355-doc/blob/master/GC3355_Register_Spec.pdf
  320. const uint8_t force_start = 1;
  321. const uint8_t uart_enable = 1;
  322. const uint8_t uart_debug = 0;
  323. const uint8_t byte_order = 0;
  324. const uint16_t rpt_cycle = (mhz * 1000000 / baud);
  325. const uint32_t cfg = (force_start << 31) | (uart_enable << 30) | (uart_debug << 29) | (byte_order << 28) | rpt_cycle;
  326. gc3355_set_register(buf, 0, chipaddr, 0xff, cfg);
  327. }
  328. static
  329. int gc3355_write(const int fd, const void * const buf, const size_t bufsz)
  330. {
  331. const int rv = icarus_write(fd, buf, bufsz);
  332. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  333. return rv;
  334. }
  335. static
  336. void gc3355_send_cmds(int fd, const char *cmds[])
  337. {
  338. int i = 0;
  339. unsigned char ob_bin[32];
  340. for(i = 0 ;; i++)
  341. {
  342. memset(ob_bin, 0, sizeof(ob_bin));
  343. if (cmds[i] == NULL)
  344. break;
  345. hex2bin(ob_bin, cmds[i], strlen(cmds[i]) / 2);
  346. icarus_write(fd, ob_bin, 8);
  347. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  348. }
  349. }
  350. void gc3355_scrypt_only_reset(int fd)
  351. {
  352. const char *initscrypt_ob[] =
  353. {
  354. "55AA1F2810000000",
  355. "55AA1F2813000000",
  356. NULL
  357. };
  358. gc3355_send_cmds(fd, initscrypt_ob);
  359. }
  360. static
  361. void gc3355_set_pll_freq(int fd, int pll_freq)
  362. {
  363. const uint8_t chipaddr = 0xf;
  364. const uint32_t baud = 115200; // FIXME: Make this configurable
  365. uint8_t buf[8];
  366. if (!pll_freq)
  367. {
  368. if (gc3355_get_cts_status(fd) == 1)
  369. //1.2v - Scrypt mode
  370. pll_freq = 850;
  371. else
  372. //0.9v - Scrypt + SHA mode
  373. pll_freq = 550;
  374. }
  375. gc3355_config_cpm(buf, chipaddr, pll_freq);
  376. gc3355_write(fd, buf, sizeof(buf));
  377. gc3355_config_sha256d(buf, chipaddr, pll_freq, baud);
  378. gc3355_write(fd, buf, sizeof(buf));
  379. }
  380. static
  381. void gc3355_open_sha2_units(int fd, int sha2_units)
  382. {
  383. int unit_count = 0;
  384. unsigned char ob_bin[8];
  385. int i;
  386. // should be 0 - 160
  387. unit_count = sha2_units < 0 ? 0 : sha2_units > 160 ? 160 : sha2_units;
  388. if (unit_count > 0)
  389. {
  390. for(i = 0; i <= unit_count; i++)
  391. {
  392. hex2bin(ob_bin, sha2_open_cmd[i], sizeof(ob_bin));
  393. gc3355_write(fd, ob_bin, 8);
  394. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  395. }
  396. }
  397. else if (unit_count == 0)
  398. gc3355_send_cmds(fd, sha2_gating_cmd);
  399. }
  400. void gc3355_scrypt_init(int fd)
  401. {
  402. gc3355_send_cmds(fd, scrypt_init_cmd);
  403. }
  404. static
  405. void gc3355_scrypt_only_init(int fd)
  406. {
  407. gc3355_send_cmds(fd, sha2_gating_cmd);
  408. gc3355_send_cmds(fd, scrypt_only_init_cmd);
  409. gc3355_scrypt_only_reset(fd);
  410. }
  411. static
  412. void gc3355_sha2_init(int fd)
  413. {
  414. gc3355_send_cmds(fd, sha2_gating_cmd);
  415. gc3355_send_cmds(fd, sha2_init_cmd);
  416. }
  417. static
  418. void gc3355_reset_chips(int fd)
  419. {
  420. // reset chips
  421. gc3355_send_cmds(fd, gcp_chip_reset_cmd);
  422. gc3355_send_cmds(fd, sha2_chip_reset_cmd);
  423. }
  424. void gc3355_init_usbstick(int fd, int pll_freq, bool scrypt_only, bool detect_only)
  425. {
  426. gc3355_reset_chips(fd);
  427. gc3355_reset_dtr(fd);
  428. // initialize units
  429. if (opt_scrypt && scrypt_only)
  430. gc3355_scrypt_only_init(fd);
  431. else
  432. {
  433. gc3355_sha2_init(fd);
  434. gc3355_scrypt_init(fd);
  435. }
  436. //set freq
  437. gc3355_set_pll_freq(fd, pll_freq);
  438. // zzz
  439. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  440. if (!detect_only)
  441. {
  442. if (!opt_scrypt)
  443. {
  444. // open sha2 units
  445. gc3355_open_sha2_units(fd, opt_sha2_units);
  446. }
  447. // set request to send (RTS) status
  448. set_serial_rts(fd, BGV_HIGH);
  449. }
  450. }
  451. void gc3355_scrypt_reset(int fd)
  452. {
  453. gc3355_send_cmds(fd, scrypt_reset_cmd);
  454. }
  455. void gc3355_scrypt_prepare_work(unsigned char cmd[156], struct work *work)
  456. {
  457. // command header
  458. cmd[0] = 0x55;
  459. cmd[1] = 0xaa;
  460. cmd[2] = 0x1f;
  461. cmd[3] = 0x00;
  462. // task data
  463. memcpy(cmd + 4, work->target, 32);
  464. memcpy(cmd + 36, work->midstate, 32);
  465. memcpy(cmd + 68, work->data, 80);
  466. // nonce_max
  467. cmd[148] = 0xff;
  468. cmd[149] = 0xff;
  469. cmd[150] = 0xff;
  470. cmd[151] = 0xff;
  471. // taskid
  472. int workid = work->id;
  473. memcpy(cmd + 152, &(workid), 4);
  474. }
  475. void gc3355_sha2_prepare_work(unsigned char cmd[52], struct work *work, bool simple)
  476. {
  477. if (simple)
  478. {
  479. // command header
  480. cmd[0] = 0x55;
  481. cmd[1] = 0xaa;
  482. cmd[2] = 0x0f;
  483. cmd[3] = 0x01; // SHA header sig
  484. memcpy(cmd + 4, work->midstate, 32);
  485. memcpy(cmd + 36, work->data + 64, 12);
  486. // taskid
  487. int workid = work->id;
  488. memcpy(cmd + 48, &(workid), 4);
  489. }
  490. else
  491. {
  492. // command header
  493. cmd[0] = 0x55;
  494. cmd[1] = 0xaa;
  495. cmd[2] = 0x0f;
  496. cmd[3] = 0x00; // Scrypt header sig - used by DualMiner in Dual Mode
  497. uint8_t temp_bin[64];
  498. memset(temp_bin, 0, 64);
  499. memcpy(temp_bin, work->midstate, 32);
  500. memcpy(temp_bin + 52, work->data + 64, 12);
  501. memcpy(cmd + 8, work->midstate, 32);
  502. memcpy(cmd + 40, temp_bin + 52, 12);
  503. }
  504. }