driver-nanofury.c 7.8 KB

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