driver-futurebit.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. static const uint8_t futurebit_max_chips = 0x01;
  24. #define FUTUREBIT_DEFAULT_FREQUENCY 104
  25. #define FUTUREBIT_MIN_CLOCK 104
  26. #define FUTUREBIT_MAX_CLOCK 400
  27. // Number of seconds chip of 54 cores @ 352mhz takes to scan full range
  28. #define FUTUREBIT_HASH_SPEED 4090.0
  29. #define FUTUREBIT_MAX_NONCE 0xffffffff
  30. #define FUTUREBIT_READ_SIZE 8
  31. #define futurebit_max_clusters_per_chip 6
  32. #define futurebit_max_cores_per_cluster 9
  33. static const uint8_t futurebit_g_head[] = {
  34. 0xd4, 0x59, 0x2d, 0x01, 0x1d, 0x01, 0x8e, 0xa7, 0x4e, 0xbb, 0x17, 0xb8, 0x06, 0x6b, 0x2a, 0x75,
  35. 0x83, 0x99, 0xd5, 0xf1, 0x9b, 0x5c, 0x60, 0x73, 0xd0, 0x9b, 0x50, 0x0d, 0x92, 0x59, 0x82, 0xad,
  36. 0xc4, 0xb3, 0xed, 0xd3, 0x52, 0xef, 0xe1, 0x46, 0x67, 0xa8, 0xca, 0x9f, 0x27, 0x9f, 0x63, 0x30,
  37. 0xcc, 0xbb, 0xb9, 0x10, 0x3b, 0x9e, 0x3a, 0x53, 0x50, 0x76, 0x50, 0x52, 0x08, 0x1d, 0xdb, 0xae,
  38. 0x89, 0x8f, 0x1e, 0xf6, 0xb8, 0xc6, 0x4f, 0x3b, 0xce, 0xf7, 0x15, 0xf6, 0, 0, 0, 1,
  39. 0, 0, 0, 1, 0x8e, 0xa7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0
  41. };
  42. BFG_REGISTER_DRIVER(futurebit_drv)
  43. static const struct bfg_set_device_definition futurebit_set_device_funcs_probe[];
  44. struct futurebit_chip {
  45. uint8_t chipid;
  46. uint8_t global_reg[8];
  47. uint16_t chip_mask[futurebit_max_clusters_per_chip];
  48. uint32_t clst_offset[futurebit_max_clusters_per_chip];
  49. unsigned active_cores;
  50. unsigned freq;
  51. };
  52. static
  53. void futurebit_chip_init(struct futurebit_chip * const chip, const uint8_t chipid)
  54. {
  55. *chip = (struct futurebit_chip){
  56. .chipid = chipid,
  57. .global_reg = {0, 4, 0x40, 0, 0, 0, 0, 1},
  58. .chip_mask = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
  59. .clst_offset = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},
  60. .active_cores = 54,
  61. .freq = FUTUREBIT_DEFAULT_FREQUENCY,
  62. };
  63. }
  64. static
  65. void futurebit_reset_board(const int fd)
  66. {
  67. if(set_serial_rts(fd, BGV_HIGH) == BGV_ERROR)
  68. applog(LOG_DEBUG, "IOCTL RTS RESET FAILED");
  69. cgsleep_ms(100);
  70. if(set_serial_rts(fd, BGV_LOW) == BGV_ERROR)
  71. applog(LOG_DEBUG, "IOCTL RTS RESET FAILED");
  72. }
  73. static
  74. void futurebit_set_diag_mode(struct futurebit_chip * const chip, bool diag_enable)
  75. {
  76. if (diag_enable)
  77. chip->global_reg[1] |= 1;
  78. else
  79. chip->global_reg[1] &= ~1;
  80. }
  81. static
  82. bool futurebit_write_global_reg(const int fd, const struct futurebit_chip * const chip)
  83. {
  84. uint8_t buf[112];
  85. memset(&buf, 0, 102);
  86. memcpy(&buf[102], &chip->global_reg[0], 8);
  87. buf[110] = 0;
  88. buf[111] = 0xff;
  89. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  90. return false;
  91. return true;
  92. }
  93. static
  94. bool futurebit_write_cluster_reg(const int fd, const struct futurebit_chip * const chip, const uint16_t cores_active, const uint32_t offset, const uint8_t clstid)
  95. {
  96. uint8_t buf[112];
  97. memset(&buf, 0, 104);
  98. pk_u16be(buf, 104, cores_active);
  99. pk_u32be(buf, 106, offset);
  100. buf[110] = clstid;
  101. buf[111] = 0xfe;
  102. //applog(LOG_DEBUG, " %u: %u: %u : %u", buf[106], buf[107], buf[108], buf[109]);
  103. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  104. return false;
  105. return true;
  106. }
  107. static
  108. bool futurebit_init_pll(const int fd, struct futurebit_chip * const chip)
  109. {
  110. unsigned freq = chip->freq;
  111. uint8_t bytes1;
  112. uint8_t bytes2;
  113. uint8_t divider;
  114. if (freq <= 200){
  115. divider = (freq - 8)/8;
  116. divider <<= 1;
  117. bytes1 = 0x70 | ((divider & 0xf0) >> 4);
  118. bytes2 = 0x30 | ((divider & 0xf0) >> 4);
  119. }else {
  120. divider = (freq - 16)/16;
  121. divider <<= 1;
  122. bytes1 = 0x60 | ((divider & 0xf0) >> 4);
  123. bytes2 = 0x20 | ((divider & 0xf0) >> 4);
  124. }
  125. uint8_t bytes3 = 0x00 | ((divider & 0x0f) << 4);
  126. pk_u16be(chip->global_reg, 2, 0x4000);
  127. chip->global_reg[1] |= 0xc;
  128. if (!futurebit_write_global_reg(fd, chip))
  129. return false;
  130. chip->global_reg[2] = bytes1;
  131. chip->global_reg[3] = bytes3;
  132. cgsleep_ms(50);
  133. if (!futurebit_write_global_reg(fd, chip))
  134. return false;
  135. chip->global_reg[2] = bytes2;
  136. chip->global_reg[1] &= ~8;
  137. cgsleep_ms(50);
  138. if (!futurebit_write_global_reg(fd, chip))
  139. return false;
  140. chip->global_reg[1] &= ~4;
  141. cgsleep_ms(50);
  142. if (!futurebit_write_global_reg(fd, chip))
  143. return false;
  144. return true;
  145. }
  146. static
  147. bool futurebit_send_golden(const int fd, const struct futurebit_chip * const chip, const void * const data, const void * const target_p)
  148. {
  149. uint8_t buf[112];
  150. const uint8_t * const target = target_p;
  151. memcpy(buf, data, 80);
  152. if (target && !target[0x1f])
  153. memcpy(&buf[80], target, 0x20);
  154. else
  155. {
  156. memset(&buf[80], 0xff, 0x1f);
  157. buf[111] = 0;
  158. }
  159. //char output[(sizeof(buf) * 2) + 1];
  160. //bin2hex(output, buf, sizeof(buf));
  161. //applog(LOG_DEBUG, "GOLDEN OUTPUT %s", output);
  162. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  163. return false;
  164. return true;
  165. }
  166. static
  167. bool futurebit_send_work(const struct thr_info * const thr, struct work * const work)
  168. {
  169. struct cgpu_info *device = thr->cgpu;
  170. uint8_t buf[112];
  171. uint8_t cmd[112];
  172. const uint8_t * const target = work->target;
  173. unsigned char swpdata[80];
  174. //buf[0] = 0;
  175. //memset(&buf[1], 0xff, 0x1f);
  176. memset(&buf[0], 0, 0x18);
  177. memcpy(&buf[24], &target[24], 0x8);
  178. swap32tobe(swpdata, work->data, 80/4);
  179. memcpy(&buf[32], swpdata, 80);
  180. for (int i = 0; i<112; i++) {
  181. cmd[i] = buf[111 - i];
  182. }
  183. if (write(device->device_fd, cmd, sizeof(cmd)) != sizeof(cmd))
  184. return false;
  185. work->blk.nonce = FUTUREBIT_MAX_NONCE;
  186. return true;
  187. }
  188. static
  189. bool futurebit_detect_one(const char * const devpath)
  190. {
  191. struct futurebit_chip *chips = NULL;
  192. unsigned total_cores = 0;
  193. const int fd = serial_open(devpath, 115200, 1, true);
  194. if (fd < 0)
  195. return_via_applog(err, , LOG_DEBUG, "%s: %s %s", futurebit_drv.dname, "Failed to open", devpath);
  196. applog(LOG_DEBUG, "%s: %s %s", futurebit_drv.dname, "Successfully opened", devpath);
  197. futurebit_reset_board(fd);
  198. // Init chips, setup PLL, and scan for good cores
  199. chips = malloc(futurebit_max_chips * sizeof(*chips));
  200. struct futurebit_chip * const dummy_chip = &chips[0];
  201. futurebit_chip_init(dummy_chip, 0);
  202. // pick up any user-defined settings passed in via --set
  203. drv_set_defaults(&futurebit_drv, futurebit_set_device_funcs_probe, dummy_chip, devpath, detectone_meta_info.serial, 1);
  204. unsigned freq = dummy_chip->freq;
  205. applog(LOG_DEBUG, "%s: %s %u mhz", futurebit_drv.dname, "Core clock set to", freq);
  206. {
  207. uint8_t buf[8];
  208. for (unsigned i = 0; i < futurebit_max_chips; ++i)
  209. {
  210. struct futurebit_chip * const chip = &chips[i];
  211. futurebit_chip_init(chip, i);
  212. chip->freq = freq;
  213. //chip->global_reg[1] = 0x05;
  214. //if (!futurebit_write_global_reg(fd, chip))
  215. // return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "global", devpath);
  216. //cgsleep_ms(50);
  217. futurebit_set_diag_mode(chip, true);
  218. if (!futurebit_init_pll(fd, chip))
  219. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "init PLL", devpath);
  220. cgsleep_ms(50);
  221. if (!futurebit_send_golden(fd, chip, futurebit_g_head, NULL))
  222. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "send scan job", devpath);
  223. while (serial_read(fd, buf, 8) == 8)
  224. {
  225. const uint8_t clsid = buf[7];
  226. if (clsid >= futurebit_max_clusters_per_chip)
  227. applog(LOG_DEBUG, "%s: Bad %s id (%u) during scan of %s chip %u", futurebit_drv.dname, "cluster", clsid, devpath, i);
  228. const uint8_t coreid = buf[6];
  229. if (coreid >= futurebit_max_cores_per_cluster)
  230. applog(LOG_DEBUG, "%s: Bad %s id (%u) during scan of %s chip %u", futurebit_drv.dname, "core", coreid, devpath, i);
  231. if (buf[0] != 0xd9 || buf[1] != 0xeb || buf[2] != 0x86 || buf[3] != 0x63) {
  232. //chips[i].chip_good[clsid][coreid] = false;
  233. 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);
  234. } else {
  235. ++total_cores;
  236. chips[i].chip_mask[clsid] |= (1 << coreid);
  237. }
  238. }
  239. }
  240. }
  241. applog(LOG_DEBUG, "%s: Identified %d cores on %s", futurebit_drv.dname, total_cores, devpath);
  242. if (total_cores == 0)
  243. goto err;
  244. futurebit_reset_board(fd);
  245. // config nonce ranges per cluster based on core responses
  246. unsigned mutiple = FUTUREBIT_MAX_NONCE / total_cores;
  247. uint32_t n_offset = 0x00000000;
  248. for (unsigned i = 0; i < futurebit_max_chips; ++i)
  249. {
  250. struct futurebit_chip * const chip = &chips[i];
  251. chips[i].active_cores = total_cores;
  252. //chip->global_reg[1] = 0x04;
  253. //if (!futurebit_write_global_reg(fd, chip))
  254. //return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "global", devpath);
  255. //cgsleep_ms(50);
  256. futurebit_set_diag_mode(chip, false);
  257. if (!futurebit_init_pll(fd, chip))
  258. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "init PLL", devpath);
  259. cgsleep_ms(50);
  260. for (unsigned x = 0; x < futurebit_max_clusters_per_chip; ++x) {
  261. unsigned gc = 0;
  262. uint16_t core_mask = chips[i].chip_mask[x];
  263. chips[i].clst_offset[x] = n_offset;
  264. applog(LOG_DEBUG, "OFFSET %u MASK %u CHIP %u CLUSTER %u", n_offset, core_mask, i, x);
  265. if (!futurebit_write_cluster_reg(fd, chip, core_mask, n_offset, x))
  266. return_via_applog(err, , LOG_DEBUG, "%s: Failed to (%s) %s", futurebit_drv.dname, "send config register", devpath);
  267. for (unsigned z = 0; z < 15; ++z) {
  268. if (core_mask & 0x0001)
  269. gc += 1;
  270. core_mask >>= 1;
  271. }
  272. n_offset += mutiple * gc;
  273. cgsleep_ms(50);
  274. }
  275. }
  276. if (serial_claim_v(devpath, &futurebit_drv))
  277. goto err;
  278. //serial_close(fd);
  279. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  280. *cgpu = (struct cgpu_info){
  281. .drv = &futurebit_drv,
  282. .device_path = strdup(devpath),
  283. .deven = DEV_ENABLED,
  284. .procs = 1,
  285. .threads = 1,
  286. .device_data = chips,
  287. };
  288. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  289. cgpu->device_fd = fd;
  290. return add_cgpu(cgpu);
  291. err:
  292. if (fd >= 0)
  293. serial_close(fd);
  294. free(chips);
  295. return false;
  296. }
  297. /*
  298. * scanhash mining loop
  299. */
  300. static
  301. void futurebit_submit_nonce(struct thr_info * const thr, const uint8_t buf[8], struct work * const work, struct timeval const start_tv)
  302. {
  303. struct cgpu_info *device = thr->cgpu;
  304. struct futurebit_chip *chips = device->device_data;
  305. uint32_t nonce = *(uint32_t *)buf;
  306. nonce = bswap_32(nonce);
  307. submit_nonce(thr, work, nonce);
  308. // hashrate calc
  309. const uint8_t clstid = buf[7];
  310. uint32_t range = chips[0].clst_offset[clstid];
  311. struct timeval now_tv;
  312. timer_set_now(&now_tv);
  313. int elapsed_ms = ms_tdiff(&now_tv, &start_tv);
  314. double total_hashes = ((nonce - range)/9.0) * chips[0].active_cores;
  315. double hashes_per_ms = total_hashes/elapsed_ms;
  316. uint64_t hashes = hashes_per_ms * ms_tdiff(&now_tv, &thr->_tv_last_hashes_done_call);
  317. if(hashes_per_ms < 1500 && hashes < 100000000)
  318. hashes_done2(thr, hashes, NULL);
  319. else
  320. hashes_done2(thr, 100000, NULL);
  321. }
  322. // send work to the device
  323. static
  324. int64_t futurebit_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
  325. {
  326. struct cgpu_info *device = thr->cgpu;
  327. int fd = device->device_fd;
  328. struct futurebit_chip *chips = device->device_data;
  329. struct timeval start_tv, nonce_range_tv;
  330. // amount of time it takes this device to scan a nonce range:
  331. uint32_t nonce_full_range_sec = FUTUREBIT_HASH_SPEED * 352.0 / FUTUREBIT_DEFAULT_FREQUENCY * 54.0 / chips[0].active_cores;
  332. // timer to break out of scanning should we close in on an entire nonce range
  333. // should break out before the range is scanned, so we are doing 95% of the range
  334. uint64_t nonce_near_range_usec = (nonce_full_range_sec * 1000000. * 0.95);
  335. timer_set_delay_from_now(&nonce_range_tv, nonce_near_range_usec);
  336. // start the job
  337. timer_set_now(&start_tv);
  338. if (!futurebit_send_work(thr, work)) {
  339. applog(LOG_DEBUG, "Failed to start job");
  340. dev_error(device, REASON_DEV_COMMS_ERROR);
  341. }
  342. uint8_t buf[8];
  343. int read = 0;
  344. bool range_nearly_scanned = false;
  345. while (!thr->work_restart // true when new work is available (miner.c)
  346. && ((read = serial_read(fd, buf, 8)) >= 0) // only check for failure - allow 0 bytes
  347. && !(range_nearly_scanned = timer_passed(&nonce_range_tv, NULL))) // true when we've nearly scanned a nonce range
  348. {
  349. if (read == 0)
  350. continue;
  351. if (read == 8) {
  352. futurebit_submit_nonce(thr, buf, work, start_tv);
  353. }
  354. else
  355. applog(LOG_ERR, "%"PRIpreprv": Unrecognized response", device->proc_repr);
  356. }
  357. if (read == -1)
  358. {
  359. applog(LOG_ERR, "%s: Failed to read result", device->dev_repr);
  360. dev_error(device, REASON_DEV_COMMS_ERROR);
  361. }
  362. return 0;
  363. }
  364. /*
  365. * setup & shutdown
  366. */
  367. static
  368. bool futurebit_lowl_probe(const struct lowlevel_device_info * const info)
  369. {
  370. return vcom_lowl_probe_wrapper(info, futurebit_detect_one);
  371. }
  372. static
  373. void futurebit_thread_shutdown(struct thr_info *thr)
  374. {
  375. struct cgpu_info *device = thr->cgpu;
  376. futurebit_reset_board(device->device_fd);
  377. serial_close(device->device_fd);
  378. }
  379. /*
  380. * specify settings / options via RPC or command line
  381. */
  382. // support for --set
  383. // must be set before probing the device
  384. // for setting clock and chips during probe / detect
  385. static
  386. 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)
  387. {
  388. struct futurebit_chip * const chip = device->device_data;
  389. int val = atoi(setting);
  390. if (val < FUTUREBIT_MIN_CLOCK || val > FUTUREBIT_MAX_CLOCK || (val%8)) {
  391. 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",
  392. setting, FUTUREBIT_MIN_CLOCK, FUTUREBIT_MAX_CLOCK);
  393. return replybuf;
  394. } else
  395. chip->freq = val;
  396. return NULL;
  397. }
  398. static
  399. const struct bfg_set_device_definition futurebit_set_device_funcs_probe[] = {
  400. { "clock", futurebit_set_clock, NULL },
  401. { NULL },
  402. };
  403. struct device_drv futurebit_drv = {
  404. .dname = "futurebit",
  405. .name = "MLD",
  406. .drv_min_nonce_diff = common_scrypt_min_nonce_diff,
  407. // detect device
  408. .lowl_probe = futurebit_lowl_probe,
  409. // specify mining type - scanhash
  410. .minerloop = minerloop_scanhash,
  411. // scanhash mining hooks
  412. .scanhash = futurebit_scanhash,
  413. // teardown device
  414. .thread_shutdown = futurebit_thread_shutdown,
  415. };