driver-nanofury.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2013 Vladimir Strinski
  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. #include "config.h"
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include "deviceapi.h"
  14. #include "driver-bitfury.h"
  15. #include "fpgautils.h"
  16. #include "libbitfury.h"
  17. #include "logging.h"
  18. #include "lowlevel.h"
  19. #include "mcp2210.h"
  20. #include "miner.h"
  21. #include "util.h"
  22. #define NANOFURY_USB_PRODUCT "NanoFury"
  23. #define NANOFURY_GP_PIN_LED 0
  24. #define NANOFURY_GP_PIN_SCK_OVR 5
  25. #define NANOFURY_GP_PIN_PWR_EN 6
  26. #define NANOFURY_MAX_BYTES_PER_SPI_TRANSFER 60 // due to MCP2210 limitation
  27. BFG_REGISTER_DRIVER(nanofury_drv)
  28. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  29. static
  30. bool nanofury_spi_reset(struct mcp2210_device * const mcp)
  31. {
  32. int r;
  33. char tx[1] = {0x81}; // will send this waveform: - _ _ _ _ _ _ -
  34. char buf[1];
  35. // SCK_OVRRIDE
  36. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_SCK_OVR, MGV_HIGH))
  37. return false;
  38. for (r = 0; r < 16; ++r)
  39. if (!mcp2210_spi_transfer(mcp, tx, buf, 1))
  40. return false;
  41. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) == MGV_ERROR)
  42. return false;
  43. return true;
  44. }
  45. static void nanofury_device_off(struct mcp2210_device *);
  46. static
  47. bool nanofury_spi_txrx(struct spi_port * const port)
  48. {
  49. struct cgpu_info * const cgpu = port->cgpu;
  50. struct thr_info * const thr = cgpu->thr[0];
  51. struct mcp2210_device * const mcp = thr->cgpu_data;
  52. const void *wrbuf = spi_gettxbuf(port);
  53. void *rdbuf = spi_getrxbuf(port);
  54. size_t bufsz = spi_getbufsz(port);
  55. const uint8_t *ptrwrbuf = wrbuf;
  56. uint8_t *ptrrdbuf = rdbuf;
  57. nanofury_spi_reset(mcp);
  58. // start by sending chunks of 60 bytes...
  59. while (bufsz >= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER)
  60. {
  61. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, NANOFURY_MAX_BYTES_PER_SPI_TRANSFER))
  62. goto err;
  63. ptrrdbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  64. ptrwrbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  65. bufsz -= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  66. }
  67. // send any remaining bytes...
  68. if (bufsz > 0)
  69. {
  70. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, bufsz))
  71. goto err;
  72. }
  73. return true;
  74. err:
  75. mcp2210_spi_cancel(mcp);
  76. nanofury_device_off(mcp);
  77. hashes_done2(thr, -1, NULL);
  78. return false;
  79. }
  80. static
  81. void nanofury_device_off(struct mcp2210_device * const mcp)
  82. {
  83. // Try to reset everything back to input
  84. for (int i = 0; i < 9; ++i)
  85. mcp2210_get_gpio_input(mcp, i);
  86. }
  87. static
  88. bool nanofury_checkport(struct mcp2210_device * const mcp)
  89. {
  90. int i;
  91. const char tmp = 0;
  92. char tmprx;
  93. // default: set everything to input
  94. for (i = 0; i < 9; ++i)
  95. if (MGV_ERROR == mcp2210_get_gpio_input(mcp, i))
  96. goto fail;
  97. // configure the pins that we need:
  98. // LED
  99. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_HIGH))
  100. goto fail;
  101. // PWR_EN
  102. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, MGV_HIGH))
  103. goto fail;
  104. // cancel any outstanding SPI transfers
  105. mcp2210_spi_cancel(mcp);
  106. // configure SPI
  107. // This is the only place where speed, mode and other settings are configured!!!
  108. if (!mcp2210_configure_spi(mcp, 200000, 0xffff, 0xffef, 0, 0, 0))
  109. goto fail;
  110. if (!mcp2210_set_spimode(mcp, 0))
  111. goto fail;
  112. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  113. goto fail;
  114. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 0 should be 0)
  115. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  116. goto fail;
  117. // switch SCK to polarity (default SCK=1 in mode 2)
  118. if (!mcp2210_set_spimode(mcp, 2))
  119. goto fail;
  120. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  121. goto fail;
  122. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 2 should be 1)
  123. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_HIGH)
  124. goto fail;
  125. // switch SCK to polarity (default SCK=0 in mode 0)
  126. if (!mcp2210_set_spimode(mcp, 0))
  127. goto fail;
  128. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  129. goto fail;
  130. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  131. goto fail;
  132. return true;
  133. fail:
  134. nanofury_device_off(mcp);
  135. return false;
  136. }
  137. static
  138. bool nanofury_lowl_match(const struct lowlevel_device_info * const info)
  139. {
  140. return lowlevel_match_lowlproduct(info, &lowl_mcp2210, NANOFURY_USB_PRODUCT);
  141. }
  142. static
  143. bool nanofury_lowl_probe(const struct lowlevel_device_info * const info)
  144. {
  145. const char * const product = info->product;
  146. const char * const serial = info->serial;
  147. struct mcp2210_device *mcp;
  148. if (info->lowl != &lowl_mcp2210)
  149. {
  150. if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
  151. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
  152. __func__, product, serial);
  153. return false;
  154. }
  155. mcp = mcp2210_open(info);
  156. if (!mcp)
  157. {
  158. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
  159. __func__, product, serial);
  160. return false;
  161. }
  162. if (!nanofury_checkport(mcp))
  163. {
  164. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
  165. __func__, product, serial);
  166. mcp2210_close(mcp);
  167. return false;
  168. }
  169. nanofury_device_off(mcp);
  170. mcp2210_close(mcp);
  171. if (bfg_claim_hid(&nanofury_drv, true, info->path))
  172. return false;
  173. struct cgpu_info *cgpu;
  174. cgpu = malloc(sizeof(*cgpu));
  175. *cgpu = (struct cgpu_info){
  176. .drv = &nanofury_drv,
  177. .device_data = lowlevel_ref(info),
  178. .threads = 1,
  179. // TODO: .name
  180. .device_path = strdup(info->path),
  181. .dev_manufacturer = maybe_strdup(info->manufacturer),
  182. .dev_product = maybe_strdup(product),
  183. .dev_serial = maybe_strdup(serial),
  184. .deven = DEV_ENABLED,
  185. // TODO: .cutofftemp
  186. };
  187. return add_cgpu(cgpu);
  188. }
  189. static
  190. bool nanofury_init(struct thr_info * const thr)
  191. {
  192. struct cgpu_info * const cgpu = thr->cgpu;
  193. struct lowlevel_device_info * const info = cgpu->device_data;
  194. struct spi_port *port;
  195. struct bitfury_device *bitfury;
  196. struct mcp2210_device *mcp;
  197. mcp = mcp2210_open(info);
  198. lowlevel_devinfo_free(info);
  199. if (!mcp)
  200. {
  201. applog(LOG_ERR, "%"PRIpreprv": Failed to open mcp2210 device", cgpu->proc_repr);
  202. return false;
  203. }
  204. if (!nanofury_checkport(mcp))
  205. {
  206. applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
  207. mcp2210_close(mcp);
  208. return false;
  209. }
  210. port = malloc(sizeof(*port));
  211. bitfury = malloc(sizeof(*bitfury));
  212. if (!(port && bitfury))
  213. {
  214. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate spi_port and bitfury_device structures", cgpu->proc_repr);
  215. free(port);
  216. free(bitfury);
  217. mcp2210_close(mcp);
  218. return false;
  219. }
  220. thr->cgpu_data = mcp;
  221. *port = (struct spi_port){
  222. .txrx = nanofury_spi_txrx,
  223. .cgpu = cgpu,
  224. .repr = cgpu->proc_repr,
  225. .logprio = LOG_ERR,
  226. };
  227. *bitfury = (struct bitfury_device){
  228. .spi = port,
  229. };
  230. cgpu->device_data = bitfury;
  231. bitfury->osc6_bits = 50;
  232. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  233. bitfury_init_chip(cgpu);
  234. timer_set_now(&thr->tv_poll);
  235. cgpu->status = LIFE_INIT2;
  236. return true;
  237. }
  238. static
  239. void nanofury_disable(struct thr_info * const thr)
  240. {
  241. struct mcp2210_device * const mcp = thr->cgpu_data;
  242. bitfury_disable(thr);
  243. nanofury_device_off(mcp);
  244. }
  245. static
  246. void nanofury_enable(struct thr_info * const thr)
  247. {
  248. struct mcp2210_device * const mcp = thr->cgpu_data;
  249. nanofury_checkport(mcp);
  250. bitfury_enable(thr);
  251. }
  252. static
  253. void nanofury_reinit(struct cgpu_info * const cgpu)
  254. {
  255. struct thr_info * const thr = cgpu->thr[0];
  256. struct mcp2210_device * const mcp = thr->cgpu_data;
  257. nanofury_device_off(mcp);
  258. cgsleep_ms(1);
  259. nanofury_enable(thr);
  260. }
  261. static
  262. void nanofury_shutdown(struct thr_info * const thr)
  263. {
  264. struct mcp2210_device * const mcp = thr->cgpu_data;
  265. nanofury_device_off(mcp);
  266. }
  267. struct device_drv nanofury_drv = {
  268. .dname = "nanofury",
  269. .name = "NFY",
  270. .lowl_match = nanofury_lowl_match,
  271. .lowl_probe = nanofury_lowl_probe,
  272. .thread_init = nanofury_init,
  273. .thread_disable = nanofury_disable,
  274. .thread_enable = nanofury_enable,
  275. .reinit_device = nanofury_reinit,
  276. .thread_shutdown = nanofury_shutdown,
  277. .minerloop = minerloop_async,
  278. .job_prepare = bitfury_job_prepare,
  279. .job_start = bitfury_noop_job_start,
  280. .poll = bitfury_do_io,
  281. .job_process_results = bitfury_job_process_results,
  282. .get_api_extra_device_detail = bitfury_api_device_detail,
  283. .get_api_extra_device_status = bitfury_api_device_status,
  284. .set_device = bitfury_set_device,
  285. #ifdef HAVE_CURSES
  286. .proc_wlogprint_status = bitfury_wlogprint_status,
  287. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  288. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  289. #endif
  290. };