driver-bfx.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. return false;
  115. }
  116. ftdi = bfx_open(info);
  117. if (!ftdi)
  118. return false;
  119. state = malloc(sizeof(*state));
  120. *state = (struct bfx_state){
  121. .ftdi = ftdi,
  122. };
  123. port = calloc(1, sizeof(*port));
  124. port->userp = state;
  125. port->txrx = bfx_spi_txrx;
  126. port->repr = bfx_drv.dname;
  127. port->logprio = LOG_DEBUG;
  128. {
  129. struct bitfury_device dummy_bitfury = {
  130. .spi = port,
  131. };
  132. drv_set_defaults(&bfx_drv, bitfury_set_device_funcs_probe, &dummy_bitfury, NULL, NULL, 1);
  133. }
  134. chips = libbitfury_detectChips1(port);
  135. free(port);
  136. bfx_device_off(ftdi);
  137. ft232r_close(ftdi);
  138. if (!chips)
  139. {
  140. free(state);
  141. applog(LOG_DEBUG, "%s: 0 chips detected", __func__);
  142. return false;
  143. }
  144. if (lowlevel_claim(&bfx_drv, true, info))
  145. {
  146. free(state);
  147. return false;
  148. }
  149. state->lowl_info = lowlevel_ref(info);
  150. struct cgpu_info *cgpu;
  151. cgpu = malloc(sizeof(*cgpu));
  152. *cgpu = (struct cgpu_info){
  153. .drv = &bfx_drv,
  154. .set_device_funcs = bfx_set_device_funcs,
  155. .device_data = state,
  156. .threads = 1,
  157. .procs = chips,
  158. // TODO: .name
  159. .dev_manufacturer = maybe_strdup(info->manufacturer),
  160. .dev_product = maybe_strdup(product),
  161. .dev_serial = maybe_strdup(serial),
  162. .deven = DEV_ENABLED,
  163. // TODO: .cutofftemp
  164. };
  165. return add_cgpu(cgpu);
  166. }
  167. static
  168. bool bfx_init(struct thr_info * const thr)
  169. {
  170. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  171. struct bfx_state * const state = cgpu->device_data;
  172. struct lowlevel_device_info * const info = state->lowl_info;
  173. struct spi_port *port;
  174. struct bitfury_device *bitfury;
  175. struct ft232r_device_handle *ftdi;
  176. ftdi = bfx_open(info);
  177. lowlevel_devinfo_free(info);
  178. if (!ftdi)
  179. {
  180. applog(LOG_ERR, "%"PRIpreprv": Failed to open ft232r device", cgpu->proc_repr);
  181. return false;
  182. }
  183. port = malloc(sizeof(*port));
  184. bitfury = malloc(sizeof(*bitfury) * cgpu->procs);
  185. if (!(port && bitfury && state))
  186. {
  187. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate structures", cgpu->proc_repr);
  188. free(port);
  189. free(bitfury);
  190. free(state);
  191. ft232r_close(ftdi);
  192. return false;
  193. }
  194. /* Be careful, read spidevc.h comments for warnings */
  195. memset(port, 0, sizeof(*port));
  196. port->txrx = bfx_spi_txrx;
  197. port->cgpu = cgpu;
  198. port->repr = cgpu->proc_repr;
  199. port->logprio = LOG_ERR;
  200. state->ftdi = ftdi;
  201. port->userp = state;
  202. for (proc = cgpu; proc; (proc = proc->next_proc), ++bitfury)
  203. {
  204. struct thr_info * const mythr = proc->thr[0];
  205. *bitfury = (struct bitfury_device){
  206. .spi = port,
  207. .fasync = proc->proc_id,
  208. };
  209. proc->device_data = bitfury;
  210. mythr->cgpu_data = state;
  211. bitfury->osc6_bits = 50;
  212. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  213. bitfury_init_chip(proc);
  214. proc->status = LIFE_INIT2;
  215. }
  216. timer_set_now(&thr->tv_poll);
  217. return true;
  218. }
  219. static
  220. void bfx_disable(struct thr_info * const thr)
  221. {
  222. struct bfx_state * const state = thr->cgpu_data;
  223. struct ft232r_device_handle * const ftdi = state->ftdi;
  224. bitfury_disable(thr);
  225. bfx_device_off(ftdi);
  226. }
  227. static
  228. void bfx_reinit(struct cgpu_info * const cgpu)
  229. {
  230. struct thr_info * const thr = cgpu->thr[0];
  231. struct bfx_state * const state = thr->cgpu_data;
  232. struct ft232r_device_handle * const ftdi = state->ftdi;
  233. bfx_device_off(ftdi);
  234. cgsleep_ms(1);
  235. bitfury_enable(thr);
  236. }
  237. static
  238. void bfx_shutdown(struct thr_info * const thr)
  239. {
  240. struct bfx_state * const state = thr->cgpu_data;
  241. struct ft232r_device_handle * const ftdi = state->ftdi;
  242. if (ftdi)
  243. bfx_device_off(ftdi);
  244. }
  245. static const struct bfg_set_device_definition bfx_set_device_funcs[] = {
  246. {"osc6_bits", bitfury_set_osc6_bits, "range 1-"BITFURY_MAX_OSC6_BITS_S" (slow to fast)"},
  247. {NULL},
  248. };
  249. struct device_drv bfx_drv = {
  250. .dname = "bfx",
  251. .name = "BFX",
  252. .lowl_probe = bfx_lowl_probe,
  253. .thread_init = bfx_init,
  254. .thread_disable = bfx_disable,
  255. .thread_enable = bitfury_enable,
  256. .reinit_device = bfx_reinit,
  257. .thread_shutdown = bfx_shutdown,
  258. .minerloop = minerloop_async,
  259. .job_prepare = bitfury_job_prepare,
  260. .job_start = bitfury_noop_job_start,
  261. .poll = bitfury_do_io,
  262. .job_process_results = bitfury_job_process_results,
  263. .get_api_extra_device_detail = bitfury_api_device_detail,
  264. .get_api_extra_device_status = bitfury_api_device_status,
  265. #ifdef HAVE_CURSES
  266. .proc_wlogprint_status = bitfury_wlogprint_status,
  267. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  268. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  269. #endif
  270. };