driver-twinfury.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Copyright 2013 Andreas Auer
  3. * Copyright 2013 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. /*
  11. * Twin Bitfury USB miner with two Bitfury ASIC
  12. */
  13. #include "config.h"
  14. #include "miner.h"
  15. #include "logging.h"
  16. #include "util.h"
  17. #include "libbitfury.h"
  18. #include "lowlevel.h"
  19. #include "lowl-vcom.h"
  20. #include "deviceapi.h"
  21. #include "sha2.h"
  22. #include "driver-twinfury.h"
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <pthread.h>
  27. BFG_REGISTER_DRIVER(twinfury_drv)
  28. static const uint8_t PREAMBLE[] = { 0xDE, 0xAD, 0xBE, 0xEF };
  29. //------------------------------------------------------------------------------
  30. static
  31. bool twinfury_send_command(const int fd, const void * const tx, const uint16_t tx_size)
  32. {
  33. if(4 != write(fd, PREAMBLE, 4))
  34. {
  35. return false;
  36. }
  37. if(tx_size != write(fd, tx, tx_size))
  38. {
  39. return false;
  40. }
  41. return true;
  42. }
  43. //------------------------------------------------------------------------------
  44. static
  45. int16_t twinfury_wait_response(const int fd, const void * const rx, const uint16_t rx_size)
  46. {
  47. int16_t rx_len;
  48. int timeout = 20;
  49. while(timeout > 0)
  50. {
  51. rx_len = serial_read(fd, rx, rx_size);
  52. if(rx_len > 0)
  53. break;
  54. timeout--;
  55. }
  56. if(unlikely(timeout == 0))
  57. {
  58. return -1;
  59. }
  60. return rx_len;
  61. }
  62. //------------------------------------------------------------------------------
  63. static bool twinfury_detect_custom(const char *devpath, struct device_drv *api, struct twinfury_info *info)
  64. {
  65. int fd = serial_open(devpath, info->baud, 1, true);
  66. if(fd < 0)
  67. {
  68. return false;
  69. }
  70. char buf[1024];
  71. int16_t len;
  72. applog(LOG_DEBUG, "%s: Probing for Twinfury device %s", twinfury_drv.dname, devpath);
  73. serial_read(fd, buf, sizeof(buf));
  74. if (!twinfury_send_command(fd, "I", 1))
  75. {
  76. applog(LOG_DEBUG, "%s: Failed writing id request to %s",
  77. twinfury_drv.dname, devpath);
  78. serial_close(fd);
  79. return false;
  80. }
  81. len = twinfury_wait_response(fd, buf, sizeof(buf));
  82. if(len != 29)
  83. {
  84. applog(LOG_DEBUG, "%s: Not a valid response from device (%d)", twinfury_drv.dname, len);
  85. serial_close(fd);
  86. return false;
  87. }
  88. info->id.version = buf[1];
  89. memcpy(info->id.product, buf+2, 16);
  90. bin2hex(info->id.serial, buf+18, 11);
  91. applog(LOG_DEBUG, "%s: %s: %d, %s %s",
  92. twinfury_drv.dname,
  93. devpath,
  94. info->id.version, info->id.product,
  95. info->id.serial);
  96. char buf_state[sizeof(struct twinfury_state)+1];
  97. if (!twinfury_send_command(fd, "R", 1))
  98. {
  99. applog(LOG_DEBUG, "%s: Failed writing reset request to %s",
  100. twinfury_drv.dname, devpath);
  101. serial_close(fd);
  102. return false;
  103. }
  104. len = 0;
  105. while(len == 0)
  106. {
  107. len = serial_read(fd, buf, sizeof(buf_state));
  108. cgsleep_ms(100);
  109. }
  110. serial_close(fd);
  111. if(len != 8)
  112. {
  113. applog(LOG_DEBUG, "%s: %s not responding to reset: %d",
  114. twinfury_drv.dname,
  115. devpath, len);
  116. return false;
  117. }
  118. if (serial_claim_v(devpath, api))
  119. return false;
  120. struct cgpu_info *bigpic;
  121. bigpic = calloc(1, sizeof(struct cgpu_info));
  122. bigpic->drv = api;
  123. bigpic->device_path = strdup(devpath);
  124. bigpic->device_fd = -1;
  125. bigpic->threads = 1;
  126. bigpic->procs = 2;
  127. add_cgpu(bigpic);
  128. applog(LOG_INFO, "Found %"PRIpreprv" at %s", bigpic->proc_repr, devpath);
  129. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d",
  130. bigpic->proc_repr, info->baud);
  131. bigpic->device_data = info;
  132. return true;
  133. }
  134. //------------------------------------------------------------------------------
  135. static bool twinfury_detect_one(const char *devpath)
  136. {
  137. struct twinfury_info *info = calloc(1, sizeof(struct twinfury_info));
  138. if (unlikely(!info))
  139. quit(1, "Failed to malloc bigpicInfo");
  140. info->baud = BPM_BAUD;
  141. if (!twinfury_detect_custom(devpath, &twinfury_drv, info))
  142. {
  143. free(info);
  144. return false;
  145. }
  146. return true;
  147. }
  148. //------------------------------------------------------------------------------
  149. static bool twinfury_lowl_match(const struct lowlevel_device_info * const info)
  150. {
  151. return lowlevel_match_product(info, "Twinfury");
  152. }
  153. //------------------------------------------------------------------------------
  154. static bool twinfury_lowl_probe(const struct lowlevel_device_info * const info)
  155. {
  156. return vcom_lowl_probe_wrapper(info, twinfury_detect_one);
  157. }
  158. //------------------------------------------------------------------------------
  159. static bool twinfury_init(struct thr_info *thr)
  160. {
  161. struct cgpu_info * const cgpu = thr->cgpu;
  162. struct twinfury_info *info = (struct twinfury_info *)cgpu->device_data;
  163. struct cgpu_info *proc;
  164. int i=0;
  165. applog(LOG_DEBUG, "%"PRIpreprv": init", cgpu->proc_repr);
  166. for(i=1, proc = cgpu->next_proc; proc; proc = proc->next_proc, i++)
  167. {
  168. struct twinfury_info *data = calloc(1, sizeof(struct twinfury_info));
  169. proc->device_data = data;
  170. data->tx_buffer[0] = 'W';
  171. data->tx_buffer[1] = i;
  172. }
  173. int fd = serial_open(cgpu->device_path, info->baud, 1, true);
  174. if (unlikely(-1 == fd))
  175. {
  176. applog(LOG_ERR, "%"PRIpreprv": Failed to open %s",
  177. cgpu->proc_repr, cgpu->device_path);
  178. return false;
  179. }
  180. cgpu->device_fd = fd;
  181. cgpu->dev_serial = info->id.serial;
  182. applog(LOG_INFO, "%"PRIpreprv": Opened %s", cgpu->proc_repr, cgpu->device_path);
  183. info->tx_buffer[0] = 'W';
  184. info->tx_buffer[1] = 0x00;
  185. if(info->id.version == 2)
  186. {
  187. char buf[8] = "V\x00\x00";
  188. if(twinfury_send_command(fd, buf, 3))
  189. {
  190. if(8 == twinfury_wait_response(fd, buf, 8))
  191. {
  192. info->voltage = (buf[4] & 0xFF);
  193. info->voltage |= (buf[5] << 8);
  194. applog(LOG_DEBUG, "%"PRIpreprv": Voltage: %dmV", cgpu->dev_repr, info->voltage);
  195. if(info->voltage < 800 || info->voltage > 950)
  196. {
  197. info->voltage = 0;
  198. }
  199. }
  200. else
  201. {
  202. applog(LOG_ERR, "%"PRIpreprv": Failed to get voltage.", cgpu->dev_repr);
  203. info->voltage = 0;
  204. }
  205. }
  206. else
  207. {
  208. applog(LOG_ERR, "%"PRIpreprv": Failed to send voltage request", cgpu->dev_repr);
  209. }
  210. }
  211. timer_set_now(&thr->tv_poll);
  212. return true;
  213. }
  214. //------------------------------------------------------------------------------
  215. static bool twinfury_process_results(struct cgpu_info * const proc)
  216. {
  217. struct twinfury_info *device = proc->device_data;
  218. uint8_t *rx_buffer = device->rx_buffer;
  219. int16_t rx_len = device->rx_len;
  220. struct work *work = proc->thr[0]->work;
  221. if(rx_len == 0 || rx_len == -1)
  222. {
  223. return false;
  224. }
  225. if(rx_buffer[3] == 0)
  226. {
  227. return false;
  228. }
  229. if(!work)
  230. {
  231. return true;
  232. }
  233. uint32_t m7 = *((uint32_t *)&work->data[64]);
  234. uint32_t ntime = *((uint32_t *)&work->data[68]);
  235. uint32_t nbits = *((uint32_t *)&work->data[72]);
  236. int j=0;
  237. for(j=0; j<rx_len; j+= 8)
  238. {
  239. struct twinfury_state state;
  240. state.chip = rx_buffer[j + 1];
  241. state.state = rx_buffer[j + 2];
  242. state.switched = rx_buffer[j + 3];
  243. memcpy(&state.nonce, rx_buffer + j + 4, 4);
  244. uint32_t nonce = bitfury_decnonce(state.nonce);
  245. if((nonce & 0xFFC00000) != 0xdf800000)
  246. {
  247. applog(LOG_DEBUG, "%"PRIpreprv": Len: %lu Cmd: %c Chip: %d State: %c Switched: %d Nonce: %08lx",
  248. proc->proc_repr,
  249. (unsigned long)rx_len, rx_buffer[j], state.chip, state.state, state.switched, (unsigned long)nonce);
  250. if (bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, &nonce))
  251. submit_nonce(proc->thr[0], work, nonce);
  252. else
  253. inc_hw_errors(proc->thr[0], work, nonce);
  254. }
  255. }
  256. return true;
  257. }
  258. //------------------------------------------------------------------------------
  259. int64_t twinfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  260. {
  261. // Bitfury chips process only 768/1024 of the nonce range
  262. return 0xbd000000;
  263. }
  264. //------------------------------------------------------------------------------
  265. static
  266. bool twinfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  267. {
  268. struct cgpu_info *board = thr->cgpu;
  269. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  270. memcpy(&info->tx_buffer[ 2], work->midstate, 32);
  271. memcpy(&info->tx_buffer[34], &work->data[64], 12);
  272. work->blk.nonce = 0xffffffff;
  273. return true;
  274. }
  275. //------------------------------------------------------------------------------
  276. static
  277. void twinfury_poll(struct thr_info *thr)
  278. {
  279. struct cgpu_info * const dev = thr->cgpu;
  280. struct twinfury_info *info = dev->device_data;
  281. uint8_t n_chips = 0;
  282. uint8_t buffer[2] = { 'Q', 0 };
  283. uint8_t response[8];
  284. bool flashed = false;
  285. if(info->send_voltage)
  286. {
  287. char buf[8] = "V";
  288. buf[1] = info->voltage & 0xFF;
  289. buf[2] = (info->voltage >> 8) & 0xFF;
  290. if(!twinfury_send_command(dev->device_fd, buf, 3))
  291. applog(LOG_ERR, "%s: Failed supply voltage", dev->dev_repr);
  292. else
  293. if(8 != twinfury_wait_response(dev->device_fd, buf, 8))
  294. {
  295. applog(LOG_ERR, "%s: Waiting for response timed out (Supply voltage)", dev->dev_repr);
  296. }
  297. else
  298. {
  299. info->voltage = (buf[4] & 0xFF);
  300. info->voltage |= (buf[5] << 8);
  301. }
  302. info->send_voltage = false;
  303. }
  304. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc, ++n_chips)
  305. {
  306. struct twinfury_info *info = (struct twinfury_info *)proc->device_data;
  307. if (proc->flash_led)
  308. {
  309. if (flashed)
  310. proc->flash_led = 0;
  311. else
  312. {
  313. char buf[] = "L";
  314. if(!twinfury_send_command(dev->device_fd, buf, 1))
  315. applog(LOG_ERR, "%s: Failed writing flash LED", dev->dev_repr);
  316. else
  317. if(1 != twinfury_wait_response(dev->device_fd, buf, 1))
  318. applog(LOG_ERR, "%s: Waiting for response timed out (Flash LED)", dev->dev_repr);
  319. else
  320. {
  321. flashed = true;
  322. proc->flash_led = 0;
  323. }
  324. }
  325. }
  326. buffer[1] = n_chips;
  327. if(!twinfury_send_command(dev->device_fd, buffer, 2))
  328. {
  329. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", proc->proc_repr);
  330. dev_error(dev, REASON_DEV_COMMS_ERROR);
  331. return;
  332. }
  333. info->rx_len = twinfury_wait_response(dev->device_fd, info->rx_buffer, sizeof(info->rx_buffer));
  334. if(unlikely(info->rx_len == -1))
  335. {
  336. applog(LOG_ERR, "%"PRIpreprv": Query timeout", proc->proc_repr);
  337. }
  338. if(twinfury_process_results(proc) == true)
  339. {
  340. struct thr_info *proc_thr = proc->thr[0];
  341. mt_job_transition(proc_thr);
  342. // TODO: Delay morework until right before it's needed
  343. timer_set_now(&proc_thr->tv_morework);
  344. job_start_complete(proc_thr);
  345. }
  346. }
  347. buffer[0] = 'T';
  348. if(twinfury_send_command(dev->device_fd, buffer, 1))
  349. {
  350. if(8 == twinfury_wait_response(dev->device_fd, response, 8))
  351. {
  352. if(response[0] == buffer[0])
  353. {
  354. uint16_t temp = response[4] | (response[5] << 8);
  355. char hex[93];
  356. bin2hex(hex, response, 8);
  357. applog(LOG_DEBUG, "%"PRIpreprv": TEMP: %s",
  358. dev->dev_repr, hex);
  359. dev->temp = (float)temp / 10.0;
  360. applog(LOG_DEBUG, "%"PRIpreprv": Temperature: %f", dev->dev_repr, dev->temp);
  361. }
  362. }
  363. else
  364. {
  365. applog(LOG_DEBUG, "%"PRIpreprv": No temperature response", dev->dev_repr);
  366. }
  367. }
  368. timer_set_delay_from_now(&thr->tv_poll, 250000);
  369. }
  370. //------------------------------------------------------------------------------
  371. static
  372. void twinfury_job_start(struct thr_info *thr)
  373. {
  374. struct cgpu_info *board = thr->cgpu;
  375. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  376. int device_fd = thr->cgpu->device->device_fd;
  377. uint8_t buffer[8];
  378. int16_t len;
  379. if (opt_dev_protocol && opt_debug)
  380. {
  381. char hex[93];
  382. bin2hex(hex, info->tx_buffer, 46);
  383. applog(LOG_DEBUG, "%"PRIpreprv": SEND: %s",
  384. board->proc_repr, hex);
  385. }
  386. if(!twinfury_send_command(device_fd, info->tx_buffer, 46))
  387. {
  388. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", board->proc_repr);
  389. dev_error(board, REASON_DEV_COMMS_ERROR);
  390. job_start_abort(thr, true);
  391. return;
  392. }
  393. len = twinfury_wait_response(device_fd, buffer, 8);
  394. if(unlikely(len == -1))
  395. {
  396. applog(LOG_ERR, "%"PRIpreprv": Work send timeout.", board->proc_repr);
  397. }
  398. }
  399. //------------------------------------------------------------------------------
  400. static void twinfury_shutdown(struct thr_info *thr)
  401. {
  402. struct cgpu_info *cgpu = thr->cgpu;
  403. serial_close(cgpu->device_fd);
  404. }
  405. //------------------------------------------------------------------------------
  406. static bool twinfury_identify(struct cgpu_info *cgpu)
  407. {
  408. cgpu->flash_led = 1;
  409. return true;
  410. }
  411. #ifdef HAVE_CURSES
  412. void twinfury_tui_wlogprint_choices(struct cgpu_info * const proc)
  413. {
  414. struct twinfury_info * const state = proc->device->device_data;
  415. if(state->id.version > 1)
  416. {
  417. wlogprint("[V]oltage ");
  418. }
  419. }
  420. const char *twinfury_tui_handle_choice(struct cgpu_info * const proc, const int input)
  421. {
  422. struct twinfury_info * const state = proc->device->device_data;
  423. if(state->id.version > 1)
  424. {
  425. switch (input)
  426. {
  427. case 'v': case 'V':
  428. {
  429. const int val = curses_int("Set supply voltage (range 800mV-950mV; slow to fast)");
  430. if (val < 800 || val > 950)
  431. return "Invalid supply voltage value\n";
  432. state->voltage = val;
  433. state->send_voltage = true;
  434. return "Supply voltage changing\n";
  435. }
  436. }
  437. }
  438. return NULL;
  439. }
  440. void twinfury_wlogprint_status(struct cgpu_info * const proc)
  441. {
  442. const struct twinfury_info * const state = proc->device->device_data;
  443. if(state->id.version > 1)
  444. {
  445. const uint32_t voltage = state->voltage;
  446. wlogprint("Supply voltage: %dmV\n", state->voltage);
  447. }
  448. }
  449. #endif
  450. //------------------------------------------------------------------------------
  451. struct device_drv twinfury_drv = {
  452. .dname = "Twinfury",
  453. .name = "TBF",
  454. .probe_priority = -111,
  455. .lowl_match = twinfury_lowl_match,
  456. .lowl_probe = twinfury_lowl_probe,
  457. .identify_device = twinfury_identify,
  458. .thread_init = twinfury_init,
  459. .minerloop = minerloop_async,
  460. .job_prepare = twinfury_job_prepare,
  461. .job_start = twinfury_job_start,
  462. .poll = twinfury_poll,
  463. .job_process_results = twinfury_job_process_results,
  464. .thread_shutdown = twinfury_shutdown,
  465. #ifdef HAVE_CURSES
  466. .proc_wlogprint_status = twinfury_wlogprint_status,
  467. .proc_tui_wlogprint_choices = twinfury_tui_wlogprint_choices,
  468. .proc_tui_handle_choice = twinfury_tui_handle_choice,
  469. #endif
  470. };