driver-nanofury.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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_foundlowl(struct lowlevel_device_info * const info, __maybe_unused void *userp)
  139. {
  140. const char * const product = info->product;
  141. const char * const serial = info->serial;
  142. struct mcp2210_device *mcp;
  143. if (info->lowl != &lowl_mcp2210)
  144. {
  145. if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
  146. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
  147. __func__, product, serial);
  148. return false;
  149. }
  150. mcp = mcp2210_open(info);
  151. if (!mcp)
  152. {
  153. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
  154. __func__, product, serial);
  155. return false;
  156. }
  157. if (!nanofury_checkport(mcp))
  158. {
  159. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
  160. __func__, product, serial);
  161. mcp2210_close(mcp);
  162. return false;
  163. }
  164. nanofury_device_off(mcp);
  165. mcp2210_close(mcp);
  166. if (bfg_claim_hid(&nanofury_drv, true, info->path))
  167. return false;
  168. struct cgpu_info *cgpu;
  169. cgpu = malloc(sizeof(*cgpu));
  170. *cgpu = (struct cgpu_info){
  171. .drv = &nanofury_drv,
  172. .device_data = info,
  173. .threads = 1,
  174. // TODO: .name
  175. .device_path = strdup(info->path),
  176. .dev_manufacturer = maybe_strdup(info->manufacturer),
  177. .dev_product = maybe_strdup(product),
  178. .dev_serial = maybe_strdup(serial),
  179. .deven = DEV_ENABLED,
  180. // TODO: .cutofftemp
  181. };
  182. return add_cgpu(cgpu);
  183. }
  184. static bool nanofury_detect_one(const char *serial)
  185. {
  186. return lowlevel_detect_serial(nanofury_foundlowl, serial);
  187. }
  188. static int nanofury_detect_auto()
  189. {
  190. return lowlevel_detect(nanofury_foundlowl, NANOFURY_USB_PRODUCT);
  191. }
  192. static void nanofury_detect()
  193. {
  194. serial_detect_auto(&nanofury_drv, nanofury_detect_one, nanofury_detect_auto);
  195. }
  196. static
  197. bool nanofury_init(struct thr_info * const thr)
  198. {
  199. struct cgpu_info * const cgpu = thr->cgpu;
  200. struct lowlevel_device_info * const info = cgpu->device_data;
  201. struct spi_port *port;
  202. struct bitfury_device *bitfury;
  203. struct mcp2210_device *mcp;
  204. mcp = mcp2210_open(info);
  205. lowlevel_devinfo_free(info);
  206. if (!mcp)
  207. {
  208. applog(LOG_ERR, "%"PRIpreprv": Failed to open mcp2210 device", cgpu->proc_repr);
  209. return false;
  210. }
  211. if (!nanofury_checkport(mcp))
  212. {
  213. applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
  214. mcp2210_close(mcp);
  215. return false;
  216. }
  217. port = malloc(sizeof(*port));
  218. bitfury = malloc(sizeof(*bitfury));
  219. if (!(port && bitfury))
  220. {
  221. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate spi_port and bitfury_device structures", cgpu->proc_repr);
  222. free(port);
  223. free(bitfury);
  224. mcp2210_close(mcp);
  225. return false;
  226. }
  227. thr->cgpu_data = mcp;
  228. /* Be careful, read spidevc.h comments for warnings */
  229. memset(port, 0, sizeof(*port));
  230. port->txrx = nanofury_spi_txrx;
  231. port->cgpu = cgpu;
  232. port->repr = cgpu->proc_repr;
  233. port->logprio = LOG_ERR;
  234. *bitfury = (struct bitfury_device){
  235. .spi = port,
  236. };
  237. cgpu->device_data = bitfury;
  238. bitfury->osc6_bits = 50;
  239. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  240. bitfury_init_chip(cgpu);
  241. timer_set_now(&thr->tv_poll);
  242. cgpu->status = LIFE_INIT2;
  243. return true;
  244. }
  245. static
  246. void nanofury_disable(struct thr_info * const thr)
  247. {
  248. struct mcp2210_device * const mcp = thr->cgpu_data;
  249. bitfury_disable(thr);
  250. nanofury_device_off(mcp);
  251. }
  252. static
  253. void nanofury_enable(struct thr_info * const thr)
  254. {
  255. struct mcp2210_device * const mcp = thr->cgpu_data;
  256. nanofury_checkport(mcp);
  257. bitfury_enable(thr);
  258. }
  259. static
  260. void nanofury_reinit(struct cgpu_info * const cgpu)
  261. {
  262. struct thr_info * const thr = cgpu->thr[0];
  263. struct mcp2210_device * const mcp = thr->cgpu_data;
  264. nanofury_device_off(mcp);
  265. cgsleep_ms(1);
  266. nanofury_enable(thr);
  267. }
  268. static
  269. void nanofury_shutdown(struct thr_info * const thr)
  270. {
  271. struct mcp2210_device * const mcp = thr->cgpu_data;
  272. if (mcp)
  273. nanofury_device_off(mcp);
  274. }
  275. struct device_drv nanofury_drv = {
  276. .dname = "nanofury",
  277. .name = "NFY",
  278. .drv_detect = nanofury_detect,
  279. .thread_init = nanofury_init,
  280. .thread_disable = nanofury_disable,
  281. .thread_enable = nanofury_enable,
  282. .reinit_device = nanofury_reinit,
  283. .thread_shutdown = nanofury_shutdown,
  284. .minerloop = minerloop_async,
  285. .job_prepare = bitfury_job_prepare,
  286. .job_start = bitfury_noop_job_start,
  287. .poll = bitfury_do_io,
  288. .job_process_results = bitfury_job_process_results,
  289. .get_api_extra_device_detail = bitfury_api_device_detail,
  290. .get_api_extra_device_status = bitfury_api_device_status,
  291. .set_device = bitfury_set_device,
  292. #ifdef HAVE_CURSES
  293. .proc_wlogprint_status = bitfury_wlogprint_status,
  294. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  295. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  296. #endif
  297. };