driver-futurebit.c 17 KB

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