driver-futurebit.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Copyright 2015 John Stefanopoulos
  3. * Copyright 2014-2015 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <stdio.h>
  17. #include <libusb.h>
  18. #include "deviceapi.h"
  19. #include "logging.h"
  20. #include "lowlevel.h"
  21. #include "lowl-vcom.h"
  22. #include "util.h"
  23. #include <bwltc-commands.h>
  24. static const uint8_t futurebit_max_chips = 0x01;
  25. #define FUTUREBIT_DEFAULT_FREQUENCY 600
  26. #define FUTUREBIT_MIN_CLOCK 384
  27. #define FUTUREBIT_MAX_CLOCK 1020
  28. // Number of seconds chip of 54 cores @ 352mhz takes to scan full range
  29. #define FUTUREBIT_HASH_SPEED 1130.0
  30. #define FUTUREBIT_MAX_NONCE 0xffffffff
  31. #define FUTUREBIT_READ_SIZE 8
  32. //#define futurebit_max_clusters_per_chip 6
  33. //#define futurebit_max_cores_per_cluster 9
  34. unsigned char job2[] = {
  35. 0x3c, 0xff, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff,
  37. 0x07, 0x00, 0x00, 0x00, 0xd7, 0xa2, 0xea, 0xb0, 0xc2, 0xd7, 0x6f, 0x1e, 0x33, 0xa4, 0xb5, 0x3e,
  38. 0x0e, 0xb2, 0x84, 0x34, 0x89, 0x5a, 0x8b, 0x10, 0xfb, 0x19, 0x7d, 0x76, 0xe6, 0xe0, 0x38, 0x60,
  39. 0x15, 0x3f, 0x6a, 0x6e, 0x00, 0x00, 0x00, 0x04, 0xb5, 0x93, 0x93, 0x27, 0xf7, 0xc9, 0xfb, 0x26,
  40. 0xdf, 0x3b, 0xde, 0xc0, 0xa6, 0x6c, 0xae, 0x10, 0xb5, 0x53, 0xb7, 0x61, 0x5d, 0x67, 0xa4, 0x97,
  41. 0xe8, 0x7f, 0x06, 0xa6, 0x27, 0xfc, 0xd5, 0x57, 0x44, 0x38, 0xb8, 0x4d, 0xb1, 0xfe, 0x4f, 0x5f,
  42. 0x31, 0xaa, 0x47, 0x3d, 0x3d, 0xb4, 0xfc, 0x03, 0xa2, 0x78, 0x92, 0x44, 0xa1, 0x39, 0xb0, 0x35,
  43. 0xe1, 0x46, 0x04, 0x1e, 0x8c, 0x0a, 0xad, 0x28, 0x58, 0xec, 0x78, 0x3c, 0x1b, 0x00, 0xa4, 0x43
  44. };
  45. BFG_REGISTER_DRIVER(futurebit_drv)
  46. static const struct bfg_set_device_definition futurebit_set_device_funcs_probe[];
  47. struct futurebit_chip {
  48. uint8_t chipid;
  49. unsigned active_cores;
  50. unsigned freq;
  51. uint32_t last_nonce;
  52. };
  53. static
  54. void futurebit_chip_init(struct futurebit_chip * const chip, const uint8_t chipid)
  55. {
  56. *chip = (struct futurebit_chip){
  57. .chipid = chipid,
  58. .active_cores = 64,
  59. .freq = FUTUREBIT_DEFAULT_FREQUENCY,
  60. .last_nonce = 0x00000000,
  61. };
  62. }
  63. static
  64. void futurebit_reset_board(const int fd)
  65. {
  66. applog(LOG_DEBUG, "RESET START");
  67. if(set_serial_rts(fd, BGV_HIGH) == BGV_ERROR)
  68. applog(LOG_DEBUG, "IOCTL RTS RESET FAILED");
  69. cgsleep_ms(1000);
  70. if(set_serial_rts(fd, BGV_LOW) == BGV_ERROR)
  71. applog(LOG_DEBUG, "IOCTL RTS RESET FAILED");
  72. applog(LOG_DEBUG, "RESET END");
  73. }
  74. int futurebit_write(const int fd, const void *buf, size_t buflen)
  75. {
  76. int repeat = 0;
  77. int size = 0;
  78. int ret = 0;
  79. int nwrite = 0;
  80. char output[(buflen * 2) + 1];
  81. bin2hex(output, buf, buflen);
  82. applog(LOG_DEBUG, "WRITE BUFFER %s", output);
  83. while(size < buflen)
  84. {
  85. nwrite = write(fd, buf, buflen);
  86. //applog(LOG_DEBUG, "FutureBit Write SIZE: %u", nwrite);
  87. if (nwrite < 0)
  88. {
  89. applog(LOG_ERR, "FutureBit Write error: %s", strerror(errno));
  90. break;
  91. }
  92. size += nwrite;
  93. if (repeat++ > 1)
  94. {
  95. break;
  96. }
  97. }
  98. return 0;
  99. }
  100. static
  101. bool futurebit_read (const int fd, unsigned char *buf, int read_amount)
  102. {
  103. ssize_t nread = 0;
  104. int size = 0;
  105. int repeat = 0;
  106. while(size < read_amount)
  107. {
  108. nread = read(fd, buf, read_amount);
  109. if(nread < 0)
  110. return false;
  111. size += nread;
  112. //char output[(read_amount * 2) + 1];
  113. // bin2hex(output, buf, read_amount);
  114. //applog(LOG_DEBUG, "READ BUFFER %s", output);
  115. if (repeat++ > 0)
  116. {
  117. break;
  118. }
  119. }
  120. #if 0
  121. int i;
  122. for (i=0; i<size; i++)
  123. {
  124. printf("0x%02x ", buf[i]);
  125. }
  126. printf("\n");
  127. #endif
  128. return true;
  129. }
  130. static
  131. char futurebit_read_register(const int fd, uint32_t chip, uint32_t moudle, uint32_t RegAddr)
  132. {
  133. uint8_t read_reg_data[8]={0};
  134. uint8_t read_reg_cmd[16]={0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0xc3};
  135. read_reg_cmd[1] = chip;
  136. read_reg_cmd[2] = moudle;
  137. read_reg_cmd[3] = 0x80|RegAddr; //read
  138. static int nonce=0;
  139. futurebit_write(fd, read_reg_cmd, 9);
  140. cgsleep_us(100000);
  141. if(!futurebit_read(fd, read_reg_data, 8))
  142. applog(LOG_DEBUG, "FutureBit read register fail");
  143. applog(LOG_DEBUG, "FutureBit Read Return:");
  144. for (int i=0; i<8; i++)
  145. {
  146. applog(LOG_DEBUG,"0x%02x ", read_reg_data[i]);
  147. }
  148. applog(LOG_DEBUG,"\n");
  149. return read_reg_data[0];
  150. }
  151. unsigned
  152. int futurebit_write_register(const int fd, uint32_t chipId, uint32_t moudle, uint32_t Regaddr, uint32_t value)
  153. {
  154. bool ret =true;
  155. uint8_t read_reg_cmd[16]={0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0xc3};
  156. read_reg_cmd[1] = chipId;
  157. read_reg_cmd[2] = moudle;
  158. read_reg_cmd[3] = 0x7f&Regaddr; //&0x7f->write\BF\BFbit[7]:1 read, 0 write
  159. read_reg_cmd[4] = value&0xff;
  160. read_reg_cmd[5] = (value>>8)&0xff;
  161. read_reg_cmd[6] = (value>>16)&0xff;
  162. read_reg_cmd[7] = (value>>24)&0xff;
  163. futurebit_write(fd, read_reg_cmd, 9);
  164. return ret;
  165. }
  166. static
  167. void futurebit_send_cmds(const int fd, const unsigned char *cmds[])
  168. {
  169. int i;
  170. for(i = 0; cmds[i] != NULL; i++)
  171. {
  172. futurebit_write(fd, cmds[i] + 1, cmds[i][0]);
  173. cgsleep_us(10000);
  174. }
  175. }
  176. static
  177. void futurebit_set_frequency(const int fd, uint32_t freq)
  178. {
  179. struct frequecy *p;
  180. unsigned char **cmd = cmd_set_600M;
  181. int i;
  182. for (i=0; i<ARRAY_LEN; i++)
  183. {
  184. if (fre_array[i].freq == freq)
  185. {
  186. cmd = fre_array[i].cmd;
  187. }
  188. }
  189. futurebit_send_cmds(fd, cmd);
  190. }
  191. void futurebit_config_all_chip(const int fd, uint32_t freq)
  192. {
  193. uint32_t reg_val;
  194. int i;
  195. futurebit_reset_board(fd);
  196. futurebit_send_cmds(fd, cmd_auto_address);
  197. cgsleep_us(100000);
  198. //futurebit_set_baudrate(fd);
  199. //cgsleep_us(100000);
  200. futurebit_set_frequency(fd, freq);
  201. cgsleep_us(100000);
  202. #if 1
  203. futurebit_write_register(fd, 0xff, 0xf8,0x22,0x11090005);//feed through
  204. cgsleep_us(100000);
  205. #endif
  206. //Start Nonce at zero for single chip
  207. //reg_val = 0xffffffff/futurebit_max_chips;
  208. //for (i=1; i<(futurebit_max_chips+1); i++)
  209. //{
  210. futurebit_write_register(fd, 0xff, 0x40, 0x00, 0x00000000);
  211. cgsleep_us(100000);
  212. //}
  213. futurebit_send_cmds(fd, gcp_cmd_reset);
  214. cgsleep_us(100000);
  215. }
  216. void futurebit_pull_up_payload(const int fd)
  217. {
  218. char i;
  219. unsigned int regval = 0;
  220. //pull up payload by steps.
  221. for (i=0; i<8; i++)
  222. {
  223. regval |= (0x0f<<(4*i));
  224. futurebit_write_register(fd, 0xff, 0xf8, 0x04, regval);
  225. cgsleep_us(35000);
  226. futurebit_write_register(fd, 0xff, 0xf8, 0x05, regval);
  227. cgsleep_us(35000);
  228. futurebit_write(fd, job2,144) ;
  229. cgsleep_us(35000);
  230. }
  231. }
  232. static
  233. bool futurebit_send_golden(const int fd, const struct futurebit_chip * const chip, const void * const data, const void * const target_p)
  234. {
  235. uint8_t buf[112];
  236. const uint8_t * const target = target_p;
  237. memcpy(buf, data, 80);
  238. if (target && !target[0x1f])
  239. memcpy(&buf[80], target, 0x20);
  240. else
  241. {
  242. memset(&buf[80], 0xff, 0x1f);
  243. buf[111] = 0;
  244. }
  245. //char output[(sizeof(buf) * 2) + 1];
  246. //bin2hex(output, buf, sizeof(buf));
  247. //applog(LOG_DEBUG, "GOLDEN OUTPUT %s", output);
  248. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  249. return false;
  250. return true;
  251. }
  252. static
  253. bool futurebit_send_work(const struct thr_info * const thr, struct work * const work)
  254. {
  255. struct cgpu_info *device = thr->cgpu;
  256. struct futurebit_chip *chips = device->device_data;
  257. uint32_t *pdata = work->data;
  258. uint32_t *midstate = work->midstate;
  259. const uint32_t ptarget[8];
  260. memset(ptarget, 0, 0x8);
  261. work->nonce_diff = 32./0x10000; //set device diff low to keep accurate hashrate and device status
  262. if(work->work_difficulty < work->nonce_diff){
  263. work->nonce_diff = work->work_difficulty;
  264. set_target_to_pdiff(&ptarget, work->work_difficulty);
  265. }else
  266. set_target_to_pdiff(&ptarget, work->nonce_diff);
  267. //applog(LOG_DEBUG, "TARGET_DIFF %u", work->work_difficulty);
  268. int i, bpos;
  269. unsigned char bin[156];
  270. // swab for big endian
  271. uint32_t midstate2[8];
  272. uint32_t data2[20];
  273. uint32_t target2[8];
  274. for(i = 0; i < 19; i++)
  275. {
  276. data2[i] = htole32(pdata[i]);
  277. if(i >= 8) continue;
  278. target2[i] = htole32(ptarget[i]);
  279. midstate2[i] = htole32(midstate[i]);
  280. }
  281. data2[19] = 0;
  282. memset(bin, 0, sizeof(bin));
  283. bpos = 0; memcpy(bin, "\x3c\xff\x40\x01", 4);
  284. // bpos += 4; memcpy(bin + bpos, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00", 32); //target
  285. bpos += 4; memcpy(bin + bpos, (unsigned char *)target2, 32); memset(bin + bpos, 0, 24);
  286. bpos += 32; memcpy(bin + bpos, (unsigned char *)midstate2, 32); //midstateno
  287. bpos += 32; memcpy(bin + bpos, (unsigned char *)data2, 76); //blockheader 76 bytes (ignore last 4bytes nounce)
  288. bpos += 76;
  289. /* char szVal[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\xff\x7f\x00\x00\x00fb357fbeda2ee2a93b841afac3e58173d4a97a400a84a4ec27c47ef5e9322ca620000000b99512c06534b34f62d0a88a5f90ac1857f0c02a1b6e6bb3185aec323b0eb79d2983a6d34c0e59272444dc28b1041e6114939ca8cdbd99f4058ef4965e293ba7598b98cc1a25e34f"; // source string
  290. char szOutput[144];
  291. size_t nLen = strlen(szVal);
  292. // Make sure it is even.
  293. if ((nLen % 2) == 1)
  294. {
  295. printf("Error string must be even number of digits %s", szVal);
  296. }
  297. // Process each set of characters as a single character.
  298. nLen >>= 1;
  299. for (size_t idx = 0; idx < nLen; idx++)
  300. {
  301. char acTmp[3];
  302. sscanf(szVal + (idx << 1), "%2s", acTmp);
  303. szOutput[idx] = (char)strtol(acTmp, NULL, 16);
  304. }
  305. */
  306. futurebit_write(device->device_fd, bin, 144);//144bytes
  307. chips[0].last_nonce = 0x00000000;
  308. /* uint8_t buf[112];
  309. uint8_t cmd[112];
  310. const uint8_t * const target = work->target;
  311. unsigned char swpdata[80];
  312. //buf[0] = 0;
  313. //memset(&buf[1], 0xff, 0x1f);
  314. memset(&buf[0], 0, 0x18);
  315. memcpy(&buf[24], &target[24], 0x8);
  316. swap32tobe(swpdata, work->data, 80/4);
  317. memcpy(&buf[32], swpdata, 80);
  318. for (int i = 0; i<112; i++) {
  319. cmd[i] = buf[111 - i];
  320. }
  321. if (write(device->device_fd, cmd, sizeof(cmd)) != sizeof(cmd))
  322. return false;
  323. */
  324. work->blk.nonce = FUTUREBIT_MAX_NONCE;
  325. return true;
  326. }
  327. static
  328. bool futurebit_detect_one(const char * const devpath)
  329. {
  330. struct futurebit_chip *chips = NULL;
  331. unsigned total_cores = 0;
  332. uint32_t regval = 0;
  333. const int fd = serial_open(devpath, 115200, 1, true);
  334. if (fd < 0)
  335. return_via_applog(err, , LOG_DEBUG, "%s: %s %s", futurebit_drv.dname, "Failed to open", devpath);
  336. applog(LOG_DEBUG, "%s: %s %s", futurebit_drv.dname, "Successfully opened", devpath);
  337. futurebit_reset_board(fd);
  338. if(futurebit_read_register(fd, 0xff, 0xf8, 0xa6) != 0x3c)
  339. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "find chip", devpath);
  340. // Init chips, setup PLL, and scan for good cores
  341. chips = malloc(futurebit_max_chips * sizeof(*chips));
  342. struct futurebit_chip * const dummy_chip = &chips[0];
  343. futurebit_chip_init(dummy_chip, 0);
  344. // pick up any user-defined settings passed in via --set
  345. drv_set_defaults(&futurebit_drv, futurebit_set_device_funcs_probe, dummy_chip, devpath, detectone_meta_info.serial, 1);
  346. unsigned freq = dummy_chip->freq;
  347. applog(LOG_DEBUG, "%s: %s %u mhz", futurebit_drv.dname, "Core clock set to", freq);
  348. struct futurebit_chip * const chip = &chips[0];
  349. futurebit_chip_init(chip, 0);
  350. chip->freq = freq;
  351. futurebit_config_all_chip(fd, freq);
  352. futurebit_pull_up_payload(fd);
  353. //chip->global_reg[1] = 0x05;
  354. //if (!futurebit_write_global_reg(fd, chip))
  355. // return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "global", devpath);
  356. //cgsleep_ms(50);
  357. /*futurebit_set_diag_mode(chip, true);
  358. if (!futurebit_init_pll(fd, chip))
  359. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "init PLL", devpath);
  360. cgsleep_ms(50);
  361. if (!futurebit_send_golden(fd, chip, futurebit_g_head, NULL))
  362. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "send scan job", devpath);
  363. while (serial_read(fd, buf, 8) == 8)
  364. {
  365. const uint8_t clsid = buf[7];
  366. if (clsid >= futurebit_max_clusters_per_chip)
  367. applog(LOG_DEBUG, "%s: Bad %s id (%u) during scan of %s chip %u", futurebit_drv.dname, "cluster", clsid, devpath, i);
  368. const uint8_t coreid = buf[6];
  369. if (coreid >= futurebit_max_cores_per_cluster)
  370. applog(LOG_DEBUG, "%s: Bad %s id (%u) during scan of %s chip %u", futurebit_drv.dname, "core", coreid, devpath, i);
  371. if (buf[0] != 0xd9 || buf[1] != 0xeb || buf[2] != 0x86 || buf[3] != 0x63) {
  372. //chips[i].chip_good[clsid][coreid] = false;
  373. applog(LOG_DEBUG, "%s: Bad %s at core (%u) during scan of %s chip %u cluster %u", futurebit_drv.dname, "nonce", coreid, devpath, i, clsid);
  374. } else {
  375. ++total_cores;
  376. chips[i].chip_mask[clsid] |= (1 << coreid);
  377. }
  378. }
  379. }
  380. }
  381. applog(LOG_DEBUG, "%s: Identified %d cores on %s", futurebit_drv.dname, total_cores, devpath);
  382. if (total_cores == 0)
  383. goto err;
  384. futurebit_reset_board(fd);
  385. // config nonce ranges per cluster based on core responses
  386. unsigned mutiple = FUTUREBIT_MAX_NONCE / total_cores;
  387. uint32_t n_offset = 0x00000000;
  388. for (unsigned i = 0; i < futurebit_max_chips; ++i)
  389. {
  390. struct futurebit_chip * const chip = &chips[i];
  391. chips[i].active_cores = total_cores;
  392. //chip->global_reg[1] = 0x04;
  393. //if (!futurebit_write_global_reg(fd, chip))
  394. //return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "global", devpath);
  395. //cgsleep_ms(50);
  396. futurebit_set_diag_mode(chip, false);
  397. if (!futurebit_init_pll(fd, chip))
  398. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "init PLL", devpath);
  399. cgsleep_ms(50);
  400. for (unsigned x = 0; x < futurebit_max_clusters_per_chip; ++x) {
  401. unsigned gc = 0;
  402. uint16_t core_mask = chips[i].chip_mask[x];
  403. chips[i].clst_offset[x] = n_offset;
  404. applog(LOG_DEBUG, "OFFSET %u MASK %u CHIP %u CLUSTER %u", n_offset, core_mask, i, x);
  405. if (!futurebit_write_cluster_reg(fd, chip, core_mask, n_offset, x))
  406. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "send config register", devpath);
  407. for (unsigned z = 0; z < 15; ++z) {
  408. if (core_mask & 0x0001)
  409. gc += 1;
  410. core_mask >>= 1;
  411. }
  412. n_offset += mutiple * gc;
  413. cgsleep_ms(50);
  414. }
  415. }
  416. */
  417. if (serial_claim_v(devpath, &futurebit_drv))
  418. goto err;
  419. //serial_close(fd);
  420. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  421. *cgpu = (struct cgpu_info){
  422. .drv = &futurebit_drv,
  423. .device_path = strdup(devpath),
  424. .deven = DEV_ENABLED,
  425. .procs = 1,
  426. .threads = 1,
  427. .device_data = chips,
  428. };
  429. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  430. cgpu->device_fd = fd;
  431. return add_cgpu(cgpu);
  432. err:
  433. if (fd >= 0)
  434. serial_close(fd);
  435. free(chips);
  436. return false;
  437. }
  438. /*
  439. * scanhash mining loop
  440. */
  441. static
  442. void futurebit_submit_nonce(struct thr_info * const thr, const uint8_t buf[8], struct work * const work, struct timeval const start_tv)
  443. {
  444. struct cgpu_info *device = thr->cgpu;
  445. struct futurebit_chip *chips = device->device_data;
  446. uint32_t nonce;
  447. uint32_t last_hashes;
  448. // swab for big endian
  449. memcpy((unsigned char *)&nonce, buf+4, 4);
  450. nonce = htole32(nonce);
  451. char output[(8 * 2) + 1];
  452. bin2hex(output, buf, 8);
  453. //applog(LOG_DEBUG, "NONCE %s", output);
  454. applog(LOG_DEBUG, "NONCE int %u", nonce);
  455. applog(LOG_DEBUG, "LAST NONCE int %u", chips[0].last_nonce);
  456. submit_nonce(thr, work, nonce);
  457. // hashrate calc
  458. last_hashes = (nonce+512)-chips[0].last_nonce;
  459. if(last_hashes > 0 && last_hashes < 0x4000000){
  460. hashes_done2(thr, last_hashes, NULL);
  461. chips[0].last_nonce = nonce;
  462. }else
  463. chips[0].last_nonce = nonce;
  464. if(chips[0].last_nonce == 0){
  465. hashes_done2(thr, 3200000*(chips[0].freq/600), NULL);
  466. }
  467. /*
  468. const uint8_t clstid = buf[7];
  469. uint32_t range = chips[0].clst_offset[clstid];
  470. struct timeval now_tv;
  471. timer_set_now(&now_tv);
  472. int elapsed_ms = ms_tdiff(&now_tv, &start_tv);
  473. double total_hashes = ((nonce - range)/9.0) * chips[0].active_cores;
  474. double hashes_per_ms = total_hashes/elapsed_ms;
  475. uint64_t hashes = hashes_per_ms * ms_tdiff(&now_tv, &thr->_tv_last_hashes_done_call);
  476. if(hashes_per_ms < 1500 && hashes < 100000000)
  477. hashes_done2(thr, hashes, NULL);
  478. else
  479. hashes_done2(thr, 100000, NULL);
  480. */
  481. }
  482. // send work to the device
  483. static
  484. int64_t futurebit_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  485. {
  486. struct cgpu_info *device = thr->cgpu;
  487. int fd = device->device_fd;
  488. struct futurebit_chip *chips = device->device_data;
  489. struct timeval start_tv, nonce_range_tv;
  490. // amount of time it takes this device to scan a nonce range:
  491. uint32_t nonce_full_range_sec = FUTUREBIT_HASH_SPEED * 352.0 / FUTUREBIT_DEFAULT_FREQUENCY * 54.0 / chips[0].active_cores;
  492. // timer to break out of scanning should we close in on an entire nonce range
  493. // should break out before the range is scanned, so we are doing 95% of the range
  494. uint64_t nonce_near_range_usec = (nonce_full_range_sec * 1000000. * 0.95);
  495. timer_set_delay_from_now(&nonce_range_tv, nonce_near_range_usec);
  496. // start the job
  497. timer_set_now(&start_tv);
  498. if (!futurebit_send_work(thr, work)) {
  499. applog(LOG_DEBUG, "Failed to start job");
  500. dev_error(device, REASON_DEV_COMMS_ERROR);
  501. }
  502. unsigned char buf[12];
  503. int read = 0;
  504. bool range_nearly_scanned = false;
  505. while (!thr->work_restart // true when new work is available (miner.c)
  506. && ((read = serial_read(fd, buf, 8)) >= 0) // only check for failure - allow 0 bytes
  507. && !(range_nearly_scanned = timer_passed(&nonce_range_tv, NULL))) // true when we've nearly scanned a nonce range
  508. {
  509. if (read == 0)
  510. continue;
  511. if (read == 8) {
  512. futurebit_submit_nonce(thr, buf, work, start_tv);
  513. }
  514. else
  515. applog(LOG_ERR, "%"PRIpreprv": Unrecognized response", device->proc_repr);
  516. }
  517. if (read == -1)
  518. {
  519. applog(LOG_ERR, "%s: Failed to read result", device->dev_repr);
  520. dev_error(device, REASON_DEV_COMMS_ERROR);
  521. }
  522. return 0;
  523. }
  524. /*
  525. * setup & shutdown
  526. */
  527. static
  528. bool futurebit_lowl_probe(const struct lowlevel_device_info * const info)
  529. {
  530. return vcom_lowl_probe_wrapper(info, futurebit_detect_one);
  531. }
  532. static
  533. void futurebit_thread_shutdown(struct thr_info *thr)
  534. {
  535. struct cgpu_info *device = thr->cgpu;
  536. futurebit_reset_board(device->device_fd);
  537. serial_close(device->device_fd);
  538. }
  539. /*
  540. * specify settings / options via RPC or command line
  541. */
  542. // support for --set
  543. // must be set before probing the device
  544. // for setting clock and chips during probe / detect
  545. static
  546. const char *futurebit_set_clock(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  547. {
  548. struct futurebit_chip * const chip = device->device_data;
  549. int val = atoi(setting);
  550. if (val < FUTUREBIT_MIN_CLOCK || val > FUTUREBIT_MAX_CLOCK ) {
  551. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d. Clock must be a mutiple of 8 between 104-200mhz, and a mutiple of 16 between 208-400mhz",
  552. setting, FUTUREBIT_MIN_CLOCK, FUTUREBIT_MAX_CLOCK);
  553. return replybuf;
  554. } else
  555. chip->freq = val;
  556. return NULL;
  557. }
  558. static
  559. const struct bfg_set_device_definition futurebit_set_device_funcs_probe[] = {
  560. { "clock", futurebit_set_clock, NULL },
  561. { NULL },
  562. };
  563. struct device_drv futurebit_drv = {
  564. .dname = "futurebit",
  565. .name = "MLD",
  566. .drv_min_nonce_diff = common_scrypt_min_nonce_diff,
  567. // detect device
  568. .lowl_probe = futurebit_lowl_probe,
  569. // specify mining type - scanhash
  570. .minerloop = minerloop_scanhash,
  571. // scanhash mining hooks
  572. .scanhash = futurebit_scanhash,
  573. // teardown device
  574. .thread_shutdown = futurebit_thread_shutdown,
  575. };