driver-nanofury.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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. static const struct bfg_set_device_definition nanofury_set_device_funcs[];
  28. struct nanofury_state {
  29. struct lowlevel_device_info *lowl_info;
  30. struct mcp2210_device *mcp;
  31. struct timeval identify_started;
  32. bool identify_requested;
  33. unsigned long current_baud;
  34. bool ledalternating;
  35. bool ledvalue;
  36. };
  37. // Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
  38. static
  39. bool nanofury_spi_reset(struct mcp2210_device * const mcp)
  40. {
  41. int r;
  42. char tx[1] = {0x81}; // will send this waveform: - _ _ _ _ _ _ -
  43. char buf[1];
  44. // SCK_OVRRIDE
  45. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_SCK_OVR, MGV_HIGH))
  46. return false;
  47. for (r = 0; r < 16; ++r)
  48. if (!mcp2210_spi_transfer(mcp, tx, buf, 1))
  49. return false;
  50. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) == MGV_ERROR)
  51. return false;
  52. return true;
  53. }
  54. static void nanofury_device_off(struct mcp2210_device *);
  55. static
  56. bool nanofury_spi_txrx(struct spi_port * const port)
  57. {
  58. struct cgpu_info * const cgpu = port->cgpu;
  59. struct nanofury_state * const state = port->userp;
  60. struct mcp2210_device * const mcp = state->mcp;
  61. const void *wrbuf = spi_gettxbuf(port);
  62. void *rdbuf = spi_getrxbuf(port);
  63. size_t bufsz = spi_getbufsz(port);
  64. const uint8_t *ptrwrbuf = wrbuf;
  65. uint8_t *ptrrdbuf = rdbuf;
  66. if (state->current_baud != port->speed)
  67. {
  68. applog(LOG_NOTICE, "%"PRIpreprv": Changing baud from %lu to %lu",
  69. cgpu ? cgpu->proc_repr : nanofury_drv.dname,
  70. (unsigned long)state->current_baud, (unsigned long)port->speed);
  71. if (!mcp2210_configure_spi(mcp, port->speed, 0xffff, 0xffef, 0, 0, 0))
  72. goto err;
  73. state->current_baud = port->speed;
  74. }
  75. nanofury_spi_reset(mcp);
  76. // start by sending chunks of 60 bytes...
  77. while (bufsz >= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER)
  78. {
  79. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, NANOFURY_MAX_BYTES_PER_SPI_TRANSFER))
  80. goto err;
  81. ptrrdbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  82. ptrwrbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  83. bufsz -= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
  84. }
  85. // send any remaining bytes...
  86. if (bufsz > 0)
  87. {
  88. if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, bufsz))
  89. goto err;
  90. }
  91. return true;
  92. err:
  93. mcp2210_spi_cancel(mcp);
  94. nanofury_device_off(mcp);
  95. if (cgpu)
  96. {
  97. struct thr_info * const thr = cgpu->thr[0];
  98. hashes_done2(thr, -1, NULL);
  99. }
  100. return false;
  101. }
  102. static
  103. void nanofury_send_led_gpio(struct nanofury_state * const state)
  104. {
  105. struct mcp2210_device * const mcp = state->mcp;
  106. mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? MGV_HIGH : MGV_LOW);
  107. }
  108. static
  109. void nanofury_do_led_alternating(struct nanofury_state * const state)
  110. {
  111. state->ledvalue = !state->ledvalue;
  112. nanofury_send_led_gpio(state);
  113. }
  114. static
  115. void nanofury_device_off(struct mcp2210_device * const mcp)
  116. {
  117. // Try to reset everything back to input
  118. for (int i = 0; i < 9; ++i)
  119. mcp2210_get_gpio_input(mcp, i);
  120. }
  121. static
  122. bool nanofury_checkport(struct mcp2210_device * const mcp, const unsigned long baud)
  123. {
  124. int i;
  125. const char tmp = 0;
  126. char tmprx;
  127. // default: set everything to input
  128. for (i = 0; i < 9; ++i)
  129. if (MGV_ERROR == mcp2210_get_gpio_input(mcp, i))
  130. goto fail;
  131. // configure the pins that we need:
  132. // LED
  133. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, MGV_HIGH))
  134. goto fail;
  135. // PWR_EN
  136. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, MGV_HIGH))
  137. goto fail;
  138. // cancel any outstanding SPI transfers
  139. mcp2210_spi_cancel(mcp);
  140. // configure SPI
  141. // This is the only place where speed, mode and other settings are configured!!!
  142. if (!mcp2210_configure_spi(mcp, baud, 0xffff, 0xffef, 0, 0, 0))
  143. goto fail;
  144. if (!mcp2210_set_spimode(mcp, 0))
  145. goto fail;
  146. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  147. goto fail;
  148. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 0 should be 0)
  149. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  150. goto fail;
  151. // switch SCK to polarity (default SCK=1 in mode 2)
  152. if (!mcp2210_set_spimode(mcp, 2))
  153. goto fail;
  154. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  155. goto fail;
  156. // after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 2 should be 1)
  157. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_HIGH)
  158. goto fail;
  159. // switch SCK to polarity (default SCK=0 in mode 0)
  160. if (!mcp2210_set_spimode(mcp, 0))
  161. goto fail;
  162. if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
  163. goto fail;
  164. if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != MGV_LOW)
  165. goto fail;
  166. return true;
  167. fail:
  168. nanofury_device_off(mcp);
  169. return false;
  170. }
  171. static
  172. bool nanofury_lowl_match(const struct lowlevel_device_info * const info)
  173. {
  174. return lowlevel_match_lowlproduct(info, &lowl_mcp2210, NANOFURY_USB_PRODUCT);
  175. }
  176. static
  177. bool nanofury_lowl_probe(const struct lowlevel_device_info * const info)
  178. {
  179. const char * const product = info->product;
  180. const char * const serial = info->serial;
  181. struct mcp2210_device *mcp;
  182. struct spi_port *port;
  183. struct nanofury_state *state;
  184. int chips;
  185. if (info->lowl != &lowl_mcp2210)
  186. {
  187. if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
  188. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
  189. __func__, product, serial);
  190. return false;
  191. }
  192. mcp = mcp2210_open(info);
  193. if (!mcp)
  194. {
  195. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
  196. __func__, product, serial);
  197. return false;
  198. }
  199. state = malloc(sizeof(*state));
  200. *state = (struct nanofury_state){
  201. .mcp = mcp,
  202. .ledvalue = true,
  203. };
  204. port = calloc(1, sizeof(*port));
  205. port->userp = state;
  206. port->txrx = nanofury_spi_txrx;
  207. port->repr = nanofury_drv.dname;
  208. port->logprio = LOG_DEBUG;
  209. port->speed = 200000;
  210. {
  211. struct bitfury_device dummy_bitfury = {
  212. .spi = sys_spi,
  213. };
  214. drv_set_defaults(&nanofury_drv, bitfury_set_device_funcs_probe, &dummy_bitfury, NULL, NULL, 1);
  215. }
  216. if (!nanofury_checkport(mcp, port->speed))
  217. {
  218. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
  219. __func__, product, serial);
  220. mcp2210_close(mcp);
  221. return false;
  222. }
  223. state->current_baud = port->speed;
  224. chips = libbitfury_detectChips1(port);
  225. free(port);
  226. nanofury_device_off(mcp);
  227. mcp2210_close(mcp);
  228. if (lowlevel_claim(&nanofury_drv, true, info))
  229. {
  230. free(state);
  231. return false;
  232. }
  233. state->lowl_info = lowlevel_ref(info);
  234. struct cgpu_info *cgpu;
  235. cgpu = malloc(sizeof(*cgpu));
  236. *cgpu = (struct cgpu_info){
  237. .drv = &nanofury_drv,
  238. .set_device_funcs = nanofury_set_device_funcs,
  239. .device_data = state,
  240. .threads = 1,
  241. .procs = chips,
  242. // TODO: .name
  243. .device_path = strdup(info->path),
  244. .dev_manufacturer = maybe_strdup(info->manufacturer),
  245. .dev_product = maybe_strdup(product),
  246. .dev_serial = maybe_strdup(serial),
  247. .deven = DEV_ENABLED,
  248. // TODO: .cutofftemp
  249. };
  250. return add_cgpu(cgpu);
  251. }
  252. static
  253. bool nanofury_init(struct thr_info * const thr)
  254. {
  255. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  256. struct nanofury_state * const state = cgpu->device_data;
  257. struct lowlevel_device_info * const info = state->lowl_info;
  258. struct spi_port *port;
  259. struct bitfury_device *bitfury;
  260. struct mcp2210_device *mcp;
  261. mcp = mcp2210_open(info);
  262. lowlevel_devinfo_free(info);
  263. if (!mcp)
  264. {
  265. applog(LOG_ERR, "%"PRIpreprv": Failed to open mcp2210 device", cgpu->proc_repr);
  266. return false;
  267. }
  268. if (!nanofury_checkport(mcp, state->current_baud))
  269. {
  270. applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
  271. mcp2210_close(mcp);
  272. return false;
  273. }
  274. port = malloc(sizeof(*port));
  275. bitfury = malloc(sizeof(*bitfury) * cgpu->procs);
  276. if (!(port && bitfury && state))
  277. {
  278. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate structures", cgpu->proc_repr);
  279. free(port);
  280. free(bitfury);
  281. free(state);
  282. mcp2210_close(mcp);
  283. return false;
  284. }
  285. /* Be careful, read spidevc.h comments for warnings */
  286. memset(port, 0, sizeof(*port));
  287. port->txrx = nanofury_spi_txrx;
  288. port->cgpu = cgpu;
  289. port->repr = cgpu->proc_repr;
  290. port->logprio = LOG_ERR;
  291. port->speed = state->current_baud;
  292. state->mcp = mcp;
  293. thr->cgpu_data = state;
  294. port->userp = state;
  295. for (proc = cgpu; proc; (proc = proc->next_proc), ++bitfury)
  296. {
  297. *bitfury = (struct bitfury_device){
  298. .spi = port,
  299. .fasync = proc->proc_id,
  300. };
  301. proc->device_data = bitfury;
  302. bitfury->osc6_bits = 50;
  303. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  304. bitfury_init_chip(proc);
  305. proc->status = LIFE_INIT2;
  306. }
  307. nanofury_send_led_gpio(state);
  308. timer_set_now(&thr->tv_poll);
  309. return true;
  310. }
  311. static
  312. void nanofury_disable(struct thr_info * const thr)
  313. {
  314. struct nanofury_state * const state = thr->cgpu_data;
  315. struct mcp2210_device * const mcp = state->mcp;
  316. bitfury_disable(thr);
  317. nanofury_device_off(mcp);
  318. }
  319. static
  320. void nanofury_enable(struct thr_info * const thr)
  321. {
  322. struct nanofury_state * const state = thr->cgpu_data;
  323. struct mcp2210_device * const mcp = state->mcp;
  324. nanofury_checkport(mcp, state->current_baud);
  325. nanofury_send_led_gpio(state);
  326. bitfury_enable(thr);
  327. }
  328. static
  329. void nanofury_reinit(struct cgpu_info * const cgpu)
  330. {
  331. struct thr_info * const thr = cgpu->thr[0];
  332. struct nanofury_state * const state = thr->cgpu_data;
  333. struct mcp2210_device * const mcp = state->mcp;
  334. nanofury_device_off(mcp);
  335. cgsleep_ms(1);
  336. nanofury_enable(thr);
  337. }
  338. static
  339. double _nanofury_total_diff1(struct cgpu_info * const dev)
  340. {
  341. double d = 0.;
  342. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  343. d += proc->diff1;
  344. return d;
  345. }
  346. static
  347. void nanofury_poll(struct thr_info * const thr)
  348. {
  349. struct cgpu_info * const dev = thr->cgpu;
  350. struct nanofury_state * const state = thr->cgpu_data;
  351. struct mcp2210_device * const mcp = state->mcp;
  352. double diff1_before;
  353. if (state->identify_requested)
  354. {
  355. if (!timer_isset(&state->identify_started))
  356. mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? MGV_LOW : MGV_HIGH);
  357. timer_set_delay_from_now(&state->identify_started, 5000000);
  358. state->identify_requested = false;
  359. }
  360. if (state->ledalternating && !timer_isset(&state->identify_started))
  361. diff1_before = _nanofury_total_diff1(dev);
  362. bitfury_do_io(thr);
  363. if (state->ledalternating && (timer_isset(&state->identify_started) || diff1_before != _nanofury_total_diff1(dev)))
  364. nanofury_do_led_alternating(state);
  365. if (timer_passed(&state->identify_started, NULL))
  366. {
  367. // Also used when setting ledmode
  368. nanofury_send_led_gpio(state);
  369. timer_unset(&state->identify_started);
  370. }
  371. }
  372. static
  373. bool nanofury_identify(struct cgpu_info * const cgpu)
  374. {
  375. struct nanofury_state * const state = cgpu->thr[0]->cgpu_data;
  376. state->identify_requested = true;
  377. return true;
  378. }
  379. static
  380. void nanofury_shutdown(struct thr_info * const thr)
  381. {
  382. struct nanofury_state * const state = thr->cgpu_data;
  383. struct mcp2210_device * const mcp = state->mcp;
  384. if (mcp)
  385. nanofury_device_off(mcp);
  386. }
  387. const char *nanofury_set_ledmode(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  388. {
  389. struct thr_info * const thr = proc->thr[0];
  390. struct nanofury_state * const state = thr->cgpu_data;
  391. if (!strcasecmp(setting, "on"))
  392. {
  393. state->ledvalue = true;
  394. state->ledalternating = false;
  395. }
  396. else
  397. if (!strcasecmp(setting, "off"))
  398. state->ledvalue = state->ledalternating = false;
  399. else
  400. if (!strcasecmp(setting, "alternating"))
  401. state->ledalternating = true;
  402. else
  403. return "Invalid LED mode; must be on/off/alternating";
  404. if (!timer_isset(&state->identify_started))
  405. timer_set_now(&state->identify_started);
  406. return NULL;
  407. }
  408. static const struct bfg_set_device_definition nanofury_set_device_funcs[] = {
  409. {"baud", bitfury_set_baud, "SPI baud rate"},
  410. {"osc6_bits", bitfury_set_osc6_bits, "range 1-"BITFURY_MAX_OSC6_BITS_S" (slow to fast)"},
  411. {"ledmode", nanofury_set_ledmode, "on/off/alternating"},
  412. {NULL},
  413. };
  414. struct device_drv nanofury_drv = {
  415. .dname = "nanofury",
  416. .name = "NFY",
  417. .lowl_match = nanofury_lowl_match,
  418. .lowl_probe = nanofury_lowl_probe,
  419. .thread_init = nanofury_init,
  420. .thread_disable = nanofury_disable,
  421. .thread_enable = nanofury_enable,
  422. .reinit_device = nanofury_reinit,
  423. .thread_shutdown = nanofury_shutdown,
  424. .minerloop = minerloop_async,
  425. .job_prepare = bitfury_job_prepare,
  426. .job_start = bitfury_noop_job_start,
  427. .poll = nanofury_poll,
  428. .job_process_results = bitfury_job_process_results,
  429. .get_api_extra_device_detail = bitfury_api_device_detail,
  430. .get_api_extra_device_status = bitfury_api_device_status,
  431. .identify_device = nanofury_identify,
  432. #ifdef HAVE_CURSES
  433. .proc_wlogprint_status = bitfury_wlogprint_status,
  434. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  435. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  436. #endif
  437. };