driver-nanofury.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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_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. if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
  201. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
  202. __func__, product, serial);
  203. return false;
  204. }
  205. mcp = mcp2210_open(info);
  206. if (!mcp)
  207. {
  208. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
  209. __func__, product, serial);
  210. return false;
  211. }
  212. state = malloc(sizeof(*state));
  213. *state = (struct nanofury_state){
  214. .mcp = mcp,
  215. .ledvalue = true,
  216. };
  217. port = calloc(1, sizeof(*port));
  218. port->userp = state;
  219. port->txrx = nanofury_spi_txrx;
  220. port->repr = nanofury_drv.dname;
  221. port->logprio = LOG_DEBUG;
  222. port->speed = 200000;
  223. {
  224. struct bitfury_device dummy_bitfury = {
  225. .spi = port,
  226. };
  227. drv_set_defaults(&nanofury_drv, bitfury_set_device_funcs_probe, &dummy_bitfury, NULL, NULL, 1);
  228. }
  229. if (!nanofury_checkport(mcp, port->speed, NULL))
  230. {
  231. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
  232. __func__, product, serial);
  233. mcp2210_close(mcp);
  234. return false;
  235. }
  236. state->current_baud = port->speed;
  237. chips = libbitfury_detectChips1(port);
  238. free(port);
  239. nanofury_device_off(mcp, NULL);
  240. mcp2210_close(mcp);
  241. if (lowlevel_claim(&nanofury_drv, true, info))
  242. {
  243. free(state);
  244. return false;
  245. }
  246. state->lowl_info = lowlevel_ref(info);
  247. struct cgpu_info *cgpu;
  248. cgpu = malloc(sizeof(*cgpu));
  249. *cgpu = (struct cgpu_info){
  250. .drv = &nanofury_drv,
  251. .set_device_funcs = nanofury_set_device_funcs,
  252. .device_data = state,
  253. .threads = 1,
  254. .procs = chips,
  255. // TODO: .name
  256. .device_path = strdup(info->path),
  257. .dev_manufacturer = maybe_strdup(info->manufacturer),
  258. .dev_product = maybe_strdup(product),
  259. .dev_serial = maybe_strdup(serial),
  260. .deven = DEV_ENABLED,
  261. // TODO: .cutofftemp
  262. };
  263. return add_cgpu(cgpu);
  264. }
  265. static
  266. bool nanofury_init(struct thr_info * const thr)
  267. {
  268. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  269. struct nanofury_state * const state = cgpu->device_data;
  270. struct lowlevel_device_info * const info = state->lowl_info;
  271. struct spi_port *port;
  272. struct bitfury_device *bitfury;
  273. struct mcp2210_device *mcp;
  274. mcp = mcp2210_open(info);
  275. lowlevel_devinfo_free(info);
  276. if (!mcp)
  277. {
  278. applog(LOG_ERR, "%"PRIpreprv": Failed to open mcp2210 device", cgpu->proc_repr);
  279. return false;
  280. }
  281. if (!nanofury_checkport(mcp, state->current_baud, state))
  282. {
  283. applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
  284. mcp2210_close(mcp);
  285. return false;
  286. }
  287. port = malloc(sizeof(*port));
  288. bitfury = malloc(sizeof(*bitfury) * cgpu->procs);
  289. if (!(port && bitfury && state))
  290. {
  291. applog(LOG_ERR, "%"PRIpreprv": Failed to allocate structures", cgpu->proc_repr);
  292. free(port);
  293. free(bitfury);
  294. free(state);
  295. mcp2210_close(mcp);
  296. return false;
  297. }
  298. /* Be careful, read lowl-spi.h comments for warnings */
  299. memset(port, 0, sizeof(*port));
  300. port->txrx = nanofury_spi_txrx;
  301. port->cgpu = cgpu;
  302. port->repr = cgpu->proc_repr;
  303. port->logprio = LOG_ERR;
  304. port->speed = state->current_baud;
  305. const int init_osc6_bits = 50;
  306. const int ramp_osc6_bits = (cgpu->procs > 1) ? 5 : init_osc6_bits;
  307. state->mcp = mcp;
  308. port->userp = state;
  309. for (proc = cgpu; proc; (proc = proc->next_proc), ++bitfury)
  310. {
  311. struct thr_info * const mythr = proc->thr[0];
  312. *bitfury = (struct bitfury_device){
  313. .spi = port,
  314. .fasync = proc->proc_id,
  315. };
  316. proc->device_data = bitfury;
  317. mythr->cgpu_data = state;
  318. bitfury->osc6_bits = ramp_osc6_bits;
  319. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  320. bitfury_init_chip(proc);
  321. }
  322. --bitfury;
  323. while (bitfury->osc6_bits < init_osc6_bits)
  324. {
  325. for (proc = cgpu; proc; proc = proc->next_proc)
  326. {
  327. bitfury = proc->device_data;
  328. bitfury->osc6_bits += 5;
  329. bitfury_send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  330. }
  331. }
  332. for (proc = cgpu; proc; proc = proc->next_proc)
  333. {
  334. bitfury_init_chip(proc);
  335. proc->status = LIFE_INIT2;
  336. }
  337. nanofury_send_led_gpio(state);
  338. timer_set_now(&thr->tv_poll);
  339. return true;
  340. }
  341. static
  342. void nanofury_disable(struct thr_info * const thr)
  343. {
  344. struct cgpu_info * const proc = thr->cgpu;
  345. struct cgpu_info * const dev = proc->device;
  346. struct nanofury_state * const state = thr->cgpu_data;
  347. struct mcp2210_device * const mcp = state->mcp;
  348. bitfury_disable(thr);
  349. // Before powering off, ensure no other chip needs power
  350. for_each_managed_proc(oproc, dev)
  351. if (oproc->deven == DEV_ENABLED)
  352. return;
  353. applog(LOG_NOTICE, "%s: Last chip disabled, shutting off power",
  354. dev->dev_repr);
  355. nanofury_device_off(mcp, state);
  356. }
  357. static
  358. void nanofury_enable(struct thr_info * const thr)
  359. {
  360. struct cgpu_info * const proc = thr->cgpu;
  361. struct cgpu_info * const dev = proc->device;
  362. struct nanofury_state * const state = thr->cgpu_data;
  363. struct mcp2210_device * const mcp = state->mcp;
  364. if (state->powered_off)
  365. {
  366. // All chips were disabled, so we need to power back on
  367. applog(LOG_DEBUG, "%s: Enabling power",
  368. dev->dev_repr);
  369. nanofury_checkport(mcp, state->current_baud, state);
  370. nanofury_send_led_gpio(state);
  371. }
  372. bitfury_enable(thr);
  373. }
  374. static
  375. void nanofury_reinit(struct cgpu_info * const proc)
  376. {
  377. struct thr_info * const thr = proc->thr[0];
  378. struct cgpu_info * const dev = proc->device;
  379. struct nanofury_state * const state = thr->cgpu_data;
  380. struct mcp2210_device * const mcp = state->mcp;
  381. nanofury_device_off(mcp, state);
  382. cgsleep_ms(1);
  383. for_each_managed_proc(oproc, dev)
  384. if (oproc->deven == DEV_ENABLED)
  385. nanofury_enable(oproc->thr[0]);
  386. }
  387. static
  388. double _nanofury_total_diff1(struct cgpu_info * const dev)
  389. {
  390. double d = 0.;
  391. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  392. d += proc->diff1;
  393. return d;
  394. }
  395. static
  396. void nanofury_poll(struct thr_info * const thr)
  397. {
  398. struct cgpu_info * const dev = thr->cgpu;
  399. struct nanofury_state * const state = thr->cgpu_data;
  400. struct mcp2210_device * const mcp = state->mcp;
  401. double diff1_before = 0.;
  402. if (state->identify_requested)
  403. {
  404. if (!timer_isset(&state->identify_started))
  405. mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? BGV_LOW : BGV_HIGH);
  406. timer_set_delay_from_now(&state->identify_started, 5000000);
  407. state->identify_requested = false;
  408. }
  409. if (state->ledalternating && !timer_isset(&state->identify_started))
  410. diff1_before = _nanofury_total_diff1(dev);
  411. bitfury_do_io(thr);
  412. if (state->ledalternating && (timer_isset(&state->identify_started) || diff1_before != _nanofury_total_diff1(dev)))
  413. nanofury_do_led_alternating(state);
  414. if (timer_passed(&state->identify_started, NULL))
  415. {
  416. // Also used when setting ledmode
  417. nanofury_send_led_gpio(state);
  418. timer_unset(&state->identify_started);
  419. }
  420. }
  421. static
  422. bool nanofury_identify(struct cgpu_info * const cgpu)
  423. {
  424. struct nanofury_state * const state = cgpu->thr[0]->cgpu_data;
  425. state->identify_requested = true;
  426. return true;
  427. }
  428. static
  429. void nanofury_shutdown(struct thr_info * const thr)
  430. {
  431. struct nanofury_state * const state = thr->cgpu_data;
  432. struct mcp2210_device * const mcp = state->mcp;
  433. if (mcp)
  434. nanofury_device_off(mcp, state);
  435. }
  436. 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)
  437. {
  438. struct thr_info * const thr = proc->thr[0];
  439. struct nanofury_state * const state = thr->cgpu_data;
  440. if (!strcasecmp(setting, "on"))
  441. {
  442. state->ledvalue = true;
  443. state->ledalternating = false;
  444. }
  445. else
  446. if (!strcasecmp(setting, "off"))
  447. state->ledvalue = state->ledalternating = false;
  448. else
  449. if (!strcasecmp(setting, "alternating"))
  450. state->ledalternating = true;
  451. else
  452. return "Invalid LED mode; must be on/off/alternating";
  453. if (!timer_isset(&state->identify_started))
  454. timer_set_now(&state->identify_started);
  455. return NULL;
  456. }
  457. static const struct bfg_set_device_definition nanofury_set_device_funcs[] = {
  458. {"baud", bitfury_set_baud, "SPI baud rate"},
  459. {"osc6_bits", bitfury_set_osc6_bits, "range 1-"BITFURY_MAX_OSC6_BITS_S" (slow to fast)"},
  460. {"ledmode", nanofury_set_ledmode, "on/off/alternating"},
  461. {NULL},
  462. };
  463. struct device_drv nanofury_drv = {
  464. .dname = "nanofury",
  465. .name = "NFY",
  466. .lowl_match = nanofury_lowl_match,
  467. .lowl_probe = nanofury_lowl_probe,
  468. .thread_init = nanofury_init,
  469. .thread_disable = nanofury_disable,
  470. .thread_enable = nanofury_enable,
  471. .reinit_device = nanofury_reinit,
  472. .thread_shutdown = nanofury_shutdown,
  473. .minerloop = minerloop_async,
  474. .job_prepare = bitfury_job_prepare,
  475. .job_start = bitfury_noop_job_start,
  476. .poll = nanofury_poll,
  477. .job_process_results = bitfury_job_process_results,
  478. .get_api_extra_device_detail = bitfury_api_device_detail,
  479. .get_api_extra_device_status = bitfury_api_device_status,
  480. .identify_device = nanofury_identify,
  481. #ifdef HAVE_CURSES
  482. .proc_wlogprint_status = bitfury_wlogprint_status,
  483. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  484. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  485. #endif
  486. };