driver-nanofury.c 7.5 KB

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