gc3355.c 16 KB

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