driver-nanofury.c 10 KB

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