driver-bitfury.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*
  2. * Copyright 2013 bitfury
  3. * Copyright 2013 Anatoly Legkodymov
  4. * Copyright 2013 Luke Dashjr
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config.h"
  25. #include <limits.h>
  26. #include "miner.h"
  27. #include <unistd.h>
  28. #include <stdbool.h>
  29. #include <stdint.h>
  30. #include <sha2.h>
  31. #include "deviceapi.h"
  32. #include "driver-bitfury.h"
  33. #include "libbitfury.h"
  34. #include "util.h"
  35. #include "spidevc.h"
  36. BFG_REGISTER_DRIVER(bitfury_drv)
  37. static
  38. int bitfury_autodetect()
  39. {
  40. RUNONCE(0);
  41. int chip_n;
  42. struct cgpu_info *bitfury_info;
  43. applog(LOG_INFO, "INFO: bitfury_detect");
  44. spi_init();
  45. if (!sys_spi)
  46. return 0;
  47. bitfury_info = calloc(1, sizeof(struct cgpu_info));
  48. bitfury_info->drv = &bitfury_drv;
  49. bitfury_info->threads = 1;
  50. chip_n = libbitfury_detectChips1(sys_spi);
  51. if (!chip_n) {
  52. applog(LOG_WARNING, "No Bitfury chips detected!");
  53. free(bitfury_info);
  54. return 0;
  55. } else {
  56. applog(LOG_WARNING, "BITFURY: %d chips detected!", chip_n);
  57. }
  58. bitfury_info->procs = chip_n;
  59. add_cgpu(bitfury_info);
  60. return 1;
  61. }
  62. static void bitfury_detect(void)
  63. {
  64. noserial_detect_manual(&bitfury_drv, bitfury_autodetect);
  65. }
  66. static
  67. void *bitfury_just_io(struct bitfury_device * const bitfury)
  68. {
  69. struct spi_port * const spi = bitfury->spi;
  70. const int chip = bitfury->fasync;
  71. void *rv;
  72. spi_clear_buf(spi);
  73. spi_emit_break(spi);
  74. spi_emit_fasync(spi, chip);
  75. rv = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  76. spi_txrx(spi);
  77. return rv;
  78. }
  79. static
  80. void bitfury_debug_nonce_array(const struct cgpu_info * const proc, const char *msg, const uint32_t * const inp)
  81. {
  82. const struct bitfury_device * const bitfury = proc->device_data;
  83. const int active = bitfury->active;
  84. char s[((1 + 8) * 0x10) + 1];
  85. char *sp = s;
  86. for (int i = 0; i < 0x10; ++i)
  87. sp += sprintf(sp, "%c%08lx",
  88. (active == i) ? '>' : ' ',
  89. (unsigned long)bitfury_decnonce(inp[i]));
  90. applog(LOG_DEBUG, "%"PRIpreprv": %s%s (job=%08lx)",
  91. proc->proc_repr, msg, s, (unsigned long)inp[0x10]);
  92. }
  93. static
  94. bool bitfury_init_oldbuf(struct cgpu_info * const proc, const uint32_t *inp)
  95. {
  96. struct bitfury_device * const bitfury = proc->device_data;
  97. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  98. uint32_t * const buf = &bitfury->newbuf[0];
  99. int i, differ, tried = 0;
  100. if (!inp)
  101. inp = bitfury_just_io(bitfury);
  102. tryagain:
  103. if (tried > 3)
  104. {
  105. applog(LOG_ERR, "%"PRIpreprv": %s: Giving up after %d tries",
  106. proc->proc_repr, __func__, tried);
  107. bitfury->desync_counter = 99;
  108. return false;
  109. }
  110. ++tried;
  111. memcpy(buf, inp, 0x10 * 4);
  112. inp = bitfury_just_io(bitfury);
  113. differ = -1;
  114. for (i = 0; i < 0x10; ++i)
  115. {
  116. if (inp[i] != buf[i])
  117. {
  118. if (differ != -1)
  119. {
  120. applog(LOG_DEBUG, "%"PRIpreprv": %s: Second differ at %d; trying again",
  121. proc->proc_repr, __func__, i);
  122. goto tryagain;
  123. }
  124. differ = i;
  125. applog(LOG_DEBUG, "%"PRIpreprv": %s: Differ at %d",
  126. proc->proc_repr, __func__, i);
  127. if (tried > 3)
  128. break;
  129. }
  130. }
  131. if (-1 == differ)
  132. {
  133. applog(LOG_DEBUG, "%"PRIpreprv": %s: No differ found; trying again",
  134. proc->proc_repr, __func__);
  135. goto tryagain;
  136. }
  137. bitfury->active = differ;
  138. memcpy(&oldbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  139. memcpy(&oldbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  140. bitfury->oldjob = inp[0x10];
  141. bitfury->desync_counter = 0;
  142. if (opt_debug)
  143. bitfury_debug_nonce_array(proc, "Init", inp);
  144. return true;
  145. }
  146. bool bitfury_init_chip(struct cgpu_info * const proc)
  147. {
  148. struct bitfury_device * const bitfury = proc->device_data;
  149. struct bitfury_payload payload = {
  150. .midstate = "\xf9\x9a\xf0\xd5\x72\x34\x41\xdc\x9e\x10\xd1\x1f\xeb\xcd\xe3\xf5"
  151. "\x52\xf1\x14\x63\x06\x14\xd1\x12\x15\x25\x39\xd1\x7d\x77\x5a\xfd",
  152. .m7 = 0xafbd0b42,
  153. .ntime = 0xb6c24563,
  154. .nbits = 0x6dfa4352,
  155. };
  156. bitfury_payload_to_atrvec(bitfury->atrvec, &payload);
  157. return bitfury_init_oldbuf(proc, NULL);
  158. }
  159. static
  160. bool bitfury_init(struct thr_info *thr)
  161. {
  162. struct cgpu_info *proc;
  163. struct bitfury_device *bitfury;
  164. for (proc = thr->cgpu; proc; proc = proc->next_proc)
  165. {
  166. bitfury = proc->device_data = malloc(sizeof(struct bitfury_device));
  167. *bitfury = (struct bitfury_device){
  168. .spi = sys_spi,
  169. .fasync = proc->proc_id,
  170. };
  171. bitfury_init_chip(proc);
  172. bitfury->osc6_bits = 50;
  173. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  174. }
  175. timer_set_now(&thr->tv_poll);
  176. return true;
  177. }
  178. void bitfury_disable(struct thr_info * const thr)
  179. {
  180. struct cgpu_info * const proc = thr->cgpu;
  181. struct bitfury_device * const bitfury = proc->device_data;
  182. applog(LOG_DEBUG, "%"PRIpreprv": Shutting down chip (disable)", proc->proc_repr);
  183. bitfury_send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  184. }
  185. void bitfury_enable(struct thr_info * const thr)
  186. {
  187. struct cgpu_info * const proc = thr->cgpu;
  188. struct bitfury_device * const bitfury = proc->device_data;
  189. struct cgpu_info * const dev = proc->device;
  190. struct thr_info * const master_thr = dev->thr[0];
  191. applog(LOG_DEBUG, "%"PRIpreprv": Reinitialising chip (enable)", proc->proc_repr);
  192. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  193. bitfury_init_chip(proc);
  194. if (!timer_isset(&master_thr->tv_poll))
  195. timer_set_now(&master_thr->tv_poll);
  196. }
  197. void bitfury_shutdown(struct thr_info *thr) {
  198. struct cgpu_info *cgpu = thr->cgpu, *proc;
  199. struct bitfury_device *bitfury;
  200. applog(LOG_INFO, "INFO bitfury_shutdown");
  201. for (proc = cgpu; proc; proc = proc->next_proc)
  202. {
  203. bitfury = proc->device_data;
  204. bitfury_send_shutdown(bitfury->spi, bitfury->slot, bitfury->fasync);
  205. }
  206. }
  207. bool bitfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  208. {
  209. struct cgpu_info * const proc = thr->cgpu;
  210. struct bitfury_device * const bitfury = proc->device_data;
  211. if (opt_debug)
  212. {
  213. char hex[153];
  214. bin2hex(hex, &work->data[0], 76);
  215. applog(LOG_DEBUG, "%"PRIpreprv": Preparing work %s",
  216. proc->proc_repr, hex);
  217. }
  218. work_to_bitfury_payload(&bitfury->payload, work);
  219. bitfury_payload_to_atrvec(bitfury->atrvec, &bitfury->payload);
  220. work->blk.nonce = 0xffffffff;
  221. return true;
  222. }
  223. static
  224. bool fudge_nonce(struct work * const work, uint32_t *nonce_p) {
  225. static const uint32_t offsets[] = {0, 0xffc00000, 0xff800000, 0x02800000, 0x02C00000, 0x00400000};
  226. uint32_t nonce;
  227. int i;
  228. if (unlikely(!work))
  229. return false;
  230. for (i = 0; i < 6; ++i)
  231. {
  232. nonce = *nonce_p + offsets[i];
  233. if (test_nonce(work, nonce, false))
  234. {
  235. *nonce_p = nonce;
  236. return true;
  237. }
  238. }
  239. return false;
  240. }
  241. void bitfury_noop_job_start(struct thr_info __maybe_unused * const thr)
  242. {
  243. }
  244. // freq_stat->{mh,s} are allocated such that [osc6_min] is the first valid index and [0] falls outside the allocation
  245. void bitfury_init_freq_stat(struct freq_stat * const c, const int osc6_min, const int osc6_max)
  246. {
  247. const int osc6_values = (osc6_max + 1 - osc6_min);
  248. void * const p = calloc(osc6_values, (sizeof(*c->mh) + sizeof(*c->s)));
  249. c->mh = p - (sizeof(*c->mh) * osc6_min);
  250. c->s = p + (sizeof(*c->mh) * osc6_values) - (sizeof(*c->s) * osc6_min);
  251. c->osc6_min = osc6_min;
  252. c->osc6_max = osc6_max;
  253. }
  254. void bitfury_clean_freq_stat(struct freq_stat * const c)
  255. {
  256. free(&c->mh[c->osc6_min]);
  257. }
  258. #define HOP_DONE 600
  259. typedef uint32_t bitfury_inp_t[0x11];
  260. static
  261. int bitfury_select_freq(struct bitfury_device *bitfury, struct cgpu_info *proc) {
  262. int freq;
  263. int random;
  264. int i;
  265. bool all_done;
  266. struct freq_stat *c;
  267. c = &bitfury->chip_stat;
  268. if (c->best_done) {
  269. freq = c->best_osc;
  270. } else {
  271. random = (int)(bitfury->mhz * 1000.0) & 1;
  272. freq = (bitfury->osc6_bits == c->osc6_max) ? c->osc6_min : bitfury->osc6_bits + random;
  273. all_done = true;
  274. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  275. if (c->s[i] <= HOP_DONE)
  276. {
  277. all_done = false;
  278. break;
  279. }
  280. if (all_done)
  281. {
  282. double mh_max = 0.0;
  283. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  284. {
  285. const double mh_actual = c->mh[i] / c->s[i];
  286. if (mh_max >= mh_actual)
  287. continue;
  288. mh_max = mh_actual;
  289. freq = i;
  290. }
  291. c->best_done = 1;
  292. c->best_osc = freq;
  293. applog(LOG_DEBUG, "%"PRIpreprv": best_osc = %d",
  294. proc->proc_repr, freq);
  295. }
  296. }
  297. applog(LOG_DEBUG, "%"PRIpreprv": Changing osc6_bits to %d",
  298. proc->proc_repr, freq);
  299. bitfury->osc6_bits = freq;
  300. bitfury_send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  301. return 0;
  302. }
  303. void bitfury_do_io(struct thr_info * const master_thr)
  304. {
  305. struct cgpu_info *proc;
  306. struct thr_info *thr;
  307. struct bitfury_device *bitfury;
  308. struct freq_stat *c;
  309. const uint32_t *inp;
  310. int n, i, j;
  311. bool newjob;
  312. uint32_t nonce;
  313. int n_chips = 0, lastchip = 0;
  314. struct spi_port *spi = NULL;
  315. bool should_be_running;
  316. struct timeval tv_now;
  317. uint32_t counter;
  318. struct timeval *tvp_stat;
  319. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  320. ++n_chips;
  321. struct cgpu_info *procs[n_chips];
  322. void *rxbuf[n_chips];
  323. bitfury_inp_t rxbuf_copy[n_chips];
  324. // NOTE: This code assumes:
  325. // 1) that chips on the same SPI bus are grouped together
  326. // 2) that chips are in sequential fasync order
  327. n_chips = 0;
  328. for (proc = master_thr->cgpu; proc; proc = proc->next_proc)
  329. {
  330. thr = proc->thr[0];
  331. bitfury = proc->device_data;
  332. should_be_running = (proc->deven == DEV_ENABLED && !thr->pause);
  333. if (should_be_running || thr->_job_transition_in_progress)
  334. {
  335. if (spi != bitfury->spi)
  336. {
  337. if (spi)
  338. spi_txrx(spi);
  339. spi = bitfury->spi;
  340. spi_clear_buf(spi);
  341. spi_emit_break(spi);
  342. lastchip = 0;
  343. }
  344. procs[n_chips] = proc;
  345. spi_emit_fasync(spi, bitfury->fasync - lastchip);
  346. lastchip = bitfury->fasync;
  347. rxbuf[n_chips] = spi_emit_data(spi, 0x3000, &bitfury->atrvec[0], 19 * 4);
  348. ++n_chips;
  349. }
  350. else
  351. if (thr->work /* is currently running */ && thr->busy_state != TBS_STARTING_JOB)
  352. ;//FIXME: shutdown chip
  353. }
  354. if (!spi)
  355. {
  356. timer_unset(&master_thr->tv_poll);
  357. return;
  358. }
  359. timer_set_now(&tv_now);
  360. spi_txrx(spi);
  361. for (j = 0; j < n_chips; ++j)
  362. {
  363. memcpy(rxbuf_copy[j], rxbuf[j], 0x11 * 4);
  364. rxbuf[j] = rxbuf_copy[j];
  365. }
  366. for (j = 0; j < n_chips; ++j)
  367. {
  368. proc = procs[j];
  369. thr = proc->thr[0];
  370. bitfury = proc->device_data;
  371. tvp_stat = &bitfury->tv_stat;
  372. c = &bitfury->chip_stat;
  373. uint32_t * const newbuf = &bitfury->newbuf[0];
  374. uint32_t * const oldbuf = &bitfury->oldbuf[0];
  375. inp = rxbuf[j];
  376. if (unlikely(bitfury->desync_counter == 99))
  377. {
  378. bitfury_init_oldbuf(proc, inp);
  379. goto out;
  380. }
  381. if (opt_debug)
  382. bitfury_debug_nonce_array(proc, "Read", inp);
  383. // To avoid dealing with wrap-around entirely, we rotate array so previous active uint32_t is at index 0
  384. memcpy(&newbuf[0], &inp[bitfury->active], 4 * (0x10 - bitfury->active));
  385. memcpy(&newbuf[0x10 - bitfury->active], &inp[0], 4 * bitfury->active);
  386. newjob = inp[0x10];
  387. if (newbuf[0xf] != oldbuf[0xf])
  388. {
  389. inc_hw_errors2(thr, NULL, NULL);
  390. if (unlikely(++bitfury->desync_counter >= 4))
  391. {
  392. applog(LOG_WARNING, "%"PRIpreprv": Previous nonce mismatch (4th try), recalibrating",
  393. proc->proc_repr);
  394. bitfury_init_oldbuf(proc, inp);
  395. continue;
  396. }
  397. applog(LOG_DEBUG, "%"PRIpreprv": Previous nonce mismatch, ignoring response",
  398. proc->proc_repr);
  399. goto out;
  400. }
  401. else
  402. bitfury->desync_counter = 0;
  403. if (bitfury->oldjob != newjob && thr->next_work)
  404. {
  405. mt_job_transition(thr);
  406. // TODO: Delay morework until right before it's needed
  407. timer_set_now(&thr->tv_morework);
  408. job_start_complete(thr);
  409. }
  410. for (n = 0; newbuf[n] == oldbuf[n]; ++n)
  411. {
  412. if (unlikely(n >= 0xf))
  413. {
  414. inc_hw_errors2(thr, NULL, NULL);
  415. applog(LOG_DEBUG, "%"PRIpreprv": Full result match, reinitialising",
  416. proc->proc_repr);
  417. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  418. bitfury->desync_counter = 99;
  419. goto out;
  420. }
  421. }
  422. counter = bitfury_decnonce(newbuf[n]);
  423. if ((counter & 0xFFC00000) == 0xdf800000)
  424. {
  425. counter &= 0x003fffff;
  426. int32_t cycles = counter - bitfury->counter1;
  427. if (cycles < 0)
  428. cycles += 0x00400000;
  429. if (cycles & 0x00200000)
  430. {
  431. long long unsigned int period;
  432. double ns;
  433. struct timeval d_time;
  434. timersub(&(tv_now), &(bitfury->timer1), &d_time);
  435. period = timeval_to_us(&d_time) * 1000ULL;
  436. ns = (double)period / (double)(cycles);
  437. bitfury->mhz = 1.0 / ns * 65.0 * 1000.0;
  438. bitfury->counter1 = counter;
  439. copy_time(&(bitfury->timer1), &tv_now);
  440. }
  441. }
  442. if (tvp_stat->tv_sec == 0 && tvp_stat->tv_usec == 0) {
  443. copy_time(tvp_stat, &tv_now);
  444. }
  445. if (c->osc6_max)
  446. {
  447. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  448. {
  449. double mh_diff, s_diff;
  450. const int osc = bitfury->osc6_bits;
  451. // Copy current statistics
  452. mh_diff = bitfury->counter2 - c->omh;
  453. s_diff = total_secs - c->os;
  454. applog(LOG_DEBUG, "%"PRIpreprv": %.0f completed in %f seconds",
  455. proc->proc_repr, mh_diff, s_diff);
  456. if (osc >= c->osc6_min && osc <= c->osc6_max)
  457. {
  458. c->mh[osc] += mh_diff;
  459. c->s[osc] += s_diff;
  460. }
  461. c->omh = bitfury->counter2;
  462. c->os = total_secs;
  463. if (opt_debug && !c->best_done)
  464. {
  465. char logbuf[0x100];
  466. logbuf[0] = '\0';
  467. for (i = c->osc6_min; i <= c->osc6_max; ++i)
  468. tailsprintf(logbuf, sizeof(logbuf), " %d=%.3f/%3.0fs",
  469. i, c->mh[i] / c->s[i], c->s[i]);
  470. applog(LOG_DEBUG, "%"PRIpreprv":%s",
  471. proc->proc_repr, logbuf);
  472. }
  473. // Change freq;
  474. if (!c->best_done) {
  475. bitfury_select_freq(bitfury, proc);
  476. } else {
  477. applog(LOG_DEBUG, "%"PRIpreprv": Stable freq, osc6_bits: %d",
  478. proc->proc_repr, bitfury->osc6_bits);
  479. }
  480. }
  481. }
  482. if (n)
  483. {
  484. for (i = 0; i < n; ++i)
  485. {
  486. nonce = bitfury_decnonce(newbuf[i]);
  487. if (fudge_nonce(thr->work, &nonce))
  488. {
  489. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (work=%p)",
  490. proc->proc_repr, i, (unsigned long)nonce, thr->work);
  491. submit_nonce(thr, thr->work, nonce);
  492. bitfury->counter2 += 1;
  493. }
  494. else
  495. if (fudge_nonce(thr->prev_work, &nonce))
  496. {
  497. applog(LOG_DEBUG, "%"PRIpreprv": nonce %x = %08lx (prev work=%p)",
  498. proc->proc_repr, i, (unsigned long)nonce, thr->prev_work);
  499. submit_nonce(thr, thr->prev_work, nonce);
  500. bitfury->counter2 += 1;
  501. }
  502. else
  503. {
  504. inc_hw_errors(thr, thr->work, nonce);
  505. ++bitfury->sample_hwe;
  506. bitfury->strange_counter += 1;
  507. }
  508. if (++bitfury->sample_tot >= 0x40 || bitfury->sample_hwe >= 8)
  509. {
  510. if (bitfury->sample_hwe >= 8)
  511. {
  512. applog(LOG_WARNING, "%"PRIpreprv": %d of the last %d results were bad, reinitialising",
  513. proc->proc_repr, bitfury->sample_hwe, bitfury->sample_tot);
  514. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  515. bitfury->desync_counter = 99;
  516. }
  517. bitfury->sample_tot = bitfury->sample_hwe = 0;
  518. }
  519. }
  520. bitfury->active = (bitfury->active + n) % 0x10;
  521. }
  522. memcpy(&oldbuf[0], &newbuf[n], 4 * (0x10 - n));
  523. memcpy(&oldbuf[0x10 - n], &newbuf[0], 4 * n);
  524. bitfury->oldjob = newjob;
  525. out:
  526. if (unlikely(bitfury->force_reinit))
  527. {
  528. applog(LOG_DEBUG, "%"PRIpreprv": Forcing reinitialisation",
  529. proc->proc_repr);
  530. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  531. bitfury->desync_counter = 99;
  532. bitfury->force_reinit = false;
  533. }
  534. if (timer_elapsed(tvp_stat, &tv_now) >= 60)
  535. copy_time(tvp_stat, &tv_now);
  536. }
  537. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  538. }
  539. int64_t bitfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  540. {
  541. // Bitfury chips process only 756/1024 of the nonce range
  542. return 0xbd000000;
  543. }
  544. struct api_data *bitfury_api_device_detail(struct cgpu_info * const cgpu)
  545. {
  546. struct bitfury_device * const bitfury = cgpu->device_data;
  547. struct api_data *root = NULL;
  548. root = api_add_uint(root, "fasync", &bitfury->fasync, false);
  549. return root;
  550. }
  551. struct api_data *bitfury_api_device_status(struct cgpu_info * const cgpu)
  552. {
  553. struct bitfury_device * const bitfury = cgpu->device_data;
  554. struct api_data *root = NULL;
  555. int clock_bits = bitfury->osc6_bits;
  556. root = api_add_int(root, "Clock Bits", &clock_bits, true);
  557. root = api_add_freq(root, "Frequency", &bitfury->mhz, false);
  558. return root;
  559. }
  560. static
  561. bool _bitfury_set_device_parse_setting(uint32_t * const rv, char * const setting, char * const replybuf, const int maxval)
  562. {
  563. char *p;
  564. long int nv;
  565. if (!setting || !*setting)
  566. {
  567. sprintf(replybuf, "missing setting");
  568. return false;
  569. }
  570. nv = strtol(setting, &p, 0);
  571. if (nv > maxval || nv < 1)
  572. {
  573. sprintf(replybuf, "invalid setting");
  574. return false;
  575. }
  576. *rv = nv;
  577. return true;
  578. }
  579. char *bitfury_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
  580. {
  581. struct bitfury_device * const bitfury = proc->device_data;
  582. uint32_t newval;
  583. if (!strcasecmp(option, "help"))
  584. {
  585. sprintf(replybuf, "baud: SPI baud rate\nosc6_bits: range 1-%d (slow to fast)", BITFURY_MAX_OSC6_BITS);
  586. return replybuf;
  587. }
  588. if (!strcasecmp(option, "baud"))
  589. {
  590. if (!_bitfury_set_device_parse_setting(&bitfury->spi->speed, setting, replybuf, INT_MAX))
  591. return replybuf;
  592. return NULL;
  593. }
  594. if (!strcasecmp(option, "osc6_bits"))
  595. {
  596. struct freq_stat * const c = &bitfury->chip_stat;
  597. newval = bitfury->osc6_bits;
  598. if (!_bitfury_set_device_parse_setting(&newval, setting, replybuf, BITFURY_MAX_OSC6_BITS))
  599. return replybuf;
  600. bitfury->osc6_bits = newval;
  601. bitfury->force_reinit = true;
  602. c->osc6_max = 0;
  603. return NULL;
  604. }
  605. sprintf(replybuf, "Unknown option: %s", option);
  606. return replybuf;
  607. }
  608. #ifdef HAVE_CURSES
  609. void bitfury_tui_wlogprint_choices(struct cgpu_info *cgpu)
  610. {
  611. wlogprint("[O]scillator bits ");
  612. }
  613. const char *bitfury_tui_handle_choice(struct cgpu_info *cgpu, int input)
  614. {
  615. struct bitfury_device * const bitfury = cgpu->device_data;
  616. char buf[0x100];
  617. switch (input)
  618. {
  619. case 'o': case 'O':
  620. {
  621. struct freq_stat * const c = &bitfury->chip_stat;
  622. int val;
  623. char *intvar;
  624. sprintf(buf, "Set oscillator bits (range 1-%d; slow to fast)", BITFURY_MAX_OSC6_BITS);
  625. intvar = curses_input(buf);
  626. if (!intvar)
  627. return "Invalid oscillator bits\n";
  628. val = atoi(intvar);
  629. free(intvar);
  630. if (val < 1 || val > BITFURY_MAX_OSC6_BITS)
  631. return "Invalid oscillator bits\n";
  632. bitfury->osc6_bits = val;
  633. bitfury->force_reinit = true;
  634. c->osc6_max = 0;
  635. return "Oscillator bits changing\n";
  636. }
  637. }
  638. return NULL;
  639. }
  640. void bitfury_wlogprint_status(struct cgpu_info *cgpu)
  641. {
  642. struct bitfury_device * const bitfury = cgpu->device_data;
  643. wlogprint("Oscillator bits: %d\n", bitfury->osc6_bits);
  644. }
  645. #endif
  646. struct device_drv bitfury_drv = {
  647. .dname = "bitfury_gpio",
  648. .name = "BFY",
  649. .drv_detect = bitfury_detect,
  650. .thread_init = bitfury_init,
  651. .thread_disable = bitfury_disable,
  652. .thread_enable = bitfury_enable,
  653. .thread_shutdown = bitfury_shutdown,
  654. .minerloop = minerloop_async,
  655. .job_prepare = bitfury_job_prepare,
  656. .job_start = bitfury_noop_job_start,
  657. .poll = bitfury_do_io,
  658. .job_process_results = bitfury_job_process_results,
  659. .get_api_extra_device_detail = bitfury_api_device_detail,
  660. .get_api_extra_device_status = bitfury_api_device_status,
  661. .set_device = bitfury_set_device,
  662. #ifdef HAVE_CURSES
  663. .proc_wlogprint_status = bitfury_wlogprint_status,
  664. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  665. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  666. #endif
  667. };