driver-minion.c 18 KB

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