driver-bfx.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright 2013-2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include "deviceapi.h"
  13. #include "driver-bitfury.h"
  14. #include "libbitfury.h"
  15. #include "logging.h"
  16. #include "lowlevel.h"
  17. #include "lowl-ftdi.h"
  18. #include "miner.h"
  19. #include "util.h"
  20. #define BFX_ADBUS_DIRECTIONS 0xfb
  21. #define BFX_ACBUS_DIRECTIONS 0xff
  22. BFG_REGISTER_DRIVER(bfx_drv)
  23. static const struct bfg_set_device_definition bfx_set_device_funcs[];
  24. struct bfx_state {
  25. struct lowlevel_device_info *lowl_info;
  26. struct ft232r_device_handle *ftdi;
  27. };
  28. static
  29. struct ft232r_device_handle *bfx_open(const struct lowlevel_device_info * const info)
  30. {
  31. struct ft232r_device_handle * const ftdi = ft232h_open_mpsse(info);
  32. if (!ftdi)
  33. applogr(NULL, LOG_ERR, "%s: Failed to open", __func__);
  34. if (!ft232h_mpsse_set_adbus(ftdi, 8, BFX_ADBUS_DIRECTIONS))
  35. {
  36. applog(LOG_ERR, "%s: Failed to set A%cBUS pins", __func__, 'D');
  37. goto err;
  38. }
  39. if (!ft232h_mpsse_set_acbus(ftdi, 0, BFX_ACBUS_DIRECTIONS))
  40. {
  41. applog(LOG_ERR, "%s: Failed to set A%cBUS pins", __func__, 'C');
  42. goto err;
  43. }
  44. if (!ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH))
  45. applog(LOG_WARNING, "%s: Failed to purge buffers", __func__);
  46. return ftdi;
  47. err:
  48. ft232h_mpsse_set_adbus(ftdi, 0, 0);
  49. ft232h_mpsse_set_acbus(ftdi, 0, 0);
  50. ft232r_close(ftdi);
  51. return NULL;
  52. }
  53. static
  54. void bfx_device_off(struct ft232r_device_handle * const ftdi)
  55. {
  56. // Try to reset everything back to input
  57. ft232h_mpsse_set_adbus(ftdi, 0, 0);
  58. ft232h_mpsse_set_acbus(ftdi, 0, 0);
  59. }
  60. static
  61. bool bfx_set_cs(struct ft232r_device_handle * const ftdi, bool high)
  62. {
  63. const uint8_t val = high ? 8 : 0;
  64. return ft232h_mpsse_set_adbus(ftdi, val, BFX_ADBUS_DIRECTIONS);
  65. }
  66. static
  67. bool bfx_spi_reset(struct ft232r_device_handle * const ftdi)
  68. {
  69. uint8_t buf[0x10] = { 0xff };
  70. for (int i = 1; i < sizeof(buf); ++i)
  71. buf[i] = ~buf[i - 1];
  72. bfx_set_cs(ftdi, true);
  73. ft232r_write_all(ftdi, buf, sizeof(buf));
  74. bfx_set_cs(ftdi, false);
  75. ft232r_purge_buffers(ftdi, FTDI_PURGE_BOTH);
  76. return true;
  77. }
  78. static
  79. bool bfx_spi_txrx(struct spi_port * const port)
  80. {
  81. struct cgpu_info * const cgpu = port->cgpu;
  82. struct bfx_state * const state = port->userp;
  83. struct ft232r_device_handle * const ftdi = state->ftdi;
  84. const void *wrbuf = spi_gettxbuf(port);
  85. void *rdbuf = spi_getrxbuf(port);
  86. size_t bufsz = spi_getbufsz(port);
  87. bfx_spi_reset(ftdi);
  88. if (ft232h_mpsse_readwrite_all(ftdi, rdbuf, wrbuf, bufsz) != bufsz)
  89. goto err;
  90. return true;
  91. err:
  92. bfx_device_off(ftdi);
  93. if (cgpu)
  94. {
  95. struct thr_info * const thr = cgpu->thr[0];
  96. hashes_done2(thr, -1, NULL);
  97. }
  98. return false;
  99. }
  100. static
  101. bool bfx_lowl_probe(const struct lowlevel_device_info * const info)
  102. {
  103. const char * const product = info->product;
  104. const char * const serial = info->serial;
  105. struct ft232r_device_handle *ftdi;
  106. struct spi_port *port;
  107. struct bfx_state *state;
  108. int chips;
  109. if (info->lowl != &lowl_ft232r)
  110. {
  111. if (info->lowl != &lowl_usb)
  112. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not ft232r!",
  113. __func__, product, serial);
  114. bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
  115. return false;
  116. }
  117. ftdi = bfx_open(info);
  118. if (!ftdi)
  119. return false;
  120. state = malloc(sizeof(*state));
  121. *state = (struct bfx_state){
  122. .ftdi = ftdi,
  123. };
  124. port = calloc(1, sizeof(*port));
  125. port->userp = state;
  126. port->txrx = bfx_spi_txrx;
  127. port->repr = bfx_drv.dname;
  128. port->logprio = LOG_DEBUG;
  129. {
  130. struct bitfury_device dummy_bitfury = {
  131. .spi = port,
  132. };
  133. drv_set_defaults(&bfx_drv, bitfury_set_device_funcs_probe, &dummy_bitfury, NULL, NULL, 1);
  134. }
  135. chips = libbitfury_detectChips1(port);
  136. free(port);
  137. bfx_device_off(ftdi);
  138. ft232r_close(ftdi);
  139. if (!chips)
  140. {
  141. free(state);
  142. applog(LOG_DEBUG, "%s: 0 chips detected", __func__);
  143. return false;
  144. }
  145. if (lowlevel_claim(&bfx_drv, true, info))
  146. {
  147. free(state);
  148. return false;
  149. }
  150. state->lowl_info = lowlevel_ref(info);
  151. struct cgpu_info *cgpu;
  152. cgpu = malloc(sizeof(*cgpu));
  153. *cgpu = (struct cgpu_info){
  154. .drv = &bfx_drv,
  155. .set_device_funcs = bfx_set_device_funcs,
  156. .device_data = state,
  157. .threads = 1,
  158. .procs = chips,
  159. // TODO: .name
  160. .dev_manufacturer = maybe_strdup(info->manufacturer),
  161. .dev_product = maybe_strdup(product),
  162. .dev_serial = maybe_strdup(serial),
  163. .deven = DEV_ENABLED,
  164. // TODO: .cutofftemp
  165. };
  166. return add_cgpu(cgpu);
  167. }
  168. static
  169. bool bfx_init(struct thr_info * const thr)
  170. {
  171. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  172. struct bfx_state * const state = cgpu->device_data;
  173. struct lowlevel_device_info * const info = state->lowl_info;
  174. struct spi_port *port;
  175. struct bitfury_device *bitfury;
  176. struct ft232r_device_handle *ftdi;
  177. ftdi = bfx_open(info);
  178. lowlevel_devinfo_free(info);
  179. if (!ftdi)
  180. {
  181. applog(LOG_ERR, "%"PRIpreprv": Failed to open ft232r device", cgpu->proc_repr);
  182. return false;
  183. }
  184. port = malloc(sizeof(*port));
  185. bitfury = malloc(sizeof(*bitfury) * cgpu->procs);
  186. if (!(port && bitfury && state))
  187. {
  188. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate structures", cgpu->proc_repr);
  189. free(port);
  190. free(bitfury);
  191. free(state);
  192. ft232r_close(ftdi);
  193. return false;
  194. }
  195. /* Be careful, read lowl-spi.h comments for warnings */
  196. memset(port, 0, sizeof(*port));
  197. port->txrx = bfx_spi_txrx;
  198. port->cgpu = cgpu;
  199. port->repr = cgpu->proc_repr;
  200. port->logprio = LOG_ERR;
  201. state->ftdi = ftdi;
  202. port->userp = state;
  203. for (proc = cgpu; proc; (proc = proc->next_proc), ++bitfury)
  204. {
  205. struct thr_info * const mythr = proc->thr[0];
  206. *bitfury = (struct bitfury_device){
  207. .spi = port,
  208. .fasync = proc->proc_id,
  209. };
  210. proc->device_data = bitfury;
  211. mythr->cgpu_data = state;
  212. bitfury->osc6_bits = 50;
  213. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  214. bitfury_init_chip(proc);
  215. proc->status = LIFE_INIT2;
  216. }
  217. timer_set_now(&thr->tv_poll);
  218. return true;
  219. }
  220. static
  221. void bfx_disable(struct thr_info * const thr)
  222. {
  223. struct bfx_state * const state = thr->cgpu_data;
  224. struct ft232r_device_handle * const ftdi = state->ftdi;
  225. bitfury_disable(thr);
  226. bfx_device_off(ftdi);
  227. }
  228. static
  229. void bfx_reinit(struct cgpu_info * const cgpu)
  230. {
  231. struct thr_info * const thr = cgpu->thr[0];
  232. struct bfx_state * const state = thr->cgpu_data;
  233. struct ft232r_device_handle * const ftdi = state->ftdi;
  234. bfx_device_off(ftdi);
  235. cgsleep_ms(1);
  236. bitfury_enable(thr);
  237. }
  238. static
  239. void bfx_shutdown(struct thr_info * const thr)
  240. {
  241. struct bfx_state * const state = thr->cgpu_data;
  242. struct ft232r_device_handle * const ftdi = state->ftdi;
  243. if (ftdi)
  244. bfx_device_off(ftdi);
  245. }
  246. static const struct bfg_set_device_definition bfx_set_device_funcs[] = {
  247. {"osc6_bits", bitfury_set_osc6_bits, "range 1-"BITFURY_MAX_OSC6_BITS_S" (slow to fast)"},
  248. {NULL},
  249. };
  250. struct device_drv bfx_drv = {
  251. .dname = "bfx",
  252. .name = "BFX",
  253. .lowl_probe = bfx_lowl_probe,
  254. .thread_init = bfx_init,
  255. .thread_disable = bfx_disable,
  256. .thread_enable = bitfury_enable,
  257. .reinit_device = bfx_reinit,
  258. .thread_shutdown = bfx_shutdown,
  259. .minerloop = minerloop_async,
  260. .job_prepare = bitfury_job_prepare,
  261. .job_start = bitfury_noop_job_start,
  262. .poll = bitfury_do_io,
  263. .job_process_results = bitfury_job_process_results,
  264. .get_api_extra_device_detail = bitfury_api_device_detail,
  265. .get_api_extra_device_status = bitfury_api_device_status,
  266. #ifdef HAVE_CURSES
  267. .proc_wlogprint_status = bitfury_wlogprint_status,
  268. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  269. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  270. #endif
  271. };