driver-twinfury.c 14 KB

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