driver-nanofury.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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
  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. return false;
  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. return false;
  71. }
  72. return true;
  73. }
  74. static
  75. void nanofury_device_off(struct mcp2210_device * const mcp)
  76. {
  77. // Try to reset everything back to input
  78. for (int i = 0; i < 9; ++i)
  79. mcp2210_get_gpio_input(mcp, i);
  80. }
  81. static
  82. bool nanofury_checkport(struct mcp2210_device * const mcp)
  83. {
  84. int i;
  85. const char tmp = 0;
  86. char tmprx;
  87. // default: set everything to input
  88. for (i = 0; i < 9; ++i)
  89. if (MGV_ERROR == mcp2210_get_gpio_input(mcp, i))
  90. goto fail;
  91. // configure the pins that we need:
  92. // LED
  93. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_HIGH))
  94. goto fail;
  95. // PWR_EN
  96. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, MGV_HIGH))
  97. goto fail;
  98. // configure SPI
  99. // This is the only place where speed, mode and other settings are configured!!!
  100. if (!mcp2210_configure_spi(mcp, 200000, 0xffff, 0xffef, 0, 0, 0))
  101. goto fail;
  102. if (!mcp2210_set_spimode(mcp, 0))
  103. goto fail;
  104. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  105. goto fail;
  106. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 0 should be 0)
  107. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  108. goto fail;
  109. // switch SCK to polarity (default SCK=1 in mode 2)
  110. if (!mcp2210_set_spimode(mcp, 2))
  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 2 should be 1)
  115. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_HIGH)
  116. goto fail;
  117. // switch SCK to polarity (default SCK=0 in mode 0)
  118. if (!mcp2210_set_spimode(mcp, 0))
  119. goto fail;
  120. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  121. goto fail;
  122. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  123. goto fail;
  124. return true;
  125. fail:
  126. nanofury_device_off(mcp);
  127. return false;
  128. }
  129. static
  130. bool nanofury_foundlowl(struct lowlevel_device_info * const info, __maybe_unused void *userp)
  131. {
  132. const char * const product = info->product;
  133. const char * const serial = info->serial;
  134. struct mcp2210_device *mcp;
  135. if (info->lowl != &lowl_mcp2210)
  136. {
  137. if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
  138. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
  139. __func__, product, serial);
  140. return false;
  141. }
  142. mcp = mcp2210_open(info);
  143. if (!mcp)
  144. {
  145. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
  146. __func__, product, serial);
  147. return false;
  148. }
  149. if (!nanofury_checkport(mcp))
  150. {
  151. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
  152. __func__, product, serial);
  153. mcp2210_close(mcp);
  154. return false;
  155. }
  156. nanofury_device_off(mcp);
  157. mcp2210_close(mcp);
  158. if (bfg_claim_hid(&nanofury_drv, true, info->path))
  159. return false;
  160. struct cgpu_info *cgpu;
  161. cgpu = malloc(sizeof(*cgpu));
  162. *cgpu = (struct cgpu_info){
  163. .drv = &nanofury_drv,
  164. .device_data = info,
  165. .threads = 1,
  166. // TODO: .name
  167. .device_path = strdup(info->path),
  168. .dev_manufacturer = maybe_strdup(info->manufacturer),
  169. .dev_product = maybe_strdup(product),
  170. .dev_serial = maybe_strdup(serial),
  171. .deven = DEV_ENABLED,
  172. // TODO: .cutofftemp
  173. };
  174. return add_cgpu(cgpu);
  175. }
  176. static bool nanofury_detect_one(const char *serial)
  177. {
  178. return lowlevel_detect_serial(nanofury_foundlowl, serial);
  179. }
  180. static int nanofury_detect_auto()
  181. {
  182. return lowlevel_detect(nanofury_foundlowl, NANOFURY_USB_PRODUCT);
  183. }
  184. static void nanofury_detect()
  185. {
  186. serial_detect_auto(&nanofury_drv, nanofury_detect_one, nanofury_detect_auto);
  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_shutdown(struct thr_info * const thr)
  253. {
  254. struct mcp2210_device * const mcp = thr->cgpu_data;
  255. nanofury_device_off(mcp);
  256. }
  257. struct device_drv nanofury_drv = {
  258. .dname = "nanofury",
  259. .name = "NFY",
  260. .drv_detect = nanofury_detect,
  261. .thread_init = nanofury_init,
  262. .thread_disable = nanofury_disable,
  263. .thread_enable = nanofury_enable,
  264. .thread_shutdown = nanofury_shutdown,
  265. .minerloop = minerloop_async,
  266. .job_prepare = bitfury_job_prepare,
  267. .job_start = bitfury_noop_job_start,
  268. .poll = bitfury_do_io,
  269. .job_process_results = bitfury_job_process_results,
  270. .get_api_extra_device_detail = bitfury_api_device_detail,
  271. .get_api_extra_device_status = bitfury_api_device_status,
  272. .set_device = bitfury_set_device,
  273. #ifdef HAVE_CURSES
  274. .proc_wlogprint_status = bitfury_wlogprint_status,
  275. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  276. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  277. #endif
  278. };