driver-twinfury.c 13 KB

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