driver-nanofury.c 14 KB

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