driver-nanofury.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "libbitfury.h"
  16. #include "logging.h"
  17. #include "lowlevel.h"
  18. #include "mcp2210.h"
  19. #include "miner.h"
  20. #include "util.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. BFG_REGISTER_DRIVER(nanofury_drv)
  27. struct nanofury_state {
  28. struct mcp2210_device *mcp;
  29. struct timeval identify_started;
  30. bool identify_requested;
  31. };
  32. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  33. static
  34. bool nanofury_spi_reset(struct mcp2210_device * const mcp)
  35. {
  36. int r;
  37. char tx[1] = {0x81}; // will send this waveform: - _ _ _ _ _ _ -
  38. char buf[1];
  39. // SCK_OVRRIDE
  40. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_SCK_OVR, MGV_HIGH))
  41. return false;
  42. for (r = 0; r < 16; ++r)
  43. if (!mcp2210_spi_transfer(mcp, tx, buf, 1))
  44. return false;
  45. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) == MGV_ERROR)
  46. return false;
  47. return true;
  48. }
  49. static void nanofury_device_off(struct mcp2210_device *);
  50. static
  51. bool nanofury_spi_txrx(struct spi_port * const port)
  52. {
  53. struct cgpu_info * const cgpu = port->cgpu;
  54. struct thr_info * const thr = cgpu->thr[0];
  55. struct nanofury_state * const state = thr->cgpu_data;
  56. struct mcp2210_device * const mcp = state->mcp;
  57. const void *wrbuf = spi_gettxbuf(port);
  58. void *rdbuf = spi_getrxbuf(port);
  59. size_t bufsz = spi_getbufsz(port);
  60. const uint8_t *ptrwrbuf = wrbuf;
  61. uint8_t *ptrrdbuf = rdbuf;
  62. nanofury_spi_reset(mcp);
  63. // start by sending chunks of 60 bytes...
  64. while (bufsz >= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER)
  65. {
  66. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, NANOFURY_MAX_BYTES_PER_SPI_TRANSFER))
  67. goto err;
  68. ptrrdbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  69. ptrwrbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  70. bufsz -= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  71. }
  72. // send any remaining bytes...
  73. if (bufsz > 0)
  74. {
  75. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, bufsz))
  76. goto err;
  77. }
  78. return true;
  79. err:
  80. mcp2210_spi_cancel(mcp);
  81. nanofury_device_off(mcp);
  82. hashes_done2(thr, -1, NULL);
  83. return false;
  84. }
  85. static
  86. void nanofury_device_off(struct mcp2210_device * const mcp)
  87. {
  88. // Try to reset everything back to input
  89. for (int i = 0; i < 9; ++i)
  90. mcp2210_get_gpio_input(mcp, i);
  91. }
  92. static
  93. bool nanofury_checkport(struct mcp2210_device * const mcp)
  94. {
  95. int i;
  96. const char tmp = 0;
  97. char tmprx;
  98. // default: set everything to input
  99. for (i = 0; i < 9; ++i)
  100. if (MGV_ERROR == mcp2210_get_gpio_input(mcp, i))
  101. goto fail;
  102. // configure the pins that we need:
  103. // LED
  104. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_HIGH))
  105. goto fail;
  106. // PWR_EN
  107. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, MGV_HIGH))
  108. goto fail;
  109. // cancel any outstanding SPI transfers
  110. mcp2210_spi_cancel(mcp);
  111. // configure SPI
  112. // This is the only place where speed, mode and other settings are configured!!!
  113. if (!mcp2210_configure_spi(mcp, 200000, 0xffff, 0xffef, 0, 0, 0))
  114. goto fail;
  115. if (!mcp2210_set_spimode(mcp, 0))
  116. goto fail;
  117. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  118. goto fail;
  119. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 0 should be 0)
  120. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  121. goto fail;
  122. // switch SCK to polarity (default SCK=1 in mode 2)
  123. if (!mcp2210_set_spimode(mcp, 2))
  124. goto fail;
  125. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  126. goto fail;
  127. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 2 should be 1)
  128. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_HIGH)
  129. goto fail;
  130. // switch SCK to polarity (default SCK=0 in mode 0)
  131. if (!mcp2210_set_spimode(mcp, 0))
  132. goto fail;
  133. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  134. goto fail;
  135. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  136. goto fail;
  137. return true;
  138. fail:
  139. nanofury_device_off(mcp);
  140. return false;
  141. }
  142. static
  143. bool nanofury_lowl_match(const struct lowlevel_device_info * const info)
  144. {
  145. return lowlevel_match_lowlproduct(info, &lowl_mcp2210, NANOFURY_USB_PRODUCT);
  146. }
  147. static
  148. bool nanofury_lowl_probe(const struct lowlevel_device_info * const info)
  149. {
  150. const char * const product = info->product;
  151. const char * const serial = info->serial;
  152. struct mcp2210_device *mcp;
  153. if (info->lowl != &lowl_mcp2210)
  154. {
  155. if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
  156. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
  157. __func__, product, serial);
  158. return false;
  159. }
  160. mcp = mcp2210_open(info);
  161. if (!mcp)
  162. {
  163. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
  164. __func__, product, serial);
  165. return false;
  166. }
  167. if (!nanofury_checkport(mcp))
  168. {
  169. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
  170. __func__, product, serial);
  171. mcp2210_close(mcp);
  172. return false;
  173. }
  174. nanofury_device_off(mcp);
  175. mcp2210_close(mcp);
  176. if (lowlevel_claim(&nanofury_drv, true, info))
  177. return false;
  178. struct cgpu_info *cgpu;
  179. cgpu = malloc(sizeof(*cgpu));
  180. *cgpu = (struct cgpu_info){
  181. .drv = &nanofury_drv,
  182. .device_data = lowlevel_ref(info),
  183. .threads = 1,
  184. // TODO: .name
  185. .device_path = strdup(info->path),
  186. .dev_manufacturer = maybe_strdup(info->manufacturer),
  187. .dev_product = maybe_strdup(product),
  188. .dev_serial = maybe_strdup(serial),
  189. .deven = DEV_ENABLED,
  190. // TODO: .cutofftemp
  191. };
  192. return add_cgpu(cgpu);
  193. }
  194. static
  195. bool nanofury_init(struct thr_info * const thr)
  196. {
  197. struct cgpu_info * const cgpu = thr->cgpu;
  198. struct lowlevel_device_info * const info = cgpu->device_data;
  199. struct spi_port *port;
  200. struct bitfury_device *bitfury;
  201. struct mcp2210_device *mcp;
  202. struct nanofury_state *state;
  203. mcp = mcp2210_open(info);
  204. lowlevel_devinfo_free(info);
  205. if (!mcp)
  206. {
  207. applog(LOG_ERR, "%"PRIpreprv": Failed to open mcp2210 device", cgpu->proc_repr);
  208. return false;
  209. }
  210. if (!nanofury_checkport(mcp))
  211. {
  212. applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
  213. mcp2210_close(mcp);
  214. return false;
  215. }
  216. port = malloc(sizeof(*port));
  217. bitfury = malloc(sizeof(*bitfury));
  218. state = malloc(sizeof(*state));
  219. if (!(port && bitfury && state))
  220. {
  221. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate structures", cgpu->proc_repr);
  222. free(port);
  223. free(bitfury);
  224. free(state);
  225. mcp2210_close(mcp);
  226. return false;
  227. }
  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. *state = (struct nanofury_state){
  238. .mcp = mcp,
  239. };
  240. cgpu->device_data = bitfury;
  241. thr->cgpu_data = state;
  242. bitfury->osc6_bits = 50;
  243. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  244. bitfury_init_chip(cgpu);
  245. timer_set_now(&thr->tv_poll);
  246. cgpu->status = LIFE_INIT2;
  247. return true;
  248. }
  249. static
  250. void nanofury_disable(struct thr_info * const thr)
  251. {
  252. struct nanofury_state * const state = thr->cgpu_data;
  253. struct mcp2210_device * const mcp = state->mcp;
  254. bitfury_disable(thr);
  255. nanofury_device_off(mcp);
  256. }
  257. static
  258. void nanofury_enable(struct thr_info * const thr)
  259. {
  260. struct nanofury_state * const state = thr->cgpu_data;
  261. struct mcp2210_device * const mcp = state->mcp;
  262. nanofury_checkport(mcp);
  263. bitfury_enable(thr);
  264. }
  265. static
  266. void nanofury_reinit(struct cgpu_info * const cgpu)
  267. {
  268. struct thr_info * const thr = cgpu->thr[0];
  269. struct nanofury_state * const state = thr->cgpu_data;
  270. struct mcp2210_device * const mcp = state->mcp;
  271. nanofury_device_off(mcp);
  272. cgsleep_ms(1);
  273. nanofury_enable(thr);
  274. }
  275. static
  276. void nanofury_poll(struct thr_info * const thr)
  277. {
  278. struct nanofury_state * const state = thr->cgpu_data;
  279. struct mcp2210_device * const mcp = state->mcp;
  280. if (state->identify_requested)
  281. {
  282. if (!timer_isset(&state->identify_started))
  283. // LED is normally on while mining, so turn it off for identify
  284. mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_LOW);
  285. timer_set_delay_from_now(&state->identify_started, 5000000);
  286. state->identify_requested = false;
  287. }
  288. bitfury_do_io(thr);
  289. if (timer_passed(&state->identify_started, NULL))
  290. {
  291. mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_HIGH);
  292. timer_unset(&state->identify_started);
  293. }
  294. }
  295. static
  296. bool nanofury_identify(struct cgpu_info * const cgpu)
  297. {
  298. struct nanofury_state * const state = cgpu->thr[0]->cgpu_data;
  299. state->identify_requested = true;
  300. return true;
  301. }
  302. static
  303. void nanofury_shutdown(struct thr_info * const thr)
  304. {
  305. struct nanofury_state * const state = thr->cgpu_data;
  306. struct mcp2210_device * const mcp = state->mcp;
  307. if (mcp)
  308. nanofury_device_off(mcp);
  309. }
  310. struct device_drv nanofury_drv = {
  311. .dname = "nanofury",
  312. .name = "NFY",
  313. .lowl_match = nanofury_lowl_match,
  314. .lowl_probe = nanofury_lowl_probe,
  315. .thread_init = nanofury_init,
  316. .thread_disable = nanofury_disable,
  317. .thread_enable = nanofury_enable,
  318. .reinit_device = nanofury_reinit,
  319. .thread_shutdown = nanofury_shutdown,
  320. .minerloop = minerloop_async,
  321. .job_prepare = bitfury_job_prepare,
  322. .job_start = bitfury_noop_job_start,
  323. .poll = nanofury_poll,
  324. .job_process_results = bitfury_job_process_results,
  325. .get_api_extra_device_detail = bitfury_api_device_detail,
  326. .get_api_extra_device_status = bitfury_api_device_status,
  327. .set_device = bitfury_set_device,
  328. .identify_device = nanofury_identify,
  329. #ifdef HAVE_CURSES
  330. .proc_wlogprint_status = bitfury_wlogprint_status,
  331. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  332. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  333. #endif
  334. };