driver-futurebit.c 14 KB

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