gc3355.c 12 KB

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