driver-nanofury.c 7.8 KB

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