driver-twinfury.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Copyright 2013 Andreas Auer
  3. * Copyright 2013-2014 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. applog(LOG_INFO, "%"PRIpreprv": Opened %s", cgpu->proc_repr, cgpu->device_path);
  182. info->tx_buffer[0] = 'W';
  183. info->tx_buffer[1] = 0x00;
  184. if(info->id.version == 2)
  185. {
  186. char buf[8] = "V\x00\x00";
  187. if(twinfury_send_command(fd, buf, 3))
  188. {
  189. if(8 == twinfury_wait_response(fd, buf, 8))
  190. {
  191. info->voltage = (buf[4] & 0xFF);
  192. info->voltage |= (buf[5] << 8);
  193. applog(LOG_DEBUG, "%"PRIpreprv": Voltage: %dmV", cgpu->dev_repr, info->voltage);
  194. if(info->voltage < 800 || info->voltage > 950)
  195. {
  196. info->voltage = 0;
  197. }
  198. }
  199. else
  200. {
  201. applog(LOG_ERR, "%"PRIpreprv": Failed to get voltage.", cgpu->dev_repr);
  202. info->voltage = 0;
  203. }
  204. }
  205. else
  206. {
  207. applog(LOG_ERR, "%"PRIpreprv": Failed to send voltage request", cgpu->dev_repr);
  208. }
  209. }
  210. timer_set_now(&thr->tv_poll);
  211. return true;
  212. }
  213. //------------------------------------------------------------------------------
  214. static bool twinfury_process_results(struct cgpu_info * const proc)
  215. {
  216. struct twinfury_info *device = proc->device_data;
  217. uint8_t *rx_buffer = device->rx_buffer;
  218. int16_t rx_len = device->rx_len;
  219. struct work *work = proc->thr[0]->work;
  220. if(rx_len == 0 || rx_len == -1)
  221. {
  222. return false;
  223. }
  224. if(rx_buffer[3] == 0)
  225. {
  226. return false;
  227. }
  228. if(!work)
  229. {
  230. return true;
  231. }
  232. uint32_t m7 = *((uint32_t *)&work->data[64]);
  233. uint32_t ntime = *((uint32_t *)&work->data[68]);
  234. uint32_t nbits = *((uint32_t *)&work->data[72]);
  235. int j=0;
  236. for(j=0; j<rx_len; j+= 8)
  237. {
  238. struct twinfury_state state;
  239. state.chip = rx_buffer[j + 1];
  240. state.state = rx_buffer[j + 2];
  241. state.switched = rx_buffer[j + 3];
  242. memcpy(&state.nonce, rx_buffer + j + 4, 4);
  243. uint32_t nonce = bitfury_decnonce(state.nonce);
  244. if((nonce & 0xFFC00000) != 0xdf800000)
  245. {
  246. applog(LOG_DEBUG, "%"PRIpreprv": Len: %lu Cmd: %c Chip: %d State: %c Switched: %d Nonce: %08lx",
  247. proc->proc_repr,
  248. (unsigned long)rx_len, rx_buffer[j], state.chip, state.state, state.switched, (unsigned long)nonce);
  249. if (bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, &nonce))
  250. submit_nonce(proc->thr[0], work, nonce);
  251. else
  252. inc_hw_errors(proc->thr[0], work, nonce);
  253. }
  254. }
  255. return true;
  256. }
  257. //------------------------------------------------------------------------------
  258. int64_t twinfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  259. {
  260. // Bitfury chips process only 768/1024 of the nonce range
  261. return 0xbd000000;
  262. }
  263. //------------------------------------------------------------------------------
  264. static
  265. bool twinfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  266. {
  267. struct cgpu_info *board = thr->cgpu;
  268. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  269. memcpy(&info->tx_buffer[ 2], work->midstate, 32);
  270. memcpy(&info->tx_buffer[34], &work->data[64], 12);
  271. work->blk.nonce = 0xffffffff;
  272. return true;
  273. }
  274. //------------------------------------------------------------------------------
  275. static
  276. void twinfury_poll(struct thr_info *thr)
  277. {
  278. struct cgpu_info * const dev = thr->cgpu;
  279. struct twinfury_info *info = dev->device_data;
  280. uint8_t n_chips = 0;
  281. uint8_t buffer[2] = { 'Q', 0 };
  282. uint8_t response[8];
  283. bool flashed = false;
  284. if(info->send_voltage)
  285. {
  286. char buf[8] = "V";
  287. buf[1] = info->voltage & 0xFF;
  288. buf[2] = (info->voltage >> 8) & 0xFF;
  289. if(!twinfury_send_command(dev->device_fd, buf, 3))
  290. applog(LOG_ERR, "%s: Failed supply voltage", dev->dev_repr);
  291. else
  292. if(8 != twinfury_wait_response(dev->device_fd, buf, 8))
  293. {
  294. applog(LOG_ERR, "%s: Waiting for response timed out (Supply voltage)", dev->dev_repr);
  295. }
  296. else
  297. {
  298. info->voltage = (buf[4] & 0xFF);
  299. info->voltage |= (buf[5] << 8);
  300. }
  301. info->send_voltage = false;
  302. }
  303. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc, ++n_chips)
  304. {
  305. struct thr_info * const proc_thr = proc->thr[0];
  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) && proc_thr->next_work)
  339. {
  340. mt_job_transition(proc_thr);
  341. // TODO: Delay morework until right before it's needed
  342. timer_set_now(&proc_thr->tv_morework);
  343. job_start_complete(proc_thr);
  344. }
  345. }
  346. buffer[0] = 'T';
  347. if(twinfury_send_command(dev->device_fd, buffer, 1))
  348. {
  349. if(8 == twinfury_wait_response(dev->device_fd, response, 8))
  350. {
  351. if(response[0] == buffer[0])
  352. {
  353. const float temp = ((uint16_t)response[4] | (uint16_t)(response[5] << 8)) / 10.0;
  354. if (opt_dev_protocol && opt_debug)
  355. {
  356. char hex[93];
  357. bin2hex(hex, response, 8);
  358. applog(LOG_DEBUG, "%"PRIpreprv": TEMP: %s",
  359. dev->dev_repr, hex);
  360. }
  361. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  362. proc->temp = temp;
  363. applog(LOG_DEBUG, "%"PRIpreprv": Temperature: %f", dev->dev_repr, temp);
  364. }
  365. }
  366. else
  367. {
  368. applog(LOG_DEBUG, "%"PRIpreprv": No temperature response", dev->dev_repr);
  369. }
  370. }
  371. timer_set_delay_from_now(&thr->tv_poll, 250000);
  372. }
  373. //------------------------------------------------------------------------------
  374. static
  375. void twinfury_job_start(struct thr_info *thr)
  376. {
  377. struct cgpu_info *board = thr->cgpu;
  378. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  379. int device_fd = thr->cgpu->device->device_fd;
  380. uint8_t buffer[8];
  381. int16_t len;
  382. if (opt_dev_protocol && opt_debug)
  383. {
  384. char hex[93];
  385. bin2hex(hex, info->tx_buffer, 46);
  386. applog(LOG_DEBUG, "%"PRIpreprv": SEND: %s",
  387. board->proc_repr, hex);
  388. }
  389. if(!twinfury_send_command(device_fd, info->tx_buffer, 46))
  390. {
  391. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", board->proc_repr);
  392. dev_error(board, REASON_DEV_COMMS_ERROR);
  393. job_start_abort(thr, true);
  394. return;
  395. }
  396. len = twinfury_wait_response(device_fd, buffer, 8);
  397. if(unlikely(len == -1))
  398. {
  399. applog(LOG_ERR, "%"PRIpreprv": Work send timeout.", board->proc_repr);
  400. }
  401. }
  402. //------------------------------------------------------------------------------
  403. static void twinfury_shutdown(struct thr_info *thr)
  404. {
  405. struct cgpu_info *cgpu = thr->cgpu;
  406. serial_close(cgpu->device_fd);
  407. }
  408. //------------------------------------------------------------------------------
  409. static bool twinfury_identify(struct cgpu_info *cgpu)
  410. {
  411. cgpu->flash_led = 1;
  412. return true;
  413. }
  414. #ifdef HAVE_CURSES
  415. void twinfury_tui_wlogprint_choices(struct cgpu_info * const proc)
  416. {
  417. struct twinfury_info * const state = proc->device->device_data;
  418. if(state->id.version > 1)
  419. {
  420. wlogprint("[V]oltage ");
  421. }
  422. }
  423. const char *twinfury_tui_handle_choice(struct cgpu_info * const proc, const int input)
  424. {
  425. struct twinfury_info * const state = proc->device->device_data;
  426. if(state->id.version > 1)
  427. {
  428. switch (input)
  429. {
  430. case 'v': case 'V':
  431. {
  432. const int val = curses_int("Set supply voltage (range 800mV-950mV; slow to fast)");
  433. if (val < 800 || val > 950)
  434. return "Invalid supply voltage value\n";
  435. state->voltage = val;
  436. state->send_voltage = true;
  437. return "Supply voltage changing\n";
  438. }
  439. }
  440. }
  441. return NULL;
  442. }
  443. void twinfury_wlogprint_status(struct cgpu_info * const proc)
  444. {
  445. const struct twinfury_info * const state = proc->device->device_data;
  446. if(state->id.version > 1)
  447. wlogprint("Supply voltage: %dmV\n", state->voltage);
  448. }
  449. #endif
  450. //------------------------------------------------------------------------------
  451. struct device_drv twinfury_drv = {
  452. //lowercase driver name so --scan pattern matching works
  453. .dname = "twinfury",
  454. .name = "TBF",
  455. .probe_priority = -111,
  456. .lowl_match = twinfury_lowl_match,
  457. .lowl_probe = twinfury_lowl_probe,
  458. .identify_device = twinfury_identify,
  459. .thread_init = twinfury_init,
  460. .minerloop = minerloop_async,
  461. .job_prepare = twinfury_job_prepare,
  462. .job_start = twinfury_job_start,
  463. .poll = twinfury_poll,
  464. .job_process_results = twinfury_job_process_results,
  465. .thread_shutdown = twinfury_shutdown,
  466. #ifdef HAVE_CURSES
  467. .proc_wlogprint_status = twinfury_wlogprint_status,
  468. .proc_tui_wlogprint_choices = twinfury_tui_wlogprint_choices,
  469. .proc_tui_handle_choice = twinfury_tui_handle_choice,
  470. #endif
  471. };