gc3355.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. #define GC3355_CHIP_NAME "gc3355"
  28. #define DEFAULT_ORB_SHA2_CORES 16
  29. // General GC3355 commands
  30. static
  31. const char *firmware_request_cmd[] =
  32. {
  33. "55AAC000909090900000000001000000", // get firmware version of GC3355
  34. NULL
  35. };
  36. static
  37. const char *no_fifo_cmd[] = {
  38. "55AAC000D0D0D0D00000000001000000",
  39. NULL
  40. };
  41. // SHA-2 commands
  42. static
  43. const char *sha2_gating_cmd[] =
  44. {
  45. "55AAEF0200000000", // Chip 1 - power down SHA-2 (unless masked w/PLL)
  46. "55AAEF0300000000", // Chip 2
  47. "55AAEF0400000000", // Chip 3
  48. "55AAEF0500000000", // Chip 4
  49. "55AAEF0600000000", // Chip 5
  50. NULL
  51. };
  52. // maps the above SHA chip gating with SHA-2 units
  53. static
  54. const char *sha2_open_cmd[] =
  55. {
  56. "55AAEF0200000001",
  57. "55AAEF0200000003",
  58. "55AAEF0200000007",
  59. "55AAEF020000000F",
  60. "55AAEF020000001F",
  61. "55AAEF020000003F",
  62. "55AAEF020000007F",
  63. "55AAEF02000000FF",
  64. "55AAEF02000001FF",
  65. "55AAEF02000003FF",
  66. "55AAEF02000007FF",
  67. "55AAEF0200000FFF",
  68. "55AAEF0200001FFF",
  69. "55AAEF0200003FFF",
  70. "55AAEF0200007FFF",
  71. "55AAEF020000FFFF",
  72. "55AAEF020001FFFF",
  73. "55AAEF020003FFFF",
  74. "55AAEF020007FFFF",
  75. "55AAEF02000FFFFF",
  76. "55AAEF02001FFFFF",
  77. "55AAEF02003FFFFF",
  78. "55AAEF02007FFFFF",
  79. "55AAEF0200FFFFFF",
  80. "55AAEF0201FFFFFF",
  81. "55AAEF0203FFFFFF",
  82. "55AAEF0207FFFFFF",
  83. "55AAEF020FFFFFFF",
  84. "55AAEF021FFFFFFF",
  85. "55AAEF023FFFFFFF",
  86. "55AAEF027FFFFFFF",
  87. "55AAEF02FFFFFFFF",
  88. "55AAEF0300000001",
  89. "55AAEF0300000003",
  90. "55AAEF0300000007",
  91. "55AAEF030000000F",
  92. "55AAEF030000001F",
  93. "55AAEF030000003F",
  94. "55AAEF030000007F",
  95. "55AAEF03000000FF",
  96. "55AAEF03000001FF",
  97. "55AAEF03000003FF",
  98. "55AAEF03000007FF",
  99. "55AAEF0300000FFF",
  100. "55AAEF0300001FFF",
  101. "55AAEF0300003FFF",
  102. "55AAEF0300007FFF",
  103. "55AAEF030000FFFF",
  104. "55AAEF030001FFFF",
  105. "55AAEF030003FFFF",
  106. "55AAEF030007FFFF",
  107. "55AAEF03000FFFFF",
  108. "55AAEF03001FFFFF",
  109. "55AAEF03003FFFFF",
  110. "55AAEF03007FFFFF",
  111. "55AAEF0300FFFFFF",
  112. "55AAEF0301FFFFFF",
  113. "55AAEF0303FFFFFF",
  114. "55AAEF0307FFFFFF",
  115. "55AAEF030FFFFFFF",
  116. "55AAEF031FFFFFFF",
  117. "55AAEF033FFFFFFF",
  118. "55AAEF037FFFFFFF",
  119. "55AAEF03FFFFFFFF",
  120. "55AAEF0400000001",
  121. "55AAEF0400000003",
  122. "55AAEF0400000007",
  123. "55AAEF040000000F",
  124. "55AAEF040000001F",
  125. "55AAEF040000003F",
  126. "55AAEF040000007F",
  127. "55AAEF04000000FF",
  128. "55AAEF04000001FF",
  129. "55AAEF04000003FF",
  130. "55AAEF04000007FF",
  131. "55AAEF0400000FFF",
  132. "55AAEF0400001FFF",
  133. "55AAEF0400003FFF",
  134. "55AAEF0400007FFF",
  135. "55AAEF040000FFFF",
  136. "55AAEF040001FFFF",
  137. "55AAEF040003FFFF",
  138. "55AAEF040007FFFF",
  139. "55AAEF04000FFFFF",
  140. "55AAEF04001FFFFF",
  141. "55AAEF04003FFFFF",
  142. "55AAEF04007FFFFF",
  143. "55AAEF0400FFFFFF",
  144. "55AAEF0401FFFFFF",
  145. "55AAEF0403FFFFFF",
  146. "55AAEF0407FFFFFF",
  147. "55AAEF040FFFFFFF",
  148. "55AAEF041FFFFFFF",
  149. "55AAEF043FFFFFFF",
  150. "55AAEF047FFFFFFF",
  151. "55AAEF04FFFFFFFF",
  152. "55AAEF0500000001",
  153. "55AAEF0500000003",
  154. "55AAEF0500000007",
  155. "55AAEF050000000F",
  156. "55AAEF050000001F",
  157. "55AAEF050000003F",
  158. "55AAEF050000007F",
  159. "55AAEF05000000FF",
  160. "55AAEF05000001FF",
  161. "55AAEF05000003FF",
  162. "55AAEF05000007FF",
  163. "55AAEF0500000FFF",
  164. "55AAEF0500001FFF",
  165. "55AAEF0500003FFF",
  166. "55AAEF0500007FFF",
  167. "55AAEF050000FFFF",
  168. "55AAEF050001FFFF",
  169. "55AAEF050003FFFF",
  170. "55AAEF050007FFFF",
  171. "55AAEF05000FFFFF",
  172. "55AAEF05001FFFFF",
  173. "55AAEF05003FFFFF",
  174. "55AAEF05007FFFFF",
  175. "55AAEF0500FFFFFF",
  176. "55AAEF0501FFFFFF",
  177. "55AAEF0503FFFFFF",
  178. "55AAEF0507FFFFFF",
  179. "55AAEF050FFFFFFF",
  180. "55AAEF051FFFFFFF",
  181. "55AAEF053FFFFFFF",
  182. "55AAEF057FFFFFFF",
  183. "55AAEF05FFFFFFFF",
  184. "55AAEF0600000001",
  185. "55AAEF0600000003",
  186. "55AAEF0600000007",
  187. "55AAEF060000000F",
  188. "55AAEF060000001F",
  189. "55AAEF060000003F",
  190. "55AAEF060000007F",
  191. "55AAEF06000000FF",
  192. "55AAEF06000001FF",
  193. "55AAEF06000003FF",
  194. "55AAEF06000007FF",
  195. "55AAEF0600000FFF",
  196. "55AAEF0600001FFF",
  197. "55AAEF0600003FFF",
  198. "55AAEF0600007FFF",
  199. "55AAEF060000FFFF",
  200. "55AAEF060001FFFF",
  201. "55AAEF060003FFFF",
  202. "55AAEF060007FFFF",
  203. "55AAEF06000FFFFF",
  204. "55AAEF06001FFFFF",
  205. "55AAEF06003FFFFF",
  206. "55AAEF06007FFFFF",
  207. "55AAEF0600FFFFFF",
  208. "55AAEF0601FFFFFF",
  209. "55AAEF0603FFFFFF",
  210. "55AAEF0607FFFFFF",
  211. "55AAEF060FFFFFFF",
  212. "55AAEF061FFFFFFF",
  213. "55AAEF063FFFFFFF",
  214. "55AAEF067FFFFFFF",
  215. "55AAEF06FFFFFFFF",
  216. NULL
  217. };
  218. static
  219. const char *multichip_init_cmd[] =
  220. {
  221. "55AAC000C0C0C0C00500000001000000", // set number of sub-chips (05 in this case)
  222. "55AAEF020000000000000000000000000000000000000000", // power down all SHA-2 modules
  223. "55AAEF3020000000", // Enable SHA-2 OR NOT - NO SCRYPT ACCEPTS WITHOUT THIS???
  224. NULL
  225. };
  226. static
  227. const char *sha2_init_cmd[] =
  228. {
  229. "55AAEF3020000000", // Enable SHA-2
  230. "55AA1F2817000000", // Enable GCP
  231. NULL
  232. };
  233. // called when initializing GridSeed device
  234. // called while initializing DualMiner when mining in scrypt+sha (dual-mode)
  235. static
  236. const char *scrypt_init_cmd[] =
  237. {
  238. "55AA1F2814000000", // Enable Scrypt
  239. "55AA1F2817000000", // Enable GCP
  240. NULL
  241. };
  242. // called before job start by GridSeed when mining scrypt
  243. // called before job start by DualMiner when mining scrypt in scrypt+sha (dual-mode)
  244. static
  245. const char *scrypt_reset_cmd[] =
  246. {
  247. // faster, for start of each job:
  248. "55AA1F2816000000", // Reset Scrypt(?)
  249. "55AA1F2817000000", // Enable GCP(?)
  250. NULL
  251. };
  252. // called while initializing DualMiner when mining scrypt in scrypt-only (not dual-mode)
  253. static
  254. const char *scrypt_only_init_cmd[] =
  255. {
  256. "55AAEF0200000000",
  257. "55AAEF0300000000",
  258. "55AAEF0400000000",
  259. "55AAEF0500000000",
  260. "55AAEF0600000000",
  261. "55AAEF3040000000",
  262. "55AA1F2810000000",
  263. "55AA1F2813000000",
  264. NULL
  265. };
  266. // called before job start by DualMiner when mining scrypt in scrypt-only (not dual-mode)
  267. // called while initializing DualMiner when mining scrypt in scrypt-only (not dual-mode)
  268. static
  269. const char *scrypt_only_reset_cmd[] =
  270. {
  271. "55AA1F2810000000", // Close Scrypt(?)
  272. "55AA1F2813000000", // Open Scrypt(?)
  273. NULL
  274. };
  275. static
  276. const char *gcp_chip_reset_cmd[] =
  277. {
  278. "55AAC000808080800000000001000000", // GCP (GridChip) reset
  279. NULL
  280. };
  281. static
  282. const char *sha2_chip_reset_cmd[] =
  283. {
  284. "55AAC000E0E0E0E00000000001000000", // SHA2 reset
  285. NULL
  286. };
  287. void gc3355_reset_dtr(int fd)
  288. {
  289. // set data terminal ready (DTR) status
  290. set_serial_dtr(fd, BGV_HIGH);
  291. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  292. set_serial_dtr(fd, BGV_LOW);
  293. }
  294. static
  295. void gc3355_set_register(uint8_t * const buf, const uint8_t clusaddr, const uint8_t chipaddr, const uint8_t regaddr, const uint32_t val)
  296. {
  297. buf[0] = 0x55;
  298. buf[1] = 0xaa;
  299. buf[2] = (clusaddr << 4) | chipaddr;
  300. buf[3] = regaddr;
  301. buf[4] = (val >> 0) & 0xff;
  302. buf[5] = (val >> 8) & 0xff;
  303. buf[6] = (val >> 0x10) & 0xff;
  304. buf[7] = (val >> 0x18) & 0xff;
  305. }
  306. static
  307. void gc3355_config_cpm(uint8_t * const buf, const uint8_t chipaddr, const float mhz)
  308. {
  309. // See https://github.com/gridseed/gc3355-doc/blob/master/GC3355_Register_Spec.pdf
  310. const uint8_t pll_bypass = 1;
  311. const uint8_t pll_bandselect = 0;
  312. const uint8_t pll_outdiv = 0;
  313. uint8_t freq_div, freq_mult, last_freq_mult = 0; // mhz = (25 / freq_div * freq_mult)
  314. float actual_mhz, last_actual_mhz = -1;
  315. for (freq_div = 1; freq_div <= 32; ++freq_div)
  316. {
  317. freq_mult = mhz * freq_div / 25;
  318. if (freq_mult > 0x80)
  319. freq_mult = 0x80;
  320. actual_mhz = 25. / freq_div * freq_mult;
  321. if (last_actual_mhz > actual_mhz)
  322. {
  323. --freq_div;
  324. freq_mult = last_freq_mult;
  325. if (opt_debug)
  326. actual_mhz = 25. / freq_div * freq_mult;
  327. break;
  328. }
  329. if (actual_mhz > mhz - .5)
  330. break;
  331. last_actual_mhz = actual_mhz;
  332. last_freq_mult = freq_mult;
  333. }
  334. const uint8_t pll_F = freq_mult - 1;
  335. const uint8_t pll_R = freq_div - 1;
  336. const uint8_t core_clk_out1_diven = 0;
  337. const uint8_t core_clk_sel1 = 0;
  338. const uint8_t core_clk_sel0 = 0;
  339. const uint8_t pll_clk_gate = 0;
  340. const uint8_t pll_recfg = 1;
  341. const uint8_t cfg_cpm = 1;
  342. 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);
  343. gc3355_set_register(buf, 0xe, chipaddr, 0, cfg);
  344. }
  345. // NOTE: MHz must match CPM config
  346. static
  347. void gc3355_config_sha256d(uint8_t * const buf, const uint8_t chipaddr, const float mhz, const uint32_t baud)
  348. {
  349. // See https://github.com/gridseed/gc3355-doc/blob/master/GC3355_Register_Spec.pdf
  350. const uint8_t force_start = 1;
  351. const uint8_t uart_enable = 1;
  352. const uint8_t uart_debug = 0;
  353. const uint8_t byte_order = 0;
  354. const uint16_t rpt_cycle = (mhz * 1000000 / baud);
  355. const uint32_t cfg = (force_start << 31) | (uart_enable << 30) | (uart_debug << 29) | (byte_order << 28) | rpt_cycle;
  356. gc3355_set_register(buf, 0, chipaddr, 0xff, cfg);
  357. }
  358. static
  359. void gc3355_log_protocol(int fd, const char *buf, size_t size, const char *prefix)
  360. {
  361. char hex[(size * 2) + 1];
  362. bin2hex(hex, buf, size);
  363. applog(LOG_DEBUG, "%s fd=%d: DEVPROTO: %s(%3lu) %s",
  364. GC3355_CHIP_NAME, fd, prefix, (unsigned long)size, hex);
  365. }
  366. int gc3355_read(int fd, char *buf, size_t size)
  367. {
  368. size_t read;
  369. int tries = 20;
  370. while (tries > 0)
  371. {
  372. read = serial_read(fd, buf, size);
  373. if (read > 0)
  374. break;
  375. tries--;
  376. }
  377. if (unlikely(tries == 0))
  378. return -1;
  379. if ((read > 0) && opt_dev_protocol)
  380. gc3355_log_protocol(fd, buf, read, "RECV");
  381. return read;
  382. }
  383. ssize_t gc3355_write(int fd, const void * const buf, const size_t size)
  384. {
  385. if (opt_dev_protocol)
  386. gc3355_log_protocol(fd, buf, size, "SEND");
  387. return write(fd, buf, size);
  388. }
  389. static
  390. void _gc3355_send_cmds_bin(int fd, const char *cmds[], bool is_bin, int size)
  391. {
  392. int i = 0;
  393. unsigned char ob_bin[512];
  394. for (i = 0; ; i++)
  395. {
  396. const char *cmd = cmds[i];
  397. if (cmd == NULL)
  398. break;
  399. if (is_bin)
  400. gc3355_write(fd, cmd, size);
  401. else
  402. {
  403. int bin_size = strlen(cmd) / 2;
  404. hex2bin(ob_bin, cmd, bin_size);
  405. gc3355_write(fd, ob_bin, bin_size);
  406. }
  407. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  408. }
  409. }
  410. #define gc3355_send_cmds_bin(fd, cmds, size) _gc3355_send_cmds_bin(fd, cmds, true, size)
  411. #define gc3355_send_cmds(fd, cmds) _gc3355_send_cmds_bin(fd, cmds, false, -1)
  412. void gc3355_scrypt_only_reset(int fd)
  413. {
  414. gc3355_send_cmds(fd, scrypt_only_reset_cmd);
  415. }
  416. void gc3355_set_pll_freq(int fd, int pll_freq)
  417. {
  418. const uint8_t chipaddr = 0xf;
  419. const uint32_t baud = 115200; // FIXME: Make this configurable
  420. uint8_t buf[8];
  421. gc3355_config_cpm(buf, chipaddr, pll_freq);
  422. gc3355_write(fd, buf, sizeof(buf));
  423. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  424. gc3355_config_sha256d(buf, chipaddr, pll_freq, baud);
  425. gc3355_write(fd, buf, sizeof(buf));
  426. }
  427. static
  428. void gc3355_open_sha2_units(int fd, int sha2_units)
  429. {
  430. int unit_count = 0;
  431. unsigned char ob_bin[8];
  432. int i;
  433. // should be 0 - 160
  434. unit_count = sha2_units < 0 ? 0 : sha2_units > 160 ? 160 : sha2_units;
  435. if (unit_count > 0)
  436. {
  437. for(i = 0; i <= unit_count; i++)
  438. {
  439. hex2bin(ob_bin, sha2_open_cmd[i], sizeof(ob_bin));
  440. gc3355_write(fd, ob_bin, 8);
  441. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  442. }
  443. }
  444. else if (unit_count == 0)
  445. gc3355_send_cmds(fd, sha2_gating_cmd);
  446. }
  447. void gc3355_scrypt_init(int fd)
  448. {
  449. gc3355_send_cmds(fd, scrypt_init_cmd);
  450. }
  451. static
  452. void gc3355_scrypt_only_init(int fd)
  453. {
  454. gc3355_send_cmds(fd, sha2_gating_cmd);
  455. gc3355_send_cmds(fd, scrypt_only_init_cmd);
  456. gc3355_scrypt_only_reset(fd);
  457. }
  458. static
  459. void gc3355_open_sha2_cores(int fd, int sha2_cores)
  460. {
  461. unsigned char cmd[24], c1, c2;
  462. uint16_t mask;
  463. int i;
  464. mask = 0x00;
  465. for (i = 0; i < sha2_cores; i++)
  466. mask = mask << 1 | 0x01;
  467. if (mask == 0)
  468. return;
  469. c1 = mask & 0x00ff;
  470. c2 = mask >> 8;
  471. memset(cmd, 0, sizeof(cmd));
  472. memcpy(cmd, "\x55\xaa\xef\x02", 4);
  473. for (i = 4; i < 24; i++) {
  474. cmd[i] = ((i % 2) == 0) ? c1 : c2;
  475. gc3355_write(fd, cmd, sizeof(cmd));
  476. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  477. }
  478. return;
  479. }
  480. static
  481. void gc3355_init_sha2_nonce(int fd)
  482. {
  483. char **cmds, *p;
  484. uint32_t nonce, step;
  485. int i;
  486. cmds = calloc(sizeof(char *) *(GC3355_ORB_DEFAULT_CHIPS + 1), 1);
  487. if (unlikely(!cmds))
  488. quit(1, "Failed to calloc init nonce commands data array");
  489. step = 0xffffffff / GC3355_ORB_DEFAULT_CHIPS;
  490. for (i = 0; i < GC3355_ORB_DEFAULT_CHIPS; i++)
  491. {
  492. p = calloc(8, 1);
  493. if (unlikely(!p))
  494. quit(1, "Failed to calloc init nonce commands data");
  495. memcpy(p, "\x55\xaa\x00\x00", 4);
  496. p[2] = i;
  497. nonce = htole32(step * i);
  498. memcpy(p + 4, &nonce, sizeof(nonce));
  499. cmds[i] = p;
  500. }
  501. cmds[i] = NULL;
  502. gc3355_send_cmds_bin(fd, (const char **)cmds, 8);
  503. for (i = 0; i < GC3355_ORB_DEFAULT_CHIPS; i++)
  504. free(cmds[i]);
  505. free(cmds);
  506. return;
  507. }
  508. void gc3355_sha2_init(int fd)
  509. {
  510. gc3355_send_cmds(fd, sha2_gating_cmd);
  511. gc3355_send_cmds(fd, sha2_init_cmd);
  512. }
  513. static
  514. void gc3355_reset_chips(int fd)
  515. {
  516. // reset chips
  517. gc3355_send_cmds(fd, gcp_chip_reset_cmd);
  518. gc3355_send_cmds(fd, sha2_chip_reset_cmd);
  519. }
  520. void gc3355_init_device(int fd, int pll_freq, bool scrypt_only, bool detect_only, bool usbstick)
  521. {
  522. gc3355_reset_chips(fd);
  523. if (usbstick)
  524. gc3355_reset_dtr(fd);
  525. if (usbstick)
  526. {
  527. // initialize units
  528. if (opt_scrypt && scrypt_only)
  529. gc3355_scrypt_only_init(fd);
  530. else
  531. {
  532. gc3355_sha2_init(fd);
  533. gc3355_scrypt_init(fd);
  534. }
  535. //set freq
  536. gc3355_set_pll_freq(fd, pll_freq);
  537. }
  538. else
  539. {
  540. // zzz
  541. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  542. // initialize units
  543. gc3355_send_cmds(fd, multichip_init_cmd);
  544. gc3355_scrypt_init(fd);
  545. //set freq
  546. gc3355_set_pll_freq(fd, pll_freq);
  547. //init sha2 nonce
  548. gc3355_init_sha2_nonce(fd);
  549. }
  550. // zzz
  551. cgsleep_ms(GC3355_COMMAND_DELAY_MS);
  552. if (!detect_only)
  553. {
  554. if (!opt_scrypt)
  555. {
  556. if (usbstick)
  557. // open sha2 units
  558. gc3355_open_sha2_units(fd, opt_sha2_units);
  559. else
  560. {
  561. // open sha2 cores
  562. gc3355_open_sha2_cores(fd, DEFAULT_ORB_SHA2_CORES);
  563. }
  564. }
  565. if (usbstick)
  566. // set request to send (RTS) status
  567. set_serial_rts(fd, BGV_HIGH);
  568. else
  569. // no fifo for orb
  570. gc3355_send_cmds(fd, no_fifo_cmd);
  571. }
  572. }
  573. void gc3355_init_usborb(int fd, int pll_freq, bool scrypt_only, bool detect_only)
  574. {
  575. gc3355_init_device(fd, pll_freq, scrypt_only, detect_only, false);
  576. }
  577. void gc3355_init_usbstick(int fd, int pll_freq, bool scrypt_only, bool detect_only)
  578. {
  579. gc3355_init_device(fd, pll_freq, scrypt_only, detect_only, true);
  580. }
  581. void gc3355_scrypt_reset(int fd)
  582. {
  583. gc3355_send_cmds(fd, scrypt_reset_cmd);
  584. }
  585. void gc3355_scrypt_prepare_work(unsigned char cmd[156], struct work *work)
  586. {
  587. // command header
  588. cmd[0] = 0x55;
  589. cmd[1] = 0xaa;
  590. cmd[2] = 0x1f;
  591. cmd[3] = 0x00;
  592. // task data
  593. memcpy(cmd + 4, work->target, 32);
  594. memcpy(cmd + 36, work->midstate, 32);
  595. memcpy(cmd + 68, work->data, 80);
  596. // nonce_max
  597. cmd[148] = 0xff;
  598. cmd[149] = 0xff;
  599. cmd[150] = 0xff;
  600. cmd[151] = 0xff;
  601. // taskid
  602. int workid = work->id;
  603. memcpy(cmd + 152, &(workid), 4);
  604. }
  605. void gc3355_sha2_prepare_work(unsigned char cmd[52], struct work *work, bool simple)
  606. {
  607. if (simple)
  608. {
  609. // command header
  610. cmd[0] = 0x55;
  611. cmd[1] = 0xaa;
  612. cmd[2] = 0x0f;
  613. cmd[3] = 0x01; // SHA header sig
  614. memcpy(cmd + 4, work->midstate, 32);
  615. memcpy(cmd + 36, work->data + 64, 12);
  616. // taskid
  617. int workid = work->id;
  618. memcpy(cmd + 48, &(workid), 4);
  619. }
  620. else
  621. {
  622. // command header
  623. cmd[0] = 0x55;
  624. cmd[1] = 0xaa;
  625. cmd[2] = 0x0f;
  626. cmd[3] = 0x00; // Scrypt header sig - used by DualMiner in Dual Mode
  627. uint8_t temp_bin[64];
  628. memset(temp_bin, 0, 64);
  629. memcpy(temp_bin, work->midstate, 32);
  630. memcpy(temp_bin + 52, work->data + 64, 12);
  631. memcpy(cmd + 8, work->midstate, 32);
  632. memcpy(cmd + 40, temp_bin + 52, 12);
  633. }
  634. }
  635. uint32_t gc3355_get_firmware_version(int fd)
  636. {
  637. gc3355_send_cmds(fd, firmware_request_cmd);
  638. char buf[GC3355_READ_SIZE];
  639. int read = gc3355_read(fd, buf, GC3355_READ_SIZE);
  640. if (read != GC3355_READ_SIZE)
  641. {
  642. applog(LOG_ERR, "%s: Failed reading work from %d", GC3355_CHIP_NAME, fd);
  643. return -1;
  644. }
  645. // firmware response begins with 55aac000 90909090
  646. if (memcmp(buf, "\x55\xaa\xc0\x00\x90\x90\x90\x90", GC3355_READ_SIZE - 4) != 0)
  647. {
  648. return -1;
  649. }
  650. uint32_t fw_version = le32toh(*(uint32_t *)(buf + 8));
  651. return fw_version;
  652. }