driver-minion.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stddef.h>
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <linux/spi/spidev.h>
  16. #include <utlist.h>
  17. #include "deviceapi.h"
  18. #include "logging.h"
  19. #include "lowl-spi.h"
  20. #include "miner.h"
  21. #include "util.h"
  22. static const uint8_t minion_max_chipid = 0x1f;
  23. static const uint8_t minion_chip_signature[] = {0x44, 0x8a, 0xac, 0xb1};
  24. static const unsigned minion_max_queued = 0x10;
  25. static const unsigned minion_poll_us = 10000;
  26. enum minion_register {
  27. MRA_SIGNATURE = 0x00,
  28. MRA_STATUS = 0x01,
  29. MRA_PLL_CFG = 0x04,
  30. MRA_MISC_CTL = 0x06,
  31. MRA_RESET = 0x07,
  32. MRA_FIFO_STATUS = 0x0b,
  33. MRA_CORE_EN_ = 0x10,
  34. MRA_RESULT = 0x20,
  35. MRA_TASK = 0x30,
  36. MRA_NONCE_START = 0x70,
  37. MRA_NONCE_INC = 0x71,
  38. };
  39. struct minion_chip {
  40. uint8_t chipid;
  41. uint8_t core_count;
  42. uint8_t core_enabled_count;
  43. uint16_t next_taskid;
  44. struct cgpu_info *first_proc;
  45. unsigned queue_count;
  46. uint32_t core_nonce_inc;
  47. uint32_t pllcfg_asserted;
  48. uint32_t pllcfg_desired;
  49. };
  50. struct minion_bus {
  51. struct spi_port *spi;
  52. };
  53. static const uint8_t minion_crystal_mhz = 12;
  54. static
  55. uint32_t minion_freq_to_pllcfg(unsigned freq)
  56. {
  57. uint32_t rv;
  58. uint8_t * const pllcfg = (void*)&rv;
  59. uint8_t best_rem = 12, pll_dm = 1;
  60. for (uint8_t try_dm = 1; try_dm <= 8; ++try_dm)
  61. {
  62. const unsigned x = freq * try_dm;
  63. if (x > 0x100 * minion_crystal_mhz)
  64. // We'd overflow pll_dn to continue
  65. break;
  66. const uint8_t rem = x % minion_crystal_mhz;
  67. if (rem > best_rem)
  68. continue;
  69. best_rem = rem;
  70. pll_dm = try_dm;
  71. if (!rem)
  72. break;
  73. }
  74. const unsigned pll_dn = freq * pll_dm / minion_crystal_mhz;
  75. freq = pll_dn * minion_crystal_mhz / pll_dm;
  76. const uint8_t pll_cont = ((freq - 800) / 300); // 2 bits
  77. static const uint8_t pll_dp = 0; // 3 bits
  78. static const uint8_t pll_byp = 0; // 1 bit
  79. static const uint8_t pll_div2 = 0; // 1 bit
  80. static const uint8_t sys_div = 1; // 3 bits
  81. pllcfg[0] = pll_dn - 1;
  82. pllcfg[1] = (pll_dm - 1) | (pll_dp << 4);
  83. pllcfg[2] = pll_cont | (pll_byp << 2) | (pll_div2 << 4) | (sys_div << 5);
  84. pllcfg[3] = 0;
  85. return rv;
  86. }
  87. static
  88. unsigned minion_pllcfg_to_freq(const uint32_t in_pllcfg)
  89. {
  90. const uint8_t * const pllcfg = (void*)&in_pllcfg;
  91. const unsigned pll_dn = (unsigned)pllcfg[0] + 1;
  92. const uint8_t pll_dm = (pllcfg[1] & 0xf) + 1;
  93. const unsigned freq = pll_dn * minion_crystal_mhz / pll_dm;
  94. // FIXME: How to interpret the rest of the pll cfg?
  95. if (minion_freq_to_pllcfg(freq) != in_pllcfg)
  96. return 0;
  97. return freq;
  98. }
  99. static
  100. void minion_get(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, void * const buf, const size_t bufsz)
  101. {
  102. const uint8_t header[] = {chipid, addr | 0x80, bufsz & 0xff, bufsz >> 8};
  103. spi_clear_buf(spi);
  104. spi_emit_buf(spi, header, sizeof(header));
  105. uint8_t dummy[bufsz];
  106. memset(dummy, 0xff, bufsz);
  107. spi_emit_buf(spi, dummy, bufsz);
  108. spi_txrx(spi);
  109. uint8_t * const rdbuf = spi_getrxbuf(spi);
  110. memcpy(buf, &rdbuf[sizeof(header)], bufsz);
  111. }
  112. static
  113. void minion_set(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, const void * const buf, const size_t bufsz)
  114. {
  115. const uint8_t header[] = {chipid, addr, bufsz & 0xff, bufsz >> 8};
  116. spi_clear_buf(spi);
  117. spi_emit_buf(spi, header, sizeof(header));
  118. spi_emit_buf(spi, buf, bufsz);
  119. spi_txrx(spi);
  120. }
  121. static
  122. unsigned minion_count_cores(struct spi_port * const spi)
  123. {
  124. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  125. unsigned total_core_count = 0;
  126. for (unsigned chipid = 0; chipid <= minion_max_chipid; ++chipid)
  127. {
  128. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  129. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  130. {
  131. for (unsigned i = 0; i < sizeof(minion_chip_signature); ++i)
  132. {
  133. if (buf[i] != 0xff)
  134. {
  135. char hex[(sizeof(minion_chip_signature) * 2) + 1];
  136. bin2hex(hex, buf, sizeof(minion_chip_signature));
  137. applog(LOG_DEBUG, "%s: chipid %u: Bad signature (%s)", spi->repr, chipid, hex);
  138. break;
  139. }
  140. }
  141. continue;
  142. }
  143. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  144. const uint8_t core_count = buf[2];
  145. applog(LOG_DEBUG, "%s: chipid %u: Found %u cores", spi->repr, chipid, core_count);
  146. total_core_count += core_count;
  147. }
  148. return total_core_count;
  149. }
  150. static inline
  151. void minion_config_pll(struct spi_port * const spi, struct minion_chip * const chip)
  152. {
  153. if (chip->pllcfg_asserted == chip->pllcfg_desired)
  154. return;
  155. const uint8_t chipid = chip->chipid;
  156. minion_set(spi, chipid, MRA_PLL_CFG, &chip->pllcfg_desired, 4);
  157. chip->pllcfg_asserted = chip->pllcfg_desired;
  158. }
  159. static inline
  160. void minion_core_enable_register_position(const uint8_t coreid, uint8_t * const corereg, uint8_t * const corebyte, uint8_t * const corebit)
  161. {
  162. *corereg = MRA_CORE_EN_ + (coreid >> 5);
  163. *corebyte = (coreid >> 3) % 4;
  164. *corebit = 1 << (coreid % 8);
  165. }
  166. static
  167. bool minion_init(struct thr_info * const thr)
  168. {
  169. struct cgpu_info * const dev = thr->cgpu, *proc = dev;
  170. struct minion_bus * const mbus = dev->device_data;
  171. struct spi_port * const spi = mbus->spi;
  172. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  173. struct minion_chip * const chips = malloc(sizeof(*chips) * ((size_t)minion_max_chipid + 1));
  174. for (unsigned chipid = 0; proc; ++chipid)
  175. {
  176. struct minion_chip * const chip = &chips[chipid];
  177. spi->repr = proc->proc_repr;
  178. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  179. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  180. continue;
  181. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  182. if (!buf[2])
  183. continue;
  184. static const uint8_t resetcmd[4] = {0xff, 0xff, 0xa5, 0xf5};
  185. minion_set(spi, chipid, MRA_RESET, resetcmd, sizeof(resetcmd));
  186. *chip = (struct minion_chip){
  187. .chipid = chipid,
  188. .core_count = buf[2],
  189. .first_proc = proc,
  190. .pllcfg_desired = minion_freq_to_pllcfg(900),
  191. };
  192. minion_set(spi, chipid, MRA_NONCE_START, "\0\0\0\0", 4);
  193. chip->core_nonce_inc = 0xffffffff / chip->core_count;
  194. pk_u32le(buf, 0, chip->core_nonce_inc);
  195. minion_set(spi, chipid, MRA_NONCE_INC, buf, 4);
  196. minion_get(spi, chipid, MRA_PLL_CFG, &chip->pllcfg_asserted, 4);
  197. minion_get(spi, chipid, MRA_MISC_CTL, buf, 4);
  198. buf[0] &= ~(1 << 4); // Unpause cores
  199. buf[0] &= ~(1 << 3); // Unpause queue
  200. buf[0] |= 1 << 2; // Enable "no nonce" result reports
  201. buf[0] &= ~(1 << 1); // Disable test mode
  202. minion_set(spi, chipid, MRA_MISC_CTL, buf, 4);
  203. timer_set_delay_from_now(&proc->thr[0]->tv_poll, minion_poll_us);
  204. for (unsigned coreid = 0; coreid < chip->core_count; ++coreid)
  205. {
  206. struct thr_info * const thr = proc->thr[0];
  207. uint8_t corereg, corebyte, corebit;
  208. minion_core_enable_register_position(coreid, &corereg, &corebyte, &corebit);
  209. if (coreid % 0x20 == 0)
  210. {
  211. spi->repr = proc->proc_repr;
  212. minion_get(spi, chipid, corereg, buf, 4);
  213. }
  214. if (buf[corebyte] & corebit)
  215. ++chip->core_enabled_count;
  216. else
  217. proc->deven = DEV_DISABLED;
  218. thr->cgpu_data = chip;
  219. proc = proc->next_proc;
  220. }
  221. }
  222. return true;
  223. }
  224. static
  225. bool minion_queue_full(struct minion_chip * const chip)
  226. {
  227. struct cgpu_info *proc = chip->first_proc;
  228. struct thr_info *thr = proc->thr[0];
  229. const bool full = (chip->queue_count >= minion_max_queued);
  230. if (full != thr->queue_full)
  231. {
  232. for (unsigned i = 0; i < chip->core_count; (proc = proc->next_proc), ++i)
  233. {
  234. thr = proc->thr[0];
  235. thr->queue_full = full;
  236. }
  237. }
  238. return full;
  239. }
  240. static
  241. void minion_core_enabledisable(struct thr_info * const thr, const bool enable)
  242. {
  243. struct cgpu_info * const proc = thr->cgpu;
  244. struct minion_bus * const mbus = proc->device_data;
  245. struct minion_chip * const chip = thr->cgpu_data;
  246. struct spi_port * const spi = mbus->spi;
  247. const uint8_t chipid = chip->chipid;
  248. uint8_t coreid = 0;
  249. for (struct cgpu_info *p = chip->first_proc; p != proc; p = p->next_proc)
  250. ++coreid;
  251. uint8_t corereg, corebyte, corebit;
  252. minion_core_enable_register_position(coreid, &corereg, &corebyte, &corebit);
  253. uint8_t buf[4];
  254. minion_get(spi, chipid, corereg, buf, 4);
  255. const uint8_t oldbyte = buf[corebyte];
  256. if (enable)
  257. buf[corebyte] |= corebit;
  258. else
  259. buf[corebyte] &= ~corebit;
  260. if (buf[corebyte] != oldbyte)
  261. {
  262. minion_set(spi, chipid, corereg, buf, 4);
  263. chip->core_enabled_count += enable ? 1 : -1;
  264. }
  265. }
  266. static
  267. void minion_core_disable(struct thr_info * const thr)
  268. {
  269. minion_core_enabledisable(thr, false);
  270. }
  271. static
  272. void minion_core_enable(struct thr_info * const thr)
  273. {
  274. minion_core_enabledisable(thr, true);
  275. }
  276. static
  277. bool minion_queue_append(struct thr_info *thr, struct work * const work)
  278. {
  279. struct cgpu_info *proc = thr->cgpu;
  280. struct minion_bus * const mbus = proc->device_data;
  281. struct minion_chip * const chip = thr->cgpu_data;
  282. proc = chip->first_proc;
  283. thr = proc->thr[0];
  284. if (minion_queue_full(chip))
  285. return false;
  286. struct spi_port * const spi = mbus->spi;
  287. const uint8_t chipid = chip->chipid;
  288. uint8_t taskdata[0x30];
  289. spi->repr = proc->proc_repr;
  290. work->device_id = ++chip->next_taskid;
  291. work->tv_stamp.tv_sec = 1;
  292. work->blk.nonce = 0;
  293. pk_u16be(taskdata, 0, work->device_id);
  294. memset(&taskdata[2], 0, 2);
  295. memcpy(&taskdata[4], work->midstate, 0x20);
  296. memcpy(&taskdata[0x24], &work->data[0x40], 0xc);
  297. minion_config_pll(spi, chip);
  298. minion_set(spi, chipid, MRA_TASK, taskdata, sizeof(taskdata));
  299. DL_APPEND(thr->work_list, work);
  300. ++chip->queue_count;
  301. minion_queue_full(chip);
  302. return true;
  303. }
  304. static
  305. void minion_queue_flush(struct thr_info * const thr)
  306. {
  307. struct cgpu_info * const proc = thr->cgpu;
  308. struct minion_bus * const mbus = proc->device_data;
  309. struct minion_chip * const chip = thr->cgpu_data;
  310. if (proc != chip->first_proc)
  311. // Redundant, all queues flush at the same time
  312. return;
  313. const uint8_t chipid = chip->chipid;
  314. struct spi_port * const spi = mbus->spi;
  315. static const uint8_t flushcmd[4] = {0xfb, 0xff, 0xff, 0xff};
  316. minion_set(spi, chipid, MRA_RESET, flushcmd, sizeof(flushcmd));
  317. struct work *work;
  318. DL_FOREACH(thr->work_list, work)
  319. {
  320. work->tv_stamp.tv_sec = 0;
  321. }
  322. chip->queue_count = 0;
  323. minion_queue_full(chip);
  324. }
  325. static
  326. void minion_hashes_done(struct cgpu_info *proc, const uint8_t core_count, const uint64_t hashes)
  327. {
  328. for (int j = 0; j < core_count; (proc = proc->next_proc), ++j)
  329. {
  330. if (proc->deven != DEV_ENABLED)
  331. continue;
  332. struct thr_info * const thr = proc->thr[0];
  333. hashes_done2(thr, hashes, NULL);
  334. }
  335. }
  336. static
  337. void minion_poll(struct thr_info * const chip_thr)
  338. {
  339. struct cgpu_info * const first_proc = chip_thr->cgpu;
  340. struct minion_bus * const mbus = first_proc->device_data;
  341. struct minion_chip * const chip = chip_thr->cgpu_data;
  342. struct spi_port * const spi = mbus->spi;
  343. const uint8_t chipid = chip->chipid;
  344. spi->repr = first_proc->proc_repr;
  345. uint8_t buf[4];
  346. minion_get(spi, chipid, MRA_FIFO_STATUS, buf, 4);
  347. const uint8_t res_fifo_len = buf[0];
  348. if (res_fifo_len)
  349. {
  350. static const size_t resbuf_i_len = 8;
  351. const size_t resbuf_len = (size_t)res_fifo_len * resbuf_i_len;
  352. uint8_t resbuf[resbuf_len], *resbuf_i = resbuf;
  353. minion_get(spi, chipid, MRA_RESULT, resbuf, resbuf_len);
  354. for (unsigned i = 0; i < res_fifo_len; (resbuf_i += resbuf_i_len), ++i)
  355. {
  356. const uint8_t coreid = resbuf_i[2];
  357. work_device_id_t taskid = upk_u16be(resbuf_i, 0);
  358. const bool have_nonce = !(resbuf_i[3] & 0x80);
  359. struct cgpu_info *proc;
  360. struct thr_info *core_thr;
  361. bool clean = false;
  362. if (likely(coreid < chip->core_count))
  363. {
  364. proc = first_proc;
  365. for (int j = 0; j < coreid; ++j)
  366. proc = proc->next_proc;
  367. core_thr = proc->thr[0];
  368. }
  369. else
  370. {
  371. proc = first_proc;
  372. core_thr = proc->thr[0];
  373. inc_hw_errors_only(core_thr);
  374. applog(LOG_ERR, "%"PRIpreprv": Core id out of range (%u >= %u)", proc->proc_repr, coreid, chip->core_count);
  375. }
  376. struct work *work;
  377. DL_SEARCH_SCALAR(chip_thr->work_list, work, device_id, taskid);
  378. if (unlikely(!work))
  379. {
  380. inc_hw_errors_only(core_thr);
  381. applog(LOG_ERR, "%"PRIpreprv": Unknown task %"PRIwdi, proc->proc_repr, taskid);
  382. continue;
  383. }
  384. if (have_nonce)
  385. {
  386. const uint32_t nonce = upk_u32le(resbuf_i, 4);
  387. if (submit_nonce(core_thr, work, nonce))
  388. {
  389. clean = (coreid < chip->core_count);
  390. // It's only 0xffffffff if we prematurely considered it complete
  391. if (likely(work->blk.nonce != 0xffffffff))
  392. {
  393. uint32_t hashes = (nonce % chip->core_nonce_inc);
  394. if (hashes > work->blk.nonce)
  395. {
  396. hashes -= work->blk.nonce - 1;
  397. minion_hashes_done(first_proc, chip->core_count, hashes);
  398. work->blk.nonce = hashes + 1;
  399. }
  400. }
  401. }
  402. }
  403. else
  404. {
  405. const uint32_t hashes = chip->core_nonce_inc - work->blk.nonce;
  406. minion_hashes_done(first_proc, chip->core_count, hashes);
  407. work->blk.nonce = 0xffffffff;
  408. }
  409. // Flag previous work(s) as done, and delete them when we are sure
  410. struct work *work_tmp;
  411. DL_FOREACH_SAFE(chip_thr->work_list, work, work_tmp)
  412. {
  413. if (work->device_id == taskid)
  414. break;
  415. if (work->blk.nonce && work->blk.nonce != 0xffffffff)
  416. {
  417. // At least one nonce was found, assume the job completed
  418. const uint32_t hashes = chip->core_nonce_inc - work->blk.nonce;
  419. minion_hashes_done(first_proc, chip->core_count, hashes);
  420. work->blk.nonce = 0xffffffff;
  421. }
  422. if (work->tv_stamp.tv_sec)
  423. {
  424. --chip->queue_count;
  425. work->tv_stamp.tv_sec = 0;
  426. }
  427. if (clean)
  428. {
  429. DL_DELETE(chip_thr->work_list, work);
  430. free_work(work);
  431. }
  432. }
  433. }
  434. minion_queue_full(chip);
  435. }
  436. minion_config_pll(spi, chip);
  437. timer_set_delay_from_now(&chip_thr->tv_poll, minion_poll_us);
  438. }
  439. BFG_REGISTER_DRIVER(minion_drv)
  440. static
  441. bool minion_detect_one(const char * const devpath)
  442. {
  443. spi_init();
  444. struct spi_port *spi = malloc(sizeof(*spi));
  445. // Be careful, read lowl-spi.h comments for warnings
  446. memset(spi, 0, sizeof(*spi));
  447. spi->speed = 50000000;
  448. spi->mode = SPI_MODE_0;
  449. spi->bits = 8;
  450. spi->txrx = linux_spi_txrx2;
  451. if (spi_open(spi, devpath) < 0)
  452. {
  453. free(spi);
  454. applogr(false, LOG_ERR, "%s: Failed to open %s", minion_drv.dname, devpath);
  455. }
  456. spi->repr = minion_drv.dname;
  457. spi->logprio = LOG_WARNING;
  458. const unsigned total_core_count = minion_count_cores(spi);
  459. struct minion_bus * const mbus = malloc(sizeof(*mbus));
  460. *mbus = (struct minion_bus){
  461. .spi = spi,
  462. };
  463. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  464. *cgpu = (struct cgpu_info){
  465. .drv = &minion_drv,
  466. .device_path = strdup(devpath),
  467. .device_data = mbus,
  468. .deven = DEV_ENABLED,
  469. .procs = total_core_count,
  470. .threads = 1,
  471. };
  472. return add_cgpu(cgpu);
  473. }
  474. static
  475. int minion_detect_auto(void)
  476. {
  477. return minion_detect_one("/dev/spidev0.0") ? 1 : 0;
  478. }
  479. static
  480. void minion_detect(void)
  481. {
  482. generic_detect(&minion_drv, minion_detect_one, minion_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  483. }
  484. struct device_drv minion_drv = {
  485. .dname = "minion",
  486. .name = "MNN",
  487. .drv_detect = minion_detect,
  488. .thread_init = minion_init,
  489. .minerloop = minerloop_queue,
  490. .thread_disable = minion_core_disable,
  491. .thread_enable = minion_core_enable,
  492. .queue_append = minion_queue_append,
  493. .queue_flush = minion_queue_flush,
  494. .poll = minion_poll,
  495. };