driver-minion.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. static const unsigned minion_min_clock = 800;
  27. static const unsigned minion_max_clock = 1999;
  28. static const unsigned long minion_temp_interval_us = 5273437;
  29. enum minion_register {
  30. MRA_SIGNATURE = 0x00,
  31. MRA_STATUS = 0x01,
  32. MRA_TEMP_CFG = 0x03,
  33. MRA_PLL_CFG = 0x04,
  34. MRA_MISC_CTL = 0x06,
  35. MRA_RESET = 0x07,
  36. MRA_FIFO_STATUS = 0x0b,
  37. MRA_CORE_EN_ = 0x10,
  38. MRA_RESULT = 0x20,
  39. MRA_TASK = 0x30,
  40. MRA_NONCE_START = 0x70,
  41. MRA_NONCE_INC = 0x71,
  42. };
  43. struct minion_chip {
  44. uint8_t chipid;
  45. uint8_t core_count;
  46. uint8_t core_enabled_count;
  47. uint16_t next_taskid;
  48. struct cgpu_info *first_proc;
  49. unsigned queue_count;
  50. uint32_t core_nonce_inc;
  51. uint32_t pllcfg_asserted;
  52. uint32_t pllcfg_desired;
  53. struct timeval tv_read_temp;
  54. unsigned long timeout_us;
  55. struct timeval tv_timeout;
  56. };
  57. struct minion_bus {
  58. struct spi_port *spi;
  59. };
  60. static const uint8_t minion_crystal_mhz = 12;
  61. static
  62. uint32_t minion_freq_to_pllcfg(unsigned freq)
  63. {
  64. uint32_t rv;
  65. uint8_t * const pllcfg = (void*)&rv;
  66. uint8_t best_rem = 12, pll_dm = 1;
  67. for (uint8_t try_dm = 1; try_dm <= 8; ++try_dm)
  68. {
  69. const unsigned x = freq * try_dm;
  70. if (x > 0x100 * minion_crystal_mhz)
  71. // We'd overflow pll_dn to continue
  72. break;
  73. const uint8_t rem = x % minion_crystal_mhz;
  74. if (rem > best_rem)
  75. continue;
  76. best_rem = rem;
  77. pll_dm = try_dm;
  78. if (!rem)
  79. break;
  80. }
  81. const unsigned pll_dn = freq * pll_dm / minion_crystal_mhz;
  82. freq = pll_dn * minion_crystal_mhz / pll_dm;
  83. const uint8_t pll_cont = ((freq - 800) / 300); // 2 bits
  84. static const uint8_t pll_dp = 0; // 3 bits
  85. static const uint8_t pll_byp = 0; // 1 bit
  86. static const uint8_t pll_div2 = 0; // 1 bit
  87. static const uint8_t sys_div = 1; // 3 bits
  88. pllcfg[0] = pll_dn - 1;
  89. pllcfg[1] = (pll_dm - 1) | (pll_dp << 4);
  90. pllcfg[2] = pll_cont | (pll_byp << 2) | (pll_div2 << 4) | (sys_div << 5);
  91. pllcfg[3] = 0;
  92. return rv;
  93. }
  94. static
  95. unsigned minion_pllcfg_to_freq(const uint32_t in_pllcfg)
  96. {
  97. const uint8_t * const pllcfg = (void*)&in_pllcfg;
  98. const unsigned pll_dn = (unsigned)pllcfg[0] + 1;
  99. const uint8_t pll_dm = (pllcfg[1] & 0xf) + 1;
  100. const unsigned freq = pll_dn * minion_crystal_mhz / pll_dm;
  101. // FIXME: How to interpret the rest of the pll cfg?
  102. if (minion_freq_to_pllcfg(freq) != in_pllcfg)
  103. return 0;
  104. return freq;
  105. }
  106. static
  107. void minion_get(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, void * const buf, const size_t bufsz)
  108. {
  109. const uint8_t header[] = {chipid, addr | 0x80, bufsz & 0xff, bufsz >> 8};
  110. spi_clear_buf(spi);
  111. spi_emit_buf(spi, header, sizeof(header));
  112. uint8_t dummy[bufsz];
  113. memset(dummy, 0xff, bufsz);
  114. spi_emit_buf(spi, dummy, bufsz);
  115. spi_txrx(spi);
  116. uint8_t * const rdbuf = spi_getrxbuf(spi);
  117. memcpy(buf, &rdbuf[sizeof(header)], bufsz);
  118. }
  119. static
  120. void minion_set(struct spi_port * const spi, const uint8_t chipid, const uint8_t addr, const void * const buf, const size_t bufsz)
  121. {
  122. const uint8_t header[] = {chipid, addr, bufsz & 0xff, bufsz >> 8};
  123. spi_clear_buf(spi);
  124. spi_emit_buf(spi, header, sizeof(header));
  125. spi_emit_buf(spi, buf, bufsz);
  126. spi_txrx(spi);
  127. }
  128. static
  129. unsigned minion_count_cores(struct spi_port * const spi)
  130. {
  131. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  132. unsigned total_core_count = 0;
  133. for (unsigned chipid = 0; chipid <= minion_max_chipid; ++chipid)
  134. {
  135. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  136. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  137. {
  138. for (unsigned i = 0; i < sizeof(minion_chip_signature); ++i)
  139. {
  140. if (buf[i] != 0xff)
  141. {
  142. char hex[(sizeof(minion_chip_signature) * 2) + 1];
  143. bin2hex(hex, buf, sizeof(minion_chip_signature));
  144. applog(LOG_DEBUG, "%s: chipid %u: Bad signature (%s)", spi->repr, chipid, hex);
  145. break;
  146. }
  147. }
  148. continue;
  149. }
  150. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  151. const uint8_t core_count = buf[2];
  152. applog(LOG_DEBUG, "%s: chipid %u: Found %u cores", spi->repr, chipid, core_count);
  153. total_core_count += core_count;
  154. }
  155. return total_core_count;
  156. }
  157. static inline
  158. void minion_config_pll(struct spi_port * const spi, struct minion_chip * const chip)
  159. {
  160. if (chip->pllcfg_asserted == chip->pllcfg_desired)
  161. return;
  162. const uint8_t chipid = chip->chipid;
  163. minion_set(spi, chipid, MRA_PLL_CFG, &chip->pllcfg_desired, 4);
  164. chip->pllcfg_asserted = chip->pllcfg_desired;
  165. // NOTE: This assumes we only ever assert pllcfgs we can decode!
  166. chip->timeout_us = 0xffffffff / minion_pllcfg_to_freq(chip->pllcfg_asserted);
  167. timer_set_delay_from_now(&chip->tv_timeout, chip->timeout_us);
  168. }
  169. static inline
  170. void minion_core_enable_register_position(const uint8_t coreid, uint8_t * const corereg, uint8_t * const corebyte, uint8_t * const corebit)
  171. {
  172. *corereg = MRA_CORE_EN_ + (coreid >> 5);
  173. *corebyte = (coreid >> 3) % 4;
  174. *corebit = 1 << (coreid % 8);
  175. }
  176. static
  177. void minion_reinit(struct cgpu_info * const first_proc, struct minion_chip * const chip, const struct timeval * const tvp_now)
  178. {
  179. struct thr_info * const thr = first_proc->thr[0];
  180. struct minion_bus * const mbus = first_proc->device_data;
  181. struct spi_port * const spi = mbus->spi;
  182. const uint8_t chipid = chip->chipid;
  183. uint8_t buf[4];
  184. static const uint8_t resetcmd[4] = {0xff, 0xff, 0xa5, 0xf5};
  185. minion_set(spi, chipid, MRA_RESET, resetcmd, sizeof(resetcmd));
  186. minion_set(spi, chipid, MRA_NONCE_START, "\0\0\0\0", 4);
  187. chip->core_nonce_inc = 0xffffffff / chip->core_count;
  188. pk_u32le(buf, 0, chip->core_nonce_inc);
  189. minion_set(spi, chipid, MRA_NONCE_INC, buf, 4);
  190. minion_get(spi, chipid, MRA_TEMP_CFG, buf, 4);
  191. buf[0] &= ~(1 << 5); // Enable temperature sensor
  192. buf[0] &= ~(1 << 4); // 20 C precision (alternative is 40 C)
  193. minion_set(spi, chipid, MRA_TEMP_CFG, buf, 4);
  194. minion_get(spi, chipid, MRA_PLL_CFG, &chip->pllcfg_asserted, 4);
  195. minion_get(spi, chipid, MRA_MISC_CTL, buf, 4);
  196. buf[0] &= ~(1 << 4); // Unpause cores
  197. buf[0] &= ~(1 << 3); // Unpause queue
  198. buf[0] |= 1 << 2; // Enable "no nonce" result reports
  199. buf[0] &= ~(1 << 1); // Disable test mode
  200. minion_set(spi, chipid, MRA_MISC_CTL, buf, 4);
  201. thr->tv_poll = *tvp_now;
  202. chip->tv_read_temp = *tvp_now;
  203. }
  204. static
  205. void minion_reenable_cores(struct cgpu_info * const first_proc, struct minion_chip * const chip)
  206. {
  207. struct minion_bus * const mbus = first_proc->device_data;
  208. struct spi_port * const spi = mbus->spi;
  209. const uint8_t chipid = chip->chipid;
  210. uint8_t buf[4] = {0,0,0,0};
  211. struct cgpu_info *proc = first_proc;
  212. for (unsigned coreid = 0; coreid < chip->core_count; (proc = proc->next_proc), ++coreid)
  213. {
  214. uint8_t corereg, corebyte, corebit;
  215. minion_core_enable_register_position(coreid, &corereg, &corebyte, &corebit);
  216. if (proc->deven == DEV_ENABLED)
  217. buf[corebyte] |= corebit;
  218. if (coreid % 0x20 == 0x1f || coreid == chip->core_count - 1)
  219. minion_set(spi, chipid, corereg, buf, 4);
  220. }
  221. }
  222. static
  223. bool minion_init(struct thr_info * const thr)
  224. {
  225. struct cgpu_info * const dev = thr->cgpu, *proc = dev;
  226. struct minion_bus * const mbus = dev->device_data;
  227. struct spi_port * const spi = mbus->spi;
  228. uint8_t buf[max(4, sizeof(minion_chip_signature))];
  229. struct timeval tv_now;
  230. timer_set_now(&tv_now);
  231. struct minion_chip * const chips = malloc(sizeof(*chips) * ((size_t)minion_max_chipid + 1));
  232. for (unsigned chipid = 0; proc; ++chipid)
  233. {
  234. struct minion_chip * const chip = &chips[chipid];
  235. spi->repr = proc->proc_repr;
  236. minion_get(spi, chipid, MRA_SIGNATURE, buf, sizeof(minion_chip_signature));
  237. if (memcmp(buf, minion_chip_signature, sizeof(minion_chip_signature)))
  238. continue;
  239. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  240. if (!buf[2])
  241. continue;
  242. *chip = (struct minion_chip){
  243. .chipid = chipid,
  244. .core_count = buf[2],
  245. .first_proc = proc,
  246. .pllcfg_desired = minion_freq_to_pllcfg(900),
  247. };
  248. minion_reinit(proc, chip, &tv_now);
  249. for (unsigned coreid = 0; coreid < chip->core_count; ++coreid)
  250. {
  251. struct thr_info * const thr = proc->thr[0];
  252. uint8_t corereg, corebyte, corebit;
  253. minion_core_enable_register_position(coreid, &corereg, &corebyte, &corebit);
  254. if (coreid % 0x20 == 0)
  255. {
  256. spi->repr = proc->proc_repr;
  257. minion_get(spi, chipid, corereg, buf, 4);
  258. }
  259. if (buf[corebyte] & corebit)
  260. ++chip->core_enabled_count;
  261. else
  262. proc->deven = DEV_DISABLED;
  263. thr->cgpu_data = chip;
  264. proc = proc->next_proc;
  265. }
  266. }
  267. return true;
  268. }
  269. static
  270. bool minion_queue_full(struct minion_chip * const chip)
  271. {
  272. struct cgpu_info *proc = chip->first_proc;
  273. struct thr_info *thr = proc->thr[0];
  274. const bool full = (chip->queue_count >= minion_max_queued);
  275. if (full != thr->queue_full)
  276. {
  277. for (unsigned i = 0; i < chip->core_count; (proc = proc->next_proc), ++i)
  278. {
  279. thr = proc->thr[0];
  280. thr->queue_full = full;
  281. }
  282. }
  283. return full;
  284. }
  285. static
  286. void minion_core_enabledisable(struct thr_info * const thr, const bool enable)
  287. {
  288. struct cgpu_info * const proc = thr->cgpu;
  289. struct minion_bus * const mbus = proc->device_data;
  290. struct minion_chip * const chip = thr->cgpu_data;
  291. struct spi_port * const spi = mbus->spi;
  292. const uint8_t chipid = chip->chipid;
  293. uint8_t coreid = 0;
  294. for (struct cgpu_info *p = chip->first_proc; p != proc; p = p->next_proc)
  295. ++coreid;
  296. uint8_t corereg, corebyte, corebit;
  297. minion_core_enable_register_position(coreid, &corereg, &corebyte, &corebit);
  298. uint8_t buf[4];
  299. minion_get(spi, chipid, corereg, buf, 4);
  300. const uint8_t oldbyte = buf[corebyte];
  301. if (enable)
  302. buf[corebyte] |= corebit;
  303. else
  304. buf[corebyte] &= ~corebit;
  305. if (buf[corebyte] != oldbyte)
  306. {
  307. minion_set(spi, chipid, corereg, buf, 4);
  308. chip->core_enabled_count += enable ? 1 : -1;
  309. }
  310. }
  311. static
  312. void minion_core_disable(struct thr_info * const thr)
  313. {
  314. minion_core_enabledisable(thr, false);
  315. }
  316. static
  317. void minion_core_enable(struct thr_info * const thr)
  318. {
  319. minion_core_enabledisable(thr, true);
  320. }
  321. static
  322. bool minion_queue_append(struct thr_info *thr, struct work * const work)
  323. {
  324. struct cgpu_info *proc = thr->cgpu;
  325. struct minion_bus * const mbus = proc->device_data;
  326. struct minion_chip * const chip = thr->cgpu_data;
  327. proc = chip->first_proc;
  328. thr = proc->thr[0];
  329. if (minion_queue_full(chip))
  330. return false;
  331. struct spi_port * const spi = mbus->spi;
  332. const uint8_t chipid = chip->chipid;
  333. uint8_t taskdata[0x30];
  334. spi->repr = proc->proc_repr;
  335. work->device_id = ++chip->next_taskid;
  336. work->tv_stamp.tv_sec = 1;
  337. work->blk.nonce = 0;
  338. pk_u16be(taskdata, 0, work->device_id);
  339. memset(&taskdata[2], 0, 2);
  340. memcpy(&taskdata[4], work->midstate, 0x20);
  341. memcpy(&taskdata[0x24], &work->data[0x40], 0xc);
  342. minion_config_pll(spi, chip);
  343. minion_set(spi, chipid, MRA_TASK, taskdata, sizeof(taskdata));
  344. DL_APPEND(thr->work_list, work);
  345. ++chip->queue_count;
  346. minion_queue_full(chip);
  347. return true;
  348. }
  349. static void minion_refill_queue(struct thr_info *);
  350. static
  351. void minion_queue_flush(struct thr_info * const thr)
  352. {
  353. struct cgpu_info * const proc = thr->cgpu;
  354. struct minion_bus * const mbus = proc->device_data;
  355. struct minion_chip * const chip = thr->cgpu_data;
  356. if (proc != chip->first_proc)
  357. // Redundant, all queues flush at the same time
  358. return;
  359. const uint8_t chipid = chip->chipid;
  360. struct spi_port * const spi = mbus->spi;
  361. static const uint8_t flushcmd[4] = {0xfb, 0xff, 0xff, 0xff};
  362. minion_set(spi, chipid, MRA_RESET, flushcmd, sizeof(flushcmd));
  363. minion_refill_queue(thr);
  364. }
  365. static
  366. void minion_refill_queue(struct thr_info * const thr)
  367. {
  368. struct minion_chip * const chip = thr->cgpu_data;
  369. struct work *work;
  370. DL_FOREACH(thr->work_list, work)
  371. {
  372. work->tv_stamp.tv_sec = 0;
  373. }
  374. chip->queue_count = 0;
  375. minion_queue_full(chip);
  376. }
  377. static
  378. void minion_hashes_done(struct cgpu_info *proc, const uint8_t core_count, const uint64_t hashes)
  379. {
  380. for (int j = 0; j < core_count; (proc = proc->next_proc), ++j)
  381. {
  382. if (proc->deven != DEV_ENABLED)
  383. continue;
  384. struct thr_info * const thr = proc->thr[0];
  385. hashes_done2(thr, hashes, NULL);
  386. }
  387. }
  388. static
  389. void minion_poll(struct thr_info * const chip_thr)
  390. {
  391. struct cgpu_info * const first_proc = chip_thr->cgpu;
  392. struct minion_bus * const mbus = first_proc->device_data;
  393. struct minion_chip * const chip = chip_thr->cgpu_data;
  394. struct spi_port * const spi = mbus->spi;
  395. const uint8_t chipid = chip->chipid;
  396. spi->repr = first_proc->proc_repr;
  397. uint8_t buf[4];
  398. minion_get(spi, chipid, MRA_FIFO_STATUS, buf, 4);
  399. const uint8_t res_fifo_len = buf[0];
  400. if (res_fifo_len)
  401. {
  402. static const size_t resbuf_i_len = 8;
  403. const size_t resbuf_len = (size_t)res_fifo_len * resbuf_i_len;
  404. uint8_t resbuf[resbuf_len], *resbuf_i = resbuf;
  405. minion_get(spi, chipid, MRA_RESULT, resbuf, resbuf_len);
  406. for (unsigned i = 0; i < res_fifo_len; (resbuf_i += resbuf_i_len), ++i)
  407. {
  408. const uint8_t coreid = resbuf_i[2];
  409. work_device_id_t taskid = upk_u16be(resbuf_i, 0);
  410. const bool have_nonce = !(resbuf_i[3] & 0x80);
  411. struct cgpu_info *proc;
  412. struct thr_info *core_thr;
  413. bool clean = false;
  414. if (likely(coreid < chip->core_count))
  415. {
  416. proc = first_proc;
  417. for (int j = 0; j < coreid; ++j)
  418. proc = proc->next_proc;
  419. core_thr = proc->thr[0];
  420. }
  421. else
  422. {
  423. proc = first_proc;
  424. core_thr = proc->thr[0];
  425. inc_hw_errors_only(core_thr);
  426. applog(LOG_ERR, "%"PRIpreprv": Core id out of range (%u >= %u)", proc->proc_repr, coreid, chip->core_count);
  427. }
  428. struct work *work;
  429. DL_SEARCH_SCALAR(chip_thr->work_list, work, device_id, taskid);
  430. if (unlikely(!work))
  431. {
  432. inc_hw_errors_only(core_thr);
  433. applog(LOG_ERR, "%"PRIpreprv": Unknown task %"PRIwdi, proc->proc_repr, taskid);
  434. continue;
  435. }
  436. if (have_nonce)
  437. {
  438. const uint32_t nonce = upk_u32le(resbuf_i, 4);
  439. if (submit_nonce(core_thr, work, nonce))
  440. {
  441. clean = (coreid < chip->core_count);
  442. // It's only 0xffffffff if we prematurely considered it complete
  443. if (likely(work->blk.nonce != 0xffffffff))
  444. {
  445. uint32_t hashes = (nonce % chip->core_nonce_inc);
  446. if (hashes > work->blk.nonce)
  447. {
  448. hashes -= work->blk.nonce - 1;
  449. minion_hashes_done(first_proc, chip->core_count, hashes);
  450. work->blk.nonce = hashes + 1;
  451. }
  452. }
  453. }
  454. }
  455. else
  456. {
  457. const uint32_t hashes = chip->core_nonce_inc - work->blk.nonce;
  458. minion_hashes_done(first_proc, chip->core_count, hashes);
  459. work->blk.nonce = 0xffffffff;
  460. }
  461. // Flag previous work(s) as done, and delete them when we are sure
  462. struct work *work_tmp;
  463. DL_FOREACH_SAFE(chip_thr->work_list, work, work_tmp)
  464. {
  465. if (work->device_id == taskid)
  466. break;
  467. if (work->blk.nonce && work->blk.nonce != 0xffffffff)
  468. {
  469. // At least one nonce was found, assume the job completed
  470. const uint32_t hashes = chip->core_nonce_inc - work->blk.nonce;
  471. minion_hashes_done(first_proc, chip->core_count, hashes);
  472. work->blk.nonce = 0xffffffff;
  473. }
  474. if (work->tv_stamp.tv_sec)
  475. {
  476. --chip->queue_count;
  477. work->tv_stamp.tv_sec = 0;
  478. }
  479. if (clean)
  480. {
  481. DL_DELETE(chip_thr->work_list, work);
  482. free_work(work);
  483. }
  484. }
  485. }
  486. minion_queue_full(chip);
  487. }
  488. struct timeval tv_now;
  489. timer_set_now(&tv_now);
  490. if (timer_passed(&chip->tv_read_temp, &tv_now))
  491. {
  492. minion_get(spi, chipid, MRA_STATUS, buf, 4);
  493. const float temp = buf[3] * 20.;
  494. struct cgpu_info *proc = first_proc;
  495. for (int j = 0; j < chip->core_count; (proc = proc->next_proc), ++j)
  496. proc->temp = temp;
  497. timer_set_delay(&chip_thr->tv_poll, &tv_now, minion_temp_interval_us);
  498. }
  499. if (res_fifo_len)
  500. timer_set_delay(&chip->tv_timeout, &tv_now, chip->timeout_us);
  501. else
  502. if (timer_passed(&chip->tv_timeout, &tv_now))
  503. {
  504. applog(LOG_WARNING, "%"PRIpreprv": Chip timeout, reinitialising", first_proc->proc_repr);
  505. minion_reinit(first_proc, chip, &tv_now);
  506. minion_reenable_cores(first_proc, chip);
  507. minion_refill_queue(chip_thr);
  508. }
  509. minion_config_pll(spi, chip);
  510. timer_set_delay(&chip_thr->tv_poll, &tv_now, minion_poll_us);
  511. }
  512. static
  513. struct api_data *minion_get_api_extra_device_status(struct cgpu_info * const proc)
  514. {
  515. struct thr_info * const thr = proc->thr[0];
  516. struct minion_chip * const chip = thr->cgpu_data;
  517. struct api_data *root = NULL;
  518. double d;
  519. d = minion_pllcfg_to_freq(chip->pllcfg_asserted);
  520. if (d > 0)
  521. root = api_add_freq(root, "Frequency", &d, true);
  522. return root;
  523. }
  524. static
  525. const char *minion_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  526. {
  527. struct thr_info * const thr = proc->thr[0];
  528. struct minion_chip * const chip = thr->cgpu_data;
  529. const int nv = atoi(newvalue);
  530. if (nv < minion_min_clock || nv > minion_max_clock)
  531. {
  532. sprintf(replybuf, "Clock frequency must be within range of %u-%u MHz", minion_min_clock, minion_max_clock);
  533. return replybuf;
  534. }
  535. const uint32_t pllcfg = minion_freq_to_pllcfg(nv);
  536. chip->pllcfg_desired = pllcfg;
  537. return NULL;
  538. }
  539. static const struct bfg_set_device_definition minion_set_device_funcs[] = {
  540. {"clock", minion_set_clock, "clock frequency"},
  541. {NULL},
  542. };
  543. #ifdef HAVE_CURSES
  544. static
  545. void minion_tui_wlogprint_choices(struct cgpu_info * const proc)
  546. {
  547. wlogprint("[C]lock speed ");
  548. }
  549. static
  550. const char *minion_tui_handle_choice(struct cgpu_info * const proc, const int input)
  551. {
  552. struct thr_info * const thr = proc->thr[0];
  553. struct minion_chip * const chip = thr->cgpu_data;
  554. char buf[0x100];
  555. switch (input)
  556. {
  557. case 'c': case 'C':
  558. {
  559. sprintf(buf, "Set clock speed (range %d-%d)", minion_min_clock, minion_max_clock);
  560. const int nv = curses_int(buf);
  561. if (nv < minion_min_clock || nv > minion_max_clock)
  562. return "Invalid clock speed\n";
  563. const uint32_t pllcfg = minion_freq_to_pllcfg(nv);
  564. chip->pllcfg_desired = pllcfg;
  565. return "Clock speed changed\n";
  566. }
  567. }
  568. return NULL;
  569. }
  570. static
  571. void minion_wlogprint_status(struct cgpu_info * const proc)
  572. {
  573. struct thr_info * const thr = proc->thr[0];
  574. struct minion_chip * const chip = thr->cgpu_data;
  575. const unsigned freq = minion_pllcfg_to_freq(chip->pllcfg_asserted);
  576. if (freq)
  577. wlogprint("Clock speed: %u\n", freq);
  578. }
  579. #endif
  580. BFG_REGISTER_DRIVER(minion_drv)
  581. static
  582. bool minion_detect_one(const char * const devpath)
  583. {
  584. spi_init();
  585. struct spi_port *spi = malloc(sizeof(*spi));
  586. // Be careful, read lowl-spi.h comments for warnings
  587. memset(spi, 0, sizeof(*spi));
  588. spi->speed = 50000000;
  589. spi->mode = SPI_MODE_0;
  590. spi->bits = 8;
  591. spi->txrx = linux_spi_txrx2;
  592. if (spi_open(spi, devpath) < 0)
  593. {
  594. free(spi);
  595. applogr(false, LOG_ERR, "%s: Failed to open %s", minion_drv.dname, devpath);
  596. }
  597. spi->repr = minion_drv.dname;
  598. spi->logprio = LOG_WARNING;
  599. const unsigned total_core_count = minion_count_cores(spi);
  600. struct minion_bus * const mbus = malloc(sizeof(*mbus));
  601. *mbus = (struct minion_bus){
  602. .spi = spi,
  603. };
  604. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  605. *cgpu = (struct cgpu_info){
  606. .drv = &minion_drv,
  607. .device_path = strdup(devpath),
  608. .device_data = mbus,
  609. .set_device_funcs = minion_set_device_funcs,
  610. .deven = DEV_ENABLED,
  611. .procs = total_core_count,
  612. .threads = 1,
  613. };
  614. return add_cgpu(cgpu);
  615. }
  616. static
  617. int minion_detect_auto(void)
  618. {
  619. return minion_detect_one("/dev/spidev0.0") ? 1 : 0;
  620. }
  621. static
  622. void minion_detect(void)
  623. {
  624. generic_detect(&minion_drv, minion_detect_one, minion_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  625. }
  626. struct device_drv minion_drv = {
  627. .dname = "minion",
  628. .name = "MNN",
  629. .drv_detect = minion_detect,
  630. .thread_init = minion_init,
  631. .minerloop = minerloop_queue,
  632. .thread_disable = minion_core_disable,
  633. .thread_enable = minion_core_enable,
  634. .queue_append = minion_queue_append,
  635. .queue_flush = minion_queue_flush,
  636. .poll = minion_poll,
  637. .get_api_extra_device_status = minion_get_api_extra_device_status,
  638. #ifdef HAVE_CURSES
  639. .proc_wlogprint_status = minion_wlogprint_status,
  640. .proc_tui_wlogprint_choices = minion_tui_wlogprint_choices,
  641. .proc_tui_handle_choice = minion_tui_handle_choice,
  642. #endif
  643. };