gc3355.c 16 KB

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