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