driver-nanofury.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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, MGV_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) == MGV_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 ? MGV_HIGH : MGV_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 ? MGV_HIGH : MGV_LOW))
  126. return false;
  127. if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN0, poweron ? MGV_LOW : MGV_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 (MGV_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, MGV_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) != MGV_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) != MGV_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) != MGV_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 spidevc.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. state->mcp = mcp;
  301. port->userp = state;
  302. for (proc = cgpu; proc; (proc = proc->next_proc), ++bitfury)
  303. {
  304. struct thr_info * const mythr = proc->thr[0];
  305. *bitfury = (struct bitfury_device){
  306. .spi = port,
  307. .fasync = proc->proc_id,
  308. };
  309. proc->device_data = bitfury;
  310. mythr->cgpu_data = state;
  311. bitfury->osc6_bits = 50;
  312. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  313. bitfury_init_chip(proc);
  314. proc->status = LIFE_INIT2;
  315. }
  316. nanofury_send_led_gpio(state);
  317. timer_set_now(&thr->tv_poll);
  318. return true;
  319. }
  320. static
  321. void nanofury_disable(struct thr_info * const thr)
  322. {
  323. struct nanofury_state * const state = thr->cgpu_data;
  324. struct mcp2210_device * const mcp = state->mcp;
  325. bitfury_disable(thr);
  326. nanofury_device_off(mcp);
  327. }
  328. static
  329. void nanofury_enable(struct thr_info * const thr)
  330. {
  331. struct nanofury_state * const state = thr->cgpu_data;
  332. struct mcp2210_device * const mcp = state->mcp;
  333. nanofury_checkport(mcp, state->current_baud);
  334. nanofury_send_led_gpio(state);
  335. bitfury_enable(thr);
  336. }
  337. static
  338. void nanofury_reinit(struct cgpu_info * const cgpu)
  339. {
  340. struct thr_info * const thr = cgpu->thr[0];
  341. struct nanofury_state * const state = thr->cgpu_data;
  342. struct mcp2210_device * const mcp = state->mcp;
  343. nanofury_device_off(mcp);
  344. cgsleep_ms(1);
  345. nanofury_enable(thr);
  346. }
  347. static
  348. double _nanofury_total_diff1(struct cgpu_info * const dev)
  349. {
  350. double d = 0.;
  351. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  352. d += proc->diff1;
  353. return d;
  354. }
  355. static
  356. void nanofury_poll(struct thr_info * const thr)
  357. {
  358. struct cgpu_info * const dev = thr->cgpu;
  359. struct nanofury_state * const state = thr->cgpu_data;
  360. struct mcp2210_device * const mcp = state->mcp;
  361. double diff1_before = 0.;
  362. if (state->identify_requested)
  363. {
  364. if (!timer_isset(&state->identify_started))
  365. mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? MGV_LOW : MGV_HIGH);
  366. timer_set_delay_from_now(&state->identify_started, 5000000);
  367. state->identify_requested = false;
  368. }
  369. if (state->ledalternating && !timer_isset(&state->identify_started))
  370. diff1_before = _nanofury_total_diff1(dev);
  371. bitfury_do_io(thr);
  372. if (state->ledalternating && (timer_isset(&state->identify_started) || diff1_before != _nanofury_total_diff1(dev)))
  373. nanofury_do_led_alternating(state);
  374. if (timer_passed(&state->identify_started, NULL))
  375. {
  376. // Also used when setting ledmode
  377. nanofury_send_led_gpio(state);
  378. timer_unset(&state->identify_started);
  379. }
  380. }
  381. static
  382. bool nanofury_identify(struct cgpu_info * const cgpu)
  383. {
  384. struct nanofury_state * const state = cgpu->thr[0]->cgpu_data;
  385. state->identify_requested = true;
  386. return true;
  387. }
  388. static
  389. void nanofury_shutdown(struct thr_info * const thr)
  390. {
  391. struct nanofury_state * const state = thr->cgpu_data;
  392. struct mcp2210_device * const mcp = state->mcp;
  393. if (mcp)
  394. nanofury_device_off(mcp);
  395. }
  396. 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)
  397. {
  398. struct thr_info * const thr = proc->thr[0];
  399. struct nanofury_state * const state = thr->cgpu_data;
  400. if (!strcasecmp(setting, "on"))
  401. {
  402. state->ledvalue = true;
  403. state->ledalternating = false;
  404. }
  405. else
  406. if (!strcasecmp(setting, "off"))
  407. state->ledvalue = state->ledalternating = false;
  408. else
  409. if (!strcasecmp(setting, "alternating"))
  410. state->ledalternating = true;
  411. else
  412. return "Invalid LED mode; must be on/off/alternating";
  413. if (!timer_isset(&state->identify_started))
  414. timer_set_now(&state->identify_started);
  415. return NULL;
  416. }
  417. static const struct bfg_set_device_definition nanofury_set_device_funcs[] = {
  418. {"baud", bitfury_set_baud, "SPI baud rate"},
  419. {"osc6_bits", bitfury_set_osc6_bits, "range 1-"BITFURY_MAX_OSC6_BITS_S" (slow to fast)"},
  420. {"ledmode", nanofury_set_ledmode, "on/off/alternating"},
  421. {NULL},
  422. };
  423. struct device_drv nanofury_drv = {
  424. .dname = "nanofury",
  425. .name = "NFY",
  426. .lowl_match = nanofury_lowl_match,
  427. .lowl_probe = nanofury_lowl_probe,
  428. .thread_init = nanofury_init,
  429. .thread_disable = nanofury_disable,
  430. .thread_enable = nanofury_enable,
  431. .reinit_device = nanofury_reinit,
  432. .thread_shutdown = nanofury_shutdown,
  433. .minerloop = minerloop_async,
  434. .job_prepare = bitfury_job_prepare,
  435. .job_start = bitfury_noop_job_start,
  436. .poll = nanofury_poll,
  437. .job_process_results = bitfury_job_process_results,
  438. .get_api_extra_device_detail = bitfury_api_device_detail,
  439. .get_api_extra_device_status = bitfury_api_device_status,
  440. .identify_device = nanofury_identify,
  441. #ifdef HAVE_CURSES
  442. .proc_wlogprint_status = bitfury_wlogprint_status,
  443. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  444. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  445. #endif
  446. };