driver-nanofury.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #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
  45. bool nanofury_spi_txrx(struct spi_port * const port)
  46. {
  47. struct cgpu_info * const cgpu = port->cgpu;
  48. struct thr_info * const thr = cgpu->thr[0];
  49. struct mcp2210_device * const mcp = thr->cgpu_data;
  50. const void *wrbuf = spi_gettxbuf(port);
  51. void *rdbuf = spi_getrxbuf(port);
  52. size_t bufsz = spi_getbufsz(port);
  53. const uint8_t *ptrwrbuf = wrbuf;
  54. uint8_t *ptrrdbuf = rdbuf;
  55. nanofury_spi_reset(mcp);
  56. // start by sending chunks of 60 bytes...
  57. while (bufsz >= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER)
  58. {
  59. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, NANOFURY_MAX_BYTES_PER_SPI_TRANSFER))
  60. return false;
  61. ptrrdbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  62. ptrwrbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  63. bufsz -= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  64. }
  65. // send any remaining bytes...
  66. if (bufsz > 0)
  67. {
  68. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, bufsz))
  69. return false;
  70. }
  71. return true;
  72. }
  73. static
  74. void nanofury_device_off(struct mcp2210_device * const mcp)
  75. {
  76. // Try to reset everything back to input
  77. for (int i = 0; i < 9; ++i)
  78. mcp2210_get_gpio_input(mcp, i);
  79. }
  80. static
  81. bool nanofury_checkport(struct mcp2210_device * const mcp)
  82. {
  83. int i;
  84. const char tmp = 0;
  85. char tmprx;
  86. // default: set everything to input
  87. for (i = 0; i < 9; ++i)
  88. if (MGV_ERROR == mcp2210_get_gpio_input(mcp, i))
  89. goto fail;
  90. // configure the pins that we need:
  91. // LED
  92. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_HIGH))
  93. goto fail;
  94. // PWR_EN
  95. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, MGV_HIGH))
  96. goto fail;
  97. // configure SPI
  98. // This is the only place where speed, mode and other settings are configured!!!
  99. if (!mcp2210_configure_spi(mcp, 200000, 0xffff, 0xffef, 0, 0, 0))
  100. goto fail;
  101. if (!mcp2210_set_spimode(mcp, 0))
  102. goto fail;
  103. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  104. goto fail;
  105. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 0 should be 0)
  106. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  107. goto fail;
  108. // switch SCK to polarity (default SCK=1 in mode 2)
  109. if (!mcp2210_set_spimode(mcp, 2))
  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 2 should be 1)
  114. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_HIGH)
  115. goto fail;
  116. // switch SCK to polarity (default SCK=0 in mode 0)
  117. if (!mcp2210_set_spimode(mcp, 0))
  118. goto fail;
  119. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  120. goto fail;
  121. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  122. goto fail;
  123. return true;
  124. fail:
  125. nanofury_device_off(mcp);
  126. return false;
  127. }
  128. static
  129. bool nanofury_foundlowl(struct lowlevel_device_info * const info, __maybe_unused void *userp)
  130. {
  131. const char * const product = info->product;
  132. const char * const serial = info->serial;
  133. struct mcp2210_device *mcp;
  134. if (info->lowl != &lowl_mcp2210)
  135. {
  136. if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
  137. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
  138. __func__, product, serial);
  139. return false;
  140. }
  141. mcp = mcp2210_open(info);
  142. if (!mcp)
  143. {
  144. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
  145. __func__, product, serial);
  146. return false;
  147. }
  148. if (!nanofury_checkport(mcp))
  149. {
  150. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
  151. __func__, product, serial);
  152. mcp2210_close(mcp);
  153. return false;
  154. }
  155. nanofury_device_off(mcp);
  156. mcp2210_close(mcp);
  157. if (bfg_claim_hid(&nanofury_drv, true, info->path))
  158. return false;
  159. struct cgpu_info *cgpu;
  160. cgpu = malloc(sizeof(*cgpu));
  161. *cgpu = (struct cgpu_info){
  162. .drv = &nanofury_drv,
  163. .device_data = info,
  164. .threads = 1,
  165. // TODO: .name
  166. .device_path = strdup(info->path),
  167. .dev_manufacturer = strdup(info->manufacturer),
  168. .dev_product = strdup(product),
  169. .dev_serial = strdup(serial),
  170. .deven = DEV_ENABLED,
  171. // TODO: .cutofftemp
  172. };
  173. return add_cgpu(cgpu);
  174. }
  175. static bool nanofury_detect_one(const char *serial)
  176. {
  177. return lowlevel_detect_serial(nanofury_foundlowl, serial);
  178. }
  179. static int nanofury_detect_auto()
  180. {
  181. return lowlevel_detect(nanofury_foundlowl, NANOFURY_USB_PRODUCT);
  182. }
  183. static void nanofury_detect()
  184. {
  185. serial_detect_auto(&nanofury_drv, nanofury_detect_one, nanofury_detect_auto);
  186. }
  187. static
  188. bool nanofury_init(struct thr_info * const thr)
  189. {
  190. struct cgpu_info * const cgpu = thr->cgpu;
  191. struct lowlevel_device_info * const info = cgpu->device_data;
  192. struct spi_port *port;
  193. struct bitfury_device *bitfury;
  194. struct mcp2210_device *mcp;
  195. mcp = mcp2210_open(info);
  196. lowlevel_devinfo_free(info);
  197. if (!mcp)
  198. {
  199. applog(LOG_ERR, "%"PRIpreprv": Failed to open mcp2210 device", cgpu->proc_repr);
  200. return false;
  201. }
  202. if (!nanofury_checkport(mcp))
  203. {
  204. applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
  205. mcp2210_close(mcp);
  206. return false;
  207. }
  208. port = malloc(sizeof(*port));
  209. bitfury = malloc(sizeof(*bitfury));
  210. if (!(port && bitfury))
  211. {
  212. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate spi_port and bitfury_device structures", cgpu->proc_repr);
  213. free(port);
  214. free(bitfury);
  215. mcp2210_close(mcp);
  216. return false;
  217. }
  218. thr->cgpu_data = mcp;
  219. *port = (struct spi_port){
  220. .txrx = nanofury_spi_txrx,
  221. .cgpu = cgpu,
  222. .repr = cgpu->proc_repr,
  223. .logprio = LOG_ERR,
  224. };
  225. *bitfury = (struct bitfury_device){
  226. .spi = port,
  227. };
  228. cgpu->device_data = bitfury;
  229. bitfury->osc6_bits = 50;
  230. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  231. bitfury_init_chip(cgpu);
  232. timer_set_now(&thr->tv_poll);
  233. cgpu->status = LIFE_INIT2;
  234. return true;
  235. }
  236. static
  237. void nanofury_disable(struct thr_info * const thr)
  238. {
  239. struct mcp2210_device * const mcp = thr->cgpu_data;
  240. bitfury_disable(thr);
  241. nanofury_device_off(mcp);
  242. }
  243. static
  244. void nanofury_enable(struct thr_info * const thr)
  245. {
  246. struct mcp2210_device * const mcp = thr->cgpu_data;
  247. nanofury_checkport(mcp);
  248. bitfury_enable(thr);
  249. }
  250. static
  251. void nanofury_shutdown(struct thr_info * const thr)
  252. {
  253. struct mcp2210_device * const mcp = thr->cgpu_data;
  254. nanofury_device_off(mcp);
  255. }
  256. struct device_drv nanofury_drv = {
  257. .dname = "nanofury",
  258. .name = "NFY",
  259. .drv_detect = nanofury_detect,
  260. .thread_init = nanofury_init,
  261. .thread_disable = nanofury_disable,
  262. .thread_enable = nanofury_enable,
  263. .thread_shutdown = nanofury_shutdown,
  264. .minerloop = minerloop_async,
  265. .job_prepare = bitfury_job_prepare,
  266. .job_start = bitfury_noop_job_start,
  267. .poll = bitfury_do_io,
  268. .job_process_results = bitfury_job_process_results,
  269. .get_api_extra_device_detail = bitfury_api_device_detail,
  270. .get_api_extra_device_status = bitfury_api_device_status,
  271. .set_device = bitfury_set_device,
  272. #ifdef HAVE_CURSES
  273. .proc_wlogprint_status = bitfury_wlogprint_status,
  274. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  275. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  276. #endif
  277. };