driver-nanofury.c 15 KB

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